blob: 419ecfaaaaac53862a0aec91e4e54376abf94d2d [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
Michael W. Hudson75eabd22005-01-18 15:56:11 +000054#else /* this is for linux/x86 (and probably any other GCC/x86 combo) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000055
Michael W. Hudson75eabd22005-01-18 15:56:11 +000056#define READ_TIMESTAMP(val) \
57 __asm__ __volatile__("rdtsc" : "=A" (val))
Michael W. Hudson800ba232004-08-12 18:19:17 +000058
59#endif
60
Tim Peters7df5e7f2006-05-26 23:14:37 +000061void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000062 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
63{
64 uint64 intr, inst, loop;
65 PyThreadState *tstate = PyThreadState_Get();
66 if (!tstate->interp->tscdump)
67 return;
68 intr = intr1 - intr0;
69 inst = inst1 - inst0 - intr;
70 loop = loop1 - loop0 - intr;
71 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
72 opcode, ticked, inst, loop);
73}
Michael W. Hudson800ba232004-08-12 18:19:17 +000074
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000075#endif
76
Guido van Rossum04691fc1992-08-12 15:35:34 +000077/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000078/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000079
Guido van Rossum408027e1996-12-30 16:17:54 +000080#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000081/* For debugging the interpreter: */
82#define LLTRACE 1 /* Low-level trace feature */
83#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000084#endif
85
Jeremy Hylton52820442001-01-03 23:52:36 +000086typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000087
Guido van Rossum374a9221991-04-04 10:40:29 +000088/* Forward declarations */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000089#ifdef WITH_TSC
Fredrik Lundh7a830892006-05-27 10:39:48 +000090static PyObject * call_function(PyObject ***, int, uint64*, uint64*);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000091#else
Fredrik Lundh7a830892006-05-27 10:39:48 +000092static PyObject * call_function(PyObject ***, int);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000093#endif
Fredrik Lundh7a830892006-05-27 10:39:48 +000094static PyObject * fast_function(PyObject *, PyObject ***, int, int, int);
95static PyObject * do_call(PyObject *, PyObject ***, int, int);
96static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int);
Thomas Wouterse2176022007-09-20 17:35:10 +000097static PyObject * update_keyword_args(PyObject *, int, PyObject ***,
98 PyObject *);
Fredrik Lundh7a830892006-05-27 10:39:48 +000099static PyObject * update_star_args(int, int, PyObject *, PyObject ***);
100static PyObject * load_args(PyObject ***, int);
Jeremy Hylton52820442001-01-03 23:52:36 +0000101#define CALL_FLAG_VAR 1
102#define CALL_FLAG_KW 2
103
Guido van Rossum0a066c01992-03-27 17:29:15 +0000104#ifdef LLTRACE
Fredrik Lundh1b949402006-05-26 12:01:49 +0000105static int lltrace;
Fredrik Lundh7a830892006-05-27 10:39:48 +0000106static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +0000107#endif
Fredrik Lundh7a830892006-05-27 10:39:48 +0000108static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
Fred Drake5755ce62001-06-27 19:19:46 +0000109 int, PyObject *);
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +0000110static int call_trace_protected(Py_tracefunc, PyObject *,
Armin Rigo1c2d7e52005-09-20 18:34:01 +0000111 PyFrameObject *, int, PyObject *);
Fredrik Lundh7a830892006-05-27 10:39:48 +0000112static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
113static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Armin Rigobf57a142004-03-22 19:24:58 +0000114 PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000115
Fredrik Lundh7a830892006-05-27 10:39:48 +0000116static PyObject * apply_slice(PyObject *, PyObject *, PyObject *);
117static int assign_slice(PyObject *, PyObject *,
Tim Petersdbd9ba62000-07-09 03:09:57 +0000118 PyObject *, PyObject *);
Fredrik Lundh7a830892006-05-27 10:39:48 +0000119static PyObject * cmp_outcome(int, PyObject *, PyObject *);
120static PyObject * import_from(PyObject *, PyObject *);
121static int import_all_from(PyObject *, PyObject *);
122static PyObject * build_class(PyObject *, PyObject *, PyObject *);
123static int exec_statement(PyFrameObject *,
Tim Petersdbd9ba62000-07-09 03:09:57 +0000124 PyObject *, PyObject *, PyObject *);
Fredrik Lundh7a830892006-05-27 10:39:48 +0000125static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *);
126static void reset_exc_info(PyThreadState *);
127static void format_exc_check_arg(PyObject *, char *, PyObject *);
128static PyObject * string_concatenate(PyObject *, PyObject *,
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000129 PyFrameObject *, unsigned char *);
Benjamin Petersone18ef192009-01-20 14:21:16 +0000130static PyObject * kwd_as_string(PyObject *);
Guido van Rossum374a9221991-04-04 10:40:29 +0000131
Paul Prescode68140d2000-08-30 20:25:01 +0000132#define NAME_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000133 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000134#define GLOBAL_NAME_ERROR_MSG \
135 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000136#define UNBOUNDLOCAL_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000137 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000138#define UNBOUNDFREE_ERROR_MSG \
139 "free variable '%.200s' referenced before assignment" \
140 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000141
Guido van Rossum950361c1997-01-24 13:49:28 +0000142/* Dynamic execution profile */
143#ifdef DYNAMIC_EXECUTION_PROFILE
144#ifdef DXPAIRS
145static long dxpairs[257][256];
146#define dxp dxpairs[256]
147#else
148static long dxp[256];
149#endif
150#endif
151
Jeremy Hylton985eba52003-02-05 23:13:00 +0000152/* Function call profile */
153#ifdef CALL_PROFILE
154#define PCALL_NUM 11
155static int pcall[PCALL_NUM];
156
157#define PCALL_ALL 0
158#define PCALL_FUNCTION 1
159#define PCALL_FAST_FUNCTION 2
160#define PCALL_FASTER_FUNCTION 3
161#define PCALL_METHOD 4
162#define PCALL_BOUND_METHOD 5
163#define PCALL_CFUNCTION 6
164#define PCALL_TYPE 7
165#define PCALL_GENERATOR 8
166#define PCALL_OTHER 9
167#define PCALL_POP 10
168
169/* Notes about the statistics
170
171 PCALL_FAST stats
172
173 FAST_FUNCTION means no argument tuple needs to be created.
174 FASTER_FUNCTION means that the fast-path frame setup code is used.
175
176 If there is a method call where the call can be optimized by changing
177 the argument tuple and calling the function directly, it gets recorded
178 twice.
179
180 As a result, the relationship among the statistics appears to be
181 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
182 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
183 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
184 PCALL_METHOD > PCALL_BOUND_METHOD
185*/
186
187#define PCALL(POS) pcall[POS]++
188
189PyObject *
190PyEval_GetCallStats(PyObject *self)
191{
Andrew M. Kuchling1f3ebe02006-10-27 13:22:46 +0000192 return Py_BuildValue("iiiiiiiiiii",
Jeremy Hylton985eba52003-02-05 23:13:00 +0000193 pcall[0], pcall[1], pcall[2], pcall[3],
194 pcall[4], pcall[5], pcall[6], pcall[7],
Andrew M. Kuchling1f3ebe02006-10-27 13:22:46 +0000195 pcall[8], pcall[9], pcall[10]);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000196}
197#else
198#define PCALL(O)
199
200PyObject *
201PyEval_GetCallStats(PyObject *self)
202{
203 Py_INCREF(Py_None);
204 return Py_None;
205}
206#endif
207
Tim Peters5ca576e2001-06-18 22:08:13 +0000208
Guido van Rossume59214e1994-08-30 08:01:59 +0000209#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000210
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000211#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000212#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000213#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000214#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000215
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000216static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000217static PyThread_type_lock pending_lock = 0; /* for pending calls */
Guido van Rossuma9672091994-09-14 13:31:22 +0000218static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000219
Tim Peters7f468f22004-10-11 02:40:51 +0000220int
221PyEval_ThreadsInitialized(void)
222{
223 return interpreter_lock != 0;
224}
225
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000226void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000227PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000228{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000229 if (interpreter_lock)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000230 return;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000231 interpreter_lock = PyThread_allocate_lock();
232 PyThread_acquire_lock(interpreter_lock, 1);
233 main_thread = PyThread_get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000234}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000235
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000236void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000237PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000238{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000239 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000240}
241
242void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000243PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000244{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000245 PyThread_release_lock(interpreter_lock);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000246}
247
248void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000249PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000250{
251 if (tstate == NULL)
252 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000253 /* Check someone has called PyEval_InitThreads() to create the lock */
254 assert(interpreter_lock);
Guido van Rossum65d5b571998-12-21 19:32:43 +0000255 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000256 if (PyThreadState_Swap(tstate) != NULL)
257 Py_FatalError(
258 "PyEval_AcquireThread: non-NULL old thread state");
259}
260
261void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000262PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000263{
264 if (tstate == NULL)
265 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
266 if (PyThreadState_Swap(NULL) != tstate)
267 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000268 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000269}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000270
271/* This function is called from PyOS_AfterFork to ensure that newly
272 created child processes don't hold locks referring to threads which
273 are not running in the child process. (This could also be done using
274 pthread_atfork mechanism, at least for the pthreads implementation.) */
275
276void
277PyEval_ReInitThreads(void)
278{
Jesse Noller5e62ca42008-07-16 20:03:47 +0000279 PyObject *threading, *result;
280 PyThreadState *tstate;
281
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000282 if (!interpreter_lock)
283 return;
284 /*XXX Can't use PyThread_free_lock here because it does too
285 much error-checking. Doing this cleanly would require
286 adding a new function to each thread_*.h. Instead, just
287 create a new lock and waste a little bit of memory */
288 interpreter_lock = PyThread_allocate_lock();
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000289 pending_lock = PyThread_allocate_lock();
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000290 PyThread_acquire_lock(interpreter_lock, 1);
291 main_thread = PyThread_get_thread_ident();
Jesse Noller5e62ca42008-07-16 20:03:47 +0000292
293 /* Update the threading module with the new state.
294 */
295 tstate = PyThreadState_GET();
296 threading = PyMapping_GetItemString(tstate->interp->modules,
297 "threading");
298 if (threading == NULL) {
299 /* threading not imported */
300 PyErr_Clear();
301 return;
302 }
303 result = PyObject_CallMethod(threading, "_after_fork", NULL);
304 if (result == NULL)
305 PyErr_WriteUnraisable(threading);
306 else
307 Py_DECREF(result);
308 Py_DECREF(threading);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000309}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000310#endif
311
Guido van Rossumff4949e1992-08-05 19:58:53 +0000312/* Functions save_thread and restore_thread are always defined so
313 dynamically loaded modules needn't be compiled separately for use
314 with and without threads: */
315
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000316PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000317PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000318{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000319 PyThreadState *tstate = PyThreadState_Swap(NULL);
320 if (tstate == NULL)
321 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000322#ifdef WITH_THREAD
Guido van Rossumb74eca91997-09-30 22:03:16 +0000323 if (interpreter_lock)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000324 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000325#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000326 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000327}
328
329void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000330PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000331{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000332 if (tstate == NULL)
333 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000334#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000335 if (interpreter_lock) {
Guido van Rossumb74eca91997-09-30 22:03:16 +0000336 int err = errno;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000337 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000338 errno = err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000339 }
340#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000341 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000342}
343
344
Guido van Rossuma9672091994-09-14 13:31:22 +0000345/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
346 signal handlers or Mac I/O completion routines) can schedule calls
347 to a function to be called synchronously.
348 The synchronous function is called with one void* argument.
349 It should return 0 for success or -1 for failure -- failure should
350 be accompanied by an exception.
351
352 If registry succeeds, the registry function returns 0; if it fails
353 (e.g. due to too many pending calls) it returns -1 (without setting
354 an exception condition).
355
356 Note that because registry may occur from within signal handlers,
357 or other asynchronous events, calling malloc() is unsafe!
358
359#ifdef WITH_THREAD
360 Any thread can schedule pending calls, but only the main thread
361 will execute them.
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000362 There is no facility to schedule calls to a particular thread, but
363 that should be easy to change, should that ever be required. In
364 that case, the static variables here should go into the python
365 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000366#endif
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000367*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000368
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000369#ifdef WITH_THREAD
370
371/* The WITH_THREAD implementation is thread-safe. It allows
372 scheduling to be made from any thread, and even from an executing
373 callback.
374 */
375
376#define NPENDINGCALLS 32
377static struct {
378 int (*func)(void *);
379 void *arg;
380} pendingcalls[NPENDINGCALLS];
381static int pendingfirst = 0;
382static int pendinglast = 0;
383static volatile int pendingcalls_to_do = 1; /* trigger initialization of lock */
384static char pendingbusy = 0;
385
386int
387Py_AddPendingCall(int (*func)(void *), void *arg)
388{
389 int i, j, result=0;
390 PyThread_type_lock lock = pending_lock;
391
392 /* try a few times for the lock. Since this mechanism is used
393 * for signal handling (on the main thread), there is a (slim)
394 * chance that a signal is delivered on the same thread while we
395 * hold the lock during the Py_MakePendingCalls() function.
396 * This avoids a deadlock in that case.
397 * Note that signals can be delivered on any thread. In particular,
398 * on Windows, a SIGINT is delivered on a system-created worker
399 * thread.
400 * We also check for lock being NULL, in the unlikely case that
401 * this function is called before any bytecode evaluation takes place.
402 */
403 if (lock != NULL) {
404 for (i = 0; i<100; i++) {
405 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
406 break;
407 }
408 if (i == 100)
409 return -1;
410 }
411
412 i = pendinglast;
413 j = (i + 1) % NPENDINGCALLS;
414 if (j == pendingfirst) {
415 result = -1; /* Queue full */
416 } else {
417 pendingcalls[i].func = func;
418 pendingcalls[i].arg = arg;
419 pendinglast = j;
420 }
421 /* signal main loop */
422 _Py_Ticker = 0;
423 pendingcalls_to_do = 1;
424 if (lock != NULL)
425 PyThread_release_lock(lock);
426 return result;
427}
428
429int
430Py_MakePendingCalls(void)
431{
432 int i;
433 int r = 0;
434
435 if (!pending_lock) {
436 /* initial allocation of the lock */
437 pending_lock = PyThread_allocate_lock();
438 if (pending_lock == NULL)
439 return -1;
440 }
441
442 /* only service pending calls on main thread */
443 if (main_thread && PyThread_get_thread_ident() != main_thread)
444 return 0;
445 /* don't perform recursive pending calls */
446 if (pendingbusy)
447 return 0;
448 pendingbusy = 1;
449 /* perform a bounded number of calls, in case of recursion */
450 for (i=0; i<NPENDINGCALLS; i++) {
451 int j;
452 int (*func)(void *);
Mark Dickinson9e58a372009-02-08 17:33:11 +0000453 void *arg = NULL;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000454
455 /* pop one item off the queue while holding the lock */
456 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
457 j = pendingfirst;
458 if (j == pendinglast) {
459 func = NULL; /* Queue empty */
460 } else {
461 func = pendingcalls[j].func;
462 arg = pendingcalls[j].arg;
463 pendingfirst = (j + 1) % NPENDINGCALLS;
464 }
465 pendingcalls_to_do = pendingfirst != pendinglast;
466 PyThread_release_lock(pending_lock);
467 /* having released the lock, perform the callback */
468 if (func == NULL)
469 break;
470 r = func(arg);
471 if (r)
472 break;
473 }
474 pendingbusy = 0;
475 return r;
476}
477
478#else /* if ! defined WITH_THREAD */
479
480/*
481 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
482 This code is used for signal handling in python that isn't built
483 with WITH_THREAD.
484 Don't use this implementation when Py_AddPendingCalls() can happen
485 on a different thread!
486
Guido van Rossuma9672091994-09-14 13:31:22 +0000487 There are two possible race conditions:
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000488 (1) nested asynchronous calls to Py_AddPendingCall()
489 (2) AddPendingCall() calls made while pending calls are being processed.
490
491 (1) is very unlikely because typically signal delivery
492 is blocked during signal handling. So it should be impossible.
493 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000494 The current code is safe against (2), but not against (1).
495 The safety against (2) is derived from the fact that only one
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000496 thread is present, interrupted by signals, and that the critical
497 section is protected with the "busy" variable. On Windows, which
498 delivers SIGINT on a system thread, this does not hold and therefore
499 Windows really shouldn't use this version.
500 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000501*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000502
Guido van Rossuma9672091994-09-14 13:31:22 +0000503#define NPENDINGCALLS 32
504static struct {
Thomas Wouters334fb892000-07-25 12:56:38 +0000505 int (*func)(void *);
506 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000507} pendingcalls[NPENDINGCALLS];
508static volatile int pendingfirst = 0;
509static volatile int pendinglast = 0;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000510static volatile int pendingcalls_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000511
512int
Thomas Wouters334fb892000-07-25 12:56:38 +0000513Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000514{
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000515 static volatile int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000516 int i, j;
517 /* XXX Begin critical section */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000518 if (busy)
519 return -1;
520 busy = 1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000521 i = pendinglast;
522 j = (i + 1) % NPENDINGCALLS;
Guido van Rossum04e70322002-07-17 16:57:13 +0000523 if (j == pendingfirst) {
524 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000525 return -1; /* Queue full */
Guido van Rossum04e70322002-07-17 16:57:13 +0000526 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000527 pendingcalls[i].func = func;
528 pendingcalls[i].arg = arg;
529 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000530
531 _Py_Ticker = 0;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000532 pendingcalls_to_do = 1; /* Signal main loop */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000533 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000534 /* XXX End critical section */
535 return 0;
536}
537
Guido van Rossum180d7b41994-09-29 09:45:57 +0000538int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000539Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000540{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000541 static int busy = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000542 if (busy)
Guido van Rossum180d7b41994-09-29 09:45:57 +0000543 return 0;
544 busy = 1;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000545 pendingcalls_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000546 for (;;) {
547 int i;
Thomas Wouters334fb892000-07-25 12:56:38 +0000548 int (*func)(void *);
549 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000550 i = pendingfirst;
551 if (i == pendinglast)
552 break; /* Queue empty */
553 func = pendingcalls[i].func;
554 arg = pendingcalls[i].arg;
555 pendingfirst = (i + 1) % NPENDINGCALLS;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000556 if (func(arg) < 0) {
557 busy = 0;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000558 pendingcalls_to_do = 1; /* We're not done yet */
Guido van Rossuma9672091994-09-14 13:31:22 +0000559 return -1;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000560 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000561 }
Guido van Rossum180d7b41994-09-29 09:45:57 +0000562 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000563 return 0;
564}
565
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000566#endif /* WITH_THREAD */
567
Guido van Rossuma9672091994-09-14 13:31:22 +0000568
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000569/* The interpreter's recursion limit */
570
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000571#ifndef Py_DEFAULT_RECURSION_LIMIT
572#define Py_DEFAULT_RECURSION_LIMIT 1000
573#endif
574static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
575int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000576
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000577int
578Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000579{
580 return recursion_limit;
581}
582
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000583void
584Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000585{
586 recursion_limit = new_limit;
Thomas Woutersae406c62007-09-19 17:27:43 +0000587 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000588}
589
Armin Rigo2b3eb402003-10-28 12:05:48 +0000590/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
591 if the recursion_depth reaches _Py_CheckRecursionLimit.
592 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
593 to guarantee that _Py_CheckRecursiveCall() is regularly called.
594 Without USE_STACKCHECK, there is no need for this. */
595int
596_Py_CheckRecursiveCall(char *where)
597{
598 PyThreadState *tstate = PyThreadState_GET();
599
600#ifdef USE_STACKCHECK
601 if (PyOS_CheckStack()) {
602 --tstate->recursion_depth;
603 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
604 return -1;
605 }
606#endif
607 if (tstate->recursion_depth > recursion_limit) {
608 --tstate->recursion_depth;
609 PyErr_Format(PyExc_RuntimeError,
610 "maximum recursion depth exceeded%s",
611 where);
612 return -1;
613 }
Thomas Woutersae406c62007-09-19 17:27:43 +0000614 _Py_CheckRecursionLimit = recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000615 return 0;
616}
617
Guido van Rossum374a9221991-04-04 10:40:29 +0000618/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000619enum why_code {
620 WHY_NOT = 0x0001, /* No error */
621 WHY_EXCEPTION = 0x0002, /* Exception occurred */
622 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
623 WHY_RETURN = 0x0008, /* 'return' statement */
624 WHY_BREAK = 0x0010, /* 'break' statement */
625 WHY_CONTINUE = 0x0020, /* 'continue' statement */
626 WHY_YIELD = 0x0040 /* 'yield' operator */
627};
Guido van Rossum374a9221991-04-04 10:40:29 +0000628
Fredrik Lundh7a830892006-05-27 10:39:48 +0000629static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
630static int unpack_iterable(PyObject *, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000631
Jeffrey Yasskinfd8a1ec2008-12-03 06:46:45 +0000632/* Records whether tracing is on for any thread. Counts the number of
633 threads for which tstate->c_tracefunc is non-NULL, so if the value
634 is 0, we know we don't have to check this thread's c_tracefunc.
635 This speeds up the if statement in PyEval_EvalFrameEx() after
636 fast_next_opcode*/
637static int _Py_TracingPossible = 0;
638
Skip Montanarod581d772002-09-03 20:10:45 +0000639/* for manipulating the thread switch and periodic "stuff" - used to be
640 per thread, now just a pair o' globals */
Skip Montanaro99dba272002-09-03 20:19:06 +0000641int _Py_CheckInterval = 100;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000642volatile int _Py_Ticker = 0; /* so that we hit a "tick" first thing */
Guido van Rossum374a9221991-04-04 10:40:29 +0000643
Guido van Rossumb209a111997-04-29 18:18:01 +0000644PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000645PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000646{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000647 return PyEval_EvalCodeEx(co,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000648 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000649 (PyObject **)NULL, 0,
650 (PyObject **)NULL, 0,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000651 (PyObject **)NULL, 0,
652 NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000653}
654
655
656/* Interpreter main loop */
657
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000658PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000659PyEval_EvalFrame(PyFrameObject *f) {
660 /* This is for backward compatibility with extension modules that
Thomas Wouterse2176022007-09-20 17:35:10 +0000661 used this API; core interpreter code should call
662 PyEval_EvalFrameEx() */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000663 return PyEval_EvalFrameEx(f, 0);
664}
665
666PyObject *
Anthony Baxtera863d332006-04-11 07:43:46 +0000667PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000668{
Guido van Rossum950361c1997-01-24 13:49:28 +0000669#ifdef DXPAIRS
670 int lastopcode = 0;
671#endif
Thomas Wouterse2176022007-09-20 17:35:10 +0000672 register PyObject **stack_pointer; /* Next free slot in value stack */
Guido van Rossum374a9221991-04-04 10:40:29 +0000673 register unsigned char *next_instr;
Armin Rigo8817fcd2004-06-17 10:22:40 +0000674 register int opcode; /* Current opcode */
675 register int oparg; /* Current opcode argument, if any */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000676 register enum why_code why; /* Reason for block stack unwind */
Guido van Rossum374a9221991-04-04 10:40:29 +0000677 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000678 register PyObject *x; /* Result object -- NULL if error */
679 register PyObject *v; /* Temporary objects popped off stack */
680 register PyObject *w;
681 register PyObject *u;
682 register PyObject *t;
Barry Warsaw23c9ec82000-08-21 15:44:01 +0000683 register PyObject *stream = NULL; /* for PRINT opcodes */
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000684 register PyObject **fastlocals, **freevars;
Guido van Rossum014518f1998-11-23 21:09:51 +0000685 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000686 PyThreadState *tstate = PyThreadState_GET();
Tim Peters5ca576e2001-06-18 22:08:13 +0000687 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000688
Tim Peters8a5c3c72004-04-05 19:36:21 +0000689 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000690
691 not (instr_lb <= current_bytecode_offset < instr_ub)
692
Tim Peters8a5c3c72004-04-05 19:36:21 +0000693 is true when the line being executed has changed. The
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000694 initial values are such as to make this false the first
695 time it is tested. */
Armin Rigobf57a142004-03-22 19:24:58 +0000696 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000697
Guido van Rossumd076c731998-10-07 19:42:25 +0000698 unsigned char *first_instr;
Skip Montanaro04d80f82002-08-04 21:03:35 +0000699 PyObject *names;
700 PyObject *consts;
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000701#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000702 /* Make it easier to find out where we are with a debugger */
Tim Peters5ca576e2001-06-18 22:08:13 +0000703 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000704#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000705
Neal Norwitza81d2202002-07-14 00:27:26 +0000706/* Tuple access macros */
707
708#ifndef Py_DEBUG
709#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
710#else
711#define GETITEM(v, i) PyTuple_GetItem((v), (i))
712#endif
713
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000714#ifdef WITH_TSC
715/* Use Pentium timestamp counter to mark certain events:
716 inst0 -- beginning of switch statement for opcode dispatch
717 inst1 -- end of switch statement (may be skipped)
718 loop0 -- the top of the mainloop
Tim Peters7df5e7f2006-05-26 23:14:37 +0000719 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000720 (may be skipped)
721 intr1 -- beginning of long interruption
722 intr2 -- end of long interruption
723
724 Many opcodes call out to helper C functions. In some cases, the
725 time in those functions should be counted towards the time for the
726 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
727 calls another Python function; there's no point in charge all the
728 bytecode executed by the called function to the caller.
729
730 It's hard to make a useful judgement statically. In the presence
731 of operator overloading, it's impossible to tell if a call will
732 execute new Python code or not.
733
734 It's a case-by-case judgement. I'll use intr1 for the following
735 cases:
736
737 EXEC_STMT
738 IMPORT_STAR
739 IMPORT_FROM
740 CALL_FUNCTION (and friends)
741
742 */
743 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
744 int ticked = 0;
745
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000746 READ_TIMESTAMP(inst0);
747 READ_TIMESTAMP(inst1);
748 READ_TIMESTAMP(loop0);
749 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000750
751 /* shut up the compiler */
752 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000753#endif
754
Guido van Rossum374a9221991-04-04 10:40:29 +0000755/* Code access macros */
756
Martin v. Löwis18e16552006-02-15 17:27:45 +0000757#define INSTR_OFFSET() ((int)(next_instr - first_instr))
Guido van Rossum374a9221991-04-04 10:40:29 +0000758#define NEXTOP() (*next_instr++)
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000759#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000760#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
Guido van Rossumd076c731998-10-07 19:42:25 +0000761#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000762#define JUMPBY(x) (next_instr += (x))
763
Raymond Hettingerf606f872003-03-16 03:11:04 +0000764/* OpCode prediction macros
Thomas Wouterse2176022007-09-20 17:35:10 +0000765 Some opcodes tend to come in pairs thus making it possible to
766 predict the second code when the first is run. For example,
767 COMPARE_OP is often followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And,
768 those opcodes are often followed by a POP_TOP.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000769
Raymond Hettingerafae11e2008-07-05 02:11:55 +0000770 Verifying the prediction costs a single high-speed test of a register
Raymond Hettingerac2072922003-03-16 15:41:11 +0000771 variable against a constant. If the pairing was good, then the
Raymond Hettingerafae11e2008-07-05 02:11:55 +0000772 processor's own internal branch predication has a high likelihood of
773 success, resulting in a nearly zero-overhead transition to the
774 next opcode. A successful prediction saves a trip through the eval-loop
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +0000775 including its two unpredictable branches, the HAS_ARG test and the
Raymond Hettingerafae11e2008-07-05 02:11:55 +0000776 switch-case. Combined with the processor's internal branch prediction,
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +0000777 a successful PREDICT has the effect of making the two opcodes run as if
Raymond Hettingerafae11e2008-07-05 02:11:55 +0000778 they were a single new opcode with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000779
Raymond Hettingerafae11e2008-07-05 02:11:55 +0000780 If collecting opcode statistics, your choices are to either keep the
781 predictions turned-on and interpret the results as if some opcodes
782 had been combined or turn-off predictions so that the opcode frequency
783 counter updates for both opcodes.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000784*/
785
Raymond Hettingera7216982004-02-08 19:59:27 +0000786#ifdef DYNAMIC_EXECUTION_PROFILE
787#define PREDICT(op) if (0) goto PRED_##op
788#else
Raymond Hettingerac2072922003-03-16 15:41:11 +0000789#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000790#endif
791
Raymond Hettingerf606f872003-03-16 03:11:04 +0000792#define PREDICTED(op) PRED_##op: next_instr++
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000793#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Raymond Hettingerf606f872003-03-16 03:11:04 +0000794
Guido van Rossum374a9221991-04-04 10:40:29 +0000795/* Stack manipulation macros */
796
Martin v. Löwis18e16552006-02-15 17:27:45 +0000797/* The stack can grow at most MAXINT deep, as co_nlocals and
798 co_stacksize are ints. */
799#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
Guido van Rossum374a9221991-04-04 10:40:29 +0000800#define EMPTY() (STACK_LEVEL() == 0)
801#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000802#define SECOND() (stack_pointer[-2])
803#define THIRD() (stack_pointer[-3])
804#define FOURTH() (stack_pointer[-4])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000805#define SET_TOP(v) (stack_pointer[-1] = (v))
806#define SET_SECOND(v) (stack_pointer[-2] = (v))
807#define SET_THIRD(v) (stack_pointer[-3] = (v))
808#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000809#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000810#define BASIC_PUSH(v) (*stack_pointer++ = (v))
811#define BASIC_POP() (*--stack_pointer)
812
Guido van Rossum96a42c81992-01-12 02:29:51 +0000813#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000814#define PUSH(v) { (void)(BASIC_PUSH(v), \
815 lltrace && prtrace(TOP(), "push")); \
Richard Jonescebbefc2006-05-23 18:28:17 +0000816 assert(STACK_LEVEL() <= co->co_stacksize); }
Thomas Wouterse2176022007-09-20 17:35:10 +0000817#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
818 BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000819#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
820 lltrace && prtrace(TOP(), "stackadj")); \
Richard Jonescebbefc2006-05-23 18:28:17 +0000821 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes52729ac2007-12-14 02:33:57 +0000822#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
823 prtrace((STACK_POINTER)[-1], "ext_pop")), \
824 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000825#else
826#define PUSH(v) BASIC_PUSH(v)
827#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000828#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000829#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000830#endif
831
Guido van Rossum681d79a1995-07-18 14:51:37 +0000832/* Local variable macros */
833
834#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000835
836/* The SETLOCAL() macro must not DECREF the local variable in-place and
837 then store the new value; it must copy the old value to a temporary
838 value, then store the new value, and then DECREF the temporary value.
839 This is because it is possible that during the DECREF the frame is
840 accessed by other code (e.g. a __del__ method or gc.collect()) and the
841 variable would be pointing to already-freed memory. */
842#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
843 GETLOCAL(i) = value; \
844 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000845
Guido van Rossuma027efa1997-05-05 20:56:21 +0000846/* Start of code */
847
Tim Peters5ca576e2001-06-18 22:08:13 +0000848 if (f == NULL)
849 return NULL;
850
Armin Rigo1d313ab2003-10-25 14:33:09 +0000851 /* push frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000852 if (Py_EnterRecursiveCall(""))
Armin Rigo1d313ab2003-10-25 14:33:09 +0000853 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000854
Tim Peters5ca576e2001-06-18 22:08:13 +0000855 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000856
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000857 if (tstate->use_tracing) {
858 if (tstate->c_tracefunc != NULL) {
859 /* tstate->c_tracefunc, if defined, is a
860 function that will be called on *every* entry
861 to a code block. Its return value, if not
862 None, is a function that will be called at
863 the start of each executed line of code.
864 (Actually, the function must return itself
865 in order to continue tracing.) The trace
866 functions are called with three arguments:
867 a pointer to the current frame, a string
868 indicating why the function is called, and
869 an argument which depends on the situation.
870 The global trace function is also called
871 whenever an exception is detected. */
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +0000872 if (call_trace_protected(tstate->c_tracefunc,
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +0000873 tstate->c_traceobj,
874 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000875 /* Trace function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000876 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000877 }
878 }
879 if (tstate->c_profilefunc != NULL) {
880 /* Similar for c_profilefunc, except it needn't
881 return itself and isn't called for "line" events */
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +0000882 if (call_trace_protected(tstate->c_profilefunc,
883 tstate->c_profileobj,
884 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000885 /* Profile function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000886 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000887 }
888 }
889 }
890
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000891 co = f->f_code;
892 names = co->co_names;
893 consts = co->co_consts;
894 fastlocals = f->f_localsplus;
Richard Jonescebbefc2006-05-23 18:28:17 +0000895 freevars = f->f_localsplus + co->co_nlocals;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000896 first_instr = (unsigned char*) PyString_AS_STRING(co->co_code);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000897 /* An explanation is in order for the next line.
898
899 f->f_lasti now refers to the index of the last instruction
900 executed. You might think this was obvious from the name, but
901 this wasn't always true before 2.3! PyFrame_New now sets
902 f->f_lasti to -1 (i.e. the index *before* the first instruction)
903 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +0000904 does work. Promise.
Raymond Hettinger4bd97d42007-01-06 01:14:41 +0000905
906 When the PREDICT() macros are enabled, some opcode pairs follow in
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +0000907 direct succession without updating f->f_lasti. A successful
Raymond Hettinger4bd97d42007-01-06 01:14:41 +0000908 prediction effectively links the two codes together as if they
909 were a single new opcode; accordingly,f->f_lasti will point to
910 the first code in the pair (for instance, GET_ITER followed by
911 FOR_ITER is effectively a single opcode and f->f_lasti will point
912 at to the beginning of the combined pair.)
913 */
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000914 next_instr = first_instr + f->f_lasti + 1;
915 stack_pointer = f->f_stacktop;
916 assert(stack_pointer != NULL);
917 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
918
Tim Peters5ca576e2001-06-18 22:08:13 +0000919#ifdef LLTRACE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000920 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +0000921#endif
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000922#if defined(Py_DEBUG) || defined(LLTRACE)
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000923 filename = PyString_AsString(co->co_filename);
Tim Peters5ca576e2001-06-18 22:08:13 +0000924#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000925
Guido van Rossum374a9221991-04-04 10:40:29 +0000926 why = WHY_NOT;
927 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +0000928 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +0000929 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +0000930
Anthony Baxtera863d332006-04-11 07:43:46 +0000931 if (throwflag) { /* support for generator.throw() */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000932 why = WHY_EXCEPTION;
933 goto on_error;
934 }
Tim Peters7df5e7f2006-05-26 23:14:37 +0000935
Guido van Rossum374a9221991-04-04 10:40:29 +0000936 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000937#ifdef WITH_TSC
938 if (inst1 == 0) {
939 /* Almost surely, the opcode executed a break
940 or a continue, preventing inst1 from being set
941 on the way out of the loop.
942 */
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000943 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000944 loop1 = inst1;
945 }
946 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
947 intr0, intr1);
948 ticked = 0;
949 inst1 = 0;
950 intr0 = 0;
951 intr1 = 0;
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000952 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000953#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000954 assert(stack_pointer >= f->f_valuestack); /* else underflow */
Richard Jonescebbefc2006-05-23 18:28:17 +0000955 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000956
Guido van Rossuma027efa1997-05-05 20:56:21 +0000957 /* Do periodic things. Doing this every time through
958 the loop would add too much overhead, so we do it
959 only every Nth instruction. We also do it if
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000960 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
Guido van Rossuma027efa1997-05-05 20:56:21 +0000961 event needs attention (e.g. a signal handler or
962 async I/O handler); see Py_AddPendingCall() and
963 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000964
Skip Montanarod581d772002-09-03 20:10:45 +0000965 if (--_Py_Ticker < 0) {
Thomas Woutersae406c62007-09-19 17:27:43 +0000966 if (*next_instr == SETUP_FINALLY) {
967 /* Make the last opcode before
968 a try: finally: block uninterruptable. */
969 goto fast_next_opcode;
970 }
Skip Montanarod581d772002-09-03 20:10:45 +0000971 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000972 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000973#ifdef WITH_TSC
974 ticked = 1;
975#endif
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000976 if (pendingcalls_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +0000977 if (Py_MakePendingCalls() < 0) {
978 why = WHY_EXCEPTION;
979 goto on_error;
980 }
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000981 if (pendingcalls_to_do)
Kurt B. Kaiser4c79a832004-11-23 18:06:08 +0000982 /* MakePendingCalls() didn't succeed.
983 Force early re-execution of this
984 "periodic" code, possibly after
985 a thread switch */
986 _Py_Ticker = 0;
Guido van Rossum8861b741996-07-30 16:49:37 +0000987 }
Guido van Rossume59214e1994-08-30 08:01:59 +0000988#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000989 if (interpreter_lock) {
990 /* Give another thread a chance */
991
Guido van Rossum25ce5661997-08-02 03:10:38 +0000992 if (PyThreadState_Swap(NULL) != tstate)
993 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000994 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000995
996 /* Other threads may run now */
997
Guido van Rossum65d5b571998-12-21 19:32:43 +0000998 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000999 if (PyThreadState_Swap(tstate) != NULL)
1000 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +00001001
1002 /* Check for thread interrupts */
1003
1004 if (tstate->async_exc != NULL) {
1005 x = tstate->async_exc;
1006 tstate->async_exc = NULL;
1007 PyErr_SetNone(x);
1008 Py_DECREF(x);
1009 why = WHY_EXCEPTION;
1010 goto on_error;
1011 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001012 }
1013#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001014 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001015
Neil Schemenauer63543862002-02-17 19:10:14 +00001016 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +00001017 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001018
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001019 /* line-by-line tracing support */
1020
Jeffrey Yasskinfd8a1ec2008-12-03 06:46:45 +00001021 if (_Py_TracingPossible &&
1022 tstate->c_tracefunc != NULL && !tstate->tracing) {
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001023 /* see maybe_call_line_trace
1024 for expository comments */
1025 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001026
Michael W. Hudson58ee2af2003-04-29 16:18:47 +00001027 err = maybe_call_line_trace(tstate->c_tracefunc,
1028 tstate->c_traceobj,
Armin Rigobf57a142004-03-22 19:24:58 +00001029 f, &instr_lb, &instr_ub,
1030 &instr_prev);
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001031 /* Reload possibly changed frame fields */
1032 JUMPTO(f->f_lasti);
Michael W. Hudson58ee2af2003-04-29 16:18:47 +00001033 if (f->f_stacktop != NULL) {
1034 stack_pointer = f->f_stacktop;
1035 f->f_stacktop = NULL;
1036 }
1037 if (err) {
1038 /* trace function raised an exception */
1039 goto on_error;
1040 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001041 }
1042
1043 /* Extract opcode and argument */
1044
Guido van Rossum374a9221991-04-04 10:40:29 +00001045 opcode = NEXTOP();
Armin Rigo8817fcd2004-06-17 10:22:40 +00001046 oparg = 0; /* allows oparg to be stored in a register because
1047 it doesn't have to be remembered across a full loop */
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001048 if (HAS_ARG(opcode))
1049 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00001050 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001051#ifdef DYNAMIC_EXECUTION_PROFILE
1052#ifdef DXPAIRS
1053 dxpairs[lastopcode][opcode]++;
1054 lastopcode = opcode;
1055#endif
1056 dxp[opcode]++;
1057#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001058
Guido van Rossum96a42c81992-01-12 02:29:51 +00001059#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +00001060 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001061
Guido van Rossum96a42c81992-01-12 02:29:51 +00001062 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001063 if (HAS_ARG(opcode)) {
1064 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001065 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001066 }
1067 else {
1068 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001069 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +00001070 }
1071 }
1072#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001073
Guido van Rossum374a9221991-04-04 10:40:29 +00001074 /* Main switch on opcode */
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001075 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001076
Guido van Rossum374a9221991-04-04 10:40:29 +00001077 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001078
Guido van Rossum374a9221991-04-04 10:40:29 +00001079 /* BEWARE!
1080 It is essential that any operation that fails sets either
1081 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1082 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001083
Guido van Rossum374a9221991-04-04 10:40:29 +00001084 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001085
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001086 case NOP:
1087 goto fast_next_opcode;
1088
Neil Schemenauer63543862002-02-17 19:10:14 +00001089 case LOAD_FAST:
1090 x = GETLOCAL(oparg);
1091 if (x != NULL) {
1092 Py_INCREF(x);
1093 PUSH(x);
1094 goto fast_next_opcode;
1095 }
1096 format_exc_check_arg(PyExc_UnboundLocalError,
1097 UNBOUNDLOCAL_ERROR_MSG,
1098 PyTuple_GetItem(co->co_varnames, oparg));
1099 break;
1100
1101 case LOAD_CONST:
Skip Montanaro04d80f82002-08-04 21:03:35 +00001102 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +00001103 Py_INCREF(x);
1104 PUSH(x);
1105 goto fast_next_opcode;
1106
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001107 PREDICTED_WITH_ARG(STORE_FAST);
Neil Schemenauer63543862002-02-17 19:10:14 +00001108 case STORE_FAST:
1109 v = POP();
1110 SETLOCAL(oparg, v);
1111 goto fast_next_opcode;
1112
Raymond Hettingerf606f872003-03-16 03:11:04 +00001113 PREDICTED(POP_TOP);
Guido van Rossum374a9221991-04-04 10:40:29 +00001114 case POP_TOP:
1115 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001116 Py_DECREF(v);
Neil Schemenauer63543862002-02-17 19:10:14 +00001117 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001118
Guido van Rossum374a9221991-04-04 10:40:29 +00001119 case ROT_TWO:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001120 v = TOP();
1121 w = SECOND();
1122 SET_TOP(w);
1123 SET_SECOND(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +00001124 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001125
Guido van Rossum374a9221991-04-04 10:40:29 +00001126 case ROT_THREE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001127 v = TOP();
1128 w = SECOND();
1129 x = THIRD();
1130 SET_TOP(w);
1131 SET_SECOND(x);
1132 SET_THIRD(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +00001133 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001134
Thomas Wouters434d0822000-08-24 20:11:32 +00001135 case ROT_FOUR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001136 u = TOP();
1137 v = SECOND();
1138 w = THIRD();
1139 x = FOURTH();
1140 SET_TOP(v);
1141 SET_SECOND(w);
1142 SET_THIRD(x);
1143 SET_FOURTH(u);
Raymond Hettinger080cb322003-03-14 01:37:42 +00001144 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001145
Guido van Rossum374a9221991-04-04 10:40:29 +00001146 case DUP_TOP:
1147 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001148 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001149 PUSH(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +00001150 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001151
Thomas Wouters434d0822000-08-24 20:11:32 +00001152 case DUP_TOPX:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001153 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001154 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +00001155 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001156 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +00001157 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001158 STACKADJ(2);
1159 SET_TOP(x);
1160 SET_SECOND(w);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001161 goto fast_next_opcode;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001162 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001163 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +00001164 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001165 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +00001166 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001167 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +00001168 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001169 STACKADJ(3);
1170 SET_TOP(x);
1171 SET_SECOND(w);
1172 SET_THIRD(v);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001173 goto fast_next_opcode;
Thomas Wouters434d0822000-08-24 20:11:32 +00001174 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001175 Py_FatalError("invalid argument to DUP_TOPX"
1176 " (bytecode corruption?)");
Jeffrey Yasskin2d873bd2008-12-08 18:55:24 +00001177 /* Never returns, so don't bother to set why. */
Tim Peters35ba6892000-10-11 07:04:49 +00001178 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001179
Guido van Rossum374a9221991-04-04 10:40:29 +00001180 case UNARY_POSITIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001181 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001182 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001183 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001184 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001185 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001186 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001187
Guido van Rossum374a9221991-04-04 10:40:29 +00001188 case UNARY_NEGATIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001189 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001190 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001191 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001192 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001193 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001194 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001195
Guido van Rossum374a9221991-04-04 10:40:29 +00001196 case UNARY_NOT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001197 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001198 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001199 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +00001200 if (err == 0) {
1201 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001202 SET_TOP(Py_True);
Guido van Rossumfc490731997-05-06 15:06:49 +00001203 continue;
1204 }
1205 else if (err > 0) {
1206 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001207 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +00001208 err = 0;
1209 continue;
1210 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00001211 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001212 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001213
Guido van Rossum374a9221991-04-04 10:40:29 +00001214 case UNARY_CONVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001215 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001216 x = PyObject_Repr(v);
1217 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001218 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001219 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001220 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001221
Guido van Rossum7928cd71991-10-24 14:59:31 +00001222 case UNARY_INVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001223 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001224 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001225 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001226 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001227 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001228 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001229
Guido van Rossum50564e81996-01-12 01:13:16 +00001230 case BINARY_POWER:
1231 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001232 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001233 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +00001234 Py_DECREF(v);
1235 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001236 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001237 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +00001238 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001239
Guido van Rossum374a9221991-04-04 10:40:29 +00001240 case BINARY_MULTIPLY:
1241 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001242 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001243 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001244 Py_DECREF(v);
1245 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001246 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001247 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001248 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001249
Guido van Rossum374a9221991-04-04 10:40:29 +00001250 case BINARY_DIVIDE:
Tim Peters3caca232001-12-06 06:23:26 +00001251 if (!_Py_QnewFlag) {
1252 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001253 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001254 x = PyNumber_Divide(v, w);
1255 Py_DECREF(v);
1256 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001257 SET_TOP(x);
Tim Peters3caca232001-12-06 06:23:26 +00001258 if (x != NULL) continue;
1259 break;
1260 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001261 /* -Qnew is in effect: fall through to
Tim Peters3caca232001-12-06 06:23:26 +00001262 BINARY_TRUE_DIVIDE */
1263 case BINARY_TRUE_DIVIDE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001264 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001265 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001266 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001267 Py_DECREF(v);
1268 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001269 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001270 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001271 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001272
Guido van Rossum4668b002001-08-08 05:00:18 +00001273 case BINARY_FLOOR_DIVIDE:
1274 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001275 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001276 x = PyNumber_FloorDivide(v, w);
1277 Py_DECREF(v);
1278 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001279 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001280 if (x != NULL) continue;
1281 break;
1282
Guido van Rossum374a9221991-04-04 10:40:29 +00001283 case BINARY_MODULO:
1284 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001285 v = TOP();
Collin Winter8725dce2009-02-20 19:30:41 +00001286 if (PyString_CheckExact(v))
1287 x = PyString_Format(v, w);
1288 else
1289 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001290 Py_DECREF(v);
1291 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001292 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001293 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001294 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001295
Guido van Rossum374a9221991-04-04 10:40:29 +00001296 case BINARY_ADD:
1297 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001298 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001299 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001300 /* INLINE: int + int */
1301 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001302 a = PyInt_AS_LONG(v);
1303 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001304 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001305 if ((i^a) < 0 && (i^b) < 0)
1306 goto slow_add;
1307 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001308 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001309 else if (PyString_CheckExact(v) &&
1310 PyString_CheckExact(w)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001311 x = string_concatenate(v, w, f, next_instr);
1312 /* string_concatenate consumed the ref to v */
1313 goto skip_decref_vx;
1314 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001315 else {
1316 slow_add:
Guido van Rossumc12da691997-07-17 23:12:42 +00001317 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001318 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001319 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001320 skip_decref_vx:
Guido van Rossumb209a111997-04-29 18:18:01 +00001321 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001322 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001323 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001324 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001325
Guido van Rossum374a9221991-04-04 10:40:29 +00001326 case BINARY_SUBTRACT:
1327 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001328 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001329 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001330 /* INLINE: int - int */
1331 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001332 a = PyInt_AS_LONG(v);
1333 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001334 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001335 if ((i^a) < 0 && (i^~b) < 0)
1336 goto slow_sub;
1337 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001338 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001339 else {
1340 slow_sub:
Guido van Rossumc12da691997-07-17 23:12:42 +00001341 x = PyNumber_Subtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001342 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001343 Py_DECREF(v);
1344 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001345 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001346 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001347 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001348
Guido van Rossum374a9221991-04-04 10:40:29 +00001349 case BINARY_SUBSCR:
1350 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001351 v = TOP();
Tim Petersb1c46982001-10-05 20:41:38 +00001352 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001353 /* INLINE: list[int] */
Neal Norwitz814e9382006-03-02 07:54:28 +00001354 Py_ssize_t i = PyInt_AsSsize_t(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001355 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +00001356 i += PyList_GET_SIZE(v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001357 if (i >= 0 && i < PyList_GET_SIZE(v)) {
Guido van Rossumfa00e951998-07-08 15:02:37 +00001358 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001359 Py_INCREF(x);
1360 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001361 else
1362 goto slow_get;
Guido van Rossumc12da691997-07-17 23:12:42 +00001363 }
1364 else
Raymond Hettinger467a6982004-04-07 11:39:21 +00001365 slow_get:
Guido van Rossumc12da691997-07-17 23:12:42 +00001366 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001367 Py_DECREF(v);
1368 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001369 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001370 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001371 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001372
Guido van Rossum7928cd71991-10-24 14:59:31 +00001373 case BINARY_LSHIFT:
1374 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001375 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001376 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001377 Py_DECREF(v);
1378 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001379 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001380 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001381 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001382
Guido van Rossum7928cd71991-10-24 14:59:31 +00001383 case BINARY_RSHIFT:
1384 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001385 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001386 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001387 Py_DECREF(v);
1388 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001389 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001390 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001391 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001392
Guido van Rossum7928cd71991-10-24 14:59:31 +00001393 case BINARY_AND:
1394 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001395 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001396 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001397 Py_DECREF(v);
1398 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001399 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001400 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001401 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001402
Guido van Rossum7928cd71991-10-24 14:59:31 +00001403 case BINARY_XOR:
1404 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001405 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001406 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001407 Py_DECREF(v);
1408 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001409 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001410 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001411 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001412
Guido van Rossum7928cd71991-10-24 14:59:31 +00001413 case BINARY_OR:
1414 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001415 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001416 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001417 Py_DECREF(v);
1418 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001419 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001420 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001421 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001422
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001423 case LIST_APPEND:
1424 w = POP();
Antoine Pitroud0c35152008-12-17 00:38:28 +00001425 v = stack_pointer[-oparg];
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001426 err = PyList_Append(v, w);
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001427 Py_DECREF(w);
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001428 if (err == 0) {
1429 PREDICT(JUMP_ABSOLUTE);
1430 continue;
1431 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001432 break;
1433
Thomas Wouters434d0822000-08-24 20:11:32 +00001434 case INPLACE_POWER:
1435 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001436 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001437 x = PyNumber_InPlacePower(v, w, Py_None);
1438 Py_DECREF(v);
1439 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001440 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001441 if (x != NULL) continue;
1442 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001443
Thomas Wouters434d0822000-08-24 20:11:32 +00001444 case INPLACE_MULTIPLY:
1445 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001446 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001447 x = PyNumber_InPlaceMultiply(v, w);
1448 Py_DECREF(v);
1449 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001450 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001451 if (x != NULL) continue;
1452 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001453
Thomas Wouters434d0822000-08-24 20:11:32 +00001454 case INPLACE_DIVIDE:
Tim Peters54b11912001-12-25 18:49:11 +00001455 if (!_Py_QnewFlag) {
1456 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001457 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001458 x = PyNumber_InPlaceDivide(v, w);
1459 Py_DECREF(v);
1460 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001461 SET_TOP(x);
Tim Peters54b11912001-12-25 18:49:11 +00001462 if (x != NULL) continue;
1463 break;
1464 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001465 /* -Qnew is in effect: fall through to
Tim Peters54b11912001-12-25 18:49:11 +00001466 INPLACE_TRUE_DIVIDE */
1467 case INPLACE_TRUE_DIVIDE:
Thomas Wouters434d0822000-08-24 20:11:32 +00001468 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001469 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001470 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001471 Py_DECREF(v);
1472 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001473 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001474 if (x != NULL) continue;
1475 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001476
Guido van Rossum4668b002001-08-08 05:00:18 +00001477 case INPLACE_FLOOR_DIVIDE:
1478 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001479 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001480 x = PyNumber_InPlaceFloorDivide(v, w);
1481 Py_DECREF(v);
1482 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001483 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001484 if (x != NULL) continue;
1485 break;
1486
Thomas Wouters434d0822000-08-24 20:11:32 +00001487 case INPLACE_MODULO:
1488 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001489 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001490 x = PyNumber_InPlaceRemainder(v, w);
1491 Py_DECREF(v);
1492 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001493 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001494 if (x != NULL) continue;
1495 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001496
Thomas Wouters434d0822000-08-24 20:11:32 +00001497 case INPLACE_ADD:
1498 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001499 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001500 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001501 /* INLINE: int + int */
1502 register long a, b, i;
1503 a = PyInt_AS_LONG(v);
1504 b = PyInt_AS_LONG(w);
1505 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001506 if ((i^a) < 0 && (i^b) < 0)
1507 goto slow_iadd;
1508 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001509 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001510 else if (PyString_CheckExact(v) &&
1511 PyString_CheckExact(w)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001512 x = string_concatenate(v, w, f, next_instr);
1513 /* string_concatenate consumed the ref to v */
1514 goto skip_decref_v;
1515 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001516 else {
1517 slow_iadd:
Thomas Wouters434d0822000-08-24 20:11:32 +00001518 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001519 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001520 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001521 skip_decref_v:
Thomas Wouters434d0822000-08-24 20:11:32 +00001522 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001523 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001524 if (x != NULL) continue;
1525 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001526
Thomas Wouters434d0822000-08-24 20:11:32 +00001527 case INPLACE_SUBTRACT:
1528 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001529 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001530 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001531 /* INLINE: int - int */
1532 register long a, b, i;
1533 a = PyInt_AS_LONG(v);
1534 b = PyInt_AS_LONG(w);
1535 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001536 if ((i^a) < 0 && (i^~b) < 0)
1537 goto slow_isub;
1538 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001539 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001540 else {
1541 slow_isub:
Thomas Wouters434d0822000-08-24 20:11:32 +00001542 x = PyNumber_InPlaceSubtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001543 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001544 Py_DECREF(v);
1545 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001546 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001547 if (x != NULL) continue;
1548 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001549
Thomas Wouters434d0822000-08-24 20:11:32 +00001550 case INPLACE_LSHIFT:
1551 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001552 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001553 x = PyNumber_InPlaceLshift(v, w);
1554 Py_DECREF(v);
1555 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001556 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001557 if (x != NULL) continue;
1558 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001559
Thomas Wouters434d0822000-08-24 20:11:32 +00001560 case INPLACE_RSHIFT:
1561 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001562 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001563 x = PyNumber_InPlaceRshift(v, w);
1564 Py_DECREF(v);
1565 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001566 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001567 if (x != NULL) continue;
1568 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001569
Thomas Wouters434d0822000-08-24 20:11:32 +00001570 case INPLACE_AND:
1571 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001572 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001573 x = PyNumber_InPlaceAnd(v, w);
1574 Py_DECREF(v);
1575 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001576 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001577 if (x != NULL) continue;
1578 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001579
Thomas Wouters434d0822000-08-24 20:11:32 +00001580 case INPLACE_XOR:
1581 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001582 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001583 x = PyNumber_InPlaceXor(v, w);
1584 Py_DECREF(v);
1585 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001586 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001587 if (x != NULL) continue;
1588 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001589
Thomas Wouters434d0822000-08-24 20:11:32 +00001590 case INPLACE_OR:
1591 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001592 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001593 x = PyNumber_InPlaceOr(v, w);
1594 Py_DECREF(v);
1595 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001596 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001597 if (x != NULL) continue;
1598 break;
1599
Guido van Rossum374a9221991-04-04 10:40:29 +00001600 case SLICE+0:
1601 case SLICE+1:
1602 case SLICE+2:
1603 case SLICE+3:
1604 if ((opcode-SLICE) & 2)
1605 w = POP();
1606 else
1607 w = NULL;
1608 if ((opcode-SLICE) & 1)
1609 v = POP();
1610 else
1611 v = NULL;
Raymond Hettinger663004b2003-01-09 15:24:30 +00001612 u = TOP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001613 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001614 Py_DECREF(u);
1615 Py_XDECREF(v);
1616 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001617 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001618 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001619 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001620
Guido van Rossum374a9221991-04-04 10:40:29 +00001621 case STORE_SLICE+0:
1622 case STORE_SLICE+1:
1623 case STORE_SLICE+2:
1624 case STORE_SLICE+3:
1625 if ((opcode-STORE_SLICE) & 2)
1626 w = POP();
1627 else
1628 w = NULL;
1629 if ((opcode-STORE_SLICE) & 1)
1630 v = POP();
1631 else
1632 v = NULL;
1633 u = POP();
1634 t = POP();
1635 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +00001636 Py_DECREF(t);
1637 Py_DECREF(u);
1638 Py_XDECREF(v);
1639 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001640 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001641 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001642
Guido van Rossum374a9221991-04-04 10:40:29 +00001643 case DELETE_SLICE+0:
1644 case DELETE_SLICE+1:
1645 case DELETE_SLICE+2:
1646 case DELETE_SLICE+3:
1647 if ((opcode-DELETE_SLICE) & 2)
1648 w = POP();
1649 else
1650 w = NULL;
1651 if ((opcode-DELETE_SLICE) & 1)
1652 v = POP();
1653 else
1654 v = NULL;
1655 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001656 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +00001657 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00001658 Py_DECREF(u);
1659 Py_XDECREF(v);
1660 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001661 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001662 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001663
Guido van Rossum374a9221991-04-04 10:40:29 +00001664 case STORE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001665 w = TOP();
1666 v = SECOND();
1667 u = THIRD();
1668 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001669 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001670 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001671 Py_DECREF(u);
1672 Py_DECREF(v);
1673 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001674 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001675 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001676
Guido van Rossum374a9221991-04-04 10:40:29 +00001677 case DELETE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001678 w = TOP();
1679 v = SECOND();
1680 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001681 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001682 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001683 Py_DECREF(v);
1684 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001685 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001686 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001687
Guido van Rossum374a9221991-04-04 10:40:29 +00001688 case PRINT_EXPR:
1689 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001690 w = PySys_GetObject("displayhook");
1691 if (w == NULL) {
1692 PyErr_SetString(PyExc_RuntimeError,
1693 "lost sys.displayhook");
1694 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001695 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001696 }
1697 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001698 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001699 if (x == NULL)
1700 err = -1;
1701 }
1702 if (err == 0) {
1703 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001704 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001705 if (w == NULL)
1706 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001707 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001708 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001709 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001710 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001711
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001712 case PRINT_ITEM_TO:
1713 w = stream = POP();
1714 /* fall through to PRINT_ITEM */
1715
Guido van Rossum374a9221991-04-04 10:40:29 +00001716 case PRINT_ITEM:
1717 v = POP();
Barry Warsaw093abe02000-08-29 04:56:13 +00001718 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001719 w = PySys_GetObject("stdout");
1720 if (w == NULL) {
1721 PyErr_SetString(PyExc_RuntimeError,
1722 "lost sys.stdout");
1723 err = -1;
1724 }
Guido van Rossum8f183201997-12-31 05:53:15 +00001725 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001726 /* PyFile_SoftSpace() can exececute arbitrary code
1727 if sys.stdout is an instance with a __getattr__.
1728 If __getattr__ raises an exception, w will
1729 be freed, so we need to prevent that temporarily. */
1730 Py_XINCREF(w);
Tim Peters8e5fd532002-03-24 19:25:00 +00001731 if (w != NULL && PyFile_SoftSpace(w, 0))
Guido van Rossumbe270261997-05-22 22:26:18 +00001732 err = PyFile_WriteString(" ", w);
1733 if (err == 0)
1734 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001735 if (err == 0) {
Tim Peters8e5fd532002-03-24 19:25:00 +00001736 /* XXX move into writeobject() ? */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001737 if (PyString_Check(v)) {
1738 char *s = PyString_AS_STRING(v);
1739 Py_ssize_t len = PyString_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001740 if (len == 0 ||
1741 !isspace(Py_CHARMASK(s[len-1])) ||
1742 s[len-1] == ' ')
1743 PyFile_SoftSpace(w, 1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001744 }
Martin v. Löwis8d3ce5a2001-12-18 22:36:40 +00001745#ifdef Py_USING_UNICODE
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001746 else if (PyUnicode_Check(v)) {
1747 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
Martin v. Löwis66851282006-04-22 11:40:03 +00001748 Py_ssize_t len = PyUnicode_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001749 if (len == 0 ||
1750 !Py_UNICODE_ISSPACE(s[len-1]) ||
1751 s[len-1] == ' ')
1752 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001753 }
Michael W. Hudsond95c8282002-05-20 13:56:11 +00001754#endif
Tim Peters8e5fd532002-03-24 19:25:00 +00001755 else
1756 PyFile_SoftSpace(w, 1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001757 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001758 Py_XDECREF(w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001759 Py_DECREF(v);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001760 Py_XDECREF(stream);
1761 stream = NULL;
1762 if (err == 0)
1763 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001764 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001765
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001766 case PRINT_NEWLINE_TO:
1767 w = stream = POP();
1768 /* fall through to PRINT_NEWLINE */
1769
Guido van Rossum374a9221991-04-04 10:40:29 +00001770 case PRINT_NEWLINE:
Barry Warsaw093abe02000-08-29 04:56:13 +00001771 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001772 w = PySys_GetObject("stdout");
Jeffrey Yasskin2d873bd2008-12-08 18:55:24 +00001773 if (w == NULL) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001774 PyErr_SetString(PyExc_RuntimeError,
1775 "lost sys.stdout");
Jeffrey Yasskin2d873bd2008-12-08 18:55:24 +00001776 why = WHY_EXCEPTION;
1777 }
Guido van Rossum3165fe61992-09-25 21:59:05 +00001778 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001779 if (w != NULL) {
Georg Brandlaa76d772008-07-01 20:56:03 +00001780 /* w.write() may replace sys.stdout, so we
1781 * have to keep our reference to it */
Amaury Forgeot d'Arcbdd941f2008-07-01 20:38:04 +00001782 Py_INCREF(w);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001783 err = PyFile_WriteString("\n", w);
1784 if (err == 0)
1785 PyFile_SoftSpace(w, 0);
Amaury Forgeot d'Arcbdd941f2008-07-01 20:38:04 +00001786 Py_DECREF(w);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001787 }
1788 Py_XDECREF(stream);
1789 stream = NULL;
Guido van Rossum374a9221991-04-04 10:40:29 +00001790 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001791
Thomas Wouters434d0822000-08-24 20:11:32 +00001792
1793#ifdef CASE_TOO_BIG
1794 default: switch (opcode) {
1795#endif
Guido van Rossumf10570b1995-07-07 22:53:21 +00001796 case RAISE_VARARGS:
1797 u = v = w = NULL;
1798 switch (oparg) {
1799 case 3:
1800 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001801 /* Fallthrough */
1802 case 2:
1803 v = POP(); /* value */
1804 /* Fallthrough */
1805 case 1:
1806 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001807 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001808 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001809 break;
1810 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001811 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001812 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001813 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001814 break;
1815 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001816 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001817
Guido van Rossum374a9221991-04-04 10:40:29 +00001818 case LOAD_LOCALS:
Raymond Hettinger467a6982004-04-07 11:39:21 +00001819 if ((x = f->f_locals) != NULL) {
1820 Py_INCREF(x);
1821 PUSH(x);
1822 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001823 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001824 PyErr_SetString(PyExc_SystemError, "no locals");
Guido van Rossum374a9221991-04-04 10:40:29 +00001825 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001826
Guido van Rossum374a9221991-04-04 10:40:29 +00001827 case RETURN_VALUE:
1828 retval = POP();
1829 why = WHY_RETURN;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001830 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001831
Tim Peters5ca576e2001-06-18 22:08:13 +00001832 case YIELD_VALUE:
1833 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001834 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001835 why = WHY_YIELD;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001836 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001837
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001838 case EXEC_STMT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001839 w = TOP();
1840 v = SECOND();
1841 u = THIRD();
1842 STACKADJ(-3);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001843 READ_TIMESTAMP(intr0);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001844 err = exec_statement(f, u, v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001845 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00001846 Py_DECREF(u);
1847 Py_DECREF(v);
1848 Py_DECREF(w);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001849 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001850
Guido van Rossum374a9221991-04-04 10:40:29 +00001851 case POP_BLOCK:
1852 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001853 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001854 while (STACK_LEVEL() > b->b_level) {
1855 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001856 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001857 }
1858 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001859 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001860
Jeffrey Yasskin9063a992008-03-03 01:27:03 +00001861 PREDICTED(END_FINALLY);
Guido van Rossum374a9221991-04-04 10:40:29 +00001862 case END_FINALLY:
1863 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001864 if (PyInt_Check(v)) {
Raymond Hettinger7c958652004-04-06 10:11:10 +00001865 why = (enum why_code) PyInt_AS_LONG(v);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001866 assert(why != WHY_YIELD);
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00001867 if (why == WHY_RETURN ||
1868 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001869 retval = POP();
1870 }
Thomas Wouterse2176022007-09-20 17:35:10 +00001871 else if (PyExceptionClass_Check(v) ||
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001872 PyString_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001873 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001874 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001875 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001876 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001877 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001878 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001879 else if (v != Py_None) {
1880 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001881 "'finally' pops bad exception");
1882 why = WHY_EXCEPTION;
1883 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001884 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001885 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001886
Guido van Rossum374a9221991-04-04 10:40:29 +00001887 case BUILD_CLASS:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001888 u = TOP();
1889 v = SECOND();
1890 w = THIRD();
1891 STACKADJ(-2);
Guido van Rossum25831651993-05-19 14:50:45 +00001892 x = build_class(u, v, w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001893 SET_TOP(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001894 Py_DECREF(u);
1895 Py_DECREF(v);
1896 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001897 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001898
Guido van Rossum374a9221991-04-04 10:40:29 +00001899 case STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001900 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001901 v = POP();
Raymond Hettinger467a6982004-04-07 11:39:21 +00001902 if ((x = f->f_locals) != NULL) {
Raymond Hettinger66bd2332004-08-02 08:30:07 +00001903 if (PyDict_CheckExact(x))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001904 err = PyDict_SetItem(x, w, v);
1905 else
1906 err = PyObject_SetItem(x, w, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001907 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001908 if (err == 0) continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001909 break;
1910 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001911 PyErr_Format(PyExc_SystemError,
1912 "no locals found when storing %s",
1913 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001914 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001915
Guido van Rossum374a9221991-04-04 10:40:29 +00001916 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001917 w = GETITEM(names, oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001918 if ((x = f->f_locals) != NULL) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001919 if ((err = PyObject_DelItem(x, w)) != 0)
Raymond Hettinger467a6982004-04-07 11:39:21 +00001920 format_exc_check_arg(PyExc_NameError,
Thomas Wouterse2176022007-09-20 17:35:10 +00001921 NAME_ERROR_MSG,
1922 w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001923 break;
1924 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001925 PyErr_Format(PyExc_SystemError,
1926 "no locals when deleting %s",
1927 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001928 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001929
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001930 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001931 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001932 v = POP();
Thomas Wouterse2176022007-09-20 17:35:10 +00001933 if (PyTuple_CheckExact(v) &&
1934 PyTuple_GET_SIZE(v) == oparg) {
1935 PyObject **items = \
1936 ((PyTupleObject *)v)->ob_item;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001937 while (oparg--) {
1938 w = items[oparg];
1939 Py_INCREF(w);
1940 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001941 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001942 Py_DECREF(v);
1943 continue;
Thomas Wouterse2176022007-09-20 17:35:10 +00001944 } else if (PyList_CheckExact(v) &&
1945 PyList_GET_SIZE(v) == oparg) {
1946 PyObject **items = \
1947 ((PyListObject *)v)->ob_item;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001948 while (oparg--) {
1949 w = items[oparg];
1950 Py_INCREF(w);
1951 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001952 }
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001953 } else if (unpack_iterable(v, oparg,
Thomas Wouterse2176022007-09-20 17:35:10 +00001954 stack_pointer + oparg)) {
Tim Petersd6d010b2001-06-21 02:49:55 +00001955 stack_pointer += oparg;
Georg Brandl5cb76c12007-03-21 09:00:39 +00001956 } else {
1957 /* unpack_iterable() raised an exception */
Barry Warsawe42b18f1997-08-25 22:13:04 +00001958 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001959 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001960 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001961 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001962
Guido van Rossum374a9221991-04-04 10:40:29 +00001963 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001964 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001965 v = TOP();
1966 u = SECOND();
1967 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001968 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1969 Py_DECREF(v);
1970 Py_DECREF(u);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001971 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001972 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001973
Guido van Rossum374a9221991-04-04 10:40:29 +00001974 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001975 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001976 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001977 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1978 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001979 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001980 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001981
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001982 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001983 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001984 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001985 err = PyDict_SetItem(f->f_globals, w, v);
1986 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001987 if (err == 0) continue;
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001988 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001989
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001990 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001991 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001992 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001993 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001994 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001995 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001996
Guido van Rossum374a9221991-04-04 10:40:29 +00001997 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001998 w = GETITEM(names, oparg);
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001999 if ((v = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00002000 PyErr_Format(PyExc_SystemError,
2001 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00002002 PyObject_REPR(w));
Jeffrey Yasskin2d873bd2008-12-08 18:55:24 +00002003 why = WHY_EXCEPTION;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002004 break;
2005 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00002006 if (PyDict_CheckExact(v)) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00002007 x = PyDict_GetItem(v, w);
Michael W. Hudsona3711f72004-08-02 14:50:43 +00002008 Py_XINCREF(x);
2009 }
Raymond Hettinger214b1c32004-07-02 06:41:07 +00002010 else {
2011 x = PyObject_GetItem(v, w);
2012 if (x == NULL && PyErr_Occurred()) {
Thomas Wouterse2176022007-09-20 17:35:10 +00002013 if (!PyErr_ExceptionMatches(
2014 PyExc_KeyError))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00002015 break;
2016 PyErr_Clear();
2017 }
2018 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002019 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002020 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002021 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002022 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002023 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00002024 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00002025 PyExc_NameError,
Thomas Wouterse2176022007-09-20 17:35:10 +00002026 NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002027 break;
2028 }
2029 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00002030 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00002031 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002032 PUSH(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002033 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002034
Guido van Rossum374a9221991-04-04 10:40:29 +00002035 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00002036 w = GETITEM(names, oparg);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002037 if (PyString_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00002038 /* Inline the PyDict_GetItem() calls.
2039 WARNING: this is an extreme speed hack.
2040 Do not try this at home. */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002041 long hash = ((PyStringObject *)w)->ob_shash;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002042 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002043 PyDictObject *d;
Armin Rigo35f6d362006-06-01 13:19:12 +00002044 PyDictEntry *e;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002045 d = (PyDictObject *)(f->f_globals);
Armin Rigo35f6d362006-06-01 13:19:12 +00002046 e = d->ma_lookup(d, w, hash);
2047 if (e == NULL) {
2048 x = NULL;
2049 break;
2050 }
2051 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002052 if (x != NULL) {
2053 Py_INCREF(x);
2054 PUSH(x);
2055 continue;
2056 }
2057 d = (PyDictObject *)(f->f_builtins);
Armin Rigo35f6d362006-06-01 13:19:12 +00002058 e = d->ma_lookup(d, w, hash);
2059 if (e == NULL) {
2060 x = NULL;
2061 break;
2062 }
2063 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002064 if (x != NULL) {
2065 Py_INCREF(x);
2066 PUSH(x);
2067 continue;
2068 }
2069 goto load_global_error;
2070 }
2071 }
2072 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00002073 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002074 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002075 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002076 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002077 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00002078 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00002079 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002080 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002081 break;
2082 }
2083 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002084 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00002085 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00002086 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002087
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002088 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00002089 x = GETLOCAL(oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002090 if (x != NULL) {
2091 SETLOCAL(oparg, NULL);
2092 continue;
Guido van Rossum2e4c8991998-05-12 20:27:36 +00002093 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00002094 format_exc_check_arg(
2095 PyExc_UnboundLocalError,
2096 UNBOUNDLOCAL_ERROR_MSG,
2097 PyTuple_GetItem(co->co_varnames, oparg)
2098 );
2099 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002100
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002101 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00002102 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002103 Py_INCREF(x);
2104 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00002105 if (x != NULL) continue;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002106 break;
2107
2108 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00002109 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002110 w = PyCell_Get(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002111 if (w != NULL) {
2112 PUSH(w);
2113 continue;
Jeremy Hylton2524d692001-02-05 17:23:16 +00002114 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00002115 err = -1;
2116 /* Don't stomp existing exception */
2117 if (PyErr_Occurred())
2118 break;
Richard Jonescebbefc2006-05-23 18:28:17 +00002119 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
2120 v = PyTuple_GET_ITEM(co->co_cellvars,
Raymond Hettinger467a6982004-04-07 11:39:21 +00002121 oparg);
2122 format_exc_check_arg(
2123 PyExc_UnboundLocalError,
2124 UNBOUNDLOCAL_ERROR_MSG,
2125 v);
2126 } else {
Thomas Wouterse2176022007-09-20 17:35:10 +00002127 v = PyTuple_GET_ITEM(co->co_freevars, oparg -
2128 PyTuple_GET_SIZE(co->co_cellvars));
2129 format_exc_check_arg(PyExc_NameError,
2130 UNBOUNDFREE_ERROR_MSG, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002131 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002132 break;
2133
2134 case STORE_DEREF:
2135 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00002136 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002137 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00002138 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002139 continue;
2140
Guido van Rossum374a9221991-04-04 10:40:29 +00002141 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00002142 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002143 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002144 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002145 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002146 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002147 }
2148 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002149 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002150 }
2151 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002152
Guido van Rossum374a9221991-04-04 10:40:29 +00002153 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00002154 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002155 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002156 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002157 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00002158 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002159 }
2160 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002161 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002162 }
2163 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002164
Guido van Rossum374a9221991-04-04 10:40:29 +00002165 case BUILD_MAP:
Raymond Hettingerfd7ed402007-12-18 21:24:09 +00002166 x = _PyDict_NewPresized((Py_ssize_t)oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002167 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002168 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002169 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002170
Raymond Hettingereffde122007-12-18 18:26:18 +00002171 case STORE_MAP:
2172 w = TOP(); /* key */
2173 u = SECOND(); /* value */
2174 v = THIRD(); /* dict */
2175 STACKADJ(-2);
2176 assert (PyDict_CheckExact(v));
2177 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2178 Py_DECREF(u);
2179 Py_DECREF(w);
2180 if (err == 0) continue;
2181 break;
2182
Guido van Rossum374a9221991-04-04 10:40:29 +00002183 case LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00002184 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002185 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002186 x = PyObject_GetAttr(v, w);
2187 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002188 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002189 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002190 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002191
Guido van Rossum374a9221991-04-04 10:40:29 +00002192 case COMPARE_OP:
2193 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002194 v = TOP();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00002195 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00002196 /* INLINE: cmp(int, int) */
2197 register long a, b;
2198 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00002199 a = PyInt_AS_LONG(v);
2200 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00002201 switch (oparg) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00002202 case PyCmp_LT: res = a < b; break;
2203 case PyCmp_LE: res = a <= b; break;
2204 case PyCmp_EQ: res = a == b; break;
2205 case PyCmp_NE: res = a != b; break;
2206 case PyCmp_GT: res = a > b; break;
2207 case PyCmp_GE: res = a >= b; break;
2208 case PyCmp_IS: res = v == w; break;
2209 case PyCmp_IS_NOT: res = v != w; break;
Guido van Rossumc12da691997-07-17 23:12:42 +00002210 default: goto slow_compare;
2211 }
2212 x = res ? Py_True : Py_False;
2213 Py_INCREF(x);
2214 }
2215 else {
2216 slow_compare:
2217 x = cmp_outcome(oparg, v, w);
2218 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002219 Py_DECREF(v);
2220 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002221 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00002222 if (x == NULL) break;
2223 PREDICT(JUMP_IF_FALSE);
2224 PREDICT(JUMP_IF_TRUE);
2225 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002226
Guido van Rossum374a9221991-04-04 10:40:29 +00002227 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00002228 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002229 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002230 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002231 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00002232 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002233 break;
2234 }
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00002235 Py_INCREF(x);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002236 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002237 u = TOP();
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002238 if (PyInt_AsLong(u) != -1 || PyErr_Occurred())
2239 w = PyTuple_Pack(5,
2240 w,
2241 f->f_globals,
2242 f->f_locals == NULL ?
2243 Py_None : f->f_locals,
2244 v,
2245 u);
2246 else
2247 w = PyTuple_Pack(4,
2248 w,
2249 f->f_globals,
2250 f->f_locals == NULL ?
2251 Py_None : f->f_locals,
2252 v);
2253 Py_DECREF(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002254 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002255 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002256 u = POP();
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00002257 Py_DECREF(x);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002258 x = NULL;
2259 break;
2260 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002261 READ_TIMESTAMP(intr0);
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00002262 v = x;
2263 x = PyEval_CallObject(v, w);
2264 Py_DECREF(v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002265 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002266 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002267 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002268 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002269 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002270
Thomas Wouters52152252000-08-17 22:55:00 +00002271 case IMPORT_STAR:
2272 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002273 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002274 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002275 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00002276 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002277 break;
2278 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002279 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002280 err = import_all_from(x, v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002281 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002282 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00002283 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002284 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002285 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002286
Thomas Wouters52152252000-08-17 22:55:00 +00002287 case IMPORT_FROM:
Skip Montanaro496e6582002-08-06 17:47:40 +00002288 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00002289 v = TOP();
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002290 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002291 x = import_from(v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002292 READ_TIMESTAMP(intr1);
Thomas Wouters52152252000-08-17 22:55:00 +00002293 PUSH(x);
2294 if (x != NULL) continue;
2295 break;
2296
Guido van Rossum374a9221991-04-04 10:40:29 +00002297 case JUMP_FORWARD:
2298 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002299 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002300
Raymond Hettingerf606f872003-03-16 03:11:04 +00002301 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002302 case JUMP_IF_FALSE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002303 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002304 if (w == Py_True) {
2305 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002306 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002307 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002308 if (w == Py_False) {
2309 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002310 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002311 }
2312 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002313 if (err > 0)
2314 err = 0;
2315 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00002316 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002317 else
2318 break;
2319 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002320
Raymond Hettingerf606f872003-03-16 03:11:04 +00002321 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002322 case JUMP_IF_TRUE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002323 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002324 if (w == Py_False) {
2325 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002326 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002327 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002328 if (w == Py_True) {
2329 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002330 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002331 }
2332 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002333 if (err > 0) {
2334 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00002335 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002336 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002337 else if (err == 0)
2338 ;
2339 else
2340 break;
2341 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002342
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00002343 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002344 case JUMP_ABSOLUTE:
2345 JUMPTO(oparg);
Raymond Hettingerdc1d1ba2007-11-07 02:45:46 +00002346#if FAST_LOOPS
2347 /* Enabling this path speeds-up all while and for-loops by bypassing
2348 the per-loop checks for signals. By default, this should be turned-off
2349 because it prevents detection of a control-break in tight loops like
2350 "while 1: pass". Compile with this option turned-on when you need
2351 the speed-up and do not need break checking inside tight loops (ones
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00002352 that contain only instructions ending with goto fast_next_opcode).
Raymond Hettingerdc1d1ba2007-11-07 02:45:46 +00002353 */
2354 goto fast_next_opcode;
2355#else
Neil Schemenauerca2a2f12003-05-30 23:59:44 +00002356 continue;
Raymond Hettingerdc1d1ba2007-11-07 02:45:46 +00002357#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002358
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002359 case GET_ITER:
2360 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002361 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002362 x = PyObject_GetIter(v);
2363 Py_DECREF(v);
2364 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002365 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002366 PREDICT(FOR_ITER);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002367 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002368 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002369 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002370 break;
2371
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002372 PREDICTED_WITH_ARG(FOR_ITER);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002373 case FOR_ITER:
2374 /* before: [iter]; after: [iter, iter()] *or* [] */
2375 v = TOP();
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002376 x = (*v->ob_type->tp_iternext)(v);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002377 if (x != NULL) {
2378 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002379 PREDICT(STORE_FAST);
2380 PREDICT(UNPACK_SEQUENCE);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002381 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002382 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002383 if (PyErr_Occurred()) {
Thomas Wouterse2176022007-09-20 17:35:10 +00002384 if (!PyErr_ExceptionMatches(
2385 PyExc_StopIteration))
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002386 break;
2387 PyErr_Clear();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002388 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002389 /* iterator ended normally */
2390 x = v = POP();
2391 Py_DECREF(v);
2392 JUMPBY(oparg);
2393 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002394
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002395 case BREAK_LOOP:
2396 why = WHY_BREAK;
2397 goto fast_block_end;
2398
2399 case CONTINUE_LOOP:
2400 retval = PyInt_FromLong(oparg);
Neal Norwitz02104df2006-05-19 06:31:23 +00002401 if (!retval) {
2402 x = NULL;
2403 break;
2404 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002405 why = WHY_CONTINUE;
2406 goto fast_block_end;
2407
Guido van Rossum374a9221991-04-04 10:40:29 +00002408 case SETUP_LOOP:
2409 case SETUP_EXCEPT:
2410 case SETUP_FINALLY:
Thomas Wouterse2176022007-09-20 17:35:10 +00002411 /* NOTE: If you add any new block-setup opcodes that
2412 are not try/except/finally handlers, you may need
2413 to update the PyGen_NeedsFinalizing() function.
2414 */
Phillip J. Eby2ba96612006-04-10 17:51:05 +00002415
Guido van Rossumb209a111997-04-29 18:18:01 +00002416 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002417 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002418 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002419
Guido van Rossumc2e20742006-02-27 22:32:47 +00002420 case WITH_CLEANUP:
2421 {
Nick Coghlan7af53be2008-03-07 14:13:28 +00002422 /* At the top of the stack are 1-3 values indicating
2423 how/why we entered the finally clause:
2424 - TOP = None
2425 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2426 - TOP = WHY_*; no retval below it
2427 - (TOP, SECOND, THIRD) = exc_info()
2428 Below them is EXIT, the context.__exit__ bound method.
Guido van Rossumc2e20742006-02-27 22:32:47 +00002429 In the last case, we must call
Nick Coghlan7af53be2008-03-07 14:13:28 +00002430 EXIT(TOP, SECOND, THIRD)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002431 otherwise we must call
Nick Coghlan7af53be2008-03-07 14:13:28 +00002432 EXIT(None, None, None)
2433
2434 In all cases, we remove EXIT from the stack, leaving
2435 the rest in the same order.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002436
2437 In addition, if the stack represents an exception,
Guido van Rossumf6694362006-03-10 02:28:35 +00002438 *and* the function call returns a 'true' value, we
2439 "zap" this information, to prevent END_FINALLY from
2440 re-raising the exception. (But non-local gotos
2441 should still be resumed.)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002442 */
Tim Peters7df5e7f2006-05-26 23:14:37 +00002443
Nick Coghlan7af53be2008-03-07 14:13:28 +00002444 PyObject *exit_func;
2445
2446 u = POP();
2447 if (u == Py_None) {
2448 exit_func = TOP();
2449 SET_TOP(u);
2450 v = w = Py_None;
2451 }
2452 else if (PyInt_Check(u)) {
2453 switch(PyInt_AS_LONG(u)) {
2454 case WHY_RETURN:
2455 case WHY_CONTINUE:
2456 /* Retval in TOP. */
2457 exit_func = SECOND();
2458 SET_SECOND(TOP());
2459 SET_TOP(u);
2460 break;
2461 default:
2462 exit_func = TOP();
2463 SET_TOP(u);
2464 break;
2465 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002466 u = v = w = Py_None;
2467 }
2468 else {
Nick Coghlan7af53be2008-03-07 14:13:28 +00002469 v = TOP();
2470 w = SECOND();
2471 exit_func = THIRD();
2472 SET_TOP(u);
2473 SET_SECOND(v);
2474 SET_THIRD(w);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002475 }
Guido van Rossumf6694362006-03-10 02:28:35 +00002476 /* XXX Not the fastest way to call it... */
Nick Coghlan7af53be2008-03-07 14:13:28 +00002477 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2478 NULL);
Amaury Forgeot d'Arcad9b5992008-12-10 23:22:49 +00002479 Py_DECREF(exit_func);
2480 if (x == NULL)
Guido van Rossumf6694362006-03-10 02:28:35 +00002481 break; /* Go to error exit */
Amaury Forgeot d'Arcad9b5992008-12-10 23:22:49 +00002482
2483 if (u != Py_None)
2484 err = PyObject_IsTrue(x);
2485 else
2486 err = 0;
2487 Py_DECREF(x);
2488
2489 if (err < 0)
2490 break; /* Go to error exit */
2491 else if (err > 0) {
2492 err = 0;
Guido van Rossumf6694362006-03-10 02:28:35 +00002493 /* There was an exception and a true return */
Nick Coghlan7af53be2008-03-07 14:13:28 +00002494 STACKADJ(-2);
Guido van Rossumf6694362006-03-10 02:28:35 +00002495 Py_INCREF(Py_None);
2496 SET_TOP(Py_None);
Guido van Rossumf6694362006-03-10 02:28:35 +00002497 Py_DECREF(u);
2498 Py_DECREF(v);
2499 Py_DECREF(w);
2500 } else {
Nick Coghlan7af53be2008-03-07 14:13:28 +00002501 /* The stack was rearranged to remove EXIT
2502 above. Let END_FINALLY do its thing */
Guido van Rossumf6694362006-03-10 02:28:35 +00002503 }
Jeffrey Yasskin9063a992008-03-03 01:27:03 +00002504 PREDICT(END_FINALLY);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002505 break;
2506 }
2507
Guido van Rossumf10570b1995-07-07 22:53:21 +00002508 case CALL_FUNCTION:
Armin Rigo8817fcd2004-06-17 10:22:40 +00002509 {
2510 PyObject **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002511 PCALL(PCALL_ALL);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002512 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002513#ifdef WITH_TSC
Armin Rigo8817fcd2004-06-17 10:22:40 +00002514 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002515#else
Armin Rigo8817fcd2004-06-17 10:22:40 +00002516 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002517#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002518 stack_pointer = sp;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002519 PUSH(x);
2520 if (x != NULL)
2521 continue;
2522 break;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002523 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002524
Jeremy Hylton76901512000-03-28 23:49:17 +00002525 case CALL_FUNCTION_VAR:
2526 case CALL_FUNCTION_KW:
2527 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002528 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002529 int na = oparg & 0xff;
2530 int nk = (oparg>>8) & 0xff;
2531 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002532 int n = na + 2 * nk;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002533 PyObject **pfunc, *func, **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002534 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002535 if (flags & CALL_FLAG_VAR)
2536 n++;
2537 if (flags & CALL_FLAG_KW)
2538 n++;
2539 pfunc = stack_pointer - n - 1;
2540 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002541
Guido van Rossumac7be682001-01-17 15:42:30 +00002542 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002543 && PyMethod_GET_SELF(func) != NULL) {
2544 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002545 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002546 func = PyMethod_GET_FUNCTION(func);
2547 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002548 Py_DECREF(*pfunc);
2549 *pfunc = self;
2550 na++;
2551 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002552 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002553 Py_INCREF(func);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002554 sp = stack_pointer;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002555 READ_TIMESTAMP(intr0);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002556 x = ext_do_call(func, &sp, flags, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002557 READ_TIMESTAMP(intr1);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002558 stack_pointer = sp;
Jeremy Hylton76901512000-03-28 23:49:17 +00002559 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002560
Jeremy Hylton76901512000-03-28 23:49:17 +00002561 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002562 w = POP();
2563 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002564 }
2565 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002566 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002567 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002568 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002569 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002570
Guido van Rossum681d79a1995-07-18 14:51:37 +00002571 case MAKE_FUNCTION:
2572 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002573 x = PyFunction_New(v, f->f_globals);
2574 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002575 /* XXX Maybe this should be a separate opcode? */
2576 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002577 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002578 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002579 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002580 x = NULL;
2581 break;
2582 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002583 while (--oparg >= 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002584 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002585 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002586 }
2587 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002588 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002589 }
2590 PUSH(x);
2591 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002592
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002593 case MAKE_CLOSURE:
2594 {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002595 v = POP(); /* code object */
2596 x = PyFunction_New(v, f->f_globals);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002597 Py_DECREF(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002598 if (x != NULL) {
2599 v = POP();
Jeffrey Yasskin2d873bd2008-12-08 18:55:24 +00002600 if (PyFunction_SetClosure(x, v) != 0) {
2601 /* Can't happen unless bytecode is corrupt. */
2602 why = WHY_EXCEPTION;
2603 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002604 Py_DECREF(v);
2605 }
2606 if (x != NULL && oparg > 0) {
2607 v = PyTuple_New(oparg);
2608 if (v == NULL) {
2609 Py_DECREF(x);
2610 x = NULL;
2611 break;
2612 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002613 while (--oparg >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002614 w = POP();
2615 PyTuple_SET_ITEM(v, oparg, w);
2616 }
Jeffrey Yasskin2d873bd2008-12-08 18:55:24 +00002617 if (PyFunction_SetDefaults(x, v) != 0) {
2618 /* Can't happen unless
2619 PyFunction_SetDefaults changes. */
2620 why = WHY_EXCEPTION;
2621 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002622 Py_DECREF(v);
2623 }
2624 PUSH(x);
2625 break;
2626 }
2627
Guido van Rossum8861b741996-07-30 16:49:37 +00002628 case BUILD_SLICE:
2629 if (oparg == 3)
2630 w = POP();
2631 else
2632 w = NULL;
2633 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002634 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002635 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002636 Py_DECREF(u);
2637 Py_DECREF(v);
2638 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002639 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002640 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002641 break;
2642
Fred Drakeef8ace32000-08-24 00:32:09 +00002643 case EXTENDED_ARG:
2644 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002645 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002646 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002647
Guido van Rossum374a9221991-04-04 10:40:29 +00002648 default:
2649 fprintf(stderr,
2650 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002651 PyCode_Addr2Line(f->f_code, f->f_lasti),
2652 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002653 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002654 why = WHY_EXCEPTION;
2655 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002656
2657#ifdef CASE_TOO_BIG
2658 }
2659#endif
2660
Guido van Rossum374a9221991-04-04 10:40:29 +00002661 } /* switch */
2662
2663 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002664
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002665 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002666
Guido van Rossum374a9221991-04-04 10:40:29 +00002667 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002668
Guido van Rossum374a9221991-04-04 10:40:29 +00002669 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002670 if (err == 0 && x != NULL) {
2671#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002672 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002673 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002674 fprintf(stderr,
2675 "XXX undetected error\n");
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002676 else {
2677#endif
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002678 READ_TIMESTAMP(loop1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002679 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002680#ifdef CHECKEXC
2681 }
2682#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +00002683 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002684 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002685 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002686 err = 0;
2687 }
2688
Guido van Rossum374a9221991-04-04 10:40:29 +00002689 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002690
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002691 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002692 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002693 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002694 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002695 why = WHY_EXCEPTION;
2696 }
2697 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002698#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002699 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002700 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002701 if (PyErr_Occurred()) {
Neal Norwitz8250fbe2008-01-27 17:12:15 +00002702 char buf[128];
Jeremy Hylton904ed862003-11-05 17:29:35 +00002703 sprintf(buf, "Stack unwind with exception "
2704 "set and why=%d", why);
2705 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002706 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002707 }
2708#endif
2709
2710 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002711
Guido van Rossum374a9221991-04-04 10:40:29 +00002712 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002713 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002714
Fred Drake8f51f542001-10-04 14:48:42 +00002715 if (tstate->c_tracefunc != NULL)
2716 call_exc_trace(tstate->c_tracefunc,
2717 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002718 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002719
Guido van Rossum374a9221991-04-04 10:40:29 +00002720 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002721
Guido van Rossum374a9221991-04-04 10:40:29 +00002722 if (why == WHY_RERAISE)
2723 why = WHY_EXCEPTION;
2724
2725 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002726
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002727fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002728 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002729 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002730
Tim Peters8a5c3c72004-04-05 19:36:21 +00002731 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002732 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2733 /* For a continue inside a try block,
2734 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002735 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2736 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002737 why = WHY_NOT;
2738 JUMPTO(PyInt_AS_LONG(retval));
2739 Py_DECREF(retval);
2740 break;
2741 }
2742
Guido van Rossum374a9221991-04-04 10:40:29 +00002743 while (STACK_LEVEL() > b->b_level) {
2744 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002745 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002746 }
2747 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2748 why = WHY_NOT;
2749 JUMPTO(b->b_handler);
2750 break;
2751 }
2752 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002753 (b->b_type == SETUP_EXCEPT &&
2754 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002755 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002756 PyObject *exc, *val, *tb;
2757 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002758 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002759 val = Py_None;
2760 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002761 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002762 /* Make the raw exception data
2763 available to the handler,
2764 so a program can emulate the
2765 Python main loop. Don't do
2766 this for 'finally'. */
2767 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002768 PyErr_NormalizeException(
2769 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002770 set_exc_info(tstate,
2771 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002772 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002773 if (tb == NULL) {
2774 Py_INCREF(Py_None);
2775 PUSH(Py_None);
2776 } else
2777 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002778 PUSH(val);
2779 PUSH(exc);
2780 }
2781 else {
Raymond Hettinger06032cb2004-04-06 09:37:35 +00002782 if (why & (WHY_RETURN | WHY_CONTINUE))
Guido van Rossum374a9221991-04-04 10:40:29 +00002783 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002784 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002785 PUSH(v);
2786 }
2787 why = WHY_NOT;
2788 JUMPTO(b->b_handler);
2789 break;
2790 }
2791 } /* unwind stack */
2792
2793 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002794
Guido van Rossum374a9221991-04-04 10:40:29 +00002795 if (why != WHY_NOT)
2796 break;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002797 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002798
Guido van Rossum374a9221991-04-04 10:40:29 +00002799 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002800
Tim Peters8a5c3c72004-04-05 19:36:21 +00002801 assert(why != WHY_YIELD);
2802 /* Pop remaining stack entries. */
2803 while (!EMPTY()) {
2804 v = POP();
2805 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002806 }
2807
Tim Peters8a5c3c72004-04-05 19:36:21 +00002808 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002809 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002810
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002811fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002812 if (tstate->use_tracing) {
Barry Warsawe2eca0b2005-08-15 18:14:19 +00002813 if (tstate->c_tracefunc) {
2814 if (why == WHY_RETURN || why == WHY_YIELD) {
2815 if (call_trace(tstate->c_tracefunc,
2816 tstate->c_traceobj, f,
2817 PyTrace_RETURN, retval)) {
2818 Py_XDECREF(retval);
2819 retval = NULL;
2820 why = WHY_EXCEPTION;
2821 }
2822 }
2823 else if (why == WHY_EXCEPTION) {
2824 call_trace_protected(tstate->c_tracefunc,
2825 tstate->c_traceobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002826 PyTrace_RETURN, NULL);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002827 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002828 }
Fred Drake8f51f542001-10-04 14:48:42 +00002829 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002830 if (why == WHY_EXCEPTION)
2831 call_trace_protected(tstate->c_profilefunc,
2832 tstate->c_profileobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002833 PyTrace_RETURN, NULL);
Fred Drake4ec5d562001-10-04 19:26:43 +00002834 else if (call_trace(tstate->c_profilefunc,
2835 tstate->c_profileobj, f,
2836 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002837 Py_XDECREF(retval);
2838 retval = NULL;
2839 why = WHY_EXCEPTION;
2840 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002841 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002842 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002843
Tim Peters7df5e7f2006-05-26 23:14:37 +00002844 if (tstate->frame->f_exc_type != NULL)
2845 reset_exc_info(tstate);
2846 else {
2847 assert(tstate->frame->f_exc_value == NULL);
2848 assert(tstate->frame->f_exc_traceback == NULL);
2849 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00002850
Tim Peters5ca576e2001-06-18 22:08:13 +00002851 /* pop frame */
Thomas Woutersae406c62007-09-19 17:27:43 +00002852exit_eval_frame:
Armin Rigo2b3eb402003-10-28 12:05:48 +00002853 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002854 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002855
Guido van Rossum96a42c81992-01-12 02:29:51 +00002856 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002857}
2858
Guido van Rossumc2e20742006-02-27 22:32:47 +00002859/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002860 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00002861 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002862
Tim Peters6d6c1a32001-08-02 04:15:00 +00002863PyObject *
2864PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002865 PyObject **args, int argcount, PyObject **kws, int kwcount,
2866 PyObject **defs, int defcount, PyObject *closure)
2867{
2868 register PyFrameObject *f;
2869 register PyObject *retval = NULL;
2870 register PyObject **fastlocals, **freevars;
2871 PyThreadState *tstate = PyThreadState_GET();
2872 PyObject *x, *u;
2873
2874 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00002875 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002876 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002877 return NULL;
2878 }
2879
Neal Norwitzdf6a6492006-08-13 18:10:10 +00002880 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00002881 assert(globals != NULL);
2882 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002883 if (f == NULL)
2884 return NULL;
2885
2886 fastlocals = f->f_localsplus;
Richard Jonescebbefc2006-05-23 18:28:17 +00002887 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00002888
2889 if (co->co_argcount > 0 ||
2890 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2891 int i;
2892 int n = argcount;
2893 PyObject *kwdict = NULL;
2894 if (co->co_flags & CO_VARKEYWORDS) {
2895 kwdict = PyDict_New();
2896 if (kwdict == NULL)
2897 goto fail;
2898 i = co->co_argcount;
2899 if (co->co_flags & CO_VARARGS)
2900 i++;
2901 SETLOCAL(i, kwdict);
2902 }
2903 if (argcount > co->co_argcount) {
2904 if (!(co->co_flags & CO_VARARGS)) {
2905 PyErr_Format(PyExc_TypeError,
2906 "%.200s() takes %s %d "
2907 "%sargument%s (%d given)",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002908 PyString_AsString(co->co_name),
Tim Peters5ca576e2001-06-18 22:08:13 +00002909 defcount ? "at most" : "exactly",
2910 co->co_argcount,
2911 kwcount ? "non-keyword " : "",
2912 co->co_argcount == 1 ? "" : "s",
2913 argcount);
2914 goto fail;
2915 }
2916 n = co->co_argcount;
2917 }
2918 for (i = 0; i < n; i++) {
2919 x = args[i];
2920 Py_INCREF(x);
2921 SETLOCAL(i, x);
2922 }
2923 if (co->co_flags & CO_VARARGS) {
2924 u = PyTuple_New(argcount - n);
2925 if (u == NULL)
2926 goto fail;
2927 SETLOCAL(co->co_argcount, u);
2928 for (i = n; i < argcount; i++) {
2929 x = args[i];
2930 Py_INCREF(x);
2931 PyTuple_SET_ITEM(u, i-n, x);
2932 }
2933 }
2934 for (i = 0; i < kwcount; i++) {
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00002935 PyObject **co_varnames;
Tim Peters5ca576e2001-06-18 22:08:13 +00002936 PyObject *keyword = kws[2*i];
2937 PyObject *value = kws[2*i + 1];
2938 int j;
Benjamin Peterson78821dd2009-01-25 17:15:10 +00002939 if (keyword == NULL || !(PyString_Check(keyword)
2940#ifdef Py_USING_UNICODE
2941 || PyUnicode_Check(keyword)
2942#endif
2943 )) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002944 PyErr_Format(PyExc_TypeError,
2945 "%.200s() keywords must be strings",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002946 PyString_AsString(co->co_name));
Tim Peters5ca576e2001-06-18 22:08:13 +00002947 goto fail;
2948 }
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00002949 /* Speed hack: do raw pointer compares. As names are
2950 normally interned this should almost always hit. */
2951 co_varnames = PySequence_Fast_ITEMS(co->co_varnames);
Tim Peters5ca576e2001-06-18 22:08:13 +00002952 for (j = 0; j < co->co_argcount; j++) {
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00002953 PyObject *nm = co_varnames[j];
2954 if (nm == keyword)
2955 goto kw_found;
2956 }
2957 /* Slow fallback, just in case */
2958 for (j = 0; j < co->co_argcount; j++) {
2959 PyObject *nm = co_varnames[j];
Tim Peters5ca576e2001-06-18 22:08:13 +00002960 int cmp = PyObject_RichCompareBool(
2961 keyword, nm, Py_EQ);
2962 if (cmp > 0)
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00002963 goto kw_found;
Tim Peters5ca576e2001-06-18 22:08:13 +00002964 else if (cmp < 0)
2965 goto fail;
2966 }
2967 /* Check errors from Compare */
2968 if (PyErr_Occurred())
2969 goto fail;
2970 if (j >= co->co_argcount) {
2971 if (kwdict == NULL) {
Benjamin Petersone18ef192009-01-20 14:21:16 +00002972 PyObject *kwd_str = kwd_as_string(keyword);
2973 if (kwd_str) {
2974 PyErr_Format(PyExc_TypeError,
2975 "%.200s() got an unexpected "
2976 "keyword argument '%.400s'",
2977 PyString_AsString(co->co_name),
2978 PyString_AsString(kwd_str));
2979 Py_DECREF(kwd_str);
2980 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002981 goto fail;
2982 }
2983 PyDict_SetItem(kwdict, keyword, value);
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00002984 continue;
Tim Peters5ca576e2001-06-18 22:08:13 +00002985 }
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00002986kw_found:
2987 if (GETLOCAL(j) != NULL) {
Benjamin Petersone18ef192009-01-20 14:21:16 +00002988 PyObject *kwd_str = kwd_as_string(keyword);
2989 if (kwd_str) {
2990 PyErr_Format(PyExc_TypeError,
2991 "%.200s() got multiple "
2992 "values for keyword "
2993 "argument '%.400s'",
2994 PyString_AsString(co->co_name),
2995 PyString_AsString(kwd_str));
2996 Py_DECREF(kwd_str);
2997 }
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00002998 goto fail;
Tim Peters5ca576e2001-06-18 22:08:13 +00002999 }
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00003000 Py_INCREF(value);
3001 SETLOCAL(j, value);
Tim Peters5ca576e2001-06-18 22:08:13 +00003002 }
3003 if (argcount < co->co_argcount) {
3004 int m = co->co_argcount - defcount;
3005 for (i = argcount; i < m; i++) {
3006 if (GETLOCAL(i) == NULL) {
3007 PyErr_Format(PyExc_TypeError,
3008 "%.200s() takes %s %d "
3009 "%sargument%s (%d given)",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003010 PyString_AsString(co->co_name),
Tim Peters5ca576e2001-06-18 22:08:13 +00003011 ((co->co_flags & CO_VARARGS) ||
3012 defcount) ? "at least"
3013 : "exactly",
3014 m, kwcount ? "non-keyword " : "",
3015 m == 1 ? "" : "s", i);
3016 goto fail;
3017 }
3018 }
3019 if (n > m)
3020 i = n - m;
3021 else
3022 i = 0;
3023 for (; i < defcount; i++) {
3024 if (GETLOCAL(m+i) == NULL) {
3025 PyObject *def = defs[i];
3026 Py_INCREF(def);
3027 SETLOCAL(m+i, def);
3028 }
3029 }
3030 }
3031 }
3032 else {
3033 if (argcount > 0 || kwcount > 0) {
3034 PyErr_Format(PyExc_TypeError,
3035 "%.200s() takes no arguments (%d given)",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003036 PyString_AsString(co->co_name),
Tim Peters5ca576e2001-06-18 22:08:13 +00003037 argcount + kwcount);
3038 goto fail;
3039 }
3040 }
3041 /* Allocate and initialize storage for cell vars, and copy free
3042 vars into frame. This isn't too efficient right now. */
Richard Jonescebbefc2006-05-23 18:28:17 +00003043 if (PyTuple_GET_SIZE(co->co_cellvars)) {
Neal Norwitz245ce8d2006-06-12 02:16:10 +00003044 int i, j, nargs, found;
Tim Peters5ca576e2001-06-18 22:08:13 +00003045 char *cellname, *argname;
3046 PyObject *c;
3047
3048 nargs = co->co_argcount;
3049 if (co->co_flags & CO_VARARGS)
3050 nargs++;
3051 if (co->co_flags & CO_VARKEYWORDS)
3052 nargs++;
3053
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003054 /* Initialize each cell var, taking into account
3055 cell vars that are initialized from arguments.
3056
3057 Should arrange for the compiler to put cellvars
3058 that are arguments at the beginning of the cellvars
3059 list so that we can march over it more efficiently?
3060 */
Richard Jonescebbefc2006-05-23 18:28:17 +00003061 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003062 cellname = PyString_AS_STRING(
Tim Peters5ca576e2001-06-18 22:08:13 +00003063 PyTuple_GET_ITEM(co->co_cellvars, i));
3064 found = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003065 for (j = 0; j < nargs; j++) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003066 argname = PyString_AS_STRING(
Tim Peters5ca576e2001-06-18 22:08:13 +00003067 PyTuple_GET_ITEM(co->co_varnames, j));
3068 if (strcmp(cellname, argname) == 0) {
3069 c = PyCell_New(GETLOCAL(j));
3070 if (c == NULL)
3071 goto fail;
Richard Jonescebbefc2006-05-23 18:28:17 +00003072 GETLOCAL(co->co_nlocals + i) = c;
Tim Peters5ca576e2001-06-18 22:08:13 +00003073 found = 1;
3074 break;
3075 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003076 }
3077 if (found == 0) {
3078 c = PyCell_New(NULL);
3079 if (c == NULL)
3080 goto fail;
Richard Jonescebbefc2006-05-23 18:28:17 +00003081 SETLOCAL(co->co_nlocals + i, c);
Tim Peters5ca576e2001-06-18 22:08:13 +00003082 }
3083 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003084 }
Richard Jonescebbefc2006-05-23 18:28:17 +00003085 if (PyTuple_GET_SIZE(co->co_freevars)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003086 int i;
Richard Jonescebbefc2006-05-23 18:28:17 +00003087 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003088 PyObject *o = PyTuple_GET_ITEM(closure, i);
3089 Py_INCREF(o);
Richard Jonescebbefc2006-05-23 18:28:17 +00003090 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Tim Peters5ca576e2001-06-18 22:08:13 +00003091 }
3092 }
3093
Tim Peters5ca576e2001-06-18 22:08:13 +00003094 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003095 /* Don't need to keep the reference to f_back, it will be set
3096 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00003097 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003098 f->f_back = NULL;
3099
Jeremy Hylton985eba52003-02-05 23:13:00 +00003100 PCALL(PCALL_GENERATOR);
3101
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003102 /* Create a new generator that owns the ready to run frame
3103 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00003104 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00003105 }
3106
Thomas Woutersae406c62007-09-19 17:27:43 +00003107 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003108
Thomas Woutersae406c62007-09-19 17:27:43 +00003109fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003110
Tim Petersb13680b2001-11-27 23:29:29 +00003111 /* decref'ing the frame can cause __del__ methods to get invoked,
3112 which can call back into Python. While we're done with the
3113 current Python frame (f), the associated C stack is still in use,
3114 so recursion_depth must be boosted for the duration.
3115 */
3116 assert(tstate != NULL);
3117 ++tstate->recursion_depth;
Thomas Woutersae406c62007-09-19 17:27:43 +00003118 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00003119 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00003120 return retval;
3121}
3122
3123
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003124
Benjamin Petersone18ef192009-01-20 14:21:16 +00003125static PyObject *
3126kwd_as_string(PyObject *kwd) {
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003127#ifdef Py_USING_UNICODE
Benjamin Petersone18ef192009-01-20 14:21:16 +00003128 if (PyString_Check(kwd)) {
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003129#else
3130 assert(PyString_Check(kwd));
3131#endif
Benjamin Petersone18ef192009-01-20 14:21:16 +00003132 Py_INCREF(kwd);
3133 return kwd;
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003134#ifdef Py_USING_UNICODE
Benjamin Petersone18ef192009-01-20 14:21:16 +00003135 }
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003136 return _PyUnicode_AsDefaultEncodedString(kwd, "replace");
3137#endif
Benjamin Petersone18ef192009-01-20 14:21:16 +00003138}
3139
3140
Guido van Rossumc9fbb722003-03-01 03:36:33 +00003141/* Implementation notes for set_exc_info() and reset_exc_info():
3142
3143- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
3144 'exc_traceback'. These always travel together.
3145
3146- tstate->curexc_ZZZ is the "hot" exception that is set by
3147 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
3148
3149- Once an exception is caught by an except clause, it is transferred
3150 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
3151 can pick it up. This is the primary task of set_exc_info().
Tim Peters7df5e7f2006-05-26 23:14:37 +00003152 XXX That can't be right: set_exc_info() doesn't look at tstate->curexc_ZZZ.
Guido van Rossumc9fbb722003-03-01 03:36:33 +00003153
3154- Now let me explain the complicated dance with frame->f_exc_ZZZ.
3155
3156 Long ago, when none of this existed, there were just a few globals:
3157 one set corresponding to the "hot" exception, and one set
3158 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
3159 globals; they were simply stored as sys.exc_ZZZ. For backwards
3160 compatibility, they still are!) The problem was that in code like
3161 this:
3162
3163 try:
3164 "something that may fail"
3165 except "some exception":
3166 "do something else first"
3167 "print the exception from sys.exc_ZZZ."
3168
3169 if "do something else first" invoked something that raised and caught
3170 an exception, sys.exc_ZZZ were overwritten. That was a frequent
3171 cause of subtle bugs. I fixed this by changing the semantics as
3172 follows:
3173
3174 - Within one frame, sys.exc_ZZZ will hold the last exception caught
3175 *in that frame*.
3176
3177 - But initially, and as long as no exception is caught in a given
3178 frame, sys.exc_ZZZ will hold the last exception caught in the
3179 previous frame (or the frame before that, etc.).
3180
3181 The first bullet fixed the bug in the above example. The second
3182 bullet was for backwards compatibility: it was (and is) common to
3183 have a function that is called when an exception is caught, and to
3184 have that function access the caught exception via sys.exc_ZZZ.
3185 (Example: traceback.print_exc()).
3186
3187 At the same time I fixed the problem that sys.exc_ZZZ weren't
3188 thread-safe, by introducing sys.exc_info() which gets it from tstate;
3189 but that's really a separate improvement.
3190
3191 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
3192 variables to what they were before the current frame was called. The
3193 set_exc_info() function saves them on the frame so that
3194 reset_exc_info() can restore them. The invariant is that
3195 frame->f_exc_ZZZ is NULL iff the current frame never caught an
3196 exception (where "catching" an exception applies only to successful
3197 except clauses); and if the current frame ever caught an exception,
3198 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
3199 at the start of the current frame.
3200
3201*/
3202
Fredrik Lundh7a830892006-05-27 10:39:48 +00003203static void
Guido van Rossumac7be682001-01-17 15:42:30 +00003204set_exc_info(PyThreadState *tstate,
3205 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00003206{
Tim Peters7df5e7f2006-05-26 23:14:37 +00003207 PyFrameObject *frame = tstate->frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003208 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00003209
Tim Peters7df5e7f2006-05-26 23:14:37 +00003210 assert(type != NULL);
3211 assert(frame != NULL);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003212 if (frame->f_exc_type == NULL) {
Tim Peters7df5e7f2006-05-26 23:14:37 +00003213 assert(frame->f_exc_value == NULL);
3214 assert(frame->f_exc_traceback == NULL);
3215 /* This frame didn't catch an exception before. */
3216 /* Save previous exception of this thread in this frame. */
Guido van Rossuma027efa1997-05-05 20:56:21 +00003217 if (tstate->exc_type == NULL) {
Tim Peters7df5e7f2006-05-26 23:14:37 +00003218 /* XXX Why is this set to Py_None? */
Guido van Rossuma027efa1997-05-05 20:56:21 +00003219 Py_INCREF(Py_None);
3220 tstate->exc_type = Py_None;
3221 }
Tim Peters7df5e7f2006-05-26 23:14:37 +00003222 Py_INCREF(tstate->exc_type);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003223 Py_XINCREF(tstate->exc_value);
3224 Py_XINCREF(tstate->exc_traceback);
3225 frame->f_exc_type = tstate->exc_type;
3226 frame->f_exc_value = tstate->exc_value;
3227 frame->f_exc_traceback = tstate->exc_traceback;
3228 }
Tim Peters7df5e7f2006-05-26 23:14:37 +00003229 /* Set new exception for this thread. */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003230 tmp_type = tstate->exc_type;
3231 tmp_value = tstate->exc_value;
3232 tmp_tb = tstate->exc_traceback;
Tim Peters7df5e7f2006-05-26 23:14:37 +00003233 Py_INCREF(type);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003234 Py_XINCREF(value);
3235 Py_XINCREF(tb);
3236 tstate->exc_type = type;
3237 tstate->exc_value = value;
3238 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003239 Py_XDECREF(tmp_type);
3240 Py_XDECREF(tmp_value);
3241 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003242 /* For b/w compatibility */
3243 PySys_SetObject("exc_type", type);
3244 PySys_SetObject("exc_value", value);
3245 PySys_SetObject("exc_traceback", tb);
3246}
3247
Fredrik Lundh7a830892006-05-27 10:39:48 +00003248static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003249reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00003250{
3251 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003252 PyObject *tmp_type, *tmp_value, *tmp_tb;
Tim Peters7df5e7f2006-05-26 23:14:37 +00003253
3254 /* It's a precondition that the thread state's frame caught an
3255 * exception -- verify in a debug build.
3256 */
3257 assert(tstate != NULL);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003258 frame = tstate->frame;
Tim Peters7df5e7f2006-05-26 23:14:37 +00003259 assert(frame != NULL);
3260 assert(frame->f_exc_type != NULL);
3261
3262 /* Copy the frame's exception info back to the thread state. */
3263 tmp_type = tstate->exc_type;
3264 tmp_value = tstate->exc_value;
3265 tmp_tb = tstate->exc_traceback;
3266 Py_INCREF(frame->f_exc_type);
3267 Py_XINCREF(frame->f_exc_value);
3268 Py_XINCREF(frame->f_exc_traceback);
3269 tstate->exc_type = frame->f_exc_type;
3270 tstate->exc_value = frame->f_exc_value;
3271 tstate->exc_traceback = frame->f_exc_traceback;
3272 Py_XDECREF(tmp_type);
3273 Py_XDECREF(tmp_value);
3274 Py_XDECREF(tmp_tb);
3275
3276 /* For b/w compatibility */
3277 PySys_SetObject("exc_type", frame->f_exc_type);
3278 PySys_SetObject("exc_value", frame->f_exc_value);
3279 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
3280
3281 /* Clear the frame's exception info. */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003282 tmp_type = frame->f_exc_type;
3283 tmp_value = frame->f_exc_value;
3284 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003285 frame->f_exc_type = NULL;
3286 frame->f_exc_value = NULL;
3287 frame->f_exc_traceback = NULL;
Tim Peters7df5e7f2006-05-26 23:14:37 +00003288 Py_DECREF(tmp_type);
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003289 Py_XDECREF(tmp_value);
3290 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003291}
3292
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003293/* Logic for the raise statement (too complicated for inlining).
3294 This *consumes* a reference count to each of its arguments. */
Fredrik Lundh7a830892006-05-27 10:39:48 +00003295static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003296do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003297{
Guido van Rossumd295f121998-04-09 21:39:57 +00003298 if (type == NULL) {
3299 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003300 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumd295f121998-04-09 21:39:57 +00003301 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
3302 value = tstate->exc_value;
3303 tb = tstate->exc_traceback;
3304 Py_XINCREF(type);
3305 Py_XINCREF(value);
3306 Py_XINCREF(tb);
3307 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003308
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003309 /* We support the following forms of raise:
3310 raise <class>, <classinstance>
3311 raise <class>, <argument tuple>
3312 raise <class>, None
3313 raise <class>, <argument>
3314 raise <classinstance>, None
3315 raise <string>, <object>
3316 raise <string>, None
3317
3318 An omitted second argument is the same as None.
3319
3320 In addition, raise <tuple>, <anything> is the same as
3321 raising the tuple's first item (and it better have one!);
3322 this rule is applied recursively.
3323
3324 Finally, an optional third argument can be supplied, which
3325 gives the traceback to be substituted (useful when
3326 re-raising an exception after examining it). */
3327
3328 /* First, check the traceback argument, replacing None with
3329 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00003330 if (tb == Py_None) {
3331 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003332 tb = NULL;
3333 }
3334 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003335 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003336 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003337 goto raise_error;
3338 }
3339
3340 /* Next, replace a missing value with None */
3341 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003342 value = Py_None;
3343 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003344 }
3345
3346 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00003347 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
3348 PyObject *tmp = type;
3349 type = PyTuple_GET_ITEM(type, 0);
3350 Py_INCREF(type);
3351 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003352 }
3353
Brett Cannon129bd522007-01-30 21:34:36 +00003354 if (PyExceptionClass_Check(type))
Barry Warsaw4249f541997-08-22 21:26:19 +00003355 PyErr_NormalizeException(&type, &value, &tb);
3356
Brett Cannonbf364092006-03-01 04:25:17 +00003357 else if (PyExceptionInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003358 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00003359 if (value != Py_None) {
3360 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003361 "instance exception may not have a separate value");
3362 goto raise_error;
3363 }
3364 else {
3365 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00003366 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003367 value = type;
Brett Cannonbf364092006-03-01 04:25:17 +00003368 type = PyExceptionInstance_Class(type);
Guido van Rossumb209a111997-04-29 18:18:01 +00003369 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003370 }
3371 }
3372 else {
3373 /* Not something you can raise. You get an exception
3374 anyway, just not what you specified :-) */
Jeremy Hylton960d9482001-04-27 02:25:33 +00003375 PyErr_Format(PyExc_TypeError,
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00003376 "exceptions must be classes or instances, not %s",
3377 type->ob_type->tp_name);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003378 goto raise_error;
3379 }
Guido van Rossum504153d2008-03-18 04:26:48 +00003380
3381 assert(PyExceptionClass_Check(type));
3382 if (Py_Py3kWarningFlag && PyClass_Check(type)) {
Benjamin Peterson9f4f4812008-04-27 03:01:45 +00003383 if (PyErr_WarnEx(PyExc_DeprecationWarning,
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00003384 "exceptions must derive from BaseException "
3385 "in 3.x", 1) < 0)
Guido van Rossum504153d2008-03-18 04:26:48 +00003386 goto raise_error;
3387 }
3388
Guido van Rossumb209a111997-04-29 18:18:01 +00003389 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003390 if (tb == NULL)
3391 return WHY_EXCEPTION;
3392 else
3393 return WHY_RERAISE;
3394 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00003395 Py_XDECREF(value);
3396 Py_XDECREF(type);
3397 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003398 return WHY_EXCEPTION;
3399}
3400
Tim Petersd6d010b2001-06-21 02:49:55 +00003401/* Iterate v argcnt times and store the results on the stack (via decreasing
3402 sp). Return 1 for success, 0 if error. */
3403
Fredrik Lundh7a830892006-05-27 10:39:48 +00003404static int
Tim Petersd6d010b2001-06-21 02:49:55 +00003405unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003406{
Tim Petersd6d010b2001-06-21 02:49:55 +00003407 int i = 0;
3408 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00003409 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00003410
Tim Petersd6d010b2001-06-21 02:49:55 +00003411 assert(v != NULL);
3412
3413 it = PyObject_GetIter(v);
3414 if (it == NULL)
3415 goto Error;
3416
3417 for (; i < argcnt; i++) {
3418 w = PyIter_Next(it);
3419 if (w == NULL) {
3420 /* Iterator done, via error or exhaustion. */
3421 if (!PyErr_Occurred()) {
3422 PyErr_Format(PyExc_ValueError,
3423 "need more than %d value%s to unpack",
3424 i, i == 1 ? "" : "s");
3425 }
3426 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003427 }
3428 *--sp = w;
3429 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003430
3431 /* We better have exhausted the iterator now. */
3432 w = PyIter_Next(it);
3433 if (w == NULL) {
3434 if (PyErr_Occurred())
3435 goto Error;
3436 Py_DECREF(it);
3437 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003438 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00003439 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00003440 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00003441 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00003442Error:
Barry Warsaw91010551997-08-25 22:30:51 +00003443 for (; i > 0; i--, sp++)
3444 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00003445 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00003446 return 0;
3447}
3448
3449
Guido van Rossum96a42c81992-01-12 02:29:51 +00003450#ifdef LLTRACE
Fredrik Lundh7a830892006-05-27 10:39:48 +00003451static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003452prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003453{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003454 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003455 if (PyObject_Print(v, stdout, 0) != 0)
3456 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003457 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003458 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003459}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003460#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003461
Fredrik Lundh7a830892006-05-27 10:39:48 +00003462static void
Fred Drake5755ce62001-06-27 19:19:46 +00003463call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003464{
Guido van Rossumb209a111997-04-29 18:18:01 +00003465 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003466 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003467 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003468 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003469 value = Py_None;
3470 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003471 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003472 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003473 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003474 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003475 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003476 }
Fred Drake5755ce62001-06-27 19:19:46 +00003477 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003478 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003479 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003480 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003481 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003482 Py_XDECREF(type);
3483 Py_XDECREF(value);
3484 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003485 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003486}
3487
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +00003488static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003489call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003490 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003491{
3492 PyObject *type, *value, *traceback;
3493 int err;
3494 PyErr_Fetch(&type, &value, &traceback);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003495 err = call_trace(func, obj, frame, what, arg);
Fred Drake4ec5d562001-10-04 19:26:43 +00003496 if (err == 0)
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +00003497 {
Fred Drake4ec5d562001-10-04 19:26:43 +00003498 PyErr_Restore(type, value, traceback);
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +00003499 return 0;
3500 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003501 else {
3502 Py_XDECREF(type);
3503 Py_XDECREF(value);
3504 Py_XDECREF(traceback);
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +00003505 return -1;
Fred Drake4ec5d562001-10-04 19:26:43 +00003506 }
3507}
3508
Fredrik Lundh7a830892006-05-27 10:39:48 +00003509static int
Fred Drake5755ce62001-06-27 19:19:46 +00003510call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3511 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003512{
Fred Drake5755ce62001-06-27 19:19:46 +00003513 register PyThreadState *tstate = frame->f_tstate;
3514 int result;
3515 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003516 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003517 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003518 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003519 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003520 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3521 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003522 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003523 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003524}
3525
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003526PyObject *
3527_PyEval_CallTracing(PyObject *func, PyObject *args)
3528{
3529 PyFrameObject *frame = PyEval_GetFrame();
3530 PyThreadState *tstate = frame->f_tstate;
3531 int save_tracing = tstate->tracing;
3532 int save_use_tracing = tstate->use_tracing;
3533 PyObject *result;
3534
3535 tstate->tracing = 0;
3536 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3537 || (tstate->c_profilefunc != NULL));
3538 result = PyObject_Call(func, args, NULL);
3539 tstate->tracing = save_tracing;
3540 tstate->use_tracing = save_use_tracing;
3541 return result;
3542}
3543
Fredrik Lundh7a830892006-05-27 10:39:48 +00003544static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003545maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003546 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3547 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003548{
Michael W. Hudson006c7522002-11-08 13:08:46 +00003549 int result = 0;
3550
Jeremy Hyltona4ebc132006-04-18 14:47:00 +00003551 /* If the last instruction executed isn't in the current
3552 instruction window, reset the window. If the last
3553 instruction happens to fall at the start of a line or if it
3554 represents a jump backwards, call the trace function.
3555 */
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003556 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Thomas Woutersae406c62007-09-19 17:27:43 +00003557 int line;
3558 PyAddrPair bounds;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003559
Thomas Woutersae406c62007-09-19 17:27:43 +00003560 line = PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3561 &bounds);
3562 if (line >= 0) {
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003563 frame->f_lineno = line;
Tim Peters8a5c3c72004-04-05 19:36:21 +00003564 result = call_trace(func, obj, frame,
Michael W. Hudson006c7522002-11-08 13:08:46 +00003565 PyTrace_LINE, Py_None);
Thomas Woutersae406c62007-09-19 17:27:43 +00003566 }
3567 *instr_lb = bounds.ap_lower;
3568 *instr_ub = bounds.ap_upper;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003569 }
Armin Rigobf57a142004-03-22 19:24:58 +00003570 else if (frame->f_lasti <= *instr_prev) {
Jeremy Hyltona4ebc132006-04-18 14:47:00 +00003571 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
Armin Rigobf57a142004-03-22 19:24:58 +00003572 }
3573 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003574 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003575}
3576
Fred Drake5755ce62001-06-27 19:19:46 +00003577void
3578PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003579{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003580 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003581 PyObject *temp = tstate->c_profileobj;
3582 Py_XINCREF(arg);
3583 tstate->c_profilefunc = NULL;
3584 tstate->c_profileobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003585 /* Must make sure that tracing is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003586 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003587 Py_XDECREF(temp);
3588 tstate->c_profilefunc = func;
3589 tstate->c_profileobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003590 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003591 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003592}
3593
3594void
3595PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3596{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003597 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003598 PyObject *temp = tstate->c_traceobj;
Jeffrey Yasskinfd8a1ec2008-12-03 06:46:45 +00003599 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003600 Py_XINCREF(arg);
3601 tstate->c_tracefunc = NULL;
3602 tstate->c_traceobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003603 /* Must make sure that profiling is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003604 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003605 Py_XDECREF(temp);
3606 tstate->c_tracefunc = func;
3607 tstate->c_traceobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003608 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003609 tstate->use_tracing = ((func != NULL)
3610 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003611}
3612
Guido van Rossumb209a111997-04-29 18:18:01 +00003613PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003614PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003615{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003616 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003617 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003618 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003619 else
3620 return current_frame->f_builtins;
3621}
3622
Guido van Rossumb209a111997-04-29 18:18:01 +00003623PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003624PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003625{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003626 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003627 if (current_frame == NULL)
3628 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003629 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003630 return current_frame->f_locals;
3631}
3632
Guido van Rossumb209a111997-04-29 18:18:01 +00003633PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003634PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003635{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003636 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003637 if (current_frame == NULL)
3638 return NULL;
3639 else
3640 return current_frame->f_globals;
3641}
3642
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003643PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003644PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003645{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003646 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003647 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003648}
3649
Guido van Rossum6135a871995-01-09 17:53:26 +00003650int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003651PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003652{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003653 PyFrameObject *current_frame = PyEval_GetFrame();
Neal Norwitzb9845e72006-06-12 02:11:18 +00003654 return current_frame == NULL ? 0 : PyFrame_IsRestricted(current_frame);
Guido van Rossum6135a871995-01-09 17:53:26 +00003655}
3656
Guido van Rossumbe270261997-05-22 22:26:18 +00003657int
Tim Peters5ba58662001-07-16 02:29:45 +00003658PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003659{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003660 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003661 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003662
3663 if (current_frame != NULL) {
3664 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003665 const int compilerflags = codeflags & PyCF_MASK;
3666 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003667 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003668 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003669 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003670#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003671 if (codeflags & CO_GENERATOR_ALLOWED) {
3672 result = 1;
3673 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3674 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003675#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003676 }
3677 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003678}
3679
3680int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003681Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003682{
Guido van Rossumb209a111997-04-29 18:18:01 +00003683 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003684 if (f == NULL)
3685 return 0;
3686 if (!PyFile_SoftSpace(f, 0))
3687 return 0;
3688 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003689}
3690
Guido van Rossum3f5da241990-12-20 15:06:42 +00003691
Guido van Rossum681d79a1995-07-18 14:51:37 +00003692/* External interface to call any callable object.
3693 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003694
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003695#undef PyEval_CallObject
3696/* for backward compatibility: export this interface */
3697
Guido van Rossumb209a111997-04-29 18:18:01 +00003698PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003699PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003700{
Guido van Rossumb209a111997-04-29 18:18:01 +00003701 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003702}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003703#define PyEval_CallObject(func,arg) \
3704 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003705
Guido van Rossumb209a111997-04-29 18:18:01 +00003706PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003707PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003708{
Jeremy Hylton52820442001-01-03 23:52:36 +00003709 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003710
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003711 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003712 arg = PyTuple_New(0);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003713 if (arg == NULL)
3714 return NULL;
3715 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003716 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003717 PyErr_SetString(PyExc_TypeError,
3718 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003719 return NULL;
3720 }
3721 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003722 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003723
Guido van Rossumb209a111997-04-29 18:18:01 +00003724 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003725 PyErr_SetString(PyExc_TypeError,
3726 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003727 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003728 return NULL;
3729 }
3730
Tim Peters6d6c1a32001-08-02 04:15:00 +00003731 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003732 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003733 return result;
3734}
3735
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003736const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003737PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003738{
3739 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003740 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003741 else if (PyFunction_Check(func))
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003742 return PyString_AsString(((PyFunctionObject*)func)->func_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00003743 else if (PyCFunction_Check(func))
3744 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3745 else if (PyClass_Check(func))
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003746 return PyString_AsString(((PyClassObject*)func)->cl_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00003747 else if (PyInstance_Check(func)) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003748 return PyString_AsString(
Jeremy Hylton512a2372001-04-11 13:52:29 +00003749 ((PyInstanceObject*)func)->in_class->cl_name);
3750 } else {
3751 return func->ob_type->tp_name;
3752 }
3753}
3754
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003755const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003756PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003757{
3758 if (PyMethod_Check(func))
3759 return "()";
3760 else if (PyFunction_Check(func))
3761 return "()";
3762 else if (PyCFunction_Check(func))
3763 return "()";
3764 else if (PyClass_Check(func))
3765 return " constructor";
3766 else if (PyInstance_Check(func)) {
3767 return " instance";
3768 } else {
3769 return " object";
3770 }
3771}
3772
Fredrik Lundh7a830892006-05-27 10:39:48 +00003773static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003774err_args(PyObject *func, int flags, int nargs)
3775{
3776 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003777 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003778 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003779 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003780 nargs);
3781 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003782 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003783 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003784 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003785 nargs);
3786}
3787
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003788#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003789if (tstate->use_tracing && tstate->c_profilefunc) { \
3790 if (call_trace(tstate->c_profilefunc, \
3791 tstate->c_profileobj, \
3792 tstate->frame, PyTrace_C_CALL, \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003793 func)) { \
3794 x = NULL; \
3795 } \
3796 else { \
3797 x = call; \
3798 if (tstate->c_profilefunc != NULL) { \
3799 if (x == NULL) { \
3800 call_trace_protected(tstate->c_profilefunc, \
3801 tstate->c_profileobj, \
3802 tstate->frame, PyTrace_C_EXCEPTION, \
3803 func); \
3804 /* XXX should pass (type, value, tb) */ \
3805 } else { \
3806 if (call_trace(tstate->c_profilefunc, \
3807 tstate->c_profileobj, \
3808 tstate->frame, PyTrace_C_RETURN, \
3809 func)) { \
3810 Py_DECREF(x); \
3811 x = NULL; \
3812 } \
3813 } \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003814 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003815 } \
3816} else { \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003817 x = call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003818 }
3819
Fredrik Lundh7a830892006-05-27 10:39:48 +00003820static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003821call_function(PyObject ***pp_stack, int oparg
3822#ifdef WITH_TSC
3823 , uint64* pintr0, uint64* pintr1
3824#endif
3825 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003826{
3827 int na = oparg & 0xff;
3828 int nk = (oparg>>8) & 0xff;
3829 int n = na + 2 * nk;
3830 PyObject **pfunc = (*pp_stack) - n - 1;
3831 PyObject *func = *pfunc;
3832 PyObject *x, *w;
3833
Jeremy Hylton985eba52003-02-05 23:13:00 +00003834 /* Always dispatch PyCFunction first, because these are
3835 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003836 */
3837 if (PyCFunction_Check(func) && nk == 0) {
3838 int flags = PyCFunction_GET_FLAGS(func);
Nicholas Bastind858a772004-06-25 23:31:06 +00003839 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003840
3841 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003842 if (flags & (METH_NOARGS | METH_O)) {
3843 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3844 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003845 if (flags & METH_NOARGS && na == 0) {
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003846 C_TRACE(x, (*meth)(self,NULL));
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003847 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003848 else if (flags & METH_O && na == 1) {
3849 PyObject *arg = EXT_POP(*pp_stack);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003850 C_TRACE(x, (*meth)(self,arg));
Jeremy Hylton192690e2002-08-16 18:36:11 +00003851 Py_DECREF(arg);
3852 }
3853 else {
3854 err_args(func, flags, na);
3855 x = NULL;
3856 }
3857 }
3858 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003859 PyObject *callargs;
3860 callargs = load_args(pp_stack, na);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003861 READ_TIMESTAMP(*pintr0);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003862 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003863 READ_TIMESTAMP(*pintr1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003864 Py_XDECREF(callargs);
3865 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003866 } else {
3867 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3868 /* optimize access to bound methods */
3869 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003870 PCALL(PCALL_METHOD);
3871 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003872 Py_INCREF(self);
3873 func = PyMethod_GET_FUNCTION(func);
3874 Py_INCREF(func);
3875 Py_DECREF(*pfunc);
3876 *pfunc = self;
3877 na++;
3878 n++;
3879 } else
3880 Py_INCREF(func);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003881 READ_TIMESTAMP(*pintr0);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003882 if (PyFunction_Check(func))
3883 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003884 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003885 x = do_call(func, pp_stack, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003886 READ_TIMESTAMP(*pintr1);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003887 Py_DECREF(func);
3888 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003889
Armin Rigod34fa522006-03-28 19:10:40 +00003890 /* Clear the stack of the function object. Also removes
3891 the arguments in case they weren't consumed already
3892 (fast_function() and err_args() leave them on the stack).
Thomas Wouters7f597322006-03-01 05:32:33 +00003893 */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003894 while ((*pp_stack) > pfunc) {
3895 w = EXT_POP(*pp_stack);
3896 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003897 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003898 }
3899 return x;
3900}
3901
Jeremy Hylton192690e2002-08-16 18:36:11 +00003902/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003903 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003904 For the simplest case -- a function that takes only positional
3905 arguments and is called with only positional arguments -- it
3906 inlines the most primitive frame setup code from
3907 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3908 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003909*/
3910
Fredrik Lundh7a830892006-05-27 10:39:48 +00003911static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003912fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003913{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003914 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003915 PyObject *globals = PyFunction_GET_GLOBALS(func);
3916 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3917 PyObject **d = NULL;
3918 int nd = 0;
3919
Jeremy Hylton985eba52003-02-05 23:13:00 +00003920 PCALL(PCALL_FUNCTION);
3921 PCALL(PCALL_FAST_FUNCTION);
Raymond Hettinger40174c32003-05-31 07:04:16 +00003922 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003923 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3924 PyFrameObject *f;
3925 PyObject *retval = NULL;
3926 PyThreadState *tstate = PyThreadState_GET();
3927 PyObject **fastlocals, **stack;
3928 int i;
3929
3930 PCALL(PCALL_FASTER_FUNCTION);
3931 assert(globals != NULL);
3932 /* XXX Perhaps we should create a specialized
3933 PyFrame_New() that doesn't take locals, but does
3934 take builtins without sanity checking them.
3935 */
Neal Norwitzdf6a6492006-08-13 18:10:10 +00003936 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003937 f = PyFrame_New(tstate, co, globals, NULL);
3938 if (f == NULL)
3939 return NULL;
3940
3941 fastlocals = f->f_localsplus;
3942 stack = (*pp_stack) - n;
3943
3944 for (i = 0; i < n; i++) {
3945 Py_INCREF(*stack);
3946 fastlocals[i] = *stack++;
3947 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003948 retval = PyEval_EvalFrameEx(f,0);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003949 ++tstate->recursion_depth;
3950 Py_DECREF(f);
3951 --tstate->recursion_depth;
3952 return retval;
3953 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003954 if (argdefs != NULL) {
3955 d = &PyTuple_GET_ITEM(argdefs, 0);
Christian Heimese93237d2007-12-19 02:37:44 +00003956 nd = Py_SIZE(argdefs);
Jeremy Hylton52820442001-01-03 23:52:36 +00003957 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003958 return PyEval_EvalCodeEx(co, globals,
3959 (PyObject *)NULL, (*pp_stack)-n, na,
3960 (*pp_stack)-2*nk, nk, d, nd,
3961 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003962}
3963
Fredrik Lundh7a830892006-05-27 10:39:48 +00003964static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003965update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3966 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003967{
3968 PyObject *kwdict = NULL;
3969 if (orig_kwdict == NULL)
3970 kwdict = PyDict_New();
3971 else {
3972 kwdict = PyDict_Copy(orig_kwdict);
3973 Py_DECREF(orig_kwdict);
3974 }
3975 if (kwdict == NULL)
3976 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003977 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003978 int err;
3979 PyObject *value = EXT_POP(*pp_stack);
3980 PyObject *key = EXT_POP(*pp_stack);
3981 if (PyDict_GetItem(kwdict, key) != NULL) {
Thomas Woutersae406c62007-09-19 17:27:43 +00003982 PyErr_Format(PyExc_TypeError,
3983 "%.200s%s got multiple values "
3984 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003985 PyEval_GetFuncName(func),
3986 PyEval_GetFuncDesc(func),
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003987 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003988 Py_DECREF(key);
3989 Py_DECREF(value);
3990 Py_DECREF(kwdict);
3991 return NULL;
3992 }
3993 err = PyDict_SetItem(kwdict, key, value);
3994 Py_DECREF(key);
3995 Py_DECREF(value);
3996 if (err) {
3997 Py_DECREF(kwdict);
3998 return NULL;
3999 }
4000 }
4001 return kwdict;
4002}
4003
Fredrik Lundh7a830892006-05-27 10:39:48 +00004004static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00004005update_star_args(int nstack, int nstar, PyObject *stararg,
4006 PyObject ***pp_stack)
4007{
4008 PyObject *callargs, *w;
4009
4010 callargs = PyTuple_New(nstack + nstar);
4011 if (callargs == NULL) {
4012 return NULL;
4013 }
4014 if (nstar) {
4015 int i;
4016 for (i = 0; i < nstar; i++) {
4017 PyObject *a = PyTuple_GET_ITEM(stararg, i);
4018 Py_INCREF(a);
4019 PyTuple_SET_ITEM(callargs, nstack + i, a);
4020 }
4021 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00004022 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00004023 w = EXT_POP(*pp_stack);
4024 PyTuple_SET_ITEM(callargs, nstack, w);
4025 }
4026 return callargs;
4027}
4028
Fredrik Lundh7a830892006-05-27 10:39:48 +00004029static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00004030load_args(PyObject ***pp_stack, int na)
4031{
4032 PyObject *args = PyTuple_New(na);
4033 PyObject *w;
4034
4035 if (args == NULL)
4036 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00004037 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00004038 w = EXT_POP(*pp_stack);
4039 PyTuple_SET_ITEM(args, na, w);
4040 }
4041 return args;
4042}
4043
Fredrik Lundh7a830892006-05-27 10:39:48 +00004044static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00004045do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4046{
4047 PyObject *callargs = NULL;
4048 PyObject *kwdict = NULL;
4049 PyObject *result = NULL;
4050
4051 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00004052 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00004053 if (kwdict == NULL)
4054 goto call_fail;
4055 }
4056 callargs = load_args(pp_stack, na);
4057 if (callargs == NULL)
4058 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004059#ifdef CALL_PROFILE
4060 /* At this point, we have to look at the type of func to
4061 update the call stats properly. Do it here so as to avoid
4062 exposing the call stats machinery outside ceval.c
4063 */
4064 if (PyFunction_Check(func))
4065 PCALL(PCALL_FUNCTION);
4066 else if (PyMethod_Check(func))
4067 PCALL(PCALL_METHOD);
4068 else if (PyType_Check(func))
4069 PCALL(PCALL_TYPE);
4070 else
4071 PCALL(PCALL_OTHER);
4072#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00004073 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00004074 call_fail:
4075 Py_XDECREF(callargs);
4076 Py_XDECREF(kwdict);
4077 return result;
4078}
4079
Fredrik Lundh7a830892006-05-27 10:39:48 +00004080static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00004081ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4082{
4083 int nstar = 0;
4084 PyObject *callargs = NULL;
4085 PyObject *stararg = NULL;
4086 PyObject *kwdict = NULL;
4087 PyObject *result = NULL;
4088
4089 if (flags & CALL_FLAG_KW) {
4090 kwdict = EXT_POP(*pp_stack);
Georg Brandl2134e752007-05-21 20:34:16 +00004091 if (!PyDict_Check(kwdict)) {
4092 PyObject *d;
4093 d = PyDict_New();
4094 if (d == NULL)
4095 goto ext_call_fail;
4096 if (PyDict_Update(d, kwdict) != 0) {
4097 Py_DECREF(d);
4098 /* PyDict_Update raises attribute
4099 * error (percolated from an attempt
4100 * to get 'keys' attribute) instead of
4101 * a type error if its second argument
4102 * is not a mapping.
4103 */
4104 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4105 PyErr_Format(PyExc_TypeError,
4106 "%.200s%.200s argument after ** "
4107 "must be a mapping, not %.200s",
4108 PyEval_GetFuncName(func),
4109 PyEval_GetFuncDesc(func),
4110 kwdict->ob_type->tp_name);
4111 }
4112 goto ext_call_fail;
4113 }
4114 Py_DECREF(kwdict);
4115 kwdict = d;
Jeremy Hylton52820442001-01-03 23:52:36 +00004116 }
4117 }
4118 if (flags & CALL_FLAG_VAR) {
4119 stararg = EXT_POP(*pp_stack);
4120 if (!PyTuple_Check(stararg)) {
4121 PyObject *t = NULL;
4122 t = PySequence_Tuple(stararg);
4123 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00004124 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4125 PyErr_Format(PyExc_TypeError,
Georg Brandl2134e752007-05-21 20:34:16 +00004126 "%.200s%.200s argument after * "
4127 "must be a sequence, not %200s",
Tim Peters6d6c1a32001-08-02 04:15:00 +00004128 PyEval_GetFuncName(func),
Georg Brandl2134e752007-05-21 20:34:16 +00004129 PyEval_GetFuncDesc(func),
4130 stararg->ob_type->tp_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00004131 }
Jeremy Hylton52820442001-01-03 23:52:36 +00004132 goto ext_call_fail;
4133 }
4134 Py_DECREF(stararg);
4135 stararg = t;
4136 }
4137 nstar = PyTuple_GET_SIZE(stararg);
4138 }
4139 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00004140 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00004141 if (kwdict == NULL)
4142 goto ext_call_fail;
4143 }
4144 callargs = update_star_args(na, nstar, stararg, pp_stack);
4145 if (callargs == NULL)
4146 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004147#ifdef CALL_PROFILE
4148 /* At this point, we have to look at the type of func to
4149 update the call stats properly. Do it here so as to avoid
4150 exposing the call stats machinery outside ceval.c
4151 */
4152 if (PyFunction_Check(func))
4153 PCALL(PCALL_FUNCTION);
4154 else if (PyMethod_Check(func))
4155 PCALL(PCALL_METHOD);
4156 else if (PyType_Check(func))
4157 PCALL(PCALL_TYPE);
4158 else
4159 PCALL(PCALL_OTHER);
4160#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00004161 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersae406c62007-09-19 17:27:43 +00004162ext_call_fail:
Jeremy Hylton52820442001-01-03 23:52:36 +00004163 Py_XDECREF(callargs);
4164 Py_XDECREF(kwdict);
4165 Py_XDECREF(stararg);
4166 return result;
4167}
4168
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004169/* Extract a slice index from a PyInt or PyLong or an object with the
4170 nb_index slot defined, and store in *pi.
4171 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4172 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 +00004173 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004174*/
Tim Petersb5196382001-12-16 19:44:20 +00004175/* Note: If v is NULL, return success without storing into *pi. This
4176 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4177 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004178*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004179int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004180_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004181{
Tim Petersb5196382001-12-16 19:44:20 +00004182 if (v != NULL) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004183 Py_ssize_t x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00004184 if (PyInt_Check(v)) {
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00004185 /* XXX(nnorwitz): I think PyInt_AS_LONG is correct,
4186 however, it looks like it should be AsSsize_t.
4187 There should be a comment here explaining why.
4188 */
4189 x = PyInt_AS_LONG(v);
Tim Peters7df5e7f2006-05-26 23:14:37 +00004190 }
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00004191 else if (PyIndex_Check(v)) {
4192 x = PyNumber_AsSsize_t(v, NULL);
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004193 if (x == -1 && PyErr_Occurred())
4194 return 0;
4195 }
4196 else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00004197 PyErr_SetString(PyExc_TypeError,
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004198 "slice indices must be integers or "
4199 "None or have an __index__ method");
Guido van Rossum20c6add2000-05-08 14:06:50 +00004200 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004201 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00004202 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004203 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00004204 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004205}
4206
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004207#undef ISINDEX
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00004208#define ISINDEX(x) ((x) == NULL || \
4209 PyInt_Check(x) || PyLong_Check(x) || PyIndex_Check(x))
Guido van Rossum50d756e2001-08-18 17:43:36 +00004210
Fredrik Lundh7a830892006-05-27 10:39:48 +00004211static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004212apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004213{
Guido van Rossum50d756e2001-08-18 17:43:36 +00004214 PyTypeObject *tp = u->ob_type;
4215 PySequenceMethods *sq = tp->tp_as_sequence;
4216
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004217 if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004218 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
Guido van Rossum50d756e2001-08-18 17:43:36 +00004219 if (!_PyEval_SliceIndex(v, &ilow))
4220 return NULL;
4221 if (!_PyEval_SliceIndex(w, &ihigh))
4222 return NULL;
4223 return PySequence_GetSlice(u, ilow, ihigh);
4224 }
4225 else {
4226 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00004227 if (slice != NULL) {
4228 PyObject *res = PyObject_GetItem(u, slice);
4229 Py_DECREF(slice);
4230 return res;
4231 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00004232 else
4233 return NULL;
4234 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004235}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004236
Fredrik Lundh7a830892006-05-27 10:39:48 +00004237static int
Guido van Rossumac7be682001-01-17 15:42:30 +00004238assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
4239 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004240{
Guido van Rossum50d756e2001-08-18 17:43:36 +00004241 PyTypeObject *tp = u->ob_type;
4242 PySequenceMethods *sq = tp->tp_as_sequence;
4243
Georg Brandl0fca97a2007-03-05 22:28:08 +00004244 if (sq && sq->sq_ass_slice && ISINDEX(v) && ISINDEX(w)) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004245 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
Guido van Rossum50d756e2001-08-18 17:43:36 +00004246 if (!_PyEval_SliceIndex(v, &ilow))
4247 return -1;
4248 if (!_PyEval_SliceIndex(w, &ihigh))
4249 return -1;
4250 if (x == NULL)
4251 return PySequence_DelSlice(u, ilow, ihigh);
4252 else
4253 return PySequence_SetSlice(u, ilow, ihigh, x);
4254 }
4255 else {
4256 PyObject *slice = PySlice_New(v, w, NULL);
4257 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00004258 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00004259 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00004260 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00004261 else
Guido van Rossum354797c2001-12-03 19:45:06 +00004262 res = PyObject_DelItem(u, slice);
4263 Py_DECREF(slice);
4264 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00004265 }
4266 else
4267 return -1;
4268 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004269}
4270
Guido van Rossum04edb522008-03-18 02:49:46 +00004271#define Py3kExceptionClass_Check(x) \
4272 (PyType_Check((x)) && \
4273 PyType_FastSubclass((PyTypeObject*)(x), Py_TPFLAGS_BASE_EXC_SUBCLASS))
4274
4275#define CANNOT_CATCH_MSG "catching classes that don't inherit from " \
Georg Brandld5b635f2008-03-25 08:29:14 +00004276 "BaseException is not allowed in 3.x"
Guido van Rossum04edb522008-03-18 02:49:46 +00004277
Fredrik Lundh7a830892006-05-27 10:39:48 +00004278static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004279cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004280{
Guido van Rossumac7be682001-01-17 15:42:30 +00004281 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004282 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00004283 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00004284 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004285 break;
4286 case PyCmp_IS_NOT:
4287 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004288 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004289 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004290 res = PySequence_Contains(w, v);
4291 if (res < 0)
4292 return NULL;
4293 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004294 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00004295 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004296 if (res < 0)
4297 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004298 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004299 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004300 case PyCmp_EXC_MATCH:
Brett Cannon129bd522007-01-30 21:34:36 +00004301 if (PyTuple_Check(w)) {
4302 Py_ssize_t i, length;
4303 length = PyTuple_Size(w);
4304 for (i = 0; i < length; i += 1) {
4305 PyObject *exc = PyTuple_GET_ITEM(w, i);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004306 if (PyString_Check(exc)) {
Brett Cannon129bd522007-01-30 21:34:36 +00004307 int ret_val;
4308 ret_val = PyErr_WarnEx(
Thomas Wouterse2176022007-09-20 17:35:10 +00004309 PyExc_DeprecationWarning,
4310 "catching of string "
4311 "exceptions is deprecated", 1);
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00004312 if (ret_val < 0)
Brett Cannon129bd522007-01-30 21:34:36 +00004313 return NULL;
4314 }
Guido van Rossum20bda582008-03-18 03:15:05 +00004315 else if (Py_Py3kWarningFlag &&
4316 !PyTuple_Check(exc) &&
4317 !Py3kExceptionClass_Check(exc))
Guido van Rossum04edb522008-03-18 02:49:46 +00004318 {
4319 int ret_val;
4320 ret_val = PyErr_WarnEx(
4321 PyExc_DeprecationWarning,
4322 CANNOT_CATCH_MSG, 1);
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00004323 if (ret_val < 0)
Guido van Rossum04edb522008-03-18 02:49:46 +00004324 return NULL;
4325 }
Brett Cannon129bd522007-01-30 21:34:36 +00004326 }
4327 }
4328 else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004329 if (PyString_Check(w)) {
Brett Cannon129bd522007-01-30 21:34:36 +00004330 int ret_val;
4331 ret_val = PyErr_WarnEx(
4332 PyExc_DeprecationWarning,
4333 "catching of string "
Thomas Wouterse2176022007-09-20 17:35:10 +00004334 "exceptions is deprecated", 1);
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00004335 if (ret_val < 0)
Brett Cannon129bd522007-01-30 21:34:36 +00004336 return NULL;
4337 }
Guido van Rossum20bda582008-03-18 03:15:05 +00004338 else if (Py_Py3kWarningFlag &&
4339 !PyTuple_Check(w) &&
4340 !Py3kExceptionClass_Check(w))
Guido van Rossum04edb522008-03-18 02:49:46 +00004341 {
4342 int ret_val;
4343 ret_val = PyErr_WarnEx(
4344 PyExc_DeprecationWarning,
4345 CANNOT_CATCH_MSG, 1);
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00004346 if (ret_val < 0)
Guido van Rossum04edb522008-03-18 02:49:46 +00004347 return NULL;
4348 }
Brett Cannon129bd522007-01-30 21:34:36 +00004349 }
Barry Warsaw4249f541997-08-22 21:26:19 +00004350 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004351 break;
4352 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00004353 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004354 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004355 v = res ? Py_True : Py_False;
4356 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004357 return v;
4358}
4359
Fredrik Lundh7a830892006-05-27 10:39:48 +00004360static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00004361import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004362{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004363 PyObject *x;
4364
4365 x = PyObject_GetAttr(v, name);
4366 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00004367 PyErr_Format(PyExc_ImportError,
4368 "cannot import name %.230s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004369 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004370 }
Thomas Wouters52152252000-08-17 22:55:00 +00004371 return x;
4372}
Guido van Rossumac7be682001-01-17 15:42:30 +00004373
Fredrik Lundh7a830892006-05-27 10:39:48 +00004374static int
Thomas Wouters52152252000-08-17 22:55:00 +00004375import_all_from(PyObject *locals, PyObject *v)
4376{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004377 PyObject *all = PyObject_GetAttrString(v, "__all__");
4378 PyObject *dict, *name, *value;
4379 int skip_leading_underscores = 0;
4380 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004381
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004382 if (all == NULL) {
4383 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4384 return -1; /* Unexpected error */
4385 PyErr_Clear();
4386 dict = PyObject_GetAttrString(v, "__dict__");
4387 if (dict == NULL) {
4388 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4389 return -1;
4390 PyErr_SetString(PyExc_ImportError,
4391 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00004392 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004393 }
4394 all = PyMapping_Keys(dict);
4395 Py_DECREF(dict);
4396 if (all == NULL)
4397 return -1;
4398 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004399 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004400
4401 for (pos = 0, err = 0; ; pos++) {
4402 name = PySequence_GetItem(all, pos);
4403 if (name == NULL) {
4404 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4405 err = -1;
4406 else
4407 PyErr_Clear();
4408 break;
4409 }
4410 if (skip_leading_underscores &&
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004411 PyString_Check(name) &&
4412 PyString_AS_STRING(name)[0] == '_')
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004413 {
4414 Py_DECREF(name);
4415 continue;
4416 }
4417 value = PyObject_GetAttr(v, name);
4418 if (value == NULL)
4419 err = -1;
Armin Rigo70370852006-11-29 21:59:22 +00004420 else if (PyDict_CheckExact(locals))
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004421 err = PyDict_SetItem(locals, name, value);
Armin Rigo70370852006-11-29 21:59:22 +00004422 else
4423 err = PyObject_SetItem(locals, name, value);
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004424 Py_DECREF(name);
4425 Py_XDECREF(value);
4426 if (err != 0)
4427 break;
4428 }
4429 Py_DECREF(all);
4430 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004431}
4432
Fredrik Lundh7a830892006-05-27 10:39:48 +00004433static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004434build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004435{
Guido van Rossum7851eea2001-09-12 19:19:18 +00004436 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004437
4438 if (PyDict_Check(methods))
4439 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00004440 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00004441 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004442 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4443 base = PyTuple_GET_ITEM(bases, 0);
4444 metaclass = PyObject_GetAttrString(base, "__class__");
4445 if (metaclass == NULL) {
4446 PyErr_Clear();
4447 metaclass = (PyObject *)base->ob_type;
4448 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00004449 }
4450 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004451 else {
4452 PyObject *g = PyEval_GetGlobals();
4453 if (g != NULL && PyDict_Check(g))
4454 metaclass = PyDict_GetItemString(g, "__metaclass__");
4455 if (metaclass == NULL)
4456 metaclass = (PyObject *) &PyClass_Type;
4457 Py_INCREF(metaclass);
4458 }
Jeremy Hylton7c1e3472007-02-26 16:14:51 +00004459 result = PyObject_CallFunctionObjArgs(metaclass, name, bases, methods,
Thomas Woutersae406c62007-09-19 17:27:43 +00004460 NULL);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004461 Py_DECREF(metaclass);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004462 if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
Tim Peters7df5e7f2006-05-26 23:14:37 +00004463 /* A type error here likely means that the user passed
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004464 in a base that was not a class (such the random module
4465 instead of the random.random type). Help them out with
Raymond Hettingercfc31922004-09-16 16:41:57 +00004466 by augmenting the error message with more information.*/
4467
4468 PyObject *ptype, *pvalue, *ptraceback;
4469
4470 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004471 if (PyString_Check(pvalue)) {
Raymond Hettingercfc31922004-09-16 16:41:57 +00004472 PyObject *newmsg;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004473 newmsg = PyString_FromFormat(
Jeremy Hylton7c1e3472007-02-26 16:14:51 +00004474 "Error when calling the metaclass bases\n"
Thomas Woutersae406c62007-09-19 17:27:43 +00004475 " %s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004476 PyString_AS_STRING(pvalue));
Raymond Hettingercfc31922004-09-16 16:41:57 +00004477 if (newmsg != NULL) {
4478 Py_DECREF(pvalue);
4479 pvalue = newmsg;
4480 }
4481 }
4482 PyErr_Restore(ptype, pvalue, ptraceback);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004483 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004484 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00004485}
4486
Fredrik Lundh7a830892006-05-27 10:39:48 +00004487static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004488exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
4489 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004490{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004491 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00004492 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004493 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004494
Guido van Rossumb209a111997-04-29 18:18:01 +00004495 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
4496 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004497 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00004498 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004499 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00004500 locals = PyTuple_GetItem(prog, 2);
4501 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004502 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004503 if (globals == Py_None) {
4504 globals = PyEval_GetGlobals();
4505 if (locals == Py_None) {
4506 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00004507 plain = 1;
4508 }
Neal Norwitzdf6a6492006-08-13 18:10:10 +00004509 if (!globals || !locals) {
4510 PyErr_SetString(PyExc_SystemError,
4511 "globals and locals cannot be NULL");
4512 return -1;
4513 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004514 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004515 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004516 locals = globals;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004517 if (!PyString_Check(prog) &&
Benjamin Peterson78821dd2009-01-25 17:15:10 +00004518#ifdef Py_USING_UNICODE
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004519 !PyUnicode_Check(prog) &&
Benjamin Peterson78821dd2009-01-25 17:15:10 +00004520#endif
Guido van Rossumb209a111997-04-29 18:18:01 +00004521 !PyCode_Check(prog) &&
4522 !PyFile_Check(prog)) {
4523 PyErr_SetString(PyExc_TypeError,
Guido van Rossumac7be682001-01-17 15:42:30 +00004524 "exec: arg 1 must be a string, file, or code object");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004525 return -1;
4526 }
Fred Drake661ea262000-10-24 19:57:45 +00004527 if (!PyDict_Check(globals)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004528 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00004529 "exec: arg 2 must be a dictionary or None");
4530 return -1;
4531 }
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004532 if (!PyMapping_Check(locals)) {
Fred Drake661ea262000-10-24 19:57:45 +00004533 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004534 "exec: arg 3 must be a mapping or None");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004535 return -1;
4536 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004537 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00004538 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00004539 if (PyCode_Check(prog)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +00004540 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4541 PyErr_SetString(PyExc_TypeError,
4542 "code object passed to exec may not contain free variables");
4543 return -1;
4544 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004545 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004546 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004547 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004548 FILE *fp = PyFile_AsFile(prog);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004549 char *name = PyString_AsString(PyFile_Name(prog));
Jeremy Hylton714b1122007-02-25 16:01:58 +00004550 PyCompilerFlags cf;
Thomas Woutersae406c62007-09-19 17:27:43 +00004551 if (name == NULL)
4552 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004553 cf.cf_flags = 0;
4554 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004555 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004556 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004557 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004558 v = PyRun_File(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004559 locals);
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004560 }
4561 else {
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004562 PyObject *tmp = NULL;
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004563 char *str;
Tim Peters5ba58662001-07-16 02:29:45 +00004564 PyCompilerFlags cf;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004565 cf.cf_flags = 0;
4566#ifdef Py_USING_UNICODE
4567 if (PyUnicode_Check(prog)) {
4568 tmp = PyUnicode_AsUTF8String(prog);
4569 if (tmp == NULL)
4570 return -1;
4571 prog = tmp;
4572 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4573 }
4574#endif
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004575 if (PyString_AsStringAndSize(prog, &str, NULL))
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004576 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004577 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters8a5c3c72004-04-05 19:36:21 +00004578 v = PyRun_StringFlags(str, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004579 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004580 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004581 v = PyRun_String(str, Py_file_input, globals, locals);
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004582 Py_XDECREF(tmp);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004583 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004584 if (plain)
4585 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004586 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004587 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00004588 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004589 return 0;
4590}
Guido van Rossum24c13741995-02-14 09:42:43 +00004591
Fredrik Lundh7a830892006-05-27 10:39:48 +00004592static void
Paul Prescode68140d2000-08-30 20:25:01 +00004593format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4594{
4595 char *obj_str;
4596
4597 if (!obj)
4598 return;
4599
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004600 obj_str = PyString_AsString(obj);
Paul Prescode68140d2000-08-30 20:25:01 +00004601 if (!obj_str)
4602 return;
4603
4604 PyErr_Format(exc, format_str, obj_str);
4605}
Guido van Rossum950361c1997-01-24 13:49:28 +00004606
Fredrik Lundh7a830892006-05-27 10:39:48 +00004607static PyObject *
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004608string_concatenate(PyObject *v, PyObject *w,
4609 PyFrameObject *f, unsigned char *next_instr)
4610{
4611 /* This function implements 'variable += expr' when both arguments
4612 are strings. */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004613 Py_ssize_t v_len = PyString_GET_SIZE(v);
4614 Py_ssize_t w_len = PyString_GET_SIZE(w);
Armin Rigo97ff0472006-08-09 15:37:26 +00004615 Py_ssize_t new_len = v_len + w_len;
4616 if (new_len < 0) {
4617 PyErr_SetString(PyExc_OverflowError,
4618 "strings are too large to concat");
4619 return NULL;
4620 }
Tim Peters7df5e7f2006-05-26 23:14:37 +00004621
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004622 if (v->ob_refcnt == 2) {
4623 /* In the common case, there are 2 references to the value
4624 * stored in 'variable' when the += is performed: one on the
Thomas Wouterse2176022007-09-20 17:35:10 +00004625 * value stack (in 'v') and one still stored in the
4626 * 'variable'. We try to delete the variable now to reduce
4627 * the refcnt to 1.
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004628 */
4629 switch (*next_instr) {
4630 case STORE_FAST:
4631 {
4632 int oparg = PEEKARG();
4633 PyObject **fastlocals = f->f_localsplus;
4634 if (GETLOCAL(oparg) == v)
4635 SETLOCAL(oparg, NULL);
4636 break;
4637 }
4638 case STORE_DEREF:
4639 {
Thomas Wouterse2176022007-09-20 17:35:10 +00004640 PyObject **freevars = (f->f_localsplus +
4641 f->f_code->co_nlocals);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004642 PyObject *c = freevars[PEEKARG()];
4643 if (PyCell_GET(c) == v)
4644 PyCell_Set(c, NULL);
4645 break;
4646 }
4647 case STORE_NAME:
4648 {
4649 PyObject *names = f->f_code->co_names;
4650 PyObject *name = GETITEM(names, PEEKARG());
4651 PyObject *locals = f->f_locals;
4652 if (PyDict_CheckExact(locals) &&
4653 PyDict_GetItem(locals, name) == v) {
4654 if (PyDict_DelItem(locals, name) != 0) {
4655 PyErr_Clear();
4656 }
4657 }
4658 break;
4659 }
4660 }
4661 }
4662
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004663 if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004664 /* Now we own the last reference to 'v', so we can resize it
4665 * in-place.
4666 */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004667 if (_PyString_Resize(&v, new_len) != 0) {
4668 /* XXX if _PyString_Resize() fails, 'v' has been
Thomas Wouterse2176022007-09-20 17:35:10 +00004669 * deallocated so it cannot be put back into
4670 * 'variable'. The MemoryError is raised when there
4671 * is no value in 'variable', which might (very
4672 * remotely) be a cause of incompatibilities.
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004673 */
4674 return NULL;
4675 }
4676 /* copy 'w' into the newly allocated area of 'v' */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004677 memcpy(PyString_AS_STRING(v) + v_len,
4678 PyString_AS_STRING(w), w_len);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004679 return v;
4680 }
4681 else {
4682 /* When in-place resizing is not an option. */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004683 PyString_Concat(&v, w);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004684 return v;
4685 }
4686}
4687
Guido van Rossum950361c1997-01-24 13:49:28 +00004688#ifdef DYNAMIC_EXECUTION_PROFILE
4689
Fredrik Lundh7a830892006-05-27 10:39:48 +00004690static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004691getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004692{
4693 int i;
4694 PyObject *l = PyList_New(256);
4695 if (l == NULL) return NULL;
4696 for (i = 0; i < 256; i++) {
4697 PyObject *x = PyInt_FromLong(a[i]);
4698 if (x == NULL) {
4699 Py_DECREF(l);
4700 return NULL;
4701 }
4702 PyList_SetItem(l, i, x);
4703 }
4704 for (i = 0; i < 256; i++)
4705 a[i] = 0;
4706 return l;
4707}
4708
4709PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004710_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004711{
4712#ifndef DXPAIRS
4713 return getarray(dxp);
4714#else
4715 int i;
4716 PyObject *l = PyList_New(257);
4717 if (l == NULL) return NULL;
4718 for (i = 0; i < 257; i++) {
4719 PyObject *x = getarray(dxpairs[i]);
4720 if (x == NULL) {
4721 Py_DECREF(l);
4722 return NULL;
4723 }
4724 PyList_SetItem(l, i, x);
4725 }
4726 return l;
4727#endif
4728}
4729
4730#endif