blob: abe157e5c154dfad82d0807a8ffbaa6850a0f54d [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003
Guido van Rossum681d79a1995-07-18 14:51:37 +00004/* XXX TO DO:
Guido van Rossum681d79a1995-07-18 14:51:37 +00005 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +00006 XXX document it!
7 */
8
Fredrik Lundh7a830892006-05-27 10:39:48 +00009/* enable more aggressive intra-module optimizations, where available */
Fredrik Lundh57640f52006-05-26 11:54:04 +000010#define PY_LOCAL_AGGRESSIVE
11
Guido van Rossumb209a111997-04-29 18:18:01 +000012#include "Python.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000013
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000014#include "code.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000015#include "frameobject.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000016#include "eval.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000017#include "opcode.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000018#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000019
Guido van Rossumc6004111993-11-05 10:22:19 +000020#include <ctype.h>
21
Tim Peters7df5e7f2006-05-26 23:14:37 +000022#ifndef WITH_TSC
Michael W. Hudson75eabd22005-01-18 15:56:11 +000023
24#define READ_TIMESTAMP(var)
25
26#else
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000027
28typedef unsigned long long uint64;
29
Michael W. Hudson800ba232004-08-12 18:19:17 +000030#if defined(__ppc__) /* <- Don't know if this is the correct symbol; this
Thomas Wouterse2176022007-09-20 17:35:10 +000031 section should work for GCC on any PowerPC
32 platform, irrespective of OS.
33 POWER? Who knows :-) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000034
Michael W. Hudson75eabd22005-01-18 15:56:11 +000035#define READ_TIMESTAMP(var) ppc_getcounter(&var)
Michael W. Hudson800ba232004-08-12 18:19:17 +000036
Fredrik Lundh7a830892006-05-27 10:39:48 +000037static void
Michael W. Hudson800ba232004-08-12 18:19:17 +000038ppc_getcounter(uint64 *v)
39{
40 register unsigned long tbu, tb, tbu2;
41
42 loop:
43 asm volatile ("mftbu %0" : "=r" (tbu) );
44 asm volatile ("mftb %0" : "=r" (tb) );
45 asm volatile ("mftbu %0" : "=r" (tbu2));
46 if (__builtin_expect(tbu != tbu2, 0)) goto loop;
47
Tim Peters7df5e7f2006-05-26 23:14:37 +000048 /* The slightly peculiar way of writing the next lines is
Michael W. Hudson800ba232004-08-12 18:19:17 +000049 compiled better by GCC than any other way I tried. */
50 ((long*)(v))[0] = tbu;
51 ((long*)(v))[1] = tb;
52}
53
Mark Dickinson648568f2009-10-31 10:14:33 +000054#elif defined(__i386__)
55
56/* this is for linux/x86 (and probably any other GCC/x86 combo) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000057
Michael W. Hudson75eabd22005-01-18 15:56:11 +000058#define READ_TIMESTAMP(val) \
59 __asm__ __volatile__("rdtsc" : "=A" (val))
Michael W. Hudson800ba232004-08-12 18:19:17 +000060
Mark Dickinson648568f2009-10-31 10:14:33 +000061#elif defined(__x86_64__)
62
63/* for gcc/x86_64, the "A" constraint in DI mode means *either* rax *or* rdx;
64 not edx:eax as it does for i386. Since rdtsc puts its result in edx:eax
65 even in 64-bit mode, we need to use "a" and "d" for the lower and upper
66 32-bit pieces of the result. */
67
68#define READ_TIMESTAMP(val) \
69 __asm__ __volatile__("rdtsc" : \
70 "=a" (((int*)&(val))[0]), "=d" (((int*)&(val))[1]));
71
72
73#else
74
75#error "Don't know how to implement timestamp counter for this architecture"
76
Michael W. Hudson800ba232004-08-12 18:19:17 +000077#endif
78
Tim Peters7df5e7f2006-05-26 23:14:37 +000079void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000080 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
81{
82 uint64 intr, inst, loop;
83 PyThreadState *tstate = PyThreadState_Get();
84 if (!tstate->interp->tscdump)
85 return;
86 intr = intr1 - intr0;
87 inst = inst1 - inst0 - intr;
88 loop = loop1 - loop0 - intr;
89 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
90 opcode, ticked, inst, loop);
91}
Michael W. Hudson800ba232004-08-12 18:19:17 +000092
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000093#endif
94
Guido van Rossum04691fc1992-08-12 15:35:34 +000095/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000096/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000097
Guido van Rossum408027e1996-12-30 16:17:54 +000098#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000099/* For debugging the interpreter: */
100#define LLTRACE 1 /* Low-level trace feature */
101#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000102#endif
103
Jeremy Hylton52820442001-01-03 23:52:36 +0000104typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +0000105
Guido van Rossum374a9221991-04-04 10:40:29 +0000106/* Forward declarations */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000107#ifdef WITH_TSC
Fredrik Lundh7a830892006-05-27 10:39:48 +0000108static PyObject * call_function(PyObject ***, int, uint64*, uint64*);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000109#else
Fredrik Lundh7a830892006-05-27 10:39:48 +0000110static PyObject * call_function(PyObject ***, int);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000111#endif
Fredrik Lundh7a830892006-05-27 10:39:48 +0000112static PyObject * fast_function(PyObject *, PyObject ***, int, int, int);
113static PyObject * do_call(PyObject *, PyObject ***, int, int);
114static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int);
Thomas Wouterse2176022007-09-20 17:35:10 +0000115static PyObject * update_keyword_args(PyObject *, int, PyObject ***,
116 PyObject *);
Fredrik Lundh7a830892006-05-27 10:39:48 +0000117static PyObject * update_star_args(int, int, PyObject *, PyObject ***);
118static PyObject * load_args(PyObject ***, int);
Jeremy Hylton52820442001-01-03 23:52:36 +0000119#define CALL_FLAG_VAR 1
120#define CALL_FLAG_KW 2
121
Guido van Rossum0a066c01992-03-27 17:29:15 +0000122#ifdef LLTRACE
Fredrik Lundh1b949402006-05-26 12:01:49 +0000123static int lltrace;
Fredrik Lundh7a830892006-05-27 10:39:48 +0000124static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +0000125#endif
Fredrik Lundh7a830892006-05-27 10:39:48 +0000126static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
Fred Drake5755ce62001-06-27 19:19:46 +0000127 int, PyObject *);
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +0000128static int call_trace_protected(Py_tracefunc, PyObject *,
Armin Rigo1c2d7e52005-09-20 18:34:01 +0000129 PyFrameObject *, int, PyObject *);
Fredrik Lundh7a830892006-05-27 10:39:48 +0000130static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
131static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Armin Rigobf57a142004-03-22 19:24:58 +0000132 PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000133
Fredrik Lundh7a830892006-05-27 10:39:48 +0000134static PyObject * apply_slice(PyObject *, PyObject *, PyObject *);
135static int assign_slice(PyObject *, PyObject *,
Tim Petersdbd9ba62000-07-09 03:09:57 +0000136 PyObject *, PyObject *);
Fredrik Lundh7a830892006-05-27 10:39:48 +0000137static PyObject * cmp_outcome(int, PyObject *, PyObject *);
138static PyObject * import_from(PyObject *, PyObject *);
139static int import_all_from(PyObject *, PyObject *);
140static PyObject * build_class(PyObject *, PyObject *, PyObject *);
141static int exec_statement(PyFrameObject *,
Tim Petersdbd9ba62000-07-09 03:09:57 +0000142 PyObject *, PyObject *, PyObject *);
Fredrik Lundh7a830892006-05-27 10:39:48 +0000143static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *);
144static void reset_exc_info(PyThreadState *);
145static void format_exc_check_arg(PyObject *, char *, PyObject *);
146static PyObject * string_concatenate(PyObject *, PyObject *,
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000147 PyFrameObject *, unsigned char *);
Guido van Rossum374a9221991-04-04 10:40:29 +0000148
Paul Prescode68140d2000-08-30 20:25:01 +0000149#define NAME_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000150 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000151#define GLOBAL_NAME_ERROR_MSG \
152 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000153#define UNBOUNDLOCAL_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000154 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000155#define UNBOUNDFREE_ERROR_MSG \
156 "free variable '%.200s' referenced before assignment" \
157 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000158
Guido van Rossum950361c1997-01-24 13:49:28 +0000159/* Dynamic execution profile */
160#ifdef DYNAMIC_EXECUTION_PROFILE
161#ifdef DXPAIRS
162static long dxpairs[257][256];
163#define dxp dxpairs[256]
164#else
165static long dxp[256];
166#endif
167#endif
168
Jeremy Hylton985eba52003-02-05 23:13:00 +0000169/* Function call profile */
170#ifdef CALL_PROFILE
171#define PCALL_NUM 11
172static int pcall[PCALL_NUM];
173
174#define PCALL_ALL 0
175#define PCALL_FUNCTION 1
176#define PCALL_FAST_FUNCTION 2
177#define PCALL_FASTER_FUNCTION 3
178#define PCALL_METHOD 4
179#define PCALL_BOUND_METHOD 5
180#define PCALL_CFUNCTION 6
181#define PCALL_TYPE 7
182#define PCALL_GENERATOR 8
183#define PCALL_OTHER 9
184#define PCALL_POP 10
185
186/* Notes about the statistics
187
188 PCALL_FAST stats
189
190 FAST_FUNCTION means no argument tuple needs to be created.
191 FASTER_FUNCTION means that the fast-path frame setup code is used.
192
193 If there is a method call where the call can be optimized by changing
194 the argument tuple and calling the function directly, it gets recorded
195 twice.
196
197 As a result, the relationship among the statistics appears to be
198 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
199 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
200 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
201 PCALL_METHOD > PCALL_BOUND_METHOD
202*/
203
204#define PCALL(POS) pcall[POS]++
205
206PyObject *
207PyEval_GetCallStats(PyObject *self)
208{
Andrew M. Kuchling1f3ebe02006-10-27 13:22:46 +0000209 return Py_BuildValue("iiiiiiiiiii",
Jeremy Hylton985eba52003-02-05 23:13:00 +0000210 pcall[0], pcall[1], pcall[2], pcall[3],
211 pcall[4], pcall[5], pcall[6], pcall[7],
Andrew M. Kuchling1f3ebe02006-10-27 13:22:46 +0000212 pcall[8], pcall[9], pcall[10]);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000213}
214#else
215#define PCALL(O)
216
217PyObject *
218PyEval_GetCallStats(PyObject *self)
219{
220 Py_INCREF(Py_None);
221 return Py_None;
222}
223#endif
224
Tim Peters5ca576e2001-06-18 22:08:13 +0000225
Guido van Rossume59214e1994-08-30 08:01:59 +0000226#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000227
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000228#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000229#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000230#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000231#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000232
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000233static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
Guido van Rossuma9672091994-09-14 13:31:22 +0000234static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000235
Tim Peters7f468f22004-10-11 02:40:51 +0000236int
237PyEval_ThreadsInitialized(void)
238{
239 return interpreter_lock != 0;
240}
241
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000242void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000243PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000244{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000245 if (interpreter_lock)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000246 return;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000247 interpreter_lock = PyThread_allocate_lock();
248 PyThread_acquire_lock(interpreter_lock, 1);
249 main_thread = PyThread_get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000250}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000251
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000252void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000253PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000254{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000255 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000256}
257
258void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000259PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000260{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000261 PyThread_release_lock(interpreter_lock);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000262}
263
264void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000265PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000266{
267 if (tstate == NULL)
268 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000269 /* Check someone has called PyEval_InitThreads() to create the lock */
270 assert(interpreter_lock);
Guido van Rossum65d5b571998-12-21 19:32:43 +0000271 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000272 if (PyThreadState_Swap(tstate) != NULL)
273 Py_FatalError(
274 "PyEval_AcquireThread: non-NULL old thread state");
275}
276
277void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000278PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000279{
280 if (tstate == NULL)
281 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
282 if (PyThreadState_Swap(NULL) != tstate)
283 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000284 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000285}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000286
287/* This function is called from PyOS_AfterFork to ensure that newly
288 created child processes don't hold locks referring to threads which
289 are not running in the child process. (This could also be done using
290 pthread_atfork mechanism, at least for the pthreads implementation.) */
291
292void
293PyEval_ReInitThreads(void)
294{
Jesse Noller5e62ca42008-07-16 20:03:47 +0000295 PyObject *threading, *result;
296 PyThreadState *tstate;
297
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000298 if (!interpreter_lock)
299 return;
300 /*XXX Can't use PyThread_free_lock here because it does too
301 much error-checking. Doing this cleanly would require
302 adding a new function to each thread_*.h. Instead, just
303 create a new lock and waste a little bit of memory */
304 interpreter_lock = PyThread_allocate_lock();
305 PyThread_acquire_lock(interpreter_lock, 1);
306 main_thread = PyThread_get_thread_ident();
Jesse Noller5e62ca42008-07-16 20:03:47 +0000307
308 /* Update the threading module with the new state.
309 */
310 tstate = PyThreadState_GET();
311 threading = PyMapping_GetItemString(tstate->interp->modules,
312 "threading");
313 if (threading == NULL) {
314 /* threading not imported */
315 PyErr_Clear();
316 return;
317 }
318 result = PyObject_CallMethod(threading, "_after_fork", NULL);
319 if (result == NULL)
320 PyErr_WriteUnraisable(threading);
321 else
322 Py_DECREF(result);
323 Py_DECREF(threading);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000324}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000325#endif
326
Guido van Rossumff4949e1992-08-05 19:58:53 +0000327/* Functions save_thread and restore_thread are always defined so
328 dynamically loaded modules needn't be compiled separately for use
329 with and without threads: */
330
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000331PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000332PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000333{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000334 PyThreadState *tstate = PyThreadState_Swap(NULL);
335 if (tstate == NULL)
336 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000337#ifdef WITH_THREAD
Guido van Rossumb74eca91997-09-30 22:03:16 +0000338 if (interpreter_lock)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000339 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000340#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000341 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000342}
343
344void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000345PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000346{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000347 if (tstate == NULL)
348 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000349#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000350 if (interpreter_lock) {
Guido van Rossumb74eca91997-09-30 22:03:16 +0000351 int err = errno;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000352 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000353 errno = err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000354 }
355#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000356 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000357}
358
359
Guido van Rossuma9672091994-09-14 13:31:22 +0000360/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
361 signal handlers or Mac I/O completion routines) can schedule calls
362 to a function to be called synchronously.
363 The synchronous function is called with one void* argument.
364 It should return 0 for success or -1 for failure -- failure should
365 be accompanied by an exception.
366
367 If registry succeeds, the registry function returns 0; if it fails
368 (e.g. due to too many pending calls) it returns -1 (without setting
369 an exception condition).
370
371 Note that because registry may occur from within signal handlers,
372 or other asynchronous events, calling malloc() is unsafe!
373
374#ifdef WITH_THREAD
375 Any thread can schedule pending calls, but only the main thread
376 will execute them.
377#endif
378
379 XXX WARNING! ASYNCHRONOUSLY EXECUTING CODE!
380 There are two possible race conditions:
381 (1) nested asynchronous registry calls;
382 (2) registry calls made while pending calls are being processed.
383 While (1) is very unlikely, (2) is a real possibility.
384 The current code is safe against (2), but not against (1).
385 The safety against (2) is derived from the fact that only one
386 thread (the main thread) ever takes things out of the queue.
Guido van Rossuma9672091994-09-14 13:31:22 +0000387
Guido van Rossuma027efa1997-05-05 20:56:21 +0000388 XXX Darn! With the advent of thread state, we should have an array
389 of pending calls per thread in the thread state! Later...
390*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000391
Guido van Rossuma9672091994-09-14 13:31:22 +0000392#define NPENDINGCALLS 32
393static struct {
Thomas Wouters334fb892000-07-25 12:56:38 +0000394 int (*func)(void *);
395 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000396} pendingcalls[NPENDINGCALLS];
397static volatile int pendingfirst = 0;
398static volatile int pendinglast = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000399static volatile int things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000400
401int
Thomas Wouters334fb892000-07-25 12:56:38 +0000402Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000403{
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000404 static volatile int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000405 int i, j;
406 /* XXX Begin critical section */
407 /* XXX If you want this to be safe against nested
408 XXX asynchronous calls, you'll have to work harder! */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000409 if (busy)
410 return -1;
411 busy = 1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000412 i = pendinglast;
413 j = (i + 1) % NPENDINGCALLS;
Guido van Rossum04e70322002-07-17 16:57:13 +0000414 if (j == pendingfirst) {
415 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000416 return -1; /* Queue full */
Guido van Rossum04e70322002-07-17 16:57:13 +0000417 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000418 pendingcalls[i].func = func;
419 pendingcalls[i].arg = arg;
420 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000421
422 _Py_Ticker = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000423 things_to_do = 1; /* Signal main loop */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000424 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000425 /* XXX End critical section */
426 return 0;
427}
428
Guido van Rossum180d7b41994-09-29 09:45:57 +0000429int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000430Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000431{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000432 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000433#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000434 if (main_thread && PyThread_get_thread_ident() != main_thread)
Guido van Rossuma9672091994-09-14 13:31:22 +0000435 return 0;
436#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000437 if (busy)
Guido van Rossum180d7b41994-09-29 09:45:57 +0000438 return 0;
439 busy = 1;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000440 things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000441 for (;;) {
442 int i;
Thomas Wouters334fb892000-07-25 12:56:38 +0000443 int (*func)(void *);
444 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000445 i = pendingfirst;
446 if (i == pendinglast)
447 break; /* Queue empty */
448 func = pendingcalls[i].func;
449 arg = pendingcalls[i].arg;
450 pendingfirst = (i + 1) % NPENDINGCALLS;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000451 if (func(arg) < 0) {
452 busy = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000453 things_to_do = 1; /* We're not done yet */
Guido van Rossuma9672091994-09-14 13:31:22 +0000454 return -1;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000455 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000456 }
Guido van Rossum180d7b41994-09-29 09:45:57 +0000457 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000458 return 0;
459}
460
461
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000462/* The interpreter's recursion limit */
463
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000464#ifndef Py_DEFAULT_RECURSION_LIMIT
465#define Py_DEFAULT_RECURSION_LIMIT 1000
466#endif
467static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
468int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000469
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000470int
471Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000472{
473 return recursion_limit;
474}
475
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000476void
477Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000478{
479 recursion_limit = new_limit;
Thomas Woutersae406c62007-09-19 17:27:43 +0000480 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000481}
482
Armin Rigo2b3eb402003-10-28 12:05:48 +0000483/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
484 if the recursion_depth reaches _Py_CheckRecursionLimit.
485 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
486 to guarantee that _Py_CheckRecursiveCall() is regularly called.
487 Without USE_STACKCHECK, there is no need for this. */
488int
489_Py_CheckRecursiveCall(char *where)
490{
491 PyThreadState *tstate = PyThreadState_GET();
492
493#ifdef USE_STACKCHECK
494 if (PyOS_CheckStack()) {
495 --tstate->recursion_depth;
496 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
497 return -1;
498 }
499#endif
500 if (tstate->recursion_depth > recursion_limit) {
501 --tstate->recursion_depth;
502 PyErr_Format(PyExc_RuntimeError,
503 "maximum recursion depth exceeded%s",
504 where);
505 return -1;
506 }
Thomas Woutersae406c62007-09-19 17:27:43 +0000507 _Py_CheckRecursionLimit = recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000508 return 0;
509}
510
Guido van Rossum374a9221991-04-04 10:40:29 +0000511/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000512enum why_code {
513 WHY_NOT = 0x0001, /* No error */
514 WHY_EXCEPTION = 0x0002, /* Exception occurred */
515 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
516 WHY_RETURN = 0x0008, /* 'return' statement */
517 WHY_BREAK = 0x0010, /* 'break' statement */
518 WHY_CONTINUE = 0x0020, /* 'continue' statement */
519 WHY_YIELD = 0x0040 /* 'yield' operator */
520};
Guido van Rossum374a9221991-04-04 10:40:29 +0000521
Fredrik Lundh7a830892006-05-27 10:39:48 +0000522static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
523static int unpack_iterable(PyObject *, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000524
Jeffrey Yasskind0a70d42008-12-12 21:25:13 +0000525/* Records whether tracing is on for any thread. Counts the number of
526 threads for which tstate->c_tracefunc is non-NULL, so if the value
527 is 0, we know we don't have to check this thread's c_tracefunc.
528 This speeds up the if statement in PyEval_EvalFrameEx() after
529 fast_next_opcode*/
530static int _Py_TracingPossible = 0;
531
Skip Montanarod581d772002-09-03 20:10:45 +0000532/* for manipulating the thread switch and periodic "stuff" - used to be
533 per thread, now just a pair o' globals */
Skip Montanaro99dba272002-09-03 20:19:06 +0000534int _Py_CheckInterval = 100;
535volatile int _Py_Ticker = 100;
Guido van Rossum374a9221991-04-04 10:40:29 +0000536
Guido van Rossumb209a111997-04-29 18:18:01 +0000537PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000538PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000539{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000540 return PyEval_EvalCodeEx(co,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000541 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000542 (PyObject **)NULL, 0,
543 (PyObject **)NULL, 0,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000544 (PyObject **)NULL, 0,
545 NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000546}
547
548
549/* Interpreter main loop */
550
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000551PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000552PyEval_EvalFrame(PyFrameObject *f) {
553 /* This is for backward compatibility with extension modules that
Thomas Wouterse2176022007-09-20 17:35:10 +0000554 used this API; core interpreter code should call
555 PyEval_EvalFrameEx() */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000556 return PyEval_EvalFrameEx(f, 0);
557}
558
559PyObject *
Anthony Baxtera863d332006-04-11 07:43:46 +0000560PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000561{
Guido van Rossum950361c1997-01-24 13:49:28 +0000562#ifdef DXPAIRS
563 int lastopcode = 0;
564#endif
Thomas Wouterse2176022007-09-20 17:35:10 +0000565 register PyObject **stack_pointer; /* Next free slot in value stack */
Guido van Rossum374a9221991-04-04 10:40:29 +0000566 register unsigned char *next_instr;
Armin Rigo8817fcd2004-06-17 10:22:40 +0000567 register int opcode; /* Current opcode */
568 register int oparg; /* Current opcode argument, if any */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000569 register enum why_code why; /* Reason for block stack unwind */
Guido van Rossum374a9221991-04-04 10:40:29 +0000570 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000571 register PyObject *x; /* Result object -- NULL if error */
572 register PyObject *v; /* Temporary objects popped off stack */
573 register PyObject *w;
574 register PyObject *u;
575 register PyObject *t;
Barry Warsaw23c9ec82000-08-21 15:44:01 +0000576 register PyObject *stream = NULL; /* for PRINT opcodes */
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000577 register PyObject **fastlocals, **freevars;
Guido van Rossum014518f1998-11-23 21:09:51 +0000578 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000579 PyThreadState *tstate = PyThreadState_GET();
Tim Peters5ca576e2001-06-18 22:08:13 +0000580 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000581
Tim Peters8a5c3c72004-04-05 19:36:21 +0000582 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000583
584 not (instr_lb <= current_bytecode_offset < instr_ub)
585
Tim Peters8a5c3c72004-04-05 19:36:21 +0000586 is true when the line being executed has changed. The
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000587 initial values are such as to make this false the first
588 time it is tested. */
Armin Rigobf57a142004-03-22 19:24:58 +0000589 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000590
Guido van Rossumd076c731998-10-07 19:42:25 +0000591 unsigned char *first_instr;
Skip Montanaro04d80f82002-08-04 21:03:35 +0000592 PyObject *names;
593 PyObject *consts;
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000594#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000595 /* Make it easier to find out where we are with a debugger */
Tim Peters5ca576e2001-06-18 22:08:13 +0000596 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000597#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000598
Neal Norwitza81d2202002-07-14 00:27:26 +0000599/* Tuple access macros */
600
601#ifndef Py_DEBUG
602#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
603#else
604#define GETITEM(v, i) PyTuple_GetItem((v), (i))
605#endif
606
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000607#ifdef WITH_TSC
608/* Use Pentium timestamp counter to mark certain events:
609 inst0 -- beginning of switch statement for opcode dispatch
610 inst1 -- end of switch statement (may be skipped)
611 loop0 -- the top of the mainloop
Tim Peters7df5e7f2006-05-26 23:14:37 +0000612 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000613 (may be skipped)
614 intr1 -- beginning of long interruption
615 intr2 -- end of long interruption
616
617 Many opcodes call out to helper C functions. In some cases, the
618 time in those functions should be counted towards the time for the
619 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
620 calls another Python function; there's no point in charge all the
621 bytecode executed by the called function to the caller.
622
623 It's hard to make a useful judgement statically. In the presence
624 of operator overloading, it's impossible to tell if a call will
625 execute new Python code or not.
626
627 It's a case-by-case judgement. I'll use intr1 for the following
628 cases:
629
630 EXEC_STMT
631 IMPORT_STAR
632 IMPORT_FROM
633 CALL_FUNCTION (and friends)
634
635 */
636 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
637 int ticked = 0;
638
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000639 READ_TIMESTAMP(inst0);
640 READ_TIMESTAMP(inst1);
641 READ_TIMESTAMP(loop0);
642 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000643
644 /* shut up the compiler */
645 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000646#endif
647
Guido van Rossum374a9221991-04-04 10:40:29 +0000648/* Code access macros */
649
Martin v. Löwis18e16552006-02-15 17:27:45 +0000650#define INSTR_OFFSET() ((int)(next_instr - first_instr))
Guido van Rossum374a9221991-04-04 10:40:29 +0000651#define NEXTOP() (*next_instr++)
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000652#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000653#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
Guido van Rossumd076c731998-10-07 19:42:25 +0000654#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000655#define JUMPBY(x) (next_instr += (x))
656
Raymond Hettingerf606f872003-03-16 03:11:04 +0000657/* OpCode prediction macros
Thomas Wouterse2176022007-09-20 17:35:10 +0000658 Some opcodes tend to come in pairs thus making it possible to
659 predict the second code when the first is run. For example,
660 COMPARE_OP is often followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And,
661 those opcodes are often followed by a POP_TOP.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000662
Raymond Hettingerafae11e2008-07-05 02:11:55 +0000663 Verifying the prediction costs a single high-speed test of a register
Raymond Hettingerac2072922003-03-16 15:41:11 +0000664 variable against a constant. If the pairing was good, then the
Raymond Hettingerafae11e2008-07-05 02:11:55 +0000665 processor's own internal branch predication has a high likelihood of
666 success, resulting in a nearly zero-overhead transition to the
667 next opcode. A successful prediction saves a trip through the eval-loop
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +0000668 including its two unpredictable branches, the HAS_ARG test and the
Raymond Hettingerafae11e2008-07-05 02:11:55 +0000669 switch-case. Combined with the processor's internal branch prediction,
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +0000670 a successful PREDICT has the effect of making the two opcodes run as if
Raymond Hettingerafae11e2008-07-05 02:11:55 +0000671 they were a single new opcode with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000672
Raymond Hettingerafae11e2008-07-05 02:11:55 +0000673 If collecting opcode statistics, your choices are to either keep the
674 predictions turned-on and interpret the results as if some opcodes
675 had been combined or turn-off predictions so that the opcode frequency
676 counter updates for both opcodes.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000677*/
678
Raymond Hettingera7216982004-02-08 19:59:27 +0000679#ifdef DYNAMIC_EXECUTION_PROFILE
680#define PREDICT(op) if (0) goto PRED_##op
681#else
Raymond Hettingerac2072922003-03-16 15:41:11 +0000682#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000683#endif
684
Raymond Hettingerf606f872003-03-16 03:11:04 +0000685#define PREDICTED(op) PRED_##op: next_instr++
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000686#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Raymond Hettingerf606f872003-03-16 03:11:04 +0000687
Guido van Rossum374a9221991-04-04 10:40:29 +0000688/* Stack manipulation macros */
689
Martin v. Löwis18e16552006-02-15 17:27:45 +0000690/* The stack can grow at most MAXINT deep, as co_nlocals and
691 co_stacksize are ints. */
692#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
Guido van Rossum374a9221991-04-04 10:40:29 +0000693#define EMPTY() (STACK_LEVEL() == 0)
694#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000695#define SECOND() (stack_pointer[-2])
696#define THIRD() (stack_pointer[-3])
697#define FOURTH() (stack_pointer[-4])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000698#define SET_TOP(v) (stack_pointer[-1] = (v))
699#define SET_SECOND(v) (stack_pointer[-2] = (v))
700#define SET_THIRD(v) (stack_pointer[-3] = (v))
701#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000702#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000703#define BASIC_PUSH(v) (*stack_pointer++ = (v))
704#define BASIC_POP() (*--stack_pointer)
705
Guido van Rossum96a42c81992-01-12 02:29:51 +0000706#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000707#define PUSH(v) { (void)(BASIC_PUSH(v), \
708 lltrace && prtrace(TOP(), "push")); \
Richard Jonescebbefc2006-05-23 18:28:17 +0000709 assert(STACK_LEVEL() <= co->co_stacksize); }
Thomas Wouterse2176022007-09-20 17:35:10 +0000710#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
711 BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000712#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
713 lltrace && prtrace(TOP(), "stackadj")); \
Richard Jonescebbefc2006-05-23 18:28:17 +0000714 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes52729ac2007-12-14 02:33:57 +0000715#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
716 prtrace((STACK_POINTER)[-1], "ext_pop")), \
717 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000718#else
719#define PUSH(v) BASIC_PUSH(v)
720#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000721#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000722#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000723#endif
724
Guido van Rossum681d79a1995-07-18 14:51:37 +0000725/* Local variable macros */
726
727#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000728
729/* The SETLOCAL() macro must not DECREF the local variable in-place and
730 then store the new value; it must copy the old value to a temporary
731 value, then store the new value, and then DECREF the temporary value.
732 This is because it is possible that during the DECREF the frame is
733 accessed by other code (e.g. a __del__ method or gc.collect()) and the
734 variable would be pointing to already-freed memory. */
735#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
736 GETLOCAL(i) = value; \
737 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000738
Guido van Rossuma027efa1997-05-05 20:56:21 +0000739/* Start of code */
740
Tim Peters5ca576e2001-06-18 22:08:13 +0000741 if (f == NULL)
742 return NULL;
743
Armin Rigo1d313ab2003-10-25 14:33:09 +0000744 /* push frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000745 if (Py_EnterRecursiveCall(""))
Armin Rigo1d313ab2003-10-25 14:33:09 +0000746 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000747
Tim Peters5ca576e2001-06-18 22:08:13 +0000748 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000749
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000750 if (tstate->use_tracing) {
751 if (tstate->c_tracefunc != NULL) {
752 /* tstate->c_tracefunc, if defined, is a
753 function that will be called on *every* entry
754 to a code block. Its return value, if not
755 None, is a function that will be called at
756 the start of each executed line of code.
757 (Actually, the function must return itself
758 in order to continue tracing.) The trace
759 functions are called with three arguments:
760 a pointer to the current frame, a string
761 indicating why the function is called, and
762 an argument which depends on the situation.
763 The global trace function is also called
764 whenever an exception is detected. */
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +0000765 if (call_trace_protected(tstate->c_tracefunc,
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +0000766 tstate->c_traceobj,
767 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000768 /* Trace function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000769 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000770 }
771 }
772 if (tstate->c_profilefunc != NULL) {
773 /* Similar for c_profilefunc, except it needn't
774 return itself and isn't called for "line" events */
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +0000775 if (call_trace_protected(tstate->c_profilefunc,
776 tstate->c_profileobj,
777 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000778 /* Profile function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000779 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000780 }
781 }
782 }
783
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000784 co = f->f_code;
785 names = co->co_names;
786 consts = co->co_consts;
787 fastlocals = f->f_localsplus;
Richard Jonescebbefc2006-05-23 18:28:17 +0000788 freevars = f->f_localsplus + co->co_nlocals;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000789 first_instr = (unsigned char*) PyString_AS_STRING(co->co_code);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000790 /* An explanation is in order for the next line.
791
792 f->f_lasti now refers to the index of the last instruction
793 executed. You might think this was obvious from the name, but
794 this wasn't always true before 2.3! PyFrame_New now sets
795 f->f_lasti to -1 (i.e. the index *before* the first instruction)
796 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +0000797 does work. Promise.
Raymond Hettinger4bd97d42007-01-06 01:14:41 +0000798
799 When the PREDICT() macros are enabled, some opcode pairs follow in
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +0000800 direct succession without updating f->f_lasti. A successful
Raymond Hettinger4bd97d42007-01-06 01:14:41 +0000801 prediction effectively links the two codes together as if they
802 were a single new opcode; accordingly,f->f_lasti will point to
803 the first code in the pair (for instance, GET_ITER followed by
804 FOR_ITER is effectively a single opcode and f->f_lasti will point
805 at to the beginning of the combined pair.)
806 */
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000807 next_instr = first_instr + f->f_lasti + 1;
808 stack_pointer = f->f_stacktop;
809 assert(stack_pointer != NULL);
810 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
811
Tim Peters5ca576e2001-06-18 22:08:13 +0000812#ifdef LLTRACE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000813 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +0000814#endif
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000815#if defined(Py_DEBUG) || defined(LLTRACE)
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000816 filename = PyString_AsString(co->co_filename);
Tim Peters5ca576e2001-06-18 22:08:13 +0000817#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000818
Guido van Rossum374a9221991-04-04 10:40:29 +0000819 why = WHY_NOT;
820 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +0000821 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +0000822 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +0000823
Anthony Baxtera863d332006-04-11 07:43:46 +0000824 if (throwflag) { /* support for generator.throw() */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000825 why = WHY_EXCEPTION;
826 goto on_error;
827 }
Tim Peters7df5e7f2006-05-26 23:14:37 +0000828
Guido van Rossum374a9221991-04-04 10:40:29 +0000829 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000830#ifdef WITH_TSC
831 if (inst1 == 0) {
832 /* Almost surely, the opcode executed a break
833 or a continue, preventing inst1 from being set
834 on the way out of the loop.
835 */
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000836 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000837 loop1 = inst1;
838 }
839 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
840 intr0, intr1);
841 ticked = 0;
842 inst1 = 0;
843 intr0 = 0;
844 intr1 = 0;
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000845 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000846#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000847 assert(stack_pointer >= f->f_valuestack); /* else underflow */
Richard Jonescebbefc2006-05-23 18:28:17 +0000848 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000849
Guido van Rossuma027efa1997-05-05 20:56:21 +0000850 /* Do periodic things. Doing this every time through
851 the loop would add too much overhead, so we do it
852 only every Nth instruction. We also do it if
853 ``things_to_do'' is set, i.e. when an asynchronous
854 event needs attention (e.g. a signal handler or
855 async I/O handler); see Py_AddPendingCall() and
856 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000857
Skip Montanarod581d772002-09-03 20:10:45 +0000858 if (--_Py_Ticker < 0) {
Thomas Woutersae406c62007-09-19 17:27:43 +0000859 if (*next_instr == SETUP_FINALLY) {
860 /* Make the last opcode before
861 a try: finally: block uninterruptable. */
862 goto fast_next_opcode;
863 }
Skip Montanarod581d772002-09-03 20:10:45 +0000864 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000865 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000866#ifdef WITH_TSC
867 ticked = 1;
868#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000869 if (things_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +0000870 if (Py_MakePendingCalls() < 0) {
871 why = WHY_EXCEPTION;
872 goto on_error;
873 }
Kurt B. Kaiser4c79a832004-11-23 18:06:08 +0000874 if (things_to_do)
875 /* MakePendingCalls() didn't succeed.
876 Force early re-execution of this
877 "periodic" code, possibly after
878 a thread switch */
879 _Py_Ticker = 0;
Guido van Rossum8861b741996-07-30 16:49:37 +0000880 }
Guido van Rossume59214e1994-08-30 08:01:59 +0000881#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000882 if (interpreter_lock) {
883 /* Give another thread a chance */
884
Guido van Rossum25ce5661997-08-02 03:10:38 +0000885 if (PyThreadState_Swap(NULL) != tstate)
886 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000887 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000888
889 /* Other threads may run now */
890
Guido van Rossum65d5b571998-12-21 19:32:43 +0000891 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000892 if (PyThreadState_Swap(tstate) != NULL)
893 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000894
895 /* Check for thread interrupts */
896
897 if (tstate->async_exc != NULL) {
898 x = tstate->async_exc;
899 tstate->async_exc = NULL;
900 PyErr_SetNone(x);
901 Py_DECREF(x);
902 why = WHY_EXCEPTION;
903 goto on_error;
904 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000905 }
906#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000907 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000908
Neil Schemenauer63543862002-02-17 19:10:14 +0000909 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +0000910 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +0000911
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000912 /* line-by-line tracing support */
913
Jeffrey Yasskind0a70d42008-12-12 21:25:13 +0000914 if (_Py_TracingPossible &&
915 tstate->c_tracefunc != NULL && !tstate->tracing) {
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000916 /* see maybe_call_line_trace
917 for expository comments */
918 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +0000919
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000920 err = maybe_call_line_trace(tstate->c_tracefunc,
921 tstate->c_traceobj,
Armin Rigobf57a142004-03-22 19:24:58 +0000922 f, &instr_lb, &instr_ub,
923 &instr_prev);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000924 /* Reload possibly changed frame fields */
925 JUMPTO(f->f_lasti);
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000926 if (f->f_stacktop != NULL) {
927 stack_pointer = f->f_stacktop;
928 f->f_stacktop = NULL;
929 }
930 if (err) {
931 /* trace function raised an exception */
932 goto on_error;
933 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000934 }
935
936 /* Extract opcode and argument */
937
Guido van Rossum374a9221991-04-04 10:40:29 +0000938 opcode = NEXTOP();
Armin Rigo8817fcd2004-06-17 10:22:40 +0000939 oparg = 0; /* allows oparg to be stored in a register because
940 it doesn't have to be remembered across a full loop */
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000941 if (HAS_ARG(opcode))
942 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +0000943 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +0000944#ifdef DYNAMIC_EXECUTION_PROFILE
945#ifdef DXPAIRS
946 dxpairs[lastopcode][opcode]++;
947 lastopcode = opcode;
948#endif
949 dxp[opcode]++;
950#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000951
Guido van Rossum96a42c81992-01-12 02:29:51 +0000952#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +0000953 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +0000954
Guido van Rossum96a42c81992-01-12 02:29:51 +0000955 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000956 if (HAS_ARG(opcode)) {
957 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000958 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +0000959 }
960 else {
961 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000962 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +0000963 }
964 }
965#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000966
Guido van Rossum374a9221991-04-04 10:40:29 +0000967 /* Main switch on opcode */
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000968 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +0000969
Guido van Rossum374a9221991-04-04 10:40:29 +0000970 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +0000971
Guido van Rossum374a9221991-04-04 10:40:29 +0000972 /* BEWARE!
973 It is essential that any operation that fails sets either
974 x to NULL, err to nonzero, or why to anything but WHY_NOT,
975 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000976
Guido van Rossum374a9221991-04-04 10:40:29 +0000977 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000978
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000979 case NOP:
980 goto fast_next_opcode;
981
Neil Schemenauer63543862002-02-17 19:10:14 +0000982 case LOAD_FAST:
983 x = GETLOCAL(oparg);
984 if (x != NULL) {
985 Py_INCREF(x);
986 PUSH(x);
987 goto fast_next_opcode;
988 }
989 format_exc_check_arg(PyExc_UnboundLocalError,
990 UNBOUNDLOCAL_ERROR_MSG,
991 PyTuple_GetItem(co->co_varnames, oparg));
992 break;
993
994 case LOAD_CONST:
Skip Montanaro04d80f82002-08-04 21:03:35 +0000995 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +0000996 Py_INCREF(x);
997 PUSH(x);
998 goto fast_next_opcode;
999
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001000 PREDICTED_WITH_ARG(STORE_FAST);
Neil Schemenauer63543862002-02-17 19:10:14 +00001001 case STORE_FAST:
1002 v = POP();
1003 SETLOCAL(oparg, v);
1004 goto fast_next_opcode;
1005
Raymond Hettingerf606f872003-03-16 03:11:04 +00001006 PREDICTED(POP_TOP);
Guido van Rossum374a9221991-04-04 10:40:29 +00001007 case POP_TOP:
1008 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001009 Py_DECREF(v);
Neil Schemenauer63543862002-02-17 19:10:14 +00001010 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001011
Guido van Rossum374a9221991-04-04 10:40:29 +00001012 case ROT_TWO:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001013 v = TOP();
1014 w = SECOND();
1015 SET_TOP(w);
1016 SET_SECOND(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +00001017 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001018
Guido van Rossum374a9221991-04-04 10:40:29 +00001019 case ROT_THREE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001020 v = TOP();
1021 w = SECOND();
1022 x = THIRD();
1023 SET_TOP(w);
1024 SET_SECOND(x);
1025 SET_THIRD(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +00001026 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001027
Thomas Wouters434d0822000-08-24 20:11:32 +00001028 case ROT_FOUR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001029 u = TOP();
1030 v = SECOND();
1031 w = THIRD();
1032 x = FOURTH();
1033 SET_TOP(v);
1034 SET_SECOND(w);
1035 SET_THIRD(x);
1036 SET_FOURTH(u);
Raymond Hettinger080cb322003-03-14 01:37:42 +00001037 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001038
Guido van Rossum374a9221991-04-04 10:40:29 +00001039 case DUP_TOP:
1040 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001041 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001042 PUSH(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +00001043 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001044
Thomas Wouters434d0822000-08-24 20:11:32 +00001045 case DUP_TOPX:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001046 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001047 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +00001048 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001049 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +00001050 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001051 STACKADJ(2);
1052 SET_TOP(x);
1053 SET_SECOND(w);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001054 goto fast_next_opcode;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001055 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001056 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +00001057 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001058 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +00001059 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001060 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +00001061 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001062 STACKADJ(3);
1063 SET_TOP(x);
1064 SET_SECOND(w);
1065 SET_THIRD(v);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001066 goto fast_next_opcode;
Thomas Wouters434d0822000-08-24 20:11:32 +00001067 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001068 Py_FatalError("invalid argument to DUP_TOPX"
1069 " (bytecode corruption?)");
Jeffrey Yasskin69614982008-12-11 05:21:18 +00001070 /* Never returns, so don't bother to set why. */
Tim Peters35ba6892000-10-11 07:04:49 +00001071 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001072
Guido van Rossum374a9221991-04-04 10:40:29 +00001073 case UNARY_POSITIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001074 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001075 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001076 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001077 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001078 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001079 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001080
Guido van Rossum374a9221991-04-04 10:40:29 +00001081 case UNARY_NEGATIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001082 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001083 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001084 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001085 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001086 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001087 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001088
Guido van Rossum374a9221991-04-04 10:40:29 +00001089 case UNARY_NOT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001090 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001091 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001092 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +00001093 if (err == 0) {
1094 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001095 SET_TOP(Py_True);
Guido van Rossumfc490731997-05-06 15:06:49 +00001096 continue;
1097 }
1098 else if (err > 0) {
1099 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001100 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +00001101 err = 0;
1102 continue;
1103 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00001104 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001105 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001106
Guido van Rossum374a9221991-04-04 10:40:29 +00001107 case UNARY_CONVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001108 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001109 x = PyObject_Repr(v);
1110 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001111 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001112 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001113 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001114
Guido van Rossum7928cd71991-10-24 14:59:31 +00001115 case UNARY_INVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001116 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001117 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001118 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001119 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001120 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001121 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001122
Guido van Rossum50564e81996-01-12 01:13:16 +00001123 case BINARY_POWER:
1124 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001125 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001126 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +00001127 Py_DECREF(v);
1128 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001129 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001130 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +00001131 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001132
Guido van Rossum374a9221991-04-04 10:40:29 +00001133 case BINARY_MULTIPLY:
1134 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001135 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001136 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001137 Py_DECREF(v);
1138 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001139 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001140 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001141 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001142
Guido van Rossum374a9221991-04-04 10:40:29 +00001143 case BINARY_DIVIDE:
Tim Peters3caca232001-12-06 06:23:26 +00001144 if (!_Py_QnewFlag) {
1145 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001146 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001147 x = PyNumber_Divide(v, w);
1148 Py_DECREF(v);
1149 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001150 SET_TOP(x);
Tim Peters3caca232001-12-06 06:23:26 +00001151 if (x != NULL) continue;
1152 break;
1153 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001154 /* -Qnew is in effect: fall through to
Tim Peters3caca232001-12-06 06:23:26 +00001155 BINARY_TRUE_DIVIDE */
1156 case BINARY_TRUE_DIVIDE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001157 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001158 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001159 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001160 Py_DECREF(v);
1161 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001162 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001163 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001164 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001165
Guido van Rossum4668b002001-08-08 05:00:18 +00001166 case BINARY_FLOOR_DIVIDE:
1167 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001168 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001169 x = PyNumber_FloorDivide(v, w);
1170 Py_DECREF(v);
1171 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001172 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001173 if (x != NULL) continue;
1174 break;
1175
Guido van Rossum374a9221991-04-04 10:40:29 +00001176 case BINARY_MODULO:
1177 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001178 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001179 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001180 Py_DECREF(v);
1181 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001182 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001183 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001184 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001185
Guido van Rossum374a9221991-04-04 10:40:29 +00001186 case BINARY_ADD:
1187 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001188 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001189 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001190 /* INLINE: int + int */
1191 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001192 a = PyInt_AS_LONG(v);
1193 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001194 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001195 if ((i^a) < 0 && (i^b) < 0)
1196 goto slow_add;
1197 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001198 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001199 else if (PyString_CheckExact(v) &&
1200 PyString_CheckExact(w)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001201 x = string_concatenate(v, w, f, next_instr);
1202 /* string_concatenate consumed the ref to v */
1203 goto skip_decref_vx;
1204 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001205 else {
1206 slow_add:
Guido van Rossumc12da691997-07-17 23:12:42 +00001207 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001208 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001209 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001210 skip_decref_vx:
Guido van Rossumb209a111997-04-29 18:18:01 +00001211 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 Rossum374a9221991-04-04 10:40:29 +00001214 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001215
Guido van Rossum374a9221991-04-04 10:40:29 +00001216 case BINARY_SUBTRACT:
1217 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001218 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001219 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001220 /* INLINE: int - int */
1221 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001222 a = PyInt_AS_LONG(v);
1223 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001224 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001225 if ((i^a) < 0 && (i^~b) < 0)
1226 goto slow_sub;
1227 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001228 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001229 else {
1230 slow_sub:
Guido van Rossumc12da691997-07-17 23:12:42 +00001231 x = PyNumber_Subtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001232 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001233 Py_DECREF(v);
1234 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001235 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001236 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001237 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001238
Guido van Rossum374a9221991-04-04 10:40:29 +00001239 case BINARY_SUBSCR:
1240 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001241 v = TOP();
Tim Petersb1c46982001-10-05 20:41:38 +00001242 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001243 /* INLINE: list[int] */
Neal Norwitz814e9382006-03-02 07:54:28 +00001244 Py_ssize_t i = PyInt_AsSsize_t(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001245 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +00001246 i += PyList_GET_SIZE(v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001247 if (i >= 0 && i < PyList_GET_SIZE(v)) {
Guido van Rossumfa00e951998-07-08 15:02:37 +00001248 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001249 Py_INCREF(x);
1250 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001251 else
1252 goto slow_get;
Guido van Rossumc12da691997-07-17 23:12:42 +00001253 }
1254 else
Raymond Hettinger467a6982004-04-07 11:39:21 +00001255 slow_get:
Guido van Rossumc12da691997-07-17 23:12:42 +00001256 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001257 Py_DECREF(v);
1258 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001259 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001260 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001261 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001262
Guido van Rossum7928cd71991-10-24 14:59:31 +00001263 case BINARY_LSHIFT:
1264 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001265 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001266 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001267 Py_DECREF(v);
1268 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001269 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001270 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001271 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001272
Guido van Rossum7928cd71991-10-24 14:59:31 +00001273 case BINARY_RSHIFT:
1274 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001275 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001276 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001277 Py_DECREF(v);
1278 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001279 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001280 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001281 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001282
Guido van Rossum7928cd71991-10-24 14:59:31 +00001283 case BINARY_AND:
1284 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001285 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001286 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001287 Py_DECREF(v);
1288 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001289 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001290 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001291 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001292
Guido van Rossum7928cd71991-10-24 14:59:31 +00001293 case BINARY_XOR:
1294 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001295 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001296 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001297 Py_DECREF(v);
1298 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001299 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001300 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001301 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001302
Guido van Rossum7928cd71991-10-24 14:59:31 +00001303 case BINARY_OR:
1304 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001305 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001306 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001307 Py_DECREF(v);
1308 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001309 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001310 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001311 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001312
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001313 case LIST_APPEND:
1314 w = POP();
1315 v = POP();
1316 err = PyList_Append(v, w);
1317 Py_DECREF(v);
1318 Py_DECREF(w);
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001319 if (err == 0) {
1320 PREDICT(JUMP_ABSOLUTE);
1321 continue;
1322 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001323 break;
1324
Thomas Wouters434d0822000-08-24 20:11:32 +00001325 case INPLACE_POWER:
1326 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001327 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001328 x = PyNumber_InPlacePower(v, w, Py_None);
1329 Py_DECREF(v);
1330 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001331 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001332 if (x != NULL) continue;
1333 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001334
Thomas Wouters434d0822000-08-24 20:11:32 +00001335 case INPLACE_MULTIPLY:
1336 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001337 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001338 x = PyNumber_InPlaceMultiply(v, w);
1339 Py_DECREF(v);
1340 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001341 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001342 if (x != NULL) continue;
1343 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001344
Thomas Wouters434d0822000-08-24 20:11:32 +00001345 case INPLACE_DIVIDE:
Tim Peters54b11912001-12-25 18:49:11 +00001346 if (!_Py_QnewFlag) {
1347 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001348 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001349 x = PyNumber_InPlaceDivide(v, w);
1350 Py_DECREF(v);
1351 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001352 SET_TOP(x);
Tim Peters54b11912001-12-25 18:49:11 +00001353 if (x != NULL) continue;
1354 break;
1355 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001356 /* -Qnew is in effect: fall through to
Tim Peters54b11912001-12-25 18:49:11 +00001357 INPLACE_TRUE_DIVIDE */
1358 case INPLACE_TRUE_DIVIDE:
Thomas Wouters434d0822000-08-24 20:11:32 +00001359 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001360 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001361 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001362 Py_DECREF(v);
1363 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001364 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001365 if (x != NULL) continue;
1366 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001367
Guido van Rossum4668b002001-08-08 05:00:18 +00001368 case INPLACE_FLOOR_DIVIDE:
1369 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001370 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001371 x = PyNumber_InPlaceFloorDivide(v, w);
1372 Py_DECREF(v);
1373 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001374 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001375 if (x != NULL) continue;
1376 break;
1377
Thomas Wouters434d0822000-08-24 20:11:32 +00001378 case INPLACE_MODULO:
1379 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001380 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001381 x = PyNumber_InPlaceRemainder(v, w);
1382 Py_DECREF(v);
1383 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001384 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001385 if (x != NULL) continue;
1386 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001387
Thomas Wouters434d0822000-08-24 20:11:32 +00001388 case INPLACE_ADD:
1389 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001390 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001391 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001392 /* INLINE: int + int */
1393 register long a, b, i;
1394 a = PyInt_AS_LONG(v);
1395 b = PyInt_AS_LONG(w);
1396 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001397 if ((i^a) < 0 && (i^b) < 0)
1398 goto slow_iadd;
1399 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001400 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001401 else if (PyString_CheckExact(v) &&
1402 PyString_CheckExact(w)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001403 x = string_concatenate(v, w, f, next_instr);
1404 /* string_concatenate consumed the ref to v */
1405 goto skip_decref_v;
1406 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001407 else {
1408 slow_iadd:
Thomas Wouters434d0822000-08-24 20:11:32 +00001409 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001410 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001411 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001412 skip_decref_v:
Thomas Wouters434d0822000-08-24 20:11:32 +00001413 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001414 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001415 if (x != NULL) continue;
1416 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001417
Thomas Wouters434d0822000-08-24 20:11:32 +00001418 case INPLACE_SUBTRACT:
1419 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001420 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001421 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001422 /* INLINE: int - int */
1423 register long a, b, i;
1424 a = PyInt_AS_LONG(v);
1425 b = PyInt_AS_LONG(w);
1426 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001427 if ((i^a) < 0 && (i^~b) < 0)
1428 goto slow_isub;
1429 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001430 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001431 else {
1432 slow_isub:
Thomas Wouters434d0822000-08-24 20:11:32 +00001433 x = PyNumber_InPlaceSubtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001434 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001435 Py_DECREF(v);
1436 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001437 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001438 if (x != NULL) continue;
1439 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001440
Thomas Wouters434d0822000-08-24 20:11:32 +00001441 case INPLACE_LSHIFT:
1442 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001443 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001444 x = PyNumber_InPlaceLshift(v, w);
1445 Py_DECREF(v);
1446 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001447 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001448 if (x != NULL) continue;
1449 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001450
Thomas Wouters434d0822000-08-24 20:11:32 +00001451 case INPLACE_RSHIFT:
1452 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001453 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001454 x = PyNumber_InPlaceRshift(v, w);
1455 Py_DECREF(v);
1456 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001457 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001458 if (x != NULL) continue;
1459 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001460
Thomas Wouters434d0822000-08-24 20:11:32 +00001461 case INPLACE_AND:
1462 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001463 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001464 x = PyNumber_InPlaceAnd(v, w);
1465 Py_DECREF(v);
1466 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001467 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001468 if (x != NULL) continue;
1469 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001470
Thomas Wouters434d0822000-08-24 20:11:32 +00001471 case INPLACE_XOR:
1472 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001473 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001474 x = PyNumber_InPlaceXor(v, w);
1475 Py_DECREF(v);
1476 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001477 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001478 if (x != NULL) continue;
1479 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001480
Thomas Wouters434d0822000-08-24 20:11:32 +00001481 case INPLACE_OR:
1482 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001483 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001484 x = PyNumber_InPlaceOr(v, w);
1485 Py_DECREF(v);
1486 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001487 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001488 if (x != NULL) continue;
1489 break;
1490
Guido van Rossum374a9221991-04-04 10:40:29 +00001491 case SLICE+0:
1492 case SLICE+1:
1493 case SLICE+2:
1494 case SLICE+3:
1495 if ((opcode-SLICE) & 2)
1496 w = POP();
1497 else
1498 w = NULL;
1499 if ((opcode-SLICE) & 1)
1500 v = POP();
1501 else
1502 v = NULL;
Raymond Hettinger663004b2003-01-09 15:24:30 +00001503 u = TOP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001504 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001505 Py_DECREF(u);
1506 Py_XDECREF(v);
1507 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001508 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001509 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001510 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001511
Guido van Rossum374a9221991-04-04 10:40:29 +00001512 case STORE_SLICE+0:
1513 case STORE_SLICE+1:
1514 case STORE_SLICE+2:
1515 case STORE_SLICE+3:
1516 if ((opcode-STORE_SLICE) & 2)
1517 w = POP();
1518 else
1519 w = NULL;
1520 if ((opcode-STORE_SLICE) & 1)
1521 v = POP();
1522 else
1523 v = NULL;
1524 u = POP();
1525 t = POP();
1526 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +00001527 Py_DECREF(t);
1528 Py_DECREF(u);
1529 Py_XDECREF(v);
1530 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001531 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001532 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001533
Guido van Rossum374a9221991-04-04 10:40:29 +00001534 case DELETE_SLICE+0:
1535 case DELETE_SLICE+1:
1536 case DELETE_SLICE+2:
1537 case DELETE_SLICE+3:
1538 if ((opcode-DELETE_SLICE) & 2)
1539 w = POP();
1540 else
1541 w = NULL;
1542 if ((opcode-DELETE_SLICE) & 1)
1543 v = POP();
1544 else
1545 v = NULL;
1546 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001547 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +00001548 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00001549 Py_DECREF(u);
1550 Py_XDECREF(v);
1551 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001552 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001553 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001554
Guido van Rossum374a9221991-04-04 10:40:29 +00001555 case STORE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001556 w = TOP();
1557 v = SECOND();
1558 u = THIRD();
1559 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001560 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001561 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001562 Py_DECREF(u);
1563 Py_DECREF(v);
1564 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001565 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001566 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001567
Guido van Rossum374a9221991-04-04 10:40:29 +00001568 case DELETE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001569 w = TOP();
1570 v = SECOND();
1571 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001572 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001573 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001574 Py_DECREF(v);
1575 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001576 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001577 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001578
Guido van Rossum374a9221991-04-04 10:40:29 +00001579 case PRINT_EXPR:
1580 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001581 w = PySys_GetObject("displayhook");
1582 if (w == NULL) {
1583 PyErr_SetString(PyExc_RuntimeError,
1584 "lost sys.displayhook");
1585 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001586 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001587 }
1588 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001589 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001590 if (x == NULL)
1591 err = -1;
1592 }
1593 if (err == 0) {
1594 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001595 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001596 if (w == NULL)
1597 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001598 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001599 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001600 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001601 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001602
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001603 case PRINT_ITEM_TO:
1604 w = stream = POP();
1605 /* fall through to PRINT_ITEM */
1606
Guido van Rossum374a9221991-04-04 10:40:29 +00001607 case PRINT_ITEM:
1608 v = POP();
Barry Warsaw093abe02000-08-29 04:56:13 +00001609 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001610 w = PySys_GetObject("stdout");
1611 if (w == NULL) {
1612 PyErr_SetString(PyExc_RuntimeError,
1613 "lost sys.stdout");
1614 err = -1;
1615 }
Guido van Rossum8f183201997-12-31 05:53:15 +00001616 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001617 /* PyFile_SoftSpace() can exececute arbitrary code
1618 if sys.stdout is an instance with a __getattr__.
1619 If __getattr__ raises an exception, w will
1620 be freed, so we need to prevent that temporarily. */
1621 Py_XINCREF(w);
Tim Peters8e5fd532002-03-24 19:25:00 +00001622 if (w != NULL && PyFile_SoftSpace(w, 0))
Guido van Rossumbe270261997-05-22 22:26:18 +00001623 err = PyFile_WriteString(" ", w);
1624 if (err == 0)
1625 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001626 if (err == 0) {
Tim Peters8e5fd532002-03-24 19:25:00 +00001627 /* XXX move into writeobject() ? */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001628 if (PyString_Check(v)) {
1629 char *s = PyString_AS_STRING(v);
1630 Py_ssize_t len = PyString_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001631 if (len == 0 ||
1632 !isspace(Py_CHARMASK(s[len-1])) ||
1633 s[len-1] == ' ')
1634 PyFile_SoftSpace(w, 1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001635 }
Martin v. Löwis8d3ce5a2001-12-18 22:36:40 +00001636#ifdef Py_USING_UNICODE
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001637 else if (PyUnicode_Check(v)) {
1638 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
Martin v. Löwis66851282006-04-22 11:40:03 +00001639 Py_ssize_t len = PyUnicode_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001640 if (len == 0 ||
1641 !Py_UNICODE_ISSPACE(s[len-1]) ||
1642 s[len-1] == ' ')
1643 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001644 }
Michael W. Hudsond95c8282002-05-20 13:56:11 +00001645#endif
Tim Peters8e5fd532002-03-24 19:25:00 +00001646 else
1647 PyFile_SoftSpace(w, 1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001648 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001649 Py_XDECREF(w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001650 Py_DECREF(v);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001651 Py_XDECREF(stream);
1652 stream = NULL;
1653 if (err == 0)
1654 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001655 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001656
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001657 case PRINT_NEWLINE_TO:
1658 w = stream = POP();
1659 /* fall through to PRINT_NEWLINE */
1660
Guido van Rossum374a9221991-04-04 10:40:29 +00001661 case PRINT_NEWLINE:
Barry Warsaw093abe02000-08-29 04:56:13 +00001662 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001663 w = PySys_GetObject("stdout");
Jeffrey Yasskin69614982008-12-11 05:21:18 +00001664 if (w == NULL) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001665 PyErr_SetString(PyExc_RuntimeError,
1666 "lost sys.stdout");
Jeffrey Yasskin69614982008-12-11 05:21:18 +00001667 why = WHY_EXCEPTION;
1668 }
Guido van Rossum3165fe61992-09-25 21:59:05 +00001669 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001670 if (w != NULL) {
Georg Brandlaa76d772008-07-01 20:56:03 +00001671 /* w.write() may replace sys.stdout, so we
1672 * have to keep our reference to it */
Amaury Forgeot d'Arcbdd941f2008-07-01 20:38:04 +00001673 Py_INCREF(w);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001674 err = PyFile_WriteString("\n", w);
1675 if (err == 0)
1676 PyFile_SoftSpace(w, 0);
Amaury Forgeot d'Arcbdd941f2008-07-01 20:38:04 +00001677 Py_DECREF(w);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001678 }
1679 Py_XDECREF(stream);
1680 stream = NULL;
Guido van Rossum374a9221991-04-04 10:40:29 +00001681 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001682
Thomas Wouters434d0822000-08-24 20:11:32 +00001683
1684#ifdef CASE_TOO_BIG
1685 default: switch (opcode) {
1686#endif
Guido van Rossumf10570b1995-07-07 22:53:21 +00001687 case RAISE_VARARGS:
1688 u = v = w = NULL;
1689 switch (oparg) {
1690 case 3:
1691 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001692 /* Fallthrough */
1693 case 2:
1694 v = POP(); /* value */
1695 /* Fallthrough */
1696 case 1:
1697 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001698 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001699 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001700 break;
1701 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001702 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001703 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001704 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001705 break;
1706 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001707 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001708
Guido van Rossum374a9221991-04-04 10:40:29 +00001709 case LOAD_LOCALS:
Raymond Hettinger467a6982004-04-07 11:39:21 +00001710 if ((x = f->f_locals) != NULL) {
1711 Py_INCREF(x);
1712 PUSH(x);
1713 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001714 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001715 PyErr_SetString(PyExc_SystemError, "no locals");
Guido van Rossum374a9221991-04-04 10:40:29 +00001716 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001717
Guido van Rossum374a9221991-04-04 10:40:29 +00001718 case RETURN_VALUE:
1719 retval = POP();
1720 why = WHY_RETURN;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001721 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001722
Tim Peters5ca576e2001-06-18 22:08:13 +00001723 case YIELD_VALUE:
1724 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001725 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001726 why = WHY_YIELD;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001727 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001728
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001729 case EXEC_STMT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001730 w = TOP();
1731 v = SECOND();
1732 u = THIRD();
1733 STACKADJ(-3);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001734 READ_TIMESTAMP(intr0);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001735 err = exec_statement(f, u, v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001736 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00001737 Py_DECREF(u);
1738 Py_DECREF(v);
1739 Py_DECREF(w);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001740 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001741
Guido van Rossum374a9221991-04-04 10:40:29 +00001742 case POP_BLOCK:
1743 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001744 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001745 while (STACK_LEVEL() > b->b_level) {
1746 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001747 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001748 }
1749 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001750 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001751
Jeffrey Yasskin9063a992008-03-03 01:27:03 +00001752 PREDICTED(END_FINALLY);
Guido van Rossum374a9221991-04-04 10:40:29 +00001753 case END_FINALLY:
1754 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001755 if (PyInt_Check(v)) {
Raymond Hettinger7c958652004-04-06 10:11:10 +00001756 why = (enum why_code) PyInt_AS_LONG(v);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001757 assert(why != WHY_YIELD);
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00001758 if (why == WHY_RETURN ||
1759 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001760 retval = POP();
1761 }
Thomas Wouterse2176022007-09-20 17:35:10 +00001762 else if (PyExceptionClass_Check(v) ||
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001763 PyString_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001764 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001765 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001766 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001767 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001768 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001769 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001770 else if (v != Py_None) {
1771 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001772 "'finally' pops bad exception");
1773 why = WHY_EXCEPTION;
1774 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001775 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001776 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001777
Guido van Rossum374a9221991-04-04 10:40:29 +00001778 case BUILD_CLASS:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001779 u = TOP();
1780 v = SECOND();
1781 w = THIRD();
1782 STACKADJ(-2);
Guido van Rossum25831651993-05-19 14:50:45 +00001783 x = build_class(u, v, w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001784 SET_TOP(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001785 Py_DECREF(u);
1786 Py_DECREF(v);
1787 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001788 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001789
Guido van Rossum374a9221991-04-04 10:40:29 +00001790 case STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001791 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001792 v = POP();
Raymond Hettinger467a6982004-04-07 11:39:21 +00001793 if ((x = f->f_locals) != NULL) {
Raymond Hettinger66bd2332004-08-02 08:30:07 +00001794 if (PyDict_CheckExact(x))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001795 err = PyDict_SetItem(x, w, v);
1796 else
1797 err = PyObject_SetItem(x, w, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001798 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001799 if (err == 0) continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001800 break;
1801 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001802 PyErr_Format(PyExc_SystemError,
1803 "no locals found when storing %s",
1804 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001805 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001806
Guido van Rossum374a9221991-04-04 10:40:29 +00001807 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001808 w = GETITEM(names, oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001809 if ((x = f->f_locals) != NULL) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001810 if ((err = PyObject_DelItem(x, w)) != 0)
Raymond Hettinger467a6982004-04-07 11:39:21 +00001811 format_exc_check_arg(PyExc_NameError,
Thomas Wouterse2176022007-09-20 17:35:10 +00001812 NAME_ERROR_MSG,
1813 w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001814 break;
1815 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001816 PyErr_Format(PyExc_SystemError,
1817 "no locals when deleting %s",
1818 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001819 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001820
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001821 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001822 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001823 v = POP();
Thomas Wouterse2176022007-09-20 17:35:10 +00001824 if (PyTuple_CheckExact(v) &&
1825 PyTuple_GET_SIZE(v) == oparg) {
1826 PyObject **items = \
1827 ((PyTupleObject *)v)->ob_item;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001828 while (oparg--) {
1829 w = items[oparg];
1830 Py_INCREF(w);
1831 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001832 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001833 Py_DECREF(v);
1834 continue;
Thomas Wouterse2176022007-09-20 17:35:10 +00001835 } else if (PyList_CheckExact(v) &&
1836 PyList_GET_SIZE(v) == oparg) {
1837 PyObject **items = \
1838 ((PyListObject *)v)->ob_item;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001839 while (oparg--) {
1840 w = items[oparg];
1841 Py_INCREF(w);
1842 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001843 }
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001844 } else if (unpack_iterable(v, oparg,
Thomas Wouterse2176022007-09-20 17:35:10 +00001845 stack_pointer + oparg)) {
Tim Petersd6d010b2001-06-21 02:49:55 +00001846 stack_pointer += oparg;
Georg Brandl5cb76c12007-03-21 09:00:39 +00001847 } else {
1848 /* unpack_iterable() raised an exception */
Barry Warsawe42b18f1997-08-25 22:13:04 +00001849 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001850 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001851 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001852 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001853
Guido van Rossum374a9221991-04-04 10:40:29 +00001854 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001855 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001856 v = TOP();
1857 u = SECOND();
1858 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001859 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1860 Py_DECREF(v);
1861 Py_DECREF(u);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001862 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001863 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001864
Guido van Rossum374a9221991-04-04 10:40:29 +00001865 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001866 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001867 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001868 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1869 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001870 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001871 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001872
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001873 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001874 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001875 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001876 err = PyDict_SetItem(f->f_globals, w, v);
1877 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001878 if (err == 0) continue;
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001879 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001880
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001881 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001882 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001883 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001884 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001885 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001886 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001887
Guido van Rossum374a9221991-04-04 10:40:29 +00001888 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001889 w = GETITEM(names, oparg);
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001890 if ((v = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001891 PyErr_Format(PyExc_SystemError,
1892 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001893 PyObject_REPR(w));
Jeffrey Yasskin69614982008-12-11 05:21:18 +00001894 why = WHY_EXCEPTION;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001895 break;
1896 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001897 if (PyDict_CheckExact(v)) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001898 x = PyDict_GetItem(v, w);
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001899 Py_XINCREF(x);
1900 }
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001901 else {
1902 x = PyObject_GetItem(v, w);
1903 if (x == NULL && PyErr_Occurred()) {
Thomas Wouterse2176022007-09-20 17:35:10 +00001904 if (!PyErr_ExceptionMatches(
1905 PyExc_KeyError))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001906 break;
1907 PyErr_Clear();
1908 }
1909 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001910 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001911 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001912 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001913 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001914 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001915 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001916 PyExc_NameError,
Thomas Wouterse2176022007-09-20 17:35:10 +00001917 NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001918 break;
1919 }
1920 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001921 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001922 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001923 PUSH(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001924 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001925
Guido van Rossum374a9221991-04-04 10:40:29 +00001926 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001927 w = GETITEM(names, oparg);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001928 if (PyString_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00001929 /* Inline the PyDict_GetItem() calls.
1930 WARNING: this is an extreme speed hack.
1931 Do not try this at home. */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001932 long hash = ((PyStringObject *)w)->ob_shash;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001933 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001934 PyDictObject *d;
Armin Rigo35f6d362006-06-01 13:19:12 +00001935 PyDictEntry *e;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001936 d = (PyDictObject *)(f->f_globals);
Armin Rigo35f6d362006-06-01 13:19:12 +00001937 e = d->ma_lookup(d, w, hash);
1938 if (e == NULL) {
1939 x = NULL;
1940 break;
1941 }
1942 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001943 if (x != NULL) {
1944 Py_INCREF(x);
1945 PUSH(x);
1946 continue;
1947 }
1948 d = (PyDictObject *)(f->f_builtins);
Armin Rigo35f6d362006-06-01 13:19:12 +00001949 e = d->ma_lookup(d, w, hash);
1950 if (e == NULL) {
1951 x = NULL;
1952 break;
1953 }
1954 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001955 if (x != NULL) {
1956 Py_INCREF(x);
1957 PUSH(x);
1958 continue;
1959 }
1960 goto load_global_error;
1961 }
1962 }
1963 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00001964 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001965 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001966 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001967 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001968 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00001969 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001970 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001971 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001972 break;
1973 }
1974 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001975 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001976 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001977 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001978
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001979 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001980 x = GETLOCAL(oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001981 if (x != NULL) {
1982 SETLOCAL(oparg, NULL);
1983 continue;
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001984 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001985 format_exc_check_arg(
1986 PyExc_UnboundLocalError,
1987 UNBOUNDLOCAL_ERROR_MSG,
1988 PyTuple_GetItem(co->co_varnames, oparg)
1989 );
1990 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001991
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001992 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001993 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001994 Py_INCREF(x);
1995 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001996 if (x != NULL) continue;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001997 break;
1998
1999 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00002000 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002001 w = PyCell_Get(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002002 if (w != NULL) {
2003 PUSH(w);
2004 continue;
Jeremy Hylton2524d692001-02-05 17:23:16 +00002005 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00002006 err = -1;
2007 /* Don't stomp existing exception */
2008 if (PyErr_Occurred())
2009 break;
Richard Jonescebbefc2006-05-23 18:28:17 +00002010 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
2011 v = PyTuple_GET_ITEM(co->co_cellvars,
Raymond Hettinger467a6982004-04-07 11:39:21 +00002012 oparg);
2013 format_exc_check_arg(
2014 PyExc_UnboundLocalError,
2015 UNBOUNDLOCAL_ERROR_MSG,
2016 v);
2017 } else {
Thomas Wouterse2176022007-09-20 17:35:10 +00002018 v = PyTuple_GET_ITEM(co->co_freevars, oparg -
2019 PyTuple_GET_SIZE(co->co_cellvars));
2020 format_exc_check_arg(PyExc_NameError,
2021 UNBOUNDFREE_ERROR_MSG, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002022 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002023 break;
2024
2025 case STORE_DEREF:
2026 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00002027 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002028 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00002029 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002030 continue;
2031
Guido van Rossum374a9221991-04-04 10:40:29 +00002032 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00002033 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002034 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002035 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002036 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002037 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002038 }
2039 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002040 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002041 }
2042 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002043
Guido van Rossum374a9221991-04-04 10:40:29 +00002044 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00002045 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002046 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002047 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002048 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00002049 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002050 }
2051 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002052 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002053 }
2054 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002055
Guido van Rossum374a9221991-04-04 10:40:29 +00002056 case BUILD_MAP:
Raymond Hettingerfd7ed402007-12-18 21:24:09 +00002057 x = _PyDict_NewPresized((Py_ssize_t)oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002058 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002059 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002060 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002061
Raymond Hettingereffde122007-12-18 18:26:18 +00002062 case STORE_MAP:
2063 w = TOP(); /* key */
2064 u = SECOND(); /* value */
2065 v = THIRD(); /* dict */
2066 STACKADJ(-2);
2067 assert (PyDict_CheckExact(v));
2068 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2069 Py_DECREF(u);
2070 Py_DECREF(w);
2071 if (err == 0) continue;
2072 break;
2073
Guido van Rossum374a9221991-04-04 10:40:29 +00002074 case LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00002075 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002076 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002077 x = PyObject_GetAttr(v, w);
2078 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002079 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002080 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002081 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002082
Guido van Rossum374a9221991-04-04 10:40:29 +00002083 case COMPARE_OP:
2084 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002085 v = TOP();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00002086 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00002087 /* INLINE: cmp(int, int) */
2088 register long a, b;
2089 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00002090 a = PyInt_AS_LONG(v);
2091 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00002092 switch (oparg) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00002093 case PyCmp_LT: res = a < b; break;
2094 case PyCmp_LE: res = a <= b; break;
2095 case PyCmp_EQ: res = a == b; break;
2096 case PyCmp_NE: res = a != b; break;
2097 case PyCmp_GT: res = a > b; break;
2098 case PyCmp_GE: res = a >= b; break;
2099 case PyCmp_IS: res = v == w; break;
2100 case PyCmp_IS_NOT: res = v != w; break;
Guido van Rossumc12da691997-07-17 23:12:42 +00002101 default: goto slow_compare;
2102 }
2103 x = res ? Py_True : Py_False;
2104 Py_INCREF(x);
2105 }
2106 else {
2107 slow_compare:
2108 x = cmp_outcome(oparg, v, w);
2109 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002110 Py_DECREF(v);
2111 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002112 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00002113 if (x == NULL) break;
2114 PREDICT(JUMP_IF_FALSE);
2115 PREDICT(JUMP_IF_TRUE);
2116 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002117
Guido van Rossum374a9221991-04-04 10:40:29 +00002118 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00002119 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002120 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002121 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002122 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00002123 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002124 break;
2125 }
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00002126 Py_INCREF(x);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002127 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002128 u = TOP();
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002129 if (PyInt_AsLong(u) != -1 || PyErr_Occurred())
2130 w = PyTuple_Pack(5,
2131 w,
2132 f->f_globals,
2133 f->f_locals == NULL ?
2134 Py_None : f->f_locals,
2135 v,
2136 u);
2137 else
2138 w = PyTuple_Pack(4,
2139 w,
2140 f->f_globals,
2141 f->f_locals == NULL ?
2142 Py_None : f->f_locals,
2143 v);
2144 Py_DECREF(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002145 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002146 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002147 u = POP();
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00002148 Py_DECREF(x);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002149 x = NULL;
2150 break;
2151 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002152 READ_TIMESTAMP(intr0);
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00002153 v = x;
2154 x = PyEval_CallObject(v, w);
2155 Py_DECREF(v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002156 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002157 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002158 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002159 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002160 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002161
Thomas Wouters52152252000-08-17 22:55:00 +00002162 case IMPORT_STAR:
2163 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002164 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002165 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002166 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00002167 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002168 break;
2169 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002170 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002171 err = import_all_from(x, v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002172 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002173 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00002174 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002175 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002176 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002177
Thomas Wouters52152252000-08-17 22:55:00 +00002178 case IMPORT_FROM:
Skip Montanaro496e6582002-08-06 17:47:40 +00002179 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00002180 v = TOP();
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002181 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002182 x = import_from(v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002183 READ_TIMESTAMP(intr1);
Thomas Wouters52152252000-08-17 22:55:00 +00002184 PUSH(x);
2185 if (x != NULL) continue;
2186 break;
2187
Guido van Rossum374a9221991-04-04 10:40:29 +00002188 case JUMP_FORWARD:
2189 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002190 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002191
Raymond Hettingerf606f872003-03-16 03:11:04 +00002192 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002193 case JUMP_IF_FALSE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002194 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002195 if (w == Py_True) {
2196 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002197 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002198 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002199 if (w == Py_False) {
2200 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002201 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002202 }
2203 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002204 if (err > 0)
2205 err = 0;
2206 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00002207 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002208 else
2209 break;
2210 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002211
Raymond Hettingerf606f872003-03-16 03:11:04 +00002212 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002213 case JUMP_IF_TRUE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002214 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002215 if (w == Py_False) {
2216 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002217 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002218 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002219 if (w == Py_True) {
2220 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002221 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002222 }
2223 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002224 if (err > 0) {
2225 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00002226 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002227 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002228 else if (err == 0)
2229 ;
2230 else
2231 break;
2232 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002233
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00002234 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002235 case JUMP_ABSOLUTE:
2236 JUMPTO(oparg);
Raymond Hettingerdc1d1ba2007-11-07 02:45:46 +00002237#if FAST_LOOPS
2238 /* Enabling this path speeds-up all while and for-loops by bypassing
2239 the per-loop checks for signals. By default, this should be turned-off
2240 because it prevents detection of a control-break in tight loops like
2241 "while 1: pass". Compile with this option turned-on when you need
2242 the speed-up and do not need break checking inside tight loops (ones
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00002243 that contain only instructions ending with goto fast_next_opcode).
Raymond Hettingerdc1d1ba2007-11-07 02:45:46 +00002244 */
2245 goto fast_next_opcode;
2246#else
Neil Schemenauerca2a2f12003-05-30 23:59:44 +00002247 continue;
Raymond Hettingerdc1d1ba2007-11-07 02:45:46 +00002248#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002249
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002250 case GET_ITER:
2251 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002252 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002253 x = PyObject_GetIter(v);
2254 Py_DECREF(v);
2255 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002256 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002257 PREDICT(FOR_ITER);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002258 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002259 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002260 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002261 break;
2262
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002263 PREDICTED_WITH_ARG(FOR_ITER);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002264 case FOR_ITER:
2265 /* before: [iter]; after: [iter, iter()] *or* [] */
2266 v = TOP();
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002267 x = (*v->ob_type->tp_iternext)(v);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002268 if (x != NULL) {
2269 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002270 PREDICT(STORE_FAST);
2271 PREDICT(UNPACK_SEQUENCE);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002272 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002273 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002274 if (PyErr_Occurred()) {
Thomas Wouterse2176022007-09-20 17:35:10 +00002275 if (!PyErr_ExceptionMatches(
2276 PyExc_StopIteration))
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002277 break;
2278 PyErr_Clear();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002279 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002280 /* iterator ended normally */
2281 x = v = POP();
2282 Py_DECREF(v);
2283 JUMPBY(oparg);
2284 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002285
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002286 case BREAK_LOOP:
2287 why = WHY_BREAK;
2288 goto fast_block_end;
2289
2290 case CONTINUE_LOOP:
2291 retval = PyInt_FromLong(oparg);
Neal Norwitz02104df2006-05-19 06:31:23 +00002292 if (!retval) {
2293 x = NULL;
2294 break;
2295 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002296 why = WHY_CONTINUE;
2297 goto fast_block_end;
2298
Guido van Rossum374a9221991-04-04 10:40:29 +00002299 case SETUP_LOOP:
2300 case SETUP_EXCEPT:
2301 case SETUP_FINALLY:
Thomas Wouterse2176022007-09-20 17:35:10 +00002302 /* NOTE: If you add any new block-setup opcodes that
2303 are not try/except/finally handlers, you may need
2304 to update the PyGen_NeedsFinalizing() function.
2305 */
Phillip J. Eby2ba96612006-04-10 17:51:05 +00002306
Guido van Rossumb209a111997-04-29 18:18:01 +00002307 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002308 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002309 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002310
Guido van Rossumc2e20742006-02-27 22:32:47 +00002311 case WITH_CLEANUP:
2312 {
Nick Coghlan7af53be2008-03-07 14:13:28 +00002313 /* At the top of the stack are 1-3 values indicating
2314 how/why we entered the finally clause:
2315 - TOP = None
2316 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2317 - TOP = WHY_*; no retval below it
2318 - (TOP, SECOND, THIRD) = exc_info()
2319 Below them is EXIT, the context.__exit__ bound method.
Guido van Rossumc2e20742006-02-27 22:32:47 +00002320 In the last case, we must call
Nick Coghlan7af53be2008-03-07 14:13:28 +00002321 EXIT(TOP, SECOND, THIRD)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002322 otherwise we must call
Nick Coghlan7af53be2008-03-07 14:13:28 +00002323 EXIT(None, None, None)
2324
2325 In all cases, we remove EXIT from the stack, leaving
2326 the rest in the same order.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002327
2328 In addition, if the stack represents an exception,
Guido van Rossumf6694362006-03-10 02:28:35 +00002329 *and* the function call returns a 'true' value, we
2330 "zap" this information, to prevent END_FINALLY from
2331 re-raising the exception. (But non-local gotos
2332 should still be resumed.)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002333 */
Tim Peters7df5e7f2006-05-26 23:14:37 +00002334
Nick Coghlan7af53be2008-03-07 14:13:28 +00002335 PyObject *exit_func;
2336
2337 u = POP();
2338 if (u == Py_None) {
2339 exit_func = TOP();
2340 SET_TOP(u);
2341 v = w = Py_None;
2342 }
2343 else if (PyInt_Check(u)) {
2344 switch(PyInt_AS_LONG(u)) {
2345 case WHY_RETURN:
2346 case WHY_CONTINUE:
2347 /* Retval in TOP. */
2348 exit_func = SECOND();
2349 SET_SECOND(TOP());
2350 SET_TOP(u);
2351 break;
2352 default:
2353 exit_func = TOP();
2354 SET_TOP(u);
2355 break;
2356 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002357 u = v = w = Py_None;
2358 }
2359 else {
Nick Coghlan7af53be2008-03-07 14:13:28 +00002360 v = TOP();
2361 w = SECOND();
2362 exit_func = THIRD();
2363 SET_TOP(u);
2364 SET_SECOND(v);
2365 SET_THIRD(w);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002366 }
Guido van Rossumf6694362006-03-10 02:28:35 +00002367 /* XXX Not the fastest way to call it... */
Nick Coghlan7af53be2008-03-07 14:13:28 +00002368 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2369 NULL);
Amaury Forgeot d'Arc79c9f762008-12-10 23:56:33 +00002370 Py_DECREF(exit_func);
2371 if (x == NULL)
Guido van Rossumf6694362006-03-10 02:28:35 +00002372 break; /* Go to error exit */
Amaury Forgeot d'Arc79c9f762008-12-10 23:56:33 +00002373
2374 if (u != Py_None)
2375 err = PyObject_IsTrue(x);
2376 else
2377 err = 0;
2378 Py_DECREF(x);
2379
2380 if (err < 0)
2381 break; /* Go to error exit */
2382 else if (err > 0) {
2383 err = 0;
Guido van Rossumf6694362006-03-10 02:28:35 +00002384 /* There was an exception and a true return */
Nick Coghlan7af53be2008-03-07 14:13:28 +00002385 STACKADJ(-2);
Guido van Rossumf6694362006-03-10 02:28:35 +00002386 Py_INCREF(Py_None);
2387 SET_TOP(Py_None);
Guido van Rossumf6694362006-03-10 02:28:35 +00002388 Py_DECREF(u);
2389 Py_DECREF(v);
2390 Py_DECREF(w);
2391 } else {
Nick Coghlan7af53be2008-03-07 14:13:28 +00002392 /* The stack was rearranged to remove EXIT
2393 above. Let END_FINALLY do its thing */
Guido van Rossumf6694362006-03-10 02:28:35 +00002394 }
Jeffrey Yasskin9063a992008-03-03 01:27:03 +00002395 PREDICT(END_FINALLY);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002396 break;
2397 }
2398
Guido van Rossumf10570b1995-07-07 22:53:21 +00002399 case CALL_FUNCTION:
Armin Rigo8817fcd2004-06-17 10:22:40 +00002400 {
2401 PyObject **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002402 PCALL(PCALL_ALL);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002403 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002404#ifdef WITH_TSC
Armin Rigo8817fcd2004-06-17 10:22:40 +00002405 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002406#else
Armin Rigo8817fcd2004-06-17 10:22:40 +00002407 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002408#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002409 stack_pointer = sp;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002410 PUSH(x);
2411 if (x != NULL)
2412 continue;
2413 break;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002414 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002415
Jeremy Hylton76901512000-03-28 23:49:17 +00002416 case CALL_FUNCTION_VAR:
2417 case CALL_FUNCTION_KW:
2418 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002419 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002420 int na = oparg & 0xff;
2421 int nk = (oparg>>8) & 0xff;
2422 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002423 int n = na + 2 * nk;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002424 PyObject **pfunc, *func, **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002425 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002426 if (flags & CALL_FLAG_VAR)
2427 n++;
2428 if (flags & CALL_FLAG_KW)
2429 n++;
2430 pfunc = stack_pointer - n - 1;
2431 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002432
Guido van Rossumac7be682001-01-17 15:42:30 +00002433 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002434 && PyMethod_GET_SELF(func) != NULL) {
2435 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002436 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002437 func = PyMethod_GET_FUNCTION(func);
2438 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002439 Py_DECREF(*pfunc);
2440 *pfunc = self;
2441 na++;
2442 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002443 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002444 Py_INCREF(func);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002445 sp = stack_pointer;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002446 READ_TIMESTAMP(intr0);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002447 x = ext_do_call(func, &sp, flags, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002448 READ_TIMESTAMP(intr1);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002449 stack_pointer = sp;
Jeremy Hylton76901512000-03-28 23:49:17 +00002450 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002451
Jeremy Hylton76901512000-03-28 23:49:17 +00002452 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002453 w = POP();
2454 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002455 }
2456 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002457 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002458 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002459 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002460 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002461
Guido van Rossum681d79a1995-07-18 14:51:37 +00002462 case MAKE_FUNCTION:
2463 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002464 x = PyFunction_New(v, f->f_globals);
2465 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002466 /* XXX Maybe this should be a separate opcode? */
2467 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002468 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002469 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002470 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002471 x = NULL;
2472 break;
2473 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002474 while (--oparg >= 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002475 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002476 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002477 }
2478 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002479 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002480 }
2481 PUSH(x);
2482 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002483
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002484 case MAKE_CLOSURE:
2485 {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002486 v = POP(); /* code object */
2487 x = PyFunction_New(v, f->f_globals);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002488 Py_DECREF(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002489 if (x != NULL) {
2490 v = POP();
Jeffrey Yasskin69614982008-12-11 05:21:18 +00002491 if (PyFunction_SetClosure(x, v) != 0) {
2492 /* Can't happen unless bytecode is corrupt. */
2493 why = WHY_EXCEPTION;
2494 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002495 Py_DECREF(v);
2496 }
2497 if (x != NULL && oparg > 0) {
2498 v = PyTuple_New(oparg);
2499 if (v == NULL) {
2500 Py_DECREF(x);
2501 x = NULL;
2502 break;
2503 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002504 while (--oparg >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002505 w = POP();
2506 PyTuple_SET_ITEM(v, oparg, w);
2507 }
Jeffrey Yasskin69614982008-12-11 05:21:18 +00002508 if (PyFunction_SetDefaults(x, v) != 0) {
2509 /* Can't happen unless
2510 PyFunction_SetDefaults changes. */
2511 why = WHY_EXCEPTION;
2512 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002513 Py_DECREF(v);
2514 }
2515 PUSH(x);
2516 break;
2517 }
2518
Guido van Rossum8861b741996-07-30 16:49:37 +00002519 case BUILD_SLICE:
2520 if (oparg == 3)
2521 w = POP();
2522 else
2523 w = NULL;
2524 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002525 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002526 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002527 Py_DECREF(u);
2528 Py_DECREF(v);
2529 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002530 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002531 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002532 break;
2533
Fred Drakeef8ace32000-08-24 00:32:09 +00002534 case EXTENDED_ARG:
2535 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002536 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002537 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002538
Guido van Rossum374a9221991-04-04 10:40:29 +00002539 default:
2540 fprintf(stderr,
2541 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002542 PyCode_Addr2Line(f->f_code, f->f_lasti),
2543 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002544 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002545 why = WHY_EXCEPTION;
2546 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002547
2548#ifdef CASE_TOO_BIG
2549 }
2550#endif
2551
Guido van Rossum374a9221991-04-04 10:40:29 +00002552 } /* switch */
2553
2554 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002555
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002556 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002557
Guido van Rossum374a9221991-04-04 10:40:29 +00002558 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002559
Guido van Rossum374a9221991-04-04 10:40:29 +00002560 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002561 if (err == 0 && x != NULL) {
2562#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002563 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002564 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002565 fprintf(stderr,
2566 "XXX undetected error\n");
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002567 else {
2568#endif
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002569 READ_TIMESTAMP(loop1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002570 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002571#ifdef CHECKEXC
2572 }
2573#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +00002574 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002575 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002576 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002577 err = 0;
2578 }
2579
Guido van Rossum374a9221991-04-04 10:40:29 +00002580 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002581
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002582 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002583 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002584 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002585 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002586 why = WHY_EXCEPTION;
2587 }
2588 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002589#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002590 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002591 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002592 if (PyErr_Occurred()) {
Neal Norwitz8250fbe2008-01-27 17:12:15 +00002593 char buf[128];
Jeremy Hylton904ed862003-11-05 17:29:35 +00002594 sprintf(buf, "Stack unwind with exception "
2595 "set and why=%d", why);
2596 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002597 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002598 }
2599#endif
2600
2601 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002602
Guido van Rossum374a9221991-04-04 10:40:29 +00002603 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002604 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002605
Fred Drake8f51f542001-10-04 14:48:42 +00002606 if (tstate->c_tracefunc != NULL)
2607 call_exc_trace(tstate->c_tracefunc,
2608 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002609 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002610
Guido van Rossum374a9221991-04-04 10:40:29 +00002611 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002612
Guido van Rossum374a9221991-04-04 10:40:29 +00002613 if (why == WHY_RERAISE)
2614 why = WHY_EXCEPTION;
2615
2616 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002617
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002618fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002619 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002620 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002621
Tim Peters8a5c3c72004-04-05 19:36:21 +00002622 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002623 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2624 /* For a continue inside a try block,
2625 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002626 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2627 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002628 why = WHY_NOT;
2629 JUMPTO(PyInt_AS_LONG(retval));
2630 Py_DECREF(retval);
2631 break;
2632 }
2633
Guido van Rossum374a9221991-04-04 10:40:29 +00002634 while (STACK_LEVEL() > b->b_level) {
2635 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002636 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002637 }
2638 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2639 why = WHY_NOT;
2640 JUMPTO(b->b_handler);
2641 break;
2642 }
2643 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002644 (b->b_type == SETUP_EXCEPT &&
2645 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002646 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002647 PyObject *exc, *val, *tb;
2648 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002649 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002650 val = Py_None;
2651 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002652 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002653 /* Make the raw exception data
2654 available to the handler,
2655 so a program can emulate the
2656 Python main loop. Don't do
2657 this for 'finally'. */
2658 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002659 PyErr_NormalizeException(
2660 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002661 set_exc_info(tstate,
2662 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002663 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002664 if (tb == NULL) {
2665 Py_INCREF(Py_None);
2666 PUSH(Py_None);
2667 } else
2668 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002669 PUSH(val);
2670 PUSH(exc);
2671 }
2672 else {
Raymond Hettinger06032cb2004-04-06 09:37:35 +00002673 if (why & (WHY_RETURN | WHY_CONTINUE))
Guido van Rossum374a9221991-04-04 10:40:29 +00002674 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002675 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002676 PUSH(v);
2677 }
2678 why = WHY_NOT;
2679 JUMPTO(b->b_handler);
2680 break;
2681 }
2682 } /* unwind stack */
2683
2684 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002685
Guido van Rossum374a9221991-04-04 10:40:29 +00002686 if (why != WHY_NOT)
2687 break;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002688 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002689
Guido van Rossum374a9221991-04-04 10:40:29 +00002690 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002691
Tim Peters8a5c3c72004-04-05 19:36:21 +00002692 assert(why != WHY_YIELD);
2693 /* Pop remaining stack entries. */
2694 while (!EMPTY()) {
2695 v = POP();
2696 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002697 }
2698
Tim Peters8a5c3c72004-04-05 19:36:21 +00002699 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002700 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002701
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002702fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002703 if (tstate->use_tracing) {
Barry Warsawe2eca0b2005-08-15 18:14:19 +00002704 if (tstate->c_tracefunc) {
2705 if (why == WHY_RETURN || why == WHY_YIELD) {
2706 if (call_trace(tstate->c_tracefunc,
2707 tstate->c_traceobj, f,
2708 PyTrace_RETURN, retval)) {
2709 Py_XDECREF(retval);
2710 retval = NULL;
2711 why = WHY_EXCEPTION;
2712 }
2713 }
2714 else if (why == WHY_EXCEPTION) {
2715 call_trace_protected(tstate->c_tracefunc,
2716 tstate->c_traceobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002717 PyTrace_RETURN, NULL);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002718 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002719 }
Fred Drake8f51f542001-10-04 14:48:42 +00002720 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002721 if (why == WHY_EXCEPTION)
2722 call_trace_protected(tstate->c_profilefunc,
2723 tstate->c_profileobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002724 PyTrace_RETURN, NULL);
Fred Drake4ec5d562001-10-04 19:26:43 +00002725 else if (call_trace(tstate->c_profilefunc,
2726 tstate->c_profileobj, f,
2727 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002728 Py_XDECREF(retval);
2729 retval = NULL;
2730 why = WHY_EXCEPTION;
2731 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002732 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002733 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002734
Tim Peters7df5e7f2006-05-26 23:14:37 +00002735 if (tstate->frame->f_exc_type != NULL)
2736 reset_exc_info(tstate);
2737 else {
2738 assert(tstate->frame->f_exc_value == NULL);
2739 assert(tstate->frame->f_exc_traceback == NULL);
2740 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00002741
Tim Peters5ca576e2001-06-18 22:08:13 +00002742 /* pop frame */
Thomas Woutersae406c62007-09-19 17:27:43 +00002743exit_eval_frame:
Armin Rigo2b3eb402003-10-28 12:05:48 +00002744 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002745 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002746
Guido van Rossum96a42c81992-01-12 02:29:51 +00002747 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002748}
2749
Guido van Rossumc2e20742006-02-27 22:32:47 +00002750/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002751 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00002752 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002753
Tim Peters6d6c1a32001-08-02 04:15:00 +00002754PyObject *
2755PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002756 PyObject **args, int argcount, PyObject **kws, int kwcount,
2757 PyObject **defs, int defcount, PyObject *closure)
2758{
2759 register PyFrameObject *f;
2760 register PyObject *retval = NULL;
2761 register PyObject **fastlocals, **freevars;
2762 PyThreadState *tstate = PyThreadState_GET();
2763 PyObject *x, *u;
2764
2765 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00002766 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002767 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002768 return NULL;
2769 }
2770
Neal Norwitzdf6a6492006-08-13 18:10:10 +00002771 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00002772 assert(globals != NULL);
2773 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002774 if (f == NULL)
2775 return NULL;
2776
2777 fastlocals = f->f_localsplus;
Richard Jonescebbefc2006-05-23 18:28:17 +00002778 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00002779
2780 if (co->co_argcount > 0 ||
2781 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2782 int i;
2783 int n = argcount;
2784 PyObject *kwdict = NULL;
2785 if (co->co_flags & CO_VARKEYWORDS) {
2786 kwdict = PyDict_New();
2787 if (kwdict == NULL)
2788 goto fail;
2789 i = co->co_argcount;
2790 if (co->co_flags & CO_VARARGS)
2791 i++;
2792 SETLOCAL(i, kwdict);
2793 }
2794 if (argcount > co->co_argcount) {
2795 if (!(co->co_flags & CO_VARARGS)) {
2796 PyErr_Format(PyExc_TypeError,
2797 "%.200s() takes %s %d "
2798 "%sargument%s (%d given)",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002799 PyString_AsString(co->co_name),
Tim Peters5ca576e2001-06-18 22:08:13 +00002800 defcount ? "at most" : "exactly",
2801 co->co_argcount,
2802 kwcount ? "non-keyword " : "",
2803 co->co_argcount == 1 ? "" : "s",
2804 argcount);
2805 goto fail;
2806 }
2807 n = co->co_argcount;
2808 }
2809 for (i = 0; i < n; i++) {
2810 x = args[i];
2811 Py_INCREF(x);
2812 SETLOCAL(i, x);
2813 }
2814 if (co->co_flags & CO_VARARGS) {
2815 u = PyTuple_New(argcount - n);
2816 if (u == NULL)
2817 goto fail;
2818 SETLOCAL(co->co_argcount, u);
2819 for (i = n; i < argcount; i++) {
2820 x = args[i];
2821 Py_INCREF(x);
2822 PyTuple_SET_ITEM(u, i-n, x);
2823 }
2824 }
2825 for (i = 0; i < kwcount; i++) {
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00002826 PyObject **co_varnames;
Tim Peters5ca576e2001-06-18 22:08:13 +00002827 PyObject *keyword = kws[2*i];
2828 PyObject *value = kws[2*i + 1];
2829 int j;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002830 if (keyword == NULL || !PyString_Check(keyword)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002831 PyErr_Format(PyExc_TypeError,
2832 "%.200s() keywords must be strings",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002833 PyString_AsString(co->co_name));
Tim Peters5ca576e2001-06-18 22:08:13 +00002834 goto fail;
2835 }
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00002836 /* Speed hack: do raw pointer compares. As names are
2837 normally interned this should almost always hit. */
2838 co_varnames = PySequence_Fast_ITEMS(co->co_varnames);
Tim Peters5ca576e2001-06-18 22:08:13 +00002839 for (j = 0; j < co->co_argcount; j++) {
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00002840 PyObject *nm = co_varnames[j];
2841 if (nm == keyword)
2842 goto kw_found;
2843 }
2844 /* Slow fallback, just in case */
2845 for (j = 0; j < co->co_argcount; j++) {
2846 PyObject *nm = co_varnames[j];
Tim Peters5ca576e2001-06-18 22:08:13 +00002847 int cmp = PyObject_RichCompareBool(
2848 keyword, nm, Py_EQ);
2849 if (cmp > 0)
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00002850 goto kw_found;
Tim Peters5ca576e2001-06-18 22:08:13 +00002851 else if (cmp < 0)
2852 goto fail;
2853 }
2854 /* Check errors from Compare */
2855 if (PyErr_Occurred())
2856 goto fail;
2857 if (j >= co->co_argcount) {
2858 if (kwdict == NULL) {
2859 PyErr_Format(PyExc_TypeError,
2860 "%.200s() got an unexpected "
2861 "keyword argument '%.400s'",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002862 PyString_AsString(co->co_name),
2863 PyString_AsString(keyword));
Tim Peters5ca576e2001-06-18 22:08:13 +00002864 goto fail;
2865 }
2866 PyDict_SetItem(kwdict, keyword, value);
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00002867 continue;
Tim Peters5ca576e2001-06-18 22:08:13 +00002868 }
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00002869kw_found:
2870 if (GETLOCAL(j) != NULL) {
2871 PyErr_Format(PyExc_TypeError,
2872 "%.200s() got multiple "
2873 "values for keyword "
2874 "argument '%.400s'",
2875 PyString_AsString(co->co_name),
2876 PyString_AsString(keyword));
2877 goto fail;
Tim Peters5ca576e2001-06-18 22:08:13 +00002878 }
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00002879 Py_INCREF(value);
2880 SETLOCAL(j, value);
Tim Peters5ca576e2001-06-18 22:08:13 +00002881 }
2882 if (argcount < co->co_argcount) {
2883 int m = co->co_argcount - defcount;
2884 for (i = argcount; i < m; i++) {
2885 if (GETLOCAL(i) == NULL) {
2886 PyErr_Format(PyExc_TypeError,
2887 "%.200s() takes %s %d "
2888 "%sargument%s (%d given)",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002889 PyString_AsString(co->co_name),
Tim Peters5ca576e2001-06-18 22:08:13 +00002890 ((co->co_flags & CO_VARARGS) ||
2891 defcount) ? "at least"
2892 : "exactly",
2893 m, kwcount ? "non-keyword " : "",
2894 m == 1 ? "" : "s", i);
2895 goto fail;
2896 }
2897 }
2898 if (n > m)
2899 i = n - m;
2900 else
2901 i = 0;
2902 for (; i < defcount; i++) {
2903 if (GETLOCAL(m+i) == NULL) {
2904 PyObject *def = defs[i];
2905 Py_INCREF(def);
2906 SETLOCAL(m+i, def);
2907 }
2908 }
2909 }
2910 }
2911 else {
2912 if (argcount > 0 || kwcount > 0) {
2913 PyErr_Format(PyExc_TypeError,
2914 "%.200s() takes no arguments (%d given)",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002915 PyString_AsString(co->co_name),
Tim Peters5ca576e2001-06-18 22:08:13 +00002916 argcount + kwcount);
2917 goto fail;
2918 }
2919 }
2920 /* Allocate and initialize storage for cell vars, and copy free
2921 vars into frame. This isn't too efficient right now. */
Richard Jonescebbefc2006-05-23 18:28:17 +00002922 if (PyTuple_GET_SIZE(co->co_cellvars)) {
Neal Norwitz245ce8d2006-06-12 02:16:10 +00002923 int i, j, nargs, found;
Tim Peters5ca576e2001-06-18 22:08:13 +00002924 char *cellname, *argname;
2925 PyObject *c;
2926
2927 nargs = co->co_argcount;
2928 if (co->co_flags & CO_VARARGS)
2929 nargs++;
2930 if (co->co_flags & CO_VARKEYWORDS)
2931 nargs++;
2932
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002933 /* Initialize each cell var, taking into account
2934 cell vars that are initialized from arguments.
2935
2936 Should arrange for the compiler to put cellvars
2937 that are arguments at the beginning of the cellvars
2938 list so that we can march over it more efficiently?
2939 */
Richard Jonescebbefc2006-05-23 18:28:17 +00002940 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002941 cellname = PyString_AS_STRING(
Tim Peters5ca576e2001-06-18 22:08:13 +00002942 PyTuple_GET_ITEM(co->co_cellvars, i));
2943 found = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002944 for (j = 0; j < nargs; j++) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002945 argname = PyString_AS_STRING(
Tim Peters5ca576e2001-06-18 22:08:13 +00002946 PyTuple_GET_ITEM(co->co_varnames, j));
2947 if (strcmp(cellname, argname) == 0) {
2948 c = PyCell_New(GETLOCAL(j));
2949 if (c == NULL)
2950 goto fail;
Richard Jonescebbefc2006-05-23 18:28:17 +00002951 GETLOCAL(co->co_nlocals + i) = c;
Tim Peters5ca576e2001-06-18 22:08:13 +00002952 found = 1;
2953 break;
2954 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002955 }
2956 if (found == 0) {
2957 c = PyCell_New(NULL);
2958 if (c == NULL)
2959 goto fail;
Richard Jonescebbefc2006-05-23 18:28:17 +00002960 SETLOCAL(co->co_nlocals + i, c);
Tim Peters5ca576e2001-06-18 22:08:13 +00002961 }
2962 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002963 }
Richard Jonescebbefc2006-05-23 18:28:17 +00002964 if (PyTuple_GET_SIZE(co->co_freevars)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002965 int i;
Richard Jonescebbefc2006-05-23 18:28:17 +00002966 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002967 PyObject *o = PyTuple_GET_ITEM(closure, i);
2968 Py_INCREF(o);
Richard Jonescebbefc2006-05-23 18:28:17 +00002969 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Tim Peters5ca576e2001-06-18 22:08:13 +00002970 }
2971 }
2972
Tim Peters5ca576e2001-06-18 22:08:13 +00002973 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002974 /* Don't need to keep the reference to f_back, it will be set
2975 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002976 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002977 f->f_back = NULL;
2978
Jeremy Hylton985eba52003-02-05 23:13:00 +00002979 PCALL(PCALL_GENERATOR);
2980
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002981 /* Create a new generator that owns the ready to run frame
2982 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00002983 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00002984 }
2985
Thomas Woutersae406c62007-09-19 17:27:43 +00002986 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00002987
Thomas Woutersae406c62007-09-19 17:27:43 +00002988fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00002989
Tim Petersb13680b2001-11-27 23:29:29 +00002990 /* decref'ing the frame can cause __del__ methods to get invoked,
2991 which can call back into Python. While we're done with the
2992 current Python frame (f), the associated C stack is still in use,
2993 so recursion_depth must be boosted for the duration.
2994 */
2995 assert(tstate != NULL);
2996 ++tstate->recursion_depth;
Thomas Woutersae406c62007-09-19 17:27:43 +00002997 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00002998 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002999 return retval;
3000}
3001
3002
Guido van Rossumc9fbb722003-03-01 03:36:33 +00003003/* Implementation notes for set_exc_info() and reset_exc_info():
3004
3005- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
3006 'exc_traceback'. These always travel together.
3007
3008- tstate->curexc_ZZZ is the "hot" exception that is set by
3009 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
3010
3011- Once an exception is caught by an except clause, it is transferred
3012 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
3013 can pick it up. This is the primary task of set_exc_info().
Tim Peters7df5e7f2006-05-26 23:14:37 +00003014 XXX That can't be right: set_exc_info() doesn't look at tstate->curexc_ZZZ.
Guido van Rossumc9fbb722003-03-01 03:36:33 +00003015
3016- Now let me explain the complicated dance with frame->f_exc_ZZZ.
3017
3018 Long ago, when none of this existed, there were just a few globals:
3019 one set corresponding to the "hot" exception, and one set
3020 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
3021 globals; they were simply stored as sys.exc_ZZZ. For backwards
3022 compatibility, they still are!) The problem was that in code like
3023 this:
3024
3025 try:
3026 "something that may fail"
3027 except "some exception":
3028 "do something else first"
3029 "print the exception from sys.exc_ZZZ."
3030
3031 if "do something else first" invoked something that raised and caught
3032 an exception, sys.exc_ZZZ were overwritten. That was a frequent
3033 cause of subtle bugs. I fixed this by changing the semantics as
3034 follows:
3035
3036 - Within one frame, sys.exc_ZZZ will hold the last exception caught
3037 *in that frame*.
3038
3039 - But initially, and as long as no exception is caught in a given
3040 frame, sys.exc_ZZZ will hold the last exception caught in the
3041 previous frame (or the frame before that, etc.).
3042
3043 The first bullet fixed the bug in the above example. The second
3044 bullet was for backwards compatibility: it was (and is) common to
3045 have a function that is called when an exception is caught, and to
3046 have that function access the caught exception via sys.exc_ZZZ.
3047 (Example: traceback.print_exc()).
3048
3049 At the same time I fixed the problem that sys.exc_ZZZ weren't
3050 thread-safe, by introducing sys.exc_info() which gets it from tstate;
3051 but that's really a separate improvement.
3052
3053 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
3054 variables to what they were before the current frame was called. The
3055 set_exc_info() function saves them on the frame so that
3056 reset_exc_info() can restore them. The invariant is that
3057 frame->f_exc_ZZZ is NULL iff the current frame never caught an
3058 exception (where "catching" an exception applies only to successful
3059 except clauses); and if the current frame ever caught an exception,
3060 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
3061 at the start of the current frame.
3062
3063*/
3064
Fredrik Lundh7a830892006-05-27 10:39:48 +00003065static void
Guido van Rossumac7be682001-01-17 15:42:30 +00003066set_exc_info(PyThreadState *tstate,
3067 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00003068{
Tim Peters7df5e7f2006-05-26 23:14:37 +00003069 PyFrameObject *frame = tstate->frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003070 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00003071
Tim Peters7df5e7f2006-05-26 23:14:37 +00003072 assert(type != NULL);
3073 assert(frame != NULL);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003074 if (frame->f_exc_type == NULL) {
Tim Peters7df5e7f2006-05-26 23:14:37 +00003075 assert(frame->f_exc_value == NULL);
3076 assert(frame->f_exc_traceback == NULL);
3077 /* This frame didn't catch an exception before. */
3078 /* Save previous exception of this thread in this frame. */
Guido van Rossuma027efa1997-05-05 20:56:21 +00003079 if (tstate->exc_type == NULL) {
Tim Peters7df5e7f2006-05-26 23:14:37 +00003080 /* XXX Why is this set to Py_None? */
Guido van Rossuma027efa1997-05-05 20:56:21 +00003081 Py_INCREF(Py_None);
3082 tstate->exc_type = Py_None;
3083 }
Tim Peters7df5e7f2006-05-26 23:14:37 +00003084 Py_INCREF(tstate->exc_type);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003085 Py_XINCREF(tstate->exc_value);
3086 Py_XINCREF(tstate->exc_traceback);
3087 frame->f_exc_type = tstate->exc_type;
3088 frame->f_exc_value = tstate->exc_value;
3089 frame->f_exc_traceback = tstate->exc_traceback;
3090 }
Tim Peters7df5e7f2006-05-26 23:14:37 +00003091 /* Set new exception for this thread. */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003092 tmp_type = tstate->exc_type;
3093 tmp_value = tstate->exc_value;
3094 tmp_tb = tstate->exc_traceback;
Tim Peters7df5e7f2006-05-26 23:14:37 +00003095 Py_INCREF(type);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003096 Py_XINCREF(value);
3097 Py_XINCREF(tb);
3098 tstate->exc_type = type;
3099 tstate->exc_value = value;
3100 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003101 Py_XDECREF(tmp_type);
3102 Py_XDECREF(tmp_value);
3103 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003104 /* For b/w compatibility */
3105 PySys_SetObject("exc_type", type);
3106 PySys_SetObject("exc_value", value);
3107 PySys_SetObject("exc_traceback", tb);
3108}
3109
Fredrik Lundh7a830892006-05-27 10:39:48 +00003110static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003111reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00003112{
3113 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003114 PyObject *tmp_type, *tmp_value, *tmp_tb;
Tim Peters7df5e7f2006-05-26 23:14:37 +00003115
3116 /* It's a precondition that the thread state's frame caught an
3117 * exception -- verify in a debug build.
3118 */
3119 assert(tstate != NULL);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003120 frame = tstate->frame;
Tim Peters7df5e7f2006-05-26 23:14:37 +00003121 assert(frame != NULL);
3122 assert(frame->f_exc_type != NULL);
3123
3124 /* Copy the frame's exception info back to the thread state. */
3125 tmp_type = tstate->exc_type;
3126 tmp_value = tstate->exc_value;
3127 tmp_tb = tstate->exc_traceback;
3128 Py_INCREF(frame->f_exc_type);
3129 Py_XINCREF(frame->f_exc_value);
3130 Py_XINCREF(frame->f_exc_traceback);
3131 tstate->exc_type = frame->f_exc_type;
3132 tstate->exc_value = frame->f_exc_value;
3133 tstate->exc_traceback = frame->f_exc_traceback;
3134 Py_XDECREF(tmp_type);
3135 Py_XDECREF(tmp_value);
3136 Py_XDECREF(tmp_tb);
3137
3138 /* For b/w compatibility */
3139 PySys_SetObject("exc_type", frame->f_exc_type);
3140 PySys_SetObject("exc_value", frame->f_exc_value);
3141 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
3142
3143 /* Clear the frame's exception info. */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003144 tmp_type = frame->f_exc_type;
3145 tmp_value = frame->f_exc_value;
3146 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003147 frame->f_exc_type = NULL;
3148 frame->f_exc_value = NULL;
3149 frame->f_exc_traceback = NULL;
Tim Peters7df5e7f2006-05-26 23:14:37 +00003150 Py_DECREF(tmp_type);
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003151 Py_XDECREF(tmp_value);
3152 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003153}
3154
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003155/* Logic for the raise statement (too complicated for inlining).
3156 This *consumes* a reference count to each of its arguments. */
Fredrik Lundh7a830892006-05-27 10:39:48 +00003157static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003158do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003159{
Guido van Rossumd295f121998-04-09 21:39:57 +00003160 if (type == NULL) {
3161 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003162 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumd295f121998-04-09 21:39:57 +00003163 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
3164 value = tstate->exc_value;
3165 tb = tstate->exc_traceback;
3166 Py_XINCREF(type);
3167 Py_XINCREF(value);
3168 Py_XINCREF(tb);
3169 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003170
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003171 /* We support the following forms of raise:
3172 raise <class>, <classinstance>
3173 raise <class>, <argument tuple>
3174 raise <class>, None
3175 raise <class>, <argument>
3176 raise <classinstance>, None
3177 raise <string>, <object>
3178 raise <string>, None
3179
3180 An omitted second argument is the same as None.
3181
3182 In addition, raise <tuple>, <anything> is the same as
3183 raising the tuple's first item (and it better have one!);
3184 this rule is applied recursively.
3185
3186 Finally, an optional third argument can be supplied, which
3187 gives the traceback to be substituted (useful when
3188 re-raising an exception after examining it). */
3189
3190 /* First, check the traceback argument, replacing None with
3191 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00003192 if (tb == Py_None) {
3193 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003194 tb = NULL;
3195 }
3196 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003197 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003198 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003199 goto raise_error;
3200 }
3201
3202 /* Next, replace a missing value with None */
3203 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003204 value = Py_None;
3205 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003206 }
3207
3208 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00003209 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
3210 PyObject *tmp = type;
3211 type = PyTuple_GET_ITEM(type, 0);
3212 Py_INCREF(type);
3213 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003214 }
3215
Brett Cannon129bd522007-01-30 21:34:36 +00003216 if (PyExceptionClass_Check(type))
Barry Warsaw4249f541997-08-22 21:26:19 +00003217 PyErr_NormalizeException(&type, &value, &tb);
3218
Brett Cannonbf364092006-03-01 04:25:17 +00003219 else if (PyExceptionInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003220 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00003221 if (value != Py_None) {
3222 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003223 "instance exception may not have a separate value");
3224 goto raise_error;
3225 }
3226 else {
3227 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00003228 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003229 value = type;
Brett Cannonbf364092006-03-01 04:25:17 +00003230 type = PyExceptionInstance_Class(type);
Guido van Rossumb209a111997-04-29 18:18:01 +00003231 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003232 }
3233 }
3234 else {
3235 /* Not something you can raise. You get an exception
3236 anyway, just not what you specified :-) */
Jeremy Hylton960d9482001-04-27 02:25:33 +00003237 PyErr_Format(PyExc_TypeError,
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00003238 "exceptions must be classes or instances, not %s",
3239 type->ob_type->tp_name);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003240 goto raise_error;
3241 }
Guido van Rossum504153d2008-03-18 04:26:48 +00003242
3243 assert(PyExceptionClass_Check(type));
3244 if (Py_Py3kWarningFlag && PyClass_Check(type)) {
Benjamin Peterson9f4f4812008-04-27 03:01:45 +00003245 if (PyErr_WarnEx(PyExc_DeprecationWarning,
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00003246 "exceptions must derive from BaseException "
3247 "in 3.x", 1) < 0)
Guido van Rossum504153d2008-03-18 04:26:48 +00003248 goto raise_error;
3249 }
3250
Guido van Rossumb209a111997-04-29 18:18:01 +00003251 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003252 if (tb == NULL)
3253 return WHY_EXCEPTION;
3254 else
3255 return WHY_RERAISE;
3256 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00003257 Py_XDECREF(value);
3258 Py_XDECREF(type);
3259 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003260 return WHY_EXCEPTION;
3261}
3262
Tim Petersd6d010b2001-06-21 02:49:55 +00003263/* Iterate v argcnt times and store the results on the stack (via decreasing
3264 sp). Return 1 for success, 0 if error. */
3265
Fredrik Lundh7a830892006-05-27 10:39:48 +00003266static int
Tim Petersd6d010b2001-06-21 02:49:55 +00003267unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003268{
Tim Petersd6d010b2001-06-21 02:49:55 +00003269 int i = 0;
3270 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00003271 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00003272
Tim Petersd6d010b2001-06-21 02:49:55 +00003273 assert(v != NULL);
3274
3275 it = PyObject_GetIter(v);
3276 if (it == NULL)
3277 goto Error;
3278
3279 for (; i < argcnt; i++) {
3280 w = PyIter_Next(it);
3281 if (w == NULL) {
3282 /* Iterator done, via error or exhaustion. */
3283 if (!PyErr_Occurred()) {
3284 PyErr_Format(PyExc_ValueError,
3285 "need more than %d value%s to unpack",
3286 i, i == 1 ? "" : "s");
3287 }
3288 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003289 }
3290 *--sp = w;
3291 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003292
3293 /* We better have exhausted the iterator now. */
3294 w = PyIter_Next(it);
3295 if (w == NULL) {
3296 if (PyErr_Occurred())
3297 goto Error;
3298 Py_DECREF(it);
3299 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003300 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00003301 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00003302 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00003303 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00003304Error:
Barry Warsaw91010551997-08-25 22:30:51 +00003305 for (; i > 0; i--, sp++)
3306 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00003307 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00003308 return 0;
3309}
3310
3311
Guido van Rossum96a42c81992-01-12 02:29:51 +00003312#ifdef LLTRACE
Fredrik Lundh7a830892006-05-27 10:39:48 +00003313static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003314prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003315{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003316 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003317 if (PyObject_Print(v, stdout, 0) != 0)
3318 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003319 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003320 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003321}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003322#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003323
Fredrik Lundh7a830892006-05-27 10:39:48 +00003324static void
Fred Drake5755ce62001-06-27 19:19:46 +00003325call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003326{
Guido van Rossumb209a111997-04-29 18:18:01 +00003327 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003328 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003329 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003330 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003331 value = Py_None;
3332 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003333 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003334 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003335 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003336 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003337 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003338 }
Fred Drake5755ce62001-06-27 19:19:46 +00003339 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003340 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003341 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003342 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003343 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003344 Py_XDECREF(type);
3345 Py_XDECREF(value);
3346 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003347 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003348}
3349
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +00003350static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003351call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003352 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003353{
3354 PyObject *type, *value, *traceback;
3355 int err;
3356 PyErr_Fetch(&type, &value, &traceback);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003357 err = call_trace(func, obj, frame, what, arg);
Fred Drake4ec5d562001-10-04 19:26:43 +00003358 if (err == 0)
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +00003359 {
Fred Drake4ec5d562001-10-04 19:26:43 +00003360 PyErr_Restore(type, value, traceback);
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +00003361 return 0;
3362 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003363 else {
3364 Py_XDECREF(type);
3365 Py_XDECREF(value);
3366 Py_XDECREF(traceback);
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +00003367 return -1;
Fred Drake4ec5d562001-10-04 19:26:43 +00003368 }
3369}
3370
Fredrik Lundh7a830892006-05-27 10:39:48 +00003371static int
Fred Drake5755ce62001-06-27 19:19:46 +00003372call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3373 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003374{
Fred Drake5755ce62001-06-27 19:19:46 +00003375 register PyThreadState *tstate = frame->f_tstate;
3376 int result;
3377 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003378 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003379 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003380 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003381 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003382 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3383 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003384 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003385 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003386}
3387
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003388PyObject *
3389_PyEval_CallTracing(PyObject *func, PyObject *args)
3390{
3391 PyFrameObject *frame = PyEval_GetFrame();
3392 PyThreadState *tstate = frame->f_tstate;
3393 int save_tracing = tstate->tracing;
3394 int save_use_tracing = tstate->use_tracing;
3395 PyObject *result;
3396
3397 tstate->tracing = 0;
3398 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3399 || (tstate->c_profilefunc != NULL));
3400 result = PyObject_Call(func, args, NULL);
3401 tstate->tracing = save_tracing;
3402 tstate->use_tracing = save_use_tracing;
3403 return result;
3404}
3405
Fredrik Lundh7a830892006-05-27 10:39:48 +00003406static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003407maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003408 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3409 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003410{
Michael W. Hudson006c7522002-11-08 13:08:46 +00003411 int result = 0;
3412
Jeremy Hyltona4ebc132006-04-18 14:47:00 +00003413 /* If the last instruction executed isn't in the current
3414 instruction window, reset the window. If the last
3415 instruction happens to fall at the start of a line or if it
3416 represents a jump backwards, call the trace function.
3417 */
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003418 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Thomas Woutersae406c62007-09-19 17:27:43 +00003419 int line;
3420 PyAddrPair bounds;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003421
Thomas Woutersae406c62007-09-19 17:27:43 +00003422 line = PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3423 &bounds);
3424 if (line >= 0) {
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003425 frame->f_lineno = line;
Tim Peters8a5c3c72004-04-05 19:36:21 +00003426 result = call_trace(func, obj, frame,
Michael W. Hudson006c7522002-11-08 13:08:46 +00003427 PyTrace_LINE, Py_None);
Thomas Woutersae406c62007-09-19 17:27:43 +00003428 }
3429 *instr_lb = bounds.ap_lower;
3430 *instr_ub = bounds.ap_upper;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003431 }
Armin Rigobf57a142004-03-22 19:24:58 +00003432 else if (frame->f_lasti <= *instr_prev) {
Jeremy Hyltona4ebc132006-04-18 14:47:00 +00003433 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
Armin Rigobf57a142004-03-22 19:24:58 +00003434 }
3435 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003436 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003437}
3438
Fred Drake5755ce62001-06-27 19:19:46 +00003439void
3440PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003441{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003442 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003443 PyObject *temp = tstate->c_profileobj;
3444 Py_XINCREF(arg);
3445 tstate->c_profilefunc = NULL;
3446 tstate->c_profileobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003447 /* Must make sure that tracing is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003448 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003449 Py_XDECREF(temp);
3450 tstate->c_profilefunc = func;
3451 tstate->c_profileobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003452 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003453 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003454}
3455
3456void
3457PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3458{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003459 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003460 PyObject *temp = tstate->c_traceobj;
Jeffrey Yasskind0a70d42008-12-12 21:25:13 +00003461 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003462 Py_XINCREF(arg);
3463 tstate->c_tracefunc = NULL;
3464 tstate->c_traceobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003465 /* Must make sure that profiling is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003466 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003467 Py_XDECREF(temp);
3468 tstate->c_tracefunc = func;
3469 tstate->c_traceobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003470 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003471 tstate->use_tracing = ((func != NULL)
3472 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003473}
3474
Guido van Rossumb209a111997-04-29 18:18:01 +00003475PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003476PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003477{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003478 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003479 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003480 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003481 else
3482 return current_frame->f_builtins;
3483}
3484
Guido van Rossumb209a111997-04-29 18:18:01 +00003485PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003486PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003487{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003488 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003489 if (current_frame == NULL)
3490 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003491 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003492 return current_frame->f_locals;
3493}
3494
Guido van Rossumb209a111997-04-29 18:18:01 +00003495PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003496PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003497{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003498 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003499 if (current_frame == NULL)
3500 return NULL;
3501 else
3502 return current_frame->f_globals;
3503}
3504
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003505PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003506PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003507{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003508 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003509 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003510}
3511
Guido van Rossum6135a871995-01-09 17:53:26 +00003512int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003513PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003514{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003515 PyFrameObject *current_frame = PyEval_GetFrame();
Neal Norwitzb9845e72006-06-12 02:11:18 +00003516 return current_frame == NULL ? 0 : PyFrame_IsRestricted(current_frame);
Guido van Rossum6135a871995-01-09 17:53:26 +00003517}
3518
Guido van Rossumbe270261997-05-22 22:26:18 +00003519int
Tim Peters5ba58662001-07-16 02:29:45 +00003520PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003521{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003522 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003523 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003524
3525 if (current_frame != NULL) {
3526 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003527 const int compilerflags = codeflags & PyCF_MASK;
3528 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003529 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003530 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003531 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003532#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003533 if (codeflags & CO_GENERATOR_ALLOWED) {
3534 result = 1;
3535 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3536 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003537#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003538 }
3539 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003540}
3541
3542int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003543Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003544{
Guido van Rossumb209a111997-04-29 18:18:01 +00003545 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003546 if (f == NULL)
3547 return 0;
3548 if (!PyFile_SoftSpace(f, 0))
3549 return 0;
3550 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003551}
3552
Guido van Rossum3f5da241990-12-20 15:06:42 +00003553
Guido van Rossum681d79a1995-07-18 14:51:37 +00003554/* External interface to call any callable object.
3555 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003556
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003557#undef PyEval_CallObject
3558/* for backward compatibility: export this interface */
3559
Guido van Rossumb209a111997-04-29 18:18:01 +00003560PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003561PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003562{
Guido van Rossumb209a111997-04-29 18:18:01 +00003563 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003564}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003565#define PyEval_CallObject(func,arg) \
3566 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003567
Guido van Rossumb209a111997-04-29 18:18:01 +00003568PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003569PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003570{
Jeremy Hylton52820442001-01-03 23:52:36 +00003571 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003572
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003573 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003574 arg = PyTuple_New(0);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003575 if (arg == NULL)
3576 return NULL;
3577 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003578 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003579 PyErr_SetString(PyExc_TypeError,
3580 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003581 return NULL;
3582 }
3583 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003584 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003585
Guido van Rossumb209a111997-04-29 18:18:01 +00003586 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003587 PyErr_SetString(PyExc_TypeError,
3588 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003589 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003590 return NULL;
3591 }
3592
Tim Peters6d6c1a32001-08-02 04:15:00 +00003593 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003594 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003595 return result;
3596}
3597
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003598const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003599PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003600{
3601 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003602 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003603 else if (PyFunction_Check(func))
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003604 return PyString_AsString(((PyFunctionObject*)func)->func_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00003605 else if (PyCFunction_Check(func))
3606 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3607 else if (PyClass_Check(func))
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003608 return PyString_AsString(((PyClassObject*)func)->cl_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00003609 else if (PyInstance_Check(func)) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003610 return PyString_AsString(
Jeremy Hylton512a2372001-04-11 13:52:29 +00003611 ((PyInstanceObject*)func)->in_class->cl_name);
3612 } else {
3613 return func->ob_type->tp_name;
3614 }
3615}
3616
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003617const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003618PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003619{
3620 if (PyMethod_Check(func))
3621 return "()";
3622 else if (PyFunction_Check(func))
3623 return "()";
3624 else if (PyCFunction_Check(func))
3625 return "()";
3626 else if (PyClass_Check(func))
3627 return " constructor";
3628 else if (PyInstance_Check(func)) {
3629 return " instance";
3630 } else {
3631 return " object";
3632 }
3633}
3634
Fredrik Lundh7a830892006-05-27 10:39:48 +00003635static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003636err_args(PyObject *func, int flags, int nargs)
3637{
3638 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003639 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003640 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003641 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003642 nargs);
3643 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003644 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003645 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003646 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003647 nargs);
3648}
3649
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003650#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003651if (tstate->use_tracing && tstate->c_profilefunc) { \
3652 if (call_trace(tstate->c_profilefunc, \
3653 tstate->c_profileobj, \
3654 tstate->frame, PyTrace_C_CALL, \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003655 func)) { \
3656 x = NULL; \
3657 } \
3658 else { \
3659 x = call; \
3660 if (tstate->c_profilefunc != NULL) { \
3661 if (x == NULL) { \
3662 call_trace_protected(tstate->c_profilefunc, \
3663 tstate->c_profileobj, \
3664 tstate->frame, PyTrace_C_EXCEPTION, \
3665 func); \
3666 /* XXX should pass (type, value, tb) */ \
3667 } else { \
3668 if (call_trace(tstate->c_profilefunc, \
3669 tstate->c_profileobj, \
3670 tstate->frame, PyTrace_C_RETURN, \
3671 func)) { \
3672 Py_DECREF(x); \
3673 x = NULL; \
3674 } \
3675 } \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003676 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003677 } \
3678} else { \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003679 x = call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003680 }
3681
Fredrik Lundh7a830892006-05-27 10:39:48 +00003682static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003683call_function(PyObject ***pp_stack, int oparg
3684#ifdef WITH_TSC
3685 , uint64* pintr0, uint64* pintr1
3686#endif
3687 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003688{
3689 int na = oparg & 0xff;
3690 int nk = (oparg>>8) & 0xff;
3691 int n = na + 2 * nk;
3692 PyObject **pfunc = (*pp_stack) - n - 1;
3693 PyObject *func = *pfunc;
3694 PyObject *x, *w;
3695
Jeremy Hylton985eba52003-02-05 23:13:00 +00003696 /* Always dispatch PyCFunction first, because these are
3697 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003698 */
3699 if (PyCFunction_Check(func) && nk == 0) {
3700 int flags = PyCFunction_GET_FLAGS(func);
Nicholas Bastind858a772004-06-25 23:31:06 +00003701 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003702
3703 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003704 if (flags & (METH_NOARGS | METH_O)) {
3705 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3706 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003707 if (flags & METH_NOARGS && na == 0) {
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003708 C_TRACE(x, (*meth)(self,NULL));
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003709 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003710 else if (flags & METH_O && na == 1) {
3711 PyObject *arg = EXT_POP(*pp_stack);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003712 C_TRACE(x, (*meth)(self,arg));
Jeremy Hylton192690e2002-08-16 18:36:11 +00003713 Py_DECREF(arg);
3714 }
3715 else {
3716 err_args(func, flags, na);
3717 x = NULL;
3718 }
3719 }
3720 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003721 PyObject *callargs;
3722 callargs = load_args(pp_stack, na);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003723 READ_TIMESTAMP(*pintr0);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003724 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003725 READ_TIMESTAMP(*pintr1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003726 Py_XDECREF(callargs);
3727 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003728 } else {
3729 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3730 /* optimize access to bound methods */
3731 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003732 PCALL(PCALL_METHOD);
3733 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003734 Py_INCREF(self);
3735 func = PyMethod_GET_FUNCTION(func);
3736 Py_INCREF(func);
3737 Py_DECREF(*pfunc);
3738 *pfunc = self;
3739 na++;
3740 n++;
3741 } else
3742 Py_INCREF(func);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003743 READ_TIMESTAMP(*pintr0);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003744 if (PyFunction_Check(func))
3745 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003746 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003747 x = do_call(func, pp_stack, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003748 READ_TIMESTAMP(*pintr1);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003749 Py_DECREF(func);
3750 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003751
Armin Rigod34fa522006-03-28 19:10:40 +00003752 /* Clear the stack of the function object. Also removes
3753 the arguments in case they weren't consumed already
3754 (fast_function() and err_args() leave them on the stack).
Thomas Wouters7f597322006-03-01 05:32:33 +00003755 */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003756 while ((*pp_stack) > pfunc) {
3757 w = EXT_POP(*pp_stack);
3758 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003759 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003760 }
3761 return x;
3762}
3763
Jeremy Hylton192690e2002-08-16 18:36:11 +00003764/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003765 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003766 For the simplest case -- a function that takes only positional
3767 arguments and is called with only positional arguments -- it
3768 inlines the most primitive frame setup code from
3769 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3770 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003771*/
3772
Fredrik Lundh7a830892006-05-27 10:39:48 +00003773static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003774fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003775{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003776 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003777 PyObject *globals = PyFunction_GET_GLOBALS(func);
3778 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3779 PyObject **d = NULL;
3780 int nd = 0;
3781
Jeremy Hylton985eba52003-02-05 23:13:00 +00003782 PCALL(PCALL_FUNCTION);
3783 PCALL(PCALL_FAST_FUNCTION);
Raymond Hettinger40174c32003-05-31 07:04:16 +00003784 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003785 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3786 PyFrameObject *f;
3787 PyObject *retval = NULL;
3788 PyThreadState *tstate = PyThreadState_GET();
3789 PyObject **fastlocals, **stack;
3790 int i;
3791
3792 PCALL(PCALL_FASTER_FUNCTION);
3793 assert(globals != NULL);
3794 /* XXX Perhaps we should create a specialized
3795 PyFrame_New() that doesn't take locals, but does
3796 take builtins without sanity checking them.
3797 */
Neal Norwitzdf6a6492006-08-13 18:10:10 +00003798 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003799 f = PyFrame_New(tstate, co, globals, NULL);
3800 if (f == NULL)
3801 return NULL;
3802
3803 fastlocals = f->f_localsplus;
3804 stack = (*pp_stack) - n;
3805
3806 for (i = 0; i < n; i++) {
3807 Py_INCREF(*stack);
3808 fastlocals[i] = *stack++;
3809 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003810 retval = PyEval_EvalFrameEx(f,0);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003811 ++tstate->recursion_depth;
3812 Py_DECREF(f);
3813 --tstate->recursion_depth;
3814 return retval;
3815 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003816 if (argdefs != NULL) {
3817 d = &PyTuple_GET_ITEM(argdefs, 0);
Christian Heimese93237d2007-12-19 02:37:44 +00003818 nd = Py_SIZE(argdefs);
Jeremy Hylton52820442001-01-03 23:52:36 +00003819 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003820 return PyEval_EvalCodeEx(co, globals,
3821 (PyObject *)NULL, (*pp_stack)-n, na,
3822 (*pp_stack)-2*nk, nk, d, nd,
3823 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003824}
3825
Fredrik Lundh7a830892006-05-27 10:39:48 +00003826static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003827update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3828 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003829{
3830 PyObject *kwdict = NULL;
3831 if (orig_kwdict == NULL)
3832 kwdict = PyDict_New();
3833 else {
3834 kwdict = PyDict_Copy(orig_kwdict);
3835 Py_DECREF(orig_kwdict);
3836 }
3837 if (kwdict == NULL)
3838 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003839 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003840 int err;
3841 PyObject *value = EXT_POP(*pp_stack);
3842 PyObject *key = EXT_POP(*pp_stack);
3843 if (PyDict_GetItem(kwdict, key) != NULL) {
Thomas Woutersae406c62007-09-19 17:27:43 +00003844 PyErr_Format(PyExc_TypeError,
3845 "%.200s%s got multiple values "
3846 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003847 PyEval_GetFuncName(func),
3848 PyEval_GetFuncDesc(func),
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003849 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003850 Py_DECREF(key);
3851 Py_DECREF(value);
3852 Py_DECREF(kwdict);
3853 return NULL;
3854 }
3855 err = PyDict_SetItem(kwdict, key, value);
3856 Py_DECREF(key);
3857 Py_DECREF(value);
3858 if (err) {
3859 Py_DECREF(kwdict);
3860 return NULL;
3861 }
3862 }
3863 return kwdict;
3864}
3865
Fredrik Lundh7a830892006-05-27 10:39:48 +00003866static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00003867update_star_args(int nstack, int nstar, PyObject *stararg,
3868 PyObject ***pp_stack)
3869{
3870 PyObject *callargs, *w;
3871
3872 callargs = PyTuple_New(nstack + nstar);
3873 if (callargs == NULL) {
3874 return NULL;
3875 }
3876 if (nstar) {
3877 int i;
3878 for (i = 0; i < nstar; i++) {
3879 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3880 Py_INCREF(a);
3881 PyTuple_SET_ITEM(callargs, nstack + i, a);
3882 }
3883 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003884 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003885 w = EXT_POP(*pp_stack);
3886 PyTuple_SET_ITEM(callargs, nstack, w);
3887 }
3888 return callargs;
3889}
3890
Fredrik Lundh7a830892006-05-27 10:39:48 +00003891static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00003892load_args(PyObject ***pp_stack, int na)
3893{
3894 PyObject *args = PyTuple_New(na);
3895 PyObject *w;
3896
3897 if (args == NULL)
3898 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003899 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003900 w = EXT_POP(*pp_stack);
3901 PyTuple_SET_ITEM(args, na, w);
3902 }
3903 return args;
3904}
3905
Fredrik Lundh7a830892006-05-27 10:39:48 +00003906static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00003907do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3908{
3909 PyObject *callargs = NULL;
3910 PyObject *kwdict = NULL;
3911 PyObject *result = NULL;
3912
3913 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003914 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003915 if (kwdict == NULL)
3916 goto call_fail;
3917 }
3918 callargs = load_args(pp_stack, na);
3919 if (callargs == NULL)
3920 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003921#ifdef CALL_PROFILE
3922 /* At this point, we have to look at the type of func to
3923 update the call stats properly. Do it here so as to avoid
3924 exposing the call stats machinery outside ceval.c
3925 */
3926 if (PyFunction_Check(func))
3927 PCALL(PCALL_FUNCTION);
3928 else if (PyMethod_Check(func))
3929 PCALL(PCALL_METHOD);
3930 else if (PyType_Check(func))
3931 PCALL(PCALL_TYPE);
Antoine Pitrou6987d542009-05-30 21:43:48 +00003932 else if (PyCFunction_Check(func))
3933 PCALL(PCALL_CFUNCTION);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003934 else
3935 PCALL(PCALL_OTHER);
3936#endif
Antoine Pitrou6987d542009-05-30 21:43:48 +00003937 if (PyCFunction_Check(func)) {
3938 PyThreadState *tstate = PyThreadState_GET();
3939 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
3940 }
3941 else
3942 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003943 call_fail:
3944 Py_XDECREF(callargs);
3945 Py_XDECREF(kwdict);
3946 return result;
3947}
3948
Fredrik Lundh7a830892006-05-27 10:39:48 +00003949static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00003950ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3951{
3952 int nstar = 0;
3953 PyObject *callargs = NULL;
3954 PyObject *stararg = NULL;
3955 PyObject *kwdict = NULL;
3956 PyObject *result = NULL;
3957
3958 if (flags & CALL_FLAG_KW) {
3959 kwdict = EXT_POP(*pp_stack);
Georg Brandl2134e752007-05-21 20:34:16 +00003960 if (!PyDict_Check(kwdict)) {
3961 PyObject *d;
3962 d = PyDict_New();
3963 if (d == NULL)
3964 goto ext_call_fail;
3965 if (PyDict_Update(d, kwdict) != 0) {
3966 Py_DECREF(d);
3967 /* PyDict_Update raises attribute
3968 * error (percolated from an attempt
3969 * to get 'keys' attribute) instead of
3970 * a type error if its second argument
3971 * is not a mapping.
3972 */
3973 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
3974 PyErr_Format(PyExc_TypeError,
3975 "%.200s%.200s argument after ** "
3976 "must be a mapping, not %.200s",
3977 PyEval_GetFuncName(func),
3978 PyEval_GetFuncDesc(func),
3979 kwdict->ob_type->tp_name);
3980 }
3981 goto ext_call_fail;
3982 }
3983 Py_DECREF(kwdict);
3984 kwdict = d;
Jeremy Hylton52820442001-01-03 23:52:36 +00003985 }
3986 }
3987 if (flags & CALL_FLAG_VAR) {
3988 stararg = EXT_POP(*pp_stack);
3989 if (!PyTuple_Check(stararg)) {
3990 PyObject *t = NULL;
3991 t = PySequence_Tuple(stararg);
3992 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003993 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3994 PyErr_Format(PyExc_TypeError,
Georg Brandl2134e752007-05-21 20:34:16 +00003995 "%.200s%.200s argument after * "
3996 "must be a sequence, not %200s",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003997 PyEval_GetFuncName(func),
Georg Brandl2134e752007-05-21 20:34:16 +00003998 PyEval_GetFuncDesc(func),
3999 stararg->ob_type->tp_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00004000 }
Jeremy Hylton52820442001-01-03 23:52:36 +00004001 goto ext_call_fail;
4002 }
4003 Py_DECREF(stararg);
4004 stararg = t;
4005 }
4006 nstar = PyTuple_GET_SIZE(stararg);
4007 }
4008 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00004009 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00004010 if (kwdict == NULL)
4011 goto ext_call_fail;
4012 }
4013 callargs = update_star_args(na, nstar, stararg, pp_stack);
4014 if (callargs == NULL)
4015 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004016#ifdef CALL_PROFILE
4017 /* At this point, we have to look at the type of func to
4018 update the call stats properly. Do it here so as to avoid
4019 exposing the call stats machinery outside ceval.c
4020 */
4021 if (PyFunction_Check(func))
4022 PCALL(PCALL_FUNCTION);
4023 else if (PyMethod_Check(func))
4024 PCALL(PCALL_METHOD);
4025 else if (PyType_Check(func))
4026 PCALL(PCALL_TYPE);
Antoine Pitrou6987d542009-05-30 21:43:48 +00004027 else if (PyCFunction_Check(func))
4028 PCALL(PCALL_CFUNCTION);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004029 else
4030 PCALL(PCALL_OTHER);
4031#endif
Antoine Pitrou6987d542009-05-30 21:43:48 +00004032 if (PyCFunction_Check(func)) {
4033 PyThreadState *tstate = PyThreadState_GET();
4034 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4035 }
4036 else
4037 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersae406c62007-09-19 17:27:43 +00004038ext_call_fail:
Jeremy Hylton52820442001-01-03 23:52:36 +00004039 Py_XDECREF(callargs);
4040 Py_XDECREF(kwdict);
4041 Py_XDECREF(stararg);
4042 return result;
4043}
4044
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004045/* Extract a slice index from a PyInt or PyLong or an object with the
4046 nb_index slot defined, and store in *pi.
4047 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4048 and silently boost values less than -PY_SSIZE_T_MAX-1 to -PY_SSIZE_T_MAX-1.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004049 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004050*/
Tim Petersb5196382001-12-16 19:44:20 +00004051/* Note: If v is NULL, return success without storing into *pi. This
4052 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4053 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004054*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004055int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004056_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004057{
Tim Petersb5196382001-12-16 19:44:20 +00004058 if (v != NULL) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004059 Py_ssize_t x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00004060 if (PyInt_Check(v)) {
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00004061 /* XXX(nnorwitz): I think PyInt_AS_LONG is correct,
4062 however, it looks like it should be AsSsize_t.
4063 There should be a comment here explaining why.
4064 */
4065 x = PyInt_AS_LONG(v);
Tim Peters7df5e7f2006-05-26 23:14:37 +00004066 }
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00004067 else if (PyIndex_Check(v)) {
4068 x = PyNumber_AsSsize_t(v, NULL);
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004069 if (x == -1 && PyErr_Occurred())
4070 return 0;
4071 }
4072 else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00004073 PyErr_SetString(PyExc_TypeError,
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004074 "slice indices must be integers or "
4075 "None or have an __index__ method");
Guido van Rossum20c6add2000-05-08 14:06:50 +00004076 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004077 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00004078 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004079 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00004080 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004081}
4082
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004083#undef ISINDEX
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00004084#define ISINDEX(x) ((x) == NULL || \
4085 PyInt_Check(x) || PyLong_Check(x) || PyIndex_Check(x))
Guido van Rossum50d756e2001-08-18 17:43:36 +00004086
Fredrik Lundh7a830892006-05-27 10:39:48 +00004087static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004088apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004089{
Guido van Rossum50d756e2001-08-18 17:43:36 +00004090 PyTypeObject *tp = u->ob_type;
4091 PySequenceMethods *sq = tp->tp_as_sequence;
4092
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004093 if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004094 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
Guido van Rossum50d756e2001-08-18 17:43:36 +00004095 if (!_PyEval_SliceIndex(v, &ilow))
4096 return NULL;
4097 if (!_PyEval_SliceIndex(w, &ihigh))
4098 return NULL;
4099 return PySequence_GetSlice(u, ilow, ihigh);
4100 }
4101 else {
4102 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00004103 if (slice != NULL) {
4104 PyObject *res = PyObject_GetItem(u, slice);
4105 Py_DECREF(slice);
4106 return res;
4107 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00004108 else
4109 return NULL;
4110 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004111}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004112
Fredrik Lundh7a830892006-05-27 10:39:48 +00004113static int
Guido van Rossumac7be682001-01-17 15:42:30 +00004114assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
4115 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004116{
Guido van Rossum50d756e2001-08-18 17:43:36 +00004117 PyTypeObject *tp = u->ob_type;
4118 PySequenceMethods *sq = tp->tp_as_sequence;
4119
Georg Brandl0fca97a2007-03-05 22:28:08 +00004120 if (sq && sq->sq_ass_slice && ISINDEX(v) && ISINDEX(w)) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004121 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
Guido van Rossum50d756e2001-08-18 17:43:36 +00004122 if (!_PyEval_SliceIndex(v, &ilow))
4123 return -1;
4124 if (!_PyEval_SliceIndex(w, &ihigh))
4125 return -1;
4126 if (x == NULL)
4127 return PySequence_DelSlice(u, ilow, ihigh);
4128 else
4129 return PySequence_SetSlice(u, ilow, ihigh, x);
4130 }
4131 else {
4132 PyObject *slice = PySlice_New(v, w, NULL);
4133 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00004134 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00004135 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00004136 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00004137 else
Guido van Rossum354797c2001-12-03 19:45:06 +00004138 res = PyObject_DelItem(u, slice);
4139 Py_DECREF(slice);
4140 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00004141 }
4142 else
4143 return -1;
4144 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004145}
4146
Guido van Rossum04edb522008-03-18 02:49:46 +00004147#define Py3kExceptionClass_Check(x) \
4148 (PyType_Check((x)) && \
4149 PyType_FastSubclass((PyTypeObject*)(x), Py_TPFLAGS_BASE_EXC_SUBCLASS))
4150
4151#define CANNOT_CATCH_MSG "catching classes that don't inherit from " \
Georg Brandld5b635f2008-03-25 08:29:14 +00004152 "BaseException is not allowed in 3.x"
Guido van Rossum04edb522008-03-18 02:49:46 +00004153
Fredrik Lundh7a830892006-05-27 10:39:48 +00004154static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004155cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004156{
Guido van Rossumac7be682001-01-17 15:42:30 +00004157 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004158 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00004159 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00004160 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004161 break;
4162 case PyCmp_IS_NOT:
4163 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004164 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004165 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004166 res = PySequence_Contains(w, v);
4167 if (res < 0)
4168 return NULL;
4169 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004170 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00004171 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004172 if (res < 0)
4173 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004174 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004175 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004176 case PyCmp_EXC_MATCH:
Brett Cannon129bd522007-01-30 21:34:36 +00004177 if (PyTuple_Check(w)) {
4178 Py_ssize_t i, length;
4179 length = PyTuple_Size(w);
4180 for (i = 0; i < length; i += 1) {
4181 PyObject *exc = PyTuple_GET_ITEM(w, i);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004182 if (PyString_Check(exc)) {
Brett Cannon129bd522007-01-30 21:34:36 +00004183 int ret_val;
4184 ret_val = PyErr_WarnEx(
Thomas Wouterse2176022007-09-20 17:35:10 +00004185 PyExc_DeprecationWarning,
4186 "catching of string "
4187 "exceptions is deprecated", 1);
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00004188 if (ret_val < 0)
Brett Cannon129bd522007-01-30 21:34:36 +00004189 return NULL;
4190 }
Guido van Rossum20bda582008-03-18 03:15:05 +00004191 else if (Py_Py3kWarningFlag &&
4192 !PyTuple_Check(exc) &&
4193 !Py3kExceptionClass_Check(exc))
Guido van Rossum04edb522008-03-18 02:49:46 +00004194 {
4195 int ret_val;
4196 ret_val = PyErr_WarnEx(
4197 PyExc_DeprecationWarning,
4198 CANNOT_CATCH_MSG, 1);
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00004199 if (ret_val < 0)
Guido van Rossum04edb522008-03-18 02:49:46 +00004200 return NULL;
4201 }
Brett Cannon129bd522007-01-30 21:34:36 +00004202 }
4203 }
4204 else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004205 if (PyString_Check(w)) {
Brett Cannon129bd522007-01-30 21:34:36 +00004206 int ret_val;
4207 ret_val = PyErr_WarnEx(
4208 PyExc_DeprecationWarning,
4209 "catching of string "
Thomas Wouterse2176022007-09-20 17:35:10 +00004210 "exceptions is deprecated", 1);
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00004211 if (ret_val < 0)
Brett Cannon129bd522007-01-30 21:34:36 +00004212 return NULL;
4213 }
Guido van Rossum20bda582008-03-18 03:15:05 +00004214 else if (Py_Py3kWarningFlag &&
4215 !PyTuple_Check(w) &&
4216 !Py3kExceptionClass_Check(w))
Guido van Rossum04edb522008-03-18 02:49:46 +00004217 {
4218 int ret_val;
4219 ret_val = PyErr_WarnEx(
4220 PyExc_DeprecationWarning,
4221 CANNOT_CATCH_MSG, 1);
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00004222 if (ret_val < 0)
Guido van Rossum04edb522008-03-18 02:49:46 +00004223 return NULL;
4224 }
Brett Cannon129bd522007-01-30 21:34:36 +00004225 }
Barry Warsaw4249f541997-08-22 21:26:19 +00004226 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004227 break;
4228 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00004229 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004230 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004231 v = res ? Py_True : Py_False;
4232 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004233 return v;
4234}
4235
Fredrik Lundh7a830892006-05-27 10:39:48 +00004236static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00004237import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004238{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004239 PyObject *x;
4240
4241 x = PyObject_GetAttr(v, name);
4242 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00004243 PyErr_Format(PyExc_ImportError,
4244 "cannot import name %.230s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004245 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004246 }
Thomas Wouters52152252000-08-17 22:55:00 +00004247 return x;
4248}
Guido van Rossumac7be682001-01-17 15:42:30 +00004249
Fredrik Lundh7a830892006-05-27 10:39:48 +00004250static int
Thomas Wouters52152252000-08-17 22:55:00 +00004251import_all_from(PyObject *locals, PyObject *v)
4252{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004253 PyObject *all = PyObject_GetAttrString(v, "__all__");
4254 PyObject *dict, *name, *value;
4255 int skip_leading_underscores = 0;
4256 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004257
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004258 if (all == NULL) {
4259 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4260 return -1; /* Unexpected error */
4261 PyErr_Clear();
4262 dict = PyObject_GetAttrString(v, "__dict__");
4263 if (dict == NULL) {
4264 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4265 return -1;
4266 PyErr_SetString(PyExc_ImportError,
4267 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00004268 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004269 }
4270 all = PyMapping_Keys(dict);
4271 Py_DECREF(dict);
4272 if (all == NULL)
4273 return -1;
4274 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004275 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004276
4277 for (pos = 0, err = 0; ; pos++) {
4278 name = PySequence_GetItem(all, pos);
4279 if (name == NULL) {
4280 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4281 err = -1;
4282 else
4283 PyErr_Clear();
4284 break;
4285 }
4286 if (skip_leading_underscores &&
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004287 PyString_Check(name) &&
4288 PyString_AS_STRING(name)[0] == '_')
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004289 {
4290 Py_DECREF(name);
4291 continue;
4292 }
4293 value = PyObject_GetAttr(v, name);
4294 if (value == NULL)
4295 err = -1;
Armin Rigo70370852006-11-29 21:59:22 +00004296 else if (PyDict_CheckExact(locals))
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004297 err = PyDict_SetItem(locals, name, value);
Armin Rigo70370852006-11-29 21:59:22 +00004298 else
4299 err = PyObject_SetItem(locals, name, value);
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004300 Py_DECREF(name);
4301 Py_XDECREF(value);
4302 if (err != 0)
4303 break;
4304 }
4305 Py_DECREF(all);
4306 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004307}
4308
Fredrik Lundh7a830892006-05-27 10:39:48 +00004309static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004310build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004311{
Guido van Rossum7851eea2001-09-12 19:19:18 +00004312 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004313
4314 if (PyDict_Check(methods))
4315 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00004316 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00004317 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004318 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4319 base = PyTuple_GET_ITEM(bases, 0);
4320 metaclass = PyObject_GetAttrString(base, "__class__");
4321 if (metaclass == NULL) {
4322 PyErr_Clear();
4323 metaclass = (PyObject *)base->ob_type;
4324 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00004325 }
4326 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004327 else {
4328 PyObject *g = PyEval_GetGlobals();
4329 if (g != NULL && PyDict_Check(g))
4330 metaclass = PyDict_GetItemString(g, "__metaclass__");
4331 if (metaclass == NULL)
4332 metaclass = (PyObject *) &PyClass_Type;
4333 Py_INCREF(metaclass);
4334 }
Jeremy Hylton7c1e3472007-02-26 16:14:51 +00004335 result = PyObject_CallFunctionObjArgs(metaclass, name, bases, methods,
Thomas Woutersae406c62007-09-19 17:27:43 +00004336 NULL);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004337 Py_DECREF(metaclass);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004338 if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
Tim Peters7df5e7f2006-05-26 23:14:37 +00004339 /* A type error here likely means that the user passed
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004340 in a base that was not a class (such the random module
4341 instead of the random.random type). Help them out with
Raymond Hettingercfc31922004-09-16 16:41:57 +00004342 by augmenting the error message with more information.*/
4343
4344 PyObject *ptype, *pvalue, *ptraceback;
4345
4346 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004347 if (PyString_Check(pvalue)) {
Raymond Hettingercfc31922004-09-16 16:41:57 +00004348 PyObject *newmsg;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004349 newmsg = PyString_FromFormat(
Jeremy Hylton7c1e3472007-02-26 16:14:51 +00004350 "Error when calling the metaclass bases\n"
Thomas Woutersae406c62007-09-19 17:27:43 +00004351 " %s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004352 PyString_AS_STRING(pvalue));
Raymond Hettingercfc31922004-09-16 16:41:57 +00004353 if (newmsg != NULL) {
4354 Py_DECREF(pvalue);
4355 pvalue = newmsg;
4356 }
4357 }
4358 PyErr_Restore(ptype, pvalue, ptraceback);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004359 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004360 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00004361}
4362
Fredrik Lundh7a830892006-05-27 10:39:48 +00004363static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004364exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
4365 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004366{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004367 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00004368 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004369 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004370
Guido van Rossumb209a111997-04-29 18:18:01 +00004371 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
4372 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004373 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00004374 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004375 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00004376 locals = PyTuple_GetItem(prog, 2);
4377 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004378 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004379 if (globals == Py_None) {
4380 globals = PyEval_GetGlobals();
4381 if (locals == Py_None) {
4382 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00004383 plain = 1;
4384 }
Neal Norwitzdf6a6492006-08-13 18:10:10 +00004385 if (!globals || !locals) {
4386 PyErr_SetString(PyExc_SystemError,
4387 "globals and locals cannot be NULL");
4388 return -1;
4389 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004390 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004391 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004392 locals = globals;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004393 if (!PyString_Check(prog) &&
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004394 !PyUnicode_Check(prog) &&
Guido van Rossumb209a111997-04-29 18:18:01 +00004395 !PyCode_Check(prog) &&
4396 !PyFile_Check(prog)) {
4397 PyErr_SetString(PyExc_TypeError,
Guido van Rossumac7be682001-01-17 15:42:30 +00004398 "exec: arg 1 must be a string, file, or code object");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004399 return -1;
4400 }
Fred Drake661ea262000-10-24 19:57:45 +00004401 if (!PyDict_Check(globals)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004402 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00004403 "exec: arg 2 must be a dictionary or None");
4404 return -1;
4405 }
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004406 if (!PyMapping_Check(locals)) {
Fred Drake661ea262000-10-24 19:57:45 +00004407 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004408 "exec: arg 3 must be a mapping or None");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004409 return -1;
4410 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004411 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00004412 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00004413 if (PyCode_Check(prog)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +00004414 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4415 PyErr_SetString(PyExc_TypeError,
4416 "code object passed to exec may not contain free variables");
4417 return -1;
4418 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004419 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004420 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004421 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004422 FILE *fp = PyFile_AsFile(prog);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004423 char *name = PyString_AsString(PyFile_Name(prog));
Jeremy Hylton714b1122007-02-25 16:01:58 +00004424 PyCompilerFlags cf;
Thomas Woutersae406c62007-09-19 17:27:43 +00004425 if (name == NULL)
4426 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004427 cf.cf_flags = 0;
4428 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004429 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004430 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004431 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004432 v = PyRun_File(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004433 locals);
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004434 }
4435 else {
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004436 PyObject *tmp = NULL;
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004437 char *str;
Tim Peters5ba58662001-07-16 02:29:45 +00004438 PyCompilerFlags cf;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004439 cf.cf_flags = 0;
4440#ifdef Py_USING_UNICODE
4441 if (PyUnicode_Check(prog)) {
4442 tmp = PyUnicode_AsUTF8String(prog);
4443 if (tmp == NULL)
4444 return -1;
4445 prog = tmp;
4446 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4447 }
4448#endif
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004449 if (PyString_AsStringAndSize(prog, &str, NULL))
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004450 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004451 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters8a5c3c72004-04-05 19:36:21 +00004452 v = PyRun_StringFlags(str, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004453 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004454 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004455 v = PyRun_String(str, Py_file_input, globals, locals);
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004456 Py_XDECREF(tmp);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004457 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004458 if (plain)
4459 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004460 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004461 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00004462 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004463 return 0;
4464}
Guido van Rossum24c13741995-02-14 09:42:43 +00004465
Fredrik Lundh7a830892006-05-27 10:39:48 +00004466static void
Paul Prescode68140d2000-08-30 20:25:01 +00004467format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4468{
4469 char *obj_str;
4470
4471 if (!obj)
4472 return;
4473
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004474 obj_str = PyString_AsString(obj);
Paul Prescode68140d2000-08-30 20:25:01 +00004475 if (!obj_str)
4476 return;
4477
4478 PyErr_Format(exc, format_str, obj_str);
4479}
Guido van Rossum950361c1997-01-24 13:49:28 +00004480
Fredrik Lundh7a830892006-05-27 10:39:48 +00004481static PyObject *
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004482string_concatenate(PyObject *v, PyObject *w,
4483 PyFrameObject *f, unsigned char *next_instr)
4484{
4485 /* This function implements 'variable += expr' when both arguments
4486 are strings. */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004487 Py_ssize_t v_len = PyString_GET_SIZE(v);
4488 Py_ssize_t w_len = PyString_GET_SIZE(w);
Armin Rigo97ff0472006-08-09 15:37:26 +00004489 Py_ssize_t new_len = v_len + w_len;
4490 if (new_len < 0) {
4491 PyErr_SetString(PyExc_OverflowError,
4492 "strings are too large to concat");
4493 return NULL;
4494 }
Tim Peters7df5e7f2006-05-26 23:14:37 +00004495
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004496 if (v->ob_refcnt == 2) {
4497 /* In the common case, there are 2 references to the value
4498 * stored in 'variable' when the += is performed: one on the
Thomas Wouterse2176022007-09-20 17:35:10 +00004499 * value stack (in 'v') and one still stored in the
4500 * 'variable'. We try to delete the variable now to reduce
4501 * the refcnt to 1.
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004502 */
4503 switch (*next_instr) {
4504 case STORE_FAST:
4505 {
4506 int oparg = PEEKARG();
4507 PyObject **fastlocals = f->f_localsplus;
4508 if (GETLOCAL(oparg) == v)
4509 SETLOCAL(oparg, NULL);
4510 break;
4511 }
4512 case STORE_DEREF:
4513 {
Thomas Wouterse2176022007-09-20 17:35:10 +00004514 PyObject **freevars = (f->f_localsplus +
4515 f->f_code->co_nlocals);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004516 PyObject *c = freevars[PEEKARG()];
4517 if (PyCell_GET(c) == v)
4518 PyCell_Set(c, NULL);
4519 break;
4520 }
4521 case STORE_NAME:
4522 {
4523 PyObject *names = f->f_code->co_names;
4524 PyObject *name = GETITEM(names, PEEKARG());
4525 PyObject *locals = f->f_locals;
4526 if (PyDict_CheckExact(locals) &&
4527 PyDict_GetItem(locals, name) == v) {
4528 if (PyDict_DelItem(locals, name) != 0) {
4529 PyErr_Clear();
4530 }
4531 }
4532 break;
4533 }
4534 }
4535 }
4536
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004537 if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004538 /* Now we own the last reference to 'v', so we can resize it
4539 * in-place.
4540 */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004541 if (_PyString_Resize(&v, new_len) != 0) {
4542 /* XXX if _PyString_Resize() fails, 'v' has been
Thomas Wouterse2176022007-09-20 17:35:10 +00004543 * deallocated so it cannot be put back into
4544 * 'variable'. The MemoryError is raised when there
4545 * is no value in 'variable', which might (very
4546 * remotely) be a cause of incompatibilities.
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004547 */
4548 return NULL;
4549 }
4550 /* copy 'w' into the newly allocated area of 'v' */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004551 memcpy(PyString_AS_STRING(v) + v_len,
4552 PyString_AS_STRING(w), w_len);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004553 return v;
4554 }
4555 else {
4556 /* When in-place resizing is not an option. */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004557 PyString_Concat(&v, w);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004558 return v;
4559 }
4560}
4561
Guido van Rossum950361c1997-01-24 13:49:28 +00004562#ifdef DYNAMIC_EXECUTION_PROFILE
4563
Fredrik Lundh7a830892006-05-27 10:39:48 +00004564static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004565getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004566{
4567 int i;
4568 PyObject *l = PyList_New(256);
4569 if (l == NULL) return NULL;
4570 for (i = 0; i < 256; i++) {
4571 PyObject *x = PyInt_FromLong(a[i]);
4572 if (x == NULL) {
4573 Py_DECREF(l);
4574 return NULL;
4575 }
4576 PyList_SetItem(l, i, x);
4577 }
4578 for (i = 0; i < 256; i++)
4579 a[i] = 0;
4580 return l;
4581}
4582
4583PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004584_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004585{
4586#ifndef DXPAIRS
4587 return getarray(dxp);
4588#else
4589 int i;
4590 PyObject *l = PyList_New(257);
4591 if (l == NULL) return NULL;
4592 for (i = 0; i < 257; i++) {
4593 PyObject *x = getarray(dxpairs[i]);
4594 if (x == NULL) {
4595 Py_DECREF(l);
4596 return NULL;
4597 }
4598 PyList_SetItem(l, i, x);
4599 }
4600 return l;
4601#endif
4602}
4603
4604#endif