blob: 6c71c27350384980d188e4bb9fdfd065e54b0fa4 [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);
Mark Dickinson6df863c92009-12-02 17:36:34 +00001194 /* cast to avoid undefined behaviour
1195 on overflow */
1196 i = (long)((unsigned long)a + b);
Guido van Rossum87780df2001-08-23 02:58:07 +00001197 if ((i^a) < 0 && (i^b) < 0)
1198 goto slow_add;
1199 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001200 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001201 else if (PyString_CheckExact(v) &&
1202 PyString_CheckExact(w)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001203 x = string_concatenate(v, w, f, next_instr);
1204 /* string_concatenate consumed the ref to v */
1205 goto skip_decref_vx;
1206 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001207 else {
1208 slow_add:
Guido van Rossumc12da691997-07-17 23:12:42 +00001209 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001210 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001211 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001212 skip_decref_vx:
Guido van Rossumb209a111997-04-29 18:18:01 +00001213 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001214 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001215 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001216 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001217
Guido van Rossum374a9221991-04-04 10:40:29 +00001218 case BINARY_SUBTRACT:
1219 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001220 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001221 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001222 /* INLINE: int - int */
1223 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001224 a = PyInt_AS_LONG(v);
1225 b = PyInt_AS_LONG(w);
Mark Dickinson6df863c92009-12-02 17:36:34 +00001226 /* cast to avoid undefined behaviour
1227 on overflow */
1228 i = (long)((unsigned long)a - b);
Guido van Rossum87780df2001-08-23 02:58:07 +00001229 if ((i^a) < 0 && (i^~b) < 0)
1230 goto slow_sub;
1231 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001232 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001233 else {
1234 slow_sub:
Guido van Rossumc12da691997-07-17 23:12:42 +00001235 x = PyNumber_Subtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001236 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001237 Py_DECREF(v);
1238 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001239 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001240 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001241 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001242
Guido van Rossum374a9221991-04-04 10:40:29 +00001243 case BINARY_SUBSCR:
1244 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001245 v = TOP();
Tim Petersb1c46982001-10-05 20:41:38 +00001246 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001247 /* INLINE: list[int] */
Neal Norwitz814e9382006-03-02 07:54:28 +00001248 Py_ssize_t i = PyInt_AsSsize_t(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001249 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +00001250 i += PyList_GET_SIZE(v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001251 if (i >= 0 && i < PyList_GET_SIZE(v)) {
Guido van Rossumfa00e951998-07-08 15:02:37 +00001252 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001253 Py_INCREF(x);
1254 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001255 else
1256 goto slow_get;
Guido van Rossumc12da691997-07-17 23:12:42 +00001257 }
1258 else
Raymond Hettinger467a6982004-04-07 11:39:21 +00001259 slow_get:
Guido van Rossumc12da691997-07-17 23:12:42 +00001260 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001261 Py_DECREF(v);
1262 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001263 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001264 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001265 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001266
Guido van Rossum7928cd71991-10-24 14:59:31 +00001267 case BINARY_LSHIFT:
1268 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001269 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001270 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001271 Py_DECREF(v);
1272 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001273 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001274 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001275 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001276
Guido van Rossum7928cd71991-10-24 14:59:31 +00001277 case BINARY_RSHIFT:
1278 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001279 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001280 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001281 Py_DECREF(v);
1282 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001283 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001284 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001285 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001286
Guido van Rossum7928cd71991-10-24 14:59:31 +00001287 case BINARY_AND:
1288 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001289 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001290 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001291 Py_DECREF(v);
1292 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001293 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001294 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001295 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001296
Guido van Rossum7928cd71991-10-24 14:59:31 +00001297 case BINARY_XOR:
1298 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001299 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001300 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001301 Py_DECREF(v);
1302 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001303 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001304 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001305 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001306
Guido van Rossum7928cd71991-10-24 14:59:31 +00001307 case BINARY_OR:
1308 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001309 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001310 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001311 Py_DECREF(v);
1312 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001313 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001314 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001315 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001316
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001317 case LIST_APPEND:
1318 w = POP();
1319 v = POP();
1320 err = PyList_Append(v, w);
1321 Py_DECREF(v);
1322 Py_DECREF(w);
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001323 if (err == 0) {
1324 PREDICT(JUMP_ABSOLUTE);
1325 continue;
1326 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001327 break;
1328
Thomas Wouters434d0822000-08-24 20:11:32 +00001329 case INPLACE_POWER:
1330 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001331 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001332 x = PyNumber_InPlacePower(v, w, Py_None);
1333 Py_DECREF(v);
1334 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001335 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001336 if (x != NULL) continue;
1337 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001338
Thomas Wouters434d0822000-08-24 20:11:32 +00001339 case INPLACE_MULTIPLY:
1340 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001341 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001342 x = PyNumber_InPlaceMultiply(v, w);
1343 Py_DECREF(v);
1344 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001345 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001346 if (x != NULL) continue;
1347 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001348
Thomas Wouters434d0822000-08-24 20:11:32 +00001349 case INPLACE_DIVIDE:
Tim Peters54b11912001-12-25 18:49:11 +00001350 if (!_Py_QnewFlag) {
1351 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001352 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001353 x = PyNumber_InPlaceDivide(v, w);
1354 Py_DECREF(v);
1355 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001356 SET_TOP(x);
Tim Peters54b11912001-12-25 18:49:11 +00001357 if (x != NULL) continue;
1358 break;
1359 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001360 /* -Qnew is in effect: fall through to
Tim Peters54b11912001-12-25 18:49:11 +00001361 INPLACE_TRUE_DIVIDE */
1362 case INPLACE_TRUE_DIVIDE:
Thomas Wouters434d0822000-08-24 20:11:32 +00001363 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001364 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001365 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001366 Py_DECREF(v);
1367 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001368 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001369 if (x != NULL) continue;
1370 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001371
Guido van Rossum4668b002001-08-08 05:00:18 +00001372 case INPLACE_FLOOR_DIVIDE:
1373 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001374 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001375 x = PyNumber_InPlaceFloorDivide(v, w);
1376 Py_DECREF(v);
1377 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001378 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001379 if (x != NULL) continue;
1380 break;
1381
Thomas Wouters434d0822000-08-24 20:11:32 +00001382 case INPLACE_MODULO:
1383 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001384 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001385 x = PyNumber_InPlaceRemainder(v, w);
1386 Py_DECREF(v);
1387 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001388 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001389 if (x != NULL) continue;
1390 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001391
Thomas Wouters434d0822000-08-24 20:11:32 +00001392 case INPLACE_ADD:
1393 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001394 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001395 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001396 /* INLINE: int + int */
1397 register long a, b, i;
1398 a = PyInt_AS_LONG(v);
1399 b = PyInt_AS_LONG(w);
1400 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001401 if ((i^a) < 0 && (i^b) < 0)
1402 goto slow_iadd;
1403 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001404 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001405 else if (PyString_CheckExact(v) &&
1406 PyString_CheckExact(w)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001407 x = string_concatenate(v, w, f, next_instr);
1408 /* string_concatenate consumed the ref to v */
1409 goto skip_decref_v;
1410 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001411 else {
1412 slow_iadd:
Thomas Wouters434d0822000-08-24 20:11:32 +00001413 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001414 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001415 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001416 skip_decref_v:
Thomas Wouters434d0822000-08-24 20:11:32 +00001417 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001418 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001419 if (x != NULL) continue;
1420 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001421
Thomas Wouters434d0822000-08-24 20:11:32 +00001422 case INPLACE_SUBTRACT:
1423 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001424 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001425 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001426 /* INLINE: int - int */
1427 register long a, b, i;
1428 a = PyInt_AS_LONG(v);
1429 b = PyInt_AS_LONG(w);
1430 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001431 if ((i^a) < 0 && (i^~b) < 0)
1432 goto slow_isub;
1433 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001434 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001435 else {
1436 slow_isub:
Thomas Wouters434d0822000-08-24 20:11:32 +00001437 x = PyNumber_InPlaceSubtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001438 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001439 Py_DECREF(v);
1440 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001441 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001442 if (x != NULL) continue;
1443 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001444
Thomas Wouters434d0822000-08-24 20:11:32 +00001445 case INPLACE_LSHIFT:
1446 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001447 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001448 x = PyNumber_InPlaceLshift(v, w);
1449 Py_DECREF(v);
1450 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001451 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001452 if (x != NULL) continue;
1453 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001454
Thomas Wouters434d0822000-08-24 20:11:32 +00001455 case INPLACE_RSHIFT:
1456 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001457 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001458 x = PyNumber_InPlaceRshift(v, w);
1459 Py_DECREF(v);
1460 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001461 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001462 if (x != NULL) continue;
1463 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001464
Thomas Wouters434d0822000-08-24 20:11:32 +00001465 case INPLACE_AND:
1466 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001467 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001468 x = PyNumber_InPlaceAnd(v, w);
1469 Py_DECREF(v);
1470 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001471 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001472 if (x != NULL) continue;
1473 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001474
Thomas Wouters434d0822000-08-24 20:11:32 +00001475 case INPLACE_XOR:
1476 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001477 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001478 x = PyNumber_InPlaceXor(v, w);
1479 Py_DECREF(v);
1480 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001481 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001482 if (x != NULL) continue;
1483 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001484
Thomas Wouters434d0822000-08-24 20:11:32 +00001485 case INPLACE_OR:
1486 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001487 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001488 x = PyNumber_InPlaceOr(v, w);
1489 Py_DECREF(v);
1490 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001491 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001492 if (x != NULL) continue;
1493 break;
1494
Guido van Rossum374a9221991-04-04 10:40:29 +00001495 case SLICE+0:
1496 case SLICE+1:
1497 case SLICE+2:
1498 case SLICE+3:
1499 if ((opcode-SLICE) & 2)
1500 w = POP();
1501 else
1502 w = NULL;
1503 if ((opcode-SLICE) & 1)
1504 v = POP();
1505 else
1506 v = NULL;
Raymond Hettinger663004b2003-01-09 15:24:30 +00001507 u = TOP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001508 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001509 Py_DECREF(u);
1510 Py_XDECREF(v);
1511 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001512 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001513 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001514 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001515
Guido van Rossum374a9221991-04-04 10:40:29 +00001516 case STORE_SLICE+0:
1517 case STORE_SLICE+1:
1518 case STORE_SLICE+2:
1519 case STORE_SLICE+3:
1520 if ((opcode-STORE_SLICE) & 2)
1521 w = POP();
1522 else
1523 w = NULL;
1524 if ((opcode-STORE_SLICE) & 1)
1525 v = POP();
1526 else
1527 v = NULL;
1528 u = POP();
1529 t = POP();
1530 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +00001531 Py_DECREF(t);
1532 Py_DECREF(u);
1533 Py_XDECREF(v);
1534 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001535 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001536 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001537
Guido van Rossum374a9221991-04-04 10:40:29 +00001538 case DELETE_SLICE+0:
1539 case DELETE_SLICE+1:
1540 case DELETE_SLICE+2:
1541 case DELETE_SLICE+3:
1542 if ((opcode-DELETE_SLICE) & 2)
1543 w = POP();
1544 else
1545 w = NULL;
1546 if ((opcode-DELETE_SLICE) & 1)
1547 v = POP();
1548 else
1549 v = NULL;
1550 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001551 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +00001552 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00001553 Py_DECREF(u);
1554 Py_XDECREF(v);
1555 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001556 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001557 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001558
Guido van Rossum374a9221991-04-04 10:40:29 +00001559 case STORE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001560 w = TOP();
1561 v = SECOND();
1562 u = THIRD();
1563 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001564 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001565 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001566 Py_DECREF(u);
1567 Py_DECREF(v);
1568 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001569 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001570 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001571
Guido van Rossum374a9221991-04-04 10:40:29 +00001572 case DELETE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001573 w = TOP();
1574 v = SECOND();
1575 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001576 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001577 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001578 Py_DECREF(v);
1579 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001580 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001581 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001582
Guido van Rossum374a9221991-04-04 10:40:29 +00001583 case PRINT_EXPR:
1584 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001585 w = PySys_GetObject("displayhook");
1586 if (w == NULL) {
1587 PyErr_SetString(PyExc_RuntimeError,
1588 "lost sys.displayhook");
1589 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001590 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001591 }
1592 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001593 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001594 if (x == NULL)
1595 err = -1;
1596 }
1597 if (err == 0) {
1598 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001599 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001600 if (w == NULL)
1601 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001602 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001603 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001604 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001605 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001606
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001607 case PRINT_ITEM_TO:
1608 w = stream = POP();
1609 /* fall through to PRINT_ITEM */
1610
Guido van Rossum374a9221991-04-04 10:40:29 +00001611 case PRINT_ITEM:
1612 v = POP();
Barry Warsaw093abe02000-08-29 04:56:13 +00001613 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001614 w = PySys_GetObject("stdout");
1615 if (w == NULL) {
1616 PyErr_SetString(PyExc_RuntimeError,
1617 "lost sys.stdout");
1618 err = -1;
1619 }
Guido van Rossum8f183201997-12-31 05:53:15 +00001620 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001621 /* PyFile_SoftSpace() can exececute arbitrary code
1622 if sys.stdout is an instance with a __getattr__.
1623 If __getattr__ raises an exception, w will
1624 be freed, so we need to prevent that temporarily. */
1625 Py_XINCREF(w);
Tim Peters8e5fd532002-03-24 19:25:00 +00001626 if (w != NULL && PyFile_SoftSpace(w, 0))
Guido van Rossumbe270261997-05-22 22:26:18 +00001627 err = PyFile_WriteString(" ", w);
1628 if (err == 0)
1629 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001630 if (err == 0) {
Tim Peters8e5fd532002-03-24 19:25:00 +00001631 /* XXX move into writeobject() ? */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001632 if (PyString_Check(v)) {
1633 char *s = PyString_AS_STRING(v);
1634 Py_ssize_t len = PyString_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001635 if (len == 0 ||
1636 !isspace(Py_CHARMASK(s[len-1])) ||
1637 s[len-1] == ' ')
1638 PyFile_SoftSpace(w, 1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001639 }
Martin v. Löwis8d3ce5a2001-12-18 22:36:40 +00001640#ifdef Py_USING_UNICODE
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001641 else if (PyUnicode_Check(v)) {
1642 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
Martin v. Löwis66851282006-04-22 11:40:03 +00001643 Py_ssize_t len = PyUnicode_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001644 if (len == 0 ||
1645 !Py_UNICODE_ISSPACE(s[len-1]) ||
1646 s[len-1] == ' ')
1647 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001648 }
Michael W. Hudsond95c8282002-05-20 13:56:11 +00001649#endif
Tim Peters8e5fd532002-03-24 19:25:00 +00001650 else
1651 PyFile_SoftSpace(w, 1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001652 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001653 Py_XDECREF(w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001654 Py_DECREF(v);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001655 Py_XDECREF(stream);
1656 stream = NULL;
1657 if (err == 0)
1658 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001659 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001660
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001661 case PRINT_NEWLINE_TO:
1662 w = stream = POP();
1663 /* fall through to PRINT_NEWLINE */
1664
Guido van Rossum374a9221991-04-04 10:40:29 +00001665 case PRINT_NEWLINE:
Barry Warsaw093abe02000-08-29 04:56:13 +00001666 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001667 w = PySys_GetObject("stdout");
Jeffrey Yasskin69614982008-12-11 05:21:18 +00001668 if (w == NULL) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001669 PyErr_SetString(PyExc_RuntimeError,
1670 "lost sys.stdout");
Jeffrey Yasskin69614982008-12-11 05:21:18 +00001671 why = WHY_EXCEPTION;
1672 }
Guido van Rossum3165fe61992-09-25 21:59:05 +00001673 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001674 if (w != NULL) {
Georg Brandlaa76d772008-07-01 20:56:03 +00001675 /* w.write() may replace sys.stdout, so we
1676 * have to keep our reference to it */
Amaury Forgeot d'Arcbdd941f2008-07-01 20:38:04 +00001677 Py_INCREF(w);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001678 err = PyFile_WriteString("\n", w);
1679 if (err == 0)
1680 PyFile_SoftSpace(w, 0);
Amaury Forgeot d'Arcbdd941f2008-07-01 20:38:04 +00001681 Py_DECREF(w);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001682 }
1683 Py_XDECREF(stream);
1684 stream = NULL;
Guido van Rossum374a9221991-04-04 10:40:29 +00001685 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001686
Thomas Wouters434d0822000-08-24 20:11:32 +00001687
1688#ifdef CASE_TOO_BIG
1689 default: switch (opcode) {
1690#endif
Guido van Rossumf10570b1995-07-07 22:53:21 +00001691 case RAISE_VARARGS:
1692 u = v = w = NULL;
1693 switch (oparg) {
1694 case 3:
1695 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001696 /* Fallthrough */
1697 case 2:
1698 v = POP(); /* value */
1699 /* Fallthrough */
1700 case 1:
1701 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001702 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001703 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001704 break;
1705 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001706 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001707 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001708 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001709 break;
1710 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001711 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001712
Guido van Rossum374a9221991-04-04 10:40:29 +00001713 case LOAD_LOCALS:
Raymond Hettinger467a6982004-04-07 11:39:21 +00001714 if ((x = f->f_locals) != NULL) {
1715 Py_INCREF(x);
1716 PUSH(x);
1717 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001718 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001719 PyErr_SetString(PyExc_SystemError, "no locals");
Guido van Rossum374a9221991-04-04 10:40:29 +00001720 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001721
Guido van Rossum374a9221991-04-04 10:40:29 +00001722 case RETURN_VALUE:
1723 retval = POP();
1724 why = WHY_RETURN;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001725 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001726
Tim Peters5ca576e2001-06-18 22:08:13 +00001727 case YIELD_VALUE:
1728 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001729 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001730 why = WHY_YIELD;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001731 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001732
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001733 case EXEC_STMT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001734 w = TOP();
1735 v = SECOND();
1736 u = THIRD();
1737 STACKADJ(-3);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001738 READ_TIMESTAMP(intr0);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001739 err = exec_statement(f, u, v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001740 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00001741 Py_DECREF(u);
1742 Py_DECREF(v);
1743 Py_DECREF(w);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001744 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001745
Guido van Rossum374a9221991-04-04 10:40:29 +00001746 case POP_BLOCK:
1747 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001748 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001749 while (STACK_LEVEL() > b->b_level) {
1750 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001751 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001752 }
1753 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001754 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001755
Jeffrey Yasskin9063a992008-03-03 01:27:03 +00001756 PREDICTED(END_FINALLY);
Guido van Rossum374a9221991-04-04 10:40:29 +00001757 case END_FINALLY:
1758 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001759 if (PyInt_Check(v)) {
Raymond Hettinger7c958652004-04-06 10:11:10 +00001760 why = (enum why_code) PyInt_AS_LONG(v);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001761 assert(why != WHY_YIELD);
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00001762 if (why == WHY_RETURN ||
1763 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001764 retval = POP();
1765 }
Thomas Wouterse2176022007-09-20 17:35:10 +00001766 else if (PyExceptionClass_Check(v) ||
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001767 PyString_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001768 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001769 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001770 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001771 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001772 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001773 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001774 else if (v != Py_None) {
1775 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001776 "'finally' pops bad exception");
1777 why = WHY_EXCEPTION;
1778 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001779 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001780 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001781
Guido van Rossum374a9221991-04-04 10:40:29 +00001782 case BUILD_CLASS:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001783 u = TOP();
1784 v = SECOND();
1785 w = THIRD();
1786 STACKADJ(-2);
Guido van Rossum25831651993-05-19 14:50:45 +00001787 x = build_class(u, v, w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001788 SET_TOP(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001789 Py_DECREF(u);
1790 Py_DECREF(v);
1791 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001792 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001793
Guido van Rossum374a9221991-04-04 10:40:29 +00001794 case STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001795 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001796 v = POP();
Raymond Hettinger467a6982004-04-07 11:39:21 +00001797 if ((x = f->f_locals) != NULL) {
Raymond Hettinger66bd2332004-08-02 08:30:07 +00001798 if (PyDict_CheckExact(x))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001799 err = PyDict_SetItem(x, w, v);
1800 else
1801 err = PyObject_SetItem(x, w, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001802 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001803 if (err == 0) continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001804 break;
1805 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001806 PyErr_Format(PyExc_SystemError,
1807 "no locals found when storing %s",
1808 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001809 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001810
Guido van Rossum374a9221991-04-04 10:40:29 +00001811 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001812 w = GETITEM(names, oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001813 if ((x = f->f_locals) != NULL) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001814 if ((err = PyObject_DelItem(x, w)) != 0)
Raymond Hettinger467a6982004-04-07 11:39:21 +00001815 format_exc_check_arg(PyExc_NameError,
Thomas Wouterse2176022007-09-20 17:35:10 +00001816 NAME_ERROR_MSG,
1817 w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001818 break;
1819 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001820 PyErr_Format(PyExc_SystemError,
1821 "no locals when deleting %s",
1822 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001823 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001824
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001825 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001826 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001827 v = POP();
Thomas Wouterse2176022007-09-20 17:35:10 +00001828 if (PyTuple_CheckExact(v) &&
1829 PyTuple_GET_SIZE(v) == oparg) {
1830 PyObject **items = \
1831 ((PyTupleObject *)v)->ob_item;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001832 while (oparg--) {
1833 w = items[oparg];
1834 Py_INCREF(w);
1835 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001836 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001837 Py_DECREF(v);
1838 continue;
Thomas Wouterse2176022007-09-20 17:35:10 +00001839 } else if (PyList_CheckExact(v) &&
1840 PyList_GET_SIZE(v) == oparg) {
1841 PyObject **items = \
1842 ((PyListObject *)v)->ob_item;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001843 while (oparg--) {
1844 w = items[oparg];
1845 Py_INCREF(w);
1846 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001847 }
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001848 } else if (unpack_iterable(v, oparg,
Thomas Wouterse2176022007-09-20 17:35:10 +00001849 stack_pointer + oparg)) {
Tim Petersd6d010b2001-06-21 02:49:55 +00001850 stack_pointer += oparg;
Georg Brandl5cb76c12007-03-21 09:00:39 +00001851 } else {
1852 /* unpack_iterable() raised an exception */
Barry Warsawe42b18f1997-08-25 22:13:04 +00001853 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001854 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001855 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001856 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001857
Guido van Rossum374a9221991-04-04 10:40:29 +00001858 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001859 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001860 v = TOP();
1861 u = SECOND();
1862 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001863 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1864 Py_DECREF(v);
1865 Py_DECREF(u);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001866 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001867 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001868
Guido van Rossum374a9221991-04-04 10:40:29 +00001869 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001870 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001871 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001872 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1873 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001874 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001875 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001876
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001877 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001878 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001879 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001880 err = PyDict_SetItem(f->f_globals, w, v);
1881 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001882 if (err == 0) continue;
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001883 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001884
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001885 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001886 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001887 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001888 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001889 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001890 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001891
Guido van Rossum374a9221991-04-04 10:40:29 +00001892 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001893 w = GETITEM(names, oparg);
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001894 if ((v = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001895 PyErr_Format(PyExc_SystemError,
1896 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001897 PyObject_REPR(w));
Jeffrey Yasskin69614982008-12-11 05:21:18 +00001898 why = WHY_EXCEPTION;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001899 break;
1900 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001901 if (PyDict_CheckExact(v)) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001902 x = PyDict_GetItem(v, w);
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001903 Py_XINCREF(x);
1904 }
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001905 else {
1906 x = PyObject_GetItem(v, w);
1907 if (x == NULL && PyErr_Occurred()) {
Thomas Wouterse2176022007-09-20 17:35:10 +00001908 if (!PyErr_ExceptionMatches(
1909 PyExc_KeyError))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001910 break;
1911 PyErr_Clear();
1912 }
1913 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001914 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001915 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001916 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001917 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001918 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001919 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001920 PyExc_NameError,
Thomas Wouterse2176022007-09-20 17:35:10 +00001921 NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001922 break;
1923 }
1924 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001925 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001926 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001927 PUSH(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001928 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001929
Guido van Rossum374a9221991-04-04 10:40:29 +00001930 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001931 w = GETITEM(names, oparg);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001932 if (PyString_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00001933 /* Inline the PyDict_GetItem() calls.
1934 WARNING: this is an extreme speed hack.
1935 Do not try this at home. */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001936 long hash = ((PyStringObject *)w)->ob_shash;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001937 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001938 PyDictObject *d;
Armin Rigo35f6d362006-06-01 13:19:12 +00001939 PyDictEntry *e;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001940 d = (PyDictObject *)(f->f_globals);
Armin Rigo35f6d362006-06-01 13:19:12 +00001941 e = d->ma_lookup(d, w, hash);
1942 if (e == NULL) {
1943 x = NULL;
1944 break;
1945 }
1946 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001947 if (x != NULL) {
1948 Py_INCREF(x);
1949 PUSH(x);
1950 continue;
1951 }
1952 d = (PyDictObject *)(f->f_builtins);
Armin Rigo35f6d362006-06-01 13:19:12 +00001953 e = d->ma_lookup(d, w, hash);
1954 if (e == NULL) {
1955 x = NULL;
1956 break;
1957 }
1958 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001959 if (x != NULL) {
1960 Py_INCREF(x);
1961 PUSH(x);
1962 continue;
1963 }
1964 goto load_global_error;
1965 }
1966 }
1967 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00001968 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001969 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001970 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001971 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001972 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00001973 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001974 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001975 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001976 break;
1977 }
1978 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001979 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001980 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001981 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001982
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001983 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001984 x = GETLOCAL(oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001985 if (x != NULL) {
1986 SETLOCAL(oparg, NULL);
1987 continue;
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001988 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001989 format_exc_check_arg(
1990 PyExc_UnboundLocalError,
1991 UNBOUNDLOCAL_ERROR_MSG,
1992 PyTuple_GetItem(co->co_varnames, oparg)
1993 );
1994 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001995
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001996 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001997 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001998 Py_INCREF(x);
1999 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00002000 if (x != NULL) continue;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002001 break;
2002
2003 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00002004 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002005 w = PyCell_Get(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002006 if (w != NULL) {
2007 PUSH(w);
2008 continue;
Jeremy Hylton2524d692001-02-05 17:23:16 +00002009 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00002010 err = -1;
2011 /* Don't stomp existing exception */
2012 if (PyErr_Occurred())
2013 break;
Richard Jonescebbefc2006-05-23 18:28:17 +00002014 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
2015 v = PyTuple_GET_ITEM(co->co_cellvars,
Raymond Hettinger467a6982004-04-07 11:39:21 +00002016 oparg);
2017 format_exc_check_arg(
2018 PyExc_UnboundLocalError,
2019 UNBOUNDLOCAL_ERROR_MSG,
2020 v);
2021 } else {
Thomas Wouterse2176022007-09-20 17:35:10 +00002022 v = PyTuple_GET_ITEM(co->co_freevars, oparg -
2023 PyTuple_GET_SIZE(co->co_cellvars));
2024 format_exc_check_arg(PyExc_NameError,
2025 UNBOUNDFREE_ERROR_MSG, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002026 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002027 break;
2028
2029 case STORE_DEREF:
2030 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00002031 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002032 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00002033 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002034 continue;
2035
Guido van Rossum374a9221991-04-04 10:40:29 +00002036 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00002037 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002038 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002039 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002040 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002041 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002042 }
2043 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002044 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002045 }
2046 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002047
Guido van Rossum374a9221991-04-04 10:40:29 +00002048 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00002049 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002050 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002051 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002052 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00002053 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002054 }
2055 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002056 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002057 }
2058 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002059
Guido van Rossum374a9221991-04-04 10:40:29 +00002060 case BUILD_MAP:
Raymond Hettingerfd7ed402007-12-18 21:24:09 +00002061 x = _PyDict_NewPresized((Py_ssize_t)oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002062 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002063 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002064 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002065
Raymond Hettingereffde122007-12-18 18:26:18 +00002066 case STORE_MAP:
2067 w = TOP(); /* key */
2068 u = SECOND(); /* value */
2069 v = THIRD(); /* dict */
2070 STACKADJ(-2);
2071 assert (PyDict_CheckExact(v));
2072 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2073 Py_DECREF(u);
2074 Py_DECREF(w);
2075 if (err == 0) continue;
2076 break;
2077
Guido van Rossum374a9221991-04-04 10:40:29 +00002078 case LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00002079 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002080 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002081 x = PyObject_GetAttr(v, w);
2082 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002083 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002084 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002085 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002086
Guido van Rossum374a9221991-04-04 10:40:29 +00002087 case COMPARE_OP:
2088 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002089 v = TOP();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00002090 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00002091 /* INLINE: cmp(int, int) */
2092 register long a, b;
2093 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00002094 a = PyInt_AS_LONG(v);
2095 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00002096 switch (oparg) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00002097 case PyCmp_LT: res = a < b; break;
2098 case PyCmp_LE: res = a <= b; break;
2099 case PyCmp_EQ: res = a == b; break;
2100 case PyCmp_NE: res = a != b; break;
2101 case PyCmp_GT: res = a > b; break;
2102 case PyCmp_GE: res = a >= b; break;
2103 case PyCmp_IS: res = v == w; break;
2104 case PyCmp_IS_NOT: res = v != w; break;
Guido van Rossumc12da691997-07-17 23:12:42 +00002105 default: goto slow_compare;
2106 }
2107 x = res ? Py_True : Py_False;
2108 Py_INCREF(x);
2109 }
2110 else {
2111 slow_compare:
2112 x = cmp_outcome(oparg, v, w);
2113 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002114 Py_DECREF(v);
2115 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002116 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00002117 if (x == NULL) break;
2118 PREDICT(JUMP_IF_FALSE);
2119 PREDICT(JUMP_IF_TRUE);
2120 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002121
Guido van Rossum374a9221991-04-04 10:40:29 +00002122 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00002123 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002124 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002125 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002126 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00002127 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002128 break;
2129 }
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00002130 Py_INCREF(x);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002131 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002132 u = TOP();
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002133 if (PyInt_AsLong(u) != -1 || PyErr_Occurred())
2134 w = PyTuple_Pack(5,
2135 w,
2136 f->f_globals,
2137 f->f_locals == NULL ?
2138 Py_None : f->f_locals,
2139 v,
2140 u);
2141 else
2142 w = PyTuple_Pack(4,
2143 w,
2144 f->f_globals,
2145 f->f_locals == NULL ?
2146 Py_None : f->f_locals,
2147 v);
2148 Py_DECREF(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002149 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002150 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002151 u = POP();
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00002152 Py_DECREF(x);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002153 x = NULL;
2154 break;
2155 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002156 READ_TIMESTAMP(intr0);
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00002157 v = x;
2158 x = PyEval_CallObject(v, w);
2159 Py_DECREF(v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002160 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002161 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002162 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002163 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002164 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002165
Thomas Wouters52152252000-08-17 22:55:00 +00002166 case IMPORT_STAR:
2167 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002168 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002169 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002170 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00002171 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002172 break;
2173 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002174 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002175 err = import_all_from(x, v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002176 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002177 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00002178 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002179 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002180 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002181
Thomas Wouters52152252000-08-17 22:55:00 +00002182 case IMPORT_FROM:
Skip Montanaro496e6582002-08-06 17:47:40 +00002183 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00002184 v = TOP();
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002185 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002186 x = import_from(v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002187 READ_TIMESTAMP(intr1);
Thomas Wouters52152252000-08-17 22:55:00 +00002188 PUSH(x);
2189 if (x != NULL) continue;
2190 break;
2191
Guido van Rossum374a9221991-04-04 10:40:29 +00002192 case JUMP_FORWARD:
2193 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002194 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002195
Raymond Hettingerf606f872003-03-16 03:11:04 +00002196 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002197 case JUMP_IF_FALSE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002198 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002199 if (w == Py_True) {
2200 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002201 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002202 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002203 if (w == Py_False) {
2204 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002205 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002206 }
2207 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002208 if (err > 0)
2209 err = 0;
2210 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00002211 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002212 else
2213 break;
2214 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002215
Raymond Hettingerf606f872003-03-16 03:11:04 +00002216 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002217 case JUMP_IF_TRUE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002218 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002219 if (w == Py_False) {
2220 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002221 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002222 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002223 if (w == Py_True) {
2224 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002225 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002226 }
2227 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002228 if (err > 0) {
2229 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00002230 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002231 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002232 else if (err == 0)
2233 ;
2234 else
2235 break;
2236 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002237
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00002238 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002239 case JUMP_ABSOLUTE:
2240 JUMPTO(oparg);
Raymond Hettingerdc1d1ba2007-11-07 02:45:46 +00002241#if FAST_LOOPS
2242 /* Enabling this path speeds-up all while and for-loops by bypassing
2243 the per-loop checks for signals. By default, this should be turned-off
2244 because it prevents detection of a control-break in tight loops like
2245 "while 1: pass". Compile with this option turned-on when you need
2246 the speed-up and do not need break checking inside tight loops (ones
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00002247 that contain only instructions ending with goto fast_next_opcode).
Raymond Hettingerdc1d1ba2007-11-07 02:45:46 +00002248 */
2249 goto fast_next_opcode;
2250#else
Neil Schemenauerca2a2f12003-05-30 23:59:44 +00002251 continue;
Raymond Hettingerdc1d1ba2007-11-07 02:45:46 +00002252#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002253
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002254 case GET_ITER:
2255 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002256 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002257 x = PyObject_GetIter(v);
2258 Py_DECREF(v);
2259 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002260 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002261 PREDICT(FOR_ITER);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002262 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002263 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002264 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002265 break;
2266
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002267 PREDICTED_WITH_ARG(FOR_ITER);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002268 case FOR_ITER:
2269 /* before: [iter]; after: [iter, iter()] *or* [] */
2270 v = TOP();
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002271 x = (*v->ob_type->tp_iternext)(v);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002272 if (x != NULL) {
2273 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002274 PREDICT(STORE_FAST);
2275 PREDICT(UNPACK_SEQUENCE);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002276 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002277 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002278 if (PyErr_Occurred()) {
Thomas Wouterse2176022007-09-20 17:35:10 +00002279 if (!PyErr_ExceptionMatches(
2280 PyExc_StopIteration))
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002281 break;
2282 PyErr_Clear();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002283 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002284 /* iterator ended normally */
2285 x = v = POP();
2286 Py_DECREF(v);
2287 JUMPBY(oparg);
2288 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002289
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002290 case BREAK_LOOP:
2291 why = WHY_BREAK;
2292 goto fast_block_end;
2293
2294 case CONTINUE_LOOP:
2295 retval = PyInt_FromLong(oparg);
Neal Norwitz02104df2006-05-19 06:31:23 +00002296 if (!retval) {
2297 x = NULL;
2298 break;
2299 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002300 why = WHY_CONTINUE;
2301 goto fast_block_end;
2302
Guido van Rossum374a9221991-04-04 10:40:29 +00002303 case SETUP_LOOP:
2304 case SETUP_EXCEPT:
2305 case SETUP_FINALLY:
Thomas Wouterse2176022007-09-20 17:35:10 +00002306 /* NOTE: If you add any new block-setup opcodes that
2307 are not try/except/finally handlers, you may need
2308 to update the PyGen_NeedsFinalizing() function.
2309 */
Phillip J. Eby2ba96612006-04-10 17:51:05 +00002310
Guido van Rossumb209a111997-04-29 18:18:01 +00002311 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002312 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002313 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002314
Guido van Rossumc2e20742006-02-27 22:32:47 +00002315 case WITH_CLEANUP:
2316 {
Nick Coghlan7af53be2008-03-07 14:13:28 +00002317 /* At the top of the stack are 1-3 values indicating
2318 how/why we entered the finally clause:
2319 - TOP = None
2320 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2321 - TOP = WHY_*; no retval below it
2322 - (TOP, SECOND, THIRD) = exc_info()
2323 Below them is EXIT, the context.__exit__ bound method.
Guido van Rossumc2e20742006-02-27 22:32:47 +00002324 In the last case, we must call
Nick Coghlan7af53be2008-03-07 14:13:28 +00002325 EXIT(TOP, SECOND, THIRD)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002326 otherwise we must call
Nick Coghlan7af53be2008-03-07 14:13:28 +00002327 EXIT(None, None, None)
2328
2329 In all cases, we remove EXIT from the stack, leaving
2330 the rest in the same order.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002331
2332 In addition, if the stack represents an exception,
Guido van Rossumf6694362006-03-10 02:28:35 +00002333 *and* the function call returns a 'true' value, we
2334 "zap" this information, to prevent END_FINALLY from
2335 re-raising the exception. (But non-local gotos
2336 should still be resumed.)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002337 */
Tim Peters7df5e7f2006-05-26 23:14:37 +00002338
Nick Coghlan7af53be2008-03-07 14:13:28 +00002339 PyObject *exit_func;
2340
2341 u = POP();
2342 if (u == Py_None) {
2343 exit_func = TOP();
2344 SET_TOP(u);
2345 v = w = Py_None;
2346 }
2347 else if (PyInt_Check(u)) {
2348 switch(PyInt_AS_LONG(u)) {
2349 case WHY_RETURN:
2350 case WHY_CONTINUE:
2351 /* Retval in TOP. */
2352 exit_func = SECOND();
2353 SET_SECOND(TOP());
2354 SET_TOP(u);
2355 break;
2356 default:
2357 exit_func = TOP();
2358 SET_TOP(u);
2359 break;
2360 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002361 u = v = w = Py_None;
2362 }
2363 else {
Nick Coghlan7af53be2008-03-07 14:13:28 +00002364 v = TOP();
2365 w = SECOND();
2366 exit_func = THIRD();
2367 SET_TOP(u);
2368 SET_SECOND(v);
2369 SET_THIRD(w);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002370 }
Guido van Rossumf6694362006-03-10 02:28:35 +00002371 /* XXX Not the fastest way to call it... */
Nick Coghlan7af53be2008-03-07 14:13:28 +00002372 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2373 NULL);
Amaury Forgeot d'Arc79c9f762008-12-10 23:56:33 +00002374 Py_DECREF(exit_func);
2375 if (x == NULL)
Guido van Rossumf6694362006-03-10 02:28:35 +00002376 break; /* Go to error exit */
Amaury Forgeot d'Arc79c9f762008-12-10 23:56:33 +00002377
2378 if (u != Py_None)
2379 err = PyObject_IsTrue(x);
2380 else
2381 err = 0;
2382 Py_DECREF(x);
2383
2384 if (err < 0)
2385 break; /* Go to error exit */
2386 else if (err > 0) {
2387 err = 0;
Guido van Rossumf6694362006-03-10 02:28:35 +00002388 /* There was an exception and a true return */
Nick Coghlan7af53be2008-03-07 14:13:28 +00002389 STACKADJ(-2);
Guido van Rossumf6694362006-03-10 02:28:35 +00002390 Py_INCREF(Py_None);
2391 SET_TOP(Py_None);
Guido van Rossumf6694362006-03-10 02:28:35 +00002392 Py_DECREF(u);
2393 Py_DECREF(v);
2394 Py_DECREF(w);
2395 } else {
Nick Coghlan7af53be2008-03-07 14:13:28 +00002396 /* The stack was rearranged to remove EXIT
2397 above. Let END_FINALLY do its thing */
Guido van Rossumf6694362006-03-10 02:28:35 +00002398 }
Jeffrey Yasskin9063a992008-03-03 01:27:03 +00002399 PREDICT(END_FINALLY);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002400 break;
2401 }
2402
Guido van Rossumf10570b1995-07-07 22:53:21 +00002403 case CALL_FUNCTION:
Armin Rigo8817fcd2004-06-17 10:22:40 +00002404 {
2405 PyObject **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002406 PCALL(PCALL_ALL);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002407 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002408#ifdef WITH_TSC
Armin Rigo8817fcd2004-06-17 10:22:40 +00002409 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002410#else
Armin Rigo8817fcd2004-06-17 10:22:40 +00002411 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002412#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002413 stack_pointer = sp;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002414 PUSH(x);
2415 if (x != NULL)
2416 continue;
2417 break;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002418 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002419
Jeremy Hylton76901512000-03-28 23:49:17 +00002420 case CALL_FUNCTION_VAR:
2421 case CALL_FUNCTION_KW:
2422 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002423 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002424 int na = oparg & 0xff;
2425 int nk = (oparg>>8) & 0xff;
2426 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002427 int n = na + 2 * nk;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002428 PyObject **pfunc, *func, **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002429 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002430 if (flags & CALL_FLAG_VAR)
2431 n++;
2432 if (flags & CALL_FLAG_KW)
2433 n++;
2434 pfunc = stack_pointer - n - 1;
2435 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002436
Guido van Rossumac7be682001-01-17 15:42:30 +00002437 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002438 && PyMethod_GET_SELF(func) != NULL) {
2439 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002440 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002441 func = PyMethod_GET_FUNCTION(func);
2442 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002443 Py_DECREF(*pfunc);
2444 *pfunc = self;
2445 na++;
2446 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002447 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002448 Py_INCREF(func);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002449 sp = stack_pointer;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002450 READ_TIMESTAMP(intr0);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002451 x = ext_do_call(func, &sp, flags, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002452 READ_TIMESTAMP(intr1);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002453 stack_pointer = sp;
Jeremy Hylton76901512000-03-28 23:49:17 +00002454 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002455
Jeremy Hylton76901512000-03-28 23:49:17 +00002456 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002457 w = POP();
2458 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002459 }
2460 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002461 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002462 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002463 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002464 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002465
Guido van Rossum681d79a1995-07-18 14:51:37 +00002466 case MAKE_FUNCTION:
2467 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002468 x = PyFunction_New(v, f->f_globals);
2469 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002470 /* XXX Maybe this should be a separate opcode? */
2471 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002472 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002473 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002474 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002475 x = NULL;
2476 break;
2477 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002478 while (--oparg >= 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002479 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002480 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002481 }
2482 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002483 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002484 }
2485 PUSH(x);
2486 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002487
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002488 case MAKE_CLOSURE:
2489 {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002490 v = POP(); /* code object */
2491 x = PyFunction_New(v, f->f_globals);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002492 Py_DECREF(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002493 if (x != NULL) {
2494 v = POP();
Jeffrey Yasskin69614982008-12-11 05:21:18 +00002495 if (PyFunction_SetClosure(x, v) != 0) {
2496 /* Can't happen unless bytecode is corrupt. */
2497 why = WHY_EXCEPTION;
2498 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002499 Py_DECREF(v);
2500 }
2501 if (x != NULL && oparg > 0) {
2502 v = PyTuple_New(oparg);
2503 if (v == NULL) {
2504 Py_DECREF(x);
2505 x = NULL;
2506 break;
2507 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002508 while (--oparg >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002509 w = POP();
2510 PyTuple_SET_ITEM(v, oparg, w);
2511 }
Jeffrey Yasskin69614982008-12-11 05:21:18 +00002512 if (PyFunction_SetDefaults(x, v) != 0) {
2513 /* Can't happen unless
2514 PyFunction_SetDefaults changes. */
2515 why = WHY_EXCEPTION;
2516 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002517 Py_DECREF(v);
2518 }
2519 PUSH(x);
2520 break;
2521 }
2522
Guido van Rossum8861b741996-07-30 16:49:37 +00002523 case BUILD_SLICE:
2524 if (oparg == 3)
2525 w = POP();
2526 else
2527 w = NULL;
2528 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002529 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002530 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002531 Py_DECREF(u);
2532 Py_DECREF(v);
2533 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002534 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002535 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002536 break;
2537
Fred Drakeef8ace32000-08-24 00:32:09 +00002538 case EXTENDED_ARG:
2539 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002540 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002541 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002542
Guido van Rossum374a9221991-04-04 10:40:29 +00002543 default:
2544 fprintf(stderr,
2545 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002546 PyCode_Addr2Line(f->f_code, f->f_lasti),
2547 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002548 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002549 why = WHY_EXCEPTION;
2550 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002551
2552#ifdef CASE_TOO_BIG
2553 }
2554#endif
2555
Guido van Rossum374a9221991-04-04 10:40:29 +00002556 } /* switch */
2557
2558 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002559
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002560 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002561
Guido van Rossum374a9221991-04-04 10:40:29 +00002562 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002563
Guido van Rossum374a9221991-04-04 10:40:29 +00002564 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002565 if (err == 0 && x != NULL) {
2566#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002567 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002568 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002569 fprintf(stderr,
2570 "XXX undetected error\n");
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002571 else {
2572#endif
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002573 READ_TIMESTAMP(loop1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002574 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002575#ifdef CHECKEXC
2576 }
2577#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +00002578 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002579 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002580 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002581 err = 0;
2582 }
2583
Guido van Rossum374a9221991-04-04 10:40:29 +00002584 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002585
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002586 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002587 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002588 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002589 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002590 why = WHY_EXCEPTION;
2591 }
2592 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002593#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002594 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002595 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002596 if (PyErr_Occurred()) {
Neal Norwitz8250fbe2008-01-27 17:12:15 +00002597 char buf[128];
Jeremy Hylton904ed862003-11-05 17:29:35 +00002598 sprintf(buf, "Stack unwind with exception "
2599 "set and why=%d", why);
2600 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002601 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002602 }
2603#endif
2604
2605 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002606
Guido van Rossum374a9221991-04-04 10:40:29 +00002607 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002608 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002609
Fred Drake8f51f542001-10-04 14:48:42 +00002610 if (tstate->c_tracefunc != NULL)
2611 call_exc_trace(tstate->c_tracefunc,
2612 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002613 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002614
Guido van Rossum374a9221991-04-04 10:40:29 +00002615 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002616
Guido van Rossum374a9221991-04-04 10:40:29 +00002617 if (why == WHY_RERAISE)
2618 why = WHY_EXCEPTION;
2619
2620 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002621
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002622fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002623 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002624 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002625
Tim Peters8a5c3c72004-04-05 19:36:21 +00002626 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002627 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2628 /* For a continue inside a try block,
2629 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002630 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2631 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002632 why = WHY_NOT;
2633 JUMPTO(PyInt_AS_LONG(retval));
2634 Py_DECREF(retval);
2635 break;
2636 }
2637
Guido van Rossum374a9221991-04-04 10:40:29 +00002638 while (STACK_LEVEL() > b->b_level) {
2639 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002640 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002641 }
2642 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2643 why = WHY_NOT;
2644 JUMPTO(b->b_handler);
2645 break;
2646 }
2647 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002648 (b->b_type == SETUP_EXCEPT &&
2649 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002650 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002651 PyObject *exc, *val, *tb;
2652 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002653 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002654 val = Py_None;
2655 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002656 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002657 /* Make the raw exception data
2658 available to the handler,
2659 so a program can emulate the
2660 Python main loop. Don't do
2661 this for 'finally'. */
2662 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002663 PyErr_NormalizeException(
2664 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002665 set_exc_info(tstate,
2666 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002667 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002668 if (tb == NULL) {
2669 Py_INCREF(Py_None);
2670 PUSH(Py_None);
2671 } else
2672 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002673 PUSH(val);
2674 PUSH(exc);
2675 }
2676 else {
Raymond Hettinger06032cb2004-04-06 09:37:35 +00002677 if (why & (WHY_RETURN | WHY_CONTINUE))
Guido van Rossum374a9221991-04-04 10:40:29 +00002678 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002679 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002680 PUSH(v);
2681 }
2682 why = WHY_NOT;
2683 JUMPTO(b->b_handler);
2684 break;
2685 }
2686 } /* unwind stack */
2687
2688 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002689
Guido van Rossum374a9221991-04-04 10:40:29 +00002690 if (why != WHY_NOT)
2691 break;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002692 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002693
Guido van Rossum374a9221991-04-04 10:40:29 +00002694 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002695
Tim Peters8a5c3c72004-04-05 19:36:21 +00002696 assert(why != WHY_YIELD);
2697 /* Pop remaining stack entries. */
2698 while (!EMPTY()) {
2699 v = POP();
2700 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002701 }
2702
Tim Peters8a5c3c72004-04-05 19:36:21 +00002703 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002704 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002705
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002706fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002707 if (tstate->use_tracing) {
Barry Warsawe2eca0b2005-08-15 18:14:19 +00002708 if (tstate->c_tracefunc) {
2709 if (why == WHY_RETURN || why == WHY_YIELD) {
2710 if (call_trace(tstate->c_tracefunc,
2711 tstate->c_traceobj, f,
2712 PyTrace_RETURN, retval)) {
2713 Py_XDECREF(retval);
2714 retval = NULL;
2715 why = WHY_EXCEPTION;
2716 }
2717 }
2718 else if (why == WHY_EXCEPTION) {
2719 call_trace_protected(tstate->c_tracefunc,
2720 tstate->c_traceobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002721 PyTrace_RETURN, NULL);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002722 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002723 }
Fred Drake8f51f542001-10-04 14:48:42 +00002724 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002725 if (why == WHY_EXCEPTION)
2726 call_trace_protected(tstate->c_profilefunc,
2727 tstate->c_profileobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002728 PyTrace_RETURN, NULL);
Fred Drake4ec5d562001-10-04 19:26:43 +00002729 else if (call_trace(tstate->c_profilefunc,
2730 tstate->c_profileobj, f,
2731 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002732 Py_XDECREF(retval);
2733 retval = NULL;
2734 why = WHY_EXCEPTION;
2735 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002736 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002737 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002738
Tim Peters7df5e7f2006-05-26 23:14:37 +00002739 if (tstate->frame->f_exc_type != NULL)
2740 reset_exc_info(tstate);
2741 else {
2742 assert(tstate->frame->f_exc_value == NULL);
2743 assert(tstate->frame->f_exc_traceback == NULL);
2744 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00002745
Tim Peters5ca576e2001-06-18 22:08:13 +00002746 /* pop frame */
Thomas Woutersae406c62007-09-19 17:27:43 +00002747exit_eval_frame:
Armin Rigo2b3eb402003-10-28 12:05:48 +00002748 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002749 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002750
Guido van Rossum96a42c81992-01-12 02:29:51 +00002751 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002752}
2753
Guido van Rossumc2e20742006-02-27 22:32:47 +00002754/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002755 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00002756 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002757
Tim Peters6d6c1a32001-08-02 04:15:00 +00002758PyObject *
2759PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002760 PyObject **args, int argcount, PyObject **kws, int kwcount,
2761 PyObject **defs, int defcount, PyObject *closure)
2762{
2763 register PyFrameObject *f;
2764 register PyObject *retval = NULL;
2765 register PyObject **fastlocals, **freevars;
2766 PyThreadState *tstate = PyThreadState_GET();
2767 PyObject *x, *u;
2768
2769 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00002770 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002771 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002772 return NULL;
2773 }
2774
Neal Norwitzdf6a6492006-08-13 18:10:10 +00002775 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00002776 assert(globals != NULL);
2777 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002778 if (f == NULL)
2779 return NULL;
2780
2781 fastlocals = f->f_localsplus;
Richard Jonescebbefc2006-05-23 18:28:17 +00002782 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00002783
2784 if (co->co_argcount > 0 ||
2785 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2786 int i;
2787 int n = argcount;
2788 PyObject *kwdict = NULL;
2789 if (co->co_flags & CO_VARKEYWORDS) {
2790 kwdict = PyDict_New();
2791 if (kwdict == NULL)
2792 goto fail;
2793 i = co->co_argcount;
2794 if (co->co_flags & CO_VARARGS)
2795 i++;
2796 SETLOCAL(i, kwdict);
2797 }
2798 if (argcount > co->co_argcount) {
2799 if (!(co->co_flags & CO_VARARGS)) {
2800 PyErr_Format(PyExc_TypeError,
2801 "%.200s() takes %s %d "
2802 "%sargument%s (%d given)",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002803 PyString_AsString(co->co_name),
Tim Peters5ca576e2001-06-18 22:08:13 +00002804 defcount ? "at most" : "exactly",
2805 co->co_argcount,
2806 kwcount ? "non-keyword " : "",
2807 co->co_argcount == 1 ? "" : "s",
2808 argcount);
2809 goto fail;
2810 }
2811 n = co->co_argcount;
2812 }
2813 for (i = 0; i < n; i++) {
2814 x = args[i];
2815 Py_INCREF(x);
2816 SETLOCAL(i, x);
2817 }
2818 if (co->co_flags & CO_VARARGS) {
2819 u = PyTuple_New(argcount - n);
2820 if (u == NULL)
2821 goto fail;
2822 SETLOCAL(co->co_argcount, u);
2823 for (i = n; i < argcount; i++) {
2824 x = args[i];
2825 Py_INCREF(x);
2826 PyTuple_SET_ITEM(u, i-n, x);
2827 }
2828 }
2829 for (i = 0; i < kwcount; i++) {
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00002830 PyObject **co_varnames;
Tim Peters5ca576e2001-06-18 22:08:13 +00002831 PyObject *keyword = kws[2*i];
2832 PyObject *value = kws[2*i + 1];
2833 int j;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002834 if (keyword == NULL || !PyString_Check(keyword)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002835 PyErr_Format(PyExc_TypeError,
2836 "%.200s() keywords must be strings",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002837 PyString_AsString(co->co_name));
Tim Peters5ca576e2001-06-18 22:08:13 +00002838 goto fail;
2839 }
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00002840 /* Speed hack: do raw pointer compares. As names are
2841 normally interned this should almost always hit. */
2842 co_varnames = PySequence_Fast_ITEMS(co->co_varnames);
Tim Peters5ca576e2001-06-18 22:08:13 +00002843 for (j = 0; j < co->co_argcount; j++) {
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00002844 PyObject *nm = co_varnames[j];
2845 if (nm == keyword)
2846 goto kw_found;
2847 }
2848 /* Slow fallback, just in case */
2849 for (j = 0; j < co->co_argcount; j++) {
2850 PyObject *nm = co_varnames[j];
Tim Peters5ca576e2001-06-18 22:08:13 +00002851 int cmp = PyObject_RichCompareBool(
2852 keyword, nm, Py_EQ);
2853 if (cmp > 0)
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00002854 goto kw_found;
Tim Peters5ca576e2001-06-18 22:08:13 +00002855 else if (cmp < 0)
2856 goto fail;
2857 }
2858 /* Check errors from Compare */
2859 if (PyErr_Occurred())
2860 goto fail;
2861 if (j >= co->co_argcount) {
2862 if (kwdict == NULL) {
2863 PyErr_Format(PyExc_TypeError,
2864 "%.200s() got an unexpected "
2865 "keyword argument '%.400s'",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002866 PyString_AsString(co->co_name),
2867 PyString_AsString(keyword));
Tim Peters5ca576e2001-06-18 22:08:13 +00002868 goto fail;
2869 }
2870 PyDict_SetItem(kwdict, keyword, value);
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00002871 continue;
Tim Peters5ca576e2001-06-18 22:08:13 +00002872 }
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00002873kw_found:
2874 if (GETLOCAL(j) != NULL) {
2875 PyErr_Format(PyExc_TypeError,
2876 "%.200s() got multiple "
2877 "values for keyword "
2878 "argument '%.400s'",
2879 PyString_AsString(co->co_name),
2880 PyString_AsString(keyword));
2881 goto fail;
Tim Peters5ca576e2001-06-18 22:08:13 +00002882 }
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00002883 Py_INCREF(value);
2884 SETLOCAL(j, value);
Tim Peters5ca576e2001-06-18 22:08:13 +00002885 }
2886 if (argcount < co->co_argcount) {
2887 int m = co->co_argcount - defcount;
2888 for (i = argcount; i < m; i++) {
2889 if (GETLOCAL(i) == NULL) {
2890 PyErr_Format(PyExc_TypeError,
2891 "%.200s() takes %s %d "
2892 "%sargument%s (%d given)",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002893 PyString_AsString(co->co_name),
Tim Peters5ca576e2001-06-18 22:08:13 +00002894 ((co->co_flags & CO_VARARGS) ||
2895 defcount) ? "at least"
2896 : "exactly",
2897 m, kwcount ? "non-keyword " : "",
2898 m == 1 ? "" : "s", i);
2899 goto fail;
2900 }
2901 }
2902 if (n > m)
2903 i = n - m;
2904 else
2905 i = 0;
2906 for (; i < defcount; i++) {
2907 if (GETLOCAL(m+i) == NULL) {
2908 PyObject *def = defs[i];
2909 Py_INCREF(def);
2910 SETLOCAL(m+i, def);
2911 }
2912 }
2913 }
2914 }
2915 else {
2916 if (argcount > 0 || kwcount > 0) {
2917 PyErr_Format(PyExc_TypeError,
2918 "%.200s() takes no arguments (%d given)",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002919 PyString_AsString(co->co_name),
Tim Peters5ca576e2001-06-18 22:08:13 +00002920 argcount + kwcount);
2921 goto fail;
2922 }
2923 }
2924 /* Allocate and initialize storage for cell vars, and copy free
2925 vars into frame. This isn't too efficient right now. */
Richard Jonescebbefc2006-05-23 18:28:17 +00002926 if (PyTuple_GET_SIZE(co->co_cellvars)) {
Neal Norwitz245ce8d2006-06-12 02:16:10 +00002927 int i, j, nargs, found;
Tim Peters5ca576e2001-06-18 22:08:13 +00002928 char *cellname, *argname;
2929 PyObject *c;
2930
2931 nargs = co->co_argcount;
2932 if (co->co_flags & CO_VARARGS)
2933 nargs++;
2934 if (co->co_flags & CO_VARKEYWORDS)
2935 nargs++;
2936
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002937 /* Initialize each cell var, taking into account
2938 cell vars that are initialized from arguments.
2939
2940 Should arrange for the compiler to put cellvars
2941 that are arguments at the beginning of the cellvars
2942 list so that we can march over it more efficiently?
2943 */
Richard Jonescebbefc2006-05-23 18:28:17 +00002944 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002945 cellname = PyString_AS_STRING(
Tim Peters5ca576e2001-06-18 22:08:13 +00002946 PyTuple_GET_ITEM(co->co_cellvars, i));
2947 found = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002948 for (j = 0; j < nargs; j++) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002949 argname = PyString_AS_STRING(
Tim Peters5ca576e2001-06-18 22:08:13 +00002950 PyTuple_GET_ITEM(co->co_varnames, j));
2951 if (strcmp(cellname, argname) == 0) {
2952 c = PyCell_New(GETLOCAL(j));
2953 if (c == NULL)
2954 goto fail;
Richard Jonescebbefc2006-05-23 18:28:17 +00002955 GETLOCAL(co->co_nlocals + i) = c;
Tim Peters5ca576e2001-06-18 22:08:13 +00002956 found = 1;
2957 break;
2958 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002959 }
2960 if (found == 0) {
2961 c = PyCell_New(NULL);
2962 if (c == NULL)
2963 goto fail;
Richard Jonescebbefc2006-05-23 18:28:17 +00002964 SETLOCAL(co->co_nlocals + i, c);
Tim Peters5ca576e2001-06-18 22:08:13 +00002965 }
2966 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002967 }
Richard Jonescebbefc2006-05-23 18:28:17 +00002968 if (PyTuple_GET_SIZE(co->co_freevars)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002969 int i;
Richard Jonescebbefc2006-05-23 18:28:17 +00002970 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002971 PyObject *o = PyTuple_GET_ITEM(closure, i);
2972 Py_INCREF(o);
Richard Jonescebbefc2006-05-23 18:28:17 +00002973 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Tim Peters5ca576e2001-06-18 22:08:13 +00002974 }
2975 }
2976
Tim Peters5ca576e2001-06-18 22:08:13 +00002977 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002978 /* Don't need to keep the reference to f_back, it will be set
2979 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002980 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002981 f->f_back = NULL;
2982
Jeremy Hylton985eba52003-02-05 23:13:00 +00002983 PCALL(PCALL_GENERATOR);
2984
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002985 /* Create a new generator that owns the ready to run frame
2986 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00002987 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00002988 }
2989
Thomas Woutersae406c62007-09-19 17:27:43 +00002990 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00002991
Thomas Woutersae406c62007-09-19 17:27:43 +00002992fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00002993
Tim Petersb13680b2001-11-27 23:29:29 +00002994 /* decref'ing the frame can cause __del__ methods to get invoked,
2995 which can call back into Python. While we're done with the
2996 current Python frame (f), the associated C stack is still in use,
2997 so recursion_depth must be boosted for the duration.
2998 */
2999 assert(tstate != NULL);
3000 ++tstate->recursion_depth;
Thomas Woutersae406c62007-09-19 17:27:43 +00003001 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00003002 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00003003 return retval;
3004}
3005
3006
Guido van Rossumc9fbb722003-03-01 03:36:33 +00003007/* Implementation notes for set_exc_info() and reset_exc_info():
3008
3009- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
3010 'exc_traceback'. These always travel together.
3011
3012- tstate->curexc_ZZZ is the "hot" exception that is set by
3013 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
3014
3015- Once an exception is caught by an except clause, it is transferred
3016 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
3017 can pick it up. This is the primary task of set_exc_info().
Tim Peters7df5e7f2006-05-26 23:14:37 +00003018 XXX That can't be right: set_exc_info() doesn't look at tstate->curexc_ZZZ.
Guido van Rossumc9fbb722003-03-01 03:36:33 +00003019
3020- Now let me explain the complicated dance with frame->f_exc_ZZZ.
3021
3022 Long ago, when none of this existed, there were just a few globals:
3023 one set corresponding to the "hot" exception, and one set
3024 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
3025 globals; they were simply stored as sys.exc_ZZZ. For backwards
3026 compatibility, they still are!) The problem was that in code like
3027 this:
3028
3029 try:
3030 "something that may fail"
3031 except "some exception":
3032 "do something else first"
3033 "print the exception from sys.exc_ZZZ."
3034
3035 if "do something else first" invoked something that raised and caught
3036 an exception, sys.exc_ZZZ were overwritten. That was a frequent
3037 cause of subtle bugs. I fixed this by changing the semantics as
3038 follows:
3039
3040 - Within one frame, sys.exc_ZZZ will hold the last exception caught
3041 *in that frame*.
3042
3043 - But initially, and as long as no exception is caught in a given
3044 frame, sys.exc_ZZZ will hold the last exception caught in the
3045 previous frame (or the frame before that, etc.).
3046
3047 The first bullet fixed the bug in the above example. The second
3048 bullet was for backwards compatibility: it was (and is) common to
3049 have a function that is called when an exception is caught, and to
3050 have that function access the caught exception via sys.exc_ZZZ.
3051 (Example: traceback.print_exc()).
3052
3053 At the same time I fixed the problem that sys.exc_ZZZ weren't
3054 thread-safe, by introducing sys.exc_info() which gets it from tstate;
3055 but that's really a separate improvement.
3056
3057 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
3058 variables to what they were before the current frame was called. The
3059 set_exc_info() function saves them on the frame so that
3060 reset_exc_info() can restore them. The invariant is that
3061 frame->f_exc_ZZZ is NULL iff the current frame never caught an
3062 exception (where "catching" an exception applies only to successful
3063 except clauses); and if the current frame ever caught an exception,
3064 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
3065 at the start of the current frame.
3066
3067*/
3068
Fredrik Lundh7a830892006-05-27 10:39:48 +00003069static void
Guido van Rossumac7be682001-01-17 15:42:30 +00003070set_exc_info(PyThreadState *tstate,
3071 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00003072{
Tim Peters7df5e7f2006-05-26 23:14:37 +00003073 PyFrameObject *frame = tstate->frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003074 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00003075
Tim Peters7df5e7f2006-05-26 23:14:37 +00003076 assert(type != NULL);
3077 assert(frame != NULL);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003078 if (frame->f_exc_type == NULL) {
Tim Peters7df5e7f2006-05-26 23:14:37 +00003079 assert(frame->f_exc_value == NULL);
3080 assert(frame->f_exc_traceback == NULL);
3081 /* This frame didn't catch an exception before. */
3082 /* Save previous exception of this thread in this frame. */
Guido van Rossuma027efa1997-05-05 20:56:21 +00003083 if (tstate->exc_type == NULL) {
Tim Peters7df5e7f2006-05-26 23:14:37 +00003084 /* XXX Why is this set to Py_None? */
Guido van Rossuma027efa1997-05-05 20:56:21 +00003085 Py_INCREF(Py_None);
3086 tstate->exc_type = Py_None;
3087 }
Tim Peters7df5e7f2006-05-26 23:14:37 +00003088 Py_INCREF(tstate->exc_type);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003089 Py_XINCREF(tstate->exc_value);
3090 Py_XINCREF(tstate->exc_traceback);
3091 frame->f_exc_type = tstate->exc_type;
3092 frame->f_exc_value = tstate->exc_value;
3093 frame->f_exc_traceback = tstate->exc_traceback;
3094 }
Tim Peters7df5e7f2006-05-26 23:14:37 +00003095 /* Set new exception for this thread. */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003096 tmp_type = tstate->exc_type;
3097 tmp_value = tstate->exc_value;
3098 tmp_tb = tstate->exc_traceback;
Tim Peters7df5e7f2006-05-26 23:14:37 +00003099 Py_INCREF(type);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003100 Py_XINCREF(value);
3101 Py_XINCREF(tb);
3102 tstate->exc_type = type;
3103 tstate->exc_value = value;
3104 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003105 Py_XDECREF(tmp_type);
3106 Py_XDECREF(tmp_value);
3107 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003108 /* For b/w compatibility */
3109 PySys_SetObject("exc_type", type);
3110 PySys_SetObject("exc_value", value);
3111 PySys_SetObject("exc_traceback", tb);
3112}
3113
Fredrik Lundh7a830892006-05-27 10:39:48 +00003114static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003115reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00003116{
3117 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003118 PyObject *tmp_type, *tmp_value, *tmp_tb;
Tim Peters7df5e7f2006-05-26 23:14:37 +00003119
3120 /* It's a precondition that the thread state's frame caught an
3121 * exception -- verify in a debug build.
3122 */
3123 assert(tstate != NULL);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003124 frame = tstate->frame;
Tim Peters7df5e7f2006-05-26 23:14:37 +00003125 assert(frame != NULL);
3126 assert(frame->f_exc_type != NULL);
3127
3128 /* Copy the frame's exception info back to the thread state. */
3129 tmp_type = tstate->exc_type;
3130 tmp_value = tstate->exc_value;
3131 tmp_tb = tstate->exc_traceback;
3132 Py_INCREF(frame->f_exc_type);
3133 Py_XINCREF(frame->f_exc_value);
3134 Py_XINCREF(frame->f_exc_traceback);
3135 tstate->exc_type = frame->f_exc_type;
3136 tstate->exc_value = frame->f_exc_value;
3137 tstate->exc_traceback = frame->f_exc_traceback;
3138 Py_XDECREF(tmp_type);
3139 Py_XDECREF(tmp_value);
3140 Py_XDECREF(tmp_tb);
3141
3142 /* For b/w compatibility */
3143 PySys_SetObject("exc_type", frame->f_exc_type);
3144 PySys_SetObject("exc_value", frame->f_exc_value);
3145 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
3146
3147 /* Clear the frame's exception info. */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003148 tmp_type = frame->f_exc_type;
3149 tmp_value = frame->f_exc_value;
3150 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003151 frame->f_exc_type = NULL;
3152 frame->f_exc_value = NULL;
3153 frame->f_exc_traceback = NULL;
Tim Peters7df5e7f2006-05-26 23:14:37 +00003154 Py_DECREF(tmp_type);
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003155 Py_XDECREF(tmp_value);
3156 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003157}
3158
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003159/* Logic for the raise statement (too complicated for inlining).
3160 This *consumes* a reference count to each of its arguments. */
Fredrik Lundh7a830892006-05-27 10:39:48 +00003161static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003162do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003163{
Guido van Rossumd295f121998-04-09 21:39:57 +00003164 if (type == NULL) {
3165 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003166 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumd295f121998-04-09 21:39:57 +00003167 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
3168 value = tstate->exc_value;
3169 tb = tstate->exc_traceback;
3170 Py_XINCREF(type);
3171 Py_XINCREF(value);
3172 Py_XINCREF(tb);
3173 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003174
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003175 /* We support the following forms of raise:
3176 raise <class>, <classinstance>
3177 raise <class>, <argument tuple>
3178 raise <class>, None
3179 raise <class>, <argument>
3180 raise <classinstance>, None
3181 raise <string>, <object>
3182 raise <string>, None
3183
3184 An omitted second argument is the same as None.
3185
3186 In addition, raise <tuple>, <anything> is the same as
3187 raising the tuple's first item (and it better have one!);
3188 this rule is applied recursively.
3189
3190 Finally, an optional third argument can be supplied, which
3191 gives the traceback to be substituted (useful when
3192 re-raising an exception after examining it). */
3193
3194 /* First, check the traceback argument, replacing None with
3195 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00003196 if (tb == Py_None) {
3197 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003198 tb = NULL;
3199 }
3200 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003201 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003202 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003203 goto raise_error;
3204 }
3205
3206 /* Next, replace a missing value with None */
3207 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003208 value = Py_None;
3209 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003210 }
3211
3212 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00003213 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
3214 PyObject *tmp = type;
3215 type = PyTuple_GET_ITEM(type, 0);
3216 Py_INCREF(type);
3217 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003218 }
3219
Brett Cannon129bd522007-01-30 21:34:36 +00003220 if (PyExceptionClass_Check(type))
Barry Warsaw4249f541997-08-22 21:26:19 +00003221 PyErr_NormalizeException(&type, &value, &tb);
3222
Brett Cannonbf364092006-03-01 04:25:17 +00003223 else if (PyExceptionInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003224 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00003225 if (value != Py_None) {
3226 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003227 "instance exception may not have a separate value");
3228 goto raise_error;
3229 }
3230 else {
3231 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00003232 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003233 value = type;
Brett Cannonbf364092006-03-01 04:25:17 +00003234 type = PyExceptionInstance_Class(type);
Guido van Rossumb209a111997-04-29 18:18:01 +00003235 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003236 }
3237 }
3238 else {
3239 /* Not something you can raise. You get an exception
3240 anyway, just not what you specified :-) */
Jeremy Hylton960d9482001-04-27 02:25:33 +00003241 PyErr_Format(PyExc_TypeError,
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00003242 "exceptions must be classes or instances, not %s",
3243 type->ob_type->tp_name);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003244 goto raise_error;
3245 }
Guido van Rossum504153d2008-03-18 04:26:48 +00003246
3247 assert(PyExceptionClass_Check(type));
3248 if (Py_Py3kWarningFlag && PyClass_Check(type)) {
Benjamin Peterson9f4f4812008-04-27 03:01:45 +00003249 if (PyErr_WarnEx(PyExc_DeprecationWarning,
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00003250 "exceptions must derive from BaseException "
3251 "in 3.x", 1) < 0)
Guido van Rossum504153d2008-03-18 04:26:48 +00003252 goto raise_error;
3253 }
3254
Guido van Rossumb209a111997-04-29 18:18:01 +00003255 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003256 if (tb == NULL)
3257 return WHY_EXCEPTION;
3258 else
3259 return WHY_RERAISE;
3260 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00003261 Py_XDECREF(value);
3262 Py_XDECREF(type);
3263 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003264 return WHY_EXCEPTION;
3265}
3266
Tim Petersd6d010b2001-06-21 02:49:55 +00003267/* Iterate v argcnt times and store the results on the stack (via decreasing
3268 sp). Return 1 for success, 0 if error. */
3269
Fredrik Lundh7a830892006-05-27 10:39:48 +00003270static int
Tim Petersd6d010b2001-06-21 02:49:55 +00003271unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003272{
Tim Petersd6d010b2001-06-21 02:49:55 +00003273 int i = 0;
3274 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00003275 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00003276
Tim Petersd6d010b2001-06-21 02:49:55 +00003277 assert(v != NULL);
3278
3279 it = PyObject_GetIter(v);
3280 if (it == NULL)
3281 goto Error;
3282
3283 for (; i < argcnt; i++) {
3284 w = PyIter_Next(it);
3285 if (w == NULL) {
3286 /* Iterator done, via error or exhaustion. */
3287 if (!PyErr_Occurred()) {
3288 PyErr_Format(PyExc_ValueError,
3289 "need more than %d value%s to unpack",
3290 i, i == 1 ? "" : "s");
3291 }
3292 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003293 }
3294 *--sp = w;
3295 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003296
3297 /* We better have exhausted the iterator now. */
3298 w = PyIter_Next(it);
3299 if (w == NULL) {
3300 if (PyErr_Occurred())
3301 goto Error;
3302 Py_DECREF(it);
3303 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003304 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00003305 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00003306 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00003307 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00003308Error:
Barry Warsaw91010551997-08-25 22:30:51 +00003309 for (; i > 0; i--, sp++)
3310 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00003311 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00003312 return 0;
3313}
3314
3315
Guido van Rossum96a42c81992-01-12 02:29:51 +00003316#ifdef LLTRACE
Fredrik Lundh7a830892006-05-27 10:39:48 +00003317static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003318prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003319{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003320 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003321 if (PyObject_Print(v, stdout, 0) != 0)
3322 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003323 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003324 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003325}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003326#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003327
Fredrik Lundh7a830892006-05-27 10:39:48 +00003328static void
Fred Drake5755ce62001-06-27 19:19:46 +00003329call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003330{
Guido van Rossumb209a111997-04-29 18:18:01 +00003331 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003332 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003333 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003334 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003335 value = Py_None;
3336 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003337 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003338 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003339 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003340 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003341 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003342 }
Fred Drake5755ce62001-06-27 19:19:46 +00003343 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003344 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003345 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003346 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003347 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003348 Py_XDECREF(type);
3349 Py_XDECREF(value);
3350 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003351 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003352}
3353
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +00003354static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003355call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003356 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003357{
3358 PyObject *type, *value, *traceback;
3359 int err;
3360 PyErr_Fetch(&type, &value, &traceback);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003361 err = call_trace(func, obj, frame, what, arg);
Fred Drake4ec5d562001-10-04 19:26:43 +00003362 if (err == 0)
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +00003363 {
Fred Drake4ec5d562001-10-04 19:26:43 +00003364 PyErr_Restore(type, value, traceback);
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +00003365 return 0;
3366 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003367 else {
3368 Py_XDECREF(type);
3369 Py_XDECREF(value);
3370 Py_XDECREF(traceback);
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +00003371 return -1;
Fred Drake4ec5d562001-10-04 19:26:43 +00003372 }
3373}
3374
Fredrik Lundh7a830892006-05-27 10:39:48 +00003375static int
Fred Drake5755ce62001-06-27 19:19:46 +00003376call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3377 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003378{
Fred Drake5755ce62001-06-27 19:19:46 +00003379 register PyThreadState *tstate = frame->f_tstate;
3380 int result;
3381 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003382 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003383 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003384 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003385 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003386 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3387 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003388 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003389 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003390}
3391
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003392PyObject *
3393_PyEval_CallTracing(PyObject *func, PyObject *args)
3394{
3395 PyFrameObject *frame = PyEval_GetFrame();
3396 PyThreadState *tstate = frame->f_tstate;
3397 int save_tracing = tstate->tracing;
3398 int save_use_tracing = tstate->use_tracing;
3399 PyObject *result;
3400
3401 tstate->tracing = 0;
3402 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3403 || (tstate->c_profilefunc != NULL));
3404 result = PyObject_Call(func, args, NULL);
3405 tstate->tracing = save_tracing;
3406 tstate->use_tracing = save_use_tracing;
3407 return result;
3408}
3409
Fredrik Lundh7a830892006-05-27 10:39:48 +00003410static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003411maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003412 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3413 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003414{
Michael W. Hudson006c7522002-11-08 13:08:46 +00003415 int result = 0;
3416
Jeremy Hyltona4ebc132006-04-18 14:47:00 +00003417 /* If the last instruction executed isn't in the current
3418 instruction window, reset the window. If the last
3419 instruction happens to fall at the start of a line or if it
3420 represents a jump backwards, call the trace function.
3421 */
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003422 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Thomas Woutersae406c62007-09-19 17:27:43 +00003423 int line;
3424 PyAddrPair bounds;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003425
Thomas Woutersae406c62007-09-19 17:27:43 +00003426 line = PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3427 &bounds);
3428 if (line >= 0) {
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003429 frame->f_lineno = line;
Tim Peters8a5c3c72004-04-05 19:36:21 +00003430 result = call_trace(func, obj, frame,
Michael W. Hudson006c7522002-11-08 13:08:46 +00003431 PyTrace_LINE, Py_None);
Thomas Woutersae406c62007-09-19 17:27:43 +00003432 }
3433 *instr_lb = bounds.ap_lower;
3434 *instr_ub = bounds.ap_upper;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003435 }
Armin Rigobf57a142004-03-22 19:24:58 +00003436 else if (frame->f_lasti <= *instr_prev) {
Jeremy Hyltona4ebc132006-04-18 14:47:00 +00003437 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
Armin Rigobf57a142004-03-22 19:24:58 +00003438 }
3439 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003440 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003441}
3442
Fred Drake5755ce62001-06-27 19:19:46 +00003443void
3444PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003445{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003446 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003447 PyObject *temp = tstate->c_profileobj;
3448 Py_XINCREF(arg);
3449 tstate->c_profilefunc = NULL;
3450 tstate->c_profileobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003451 /* Must make sure that tracing is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003452 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003453 Py_XDECREF(temp);
3454 tstate->c_profilefunc = func;
3455 tstate->c_profileobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003456 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003457 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003458}
3459
3460void
3461PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3462{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003463 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003464 PyObject *temp = tstate->c_traceobj;
Jeffrey Yasskind0a70d42008-12-12 21:25:13 +00003465 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003466 Py_XINCREF(arg);
3467 tstate->c_tracefunc = NULL;
3468 tstate->c_traceobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003469 /* Must make sure that profiling is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003470 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003471 Py_XDECREF(temp);
3472 tstate->c_tracefunc = func;
3473 tstate->c_traceobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003474 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003475 tstate->use_tracing = ((func != NULL)
3476 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003477}
3478
Guido van Rossumb209a111997-04-29 18:18:01 +00003479PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003480PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003481{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003482 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003483 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003484 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003485 else
3486 return current_frame->f_builtins;
3487}
3488
Guido van Rossumb209a111997-04-29 18:18:01 +00003489PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003490PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003491{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003492 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003493 if (current_frame == NULL)
3494 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003495 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003496 return current_frame->f_locals;
3497}
3498
Guido van Rossumb209a111997-04-29 18:18:01 +00003499PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003500PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003501{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003502 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003503 if (current_frame == NULL)
3504 return NULL;
3505 else
3506 return current_frame->f_globals;
3507}
3508
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003509PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003510PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003511{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003512 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003513 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003514}
3515
Guido van Rossum6135a871995-01-09 17:53:26 +00003516int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003517PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003518{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003519 PyFrameObject *current_frame = PyEval_GetFrame();
Neal Norwitzb9845e72006-06-12 02:11:18 +00003520 return current_frame == NULL ? 0 : PyFrame_IsRestricted(current_frame);
Guido van Rossum6135a871995-01-09 17:53:26 +00003521}
3522
Guido van Rossumbe270261997-05-22 22:26:18 +00003523int
Tim Peters5ba58662001-07-16 02:29:45 +00003524PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003525{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003526 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003527 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003528
3529 if (current_frame != NULL) {
3530 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003531 const int compilerflags = codeflags & PyCF_MASK;
3532 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003533 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003534 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003535 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003536#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003537 if (codeflags & CO_GENERATOR_ALLOWED) {
3538 result = 1;
3539 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3540 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003541#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003542 }
3543 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003544}
3545
3546int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003547Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003548{
Guido van Rossumb209a111997-04-29 18:18:01 +00003549 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003550 if (f == NULL)
3551 return 0;
3552 if (!PyFile_SoftSpace(f, 0))
3553 return 0;
3554 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003555}
3556
Guido van Rossum3f5da241990-12-20 15:06:42 +00003557
Guido van Rossum681d79a1995-07-18 14:51:37 +00003558/* External interface to call any callable object.
3559 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003560
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003561#undef PyEval_CallObject
3562/* for backward compatibility: export this interface */
3563
Guido van Rossumb209a111997-04-29 18:18:01 +00003564PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003565PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003566{
Guido van Rossumb209a111997-04-29 18:18:01 +00003567 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003568}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003569#define PyEval_CallObject(func,arg) \
3570 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003571
Guido van Rossumb209a111997-04-29 18:18:01 +00003572PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003573PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003574{
Jeremy Hylton52820442001-01-03 23:52:36 +00003575 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003576
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003577 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003578 arg = PyTuple_New(0);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003579 if (arg == NULL)
3580 return NULL;
3581 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003582 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003583 PyErr_SetString(PyExc_TypeError,
3584 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003585 return NULL;
3586 }
3587 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003588 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003589
Guido van Rossumb209a111997-04-29 18:18:01 +00003590 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003591 PyErr_SetString(PyExc_TypeError,
3592 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003593 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003594 return NULL;
3595 }
3596
Tim Peters6d6c1a32001-08-02 04:15:00 +00003597 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003598 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003599 return result;
3600}
3601
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003602const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003603PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003604{
3605 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003606 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003607 else if (PyFunction_Check(func))
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003608 return PyString_AsString(((PyFunctionObject*)func)->func_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00003609 else if (PyCFunction_Check(func))
3610 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3611 else if (PyClass_Check(func))
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003612 return PyString_AsString(((PyClassObject*)func)->cl_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00003613 else if (PyInstance_Check(func)) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003614 return PyString_AsString(
Jeremy Hylton512a2372001-04-11 13:52:29 +00003615 ((PyInstanceObject*)func)->in_class->cl_name);
3616 } else {
3617 return func->ob_type->tp_name;
3618 }
3619}
3620
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003621const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003622PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003623{
3624 if (PyMethod_Check(func))
3625 return "()";
3626 else if (PyFunction_Check(func))
3627 return "()";
3628 else if (PyCFunction_Check(func))
3629 return "()";
3630 else if (PyClass_Check(func))
3631 return " constructor";
3632 else if (PyInstance_Check(func)) {
3633 return " instance";
3634 } else {
3635 return " object";
3636 }
3637}
3638
Fredrik Lundh7a830892006-05-27 10:39:48 +00003639static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003640err_args(PyObject *func, int flags, int nargs)
3641{
3642 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003643 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003644 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003645 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003646 nargs);
3647 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003648 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003649 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003650 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003651 nargs);
3652}
3653
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003654#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003655if (tstate->use_tracing && tstate->c_profilefunc) { \
3656 if (call_trace(tstate->c_profilefunc, \
3657 tstate->c_profileobj, \
3658 tstate->frame, PyTrace_C_CALL, \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003659 func)) { \
3660 x = NULL; \
3661 } \
3662 else { \
3663 x = call; \
3664 if (tstate->c_profilefunc != NULL) { \
3665 if (x == NULL) { \
3666 call_trace_protected(tstate->c_profilefunc, \
3667 tstate->c_profileobj, \
3668 tstate->frame, PyTrace_C_EXCEPTION, \
3669 func); \
3670 /* XXX should pass (type, value, tb) */ \
3671 } else { \
3672 if (call_trace(tstate->c_profilefunc, \
3673 tstate->c_profileobj, \
3674 tstate->frame, PyTrace_C_RETURN, \
3675 func)) { \
3676 Py_DECREF(x); \
3677 x = NULL; \
3678 } \
3679 } \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003680 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003681 } \
3682} else { \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003683 x = call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003684 }
3685
Fredrik Lundh7a830892006-05-27 10:39:48 +00003686static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003687call_function(PyObject ***pp_stack, int oparg
3688#ifdef WITH_TSC
3689 , uint64* pintr0, uint64* pintr1
3690#endif
3691 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003692{
3693 int na = oparg & 0xff;
3694 int nk = (oparg>>8) & 0xff;
3695 int n = na + 2 * nk;
3696 PyObject **pfunc = (*pp_stack) - n - 1;
3697 PyObject *func = *pfunc;
3698 PyObject *x, *w;
3699
Jeremy Hylton985eba52003-02-05 23:13:00 +00003700 /* Always dispatch PyCFunction first, because these are
3701 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003702 */
3703 if (PyCFunction_Check(func) && nk == 0) {
3704 int flags = PyCFunction_GET_FLAGS(func);
Nicholas Bastind858a772004-06-25 23:31:06 +00003705 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003706
3707 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003708 if (flags & (METH_NOARGS | METH_O)) {
3709 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3710 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003711 if (flags & METH_NOARGS && na == 0) {
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003712 C_TRACE(x, (*meth)(self,NULL));
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003713 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003714 else if (flags & METH_O && na == 1) {
3715 PyObject *arg = EXT_POP(*pp_stack);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003716 C_TRACE(x, (*meth)(self,arg));
Jeremy Hylton192690e2002-08-16 18:36:11 +00003717 Py_DECREF(arg);
3718 }
3719 else {
3720 err_args(func, flags, na);
3721 x = NULL;
3722 }
3723 }
3724 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003725 PyObject *callargs;
3726 callargs = load_args(pp_stack, na);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003727 READ_TIMESTAMP(*pintr0);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003728 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003729 READ_TIMESTAMP(*pintr1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003730 Py_XDECREF(callargs);
3731 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003732 } else {
3733 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3734 /* optimize access to bound methods */
3735 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003736 PCALL(PCALL_METHOD);
3737 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003738 Py_INCREF(self);
3739 func = PyMethod_GET_FUNCTION(func);
3740 Py_INCREF(func);
3741 Py_DECREF(*pfunc);
3742 *pfunc = self;
3743 na++;
3744 n++;
3745 } else
3746 Py_INCREF(func);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003747 READ_TIMESTAMP(*pintr0);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003748 if (PyFunction_Check(func))
3749 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003750 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003751 x = do_call(func, pp_stack, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003752 READ_TIMESTAMP(*pintr1);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003753 Py_DECREF(func);
3754 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003755
Armin Rigod34fa522006-03-28 19:10:40 +00003756 /* Clear the stack of the function object. Also removes
3757 the arguments in case they weren't consumed already
3758 (fast_function() and err_args() leave them on the stack).
Thomas Wouters7f597322006-03-01 05:32:33 +00003759 */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003760 while ((*pp_stack) > pfunc) {
3761 w = EXT_POP(*pp_stack);
3762 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003763 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003764 }
3765 return x;
3766}
3767
Jeremy Hylton192690e2002-08-16 18:36:11 +00003768/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003769 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003770 For the simplest case -- a function that takes only positional
3771 arguments and is called with only positional arguments -- it
3772 inlines the most primitive frame setup code from
3773 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3774 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003775*/
3776
Fredrik Lundh7a830892006-05-27 10:39:48 +00003777static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003778fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003779{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003780 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003781 PyObject *globals = PyFunction_GET_GLOBALS(func);
3782 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3783 PyObject **d = NULL;
3784 int nd = 0;
3785
Jeremy Hylton985eba52003-02-05 23:13:00 +00003786 PCALL(PCALL_FUNCTION);
3787 PCALL(PCALL_FAST_FUNCTION);
Raymond Hettinger40174c32003-05-31 07:04:16 +00003788 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003789 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3790 PyFrameObject *f;
3791 PyObject *retval = NULL;
3792 PyThreadState *tstate = PyThreadState_GET();
3793 PyObject **fastlocals, **stack;
3794 int i;
3795
3796 PCALL(PCALL_FASTER_FUNCTION);
3797 assert(globals != NULL);
3798 /* XXX Perhaps we should create a specialized
3799 PyFrame_New() that doesn't take locals, but does
3800 take builtins without sanity checking them.
3801 */
Neal Norwitzdf6a6492006-08-13 18:10:10 +00003802 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003803 f = PyFrame_New(tstate, co, globals, NULL);
3804 if (f == NULL)
3805 return NULL;
3806
3807 fastlocals = f->f_localsplus;
3808 stack = (*pp_stack) - n;
3809
3810 for (i = 0; i < n; i++) {
3811 Py_INCREF(*stack);
3812 fastlocals[i] = *stack++;
3813 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003814 retval = PyEval_EvalFrameEx(f,0);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003815 ++tstate->recursion_depth;
3816 Py_DECREF(f);
3817 --tstate->recursion_depth;
3818 return retval;
3819 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003820 if (argdefs != NULL) {
3821 d = &PyTuple_GET_ITEM(argdefs, 0);
Christian Heimese93237d2007-12-19 02:37:44 +00003822 nd = Py_SIZE(argdefs);
Jeremy Hylton52820442001-01-03 23:52:36 +00003823 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003824 return PyEval_EvalCodeEx(co, globals,
3825 (PyObject *)NULL, (*pp_stack)-n, na,
3826 (*pp_stack)-2*nk, nk, d, nd,
3827 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003828}
3829
Fredrik Lundh7a830892006-05-27 10:39:48 +00003830static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003831update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3832 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003833{
3834 PyObject *kwdict = NULL;
3835 if (orig_kwdict == NULL)
3836 kwdict = PyDict_New();
3837 else {
3838 kwdict = PyDict_Copy(orig_kwdict);
3839 Py_DECREF(orig_kwdict);
3840 }
3841 if (kwdict == NULL)
3842 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003843 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003844 int err;
3845 PyObject *value = EXT_POP(*pp_stack);
3846 PyObject *key = EXT_POP(*pp_stack);
3847 if (PyDict_GetItem(kwdict, key) != NULL) {
Thomas Woutersae406c62007-09-19 17:27:43 +00003848 PyErr_Format(PyExc_TypeError,
3849 "%.200s%s got multiple values "
3850 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003851 PyEval_GetFuncName(func),
3852 PyEval_GetFuncDesc(func),
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003853 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003854 Py_DECREF(key);
3855 Py_DECREF(value);
3856 Py_DECREF(kwdict);
3857 return NULL;
3858 }
3859 err = PyDict_SetItem(kwdict, key, value);
3860 Py_DECREF(key);
3861 Py_DECREF(value);
3862 if (err) {
3863 Py_DECREF(kwdict);
3864 return NULL;
3865 }
3866 }
3867 return kwdict;
3868}
3869
Fredrik Lundh7a830892006-05-27 10:39:48 +00003870static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00003871update_star_args(int nstack, int nstar, PyObject *stararg,
3872 PyObject ***pp_stack)
3873{
3874 PyObject *callargs, *w;
3875
3876 callargs = PyTuple_New(nstack + nstar);
3877 if (callargs == NULL) {
3878 return NULL;
3879 }
3880 if (nstar) {
3881 int i;
3882 for (i = 0; i < nstar; i++) {
3883 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3884 Py_INCREF(a);
3885 PyTuple_SET_ITEM(callargs, nstack + i, a);
3886 }
3887 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003888 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003889 w = EXT_POP(*pp_stack);
3890 PyTuple_SET_ITEM(callargs, nstack, w);
3891 }
3892 return callargs;
3893}
3894
Fredrik Lundh7a830892006-05-27 10:39:48 +00003895static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00003896load_args(PyObject ***pp_stack, int na)
3897{
3898 PyObject *args = PyTuple_New(na);
3899 PyObject *w;
3900
3901 if (args == NULL)
3902 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003903 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003904 w = EXT_POP(*pp_stack);
3905 PyTuple_SET_ITEM(args, na, w);
3906 }
3907 return args;
3908}
3909
Fredrik Lundh7a830892006-05-27 10:39:48 +00003910static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00003911do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3912{
3913 PyObject *callargs = NULL;
3914 PyObject *kwdict = NULL;
3915 PyObject *result = NULL;
3916
3917 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003918 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003919 if (kwdict == NULL)
3920 goto call_fail;
3921 }
3922 callargs = load_args(pp_stack, na);
3923 if (callargs == NULL)
3924 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003925#ifdef CALL_PROFILE
3926 /* At this point, we have to look at the type of func to
3927 update the call stats properly. Do it here so as to avoid
3928 exposing the call stats machinery outside ceval.c
3929 */
3930 if (PyFunction_Check(func))
3931 PCALL(PCALL_FUNCTION);
3932 else if (PyMethod_Check(func))
3933 PCALL(PCALL_METHOD);
3934 else if (PyType_Check(func))
3935 PCALL(PCALL_TYPE);
Antoine Pitrou6987d542009-05-30 21:43:48 +00003936 else if (PyCFunction_Check(func))
3937 PCALL(PCALL_CFUNCTION);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003938 else
3939 PCALL(PCALL_OTHER);
3940#endif
Antoine Pitrou6987d542009-05-30 21:43:48 +00003941 if (PyCFunction_Check(func)) {
3942 PyThreadState *tstate = PyThreadState_GET();
3943 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
3944 }
3945 else
3946 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003947 call_fail:
3948 Py_XDECREF(callargs);
3949 Py_XDECREF(kwdict);
3950 return result;
3951}
3952
Fredrik Lundh7a830892006-05-27 10:39:48 +00003953static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00003954ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3955{
3956 int nstar = 0;
3957 PyObject *callargs = NULL;
3958 PyObject *stararg = NULL;
3959 PyObject *kwdict = NULL;
3960 PyObject *result = NULL;
3961
3962 if (flags & CALL_FLAG_KW) {
3963 kwdict = EXT_POP(*pp_stack);
Georg Brandl2134e752007-05-21 20:34:16 +00003964 if (!PyDict_Check(kwdict)) {
3965 PyObject *d;
3966 d = PyDict_New();
3967 if (d == NULL)
3968 goto ext_call_fail;
3969 if (PyDict_Update(d, kwdict) != 0) {
3970 Py_DECREF(d);
3971 /* PyDict_Update raises attribute
3972 * error (percolated from an attempt
3973 * to get 'keys' attribute) instead of
3974 * a type error if its second argument
3975 * is not a mapping.
3976 */
3977 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
3978 PyErr_Format(PyExc_TypeError,
3979 "%.200s%.200s argument after ** "
3980 "must be a mapping, not %.200s",
3981 PyEval_GetFuncName(func),
3982 PyEval_GetFuncDesc(func),
3983 kwdict->ob_type->tp_name);
3984 }
3985 goto ext_call_fail;
3986 }
3987 Py_DECREF(kwdict);
3988 kwdict = d;
Jeremy Hylton52820442001-01-03 23:52:36 +00003989 }
3990 }
3991 if (flags & CALL_FLAG_VAR) {
3992 stararg = EXT_POP(*pp_stack);
3993 if (!PyTuple_Check(stararg)) {
3994 PyObject *t = NULL;
3995 t = PySequence_Tuple(stararg);
3996 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003997 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3998 PyErr_Format(PyExc_TypeError,
Georg Brandl2134e752007-05-21 20:34:16 +00003999 "%.200s%.200s argument after * "
4000 "must be a sequence, not %200s",
Tim Peters6d6c1a32001-08-02 04:15:00 +00004001 PyEval_GetFuncName(func),
Georg Brandl2134e752007-05-21 20:34:16 +00004002 PyEval_GetFuncDesc(func),
4003 stararg->ob_type->tp_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00004004 }
Jeremy Hylton52820442001-01-03 23:52:36 +00004005 goto ext_call_fail;
4006 }
4007 Py_DECREF(stararg);
4008 stararg = t;
4009 }
4010 nstar = PyTuple_GET_SIZE(stararg);
4011 }
4012 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00004013 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00004014 if (kwdict == NULL)
4015 goto ext_call_fail;
4016 }
4017 callargs = update_star_args(na, nstar, stararg, pp_stack);
4018 if (callargs == NULL)
4019 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004020#ifdef CALL_PROFILE
4021 /* At this point, we have to look at the type of func to
4022 update the call stats properly. Do it here so as to avoid
4023 exposing the call stats machinery outside ceval.c
4024 */
4025 if (PyFunction_Check(func))
4026 PCALL(PCALL_FUNCTION);
4027 else if (PyMethod_Check(func))
4028 PCALL(PCALL_METHOD);
4029 else if (PyType_Check(func))
4030 PCALL(PCALL_TYPE);
Antoine Pitrou6987d542009-05-30 21:43:48 +00004031 else if (PyCFunction_Check(func))
4032 PCALL(PCALL_CFUNCTION);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004033 else
4034 PCALL(PCALL_OTHER);
4035#endif
Antoine Pitrou6987d542009-05-30 21:43:48 +00004036 if (PyCFunction_Check(func)) {
4037 PyThreadState *tstate = PyThreadState_GET();
4038 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4039 }
4040 else
4041 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersae406c62007-09-19 17:27:43 +00004042ext_call_fail:
Jeremy Hylton52820442001-01-03 23:52:36 +00004043 Py_XDECREF(callargs);
4044 Py_XDECREF(kwdict);
4045 Py_XDECREF(stararg);
4046 return result;
4047}
4048
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004049/* Extract a slice index from a PyInt or PyLong or an object with the
4050 nb_index slot defined, and store in *pi.
4051 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4052 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 +00004053 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004054*/
Tim Petersb5196382001-12-16 19:44:20 +00004055/* Note: If v is NULL, return success without storing into *pi. This
4056 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4057 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004058*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004059int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004060_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004061{
Tim Petersb5196382001-12-16 19:44:20 +00004062 if (v != NULL) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004063 Py_ssize_t x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00004064 if (PyInt_Check(v)) {
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00004065 /* XXX(nnorwitz): I think PyInt_AS_LONG is correct,
4066 however, it looks like it should be AsSsize_t.
4067 There should be a comment here explaining why.
4068 */
4069 x = PyInt_AS_LONG(v);
Tim Peters7df5e7f2006-05-26 23:14:37 +00004070 }
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00004071 else if (PyIndex_Check(v)) {
4072 x = PyNumber_AsSsize_t(v, NULL);
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004073 if (x == -1 && PyErr_Occurred())
4074 return 0;
4075 }
4076 else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00004077 PyErr_SetString(PyExc_TypeError,
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004078 "slice indices must be integers or "
4079 "None or have an __index__ method");
Guido van Rossum20c6add2000-05-08 14:06:50 +00004080 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004081 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00004082 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004083 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00004084 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004085}
4086
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004087#undef ISINDEX
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00004088#define ISINDEX(x) ((x) == NULL || \
4089 PyInt_Check(x) || PyLong_Check(x) || PyIndex_Check(x))
Guido van Rossum50d756e2001-08-18 17:43:36 +00004090
Fredrik Lundh7a830892006-05-27 10:39:48 +00004091static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004092apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004093{
Guido van Rossum50d756e2001-08-18 17:43:36 +00004094 PyTypeObject *tp = u->ob_type;
4095 PySequenceMethods *sq = tp->tp_as_sequence;
4096
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004097 if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004098 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
Guido van Rossum50d756e2001-08-18 17:43:36 +00004099 if (!_PyEval_SliceIndex(v, &ilow))
4100 return NULL;
4101 if (!_PyEval_SliceIndex(w, &ihigh))
4102 return NULL;
4103 return PySequence_GetSlice(u, ilow, ihigh);
4104 }
4105 else {
4106 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00004107 if (slice != NULL) {
4108 PyObject *res = PyObject_GetItem(u, slice);
4109 Py_DECREF(slice);
4110 return res;
4111 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00004112 else
4113 return NULL;
4114 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004115}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004116
Fredrik Lundh7a830892006-05-27 10:39:48 +00004117static int
Guido van Rossumac7be682001-01-17 15:42:30 +00004118assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
4119 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004120{
Guido van Rossum50d756e2001-08-18 17:43:36 +00004121 PyTypeObject *tp = u->ob_type;
4122 PySequenceMethods *sq = tp->tp_as_sequence;
4123
Georg Brandl0fca97a2007-03-05 22:28:08 +00004124 if (sq && sq->sq_ass_slice && ISINDEX(v) && ISINDEX(w)) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004125 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
Guido van Rossum50d756e2001-08-18 17:43:36 +00004126 if (!_PyEval_SliceIndex(v, &ilow))
4127 return -1;
4128 if (!_PyEval_SliceIndex(w, &ihigh))
4129 return -1;
4130 if (x == NULL)
4131 return PySequence_DelSlice(u, ilow, ihigh);
4132 else
4133 return PySequence_SetSlice(u, ilow, ihigh, x);
4134 }
4135 else {
4136 PyObject *slice = PySlice_New(v, w, NULL);
4137 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00004138 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00004139 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00004140 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00004141 else
Guido van Rossum354797c2001-12-03 19:45:06 +00004142 res = PyObject_DelItem(u, slice);
4143 Py_DECREF(slice);
4144 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00004145 }
4146 else
4147 return -1;
4148 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004149}
4150
Guido van Rossum04edb522008-03-18 02:49:46 +00004151#define Py3kExceptionClass_Check(x) \
4152 (PyType_Check((x)) && \
4153 PyType_FastSubclass((PyTypeObject*)(x), Py_TPFLAGS_BASE_EXC_SUBCLASS))
4154
4155#define CANNOT_CATCH_MSG "catching classes that don't inherit from " \
Georg Brandld5b635f2008-03-25 08:29:14 +00004156 "BaseException is not allowed in 3.x"
Guido van Rossum04edb522008-03-18 02:49:46 +00004157
Fredrik Lundh7a830892006-05-27 10:39:48 +00004158static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004159cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004160{
Guido van Rossumac7be682001-01-17 15:42:30 +00004161 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004162 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00004163 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00004164 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004165 break;
4166 case PyCmp_IS_NOT:
4167 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004168 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004169 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004170 res = PySequence_Contains(w, v);
4171 if (res < 0)
4172 return NULL;
4173 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004174 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00004175 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004176 if (res < 0)
4177 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004178 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004179 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004180 case PyCmp_EXC_MATCH:
Brett Cannon129bd522007-01-30 21:34:36 +00004181 if (PyTuple_Check(w)) {
4182 Py_ssize_t i, length;
4183 length = PyTuple_Size(w);
4184 for (i = 0; i < length; i += 1) {
4185 PyObject *exc = PyTuple_GET_ITEM(w, i);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004186 if (PyString_Check(exc)) {
Brett Cannon129bd522007-01-30 21:34:36 +00004187 int ret_val;
4188 ret_val = PyErr_WarnEx(
Thomas Wouterse2176022007-09-20 17:35:10 +00004189 PyExc_DeprecationWarning,
4190 "catching of string "
4191 "exceptions is deprecated", 1);
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00004192 if (ret_val < 0)
Brett Cannon129bd522007-01-30 21:34:36 +00004193 return NULL;
4194 }
Guido van Rossum20bda582008-03-18 03:15:05 +00004195 else if (Py_Py3kWarningFlag &&
4196 !PyTuple_Check(exc) &&
4197 !Py3kExceptionClass_Check(exc))
Guido van Rossum04edb522008-03-18 02:49:46 +00004198 {
4199 int ret_val;
4200 ret_val = PyErr_WarnEx(
4201 PyExc_DeprecationWarning,
4202 CANNOT_CATCH_MSG, 1);
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00004203 if (ret_val < 0)
Guido van Rossum04edb522008-03-18 02:49:46 +00004204 return NULL;
4205 }
Brett Cannon129bd522007-01-30 21:34:36 +00004206 }
4207 }
4208 else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004209 if (PyString_Check(w)) {
Brett Cannon129bd522007-01-30 21:34:36 +00004210 int ret_val;
4211 ret_val = PyErr_WarnEx(
4212 PyExc_DeprecationWarning,
4213 "catching of string "
Thomas Wouterse2176022007-09-20 17:35:10 +00004214 "exceptions is deprecated", 1);
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00004215 if (ret_val < 0)
Brett Cannon129bd522007-01-30 21:34:36 +00004216 return NULL;
4217 }
Guido van Rossum20bda582008-03-18 03:15:05 +00004218 else if (Py_Py3kWarningFlag &&
4219 !PyTuple_Check(w) &&
4220 !Py3kExceptionClass_Check(w))
Guido van Rossum04edb522008-03-18 02:49:46 +00004221 {
4222 int ret_val;
4223 ret_val = PyErr_WarnEx(
4224 PyExc_DeprecationWarning,
4225 CANNOT_CATCH_MSG, 1);
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00004226 if (ret_val < 0)
Guido van Rossum04edb522008-03-18 02:49:46 +00004227 return NULL;
4228 }
Brett Cannon129bd522007-01-30 21:34:36 +00004229 }
Barry Warsaw4249f541997-08-22 21:26:19 +00004230 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004231 break;
4232 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00004233 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004234 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004235 v = res ? Py_True : Py_False;
4236 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004237 return v;
4238}
4239
Fredrik Lundh7a830892006-05-27 10:39:48 +00004240static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00004241import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004242{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004243 PyObject *x;
4244
4245 x = PyObject_GetAttr(v, name);
4246 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00004247 PyErr_Format(PyExc_ImportError,
4248 "cannot import name %.230s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004249 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004250 }
Thomas Wouters52152252000-08-17 22:55:00 +00004251 return x;
4252}
Guido van Rossumac7be682001-01-17 15:42:30 +00004253
Fredrik Lundh7a830892006-05-27 10:39:48 +00004254static int
Thomas Wouters52152252000-08-17 22:55:00 +00004255import_all_from(PyObject *locals, PyObject *v)
4256{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004257 PyObject *all = PyObject_GetAttrString(v, "__all__");
4258 PyObject *dict, *name, *value;
4259 int skip_leading_underscores = 0;
4260 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004261
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004262 if (all == NULL) {
4263 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4264 return -1; /* Unexpected error */
4265 PyErr_Clear();
4266 dict = PyObject_GetAttrString(v, "__dict__");
4267 if (dict == NULL) {
4268 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4269 return -1;
4270 PyErr_SetString(PyExc_ImportError,
4271 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00004272 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004273 }
4274 all = PyMapping_Keys(dict);
4275 Py_DECREF(dict);
4276 if (all == NULL)
4277 return -1;
4278 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004279 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004280
4281 for (pos = 0, err = 0; ; pos++) {
4282 name = PySequence_GetItem(all, pos);
4283 if (name == NULL) {
4284 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4285 err = -1;
4286 else
4287 PyErr_Clear();
4288 break;
4289 }
4290 if (skip_leading_underscores &&
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004291 PyString_Check(name) &&
4292 PyString_AS_STRING(name)[0] == '_')
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004293 {
4294 Py_DECREF(name);
4295 continue;
4296 }
4297 value = PyObject_GetAttr(v, name);
4298 if (value == NULL)
4299 err = -1;
Armin Rigo70370852006-11-29 21:59:22 +00004300 else if (PyDict_CheckExact(locals))
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004301 err = PyDict_SetItem(locals, name, value);
Armin Rigo70370852006-11-29 21:59:22 +00004302 else
4303 err = PyObject_SetItem(locals, name, value);
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004304 Py_DECREF(name);
4305 Py_XDECREF(value);
4306 if (err != 0)
4307 break;
4308 }
4309 Py_DECREF(all);
4310 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004311}
4312
Fredrik Lundh7a830892006-05-27 10:39:48 +00004313static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004314build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004315{
Guido van Rossum7851eea2001-09-12 19:19:18 +00004316 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004317
4318 if (PyDict_Check(methods))
4319 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00004320 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00004321 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004322 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4323 base = PyTuple_GET_ITEM(bases, 0);
4324 metaclass = PyObject_GetAttrString(base, "__class__");
4325 if (metaclass == NULL) {
4326 PyErr_Clear();
4327 metaclass = (PyObject *)base->ob_type;
4328 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00004329 }
4330 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004331 else {
4332 PyObject *g = PyEval_GetGlobals();
4333 if (g != NULL && PyDict_Check(g))
4334 metaclass = PyDict_GetItemString(g, "__metaclass__");
4335 if (metaclass == NULL)
4336 metaclass = (PyObject *) &PyClass_Type;
4337 Py_INCREF(metaclass);
4338 }
Jeremy Hylton7c1e3472007-02-26 16:14:51 +00004339 result = PyObject_CallFunctionObjArgs(metaclass, name, bases, methods,
Thomas Woutersae406c62007-09-19 17:27:43 +00004340 NULL);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004341 Py_DECREF(metaclass);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004342 if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
Tim Peters7df5e7f2006-05-26 23:14:37 +00004343 /* A type error here likely means that the user passed
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004344 in a base that was not a class (such the random module
4345 instead of the random.random type). Help them out with
Raymond Hettingercfc31922004-09-16 16:41:57 +00004346 by augmenting the error message with more information.*/
4347
4348 PyObject *ptype, *pvalue, *ptraceback;
4349
4350 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004351 if (PyString_Check(pvalue)) {
Raymond Hettingercfc31922004-09-16 16:41:57 +00004352 PyObject *newmsg;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004353 newmsg = PyString_FromFormat(
Jeremy Hylton7c1e3472007-02-26 16:14:51 +00004354 "Error when calling the metaclass bases\n"
Thomas Woutersae406c62007-09-19 17:27:43 +00004355 " %s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004356 PyString_AS_STRING(pvalue));
Raymond Hettingercfc31922004-09-16 16:41:57 +00004357 if (newmsg != NULL) {
4358 Py_DECREF(pvalue);
4359 pvalue = newmsg;
4360 }
4361 }
4362 PyErr_Restore(ptype, pvalue, ptraceback);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004363 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004364 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00004365}
4366
Fredrik Lundh7a830892006-05-27 10:39:48 +00004367static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004368exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
4369 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004370{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004371 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00004372 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004373 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004374
Guido van Rossumb209a111997-04-29 18:18:01 +00004375 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
4376 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004377 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00004378 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004379 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00004380 locals = PyTuple_GetItem(prog, 2);
4381 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004382 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004383 if (globals == Py_None) {
4384 globals = PyEval_GetGlobals();
4385 if (locals == Py_None) {
4386 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00004387 plain = 1;
4388 }
Neal Norwitzdf6a6492006-08-13 18:10:10 +00004389 if (!globals || !locals) {
4390 PyErr_SetString(PyExc_SystemError,
4391 "globals and locals cannot be NULL");
4392 return -1;
4393 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004394 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004395 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004396 locals = globals;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004397 if (!PyString_Check(prog) &&
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004398 !PyUnicode_Check(prog) &&
Guido van Rossumb209a111997-04-29 18:18:01 +00004399 !PyCode_Check(prog) &&
4400 !PyFile_Check(prog)) {
4401 PyErr_SetString(PyExc_TypeError,
Guido van Rossumac7be682001-01-17 15:42:30 +00004402 "exec: arg 1 must be a string, file, or code object");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004403 return -1;
4404 }
Fred Drake661ea262000-10-24 19:57:45 +00004405 if (!PyDict_Check(globals)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004406 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00004407 "exec: arg 2 must be a dictionary or None");
4408 return -1;
4409 }
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004410 if (!PyMapping_Check(locals)) {
Fred Drake661ea262000-10-24 19:57:45 +00004411 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004412 "exec: arg 3 must be a mapping or None");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004413 return -1;
4414 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004415 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00004416 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00004417 if (PyCode_Check(prog)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +00004418 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4419 PyErr_SetString(PyExc_TypeError,
4420 "code object passed to exec may not contain free variables");
4421 return -1;
4422 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004423 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004424 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004425 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004426 FILE *fp = PyFile_AsFile(prog);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004427 char *name = PyString_AsString(PyFile_Name(prog));
Jeremy Hylton714b1122007-02-25 16:01:58 +00004428 PyCompilerFlags cf;
Thomas Woutersae406c62007-09-19 17:27:43 +00004429 if (name == NULL)
4430 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004431 cf.cf_flags = 0;
4432 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004433 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004434 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004435 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004436 v = PyRun_File(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004437 locals);
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004438 }
4439 else {
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004440 PyObject *tmp = NULL;
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004441 char *str;
Tim Peters5ba58662001-07-16 02:29:45 +00004442 PyCompilerFlags cf;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004443 cf.cf_flags = 0;
4444#ifdef Py_USING_UNICODE
4445 if (PyUnicode_Check(prog)) {
4446 tmp = PyUnicode_AsUTF8String(prog);
4447 if (tmp == NULL)
4448 return -1;
4449 prog = tmp;
4450 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4451 }
4452#endif
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004453 if (PyString_AsStringAndSize(prog, &str, NULL))
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004454 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004455 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters8a5c3c72004-04-05 19:36:21 +00004456 v = PyRun_StringFlags(str, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004457 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004458 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004459 v = PyRun_String(str, Py_file_input, globals, locals);
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004460 Py_XDECREF(tmp);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004461 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004462 if (plain)
4463 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004464 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004465 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00004466 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004467 return 0;
4468}
Guido van Rossum24c13741995-02-14 09:42:43 +00004469
Fredrik Lundh7a830892006-05-27 10:39:48 +00004470static void
Paul Prescode68140d2000-08-30 20:25:01 +00004471format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4472{
4473 char *obj_str;
4474
4475 if (!obj)
4476 return;
4477
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004478 obj_str = PyString_AsString(obj);
Paul Prescode68140d2000-08-30 20:25:01 +00004479 if (!obj_str)
4480 return;
4481
4482 PyErr_Format(exc, format_str, obj_str);
4483}
Guido van Rossum950361c1997-01-24 13:49:28 +00004484
Fredrik Lundh7a830892006-05-27 10:39:48 +00004485static PyObject *
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004486string_concatenate(PyObject *v, PyObject *w,
4487 PyFrameObject *f, unsigned char *next_instr)
4488{
4489 /* This function implements 'variable += expr' when both arguments
4490 are strings. */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004491 Py_ssize_t v_len = PyString_GET_SIZE(v);
4492 Py_ssize_t w_len = PyString_GET_SIZE(w);
Armin Rigo97ff0472006-08-09 15:37:26 +00004493 Py_ssize_t new_len = v_len + w_len;
4494 if (new_len < 0) {
4495 PyErr_SetString(PyExc_OverflowError,
4496 "strings are too large to concat");
4497 return NULL;
4498 }
Tim Peters7df5e7f2006-05-26 23:14:37 +00004499
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004500 if (v->ob_refcnt == 2) {
4501 /* In the common case, there are 2 references to the value
4502 * stored in 'variable' when the += is performed: one on the
Thomas Wouterse2176022007-09-20 17:35:10 +00004503 * value stack (in 'v') and one still stored in the
4504 * 'variable'. We try to delete the variable now to reduce
4505 * the refcnt to 1.
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004506 */
4507 switch (*next_instr) {
4508 case STORE_FAST:
4509 {
4510 int oparg = PEEKARG();
4511 PyObject **fastlocals = f->f_localsplus;
4512 if (GETLOCAL(oparg) == v)
4513 SETLOCAL(oparg, NULL);
4514 break;
4515 }
4516 case STORE_DEREF:
4517 {
Thomas Wouterse2176022007-09-20 17:35:10 +00004518 PyObject **freevars = (f->f_localsplus +
4519 f->f_code->co_nlocals);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004520 PyObject *c = freevars[PEEKARG()];
4521 if (PyCell_GET(c) == v)
4522 PyCell_Set(c, NULL);
4523 break;
4524 }
4525 case STORE_NAME:
4526 {
4527 PyObject *names = f->f_code->co_names;
4528 PyObject *name = GETITEM(names, PEEKARG());
4529 PyObject *locals = f->f_locals;
4530 if (PyDict_CheckExact(locals) &&
4531 PyDict_GetItem(locals, name) == v) {
4532 if (PyDict_DelItem(locals, name) != 0) {
4533 PyErr_Clear();
4534 }
4535 }
4536 break;
4537 }
4538 }
4539 }
4540
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004541 if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004542 /* Now we own the last reference to 'v', so we can resize it
4543 * in-place.
4544 */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004545 if (_PyString_Resize(&v, new_len) != 0) {
4546 /* XXX if _PyString_Resize() fails, 'v' has been
Thomas Wouterse2176022007-09-20 17:35:10 +00004547 * deallocated so it cannot be put back into
4548 * 'variable'. The MemoryError is raised when there
4549 * is no value in 'variable', which might (very
4550 * remotely) be a cause of incompatibilities.
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004551 */
4552 return NULL;
4553 }
4554 /* copy 'w' into the newly allocated area of 'v' */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004555 memcpy(PyString_AS_STRING(v) + v_len,
4556 PyString_AS_STRING(w), w_len);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004557 return v;
4558 }
4559 else {
4560 /* When in-place resizing is not an option. */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004561 PyString_Concat(&v, w);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004562 return v;
4563 }
4564}
4565
Guido van Rossum950361c1997-01-24 13:49:28 +00004566#ifdef DYNAMIC_EXECUTION_PROFILE
4567
Fredrik Lundh7a830892006-05-27 10:39:48 +00004568static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004569getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004570{
4571 int i;
4572 PyObject *l = PyList_New(256);
4573 if (l == NULL) return NULL;
4574 for (i = 0; i < 256; i++) {
4575 PyObject *x = PyInt_FromLong(a[i]);
4576 if (x == NULL) {
4577 Py_DECREF(l);
4578 return NULL;
4579 }
4580 PyList_SetItem(l, i, x);
4581 }
4582 for (i = 0; i < 256; i++)
4583 a[i] = 0;
4584 return l;
4585}
4586
4587PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004588_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004589{
4590#ifndef DXPAIRS
4591 return getarray(dxp);
4592#else
4593 int i;
4594 PyObject *l = PyList_New(257);
4595 if (l == NULL) return NULL;
4596 for (i = 0; i < 257; i++) {
4597 PyObject *x = getarray(dxpairs[i]);
4598 if (x == NULL) {
4599 Py_DECREF(l);
4600 return NULL;
4601 }
4602 PyList_SetItem(l, i, x);
4603 }
4604 return l;
4605#endif
4606}
4607
4608#endif