blob: c5ae6cc2bb13f7d8caf4ee57ea3ac94567b94f5a [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
Fredrik Lundh57640f52006-05-26 11:54:04 +00009/* enable more aggressive local inlining (platform dependent) */
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
Fredrik Lundh57640f52006-05-26 11:54:04 +000022#if defined(_MSC_VER)
23/* enable more aggressive optimization for visual studio */
24#pragma optimize("agtw", on)
25#endif
26
Martin v. Löwis87fa7852004-08-29 15:51:52 +000027#ifndef WITH_TSC
Michael W. Hudson75eabd22005-01-18 15:56:11 +000028
29#define READ_TIMESTAMP(var)
30
31#else
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000032
33typedef unsigned long long uint64;
34
Michael W. Hudson800ba232004-08-12 18:19:17 +000035#if defined(__ppc__) /* <- Don't know if this is the correct symbol; this
36 section should work for GCC on any PowerPC platform,
37 irrespective of OS. POWER? Who knows :-) */
38
Michael W. Hudson75eabd22005-01-18 15:56:11 +000039#define READ_TIMESTAMP(var) ppc_getcounter(&var)
Michael W. Hudson800ba232004-08-12 18:19:17 +000040
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +000041Py_LOCAL(void)
Michael W. Hudson800ba232004-08-12 18:19:17 +000042ppc_getcounter(uint64 *v)
43{
44 register unsigned long tbu, tb, tbu2;
45
46 loop:
47 asm volatile ("mftbu %0" : "=r" (tbu) );
48 asm volatile ("mftb %0" : "=r" (tb) );
49 asm volatile ("mftbu %0" : "=r" (tbu2));
50 if (__builtin_expect(tbu != tbu2, 0)) goto loop;
51
52 /* The slightly peculiar way of writing the next lines is
53 compiled better by GCC than any other way I tried. */
54 ((long*)(v))[0] = tbu;
55 ((long*)(v))[1] = tb;
56}
57
Michael W. Hudson75eabd22005-01-18 15:56:11 +000058#else /* this is for linux/x86 (and probably any other GCC/x86 combo) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000059
Michael W. Hudson75eabd22005-01-18 15:56:11 +000060#define READ_TIMESTAMP(val) \
61 __asm__ __volatile__("rdtsc" : "=A" (val))
Michael W. Hudson800ba232004-08-12 18:19:17 +000062
63#endif
64
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000065void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
66 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
67{
68 uint64 intr, inst, loop;
69 PyThreadState *tstate = PyThreadState_Get();
70 if (!tstate->interp->tscdump)
71 return;
72 intr = intr1 - intr0;
73 inst = inst1 - inst0 - intr;
74 loop = loop1 - loop0 - intr;
75 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
76 opcode, ticked, inst, loop);
77}
Michael W. Hudson800ba232004-08-12 18:19:17 +000078
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000079#endif
80
Guido van Rossum04691fc1992-08-12 15:35:34 +000081/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000082/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000083
Guido van Rossum408027e1996-12-30 16:17:54 +000084#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000085/* For debugging the interpreter: */
86#define LLTRACE 1 /* Low-level trace feature */
87#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000088#endif
89
Jeremy Hylton52820442001-01-03 23:52:36 +000090typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000091
Guido van Rossum374a9221991-04-04 10:40:29 +000092/* Forward declarations */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000093#ifdef WITH_TSC
Fredrik Lundh57640f52006-05-26 11:54:04 +000094Py_LOCAL(PyObject *) call_function(PyObject ***, int, uint64*, uint64*);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000095#else
Fredrik Lundh57640f52006-05-26 11:54:04 +000096Py_LOCAL(PyObject *) call_function(PyObject ***, int);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000097#endif
Fredrik Lundh57640f52006-05-26 11:54:04 +000098Py_LOCAL(PyObject *) fast_function(PyObject *, PyObject ***, int, int, int);
99Py_LOCAL(PyObject *) do_call(PyObject *, PyObject ***, int, int);
100Py_LOCAL(PyObject *) ext_do_call(PyObject *, PyObject ***, int, int, int);
101Py_LOCAL(PyObject *) update_keyword_args(PyObject *, int, PyObject ***,PyObject *);
102Py_LOCAL(PyObject *) update_star_args(int, int, PyObject *, PyObject ***);
103Py_LOCAL(PyObject *) load_args(PyObject ***, int);
Jeremy Hylton52820442001-01-03 23:52:36 +0000104#define CALL_FLAG_VAR 1
105#define CALL_FLAG_KW 2
106
Guido van Rossum0a066c01992-03-27 17:29:15 +0000107#ifdef LLTRACE
Fredrik Lundh1b949402006-05-26 12:01:49 +0000108static int lltrace;
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +0000109Py_LOCAL(int) prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +0000110#endif
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +0000111Py_LOCAL(int) call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
Fred Drake5755ce62001-06-27 19:19:46 +0000112 int, PyObject *);
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +0000113Py_LOCAL(void) call_trace_protected(Py_tracefunc, PyObject *,
Armin Rigo1c2d7e52005-09-20 18:34:01 +0000114 PyFrameObject *, int, PyObject *);
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +0000115Py_LOCAL(void) call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
116Py_LOCAL(int) maybe_call_line_trace(Py_tracefunc, PyObject *,
Armin Rigobf57a142004-03-22 19:24:58 +0000117 PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000118
Fredrik Lundh57640f52006-05-26 11:54:04 +0000119Py_LOCAL(PyObject *) apply_slice(PyObject *, PyObject *, PyObject *);
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +0000120Py_LOCAL(int) assign_slice(PyObject *, PyObject *,
Tim Petersdbd9ba62000-07-09 03:09:57 +0000121 PyObject *, PyObject *);
Fredrik Lundh57640f52006-05-26 11:54:04 +0000122Py_LOCAL(PyObject *) cmp_outcome(int, PyObject *, PyObject *);
123Py_LOCAL(PyObject *) import_from(PyObject *, PyObject *);
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +0000124Py_LOCAL(int) import_all_from(PyObject *, PyObject *);
Fredrik Lundh57640f52006-05-26 11:54:04 +0000125Py_LOCAL(PyObject *) build_class(PyObject *, PyObject *, PyObject *);
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +0000126Py_LOCAL(int) exec_statement(PyFrameObject *,
Tim Petersdbd9ba62000-07-09 03:09:57 +0000127 PyObject *, PyObject *, PyObject *);
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +0000128Py_LOCAL(void) set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *);
129Py_LOCAL(void) reset_exc_info(PyThreadState *);
130Py_LOCAL(void) format_exc_check_arg(PyObject *, char *, PyObject *);
Fredrik Lundh57640f52006-05-26 11:54:04 +0000131Py_LOCAL(PyObject *) string_concatenate(PyObject *, PyObject *,
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000132 PyFrameObject *, unsigned char *);
Guido van Rossum374a9221991-04-04 10:40:29 +0000133
Paul Prescode68140d2000-08-30 20:25:01 +0000134#define NAME_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000135 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000136#define GLOBAL_NAME_ERROR_MSG \
137 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000138#define UNBOUNDLOCAL_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000139 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000140#define UNBOUNDFREE_ERROR_MSG \
141 "free variable '%.200s' referenced before assignment" \
142 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000143
Guido van Rossum950361c1997-01-24 13:49:28 +0000144/* Dynamic execution profile */
145#ifdef DYNAMIC_EXECUTION_PROFILE
146#ifdef DXPAIRS
147static long dxpairs[257][256];
148#define dxp dxpairs[256]
149#else
150static long dxp[256];
151#endif
152#endif
153
Jeremy Hylton985eba52003-02-05 23:13:00 +0000154/* Function call profile */
155#ifdef CALL_PROFILE
156#define PCALL_NUM 11
157static int pcall[PCALL_NUM];
158
159#define PCALL_ALL 0
160#define PCALL_FUNCTION 1
161#define PCALL_FAST_FUNCTION 2
162#define PCALL_FASTER_FUNCTION 3
163#define PCALL_METHOD 4
164#define PCALL_BOUND_METHOD 5
165#define PCALL_CFUNCTION 6
166#define PCALL_TYPE 7
167#define PCALL_GENERATOR 8
168#define PCALL_OTHER 9
169#define PCALL_POP 10
170
171/* Notes about the statistics
172
173 PCALL_FAST stats
174
175 FAST_FUNCTION means no argument tuple needs to be created.
176 FASTER_FUNCTION means that the fast-path frame setup code is used.
177
178 If there is a method call where the call can be optimized by changing
179 the argument tuple and calling the function directly, it gets recorded
180 twice.
181
182 As a result, the relationship among the statistics appears to be
183 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
184 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
185 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
186 PCALL_METHOD > PCALL_BOUND_METHOD
187*/
188
189#define PCALL(POS) pcall[POS]++
190
191PyObject *
192PyEval_GetCallStats(PyObject *self)
193{
Tim Peters8a5c3c72004-04-05 19:36:21 +0000194 return Py_BuildValue("iiiiiiiiii",
Jeremy Hylton985eba52003-02-05 23:13:00 +0000195 pcall[0], pcall[1], pcall[2], pcall[3],
196 pcall[4], pcall[5], pcall[6], pcall[7],
197 pcall[8], pcall[9]);
198}
199#else
200#define PCALL(O)
201
202PyObject *
203PyEval_GetCallStats(PyObject *self)
204{
205 Py_INCREF(Py_None);
206 return Py_None;
207}
208#endif
209
Tim Peters5ca576e2001-06-18 22:08:13 +0000210
Guido van Rossume59214e1994-08-30 08:01:59 +0000211#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000212
Guido van Rossum2571cc81999-04-07 16:07:23 +0000213#ifndef DONT_HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000214#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000215#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000216#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000217
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000218static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
Guido van Rossuma9672091994-09-14 13:31:22 +0000219static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000220
Tim Peters7f468f22004-10-11 02:40:51 +0000221int
222PyEval_ThreadsInitialized(void)
223{
224 return interpreter_lock != 0;
225}
226
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000227void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000228PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000229{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000230 if (interpreter_lock)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000231 return;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000232 interpreter_lock = PyThread_allocate_lock();
233 PyThread_acquire_lock(interpreter_lock, 1);
234 main_thread = PyThread_get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000235}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000236
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000237void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000238PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000239{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000240 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000241}
242
243void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000244PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000245{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000246 PyThread_release_lock(interpreter_lock);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000247}
248
249void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000250PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000251{
252 if (tstate == NULL)
253 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000254 /* Check someone has called PyEval_InitThreads() to create the lock */
255 assert(interpreter_lock);
Guido van Rossum65d5b571998-12-21 19:32:43 +0000256 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000257 if (PyThreadState_Swap(tstate) != NULL)
258 Py_FatalError(
259 "PyEval_AcquireThread: non-NULL old thread state");
260}
261
262void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000263PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000264{
265 if (tstate == NULL)
266 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
267 if (PyThreadState_Swap(NULL) != tstate)
268 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000269 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000270}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000271
272/* This function is called from PyOS_AfterFork to ensure that newly
273 created child processes don't hold locks referring to threads which
274 are not running in the child process. (This could also be done using
275 pthread_atfork mechanism, at least for the pthreads implementation.) */
276
277void
278PyEval_ReInitThreads(void)
279{
280 if (!interpreter_lock)
281 return;
282 /*XXX Can't use PyThread_free_lock here because it does too
283 much error-checking. Doing this cleanly would require
284 adding a new function to each thread_*.h. Instead, just
285 create a new lock and waste a little bit of memory */
286 interpreter_lock = PyThread_allocate_lock();
287 PyThread_acquire_lock(interpreter_lock, 1);
288 main_thread = PyThread_get_thread_ident();
289}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000290#endif
291
Guido van Rossumff4949e1992-08-05 19:58:53 +0000292/* Functions save_thread and restore_thread are always defined so
293 dynamically loaded modules needn't be compiled separately for use
294 with and without threads: */
295
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000296PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000297PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000298{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000299 PyThreadState *tstate = PyThreadState_Swap(NULL);
300 if (tstate == NULL)
301 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000302#ifdef WITH_THREAD
Guido van Rossumb74eca91997-09-30 22:03:16 +0000303 if (interpreter_lock)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000304 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000305#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000306 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000307}
308
309void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000310PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000311{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000312 if (tstate == NULL)
313 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000314#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000315 if (interpreter_lock) {
Guido van Rossumb74eca91997-09-30 22:03:16 +0000316 int err = errno;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000317 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000318 errno = err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000319 }
320#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000321 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000322}
323
324
Guido van Rossuma9672091994-09-14 13:31:22 +0000325/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
326 signal handlers or Mac I/O completion routines) can schedule calls
327 to a function to be called synchronously.
328 The synchronous function is called with one void* argument.
329 It should return 0 for success or -1 for failure -- failure should
330 be accompanied by an exception.
331
332 If registry succeeds, the registry function returns 0; if it fails
333 (e.g. due to too many pending calls) it returns -1 (without setting
334 an exception condition).
335
336 Note that because registry may occur from within signal handlers,
337 or other asynchronous events, calling malloc() is unsafe!
338
339#ifdef WITH_THREAD
340 Any thread can schedule pending calls, but only the main thread
341 will execute them.
342#endif
343
344 XXX WARNING! ASYNCHRONOUSLY EXECUTING CODE!
345 There are two possible race conditions:
346 (1) nested asynchronous registry calls;
347 (2) registry calls made while pending calls are being processed.
348 While (1) is very unlikely, (2) is a real possibility.
349 The current code is safe against (2), but not against (1).
350 The safety against (2) is derived from the fact that only one
351 thread (the main thread) ever takes things out of the queue.
Guido van Rossuma9672091994-09-14 13:31:22 +0000352
Guido van Rossuma027efa1997-05-05 20:56:21 +0000353 XXX Darn! With the advent of thread state, we should have an array
354 of pending calls per thread in the thread state! Later...
355*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000356
Guido van Rossuma9672091994-09-14 13:31:22 +0000357#define NPENDINGCALLS 32
358static struct {
Thomas Wouters334fb892000-07-25 12:56:38 +0000359 int (*func)(void *);
360 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000361} pendingcalls[NPENDINGCALLS];
362static volatile int pendingfirst = 0;
363static volatile int pendinglast = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000364static volatile int things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000365
366int
Thomas Wouters334fb892000-07-25 12:56:38 +0000367Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000368{
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000369 static volatile int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000370 int i, j;
371 /* XXX Begin critical section */
372 /* XXX If you want this to be safe against nested
373 XXX asynchronous calls, you'll have to work harder! */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000374 if (busy)
375 return -1;
376 busy = 1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000377 i = pendinglast;
378 j = (i + 1) % NPENDINGCALLS;
Guido van Rossum04e70322002-07-17 16:57:13 +0000379 if (j == pendingfirst) {
380 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000381 return -1; /* Queue full */
Guido van Rossum04e70322002-07-17 16:57:13 +0000382 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000383 pendingcalls[i].func = func;
384 pendingcalls[i].arg = arg;
385 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000386
387 _Py_Ticker = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000388 things_to_do = 1; /* Signal main loop */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000389 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000390 /* XXX End critical section */
391 return 0;
392}
393
Guido van Rossum180d7b41994-09-29 09:45:57 +0000394int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000395Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000396{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000397 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000398#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000399 if (main_thread && PyThread_get_thread_ident() != main_thread)
Guido van Rossuma9672091994-09-14 13:31:22 +0000400 return 0;
401#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000402 if (busy)
Guido van Rossum180d7b41994-09-29 09:45:57 +0000403 return 0;
404 busy = 1;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000405 things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000406 for (;;) {
407 int i;
Thomas Wouters334fb892000-07-25 12:56:38 +0000408 int (*func)(void *);
409 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000410 i = pendingfirst;
411 if (i == pendinglast)
412 break; /* Queue empty */
413 func = pendingcalls[i].func;
414 arg = pendingcalls[i].arg;
415 pendingfirst = (i + 1) % NPENDINGCALLS;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000416 if (func(arg) < 0) {
417 busy = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000418 things_to_do = 1; /* We're not done yet */
Guido van Rossuma9672091994-09-14 13:31:22 +0000419 return -1;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000420 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000421 }
Guido van Rossum180d7b41994-09-29 09:45:57 +0000422 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000423 return 0;
424}
425
426
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000427/* The interpreter's recursion limit */
428
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000429#ifndef Py_DEFAULT_RECURSION_LIMIT
430#define Py_DEFAULT_RECURSION_LIMIT 1000
431#endif
432static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
433int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000434
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000435int
436Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000437{
438 return recursion_limit;
439}
440
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000441void
442Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000443{
444 recursion_limit = new_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000445 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000446}
447
Armin Rigo2b3eb402003-10-28 12:05:48 +0000448/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
449 if the recursion_depth reaches _Py_CheckRecursionLimit.
450 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
451 to guarantee that _Py_CheckRecursiveCall() is regularly called.
452 Without USE_STACKCHECK, there is no need for this. */
453int
454_Py_CheckRecursiveCall(char *where)
455{
456 PyThreadState *tstate = PyThreadState_GET();
457
458#ifdef USE_STACKCHECK
459 if (PyOS_CheckStack()) {
460 --tstate->recursion_depth;
461 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
462 return -1;
463 }
464#endif
465 if (tstate->recursion_depth > recursion_limit) {
466 --tstate->recursion_depth;
467 PyErr_Format(PyExc_RuntimeError,
468 "maximum recursion depth exceeded%s",
469 where);
470 return -1;
471 }
472 _Py_CheckRecursionLimit = recursion_limit;
473 return 0;
474}
475
Guido van Rossum374a9221991-04-04 10:40:29 +0000476/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000477enum why_code {
478 WHY_NOT = 0x0001, /* No error */
479 WHY_EXCEPTION = 0x0002, /* Exception occurred */
480 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
481 WHY_RETURN = 0x0008, /* 'return' statement */
482 WHY_BREAK = 0x0010, /* 'break' statement */
483 WHY_CONTINUE = 0x0020, /* 'continue' statement */
484 WHY_YIELD = 0x0040 /* 'yield' operator */
485};
Guido van Rossum374a9221991-04-04 10:40:29 +0000486
Fredrik Lundh57640f52006-05-26 11:54:04 +0000487Py_LOCAL(enum why_code) do_raise(PyObject *, PyObject *, PyObject *);
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +0000488Py_LOCAL(int) unpack_iterable(PyObject *, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000489
Skip Montanarod581d772002-09-03 20:10:45 +0000490/* for manipulating the thread switch and periodic "stuff" - used to be
491 per thread, now just a pair o' globals */
Skip Montanaro99dba272002-09-03 20:19:06 +0000492int _Py_CheckInterval = 100;
493volatile int _Py_Ticker = 100;
Guido van Rossum374a9221991-04-04 10:40:29 +0000494
Guido van Rossumb209a111997-04-29 18:18:01 +0000495PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000496PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000497{
Jeremy Hylton985eba52003-02-05 23:13:00 +0000498 /* XXX raise SystemError if globals is NULL */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000499 return PyEval_EvalCodeEx(co,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000500 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000501 (PyObject **)NULL, 0,
502 (PyObject **)NULL, 0,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000503 (PyObject **)NULL, 0,
504 NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000505}
506
507
508/* Interpreter main loop */
509
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000510PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000511PyEval_EvalFrame(PyFrameObject *f) {
512 /* This is for backward compatibility with extension modules that
513 used this API; core interpreter code should call PyEval_EvalFrameEx() */
514 return PyEval_EvalFrameEx(f, 0);
515}
516
517PyObject *
Anthony Baxtera863d332006-04-11 07:43:46 +0000518PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000519{
Guido van Rossum950361c1997-01-24 13:49:28 +0000520#ifdef DXPAIRS
521 int lastopcode = 0;
522#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +0000523 register PyObject **stack_pointer; /* Next free slot in value stack */
Guido van Rossum374a9221991-04-04 10:40:29 +0000524 register unsigned char *next_instr;
Armin Rigo8817fcd2004-06-17 10:22:40 +0000525 register int opcode; /* Current opcode */
526 register int oparg; /* Current opcode argument, if any */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000527 register enum why_code why; /* Reason for block stack unwind */
Guido van Rossum374a9221991-04-04 10:40:29 +0000528 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000529 register PyObject *x; /* Result object -- NULL if error */
530 register PyObject *v; /* Temporary objects popped off stack */
531 register PyObject *w;
532 register PyObject *u;
533 register PyObject *t;
Barry Warsaw23c9ec82000-08-21 15:44:01 +0000534 register PyObject *stream = NULL; /* for PRINT opcodes */
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000535 register PyObject **fastlocals, **freevars;
Guido van Rossum014518f1998-11-23 21:09:51 +0000536 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000537 PyThreadState *tstate = PyThreadState_GET();
Tim Peters5ca576e2001-06-18 22:08:13 +0000538 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000539
Tim Peters8a5c3c72004-04-05 19:36:21 +0000540 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000541
542 not (instr_lb <= current_bytecode_offset < instr_ub)
543
Tim Peters8a5c3c72004-04-05 19:36:21 +0000544 is true when the line being executed has changed. The
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000545 initial values are such as to make this false the first
546 time it is tested. */
Armin Rigobf57a142004-03-22 19:24:58 +0000547 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000548
Guido van Rossumd076c731998-10-07 19:42:25 +0000549 unsigned char *first_instr;
Skip Montanaro04d80f82002-08-04 21:03:35 +0000550 PyObject *names;
551 PyObject *consts;
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000552#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000553 /* Make it easier to find out where we are with a debugger */
Tim Peters5ca576e2001-06-18 22:08:13 +0000554 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000555#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000556
Neal Norwitza81d2202002-07-14 00:27:26 +0000557/* Tuple access macros */
558
559#ifndef Py_DEBUG
560#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
561#else
562#define GETITEM(v, i) PyTuple_GetItem((v), (i))
563#endif
564
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000565#ifdef WITH_TSC
566/* Use Pentium timestamp counter to mark certain events:
567 inst0 -- beginning of switch statement for opcode dispatch
568 inst1 -- end of switch statement (may be skipped)
569 loop0 -- the top of the mainloop
570 loop1 -- place where control returns again to top of mainloop
571 (may be skipped)
572 intr1 -- beginning of long interruption
573 intr2 -- end of long interruption
574
575 Many opcodes call out to helper C functions. In some cases, the
576 time in those functions should be counted towards the time for the
577 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
578 calls another Python function; there's no point in charge all the
579 bytecode executed by the called function to the caller.
580
581 It's hard to make a useful judgement statically. In the presence
582 of operator overloading, it's impossible to tell if a call will
583 execute new Python code or not.
584
585 It's a case-by-case judgement. I'll use intr1 for the following
586 cases:
587
588 EXEC_STMT
589 IMPORT_STAR
590 IMPORT_FROM
591 CALL_FUNCTION (and friends)
592
593 */
594 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
595 int ticked = 0;
596
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000597 READ_TIMESTAMP(inst0);
598 READ_TIMESTAMP(inst1);
599 READ_TIMESTAMP(loop0);
600 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000601
602 /* shut up the compiler */
603 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000604#endif
605
Guido van Rossum374a9221991-04-04 10:40:29 +0000606/* Code access macros */
607
Martin v. Löwis18e16552006-02-15 17:27:45 +0000608#define INSTR_OFFSET() ((int)(next_instr - first_instr))
Guido van Rossum374a9221991-04-04 10:40:29 +0000609#define NEXTOP() (*next_instr++)
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000610#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000611#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
Guido van Rossumd076c731998-10-07 19:42:25 +0000612#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000613#define JUMPBY(x) (next_instr += (x))
614
Raymond Hettingerf606f872003-03-16 03:11:04 +0000615/* OpCode prediction macros
616 Some opcodes tend to come in pairs thus making it possible to predict
617 the second code when the first is run. For example, COMPARE_OP is often
618 followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And, those opcodes are often
619 followed by a POP_TOP.
620
621 Verifying the prediction costs a single high-speed test of register
Raymond Hettingerac2072922003-03-16 15:41:11 +0000622 variable against a constant. If the pairing was good, then the
Raymond Hettingerf606f872003-03-16 03:11:04 +0000623 processor has a high likelihood of making its own successful branch
624 prediction which results in a nearly zero overhead transition to the
625 next opcode.
626
627 A successful prediction saves a trip through the eval-loop including
628 its two unpredictable branches, the HASARG test and the switch-case.
Raymond Hettingera7216982004-02-08 19:59:27 +0000629
Tim Peters8a5c3c72004-04-05 19:36:21 +0000630 If collecting opcode statistics, turn off prediction so that
631 statistics are accurately maintained (the predictions bypass
Raymond Hettingera7216982004-02-08 19:59:27 +0000632 the opcode frequency counter updates).
Raymond Hettingerf606f872003-03-16 03:11:04 +0000633*/
634
Raymond Hettingera7216982004-02-08 19:59:27 +0000635#ifdef DYNAMIC_EXECUTION_PROFILE
636#define PREDICT(op) if (0) goto PRED_##op
637#else
Raymond Hettingerac2072922003-03-16 15:41:11 +0000638#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000639#endif
640
Raymond Hettingerf606f872003-03-16 03:11:04 +0000641#define PREDICTED(op) PRED_##op: next_instr++
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000642#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Raymond Hettingerf606f872003-03-16 03:11:04 +0000643
Guido van Rossum374a9221991-04-04 10:40:29 +0000644/* Stack manipulation macros */
645
Martin v. Löwis18e16552006-02-15 17:27:45 +0000646/* The stack can grow at most MAXINT deep, as co_nlocals and
647 co_stacksize are ints. */
648#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
Guido van Rossum374a9221991-04-04 10:40:29 +0000649#define EMPTY() (STACK_LEVEL() == 0)
650#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000651#define SECOND() (stack_pointer[-2])
652#define THIRD() (stack_pointer[-3])
653#define FOURTH() (stack_pointer[-4])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000654#define SET_TOP(v) (stack_pointer[-1] = (v))
655#define SET_SECOND(v) (stack_pointer[-2] = (v))
656#define SET_THIRD(v) (stack_pointer[-3] = (v))
657#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000658#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000659#define BASIC_PUSH(v) (*stack_pointer++ = (v))
660#define BASIC_POP() (*--stack_pointer)
661
Guido van Rossum96a42c81992-01-12 02:29:51 +0000662#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000663#define PUSH(v) { (void)(BASIC_PUSH(v), \
664 lltrace && prtrace(TOP(), "push")); \
Richard Jonescebbefc2006-05-23 18:28:17 +0000665 assert(STACK_LEVEL() <= co->co_stacksize); }
Fred Drakede26cfc2001-10-13 06:11:28 +0000666#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000667#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
668 lltrace && prtrace(TOP(), "stackadj")); \
Richard Jonescebbefc2006-05-23 18:28:17 +0000669 assert(STACK_LEVEL() <= co->co_stacksize); }
Guido van Rossumc2e20742006-02-27 22:32:47 +0000670#define EXT_POP(STACK_POINTER) (lltrace && prtrace(*(STACK_POINTER), "ext_pop"), *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000671#else
672#define PUSH(v) BASIC_PUSH(v)
673#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000674#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000675#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000676#endif
677
Guido van Rossum681d79a1995-07-18 14:51:37 +0000678/* Local variable macros */
679
680#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000681
682/* The SETLOCAL() macro must not DECREF the local variable in-place and
683 then store the new value; it must copy the old value to a temporary
684 value, then store the new value, and then DECREF the temporary value.
685 This is because it is possible that during the DECREF the frame is
686 accessed by other code (e.g. a __del__ method or gc.collect()) and the
687 variable would be pointing to already-freed memory. */
688#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
689 GETLOCAL(i) = value; \
690 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000691
Guido van Rossuma027efa1997-05-05 20:56:21 +0000692/* Start of code */
693
Tim Peters5ca576e2001-06-18 22:08:13 +0000694 if (f == NULL)
695 return NULL;
696
Armin Rigo1d313ab2003-10-25 14:33:09 +0000697 /* push frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000698 if (Py_EnterRecursiveCall(""))
Armin Rigo1d313ab2003-10-25 14:33:09 +0000699 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000700
Tim Peters5ca576e2001-06-18 22:08:13 +0000701 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000702
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000703 if (tstate->use_tracing) {
704 if (tstate->c_tracefunc != NULL) {
705 /* tstate->c_tracefunc, if defined, is a
706 function that will be called on *every* entry
707 to a code block. Its return value, if not
708 None, is a function that will be called at
709 the start of each executed line of code.
710 (Actually, the function must return itself
711 in order to continue tracing.) The trace
712 functions are called with three arguments:
713 a pointer to the current frame, a string
714 indicating why the function is called, and
715 an argument which depends on the situation.
716 The global trace function is also called
717 whenever an exception is detected. */
718 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
719 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000720 /* Trace function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000721 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000722 }
723 }
724 if (tstate->c_profilefunc != NULL) {
725 /* Similar for c_profilefunc, except it needn't
726 return itself and isn't called for "line" events */
727 if (call_trace(tstate->c_profilefunc,
728 tstate->c_profileobj,
729 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000730 /* Profile function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000731 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000732 }
733 }
734 }
735
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000736 co = f->f_code;
737 names = co->co_names;
738 consts = co->co_consts;
739 fastlocals = f->f_localsplus;
Richard Jonescebbefc2006-05-23 18:28:17 +0000740 freevars = f->f_localsplus + co->co_nlocals;
Brett Cannonc9371d42005-06-25 08:23:41 +0000741 first_instr = (unsigned char*) PyString_AS_STRING(co->co_code);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000742 /* An explanation is in order for the next line.
743
744 f->f_lasti now refers to the index of the last instruction
745 executed. You might think this was obvious from the name, but
746 this wasn't always true before 2.3! PyFrame_New now sets
747 f->f_lasti to -1 (i.e. the index *before* the first instruction)
748 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
749 does work. Promise. */
750 next_instr = first_instr + f->f_lasti + 1;
751 stack_pointer = f->f_stacktop;
752 assert(stack_pointer != NULL);
753 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
754
Tim Peters5ca576e2001-06-18 22:08:13 +0000755#ifdef LLTRACE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000756 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +0000757#endif
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000758#if defined(Py_DEBUG) || defined(LLTRACE)
Tim Peters5ca576e2001-06-18 22:08:13 +0000759 filename = PyString_AsString(co->co_filename);
760#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000761
Guido van Rossum374a9221991-04-04 10:40:29 +0000762 why = WHY_NOT;
763 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +0000764 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +0000765 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +0000766
Anthony Baxtera863d332006-04-11 07:43:46 +0000767 if (throwflag) { /* support for generator.throw() */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000768 why = WHY_EXCEPTION;
769 goto on_error;
770 }
771
Guido van Rossum374a9221991-04-04 10:40:29 +0000772 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000773#ifdef WITH_TSC
774 if (inst1 == 0) {
775 /* Almost surely, the opcode executed a break
776 or a continue, preventing inst1 from being set
777 on the way out of the loop.
778 */
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000779 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000780 loop1 = inst1;
781 }
782 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
783 intr0, intr1);
784 ticked = 0;
785 inst1 = 0;
786 intr0 = 0;
787 intr1 = 0;
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000788 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000789#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000790 assert(stack_pointer >= f->f_valuestack); /* else underflow */
Richard Jonescebbefc2006-05-23 18:28:17 +0000791 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000792
Guido van Rossuma027efa1997-05-05 20:56:21 +0000793 /* Do periodic things. Doing this every time through
794 the loop would add too much overhead, so we do it
795 only every Nth instruction. We also do it if
796 ``things_to_do'' is set, i.e. when an asynchronous
797 event needs attention (e.g. a signal handler or
798 async I/O handler); see Py_AddPendingCall() and
799 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000800
Skip Montanarod581d772002-09-03 20:10:45 +0000801 if (--_Py_Ticker < 0) {
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000802 if (*next_instr == SETUP_FINALLY) {
803 /* Make the last opcode before
804 a try: finally: block uninterruptable. */
805 goto fast_next_opcode;
806 }
Skip Montanarod581d772002-09-03 20:10:45 +0000807 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000808 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000809#ifdef WITH_TSC
810 ticked = 1;
811#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000812 if (things_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +0000813 if (Py_MakePendingCalls() < 0) {
814 why = WHY_EXCEPTION;
815 goto on_error;
816 }
Kurt B. Kaiser4c79a832004-11-23 18:06:08 +0000817 if (things_to_do)
818 /* MakePendingCalls() didn't succeed.
819 Force early re-execution of this
820 "periodic" code, possibly after
821 a thread switch */
822 _Py_Ticker = 0;
Guido van Rossum8861b741996-07-30 16:49:37 +0000823 }
Guido van Rossume59214e1994-08-30 08:01:59 +0000824#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000825 if (interpreter_lock) {
826 /* Give another thread a chance */
827
Guido van Rossum25ce5661997-08-02 03:10:38 +0000828 if (PyThreadState_Swap(NULL) != tstate)
829 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000830 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000831
832 /* Other threads may run now */
833
Guido van Rossum65d5b571998-12-21 19:32:43 +0000834 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000835 if (PyThreadState_Swap(tstate) != NULL)
836 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000837
838 /* Check for thread interrupts */
839
840 if (tstate->async_exc != NULL) {
841 x = tstate->async_exc;
842 tstate->async_exc = NULL;
843 PyErr_SetNone(x);
844 Py_DECREF(x);
845 why = WHY_EXCEPTION;
846 goto on_error;
847 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000848 }
849#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000850 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000851
Neil Schemenauer63543862002-02-17 19:10:14 +0000852 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +0000853 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +0000854
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000855 /* line-by-line tracing support */
856
857 if (tstate->c_tracefunc != NULL && !tstate->tracing) {
858 /* see maybe_call_line_trace
859 for expository comments */
860 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +0000861
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000862 err = maybe_call_line_trace(tstate->c_tracefunc,
863 tstate->c_traceobj,
Armin Rigobf57a142004-03-22 19:24:58 +0000864 f, &instr_lb, &instr_ub,
865 &instr_prev);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000866 /* Reload possibly changed frame fields */
867 JUMPTO(f->f_lasti);
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000868 if (f->f_stacktop != NULL) {
869 stack_pointer = f->f_stacktop;
870 f->f_stacktop = NULL;
871 }
872 if (err) {
873 /* trace function raised an exception */
874 goto on_error;
875 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000876 }
877
878 /* Extract opcode and argument */
879
Guido van Rossum374a9221991-04-04 10:40:29 +0000880 opcode = NEXTOP();
Armin Rigo8817fcd2004-06-17 10:22:40 +0000881 oparg = 0; /* allows oparg to be stored in a register because
882 it doesn't have to be remembered across a full loop */
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000883 if (HAS_ARG(opcode))
884 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +0000885 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +0000886#ifdef DYNAMIC_EXECUTION_PROFILE
887#ifdef DXPAIRS
888 dxpairs[lastopcode][opcode]++;
889 lastopcode = opcode;
890#endif
891 dxp[opcode]++;
892#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000893
Guido van Rossum96a42c81992-01-12 02:29:51 +0000894#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +0000895 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +0000896
Guido van Rossum96a42c81992-01-12 02:29:51 +0000897 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000898 if (HAS_ARG(opcode)) {
899 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000900 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +0000901 }
902 else {
903 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000904 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +0000905 }
906 }
907#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000908
Guido van Rossum374a9221991-04-04 10:40:29 +0000909 /* Main switch on opcode */
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000910 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +0000911
Guido van Rossum374a9221991-04-04 10:40:29 +0000912 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +0000913
Guido van Rossum374a9221991-04-04 10:40:29 +0000914 /* BEWARE!
915 It is essential that any operation that fails sets either
916 x to NULL, err to nonzero, or why to anything but WHY_NOT,
917 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000918
Guido van Rossum374a9221991-04-04 10:40:29 +0000919 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000920
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000921 case NOP:
922 goto fast_next_opcode;
923
Neil Schemenauer63543862002-02-17 19:10:14 +0000924 case LOAD_FAST:
925 x = GETLOCAL(oparg);
926 if (x != NULL) {
927 Py_INCREF(x);
928 PUSH(x);
929 goto fast_next_opcode;
930 }
931 format_exc_check_arg(PyExc_UnboundLocalError,
932 UNBOUNDLOCAL_ERROR_MSG,
933 PyTuple_GetItem(co->co_varnames, oparg));
934 break;
935
936 case LOAD_CONST:
Skip Montanaro04d80f82002-08-04 21:03:35 +0000937 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +0000938 Py_INCREF(x);
939 PUSH(x);
940 goto fast_next_opcode;
941
Raymond Hettinger7dc52212003-03-16 20:14:44 +0000942 PREDICTED_WITH_ARG(STORE_FAST);
Neil Schemenauer63543862002-02-17 19:10:14 +0000943 case STORE_FAST:
944 v = POP();
945 SETLOCAL(oparg, v);
946 goto fast_next_opcode;
947
Raymond Hettingerf606f872003-03-16 03:11:04 +0000948 PREDICTED(POP_TOP);
Guido van Rossum374a9221991-04-04 10:40:29 +0000949 case POP_TOP:
950 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000951 Py_DECREF(v);
Neil Schemenauer63543862002-02-17 19:10:14 +0000952 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000953
Guido van Rossum374a9221991-04-04 10:40:29 +0000954 case ROT_TWO:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000955 v = TOP();
956 w = SECOND();
957 SET_TOP(w);
958 SET_SECOND(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000959 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000960
Guido van Rossum374a9221991-04-04 10:40:29 +0000961 case ROT_THREE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000962 v = TOP();
963 w = SECOND();
964 x = THIRD();
965 SET_TOP(w);
966 SET_SECOND(x);
967 SET_THIRD(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000968 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000969
Thomas Wouters434d0822000-08-24 20:11:32 +0000970 case ROT_FOUR:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000971 u = TOP();
972 v = SECOND();
973 w = THIRD();
974 x = FOURTH();
975 SET_TOP(v);
976 SET_SECOND(w);
977 SET_THIRD(x);
978 SET_FOURTH(u);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000979 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000980
Guido van Rossum374a9221991-04-04 10:40:29 +0000981 case DUP_TOP:
982 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000983 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000984 PUSH(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000985 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000986
Thomas Wouters434d0822000-08-24 20:11:32 +0000987 case DUP_TOPX:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000988 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000989 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000990 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000991 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000992 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000993 STACKADJ(2);
994 SET_TOP(x);
995 SET_SECOND(w);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000996 goto fast_next_opcode;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000997 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000998 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000999 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001000 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +00001001 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001002 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +00001003 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001004 STACKADJ(3);
1005 SET_TOP(x);
1006 SET_SECOND(w);
1007 SET_THIRD(v);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001008 goto fast_next_opcode;
Thomas Wouters434d0822000-08-24 20:11:32 +00001009 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001010 Py_FatalError("invalid argument to DUP_TOPX"
1011 " (bytecode corruption?)");
Tim Peters35ba6892000-10-11 07:04:49 +00001012 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001013
Guido van Rossum374a9221991-04-04 10:40:29 +00001014 case UNARY_POSITIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001015 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001016 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001017 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001018 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001019 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001020 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001021
Guido van Rossum374a9221991-04-04 10:40:29 +00001022 case UNARY_NEGATIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001023 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001024 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001025 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001026 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001027 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001028 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001029
Guido van Rossum374a9221991-04-04 10:40:29 +00001030 case UNARY_NOT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001031 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001032 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001033 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +00001034 if (err == 0) {
1035 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001036 SET_TOP(Py_True);
Guido van Rossumfc490731997-05-06 15:06:49 +00001037 continue;
1038 }
1039 else if (err > 0) {
1040 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001041 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +00001042 err = 0;
1043 continue;
1044 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00001045 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001046 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001047
Guido van Rossum374a9221991-04-04 10:40:29 +00001048 case UNARY_CONVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001049 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001050 x = PyObject_Repr(v);
1051 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001052 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001053 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001054 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001055
Guido van Rossum7928cd71991-10-24 14:59:31 +00001056 case UNARY_INVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001057 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001058 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001059 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001060 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001061 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001062 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001063
Guido van Rossum50564e81996-01-12 01:13:16 +00001064 case BINARY_POWER:
1065 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001066 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001067 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +00001068 Py_DECREF(v);
1069 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001070 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001071 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +00001072 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001073
Guido van Rossum374a9221991-04-04 10:40:29 +00001074 case BINARY_MULTIPLY:
1075 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001076 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001077 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001078 Py_DECREF(v);
1079 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001080 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001081 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001082 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001083
Guido van Rossum374a9221991-04-04 10:40:29 +00001084 case BINARY_DIVIDE:
Tim Peters3caca232001-12-06 06:23:26 +00001085 if (!_Py_QnewFlag) {
1086 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001087 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001088 x = PyNumber_Divide(v, w);
1089 Py_DECREF(v);
1090 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001091 SET_TOP(x);
Tim Peters3caca232001-12-06 06:23:26 +00001092 if (x != NULL) continue;
1093 break;
1094 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001095 /* -Qnew is in effect: fall through to
Tim Peters3caca232001-12-06 06:23:26 +00001096 BINARY_TRUE_DIVIDE */
1097 case BINARY_TRUE_DIVIDE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001098 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001099 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001100 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001101 Py_DECREF(v);
1102 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001103 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001104 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001105 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001106
Guido van Rossum4668b002001-08-08 05:00:18 +00001107 case BINARY_FLOOR_DIVIDE:
1108 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001109 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001110 x = PyNumber_FloorDivide(v, w);
1111 Py_DECREF(v);
1112 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001113 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001114 if (x != NULL) continue;
1115 break;
1116
Guido van Rossum374a9221991-04-04 10:40:29 +00001117 case BINARY_MODULO:
1118 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001119 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001120 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001121 Py_DECREF(v);
1122 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001123 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001124 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001125 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001126
Guido van Rossum374a9221991-04-04 10:40:29 +00001127 case BINARY_ADD:
1128 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001129 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001130 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001131 /* INLINE: int + int */
1132 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001133 a = PyInt_AS_LONG(v);
1134 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001135 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001136 if ((i^a) < 0 && (i^b) < 0)
1137 goto slow_add;
1138 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001139 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001140 else if (PyString_CheckExact(v) &&
1141 PyString_CheckExact(w)) {
1142 x = string_concatenate(v, w, f, next_instr);
1143 /* string_concatenate consumed the ref to v */
1144 goto skip_decref_vx;
1145 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001146 else {
1147 slow_add:
Guido van Rossumc12da691997-07-17 23:12:42 +00001148 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001149 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001150 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001151 skip_decref_vx:
Guido van Rossumb209a111997-04-29 18:18:01 +00001152 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001153 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001154 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001155 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001156
Guido van Rossum374a9221991-04-04 10:40:29 +00001157 case BINARY_SUBTRACT:
1158 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001159 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001160 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001161 /* INLINE: int - int */
1162 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001163 a = PyInt_AS_LONG(v);
1164 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001165 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001166 if ((i^a) < 0 && (i^~b) < 0)
1167 goto slow_sub;
1168 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001169 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001170 else {
1171 slow_sub:
Guido van Rossumc12da691997-07-17 23:12:42 +00001172 x = PyNumber_Subtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001173 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001174 Py_DECREF(v);
1175 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001176 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001177 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001178 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001179
Guido van Rossum374a9221991-04-04 10:40:29 +00001180 case BINARY_SUBSCR:
1181 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001182 v = TOP();
Tim Petersb1c46982001-10-05 20:41:38 +00001183 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001184 /* INLINE: list[int] */
Neal Norwitz814e9382006-03-02 07:54:28 +00001185 Py_ssize_t i = PyInt_AsSsize_t(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001186 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +00001187 i += PyList_GET_SIZE(v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001188 if (i >= 0 && i < PyList_GET_SIZE(v)) {
Guido van Rossumfa00e951998-07-08 15:02:37 +00001189 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001190 Py_INCREF(x);
1191 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001192 else
1193 goto slow_get;
Guido van Rossumc12da691997-07-17 23:12:42 +00001194 }
1195 else
Raymond Hettinger467a6982004-04-07 11:39:21 +00001196 slow_get:
Guido van Rossumc12da691997-07-17 23:12:42 +00001197 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001198 Py_DECREF(v);
1199 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001200 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001201 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001202 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001203
Guido van Rossum7928cd71991-10-24 14:59:31 +00001204 case BINARY_LSHIFT:
1205 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001206 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001207 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001208 Py_DECREF(v);
1209 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001210 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001211 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001212 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001213
Guido van Rossum7928cd71991-10-24 14:59:31 +00001214 case BINARY_RSHIFT:
1215 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001216 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001217 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001218 Py_DECREF(v);
1219 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001220 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001221 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001222 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001223
Guido van Rossum7928cd71991-10-24 14:59:31 +00001224 case BINARY_AND:
1225 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001226 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001227 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001228 Py_DECREF(v);
1229 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001230 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001231 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001232 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001233
Guido van Rossum7928cd71991-10-24 14:59:31 +00001234 case BINARY_XOR:
1235 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001236 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001237 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001238 Py_DECREF(v);
1239 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001240 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001241 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001242 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001243
Guido van Rossum7928cd71991-10-24 14:59:31 +00001244 case BINARY_OR:
1245 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001246 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001247 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001248 Py_DECREF(v);
1249 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001250 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001251 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001252 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001253
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001254 case LIST_APPEND:
1255 w = POP();
1256 v = POP();
1257 err = PyList_Append(v, w);
1258 Py_DECREF(v);
1259 Py_DECREF(w);
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001260 if (err == 0) {
1261 PREDICT(JUMP_ABSOLUTE);
1262 continue;
1263 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001264 break;
1265
Thomas Wouters434d0822000-08-24 20:11:32 +00001266 case INPLACE_POWER:
1267 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001268 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001269 x = PyNumber_InPlacePower(v, w, Py_None);
1270 Py_DECREF(v);
1271 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001272 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001273 if (x != NULL) continue;
1274 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001275
Thomas Wouters434d0822000-08-24 20:11:32 +00001276 case INPLACE_MULTIPLY:
1277 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001278 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001279 x = PyNumber_InPlaceMultiply(v, w);
1280 Py_DECREF(v);
1281 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001282 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001283 if (x != NULL) continue;
1284 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001285
Thomas Wouters434d0822000-08-24 20:11:32 +00001286 case INPLACE_DIVIDE:
Tim Peters54b11912001-12-25 18:49:11 +00001287 if (!_Py_QnewFlag) {
1288 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001289 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001290 x = PyNumber_InPlaceDivide(v, w);
1291 Py_DECREF(v);
1292 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001293 SET_TOP(x);
Tim Peters54b11912001-12-25 18:49:11 +00001294 if (x != NULL) continue;
1295 break;
1296 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001297 /* -Qnew is in effect: fall through to
Tim Peters54b11912001-12-25 18:49:11 +00001298 INPLACE_TRUE_DIVIDE */
1299 case INPLACE_TRUE_DIVIDE:
Thomas Wouters434d0822000-08-24 20:11:32 +00001300 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001301 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001302 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001303 Py_DECREF(v);
1304 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001305 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001306 if (x != NULL) continue;
1307 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001308
Guido van Rossum4668b002001-08-08 05:00:18 +00001309 case INPLACE_FLOOR_DIVIDE:
1310 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001311 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001312 x = PyNumber_InPlaceFloorDivide(v, w);
1313 Py_DECREF(v);
1314 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001315 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001316 if (x != NULL) continue;
1317 break;
1318
Thomas Wouters434d0822000-08-24 20:11:32 +00001319 case INPLACE_MODULO:
1320 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001321 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001322 x = PyNumber_InPlaceRemainder(v, w);
1323 Py_DECREF(v);
1324 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001325 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001326 if (x != NULL) continue;
1327 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001328
Thomas Wouters434d0822000-08-24 20:11:32 +00001329 case INPLACE_ADD:
1330 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001331 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001332 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001333 /* INLINE: int + int */
1334 register long a, b, i;
1335 a = PyInt_AS_LONG(v);
1336 b = PyInt_AS_LONG(w);
1337 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001338 if ((i^a) < 0 && (i^b) < 0)
1339 goto slow_iadd;
1340 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001341 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001342 else if (PyString_CheckExact(v) &&
1343 PyString_CheckExact(w)) {
1344 x = string_concatenate(v, w, f, next_instr);
1345 /* string_concatenate consumed the ref to v */
1346 goto skip_decref_v;
1347 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001348 else {
1349 slow_iadd:
Thomas Wouters434d0822000-08-24 20:11:32 +00001350 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001351 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001352 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001353 skip_decref_v:
Thomas Wouters434d0822000-08-24 20:11:32 +00001354 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001355 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001356 if (x != NULL) continue;
1357 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001358
Thomas Wouters434d0822000-08-24 20:11:32 +00001359 case INPLACE_SUBTRACT:
1360 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001361 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001362 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001363 /* INLINE: int - int */
1364 register long a, b, i;
1365 a = PyInt_AS_LONG(v);
1366 b = PyInt_AS_LONG(w);
1367 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001368 if ((i^a) < 0 && (i^~b) < 0)
1369 goto slow_isub;
1370 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001371 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001372 else {
1373 slow_isub:
Thomas Wouters434d0822000-08-24 20:11:32 +00001374 x = PyNumber_InPlaceSubtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001375 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001376 Py_DECREF(v);
1377 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001378 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001379 if (x != NULL) continue;
1380 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001381
Thomas Wouters434d0822000-08-24 20:11:32 +00001382 case INPLACE_LSHIFT:
1383 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001384 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001385 x = PyNumber_InPlaceLshift(v, w);
1386 Py_DECREF(v);
1387 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001388 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001389 if (x != NULL) continue;
1390 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001391
Thomas Wouters434d0822000-08-24 20:11:32 +00001392 case INPLACE_RSHIFT:
1393 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001394 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001395 x = PyNumber_InPlaceRshift(v, w);
1396 Py_DECREF(v);
1397 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001398 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001399 if (x != NULL) continue;
1400 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001401
Thomas Wouters434d0822000-08-24 20:11:32 +00001402 case INPLACE_AND:
1403 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001404 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001405 x = PyNumber_InPlaceAnd(v, w);
1406 Py_DECREF(v);
1407 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001408 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001409 if (x != NULL) continue;
1410 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001411
Thomas Wouters434d0822000-08-24 20:11:32 +00001412 case INPLACE_XOR:
1413 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001414 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001415 x = PyNumber_InPlaceXor(v, w);
1416 Py_DECREF(v);
1417 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001418 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001419 if (x != NULL) continue;
1420 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001421
Thomas Wouters434d0822000-08-24 20:11:32 +00001422 case INPLACE_OR:
1423 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001424 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001425 x = PyNumber_InPlaceOr(v, w);
1426 Py_DECREF(v);
1427 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001428 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001429 if (x != NULL) continue;
1430 break;
1431
Guido van Rossum374a9221991-04-04 10:40:29 +00001432 case SLICE+0:
1433 case SLICE+1:
1434 case SLICE+2:
1435 case SLICE+3:
1436 if ((opcode-SLICE) & 2)
1437 w = POP();
1438 else
1439 w = NULL;
1440 if ((opcode-SLICE) & 1)
1441 v = POP();
1442 else
1443 v = NULL;
Raymond Hettinger663004b2003-01-09 15:24:30 +00001444 u = TOP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001445 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001446 Py_DECREF(u);
1447 Py_XDECREF(v);
1448 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001449 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001450 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001451 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001452
Guido van Rossum374a9221991-04-04 10:40:29 +00001453 case STORE_SLICE+0:
1454 case STORE_SLICE+1:
1455 case STORE_SLICE+2:
1456 case STORE_SLICE+3:
1457 if ((opcode-STORE_SLICE) & 2)
1458 w = POP();
1459 else
1460 w = NULL;
1461 if ((opcode-STORE_SLICE) & 1)
1462 v = POP();
1463 else
1464 v = NULL;
1465 u = POP();
1466 t = POP();
1467 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +00001468 Py_DECREF(t);
1469 Py_DECREF(u);
1470 Py_XDECREF(v);
1471 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001472 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001473 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001474
Guido van Rossum374a9221991-04-04 10:40:29 +00001475 case DELETE_SLICE+0:
1476 case DELETE_SLICE+1:
1477 case DELETE_SLICE+2:
1478 case DELETE_SLICE+3:
1479 if ((opcode-DELETE_SLICE) & 2)
1480 w = POP();
1481 else
1482 w = NULL;
1483 if ((opcode-DELETE_SLICE) & 1)
1484 v = POP();
1485 else
1486 v = NULL;
1487 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001488 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +00001489 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00001490 Py_DECREF(u);
1491 Py_XDECREF(v);
1492 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001493 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001494 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001495
Guido van Rossum374a9221991-04-04 10:40:29 +00001496 case STORE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001497 w = TOP();
1498 v = SECOND();
1499 u = THIRD();
1500 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001501 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001502 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001503 Py_DECREF(u);
1504 Py_DECREF(v);
1505 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001506 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001507 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001508
Guido van Rossum374a9221991-04-04 10:40:29 +00001509 case DELETE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001510 w = TOP();
1511 v = SECOND();
1512 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001513 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001514 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001515 Py_DECREF(v);
1516 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001517 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001518 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001519
Guido van Rossum374a9221991-04-04 10:40:29 +00001520 case PRINT_EXPR:
1521 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001522 w = PySys_GetObject("displayhook");
1523 if (w == NULL) {
1524 PyErr_SetString(PyExc_RuntimeError,
1525 "lost sys.displayhook");
1526 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001527 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001528 }
1529 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001530 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001531 if (x == NULL)
1532 err = -1;
1533 }
1534 if (err == 0) {
1535 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001536 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001537 if (w == NULL)
1538 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001539 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001540 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001541 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001542 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001543
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001544 case PRINT_ITEM_TO:
1545 w = stream = POP();
1546 /* fall through to PRINT_ITEM */
1547
Guido van Rossum374a9221991-04-04 10:40:29 +00001548 case PRINT_ITEM:
1549 v = POP();
Barry Warsaw093abe02000-08-29 04:56:13 +00001550 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001551 w = PySys_GetObject("stdout");
1552 if (w == NULL) {
1553 PyErr_SetString(PyExc_RuntimeError,
1554 "lost sys.stdout");
1555 err = -1;
1556 }
Guido van Rossum8f183201997-12-31 05:53:15 +00001557 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001558 /* PyFile_SoftSpace() can exececute arbitrary code
1559 if sys.stdout is an instance with a __getattr__.
1560 If __getattr__ raises an exception, w will
1561 be freed, so we need to prevent that temporarily. */
1562 Py_XINCREF(w);
Tim Peters8e5fd532002-03-24 19:25:00 +00001563 if (w != NULL && PyFile_SoftSpace(w, 0))
Guido van Rossumbe270261997-05-22 22:26:18 +00001564 err = PyFile_WriteString(" ", w);
1565 if (err == 0)
1566 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001567 if (err == 0) {
Tim Peters8e5fd532002-03-24 19:25:00 +00001568 /* XXX move into writeobject() ? */
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001569 if (PyString_Check(v)) {
1570 char *s = PyString_AS_STRING(v);
Martin v. Löwis66851282006-04-22 11:40:03 +00001571 Py_ssize_t len = PyString_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001572 if (len == 0 ||
1573 !isspace(Py_CHARMASK(s[len-1])) ||
1574 s[len-1] == ' ')
1575 PyFile_SoftSpace(w, 1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001576 }
Martin v. Löwis8d3ce5a2001-12-18 22:36:40 +00001577#ifdef Py_USING_UNICODE
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001578 else if (PyUnicode_Check(v)) {
1579 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
Martin v. Löwis66851282006-04-22 11:40:03 +00001580 Py_ssize_t len = PyUnicode_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001581 if (len == 0 ||
1582 !Py_UNICODE_ISSPACE(s[len-1]) ||
1583 s[len-1] == ' ')
1584 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001585 }
Michael W. Hudsond95c8282002-05-20 13:56:11 +00001586#endif
Tim Peters8e5fd532002-03-24 19:25:00 +00001587 else
1588 PyFile_SoftSpace(w, 1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001589 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001590 Py_XDECREF(w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001591 Py_DECREF(v);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001592 Py_XDECREF(stream);
1593 stream = NULL;
1594 if (err == 0)
1595 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001596 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001597
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001598 case PRINT_NEWLINE_TO:
1599 w = stream = POP();
1600 /* fall through to PRINT_NEWLINE */
1601
Guido van Rossum374a9221991-04-04 10:40:29 +00001602 case PRINT_NEWLINE:
Barry Warsaw093abe02000-08-29 04:56:13 +00001603 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001604 w = PySys_GetObject("stdout");
1605 if (w == NULL)
1606 PyErr_SetString(PyExc_RuntimeError,
1607 "lost sys.stdout");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001608 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001609 if (w != NULL) {
1610 err = PyFile_WriteString("\n", w);
1611 if (err == 0)
1612 PyFile_SoftSpace(w, 0);
1613 }
1614 Py_XDECREF(stream);
1615 stream = NULL;
Guido van Rossum374a9221991-04-04 10:40:29 +00001616 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001617
Thomas Wouters434d0822000-08-24 20:11:32 +00001618
1619#ifdef CASE_TOO_BIG
1620 default: switch (opcode) {
1621#endif
Guido van Rossumf10570b1995-07-07 22:53:21 +00001622 case RAISE_VARARGS:
1623 u = v = w = NULL;
1624 switch (oparg) {
1625 case 3:
1626 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001627 /* Fallthrough */
1628 case 2:
1629 v = POP(); /* value */
1630 /* Fallthrough */
1631 case 1:
1632 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001633 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001634 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001635 break;
1636 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001637 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001638 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001639 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001640 break;
1641 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001642 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001643
Guido van Rossum374a9221991-04-04 10:40:29 +00001644 case LOAD_LOCALS:
Raymond Hettinger467a6982004-04-07 11:39:21 +00001645 if ((x = f->f_locals) != NULL) {
1646 Py_INCREF(x);
1647 PUSH(x);
1648 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001649 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001650 PyErr_SetString(PyExc_SystemError, "no locals");
Guido van Rossum374a9221991-04-04 10:40:29 +00001651 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001652
Guido van Rossum374a9221991-04-04 10:40:29 +00001653 case RETURN_VALUE:
1654 retval = POP();
1655 why = WHY_RETURN;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001656 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001657
Tim Peters5ca576e2001-06-18 22:08:13 +00001658 case YIELD_VALUE:
1659 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001660 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001661 why = WHY_YIELD;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001662 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001663
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001664 case EXEC_STMT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001665 w = TOP();
1666 v = SECOND();
1667 u = THIRD();
1668 STACKADJ(-3);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001669 READ_TIMESTAMP(intr0);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001670 err = exec_statement(f, u, v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001671 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00001672 Py_DECREF(u);
1673 Py_DECREF(v);
1674 Py_DECREF(w);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001675 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001676
Guido van Rossum374a9221991-04-04 10:40:29 +00001677 case POP_BLOCK:
1678 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001679 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001680 while (STACK_LEVEL() > b->b_level) {
1681 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001682 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001683 }
1684 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001685 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001686
Guido van Rossum374a9221991-04-04 10:40:29 +00001687 case END_FINALLY:
1688 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001689 if (PyInt_Check(v)) {
Raymond Hettinger7c958652004-04-06 10:11:10 +00001690 why = (enum why_code) PyInt_AS_LONG(v);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001691 assert(why != WHY_YIELD);
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00001692 if (why == WHY_RETURN ||
1693 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001694 retval = POP();
1695 }
Brett Cannonbf364092006-03-01 04:25:17 +00001696 else if (PyExceptionClass_Check(v) || PyString_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001697 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001698 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001699 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001700 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001701 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001702 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001703 else if (v != Py_None) {
1704 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001705 "'finally' pops bad exception");
1706 why = WHY_EXCEPTION;
1707 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001708 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001709 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001710
Guido van Rossum374a9221991-04-04 10:40:29 +00001711 case BUILD_CLASS:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001712 u = TOP();
1713 v = SECOND();
1714 w = THIRD();
1715 STACKADJ(-2);
Guido van Rossum25831651993-05-19 14:50:45 +00001716 x = build_class(u, v, w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001717 SET_TOP(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001718 Py_DECREF(u);
1719 Py_DECREF(v);
1720 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001721 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001722
Guido van Rossum374a9221991-04-04 10:40:29 +00001723 case STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001724 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001725 v = POP();
Raymond Hettinger467a6982004-04-07 11:39:21 +00001726 if ((x = f->f_locals) != NULL) {
Raymond Hettinger66bd2332004-08-02 08:30:07 +00001727 if (PyDict_CheckExact(x))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001728 err = PyDict_SetItem(x, w, v);
1729 else
1730 err = PyObject_SetItem(x, w, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001731 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001732 if (err == 0) continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001733 break;
1734 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001735 PyErr_Format(PyExc_SystemError,
1736 "no locals found when storing %s",
1737 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001738 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001739
Guido van Rossum374a9221991-04-04 10:40:29 +00001740 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001741 w = GETITEM(names, oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001742 if ((x = f->f_locals) != NULL) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001743 if ((err = PyObject_DelItem(x, w)) != 0)
Raymond Hettinger467a6982004-04-07 11:39:21 +00001744 format_exc_check_arg(PyExc_NameError,
1745 NAME_ERROR_MSG ,w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001746 break;
1747 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001748 PyErr_Format(PyExc_SystemError,
1749 "no locals when deleting %s",
1750 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001751 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001752
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001753 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001754 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001755 v = POP();
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001756 if (PyTuple_CheckExact(v) && PyTuple_GET_SIZE(v) == oparg) {
1757 PyObject **items = ((PyTupleObject *)v)->ob_item;
1758 while (oparg--) {
1759 w = items[oparg];
1760 Py_INCREF(w);
1761 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001762 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001763 Py_DECREF(v);
1764 continue;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001765 } else if (PyList_CheckExact(v) && PyList_GET_SIZE(v) == oparg) {
1766 PyObject **items = ((PyListObject *)v)->ob_item;
1767 while (oparg--) {
1768 w = items[oparg];
1769 Py_INCREF(w);
1770 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001771 }
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001772 } else if (unpack_iterable(v, oparg,
Tim Petersd6d010b2001-06-21 02:49:55 +00001773 stack_pointer + oparg))
1774 stack_pointer += oparg;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001775 else {
1776 if (PyErr_ExceptionMatches(PyExc_TypeError))
1777 PyErr_SetString(PyExc_TypeError,
1778 "unpack non-sequence");
Barry Warsawe42b18f1997-08-25 22:13:04 +00001779 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001780 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001781 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001782 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001783
Guido van Rossum374a9221991-04-04 10:40:29 +00001784 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001785 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001786 v = TOP();
1787 u = SECOND();
1788 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001789 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1790 Py_DECREF(v);
1791 Py_DECREF(u);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001792 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001793 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001794
Guido van Rossum374a9221991-04-04 10:40:29 +00001795 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001796 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001797 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001798 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1799 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001800 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001801 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001802
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001803 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001804 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001805 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001806 err = PyDict_SetItem(f->f_globals, w, v);
1807 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001808 if (err == 0) continue;
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001809 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001810
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001811 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001812 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001813 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001814 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001815 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001816 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001817
Guido van Rossum374a9221991-04-04 10:40:29 +00001818 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001819 w = GETITEM(names, oparg);
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001820 if ((v = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001821 PyErr_Format(PyExc_SystemError,
1822 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001823 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001824 break;
1825 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001826 if (PyDict_CheckExact(v)) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001827 x = PyDict_GetItem(v, w);
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001828 Py_XINCREF(x);
1829 }
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001830 else {
1831 x = PyObject_GetItem(v, w);
1832 if (x == NULL && PyErr_Occurred()) {
1833 if (!PyErr_ExceptionMatches(PyExc_KeyError))
1834 break;
1835 PyErr_Clear();
1836 }
1837 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001838 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001839 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001840 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001841 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001842 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001843 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001844 PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001845 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001846 break;
1847 }
1848 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001849 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001850 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001851 PUSH(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001852 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001853
Guido van Rossum374a9221991-04-04 10:40:29 +00001854 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001855 w = GETITEM(names, oparg);
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001856 if (PyString_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00001857 /* Inline the PyDict_GetItem() calls.
1858 WARNING: this is an extreme speed hack.
1859 Do not try this at home. */
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001860 long hash = ((PyStringObject *)w)->ob_shash;
1861 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001862 PyDictObject *d;
1863 d = (PyDictObject *)(f->f_globals);
1864 x = d->ma_lookup(d, w, hash)->me_value;
1865 if (x != NULL) {
1866 Py_INCREF(x);
1867 PUSH(x);
1868 continue;
1869 }
1870 d = (PyDictObject *)(f->f_builtins);
1871 x = d->ma_lookup(d, w, hash)->me_value;
1872 if (x != NULL) {
1873 Py_INCREF(x);
1874 PUSH(x);
1875 continue;
1876 }
1877 goto load_global_error;
1878 }
1879 }
1880 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00001881 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001882 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001883 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001884 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001885 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00001886 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001887 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001888 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001889 break;
1890 }
1891 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001892 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001893 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001894 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001895
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001896 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001897 x = GETLOCAL(oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001898 if (x != NULL) {
1899 SETLOCAL(oparg, NULL);
1900 continue;
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001901 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001902 format_exc_check_arg(
1903 PyExc_UnboundLocalError,
1904 UNBOUNDLOCAL_ERROR_MSG,
1905 PyTuple_GetItem(co->co_varnames, oparg)
1906 );
1907 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001908
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001909 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001910 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001911 Py_INCREF(x);
1912 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001913 if (x != NULL) continue;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001914 break;
1915
1916 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001917 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001918 w = PyCell_Get(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001919 if (w != NULL) {
1920 PUSH(w);
1921 continue;
Jeremy Hylton2524d692001-02-05 17:23:16 +00001922 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001923 err = -1;
1924 /* Don't stomp existing exception */
1925 if (PyErr_Occurred())
1926 break;
Richard Jonescebbefc2006-05-23 18:28:17 +00001927 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
1928 v = PyTuple_GET_ITEM(co->co_cellvars,
Raymond Hettinger467a6982004-04-07 11:39:21 +00001929 oparg);
1930 format_exc_check_arg(
1931 PyExc_UnboundLocalError,
1932 UNBOUNDLOCAL_ERROR_MSG,
1933 v);
1934 } else {
Richard Jonescebbefc2006-05-23 18:28:17 +00001935 v = PyTuple_GET_ITEM(
Raymond Hettinger467a6982004-04-07 11:39:21 +00001936 co->co_freevars,
Richard Jonescebbefc2006-05-23 18:28:17 +00001937 oparg - PyTuple_GET_SIZE(co->co_cellvars));
Raymond Hettinger467a6982004-04-07 11:39:21 +00001938 format_exc_check_arg(
1939 PyExc_NameError,
1940 UNBOUNDFREE_ERROR_MSG,
1941 v);
1942 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001943 break;
1944
1945 case STORE_DEREF:
1946 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001947 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001948 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00001949 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001950 continue;
1951
Guido van Rossum374a9221991-04-04 10:40:29 +00001952 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001953 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001954 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001955 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001956 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001957 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001958 }
1959 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001960 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001961 }
1962 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001963
Guido van Rossum374a9221991-04-04 10:40:29 +00001964 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00001965 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001966 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001967 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001968 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00001969 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001970 }
1971 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001972 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001973 }
1974 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001975
Guido van Rossum374a9221991-04-04 10:40:29 +00001976 case BUILD_MAP:
Guido van Rossumb209a111997-04-29 18:18:01 +00001977 x = PyDict_New();
Guido van Rossum374a9221991-04-04 10:40:29 +00001978 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001979 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001980 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001981
Guido van Rossum374a9221991-04-04 10:40:29 +00001982 case LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001983 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001984 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001985 x = PyObject_GetAttr(v, w);
1986 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001987 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001988 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001989 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001990
Guido van Rossum374a9221991-04-04 10:40:29 +00001991 case COMPARE_OP:
1992 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001993 v = TOP();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001994 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001995 /* INLINE: cmp(int, int) */
1996 register long a, b;
1997 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001998 a = PyInt_AS_LONG(v);
1999 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00002000 switch (oparg) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00002001 case PyCmp_LT: res = a < b; break;
2002 case PyCmp_LE: res = a <= b; break;
2003 case PyCmp_EQ: res = a == b; break;
2004 case PyCmp_NE: res = a != b; break;
2005 case PyCmp_GT: res = a > b; break;
2006 case PyCmp_GE: res = a >= b; break;
2007 case PyCmp_IS: res = v == w; break;
2008 case PyCmp_IS_NOT: res = v != w; break;
Guido van Rossumc12da691997-07-17 23:12:42 +00002009 default: goto slow_compare;
2010 }
2011 x = res ? Py_True : Py_False;
2012 Py_INCREF(x);
2013 }
2014 else {
2015 slow_compare:
2016 x = cmp_outcome(oparg, v, w);
2017 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002018 Py_DECREF(v);
2019 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002020 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00002021 if (x == NULL) break;
2022 PREDICT(JUMP_IF_FALSE);
2023 PREDICT(JUMP_IF_TRUE);
2024 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002025
Guido van Rossum374a9221991-04-04 10:40:29 +00002026 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00002027 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002028 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002029 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002030 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00002031 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002032 break;
2033 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002034 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002035 u = TOP();
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002036 if (PyInt_AsLong(u) != -1 || PyErr_Occurred())
2037 w = PyTuple_Pack(5,
2038 w,
2039 f->f_globals,
2040 f->f_locals == NULL ?
2041 Py_None : f->f_locals,
2042 v,
2043 u);
2044 else
2045 w = PyTuple_Pack(4,
2046 w,
2047 f->f_globals,
2048 f->f_locals == NULL ?
2049 Py_None : f->f_locals,
2050 v);
2051 Py_DECREF(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002052 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002053 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002054 u = POP();
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002055 x = NULL;
2056 break;
2057 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002058 READ_TIMESTAMP(intr0);
Guido van Rossumb209a111997-04-29 18:18:01 +00002059 x = PyEval_CallObject(x, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002060 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002061 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002062 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002063 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002064 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002065
Thomas Wouters52152252000-08-17 22:55:00 +00002066 case IMPORT_STAR:
2067 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002068 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002069 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002070 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00002071 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002072 break;
2073 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002074 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002075 err = import_all_from(x, v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002076 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002077 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00002078 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002079 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002080 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002081
Thomas Wouters52152252000-08-17 22:55:00 +00002082 case IMPORT_FROM:
Skip Montanaro496e6582002-08-06 17:47:40 +00002083 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00002084 v = TOP();
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002085 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002086 x = import_from(v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002087 READ_TIMESTAMP(intr1);
Thomas Wouters52152252000-08-17 22:55:00 +00002088 PUSH(x);
2089 if (x != NULL) continue;
2090 break;
2091
Guido van Rossum374a9221991-04-04 10:40:29 +00002092 case JUMP_FORWARD:
2093 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002094 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002095
Raymond Hettingerf606f872003-03-16 03:11:04 +00002096 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002097 case JUMP_IF_FALSE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002098 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002099 if (w == Py_True) {
2100 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002101 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002102 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002103 if (w == Py_False) {
2104 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002105 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002106 }
2107 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002108 if (err > 0)
2109 err = 0;
2110 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00002111 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002112 else
2113 break;
2114 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002115
Raymond Hettingerf606f872003-03-16 03:11:04 +00002116 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002117 case JUMP_IF_TRUE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002118 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002119 if (w == Py_False) {
2120 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002121 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002122 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002123 if (w == Py_True) {
2124 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002125 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002126 }
2127 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002128 if (err > 0) {
2129 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00002130 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002131 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002132 else if (err == 0)
2133 ;
2134 else
2135 break;
2136 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002137
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00002138 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002139 case JUMP_ABSOLUTE:
2140 JUMPTO(oparg);
Neil Schemenauerca2a2f12003-05-30 23:59:44 +00002141 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002142
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002143 case GET_ITER:
2144 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002145 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002146 x = PyObject_GetIter(v);
2147 Py_DECREF(v);
2148 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002149 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002150 PREDICT(FOR_ITER);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002151 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002152 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002153 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002154 break;
2155
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002156 PREDICTED_WITH_ARG(FOR_ITER);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002157 case FOR_ITER:
2158 /* before: [iter]; after: [iter, iter()] *or* [] */
2159 v = TOP();
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002160 x = (*v->ob_type->tp_iternext)(v);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002161 if (x != NULL) {
2162 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002163 PREDICT(STORE_FAST);
2164 PREDICT(UNPACK_SEQUENCE);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002165 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002166 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002167 if (PyErr_Occurred()) {
2168 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2169 break;
2170 PyErr_Clear();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002171 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002172 /* iterator ended normally */
2173 x = v = POP();
2174 Py_DECREF(v);
2175 JUMPBY(oparg);
2176 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002177
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002178 case BREAK_LOOP:
2179 why = WHY_BREAK;
2180 goto fast_block_end;
2181
2182 case CONTINUE_LOOP:
2183 retval = PyInt_FromLong(oparg);
Neal Norwitz02104df2006-05-19 06:31:23 +00002184 if (!retval) {
2185 x = NULL;
2186 break;
2187 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002188 why = WHY_CONTINUE;
2189 goto fast_block_end;
2190
Guido van Rossum374a9221991-04-04 10:40:29 +00002191 case SETUP_LOOP:
2192 case SETUP_EXCEPT:
2193 case SETUP_FINALLY:
Phillip J. Eby2ba96612006-04-10 17:51:05 +00002194 /* NOTE: If you add any new block-setup opcodes that are not try/except/finally
2195 handlers, you may need to update the PyGen_NeedsFinalizing() function. */
2196
Guido van Rossumb209a111997-04-29 18:18:01 +00002197 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002198 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002199 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002200
Guido van Rossumc2e20742006-02-27 22:32:47 +00002201 case WITH_CLEANUP:
2202 {
2203 /* TOP is the context.__exit__ bound method.
2204 Below that are 1-3 values indicating how/why
2205 we entered the finally clause:
2206 - SECOND = None
Guido van Rossumf6694362006-03-10 02:28:35 +00002207 - (SECOND, THIRD) = (WHY_{RETURN,CONTINUE}), retval
Guido van Rossumc2e20742006-02-27 22:32:47 +00002208 - SECOND = WHY_*; no retval below it
2209 - (SECOND, THIRD, FOURTH) = exc_info()
2210 In the last case, we must call
2211 TOP(SECOND, THIRD, FOURTH)
2212 otherwise we must call
2213 TOP(None, None, None)
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002214
2215 In addition, if the stack represents an exception,
Guido van Rossumf6694362006-03-10 02:28:35 +00002216 *and* the function call returns a 'true' value, we
2217 "zap" this information, to prevent END_FINALLY from
2218 re-raising the exception. (But non-local gotos
2219 should still be resumed.)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002220 */
2221
2222 x = TOP();
2223 u = SECOND();
2224 if (PyInt_Check(u) || u == Py_None) {
2225 u = v = w = Py_None;
2226 }
2227 else {
2228 v = THIRD();
2229 w = FOURTH();
2230 }
Guido van Rossumf6694362006-03-10 02:28:35 +00002231 /* XXX Not the fastest way to call it... */
2232 x = PyObject_CallFunctionObjArgs(x, u, v, w, NULL);
2233 if (x == NULL)
2234 break; /* Go to error exit */
2235 if (u != Py_None && PyObject_IsTrue(x)) {
2236 /* There was an exception and a true return */
2237 Py_DECREF(x);
2238 x = TOP(); /* Again */
2239 STACKADJ(-3);
2240 Py_INCREF(Py_None);
2241 SET_TOP(Py_None);
2242 Py_DECREF(x);
2243 Py_DECREF(u);
2244 Py_DECREF(v);
2245 Py_DECREF(w);
2246 } else {
2247 /* Let END_FINALLY do its thing */
2248 Py_DECREF(x);
2249 x = POP();
2250 Py_DECREF(x);
2251 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002252 break;
2253 }
2254
Guido van Rossumf10570b1995-07-07 22:53:21 +00002255 case CALL_FUNCTION:
Armin Rigo8817fcd2004-06-17 10:22:40 +00002256 {
2257 PyObject **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002258 PCALL(PCALL_ALL);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002259 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002260#ifdef WITH_TSC
Armin Rigo8817fcd2004-06-17 10:22:40 +00002261 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002262#else
Armin Rigo8817fcd2004-06-17 10:22:40 +00002263 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002264#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002265 stack_pointer = sp;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002266 PUSH(x);
2267 if (x != NULL)
2268 continue;
2269 break;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002270 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002271
Jeremy Hylton76901512000-03-28 23:49:17 +00002272 case CALL_FUNCTION_VAR:
2273 case CALL_FUNCTION_KW:
2274 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002275 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002276 int na = oparg & 0xff;
2277 int nk = (oparg>>8) & 0xff;
2278 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002279 int n = na + 2 * nk;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002280 PyObject **pfunc, *func, **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002281 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002282 if (flags & CALL_FLAG_VAR)
2283 n++;
2284 if (flags & CALL_FLAG_KW)
2285 n++;
2286 pfunc = stack_pointer - n - 1;
2287 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002288
Guido van Rossumac7be682001-01-17 15:42:30 +00002289 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002290 && PyMethod_GET_SELF(func) != NULL) {
2291 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002292 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002293 func = PyMethod_GET_FUNCTION(func);
2294 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002295 Py_DECREF(*pfunc);
2296 *pfunc = self;
2297 na++;
2298 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002299 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002300 Py_INCREF(func);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002301 sp = stack_pointer;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002302 READ_TIMESTAMP(intr0);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002303 x = ext_do_call(func, &sp, flags, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002304 READ_TIMESTAMP(intr1);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002305 stack_pointer = sp;
Jeremy Hylton76901512000-03-28 23:49:17 +00002306 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002307
Jeremy Hylton76901512000-03-28 23:49:17 +00002308 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002309 w = POP();
2310 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002311 }
2312 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002313 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002314 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002315 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002316 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002317
Guido van Rossum681d79a1995-07-18 14:51:37 +00002318 case MAKE_FUNCTION:
2319 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002320 x = PyFunction_New(v, f->f_globals);
2321 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002322 /* XXX Maybe this should be a separate opcode? */
2323 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002324 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002325 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002326 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002327 x = NULL;
2328 break;
2329 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002330 while (--oparg >= 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002331 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002332 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002333 }
2334 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002335 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002336 }
2337 PUSH(x);
2338 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002339
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002340 case MAKE_CLOSURE:
2341 {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002342 v = POP(); /* code object */
2343 x = PyFunction_New(v, f->f_globals);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002344 Py_DECREF(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002345 if (x != NULL) {
2346 v = POP();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002347 err = PyFunction_SetClosure(x, v);
2348 Py_DECREF(v);
2349 }
2350 if (x != NULL && oparg > 0) {
2351 v = PyTuple_New(oparg);
2352 if (v == NULL) {
2353 Py_DECREF(x);
2354 x = NULL;
2355 break;
2356 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002357 while (--oparg >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002358 w = POP();
2359 PyTuple_SET_ITEM(v, oparg, w);
2360 }
2361 err = PyFunction_SetDefaults(x, v);
2362 Py_DECREF(v);
2363 }
2364 PUSH(x);
2365 break;
2366 }
2367
Guido van Rossum8861b741996-07-30 16:49:37 +00002368 case BUILD_SLICE:
2369 if (oparg == 3)
2370 w = POP();
2371 else
2372 w = NULL;
2373 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002374 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002375 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002376 Py_DECREF(u);
2377 Py_DECREF(v);
2378 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002379 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002380 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002381 break;
2382
Fred Drakeef8ace32000-08-24 00:32:09 +00002383 case EXTENDED_ARG:
2384 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002385 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002386 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002387
Guido van Rossum374a9221991-04-04 10:40:29 +00002388 default:
2389 fprintf(stderr,
2390 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002391 PyCode_Addr2Line(f->f_code, f->f_lasti),
2392 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002393 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002394 why = WHY_EXCEPTION;
2395 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002396
2397#ifdef CASE_TOO_BIG
2398 }
2399#endif
2400
Guido van Rossum374a9221991-04-04 10:40:29 +00002401 } /* switch */
2402
2403 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002404
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002405 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002406
Guido van Rossum374a9221991-04-04 10:40:29 +00002407 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002408
Guido van Rossum374a9221991-04-04 10:40:29 +00002409 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002410 if (err == 0 && x != NULL) {
2411#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002412 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002413 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002414 fprintf(stderr,
2415 "XXX undetected error\n");
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002416 else {
2417#endif
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002418 READ_TIMESTAMP(loop1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002419 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002420#ifdef CHECKEXC
2421 }
2422#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +00002423 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002424 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002425 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002426 err = 0;
2427 }
2428
Guido van Rossum374a9221991-04-04 10:40:29 +00002429 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002430
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002431 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002432 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002433 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002434 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002435 why = WHY_EXCEPTION;
2436 }
2437 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002438#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002439 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002440 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002441 if (PyErr_Occurred()) {
Jeremy Hylton904ed862003-11-05 17:29:35 +00002442 char buf[1024];
2443 sprintf(buf, "Stack unwind with exception "
2444 "set and why=%d", why);
2445 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002446 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002447 }
2448#endif
2449
2450 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002451
Guido van Rossum374a9221991-04-04 10:40:29 +00002452 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002453 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002454
Fred Drake8f51f542001-10-04 14:48:42 +00002455 if (tstate->c_tracefunc != NULL)
2456 call_exc_trace(tstate->c_tracefunc,
2457 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002458 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002459
Guido van Rossum374a9221991-04-04 10:40:29 +00002460 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002461
Guido van Rossum374a9221991-04-04 10:40:29 +00002462 if (why == WHY_RERAISE)
2463 why = WHY_EXCEPTION;
2464
2465 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002466
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002467fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002468 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002469 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002470
Tim Peters8a5c3c72004-04-05 19:36:21 +00002471 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002472 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2473 /* For a continue inside a try block,
2474 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002475 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2476 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002477 why = WHY_NOT;
2478 JUMPTO(PyInt_AS_LONG(retval));
2479 Py_DECREF(retval);
2480 break;
2481 }
2482
Guido van Rossum374a9221991-04-04 10:40:29 +00002483 while (STACK_LEVEL() > b->b_level) {
2484 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002485 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002486 }
2487 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2488 why = WHY_NOT;
2489 JUMPTO(b->b_handler);
2490 break;
2491 }
2492 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002493 (b->b_type == SETUP_EXCEPT &&
2494 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002495 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002496 PyObject *exc, *val, *tb;
2497 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002498 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002499 val = Py_None;
2500 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002501 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002502 /* Make the raw exception data
2503 available to the handler,
2504 so a program can emulate the
2505 Python main loop. Don't do
2506 this for 'finally'. */
2507 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002508 PyErr_NormalizeException(
2509 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002510 set_exc_info(tstate,
2511 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002512 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002513 if (tb == NULL) {
2514 Py_INCREF(Py_None);
2515 PUSH(Py_None);
2516 } else
2517 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002518 PUSH(val);
2519 PUSH(exc);
2520 }
2521 else {
Raymond Hettinger06032cb2004-04-06 09:37:35 +00002522 if (why & (WHY_RETURN | WHY_CONTINUE))
Guido van Rossum374a9221991-04-04 10:40:29 +00002523 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002524 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002525 PUSH(v);
2526 }
2527 why = WHY_NOT;
2528 JUMPTO(b->b_handler);
2529 break;
2530 }
2531 } /* unwind stack */
2532
2533 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002534
Guido van Rossum374a9221991-04-04 10:40:29 +00002535 if (why != WHY_NOT)
2536 break;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002537 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002538
Guido van Rossum374a9221991-04-04 10:40:29 +00002539 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002540
Tim Peters8a5c3c72004-04-05 19:36:21 +00002541 assert(why != WHY_YIELD);
2542 /* Pop remaining stack entries. */
2543 while (!EMPTY()) {
2544 v = POP();
2545 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002546 }
2547
Tim Peters8a5c3c72004-04-05 19:36:21 +00002548 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002549 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002550
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002551fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002552 if (tstate->use_tracing) {
Barry Warsawe2eca0b2005-08-15 18:14:19 +00002553 if (tstate->c_tracefunc) {
2554 if (why == WHY_RETURN || why == WHY_YIELD) {
2555 if (call_trace(tstate->c_tracefunc,
2556 tstate->c_traceobj, f,
2557 PyTrace_RETURN, retval)) {
2558 Py_XDECREF(retval);
2559 retval = NULL;
2560 why = WHY_EXCEPTION;
2561 }
2562 }
2563 else if (why == WHY_EXCEPTION) {
2564 call_trace_protected(tstate->c_tracefunc,
2565 tstate->c_traceobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002566 PyTrace_RETURN, NULL);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002567 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002568 }
Fred Drake8f51f542001-10-04 14:48:42 +00002569 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002570 if (why == WHY_EXCEPTION)
2571 call_trace_protected(tstate->c_profilefunc,
2572 tstate->c_profileobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002573 PyTrace_RETURN, NULL);
Fred Drake4ec5d562001-10-04 19:26:43 +00002574 else if (call_trace(tstate->c_profilefunc,
2575 tstate->c_profileobj, f,
2576 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002577 Py_XDECREF(retval);
2578 retval = NULL;
2579 why = WHY_EXCEPTION;
2580 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002581 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002582 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002583
Guido van Rossuma027efa1997-05-05 20:56:21 +00002584 reset_exc_info(tstate);
2585
Tim Peters5ca576e2001-06-18 22:08:13 +00002586 /* pop frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +00002587 exit_eval_frame:
2588 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002589 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002590
Guido van Rossum96a42c81992-01-12 02:29:51 +00002591 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002592}
2593
Guido van Rossumc2e20742006-02-27 22:32:47 +00002594/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002595 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00002596 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002597
Tim Peters6d6c1a32001-08-02 04:15:00 +00002598PyObject *
2599PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002600 PyObject **args, int argcount, PyObject **kws, int kwcount,
2601 PyObject **defs, int defcount, PyObject *closure)
2602{
2603 register PyFrameObject *f;
2604 register PyObject *retval = NULL;
2605 register PyObject **fastlocals, **freevars;
2606 PyThreadState *tstate = PyThreadState_GET();
2607 PyObject *x, *u;
2608
2609 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00002610 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002611 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002612 return NULL;
2613 }
2614
Jeremy Hylton985eba52003-02-05 23:13:00 +00002615 assert(globals != NULL);
2616 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002617 if (f == NULL)
2618 return NULL;
2619
2620 fastlocals = f->f_localsplus;
Richard Jonescebbefc2006-05-23 18:28:17 +00002621 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00002622
2623 if (co->co_argcount > 0 ||
2624 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2625 int i;
2626 int n = argcount;
2627 PyObject *kwdict = NULL;
2628 if (co->co_flags & CO_VARKEYWORDS) {
2629 kwdict = PyDict_New();
2630 if (kwdict == NULL)
2631 goto fail;
2632 i = co->co_argcount;
2633 if (co->co_flags & CO_VARARGS)
2634 i++;
2635 SETLOCAL(i, kwdict);
2636 }
2637 if (argcount > co->co_argcount) {
2638 if (!(co->co_flags & CO_VARARGS)) {
2639 PyErr_Format(PyExc_TypeError,
2640 "%.200s() takes %s %d "
2641 "%sargument%s (%d given)",
2642 PyString_AsString(co->co_name),
2643 defcount ? "at most" : "exactly",
2644 co->co_argcount,
2645 kwcount ? "non-keyword " : "",
2646 co->co_argcount == 1 ? "" : "s",
2647 argcount);
2648 goto fail;
2649 }
2650 n = co->co_argcount;
2651 }
2652 for (i = 0; i < n; i++) {
2653 x = args[i];
2654 Py_INCREF(x);
2655 SETLOCAL(i, x);
2656 }
2657 if (co->co_flags & CO_VARARGS) {
2658 u = PyTuple_New(argcount - n);
2659 if (u == NULL)
2660 goto fail;
2661 SETLOCAL(co->co_argcount, u);
2662 for (i = n; i < argcount; i++) {
2663 x = args[i];
2664 Py_INCREF(x);
2665 PyTuple_SET_ITEM(u, i-n, x);
2666 }
2667 }
2668 for (i = 0; i < kwcount; i++) {
2669 PyObject *keyword = kws[2*i];
2670 PyObject *value = kws[2*i + 1];
2671 int j;
2672 if (keyword == NULL || !PyString_Check(keyword)) {
2673 PyErr_Format(PyExc_TypeError,
2674 "%.200s() keywords must be strings",
2675 PyString_AsString(co->co_name));
2676 goto fail;
2677 }
2678 /* XXX slow -- speed up using dictionary? */
2679 for (j = 0; j < co->co_argcount; j++) {
2680 PyObject *nm = PyTuple_GET_ITEM(
2681 co->co_varnames, j);
2682 int cmp = PyObject_RichCompareBool(
2683 keyword, nm, Py_EQ);
2684 if (cmp > 0)
2685 break;
2686 else if (cmp < 0)
2687 goto fail;
2688 }
2689 /* Check errors from Compare */
2690 if (PyErr_Occurred())
2691 goto fail;
2692 if (j >= co->co_argcount) {
2693 if (kwdict == NULL) {
2694 PyErr_Format(PyExc_TypeError,
2695 "%.200s() got an unexpected "
2696 "keyword argument '%.400s'",
2697 PyString_AsString(co->co_name),
2698 PyString_AsString(keyword));
2699 goto fail;
2700 }
2701 PyDict_SetItem(kwdict, keyword, value);
2702 }
2703 else {
2704 if (GETLOCAL(j) != NULL) {
2705 PyErr_Format(PyExc_TypeError,
2706 "%.200s() got multiple "
2707 "values for keyword "
2708 "argument '%.400s'",
2709 PyString_AsString(co->co_name),
2710 PyString_AsString(keyword));
2711 goto fail;
2712 }
2713 Py_INCREF(value);
2714 SETLOCAL(j, value);
2715 }
2716 }
2717 if (argcount < co->co_argcount) {
2718 int m = co->co_argcount - defcount;
2719 for (i = argcount; i < m; i++) {
2720 if (GETLOCAL(i) == NULL) {
2721 PyErr_Format(PyExc_TypeError,
2722 "%.200s() takes %s %d "
2723 "%sargument%s (%d given)",
2724 PyString_AsString(co->co_name),
2725 ((co->co_flags & CO_VARARGS) ||
2726 defcount) ? "at least"
2727 : "exactly",
2728 m, kwcount ? "non-keyword " : "",
2729 m == 1 ? "" : "s", i);
2730 goto fail;
2731 }
2732 }
2733 if (n > m)
2734 i = n - m;
2735 else
2736 i = 0;
2737 for (; i < defcount; i++) {
2738 if (GETLOCAL(m+i) == NULL) {
2739 PyObject *def = defs[i];
2740 Py_INCREF(def);
2741 SETLOCAL(m+i, def);
2742 }
2743 }
2744 }
2745 }
2746 else {
2747 if (argcount > 0 || kwcount > 0) {
2748 PyErr_Format(PyExc_TypeError,
2749 "%.200s() takes no arguments (%d given)",
2750 PyString_AsString(co->co_name),
2751 argcount + kwcount);
2752 goto fail;
2753 }
2754 }
2755 /* Allocate and initialize storage for cell vars, and copy free
2756 vars into frame. This isn't too efficient right now. */
Richard Jonescebbefc2006-05-23 18:28:17 +00002757 if (PyTuple_GET_SIZE(co->co_cellvars)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002758 int i = 0, j = 0, nargs, found;
2759 char *cellname, *argname;
2760 PyObject *c;
2761
2762 nargs = co->co_argcount;
2763 if (co->co_flags & CO_VARARGS)
2764 nargs++;
2765 if (co->co_flags & CO_VARKEYWORDS)
2766 nargs++;
2767
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002768 /* Initialize each cell var, taking into account
2769 cell vars that are initialized from arguments.
2770
2771 Should arrange for the compiler to put cellvars
2772 that are arguments at the beginning of the cellvars
2773 list so that we can march over it more efficiently?
2774 */
Richard Jonescebbefc2006-05-23 18:28:17 +00002775 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002776 cellname = PyString_AS_STRING(
2777 PyTuple_GET_ITEM(co->co_cellvars, i));
2778 found = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002779 for (j = 0; j < nargs; j++) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002780 argname = PyString_AS_STRING(
2781 PyTuple_GET_ITEM(co->co_varnames, j));
2782 if (strcmp(cellname, argname) == 0) {
2783 c = PyCell_New(GETLOCAL(j));
2784 if (c == NULL)
2785 goto fail;
Richard Jonescebbefc2006-05-23 18:28:17 +00002786 GETLOCAL(co->co_nlocals + i) = c;
Tim Peters5ca576e2001-06-18 22:08:13 +00002787 found = 1;
2788 break;
2789 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002790 }
2791 if (found == 0) {
2792 c = PyCell_New(NULL);
2793 if (c == NULL)
2794 goto fail;
Richard Jonescebbefc2006-05-23 18:28:17 +00002795 SETLOCAL(co->co_nlocals + i, c);
Tim Peters5ca576e2001-06-18 22:08:13 +00002796 }
2797 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002798 }
Richard Jonescebbefc2006-05-23 18:28:17 +00002799 if (PyTuple_GET_SIZE(co->co_freevars)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002800 int i;
Richard Jonescebbefc2006-05-23 18:28:17 +00002801 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002802 PyObject *o = PyTuple_GET_ITEM(closure, i);
2803 Py_INCREF(o);
Richard Jonescebbefc2006-05-23 18:28:17 +00002804 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Tim Peters5ca576e2001-06-18 22:08:13 +00002805 }
2806 }
2807
Tim Peters5ca576e2001-06-18 22:08:13 +00002808 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002809 /* Don't need to keep the reference to f_back, it will be set
2810 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002811 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002812 f->f_back = NULL;
2813
Jeremy Hylton985eba52003-02-05 23:13:00 +00002814 PCALL(PCALL_GENERATOR);
2815
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002816 /* Create a new generator that owns the ready to run frame
2817 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00002818 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00002819 }
2820
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002821 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00002822
2823 fail: /* Jump here from prelude on failure */
2824
Tim Petersb13680b2001-11-27 23:29:29 +00002825 /* decref'ing the frame can cause __del__ methods to get invoked,
2826 which can call back into Python. While we're done with the
2827 current Python frame (f), the associated C stack is still in use,
2828 so recursion_depth must be boosted for the duration.
2829 */
2830 assert(tstate != NULL);
2831 ++tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002832 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00002833 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002834 return retval;
2835}
2836
2837
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002838/* Implementation notes for set_exc_info() and reset_exc_info():
2839
2840- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2841 'exc_traceback'. These always travel together.
2842
2843- tstate->curexc_ZZZ is the "hot" exception that is set by
2844 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2845
2846- Once an exception is caught by an except clause, it is transferred
2847 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2848 can pick it up. This is the primary task of set_exc_info().
2849
2850- Now let me explain the complicated dance with frame->f_exc_ZZZ.
2851
2852 Long ago, when none of this existed, there were just a few globals:
2853 one set corresponding to the "hot" exception, and one set
2854 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2855 globals; they were simply stored as sys.exc_ZZZ. For backwards
2856 compatibility, they still are!) The problem was that in code like
2857 this:
2858
2859 try:
2860 "something that may fail"
2861 except "some exception":
2862 "do something else first"
2863 "print the exception from sys.exc_ZZZ."
2864
2865 if "do something else first" invoked something that raised and caught
2866 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2867 cause of subtle bugs. I fixed this by changing the semantics as
2868 follows:
2869
2870 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2871 *in that frame*.
2872
2873 - But initially, and as long as no exception is caught in a given
2874 frame, sys.exc_ZZZ will hold the last exception caught in the
2875 previous frame (or the frame before that, etc.).
2876
2877 The first bullet fixed the bug in the above example. The second
2878 bullet was for backwards compatibility: it was (and is) common to
2879 have a function that is called when an exception is caught, and to
2880 have that function access the caught exception via sys.exc_ZZZ.
2881 (Example: traceback.print_exc()).
2882
2883 At the same time I fixed the problem that sys.exc_ZZZ weren't
2884 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2885 but that's really a separate improvement.
2886
2887 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2888 variables to what they were before the current frame was called. The
2889 set_exc_info() function saves them on the frame so that
2890 reset_exc_info() can restore them. The invariant is that
2891 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2892 exception (where "catching" an exception applies only to successful
2893 except clauses); and if the current frame ever caught an exception,
2894 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2895 at the start of the current frame.
2896
2897*/
2898
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00002899Py_LOCAL(void)
Guido van Rossumac7be682001-01-17 15:42:30 +00002900set_exc_info(PyThreadState *tstate,
2901 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002902{
2903 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002904 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00002905
Guido van Rossuma027efa1997-05-05 20:56:21 +00002906 frame = tstate->frame;
2907 if (frame->f_exc_type == NULL) {
2908 /* This frame didn't catch an exception before */
2909 /* Save previous exception of this thread in this frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002910 if (tstate->exc_type == NULL) {
2911 Py_INCREF(Py_None);
2912 tstate->exc_type = Py_None;
2913 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002914 tmp_value = frame->f_exc_value;
2915 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002916 Py_XINCREF(tstate->exc_type);
2917 Py_XINCREF(tstate->exc_value);
2918 Py_XINCREF(tstate->exc_traceback);
2919 frame->f_exc_type = tstate->exc_type;
2920 frame->f_exc_value = tstate->exc_value;
2921 frame->f_exc_traceback = tstate->exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002922 Py_XDECREF(tmp_value);
2923 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002924 }
2925 /* Set new exception for this thread */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002926 tmp_type = tstate->exc_type;
2927 tmp_value = tstate->exc_value;
2928 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002929 Py_XINCREF(type);
2930 Py_XINCREF(value);
2931 Py_XINCREF(tb);
2932 tstate->exc_type = type;
2933 tstate->exc_value = value;
2934 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002935 Py_XDECREF(tmp_type);
2936 Py_XDECREF(tmp_value);
2937 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002938 /* For b/w compatibility */
2939 PySys_SetObject("exc_type", type);
2940 PySys_SetObject("exc_value", value);
2941 PySys_SetObject("exc_traceback", tb);
2942}
2943
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00002944Py_LOCAL(void)
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002945reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002946{
2947 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002948 PyObject *tmp_type, *tmp_value, *tmp_tb;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002949 frame = tstate->frame;
2950 if (frame->f_exc_type != NULL) {
2951 /* This frame caught an exception */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002952 tmp_type = tstate->exc_type;
2953 tmp_value = tstate->exc_value;
2954 tmp_tb = tstate->exc_traceback;
Neal Norwitzb2550692006-05-09 05:38:56 +00002955 Py_INCREF(frame->f_exc_type);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002956 Py_XINCREF(frame->f_exc_value);
2957 Py_XINCREF(frame->f_exc_traceback);
2958 tstate->exc_type = frame->f_exc_type;
2959 tstate->exc_value = frame->f_exc_value;
2960 tstate->exc_traceback = frame->f_exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002961 Py_XDECREF(tmp_type);
2962 Py_XDECREF(tmp_value);
2963 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002964 /* For b/w compatibility */
2965 PySys_SetObject("exc_type", frame->f_exc_type);
2966 PySys_SetObject("exc_value", frame->f_exc_value);
2967 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
2968 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002969 tmp_type = frame->f_exc_type;
2970 tmp_value = frame->f_exc_value;
2971 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002972 frame->f_exc_type = NULL;
2973 frame->f_exc_value = NULL;
2974 frame->f_exc_traceback = NULL;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002975 Py_XDECREF(tmp_type);
2976 Py_XDECREF(tmp_value);
2977 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002978}
2979
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002980/* Logic for the raise statement (too complicated for inlining).
2981 This *consumes* a reference count to each of its arguments. */
Fredrik Lundh57640f52006-05-26 11:54:04 +00002982Py_LOCAL(enum why_code)
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002983do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002984{
Guido van Rossumd295f121998-04-09 21:39:57 +00002985 if (type == NULL) {
2986 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00002987 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumd295f121998-04-09 21:39:57 +00002988 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
2989 value = tstate->exc_value;
2990 tb = tstate->exc_traceback;
2991 Py_XINCREF(type);
2992 Py_XINCREF(value);
2993 Py_XINCREF(tb);
2994 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002995
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002996 /* We support the following forms of raise:
2997 raise <class>, <classinstance>
2998 raise <class>, <argument tuple>
2999 raise <class>, None
3000 raise <class>, <argument>
3001 raise <classinstance>, None
3002 raise <string>, <object>
3003 raise <string>, None
3004
3005 An omitted second argument is the same as None.
3006
3007 In addition, raise <tuple>, <anything> is the same as
3008 raising the tuple's first item (and it better have one!);
3009 this rule is applied recursively.
3010
3011 Finally, an optional third argument can be supplied, which
3012 gives the traceback to be substituted (useful when
3013 re-raising an exception after examining it). */
3014
3015 /* First, check the traceback argument, replacing None with
3016 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00003017 if (tb == Py_None) {
3018 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003019 tb = NULL;
3020 }
3021 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003022 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003023 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003024 goto raise_error;
3025 }
3026
3027 /* Next, replace a missing value with None */
3028 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003029 value = Py_None;
3030 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003031 }
3032
3033 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00003034 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
3035 PyObject *tmp = type;
3036 type = PyTuple_GET_ITEM(type, 0);
3037 Py_INCREF(type);
3038 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003039 }
3040
Brett Cannona7446e32006-02-27 23:39:10 +00003041 if (PyString_CheckExact(type)) {
Tim Petersafb2c802002-04-18 18:06:20 +00003042 /* Raising builtin string is deprecated but still allowed --
3043 * do nothing. Raising an instance of a new-style str
3044 * subclass is right out. */
Brett Cannonbf364092006-03-01 04:25:17 +00003045 if (PyErr_Warn(PyExc_DeprecationWarning,
Brett Cannona7446e32006-02-27 23:39:10 +00003046 "raising a string exception is deprecated"))
3047 goto raise_error;
3048 }
Brett Cannonbf364092006-03-01 04:25:17 +00003049 else if (PyExceptionClass_Check(type))
Barry Warsaw4249f541997-08-22 21:26:19 +00003050 PyErr_NormalizeException(&type, &value, &tb);
3051
Brett Cannonbf364092006-03-01 04:25:17 +00003052 else if (PyExceptionInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003053 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00003054 if (value != Py_None) {
3055 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003056 "instance exception may not have a separate value");
3057 goto raise_error;
3058 }
3059 else {
3060 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00003061 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003062 value = type;
Brett Cannonbf364092006-03-01 04:25:17 +00003063 type = PyExceptionInstance_Class(type);
Guido van Rossumb209a111997-04-29 18:18:01 +00003064 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003065 }
3066 }
3067 else {
3068 /* Not something you can raise. You get an exception
3069 anyway, just not what you specified :-) */
Jeremy Hylton960d9482001-04-27 02:25:33 +00003070 PyErr_Format(PyExc_TypeError,
Neal Norwitz37aa0662003-01-10 15:31:15 +00003071 "exceptions must be classes, instances, or "
3072 "strings (deprecated), not %s",
3073 type->ob_type->tp_name);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003074 goto raise_error;
3075 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003076 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003077 if (tb == NULL)
3078 return WHY_EXCEPTION;
3079 else
3080 return WHY_RERAISE;
3081 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00003082 Py_XDECREF(value);
3083 Py_XDECREF(type);
3084 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003085 return WHY_EXCEPTION;
3086}
3087
Tim Petersd6d010b2001-06-21 02:49:55 +00003088/* Iterate v argcnt times and store the results on the stack (via decreasing
3089 sp). Return 1 for success, 0 if error. */
3090
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00003091Py_LOCAL(int)
Tim Petersd6d010b2001-06-21 02:49:55 +00003092unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003093{
Tim Petersd6d010b2001-06-21 02:49:55 +00003094 int i = 0;
3095 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00003096 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00003097
Tim Petersd6d010b2001-06-21 02:49:55 +00003098 assert(v != NULL);
3099
3100 it = PyObject_GetIter(v);
3101 if (it == NULL)
3102 goto Error;
3103
3104 for (; i < argcnt; i++) {
3105 w = PyIter_Next(it);
3106 if (w == NULL) {
3107 /* Iterator done, via error or exhaustion. */
3108 if (!PyErr_Occurred()) {
3109 PyErr_Format(PyExc_ValueError,
3110 "need more than %d value%s to unpack",
3111 i, i == 1 ? "" : "s");
3112 }
3113 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003114 }
3115 *--sp = w;
3116 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003117
3118 /* We better have exhausted the iterator now. */
3119 w = PyIter_Next(it);
3120 if (w == NULL) {
3121 if (PyErr_Occurred())
3122 goto Error;
3123 Py_DECREF(it);
3124 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003125 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00003126 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00003127 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00003128 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00003129Error:
Barry Warsaw91010551997-08-25 22:30:51 +00003130 for (; i > 0; i--, sp++)
3131 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00003132 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00003133 return 0;
3134}
3135
3136
Guido van Rossum96a42c81992-01-12 02:29:51 +00003137#ifdef LLTRACE
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00003138Py_LOCAL(int)
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003139prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003140{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003141 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003142 if (PyObject_Print(v, stdout, 0) != 0)
3143 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003144 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003145 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003146}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003147#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003148
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00003149Py_LOCAL(void)
Fred Drake5755ce62001-06-27 19:19:46 +00003150call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003151{
Guido van Rossumb209a111997-04-29 18:18:01 +00003152 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003153 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003154 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003155 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003156 value = Py_None;
3157 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003158 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003159 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003160 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003161 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003162 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003163 }
Fred Drake5755ce62001-06-27 19:19:46 +00003164 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003165 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003166 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003167 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003168 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003169 Py_XDECREF(type);
3170 Py_XDECREF(value);
3171 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003172 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003173}
3174
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00003175Py_LOCAL(void)
Fred Drake4ec5d562001-10-04 19:26:43 +00003176call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003177 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003178{
3179 PyObject *type, *value, *traceback;
3180 int err;
3181 PyErr_Fetch(&type, &value, &traceback);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003182 err = call_trace(func, obj, frame, what, arg);
Fred Drake4ec5d562001-10-04 19:26:43 +00003183 if (err == 0)
3184 PyErr_Restore(type, value, traceback);
3185 else {
3186 Py_XDECREF(type);
3187 Py_XDECREF(value);
3188 Py_XDECREF(traceback);
3189 }
3190}
3191
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00003192Py_LOCAL(int)
Fred Drake5755ce62001-06-27 19:19:46 +00003193call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3194 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003195{
Fred Drake5755ce62001-06-27 19:19:46 +00003196 register PyThreadState *tstate = frame->f_tstate;
3197 int result;
3198 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003199 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003200 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003201 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003202 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003203 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3204 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003205 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003206 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003207}
3208
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003209PyObject *
3210_PyEval_CallTracing(PyObject *func, PyObject *args)
3211{
3212 PyFrameObject *frame = PyEval_GetFrame();
3213 PyThreadState *tstate = frame->f_tstate;
3214 int save_tracing = tstate->tracing;
3215 int save_use_tracing = tstate->use_tracing;
3216 PyObject *result;
3217
3218 tstate->tracing = 0;
3219 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3220 || (tstate->c_profilefunc != NULL));
3221 result = PyObject_Call(func, args, NULL);
3222 tstate->tracing = save_tracing;
3223 tstate->use_tracing = save_use_tracing;
3224 return result;
3225}
3226
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00003227Py_LOCAL(int)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003228maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003229 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3230 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003231{
Michael W. Hudson006c7522002-11-08 13:08:46 +00003232 int result = 0;
3233
Jeremy Hyltona4ebc132006-04-18 14:47:00 +00003234 /* If the last instruction executed isn't in the current
3235 instruction window, reset the window. If the last
3236 instruction happens to fall at the start of a line or if it
3237 represents a jump backwards, call the trace function.
3238 */
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003239 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Jeremy Hyltona4ebc132006-04-18 14:47:00 +00003240 int line;
3241 PyAddrPair bounds;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003242
Jeremy Hyltona4ebc132006-04-18 14:47:00 +00003243 line = PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3244 &bounds);
3245 if (line >= 0) {
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003246 frame->f_lineno = line;
Tim Peters8a5c3c72004-04-05 19:36:21 +00003247 result = call_trace(func, obj, frame,
Michael W. Hudson006c7522002-11-08 13:08:46 +00003248 PyTrace_LINE, Py_None);
Jeremy Hyltona4ebc132006-04-18 14:47:00 +00003249 }
3250 *instr_lb = bounds.ap_lower;
3251 *instr_ub = bounds.ap_upper;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003252 }
Armin Rigobf57a142004-03-22 19:24:58 +00003253 else if (frame->f_lasti <= *instr_prev) {
Jeremy Hyltona4ebc132006-04-18 14:47:00 +00003254 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
Armin Rigobf57a142004-03-22 19:24:58 +00003255 }
3256 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003257 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003258}
3259
Fred Drake5755ce62001-06-27 19:19:46 +00003260void
3261PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003262{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003263 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003264 PyObject *temp = tstate->c_profileobj;
3265 Py_XINCREF(arg);
3266 tstate->c_profilefunc = NULL;
3267 tstate->c_profileobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003268 /* Must make sure that tracing is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003269 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003270 Py_XDECREF(temp);
3271 tstate->c_profilefunc = func;
3272 tstate->c_profileobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003273 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003274 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003275}
3276
3277void
3278PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3279{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003280 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003281 PyObject *temp = tstate->c_traceobj;
3282 Py_XINCREF(arg);
3283 tstate->c_tracefunc = NULL;
3284 tstate->c_traceobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003285 /* Must make sure that profiling is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003286 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003287 Py_XDECREF(temp);
3288 tstate->c_tracefunc = func;
3289 tstate->c_traceobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003290 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003291 tstate->use_tracing = ((func != NULL)
3292 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003293}
3294
Guido van Rossumb209a111997-04-29 18:18:01 +00003295PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003296PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003297{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003298 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003299 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003300 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003301 else
3302 return current_frame->f_builtins;
3303}
3304
Guido van Rossumb209a111997-04-29 18:18:01 +00003305PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003306PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003307{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003308 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003309 if (current_frame == NULL)
3310 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003311 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003312 return current_frame->f_locals;
3313}
3314
Guido van Rossumb209a111997-04-29 18:18:01 +00003315PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003316PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003317{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003318 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003319 if (current_frame == NULL)
3320 return NULL;
3321 else
3322 return current_frame->f_globals;
3323}
3324
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003325PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003326PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003327{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003328 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003329 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003330}
3331
Guido van Rossum6135a871995-01-09 17:53:26 +00003332int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003333PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003334{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003335 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003336 return current_frame == NULL ? 0 : current_frame->f_restricted;
3337}
3338
Guido van Rossumbe270261997-05-22 22:26:18 +00003339int
Tim Peters5ba58662001-07-16 02:29:45 +00003340PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003341{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003342 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003343 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003344
3345 if (current_frame != NULL) {
3346 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003347 const int compilerflags = codeflags & PyCF_MASK;
3348 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003349 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003350 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003351 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003352#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003353 if (codeflags & CO_GENERATOR_ALLOWED) {
3354 result = 1;
3355 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3356 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003357#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003358 }
3359 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003360}
3361
3362int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003363Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003364{
Guido van Rossumb209a111997-04-29 18:18:01 +00003365 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003366 if (f == NULL)
3367 return 0;
3368 if (!PyFile_SoftSpace(f, 0))
3369 return 0;
3370 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003371}
3372
Guido van Rossum3f5da241990-12-20 15:06:42 +00003373
Guido van Rossum681d79a1995-07-18 14:51:37 +00003374/* External interface to call any callable object.
3375 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003376
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003377#undef PyEval_CallObject
3378/* for backward compatibility: export this interface */
3379
Guido van Rossumb209a111997-04-29 18:18:01 +00003380PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003381PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003382{
Guido van Rossumb209a111997-04-29 18:18:01 +00003383 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003384}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003385#define PyEval_CallObject(func,arg) \
3386 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003387
Guido van Rossumb209a111997-04-29 18:18:01 +00003388PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003389PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003390{
Jeremy Hylton52820442001-01-03 23:52:36 +00003391 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003392
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003393 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003394 arg = PyTuple_New(0);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003395 if (arg == NULL)
3396 return NULL;
3397 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003398 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003399 PyErr_SetString(PyExc_TypeError,
3400 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003401 return NULL;
3402 }
3403 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003404 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003405
Guido van Rossumb209a111997-04-29 18:18:01 +00003406 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003407 PyErr_SetString(PyExc_TypeError,
3408 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003409 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003410 return NULL;
3411 }
3412
Tim Peters6d6c1a32001-08-02 04:15:00 +00003413 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003414 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003415 return result;
3416}
3417
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003418const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003419PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003420{
3421 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003422 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003423 else if (PyFunction_Check(func))
3424 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3425 else if (PyCFunction_Check(func))
3426 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3427 else if (PyClass_Check(func))
3428 return PyString_AsString(((PyClassObject*)func)->cl_name);
3429 else if (PyInstance_Check(func)) {
3430 return PyString_AsString(
3431 ((PyInstanceObject*)func)->in_class->cl_name);
3432 } else {
3433 return func->ob_type->tp_name;
3434 }
3435}
3436
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003437const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003438PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003439{
3440 if (PyMethod_Check(func))
3441 return "()";
3442 else if (PyFunction_Check(func))
3443 return "()";
3444 else if (PyCFunction_Check(func))
3445 return "()";
3446 else if (PyClass_Check(func))
3447 return " constructor";
3448 else if (PyInstance_Check(func)) {
3449 return " instance";
3450 } else {
3451 return " object";
3452 }
3453}
3454
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00003455Py_LOCAL(void)
Jeremy Hylton192690e2002-08-16 18:36:11 +00003456err_args(PyObject *func, int flags, int nargs)
3457{
3458 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003459 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003460 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003461 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003462 nargs);
3463 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003464 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003465 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003466 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003467 nargs);
3468}
3469
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003470#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003471if (tstate->use_tracing && tstate->c_profilefunc) { \
3472 if (call_trace(tstate->c_profilefunc, \
3473 tstate->c_profileobj, \
3474 tstate->frame, PyTrace_C_CALL, \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003475 func)) { \
3476 x = NULL; \
3477 } \
3478 else { \
3479 x = call; \
3480 if (tstate->c_profilefunc != NULL) { \
3481 if (x == NULL) { \
3482 call_trace_protected(tstate->c_profilefunc, \
3483 tstate->c_profileobj, \
3484 tstate->frame, PyTrace_C_EXCEPTION, \
3485 func); \
3486 /* XXX should pass (type, value, tb) */ \
3487 } else { \
3488 if (call_trace(tstate->c_profilefunc, \
3489 tstate->c_profileobj, \
3490 tstate->frame, PyTrace_C_RETURN, \
3491 func)) { \
3492 Py_DECREF(x); \
3493 x = NULL; \
3494 } \
3495 } \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003496 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003497 } \
3498} else { \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003499 x = call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003500 }
3501
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00003502Py_LOCAL(PyObject *)
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003503call_function(PyObject ***pp_stack, int oparg
3504#ifdef WITH_TSC
3505 , uint64* pintr0, uint64* pintr1
3506#endif
3507 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003508{
3509 int na = oparg & 0xff;
3510 int nk = (oparg>>8) & 0xff;
3511 int n = na + 2 * nk;
3512 PyObject **pfunc = (*pp_stack) - n - 1;
3513 PyObject *func = *pfunc;
3514 PyObject *x, *w;
3515
Jeremy Hylton985eba52003-02-05 23:13:00 +00003516 /* Always dispatch PyCFunction first, because these are
3517 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003518 */
3519 if (PyCFunction_Check(func) && nk == 0) {
3520 int flags = PyCFunction_GET_FLAGS(func);
Nicholas Bastind858a772004-06-25 23:31:06 +00003521 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003522
3523 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003524 if (flags & (METH_NOARGS | METH_O)) {
3525 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3526 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003527 if (flags & METH_NOARGS && na == 0) {
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003528 C_TRACE(x, (*meth)(self,NULL));
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003529 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003530 else if (flags & METH_O && na == 1) {
3531 PyObject *arg = EXT_POP(*pp_stack);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003532 C_TRACE(x, (*meth)(self,arg));
Jeremy Hylton192690e2002-08-16 18:36:11 +00003533 Py_DECREF(arg);
3534 }
3535 else {
3536 err_args(func, flags, na);
3537 x = NULL;
3538 }
3539 }
3540 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003541 PyObject *callargs;
3542 callargs = load_args(pp_stack, na);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003543 READ_TIMESTAMP(*pintr0);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003544 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003545 READ_TIMESTAMP(*pintr1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003546 Py_XDECREF(callargs);
3547 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003548 } else {
3549 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3550 /* optimize access to bound methods */
3551 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003552 PCALL(PCALL_METHOD);
3553 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003554 Py_INCREF(self);
3555 func = PyMethod_GET_FUNCTION(func);
3556 Py_INCREF(func);
3557 Py_DECREF(*pfunc);
3558 *pfunc = self;
3559 na++;
3560 n++;
3561 } else
3562 Py_INCREF(func);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003563 READ_TIMESTAMP(*pintr0);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003564 if (PyFunction_Check(func))
3565 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003566 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003567 x = do_call(func, pp_stack, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003568 READ_TIMESTAMP(*pintr1);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003569 Py_DECREF(func);
3570 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003571
Armin Rigod34fa522006-03-28 19:10:40 +00003572 /* Clear the stack of the function object. Also removes
3573 the arguments in case they weren't consumed already
3574 (fast_function() and err_args() leave them on the stack).
Thomas Wouters7f597322006-03-01 05:32:33 +00003575 */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003576 while ((*pp_stack) > pfunc) {
3577 w = EXT_POP(*pp_stack);
3578 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003579 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003580 }
3581 return x;
3582}
3583
Jeremy Hylton192690e2002-08-16 18:36:11 +00003584/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003585 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003586 For the simplest case -- a function that takes only positional
3587 arguments and is called with only positional arguments -- it
3588 inlines the most primitive frame setup code from
3589 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3590 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003591*/
3592
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00003593Py_LOCAL(PyObject *)
Guido van Rossumac7be682001-01-17 15:42:30 +00003594fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003595{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003596 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003597 PyObject *globals = PyFunction_GET_GLOBALS(func);
3598 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3599 PyObject **d = NULL;
3600 int nd = 0;
3601
Jeremy Hylton985eba52003-02-05 23:13:00 +00003602 PCALL(PCALL_FUNCTION);
3603 PCALL(PCALL_FAST_FUNCTION);
Raymond Hettinger40174c32003-05-31 07:04:16 +00003604 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003605 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3606 PyFrameObject *f;
3607 PyObject *retval = NULL;
3608 PyThreadState *tstate = PyThreadState_GET();
3609 PyObject **fastlocals, **stack;
3610 int i;
3611
3612 PCALL(PCALL_FASTER_FUNCTION);
3613 assert(globals != NULL);
3614 /* XXX Perhaps we should create a specialized
3615 PyFrame_New() that doesn't take locals, but does
3616 take builtins without sanity checking them.
3617 */
3618 f = PyFrame_New(tstate, co, globals, NULL);
3619 if (f == NULL)
3620 return NULL;
3621
3622 fastlocals = f->f_localsplus;
3623 stack = (*pp_stack) - n;
3624
3625 for (i = 0; i < n; i++) {
3626 Py_INCREF(*stack);
3627 fastlocals[i] = *stack++;
3628 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003629 retval = PyEval_EvalFrameEx(f,0);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003630 assert(tstate != NULL);
3631 ++tstate->recursion_depth;
3632 Py_DECREF(f);
3633 --tstate->recursion_depth;
3634 return retval;
3635 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003636 if (argdefs != NULL) {
3637 d = &PyTuple_GET_ITEM(argdefs, 0);
3638 nd = ((PyTupleObject *)argdefs)->ob_size;
3639 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003640 return PyEval_EvalCodeEx(co, globals,
3641 (PyObject *)NULL, (*pp_stack)-n, na,
3642 (*pp_stack)-2*nk, nk, d, nd,
3643 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003644}
3645
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00003646Py_LOCAL(PyObject *)
Ka-Ping Yee20579702001-01-15 22:14:16 +00003647update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3648 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003649{
3650 PyObject *kwdict = NULL;
3651 if (orig_kwdict == NULL)
3652 kwdict = PyDict_New();
3653 else {
3654 kwdict = PyDict_Copy(orig_kwdict);
3655 Py_DECREF(orig_kwdict);
3656 }
3657 if (kwdict == NULL)
3658 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003659 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003660 int err;
3661 PyObject *value = EXT_POP(*pp_stack);
3662 PyObject *key = EXT_POP(*pp_stack);
3663 if (PyDict_GetItem(kwdict, key) != NULL) {
Guido van Rossumac7be682001-01-17 15:42:30 +00003664 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +00003665 "%.200s%s got multiple values "
Jeremy Hylton512a2372001-04-11 13:52:29 +00003666 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003667 PyEval_GetFuncName(func),
3668 PyEval_GetFuncDesc(func),
Jeremy Hylton512a2372001-04-11 13:52:29 +00003669 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003670 Py_DECREF(key);
3671 Py_DECREF(value);
3672 Py_DECREF(kwdict);
3673 return NULL;
3674 }
3675 err = PyDict_SetItem(kwdict, key, value);
3676 Py_DECREF(key);
3677 Py_DECREF(value);
3678 if (err) {
3679 Py_DECREF(kwdict);
3680 return NULL;
3681 }
3682 }
3683 return kwdict;
3684}
3685
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00003686Py_LOCAL(PyObject *)
Jeremy Hylton52820442001-01-03 23:52:36 +00003687update_star_args(int nstack, int nstar, PyObject *stararg,
3688 PyObject ***pp_stack)
3689{
3690 PyObject *callargs, *w;
3691
3692 callargs = PyTuple_New(nstack + nstar);
3693 if (callargs == NULL) {
3694 return NULL;
3695 }
3696 if (nstar) {
3697 int i;
3698 for (i = 0; i < nstar; i++) {
3699 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3700 Py_INCREF(a);
3701 PyTuple_SET_ITEM(callargs, nstack + i, a);
3702 }
3703 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003704 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003705 w = EXT_POP(*pp_stack);
3706 PyTuple_SET_ITEM(callargs, nstack, w);
3707 }
3708 return callargs;
3709}
3710
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00003711Py_LOCAL(PyObject *)
Jeremy Hylton52820442001-01-03 23:52:36 +00003712load_args(PyObject ***pp_stack, int na)
3713{
3714 PyObject *args = PyTuple_New(na);
3715 PyObject *w;
3716
3717 if (args == NULL)
3718 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003719 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003720 w = EXT_POP(*pp_stack);
3721 PyTuple_SET_ITEM(args, na, w);
3722 }
3723 return args;
3724}
3725
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00003726Py_LOCAL(PyObject *)
Jeremy Hylton52820442001-01-03 23:52:36 +00003727do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3728{
3729 PyObject *callargs = NULL;
3730 PyObject *kwdict = NULL;
3731 PyObject *result = NULL;
3732
3733 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003734 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003735 if (kwdict == NULL)
3736 goto call_fail;
3737 }
3738 callargs = load_args(pp_stack, na);
3739 if (callargs == NULL)
3740 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003741#ifdef CALL_PROFILE
3742 /* At this point, we have to look at the type of func to
3743 update the call stats properly. Do it here so as to avoid
3744 exposing the call stats machinery outside ceval.c
3745 */
3746 if (PyFunction_Check(func))
3747 PCALL(PCALL_FUNCTION);
3748 else if (PyMethod_Check(func))
3749 PCALL(PCALL_METHOD);
3750 else if (PyType_Check(func))
3751 PCALL(PCALL_TYPE);
3752 else
3753 PCALL(PCALL_OTHER);
3754#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003755 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003756 call_fail:
3757 Py_XDECREF(callargs);
3758 Py_XDECREF(kwdict);
3759 return result;
3760}
3761
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00003762Py_LOCAL(PyObject *)
Jeremy Hylton52820442001-01-03 23:52:36 +00003763ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3764{
3765 int nstar = 0;
3766 PyObject *callargs = NULL;
3767 PyObject *stararg = NULL;
3768 PyObject *kwdict = NULL;
3769 PyObject *result = NULL;
3770
3771 if (flags & CALL_FLAG_KW) {
3772 kwdict = EXT_POP(*pp_stack);
3773 if (!(kwdict && PyDict_Check(kwdict))) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003774 PyErr_Format(PyExc_TypeError,
Jeremy Hylton512a2372001-04-11 13:52:29 +00003775 "%s%s argument after ** "
3776 "must be a dictionary",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003777 PyEval_GetFuncName(func),
3778 PyEval_GetFuncDesc(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003779 goto ext_call_fail;
3780 }
3781 }
3782 if (flags & CALL_FLAG_VAR) {
3783 stararg = EXT_POP(*pp_stack);
3784 if (!PyTuple_Check(stararg)) {
3785 PyObject *t = NULL;
3786 t = PySequence_Tuple(stararg);
3787 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003788 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3789 PyErr_Format(PyExc_TypeError,
3790 "%s%s argument after * "
3791 "must be a sequence",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003792 PyEval_GetFuncName(func),
3793 PyEval_GetFuncDesc(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003794 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003795 goto ext_call_fail;
3796 }
3797 Py_DECREF(stararg);
3798 stararg = t;
3799 }
3800 nstar = PyTuple_GET_SIZE(stararg);
3801 }
3802 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003803 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003804 if (kwdict == NULL)
3805 goto ext_call_fail;
3806 }
3807 callargs = update_star_args(na, nstar, stararg, pp_stack);
3808 if (callargs == NULL)
3809 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003810#ifdef CALL_PROFILE
3811 /* At this point, we have to look at the type of func to
3812 update the call stats properly. Do it here so as to avoid
3813 exposing the call stats machinery outside ceval.c
3814 */
3815 if (PyFunction_Check(func))
3816 PCALL(PCALL_FUNCTION);
3817 else if (PyMethod_Check(func))
3818 PCALL(PCALL_METHOD);
3819 else if (PyType_Check(func))
3820 PCALL(PCALL_TYPE);
3821 else
3822 PCALL(PCALL_OTHER);
3823#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003824 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003825 ext_call_fail:
3826 Py_XDECREF(callargs);
3827 Py_XDECREF(kwdict);
3828 Py_XDECREF(stararg);
3829 return result;
3830}
3831
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003832/* Extract a slice index from a PyInt or PyLong or an object with the
3833 nb_index slot defined, and store in *pi.
3834 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
3835 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 +00003836 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00003837*/
Tim Petersb5196382001-12-16 19:44:20 +00003838/* Note: If v is NULL, return success without storing into *pi. This
3839 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3840 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00003841*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00003842int
Martin v. Löwis18e16552006-02-15 17:27:45 +00003843_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003844{
Tim Petersb5196382001-12-16 19:44:20 +00003845 if (v != NULL) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00003846 Py_ssize_t x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003847 if (PyInt_Check(v)) {
Neal Norwitz90768422006-03-23 05:48:09 +00003848 x = PyInt_AsSsize_t(v);
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003849 }
3850 else if (v->ob_type->tp_as_number &&
3851 PyType_HasFeature(v->ob_type, Py_TPFLAGS_HAVE_INDEX)
3852 && v->ob_type->tp_as_number->nb_index) {
3853 x = v->ob_type->tp_as_number->nb_index(v);
3854 if (x == -1 && PyErr_Occurred())
3855 return 0;
3856 }
3857 else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003858 PyErr_SetString(PyExc_TypeError,
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003859 "slice indices must be integers or "
3860 "None or have an __index__ method");
Guido van Rossum20c6add2000-05-08 14:06:50 +00003861 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003862 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003863 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003864 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00003865 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003866}
3867
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003868#undef ISINDEX
3869#define ISINDEX(x) ((x) == NULL || PyInt_Check(x) || PyLong_Check(x) || \
3870 ((x)->ob_type->tp_as_number && \
3871 PyType_HasFeature((x)->ob_type, Py_TPFLAGS_HAVE_INDEX) \
3872 && (x)->ob_type->tp_as_number->nb_index))
Guido van Rossum50d756e2001-08-18 17:43:36 +00003873
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00003874Py_LOCAL(PyObject *)
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003875apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003876{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003877 PyTypeObject *tp = u->ob_type;
3878 PySequenceMethods *sq = tp->tp_as_sequence;
3879
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003880 if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00003881 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003882 if (!_PyEval_SliceIndex(v, &ilow))
3883 return NULL;
3884 if (!_PyEval_SliceIndex(w, &ihigh))
3885 return NULL;
3886 return PySequence_GetSlice(u, ilow, ihigh);
3887 }
3888 else {
3889 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00003890 if (slice != NULL) {
3891 PyObject *res = PyObject_GetItem(u, slice);
3892 Py_DECREF(slice);
3893 return res;
3894 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00003895 else
3896 return NULL;
3897 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003898}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003899
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00003900Py_LOCAL(int)
Guido van Rossumac7be682001-01-17 15:42:30 +00003901assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
3902 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003903{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003904 PyTypeObject *tp = u->ob_type;
3905 PySequenceMethods *sq = tp->tp_as_sequence;
3906
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003907 if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00003908 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003909 if (!_PyEval_SliceIndex(v, &ilow))
3910 return -1;
3911 if (!_PyEval_SliceIndex(w, &ihigh))
3912 return -1;
3913 if (x == NULL)
3914 return PySequence_DelSlice(u, ilow, ihigh);
3915 else
3916 return PySequence_SetSlice(u, ilow, ihigh, x);
3917 }
3918 else {
3919 PyObject *slice = PySlice_New(v, w, NULL);
3920 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00003921 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003922 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00003923 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00003924 else
Guido van Rossum354797c2001-12-03 19:45:06 +00003925 res = PyObject_DelItem(u, slice);
3926 Py_DECREF(slice);
3927 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003928 }
3929 else
3930 return -1;
3931 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003932}
3933
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00003934Py_LOCAL(PyObject *)
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003935cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003936{
Guido van Rossumac7be682001-01-17 15:42:30 +00003937 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003938 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00003939 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003940 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003941 break;
3942 case PyCmp_IS_NOT:
3943 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003944 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003945 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003946 res = PySequence_Contains(w, v);
3947 if (res < 0)
3948 return NULL;
3949 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003950 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00003951 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003952 if (res < 0)
3953 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003954 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003955 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003956 case PyCmp_EXC_MATCH:
Barry Warsaw4249f541997-08-22 21:26:19 +00003957 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003958 break;
3959 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00003960 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003961 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003962 v = res ? Py_True : Py_False;
3963 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003964 return v;
3965}
3966
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00003967Py_LOCAL(PyObject *)
Thomas Wouters52152252000-08-17 22:55:00 +00003968import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003969{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003970 PyObject *x;
3971
3972 x = PyObject_GetAttr(v, name);
3973 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00003974 PyErr_Format(PyExc_ImportError,
3975 "cannot import name %.230s",
3976 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003977 }
Thomas Wouters52152252000-08-17 22:55:00 +00003978 return x;
3979}
Guido van Rossumac7be682001-01-17 15:42:30 +00003980
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00003981Py_LOCAL(int)
Thomas Wouters52152252000-08-17 22:55:00 +00003982import_all_from(PyObject *locals, PyObject *v)
3983{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003984 PyObject *all = PyObject_GetAttrString(v, "__all__");
3985 PyObject *dict, *name, *value;
3986 int skip_leading_underscores = 0;
3987 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00003988
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003989 if (all == NULL) {
3990 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3991 return -1; /* Unexpected error */
3992 PyErr_Clear();
3993 dict = PyObject_GetAttrString(v, "__dict__");
3994 if (dict == NULL) {
3995 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3996 return -1;
3997 PyErr_SetString(PyExc_ImportError,
3998 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00003999 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004000 }
4001 all = PyMapping_Keys(dict);
4002 Py_DECREF(dict);
4003 if (all == NULL)
4004 return -1;
4005 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004006 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004007
4008 for (pos = 0, err = 0; ; pos++) {
4009 name = PySequence_GetItem(all, pos);
4010 if (name == NULL) {
4011 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4012 err = -1;
4013 else
4014 PyErr_Clear();
4015 break;
4016 }
4017 if (skip_leading_underscores &&
4018 PyString_Check(name) &&
4019 PyString_AS_STRING(name)[0] == '_')
4020 {
4021 Py_DECREF(name);
4022 continue;
4023 }
4024 value = PyObject_GetAttr(v, name);
4025 if (value == NULL)
4026 err = -1;
4027 else
4028 err = PyDict_SetItem(locals, name, value);
4029 Py_DECREF(name);
4030 Py_XDECREF(value);
4031 if (err != 0)
4032 break;
4033 }
4034 Py_DECREF(all);
4035 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004036}
4037
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00004038Py_LOCAL(PyObject *)
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004039build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004040{
Guido van Rossum7851eea2001-09-12 19:19:18 +00004041 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004042
4043 if (PyDict_Check(methods))
4044 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00004045 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00004046 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004047 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4048 base = PyTuple_GET_ITEM(bases, 0);
4049 metaclass = PyObject_GetAttrString(base, "__class__");
4050 if (metaclass == NULL) {
4051 PyErr_Clear();
4052 metaclass = (PyObject *)base->ob_type;
4053 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00004054 }
4055 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004056 else {
4057 PyObject *g = PyEval_GetGlobals();
4058 if (g != NULL && PyDict_Check(g))
4059 metaclass = PyDict_GetItemString(g, "__metaclass__");
4060 if (metaclass == NULL)
4061 metaclass = (PyObject *) &PyClass_Type;
4062 Py_INCREF(metaclass);
4063 }
Georg Brandl684fd0c2006-05-25 19:15:31 +00004064 result = PyObject_CallFunctionObjArgs(metaclass, name, bases, methods, NULL);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004065 Py_DECREF(metaclass);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004066 if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
4067 /* A type error here likely means that the user passed
4068 in a base that was not a class (such the random module
4069 instead of the random.random type). Help them out with
Raymond Hettingercfc31922004-09-16 16:41:57 +00004070 by augmenting the error message with more information.*/
4071
4072 PyObject *ptype, *pvalue, *ptraceback;
4073
4074 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
4075 if (PyString_Check(pvalue)) {
4076 PyObject *newmsg;
4077 newmsg = PyString_FromFormat(
4078 "Error when calling the metaclass bases\n %s",
4079 PyString_AS_STRING(pvalue));
4080 if (newmsg != NULL) {
4081 Py_DECREF(pvalue);
4082 pvalue = newmsg;
4083 }
4084 }
4085 PyErr_Restore(ptype, pvalue, ptraceback);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004086 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004087 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00004088}
4089
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00004090Py_LOCAL(int)
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004091exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
4092 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004093{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004094 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00004095 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004096 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004097
Guido van Rossumb209a111997-04-29 18:18:01 +00004098 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
4099 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004100 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00004101 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004102 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00004103 locals = PyTuple_GetItem(prog, 2);
4104 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004105 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004106 if (globals == Py_None) {
4107 globals = PyEval_GetGlobals();
4108 if (locals == Py_None) {
4109 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00004110 plain = 1;
4111 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004112 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004113 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004114 locals = globals;
Guido van Rossumb209a111997-04-29 18:18:01 +00004115 if (!PyString_Check(prog) &&
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004116 !PyUnicode_Check(prog) &&
Guido van Rossumb209a111997-04-29 18:18:01 +00004117 !PyCode_Check(prog) &&
4118 !PyFile_Check(prog)) {
4119 PyErr_SetString(PyExc_TypeError,
Guido van Rossumac7be682001-01-17 15:42:30 +00004120 "exec: arg 1 must be a string, file, or code object");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004121 return -1;
4122 }
Fred Drake661ea262000-10-24 19:57:45 +00004123 if (!PyDict_Check(globals)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004124 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00004125 "exec: arg 2 must be a dictionary or None");
4126 return -1;
4127 }
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004128 if (!PyMapping_Check(locals)) {
Fred Drake661ea262000-10-24 19:57:45 +00004129 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004130 "exec: arg 3 must be a mapping or None");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004131 return -1;
4132 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004133 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00004134 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00004135 if (PyCode_Check(prog)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +00004136 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4137 PyErr_SetString(PyExc_TypeError,
4138 "code object passed to exec may not contain free variables");
4139 return -1;
4140 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004141 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004142 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004143 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004144 FILE *fp = PyFile_AsFile(prog);
4145 char *name = PyString_AsString(PyFile_Name(prog));
Tim Peters5ba58662001-07-16 02:29:45 +00004146 PyCompilerFlags cf;
4147 cf.cf_flags = 0;
4148 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004149 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004150 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004151 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004152 v = PyRun_File(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004153 locals);
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004154 }
4155 else {
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004156 PyObject *tmp = NULL;
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004157 char *str;
Tim Peters5ba58662001-07-16 02:29:45 +00004158 PyCompilerFlags cf;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004159 cf.cf_flags = 0;
4160#ifdef Py_USING_UNICODE
4161 if (PyUnicode_Check(prog)) {
4162 tmp = PyUnicode_AsUTF8String(prog);
4163 if (tmp == NULL)
4164 return -1;
4165 prog = tmp;
4166 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4167 }
4168#endif
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004169 if (PyString_AsStringAndSize(prog, &str, NULL))
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004170 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004171 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters8a5c3c72004-04-05 19:36:21 +00004172 v = PyRun_StringFlags(str, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004173 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004174 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004175 v = PyRun_String(str, Py_file_input, globals, locals);
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004176 Py_XDECREF(tmp);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004177 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004178 if (plain)
4179 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004180 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004181 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00004182 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004183 return 0;
4184}
Guido van Rossum24c13741995-02-14 09:42:43 +00004185
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00004186Py_LOCAL(void)
Paul Prescode68140d2000-08-30 20:25:01 +00004187format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4188{
4189 char *obj_str;
4190
4191 if (!obj)
4192 return;
4193
4194 obj_str = PyString_AsString(obj);
4195 if (!obj_str)
4196 return;
4197
4198 PyErr_Format(exc, format_str, obj_str);
4199}
Guido van Rossum950361c1997-01-24 13:49:28 +00004200
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00004201Py_LOCAL(PyObject *)
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004202string_concatenate(PyObject *v, PyObject *w,
4203 PyFrameObject *f, unsigned char *next_instr)
4204{
4205 /* This function implements 'variable += expr' when both arguments
4206 are strings. */
4207
4208 if (v->ob_refcnt == 2) {
4209 /* In the common case, there are 2 references to the value
4210 * stored in 'variable' when the += is performed: one on the
4211 * value stack (in 'v') and one still stored in the 'variable'.
4212 * We try to delete the variable now to reduce the refcnt to 1.
4213 */
4214 switch (*next_instr) {
4215 case STORE_FAST:
4216 {
4217 int oparg = PEEKARG();
4218 PyObject **fastlocals = f->f_localsplus;
4219 if (GETLOCAL(oparg) == v)
4220 SETLOCAL(oparg, NULL);
4221 break;
4222 }
4223 case STORE_DEREF:
4224 {
Richard Jonescebbefc2006-05-23 18:28:17 +00004225 PyObject **freevars = f->f_localsplus + f->f_code->co_nlocals;
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004226 PyObject *c = freevars[PEEKARG()];
4227 if (PyCell_GET(c) == v)
4228 PyCell_Set(c, NULL);
4229 break;
4230 }
4231 case STORE_NAME:
4232 {
4233 PyObject *names = f->f_code->co_names;
4234 PyObject *name = GETITEM(names, PEEKARG());
4235 PyObject *locals = f->f_locals;
4236 if (PyDict_CheckExact(locals) &&
4237 PyDict_GetItem(locals, name) == v) {
4238 if (PyDict_DelItem(locals, name) != 0) {
4239 PyErr_Clear();
4240 }
4241 }
4242 break;
4243 }
4244 }
4245 }
4246
Armin Rigo618fbf52004-08-07 20:58:32 +00004247 if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004248 /* Now we own the last reference to 'v', so we can resize it
4249 * in-place.
4250 */
Thomas Wouters79cdce32006-04-19 15:09:44 +00004251 Py_ssize_t v_len = PyString_GET_SIZE(v);
4252 Py_ssize_t w_len = PyString_GET_SIZE(w);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004253 if (_PyString_Resize(&v, v_len + w_len) != 0) {
4254 /* XXX if _PyString_Resize() fails, 'v' has been
4255 * deallocated so it cannot be put back into 'variable'.
4256 * The MemoryError is raised when there is no value in
4257 * 'variable', which might (very remotely) be a cause
4258 * of incompatibilities.
4259 */
4260 return NULL;
4261 }
4262 /* copy 'w' into the newly allocated area of 'v' */
4263 memcpy(PyString_AS_STRING(v) + v_len,
4264 PyString_AS_STRING(w), w_len);
4265 return v;
4266 }
4267 else {
4268 /* When in-place resizing is not an option. */
4269 PyString_Concat(&v, w);
4270 return v;
4271 }
4272}
4273
Guido van Rossum950361c1997-01-24 13:49:28 +00004274#ifdef DYNAMIC_EXECUTION_PROFILE
4275
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00004276Py_LOCAL(PyObject *)
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004277getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004278{
4279 int i;
4280 PyObject *l = PyList_New(256);
4281 if (l == NULL) return NULL;
4282 for (i = 0; i < 256; i++) {
4283 PyObject *x = PyInt_FromLong(a[i]);
4284 if (x == NULL) {
4285 Py_DECREF(l);
4286 return NULL;
4287 }
4288 PyList_SetItem(l, i, x);
4289 }
4290 for (i = 0; i < 256; i++)
4291 a[i] = 0;
4292 return l;
4293}
4294
4295PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004296_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004297{
4298#ifndef DXPAIRS
4299 return getarray(dxp);
4300#else
4301 int i;
4302 PyObject *l = PyList_New(257);
4303 if (l == NULL) return NULL;
4304 for (i = 0; i < 257; i++) {
4305 PyObject *x = getarray(dxpairs[i]);
4306 if (x == NULL) {
4307 Py_DECREF(l);
4308 return NULL;
4309 }
4310 PyList_SetItem(l, i, x);
4311 }
4312 return l;
4313#endif
4314}
4315
4316#endif