blob: 6f66c8c15a6efd3873f8ff22d3138240144b3905 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003
Guido van Rossum681d79a1995-07-18 14:51:37 +00004/* XXX TO DO:
Guido van Rossum681d79a1995-07-18 14:51:37 +00005 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +00006 XXX document it!
7 */
8
Guido van Rossumb209a111997-04-29 18:18:01 +00009#include "Python.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000010
Guido van Rossum10dc2e81990-11-18 17:27:39 +000011#include "compile.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000012#include "frameobject.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000013#include "eval.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000014#include "opcode.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000015#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000016
Guido van Rossumc6004111993-11-05 10:22:19 +000017#include <ctype.h>
18
Martin v. Löwis87fa7852004-08-29 15:51:52 +000019#ifndef WITH_TSC
20#define rdtscll(var)
21#else /*WITH_TSC defined*/
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000022
23typedef unsigned long long uint64;
24
Michael W. Hudson800ba232004-08-12 18:19:17 +000025#if defined(__ppc__) /* <- Don't know if this is the correct symbol; this
26 section should work for GCC on any PowerPC platform,
27 irrespective of OS. POWER? Who knows :-) */
28
29#define rdtscll(var) ppc_getcounter(&var)
30
31static void
32ppc_getcounter(uint64 *v)
33{
34 register unsigned long tbu, tb, tbu2;
35
36 loop:
37 asm volatile ("mftbu %0" : "=r" (tbu) );
38 asm volatile ("mftb %0" : "=r" (tb) );
39 asm volatile ("mftbu %0" : "=r" (tbu2));
40 if (__builtin_expect(tbu != tbu2, 0)) goto loop;
41
42 /* The slightly peculiar way of writing the next lines is
43 compiled better by GCC than any other way I tried. */
44 ((long*)(v))[0] = tbu;
45 ((long*)(v))[1] = tb;
46}
47
48#else /* this section is for linux/x86 */
49
50#include <asm/msr.h>
51
52#endif
53
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000054void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
55 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
56{
57 uint64 intr, inst, loop;
58 PyThreadState *tstate = PyThreadState_Get();
59 if (!tstate->interp->tscdump)
60 return;
61 intr = intr1 - intr0;
62 inst = inst1 - inst0 - intr;
63 loop = loop1 - loop0 - intr;
64 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
65 opcode, ticked, inst, loop);
66}
Michael W. Hudson800ba232004-08-12 18:19:17 +000067
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000068#endif
69
Guido van Rossum04691fc1992-08-12 15:35:34 +000070/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000071/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000072
Guido van Rossum408027e1996-12-30 16:17:54 +000073#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000074/* For debugging the interpreter: */
75#define LLTRACE 1 /* Low-level trace feature */
76#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000077#endif
78
Jeremy Hylton52820442001-01-03 23:52:36 +000079typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000080
Guido van Rossum374a9221991-04-04 10:40:29 +000081/* Forward declarations */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000082#ifdef WITH_TSC
83static PyObject *call_function(PyObject ***, int, uint64*, uint64*);
84#else
Jeremy Hyltone8c04322002-08-16 17:47:26 +000085static PyObject *call_function(PyObject ***, int);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000086#endif
Jeremy Hylton52820442001-01-03 23:52:36 +000087static PyObject *fast_function(PyObject *, PyObject ***, int, int, int);
Jeremy Hylton52820442001-01-03 23:52:36 +000088static PyObject *do_call(PyObject *, PyObject ***, int, int);
89static PyObject *ext_do_call(PyObject *, PyObject ***, int, int, int);
Guido van Rossumac7be682001-01-17 15:42:30 +000090static PyObject *update_keyword_args(PyObject *, int, PyObject ***,PyObject *);
Ka-Ping Yee20579702001-01-15 22:14:16 +000091static PyObject *update_star_args(int, int, PyObject *, PyObject ***);
Jeremy Hylton52820442001-01-03 23:52:36 +000092static PyObject *load_args(PyObject ***, int);
93#define CALL_FLAG_VAR 1
94#define CALL_FLAG_KW 2
95
Guido van Rossum0a066c01992-03-27 17:29:15 +000096#ifdef LLTRACE
Tim Petersdbd9ba62000-07-09 03:09:57 +000097static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000098#endif
Fred Drake5755ce62001-06-27 19:19:46 +000099static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
100 int, PyObject *);
Fred Drake4ec5d562001-10-04 19:26:43 +0000101static void call_trace_protected(Py_tracefunc, PyObject *,
102 PyFrameObject *, int);
Fred Drake5755ce62001-06-27 19:19:46 +0000103static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +0000104static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Armin Rigobf57a142004-03-22 19:24:58 +0000105 PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000106
Tim Petersdbd9ba62000-07-09 03:09:57 +0000107static PyObject *apply_slice(PyObject *, PyObject *, PyObject *);
108static int assign_slice(PyObject *, PyObject *,
109 PyObject *, PyObject *);
110static PyObject *cmp_outcome(int, PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +0000111static PyObject *import_from(PyObject *, PyObject *);
112static int import_all_from(PyObject *, PyObject *);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000113static PyObject *build_class(PyObject *, PyObject *, PyObject *);
114static int exec_statement(PyFrameObject *,
115 PyObject *, PyObject *, PyObject *);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000116static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *);
117static void reset_exc_info(PyThreadState *);
Paul Prescode68140d2000-08-30 20:25:01 +0000118static void format_exc_check_arg(PyObject *, char *, PyObject *);
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000119static PyObject *string_concatenate(PyObject *, PyObject *,
120 PyFrameObject *, unsigned char *);
Guido van Rossum374a9221991-04-04 10:40:29 +0000121
Paul Prescode68140d2000-08-30 20:25:01 +0000122#define NAME_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000123 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000124#define GLOBAL_NAME_ERROR_MSG \
125 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000126#define UNBOUNDLOCAL_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000127 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000128#define UNBOUNDFREE_ERROR_MSG \
129 "free variable '%.200s' referenced before assignment" \
130 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000131
Guido van Rossum950361c1997-01-24 13:49:28 +0000132/* Dynamic execution profile */
133#ifdef DYNAMIC_EXECUTION_PROFILE
134#ifdef DXPAIRS
135static long dxpairs[257][256];
136#define dxp dxpairs[256]
137#else
138static long dxp[256];
139#endif
140#endif
141
Jeremy Hylton985eba52003-02-05 23:13:00 +0000142/* Function call profile */
143#ifdef CALL_PROFILE
144#define PCALL_NUM 11
145static int pcall[PCALL_NUM];
146
147#define PCALL_ALL 0
148#define PCALL_FUNCTION 1
149#define PCALL_FAST_FUNCTION 2
150#define PCALL_FASTER_FUNCTION 3
151#define PCALL_METHOD 4
152#define PCALL_BOUND_METHOD 5
153#define PCALL_CFUNCTION 6
154#define PCALL_TYPE 7
155#define PCALL_GENERATOR 8
156#define PCALL_OTHER 9
157#define PCALL_POP 10
158
159/* Notes about the statistics
160
161 PCALL_FAST stats
162
163 FAST_FUNCTION means no argument tuple needs to be created.
164 FASTER_FUNCTION means that the fast-path frame setup code is used.
165
166 If there is a method call where the call can be optimized by changing
167 the argument tuple and calling the function directly, it gets recorded
168 twice.
169
170 As a result, the relationship among the statistics appears to be
171 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
172 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
173 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
174 PCALL_METHOD > PCALL_BOUND_METHOD
175*/
176
177#define PCALL(POS) pcall[POS]++
178
179PyObject *
180PyEval_GetCallStats(PyObject *self)
181{
Tim Peters8a5c3c72004-04-05 19:36:21 +0000182 return Py_BuildValue("iiiiiiiiii",
Jeremy Hylton985eba52003-02-05 23:13:00 +0000183 pcall[0], pcall[1], pcall[2], pcall[3],
184 pcall[4], pcall[5], pcall[6], pcall[7],
185 pcall[8], pcall[9]);
186}
187#else
188#define PCALL(O)
189
190PyObject *
191PyEval_GetCallStats(PyObject *self)
192{
193 Py_INCREF(Py_None);
194 return Py_None;
195}
196#endif
197
Tim Peters5ca576e2001-06-18 22:08:13 +0000198
Guido van Rossume59214e1994-08-30 08:01:59 +0000199#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000200
Guido van Rossum2571cc81999-04-07 16:07:23 +0000201#ifndef DONT_HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000202#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000203#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000204#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000205
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000206static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
Guido van Rossuma9672091994-09-14 13:31:22 +0000207static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000208
Tim Peters7f468f22004-10-11 02:40:51 +0000209int
210PyEval_ThreadsInitialized(void)
211{
212 return interpreter_lock != 0;
213}
214
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000215void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000216PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000217{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000218 if (interpreter_lock)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000219 return;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000220 interpreter_lock = PyThread_allocate_lock();
221 PyThread_acquire_lock(interpreter_lock, 1);
222 main_thread = PyThread_get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000223}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000224
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000225void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000226PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000227{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000228 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000229}
230
231void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000232PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000233{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000234 PyThread_release_lock(interpreter_lock);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000235}
236
237void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000238PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000239{
240 if (tstate == NULL)
241 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000242 /* Check someone has called PyEval_InitThreads() to create the lock */
243 assert(interpreter_lock);
Guido van Rossum65d5b571998-12-21 19:32:43 +0000244 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000245 if (PyThreadState_Swap(tstate) != NULL)
246 Py_FatalError(
247 "PyEval_AcquireThread: non-NULL old thread state");
248}
249
250void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000251PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000252{
253 if (tstate == NULL)
254 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
255 if (PyThreadState_Swap(NULL) != tstate)
256 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000257 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000258}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000259
260/* This function is called from PyOS_AfterFork to ensure that newly
261 created child processes don't hold locks referring to threads which
262 are not running in the child process. (This could also be done using
263 pthread_atfork mechanism, at least for the pthreads implementation.) */
264
265void
266PyEval_ReInitThreads(void)
267{
268 if (!interpreter_lock)
269 return;
270 /*XXX Can't use PyThread_free_lock here because it does too
271 much error-checking. Doing this cleanly would require
272 adding a new function to each thread_*.h. Instead, just
273 create a new lock and waste a little bit of memory */
274 interpreter_lock = PyThread_allocate_lock();
275 PyThread_acquire_lock(interpreter_lock, 1);
276 main_thread = PyThread_get_thread_ident();
277}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000278#endif
279
Guido van Rossumff4949e1992-08-05 19:58:53 +0000280/* Functions save_thread and restore_thread are always defined so
281 dynamically loaded modules needn't be compiled separately for use
282 with and without threads: */
283
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000284PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000285PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000286{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000287 PyThreadState *tstate = PyThreadState_Swap(NULL);
288 if (tstate == NULL)
289 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000290#ifdef WITH_THREAD
Guido van Rossumb74eca91997-09-30 22:03:16 +0000291 if (interpreter_lock)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000292 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000293#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000294 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000295}
296
297void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000298PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000299{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000300 if (tstate == NULL)
301 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000302#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000303 if (interpreter_lock) {
Guido van Rossumb74eca91997-09-30 22:03:16 +0000304 int err = errno;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000305 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000306 errno = err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000307 }
308#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000309 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000310}
311
312
Guido van Rossuma9672091994-09-14 13:31:22 +0000313/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
314 signal handlers or Mac I/O completion routines) can schedule calls
315 to a function to be called synchronously.
316 The synchronous function is called with one void* argument.
317 It should return 0 for success or -1 for failure -- failure should
318 be accompanied by an exception.
319
320 If registry succeeds, the registry function returns 0; if it fails
321 (e.g. due to too many pending calls) it returns -1 (without setting
322 an exception condition).
323
324 Note that because registry may occur from within signal handlers,
325 or other asynchronous events, calling malloc() is unsafe!
326
327#ifdef WITH_THREAD
328 Any thread can schedule pending calls, but only the main thread
329 will execute them.
330#endif
331
332 XXX WARNING! ASYNCHRONOUSLY EXECUTING CODE!
333 There are two possible race conditions:
334 (1) nested asynchronous registry calls;
335 (2) registry calls made while pending calls are being processed.
336 While (1) is very unlikely, (2) is a real possibility.
337 The current code is safe against (2), but not against (1).
338 The safety against (2) is derived from the fact that only one
339 thread (the main thread) ever takes things out of the queue.
Guido van Rossuma9672091994-09-14 13:31:22 +0000340
Guido van Rossuma027efa1997-05-05 20:56:21 +0000341 XXX Darn! With the advent of thread state, we should have an array
342 of pending calls per thread in the thread state! Later...
343*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000344
Guido van Rossuma9672091994-09-14 13:31:22 +0000345#define NPENDINGCALLS 32
346static struct {
Thomas Wouters334fb892000-07-25 12:56:38 +0000347 int (*func)(void *);
348 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000349} pendingcalls[NPENDINGCALLS];
350static volatile int pendingfirst = 0;
351static volatile int pendinglast = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000352static volatile int things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000353
354int
Thomas Wouters334fb892000-07-25 12:56:38 +0000355Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000356{
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000357 static volatile int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000358 int i, j;
359 /* XXX Begin critical section */
360 /* XXX If you want this to be safe against nested
361 XXX asynchronous calls, you'll have to work harder! */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000362 if (busy)
363 return -1;
364 busy = 1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000365 i = pendinglast;
366 j = (i + 1) % NPENDINGCALLS;
Guido van Rossum04e70322002-07-17 16:57:13 +0000367 if (j == pendingfirst) {
368 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000369 return -1; /* Queue full */
Guido van Rossum04e70322002-07-17 16:57:13 +0000370 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000371 pendingcalls[i].func = func;
372 pendingcalls[i].arg = arg;
373 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000374
375 _Py_Ticker = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000376 things_to_do = 1; /* Signal main loop */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000377 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000378 /* XXX End critical section */
379 return 0;
380}
381
Guido van Rossum180d7b41994-09-29 09:45:57 +0000382int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000383Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000384{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000385 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000386#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000387 if (main_thread && PyThread_get_thread_ident() != main_thread)
Guido van Rossuma9672091994-09-14 13:31:22 +0000388 return 0;
389#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000390 if (busy)
Guido van Rossum180d7b41994-09-29 09:45:57 +0000391 return 0;
392 busy = 1;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000393 things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000394 for (;;) {
395 int i;
Thomas Wouters334fb892000-07-25 12:56:38 +0000396 int (*func)(void *);
397 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000398 i = pendingfirst;
399 if (i == pendinglast)
400 break; /* Queue empty */
401 func = pendingcalls[i].func;
402 arg = pendingcalls[i].arg;
403 pendingfirst = (i + 1) % NPENDINGCALLS;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000404 if (func(arg) < 0) {
405 busy = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000406 things_to_do = 1; /* We're not done yet */
Guido van Rossuma9672091994-09-14 13:31:22 +0000407 return -1;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000408 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000409 }
Guido van Rossum180d7b41994-09-29 09:45:57 +0000410 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000411 return 0;
412}
413
414
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000415/* The interpreter's recursion limit */
416
Guido van Rossum349ff6f2000-09-01 01:52:08 +0000417static int recursion_limit = 1000;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000418int _Py_CheckRecursionLimit = 1000;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000419
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000420int
421Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000422{
423 return recursion_limit;
424}
425
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000426void
427Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000428{
429 recursion_limit = new_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000430 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000431}
432
Armin Rigo2b3eb402003-10-28 12:05:48 +0000433/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
434 if the recursion_depth reaches _Py_CheckRecursionLimit.
435 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
436 to guarantee that _Py_CheckRecursiveCall() is regularly called.
437 Without USE_STACKCHECK, there is no need for this. */
438int
439_Py_CheckRecursiveCall(char *where)
440{
441 PyThreadState *tstate = PyThreadState_GET();
442
443#ifdef USE_STACKCHECK
444 if (PyOS_CheckStack()) {
445 --tstate->recursion_depth;
446 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
447 return -1;
448 }
449#endif
450 if (tstate->recursion_depth > recursion_limit) {
451 --tstate->recursion_depth;
452 PyErr_Format(PyExc_RuntimeError,
453 "maximum recursion depth exceeded%s",
454 where);
455 return -1;
456 }
457 _Py_CheckRecursionLimit = recursion_limit;
458 return 0;
459}
460
Guido van Rossum374a9221991-04-04 10:40:29 +0000461/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000462enum why_code {
463 WHY_NOT = 0x0001, /* No error */
464 WHY_EXCEPTION = 0x0002, /* Exception occurred */
465 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
466 WHY_RETURN = 0x0008, /* 'return' statement */
467 WHY_BREAK = 0x0010, /* 'break' statement */
468 WHY_CONTINUE = 0x0020, /* 'continue' statement */
469 WHY_YIELD = 0x0040 /* 'yield' operator */
470};
Guido van Rossum374a9221991-04-04 10:40:29 +0000471
Raymond Hettinger7c958652004-04-06 10:11:10 +0000472static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
Tim Petersd6d010b2001-06-21 02:49:55 +0000473static int unpack_iterable(PyObject *, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000474
Skip Montanarod581d772002-09-03 20:10:45 +0000475/* for manipulating the thread switch and periodic "stuff" - used to be
476 per thread, now just a pair o' globals */
Skip Montanaro99dba272002-09-03 20:19:06 +0000477int _Py_CheckInterval = 100;
478volatile int _Py_Ticker = 100;
Guido van Rossum374a9221991-04-04 10:40:29 +0000479
Guido van Rossumb209a111997-04-29 18:18:01 +0000480PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000481PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000482{
Jeremy Hylton985eba52003-02-05 23:13:00 +0000483 /* XXX raise SystemError if globals is NULL */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000484 return PyEval_EvalCodeEx(co,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000485 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000486 (PyObject **)NULL, 0,
487 (PyObject **)NULL, 0,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000488 (PyObject **)NULL, 0,
489 NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000490}
491
492
493/* Interpreter main loop */
494
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000495PyObject *
496PyEval_EvalFrame(PyFrameObject *f)
Guido van Rossum374a9221991-04-04 10:40:29 +0000497{
Guido van Rossum950361c1997-01-24 13:49:28 +0000498#ifdef DXPAIRS
499 int lastopcode = 0;
500#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +0000501 register PyObject **stack_pointer; /* Next free slot in value stack */
Guido van Rossum374a9221991-04-04 10:40:29 +0000502 register unsigned char *next_instr;
Armin Rigo8817fcd2004-06-17 10:22:40 +0000503 register int opcode; /* Current opcode */
504 register int oparg; /* Current opcode argument, if any */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000505 register enum why_code why; /* Reason for block stack unwind */
Guido van Rossum374a9221991-04-04 10:40:29 +0000506 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000507 register PyObject *x; /* Result object -- NULL if error */
508 register PyObject *v; /* Temporary objects popped off stack */
509 register PyObject *w;
510 register PyObject *u;
511 register PyObject *t;
Barry Warsaw23c9ec82000-08-21 15:44:01 +0000512 register PyObject *stream = NULL; /* for PRINT opcodes */
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000513 register PyObject **fastlocals, **freevars;
Guido van Rossum014518f1998-11-23 21:09:51 +0000514 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000515 PyThreadState *tstate = PyThreadState_GET();
Tim Peters5ca576e2001-06-18 22:08:13 +0000516 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000517
Tim Peters8a5c3c72004-04-05 19:36:21 +0000518 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000519
520 not (instr_lb <= current_bytecode_offset < instr_ub)
521
Tim Peters8a5c3c72004-04-05 19:36:21 +0000522 is true when the line being executed has changed. The
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000523 initial values are such as to make this false the first
524 time it is tested. */
Armin Rigobf57a142004-03-22 19:24:58 +0000525 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000526
Guido van Rossumd076c731998-10-07 19:42:25 +0000527 unsigned char *first_instr;
Skip Montanaro04d80f82002-08-04 21:03:35 +0000528 PyObject *names;
529 PyObject *consts;
Guido van Rossum96a42c81992-01-12 02:29:51 +0000530#ifdef LLTRACE
Guido van Rossumacbe8da1993-04-15 15:33:52 +0000531 int lltrace;
Guido van Rossum374a9221991-04-04 10:40:29 +0000532#endif
Guido van Rossum408027e1996-12-30 16:17:54 +0000533#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000534 /* Make it easier to find out where we are with a debugger */
Tim Peters5ca576e2001-06-18 22:08:13 +0000535 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000536#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000537
Neal Norwitza81d2202002-07-14 00:27:26 +0000538/* Tuple access macros */
539
540#ifndef Py_DEBUG
541#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
542#else
543#define GETITEM(v, i) PyTuple_GetItem((v), (i))
544#endif
545
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000546#ifdef WITH_TSC
547/* Use Pentium timestamp counter to mark certain events:
548 inst0 -- beginning of switch statement for opcode dispatch
549 inst1 -- end of switch statement (may be skipped)
550 loop0 -- the top of the mainloop
551 loop1 -- place where control returns again to top of mainloop
552 (may be skipped)
553 intr1 -- beginning of long interruption
554 intr2 -- end of long interruption
555
556 Many opcodes call out to helper C functions. In some cases, the
557 time in those functions should be counted towards the time for the
558 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
559 calls another Python function; there's no point in charge all the
560 bytecode executed by the called function to the caller.
561
562 It's hard to make a useful judgement statically. In the presence
563 of operator overloading, it's impossible to tell if a call will
564 execute new Python code or not.
565
566 It's a case-by-case judgement. I'll use intr1 for the following
567 cases:
568
569 EXEC_STMT
570 IMPORT_STAR
571 IMPORT_FROM
572 CALL_FUNCTION (and friends)
573
574 */
575 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
576 int ticked = 0;
577
578 rdtscll(inst0);
579 rdtscll(inst1);
580 rdtscll(loop0);
581 rdtscll(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000582
583 /* shut up the compiler */
584 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000585#endif
586
Guido van Rossum374a9221991-04-04 10:40:29 +0000587/* Code access macros */
588
Guido van Rossumd076c731998-10-07 19:42:25 +0000589#define INSTR_OFFSET() (next_instr - first_instr)
Guido van Rossum374a9221991-04-04 10:40:29 +0000590#define NEXTOP() (*next_instr++)
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000591#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000592#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
Guido van Rossumd076c731998-10-07 19:42:25 +0000593#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000594#define JUMPBY(x) (next_instr += (x))
595
Raymond Hettingerf606f872003-03-16 03:11:04 +0000596/* OpCode prediction macros
597 Some opcodes tend to come in pairs thus making it possible to predict
598 the second code when the first is run. For example, COMPARE_OP is often
599 followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And, those opcodes are often
600 followed by a POP_TOP.
601
602 Verifying the prediction costs a single high-speed test of register
Raymond Hettingerac2072922003-03-16 15:41:11 +0000603 variable against a constant. If the pairing was good, then the
Raymond Hettingerf606f872003-03-16 03:11:04 +0000604 processor has a high likelihood of making its own successful branch
605 prediction which results in a nearly zero overhead transition to the
606 next opcode.
607
608 A successful prediction saves a trip through the eval-loop including
609 its two unpredictable branches, the HASARG test and the switch-case.
Raymond Hettingera7216982004-02-08 19:59:27 +0000610
Tim Peters8a5c3c72004-04-05 19:36:21 +0000611 If collecting opcode statistics, turn off prediction so that
612 statistics are accurately maintained (the predictions bypass
Raymond Hettingera7216982004-02-08 19:59:27 +0000613 the opcode frequency counter updates).
Raymond Hettingerf606f872003-03-16 03:11:04 +0000614*/
615
Raymond Hettingera7216982004-02-08 19:59:27 +0000616#ifdef DYNAMIC_EXECUTION_PROFILE
617#define PREDICT(op) if (0) goto PRED_##op
618#else
Raymond Hettingerac2072922003-03-16 15:41:11 +0000619#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000620#endif
621
Raymond Hettingerf606f872003-03-16 03:11:04 +0000622#define PREDICTED(op) PRED_##op: next_instr++
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000623#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Raymond Hettingerf606f872003-03-16 03:11:04 +0000624
Guido van Rossum374a9221991-04-04 10:40:29 +0000625/* Stack manipulation macros */
626
627#define STACK_LEVEL() (stack_pointer - f->f_valuestack)
628#define EMPTY() (STACK_LEVEL() == 0)
629#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000630#define SECOND() (stack_pointer[-2])
631#define THIRD() (stack_pointer[-3])
632#define FOURTH() (stack_pointer[-4])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000633#define SET_TOP(v) (stack_pointer[-1] = (v))
634#define SET_SECOND(v) (stack_pointer[-2] = (v))
635#define SET_THIRD(v) (stack_pointer[-3] = (v))
636#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000637#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000638#define BASIC_PUSH(v) (*stack_pointer++ = (v))
639#define BASIC_POP() (*--stack_pointer)
640
Guido van Rossum96a42c81992-01-12 02:29:51 +0000641#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000642#define PUSH(v) { (void)(BASIC_PUSH(v), \
643 lltrace && prtrace(TOP(), "push")); \
644 assert(STACK_LEVEL() <= f->f_stacksize); }
Fred Drakede26cfc2001-10-13 06:11:28 +0000645#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000646#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
647 lltrace && prtrace(TOP(), "stackadj")); \
648 assert(STACK_LEVEL() <= f->f_stacksize); }
Guido van Rossum374a9221991-04-04 10:40:29 +0000649#else
650#define PUSH(v) BASIC_PUSH(v)
651#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000652#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000653#endif
654
Guido van Rossum681d79a1995-07-18 14:51:37 +0000655/* Local variable macros */
656
657#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000658
659/* The SETLOCAL() macro must not DECREF the local variable in-place and
660 then store the new value; it must copy the old value to a temporary
661 value, then store the new value, and then DECREF the temporary value.
662 This is because it is possible that during the DECREF the frame is
663 accessed by other code (e.g. a __del__ method or gc.collect()) and the
664 variable would be pointing to already-freed memory. */
665#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
666 GETLOCAL(i) = value; \
667 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000668
Guido van Rossuma027efa1997-05-05 20:56:21 +0000669/* Start of code */
670
Tim Peters5ca576e2001-06-18 22:08:13 +0000671 if (f == NULL)
672 return NULL;
673
Armin Rigo1d313ab2003-10-25 14:33:09 +0000674 /* push frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000675 if (Py_EnterRecursiveCall(""))
Armin Rigo1d313ab2003-10-25 14:33:09 +0000676 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000677
Tim Peters5ca576e2001-06-18 22:08:13 +0000678 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000679
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000680 if (tstate->use_tracing) {
681 if (tstate->c_tracefunc != NULL) {
682 /* tstate->c_tracefunc, if defined, is a
683 function that will be called on *every* entry
684 to a code block. Its return value, if not
685 None, is a function that will be called at
686 the start of each executed line of code.
687 (Actually, the function must return itself
688 in order to continue tracing.) The trace
689 functions are called with three arguments:
690 a pointer to the current frame, a string
691 indicating why the function is called, and
692 an argument which depends on the situation.
693 The global trace function is also called
694 whenever an exception is detected. */
695 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
696 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000697 /* Trace function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000698 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000699 }
700 }
701 if (tstate->c_profilefunc != NULL) {
702 /* Similar for c_profilefunc, except it needn't
703 return itself and isn't called for "line" events */
704 if (call_trace(tstate->c_profilefunc,
705 tstate->c_profileobj,
706 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000707 /* Profile function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000708 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000709 }
710 }
711 }
712
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000713 co = f->f_code;
714 names = co->co_names;
715 consts = co->co_consts;
716 fastlocals = f->f_localsplus;
717 freevars = f->f_localsplus + f->f_nlocals;
Michael W. Hudsonecfeb7f2004-02-12 15:28:27 +0000718 first_instr = PyString_AS_STRING(co->co_code);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000719 /* An explanation is in order for the next line.
720
721 f->f_lasti now refers to the index of the last instruction
722 executed. You might think this was obvious from the name, but
723 this wasn't always true before 2.3! PyFrame_New now sets
724 f->f_lasti to -1 (i.e. the index *before* the first instruction)
725 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
726 does work. Promise. */
727 next_instr = first_instr + f->f_lasti + 1;
728 stack_pointer = f->f_stacktop;
729 assert(stack_pointer != NULL);
730 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
731
Tim Peters5ca576e2001-06-18 22:08:13 +0000732#ifdef LLTRACE
733 lltrace = PyDict_GetItemString(f->f_globals,"__lltrace__") != NULL;
734#endif
735#if defined(Py_DEBUG) || defined(LLTRACE)
736 filename = PyString_AsString(co->co_filename);
737#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000738
Guido van Rossum374a9221991-04-04 10:40:29 +0000739 why = WHY_NOT;
740 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +0000741 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +0000742 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +0000743
Guido van Rossum374a9221991-04-04 10:40:29 +0000744 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000745#ifdef WITH_TSC
746 if (inst1 == 0) {
747 /* Almost surely, the opcode executed a break
748 or a continue, preventing inst1 from being set
749 on the way out of the loop.
750 */
751 rdtscll(inst1);
752 loop1 = inst1;
753 }
754 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
755 intr0, intr1);
756 ticked = 0;
757 inst1 = 0;
758 intr0 = 0;
759 intr1 = 0;
760 rdtscll(loop0);
761#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000762 assert(stack_pointer >= f->f_valuestack); /* else underflow */
763 assert(STACK_LEVEL() <= f->f_stacksize); /* else overflow */
764
Guido van Rossuma027efa1997-05-05 20:56:21 +0000765 /* Do periodic things. Doing this every time through
766 the loop would add too much overhead, so we do it
767 only every Nth instruction. We also do it if
768 ``things_to_do'' is set, i.e. when an asynchronous
769 event needs attention (e.g. a signal handler or
770 async I/O handler); see Py_AddPendingCall() and
771 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000772
Skip Montanarod581d772002-09-03 20:10:45 +0000773 if (--_Py_Ticker < 0) {
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000774 if (*next_instr == SETUP_FINALLY) {
775 /* Make the last opcode before
776 a try: finally: block uninterruptable. */
777 goto fast_next_opcode;
778 }
Skip Montanarod581d772002-09-03 20:10:45 +0000779 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000780 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000781#ifdef WITH_TSC
782 ticked = 1;
783#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000784 if (things_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +0000785 if (Py_MakePendingCalls() < 0) {
786 why = WHY_EXCEPTION;
787 goto on_error;
788 }
Kurt B. Kaiser4c79a832004-11-23 18:06:08 +0000789 if (things_to_do)
790 /* MakePendingCalls() didn't succeed.
791 Force early re-execution of this
792 "periodic" code, possibly after
793 a thread switch */
794 _Py_Ticker = 0;
Guido van Rossum8861b741996-07-30 16:49:37 +0000795 }
Guido van Rossume59214e1994-08-30 08:01:59 +0000796#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000797 if (interpreter_lock) {
798 /* Give another thread a chance */
799
Guido van Rossum25ce5661997-08-02 03:10:38 +0000800 if (PyThreadState_Swap(NULL) != tstate)
801 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000802 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000803
804 /* Other threads may run now */
805
Guido van Rossum65d5b571998-12-21 19:32:43 +0000806 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000807 if (PyThreadState_Swap(tstate) != NULL)
808 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000809
810 /* Check for thread interrupts */
811
812 if (tstate->async_exc != NULL) {
813 x = tstate->async_exc;
814 tstate->async_exc = NULL;
815 PyErr_SetNone(x);
816 Py_DECREF(x);
817 why = WHY_EXCEPTION;
818 goto on_error;
819 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000820 }
821#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000822 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000823
Neil Schemenauer63543862002-02-17 19:10:14 +0000824 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +0000825 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +0000826
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000827 /* line-by-line tracing support */
828
829 if (tstate->c_tracefunc != NULL && !tstate->tracing) {
830 /* see maybe_call_line_trace
831 for expository comments */
832 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +0000833
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000834 err = maybe_call_line_trace(tstate->c_tracefunc,
835 tstate->c_traceobj,
Armin Rigobf57a142004-03-22 19:24:58 +0000836 f, &instr_lb, &instr_ub,
837 &instr_prev);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000838 /* Reload possibly changed frame fields */
839 JUMPTO(f->f_lasti);
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000840 if (f->f_stacktop != NULL) {
841 stack_pointer = f->f_stacktop;
842 f->f_stacktop = NULL;
843 }
844 if (err) {
845 /* trace function raised an exception */
846 goto on_error;
847 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000848 }
849
850 /* Extract opcode and argument */
851
Guido van Rossum374a9221991-04-04 10:40:29 +0000852 opcode = NEXTOP();
Armin Rigo8817fcd2004-06-17 10:22:40 +0000853 oparg = 0; /* allows oparg to be stored in a register because
854 it doesn't have to be remembered across a full loop */
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000855 if (HAS_ARG(opcode))
856 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +0000857 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +0000858#ifdef DYNAMIC_EXECUTION_PROFILE
859#ifdef DXPAIRS
860 dxpairs[lastopcode][opcode]++;
861 lastopcode = opcode;
862#endif
863 dxp[opcode]++;
864#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000865
Guido van Rossum96a42c81992-01-12 02:29:51 +0000866#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +0000867 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +0000868
Guido van Rossum96a42c81992-01-12 02:29:51 +0000869 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000870 if (HAS_ARG(opcode)) {
871 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000872 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +0000873 }
874 else {
875 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000876 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +0000877 }
878 }
879#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000880
Guido van Rossum374a9221991-04-04 10:40:29 +0000881 /* Main switch on opcode */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000882 rdtscll(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +0000883
Guido van Rossum374a9221991-04-04 10:40:29 +0000884 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +0000885
Guido van Rossum374a9221991-04-04 10:40:29 +0000886 /* BEWARE!
887 It is essential that any operation that fails sets either
888 x to NULL, err to nonzero, or why to anything but WHY_NOT,
889 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000890
Guido van Rossum374a9221991-04-04 10:40:29 +0000891 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000892
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000893 case NOP:
894 goto fast_next_opcode;
895
Neil Schemenauer63543862002-02-17 19:10:14 +0000896 case LOAD_FAST:
897 x = GETLOCAL(oparg);
898 if (x != NULL) {
899 Py_INCREF(x);
900 PUSH(x);
901 goto fast_next_opcode;
902 }
903 format_exc_check_arg(PyExc_UnboundLocalError,
904 UNBOUNDLOCAL_ERROR_MSG,
905 PyTuple_GetItem(co->co_varnames, oparg));
906 break;
907
908 case LOAD_CONST:
Skip Montanaro04d80f82002-08-04 21:03:35 +0000909 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +0000910 Py_INCREF(x);
911 PUSH(x);
912 goto fast_next_opcode;
913
Raymond Hettinger7dc52212003-03-16 20:14:44 +0000914 PREDICTED_WITH_ARG(STORE_FAST);
Neil Schemenauer63543862002-02-17 19:10:14 +0000915 case STORE_FAST:
916 v = POP();
917 SETLOCAL(oparg, v);
918 goto fast_next_opcode;
919
Raymond Hettingerf606f872003-03-16 03:11:04 +0000920 PREDICTED(POP_TOP);
Guido van Rossum374a9221991-04-04 10:40:29 +0000921 case POP_TOP:
922 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000923 Py_DECREF(v);
Neil Schemenauer63543862002-02-17 19:10:14 +0000924 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000925
Guido van Rossum374a9221991-04-04 10:40:29 +0000926 case ROT_TWO:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000927 v = TOP();
928 w = SECOND();
929 SET_TOP(w);
930 SET_SECOND(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000931 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000932
Guido van Rossum374a9221991-04-04 10:40:29 +0000933 case ROT_THREE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000934 v = TOP();
935 w = SECOND();
936 x = THIRD();
937 SET_TOP(w);
938 SET_SECOND(x);
939 SET_THIRD(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000940 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000941
Thomas Wouters434d0822000-08-24 20:11:32 +0000942 case ROT_FOUR:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000943 u = TOP();
944 v = SECOND();
945 w = THIRD();
946 x = FOURTH();
947 SET_TOP(v);
948 SET_SECOND(w);
949 SET_THIRD(x);
950 SET_FOURTH(u);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000951 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000952
Guido van Rossum374a9221991-04-04 10:40:29 +0000953 case DUP_TOP:
954 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000955 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000956 PUSH(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000957 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000958
Thomas Wouters434d0822000-08-24 20:11:32 +0000959 case DUP_TOPX:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000960 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000961 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000962 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000963 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000964 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000965 STACKADJ(2);
966 SET_TOP(x);
967 SET_SECOND(w);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000968 goto fast_next_opcode;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000969 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000970 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000971 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000972 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000973 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000974 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +0000975 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000976 STACKADJ(3);
977 SET_TOP(x);
978 SET_SECOND(w);
979 SET_THIRD(v);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000980 goto fast_next_opcode;
Thomas Wouters434d0822000-08-24 20:11:32 +0000981 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000982 Py_FatalError("invalid argument to DUP_TOPX"
983 " (bytecode corruption?)");
Tim Peters35ba6892000-10-11 07:04:49 +0000984 break;
Thomas Wouters434d0822000-08-24 20:11:32 +0000985
Guido van Rossum374a9221991-04-04 10:40:29 +0000986 case UNARY_POSITIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000987 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000988 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000989 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000990 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000991 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000992 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000993
Guido van Rossum374a9221991-04-04 10:40:29 +0000994 case UNARY_NEGATIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000995 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000996 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000997 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000998 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000999 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001000 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001001
Guido van Rossum374a9221991-04-04 10:40:29 +00001002 case UNARY_NOT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001003 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001004 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001005 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +00001006 if (err == 0) {
1007 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001008 SET_TOP(Py_True);
Guido van Rossumfc490731997-05-06 15:06:49 +00001009 continue;
1010 }
1011 else if (err > 0) {
1012 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001013 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +00001014 err = 0;
1015 continue;
1016 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00001017 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001018 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001019
Guido van Rossum374a9221991-04-04 10:40:29 +00001020 case UNARY_CONVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001021 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001022 x = PyObject_Repr(v);
1023 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001024 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001025 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001026 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001027
Guido van Rossum7928cd71991-10-24 14:59:31 +00001028 case UNARY_INVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001029 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001030 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001031 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001032 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001033 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001034 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001035
Guido van Rossum50564e81996-01-12 01:13:16 +00001036 case BINARY_POWER:
1037 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001038 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001039 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +00001040 Py_DECREF(v);
1041 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001042 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001043 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +00001044 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001045
Guido van Rossum374a9221991-04-04 10:40:29 +00001046 case BINARY_MULTIPLY:
1047 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001048 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001049 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001050 Py_DECREF(v);
1051 Py_DECREF(w);
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 Rossum374a9221991-04-04 10:40:29 +00001056 case BINARY_DIVIDE:
Tim Peters3caca232001-12-06 06:23:26 +00001057 if (!_Py_QnewFlag) {
1058 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001059 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001060 x = PyNumber_Divide(v, w);
1061 Py_DECREF(v);
1062 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001063 SET_TOP(x);
Tim Peters3caca232001-12-06 06:23:26 +00001064 if (x != NULL) continue;
1065 break;
1066 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001067 /* -Qnew is in effect: fall through to
Tim Peters3caca232001-12-06 06:23:26 +00001068 BINARY_TRUE_DIVIDE */
1069 case BINARY_TRUE_DIVIDE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001070 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001071 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001072 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001073 Py_DECREF(v);
1074 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001075 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001076 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001077 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001078
Guido van Rossum4668b002001-08-08 05:00:18 +00001079 case BINARY_FLOOR_DIVIDE:
1080 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001081 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001082 x = PyNumber_FloorDivide(v, w);
1083 Py_DECREF(v);
1084 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001085 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001086 if (x != NULL) continue;
1087 break;
1088
Guido van Rossum374a9221991-04-04 10:40:29 +00001089 case BINARY_MODULO:
1090 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001091 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001092 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001093 Py_DECREF(v);
1094 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001095 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001096 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001097 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001098
Guido van Rossum374a9221991-04-04 10:40:29 +00001099 case BINARY_ADD:
1100 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001101 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001102 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001103 /* INLINE: int + int */
1104 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001105 a = PyInt_AS_LONG(v);
1106 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001107 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001108 if ((i^a) < 0 && (i^b) < 0)
1109 goto slow_add;
1110 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001111 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001112 else if (PyString_CheckExact(v) &&
1113 PyString_CheckExact(w)) {
1114 x = string_concatenate(v, w, f, next_instr);
1115 /* string_concatenate consumed the ref to v */
1116 goto skip_decref_vx;
1117 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001118 else {
1119 slow_add:
Guido van Rossumc12da691997-07-17 23:12:42 +00001120 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001121 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001122 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001123 skip_decref_vx:
Guido van Rossumb209a111997-04-29 18:18:01 +00001124 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001125 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001126 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001127 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001128
Guido van Rossum374a9221991-04-04 10:40:29 +00001129 case BINARY_SUBTRACT:
1130 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001131 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001132 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001133 /* INLINE: int - int */
1134 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001135 a = PyInt_AS_LONG(v);
1136 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001137 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001138 if ((i^a) < 0 && (i^~b) < 0)
1139 goto slow_sub;
1140 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001141 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001142 else {
1143 slow_sub:
Guido van Rossumc12da691997-07-17 23:12:42 +00001144 x = PyNumber_Subtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001145 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001146 Py_DECREF(v);
1147 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001148 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001149 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001150 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001151
Guido van Rossum374a9221991-04-04 10:40:29 +00001152 case BINARY_SUBSCR:
1153 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001154 v = TOP();
Tim Petersb1c46982001-10-05 20:41:38 +00001155 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001156 /* INLINE: list[int] */
1157 long i = PyInt_AsLong(w);
1158 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +00001159 i += PyList_GET_SIZE(v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001160 if (i >= 0 && i < PyList_GET_SIZE(v)) {
Guido van Rossumfa00e951998-07-08 15:02:37 +00001161 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001162 Py_INCREF(x);
1163 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001164 else
1165 goto slow_get;
Guido van Rossumc12da691997-07-17 23:12:42 +00001166 }
1167 else
Raymond Hettinger467a6982004-04-07 11:39:21 +00001168 slow_get:
Guido van Rossumc12da691997-07-17 23:12:42 +00001169 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001170 Py_DECREF(v);
1171 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001172 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001173 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001174 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001175
Guido van Rossum7928cd71991-10-24 14:59:31 +00001176 case BINARY_LSHIFT:
1177 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001178 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001179 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001180 Py_DECREF(v);
1181 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001182 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001183 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001184 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001185
Guido van Rossum7928cd71991-10-24 14:59:31 +00001186 case BINARY_RSHIFT:
1187 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001188 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001189 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001190 Py_DECREF(v);
1191 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001192 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001193 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001194 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001195
Guido van Rossum7928cd71991-10-24 14:59:31 +00001196 case BINARY_AND:
1197 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001198 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001199 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001200 Py_DECREF(v);
1201 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001202 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001203 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001204 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001205
Guido van Rossum7928cd71991-10-24 14:59:31 +00001206 case BINARY_XOR:
1207 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001208 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001209 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001210 Py_DECREF(v);
1211 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001212 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001213 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001214 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001215
Guido van Rossum7928cd71991-10-24 14:59:31 +00001216 case BINARY_OR:
1217 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001218 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001219 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001220 Py_DECREF(v);
1221 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001222 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001223 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001224 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001225
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001226 case LIST_APPEND:
1227 w = POP();
1228 v = POP();
1229 err = PyList_Append(v, w);
1230 Py_DECREF(v);
1231 Py_DECREF(w);
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001232 if (err == 0) {
1233 PREDICT(JUMP_ABSOLUTE);
1234 continue;
1235 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001236 break;
1237
Thomas Wouters434d0822000-08-24 20:11:32 +00001238 case INPLACE_POWER:
1239 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001240 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001241 x = PyNumber_InPlacePower(v, w, Py_None);
1242 Py_DECREF(v);
1243 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001244 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001245 if (x != NULL) continue;
1246 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001247
Thomas Wouters434d0822000-08-24 20:11:32 +00001248 case INPLACE_MULTIPLY:
1249 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001250 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001251 x = PyNumber_InPlaceMultiply(v, w);
1252 Py_DECREF(v);
1253 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001254 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001255 if (x != NULL) continue;
1256 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001257
Thomas Wouters434d0822000-08-24 20:11:32 +00001258 case INPLACE_DIVIDE:
Tim Peters54b11912001-12-25 18:49:11 +00001259 if (!_Py_QnewFlag) {
1260 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001261 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001262 x = PyNumber_InPlaceDivide(v, w);
1263 Py_DECREF(v);
1264 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001265 SET_TOP(x);
Tim Peters54b11912001-12-25 18:49:11 +00001266 if (x != NULL) continue;
1267 break;
1268 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001269 /* -Qnew is in effect: fall through to
Tim Peters54b11912001-12-25 18:49:11 +00001270 INPLACE_TRUE_DIVIDE */
1271 case INPLACE_TRUE_DIVIDE:
Thomas Wouters434d0822000-08-24 20:11:32 +00001272 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001273 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001274 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001275 Py_DECREF(v);
1276 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001277 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001278 if (x != NULL) continue;
1279 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001280
Guido van Rossum4668b002001-08-08 05:00:18 +00001281 case INPLACE_FLOOR_DIVIDE:
1282 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001283 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001284 x = PyNumber_InPlaceFloorDivide(v, w);
1285 Py_DECREF(v);
1286 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001287 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001288 if (x != NULL) continue;
1289 break;
1290
Thomas Wouters434d0822000-08-24 20:11:32 +00001291 case INPLACE_MODULO:
1292 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001293 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001294 x = PyNumber_InPlaceRemainder(v, w);
1295 Py_DECREF(v);
1296 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001297 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001298 if (x != NULL) continue;
1299 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001300
Thomas Wouters434d0822000-08-24 20:11:32 +00001301 case INPLACE_ADD:
1302 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001303 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001304 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001305 /* INLINE: int + int */
1306 register long a, b, i;
1307 a = PyInt_AS_LONG(v);
1308 b = PyInt_AS_LONG(w);
1309 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001310 if ((i^a) < 0 && (i^b) < 0)
1311 goto slow_iadd;
1312 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001313 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001314 else if (PyString_CheckExact(v) &&
1315 PyString_CheckExact(w)) {
1316 x = string_concatenate(v, w, f, next_instr);
1317 /* string_concatenate consumed the ref to v */
1318 goto skip_decref_v;
1319 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001320 else {
1321 slow_iadd:
Thomas Wouters434d0822000-08-24 20:11:32 +00001322 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001323 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001324 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001325 skip_decref_v:
Thomas Wouters434d0822000-08-24 20:11:32 +00001326 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001327 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001328 if (x != NULL) continue;
1329 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001330
Thomas Wouters434d0822000-08-24 20:11:32 +00001331 case INPLACE_SUBTRACT:
1332 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001333 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001334 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001335 /* INLINE: int - int */
1336 register long a, b, i;
1337 a = PyInt_AS_LONG(v);
1338 b = PyInt_AS_LONG(w);
1339 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001340 if ((i^a) < 0 && (i^~b) < 0)
1341 goto slow_isub;
1342 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001343 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001344 else {
1345 slow_isub:
Thomas Wouters434d0822000-08-24 20:11:32 +00001346 x = PyNumber_InPlaceSubtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001347 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001348 Py_DECREF(v);
1349 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001350 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001351 if (x != NULL) continue;
1352 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001353
Thomas Wouters434d0822000-08-24 20:11:32 +00001354 case INPLACE_LSHIFT:
1355 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001356 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001357 x = PyNumber_InPlaceLshift(v, w);
1358 Py_DECREF(v);
1359 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001360 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001361 if (x != NULL) continue;
1362 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001363
Thomas Wouters434d0822000-08-24 20:11:32 +00001364 case INPLACE_RSHIFT:
1365 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001366 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001367 x = PyNumber_InPlaceRshift(v, w);
1368 Py_DECREF(v);
1369 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001370 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001371 if (x != NULL) continue;
1372 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001373
Thomas Wouters434d0822000-08-24 20:11:32 +00001374 case INPLACE_AND:
1375 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001376 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001377 x = PyNumber_InPlaceAnd(v, w);
1378 Py_DECREF(v);
1379 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001380 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001381 if (x != NULL) continue;
1382 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001383
Thomas Wouters434d0822000-08-24 20:11:32 +00001384 case INPLACE_XOR:
1385 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001386 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001387 x = PyNumber_InPlaceXor(v, w);
1388 Py_DECREF(v);
1389 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001390 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001391 if (x != NULL) continue;
1392 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001393
Thomas Wouters434d0822000-08-24 20:11:32 +00001394 case INPLACE_OR:
1395 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001396 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001397 x = PyNumber_InPlaceOr(v, w);
1398 Py_DECREF(v);
1399 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001400 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001401 if (x != NULL) continue;
1402 break;
1403
Guido van Rossum374a9221991-04-04 10:40:29 +00001404 case SLICE+0:
1405 case SLICE+1:
1406 case SLICE+2:
1407 case SLICE+3:
1408 if ((opcode-SLICE) & 2)
1409 w = POP();
1410 else
1411 w = NULL;
1412 if ((opcode-SLICE) & 1)
1413 v = POP();
1414 else
1415 v = NULL;
Raymond Hettinger663004b2003-01-09 15:24:30 +00001416 u = TOP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001417 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001418 Py_DECREF(u);
1419 Py_XDECREF(v);
1420 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001421 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001422 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001423 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001424
Guido van Rossum374a9221991-04-04 10:40:29 +00001425 case STORE_SLICE+0:
1426 case STORE_SLICE+1:
1427 case STORE_SLICE+2:
1428 case STORE_SLICE+3:
1429 if ((opcode-STORE_SLICE) & 2)
1430 w = POP();
1431 else
1432 w = NULL;
1433 if ((opcode-STORE_SLICE) & 1)
1434 v = POP();
1435 else
1436 v = NULL;
1437 u = POP();
1438 t = POP();
1439 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +00001440 Py_DECREF(t);
1441 Py_DECREF(u);
1442 Py_XDECREF(v);
1443 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001444 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001445 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001446
Guido van Rossum374a9221991-04-04 10:40:29 +00001447 case DELETE_SLICE+0:
1448 case DELETE_SLICE+1:
1449 case DELETE_SLICE+2:
1450 case DELETE_SLICE+3:
1451 if ((opcode-DELETE_SLICE) & 2)
1452 w = POP();
1453 else
1454 w = NULL;
1455 if ((opcode-DELETE_SLICE) & 1)
1456 v = POP();
1457 else
1458 v = NULL;
1459 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001460 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +00001461 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00001462 Py_DECREF(u);
1463 Py_XDECREF(v);
1464 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001465 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001466 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001467
Guido van Rossum374a9221991-04-04 10:40:29 +00001468 case STORE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001469 w = TOP();
1470 v = SECOND();
1471 u = THIRD();
1472 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001473 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001474 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001475 Py_DECREF(u);
1476 Py_DECREF(v);
1477 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001478 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001479 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001480
Guido van Rossum374a9221991-04-04 10:40:29 +00001481 case DELETE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001482 w = TOP();
1483 v = SECOND();
1484 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001485 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001486 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001487 Py_DECREF(v);
1488 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001489 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001490 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001491
Guido van Rossum374a9221991-04-04 10:40:29 +00001492 case PRINT_EXPR:
1493 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001494 w = PySys_GetObject("displayhook");
1495 if (w == NULL) {
1496 PyErr_SetString(PyExc_RuntimeError,
1497 "lost sys.displayhook");
1498 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001499 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001500 }
1501 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001502 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001503 if (x == NULL)
1504 err = -1;
1505 }
1506 if (err == 0) {
1507 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001508 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001509 if (w == NULL)
1510 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001511 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001512 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001513 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001514 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001515
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001516 case PRINT_ITEM_TO:
1517 w = stream = POP();
1518 /* fall through to PRINT_ITEM */
1519
Guido van Rossum374a9221991-04-04 10:40:29 +00001520 case PRINT_ITEM:
1521 v = POP();
Barry Warsaw093abe02000-08-29 04:56:13 +00001522 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001523 w = PySys_GetObject("stdout");
1524 if (w == NULL) {
1525 PyErr_SetString(PyExc_RuntimeError,
1526 "lost sys.stdout");
1527 err = -1;
1528 }
Guido van Rossum8f183201997-12-31 05:53:15 +00001529 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001530 /* PyFile_SoftSpace() can exececute arbitrary code
1531 if sys.stdout is an instance with a __getattr__.
1532 If __getattr__ raises an exception, w will
1533 be freed, so we need to prevent that temporarily. */
1534 Py_XINCREF(w);
Tim Peters8e5fd532002-03-24 19:25:00 +00001535 if (w != NULL && PyFile_SoftSpace(w, 0))
Guido van Rossumbe270261997-05-22 22:26:18 +00001536 err = PyFile_WriteString(" ", w);
1537 if (err == 0)
1538 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001539 if (err == 0) {
Tim Peters8e5fd532002-03-24 19:25:00 +00001540 /* XXX move into writeobject() ? */
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001541 if (PyString_Check(v)) {
1542 char *s = PyString_AS_STRING(v);
1543 int len = PyString_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001544 if (len == 0 ||
1545 !isspace(Py_CHARMASK(s[len-1])) ||
1546 s[len-1] == ' ')
1547 PyFile_SoftSpace(w, 1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001548 }
Martin v. Löwis8d3ce5a2001-12-18 22:36:40 +00001549#ifdef Py_USING_UNICODE
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001550 else if (PyUnicode_Check(v)) {
1551 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
1552 int len = PyUnicode_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001553 if (len == 0 ||
1554 !Py_UNICODE_ISSPACE(s[len-1]) ||
1555 s[len-1] == ' ')
1556 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001557 }
Michael W. Hudsond95c8282002-05-20 13:56:11 +00001558#endif
Tim Peters8e5fd532002-03-24 19:25:00 +00001559 else
1560 PyFile_SoftSpace(w, 1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001561 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001562 Py_XDECREF(w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001563 Py_DECREF(v);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001564 Py_XDECREF(stream);
1565 stream = NULL;
1566 if (err == 0)
1567 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001568 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001569
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001570 case PRINT_NEWLINE_TO:
1571 w = stream = POP();
1572 /* fall through to PRINT_NEWLINE */
1573
Guido van Rossum374a9221991-04-04 10:40:29 +00001574 case PRINT_NEWLINE:
Barry Warsaw093abe02000-08-29 04:56:13 +00001575 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001576 w = PySys_GetObject("stdout");
1577 if (w == NULL)
1578 PyErr_SetString(PyExc_RuntimeError,
1579 "lost sys.stdout");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001580 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001581 if (w != NULL) {
1582 err = PyFile_WriteString("\n", w);
1583 if (err == 0)
1584 PyFile_SoftSpace(w, 0);
1585 }
1586 Py_XDECREF(stream);
1587 stream = NULL;
Guido van Rossum374a9221991-04-04 10:40:29 +00001588 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001589
Thomas Wouters434d0822000-08-24 20:11:32 +00001590
1591#ifdef CASE_TOO_BIG
1592 default: switch (opcode) {
1593#endif
Guido van Rossumf10570b1995-07-07 22:53:21 +00001594 case RAISE_VARARGS:
1595 u = v = w = NULL;
1596 switch (oparg) {
1597 case 3:
1598 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001599 /* Fallthrough */
1600 case 2:
1601 v = POP(); /* value */
1602 /* Fallthrough */
1603 case 1:
1604 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001605 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001606 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001607 break;
1608 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001609 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001610 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001611 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001612 break;
1613 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001614 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001615
Guido van Rossum374a9221991-04-04 10:40:29 +00001616 case LOAD_LOCALS:
Raymond Hettinger467a6982004-04-07 11:39:21 +00001617 if ((x = f->f_locals) != NULL) {
1618 Py_INCREF(x);
1619 PUSH(x);
1620 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001621 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001622 PyErr_SetString(PyExc_SystemError, "no locals");
Guido van Rossum374a9221991-04-04 10:40:29 +00001623 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001624
Guido van Rossum374a9221991-04-04 10:40:29 +00001625 case RETURN_VALUE:
1626 retval = POP();
1627 why = WHY_RETURN;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001628 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001629
Tim Peters5ca576e2001-06-18 22:08:13 +00001630 case YIELD_VALUE:
1631 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001632 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001633 why = WHY_YIELD;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001634 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001635
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001636 case EXEC_STMT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001637 w = TOP();
1638 v = SECOND();
1639 u = THIRD();
1640 STACKADJ(-3);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001641 rdtscll(intr0);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001642 err = exec_statement(f, u, v, w);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001643 rdtscll(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00001644 Py_DECREF(u);
1645 Py_DECREF(v);
1646 Py_DECREF(w);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001647 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001648
Guido van Rossum374a9221991-04-04 10:40:29 +00001649 case POP_BLOCK:
1650 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001651 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001652 while (STACK_LEVEL() > b->b_level) {
1653 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001654 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001655 }
1656 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001657 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001658
Guido van Rossum374a9221991-04-04 10:40:29 +00001659 case END_FINALLY:
1660 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001661 if (PyInt_Check(v)) {
Raymond Hettinger7c958652004-04-06 10:11:10 +00001662 why = (enum why_code) PyInt_AS_LONG(v);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001663 assert(why != WHY_YIELD);
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00001664 if (why == WHY_RETURN ||
1665 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001666 retval = POP();
1667 }
Raymond Hettingerd3b836d2004-04-07 13:17:27 +00001668 else if (PyClass_Check(v) || PyString_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001669 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001670 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001671 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001672 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001673 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001674 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001675 else if (v != Py_None) {
1676 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001677 "'finally' pops bad exception");
1678 why = WHY_EXCEPTION;
1679 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001680 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001681 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001682
Guido van Rossum374a9221991-04-04 10:40:29 +00001683 case BUILD_CLASS:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001684 u = TOP();
1685 v = SECOND();
1686 w = THIRD();
1687 STACKADJ(-2);
Guido van Rossum25831651993-05-19 14:50:45 +00001688 x = build_class(u, v, w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001689 SET_TOP(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001690 Py_DECREF(u);
1691 Py_DECREF(v);
1692 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001693 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001694
Guido van Rossum374a9221991-04-04 10:40:29 +00001695 case STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001696 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001697 v = POP();
Raymond Hettinger467a6982004-04-07 11:39:21 +00001698 if ((x = f->f_locals) != NULL) {
Raymond Hettinger66bd2332004-08-02 08:30:07 +00001699 if (PyDict_CheckExact(x))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001700 err = PyDict_SetItem(x, w, v);
1701 else
1702 err = PyObject_SetItem(x, w, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001703 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001704 if (err == 0) continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001705 break;
1706 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001707 PyErr_Format(PyExc_SystemError,
1708 "no locals found when storing %s",
1709 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001710 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001711
Guido van Rossum374a9221991-04-04 10:40:29 +00001712 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001713 w = GETITEM(names, oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001714 if ((x = f->f_locals) != NULL) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001715 if ((err = PyObject_DelItem(x, w)) != 0)
Raymond Hettinger467a6982004-04-07 11:39:21 +00001716 format_exc_check_arg(PyExc_NameError,
1717 NAME_ERROR_MSG ,w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001718 break;
1719 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001720 PyErr_Format(PyExc_SystemError,
1721 "no locals when deleting %s",
1722 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001723 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001724
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001725 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001726 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001727 v = POP();
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001728 if (PyTuple_CheckExact(v) && PyTuple_GET_SIZE(v) == oparg) {
1729 PyObject **items = ((PyTupleObject *)v)->ob_item;
1730 while (oparg--) {
1731 w = items[oparg];
1732 Py_INCREF(w);
1733 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001734 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001735 Py_DECREF(v);
1736 continue;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001737 } else if (PyList_CheckExact(v) && PyList_GET_SIZE(v) == oparg) {
1738 PyObject **items = ((PyListObject *)v)->ob_item;
1739 while (oparg--) {
1740 w = items[oparg];
1741 Py_INCREF(w);
1742 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001743 }
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001744 } else if (unpack_iterable(v, oparg,
Tim Petersd6d010b2001-06-21 02:49:55 +00001745 stack_pointer + oparg))
1746 stack_pointer += oparg;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001747 else {
1748 if (PyErr_ExceptionMatches(PyExc_TypeError))
1749 PyErr_SetString(PyExc_TypeError,
1750 "unpack non-sequence");
Barry Warsawe42b18f1997-08-25 22:13:04 +00001751 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001752 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001753 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001754 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001755
Guido van Rossum374a9221991-04-04 10:40:29 +00001756 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001757 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001758 v = TOP();
1759 u = SECOND();
1760 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001761 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1762 Py_DECREF(v);
1763 Py_DECREF(u);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001764 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001765 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001766
Guido van Rossum374a9221991-04-04 10:40:29 +00001767 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001768 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001769 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001770 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1771 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001772 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001773 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001774
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001775 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001776 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001777 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001778 err = PyDict_SetItem(f->f_globals, w, v);
1779 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001780 if (err == 0) continue;
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001781 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001782
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001783 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001784 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001785 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001786 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001787 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001788 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001789
Guido van Rossum374a9221991-04-04 10:40:29 +00001790 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001791 w = GETITEM(names, oparg);
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001792 if ((v = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001793 PyErr_Format(PyExc_SystemError,
1794 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001795 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001796 break;
1797 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001798 if (PyDict_CheckExact(v)) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001799 x = PyDict_GetItem(v, w);
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001800 Py_XINCREF(x);
1801 }
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001802 else {
1803 x = PyObject_GetItem(v, w);
1804 if (x == NULL && PyErr_Occurred()) {
1805 if (!PyErr_ExceptionMatches(PyExc_KeyError))
1806 break;
1807 PyErr_Clear();
1808 }
1809 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001810 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001811 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001812 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001813 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001814 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001815 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001816 PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001817 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001818 break;
1819 }
1820 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001821 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001822 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001823 PUSH(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001824 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001825
Guido van Rossum374a9221991-04-04 10:40:29 +00001826 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001827 w = GETITEM(names, oparg);
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001828 if (PyString_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00001829 /* Inline the PyDict_GetItem() calls.
1830 WARNING: this is an extreme speed hack.
1831 Do not try this at home. */
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001832 long hash = ((PyStringObject *)w)->ob_shash;
1833 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001834 PyDictObject *d;
1835 d = (PyDictObject *)(f->f_globals);
1836 x = d->ma_lookup(d, w, hash)->me_value;
1837 if (x != NULL) {
1838 Py_INCREF(x);
1839 PUSH(x);
1840 continue;
1841 }
1842 d = (PyDictObject *)(f->f_builtins);
1843 x = d->ma_lookup(d, w, hash)->me_value;
1844 if (x != NULL) {
1845 Py_INCREF(x);
1846 PUSH(x);
1847 continue;
1848 }
1849 goto load_global_error;
1850 }
1851 }
1852 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00001853 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001854 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001855 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001856 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001857 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00001858 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001859 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001860 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001861 break;
1862 }
1863 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001864 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001865 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001866 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001867
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001868 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001869 x = GETLOCAL(oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001870 if (x != NULL) {
1871 SETLOCAL(oparg, NULL);
1872 continue;
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001873 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001874 format_exc_check_arg(
1875 PyExc_UnboundLocalError,
1876 UNBOUNDLOCAL_ERROR_MSG,
1877 PyTuple_GetItem(co->co_varnames, oparg)
1878 );
1879 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001880
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001881 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001882 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001883 Py_INCREF(x);
1884 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001885 if (x != NULL) continue;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001886 break;
1887
1888 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001889 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001890 w = PyCell_Get(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001891 if (w != NULL) {
1892 PUSH(w);
1893 continue;
Jeremy Hylton2524d692001-02-05 17:23:16 +00001894 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001895 err = -1;
1896 /* Don't stomp existing exception */
1897 if (PyErr_Occurred())
1898 break;
1899 if (oparg < f->f_ncells) {
1900 v = PyTuple_GetItem(co->co_cellvars,
1901 oparg);
1902 format_exc_check_arg(
1903 PyExc_UnboundLocalError,
1904 UNBOUNDLOCAL_ERROR_MSG,
1905 v);
1906 } else {
1907 v = PyTuple_GetItem(
1908 co->co_freevars,
1909 oparg - f->f_ncells);
1910 format_exc_check_arg(
1911 PyExc_NameError,
1912 UNBOUNDFREE_ERROR_MSG,
1913 v);
1914 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001915 break;
1916
1917 case STORE_DEREF:
1918 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001919 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001920 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00001921 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001922 continue;
1923
Guido van Rossum374a9221991-04-04 10:40:29 +00001924 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001925 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001926 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001927 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001928 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001929 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001930 }
1931 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001932 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001933 }
1934 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001935
Guido van Rossum374a9221991-04-04 10:40:29 +00001936 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00001937 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001938 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001939 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001940 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00001941 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001942 }
1943 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001944 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001945 }
1946 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001947
Guido van Rossum374a9221991-04-04 10:40:29 +00001948 case BUILD_MAP:
Guido van Rossumb209a111997-04-29 18:18:01 +00001949 x = PyDict_New();
Guido van Rossum374a9221991-04-04 10:40:29 +00001950 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001951 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001952 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001953
Guido van Rossum374a9221991-04-04 10:40:29 +00001954 case LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001955 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001956 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001957 x = PyObject_GetAttr(v, w);
1958 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001959 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001960 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001961 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001962
Guido van Rossum374a9221991-04-04 10:40:29 +00001963 case COMPARE_OP:
1964 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001965 v = TOP();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001966 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001967 /* INLINE: cmp(int, int) */
1968 register long a, b;
1969 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001970 a = PyInt_AS_LONG(v);
1971 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001972 switch (oparg) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00001973 case PyCmp_LT: res = a < b; break;
1974 case PyCmp_LE: res = a <= b; break;
1975 case PyCmp_EQ: res = a == b; break;
1976 case PyCmp_NE: res = a != b; break;
1977 case PyCmp_GT: res = a > b; break;
1978 case PyCmp_GE: res = a >= b; break;
1979 case PyCmp_IS: res = v == w; break;
1980 case PyCmp_IS_NOT: res = v != w; break;
Guido van Rossumc12da691997-07-17 23:12:42 +00001981 default: goto slow_compare;
1982 }
1983 x = res ? Py_True : Py_False;
1984 Py_INCREF(x);
1985 }
1986 else {
1987 slow_compare:
1988 x = cmp_outcome(oparg, v, w);
1989 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001990 Py_DECREF(v);
1991 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001992 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001993 if (x == NULL) break;
1994 PREDICT(JUMP_IF_FALSE);
1995 PREDICT(JUMP_IF_TRUE);
1996 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001997
Guido van Rossum374a9221991-04-04 10:40:29 +00001998 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001999 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002000 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002001 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002002 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00002003 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002004 break;
2005 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00002006 u = TOP();
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002007 w = PyTuple_Pack(4,
Guido van Rossum681d79a1995-07-18 14:51:37 +00002008 w,
2009 f->f_globals,
Guido van Rossuma027efa1997-05-05 20:56:21 +00002010 f->f_locals == NULL ?
2011 Py_None : f->f_locals,
Guido van Rossum681d79a1995-07-18 14:51:37 +00002012 u);
Guido van Rossumb209a111997-04-29 18:18:01 +00002013 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002014 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002015 u = POP();
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002016 x = NULL;
2017 break;
2018 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002019 rdtscll(intr0);
Guido van Rossumb209a111997-04-29 18:18:01 +00002020 x = PyEval_CallObject(x, w);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002021 rdtscll(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002022 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002023 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002024 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002025 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002026
Thomas Wouters52152252000-08-17 22:55:00 +00002027 case IMPORT_STAR:
2028 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002029 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002030 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002031 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00002032 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002033 break;
2034 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002035 rdtscll(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002036 err = import_all_from(x, v);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002037 rdtscll(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002038 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00002039 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002040 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002041 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002042
Thomas Wouters52152252000-08-17 22:55:00 +00002043 case IMPORT_FROM:
Skip Montanaro496e6582002-08-06 17:47:40 +00002044 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00002045 v = TOP();
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002046 rdtscll(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002047 x = import_from(v, w);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002048 rdtscll(intr1);
Thomas Wouters52152252000-08-17 22:55:00 +00002049 PUSH(x);
2050 if (x != NULL) continue;
2051 break;
2052
Guido van Rossum374a9221991-04-04 10:40:29 +00002053 case JUMP_FORWARD:
2054 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002055 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002056
Raymond Hettingerf606f872003-03-16 03:11:04 +00002057 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002058 case JUMP_IF_FALSE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002059 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002060 if (w == Py_True) {
2061 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002062 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002063 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002064 if (w == Py_False) {
2065 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002066 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002067 }
2068 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002069 if (err > 0)
2070 err = 0;
2071 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00002072 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002073 else
2074 break;
2075 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002076
Raymond Hettingerf606f872003-03-16 03:11:04 +00002077 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002078 case JUMP_IF_TRUE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002079 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002080 if (w == Py_False) {
2081 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002082 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002083 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002084 if (w == Py_True) {
2085 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002086 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002087 }
2088 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002089 if (err > 0) {
2090 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00002091 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002092 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002093 else if (err == 0)
2094 ;
2095 else
2096 break;
2097 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002098
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00002099 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002100 case JUMP_ABSOLUTE:
2101 JUMPTO(oparg);
Neil Schemenauerca2a2f12003-05-30 23:59:44 +00002102 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002103
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002104 case GET_ITER:
2105 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002106 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002107 x = PyObject_GetIter(v);
2108 Py_DECREF(v);
2109 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002110 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002111 PREDICT(FOR_ITER);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002112 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002113 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002114 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002115 break;
2116
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002117 PREDICTED_WITH_ARG(FOR_ITER);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002118 case FOR_ITER:
2119 /* before: [iter]; after: [iter, iter()] *or* [] */
2120 v = TOP();
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002121 x = (*v->ob_type->tp_iternext)(v);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002122 if (x != NULL) {
2123 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002124 PREDICT(STORE_FAST);
2125 PREDICT(UNPACK_SEQUENCE);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002126 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002127 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002128 if (PyErr_Occurred()) {
2129 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2130 break;
2131 PyErr_Clear();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002132 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002133 /* iterator ended normally */
2134 x = v = POP();
2135 Py_DECREF(v);
2136 JUMPBY(oparg);
2137 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002138
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002139 case BREAK_LOOP:
2140 why = WHY_BREAK;
2141 goto fast_block_end;
2142
2143 case CONTINUE_LOOP:
2144 retval = PyInt_FromLong(oparg);
2145 why = WHY_CONTINUE;
2146 goto fast_block_end;
2147
Guido van Rossum374a9221991-04-04 10:40:29 +00002148 case SETUP_LOOP:
2149 case SETUP_EXCEPT:
2150 case SETUP_FINALLY:
Guido van Rossumb209a111997-04-29 18:18:01 +00002151 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002152 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002153 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002154
Guido van Rossumf10570b1995-07-07 22:53:21 +00002155 case CALL_FUNCTION:
Armin Rigo8817fcd2004-06-17 10:22:40 +00002156 {
2157 PyObject **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002158 PCALL(PCALL_ALL);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002159 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002160#ifdef WITH_TSC
Armin Rigo8817fcd2004-06-17 10:22:40 +00002161 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002162#else
Armin Rigo8817fcd2004-06-17 10:22:40 +00002163 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002164#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002165 stack_pointer = sp;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002166 PUSH(x);
2167 if (x != NULL)
2168 continue;
2169 break;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002170 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002171
Jeremy Hylton76901512000-03-28 23:49:17 +00002172 case CALL_FUNCTION_VAR:
2173 case CALL_FUNCTION_KW:
2174 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002175 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002176 int na = oparg & 0xff;
2177 int nk = (oparg>>8) & 0xff;
2178 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002179 int n = na + 2 * nk;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002180 PyObject **pfunc, *func, **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002181 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002182 if (flags & CALL_FLAG_VAR)
2183 n++;
2184 if (flags & CALL_FLAG_KW)
2185 n++;
2186 pfunc = stack_pointer - n - 1;
2187 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002188
Guido van Rossumac7be682001-01-17 15:42:30 +00002189 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002190 && PyMethod_GET_SELF(func) != NULL) {
2191 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002192 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002193 func = PyMethod_GET_FUNCTION(func);
2194 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002195 Py_DECREF(*pfunc);
2196 *pfunc = self;
2197 na++;
2198 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002199 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002200 Py_INCREF(func);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002201 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002202 rdtscll(intr0);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002203 x = ext_do_call(func, &sp, flags, na, nk);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002204 rdtscll(intr1);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002205 stack_pointer = sp;
Jeremy Hylton76901512000-03-28 23:49:17 +00002206 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002207
Jeremy Hylton76901512000-03-28 23:49:17 +00002208 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002209 w = POP();
2210 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002211 }
2212 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002213 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002214 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002215 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002216 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002217
Guido van Rossum681d79a1995-07-18 14:51:37 +00002218 case MAKE_FUNCTION:
2219 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002220 x = PyFunction_New(v, f->f_globals);
2221 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002222 /* XXX Maybe this should be a separate opcode? */
2223 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002224 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002225 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002226 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002227 x = NULL;
2228 break;
2229 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002230 while (--oparg >= 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002231 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002232 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002233 }
2234 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002235 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002236 }
2237 PUSH(x);
2238 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002239
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002240 case MAKE_CLOSURE:
2241 {
2242 int nfree;
2243 v = POP(); /* code object */
2244 x = PyFunction_New(v, f->f_globals);
Jeremy Hylton733c8932001-12-13 19:51:56 +00002245 nfree = PyCode_GetNumFree((PyCodeObject *)v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002246 Py_DECREF(v);
2247 /* XXX Maybe this should be a separate opcode? */
2248 if (x != NULL && nfree > 0) {
2249 v = PyTuple_New(nfree);
2250 if (v == NULL) {
2251 Py_DECREF(x);
2252 x = NULL;
2253 break;
2254 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002255 while (--nfree >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002256 w = POP();
2257 PyTuple_SET_ITEM(v, nfree, w);
2258 }
2259 err = PyFunction_SetClosure(x, v);
2260 Py_DECREF(v);
2261 }
2262 if (x != NULL && oparg > 0) {
2263 v = PyTuple_New(oparg);
2264 if (v == NULL) {
2265 Py_DECREF(x);
2266 x = NULL;
2267 break;
2268 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002269 while (--oparg >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002270 w = POP();
2271 PyTuple_SET_ITEM(v, oparg, w);
2272 }
2273 err = PyFunction_SetDefaults(x, v);
2274 Py_DECREF(v);
2275 }
2276 PUSH(x);
2277 break;
2278 }
2279
Guido van Rossum8861b741996-07-30 16:49:37 +00002280 case BUILD_SLICE:
2281 if (oparg == 3)
2282 w = POP();
2283 else
2284 w = NULL;
2285 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002286 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002287 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002288 Py_DECREF(u);
2289 Py_DECREF(v);
2290 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002291 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002292 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002293 break;
2294
Fred Drakeef8ace32000-08-24 00:32:09 +00002295 case EXTENDED_ARG:
2296 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002297 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002298 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002299
Guido van Rossum374a9221991-04-04 10:40:29 +00002300 default:
2301 fprintf(stderr,
2302 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002303 PyCode_Addr2Line(f->f_code, f->f_lasti),
2304 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002305 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002306 why = WHY_EXCEPTION;
2307 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002308
2309#ifdef CASE_TOO_BIG
2310 }
2311#endif
2312
Guido van Rossum374a9221991-04-04 10:40:29 +00002313 } /* switch */
2314
2315 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002316
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002317 rdtscll(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002318
Guido van Rossum374a9221991-04-04 10:40:29 +00002319 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002320
Guido van Rossum374a9221991-04-04 10:40:29 +00002321 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002322 if (err == 0 && x != NULL) {
2323#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002324 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002325 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002326 fprintf(stderr,
2327 "XXX undetected error\n");
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002328 else {
2329#endif
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002330 rdtscll(loop1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002331 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002332#ifdef CHECKEXC
2333 }
2334#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +00002335 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002336 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002337 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002338 err = 0;
2339 }
2340
Guido van Rossum374a9221991-04-04 10:40:29 +00002341 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002342
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002343 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002344 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002345 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002346 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002347 why = WHY_EXCEPTION;
2348 }
2349 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002350#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002351 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002352 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002353 if (PyErr_Occurred()) {
Jeremy Hylton904ed862003-11-05 17:29:35 +00002354 char buf[1024];
2355 sprintf(buf, "Stack unwind with exception "
2356 "set and why=%d", why);
2357 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002358 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002359 }
2360#endif
2361
2362 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002363
Guido van Rossum374a9221991-04-04 10:40:29 +00002364 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002365 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002366
Fred Drake8f51f542001-10-04 14:48:42 +00002367 if (tstate->c_tracefunc != NULL)
2368 call_exc_trace(tstate->c_tracefunc,
2369 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002370 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002371
Guido van Rossum374a9221991-04-04 10:40:29 +00002372 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002373
Guido van Rossum374a9221991-04-04 10:40:29 +00002374 if (why == WHY_RERAISE)
2375 why = WHY_EXCEPTION;
2376
2377 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002378
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002379fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002380 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002381 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002382
Tim Peters8a5c3c72004-04-05 19:36:21 +00002383 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002384 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2385 /* For a continue inside a try block,
2386 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002387 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2388 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002389 why = WHY_NOT;
2390 JUMPTO(PyInt_AS_LONG(retval));
2391 Py_DECREF(retval);
2392 break;
2393 }
2394
Guido van Rossum374a9221991-04-04 10:40:29 +00002395 while (STACK_LEVEL() > b->b_level) {
2396 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002397 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002398 }
2399 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2400 why = WHY_NOT;
2401 JUMPTO(b->b_handler);
2402 break;
2403 }
2404 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002405 (b->b_type == SETUP_EXCEPT &&
2406 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002407 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002408 PyObject *exc, *val, *tb;
2409 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002410 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002411 val = Py_None;
2412 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002413 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002414 /* Make the raw exception data
2415 available to the handler,
2416 so a program can emulate the
2417 Python main loop. Don't do
2418 this for 'finally'. */
2419 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002420 PyErr_NormalizeException(
2421 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002422 set_exc_info(tstate,
2423 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002424 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002425 if (tb == NULL) {
2426 Py_INCREF(Py_None);
2427 PUSH(Py_None);
2428 } else
2429 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002430 PUSH(val);
2431 PUSH(exc);
2432 }
2433 else {
Raymond Hettinger06032cb2004-04-06 09:37:35 +00002434 if (why & (WHY_RETURN | WHY_CONTINUE))
Guido van Rossum374a9221991-04-04 10:40:29 +00002435 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002436 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002437 PUSH(v);
2438 }
2439 why = WHY_NOT;
2440 JUMPTO(b->b_handler);
2441 break;
2442 }
2443 } /* unwind stack */
2444
2445 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002446
Guido van Rossum374a9221991-04-04 10:40:29 +00002447 if (why != WHY_NOT)
2448 break;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002449 rdtscll(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002450
Guido van Rossum374a9221991-04-04 10:40:29 +00002451 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002452
Tim Peters8a5c3c72004-04-05 19:36:21 +00002453 assert(why != WHY_YIELD);
2454 /* Pop remaining stack entries. */
2455 while (!EMPTY()) {
2456 v = POP();
2457 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002458 }
2459
Tim Peters8a5c3c72004-04-05 19:36:21 +00002460 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002461 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002462
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002463fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002464 if (tstate->use_tracing) {
2465 if (tstate->c_tracefunc
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002466 && (why == WHY_RETURN || why == WHY_YIELD)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002467 if (call_trace(tstate->c_tracefunc,
2468 tstate->c_traceobj, f,
2469 PyTrace_RETURN, retval)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002470 Py_XDECREF(retval);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002471 retval = NULL;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002472 why = WHY_EXCEPTION;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002473 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002474 }
Fred Drake8f51f542001-10-04 14:48:42 +00002475 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002476 if (why == WHY_EXCEPTION)
2477 call_trace_protected(tstate->c_profilefunc,
2478 tstate->c_profileobj, f,
2479 PyTrace_RETURN);
2480 else if (call_trace(tstate->c_profilefunc,
2481 tstate->c_profileobj, f,
2482 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002483 Py_XDECREF(retval);
2484 retval = NULL;
2485 why = WHY_EXCEPTION;
2486 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002487 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002488 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002489
Guido van Rossuma027efa1997-05-05 20:56:21 +00002490 reset_exc_info(tstate);
2491
Tim Peters5ca576e2001-06-18 22:08:13 +00002492 /* pop frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +00002493 exit_eval_frame:
2494 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002495 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002496
Guido van Rossum96a42c81992-01-12 02:29:51 +00002497 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002498}
2499
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002500/* this is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002501 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Skip Montanaroc9a47622005-01-08 21:58:58 +00002502 the test in the if statement in Misc/gdbinit:pystack* */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002503
Tim Peters6d6c1a32001-08-02 04:15:00 +00002504PyObject *
2505PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002506 PyObject **args, int argcount, PyObject **kws, int kwcount,
2507 PyObject **defs, int defcount, PyObject *closure)
2508{
2509 register PyFrameObject *f;
2510 register PyObject *retval = NULL;
2511 register PyObject **fastlocals, **freevars;
2512 PyThreadState *tstate = PyThreadState_GET();
2513 PyObject *x, *u;
2514
2515 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00002516 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002517 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002518 return NULL;
2519 }
2520
Jeremy Hylton985eba52003-02-05 23:13:00 +00002521 assert(globals != NULL);
2522 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002523 if (f == NULL)
2524 return NULL;
2525
2526 fastlocals = f->f_localsplus;
2527 freevars = f->f_localsplus + f->f_nlocals;
2528
2529 if (co->co_argcount > 0 ||
2530 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2531 int i;
2532 int n = argcount;
2533 PyObject *kwdict = NULL;
2534 if (co->co_flags & CO_VARKEYWORDS) {
2535 kwdict = PyDict_New();
2536 if (kwdict == NULL)
2537 goto fail;
2538 i = co->co_argcount;
2539 if (co->co_flags & CO_VARARGS)
2540 i++;
2541 SETLOCAL(i, kwdict);
2542 }
2543 if (argcount > co->co_argcount) {
2544 if (!(co->co_flags & CO_VARARGS)) {
2545 PyErr_Format(PyExc_TypeError,
2546 "%.200s() takes %s %d "
2547 "%sargument%s (%d given)",
2548 PyString_AsString(co->co_name),
2549 defcount ? "at most" : "exactly",
2550 co->co_argcount,
2551 kwcount ? "non-keyword " : "",
2552 co->co_argcount == 1 ? "" : "s",
2553 argcount);
2554 goto fail;
2555 }
2556 n = co->co_argcount;
2557 }
2558 for (i = 0; i < n; i++) {
2559 x = args[i];
2560 Py_INCREF(x);
2561 SETLOCAL(i, x);
2562 }
2563 if (co->co_flags & CO_VARARGS) {
2564 u = PyTuple_New(argcount - n);
2565 if (u == NULL)
2566 goto fail;
2567 SETLOCAL(co->co_argcount, u);
2568 for (i = n; i < argcount; i++) {
2569 x = args[i];
2570 Py_INCREF(x);
2571 PyTuple_SET_ITEM(u, i-n, x);
2572 }
2573 }
2574 for (i = 0; i < kwcount; i++) {
2575 PyObject *keyword = kws[2*i];
2576 PyObject *value = kws[2*i + 1];
2577 int j;
2578 if (keyword == NULL || !PyString_Check(keyword)) {
2579 PyErr_Format(PyExc_TypeError,
2580 "%.200s() keywords must be strings",
2581 PyString_AsString(co->co_name));
2582 goto fail;
2583 }
2584 /* XXX slow -- speed up using dictionary? */
2585 for (j = 0; j < co->co_argcount; j++) {
2586 PyObject *nm = PyTuple_GET_ITEM(
2587 co->co_varnames, j);
2588 int cmp = PyObject_RichCompareBool(
2589 keyword, nm, Py_EQ);
2590 if (cmp > 0)
2591 break;
2592 else if (cmp < 0)
2593 goto fail;
2594 }
2595 /* Check errors from Compare */
2596 if (PyErr_Occurred())
2597 goto fail;
2598 if (j >= co->co_argcount) {
2599 if (kwdict == NULL) {
2600 PyErr_Format(PyExc_TypeError,
2601 "%.200s() got an unexpected "
2602 "keyword argument '%.400s'",
2603 PyString_AsString(co->co_name),
2604 PyString_AsString(keyword));
2605 goto fail;
2606 }
2607 PyDict_SetItem(kwdict, keyword, value);
2608 }
2609 else {
2610 if (GETLOCAL(j) != NULL) {
2611 PyErr_Format(PyExc_TypeError,
2612 "%.200s() got multiple "
2613 "values for keyword "
2614 "argument '%.400s'",
2615 PyString_AsString(co->co_name),
2616 PyString_AsString(keyword));
2617 goto fail;
2618 }
2619 Py_INCREF(value);
2620 SETLOCAL(j, value);
2621 }
2622 }
2623 if (argcount < co->co_argcount) {
2624 int m = co->co_argcount - defcount;
2625 for (i = argcount; i < m; i++) {
2626 if (GETLOCAL(i) == NULL) {
2627 PyErr_Format(PyExc_TypeError,
2628 "%.200s() takes %s %d "
2629 "%sargument%s (%d given)",
2630 PyString_AsString(co->co_name),
2631 ((co->co_flags & CO_VARARGS) ||
2632 defcount) ? "at least"
2633 : "exactly",
2634 m, kwcount ? "non-keyword " : "",
2635 m == 1 ? "" : "s", i);
2636 goto fail;
2637 }
2638 }
2639 if (n > m)
2640 i = n - m;
2641 else
2642 i = 0;
2643 for (; i < defcount; i++) {
2644 if (GETLOCAL(m+i) == NULL) {
2645 PyObject *def = defs[i];
2646 Py_INCREF(def);
2647 SETLOCAL(m+i, def);
2648 }
2649 }
2650 }
2651 }
2652 else {
2653 if (argcount > 0 || kwcount > 0) {
2654 PyErr_Format(PyExc_TypeError,
2655 "%.200s() takes no arguments (%d given)",
2656 PyString_AsString(co->co_name),
2657 argcount + kwcount);
2658 goto fail;
2659 }
2660 }
2661 /* Allocate and initialize storage for cell vars, and copy free
2662 vars into frame. This isn't too efficient right now. */
2663 if (f->f_ncells) {
2664 int i = 0, j = 0, nargs, found;
2665 char *cellname, *argname;
2666 PyObject *c;
2667
2668 nargs = co->co_argcount;
2669 if (co->co_flags & CO_VARARGS)
2670 nargs++;
2671 if (co->co_flags & CO_VARKEYWORDS)
2672 nargs++;
2673
2674 /* Check for cells that shadow args */
2675 for (i = 0; i < f->f_ncells && j < nargs; ++i) {
2676 cellname = PyString_AS_STRING(
2677 PyTuple_GET_ITEM(co->co_cellvars, i));
2678 found = 0;
2679 while (j < nargs) {
2680 argname = PyString_AS_STRING(
2681 PyTuple_GET_ITEM(co->co_varnames, j));
2682 if (strcmp(cellname, argname) == 0) {
2683 c = PyCell_New(GETLOCAL(j));
2684 if (c == NULL)
2685 goto fail;
2686 GETLOCAL(f->f_nlocals + i) = c;
2687 found = 1;
2688 break;
2689 }
2690 j++;
2691 }
2692 if (found == 0) {
2693 c = PyCell_New(NULL);
2694 if (c == NULL)
2695 goto fail;
2696 SETLOCAL(f->f_nlocals + i, c);
2697 }
2698 }
2699 /* Initialize any that are left */
2700 while (i < f->f_ncells) {
2701 c = PyCell_New(NULL);
2702 if (c == NULL)
2703 goto fail;
2704 SETLOCAL(f->f_nlocals + i, c);
2705 i++;
2706 }
2707 }
2708 if (f->f_nfreevars) {
2709 int i;
2710 for (i = 0; i < f->f_nfreevars; ++i) {
2711 PyObject *o = PyTuple_GET_ITEM(closure, i);
2712 Py_INCREF(o);
2713 freevars[f->f_ncells + i] = o;
2714 }
2715 }
2716
Tim Peters5ca576e2001-06-18 22:08:13 +00002717 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002718 /* Don't need to keep the reference to f_back, it will be set
2719 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002720 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002721 f->f_back = NULL;
2722
Jeremy Hylton985eba52003-02-05 23:13:00 +00002723 PCALL(PCALL_GENERATOR);
2724
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002725 /* Create a new generator that owns the ready to run frame
2726 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00002727 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00002728 }
2729
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002730 retval = PyEval_EvalFrame(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00002731
2732 fail: /* Jump here from prelude on failure */
2733
Tim Petersb13680b2001-11-27 23:29:29 +00002734 /* decref'ing the frame can cause __del__ methods to get invoked,
2735 which can call back into Python. While we're done with the
2736 current Python frame (f), the associated C stack is still in use,
2737 so recursion_depth must be boosted for the duration.
2738 */
2739 assert(tstate != NULL);
2740 ++tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002741 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00002742 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002743 return retval;
2744}
2745
2746
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002747/* Implementation notes for set_exc_info() and reset_exc_info():
2748
2749- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2750 'exc_traceback'. These always travel together.
2751
2752- tstate->curexc_ZZZ is the "hot" exception that is set by
2753 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2754
2755- Once an exception is caught by an except clause, it is transferred
2756 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2757 can pick it up. This is the primary task of set_exc_info().
2758
2759- Now let me explain the complicated dance with frame->f_exc_ZZZ.
2760
2761 Long ago, when none of this existed, there were just a few globals:
2762 one set corresponding to the "hot" exception, and one set
2763 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2764 globals; they were simply stored as sys.exc_ZZZ. For backwards
2765 compatibility, they still are!) The problem was that in code like
2766 this:
2767
2768 try:
2769 "something that may fail"
2770 except "some exception":
2771 "do something else first"
2772 "print the exception from sys.exc_ZZZ."
2773
2774 if "do something else first" invoked something that raised and caught
2775 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2776 cause of subtle bugs. I fixed this by changing the semantics as
2777 follows:
2778
2779 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2780 *in that frame*.
2781
2782 - But initially, and as long as no exception is caught in a given
2783 frame, sys.exc_ZZZ will hold the last exception caught in the
2784 previous frame (or the frame before that, etc.).
2785
2786 The first bullet fixed the bug in the above example. The second
2787 bullet was for backwards compatibility: it was (and is) common to
2788 have a function that is called when an exception is caught, and to
2789 have that function access the caught exception via sys.exc_ZZZ.
2790 (Example: traceback.print_exc()).
2791
2792 At the same time I fixed the problem that sys.exc_ZZZ weren't
2793 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2794 but that's really a separate improvement.
2795
2796 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2797 variables to what they were before the current frame was called. The
2798 set_exc_info() function saves them on the frame so that
2799 reset_exc_info() can restore them. The invariant is that
2800 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2801 exception (where "catching" an exception applies only to successful
2802 except clauses); and if the current frame ever caught an exception,
2803 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2804 at the start of the current frame.
2805
2806*/
2807
Guido van Rossuma027efa1997-05-05 20:56:21 +00002808static void
Guido van Rossumac7be682001-01-17 15:42:30 +00002809set_exc_info(PyThreadState *tstate,
2810 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002811{
2812 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002813 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00002814
Guido van Rossuma027efa1997-05-05 20:56:21 +00002815 frame = tstate->frame;
2816 if (frame->f_exc_type == NULL) {
2817 /* This frame didn't catch an exception before */
2818 /* Save previous exception of this thread in this frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002819 if (tstate->exc_type == NULL) {
2820 Py_INCREF(Py_None);
2821 tstate->exc_type = Py_None;
2822 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002823 tmp_type = frame->f_exc_type;
2824 tmp_value = frame->f_exc_value;
2825 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002826 Py_XINCREF(tstate->exc_type);
2827 Py_XINCREF(tstate->exc_value);
2828 Py_XINCREF(tstate->exc_traceback);
2829 frame->f_exc_type = tstate->exc_type;
2830 frame->f_exc_value = tstate->exc_value;
2831 frame->f_exc_traceback = tstate->exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002832 Py_XDECREF(tmp_type);
2833 Py_XDECREF(tmp_value);
2834 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002835 }
2836 /* Set new exception for this thread */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002837 tmp_type = tstate->exc_type;
2838 tmp_value = tstate->exc_value;
2839 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002840 Py_XINCREF(type);
2841 Py_XINCREF(value);
2842 Py_XINCREF(tb);
2843 tstate->exc_type = type;
2844 tstate->exc_value = value;
2845 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002846 Py_XDECREF(tmp_type);
2847 Py_XDECREF(tmp_value);
2848 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002849 /* For b/w compatibility */
2850 PySys_SetObject("exc_type", type);
2851 PySys_SetObject("exc_value", value);
2852 PySys_SetObject("exc_traceback", tb);
2853}
2854
2855static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002856reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002857{
2858 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002859 PyObject *tmp_type, *tmp_value, *tmp_tb;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002860 frame = tstate->frame;
2861 if (frame->f_exc_type != NULL) {
2862 /* This frame caught an exception */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002863 tmp_type = tstate->exc_type;
2864 tmp_value = tstate->exc_value;
2865 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002866 Py_XINCREF(frame->f_exc_type);
2867 Py_XINCREF(frame->f_exc_value);
2868 Py_XINCREF(frame->f_exc_traceback);
2869 tstate->exc_type = frame->f_exc_type;
2870 tstate->exc_value = frame->f_exc_value;
2871 tstate->exc_traceback = frame->f_exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002872 Py_XDECREF(tmp_type);
2873 Py_XDECREF(tmp_value);
2874 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002875 /* For b/w compatibility */
2876 PySys_SetObject("exc_type", frame->f_exc_type);
2877 PySys_SetObject("exc_value", frame->f_exc_value);
2878 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
2879 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002880 tmp_type = frame->f_exc_type;
2881 tmp_value = frame->f_exc_value;
2882 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002883 frame->f_exc_type = NULL;
2884 frame->f_exc_value = NULL;
2885 frame->f_exc_traceback = NULL;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002886 Py_XDECREF(tmp_type);
2887 Py_XDECREF(tmp_value);
2888 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002889}
2890
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002891/* Logic for the raise statement (too complicated for inlining).
2892 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00002893static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002894do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002895{
Guido van Rossumd295f121998-04-09 21:39:57 +00002896 if (type == NULL) {
2897 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00002898 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumd295f121998-04-09 21:39:57 +00002899 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
2900 value = tstate->exc_value;
2901 tb = tstate->exc_traceback;
2902 Py_XINCREF(type);
2903 Py_XINCREF(value);
2904 Py_XINCREF(tb);
2905 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002906
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002907 /* We support the following forms of raise:
2908 raise <class>, <classinstance>
2909 raise <class>, <argument tuple>
2910 raise <class>, None
2911 raise <class>, <argument>
2912 raise <classinstance>, None
2913 raise <string>, <object>
2914 raise <string>, None
2915
2916 An omitted second argument is the same as None.
2917
2918 In addition, raise <tuple>, <anything> is the same as
2919 raising the tuple's first item (and it better have one!);
2920 this rule is applied recursively.
2921
2922 Finally, an optional third argument can be supplied, which
2923 gives the traceback to be substituted (useful when
2924 re-raising an exception after examining it). */
2925
2926 /* First, check the traceback argument, replacing None with
2927 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002928 if (tb == Py_None) {
2929 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002930 tb = NULL;
2931 }
2932 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002933 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002934 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002935 goto raise_error;
2936 }
2937
2938 /* Next, replace a missing value with None */
2939 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002940 value = Py_None;
2941 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002942 }
2943
2944 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00002945 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
2946 PyObject *tmp = type;
2947 type = PyTuple_GET_ITEM(type, 0);
2948 Py_INCREF(type);
2949 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002950 }
2951
Tim Petersafb2c802002-04-18 18:06:20 +00002952 if (PyString_CheckExact(type))
2953 /* Raising builtin string is deprecated but still allowed --
2954 * do nothing. Raising an instance of a new-style str
2955 * subclass is right out. */
Neal Norwitz37aa0662003-01-10 15:31:15 +00002956 PyErr_Warn(PyExc_PendingDeprecationWarning,
2957 "raising a string exception is deprecated");
Barry Warsaw4249f541997-08-22 21:26:19 +00002958
2959 else if (PyClass_Check(type))
2960 PyErr_NormalizeException(&type, &value, &tb);
2961
Guido van Rossumb209a111997-04-29 18:18:01 +00002962 else if (PyInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002963 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002964 if (value != Py_None) {
2965 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002966 "instance exception may not have a separate value");
2967 goto raise_error;
2968 }
2969 else {
2970 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00002971 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002972 value = type;
Guido van Rossumb209a111997-04-29 18:18:01 +00002973 type = (PyObject*) ((PyInstanceObject*)type)->in_class;
2974 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002975 }
2976 }
2977 else {
2978 /* Not something you can raise. You get an exception
2979 anyway, just not what you specified :-) */
Jeremy Hylton960d9482001-04-27 02:25:33 +00002980 PyErr_Format(PyExc_TypeError,
Neal Norwitz37aa0662003-01-10 15:31:15 +00002981 "exceptions must be classes, instances, or "
2982 "strings (deprecated), not %s",
2983 type->ob_type->tp_name);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002984 goto raise_error;
2985 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002986 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002987 if (tb == NULL)
2988 return WHY_EXCEPTION;
2989 else
2990 return WHY_RERAISE;
2991 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00002992 Py_XDECREF(value);
2993 Py_XDECREF(type);
2994 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002995 return WHY_EXCEPTION;
2996}
2997
Tim Petersd6d010b2001-06-21 02:49:55 +00002998/* Iterate v argcnt times and store the results on the stack (via decreasing
2999 sp). Return 1 for success, 0 if error. */
3000
Barry Warsawe42b18f1997-08-25 22:13:04 +00003001static int
Tim Petersd6d010b2001-06-21 02:49:55 +00003002unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003003{
Tim Petersd6d010b2001-06-21 02:49:55 +00003004 int i = 0;
3005 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00003006 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00003007
Tim Petersd6d010b2001-06-21 02:49:55 +00003008 assert(v != NULL);
3009
3010 it = PyObject_GetIter(v);
3011 if (it == NULL)
3012 goto Error;
3013
3014 for (; i < argcnt; i++) {
3015 w = PyIter_Next(it);
3016 if (w == NULL) {
3017 /* Iterator done, via error or exhaustion. */
3018 if (!PyErr_Occurred()) {
3019 PyErr_Format(PyExc_ValueError,
3020 "need more than %d value%s to unpack",
3021 i, i == 1 ? "" : "s");
3022 }
3023 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003024 }
3025 *--sp = w;
3026 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003027
3028 /* We better have exhausted the iterator now. */
3029 w = PyIter_Next(it);
3030 if (w == NULL) {
3031 if (PyErr_Occurred())
3032 goto Error;
3033 Py_DECREF(it);
3034 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003035 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00003036 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00003037 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00003038 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00003039Error:
Barry Warsaw91010551997-08-25 22:30:51 +00003040 for (; i > 0; i--, sp++)
3041 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00003042 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00003043 return 0;
3044}
3045
3046
Guido van Rossum96a42c81992-01-12 02:29:51 +00003047#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003048static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003049prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003050{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003051 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003052 if (PyObject_Print(v, stdout, 0) != 0)
3053 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003054 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003055 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003056}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003057#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003058
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003059static void
Fred Drake5755ce62001-06-27 19:19:46 +00003060call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003061{
Guido van Rossumb209a111997-04-29 18:18:01 +00003062 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003063 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003064 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003065 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003066 value = Py_None;
3067 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003068 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003069 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003070 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003071 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003072 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003073 }
Fred Drake5755ce62001-06-27 19:19:46 +00003074 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003075 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003076 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003077 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003078 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003079 Py_XDECREF(type);
3080 Py_XDECREF(value);
3081 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003082 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003083}
3084
Fred Drake4ec5d562001-10-04 19:26:43 +00003085static void
3086call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3087 int what)
3088{
3089 PyObject *type, *value, *traceback;
3090 int err;
3091 PyErr_Fetch(&type, &value, &traceback);
3092 err = call_trace(func, obj, frame, what, NULL);
3093 if (err == 0)
3094 PyErr_Restore(type, value, traceback);
3095 else {
3096 Py_XDECREF(type);
3097 Py_XDECREF(value);
3098 Py_XDECREF(traceback);
3099 }
3100}
3101
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003102static int
Fred Drake5755ce62001-06-27 19:19:46 +00003103call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3104 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003105{
Fred Drake5755ce62001-06-27 19:19:46 +00003106 register PyThreadState *tstate = frame->f_tstate;
3107 int result;
3108 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003109 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003110 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003111 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003112 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003113 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3114 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003115 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003116 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003117}
3118
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003119PyObject *
3120_PyEval_CallTracing(PyObject *func, PyObject *args)
3121{
3122 PyFrameObject *frame = PyEval_GetFrame();
3123 PyThreadState *tstate = frame->f_tstate;
3124 int save_tracing = tstate->tracing;
3125 int save_use_tracing = tstate->use_tracing;
3126 PyObject *result;
3127
3128 tstate->tracing = 0;
3129 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3130 || (tstate->c_profilefunc != NULL));
3131 result = PyObject_Call(func, args, NULL);
3132 tstate->tracing = save_tracing;
3133 tstate->use_tracing = save_use_tracing;
3134 return result;
3135}
3136
Michael W. Hudson006c7522002-11-08 13:08:46 +00003137static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003138maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003139 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3140 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003141{
3142 /* The theory of SET_LINENO-less tracing.
Tim Peters8a5c3c72004-04-05 19:36:21 +00003143
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003144 In a nutshell, we use the co_lnotab field of the code object
3145 to tell when execution has moved onto a different line.
3146
3147 As mentioned above, the basic idea is so set things up so
3148 that
3149
3150 *instr_lb <= frame->f_lasti < *instr_ub
3151
3152 is true so long as execution does not change lines.
3153
3154 This is all fairly simple. Digging the information out of
3155 co_lnotab takes some work, but is conceptually clear.
3156
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003157 Somewhat harder to explain is why we don't *always* call the
3158 line trace function when the above test fails.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003159
3160 Consider this code:
3161
3162 1: def f(a):
3163 2: if a:
3164 3: print 1
3165 4: else:
3166 5: print 2
3167
3168 which compiles to this:
3169
3170 2 0 LOAD_FAST 0 (a)
3171 3 JUMP_IF_FALSE 9 (to 15)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003172 6 POP_TOP
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003173
3174 3 7 LOAD_CONST 1 (1)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003175 10 PRINT_ITEM
3176 11 PRINT_NEWLINE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003177 12 JUMP_FORWARD 6 (to 21)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003178 >> 15 POP_TOP
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003179
3180 5 16 LOAD_CONST 2 (2)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003181 19 PRINT_ITEM
3182 20 PRINT_NEWLINE
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003183 >> 21 LOAD_CONST 0 (None)
3184 24 RETURN_VALUE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003185
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003186 If 'a' is false, execution will jump to instruction at offset
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003187 15 and the co_lnotab will claim that execution has moved to
3188 line 3. This is at best misleading. In this case we could
3189 associate the POP_TOP with line 4, but that doesn't make
3190 sense in all cases (I think).
3191
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003192 What we do is only call the line trace function if the co_lnotab
3193 indicates we have jumped to the *start* of a line, i.e. if the
3194 current instruction offset matches the offset given for the
3195 start of a line by the co_lnotab.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003196
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003197 This also takes care of the situation where 'a' is true.
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003198 Execution will jump from instruction offset 12 to offset 21.
3199 Then the co_lnotab would imply that execution has moved to line
3200 5, which is again misleading.
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003201
3202 Why do we set f_lineno when tracing? Well, consider the code
3203 above when 'a' is true. If stepping through this with 'n' in
3204 pdb, you would stop at line 1 with a "call" type event, then
3205 line events on lines 2 and 3, then a "return" type event -- but
3206 you would be shown line 5 during this event. This is a change
3207 from the behaviour in 2.2 and before, and I've found it
3208 confusing in practice. By setting and using f_lineno when
3209 tracing, one can report a line number different from that
3210 suggested by f_lasti on this one occasion where it's desirable.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003211 */
3212
Michael W. Hudson006c7522002-11-08 13:08:46 +00003213 int result = 0;
3214
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003215 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003216 PyCodeObject* co = frame->f_code;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003217 int size, addr, line;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003218 unsigned char* p;
3219
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003220 size = PyString_GET_SIZE(co->co_lnotab) / 2;
3221 p = (unsigned char*)PyString_AS_STRING(co->co_lnotab);
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003222
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003223 addr = 0;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003224 line = co->co_firstlineno;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003225
3226 /* possible optimization: if f->f_lasti == instr_ub
3227 (likely to be a common case) then we already know
3228 instr_lb -- if we stored the matching value of p
3229 somwhere we could skip the first while loop. */
3230
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003231 /* see comments in compile.c for the description of
3232 co_lnotab. A point to remember: increments to p
3233 should come in pairs -- although we don't care about
3234 the line increments here, treating them as byte
3235 increments gets confusing, to say the least. */
3236
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003237 while (size > 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003238 if (addr + *p > frame->f_lasti)
3239 break;
3240 addr += *p++;
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003241 if (*p) *instr_lb = addr;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003242 line += *p++;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003243 --size;
3244 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003245
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003246 if (addr == frame->f_lasti) {
3247 frame->f_lineno = line;
Tim Peters8a5c3c72004-04-05 19:36:21 +00003248 result = call_trace(func, obj, frame,
Michael W. Hudson006c7522002-11-08 13:08:46 +00003249 PyTrace_LINE, Py_None);
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003250 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003251
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003252 if (size > 0) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003253 while (--size >= 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003254 addr += *p++;
3255 if (*p++)
3256 break;
3257 }
3258 *instr_ub = addr;
3259 }
3260 else {
3261 *instr_ub = INT_MAX;
3262 }
3263 }
Armin Rigobf57a142004-03-22 19:24:58 +00003264 else if (frame->f_lasti <= *instr_prev) {
3265 /* jumping back in the same line forces a trace event */
Tim Peters8a5c3c72004-04-05 19:36:21 +00003266 result = call_trace(func, obj, frame,
Armin Rigobf57a142004-03-22 19:24:58 +00003267 PyTrace_LINE, Py_None);
3268 }
3269 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003270 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003271}
3272
Fred Drake5755ce62001-06-27 19:19:46 +00003273void
3274PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003275{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003276 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003277 PyObject *temp = tstate->c_profileobj;
3278 Py_XINCREF(arg);
3279 tstate->c_profilefunc = NULL;
3280 tstate->c_profileobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003281 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003282 Py_XDECREF(temp);
3283 tstate->c_profilefunc = func;
3284 tstate->c_profileobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003285 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003286}
3287
3288void
3289PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3290{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003291 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003292 PyObject *temp = tstate->c_traceobj;
3293 Py_XINCREF(arg);
3294 tstate->c_tracefunc = NULL;
3295 tstate->c_traceobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003296 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003297 Py_XDECREF(temp);
3298 tstate->c_tracefunc = func;
3299 tstate->c_traceobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003300 tstate->use_tracing = ((func != NULL)
3301 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003302}
3303
Guido van Rossumb209a111997-04-29 18:18:01 +00003304PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003305PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003306{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003307 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003308 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003309 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003310 else
3311 return current_frame->f_builtins;
3312}
3313
Guido van Rossumb209a111997-04-29 18:18:01 +00003314PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003315PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003316{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003317 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003318 if (current_frame == NULL)
3319 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003320 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003321 return current_frame->f_locals;
3322}
3323
Guido van Rossumb209a111997-04-29 18:18:01 +00003324PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003325PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003326{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003327 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003328 if (current_frame == NULL)
3329 return NULL;
3330 else
3331 return current_frame->f_globals;
3332}
3333
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003334PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003335PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003336{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003337 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003338 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003339}
3340
Guido van Rossum6135a871995-01-09 17:53:26 +00003341int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003342PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003343{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003344 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003345 return current_frame == NULL ? 0 : current_frame->f_restricted;
3346}
3347
Guido van Rossumbe270261997-05-22 22:26:18 +00003348int
Tim Peters5ba58662001-07-16 02:29:45 +00003349PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003350{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003351 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003352 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003353
3354 if (current_frame != NULL) {
3355 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003356 const int compilerflags = codeflags & PyCF_MASK;
3357 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003358 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003359 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003360 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003361#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003362 if (codeflags & CO_GENERATOR_ALLOWED) {
3363 result = 1;
3364 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3365 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003366#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003367 }
3368 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003369}
3370
3371int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003372Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003373{
Guido van Rossumb209a111997-04-29 18:18:01 +00003374 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003375 if (f == NULL)
3376 return 0;
3377 if (!PyFile_SoftSpace(f, 0))
3378 return 0;
3379 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003380}
3381
Guido van Rossum3f5da241990-12-20 15:06:42 +00003382
Guido van Rossum681d79a1995-07-18 14:51:37 +00003383/* External interface to call any callable object.
3384 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003385
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003386#undef PyEval_CallObject
3387/* for backward compatibility: export this interface */
3388
Guido van Rossumb209a111997-04-29 18:18:01 +00003389PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003390PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003391{
Guido van Rossumb209a111997-04-29 18:18:01 +00003392 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003393}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003394#define PyEval_CallObject(func,arg) \
3395 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003396
Guido van Rossumb209a111997-04-29 18:18:01 +00003397PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003398PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003399{
Jeremy Hylton52820442001-01-03 23:52:36 +00003400 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003401
3402 if (arg == NULL)
Guido van Rossumb209a111997-04-29 18:18:01 +00003403 arg = PyTuple_New(0);
3404 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003405 PyErr_SetString(PyExc_TypeError,
3406 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003407 return NULL;
3408 }
3409 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003410 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003411
Guido van Rossumb209a111997-04-29 18:18:01 +00003412 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003413 PyErr_SetString(PyExc_TypeError,
3414 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003415 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003416 return NULL;
3417 }
3418
Tim Peters6d6c1a32001-08-02 04:15:00 +00003419 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003420 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003421 return result;
3422}
3423
Tim Peters6d6c1a32001-08-02 04:15:00 +00003424char *
3425PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003426{
3427 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003428 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003429 else if (PyFunction_Check(func))
3430 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3431 else if (PyCFunction_Check(func))
3432 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3433 else if (PyClass_Check(func))
3434 return PyString_AsString(((PyClassObject*)func)->cl_name);
3435 else if (PyInstance_Check(func)) {
3436 return PyString_AsString(
3437 ((PyInstanceObject*)func)->in_class->cl_name);
3438 } else {
3439 return func->ob_type->tp_name;
3440 }
3441}
3442
Tim Peters6d6c1a32001-08-02 04:15:00 +00003443char *
3444PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003445{
3446 if (PyMethod_Check(func))
3447 return "()";
3448 else if (PyFunction_Check(func))
3449 return "()";
3450 else if (PyCFunction_Check(func))
3451 return "()";
3452 else if (PyClass_Check(func))
3453 return " constructor";
3454 else if (PyInstance_Check(func)) {
3455 return " instance";
3456 } else {
3457 return " object";
3458 }
3459}
3460
Jeremy Hylton52820442001-01-03 23:52:36 +00003461#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
3462
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003463static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003464err_args(PyObject *func, int flags, int nargs)
3465{
3466 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003467 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003468 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003469 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003470 nargs);
3471 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003472 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003473 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003474 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003475 nargs);
3476}
3477
Nicholas Bastind858a772004-06-25 23:31:06 +00003478#define C_TRACE(call) \
3479if (tstate->use_tracing && tstate->c_profilefunc) { \
3480 if (call_trace(tstate->c_profilefunc, \
3481 tstate->c_profileobj, \
3482 tstate->frame, PyTrace_C_CALL, \
3483 func)) \
3484 { return NULL; } \
3485 call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003486 if (tstate->c_profilefunc != NULL) { \
Nicholas Bastind858a772004-06-25 23:31:06 +00003487 if (x == NULL) { \
3488 if (call_trace (tstate->c_profilefunc, \
3489 tstate->c_profileobj, \
3490 tstate->frame, PyTrace_C_EXCEPTION, \
3491 func)) \
3492 { return NULL; } \
3493 } else { \
3494 if (call_trace(tstate->c_profilefunc, \
3495 tstate->c_profileobj, \
3496 tstate->frame, PyTrace_C_RETURN, \
3497 func)) \
3498 { return NULL; } \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003499 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003500 } \
3501} else { \
3502 call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003503 }
3504
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003505static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003506call_function(PyObject ***pp_stack, int oparg
3507#ifdef WITH_TSC
3508 , uint64* pintr0, uint64* pintr1
3509#endif
3510 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003511{
3512 int na = oparg & 0xff;
3513 int nk = (oparg>>8) & 0xff;
3514 int n = na + 2 * nk;
3515 PyObject **pfunc = (*pp_stack) - n - 1;
3516 PyObject *func = *pfunc;
3517 PyObject *x, *w;
3518
Jeremy Hylton985eba52003-02-05 23:13:00 +00003519 /* Always dispatch PyCFunction first, because these are
3520 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003521 */
3522 if (PyCFunction_Check(func) && nk == 0) {
3523 int flags = PyCFunction_GET_FLAGS(func);
Nicholas Bastind858a772004-06-25 23:31:06 +00003524 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003525
3526 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003527 if (flags & (METH_NOARGS | METH_O)) {
3528 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3529 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003530 if (flags & METH_NOARGS && na == 0) {
Nicholas Bastind858a772004-06-25 23:31:06 +00003531 C_TRACE(x=(*meth)(self,NULL));
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003532 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003533 else if (flags & METH_O && na == 1) {
3534 PyObject *arg = EXT_POP(*pp_stack);
Nicholas Bastind858a772004-06-25 23:31:06 +00003535 C_TRACE(x=(*meth)(self,arg));
Jeremy Hylton192690e2002-08-16 18:36:11 +00003536 Py_DECREF(arg);
3537 }
3538 else {
3539 err_args(func, flags, na);
3540 x = NULL;
3541 }
3542 }
3543 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003544 PyObject *callargs;
3545 callargs = load_args(pp_stack, na);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003546 rdtscll(*pintr0);
Nicholas Bastind858a772004-06-25 23:31:06 +00003547 C_TRACE(x=PyCFunction_Call(func,callargs,NULL));
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003548 rdtscll(*pintr1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003549 Py_XDECREF(callargs);
3550 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003551 } else {
3552 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3553 /* optimize access to bound methods */
3554 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003555 PCALL(PCALL_METHOD);
3556 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003557 Py_INCREF(self);
3558 func = PyMethod_GET_FUNCTION(func);
3559 Py_INCREF(func);
3560 Py_DECREF(*pfunc);
3561 *pfunc = self;
3562 na++;
3563 n++;
3564 } else
3565 Py_INCREF(func);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003566 rdtscll(*pintr0);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003567 if (PyFunction_Check(func))
3568 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003569 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003570 x = do_call(func, pp_stack, na, nk);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003571 rdtscll(*pintr1);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003572 Py_DECREF(func);
3573 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003574
Jeremy Hylton985eba52003-02-05 23:13:00 +00003575 /* What does this do? */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003576 while ((*pp_stack) > pfunc) {
3577 w = EXT_POP(*pp_stack);
3578 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003579 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003580 }
3581 return x;
3582}
3583
Jeremy Hylton192690e2002-08-16 18:36:11 +00003584/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003585 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003586 For the simplest case -- a function that takes only positional
3587 arguments and is called with only positional arguments -- it
3588 inlines the most primitive frame setup code from
3589 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3590 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003591*/
3592
3593static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003594fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003595{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003596 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003597 PyObject *globals = PyFunction_GET_GLOBALS(func);
3598 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3599 PyObject **d = NULL;
3600 int nd = 0;
3601
Jeremy Hylton985eba52003-02-05 23:13:00 +00003602 PCALL(PCALL_FUNCTION);
3603 PCALL(PCALL_FAST_FUNCTION);
Raymond Hettinger40174c32003-05-31 07:04:16 +00003604 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003605 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3606 PyFrameObject *f;
3607 PyObject *retval = NULL;
3608 PyThreadState *tstate = PyThreadState_GET();
3609 PyObject **fastlocals, **stack;
3610 int i;
3611
3612 PCALL(PCALL_FASTER_FUNCTION);
3613 assert(globals != NULL);
3614 /* XXX Perhaps we should create a specialized
3615 PyFrame_New() that doesn't take locals, but does
3616 take builtins without sanity checking them.
3617 */
3618 f = PyFrame_New(tstate, co, globals, NULL);
3619 if (f == NULL)
3620 return NULL;
3621
3622 fastlocals = f->f_localsplus;
3623 stack = (*pp_stack) - n;
3624
3625 for (i = 0; i < n; i++) {
3626 Py_INCREF(*stack);
3627 fastlocals[i] = *stack++;
3628 }
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003629 retval = PyEval_EvalFrame(f);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003630 assert(tstate != NULL);
3631 ++tstate->recursion_depth;
3632 Py_DECREF(f);
3633 --tstate->recursion_depth;
3634 return retval;
3635 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003636 if (argdefs != NULL) {
3637 d = &PyTuple_GET_ITEM(argdefs, 0);
3638 nd = ((PyTupleObject *)argdefs)->ob_size;
3639 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003640 return PyEval_EvalCodeEx(co, globals,
3641 (PyObject *)NULL, (*pp_stack)-n, na,
3642 (*pp_stack)-2*nk, nk, d, nd,
3643 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003644}
3645
3646static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003647update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3648 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003649{
3650 PyObject *kwdict = NULL;
3651 if (orig_kwdict == NULL)
3652 kwdict = PyDict_New();
3653 else {
3654 kwdict = PyDict_Copy(orig_kwdict);
3655 Py_DECREF(orig_kwdict);
3656 }
3657 if (kwdict == NULL)
3658 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003659 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003660 int err;
3661 PyObject *value = EXT_POP(*pp_stack);
3662 PyObject *key = EXT_POP(*pp_stack);
3663 if (PyDict_GetItem(kwdict, key) != NULL) {
Guido van Rossumac7be682001-01-17 15:42:30 +00003664 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +00003665 "%.200s%s got multiple values "
Jeremy Hylton512a2372001-04-11 13:52:29 +00003666 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003667 PyEval_GetFuncName(func),
3668 PyEval_GetFuncDesc(func),
Jeremy Hylton512a2372001-04-11 13:52:29 +00003669 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003670 Py_DECREF(key);
3671 Py_DECREF(value);
3672 Py_DECREF(kwdict);
3673 return NULL;
3674 }
3675 err = PyDict_SetItem(kwdict, key, value);
3676 Py_DECREF(key);
3677 Py_DECREF(value);
3678 if (err) {
3679 Py_DECREF(kwdict);
3680 return NULL;
3681 }
3682 }
3683 return kwdict;
3684}
3685
3686static PyObject *
3687update_star_args(int nstack, int nstar, PyObject *stararg,
3688 PyObject ***pp_stack)
3689{
3690 PyObject *callargs, *w;
3691
3692 callargs = PyTuple_New(nstack + nstar);
3693 if (callargs == NULL) {
3694 return NULL;
3695 }
3696 if (nstar) {
3697 int i;
3698 for (i = 0; i < nstar; i++) {
3699 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3700 Py_INCREF(a);
3701 PyTuple_SET_ITEM(callargs, nstack + i, a);
3702 }
3703 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003704 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003705 w = EXT_POP(*pp_stack);
3706 PyTuple_SET_ITEM(callargs, nstack, w);
3707 }
3708 return callargs;
3709}
3710
3711static PyObject *
3712load_args(PyObject ***pp_stack, int na)
3713{
3714 PyObject *args = PyTuple_New(na);
3715 PyObject *w;
3716
3717 if (args == NULL)
3718 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003719 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003720 w = EXT_POP(*pp_stack);
3721 PyTuple_SET_ITEM(args, na, w);
3722 }
3723 return args;
3724}
3725
3726static PyObject *
3727do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3728{
3729 PyObject *callargs = NULL;
3730 PyObject *kwdict = NULL;
3731 PyObject *result = NULL;
3732
3733 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003734 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003735 if (kwdict == NULL)
3736 goto call_fail;
3737 }
3738 callargs = load_args(pp_stack, na);
3739 if (callargs == NULL)
3740 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003741#ifdef CALL_PROFILE
3742 /* At this point, we have to look at the type of func to
3743 update the call stats properly. Do it here so as to avoid
3744 exposing the call stats machinery outside ceval.c
3745 */
3746 if (PyFunction_Check(func))
3747 PCALL(PCALL_FUNCTION);
3748 else if (PyMethod_Check(func))
3749 PCALL(PCALL_METHOD);
3750 else if (PyType_Check(func))
3751 PCALL(PCALL_TYPE);
3752 else
3753 PCALL(PCALL_OTHER);
3754#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003755 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003756 call_fail:
3757 Py_XDECREF(callargs);
3758 Py_XDECREF(kwdict);
3759 return result;
3760}
3761
3762static PyObject *
3763ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3764{
3765 int nstar = 0;
3766 PyObject *callargs = NULL;
3767 PyObject *stararg = NULL;
3768 PyObject *kwdict = NULL;
3769 PyObject *result = NULL;
3770
3771 if (flags & CALL_FLAG_KW) {
3772 kwdict = EXT_POP(*pp_stack);
3773 if (!(kwdict && PyDict_Check(kwdict))) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003774 PyErr_Format(PyExc_TypeError,
Jeremy Hylton512a2372001-04-11 13:52:29 +00003775 "%s%s argument after ** "
3776 "must be a dictionary",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003777 PyEval_GetFuncName(func),
3778 PyEval_GetFuncDesc(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003779 goto ext_call_fail;
3780 }
3781 }
3782 if (flags & CALL_FLAG_VAR) {
3783 stararg = EXT_POP(*pp_stack);
3784 if (!PyTuple_Check(stararg)) {
3785 PyObject *t = NULL;
3786 t = PySequence_Tuple(stararg);
3787 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003788 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3789 PyErr_Format(PyExc_TypeError,
3790 "%s%s argument after * "
3791 "must be a sequence",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003792 PyEval_GetFuncName(func),
3793 PyEval_GetFuncDesc(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003794 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003795 goto ext_call_fail;
3796 }
3797 Py_DECREF(stararg);
3798 stararg = t;
3799 }
3800 nstar = PyTuple_GET_SIZE(stararg);
3801 }
3802 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003803 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003804 if (kwdict == NULL)
3805 goto ext_call_fail;
3806 }
3807 callargs = update_star_args(na, nstar, stararg, pp_stack);
3808 if (callargs == NULL)
3809 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003810#ifdef CALL_PROFILE
3811 /* At this point, we have to look at the type of func to
3812 update the call stats properly. Do it here so as to avoid
3813 exposing the call stats machinery outside ceval.c
3814 */
3815 if (PyFunction_Check(func))
3816 PCALL(PCALL_FUNCTION);
3817 else if (PyMethod_Check(func))
3818 PCALL(PCALL_METHOD);
3819 else if (PyType_Check(func))
3820 PCALL(PCALL_TYPE);
3821 else
3822 PCALL(PCALL_OTHER);
3823#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003824 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003825 ext_call_fail:
3826 Py_XDECREF(callargs);
3827 Py_XDECREF(kwdict);
3828 Py_XDECREF(stararg);
3829 return result;
3830}
3831
Tim Peterscb479e72001-12-16 19:11:44 +00003832/* Extract a slice index from a PyInt or PyLong, and store in *pi.
3833 Silently reduce values larger than INT_MAX to INT_MAX, and silently
3834 boost values less than -INT_MAX to 0. Return 0 on error, 1 on success.
3835*/
Tim Petersb5196382001-12-16 19:44:20 +00003836/* Note: If v is NULL, return success without storing into *pi. This
3837 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3838 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00003839*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00003840int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003841_PyEval_SliceIndex(PyObject *v, int *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003842{
Tim Petersb5196382001-12-16 19:44:20 +00003843 if (v != NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003844 long x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003845 if (PyInt_Check(v)) {
3846 x = PyInt_AsLong(v);
3847 } else if (PyLong_Check(v)) {
3848 x = PyLong_AsLong(v);
3849 if (x==-1 && PyErr_Occurred()) {
3850 PyObject *long_zero;
Guido van Rossumac7be682001-01-17 15:42:30 +00003851 int cmp;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003852
Guido van Rossumac7be682001-01-17 15:42:30 +00003853 if (!PyErr_ExceptionMatches(
3854 PyExc_OverflowError)) {
3855 /* It's not an overflow error, so just
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003856 signal an error */
Guido van Rossum20c6add2000-05-08 14:06:50 +00003857 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003858 }
3859
Guido van Rossumac7be682001-01-17 15:42:30 +00003860 /* Clear the OverflowError */
3861 PyErr_Clear();
3862
3863 /* It's an overflow error, so we need to
3864 check the sign of the long integer,
Tim Peters8a5c3c72004-04-05 19:36:21 +00003865 set the value to INT_MAX or -INT_MAX,
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003866 and clear the error. */
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003867
3868 /* Create a long integer with a value of 0 */
Guido van Rossumac7be682001-01-17 15:42:30 +00003869 long_zero = PyLong_FromLong(0L);
Tim Peterscb479e72001-12-16 19:11:44 +00003870 if (long_zero == NULL)
3871 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003872
3873 /* Check sign */
Guido van Rossumac7be682001-01-17 15:42:30 +00003874 cmp = PyObject_RichCompareBool(v, long_zero,
3875 Py_GT);
3876 Py_DECREF(long_zero);
3877 if (cmp < 0)
3878 return 0;
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003879 else if (cmp)
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003880 x = INT_MAX;
3881 else
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003882 x = -INT_MAX;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003883 }
3884 } else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003885 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003886 "slice indices must be integers");
Guido van Rossum20c6add2000-05-08 14:06:50 +00003887 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003888 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003889 /* Truncate -- very long indices are truncated anyway */
3890 if (x > INT_MAX)
3891 x = INT_MAX;
3892 else if (x < -INT_MAX)
Michael W. Hudsoncbd6fb92002-11-06 15:17:32 +00003893 x = -INT_MAX;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003894 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003895 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00003896 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003897}
3898
Guido van Rossum50d756e2001-08-18 17:43:36 +00003899#undef ISINT
3900#define ISINT(x) ((x) == NULL || PyInt_Check(x) || PyLong_Check(x))
3901
Guido van Rossumb209a111997-04-29 18:18:01 +00003902static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003903apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003904{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003905 PyTypeObject *tp = u->ob_type;
3906 PySequenceMethods *sq = tp->tp_as_sequence;
3907
3908 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3909 int ilow = 0, ihigh = INT_MAX;
3910 if (!_PyEval_SliceIndex(v, &ilow))
3911 return NULL;
3912 if (!_PyEval_SliceIndex(w, &ihigh))
3913 return NULL;
3914 return PySequence_GetSlice(u, ilow, ihigh);
3915 }
3916 else {
3917 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00003918 if (slice != NULL) {
3919 PyObject *res = PyObject_GetItem(u, slice);
3920 Py_DECREF(slice);
3921 return res;
3922 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00003923 else
3924 return NULL;
3925 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003926}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003927
3928static int
Guido van Rossumac7be682001-01-17 15:42:30 +00003929assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
3930 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003931{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003932 PyTypeObject *tp = u->ob_type;
3933 PySequenceMethods *sq = tp->tp_as_sequence;
3934
3935 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3936 int ilow = 0, ihigh = INT_MAX;
3937 if (!_PyEval_SliceIndex(v, &ilow))
3938 return -1;
3939 if (!_PyEval_SliceIndex(w, &ihigh))
3940 return -1;
3941 if (x == NULL)
3942 return PySequence_DelSlice(u, ilow, ihigh);
3943 else
3944 return PySequence_SetSlice(u, ilow, ihigh, x);
3945 }
3946 else {
3947 PyObject *slice = PySlice_New(v, w, NULL);
3948 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00003949 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003950 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00003951 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00003952 else
Guido van Rossum354797c2001-12-03 19:45:06 +00003953 res = PyObject_DelItem(u, slice);
3954 Py_DECREF(slice);
3955 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003956 }
3957 else
3958 return -1;
3959 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003960}
3961
Guido van Rossumb209a111997-04-29 18:18:01 +00003962static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003963cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003964{
Guido van Rossumac7be682001-01-17 15:42:30 +00003965 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003966 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00003967 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003968 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003969 break;
3970 case PyCmp_IS_NOT:
3971 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003972 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003973 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003974 res = PySequence_Contains(w, v);
3975 if (res < 0)
3976 return NULL;
3977 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003978 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00003979 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003980 if (res < 0)
3981 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003982 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003983 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003984 case PyCmp_EXC_MATCH:
Barry Warsaw4249f541997-08-22 21:26:19 +00003985 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003986 break;
3987 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00003988 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003989 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003990 v = res ? Py_True : Py_False;
3991 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003992 return v;
3993}
3994
Thomas Wouters52152252000-08-17 22:55:00 +00003995static PyObject *
3996import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003997{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003998 PyObject *x;
3999
4000 x = PyObject_GetAttr(v, name);
4001 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00004002 PyErr_Format(PyExc_ImportError,
4003 "cannot import name %.230s",
4004 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004005 }
Thomas Wouters52152252000-08-17 22:55:00 +00004006 return x;
4007}
Guido van Rossumac7be682001-01-17 15:42:30 +00004008
Thomas Wouters52152252000-08-17 22:55:00 +00004009static int
4010import_all_from(PyObject *locals, PyObject *v)
4011{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004012 PyObject *all = PyObject_GetAttrString(v, "__all__");
4013 PyObject *dict, *name, *value;
4014 int skip_leading_underscores = 0;
4015 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004016
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004017 if (all == NULL) {
4018 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4019 return -1; /* Unexpected error */
4020 PyErr_Clear();
4021 dict = PyObject_GetAttrString(v, "__dict__");
4022 if (dict == NULL) {
4023 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4024 return -1;
4025 PyErr_SetString(PyExc_ImportError,
4026 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00004027 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004028 }
4029 all = PyMapping_Keys(dict);
4030 Py_DECREF(dict);
4031 if (all == NULL)
4032 return -1;
4033 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004034 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004035
4036 for (pos = 0, err = 0; ; pos++) {
4037 name = PySequence_GetItem(all, pos);
4038 if (name == NULL) {
4039 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4040 err = -1;
4041 else
4042 PyErr_Clear();
4043 break;
4044 }
4045 if (skip_leading_underscores &&
4046 PyString_Check(name) &&
4047 PyString_AS_STRING(name)[0] == '_')
4048 {
4049 Py_DECREF(name);
4050 continue;
4051 }
4052 value = PyObject_GetAttr(v, name);
4053 if (value == NULL)
4054 err = -1;
4055 else
4056 err = PyDict_SetItem(locals, name, value);
4057 Py_DECREF(name);
4058 Py_XDECREF(value);
4059 if (err != 0)
4060 break;
4061 }
4062 Py_DECREF(all);
4063 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004064}
4065
Guido van Rossumb209a111997-04-29 18:18:01 +00004066static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004067build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004068{
Guido van Rossum7851eea2001-09-12 19:19:18 +00004069 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004070
4071 if (PyDict_Check(methods))
4072 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00004073 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00004074 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004075 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4076 base = PyTuple_GET_ITEM(bases, 0);
4077 metaclass = PyObject_GetAttrString(base, "__class__");
4078 if (metaclass == NULL) {
4079 PyErr_Clear();
4080 metaclass = (PyObject *)base->ob_type;
4081 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00004082 }
4083 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004084 else {
4085 PyObject *g = PyEval_GetGlobals();
4086 if (g != NULL && PyDict_Check(g))
4087 metaclass = PyDict_GetItemString(g, "__metaclass__");
4088 if (metaclass == NULL)
4089 metaclass = (PyObject *) &PyClass_Type;
4090 Py_INCREF(metaclass);
4091 }
4092 result = PyObject_CallFunction(metaclass, "OOO", name, bases, methods);
4093 Py_DECREF(metaclass);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004094 if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
4095 /* A type error here likely means that the user passed
4096 in a base that was not a class (such the random module
4097 instead of the random.random type). Help them out with
Raymond Hettingercfc31922004-09-16 16:41:57 +00004098 by augmenting the error message with more information.*/
4099
4100 PyObject *ptype, *pvalue, *ptraceback;
4101
4102 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
4103 if (PyString_Check(pvalue)) {
4104 PyObject *newmsg;
4105 newmsg = PyString_FromFormat(
4106 "Error when calling the metaclass bases\n %s",
4107 PyString_AS_STRING(pvalue));
4108 if (newmsg != NULL) {
4109 Py_DECREF(pvalue);
4110 pvalue = newmsg;
4111 }
4112 }
4113 PyErr_Restore(ptype, pvalue, ptraceback);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004114 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004115 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00004116}
4117
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004118static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004119exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
4120 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004121{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004122 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00004123 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004124 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004125
Guido van Rossumb209a111997-04-29 18:18:01 +00004126 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
4127 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004128 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00004129 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004130 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00004131 locals = PyTuple_GetItem(prog, 2);
4132 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004133 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004134 if (globals == Py_None) {
4135 globals = PyEval_GetGlobals();
4136 if (locals == Py_None) {
4137 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00004138 plain = 1;
4139 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004140 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004141 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004142 locals = globals;
Guido van Rossumb209a111997-04-29 18:18:01 +00004143 if (!PyString_Check(prog) &&
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004144 !PyUnicode_Check(prog) &&
Guido van Rossumb209a111997-04-29 18:18:01 +00004145 !PyCode_Check(prog) &&
4146 !PyFile_Check(prog)) {
4147 PyErr_SetString(PyExc_TypeError,
Guido van Rossumac7be682001-01-17 15:42:30 +00004148 "exec: arg 1 must be a string, file, or code object");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004149 return -1;
4150 }
Fred Drake661ea262000-10-24 19:57:45 +00004151 if (!PyDict_Check(globals)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004152 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00004153 "exec: arg 2 must be a dictionary or None");
4154 return -1;
4155 }
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004156 if (!PyMapping_Check(locals)) {
Fred Drake661ea262000-10-24 19:57:45 +00004157 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004158 "exec: arg 3 must be a mapping or None");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004159 return -1;
4160 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004161 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00004162 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00004163 if (PyCode_Check(prog)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +00004164 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4165 PyErr_SetString(PyExc_TypeError,
4166 "code object passed to exec may not contain free variables");
4167 return -1;
4168 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004169 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004170 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004171 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004172 FILE *fp = PyFile_AsFile(prog);
4173 char *name = PyString_AsString(PyFile_Name(prog));
Tim Peters5ba58662001-07-16 02:29:45 +00004174 PyCompilerFlags cf;
4175 cf.cf_flags = 0;
4176 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004177 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004178 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004179 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004180 v = PyRun_File(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004181 locals);
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004182 }
4183 else {
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004184 PyObject *tmp = NULL;
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004185 char *str;
Tim Peters5ba58662001-07-16 02:29:45 +00004186 PyCompilerFlags cf;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004187 cf.cf_flags = 0;
4188#ifdef Py_USING_UNICODE
4189 if (PyUnicode_Check(prog)) {
4190 tmp = PyUnicode_AsUTF8String(prog);
4191 if (tmp == NULL)
4192 return -1;
4193 prog = tmp;
4194 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4195 }
4196#endif
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004197 if (PyString_AsStringAndSize(prog, &str, NULL))
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004198 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004199 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters8a5c3c72004-04-05 19:36:21 +00004200 v = PyRun_StringFlags(str, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004201 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004202 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004203 v = PyRun_String(str, Py_file_input, globals, locals);
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004204 Py_XDECREF(tmp);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004205 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004206 if (plain)
4207 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004208 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004209 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00004210 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004211 return 0;
4212}
Guido van Rossum24c13741995-02-14 09:42:43 +00004213
Guido van Rossumac7be682001-01-17 15:42:30 +00004214static void
Paul Prescode68140d2000-08-30 20:25:01 +00004215format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4216{
4217 char *obj_str;
4218
4219 if (!obj)
4220 return;
4221
4222 obj_str = PyString_AsString(obj);
4223 if (!obj_str)
4224 return;
4225
4226 PyErr_Format(exc, format_str, obj_str);
4227}
Guido van Rossum950361c1997-01-24 13:49:28 +00004228
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004229static PyObject *
4230string_concatenate(PyObject *v, PyObject *w,
4231 PyFrameObject *f, unsigned char *next_instr)
4232{
4233 /* This function implements 'variable += expr' when both arguments
4234 are strings. */
4235
4236 if (v->ob_refcnt == 2) {
4237 /* In the common case, there are 2 references to the value
4238 * stored in 'variable' when the += is performed: one on the
4239 * value stack (in 'v') and one still stored in the 'variable'.
4240 * We try to delete the variable now to reduce the refcnt to 1.
4241 */
4242 switch (*next_instr) {
4243 case STORE_FAST:
4244 {
4245 int oparg = PEEKARG();
4246 PyObject **fastlocals = f->f_localsplus;
4247 if (GETLOCAL(oparg) == v)
4248 SETLOCAL(oparg, NULL);
4249 break;
4250 }
4251 case STORE_DEREF:
4252 {
4253 PyObject **freevars = f->f_localsplus + f->f_nlocals;
4254 PyObject *c = freevars[PEEKARG()];
4255 if (PyCell_GET(c) == v)
4256 PyCell_Set(c, NULL);
4257 break;
4258 }
4259 case STORE_NAME:
4260 {
4261 PyObject *names = f->f_code->co_names;
4262 PyObject *name = GETITEM(names, PEEKARG());
4263 PyObject *locals = f->f_locals;
4264 if (PyDict_CheckExact(locals) &&
4265 PyDict_GetItem(locals, name) == v) {
4266 if (PyDict_DelItem(locals, name) != 0) {
4267 PyErr_Clear();
4268 }
4269 }
4270 break;
4271 }
4272 }
4273 }
4274
Armin Rigo618fbf52004-08-07 20:58:32 +00004275 if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004276 /* Now we own the last reference to 'v', so we can resize it
4277 * in-place.
4278 */
4279 int v_len = PyString_GET_SIZE(v);
4280 int w_len = PyString_GET_SIZE(w);
4281 if (_PyString_Resize(&v, v_len + w_len) != 0) {
4282 /* XXX if _PyString_Resize() fails, 'v' has been
4283 * deallocated so it cannot be put back into 'variable'.
4284 * The MemoryError is raised when there is no value in
4285 * 'variable', which might (very remotely) be a cause
4286 * of incompatibilities.
4287 */
4288 return NULL;
4289 }
4290 /* copy 'w' into the newly allocated area of 'v' */
4291 memcpy(PyString_AS_STRING(v) + v_len,
4292 PyString_AS_STRING(w), w_len);
4293 return v;
4294 }
4295 else {
4296 /* When in-place resizing is not an option. */
4297 PyString_Concat(&v, w);
4298 return v;
4299 }
4300}
4301
Guido van Rossum950361c1997-01-24 13:49:28 +00004302#ifdef DYNAMIC_EXECUTION_PROFILE
4303
Skip Montanarof118cb12001-10-15 20:51:38 +00004304static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004305getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004306{
4307 int i;
4308 PyObject *l = PyList_New(256);
4309 if (l == NULL) return NULL;
4310 for (i = 0; i < 256; i++) {
4311 PyObject *x = PyInt_FromLong(a[i]);
4312 if (x == NULL) {
4313 Py_DECREF(l);
4314 return NULL;
4315 }
4316 PyList_SetItem(l, i, x);
4317 }
4318 for (i = 0; i < 256; i++)
4319 a[i] = 0;
4320 return l;
4321}
4322
4323PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004324_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004325{
4326#ifndef DXPAIRS
4327 return getarray(dxp);
4328#else
4329 int i;
4330 PyObject *l = PyList_New(257);
4331 if (l == NULL) return NULL;
4332 for (i = 0; i < 257; i++) {
4333 PyObject *x = getarray(dxpairs[i]);
4334 if (x == NULL) {
4335 Py_DECREF(l);
4336 return NULL;
4337 }
4338 PyList_SetItem(l, i, x);
4339 }
4340 return l;
4341#endif
4342}
4343
4344#endif