blob: c26fa4dc552d49cf021e38bd11987f9e45bfc1d9 [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
Tim Peters7df5e7f2006-05-26 23:14:37 +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
Tim Peters7df5e7f2006-05-26 23:14:37 +000052 /* The slightly peculiar way of writing the next lines is
Michael W. Hudson800ba232004-08-12 18:19:17 +000053 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
Tim Peters7df5e7f2006-05-26 23:14:37 +000065void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000066 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
Tim Peters7df5e7f2006-05-26 23:14:37 +0000570 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000571 (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 }
Tim Peters7df5e7f2006-05-26 23:14:37 +0000771
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 */
Tim Peters7df5e7f2006-05-26 23:14:37 +00002221
Guido van Rossumc2e20742006-02-27 22:32:47 +00002222 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
Tim Peters7df5e7f2006-05-26 23:14:37 +00002584 if (tstate->frame->f_exc_type != NULL)
2585 reset_exc_info(tstate);
2586 else {
2587 assert(tstate->frame->f_exc_value == NULL);
2588 assert(tstate->frame->f_exc_traceback == NULL);
2589 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00002590
Tim Peters5ca576e2001-06-18 22:08:13 +00002591 /* pop frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +00002592 exit_eval_frame:
2593 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002594 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002595
Guido van Rossum96a42c81992-01-12 02:29:51 +00002596 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002597}
2598
Guido van Rossumc2e20742006-02-27 22:32:47 +00002599/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002600 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00002601 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002602
Tim Peters6d6c1a32001-08-02 04:15:00 +00002603PyObject *
2604PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002605 PyObject **args, int argcount, PyObject **kws, int kwcount,
2606 PyObject **defs, int defcount, PyObject *closure)
2607{
2608 register PyFrameObject *f;
2609 register PyObject *retval = NULL;
2610 register PyObject **fastlocals, **freevars;
2611 PyThreadState *tstate = PyThreadState_GET();
2612 PyObject *x, *u;
2613
2614 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00002615 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002616 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002617 return NULL;
2618 }
2619
Jeremy Hylton985eba52003-02-05 23:13:00 +00002620 assert(globals != NULL);
2621 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002622 if (f == NULL)
2623 return NULL;
2624
2625 fastlocals = f->f_localsplus;
Richard Jonescebbefc2006-05-23 18:28:17 +00002626 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00002627
2628 if (co->co_argcount > 0 ||
2629 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2630 int i;
2631 int n = argcount;
2632 PyObject *kwdict = NULL;
2633 if (co->co_flags & CO_VARKEYWORDS) {
2634 kwdict = PyDict_New();
2635 if (kwdict == NULL)
2636 goto fail;
2637 i = co->co_argcount;
2638 if (co->co_flags & CO_VARARGS)
2639 i++;
2640 SETLOCAL(i, kwdict);
2641 }
2642 if (argcount > co->co_argcount) {
2643 if (!(co->co_flags & CO_VARARGS)) {
2644 PyErr_Format(PyExc_TypeError,
2645 "%.200s() takes %s %d "
2646 "%sargument%s (%d given)",
2647 PyString_AsString(co->co_name),
2648 defcount ? "at most" : "exactly",
2649 co->co_argcount,
2650 kwcount ? "non-keyword " : "",
2651 co->co_argcount == 1 ? "" : "s",
2652 argcount);
2653 goto fail;
2654 }
2655 n = co->co_argcount;
2656 }
2657 for (i = 0; i < n; i++) {
2658 x = args[i];
2659 Py_INCREF(x);
2660 SETLOCAL(i, x);
2661 }
2662 if (co->co_flags & CO_VARARGS) {
2663 u = PyTuple_New(argcount - n);
2664 if (u == NULL)
2665 goto fail;
2666 SETLOCAL(co->co_argcount, u);
2667 for (i = n; i < argcount; i++) {
2668 x = args[i];
2669 Py_INCREF(x);
2670 PyTuple_SET_ITEM(u, i-n, x);
2671 }
2672 }
2673 for (i = 0; i < kwcount; i++) {
2674 PyObject *keyword = kws[2*i];
2675 PyObject *value = kws[2*i + 1];
2676 int j;
2677 if (keyword == NULL || !PyString_Check(keyword)) {
2678 PyErr_Format(PyExc_TypeError,
2679 "%.200s() keywords must be strings",
2680 PyString_AsString(co->co_name));
2681 goto fail;
2682 }
2683 /* XXX slow -- speed up using dictionary? */
2684 for (j = 0; j < co->co_argcount; j++) {
2685 PyObject *nm = PyTuple_GET_ITEM(
2686 co->co_varnames, j);
2687 int cmp = PyObject_RichCompareBool(
2688 keyword, nm, Py_EQ);
2689 if (cmp > 0)
2690 break;
2691 else if (cmp < 0)
2692 goto fail;
2693 }
2694 /* Check errors from Compare */
2695 if (PyErr_Occurred())
2696 goto fail;
2697 if (j >= co->co_argcount) {
2698 if (kwdict == NULL) {
2699 PyErr_Format(PyExc_TypeError,
2700 "%.200s() got an unexpected "
2701 "keyword argument '%.400s'",
2702 PyString_AsString(co->co_name),
2703 PyString_AsString(keyword));
2704 goto fail;
2705 }
2706 PyDict_SetItem(kwdict, keyword, value);
2707 }
2708 else {
2709 if (GETLOCAL(j) != NULL) {
2710 PyErr_Format(PyExc_TypeError,
2711 "%.200s() got multiple "
2712 "values for keyword "
2713 "argument '%.400s'",
2714 PyString_AsString(co->co_name),
2715 PyString_AsString(keyword));
2716 goto fail;
2717 }
2718 Py_INCREF(value);
2719 SETLOCAL(j, value);
2720 }
2721 }
2722 if (argcount < co->co_argcount) {
2723 int m = co->co_argcount - defcount;
2724 for (i = argcount; i < m; i++) {
2725 if (GETLOCAL(i) == NULL) {
2726 PyErr_Format(PyExc_TypeError,
2727 "%.200s() takes %s %d "
2728 "%sargument%s (%d given)",
2729 PyString_AsString(co->co_name),
2730 ((co->co_flags & CO_VARARGS) ||
2731 defcount) ? "at least"
2732 : "exactly",
2733 m, kwcount ? "non-keyword " : "",
2734 m == 1 ? "" : "s", i);
2735 goto fail;
2736 }
2737 }
2738 if (n > m)
2739 i = n - m;
2740 else
2741 i = 0;
2742 for (; i < defcount; i++) {
2743 if (GETLOCAL(m+i) == NULL) {
2744 PyObject *def = defs[i];
2745 Py_INCREF(def);
2746 SETLOCAL(m+i, def);
2747 }
2748 }
2749 }
2750 }
2751 else {
2752 if (argcount > 0 || kwcount > 0) {
2753 PyErr_Format(PyExc_TypeError,
2754 "%.200s() takes no arguments (%d given)",
2755 PyString_AsString(co->co_name),
2756 argcount + kwcount);
2757 goto fail;
2758 }
2759 }
2760 /* Allocate and initialize storage for cell vars, and copy free
2761 vars into frame. This isn't too efficient right now. */
Richard Jonescebbefc2006-05-23 18:28:17 +00002762 if (PyTuple_GET_SIZE(co->co_cellvars)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002763 int i = 0, j = 0, nargs, found;
2764 char *cellname, *argname;
2765 PyObject *c;
2766
2767 nargs = co->co_argcount;
2768 if (co->co_flags & CO_VARARGS)
2769 nargs++;
2770 if (co->co_flags & CO_VARKEYWORDS)
2771 nargs++;
2772
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002773 /* Initialize each cell var, taking into account
2774 cell vars that are initialized from arguments.
2775
2776 Should arrange for the compiler to put cellvars
2777 that are arguments at the beginning of the cellvars
2778 list so that we can march over it more efficiently?
2779 */
Richard Jonescebbefc2006-05-23 18:28:17 +00002780 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002781 cellname = PyString_AS_STRING(
2782 PyTuple_GET_ITEM(co->co_cellvars, i));
2783 found = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002784 for (j = 0; j < nargs; j++) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002785 argname = PyString_AS_STRING(
2786 PyTuple_GET_ITEM(co->co_varnames, j));
2787 if (strcmp(cellname, argname) == 0) {
2788 c = PyCell_New(GETLOCAL(j));
2789 if (c == NULL)
2790 goto fail;
Richard Jonescebbefc2006-05-23 18:28:17 +00002791 GETLOCAL(co->co_nlocals + i) = c;
Tim Peters5ca576e2001-06-18 22:08:13 +00002792 found = 1;
2793 break;
2794 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002795 }
2796 if (found == 0) {
2797 c = PyCell_New(NULL);
2798 if (c == NULL)
2799 goto fail;
Richard Jonescebbefc2006-05-23 18:28:17 +00002800 SETLOCAL(co->co_nlocals + i, c);
Tim Peters5ca576e2001-06-18 22:08:13 +00002801 }
2802 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002803 }
Richard Jonescebbefc2006-05-23 18:28:17 +00002804 if (PyTuple_GET_SIZE(co->co_freevars)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002805 int i;
Richard Jonescebbefc2006-05-23 18:28:17 +00002806 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002807 PyObject *o = PyTuple_GET_ITEM(closure, i);
2808 Py_INCREF(o);
Richard Jonescebbefc2006-05-23 18:28:17 +00002809 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Tim Peters5ca576e2001-06-18 22:08:13 +00002810 }
2811 }
2812
Tim Peters5ca576e2001-06-18 22:08:13 +00002813 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002814 /* Don't need to keep the reference to f_back, it will be set
2815 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002816 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002817 f->f_back = NULL;
2818
Jeremy Hylton985eba52003-02-05 23:13:00 +00002819 PCALL(PCALL_GENERATOR);
2820
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002821 /* Create a new generator that owns the ready to run frame
2822 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00002823 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00002824 }
2825
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002826 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00002827
2828 fail: /* Jump here from prelude on failure */
2829
Tim Petersb13680b2001-11-27 23:29:29 +00002830 /* decref'ing the frame can cause __del__ methods to get invoked,
2831 which can call back into Python. While we're done with the
2832 current Python frame (f), the associated C stack is still in use,
2833 so recursion_depth must be boosted for the duration.
2834 */
2835 assert(tstate != NULL);
2836 ++tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002837 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00002838 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002839 return retval;
2840}
2841
2842
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002843/* Implementation notes for set_exc_info() and reset_exc_info():
2844
2845- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2846 'exc_traceback'. These always travel together.
2847
2848- tstate->curexc_ZZZ is the "hot" exception that is set by
2849 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2850
2851- Once an exception is caught by an except clause, it is transferred
2852 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2853 can pick it up. This is the primary task of set_exc_info().
Tim Peters7df5e7f2006-05-26 23:14:37 +00002854 XXX That can't be right: set_exc_info() doesn't look at tstate->curexc_ZZZ.
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002855
2856- Now let me explain the complicated dance with frame->f_exc_ZZZ.
2857
2858 Long ago, when none of this existed, there were just a few globals:
2859 one set corresponding to the "hot" exception, and one set
2860 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2861 globals; they were simply stored as sys.exc_ZZZ. For backwards
2862 compatibility, they still are!) The problem was that in code like
2863 this:
2864
2865 try:
2866 "something that may fail"
2867 except "some exception":
2868 "do something else first"
2869 "print the exception from sys.exc_ZZZ."
2870
2871 if "do something else first" invoked something that raised and caught
2872 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2873 cause of subtle bugs. I fixed this by changing the semantics as
2874 follows:
2875
2876 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2877 *in that frame*.
2878
2879 - But initially, and as long as no exception is caught in a given
2880 frame, sys.exc_ZZZ will hold the last exception caught in the
2881 previous frame (or the frame before that, etc.).
2882
2883 The first bullet fixed the bug in the above example. The second
2884 bullet was for backwards compatibility: it was (and is) common to
2885 have a function that is called when an exception is caught, and to
2886 have that function access the caught exception via sys.exc_ZZZ.
2887 (Example: traceback.print_exc()).
2888
2889 At the same time I fixed the problem that sys.exc_ZZZ weren't
2890 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2891 but that's really a separate improvement.
2892
2893 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2894 variables to what they were before the current frame was called. The
2895 set_exc_info() function saves them on the frame so that
2896 reset_exc_info() can restore them. The invariant is that
2897 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2898 exception (where "catching" an exception applies only to successful
2899 except clauses); and if the current frame ever caught an exception,
2900 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2901 at the start of the current frame.
2902
2903*/
2904
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00002905Py_LOCAL(void)
Guido van Rossumac7be682001-01-17 15:42:30 +00002906set_exc_info(PyThreadState *tstate,
2907 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002908{
Tim Peters7df5e7f2006-05-26 23:14:37 +00002909 PyFrameObject *frame = tstate->frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002910 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00002911
Tim Peters7df5e7f2006-05-26 23:14:37 +00002912 assert(type != NULL);
2913 assert(frame != NULL);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002914 if (frame->f_exc_type == NULL) {
Tim Peters7df5e7f2006-05-26 23:14:37 +00002915 assert(frame->f_exc_value == NULL);
2916 assert(frame->f_exc_traceback == NULL);
2917 /* This frame didn't catch an exception before. */
2918 /* Save previous exception of this thread in this frame. */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002919 if (tstate->exc_type == NULL) {
Tim Peters7df5e7f2006-05-26 23:14:37 +00002920 /* XXX Why is this set to Py_None? */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002921 Py_INCREF(Py_None);
2922 tstate->exc_type = Py_None;
2923 }
Tim Peters7df5e7f2006-05-26 23:14:37 +00002924 Py_INCREF(tstate->exc_type);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002925 Py_XINCREF(tstate->exc_value);
2926 Py_XINCREF(tstate->exc_traceback);
2927 frame->f_exc_type = tstate->exc_type;
2928 frame->f_exc_value = tstate->exc_value;
2929 frame->f_exc_traceback = tstate->exc_traceback;
2930 }
Tim Peters7df5e7f2006-05-26 23:14:37 +00002931 /* Set new exception for this thread. */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002932 tmp_type = tstate->exc_type;
2933 tmp_value = tstate->exc_value;
2934 tmp_tb = tstate->exc_traceback;
Tim Peters7df5e7f2006-05-26 23:14:37 +00002935 Py_INCREF(type);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002936 Py_XINCREF(value);
2937 Py_XINCREF(tb);
2938 tstate->exc_type = type;
2939 tstate->exc_value = value;
2940 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002941 Py_XDECREF(tmp_type);
2942 Py_XDECREF(tmp_value);
2943 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002944 /* For b/w compatibility */
2945 PySys_SetObject("exc_type", type);
2946 PySys_SetObject("exc_value", value);
2947 PySys_SetObject("exc_traceback", tb);
2948}
2949
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00002950Py_LOCAL(void)
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002951reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002952{
2953 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002954 PyObject *tmp_type, *tmp_value, *tmp_tb;
Tim Peters7df5e7f2006-05-26 23:14:37 +00002955
2956 /* It's a precondition that the thread state's frame caught an
2957 * exception -- verify in a debug build.
2958 */
2959 assert(tstate != NULL);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002960 frame = tstate->frame;
Tim Peters7df5e7f2006-05-26 23:14:37 +00002961 assert(frame != NULL);
2962 assert(frame->f_exc_type != NULL);
2963
2964 /* Copy the frame's exception info back to the thread state. */
2965 tmp_type = tstate->exc_type;
2966 tmp_value = tstate->exc_value;
2967 tmp_tb = tstate->exc_traceback;
2968 Py_INCREF(frame->f_exc_type);
2969 Py_XINCREF(frame->f_exc_value);
2970 Py_XINCREF(frame->f_exc_traceback);
2971 tstate->exc_type = frame->f_exc_type;
2972 tstate->exc_value = frame->f_exc_value;
2973 tstate->exc_traceback = frame->f_exc_traceback;
2974 Py_XDECREF(tmp_type);
2975 Py_XDECREF(tmp_value);
2976 Py_XDECREF(tmp_tb);
2977
2978 /* For b/w compatibility */
2979 PySys_SetObject("exc_type", frame->f_exc_type);
2980 PySys_SetObject("exc_value", frame->f_exc_value);
2981 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
2982
2983 /* Clear the frame's exception info. */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002984 tmp_type = frame->f_exc_type;
2985 tmp_value = frame->f_exc_value;
2986 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002987 frame->f_exc_type = NULL;
2988 frame->f_exc_value = NULL;
2989 frame->f_exc_traceback = NULL;
Tim Peters7df5e7f2006-05-26 23:14:37 +00002990 Py_DECREF(tmp_type);
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002991 Py_XDECREF(tmp_value);
2992 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002993}
2994
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002995/* Logic for the raise statement (too complicated for inlining).
2996 This *consumes* a reference count to each of its arguments. */
Fredrik Lundh57640f52006-05-26 11:54:04 +00002997Py_LOCAL(enum why_code)
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002998do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002999{
Guido van Rossumd295f121998-04-09 21:39:57 +00003000 if (type == NULL) {
3001 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003002 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumd295f121998-04-09 21:39:57 +00003003 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
3004 value = tstate->exc_value;
3005 tb = tstate->exc_traceback;
3006 Py_XINCREF(type);
3007 Py_XINCREF(value);
3008 Py_XINCREF(tb);
3009 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003010
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003011 /* We support the following forms of raise:
3012 raise <class>, <classinstance>
3013 raise <class>, <argument tuple>
3014 raise <class>, None
3015 raise <class>, <argument>
3016 raise <classinstance>, None
3017 raise <string>, <object>
3018 raise <string>, None
3019
3020 An omitted second argument is the same as None.
3021
3022 In addition, raise <tuple>, <anything> is the same as
3023 raising the tuple's first item (and it better have one!);
3024 this rule is applied recursively.
3025
3026 Finally, an optional third argument can be supplied, which
3027 gives the traceback to be substituted (useful when
3028 re-raising an exception after examining it). */
3029
3030 /* First, check the traceback argument, replacing None with
3031 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00003032 if (tb == Py_None) {
3033 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003034 tb = NULL;
3035 }
3036 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003037 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003038 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003039 goto raise_error;
3040 }
3041
3042 /* Next, replace a missing value with None */
3043 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003044 value = Py_None;
3045 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003046 }
3047
3048 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00003049 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
3050 PyObject *tmp = type;
3051 type = PyTuple_GET_ITEM(type, 0);
3052 Py_INCREF(type);
3053 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003054 }
3055
Brett Cannona7446e32006-02-27 23:39:10 +00003056 if (PyString_CheckExact(type)) {
Tim Petersafb2c802002-04-18 18:06:20 +00003057 /* Raising builtin string is deprecated but still allowed --
3058 * do nothing. Raising an instance of a new-style str
3059 * subclass is right out. */
Brett Cannonbf364092006-03-01 04:25:17 +00003060 if (PyErr_Warn(PyExc_DeprecationWarning,
Brett Cannona7446e32006-02-27 23:39:10 +00003061 "raising a string exception is deprecated"))
3062 goto raise_error;
3063 }
Brett Cannonbf364092006-03-01 04:25:17 +00003064 else if (PyExceptionClass_Check(type))
Barry Warsaw4249f541997-08-22 21:26:19 +00003065 PyErr_NormalizeException(&type, &value, &tb);
3066
Brett Cannonbf364092006-03-01 04:25:17 +00003067 else if (PyExceptionInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003068 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00003069 if (value != Py_None) {
3070 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003071 "instance exception may not have a separate value");
3072 goto raise_error;
3073 }
3074 else {
3075 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00003076 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003077 value = type;
Brett Cannonbf364092006-03-01 04:25:17 +00003078 type = PyExceptionInstance_Class(type);
Guido van Rossumb209a111997-04-29 18:18:01 +00003079 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003080 }
3081 }
3082 else {
3083 /* Not something you can raise. You get an exception
3084 anyway, just not what you specified :-) */
Jeremy Hylton960d9482001-04-27 02:25:33 +00003085 PyErr_Format(PyExc_TypeError,
Neal Norwitz37aa0662003-01-10 15:31:15 +00003086 "exceptions must be classes, instances, or "
3087 "strings (deprecated), not %s",
3088 type->ob_type->tp_name);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003089 goto raise_error;
3090 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003091 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003092 if (tb == NULL)
3093 return WHY_EXCEPTION;
3094 else
3095 return WHY_RERAISE;
3096 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00003097 Py_XDECREF(value);
3098 Py_XDECREF(type);
3099 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003100 return WHY_EXCEPTION;
3101}
3102
Tim Petersd6d010b2001-06-21 02:49:55 +00003103/* Iterate v argcnt times and store the results on the stack (via decreasing
3104 sp). Return 1 for success, 0 if error. */
3105
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00003106Py_LOCAL(int)
Tim Petersd6d010b2001-06-21 02:49:55 +00003107unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003108{
Tim Petersd6d010b2001-06-21 02:49:55 +00003109 int i = 0;
3110 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00003111 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00003112
Tim Petersd6d010b2001-06-21 02:49:55 +00003113 assert(v != NULL);
3114
3115 it = PyObject_GetIter(v);
3116 if (it == NULL)
3117 goto Error;
3118
3119 for (; i < argcnt; i++) {
3120 w = PyIter_Next(it);
3121 if (w == NULL) {
3122 /* Iterator done, via error or exhaustion. */
3123 if (!PyErr_Occurred()) {
3124 PyErr_Format(PyExc_ValueError,
3125 "need more than %d value%s to unpack",
3126 i, i == 1 ? "" : "s");
3127 }
3128 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003129 }
3130 *--sp = w;
3131 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003132
3133 /* We better have exhausted the iterator now. */
3134 w = PyIter_Next(it);
3135 if (w == NULL) {
3136 if (PyErr_Occurred())
3137 goto Error;
3138 Py_DECREF(it);
3139 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003140 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00003141 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00003142 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00003143 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00003144Error:
Barry Warsaw91010551997-08-25 22:30:51 +00003145 for (; i > 0; i--, sp++)
3146 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00003147 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00003148 return 0;
3149}
3150
3151
Guido van Rossum96a42c81992-01-12 02:29:51 +00003152#ifdef LLTRACE
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00003153Py_LOCAL(int)
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003154prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003155{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003156 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003157 if (PyObject_Print(v, stdout, 0) != 0)
3158 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003159 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003160 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003161}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003162#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003163
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00003164Py_LOCAL(void)
Fred Drake5755ce62001-06-27 19:19:46 +00003165call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003166{
Guido van Rossumb209a111997-04-29 18:18:01 +00003167 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003168 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003169 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003170 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003171 value = Py_None;
3172 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003173 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003174 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003175 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003176 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003177 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003178 }
Fred Drake5755ce62001-06-27 19:19:46 +00003179 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003180 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003181 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003182 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003183 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003184 Py_XDECREF(type);
3185 Py_XDECREF(value);
3186 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003187 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003188}
3189
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00003190Py_LOCAL(void)
Fred Drake4ec5d562001-10-04 19:26:43 +00003191call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003192 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003193{
3194 PyObject *type, *value, *traceback;
3195 int err;
3196 PyErr_Fetch(&type, &value, &traceback);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003197 err = call_trace(func, obj, frame, what, arg);
Fred Drake4ec5d562001-10-04 19:26:43 +00003198 if (err == 0)
3199 PyErr_Restore(type, value, traceback);
3200 else {
3201 Py_XDECREF(type);
3202 Py_XDECREF(value);
3203 Py_XDECREF(traceback);
3204 }
3205}
3206
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00003207Py_LOCAL(int)
Fred Drake5755ce62001-06-27 19:19:46 +00003208call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3209 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003210{
Fred Drake5755ce62001-06-27 19:19:46 +00003211 register PyThreadState *tstate = frame->f_tstate;
3212 int result;
3213 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003214 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003215 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003216 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003217 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003218 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3219 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003220 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003221 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003222}
3223
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003224PyObject *
3225_PyEval_CallTracing(PyObject *func, PyObject *args)
3226{
3227 PyFrameObject *frame = PyEval_GetFrame();
3228 PyThreadState *tstate = frame->f_tstate;
3229 int save_tracing = tstate->tracing;
3230 int save_use_tracing = tstate->use_tracing;
3231 PyObject *result;
3232
3233 tstate->tracing = 0;
3234 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3235 || (tstate->c_profilefunc != NULL));
3236 result = PyObject_Call(func, args, NULL);
3237 tstate->tracing = save_tracing;
3238 tstate->use_tracing = save_use_tracing;
3239 return result;
3240}
3241
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00003242Py_LOCAL(int)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003243maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003244 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3245 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003246{
Michael W. Hudson006c7522002-11-08 13:08:46 +00003247 int result = 0;
3248
Jeremy Hyltona4ebc132006-04-18 14:47:00 +00003249 /* If the last instruction executed isn't in the current
3250 instruction window, reset the window. If the last
3251 instruction happens to fall at the start of a line or if it
3252 represents a jump backwards, call the trace function.
3253 */
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003254 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Jeremy Hyltona4ebc132006-04-18 14:47:00 +00003255 int line;
3256 PyAddrPair bounds;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003257
Jeremy Hyltona4ebc132006-04-18 14:47:00 +00003258 line = PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3259 &bounds);
3260 if (line >= 0) {
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003261 frame->f_lineno = line;
Tim Peters8a5c3c72004-04-05 19:36:21 +00003262 result = call_trace(func, obj, frame,
Michael W. Hudson006c7522002-11-08 13:08:46 +00003263 PyTrace_LINE, Py_None);
Jeremy Hyltona4ebc132006-04-18 14:47:00 +00003264 }
3265 *instr_lb = bounds.ap_lower;
3266 *instr_ub = bounds.ap_upper;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003267 }
Armin Rigobf57a142004-03-22 19:24:58 +00003268 else if (frame->f_lasti <= *instr_prev) {
Jeremy Hyltona4ebc132006-04-18 14:47:00 +00003269 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
Armin Rigobf57a142004-03-22 19:24:58 +00003270 }
3271 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003272 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003273}
3274
Fred Drake5755ce62001-06-27 19:19:46 +00003275void
3276PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003277{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003278 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003279 PyObject *temp = tstate->c_profileobj;
3280 Py_XINCREF(arg);
3281 tstate->c_profilefunc = NULL;
3282 tstate->c_profileobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003283 /* Must make sure that tracing is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003284 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003285 Py_XDECREF(temp);
3286 tstate->c_profilefunc = func;
3287 tstate->c_profileobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003288 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003289 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003290}
3291
3292void
3293PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3294{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003295 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003296 PyObject *temp = tstate->c_traceobj;
3297 Py_XINCREF(arg);
3298 tstate->c_tracefunc = NULL;
3299 tstate->c_traceobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003300 /* Must make sure that profiling is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003301 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003302 Py_XDECREF(temp);
3303 tstate->c_tracefunc = func;
3304 tstate->c_traceobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003305 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003306 tstate->use_tracing = ((func != NULL)
3307 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003308}
3309
Guido van Rossumb209a111997-04-29 18:18:01 +00003310PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003311PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003312{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003313 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003314 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003315 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003316 else
3317 return current_frame->f_builtins;
3318}
3319
Guido van Rossumb209a111997-04-29 18:18:01 +00003320PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003321PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003322{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003323 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003324 if (current_frame == NULL)
3325 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003326 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003327 return current_frame->f_locals;
3328}
3329
Guido van Rossumb209a111997-04-29 18:18:01 +00003330PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003331PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003332{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003333 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003334 if (current_frame == NULL)
3335 return NULL;
3336 else
3337 return current_frame->f_globals;
3338}
3339
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003340PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003341PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003342{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003343 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003344 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003345}
3346
Guido van Rossum6135a871995-01-09 17:53:26 +00003347int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003348PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003349{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003350 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003351 return current_frame == NULL ? 0 : current_frame->f_restricted;
3352}
3353
Guido van Rossumbe270261997-05-22 22:26:18 +00003354int
Tim Peters5ba58662001-07-16 02:29:45 +00003355PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003356{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003357 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003358 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003359
3360 if (current_frame != NULL) {
3361 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003362 const int compilerflags = codeflags & PyCF_MASK;
3363 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003364 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003365 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003366 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003367#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003368 if (codeflags & CO_GENERATOR_ALLOWED) {
3369 result = 1;
3370 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3371 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003372#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003373 }
3374 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003375}
3376
3377int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003378Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003379{
Guido van Rossumb209a111997-04-29 18:18:01 +00003380 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003381 if (f == NULL)
3382 return 0;
3383 if (!PyFile_SoftSpace(f, 0))
3384 return 0;
3385 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003386}
3387
Guido van Rossum3f5da241990-12-20 15:06:42 +00003388
Guido van Rossum681d79a1995-07-18 14:51:37 +00003389/* External interface to call any callable object.
3390 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003391
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003392#undef PyEval_CallObject
3393/* for backward compatibility: export this interface */
3394
Guido van Rossumb209a111997-04-29 18:18:01 +00003395PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003396PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003397{
Guido van Rossumb209a111997-04-29 18:18:01 +00003398 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003399}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003400#define PyEval_CallObject(func,arg) \
3401 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003402
Guido van Rossumb209a111997-04-29 18:18:01 +00003403PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003404PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003405{
Jeremy Hylton52820442001-01-03 23:52:36 +00003406 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003407
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003408 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003409 arg = PyTuple_New(0);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003410 if (arg == NULL)
3411 return NULL;
3412 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003413 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003414 PyErr_SetString(PyExc_TypeError,
3415 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003416 return NULL;
3417 }
3418 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003419 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003420
Guido van Rossumb209a111997-04-29 18:18:01 +00003421 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003422 PyErr_SetString(PyExc_TypeError,
3423 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003424 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003425 return NULL;
3426 }
3427
Tim Peters6d6c1a32001-08-02 04:15:00 +00003428 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003429 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003430 return result;
3431}
3432
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003433const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003434PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003435{
3436 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003437 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003438 else if (PyFunction_Check(func))
3439 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3440 else if (PyCFunction_Check(func))
3441 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3442 else if (PyClass_Check(func))
3443 return PyString_AsString(((PyClassObject*)func)->cl_name);
3444 else if (PyInstance_Check(func)) {
3445 return PyString_AsString(
3446 ((PyInstanceObject*)func)->in_class->cl_name);
3447 } else {
3448 return func->ob_type->tp_name;
3449 }
3450}
3451
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003452const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003453PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003454{
3455 if (PyMethod_Check(func))
3456 return "()";
3457 else if (PyFunction_Check(func))
3458 return "()";
3459 else if (PyCFunction_Check(func))
3460 return "()";
3461 else if (PyClass_Check(func))
3462 return " constructor";
3463 else if (PyInstance_Check(func)) {
3464 return " instance";
3465 } else {
3466 return " object";
3467 }
3468}
3469
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00003470Py_LOCAL(void)
Jeremy Hylton192690e2002-08-16 18:36:11 +00003471err_args(PyObject *func, int flags, int nargs)
3472{
3473 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003474 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003475 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003476 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003477 nargs);
3478 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003479 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003480 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003481 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003482 nargs);
3483}
3484
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003485#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003486if (tstate->use_tracing && tstate->c_profilefunc) { \
3487 if (call_trace(tstate->c_profilefunc, \
3488 tstate->c_profileobj, \
3489 tstate->frame, PyTrace_C_CALL, \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003490 func)) { \
3491 x = NULL; \
3492 } \
3493 else { \
3494 x = call; \
3495 if (tstate->c_profilefunc != NULL) { \
3496 if (x == NULL) { \
3497 call_trace_protected(tstate->c_profilefunc, \
3498 tstate->c_profileobj, \
3499 tstate->frame, PyTrace_C_EXCEPTION, \
3500 func); \
3501 /* XXX should pass (type, value, tb) */ \
3502 } else { \
3503 if (call_trace(tstate->c_profilefunc, \
3504 tstate->c_profileobj, \
3505 tstate->frame, PyTrace_C_RETURN, \
3506 func)) { \
3507 Py_DECREF(x); \
3508 x = NULL; \
3509 } \
3510 } \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003511 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003512 } \
3513} else { \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003514 x = call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003515 }
3516
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00003517Py_LOCAL(PyObject *)
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003518call_function(PyObject ***pp_stack, int oparg
3519#ifdef WITH_TSC
3520 , uint64* pintr0, uint64* pintr1
3521#endif
3522 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003523{
3524 int na = oparg & 0xff;
3525 int nk = (oparg>>8) & 0xff;
3526 int n = na + 2 * nk;
3527 PyObject **pfunc = (*pp_stack) - n - 1;
3528 PyObject *func = *pfunc;
3529 PyObject *x, *w;
3530
Jeremy Hylton985eba52003-02-05 23:13:00 +00003531 /* Always dispatch PyCFunction first, because these are
3532 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003533 */
3534 if (PyCFunction_Check(func) && nk == 0) {
3535 int flags = PyCFunction_GET_FLAGS(func);
Nicholas Bastind858a772004-06-25 23:31:06 +00003536 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003537
3538 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003539 if (flags & (METH_NOARGS | METH_O)) {
3540 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3541 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003542 if (flags & METH_NOARGS && na == 0) {
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003543 C_TRACE(x, (*meth)(self,NULL));
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003544 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003545 else if (flags & METH_O && na == 1) {
3546 PyObject *arg = EXT_POP(*pp_stack);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003547 C_TRACE(x, (*meth)(self,arg));
Jeremy Hylton192690e2002-08-16 18:36:11 +00003548 Py_DECREF(arg);
3549 }
3550 else {
3551 err_args(func, flags, na);
3552 x = NULL;
3553 }
3554 }
3555 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003556 PyObject *callargs;
3557 callargs = load_args(pp_stack, na);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003558 READ_TIMESTAMP(*pintr0);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003559 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003560 READ_TIMESTAMP(*pintr1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003561 Py_XDECREF(callargs);
3562 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003563 } else {
3564 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3565 /* optimize access to bound methods */
3566 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003567 PCALL(PCALL_METHOD);
3568 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003569 Py_INCREF(self);
3570 func = PyMethod_GET_FUNCTION(func);
3571 Py_INCREF(func);
3572 Py_DECREF(*pfunc);
3573 *pfunc = self;
3574 na++;
3575 n++;
3576 } else
3577 Py_INCREF(func);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003578 READ_TIMESTAMP(*pintr0);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003579 if (PyFunction_Check(func))
3580 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003581 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003582 x = do_call(func, pp_stack, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003583 READ_TIMESTAMP(*pintr1);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003584 Py_DECREF(func);
3585 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003586
Armin Rigod34fa522006-03-28 19:10:40 +00003587 /* Clear the stack of the function object. Also removes
3588 the arguments in case they weren't consumed already
3589 (fast_function() and err_args() leave them on the stack).
Thomas Wouters7f597322006-03-01 05:32:33 +00003590 */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003591 while ((*pp_stack) > pfunc) {
3592 w = EXT_POP(*pp_stack);
3593 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003594 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003595 }
3596 return x;
3597}
3598
Jeremy Hylton192690e2002-08-16 18:36:11 +00003599/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003600 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003601 For the simplest case -- a function that takes only positional
3602 arguments and is called with only positional arguments -- it
3603 inlines the most primitive frame setup code from
3604 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3605 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003606*/
3607
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00003608Py_LOCAL(PyObject *)
Guido van Rossumac7be682001-01-17 15:42:30 +00003609fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003610{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003611 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003612 PyObject *globals = PyFunction_GET_GLOBALS(func);
3613 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3614 PyObject **d = NULL;
3615 int nd = 0;
3616
Jeremy Hylton985eba52003-02-05 23:13:00 +00003617 PCALL(PCALL_FUNCTION);
3618 PCALL(PCALL_FAST_FUNCTION);
Raymond Hettinger40174c32003-05-31 07:04:16 +00003619 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003620 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3621 PyFrameObject *f;
3622 PyObject *retval = NULL;
3623 PyThreadState *tstate = PyThreadState_GET();
3624 PyObject **fastlocals, **stack;
3625 int i;
3626
3627 PCALL(PCALL_FASTER_FUNCTION);
3628 assert(globals != NULL);
3629 /* XXX Perhaps we should create a specialized
3630 PyFrame_New() that doesn't take locals, but does
3631 take builtins without sanity checking them.
3632 */
3633 f = PyFrame_New(tstate, co, globals, NULL);
3634 if (f == NULL)
3635 return NULL;
3636
3637 fastlocals = f->f_localsplus;
3638 stack = (*pp_stack) - n;
3639
3640 for (i = 0; i < n; i++) {
3641 Py_INCREF(*stack);
3642 fastlocals[i] = *stack++;
3643 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003644 retval = PyEval_EvalFrameEx(f,0);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003645 assert(tstate != NULL);
3646 ++tstate->recursion_depth;
3647 Py_DECREF(f);
3648 --tstate->recursion_depth;
3649 return retval;
3650 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003651 if (argdefs != NULL) {
3652 d = &PyTuple_GET_ITEM(argdefs, 0);
3653 nd = ((PyTupleObject *)argdefs)->ob_size;
3654 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003655 return PyEval_EvalCodeEx(co, globals,
3656 (PyObject *)NULL, (*pp_stack)-n, na,
3657 (*pp_stack)-2*nk, nk, d, nd,
3658 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003659}
3660
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00003661Py_LOCAL(PyObject *)
Ka-Ping Yee20579702001-01-15 22:14:16 +00003662update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3663 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003664{
3665 PyObject *kwdict = NULL;
3666 if (orig_kwdict == NULL)
3667 kwdict = PyDict_New();
3668 else {
3669 kwdict = PyDict_Copy(orig_kwdict);
3670 Py_DECREF(orig_kwdict);
3671 }
3672 if (kwdict == NULL)
3673 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003674 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003675 int err;
3676 PyObject *value = EXT_POP(*pp_stack);
3677 PyObject *key = EXT_POP(*pp_stack);
3678 if (PyDict_GetItem(kwdict, key) != NULL) {
Guido van Rossumac7be682001-01-17 15:42:30 +00003679 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +00003680 "%.200s%s got multiple values "
Jeremy Hylton512a2372001-04-11 13:52:29 +00003681 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003682 PyEval_GetFuncName(func),
3683 PyEval_GetFuncDesc(func),
Jeremy Hylton512a2372001-04-11 13:52:29 +00003684 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003685 Py_DECREF(key);
3686 Py_DECREF(value);
3687 Py_DECREF(kwdict);
3688 return NULL;
3689 }
3690 err = PyDict_SetItem(kwdict, key, value);
3691 Py_DECREF(key);
3692 Py_DECREF(value);
3693 if (err) {
3694 Py_DECREF(kwdict);
3695 return NULL;
3696 }
3697 }
3698 return kwdict;
3699}
3700
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00003701Py_LOCAL(PyObject *)
Jeremy Hylton52820442001-01-03 23:52:36 +00003702update_star_args(int nstack, int nstar, PyObject *stararg,
3703 PyObject ***pp_stack)
3704{
3705 PyObject *callargs, *w;
3706
3707 callargs = PyTuple_New(nstack + nstar);
3708 if (callargs == NULL) {
3709 return NULL;
3710 }
3711 if (nstar) {
3712 int i;
3713 for (i = 0; i < nstar; i++) {
3714 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3715 Py_INCREF(a);
3716 PyTuple_SET_ITEM(callargs, nstack + i, a);
3717 }
3718 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003719 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003720 w = EXT_POP(*pp_stack);
3721 PyTuple_SET_ITEM(callargs, nstack, w);
3722 }
3723 return callargs;
3724}
3725
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00003726Py_LOCAL(PyObject *)
Jeremy Hylton52820442001-01-03 23:52:36 +00003727load_args(PyObject ***pp_stack, int na)
3728{
3729 PyObject *args = PyTuple_New(na);
3730 PyObject *w;
3731
3732 if (args == NULL)
3733 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003734 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003735 w = EXT_POP(*pp_stack);
3736 PyTuple_SET_ITEM(args, na, w);
3737 }
3738 return args;
3739}
3740
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00003741Py_LOCAL(PyObject *)
Jeremy Hylton52820442001-01-03 23:52:36 +00003742do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3743{
3744 PyObject *callargs = NULL;
3745 PyObject *kwdict = NULL;
3746 PyObject *result = NULL;
3747
3748 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003749 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003750 if (kwdict == NULL)
3751 goto call_fail;
3752 }
3753 callargs = load_args(pp_stack, na);
3754 if (callargs == NULL)
3755 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003756#ifdef CALL_PROFILE
3757 /* At this point, we have to look at the type of func to
3758 update the call stats properly. Do it here so as to avoid
3759 exposing the call stats machinery outside ceval.c
3760 */
3761 if (PyFunction_Check(func))
3762 PCALL(PCALL_FUNCTION);
3763 else if (PyMethod_Check(func))
3764 PCALL(PCALL_METHOD);
3765 else if (PyType_Check(func))
3766 PCALL(PCALL_TYPE);
3767 else
3768 PCALL(PCALL_OTHER);
3769#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003770 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003771 call_fail:
3772 Py_XDECREF(callargs);
3773 Py_XDECREF(kwdict);
3774 return result;
3775}
3776
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00003777Py_LOCAL(PyObject *)
Jeremy Hylton52820442001-01-03 23:52:36 +00003778ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3779{
3780 int nstar = 0;
3781 PyObject *callargs = NULL;
3782 PyObject *stararg = NULL;
3783 PyObject *kwdict = NULL;
3784 PyObject *result = NULL;
3785
3786 if (flags & CALL_FLAG_KW) {
3787 kwdict = EXT_POP(*pp_stack);
3788 if (!(kwdict && PyDict_Check(kwdict))) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003789 PyErr_Format(PyExc_TypeError,
Jeremy Hylton512a2372001-04-11 13:52:29 +00003790 "%s%s argument after ** "
3791 "must be a dictionary",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003792 PyEval_GetFuncName(func),
3793 PyEval_GetFuncDesc(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003794 goto ext_call_fail;
3795 }
3796 }
3797 if (flags & CALL_FLAG_VAR) {
3798 stararg = EXT_POP(*pp_stack);
3799 if (!PyTuple_Check(stararg)) {
3800 PyObject *t = NULL;
3801 t = PySequence_Tuple(stararg);
3802 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003803 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3804 PyErr_Format(PyExc_TypeError,
3805 "%s%s argument after * "
3806 "must be a sequence",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003807 PyEval_GetFuncName(func),
3808 PyEval_GetFuncDesc(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003809 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003810 goto ext_call_fail;
3811 }
3812 Py_DECREF(stararg);
3813 stararg = t;
3814 }
3815 nstar = PyTuple_GET_SIZE(stararg);
3816 }
3817 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003818 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003819 if (kwdict == NULL)
3820 goto ext_call_fail;
3821 }
3822 callargs = update_star_args(na, nstar, stararg, pp_stack);
3823 if (callargs == NULL)
3824 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003825#ifdef CALL_PROFILE
3826 /* At this point, we have to look at the type of func to
3827 update the call stats properly. Do it here so as to avoid
3828 exposing the call stats machinery outside ceval.c
3829 */
3830 if (PyFunction_Check(func))
3831 PCALL(PCALL_FUNCTION);
3832 else if (PyMethod_Check(func))
3833 PCALL(PCALL_METHOD);
3834 else if (PyType_Check(func))
3835 PCALL(PCALL_TYPE);
3836 else
3837 PCALL(PCALL_OTHER);
3838#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003839 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003840 ext_call_fail:
3841 Py_XDECREF(callargs);
3842 Py_XDECREF(kwdict);
3843 Py_XDECREF(stararg);
3844 return result;
3845}
3846
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003847/* Extract a slice index from a PyInt or PyLong or an object with the
3848 nb_index slot defined, and store in *pi.
3849 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
3850 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 +00003851 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00003852*/
Tim Petersb5196382001-12-16 19:44:20 +00003853/* Note: If v is NULL, return success without storing into *pi. This
3854 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3855 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00003856*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00003857int
Martin v. Löwis18e16552006-02-15 17:27:45 +00003858_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003859{
Tim Petersb5196382001-12-16 19:44:20 +00003860 if (v != NULL) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00003861 Py_ssize_t x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003862 if (PyInt_Check(v)) {
Neal Norwitz90768422006-03-23 05:48:09 +00003863 x = PyInt_AsSsize_t(v);
Tim Peters7df5e7f2006-05-26 23:14:37 +00003864 }
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003865 else if (v->ob_type->tp_as_number &&
3866 PyType_HasFeature(v->ob_type, Py_TPFLAGS_HAVE_INDEX)
3867 && v->ob_type->tp_as_number->nb_index) {
3868 x = v->ob_type->tp_as_number->nb_index(v);
3869 if (x == -1 && PyErr_Occurred())
3870 return 0;
3871 }
3872 else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003873 PyErr_SetString(PyExc_TypeError,
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003874 "slice indices must be integers or "
3875 "None or have an __index__ method");
Guido van Rossum20c6add2000-05-08 14:06:50 +00003876 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003877 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003878 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003879 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00003880 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003881}
3882
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003883#undef ISINDEX
3884#define ISINDEX(x) ((x) == NULL || PyInt_Check(x) || PyLong_Check(x) || \
3885 ((x)->ob_type->tp_as_number && \
3886 PyType_HasFeature((x)->ob_type, Py_TPFLAGS_HAVE_INDEX) \
3887 && (x)->ob_type->tp_as_number->nb_index))
Guido van Rossum50d756e2001-08-18 17:43:36 +00003888
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00003889Py_LOCAL(PyObject *)
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003890apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003891{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003892 PyTypeObject *tp = u->ob_type;
3893 PySequenceMethods *sq = tp->tp_as_sequence;
3894
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003895 if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00003896 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003897 if (!_PyEval_SliceIndex(v, &ilow))
3898 return NULL;
3899 if (!_PyEval_SliceIndex(w, &ihigh))
3900 return NULL;
3901 return PySequence_GetSlice(u, ilow, ihigh);
3902 }
3903 else {
3904 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00003905 if (slice != NULL) {
3906 PyObject *res = PyObject_GetItem(u, slice);
3907 Py_DECREF(slice);
3908 return res;
3909 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00003910 else
3911 return NULL;
3912 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003913}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003914
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00003915Py_LOCAL(int)
Guido van Rossumac7be682001-01-17 15:42:30 +00003916assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
3917 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003918{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003919 PyTypeObject *tp = u->ob_type;
3920 PySequenceMethods *sq = tp->tp_as_sequence;
3921
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003922 if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00003923 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003924 if (!_PyEval_SliceIndex(v, &ilow))
3925 return -1;
3926 if (!_PyEval_SliceIndex(w, &ihigh))
3927 return -1;
3928 if (x == NULL)
3929 return PySequence_DelSlice(u, ilow, ihigh);
3930 else
3931 return PySequence_SetSlice(u, ilow, ihigh, x);
3932 }
3933 else {
3934 PyObject *slice = PySlice_New(v, w, NULL);
3935 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00003936 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003937 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00003938 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00003939 else
Guido van Rossum354797c2001-12-03 19:45:06 +00003940 res = PyObject_DelItem(u, slice);
3941 Py_DECREF(slice);
3942 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003943 }
3944 else
3945 return -1;
3946 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003947}
3948
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00003949Py_LOCAL(PyObject *)
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003950cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003951{
Guido van Rossumac7be682001-01-17 15:42:30 +00003952 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003953 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00003954 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003955 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003956 break;
3957 case PyCmp_IS_NOT:
3958 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003959 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003960 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003961 res = PySequence_Contains(w, v);
3962 if (res < 0)
3963 return NULL;
3964 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003965 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00003966 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003967 if (res < 0)
3968 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003969 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003970 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003971 case PyCmp_EXC_MATCH:
Barry Warsaw4249f541997-08-22 21:26:19 +00003972 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003973 break;
3974 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00003975 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003976 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003977 v = res ? Py_True : Py_False;
3978 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003979 return v;
3980}
3981
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00003982Py_LOCAL(PyObject *)
Thomas Wouters52152252000-08-17 22:55:00 +00003983import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003984{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003985 PyObject *x;
3986
3987 x = PyObject_GetAttr(v, name);
3988 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00003989 PyErr_Format(PyExc_ImportError,
3990 "cannot import name %.230s",
3991 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003992 }
Thomas Wouters52152252000-08-17 22:55:00 +00003993 return x;
3994}
Guido van Rossumac7be682001-01-17 15:42:30 +00003995
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00003996Py_LOCAL(int)
Thomas Wouters52152252000-08-17 22:55:00 +00003997import_all_from(PyObject *locals, PyObject *v)
3998{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003999 PyObject *all = PyObject_GetAttrString(v, "__all__");
4000 PyObject *dict, *name, *value;
4001 int skip_leading_underscores = 0;
4002 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004003
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004004 if (all == NULL) {
4005 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4006 return -1; /* Unexpected error */
4007 PyErr_Clear();
4008 dict = PyObject_GetAttrString(v, "__dict__");
4009 if (dict == NULL) {
4010 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4011 return -1;
4012 PyErr_SetString(PyExc_ImportError,
4013 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00004014 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004015 }
4016 all = PyMapping_Keys(dict);
4017 Py_DECREF(dict);
4018 if (all == NULL)
4019 return -1;
4020 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004021 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004022
4023 for (pos = 0, err = 0; ; pos++) {
4024 name = PySequence_GetItem(all, pos);
4025 if (name == NULL) {
4026 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4027 err = -1;
4028 else
4029 PyErr_Clear();
4030 break;
4031 }
4032 if (skip_leading_underscores &&
4033 PyString_Check(name) &&
4034 PyString_AS_STRING(name)[0] == '_')
4035 {
4036 Py_DECREF(name);
4037 continue;
4038 }
4039 value = PyObject_GetAttr(v, name);
4040 if (value == NULL)
4041 err = -1;
4042 else
4043 err = PyDict_SetItem(locals, name, value);
4044 Py_DECREF(name);
4045 Py_XDECREF(value);
4046 if (err != 0)
4047 break;
4048 }
4049 Py_DECREF(all);
4050 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004051}
4052
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00004053Py_LOCAL(PyObject *)
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004054build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004055{
Guido van Rossum7851eea2001-09-12 19:19:18 +00004056 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004057
4058 if (PyDict_Check(methods))
4059 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00004060 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00004061 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004062 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4063 base = PyTuple_GET_ITEM(bases, 0);
4064 metaclass = PyObject_GetAttrString(base, "__class__");
4065 if (metaclass == NULL) {
4066 PyErr_Clear();
4067 metaclass = (PyObject *)base->ob_type;
4068 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00004069 }
4070 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004071 else {
4072 PyObject *g = PyEval_GetGlobals();
4073 if (g != NULL && PyDict_Check(g))
4074 metaclass = PyDict_GetItemString(g, "__metaclass__");
4075 if (metaclass == NULL)
4076 metaclass = (PyObject *) &PyClass_Type;
4077 Py_INCREF(metaclass);
4078 }
Georg Brandl684fd0c2006-05-25 19:15:31 +00004079 result = PyObject_CallFunctionObjArgs(metaclass, name, bases, methods, NULL);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004080 Py_DECREF(metaclass);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004081 if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
Tim Peters7df5e7f2006-05-26 23:14:37 +00004082 /* A type error here likely means that the user passed
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004083 in a base that was not a class (such the random module
4084 instead of the random.random type). Help them out with
Raymond Hettingercfc31922004-09-16 16:41:57 +00004085 by augmenting the error message with more information.*/
4086
4087 PyObject *ptype, *pvalue, *ptraceback;
4088
4089 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
4090 if (PyString_Check(pvalue)) {
4091 PyObject *newmsg;
4092 newmsg = PyString_FromFormat(
4093 "Error when calling the metaclass bases\n %s",
4094 PyString_AS_STRING(pvalue));
4095 if (newmsg != NULL) {
4096 Py_DECREF(pvalue);
4097 pvalue = newmsg;
4098 }
4099 }
4100 PyErr_Restore(ptype, pvalue, ptraceback);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004101 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004102 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00004103}
4104
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00004105Py_LOCAL(int)
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004106exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
4107 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004108{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004109 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00004110 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004111 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004112
Guido van Rossumb209a111997-04-29 18:18:01 +00004113 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
4114 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004115 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00004116 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004117 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00004118 locals = PyTuple_GetItem(prog, 2);
4119 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004120 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004121 if (globals == Py_None) {
4122 globals = PyEval_GetGlobals();
4123 if (locals == Py_None) {
4124 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00004125 plain = 1;
4126 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004127 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004128 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004129 locals = globals;
Guido van Rossumb209a111997-04-29 18:18:01 +00004130 if (!PyString_Check(prog) &&
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004131 !PyUnicode_Check(prog) &&
Guido van Rossumb209a111997-04-29 18:18:01 +00004132 !PyCode_Check(prog) &&
4133 !PyFile_Check(prog)) {
4134 PyErr_SetString(PyExc_TypeError,
Guido van Rossumac7be682001-01-17 15:42:30 +00004135 "exec: arg 1 must be a string, file, or code object");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004136 return -1;
4137 }
Fred Drake661ea262000-10-24 19:57:45 +00004138 if (!PyDict_Check(globals)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004139 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00004140 "exec: arg 2 must be a dictionary or None");
4141 return -1;
4142 }
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004143 if (!PyMapping_Check(locals)) {
Fred Drake661ea262000-10-24 19:57:45 +00004144 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004145 "exec: arg 3 must be a mapping or None");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004146 return -1;
4147 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004148 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00004149 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00004150 if (PyCode_Check(prog)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +00004151 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4152 PyErr_SetString(PyExc_TypeError,
4153 "code object passed to exec may not contain free variables");
4154 return -1;
4155 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004156 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004157 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004158 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004159 FILE *fp = PyFile_AsFile(prog);
4160 char *name = PyString_AsString(PyFile_Name(prog));
Tim Peters5ba58662001-07-16 02:29:45 +00004161 PyCompilerFlags cf;
4162 cf.cf_flags = 0;
4163 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004164 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004165 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004166 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004167 v = PyRun_File(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004168 locals);
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004169 }
4170 else {
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004171 PyObject *tmp = NULL;
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004172 char *str;
Tim Peters5ba58662001-07-16 02:29:45 +00004173 PyCompilerFlags cf;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004174 cf.cf_flags = 0;
4175#ifdef Py_USING_UNICODE
4176 if (PyUnicode_Check(prog)) {
4177 tmp = PyUnicode_AsUTF8String(prog);
4178 if (tmp == NULL)
4179 return -1;
4180 prog = tmp;
4181 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4182 }
4183#endif
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004184 if (PyString_AsStringAndSize(prog, &str, NULL))
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004185 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004186 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters8a5c3c72004-04-05 19:36:21 +00004187 v = PyRun_StringFlags(str, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004188 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004189 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004190 v = PyRun_String(str, Py_file_input, globals, locals);
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004191 Py_XDECREF(tmp);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004192 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004193 if (plain)
4194 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004195 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004196 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00004197 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004198 return 0;
4199}
Guido van Rossum24c13741995-02-14 09:42:43 +00004200
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00004201Py_LOCAL(void)
Paul Prescode68140d2000-08-30 20:25:01 +00004202format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4203{
4204 char *obj_str;
4205
4206 if (!obj)
4207 return;
4208
4209 obj_str = PyString_AsString(obj);
4210 if (!obj_str)
4211 return;
4212
4213 PyErr_Format(exc, format_str, obj_str);
4214}
Guido van Rossum950361c1997-01-24 13:49:28 +00004215
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00004216Py_LOCAL(PyObject *)
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004217string_concatenate(PyObject *v, PyObject *w,
4218 PyFrameObject *f, unsigned char *next_instr)
4219{
4220 /* This function implements 'variable += expr' when both arguments
4221 are strings. */
Tim Peters7df5e7f2006-05-26 23:14:37 +00004222
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004223 if (v->ob_refcnt == 2) {
4224 /* In the common case, there are 2 references to the value
4225 * stored in 'variable' when the += is performed: one on the
4226 * value stack (in 'v') and one still stored in the 'variable'.
4227 * We try to delete the variable now to reduce the refcnt to 1.
4228 */
4229 switch (*next_instr) {
4230 case STORE_FAST:
4231 {
4232 int oparg = PEEKARG();
4233 PyObject **fastlocals = f->f_localsplus;
4234 if (GETLOCAL(oparg) == v)
4235 SETLOCAL(oparg, NULL);
4236 break;
4237 }
4238 case STORE_DEREF:
4239 {
Richard Jonescebbefc2006-05-23 18:28:17 +00004240 PyObject **freevars = f->f_localsplus + f->f_code->co_nlocals;
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004241 PyObject *c = freevars[PEEKARG()];
4242 if (PyCell_GET(c) == v)
4243 PyCell_Set(c, NULL);
4244 break;
4245 }
4246 case STORE_NAME:
4247 {
4248 PyObject *names = f->f_code->co_names;
4249 PyObject *name = GETITEM(names, PEEKARG());
4250 PyObject *locals = f->f_locals;
4251 if (PyDict_CheckExact(locals) &&
4252 PyDict_GetItem(locals, name) == v) {
4253 if (PyDict_DelItem(locals, name) != 0) {
4254 PyErr_Clear();
4255 }
4256 }
4257 break;
4258 }
4259 }
4260 }
4261
Armin Rigo618fbf52004-08-07 20:58:32 +00004262 if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004263 /* Now we own the last reference to 'v', so we can resize it
4264 * in-place.
4265 */
Thomas Wouters79cdce32006-04-19 15:09:44 +00004266 Py_ssize_t v_len = PyString_GET_SIZE(v);
4267 Py_ssize_t w_len = PyString_GET_SIZE(w);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004268 if (_PyString_Resize(&v, v_len + w_len) != 0) {
4269 /* XXX if _PyString_Resize() fails, 'v' has been
4270 * deallocated so it cannot be put back into 'variable'.
4271 * The MemoryError is raised when there is no value in
4272 * 'variable', which might (very remotely) be a cause
4273 * of incompatibilities.
4274 */
4275 return NULL;
4276 }
4277 /* copy 'w' into the newly allocated area of 'v' */
4278 memcpy(PyString_AS_STRING(v) + v_len,
4279 PyString_AS_STRING(w), w_len);
4280 return v;
4281 }
4282 else {
4283 /* When in-place resizing is not an option. */
4284 PyString_Concat(&v, w);
4285 return v;
4286 }
4287}
4288
Guido van Rossum950361c1997-01-24 13:49:28 +00004289#ifdef DYNAMIC_EXECUTION_PROFILE
4290
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +00004291Py_LOCAL(PyObject *)
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004292getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004293{
4294 int i;
4295 PyObject *l = PyList_New(256);
4296 if (l == NULL) return NULL;
4297 for (i = 0; i < 256; i++) {
4298 PyObject *x = PyInt_FromLong(a[i]);
4299 if (x == NULL) {
4300 Py_DECREF(l);
4301 return NULL;
4302 }
4303 PyList_SetItem(l, i, x);
4304 }
4305 for (i = 0; i < 256; i++)
4306 a[i] = 0;
4307 return l;
4308}
4309
4310PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004311_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004312{
4313#ifndef DXPAIRS
4314 return getarray(dxp);
4315#else
4316 int i;
4317 PyObject *l = PyList_New(257);
4318 if (l == NULL) return NULL;
4319 for (i = 0; i < 257; i++) {
4320 PyObject *x = getarray(dxpairs[i]);
4321 if (x == NULL) {
4322 Py_DECREF(l);
4323 return NULL;
4324 }
4325 PyList_SetItem(l, i, x);
4326 }
4327 return l;
4328#endif
4329}
4330
4331#endif