blob: 80adc30ed417b6e9c3f4fa719fc8ff52a0101538 [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
Thomas Wouters477c8d52006-05-27 19:21:47 +00009/* enable more aggressive intra-module optimizations, where available */
10#define PY_LOCAL_AGGRESSIVE
11
Guido van Rossumb209a111997-04-29 18:18:01 +000012#include "Python.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000013
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000014#include "code.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000015#include "frameobject.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000016#include "eval.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000017#include "opcode.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000018#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000019
Guido van Rossumc6004111993-11-05 10:22:19 +000020#include <ctype.h>
21
Thomas Wouters477c8d52006-05-27 19:21:47 +000022#ifndef WITH_TSC
Michael W. Hudson75eabd22005-01-18 15:56:11 +000023
24#define READ_TIMESTAMP(var)
25
26#else
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000027
28typedef unsigned long long uint64;
29
Michael W. Hudson800ba232004-08-12 18:19:17 +000030#if defined(__ppc__) /* <- Don't know if this is the correct symbol; this
31 section should work for GCC on any PowerPC platform,
32 irrespective of OS. POWER? Who knows :-) */
33
Michael W. Hudson75eabd22005-01-18 15:56:11 +000034#define READ_TIMESTAMP(var) ppc_getcounter(&var)
Michael W. Hudson800ba232004-08-12 18:19:17 +000035
36static void
37ppc_getcounter(uint64 *v)
38{
39 register unsigned long tbu, tb, tbu2;
40
41 loop:
42 asm volatile ("mftbu %0" : "=r" (tbu) );
43 asm volatile ("mftb %0" : "=r" (tb) );
44 asm volatile ("mftbu %0" : "=r" (tbu2));
45 if (__builtin_expect(tbu != tbu2, 0)) goto loop;
46
Thomas Wouters477c8d52006-05-27 19:21:47 +000047 /* The slightly peculiar way of writing the next lines is
Michael W. Hudson800ba232004-08-12 18:19:17 +000048 compiled better by GCC than any other way I tried. */
49 ((long*)(v))[0] = tbu;
50 ((long*)(v))[1] = tb;
51}
52
Michael W. Hudson75eabd22005-01-18 15:56:11 +000053#else /* this is for linux/x86 (and probably any other GCC/x86 combo) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000054
Michael W. Hudson75eabd22005-01-18 15:56:11 +000055#define READ_TIMESTAMP(val) \
56 __asm__ __volatile__("rdtsc" : "=A" (val))
Michael W. Hudson800ba232004-08-12 18:19:17 +000057
58#endif
59
Thomas Wouters477c8d52006-05-27 19:21:47 +000060void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000061 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
62{
63 uint64 intr, inst, loop;
64 PyThreadState *tstate = PyThreadState_Get();
65 if (!tstate->interp->tscdump)
66 return;
67 intr = intr1 - intr0;
68 inst = inst1 - inst0 - intr;
69 loop = loop1 - loop0 - intr;
70 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
71 opcode, ticked, inst, loop);
72}
Michael W. Hudson800ba232004-08-12 18:19:17 +000073
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000074#endif
75
Guido van Rossum04691fc1992-08-12 15:35:34 +000076/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000077/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000078
Guido van Rossum408027e1996-12-30 16:17:54 +000079#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000080/* For debugging the interpreter: */
81#define LLTRACE 1 /* Low-level trace feature */
82#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000083#endif
84
Jeremy Hylton52820442001-01-03 23:52:36 +000085typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000086
Guido van Rossum374a9221991-04-04 10:40:29 +000087/* Forward declarations */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000088#ifdef WITH_TSC
Thomas Wouters477c8d52006-05-27 19:21:47 +000089static PyObject * call_function(PyObject ***, int, uint64*, uint64*);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000090#else
Thomas Wouters477c8d52006-05-27 19:21:47 +000091static PyObject * call_function(PyObject ***, int);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000092#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000093static PyObject * fast_function(PyObject *, PyObject ***, int, int, int);
94static PyObject * do_call(PyObject *, PyObject ***, int, int);
95static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int);
96static PyObject * update_keyword_args(PyObject *, int, PyObject ***,PyObject *);
97static PyObject * update_star_args(int, int, PyObject *, PyObject ***);
98static PyObject * load_args(PyObject ***, int);
Jeremy Hylton52820442001-01-03 23:52:36 +000099#define CALL_FLAG_VAR 1
100#define CALL_FLAG_KW 2
101
Guido van Rossum0a066c01992-03-27 17:29:15 +0000102#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +0000103static int lltrace;
Tim Petersdbd9ba62000-07-09 03:09:57 +0000104static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +0000105#endif
Fred Drake5755ce62001-06-27 19:19:46 +0000106static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
107 int, PyObject *);
Fred Drake4ec5d562001-10-04 19:26:43 +0000108static void call_trace_protected(Py_tracefunc, PyObject *,
Armin Rigo1c2d7e52005-09-20 18:34:01 +0000109 PyFrameObject *, int, PyObject *);
Fred Drake5755ce62001-06-27 19:19:46 +0000110static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +0000111static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Armin Rigobf57a142004-03-22 19:24:58 +0000112 PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000113
Thomas Wouters477c8d52006-05-27 19:21:47 +0000114static PyObject * apply_slice(PyObject *, PyObject *, PyObject *);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000115static int assign_slice(PyObject *, PyObject *,
116 PyObject *, PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000117static PyObject * cmp_outcome(int, PyObject *, PyObject *);
118static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +0000119static int import_all_from(PyObject *, PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000120static PyObject * build_class(PyObject *, PyObject *, PyObject *);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000121static int exec_statement(PyFrameObject *,
122 PyObject *, PyObject *, PyObject *);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000123static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *);
124static void reset_exc_info(PyThreadState *);
Paul Prescode68140d2000-08-30 20:25:01 +0000125static void format_exc_check_arg(PyObject *, char *, PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000126static PyObject * string_concatenate(PyObject *, PyObject *,
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000127 PyFrameObject *, unsigned char *);
Guido van Rossum374a9221991-04-04 10:40:29 +0000128
Paul Prescode68140d2000-08-30 20:25:01 +0000129#define NAME_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000130 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000131#define GLOBAL_NAME_ERROR_MSG \
132 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000133#define UNBOUNDLOCAL_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000134 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000135#define UNBOUNDFREE_ERROR_MSG \
136 "free variable '%.200s' referenced before assignment" \
137 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000138
Guido van Rossum950361c1997-01-24 13:49:28 +0000139/* Dynamic execution profile */
140#ifdef DYNAMIC_EXECUTION_PROFILE
141#ifdef DXPAIRS
142static long dxpairs[257][256];
143#define dxp dxpairs[256]
144#else
145static long dxp[256];
146#endif
147#endif
148
Jeremy Hylton985eba52003-02-05 23:13:00 +0000149/* Function call profile */
150#ifdef CALL_PROFILE
151#define PCALL_NUM 11
152static int pcall[PCALL_NUM];
153
154#define PCALL_ALL 0
155#define PCALL_FUNCTION 1
156#define PCALL_FAST_FUNCTION 2
157#define PCALL_FASTER_FUNCTION 3
158#define PCALL_METHOD 4
159#define PCALL_BOUND_METHOD 5
160#define PCALL_CFUNCTION 6
161#define PCALL_TYPE 7
162#define PCALL_GENERATOR 8
163#define PCALL_OTHER 9
164#define PCALL_POP 10
165
166/* Notes about the statistics
167
168 PCALL_FAST stats
169
170 FAST_FUNCTION means no argument tuple needs to be created.
171 FASTER_FUNCTION means that the fast-path frame setup code is used.
172
173 If there is a method call where the call can be optimized by changing
174 the argument tuple and calling the function directly, it gets recorded
175 twice.
176
177 As a result, the relationship among the statistics appears to be
178 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
179 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
180 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
181 PCALL_METHOD > PCALL_BOUND_METHOD
182*/
183
184#define PCALL(POS) pcall[POS]++
185
186PyObject *
187PyEval_GetCallStats(PyObject *self)
188{
Tim Peters8a5c3c72004-04-05 19:36:21 +0000189 return Py_BuildValue("iiiiiiiiii",
Jeremy Hylton985eba52003-02-05 23:13:00 +0000190 pcall[0], pcall[1], pcall[2], pcall[3],
191 pcall[4], pcall[5], pcall[6], pcall[7],
192 pcall[8], pcall[9]);
193}
194#else
195#define PCALL(O)
196
197PyObject *
198PyEval_GetCallStats(PyObject *self)
199{
200 Py_INCREF(Py_None);
201 return Py_None;
202}
203#endif
204
Tim Peters5ca576e2001-06-18 22:08:13 +0000205
Guido van Rossume59214e1994-08-30 08:01:59 +0000206#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000207
Guido van Rossum2571cc81999-04-07 16:07:23 +0000208#ifndef DONT_HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000209#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000210#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000211#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000212
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000213static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
Guido van Rossuma9672091994-09-14 13:31:22 +0000214static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000215
Tim Peters7f468f22004-10-11 02:40:51 +0000216int
217PyEval_ThreadsInitialized(void)
218{
219 return interpreter_lock != 0;
220}
221
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000222void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000223PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000224{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000225 if (interpreter_lock)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000226 return;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000227 interpreter_lock = PyThread_allocate_lock();
228 PyThread_acquire_lock(interpreter_lock, 1);
229 main_thread = PyThread_get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000230}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000231
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000232void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000233PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000234{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000235 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000236}
237
238void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000239PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000240{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000241 PyThread_release_lock(interpreter_lock);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000242}
243
244void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000245PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000246{
247 if (tstate == NULL)
248 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000249 /* Check someone has called PyEval_InitThreads() to create the lock */
250 assert(interpreter_lock);
Guido van Rossum65d5b571998-12-21 19:32:43 +0000251 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000252 if (PyThreadState_Swap(tstate) != NULL)
253 Py_FatalError(
254 "PyEval_AcquireThread: non-NULL old thread state");
255}
256
257void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000258PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000259{
260 if (tstate == NULL)
261 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
262 if (PyThreadState_Swap(NULL) != tstate)
263 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000264 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000265}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000266
267/* This function is called from PyOS_AfterFork to ensure that newly
268 created child processes don't hold locks referring to threads which
269 are not running in the child process. (This could also be done using
270 pthread_atfork mechanism, at least for the pthreads implementation.) */
271
272void
273PyEval_ReInitThreads(void)
274{
275 if (!interpreter_lock)
276 return;
277 /*XXX Can't use PyThread_free_lock here because it does too
278 much error-checking. Doing this cleanly would require
279 adding a new function to each thread_*.h. Instead, just
280 create a new lock and waste a little bit of memory */
281 interpreter_lock = PyThread_allocate_lock();
282 PyThread_acquire_lock(interpreter_lock, 1);
283 main_thread = PyThread_get_thread_ident();
284}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000285#endif
286
Guido van Rossumff4949e1992-08-05 19:58:53 +0000287/* Functions save_thread and restore_thread are always defined so
288 dynamically loaded modules needn't be compiled separately for use
289 with and without threads: */
290
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000291PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000292PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000293{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000294 PyThreadState *tstate = PyThreadState_Swap(NULL);
295 if (tstate == NULL)
296 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000297#ifdef WITH_THREAD
Guido van Rossumb74eca91997-09-30 22:03:16 +0000298 if (interpreter_lock)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000299 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000300#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000301 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000302}
303
304void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000305PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000306{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000307 if (tstate == NULL)
308 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000309#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000310 if (interpreter_lock) {
Guido van Rossumb74eca91997-09-30 22:03:16 +0000311 int err = errno;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000312 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000313 errno = err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000314 }
315#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000316 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000317}
318
319
Guido van Rossuma9672091994-09-14 13:31:22 +0000320/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
321 signal handlers or Mac I/O completion routines) can schedule calls
322 to a function to be called synchronously.
323 The synchronous function is called with one void* argument.
324 It should return 0 for success or -1 for failure -- failure should
325 be accompanied by an exception.
326
327 If registry succeeds, the registry function returns 0; if it fails
328 (e.g. due to too many pending calls) it returns -1 (without setting
329 an exception condition).
330
331 Note that because registry may occur from within signal handlers,
332 or other asynchronous events, calling malloc() is unsafe!
333
334#ifdef WITH_THREAD
335 Any thread can schedule pending calls, but only the main thread
336 will execute them.
337#endif
338
339 XXX WARNING! ASYNCHRONOUSLY EXECUTING CODE!
340 There are two possible race conditions:
341 (1) nested asynchronous registry calls;
342 (2) registry calls made while pending calls are being processed.
343 While (1) is very unlikely, (2) is a real possibility.
344 The current code is safe against (2), but not against (1).
345 The safety against (2) is derived from the fact that only one
346 thread (the main thread) ever takes things out of the queue.
Guido van Rossuma9672091994-09-14 13:31:22 +0000347
Guido van Rossuma027efa1997-05-05 20:56:21 +0000348 XXX Darn! With the advent of thread state, we should have an array
349 of pending calls per thread in the thread state! Later...
350*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000351
Guido van Rossuma9672091994-09-14 13:31:22 +0000352#define NPENDINGCALLS 32
353static struct {
Thomas Wouters334fb892000-07-25 12:56:38 +0000354 int (*func)(void *);
355 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000356} pendingcalls[NPENDINGCALLS];
357static volatile int pendingfirst = 0;
358static volatile int pendinglast = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000359static volatile int things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000360
361int
Thomas Wouters334fb892000-07-25 12:56:38 +0000362Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000363{
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000364 static volatile int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000365 int i, j;
366 /* XXX Begin critical section */
367 /* XXX If you want this to be safe against nested
368 XXX asynchronous calls, you'll have to work harder! */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000369 if (busy)
370 return -1;
371 busy = 1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000372 i = pendinglast;
373 j = (i + 1) % NPENDINGCALLS;
Guido van Rossum04e70322002-07-17 16:57:13 +0000374 if (j == pendingfirst) {
375 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000376 return -1; /* Queue full */
Guido van Rossum04e70322002-07-17 16:57:13 +0000377 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000378 pendingcalls[i].func = func;
379 pendingcalls[i].arg = arg;
380 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000381
382 _Py_Ticker = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000383 things_to_do = 1; /* Signal main loop */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000384 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000385 /* XXX End critical section */
386 return 0;
387}
388
Guido van Rossum180d7b41994-09-29 09:45:57 +0000389int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000390Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000391{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000392 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000393#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000394 if (main_thread && PyThread_get_thread_ident() != main_thread)
Guido van Rossuma9672091994-09-14 13:31:22 +0000395 return 0;
396#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000397 if (busy)
Guido van Rossum180d7b41994-09-29 09:45:57 +0000398 return 0;
399 busy = 1;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000400 things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000401 for (;;) {
402 int i;
Thomas Wouters334fb892000-07-25 12:56:38 +0000403 int (*func)(void *);
404 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000405 i = pendingfirst;
406 if (i == pendinglast)
407 break; /* Queue empty */
408 func = pendingcalls[i].func;
409 arg = pendingcalls[i].arg;
410 pendingfirst = (i + 1) % NPENDINGCALLS;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000411 if (func(arg) < 0) {
412 busy = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000413 things_to_do = 1; /* We're not done yet */
Guido van Rossuma9672091994-09-14 13:31:22 +0000414 return -1;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000415 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000416 }
Guido van Rossum180d7b41994-09-29 09:45:57 +0000417 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000418 return 0;
419}
420
421
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000422/* The interpreter's recursion limit */
423
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000424#ifndef Py_DEFAULT_RECURSION_LIMIT
425#define Py_DEFAULT_RECURSION_LIMIT 1000
426#endif
427static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
428int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000429
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000430int
431Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000432{
433 return recursion_limit;
434}
435
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000436void
437Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000438{
439 recursion_limit = new_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000440 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000441}
442
Armin Rigo2b3eb402003-10-28 12:05:48 +0000443/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
444 if the recursion_depth reaches _Py_CheckRecursionLimit.
445 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
446 to guarantee that _Py_CheckRecursiveCall() is regularly called.
447 Without USE_STACKCHECK, there is no need for this. */
448int
449_Py_CheckRecursiveCall(char *where)
450{
451 PyThreadState *tstate = PyThreadState_GET();
452
453#ifdef USE_STACKCHECK
454 if (PyOS_CheckStack()) {
455 --tstate->recursion_depth;
456 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
457 return -1;
458 }
459#endif
460 if (tstate->recursion_depth > recursion_limit) {
461 --tstate->recursion_depth;
462 PyErr_Format(PyExc_RuntimeError,
463 "maximum recursion depth exceeded%s",
464 where);
465 return -1;
466 }
467 _Py_CheckRecursionLimit = recursion_limit;
468 return 0;
469}
470
Guido van Rossum374a9221991-04-04 10:40:29 +0000471/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000472enum why_code {
473 WHY_NOT = 0x0001, /* No error */
474 WHY_EXCEPTION = 0x0002, /* Exception occurred */
475 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
476 WHY_RETURN = 0x0008, /* 'return' statement */
477 WHY_BREAK = 0x0010, /* 'break' statement */
478 WHY_CONTINUE = 0x0020, /* 'continue' statement */
479 WHY_YIELD = 0x0040 /* 'yield' operator */
480};
Guido van Rossum374a9221991-04-04 10:40:29 +0000481
Raymond Hettinger7c958652004-04-06 10:11:10 +0000482static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
Tim Petersd6d010b2001-06-21 02:49:55 +0000483static int unpack_iterable(PyObject *, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000484
Skip Montanarod581d772002-09-03 20:10:45 +0000485/* for manipulating the thread switch and periodic "stuff" - used to be
486 per thread, now just a pair o' globals */
Skip Montanaro99dba272002-09-03 20:19:06 +0000487int _Py_CheckInterval = 100;
488volatile int _Py_Ticker = 100;
Guido van Rossum374a9221991-04-04 10:40:29 +0000489
Guido van Rossumb209a111997-04-29 18:18:01 +0000490PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000491PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000492{
Jeremy Hylton985eba52003-02-05 23:13:00 +0000493 /* XXX raise SystemError if globals is NULL */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000494 return PyEval_EvalCodeEx(co,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000495 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000496 (PyObject **)NULL, 0,
497 (PyObject **)NULL, 0,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000498 (PyObject **)NULL, 0,
499 NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000500}
501
502
503/* Interpreter main loop */
504
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000505PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000506PyEval_EvalFrame(PyFrameObject *f) {
507 /* This is for backward compatibility with extension modules that
508 used this API; core interpreter code should call PyEval_EvalFrameEx() */
509 return PyEval_EvalFrameEx(f, 0);
510}
511
512PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000513PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000514{
Guido van Rossum950361c1997-01-24 13:49:28 +0000515#ifdef DXPAIRS
516 int lastopcode = 0;
517#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +0000518 register PyObject **stack_pointer; /* Next free slot in value stack */
Guido van Rossum374a9221991-04-04 10:40:29 +0000519 register unsigned char *next_instr;
Armin Rigo8817fcd2004-06-17 10:22:40 +0000520 register int opcode; /* Current opcode */
521 register int oparg; /* Current opcode argument, if any */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000522 register enum why_code why; /* Reason for block stack unwind */
Guido van Rossum374a9221991-04-04 10:40:29 +0000523 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000524 register PyObject *x; /* Result object -- NULL if error */
525 register PyObject *v; /* Temporary objects popped off stack */
526 register PyObject *w;
527 register PyObject *u;
528 register PyObject *t;
Barry Warsaw23c9ec82000-08-21 15:44:01 +0000529 register PyObject *stream = NULL; /* for PRINT opcodes */
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000530 register PyObject **fastlocals, **freevars;
Guido van Rossum014518f1998-11-23 21:09:51 +0000531 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000532 PyThreadState *tstate = PyThreadState_GET();
Tim Peters5ca576e2001-06-18 22:08:13 +0000533 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000534
Tim Peters8a5c3c72004-04-05 19:36:21 +0000535 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000536
537 not (instr_lb <= current_bytecode_offset < instr_ub)
538
Tim Peters8a5c3c72004-04-05 19:36:21 +0000539 is true when the line being executed has changed. The
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000540 initial values are such as to make this false the first
541 time it is tested. */
Armin Rigobf57a142004-03-22 19:24:58 +0000542 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000543
Guido van Rossumd076c731998-10-07 19:42:25 +0000544 unsigned char *first_instr;
Skip Montanaro04d80f82002-08-04 21:03:35 +0000545 PyObject *names;
546 PyObject *consts;
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000547#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000548 /* Make it easier to find out where we are with a debugger */
Tim Peters5ca576e2001-06-18 22:08:13 +0000549 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000550#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000551
Neal Norwitza81d2202002-07-14 00:27:26 +0000552/* Tuple access macros */
553
554#ifndef Py_DEBUG
555#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
556#else
557#define GETITEM(v, i) PyTuple_GetItem((v), (i))
558#endif
559
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000560#ifdef WITH_TSC
561/* Use Pentium timestamp counter to mark certain events:
562 inst0 -- beginning of switch statement for opcode dispatch
563 inst1 -- end of switch statement (may be skipped)
564 loop0 -- the top of the mainloop
Thomas Wouters477c8d52006-05-27 19:21:47 +0000565 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000566 (may be skipped)
567 intr1 -- beginning of long interruption
568 intr2 -- end of long interruption
569
570 Many opcodes call out to helper C functions. In some cases, the
571 time in those functions should be counted towards the time for the
572 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
573 calls another Python function; there's no point in charge all the
574 bytecode executed by the called function to the caller.
575
576 It's hard to make a useful judgement statically. In the presence
577 of operator overloading, it's impossible to tell if a call will
578 execute new Python code or not.
579
580 It's a case-by-case judgement. I'll use intr1 for the following
581 cases:
582
583 EXEC_STMT
584 IMPORT_STAR
585 IMPORT_FROM
586 CALL_FUNCTION (and friends)
587
588 */
589 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
590 int ticked = 0;
591
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000592 READ_TIMESTAMP(inst0);
593 READ_TIMESTAMP(inst1);
594 READ_TIMESTAMP(loop0);
595 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000596
597 /* shut up the compiler */
598 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000599#endif
600
Guido van Rossum374a9221991-04-04 10:40:29 +0000601/* Code access macros */
602
Martin v. Löwis18e16552006-02-15 17:27:45 +0000603#define INSTR_OFFSET() ((int)(next_instr - first_instr))
Guido van Rossum374a9221991-04-04 10:40:29 +0000604#define NEXTOP() (*next_instr++)
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000605#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000606#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
Guido van Rossumd076c731998-10-07 19:42:25 +0000607#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000608#define JUMPBY(x) (next_instr += (x))
609
Raymond Hettingerf606f872003-03-16 03:11:04 +0000610/* OpCode prediction macros
611 Some opcodes tend to come in pairs thus making it possible to predict
612 the second code when the first is run. For example, COMPARE_OP is often
613 followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And, those opcodes are often
614 followed by a POP_TOP.
615
616 Verifying the prediction costs a single high-speed test of register
Raymond Hettingerac2072922003-03-16 15:41:11 +0000617 variable against a constant. If the pairing was good, then the
Raymond Hettingerf606f872003-03-16 03:11:04 +0000618 processor has a high likelihood of making its own successful branch
619 prediction which results in a nearly zero overhead transition to the
620 next opcode.
621
622 A successful prediction saves a trip through the eval-loop including
623 its two unpredictable branches, the HASARG test and the switch-case.
Raymond Hettingera7216982004-02-08 19:59:27 +0000624
Tim Peters8a5c3c72004-04-05 19:36:21 +0000625 If collecting opcode statistics, turn off prediction so that
626 statistics are accurately maintained (the predictions bypass
Raymond Hettingera7216982004-02-08 19:59:27 +0000627 the opcode frequency counter updates).
Raymond Hettingerf606f872003-03-16 03:11:04 +0000628*/
629
Raymond Hettingera7216982004-02-08 19:59:27 +0000630#ifdef DYNAMIC_EXECUTION_PROFILE
631#define PREDICT(op) if (0) goto PRED_##op
632#else
Raymond Hettingerac2072922003-03-16 15:41:11 +0000633#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000634#endif
635
Raymond Hettingerf606f872003-03-16 03:11:04 +0000636#define PREDICTED(op) PRED_##op: next_instr++
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000637#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Raymond Hettingerf606f872003-03-16 03:11:04 +0000638
Guido van Rossum374a9221991-04-04 10:40:29 +0000639/* Stack manipulation macros */
640
Martin v. Löwis18e16552006-02-15 17:27:45 +0000641/* The stack can grow at most MAXINT deep, as co_nlocals and
642 co_stacksize are ints. */
643#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
Guido van Rossum374a9221991-04-04 10:40:29 +0000644#define EMPTY() (STACK_LEVEL() == 0)
645#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000646#define SECOND() (stack_pointer[-2])
647#define THIRD() (stack_pointer[-3])
648#define FOURTH() (stack_pointer[-4])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000649#define SET_TOP(v) (stack_pointer[-1] = (v))
650#define SET_SECOND(v) (stack_pointer[-2] = (v))
651#define SET_THIRD(v) (stack_pointer[-3] = (v))
652#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000653#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000654#define BASIC_PUSH(v) (*stack_pointer++ = (v))
655#define BASIC_POP() (*--stack_pointer)
656
Guido van Rossum96a42c81992-01-12 02:29:51 +0000657#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000658#define PUSH(v) { (void)(BASIC_PUSH(v), \
659 lltrace && prtrace(TOP(), "push")); \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000660 assert(STACK_LEVEL() <= co->co_stacksize); }
Fred Drakede26cfc2001-10-13 06:11:28 +0000661#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000662#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
663 lltrace && prtrace(TOP(), "stackadj")); \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000664 assert(STACK_LEVEL() <= co->co_stacksize); }
Guido van Rossumc2e20742006-02-27 22:32:47 +0000665#define EXT_POP(STACK_POINTER) (lltrace && prtrace(*(STACK_POINTER), "ext_pop"), *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000666#else
667#define PUSH(v) BASIC_PUSH(v)
668#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000669#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000670#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000671#endif
672
Guido van Rossum681d79a1995-07-18 14:51:37 +0000673/* Local variable macros */
674
675#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000676
677/* The SETLOCAL() macro must not DECREF the local variable in-place and
678 then store the new value; it must copy the old value to a temporary
679 value, then store the new value, and then DECREF the temporary value.
680 This is because it is possible that during the DECREF the frame is
681 accessed by other code (e.g. a __del__ method or gc.collect()) and the
682 variable would be pointing to already-freed memory. */
683#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
684 GETLOCAL(i) = value; \
685 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000686
Guido van Rossuma027efa1997-05-05 20:56:21 +0000687/* Start of code */
688
Tim Peters5ca576e2001-06-18 22:08:13 +0000689 if (f == NULL)
690 return NULL;
691
Armin Rigo1d313ab2003-10-25 14:33:09 +0000692 /* push frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000693 if (Py_EnterRecursiveCall(""))
Armin Rigo1d313ab2003-10-25 14:33:09 +0000694 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000695
Tim Peters5ca576e2001-06-18 22:08:13 +0000696 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000697
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000698 if (tstate->use_tracing) {
699 if (tstate->c_tracefunc != NULL) {
700 /* tstate->c_tracefunc, if defined, is a
701 function that will be called on *every* entry
702 to a code block. Its return value, if not
703 None, is a function that will be called at
704 the start of each executed line of code.
705 (Actually, the function must return itself
706 in order to continue tracing.) The trace
707 functions are called with three arguments:
708 a pointer to the current frame, a string
709 indicating why the function is called, and
710 an argument which depends on the situation.
711 The global trace function is also called
712 whenever an exception is detected. */
713 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
714 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000715 /* Trace function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000716 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000717 }
718 }
719 if (tstate->c_profilefunc != NULL) {
720 /* Similar for c_profilefunc, except it needn't
721 return itself and isn't called for "line" events */
722 if (call_trace(tstate->c_profilefunc,
723 tstate->c_profileobj,
724 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000725 /* Profile function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000726 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000727 }
728 }
729 }
730
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000731 co = f->f_code;
732 names = co->co_names;
733 consts = co->co_consts;
734 fastlocals = f->f_localsplus;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000735 freevars = f->f_localsplus + co->co_nlocals;
Brett Cannonc9371d42005-06-25 08:23:41 +0000736 first_instr = (unsigned char*) PyString_AS_STRING(co->co_code);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000737 /* An explanation is in order for the next line.
738
739 f->f_lasti now refers to the index of the last instruction
740 executed. You might think this was obvious from the name, but
741 this wasn't always true before 2.3! PyFrame_New now sets
742 f->f_lasti to -1 (i.e. the index *before* the first instruction)
743 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
744 does work. Promise. */
745 next_instr = first_instr + f->f_lasti + 1;
746 stack_pointer = f->f_stacktop;
747 assert(stack_pointer != NULL);
748 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
749
Tim Peters5ca576e2001-06-18 22:08:13 +0000750#ifdef LLTRACE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000751 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +0000752#endif
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000753#if defined(Py_DEBUG) || defined(LLTRACE)
Tim Peters5ca576e2001-06-18 22:08:13 +0000754 filename = PyString_AsString(co->co_filename);
755#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000756
Guido van Rossum374a9221991-04-04 10:40:29 +0000757 why = WHY_NOT;
758 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +0000759 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +0000760 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +0000761
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000762 if (throwflag) { /* support for generator.throw() */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000763 why = WHY_EXCEPTION;
764 goto on_error;
765 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000766
Guido van Rossum374a9221991-04-04 10:40:29 +0000767 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000768#ifdef WITH_TSC
769 if (inst1 == 0) {
770 /* Almost surely, the opcode executed a break
771 or a continue, preventing inst1 from being set
772 on the way out of the loop.
773 */
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000774 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000775 loop1 = inst1;
776 }
777 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
778 intr0, intr1);
779 ticked = 0;
780 inst1 = 0;
781 intr0 = 0;
782 intr1 = 0;
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000783 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000784#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000785 assert(stack_pointer >= f->f_valuestack); /* else underflow */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000786 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000787
Guido van Rossuma027efa1997-05-05 20:56:21 +0000788 /* Do periodic things. Doing this every time through
789 the loop would add too much overhead, so we do it
790 only every Nth instruction. We also do it if
791 ``things_to_do'' is set, i.e. when an asynchronous
792 event needs attention (e.g. a signal handler or
793 async I/O handler); see Py_AddPendingCall() and
794 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000795
Skip Montanarod581d772002-09-03 20:10:45 +0000796 if (--_Py_Ticker < 0) {
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000797 if (*next_instr == SETUP_FINALLY) {
798 /* Make the last opcode before
799 a try: finally: block uninterruptable. */
800 goto fast_next_opcode;
801 }
Skip Montanarod581d772002-09-03 20:10:45 +0000802 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000803 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000804#ifdef WITH_TSC
805 ticked = 1;
806#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000807 if (things_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +0000808 if (Py_MakePendingCalls() < 0) {
809 why = WHY_EXCEPTION;
810 goto on_error;
811 }
Kurt B. Kaiser4c79a832004-11-23 18:06:08 +0000812 if (things_to_do)
813 /* MakePendingCalls() didn't succeed.
814 Force early re-execution of this
815 "periodic" code, possibly after
816 a thread switch */
817 _Py_Ticker = 0;
Guido van Rossum8861b741996-07-30 16:49:37 +0000818 }
Guido van Rossume59214e1994-08-30 08:01:59 +0000819#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000820 if (interpreter_lock) {
821 /* Give another thread a chance */
822
Guido van Rossum25ce5661997-08-02 03:10:38 +0000823 if (PyThreadState_Swap(NULL) != tstate)
824 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000825 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000826
827 /* Other threads may run now */
828
Guido van Rossum65d5b571998-12-21 19:32:43 +0000829 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000830 if (PyThreadState_Swap(tstate) != NULL)
831 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000832
833 /* Check for thread interrupts */
834
835 if (tstate->async_exc != NULL) {
836 x = tstate->async_exc;
837 tstate->async_exc = NULL;
838 PyErr_SetNone(x);
839 Py_DECREF(x);
840 why = WHY_EXCEPTION;
841 goto on_error;
842 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000843 }
844#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000845 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000846
Neil Schemenauer63543862002-02-17 19:10:14 +0000847 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +0000848 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +0000849
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000850 /* line-by-line tracing support */
851
852 if (tstate->c_tracefunc != NULL && !tstate->tracing) {
853 /* see maybe_call_line_trace
854 for expository comments */
855 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +0000856
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000857 err = maybe_call_line_trace(tstate->c_tracefunc,
858 tstate->c_traceobj,
Armin Rigobf57a142004-03-22 19:24:58 +0000859 f, &instr_lb, &instr_ub,
860 &instr_prev);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000861 /* Reload possibly changed frame fields */
862 JUMPTO(f->f_lasti);
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000863 if (f->f_stacktop != NULL) {
864 stack_pointer = f->f_stacktop;
865 f->f_stacktop = NULL;
866 }
867 if (err) {
868 /* trace function raised an exception */
869 goto on_error;
870 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000871 }
872
873 /* Extract opcode and argument */
874
Guido van Rossum374a9221991-04-04 10:40:29 +0000875 opcode = NEXTOP();
Armin Rigo8817fcd2004-06-17 10:22:40 +0000876 oparg = 0; /* allows oparg to be stored in a register because
877 it doesn't have to be remembered across a full loop */
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000878 if (HAS_ARG(opcode))
879 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +0000880 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +0000881#ifdef DYNAMIC_EXECUTION_PROFILE
882#ifdef DXPAIRS
883 dxpairs[lastopcode][opcode]++;
884 lastopcode = opcode;
885#endif
886 dxp[opcode]++;
887#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000888
Guido van Rossum96a42c81992-01-12 02:29:51 +0000889#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +0000890 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +0000891
Guido van Rossum96a42c81992-01-12 02:29:51 +0000892 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000893 if (HAS_ARG(opcode)) {
894 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000895 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +0000896 }
897 else {
898 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000899 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +0000900 }
901 }
902#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000903
Guido van Rossum374a9221991-04-04 10:40:29 +0000904 /* Main switch on opcode */
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000905 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +0000906
Guido van Rossum374a9221991-04-04 10:40:29 +0000907 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +0000908
Guido van Rossum374a9221991-04-04 10:40:29 +0000909 /* BEWARE!
910 It is essential that any operation that fails sets either
911 x to NULL, err to nonzero, or why to anything but WHY_NOT,
912 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000913
Guido van Rossum374a9221991-04-04 10:40:29 +0000914 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000915
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000916 case NOP:
917 goto fast_next_opcode;
918
Neil Schemenauer63543862002-02-17 19:10:14 +0000919 case LOAD_FAST:
920 x = GETLOCAL(oparg);
921 if (x != NULL) {
922 Py_INCREF(x);
923 PUSH(x);
924 goto fast_next_opcode;
925 }
926 format_exc_check_arg(PyExc_UnboundLocalError,
927 UNBOUNDLOCAL_ERROR_MSG,
928 PyTuple_GetItem(co->co_varnames, oparg));
929 break;
930
931 case LOAD_CONST:
Skip Montanaro04d80f82002-08-04 21:03:35 +0000932 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +0000933 Py_INCREF(x);
934 PUSH(x);
935 goto fast_next_opcode;
936
Raymond Hettinger7dc52212003-03-16 20:14:44 +0000937 PREDICTED_WITH_ARG(STORE_FAST);
Neil Schemenauer63543862002-02-17 19:10:14 +0000938 case STORE_FAST:
939 v = POP();
940 SETLOCAL(oparg, v);
941 goto fast_next_opcode;
942
Raymond Hettingerf606f872003-03-16 03:11:04 +0000943 PREDICTED(POP_TOP);
Guido van Rossum374a9221991-04-04 10:40:29 +0000944 case POP_TOP:
945 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000946 Py_DECREF(v);
Neil Schemenauer63543862002-02-17 19:10:14 +0000947 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000948
Guido van Rossum374a9221991-04-04 10:40:29 +0000949 case ROT_TWO:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000950 v = TOP();
951 w = SECOND();
952 SET_TOP(w);
953 SET_SECOND(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000954 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000955
Guido van Rossum374a9221991-04-04 10:40:29 +0000956 case ROT_THREE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000957 v = TOP();
958 w = SECOND();
959 x = THIRD();
960 SET_TOP(w);
961 SET_SECOND(x);
962 SET_THIRD(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000963 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000964
Thomas Wouters434d0822000-08-24 20:11:32 +0000965 case ROT_FOUR:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000966 u = TOP();
967 v = SECOND();
968 w = THIRD();
969 x = FOURTH();
970 SET_TOP(v);
971 SET_SECOND(w);
972 SET_THIRD(x);
973 SET_FOURTH(u);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000974 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000975
Guido van Rossum374a9221991-04-04 10:40:29 +0000976 case DUP_TOP:
977 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000978 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000979 PUSH(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000980 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000981
Thomas Wouters434d0822000-08-24 20:11:32 +0000982 case DUP_TOPX:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000983 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000984 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000985 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000986 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000987 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000988 STACKADJ(2);
989 SET_TOP(x);
990 SET_SECOND(w);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000991 goto fast_next_opcode;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000992 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000993 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000994 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000995 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000996 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000997 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +0000998 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000999 STACKADJ(3);
1000 SET_TOP(x);
1001 SET_SECOND(w);
1002 SET_THIRD(v);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001003 goto fast_next_opcode;
Thomas Wouters434d0822000-08-24 20:11:32 +00001004 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001005 Py_FatalError("invalid argument to DUP_TOPX"
1006 " (bytecode corruption?)");
Tim Peters35ba6892000-10-11 07:04:49 +00001007 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001008
Guido van Rossum374a9221991-04-04 10:40:29 +00001009 case UNARY_POSITIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001010 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001011 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001012 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001013 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001014 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001015 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001016
Guido van Rossum374a9221991-04-04 10:40:29 +00001017 case UNARY_NEGATIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001018 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001019 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001020 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001021 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001022 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001023 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001024
Guido van Rossum374a9221991-04-04 10:40:29 +00001025 case UNARY_NOT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001026 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001027 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001028 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +00001029 if (err == 0) {
1030 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001031 SET_TOP(Py_True);
Guido van Rossumfc490731997-05-06 15:06:49 +00001032 continue;
1033 }
1034 else if (err > 0) {
1035 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001036 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +00001037 err = 0;
1038 continue;
1039 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00001040 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001041 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001042
Guido van Rossum374a9221991-04-04 10:40:29 +00001043 case UNARY_CONVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001044 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001045 x = PyObject_Repr(v);
1046 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001047 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001048 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001049 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001050
Guido van Rossum7928cd71991-10-24 14:59:31 +00001051 case UNARY_INVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001052 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001053 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001054 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001055 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001056 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001057 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001058
Guido van Rossum50564e81996-01-12 01:13:16 +00001059 case BINARY_POWER:
1060 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001061 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001062 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +00001063 Py_DECREF(v);
1064 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001065 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001066 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +00001067 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001068
Guido van Rossum374a9221991-04-04 10:40:29 +00001069 case BINARY_MULTIPLY:
1070 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001071 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001072 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001073 Py_DECREF(v);
1074 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001075 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001076 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001077 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001078
Tim Peters3caca232001-12-06 06:23:26 +00001079 case BINARY_TRUE_DIVIDE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001080 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001081 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001082 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001083 Py_DECREF(v);
1084 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001085 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001086 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001087 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001088
Guido van Rossum4668b002001-08-08 05:00:18 +00001089 case BINARY_FLOOR_DIVIDE:
1090 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001091 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001092 x = PyNumber_FloorDivide(v, w);
1093 Py_DECREF(v);
1094 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001095 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001096 if (x != NULL) continue;
1097 break;
1098
Guido van Rossum374a9221991-04-04 10:40:29 +00001099 case BINARY_MODULO:
1100 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001101 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001102 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001103 Py_DECREF(v);
1104 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001105 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001106 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001107 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001108
Guido van Rossum374a9221991-04-04 10:40:29 +00001109 case BINARY_ADD:
1110 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001111 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001112 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001113 /* INLINE: int + int */
1114 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001115 a = PyInt_AS_LONG(v);
1116 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001117 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001118 if ((i^a) < 0 && (i^b) < 0)
1119 goto slow_add;
1120 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001121 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001122 else if (PyString_CheckExact(v) &&
1123 PyString_CheckExact(w)) {
1124 x = string_concatenate(v, w, f, next_instr);
1125 /* string_concatenate consumed the ref to v */
1126 goto skip_decref_vx;
1127 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001128 else {
1129 slow_add:
Guido van Rossumc12da691997-07-17 23:12:42 +00001130 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001131 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001132 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001133 skip_decref_vx:
Guido van Rossumb209a111997-04-29 18:18:01 +00001134 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001135 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001136 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001137 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001138
Guido van Rossum374a9221991-04-04 10:40:29 +00001139 case BINARY_SUBTRACT:
1140 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001141 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001142 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001143 /* INLINE: int - int */
1144 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001145 a = PyInt_AS_LONG(v);
1146 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001147 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001148 if ((i^a) < 0 && (i^~b) < 0)
1149 goto slow_sub;
1150 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001151 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001152 else {
1153 slow_sub:
Guido van Rossumc12da691997-07-17 23:12:42 +00001154 x = PyNumber_Subtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001155 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001156 Py_DECREF(v);
1157 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001158 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001159 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001160 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001161
Guido van Rossum374a9221991-04-04 10:40:29 +00001162 case BINARY_SUBSCR:
1163 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001164 v = TOP();
Tim Petersb1c46982001-10-05 20:41:38 +00001165 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001166 /* INLINE: list[int] */
Neal Norwitz814e9382006-03-02 07:54:28 +00001167 Py_ssize_t i = PyInt_AsSsize_t(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001168 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +00001169 i += PyList_GET_SIZE(v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001170 if (i >= 0 && i < PyList_GET_SIZE(v)) {
Guido van Rossumfa00e951998-07-08 15:02:37 +00001171 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001172 Py_INCREF(x);
1173 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001174 else
1175 goto slow_get;
Guido van Rossumc12da691997-07-17 23:12:42 +00001176 }
1177 else
Raymond Hettinger467a6982004-04-07 11:39:21 +00001178 slow_get:
Guido van Rossumc12da691997-07-17 23:12:42 +00001179 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001180 Py_DECREF(v);
1181 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001182 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001183 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001184 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001185
Guido van Rossum7928cd71991-10-24 14:59:31 +00001186 case BINARY_LSHIFT:
1187 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001188 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001189 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001190 Py_DECREF(v);
1191 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001192 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001193 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001194 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001195
Guido van Rossum7928cd71991-10-24 14:59:31 +00001196 case BINARY_RSHIFT:
1197 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001198 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001199 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001200 Py_DECREF(v);
1201 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001202 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001203 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001204 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001205
Guido van Rossum7928cd71991-10-24 14:59:31 +00001206 case BINARY_AND:
1207 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001208 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001209 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001210 Py_DECREF(v);
1211 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001212 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001213 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001214 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001215
Guido van Rossum7928cd71991-10-24 14:59:31 +00001216 case BINARY_XOR:
1217 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001218 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001219 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001220 Py_DECREF(v);
1221 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001222 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001223 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001224 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001225
Guido van Rossum7928cd71991-10-24 14:59:31 +00001226 case BINARY_OR:
1227 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001228 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001229 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001230 Py_DECREF(v);
1231 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001232 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001233 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001234 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001235
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001236 case LIST_APPEND:
1237 w = POP();
1238 v = POP();
1239 err = PyList_Append(v, w);
1240 Py_DECREF(v);
1241 Py_DECREF(w);
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001242 if (err == 0) {
1243 PREDICT(JUMP_ABSOLUTE);
1244 continue;
1245 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001246 break;
1247
Thomas Wouters434d0822000-08-24 20:11:32 +00001248 case INPLACE_POWER:
1249 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001250 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001251 x = PyNumber_InPlacePower(v, w, Py_None);
1252 Py_DECREF(v);
1253 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001254 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001255 if (x != NULL) continue;
1256 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001257
Thomas Wouters434d0822000-08-24 20:11:32 +00001258 case INPLACE_MULTIPLY:
1259 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001260 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001261 x = PyNumber_InPlaceMultiply(v, w);
1262 Py_DECREF(v);
1263 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001264 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001265 if (x != NULL) continue;
1266 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001267
Tim Peters54b11912001-12-25 18:49:11 +00001268 case INPLACE_TRUE_DIVIDE:
Thomas Wouters434d0822000-08-24 20:11:32 +00001269 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001270 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001271 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001272 Py_DECREF(v);
1273 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001274 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001275 if (x != NULL) continue;
1276 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001277
Guido van Rossum4668b002001-08-08 05:00:18 +00001278 case INPLACE_FLOOR_DIVIDE:
1279 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001280 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001281 x = PyNumber_InPlaceFloorDivide(v, w);
1282 Py_DECREF(v);
1283 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001284 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001285 if (x != NULL) continue;
1286 break;
1287
Thomas Wouters434d0822000-08-24 20:11:32 +00001288 case INPLACE_MODULO:
1289 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001290 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001291 x = PyNumber_InPlaceRemainder(v, w);
1292 Py_DECREF(v);
1293 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001294 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001295 if (x != NULL) continue;
1296 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001297
Thomas Wouters434d0822000-08-24 20:11:32 +00001298 case INPLACE_ADD:
1299 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001300 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001301 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001302 /* INLINE: int + int */
1303 register long a, b, i;
1304 a = PyInt_AS_LONG(v);
1305 b = PyInt_AS_LONG(w);
1306 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001307 if ((i^a) < 0 && (i^b) < 0)
1308 goto slow_iadd;
1309 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001310 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001311 else if (PyString_CheckExact(v) &&
1312 PyString_CheckExact(w)) {
1313 x = string_concatenate(v, w, f, next_instr);
1314 /* string_concatenate consumed the ref to v */
1315 goto skip_decref_v;
1316 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001317 else {
1318 slow_iadd:
Thomas Wouters434d0822000-08-24 20:11:32 +00001319 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001320 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001321 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001322 skip_decref_v:
Thomas Wouters434d0822000-08-24 20:11:32 +00001323 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001324 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001325 if (x != NULL) continue;
1326 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001327
Thomas Wouters434d0822000-08-24 20:11:32 +00001328 case INPLACE_SUBTRACT:
1329 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001330 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001331 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001332 /* INLINE: int - int */
1333 register long a, b, i;
1334 a = PyInt_AS_LONG(v);
1335 b = PyInt_AS_LONG(w);
1336 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001337 if ((i^a) < 0 && (i^~b) < 0)
1338 goto slow_isub;
1339 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001340 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001341 else {
1342 slow_isub:
Thomas Wouters434d0822000-08-24 20:11:32 +00001343 x = PyNumber_InPlaceSubtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001344 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001345 Py_DECREF(v);
1346 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001347 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001348 if (x != NULL) continue;
1349 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001350
Thomas Wouters434d0822000-08-24 20:11:32 +00001351 case INPLACE_LSHIFT:
1352 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001353 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001354 x = PyNumber_InPlaceLshift(v, w);
1355 Py_DECREF(v);
1356 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001357 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001358 if (x != NULL) continue;
1359 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001360
Thomas Wouters434d0822000-08-24 20:11:32 +00001361 case INPLACE_RSHIFT:
1362 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001363 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001364 x = PyNumber_InPlaceRshift(v, w);
1365 Py_DECREF(v);
1366 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001367 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001368 if (x != NULL) continue;
1369 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001370
Thomas Wouters434d0822000-08-24 20:11:32 +00001371 case INPLACE_AND:
1372 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001373 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001374 x = PyNumber_InPlaceAnd(v, w);
1375 Py_DECREF(v);
1376 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001377 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001378 if (x != NULL) continue;
1379 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001380
Thomas Wouters434d0822000-08-24 20:11:32 +00001381 case INPLACE_XOR:
1382 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001383 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001384 x = PyNumber_InPlaceXor(v, w);
1385 Py_DECREF(v);
1386 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001387 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001388 if (x != NULL) continue;
1389 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001390
Thomas Wouters434d0822000-08-24 20:11:32 +00001391 case INPLACE_OR:
1392 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001393 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001394 x = PyNumber_InPlaceOr(v, w);
1395 Py_DECREF(v);
1396 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001397 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001398 if (x != NULL) continue;
1399 break;
1400
Guido van Rossum374a9221991-04-04 10:40:29 +00001401 case SLICE+0:
1402 case SLICE+1:
1403 case SLICE+2:
1404 case SLICE+3:
1405 if ((opcode-SLICE) & 2)
1406 w = POP();
1407 else
1408 w = NULL;
1409 if ((opcode-SLICE) & 1)
1410 v = POP();
1411 else
1412 v = NULL;
Raymond Hettinger663004b2003-01-09 15:24:30 +00001413 u = TOP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001414 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001415 Py_DECREF(u);
1416 Py_XDECREF(v);
1417 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001418 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001419 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001420 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001421
Guido van Rossum374a9221991-04-04 10:40:29 +00001422 case STORE_SLICE+0:
1423 case STORE_SLICE+1:
1424 case STORE_SLICE+2:
1425 case STORE_SLICE+3:
1426 if ((opcode-STORE_SLICE) & 2)
1427 w = POP();
1428 else
1429 w = NULL;
1430 if ((opcode-STORE_SLICE) & 1)
1431 v = POP();
1432 else
1433 v = NULL;
1434 u = POP();
1435 t = POP();
1436 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +00001437 Py_DECREF(t);
1438 Py_DECREF(u);
1439 Py_XDECREF(v);
1440 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001441 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001442 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001443
Guido van Rossum374a9221991-04-04 10:40:29 +00001444 case DELETE_SLICE+0:
1445 case DELETE_SLICE+1:
1446 case DELETE_SLICE+2:
1447 case DELETE_SLICE+3:
1448 if ((opcode-DELETE_SLICE) & 2)
1449 w = POP();
1450 else
1451 w = NULL;
1452 if ((opcode-DELETE_SLICE) & 1)
1453 v = POP();
1454 else
1455 v = NULL;
1456 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001457 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +00001458 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00001459 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 STORE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001466 w = TOP();
1467 v = SECOND();
1468 u = THIRD();
1469 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001470 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001471 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001472 Py_DECREF(u);
1473 Py_DECREF(v);
1474 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001475 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001476 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001477
Guido van Rossum374a9221991-04-04 10:40:29 +00001478 case DELETE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001479 w = TOP();
1480 v = SECOND();
1481 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001482 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001483 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001484 Py_DECREF(v);
1485 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001486 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001487 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001488
Guido van Rossum374a9221991-04-04 10:40:29 +00001489 case PRINT_EXPR:
1490 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001491 w = PySys_GetObject("displayhook");
1492 if (w == NULL) {
1493 PyErr_SetString(PyExc_RuntimeError,
1494 "lost sys.displayhook");
1495 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001496 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001497 }
1498 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001499 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001500 if (x == NULL)
1501 err = -1;
1502 }
1503 if (err == 0) {
1504 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001505 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001506 if (w == NULL)
1507 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001508 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001509 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001510 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001511 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001512
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001513 case PRINT_ITEM_TO:
1514 w = stream = POP();
1515 /* fall through to PRINT_ITEM */
1516
Guido van Rossum374a9221991-04-04 10:40:29 +00001517 case PRINT_ITEM:
1518 v = POP();
Barry Warsaw093abe02000-08-29 04:56:13 +00001519 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001520 w = PySys_GetObject("stdout");
1521 if (w == NULL) {
1522 PyErr_SetString(PyExc_RuntimeError,
1523 "lost sys.stdout");
1524 err = -1;
1525 }
Guido van Rossum8f183201997-12-31 05:53:15 +00001526 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001527 /* PyFile_SoftSpace() can exececute arbitrary code
1528 if sys.stdout is an instance with a __getattr__.
1529 If __getattr__ raises an exception, w will
1530 be freed, so we need to prevent that temporarily. */
1531 Py_XINCREF(w);
Tim Peters8e5fd532002-03-24 19:25:00 +00001532 if (w != NULL && PyFile_SoftSpace(w, 0))
Guido van Rossumbe270261997-05-22 22:26:18 +00001533 err = PyFile_WriteString(" ", w);
1534 if (err == 0)
1535 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001536 if (err == 0) {
Tim Peters8e5fd532002-03-24 19:25:00 +00001537 /* XXX move into writeobject() ? */
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001538 if (PyString_Check(v)) {
1539 char *s = PyString_AS_STRING(v);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001540 Py_ssize_t len = PyString_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001541 if (len == 0 ||
1542 !isspace(Py_CHARMASK(s[len-1])) ||
1543 s[len-1] == ' ')
1544 PyFile_SoftSpace(w, 1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001545 }
Martin v. Löwis8d3ce5a2001-12-18 22:36:40 +00001546#ifdef Py_USING_UNICODE
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001547 else if (PyUnicode_Check(v)) {
1548 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001549 Py_ssize_t len = PyUnicode_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001550 if (len == 0 ||
1551 !Py_UNICODE_ISSPACE(s[len-1]) ||
1552 s[len-1] == ' ')
1553 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001554 }
Michael W. Hudsond95c8282002-05-20 13:56:11 +00001555#endif
Tim Peters8e5fd532002-03-24 19:25:00 +00001556 else
1557 PyFile_SoftSpace(w, 1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001558 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001559 Py_XDECREF(w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001560 Py_DECREF(v);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001561 Py_XDECREF(stream);
1562 stream = NULL;
1563 if (err == 0)
1564 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001565 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001566
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001567 case PRINT_NEWLINE_TO:
1568 w = stream = POP();
1569 /* fall through to PRINT_NEWLINE */
1570
Guido van Rossum374a9221991-04-04 10:40:29 +00001571 case PRINT_NEWLINE:
Barry Warsaw093abe02000-08-29 04:56:13 +00001572 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001573 w = PySys_GetObject("stdout");
1574 if (w == NULL)
1575 PyErr_SetString(PyExc_RuntimeError,
1576 "lost sys.stdout");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001577 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001578 if (w != NULL) {
1579 err = PyFile_WriteString("\n", w);
1580 if (err == 0)
1581 PyFile_SoftSpace(w, 0);
1582 }
1583 Py_XDECREF(stream);
1584 stream = NULL;
Guido van Rossum374a9221991-04-04 10:40:29 +00001585 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001586
Thomas Wouters434d0822000-08-24 20:11:32 +00001587
1588#ifdef CASE_TOO_BIG
1589 default: switch (opcode) {
1590#endif
Guido van Rossumf10570b1995-07-07 22:53:21 +00001591 case RAISE_VARARGS:
1592 u = v = w = NULL;
1593 switch (oparg) {
1594 case 3:
1595 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001596 /* Fallthrough */
1597 case 2:
1598 v = POP(); /* value */
1599 /* Fallthrough */
1600 case 1:
1601 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001602 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001603 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001604 break;
1605 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001606 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001607 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001608 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001609 break;
1610 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001611 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001612
Guido van Rossum374a9221991-04-04 10:40:29 +00001613 case LOAD_LOCALS:
Raymond Hettinger467a6982004-04-07 11:39:21 +00001614 if ((x = f->f_locals) != NULL) {
1615 Py_INCREF(x);
1616 PUSH(x);
1617 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001618 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001619 PyErr_SetString(PyExc_SystemError, "no locals");
Guido van Rossum374a9221991-04-04 10:40:29 +00001620 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001621
Guido van Rossum374a9221991-04-04 10:40:29 +00001622 case RETURN_VALUE:
1623 retval = POP();
1624 why = WHY_RETURN;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001625 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001626
Tim Peters5ca576e2001-06-18 22:08:13 +00001627 case YIELD_VALUE:
1628 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001629 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001630 why = WHY_YIELD;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001631 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001632
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001633 case EXEC_STMT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001634 w = TOP();
1635 v = SECOND();
1636 u = THIRD();
1637 STACKADJ(-3);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001638 READ_TIMESTAMP(intr0);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001639 err = exec_statement(f, u, v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001640 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00001641 Py_DECREF(u);
1642 Py_DECREF(v);
1643 Py_DECREF(w);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001644 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001645
Guido van Rossum374a9221991-04-04 10:40:29 +00001646 case POP_BLOCK:
1647 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001648 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001649 while (STACK_LEVEL() > b->b_level) {
1650 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001651 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001652 }
1653 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001654 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001655
Guido van Rossum374a9221991-04-04 10:40:29 +00001656 case END_FINALLY:
1657 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001658 if (PyInt_Check(v)) {
Raymond Hettinger7c958652004-04-06 10:11:10 +00001659 why = (enum why_code) PyInt_AS_LONG(v);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001660 assert(why != WHY_YIELD);
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00001661 if (why == WHY_RETURN ||
1662 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001663 retval = POP();
1664 }
Brett Cannonbf364092006-03-01 04:25:17 +00001665 else if (PyExceptionClass_Check(v) || PyString_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001666 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001667 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001668 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001669 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001670 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001671 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001672 else if (v != Py_None) {
1673 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001674 "'finally' pops bad exception");
1675 why = WHY_EXCEPTION;
1676 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001677 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001678 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001679
Guido van Rossum374a9221991-04-04 10:40:29 +00001680 case BUILD_CLASS:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001681 u = TOP();
1682 v = SECOND();
1683 w = THIRD();
1684 STACKADJ(-2);
Guido van Rossum25831651993-05-19 14:50:45 +00001685 x = build_class(u, v, w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001686 SET_TOP(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001687 Py_DECREF(u);
1688 Py_DECREF(v);
1689 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001690 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001691
Guido van Rossum374a9221991-04-04 10:40:29 +00001692 case STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001693 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001694 v = POP();
Raymond Hettinger467a6982004-04-07 11:39:21 +00001695 if ((x = f->f_locals) != NULL) {
Raymond Hettinger66bd2332004-08-02 08:30:07 +00001696 if (PyDict_CheckExact(x))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001697 err = PyDict_SetItem(x, w, v);
1698 else
1699 err = PyObject_SetItem(x, w, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001700 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001701 if (err == 0) continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001702 break;
1703 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001704 PyErr_Format(PyExc_SystemError,
1705 "no locals found when storing %s",
1706 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001707 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001708
Guido van Rossum374a9221991-04-04 10:40:29 +00001709 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001710 w = GETITEM(names, oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001711 if ((x = f->f_locals) != NULL) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001712 if ((err = PyObject_DelItem(x, w)) != 0)
Raymond Hettinger467a6982004-04-07 11:39:21 +00001713 format_exc_check_arg(PyExc_NameError,
1714 NAME_ERROR_MSG ,w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001715 break;
1716 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001717 PyErr_Format(PyExc_SystemError,
1718 "no locals when deleting %s",
1719 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001720 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001721
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001722 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001723 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001724 v = POP();
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001725 if (PyTuple_CheckExact(v) && PyTuple_GET_SIZE(v) == oparg) {
1726 PyObject **items = ((PyTupleObject *)v)->ob_item;
1727 while (oparg--) {
1728 w = items[oparg];
1729 Py_INCREF(w);
1730 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001731 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001732 Py_DECREF(v);
1733 continue;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001734 } else if (PyList_CheckExact(v) && PyList_GET_SIZE(v) == oparg) {
1735 PyObject **items = ((PyListObject *)v)->ob_item;
1736 while (oparg--) {
1737 w = items[oparg];
1738 Py_INCREF(w);
1739 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001740 }
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001741 } else if (unpack_iterable(v, oparg,
Tim Petersd6d010b2001-06-21 02:49:55 +00001742 stack_pointer + oparg))
1743 stack_pointer += oparg;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001744 else {
1745 if (PyErr_ExceptionMatches(PyExc_TypeError))
1746 PyErr_SetString(PyExc_TypeError,
1747 "unpack non-sequence");
Barry Warsawe42b18f1997-08-25 22:13:04 +00001748 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001749 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001750 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001751 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001752
Guido van Rossum374a9221991-04-04 10:40:29 +00001753 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001754 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001755 v = TOP();
1756 u = SECOND();
1757 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001758 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1759 Py_DECREF(v);
1760 Py_DECREF(u);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001761 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001762 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001763
Guido van Rossum374a9221991-04-04 10:40:29 +00001764 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001765 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001766 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001767 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1768 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001769 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001770 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001771
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001772 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001773 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001774 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001775 err = PyDict_SetItem(f->f_globals, w, v);
1776 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001777 if (err == 0) continue;
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001778 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001779
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001780 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001781 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001782 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001783 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001784 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001785 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001786
Guido van Rossum374a9221991-04-04 10:40:29 +00001787 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001788 w = GETITEM(names, oparg);
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001789 if ((v = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001790 PyErr_Format(PyExc_SystemError,
1791 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001792 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001793 break;
1794 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001795 if (PyDict_CheckExact(v)) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001796 x = PyDict_GetItem(v, w);
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001797 Py_XINCREF(x);
1798 }
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001799 else {
1800 x = PyObject_GetItem(v, w);
1801 if (x == NULL && PyErr_Occurred()) {
1802 if (!PyErr_ExceptionMatches(PyExc_KeyError))
1803 break;
1804 PyErr_Clear();
1805 }
1806 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001807 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001808 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001809 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001810 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001811 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001812 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001813 PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001814 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001815 break;
1816 }
1817 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001818 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001819 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001820 PUSH(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001821 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001822
Guido van Rossum374a9221991-04-04 10:40:29 +00001823 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001824 w = GETITEM(names, oparg);
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001825 if (PyString_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00001826 /* Inline the PyDict_GetItem() calls.
1827 WARNING: this is an extreme speed hack.
1828 Do not try this at home. */
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001829 long hash = ((PyStringObject *)w)->ob_shash;
1830 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001831 PyDictObject *d;
1832 d = (PyDictObject *)(f->f_globals);
1833 x = d->ma_lookup(d, w, hash)->me_value;
1834 if (x != NULL) {
1835 Py_INCREF(x);
1836 PUSH(x);
1837 continue;
1838 }
1839 d = (PyDictObject *)(f->f_builtins);
1840 x = d->ma_lookup(d, w, hash)->me_value;
1841 if (x != NULL) {
1842 Py_INCREF(x);
1843 PUSH(x);
1844 continue;
1845 }
1846 goto load_global_error;
1847 }
1848 }
1849 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00001850 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001851 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001852 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001853 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001854 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00001855 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001856 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001857 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001858 break;
1859 }
1860 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001861 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001862 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001863 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001864
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001865 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001866 x = GETLOCAL(oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001867 if (x != NULL) {
1868 SETLOCAL(oparg, NULL);
1869 continue;
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001870 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001871 format_exc_check_arg(
1872 PyExc_UnboundLocalError,
1873 UNBOUNDLOCAL_ERROR_MSG,
1874 PyTuple_GetItem(co->co_varnames, oparg)
1875 );
1876 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001877
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001878 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001879 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001880 Py_INCREF(x);
1881 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001882 if (x != NULL) continue;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001883 break;
1884
1885 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001886 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001887 w = PyCell_Get(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001888 if (w != NULL) {
1889 PUSH(w);
1890 continue;
Jeremy Hylton2524d692001-02-05 17:23:16 +00001891 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001892 err = -1;
1893 /* Don't stomp existing exception */
1894 if (PyErr_Occurred())
1895 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001896 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
1897 v = PyTuple_GET_ITEM(co->co_cellvars,
Raymond Hettinger467a6982004-04-07 11:39:21 +00001898 oparg);
1899 format_exc_check_arg(
1900 PyExc_UnboundLocalError,
1901 UNBOUNDLOCAL_ERROR_MSG,
1902 v);
1903 } else {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001904 v = PyTuple_GET_ITEM(
Raymond Hettinger467a6982004-04-07 11:39:21 +00001905 co->co_freevars,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001906 oparg - PyTuple_GET_SIZE(co->co_cellvars));
Raymond Hettinger467a6982004-04-07 11:39:21 +00001907 format_exc_check_arg(
1908 PyExc_NameError,
1909 UNBOUNDFREE_ERROR_MSG,
1910 v);
1911 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001912 break;
1913
1914 case STORE_DEREF:
1915 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001916 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001917 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00001918 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001919 continue;
1920
Guido van Rossum374a9221991-04-04 10:40:29 +00001921 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001922 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001923 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001924 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001925 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001926 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001927 }
1928 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001929 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001930 }
1931 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001932
Guido van Rossum374a9221991-04-04 10:40:29 +00001933 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00001934 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001935 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001936 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001937 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00001938 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001939 }
1940 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001941 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001942 }
1943 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001944
Guido van Rossum374a9221991-04-04 10:40:29 +00001945 case BUILD_MAP:
Guido van Rossumb209a111997-04-29 18:18:01 +00001946 x = PyDict_New();
Guido van Rossum374a9221991-04-04 10:40:29 +00001947 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001948 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001949 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001950
Guido van Rossum374a9221991-04-04 10:40:29 +00001951 case LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001952 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001953 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001954 x = PyObject_GetAttr(v, w);
1955 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001956 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001957 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001958 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001959
Guido van Rossum374a9221991-04-04 10:40:29 +00001960 case COMPARE_OP:
1961 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001962 v = TOP();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001963 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001964 /* INLINE: cmp(int, int) */
1965 register long a, b;
1966 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001967 a = PyInt_AS_LONG(v);
1968 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001969 switch (oparg) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00001970 case PyCmp_LT: res = a < b; break;
1971 case PyCmp_LE: res = a <= b; break;
1972 case PyCmp_EQ: res = a == b; break;
1973 case PyCmp_NE: res = a != b; break;
1974 case PyCmp_GT: res = a > b; break;
1975 case PyCmp_GE: res = a >= b; break;
1976 case PyCmp_IS: res = v == w; break;
1977 case PyCmp_IS_NOT: res = v != w; break;
Guido van Rossumc12da691997-07-17 23:12:42 +00001978 default: goto slow_compare;
1979 }
1980 x = res ? Py_True : Py_False;
1981 Py_INCREF(x);
1982 }
1983 else {
1984 slow_compare:
1985 x = cmp_outcome(oparg, v, w);
1986 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001987 Py_DECREF(v);
1988 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001989 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001990 if (x == NULL) break;
1991 PREDICT(JUMP_IF_FALSE);
1992 PREDICT(JUMP_IF_TRUE);
1993 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001994
Guido van Rossum374a9221991-04-04 10:40:29 +00001995 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001996 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001997 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001998 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001999 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00002000 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002001 break;
2002 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002003 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002004 u = TOP();
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002005 if (PyInt_AsLong(u) != -1 || PyErr_Occurred())
2006 w = PyTuple_Pack(5,
2007 w,
2008 f->f_globals,
2009 f->f_locals == NULL ?
2010 Py_None : f->f_locals,
2011 v,
2012 u);
2013 else
2014 w = PyTuple_Pack(4,
2015 w,
2016 f->f_globals,
2017 f->f_locals == NULL ?
2018 Py_None : f->f_locals,
2019 v);
2020 Py_DECREF(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002021 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002022 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002023 u = POP();
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002024 x = NULL;
2025 break;
2026 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002027 READ_TIMESTAMP(intr0);
Guido van Rossumb209a111997-04-29 18:18:01 +00002028 x = PyEval_CallObject(x, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002029 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002030 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002031 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002032 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002033 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002034
Thomas Wouters52152252000-08-17 22:55:00 +00002035 case IMPORT_STAR:
2036 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002037 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002038 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002039 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00002040 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002041 break;
2042 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002043 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002044 err = import_all_from(x, v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002045 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002046 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00002047 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002048 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002049 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002050
Thomas Wouters52152252000-08-17 22:55:00 +00002051 case IMPORT_FROM:
Skip Montanaro496e6582002-08-06 17:47:40 +00002052 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00002053 v = TOP();
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002054 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002055 x = import_from(v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002056 READ_TIMESTAMP(intr1);
Thomas Wouters52152252000-08-17 22:55:00 +00002057 PUSH(x);
2058 if (x != NULL) continue;
2059 break;
2060
Guido van Rossum374a9221991-04-04 10:40:29 +00002061 case JUMP_FORWARD:
2062 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002063 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002064
Raymond Hettingerf606f872003-03-16 03:11:04 +00002065 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002066 case JUMP_IF_FALSE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002067 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002068 if (w == Py_True) {
2069 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002070 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002071 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002072 if (w == Py_False) {
2073 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002074 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002075 }
2076 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002077 if (err > 0)
2078 err = 0;
2079 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00002080 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002081 else
2082 break;
2083 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002084
Raymond Hettingerf606f872003-03-16 03:11:04 +00002085 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002086 case JUMP_IF_TRUE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002087 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002088 if (w == Py_False) {
2089 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002090 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002091 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002092 if (w == Py_True) {
2093 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002094 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002095 }
2096 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002097 if (err > 0) {
2098 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00002099 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002100 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002101 else if (err == 0)
2102 ;
2103 else
2104 break;
2105 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002106
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00002107 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002108 case JUMP_ABSOLUTE:
2109 JUMPTO(oparg);
Neil Schemenauerca2a2f12003-05-30 23:59:44 +00002110 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002111
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002112 case GET_ITER:
2113 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002114 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002115 x = PyObject_GetIter(v);
2116 Py_DECREF(v);
2117 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002118 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002119 PREDICT(FOR_ITER);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002120 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002121 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002122 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002123 break;
2124
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002125 PREDICTED_WITH_ARG(FOR_ITER);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002126 case FOR_ITER:
2127 /* before: [iter]; after: [iter, iter()] *or* [] */
2128 v = TOP();
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002129 x = (*v->ob_type->tp_iternext)(v);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002130 if (x != NULL) {
2131 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002132 PREDICT(STORE_FAST);
2133 PREDICT(UNPACK_SEQUENCE);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002134 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002135 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002136 if (PyErr_Occurred()) {
2137 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2138 break;
2139 PyErr_Clear();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002140 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002141 /* iterator ended normally */
2142 x = v = POP();
2143 Py_DECREF(v);
2144 JUMPBY(oparg);
2145 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002146
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002147 case BREAK_LOOP:
2148 why = WHY_BREAK;
2149 goto fast_block_end;
2150
2151 case CONTINUE_LOOP:
2152 retval = PyInt_FromLong(oparg);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002153 if (!retval) {
2154 x = NULL;
2155 break;
2156 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002157 why = WHY_CONTINUE;
2158 goto fast_block_end;
2159
Guido van Rossum374a9221991-04-04 10:40:29 +00002160 case SETUP_LOOP:
2161 case SETUP_EXCEPT:
2162 case SETUP_FINALLY:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002163 /* NOTE: If you add any new block-setup opcodes that are not try/except/finally
2164 handlers, you may need to update the PyGen_NeedsFinalizing() function. */
2165
Guido van Rossumb209a111997-04-29 18:18:01 +00002166 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002167 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002168 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002169
Guido van Rossumc2e20742006-02-27 22:32:47 +00002170 case WITH_CLEANUP:
2171 {
2172 /* TOP is the context.__exit__ bound method.
2173 Below that are 1-3 values indicating how/why
2174 we entered the finally clause:
2175 - SECOND = None
Guido van Rossumf6694362006-03-10 02:28:35 +00002176 - (SECOND, THIRD) = (WHY_{RETURN,CONTINUE}), retval
Guido van Rossumc2e20742006-02-27 22:32:47 +00002177 - SECOND = WHY_*; no retval below it
2178 - (SECOND, THIRD, FOURTH) = exc_info()
2179 In the last case, we must call
2180 TOP(SECOND, THIRD, FOURTH)
2181 otherwise we must call
2182 TOP(None, None, None)
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002183
2184 In addition, if the stack represents an exception,
Guido van Rossumf6694362006-03-10 02:28:35 +00002185 *and* the function call returns a 'true' value, we
2186 "zap" this information, to prevent END_FINALLY from
2187 re-raising the exception. (But non-local gotos
2188 should still be resumed.)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002189 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002190
Guido van Rossumc2e20742006-02-27 22:32:47 +00002191 x = TOP();
2192 u = SECOND();
2193 if (PyInt_Check(u) || u == Py_None) {
2194 u = v = w = Py_None;
2195 }
2196 else {
2197 v = THIRD();
2198 w = FOURTH();
2199 }
Guido van Rossumf6694362006-03-10 02:28:35 +00002200 /* XXX Not the fastest way to call it... */
2201 x = PyObject_CallFunctionObjArgs(x, u, v, w, NULL);
2202 if (x == NULL)
2203 break; /* Go to error exit */
2204 if (u != Py_None && PyObject_IsTrue(x)) {
2205 /* There was an exception and a true return */
2206 Py_DECREF(x);
2207 x = TOP(); /* Again */
2208 STACKADJ(-3);
2209 Py_INCREF(Py_None);
2210 SET_TOP(Py_None);
2211 Py_DECREF(x);
2212 Py_DECREF(u);
2213 Py_DECREF(v);
2214 Py_DECREF(w);
2215 } else {
2216 /* Let END_FINALLY do its thing */
2217 Py_DECREF(x);
2218 x = POP();
2219 Py_DECREF(x);
2220 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002221 break;
2222 }
2223
Guido van Rossumf10570b1995-07-07 22:53:21 +00002224 case CALL_FUNCTION:
Armin Rigo8817fcd2004-06-17 10:22:40 +00002225 {
2226 PyObject **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002227 PCALL(PCALL_ALL);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002228 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002229#ifdef WITH_TSC
Armin Rigo8817fcd2004-06-17 10:22:40 +00002230 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002231#else
Armin Rigo8817fcd2004-06-17 10:22:40 +00002232 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002233#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002234 stack_pointer = sp;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002235 PUSH(x);
2236 if (x != NULL)
2237 continue;
2238 break;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002239 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002240
Jeremy Hylton76901512000-03-28 23:49:17 +00002241 case CALL_FUNCTION_VAR:
2242 case CALL_FUNCTION_KW:
2243 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002244 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002245 int na = oparg & 0xff;
2246 int nk = (oparg>>8) & 0xff;
2247 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002248 int n = na + 2 * nk;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002249 PyObject **pfunc, *func, **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002250 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002251 if (flags & CALL_FLAG_VAR)
2252 n++;
2253 if (flags & CALL_FLAG_KW)
2254 n++;
2255 pfunc = stack_pointer - n - 1;
2256 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002257
Guido van Rossumac7be682001-01-17 15:42:30 +00002258 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002259 && PyMethod_GET_SELF(func) != NULL) {
2260 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002261 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002262 func = PyMethod_GET_FUNCTION(func);
2263 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002264 Py_DECREF(*pfunc);
2265 *pfunc = self;
2266 na++;
2267 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002268 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002269 Py_INCREF(func);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002270 sp = stack_pointer;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002271 READ_TIMESTAMP(intr0);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002272 x = ext_do_call(func, &sp, flags, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002273 READ_TIMESTAMP(intr1);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002274 stack_pointer = sp;
Jeremy Hylton76901512000-03-28 23:49:17 +00002275 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002276
Jeremy Hylton76901512000-03-28 23:49:17 +00002277 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002278 w = POP();
2279 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002280 }
2281 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002282 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002283 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002284 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002285 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002286
Guido van Rossum681d79a1995-07-18 14:51:37 +00002287 case MAKE_FUNCTION:
2288 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002289 x = PyFunction_New(v, f->f_globals);
2290 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002291 /* XXX Maybe this should be a separate opcode? */
2292 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002293 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002294 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002295 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002296 x = NULL;
2297 break;
2298 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002299 while (--oparg >= 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002300 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002301 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002302 }
2303 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002304 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002305 }
2306 PUSH(x);
2307 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002308
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002309 case MAKE_CLOSURE:
2310 {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002311 v = POP(); /* code object */
2312 x = PyFunction_New(v, f->f_globals);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002313 Py_DECREF(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002314 if (x != NULL) {
2315 v = POP();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002316 err = PyFunction_SetClosure(x, v);
2317 Py_DECREF(v);
2318 }
2319 if (x != NULL && oparg > 0) {
2320 v = PyTuple_New(oparg);
2321 if (v == NULL) {
2322 Py_DECREF(x);
2323 x = NULL;
2324 break;
2325 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002326 while (--oparg >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002327 w = POP();
2328 PyTuple_SET_ITEM(v, oparg, w);
2329 }
2330 err = PyFunction_SetDefaults(x, v);
2331 Py_DECREF(v);
2332 }
2333 PUSH(x);
2334 break;
2335 }
2336
Guido van Rossum8861b741996-07-30 16:49:37 +00002337 case BUILD_SLICE:
2338 if (oparg == 3)
2339 w = POP();
2340 else
2341 w = NULL;
2342 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002343 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002344 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002345 Py_DECREF(u);
2346 Py_DECREF(v);
2347 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002348 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002349 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002350 break;
2351
Fred Drakeef8ace32000-08-24 00:32:09 +00002352 case EXTENDED_ARG:
2353 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002354 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002355 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002356
Guido van Rossum374a9221991-04-04 10:40:29 +00002357 default:
2358 fprintf(stderr,
2359 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002360 PyCode_Addr2Line(f->f_code, f->f_lasti),
2361 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002362 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002363 why = WHY_EXCEPTION;
2364 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002365
2366#ifdef CASE_TOO_BIG
2367 }
2368#endif
2369
Guido van Rossum374a9221991-04-04 10:40:29 +00002370 } /* switch */
2371
2372 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002373
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002374 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002375
Guido van Rossum374a9221991-04-04 10:40:29 +00002376 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002377
Guido van Rossum374a9221991-04-04 10:40:29 +00002378 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002379 if (err == 0 && x != NULL) {
2380#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002381 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002382 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002383 fprintf(stderr,
2384 "XXX undetected error\n");
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002385 else {
2386#endif
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002387 READ_TIMESTAMP(loop1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002388 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002389#ifdef CHECKEXC
2390 }
2391#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +00002392 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002393 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002394 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002395 err = 0;
2396 }
2397
Guido van Rossum374a9221991-04-04 10:40:29 +00002398 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002399
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002400 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002401 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002402 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002403 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002404 why = WHY_EXCEPTION;
2405 }
2406 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002407#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002408 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002409 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002410 if (PyErr_Occurred()) {
Jeremy Hylton904ed862003-11-05 17:29:35 +00002411 char buf[1024];
2412 sprintf(buf, "Stack unwind with exception "
2413 "set and why=%d", why);
2414 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002415 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002416 }
2417#endif
2418
2419 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002420
Guido van Rossum374a9221991-04-04 10:40:29 +00002421 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002422 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002423
Fred Drake8f51f542001-10-04 14:48:42 +00002424 if (tstate->c_tracefunc != NULL)
2425 call_exc_trace(tstate->c_tracefunc,
2426 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002427 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002428
Guido van Rossum374a9221991-04-04 10:40:29 +00002429 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002430
Guido van Rossum374a9221991-04-04 10:40:29 +00002431 if (why == WHY_RERAISE)
2432 why = WHY_EXCEPTION;
2433
2434 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002435
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002436fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002437 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002438 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002439
Tim Peters8a5c3c72004-04-05 19:36:21 +00002440 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002441 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2442 /* For a continue inside a try block,
2443 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002444 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2445 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002446 why = WHY_NOT;
2447 JUMPTO(PyInt_AS_LONG(retval));
2448 Py_DECREF(retval);
2449 break;
2450 }
2451
Guido van Rossum374a9221991-04-04 10:40:29 +00002452 while (STACK_LEVEL() > b->b_level) {
2453 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002454 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002455 }
2456 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2457 why = WHY_NOT;
2458 JUMPTO(b->b_handler);
2459 break;
2460 }
2461 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002462 (b->b_type == SETUP_EXCEPT &&
2463 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002464 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002465 PyObject *exc, *val, *tb;
2466 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002467 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002468 val = Py_None;
2469 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002470 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002471 /* Make the raw exception data
2472 available to the handler,
2473 so a program can emulate the
2474 Python main loop. Don't do
2475 this for 'finally'. */
2476 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002477 PyErr_NormalizeException(
2478 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002479 set_exc_info(tstate,
2480 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002481 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002482 if (tb == NULL) {
2483 Py_INCREF(Py_None);
2484 PUSH(Py_None);
2485 } else
2486 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002487 PUSH(val);
2488 PUSH(exc);
2489 }
2490 else {
Raymond Hettinger06032cb2004-04-06 09:37:35 +00002491 if (why & (WHY_RETURN | WHY_CONTINUE))
Guido van Rossum374a9221991-04-04 10:40:29 +00002492 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002493 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002494 PUSH(v);
2495 }
2496 why = WHY_NOT;
2497 JUMPTO(b->b_handler);
2498 break;
2499 }
2500 } /* unwind stack */
2501
2502 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002503
Guido van Rossum374a9221991-04-04 10:40:29 +00002504 if (why != WHY_NOT)
2505 break;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002506 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002507
Guido van Rossum374a9221991-04-04 10:40:29 +00002508 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002509
Tim Peters8a5c3c72004-04-05 19:36:21 +00002510 assert(why != WHY_YIELD);
2511 /* Pop remaining stack entries. */
2512 while (!EMPTY()) {
2513 v = POP();
2514 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002515 }
2516
Tim Peters8a5c3c72004-04-05 19:36:21 +00002517 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002518 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002519
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002520fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002521 if (tstate->use_tracing) {
Barry Warsawe2eca0b2005-08-15 18:14:19 +00002522 if (tstate->c_tracefunc) {
2523 if (why == WHY_RETURN || why == WHY_YIELD) {
2524 if (call_trace(tstate->c_tracefunc,
2525 tstate->c_traceobj, f,
2526 PyTrace_RETURN, retval)) {
2527 Py_XDECREF(retval);
2528 retval = NULL;
2529 why = WHY_EXCEPTION;
2530 }
2531 }
2532 else if (why == WHY_EXCEPTION) {
2533 call_trace_protected(tstate->c_tracefunc,
2534 tstate->c_traceobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002535 PyTrace_RETURN, NULL);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002536 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002537 }
Fred Drake8f51f542001-10-04 14:48:42 +00002538 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002539 if (why == WHY_EXCEPTION)
2540 call_trace_protected(tstate->c_profilefunc,
2541 tstate->c_profileobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002542 PyTrace_RETURN, NULL);
Fred Drake4ec5d562001-10-04 19:26:43 +00002543 else if (call_trace(tstate->c_profilefunc,
2544 tstate->c_profileobj, f,
2545 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002546 Py_XDECREF(retval);
2547 retval = NULL;
2548 why = WHY_EXCEPTION;
2549 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002550 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002551 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002552
Thomas Wouters477c8d52006-05-27 19:21:47 +00002553 if (tstate->frame->f_exc_type != NULL)
2554 reset_exc_info(tstate);
2555 else {
2556 assert(tstate->frame->f_exc_value == NULL);
2557 assert(tstate->frame->f_exc_traceback == NULL);
2558 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00002559
Tim Peters5ca576e2001-06-18 22:08:13 +00002560 /* pop frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +00002561 exit_eval_frame:
2562 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002563 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002564
Guido van Rossum96a42c81992-01-12 02:29:51 +00002565 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002566}
2567
Guido van Rossumc2e20742006-02-27 22:32:47 +00002568/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002569 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00002570 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002571
Tim Peters6d6c1a32001-08-02 04:15:00 +00002572PyObject *
2573PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002574 PyObject **args, int argcount, PyObject **kws, int kwcount,
2575 PyObject **defs, int defcount, PyObject *closure)
2576{
2577 register PyFrameObject *f;
2578 register PyObject *retval = NULL;
2579 register PyObject **fastlocals, **freevars;
2580 PyThreadState *tstate = PyThreadState_GET();
2581 PyObject *x, *u;
2582
2583 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00002584 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002585 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002586 return NULL;
2587 }
2588
Jeremy Hylton985eba52003-02-05 23:13:00 +00002589 assert(globals != NULL);
2590 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002591 if (f == NULL)
2592 return NULL;
2593
2594 fastlocals = f->f_localsplus;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002595 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00002596
2597 if (co->co_argcount > 0 ||
2598 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2599 int i;
2600 int n = argcount;
2601 PyObject *kwdict = NULL;
2602 if (co->co_flags & CO_VARKEYWORDS) {
2603 kwdict = PyDict_New();
2604 if (kwdict == NULL)
2605 goto fail;
2606 i = co->co_argcount;
2607 if (co->co_flags & CO_VARARGS)
2608 i++;
2609 SETLOCAL(i, kwdict);
2610 }
2611 if (argcount > co->co_argcount) {
2612 if (!(co->co_flags & CO_VARARGS)) {
2613 PyErr_Format(PyExc_TypeError,
2614 "%.200s() takes %s %d "
2615 "%sargument%s (%d given)",
2616 PyString_AsString(co->co_name),
2617 defcount ? "at most" : "exactly",
2618 co->co_argcount,
2619 kwcount ? "non-keyword " : "",
2620 co->co_argcount == 1 ? "" : "s",
2621 argcount);
2622 goto fail;
2623 }
2624 n = co->co_argcount;
2625 }
2626 for (i = 0; i < n; i++) {
2627 x = args[i];
2628 Py_INCREF(x);
2629 SETLOCAL(i, x);
2630 }
2631 if (co->co_flags & CO_VARARGS) {
2632 u = PyTuple_New(argcount - n);
2633 if (u == NULL)
2634 goto fail;
2635 SETLOCAL(co->co_argcount, u);
2636 for (i = n; i < argcount; i++) {
2637 x = args[i];
2638 Py_INCREF(x);
2639 PyTuple_SET_ITEM(u, i-n, x);
2640 }
2641 }
2642 for (i = 0; i < kwcount; i++) {
2643 PyObject *keyword = kws[2*i];
2644 PyObject *value = kws[2*i + 1];
2645 int j;
2646 if (keyword == NULL || !PyString_Check(keyword)) {
2647 PyErr_Format(PyExc_TypeError,
2648 "%.200s() keywords must be strings",
2649 PyString_AsString(co->co_name));
2650 goto fail;
2651 }
2652 /* XXX slow -- speed up using dictionary? */
2653 for (j = 0; j < co->co_argcount; j++) {
2654 PyObject *nm = PyTuple_GET_ITEM(
2655 co->co_varnames, j);
2656 int cmp = PyObject_RichCompareBool(
2657 keyword, nm, Py_EQ);
2658 if (cmp > 0)
2659 break;
2660 else if (cmp < 0)
2661 goto fail;
2662 }
2663 /* Check errors from Compare */
2664 if (PyErr_Occurred())
2665 goto fail;
2666 if (j >= co->co_argcount) {
2667 if (kwdict == NULL) {
2668 PyErr_Format(PyExc_TypeError,
2669 "%.200s() got an unexpected "
2670 "keyword argument '%.400s'",
2671 PyString_AsString(co->co_name),
2672 PyString_AsString(keyword));
2673 goto fail;
2674 }
2675 PyDict_SetItem(kwdict, keyword, value);
2676 }
2677 else {
2678 if (GETLOCAL(j) != NULL) {
2679 PyErr_Format(PyExc_TypeError,
2680 "%.200s() got multiple "
2681 "values for keyword "
2682 "argument '%.400s'",
2683 PyString_AsString(co->co_name),
2684 PyString_AsString(keyword));
2685 goto fail;
2686 }
2687 Py_INCREF(value);
2688 SETLOCAL(j, value);
2689 }
2690 }
2691 if (argcount < co->co_argcount) {
2692 int m = co->co_argcount - defcount;
2693 for (i = argcount; i < m; i++) {
2694 if (GETLOCAL(i) == NULL) {
2695 PyErr_Format(PyExc_TypeError,
2696 "%.200s() takes %s %d "
2697 "%sargument%s (%d given)",
2698 PyString_AsString(co->co_name),
2699 ((co->co_flags & CO_VARARGS) ||
2700 defcount) ? "at least"
2701 : "exactly",
2702 m, kwcount ? "non-keyword " : "",
2703 m == 1 ? "" : "s", i);
2704 goto fail;
2705 }
2706 }
2707 if (n > m)
2708 i = n - m;
2709 else
2710 i = 0;
2711 for (; i < defcount; i++) {
2712 if (GETLOCAL(m+i) == NULL) {
2713 PyObject *def = defs[i];
2714 Py_INCREF(def);
2715 SETLOCAL(m+i, def);
2716 }
2717 }
2718 }
2719 }
2720 else {
2721 if (argcount > 0 || kwcount > 0) {
2722 PyErr_Format(PyExc_TypeError,
2723 "%.200s() takes no arguments (%d given)",
2724 PyString_AsString(co->co_name),
2725 argcount + kwcount);
2726 goto fail;
2727 }
2728 }
2729 /* Allocate and initialize storage for cell vars, and copy free
2730 vars into frame. This isn't too efficient right now. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002731 if (PyTuple_GET_SIZE(co->co_cellvars)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002732 int i = 0, j = 0, nargs, found;
2733 char *cellname, *argname;
2734 PyObject *c;
2735
2736 nargs = co->co_argcount;
2737 if (co->co_flags & CO_VARARGS)
2738 nargs++;
2739 if (co->co_flags & CO_VARKEYWORDS)
2740 nargs++;
2741
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002742 /* Initialize each cell var, taking into account
2743 cell vars that are initialized from arguments.
2744
2745 Should arrange for the compiler to put cellvars
2746 that are arguments at the beginning of the cellvars
2747 list so that we can march over it more efficiently?
2748 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002749 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002750 cellname = PyString_AS_STRING(
2751 PyTuple_GET_ITEM(co->co_cellvars, i));
2752 found = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002753 for (j = 0; j < nargs; j++) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002754 argname = PyString_AS_STRING(
2755 PyTuple_GET_ITEM(co->co_varnames, j));
2756 if (strcmp(cellname, argname) == 0) {
2757 c = PyCell_New(GETLOCAL(j));
2758 if (c == NULL)
2759 goto fail;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002760 GETLOCAL(co->co_nlocals + i) = c;
Tim Peters5ca576e2001-06-18 22:08:13 +00002761 found = 1;
2762 break;
2763 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002764 }
2765 if (found == 0) {
2766 c = PyCell_New(NULL);
2767 if (c == NULL)
2768 goto fail;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002769 SETLOCAL(co->co_nlocals + i, c);
Tim Peters5ca576e2001-06-18 22:08:13 +00002770 }
2771 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002772 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002773 if (PyTuple_GET_SIZE(co->co_freevars)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002774 int i;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002775 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002776 PyObject *o = PyTuple_GET_ITEM(closure, i);
2777 Py_INCREF(o);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002778 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Tim Peters5ca576e2001-06-18 22:08:13 +00002779 }
2780 }
2781
Tim Peters5ca576e2001-06-18 22:08:13 +00002782 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002783 /* Don't need to keep the reference to f_back, it will be set
2784 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002785 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002786 f->f_back = NULL;
2787
Jeremy Hylton985eba52003-02-05 23:13:00 +00002788 PCALL(PCALL_GENERATOR);
2789
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002790 /* Create a new generator that owns the ready to run frame
2791 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00002792 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00002793 }
2794
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002795 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00002796
2797 fail: /* Jump here from prelude on failure */
2798
Tim Petersb13680b2001-11-27 23:29:29 +00002799 /* decref'ing the frame can cause __del__ methods to get invoked,
2800 which can call back into Python. While we're done with the
2801 current Python frame (f), the associated C stack is still in use,
2802 so recursion_depth must be boosted for the duration.
2803 */
2804 assert(tstate != NULL);
2805 ++tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002806 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00002807 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002808 return retval;
2809}
2810
2811
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002812/* Implementation notes for set_exc_info() and reset_exc_info():
2813
2814- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2815 'exc_traceback'. These always travel together.
2816
2817- tstate->curexc_ZZZ is the "hot" exception that is set by
2818 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2819
2820- Once an exception is caught by an except clause, it is transferred
2821 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2822 can pick it up. This is the primary task of set_exc_info().
Thomas Wouters477c8d52006-05-27 19:21:47 +00002823 XXX That can't be right: set_exc_info() doesn't look at tstate->curexc_ZZZ.
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002824
2825- Now let me explain the complicated dance with frame->f_exc_ZZZ.
2826
2827 Long ago, when none of this existed, there were just a few globals:
2828 one set corresponding to the "hot" exception, and one set
2829 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2830 globals; they were simply stored as sys.exc_ZZZ. For backwards
2831 compatibility, they still are!) The problem was that in code like
2832 this:
2833
2834 try:
2835 "something that may fail"
2836 except "some exception":
2837 "do something else first"
2838 "print the exception from sys.exc_ZZZ."
2839
2840 if "do something else first" invoked something that raised and caught
2841 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2842 cause of subtle bugs. I fixed this by changing the semantics as
2843 follows:
2844
2845 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2846 *in that frame*.
2847
2848 - But initially, and as long as no exception is caught in a given
2849 frame, sys.exc_ZZZ will hold the last exception caught in the
2850 previous frame (or the frame before that, etc.).
2851
2852 The first bullet fixed the bug in the above example. The second
2853 bullet was for backwards compatibility: it was (and is) common to
2854 have a function that is called when an exception is caught, and to
2855 have that function access the caught exception via sys.exc_ZZZ.
2856 (Example: traceback.print_exc()).
2857
2858 At the same time I fixed the problem that sys.exc_ZZZ weren't
2859 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2860 but that's really a separate improvement.
2861
2862 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2863 variables to what they were before the current frame was called. The
2864 set_exc_info() function saves them on the frame so that
2865 reset_exc_info() can restore them. The invariant is that
2866 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2867 exception (where "catching" an exception applies only to successful
2868 except clauses); and if the current frame ever caught an exception,
2869 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2870 at the start of the current frame.
2871
2872*/
2873
Guido van Rossuma027efa1997-05-05 20:56:21 +00002874static void
Guido van Rossumac7be682001-01-17 15:42:30 +00002875set_exc_info(PyThreadState *tstate,
2876 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002877{
Thomas Wouters477c8d52006-05-27 19:21:47 +00002878 PyFrameObject *frame = tstate->frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002879 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00002880
Thomas Wouters477c8d52006-05-27 19:21:47 +00002881 assert(type != NULL);
2882 assert(frame != NULL);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002883 if (frame->f_exc_type == NULL) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002884 assert(frame->f_exc_value == NULL);
2885 assert(frame->f_exc_traceback == NULL);
2886 /* This frame didn't catch an exception before. */
2887 /* Save previous exception of this thread in this frame. */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002888 if (tstate->exc_type == NULL) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002889 /* XXX Why is this set to Py_None? */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002890 Py_INCREF(Py_None);
2891 tstate->exc_type = Py_None;
2892 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002893 Py_INCREF(tstate->exc_type);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002894 Py_XINCREF(tstate->exc_value);
2895 Py_XINCREF(tstate->exc_traceback);
2896 frame->f_exc_type = tstate->exc_type;
2897 frame->f_exc_value = tstate->exc_value;
2898 frame->f_exc_traceback = tstate->exc_traceback;
2899 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002900 /* Set new exception for this thread. */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002901 tmp_type = tstate->exc_type;
2902 tmp_value = tstate->exc_value;
2903 tmp_tb = tstate->exc_traceback;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002904 Py_INCREF(type);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002905 Py_XINCREF(value);
2906 Py_XINCREF(tb);
2907 tstate->exc_type = type;
2908 tstate->exc_value = value;
2909 tstate->exc_traceback = tb;
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 /* For b/w compatibility */
2914 PySys_SetObject("exc_type", type);
2915 PySys_SetObject("exc_value", value);
2916 PySys_SetObject("exc_traceback", tb);
2917}
2918
2919static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002920reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002921{
2922 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002923 PyObject *tmp_type, *tmp_value, *tmp_tb;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002924
2925 /* It's a precondition that the thread state's frame caught an
2926 * exception -- verify in a debug build.
2927 */
2928 assert(tstate != NULL);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002929 frame = tstate->frame;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002930 assert(frame != NULL);
2931 assert(frame->f_exc_type != NULL);
2932
2933 /* Copy the frame's exception info back to the thread state. */
2934 tmp_type = tstate->exc_type;
2935 tmp_value = tstate->exc_value;
2936 tmp_tb = tstate->exc_traceback;
2937 Py_INCREF(frame->f_exc_type);
2938 Py_XINCREF(frame->f_exc_value);
2939 Py_XINCREF(frame->f_exc_traceback);
2940 tstate->exc_type = frame->f_exc_type;
2941 tstate->exc_value = frame->f_exc_value;
2942 tstate->exc_traceback = frame->f_exc_traceback;
2943 Py_XDECREF(tmp_type);
2944 Py_XDECREF(tmp_value);
2945 Py_XDECREF(tmp_tb);
2946
2947 /* For b/w compatibility */
2948 PySys_SetObject("exc_type", frame->f_exc_type);
2949 PySys_SetObject("exc_value", frame->f_exc_value);
2950 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
2951
2952 /* Clear the frame's exception info. */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002953 tmp_type = frame->f_exc_type;
2954 tmp_value = frame->f_exc_value;
2955 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002956 frame->f_exc_type = NULL;
2957 frame->f_exc_value = NULL;
2958 frame->f_exc_traceback = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002959 Py_DECREF(tmp_type);
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002960 Py_XDECREF(tmp_value);
2961 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002962}
2963
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002964/* Logic for the raise statement (too complicated for inlining).
2965 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00002966static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002967do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002968{
Guido van Rossumd295f121998-04-09 21:39:57 +00002969 if (type == NULL) {
2970 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00002971 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumd295f121998-04-09 21:39:57 +00002972 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
2973 value = tstate->exc_value;
2974 tb = tstate->exc_traceback;
2975 Py_XINCREF(type);
2976 Py_XINCREF(value);
2977 Py_XINCREF(tb);
2978 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002979
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002980 /* We support the following forms of raise:
2981 raise <class>, <classinstance>
2982 raise <class>, <argument tuple>
2983 raise <class>, None
2984 raise <class>, <argument>
2985 raise <classinstance>, None
2986 raise <string>, <object>
2987 raise <string>, None
2988
2989 An omitted second argument is the same as None.
2990
2991 In addition, raise <tuple>, <anything> is the same as
2992 raising the tuple's first item (and it better have one!);
2993 this rule is applied recursively.
2994
2995 Finally, an optional third argument can be supplied, which
2996 gives the traceback to be substituted (useful when
2997 re-raising an exception after examining it). */
2998
2999 /* First, check the traceback argument, replacing None with
3000 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00003001 if (tb == Py_None) {
3002 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003003 tb = NULL;
3004 }
3005 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003006 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003007 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003008 goto raise_error;
3009 }
3010
3011 /* Next, replace a missing value with None */
3012 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003013 value = Py_None;
3014 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003015 }
3016
3017 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00003018 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
3019 PyObject *tmp = type;
3020 type = PyTuple_GET_ITEM(type, 0);
3021 Py_INCREF(type);
3022 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003023 }
3024
Guido van Rossum45aecf42006-03-15 04:58:47 +00003025 if (PyExceptionClass_Check(type))
Barry Warsaw4249f541997-08-22 21:26:19 +00003026 PyErr_NormalizeException(&type, &value, &tb);
3027
Brett Cannonbf364092006-03-01 04:25:17 +00003028 else if (PyExceptionInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003029 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00003030 if (value != Py_None) {
3031 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003032 "instance exception may not have a separate value");
3033 goto raise_error;
3034 }
3035 else {
3036 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00003037 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003038 value = type;
Brett Cannonbf364092006-03-01 04:25:17 +00003039 type = PyExceptionInstance_Class(type);
Guido van Rossumb209a111997-04-29 18:18:01 +00003040 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003041 }
3042 }
3043 else {
3044 /* Not something you can raise. You get an exception
3045 anyway, just not what you specified :-) */
Guido van Rossum45aecf42006-03-15 04:58:47 +00003046 PyErr_SetString(PyExc_TypeError,
3047 "exceptions must derive from BaseException");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003048 goto raise_error;
3049 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003050 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003051 if (tb == NULL)
3052 return WHY_EXCEPTION;
3053 else
3054 return WHY_RERAISE;
3055 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00003056 Py_XDECREF(value);
3057 Py_XDECREF(type);
3058 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003059 return WHY_EXCEPTION;
3060}
3061
Tim Petersd6d010b2001-06-21 02:49:55 +00003062/* Iterate v argcnt times and store the results on the stack (via decreasing
3063 sp). Return 1 for success, 0 if error. */
3064
Barry Warsawe42b18f1997-08-25 22:13:04 +00003065static int
Tim Petersd6d010b2001-06-21 02:49:55 +00003066unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003067{
Tim Petersd6d010b2001-06-21 02:49:55 +00003068 int i = 0;
3069 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00003070 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00003071
Tim Petersd6d010b2001-06-21 02:49:55 +00003072 assert(v != NULL);
3073
3074 it = PyObject_GetIter(v);
3075 if (it == NULL)
3076 goto Error;
3077
3078 for (; i < argcnt; i++) {
3079 w = PyIter_Next(it);
3080 if (w == NULL) {
3081 /* Iterator done, via error or exhaustion. */
3082 if (!PyErr_Occurred()) {
3083 PyErr_Format(PyExc_ValueError,
3084 "need more than %d value%s to unpack",
3085 i, i == 1 ? "" : "s");
3086 }
3087 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003088 }
3089 *--sp = w;
3090 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003091
3092 /* We better have exhausted the iterator now. */
3093 w = PyIter_Next(it);
3094 if (w == NULL) {
3095 if (PyErr_Occurred())
3096 goto Error;
3097 Py_DECREF(it);
3098 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003099 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00003100 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00003101 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00003102 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00003103Error:
Barry Warsaw91010551997-08-25 22:30:51 +00003104 for (; i > 0; i--, sp++)
3105 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00003106 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00003107 return 0;
3108}
3109
3110
Guido van Rossum96a42c81992-01-12 02:29:51 +00003111#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003112static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003113prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003114{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003115 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003116 if (PyObject_Print(v, stdout, 0) != 0)
3117 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003118 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003119 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003120}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003121#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003122
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003123static void
Fred Drake5755ce62001-06-27 19:19:46 +00003124call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003125{
Guido van Rossumb209a111997-04-29 18:18:01 +00003126 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003127 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003128 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003129 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003130 value = Py_None;
3131 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003132 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003133 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003134 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003135 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003136 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003137 }
Fred Drake5755ce62001-06-27 19:19:46 +00003138 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003139 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003140 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003141 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003142 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003143 Py_XDECREF(type);
3144 Py_XDECREF(value);
3145 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003146 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003147}
3148
Fred Drake4ec5d562001-10-04 19:26:43 +00003149static void
3150call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003151 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003152{
3153 PyObject *type, *value, *traceback;
3154 int err;
3155 PyErr_Fetch(&type, &value, &traceback);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003156 err = call_trace(func, obj, frame, what, arg);
Fred Drake4ec5d562001-10-04 19:26:43 +00003157 if (err == 0)
3158 PyErr_Restore(type, value, traceback);
3159 else {
3160 Py_XDECREF(type);
3161 Py_XDECREF(value);
3162 Py_XDECREF(traceback);
3163 }
3164}
3165
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003166static int
Fred Drake5755ce62001-06-27 19:19:46 +00003167call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3168 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003169{
Fred Drake5755ce62001-06-27 19:19:46 +00003170 register PyThreadState *tstate = frame->f_tstate;
3171 int result;
3172 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003173 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003174 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003175 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003176 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003177 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3178 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003179 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003180 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003181}
3182
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003183PyObject *
3184_PyEval_CallTracing(PyObject *func, PyObject *args)
3185{
3186 PyFrameObject *frame = PyEval_GetFrame();
3187 PyThreadState *tstate = frame->f_tstate;
3188 int save_tracing = tstate->tracing;
3189 int save_use_tracing = tstate->use_tracing;
3190 PyObject *result;
3191
3192 tstate->tracing = 0;
3193 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3194 || (tstate->c_profilefunc != NULL));
3195 result = PyObject_Call(func, args, NULL);
3196 tstate->tracing = save_tracing;
3197 tstate->use_tracing = save_use_tracing;
3198 return result;
3199}
3200
Michael W. Hudson006c7522002-11-08 13:08:46 +00003201static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003202maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003203 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3204 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003205{
Michael W. Hudson006c7522002-11-08 13:08:46 +00003206 int result = 0;
3207
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003208 /* If the last instruction executed isn't in the current
3209 instruction window, reset the window. If the last
3210 instruction happens to fall at the start of a line or if it
3211 represents a jump backwards, call the trace function.
3212 */
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003213 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003214 int line;
3215 PyAddrPair bounds;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003216
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003217 line = PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3218 &bounds);
3219 if (line >= 0) {
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003220 frame->f_lineno = line;
Tim Peters8a5c3c72004-04-05 19:36:21 +00003221 result = call_trace(func, obj, frame,
Michael W. Hudson006c7522002-11-08 13:08:46 +00003222 PyTrace_LINE, Py_None);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003223 }
3224 *instr_lb = bounds.ap_lower;
3225 *instr_ub = bounds.ap_upper;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003226 }
Armin Rigobf57a142004-03-22 19:24:58 +00003227 else if (frame->f_lasti <= *instr_prev) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003228 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
Armin Rigobf57a142004-03-22 19:24:58 +00003229 }
3230 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003231 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003232}
3233
Fred Drake5755ce62001-06-27 19:19:46 +00003234void
3235PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003236{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003237 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003238 PyObject *temp = tstate->c_profileobj;
3239 Py_XINCREF(arg);
3240 tstate->c_profilefunc = NULL;
3241 tstate->c_profileobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003242 /* Must make sure that tracing is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003243 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003244 Py_XDECREF(temp);
3245 tstate->c_profilefunc = func;
3246 tstate->c_profileobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003247 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003248 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003249}
3250
3251void
3252PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3253{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003254 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003255 PyObject *temp = tstate->c_traceobj;
3256 Py_XINCREF(arg);
3257 tstate->c_tracefunc = NULL;
3258 tstate->c_traceobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003259 /* Must make sure that profiling is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003260 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003261 Py_XDECREF(temp);
3262 tstate->c_tracefunc = func;
3263 tstate->c_traceobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003264 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003265 tstate->use_tracing = ((func != NULL)
3266 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003267}
3268
Guido van Rossumb209a111997-04-29 18:18:01 +00003269PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003270PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003271{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003272 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003273 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003274 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003275 else
3276 return current_frame->f_builtins;
3277}
3278
Guido van Rossumb209a111997-04-29 18:18:01 +00003279PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003280PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003281{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003282 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003283 if (current_frame == NULL)
3284 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003285 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003286 return current_frame->f_locals;
3287}
3288
Guido van Rossumb209a111997-04-29 18:18:01 +00003289PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003290PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003291{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003292 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003293 if (current_frame == NULL)
3294 return NULL;
3295 else
3296 return current_frame->f_globals;
3297}
3298
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003299PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003300PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003301{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003302 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003303 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003304}
3305
Guido van Rossum6135a871995-01-09 17:53:26 +00003306int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003307PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003308{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003309 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003310 return current_frame == NULL ? 0 : current_frame->f_restricted;
3311}
3312
Guido van Rossumbe270261997-05-22 22:26:18 +00003313int
Tim Peters5ba58662001-07-16 02:29:45 +00003314PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003315{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003316 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003317 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003318
3319 if (current_frame != NULL) {
3320 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003321 const int compilerflags = codeflags & PyCF_MASK;
3322 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003323 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003324 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003325 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003326#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003327 if (codeflags & CO_GENERATOR_ALLOWED) {
3328 result = 1;
3329 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3330 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003331#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003332 }
3333 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003334}
3335
3336int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003337Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003338{
Guido van Rossumb209a111997-04-29 18:18:01 +00003339 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003340 if (f == NULL)
3341 return 0;
3342 if (!PyFile_SoftSpace(f, 0))
3343 return 0;
3344 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003345}
3346
Guido van Rossum3f5da241990-12-20 15:06:42 +00003347
Guido van Rossum681d79a1995-07-18 14:51:37 +00003348/* External interface to call any callable object.
3349 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003350
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003351#undef PyEval_CallObject
3352/* for backward compatibility: export this interface */
3353
Guido van Rossumb209a111997-04-29 18:18:01 +00003354PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003355PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003356{
Guido van Rossumb209a111997-04-29 18:18:01 +00003357 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003358}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003359#define PyEval_CallObject(func,arg) \
3360 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003361
Guido van Rossumb209a111997-04-29 18:18:01 +00003362PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003363PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003364{
Jeremy Hylton52820442001-01-03 23:52:36 +00003365 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003366
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003367 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003368 arg = PyTuple_New(0);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003369 if (arg == NULL)
3370 return NULL;
3371 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003372 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003373 PyErr_SetString(PyExc_TypeError,
3374 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003375 return NULL;
3376 }
3377 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003378 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003379
Guido van Rossumb209a111997-04-29 18:18:01 +00003380 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003381 PyErr_SetString(PyExc_TypeError,
3382 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003383 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003384 return NULL;
3385 }
3386
Tim Peters6d6c1a32001-08-02 04:15:00 +00003387 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003388 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003389 return result;
3390}
3391
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003392const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003393PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003394{
3395 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003396 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003397 else if (PyFunction_Check(func))
3398 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3399 else if (PyCFunction_Check(func))
3400 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3401 else if (PyClass_Check(func))
3402 return PyString_AsString(((PyClassObject*)func)->cl_name);
3403 else if (PyInstance_Check(func)) {
3404 return PyString_AsString(
3405 ((PyInstanceObject*)func)->in_class->cl_name);
3406 } else {
3407 return func->ob_type->tp_name;
3408 }
3409}
3410
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003411const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003412PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003413{
3414 if (PyMethod_Check(func))
3415 return "()";
3416 else if (PyFunction_Check(func))
3417 return "()";
3418 else if (PyCFunction_Check(func))
3419 return "()";
3420 else if (PyClass_Check(func))
3421 return " constructor";
3422 else if (PyInstance_Check(func)) {
3423 return " instance";
3424 } else {
3425 return " object";
3426 }
3427}
3428
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003429static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003430err_args(PyObject *func, int flags, int nargs)
3431{
3432 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003433 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003434 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003435 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003436 nargs);
3437 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003438 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003439 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003440 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003441 nargs);
3442}
3443
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003444#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003445if (tstate->use_tracing && tstate->c_profilefunc) { \
3446 if (call_trace(tstate->c_profilefunc, \
3447 tstate->c_profileobj, \
3448 tstate->frame, PyTrace_C_CALL, \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003449 func)) { \
3450 x = NULL; \
3451 } \
3452 else { \
3453 x = call; \
3454 if (tstate->c_profilefunc != NULL) { \
3455 if (x == NULL) { \
3456 call_trace_protected(tstate->c_profilefunc, \
3457 tstate->c_profileobj, \
3458 tstate->frame, PyTrace_C_EXCEPTION, \
3459 func); \
3460 /* XXX should pass (type, value, tb) */ \
3461 } else { \
3462 if (call_trace(tstate->c_profilefunc, \
3463 tstate->c_profileobj, \
3464 tstate->frame, PyTrace_C_RETURN, \
3465 func)) { \
3466 Py_DECREF(x); \
3467 x = NULL; \
3468 } \
3469 } \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003470 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003471 } \
3472} else { \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003473 x = call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003474 }
3475
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003476static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003477call_function(PyObject ***pp_stack, int oparg
3478#ifdef WITH_TSC
3479 , uint64* pintr0, uint64* pintr1
3480#endif
3481 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003482{
3483 int na = oparg & 0xff;
3484 int nk = (oparg>>8) & 0xff;
3485 int n = na + 2 * nk;
3486 PyObject **pfunc = (*pp_stack) - n - 1;
3487 PyObject *func = *pfunc;
3488 PyObject *x, *w;
3489
Jeremy Hylton985eba52003-02-05 23:13:00 +00003490 /* Always dispatch PyCFunction first, because these are
3491 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003492 */
3493 if (PyCFunction_Check(func) && nk == 0) {
3494 int flags = PyCFunction_GET_FLAGS(func);
Nicholas Bastind858a772004-06-25 23:31:06 +00003495 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003496
3497 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003498 if (flags & (METH_NOARGS | METH_O)) {
3499 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3500 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003501 if (flags & METH_NOARGS && na == 0) {
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003502 C_TRACE(x, (*meth)(self,NULL));
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003503 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003504 else if (flags & METH_O && na == 1) {
3505 PyObject *arg = EXT_POP(*pp_stack);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003506 C_TRACE(x, (*meth)(self,arg));
Jeremy Hylton192690e2002-08-16 18:36:11 +00003507 Py_DECREF(arg);
3508 }
3509 else {
3510 err_args(func, flags, na);
3511 x = NULL;
3512 }
3513 }
3514 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003515 PyObject *callargs;
3516 callargs = load_args(pp_stack, na);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003517 READ_TIMESTAMP(*pintr0);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003518 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003519 READ_TIMESTAMP(*pintr1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003520 Py_XDECREF(callargs);
3521 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003522 } else {
3523 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3524 /* optimize access to bound methods */
3525 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003526 PCALL(PCALL_METHOD);
3527 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003528 Py_INCREF(self);
3529 func = PyMethod_GET_FUNCTION(func);
3530 Py_INCREF(func);
3531 Py_DECREF(*pfunc);
3532 *pfunc = self;
3533 na++;
3534 n++;
3535 } else
3536 Py_INCREF(func);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003537 READ_TIMESTAMP(*pintr0);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003538 if (PyFunction_Check(func))
3539 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003540 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003541 x = do_call(func, pp_stack, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003542 READ_TIMESTAMP(*pintr1);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003543 Py_DECREF(func);
3544 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003545
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003546 /* Clear the stack of the function object. Also removes
3547 the arguments in case they weren't consumed already
3548 (fast_function() and err_args() leave them on the stack).
Thomas Wouters7f597322006-03-01 05:32:33 +00003549 */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003550 while ((*pp_stack) > pfunc) {
3551 w = EXT_POP(*pp_stack);
3552 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003553 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003554 }
3555 return x;
3556}
3557
Jeremy Hylton192690e2002-08-16 18:36:11 +00003558/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003559 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003560 For the simplest case -- a function that takes only positional
3561 arguments and is called with only positional arguments -- it
3562 inlines the most primitive frame setup code from
3563 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3564 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003565*/
3566
3567static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003568fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003569{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003570 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003571 PyObject *globals = PyFunction_GET_GLOBALS(func);
3572 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3573 PyObject **d = NULL;
3574 int nd = 0;
3575
Jeremy Hylton985eba52003-02-05 23:13:00 +00003576 PCALL(PCALL_FUNCTION);
3577 PCALL(PCALL_FAST_FUNCTION);
Raymond Hettinger40174c32003-05-31 07:04:16 +00003578 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003579 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3580 PyFrameObject *f;
3581 PyObject *retval = NULL;
3582 PyThreadState *tstate = PyThreadState_GET();
3583 PyObject **fastlocals, **stack;
3584 int i;
3585
3586 PCALL(PCALL_FASTER_FUNCTION);
3587 assert(globals != NULL);
3588 /* XXX Perhaps we should create a specialized
3589 PyFrame_New() that doesn't take locals, but does
3590 take builtins without sanity checking them.
3591 */
3592 f = PyFrame_New(tstate, co, globals, NULL);
3593 if (f == NULL)
3594 return NULL;
3595
3596 fastlocals = f->f_localsplus;
3597 stack = (*pp_stack) - n;
3598
3599 for (i = 0; i < n; i++) {
3600 Py_INCREF(*stack);
3601 fastlocals[i] = *stack++;
3602 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003603 retval = PyEval_EvalFrameEx(f,0);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003604 assert(tstate != NULL);
3605 ++tstate->recursion_depth;
3606 Py_DECREF(f);
3607 --tstate->recursion_depth;
3608 return retval;
3609 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003610 if (argdefs != NULL) {
3611 d = &PyTuple_GET_ITEM(argdefs, 0);
3612 nd = ((PyTupleObject *)argdefs)->ob_size;
3613 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003614 return PyEval_EvalCodeEx(co, globals,
3615 (PyObject *)NULL, (*pp_stack)-n, na,
3616 (*pp_stack)-2*nk, nk, d, nd,
3617 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003618}
3619
3620static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003621update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3622 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003623{
3624 PyObject *kwdict = NULL;
3625 if (orig_kwdict == NULL)
3626 kwdict = PyDict_New();
3627 else {
3628 kwdict = PyDict_Copy(orig_kwdict);
3629 Py_DECREF(orig_kwdict);
3630 }
3631 if (kwdict == NULL)
3632 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003633 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003634 int err;
3635 PyObject *value = EXT_POP(*pp_stack);
3636 PyObject *key = EXT_POP(*pp_stack);
3637 if (PyDict_GetItem(kwdict, key) != NULL) {
Guido van Rossumac7be682001-01-17 15:42:30 +00003638 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +00003639 "%.200s%s got multiple values "
Jeremy Hylton512a2372001-04-11 13:52:29 +00003640 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003641 PyEval_GetFuncName(func),
3642 PyEval_GetFuncDesc(func),
Jeremy Hylton512a2372001-04-11 13:52:29 +00003643 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003644 Py_DECREF(key);
3645 Py_DECREF(value);
3646 Py_DECREF(kwdict);
3647 return NULL;
3648 }
3649 err = PyDict_SetItem(kwdict, key, value);
3650 Py_DECREF(key);
3651 Py_DECREF(value);
3652 if (err) {
3653 Py_DECREF(kwdict);
3654 return NULL;
3655 }
3656 }
3657 return kwdict;
3658}
3659
3660static PyObject *
3661update_star_args(int nstack, int nstar, PyObject *stararg,
3662 PyObject ***pp_stack)
3663{
3664 PyObject *callargs, *w;
3665
3666 callargs = PyTuple_New(nstack + nstar);
3667 if (callargs == NULL) {
3668 return NULL;
3669 }
3670 if (nstar) {
3671 int i;
3672 for (i = 0; i < nstar; i++) {
3673 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3674 Py_INCREF(a);
3675 PyTuple_SET_ITEM(callargs, nstack + i, a);
3676 }
3677 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003678 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003679 w = EXT_POP(*pp_stack);
3680 PyTuple_SET_ITEM(callargs, nstack, w);
3681 }
3682 return callargs;
3683}
3684
3685static PyObject *
3686load_args(PyObject ***pp_stack, int na)
3687{
3688 PyObject *args = PyTuple_New(na);
3689 PyObject *w;
3690
3691 if (args == NULL)
3692 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003693 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003694 w = EXT_POP(*pp_stack);
3695 PyTuple_SET_ITEM(args, na, w);
3696 }
3697 return args;
3698}
3699
3700static PyObject *
3701do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3702{
3703 PyObject *callargs = NULL;
3704 PyObject *kwdict = NULL;
3705 PyObject *result = NULL;
3706
3707 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003708 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003709 if (kwdict == NULL)
3710 goto call_fail;
3711 }
3712 callargs = load_args(pp_stack, na);
3713 if (callargs == NULL)
3714 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003715#ifdef CALL_PROFILE
3716 /* At this point, we have to look at the type of func to
3717 update the call stats properly. Do it here so as to avoid
3718 exposing the call stats machinery outside ceval.c
3719 */
3720 if (PyFunction_Check(func))
3721 PCALL(PCALL_FUNCTION);
3722 else if (PyMethod_Check(func))
3723 PCALL(PCALL_METHOD);
3724 else if (PyType_Check(func))
3725 PCALL(PCALL_TYPE);
3726 else
3727 PCALL(PCALL_OTHER);
3728#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003729 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003730 call_fail:
3731 Py_XDECREF(callargs);
3732 Py_XDECREF(kwdict);
3733 return result;
3734}
3735
3736static PyObject *
3737ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3738{
3739 int nstar = 0;
3740 PyObject *callargs = NULL;
3741 PyObject *stararg = NULL;
3742 PyObject *kwdict = NULL;
3743 PyObject *result = NULL;
3744
3745 if (flags & CALL_FLAG_KW) {
3746 kwdict = EXT_POP(*pp_stack);
3747 if (!(kwdict && PyDict_Check(kwdict))) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003748 PyErr_Format(PyExc_TypeError,
Jeremy Hylton512a2372001-04-11 13:52:29 +00003749 "%s%s argument after ** "
3750 "must be a dictionary",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003751 PyEval_GetFuncName(func),
3752 PyEval_GetFuncDesc(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003753 goto ext_call_fail;
3754 }
3755 }
3756 if (flags & CALL_FLAG_VAR) {
3757 stararg = EXT_POP(*pp_stack);
3758 if (!PyTuple_Check(stararg)) {
3759 PyObject *t = NULL;
3760 t = PySequence_Tuple(stararg);
3761 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003762 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3763 PyErr_Format(PyExc_TypeError,
3764 "%s%s argument after * "
3765 "must be a sequence",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003766 PyEval_GetFuncName(func),
3767 PyEval_GetFuncDesc(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003768 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003769 goto ext_call_fail;
3770 }
3771 Py_DECREF(stararg);
3772 stararg = t;
3773 }
3774 nstar = PyTuple_GET_SIZE(stararg);
3775 }
3776 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003777 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003778 if (kwdict == NULL)
3779 goto ext_call_fail;
3780 }
3781 callargs = update_star_args(na, nstar, stararg, pp_stack);
3782 if (callargs == NULL)
3783 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003784#ifdef CALL_PROFILE
3785 /* At this point, we have to look at the type of func to
3786 update the call stats properly. Do it here so as to avoid
3787 exposing the call stats machinery outside ceval.c
3788 */
3789 if (PyFunction_Check(func))
3790 PCALL(PCALL_FUNCTION);
3791 else if (PyMethod_Check(func))
3792 PCALL(PCALL_METHOD);
3793 else if (PyType_Check(func))
3794 PCALL(PCALL_TYPE);
3795 else
3796 PCALL(PCALL_OTHER);
3797#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003798 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003799 ext_call_fail:
3800 Py_XDECREF(callargs);
3801 Py_XDECREF(kwdict);
3802 Py_XDECREF(stararg);
3803 return result;
3804}
3805
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003806/* Extract a slice index from a PyInt or PyLong or an object with the
3807 nb_index slot defined, and store in *pi.
3808 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
3809 and silently boost values less than -PY_SSIZE_T_MAX-1 to -PY_SSIZE_T_MAX-1.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00003810 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00003811*/
Tim Petersb5196382001-12-16 19:44:20 +00003812/* Note: If v is NULL, return success without storing into *pi. This
3813 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3814 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00003815*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00003816int
Martin v. Löwis18e16552006-02-15 17:27:45 +00003817_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003818{
Tim Petersb5196382001-12-16 19:44:20 +00003819 if (v != NULL) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00003820 Py_ssize_t x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003821 if (PyInt_Check(v)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003822 x = PyInt_AsSsize_t(v);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003823 }
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003824 else if (v->ob_type->tp_as_number &&
3825 PyType_HasFeature(v->ob_type, Py_TPFLAGS_HAVE_INDEX)
3826 && v->ob_type->tp_as_number->nb_index) {
3827 x = v->ob_type->tp_as_number->nb_index(v);
3828 if (x == -1 && PyErr_Occurred())
3829 return 0;
3830 }
3831 else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003832 PyErr_SetString(PyExc_TypeError,
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003833 "slice indices must be integers or "
3834 "None or have an __index__ method");
Guido van Rossum20c6add2000-05-08 14:06:50 +00003835 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003836 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003837 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003838 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00003839 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003840}
3841
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003842#undef ISINDEX
3843#define ISINDEX(x) ((x) == NULL || PyInt_Check(x) || PyLong_Check(x) || \
3844 ((x)->ob_type->tp_as_number && \
3845 PyType_HasFeature((x)->ob_type, Py_TPFLAGS_HAVE_INDEX) \
3846 && (x)->ob_type->tp_as_number->nb_index))
Guido van Rossum50d756e2001-08-18 17:43:36 +00003847
Guido van Rossumb209a111997-04-29 18:18:01 +00003848static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003849apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003850{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003851 PyTypeObject *tp = u->ob_type;
3852 PySequenceMethods *sq = tp->tp_as_sequence;
3853
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003854 if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00003855 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003856 if (!_PyEval_SliceIndex(v, &ilow))
3857 return NULL;
3858 if (!_PyEval_SliceIndex(w, &ihigh))
3859 return NULL;
3860 return PySequence_GetSlice(u, ilow, ihigh);
3861 }
3862 else {
3863 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00003864 if (slice != NULL) {
3865 PyObject *res = PyObject_GetItem(u, slice);
3866 Py_DECREF(slice);
3867 return res;
3868 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00003869 else
3870 return NULL;
3871 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003872}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003873
3874static int
Guido van Rossumac7be682001-01-17 15:42:30 +00003875assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
3876 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003877{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003878 PyTypeObject *tp = u->ob_type;
3879 PySequenceMethods *sq = tp->tp_as_sequence;
3880
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003881 if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00003882 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003883 if (!_PyEval_SliceIndex(v, &ilow))
3884 return -1;
3885 if (!_PyEval_SliceIndex(w, &ihigh))
3886 return -1;
3887 if (x == NULL)
3888 return PySequence_DelSlice(u, ilow, ihigh);
3889 else
3890 return PySequence_SetSlice(u, ilow, ihigh, x);
3891 }
3892 else {
3893 PyObject *slice = PySlice_New(v, w, NULL);
3894 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00003895 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003896 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00003897 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00003898 else
Guido van Rossum354797c2001-12-03 19:45:06 +00003899 res = PyObject_DelItem(u, slice);
3900 Py_DECREF(slice);
3901 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003902 }
3903 else
3904 return -1;
3905 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003906}
3907
Guido van Rossumb209a111997-04-29 18:18:01 +00003908static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003909cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003910{
Guido van Rossumac7be682001-01-17 15:42:30 +00003911 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003912 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00003913 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003914 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003915 break;
3916 case PyCmp_IS_NOT:
3917 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003918 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003919 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003920 res = PySequence_Contains(w, v);
3921 if (res < 0)
3922 return NULL;
3923 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003924 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00003925 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003926 if (res < 0)
3927 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003928 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003929 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003930 case PyCmp_EXC_MATCH:
Barry Warsaw4249f541997-08-22 21:26:19 +00003931 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003932 break;
3933 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00003934 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003935 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003936 v = res ? Py_True : Py_False;
3937 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003938 return v;
3939}
3940
Thomas Wouters52152252000-08-17 22:55:00 +00003941static PyObject *
3942import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003943{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003944 PyObject *x;
3945
3946 x = PyObject_GetAttr(v, name);
3947 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00003948 PyErr_Format(PyExc_ImportError,
3949 "cannot import name %.230s",
3950 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003951 }
Thomas Wouters52152252000-08-17 22:55:00 +00003952 return x;
3953}
Guido van Rossumac7be682001-01-17 15:42:30 +00003954
Thomas Wouters52152252000-08-17 22:55:00 +00003955static int
3956import_all_from(PyObject *locals, PyObject *v)
3957{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003958 PyObject *all = PyObject_GetAttrString(v, "__all__");
3959 PyObject *dict, *name, *value;
3960 int skip_leading_underscores = 0;
3961 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00003962
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003963 if (all == NULL) {
3964 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3965 return -1; /* Unexpected error */
3966 PyErr_Clear();
3967 dict = PyObject_GetAttrString(v, "__dict__");
3968 if (dict == NULL) {
3969 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3970 return -1;
3971 PyErr_SetString(PyExc_ImportError,
3972 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00003973 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003974 }
3975 all = PyMapping_Keys(dict);
3976 Py_DECREF(dict);
3977 if (all == NULL)
3978 return -1;
3979 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00003980 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003981
3982 for (pos = 0, err = 0; ; pos++) {
3983 name = PySequence_GetItem(all, pos);
3984 if (name == NULL) {
3985 if (!PyErr_ExceptionMatches(PyExc_IndexError))
3986 err = -1;
3987 else
3988 PyErr_Clear();
3989 break;
3990 }
3991 if (skip_leading_underscores &&
3992 PyString_Check(name) &&
3993 PyString_AS_STRING(name)[0] == '_')
3994 {
3995 Py_DECREF(name);
3996 continue;
3997 }
3998 value = PyObject_GetAttr(v, name);
3999 if (value == NULL)
4000 err = -1;
4001 else
4002 err = PyDict_SetItem(locals, name, value);
4003 Py_DECREF(name);
4004 Py_XDECREF(value);
4005 if (err != 0)
4006 break;
4007 }
4008 Py_DECREF(all);
4009 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004010}
4011
Guido van Rossumb209a111997-04-29 18:18:01 +00004012static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004013build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004014{
Guido van Rossum7851eea2001-09-12 19:19:18 +00004015 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004016
4017 if (PyDict_Check(methods))
4018 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00004019 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00004020 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004021 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4022 base = PyTuple_GET_ITEM(bases, 0);
4023 metaclass = PyObject_GetAttrString(base, "__class__");
4024 if (metaclass == NULL) {
4025 PyErr_Clear();
4026 metaclass = (PyObject *)base->ob_type;
4027 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00004028 }
4029 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004030 else {
4031 PyObject *g = PyEval_GetGlobals();
4032 if (g != NULL && PyDict_Check(g))
4033 metaclass = PyDict_GetItemString(g, "__metaclass__");
4034 if (metaclass == NULL)
Guido van Rossum45aecf42006-03-15 04:58:47 +00004035 metaclass = (PyObject *) &PyType_Type;
Guido van Rossum7851eea2001-09-12 19:19:18 +00004036 Py_INCREF(metaclass);
4037 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00004038 result = PyObject_CallFunctionObjArgs(metaclass, name, bases, methods, NULL);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004039 Py_DECREF(metaclass);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004040 if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00004041 /* A type error here likely means that the user passed
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004042 in a base that was not a class (such the random module
4043 instead of the random.random type). Help them out with
Raymond Hettingercfc31922004-09-16 16:41:57 +00004044 by augmenting the error message with more information.*/
4045
4046 PyObject *ptype, *pvalue, *ptraceback;
4047
4048 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
4049 if (PyString_Check(pvalue)) {
4050 PyObject *newmsg;
4051 newmsg = PyString_FromFormat(
4052 "Error when calling the metaclass bases\n %s",
4053 PyString_AS_STRING(pvalue));
4054 if (newmsg != NULL) {
4055 Py_DECREF(pvalue);
4056 pvalue = newmsg;
4057 }
4058 }
4059 PyErr_Restore(ptype, pvalue, ptraceback);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004060 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004061 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00004062}
4063
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004064static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004065exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
4066 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004067{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004068 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00004069 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004070 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004071
Guido van Rossumb209a111997-04-29 18:18:01 +00004072 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
4073 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004074 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00004075 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004076 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00004077 locals = PyTuple_GetItem(prog, 2);
4078 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004079 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004080 if (globals == Py_None) {
4081 globals = PyEval_GetGlobals();
4082 if (locals == Py_None) {
4083 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00004084 plain = 1;
4085 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004086 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004087 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004088 locals = globals;
Guido van Rossumb209a111997-04-29 18:18:01 +00004089 if (!PyString_Check(prog) &&
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004090 !PyUnicode_Check(prog) &&
Guido van Rossumb209a111997-04-29 18:18:01 +00004091 !PyCode_Check(prog) &&
4092 !PyFile_Check(prog)) {
4093 PyErr_SetString(PyExc_TypeError,
Guido van Rossumac7be682001-01-17 15:42:30 +00004094 "exec: arg 1 must be a string, file, or code object");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004095 return -1;
4096 }
Fred Drake661ea262000-10-24 19:57:45 +00004097 if (!PyDict_Check(globals)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004098 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00004099 "exec: arg 2 must be a dictionary or None");
4100 return -1;
4101 }
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004102 if (!PyMapping_Check(locals)) {
Fred Drake661ea262000-10-24 19:57:45 +00004103 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004104 "exec: arg 3 must be a mapping or None");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004105 return -1;
4106 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004107 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00004108 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00004109 if (PyCode_Check(prog)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +00004110 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4111 PyErr_SetString(PyExc_TypeError,
4112 "code object passed to exec may not contain free variables");
4113 return -1;
4114 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004115 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004116 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004117 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004118 FILE *fp = PyFile_AsFile(prog);
4119 char *name = PyString_AsString(PyFile_Name(prog));
Tim Peters5ba58662001-07-16 02:29:45 +00004120 PyCompilerFlags cf;
4121 cf.cf_flags = 0;
4122 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004123 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004124 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004125 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004126 v = PyRun_File(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004127 locals);
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004128 }
4129 else {
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004130 PyObject *tmp = NULL;
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004131 char *str;
Tim Peters5ba58662001-07-16 02:29:45 +00004132 PyCompilerFlags cf;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004133 cf.cf_flags = 0;
4134#ifdef Py_USING_UNICODE
4135 if (PyUnicode_Check(prog)) {
4136 tmp = PyUnicode_AsUTF8String(prog);
4137 if (tmp == NULL)
4138 return -1;
4139 prog = tmp;
4140 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4141 }
4142#endif
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004143 if (PyString_AsStringAndSize(prog, &str, NULL))
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004144 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004145 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters8a5c3c72004-04-05 19:36:21 +00004146 v = PyRun_StringFlags(str, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004147 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004148 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004149 v = PyRun_String(str, Py_file_input, globals, locals);
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004150 Py_XDECREF(tmp);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004151 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004152 if (plain)
4153 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004154 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004155 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00004156 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004157 return 0;
4158}
Guido van Rossum24c13741995-02-14 09:42:43 +00004159
Guido van Rossumac7be682001-01-17 15:42:30 +00004160static void
Paul Prescode68140d2000-08-30 20:25:01 +00004161format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4162{
4163 char *obj_str;
4164
4165 if (!obj)
4166 return;
4167
4168 obj_str = PyString_AsString(obj);
4169 if (!obj_str)
4170 return;
4171
4172 PyErr_Format(exc, format_str, obj_str);
4173}
Guido van Rossum950361c1997-01-24 13:49:28 +00004174
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004175static PyObject *
4176string_concatenate(PyObject *v, PyObject *w,
4177 PyFrameObject *f, unsigned char *next_instr)
4178{
4179 /* This function implements 'variable += expr' when both arguments
4180 are strings. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00004181
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004182 if (v->ob_refcnt == 2) {
4183 /* In the common case, there are 2 references to the value
4184 * stored in 'variable' when the += is performed: one on the
4185 * value stack (in 'v') and one still stored in the 'variable'.
4186 * We try to delete the variable now to reduce the refcnt to 1.
4187 */
4188 switch (*next_instr) {
4189 case STORE_FAST:
4190 {
4191 int oparg = PEEKARG();
4192 PyObject **fastlocals = f->f_localsplus;
4193 if (GETLOCAL(oparg) == v)
4194 SETLOCAL(oparg, NULL);
4195 break;
4196 }
4197 case STORE_DEREF:
4198 {
Thomas Wouters477c8d52006-05-27 19:21:47 +00004199 PyObject **freevars = f->f_localsplus + f->f_code->co_nlocals;
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004200 PyObject *c = freevars[PEEKARG()];
4201 if (PyCell_GET(c) == v)
4202 PyCell_Set(c, NULL);
4203 break;
4204 }
4205 case STORE_NAME:
4206 {
4207 PyObject *names = f->f_code->co_names;
4208 PyObject *name = GETITEM(names, PEEKARG());
4209 PyObject *locals = f->f_locals;
4210 if (PyDict_CheckExact(locals) &&
4211 PyDict_GetItem(locals, name) == v) {
4212 if (PyDict_DelItem(locals, name) != 0) {
4213 PyErr_Clear();
4214 }
4215 }
4216 break;
4217 }
4218 }
4219 }
4220
Armin Rigo618fbf52004-08-07 20:58:32 +00004221 if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004222 /* Now we own the last reference to 'v', so we can resize it
4223 * in-place.
4224 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004225 Py_ssize_t v_len = PyString_GET_SIZE(v);
4226 Py_ssize_t w_len = PyString_GET_SIZE(w);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004227 if (_PyString_Resize(&v, v_len + w_len) != 0) {
4228 /* XXX if _PyString_Resize() fails, 'v' has been
4229 * deallocated so it cannot be put back into 'variable'.
4230 * The MemoryError is raised when there is no value in
4231 * 'variable', which might (very remotely) be a cause
4232 * of incompatibilities.
4233 */
4234 return NULL;
4235 }
4236 /* copy 'w' into the newly allocated area of 'v' */
4237 memcpy(PyString_AS_STRING(v) + v_len,
4238 PyString_AS_STRING(w), w_len);
4239 return v;
4240 }
4241 else {
4242 /* When in-place resizing is not an option. */
4243 PyString_Concat(&v, w);
4244 return v;
4245 }
4246}
4247
Guido van Rossum950361c1997-01-24 13:49:28 +00004248#ifdef DYNAMIC_EXECUTION_PROFILE
4249
Skip Montanarof118cb12001-10-15 20:51:38 +00004250static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004251getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004252{
4253 int i;
4254 PyObject *l = PyList_New(256);
4255 if (l == NULL) return NULL;
4256 for (i = 0; i < 256; i++) {
4257 PyObject *x = PyInt_FromLong(a[i]);
4258 if (x == NULL) {
4259 Py_DECREF(l);
4260 return NULL;
4261 }
4262 PyList_SetItem(l, i, x);
4263 }
4264 for (i = 0; i < 256; i++)
4265 a[i] = 0;
4266 return l;
4267}
4268
4269PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004270_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004271{
4272#ifndef DXPAIRS
4273 return getarray(dxp);
4274#else
4275 int i;
4276 PyObject *l = PyList_New(257);
4277 if (l == NULL) return NULL;
4278 for (i = 0; i < 257; i++) {
4279 PyObject *x = getarray(dxpairs[i]);
4280 if (x == NULL) {
4281 Py_DECREF(l);
4282 return NULL;
4283 }
4284 PyList_SetItem(l, i, x);
4285 }
4286 return l;
4287#endif
4288}
4289
4290#endif