blob: ee3c7bbb98376105d036c6de1c85da82e535de67 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003
Guido van Rossum681d79a1995-07-18 14:51:37 +00004/* XXX TO DO:
Guido van Rossum681d79a1995-07-18 14:51:37 +00005 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +00006 XXX document it!
7 */
8
Guido van Rossumb209a111997-04-29 18:18:01 +00009#include "Python.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000010
Guido van Rossum10dc2e81990-11-18 17:27:39 +000011#include "compile.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000012#include "frameobject.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000013#include "eval.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000014#include "opcode.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000015#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000016
Guido van Rossumc6004111993-11-05 10:22:19 +000017#include <ctype.h>
18
Martin v. Löwis87fa7852004-08-29 15:51:52 +000019#ifndef WITH_TSC
Michael W. Hudson75eabd22005-01-18 15:56:11 +000020
21#define READ_TIMESTAMP(var)
22
23#else
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000024
25typedef unsigned long long uint64;
26
Michael W. Hudson800ba232004-08-12 18:19:17 +000027#if defined(__ppc__) /* <- Don't know if this is the correct symbol; this
28 section should work for GCC on any PowerPC platform,
29 irrespective of OS. POWER? Who knows :-) */
30
Michael W. Hudson75eabd22005-01-18 15:56:11 +000031#define READ_TIMESTAMP(var) ppc_getcounter(&var)
Michael W. Hudson800ba232004-08-12 18:19:17 +000032
33static void
34ppc_getcounter(uint64 *v)
35{
36 register unsigned long tbu, tb, tbu2;
37
38 loop:
39 asm volatile ("mftbu %0" : "=r" (tbu) );
40 asm volatile ("mftb %0" : "=r" (tb) );
41 asm volatile ("mftbu %0" : "=r" (tbu2));
42 if (__builtin_expect(tbu != tbu2, 0)) goto loop;
43
44 /* The slightly peculiar way of writing the next lines is
45 compiled better by GCC than any other way I tried. */
46 ((long*)(v))[0] = tbu;
47 ((long*)(v))[1] = tb;
48}
49
Michael W. Hudson75eabd22005-01-18 15:56:11 +000050#else /* this is for linux/x86 (and probably any other GCC/x86 combo) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000051
Michael W. Hudson75eabd22005-01-18 15:56:11 +000052#define READ_TIMESTAMP(val) \
53 __asm__ __volatile__("rdtsc" : "=A" (val))
Michael W. Hudson800ba232004-08-12 18:19:17 +000054
55#endif
56
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000057void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
58 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
59{
60 uint64 intr, inst, loop;
61 PyThreadState *tstate = PyThreadState_Get();
62 if (!tstate->interp->tscdump)
63 return;
64 intr = intr1 - intr0;
65 inst = inst1 - inst0 - intr;
66 loop = loop1 - loop0 - intr;
67 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
68 opcode, ticked, inst, loop);
69}
Michael W. Hudson800ba232004-08-12 18:19:17 +000070
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000071#endif
72
Guido van Rossum04691fc1992-08-12 15:35:34 +000073/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000074/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000075
Guido van Rossum408027e1996-12-30 16:17:54 +000076#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000077/* For debugging the interpreter: */
78#define LLTRACE 1 /* Low-level trace feature */
79#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000080#endif
81
Jeremy Hylton52820442001-01-03 23:52:36 +000082typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000083
Guido van Rossum374a9221991-04-04 10:40:29 +000084/* Forward declarations */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000085#ifdef WITH_TSC
86static PyObject *call_function(PyObject ***, int, uint64*, uint64*);
87#else
Jeremy Hyltone8c04322002-08-16 17:47:26 +000088static PyObject *call_function(PyObject ***, int);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000089#endif
Jeremy Hylton52820442001-01-03 23:52:36 +000090static PyObject *fast_function(PyObject *, PyObject ***, int, int, int);
Jeremy Hylton52820442001-01-03 23:52:36 +000091static PyObject *do_call(PyObject *, PyObject ***, int, int);
92static PyObject *ext_do_call(PyObject *, PyObject ***, int, int, int);
Guido van Rossumac7be682001-01-17 15:42:30 +000093static PyObject *update_keyword_args(PyObject *, int, PyObject ***,PyObject *);
Ka-Ping Yee20579702001-01-15 22:14:16 +000094static PyObject *update_star_args(int, int, PyObject *, PyObject ***);
Jeremy Hylton52820442001-01-03 23:52:36 +000095static PyObject *load_args(PyObject ***, int);
96#define CALL_FLAG_VAR 1
97#define CALL_FLAG_KW 2
98
Guido van Rossum0a066c01992-03-27 17:29:15 +000099#ifdef LLTRACE
Tim Petersdbd9ba62000-07-09 03:09:57 +0000100static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +0000101#endif
Fred Drake5755ce62001-06-27 19:19:46 +0000102static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
103 int, PyObject *);
Fred Drake4ec5d562001-10-04 19:26:43 +0000104static void call_trace_protected(Py_tracefunc, PyObject *,
105 PyFrameObject *, int);
Fred Drake5755ce62001-06-27 19:19:46 +0000106static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +0000107static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Armin Rigobf57a142004-03-22 19:24:58 +0000108 PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000109
Tim Petersdbd9ba62000-07-09 03:09:57 +0000110static PyObject *apply_slice(PyObject *, PyObject *, PyObject *);
111static int assign_slice(PyObject *, PyObject *,
112 PyObject *, PyObject *);
113static PyObject *cmp_outcome(int, PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +0000114static PyObject *import_from(PyObject *, PyObject *);
115static int import_all_from(PyObject *, PyObject *);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000116static PyObject *build_class(PyObject *, PyObject *, PyObject *);
117static int exec_statement(PyFrameObject *,
118 PyObject *, PyObject *, PyObject *);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000119static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *);
120static void reset_exc_info(PyThreadState *);
Paul Prescode68140d2000-08-30 20:25:01 +0000121static void format_exc_check_arg(PyObject *, char *, PyObject *);
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000122static PyObject *string_concatenate(PyObject *, PyObject *,
123 PyFrameObject *, unsigned char *);
Guido van Rossum374a9221991-04-04 10:40:29 +0000124
Paul Prescode68140d2000-08-30 20:25:01 +0000125#define NAME_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000126 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000127#define GLOBAL_NAME_ERROR_MSG \
128 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000129#define UNBOUNDLOCAL_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000130 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000131#define UNBOUNDFREE_ERROR_MSG \
132 "free variable '%.200s' referenced before assignment" \
133 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000134
Guido van Rossum950361c1997-01-24 13:49:28 +0000135/* Dynamic execution profile */
136#ifdef DYNAMIC_EXECUTION_PROFILE
137#ifdef DXPAIRS
138static long dxpairs[257][256];
139#define dxp dxpairs[256]
140#else
141static long dxp[256];
142#endif
143#endif
144
Jeremy Hylton985eba52003-02-05 23:13:00 +0000145/* Function call profile */
146#ifdef CALL_PROFILE
147#define PCALL_NUM 11
148static int pcall[PCALL_NUM];
149
150#define PCALL_ALL 0
151#define PCALL_FUNCTION 1
152#define PCALL_FAST_FUNCTION 2
153#define PCALL_FASTER_FUNCTION 3
154#define PCALL_METHOD 4
155#define PCALL_BOUND_METHOD 5
156#define PCALL_CFUNCTION 6
157#define PCALL_TYPE 7
158#define PCALL_GENERATOR 8
159#define PCALL_OTHER 9
160#define PCALL_POP 10
161
162/* Notes about the statistics
163
164 PCALL_FAST stats
165
166 FAST_FUNCTION means no argument tuple needs to be created.
167 FASTER_FUNCTION means that the fast-path frame setup code is used.
168
169 If there is a method call where the call can be optimized by changing
170 the argument tuple and calling the function directly, it gets recorded
171 twice.
172
173 As a result, the relationship among the statistics appears to be
174 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
175 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
176 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
177 PCALL_METHOD > PCALL_BOUND_METHOD
178*/
179
180#define PCALL(POS) pcall[POS]++
181
182PyObject *
183PyEval_GetCallStats(PyObject *self)
184{
Tim Peters8a5c3c72004-04-05 19:36:21 +0000185 return Py_BuildValue("iiiiiiiiii",
Jeremy Hylton985eba52003-02-05 23:13:00 +0000186 pcall[0], pcall[1], pcall[2], pcall[3],
187 pcall[4], pcall[5], pcall[6], pcall[7],
188 pcall[8], pcall[9]);
189}
190#else
191#define PCALL(O)
192
193PyObject *
194PyEval_GetCallStats(PyObject *self)
195{
196 Py_INCREF(Py_None);
197 return Py_None;
198}
199#endif
200
Tim Peters5ca576e2001-06-18 22:08:13 +0000201
Guido van Rossume59214e1994-08-30 08:01:59 +0000202#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000203
Guido van Rossum2571cc81999-04-07 16:07:23 +0000204#ifndef DONT_HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000205#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000206#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000207#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000208
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000209static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
Guido van Rossuma9672091994-09-14 13:31:22 +0000210static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000211
Tim Peters7f468f22004-10-11 02:40:51 +0000212int
213PyEval_ThreadsInitialized(void)
214{
215 return interpreter_lock != 0;
216}
217
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000218void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000219PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000220{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000221 if (interpreter_lock)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000222 return;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000223 interpreter_lock = PyThread_allocate_lock();
224 PyThread_acquire_lock(interpreter_lock, 1);
225 main_thread = PyThread_get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000226}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000227
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000228void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000229PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000230{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000231 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000232}
233
234void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000235PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000236{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000237 PyThread_release_lock(interpreter_lock);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000238}
239
240void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000241PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000242{
243 if (tstate == NULL)
244 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000245 /* Check someone has called PyEval_InitThreads() to create the lock */
246 assert(interpreter_lock);
Guido van Rossum65d5b571998-12-21 19:32:43 +0000247 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000248 if (PyThreadState_Swap(tstate) != NULL)
249 Py_FatalError(
250 "PyEval_AcquireThread: non-NULL old thread state");
251}
252
253void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000254PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000255{
256 if (tstate == NULL)
257 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
258 if (PyThreadState_Swap(NULL) != tstate)
259 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000260 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000261}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000262
263/* This function is called from PyOS_AfterFork to ensure that newly
264 created child processes don't hold locks referring to threads which
265 are not running in the child process. (This could also be done using
266 pthread_atfork mechanism, at least for the pthreads implementation.) */
267
268void
269PyEval_ReInitThreads(void)
270{
271 if (!interpreter_lock)
272 return;
273 /*XXX Can't use PyThread_free_lock here because it does too
274 much error-checking. Doing this cleanly would require
275 adding a new function to each thread_*.h. Instead, just
276 create a new lock and waste a little bit of memory */
277 interpreter_lock = PyThread_allocate_lock();
278 PyThread_acquire_lock(interpreter_lock, 1);
279 main_thread = PyThread_get_thread_ident();
280}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000281#endif
282
Guido van Rossumff4949e1992-08-05 19:58:53 +0000283/* Functions save_thread and restore_thread are always defined so
284 dynamically loaded modules needn't be compiled separately for use
285 with and without threads: */
286
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000287PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000288PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000289{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000290 PyThreadState *tstate = PyThreadState_Swap(NULL);
291 if (tstate == NULL)
292 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000293#ifdef WITH_THREAD
Guido van Rossumb74eca91997-09-30 22:03:16 +0000294 if (interpreter_lock)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000295 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000296#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000297 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000298}
299
300void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000301PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000302{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000303 if (tstate == NULL)
304 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000305#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000306 if (interpreter_lock) {
Guido van Rossumb74eca91997-09-30 22:03:16 +0000307 int err = errno;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000308 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000309 errno = err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000310 }
311#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000312 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000313}
314
315
Guido van Rossuma9672091994-09-14 13:31:22 +0000316/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
317 signal handlers or Mac I/O completion routines) can schedule calls
318 to a function to be called synchronously.
319 The synchronous function is called with one void* argument.
320 It should return 0 for success or -1 for failure -- failure should
321 be accompanied by an exception.
322
323 If registry succeeds, the registry function returns 0; if it fails
324 (e.g. due to too many pending calls) it returns -1 (without setting
325 an exception condition).
326
327 Note that because registry may occur from within signal handlers,
328 or other asynchronous events, calling malloc() is unsafe!
329
330#ifdef WITH_THREAD
331 Any thread can schedule pending calls, but only the main thread
332 will execute them.
333#endif
334
335 XXX WARNING! ASYNCHRONOUSLY EXECUTING CODE!
336 There are two possible race conditions:
337 (1) nested asynchronous registry calls;
338 (2) registry calls made while pending calls are being processed.
339 While (1) is very unlikely, (2) is a real possibility.
340 The current code is safe against (2), but not against (1).
341 The safety against (2) is derived from the fact that only one
342 thread (the main thread) ever takes things out of the queue.
Guido van Rossuma9672091994-09-14 13:31:22 +0000343
Guido van Rossuma027efa1997-05-05 20:56:21 +0000344 XXX Darn! With the advent of thread state, we should have an array
345 of pending calls per thread in the thread state! Later...
346*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000347
Guido van Rossuma9672091994-09-14 13:31:22 +0000348#define NPENDINGCALLS 32
349static struct {
Thomas Wouters334fb892000-07-25 12:56:38 +0000350 int (*func)(void *);
351 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000352} pendingcalls[NPENDINGCALLS];
353static volatile int pendingfirst = 0;
354static volatile int pendinglast = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000355static volatile int things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000356
357int
Thomas Wouters334fb892000-07-25 12:56:38 +0000358Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000359{
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000360 static volatile int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000361 int i, j;
362 /* XXX Begin critical section */
363 /* XXX If you want this to be safe against nested
364 XXX asynchronous calls, you'll have to work harder! */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000365 if (busy)
366 return -1;
367 busy = 1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000368 i = pendinglast;
369 j = (i + 1) % NPENDINGCALLS;
Guido van Rossum04e70322002-07-17 16:57:13 +0000370 if (j == pendingfirst) {
371 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000372 return -1; /* Queue full */
Guido van Rossum04e70322002-07-17 16:57:13 +0000373 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000374 pendingcalls[i].func = func;
375 pendingcalls[i].arg = arg;
376 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000377
378 _Py_Ticker = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000379 things_to_do = 1; /* Signal main loop */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000380 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000381 /* XXX End critical section */
382 return 0;
383}
384
Guido van Rossum180d7b41994-09-29 09:45:57 +0000385int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000386Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000387{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000388 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000389#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000390 if (main_thread && PyThread_get_thread_ident() != main_thread)
Guido van Rossuma9672091994-09-14 13:31:22 +0000391 return 0;
392#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000393 if (busy)
Guido van Rossum180d7b41994-09-29 09:45:57 +0000394 return 0;
395 busy = 1;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000396 things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000397 for (;;) {
398 int i;
Thomas Wouters334fb892000-07-25 12:56:38 +0000399 int (*func)(void *);
400 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000401 i = pendingfirst;
402 if (i == pendinglast)
403 break; /* Queue empty */
404 func = pendingcalls[i].func;
405 arg = pendingcalls[i].arg;
406 pendingfirst = (i + 1) % NPENDINGCALLS;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000407 if (func(arg) < 0) {
408 busy = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000409 things_to_do = 1; /* We're not done yet */
Guido van Rossuma9672091994-09-14 13:31:22 +0000410 return -1;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000411 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000412 }
Guido van Rossum180d7b41994-09-29 09:45:57 +0000413 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000414 return 0;
415}
416
417
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000418/* The interpreter's recursion limit */
419
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000420#ifndef Py_DEFAULT_RECURSION_LIMIT
421#define Py_DEFAULT_RECURSION_LIMIT 1000
422#endif
423static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
424int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000425
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000426int
427Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000428{
429 return recursion_limit;
430}
431
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000432void
433Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000434{
435 recursion_limit = new_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000436 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000437}
438
Armin Rigo2b3eb402003-10-28 12:05:48 +0000439/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
440 if the recursion_depth reaches _Py_CheckRecursionLimit.
441 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
442 to guarantee that _Py_CheckRecursiveCall() is regularly called.
443 Without USE_STACKCHECK, there is no need for this. */
444int
445_Py_CheckRecursiveCall(char *where)
446{
447 PyThreadState *tstate = PyThreadState_GET();
448
449#ifdef USE_STACKCHECK
450 if (PyOS_CheckStack()) {
451 --tstate->recursion_depth;
452 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
453 return -1;
454 }
455#endif
456 if (tstate->recursion_depth > recursion_limit) {
457 --tstate->recursion_depth;
458 PyErr_Format(PyExc_RuntimeError,
459 "maximum recursion depth exceeded%s",
460 where);
461 return -1;
462 }
463 _Py_CheckRecursionLimit = recursion_limit;
464 return 0;
465}
466
Guido van Rossum374a9221991-04-04 10:40:29 +0000467/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000468enum why_code {
469 WHY_NOT = 0x0001, /* No error */
470 WHY_EXCEPTION = 0x0002, /* Exception occurred */
471 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
472 WHY_RETURN = 0x0008, /* 'return' statement */
473 WHY_BREAK = 0x0010, /* 'break' statement */
474 WHY_CONTINUE = 0x0020, /* 'continue' statement */
475 WHY_YIELD = 0x0040 /* 'yield' operator */
476};
Guido van Rossum374a9221991-04-04 10:40:29 +0000477
Raymond Hettinger7c958652004-04-06 10:11:10 +0000478static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
Tim Petersd6d010b2001-06-21 02:49:55 +0000479static int unpack_iterable(PyObject *, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000480
Skip Montanarod581d772002-09-03 20:10:45 +0000481/* for manipulating the thread switch and periodic "stuff" - used to be
482 per thread, now just a pair o' globals */
Skip Montanaro99dba272002-09-03 20:19:06 +0000483int _Py_CheckInterval = 100;
484volatile int _Py_Ticker = 100;
Guido van Rossum374a9221991-04-04 10:40:29 +0000485
Guido van Rossumb209a111997-04-29 18:18:01 +0000486PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000487PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000488{
Jeremy Hylton985eba52003-02-05 23:13:00 +0000489 /* XXX raise SystemError if globals is NULL */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000490 return PyEval_EvalCodeEx(co,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000491 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000492 (PyObject **)NULL, 0,
493 (PyObject **)NULL, 0,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000494 (PyObject **)NULL, 0,
495 NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000496}
497
498
499/* Interpreter main loop */
500
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000501PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000502PyEval_EvalFrame(PyFrameObject *f) {
503 /* This is for backward compatibility with extension modules that
504 used this API; core interpreter code should call PyEval_EvalFrameEx() */
505 return PyEval_EvalFrameEx(f, 0);
506}
507
508PyObject *
509PyEval_EvalFrameEx(PyFrameObject *f, int throw)
Guido van Rossum374a9221991-04-04 10:40:29 +0000510{
Guido van Rossum950361c1997-01-24 13:49:28 +0000511#ifdef DXPAIRS
512 int lastopcode = 0;
513#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +0000514 register PyObject **stack_pointer; /* Next free slot in value stack */
Guido van Rossum374a9221991-04-04 10:40:29 +0000515 register unsigned char *next_instr;
Armin Rigo8817fcd2004-06-17 10:22:40 +0000516 register int opcode; /* Current opcode */
517 register int oparg; /* Current opcode argument, if any */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000518 register enum why_code why; /* Reason for block stack unwind */
Guido van Rossum374a9221991-04-04 10:40:29 +0000519 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000520 register PyObject *x; /* Result object -- NULL if error */
521 register PyObject *v; /* Temporary objects popped off stack */
522 register PyObject *w;
523 register PyObject *u;
524 register PyObject *t;
Barry Warsaw23c9ec82000-08-21 15:44:01 +0000525 register PyObject *stream = NULL; /* for PRINT opcodes */
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000526 register PyObject **fastlocals, **freevars;
Guido van Rossum014518f1998-11-23 21:09:51 +0000527 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000528 PyThreadState *tstate = PyThreadState_GET();
Tim Peters5ca576e2001-06-18 22:08:13 +0000529 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000530
Tim Peters8a5c3c72004-04-05 19:36:21 +0000531 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000532
533 not (instr_lb <= current_bytecode_offset < instr_ub)
534
Tim Peters8a5c3c72004-04-05 19:36:21 +0000535 is true when the line being executed has changed. The
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000536 initial values are such as to make this false the first
537 time it is tested. */
Armin Rigobf57a142004-03-22 19:24:58 +0000538 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000539
Guido van Rossumd076c731998-10-07 19:42:25 +0000540 unsigned char *first_instr;
Skip Montanaro04d80f82002-08-04 21:03:35 +0000541 PyObject *names;
542 PyObject *consts;
Guido van Rossum96a42c81992-01-12 02:29:51 +0000543#ifdef LLTRACE
Guido van Rossumacbe8da1993-04-15 15:33:52 +0000544 int lltrace;
Guido van Rossum374a9221991-04-04 10:40:29 +0000545#endif
Guido van Rossum408027e1996-12-30 16:17:54 +0000546#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000547 /* Make it easier to find out where we are with a debugger */
Tim Peters5ca576e2001-06-18 22:08:13 +0000548 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000549#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000550
Neal Norwitza81d2202002-07-14 00:27:26 +0000551/* Tuple access macros */
552
553#ifndef Py_DEBUG
554#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
555#else
556#define GETITEM(v, i) PyTuple_GetItem((v), (i))
557#endif
558
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000559#ifdef WITH_TSC
560/* Use Pentium timestamp counter to mark certain events:
561 inst0 -- beginning of switch statement for opcode dispatch
562 inst1 -- end of switch statement (may be skipped)
563 loop0 -- the top of the mainloop
564 loop1 -- place where control returns again to top of mainloop
565 (may be skipped)
566 intr1 -- beginning of long interruption
567 intr2 -- end of long interruption
568
569 Many opcodes call out to helper C functions. In some cases, the
570 time in those functions should be counted towards the time for the
571 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
572 calls another Python function; there's no point in charge all the
573 bytecode executed by the called function to the caller.
574
575 It's hard to make a useful judgement statically. In the presence
576 of operator overloading, it's impossible to tell if a call will
577 execute new Python code or not.
578
579 It's a case-by-case judgement. I'll use intr1 for the following
580 cases:
581
582 EXEC_STMT
583 IMPORT_STAR
584 IMPORT_FROM
585 CALL_FUNCTION (and friends)
586
587 */
588 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
589 int ticked = 0;
590
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000591 READ_TIMESTAMP(inst0);
592 READ_TIMESTAMP(inst1);
593 READ_TIMESTAMP(loop0);
594 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000595
596 /* shut up the compiler */
597 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000598#endif
599
Guido van Rossum374a9221991-04-04 10:40:29 +0000600/* Code access macros */
601
Guido van Rossumd076c731998-10-07 19:42:25 +0000602#define INSTR_OFFSET() (next_instr - first_instr)
Guido van Rossum374a9221991-04-04 10:40:29 +0000603#define NEXTOP() (*next_instr++)
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000604#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000605#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
Guido van Rossumd076c731998-10-07 19:42:25 +0000606#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000607#define JUMPBY(x) (next_instr += (x))
608
Raymond Hettingerf606f872003-03-16 03:11:04 +0000609/* OpCode prediction macros
610 Some opcodes tend to come in pairs thus making it possible to predict
611 the second code when the first is run. For example, COMPARE_OP is often
612 followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And, those opcodes are often
613 followed by a POP_TOP.
614
615 Verifying the prediction costs a single high-speed test of register
Raymond Hettingerac2072922003-03-16 15:41:11 +0000616 variable against a constant. If the pairing was good, then the
Raymond Hettingerf606f872003-03-16 03:11:04 +0000617 processor has a high likelihood of making its own successful branch
618 prediction which results in a nearly zero overhead transition to the
619 next opcode.
620
621 A successful prediction saves a trip through the eval-loop including
622 its two unpredictable branches, the HASARG test and the switch-case.
Raymond Hettingera7216982004-02-08 19:59:27 +0000623
Tim Peters8a5c3c72004-04-05 19:36:21 +0000624 If collecting opcode statistics, turn off prediction so that
625 statistics are accurately maintained (the predictions bypass
Raymond Hettingera7216982004-02-08 19:59:27 +0000626 the opcode frequency counter updates).
Raymond Hettingerf606f872003-03-16 03:11:04 +0000627*/
628
Raymond Hettingera7216982004-02-08 19:59:27 +0000629#ifdef DYNAMIC_EXECUTION_PROFILE
630#define PREDICT(op) if (0) goto PRED_##op
631#else
Raymond Hettingerac2072922003-03-16 15:41:11 +0000632#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000633#endif
634
Raymond Hettingerf606f872003-03-16 03:11:04 +0000635#define PREDICTED(op) PRED_##op: next_instr++
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000636#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Raymond Hettingerf606f872003-03-16 03:11:04 +0000637
Guido van Rossum374a9221991-04-04 10:40:29 +0000638/* Stack manipulation macros */
639
640#define STACK_LEVEL() (stack_pointer - f->f_valuestack)
641#define EMPTY() (STACK_LEVEL() == 0)
642#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000643#define SECOND() (stack_pointer[-2])
644#define THIRD() (stack_pointer[-3])
645#define FOURTH() (stack_pointer[-4])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000646#define SET_TOP(v) (stack_pointer[-1] = (v))
647#define SET_SECOND(v) (stack_pointer[-2] = (v))
648#define SET_THIRD(v) (stack_pointer[-3] = (v))
649#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000650#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000651#define BASIC_PUSH(v) (*stack_pointer++ = (v))
652#define BASIC_POP() (*--stack_pointer)
653
Guido van Rossum96a42c81992-01-12 02:29:51 +0000654#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000655#define PUSH(v) { (void)(BASIC_PUSH(v), \
656 lltrace && prtrace(TOP(), "push")); \
657 assert(STACK_LEVEL() <= f->f_stacksize); }
Fred Drakede26cfc2001-10-13 06:11:28 +0000658#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000659#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
660 lltrace && prtrace(TOP(), "stackadj")); \
661 assert(STACK_LEVEL() <= f->f_stacksize); }
Guido van Rossum374a9221991-04-04 10:40:29 +0000662#else
663#define PUSH(v) BASIC_PUSH(v)
664#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000665#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000666#endif
667
Guido van Rossum681d79a1995-07-18 14:51:37 +0000668/* Local variable macros */
669
670#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000671
672/* The SETLOCAL() macro must not DECREF the local variable in-place and
673 then store the new value; it must copy the old value to a temporary
674 value, then store the new value, and then DECREF the temporary value.
675 This is because it is possible that during the DECREF the frame is
676 accessed by other code (e.g. a __del__ method or gc.collect()) and the
677 variable would be pointing to already-freed memory. */
678#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
679 GETLOCAL(i) = value; \
680 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000681
Guido van Rossuma027efa1997-05-05 20:56:21 +0000682/* Start of code */
683
Tim Peters5ca576e2001-06-18 22:08:13 +0000684 if (f == NULL)
685 return NULL;
686
Armin Rigo1d313ab2003-10-25 14:33:09 +0000687 /* push frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000688 if (Py_EnterRecursiveCall(""))
Armin Rigo1d313ab2003-10-25 14:33:09 +0000689 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000690
Tim Peters5ca576e2001-06-18 22:08:13 +0000691 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000692
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000693 if (tstate->use_tracing) {
694 if (tstate->c_tracefunc != NULL) {
695 /* tstate->c_tracefunc, if defined, is a
696 function that will be called on *every* entry
697 to a code block. Its return value, if not
698 None, is a function that will be called at
699 the start of each executed line of code.
700 (Actually, the function must return itself
701 in order to continue tracing.) The trace
702 functions are called with three arguments:
703 a pointer to the current frame, a string
704 indicating why the function is called, and
705 an argument which depends on the situation.
706 The global trace function is also called
707 whenever an exception is detected. */
708 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
709 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000710 /* Trace function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000711 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000712 }
713 }
714 if (tstate->c_profilefunc != NULL) {
715 /* Similar for c_profilefunc, except it needn't
716 return itself and isn't called for "line" events */
717 if (call_trace(tstate->c_profilefunc,
718 tstate->c_profileobj,
719 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000720 /* Profile function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000721 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000722 }
723 }
724 }
725
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000726 co = f->f_code;
727 names = co->co_names;
728 consts = co->co_consts;
729 fastlocals = f->f_localsplus;
730 freevars = f->f_localsplus + f->f_nlocals;
Brett Cannonc9371d42005-06-25 08:23:41 +0000731 first_instr = (unsigned char*) PyString_AS_STRING(co->co_code);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000732 /* An explanation is in order for the next line.
733
734 f->f_lasti now refers to the index of the last instruction
735 executed. You might think this was obvious from the name, but
736 this wasn't always true before 2.3! PyFrame_New now sets
737 f->f_lasti to -1 (i.e. the index *before* the first instruction)
738 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
739 does work. Promise. */
740 next_instr = first_instr + f->f_lasti + 1;
741 stack_pointer = f->f_stacktop;
742 assert(stack_pointer != NULL);
743 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
744
Tim Peters5ca576e2001-06-18 22:08:13 +0000745#ifdef LLTRACE
746 lltrace = PyDict_GetItemString(f->f_globals,"__lltrace__") != NULL;
747#endif
748#if defined(Py_DEBUG) || defined(LLTRACE)
749 filename = PyString_AsString(co->co_filename);
750#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000751
Guido van Rossum374a9221991-04-04 10:40:29 +0000752 why = WHY_NOT;
753 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +0000754 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +0000755 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +0000756
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000757 if (throw) { /* support for generator.throw() */
758 why = WHY_EXCEPTION;
759 goto on_error;
760 }
761
Guido van Rossum374a9221991-04-04 10:40:29 +0000762 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000763#ifdef WITH_TSC
764 if (inst1 == 0) {
765 /* Almost surely, the opcode executed a break
766 or a continue, preventing inst1 from being set
767 on the way out of the loop.
768 */
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000769 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000770 loop1 = inst1;
771 }
772 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
773 intr0, intr1);
774 ticked = 0;
775 inst1 = 0;
776 intr0 = 0;
777 intr1 = 0;
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000778 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000779#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000780 assert(stack_pointer >= f->f_valuestack); /* else underflow */
781 assert(STACK_LEVEL() <= f->f_stacksize); /* else overflow */
782
Guido van Rossuma027efa1997-05-05 20:56:21 +0000783 /* Do periodic things. Doing this every time through
784 the loop would add too much overhead, so we do it
785 only every Nth instruction. We also do it if
786 ``things_to_do'' is set, i.e. when an asynchronous
787 event needs attention (e.g. a signal handler or
788 async I/O handler); see Py_AddPendingCall() and
789 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000790
Skip Montanarod581d772002-09-03 20:10:45 +0000791 if (--_Py_Ticker < 0) {
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000792 if (*next_instr == SETUP_FINALLY) {
793 /* Make the last opcode before
794 a try: finally: block uninterruptable. */
795 goto fast_next_opcode;
796 }
Skip Montanarod581d772002-09-03 20:10:45 +0000797 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000798 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000799#ifdef WITH_TSC
800 ticked = 1;
801#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000802 if (things_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +0000803 if (Py_MakePendingCalls() < 0) {
804 why = WHY_EXCEPTION;
805 goto on_error;
806 }
Kurt B. Kaiser4c79a832004-11-23 18:06:08 +0000807 if (things_to_do)
808 /* MakePendingCalls() didn't succeed.
809 Force early re-execution of this
810 "periodic" code, possibly after
811 a thread switch */
812 _Py_Ticker = 0;
Guido van Rossum8861b741996-07-30 16:49:37 +0000813 }
Guido van Rossume59214e1994-08-30 08:01:59 +0000814#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000815 if (interpreter_lock) {
816 /* Give another thread a chance */
817
Guido van Rossum25ce5661997-08-02 03:10:38 +0000818 if (PyThreadState_Swap(NULL) != tstate)
819 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000820 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000821
822 /* Other threads may run now */
823
Guido van Rossum65d5b571998-12-21 19:32:43 +0000824 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000825 if (PyThreadState_Swap(tstate) != NULL)
826 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000827
828 /* Check for thread interrupts */
829
830 if (tstate->async_exc != NULL) {
831 x = tstate->async_exc;
832 tstate->async_exc = NULL;
833 PyErr_SetNone(x);
834 Py_DECREF(x);
835 why = WHY_EXCEPTION;
836 goto on_error;
837 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000838 }
839#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000840 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000841
Neil Schemenauer63543862002-02-17 19:10:14 +0000842 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +0000843 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +0000844
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000845 /* line-by-line tracing support */
846
847 if (tstate->c_tracefunc != NULL && !tstate->tracing) {
848 /* see maybe_call_line_trace
849 for expository comments */
850 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +0000851
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000852 err = maybe_call_line_trace(tstate->c_tracefunc,
853 tstate->c_traceobj,
Armin Rigobf57a142004-03-22 19:24:58 +0000854 f, &instr_lb, &instr_ub,
855 &instr_prev);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000856 /* Reload possibly changed frame fields */
857 JUMPTO(f->f_lasti);
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000858 if (f->f_stacktop != NULL) {
859 stack_pointer = f->f_stacktop;
860 f->f_stacktop = NULL;
861 }
862 if (err) {
863 /* trace function raised an exception */
864 goto on_error;
865 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000866 }
867
868 /* Extract opcode and argument */
869
Guido van Rossum374a9221991-04-04 10:40:29 +0000870 opcode = NEXTOP();
Armin Rigo8817fcd2004-06-17 10:22:40 +0000871 oparg = 0; /* allows oparg to be stored in a register because
872 it doesn't have to be remembered across a full loop */
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000873 if (HAS_ARG(opcode))
874 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +0000875 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +0000876#ifdef DYNAMIC_EXECUTION_PROFILE
877#ifdef DXPAIRS
878 dxpairs[lastopcode][opcode]++;
879 lastopcode = opcode;
880#endif
881 dxp[opcode]++;
882#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000883
Guido van Rossum96a42c81992-01-12 02:29:51 +0000884#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +0000885 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +0000886
Guido van Rossum96a42c81992-01-12 02:29:51 +0000887 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000888 if (HAS_ARG(opcode)) {
889 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000890 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +0000891 }
892 else {
893 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000894 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +0000895 }
896 }
897#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000898
Guido van Rossum374a9221991-04-04 10:40:29 +0000899 /* Main switch on opcode */
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000900 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +0000901
Guido van Rossum374a9221991-04-04 10:40:29 +0000902 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +0000903
Guido van Rossum374a9221991-04-04 10:40:29 +0000904 /* BEWARE!
905 It is essential that any operation that fails sets either
906 x to NULL, err to nonzero, or why to anything but WHY_NOT,
907 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000908
Guido van Rossum374a9221991-04-04 10:40:29 +0000909 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000910
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000911 case NOP:
912 goto fast_next_opcode;
913
Neil Schemenauer63543862002-02-17 19:10:14 +0000914 case LOAD_FAST:
915 x = GETLOCAL(oparg);
916 if (x != NULL) {
917 Py_INCREF(x);
918 PUSH(x);
919 goto fast_next_opcode;
920 }
921 format_exc_check_arg(PyExc_UnboundLocalError,
922 UNBOUNDLOCAL_ERROR_MSG,
923 PyTuple_GetItem(co->co_varnames, oparg));
924 break;
925
926 case LOAD_CONST:
Skip Montanaro04d80f82002-08-04 21:03:35 +0000927 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +0000928 Py_INCREF(x);
929 PUSH(x);
930 goto fast_next_opcode;
931
Raymond Hettinger7dc52212003-03-16 20:14:44 +0000932 PREDICTED_WITH_ARG(STORE_FAST);
Neil Schemenauer63543862002-02-17 19:10:14 +0000933 case STORE_FAST:
934 v = POP();
935 SETLOCAL(oparg, v);
936 goto fast_next_opcode;
937
Raymond Hettingerf606f872003-03-16 03:11:04 +0000938 PREDICTED(POP_TOP);
Guido van Rossum374a9221991-04-04 10:40:29 +0000939 case POP_TOP:
940 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000941 Py_DECREF(v);
Neil Schemenauer63543862002-02-17 19:10:14 +0000942 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000943
Guido van Rossum374a9221991-04-04 10:40:29 +0000944 case ROT_TWO:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000945 v = TOP();
946 w = SECOND();
947 SET_TOP(w);
948 SET_SECOND(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000949 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000950
Guido van Rossum374a9221991-04-04 10:40:29 +0000951 case ROT_THREE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000952 v = TOP();
953 w = SECOND();
954 x = THIRD();
955 SET_TOP(w);
956 SET_SECOND(x);
957 SET_THIRD(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000958 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000959
Thomas Wouters434d0822000-08-24 20:11:32 +0000960 case ROT_FOUR:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000961 u = TOP();
962 v = SECOND();
963 w = THIRD();
964 x = FOURTH();
965 SET_TOP(v);
966 SET_SECOND(w);
967 SET_THIRD(x);
968 SET_FOURTH(u);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000969 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000970
Guido van Rossum374a9221991-04-04 10:40:29 +0000971 case DUP_TOP:
972 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000973 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000974 PUSH(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000975 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000976
Thomas Wouters434d0822000-08-24 20:11:32 +0000977 case DUP_TOPX:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000978 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000979 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000980 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000981 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000982 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000983 STACKADJ(2);
984 SET_TOP(x);
985 SET_SECOND(w);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000986 goto fast_next_opcode;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000987 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000988 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000989 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000990 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000991 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000992 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +0000993 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000994 STACKADJ(3);
995 SET_TOP(x);
996 SET_SECOND(w);
997 SET_THIRD(v);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000998 goto fast_next_opcode;
Thomas Wouters434d0822000-08-24 20:11:32 +0000999 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001000 Py_FatalError("invalid argument to DUP_TOPX"
1001 " (bytecode corruption?)");
Tim Peters35ba6892000-10-11 07:04:49 +00001002 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001003
Guido van Rossum374a9221991-04-04 10:40:29 +00001004 case UNARY_POSITIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001005 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001006 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001007 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001008 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001009 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001010 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001011
Guido van Rossum374a9221991-04-04 10:40:29 +00001012 case UNARY_NEGATIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001013 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001014 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001015 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001016 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001017 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001018 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001019
Guido van Rossum374a9221991-04-04 10:40:29 +00001020 case UNARY_NOT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001021 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001022 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001023 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +00001024 if (err == 0) {
1025 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001026 SET_TOP(Py_True);
Guido van Rossumfc490731997-05-06 15:06:49 +00001027 continue;
1028 }
1029 else if (err > 0) {
1030 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001031 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +00001032 err = 0;
1033 continue;
1034 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00001035 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001036 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001037
Guido van Rossum374a9221991-04-04 10:40:29 +00001038 case UNARY_CONVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001039 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001040 x = PyObject_Repr(v);
1041 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001042 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001043 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001044 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001045
Guido van Rossum7928cd71991-10-24 14:59:31 +00001046 case UNARY_INVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001047 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001048 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001049 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001050 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001051 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001052 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001053
Guido van Rossum50564e81996-01-12 01:13:16 +00001054 case BINARY_POWER:
1055 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001056 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001057 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +00001058 Py_DECREF(v);
1059 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001060 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001061 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +00001062 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001063
Guido van Rossum374a9221991-04-04 10:40:29 +00001064 case BINARY_MULTIPLY:
1065 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001066 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001067 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001068 Py_DECREF(v);
1069 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001070 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001071 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001072 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001073
Guido van Rossum374a9221991-04-04 10:40:29 +00001074 case BINARY_DIVIDE:
Tim Peters3caca232001-12-06 06:23:26 +00001075 if (!_Py_QnewFlag) {
1076 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001077 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001078 x = PyNumber_Divide(v, w);
1079 Py_DECREF(v);
1080 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001081 SET_TOP(x);
Tim Peters3caca232001-12-06 06:23:26 +00001082 if (x != NULL) continue;
1083 break;
1084 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001085 /* -Qnew is in effect: fall through to
Tim Peters3caca232001-12-06 06:23:26 +00001086 BINARY_TRUE_DIVIDE */
1087 case BINARY_TRUE_DIVIDE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001088 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001089 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001090 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001091 Py_DECREF(v);
1092 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001093 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001094 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001095 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001096
Guido van Rossum4668b002001-08-08 05:00:18 +00001097 case BINARY_FLOOR_DIVIDE:
1098 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001099 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001100 x = PyNumber_FloorDivide(v, w);
1101 Py_DECREF(v);
1102 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001103 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001104 if (x != NULL) continue;
1105 break;
1106
Guido van Rossum374a9221991-04-04 10:40:29 +00001107 case BINARY_MODULO:
1108 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001109 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001110 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001111 Py_DECREF(v);
1112 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001113 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001114 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001115 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001116
Guido van Rossum374a9221991-04-04 10:40:29 +00001117 case BINARY_ADD:
1118 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001119 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001120 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001121 /* INLINE: int + int */
1122 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001123 a = PyInt_AS_LONG(v);
1124 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001125 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001126 if ((i^a) < 0 && (i^b) < 0)
1127 goto slow_add;
1128 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001129 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001130 else if (PyString_CheckExact(v) &&
1131 PyString_CheckExact(w)) {
1132 x = string_concatenate(v, w, f, next_instr);
1133 /* string_concatenate consumed the ref to v */
1134 goto skip_decref_vx;
1135 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001136 else {
1137 slow_add:
Guido van Rossumc12da691997-07-17 23:12:42 +00001138 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001139 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001140 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001141 skip_decref_vx:
Guido van Rossumb209a111997-04-29 18:18:01 +00001142 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001143 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001144 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001145 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001146
Guido van Rossum374a9221991-04-04 10:40:29 +00001147 case BINARY_SUBTRACT:
1148 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001149 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001150 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001151 /* INLINE: int - int */
1152 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001153 a = PyInt_AS_LONG(v);
1154 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001155 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001156 if ((i^a) < 0 && (i^~b) < 0)
1157 goto slow_sub;
1158 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001159 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001160 else {
1161 slow_sub:
Guido van Rossumc12da691997-07-17 23:12:42 +00001162 x = PyNumber_Subtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001163 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001164 Py_DECREF(v);
1165 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001166 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001167 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001168 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001169
Guido van Rossum374a9221991-04-04 10:40:29 +00001170 case BINARY_SUBSCR:
1171 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001172 v = TOP();
Tim Petersb1c46982001-10-05 20:41:38 +00001173 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001174 /* INLINE: list[int] */
1175 long i = PyInt_AsLong(w);
1176 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +00001177 i += PyList_GET_SIZE(v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001178 if (i >= 0 && i < PyList_GET_SIZE(v)) {
Guido van Rossumfa00e951998-07-08 15:02:37 +00001179 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001180 Py_INCREF(x);
1181 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001182 else
1183 goto slow_get;
Guido van Rossumc12da691997-07-17 23:12:42 +00001184 }
1185 else
Raymond Hettinger467a6982004-04-07 11:39:21 +00001186 slow_get:
Guido van Rossumc12da691997-07-17 23:12:42 +00001187 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001188 Py_DECREF(v);
1189 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001190 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001191 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001192 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001193
Guido van Rossum7928cd71991-10-24 14:59:31 +00001194 case BINARY_LSHIFT:
1195 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001196 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001197 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001198 Py_DECREF(v);
1199 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001200 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001201 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001202 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001203
Guido van Rossum7928cd71991-10-24 14:59:31 +00001204 case BINARY_RSHIFT:
1205 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001206 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001207 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001208 Py_DECREF(v);
1209 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001210 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001211 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001212 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001213
Guido van Rossum7928cd71991-10-24 14:59:31 +00001214 case BINARY_AND:
1215 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001216 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001217 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001218 Py_DECREF(v);
1219 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001220 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001221 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001222 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001223
Guido van Rossum7928cd71991-10-24 14:59:31 +00001224 case BINARY_XOR:
1225 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001226 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001227 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001228 Py_DECREF(v);
1229 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001230 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001231 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001232 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001233
Guido van Rossum7928cd71991-10-24 14:59:31 +00001234 case BINARY_OR:
1235 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001236 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001237 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001238 Py_DECREF(v);
1239 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001240 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001241 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001242 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001243
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001244 case LIST_APPEND:
1245 w = POP();
1246 v = POP();
1247 err = PyList_Append(v, w);
1248 Py_DECREF(v);
1249 Py_DECREF(w);
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001250 if (err == 0) {
1251 PREDICT(JUMP_ABSOLUTE);
1252 continue;
1253 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001254 break;
1255
Thomas Wouters434d0822000-08-24 20:11:32 +00001256 case INPLACE_POWER:
1257 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001258 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001259 x = PyNumber_InPlacePower(v, w, Py_None);
1260 Py_DECREF(v);
1261 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001262 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001263 if (x != NULL) continue;
1264 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001265
Thomas Wouters434d0822000-08-24 20:11:32 +00001266 case INPLACE_MULTIPLY:
1267 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001268 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001269 x = PyNumber_InPlaceMultiply(v, w);
1270 Py_DECREF(v);
1271 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001272 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001273 if (x != NULL) continue;
1274 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001275
Thomas Wouters434d0822000-08-24 20:11:32 +00001276 case INPLACE_DIVIDE:
Tim Peters54b11912001-12-25 18:49:11 +00001277 if (!_Py_QnewFlag) {
1278 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001279 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001280 x = PyNumber_InPlaceDivide(v, w);
1281 Py_DECREF(v);
1282 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001283 SET_TOP(x);
Tim Peters54b11912001-12-25 18:49:11 +00001284 if (x != NULL) continue;
1285 break;
1286 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001287 /* -Qnew is in effect: fall through to
Tim Peters54b11912001-12-25 18:49:11 +00001288 INPLACE_TRUE_DIVIDE */
1289 case INPLACE_TRUE_DIVIDE:
Thomas Wouters434d0822000-08-24 20:11:32 +00001290 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001291 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001292 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001293 Py_DECREF(v);
1294 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001295 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001296 if (x != NULL) continue;
1297 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001298
Guido van Rossum4668b002001-08-08 05:00:18 +00001299 case INPLACE_FLOOR_DIVIDE:
1300 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001301 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001302 x = PyNumber_InPlaceFloorDivide(v, w);
1303 Py_DECREF(v);
1304 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001305 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001306 if (x != NULL) continue;
1307 break;
1308
Thomas Wouters434d0822000-08-24 20:11:32 +00001309 case INPLACE_MODULO:
1310 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001311 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001312 x = PyNumber_InPlaceRemainder(v, w);
1313 Py_DECREF(v);
1314 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001315 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001316 if (x != NULL) continue;
1317 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001318
Thomas Wouters434d0822000-08-24 20:11:32 +00001319 case INPLACE_ADD:
1320 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001321 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001322 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001323 /* INLINE: int + int */
1324 register long a, b, i;
1325 a = PyInt_AS_LONG(v);
1326 b = PyInt_AS_LONG(w);
1327 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001328 if ((i^a) < 0 && (i^b) < 0)
1329 goto slow_iadd;
1330 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001331 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001332 else if (PyString_CheckExact(v) &&
1333 PyString_CheckExact(w)) {
1334 x = string_concatenate(v, w, f, next_instr);
1335 /* string_concatenate consumed the ref to v */
1336 goto skip_decref_v;
1337 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001338 else {
1339 slow_iadd:
Thomas Wouters434d0822000-08-24 20:11:32 +00001340 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001341 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001342 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001343 skip_decref_v:
Thomas Wouters434d0822000-08-24 20:11:32 +00001344 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001345 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001346 if (x != NULL) continue;
1347 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001348
Thomas Wouters434d0822000-08-24 20:11:32 +00001349 case INPLACE_SUBTRACT:
1350 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001351 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001352 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001353 /* INLINE: int - int */
1354 register long a, b, i;
1355 a = PyInt_AS_LONG(v);
1356 b = PyInt_AS_LONG(w);
1357 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001358 if ((i^a) < 0 && (i^~b) < 0)
1359 goto slow_isub;
1360 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001361 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001362 else {
1363 slow_isub:
Thomas Wouters434d0822000-08-24 20:11:32 +00001364 x = PyNumber_InPlaceSubtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001365 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001366 Py_DECREF(v);
1367 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001368 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001369 if (x != NULL) continue;
1370 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001371
Thomas Wouters434d0822000-08-24 20:11:32 +00001372 case INPLACE_LSHIFT:
1373 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001374 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001375 x = PyNumber_InPlaceLshift(v, w);
1376 Py_DECREF(v);
1377 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001378 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001379 if (x != NULL) continue;
1380 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001381
Thomas Wouters434d0822000-08-24 20:11:32 +00001382 case INPLACE_RSHIFT:
1383 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001384 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001385 x = PyNumber_InPlaceRshift(v, w);
1386 Py_DECREF(v);
1387 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001388 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001389 if (x != NULL) continue;
1390 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001391
Thomas Wouters434d0822000-08-24 20:11:32 +00001392 case INPLACE_AND:
1393 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001394 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001395 x = PyNumber_InPlaceAnd(v, w);
1396 Py_DECREF(v);
1397 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001398 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001399 if (x != NULL) continue;
1400 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001401
Thomas Wouters434d0822000-08-24 20:11:32 +00001402 case INPLACE_XOR:
1403 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001404 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001405 x = PyNumber_InPlaceXor(v, w);
1406 Py_DECREF(v);
1407 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001408 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001409 if (x != NULL) continue;
1410 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001411
Thomas Wouters434d0822000-08-24 20:11:32 +00001412 case INPLACE_OR:
1413 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001414 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001415 x = PyNumber_InPlaceOr(v, w);
1416 Py_DECREF(v);
1417 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001418 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001419 if (x != NULL) continue;
1420 break;
1421
Guido van Rossum374a9221991-04-04 10:40:29 +00001422 case SLICE+0:
1423 case SLICE+1:
1424 case SLICE+2:
1425 case SLICE+3:
1426 if ((opcode-SLICE) & 2)
1427 w = POP();
1428 else
1429 w = NULL;
1430 if ((opcode-SLICE) & 1)
1431 v = POP();
1432 else
1433 v = NULL;
Raymond Hettinger663004b2003-01-09 15:24:30 +00001434 u = TOP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001435 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001436 Py_DECREF(u);
1437 Py_XDECREF(v);
1438 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001439 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001440 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001441 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001442
Guido van Rossum374a9221991-04-04 10:40:29 +00001443 case STORE_SLICE+0:
1444 case STORE_SLICE+1:
1445 case STORE_SLICE+2:
1446 case STORE_SLICE+3:
1447 if ((opcode-STORE_SLICE) & 2)
1448 w = POP();
1449 else
1450 w = NULL;
1451 if ((opcode-STORE_SLICE) & 1)
1452 v = POP();
1453 else
1454 v = NULL;
1455 u = POP();
1456 t = POP();
1457 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +00001458 Py_DECREF(t);
1459 Py_DECREF(u);
1460 Py_XDECREF(v);
1461 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001462 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001463 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001464
Guido van Rossum374a9221991-04-04 10:40:29 +00001465 case DELETE_SLICE+0:
1466 case DELETE_SLICE+1:
1467 case DELETE_SLICE+2:
1468 case DELETE_SLICE+3:
1469 if ((opcode-DELETE_SLICE) & 2)
1470 w = POP();
1471 else
1472 w = NULL;
1473 if ((opcode-DELETE_SLICE) & 1)
1474 v = POP();
1475 else
1476 v = NULL;
1477 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001478 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +00001479 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00001480 Py_DECREF(u);
1481 Py_XDECREF(v);
1482 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001483 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001484 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001485
Guido van Rossum374a9221991-04-04 10:40:29 +00001486 case STORE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001487 w = TOP();
1488 v = SECOND();
1489 u = THIRD();
1490 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001491 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001492 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001493 Py_DECREF(u);
1494 Py_DECREF(v);
1495 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001496 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001497 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001498
Guido van Rossum374a9221991-04-04 10:40:29 +00001499 case DELETE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001500 w = TOP();
1501 v = SECOND();
1502 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001503 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001504 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001505 Py_DECREF(v);
1506 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001507 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001508 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001509
Guido van Rossum374a9221991-04-04 10:40:29 +00001510 case PRINT_EXPR:
1511 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001512 w = PySys_GetObject("displayhook");
1513 if (w == NULL) {
1514 PyErr_SetString(PyExc_RuntimeError,
1515 "lost sys.displayhook");
1516 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001517 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001518 }
1519 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001520 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001521 if (x == NULL)
1522 err = -1;
1523 }
1524 if (err == 0) {
1525 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001526 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001527 if (w == NULL)
1528 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001529 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001530 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001531 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001532 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001533
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001534 case PRINT_ITEM_TO:
1535 w = stream = POP();
1536 /* fall through to PRINT_ITEM */
1537
Guido van Rossum374a9221991-04-04 10:40:29 +00001538 case PRINT_ITEM:
1539 v = POP();
Barry Warsaw093abe02000-08-29 04:56:13 +00001540 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001541 w = PySys_GetObject("stdout");
1542 if (w == NULL) {
1543 PyErr_SetString(PyExc_RuntimeError,
1544 "lost sys.stdout");
1545 err = -1;
1546 }
Guido van Rossum8f183201997-12-31 05:53:15 +00001547 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001548 /* PyFile_SoftSpace() can exececute arbitrary code
1549 if sys.stdout is an instance with a __getattr__.
1550 If __getattr__ raises an exception, w will
1551 be freed, so we need to prevent that temporarily. */
1552 Py_XINCREF(w);
Tim Peters8e5fd532002-03-24 19:25:00 +00001553 if (w != NULL && PyFile_SoftSpace(w, 0))
Guido van Rossumbe270261997-05-22 22:26:18 +00001554 err = PyFile_WriteString(" ", w);
1555 if (err == 0)
1556 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001557 if (err == 0) {
Tim Peters8e5fd532002-03-24 19:25:00 +00001558 /* XXX move into writeobject() ? */
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001559 if (PyString_Check(v)) {
1560 char *s = PyString_AS_STRING(v);
1561 int len = PyString_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001562 if (len == 0 ||
1563 !isspace(Py_CHARMASK(s[len-1])) ||
1564 s[len-1] == ' ')
1565 PyFile_SoftSpace(w, 1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001566 }
Martin v. Löwis8d3ce5a2001-12-18 22:36:40 +00001567#ifdef Py_USING_UNICODE
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001568 else if (PyUnicode_Check(v)) {
1569 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
1570 int len = PyUnicode_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001571 if (len == 0 ||
1572 !Py_UNICODE_ISSPACE(s[len-1]) ||
1573 s[len-1] == ' ')
1574 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001575 }
Michael W. Hudsond95c8282002-05-20 13:56:11 +00001576#endif
Tim Peters8e5fd532002-03-24 19:25:00 +00001577 else
1578 PyFile_SoftSpace(w, 1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001579 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001580 Py_XDECREF(w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001581 Py_DECREF(v);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001582 Py_XDECREF(stream);
1583 stream = NULL;
1584 if (err == 0)
1585 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001586 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001587
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001588 case PRINT_NEWLINE_TO:
1589 w = stream = POP();
1590 /* fall through to PRINT_NEWLINE */
1591
Guido van Rossum374a9221991-04-04 10:40:29 +00001592 case PRINT_NEWLINE:
Barry Warsaw093abe02000-08-29 04:56:13 +00001593 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001594 w = PySys_GetObject("stdout");
1595 if (w == NULL)
1596 PyErr_SetString(PyExc_RuntimeError,
1597 "lost sys.stdout");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001598 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001599 if (w != NULL) {
1600 err = PyFile_WriteString("\n", w);
1601 if (err == 0)
1602 PyFile_SoftSpace(w, 0);
1603 }
1604 Py_XDECREF(stream);
1605 stream = NULL;
Guido van Rossum374a9221991-04-04 10:40:29 +00001606 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001607
Thomas Wouters434d0822000-08-24 20:11:32 +00001608
1609#ifdef CASE_TOO_BIG
1610 default: switch (opcode) {
1611#endif
Guido van Rossumf10570b1995-07-07 22:53:21 +00001612 case RAISE_VARARGS:
1613 u = v = w = NULL;
1614 switch (oparg) {
1615 case 3:
1616 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001617 /* Fallthrough */
1618 case 2:
1619 v = POP(); /* value */
1620 /* Fallthrough */
1621 case 1:
1622 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001623 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001624 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001625 break;
1626 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001627 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001628 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001629 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001630 break;
1631 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001632 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001633
Guido van Rossum374a9221991-04-04 10:40:29 +00001634 case LOAD_LOCALS:
Raymond Hettinger467a6982004-04-07 11:39:21 +00001635 if ((x = f->f_locals) != NULL) {
1636 Py_INCREF(x);
1637 PUSH(x);
1638 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001639 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001640 PyErr_SetString(PyExc_SystemError, "no locals");
Guido van Rossum374a9221991-04-04 10:40:29 +00001641 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001642
Guido van Rossum374a9221991-04-04 10:40:29 +00001643 case RETURN_VALUE:
1644 retval = POP();
1645 why = WHY_RETURN;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001646 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001647
Tim Peters5ca576e2001-06-18 22:08:13 +00001648 case YIELD_VALUE:
1649 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001650 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001651 why = WHY_YIELD;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001652 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001653
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001654 case EXEC_STMT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001655 w = TOP();
1656 v = SECOND();
1657 u = THIRD();
1658 STACKADJ(-3);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001659 READ_TIMESTAMP(intr0);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001660 err = exec_statement(f, u, v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001661 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00001662 Py_DECREF(u);
1663 Py_DECREF(v);
1664 Py_DECREF(w);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001665 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001666
Guido van Rossum374a9221991-04-04 10:40:29 +00001667 case POP_BLOCK:
1668 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001669 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001670 while (STACK_LEVEL() > b->b_level) {
1671 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001672 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001673 }
1674 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001675 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001676
Guido van Rossum374a9221991-04-04 10:40:29 +00001677 case END_FINALLY:
1678 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001679 if (PyInt_Check(v)) {
Raymond Hettinger7c958652004-04-06 10:11:10 +00001680 why = (enum why_code) PyInt_AS_LONG(v);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001681 assert(why != WHY_YIELD);
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00001682 if (why == WHY_RETURN ||
1683 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001684 retval = POP();
1685 }
Raymond Hettingerd3b836d2004-04-07 13:17:27 +00001686 else if (PyClass_Check(v) || PyString_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001687 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001688 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001689 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001690 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001691 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001692 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001693 else if (v != Py_None) {
1694 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001695 "'finally' pops bad exception");
1696 why = WHY_EXCEPTION;
1697 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001698 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001699 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001700
Guido van Rossum374a9221991-04-04 10:40:29 +00001701 case BUILD_CLASS:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001702 u = TOP();
1703 v = SECOND();
1704 w = THIRD();
1705 STACKADJ(-2);
Guido van Rossum25831651993-05-19 14:50:45 +00001706 x = build_class(u, v, w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001707 SET_TOP(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001708 Py_DECREF(u);
1709 Py_DECREF(v);
1710 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001711 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001712
Guido van Rossum374a9221991-04-04 10:40:29 +00001713 case STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001714 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001715 v = POP();
Raymond Hettinger467a6982004-04-07 11:39:21 +00001716 if ((x = f->f_locals) != NULL) {
Raymond Hettinger66bd2332004-08-02 08:30:07 +00001717 if (PyDict_CheckExact(x))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001718 err = PyDict_SetItem(x, w, v);
1719 else
1720 err = PyObject_SetItem(x, w, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001721 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001722 if (err == 0) continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001723 break;
1724 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001725 PyErr_Format(PyExc_SystemError,
1726 "no locals found when storing %s",
1727 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001728 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001729
Guido van Rossum374a9221991-04-04 10:40:29 +00001730 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001731 w = GETITEM(names, oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001732 if ((x = f->f_locals) != NULL) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001733 if ((err = PyObject_DelItem(x, w)) != 0)
Raymond Hettinger467a6982004-04-07 11:39:21 +00001734 format_exc_check_arg(PyExc_NameError,
1735 NAME_ERROR_MSG ,w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001736 break;
1737 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001738 PyErr_Format(PyExc_SystemError,
1739 "no locals when deleting %s",
1740 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001741 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001742
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001743 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001744 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001745 v = POP();
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001746 if (PyTuple_CheckExact(v) && PyTuple_GET_SIZE(v) == oparg) {
1747 PyObject **items = ((PyTupleObject *)v)->ob_item;
1748 while (oparg--) {
1749 w = items[oparg];
1750 Py_INCREF(w);
1751 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001752 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001753 Py_DECREF(v);
1754 continue;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001755 } else if (PyList_CheckExact(v) && PyList_GET_SIZE(v) == oparg) {
1756 PyObject **items = ((PyListObject *)v)->ob_item;
1757 while (oparg--) {
1758 w = items[oparg];
1759 Py_INCREF(w);
1760 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001761 }
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001762 } else if (unpack_iterable(v, oparg,
Tim Petersd6d010b2001-06-21 02:49:55 +00001763 stack_pointer + oparg))
1764 stack_pointer += oparg;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001765 else {
1766 if (PyErr_ExceptionMatches(PyExc_TypeError))
1767 PyErr_SetString(PyExc_TypeError,
1768 "unpack non-sequence");
Barry Warsawe42b18f1997-08-25 22:13:04 +00001769 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001770 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001771 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001772 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001773
Guido van Rossum374a9221991-04-04 10:40:29 +00001774 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001775 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001776 v = TOP();
1777 u = SECOND();
1778 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001779 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1780 Py_DECREF(v);
1781 Py_DECREF(u);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001782 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001783 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001784
Guido van Rossum374a9221991-04-04 10:40:29 +00001785 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001786 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001787 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001788 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1789 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001790 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001791 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001792
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001793 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001794 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001795 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001796 err = PyDict_SetItem(f->f_globals, w, v);
1797 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001798 if (err == 0) continue;
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001799 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001800
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001801 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001802 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001803 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001804 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001805 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001806 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001807
Guido van Rossum374a9221991-04-04 10:40:29 +00001808 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001809 w = GETITEM(names, oparg);
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001810 if ((v = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001811 PyErr_Format(PyExc_SystemError,
1812 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001813 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001814 break;
1815 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001816 if (PyDict_CheckExact(v)) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001817 x = PyDict_GetItem(v, w);
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001818 Py_XINCREF(x);
1819 }
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001820 else {
1821 x = PyObject_GetItem(v, w);
1822 if (x == NULL && PyErr_Occurred()) {
1823 if (!PyErr_ExceptionMatches(PyExc_KeyError))
1824 break;
1825 PyErr_Clear();
1826 }
1827 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001828 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001829 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001830 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001831 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001832 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001833 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001834 PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001835 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001836 break;
1837 }
1838 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001839 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001840 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001841 PUSH(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001842 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001843
Guido van Rossum374a9221991-04-04 10:40:29 +00001844 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001845 w = GETITEM(names, oparg);
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001846 if (PyString_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00001847 /* Inline the PyDict_GetItem() calls.
1848 WARNING: this is an extreme speed hack.
1849 Do not try this at home. */
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001850 long hash = ((PyStringObject *)w)->ob_shash;
1851 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001852 PyDictObject *d;
1853 d = (PyDictObject *)(f->f_globals);
1854 x = d->ma_lookup(d, w, hash)->me_value;
1855 if (x != NULL) {
1856 Py_INCREF(x);
1857 PUSH(x);
1858 continue;
1859 }
1860 d = (PyDictObject *)(f->f_builtins);
1861 x = d->ma_lookup(d, w, hash)->me_value;
1862 if (x != NULL) {
1863 Py_INCREF(x);
1864 PUSH(x);
1865 continue;
1866 }
1867 goto load_global_error;
1868 }
1869 }
1870 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00001871 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001872 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001873 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001874 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001875 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00001876 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001877 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001878 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001879 break;
1880 }
1881 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001882 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001883 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001884 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001885
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001886 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001887 x = GETLOCAL(oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001888 if (x != NULL) {
1889 SETLOCAL(oparg, NULL);
1890 continue;
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001891 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001892 format_exc_check_arg(
1893 PyExc_UnboundLocalError,
1894 UNBOUNDLOCAL_ERROR_MSG,
1895 PyTuple_GetItem(co->co_varnames, oparg)
1896 );
1897 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001898
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001899 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001900 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001901 Py_INCREF(x);
1902 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001903 if (x != NULL) continue;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001904 break;
1905
1906 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001907 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001908 w = PyCell_Get(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001909 if (w != NULL) {
1910 PUSH(w);
1911 continue;
Jeremy Hylton2524d692001-02-05 17:23:16 +00001912 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001913 err = -1;
1914 /* Don't stomp existing exception */
1915 if (PyErr_Occurred())
1916 break;
1917 if (oparg < f->f_ncells) {
1918 v = PyTuple_GetItem(co->co_cellvars,
1919 oparg);
1920 format_exc_check_arg(
1921 PyExc_UnboundLocalError,
1922 UNBOUNDLOCAL_ERROR_MSG,
1923 v);
1924 } else {
1925 v = PyTuple_GetItem(
1926 co->co_freevars,
1927 oparg - f->f_ncells);
1928 format_exc_check_arg(
1929 PyExc_NameError,
1930 UNBOUNDFREE_ERROR_MSG,
1931 v);
1932 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001933 break;
1934
1935 case STORE_DEREF:
1936 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001937 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001938 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00001939 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001940 continue;
1941
Guido van Rossum374a9221991-04-04 10:40:29 +00001942 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001943 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001944 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001945 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001946 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001947 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001948 }
1949 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001950 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001951 }
1952 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001953
Guido van Rossum374a9221991-04-04 10:40:29 +00001954 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00001955 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001956 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001957 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001958 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00001959 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001960 }
1961 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001962 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001963 }
1964 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001965
Guido van Rossum374a9221991-04-04 10:40:29 +00001966 case BUILD_MAP:
Guido van Rossumb209a111997-04-29 18:18:01 +00001967 x = PyDict_New();
Guido van Rossum374a9221991-04-04 10:40:29 +00001968 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001969 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001970 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001971
Guido van Rossum374a9221991-04-04 10:40:29 +00001972 case LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001973 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001974 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001975 x = PyObject_GetAttr(v, w);
1976 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001977 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001978 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001979 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001980
Guido van Rossum374a9221991-04-04 10:40:29 +00001981 case COMPARE_OP:
1982 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001983 v = TOP();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001984 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001985 /* INLINE: cmp(int, int) */
1986 register long a, b;
1987 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001988 a = PyInt_AS_LONG(v);
1989 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001990 switch (oparg) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00001991 case PyCmp_LT: res = a < b; break;
1992 case PyCmp_LE: res = a <= b; break;
1993 case PyCmp_EQ: res = a == b; break;
1994 case PyCmp_NE: res = a != b; break;
1995 case PyCmp_GT: res = a > b; break;
1996 case PyCmp_GE: res = a >= b; break;
1997 case PyCmp_IS: res = v == w; break;
1998 case PyCmp_IS_NOT: res = v != w; break;
Guido van Rossumc12da691997-07-17 23:12:42 +00001999 default: goto slow_compare;
2000 }
2001 x = res ? Py_True : Py_False;
2002 Py_INCREF(x);
2003 }
2004 else {
2005 slow_compare:
2006 x = cmp_outcome(oparg, v, w);
2007 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002008 Py_DECREF(v);
2009 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002010 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00002011 if (x == NULL) break;
2012 PREDICT(JUMP_IF_FALSE);
2013 PREDICT(JUMP_IF_TRUE);
2014 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002015
Guido van Rossum374a9221991-04-04 10:40:29 +00002016 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00002017 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002018 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002019 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002020 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00002021 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002022 break;
2023 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00002024 u = TOP();
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002025 w = PyTuple_Pack(4,
Guido van Rossum681d79a1995-07-18 14:51:37 +00002026 w,
2027 f->f_globals,
Guido van Rossuma027efa1997-05-05 20:56:21 +00002028 f->f_locals == NULL ?
2029 Py_None : f->f_locals,
Guido van Rossum681d79a1995-07-18 14:51:37 +00002030 u);
Guido van Rossumb209a111997-04-29 18:18:01 +00002031 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002032 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002033 u = POP();
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002034 x = NULL;
2035 break;
2036 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002037 READ_TIMESTAMP(intr0);
Guido van Rossumb209a111997-04-29 18:18:01 +00002038 x = PyEval_CallObject(x, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002039 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002040 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002041 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002042 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002043 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002044
Thomas Wouters52152252000-08-17 22:55:00 +00002045 case IMPORT_STAR:
2046 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002047 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002048 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002049 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00002050 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002051 break;
2052 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002053 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002054 err = import_all_from(x, v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002055 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002056 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00002057 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002058 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002059 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002060
Thomas Wouters52152252000-08-17 22:55:00 +00002061 case IMPORT_FROM:
Skip Montanaro496e6582002-08-06 17:47:40 +00002062 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00002063 v = TOP();
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002064 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002065 x = import_from(v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002066 READ_TIMESTAMP(intr1);
Thomas Wouters52152252000-08-17 22:55:00 +00002067 PUSH(x);
2068 if (x != NULL) continue;
2069 break;
2070
Guido van Rossum374a9221991-04-04 10:40:29 +00002071 case JUMP_FORWARD:
2072 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002073 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002074
Raymond Hettingerf606f872003-03-16 03:11:04 +00002075 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002076 case JUMP_IF_FALSE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002077 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002078 if (w == Py_True) {
2079 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002080 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002081 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002082 if (w == Py_False) {
2083 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002084 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002085 }
2086 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002087 if (err > 0)
2088 err = 0;
2089 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00002090 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002091 else
2092 break;
2093 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002094
Raymond Hettingerf606f872003-03-16 03:11:04 +00002095 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002096 case JUMP_IF_TRUE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002097 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002098 if (w == Py_False) {
2099 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002100 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002101 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002102 if (w == Py_True) {
2103 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002104 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002105 }
2106 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002107 if (err > 0) {
2108 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00002109 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002110 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002111 else if (err == 0)
2112 ;
2113 else
2114 break;
2115 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002116
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00002117 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002118 case JUMP_ABSOLUTE:
2119 JUMPTO(oparg);
Neil Schemenauerca2a2f12003-05-30 23:59:44 +00002120 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002121
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002122 case GET_ITER:
2123 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002124 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002125 x = PyObject_GetIter(v);
2126 Py_DECREF(v);
2127 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002128 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002129 PREDICT(FOR_ITER);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002130 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002131 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002132 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002133 break;
2134
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002135 PREDICTED_WITH_ARG(FOR_ITER);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002136 case FOR_ITER:
2137 /* before: [iter]; after: [iter, iter()] *or* [] */
2138 v = TOP();
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002139 x = (*v->ob_type->tp_iternext)(v);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002140 if (x != NULL) {
2141 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002142 PREDICT(STORE_FAST);
2143 PREDICT(UNPACK_SEQUENCE);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002144 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002145 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002146 if (PyErr_Occurred()) {
2147 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2148 break;
2149 PyErr_Clear();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002150 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002151 /* iterator ended normally */
2152 x = v = POP();
2153 Py_DECREF(v);
2154 JUMPBY(oparg);
2155 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002156
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002157 case BREAK_LOOP:
2158 why = WHY_BREAK;
2159 goto fast_block_end;
2160
2161 case CONTINUE_LOOP:
2162 retval = PyInt_FromLong(oparg);
2163 why = WHY_CONTINUE;
2164 goto fast_block_end;
2165
Guido van Rossum374a9221991-04-04 10:40:29 +00002166 case SETUP_LOOP:
2167 case SETUP_EXCEPT:
2168 case SETUP_FINALLY:
Guido van Rossumb209a111997-04-29 18:18:01 +00002169 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002170 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002171 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002172
Guido van Rossumf10570b1995-07-07 22:53:21 +00002173 case CALL_FUNCTION:
Armin Rigo8817fcd2004-06-17 10:22:40 +00002174 {
2175 PyObject **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002176 PCALL(PCALL_ALL);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002177 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002178#ifdef WITH_TSC
Armin Rigo8817fcd2004-06-17 10:22:40 +00002179 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002180#else
Armin Rigo8817fcd2004-06-17 10:22:40 +00002181 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002182#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002183 stack_pointer = sp;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002184 PUSH(x);
2185 if (x != NULL)
2186 continue;
2187 break;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002188 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002189
Jeremy Hylton76901512000-03-28 23:49:17 +00002190 case CALL_FUNCTION_VAR:
2191 case CALL_FUNCTION_KW:
2192 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002193 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002194 int na = oparg & 0xff;
2195 int nk = (oparg>>8) & 0xff;
2196 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002197 int n = na + 2 * nk;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002198 PyObject **pfunc, *func, **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002199 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002200 if (flags & CALL_FLAG_VAR)
2201 n++;
2202 if (flags & CALL_FLAG_KW)
2203 n++;
2204 pfunc = stack_pointer - n - 1;
2205 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002206
Guido van Rossumac7be682001-01-17 15:42:30 +00002207 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002208 && PyMethod_GET_SELF(func) != NULL) {
2209 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002210 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002211 func = PyMethod_GET_FUNCTION(func);
2212 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002213 Py_DECREF(*pfunc);
2214 *pfunc = self;
2215 na++;
2216 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002217 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002218 Py_INCREF(func);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002219 sp = stack_pointer;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002220 READ_TIMESTAMP(intr0);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002221 x = ext_do_call(func, &sp, flags, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002222 READ_TIMESTAMP(intr1);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002223 stack_pointer = sp;
Jeremy Hylton76901512000-03-28 23:49:17 +00002224 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002225
Jeremy Hylton76901512000-03-28 23:49:17 +00002226 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002227 w = POP();
2228 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002229 }
2230 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002231 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002232 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002233 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002234 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002235
Guido van Rossum681d79a1995-07-18 14:51:37 +00002236 case MAKE_FUNCTION:
2237 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002238 x = PyFunction_New(v, f->f_globals);
2239 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002240 /* XXX Maybe this should be a separate opcode? */
2241 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002242 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002243 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002244 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002245 x = NULL;
2246 break;
2247 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002248 while (--oparg >= 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002249 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002250 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002251 }
2252 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002253 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002254 }
2255 PUSH(x);
2256 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002257
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002258 case MAKE_CLOSURE:
2259 {
2260 int nfree;
2261 v = POP(); /* code object */
2262 x = PyFunction_New(v, f->f_globals);
Jeremy Hylton733c8932001-12-13 19:51:56 +00002263 nfree = PyCode_GetNumFree((PyCodeObject *)v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002264 Py_DECREF(v);
2265 /* XXX Maybe this should be a separate opcode? */
2266 if (x != NULL && nfree > 0) {
2267 v = PyTuple_New(nfree);
2268 if (v == NULL) {
2269 Py_DECREF(x);
2270 x = NULL;
2271 break;
2272 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002273 while (--nfree >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002274 w = POP();
2275 PyTuple_SET_ITEM(v, nfree, w);
2276 }
2277 err = PyFunction_SetClosure(x, v);
2278 Py_DECREF(v);
2279 }
2280 if (x != NULL && oparg > 0) {
2281 v = PyTuple_New(oparg);
2282 if (v == NULL) {
2283 Py_DECREF(x);
2284 x = NULL;
2285 break;
2286 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002287 while (--oparg >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002288 w = POP();
2289 PyTuple_SET_ITEM(v, oparg, w);
2290 }
2291 err = PyFunction_SetDefaults(x, v);
2292 Py_DECREF(v);
2293 }
2294 PUSH(x);
2295 break;
2296 }
2297
Guido van Rossum8861b741996-07-30 16:49:37 +00002298 case BUILD_SLICE:
2299 if (oparg == 3)
2300 w = POP();
2301 else
2302 w = NULL;
2303 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002304 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002305 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002306 Py_DECREF(u);
2307 Py_DECREF(v);
2308 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002309 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002310 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002311 break;
2312
Fred Drakeef8ace32000-08-24 00:32:09 +00002313 case EXTENDED_ARG:
2314 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002315 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002316 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002317
Guido van Rossum374a9221991-04-04 10:40:29 +00002318 default:
2319 fprintf(stderr,
2320 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002321 PyCode_Addr2Line(f->f_code, f->f_lasti),
2322 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002323 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002324 why = WHY_EXCEPTION;
2325 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002326
2327#ifdef CASE_TOO_BIG
2328 }
2329#endif
2330
Guido van Rossum374a9221991-04-04 10:40:29 +00002331 } /* switch */
2332
2333 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002334
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002335 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002336
Guido van Rossum374a9221991-04-04 10:40:29 +00002337 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002338
Guido van Rossum374a9221991-04-04 10:40:29 +00002339 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002340 if (err == 0 && x != NULL) {
2341#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002342 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002343 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002344 fprintf(stderr,
2345 "XXX undetected error\n");
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002346 else {
2347#endif
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002348 READ_TIMESTAMP(loop1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002349 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002350#ifdef CHECKEXC
2351 }
2352#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +00002353 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002354 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002355 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002356 err = 0;
2357 }
2358
Guido van Rossum374a9221991-04-04 10:40:29 +00002359 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002360
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002361 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002362 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002363 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002364 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002365 why = WHY_EXCEPTION;
2366 }
2367 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002368#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002369 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002370 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002371 if (PyErr_Occurred()) {
Jeremy Hylton904ed862003-11-05 17:29:35 +00002372 char buf[1024];
2373 sprintf(buf, "Stack unwind with exception "
2374 "set and why=%d", why);
2375 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002376 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002377 }
2378#endif
2379
2380 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002381
Guido van Rossum374a9221991-04-04 10:40:29 +00002382 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002383 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002384
Fred Drake8f51f542001-10-04 14:48:42 +00002385 if (tstate->c_tracefunc != NULL)
2386 call_exc_trace(tstate->c_tracefunc,
2387 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002388 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002389
Guido van Rossum374a9221991-04-04 10:40:29 +00002390 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002391
Guido van Rossum374a9221991-04-04 10:40:29 +00002392 if (why == WHY_RERAISE)
2393 why = WHY_EXCEPTION;
2394
2395 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002396
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002397fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002398 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002399 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002400
Tim Peters8a5c3c72004-04-05 19:36:21 +00002401 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002402 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2403 /* For a continue inside a try block,
2404 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002405 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2406 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002407 why = WHY_NOT;
2408 JUMPTO(PyInt_AS_LONG(retval));
2409 Py_DECREF(retval);
2410 break;
2411 }
2412
Guido van Rossum374a9221991-04-04 10:40:29 +00002413 while (STACK_LEVEL() > b->b_level) {
2414 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002415 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002416 }
2417 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2418 why = WHY_NOT;
2419 JUMPTO(b->b_handler);
2420 break;
2421 }
2422 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002423 (b->b_type == SETUP_EXCEPT &&
2424 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002425 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002426 PyObject *exc, *val, *tb;
2427 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002428 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002429 val = Py_None;
2430 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002431 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002432 /* Make the raw exception data
2433 available to the handler,
2434 so a program can emulate the
2435 Python main loop. Don't do
2436 this for 'finally'. */
2437 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002438 PyErr_NormalizeException(
2439 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002440 set_exc_info(tstate,
2441 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002442 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002443 if (tb == NULL) {
2444 Py_INCREF(Py_None);
2445 PUSH(Py_None);
2446 } else
2447 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002448 PUSH(val);
2449 PUSH(exc);
2450 }
2451 else {
Raymond Hettinger06032cb2004-04-06 09:37:35 +00002452 if (why & (WHY_RETURN | WHY_CONTINUE))
Guido van Rossum374a9221991-04-04 10:40:29 +00002453 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002454 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002455 PUSH(v);
2456 }
2457 why = WHY_NOT;
2458 JUMPTO(b->b_handler);
2459 break;
2460 }
2461 } /* unwind stack */
2462
2463 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002464
Guido van Rossum374a9221991-04-04 10:40:29 +00002465 if (why != WHY_NOT)
2466 break;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002467 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002468
Guido van Rossum374a9221991-04-04 10:40:29 +00002469 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002470
Tim Peters8a5c3c72004-04-05 19:36:21 +00002471 assert(why != WHY_YIELD);
2472 /* Pop remaining stack entries. */
2473 while (!EMPTY()) {
2474 v = POP();
2475 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002476 }
2477
Tim Peters8a5c3c72004-04-05 19:36:21 +00002478 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002479 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002480
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002481fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002482 if (tstate->use_tracing) {
Barry Warsawe2eca0b2005-08-15 18:14:19 +00002483 if (tstate->c_tracefunc) {
2484 if (why == WHY_RETURN || why == WHY_YIELD) {
2485 if (call_trace(tstate->c_tracefunc,
2486 tstate->c_traceobj, f,
2487 PyTrace_RETURN, retval)) {
2488 Py_XDECREF(retval);
2489 retval = NULL;
2490 why = WHY_EXCEPTION;
2491 }
2492 }
2493 else if (why == WHY_EXCEPTION) {
2494 call_trace_protected(tstate->c_tracefunc,
2495 tstate->c_traceobj, f,
2496 PyTrace_RETURN);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002497 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002498 }
Fred Drake8f51f542001-10-04 14:48:42 +00002499 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002500 if (why == WHY_EXCEPTION)
2501 call_trace_protected(tstate->c_profilefunc,
2502 tstate->c_profileobj, f,
2503 PyTrace_RETURN);
2504 else if (call_trace(tstate->c_profilefunc,
2505 tstate->c_profileobj, f,
2506 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002507 Py_XDECREF(retval);
2508 retval = NULL;
2509 why = WHY_EXCEPTION;
2510 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002511 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002512 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002513
Guido van Rossuma027efa1997-05-05 20:56:21 +00002514 reset_exc_info(tstate);
2515
Tim Peters5ca576e2001-06-18 22:08:13 +00002516 /* pop frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +00002517 exit_eval_frame:
2518 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002519 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002520
Guido van Rossum96a42c81992-01-12 02:29:51 +00002521 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002522}
2523
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002524/* this is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002525 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Skip Montanaroc9a47622005-01-08 21:58:58 +00002526 the test in the if statement in Misc/gdbinit:pystack* */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002527
Tim Peters6d6c1a32001-08-02 04:15:00 +00002528PyObject *
2529PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002530 PyObject **args, int argcount, PyObject **kws, int kwcount,
2531 PyObject **defs, int defcount, PyObject *closure)
2532{
2533 register PyFrameObject *f;
2534 register PyObject *retval = NULL;
2535 register PyObject **fastlocals, **freevars;
2536 PyThreadState *tstate = PyThreadState_GET();
2537 PyObject *x, *u;
2538
2539 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00002540 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002541 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002542 return NULL;
2543 }
2544
Jeremy Hylton985eba52003-02-05 23:13:00 +00002545 assert(globals != NULL);
2546 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002547 if (f == NULL)
2548 return NULL;
2549
2550 fastlocals = f->f_localsplus;
2551 freevars = f->f_localsplus + f->f_nlocals;
2552
2553 if (co->co_argcount > 0 ||
2554 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2555 int i;
2556 int n = argcount;
2557 PyObject *kwdict = NULL;
2558 if (co->co_flags & CO_VARKEYWORDS) {
2559 kwdict = PyDict_New();
2560 if (kwdict == NULL)
2561 goto fail;
2562 i = co->co_argcount;
2563 if (co->co_flags & CO_VARARGS)
2564 i++;
2565 SETLOCAL(i, kwdict);
2566 }
2567 if (argcount > co->co_argcount) {
2568 if (!(co->co_flags & CO_VARARGS)) {
2569 PyErr_Format(PyExc_TypeError,
2570 "%.200s() takes %s %d "
2571 "%sargument%s (%d given)",
2572 PyString_AsString(co->co_name),
2573 defcount ? "at most" : "exactly",
2574 co->co_argcount,
2575 kwcount ? "non-keyword " : "",
2576 co->co_argcount == 1 ? "" : "s",
2577 argcount);
2578 goto fail;
2579 }
2580 n = co->co_argcount;
2581 }
2582 for (i = 0; i < n; i++) {
2583 x = args[i];
2584 Py_INCREF(x);
2585 SETLOCAL(i, x);
2586 }
2587 if (co->co_flags & CO_VARARGS) {
2588 u = PyTuple_New(argcount - n);
2589 if (u == NULL)
2590 goto fail;
2591 SETLOCAL(co->co_argcount, u);
2592 for (i = n; i < argcount; i++) {
2593 x = args[i];
2594 Py_INCREF(x);
2595 PyTuple_SET_ITEM(u, i-n, x);
2596 }
2597 }
2598 for (i = 0; i < kwcount; i++) {
2599 PyObject *keyword = kws[2*i];
2600 PyObject *value = kws[2*i + 1];
2601 int j;
2602 if (keyword == NULL || !PyString_Check(keyword)) {
2603 PyErr_Format(PyExc_TypeError,
2604 "%.200s() keywords must be strings",
2605 PyString_AsString(co->co_name));
2606 goto fail;
2607 }
2608 /* XXX slow -- speed up using dictionary? */
2609 for (j = 0; j < co->co_argcount; j++) {
2610 PyObject *nm = PyTuple_GET_ITEM(
2611 co->co_varnames, j);
2612 int cmp = PyObject_RichCompareBool(
2613 keyword, nm, Py_EQ);
2614 if (cmp > 0)
2615 break;
2616 else if (cmp < 0)
2617 goto fail;
2618 }
2619 /* Check errors from Compare */
2620 if (PyErr_Occurred())
2621 goto fail;
2622 if (j >= co->co_argcount) {
2623 if (kwdict == NULL) {
2624 PyErr_Format(PyExc_TypeError,
2625 "%.200s() got an unexpected "
2626 "keyword argument '%.400s'",
2627 PyString_AsString(co->co_name),
2628 PyString_AsString(keyword));
2629 goto fail;
2630 }
2631 PyDict_SetItem(kwdict, keyword, value);
2632 }
2633 else {
2634 if (GETLOCAL(j) != NULL) {
2635 PyErr_Format(PyExc_TypeError,
2636 "%.200s() got multiple "
2637 "values for keyword "
2638 "argument '%.400s'",
2639 PyString_AsString(co->co_name),
2640 PyString_AsString(keyword));
2641 goto fail;
2642 }
2643 Py_INCREF(value);
2644 SETLOCAL(j, value);
2645 }
2646 }
2647 if (argcount < co->co_argcount) {
2648 int m = co->co_argcount - defcount;
2649 for (i = argcount; i < m; i++) {
2650 if (GETLOCAL(i) == NULL) {
2651 PyErr_Format(PyExc_TypeError,
2652 "%.200s() takes %s %d "
2653 "%sargument%s (%d given)",
2654 PyString_AsString(co->co_name),
2655 ((co->co_flags & CO_VARARGS) ||
2656 defcount) ? "at least"
2657 : "exactly",
2658 m, kwcount ? "non-keyword " : "",
2659 m == 1 ? "" : "s", i);
2660 goto fail;
2661 }
2662 }
2663 if (n > m)
2664 i = n - m;
2665 else
2666 i = 0;
2667 for (; i < defcount; i++) {
2668 if (GETLOCAL(m+i) == NULL) {
2669 PyObject *def = defs[i];
2670 Py_INCREF(def);
2671 SETLOCAL(m+i, def);
2672 }
2673 }
2674 }
2675 }
2676 else {
2677 if (argcount > 0 || kwcount > 0) {
2678 PyErr_Format(PyExc_TypeError,
2679 "%.200s() takes no arguments (%d given)",
2680 PyString_AsString(co->co_name),
2681 argcount + kwcount);
2682 goto fail;
2683 }
2684 }
2685 /* Allocate and initialize storage for cell vars, and copy free
2686 vars into frame. This isn't too efficient right now. */
2687 if (f->f_ncells) {
2688 int i = 0, j = 0, nargs, found;
2689 char *cellname, *argname;
2690 PyObject *c;
2691
2692 nargs = co->co_argcount;
2693 if (co->co_flags & CO_VARARGS)
2694 nargs++;
2695 if (co->co_flags & CO_VARKEYWORDS)
2696 nargs++;
2697
2698 /* Check for cells that shadow args */
2699 for (i = 0; i < f->f_ncells && j < nargs; ++i) {
2700 cellname = PyString_AS_STRING(
2701 PyTuple_GET_ITEM(co->co_cellvars, i));
2702 found = 0;
2703 while (j < nargs) {
2704 argname = PyString_AS_STRING(
2705 PyTuple_GET_ITEM(co->co_varnames, j));
2706 if (strcmp(cellname, argname) == 0) {
2707 c = PyCell_New(GETLOCAL(j));
2708 if (c == NULL)
2709 goto fail;
2710 GETLOCAL(f->f_nlocals + i) = c;
2711 found = 1;
2712 break;
2713 }
2714 j++;
2715 }
2716 if (found == 0) {
2717 c = PyCell_New(NULL);
2718 if (c == NULL)
2719 goto fail;
2720 SETLOCAL(f->f_nlocals + i, c);
2721 }
2722 }
2723 /* Initialize any that are left */
2724 while (i < f->f_ncells) {
2725 c = PyCell_New(NULL);
2726 if (c == NULL)
2727 goto fail;
2728 SETLOCAL(f->f_nlocals + i, c);
2729 i++;
2730 }
2731 }
2732 if (f->f_nfreevars) {
2733 int i;
2734 for (i = 0; i < f->f_nfreevars; ++i) {
2735 PyObject *o = PyTuple_GET_ITEM(closure, i);
2736 Py_INCREF(o);
2737 freevars[f->f_ncells + i] = o;
2738 }
2739 }
2740
Tim Peters5ca576e2001-06-18 22:08:13 +00002741 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002742 /* Don't need to keep the reference to f_back, it will be set
2743 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002744 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002745 f->f_back = NULL;
2746
Jeremy Hylton985eba52003-02-05 23:13:00 +00002747 PCALL(PCALL_GENERATOR);
2748
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002749 /* Create a new generator that owns the ready to run frame
2750 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00002751 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00002752 }
2753
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002754 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00002755
2756 fail: /* Jump here from prelude on failure */
2757
Tim Petersb13680b2001-11-27 23:29:29 +00002758 /* decref'ing the frame can cause __del__ methods to get invoked,
2759 which can call back into Python. While we're done with the
2760 current Python frame (f), the associated C stack is still in use,
2761 so recursion_depth must be boosted for the duration.
2762 */
2763 assert(tstate != NULL);
2764 ++tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002765 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00002766 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002767 return retval;
2768}
2769
2770
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002771/* Implementation notes for set_exc_info() and reset_exc_info():
2772
2773- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2774 'exc_traceback'. These always travel together.
2775
2776- tstate->curexc_ZZZ is the "hot" exception that is set by
2777 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2778
2779- Once an exception is caught by an except clause, it is transferred
2780 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2781 can pick it up. This is the primary task of set_exc_info().
2782
2783- Now let me explain the complicated dance with frame->f_exc_ZZZ.
2784
2785 Long ago, when none of this existed, there were just a few globals:
2786 one set corresponding to the "hot" exception, and one set
2787 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2788 globals; they were simply stored as sys.exc_ZZZ. For backwards
2789 compatibility, they still are!) The problem was that in code like
2790 this:
2791
2792 try:
2793 "something that may fail"
2794 except "some exception":
2795 "do something else first"
2796 "print the exception from sys.exc_ZZZ."
2797
2798 if "do something else first" invoked something that raised and caught
2799 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2800 cause of subtle bugs. I fixed this by changing the semantics as
2801 follows:
2802
2803 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2804 *in that frame*.
2805
2806 - But initially, and as long as no exception is caught in a given
2807 frame, sys.exc_ZZZ will hold the last exception caught in the
2808 previous frame (or the frame before that, etc.).
2809
2810 The first bullet fixed the bug in the above example. The second
2811 bullet was for backwards compatibility: it was (and is) common to
2812 have a function that is called when an exception is caught, and to
2813 have that function access the caught exception via sys.exc_ZZZ.
2814 (Example: traceback.print_exc()).
2815
2816 At the same time I fixed the problem that sys.exc_ZZZ weren't
2817 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2818 but that's really a separate improvement.
2819
2820 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2821 variables to what they were before the current frame was called. The
2822 set_exc_info() function saves them on the frame so that
2823 reset_exc_info() can restore them. The invariant is that
2824 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2825 exception (where "catching" an exception applies only to successful
2826 except clauses); and if the current frame ever caught an exception,
2827 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2828 at the start of the current frame.
2829
2830*/
2831
Guido van Rossuma027efa1997-05-05 20:56:21 +00002832static void
Guido van Rossumac7be682001-01-17 15:42:30 +00002833set_exc_info(PyThreadState *tstate,
2834 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002835{
2836 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002837 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00002838
Guido van Rossuma027efa1997-05-05 20:56:21 +00002839 frame = tstate->frame;
2840 if (frame->f_exc_type == NULL) {
2841 /* This frame didn't catch an exception before */
2842 /* Save previous exception of this thread in this frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002843 if (tstate->exc_type == NULL) {
2844 Py_INCREF(Py_None);
2845 tstate->exc_type = Py_None;
2846 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002847 tmp_type = frame->f_exc_type;
2848 tmp_value = frame->f_exc_value;
2849 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002850 Py_XINCREF(tstate->exc_type);
2851 Py_XINCREF(tstate->exc_value);
2852 Py_XINCREF(tstate->exc_traceback);
2853 frame->f_exc_type = tstate->exc_type;
2854 frame->f_exc_value = tstate->exc_value;
2855 frame->f_exc_traceback = tstate->exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002856 Py_XDECREF(tmp_type);
2857 Py_XDECREF(tmp_value);
2858 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002859 }
2860 /* Set new exception for this thread */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002861 tmp_type = tstate->exc_type;
2862 tmp_value = tstate->exc_value;
2863 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002864 Py_XINCREF(type);
2865 Py_XINCREF(value);
2866 Py_XINCREF(tb);
2867 tstate->exc_type = type;
2868 tstate->exc_value = value;
2869 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002870 Py_XDECREF(tmp_type);
2871 Py_XDECREF(tmp_value);
2872 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002873 /* For b/w compatibility */
2874 PySys_SetObject("exc_type", type);
2875 PySys_SetObject("exc_value", value);
2876 PySys_SetObject("exc_traceback", tb);
2877}
2878
2879static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002880reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002881{
2882 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002883 PyObject *tmp_type, *tmp_value, *tmp_tb;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002884 frame = tstate->frame;
2885 if (frame->f_exc_type != NULL) {
2886 /* This frame caught an exception */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002887 tmp_type = tstate->exc_type;
2888 tmp_value = tstate->exc_value;
2889 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002890 Py_XINCREF(frame->f_exc_type);
2891 Py_XINCREF(frame->f_exc_value);
2892 Py_XINCREF(frame->f_exc_traceback);
2893 tstate->exc_type = frame->f_exc_type;
2894 tstate->exc_value = frame->f_exc_value;
2895 tstate->exc_traceback = frame->f_exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002896 Py_XDECREF(tmp_type);
2897 Py_XDECREF(tmp_value);
2898 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002899 /* For b/w compatibility */
2900 PySys_SetObject("exc_type", frame->f_exc_type);
2901 PySys_SetObject("exc_value", frame->f_exc_value);
2902 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
2903 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002904 tmp_type = frame->f_exc_type;
2905 tmp_value = frame->f_exc_value;
2906 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002907 frame->f_exc_type = NULL;
2908 frame->f_exc_value = NULL;
2909 frame->f_exc_traceback = NULL;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002910 Py_XDECREF(tmp_type);
2911 Py_XDECREF(tmp_value);
2912 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002913}
2914
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002915/* Logic for the raise statement (too complicated for inlining).
2916 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00002917static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002918do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002919{
Guido van Rossumd295f121998-04-09 21:39:57 +00002920 if (type == NULL) {
2921 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00002922 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumd295f121998-04-09 21:39:57 +00002923 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
2924 value = tstate->exc_value;
2925 tb = tstate->exc_traceback;
2926 Py_XINCREF(type);
2927 Py_XINCREF(value);
2928 Py_XINCREF(tb);
2929 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002930
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002931 /* We support the following forms of raise:
2932 raise <class>, <classinstance>
2933 raise <class>, <argument tuple>
2934 raise <class>, None
2935 raise <class>, <argument>
2936 raise <classinstance>, None
2937 raise <string>, <object>
2938 raise <string>, None
2939
2940 An omitted second argument is the same as None.
2941
2942 In addition, raise <tuple>, <anything> is the same as
2943 raising the tuple's first item (and it better have one!);
2944 this rule is applied recursively.
2945
2946 Finally, an optional third argument can be supplied, which
2947 gives the traceback to be substituted (useful when
2948 re-raising an exception after examining it). */
2949
2950 /* First, check the traceback argument, replacing None with
2951 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002952 if (tb == Py_None) {
2953 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002954 tb = NULL;
2955 }
2956 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002957 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002958 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002959 goto raise_error;
2960 }
2961
2962 /* Next, replace a missing value with None */
2963 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002964 value = Py_None;
2965 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002966 }
2967
2968 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00002969 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
2970 PyObject *tmp = type;
2971 type = PyTuple_GET_ITEM(type, 0);
2972 Py_INCREF(type);
2973 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002974 }
2975
Tim Petersafb2c802002-04-18 18:06:20 +00002976 if (PyString_CheckExact(type))
2977 /* Raising builtin string is deprecated but still allowed --
2978 * do nothing. Raising an instance of a new-style str
2979 * subclass is right out. */
Neal Norwitz37aa0662003-01-10 15:31:15 +00002980 PyErr_Warn(PyExc_PendingDeprecationWarning,
2981 "raising a string exception is deprecated");
Barry Warsaw4249f541997-08-22 21:26:19 +00002982
2983 else if (PyClass_Check(type))
2984 PyErr_NormalizeException(&type, &value, &tb);
2985
Guido van Rossumb209a111997-04-29 18:18:01 +00002986 else if (PyInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002987 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002988 if (value != Py_None) {
2989 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002990 "instance exception may not have a separate value");
2991 goto raise_error;
2992 }
2993 else {
2994 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00002995 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002996 value = type;
Guido van Rossumb209a111997-04-29 18:18:01 +00002997 type = (PyObject*) ((PyInstanceObject*)type)->in_class;
2998 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002999 }
3000 }
3001 else {
3002 /* Not something you can raise. You get an exception
3003 anyway, just not what you specified :-) */
Jeremy Hylton960d9482001-04-27 02:25:33 +00003004 PyErr_Format(PyExc_TypeError,
Neal Norwitz37aa0662003-01-10 15:31:15 +00003005 "exceptions must be classes, instances, or "
3006 "strings (deprecated), not %s",
3007 type->ob_type->tp_name);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003008 goto raise_error;
3009 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003010 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003011 if (tb == NULL)
3012 return WHY_EXCEPTION;
3013 else
3014 return WHY_RERAISE;
3015 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00003016 Py_XDECREF(value);
3017 Py_XDECREF(type);
3018 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003019 return WHY_EXCEPTION;
3020}
3021
Tim Petersd6d010b2001-06-21 02:49:55 +00003022/* Iterate v argcnt times and store the results on the stack (via decreasing
3023 sp). Return 1 for success, 0 if error. */
3024
Barry Warsawe42b18f1997-08-25 22:13:04 +00003025static int
Tim Petersd6d010b2001-06-21 02:49:55 +00003026unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003027{
Tim Petersd6d010b2001-06-21 02:49:55 +00003028 int i = 0;
3029 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00003030 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00003031
Tim Petersd6d010b2001-06-21 02:49:55 +00003032 assert(v != NULL);
3033
3034 it = PyObject_GetIter(v);
3035 if (it == NULL)
3036 goto Error;
3037
3038 for (; i < argcnt; i++) {
3039 w = PyIter_Next(it);
3040 if (w == NULL) {
3041 /* Iterator done, via error or exhaustion. */
3042 if (!PyErr_Occurred()) {
3043 PyErr_Format(PyExc_ValueError,
3044 "need more than %d value%s to unpack",
3045 i, i == 1 ? "" : "s");
3046 }
3047 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003048 }
3049 *--sp = w;
3050 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003051
3052 /* We better have exhausted the iterator now. */
3053 w = PyIter_Next(it);
3054 if (w == NULL) {
3055 if (PyErr_Occurred())
3056 goto Error;
3057 Py_DECREF(it);
3058 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003059 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00003060 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00003061 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00003062 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00003063Error:
Barry Warsaw91010551997-08-25 22:30:51 +00003064 for (; i > 0; i--, sp++)
3065 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00003066 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00003067 return 0;
3068}
3069
3070
Guido van Rossum96a42c81992-01-12 02:29:51 +00003071#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003072static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003073prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003074{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003075 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003076 if (PyObject_Print(v, stdout, 0) != 0)
3077 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003078 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003079 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003080}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003081#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003082
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003083static void
Fred Drake5755ce62001-06-27 19:19:46 +00003084call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003085{
Guido van Rossumb209a111997-04-29 18:18:01 +00003086 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003087 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003088 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003089 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003090 value = Py_None;
3091 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003092 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003093 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003094 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003095 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003096 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003097 }
Fred Drake5755ce62001-06-27 19:19:46 +00003098 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003099 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003100 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003101 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003102 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003103 Py_XDECREF(type);
3104 Py_XDECREF(value);
3105 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003106 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003107}
3108
Fred Drake4ec5d562001-10-04 19:26:43 +00003109static void
3110call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3111 int what)
3112{
3113 PyObject *type, *value, *traceback;
3114 int err;
3115 PyErr_Fetch(&type, &value, &traceback);
3116 err = call_trace(func, obj, frame, what, NULL);
3117 if (err == 0)
3118 PyErr_Restore(type, value, traceback);
3119 else {
3120 Py_XDECREF(type);
3121 Py_XDECREF(value);
3122 Py_XDECREF(traceback);
3123 }
3124}
3125
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003126static int
Fred Drake5755ce62001-06-27 19:19:46 +00003127call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3128 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003129{
Fred Drake5755ce62001-06-27 19:19:46 +00003130 register PyThreadState *tstate = frame->f_tstate;
3131 int result;
3132 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003133 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003134 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003135 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003136 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003137 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3138 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003139 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003140 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003141}
3142
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003143PyObject *
3144_PyEval_CallTracing(PyObject *func, PyObject *args)
3145{
3146 PyFrameObject *frame = PyEval_GetFrame();
3147 PyThreadState *tstate = frame->f_tstate;
3148 int save_tracing = tstate->tracing;
3149 int save_use_tracing = tstate->use_tracing;
3150 PyObject *result;
3151
3152 tstate->tracing = 0;
3153 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3154 || (tstate->c_profilefunc != NULL));
3155 result = PyObject_Call(func, args, NULL);
3156 tstate->tracing = save_tracing;
3157 tstate->use_tracing = save_use_tracing;
3158 return result;
3159}
3160
Michael W. Hudson006c7522002-11-08 13:08:46 +00003161static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003162maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003163 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3164 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003165{
3166 /* The theory of SET_LINENO-less tracing.
Tim Peters8a5c3c72004-04-05 19:36:21 +00003167
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003168 In a nutshell, we use the co_lnotab field of the code object
3169 to tell when execution has moved onto a different line.
3170
3171 As mentioned above, the basic idea is so set things up so
3172 that
3173
3174 *instr_lb <= frame->f_lasti < *instr_ub
3175
3176 is true so long as execution does not change lines.
3177
3178 This is all fairly simple. Digging the information out of
3179 co_lnotab takes some work, but is conceptually clear.
3180
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003181 Somewhat harder to explain is why we don't *always* call the
3182 line trace function when the above test fails.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003183
3184 Consider this code:
3185
3186 1: def f(a):
3187 2: if a:
3188 3: print 1
3189 4: else:
3190 5: print 2
3191
3192 which compiles to this:
3193
3194 2 0 LOAD_FAST 0 (a)
3195 3 JUMP_IF_FALSE 9 (to 15)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003196 6 POP_TOP
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003197
3198 3 7 LOAD_CONST 1 (1)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003199 10 PRINT_ITEM
3200 11 PRINT_NEWLINE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003201 12 JUMP_FORWARD 6 (to 21)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003202 >> 15 POP_TOP
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003203
3204 5 16 LOAD_CONST 2 (2)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003205 19 PRINT_ITEM
3206 20 PRINT_NEWLINE
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003207 >> 21 LOAD_CONST 0 (None)
3208 24 RETURN_VALUE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003209
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003210 If 'a' is false, execution will jump to instruction at offset
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003211 15 and the co_lnotab will claim that execution has moved to
3212 line 3. This is at best misleading. In this case we could
3213 associate the POP_TOP with line 4, but that doesn't make
3214 sense in all cases (I think).
3215
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003216 What we do is only call the line trace function if the co_lnotab
3217 indicates we have jumped to the *start* of a line, i.e. if the
3218 current instruction offset matches the offset given for the
3219 start of a line by the co_lnotab.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003220
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003221 This also takes care of the situation where 'a' is true.
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003222 Execution will jump from instruction offset 12 to offset 21.
3223 Then the co_lnotab would imply that execution has moved to line
3224 5, which is again misleading.
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003225
3226 Why do we set f_lineno when tracing? Well, consider the code
3227 above when 'a' is true. If stepping through this with 'n' in
3228 pdb, you would stop at line 1 with a "call" type event, then
3229 line events on lines 2 and 3, then a "return" type event -- but
3230 you would be shown line 5 during this event. This is a change
3231 from the behaviour in 2.2 and before, and I've found it
3232 confusing in practice. By setting and using f_lineno when
3233 tracing, one can report a line number different from that
3234 suggested by f_lasti on this one occasion where it's desirable.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003235 */
3236
Michael W. Hudson006c7522002-11-08 13:08:46 +00003237 int result = 0;
3238
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003239 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003240 PyCodeObject* co = frame->f_code;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003241 int size, addr, line;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003242 unsigned char* p;
3243
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003244 size = PyString_GET_SIZE(co->co_lnotab) / 2;
3245 p = (unsigned char*)PyString_AS_STRING(co->co_lnotab);
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003246
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003247 addr = 0;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003248 line = co->co_firstlineno;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003249
3250 /* possible optimization: if f->f_lasti == instr_ub
3251 (likely to be a common case) then we already know
3252 instr_lb -- if we stored the matching value of p
3253 somwhere we could skip the first while loop. */
3254
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003255 /* see comments in compile.c for the description of
3256 co_lnotab. A point to remember: increments to p
3257 should come in pairs -- although we don't care about
3258 the line increments here, treating them as byte
3259 increments gets confusing, to say the least. */
3260
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003261 while (size > 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003262 if (addr + *p > frame->f_lasti)
3263 break;
3264 addr += *p++;
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003265 if (*p) *instr_lb = addr;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003266 line += *p++;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003267 --size;
3268 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003269
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003270 if (addr == frame->f_lasti) {
3271 frame->f_lineno = line;
Tim Peters8a5c3c72004-04-05 19:36:21 +00003272 result = call_trace(func, obj, frame,
Michael W. Hudson006c7522002-11-08 13:08:46 +00003273 PyTrace_LINE, Py_None);
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003274 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003275
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003276 if (size > 0) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003277 while (--size >= 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003278 addr += *p++;
3279 if (*p++)
3280 break;
3281 }
3282 *instr_ub = addr;
3283 }
3284 else {
3285 *instr_ub = INT_MAX;
3286 }
3287 }
Armin Rigobf57a142004-03-22 19:24:58 +00003288 else if (frame->f_lasti <= *instr_prev) {
3289 /* jumping back in the same line forces a trace event */
Tim Peters8a5c3c72004-04-05 19:36:21 +00003290 result = call_trace(func, obj, frame,
Armin Rigobf57a142004-03-22 19:24:58 +00003291 PyTrace_LINE, Py_None);
3292 }
3293 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003294 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003295}
3296
Fred Drake5755ce62001-06-27 19:19:46 +00003297void
3298PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003299{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003300 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003301 PyObject *temp = tstate->c_profileobj;
3302 Py_XINCREF(arg);
3303 tstate->c_profilefunc = NULL;
3304 tstate->c_profileobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003305 /* Must make sure that tracing is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003306 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003307 Py_XDECREF(temp);
3308 tstate->c_profilefunc = func;
3309 tstate->c_profileobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003310 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003311 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003312}
3313
3314void
3315PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3316{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003317 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003318 PyObject *temp = tstate->c_traceobj;
3319 Py_XINCREF(arg);
3320 tstate->c_tracefunc = NULL;
3321 tstate->c_traceobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003322 /* Must make sure that profiling is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003323 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003324 Py_XDECREF(temp);
3325 tstate->c_tracefunc = func;
3326 tstate->c_traceobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003327 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003328 tstate->use_tracing = ((func != NULL)
3329 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003330}
3331
Guido van Rossumb209a111997-04-29 18:18:01 +00003332PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003333PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003334{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003335 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003336 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003337 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003338 else
3339 return current_frame->f_builtins;
3340}
3341
Guido van Rossumb209a111997-04-29 18:18:01 +00003342PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003343PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003344{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003345 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003346 if (current_frame == NULL)
3347 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003348 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003349 return current_frame->f_locals;
3350}
3351
Guido van Rossumb209a111997-04-29 18:18:01 +00003352PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003353PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003354{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003355 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003356 if (current_frame == NULL)
3357 return NULL;
3358 else
3359 return current_frame->f_globals;
3360}
3361
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003362PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003363PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003364{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003365 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003366 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003367}
3368
Guido van Rossum6135a871995-01-09 17:53:26 +00003369int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003370PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003371{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003372 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003373 return current_frame == NULL ? 0 : current_frame->f_restricted;
3374}
3375
Guido van Rossumbe270261997-05-22 22:26:18 +00003376int
Tim Peters5ba58662001-07-16 02:29:45 +00003377PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003378{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003379 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003380 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003381
3382 if (current_frame != NULL) {
3383 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003384 const int compilerflags = codeflags & PyCF_MASK;
3385 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003386 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003387 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003388 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003389#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003390 if (codeflags & CO_GENERATOR_ALLOWED) {
3391 result = 1;
3392 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3393 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003394#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003395 }
3396 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003397}
3398
3399int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003400Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003401{
Guido van Rossumb209a111997-04-29 18:18:01 +00003402 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003403 if (f == NULL)
3404 return 0;
3405 if (!PyFile_SoftSpace(f, 0))
3406 return 0;
3407 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003408}
3409
Guido van Rossum3f5da241990-12-20 15:06:42 +00003410
Guido van Rossum681d79a1995-07-18 14:51:37 +00003411/* External interface to call any callable object.
3412 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003413
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003414#undef PyEval_CallObject
3415/* for backward compatibility: export this interface */
3416
Guido van Rossumb209a111997-04-29 18:18:01 +00003417PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003418PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003419{
Guido van Rossumb209a111997-04-29 18:18:01 +00003420 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003421}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003422#define PyEval_CallObject(func,arg) \
3423 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003424
Guido van Rossumb209a111997-04-29 18:18:01 +00003425PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003426PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003427{
Jeremy Hylton52820442001-01-03 23:52:36 +00003428 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003429
3430 if (arg == NULL)
Guido van Rossumb209a111997-04-29 18:18:01 +00003431 arg = PyTuple_New(0);
3432 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003433 PyErr_SetString(PyExc_TypeError,
3434 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003435 return NULL;
3436 }
3437 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003438 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003439
Guido van Rossumb209a111997-04-29 18:18:01 +00003440 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003441 PyErr_SetString(PyExc_TypeError,
3442 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003443 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003444 return NULL;
3445 }
3446
Tim Peters6d6c1a32001-08-02 04:15:00 +00003447 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003448 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003449 return result;
3450}
3451
Tim Peters6d6c1a32001-08-02 04:15:00 +00003452char *
3453PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003454{
3455 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003456 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003457 else if (PyFunction_Check(func))
3458 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3459 else if (PyCFunction_Check(func))
3460 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3461 else if (PyClass_Check(func))
3462 return PyString_AsString(((PyClassObject*)func)->cl_name);
3463 else if (PyInstance_Check(func)) {
3464 return PyString_AsString(
3465 ((PyInstanceObject*)func)->in_class->cl_name);
3466 } else {
3467 return func->ob_type->tp_name;
3468 }
3469}
3470
Tim Peters6d6c1a32001-08-02 04:15:00 +00003471char *
3472PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003473{
3474 if (PyMethod_Check(func))
3475 return "()";
3476 else if (PyFunction_Check(func))
3477 return "()";
3478 else if (PyCFunction_Check(func))
3479 return "()";
3480 else if (PyClass_Check(func))
3481 return " constructor";
3482 else if (PyInstance_Check(func)) {
3483 return " instance";
3484 } else {
3485 return " object";
3486 }
3487}
3488
Jeremy Hylton52820442001-01-03 23:52:36 +00003489#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
3490
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003491static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003492err_args(PyObject *func, int flags, int nargs)
3493{
3494 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003495 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003496 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003497 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003498 nargs);
3499 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003500 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003501 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003502 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003503 nargs);
3504}
3505
Nicholas Bastind858a772004-06-25 23:31:06 +00003506#define C_TRACE(call) \
3507if (tstate->use_tracing && tstate->c_profilefunc) { \
3508 if (call_trace(tstate->c_profilefunc, \
3509 tstate->c_profileobj, \
3510 tstate->frame, PyTrace_C_CALL, \
3511 func)) \
3512 { return NULL; } \
3513 call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003514 if (tstate->c_profilefunc != NULL) { \
Nicholas Bastind858a772004-06-25 23:31:06 +00003515 if (x == NULL) { \
3516 if (call_trace (tstate->c_profilefunc, \
3517 tstate->c_profileobj, \
3518 tstate->frame, PyTrace_C_EXCEPTION, \
3519 func)) \
3520 { return NULL; } \
3521 } else { \
3522 if (call_trace(tstate->c_profilefunc, \
3523 tstate->c_profileobj, \
3524 tstate->frame, PyTrace_C_RETURN, \
3525 func)) \
3526 { return NULL; } \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003527 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003528 } \
3529} else { \
3530 call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003531 }
3532
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003533static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003534call_function(PyObject ***pp_stack, int oparg
3535#ifdef WITH_TSC
3536 , uint64* pintr0, uint64* pintr1
3537#endif
3538 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003539{
3540 int na = oparg & 0xff;
3541 int nk = (oparg>>8) & 0xff;
3542 int n = na + 2 * nk;
3543 PyObject **pfunc = (*pp_stack) - n - 1;
3544 PyObject *func = *pfunc;
3545 PyObject *x, *w;
3546
Jeremy Hylton985eba52003-02-05 23:13:00 +00003547 /* Always dispatch PyCFunction first, because these are
3548 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003549 */
3550 if (PyCFunction_Check(func) && nk == 0) {
3551 int flags = PyCFunction_GET_FLAGS(func);
Nicholas Bastind858a772004-06-25 23:31:06 +00003552 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003553
3554 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003555 if (flags & (METH_NOARGS | METH_O)) {
3556 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3557 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003558 if (flags & METH_NOARGS && na == 0) {
Nicholas Bastind858a772004-06-25 23:31:06 +00003559 C_TRACE(x=(*meth)(self,NULL));
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003560 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003561 else if (flags & METH_O && na == 1) {
3562 PyObject *arg = EXT_POP(*pp_stack);
Nicholas Bastind858a772004-06-25 23:31:06 +00003563 C_TRACE(x=(*meth)(self,arg));
Jeremy Hylton192690e2002-08-16 18:36:11 +00003564 Py_DECREF(arg);
3565 }
3566 else {
3567 err_args(func, flags, na);
3568 x = NULL;
3569 }
3570 }
3571 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003572 PyObject *callargs;
3573 callargs = load_args(pp_stack, na);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003574 READ_TIMESTAMP(*pintr0);
Nicholas Bastind858a772004-06-25 23:31:06 +00003575 C_TRACE(x=PyCFunction_Call(func,callargs,NULL));
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003576 READ_TIMESTAMP(*pintr1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003577 Py_XDECREF(callargs);
3578 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003579 } else {
3580 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3581 /* optimize access to bound methods */
3582 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003583 PCALL(PCALL_METHOD);
3584 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003585 Py_INCREF(self);
3586 func = PyMethod_GET_FUNCTION(func);
3587 Py_INCREF(func);
3588 Py_DECREF(*pfunc);
3589 *pfunc = self;
3590 na++;
3591 n++;
3592 } else
3593 Py_INCREF(func);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003594 READ_TIMESTAMP(*pintr0);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003595 if (PyFunction_Check(func))
3596 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003597 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003598 x = do_call(func, pp_stack, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003599 READ_TIMESTAMP(*pintr1);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003600 Py_DECREF(func);
3601 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003602
Jeremy Hylton985eba52003-02-05 23:13:00 +00003603 /* What does this do? */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003604 while ((*pp_stack) > pfunc) {
3605 w = EXT_POP(*pp_stack);
3606 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003607 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003608 }
3609 return x;
3610}
3611
Jeremy Hylton192690e2002-08-16 18:36:11 +00003612/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003613 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003614 For the simplest case -- a function that takes only positional
3615 arguments and is called with only positional arguments -- it
3616 inlines the most primitive frame setup code from
3617 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3618 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003619*/
3620
3621static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003622fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003623{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003624 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003625 PyObject *globals = PyFunction_GET_GLOBALS(func);
3626 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3627 PyObject **d = NULL;
3628 int nd = 0;
3629
Jeremy Hylton985eba52003-02-05 23:13:00 +00003630 PCALL(PCALL_FUNCTION);
3631 PCALL(PCALL_FAST_FUNCTION);
Raymond Hettinger40174c32003-05-31 07:04:16 +00003632 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003633 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3634 PyFrameObject *f;
3635 PyObject *retval = NULL;
3636 PyThreadState *tstate = PyThreadState_GET();
3637 PyObject **fastlocals, **stack;
3638 int i;
3639
3640 PCALL(PCALL_FASTER_FUNCTION);
3641 assert(globals != NULL);
3642 /* XXX Perhaps we should create a specialized
3643 PyFrame_New() that doesn't take locals, but does
3644 take builtins without sanity checking them.
3645 */
3646 f = PyFrame_New(tstate, co, globals, NULL);
3647 if (f == NULL)
3648 return NULL;
3649
3650 fastlocals = f->f_localsplus;
3651 stack = (*pp_stack) - n;
3652
3653 for (i = 0; i < n; i++) {
3654 Py_INCREF(*stack);
3655 fastlocals[i] = *stack++;
3656 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003657 retval = PyEval_EvalFrameEx(f,0);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003658 assert(tstate != NULL);
3659 ++tstate->recursion_depth;
3660 Py_DECREF(f);
3661 --tstate->recursion_depth;
3662 return retval;
3663 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003664 if (argdefs != NULL) {
3665 d = &PyTuple_GET_ITEM(argdefs, 0);
3666 nd = ((PyTupleObject *)argdefs)->ob_size;
3667 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003668 return PyEval_EvalCodeEx(co, globals,
3669 (PyObject *)NULL, (*pp_stack)-n, na,
3670 (*pp_stack)-2*nk, nk, d, nd,
3671 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003672}
3673
3674static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003675update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3676 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003677{
3678 PyObject *kwdict = NULL;
3679 if (orig_kwdict == NULL)
3680 kwdict = PyDict_New();
3681 else {
3682 kwdict = PyDict_Copy(orig_kwdict);
3683 Py_DECREF(orig_kwdict);
3684 }
3685 if (kwdict == NULL)
3686 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003687 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003688 int err;
3689 PyObject *value = EXT_POP(*pp_stack);
3690 PyObject *key = EXT_POP(*pp_stack);
3691 if (PyDict_GetItem(kwdict, key) != NULL) {
Guido van Rossumac7be682001-01-17 15:42:30 +00003692 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +00003693 "%.200s%s got multiple values "
Jeremy Hylton512a2372001-04-11 13:52:29 +00003694 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003695 PyEval_GetFuncName(func),
3696 PyEval_GetFuncDesc(func),
Jeremy Hylton512a2372001-04-11 13:52:29 +00003697 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003698 Py_DECREF(key);
3699 Py_DECREF(value);
3700 Py_DECREF(kwdict);
3701 return NULL;
3702 }
3703 err = PyDict_SetItem(kwdict, key, value);
3704 Py_DECREF(key);
3705 Py_DECREF(value);
3706 if (err) {
3707 Py_DECREF(kwdict);
3708 return NULL;
3709 }
3710 }
3711 return kwdict;
3712}
3713
3714static PyObject *
3715update_star_args(int nstack, int nstar, PyObject *stararg,
3716 PyObject ***pp_stack)
3717{
3718 PyObject *callargs, *w;
3719
3720 callargs = PyTuple_New(nstack + nstar);
3721 if (callargs == NULL) {
3722 return NULL;
3723 }
3724 if (nstar) {
3725 int i;
3726 for (i = 0; i < nstar; i++) {
3727 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3728 Py_INCREF(a);
3729 PyTuple_SET_ITEM(callargs, nstack + i, a);
3730 }
3731 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003732 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003733 w = EXT_POP(*pp_stack);
3734 PyTuple_SET_ITEM(callargs, nstack, w);
3735 }
3736 return callargs;
3737}
3738
3739static PyObject *
3740load_args(PyObject ***pp_stack, int na)
3741{
3742 PyObject *args = PyTuple_New(na);
3743 PyObject *w;
3744
3745 if (args == NULL)
3746 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003747 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003748 w = EXT_POP(*pp_stack);
3749 PyTuple_SET_ITEM(args, na, w);
3750 }
3751 return args;
3752}
3753
3754static PyObject *
3755do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3756{
3757 PyObject *callargs = NULL;
3758 PyObject *kwdict = NULL;
3759 PyObject *result = NULL;
3760
3761 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003762 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003763 if (kwdict == NULL)
3764 goto call_fail;
3765 }
3766 callargs = load_args(pp_stack, na);
3767 if (callargs == NULL)
3768 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003769#ifdef CALL_PROFILE
3770 /* At this point, we have to look at the type of func to
3771 update the call stats properly. Do it here so as to avoid
3772 exposing the call stats machinery outside ceval.c
3773 */
3774 if (PyFunction_Check(func))
3775 PCALL(PCALL_FUNCTION);
3776 else if (PyMethod_Check(func))
3777 PCALL(PCALL_METHOD);
3778 else if (PyType_Check(func))
3779 PCALL(PCALL_TYPE);
3780 else
3781 PCALL(PCALL_OTHER);
3782#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003783 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003784 call_fail:
3785 Py_XDECREF(callargs);
3786 Py_XDECREF(kwdict);
3787 return result;
3788}
3789
3790static PyObject *
3791ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3792{
3793 int nstar = 0;
3794 PyObject *callargs = NULL;
3795 PyObject *stararg = NULL;
3796 PyObject *kwdict = NULL;
3797 PyObject *result = NULL;
3798
3799 if (flags & CALL_FLAG_KW) {
3800 kwdict = EXT_POP(*pp_stack);
3801 if (!(kwdict && PyDict_Check(kwdict))) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003802 PyErr_Format(PyExc_TypeError,
Jeremy Hylton512a2372001-04-11 13:52:29 +00003803 "%s%s argument after ** "
3804 "must be a dictionary",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003805 PyEval_GetFuncName(func),
3806 PyEval_GetFuncDesc(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003807 goto ext_call_fail;
3808 }
3809 }
3810 if (flags & CALL_FLAG_VAR) {
3811 stararg = EXT_POP(*pp_stack);
3812 if (!PyTuple_Check(stararg)) {
3813 PyObject *t = NULL;
3814 t = PySequence_Tuple(stararg);
3815 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003816 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3817 PyErr_Format(PyExc_TypeError,
3818 "%s%s argument after * "
3819 "must be a sequence",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003820 PyEval_GetFuncName(func),
3821 PyEval_GetFuncDesc(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003822 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003823 goto ext_call_fail;
3824 }
3825 Py_DECREF(stararg);
3826 stararg = t;
3827 }
3828 nstar = PyTuple_GET_SIZE(stararg);
3829 }
3830 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003831 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003832 if (kwdict == NULL)
3833 goto ext_call_fail;
3834 }
3835 callargs = update_star_args(na, nstar, stararg, pp_stack);
3836 if (callargs == NULL)
3837 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003838#ifdef CALL_PROFILE
3839 /* At this point, we have to look at the type of func to
3840 update the call stats properly. Do it here so as to avoid
3841 exposing the call stats machinery outside ceval.c
3842 */
3843 if (PyFunction_Check(func))
3844 PCALL(PCALL_FUNCTION);
3845 else if (PyMethod_Check(func))
3846 PCALL(PCALL_METHOD);
3847 else if (PyType_Check(func))
3848 PCALL(PCALL_TYPE);
3849 else
3850 PCALL(PCALL_OTHER);
3851#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003852 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003853 ext_call_fail:
3854 Py_XDECREF(callargs);
3855 Py_XDECREF(kwdict);
3856 Py_XDECREF(stararg);
3857 return result;
3858}
3859
Tim Peterscb479e72001-12-16 19:11:44 +00003860/* Extract a slice index from a PyInt or PyLong, and store in *pi.
3861 Silently reduce values larger than INT_MAX to INT_MAX, and silently
3862 boost values less than -INT_MAX to 0. Return 0 on error, 1 on success.
3863*/
Tim Petersb5196382001-12-16 19:44:20 +00003864/* Note: If v is NULL, return success without storing into *pi. This
3865 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3866 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00003867*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00003868int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003869_PyEval_SliceIndex(PyObject *v, int *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003870{
Tim Petersb5196382001-12-16 19:44:20 +00003871 if (v != NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003872 long x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003873 if (PyInt_Check(v)) {
3874 x = PyInt_AsLong(v);
3875 } else if (PyLong_Check(v)) {
3876 x = PyLong_AsLong(v);
3877 if (x==-1 && PyErr_Occurred()) {
3878 PyObject *long_zero;
Guido van Rossumac7be682001-01-17 15:42:30 +00003879 int cmp;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003880
Guido van Rossumac7be682001-01-17 15:42:30 +00003881 if (!PyErr_ExceptionMatches(
3882 PyExc_OverflowError)) {
3883 /* It's not an overflow error, so just
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003884 signal an error */
Guido van Rossum20c6add2000-05-08 14:06:50 +00003885 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003886 }
3887
Guido van Rossumac7be682001-01-17 15:42:30 +00003888 /* Clear the OverflowError */
3889 PyErr_Clear();
3890
3891 /* It's an overflow error, so we need to
3892 check the sign of the long integer,
Tim Peters8a5c3c72004-04-05 19:36:21 +00003893 set the value to INT_MAX or -INT_MAX,
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003894 and clear the error. */
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003895
3896 /* Create a long integer with a value of 0 */
Guido van Rossumac7be682001-01-17 15:42:30 +00003897 long_zero = PyLong_FromLong(0L);
Tim Peterscb479e72001-12-16 19:11:44 +00003898 if (long_zero == NULL)
3899 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003900
3901 /* Check sign */
Guido van Rossumac7be682001-01-17 15:42:30 +00003902 cmp = PyObject_RichCompareBool(v, long_zero,
3903 Py_GT);
3904 Py_DECREF(long_zero);
3905 if (cmp < 0)
3906 return 0;
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003907 else if (cmp)
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003908 x = INT_MAX;
3909 else
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003910 x = -INT_MAX;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003911 }
3912 } else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003913 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003914 "slice indices must be integers");
Guido van Rossum20c6add2000-05-08 14:06:50 +00003915 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003916 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003917 /* Truncate -- very long indices are truncated anyway */
3918 if (x > INT_MAX)
3919 x = INT_MAX;
3920 else if (x < -INT_MAX)
Michael W. Hudsoncbd6fb92002-11-06 15:17:32 +00003921 x = -INT_MAX;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003922 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003923 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00003924 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003925}
3926
Guido van Rossum50d756e2001-08-18 17:43:36 +00003927#undef ISINT
3928#define ISINT(x) ((x) == NULL || PyInt_Check(x) || PyLong_Check(x))
3929
Guido van Rossumb209a111997-04-29 18:18:01 +00003930static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003931apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003932{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003933 PyTypeObject *tp = u->ob_type;
3934 PySequenceMethods *sq = tp->tp_as_sequence;
3935
3936 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3937 int ilow = 0, ihigh = INT_MAX;
3938 if (!_PyEval_SliceIndex(v, &ilow))
3939 return NULL;
3940 if (!_PyEval_SliceIndex(w, &ihigh))
3941 return NULL;
3942 return PySequence_GetSlice(u, ilow, ihigh);
3943 }
3944 else {
3945 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00003946 if (slice != NULL) {
3947 PyObject *res = PyObject_GetItem(u, slice);
3948 Py_DECREF(slice);
3949 return res;
3950 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00003951 else
3952 return NULL;
3953 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003954}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003955
3956static int
Guido van Rossumac7be682001-01-17 15:42:30 +00003957assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
3958 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003959{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003960 PyTypeObject *tp = u->ob_type;
3961 PySequenceMethods *sq = tp->tp_as_sequence;
3962
3963 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3964 int ilow = 0, ihigh = INT_MAX;
3965 if (!_PyEval_SliceIndex(v, &ilow))
3966 return -1;
3967 if (!_PyEval_SliceIndex(w, &ihigh))
3968 return -1;
3969 if (x == NULL)
3970 return PySequence_DelSlice(u, ilow, ihigh);
3971 else
3972 return PySequence_SetSlice(u, ilow, ihigh, x);
3973 }
3974 else {
3975 PyObject *slice = PySlice_New(v, w, NULL);
3976 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00003977 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003978 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00003979 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00003980 else
Guido van Rossum354797c2001-12-03 19:45:06 +00003981 res = PyObject_DelItem(u, slice);
3982 Py_DECREF(slice);
3983 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003984 }
3985 else
3986 return -1;
3987 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003988}
3989
Guido van Rossumb209a111997-04-29 18:18:01 +00003990static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003991cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003992{
Guido van Rossumac7be682001-01-17 15:42:30 +00003993 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003994 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00003995 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003996 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003997 break;
3998 case PyCmp_IS_NOT:
3999 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004000 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004001 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004002 res = PySequence_Contains(w, v);
4003 if (res < 0)
4004 return NULL;
4005 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004006 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00004007 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004008 if (res < 0)
4009 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004010 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004011 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004012 case PyCmp_EXC_MATCH:
Barry Warsaw4249f541997-08-22 21:26:19 +00004013 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004014 break;
4015 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00004016 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004017 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004018 v = res ? Py_True : Py_False;
4019 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004020 return v;
4021}
4022
Thomas Wouters52152252000-08-17 22:55:00 +00004023static PyObject *
4024import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004025{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004026 PyObject *x;
4027
4028 x = PyObject_GetAttr(v, name);
4029 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00004030 PyErr_Format(PyExc_ImportError,
4031 "cannot import name %.230s",
4032 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004033 }
Thomas Wouters52152252000-08-17 22:55:00 +00004034 return x;
4035}
Guido van Rossumac7be682001-01-17 15:42:30 +00004036
Thomas Wouters52152252000-08-17 22:55:00 +00004037static int
4038import_all_from(PyObject *locals, PyObject *v)
4039{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004040 PyObject *all = PyObject_GetAttrString(v, "__all__");
4041 PyObject *dict, *name, *value;
4042 int skip_leading_underscores = 0;
4043 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004044
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004045 if (all == NULL) {
4046 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4047 return -1; /* Unexpected error */
4048 PyErr_Clear();
4049 dict = PyObject_GetAttrString(v, "__dict__");
4050 if (dict == NULL) {
4051 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4052 return -1;
4053 PyErr_SetString(PyExc_ImportError,
4054 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00004055 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004056 }
4057 all = PyMapping_Keys(dict);
4058 Py_DECREF(dict);
4059 if (all == NULL)
4060 return -1;
4061 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004062 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004063
4064 for (pos = 0, err = 0; ; pos++) {
4065 name = PySequence_GetItem(all, pos);
4066 if (name == NULL) {
4067 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4068 err = -1;
4069 else
4070 PyErr_Clear();
4071 break;
4072 }
4073 if (skip_leading_underscores &&
4074 PyString_Check(name) &&
4075 PyString_AS_STRING(name)[0] == '_')
4076 {
4077 Py_DECREF(name);
4078 continue;
4079 }
4080 value = PyObject_GetAttr(v, name);
4081 if (value == NULL)
4082 err = -1;
4083 else
4084 err = PyDict_SetItem(locals, name, value);
4085 Py_DECREF(name);
4086 Py_XDECREF(value);
4087 if (err != 0)
4088 break;
4089 }
4090 Py_DECREF(all);
4091 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004092}
4093
Guido van Rossumb209a111997-04-29 18:18:01 +00004094static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004095build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004096{
Guido van Rossum7851eea2001-09-12 19:19:18 +00004097 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004098
4099 if (PyDict_Check(methods))
4100 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00004101 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00004102 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004103 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4104 base = PyTuple_GET_ITEM(bases, 0);
4105 metaclass = PyObject_GetAttrString(base, "__class__");
4106 if (metaclass == NULL) {
4107 PyErr_Clear();
4108 metaclass = (PyObject *)base->ob_type;
4109 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00004110 }
4111 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004112 else {
4113 PyObject *g = PyEval_GetGlobals();
4114 if (g != NULL && PyDict_Check(g))
4115 metaclass = PyDict_GetItemString(g, "__metaclass__");
4116 if (metaclass == NULL)
4117 metaclass = (PyObject *) &PyClass_Type;
4118 Py_INCREF(metaclass);
4119 }
4120 result = PyObject_CallFunction(metaclass, "OOO", name, bases, methods);
4121 Py_DECREF(metaclass);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004122 if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
4123 /* A type error here likely means that the user passed
4124 in a base that was not a class (such the random module
4125 instead of the random.random type). Help them out with
Raymond Hettingercfc31922004-09-16 16:41:57 +00004126 by augmenting the error message with more information.*/
4127
4128 PyObject *ptype, *pvalue, *ptraceback;
4129
4130 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
4131 if (PyString_Check(pvalue)) {
4132 PyObject *newmsg;
4133 newmsg = PyString_FromFormat(
4134 "Error when calling the metaclass bases\n %s",
4135 PyString_AS_STRING(pvalue));
4136 if (newmsg != NULL) {
4137 Py_DECREF(pvalue);
4138 pvalue = newmsg;
4139 }
4140 }
4141 PyErr_Restore(ptype, pvalue, ptraceback);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004142 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004143 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00004144}
4145
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004146static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004147exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
4148 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004149{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004150 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00004151 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004152 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004153
Guido van Rossumb209a111997-04-29 18:18:01 +00004154 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
4155 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004156 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00004157 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004158 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00004159 locals = PyTuple_GetItem(prog, 2);
4160 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004161 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004162 if (globals == Py_None) {
4163 globals = PyEval_GetGlobals();
4164 if (locals == Py_None) {
4165 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00004166 plain = 1;
4167 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004168 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004169 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004170 locals = globals;
Guido van Rossumb209a111997-04-29 18:18:01 +00004171 if (!PyString_Check(prog) &&
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004172 !PyUnicode_Check(prog) &&
Guido van Rossumb209a111997-04-29 18:18:01 +00004173 !PyCode_Check(prog) &&
4174 !PyFile_Check(prog)) {
4175 PyErr_SetString(PyExc_TypeError,
Guido van Rossumac7be682001-01-17 15:42:30 +00004176 "exec: arg 1 must be a string, file, or code object");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004177 return -1;
4178 }
Fred Drake661ea262000-10-24 19:57:45 +00004179 if (!PyDict_Check(globals)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004180 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00004181 "exec: arg 2 must be a dictionary or None");
4182 return -1;
4183 }
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004184 if (!PyMapping_Check(locals)) {
Fred Drake661ea262000-10-24 19:57:45 +00004185 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004186 "exec: arg 3 must be a mapping or None");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004187 return -1;
4188 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004189 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00004190 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00004191 if (PyCode_Check(prog)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +00004192 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4193 PyErr_SetString(PyExc_TypeError,
4194 "code object passed to exec may not contain free variables");
4195 return -1;
4196 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004197 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004198 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004199 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004200 FILE *fp = PyFile_AsFile(prog);
4201 char *name = PyString_AsString(PyFile_Name(prog));
Tim Peters5ba58662001-07-16 02:29:45 +00004202 PyCompilerFlags cf;
4203 cf.cf_flags = 0;
4204 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004205 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004206 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004207 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004208 v = PyRun_File(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004209 locals);
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004210 }
4211 else {
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004212 PyObject *tmp = NULL;
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004213 char *str;
Tim Peters5ba58662001-07-16 02:29:45 +00004214 PyCompilerFlags cf;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004215 cf.cf_flags = 0;
4216#ifdef Py_USING_UNICODE
4217 if (PyUnicode_Check(prog)) {
4218 tmp = PyUnicode_AsUTF8String(prog);
4219 if (tmp == NULL)
4220 return -1;
4221 prog = tmp;
4222 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4223 }
4224#endif
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004225 if (PyString_AsStringAndSize(prog, &str, NULL))
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004226 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004227 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters8a5c3c72004-04-05 19:36:21 +00004228 v = PyRun_StringFlags(str, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004229 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004230 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004231 v = PyRun_String(str, Py_file_input, globals, locals);
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004232 Py_XDECREF(tmp);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004233 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004234 if (plain)
4235 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004236 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004237 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00004238 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004239 return 0;
4240}
Guido van Rossum24c13741995-02-14 09:42:43 +00004241
Guido van Rossumac7be682001-01-17 15:42:30 +00004242static void
Paul Prescode68140d2000-08-30 20:25:01 +00004243format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4244{
4245 char *obj_str;
4246
4247 if (!obj)
4248 return;
4249
4250 obj_str = PyString_AsString(obj);
4251 if (!obj_str)
4252 return;
4253
4254 PyErr_Format(exc, format_str, obj_str);
4255}
Guido van Rossum950361c1997-01-24 13:49:28 +00004256
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004257static PyObject *
4258string_concatenate(PyObject *v, PyObject *w,
4259 PyFrameObject *f, unsigned char *next_instr)
4260{
4261 /* This function implements 'variable += expr' when both arguments
4262 are strings. */
4263
4264 if (v->ob_refcnt == 2) {
4265 /* In the common case, there are 2 references to the value
4266 * stored in 'variable' when the += is performed: one on the
4267 * value stack (in 'v') and one still stored in the 'variable'.
4268 * We try to delete the variable now to reduce the refcnt to 1.
4269 */
4270 switch (*next_instr) {
4271 case STORE_FAST:
4272 {
4273 int oparg = PEEKARG();
4274 PyObject **fastlocals = f->f_localsplus;
4275 if (GETLOCAL(oparg) == v)
4276 SETLOCAL(oparg, NULL);
4277 break;
4278 }
4279 case STORE_DEREF:
4280 {
4281 PyObject **freevars = f->f_localsplus + f->f_nlocals;
4282 PyObject *c = freevars[PEEKARG()];
4283 if (PyCell_GET(c) == v)
4284 PyCell_Set(c, NULL);
4285 break;
4286 }
4287 case STORE_NAME:
4288 {
4289 PyObject *names = f->f_code->co_names;
4290 PyObject *name = GETITEM(names, PEEKARG());
4291 PyObject *locals = f->f_locals;
4292 if (PyDict_CheckExact(locals) &&
4293 PyDict_GetItem(locals, name) == v) {
4294 if (PyDict_DelItem(locals, name) != 0) {
4295 PyErr_Clear();
4296 }
4297 }
4298 break;
4299 }
4300 }
4301 }
4302
Armin Rigo618fbf52004-08-07 20:58:32 +00004303 if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004304 /* Now we own the last reference to 'v', so we can resize it
4305 * in-place.
4306 */
4307 int v_len = PyString_GET_SIZE(v);
4308 int w_len = PyString_GET_SIZE(w);
4309 if (_PyString_Resize(&v, v_len + w_len) != 0) {
4310 /* XXX if _PyString_Resize() fails, 'v' has been
4311 * deallocated so it cannot be put back into 'variable'.
4312 * The MemoryError is raised when there is no value in
4313 * 'variable', which might (very remotely) be a cause
4314 * of incompatibilities.
4315 */
4316 return NULL;
4317 }
4318 /* copy 'w' into the newly allocated area of 'v' */
4319 memcpy(PyString_AS_STRING(v) + v_len,
4320 PyString_AS_STRING(w), w_len);
4321 return v;
4322 }
4323 else {
4324 /* When in-place resizing is not an option. */
4325 PyString_Concat(&v, w);
4326 return v;
4327 }
4328}
4329
Guido van Rossum950361c1997-01-24 13:49:28 +00004330#ifdef DYNAMIC_EXECUTION_PROFILE
4331
Skip Montanarof118cb12001-10-15 20:51:38 +00004332static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004333getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004334{
4335 int i;
4336 PyObject *l = PyList_New(256);
4337 if (l == NULL) return NULL;
4338 for (i = 0; i < 256; i++) {
4339 PyObject *x = PyInt_FromLong(a[i]);
4340 if (x == NULL) {
4341 Py_DECREF(l);
4342 return NULL;
4343 }
4344 PyList_SetItem(l, i, x);
4345 }
4346 for (i = 0; i < 256; i++)
4347 a[i] = 0;
4348 return l;
4349}
4350
4351PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004352_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004353{
4354#ifndef DXPAIRS
4355 return getarray(dxp);
4356#else
4357 int i;
4358 PyObject *l = PyList_New(257);
4359 if (l == NULL) return NULL;
4360 for (i = 0; i < 257; i++) {
4361 PyObject *x = getarray(dxpairs[i]);
4362 if (x == NULL) {
4363 Py_DECREF(l);
4364 return NULL;
4365 }
4366 PyList_SetItem(l, i, x);
4367 }
4368 return l;
4369#endif
4370}
4371
4372#endif