blob: 459fd0d0b19ade58c1f65d2b5f1e536aedeee2ee [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 *
502PyEval_EvalFrame(PyFrameObject *f)
Guido van Rossum374a9221991-04-04 10:40:29 +0000503{
Guido van Rossum950361c1997-01-24 13:49:28 +0000504#ifdef DXPAIRS
505 int lastopcode = 0;
506#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +0000507 register PyObject **stack_pointer; /* Next free slot in value stack */
Guido van Rossum374a9221991-04-04 10:40:29 +0000508 register unsigned char *next_instr;
Armin Rigo8817fcd2004-06-17 10:22:40 +0000509 register int opcode; /* Current opcode */
510 register int oparg; /* Current opcode argument, if any */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000511 register enum why_code why; /* Reason for block stack unwind */
Guido van Rossum374a9221991-04-04 10:40:29 +0000512 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000513 register PyObject *x; /* Result object -- NULL if error */
514 register PyObject *v; /* Temporary objects popped off stack */
515 register PyObject *w;
516 register PyObject *u;
517 register PyObject *t;
Barry Warsaw23c9ec82000-08-21 15:44:01 +0000518 register PyObject *stream = NULL; /* for PRINT opcodes */
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000519 register PyObject **fastlocals, **freevars;
Guido van Rossum014518f1998-11-23 21:09:51 +0000520 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000521 PyThreadState *tstate = PyThreadState_GET();
Tim Peters5ca576e2001-06-18 22:08:13 +0000522 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000523
Tim Peters8a5c3c72004-04-05 19:36:21 +0000524 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000525
526 not (instr_lb <= current_bytecode_offset < instr_ub)
527
Tim Peters8a5c3c72004-04-05 19:36:21 +0000528 is true when the line being executed has changed. The
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000529 initial values are such as to make this false the first
530 time it is tested. */
Armin Rigobf57a142004-03-22 19:24:58 +0000531 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000532
Guido van Rossumd076c731998-10-07 19:42:25 +0000533 unsigned char *first_instr;
Skip Montanaro04d80f82002-08-04 21:03:35 +0000534 PyObject *names;
535 PyObject *consts;
Guido van Rossum96a42c81992-01-12 02:29:51 +0000536#ifdef LLTRACE
Guido van Rossumacbe8da1993-04-15 15:33:52 +0000537 int lltrace;
Guido van Rossum374a9221991-04-04 10:40:29 +0000538#endif
Guido van Rossum408027e1996-12-30 16:17:54 +0000539#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000540 /* Make it easier to find out where we are with a debugger */
Tim Peters5ca576e2001-06-18 22:08:13 +0000541 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000542#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000543
Neal Norwitza81d2202002-07-14 00:27:26 +0000544/* Tuple access macros */
545
546#ifndef Py_DEBUG
547#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
548#else
549#define GETITEM(v, i) PyTuple_GetItem((v), (i))
550#endif
551
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000552#ifdef WITH_TSC
553/* Use Pentium timestamp counter to mark certain events:
554 inst0 -- beginning of switch statement for opcode dispatch
555 inst1 -- end of switch statement (may be skipped)
556 loop0 -- the top of the mainloop
557 loop1 -- place where control returns again to top of mainloop
558 (may be skipped)
559 intr1 -- beginning of long interruption
560 intr2 -- end of long interruption
561
562 Many opcodes call out to helper C functions. In some cases, the
563 time in those functions should be counted towards the time for the
564 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
565 calls another Python function; there's no point in charge all the
566 bytecode executed by the called function to the caller.
567
568 It's hard to make a useful judgement statically. In the presence
569 of operator overloading, it's impossible to tell if a call will
570 execute new Python code or not.
571
572 It's a case-by-case judgement. I'll use intr1 for the following
573 cases:
574
575 EXEC_STMT
576 IMPORT_STAR
577 IMPORT_FROM
578 CALL_FUNCTION (and friends)
579
580 */
581 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
582 int ticked = 0;
583
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000584 READ_TIMESTAMP(inst0);
585 READ_TIMESTAMP(inst1);
586 READ_TIMESTAMP(loop0);
587 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000588
589 /* shut up the compiler */
590 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000591#endif
592
Guido van Rossum374a9221991-04-04 10:40:29 +0000593/* Code access macros */
594
Guido van Rossumd076c731998-10-07 19:42:25 +0000595#define INSTR_OFFSET() (next_instr - first_instr)
Guido van Rossum374a9221991-04-04 10:40:29 +0000596#define NEXTOP() (*next_instr++)
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000597#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000598#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
Guido van Rossumd076c731998-10-07 19:42:25 +0000599#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000600#define JUMPBY(x) (next_instr += (x))
601
Raymond Hettingerf606f872003-03-16 03:11:04 +0000602/* OpCode prediction macros
603 Some opcodes tend to come in pairs thus making it possible to predict
604 the second code when the first is run. For example, COMPARE_OP is often
605 followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And, those opcodes are often
606 followed by a POP_TOP.
607
608 Verifying the prediction costs a single high-speed test of register
Raymond Hettingerac2072922003-03-16 15:41:11 +0000609 variable against a constant. If the pairing was good, then the
Raymond Hettingerf606f872003-03-16 03:11:04 +0000610 processor has a high likelihood of making its own successful branch
611 prediction which results in a nearly zero overhead transition to the
612 next opcode.
613
614 A successful prediction saves a trip through the eval-loop including
615 its two unpredictable branches, the HASARG test and the switch-case.
Raymond Hettingera7216982004-02-08 19:59:27 +0000616
Tim Peters8a5c3c72004-04-05 19:36:21 +0000617 If collecting opcode statistics, turn off prediction so that
618 statistics are accurately maintained (the predictions bypass
Raymond Hettingera7216982004-02-08 19:59:27 +0000619 the opcode frequency counter updates).
Raymond Hettingerf606f872003-03-16 03:11:04 +0000620*/
621
Raymond Hettingera7216982004-02-08 19:59:27 +0000622#ifdef DYNAMIC_EXECUTION_PROFILE
623#define PREDICT(op) if (0) goto PRED_##op
624#else
Raymond Hettingerac2072922003-03-16 15:41:11 +0000625#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000626#endif
627
Raymond Hettingerf606f872003-03-16 03:11:04 +0000628#define PREDICTED(op) PRED_##op: next_instr++
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000629#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Raymond Hettingerf606f872003-03-16 03:11:04 +0000630
Guido van Rossum374a9221991-04-04 10:40:29 +0000631/* Stack manipulation macros */
632
633#define STACK_LEVEL() (stack_pointer - f->f_valuestack)
634#define EMPTY() (STACK_LEVEL() == 0)
635#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000636#define SECOND() (stack_pointer[-2])
637#define THIRD() (stack_pointer[-3])
638#define FOURTH() (stack_pointer[-4])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000639#define SET_TOP(v) (stack_pointer[-1] = (v))
640#define SET_SECOND(v) (stack_pointer[-2] = (v))
641#define SET_THIRD(v) (stack_pointer[-3] = (v))
642#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000643#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000644#define BASIC_PUSH(v) (*stack_pointer++ = (v))
645#define BASIC_POP() (*--stack_pointer)
646
Guido van Rossum96a42c81992-01-12 02:29:51 +0000647#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000648#define PUSH(v) { (void)(BASIC_PUSH(v), \
649 lltrace && prtrace(TOP(), "push")); \
650 assert(STACK_LEVEL() <= f->f_stacksize); }
Fred Drakede26cfc2001-10-13 06:11:28 +0000651#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000652#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
653 lltrace && prtrace(TOP(), "stackadj")); \
654 assert(STACK_LEVEL() <= f->f_stacksize); }
Guido van Rossum374a9221991-04-04 10:40:29 +0000655#else
656#define PUSH(v) BASIC_PUSH(v)
657#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000658#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000659#endif
660
Guido van Rossum681d79a1995-07-18 14:51:37 +0000661/* Local variable macros */
662
663#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000664
665/* The SETLOCAL() macro must not DECREF the local variable in-place and
666 then store the new value; it must copy the old value to a temporary
667 value, then store the new value, and then DECREF the temporary value.
668 This is because it is possible that during the DECREF the frame is
669 accessed by other code (e.g. a __del__ method or gc.collect()) and the
670 variable would be pointing to already-freed memory. */
671#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
672 GETLOCAL(i) = value; \
673 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000674
Guido van Rossuma027efa1997-05-05 20:56:21 +0000675/* Start of code */
676
Tim Peters5ca576e2001-06-18 22:08:13 +0000677 if (f == NULL)
678 return NULL;
679
Armin Rigo1d313ab2003-10-25 14:33:09 +0000680 /* push frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000681 if (Py_EnterRecursiveCall(""))
Armin Rigo1d313ab2003-10-25 14:33:09 +0000682 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000683
Tim Peters5ca576e2001-06-18 22:08:13 +0000684 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000685
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000686 if (tstate->use_tracing) {
687 if (tstate->c_tracefunc != NULL) {
688 /* tstate->c_tracefunc, if defined, is a
689 function that will be called on *every* entry
690 to a code block. Its return value, if not
691 None, is a function that will be called at
692 the start of each executed line of code.
693 (Actually, the function must return itself
694 in order to continue tracing.) The trace
695 functions are called with three arguments:
696 a pointer to the current frame, a string
697 indicating why the function is called, and
698 an argument which depends on the situation.
699 The global trace function is also called
700 whenever an exception is detected. */
701 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
702 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000703 /* Trace function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000704 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000705 }
706 }
707 if (tstate->c_profilefunc != NULL) {
708 /* Similar for c_profilefunc, except it needn't
709 return itself and isn't called for "line" events */
710 if (call_trace(tstate->c_profilefunc,
711 tstate->c_profileobj,
712 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000713 /* Profile function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000714 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000715 }
716 }
717 }
718
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000719 co = f->f_code;
720 names = co->co_names;
721 consts = co->co_consts;
722 fastlocals = f->f_localsplus;
723 freevars = f->f_localsplus + f->f_nlocals;
Michael W. Hudsonecfeb7f2004-02-12 15:28:27 +0000724 first_instr = PyString_AS_STRING(co->co_code);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000725 /* An explanation is in order for the next line.
726
727 f->f_lasti now refers to the index of the last instruction
728 executed. You might think this was obvious from the name, but
729 this wasn't always true before 2.3! PyFrame_New now sets
730 f->f_lasti to -1 (i.e. the index *before* the first instruction)
731 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
732 does work. Promise. */
733 next_instr = first_instr + f->f_lasti + 1;
734 stack_pointer = f->f_stacktop;
735 assert(stack_pointer != NULL);
736 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
737
Tim Peters5ca576e2001-06-18 22:08:13 +0000738#ifdef LLTRACE
739 lltrace = PyDict_GetItemString(f->f_globals,"__lltrace__") != NULL;
740#endif
741#if defined(Py_DEBUG) || defined(LLTRACE)
742 filename = PyString_AsString(co->co_filename);
743#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000744
Guido van Rossum374a9221991-04-04 10:40:29 +0000745 why = WHY_NOT;
746 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +0000747 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +0000748 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +0000749
Guido van Rossum374a9221991-04-04 10:40:29 +0000750 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000751#ifdef WITH_TSC
752 if (inst1 == 0) {
753 /* Almost surely, the opcode executed a break
754 or a continue, preventing inst1 from being set
755 on the way out of the loop.
756 */
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000757 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000758 loop1 = inst1;
759 }
760 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
761 intr0, intr1);
762 ticked = 0;
763 inst1 = 0;
764 intr0 = 0;
765 intr1 = 0;
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000766 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000767#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000768 assert(stack_pointer >= f->f_valuestack); /* else underflow */
769 assert(STACK_LEVEL() <= f->f_stacksize); /* else overflow */
770
Guido van Rossuma027efa1997-05-05 20:56:21 +0000771 /* Do periodic things. Doing this every time through
772 the loop would add too much overhead, so we do it
773 only every Nth instruction. We also do it if
774 ``things_to_do'' is set, i.e. when an asynchronous
775 event needs attention (e.g. a signal handler or
776 async I/O handler); see Py_AddPendingCall() and
777 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000778
Skip Montanarod581d772002-09-03 20:10:45 +0000779 if (--_Py_Ticker < 0) {
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000780 if (*next_instr == SETUP_FINALLY) {
781 /* Make the last opcode before
782 a try: finally: block uninterruptable. */
783 goto fast_next_opcode;
784 }
Skip Montanarod581d772002-09-03 20:10:45 +0000785 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000786 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000787#ifdef WITH_TSC
788 ticked = 1;
789#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000790 if (things_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +0000791 if (Py_MakePendingCalls() < 0) {
792 why = WHY_EXCEPTION;
793 goto on_error;
794 }
Kurt B. Kaiser4c79a832004-11-23 18:06:08 +0000795 if (things_to_do)
796 /* MakePendingCalls() didn't succeed.
797 Force early re-execution of this
798 "periodic" code, possibly after
799 a thread switch */
800 _Py_Ticker = 0;
Guido van Rossum8861b741996-07-30 16:49:37 +0000801 }
Guido van Rossume59214e1994-08-30 08:01:59 +0000802#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000803 if (interpreter_lock) {
804 /* Give another thread a chance */
805
Guido van Rossum25ce5661997-08-02 03:10:38 +0000806 if (PyThreadState_Swap(NULL) != tstate)
807 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000808 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000809
810 /* Other threads may run now */
811
Guido van Rossum65d5b571998-12-21 19:32:43 +0000812 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000813 if (PyThreadState_Swap(tstate) != NULL)
814 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000815
816 /* Check for thread interrupts */
817
818 if (tstate->async_exc != NULL) {
819 x = tstate->async_exc;
820 tstate->async_exc = NULL;
821 PyErr_SetNone(x);
822 Py_DECREF(x);
823 why = WHY_EXCEPTION;
824 goto on_error;
825 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000826 }
827#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000828 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000829
Neil Schemenauer63543862002-02-17 19:10:14 +0000830 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +0000831 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +0000832
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000833 /* line-by-line tracing support */
834
835 if (tstate->c_tracefunc != NULL && !tstate->tracing) {
836 /* see maybe_call_line_trace
837 for expository comments */
838 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +0000839
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000840 err = maybe_call_line_trace(tstate->c_tracefunc,
841 tstate->c_traceobj,
Armin Rigobf57a142004-03-22 19:24:58 +0000842 f, &instr_lb, &instr_ub,
843 &instr_prev);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000844 /* Reload possibly changed frame fields */
845 JUMPTO(f->f_lasti);
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000846 if (f->f_stacktop != NULL) {
847 stack_pointer = f->f_stacktop;
848 f->f_stacktop = NULL;
849 }
850 if (err) {
851 /* trace function raised an exception */
852 goto on_error;
853 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000854 }
855
856 /* Extract opcode and argument */
857
Guido van Rossum374a9221991-04-04 10:40:29 +0000858 opcode = NEXTOP();
Armin Rigo8817fcd2004-06-17 10:22:40 +0000859 oparg = 0; /* allows oparg to be stored in a register because
860 it doesn't have to be remembered across a full loop */
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000861 if (HAS_ARG(opcode))
862 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +0000863 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +0000864#ifdef DYNAMIC_EXECUTION_PROFILE
865#ifdef DXPAIRS
866 dxpairs[lastopcode][opcode]++;
867 lastopcode = opcode;
868#endif
869 dxp[opcode]++;
870#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000871
Guido van Rossum96a42c81992-01-12 02:29:51 +0000872#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +0000873 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +0000874
Guido van Rossum96a42c81992-01-12 02:29:51 +0000875 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000876 if (HAS_ARG(opcode)) {
877 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000878 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +0000879 }
880 else {
881 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000882 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +0000883 }
884 }
885#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000886
Guido van Rossum374a9221991-04-04 10:40:29 +0000887 /* Main switch on opcode */
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000888 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +0000889
Guido van Rossum374a9221991-04-04 10:40:29 +0000890 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +0000891
Guido van Rossum374a9221991-04-04 10:40:29 +0000892 /* BEWARE!
893 It is essential that any operation that fails sets either
894 x to NULL, err to nonzero, or why to anything but WHY_NOT,
895 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000896
Guido van Rossum374a9221991-04-04 10:40:29 +0000897 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000898
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000899 case NOP:
900 goto fast_next_opcode;
901
Neil Schemenauer63543862002-02-17 19:10:14 +0000902 case LOAD_FAST:
903 x = GETLOCAL(oparg);
904 if (x != NULL) {
905 Py_INCREF(x);
906 PUSH(x);
907 goto fast_next_opcode;
908 }
909 format_exc_check_arg(PyExc_UnboundLocalError,
910 UNBOUNDLOCAL_ERROR_MSG,
911 PyTuple_GetItem(co->co_varnames, oparg));
912 break;
913
914 case LOAD_CONST:
Skip Montanaro04d80f82002-08-04 21:03:35 +0000915 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +0000916 Py_INCREF(x);
917 PUSH(x);
918 goto fast_next_opcode;
919
Raymond Hettinger7dc52212003-03-16 20:14:44 +0000920 PREDICTED_WITH_ARG(STORE_FAST);
Neil Schemenauer63543862002-02-17 19:10:14 +0000921 case STORE_FAST:
922 v = POP();
923 SETLOCAL(oparg, v);
924 goto fast_next_opcode;
925
Raymond Hettingerf606f872003-03-16 03:11:04 +0000926 PREDICTED(POP_TOP);
Guido van Rossum374a9221991-04-04 10:40:29 +0000927 case POP_TOP:
928 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000929 Py_DECREF(v);
Neil Schemenauer63543862002-02-17 19:10:14 +0000930 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000931
Guido van Rossum374a9221991-04-04 10:40:29 +0000932 case ROT_TWO:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000933 v = TOP();
934 w = SECOND();
935 SET_TOP(w);
936 SET_SECOND(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000937 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000938
Guido van Rossum374a9221991-04-04 10:40:29 +0000939 case ROT_THREE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000940 v = TOP();
941 w = SECOND();
942 x = THIRD();
943 SET_TOP(w);
944 SET_SECOND(x);
945 SET_THIRD(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000946 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000947
Thomas Wouters434d0822000-08-24 20:11:32 +0000948 case ROT_FOUR:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000949 u = TOP();
950 v = SECOND();
951 w = THIRD();
952 x = FOURTH();
953 SET_TOP(v);
954 SET_SECOND(w);
955 SET_THIRD(x);
956 SET_FOURTH(u);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000957 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000958
Guido van Rossum374a9221991-04-04 10:40:29 +0000959 case DUP_TOP:
960 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000961 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000962 PUSH(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000963 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000964
Thomas Wouters434d0822000-08-24 20:11:32 +0000965 case DUP_TOPX:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000966 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000967 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000968 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000969 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000970 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000971 STACKADJ(2);
972 SET_TOP(x);
973 SET_SECOND(w);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000974 goto fast_next_opcode;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000975 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000976 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000977 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000978 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000979 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000980 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +0000981 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000982 STACKADJ(3);
983 SET_TOP(x);
984 SET_SECOND(w);
985 SET_THIRD(v);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000986 goto fast_next_opcode;
Thomas Wouters434d0822000-08-24 20:11:32 +0000987 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000988 Py_FatalError("invalid argument to DUP_TOPX"
989 " (bytecode corruption?)");
Tim Peters35ba6892000-10-11 07:04:49 +0000990 break;
Thomas Wouters434d0822000-08-24 20:11:32 +0000991
Guido van Rossum374a9221991-04-04 10:40:29 +0000992 case UNARY_POSITIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000993 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000994 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000995 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000996 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000997 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000998 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000999
Guido van Rossum374a9221991-04-04 10:40:29 +00001000 case UNARY_NEGATIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001001 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001002 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001003 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001004 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001005 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001006 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001007
Guido van Rossum374a9221991-04-04 10:40:29 +00001008 case UNARY_NOT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001009 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001010 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001011 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +00001012 if (err == 0) {
1013 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001014 SET_TOP(Py_True);
Guido van Rossumfc490731997-05-06 15:06:49 +00001015 continue;
1016 }
1017 else if (err > 0) {
1018 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001019 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +00001020 err = 0;
1021 continue;
1022 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00001023 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001024 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001025
Guido van Rossum374a9221991-04-04 10:40:29 +00001026 case UNARY_CONVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001027 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001028 x = PyObject_Repr(v);
1029 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001030 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001031 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001032 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001033
Guido van Rossum7928cd71991-10-24 14:59:31 +00001034 case UNARY_INVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001035 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001036 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001037 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001038 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001039 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001040 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001041
Guido van Rossum50564e81996-01-12 01:13:16 +00001042 case BINARY_POWER:
1043 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001044 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001045 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +00001046 Py_DECREF(v);
1047 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001048 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001049 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +00001050 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001051
Guido van Rossum374a9221991-04-04 10:40:29 +00001052 case BINARY_MULTIPLY:
1053 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001054 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001055 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001056 Py_DECREF(v);
1057 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001058 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001059 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001060 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001061
Guido van Rossum374a9221991-04-04 10:40:29 +00001062 case BINARY_DIVIDE:
Tim Peters3caca232001-12-06 06:23:26 +00001063 if (!_Py_QnewFlag) {
1064 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001065 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001066 x = PyNumber_Divide(v, w);
1067 Py_DECREF(v);
1068 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001069 SET_TOP(x);
Tim Peters3caca232001-12-06 06:23:26 +00001070 if (x != NULL) continue;
1071 break;
1072 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001073 /* -Qnew is in effect: fall through to
Tim Peters3caca232001-12-06 06:23:26 +00001074 BINARY_TRUE_DIVIDE */
1075 case BINARY_TRUE_DIVIDE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001076 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001077 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001078 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001079 Py_DECREF(v);
1080 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001081 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001082 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001083 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001084
Guido van Rossum4668b002001-08-08 05:00:18 +00001085 case BINARY_FLOOR_DIVIDE:
1086 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001087 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001088 x = PyNumber_FloorDivide(v, w);
1089 Py_DECREF(v);
1090 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001091 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001092 if (x != NULL) continue;
1093 break;
1094
Guido van Rossum374a9221991-04-04 10:40:29 +00001095 case BINARY_MODULO:
1096 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001097 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001098 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001099 Py_DECREF(v);
1100 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001101 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001102 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001103 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001104
Guido van Rossum374a9221991-04-04 10:40:29 +00001105 case BINARY_ADD:
1106 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001107 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001108 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001109 /* INLINE: int + int */
1110 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001111 a = PyInt_AS_LONG(v);
1112 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001113 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001114 if ((i^a) < 0 && (i^b) < 0)
1115 goto slow_add;
1116 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001117 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001118 else if (PyString_CheckExact(v) &&
1119 PyString_CheckExact(w)) {
1120 x = string_concatenate(v, w, f, next_instr);
1121 /* string_concatenate consumed the ref to v */
1122 goto skip_decref_vx;
1123 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001124 else {
1125 slow_add:
Guido van Rossumc12da691997-07-17 23:12:42 +00001126 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001127 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001128 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001129 skip_decref_vx:
Guido van Rossumb209a111997-04-29 18:18:01 +00001130 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001131 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001132 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001133 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001134
Guido van Rossum374a9221991-04-04 10:40:29 +00001135 case BINARY_SUBTRACT:
1136 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001137 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001138 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001139 /* INLINE: int - int */
1140 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001141 a = PyInt_AS_LONG(v);
1142 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001143 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001144 if ((i^a) < 0 && (i^~b) < 0)
1145 goto slow_sub;
1146 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001147 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001148 else {
1149 slow_sub:
Guido van Rossumc12da691997-07-17 23:12:42 +00001150 x = PyNumber_Subtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001151 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001152 Py_DECREF(v);
1153 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001154 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001155 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001156 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001157
Guido van Rossum374a9221991-04-04 10:40:29 +00001158 case BINARY_SUBSCR:
1159 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001160 v = TOP();
Tim Petersb1c46982001-10-05 20:41:38 +00001161 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001162 /* INLINE: list[int] */
1163 long i = PyInt_AsLong(w);
1164 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +00001165 i += PyList_GET_SIZE(v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001166 if (i >= 0 && i < PyList_GET_SIZE(v)) {
Guido van Rossumfa00e951998-07-08 15:02:37 +00001167 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001168 Py_INCREF(x);
1169 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001170 else
1171 goto slow_get;
Guido van Rossumc12da691997-07-17 23:12:42 +00001172 }
1173 else
Raymond Hettinger467a6982004-04-07 11:39:21 +00001174 slow_get:
Guido van Rossumc12da691997-07-17 23:12:42 +00001175 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001176 Py_DECREF(v);
1177 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001178 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001179 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001180 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001181
Guido van Rossum7928cd71991-10-24 14:59:31 +00001182 case BINARY_LSHIFT:
1183 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001184 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001185 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001186 Py_DECREF(v);
1187 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001188 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001189 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001190 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001191
Guido van Rossum7928cd71991-10-24 14:59:31 +00001192 case BINARY_RSHIFT:
1193 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001194 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001195 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001196 Py_DECREF(v);
1197 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001198 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001199 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001200 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001201
Guido van Rossum7928cd71991-10-24 14:59:31 +00001202 case BINARY_AND:
1203 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001204 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001205 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001206 Py_DECREF(v);
1207 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001208 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001209 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001210 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001211
Guido van Rossum7928cd71991-10-24 14:59:31 +00001212 case BINARY_XOR:
1213 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001214 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001215 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001216 Py_DECREF(v);
1217 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001218 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001219 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001220 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001221
Guido van Rossum7928cd71991-10-24 14:59:31 +00001222 case BINARY_OR:
1223 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001224 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001225 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001226 Py_DECREF(v);
1227 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001228 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001229 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001230 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001231
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001232 case LIST_APPEND:
1233 w = POP();
1234 v = POP();
1235 err = PyList_Append(v, w);
1236 Py_DECREF(v);
1237 Py_DECREF(w);
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001238 if (err == 0) {
1239 PREDICT(JUMP_ABSOLUTE);
1240 continue;
1241 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001242 break;
1243
Thomas Wouters434d0822000-08-24 20:11:32 +00001244 case INPLACE_POWER:
1245 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001246 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001247 x = PyNumber_InPlacePower(v, w, Py_None);
1248 Py_DECREF(v);
1249 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001250 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001251 if (x != NULL) continue;
1252 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001253
Thomas Wouters434d0822000-08-24 20:11:32 +00001254 case INPLACE_MULTIPLY:
1255 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001256 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001257 x = PyNumber_InPlaceMultiply(v, w);
1258 Py_DECREF(v);
1259 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001260 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001261 if (x != NULL) continue;
1262 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001263
Thomas Wouters434d0822000-08-24 20:11:32 +00001264 case INPLACE_DIVIDE:
Tim Peters54b11912001-12-25 18:49:11 +00001265 if (!_Py_QnewFlag) {
1266 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001267 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001268 x = PyNumber_InPlaceDivide(v, w);
1269 Py_DECREF(v);
1270 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001271 SET_TOP(x);
Tim Peters54b11912001-12-25 18:49:11 +00001272 if (x != NULL) continue;
1273 break;
1274 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001275 /* -Qnew is in effect: fall through to
Tim Peters54b11912001-12-25 18:49:11 +00001276 INPLACE_TRUE_DIVIDE */
1277 case INPLACE_TRUE_DIVIDE:
Thomas Wouters434d0822000-08-24 20:11:32 +00001278 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001279 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001280 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001281 Py_DECREF(v);
1282 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001283 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001284 if (x != NULL) continue;
1285 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001286
Guido van Rossum4668b002001-08-08 05:00:18 +00001287 case INPLACE_FLOOR_DIVIDE:
1288 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001289 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001290 x = PyNumber_InPlaceFloorDivide(v, w);
1291 Py_DECREF(v);
1292 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001293 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001294 if (x != NULL) continue;
1295 break;
1296
Thomas Wouters434d0822000-08-24 20:11:32 +00001297 case INPLACE_MODULO:
1298 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001299 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001300 x = PyNumber_InPlaceRemainder(v, w);
1301 Py_DECREF(v);
1302 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001303 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001304 if (x != NULL) continue;
1305 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001306
Thomas Wouters434d0822000-08-24 20:11:32 +00001307 case INPLACE_ADD:
1308 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001309 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001310 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001311 /* INLINE: int + int */
1312 register long a, b, i;
1313 a = PyInt_AS_LONG(v);
1314 b = PyInt_AS_LONG(w);
1315 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001316 if ((i^a) < 0 && (i^b) < 0)
1317 goto slow_iadd;
1318 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001319 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001320 else if (PyString_CheckExact(v) &&
1321 PyString_CheckExact(w)) {
1322 x = string_concatenate(v, w, f, next_instr);
1323 /* string_concatenate consumed the ref to v */
1324 goto skip_decref_v;
1325 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001326 else {
1327 slow_iadd:
Thomas Wouters434d0822000-08-24 20:11:32 +00001328 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001329 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001330 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001331 skip_decref_v:
Thomas Wouters434d0822000-08-24 20:11:32 +00001332 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001333 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001334 if (x != NULL) continue;
1335 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001336
Thomas Wouters434d0822000-08-24 20:11:32 +00001337 case INPLACE_SUBTRACT:
1338 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001339 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001340 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001341 /* INLINE: int - int */
1342 register long a, b, i;
1343 a = PyInt_AS_LONG(v);
1344 b = PyInt_AS_LONG(w);
1345 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001346 if ((i^a) < 0 && (i^~b) < 0)
1347 goto slow_isub;
1348 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001349 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001350 else {
1351 slow_isub:
Thomas Wouters434d0822000-08-24 20:11:32 +00001352 x = PyNumber_InPlaceSubtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001353 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001354 Py_DECREF(v);
1355 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001356 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001357 if (x != NULL) continue;
1358 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001359
Thomas Wouters434d0822000-08-24 20:11:32 +00001360 case INPLACE_LSHIFT:
1361 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001362 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001363 x = PyNumber_InPlaceLshift(v, w);
1364 Py_DECREF(v);
1365 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001366 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001367 if (x != NULL) continue;
1368 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001369
Thomas Wouters434d0822000-08-24 20:11:32 +00001370 case INPLACE_RSHIFT:
1371 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001372 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001373 x = PyNumber_InPlaceRshift(v, w);
1374 Py_DECREF(v);
1375 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001376 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001377 if (x != NULL) continue;
1378 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001379
Thomas Wouters434d0822000-08-24 20:11:32 +00001380 case INPLACE_AND:
1381 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001382 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001383 x = PyNumber_InPlaceAnd(v, w);
1384 Py_DECREF(v);
1385 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001386 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001387 if (x != NULL) continue;
1388 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001389
Thomas Wouters434d0822000-08-24 20:11:32 +00001390 case INPLACE_XOR:
1391 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001392 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001393 x = PyNumber_InPlaceXor(v, w);
1394 Py_DECREF(v);
1395 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001396 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001397 if (x != NULL) continue;
1398 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001399
Thomas Wouters434d0822000-08-24 20:11:32 +00001400 case INPLACE_OR:
1401 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001402 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001403 x = PyNumber_InPlaceOr(v, w);
1404 Py_DECREF(v);
1405 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001406 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001407 if (x != NULL) continue;
1408 break;
1409
Guido van Rossum374a9221991-04-04 10:40:29 +00001410 case SLICE+0:
1411 case SLICE+1:
1412 case SLICE+2:
1413 case SLICE+3:
1414 if ((opcode-SLICE) & 2)
1415 w = POP();
1416 else
1417 w = NULL;
1418 if ((opcode-SLICE) & 1)
1419 v = POP();
1420 else
1421 v = NULL;
Raymond Hettinger663004b2003-01-09 15:24:30 +00001422 u = TOP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001423 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001424 Py_DECREF(u);
1425 Py_XDECREF(v);
1426 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001427 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001428 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001429 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001430
Guido van Rossum374a9221991-04-04 10:40:29 +00001431 case STORE_SLICE+0:
1432 case STORE_SLICE+1:
1433 case STORE_SLICE+2:
1434 case STORE_SLICE+3:
1435 if ((opcode-STORE_SLICE) & 2)
1436 w = POP();
1437 else
1438 w = NULL;
1439 if ((opcode-STORE_SLICE) & 1)
1440 v = POP();
1441 else
1442 v = NULL;
1443 u = POP();
1444 t = POP();
1445 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +00001446 Py_DECREF(t);
1447 Py_DECREF(u);
1448 Py_XDECREF(v);
1449 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001450 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001451 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001452
Guido van Rossum374a9221991-04-04 10:40:29 +00001453 case DELETE_SLICE+0:
1454 case DELETE_SLICE+1:
1455 case DELETE_SLICE+2:
1456 case DELETE_SLICE+3:
1457 if ((opcode-DELETE_SLICE) & 2)
1458 w = POP();
1459 else
1460 w = NULL;
1461 if ((opcode-DELETE_SLICE) & 1)
1462 v = POP();
1463 else
1464 v = NULL;
1465 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001466 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +00001467 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00001468 Py_DECREF(u);
1469 Py_XDECREF(v);
1470 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001471 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001472 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001473
Guido van Rossum374a9221991-04-04 10:40:29 +00001474 case STORE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001475 w = TOP();
1476 v = SECOND();
1477 u = THIRD();
1478 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001479 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001480 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001481 Py_DECREF(u);
1482 Py_DECREF(v);
1483 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001484 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001485 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001486
Guido van Rossum374a9221991-04-04 10:40:29 +00001487 case DELETE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001488 w = TOP();
1489 v = SECOND();
1490 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001491 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001492 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001493 Py_DECREF(v);
1494 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001495 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001496 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001497
Guido van Rossum374a9221991-04-04 10:40:29 +00001498 case PRINT_EXPR:
1499 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001500 w = PySys_GetObject("displayhook");
1501 if (w == NULL) {
1502 PyErr_SetString(PyExc_RuntimeError,
1503 "lost sys.displayhook");
1504 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001505 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001506 }
1507 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001508 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001509 if (x == NULL)
1510 err = -1;
1511 }
1512 if (err == 0) {
1513 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001514 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001515 if (w == NULL)
1516 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001517 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001518 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001519 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001520 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001521
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001522 case PRINT_ITEM_TO:
1523 w = stream = POP();
1524 /* fall through to PRINT_ITEM */
1525
Guido van Rossum374a9221991-04-04 10:40:29 +00001526 case PRINT_ITEM:
1527 v = POP();
Barry Warsaw093abe02000-08-29 04:56:13 +00001528 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001529 w = PySys_GetObject("stdout");
1530 if (w == NULL) {
1531 PyErr_SetString(PyExc_RuntimeError,
1532 "lost sys.stdout");
1533 err = -1;
1534 }
Guido van Rossum8f183201997-12-31 05:53:15 +00001535 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001536 /* PyFile_SoftSpace() can exececute arbitrary code
1537 if sys.stdout is an instance with a __getattr__.
1538 If __getattr__ raises an exception, w will
1539 be freed, so we need to prevent that temporarily. */
1540 Py_XINCREF(w);
Tim Peters8e5fd532002-03-24 19:25:00 +00001541 if (w != NULL && PyFile_SoftSpace(w, 0))
Guido van Rossumbe270261997-05-22 22:26:18 +00001542 err = PyFile_WriteString(" ", w);
1543 if (err == 0)
1544 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001545 if (err == 0) {
Tim Peters8e5fd532002-03-24 19:25:00 +00001546 /* XXX move into writeobject() ? */
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001547 if (PyString_Check(v)) {
1548 char *s = PyString_AS_STRING(v);
1549 int len = PyString_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001550 if (len == 0 ||
1551 !isspace(Py_CHARMASK(s[len-1])) ||
1552 s[len-1] == ' ')
1553 PyFile_SoftSpace(w, 1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001554 }
Martin v. Löwis8d3ce5a2001-12-18 22:36:40 +00001555#ifdef Py_USING_UNICODE
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001556 else if (PyUnicode_Check(v)) {
1557 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
1558 int len = PyUnicode_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001559 if (len == 0 ||
1560 !Py_UNICODE_ISSPACE(s[len-1]) ||
1561 s[len-1] == ' ')
1562 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001563 }
Michael W. Hudsond95c8282002-05-20 13:56:11 +00001564#endif
Tim Peters8e5fd532002-03-24 19:25:00 +00001565 else
1566 PyFile_SoftSpace(w, 1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001567 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001568 Py_XDECREF(w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001569 Py_DECREF(v);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001570 Py_XDECREF(stream);
1571 stream = NULL;
1572 if (err == 0)
1573 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001574 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001575
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001576 case PRINT_NEWLINE_TO:
1577 w = stream = POP();
1578 /* fall through to PRINT_NEWLINE */
1579
Guido van Rossum374a9221991-04-04 10:40:29 +00001580 case PRINT_NEWLINE:
Barry Warsaw093abe02000-08-29 04:56:13 +00001581 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001582 w = PySys_GetObject("stdout");
1583 if (w == NULL)
1584 PyErr_SetString(PyExc_RuntimeError,
1585 "lost sys.stdout");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001586 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001587 if (w != NULL) {
1588 err = PyFile_WriteString("\n", w);
1589 if (err == 0)
1590 PyFile_SoftSpace(w, 0);
1591 }
1592 Py_XDECREF(stream);
1593 stream = NULL;
Guido van Rossum374a9221991-04-04 10:40:29 +00001594 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001595
Thomas Wouters434d0822000-08-24 20:11:32 +00001596
1597#ifdef CASE_TOO_BIG
1598 default: switch (opcode) {
1599#endif
Guido van Rossumf10570b1995-07-07 22:53:21 +00001600 case RAISE_VARARGS:
1601 u = v = w = NULL;
1602 switch (oparg) {
1603 case 3:
1604 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001605 /* Fallthrough */
1606 case 2:
1607 v = POP(); /* value */
1608 /* Fallthrough */
1609 case 1:
1610 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001611 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001612 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001613 break;
1614 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001615 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001616 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001617 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001618 break;
1619 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001620 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001621
Guido van Rossum374a9221991-04-04 10:40:29 +00001622 case LOAD_LOCALS:
Raymond Hettinger467a6982004-04-07 11:39:21 +00001623 if ((x = f->f_locals) != NULL) {
1624 Py_INCREF(x);
1625 PUSH(x);
1626 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001627 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001628 PyErr_SetString(PyExc_SystemError, "no locals");
Guido van Rossum374a9221991-04-04 10:40:29 +00001629 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001630
Guido van Rossum374a9221991-04-04 10:40:29 +00001631 case RETURN_VALUE:
1632 retval = POP();
1633 why = WHY_RETURN;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001634 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001635
Tim Peters5ca576e2001-06-18 22:08:13 +00001636 case YIELD_VALUE:
1637 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001638 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001639 why = WHY_YIELD;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001640 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001641
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001642 case EXEC_STMT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001643 w = TOP();
1644 v = SECOND();
1645 u = THIRD();
1646 STACKADJ(-3);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001647 READ_TIMESTAMP(intr0);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001648 err = exec_statement(f, u, v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001649 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00001650 Py_DECREF(u);
1651 Py_DECREF(v);
1652 Py_DECREF(w);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001653 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001654
Guido van Rossum374a9221991-04-04 10:40:29 +00001655 case POP_BLOCK:
1656 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001657 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001658 while (STACK_LEVEL() > b->b_level) {
1659 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001660 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001661 }
1662 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001663 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001664
Guido van Rossum374a9221991-04-04 10:40:29 +00001665 case END_FINALLY:
1666 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001667 if (PyInt_Check(v)) {
Raymond Hettinger7c958652004-04-06 10:11:10 +00001668 why = (enum why_code) PyInt_AS_LONG(v);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001669 assert(why != WHY_YIELD);
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00001670 if (why == WHY_RETURN ||
1671 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001672 retval = POP();
1673 }
Raymond Hettingerd3b836d2004-04-07 13:17:27 +00001674 else if (PyClass_Check(v) || PyString_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001675 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001676 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001677 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001678 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001679 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001680 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001681 else if (v != Py_None) {
1682 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001683 "'finally' pops bad exception");
1684 why = WHY_EXCEPTION;
1685 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001686 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001687 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001688
Guido van Rossum374a9221991-04-04 10:40:29 +00001689 case BUILD_CLASS:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001690 u = TOP();
1691 v = SECOND();
1692 w = THIRD();
1693 STACKADJ(-2);
Guido van Rossum25831651993-05-19 14:50:45 +00001694 x = build_class(u, v, w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001695 SET_TOP(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001696 Py_DECREF(u);
1697 Py_DECREF(v);
1698 Py_DECREF(w);
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 STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001702 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001703 v = POP();
Raymond Hettinger467a6982004-04-07 11:39:21 +00001704 if ((x = f->f_locals) != NULL) {
Raymond Hettinger66bd2332004-08-02 08:30:07 +00001705 if (PyDict_CheckExact(x))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001706 err = PyDict_SetItem(x, w, v);
1707 else
1708 err = PyObject_SetItem(x, w, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001709 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001710 if (err == 0) continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001711 break;
1712 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001713 PyErr_Format(PyExc_SystemError,
1714 "no locals found when storing %s",
1715 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001716 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001717
Guido van Rossum374a9221991-04-04 10:40:29 +00001718 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001719 w = GETITEM(names, oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001720 if ((x = f->f_locals) != NULL) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001721 if ((err = PyObject_DelItem(x, w)) != 0)
Raymond Hettinger467a6982004-04-07 11:39:21 +00001722 format_exc_check_arg(PyExc_NameError,
1723 NAME_ERROR_MSG ,w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001724 break;
1725 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001726 PyErr_Format(PyExc_SystemError,
1727 "no locals when deleting %s",
1728 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001729 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001730
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001731 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001732 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001733 v = POP();
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001734 if (PyTuple_CheckExact(v) && PyTuple_GET_SIZE(v) == oparg) {
1735 PyObject **items = ((PyTupleObject *)v)->ob_item;
1736 while (oparg--) {
1737 w = items[oparg];
1738 Py_INCREF(w);
1739 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001740 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001741 Py_DECREF(v);
1742 continue;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001743 } else if (PyList_CheckExact(v) && PyList_GET_SIZE(v) == oparg) {
1744 PyObject **items = ((PyListObject *)v)->ob_item;
1745 while (oparg--) {
1746 w = items[oparg];
1747 Py_INCREF(w);
1748 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001749 }
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001750 } else if (unpack_iterable(v, oparg,
Tim Petersd6d010b2001-06-21 02:49:55 +00001751 stack_pointer + oparg))
1752 stack_pointer += oparg;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001753 else {
1754 if (PyErr_ExceptionMatches(PyExc_TypeError))
1755 PyErr_SetString(PyExc_TypeError,
1756 "unpack non-sequence");
Barry Warsawe42b18f1997-08-25 22:13:04 +00001757 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001758 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001759 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001760 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001761
Guido van Rossum374a9221991-04-04 10:40:29 +00001762 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001763 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001764 v = TOP();
1765 u = SECOND();
1766 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001767 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1768 Py_DECREF(v);
1769 Py_DECREF(u);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001770 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001771 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001772
Guido van Rossum374a9221991-04-04 10:40:29 +00001773 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001774 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001775 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001776 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1777 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001778 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001779 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001780
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001781 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001782 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001783 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001784 err = PyDict_SetItem(f->f_globals, w, v);
1785 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001786 if (err == 0) continue;
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001787 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001788
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001789 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001790 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001791 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001792 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001793 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001794 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001795
Guido van Rossum374a9221991-04-04 10:40:29 +00001796 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001797 w = GETITEM(names, oparg);
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001798 if ((v = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001799 PyErr_Format(PyExc_SystemError,
1800 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001801 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001802 break;
1803 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001804 if (PyDict_CheckExact(v)) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001805 x = PyDict_GetItem(v, w);
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001806 Py_XINCREF(x);
1807 }
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001808 else {
1809 x = PyObject_GetItem(v, w);
1810 if (x == NULL && PyErr_Occurred()) {
1811 if (!PyErr_ExceptionMatches(PyExc_KeyError))
1812 break;
1813 PyErr_Clear();
1814 }
1815 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001816 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001817 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001818 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001819 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001820 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001821 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001822 PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001823 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001824 break;
1825 }
1826 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001827 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001828 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001829 PUSH(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001830 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001831
Guido van Rossum374a9221991-04-04 10:40:29 +00001832 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001833 w = GETITEM(names, oparg);
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001834 if (PyString_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00001835 /* Inline the PyDict_GetItem() calls.
1836 WARNING: this is an extreme speed hack.
1837 Do not try this at home. */
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001838 long hash = ((PyStringObject *)w)->ob_shash;
1839 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001840 PyDictObject *d;
1841 d = (PyDictObject *)(f->f_globals);
1842 x = d->ma_lookup(d, w, hash)->me_value;
1843 if (x != NULL) {
1844 Py_INCREF(x);
1845 PUSH(x);
1846 continue;
1847 }
1848 d = (PyDictObject *)(f->f_builtins);
1849 x = d->ma_lookup(d, w, hash)->me_value;
1850 if (x != NULL) {
1851 Py_INCREF(x);
1852 PUSH(x);
1853 continue;
1854 }
1855 goto load_global_error;
1856 }
1857 }
1858 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00001859 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001860 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001861 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001862 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001863 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00001864 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001865 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001866 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001867 break;
1868 }
1869 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001870 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001871 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001872 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001873
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001874 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001875 x = GETLOCAL(oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001876 if (x != NULL) {
1877 SETLOCAL(oparg, NULL);
1878 continue;
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001879 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001880 format_exc_check_arg(
1881 PyExc_UnboundLocalError,
1882 UNBOUNDLOCAL_ERROR_MSG,
1883 PyTuple_GetItem(co->co_varnames, oparg)
1884 );
1885 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001886
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001887 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001888 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001889 Py_INCREF(x);
1890 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001891 if (x != NULL) continue;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001892 break;
1893
1894 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001895 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001896 w = PyCell_Get(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001897 if (w != NULL) {
1898 PUSH(w);
1899 continue;
Jeremy Hylton2524d692001-02-05 17:23:16 +00001900 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001901 err = -1;
1902 /* Don't stomp existing exception */
1903 if (PyErr_Occurred())
1904 break;
1905 if (oparg < f->f_ncells) {
1906 v = PyTuple_GetItem(co->co_cellvars,
1907 oparg);
1908 format_exc_check_arg(
1909 PyExc_UnboundLocalError,
1910 UNBOUNDLOCAL_ERROR_MSG,
1911 v);
1912 } else {
1913 v = PyTuple_GetItem(
1914 co->co_freevars,
1915 oparg - f->f_ncells);
1916 format_exc_check_arg(
1917 PyExc_NameError,
1918 UNBOUNDFREE_ERROR_MSG,
1919 v);
1920 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001921 break;
1922
1923 case STORE_DEREF:
1924 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001925 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001926 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00001927 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001928 continue;
1929
Guido van Rossum374a9221991-04-04 10:40:29 +00001930 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001931 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001932 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001933 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001934 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001935 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001936 }
1937 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001938 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001939 }
1940 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001941
Guido van Rossum374a9221991-04-04 10:40:29 +00001942 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00001943 x = PyList_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 Rossum5053efc1998-08-04 15:27:50 +00001947 PyList_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_MAP:
Guido van Rossumb209a111997-04-29 18:18:01 +00001955 x = PyDict_New();
Guido van Rossum374a9221991-04-04 10:40:29 +00001956 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001957 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001958 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001959
Guido van Rossum374a9221991-04-04 10:40:29 +00001960 case LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001961 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001962 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001963 x = PyObject_GetAttr(v, w);
1964 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001965 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001966 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001967 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001968
Guido van Rossum374a9221991-04-04 10:40:29 +00001969 case COMPARE_OP:
1970 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001971 v = TOP();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001972 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001973 /* INLINE: cmp(int, int) */
1974 register long a, b;
1975 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001976 a = PyInt_AS_LONG(v);
1977 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001978 switch (oparg) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00001979 case PyCmp_LT: res = a < b; break;
1980 case PyCmp_LE: res = a <= b; break;
1981 case PyCmp_EQ: res = a == b; break;
1982 case PyCmp_NE: res = a != b; break;
1983 case PyCmp_GT: res = a > b; break;
1984 case PyCmp_GE: res = a >= b; break;
1985 case PyCmp_IS: res = v == w; break;
1986 case PyCmp_IS_NOT: res = v != w; break;
Guido van Rossumc12da691997-07-17 23:12:42 +00001987 default: goto slow_compare;
1988 }
1989 x = res ? Py_True : Py_False;
1990 Py_INCREF(x);
1991 }
1992 else {
1993 slow_compare:
1994 x = cmp_outcome(oparg, v, w);
1995 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001996 Py_DECREF(v);
1997 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001998 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001999 if (x == NULL) break;
2000 PREDICT(JUMP_IF_FALSE);
2001 PREDICT(JUMP_IF_TRUE);
2002 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002003
Guido van Rossum374a9221991-04-04 10:40:29 +00002004 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00002005 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002006 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002007 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002008 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00002009 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002010 break;
2011 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00002012 u = TOP();
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002013 w = PyTuple_Pack(4,
Guido van Rossum681d79a1995-07-18 14:51:37 +00002014 w,
2015 f->f_globals,
Guido van Rossuma027efa1997-05-05 20:56:21 +00002016 f->f_locals == NULL ?
2017 Py_None : f->f_locals,
Guido van Rossum681d79a1995-07-18 14:51:37 +00002018 u);
Guido van Rossumb209a111997-04-29 18:18:01 +00002019 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002020 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002021 u = POP();
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002022 x = NULL;
2023 break;
2024 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002025 READ_TIMESTAMP(intr0);
Guido van Rossumb209a111997-04-29 18:18:01 +00002026 x = PyEval_CallObject(x, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002027 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002028 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002029 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002030 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002031 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002032
Thomas Wouters52152252000-08-17 22:55:00 +00002033 case IMPORT_STAR:
2034 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002035 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002036 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002037 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00002038 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002039 break;
2040 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002041 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002042 err = import_all_from(x, v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002043 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002044 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00002045 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002046 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002047 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002048
Thomas Wouters52152252000-08-17 22:55:00 +00002049 case IMPORT_FROM:
Skip Montanaro496e6582002-08-06 17:47:40 +00002050 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00002051 v = TOP();
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002052 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002053 x = import_from(v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002054 READ_TIMESTAMP(intr1);
Thomas Wouters52152252000-08-17 22:55:00 +00002055 PUSH(x);
2056 if (x != NULL) continue;
2057 break;
2058
Guido van Rossum374a9221991-04-04 10:40:29 +00002059 case JUMP_FORWARD:
2060 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002061 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002062
Raymond Hettingerf606f872003-03-16 03:11:04 +00002063 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002064 case JUMP_IF_FALSE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002065 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002066 if (w == Py_True) {
2067 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002068 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002069 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002070 if (w == Py_False) {
2071 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002072 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002073 }
2074 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002075 if (err > 0)
2076 err = 0;
2077 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00002078 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002079 else
2080 break;
2081 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002082
Raymond Hettingerf606f872003-03-16 03:11:04 +00002083 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002084 case JUMP_IF_TRUE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002085 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002086 if (w == Py_False) {
2087 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002088 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002089 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002090 if (w == Py_True) {
2091 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002092 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002093 }
2094 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002095 if (err > 0) {
2096 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00002097 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002098 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002099 else if (err == 0)
2100 ;
2101 else
2102 break;
2103 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002104
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00002105 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002106 case JUMP_ABSOLUTE:
2107 JUMPTO(oparg);
Neil Schemenauerca2a2f12003-05-30 23:59:44 +00002108 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002109
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002110 case GET_ITER:
2111 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002112 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002113 x = PyObject_GetIter(v);
2114 Py_DECREF(v);
2115 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002116 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002117 PREDICT(FOR_ITER);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002118 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002119 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002120 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002121 break;
2122
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002123 PREDICTED_WITH_ARG(FOR_ITER);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002124 case FOR_ITER:
2125 /* before: [iter]; after: [iter, iter()] *or* [] */
2126 v = TOP();
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002127 x = (*v->ob_type->tp_iternext)(v);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002128 if (x != NULL) {
2129 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002130 PREDICT(STORE_FAST);
2131 PREDICT(UNPACK_SEQUENCE);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002132 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002133 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002134 if (PyErr_Occurred()) {
2135 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2136 break;
2137 PyErr_Clear();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002138 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002139 /* iterator ended normally */
2140 x = v = POP();
2141 Py_DECREF(v);
2142 JUMPBY(oparg);
2143 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002144
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002145 case BREAK_LOOP:
2146 why = WHY_BREAK;
2147 goto fast_block_end;
2148
2149 case CONTINUE_LOOP:
2150 retval = PyInt_FromLong(oparg);
2151 why = WHY_CONTINUE;
2152 goto fast_block_end;
2153
Guido van Rossum374a9221991-04-04 10:40:29 +00002154 case SETUP_LOOP:
2155 case SETUP_EXCEPT:
2156 case SETUP_FINALLY:
Guido van Rossumb209a111997-04-29 18:18:01 +00002157 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002158 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002159 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002160
Guido van Rossumf10570b1995-07-07 22:53:21 +00002161 case CALL_FUNCTION:
Armin Rigo8817fcd2004-06-17 10:22:40 +00002162 {
2163 PyObject **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002164 PCALL(PCALL_ALL);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002165 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002166#ifdef WITH_TSC
Armin Rigo8817fcd2004-06-17 10:22:40 +00002167 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002168#else
Armin Rigo8817fcd2004-06-17 10:22:40 +00002169 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002170#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002171 stack_pointer = sp;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002172 PUSH(x);
2173 if (x != NULL)
2174 continue;
2175 break;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002176 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002177
Jeremy Hylton76901512000-03-28 23:49:17 +00002178 case CALL_FUNCTION_VAR:
2179 case CALL_FUNCTION_KW:
2180 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002181 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002182 int na = oparg & 0xff;
2183 int nk = (oparg>>8) & 0xff;
2184 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002185 int n = na + 2 * nk;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002186 PyObject **pfunc, *func, **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002187 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002188 if (flags & CALL_FLAG_VAR)
2189 n++;
2190 if (flags & CALL_FLAG_KW)
2191 n++;
2192 pfunc = stack_pointer - n - 1;
2193 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002194
Guido van Rossumac7be682001-01-17 15:42:30 +00002195 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002196 && PyMethod_GET_SELF(func) != NULL) {
2197 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002198 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002199 func = PyMethod_GET_FUNCTION(func);
2200 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002201 Py_DECREF(*pfunc);
2202 *pfunc = self;
2203 na++;
2204 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002205 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002206 Py_INCREF(func);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002207 sp = stack_pointer;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002208 READ_TIMESTAMP(intr0);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002209 x = ext_do_call(func, &sp, flags, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002210 READ_TIMESTAMP(intr1);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002211 stack_pointer = sp;
Jeremy Hylton76901512000-03-28 23:49:17 +00002212 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002213
Jeremy Hylton76901512000-03-28 23:49:17 +00002214 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002215 w = POP();
2216 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002217 }
2218 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002219 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002220 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002221 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002222 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002223
Guido van Rossum681d79a1995-07-18 14:51:37 +00002224 case MAKE_FUNCTION:
2225 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002226 x = PyFunction_New(v, f->f_globals);
2227 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002228 /* XXX Maybe this should be a separate opcode? */
2229 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002230 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002231 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002232 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002233 x = NULL;
2234 break;
2235 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002236 while (--oparg >= 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002237 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002238 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002239 }
2240 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002241 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002242 }
2243 PUSH(x);
2244 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002245
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002246 case MAKE_CLOSURE:
2247 {
2248 int nfree;
2249 v = POP(); /* code object */
2250 x = PyFunction_New(v, f->f_globals);
Jeremy Hylton733c8932001-12-13 19:51:56 +00002251 nfree = PyCode_GetNumFree((PyCodeObject *)v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002252 Py_DECREF(v);
2253 /* XXX Maybe this should be a separate opcode? */
2254 if (x != NULL && nfree > 0) {
2255 v = PyTuple_New(nfree);
2256 if (v == NULL) {
2257 Py_DECREF(x);
2258 x = NULL;
2259 break;
2260 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002261 while (--nfree >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002262 w = POP();
2263 PyTuple_SET_ITEM(v, nfree, w);
2264 }
2265 err = PyFunction_SetClosure(x, v);
2266 Py_DECREF(v);
2267 }
2268 if (x != NULL && oparg > 0) {
2269 v = PyTuple_New(oparg);
2270 if (v == NULL) {
2271 Py_DECREF(x);
2272 x = NULL;
2273 break;
2274 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002275 while (--oparg >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002276 w = POP();
2277 PyTuple_SET_ITEM(v, oparg, w);
2278 }
2279 err = PyFunction_SetDefaults(x, v);
2280 Py_DECREF(v);
2281 }
2282 PUSH(x);
2283 break;
2284 }
2285
Guido van Rossum8861b741996-07-30 16:49:37 +00002286 case BUILD_SLICE:
2287 if (oparg == 3)
2288 w = POP();
2289 else
2290 w = NULL;
2291 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002292 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002293 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002294 Py_DECREF(u);
2295 Py_DECREF(v);
2296 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002297 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002298 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002299 break;
2300
Fred Drakeef8ace32000-08-24 00:32:09 +00002301 case EXTENDED_ARG:
2302 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002303 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002304 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002305
Guido van Rossum374a9221991-04-04 10:40:29 +00002306 default:
2307 fprintf(stderr,
2308 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002309 PyCode_Addr2Line(f->f_code, f->f_lasti),
2310 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002311 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002312 why = WHY_EXCEPTION;
2313 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002314
2315#ifdef CASE_TOO_BIG
2316 }
2317#endif
2318
Guido van Rossum374a9221991-04-04 10:40:29 +00002319 } /* switch */
2320
2321 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002322
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002323 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002324
Guido van Rossum374a9221991-04-04 10:40:29 +00002325 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002326
Guido van Rossum374a9221991-04-04 10:40:29 +00002327 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002328 if (err == 0 && x != NULL) {
2329#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002330 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002331 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002332 fprintf(stderr,
2333 "XXX undetected error\n");
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002334 else {
2335#endif
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002336 READ_TIMESTAMP(loop1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002337 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002338#ifdef CHECKEXC
2339 }
2340#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +00002341 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002342 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002343 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002344 err = 0;
2345 }
2346
Guido van Rossum374a9221991-04-04 10:40:29 +00002347 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002348
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002349 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002350 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002351 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002352 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002353 why = WHY_EXCEPTION;
2354 }
2355 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002356#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002357 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002358 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002359 if (PyErr_Occurred()) {
Jeremy Hylton904ed862003-11-05 17:29:35 +00002360 char buf[1024];
2361 sprintf(buf, "Stack unwind with exception "
2362 "set and why=%d", why);
2363 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002364 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002365 }
2366#endif
2367
2368 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002369
Guido van Rossum374a9221991-04-04 10:40:29 +00002370 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002371 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002372
Fred Drake8f51f542001-10-04 14:48:42 +00002373 if (tstate->c_tracefunc != NULL)
2374 call_exc_trace(tstate->c_tracefunc,
2375 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002376 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002377
Guido van Rossum374a9221991-04-04 10:40:29 +00002378 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002379
Guido van Rossum374a9221991-04-04 10:40:29 +00002380 if (why == WHY_RERAISE)
2381 why = WHY_EXCEPTION;
2382
2383 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002384
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002385fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002386 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002387 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002388
Tim Peters8a5c3c72004-04-05 19:36:21 +00002389 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002390 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2391 /* For a continue inside a try block,
2392 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002393 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2394 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002395 why = WHY_NOT;
2396 JUMPTO(PyInt_AS_LONG(retval));
2397 Py_DECREF(retval);
2398 break;
2399 }
2400
Guido van Rossum374a9221991-04-04 10:40:29 +00002401 while (STACK_LEVEL() > b->b_level) {
2402 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002403 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002404 }
2405 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2406 why = WHY_NOT;
2407 JUMPTO(b->b_handler);
2408 break;
2409 }
2410 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002411 (b->b_type == SETUP_EXCEPT &&
2412 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002413 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002414 PyObject *exc, *val, *tb;
2415 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002416 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002417 val = Py_None;
2418 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002419 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002420 /* Make the raw exception data
2421 available to the handler,
2422 so a program can emulate the
2423 Python main loop. Don't do
2424 this for 'finally'. */
2425 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002426 PyErr_NormalizeException(
2427 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002428 set_exc_info(tstate,
2429 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002430 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002431 if (tb == NULL) {
2432 Py_INCREF(Py_None);
2433 PUSH(Py_None);
2434 } else
2435 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002436 PUSH(val);
2437 PUSH(exc);
2438 }
2439 else {
Raymond Hettinger06032cb2004-04-06 09:37:35 +00002440 if (why & (WHY_RETURN | WHY_CONTINUE))
Guido van Rossum374a9221991-04-04 10:40:29 +00002441 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002442 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002443 PUSH(v);
2444 }
2445 why = WHY_NOT;
2446 JUMPTO(b->b_handler);
2447 break;
2448 }
2449 } /* unwind stack */
2450
2451 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002452
Guido van Rossum374a9221991-04-04 10:40:29 +00002453 if (why != WHY_NOT)
2454 break;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002455 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002456
Guido van Rossum374a9221991-04-04 10:40:29 +00002457 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002458
Tim Peters8a5c3c72004-04-05 19:36:21 +00002459 assert(why != WHY_YIELD);
2460 /* Pop remaining stack entries. */
2461 while (!EMPTY()) {
2462 v = POP();
2463 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002464 }
2465
Tim Peters8a5c3c72004-04-05 19:36:21 +00002466 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002467 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002468
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002469fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002470 if (tstate->use_tracing) {
2471 if (tstate->c_tracefunc
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002472 && (why == WHY_RETURN || why == WHY_YIELD)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002473 if (call_trace(tstate->c_tracefunc,
2474 tstate->c_traceobj, f,
2475 PyTrace_RETURN, retval)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002476 Py_XDECREF(retval);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002477 retval = NULL;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002478 why = WHY_EXCEPTION;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002479 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002480 }
Fred Drake8f51f542001-10-04 14:48:42 +00002481 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002482 if (why == WHY_EXCEPTION)
2483 call_trace_protected(tstate->c_profilefunc,
2484 tstate->c_profileobj, f,
2485 PyTrace_RETURN);
2486 else if (call_trace(tstate->c_profilefunc,
2487 tstate->c_profileobj, f,
2488 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002489 Py_XDECREF(retval);
2490 retval = NULL;
2491 why = WHY_EXCEPTION;
2492 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002493 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002494 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002495
Guido van Rossuma027efa1997-05-05 20:56:21 +00002496 reset_exc_info(tstate);
2497
Tim Peters5ca576e2001-06-18 22:08:13 +00002498 /* pop frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +00002499 exit_eval_frame:
2500 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002501 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002502
Guido van Rossum96a42c81992-01-12 02:29:51 +00002503 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002504}
2505
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002506/* this is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002507 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Skip Montanaroc9a47622005-01-08 21:58:58 +00002508 the test in the if statement in Misc/gdbinit:pystack* */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002509
Tim Peters6d6c1a32001-08-02 04:15:00 +00002510PyObject *
2511PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002512 PyObject **args, int argcount, PyObject **kws, int kwcount,
2513 PyObject **defs, int defcount, PyObject *closure)
2514{
2515 register PyFrameObject *f;
2516 register PyObject *retval = NULL;
2517 register PyObject **fastlocals, **freevars;
2518 PyThreadState *tstate = PyThreadState_GET();
2519 PyObject *x, *u;
2520
2521 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00002522 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002523 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002524 return NULL;
2525 }
2526
Jeremy Hylton985eba52003-02-05 23:13:00 +00002527 assert(globals != NULL);
2528 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002529 if (f == NULL)
2530 return NULL;
2531
2532 fastlocals = f->f_localsplus;
2533 freevars = f->f_localsplus + f->f_nlocals;
2534
2535 if (co->co_argcount > 0 ||
2536 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2537 int i;
2538 int n = argcount;
2539 PyObject *kwdict = NULL;
2540 if (co->co_flags & CO_VARKEYWORDS) {
2541 kwdict = PyDict_New();
2542 if (kwdict == NULL)
2543 goto fail;
2544 i = co->co_argcount;
2545 if (co->co_flags & CO_VARARGS)
2546 i++;
2547 SETLOCAL(i, kwdict);
2548 }
2549 if (argcount > co->co_argcount) {
2550 if (!(co->co_flags & CO_VARARGS)) {
2551 PyErr_Format(PyExc_TypeError,
2552 "%.200s() takes %s %d "
2553 "%sargument%s (%d given)",
2554 PyString_AsString(co->co_name),
2555 defcount ? "at most" : "exactly",
2556 co->co_argcount,
2557 kwcount ? "non-keyword " : "",
2558 co->co_argcount == 1 ? "" : "s",
2559 argcount);
2560 goto fail;
2561 }
2562 n = co->co_argcount;
2563 }
2564 for (i = 0; i < n; i++) {
2565 x = args[i];
2566 Py_INCREF(x);
2567 SETLOCAL(i, x);
2568 }
2569 if (co->co_flags & CO_VARARGS) {
2570 u = PyTuple_New(argcount - n);
2571 if (u == NULL)
2572 goto fail;
2573 SETLOCAL(co->co_argcount, u);
2574 for (i = n; i < argcount; i++) {
2575 x = args[i];
2576 Py_INCREF(x);
2577 PyTuple_SET_ITEM(u, i-n, x);
2578 }
2579 }
2580 for (i = 0; i < kwcount; i++) {
2581 PyObject *keyword = kws[2*i];
2582 PyObject *value = kws[2*i + 1];
2583 int j;
2584 if (keyword == NULL || !PyString_Check(keyword)) {
2585 PyErr_Format(PyExc_TypeError,
2586 "%.200s() keywords must be strings",
2587 PyString_AsString(co->co_name));
2588 goto fail;
2589 }
2590 /* XXX slow -- speed up using dictionary? */
2591 for (j = 0; j < co->co_argcount; j++) {
2592 PyObject *nm = PyTuple_GET_ITEM(
2593 co->co_varnames, j);
2594 int cmp = PyObject_RichCompareBool(
2595 keyword, nm, Py_EQ);
2596 if (cmp > 0)
2597 break;
2598 else if (cmp < 0)
2599 goto fail;
2600 }
2601 /* Check errors from Compare */
2602 if (PyErr_Occurred())
2603 goto fail;
2604 if (j >= co->co_argcount) {
2605 if (kwdict == NULL) {
2606 PyErr_Format(PyExc_TypeError,
2607 "%.200s() got an unexpected "
2608 "keyword argument '%.400s'",
2609 PyString_AsString(co->co_name),
2610 PyString_AsString(keyword));
2611 goto fail;
2612 }
2613 PyDict_SetItem(kwdict, keyword, value);
2614 }
2615 else {
2616 if (GETLOCAL(j) != NULL) {
2617 PyErr_Format(PyExc_TypeError,
2618 "%.200s() got multiple "
2619 "values for keyword "
2620 "argument '%.400s'",
2621 PyString_AsString(co->co_name),
2622 PyString_AsString(keyword));
2623 goto fail;
2624 }
2625 Py_INCREF(value);
2626 SETLOCAL(j, value);
2627 }
2628 }
2629 if (argcount < co->co_argcount) {
2630 int m = co->co_argcount - defcount;
2631 for (i = argcount; i < m; i++) {
2632 if (GETLOCAL(i) == NULL) {
2633 PyErr_Format(PyExc_TypeError,
2634 "%.200s() takes %s %d "
2635 "%sargument%s (%d given)",
2636 PyString_AsString(co->co_name),
2637 ((co->co_flags & CO_VARARGS) ||
2638 defcount) ? "at least"
2639 : "exactly",
2640 m, kwcount ? "non-keyword " : "",
2641 m == 1 ? "" : "s", i);
2642 goto fail;
2643 }
2644 }
2645 if (n > m)
2646 i = n - m;
2647 else
2648 i = 0;
2649 for (; i < defcount; i++) {
2650 if (GETLOCAL(m+i) == NULL) {
2651 PyObject *def = defs[i];
2652 Py_INCREF(def);
2653 SETLOCAL(m+i, def);
2654 }
2655 }
2656 }
2657 }
2658 else {
2659 if (argcount > 0 || kwcount > 0) {
2660 PyErr_Format(PyExc_TypeError,
2661 "%.200s() takes no arguments (%d given)",
2662 PyString_AsString(co->co_name),
2663 argcount + kwcount);
2664 goto fail;
2665 }
2666 }
2667 /* Allocate and initialize storage for cell vars, and copy free
2668 vars into frame. This isn't too efficient right now. */
2669 if (f->f_ncells) {
2670 int i = 0, j = 0, nargs, found;
2671 char *cellname, *argname;
2672 PyObject *c;
2673
2674 nargs = co->co_argcount;
2675 if (co->co_flags & CO_VARARGS)
2676 nargs++;
2677 if (co->co_flags & CO_VARKEYWORDS)
2678 nargs++;
2679
2680 /* Check for cells that shadow args */
2681 for (i = 0; i < f->f_ncells && j < nargs; ++i) {
2682 cellname = PyString_AS_STRING(
2683 PyTuple_GET_ITEM(co->co_cellvars, i));
2684 found = 0;
2685 while (j < nargs) {
2686 argname = PyString_AS_STRING(
2687 PyTuple_GET_ITEM(co->co_varnames, j));
2688 if (strcmp(cellname, argname) == 0) {
2689 c = PyCell_New(GETLOCAL(j));
2690 if (c == NULL)
2691 goto fail;
2692 GETLOCAL(f->f_nlocals + i) = c;
2693 found = 1;
2694 break;
2695 }
2696 j++;
2697 }
2698 if (found == 0) {
2699 c = PyCell_New(NULL);
2700 if (c == NULL)
2701 goto fail;
2702 SETLOCAL(f->f_nlocals + i, c);
2703 }
2704 }
2705 /* Initialize any that are left */
2706 while (i < f->f_ncells) {
2707 c = PyCell_New(NULL);
2708 if (c == NULL)
2709 goto fail;
2710 SETLOCAL(f->f_nlocals + i, c);
2711 i++;
2712 }
2713 }
2714 if (f->f_nfreevars) {
2715 int i;
2716 for (i = 0; i < f->f_nfreevars; ++i) {
2717 PyObject *o = PyTuple_GET_ITEM(closure, i);
2718 Py_INCREF(o);
2719 freevars[f->f_ncells + i] = o;
2720 }
2721 }
2722
Tim Peters5ca576e2001-06-18 22:08:13 +00002723 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002724 /* Don't need to keep the reference to f_back, it will be set
2725 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002726 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002727 f->f_back = NULL;
2728
Jeremy Hylton985eba52003-02-05 23:13:00 +00002729 PCALL(PCALL_GENERATOR);
2730
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002731 /* Create a new generator that owns the ready to run frame
2732 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00002733 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00002734 }
2735
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002736 retval = PyEval_EvalFrame(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00002737
2738 fail: /* Jump here from prelude on failure */
2739
Tim Petersb13680b2001-11-27 23:29:29 +00002740 /* decref'ing the frame can cause __del__ methods to get invoked,
2741 which can call back into Python. While we're done with the
2742 current Python frame (f), the associated C stack is still in use,
2743 so recursion_depth must be boosted for the duration.
2744 */
2745 assert(tstate != NULL);
2746 ++tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002747 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00002748 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002749 return retval;
2750}
2751
2752
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002753/* Implementation notes for set_exc_info() and reset_exc_info():
2754
2755- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2756 'exc_traceback'. These always travel together.
2757
2758- tstate->curexc_ZZZ is the "hot" exception that is set by
2759 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2760
2761- Once an exception is caught by an except clause, it is transferred
2762 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2763 can pick it up. This is the primary task of set_exc_info().
2764
2765- Now let me explain the complicated dance with frame->f_exc_ZZZ.
2766
2767 Long ago, when none of this existed, there were just a few globals:
2768 one set corresponding to the "hot" exception, and one set
2769 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2770 globals; they were simply stored as sys.exc_ZZZ. For backwards
2771 compatibility, they still are!) The problem was that in code like
2772 this:
2773
2774 try:
2775 "something that may fail"
2776 except "some exception":
2777 "do something else first"
2778 "print the exception from sys.exc_ZZZ."
2779
2780 if "do something else first" invoked something that raised and caught
2781 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2782 cause of subtle bugs. I fixed this by changing the semantics as
2783 follows:
2784
2785 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2786 *in that frame*.
2787
2788 - But initially, and as long as no exception is caught in a given
2789 frame, sys.exc_ZZZ will hold the last exception caught in the
2790 previous frame (or the frame before that, etc.).
2791
2792 The first bullet fixed the bug in the above example. The second
2793 bullet was for backwards compatibility: it was (and is) common to
2794 have a function that is called when an exception is caught, and to
2795 have that function access the caught exception via sys.exc_ZZZ.
2796 (Example: traceback.print_exc()).
2797
2798 At the same time I fixed the problem that sys.exc_ZZZ weren't
2799 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2800 but that's really a separate improvement.
2801
2802 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2803 variables to what they were before the current frame was called. The
2804 set_exc_info() function saves them on the frame so that
2805 reset_exc_info() can restore them. The invariant is that
2806 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2807 exception (where "catching" an exception applies only to successful
2808 except clauses); and if the current frame ever caught an exception,
2809 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2810 at the start of the current frame.
2811
2812*/
2813
Guido van Rossuma027efa1997-05-05 20:56:21 +00002814static void
Guido van Rossumac7be682001-01-17 15:42:30 +00002815set_exc_info(PyThreadState *tstate,
2816 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002817{
2818 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002819 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00002820
Guido van Rossuma027efa1997-05-05 20:56:21 +00002821 frame = tstate->frame;
2822 if (frame->f_exc_type == NULL) {
2823 /* This frame didn't catch an exception before */
2824 /* Save previous exception of this thread in this frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002825 if (tstate->exc_type == NULL) {
2826 Py_INCREF(Py_None);
2827 tstate->exc_type = Py_None;
2828 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002829 tmp_type = frame->f_exc_type;
2830 tmp_value = frame->f_exc_value;
2831 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002832 Py_XINCREF(tstate->exc_type);
2833 Py_XINCREF(tstate->exc_value);
2834 Py_XINCREF(tstate->exc_traceback);
2835 frame->f_exc_type = tstate->exc_type;
2836 frame->f_exc_value = tstate->exc_value;
2837 frame->f_exc_traceback = tstate->exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002838 Py_XDECREF(tmp_type);
2839 Py_XDECREF(tmp_value);
2840 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002841 }
2842 /* Set new exception for this thread */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002843 tmp_type = tstate->exc_type;
2844 tmp_value = tstate->exc_value;
2845 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002846 Py_XINCREF(type);
2847 Py_XINCREF(value);
2848 Py_XINCREF(tb);
2849 tstate->exc_type = type;
2850 tstate->exc_value = value;
2851 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002852 Py_XDECREF(tmp_type);
2853 Py_XDECREF(tmp_value);
2854 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002855 /* For b/w compatibility */
2856 PySys_SetObject("exc_type", type);
2857 PySys_SetObject("exc_value", value);
2858 PySys_SetObject("exc_traceback", tb);
2859}
2860
2861static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002862reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002863{
2864 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002865 PyObject *tmp_type, *tmp_value, *tmp_tb;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002866 frame = tstate->frame;
2867 if (frame->f_exc_type != NULL) {
2868 /* This frame caught an exception */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002869 tmp_type = tstate->exc_type;
2870 tmp_value = tstate->exc_value;
2871 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002872 Py_XINCREF(frame->f_exc_type);
2873 Py_XINCREF(frame->f_exc_value);
2874 Py_XINCREF(frame->f_exc_traceback);
2875 tstate->exc_type = frame->f_exc_type;
2876 tstate->exc_value = frame->f_exc_value;
2877 tstate->exc_traceback = frame->f_exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002878 Py_XDECREF(tmp_type);
2879 Py_XDECREF(tmp_value);
2880 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002881 /* For b/w compatibility */
2882 PySys_SetObject("exc_type", frame->f_exc_type);
2883 PySys_SetObject("exc_value", frame->f_exc_value);
2884 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
2885 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002886 tmp_type = frame->f_exc_type;
2887 tmp_value = frame->f_exc_value;
2888 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002889 frame->f_exc_type = NULL;
2890 frame->f_exc_value = NULL;
2891 frame->f_exc_traceback = NULL;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002892 Py_XDECREF(tmp_type);
2893 Py_XDECREF(tmp_value);
2894 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002895}
2896
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002897/* Logic for the raise statement (too complicated for inlining).
2898 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00002899static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002900do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002901{
Guido van Rossumd295f121998-04-09 21:39:57 +00002902 if (type == NULL) {
2903 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00002904 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumd295f121998-04-09 21:39:57 +00002905 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
2906 value = tstate->exc_value;
2907 tb = tstate->exc_traceback;
2908 Py_XINCREF(type);
2909 Py_XINCREF(value);
2910 Py_XINCREF(tb);
2911 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002912
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002913 /* We support the following forms of raise:
2914 raise <class>, <classinstance>
2915 raise <class>, <argument tuple>
2916 raise <class>, None
2917 raise <class>, <argument>
2918 raise <classinstance>, None
2919 raise <string>, <object>
2920 raise <string>, None
2921
2922 An omitted second argument is the same as None.
2923
2924 In addition, raise <tuple>, <anything> is the same as
2925 raising the tuple's first item (and it better have one!);
2926 this rule is applied recursively.
2927
2928 Finally, an optional third argument can be supplied, which
2929 gives the traceback to be substituted (useful when
2930 re-raising an exception after examining it). */
2931
2932 /* First, check the traceback argument, replacing None with
2933 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002934 if (tb == Py_None) {
2935 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002936 tb = NULL;
2937 }
2938 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002939 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002940 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002941 goto raise_error;
2942 }
2943
2944 /* Next, replace a missing value with None */
2945 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002946 value = Py_None;
2947 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002948 }
2949
2950 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00002951 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
2952 PyObject *tmp = type;
2953 type = PyTuple_GET_ITEM(type, 0);
2954 Py_INCREF(type);
2955 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002956 }
2957
Tim Petersafb2c802002-04-18 18:06:20 +00002958 if (PyString_CheckExact(type))
2959 /* Raising builtin string is deprecated but still allowed --
2960 * do nothing. Raising an instance of a new-style str
2961 * subclass is right out. */
Neal Norwitz37aa0662003-01-10 15:31:15 +00002962 PyErr_Warn(PyExc_PendingDeprecationWarning,
2963 "raising a string exception is deprecated");
Barry Warsaw4249f541997-08-22 21:26:19 +00002964
2965 else if (PyClass_Check(type))
2966 PyErr_NormalizeException(&type, &value, &tb);
2967
Guido van Rossumb209a111997-04-29 18:18:01 +00002968 else if (PyInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002969 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002970 if (value != Py_None) {
2971 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002972 "instance exception may not have a separate value");
2973 goto raise_error;
2974 }
2975 else {
2976 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00002977 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002978 value = type;
Guido van Rossumb209a111997-04-29 18:18:01 +00002979 type = (PyObject*) ((PyInstanceObject*)type)->in_class;
2980 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002981 }
2982 }
2983 else {
2984 /* Not something you can raise. You get an exception
2985 anyway, just not what you specified :-) */
Jeremy Hylton960d9482001-04-27 02:25:33 +00002986 PyErr_Format(PyExc_TypeError,
Neal Norwitz37aa0662003-01-10 15:31:15 +00002987 "exceptions must be classes, instances, or "
2988 "strings (deprecated), not %s",
2989 type->ob_type->tp_name);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002990 goto raise_error;
2991 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002992 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002993 if (tb == NULL)
2994 return WHY_EXCEPTION;
2995 else
2996 return WHY_RERAISE;
2997 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00002998 Py_XDECREF(value);
2999 Py_XDECREF(type);
3000 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003001 return WHY_EXCEPTION;
3002}
3003
Tim Petersd6d010b2001-06-21 02:49:55 +00003004/* Iterate v argcnt times and store the results on the stack (via decreasing
3005 sp). Return 1 for success, 0 if error. */
3006
Barry Warsawe42b18f1997-08-25 22:13:04 +00003007static int
Tim Petersd6d010b2001-06-21 02:49:55 +00003008unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003009{
Tim Petersd6d010b2001-06-21 02:49:55 +00003010 int i = 0;
3011 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00003012 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00003013
Tim Petersd6d010b2001-06-21 02:49:55 +00003014 assert(v != NULL);
3015
3016 it = PyObject_GetIter(v);
3017 if (it == NULL)
3018 goto Error;
3019
3020 for (; i < argcnt; i++) {
3021 w = PyIter_Next(it);
3022 if (w == NULL) {
3023 /* Iterator done, via error or exhaustion. */
3024 if (!PyErr_Occurred()) {
3025 PyErr_Format(PyExc_ValueError,
3026 "need more than %d value%s to unpack",
3027 i, i == 1 ? "" : "s");
3028 }
3029 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003030 }
3031 *--sp = w;
3032 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003033
3034 /* We better have exhausted the iterator now. */
3035 w = PyIter_Next(it);
3036 if (w == NULL) {
3037 if (PyErr_Occurred())
3038 goto Error;
3039 Py_DECREF(it);
3040 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003041 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00003042 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00003043 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00003044 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00003045Error:
Barry Warsaw91010551997-08-25 22:30:51 +00003046 for (; i > 0; i--, sp++)
3047 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00003048 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00003049 return 0;
3050}
3051
3052
Guido van Rossum96a42c81992-01-12 02:29:51 +00003053#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003054static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003055prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003056{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003057 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003058 if (PyObject_Print(v, stdout, 0) != 0)
3059 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003060 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003061 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003062}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003063#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003064
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003065static void
Fred Drake5755ce62001-06-27 19:19:46 +00003066call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003067{
Guido van Rossumb209a111997-04-29 18:18:01 +00003068 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003069 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003070 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003071 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003072 value = Py_None;
3073 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003074 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003075 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003076 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003077 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003078 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003079 }
Fred Drake5755ce62001-06-27 19:19:46 +00003080 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003081 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003082 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003083 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003084 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003085 Py_XDECREF(type);
3086 Py_XDECREF(value);
3087 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003088 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003089}
3090
Fred Drake4ec5d562001-10-04 19:26:43 +00003091static void
3092call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3093 int what)
3094{
3095 PyObject *type, *value, *traceback;
3096 int err;
3097 PyErr_Fetch(&type, &value, &traceback);
3098 err = call_trace(func, obj, frame, what, NULL);
3099 if (err == 0)
3100 PyErr_Restore(type, value, traceback);
3101 else {
3102 Py_XDECREF(type);
3103 Py_XDECREF(value);
3104 Py_XDECREF(traceback);
3105 }
3106}
3107
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003108static int
Fred Drake5755ce62001-06-27 19:19:46 +00003109call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3110 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003111{
Fred Drake5755ce62001-06-27 19:19:46 +00003112 register PyThreadState *tstate = frame->f_tstate;
3113 int result;
3114 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003115 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003116 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003117 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003118 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003119 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3120 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003121 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003122 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003123}
3124
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003125PyObject *
3126_PyEval_CallTracing(PyObject *func, PyObject *args)
3127{
3128 PyFrameObject *frame = PyEval_GetFrame();
3129 PyThreadState *tstate = frame->f_tstate;
3130 int save_tracing = tstate->tracing;
3131 int save_use_tracing = tstate->use_tracing;
3132 PyObject *result;
3133
3134 tstate->tracing = 0;
3135 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3136 || (tstate->c_profilefunc != NULL));
3137 result = PyObject_Call(func, args, NULL);
3138 tstate->tracing = save_tracing;
3139 tstate->use_tracing = save_use_tracing;
3140 return result;
3141}
3142
Michael W. Hudson006c7522002-11-08 13:08:46 +00003143static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003144maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003145 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3146 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003147{
3148 /* The theory of SET_LINENO-less tracing.
Tim Peters8a5c3c72004-04-05 19:36:21 +00003149
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003150 In a nutshell, we use the co_lnotab field of the code object
3151 to tell when execution has moved onto a different line.
3152
3153 As mentioned above, the basic idea is so set things up so
3154 that
3155
3156 *instr_lb <= frame->f_lasti < *instr_ub
3157
3158 is true so long as execution does not change lines.
3159
3160 This is all fairly simple. Digging the information out of
3161 co_lnotab takes some work, but is conceptually clear.
3162
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003163 Somewhat harder to explain is why we don't *always* call the
3164 line trace function when the above test fails.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003165
3166 Consider this code:
3167
3168 1: def f(a):
3169 2: if a:
3170 3: print 1
3171 4: else:
3172 5: print 2
3173
3174 which compiles to this:
3175
3176 2 0 LOAD_FAST 0 (a)
3177 3 JUMP_IF_FALSE 9 (to 15)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003178 6 POP_TOP
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003179
3180 3 7 LOAD_CONST 1 (1)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003181 10 PRINT_ITEM
3182 11 PRINT_NEWLINE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003183 12 JUMP_FORWARD 6 (to 21)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003184 >> 15 POP_TOP
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003185
3186 5 16 LOAD_CONST 2 (2)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003187 19 PRINT_ITEM
3188 20 PRINT_NEWLINE
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003189 >> 21 LOAD_CONST 0 (None)
3190 24 RETURN_VALUE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003191
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003192 If 'a' is false, execution will jump to instruction at offset
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003193 15 and the co_lnotab will claim that execution has moved to
3194 line 3. This is at best misleading. In this case we could
3195 associate the POP_TOP with line 4, but that doesn't make
3196 sense in all cases (I think).
3197
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003198 What we do is only call the line trace function if the co_lnotab
3199 indicates we have jumped to the *start* of a line, i.e. if the
3200 current instruction offset matches the offset given for the
3201 start of a line by the co_lnotab.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003202
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003203 This also takes care of the situation where 'a' is true.
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003204 Execution will jump from instruction offset 12 to offset 21.
3205 Then the co_lnotab would imply that execution has moved to line
3206 5, which is again misleading.
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003207
3208 Why do we set f_lineno when tracing? Well, consider the code
3209 above when 'a' is true. If stepping through this with 'n' in
3210 pdb, you would stop at line 1 with a "call" type event, then
3211 line events on lines 2 and 3, then a "return" type event -- but
3212 you would be shown line 5 during this event. This is a change
3213 from the behaviour in 2.2 and before, and I've found it
3214 confusing in practice. By setting and using f_lineno when
3215 tracing, one can report a line number different from that
3216 suggested by f_lasti on this one occasion where it's desirable.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003217 */
3218
Michael W. Hudson006c7522002-11-08 13:08:46 +00003219 int result = 0;
3220
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003221 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003222 PyCodeObject* co = frame->f_code;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003223 int size, addr, line;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003224 unsigned char* p;
3225
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003226 size = PyString_GET_SIZE(co->co_lnotab) / 2;
3227 p = (unsigned char*)PyString_AS_STRING(co->co_lnotab);
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003228
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003229 addr = 0;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003230 line = co->co_firstlineno;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003231
3232 /* possible optimization: if f->f_lasti == instr_ub
3233 (likely to be a common case) then we already know
3234 instr_lb -- if we stored the matching value of p
3235 somwhere we could skip the first while loop. */
3236
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003237 /* see comments in compile.c for the description of
3238 co_lnotab. A point to remember: increments to p
3239 should come in pairs -- although we don't care about
3240 the line increments here, treating them as byte
3241 increments gets confusing, to say the least. */
3242
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003243 while (size > 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003244 if (addr + *p > frame->f_lasti)
3245 break;
3246 addr += *p++;
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003247 if (*p) *instr_lb = addr;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003248 line += *p++;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003249 --size;
3250 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003251
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003252 if (addr == frame->f_lasti) {
3253 frame->f_lineno = line;
Tim Peters8a5c3c72004-04-05 19:36:21 +00003254 result = call_trace(func, obj, frame,
Michael W. Hudson006c7522002-11-08 13:08:46 +00003255 PyTrace_LINE, Py_None);
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003256 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003257
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003258 if (size > 0) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003259 while (--size >= 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003260 addr += *p++;
3261 if (*p++)
3262 break;
3263 }
3264 *instr_ub = addr;
3265 }
3266 else {
3267 *instr_ub = INT_MAX;
3268 }
3269 }
Armin Rigobf57a142004-03-22 19:24:58 +00003270 else if (frame->f_lasti <= *instr_prev) {
3271 /* jumping back in the same line forces a trace event */
Tim Peters8a5c3c72004-04-05 19:36:21 +00003272 result = call_trace(func, obj, frame,
Armin Rigobf57a142004-03-22 19:24:58 +00003273 PyTrace_LINE, Py_None);
3274 }
3275 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003276 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003277}
3278
Fred Drake5755ce62001-06-27 19:19:46 +00003279void
3280PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003281{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003282 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003283 PyObject *temp = tstate->c_profileobj;
3284 Py_XINCREF(arg);
3285 tstate->c_profilefunc = NULL;
3286 tstate->c_profileobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003287 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003288 Py_XDECREF(temp);
3289 tstate->c_profilefunc = func;
3290 tstate->c_profileobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003291 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003292}
3293
3294void
3295PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3296{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003297 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003298 PyObject *temp = tstate->c_traceobj;
3299 Py_XINCREF(arg);
3300 tstate->c_tracefunc = NULL;
3301 tstate->c_traceobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003302 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003303 Py_XDECREF(temp);
3304 tstate->c_tracefunc = func;
3305 tstate->c_traceobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003306 tstate->use_tracing = ((func != NULL)
3307 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003308}
3309
Guido van Rossumb209a111997-04-29 18:18:01 +00003310PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003311PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003312{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003313 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003314 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003315 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003316 else
3317 return current_frame->f_builtins;
3318}
3319
Guido van Rossumb209a111997-04-29 18:18:01 +00003320PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003321PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003322{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003323 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003324 if (current_frame == NULL)
3325 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003326 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003327 return current_frame->f_locals;
3328}
3329
Guido van Rossumb209a111997-04-29 18:18:01 +00003330PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003331PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003332{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003333 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003334 if (current_frame == NULL)
3335 return NULL;
3336 else
3337 return current_frame->f_globals;
3338}
3339
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003340PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003341PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003342{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003343 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003344 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003345}
3346
Guido van Rossum6135a871995-01-09 17:53:26 +00003347int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003348PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003349{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003350 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003351 return current_frame == NULL ? 0 : current_frame->f_restricted;
3352}
3353
Guido van Rossumbe270261997-05-22 22:26:18 +00003354int
Tim Peters5ba58662001-07-16 02:29:45 +00003355PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003356{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003357 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003358 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003359
3360 if (current_frame != NULL) {
3361 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003362 const int compilerflags = codeflags & PyCF_MASK;
3363 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003364 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003365 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003366 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003367#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003368 if (codeflags & CO_GENERATOR_ALLOWED) {
3369 result = 1;
3370 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3371 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003372#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003373 }
3374 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003375}
3376
3377int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003378Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003379{
Guido van Rossumb209a111997-04-29 18:18:01 +00003380 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003381 if (f == NULL)
3382 return 0;
3383 if (!PyFile_SoftSpace(f, 0))
3384 return 0;
3385 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003386}
3387
Guido van Rossum3f5da241990-12-20 15:06:42 +00003388
Guido van Rossum681d79a1995-07-18 14:51:37 +00003389/* External interface to call any callable object.
3390 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003391
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003392#undef PyEval_CallObject
3393/* for backward compatibility: export this interface */
3394
Guido van Rossumb209a111997-04-29 18:18:01 +00003395PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003396PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003397{
Guido van Rossumb209a111997-04-29 18:18:01 +00003398 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003399}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003400#define PyEval_CallObject(func,arg) \
3401 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003402
Guido van Rossumb209a111997-04-29 18:18:01 +00003403PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003404PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003405{
Jeremy Hylton52820442001-01-03 23:52:36 +00003406 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003407
3408 if (arg == NULL)
Guido van Rossumb209a111997-04-29 18:18:01 +00003409 arg = PyTuple_New(0);
3410 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003411 PyErr_SetString(PyExc_TypeError,
3412 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003413 return NULL;
3414 }
3415 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003416 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003417
Guido van Rossumb209a111997-04-29 18:18:01 +00003418 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003419 PyErr_SetString(PyExc_TypeError,
3420 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003421 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003422 return NULL;
3423 }
3424
Tim Peters6d6c1a32001-08-02 04:15:00 +00003425 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003426 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003427 return result;
3428}
3429
Tim Peters6d6c1a32001-08-02 04:15:00 +00003430char *
3431PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003432{
3433 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003434 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003435 else if (PyFunction_Check(func))
3436 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3437 else if (PyCFunction_Check(func))
3438 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3439 else if (PyClass_Check(func))
3440 return PyString_AsString(((PyClassObject*)func)->cl_name);
3441 else if (PyInstance_Check(func)) {
3442 return PyString_AsString(
3443 ((PyInstanceObject*)func)->in_class->cl_name);
3444 } else {
3445 return func->ob_type->tp_name;
3446 }
3447}
3448
Tim Peters6d6c1a32001-08-02 04:15:00 +00003449char *
3450PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003451{
3452 if (PyMethod_Check(func))
3453 return "()";
3454 else if (PyFunction_Check(func))
3455 return "()";
3456 else if (PyCFunction_Check(func))
3457 return "()";
3458 else if (PyClass_Check(func))
3459 return " constructor";
3460 else if (PyInstance_Check(func)) {
3461 return " instance";
3462 } else {
3463 return " object";
3464 }
3465}
3466
Jeremy Hylton52820442001-01-03 23:52:36 +00003467#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
3468
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003469static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003470err_args(PyObject *func, int flags, int nargs)
3471{
3472 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003473 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003474 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003475 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003476 nargs);
3477 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003478 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003479 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003480 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003481 nargs);
3482}
3483
Nicholas Bastind858a772004-06-25 23:31:06 +00003484#define C_TRACE(call) \
3485if (tstate->use_tracing && tstate->c_profilefunc) { \
3486 if (call_trace(tstate->c_profilefunc, \
3487 tstate->c_profileobj, \
3488 tstate->frame, PyTrace_C_CALL, \
3489 func)) \
3490 { return NULL; } \
3491 call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003492 if (tstate->c_profilefunc != NULL) { \
Nicholas Bastind858a772004-06-25 23:31:06 +00003493 if (x == NULL) { \
3494 if (call_trace (tstate->c_profilefunc, \
3495 tstate->c_profileobj, \
3496 tstate->frame, PyTrace_C_EXCEPTION, \
3497 func)) \
3498 { return NULL; } \
3499 } else { \
3500 if (call_trace(tstate->c_profilefunc, \
3501 tstate->c_profileobj, \
3502 tstate->frame, PyTrace_C_RETURN, \
3503 func)) \
3504 { return NULL; } \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003505 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003506 } \
3507} else { \
3508 call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003509 }
3510
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003511static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003512call_function(PyObject ***pp_stack, int oparg
3513#ifdef WITH_TSC
3514 , uint64* pintr0, uint64* pintr1
3515#endif
3516 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003517{
3518 int na = oparg & 0xff;
3519 int nk = (oparg>>8) & 0xff;
3520 int n = na + 2 * nk;
3521 PyObject **pfunc = (*pp_stack) - n - 1;
3522 PyObject *func = *pfunc;
3523 PyObject *x, *w;
3524
Jeremy Hylton985eba52003-02-05 23:13:00 +00003525 /* Always dispatch PyCFunction first, because these are
3526 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003527 */
3528 if (PyCFunction_Check(func) && nk == 0) {
3529 int flags = PyCFunction_GET_FLAGS(func);
Nicholas Bastind858a772004-06-25 23:31:06 +00003530 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003531
3532 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003533 if (flags & (METH_NOARGS | METH_O)) {
3534 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3535 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003536 if (flags & METH_NOARGS && na == 0) {
Nicholas Bastind858a772004-06-25 23:31:06 +00003537 C_TRACE(x=(*meth)(self,NULL));
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003538 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003539 else if (flags & METH_O && na == 1) {
3540 PyObject *arg = EXT_POP(*pp_stack);
Nicholas Bastind858a772004-06-25 23:31:06 +00003541 C_TRACE(x=(*meth)(self,arg));
Jeremy Hylton192690e2002-08-16 18:36:11 +00003542 Py_DECREF(arg);
3543 }
3544 else {
3545 err_args(func, flags, na);
3546 x = NULL;
3547 }
3548 }
3549 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003550 PyObject *callargs;
3551 callargs = load_args(pp_stack, na);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003552 READ_TIMESTAMP(*pintr0);
Nicholas Bastind858a772004-06-25 23:31:06 +00003553 C_TRACE(x=PyCFunction_Call(func,callargs,NULL));
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003554 READ_TIMESTAMP(*pintr1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003555 Py_XDECREF(callargs);
3556 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003557 } else {
3558 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3559 /* optimize access to bound methods */
3560 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003561 PCALL(PCALL_METHOD);
3562 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003563 Py_INCREF(self);
3564 func = PyMethod_GET_FUNCTION(func);
3565 Py_INCREF(func);
3566 Py_DECREF(*pfunc);
3567 *pfunc = self;
3568 na++;
3569 n++;
3570 } else
3571 Py_INCREF(func);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003572 READ_TIMESTAMP(*pintr0);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003573 if (PyFunction_Check(func))
3574 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003575 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003576 x = do_call(func, pp_stack, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003577 READ_TIMESTAMP(*pintr1);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003578 Py_DECREF(func);
3579 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003580
Jeremy Hylton985eba52003-02-05 23:13:00 +00003581 /* What does this do? */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003582 while ((*pp_stack) > pfunc) {
3583 w = EXT_POP(*pp_stack);
3584 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003585 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003586 }
3587 return x;
3588}
3589
Jeremy Hylton192690e2002-08-16 18:36:11 +00003590/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003591 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003592 For the simplest case -- a function that takes only positional
3593 arguments and is called with only positional arguments -- it
3594 inlines the most primitive frame setup code from
3595 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3596 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003597*/
3598
3599static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003600fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003601{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003602 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003603 PyObject *globals = PyFunction_GET_GLOBALS(func);
3604 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3605 PyObject **d = NULL;
3606 int nd = 0;
3607
Jeremy Hylton985eba52003-02-05 23:13:00 +00003608 PCALL(PCALL_FUNCTION);
3609 PCALL(PCALL_FAST_FUNCTION);
Raymond Hettinger40174c32003-05-31 07:04:16 +00003610 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003611 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3612 PyFrameObject *f;
3613 PyObject *retval = NULL;
3614 PyThreadState *tstate = PyThreadState_GET();
3615 PyObject **fastlocals, **stack;
3616 int i;
3617
3618 PCALL(PCALL_FASTER_FUNCTION);
3619 assert(globals != NULL);
3620 /* XXX Perhaps we should create a specialized
3621 PyFrame_New() that doesn't take locals, but does
3622 take builtins without sanity checking them.
3623 */
3624 f = PyFrame_New(tstate, co, globals, NULL);
3625 if (f == NULL)
3626 return NULL;
3627
3628 fastlocals = f->f_localsplus;
3629 stack = (*pp_stack) - n;
3630
3631 for (i = 0; i < n; i++) {
3632 Py_INCREF(*stack);
3633 fastlocals[i] = *stack++;
3634 }
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003635 retval = PyEval_EvalFrame(f);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003636 assert(tstate != NULL);
3637 ++tstate->recursion_depth;
3638 Py_DECREF(f);
3639 --tstate->recursion_depth;
3640 return retval;
3641 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003642 if (argdefs != NULL) {
3643 d = &PyTuple_GET_ITEM(argdefs, 0);
3644 nd = ((PyTupleObject *)argdefs)->ob_size;
3645 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003646 return PyEval_EvalCodeEx(co, globals,
3647 (PyObject *)NULL, (*pp_stack)-n, na,
3648 (*pp_stack)-2*nk, nk, d, nd,
3649 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003650}
3651
3652static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003653update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3654 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003655{
3656 PyObject *kwdict = NULL;
3657 if (orig_kwdict == NULL)
3658 kwdict = PyDict_New();
3659 else {
3660 kwdict = PyDict_Copy(orig_kwdict);
3661 Py_DECREF(orig_kwdict);
3662 }
3663 if (kwdict == NULL)
3664 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003665 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003666 int err;
3667 PyObject *value = EXT_POP(*pp_stack);
3668 PyObject *key = EXT_POP(*pp_stack);
3669 if (PyDict_GetItem(kwdict, key) != NULL) {
Guido van Rossumac7be682001-01-17 15:42:30 +00003670 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +00003671 "%.200s%s got multiple values "
Jeremy Hylton512a2372001-04-11 13:52:29 +00003672 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003673 PyEval_GetFuncName(func),
3674 PyEval_GetFuncDesc(func),
Jeremy Hylton512a2372001-04-11 13:52:29 +00003675 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003676 Py_DECREF(key);
3677 Py_DECREF(value);
3678 Py_DECREF(kwdict);
3679 return NULL;
3680 }
3681 err = PyDict_SetItem(kwdict, key, value);
3682 Py_DECREF(key);
3683 Py_DECREF(value);
3684 if (err) {
3685 Py_DECREF(kwdict);
3686 return NULL;
3687 }
3688 }
3689 return kwdict;
3690}
3691
3692static PyObject *
3693update_star_args(int nstack, int nstar, PyObject *stararg,
3694 PyObject ***pp_stack)
3695{
3696 PyObject *callargs, *w;
3697
3698 callargs = PyTuple_New(nstack + nstar);
3699 if (callargs == NULL) {
3700 return NULL;
3701 }
3702 if (nstar) {
3703 int i;
3704 for (i = 0; i < nstar; i++) {
3705 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3706 Py_INCREF(a);
3707 PyTuple_SET_ITEM(callargs, nstack + i, a);
3708 }
3709 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003710 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003711 w = EXT_POP(*pp_stack);
3712 PyTuple_SET_ITEM(callargs, nstack, w);
3713 }
3714 return callargs;
3715}
3716
3717static PyObject *
3718load_args(PyObject ***pp_stack, int na)
3719{
3720 PyObject *args = PyTuple_New(na);
3721 PyObject *w;
3722
3723 if (args == NULL)
3724 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003725 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003726 w = EXT_POP(*pp_stack);
3727 PyTuple_SET_ITEM(args, na, w);
3728 }
3729 return args;
3730}
3731
3732static PyObject *
3733do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3734{
3735 PyObject *callargs = NULL;
3736 PyObject *kwdict = NULL;
3737 PyObject *result = NULL;
3738
3739 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003740 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003741 if (kwdict == NULL)
3742 goto call_fail;
3743 }
3744 callargs = load_args(pp_stack, na);
3745 if (callargs == NULL)
3746 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003747#ifdef CALL_PROFILE
3748 /* At this point, we have to look at the type of func to
3749 update the call stats properly. Do it here so as to avoid
3750 exposing the call stats machinery outside ceval.c
3751 */
3752 if (PyFunction_Check(func))
3753 PCALL(PCALL_FUNCTION);
3754 else if (PyMethod_Check(func))
3755 PCALL(PCALL_METHOD);
3756 else if (PyType_Check(func))
3757 PCALL(PCALL_TYPE);
3758 else
3759 PCALL(PCALL_OTHER);
3760#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003761 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003762 call_fail:
3763 Py_XDECREF(callargs);
3764 Py_XDECREF(kwdict);
3765 return result;
3766}
3767
3768static PyObject *
3769ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3770{
3771 int nstar = 0;
3772 PyObject *callargs = NULL;
3773 PyObject *stararg = NULL;
3774 PyObject *kwdict = NULL;
3775 PyObject *result = NULL;
3776
3777 if (flags & CALL_FLAG_KW) {
3778 kwdict = EXT_POP(*pp_stack);
3779 if (!(kwdict && PyDict_Check(kwdict))) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003780 PyErr_Format(PyExc_TypeError,
Jeremy Hylton512a2372001-04-11 13:52:29 +00003781 "%s%s argument after ** "
3782 "must be a dictionary",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003783 PyEval_GetFuncName(func),
3784 PyEval_GetFuncDesc(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003785 goto ext_call_fail;
3786 }
3787 }
3788 if (flags & CALL_FLAG_VAR) {
3789 stararg = EXT_POP(*pp_stack);
3790 if (!PyTuple_Check(stararg)) {
3791 PyObject *t = NULL;
3792 t = PySequence_Tuple(stararg);
3793 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003794 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3795 PyErr_Format(PyExc_TypeError,
3796 "%s%s argument after * "
3797 "must be a sequence",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003798 PyEval_GetFuncName(func),
3799 PyEval_GetFuncDesc(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003800 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003801 goto ext_call_fail;
3802 }
3803 Py_DECREF(stararg);
3804 stararg = t;
3805 }
3806 nstar = PyTuple_GET_SIZE(stararg);
3807 }
3808 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003809 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003810 if (kwdict == NULL)
3811 goto ext_call_fail;
3812 }
3813 callargs = update_star_args(na, nstar, stararg, pp_stack);
3814 if (callargs == NULL)
3815 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003816#ifdef CALL_PROFILE
3817 /* At this point, we have to look at the type of func to
3818 update the call stats properly. Do it here so as to avoid
3819 exposing the call stats machinery outside ceval.c
3820 */
3821 if (PyFunction_Check(func))
3822 PCALL(PCALL_FUNCTION);
3823 else if (PyMethod_Check(func))
3824 PCALL(PCALL_METHOD);
3825 else if (PyType_Check(func))
3826 PCALL(PCALL_TYPE);
3827 else
3828 PCALL(PCALL_OTHER);
3829#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003830 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003831 ext_call_fail:
3832 Py_XDECREF(callargs);
3833 Py_XDECREF(kwdict);
3834 Py_XDECREF(stararg);
3835 return result;
3836}
3837
Tim Peterscb479e72001-12-16 19:11:44 +00003838/* Extract a slice index from a PyInt or PyLong, and store in *pi.
3839 Silently reduce values larger than INT_MAX to INT_MAX, and silently
3840 boost values less than -INT_MAX to 0. Return 0 on error, 1 on success.
3841*/
Tim Petersb5196382001-12-16 19:44:20 +00003842/* Note: If v is NULL, return success without storing into *pi. This
3843 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3844 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00003845*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00003846int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003847_PyEval_SliceIndex(PyObject *v, int *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003848{
Tim Petersb5196382001-12-16 19:44:20 +00003849 if (v != NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003850 long x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003851 if (PyInt_Check(v)) {
3852 x = PyInt_AsLong(v);
3853 } else if (PyLong_Check(v)) {
3854 x = PyLong_AsLong(v);
3855 if (x==-1 && PyErr_Occurred()) {
3856 PyObject *long_zero;
Guido van Rossumac7be682001-01-17 15:42:30 +00003857 int cmp;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003858
Guido van Rossumac7be682001-01-17 15:42:30 +00003859 if (!PyErr_ExceptionMatches(
3860 PyExc_OverflowError)) {
3861 /* It's not an overflow error, so just
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003862 signal an error */
Guido van Rossum20c6add2000-05-08 14:06:50 +00003863 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003864 }
3865
Guido van Rossumac7be682001-01-17 15:42:30 +00003866 /* Clear the OverflowError */
3867 PyErr_Clear();
3868
3869 /* It's an overflow error, so we need to
3870 check the sign of the long integer,
Tim Peters8a5c3c72004-04-05 19:36:21 +00003871 set the value to INT_MAX or -INT_MAX,
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003872 and clear the error. */
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003873
3874 /* Create a long integer with a value of 0 */
Guido van Rossumac7be682001-01-17 15:42:30 +00003875 long_zero = PyLong_FromLong(0L);
Tim Peterscb479e72001-12-16 19:11:44 +00003876 if (long_zero == NULL)
3877 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003878
3879 /* Check sign */
Guido van Rossumac7be682001-01-17 15:42:30 +00003880 cmp = PyObject_RichCompareBool(v, long_zero,
3881 Py_GT);
3882 Py_DECREF(long_zero);
3883 if (cmp < 0)
3884 return 0;
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003885 else if (cmp)
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003886 x = INT_MAX;
3887 else
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003888 x = -INT_MAX;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003889 }
3890 } else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003891 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003892 "slice indices must be integers");
Guido van Rossum20c6add2000-05-08 14:06:50 +00003893 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003894 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003895 /* Truncate -- very long indices are truncated anyway */
3896 if (x > INT_MAX)
3897 x = INT_MAX;
3898 else if (x < -INT_MAX)
Michael W. Hudsoncbd6fb92002-11-06 15:17:32 +00003899 x = -INT_MAX;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003900 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003901 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00003902 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003903}
3904
Guido van Rossum50d756e2001-08-18 17:43:36 +00003905#undef ISINT
3906#define ISINT(x) ((x) == NULL || PyInt_Check(x) || PyLong_Check(x))
3907
Guido van Rossumb209a111997-04-29 18:18:01 +00003908static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003909apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003910{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003911 PyTypeObject *tp = u->ob_type;
3912 PySequenceMethods *sq = tp->tp_as_sequence;
3913
3914 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3915 int ilow = 0, ihigh = INT_MAX;
3916 if (!_PyEval_SliceIndex(v, &ilow))
3917 return NULL;
3918 if (!_PyEval_SliceIndex(w, &ihigh))
3919 return NULL;
3920 return PySequence_GetSlice(u, ilow, ihigh);
3921 }
3922 else {
3923 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00003924 if (slice != NULL) {
3925 PyObject *res = PyObject_GetItem(u, slice);
3926 Py_DECREF(slice);
3927 return res;
3928 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00003929 else
3930 return NULL;
3931 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003932}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003933
3934static int
Guido van Rossumac7be682001-01-17 15:42:30 +00003935assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
3936 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003937{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003938 PyTypeObject *tp = u->ob_type;
3939 PySequenceMethods *sq = tp->tp_as_sequence;
3940
3941 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3942 int ilow = 0, ihigh = INT_MAX;
3943 if (!_PyEval_SliceIndex(v, &ilow))
3944 return -1;
3945 if (!_PyEval_SliceIndex(w, &ihigh))
3946 return -1;
3947 if (x == NULL)
3948 return PySequence_DelSlice(u, ilow, ihigh);
3949 else
3950 return PySequence_SetSlice(u, ilow, ihigh, x);
3951 }
3952 else {
3953 PyObject *slice = PySlice_New(v, w, NULL);
3954 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00003955 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003956 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00003957 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00003958 else
Guido van Rossum354797c2001-12-03 19:45:06 +00003959 res = PyObject_DelItem(u, slice);
3960 Py_DECREF(slice);
3961 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003962 }
3963 else
3964 return -1;
3965 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003966}
3967
Guido van Rossumb209a111997-04-29 18:18:01 +00003968static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003969cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003970{
Guido van Rossumac7be682001-01-17 15:42:30 +00003971 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003972 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00003973 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003974 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003975 break;
3976 case PyCmp_IS_NOT:
3977 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003978 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003979 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003980 res = PySequence_Contains(w, v);
3981 if (res < 0)
3982 return NULL;
3983 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003984 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00003985 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003986 if (res < 0)
3987 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003988 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003989 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003990 case PyCmp_EXC_MATCH:
Barry Warsaw4249f541997-08-22 21:26:19 +00003991 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003992 break;
3993 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00003994 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003995 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003996 v = res ? Py_True : Py_False;
3997 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003998 return v;
3999}
4000
Thomas Wouters52152252000-08-17 22:55:00 +00004001static PyObject *
4002import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004003{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004004 PyObject *x;
4005
4006 x = PyObject_GetAttr(v, name);
4007 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00004008 PyErr_Format(PyExc_ImportError,
4009 "cannot import name %.230s",
4010 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004011 }
Thomas Wouters52152252000-08-17 22:55:00 +00004012 return x;
4013}
Guido van Rossumac7be682001-01-17 15:42:30 +00004014
Thomas Wouters52152252000-08-17 22:55:00 +00004015static int
4016import_all_from(PyObject *locals, PyObject *v)
4017{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004018 PyObject *all = PyObject_GetAttrString(v, "__all__");
4019 PyObject *dict, *name, *value;
4020 int skip_leading_underscores = 0;
4021 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004022
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004023 if (all == NULL) {
4024 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4025 return -1; /* Unexpected error */
4026 PyErr_Clear();
4027 dict = PyObject_GetAttrString(v, "__dict__");
4028 if (dict == NULL) {
4029 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4030 return -1;
4031 PyErr_SetString(PyExc_ImportError,
4032 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00004033 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004034 }
4035 all = PyMapping_Keys(dict);
4036 Py_DECREF(dict);
4037 if (all == NULL)
4038 return -1;
4039 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004040 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004041
4042 for (pos = 0, err = 0; ; pos++) {
4043 name = PySequence_GetItem(all, pos);
4044 if (name == NULL) {
4045 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4046 err = -1;
4047 else
4048 PyErr_Clear();
4049 break;
4050 }
4051 if (skip_leading_underscores &&
4052 PyString_Check(name) &&
4053 PyString_AS_STRING(name)[0] == '_')
4054 {
4055 Py_DECREF(name);
4056 continue;
4057 }
4058 value = PyObject_GetAttr(v, name);
4059 if (value == NULL)
4060 err = -1;
4061 else
4062 err = PyDict_SetItem(locals, name, value);
4063 Py_DECREF(name);
4064 Py_XDECREF(value);
4065 if (err != 0)
4066 break;
4067 }
4068 Py_DECREF(all);
4069 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004070}
4071
Guido van Rossumb209a111997-04-29 18:18:01 +00004072static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004073build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004074{
Guido van Rossum7851eea2001-09-12 19:19:18 +00004075 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004076
4077 if (PyDict_Check(methods))
4078 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00004079 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00004080 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004081 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4082 base = PyTuple_GET_ITEM(bases, 0);
4083 metaclass = PyObject_GetAttrString(base, "__class__");
4084 if (metaclass == NULL) {
4085 PyErr_Clear();
4086 metaclass = (PyObject *)base->ob_type;
4087 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00004088 }
4089 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004090 else {
4091 PyObject *g = PyEval_GetGlobals();
4092 if (g != NULL && PyDict_Check(g))
4093 metaclass = PyDict_GetItemString(g, "__metaclass__");
4094 if (metaclass == NULL)
4095 metaclass = (PyObject *) &PyClass_Type;
4096 Py_INCREF(metaclass);
4097 }
4098 result = PyObject_CallFunction(metaclass, "OOO", name, bases, methods);
4099 Py_DECREF(metaclass);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004100 if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
4101 /* A type error here likely means that the user passed
4102 in a base that was not a class (such the random module
4103 instead of the random.random type). Help them out with
Raymond Hettingercfc31922004-09-16 16:41:57 +00004104 by augmenting the error message with more information.*/
4105
4106 PyObject *ptype, *pvalue, *ptraceback;
4107
4108 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
4109 if (PyString_Check(pvalue)) {
4110 PyObject *newmsg;
4111 newmsg = PyString_FromFormat(
4112 "Error when calling the metaclass bases\n %s",
4113 PyString_AS_STRING(pvalue));
4114 if (newmsg != NULL) {
4115 Py_DECREF(pvalue);
4116 pvalue = newmsg;
4117 }
4118 }
4119 PyErr_Restore(ptype, pvalue, ptraceback);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004120 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004121 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00004122}
4123
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004124static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004125exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
4126 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004127{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004128 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00004129 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004130 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004131
Guido van Rossumb209a111997-04-29 18:18:01 +00004132 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
4133 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004134 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00004135 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004136 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00004137 locals = PyTuple_GetItem(prog, 2);
4138 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004139 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004140 if (globals == Py_None) {
4141 globals = PyEval_GetGlobals();
4142 if (locals == Py_None) {
4143 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00004144 plain = 1;
4145 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004146 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004147 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004148 locals = globals;
Guido van Rossumb209a111997-04-29 18:18:01 +00004149 if (!PyString_Check(prog) &&
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004150 !PyUnicode_Check(prog) &&
Guido van Rossumb209a111997-04-29 18:18:01 +00004151 !PyCode_Check(prog) &&
4152 !PyFile_Check(prog)) {
4153 PyErr_SetString(PyExc_TypeError,
Guido van Rossumac7be682001-01-17 15:42:30 +00004154 "exec: arg 1 must be a string, file, or code object");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004155 return -1;
4156 }
Fred Drake661ea262000-10-24 19:57:45 +00004157 if (!PyDict_Check(globals)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004158 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00004159 "exec: arg 2 must be a dictionary or None");
4160 return -1;
4161 }
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004162 if (!PyMapping_Check(locals)) {
Fred Drake661ea262000-10-24 19:57:45 +00004163 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004164 "exec: arg 3 must be a mapping or None");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004165 return -1;
4166 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004167 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00004168 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00004169 if (PyCode_Check(prog)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +00004170 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4171 PyErr_SetString(PyExc_TypeError,
4172 "code object passed to exec may not contain free variables");
4173 return -1;
4174 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004175 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004176 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004177 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004178 FILE *fp = PyFile_AsFile(prog);
4179 char *name = PyString_AsString(PyFile_Name(prog));
Tim Peters5ba58662001-07-16 02:29:45 +00004180 PyCompilerFlags cf;
4181 cf.cf_flags = 0;
4182 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004183 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004184 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004185 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004186 v = PyRun_File(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004187 locals);
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004188 }
4189 else {
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004190 PyObject *tmp = NULL;
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004191 char *str;
Tim Peters5ba58662001-07-16 02:29:45 +00004192 PyCompilerFlags cf;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004193 cf.cf_flags = 0;
4194#ifdef Py_USING_UNICODE
4195 if (PyUnicode_Check(prog)) {
4196 tmp = PyUnicode_AsUTF8String(prog);
4197 if (tmp == NULL)
4198 return -1;
4199 prog = tmp;
4200 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4201 }
4202#endif
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004203 if (PyString_AsStringAndSize(prog, &str, NULL))
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004204 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004205 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters8a5c3c72004-04-05 19:36:21 +00004206 v = PyRun_StringFlags(str, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004207 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004208 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004209 v = PyRun_String(str, Py_file_input, globals, locals);
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004210 Py_XDECREF(tmp);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004211 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004212 if (plain)
4213 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004214 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004215 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00004216 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004217 return 0;
4218}
Guido van Rossum24c13741995-02-14 09:42:43 +00004219
Guido van Rossumac7be682001-01-17 15:42:30 +00004220static void
Paul Prescode68140d2000-08-30 20:25:01 +00004221format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4222{
4223 char *obj_str;
4224
4225 if (!obj)
4226 return;
4227
4228 obj_str = PyString_AsString(obj);
4229 if (!obj_str)
4230 return;
4231
4232 PyErr_Format(exc, format_str, obj_str);
4233}
Guido van Rossum950361c1997-01-24 13:49:28 +00004234
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004235static PyObject *
4236string_concatenate(PyObject *v, PyObject *w,
4237 PyFrameObject *f, unsigned char *next_instr)
4238{
4239 /* This function implements 'variable += expr' when both arguments
4240 are strings. */
4241
4242 if (v->ob_refcnt == 2) {
4243 /* In the common case, there are 2 references to the value
4244 * stored in 'variable' when the += is performed: one on the
4245 * value stack (in 'v') and one still stored in the 'variable'.
4246 * We try to delete the variable now to reduce the refcnt to 1.
4247 */
4248 switch (*next_instr) {
4249 case STORE_FAST:
4250 {
4251 int oparg = PEEKARG();
4252 PyObject **fastlocals = f->f_localsplus;
4253 if (GETLOCAL(oparg) == v)
4254 SETLOCAL(oparg, NULL);
4255 break;
4256 }
4257 case STORE_DEREF:
4258 {
4259 PyObject **freevars = f->f_localsplus + f->f_nlocals;
4260 PyObject *c = freevars[PEEKARG()];
4261 if (PyCell_GET(c) == v)
4262 PyCell_Set(c, NULL);
4263 break;
4264 }
4265 case STORE_NAME:
4266 {
4267 PyObject *names = f->f_code->co_names;
4268 PyObject *name = GETITEM(names, PEEKARG());
4269 PyObject *locals = f->f_locals;
4270 if (PyDict_CheckExact(locals) &&
4271 PyDict_GetItem(locals, name) == v) {
4272 if (PyDict_DelItem(locals, name) != 0) {
4273 PyErr_Clear();
4274 }
4275 }
4276 break;
4277 }
4278 }
4279 }
4280
Armin Rigo618fbf52004-08-07 20:58:32 +00004281 if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004282 /* Now we own the last reference to 'v', so we can resize it
4283 * in-place.
4284 */
4285 int v_len = PyString_GET_SIZE(v);
4286 int w_len = PyString_GET_SIZE(w);
4287 if (_PyString_Resize(&v, v_len + w_len) != 0) {
4288 /* XXX if _PyString_Resize() fails, 'v' has been
4289 * deallocated so it cannot be put back into 'variable'.
4290 * The MemoryError is raised when there is no value in
4291 * 'variable', which might (very remotely) be a cause
4292 * of incompatibilities.
4293 */
4294 return NULL;
4295 }
4296 /* copy 'w' into the newly allocated area of 'v' */
4297 memcpy(PyString_AS_STRING(v) + v_len,
4298 PyString_AS_STRING(w), w_len);
4299 return v;
4300 }
4301 else {
4302 /* When in-place resizing is not an option. */
4303 PyString_Concat(&v, w);
4304 return v;
4305 }
4306}
4307
Guido van Rossum950361c1997-01-24 13:49:28 +00004308#ifdef DYNAMIC_EXECUTION_PROFILE
4309
Skip Montanarof118cb12001-10-15 20:51:38 +00004310static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004311getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004312{
4313 int i;
4314 PyObject *l = PyList_New(256);
4315 if (l == NULL) return NULL;
4316 for (i = 0; i < 256; i++) {
4317 PyObject *x = PyInt_FromLong(a[i]);
4318 if (x == NULL) {
4319 Py_DECREF(l);
4320 return NULL;
4321 }
4322 PyList_SetItem(l, i, x);
4323 }
4324 for (i = 0; i < 256; i++)
4325 a[i] = 0;
4326 return l;
4327}
4328
4329PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004330_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004331{
4332#ifndef DXPAIRS
4333 return getarray(dxp);
4334#else
4335 int i;
4336 PyObject *l = PyList_New(257);
4337 if (l == NULL) return NULL;
4338 for (i = 0; i < 257; i++) {
4339 PyObject *x = getarray(dxpairs[i]);
4340 if (x == NULL) {
4341 Py_DECREF(l);
4342 return NULL;
4343 }
4344 PyList_SetItem(l, i, x);
4345 }
4346 return l;
4347#endif
4348}
4349
4350#endif