blob: c75caf68231a219b43710523654cfb30fa476f27 [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
Thomas Wouters477c8d52006-05-27 19:21:47 +00009/* enable more aggressive intra-module optimizations, where available */
10#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
Thomas Wouters477c8d52006-05-27 19:21:47 +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 Wouters8ce81f72007-09-20 18:22:40 +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
37static void
38ppc_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
Thomas Wouters477c8d52006-05-27 19:21:47 +000048 /* The slightly peculiar way of writing the next lines is
Michael W. Hudson800ba232004-08-12 18:19:17 +000049 compiled better by GCC than any other way I tried. */
50 ((long*)(v))[0] = tbu;
51 ((long*)(v))[1] = tb;
52}
53
Michael W. Hudson75eabd22005-01-18 15:56:11 +000054#else /* this is for linux/x86 (and probably any other GCC/x86 combo) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000055
Michael W. Hudson75eabd22005-01-18 15:56:11 +000056#define READ_TIMESTAMP(val) \
57 __asm__ __volatile__("rdtsc" : "=A" (val))
Michael W. Hudson800ba232004-08-12 18:19:17 +000058
59#endif
60
Thomas Wouters477c8d52006-05-27 19:21:47 +000061void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000062 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
63{
64 uint64 intr, inst, loop;
65 PyThreadState *tstate = PyThreadState_Get();
66 if (!tstate->interp->tscdump)
67 return;
68 intr = intr1 - intr0;
69 inst = inst1 - inst0 - intr;
70 loop = loop1 - loop0 - intr;
71 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
72 opcode, ticked, inst, loop);
73}
Michael W. Hudson800ba232004-08-12 18:19:17 +000074
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000075#endif
76
Guido van Rossum04691fc1992-08-12 15:35:34 +000077/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000078/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000079
Guido van Rossum408027e1996-12-30 16:17:54 +000080#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000081/* For debugging the interpreter: */
82#define LLTRACE 1 /* Low-level trace feature */
83#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000084#endif
85
Jeremy Hylton52820442001-01-03 23:52:36 +000086typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000087
Guido van Rossum374a9221991-04-04 10:40:29 +000088/* Forward declarations */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000089#ifdef WITH_TSC
Thomas Wouters477c8d52006-05-27 19:21:47 +000090static PyObject * call_function(PyObject ***, int, uint64*, uint64*);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000091#else
Thomas Wouters477c8d52006-05-27 19:21:47 +000092static PyObject * call_function(PyObject ***, int);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000093#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000094static PyObject * fast_function(PyObject *, PyObject ***, int, int, int);
95static PyObject * do_call(PyObject *, PyObject ***, int, int);
96static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int);
Thomas Wouters8ce81f72007-09-20 18:22:40 +000097static PyObject * update_keyword_args(PyObject *, int, PyObject ***,
98 PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +000099static PyObject * update_star_args(int, int, PyObject *, PyObject ***);
100static PyObject * load_args(PyObject ***, int);
Jeremy Hylton52820442001-01-03 23:52:36 +0000101#define CALL_FLAG_VAR 1
102#define CALL_FLAG_KW 2
103
Guido van Rossum0a066c01992-03-27 17:29:15 +0000104#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +0000105static int lltrace;
Tim Petersdbd9ba62000-07-09 03:09:57 +0000106static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +0000107#endif
Fred Drake5755ce62001-06-27 19:19:46 +0000108static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
109 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +0000110static int call_trace_protected(Py_tracefunc, PyObject *,
Armin Rigo1c2d7e52005-09-20 18:34:01 +0000111 PyFrameObject *, int, PyObject *);
Fred Drake5755ce62001-06-27 19:19:46 +0000112static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +0000113static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Armin Rigobf57a142004-03-22 19:24:58 +0000114 PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000115
Thomas Wouters477c8d52006-05-27 19:21:47 +0000116static PyObject * cmp_outcome(int, PyObject *, PyObject *);
117static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +0000118static int import_all_from(PyObject *, PyObject *);
Neal Norwitzda059e32007-08-26 05:33:45 +0000119static void format_exc_check_arg(PyObject *, const char *, PyObject *);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000120static PyObject * unicode_concatenate(PyObject *, PyObject *,
121 PyFrameObject *, unsigned char *);
Guido van Rossum374a9221991-04-04 10:40:29 +0000122
Paul Prescode68140d2000-08-30 20:25:01 +0000123#define NAME_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000124 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000125#define GLOBAL_NAME_ERROR_MSG \
126 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000127#define UNBOUNDLOCAL_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000128 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000129#define UNBOUNDFREE_ERROR_MSG \
130 "free variable '%.200s' referenced before assignment" \
131 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000132
Guido van Rossum950361c1997-01-24 13:49:28 +0000133/* Dynamic execution profile */
134#ifdef DYNAMIC_EXECUTION_PROFILE
135#ifdef DXPAIRS
136static long dxpairs[257][256];
137#define dxp dxpairs[256]
138#else
139static long dxp[256];
140#endif
141#endif
142
Jeremy Hylton985eba52003-02-05 23:13:00 +0000143/* Function call profile */
144#ifdef CALL_PROFILE
145#define PCALL_NUM 11
146static int pcall[PCALL_NUM];
147
148#define PCALL_ALL 0
149#define PCALL_FUNCTION 1
150#define PCALL_FAST_FUNCTION 2
151#define PCALL_FASTER_FUNCTION 3
152#define PCALL_METHOD 4
153#define PCALL_BOUND_METHOD 5
154#define PCALL_CFUNCTION 6
155#define PCALL_TYPE 7
156#define PCALL_GENERATOR 8
157#define PCALL_OTHER 9
158#define PCALL_POP 10
159
160/* Notes about the statistics
161
162 PCALL_FAST stats
163
164 FAST_FUNCTION means no argument tuple needs to be created.
165 FASTER_FUNCTION means that the fast-path frame setup code is used.
166
167 If there is a method call where the call can be optimized by changing
168 the argument tuple and calling the function directly, it gets recorded
169 twice.
170
171 As a result, the relationship among the statistics appears to be
172 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
173 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
174 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
175 PCALL_METHOD > PCALL_BOUND_METHOD
176*/
177
178#define PCALL(POS) pcall[POS]++
179
180PyObject *
181PyEval_GetCallStats(PyObject *self)
182{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000183 return Py_BuildValue("iiiiiiiiiii",
Jeremy Hylton985eba52003-02-05 23:13:00 +0000184 pcall[0], pcall[1], pcall[2], pcall[3],
185 pcall[4], pcall[5], pcall[6], pcall[7],
Thomas Wouters89f507f2006-12-13 04:49:30 +0000186 pcall[8], pcall[9], pcall[10]);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000187}
188#else
189#define PCALL(O)
190
191PyObject *
192PyEval_GetCallStats(PyObject *self)
193{
194 Py_INCREF(Py_None);
195 return Py_None;
196}
197#endif
198
Tim Peters5ca576e2001-06-18 22:08:13 +0000199
Guido van Rossume59214e1994-08-30 08:01:59 +0000200#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000201
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000202#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000203#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000204#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000205#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000206
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000207static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
Guido van Rossuma9672091994-09-14 13:31:22 +0000208static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000209
Tim Peters7f468f22004-10-11 02:40:51 +0000210int
211PyEval_ThreadsInitialized(void)
212{
213 return interpreter_lock != 0;
214}
215
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000216void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000217PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000218{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000219 if (interpreter_lock)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000220 return;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000221 interpreter_lock = PyThread_allocate_lock();
222 PyThread_acquire_lock(interpreter_lock, 1);
223 main_thread = PyThread_get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000224}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000225
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000226void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000227PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000228{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000229 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000230}
231
232void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000233PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000234{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000235 PyThread_release_lock(interpreter_lock);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000236}
237
238void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000239PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000240{
241 if (tstate == NULL)
242 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000243 /* Check someone has called PyEval_InitThreads() to create the lock */
244 assert(interpreter_lock);
Guido van Rossum65d5b571998-12-21 19:32:43 +0000245 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000246 if (PyThreadState_Swap(tstate) != NULL)
247 Py_FatalError(
248 "PyEval_AcquireThread: non-NULL old thread state");
249}
250
251void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000252PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000253{
254 if (tstate == NULL)
255 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
256 if (PyThreadState_Swap(NULL) != tstate)
257 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000258 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000259}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000260
261/* This function is called from PyOS_AfterFork to ensure that newly
262 created child processes don't hold locks referring to threads which
263 are not running in the child process. (This could also be done using
264 pthread_atfork mechanism, at least for the pthreads implementation.) */
265
266void
267PyEval_ReInitThreads(void)
268{
269 if (!interpreter_lock)
270 return;
271 /*XXX Can't use PyThread_free_lock here because it does too
272 much error-checking. Doing this cleanly would require
273 adding a new function to each thread_*.h. Instead, just
274 create a new lock and waste a little bit of memory */
275 interpreter_lock = PyThread_allocate_lock();
276 PyThread_acquire_lock(interpreter_lock, 1);
277 main_thread = PyThread_get_thread_ident();
278}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000279#endif
280
Guido van Rossumff4949e1992-08-05 19:58:53 +0000281/* Functions save_thread and restore_thread are always defined so
282 dynamically loaded modules needn't be compiled separately for use
283 with and without threads: */
284
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000285PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000286PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000287{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000288 PyThreadState *tstate = PyThreadState_Swap(NULL);
289 if (tstate == NULL)
290 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000291#ifdef WITH_THREAD
Guido van Rossumb74eca91997-09-30 22:03:16 +0000292 if (interpreter_lock)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000293 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000294#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000295 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000296}
297
298void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000299PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000300{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000301 if (tstate == NULL)
302 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000303#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000304 if (interpreter_lock) {
Guido van Rossumb74eca91997-09-30 22:03:16 +0000305 int err = errno;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000306 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000307 errno = err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000308 }
309#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000310 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000311}
312
313
Guido van Rossuma9672091994-09-14 13:31:22 +0000314/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
315 signal handlers or Mac I/O completion routines) can schedule calls
316 to a function to be called synchronously.
317 The synchronous function is called with one void* argument.
318 It should return 0 for success or -1 for failure -- failure should
319 be accompanied by an exception.
320
321 If registry succeeds, the registry function returns 0; if it fails
322 (e.g. due to too many pending calls) it returns -1 (without setting
323 an exception condition).
324
325 Note that because registry may occur from within signal handlers,
326 or other asynchronous events, calling malloc() is unsafe!
327
328#ifdef WITH_THREAD
329 Any thread can schedule pending calls, but only the main thread
330 will execute them.
331#endif
332
333 XXX WARNING! ASYNCHRONOUSLY EXECUTING CODE!
334 There are two possible race conditions:
335 (1) nested asynchronous registry calls;
336 (2) registry calls made while pending calls are being processed.
337 While (1) is very unlikely, (2) is a real possibility.
338 The current code is safe against (2), but not against (1).
339 The safety against (2) is derived from the fact that only one
340 thread (the main thread) ever takes things out of the queue.
Guido van Rossuma9672091994-09-14 13:31:22 +0000341
Guido van Rossuma027efa1997-05-05 20:56:21 +0000342 XXX Darn! With the advent of thread state, we should have an array
343 of pending calls per thread in the thread state! Later...
344*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000345
Guido van Rossuma9672091994-09-14 13:31:22 +0000346#define NPENDINGCALLS 32
347static struct {
Thomas Wouters334fb892000-07-25 12:56:38 +0000348 int (*func)(void *);
349 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000350} pendingcalls[NPENDINGCALLS];
351static volatile int pendingfirst = 0;
352static volatile int pendinglast = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000353static volatile int things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000354
355int
Thomas Wouters334fb892000-07-25 12:56:38 +0000356Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000357{
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000358 static volatile int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000359 int i, j;
360 /* XXX Begin critical section */
361 /* XXX If you want this to be safe against nested
362 XXX asynchronous calls, you'll have to work harder! */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000363 if (busy)
364 return -1;
365 busy = 1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000366 i = pendinglast;
367 j = (i + 1) % NPENDINGCALLS;
Guido van Rossum04e70322002-07-17 16:57:13 +0000368 if (j == pendingfirst) {
369 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000370 return -1; /* Queue full */
Guido van Rossum04e70322002-07-17 16:57:13 +0000371 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000372 pendingcalls[i].func = func;
373 pendingcalls[i].arg = arg;
374 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000375
376 _Py_Ticker = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000377 things_to_do = 1; /* Signal main loop */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000378 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000379 /* XXX End critical section */
380 return 0;
381}
382
Guido van Rossum180d7b41994-09-29 09:45:57 +0000383int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000384Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000385{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000386 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000387#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000388 if (main_thread && PyThread_get_thread_ident() != main_thread)
Guido van Rossuma9672091994-09-14 13:31:22 +0000389 return 0;
390#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000391 if (busy)
Guido van Rossum180d7b41994-09-29 09:45:57 +0000392 return 0;
393 busy = 1;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000394 things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000395 for (;;) {
396 int i;
Thomas Wouters334fb892000-07-25 12:56:38 +0000397 int (*func)(void *);
398 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000399 i = pendingfirst;
400 if (i == pendinglast)
401 break; /* Queue empty */
402 func = pendingcalls[i].func;
403 arg = pendingcalls[i].arg;
404 pendingfirst = (i + 1) % NPENDINGCALLS;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000405 if (func(arg) < 0) {
406 busy = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000407 things_to_do = 1; /* We're not done yet */
Guido van Rossuma9672091994-09-14 13:31:22 +0000408 return -1;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000409 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000410 }
Guido van Rossum180d7b41994-09-29 09:45:57 +0000411 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000412 return 0;
413}
414
415
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000416/* The interpreter's recursion limit */
417
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000418#ifndef Py_DEFAULT_RECURSION_LIMIT
419#define Py_DEFAULT_RECURSION_LIMIT 1000
420#endif
421static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
422int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000423
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000424int
425Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000426{
427 return recursion_limit;
428}
429
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000430void
431Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000432{
433 recursion_limit = new_limit;
Thomas Woutersce272b62007-09-19 21:19:28 +0000434 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000435}
436
Armin Rigo2b3eb402003-10-28 12:05:48 +0000437/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
438 if the recursion_depth reaches _Py_CheckRecursionLimit.
439 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
440 to guarantee that _Py_CheckRecursiveCall() is regularly called.
441 Without USE_STACKCHECK, there is no need for this. */
442int
443_Py_CheckRecursiveCall(char *where)
444{
445 PyThreadState *tstate = PyThreadState_GET();
446
447#ifdef USE_STACKCHECK
448 if (PyOS_CheckStack()) {
449 --tstate->recursion_depth;
450 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
451 return -1;
452 }
453#endif
Martin v. Löwis5b222132007-06-10 09:51:05 +0000454 if (tstate->recursion_critical)
455 /* Somebody asked that we don't check for recursion. */
456 return 0;
457 if (tstate->overflowed) {
458 if (tstate->recursion_depth > recursion_limit + 50) {
459 /* Overflowing while handling an overflow. Give up. */
460 Py_FatalError("Cannot recover from stack overflow.");
461 }
462 return 0;
463 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000464 if (tstate->recursion_depth > recursion_limit) {
465 --tstate->recursion_depth;
Martin v. Löwis5b222132007-06-10 09:51:05 +0000466 tstate->overflowed = 1;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000467 PyErr_Format(PyExc_RuntimeError,
468 "maximum recursion depth exceeded%s",
469 where);
470 return -1;
471 }
Thomas Woutersce272b62007-09-19 21:19:28 +0000472 _Py_CheckRecursionLimit = recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000473 return 0;
474}
475
Guido van Rossum374a9221991-04-04 10:40:29 +0000476/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000477enum why_code {
478 WHY_NOT = 0x0001, /* No error */
479 WHY_EXCEPTION = 0x0002, /* Exception occurred */
480 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
481 WHY_RETURN = 0x0008, /* 'return' statement */
482 WHY_BREAK = 0x0010, /* 'break' statement */
483 WHY_CONTINUE = 0x0020, /* 'continue' statement */
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000484 WHY_YIELD = 0x0040, /* 'yield' operator */
485 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000486};
Guido van Rossum374a9221991-04-04 10:40:29 +0000487
Collin Winter828f04a2007-08-31 00:04:24 +0000488static enum why_code do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000489static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000490
Skip Montanarod581d772002-09-03 20:10:45 +0000491/* for manipulating the thread switch and periodic "stuff" - used to be
492 per thread, now just a pair o' globals */
Skip Montanaro99dba272002-09-03 20:19:06 +0000493int _Py_CheckInterval = 100;
494volatile int _Py_Ticker = 100;
Guido van Rossum374a9221991-04-04 10:40:29 +0000495
Guido van Rossumb209a111997-04-29 18:18:01 +0000496PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000497PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000498{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000499 return PyEval_EvalCodeEx(co,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000500 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000501 (PyObject **)NULL, 0,
502 (PyObject **)NULL, 0,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000503 (PyObject **)NULL, 0,
Guido van Rossum4f72a782006-10-27 23:31:49 +0000504 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000505}
506
507
508/* Interpreter main loop */
509
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000510PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000511PyEval_EvalFrame(PyFrameObject *f) {
512 /* This is for backward compatibility with extension modules that
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000513 used this API; core interpreter code should call
514 PyEval_EvalFrameEx() */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000515 return PyEval_EvalFrameEx(f, 0);
516}
517
518PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000519PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000520{
Guido van Rossum950361c1997-01-24 13:49:28 +0000521#ifdef DXPAIRS
522 int lastopcode = 0;
523#endif
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000524 register PyObject **stack_pointer; /* Next free slot in value stack */
Guido van Rossum374a9221991-04-04 10:40:29 +0000525 register unsigned char *next_instr;
Armin Rigo8817fcd2004-06-17 10:22:40 +0000526 register int opcode; /* Current opcode */
527 register int oparg; /* Current opcode argument, if any */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000528 register enum why_code why; /* Reason for block stack unwind */
Guido van Rossum374a9221991-04-04 10:40:29 +0000529 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000530 register PyObject *x; /* Result object -- NULL if error */
531 register PyObject *v; /* Temporary objects popped off stack */
532 register PyObject *w;
533 register PyObject *u;
534 register PyObject *t;
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000535 register PyObject **fastlocals, **freevars;
Guido van Rossum014518f1998-11-23 21:09:51 +0000536 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000537 PyThreadState *tstate = PyThreadState_GET();
Tim Peters5ca576e2001-06-18 22:08:13 +0000538 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000539
Tim Peters8a5c3c72004-04-05 19:36:21 +0000540 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000541
542 not (instr_lb <= current_bytecode_offset < instr_ub)
543
Tim Peters8a5c3c72004-04-05 19:36:21 +0000544 is true when the line being executed has changed. The
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000545 initial values are such as to make this false the first
546 time it is tested. */
Armin Rigobf57a142004-03-22 19:24:58 +0000547 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000548
Guido van Rossumd076c731998-10-07 19:42:25 +0000549 unsigned char *first_instr;
Skip Montanaro04d80f82002-08-04 21:03:35 +0000550 PyObject *names;
551 PyObject *consts;
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000552#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000553 /* Make it easier to find out where we are with a debugger */
Tim Peters5ca576e2001-06-18 22:08:13 +0000554 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000555#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000556
Neal Norwitza81d2202002-07-14 00:27:26 +0000557/* Tuple access macros */
558
559#ifndef Py_DEBUG
560#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
561#else
562#define GETITEM(v, i) PyTuple_GetItem((v), (i))
563#endif
564
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000565#ifdef WITH_TSC
566/* Use Pentium timestamp counter to mark certain events:
567 inst0 -- beginning of switch statement for opcode dispatch
568 inst1 -- end of switch statement (may be skipped)
569 loop0 -- the top of the mainloop
Thomas Wouters477c8d52006-05-27 19:21:47 +0000570 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000571 (may be skipped)
572 intr1 -- beginning of long interruption
573 intr2 -- end of long interruption
574
575 Many opcodes call out to helper C functions. In some cases, the
576 time in those functions should be counted towards the time for the
577 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
578 calls another Python function; there's no point in charge all the
579 bytecode executed by the called function to the caller.
580
581 It's hard to make a useful judgement statically. In the presence
582 of operator overloading, it's impossible to tell if a call will
583 execute new Python code or not.
584
585 It's a case-by-case judgement. I'll use intr1 for the following
586 cases:
587
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000588 IMPORT_STAR
589 IMPORT_FROM
590 CALL_FUNCTION (and friends)
591
592 */
593 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
594 int ticked = 0;
595
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000596 READ_TIMESTAMP(inst0);
597 READ_TIMESTAMP(inst1);
598 READ_TIMESTAMP(loop0);
599 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000600
601 /* shut up the compiler */
602 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000603#endif
604
Guido van Rossum374a9221991-04-04 10:40:29 +0000605/* Code access macros */
606
Martin v. Löwis18e16552006-02-15 17:27:45 +0000607#define INSTR_OFFSET() ((int)(next_instr - first_instr))
Guido van Rossum374a9221991-04-04 10:40:29 +0000608#define NEXTOP() (*next_instr++)
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000609#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000610#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
Guido van Rossumd076c731998-10-07 19:42:25 +0000611#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000612#define JUMPBY(x) (next_instr += (x))
613
Raymond Hettingerf606f872003-03-16 03:11:04 +0000614/* OpCode prediction macros
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000615 Some opcodes tend to come in pairs thus making it possible to
616 predict the second code when the first is run. For example,
617 COMPARE_OP is often followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And,
618 those opcodes are often followed by a POP_TOP.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000619
Georg Brandl86b2fb92008-07-16 03:43:04 +0000620 Verifying the prediction costs a single high-speed test of a register
Raymond Hettingerac2072922003-03-16 15:41:11 +0000621 variable against a constant. If the pairing was good, then the
Georg Brandl86b2fb92008-07-16 03:43:04 +0000622 processor's own internal branch predication has a high likelihood of
623 success, resulting in a nearly zero-overhead transition to the
624 next opcode. A successful prediction saves a trip through the eval-loop
625 including its two unpredictable branches, the HAS_ARG test and the
626 switch-case. Combined with the processor's internal branch prediction,
627 a successful PREDICT has the effect of making the two opcodes run as if
628 they were a single new opcode with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000629
Georg Brandl86b2fb92008-07-16 03:43:04 +0000630 If collecting opcode statistics, your choices are to either keep the
631 predictions turned-on and interpret the results as if some opcodes
632 had been combined or turn-off predictions so that the opcode frequency
633 counter updates for both opcodes.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000634*/
635
Raymond Hettingera7216982004-02-08 19:59:27 +0000636#ifdef DYNAMIC_EXECUTION_PROFILE
637#define PREDICT(op) if (0) goto PRED_##op
638#else
Raymond Hettingerac2072922003-03-16 15:41:11 +0000639#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000640#endif
641
Raymond Hettingerf606f872003-03-16 03:11:04 +0000642#define PREDICTED(op) PRED_##op: next_instr++
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000643#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Raymond Hettingerf606f872003-03-16 03:11:04 +0000644
Guido van Rossum374a9221991-04-04 10:40:29 +0000645/* Stack manipulation macros */
646
Martin v. Löwis18e16552006-02-15 17:27:45 +0000647/* The stack can grow at most MAXINT deep, as co_nlocals and
648 co_stacksize are ints. */
649#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
Guido van Rossum374a9221991-04-04 10:40:29 +0000650#define EMPTY() (STACK_LEVEL() == 0)
651#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000652#define SECOND() (stack_pointer[-2])
653#define THIRD() (stack_pointer[-3])
654#define FOURTH() (stack_pointer[-4])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000655#define SET_TOP(v) (stack_pointer[-1] = (v))
656#define SET_SECOND(v) (stack_pointer[-2] = (v))
657#define SET_THIRD(v) (stack_pointer[-3] = (v))
658#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000659#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000660#define BASIC_PUSH(v) (*stack_pointer++ = (v))
661#define BASIC_POP() (*--stack_pointer)
662
Guido van Rossum96a42c81992-01-12 02:29:51 +0000663#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000664#define PUSH(v) { (void)(BASIC_PUSH(v), \
665 lltrace && prtrace(TOP(), "push")); \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000666 assert(STACK_LEVEL() <= co->co_stacksize); }
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000667#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
668 BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000669#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
670 lltrace && prtrace(TOP(), "stackadj")); \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000671 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +0000672#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
673 prtrace((STACK_POINTER)[-1], "ext_pop")), \
674 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000675#else
676#define PUSH(v) BASIC_PUSH(v)
677#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000678#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000679#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000680#endif
681
Guido van Rossum681d79a1995-07-18 14:51:37 +0000682/* Local variable macros */
683
684#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000685
686/* The SETLOCAL() macro must not DECREF the local variable in-place and
687 then store the new value; it must copy the old value to a temporary
688 value, then store the new value, and then DECREF the temporary value.
689 This is because it is possible that during the DECREF the frame is
690 accessed by other code (e.g. a __del__ method or gc.collect()) and the
691 variable would be pointing to already-freed memory. */
692#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
693 GETLOCAL(i) = value; \
694 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000695
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000696
697#define UNWIND_BLOCK(b) \
698 while (STACK_LEVEL() > (b)->b_level) { \
699 PyObject *v = POP(); \
700 Py_XDECREF(v); \
701 }
702
703#define UNWIND_EXCEPT_HANDLER(b) \
Benjamin Peterson27d63672008-06-15 20:09:12 +0000704 { \
705 PyObject *type, *value, *traceback; \
706 assert(STACK_LEVEL() >= (b)->b_level + 3); \
707 while (STACK_LEVEL() > (b)->b_level + 3) { \
708 value = POP(); \
709 Py_XDECREF(value); \
710 } \
711 type = tstate->exc_type; \
712 value = tstate->exc_value; \
713 traceback = tstate->exc_traceback; \
714 tstate->exc_type = POP(); \
715 tstate->exc_value = POP(); \
716 tstate->exc_traceback = POP(); \
717 Py_XDECREF(type); \
718 Py_XDECREF(value); \
719 Py_XDECREF(traceback); \
720 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000721
722#define SAVE_EXC_STATE() \
723 { \
Benjamin Peterson27d63672008-06-15 20:09:12 +0000724 PyObject *type, *value, *traceback; \
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000725 Py_XINCREF(tstate->exc_type); \
726 Py_XINCREF(tstate->exc_value); \
727 Py_XINCREF(tstate->exc_traceback); \
Benjamin Peterson27d63672008-06-15 20:09:12 +0000728 type = f->f_exc_type; \
729 value = f->f_exc_value; \
730 traceback = f->f_exc_traceback; \
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000731 f->f_exc_type = tstate->exc_type; \
732 f->f_exc_value = tstate->exc_value; \
733 f->f_exc_traceback = tstate->exc_traceback; \
Benjamin Peterson27d63672008-06-15 20:09:12 +0000734 Py_XDECREF(type); \
735 Py_XDECREF(value); \
736 Py_XDECREF(traceback); \
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000737 }
738
739#define SWAP_EXC_STATE() \
740 { \
741 PyObject *tmp; \
742 tmp = tstate->exc_type; \
743 tstate->exc_type = f->f_exc_type; \
744 f->f_exc_type = tmp; \
745 tmp = tstate->exc_value; \
746 tstate->exc_value = f->f_exc_value; \
747 f->f_exc_value = tmp; \
748 tmp = tstate->exc_traceback; \
749 tstate->exc_traceback = f->f_exc_traceback; \
750 f->f_exc_traceback = tmp; \
751 }
752
Guido van Rossuma027efa1997-05-05 20:56:21 +0000753/* Start of code */
754
Tim Peters5ca576e2001-06-18 22:08:13 +0000755 if (f == NULL)
756 return NULL;
757
Armin Rigo1d313ab2003-10-25 14:33:09 +0000758 /* push frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000759 if (Py_EnterRecursiveCall(""))
Armin Rigo1d313ab2003-10-25 14:33:09 +0000760 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000761
Tim Peters5ca576e2001-06-18 22:08:13 +0000762 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000763
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000764 if (tstate->use_tracing) {
765 if (tstate->c_tracefunc != NULL) {
766 /* tstate->c_tracefunc, if defined, is a
767 function that will be called on *every* entry
768 to a code block. Its return value, if not
769 None, is a function that will be called at
770 the start of each executed line of code.
771 (Actually, the function must return itself
772 in order to continue tracing.) The trace
773 functions are called with three arguments:
774 a pointer to the current frame, a string
775 indicating why the function is called, and
776 an argument which depends on the situation.
777 The global trace function is also called
778 whenever an exception is detected. */
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +0000779 if (call_trace_protected(tstate->c_tracefunc,
780 tstate->c_traceobj,
781 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000782 /* Trace function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000783 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000784 }
785 }
786 if (tstate->c_profilefunc != NULL) {
787 /* Similar for c_profilefunc, except it needn't
788 return itself and isn't called for "line" events */
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +0000789 if (call_trace_protected(tstate->c_profilefunc,
790 tstate->c_profileobj,
791 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000792 /* Profile function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000793 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000794 }
795 }
796 }
797
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000798 co = f->f_code;
799 names = co->co_names;
800 consts = co->co_consts;
801 fastlocals = f->f_localsplus;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000802 freevars = f->f_localsplus + co->co_nlocals;
Christian Heimes72b710a2008-05-26 13:28:38 +0000803 first_instr = (unsigned char*) PyBytes_AS_STRING(co->co_code);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000804 /* An explanation is in order for the next line.
805
806 f->f_lasti now refers to the index of the last instruction
807 executed. You might think this was obvious from the name, but
808 this wasn't always true before 2.3! PyFrame_New now sets
809 f->f_lasti to -1 (i.e. the index *before* the first instruction)
810 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000811 does work. Promise.
812
813 When the PREDICT() macros are enabled, some opcode pairs follow in
814 direct succession without updating f->f_lasti. A successful
815 prediction effectively links the two codes together as if they
816 were a single new opcode; accordingly,f->f_lasti will point to
817 the first code in the pair (for instance, GET_ITER followed by
818 FOR_ITER is effectively a single opcode and f->f_lasti will point
819 at to the beginning of the combined pair.)
820 */
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000821 next_instr = first_instr + f->f_lasti + 1;
822 stack_pointer = f->f_stacktop;
823 assert(stack_pointer != NULL);
824 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
825
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000826 if (f->f_code->co_flags & CO_GENERATOR) {
827 if (f->f_exc_type != NULL && f->f_exc_type != Py_None) {
828 /* We were in an except handler when we left,
829 restore the exception state which was put aside
830 (see YIELD_VALUE). */
831 SWAP_EXC_STATE();
832 }
833 else {
834 SAVE_EXC_STATE();
835 }
836 }
837
Tim Peters5ca576e2001-06-18 22:08:13 +0000838#ifdef LLTRACE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000839 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +0000840#endif
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000841#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum00bc0e02007-10-15 02:52:41 +0000842 filename = PyUnicode_AsString(co->co_filename);
Tim Peters5ca576e2001-06-18 22:08:13 +0000843#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000844
Guido van Rossum374a9221991-04-04 10:40:29 +0000845 why = WHY_NOT;
846 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +0000847 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +0000848 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +0000849
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000850 if (throwflag) { /* support for generator.throw() */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000851 why = WHY_EXCEPTION;
852 goto on_error;
853 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000854
Guido van Rossum374a9221991-04-04 10:40:29 +0000855 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000856#ifdef WITH_TSC
857 if (inst1 == 0) {
858 /* Almost surely, the opcode executed a break
859 or a continue, preventing inst1 from being set
860 on the way out of the loop.
861 */
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000862 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000863 loop1 = inst1;
864 }
865 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
866 intr0, intr1);
867 ticked = 0;
868 inst1 = 0;
869 intr0 = 0;
870 intr1 = 0;
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000871 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000872#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000873 assert(stack_pointer >= f->f_valuestack); /* else underflow */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000874 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000875
Guido van Rossuma027efa1997-05-05 20:56:21 +0000876 /* Do periodic things. Doing this every time through
877 the loop would add too much overhead, so we do it
878 only every Nth instruction. We also do it if
879 ``things_to_do'' is set, i.e. when an asynchronous
880 event needs attention (e.g. a signal handler or
881 async I/O handler); see Py_AddPendingCall() and
882 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000883
Skip Montanarod581d772002-09-03 20:10:45 +0000884 if (--_Py_Ticker < 0) {
Thomas Woutersce272b62007-09-19 21:19:28 +0000885 if (*next_instr == SETUP_FINALLY) {
886 /* Make the last opcode before
887 a try: finally: block uninterruptable. */
888 goto fast_next_opcode;
889 }
Skip Montanarod581d772002-09-03 20:10:45 +0000890 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000891 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000892#ifdef WITH_TSC
893 ticked = 1;
894#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000895 if (things_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +0000896 if (Py_MakePendingCalls() < 0) {
897 why = WHY_EXCEPTION;
898 goto on_error;
899 }
Kurt B. Kaiser4c79a832004-11-23 18:06:08 +0000900 if (things_to_do)
901 /* MakePendingCalls() didn't succeed.
902 Force early re-execution of this
903 "periodic" code, possibly after
904 a thread switch */
905 _Py_Ticker = 0;
Guido van Rossum8861b741996-07-30 16:49:37 +0000906 }
Guido van Rossume59214e1994-08-30 08:01:59 +0000907#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000908 if (interpreter_lock) {
909 /* Give another thread a chance */
910
Guido van Rossum25ce5661997-08-02 03:10:38 +0000911 if (PyThreadState_Swap(NULL) != tstate)
912 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000913 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000914
915 /* Other threads may run now */
916
Guido van Rossum65d5b571998-12-21 19:32:43 +0000917 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000918 if (PyThreadState_Swap(tstate) != NULL)
919 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000920
921 /* Check for thread interrupts */
922
923 if (tstate->async_exc != NULL) {
924 x = tstate->async_exc;
925 tstate->async_exc = NULL;
926 PyErr_SetNone(x);
927 Py_DECREF(x);
928 why = WHY_EXCEPTION;
929 goto on_error;
930 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000931 }
932#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000933 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000934
Neil Schemenauer63543862002-02-17 19:10:14 +0000935 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +0000936 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +0000937
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000938 /* line-by-line tracing support */
939
940 if (tstate->c_tracefunc != NULL && !tstate->tracing) {
941 /* see maybe_call_line_trace
942 for expository comments */
943 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +0000944
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000945 err = maybe_call_line_trace(tstate->c_tracefunc,
946 tstate->c_traceobj,
Armin Rigobf57a142004-03-22 19:24:58 +0000947 f, &instr_lb, &instr_ub,
948 &instr_prev);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000949 /* Reload possibly changed frame fields */
950 JUMPTO(f->f_lasti);
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000951 if (f->f_stacktop != NULL) {
952 stack_pointer = f->f_stacktop;
953 f->f_stacktop = NULL;
954 }
955 if (err) {
956 /* trace function raised an exception */
957 goto on_error;
958 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000959 }
960
961 /* Extract opcode and argument */
962
Guido van Rossum374a9221991-04-04 10:40:29 +0000963 opcode = NEXTOP();
Armin Rigo8817fcd2004-06-17 10:22:40 +0000964 oparg = 0; /* allows oparg to be stored in a register because
965 it doesn't have to be remembered across a full loop */
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000966 if (HAS_ARG(opcode))
967 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +0000968 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +0000969#ifdef DYNAMIC_EXECUTION_PROFILE
970#ifdef DXPAIRS
971 dxpairs[lastopcode][opcode]++;
972 lastopcode = opcode;
973#endif
974 dxp[opcode]++;
975#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000976
Guido van Rossum96a42c81992-01-12 02:29:51 +0000977#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +0000978 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +0000979
Guido van Rossum96a42c81992-01-12 02:29:51 +0000980 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000981 if (HAS_ARG(opcode)) {
982 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000983 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +0000984 }
985 else {
986 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000987 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +0000988 }
989 }
990#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000991
Guido van Rossum374a9221991-04-04 10:40:29 +0000992 /* Main switch on opcode */
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000993 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +0000994
Guido van Rossum374a9221991-04-04 10:40:29 +0000995 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +0000996
Guido van Rossum374a9221991-04-04 10:40:29 +0000997 /* BEWARE!
998 It is essential that any operation that fails sets either
999 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1000 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001001
Guido van Rossum374a9221991-04-04 10:40:29 +00001002 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001003
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001004 case NOP:
1005 goto fast_next_opcode;
1006
Neil Schemenauer63543862002-02-17 19:10:14 +00001007 case LOAD_FAST:
1008 x = GETLOCAL(oparg);
1009 if (x != NULL) {
1010 Py_INCREF(x);
1011 PUSH(x);
1012 goto fast_next_opcode;
1013 }
1014 format_exc_check_arg(PyExc_UnboundLocalError,
1015 UNBOUNDLOCAL_ERROR_MSG,
1016 PyTuple_GetItem(co->co_varnames, oparg));
1017 break;
1018
1019 case LOAD_CONST:
Skip Montanaro04d80f82002-08-04 21:03:35 +00001020 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +00001021 Py_INCREF(x);
1022 PUSH(x);
1023 goto fast_next_opcode;
1024
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001025 PREDICTED_WITH_ARG(STORE_FAST);
Neil Schemenauer63543862002-02-17 19:10:14 +00001026 case STORE_FAST:
1027 v = POP();
1028 SETLOCAL(oparg, v);
1029 goto fast_next_opcode;
1030
Raymond Hettingerf606f872003-03-16 03:11:04 +00001031 PREDICTED(POP_TOP);
Guido van Rossum374a9221991-04-04 10:40:29 +00001032 case POP_TOP:
1033 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001034 Py_DECREF(v);
Neil Schemenauer63543862002-02-17 19:10:14 +00001035 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001036
Guido van Rossum374a9221991-04-04 10:40:29 +00001037 case ROT_TWO:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001038 v = TOP();
1039 w = SECOND();
1040 SET_TOP(w);
1041 SET_SECOND(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +00001042 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001043
Guido van Rossum374a9221991-04-04 10:40:29 +00001044 case ROT_THREE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001045 v = TOP();
1046 w = SECOND();
1047 x = THIRD();
1048 SET_TOP(w);
1049 SET_SECOND(x);
1050 SET_THIRD(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +00001051 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001052
Thomas Wouters434d0822000-08-24 20:11:32 +00001053 case ROT_FOUR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001054 u = TOP();
1055 v = SECOND();
1056 w = THIRD();
1057 x = FOURTH();
1058 SET_TOP(v);
1059 SET_SECOND(w);
1060 SET_THIRD(x);
1061 SET_FOURTH(u);
Raymond Hettinger080cb322003-03-14 01:37:42 +00001062 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001063
Guido van Rossum374a9221991-04-04 10:40:29 +00001064 case DUP_TOP:
1065 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001066 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001067 PUSH(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +00001068 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001069
Thomas Wouters434d0822000-08-24 20:11:32 +00001070 case DUP_TOPX:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001071 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001072 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +00001073 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001074 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +00001075 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001076 STACKADJ(2);
1077 SET_TOP(x);
1078 SET_SECOND(w);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001079 goto fast_next_opcode;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001080 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001081 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +00001082 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001083 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +00001084 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001085 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +00001086 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001087 STACKADJ(3);
1088 SET_TOP(x);
1089 SET_SECOND(w);
1090 SET_THIRD(v);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001091 goto fast_next_opcode;
Thomas Wouters434d0822000-08-24 20:11:32 +00001092 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001093 Py_FatalError("invalid argument to DUP_TOPX"
1094 " (bytecode corruption?)");
Tim Peters35ba6892000-10-11 07:04:49 +00001095 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001096
Guido van Rossum374a9221991-04-04 10:40:29 +00001097 case UNARY_POSITIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001098 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001099 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001100 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001101 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001102 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001103 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001104
Guido van Rossum374a9221991-04-04 10:40:29 +00001105 case UNARY_NEGATIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001106 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001107 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001108 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001109 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001110 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001111 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001112
Guido van Rossum374a9221991-04-04 10:40:29 +00001113 case UNARY_NOT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001114 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001115 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001116 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +00001117 if (err == 0) {
1118 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001119 SET_TOP(Py_True);
Guido van Rossumfc490731997-05-06 15:06:49 +00001120 continue;
1121 }
1122 else if (err > 0) {
1123 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001124 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +00001125 err = 0;
1126 continue;
1127 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00001128 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001129 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001130
Guido van Rossum7928cd71991-10-24 14:59:31 +00001131 case UNARY_INVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001132 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001133 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001134 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001135 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001136 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001137 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001138
Guido van Rossum50564e81996-01-12 01:13:16 +00001139 case BINARY_POWER:
1140 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001141 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001142 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +00001143 Py_DECREF(v);
1144 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001145 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001146 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +00001147 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001148
Guido van Rossum374a9221991-04-04 10:40:29 +00001149 case BINARY_MULTIPLY:
1150 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001151 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001152 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001153 Py_DECREF(v);
1154 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001155 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001156 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001157 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001158
Tim Peters3caca232001-12-06 06:23:26 +00001159 case BINARY_TRUE_DIVIDE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001160 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001161 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001162 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001163 Py_DECREF(v);
1164 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001165 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001166 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001167 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001168
Guido van Rossum4668b002001-08-08 05:00:18 +00001169 case BINARY_FLOOR_DIVIDE:
1170 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001171 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001172 x = PyNumber_FloorDivide(v, w);
1173 Py_DECREF(v);
1174 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001175 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001176 if (x != NULL) continue;
1177 break;
1178
Guido van Rossum374a9221991-04-04 10:40:29 +00001179 case BINARY_MODULO:
1180 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001181 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001182 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001183 Py_DECREF(v);
1184 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001185 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001186 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001187 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001188
Guido van Rossum374a9221991-04-04 10:40:29 +00001189 case BINARY_ADD:
1190 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001191 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001192 if (PyUnicode_CheckExact(v) &&
Guido van Rossum98297ee2007-11-06 21:34:58 +00001193 PyUnicode_CheckExact(w)) {
1194 x = unicode_concatenate(v, w, f, next_instr);
1195 /* unicode_concatenate consumed the ref to v */
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001196 goto skip_decref_vx;
1197 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001198 else {
Guido van Rossumc12da691997-07-17 23:12:42 +00001199 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001200 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001201 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001202 skip_decref_vx:
Guido van Rossumb209a111997-04-29 18:18:01 +00001203 Py_DECREF(w);
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 BINARY_SUBTRACT:
1209 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001210 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001211 x = PyNumber_Subtract(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001212 Py_DECREF(v);
1213 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001214 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001215 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001216 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001217
Guido van Rossum374a9221991-04-04 10:40:29 +00001218 case BINARY_SUBSCR:
1219 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001220 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001221 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001222 Py_DECREF(v);
1223 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001224 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001225 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001226 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001227
Guido van Rossum7928cd71991-10-24 14:59:31 +00001228 case BINARY_LSHIFT:
1229 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001230 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001231 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001232 Py_DECREF(v);
1233 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001234 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001235 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001236 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001237
Guido van Rossum7928cd71991-10-24 14:59:31 +00001238 case BINARY_RSHIFT:
1239 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001240 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001241 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001242 Py_DECREF(v);
1243 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001244 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001245 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001246 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001247
Guido van Rossum7928cd71991-10-24 14:59:31 +00001248 case BINARY_AND:
1249 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001250 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001251 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001252 Py_DECREF(v);
1253 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001254 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001255 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001256 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001257
Guido van Rossum7928cd71991-10-24 14:59:31 +00001258 case BINARY_XOR:
1259 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001260 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001261 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001262 Py_DECREF(v);
1263 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001264 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001265 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001266 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001267
Guido van Rossum7928cd71991-10-24 14:59:31 +00001268 case BINARY_OR:
1269 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001270 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001271 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001272 Py_DECREF(v);
1273 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001274 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001275 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001276 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001277
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001278 case LIST_APPEND:
1279 w = POP();
1280 v = POP();
1281 err = PyList_Append(v, w);
1282 Py_DECREF(v);
1283 Py_DECREF(w);
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001284 if (err == 0) {
1285 PREDICT(JUMP_ABSOLUTE);
1286 continue;
1287 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001288 break;
1289
Nick Coghlan650f0d02007-04-15 12:05:43 +00001290 case SET_ADD:
1291 w = POP();
1292 v = POP();
1293 err = PySet_Add(v, w);
1294 Py_DECREF(v);
1295 Py_DECREF(w);
1296 if (err == 0) {
1297 PREDICT(JUMP_ABSOLUTE);
1298 continue;
1299 }
1300 break;
1301
Thomas Wouters434d0822000-08-24 20:11:32 +00001302 case INPLACE_POWER:
1303 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001304 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001305 x = PyNumber_InPlacePower(v, w, Py_None);
1306 Py_DECREF(v);
1307 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001308 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001309 if (x != NULL) continue;
1310 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001311
Thomas Wouters434d0822000-08-24 20:11:32 +00001312 case INPLACE_MULTIPLY:
1313 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001314 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001315 x = PyNumber_InPlaceMultiply(v, w);
1316 Py_DECREF(v);
1317 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001318 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001319 if (x != NULL) continue;
1320 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001321
Tim Peters54b11912001-12-25 18:49:11 +00001322 case INPLACE_TRUE_DIVIDE:
Thomas Wouters434d0822000-08-24 20:11:32 +00001323 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001324 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001325 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001326 Py_DECREF(v);
1327 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001328 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001329 if (x != NULL) continue;
1330 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001331
Guido van Rossum4668b002001-08-08 05:00:18 +00001332 case INPLACE_FLOOR_DIVIDE:
1333 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001334 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001335 x = PyNumber_InPlaceFloorDivide(v, w);
1336 Py_DECREF(v);
1337 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001338 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001339 if (x != NULL) continue;
1340 break;
1341
Thomas Wouters434d0822000-08-24 20:11:32 +00001342 case INPLACE_MODULO:
1343 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001344 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001345 x = PyNumber_InPlaceRemainder(v, w);
1346 Py_DECREF(v);
1347 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001348 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001349 if (x != NULL) continue;
1350 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001351
Thomas Wouters434d0822000-08-24 20:11:32 +00001352 case INPLACE_ADD:
1353 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001354 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001355 if (PyUnicode_CheckExact(v) &&
Guido van Rossum98297ee2007-11-06 21:34:58 +00001356 PyUnicode_CheckExact(w)) {
1357 x = unicode_concatenate(v, w, f, next_instr);
1358 /* unicode_concatenate consumed the ref to v */
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001359 goto skip_decref_v;
1360 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001361 else {
Thomas Wouters434d0822000-08-24 20:11:32 +00001362 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001363 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001364 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001365 skip_decref_v:
Thomas Wouters434d0822000-08-24 20:11:32 +00001366 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001367 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001368 if (x != NULL) continue;
1369 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001370
Thomas Wouters434d0822000-08-24 20:11:32 +00001371 case INPLACE_SUBTRACT:
1372 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001373 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001374 x = PyNumber_InPlaceSubtract(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001375 Py_DECREF(v);
1376 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001377 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001378 if (x != NULL) continue;
1379 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001380
Thomas Wouters434d0822000-08-24 20:11:32 +00001381 case INPLACE_LSHIFT:
1382 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001383 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001384 x = PyNumber_InPlaceLshift(v, w);
1385 Py_DECREF(v);
1386 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001387 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001388 if (x != NULL) continue;
1389 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001390
Thomas Wouters434d0822000-08-24 20:11:32 +00001391 case INPLACE_RSHIFT:
1392 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001393 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001394 x = PyNumber_InPlaceRshift(v, w);
1395 Py_DECREF(v);
1396 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001397 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001398 if (x != NULL) continue;
1399 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001400
Thomas Wouters434d0822000-08-24 20:11:32 +00001401 case INPLACE_AND:
1402 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001403 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001404 x = PyNumber_InPlaceAnd(v, w);
1405 Py_DECREF(v);
1406 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001407 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001408 if (x != NULL) continue;
1409 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001410
Thomas Wouters434d0822000-08-24 20:11:32 +00001411 case INPLACE_XOR:
1412 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001413 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001414 x = PyNumber_InPlaceXor(v, w);
1415 Py_DECREF(v);
1416 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001417 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001418 if (x != NULL) continue;
1419 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001420
Thomas Wouters434d0822000-08-24 20:11:32 +00001421 case INPLACE_OR:
1422 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001423 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001424 x = PyNumber_InPlaceOr(v, w);
1425 Py_DECREF(v);
1426 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001427 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001428 if (x != NULL) continue;
1429 break;
1430
Guido van Rossum374a9221991-04-04 10:40:29 +00001431 case STORE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001432 w = TOP();
1433 v = SECOND();
1434 u = THIRD();
1435 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001436 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001437 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001438 Py_DECREF(u);
1439 Py_DECREF(v);
1440 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001441 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001442 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001443
Guido van Rossum374a9221991-04-04 10:40:29 +00001444 case DELETE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001445 w = TOP();
1446 v = SECOND();
1447 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001448 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001449 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001450 Py_DECREF(v);
1451 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001452 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001453 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001454
Guido van Rossum374a9221991-04-04 10:40:29 +00001455 case PRINT_EXPR:
1456 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001457 w = PySys_GetObject("displayhook");
1458 if (w == NULL) {
1459 PyErr_SetString(PyExc_RuntimeError,
1460 "lost sys.displayhook");
1461 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001462 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001463 }
1464 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001465 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001466 if (x == NULL)
1467 err = -1;
1468 }
1469 if (err == 0) {
1470 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001471 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001472 if (w == NULL)
1473 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001474 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001475 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001476 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001477 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001478
Thomas Wouters434d0822000-08-24 20:11:32 +00001479#ifdef CASE_TOO_BIG
1480 default: switch (opcode) {
1481#endif
Guido van Rossumf10570b1995-07-07 22:53:21 +00001482 case RAISE_VARARGS:
Collin Winter828f04a2007-08-31 00:04:24 +00001483 v = w = NULL;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001484 switch (oparg) {
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00001485 case 2:
1486 v = POP(); /* cause */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001487 case 1:
1488 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001489 case 0: /* Fallthrough */
Collin Winter828f04a2007-08-31 00:04:24 +00001490 why = do_raise(w, v);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001491 break;
1492 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001493 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001494 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001495 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001496 break;
1497 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001498 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001499
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001500 case STORE_LOCALS:
1501 x = POP();
1502 v = f->f_locals;
1503 Py_XDECREF(v);
1504 f->f_locals = x;
1505 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001506
Guido van Rossum374a9221991-04-04 10:40:29 +00001507 case RETURN_VALUE:
1508 retval = POP();
1509 why = WHY_RETURN;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001510 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001511
Tim Peters5ca576e2001-06-18 22:08:13 +00001512 case YIELD_VALUE:
1513 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001514 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001515 why = WHY_YIELD;
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001516 /* Put aside the current exception state and restore
1517 that of the calling frame. This only serves when
1518 "yield" is used inside an except handler. */
1519 SWAP_EXC_STATE();
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001520 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001521
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001522 case POP_EXCEPT:
1523 {
1524 PyTryBlock *b = PyFrame_BlockPop(f);
1525 if (b->b_type != EXCEPT_HANDLER) {
1526 PyErr_SetString(PyExc_SystemError,
1527 "popped block is not an except handler");
1528 why = WHY_EXCEPTION;
1529 break;
1530 }
1531 UNWIND_EXCEPT_HANDLER(b);
1532 }
1533 continue;
1534
Guido van Rossum374a9221991-04-04 10:40:29 +00001535 case POP_BLOCK:
1536 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001537 PyTryBlock *b = PyFrame_BlockPop(f);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001538 UNWIND_BLOCK(b);
Guido van Rossum374a9221991-04-04 10:40:29 +00001539 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001540 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001541
Christian Heimes180510d2008-03-03 19:15:45 +00001542 PREDICTED(END_FINALLY);
Guido van Rossum374a9221991-04-04 10:40:29 +00001543 case END_FINALLY:
1544 v = POP();
Christian Heimes217cfd12007-12-02 14:31:20 +00001545 if (PyLong_Check(v)) {
1546 why = (enum why_code) PyLong_AS_LONG(v);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001547 assert(why != WHY_YIELD);
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00001548 if (why == WHY_RETURN ||
1549 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001550 retval = POP();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001551 if (why == WHY_SILENCED) {
1552 /* An exception was silenced by 'with', we must
1553 manually unwind the EXCEPT_HANDLER block which was
1554 created when the exception was caught, otherwise
1555 the stack will be in an inconsistent state. */
1556 PyTryBlock *b = PyFrame_BlockPop(f);
1557 if (b->b_type != EXCEPT_HANDLER) {
1558 PyErr_SetString(PyExc_SystemError,
1559 "popped block is not an except handler");
1560 why = WHY_EXCEPTION;
1561 }
1562 else {
1563 UNWIND_EXCEPT_HANDLER(b);
1564 why = WHY_NOT;
1565 }
1566 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001567 }
Brett Cannonf74225d2007-02-26 21:10:16 +00001568 else if (PyExceptionClass_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001569 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001570 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001571 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001572 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001573 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001574 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001575 else if (v != Py_None) {
1576 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001577 "'finally' pops bad exception");
1578 why = WHY_EXCEPTION;
1579 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001580 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001581 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001582
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001583 case LOAD_BUILD_CLASS:
1584 x = PyDict_GetItemString(f->f_builtins,
1585 "__build_class__");
1586 if (x == NULL) {
1587 PyErr_SetString(PyExc_ImportError,
1588 "__build_class__ not found");
1589 break;
1590 }
1591 Py_INCREF(x);
1592 PUSH(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001593 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001594
Guido van Rossum374a9221991-04-04 10:40:29 +00001595 case STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001596 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001597 v = POP();
Raymond Hettinger467a6982004-04-07 11:39:21 +00001598 if ((x = f->f_locals) != NULL) {
Raymond Hettinger66bd2332004-08-02 08:30:07 +00001599 if (PyDict_CheckExact(x))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001600 err = PyDict_SetItem(x, w, v);
1601 else
1602 err = PyObject_SetItem(x, w, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001603 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001604 if (err == 0) continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001605 break;
1606 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001607 PyErr_Format(PyExc_SystemError,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001608 "no locals found when storing %R", w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001609 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001610
Guido van Rossum374a9221991-04-04 10:40:29 +00001611 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001612 w = GETITEM(names, oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001613 if ((x = f->f_locals) != NULL) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001614 if ((err = PyObject_DelItem(x, w)) != 0)
Raymond Hettinger467a6982004-04-07 11:39:21 +00001615 format_exc_check_arg(PyExc_NameError,
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001616 NAME_ERROR_MSG,
1617 w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001618 break;
1619 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001620 PyErr_Format(PyExc_SystemError,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001621 "no locals when deleting %R", w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001622 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001623
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001624 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001625 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001626 v = POP();
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001627 if (PyTuple_CheckExact(v) &&
1628 PyTuple_GET_SIZE(v) == oparg) {
1629 PyObject **items = \
1630 ((PyTupleObject *)v)->ob_item;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001631 while (oparg--) {
1632 w = items[oparg];
1633 Py_INCREF(w);
1634 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001635 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001636 Py_DECREF(v);
1637 continue;
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001638 } else if (PyList_CheckExact(v) &&
1639 PyList_GET_SIZE(v) == oparg) {
1640 PyObject **items = \
1641 ((PyListObject *)v)->ob_item;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001642 while (oparg--) {
1643 w = items[oparg];
1644 Py_INCREF(w);
1645 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001646 }
Guido van Rossum0368b722007-05-11 16:50:42 +00001647 } else if (unpack_iterable(v, oparg, -1,
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001648 stack_pointer + oparg)) {
Tim Petersd6d010b2001-06-21 02:49:55 +00001649 stack_pointer += oparg;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001650 } else {
1651 /* unpack_iterable() raised an exception */
Barry Warsawe42b18f1997-08-25 22:13:04 +00001652 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001653 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001654 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001655 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001656
Guido van Rossum0368b722007-05-11 16:50:42 +00001657 case UNPACK_EX:
1658 {
1659 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
1660 v = POP();
1661
1662 if (unpack_iterable(v, oparg & 0xFF, oparg >> 8,
1663 stack_pointer + totalargs)) {
1664 stack_pointer += totalargs;
1665 } else {
1666 why = WHY_EXCEPTION;
1667 }
1668 Py_DECREF(v);
1669 break;
1670 }
1671
Guido van Rossum374a9221991-04-04 10:40:29 +00001672 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001673 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001674 v = TOP();
1675 u = SECOND();
1676 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001677 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1678 Py_DECREF(v);
1679 Py_DECREF(u);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001680 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001681 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001682
Guido van Rossum374a9221991-04-04 10:40:29 +00001683 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001684 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001685 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001686 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1687 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001688 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001689 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001690
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001691 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001692 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001693 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001694 err = PyDict_SetItem(f->f_globals, w, v);
1695 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001696 if (err == 0) continue;
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001697 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001698
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001699 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001700 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001701 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001702 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001703 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001704 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001705
Guido van Rossum374a9221991-04-04 10:40:29 +00001706 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001707 w = GETITEM(names, oparg);
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001708 if ((v = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001709 PyErr_Format(PyExc_SystemError,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001710 "no locals when loading %R", w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001711 break;
1712 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001713 if (PyDict_CheckExact(v)) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001714 x = PyDict_GetItem(v, w);
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001715 Py_XINCREF(x);
1716 }
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001717 else {
1718 x = PyObject_GetItem(v, w);
1719 if (x == NULL && PyErr_Occurred()) {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001720 if (!PyErr_ExceptionMatches(
1721 PyExc_KeyError))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001722 break;
1723 PyErr_Clear();
1724 }
1725 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001726 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001727 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001728 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001729 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001730 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001731 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001732 PyExc_NameError,
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001733 NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001734 break;
1735 }
1736 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001737 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001738 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001739 PUSH(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001740 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001741
Guido van Rossum374a9221991-04-04 10:40:29 +00001742 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001743 w = GETITEM(names, oparg);
Neal Norwitzda059e32007-08-26 05:33:45 +00001744 if (PyUnicode_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00001745 /* Inline the PyDict_GetItem() calls.
1746 WARNING: this is an extreme speed hack.
1747 Do not try this at home. */
Neal Norwitzda059e32007-08-26 05:33:45 +00001748 long hash = ((PyUnicodeObject *)w)->hash;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001749 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001750 PyDictObject *d;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001751 PyDictEntry *e;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001752 d = (PyDictObject *)(f->f_globals);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001753 e = d->ma_lookup(d, w, hash);
1754 if (e == NULL) {
1755 x = NULL;
1756 break;
1757 }
1758 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001759 if (x != NULL) {
1760 Py_INCREF(x);
1761 PUSH(x);
1762 continue;
1763 }
1764 d = (PyDictObject *)(f->f_builtins);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001765 e = d->ma_lookup(d, w, hash);
1766 if (e == NULL) {
1767 x = NULL;
1768 break;
1769 }
1770 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001771 if (x != NULL) {
1772 Py_INCREF(x);
1773 PUSH(x);
1774 continue;
1775 }
1776 goto load_global_error;
1777 }
1778 }
1779 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00001780 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001781 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001782 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001783 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001784 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00001785 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001786 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001787 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001788 break;
1789 }
1790 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001791 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001792 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001793 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001794
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001795 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001796 x = GETLOCAL(oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001797 if (x != NULL) {
1798 SETLOCAL(oparg, NULL);
1799 continue;
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001800 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001801 format_exc_check_arg(
1802 PyExc_UnboundLocalError,
1803 UNBOUNDLOCAL_ERROR_MSG,
1804 PyTuple_GetItem(co->co_varnames, oparg)
1805 );
1806 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001807
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001808 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001809 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001810 Py_INCREF(x);
1811 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001812 if (x != NULL) continue;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001813 break;
1814
1815 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001816 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001817 w = PyCell_Get(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001818 if (w != NULL) {
1819 PUSH(w);
1820 continue;
Jeremy Hylton2524d692001-02-05 17:23:16 +00001821 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001822 err = -1;
1823 /* Don't stomp existing exception */
1824 if (PyErr_Occurred())
1825 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001826 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
1827 v = PyTuple_GET_ITEM(co->co_cellvars,
Raymond Hettinger467a6982004-04-07 11:39:21 +00001828 oparg);
1829 format_exc_check_arg(
1830 PyExc_UnboundLocalError,
1831 UNBOUNDLOCAL_ERROR_MSG,
1832 v);
1833 } else {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001834 v = PyTuple_GET_ITEM(co->co_freevars, oparg -
1835 PyTuple_GET_SIZE(co->co_cellvars));
1836 format_exc_check_arg(PyExc_NameError,
1837 UNBOUNDFREE_ERROR_MSG, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001838 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001839 break;
1840
1841 case STORE_DEREF:
1842 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001843 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001844 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00001845 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001846 continue;
1847
Guido van Rossum374a9221991-04-04 10:40:29 +00001848 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001849 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001850 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001851 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001852 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001853 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001854 }
1855 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001856 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001857 }
1858 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001859
Guido van Rossum374a9221991-04-04 10:40:29 +00001860 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00001861 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001862 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001863 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001864 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00001865 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001866 }
1867 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001868 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001869 }
1870 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001871
Guido van Rossum86e58e22006-08-28 15:27:34 +00001872 case BUILD_SET:
1873 x = PySet_New(NULL);
1874 if (x != NULL) {
1875 for (; --oparg >= 0;) {
1876 w = POP();
1877 if (err == 0)
1878 err = PySet_Add(x, w);
1879 Py_DECREF(w);
1880 }
1881 if (err != 0) {
1882 Py_DECREF(x);
1883 break;
1884 }
1885 PUSH(x);
1886 continue;
1887 }
1888 break;
1889
Guido van Rossum374a9221991-04-04 10:40:29 +00001890 case BUILD_MAP:
Christian Heimes99170a52007-12-19 02:07:34 +00001891 x = _PyDict_NewPresized((Py_ssize_t)oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001892 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001893 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001894 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001895
Christian Heimes99170a52007-12-19 02:07:34 +00001896 case STORE_MAP:
1897 w = TOP(); /* key */
1898 u = SECOND(); /* value */
1899 v = THIRD(); /* dict */
1900 STACKADJ(-2);
1901 assert (PyDict_CheckExact(v));
1902 err = PyDict_SetItem(v, w, u); /* v[w] = u */
1903 Py_DECREF(u);
1904 Py_DECREF(w);
1905 if (err == 0) continue;
1906 break;
1907
Guido van Rossum374a9221991-04-04 10:40:29 +00001908 case LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001909 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001910 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001911 x = PyObject_GetAttr(v, w);
1912 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001913 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001914 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001915 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001916
Guido van Rossum374a9221991-04-04 10:40:29 +00001917 case COMPARE_OP:
1918 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001919 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001920 x = cmp_outcome(oparg, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001921 Py_DECREF(v);
1922 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001923 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001924 if (x == NULL) break;
1925 PREDICT(JUMP_IF_FALSE);
1926 PREDICT(JUMP_IF_TRUE);
1927 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001928
Guido van Rossum374a9221991-04-04 10:40:29 +00001929 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001930 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001931 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001932 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001933 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00001934 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001935 break;
1936 }
Christian Heimesbbffeb62008-01-24 09:42:52 +00001937 Py_INCREF(x);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001938 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001939 u = TOP();
Christian Heimes217cfd12007-12-02 14:31:20 +00001940 if (PyLong_AsLong(u) != -1 || PyErr_Occurred())
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001941 w = PyTuple_Pack(5,
1942 w,
1943 f->f_globals,
1944 f->f_locals == NULL ?
1945 Py_None : f->f_locals,
1946 v,
1947 u);
1948 else
1949 w = PyTuple_Pack(4,
1950 w,
1951 f->f_globals,
1952 f->f_locals == NULL ?
1953 Py_None : f->f_locals,
1954 v);
1955 Py_DECREF(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001956 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001957 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001958 u = POP();
Christian Heimesbbffeb62008-01-24 09:42:52 +00001959 Py_DECREF(x);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001960 x = NULL;
1961 break;
1962 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001963 READ_TIMESTAMP(intr0);
Christian Heimesbbffeb62008-01-24 09:42:52 +00001964 v = x;
1965 x = PyEval_CallObject(v, w);
1966 Py_DECREF(v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001967 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00001968 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001969 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001970 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001971 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001972
Thomas Wouters52152252000-08-17 22:55:00 +00001973 case IMPORT_STAR:
1974 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001975 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001976 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001977 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001978 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001979 break;
1980 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001981 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00001982 err = import_all_from(x, v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001983 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00001984 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00001985 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001986 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001987 break;
Guido van Rossum25831651993-05-19 14:50:45 +00001988
Thomas Wouters52152252000-08-17 22:55:00 +00001989 case IMPORT_FROM:
Skip Montanaro496e6582002-08-06 17:47:40 +00001990 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00001991 v = TOP();
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001992 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00001993 x = import_from(v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001994 READ_TIMESTAMP(intr1);
Thomas Wouters52152252000-08-17 22:55:00 +00001995 PUSH(x);
1996 if (x != NULL) continue;
1997 break;
1998
Guido van Rossum374a9221991-04-04 10:40:29 +00001999 case JUMP_FORWARD:
2000 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002001 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002002
Raymond Hettingerf606f872003-03-16 03:11:04 +00002003 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002004 case JUMP_IF_FALSE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002005 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002006 if (w == Py_True) {
2007 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002008 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002009 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002010 if (w == Py_False) {
2011 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002012 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002013 }
2014 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002015 if (err > 0)
2016 err = 0;
2017 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00002018 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002019 else
2020 break;
2021 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002022
Raymond Hettingerf606f872003-03-16 03:11:04 +00002023 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002024 case JUMP_IF_TRUE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002025 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002026 if (w == Py_False) {
2027 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002028 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002029 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002030 if (w == Py_True) {
2031 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002032 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002033 }
2034 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002035 if (err > 0) {
2036 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00002037 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002038 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002039 else if (err == 0)
2040 ;
2041 else
2042 break;
2043 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002044
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00002045 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002046 case JUMP_ABSOLUTE:
2047 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002048#if FAST_LOOPS
2049 /* Enabling this path speeds-up all while and for-loops by bypassing
2050 the per-loop checks for signals. By default, this should be turned-off
2051 because it prevents detection of a control-break in tight loops like
2052 "while 1: pass". Compile with this option turned-on when you need
2053 the speed-up and do not need break checking inside tight loops (ones
2054 that contain only instructions ending with goto fast_next_opcode).
2055 */
2056 goto fast_next_opcode;
2057#else
Neil Schemenauerca2a2f12003-05-30 23:59:44 +00002058 continue;
Guido van Rossum58da9312007-11-10 23:39:45 +00002059#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002060
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002061 case GET_ITER:
2062 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002063 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002064 x = PyObject_GetIter(v);
2065 Py_DECREF(v);
2066 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002067 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002068 PREDICT(FOR_ITER);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002069 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002070 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002071 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002072 break;
2073
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002074 PREDICTED_WITH_ARG(FOR_ITER);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002075 case FOR_ITER:
2076 /* before: [iter]; after: [iter, iter()] *or* [] */
2077 v = TOP();
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002078 x = (*v->ob_type->tp_iternext)(v);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002079 if (x != NULL) {
2080 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002081 PREDICT(STORE_FAST);
2082 PREDICT(UNPACK_SEQUENCE);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002083 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002084 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002085 if (PyErr_Occurred()) {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00002086 if (!PyErr_ExceptionMatches(
2087 PyExc_StopIteration))
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002088 break;
2089 PyErr_Clear();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002090 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002091 /* iterator ended normally */
2092 x = v = POP();
2093 Py_DECREF(v);
2094 JUMPBY(oparg);
2095 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002096
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002097 case BREAK_LOOP:
2098 why = WHY_BREAK;
2099 goto fast_block_end;
2100
2101 case CONTINUE_LOOP:
Christian Heimes217cfd12007-12-02 14:31:20 +00002102 retval = PyLong_FromLong(oparg);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002103 if (!retval) {
2104 x = NULL;
2105 break;
2106 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002107 why = WHY_CONTINUE;
2108 goto fast_block_end;
2109
Guido van Rossum374a9221991-04-04 10:40:29 +00002110 case SETUP_LOOP:
2111 case SETUP_EXCEPT:
2112 case SETUP_FINALLY:
Thomas Wouters8ce81f72007-09-20 18:22:40 +00002113 /* NOTE: If you add any new block-setup opcodes that
2114 are not try/except/finally handlers, you may need
2115 to update the PyGen_NeedsFinalizing() function.
2116 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002117
Guido van Rossumb209a111997-04-29 18:18:01 +00002118 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002119 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002120 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002121
Guido van Rossumc2e20742006-02-27 22:32:47 +00002122 case WITH_CLEANUP:
2123 {
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002124 /* At the top of the stack are 1-3 values indicating
2125 how/why we entered the finally clause:
2126 - TOP = None
2127 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2128 - TOP = WHY_*; no retval below it
2129 - (TOP, SECOND, THIRD) = exc_info()
2130 Below them is EXIT, the context.__exit__ bound method.
Guido van Rossumc2e20742006-02-27 22:32:47 +00002131 In the last case, we must call
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002132 EXIT(TOP, SECOND, THIRD)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002133 otherwise we must call
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002134 EXIT(None, None, None)
2135
2136 In all cases, we remove EXIT from the stack, leaving
2137 the rest in the same order.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002138
2139 In addition, if the stack represents an exception,
Guido van Rossumf6694362006-03-10 02:28:35 +00002140 *and* the function call returns a 'true' value, we
2141 "zap" this information, to prevent END_FINALLY from
2142 re-raising the exception. (But non-local gotos
2143 should still be resumed.)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002144 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002145
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002146 PyObject *exit_func = POP();
2147 u = TOP();
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002148 if (u == Py_None) {
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002149 v = w = Py_None;
2150 }
2151 else if (PyLong_Check(u)) {
Guido van Rossumc2e20742006-02-27 22:32:47 +00002152 u = v = w = Py_None;
2153 }
2154 else {
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002155 v = SECOND();
2156 w = THIRD();
Guido van Rossumc2e20742006-02-27 22:32:47 +00002157 }
Guido van Rossumf6694362006-03-10 02:28:35 +00002158 /* XXX Not the fastest way to call it... */
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002159 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2160 NULL);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002161 Py_DECREF(exit_func);
2162 if (x == NULL)
Guido van Rossumf6694362006-03-10 02:28:35 +00002163 break; /* Go to error exit */
2164 if (u != Py_None && PyObject_IsTrue(x)) {
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002165 /* There was an exception and a True return */
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002166 STACKADJ(-2);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002167 SET_TOP(PyLong_FromLong((long) WHY_SILENCED));
Guido van Rossumf6694362006-03-10 02:28:35 +00002168 Py_DECREF(u);
2169 Py_DECREF(v);
2170 Py_DECREF(w);
Guido van Rossumf6694362006-03-10 02:28:35 +00002171 }
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002172 Py_DECREF(x);
Christian Heimes180510d2008-03-03 19:15:45 +00002173 PREDICT(END_FINALLY);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002174 break;
2175 }
2176
Guido van Rossumf10570b1995-07-07 22:53:21 +00002177 case CALL_FUNCTION:
Armin Rigo8817fcd2004-06-17 10:22:40 +00002178 {
2179 PyObject **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002180 PCALL(PCALL_ALL);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002181 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002182#ifdef WITH_TSC
Armin Rigo8817fcd2004-06-17 10:22:40 +00002183 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002184#else
Armin Rigo8817fcd2004-06-17 10:22:40 +00002185 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002186#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002187 stack_pointer = sp;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002188 PUSH(x);
2189 if (x != NULL)
2190 continue;
2191 break;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002192 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002193
Jeremy Hylton76901512000-03-28 23:49:17 +00002194 case CALL_FUNCTION_VAR:
2195 case CALL_FUNCTION_KW:
2196 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002197 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002198 int na = oparg & 0xff;
2199 int nk = (oparg>>8) & 0xff;
2200 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002201 int n = na + 2 * nk;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002202 PyObject **pfunc, *func, **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002203 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002204 if (flags & CALL_FLAG_VAR)
2205 n++;
2206 if (flags & CALL_FLAG_KW)
2207 n++;
2208 pfunc = stack_pointer - n - 1;
2209 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002210
Guido van Rossumac7be682001-01-17 15:42:30 +00002211 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002212 && PyMethod_GET_SELF(func) != NULL) {
2213 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002214 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002215 func = PyMethod_GET_FUNCTION(func);
2216 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002217 Py_DECREF(*pfunc);
2218 *pfunc = self;
2219 na++;
2220 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002221 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002222 Py_INCREF(func);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002223 sp = stack_pointer;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002224 READ_TIMESTAMP(intr0);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002225 x = ext_do_call(func, &sp, flags, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002226 READ_TIMESTAMP(intr1);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002227 stack_pointer = sp;
Jeremy Hylton76901512000-03-28 23:49:17 +00002228 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002229
Jeremy Hylton76901512000-03-28 23:49:17 +00002230 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002231 w = POP();
2232 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002233 }
2234 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002235 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002236 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002237 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002238 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002239
Guido van Rossum0240b922007-02-26 21:23:50 +00002240 case MAKE_CLOSURE:
Guido van Rossum681d79a1995-07-18 14:51:37 +00002241 case MAKE_FUNCTION:
Guido van Rossum4f72a782006-10-27 23:31:49 +00002242 {
2243 int posdefaults = oparg & 0xff;
2244 int kwdefaults = (oparg>>8) & 0xff;
Neal Norwitzc1505362006-12-28 06:47:50 +00002245 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002246
Guido van Rossum681d79a1995-07-18 14:51:37 +00002247 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002248 x = PyFunction_New(v, f->f_globals);
2249 Py_DECREF(v);
Guido van Rossum0240b922007-02-26 21:23:50 +00002250
2251 if (x != NULL && opcode == MAKE_CLOSURE) {
2252 v = POP();
2253 err = PyFunction_SetClosure(x, v);
2254 Py_DECREF(v);
2255 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002256
2257 if (x != NULL && num_annotations > 0) {
2258 Py_ssize_t name_ix;
2259 u = POP(); /* names of args with annotations */
2260 v = PyDict_New();
2261 if (v == NULL) {
2262 Py_DECREF(x);
2263 x = NULL;
2264 break;
2265 }
2266 name_ix = PyTuple_Size(u);
2267 assert(num_annotations == name_ix+1);
2268 while (name_ix > 0) {
2269 --name_ix;
2270 t = PyTuple_GET_ITEM(u, name_ix);
2271 w = POP();
2272 /* XXX(nnorwitz): check for errors */
2273 PyDict_SetItem(v, t, w);
2274 Py_DECREF(w);
2275 }
2276
2277 err = PyFunction_SetAnnotations(x, v);
2278 Py_DECREF(v);
2279 Py_DECREF(u);
2280 }
2281
Guido van Rossum681d79a1995-07-18 14:51:37 +00002282 /* XXX Maybe this should be a separate opcode? */
Guido van Rossum4f72a782006-10-27 23:31:49 +00002283 if (x != NULL && posdefaults > 0) {
2284 v = PyTuple_New(posdefaults);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002285 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002286 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002287 x = NULL;
2288 break;
2289 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002290 while (--posdefaults >= 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002291 w = POP();
Guido van Rossum4f72a782006-10-27 23:31:49 +00002292 PyTuple_SET_ITEM(v, posdefaults, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002293 }
2294 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002295 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002296 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002297 if (x != NULL && kwdefaults > 0) {
2298 v = PyDict_New();
2299 if (v == NULL) {
2300 Py_DECREF(x);
2301 x = NULL;
2302 break;
2303 }
2304 while (--kwdefaults >= 0) {
2305 w = POP(); /* default value */
2306 u = POP(); /* kw only arg name */
Neal Norwitzc1505362006-12-28 06:47:50 +00002307 /* XXX(nnorwitz): check for errors */
Guido van Rossum4f72a782006-10-27 23:31:49 +00002308 PyDict_SetItem(v, u, w);
Georg Brandl94ab0002007-02-26 13:58:18 +00002309 Py_DECREF(w);
2310 Py_DECREF(u);
Guido van Rossum4f72a782006-10-27 23:31:49 +00002311 }
2312 err = PyFunction_SetKwDefaults(x, v);
2313 Py_DECREF(v);
2314 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002315 PUSH(x);
2316 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002317 }
Guido van Rossum8861b741996-07-30 16:49:37 +00002318
2319 case BUILD_SLICE:
2320 if (oparg == 3)
2321 w = POP();
2322 else
2323 w = NULL;
2324 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002325 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002326 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002327 Py_DECREF(u);
2328 Py_DECREF(v);
2329 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002330 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002331 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002332 break;
2333
Fred Drakeef8ace32000-08-24 00:32:09 +00002334 case EXTENDED_ARG:
2335 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002336 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002337 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002338
Guido van Rossum374a9221991-04-04 10:40:29 +00002339 default:
2340 fprintf(stderr,
2341 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002342 PyCode_Addr2Line(f->f_code, f->f_lasti),
2343 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002344 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002345 why = WHY_EXCEPTION;
2346 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002347
2348#ifdef CASE_TOO_BIG
2349 }
2350#endif
2351
Guido van Rossum374a9221991-04-04 10:40:29 +00002352 } /* switch */
2353
2354 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002355
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002356 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002357
Guido van Rossum374a9221991-04-04 10:40:29 +00002358 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002359
Guido van Rossum374a9221991-04-04 10:40:29 +00002360 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002361 if (err == 0 && x != NULL) {
2362#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002363 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002364 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002365 fprintf(stderr,
2366 "XXX undetected error\n");
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002367 else {
2368#endif
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002369 READ_TIMESTAMP(loop1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002370 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002371#ifdef CHECKEXC
2372 }
2373#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +00002374 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002375 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002376 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002377 err = 0;
2378 }
2379
Guido van Rossum374a9221991-04-04 10:40:29 +00002380 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002381
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002382 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002383 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002384 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002385 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002386 why = WHY_EXCEPTION;
2387 }
2388 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002389#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002390 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002391 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002392 if (PyErr_Occurred()) {
Christian Heimes412dc9c2008-01-27 18:55:54 +00002393 char buf[128];
Jeremy Hylton904ed862003-11-05 17:29:35 +00002394 sprintf(buf, "Stack unwind with exception "
2395 "set and why=%d", why);
2396 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002397 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002398 }
2399#endif
2400
2401 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002402
Guido van Rossum374a9221991-04-04 10:40:29 +00002403 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002404 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002405
Fred Drake8f51f542001-10-04 14:48:42 +00002406 if (tstate->c_tracefunc != NULL)
2407 call_exc_trace(tstate->c_tracefunc,
2408 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002409 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002410
Guido van Rossum374a9221991-04-04 10:40:29 +00002411 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002412
Guido van Rossum374a9221991-04-04 10:40:29 +00002413 if (why == WHY_RERAISE)
2414 why = WHY_EXCEPTION;
2415
2416 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002417
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002418fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002419 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002420 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002421
Tim Peters8a5c3c72004-04-05 19:36:21 +00002422 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002423 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2424 /* For a continue inside a try block,
2425 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002426 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2427 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002428 why = WHY_NOT;
Christian Heimes217cfd12007-12-02 14:31:20 +00002429 JUMPTO(PyLong_AS_LONG(retval));
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002430 Py_DECREF(retval);
2431 break;
2432 }
2433
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002434 if (b->b_type == EXCEPT_HANDLER) {
2435 UNWIND_EXCEPT_HANDLER(b);
2436 if (why == WHY_EXCEPTION) {
2437 Py_CLEAR(tstate->exc_type);
2438 Py_CLEAR(tstate->exc_value);
2439 Py_CLEAR(tstate->exc_traceback);
2440 }
2441 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002442 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002443 UNWIND_BLOCK(b);
Guido van Rossum374a9221991-04-04 10:40:29 +00002444 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2445 why = WHY_NOT;
2446 JUMPTO(b->b_handler);
2447 break;
2448 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002449 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
2450 || b->b_type == SETUP_FINALLY)) {
2451 PyObject *exc, *val, *tb;
2452 int handler = b->b_handler;
2453 /* Beware, this invalidates all b->b_* fields */
2454 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
2455 PUSH(tstate->exc_traceback);
2456 PUSH(tstate->exc_value);
2457 if (tstate->exc_type != NULL) {
2458 PUSH(tstate->exc_type);
Guido van Rossum374a9221991-04-04 10:40:29 +00002459 }
2460 else {
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002461 Py_INCREF(Py_None);
2462 PUSH(Py_None);
Guido van Rossum374a9221991-04-04 10:40:29 +00002463 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002464 PyErr_Fetch(&exc, &val, &tb);
2465 /* Make the raw exception data
2466 available to the handler,
2467 so a program can emulate the
2468 Python main loop. */
2469 PyErr_NormalizeException(
2470 &exc, &val, &tb);
2471 PyException_SetTraceback(val, tb);
2472 Py_INCREF(exc);
2473 tstate->exc_type = exc;
2474 Py_INCREF(val);
2475 tstate->exc_value = val;
2476 tstate->exc_traceback = tb;
2477 if (tb == NULL)
2478 tb = Py_None;
2479 Py_INCREF(tb);
2480 PUSH(tb);
2481 PUSH(val);
2482 PUSH(exc);
2483 why = WHY_NOT;
2484 JUMPTO(handler);
2485 break;
2486 }
2487 if (b->b_type == SETUP_FINALLY) {
2488 if (why & (WHY_RETURN | WHY_CONTINUE))
2489 PUSH(retval);
2490 PUSH(PyLong_FromLong((long)why));
Guido van Rossum374a9221991-04-04 10:40:29 +00002491 why = WHY_NOT;
2492 JUMPTO(b->b_handler);
2493 break;
2494 }
2495 } /* unwind stack */
2496
2497 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002498
Guido van Rossum374a9221991-04-04 10:40:29 +00002499 if (why != WHY_NOT)
2500 break;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002501 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002502
Guido van Rossum374a9221991-04-04 10:40:29 +00002503 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002504
Tim Peters8a5c3c72004-04-05 19:36:21 +00002505 assert(why != WHY_YIELD);
2506 /* Pop remaining stack entries. */
2507 while (!EMPTY()) {
2508 v = POP();
2509 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002510 }
2511
Tim Peters8a5c3c72004-04-05 19:36:21 +00002512 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002513 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002514
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002515fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002516 if (tstate->use_tracing) {
Barry Warsawe2eca0b2005-08-15 18:14:19 +00002517 if (tstate->c_tracefunc) {
2518 if (why == WHY_RETURN || why == WHY_YIELD) {
2519 if (call_trace(tstate->c_tracefunc,
2520 tstate->c_traceobj, f,
2521 PyTrace_RETURN, retval)) {
2522 Py_XDECREF(retval);
2523 retval = NULL;
2524 why = WHY_EXCEPTION;
2525 }
2526 }
2527 else if (why == WHY_EXCEPTION) {
2528 call_trace_protected(tstate->c_tracefunc,
2529 tstate->c_traceobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002530 PyTrace_RETURN, NULL);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002531 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002532 }
Fred Drake8f51f542001-10-04 14:48:42 +00002533 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002534 if (why == WHY_EXCEPTION)
2535 call_trace_protected(tstate->c_profilefunc,
2536 tstate->c_profileobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002537 PyTrace_RETURN, NULL);
Fred Drake4ec5d562001-10-04 19:26:43 +00002538 else if (call_trace(tstate->c_profilefunc,
2539 tstate->c_profileobj, f,
2540 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002541 Py_XDECREF(retval);
2542 retval = NULL;
2543 why = WHY_EXCEPTION;
2544 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002545 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002546 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002547
Tim Peters5ca576e2001-06-18 22:08:13 +00002548 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00002549exit_eval_frame:
Armin Rigo2b3eb402003-10-28 12:05:48 +00002550 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002551 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002552
Guido van Rossum96a42c81992-01-12 02:29:51 +00002553 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002554}
2555
Guido van Rossumc2e20742006-02-27 22:32:47 +00002556/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002557 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00002558 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002559
Tim Peters6d6c1a32001-08-02 04:15:00 +00002560PyObject *
2561PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002562 PyObject **args, int argcount, PyObject **kws, int kwcount,
Guido van Rossum4f72a782006-10-27 23:31:49 +00002563 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00002564{
2565 register PyFrameObject *f;
2566 register PyObject *retval = NULL;
2567 register PyObject **fastlocals, **freevars;
2568 PyThreadState *tstate = PyThreadState_GET();
2569 PyObject *x, *u;
2570
2571 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00002572 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002573 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002574 return NULL;
2575 }
2576
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00002577 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00002578 assert(globals != NULL);
2579 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002580 if (f == NULL)
2581 return NULL;
2582
2583 fastlocals = f->f_localsplus;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002584 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00002585
2586 if (co->co_argcount > 0 ||
Guido van Rossum4f72a782006-10-27 23:31:49 +00002587 co->co_kwonlyargcount > 0 ||
Tim Peters5ca576e2001-06-18 22:08:13 +00002588 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2589 int i;
2590 int n = argcount;
2591 PyObject *kwdict = NULL;
2592 if (co->co_flags & CO_VARKEYWORDS) {
2593 kwdict = PyDict_New();
2594 if (kwdict == NULL)
2595 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002596 i = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00002597 if (co->co_flags & CO_VARARGS)
2598 i++;
2599 SETLOCAL(i, kwdict);
2600 }
2601 if (argcount > co->co_argcount) {
2602 if (!(co->co_flags & CO_VARARGS)) {
2603 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00002604 "%U() takes %s %d "
Guido van Rossum4f72a782006-10-27 23:31:49 +00002605 "%spositional argument%s (%d given)",
Walter Dörwald573c08c2007-05-25 15:46:59 +00002606 co->co_name,
Tim Peters5ca576e2001-06-18 22:08:13 +00002607 defcount ? "at most" : "exactly",
2608 co->co_argcount,
2609 kwcount ? "non-keyword " : "",
2610 co->co_argcount == 1 ? "" : "s",
2611 argcount);
2612 goto fail;
2613 }
2614 n = co->co_argcount;
2615 }
2616 for (i = 0; i < n; i++) {
2617 x = args[i];
2618 Py_INCREF(x);
2619 SETLOCAL(i, x);
2620 }
2621 if (co->co_flags & CO_VARARGS) {
2622 u = PyTuple_New(argcount - n);
2623 if (u == NULL)
2624 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002625 SETLOCAL(co->co_argcount + co->co_kwonlyargcount, u);
Tim Peters5ca576e2001-06-18 22:08:13 +00002626 for (i = n; i < argcount; i++) {
2627 x = args[i];
2628 Py_INCREF(x);
2629 PyTuple_SET_ITEM(u, i-n, x);
2630 }
2631 }
2632 for (i = 0; i < kwcount; i++) {
2633 PyObject *keyword = kws[2*i];
2634 PyObject *value = kws[2*i + 1];
2635 int j;
Neal Norwitzda059e32007-08-26 05:33:45 +00002636 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002637 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00002638 "%U() keywords must be strings",
Walter Dörwald573c08c2007-05-25 15:46:59 +00002639 co->co_name);
Tim Peters5ca576e2001-06-18 22:08:13 +00002640 goto fail;
2641 }
2642 /* XXX slow -- speed up using dictionary? */
Guido van Rossum4f72a782006-10-27 23:31:49 +00002643 for (j = 0;
2644 j < co->co_argcount + co->co_kwonlyargcount;
2645 j++) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002646 PyObject *nm = PyTuple_GET_ITEM(
2647 co->co_varnames, j);
2648 int cmp = PyObject_RichCompareBool(
2649 keyword, nm, Py_EQ);
2650 if (cmp > 0)
2651 break;
2652 else if (cmp < 0)
2653 goto fail;
2654 }
2655 /* Check errors from Compare */
2656 if (PyErr_Occurred())
2657 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002658 if (j >= co->co_argcount + co->co_kwonlyargcount) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002659 if (kwdict == NULL) {
2660 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00002661 "%U() got an unexpected "
Walter Dörwald573c08c2007-05-25 15:46:59 +00002662 "keyword argument '%S'",
2663 co->co_name,
2664 keyword);
Tim Peters5ca576e2001-06-18 22:08:13 +00002665 goto fail;
2666 }
2667 PyDict_SetItem(kwdict, keyword, value);
2668 }
2669 else {
2670 if (GETLOCAL(j) != NULL) {
2671 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00002672 "%U() got multiple "
Tim Peters5ca576e2001-06-18 22:08:13 +00002673 "values for keyword "
Walter Dörwald573c08c2007-05-25 15:46:59 +00002674 "argument '%S'",
2675 co->co_name,
2676 keyword);
Tim Peters5ca576e2001-06-18 22:08:13 +00002677 goto fail;
2678 }
2679 Py_INCREF(value);
2680 SETLOCAL(j, value);
2681 }
2682 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002683 if (co->co_kwonlyargcount > 0) {
2684 for (i = co->co_argcount;
2685 i < co->co_argcount + co->co_kwonlyargcount;
2686 i++) {
Guido van Rossum29602e42006-11-22 04:45:33 +00002687 PyObject *name, *def;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002688 if (GETLOCAL(i) != NULL)
2689 continue;
Guido van Rossum29602e42006-11-22 04:45:33 +00002690 name = PyTuple_GET_ITEM(co->co_varnames, i);
2691 def = NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002692 if (kwdefs != NULL)
2693 def = PyDict_GetItem(kwdefs, name);
2694 if (def != NULL) {
2695 Py_INCREF(def);
2696 SETLOCAL(i, def);
2697 continue;
2698 }
2699 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00002700 "%U() needs keyword-only argument %S",
Walter Dörwald573c08c2007-05-25 15:46:59 +00002701 co->co_name, name);
Guido van Rossum4f72a782006-10-27 23:31:49 +00002702 goto fail;
2703 }
2704 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002705 if (argcount < co->co_argcount) {
2706 int m = co->co_argcount - defcount;
2707 for (i = argcount; i < m; i++) {
2708 if (GETLOCAL(i) == NULL) {
2709 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00002710 "%U() takes %s %d "
Guido van Rossum4f72a782006-10-27 23:31:49 +00002711 "%spositional argument%s "
2712 "(%d given)",
Walter Dörwald573c08c2007-05-25 15:46:59 +00002713 co->co_name,
Tim Peters5ca576e2001-06-18 22:08:13 +00002714 ((co->co_flags & CO_VARARGS) ||
2715 defcount) ? "at least"
2716 : "exactly",
2717 m, kwcount ? "non-keyword " : "",
2718 m == 1 ? "" : "s", i);
2719 goto fail;
2720 }
2721 }
2722 if (n > m)
2723 i = n - m;
2724 else
2725 i = 0;
2726 for (; i < defcount; i++) {
2727 if (GETLOCAL(m+i) == NULL) {
2728 PyObject *def = defs[i];
2729 Py_INCREF(def);
2730 SETLOCAL(m+i, def);
2731 }
2732 }
2733 }
2734 }
2735 else {
2736 if (argcount > 0 || kwcount > 0) {
2737 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00002738 "%U() takes no arguments (%d given)",
Walter Dörwald573c08c2007-05-25 15:46:59 +00002739 co->co_name,
Tim Peters5ca576e2001-06-18 22:08:13 +00002740 argcount + kwcount);
2741 goto fail;
2742 }
2743 }
2744 /* Allocate and initialize storage for cell vars, and copy free
2745 vars into frame. This isn't too efficient right now. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002746 if (PyTuple_GET_SIZE(co->co_cellvars)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002747 int i, j, nargs, found;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002748 Py_UNICODE *cellname, *argname;
Tim Peters5ca576e2001-06-18 22:08:13 +00002749 PyObject *c;
2750
Amaury Forgeot d'Arce670bd42007-11-24 00:29:24 +00002751 nargs = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00002752 if (co->co_flags & CO_VARARGS)
2753 nargs++;
2754 if (co->co_flags & CO_VARKEYWORDS)
2755 nargs++;
2756
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002757 /* Initialize each cell var, taking into account
2758 cell vars that are initialized from arguments.
2759
2760 Should arrange for the compiler to put cellvars
2761 that are arguments at the beginning of the cellvars
2762 list so that we can march over it more efficiently?
2763 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002764 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002765 cellname = PyUnicode_AS_UNICODE(
Tim Peters5ca576e2001-06-18 22:08:13 +00002766 PyTuple_GET_ITEM(co->co_cellvars, i));
2767 found = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002768 for (j = 0; j < nargs; j++) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002769 argname = PyUnicode_AS_UNICODE(
Tim Peters5ca576e2001-06-18 22:08:13 +00002770 PyTuple_GET_ITEM(co->co_varnames, j));
Martin v. Löwis5b222132007-06-10 09:51:05 +00002771 if (Py_UNICODE_strcmp(cellname, argname) == 0) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002772 c = PyCell_New(GETLOCAL(j));
2773 if (c == NULL)
2774 goto fail;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002775 GETLOCAL(co->co_nlocals + i) = c;
Tim Peters5ca576e2001-06-18 22:08:13 +00002776 found = 1;
2777 break;
2778 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002779 }
2780 if (found == 0) {
2781 c = PyCell_New(NULL);
2782 if (c == NULL)
2783 goto fail;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002784 SETLOCAL(co->co_nlocals + i, c);
Tim Peters5ca576e2001-06-18 22:08:13 +00002785 }
2786 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002787 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002788 if (PyTuple_GET_SIZE(co->co_freevars)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002789 int i;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002790 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002791 PyObject *o = PyTuple_GET_ITEM(closure, i);
2792 Py_INCREF(o);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002793 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Tim Peters5ca576e2001-06-18 22:08:13 +00002794 }
2795 }
2796
Tim Peters5ca576e2001-06-18 22:08:13 +00002797 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002798 /* Don't need to keep the reference to f_back, it will be set
2799 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002800 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002801 f->f_back = NULL;
2802
Jeremy Hylton985eba52003-02-05 23:13:00 +00002803 PCALL(PCALL_GENERATOR);
2804
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002805 /* Create a new generator that owns the ready to run frame
2806 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00002807 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00002808 }
2809
Thomas Woutersce272b62007-09-19 21:19:28 +00002810 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00002811
Thomas Woutersce272b62007-09-19 21:19:28 +00002812fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00002813
Tim Petersb13680b2001-11-27 23:29:29 +00002814 /* decref'ing the frame can cause __del__ methods to get invoked,
2815 which can call back into Python. While we're done with the
2816 current Python frame (f), the associated C stack is still in use,
2817 so recursion_depth must be boosted for the duration.
2818 */
2819 assert(tstate != NULL);
2820 ++tstate->recursion_depth;
Thomas Woutersce272b62007-09-19 21:19:28 +00002821 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00002822 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002823 return retval;
2824}
2825
2826
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002827/* Logic for the raise statement (too complicated for inlining).
2828 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00002829static enum why_code
Collin Winter828f04a2007-08-31 00:04:24 +00002830do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002831{
Guido van Rossumb4fb6e42008-06-14 20:20:24 +00002832 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00002833
2834 if (exc == NULL) {
Guido van Rossumd295f121998-04-09 21:39:57 +00002835 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00002836 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumb4fb6e42008-06-14 20:20:24 +00002837 PyObject *tb;
Collin Winter828f04a2007-08-31 00:04:24 +00002838 type = tstate->exc_type;
Guido van Rossumd295f121998-04-09 21:39:57 +00002839 value = tstate->exc_value;
2840 tb = tstate->exc_traceback;
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00002841 if (type == Py_None) {
2842 PyErr_SetString(PyExc_RuntimeError,
2843 "No active exception to reraise");
2844 return WHY_EXCEPTION;
2845 }
2846 Py_XINCREF(type);
Guido van Rossumd295f121998-04-09 21:39:57 +00002847 Py_XINCREF(value);
2848 Py_XINCREF(tb);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00002849 PyErr_Restore(type, value, tb);
2850 return WHY_RERAISE;
Guido van Rossumd295f121998-04-09 21:39:57 +00002851 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002852
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002853 /* We support the following forms of raise:
Collin Winter828f04a2007-08-31 00:04:24 +00002854 raise
2855 raise <instance>
2856 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002857
Collin Winter828f04a2007-08-31 00:04:24 +00002858 if (PyExceptionClass_Check(exc)) {
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00002859 type = exc;
2860 value = PyObject_CallObject(exc, NULL);
Collin Winter828f04a2007-08-31 00:04:24 +00002861 if (value == NULL)
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00002862 goto raise_error;
2863 }
Collin Winter828f04a2007-08-31 00:04:24 +00002864 else if (PyExceptionInstance_Check(exc)) {
2865 value = exc;
2866 type = PyExceptionInstance_Class(exc);
Guido van Rossumb209a111997-04-29 18:18:01 +00002867 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002868 }
2869 else {
2870 /* Not something you can raise. You get an exception
2871 anyway, just not what you specified :-) */
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00002872 Py_DECREF(exc);
Guido van Rossum45aecf42006-03-15 04:58:47 +00002873 PyErr_SetString(PyExc_TypeError,
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00002874 "exceptions must derive from BaseException");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002875 goto raise_error;
2876 }
Collin Winter828f04a2007-08-31 00:04:24 +00002877
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00002878 if (cause) {
2879 PyObject *fixed_cause;
2880 if (PyExceptionClass_Check(cause)) {
2881 fixed_cause = PyObject_CallObject(cause, NULL);
2882 if (fixed_cause == NULL)
2883 goto raise_error;
2884 Py_DECREF(cause);
2885 }
2886 else if (PyExceptionInstance_Check(cause)) {
2887 fixed_cause = cause;
2888 }
2889 else {
2890 PyErr_SetString(PyExc_TypeError,
2891 "exception causes must derive from "
2892 "BaseException");
2893 goto raise_error;
2894 }
2895 PyException_SetCause(value, fixed_cause);
2896 }
Collin Winter828f04a2007-08-31 00:04:24 +00002897
Guido van Rossumb4fb6e42008-06-14 20:20:24 +00002898 PyErr_SetObject(type, value);
2899 /* PyErr_SetObject incref's its arguments */
2900 Py_XDECREF(value);
2901 Py_XDECREF(type);
Collin Winter828f04a2007-08-31 00:04:24 +00002902 return WHY_EXCEPTION;
2903
2904raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00002905 Py_XDECREF(value);
2906 Py_XDECREF(type);
Collin Winter1966f1c2007-09-01 20:26:44 +00002907 Py_XDECREF(cause);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002908 return WHY_EXCEPTION;
2909}
2910
Tim Petersd6d010b2001-06-21 02:49:55 +00002911/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00002912 sp). Return 1 for success, 0 if error.
2913
2914 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
2915 with a variable target.
2916*/
Tim Petersd6d010b2001-06-21 02:49:55 +00002917
Barry Warsawe42b18f1997-08-25 22:13:04 +00002918static int
Guido van Rossum0368b722007-05-11 16:50:42 +00002919unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00002920{
Guido van Rossum0368b722007-05-11 16:50:42 +00002921 int i = 0, j = 0;
2922 Py_ssize_t ll = 0;
Tim Petersd6d010b2001-06-21 02:49:55 +00002923 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00002924 PyObject *w;
Guido van Rossum0368b722007-05-11 16:50:42 +00002925 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00002926
Tim Petersd6d010b2001-06-21 02:49:55 +00002927 assert(v != NULL);
2928
2929 it = PyObject_GetIter(v);
2930 if (it == NULL)
2931 goto Error;
2932
2933 for (; i < argcnt; i++) {
2934 w = PyIter_Next(it);
2935 if (w == NULL) {
2936 /* Iterator done, via error or exhaustion. */
2937 if (!PyErr_Occurred()) {
2938 PyErr_Format(PyExc_ValueError,
2939 "need more than %d value%s to unpack",
2940 i, i == 1 ? "" : "s");
2941 }
2942 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00002943 }
2944 *--sp = w;
2945 }
Tim Petersd6d010b2001-06-21 02:49:55 +00002946
Guido van Rossum0368b722007-05-11 16:50:42 +00002947 if (argcntafter == -1) {
2948 /* We better have exhausted the iterator now. */
2949 w = PyIter_Next(it);
2950 if (w == NULL) {
2951 if (PyErr_Occurred())
2952 goto Error;
2953 Py_DECREF(it);
2954 return 1;
2955 }
2956 Py_DECREF(w);
2957 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
2958 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00002959 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002960
2961 l = PySequence_List(it);
2962 if (l == NULL)
2963 goto Error;
2964 *--sp = l;
2965 i++;
2966
2967 ll = PyList_GET_SIZE(l);
2968 if (ll < argcntafter) {
2969 PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack",
2970 argcnt + ll);
2971 goto Error;
2972 }
2973
2974 /* Pop the "after-variable" args off the list. */
2975 for (j = argcntafter; j > 0; j--, i++) {
2976 *--sp = PyList_GET_ITEM(l, ll - j);
2977 }
2978 /* Resize the list. */
Christian Heimes90aa7642007-12-19 02:45:37 +00002979 Py_SIZE(l) = ll - argcntafter;
Guido van Rossum0368b722007-05-11 16:50:42 +00002980 Py_DECREF(it);
2981 return 1;
2982
Tim Petersd6d010b2001-06-21 02:49:55 +00002983Error:
Barry Warsaw91010551997-08-25 22:30:51 +00002984 for (; i > 0; i--, sp++)
2985 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00002986 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00002987 return 0;
2988}
2989
2990
Guido van Rossum96a42c81992-01-12 02:29:51 +00002991#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00002992static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002993prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002994{
Guido van Rossum3f5da241990-12-20 15:06:42 +00002995 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00002996 if (PyObject_Print(v, stdout, 0) != 0)
2997 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00002998 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00002999 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003000}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003001#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003002
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003003static void
Fred Drake5755ce62001-06-27 19:19:46 +00003004call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003005{
Guido van Rossumb209a111997-04-29 18:18:01 +00003006 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003007 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003008 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003009 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003010 value = Py_None;
3011 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003012 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003013 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003014 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003015 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003016 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003017 }
Fred Drake5755ce62001-06-27 19:19:46 +00003018 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003019 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003020 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003021 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003022 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003023 Py_XDECREF(type);
3024 Py_XDECREF(value);
3025 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003026 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003027}
3028
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003029static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003030call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003031 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003032{
3033 PyObject *type, *value, *traceback;
3034 int err;
3035 PyErr_Fetch(&type, &value, &traceback);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003036 err = call_trace(func, obj, frame, what, arg);
Fred Drake4ec5d562001-10-04 19:26:43 +00003037 if (err == 0)
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003038 {
Fred Drake4ec5d562001-10-04 19:26:43 +00003039 PyErr_Restore(type, value, traceback);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003040 return 0;
3041 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003042 else {
3043 Py_XDECREF(type);
3044 Py_XDECREF(value);
3045 Py_XDECREF(traceback);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003046 return -1;
Fred Drake4ec5d562001-10-04 19:26:43 +00003047 }
3048}
3049
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003050static int
Fred Drake5755ce62001-06-27 19:19:46 +00003051call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3052 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003053{
Fred Drake5755ce62001-06-27 19:19:46 +00003054 register PyThreadState *tstate = frame->f_tstate;
3055 int result;
3056 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003057 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003058 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003059 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003060 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003061 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3062 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003063 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003064 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003065}
3066
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003067PyObject *
3068_PyEval_CallTracing(PyObject *func, PyObject *args)
3069{
3070 PyFrameObject *frame = PyEval_GetFrame();
3071 PyThreadState *tstate = frame->f_tstate;
3072 int save_tracing = tstate->tracing;
3073 int save_use_tracing = tstate->use_tracing;
3074 PyObject *result;
3075
3076 tstate->tracing = 0;
3077 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3078 || (tstate->c_profilefunc != NULL));
3079 result = PyObject_Call(func, args, NULL);
3080 tstate->tracing = save_tracing;
3081 tstate->use_tracing = save_use_tracing;
3082 return result;
3083}
3084
Michael W. Hudson006c7522002-11-08 13:08:46 +00003085static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003086maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003087 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3088 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003089{
Michael W. Hudson006c7522002-11-08 13:08:46 +00003090 int result = 0;
3091
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003092 /* If the last instruction executed isn't in the current
3093 instruction window, reset the window. If the last
3094 instruction happens to fall at the start of a line or if it
3095 represents a jump backwards, call the trace function.
3096 */
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003097 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Thomas Woutersce272b62007-09-19 21:19:28 +00003098 int line;
3099 PyAddrPair bounds;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003100
Thomas Woutersce272b62007-09-19 21:19:28 +00003101 line = PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3102 &bounds);
3103 if (line >= 0) {
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003104 frame->f_lineno = line;
Tim Peters8a5c3c72004-04-05 19:36:21 +00003105 result = call_trace(func, obj, frame,
Michael W. Hudson006c7522002-11-08 13:08:46 +00003106 PyTrace_LINE, Py_None);
Thomas Woutersce272b62007-09-19 21:19:28 +00003107 }
3108 *instr_lb = bounds.ap_lower;
3109 *instr_ub = bounds.ap_upper;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003110 }
Armin Rigobf57a142004-03-22 19:24:58 +00003111 else if (frame->f_lasti <= *instr_prev) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003112 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
Armin Rigobf57a142004-03-22 19:24:58 +00003113 }
3114 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003115 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003116}
3117
Fred Drake5755ce62001-06-27 19:19:46 +00003118void
3119PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003120{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003121 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003122 PyObject *temp = tstate->c_profileobj;
3123 Py_XINCREF(arg);
3124 tstate->c_profilefunc = NULL;
3125 tstate->c_profileobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003126 /* Must make sure that tracing is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003127 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003128 Py_XDECREF(temp);
3129 tstate->c_profilefunc = func;
3130 tstate->c_profileobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003131 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003132 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003133}
3134
3135void
3136PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3137{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003138 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003139 PyObject *temp = tstate->c_traceobj;
3140 Py_XINCREF(arg);
3141 tstate->c_tracefunc = NULL;
3142 tstate->c_traceobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003143 /* Must make sure that profiling is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003144 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003145 Py_XDECREF(temp);
3146 tstate->c_tracefunc = func;
3147 tstate->c_traceobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003148 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003149 tstate->use_tracing = ((func != NULL)
3150 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003151}
3152
Guido van Rossumb209a111997-04-29 18:18:01 +00003153PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003154PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003155{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003156 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003157 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003158 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003159 else
3160 return current_frame->f_builtins;
3161}
3162
Guido van Rossumb209a111997-04-29 18:18:01 +00003163PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003164PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003165{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003166 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003167 if (current_frame == NULL)
3168 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003169 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003170 return current_frame->f_locals;
3171}
3172
Guido van Rossumb209a111997-04-29 18:18:01 +00003173PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003174PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003175{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003176 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003177 if (current_frame == NULL)
3178 return NULL;
3179 else
3180 return current_frame->f_globals;
3181}
3182
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003183PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003184PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003185{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003186 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003187 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003188}
3189
Guido van Rossum6135a871995-01-09 17:53:26 +00003190int
Tim Peters5ba58662001-07-16 02:29:45 +00003191PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003192{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003193 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003194 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003195
3196 if (current_frame != NULL) {
3197 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003198 const int compilerflags = codeflags & PyCF_MASK;
3199 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003200 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003201 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003202 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003203#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003204 if (codeflags & CO_GENERATOR_ALLOWED) {
3205 result = 1;
3206 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3207 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003208#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003209 }
3210 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003211}
3212
Guido van Rossum3f5da241990-12-20 15:06:42 +00003213
Guido van Rossum681d79a1995-07-18 14:51:37 +00003214/* External interface to call any callable object.
3215 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003216
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003217#undef PyEval_CallObject
3218/* for backward compatibility: export this interface */
3219
Guido van Rossumb209a111997-04-29 18:18:01 +00003220PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003221PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003222{
Guido van Rossumb209a111997-04-29 18:18:01 +00003223 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003224}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003225#define PyEval_CallObject(func,arg) \
3226 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003227
Guido van Rossumb209a111997-04-29 18:18:01 +00003228PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003229PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003230{
Jeremy Hylton52820442001-01-03 23:52:36 +00003231 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003232
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003233 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003234 arg = PyTuple_New(0);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003235 if (arg == NULL)
3236 return NULL;
3237 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003238 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003239 PyErr_SetString(PyExc_TypeError,
3240 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003241 return NULL;
3242 }
3243 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003244 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003245
Guido van Rossumb209a111997-04-29 18:18:01 +00003246 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003247 PyErr_SetString(PyExc_TypeError,
3248 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003249 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003250 return NULL;
3251 }
3252
Tim Peters6d6c1a32001-08-02 04:15:00 +00003253 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003254 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003255 return result;
3256}
3257
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003258const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003259PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003260{
3261 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003262 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003263 else if (PyFunction_Check(func))
Martin v. Löwis5b222132007-06-10 09:51:05 +00003264 return PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00003265 else if (PyCFunction_Check(func))
3266 return ((PyCFunctionObject*)func)->m_ml->ml_name;
Guido van Rossum50e9fb92006-08-17 05:42:55 +00003267 else
Jeremy Hylton512a2372001-04-11 13:52:29 +00003268 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00003269}
3270
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003271const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003272PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003273{
3274 if (PyMethod_Check(func))
3275 return "()";
3276 else if (PyFunction_Check(func))
3277 return "()";
3278 else if (PyCFunction_Check(func))
3279 return "()";
Guido van Rossum50e9fb92006-08-17 05:42:55 +00003280 else
Jeremy Hylton512a2372001-04-11 13:52:29 +00003281 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00003282}
3283
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003284static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003285err_args(PyObject *func, int flags, int nargs)
3286{
3287 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003288 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003289 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003290 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003291 nargs);
3292 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003293 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003294 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003295 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003296 nargs);
3297}
3298
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003299#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003300if (tstate->use_tracing && tstate->c_profilefunc) { \
3301 if (call_trace(tstate->c_profilefunc, \
3302 tstate->c_profileobj, \
3303 tstate->frame, PyTrace_C_CALL, \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003304 func)) { \
3305 x = NULL; \
3306 } \
3307 else { \
3308 x = call; \
3309 if (tstate->c_profilefunc != NULL) { \
3310 if (x == NULL) { \
3311 call_trace_protected(tstate->c_profilefunc, \
3312 tstate->c_profileobj, \
3313 tstate->frame, PyTrace_C_EXCEPTION, \
3314 func); \
3315 /* XXX should pass (type, value, tb) */ \
3316 } else { \
3317 if (call_trace(tstate->c_profilefunc, \
3318 tstate->c_profileobj, \
3319 tstate->frame, PyTrace_C_RETURN, \
3320 func)) { \
3321 Py_DECREF(x); \
3322 x = NULL; \
3323 } \
3324 } \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003325 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003326 } \
3327} else { \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003328 x = call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003329 }
3330
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003331static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003332call_function(PyObject ***pp_stack, int oparg
3333#ifdef WITH_TSC
3334 , uint64* pintr0, uint64* pintr1
3335#endif
3336 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003337{
3338 int na = oparg & 0xff;
3339 int nk = (oparg>>8) & 0xff;
3340 int n = na + 2 * nk;
3341 PyObject **pfunc = (*pp_stack) - n - 1;
3342 PyObject *func = *pfunc;
3343 PyObject *x, *w;
3344
Jeremy Hylton985eba52003-02-05 23:13:00 +00003345 /* Always dispatch PyCFunction first, because these are
3346 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003347 */
3348 if (PyCFunction_Check(func) && nk == 0) {
3349 int flags = PyCFunction_GET_FLAGS(func);
Nicholas Bastind858a772004-06-25 23:31:06 +00003350 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003351
3352 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003353 if (flags & (METH_NOARGS | METH_O)) {
3354 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3355 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003356 if (flags & METH_NOARGS && na == 0) {
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003357 C_TRACE(x, (*meth)(self,NULL));
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003358 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003359 else if (flags & METH_O && na == 1) {
3360 PyObject *arg = EXT_POP(*pp_stack);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003361 C_TRACE(x, (*meth)(self,arg));
Jeremy Hylton192690e2002-08-16 18:36:11 +00003362 Py_DECREF(arg);
3363 }
3364 else {
3365 err_args(func, flags, na);
3366 x = NULL;
3367 }
3368 }
3369 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003370 PyObject *callargs;
3371 callargs = load_args(pp_stack, na);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003372 READ_TIMESTAMP(*pintr0);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003373 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003374 READ_TIMESTAMP(*pintr1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003375 Py_XDECREF(callargs);
3376 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003377 } else {
3378 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3379 /* optimize access to bound methods */
3380 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003381 PCALL(PCALL_METHOD);
3382 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003383 Py_INCREF(self);
3384 func = PyMethod_GET_FUNCTION(func);
3385 Py_INCREF(func);
3386 Py_DECREF(*pfunc);
3387 *pfunc = self;
3388 na++;
3389 n++;
3390 } else
3391 Py_INCREF(func);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003392 READ_TIMESTAMP(*pintr0);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003393 if (PyFunction_Check(func))
3394 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003395 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003396 x = do_call(func, pp_stack, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003397 READ_TIMESTAMP(*pintr1);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003398 Py_DECREF(func);
3399 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003400
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003401 /* Clear the stack of the function object. Also removes
3402 the arguments in case they weren't consumed already
3403 (fast_function() and err_args() leave them on the stack).
Thomas Wouters7f597322006-03-01 05:32:33 +00003404 */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003405 while ((*pp_stack) > pfunc) {
3406 w = EXT_POP(*pp_stack);
3407 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003408 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003409 }
3410 return x;
3411}
3412
Jeremy Hylton192690e2002-08-16 18:36:11 +00003413/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003414 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003415 For the simplest case -- a function that takes only positional
3416 arguments and is called with only positional arguments -- it
3417 inlines the most primitive frame setup code from
3418 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3419 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003420*/
3421
3422static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003423fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003424{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003425 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003426 PyObject *globals = PyFunction_GET_GLOBALS(func);
3427 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003428 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003429 PyObject **d = NULL;
3430 int nd = 0;
3431
Jeremy Hylton985eba52003-02-05 23:13:00 +00003432 PCALL(PCALL_FUNCTION);
3433 PCALL(PCALL_FAST_FUNCTION);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003434 if (argdefs == NULL && co->co_argcount == n &&
3435 co->co_kwonlyargcount == 0 && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003436 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3437 PyFrameObject *f;
3438 PyObject *retval = NULL;
3439 PyThreadState *tstate = PyThreadState_GET();
3440 PyObject **fastlocals, **stack;
3441 int i;
3442
3443 PCALL(PCALL_FASTER_FUNCTION);
3444 assert(globals != NULL);
3445 /* XXX Perhaps we should create a specialized
3446 PyFrame_New() that doesn't take locals, but does
3447 take builtins without sanity checking them.
3448 */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003449 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003450 f = PyFrame_New(tstate, co, globals, NULL);
3451 if (f == NULL)
3452 return NULL;
3453
3454 fastlocals = f->f_localsplus;
3455 stack = (*pp_stack) - n;
3456
3457 for (i = 0; i < n; i++) {
3458 Py_INCREF(*stack);
3459 fastlocals[i] = *stack++;
3460 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003461 retval = PyEval_EvalFrameEx(f,0);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003462 ++tstate->recursion_depth;
3463 Py_DECREF(f);
3464 --tstate->recursion_depth;
3465 return retval;
3466 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003467 if (argdefs != NULL) {
3468 d = &PyTuple_GET_ITEM(argdefs, 0);
Christian Heimes90aa7642007-12-19 02:45:37 +00003469 nd = Py_SIZE(argdefs);
Jeremy Hylton52820442001-01-03 23:52:36 +00003470 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003471 return PyEval_EvalCodeEx(co, globals,
3472 (PyObject *)NULL, (*pp_stack)-n, na,
Guido van Rossum4f72a782006-10-27 23:31:49 +00003473 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
Jeremy Hylton985eba52003-02-05 23:13:00 +00003474 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003475}
3476
3477static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003478update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3479 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003480{
3481 PyObject *kwdict = NULL;
3482 if (orig_kwdict == NULL)
3483 kwdict = PyDict_New();
3484 else {
3485 kwdict = PyDict_Copy(orig_kwdict);
3486 Py_DECREF(orig_kwdict);
3487 }
3488 if (kwdict == NULL)
3489 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003490 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003491 int err;
3492 PyObject *value = EXT_POP(*pp_stack);
3493 PyObject *key = EXT_POP(*pp_stack);
3494 if (PyDict_GetItem(kwdict, key) != NULL) {
Thomas Woutersce272b62007-09-19 21:19:28 +00003495 PyErr_Format(PyExc_TypeError,
3496 "%.200s%s got multiple values "
3497 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003498 PyEval_GetFuncName(func),
3499 PyEval_GetFuncDesc(func),
Neal Norwitzda059e32007-08-26 05:33:45 +00003500 PyUnicode_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003501 Py_DECREF(key);
3502 Py_DECREF(value);
3503 Py_DECREF(kwdict);
3504 return NULL;
3505 }
3506 err = PyDict_SetItem(kwdict, key, value);
3507 Py_DECREF(key);
3508 Py_DECREF(value);
3509 if (err) {
3510 Py_DECREF(kwdict);
3511 return NULL;
3512 }
3513 }
3514 return kwdict;
3515}
3516
3517static PyObject *
3518update_star_args(int nstack, int nstar, PyObject *stararg,
3519 PyObject ***pp_stack)
3520{
3521 PyObject *callargs, *w;
3522
3523 callargs = PyTuple_New(nstack + nstar);
3524 if (callargs == NULL) {
3525 return NULL;
3526 }
3527 if (nstar) {
3528 int i;
3529 for (i = 0; i < nstar; i++) {
3530 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3531 Py_INCREF(a);
3532 PyTuple_SET_ITEM(callargs, nstack + i, a);
3533 }
3534 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003535 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003536 w = EXT_POP(*pp_stack);
3537 PyTuple_SET_ITEM(callargs, nstack, w);
3538 }
3539 return callargs;
3540}
3541
3542static PyObject *
3543load_args(PyObject ***pp_stack, int na)
3544{
3545 PyObject *args = PyTuple_New(na);
3546 PyObject *w;
3547
3548 if (args == NULL)
3549 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003550 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003551 w = EXT_POP(*pp_stack);
3552 PyTuple_SET_ITEM(args, na, w);
3553 }
3554 return args;
3555}
3556
3557static PyObject *
3558do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3559{
3560 PyObject *callargs = NULL;
3561 PyObject *kwdict = NULL;
3562 PyObject *result = NULL;
3563
3564 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003565 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003566 if (kwdict == NULL)
3567 goto call_fail;
3568 }
3569 callargs = load_args(pp_stack, na);
3570 if (callargs == NULL)
3571 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003572#ifdef CALL_PROFILE
3573 /* At this point, we have to look at the type of func to
3574 update the call stats properly. Do it here so as to avoid
3575 exposing the call stats machinery outside ceval.c
3576 */
3577 if (PyFunction_Check(func))
3578 PCALL(PCALL_FUNCTION);
3579 else if (PyMethod_Check(func))
3580 PCALL(PCALL_METHOD);
3581 else if (PyType_Check(func))
3582 PCALL(PCALL_TYPE);
3583 else
3584 PCALL(PCALL_OTHER);
3585#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003586 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003587call_fail:
Jeremy Hylton52820442001-01-03 23:52:36 +00003588 Py_XDECREF(callargs);
3589 Py_XDECREF(kwdict);
3590 return result;
3591}
3592
3593static PyObject *
3594ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3595{
3596 int nstar = 0;
3597 PyObject *callargs = NULL;
3598 PyObject *stararg = NULL;
3599 PyObject *kwdict = NULL;
3600 PyObject *result = NULL;
3601
3602 if (flags & CALL_FLAG_KW) {
3603 kwdict = EXT_POP(*pp_stack);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003604 if (!PyDict_Check(kwdict)) {
3605 PyObject *d;
3606 d = PyDict_New();
3607 if (d == NULL)
3608 goto ext_call_fail;
3609 if (PyDict_Update(d, kwdict) != 0) {
3610 Py_DECREF(d);
3611 /* PyDict_Update raises attribute
3612 * error (percolated from an attempt
3613 * to get 'keys' attribute) instead of
3614 * a type error if its second argument
3615 * is not a mapping.
3616 */
3617 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
3618 PyErr_Format(PyExc_TypeError,
3619 "%.200s%.200s argument after ** "
3620 "must be a mapping, not %.200s",
3621 PyEval_GetFuncName(func),
3622 PyEval_GetFuncDesc(func),
3623 kwdict->ob_type->tp_name);
3624 }
3625 goto ext_call_fail;
3626 }
3627 Py_DECREF(kwdict);
3628 kwdict = d;
Jeremy Hylton52820442001-01-03 23:52:36 +00003629 }
3630 }
3631 if (flags & CALL_FLAG_VAR) {
3632 stararg = EXT_POP(*pp_stack);
3633 if (!PyTuple_Check(stararg)) {
3634 PyObject *t = NULL;
3635 t = PySequence_Tuple(stararg);
3636 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003637 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3638 PyErr_Format(PyExc_TypeError,
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003639 "%.200s%.200s argument after * "
3640 "must be a sequence, not %200s",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003641 PyEval_GetFuncName(func),
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003642 PyEval_GetFuncDesc(func),
3643 stararg->ob_type->tp_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00003644 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003645 goto ext_call_fail;
3646 }
3647 Py_DECREF(stararg);
3648 stararg = t;
3649 }
3650 nstar = PyTuple_GET_SIZE(stararg);
3651 }
3652 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003653 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003654 if (kwdict == NULL)
3655 goto ext_call_fail;
3656 }
3657 callargs = update_star_args(na, nstar, stararg, pp_stack);
3658 if (callargs == NULL)
3659 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003660#ifdef CALL_PROFILE
3661 /* At this point, we have to look at the type of func to
3662 update the call stats properly. Do it here so as to avoid
3663 exposing the call stats machinery outside ceval.c
3664 */
3665 if (PyFunction_Check(func))
3666 PCALL(PCALL_FUNCTION);
3667 else if (PyMethod_Check(func))
3668 PCALL(PCALL_METHOD);
3669 else if (PyType_Check(func))
3670 PCALL(PCALL_TYPE);
3671 else
3672 PCALL(PCALL_OTHER);
3673#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003674 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00003675ext_call_fail:
Jeremy Hylton52820442001-01-03 23:52:36 +00003676 Py_XDECREF(callargs);
3677 Py_XDECREF(kwdict);
3678 Py_XDECREF(stararg);
3679 return result;
3680}
3681
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003682/* Extract a slice index from a PyInt or PyLong or an object with the
3683 nb_index slot defined, and store in *pi.
3684 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
3685 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 +00003686 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00003687*/
Tim Petersb5196382001-12-16 19:44:20 +00003688/* Note: If v is NULL, return success without storing into *pi. This
3689 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3690 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00003691*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00003692int
Martin v. Löwis18e16552006-02-15 17:27:45 +00003693_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003694{
Tim Petersb5196382001-12-16 19:44:20 +00003695 if (v != NULL) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00003696 Py_ssize_t x;
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +00003697 if (PyIndex_Check(v)) {
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003698 x = PyNumber_AsSsize_t(v, NULL);
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003699 if (x == -1 && PyErr_Occurred())
3700 return 0;
3701 }
3702 else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003703 PyErr_SetString(PyExc_TypeError,
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003704 "slice indices must be integers or "
3705 "None or have an __index__ method");
Guido van Rossum20c6add2000-05-08 14:06:50 +00003706 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003707 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003708 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003709 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00003710 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003711}
3712
Guido van Rossum486364b2007-06-30 05:01:58 +00003713#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Brett Cannon39590462007-02-26 22:01:14 +00003714 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00003715
Guido van Rossumb209a111997-04-29 18:18:01 +00003716static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003717cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003718{
Guido van Rossumac7be682001-01-17 15:42:30 +00003719 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003720 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00003721 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003722 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003723 break;
3724 case PyCmp_IS_NOT:
3725 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003726 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003727 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003728 res = PySequence_Contains(w, v);
3729 if (res < 0)
3730 return NULL;
3731 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003732 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00003733 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003734 if (res < 0)
3735 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003736 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003737 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003738 case PyCmp_EXC_MATCH:
Thomas Wouters9fe394c2007-02-05 01:24:16 +00003739 if (PyTuple_Check(w)) {
3740 Py_ssize_t i, length;
3741 length = PyTuple_Size(w);
3742 for (i = 0; i < length; i += 1) {
3743 PyObject *exc = PyTuple_GET_ITEM(w, i);
Brett Cannon39590462007-02-26 22:01:14 +00003744 if (!PyExceptionClass_Check(exc)) {
3745 PyErr_SetString(PyExc_TypeError,
3746 CANNOT_CATCH_MSG);
Brett Cannonf74225d2007-02-26 21:10:16 +00003747 return NULL;
Thomas Wouters9fe394c2007-02-05 01:24:16 +00003748 }
3749 }
3750 }
3751 else {
Brett Cannon39590462007-02-26 22:01:14 +00003752 if (!PyExceptionClass_Check(w)) {
3753 PyErr_SetString(PyExc_TypeError,
3754 CANNOT_CATCH_MSG);
Brett Cannonf74225d2007-02-26 21:10:16 +00003755 return NULL;
Thomas Wouters9fe394c2007-02-05 01:24:16 +00003756 }
3757 }
Barry Warsaw4249f541997-08-22 21:26:19 +00003758 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003759 break;
3760 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00003761 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003762 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003763 v = res ? Py_True : Py_False;
3764 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003765 return v;
3766}
3767
Thomas Wouters52152252000-08-17 22:55:00 +00003768static PyObject *
3769import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003770{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003771 PyObject *x;
3772
3773 x = PyObject_GetAttr(v, name);
3774 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Walter Dörwald573c08c2007-05-25 15:46:59 +00003775 PyErr_Format(PyExc_ImportError, "cannot import name %S", name);
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003776 }
Thomas Wouters52152252000-08-17 22:55:00 +00003777 return x;
3778}
Guido van Rossumac7be682001-01-17 15:42:30 +00003779
Thomas Wouters52152252000-08-17 22:55:00 +00003780static int
3781import_all_from(PyObject *locals, PyObject *v)
3782{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003783 PyObject *all = PyObject_GetAttrString(v, "__all__");
3784 PyObject *dict, *name, *value;
3785 int skip_leading_underscores = 0;
3786 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00003787
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003788 if (all == NULL) {
3789 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3790 return -1; /* Unexpected error */
3791 PyErr_Clear();
3792 dict = PyObject_GetAttrString(v, "__dict__");
3793 if (dict == NULL) {
3794 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3795 return -1;
3796 PyErr_SetString(PyExc_ImportError,
3797 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00003798 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003799 }
3800 all = PyMapping_Keys(dict);
3801 Py_DECREF(dict);
3802 if (all == NULL)
3803 return -1;
3804 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00003805 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003806
3807 for (pos = 0, err = 0; ; pos++) {
3808 name = PySequence_GetItem(all, pos);
3809 if (name == NULL) {
3810 if (!PyErr_ExceptionMatches(PyExc_IndexError))
3811 err = -1;
3812 else
3813 PyErr_Clear();
3814 break;
3815 }
3816 if (skip_leading_underscores &&
Martin v. Löwis5b222132007-06-10 09:51:05 +00003817 PyUnicode_Check(name) &&
3818 PyUnicode_AS_UNICODE(name)[0] == '_')
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003819 {
3820 Py_DECREF(name);
3821 continue;
3822 }
3823 value = PyObject_GetAttr(v, name);
3824 if (value == NULL)
3825 err = -1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003826 else if (PyDict_CheckExact(locals))
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003827 err = PyDict_SetItem(locals, name, value);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003828 else
3829 err = PyObject_SetItem(locals, name, value);
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003830 Py_DECREF(name);
3831 Py_XDECREF(value);
3832 if (err != 0)
3833 break;
3834 }
3835 Py_DECREF(all);
3836 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00003837}
3838
Guido van Rossumac7be682001-01-17 15:42:30 +00003839static void
Neal Norwitzda059e32007-08-26 05:33:45 +00003840format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00003841{
Neal Norwitzda059e32007-08-26 05:33:45 +00003842 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00003843
3844 if (!obj)
3845 return;
3846
Neal Norwitzda059e32007-08-26 05:33:45 +00003847 obj_str = PyUnicode_AsString(obj);
Paul Prescode68140d2000-08-30 20:25:01 +00003848 if (!obj_str)
3849 return;
3850
3851 PyErr_Format(exc, format_str, obj_str);
3852}
Guido van Rossum950361c1997-01-24 13:49:28 +00003853
Raymond Hettinger52a21b82004-08-06 18:43:09 +00003854static PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +00003855unicode_concatenate(PyObject *v, PyObject *w,
Raymond Hettinger52a21b82004-08-06 18:43:09 +00003856 PyFrameObject *f, unsigned char *next_instr)
3857{
3858 /* This function implements 'variable += expr' when both arguments
Guido van Rossum98297ee2007-11-06 21:34:58 +00003859 are (Unicode) strings. */
3860 Py_ssize_t v_len = PyUnicode_GET_SIZE(v);
3861 Py_ssize_t w_len = PyUnicode_GET_SIZE(w);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003862 Py_ssize_t new_len = v_len + w_len;
3863 if (new_len < 0) {
3864 PyErr_SetString(PyExc_OverflowError,
3865 "strings are too large to concat");
3866 return NULL;
3867 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00003868
Raymond Hettinger52a21b82004-08-06 18:43:09 +00003869 if (v->ob_refcnt == 2) {
3870 /* In the common case, there are 2 references to the value
3871 * stored in 'variable' when the += is performed: one on the
Thomas Wouters8ce81f72007-09-20 18:22:40 +00003872 * value stack (in 'v') and one still stored in the
3873 * 'variable'. We try to delete the variable now to reduce
3874 * the refcnt to 1.
Raymond Hettinger52a21b82004-08-06 18:43:09 +00003875 */
3876 switch (*next_instr) {
3877 case STORE_FAST:
3878 {
3879 int oparg = PEEKARG();
3880 PyObject **fastlocals = f->f_localsplus;
3881 if (GETLOCAL(oparg) == v)
3882 SETLOCAL(oparg, NULL);
3883 break;
3884 }
3885 case STORE_DEREF:
3886 {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00003887 PyObject **freevars = (f->f_localsplus +
3888 f->f_code->co_nlocals);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00003889 PyObject *c = freevars[PEEKARG()];
3890 if (PyCell_GET(c) == v)
3891 PyCell_Set(c, NULL);
3892 break;
3893 }
3894 case STORE_NAME:
3895 {
3896 PyObject *names = f->f_code->co_names;
3897 PyObject *name = GETITEM(names, PEEKARG());
3898 PyObject *locals = f->f_locals;
3899 if (PyDict_CheckExact(locals) &&
3900 PyDict_GetItem(locals, name) == v) {
3901 if (PyDict_DelItem(locals, name) != 0) {
3902 PyErr_Clear();
3903 }
3904 }
3905 break;
3906 }
3907 }
3908 }
3909
Guido van Rossum98297ee2007-11-06 21:34:58 +00003910 if (v->ob_refcnt == 1 && !PyUnicode_CHECK_INTERNED(v)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00003911 /* Now we own the last reference to 'v', so we can resize it
3912 * in-place.
3913 */
Guido van Rossum98297ee2007-11-06 21:34:58 +00003914 if (PyUnicode_Resize(&v, new_len) != 0) {
3915 /* XXX if PyUnicode_Resize() fails, 'v' has been
Thomas Wouters8ce81f72007-09-20 18:22:40 +00003916 * deallocated so it cannot be put back into
3917 * 'variable'. The MemoryError is raised when there
3918 * is no value in 'variable', which might (very
3919 * remotely) be a cause of incompatibilities.
Raymond Hettinger52a21b82004-08-06 18:43:09 +00003920 */
3921 return NULL;
3922 }
3923 /* copy 'w' into the newly allocated area of 'v' */
Guido van Rossum98297ee2007-11-06 21:34:58 +00003924 memcpy(PyUnicode_AS_UNICODE(v) + v_len,
3925 PyUnicode_AS_UNICODE(w), w_len*sizeof(Py_UNICODE));
Raymond Hettinger52a21b82004-08-06 18:43:09 +00003926 return v;
3927 }
3928 else {
3929 /* When in-place resizing is not an option. */
Guido van Rossum98297ee2007-11-06 21:34:58 +00003930 w = PyUnicode_Concat(v, w);
3931 Py_DECREF(v);
3932 return w;
Raymond Hettinger52a21b82004-08-06 18:43:09 +00003933 }
3934}
3935
Guido van Rossum950361c1997-01-24 13:49:28 +00003936#ifdef DYNAMIC_EXECUTION_PROFILE
3937
Skip Montanarof118cb12001-10-15 20:51:38 +00003938static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003939getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00003940{
3941 int i;
3942 PyObject *l = PyList_New(256);
3943 if (l == NULL) return NULL;
3944 for (i = 0; i < 256; i++) {
Christian Heimes217cfd12007-12-02 14:31:20 +00003945 PyObject *x = PyLong_FromLong(a[i]);
Guido van Rossum950361c1997-01-24 13:49:28 +00003946 if (x == NULL) {
3947 Py_DECREF(l);
3948 return NULL;
3949 }
3950 PyList_SetItem(l, i, x);
3951 }
3952 for (i = 0; i < 256; i++)
3953 a[i] = 0;
3954 return l;
3955}
3956
3957PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003958_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00003959{
3960#ifndef DXPAIRS
3961 return getarray(dxp);
3962#else
3963 int i;
3964 PyObject *l = PyList_New(257);
3965 if (l == NULL) return NULL;
3966 for (i = 0; i < 257; i++) {
3967 PyObject *x = getarray(dxpairs[i]);
3968 if (x == NULL) {
3969 Py_DECREF(l);
3970 return NULL;
3971 }
3972 PyList_SetItem(l, i, x);
3973 }
3974 return l;
3975#endif
3976}
3977
3978#endif