blob: 0b8a377ee443e46fe0fe83d9c2f270abc6a1b6a7 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003
Guido van Rossum681d79a1995-07-18 14:51:37 +00004/* XXX TO DO:
Guido van Rossum681d79a1995-07-18 14:51:37 +00005 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +00006 XXX document it!
7 */
8
Fredrik Lundh7a830892006-05-27 10:39:48 +00009/* enable more aggressive intra-module optimizations, where available */
Fredrik Lundh57640f52006-05-26 11:54:04 +000010#define PY_LOCAL_AGGRESSIVE
11
Guido van Rossumb209a111997-04-29 18:18:01 +000012#include "Python.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000013
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000014#include "code.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000015#include "frameobject.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000016#include "eval.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000017#include "opcode.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000018#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000019
Guido van Rossumc6004111993-11-05 10:22:19 +000020#include <ctype.h>
21
Tim Peters7df5e7f2006-05-26 23:14:37 +000022#ifndef WITH_TSC
Michael W. Hudson75eabd22005-01-18 15:56:11 +000023
24#define READ_TIMESTAMP(var)
25
26#else
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000027
28typedef unsigned long long uint64;
29
Michael W. Hudson800ba232004-08-12 18:19:17 +000030#if defined(__ppc__) /* <- Don't know if this is the correct symbol; this
Thomas Wouterse2176022007-09-20 17:35:10 +000031 section should work for GCC on any PowerPC
32 platform, irrespective of OS.
33 POWER? Who knows :-) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000034
Michael W. Hudson75eabd22005-01-18 15:56:11 +000035#define READ_TIMESTAMP(var) ppc_getcounter(&var)
Michael W. Hudson800ba232004-08-12 18:19:17 +000036
Fredrik Lundh7a830892006-05-27 10:39:48 +000037static void
Michael W. Hudson800ba232004-08-12 18:19:17 +000038ppc_getcounter(uint64 *v)
39{
40 register unsigned long tbu, tb, tbu2;
41
42 loop:
43 asm volatile ("mftbu %0" : "=r" (tbu) );
44 asm volatile ("mftb %0" : "=r" (tb) );
45 asm volatile ("mftbu %0" : "=r" (tbu2));
46 if (__builtin_expect(tbu != tbu2, 0)) goto loop;
47
Tim Peters7df5e7f2006-05-26 23:14:37 +000048 /* The slightly peculiar way of writing the next lines is
Michael W. Hudson800ba232004-08-12 18:19:17 +000049 compiled better by GCC than any other way I tried. */
50 ((long*)(v))[0] = tbu;
51 ((long*)(v))[1] = tb;
52}
53
Mark Dickinson504a1512009-10-31 10:11:28 +000054#elif defined(__i386__)
55
56/* this is for linux/x86 (and probably any other GCC/x86 combo) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000057
Michael W. Hudson75eabd22005-01-18 15:56:11 +000058#define READ_TIMESTAMP(val) \
59 __asm__ __volatile__("rdtsc" : "=A" (val))
Michael W. Hudson800ba232004-08-12 18:19:17 +000060
Mark Dickinson504a1512009-10-31 10:11:28 +000061#elif defined(__x86_64__)
62
63/* for gcc/x86_64, the "A" constraint in DI mode means *either* rax *or* rdx;
64 not edx:eax as it does for i386. Since rdtsc puts its result in edx:eax
65 even in 64-bit mode, we need to use "a" and "d" for the lower and upper
66 32-bit pieces of the result. */
67
68#define READ_TIMESTAMP(val) \
69 __asm__ __volatile__("rdtsc" : \
70 "=a" (((int*)&(val))[0]), "=d" (((int*)&(val))[1]));
71
72
73#else
74
75#error "Don't know how to implement timestamp counter for this architecture"
76
Michael W. Hudson800ba232004-08-12 18:19:17 +000077#endif
78
Tim Peters7df5e7f2006-05-26 23:14:37 +000079void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000080 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
81{
82 uint64 intr, inst, loop;
83 PyThreadState *tstate = PyThreadState_Get();
84 if (!tstate->interp->tscdump)
85 return;
86 intr = intr1 - intr0;
87 inst = inst1 - inst0 - intr;
88 loop = loop1 - loop0 - intr;
89 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
90 opcode, ticked, inst, loop);
91}
Michael W. Hudson800ba232004-08-12 18:19:17 +000092
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000093#endif
94
Guido van Rossum04691fc1992-08-12 15:35:34 +000095/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000096/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000097
Guido van Rossum408027e1996-12-30 16:17:54 +000098#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000099/* For debugging the interpreter: */
100#define LLTRACE 1 /* Low-level trace feature */
101#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000102#endif
103
Jeremy Hylton52820442001-01-03 23:52:36 +0000104typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +0000105
Guido van Rossum374a9221991-04-04 10:40:29 +0000106/* Forward declarations */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000107#ifdef WITH_TSC
Fredrik Lundh7a830892006-05-27 10:39:48 +0000108static PyObject * call_function(PyObject ***, int, uint64*, uint64*);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000109#else
Fredrik Lundh7a830892006-05-27 10:39:48 +0000110static PyObject * call_function(PyObject ***, int);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000111#endif
Fredrik Lundh7a830892006-05-27 10:39:48 +0000112static PyObject * fast_function(PyObject *, PyObject ***, int, int, int);
113static PyObject * do_call(PyObject *, PyObject ***, int, int);
114static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int);
Thomas Wouterse2176022007-09-20 17:35:10 +0000115static PyObject * update_keyword_args(PyObject *, int, PyObject ***,
116 PyObject *);
Fredrik Lundh7a830892006-05-27 10:39:48 +0000117static PyObject * update_star_args(int, int, PyObject *, PyObject ***);
118static PyObject * load_args(PyObject ***, int);
Jeremy Hylton52820442001-01-03 23:52:36 +0000119#define CALL_FLAG_VAR 1
120#define CALL_FLAG_KW 2
121
Guido van Rossum0a066c01992-03-27 17:29:15 +0000122#ifdef LLTRACE
Fredrik Lundh1b949402006-05-26 12:01:49 +0000123static int lltrace;
Fredrik Lundh7a830892006-05-27 10:39:48 +0000124static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +0000125#endif
Fredrik Lundh7a830892006-05-27 10:39:48 +0000126static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
Fred Drake5755ce62001-06-27 19:19:46 +0000127 int, PyObject *);
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +0000128static int call_trace_protected(Py_tracefunc, PyObject *,
Armin Rigo1c2d7e52005-09-20 18:34:01 +0000129 PyFrameObject *, int, PyObject *);
Fredrik Lundh7a830892006-05-27 10:39:48 +0000130static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
131static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Armin Rigobf57a142004-03-22 19:24:58 +0000132 PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000133
Fredrik Lundh7a830892006-05-27 10:39:48 +0000134static PyObject * apply_slice(PyObject *, PyObject *, PyObject *);
135static int assign_slice(PyObject *, PyObject *,
Tim Petersdbd9ba62000-07-09 03:09:57 +0000136 PyObject *, PyObject *);
Fredrik Lundh7a830892006-05-27 10:39:48 +0000137static PyObject * cmp_outcome(int, PyObject *, PyObject *);
138static PyObject * import_from(PyObject *, PyObject *);
139static int import_all_from(PyObject *, PyObject *);
140static PyObject * build_class(PyObject *, PyObject *, PyObject *);
141static int exec_statement(PyFrameObject *,
Tim Petersdbd9ba62000-07-09 03:09:57 +0000142 PyObject *, PyObject *, PyObject *);
Fredrik Lundh7a830892006-05-27 10:39:48 +0000143static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *);
144static void reset_exc_info(PyThreadState *);
145static void format_exc_check_arg(PyObject *, char *, PyObject *);
146static PyObject * string_concatenate(PyObject *, PyObject *,
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000147 PyFrameObject *, unsigned char *);
Benjamin Petersone18ef192009-01-20 14:21:16 +0000148static PyObject * kwd_as_string(PyObject *);
Benjamin Peterson1880d8b2009-05-25 13:13:44 +0000149static PyObject * special_lookup(PyObject *, char *, PyObject **);
Guido van Rossum374a9221991-04-04 10:40:29 +0000150
Paul Prescode68140d2000-08-30 20:25:01 +0000151#define NAME_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000152 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000153#define GLOBAL_NAME_ERROR_MSG \
154 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000155#define UNBOUNDLOCAL_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000156 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000157#define UNBOUNDFREE_ERROR_MSG \
158 "free variable '%.200s' referenced before assignment" \
159 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000160
Guido van Rossum950361c1997-01-24 13:49:28 +0000161/* Dynamic execution profile */
162#ifdef DYNAMIC_EXECUTION_PROFILE
163#ifdef DXPAIRS
164static long dxpairs[257][256];
165#define dxp dxpairs[256]
166#else
167static long dxp[256];
168#endif
169#endif
170
Jeremy Hylton985eba52003-02-05 23:13:00 +0000171/* Function call profile */
172#ifdef CALL_PROFILE
173#define PCALL_NUM 11
174static int pcall[PCALL_NUM];
175
176#define PCALL_ALL 0
177#define PCALL_FUNCTION 1
178#define PCALL_FAST_FUNCTION 2
179#define PCALL_FASTER_FUNCTION 3
180#define PCALL_METHOD 4
181#define PCALL_BOUND_METHOD 5
182#define PCALL_CFUNCTION 6
183#define PCALL_TYPE 7
184#define PCALL_GENERATOR 8
185#define PCALL_OTHER 9
186#define PCALL_POP 10
187
188/* Notes about the statistics
189
190 PCALL_FAST stats
191
192 FAST_FUNCTION means no argument tuple needs to be created.
193 FASTER_FUNCTION means that the fast-path frame setup code is used.
194
195 If there is a method call where the call can be optimized by changing
196 the argument tuple and calling the function directly, it gets recorded
197 twice.
198
199 As a result, the relationship among the statistics appears to be
200 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
201 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
202 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
203 PCALL_METHOD > PCALL_BOUND_METHOD
204*/
205
206#define PCALL(POS) pcall[POS]++
207
208PyObject *
209PyEval_GetCallStats(PyObject *self)
210{
Andrew M. Kuchling1f3ebe02006-10-27 13:22:46 +0000211 return Py_BuildValue("iiiiiiiiiii",
Jeremy Hylton985eba52003-02-05 23:13:00 +0000212 pcall[0], pcall[1], pcall[2], pcall[3],
213 pcall[4], pcall[5], pcall[6], pcall[7],
Andrew M. Kuchling1f3ebe02006-10-27 13:22:46 +0000214 pcall[8], pcall[9], pcall[10]);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000215}
216#else
217#define PCALL(O)
218
219PyObject *
220PyEval_GetCallStats(PyObject *self)
221{
222 Py_INCREF(Py_None);
223 return Py_None;
224}
225#endif
226
Tim Peters5ca576e2001-06-18 22:08:13 +0000227
Guido van Rossume59214e1994-08-30 08:01:59 +0000228#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000229
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000230#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000231#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000232#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000233#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000234
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000235static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000236static PyThread_type_lock pending_lock = 0; /* for pending calls */
Guido van Rossuma9672091994-09-14 13:31:22 +0000237static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000238
Tim Peters7f468f22004-10-11 02:40:51 +0000239int
240PyEval_ThreadsInitialized(void)
241{
242 return interpreter_lock != 0;
243}
244
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000245void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000246PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000247{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000248 if (interpreter_lock)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000249 return;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000250 interpreter_lock = PyThread_allocate_lock();
251 PyThread_acquire_lock(interpreter_lock, 1);
252 main_thread = PyThread_get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000253}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000254
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000255void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000256PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000257{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000258 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000259}
260
261void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000262PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000263{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000264 PyThread_release_lock(interpreter_lock);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000265}
266
267void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000268PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000269{
270 if (tstate == NULL)
271 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000272 /* Check someone has called PyEval_InitThreads() to create the lock */
273 assert(interpreter_lock);
Guido van Rossum65d5b571998-12-21 19:32:43 +0000274 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000275 if (PyThreadState_Swap(tstate) != NULL)
276 Py_FatalError(
277 "PyEval_AcquireThread: non-NULL old thread state");
278}
279
280void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000281PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000282{
283 if (tstate == NULL)
284 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
285 if (PyThreadState_Swap(NULL) != tstate)
286 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000287 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000288}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000289
290/* This function is called from PyOS_AfterFork to ensure that newly
291 created child processes don't hold locks referring to threads which
292 are not running in the child process. (This could also be done using
293 pthread_atfork mechanism, at least for the pthreads implementation.) */
294
295void
296PyEval_ReInitThreads(void)
297{
Jesse Noller5e62ca42008-07-16 20:03:47 +0000298 PyObject *threading, *result;
299 PyThreadState *tstate;
300
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000301 if (!interpreter_lock)
302 return;
303 /*XXX Can't use PyThread_free_lock here because it does too
304 much error-checking. Doing this cleanly would require
305 adding a new function to each thread_*.h. Instead, just
306 create a new lock and waste a little bit of memory */
307 interpreter_lock = PyThread_allocate_lock();
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000308 pending_lock = PyThread_allocate_lock();
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000309 PyThread_acquire_lock(interpreter_lock, 1);
310 main_thread = PyThread_get_thread_ident();
Jesse Noller5e62ca42008-07-16 20:03:47 +0000311
312 /* Update the threading module with the new state.
313 */
314 tstate = PyThreadState_GET();
315 threading = PyMapping_GetItemString(tstate->interp->modules,
316 "threading");
317 if (threading == NULL) {
318 /* threading not imported */
319 PyErr_Clear();
320 return;
321 }
322 result = PyObject_CallMethod(threading, "_after_fork", NULL);
323 if (result == NULL)
324 PyErr_WriteUnraisable(threading);
325 else
326 Py_DECREF(result);
327 Py_DECREF(threading);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000328}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000329#endif
330
Guido van Rossumff4949e1992-08-05 19:58:53 +0000331/* Functions save_thread and restore_thread are always defined so
332 dynamically loaded modules needn't be compiled separately for use
333 with and without threads: */
334
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000335PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000336PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000337{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000338 PyThreadState *tstate = PyThreadState_Swap(NULL);
339 if (tstate == NULL)
340 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000341#ifdef WITH_THREAD
Guido van Rossumb74eca91997-09-30 22:03:16 +0000342 if (interpreter_lock)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000343 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000344#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000345 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000346}
347
348void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000349PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000350{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000351 if (tstate == NULL)
352 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000353#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000354 if (interpreter_lock) {
Guido van Rossumb74eca91997-09-30 22:03:16 +0000355 int err = errno;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000356 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000357 errno = err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000358 }
359#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000360 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000361}
362
363
Guido van Rossuma9672091994-09-14 13:31:22 +0000364/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
365 signal handlers or Mac I/O completion routines) can schedule calls
366 to a function to be called synchronously.
367 The synchronous function is called with one void* argument.
368 It should return 0 for success or -1 for failure -- failure should
369 be accompanied by an exception.
370
371 If registry succeeds, the registry function returns 0; if it fails
372 (e.g. due to too many pending calls) it returns -1 (without setting
373 an exception condition).
374
375 Note that because registry may occur from within signal handlers,
376 or other asynchronous events, calling malloc() is unsafe!
377
378#ifdef WITH_THREAD
379 Any thread can schedule pending calls, but only the main thread
380 will execute them.
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000381 There is no facility to schedule calls to a particular thread, but
382 that should be easy to change, should that ever be required. In
383 that case, the static variables here should go into the python
384 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000385#endif
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000386*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000387
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000388#ifdef WITH_THREAD
389
390/* The WITH_THREAD implementation is thread-safe. It allows
391 scheduling to be made from any thread, and even from an executing
392 callback.
393 */
394
395#define NPENDINGCALLS 32
396static struct {
397 int (*func)(void *);
398 void *arg;
399} pendingcalls[NPENDINGCALLS];
400static int pendingfirst = 0;
401static int pendinglast = 0;
402static volatile int pendingcalls_to_do = 1; /* trigger initialization of lock */
403static char pendingbusy = 0;
404
405int
406Py_AddPendingCall(int (*func)(void *), void *arg)
407{
408 int i, j, result=0;
409 PyThread_type_lock lock = pending_lock;
410
411 /* try a few times for the lock. Since this mechanism is used
412 * for signal handling (on the main thread), there is a (slim)
413 * chance that a signal is delivered on the same thread while we
414 * hold the lock during the Py_MakePendingCalls() function.
415 * This avoids a deadlock in that case.
416 * Note that signals can be delivered on any thread. In particular,
417 * on Windows, a SIGINT is delivered on a system-created worker
418 * thread.
419 * We also check for lock being NULL, in the unlikely case that
420 * this function is called before any bytecode evaluation takes place.
421 */
422 if (lock != NULL) {
423 for (i = 0; i<100; i++) {
424 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
425 break;
426 }
427 if (i == 100)
428 return -1;
429 }
430
431 i = pendinglast;
432 j = (i + 1) % NPENDINGCALLS;
433 if (j == pendingfirst) {
434 result = -1; /* Queue full */
435 } else {
436 pendingcalls[i].func = func;
437 pendingcalls[i].arg = arg;
438 pendinglast = j;
439 }
440 /* signal main loop */
441 _Py_Ticker = 0;
442 pendingcalls_to_do = 1;
443 if (lock != NULL)
444 PyThread_release_lock(lock);
445 return result;
446}
447
448int
449Py_MakePendingCalls(void)
450{
451 int i;
452 int r = 0;
453
454 if (!pending_lock) {
455 /* initial allocation of the lock */
456 pending_lock = PyThread_allocate_lock();
457 if (pending_lock == NULL)
458 return -1;
459 }
460
461 /* only service pending calls on main thread */
462 if (main_thread && PyThread_get_thread_ident() != main_thread)
463 return 0;
464 /* don't perform recursive pending calls */
465 if (pendingbusy)
466 return 0;
467 pendingbusy = 1;
468 /* perform a bounded number of calls, in case of recursion */
469 for (i=0; i<NPENDINGCALLS; i++) {
470 int j;
471 int (*func)(void *);
Mark Dickinson9e58a372009-02-08 17:33:11 +0000472 void *arg = NULL;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000473
474 /* pop one item off the queue while holding the lock */
475 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
476 j = pendingfirst;
477 if (j == pendinglast) {
478 func = NULL; /* Queue empty */
479 } else {
480 func = pendingcalls[j].func;
481 arg = pendingcalls[j].arg;
482 pendingfirst = (j + 1) % NPENDINGCALLS;
483 }
484 pendingcalls_to_do = pendingfirst != pendinglast;
485 PyThread_release_lock(pending_lock);
486 /* having released the lock, perform the callback */
487 if (func == NULL)
488 break;
489 r = func(arg);
490 if (r)
491 break;
492 }
493 pendingbusy = 0;
494 return r;
495}
496
497#else /* if ! defined WITH_THREAD */
498
499/*
500 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
501 This code is used for signal handling in python that isn't built
502 with WITH_THREAD.
503 Don't use this implementation when Py_AddPendingCalls() can happen
504 on a different thread!
505
Guido van Rossuma9672091994-09-14 13:31:22 +0000506 There are two possible race conditions:
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000507 (1) nested asynchronous calls to Py_AddPendingCall()
508 (2) AddPendingCall() calls made while pending calls are being processed.
509
510 (1) is very unlikely because typically signal delivery
511 is blocked during signal handling. So it should be impossible.
512 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000513 The current code is safe against (2), but not against (1).
514 The safety against (2) is derived from the fact that only one
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000515 thread is present, interrupted by signals, and that the critical
516 section is protected with the "busy" variable. On Windows, which
517 delivers SIGINT on a system thread, this does not hold and therefore
518 Windows really shouldn't use this version.
519 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000520*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000521
Guido van Rossuma9672091994-09-14 13:31:22 +0000522#define NPENDINGCALLS 32
523static struct {
Thomas Wouters334fb892000-07-25 12:56:38 +0000524 int (*func)(void *);
525 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000526} pendingcalls[NPENDINGCALLS];
527static volatile int pendingfirst = 0;
528static volatile int pendinglast = 0;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000529static volatile int pendingcalls_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000530
531int
Thomas Wouters334fb892000-07-25 12:56:38 +0000532Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000533{
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000534 static volatile int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000535 int i, j;
536 /* XXX Begin critical section */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000537 if (busy)
538 return -1;
539 busy = 1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000540 i = pendinglast;
541 j = (i + 1) % NPENDINGCALLS;
Guido van Rossum04e70322002-07-17 16:57:13 +0000542 if (j == pendingfirst) {
543 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000544 return -1; /* Queue full */
Guido van Rossum04e70322002-07-17 16:57:13 +0000545 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000546 pendingcalls[i].func = func;
547 pendingcalls[i].arg = arg;
548 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000549
550 _Py_Ticker = 0;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000551 pendingcalls_to_do = 1; /* Signal main loop */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000552 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000553 /* XXX End critical section */
554 return 0;
555}
556
Guido van Rossum180d7b41994-09-29 09:45:57 +0000557int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000558Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000559{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000560 static int busy = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000561 if (busy)
Guido van Rossum180d7b41994-09-29 09:45:57 +0000562 return 0;
563 busy = 1;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000564 pendingcalls_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000565 for (;;) {
566 int i;
Thomas Wouters334fb892000-07-25 12:56:38 +0000567 int (*func)(void *);
568 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000569 i = pendingfirst;
570 if (i == pendinglast)
571 break; /* Queue empty */
572 func = pendingcalls[i].func;
573 arg = pendingcalls[i].arg;
574 pendingfirst = (i + 1) % NPENDINGCALLS;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000575 if (func(arg) < 0) {
576 busy = 0;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000577 pendingcalls_to_do = 1; /* We're not done yet */
Guido van Rossuma9672091994-09-14 13:31:22 +0000578 return -1;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000579 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000580 }
Guido van Rossum180d7b41994-09-29 09:45:57 +0000581 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000582 return 0;
583}
584
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000585#endif /* WITH_THREAD */
586
Guido van Rossuma9672091994-09-14 13:31:22 +0000587
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000588/* The interpreter's recursion limit */
589
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000590#ifndef Py_DEFAULT_RECURSION_LIMIT
591#define Py_DEFAULT_RECURSION_LIMIT 1000
592#endif
593static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
594int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000595
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000596int
597Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000598{
599 return recursion_limit;
600}
601
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000602void
603Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000604{
605 recursion_limit = new_limit;
Thomas Woutersae406c62007-09-19 17:27:43 +0000606 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000607}
608
Armin Rigo2b3eb402003-10-28 12:05:48 +0000609/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
610 if the recursion_depth reaches _Py_CheckRecursionLimit.
611 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
612 to guarantee that _Py_CheckRecursiveCall() is regularly called.
613 Without USE_STACKCHECK, there is no need for this. */
614int
615_Py_CheckRecursiveCall(char *where)
616{
617 PyThreadState *tstate = PyThreadState_GET();
618
619#ifdef USE_STACKCHECK
620 if (PyOS_CheckStack()) {
621 --tstate->recursion_depth;
622 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
623 return -1;
624 }
625#endif
626 if (tstate->recursion_depth > recursion_limit) {
627 --tstate->recursion_depth;
628 PyErr_Format(PyExc_RuntimeError,
629 "maximum recursion depth exceeded%s",
630 where);
631 return -1;
632 }
Thomas Woutersae406c62007-09-19 17:27:43 +0000633 _Py_CheckRecursionLimit = recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000634 return 0;
635}
636
Guido van Rossum374a9221991-04-04 10:40:29 +0000637/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000638enum why_code {
639 WHY_NOT = 0x0001, /* No error */
640 WHY_EXCEPTION = 0x0002, /* Exception occurred */
641 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
642 WHY_RETURN = 0x0008, /* 'return' statement */
643 WHY_BREAK = 0x0010, /* 'break' statement */
644 WHY_CONTINUE = 0x0020, /* 'continue' statement */
645 WHY_YIELD = 0x0040 /* 'yield' operator */
646};
Guido van Rossum374a9221991-04-04 10:40:29 +0000647
Fredrik Lundh7a830892006-05-27 10:39:48 +0000648static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
649static int unpack_iterable(PyObject *, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000650
Jeffrey Yasskinfd8a1ec2008-12-03 06:46:45 +0000651/* Records whether tracing is on for any thread. Counts the number of
652 threads for which tstate->c_tracefunc is non-NULL, so if the value
653 is 0, we know we don't have to check this thread's c_tracefunc.
654 This speeds up the if statement in PyEval_EvalFrameEx() after
655 fast_next_opcode*/
656static int _Py_TracingPossible = 0;
657
Skip Montanarod581d772002-09-03 20:10:45 +0000658/* for manipulating the thread switch and periodic "stuff" - used to be
659 per thread, now just a pair o' globals */
Skip Montanaro99dba272002-09-03 20:19:06 +0000660int _Py_CheckInterval = 100;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000661volatile int _Py_Ticker = 0; /* so that we hit a "tick" first thing */
Guido van Rossum374a9221991-04-04 10:40:29 +0000662
Guido van Rossumb209a111997-04-29 18:18:01 +0000663PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000664PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000665{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000666 return PyEval_EvalCodeEx(co,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000667 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000668 (PyObject **)NULL, 0,
669 (PyObject **)NULL, 0,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000670 (PyObject **)NULL, 0,
671 NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000672}
673
674
675/* Interpreter main loop */
676
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000677PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000678PyEval_EvalFrame(PyFrameObject *f) {
679 /* This is for backward compatibility with extension modules that
Thomas Wouterse2176022007-09-20 17:35:10 +0000680 used this API; core interpreter code should call
681 PyEval_EvalFrameEx() */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000682 return PyEval_EvalFrameEx(f, 0);
683}
684
685PyObject *
Anthony Baxtera863d332006-04-11 07:43:46 +0000686PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000687{
Guido van Rossum950361c1997-01-24 13:49:28 +0000688#ifdef DXPAIRS
689 int lastopcode = 0;
690#endif
Thomas Wouterse2176022007-09-20 17:35:10 +0000691 register PyObject **stack_pointer; /* Next free slot in value stack */
Guido van Rossum374a9221991-04-04 10:40:29 +0000692 register unsigned char *next_instr;
Armin Rigo8817fcd2004-06-17 10:22:40 +0000693 register int opcode; /* Current opcode */
694 register int oparg; /* Current opcode argument, if any */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000695 register enum why_code why; /* Reason for block stack unwind */
Guido van Rossum374a9221991-04-04 10:40:29 +0000696 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000697 register PyObject *x; /* Result object -- NULL if error */
698 register PyObject *v; /* Temporary objects popped off stack */
699 register PyObject *w;
700 register PyObject *u;
701 register PyObject *t;
Barry Warsaw23c9ec82000-08-21 15:44:01 +0000702 register PyObject *stream = NULL; /* for PRINT opcodes */
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000703 register PyObject **fastlocals, **freevars;
Guido van Rossum014518f1998-11-23 21:09:51 +0000704 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000705 PyThreadState *tstate = PyThreadState_GET();
Tim Peters5ca576e2001-06-18 22:08:13 +0000706 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000707
Tim Peters8a5c3c72004-04-05 19:36:21 +0000708 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000709
710 not (instr_lb <= current_bytecode_offset < instr_ub)
711
Tim Peters8a5c3c72004-04-05 19:36:21 +0000712 is true when the line being executed has changed. The
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000713 initial values are such as to make this false the first
714 time it is tested. */
Armin Rigobf57a142004-03-22 19:24:58 +0000715 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000716
Guido van Rossumd076c731998-10-07 19:42:25 +0000717 unsigned char *first_instr;
Skip Montanaro04d80f82002-08-04 21:03:35 +0000718 PyObject *names;
719 PyObject *consts;
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000720#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000721 /* Make it easier to find out where we are with a debugger */
Tim Peters5ca576e2001-06-18 22:08:13 +0000722 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000723#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000724
Neal Norwitza81d2202002-07-14 00:27:26 +0000725/* Tuple access macros */
726
727#ifndef Py_DEBUG
728#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
729#else
730#define GETITEM(v, i) PyTuple_GetItem((v), (i))
731#endif
732
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000733#ifdef WITH_TSC
734/* Use Pentium timestamp counter to mark certain events:
735 inst0 -- beginning of switch statement for opcode dispatch
736 inst1 -- end of switch statement (may be skipped)
737 loop0 -- the top of the mainloop
Tim Peters7df5e7f2006-05-26 23:14:37 +0000738 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000739 (may be skipped)
740 intr1 -- beginning of long interruption
741 intr2 -- end of long interruption
742
743 Many opcodes call out to helper C functions. In some cases, the
744 time in those functions should be counted towards the time for the
745 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
746 calls another Python function; there's no point in charge all the
747 bytecode executed by the called function to the caller.
748
749 It's hard to make a useful judgement statically. In the presence
750 of operator overloading, it's impossible to tell if a call will
751 execute new Python code or not.
752
753 It's a case-by-case judgement. I'll use intr1 for the following
754 cases:
755
756 EXEC_STMT
757 IMPORT_STAR
758 IMPORT_FROM
759 CALL_FUNCTION (and friends)
760
761 */
762 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
763 int ticked = 0;
764
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000765 READ_TIMESTAMP(inst0);
766 READ_TIMESTAMP(inst1);
767 READ_TIMESTAMP(loop0);
768 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000769
770 /* shut up the compiler */
771 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000772#endif
773
Guido van Rossum374a9221991-04-04 10:40:29 +0000774/* Code access macros */
775
Martin v. Löwis18e16552006-02-15 17:27:45 +0000776#define INSTR_OFFSET() ((int)(next_instr - first_instr))
Guido van Rossum374a9221991-04-04 10:40:29 +0000777#define NEXTOP() (*next_instr++)
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000778#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000779#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
Guido van Rossumd076c731998-10-07 19:42:25 +0000780#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000781#define JUMPBY(x) (next_instr += (x))
782
Raymond Hettingerf606f872003-03-16 03:11:04 +0000783/* OpCode prediction macros
Thomas Wouterse2176022007-09-20 17:35:10 +0000784 Some opcodes tend to come in pairs thus making it possible to
785 predict the second code when the first is run. For example,
Jeffrey Yasskin68d68522009-02-28 19:03:21 +0000786 GET_ITER is often followed by FOR_ITER. And FOR_ITER is often
787 followed by STORE_FAST or UNPACK_SEQUENCE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000788
Raymond Hettingerafae11e2008-07-05 02:11:55 +0000789 Verifying the prediction costs a single high-speed test of a register
Raymond Hettingerac2072922003-03-16 15:41:11 +0000790 variable against a constant. If the pairing was good, then the
Raymond Hettingerafae11e2008-07-05 02:11:55 +0000791 processor's own internal branch predication has a high likelihood of
792 success, resulting in a nearly zero-overhead transition to the
793 next opcode. A successful prediction saves a trip through the eval-loop
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +0000794 including its two unpredictable branches, the HAS_ARG test and the
Raymond Hettingerafae11e2008-07-05 02:11:55 +0000795 switch-case. Combined with the processor's internal branch prediction,
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +0000796 a successful PREDICT has the effect of making the two opcodes run as if
Raymond Hettingerafae11e2008-07-05 02:11:55 +0000797 they were a single new opcode with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000798
Raymond Hettingerafae11e2008-07-05 02:11:55 +0000799 If collecting opcode statistics, your choices are to either keep the
800 predictions turned-on and interpret the results as if some opcodes
801 had been combined or turn-off predictions so that the opcode frequency
802 counter updates for both opcodes.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000803*/
804
Raymond Hettingera7216982004-02-08 19:59:27 +0000805#ifdef DYNAMIC_EXECUTION_PROFILE
806#define PREDICT(op) if (0) goto PRED_##op
807#else
Raymond Hettingerac2072922003-03-16 15:41:11 +0000808#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000809#endif
810
Raymond Hettingerf606f872003-03-16 03:11:04 +0000811#define PREDICTED(op) PRED_##op: next_instr++
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000812#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Raymond Hettingerf606f872003-03-16 03:11:04 +0000813
Guido van Rossum374a9221991-04-04 10:40:29 +0000814/* Stack manipulation macros */
815
Martin v. Löwis18e16552006-02-15 17:27:45 +0000816/* The stack can grow at most MAXINT deep, as co_nlocals and
817 co_stacksize are ints. */
818#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
Guido van Rossum374a9221991-04-04 10:40:29 +0000819#define EMPTY() (STACK_LEVEL() == 0)
820#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000821#define SECOND() (stack_pointer[-2])
822#define THIRD() (stack_pointer[-3])
823#define FOURTH() (stack_pointer[-4])
Benjamin Petersonb8338ab2009-06-28 16:08:02 +0000824#define PEEK(n) (stack_pointer[-(n)])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000825#define SET_TOP(v) (stack_pointer[-1] = (v))
826#define SET_SECOND(v) (stack_pointer[-2] = (v))
827#define SET_THIRD(v) (stack_pointer[-3] = (v))
828#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Benjamin Petersonb8338ab2009-06-28 16:08:02 +0000829#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000830#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000831#define BASIC_PUSH(v) (*stack_pointer++ = (v))
832#define BASIC_POP() (*--stack_pointer)
833
Guido van Rossum96a42c81992-01-12 02:29:51 +0000834#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000835#define PUSH(v) { (void)(BASIC_PUSH(v), \
836 lltrace && prtrace(TOP(), "push")); \
Richard Jonescebbefc2006-05-23 18:28:17 +0000837 assert(STACK_LEVEL() <= co->co_stacksize); }
Thomas Wouterse2176022007-09-20 17:35:10 +0000838#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
839 BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000840#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
841 lltrace && prtrace(TOP(), "stackadj")); \
Richard Jonescebbefc2006-05-23 18:28:17 +0000842 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes52729ac2007-12-14 02:33:57 +0000843#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
844 prtrace((STACK_POINTER)[-1], "ext_pop")), \
845 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000846#else
847#define PUSH(v) BASIC_PUSH(v)
848#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000849#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000850#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000851#endif
852
Guido van Rossum681d79a1995-07-18 14:51:37 +0000853/* Local variable macros */
854
855#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000856
857/* The SETLOCAL() macro must not DECREF the local variable in-place and
858 then store the new value; it must copy the old value to a temporary
859 value, then store the new value, and then DECREF the temporary value.
860 This is because it is possible that during the DECREF the frame is
861 accessed by other code (e.g. a __del__ method or gc.collect()) and the
862 variable would be pointing to already-freed memory. */
863#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
864 GETLOCAL(i) = value; \
865 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000866
Guido van Rossuma027efa1997-05-05 20:56:21 +0000867/* Start of code */
868
Tim Peters5ca576e2001-06-18 22:08:13 +0000869 if (f == NULL)
870 return NULL;
871
Armin Rigo1d313ab2003-10-25 14:33:09 +0000872 /* push frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000873 if (Py_EnterRecursiveCall(""))
Armin Rigo1d313ab2003-10-25 14:33:09 +0000874 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000875
Tim Peters5ca576e2001-06-18 22:08:13 +0000876 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000877
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000878 if (tstate->use_tracing) {
879 if (tstate->c_tracefunc != NULL) {
880 /* tstate->c_tracefunc, if defined, is a
881 function that will be called on *every* entry
882 to a code block. Its return value, if not
883 None, is a function that will be called at
884 the start of each executed line of code.
885 (Actually, the function must return itself
886 in order to continue tracing.) The trace
887 functions are called with three arguments:
888 a pointer to the current frame, a string
889 indicating why the function is called, and
890 an argument which depends on the situation.
891 The global trace function is also called
892 whenever an exception is detected. */
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +0000893 if (call_trace_protected(tstate->c_tracefunc,
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +0000894 tstate->c_traceobj,
895 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000896 /* Trace function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000897 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000898 }
899 }
900 if (tstate->c_profilefunc != NULL) {
901 /* Similar for c_profilefunc, except it needn't
902 return itself and isn't called for "line" events */
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +0000903 if (call_trace_protected(tstate->c_profilefunc,
904 tstate->c_profileobj,
905 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000906 /* Profile function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000907 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000908 }
909 }
910 }
911
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000912 co = f->f_code;
913 names = co->co_names;
914 consts = co->co_consts;
915 fastlocals = f->f_localsplus;
Richard Jonescebbefc2006-05-23 18:28:17 +0000916 freevars = f->f_localsplus + co->co_nlocals;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000917 first_instr = (unsigned char*) PyString_AS_STRING(co->co_code);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000918 /* An explanation is in order for the next line.
919
920 f->f_lasti now refers to the index of the last instruction
921 executed. You might think this was obvious from the name, but
922 this wasn't always true before 2.3! PyFrame_New now sets
923 f->f_lasti to -1 (i.e. the index *before* the first instruction)
924 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +0000925 does work. Promise.
Raymond Hettinger4bd97d42007-01-06 01:14:41 +0000926
927 When the PREDICT() macros are enabled, some opcode pairs follow in
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +0000928 direct succession without updating f->f_lasti. A successful
Raymond Hettinger4bd97d42007-01-06 01:14:41 +0000929 prediction effectively links the two codes together as if they
930 were a single new opcode; accordingly,f->f_lasti will point to
931 the first code in the pair (for instance, GET_ITER followed by
932 FOR_ITER is effectively a single opcode and f->f_lasti will point
933 at to the beginning of the combined pair.)
934 */
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000935 next_instr = first_instr + f->f_lasti + 1;
936 stack_pointer = f->f_stacktop;
937 assert(stack_pointer != NULL);
938 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
939
Tim Peters5ca576e2001-06-18 22:08:13 +0000940#ifdef LLTRACE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000941 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +0000942#endif
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000943#if defined(Py_DEBUG) || defined(LLTRACE)
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000944 filename = PyString_AsString(co->co_filename);
Tim Peters5ca576e2001-06-18 22:08:13 +0000945#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000946
Guido van Rossum374a9221991-04-04 10:40:29 +0000947 why = WHY_NOT;
948 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +0000949 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +0000950 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +0000951
Anthony Baxtera863d332006-04-11 07:43:46 +0000952 if (throwflag) { /* support for generator.throw() */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000953 why = WHY_EXCEPTION;
954 goto on_error;
955 }
Tim Peters7df5e7f2006-05-26 23:14:37 +0000956
Guido van Rossum374a9221991-04-04 10:40:29 +0000957 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000958#ifdef WITH_TSC
959 if (inst1 == 0) {
960 /* Almost surely, the opcode executed a break
961 or a continue, preventing inst1 from being set
962 on the way out of the loop.
963 */
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000964 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000965 loop1 = inst1;
966 }
967 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
968 intr0, intr1);
969 ticked = 0;
970 inst1 = 0;
971 intr0 = 0;
972 intr1 = 0;
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000973 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000974#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000975 assert(stack_pointer >= f->f_valuestack); /* else underflow */
Richard Jonescebbefc2006-05-23 18:28:17 +0000976 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000977
Guido van Rossuma027efa1997-05-05 20:56:21 +0000978 /* Do periodic things. Doing this every time through
979 the loop would add too much overhead, so we do it
980 only every Nth instruction. We also do it if
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000981 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
Guido van Rossuma027efa1997-05-05 20:56:21 +0000982 event needs attention (e.g. a signal handler or
983 async I/O handler); see Py_AddPendingCall() and
984 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000985
Skip Montanarod581d772002-09-03 20:10:45 +0000986 if (--_Py_Ticker < 0) {
Thomas Woutersae406c62007-09-19 17:27:43 +0000987 if (*next_instr == SETUP_FINALLY) {
988 /* Make the last opcode before
989 a try: finally: block uninterruptable. */
990 goto fast_next_opcode;
991 }
Skip Montanarod581d772002-09-03 20:10:45 +0000992 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000993 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000994#ifdef WITH_TSC
995 ticked = 1;
996#endif
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000997 if (pendingcalls_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +0000998 if (Py_MakePendingCalls() < 0) {
999 why = WHY_EXCEPTION;
1000 goto on_error;
1001 }
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +00001002 if (pendingcalls_to_do)
Kurt B. Kaiser4c79a832004-11-23 18:06:08 +00001003 /* MakePendingCalls() didn't succeed.
1004 Force early re-execution of this
1005 "periodic" code, possibly after
1006 a thread switch */
1007 _Py_Ticker = 0;
Guido van Rossum8861b741996-07-30 16:49:37 +00001008 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001009#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001010 if (interpreter_lock) {
1011 /* Give another thread a chance */
1012
Guido van Rossum25ce5661997-08-02 03:10:38 +00001013 if (PyThreadState_Swap(NULL) != tstate)
1014 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +00001015 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001016
1017 /* Other threads may run now */
1018
Guido van Rossum65d5b571998-12-21 19:32:43 +00001019 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001020 if (PyThreadState_Swap(tstate) != NULL)
1021 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +00001022
1023 /* Check for thread interrupts */
1024
1025 if (tstate->async_exc != NULL) {
1026 x = tstate->async_exc;
1027 tstate->async_exc = NULL;
1028 PyErr_SetNone(x);
1029 Py_DECREF(x);
1030 why = WHY_EXCEPTION;
1031 goto on_error;
1032 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001033 }
1034#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001035 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001036
Neil Schemenauer63543862002-02-17 19:10:14 +00001037 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +00001038 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001039
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001040 /* line-by-line tracing support */
1041
Jeffrey Yasskinfd8a1ec2008-12-03 06:46:45 +00001042 if (_Py_TracingPossible &&
1043 tstate->c_tracefunc != NULL && !tstate->tracing) {
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001044 /* see maybe_call_line_trace
1045 for expository comments */
1046 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001047
Michael W. Hudson58ee2af2003-04-29 16:18:47 +00001048 err = maybe_call_line_trace(tstate->c_tracefunc,
1049 tstate->c_traceobj,
Armin Rigobf57a142004-03-22 19:24:58 +00001050 f, &instr_lb, &instr_ub,
1051 &instr_prev);
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001052 /* Reload possibly changed frame fields */
1053 JUMPTO(f->f_lasti);
Michael W. Hudson58ee2af2003-04-29 16:18:47 +00001054 if (f->f_stacktop != NULL) {
1055 stack_pointer = f->f_stacktop;
1056 f->f_stacktop = NULL;
1057 }
1058 if (err) {
1059 /* trace function raised an exception */
1060 goto on_error;
1061 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001062 }
1063
1064 /* Extract opcode and argument */
1065
Guido van Rossum374a9221991-04-04 10:40:29 +00001066 opcode = NEXTOP();
Armin Rigo8817fcd2004-06-17 10:22:40 +00001067 oparg = 0; /* allows oparg to be stored in a register because
1068 it doesn't have to be remembered across a full loop */
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001069 if (HAS_ARG(opcode))
1070 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00001071 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001072#ifdef DYNAMIC_EXECUTION_PROFILE
1073#ifdef DXPAIRS
1074 dxpairs[lastopcode][opcode]++;
1075 lastopcode = opcode;
1076#endif
1077 dxp[opcode]++;
1078#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001079
Guido van Rossum96a42c81992-01-12 02:29:51 +00001080#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +00001081 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001082
Guido van Rossum96a42c81992-01-12 02:29:51 +00001083 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001084 if (HAS_ARG(opcode)) {
1085 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001086 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001087 }
1088 else {
1089 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001090 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +00001091 }
1092 }
1093#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001094
Guido van Rossum374a9221991-04-04 10:40:29 +00001095 /* Main switch on opcode */
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001096 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001097
Guido van Rossum374a9221991-04-04 10:40:29 +00001098 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001099
Guido van Rossum374a9221991-04-04 10:40:29 +00001100 /* BEWARE!
1101 It is essential that any operation that fails sets either
1102 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1103 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001104
Guido van Rossum374a9221991-04-04 10:40:29 +00001105 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001106
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001107 case NOP:
1108 goto fast_next_opcode;
1109
Neil Schemenauer63543862002-02-17 19:10:14 +00001110 case LOAD_FAST:
1111 x = GETLOCAL(oparg);
1112 if (x != NULL) {
1113 Py_INCREF(x);
1114 PUSH(x);
1115 goto fast_next_opcode;
1116 }
1117 format_exc_check_arg(PyExc_UnboundLocalError,
1118 UNBOUNDLOCAL_ERROR_MSG,
1119 PyTuple_GetItem(co->co_varnames, oparg));
1120 break;
1121
1122 case LOAD_CONST:
Skip Montanaro04d80f82002-08-04 21:03:35 +00001123 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +00001124 Py_INCREF(x);
1125 PUSH(x);
1126 goto fast_next_opcode;
1127
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001128 PREDICTED_WITH_ARG(STORE_FAST);
Neil Schemenauer63543862002-02-17 19:10:14 +00001129 case STORE_FAST:
1130 v = POP();
1131 SETLOCAL(oparg, v);
1132 goto fast_next_opcode;
1133
Guido van Rossum374a9221991-04-04 10:40:29 +00001134 case POP_TOP:
1135 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001136 Py_DECREF(v);
Neil Schemenauer63543862002-02-17 19:10:14 +00001137 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001138
Guido van Rossum374a9221991-04-04 10:40:29 +00001139 case ROT_TWO:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001140 v = TOP();
1141 w = SECOND();
1142 SET_TOP(w);
1143 SET_SECOND(v);
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 ROT_THREE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001147 v = TOP();
1148 w = SECOND();
1149 x = THIRD();
1150 SET_TOP(w);
1151 SET_SECOND(x);
1152 SET_THIRD(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +00001153 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001154
Thomas Wouters434d0822000-08-24 20:11:32 +00001155 case ROT_FOUR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001156 u = TOP();
1157 v = SECOND();
1158 w = THIRD();
1159 x = FOURTH();
1160 SET_TOP(v);
1161 SET_SECOND(w);
1162 SET_THIRD(x);
1163 SET_FOURTH(u);
Raymond Hettinger080cb322003-03-14 01:37:42 +00001164 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001165
Guido van Rossum374a9221991-04-04 10:40:29 +00001166 case DUP_TOP:
1167 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001168 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001169 PUSH(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +00001170 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001171
Thomas Wouters434d0822000-08-24 20:11:32 +00001172 case DUP_TOPX:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001173 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001174 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +00001175 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001176 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +00001177 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001178 STACKADJ(2);
1179 SET_TOP(x);
1180 SET_SECOND(w);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001181 goto fast_next_opcode;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001182 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001183 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +00001184 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001185 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +00001186 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001187 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +00001188 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001189 STACKADJ(3);
1190 SET_TOP(x);
1191 SET_SECOND(w);
1192 SET_THIRD(v);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001193 goto fast_next_opcode;
Thomas Wouters434d0822000-08-24 20:11:32 +00001194 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001195 Py_FatalError("invalid argument to DUP_TOPX"
1196 " (bytecode corruption?)");
Jeffrey Yasskin2d873bd2008-12-08 18:55:24 +00001197 /* Never returns, so don't bother to set why. */
Tim Peters35ba6892000-10-11 07:04:49 +00001198 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001199
Guido van Rossum374a9221991-04-04 10:40:29 +00001200 case UNARY_POSITIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001201 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001202 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001203 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001204 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001205 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001206 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001207
Guido van Rossum374a9221991-04-04 10:40:29 +00001208 case UNARY_NEGATIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001209 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001210 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001211 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001212 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001213 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001214 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001215
Guido van Rossum374a9221991-04-04 10:40:29 +00001216 case UNARY_NOT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001217 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001218 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001219 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +00001220 if (err == 0) {
1221 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001222 SET_TOP(Py_True);
Guido van Rossumfc490731997-05-06 15:06:49 +00001223 continue;
1224 }
1225 else if (err > 0) {
1226 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001227 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +00001228 err = 0;
1229 continue;
1230 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00001231 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001232 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001233
Guido van Rossum374a9221991-04-04 10:40:29 +00001234 case UNARY_CONVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001235 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001236 x = PyObject_Repr(v);
1237 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001238 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001239 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001240 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001241
Guido van Rossum7928cd71991-10-24 14:59:31 +00001242 case UNARY_INVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001243 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001244 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001245 Py_DECREF(v);
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 Rossum7928cd71991-10-24 14:59:31 +00001248 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001249
Guido van Rossum50564e81996-01-12 01:13:16 +00001250 case BINARY_POWER:
1251 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001252 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001253 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +00001254 Py_DECREF(v);
1255 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001256 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001257 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +00001258 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001259
Guido van Rossum374a9221991-04-04 10:40:29 +00001260 case BINARY_MULTIPLY:
1261 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001262 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001263 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001264 Py_DECREF(v);
1265 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001266 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001267 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001268 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001269
Guido van Rossum374a9221991-04-04 10:40:29 +00001270 case BINARY_DIVIDE:
Tim Peters3caca232001-12-06 06:23:26 +00001271 if (!_Py_QnewFlag) {
1272 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001273 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001274 x = PyNumber_Divide(v, w);
1275 Py_DECREF(v);
1276 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001277 SET_TOP(x);
Tim Peters3caca232001-12-06 06:23:26 +00001278 if (x != NULL) continue;
1279 break;
1280 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001281 /* -Qnew is in effect: fall through to
Tim Peters3caca232001-12-06 06:23:26 +00001282 BINARY_TRUE_DIVIDE */
1283 case BINARY_TRUE_DIVIDE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001284 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001285 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001286 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001287 Py_DECREF(v);
1288 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001289 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001290 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001291 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001292
Guido van Rossum4668b002001-08-08 05:00:18 +00001293 case BINARY_FLOOR_DIVIDE:
1294 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001295 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001296 x = PyNumber_FloorDivide(v, w);
1297 Py_DECREF(v);
1298 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001299 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001300 if (x != NULL) continue;
1301 break;
1302
Guido van Rossum374a9221991-04-04 10:40:29 +00001303 case BINARY_MODULO:
1304 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001305 v = TOP();
Collin Winter8725dce2009-02-20 19:30:41 +00001306 if (PyString_CheckExact(v))
1307 x = PyString_Format(v, w);
1308 else
1309 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001310 Py_DECREF(v);
1311 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001312 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001313 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001314 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001315
Guido van Rossum374a9221991-04-04 10:40:29 +00001316 case BINARY_ADD:
1317 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001318 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001319 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001320 /* INLINE: int + int */
1321 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001322 a = PyInt_AS_LONG(v);
1323 b = PyInt_AS_LONG(w);
Mark Dickinson34398182009-12-02 17:33:41 +00001324 /* cast to avoid undefined behaviour
1325 on overflow */
1326 i = (long)((unsigned long)a + b);
Guido van Rossum87780df2001-08-23 02:58:07 +00001327 if ((i^a) < 0 && (i^b) < 0)
1328 goto slow_add;
1329 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001330 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001331 else if (PyString_CheckExact(v) &&
1332 PyString_CheckExact(w)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001333 x = string_concatenate(v, w, f, next_instr);
1334 /* string_concatenate consumed the ref to v */
1335 goto skip_decref_vx;
1336 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001337 else {
1338 slow_add:
Guido van Rossumc12da691997-07-17 23:12:42 +00001339 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001340 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001341 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001342 skip_decref_vx:
Guido van Rossumb209a111997-04-29 18:18:01 +00001343 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001344 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001345 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001346 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001347
Guido van Rossum374a9221991-04-04 10:40:29 +00001348 case BINARY_SUBTRACT:
1349 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001350 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001351 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001352 /* INLINE: int - int */
1353 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001354 a = PyInt_AS_LONG(v);
1355 b = PyInt_AS_LONG(w);
Mark Dickinson34398182009-12-02 17:33:41 +00001356 /* cast to avoid undefined behaviour
1357 on overflow */
1358 i = (long)((unsigned long)a - b);
Guido van Rossum87780df2001-08-23 02:58:07 +00001359 if ((i^a) < 0 && (i^~b) < 0)
1360 goto slow_sub;
1361 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001362 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001363 else {
1364 slow_sub:
Guido van Rossumc12da691997-07-17 23:12:42 +00001365 x = PyNumber_Subtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001366 }
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 Rossum374a9221991-04-04 10:40:29 +00001373 case BINARY_SUBSCR:
1374 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001375 v = TOP();
Tim Petersb1c46982001-10-05 20:41:38 +00001376 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001377 /* INLINE: list[int] */
Neal Norwitz814e9382006-03-02 07:54:28 +00001378 Py_ssize_t i = PyInt_AsSsize_t(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001379 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +00001380 i += PyList_GET_SIZE(v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001381 if (i >= 0 && i < PyList_GET_SIZE(v)) {
Guido van Rossumfa00e951998-07-08 15:02:37 +00001382 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001383 Py_INCREF(x);
1384 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001385 else
1386 goto slow_get;
Guido van Rossumc12da691997-07-17 23:12:42 +00001387 }
1388 else
Raymond Hettinger467a6982004-04-07 11:39:21 +00001389 slow_get:
Guido van Rossumc12da691997-07-17 23:12:42 +00001390 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001391 Py_DECREF(v);
1392 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001393 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001394 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001395 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001396
Guido van Rossum7928cd71991-10-24 14:59:31 +00001397 case BINARY_LSHIFT:
1398 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001399 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001400 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001401 Py_DECREF(v);
1402 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001403 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001404 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001405 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001406
Guido van Rossum7928cd71991-10-24 14:59:31 +00001407 case BINARY_RSHIFT:
1408 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001409 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001410 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001411 Py_DECREF(v);
1412 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001413 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001414 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001415 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001416
Guido van Rossum7928cd71991-10-24 14:59:31 +00001417 case BINARY_AND:
1418 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001419 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001420 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001421 Py_DECREF(v);
1422 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001423 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001424 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001425 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001426
Guido van Rossum7928cd71991-10-24 14:59:31 +00001427 case BINARY_XOR:
1428 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001429 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001430 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001431 Py_DECREF(v);
1432 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001433 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001434 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001435 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001436
Guido van Rossum7928cd71991-10-24 14:59:31 +00001437 case BINARY_OR:
1438 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001439 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001440 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001441 Py_DECREF(v);
1442 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001443 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001444 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001445 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001446
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001447 case LIST_APPEND:
1448 w = POP();
Benjamin Peterson8f7b94e2009-06-28 16:14:07 +00001449 v = PEEK(oparg);
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001450 err = PyList_Append(v, w);
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001451 Py_DECREF(w);
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001452 if (err == 0) {
1453 PREDICT(JUMP_ABSOLUTE);
1454 continue;
1455 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001456 break;
1457
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001458 case SET_ADD:
1459 w = POP();
1460 v = stack_pointer[-oparg];
1461 err = PySet_Add(v, w);
1462 Py_DECREF(w);
1463 if (err == 0) {
1464 PREDICT(JUMP_ABSOLUTE);
1465 continue;
1466 }
1467 break;
1468
Thomas Wouters434d0822000-08-24 20:11:32 +00001469 case INPLACE_POWER:
1470 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001471 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001472 x = PyNumber_InPlacePower(v, w, Py_None);
1473 Py_DECREF(v);
1474 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001475 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001476 if (x != NULL) continue;
1477 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001478
Thomas Wouters434d0822000-08-24 20:11:32 +00001479 case INPLACE_MULTIPLY:
1480 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001481 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001482 x = PyNumber_InPlaceMultiply(v, w);
1483 Py_DECREF(v);
1484 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001485 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001486 if (x != NULL) continue;
1487 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001488
Thomas Wouters434d0822000-08-24 20:11:32 +00001489 case INPLACE_DIVIDE:
Tim Peters54b11912001-12-25 18:49:11 +00001490 if (!_Py_QnewFlag) {
1491 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001492 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001493 x = PyNumber_InPlaceDivide(v, w);
1494 Py_DECREF(v);
1495 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001496 SET_TOP(x);
Tim Peters54b11912001-12-25 18:49:11 +00001497 if (x != NULL) continue;
1498 break;
1499 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001500 /* -Qnew is in effect: fall through to
Tim Peters54b11912001-12-25 18:49:11 +00001501 INPLACE_TRUE_DIVIDE */
1502 case INPLACE_TRUE_DIVIDE:
Thomas Wouters434d0822000-08-24 20:11:32 +00001503 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001504 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001505 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001506 Py_DECREF(v);
1507 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001508 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001509 if (x != NULL) continue;
1510 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001511
Guido van Rossum4668b002001-08-08 05:00:18 +00001512 case INPLACE_FLOOR_DIVIDE:
1513 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001514 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001515 x = PyNumber_InPlaceFloorDivide(v, w);
1516 Py_DECREF(v);
1517 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001518 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001519 if (x != NULL) continue;
1520 break;
1521
Thomas Wouters434d0822000-08-24 20:11:32 +00001522 case INPLACE_MODULO:
1523 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001524 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001525 x = PyNumber_InPlaceRemainder(v, w);
1526 Py_DECREF(v);
1527 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001528 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001529 if (x != NULL) continue;
1530 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001531
Thomas Wouters434d0822000-08-24 20:11:32 +00001532 case INPLACE_ADD:
1533 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001534 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001535 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001536 /* INLINE: int + int */
1537 register long a, b, i;
1538 a = PyInt_AS_LONG(v);
1539 b = PyInt_AS_LONG(w);
1540 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001541 if ((i^a) < 0 && (i^b) < 0)
1542 goto slow_iadd;
1543 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001544 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001545 else if (PyString_CheckExact(v) &&
1546 PyString_CheckExact(w)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001547 x = string_concatenate(v, w, f, next_instr);
1548 /* string_concatenate consumed the ref to v */
1549 goto skip_decref_v;
1550 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001551 else {
1552 slow_iadd:
Thomas Wouters434d0822000-08-24 20:11:32 +00001553 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001554 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001555 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001556 skip_decref_v:
Thomas Wouters434d0822000-08-24 20:11:32 +00001557 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001558 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001559 if (x != NULL) continue;
1560 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001561
Thomas Wouters434d0822000-08-24 20:11:32 +00001562 case INPLACE_SUBTRACT:
1563 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001564 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001565 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001566 /* INLINE: int - int */
1567 register long a, b, i;
1568 a = PyInt_AS_LONG(v);
1569 b = PyInt_AS_LONG(w);
1570 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001571 if ((i^a) < 0 && (i^~b) < 0)
1572 goto slow_isub;
1573 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001574 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001575 else {
1576 slow_isub:
Thomas Wouters434d0822000-08-24 20:11:32 +00001577 x = PyNumber_InPlaceSubtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001578 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001579 Py_DECREF(v);
1580 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001581 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001582 if (x != NULL) continue;
1583 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001584
Thomas Wouters434d0822000-08-24 20:11:32 +00001585 case INPLACE_LSHIFT:
1586 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001587 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001588 x = PyNumber_InPlaceLshift(v, w);
1589 Py_DECREF(v);
1590 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001591 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001592 if (x != NULL) continue;
1593 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001594
Thomas Wouters434d0822000-08-24 20:11:32 +00001595 case INPLACE_RSHIFT:
1596 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001597 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001598 x = PyNumber_InPlaceRshift(v, w);
1599 Py_DECREF(v);
1600 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001601 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001602 if (x != NULL) continue;
1603 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001604
Thomas Wouters434d0822000-08-24 20:11:32 +00001605 case INPLACE_AND:
1606 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001607 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001608 x = PyNumber_InPlaceAnd(v, w);
1609 Py_DECREF(v);
1610 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001611 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001612 if (x != NULL) continue;
1613 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001614
Thomas Wouters434d0822000-08-24 20:11:32 +00001615 case INPLACE_XOR:
1616 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001617 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001618 x = PyNumber_InPlaceXor(v, w);
1619 Py_DECREF(v);
1620 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001621 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001622 if (x != NULL) continue;
1623 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001624
Thomas Wouters434d0822000-08-24 20:11:32 +00001625 case INPLACE_OR:
1626 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001627 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001628 x = PyNumber_InPlaceOr(v, w);
1629 Py_DECREF(v);
1630 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001631 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001632 if (x != NULL) continue;
1633 break;
1634
Guido van Rossum374a9221991-04-04 10:40:29 +00001635 case SLICE+0:
1636 case SLICE+1:
1637 case SLICE+2:
1638 case SLICE+3:
1639 if ((opcode-SLICE) & 2)
1640 w = POP();
1641 else
1642 w = NULL;
1643 if ((opcode-SLICE) & 1)
1644 v = POP();
1645 else
1646 v = NULL;
Raymond Hettinger663004b2003-01-09 15:24:30 +00001647 u = TOP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001648 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001649 Py_DECREF(u);
1650 Py_XDECREF(v);
1651 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001652 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001653 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001654 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001655
Guido van Rossum374a9221991-04-04 10:40:29 +00001656 case STORE_SLICE+0:
1657 case STORE_SLICE+1:
1658 case STORE_SLICE+2:
1659 case STORE_SLICE+3:
1660 if ((opcode-STORE_SLICE) & 2)
1661 w = POP();
1662 else
1663 w = NULL;
1664 if ((opcode-STORE_SLICE) & 1)
1665 v = POP();
1666 else
1667 v = NULL;
1668 u = POP();
1669 t = POP();
1670 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +00001671 Py_DECREF(t);
1672 Py_DECREF(u);
1673 Py_XDECREF(v);
1674 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001675 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001676 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001677
Guido van Rossum374a9221991-04-04 10:40:29 +00001678 case DELETE_SLICE+0:
1679 case DELETE_SLICE+1:
1680 case DELETE_SLICE+2:
1681 case DELETE_SLICE+3:
1682 if ((opcode-DELETE_SLICE) & 2)
1683 w = POP();
1684 else
1685 w = NULL;
1686 if ((opcode-DELETE_SLICE) & 1)
1687 v = POP();
1688 else
1689 v = NULL;
1690 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001691 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +00001692 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00001693 Py_DECREF(u);
1694 Py_XDECREF(v);
1695 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001696 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001697 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001698
Guido van Rossum374a9221991-04-04 10:40:29 +00001699 case STORE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001700 w = TOP();
1701 v = SECOND();
1702 u = THIRD();
1703 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001704 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001705 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001706 Py_DECREF(u);
1707 Py_DECREF(v);
1708 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001709 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001710 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001711
Guido van Rossum374a9221991-04-04 10:40:29 +00001712 case DELETE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001713 w = TOP();
1714 v = SECOND();
1715 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001716 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001717 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001718 Py_DECREF(v);
1719 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001720 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001721 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001722
Guido van Rossum374a9221991-04-04 10:40:29 +00001723 case PRINT_EXPR:
1724 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001725 w = PySys_GetObject("displayhook");
1726 if (w == NULL) {
1727 PyErr_SetString(PyExc_RuntimeError,
1728 "lost sys.displayhook");
1729 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001730 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001731 }
1732 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001733 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001734 if (x == NULL)
1735 err = -1;
1736 }
1737 if (err == 0) {
1738 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001739 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001740 if (w == NULL)
1741 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001742 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001743 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001744 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001745 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001746
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001747 case PRINT_ITEM_TO:
1748 w = stream = POP();
1749 /* fall through to PRINT_ITEM */
1750
Guido van Rossum374a9221991-04-04 10:40:29 +00001751 case PRINT_ITEM:
1752 v = POP();
Barry Warsaw093abe02000-08-29 04:56:13 +00001753 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001754 w = PySys_GetObject("stdout");
1755 if (w == NULL) {
1756 PyErr_SetString(PyExc_RuntimeError,
1757 "lost sys.stdout");
1758 err = -1;
1759 }
Guido van Rossum8f183201997-12-31 05:53:15 +00001760 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001761 /* PyFile_SoftSpace() can exececute arbitrary code
1762 if sys.stdout is an instance with a __getattr__.
1763 If __getattr__ raises an exception, w will
1764 be freed, so we need to prevent that temporarily. */
1765 Py_XINCREF(w);
Tim Peters8e5fd532002-03-24 19:25:00 +00001766 if (w != NULL && PyFile_SoftSpace(w, 0))
Guido van Rossumbe270261997-05-22 22:26:18 +00001767 err = PyFile_WriteString(" ", w);
1768 if (err == 0)
1769 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001770 if (err == 0) {
Tim Peters8e5fd532002-03-24 19:25:00 +00001771 /* XXX move into writeobject() ? */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001772 if (PyString_Check(v)) {
1773 char *s = PyString_AS_STRING(v);
1774 Py_ssize_t len = PyString_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001775 if (len == 0 ||
1776 !isspace(Py_CHARMASK(s[len-1])) ||
1777 s[len-1] == ' ')
1778 PyFile_SoftSpace(w, 1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001779 }
Martin v. Löwis8d3ce5a2001-12-18 22:36:40 +00001780#ifdef Py_USING_UNICODE
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001781 else if (PyUnicode_Check(v)) {
1782 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
Martin v. Löwis66851282006-04-22 11:40:03 +00001783 Py_ssize_t len = PyUnicode_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001784 if (len == 0 ||
1785 !Py_UNICODE_ISSPACE(s[len-1]) ||
1786 s[len-1] == ' ')
1787 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001788 }
Michael W. Hudsond95c8282002-05-20 13:56:11 +00001789#endif
Tim Peters8e5fd532002-03-24 19:25:00 +00001790 else
1791 PyFile_SoftSpace(w, 1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001792 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001793 Py_XDECREF(w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001794 Py_DECREF(v);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001795 Py_XDECREF(stream);
1796 stream = NULL;
1797 if (err == 0)
1798 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001799 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001800
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001801 case PRINT_NEWLINE_TO:
1802 w = stream = POP();
1803 /* fall through to PRINT_NEWLINE */
1804
Guido van Rossum374a9221991-04-04 10:40:29 +00001805 case PRINT_NEWLINE:
Barry Warsaw093abe02000-08-29 04:56:13 +00001806 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001807 w = PySys_GetObject("stdout");
Jeffrey Yasskin2d873bd2008-12-08 18:55:24 +00001808 if (w == NULL) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001809 PyErr_SetString(PyExc_RuntimeError,
1810 "lost sys.stdout");
Jeffrey Yasskin2d873bd2008-12-08 18:55:24 +00001811 why = WHY_EXCEPTION;
1812 }
Guido van Rossum3165fe61992-09-25 21:59:05 +00001813 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001814 if (w != NULL) {
Georg Brandlaa76d772008-07-01 20:56:03 +00001815 /* w.write() may replace sys.stdout, so we
1816 * have to keep our reference to it */
Amaury Forgeot d'Arcbdd941f2008-07-01 20:38:04 +00001817 Py_INCREF(w);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001818 err = PyFile_WriteString("\n", w);
1819 if (err == 0)
1820 PyFile_SoftSpace(w, 0);
Amaury Forgeot d'Arcbdd941f2008-07-01 20:38:04 +00001821 Py_DECREF(w);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001822 }
1823 Py_XDECREF(stream);
1824 stream = NULL;
Guido van Rossum374a9221991-04-04 10:40:29 +00001825 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001826
Thomas Wouters434d0822000-08-24 20:11:32 +00001827
1828#ifdef CASE_TOO_BIG
1829 default: switch (opcode) {
1830#endif
Guido van Rossumf10570b1995-07-07 22:53:21 +00001831 case RAISE_VARARGS:
1832 u = v = w = NULL;
1833 switch (oparg) {
1834 case 3:
1835 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001836 /* Fallthrough */
1837 case 2:
1838 v = POP(); /* value */
1839 /* Fallthrough */
1840 case 1:
1841 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001842 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001843 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001844 break;
1845 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001846 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001847 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001848 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001849 break;
1850 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001851 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001852
Guido van Rossum374a9221991-04-04 10:40:29 +00001853 case LOAD_LOCALS:
Raymond Hettinger467a6982004-04-07 11:39:21 +00001854 if ((x = f->f_locals) != NULL) {
1855 Py_INCREF(x);
1856 PUSH(x);
1857 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001858 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001859 PyErr_SetString(PyExc_SystemError, "no locals");
Guido van Rossum374a9221991-04-04 10:40:29 +00001860 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001861
Guido van Rossum374a9221991-04-04 10:40:29 +00001862 case RETURN_VALUE:
1863 retval = POP();
1864 why = WHY_RETURN;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001865 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001866
Tim Peters5ca576e2001-06-18 22:08:13 +00001867 case YIELD_VALUE:
1868 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001869 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001870 why = WHY_YIELD;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001871 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001872
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001873 case EXEC_STMT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001874 w = TOP();
1875 v = SECOND();
1876 u = THIRD();
1877 STACKADJ(-3);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001878 READ_TIMESTAMP(intr0);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001879 err = exec_statement(f, u, v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001880 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00001881 Py_DECREF(u);
1882 Py_DECREF(v);
1883 Py_DECREF(w);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001884 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001885
Guido van Rossum374a9221991-04-04 10:40:29 +00001886 case POP_BLOCK:
1887 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001888 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001889 while (STACK_LEVEL() > b->b_level) {
1890 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001891 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001892 }
1893 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001894 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001895
Jeffrey Yasskin9063a992008-03-03 01:27:03 +00001896 PREDICTED(END_FINALLY);
Guido van Rossum374a9221991-04-04 10:40:29 +00001897 case END_FINALLY:
1898 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001899 if (PyInt_Check(v)) {
Raymond Hettinger7c958652004-04-06 10:11:10 +00001900 why = (enum why_code) PyInt_AS_LONG(v);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001901 assert(why != WHY_YIELD);
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00001902 if (why == WHY_RETURN ||
1903 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001904 retval = POP();
1905 }
Thomas Wouterse2176022007-09-20 17:35:10 +00001906 else if (PyExceptionClass_Check(v) ||
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001907 PyString_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001908 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001909 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001910 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001911 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001912 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001913 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001914 else if (v != Py_None) {
1915 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001916 "'finally' pops bad exception");
1917 why = WHY_EXCEPTION;
1918 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001919 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001920 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001921
Guido van Rossum374a9221991-04-04 10:40:29 +00001922 case BUILD_CLASS:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001923 u = TOP();
1924 v = SECOND();
1925 w = THIRD();
1926 STACKADJ(-2);
Guido van Rossum25831651993-05-19 14:50:45 +00001927 x = build_class(u, v, w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001928 SET_TOP(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001929 Py_DECREF(u);
1930 Py_DECREF(v);
1931 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001932 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001933
Guido van Rossum374a9221991-04-04 10:40:29 +00001934 case STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001935 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001936 v = POP();
Raymond Hettinger467a6982004-04-07 11:39:21 +00001937 if ((x = f->f_locals) != NULL) {
Raymond Hettinger66bd2332004-08-02 08:30:07 +00001938 if (PyDict_CheckExact(x))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001939 err = PyDict_SetItem(x, w, v);
1940 else
1941 err = PyObject_SetItem(x, w, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001942 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001943 if (err == 0) continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001944 break;
1945 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001946 PyErr_Format(PyExc_SystemError,
1947 "no locals found when storing %s",
1948 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001949 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001950
Guido van Rossum374a9221991-04-04 10:40:29 +00001951 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001952 w = GETITEM(names, oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001953 if ((x = f->f_locals) != NULL) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001954 if ((err = PyObject_DelItem(x, w)) != 0)
Raymond Hettinger467a6982004-04-07 11:39:21 +00001955 format_exc_check_arg(PyExc_NameError,
Thomas Wouterse2176022007-09-20 17:35:10 +00001956 NAME_ERROR_MSG,
1957 w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001958 break;
1959 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001960 PyErr_Format(PyExc_SystemError,
1961 "no locals when deleting %s",
1962 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001963 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001964
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001965 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001966 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001967 v = POP();
Thomas Wouterse2176022007-09-20 17:35:10 +00001968 if (PyTuple_CheckExact(v) &&
1969 PyTuple_GET_SIZE(v) == oparg) {
1970 PyObject **items = \
1971 ((PyTupleObject *)v)->ob_item;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001972 while (oparg--) {
1973 w = items[oparg];
1974 Py_INCREF(w);
1975 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001976 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001977 Py_DECREF(v);
1978 continue;
Thomas Wouterse2176022007-09-20 17:35:10 +00001979 } else if (PyList_CheckExact(v) &&
1980 PyList_GET_SIZE(v) == oparg) {
1981 PyObject **items = \
1982 ((PyListObject *)v)->ob_item;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001983 while (oparg--) {
1984 w = items[oparg];
1985 Py_INCREF(w);
1986 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001987 }
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001988 } else if (unpack_iterable(v, oparg,
Thomas Wouterse2176022007-09-20 17:35:10 +00001989 stack_pointer + oparg)) {
Benjamin Peterson8f7b94e2009-06-28 16:14:07 +00001990 STACKADJ(oparg);
Georg Brandl5cb76c12007-03-21 09:00:39 +00001991 } else {
1992 /* unpack_iterable() raised an exception */
Barry Warsawe42b18f1997-08-25 22:13:04 +00001993 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001994 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001995 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001996 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001997
Guido van Rossum374a9221991-04-04 10:40:29 +00001998 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001999 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002000 v = TOP();
2001 u = SECOND();
2002 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00002003 err = PyObject_SetAttr(v, w, u); /* v.w = u */
2004 Py_DECREF(v);
2005 Py_DECREF(u);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00002006 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002007 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002008
Guido van Rossum374a9221991-04-04 10:40:29 +00002009 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00002010 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002011 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002012 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
2013 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00002014 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002015 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002016
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00002017 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00002018 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00002019 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002020 err = PyDict_SetItem(f->f_globals, w, v);
2021 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00002022 if (err == 0) continue;
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00002023 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002024
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00002025 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00002026 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002027 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00002028 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002029 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00002030 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002031
Guido van Rossum374a9221991-04-04 10:40:29 +00002032 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00002033 w = GETITEM(names, oparg);
Raymond Hettinger214b1c32004-07-02 06:41:07 +00002034 if ((v = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00002035 PyErr_Format(PyExc_SystemError,
2036 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00002037 PyObject_REPR(w));
Jeffrey Yasskin2d873bd2008-12-08 18:55:24 +00002038 why = WHY_EXCEPTION;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002039 break;
2040 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00002041 if (PyDict_CheckExact(v)) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00002042 x = PyDict_GetItem(v, w);
Michael W. Hudsona3711f72004-08-02 14:50:43 +00002043 Py_XINCREF(x);
2044 }
Raymond Hettinger214b1c32004-07-02 06:41:07 +00002045 else {
2046 x = PyObject_GetItem(v, w);
2047 if (x == NULL && PyErr_Occurred()) {
Thomas Wouterse2176022007-09-20 17:35:10 +00002048 if (!PyErr_ExceptionMatches(
2049 PyExc_KeyError))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00002050 break;
2051 PyErr_Clear();
2052 }
2053 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002054 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002055 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002056 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002057 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002058 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00002059 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00002060 PyExc_NameError,
Thomas Wouterse2176022007-09-20 17:35:10 +00002061 NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002062 break;
2063 }
2064 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00002065 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00002066 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002067 PUSH(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002068 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002069
Guido van Rossum374a9221991-04-04 10:40:29 +00002070 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00002071 w = GETITEM(names, oparg);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002072 if (PyString_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00002073 /* Inline the PyDict_GetItem() calls.
2074 WARNING: this is an extreme speed hack.
2075 Do not try this at home. */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002076 long hash = ((PyStringObject *)w)->ob_shash;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002077 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002078 PyDictObject *d;
Armin Rigo35f6d362006-06-01 13:19:12 +00002079 PyDictEntry *e;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002080 d = (PyDictObject *)(f->f_globals);
Armin Rigo35f6d362006-06-01 13:19:12 +00002081 e = d->ma_lookup(d, w, hash);
2082 if (e == NULL) {
2083 x = NULL;
2084 break;
2085 }
2086 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002087 if (x != NULL) {
2088 Py_INCREF(x);
2089 PUSH(x);
2090 continue;
2091 }
2092 d = (PyDictObject *)(f->f_builtins);
Armin Rigo35f6d362006-06-01 13:19:12 +00002093 e = d->ma_lookup(d, w, hash);
2094 if (e == NULL) {
2095 x = NULL;
2096 break;
2097 }
2098 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002099 if (x != NULL) {
2100 Py_INCREF(x);
2101 PUSH(x);
2102 continue;
2103 }
2104 goto load_global_error;
2105 }
2106 }
2107 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00002108 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002109 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002110 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002111 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002112 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00002113 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00002114 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002115 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002116 break;
2117 }
2118 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002119 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00002120 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00002121 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002122
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002123 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00002124 x = GETLOCAL(oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002125 if (x != NULL) {
2126 SETLOCAL(oparg, NULL);
2127 continue;
Guido van Rossum2e4c8991998-05-12 20:27:36 +00002128 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00002129 format_exc_check_arg(
2130 PyExc_UnboundLocalError,
2131 UNBOUNDLOCAL_ERROR_MSG,
2132 PyTuple_GetItem(co->co_varnames, oparg)
2133 );
2134 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002135
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002136 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00002137 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002138 Py_INCREF(x);
2139 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00002140 if (x != NULL) continue;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002141 break;
2142
2143 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00002144 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002145 w = PyCell_Get(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002146 if (w != NULL) {
2147 PUSH(w);
2148 continue;
Jeremy Hylton2524d692001-02-05 17:23:16 +00002149 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00002150 err = -1;
2151 /* Don't stomp existing exception */
2152 if (PyErr_Occurred())
2153 break;
Richard Jonescebbefc2006-05-23 18:28:17 +00002154 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
2155 v = PyTuple_GET_ITEM(co->co_cellvars,
Raymond Hettinger467a6982004-04-07 11:39:21 +00002156 oparg);
2157 format_exc_check_arg(
2158 PyExc_UnboundLocalError,
2159 UNBOUNDLOCAL_ERROR_MSG,
2160 v);
2161 } else {
Thomas Wouterse2176022007-09-20 17:35:10 +00002162 v = PyTuple_GET_ITEM(co->co_freevars, oparg -
2163 PyTuple_GET_SIZE(co->co_cellvars));
2164 format_exc_check_arg(PyExc_NameError,
2165 UNBOUNDFREE_ERROR_MSG, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002166 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002167 break;
2168
2169 case STORE_DEREF:
2170 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00002171 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002172 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00002173 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002174 continue;
2175
Guido van Rossum374a9221991-04-04 10:40:29 +00002176 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00002177 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002178 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002179 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002180 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002181 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002182 }
2183 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002184 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002185 }
2186 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002187
Guido van Rossum374a9221991-04-04 10:40:29 +00002188 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00002189 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002190 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002191 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002192 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00002193 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002194 }
2195 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002196 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002197 }
2198 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002199
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00002200 case BUILD_SET:
2201 x = PySet_New(NULL);
2202 if (x != NULL) {
2203 for (; --oparg >= 0;) {
2204 w = POP();
2205 if (err == 0)
2206 err = PySet_Add(x, w);
2207 Py_DECREF(w);
2208 }
2209 if (err != 0) {
2210 Py_DECREF(x);
2211 break;
2212 }
2213 PUSH(x);
2214 continue;
2215 }
2216 break;
2217
2218
Guido van Rossum374a9221991-04-04 10:40:29 +00002219 case BUILD_MAP:
Raymond Hettingerfd7ed402007-12-18 21:24:09 +00002220 x = _PyDict_NewPresized((Py_ssize_t)oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002221 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002222 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002223 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002224
Raymond Hettingereffde122007-12-18 18:26:18 +00002225 case STORE_MAP:
2226 w = TOP(); /* key */
2227 u = SECOND(); /* value */
2228 v = THIRD(); /* dict */
2229 STACKADJ(-2);
2230 assert (PyDict_CheckExact(v));
2231 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2232 Py_DECREF(u);
2233 Py_DECREF(w);
2234 if (err == 0) continue;
2235 break;
2236
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002237 case MAP_ADD:
2238 w = TOP(); /* key */
2239 u = SECOND(); /* value */
2240 STACKADJ(-2);
2241 v = stack_pointer[-oparg]; /* dict */
2242 assert (PyDict_CheckExact(v));
2243 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2244 Py_DECREF(u);
2245 Py_DECREF(w);
2246 if (err == 0) {
2247 PREDICT(JUMP_ABSOLUTE);
2248 continue;
2249 }
2250 break;
2251
Guido van Rossum374a9221991-04-04 10:40:29 +00002252 case LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00002253 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002254 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002255 x = PyObject_GetAttr(v, w);
2256 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002257 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002258 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002259 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002260
Guido van Rossum374a9221991-04-04 10:40:29 +00002261 case COMPARE_OP:
2262 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002263 v = TOP();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00002264 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00002265 /* INLINE: cmp(int, int) */
2266 register long a, b;
2267 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00002268 a = PyInt_AS_LONG(v);
2269 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00002270 switch (oparg) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00002271 case PyCmp_LT: res = a < b; break;
2272 case PyCmp_LE: res = a <= b; break;
2273 case PyCmp_EQ: res = a == b; break;
2274 case PyCmp_NE: res = a != b; break;
2275 case PyCmp_GT: res = a > b; break;
2276 case PyCmp_GE: res = a >= b; break;
2277 case PyCmp_IS: res = v == w; break;
2278 case PyCmp_IS_NOT: res = v != w; break;
Guido van Rossumc12da691997-07-17 23:12:42 +00002279 default: goto slow_compare;
2280 }
2281 x = res ? Py_True : Py_False;
2282 Py_INCREF(x);
2283 }
2284 else {
2285 slow_compare:
2286 x = cmp_outcome(oparg, v, w);
2287 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002288 Py_DECREF(v);
2289 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002290 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00002291 if (x == NULL) break;
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002292 PREDICT(POP_JUMP_IF_FALSE);
2293 PREDICT(POP_JUMP_IF_TRUE);
Raymond Hettingerf606f872003-03-16 03:11:04 +00002294 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002295
Guido van Rossum374a9221991-04-04 10:40:29 +00002296 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00002297 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002298 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002299 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002300 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00002301 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002302 break;
2303 }
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00002304 Py_INCREF(x);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002305 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002306 u = TOP();
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002307 if (PyInt_AsLong(u) != -1 || PyErr_Occurred())
2308 w = PyTuple_Pack(5,
2309 w,
2310 f->f_globals,
2311 f->f_locals == NULL ?
2312 Py_None : f->f_locals,
2313 v,
2314 u);
2315 else
2316 w = PyTuple_Pack(4,
2317 w,
2318 f->f_globals,
2319 f->f_locals == NULL ?
2320 Py_None : f->f_locals,
2321 v);
2322 Py_DECREF(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002323 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002324 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002325 u = POP();
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00002326 Py_DECREF(x);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002327 x = NULL;
2328 break;
2329 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002330 READ_TIMESTAMP(intr0);
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00002331 v = x;
2332 x = PyEval_CallObject(v, w);
2333 Py_DECREF(v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002334 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002335 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002336 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002337 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002338 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002339
Thomas Wouters52152252000-08-17 22:55:00 +00002340 case IMPORT_STAR:
2341 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002342 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002343 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002344 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00002345 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002346 break;
2347 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002348 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002349 err = import_all_from(x, v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002350 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002351 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00002352 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002353 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002354 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002355
Thomas Wouters52152252000-08-17 22:55:00 +00002356 case IMPORT_FROM:
Skip Montanaro496e6582002-08-06 17:47:40 +00002357 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00002358 v = TOP();
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002359 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002360 x = import_from(v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002361 READ_TIMESTAMP(intr1);
Thomas Wouters52152252000-08-17 22:55:00 +00002362 PUSH(x);
2363 if (x != NULL) continue;
2364 break;
2365
Guido van Rossum374a9221991-04-04 10:40:29 +00002366 case JUMP_FORWARD:
2367 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002368 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002369
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002370 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
2371 case POP_JUMP_IF_FALSE:
2372 w = POP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002373 if (w == Py_True) {
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002374 Py_DECREF(w);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002375 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002376 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002377 if (w == Py_False) {
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002378 Py_DECREF(w);
2379 JUMPTO(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002380 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002381 }
2382 err = PyObject_IsTrue(w);
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002383 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002384 if (err > 0)
2385 err = 0;
2386 else if (err == 0)
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002387 JUMPTO(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002388 else
2389 break;
2390 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002391
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002392 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
2393 case POP_JUMP_IF_TRUE:
2394 w = POP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002395 if (w == Py_False) {
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002396 Py_DECREF(w);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002397 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002398 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002399 if (w == Py_True) {
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002400 Py_DECREF(w);
2401 JUMPTO(oparg);
2402 goto fast_next_opcode;
2403 }
2404 err = PyObject_IsTrue(w);
2405 Py_DECREF(w);
2406 if (err > 0) {
2407 err = 0;
2408 JUMPTO(oparg);
2409 }
2410 else if (err == 0)
2411 ;
2412 else
2413 break;
2414 continue;
2415
2416 case JUMP_IF_FALSE_OR_POP:
2417 w = TOP();
2418 if (w == Py_True) {
2419 STACKADJ(-1);
2420 Py_DECREF(w);
2421 goto fast_next_opcode;
2422 }
2423 if (w == Py_False) {
2424 JUMPTO(oparg);
2425 goto fast_next_opcode;
2426 }
2427 err = PyObject_IsTrue(w);
2428 if (err > 0) {
2429 STACKADJ(-1);
2430 Py_DECREF(w);
2431 err = 0;
2432 }
2433 else if (err == 0)
2434 JUMPTO(oparg);
2435 else
2436 break;
2437 continue;
2438
2439 case JUMP_IF_TRUE_OR_POP:
2440 w = TOP();
2441 if (w == Py_False) {
2442 STACKADJ(-1);
2443 Py_DECREF(w);
2444 goto fast_next_opcode;
2445 }
2446 if (w == Py_True) {
2447 JUMPTO(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002448 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002449 }
2450 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002451 if (err > 0) {
2452 err = 0;
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002453 JUMPTO(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002454 }
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002455 else if (err == 0) {
2456 STACKADJ(-1);
2457 Py_DECREF(w);
2458 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002459 else
2460 break;
2461 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002462
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00002463 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002464 case JUMP_ABSOLUTE:
2465 JUMPTO(oparg);
Raymond Hettingerdc1d1ba2007-11-07 02:45:46 +00002466#if FAST_LOOPS
2467 /* Enabling this path speeds-up all while and for-loops by bypassing
2468 the per-loop checks for signals. By default, this should be turned-off
2469 because it prevents detection of a control-break in tight loops like
2470 "while 1: pass". Compile with this option turned-on when you need
2471 the speed-up and do not need break checking inside tight loops (ones
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00002472 that contain only instructions ending with goto fast_next_opcode).
Raymond Hettingerdc1d1ba2007-11-07 02:45:46 +00002473 */
2474 goto fast_next_opcode;
2475#else
Neil Schemenauerca2a2f12003-05-30 23:59:44 +00002476 continue;
Raymond Hettingerdc1d1ba2007-11-07 02:45:46 +00002477#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002478
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002479 case GET_ITER:
2480 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002481 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002482 x = PyObject_GetIter(v);
2483 Py_DECREF(v);
2484 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002485 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002486 PREDICT(FOR_ITER);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002487 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002488 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002489 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002490 break;
2491
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002492 PREDICTED_WITH_ARG(FOR_ITER);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002493 case FOR_ITER:
2494 /* before: [iter]; after: [iter, iter()] *or* [] */
2495 v = TOP();
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002496 x = (*v->ob_type->tp_iternext)(v);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002497 if (x != NULL) {
2498 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002499 PREDICT(STORE_FAST);
2500 PREDICT(UNPACK_SEQUENCE);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002501 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002502 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002503 if (PyErr_Occurred()) {
Thomas Wouterse2176022007-09-20 17:35:10 +00002504 if (!PyErr_ExceptionMatches(
2505 PyExc_StopIteration))
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002506 break;
2507 PyErr_Clear();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002508 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002509 /* iterator ended normally */
2510 x = v = POP();
2511 Py_DECREF(v);
2512 JUMPBY(oparg);
2513 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002514
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002515 case BREAK_LOOP:
2516 why = WHY_BREAK;
2517 goto fast_block_end;
2518
2519 case CONTINUE_LOOP:
2520 retval = PyInt_FromLong(oparg);
Neal Norwitz02104df2006-05-19 06:31:23 +00002521 if (!retval) {
2522 x = NULL;
2523 break;
2524 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002525 why = WHY_CONTINUE;
2526 goto fast_block_end;
2527
Guido van Rossum374a9221991-04-04 10:40:29 +00002528 case SETUP_LOOP:
2529 case SETUP_EXCEPT:
2530 case SETUP_FINALLY:
Thomas Wouterse2176022007-09-20 17:35:10 +00002531 /* NOTE: If you add any new block-setup opcodes that
2532 are not try/except/finally handlers, you may need
2533 to update the PyGen_NeedsFinalizing() function.
2534 */
Phillip J. Eby2ba96612006-04-10 17:51:05 +00002535
Guido van Rossumb209a111997-04-29 18:18:01 +00002536 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002537 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002538 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002539
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00002540 case SETUP_WITH:
2541 {
2542 static PyObject *exit, *enter;
2543 w = TOP();
2544 x = special_lookup(w, "__exit__", &exit);
2545 if (!x)
2546 break;
2547 SET_TOP(x);
2548 u = special_lookup(w, "__enter__", &enter);
2549 Py_DECREF(w);
2550 if (!u) {
2551 x = NULL;
2552 break;
2553 }
2554 x = PyObject_CallFunctionObjArgs(u, NULL);
2555 Py_DECREF(u);
2556 if (!x)
2557 break;
Benjamin Peterson565d7852010-02-05 02:12:14 +00002558 /* Setup a finally block (SETUP_WITH as a block is
2559 equivalent to SETUP_FINALLY except it normalizes
2560 the exception) before pushing the result of
2561 __enter__ on the stack. */
2562 PyFrame_BlockSetup(f, SETUP_WITH, INSTR_OFFSET() + oparg,
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00002563 STACK_LEVEL());
2564
2565 PUSH(x);
2566 continue;
2567 }
2568
Guido van Rossumc2e20742006-02-27 22:32:47 +00002569 case WITH_CLEANUP:
2570 {
Nick Coghlan7af53be2008-03-07 14:13:28 +00002571 /* At the top of the stack are 1-3 values indicating
2572 how/why we entered the finally clause:
2573 - TOP = None
2574 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2575 - TOP = WHY_*; no retval below it
2576 - (TOP, SECOND, THIRD) = exc_info()
2577 Below them is EXIT, the context.__exit__ bound method.
Guido van Rossumc2e20742006-02-27 22:32:47 +00002578 In the last case, we must call
Nick Coghlan7af53be2008-03-07 14:13:28 +00002579 EXIT(TOP, SECOND, THIRD)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002580 otherwise we must call
Nick Coghlan7af53be2008-03-07 14:13:28 +00002581 EXIT(None, None, None)
2582
2583 In all cases, we remove EXIT from the stack, leaving
2584 the rest in the same order.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002585
2586 In addition, if the stack represents an exception,
Guido van Rossumf6694362006-03-10 02:28:35 +00002587 *and* the function call returns a 'true' value, we
2588 "zap" this information, to prevent END_FINALLY from
2589 re-raising the exception. (But non-local gotos
2590 should still be resumed.)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002591 */
Tim Peters7df5e7f2006-05-26 23:14:37 +00002592
Nick Coghlan7af53be2008-03-07 14:13:28 +00002593 PyObject *exit_func;
2594
2595 u = POP();
2596 if (u == Py_None) {
2597 exit_func = TOP();
2598 SET_TOP(u);
2599 v = w = Py_None;
2600 }
2601 else if (PyInt_Check(u)) {
2602 switch(PyInt_AS_LONG(u)) {
2603 case WHY_RETURN:
2604 case WHY_CONTINUE:
2605 /* Retval in TOP. */
2606 exit_func = SECOND();
2607 SET_SECOND(TOP());
2608 SET_TOP(u);
2609 break;
2610 default:
2611 exit_func = TOP();
2612 SET_TOP(u);
2613 break;
2614 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002615 u = v = w = Py_None;
2616 }
2617 else {
Nick Coghlan7af53be2008-03-07 14:13:28 +00002618 v = TOP();
2619 w = SECOND();
2620 exit_func = THIRD();
2621 SET_TOP(u);
2622 SET_SECOND(v);
2623 SET_THIRD(w);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002624 }
Guido van Rossumf6694362006-03-10 02:28:35 +00002625 /* XXX Not the fastest way to call it... */
Nick Coghlan7af53be2008-03-07 14:13:28 +00002626 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2627 NULL);
Amaury Forgeot d'Arcad9b5992008-12-10 23:22:49 +00002628 Py_DECREF(exit_func);
2629 if (x == NULL)
Guido van Rossumf6694362006-03-10 02:28:35 +00002630 break; /* Go to error exit */
Amaury Forgeot d'Arcad9b5992008-12-10 23:22:49 +00002631
2632 if (u != Py_None)
2633 err = PyObject_IsTrue(x);
2634 else
2635 err = 0;
2636 Py_DECREF(x);
2637
2638 if (err < 0)
2639 break; /* Go to error exit */
2640 else if (err > 0) {
2641 err = 0;
Guido van Rossumf6694362006-03-10 02:28:35 +00002642 /* There was an exception and a true return */
Nick Coghlan7af53be2008-03-07 14:13:28 +00002643 STACKADJ(-2);
Guido van Rossumf6694362006-03-10 02:28:35 +00002644 Py_INCREF(Py_None);
2645 SET_TOP(Py_None);
Guido van Rossumf6694362006-03-10 02:28:35 +00002646 Py_DECREF(u);
2647 Py_DECREF(v);
2648 Py_DECREF(w);
2649 } else {
Nick Coghlan7af53be2008-03-07 14:13:28 +00002650 /* The stack was rearranged to remove EXIT
2651 above. Let END_FINALLY do its thing */
Guido van Rossumf6694362006-03-10 02:28:35 +00002652 }
Jeffrey Yasskin9063a992008-03-03 01:27:03 +00002653 PREDICT(END_FINALLY);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002654 break;
2655 }
2656
Guido van Rossumf10570b1995-07-07 22:53:21 +00002657 case CALL_FUNCTION:
Armin Rigo8817fcd2004-06-17 10:22:40 +00002658 {
2659 PyObject **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002660 PCALL(PCALL_ALL);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002661 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002662#ifdef WITH_TSC
Armin Rigo8817fcd2004-06-17 10:22:40 +00002663 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002664#else
Armin Rigo8817fcd2004-06-17 10:22:40 +00002665 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002666#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002667 stack_pointer = sp;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002668 PUSH(x);
2669 if (x != NULL)
2670 continue;
2671 break;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002672 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002673
Jeremy Hylton76901512000-03-28 23:49:17 +00002674 case CALL_FUNCTION_VAR:
2675 case CALL_FUNCTION_KW:
2676 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002677 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002678 int na = oparg & 0xff;
2679 int nk = (oparg>>8) & 0xff;
2680 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002681 int n = na + 2 * nk;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002682 PyObject **pfunc, *func, **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002683 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002684 if (flags & CALL_FLAG_VAR)
2685 n++;
2686 if (flags & CALL_FLAG_KW)
2687 n++;
2688 pfunc = stack_pointer - n - 1;
2689 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002690
Guido van Rossumac7be682001-01-17 15:42:30 +00002691 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002692 && PyMethod_GET_SELF(func) != NULL) {
2693 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002694 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002695 func = PyMethod_GET_FUNCTION(func);
2696 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002697 Py_DECREF(*pfunc);
2698 *pfunc = self;
2699 na++;
2700 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002701 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002702 Py_INCREF(func);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002703 sp = stack_pointer;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002704 READ_TIMESTAMP(intr0);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002705 x = ext_do_call(func, &sp, flags, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002706 READ_TIMESTAMP(intr1);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002707 stack_pointer = sp;
Jeremy Hylton76901512000-03-28 23:49:17 +00002708 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002709
Jeremy Hylton76901512000-03-28 23:49:17 +00002710 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002711 w = POP();
2712 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002713 }
2714 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002715 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002716 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002717 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002718 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002719
Guido van Rossum681d79a1995-07-18 14:51:37 +00002720 case MAKE_FUNCTION:
2721 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002722 x = PyFunction_New(v, f->f_globals);
2723 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002724 /* XXX Maybe this should be a separate opcode? */
2725 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002726 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002727 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002728 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002729 x = NULL;
2730 break;
2731 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002732 while (--oparg >= 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002733 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002734 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002735 }
2736 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002737 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002738 }
2739 PUSH(x);
2740 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002741
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002742 case MAKE_CLOSURE:
2743 {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002744 v = POP(); /* code object */
2745 x = PyFunction_New(v, f->f_globals);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002746 Py_DECREF(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002747 if (x != NULL) {
2748 v = POP();
Jeffrey Yasskin2d873bd2008-12-08 18:55:24 +00002749 if (PyFunction_SetClosure(x, v) != 0) {
2750 /* Can't happen unless bytecode is corrupt. */
2751 why = WHY_EXCEPTION;
2752 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002753 Py_DECREF(v);
2754 }
2755 if (x != NULL && oparg > 0) {
2756 v = PyTuple_New(oparg);
2757 if (v == NULL) {
2758 Py_DECREF(x);
2759 x = NULL;
2760 break;
2761 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002762 while (--oparg >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002763 w = POP();
2764 PyTuple_SET_ITEM(v, oparg, w);
2765 }
Jeffrey Yasskin2d873bd2008-12-08 18:55:24 +00002766 if (PyFunction_SetDefaults(x, v) != 0) {
2767 /* Can't happen unless
2768 PyFunction_SetDefaults changes. */
2769 why = WHY_EXCEPTION;
2770 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002771 Py_DECREF(v);
2772 }
2773 PUSH(x);
2774 break;
2775 }
2776
Guido van Rossum8861b741996-07-30 16:49:37 +00002777 case BUILD_SLICE:
2778 if (oparg == 3)
2779 w = POP();
2780 else
2781 w = NULL;
2782 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002783 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002784 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002785 Py_DECREF(u);
2786 Py_DECREF(v);
2787 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002788 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002789 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002790 break;
2791
Fred Drakeef8ace32000-08-24 00:32:09 +00002792 case EXTENDED_ARG:
2793 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002794 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002795 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002796
Guido van Rossum374a9221991-04-04 10:40:29 +00002797 default:
2798 fprintf(stderr,
2799 "XXX lineno: %d, opcode: %d\n",
Jeffrey Yasskinf7f858d2009-05-08 22:23:21 +00002800 PyFrame_GetLineNumber(f),
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002801 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002802 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002803 why = WHY_EXCEPTION;
2804 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002805
2806#ifdef CASE_TOO_BIG
2807 }
2808#endif
2809
Guido van Rossum374a9221991-04-04 10:40:29 +00002810 } /* switch */
2811
2812 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002813
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002814 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002815
Guido van Rossum374a9221991-04-04 10:40:29 +00002816 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002817
Guido van Rossum374a9221991-04-04 10:40:29 +00002818 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002819 if (err == 0 && x != NULL) {
2820#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002821 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002822 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002823 fprintf(stderr,
2824 "XXX undetected error\n");
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002825 else {
2826#endif
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002827 READ_TIMESTAMP(loop1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002828 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002829#ifdef CHECKEXC
2830 }
2831#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +00002832 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002833 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002834 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002835 err = 0;
2836 }
2837
Guido van Rossum374a9221991-04-04 10:40:29 +00002838 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002839
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002840 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002841 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002842 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002843 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002844 why = WHY_EXCEPTION;
2845 }
2846 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002847#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002848 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002849 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002850 if (PyErr_Occurred()) {
Neal Norwitz8250fbe2008-01-27 17:12:15 +00002851 char buf[128];
Jeremy Hylton904ed862003-11-05 17:29:35 +00002852 sprintf(buf, "Stack unwind with exception "
2853 "set and why=%d", why);
2854 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002855 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002856 }
2857#endif
2858
2859 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002860
Guido van Rossum374a9221991-04-04 10:40:29 +00002861 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002862 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002863
Fred Drake8f51f542001-10-04 14:48:42 +00002864 if (tstate->c_tracefunc != NULL)
2865 call_exc_trace(tstate->c_tracefunc,
2866 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002867 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002868
Guido van Rossum374a9221991-04-04 10:40:29 +00002869 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002870
Guido van Rossum374a9221991-04-04 10:40:29 +00002871 if (why == WHY_RERAISE)
2872 why = WHY_EXCEPTION;
2873
2874 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002875
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002876fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002877 while (why != WHY_NOT && f->f_iblock > 0) {
Benjamin Peterson4a3cf192009-07-01 23:45:19 +00002878 /* Peek at the current block. */
2879 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002880
Tim Peters8a5c3c72004-04-05 19:36:21 +00002881 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002882 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002883 why = WHY_NOT;
2884 JUMPTO(PyInt_AS_LONG(retval));
2885 Py_DECREF(retval);
2886 break;
2887 }
2888
Benjamin Peterson4a3cf192009-07-01 23:45:19 +00002889 /* Now we have to pop the block. */
2890 f->f_iblock--;
2891
Guido van Rossum374a9221991-04-04 10:40:29 +00002892 while (STACK_LEVEL() > b->b_level) {
2893 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002894 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002895 }
2896 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2897 why = WHY_NOT;
2898 JUMPTO(b->b_handler);
2899 break;
2900 }
2901 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002902 (b->b_type == SETUP_EXCEPT &&
Benjamin Peterson565d7852010-02-05 02:12:14 +00002903 why == WHY_EXCEPTION) ||
2904 b->b_type == SETUP_WITH) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002905 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002906 PyObject *exc, *val, *tb;
2907 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002908 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002909 val = Py_None;
2910 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002911 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002912 /* Make the raw exception data
2913 available to the handler,
2914 so a program can emulate the
2915 Python main loop. Don't do
2916 this for 'finally'. */
Benjamin Peterson565d7852010-02-05 02:12:14 +00002917 if (b->b_type == SETUP_EXCEPT ||
2918 b->b_type == SETUP_WITH) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002919 PyErr_NormalizeException(
2920 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002921 set_exc_info(tstate,
2922 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002923 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002924 if (tb == NULL) {
2925 Py_INCREF(Py_None);
2926 PUSH(Py_None);
2927 } else
2928 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002929 PUSH(val);
2930 PUSH(exc);
2931 }
2932 else {
Raymond Hettinger06032cb2004-04-06 09:37:35 +00002933 if (why & (WHY_RETURN | WHY_CONTINUE))
Guido van Rossum374a9221991-04-04 10:40:29 +00002934 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002935 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002936 PUSH(v);
2937 }
2938 why = WHY_NOT;
2939 JUMPTO(b->b_handler);
2940 break;
2941 }
2942 } /* unwind stack */
2943
2944 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002945
Guido van Rossum374a9221991-04-04 10:40:29 +00002946 if (why != WHY_NOT)
2947 break;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002948 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002949
Guido van Rossum374a9221991-04-04 10:40:29 +00002950 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002951
Tim Peters8a5c3c72004-04-05 19:36:21 +00002952 assert(why != WHY_YIELD);
2953 /* Pop remaining stack entries. */
2954 while (!EMPTY()) {
2955 v = POP();
2956 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002957 }
2958
Tim Peters8a5c3c72004-04-05 19:36:21 +00002959 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002960 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002961
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002962fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002963 if (tstate->use_tracing) {
Barry Warsawe2eca0b2005-08-15 18:14:19 +00002964 if (tstate->c_tracefunc) {
2965 if (why == WHY_RETURN || why == WHY_YIELD) {
2966 if (call_trace(tstate->c_tracefunc,
2967 tstate->c_traceobj, f,
2968 PyTrace_RETURN, retval)) {
2969 Py_XDECREF(retval);
2970 retval = NULL;
2971 why = WHY_EXCEPTION;
2972 }
2973 }
2974 else if (why == WHY_EXCEPTION) {
2975 call_trace_protected(tstate->c_tracefunc,
2976 tstate->c_traceobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002977 PyTrace_RETURN, NULL);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002978 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002979 }
Fred Drake8f51f542001-10-04 14:48:42 +00002980 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002981 if (why == WHY_EXCEPTION)
2982 call_trace_protected(tstate->c_profilefunc,
2983 tstate->c_profileobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002984 PyTrace_RETURN, NULL);
Fred Drake4ec5d562001-10-04 19:26:43 +00002985 else if (call_trace(tstate->c_profilefunc,
2986 tstate->c_profileobj, f,
2987 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002988 Py_XDECREF(retval);
2989 retval = NULL;
2990 why = WHY_EXCEPTION;
2991 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002992 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002993 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002994
Tim Peters7df5e7f2006-05-26 23:14:37 +00002995 if (tstate->frame->f_exc_type != NULL)
2996 reset_exc_info(tstate);
2997 else {
2998 assert(tstate->frame->f_exc_value == NULL);
2999 assert(tstate->frame->f_exc_traceback == NULL);
3000 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003001
Tim Peters5ca576e2001-06-18 22:08:13 +00003002 /* pop frame */
Thomas Woutersae406c62007-09-19 17:27:43 +00003003exit_eval_frame:
Armin Rigo2b3eb402003-10-28 12:05:48 +00003004 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00003005 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003006
Guido van Rossum96a42c81992-01-12 02:29:51 +00003007 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00003008}
3009
Guido van Rossumc2e20742006-02-27 22:32:47 +00003010/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003011 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003012 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003013
Tim Peters6d6c1a32001-08-02 04:15:00 +00003014PyObject *
3015PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00003016 PyObject **args, int argcount, PyObject **kws, int kwcount,
3017 PyObject **defs, int defcount, PyObject *closure)
3018{
3019 register PyFrameObject *f;
3020 register PyObject *retval = NULL;
3021 register PyObject **fastlocals, **freevars;
3022 PyThreadState *tstate = PyThreadState_GET();
3023 PyObject *x, *u;
3024
3025 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00003026 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00003027 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00003028 return NULL;
3029 }
3030
Neal Norwitzdf6a6492006-08-13 18:10:10 +00003031 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003032 assert(globals != NULL);
3033 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00003034 if (f == NULL)
3035 return NULL;
3036
3037 fastlocals = f->f_localsplus;
Richard Jonescebbefc2006-05-23 18:28:17 +00003038 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003039
3040 if (co->co_argcount > 0 ||
3041 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
3042 int i;
3043 int n = argcount;
3044 PyObject *kwdict = NULL;
3045 if (co->co_flags & CO_VARKEYWORDS) {
3046 kwdict = PyDict_New();
3047 if (kwdict == NULL)
3048 goto fail;
3049 i = co->co_argcount;
3050 if (co->co_flags & CO_VARARGS)
3051 i++;
3052 SETLOCAL(i, kwdict);
3053 }
3054 if (argcount > co->co_argcount) {
3055 if (!(co->co_flags & CO_VARARGS)) {
3056 PyErr_Format(PyExc_TypeError,
3057 "%.200s() takes %s %d "
3058 "%sargument%s (%d given)",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003059 PyString_AsString(co->co_name),
Tim Peters5ca576e2001-06-18 22:08:13 +00003060 defcount ? "at most" : "exactly",
3061 co->co_argcount,
3062 kwcount ? "non-keyword " : "",
3063 co->co_argcount == 1 ? "" : "s",
3064 argcount);
3065 goto fail;
3066 }
3067 n = co->co_argcount;
3068 }
3069 for (i = 0; i < n; i++) {
3070 x = args[i];
3071 Py_INCREF(x);
3072 SETLOCAL(i, x);
3073 }
3074 if (co->co_flags & CO_VARARGS) {
3075 u = PyTuple_New(argcount - n);
3076 if (u == NULL)
3077 goto fail;
3078 SETLOCAL(co->co_argcount, u);
3079 for (i = n; i < argcount; i++) {
3080 x = args[i];
3081 Py_INCREF(x);
3082 PyTuple_SET_ITEM(u, i-n, x);
3083 }
3084 }
3085 for (i = 0; i < kwcount; i++) {
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00003086 PyObject **co_varnames;
Tim Peters5ca576e2001-06-18 22:08:13 +00003087 PyObject *keyword = kws[2*i];
3088 PyObject *value = kws[2*i + 1];
3089 int j;
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003090 if (keyword == NULL || !(PyString_Check(keyword)
3091#ifdef Py_USING_UNICODE
3092 || PyUnicode_Check(keyword)
3093#endif
3094 )) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003095 PyErr_Format(PyExc_TypeError,
3096 "%.200s() keywords must be strings",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003097 PyString_AsString(co->co_name));
Tim Peters5ca576e2001-06-18 22:08:13 +00003098 goto fail;
3099 }
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00003100 /* Speed hack: do raw pointer compares. As names are
3101 normally interned this should almost always hit. */
3102 co_varnames = PySequence_Fast_ITEMS(co->co_varnames);
Tim Peters5ca576e2001-06-18 22:08:13 +00003103 for (j = 0; j < co->co_argcount; j++) {
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00003104 PyObject *nm = co_varnames[j];
3105 if (nm == keyword)
3106 goto kw_found;
3107 }
3108 /* Slow fallback, just in case */
3109 for (j = 0; j < co->co_argcount; j++) {
3110 PyObject *nm = co_varnames[j];
Tim Peters5ca576e2001-06-18 22:08:13 +00003111 int cmp = PyObject_RichCompareBool(
3112 keyword, nm, Py_EQ);
3113 if (cmp > 0)
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00003114 goto kw_found;
Tim Peters5ca576e2001-06-18 22:08:13 +00003115 else if (cmp < 0)
3116 goto fail;
3117 }
3118 /* Check errors from Compare */
3119 if (PyErr_Occurred())
3120 goto fail;
3121 if (j >= co->co_argcount) {
3122 if (kwdict == NULL) {
Benjamin Petersone18ef192009-01-20 14:21:16 +00003123 PyObject *kwd_str = kwd_as_string(keyword);
3124 if (kwd_str) {
3125 PyErr_Format(PyExc_TypeError,
3126 "%.200s() got an unexpected "
3127 "keyword argument '%.400s'",
3128 PyString_AsString(co->co_name),
3129 PyString_AsString(kwd_str));
3130 Py_DECREF(kwd_str);
3131 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003132 goto fail;
3133 }
3134 PyDict_SetItem(kwdict, keyword, value);
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00003135 continue;
Tim Peters5ca576e2001-06-18 22:08:13 +00003136 }
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00003137kw_found:
3138 if (GETLOCAL(j) != NULL) {
Benjamin Petersone18ef192009-01-20 14:21:16 +00003139 PyObject *kwd_str = kwd_as_string(keyword);
3140 if (kwd_str) {
3141 PyErr_Format(PyExc_TypeError,
3142 "%.200s() got multiple "
3143 "values for keyword "
3144 "argument '%.400s'",
3145 PyString_AsString(co->co_name),
3146 PyString_AsString(kwd_str));
3147 Py_DECREF(kwd_str);
3148 }
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00003149 goto fail;
Tim Peters5ca576e2001-06-18 22:08:13 +00003150 }
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00003151 Py_INCREF(value);
3152 SETLOCAL(j, value);
Tim Peters5ca576e2001-06-18 22:08:13 +00003153 }
3154 if (argcount < co->co_argcount) {
3155 int m = co->co_argcount - defcount;
3156 for (i = argcount; i < m; i++) {
3157 if (GETLOCAL(i) == NULL) {
3158 PyErr_Format(PyExc_TypeError,
3159 "%.200s() takes %s %d "
3160 "%sargument%s (%d given)",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003161 PyString_AsString(co->co_name),
Tim Peters5ca576e2001-06-18 22:08:13 +00003162 ((co->co_flags & CO_VARARGS) ||
3163 defcount) ? "at least"
3164 : "exactly",
3165 m, kwcount ? "non-keyword " : "",
3166 m == 1 ? "" : "s", i);
3167 goto fail;
3168 }
3169 }
3170 if (n > m)
3171 i = n - m;
3172 else
3173 i = 0;
3174 for (; i < defcount; i++) {
3175 if (GETLOCAL(m+i) == NULL) {
3176 PyObject *def = defs[i];
3177 Py_INCREF(def);
3178 SETLOCAL(m+i, def);
3179 }
3180 }
3181 }
3182 }
3183 else {
3184 if (argcount > 0 || kwcount > 0) {
3185 PyErr_Format(PyExc_TypeError,
3186 "%.200s() takes no arguments (%d given)",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003187 PyString_AsString(co->co_name),
Tim Peters5ca576e2001-06-18 22:08:13 +00003188 argcount + kwcount);
3189 goto fail;
3190 }
3191 }
3192 /* Allocate and initialize storage for cell vars, and copy free
3193 vars into frame. This isn't too efficient right now. */
Richard Jonescebbefc2006-05-23 18:28:17 +00003194 if (PyTuple_GET_SIZE(co->co_cellvars)) {
Neal Norwitz245ce8d2006-06-12 02:16:10 +00003195 int i, j, nargs, found;
Tim Peters5ca576e2001-06-18 22:08:13 +00003196 char *cellname, *argname;
3197 PyObject *c;
3198
3199 nargs = co->co_argcount;
3200 if (co->co_flags & CO_VARARGS)
3201 nargs++;
3202 if (co->co_flags & CO_VARKEYWORDS)
3203 nargs++;
3204
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003205 /* Initialize each cell var, taking into account
3206 cell vars that are initialized from arguments.
3207
3208 Should arrange for the compiler to put cellvars
3209 that are arguments at the beginning of the cellvars
3210 list so that we can march over it more efficiently?
3211 */
Richard Jonescebbefc2006-05-23 18:28:17 +00003212 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003213 cellname = PyString_AS_STRING(
Tim Peters5ca576e2001-06-18 22:08:13 +00003214 PyTuple_GET_ITEM(co->co_cellvars, i));
3215 found = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003216 for (j = 0; j < nargs; j++) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003217 argname = PyString_AS_STRING(
Tim Peters5ca576e2001-06-18 22:08:13 +00003218 PyTuple_GET_ITEM(co->co_varnames, j));
3219 if (strcmp(cellname, argname) == 0) {
3220 c = PyCell_New(GETLOCAL(j));
3221 if (c == NULL)
3222 goto fail;
Richard Jonescebbefc2006-05-23 18:28:17 +00003223 GETLOCAL(co->co_nlocals + i) = c;
Tim Peters5ca576e2001-06-18 22:08:13 +00003224 found = 1;
3225 break;
3226 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003227 }
3228 if (found == 0) {
3229 c = PyCell_New(NULL);
3230 if (c == NULL)
3231 goto fail;
Richard Jonescebbefc2006-05-23 18:28:17 +00003232 SETLOCAL(co->co_nlocals + i, c);
Tim Peters5ca576e2001-06-18 22:08:13 +00003233 }
3234 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003235 }
Richard Jonescebbefc2006-05-23 18:28:17 +00003236 if (PyTuple_GET_SIZE(co->co_freevars)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003237 int i;
Richard Jonescebbefc2006-05-23 18:28:17 +00003238 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003239 PyObject *o = PyTuple_GET_ITEM(closure, i);
3240 Py_INCREF(o);
Richard Jonescebbefc2006-05-23 18:28:17 +00003241 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Tim Peters5ca576e2001-06-18 22:08:13 +00003242 }
3243 }
3244
Tim Peters5ca576e2001-06-18 22:08:13 +00003245 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003246 /* Don't need to keep the reference to f_back, it will be set
3247 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00003248 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003249 f->f_back = NULL;
3250
Jeremy Hylton985eba52003-02-05 23:13:00 +00003251 PCALL(PCALL_GENERATOR);
3252
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003253 /* Create a new generator that owns the ready to run frame
3254 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00003255 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00003256 }
3257
Thomas Woutersae406c62007-09-19 17:27:43 +00003258 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003259
Thomas Woutersae406c62007-09-19 17:27:43 +00003260fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003261
Tim Petersb13680b2001-11-27 23:29:29 +00003262 /* decref'ing the frame can cause __del__ methods to get invoked,
3263 which can call back into Python. While we're done with the
3264 current Python frame (f), the associated C stack is still in use,
3265 so recursion_depth must be boosted for the duration.
3266 */
3267 assert(tstate != NULL);
3268 ++tstate->recursion_depth;
Thomas Woutersae406c62007-09-19 17:27:43 +00003269 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00003270 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00003271 return retval;
3272}
3273
3274
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00003275static PyObject *
3276special_lookup(PyObject *o, char *meth, PyObject **cache)
3277{
3278 PyObject *res;
3279 if (PyInstance_Check(o)) {
3280 if (!*cache)
3281 return PyObject_GetAttrString(o, meth);
3282 else
3283 return PyObject_GetAttr(o, *cache);
3284 }
3285 res = _PyObject_LookupSpecial(o, meth, cache);
3286 if (res == NULL && !PyErr_Occurred()) {
3287 PyErr_SetObject(PyExc_AttributeError, *cache);
3288 return NULL;
3289 }
3290 return res;
3291}
3292
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003293
Benjamin Petersone18ef192009-01-20 14:21:16 +00003294static PyObject *
3295kwd_as_string(PyObject *kwd) {
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003296#ifdef Py_USING_UNICODE
Benjamin Petersone18ef192009-01-20 14:21:16 +00003297 if (PyString_Check(kwd)) {
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003298#else
3299 assert(PyString_Check(kwd));
3300#endif
Benjamin Petersone18ef192009-01-20 14:21:16 +00003301 Py_INCREF(kwd);
3302 return kwd;
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003303#ifdef Py_USING_UNICODE
Benjamin Petersone18ef192009-01-20 14:21:16 +00003304 }
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003305 return _PyUnicode_AsDefaultEncodedString(kwd, "replace");
3306#endif
Benjamin Petersone18ef192009-01-20 14:21:16 +00003307}
3308
3309
Guido van Rossumc9fbb722003-03-01 03:36:33 +00003310/* Implementation notes for set_exc_info() and reset_exc_info():
3311
3312- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
3313 'exc_traceback'. These always travel together.
3314
3315- tstate->curexc_ZZZ is the "hot" exception that is set by
3316 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
3317
3318- Once an exception is caught by an except clause, it is transferred
3319 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
3320 can pick it up. This is the primary task of set_exc_info().
Tim Peters7df5e7f2006-05-26 23:14:37 +00003321 XXX That can't be right: set_exc_info() doesn't look at tstate->curexc_ZZZ.
Guido van Rossumc9fbb722003-03-01 03:36:33 +00003322
3323- Now let me explain the complicated dance with frame->f_exc_ZZZ.
3324
3325 Long ago, when none of this existed, there were just a few globals:
3326 one set corresponding to the "hot" exception, and one set
3327 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
3328 globals; they were simply stored as sys.exc_ZZZ. For backwards
3329 compatibility, they still are!) The problem was that in code like
3330 this:
3331
3332 try:
3333 "something that may fail"
3334 except "some exception":
3335 "do something else first"
3336 "print the exception from sys.exc_ZZZ."
3337
3338 if "do something else first" invoked something that raised and caught
3339 an exception, sys.exc_ZZZ were overwritten. That was a frequent
3340 cause of subtle bugs. I fixed this by changing the semantics as
3341 follows:
3342
3343 - Within one frame, sys.exc_ZZZ will hold the last exception caught
3344 *in that frame*.
3345
3346 - But initially, and as long as no exception is caught in a given
3347 frame, sys.exc_ZZZ will hold the last exception caught in the
3348 previous frame (or the frame before that, etc.).
3349
3350 The first bullet fixed the bug in the above example. The second
3351 bullet was for backwards compatibility: it was (and is) common to
3352 have a function that is called when an exception is caught, and to
3353 have that function access the caught exception via sys.exc_ZZZ.
3354 (Example: traceback.print_exc()).
3355
3356 At the same time I fixed the problem that sys.exc_ZZZ weren't
3357 thread-safe, by introducing sys.exc_info() which gets it from tstate;
3358 but that's really a separate improvement.
3359
3360 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
3361 variables to what they were before the current frame was called. The
3362 set_exc_info() function saves them on the frame so that
3363 reset_exc_info() can restore them. The invariant is that
3364 frame->f_exc_ZZZ is NULL iff the current frame never caught an
3365 exception (where "catching" an exception applies only to successful
3366 except clauses); and if the current frame ever caught an exception,
3367 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
3368 at the start of the current frame.
3369
3370*/
3371
Fredrik Lundh7a830892006-05-27 10:39:48 +00003372static void
Guido van Rossumac7be682001-01-17 15:42:30 +00003373set_exc_info(PyThreadState *tstate,
3374 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00003375{
Tim Peters7df5e7f2006-05-26 23:14:37 +00003376 PyFrameObject *frame = tstate->frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003377 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00003378
Tim Peters7df5e7f2006-05-26 23:14:37 +00003379 assert(type != NULL);
3380 assert(frame != NULL);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003381 if (frame->f_exc_type == NULL) {
Tim Peters7df5e7f2006-05-26 23:14:37 +00003382 assert(frame->f_exc_value == NULL);
3383 assert(frame->f_exc_traceback == NULL);
3384 /* This frame didn't catch an exception before. */
3385 /* Save previous exception of this thread in this frame. */
Guido van Rossuma027efa1997-05-05 20:56:21 +00003386 if (tstate->exc_type == NULL) {
Tim Peters7df5e7f2006-05-26 23:14:37 +00003387 /* XXX Why is this set to Py_None? */
Guido van Rossuma027efa1997-05-05 20:56:21 +00003388 Py_INCREF(Py_None);
3389 tstate->exc_type = Py_None;
3390 }
Tim Peters7df5e7f2006-05-26 23:14:37 +00003391 Py_INCREF(tstate->exc_type);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003392 Py_XINCREF(tstate->exc_value);
3393 Py_XINCREF(tstate->exc_traceback);
3394 frame->f_exc_type = tstate->exc_type;
3395 frame->f_exc_value = tstate->exc_value;
3396 frame->f_exc_traceback = tstate->exc_traceback;
3397 }
Tim Peters7df5e7f2006-05-26 23:14:37 +00003398 /* Set new exception for this thread. */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003399 tmp_type = tstate->exc_type;
3400 tmp_value = tstate->exc_value;
3401 tmp_tb = tstate->exc_traceback;
Tim Peters7df5e7f2006-05-26 23:14:37 +00003402 Py_INCREF(type);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003403 Py_XINCREF(value);
3404 Py_XINCREF(tb);
3405 tstate->exc_type = type;
3406 tstate->exc_value = value;
3407 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003408 Py_XDECREF(tmp_type);
3409 Py_XDECREF(tmp_value);
3410 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003411 /* For b/w compatibility */
3412 PySys_SetObject("exc_type", type);
3413 PySys_SetObject("exc_value", value);
3414 PySys_SetObject("exc_traceback", tb);
3415}
3416
Fredrik Lundh7a830892006-05-27 10:39:48 +00003417static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003418reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00003419{
3420 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003421 PyObject *tmp_type, *tmp_value, *tmp_tb;
Tim Peters7df5e7f2006-05-26 23:14:37 +00003422
3423 /* It's a precondition that the thread state's frame caught an
3424 * exception -- verify in a debug build.
3425 */
3426 assert(tstate != NULL);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003427 frame = tstate->frame;
Tim Peters7df5e7f2006-05-26 23:14:37 +00003428 assert(frame != NULL);
3429 assert(frame->f_exc_type != NULL);
3430
3431 /* Copy the frame's exception info back to the thread state. */
3432 tmp_type = tstate->exc_type;
3433 tmp_value = tstate->exc_value;
3434 tmp_tb = tstate->exc_traceback;
3435 Py_INCREF(frame->f_exc_type);
3436 Py_XINCREF(frame->f_exc_value);
3437 Py_XINCREF(frame->f_exc_traceback);
3438 tstate->exc_type = frame->f_exc_type;
3439 tstate->exc_value = frame->f_exc_value;
3440 tstate->exc_traceback = frame->f_exc_traceback;
3441 Py_XDECREF(tmp_type);
3442 Py_XDECREF(tmp_value);
3443 Py_XDECREF(tmp_tb);
3444
3445 /* For b/w compatibility */
3446 PySys_SetObject("exc_type", frame->f_exc_type);
3447 PySys_SetObject("exc_value", frame->f_exc_value);
3448 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
3449
3450 /* Clear the frame's exception info. */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003451 tmp_type = frame->f_exc_type;
3452 tmp_value = frame->f_exc_value;
3453 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003454 frame->f_exc_type = NULL;
3455 frame->f_exc_value = NULL;
3456 frame->f_exc_traceback = NULL;
Tim Peters7df5e7f2006-05-26 23:14:37 +00003457 Py_DECREF(tmp_type);
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003458 Py_XDECREF(tmp_value);
3459 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003460}
3461
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003462/* Logic for the raise statement (too complicated for inlining).
3463 This *consumes* a reference count to each of its arguments. */
Fredrik Lundh7a830892006-05-27 10:39:48 +00003464static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003465do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003466{
Guido van Rossumd295f121998-04-09 21:39:57 +00003467 if (type == NULL) {
3468 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003469 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumd295f121998-04-09 21:39:57 +00003470 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
3471 value = tstate->exc_value;
3472 tb = tstate->exc_traceback;
3473 Py_XINCREF(type);
3474 Py_XINCREF(value);
3475 Py_XINCREF(tb);
3476 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003477
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003478 /* We support the following forms of raise:
3479 raise <class>, <classinstance>
3480 raise <class>, <argument tuple>
3481 raise <class>, None
3482 raise <class>, <argument>
3483 raise <classinstance>, None
3484 raise <string>, <object>
3485 raise <string>, None
3486
3487 An omitted second argument is the same as None.
3488
3489 In addition, raise <tuple>, <anything> is the same as
3490 raising the tuple's first item (and it better have one!);
3491 this rule is applied recursively.
3492
3493 Finally, an optional third argument can be supplied, which
3494 gives the traceback to be substituted (useful when
3495 re-raising an exception after examining it). */
3496
3497 /* First, check the traceback argument, replacing None with
3498 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00003499 if (tb == Py_None) {
3500 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003501 tb = NULL;
3502 }
3503 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003504 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003505 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003506 goto raise_error;
3507 }
3508
3509 /* Next, replace a missing value with None */
3510 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003511 value = Py_None;
3512 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003513 }
3514
3515 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00003516 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
3517 PyObject *tmp = type;
3518 type = PyTuple_GET_ITEM(type, 0);
3519 Py_INCREF(type);
3520 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003521 }
3522
Brett Cannon129bd522007-01-30 21:34:36 +00003523 if (PyExceptionClass_Check(type))
Barry Warsaw4249f541997-08-22 21:26:19 +00003524 PyErr_NormalizeException(&type, &value, &tb);
3525
Brett Cannonbf364092006-03-01 04:25:17 +00003526 else if (PyExceptionInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003527 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00003528 if (value != Py_None) {
3529 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003530 "instance exception may not have a separate value");
3531 goto raise_error;
3532 }
3533 else {
3534 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00003535 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003536 value = type;
Brett Cannonbf364092006-03-01 04:25:17 +00003537 type = PyExceptionInstance_Class(type);
Guido van Rossumb209a111997-04-29 18:18:01 +00003538 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003539 }
3540 }
3541 else {
3542 /* Not something you can raise. You get an exception
3543 anyway, just not what you specified :-) */
Jeremy Hylton960d9482001-04-27 02:25:33 +00003544 PyErr_Format(PyExc_TypeError,
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00003545 "exceptions must be classes or instances, not %s",
3546 type->ob_type->tp_name);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003547 goto raise_error;
3548 }
Guido van Rossum504153d2008-03-18 04:26:48 +00003549
3550 assert(PyExceptionClass_Check(type));
3551 if (Py_Py3kWarningFlag && PyClass_Check(type)) {
Benjamin Peterson9f4f4812008-04-27 03:01:45 +00003552 if (PyErr_WarnEx(PyExc_DeprecationWarning,
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00003553 "exceptions must derive from BaseException "
3554 "in 3.x", 1) < 0)
Guido van Rossum504153d2008-03-18 04:26:48 +00003555 goto raise_error;
3556 }
3557
Guido van Rossumb209a111997-04-29 18:18:01 +00003558 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003559 if (tb == NULL)
3560 return WHY_EXCEPTION;
3561 else
3562 return WHY_RERAISE;
3563 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00003564 Py_XDECREF(value);
3565 Py_XDECREF(type);
3566 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003567 return WHY_EXCEPTION;
3568}
3569
Tim Petersd6d010b2001-06-21 02:49:55 +00003570/* Iterate v argcnt times and store the results on the stack (via decreasing
3571 sp). Return 1 for success, 0 if error. */
3572
Fredrik Lundh7a830892006-05-27 10:39:48 +00003573static int
Tim Petersd6d010b2001-06-21 02:49:55 +00003574unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003575{
Tim Petersd6d010b2001-06-21 02:49:55 +00003576 int i = 0;
3577 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00003578 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00003579
Tim Petersd6d010b2001-06-21 02:49:55 +00003580 assert(v != NULL);
3581
3582 it = PyObject_GetIter(v);
3583 if (it == NULL)
3584 goto Error;
3585
3586 for (; i < argcnt; i++) {
3587 w = PyIter_Next(it);
3588 if (w == NULL) {
3589 /* Iterator done, via error or exhaustion. */
3590 if (!PyErr_Occurred()) {
3591 PyErr_Format(PyExc_ValueError,
3592 "need more than %d value%s to unpack",
3593 i, i == 1 ? "" : "s");
3594 }
3595 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003596 }
3597 *--sp = w;
3598 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003599
3600 /* We better have exhausted the iterator now. */
3601 w = PyIter_Next(it);
3602 if (w == NULL) {
3603 if (PyErr_Occurred())
3604 goto Error;
3605 Py_DECREF(it);
3606 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003607 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00003608 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00003609 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00003610 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00003611Error:
Barry Warsaw91010551997-08-25 22:30:51 +00003612 for (; i > 0; i--, sp++)
3613 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00003614 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00003615 return 0;
3616}
3617
3618
Guido van Rossum96a42c81992-01-12 02:29:51 +00003619#ifdef LLTRACE
Fredrik Lundh7a830892006-05-27 10:39:48 +00003620static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003621prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003622{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003623 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003624 if (PyObject_Print(v, stdout, 0) != 0)
3625 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003626 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003627 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003628}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003629#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003630
Fredrik Lundh7a830892006-05-27 10:39:48 +00003631static void
Fred Drake5755ce62001-06-27 19:19:46 +00003632call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003633{
Guido van Rossumb209a111997-04-29 18:18:01 +00003634 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003635 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003636 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003637 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003638 value = Py_None;
3639 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003640 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003641 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003642 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003643 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003644 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003645 }
Fred Drake5755ce62001-06-27 19:19:46 +00003646 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003647 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003648 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003649 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003650 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003651 Py_XDECREF(type);
3652 Py_XDECREF(value);
3653 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003654 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003655}
3656
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +00003657static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003658call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003659 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003660{
3661 PyObject *type, *value, *traceback;
3662 int err;
3663 PyErr_Fetch(&type, &value, &traceback);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003664 err = call_trace(func, obj, frame, what, arg);
Fred Drake4ec5d562001-10-04 19:26:43 +00003665 if (err == 0)
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +00003666 {
Fred Drake4ec5d562001-10-04 19:26:43 +00003667 PyErr_Restore(type, value, traceback);
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +00003668 return 0;
3669 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003670 else {
3671 Py_XDECREF(type);
3672 Py_XDECREF(value);
3673 Py_XDECREF(traceback);
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +00003674 return -1;
Fred Drake4ec5d562001-10-04 19:26:43 +00003675 }
3676}
3677
Fredrik Lundh7a830892006-05-27 10:39:48 +00003678static int
Fred Drake5755ce62001-06-27 19:19:46 +00003679call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3680 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003681{
Fred Drake5755ce62001-06-27 19:19:46 +00003682 register PyThreadState *tstate = frame->f_tstate;
3683 int result;
3684 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003685 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003686 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003687 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003688 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003689 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3690 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003691 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003692 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003693}
3694
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003695PyObject *
3696_PyEval_CallTracing(PyObject *func, PyObject *args)
3697{
3698 PyFrameObject *frame = PyEval_GetFrame();
3699 PyThreadState *tstate = frame->f_tstate;
3700 int save_tracing = tstate->tracing;
3701 int save_use_tracing = tstate->use_tracing;
3702 PyObject *result;
3703
3704 tstate->tracing = 0;
3705 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3706 || (tstate->c_profilefunc != NULL));
3707 result = PyObject_Call(func, args, NULL);
3708 tstate->tracing = save_tracing;
3709 tstate->use_tracing = save_use_tracing;
3710 return result;
3711}
3712
Jeffrey Yasskin655d8352009-05-23 23:23:01 +00003713/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Fredrik Lundh7a830892006-05-27 10:39:48 +00003714static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003715maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003716 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3717 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003718{
Michael W. Hudson006c7522002-11-08 13:08:46 +00003719 int result = 0;
Jeffrey Yasskin655d8352009-05-23 23:23:01 +00003720 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003721
Jeremy Hyltona4ebc132006-04-18 14:47:00 +00003722 /* If the last instruction executed isn't in the current
Jeffrey Yasskin655d8352009-05-23 23:23:01 +00003723 instruction window, reset the window.
Jeremy Hyltona4ebc132006-04-18 14:47:00 +00003724 */
Jeffrey Yasskin655d8352009-05-23 23:23:01 +00003725 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
Thomas Woutersae406c62007-09-19 17:27:43 +00003726 PyAddrPair bounds;
Jeffrey Yasskin655d8352009-05-23 23:23:01 +00003727 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3728 &bounds);
Thomas Woutersae406c62007-09-19 17:27:43 +00003729 *instr_lb = bounds.ap_lower;
3730 *instr_ub = bounds.ap_upper;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003731 }
Jeffrey Yasskin655d8352009-05-23 23:23:01 +00003732 /* If the last instruction falls at the start of a line or if
3733 it represents a jump backwards, update the frame's line
3734 number and call the trace function. */
3735 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
3736 frame->f_lineno = line;
Jeremy Hyltona4ebc132006-04-18 14:47:00 +00003737 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
Armin Rigobf57a142004-03-22 19:24:58 +00003738 }
3739 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003740 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003741}
3742
Fred Drake5755ce62001-06-27 19:19:46 +00003743void
3744PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003745{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003746 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003747 PyObject *temp = tstate->c_profileobj;
3748 Py_XINCREF(arg);
3749 tstate->c_profilefunc = NULL;
3750 tstate->c_profileobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003751 /* Must make sure that tracing is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003752 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003753 Py_XDECREF(temp);
3754 tstate->c_profilefunc = func;
3755 tstate->c_profileobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003756 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003757 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003758}
3759
3760void
3761PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3762{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003763 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003764 PyObject *temp = tstate->c_traceobj;
Jeffrey Yasskinfd8a1ec2008-12-03 06:46:45 +00003765 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003766 Py_XINCREF(arg);
3767 tstate->c_tracefunc = NULL;
3768 tstate->c_traceobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003769 /* Must make sure that profiling is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003770 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003771 Py_XDECREF(temp);
3772 tstate->c_tracefunc = func;
3773 tstate->c_traceobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003774 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003775 tstate->use_tracing = ((func != NULL)
3776 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003777}
3778
Guido van Rossumb209a111997-04-29 18:18:01 +00003779PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003780PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003781{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003782 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003783 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003784 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003785 else
3786 return current_frame->f_builtins;
3787}
3788
Guido van Rossumb209a111997-04-29 18:18:01 +00003789PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003790PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003791{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003792 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003793 if (current_frame == NULL)
3794 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003795 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003796 return current_frame->f_locals;
3797}
3798
Guido van Rossumb209a111997-04-29 18:18:01 +00003799PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003800PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003801{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003802 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003803 if (current_frame == NULL)
3804 return NULL;
3805 else
3806 return current_frame->f_globals;
3807}
3808
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003809PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003810PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003811{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003812 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003813 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003814}
3815
Guido van Rossum6135a871995-01-09 17:53:26 +00003816int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003817PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003818{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003819 PyFrameObject *current_frame = PyEval_GetFrame();
Neal Norwitzb9845e72006-06-12 02:11:18 +00003820 return current_frame == NULL ? 0 : PyFrame_IsRestricted(current_frame);
Guido van Rossum6135a871995-01-09 17:53:26 +00003821}
3822
Guido van Rossumbe270261997-05-22 22:26:18 +00003823int
Tim Peters5ba58662001-07-16 02:29:45 +00003824PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003825{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003826 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003827 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003828
3829 if (current_frame != NULL) {
3830 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003831 const int compilerflags = codeflags & PyCF_MASK;
3832 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003833 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003834 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003835 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003836#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003837 if (codeflags & CO_GENERATOR_ALLOWED) {
3838 result = 1;
3839 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3840 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003841#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003842 }
3843 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003844}
3845
3846int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003847Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003848{
Guido van Rossumb209a111997-04-29 18:18:01 +00003849 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003850 if (f == NULL)
3851 return 0;
3852 if (!PyFile_SoftSpace(f, 0))
3853 return 0;
3854 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003855}
3856
Guido van Rossum3f5da241990-12-20 15:06:42 +00003857
Guido van Rossum681d79a1995-07-18 14:51:37 +00003858/* External interface to call any callable object.
3859 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003860
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003861#undef PyEval_CallObject
3862/* for backward compatibility: export this interface */
3863
Guido van Rossumb209a111997-04-29 18:18:01 +00003864PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003865PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003866{
Guido van Rossumb209a111997-04-29 18:18:01 +00003867 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003868}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003869#define PyEval_CallObject(func,arg) \
3870 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003871
Guido van Rossumb209a111997-04-29 18:18:01 +00003872PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003873PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003874{
Jeremy Hylton52820442001-01-03 23:52:36 +00003875 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003876
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003877 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003878 arg = PyTuple_New(0);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003879 if (arg == NULL)
3880 return NULL;
3881 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003882 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003883 PyErr_SetString(PyExc_TypeError,
3884 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003885 return NULL;
3886 }
3887 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003888 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003889
Guido van Rossumb209a111997-04-29 18:18:01 +00003890 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003891 PyErr_SetString(PyExc_TypeError,
3892 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003893 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003894 return NULL;
3895 }
3896
Tim Peters6d6c1a32001-08-02 04:15:00 +00003897 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003898 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003899 return result;
3900}
3901
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003902const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003903PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003904{
3905 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003906 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003907 else if (PyFunction_Check(func))
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003908 return PyString_AsString(((PyFunctionObject*)func)->func_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00003909 else if (PyCFunction_Check(func))
3910 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3911 else if (PyClass_Check(func))
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003912 return PyString_AsString(((PyClassObject*)func)->cl_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00003913 else if (PyInstance_Check(func)) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003914 return PyString_AsString(
Jeremy Hylton512a2372001-04-11 13:52:29 +00003915 ((PyInstanceObject*)func)->in_class->cl_name);
3916 } else {
3917 return func->ob_type->tp_name;
3918 }
3919}
3920
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003921const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003922PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003923{
3924 if (PyMethod_Check(func))
3925 return "()";
3926 else if (PyFunction_Check(func))
3927 return "()";
3928 else if (PyCFunction_Check(func))
3929 return "()";
3930 else if (PyClass_Check(func))
3931 return " constructor";
3932 else if (PyInstance_Check(func)) {
3933 return " instance";
3934 } else {
3935 return " object";
3936 }
3937}
3938
Fredrik Lundh7a830892006-05-27 10:39:48 +00003939static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003940err_args(PyObject *func, int flags, int nargs)
3941{
3942 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003943 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003944 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003945 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003946 nargs);
3947 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003948 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003949 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003950 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003951 nargs);
3952}
3953
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003954#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003955if (tstate->use_tracing && tstate->c_profilefunc) { \
3956 if (call_trace(tstate->c_profilefunc, \
3957 tstate->c_profileobj, \
3958 tstate->frame, PyTrace_C_CALL, \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003959 func)) { \
3960 x = NULL; \
3961 } \
3962 else { \
3963 x = call; \
3964 if (tstate->c_profilefunc != NULL) { \
3965 if (x == NULL) { \
3966 call_trace_protected(tstate->c_profilefunc, \
3967 tstate->c_profileobj, \
3968 tstate->frame, PyTrace_C_EXCEPTION, \
3969 func); \
3970 /* XXX should pass (type, value, tb) */ \
3971 } else { \
3972 if (call_trace(tstate->c_profilefunc, \
3973 tstate->c_profileobj, \
3974 tstate->frame, PyTrace_C_RETURN, \
3975 func)) { \
3976 Py_DECREF(x); \
3977 x = NULL; \
3978 } \
3979 } \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003980 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003981 } \
3982} else { \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003983 x = call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003984 }
3985
Fredrik Lundh7a830892006-05-27 10:39:48 +00003986static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003987call_function(PyObject ***pp_stack, int oparg
3988#ifdef WITH_TSC
3989 , uint64* pintr0, uint64* pintr1
3990#endif
3991 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003992{
3993 int na = oparg & 0xff;
3994 int nk = (oparg>>8) & 0xff;
3995 int n = na + 2 * nk;
3996 PyObject **pfunc = (*pp_stack) - n - 1;
3997 PyObject *func = *pfunc;
3998 PyObject *x, *w;
3999
Jeremy Hylton985eba52003-02-05 23:13:00 +00004000 /* Always dispatch PyCFunction first, because these are
4001 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004002 */
4003 if (PyCFunction_Check(func) && nk == 0) {
4004 int flags = PyCFunction_GET_FLAGS(func);
Nicholas Bastind858a772004-06-25 23:31:06 +00004005 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00004006
4007 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00004008 if (flags & (METH_NOARGS | METH_O)) {
4009 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
4010 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004011 if (flags & METH_NOARGS && na == 0) {
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004012 C_TRACE(x, (*meth)(self,NULL));
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004013 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00004014 else if (flags & METH_O && na == 1) {
4015 PyObject *arg = EXT_POP(*pp_stack);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004016 C_TRACE(x, (*meth)(self,arg));
Jeremy Hylton192690e2002-08-16 18:36:11 +00004017 Py_DECREF(arg);
4018 }
4019 else {
4020 err_args(func, flags, na);
4021 x = NULL;
4022 }
4023 }
4024 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004025 PyObject *callargs;
4026 callargs = load_args(pp_stack, na);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00004027 READ_TIMESTAMP(*pintr0);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004028 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
Michael W. Hudson75eabd22005-01-18 15:56:11 +00004029 READ_TIMESTAMP(*pintr1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00004030 Py_XDECREF(callargs);
4031 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004032 } else {
4033 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
4034 /* optimize access to bound methods */
4035 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004036 PCALL(PCALL_METHOD);
4037 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004038 Py_INCREF(self);
4039 func = PyMethod_GET_FUNCTION(func);
4040 Py_INCREF(func);
4041 Py_DECREF(*pfunc);
4042 *pfunc = self;
4043 na++;
4044 n++;
4045 } else
4046 Py_INCREF(func);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00004047 READ_TIMESTAMP(*pintr0);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004048 if (PyFunction_Check(func))
4049 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00004050 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004051 x = do_call(func, pp_stack, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00004052 READ_TIMESTAMP(*pintr1);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004053 Py_DECREF(func);
4054 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004055
Armin Rigod34fa522006-03-28 19:10:40 +00004056 /* Clear the stack of the function object. Also removes
4057 the arguments in case they weren't consumed already
4058 (fast_function() and err_args() leave them on the stack).
Thomas Wouters7f597322006-03-01 05:32:33 +00004059 */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004060 while ((*pp_stack) > pfunc) {
4061 w = EXT_POP(*pp_stack);
4062 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004063 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004064 }
4065 return x;
4066}
4067
Jeremy Hylton192690e2002-08-16 18:36:11 +00004068/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00004069 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00004070 For the simplest case -- a function that takes only positional
4071 arguments and is called with only positional arguments -- it
4072 inlines the most primitive frame setup code from
4073 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
4074 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00004075*/
4076
Fredrik Lundh7a830892006-05-27 10:39:48 +00004077static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00004078fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00004079{
Jeremy Hylton985eba52003-02-05 23:13:00 +00004080 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00004081 PyObject *globals = PyFunction_GET_GLOBALS(func);
4082 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
4083 PyObject **d = NULL;
4084 int nd = 0;
4085
Jeremy Hylton985eba52003-02-05 23:13:00 +00004086 PCALL(PCALL_FUNCTION);
4087 PCALL(PCALL_FAST_FUNCTION);
Raymond Hettinger40174c32003-05-31 07:04:16 +00004088 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00004089 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
4090 PyFrameObject *f;
4091 PyObject *retval = NULL;
4092 PyThreadState *tstate = PyThreadState_GET();
4093 PyObject **fastlocals, **stack;
4094 int i;
4095
4096 PCALL(PCALL_FASTER_FUNCTION);
4097 assert(globals != NULL);
4098 /* XXX Perhaps we should create a specialized
4099 PyFrame_New() that doesn't take locals, but does
4100 take builtins without sanity checking them.
4101 */
Neal Norwitzdf6a6492006-08-13 18:10:10 +00004102 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004103 f = PyFrame_New(tstate, co, globals, NULL);
4104 if (f == NULL)
4105 return NULL;
4106
4107 fastlocals = f->f_localsplus;
4108 stack = (*pp_stack) - n;
4109
4110 for (i = 0; i < n; i++) {
4111 Py_INCREF(*stack);
4112 fastlocals[i] = *stack++;
4113 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00004114 retval = PyEval_EvalFrameEx(f,0);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004115 ++tstate->recursion_depth;
4116 Py_DECREF(f);
4117 --tstate->recursion_depth;
4118 return retval;
4119 }
Jeremy Hylton52820442001-01-03 23:52:36 +00004120 if (argdefs != NULL) {
4121 d = &PyTuple_GET_ITEM(argdefs, 0);
Christian Heimese93237d2007-12-19 02:37:44 +00004122 nd = Py_SIZE(argdefs);
Jeremy Hylton52820442001-01-03 23:52:36 +00004123 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00004124 return PyEval_EvalCodeEx(co, globals,
4125 (PyObject *)NULL, (*pp_stack)-n, na,
4126 (*pp_stack)-2*nk, nk, d, nd,
4127 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00004128}
4129
Fredrik Lundh7a830892006-05-27 10:39:48 +00004130static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00004131update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
4132 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00004133{
4134 PyObject *kwdict = NULL;
4135 if (orig_kwdict == NULL)
4136 kwdict = PyDict_New();
4137 else {
4138 kwdict = PyDict_Copy(orig_kwdict);
4139 Py_DECREF(orig_kwdict);
4140 }
4141 if (kwdict == NULL)
4142 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00004143 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00004144 int err;
4145 PyObject *value = EXT_POP(*pp_stack);
4146 PyObject *key = EXT_POP(*pp_stack);
4147 if (PyDict_GetItem(kwdict, key) != NULL) {
Thomas Woutersae406c62007-09-19 17:27:43 +00004148 PyErr_Format(PyExc_TypeError,
4149 "%.200s%s got multiple values "
4150 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00004151 PyEval_GetFuncName(func),
4152 PyEval_GetFuncDesc(func),
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004153 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00004154 Py_DECREF(key);
4155 Py_DECREF(value);
4156 Py_DECREF(kwdict);
4157 return NULL;
4158 }
4159 err = PyDict_SetItem(kwdict, key, value);
4160 Py_DECREF(key);
4161 Py_DECREF(value);
4162 if (err) {
4163 Py_DECREF(kwdict);
4164 return NULL;
4165 }
4166 }
4167 return kwdict;
4168}
4169
Fredrik Lundh7a830892006-05-27 10:39:48 +00004170static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00004171update_star_args(int nstack, int nstar, PyObject *stararg,
4172 PyObject ***pp_stack)
4173{
4174 PyObject *callargs, *w;
4175
4176 callargs = PyTuple_New(nstack + nstar);
4177 if (callargs == NULL) {
4178 return NULL;
4179 }
4180 if (nstar) {
4181 int i;
4182 for (i = 0; i < nstar; i++) {
4183 PyObject *a = PyTuple_GET_ITEM(stararg, i);
4184 Py_INCREF(a);
4185 PyTuple_SET_ITEM(callargs, nstack + i, a);
4186 }
4187 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00004188 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00004189 w = EXT_POP(*pp_stack);
4190 PyTuple_SET_ITEM(callargs, nstack, w);
4191 }
4192 return callargs;
4193}
4194
Fredrik Lundh7a830892006-05-27 10:39:48 +00004195static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00004196load_args(PyObject ***pp_stack, int na)
4197{
4198 PyObject *args = PyTuple_New(na);
4199 PyObject *w;
4200
4201 if (args == NULL)
4202 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00004203 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00004204 w = EXT_POP(*pp_stack);
4205 PyTuple_SET_ITEM(args, na, w);
4206 }
4207 return args;
4208}
4209
Fredrik Lundh7a830892006-05-27 10:39:48 +00004210static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00004211do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4212{
4213 PyObject *callargs = NULL;
4214 PyObject *kwdict = NULL;
4215 PyObject *result = NULL;
4216
4217 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00004218 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00004219 if (kwdict == NULL)
4220 goto call_fail;
4221 }
4222 callargs = load_args(pp_stack, na);
4223 if (callargs == NULL)
4224 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004225#ifdef CALL_PROFILE
4226 /* At this point, we have to look at the type of func to
4227 update the call stats properly. Do it here so as to avoid
4228 exposing the call stats machinery outside ceval.c
4229 */
4230 if (PyFunction_Check(func))
4231 PCALL(PCALL_FUNCTION);
4232 else if (PyMethod_Check(func))
4233 PCALL(PCALL_METHOD);
4234 else if (PyType_Check(func))
4235 PCALL(PCALL_TYPE);
Antoine Pitrou46dbe272009-05-30 21:27:00 +00004236 else if (PyCFunction_Check(func))
4237 PCALL(PCALL_CFUNCTION);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004238 else
4239 PCALL(PCALL_OTHER);
4240#endif
Antoine Pitrou46dbe272009-05-30 21:27:00 +00004241 if (PyCFunction_Check(func)) {
4242 PyThreadState *tstate = PyThreadState_GET();
4243 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4244 }
4245 else
4246 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00004247 call_fail:
4248 Py_XDECREF(callargs);
4249 Py_XDECREF(kwdict);
4250 return result;
4251}
4252
Fredrik Lundh7a830892006-05-27 10:39:48 +00004253static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00004254ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4255{
4256 int nstar = 0;
4257 PyObject *callargs = NULL;
4258 PyObject *stararg = NULL;
4259 PyObject *kwdict = NULL;
4260 PyObject *result = NULL;
4261
4262 if (flags & CALL_FLAG_KW) {
4263 kwdict = EXT_POP(*pp_stack);
Georg Brandl2134e752007-05-21 20:34:16 +00004264 if (!PyDict_Check(kwdict)) {
4265 PyObject *d;
4266 d = PyDict_New();
4267 if (d == NULL)
4268 goto ext_call_fail;
4269 if (PyDict_Update(d, kwdict) != 0) {
4270 Py_DECREF(d);
4271 /* PyDict_Update raises attribute
4272 * error (percolated from an attempt
4273 * to get 'keys' attribute) instead of
4274 * a type error if its second argument
4275 * is not a mapping.
4276 */
4277 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4278 PyErr_Format(PyExc_TypeError,
4279 "%.200s%.200s argument after ** "
4280 "must be a mapping, not %.200s",
4281 PyEval_GetFuncName(func),
4282 PyEval_GetFuncDesc(func),
4283 kwdict->ob_type->tp_name);
4284 }
4285 goto ext_call_fail;
4286 }
4287 Py_DECREF(kwdict);
4288 kwdict = d;
Jeremy Hylton52820442001-01-03 23:52:36 +00004289 }
4290 }
4291 if (flags & CALL_FLAG_VAR) {
4292 stararg = EXT_POP(*pp_stack);
4293 if (!PyTuple_Check(stararg)) {
4294 PyObject *t = NULL;
4295 t = PySequence_Tuple(stararg);
4296 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00004297 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4298 PyErr_Format(PyExc_TypeError,
Georg Brandl2134e752007-05-21 20:34:16 +00004299 "%.200s%.200s argument after * "
4300 "must be a sequence, not %200s",
Tim Peters6d6c1a32001-08-02 04:15:00 +00004301 PyEval_GetFuncName(func),
Georg Brandl2134e752007-05-21 20:34:16 +00004302 PyEval_GetFuncDesc(func),
4303 stararg->ob_type->tp_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00004304 }
Jeremy Hylton52820442001-01-03 23:52:36 +00004305 goto ext_call_fail;
4306 }
4307 Py_DECREF(stararg);
4308 stararg = t;
4309 }
4310 nstar = PyTuple_GET_SIZE(stararg);
4311 }
4312 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00004313 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00004314 if (kwdict == NULL)
4315 goto ext_call_fail;
4316 }
4317 callargs = update_star_args(na, nstar, stararg, pp_stack);
4318 if (callargs == NULL)
4319 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004320#ifdef CALL_PROFILE
4321 /* At this point, we have to look at the type of func to
4322 update the call stats properly. Do it here so as to avoid
4323 exposing the call stats machinery outside ceval.c
4324 */
4325 if (PyFunction_Check(func))
4326 PCALL(PCALL_FUNCTION);
4327 else if (PyMethod_Check(func))
4328 PCALL(PCALL_METHOD);
4329 else if (PyType_Check(func))
4330 PCALL(PCALL_TYPE);
Antoine Pitrou46dbe272009-05-30 21:27:00 +00004331 else if (PyCFunction_Check(func))
4332 PCALL(PCALL_CFUNCTION);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004333 else
4334 PCALL(PCALL_OTHER);
4335#endif
Antoine Pitrou46dbe272009-05-30 21:27:00 +00004336 if (PyCFunction_Check(func)) {
4337 PyThreadState *tstate = PyThreadState_GET();
4338 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4339 }
4340 else
4341 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersae406c62007-09-19 17:27:43 +00004342ext_call_fail:
Jeremy Hylton52820442001-01-03 23:52:36 +00004343 Py_XDECREF(callargs);
4344 Py_XDECREF(kwdict);
4345 Py_XDECREF(stararg);
4346 return result;
4347}
4348
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004349/* Extract a slice index from a PyInt or PyLong or an object with the
4350 nb_index slot defined, and store in *pi.
4351 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4352 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 +00004353 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004354*/
Tim Petersb5196382001-12-16 19:44:20 +00004355/* Note: If v is NULL, return success without storing into *pi. This
4356 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4357 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004358*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004359int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004360_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004361{
Tim Petersb5196382001-12-16 19:44:20 +00004362 if (v != NULL) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004363 Py_ssize_t x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00004364 if (PyInt_Check(v)) {
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00004365 /* XXX(nnorwitz): I think PyInt_AS_LONG is correct,
4366 however, it looks like it should be AsSsize_t.
4367 There should be a comment here explaining why.
4368 */
4369 x = PyInt_AS_LONG(v);
Tim Peters7df5e7f2006-05-26 23:14:37 +00004370 }
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00004371 else if (PyIndex_Check(v)) {
4372 x = PyNumber_AsSsize_t(v, NULL);
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004373 if (x == -1 && PyErr_Occurred())
4374 return 0;
4375 }
4376 else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00004377 PyErr_SetString(PyExc_TypeError,
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004378 "slice indices must be integers or "
4379 "None or have an __index__ method");
Guido van Rossum20c6add2000-05-08 14:06:50 +00004380 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004381 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00004382 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004383 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00004384 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004385}
4386
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004387#undef ISINDEX
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00004388#define ISINDEX(x) ((x) == NULL || \
4389 PyInt_Check(x) || PyLong_Check(x) || PyIndex_Check(x))
Guido van Rossum50d756e2001-08-18 17:43:36 +00004390
Fredrik Lundh7a830892006-05-27 10:39:48 +00004391static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004392apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004393{
Guido van Rossum50d756e2001-08-18 17:43:36 +00004394 PyTypeObject *tp = u->ob_type;
4395 PySequenceMethods *sq = tp->tp_as_sequence;
4396
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004397 if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004398 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
Guido van Rossum50d756e2001-08-18 17:43:36 +00004399 if (!_PyEval_SliceIndex(v, &ilow))
4400 return NULL;
4401 if (!_PyEval_SliceIndex(w, &ihigh))
4402 return NULL;
4403 return PySequence_GetSlice(u, ilow, ihigh);
4404 }
4405 else {
4406 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00004407 if (slice != NULL) {
4408 PyObject *res = PyObject_GetItem(u, slice);
4409 Py_DECREF(slice);
4410 return res;
4411 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00004412 else
4413 return NULL;
4414 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004415}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004416
Fredrik Lundh7a830892006-05-27 10:39:48 +00004417static int
Guido van Rossumac7be682001-01-17 15:42:30 +00004418assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
4419 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004420{
Guido van Rossum50d756e2001-08-18 17:43:36 +00004421 PyTypeObject *tp = u->ob_type;
4422 PySequenceMethods *sq = tp->tp_as_sequence;
4423
Georg Brandl0fca97a2007-03-05 22:28:08 +00004424 if (sq && sq->sq_ass_slice && ISINDEX(v) && ISINDEX(w)) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004425 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
Guido van Rossum50d756e2001-08-18 17:43:36 +00004426 if (!_PyEval_SliceIndex(v, &ilow))
4427 return -1;
4428 if (!_PyEval_SliceIndex(w, &ihigh))
4429 return -1;
4430 if (x == NULL)
4431 return PySequence_DelSlice(u, ilow, ihigh);
4432 else
4433 return PySequence_SetSlice(u, ilow, ihigh, x);
4434 }
4435 else {
4436 PyObject *slice = PySlice_New(v, w, NULL);
4437 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00004438 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00004439 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00004440 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00004441 else
Guido van Rossum354797c2001-12-03 19:45:06 +00004442 res = PyObject_DelItem(u, slice);
4443 Py_DECREF(slice);
4444 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00004445 }
4446 else
4447 return -1;
4448 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004449}
4450
Guido van Rossum04edb522008-03-18 02:49:46 +00004451#define Py3kExceptionClass_Check(x) \
4452 (PyType_Check((x)) && \
4453 PyType_FastSubclass((PyTypeObject*)(x), Py_TPFLAGS_BASE_EXC_SUBCLASS))
4454
4455#define CANNOT_CATCH_MSG "catching classes that don't inherit from " \
Georg Brandld5b635f2008-03-25 08:29:14 +00004456 "BaseException is not allowed in 3.x"
Guido van Rossum04edb522008-03-18 02:49:46 +00004457
Fredrik Lundh7a830892006-05-27 10:39:48 +00004458static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004459cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004460{
Guido van Rossumac7be682001-01-17 15:42:30 +00004461 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004462 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00004463 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00004464 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004465 break;
4466 case PyCmp_IS_NOT:
4467 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004468 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004469 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004470 res = PySequence_Contains(w, v);
4471 if (res < 0)
4472 return NULL;
4473 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004474 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00004475 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004476 if (res < 0)
4477 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004478 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004479 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004480 case PyCmp_EXC_MATCH:
Brett Cannon129bd522007-01-30 21:34:36 +00004481 if (PyTuple_Check(w)) {
4482 Py_ssize_t i, length;
4483 length = PyTuple_Size(w);
4484 for (i = 0; i < length; i += 1) {
4485 PyObject *exc = PyTuple_GET_ITEM(w, i);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004486 if (PyString_Check(exc)) {
Brett Cannon129bd522007-01-30 21:34:36 +00004487 int ret_val;
4488 ret_val = PyErr_WarnEx(
Thomas Wouterse2176022007-09-20 17:35:10 +00004489 PyExc_DeprecationWarning,
4490 "catching of string "
4491 "exceptions is deprecated", 1);
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00004492 if (ret_val < 0)
Brett Cannon129bd522007-01-30 21:34:36 +00004493 return NULL;
4494 }
Guido van Rossum20bda582008-03-18 03:15:05 +00004495 else if (Py_Py3kWarningFlag &&
4496 !PyTuple_Check(exc) &&
4497 !Py3kExceptionClass_Check(exc))
Guido van Rossum04edb522008-03-18 02:49:46 +00004498 {
4499 int ret_val;
4500 ret_val = PyErr_WarnEx(
4501 PyExc_DeprecationWarning,
4502 CANNOT_CATCH_MSG, 1);
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00004503 if (ret_val < 0)
Guido van Rossum04edb522008-03-18 02:49:46 +00004504 return NULL;
4505 }
Brett Cannon129bd522007-01-30 21:34:36 +00004506 }
4507 }
4508 else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004509 if (PyString_Check(w)) {
Brett Cannon129bd522007-01-30 21:34:36 +00004510 int ret_val;
4511 ret_val = PyErr_WarnEx(
4512 PyExc_DeprecationWarning,
4513 "catching of string "
Thomas Wouterse2176022007-09-20 17:35:10 +00004514 "exceptions is deprecated", 1);
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00004515 if (ret_val < 0)
Brett Cannon129bd522007-01-30 21:34:36 +00004516 return NULL;
4517 }
Guido van Rossum20bda582008-03-18 03:15:05 +00004518 else if (Py_Py3kWarningFlag &&
4519 !PyTuple_Check(w) &&
4520 !Py3kExceptionClass_Check(w))
Guido van Rossum04edb522008-03-18 02:49:46 +00004521 {
4522 int ret_val;
4523 ret_val = PyErr_WarnEx(
4524 PyExc_DeprecationWarning,
4525 CANNOT_CATCH_MSG, 1);
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00004526 if (ret_val < 0)
Guido van Rossum04edb522008-03-18 02:49:46 +00004527 return NULL;
4528 }
Brett Cannon129bd522007-01-30 21:34:36 +00004529 }
Barry Warsaw4249f541997-08-22 21:26:19 +00004530 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004531 break;
4532 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00004533 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004534 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004535 v = res ? Py_True : Py_False;
4536 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004537 return v;
4538}
4539
Fredrik Lundh7a830892006-05-27 10:39:48 +00004540static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00004541import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004542{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004543 PyObject *x;
4544
4545 x = PyObject_GetAttr(v, name);
4546 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00004547 PyErr_Format(PyExc_ImportError,
4548 "cannot import name %.230s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004549 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004550 }
Thomas Wouters52152252000-08-17 22:55:00 +00004551 return x;
4552}
Guido van Rossumac7be682001-01-17 15:42:30 +00004553
Fredrik Lundh7a830892006-05-27 10:39:48 +00004554static int
Thomas Wouters52152252000-08-17 22:55:00 +00004555import_all_from(PyObject *locals, PyObject *v)
4556{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004557 PyObject *all = PyObject_GetAttrString(v, "__all__");
4558 PyObject *dict, *name, *value;
4559 int skip_leading_underscores = 0;
4560 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004561
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004562 if (all == NULL) {
4563 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4564 return -1; /* Unexpected error */
4565 PyErr_Clear();
4566 dict = PyObject_GetAttrString(v, "__dict__");
4567 if (dict == NULL) {
4568 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4569 return -1;
4570 PyErr_SetString(PyExc_ImportError,
4571 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00004572 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004573 }
4574 all = PyMapping_Keys(dict);
4575 Py_DECREF(dict);
4576 if (all == NULL)
4577 return -1;
4578 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004579 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004580
4581 for (pos = 0, err = 0; ; pos++) {
4582 name = PySequence_GetItem(all, pos);
4583 if (name == NULL) {
4584 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4585 err = -1;
4586 else
4587 PyErr_Clear();
4588 break;
4589 }
4590 if (skip_leading_underscores &&
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004591 PyString_Check(name) &&
4592 PyString_AS_STRING(name)[0] == '_')
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004593 {
4594 Py_DECREF(name);
4595 continue;
4596 }
4597 value = PyObject_GetAttr(v, name);
4598 if (value == NULL)
4599 err = -1;
Armin Rigo70370852006-11-29 21:59:22 +00004600 else if (PyDict_CheckExact(locals))
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004601 err = PyDict_SetItem(locals, name, value);
Armin Rigo70370852006-11-29 21:59:22 +00004602 else
4603 err = PyObject_SetItem(locals, name, value);
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004604 Py_DECREF(name);
4605 Py_XDECREF(value);
4606 if (err != 0)
4607 break;
4608 }
4609 Py_DECREF(all);
4610 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004611}
4612
Fredrik Lundh7a830892006-05-27 10:39:48 +00004613static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004614build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004615{
Guido van Rossum7851eea2001-09-12 19:19:18 +00004616 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004617
4618 if (PyDict_Check(methods))
4619 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00004620 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00004621 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004622 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4623 base = PyTuple_GET_ITEM(bases, 0);
4624 metaclass = PyObject_GetAttrString(base, "__class__");
4625 if (metaclass == NULL) {
4626 PyErr_Clear();
4627 metaclass = (PyObject *)base->ob_type;
4628 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00004629 }
4630 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004631 else {
4632 PyObject *g = PyEval_GetGlobals();
4633 if (g != NULL && PyDict_Check(g))
4634 metaclass = PyDict_GetItemString(g, "__metaclass__");
4635 if (metaclass == NULL)
4636 metaclass = (PyObject *) &PyClass_Type;
4637 Py_INCREF(metaclass);
4638 }
Jeremy Hylton7c1e3472007-02-26 16:14:51 +00004639 result = PyObject_CallFunctionObjArgs(metaclass, name, bases, methods,
Thomas Woutersae406c62007-09-19 17:27:43 +00004640 NULL);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004641 Py_DECREF(metaclass);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004642 if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
Tim Peters7df5e7f2006-05-26 23:14:37 +00004643 /* A type error here likely means that the user passed
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004644 in a base that was not a class (such the random module
4645 instead of the random.random type). Help them out with
Raymond Hettingercfc31922004-09-16 16:41:57 +00004646 by augmenting the error message with more information.*/
4647
4648 PyObject *ptype, *pvalue, *ptraceback;
4649
4650 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004651 if (PyString_Check(pvalue)) {
Raymond Hettingercfc31922004-09-16 16:41:57 +00004652 PyObject *newmsg;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004653 newmsg = PyString_FromFormat(
Jeremy Hylton7c1e3472007-02-26 16:14:51 +00004654 "Error when calling the metaclass bases\n"
Thomas Woutersae406c62007-09-19 17:27:43 +00004655 " %s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004656 PyString_AS_STRING(pvalue));
Raymond Hettingercfc31922004-09-16 16:41:57 +00004657 if (newmsg != NULL) {
4658 Py_DECREF(pvalue);
4659 pvalue = newmsg;
4660 }
4661 }
4662 PyErr_Restore(ptype, pvalue, ptraceback);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004663 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004664 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00004665}
4666
Fredrik Lundh7a830892006-05-27 10:39:48 +00004667static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004668exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
4669 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004670{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004671 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00004672 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004673 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004674
Guido van Rossumb209a111997-04-29 18:18:01 +00004675 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
4676 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004677 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00004678 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004679 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00004680 locals = PyTuple_GetItem(prog, 2);
4681 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004682 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004683 if (globals == Py_None) {
4684 globals = PyEval_GetGlobals();
4685 if (locals == Py_None) {
4686 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00004687 plain = 1;
4688 }
Neal Norwitzdf6a6492006-08-13 18:10:10 +00004689 if (!globals || !locals) {
4690 PyErr_SetString(PyExc_SystemError,
4691 "globals and locals cannot be NULL");
4692 return -1;
4693 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004694 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004695 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004696 locals = globals;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004697 if (!PyString_Check(prog) &&
Benjamin Peterson78821dd2009-01-25 17:15:10 +00004698#ifdef Py_USING_UNICODE
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004699 !PyUnicode_Check(prog) &&
Benjamin Peterson78821dd2009-01-25 17:15:10 +00004700#endif
Guido van Rossumb209a111997-04-29 18:18:01 +00004701 !PyCode_Check(prog) &&
4702 !PyFile_Check(prog)) {
4703 PyErr_SetString(PyExc_TypeError,
Guido van Rossumac7be682001-01-17 15:42:30 +00004704 "exec: arg 1 must be a string, file, or code object");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004705 return -1;
4706 }
Fred Drake661ea262000-10-24 19:57:45 +00004707 if (!PyDict_Check(globals)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004708 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00004709 "exec: arg 2 must be a dictionary or None");
4710 return -1;
4711 }
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004712 if (!PyMapping_Check(locals)) {
Fred Drake661ea262000-10-24 19:57:45 +00004713 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004714 "exec: arg 3 must be a mapping or None");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004715 return -1;
4716 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004717 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00004718 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00004719 if (PyCode_Check(prog)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +00004720 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4721 PyErr_SetString(PyExc_TypeError,
4722 "code object passed to exec may not contain free variables");
4723 return -1;
4724 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004725 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004726 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004727 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004728 FILE *fp = PyFile_AsFile(prog);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004729 char *name = PyString_AsString(PyFile_Name(prog));
Jeremy Hylton714b1122007-02-25 16:01:58 +00004730 PyCompilerFlags cf;
Thomas Woutersae406c62007-09-19 17:27:43 +00004731 if (name == NULL)
4732 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004733 cf.cf_flags = 0;
4734 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004735 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004736 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004737 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004738 v = PyRun_File(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004739 locals);
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004740 }
4741 else {
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004742 PyObject *tmp = NULL;
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004743 char *str;
Tim Peters5ba58662001-07-16 02:29:45 +00004744 PyCompilerFlags cf;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004745 cf.cf_flags = 0;
4746#ifdef Py_USING_UNICODE
4747 if (PyUnicode_Check(prog)) {
4748 tmp = PyUnicode_AsUTF8String(prog);
4749 if (tmp == NULL)
4750 return -1;
4751 prog = tmp;
4752 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4753 }
4754#endif
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004755 if (PyString_AsStringAndSize(prog, &str, NULL))
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004756 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004757 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters8a5c3c72004-04-05 19:36:21 +00004758 v = PyRun_StringFlags(str, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004759 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004760 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004761 v = PyRun_String(str, Py_file_input, globals, locals);
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004762 Py_XDECREF(tmp);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004763 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004764 if (plain)
4765 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004766 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004767 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00004768 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004769 return 0;
4770}
Guido van Rossum24c13741995-02-14 09:42:43 +00004771
Fredrik Lundh7a830892006-05-27 10:39:48 +00004772static void
Paul Prescode68140d2000-08-30 20:25:01 +00004773format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4774{
4775 char *obj_str;
4776
4777 if (!obj)
4778 return;
4779
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004780 obj_str = PyString_AsString(obj);
Paul Prescode68140d2000-08-30 20:25:01 +00004781 if (!obj_str)
4782 return;
4783
4784 PyErr_Format(exc, format_str, obj_str);
4785}
Guido van Rossum950361c1997-01-24 13:49:28 +00004786
Fredrik Lundh7a830892006-05-27 10:39:48 +00004787static PyObject *
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004788string_concatenate(PyObject *v, PyObject *w,
4789 PyFrameObject *f, unsigned char *next_instr)
4790{
4791 /* This function implements 'variable += expr' when both arguments
4792 are strings. */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004793 Py_ssize_t v_len = PyString_GET_SIZE(v);
4794 Py_ssize_t w_len = PyString_GET_SIZE(w);
Armin Rigo97ff0472006-08-09 15:37:26 +00004795 Py_ssize_t new_len = v_len + w_len;
4796 if (new_len < 0) {
4797 PyErr_SetString(PyExc_OverflowError,
4798 "strings are too large to concat");
4799 return NULL;
4800 }
Tim Peters7df5e7f2006-05-26 23:14:37 +00004801
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004802 if (v->ob_refcnt == 2) {
4803 /* In the common case, there are 2 references to the value
4804 * stored in 'variable' when the += is performed: one on the
Thomas Wouterse2176022007-09-20 17:35:10 +00004805 * value stack (in 'v') and one still stored in the
4806 * 'variable'. We try to delete the variable now to reduce
4807 * the refcnt to 1.
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004808 */
4809 switch (*next_instr) {
4810 case STORE_FAST:
4811 {
4812 int oparg = PEEKARG();
4813 PyObject **fastlocals = f->f_localsplus;
4814 if (GETLOCAL(oparg) == v)
4815 SETLOCAL(oparg, NULL);
4816 break;
4817 }
4818 case STORE_DEREF:
4819 {
Thomas Wouterse2176022007-09-20 17:35:10 +00004820 PyObject **freevars = (f->f_localsplus +
4821 f->f_code->co_nlocals);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004822 PyObject *c = freevars[PEEKARG()];
4823 if (PyCell_GET(c) == v)
4824 PyCell_Set(c, NULL);
4825 break;
4826 }
4827 case STORE_NAME:
4828 {
4829 PyObject *names = f->f_code->co_names;
4830 PyObject *name = GETITEM(names, PEEKARG());
4831 PyObject *locals = f->f_locals;
4832 if (PyDict_CheckExact(locals) &&
4833 PyDict_GetItem(locals, name) == v) {
4834 if (PyDict_DelItem(locals, name) != 0) {
4835 PyErr_Clear();
4836 }
4837 }
4838 break;
4839 }
4840 }
4841 }
4842
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004843 if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004844 /* Now we own the last reference to 'v', so we can resize it
4845 * in-place.
4846 */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004847 if (_PyString_Resize(&v, new_len) != 0) {
4848 /* XXX if _PyString_Resize() fails, 'v' has been
Thomas Wouterse2176022007-09-20 17:35:10 +00004849 * deallocated so it cannot be put back into
4850 * 'variable'. The MemoryError is raised when there
4851 * is no value in 'variable', which might (very
4852 * remotely) be a cause of incompatibilities.
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004853 */
4854 return NULL;
4855 }
4856 /* copy 'w' into the newly allocated area of 'v' */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004857 memcpy(PyString_AS_STRING(v) + v_len,
4858 PyString_AS_STRING(w), w_len);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004859 return v;
4860 }
4861 else {
4862 /* When in-place resizing is not an option. */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004863 PyString_Concat(&v, w);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004864 return v;
4865 }
4866}
4867
Guido van Rossum950361c1997-01-24 13:49:28 +00004868#ifdef DYNAMIC_EXECUTION_PROFILE
4869
Fredrik Lundh7a830892006-05-27 10:39:48 +00004870static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004871getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004872{
4873 int i;
4874 PyObject *l = PyList_New(256);
4875 if (l == NULL) return NULL;
4876 for (i = 0; i < 256; i++) {
4877 PyObject *x = PyInt_FromLong(a[i]);
4878 if (x == NULL) {
4879 Py_DECREF(l);
4880 return NULL;
4881 }
4882 PyList_SetItem(l, i, x);
4883 }
4884 for (i = 0; i < 256; i++)
4885 a[i] = 0;
4886 return l;
4887}
4888
4889PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004890_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004891{
4892#ifndef DXPAIRS
4893 return getarray(dxp);
4894#else
4895 int i;
4896 PyObject *l = PyList_New(257);
4897 if (l == NULL) return NULL;
4898 for (i = 0; i < 257; i++) {
4899 PyObject *x = getarray(dxpairs[i]);
4900 if (x == NULL) {
4901 Py_DECREF(l);
4902 return NULL;
4903 }
4904 PyList_SetItem(l, i, x);
4905 }
4906 return l;
4907#endif
4908}
4909
4910#endif