blob: f72fe4ae02fdcffc080832da062e395da2290c5c [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003
Guido van Rossum681d79a1995-07-18 14:51:37 +00004/* XXX TO DO:
Guido van Rossum681d79a1995-07-18 14:51:37 +00005 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +00006 XXX document it!
7 */
8
Fredrik Lundh7a830892006-05-27 10:39:48 +00009/* enable more aggressive intra-module optimizations, where available */
Fredrik Lundh57640f52006-05-26 11:54:04 +000010#define PY_LOCAL_AGGRESSIVE
11
Guido van Rossumb209a111997-04-29 18:18:01 +000012#include "Python.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000013
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000014#include "code.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000015#include "frameobject.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000016#include "eval.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000017#include "opcode.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000018#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000019
Guido van Rossumc6004111993-11-05 10:22:19 +000020#include <ctype.h>
21
Tim Peters7df5e7f2006-05-26 23:14:37 +000022#ifndef WITH_TSC
Michael W. Hudson75eabd22005-01-18 15:56:11 +000023
24#define READ_TIMESTAMP(var)
25
26#else
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000027
28typedef unsigned long long uint64;
29
Michael W. Hudson800ba232004-08-12 18:19:17 +000030#if defined(__ppc__) /* <- Don't know if this is the correct symbol; this
Thomas Wouterse2176022007-09-20 17:35:10 +000031 section should work for GCC on any PowerPC
32 platform, irrespective of OS.
33 POWER? Who knows :-) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000034
Michael W. Hudson75eabd22005-01-18 15:56:11 +000035#define READ_TIMESTAMP(var) ppc_getcounter(&var)
Michael W. Hudson800ba232004-08-12 18:19:17 +000036
Fredrik Lundh7a830892006-05-27 10:39:48 +000037static void
Michael W. Hudson800ba232004-08-12 18:19:17 +000038ppc_getcounter(uint64 *v)
39{
40 register unsigned long tbu, tb, tbu2;
41
42 loop:
43 asm volatile ("mftbu %0" : "=r" (tbu) );
44 asm volatile ("mftb %0" : "=r" (tb) );
45 asm volatile ("mftbu %0" : "=r" (tbu2));
46 if (__builtin_expect(tbu != tbu2, 0)) goto loop;
47
Tim Peters7df5e7f2006-05-26 23:14:37 +000048 /* The slightly peculiar way of writing the next lines is
Michael W. Hudson800ba232004-08-12 18:19:17 +000049 compiled better by GCC than any other way I tried. */
50 ((long*)(v))[0] = tbu;
51 ((long*)(v))[1] = tb;
52}
53
Mark Dickinson648568f2009-10-31 10:14:33 +000054#elif defined(__i386__)
55
56/* this is for linux/x86 (and probably any other GCC/x86 combo) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000057
Michael W. Hudson75eabd22005-01-18 15:56:11 +000058#define READ_TIMESTAMP(val) \
59 __asm__ __volatile__("rdtsc" : "=A" (val))
Michael W. Hudson800ba232004-08-12 18:19:17 +000060
Mark Dickinson648568f2009-10-31 10:14:33 +000061#elif defined(__x86_64__)
62
63/* for gcc/x86_64, the "A" constraint in DI mode means *either* rax *or* rdx;
64 not edx:eax as it does for i386. Since rdtsc puts its result in edx:eax
65 even in 64-bit mode, we need to use "a" and "d" for the lower and upper
66 32-bit pieces of the result. */
67
68#define READ_TIMESTAMP(val) \
69 __asm__ __volatile__("rdtsc" : \
70 "=a" (((int*)&(val))[0]), "=d" (((int*)&(val))[1]));
71
72
73#else
74
75#error "Don't know how to implement timestamp counter for this architecture"
76
Michael W. Hudson800ba232004-08-12 18:19:17 +000077#endif
78
Tim Peters7df5e7f2006-05-26 23:14:37 +000079void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000080 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
81{
82 uint64 intr, inst, loop;
83 PyThreadState *tstate = PyThreadState_Get();
84 if (!tstate->interp->tscdump)
85 return;
86 intr = intr1 - intr0;
87 inst = inst1 - inst0 - intr;
88 loop = loop1 - loop0 - intr;
89 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
90 opcode, ticked, inst, loop);
91}
Michael W. Hudson800ba232004-08-12 18:19:17 +000092
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000093#endif
94
Guido van Rossum04691fc1992-08-12 15:35:34 +000095/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000096/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000097
Guido van Rossum408027e1996-12-30 16:17:54 +000098#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000099/* For debugging the interpreter: */
100#define LLTRACE 1 /* Low-level trace feature */
101#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000102#endif
103
Jeremy Hylton52820442001-01-03 23:52:36 +0000104typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +0000105
Guido van Rossum374a9221991-04-04 10:40:29 +0000106/* Forward declarations */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000107#ifdef WITH_TSC
Fredrik Lundh7a830892006-05-27 10:39:48 +0000108static PyObject * call_function(PyObject ***, int, uint64*, uint64*);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000109#else
Fredrik Lundh7a830892006-05-27 10:39:48 +0000110static PyObject * call_function(PyObject ***, int);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000111#endif
Fredrik Lundh7a830892006-05-27 10:39:48 +0000112static PyObject * fast_function(PyObject *, PyObject ***, int, int, int);
113static PyObject * do_call(PyObject *, PyObject ***, int, int);
114static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int);
Thomas Wouterse2176022007-09-20 17:35:10 +0000115static PyObject * update_keyword_args(PyObject *, int, PyObject ***,
116 PyObject *);
Fredrik Lundh7a830892006-05-27 10:39:48 +0000117static PyObject * update_star_args(int, int, PyObject *, PyObject ***);
118static PyObject * load_args(PyObject ***, int);
Jeremy Hylton52820442001-01-03 23:52:36 +0000119#define CALL_FLAG_VAR 1
120#define CALL_FLAG_KW 2
121
Guido van Rossum0a066c01992-03-27 17:29:15 +0000122#ifdef LLTRACE
Fredrik Lundh1b949402006-05-26 12:01:49 +0000123static int lltrace;
Fredrik Lundh7a830892006-05-27 10:39:48 +0000124static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +0000125#endif
Fredrik Lundh7a830892006-05-27 10:39:48 +0000126static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
Fred Drake5755ce62001-06-27 19:19:46 +0000127 int, PyObject *);
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +0000128static int call_trace_protected(Py_tracefunc, PyObject *,
Armin Rigo1c2d7e52005-09-20 18:34:01 +0000129 PyFrameObject *, int, PyObject *);
Fredrik Lundh7a830892006-05-27 10:39:48 +0000130static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
131static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Armin Rigobf57a142004-03-22 19:24:58 +0000132 PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000133
Fredrik Lundh7a830892006-05-27 10:39:48 +0000134static PyObject * apply_slice(PyObject *, PyObject *, PyObject *);
135static int assign_slice(PyObject *, PyObject *,
Tim Petersdbd9ba62000-07-09 03:09:57 +0000136 PyObject *, PyObject *);
Fredrik Lundh7a830892006-05-27 10:39:48 +0000137static PyObject * cmp_outcome(int, PyObject *, PyObject *);
138static PyObject * import_from(PyObject *, PyObject *);
139static int import_all_from(PyObject *, PyObject *);
140static PyObject * build_class(PyObject *, PyObject *, PyObject *);
141static int exec_statement(PyFrameObject *,
Tim Petersdbd9ba62000-07-09 03:09:57 +0000142 PyObject *, PyObject *, PyObject *);
Fredrik Lundh7a830892006-05-27 10:39:48 +0000143static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *);
144static void reset_exc_info(PyThreadState *);
145static void format_exc_check_arg(PyObject *, char *, PyObject *);
146static PyObject * string_concatenate(PyObject *, PyObject *,
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000147 PyFrameObject *, unsigned char *);
Benjamin Peterson004f3dc2010-02-06 19:16:33 +0000148static PyObject * kwd_as_string(PyObject *);
Guido van Rossum374a9221991-04-04 10:40:29 +0000149
Paul Prescode68140d2000-08-30 20:25:01 +0000150#define NAME_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000151 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000152#define GLOBAL_NAME_ERROR_MSG \
153 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000154#define UNBOUNDLOCAL_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000155 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000156#define UNBOUNDFREE_ERROR_MSG \
157 "free variable '%.200s' referenced before assignment" \
158 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000159
Guido van Rossum950361c1997-01-24 13:49:28 +0000160/* Dynamic execution profile */
161#ifdef DYNAMIC_EXECUTION_PROFILE
162#ifdef DXPAIRS
163static long dxpairs[257][256];
164#define dxp dxpairs[256]
165#else
166static long dxp[256];
167#endif
168#endif
169
Jeremy Hylton985eba52003-02-05 23:13:00 +0000170/* Function call profile */
171#ifdef CALL_PROFILE
172#define PCALL_NUM 11
173static int pcall[PCALL_NUM];
174
175#define PCALL_ALL 0
176#define PCALL_FUNCTION 1
177#define PCALL_FAST_FUNCTION 2
178#define PCALL_FASTER_FUNCTION 3
179#define PCALL_METHOD 4
180#define PCALL_BOUND_METHOD 5
181#define PCALL_CFUNCTION 6
182#define PCALL_TYPE 7
183#define PCALL_GENERATOR 8
184#define PCALL_OTHER 9
185#define PCALL_POP 10
186
187/* Notes about the statistics
188
189 PCALL_FAST stats
190
191 FAST_FUNCTION means no argument tuple needs to be created.
192 FASTER_FUNCTION means that the fast-path frame setup code is used.
193
194 If there is a method call where the call can be optimized by changing
195 the argument tuple and calling the function directly, it gets recorded
196 twice.
197
198 As a result, the relationship among the statistics appears to be
199 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
200 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
201 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
202 PCALL_METHOD > PCALL_BOUND_METHOD
203*/
204
205#define PCALL(POS) pcall[POS]++
206
207PyObject *
208PyEval_GetCallStats(PyObject *self)
209{
Andrew M. Kuchling1f3ebe02006-10-27 13:22:46 +0000210 return Py_BuildValue("iiiiiiiiiii",
Jeremy Hylton985eba52003-02-05 23:13:00 +0000211 pcall[0], pcall[1], pcall[2], pcall[3],
212 pcall[4], pcall[5], pcall[6], pcall[7],
Andrew M. Kuchling1f3ebe02006-10-27 13:22:46 +0000213 pcall[8], pcall[9], pcall[10]);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000214}
215#else
216#define PCALL(O)
217
218PyObject *
219PyEval_GetCallStats(PyObject *self)
220{
221 Py_INCREF(Py_None);
222 return Py_None;
223}
224#endif
225
Tim Peters5ca576e2001-06-18 22:08:13 +0000226
Guido van Rossume59214e1994-08-30 08:01:59 +0000227#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000228
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000229#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000230#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000231#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000232#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000233
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000234static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
Guido van Rossuma9672091994-09-14 13:31:22 +0000235static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000236
Tim Peters7f468f22004-10-11 02:40:51 +0000237int
238PyEval_ThreadsInitialized(void)
239{
240 return interpreter_lock != 0;
241}
242
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000243void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000244PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000245{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000246 if (interpreter_lock)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000247 return;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000248 interpreter_lock = PyThread_allocate_lock();
249 PyThread_acquire_lock(interpreter_lock, 1);
250 main_thread = PyThread_get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000251}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000252
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000253void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000254PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000255{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000256 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000257}
258
259void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000260PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000261{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000262 PyThread_release_lock(interpreter_lock);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000263}
264
265void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000266PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000267{
268 if (tstate == NULL)
269 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000270 /* Check someone has called PyEval_InitThreads() to create the lock */
271 assert(interpreter_lock);
Guido van Rossum65d5b571998-12-21 19:32:43 +0000272 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000273 if (PyThreadState_Swap(tstate) != NULL)
274 Py_FatalError(
275 "PyEval_AcquireThread: non-NULL old thread state");
276}
277
278void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000279PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000280{
281 if (tstate == NULL)
282 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
283 if (PyThreadState_Swap(NULL) != tstate)
284 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000285 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000286}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000287
288/* This function is called from PyOS_AfterFork to ensure that newly
289 created child processes don't hold locks referring to threads which
290 are not running in the child process. (This could also be done using
291 pthread_atfork mechanism, at least for the pthreads implementation.) */
292
293void
294PyEval_ReInitThreads(void)
295{
Jesse Noller5e62ca42008-07-16 20:03:47 +0000296 PyObject *threading, *result;
297 PyThreadState *tstate;
298
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000299 if (!interpreter_lock)
300 return;
301 /*XXX Can't use PyThread_free_lock here because it does too
302 much error-checking. Doing this cleanly would require
303 adding a new function to each thread_*.h. Instead, just
304 create a new lock and waste a little bit of memory */
305 interpreter_lock = PyThread_allocate_lock();
306 PyThread_acquire_lock(interpreter_lock, 1);
307 main_thread = PyThread_get_thread_ident();
Jesse Noller5e62ca42008-07-16 20:03:47 +0000308
309 /* Update the threading module with the new state.
310 */
311 tstate = PyThreadState_GET();
312 threading = PyMapping_GetItemString(tstate->interp->modules,
313 "threading");
314 if (threading == NULL) {
315 /* threading not imported */
316 PyErr_Clear();
317 return;
318 }
319 result = PyObject_CallMethod(threading, "_after_fork", NULL);
320 if (result == NULL)
321 PyErr_WriteUnraisable(threading);
322 else
323 Py_DECREF(result);
324 Py_DECREF(threading);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000325}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000326#endif
327
Guido van Rossumff4949e1992-08-05 19:58:53 +0000328/* Functions save_thread and restore_thread are always defined so
329 dynamically loaded modules needn't be compiled separately for use
330 with and without threads: */
331
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000332PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000333PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000334{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000335 PyThreadState *tstate = PyThreadState_Swap(NULL);
336 if (tstate == NULL)
337 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000338#ifdef WITH_THREAD
Guido van Rossumb74eca91997-09-30 22:03:16 +0000339 if (interpreter_lock)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000340 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000341#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000342 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000343}
344
345void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000346PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000347{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000348 if (tstate == NULL)
349 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000350#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000351 if (interpreter_lock) {
Guido van Rossumb74eca91997-09-30 22:03:16 +0000352 int err = errno;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000353 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000354 errno = err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000355 }
356#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000357 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000358}
359
360
Guido van Rossuma9672091994-09-14 13:31:22 +0000361/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
362 signal handlers or Mac I/O completion routines) can schedule calls
363 to a function to be called synchronously.
364 The synchronous function is called with one void* argument.
365 It should return 0 for success or -1 for failure -- failure should
366 be accompanied by an exception.
367
368 If registry succeeds, the registry function returns 0; if it fails
369 (e.g. due to too many pending calls) it returns -1 (without setting
370 an exception condition).
371
372 Note that because registry may occur from within signal handlers,
373 or other asynchronous events, calling malloc() is unsafe!
374
375#ifdef WITH_THREAD
376 Any thread can schedule pending calls, but only the main thread
377 will execute them.
378#endif
379
380 XXX WARNING! ASYNCHRONOUSLY EXECUTING CODE!
381 There are two possible race conditions:
382 (1) nested asynchronous registry calls;
383 (2) registry calls made while pending calls are being processed.
384 While (1) is very unlikely, (2) is a real possibility.
385 The current code is safe against (2), but not against (1).
386 The safety against (2) is derived from the fact that only one
387 thread (the main thread) ever takes things out of the queue.
Guido van Rossuma9672091994-09-14 13:31:22 +0000388
Guido van Rossuma027efa1997-05-05 20:56:21 +0000389 XXX Darn! With the advent of thread state, we should have an array
390 of pending calls per thread in the thread state! Later...
391*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000392
Guido van Rossuma9672091994-09-14 13:31:22 +0000393#define NPENDINGCALLS 32
394static struct {
Thomas Wouters334fb892000-07-25 12:56:38 +0000395 int (*func)(void *);
396 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000397} pendingcalls[NPENDINGCALLS];
398static volatile int pendingfirst = 0;
399static volatile int pendinglast = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000400static volatile int things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000401
402int
Thomas Wouters334fb892000-07-25 12:56:38 +0000403Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000404{
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000405 static volatile int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000406 int i, j;
407 /* XXX Begin critical section */
408 /* XXX If you want this to be safe against nested
409 XXX asynchronous calls, you'll have to work harder! */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000410 if (busy)
411 return -1;
412 busy = 1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000413 i = pendinglast;
414 j = (i + 1) % NPENDINGCALLS;
Guido van Rossum04e70322002-07-17 16:57:13 +0000415 if (j == pendingfirst) {
416 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000417 return -1; /* Queue full */
Guido van Rossum04e70322002-07-17 16:57:13 +0000418 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000419 pendingcalls[i].func = func;
420 pendingcalls[i].arg = arg;
421 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000422
423 _Py_Ticker = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000424 things_to_do = 1; /* Signal main loop */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000425 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000426 /* XXX End critical section */
427 return 0;
428}
429
Guido van Rossum180d7b41994-09-29 09:45:57 +0000430int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000431Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000432{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000433 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000434#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000435 if (main_thread && PyThread_get_thread_ident() != main_thread)
Guido van Rossuma9672091994-09-14 13:31:22 +0000436 return 0;
437#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000438 if (busy)
Guido van Rossum180d7b41994-09-29 09:45:57 +0000439 return 0;
440 busy = 1;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000441 things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000442 for (;;) {
443 int i;
Thomas Wouters334fb892000-07-25 12:56:38 +0000444 int (*func)(void *);
445 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000446 i = pendingfirst;
447 if (i == pendinglast)
448 break; /* Queue empty */
449 func = pendingcalls[i].func;
450 arg = pendingcalls[i].arg;
451 pendingfirst = (i + 1) % NPENDINGCALLS;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000452 if (func(arg) < 0) {
453 busy = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000454 things_to_do = 1; /* We're not done yet */
Guido van Rossuma9672091994-09-14 13:31:22 +0000455 return -1;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000456 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000457 }
Guido van Rossum180d7b41994-09-29 09:45:57 +0000458 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000459 return 0;
460}
461
462
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000463/* The interpreter's recursion limit */
464
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000465#ifndef Py_DEFAULT_RECURSION_LIMIT
466#define Py_DEFAULT_RECURSION_LIMIT 1000
467#endif
468static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
469int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000470
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000471int
472Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000473{
474 return recursion_limit;
475}
476
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000477void
478Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000479{
480 recursion_limit = new_limit;
Thomas Woutersae406c62007-09-19 17:27:43 +0000481 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000482}
483
Armin Rigo2b3eb402003-10-28 12:05:48 +0000484/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
485 if the recursion_depth reaches _Py_CheckRecursionLimit.
486 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
487 to guarantee that _Py_CheckRecursiveCall() is regularly called.
488 Without USE_STACKCHECK, there is no need for this. */
489int
490_Py_CheckRecursiveCall(char *where)
491{
492 PyThreadState *tstate = PyThreadState_GET();
493
494#ifdef USE_STACKCHECK
495 if (PyOS_CheckStack()) {
496 --tstate->recursion_depth;
497 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
498 return -1;
499 }
500#endif
501 if (tstate->recursion_depth > recursion_limit) {
502 --tstate->recursion_depth;
503 PyErr_Format(PyExc_RuntimeError,
504 "maximum recursion depth exceeded%s",
505 where);
506 return -1;
507 }
Thomas Woutersae406c62007-09-19 17:27:43 +0000508 _Py_CheckRecursionLimit = recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000509 return 0;
510}
511
Guido van Rossum374a9221991-04-04 10:40:29 +0000512/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000513enum why_code {
514 WHY_NOT = 0x0001, /* No error */
515 WHY_EXCEPTION = 0x0002, /* Exception occurred */
516 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
517 WHY_RETURN = 0x0008, /* 'return' statement */
518 WHY_BREAK = 0x0010, /* 'break' statement */
519 WHY_CONTINUE = 0x0020, /* 'continue' statement */
520 WHY_YIELD = 0x0040 /* 'yield' operator */
521};
Guido van Rossum374a9221991-04-04 10:40:29 +0000522
Fredrik Lundh7a830892006-05-27 10:39:48 +0000523static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
524static int unpack_iterable(PyObject *, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000525
Jeffrey Yasskind0a70d42008-12-12 21:25:13 +0000526/* Records whether tracing is on for any thread. Counts the number of
527 threads for which tstate->c_tracefunc is non-NULL, so if the value
528 is 0, we know we don't have to check this thread's c_tracefunc.
529 This speeds up the if statement in PyEval_EvalFrameEx() after
530 fast_next_opcode*/
531static int _Py_TracingPossible = 0;
532
Skip Montanarod581d772002-09-03 20:10:45 +0000533/* for manipulating the thread switch and periodic "stuff" - used to be
534 per thread, now just a pair o' globals */
Skip Montanaro99dba272002-09-03 20:19:06 +0000535int _Py_CheckInterval = 100;
536volatile int _Py_Ticker = 100;
Guido van Rossum374a9221991-04-04 10:40:29 +0000537
Guido van Rossumb209a111997-04-29 18:18:01 +0000538PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000539PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000540{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000541 return PyEval_EvalCodeEx(co,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000542 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000543 (PyObject **)NULL, 0,
544 (PyObject **)NULL, 0,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000545 (PyObject **)NULL, 0,
546 NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000547}
548
549
550/* Interpreter main loop */
551
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000552PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000553PyEval_EvalFrame(PyFrameObject *f) {
554 /* This is for backward compatibility with extension modules that
Thomas Wouterse2176022007-09-20 17:35:10 +0000555 used this API; core interpreter code should call
556 PyEval_EvalFrameEx() */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000557 return PyEval_EvalFrameEx(f, 0);
558}
559
560PyObject *
Anthony Baxtera863d332006-04-11 07:43:46 +0000561PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000562{
Guido van Rossum950361c1997-01-24 13:49:28 +0000563#ifdef DXPAIRS
564 int lastopcode = 0;
565#endif
Thomas Wouterse2176022007-09-20 17:35:10 +0000566 register PyObject **stack_pointer; /* Next free slot in value stack */
Guido van Rossum374a9221991-04-04 10:40:29 +0000567 register unsigned char *next_instr;
Armin Rigo8817fcd2004-06-17 10:22:40 +0000568 register int opcode; /* Current opcode */
569 register int oparg; /* Current opcode argument, if any */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000570 register enum why_code why; /* Reason for block stack unwind */
Guido van Rossum374a9221991-04-04 10:40:29 +0000571 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000572 register PyObject *x; /* Result object -- NULL if error */
573 register PyObject *v; /* Temporary objects popped off stack */
574 register PyObject *w;
575 register PyObject *u;
576 register PyObject *t;
Barry Warsaw23c9ec82000-08-21 15:44:01 +0000577 register PyObject *stream = NULL; /* for PRINT opcodes */
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000578 register PyObject **fastlocals, **freevars;
Guido van Rossum014518f1998-11-23 21:09:51 +0000579 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000580 PyThreadState *tstate = PyThreadState_GET();
Tim Peters5ca576e2001-06-18 22:08:13 +0000581 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000582
Tim Peters8a5c3c72004-04-05 19:36:21 +0000583 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000584
585 not (instr_lb <= current_bytecode_offset < instr_ub)
586
Tim Peters8a5c3c72004-04-05 19:36:21 +0000587 is true when the line being executed has changed. The
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000588 initial values are such as to make this false the first
589 time it is tested. */
Armin Rigobf57a142004-03-22 19:24:58 +0000590 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000591
Guido van Rossumd076c731998-10-07 19:42:25 +0000592 unsigned char *first_instr;
Skip Montanaro04d80f82002-08-04 21:03:35 +0000593 PyObject *names;
594 PyObject *consts;
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000595#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000596 /* Make it easier to find out where we are with a debugger */
Tim Peters5ca576e2001-06-18 22:08:13 +0000597 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000598#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000599
Neal Norwitza81d2202002-07-14 00:27:26 +0000600/* Tuple access macros */
601
602#ifndef Py_DEBUG
603#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
604#else
605#define GETITEM(v, i) PyTuple_GetItem((v), (i))
606#endif
607
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000608#ifdef WITH_TSC
609/* Use Pentium timestamp counter to mark certain events:
610 inst0 -- beginning of switch statement for opcode dispatch
611 inst1 -- end of switch statement (may be skipped)
612 loop0 -- the top of the mainloop
Tim Peters7df5e7f2006-05-26 23:14:37 +0000613 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000614 (may be skipped)
615 intr1 -- beginning of long interruption
616 intr2 -- end of long interruption
617
618 Many opcodes call out to helper C functions. In some cases, the
619 time in those functions should be counted towards the time for the
620 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
621 calls another Python function; there's no point in charge all the
622 bytecode executed by the called function to the caller.
623
624 It's hard to make a useful judgement statically. In the presence
625 of operator overloading, it's impossible to tell if a call will
626 execute new Python code or not.
627
628 It's a case-by-case judgement. I'll use intr1 for the following
629 cases:
630
631 EXEC_STMT
632 IMPORT_STAR
633 IMPORT_FROM
634 CALL_FUNCTION (and friends)
635
636 */
637 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
638 int ticked = 0;
639
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000640 READ_TIMESTAMP(inst0);
641 READ_TIMESTAMP(inst1);
642 READ_TIMESTAMP(loop0);
643 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000644
645 /* shut up the compiler */
646 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000647#endif
648
Guido van Rossum374a9221991-04-04 10:40:29 +0000649/* Code access macros */
650
Martin v. Löwis18e16552006-02-15 17:27:45 +0000651#define INSTR_OFFSET() ((int)(next_instr - first_instr))
Guido van Rossum374a9221991-04-04 10:40:29 +0000652#define NEXTOP() (*next_instr++)
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000653#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000654#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
Guido van Rossumd076c731998-10-07 19:42:25 +0000655#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000656#define JUMPBY(x) (next_instr += (x))
657
Raymond Hettingerf606f872003-03-16 03:11:04 +0000658/* OpCode prediction macros
Thomas Wouterse2176022007-09-20 17:35:10 +0000659 Some opcodes tend to come in pairs thus making it possible to
660 predict the second code when the first is run. For example,
661 COMPARE_OP is often followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And,
662 those opcodes are often followed by a POP_TOP.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000663
Raymond Hettingerafae11e2008-07-05 02:11:55 +0000664 Verifying the prediction costs a single high-speed test of a register
Raymond Hettingerac2072922003-03-16 15:41:11 +0000665 variable against a constant. If the pairing was good, then the
Raymond Hettingerafae11e2008-07-05 02:11:55 +0000666 processor's own internal branch predication has a high likelihood of
667 success, resulting in a nearly zero-overhead transition to the
668 next opcode. A successful prediction saves a trip through the eval-loop
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +0000669 including its two unpredictable branches, the HAS_ARG test and the
Raymond Hettingerafae11e2008-07-05 02:11:55 +0000670 switch-case. Combined with the processor's internal branch prediction,
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +0000671 a successful PREDICT has the effect of making the two opcodes run as if
Raymond Hettingerafae11e2008-07-05 02:11:55 +0000672 they were a single new opcode with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000673
Raymond Hettingerafae11e2008-07-05 02:11:55 +0000674 If collecting opcode statistics, your choices are to either keep the
675 predictions turned-on and interpret the results as if some opcodes
676 had been combined or turn-off predictions so that the opcode frequency
677 counter updates for both opcodes.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000678*/
679
Raymond Hettingera7216982004-02-08 19:59:27 +0000680#ifdef DYNAMIC_EXECUTION_PROFILE
681#define PREDICT(op) if (0) goto PRED_##op
682#else
Raymond Hettingerac2072922003-03-16 15:41:11 +0000683#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000684#endif
685
Raymond Hettingerf606f872003-03-16 03:11:04 +0000686#define PREDICTED(op) PRED_##op: next_instr++
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000687#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Raymond Hettingerf606f872003-03-16 03:11:04 +0000688
Guido van Rossum374a9221991-04-04 10:40:29 +0000689/* Stack manipulation macros */
690
Martin v. Löwis18e16552006-02-15 17:27:45 +0000691/* The stack can grow at most MAXINT deep, as co_nlocals and
692 co_stacksize are ints. */
693#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
Guido van Rossum374a9221991-04-04 10:40:29 +0000694#define EMPTY() (STACK_LEVEL() == 0)
695#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000696#define SECOND() (stack_pointer[-2])
697#define THIRD() (stack_pointer[-3])
698#define FOURTH() (stack_pointer[-4])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000699#define SET_TOP(v) (stack_pointer[-1] = (v))
700#define SET_SECOND(v) (stack_pointer[-2] = (v))
701#define SET_THIRD(v) (stack_pointer[-3] = (v))
702#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000703#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000704#define BASIC_PUSH(v) (*stack_pointer++ = (v))
705#define BASIC_POP() (*--stack_pointer)
706
Guido van Rossum96a42c81992-01-12 02:29:51 +0000707#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000708#define PUSH(v) { (void)(BASIC_PUSH(v), \
709 lltrace && prtrace(TOP(), "push")); \
Richard Jonescebbefc2006-05-23 18:28:17 +0000710 assert(STACK_LEVEL() <= co->co_stacksize); }
Thomas Wouterse2176022007-09-20 17:35:10 +0000711#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
712 BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000713#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
714 lltrace && prtrace(TOP(), "stackadj")); \
Richard Jonescebbefc2006-05-23 18:28:17 +0000715 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes52729ac2007-12-14 02:33:57 +0000716#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
717 prtrace((STACK_POINTER)[-1], "ext_pop")), \
718 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000719#else
720#define PUSH(v) BASIC_PUSH(v)
721#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000722#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000723#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000724#endif
725
Guido van Rossum681d79a1995-07-18 14:51:37 +0000726/* Local variable macros */
727
728#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000729
730/* The SETLOCAL() macro must not DECREF the local variable in-place and
731 then store the new value; it must copy the old value to a temporary
732 value, then store the new value, and then DECREF the temporary value.
733 This is because it is possible that during the DECREF the frame is
734 accessed by other code (e.g. a __del__ method or gc.collect()) and the
735 variable would be pointing to already-freed memory. */
736#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
737 GETLOCAL(i) = value; \
738 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000739
Guido van Rossuma027efa1997-05-05 20:56:21 +0000740/* Start of code */
741
Tim Peters5ca576e2001-06-18 22:08:13 +0000742 if (f == NULL)
743 return NULL;
744
Armin Rigo1d313ab2003-10-25 14:33:09 +0000745 /* push frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000746 if (Py_EnterRecursiveCall(""))
Armin Rigo1d313ab2003-10-25 14:33:09 +0000747 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000748
Tim Peters5ca576e2001-06-18 22:08:13 +0000749 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000750
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000751 if (tstate->use_tracing) {
752 if (tstate->c_tracefunc != NULL) {
753 /* tstate->c_tracefunc, if defined, is a
754 function that will be called on *every* entry
755 to a code block. Its return value, if not
756 None, is a function that will be called at
757 the start of each executed line of code.
758 (Actually, the function must return itself
759 in order to continue tracing.) The trace
760 functions are called with three arguments:
761 a pointer to the current frame, a string
762 indicating why the function is called, and
763 an argument which depends on the situation.
764 The global trace function is also called
765 whenever an exception is detected. */
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +0000766 if (call_trace_protected(tstate->c_tracefunc,
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +0000767 tstate->c_traceobj,
768 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000769 /* Trace function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000770 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000771 }
772 }
773 if (tstate->c_profilefunc != NULL) {
774 /* Similar for c_profilefunc, except it needn't
775 return itself and isn't called for "line" events */
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +0000776 if (call_trace_protected(tstate->c_profilefunc,
777 tstate->c_profileobj,
778 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000779 /* Profile function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000780 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000781 }
782 }
783 }
784
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000785 co = f->f_code;
786 names = co->co_names;
787 consts = co->co_consts;
788 fastlocals = f->f_localsplus;
Richard Jonescebbefc2006-05-23 18:28:17 +0000789 freevars = f->f_localsplus + co->co_nlocals;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000790 first_instr = (unsigned char*) PyString_AS_STRING(co->co_code);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000791 /* An explanation is in order for the next line.
792
793 f->f_lasti now refers to the index of the last instruction
794 executed. You might think this was obvious from the name, but
795 this wasn't always true before 2.3! PyFrame_New now sets
796 f->f_lasti to -1 (i.e. the index *before* the first instruction)
797 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +0000798 does work. Promise.
Raymond Hettinger4bd97d42007-01-06 01:14:41 +0000799
800 When the PREDICT() macros are enabled, some opcode pairs follow in
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +0000801 direct succession without updating f->f_lasti. A successful
Raymond Hettinger4bd97d42007-01-06 01:14:41 +0000802 prediction effectively links the two codes together as if they
803 were a single new opcode; accordingly,f->f_lasti will point to
804 the first code in the pair (for instance, GET_ITER followed by
805 FOR_ITER is effectively a single opcode and f->f_lasti will point
806 at to the beginning of the combined pair.)
807 */
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000808 next_instr = first_instr + f->f_lasti + 1;
809 stack_pointer = f->f_stacktop;
810 assert(stack_pointer != NULL);
811 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
812
Tim Peters5ca576e2001-06-18 22:08:13 +0000813#ifdef LLTRACE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000814 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +0000815#endif
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000816#if defined(Py_DEBUG) || defined(LLTRACE)
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000817 filename = PyString_AsString(co->co_filename);
Tim Peters5ca576e2001-06-18 22:08:13 +0000818#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000819
Guido van Rossum374a9221991-04-04 10:40:29 +0000820 why = WHY_NOT;
821 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +0000822 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +0000823 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +0000824
Anthony Baxtera863d332006-04-11 07:43:46 +0000825 if (throwflag) { /* support for generator.throw() */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000826 why = WHY_EXCEPTION;
827 goto on_error;
828 }
Tim Peters7df5e7f2006-05-26 23:14:37 +0000829
Guido van Rossum374a9221991-04-04 10:40:29 +0000830 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000831#ifdef WITH_TSC
832 if (inst1 == 0) {
833 /* Almost surely, the opcode executed a break
834 or a continue, preventing inst1 from being set
835 on the way out of the loop.
836 */
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000837 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000838 loop1 = inst1;
839 }
840 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
841 intr0, intr1);
842 ticked = 0;
843 inst1 = 0;
844 intr0 = 0;
845 intr1 = 0;
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000846 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000847#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000848 assert(stack_pointer >= f->f_valuestack); /* else underflow */
Richard Jonescebbefc2006-05-23 18:28:17 +0000849 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000850
Guido van Rossuma027efa1997-05-05 20:56:21 +0000851 /* Do periodic things. Doing this every time through
852 the loop would add too much overhead, so we do it
853 only every Nth instruction. We also do it if
854 ``things_to_do'' is set, i.e. when an asynchronous
855 event needs attention (e.g. a signal handler or
856 async I/O handler); see Py_AddPendingCall() and
857 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000858
Skip Montanarod581d772002-09-03 20:10:45 +0000859 if (--_Py_Ticker < 0) {
Thomas Woutersae406c62007-09-19 17:27:43 +0000860 if (*next_instr == SETUP_FINALLY) {
861 /* Make the last opcode before
862 a try: finally: block uninterruptable. */
863 goto fast_next_opcode;
864 }
Skip Montanarod581d772002-09-03 20:10:45 +0000865 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000866 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000867#ifdef WITH_TSC
868 ticked = 1;
869#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000870 if (things_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +0000871 if (Py_MakePendingCalls() < 0) {
872 why = WHY_EXCEPTION;
873 goto on_error;
874 }
Kurt B. Kaiser4c79a832004-11-23 18:06:08 +0000875 if (things_to_do)
876 /* MakePendingCalls() didn't succeed.
877 Force early re-execution of this
878 "periodic" code, possibly after
879 a thread switch */
880 _Py_Ticker = 0;
Guido van Rossum8861b741996-07-30 16:49:37 +0000881 }
Guido van Rossume59214e1994-08-30 08:01:59 +0000882#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000883 if (interpreter_lock) {
884 /* Give another thread a chance */
885
Guido van Rossum25ce5661997-08-02 03:10:38 +0000886 if (PyThreadState_Swap(NULL) != tstate)
887 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000888 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000889
890 /* Other threads may run now */
891
Guido van Rossum65d5b571998-12-21 19:32:43 +0000892 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000893 if (PyThreadState_Swap(tstate) != NULL)
894 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000895
896 /* Check for thread interrupts */
897
898 if (tstate->async_exc != NULL) {
899 x = tstate->async_exc;
900 tstate->async_exc = NULL;
901 PyErr_SetNone(x);
902 Py_DECREF(x);
903 why = WHY_EXCEPTION;
904 goto on_error;
905 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000906 }
907#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000908 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000909
Neil Schemenauer63543862002-02-17 19:10:14 +0000910 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +0000911 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +0000912
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000913 /* line-by-line tracing support */
914
Jeffrey Yasskind0a70d42008-12-12 21:25:13 +0000915 if (_Py_TracingPossible &&
916 tstate->c_tracefunc != NULL && !tstate->tracing) {
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000917 /* see maybe_call_line_trace
918 for expository comments */
919 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +0000920
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000921 err = maybe_call_line_trace(tstate->c_tracefunc,
922 tstate->c_traceobj,
Armin Rigobf57a142004-03-22 19:24:58 +0000923 f, &instr_lb, &instr_ub,
924 &instr_prev);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000925 /* Reload possibly changed frame fields */
926 JUMPTO(f->f_lasti);
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000927 if (f->f_stacktop != NULL) {
928 stack_pointer = f->f_stacktop;
929 f->f_stacktop = NULL;
930 }
931 if (err) {
932 /* trace function raised an exception */
933 goto on_error;
934 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000935 }
936
937 /* Extract opcode and argument */
938
Guido van Rossum374a9221991-04-04 10:40:29 +0000939 opcode = NEXTOP();
Armin Rigo8817fcd2004-06-17 10:22:40 +0000940 oparg = 0; /* allows oparg to be stored in a register because
941 it doesn't have to be remembered across a full loop */
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000942 if (HAS_ARG(opcode))
943 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +0000944 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +0000945#ifdef DYNAMIC_EXECUTION_PROFILE
946#ifdef DXPAIRS
947 dxpairs[lastopcode][opcode]++;
948 lastopcode = opcode;
949#endif
950 dxp[opcode]++;
951#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000952
Guido van Rossum96a42c81992-01-12 02:29:51 +0000953#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +0000954 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +0000955
Guido van Rossum96a42c81992-01-12 02:29:51 +0000956 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000957 if (HAS_ARG(opcode)) {
958 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000959 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +0000960 }
961 else {
962 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000963 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +0000964 }
965 }
966#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000967
Guido van Rossum374a9221991-04-04 10:40:29 +0000968 /* Main switch on opcode */
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000969 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +0000970
Guido van Rossum374a9221991-04-04 10:40:29 +0000971 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +0000972
Guido van Rossum374a9221991-04-04 10:40:29 +0000973 /* BEWARE!
974 It is essential that any operation that fails sets either
975 x to NULL, err to nonzero, or why to anything but WHY_NOT,
976 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000977
Guido van Rossum374a9221991-04-04 10:40:29 +0000978 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000979
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000980 case NOP:
981 goto fast_next_opcode;
982
Neil Schemenauer63543862002-02-17 19:10:14 +0000983 case LOAD_FAST:
984 x = GETLOCAL(oparg);
985 if (x != NULL) {
986 Py_INCREF(x);
987 PUSH(x);
988 goto fast_next_opcode;
989 }
990 format_exc_check_arg(PyExc_UnboundLocalError,
991 UNBOUNDLOCAL_ERROR_MSG,
992 PyTuple_GetItem(co->co_varnames, oparg));
993 break;
994
995 case LOAD_CONST:
Skip Montanaro04d80f82002-08-04 21:03:35 +0000996 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +0000997 Py_INCREF(x);
998 PUSH(x);
999 goto fast_next_opcode;
1000
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001001 PREDICTED_WITH_ARG(STORE_FAST);
Neil Schemenauer63543862002-02-17 19:10:14 +00001002 case STORE_FAST:
1003 v = POP();
1004 SETLOCAL(oparg, v);
1005 goto fast_next_opcode;
1006
Raymond Hettingerf606f872003-03-16 03:11:04 +00001007 PREDICTED(POP_TOP);
Guido van Rossum374a9221991-04-04 10:40:29 +00001008 case POP_TOP:
1009 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001010 Py_DECREF(v);
Neil Schemenauer63543862002-02-17 19:10:14 +00001011 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001012
Guido van Rossum374a9221991-04-04 10:40:29 +00001013 case ROT_TWO:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001014 v = TOP();
1015 w = SECOND();
1016 SET_TOP(w);
1017 SET_SECOND(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +00001018 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001019
Guido van Rossum374a9221991-04-04 10:40:29 +00001020 case ROT_THREE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001021 v = TOP();
1022 w = SECOND();
1023 x = THIRD();
1024 SET_TOP(w);
1025 SET_SECOND(x);
1026 SET_THIRD(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +00001027 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001028
Thomas Wouters434d0822000-08-24 20:11:32 +00001029 case ROT_FOUR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001030 u = TOP();
1031 v = SECOND();
1032 w = THIRD();
1033 x = FOURTH();
1034 SET_TOP(v);
1035 SET_SECOND(w);
1036 SET_THIRD(x);
1037 SET_FOURTH(u);
Raymond Hettinger080cb322003-03-14 01:37:42 +00001038 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001039
Guido van Rossum374a9221991-04-04 10:40:29 +00001040 case DUP_TOP:
1041 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001042 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001043 PUSH(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +00001044 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001045
Thomas Wouters434d0822000-08-24 20:11:32 +00001046 case DUP_TOPX:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001047 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001048 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +00001049 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001050 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +00001051 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001052 STACKADJ(2);
1053 SET_TOP(x);
1054 SET_SECOND(w);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001055 goto fast_next_opcode;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001056 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001057 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +00001058 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001059 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +00001060 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001061 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +00001062 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001063 STACKADJ(3);
1064 SET_TOP(x);
1065 SET_SECOND(w);
1066 SET_THIRD(v);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001067 goto fast_next_opcode;
Thomas Wouters434d0822000-08-24 20:11:32 +00001068 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001069 Py_FatalError("invalid argument to DUP_TOPX"
1070 " (bytecode corruption?)");
Jeffrey Yasskin69614982008-12-11 05:21:18 +00001071 /* Never returns, so don't bother to set why. */
Tim Peters35ba6892000-10-11 07:04:49 +00001072 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001073
Guido van Rossum374a9221991-04-04 10:40:29 +00001074 case UNARY_POSITIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001075 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001076 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001077 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001078 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001079 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001080 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001081
Guido van Rossum374a9221991-04-04 10:40:29 +00001082 case UNARY_NEGATIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001083 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001084 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001085 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001086 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001087 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001088 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001089
Guido van Rossum374a9221991-04-04 10:40:29 +00001090 case UNARY_NOT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001091 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001092 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001093 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +00001094 if (err == 0) {
1095 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001096 SET_TOP(Py_True);
Guido van Rossumfc490731997-05-06 15:06:49 +00001097 continue;
1098 }
1099 else if (err > 0) {
1100 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001101 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +00001102 err = 0;
1103 continue;
1104 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00001105 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001106 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001107
Guido van Rossum374a9221991-04-04 10:40:29 +00001108 case UNARY_CONVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001109 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001110 x = PyObject_Repr(v);
1111 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001112 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001113 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001114 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001115
Guido van Rossum7928cd71991-10-24 14:59:31 +00001116 case UNARY_INVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001117 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001118 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001119 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001120 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001121 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001122 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001123
Guido van Rossum50564e81996-01-12 01:13:16 +00001124 case BINARY_POWER:
1125 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001126 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001127 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +00001128 Py_DECREF(v);
1129 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001130 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001131 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +00001132 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001133
Guido van Rossum374a9221991-04-04 10:40:29 +00001134 case BINARY_MULTIPLY:
1135 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001136 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001137 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001138 Py_DECREF(v);
1139 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001140 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001141 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001142 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001143
Guido van Rossum374a9221991-04-04 10:40:29 +00001144 case BINARY_DIVIDE:
Tim Peters3caca232001-12-06 06:23:26 +00001145 if (!_Py_QnewFlag) {
1146 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001147 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001148 x = PyNumber_Divide(v, w);
1149 Py_DECREF(v);
1150 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001151 SET_TOP(x);
Tim Peters3caca232001-12-06 06:23:26 +00001152 if (x != NULL) continue;
1153 break;
1154 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001155 /* -Qnew is in effect: fall through to
Tim Peters3caca232001-12-06 06:23:26 +00001156 BINARY_TRUE_DIVIDE */
1157 case BINARY_TRUE_DIVIDE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001158 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001159 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001160 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001161 Py_DECREF(v);
1162 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001163 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001164 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001165 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001166
Guido van Rossum4668b002001-08-08 05:00:18 +00001167 case BINARY_FLOOR_DIVIDE:
1168 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001169 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001170 x = PyNumber_FloorDivide(v, w);
1171 Py_DECREF(v);
1172 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001173 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001174 if (x != NULL) continue;
1175 break;
1176
Guido van Rossum374a9221991-04-04 10:40:29 +00001177 case BINARY_MODULO:
1178 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001179 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001180 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001181 Py_DECREF(v);
1182 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001183 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001184 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001185 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001186
Guido van Rossum374a9221991-04-04 10:40:29 +00001187 case BINARY_ADD:
1188 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001189 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001190 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001191 /* INLINE: int + int */
1192 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001193 a = PyInt_AS_LONG(v);
1194 b = PyInt_AS_LONG(w);
Mark Dickinson6df863c92009-12-02 17:36:34 +00001195 /* cast to avoid undefined behaviour
1196 on overflow */
1197 i = (long)((unsigned long)a + b);
Guido van Rossum87780df2001-08-23 02:58:07 +00001198 if ((i^a) < 0 && (i^b) < 0)
1199 goto slow_add;
1200 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001201 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001202 else if (PyString_CheckExact(v) &&
1203 PyString_CheckExact(w)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001204 x = string_concatenate(v, w, f, next_instr);
1205 /* string_concatenate consumed the ref to v */
1206 goto skip_decref_vx;
1207 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001208 else {
1209 slow_add:
Guido van Rossumc12da691997-07-17 23:12:42 +00001210 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001211 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001212 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001213 skip_decref_vx:
Guido van Rossumb209a111997-04-29 18:18:01 +00001214 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001215 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001216 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001217 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001218
Guido van Rossum374a9221991-04-04 10:40:29 +00001219 case BINARY_SUBTRACT:
1220 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001221 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001222 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001223 /* INLINE: int - int */
1224 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001225 a = PyInt_AS_LONG(v);
1226 b = PyInt_AS_LONG(w);
Mark Dickinson6df863c92009-12-02 17:36:34 +00001227 /* cast to avoid undefined behaviour
1228 on overflow */
1229 i = (long)((unsigned long)a - b);
Guido van Rossum87780df2001-08-23 02:58:07 +00001230 if ((i^a) < 0 && (i^~b) < 0)
1231 goto slow_sub;
1232 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001233 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001234 else {
1235 slow_sub:
Guido van Rossumc12da691997-07-17 23:12:42 +00001236 x = PyNumber_Subtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001237 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001238 Py_DECREF(v);
1239 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001240 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001241 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001242 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001243
Guido van Rossum374a9221991-04-04 10:40:29 +00001244 case BINARY_SUBSCR:
1245 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001246 v = TOP();
Tim Petersb1c46982001-10-05 20:41:38 +00001247 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001248 /* INLINE: list[int] */
Neal Norwitz814e9382006-03-02 07:54:28 +00001249 Py_ssize_t i = PyInt_AsSsize_t(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001250 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +00001251 i += PyList_GET_SIZE(v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001252 if (i >= 0 && i < PyList_GET_SIZE(v)) {
Guido van Rossumfa00e951998-07-08 15:02:37 +00001253 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001254 Py_INCREF(x);
1255 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001256 else
1257 goto slow_get;
Guido van Rossumc12da691997-07-17 23:12:42 +00001258 }
1259 else
Raymond Hettinger467a6982004-04-07 11:39:21 +00001260 slow_get:
Guido van Rossumc12da691997-07-17 23:12:42 +00001261 x = PyObject_GetItem(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 Rossum374a9221991-04-04 10:40:29 +00001266 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001267
Guido van Rossum7928cd71991-10-24 14:59:31 +00001268 case BINARY_LSHIFT:
1269 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001270 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001271 x = PyNumber_Lshift(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;
Guido van Rossumac7be682001-01-17 15:42:30 +00001277
Guido van Rossum7928cd71991-10-24 14:59:31 +00001278 case BINARY_RSHIFT:
1279 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001280 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001281 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001282 Py_DECREF(v);
1283 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001284 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001285 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001286 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001287
Guido van Rossum7928cd71991-10-24 14:59:31 +00001288 case BINARY_AND:
1289 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001290 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001291 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001292 Py_DECREF(v);
1293 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001294 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001295 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001296 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001297
Guido van Rossum7928cd71991-10-24 14:59:31 +00001298 case BINARY_XOR:
1299 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001300 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001301 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001302 Py_DECREF(v);
1303 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001304 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001305 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001306 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001307
Guido van Rossum7928cd71991-10-24 14:59:31 +00001308 case BINARY_OR:
1309 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001310 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001311 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001312 Py_DECREF(v);
1313 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001314 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001315 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001316 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001317
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001318 case LIST_APPEND:
1319 w = POP();
1320 v = POP();
1321 err = PyList_Append(v, w);
1322 Py_DECREF(v);
1323 Py_DECREF(w);
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001324 if (err == 0) {
1325 PREDICT(JUMP_ABSOLUTE);
1326 continue;
1327 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001328 break;
1329
Thomas Wouters434d0822000-08-24 20:11:32 +00001330 case INPLACE_POWER:
1331 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001332 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001333 x = PyNumber_InPlacePower(v, w, Py_None);
1334 Py_DECREF(v);
1335 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001336 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001337 if (x != NULL) continue;
1338 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001339
Thomas Wouters434d0822000-08-24 20:11:32 +00001340 case INPLACE_MULTIPLY:
1341 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001342 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001343 x = PyNumber_InPlaceMultiply(v, w);
1344 Py_DECREF(v);
1345 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001346 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001347 if (x != NULL) continue;
1348 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001349
Thomas Wouters434d0822000-08-24 20:11:32 +00001350 case INPLACE_DIVIDE:
Tim Peters54b11912001-12-25 18:49:11 +00001351 if (!_Py_QnewFlag) {
1352 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001353 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001354 x = PyNumber_InPlaceDivide(v, w);
1355 Py_DECREF(v);
1356 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001357 SET_TOP(x);
Tim Peters54b11912001-12-25 18:49:11 +00001358 if (x != NULL) continue;
1359 break;
1360 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001361 /* -Qnew is in effect: fall through to
Tim Peters54b11912001-12-25 18:49:11 +00001362 INPLACE_TRUE_DIVIDE */
1363 case INPLACE_TRUE_DIVIDE:
Thomas Wouters434d0822000-08-24 20:11:32 +00001364 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001365 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001366 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001367 Py_DECREF(v);
1368 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001369 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001370 if (x != NULL) continue;
1371 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001372
Guido van Rossum4668b002001-08-08 05:00:18 +00001373 case INPLACE_FLOOR_DIVIDE:
1374 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001375 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001376 x = PyNumber_InPlaceFloorDivide(v, w);
1377 Py_DECREF(v);
1378 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001379 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001380 if (x != NULL) continue;
1381 break;
1382
Thomas Wouters434d0822000-08-24 20:11:32 +00001383 case INPLACE_MODULO:
1384 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001385 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001386 x = PyNumber_InPlaceRemainder(v, w);
1387 Py_DECREF(v);
1388 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001389 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001390 if (x != NULL) continue;
1391 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001392
Thomas Wouters434d0822000-08-24 20:11:32 +00001393 case INPLACE_ADD:
1394 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001395 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001396 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001397 /* INLINE: int + int */
1398 register long a, b, i;
1399 a = PyInt_AS_LONG(v);
1400 b = PyInt_AS_LONG(w);
1401 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001402 if ((i^a) < 0 && (i^b) < 0)
1403 goto slow_iadd;
1404 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001405 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001406 else if (PyString_CheckExact(v) &&
1407 PyString_CheckExact(w)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001408 x = string_concatenate(v, w, f, next_instr);
1409 /* string_concatenate consumed the ref to v */
1410 goto skip_decref_v;
1411 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001412 else {
1413 slow_iadd:
Thomas Wouters434d0822000-08-24 20:11:32 +00001414 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001415 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001416 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001417 skip_decref_v:
Thomas Wouters434d0822000-08-24 20:11:32 +00001418 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001419 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001420 if (x != NULL) continue;
1421 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001422
Thomas Wouters434d0822000-08-24 20:11:32 +00001423 case INPLACE_SUBTRACT:
1424 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001425 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001426 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001427 /* INLINE: int - int */
1428 register long a, b, i;
1429 a = PyInt_AS_LONG(v);
1430 b = PyInt_AS_LONG(w);
1431 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001432 if ((i^a) < 0 && (i^~b) < 0)
1433 goto slow_isub;
1434 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001435 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001436 else {
1437 slow_isub:
Thomas Wouters434d0822000-08-24 20:11:32 +00001438 x = PyNumber_InPlaceSubtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001439 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001440 Py_DECREF(v);
1441 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001442 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001443 if (x != NULL) continue;
1444 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001445
Thomas Wouters434d0822000-08-24 20:11:32 +00001446 case INPLACE_LSHIFT:
1447 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001448 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001449 x = PyNumber_InPlaceLshift(v, w);
1450 Py_DECREF(v);
1451 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001452 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001453 if (x != NULL) continue;
1454 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001455
Thomas Wouters434d0822000-08-24 20:11:32 +00001456 case INPLACE_RSHIFT:
1457 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001458 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001459 x = PyNumber_InPlaceRshift(v, w);
1460 Py_DECREF(v);
1461 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001462 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001463 if (x != NULL) continue;
1464 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001465
Thomas Wouters434d0822000-08-24 20:11:32 +00001466 case INPLACE_AND:
1467 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001468 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001469 x = PyNumber_InPlaceAnd(v, w);
1470 Py_DECREF(v);
1471 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001472 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001473 if (x != NULL) continue;
1474 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001475
Thomas Wouters434d0822000-08-24 20:11:32 +00001476 case INPLACE_XOR:
1477 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001478 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001479 x = PyNumber_InPlaceXor(v, w);
1480 Py_DECREF(v);
1481 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001482 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001483 if (x != NULL) continue;
1484 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001485
Thomas Wouters434d0822000-08-24 20:11:32 +00001486 case INPLACE_OR:
1487 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001488 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001489 x = PyNumber_InPlaceOr(v, w);
1490 Py_DECREF(v);
1491 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001492 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001493 if (x != NULL) continue;
1494 break;
1495
Guido van Rossum374a9221991-04-04 10:40:29 +00001496 case SLICE+0:
1497 case SLICE+1:
1498 case SLICE+2:
1499 case SLICE+3:
1500 if ((opcode-SLICE) & 2)
1501 w = POP();
1502 else
1503 w = NULL;
1504 if ((opcode-SLICE) & 1)
1505 v = POP();
1506 else
1507 v = NULL;
Raymond Hettinger663004b2003-01-09 15:24:30 +00001508 u = TOP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001509 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001510 Py_DECREF(u);
1511 Py_XDECREF(v);
1512 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001513 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001514 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001515 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001516
Guido van Rossum374a9221991-04-04 10:40:29 +00001517 case STORE_SLICE+0:
1518 case STORE_SLICE+1:
1519 case STORE_SLICE+2:
1520 case STORE_SLICE+3:
1521 if ((opcode-STORE_SLICE) & 2)
1522 w = POP();
1523 else
1524 w = NULL;
1525 if ((opcode-STORE_SLICE) & 1)
1526 v = POP();
1527 else
1528 v = NULL;
1529 u = POP();
1530 t = POP();
1531 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +00001532 Py_DECREF(t);
1533 Py_DECREF(u);
1534 Py_XDECREF(v);
1535 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001536 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001537 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001538
Guido van Rossum374a9221991-04-04 10:40:29 +00001539 case DELETE_SLICE+0:
1540 case DELETE_SLICE+1:
1541 case DELETE_SLICE+2:
1542 case DELETE_SLICE+3:
1543 if ((opcode-DELETE_SLICE) & 2)
1544 w = POP();
1545 else
1546 w = NULL;
1547 if ((opcode-DELETE_SLICE) & 1)
1548 v = POP();
1549 else
1550 v = NULL;
1551 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001552 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +00001553 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00001554 Py_DECREF(u);
1555 Py_XDECREF(v);
1556 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001557 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001558 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001559
Guido van Rossum374a9221991-04-04 10:40:29 +00001560 case STORE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001561 w = TOP();
1562 v = SECOND();
1563 u = THIRD();
1564 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001565 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001566 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001567 Py_DECREF(u);
1568 Py_DECREF(v);
1569 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001570 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001571 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001572
Guido van Rossum374a9221991-04-04 10:40:29 +00001573 case DELETE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001574 w = TOP();
1575 v = SECOND();
1576 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001577 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001578 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001579 Py_DECREF(v);
1580 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001581 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001582 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001583
Guido van Rossum374a9221991-04-04 10:40:29 +00001584 case PRINT_EXPR:
1585 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001586 w = PySys_GetObject("displayhook");
1587 if (w == NULL) {
1588 PyErr_SetString(PyExc_RuntimeError,
1589 "lost sys.displayhook");
1590 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001591 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001592 }
1593 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001594 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001595 if (x == NULL)
1596 err = -1;
1597 }
1598 if (err == 0) {
1599 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001600 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001601 if (w == NULL)
1602 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001603 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001604 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001605 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001606 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001607
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001608 case PRINT_ITEM_TO:
1609 w = stream = POP();
1610 /* fall through to PRINT_ITEM */
1611
Guido van Rossum374a9221991-04-04 10:40:29 +00001612 case PRINT_ITEM:
1613 v = POP();
Barry Warsaw093abe02000-08-29 04:56:13 +00001614 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001615 w = PySys_GetObject("stdout");
1616 if (w == NULL) {
1617 PyErr_SetString(PyExc_RuntimeError,
1618 "lost sys.stdout");
1619 err = -1;
1620 }
Guido van Rossum8f183201997-12-31 05:53:15 +00001621 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001622 /* PyFile_SoftSpace() can exececute arbitrary code
1623 if sys.stdout is an instance with a __getattr__.
1624 If __getattr__ raises an exception, w will
1625 be freed, so we need to prevent that temporarily. */
1626 Py_XINCREF(w);
Tim Peters8e5fd532002-03-24 19:25:00 +00001627 if (w != NULL && PyFile_SoftSpace(w, 0))
Guido van Rossumbe270261997-05-22 22:26:18 +00001628 err = PyFile_WriteString(" ", w);
1629 if (err == 0)
1630 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001631 if (err == 0) {
Tim Peters8e5fd532002-03-24 19:25:00 +00001632 /* XXX move into writeobject() ? */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001633 if (PyString_Check(v)) {
1634 char *s = PyString_AS_STRING(v);
1635 Py_ssize_t len = PyString_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001636 if (len == 0 ||
1637 !isspace(Py_CHARMASK(s[len-1])) ||
1638 s[len-1] == ' ')
1639 PyFile_SoftSpace(w, 1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001640 }
Martin v. Löwis8d3ce5a2001-12-18 22:36:40 +00001641#ifdef Py_USING_UNICODE
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001642 else if (PyUnicode_Check(v)) {
1643 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
Martin v. Löwis66851282006-04-22 11:40:03 +00001644 Py_ssize_t len = PyUnicode_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001645 if (len == 0 ||
1646 !Py_UNICODE_ISSPACE(s[len-1]) ||
1647 s[len-1] == ' ')
1648 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001649 }
Michael W. Hudsond95c8282002-05-20 13:56:11 +00001650#endif
Tim Peters8e5fd532002-03-24 19:25:00 +00001651 else
1652 PyFile_SoftSpace(w, 1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001653 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001654 Py_XDECREF(w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001655 Py_DECREF(v);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001656 Py_XDECREF(stream);
1657 stream = NULL;
1658 if (err == 0)
1659 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001660 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001661
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001662 case PRINT_NEWLINE_TO:
1663 w = stream = POP();
1664 /* fall through to PRINT_NEWLINE */
1665
Guido van Rossum374a9221991-04-04 10:40:29 +00001666 case PRINT_NEWLINE:
Barry Warsaw093abe02000-08-29 04:56:13 +00001667 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001668 w = PySys_GetObject("stdout");
Jeffrey Yasskin69614982008-12-11 05:21:18 +00001669 if (w == NULL) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001670 PyErr_SetString(PyExc_RuntimeError,
1671 "lost sys.stdout");
Jeffrey Yasskin69614982008-12-11 05:21:18 +00001672 why = WHY_EXCEPTION;
1673 }
Guido van Rossum3165fe61992-09-25 21:59:05 +00001674 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001675 if (w != NULL) {
Georg Brandlaa76d772008-07-01 20:56:03 +00001676 /* w.write() may replace sys.stdout, so we
1677 * have to keep our reference to it */
Amaury Forgeot d'Arcbdd941f2008-07-01 20:38:04 +00001678 Py_INCREF(w);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001679 err = PyFile_WriteString("\n", w);
1680 if (err == 0)
1681 PyFile_SoftSpace(w, 0);
Amaury Forgeot d'Arcbdd941f2008-07-01 20:38:04 +00001682 Py_DECREF(w);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001683 }
1684 Py_XDECREF(stream);
1685 stream = NULL;
Guido van Rossum374a9221991-04-04 10:40:29 +00001686 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001687
Thomas Wouters434d0822000-08-24 20:11:32 +00001688
1689#ifdef CASE_TOO_BIG
1690 default: switch (opcode) {
1691#endif
Guido van Rossumf10570b1995-07-07 22:53:21 +00001692 case RAISE_VARARGS:
1693 u = v = w = NULL;
1694 switch (oparg) {
1695 case 3:
1696 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001697 /* Fallthrough */
1698 case 2:
1699 v = POP(); /* value */
1700 /* Fallthrough */
1701 case 1:
1702 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001703 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001704 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001705 break;
1706 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001707 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001708 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001709 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001710 break;
1711 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001712 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001713
Guido van Rossum374a9221991-04-04 10:40:29 +00001714 case LOAD_LOCALS:
Raymond Hettinger467a6982004-04-07 11:39:21 +00001715 if ((x = f->f_locals) != NULL) {
1716 Py_INCREF(x);
1717 PUSH(x);
1718 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001719 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001720 PyErr_SetString(PyExc_SystemError, "no locals");
Guido van Rossum374a9221991-04-04 10:40:29 +00001721 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001722
Guido van Rossum374a9221991-04-04 10:40:29 +00001723 case RETURN_VALUE:
1724 retval = POP();
1725 why = WHY_RETURN;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001726 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001727
Tim Peters5ca576e2001-06-18 22:08:13 +00001728 case YIELD_VALUE:
1729 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001730 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001731 why = WHY_YIELD;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001732 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001733
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001734 case EXEC_STMT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001735 w = TOP();
1736 v = SECOND();
1737 u = THIRD();
1738 STACKADJ(-3);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001739 READ_TIMESTAMP(intr0);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001740 err = exec_statement(f, u, v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001741 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00001742 Py_DECREF(u);
1743 Py_DECREF(v);
1744 Py_DECREF(w);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001745 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001746
Guido van Rossum374a9221991-04-04 10:40:29 +00001747 case POP_BLOCK:
1748 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001749 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001750 while (STACK_LEVEL() > b->b_level) {
1751 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001752 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001753 }
1754 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001755 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001756
Jeffrey Yasskin9063a992008-03-03 01:27:03 +00001757 PREDICTED(END_FINALLY);
Guido van Rossum374a9221991-04-04 10:40:29 +00001758 case END_FINALLY:
1759 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001760 if (PyInt_Check(v)) {
Raymond Hettinger7c958652004-04-06 10:11:10 +00001761 why = (enum why_code) PyInt_AS_LONG(v);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001762 assert(why != WHY_YIELD);
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00001763 if (why == WHY_RETURN ||
1764 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001765 retval = POP();
1766 }
Thomas Wouterse2176022007-09-20 17:35:10 +00001767 else if (PyExceptionClass_Check(v) ||
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001768 PyString_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001769 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001770 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001771 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001772 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001773 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001774 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001775 else if (v != Py_None) {
1776 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001777 "'finally' pops bad exception");
1778 why = WHY_EXCEPTION;
1779 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001780 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001781 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001782
Guido van Rossum374a9221991-04-04 10:40:29 +00001783 case BUILD_CLASS:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001784 u = TOP();
1785 v = SECOND();
1786 w = THIRD();
1787 STACKADJ(-2);
Guido van Rossum25831651993-05-19 14:50:45 +00001788 x = build_class(u, v, w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001789 SET_TOP(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001790 Py_DECREF(u);
1791 Py_DECREF(v);
1792 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001793 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001794
Guido van Rossum374a9221991-04-04 10:40:29 +00001795 case STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001796 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001797 v = POP();
Raymond Hettinger467a6982004-04-07 11:39:21 +00001798 if ((x = f->f_locals) != NULL) {
Raymond Hettinger66bd2332004-08-02 08:30:07 +00001799 if (PyDict_CheckExact(x))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001800 err = PyDict_SetItem(x, w, v);
1801 else
1802 err = PyObject_SetItem(x, w, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001803 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001804 if (err == 0) continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001805 break;
1806 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001807 PyErr_Format(PyExc_SystemError,
1808 "no locals found when storing %s",
1809 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001810 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001811
Guido van Rossum374a9221991-04-04 10:40:29 +00001812 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001813 w = GETITEM(names, oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001814 if ((x = f->f_locals) != NULL) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001815 if ((err = PyObject_DelItem(x, w)) != 0)
Raymond Hettinger467a6982004-04-07 11:39:21 +00001816 format_exc_check_arg(PyExc_NameError,
Thomas Wouterse2176022007-09-20 17:35:10 +00001817 NAME_ERROR_MSG,
1818 w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001819 break;
1820 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001821 PyErr_Format(PyExc_SystemError,
1822 "no locals when deleting %s",
1823 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001824 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001825
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001826 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001827 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001828 v = POP();
Thomas Wouterse2176022007-09-20 17:35:10 +00001829 if (PyTuple_CheckExact(v) &&
1830 PyTuple_GET_SIZE(v) == oparg) {
1831 PyObject **items = \
1832 ((PyTupleObject *)v)->ob_item;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001833 while (oparg--) {
1834 w = items[oparg];
1835 Py_INCREF(w);
1836 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001837 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001838 Py_DECREF(v);
1839 continue;
Thomas Wouterse2176022007-09-20 17:35:10 +00001840 } else if (PyList_CheckExact(v) &&
1841 PyList_GET_SIZE(v) == oparg) {
1842 PyObject **items = \
1843 ((PyListObject *)v)->ob_item;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001844 while (oparg--) {
1845 w = items[oparg];
1846 Py_INCREF(w);
1847 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001848 }
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001849 } else if (unpack_iterable(v, oparg,
Thomas Wouterse2176022007-09-20 17:35:10 +00001850 stack_pointer + oparg)) {
Tim Petersd6d010b2001-06-21 02:49:55 +00001851 stack_pointer += oparg;
Georg Brandl5cb76c12007-03-21 09:00:39 +00001852 } else {
1853 /* unpack_iterable() raised an exception */
Barry Warsawe42b18f1997-08-25 22:13:04 +00001854 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001855 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001856 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001857 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001858
Guido van Rossum374a9221991-04-04 10:40:29 +00001859 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001860 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001861 v = TOP();
1862 u = SECOND();
1863 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001864 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1865 Py_DECREF(v);
1866 Py_DECREF(u);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001867 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001868 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001869
Guido van Rossum374a9221991-04-04 10:40:29 +00001870 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001871 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001872 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001873 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1874 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001875 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001876 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001877
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001878 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001879 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001880 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001881 err = PyDict_SetItem(f->f_globals, w, v);
1882 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001883 if (err == 0) continue;
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001884 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001885
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001886 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001887 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001888 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001889 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001890 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001891 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001892
Guido van Rossum374a9221991-04-04 10:40:29 +00001893 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001894 w = GETITEM(names, oparg);
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001895 if ((v = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001896 PyErr_Format(PyExc_SystemError,
1897 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001898 PyObject_REPR(w));
Jeffrey Yasskin69614982008-12-11 05:21:18 +00001899 why = WHY_EXCEPTION;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001900 break;
1901 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001902 if (PyDict_CheckExact(v)) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001903 x = PyDict_GetItem(v, w);
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001904 Py_XINCREF(x);
1905 }
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001906 else {
1907 x = PyObject_GetItem(v, w);
1908 if (x == NULL && PyErr_Occurred()) {
Thomas Wouterse2176022007-09-20 17:35:10 +00001909 if (!PyErr_ExceptionMatches(
1910 PyExc_KeyError))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001911 break;
1912 PyErr_Clear();
1913 }
1914 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001915 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001916 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001917 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001918 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001919 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001920 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001921 PyExc_NameError,
Thomas Wouterse2176022007-09-20 17:35:10 +00001922 NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001923 break;
1924 }
1925 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001926 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001927 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001928 PUSH(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001929 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001930
Guido van Rossum374a9221991-04-04 10:40:29 +00001931 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001932 w = GETITEM(names, oparg);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001933 if (PyString_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00001934 /* Inline the PyDict_GetItem() calls.
1935 WARNING: this is an extreme speed hack.
1936 Do not try this at home. */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001937 long hash = ((PyStringObject *)w)->ob_shash;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001938 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001939 PyDictObject *d;
Armin Rigo35f6d362006-06-01 13:19:12 +00001940 PyDictEntry *e;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001941 d = (PyDictObject *)(f->f_globals);
Armin Rigo35f6d362006-06-01 13:19:12 +00001942 e = d->ma_lookup(d, w, hash);
1943 if (e == NULL) {
1944 x = NULL;
1945 break;
1946 }
1947 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001948 if (x != NULL) {
1949 Py_INCREF(x);
1950 PUSH(x);
1951 continue;
1952 }
1953 d = (PyDictObject *)(f->f_builtins);
Armin Rigo35f6d362006-06-01 13:19:12 +00001954 e = d->ma_lookup(d, w, hash);
1955 if (e == NULL) {
1956 x = NULL;
1957 break;
1958 }
1959 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001960 if (x != NULL) {
1961 Py_INCREF(x);
1962 PUSH(x);
1963 continue;
1964 }
1965 goto load_global_error;
1966 }
1967 }
1968 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00001969 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001970 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001971 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001972 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001973 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00001974 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001975 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001976 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001977 break;
1978 }
1979 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001980 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001981 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001982 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001983
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001984 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001985 x = GETLOCAL(oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001986 if (x != NULL) {
1987 SETLOCAL(oparg, NULL);
1988 continue;
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001989 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001990 format_exc_check_arg(
1991 PyExc_UnboundLocalError,
1992 UNBOUNDLOCAL_ERROR_MSG,
1993 PyTuple_GetItem(co->co_varnames, oparg)
1994 );
1995 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001996
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001997 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001998 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001999 Py_INCREF(x);
2000 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00002001 if (x != NULL) continue;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002002 break;
2003
2004 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00002005 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002006 w = PyCell_Get(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002007 if (w != NULL) {
2008 PUSH(w);
2009 continue;
Jeremy Hylton2524d692001-02-05 17:23:16 +00002010 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00002011 err = -1;
2012 /* Don't stomp existing exception */
2013 if (PyErr_Occurred())
2014 break;
Richard Jonescebbefc2006-05-23 18:28:17 +00002015 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
2016 v = PyTuple_GET_ITEM(co->co_cellvars,
Raymond Hettinger467a6982004-04-07 11:39:21 +00002017 oparg);
2018 format_exc_check_arg(
2019 PyExc_UnboundLocalError,
2020 UNBOUNDLOCAL_ERROR_MSG,
2021 v);
2022 } else {
Thomas Wouterse2176022007-09-20 17:35:10 +00002023 v = PyTuple_GET_ITEM(co->co_freevars, oparg -
2024 PyTuple_GET_SIZE(co->co_cellvars));
2025 format_exc_check_arg(PyExc_NameError,
2026 UNBOUNDFREE_ERROR_MSG, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002027 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002028 break;
2029
2030 case STORE_DEREF:
2031 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00002032 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002033 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00002034 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002035 continue;
2036
Guido van Rossum374a9221991-04-04 10:40:29 +00002037 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00002038 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002039 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002040 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002041 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002042 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002043 }
2044 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002045 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002046 }
2047 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002048
Guido van Rossum374a9221991-04-04 10:40:29 +00002049 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00002050 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002051 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002052 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002053 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00002054 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002055 }
2056 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002057 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002058 }
2059 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002060
Guido van Rossum374a9221991-04-04 10:40:29 +00002061 case BUILD_MAP:
Raymond Hettingerfd7ed402007-12-18 21:24:09 +00002062 x = _PyDict_NewPresized((Py_ssize_t)oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002063 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002064 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002065 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002066
Raymond Hettingereffde122007-12-18 18:26:18 +00002067 case STORE_MAP:
2068 w = TOP(); /* key */
2069 u = SECOND(); /* value */
2070 v = THIRD(); /* dict */
2071 STACKADJ(-2);
2072 assert (PyDict_CheckExact(v));
2073 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2074 Py_DECREF(u);
2075 Py_DECREF(w);
2076 if (err == 0) continue;
2077 break;
2078
Guido van Rossum374a9221991-04-04 10:40:29 +00002079 case LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00002080 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002081 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002082 x = PyObject_GetAttr(v, w);
2083 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002084 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002085 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002086 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002087
Guido van Rossum374a9221991-04-04 10:40:29 +00002088 case COMPARE_OP:
2089 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002090 v = TOP();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00002091 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00002092 /* INLINE: cmp(int, int) */
2093 register long a, b;
2094 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00002095 a = PyInt_AS_LONG(v);
2096 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00002097 switch (oparg) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00002098 case PyCmp_LT: res = a < b; break;
2099 case PyCmp_LE: res = a <= b; break;
2100 case PyCmp_EQ: res = a == b; break;
2101 case PyCmp_NE: res = a != b; break;
2102 case PyCmp_GT: res = a > b; break;
2103 case PyCmp_GE: res = a >= b; break;
2104 case PyCmp_IS: res = v == w; break;
2105 case PyCmp_IS_NOT: res = v != w; break;
Guido van Rossumc12da691997-07-17 23:12:42 +00002106 default: goto slow_compare;
2107 }
2108 x = res ? Py_True : Py_False;
2109 Py_INCREF(x);
2110 }
2111 else {
2112 slow_compare:
2113 x = cmp_outcome(oparg, v, w);
2114 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002115 Py_DECREF(v);
2116 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002117 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00002118 if (x == NULL) break;
2119 PREDICT(JUMP_IF_FALSE);
2120 PREDICT(JUMP_IF_TRUE);
2121 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002122
Guido van Rossum374a9221991-04-04 10:40:29 +00002123 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00002124 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002125 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002126 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002127 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00002128 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002129 break;
2130 }
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00002131 Py_INCREF(x);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002132 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002133 u = TOP();
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002134 if (PyInt_AsLong(u) != -1 || PyErr_Occurred())
2135 w = PyTuple_Pack(5,
2136 w,
2137 f->f_globals,
2138 f->f_locals == NULL ?
2139 Py_None : f->f_locals,
2140 v,
2141 u);
2142 else
2143 w = PyTuple_Pack(4,
2144 w,
2145 f->f_globals,
2146 f->f_locals == NULL ?
2147 Py_None : f->f_locals,
2148 v);
2149 Py_DECREF(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002150 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002151 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002152 u = POP();
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00002153 Py_DECREF(x);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002154 x = NULL;
2155 break;
2156 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002157 READ_TIMESTAMP(intr0);
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00002158 v = x;
2159 x = PyEval_CallObject(v, w);
2160 Py_DECREF(v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002161 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002162 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002163 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002164 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002165 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002166
Thomas Wouters52152252000-08-17 22:55:00 +00002167 case IMPORT_STAR:
2168 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002169 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002170 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002171 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00002172 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002173 break;
2174 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002175 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002176 err = import_all_from(x, v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002177 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002178 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00002179 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002180 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002181 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002182
Thomas Wouters52152252000-08-17 22:55:00 +00002183 case IMPORT_FROM:
Skip Montanaro496e6582002-08-06 17:47:40 +00002184 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00002185 v = TOP();
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002186 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002187 x = import_from(v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002188 READ_TIMESTAMP(intr1);
Thomas Wouters52152252000-08-17 22:55:00 +00002189 PUSH(x);
2190 if (x != NULL) continue;
2191 break;
2192
Guido van Rossum374a9221991-04-04 10:40:29 +00002193 case JUMP_FORWARD:
2194 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002195 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002196
Raymond Hettingerf606f872003-03-16 03:11:04 +00002197 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002198 case JUMP_IF_FALSE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002199 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002200 if (w == Py_True) {
2201 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002202 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002203 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002204 if (w == Py_False) {
2205 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002206 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002207 }
2208 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002209 if (err > 0)
2210 err = 0;
2211 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00002212 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002213 else
2214 break;
2215 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002216
Raymond Hettingerf606f872003-03-16 03:11:04 +00002217 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002218 case JUMP_IF_TRUE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002219 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002220 if (w == Py_False) {
2221 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002222 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002223 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002224 if (w == Py_True) {
2225 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002226 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002227 }
2228 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002229 if (err > 0) {
2230 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00002231 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002232 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002233 else if (err == 0)
2234 ;
2235 else
2236 break;
2237 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002238
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00002239 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002240 case JUMP_ABSOLUTE:
2241 JUMPTO(oparg);
Raymond Hettingerdc1d1ba2007-11-07 02:45:46 +00002242#if FAST_LOOPS
2243 /* Enabling this path speeds-up all while and for-loops by bypassing
2244 the per-loop checks for signals. By default, this should be turned-off
2245 because it prevents detection of a control-break in tight loops like
2246 "while 1: pass". Compile with this option turned-on when you need
2247 the speed-up and do not need break checking inside tight loops (ones
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00002248 that contain only instructions ending with goto fast_next_opcode).
Raymond Hettingerdc1d1ba2007-11-07 02:45:46 +00002249 */
2250 goto fast_next_opcode;
2251#else
Neil Schemenauerca2a2f12003-05-30 23:59:44 +00002252 continue;
Raymond Hettingerdc1d1ba2007-11-07 02:45:46 +00002253#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002254
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002255 case GET_ITER:
2256 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002257 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002258 x = PyObject_GetIter(v);
2259 Py_DECREF(v);
2260 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002261 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002262 PREDICT(FOR_ITER);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002263 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002264 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002265 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002266 break;
2267
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002268 PREDICTED_WITH_ARG(FOR_ITER);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002269 case FOR_ITER:
2270 /* before: [iter]; after: [iter, iter()] *or* [] */
2271 v = TOP();
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002272 x = (*v->ob_type->tp_iternext)(v);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002273 if (x != NULL) {
2274 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002275 PREDICT(STORE_FAST);
2276 PREDICT(UNPACK_SEQUENCE);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002277 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002278 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002279 if (PyErr_Occurred()) {
Thomas Wouterse2176022007-09-20 17:35:10 +00002280 if (!PyErr_ExceptionMatches(
2281 PyExc_StopIteration))
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002282 break;
2283 PyErr_Clear();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002284 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002285 /* iterator ended normally */
2286 x = v = POP();
2287 Py_DECREF(v);
2288 JUMPBY(oparg);
2289 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002290
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002291 case BREAK_LOOP:
2292 why = WHY_BREAK;
2293 goto fast_block_end;
2294
2295 case CONTINUE_LOOP:
2296 retval = PyInt_FromLong(oparg);
Neal Norwitz02104df2006-05-19 06:31:23 +00002297 if (!retval) {
2298 x = NULL;
2299 break;
2300 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002301 why = WHY_CONTINUE;
2302 goto fast_block_end;
2303
Guido van Rossum374a9221991-04-04 10:40:29 +00002304 case SETUP_LOOP:
2305 case SETUP_EXCEPT:
2306 case SETUP_FINALLY:
Thomas Wouterse2176022007-09-20 17:35:10 +00002307 /* NOTE: If you add any new block-setup opcodes that
2308 are not try/except/finally handlers, you may need
2309 to update the PyGen_NeedsFinalizing() function.
2310 */
Phillip J. Eby2ba96612006-04-10 17:51:05 +00002311
Guido van Rossumb209a111997-04-29 18:18:01 +00002312 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002313 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002314 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002315
Guido van Rossumc2e20742006-02-27 22:32:47 +00002316 case WITH_CLEANUP:
2317 {
Nick Coghlan7af53be2008-03-07 14:13:28 +00002318 /* At the top of the stack are 1-3 values indicating
2319 how/why we entered the finally clause:
2320 - TOP = None
2321 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2322 - TOP = WHY_*; no retval below it
2323 - (TOP, SECOND, THIRD) = exc_info()
2324 Below them is EXIT, the context.__exit__ bound method.
Guido van Rossumc2e20742006-02-27 22:32:47 +00002325 In the last case, we must call
Nick Coghlan7af53be2008-03-07 14:13:28 +00002326 EXIT(TOP, SECOND, THIRD)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002327 otherwise we must call
Nick Coghlan7af53be2008-03-07 14:13:28 +00002328 EXIT(None, None, None)
2329
2330 In all cases, we remove EXIT from the stack, leaving
2331 the rest in the same order.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002332
2333 In addition, if the stack represents an exception,
Guido van Rossumf6694362006-03-10 02:28:35 +00002334 *and* the function call returns a 'true' value, we
2335 "zap" this information, to prevent END_FINALLY from
2336 re-raising the exception. (But non-local gotos
2337 should still be resumed.)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002338 */
Tim Peters7df5e7f2006-05-26 23:14:37 +00002339
Nick Coghlan7af53be2008-03-07 14:13:28 +00002340 PyObject *exit_func;
2341
2342 u = POP();
2343 if (u == Py_None) {
2344 exit_func = TOP();
2345 SET_TOP(u);
2346 v = w = Py_None;
2347 }
2348 else if (PyInt_Check(u)) {
2349 switch(PyInt_AS_LONG(u)) {
2350 case WHY_RETURN:
2351 case WHY_CONTINUE:
2352 /* Retval in TOP. */
2353 exit_func = SECOND();
2354 SET_SECOND(TOP());
2355 SET_TOP(u);
2356 break;
2357 default:
2358 exit_func = TOP();
2359 SET_TOP(u);
2360 break;
2361 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002362 u = v = w = Py_None;
2363 }
2364 else {
Nick Coghlan7af53be2008-03-07 14:13:28 +00002365 v = TOP();
2366 w = SECOND();
2367 exit_func = THIRD();
2368 SET_TOP(u);
2369 SET_SECOND(v);
2370 SET_THIRD(w);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002371 }
Guido van Rossumf6694362006-03-10 02:28:35 +00002372 /* XXX Not the fastest way to call it... */
Nick Coghlan7af53be2008-03-07 14:13:28 +00002373 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2374 NULL);
Amaury Forgeot d'Arc79c9f762008-12-10 23:56:33 +00002375 Py_DECREF(exit_func);
2376 if (x == NULL)
Guido van Rossumf6694362006-03-10 02:28:35 +00002377 break; /* Go to error exit */
Amaury Forgeot d'Arc79c9f762008-12-10 23:56:33 +00002378
2379 if (u != Py_None)
2380 err = PyObject_IsTrue(x);
2381 else
2382 err = 0;
2383 Py_DECREF(x);
2384
2385 if (err < 0)
2386 break; /* Go to error exit */
2387 else if (err > 0) {
2388 err = 0;
Guido van Rossumf6694362006-03-10 02:28:35 +00002389 /* There was an exception and a true return */
Nick Coghlan7af53be2008-03-07 14:13:28 +00002390 STACKADJ(-2);
Guido van Rossumf6694362006-03-10 02:28:35 +00002391 Py_INCREF(Py_None);
2392 SET_TOP(Py_None);
Guido van Rossumf6694362006-03-10 02:28:35 +00002393 Py_DECREF(u);
2394 Py_DECREF(v);
2395 Py_DECREF(w);
2396 } else {
Nick Coghlan7af53be2008-03-07 14:13:28 +00002397 /* The stack was rearranged to remove EXIT
2398 above. Let END_FINALLY do its thing */
Guido van Rossumf6694362006-03-10 02:28:35 +00002399 }
Jeffrey Yasskin9063a992008-03-03 01:27:03 +00002400 PREDICT(END_FINALLY);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002401 break;
2402 }
2403
Guido van Rossumf10570b1995-07-07 22:53:21 +00002404 case CALL_FUNCTION:
Armin Rigo8817fcd2004-06-17 10:22:40 +00002405 {
2406 PyObject **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002407 PCALL(PCALL_ALL);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002408 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002409#ifdef WITH_TSC
Armin Rigo8817fcd2004-06-17 10:22:40 +00002410 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002411#else
Armin Rigo8817fcd2004-06-17 10:22:40 +00002412 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002413#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002414 stack_pointer = sp;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002415 PUSH(x);
2416 if (x != NULL)
2417 continue;
2418 break;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002419 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002420
Jeremy Hylton76901512000-03-28 23:49:17 +00002421 case CALL_FUNCTION_VAR:
2422 case CALL_FUNCTION_KW:
2423 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002424 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002425 int na = oparg & 0xff;
2426 int nk = (oparg>>8) & 0xff;
2427 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002428 int n = na + 2 * nk;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002429 PyObject **pfunc, *func, **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002430 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002431 if (flags & CALL_FLAG_VAR)
2432 n++;
2433 if (flags & CALL_FLAG_KW)
2434 n++;
2435 pfunc = stack_pointer - n - 1;
2436 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002437
Guido van Rossumac7be682001-01-17 15:42:30 +00002438 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002439 && PyMethod_GET_SELF(func) != NULL) {
2440 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002441 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002442 func = PyMethod_GET_FUNCTION(func);
2443 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002444 Py_DECREF(*pfunc);
2445 *pfunc = self;
2446 na++;
2447 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002448 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002449 Py_INCREF(func);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002450 sp = stack_pointer;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002451 READ_TIMESTAMP(intr0);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002452 x = ext_do_call(func, &sp, flags, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002453 READ_TIMESTAMP(intr1);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002454 stack_pointer = sp;
Jeremy Hylton76901512000-03-28 23:49:17 +00002455 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002456
Jeremy Hylton76901512000-03-28 23:49:17 +00002457 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002458 w = POP();
2459 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002460 }
2461 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002462 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002463 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002464 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002465 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002466
Guido van Rossum681d79a1995-07-18 14:51:37 +00002467 case MAKE_FUNCTION:
2468 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002469 x = PyFunction_New(v, f->f_globals);
2470 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002471 /* XXX Maybe this should be a separate opcode? */
2472 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002473 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002474 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002475 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002476 x = NULL;
2477 break;
2478 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002479 while (--oparg >= 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002480 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002481 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002482 }
2483 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002484 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002485 }
2486 PUSH(x);
2487 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002488
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002489 case MAKE_CLOSURE:
2490 {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002491 v = POP(); /* code object */
2492 x = PyFunction_New(v, f->f_globals);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002493 Py_DECREF(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002494 if (x != NULL) {
2495 v = POP();
Jeffrey Yasskin69614982008-12-11 05:21:18 +00002496 if (PyFunction_SetClosure(x, v) != 0) {
2497 /* Can't happen unless bytecode is corrupt. */
2498 why = WHY_EXCEPTION;
2499 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002500 Py_DECREF(v);
2501 }
2502 if (x != NULL && oparg > 0) {
2503 v = PyTuple_New(oparg);
2504 if (v == NULL) {
2505 Py_DECREF(x);
2506 x = NULL;
2507 break;
2508 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002509 while (--oparg >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002510 w = POP();
2511 PyTuple_SET_ITEM(v, oparg, w);
2512 }
Jeffrey Yasskin69614982008-12-11 05:21:18 +00002513 if (PyFunction_SetDefaults(x, v) != 0) {
2514 /* Can't happen unless
2515 PyFunction_SetDefaults changes. */
2516 why = WHY_EXCEPTION;
2517 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002518 Py_DECREF(v);
2519 }
2520 PUSH(x);
2521 break;
2522 }
2523
Guido van Rossum8861b741996-07-30 16:49:37 +00002524 case BUILD_SLICE:
2525 if (oparg == 3)
2526 w = POP();
2527 else
2528 w = NULL;
2529 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002530 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002531 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002532 Py_DECREF(u);
2533 Py_DECREF(v);
2534 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002535 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002536 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002537 break;
2538
Fred Drakeef8ace32000-08-24 00:32:09 +00002539 case EXTENDED_ARG:
2540 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002541 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002542 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002543
Guido van Rossum374a9221991-04-04 10:40:29 +00002544 default:
2545 fprintf(stderr,
2546 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002547 PyCode_Addr2Line(f->f_code, f->f_lasti),
2548 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002549 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002550 why = WHY_EXCEPTION;
2551 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002552
2553#ifdef CASE_TOO_BIG
2554 }
2555#endif
2556
Guido van Rossum374a9221991-04-04 10:40:29 +00002557 } /* switch */
2558
2559 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002560
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002561 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002562
Guido van Rossum374a9221991-04-04 10:40:29 +00002563 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002564
Guido van Rossum374a9221991-04-04 10:40:29 +00002565 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002566 if (err == 0 && x != NULL) {
2567#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002568 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002569 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002570 fprintf(stderr,
2571 "XXX undetected error\n");
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002572 else {
2573#endif
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002574 READ_TIMESTAMP(loop1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002575 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002576#ifdef CHECKEXC
2577 }
2578#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +00002579 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002580 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002581 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002582 err = 0;
2583 }
2584
Guido van Rossum374a9221991-04-04 10:40:29 +00002585 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002586
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002587 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002588 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002589 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002590 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002591 why = WHY_EXCEPTION;
2592 }
2593 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002594#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002595 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002596 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002597 if (PyErr_Occurred()) {
Neal Norwitz8250fbe2008-01-27 17:12:15 +00002598 char buf[128];
Jeremy Hylton904ed862003-11-05 17:29:35 +00002599 sprintf(buf, "Stack unwind with exception "
2600 "set and why=%d", why);
2601 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002602 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002603 }
2604#endif
2605
2606 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002607
Guido van Rossum374a9221991-04-04 10:40:29 +00002608 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002609 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002610
Fred Drake8f51f542001-10-04 14:48:42 +00002611 if (tstate->c_tracefunc != NULL)
2612 call_exc_trace(tstate->c_tracefunc,
2613 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002614 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002615
Guido van Rossum374a9221991-04-04 10:40:29 +00002616 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002617
Guido van Rossum374a9221991-04-04 10:40:29 +00002618 if (why == WHY_RERAISE)
2619 why = WHY_EXCEPTION;
2620
2621 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002622
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002623fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002624 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002625 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002626
Tim Peters8a5c3c72004-04-05 19:36:21 +00002627 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002628 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2629 /* For a continue inside a try block,
2630 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002631 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2632 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002633 why = WHY_NOT;
2634 JUMPTO(PyInt_AS_LONG(retval));
2635 Py_DECREF(retval);
2636 break;
2637 }
2638
Guido van Rossum374a9221991-04-04 10:40:29 +00002639 while (STACK_LEVEL() > b->b_level) {
2640 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002641 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002642 }
2643 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2644 why = WHY_NOT;
2645 JUMPTO(b->b_handler);
2646 break;
2647 }
2648 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002649 (b->b_type == SETUP_EXCEPT &&
2650 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002651 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002652 PyObject *exc, *val, *tb;
2653 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002654 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002655 val = Py_None;
2656 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002657 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002658 /* Make the raw exception data
2659 available to the handler,
2660 so a program can emulate the
2661 Python main loop. Don't do
2662 this for 'finally'. */
2663 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002664 PyErr_NormalizeException(
2665 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002666 set_exc_info(tstate,
2667 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002668 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002669 if (tb == NULL) {
2670 Py_INCREF(Py_None);
2671 PUSH(Py_None);
2672 } else
2673 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002674 PUSH(val);
2675 PUSH(exc);
2676 }
2677 else {
Raymond Hettinger06032cb2004-04-06 09:37:35 +00002678 if (why & (WHY_RETURN | WHY_CONTINUE))
Guido van Rossum374a9221991-04-04 10:40:29 +00002679 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002680 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002681 PUSH(v);
2682 }
2683 why = WHY_NOT;
2684 JUMPTO(b->b_handler);
2685 break;
2686 }
2687 } /* unwind stack */
2688
2689 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002690
Guido van Rossum374a9221991-04-04 10:40:29 +00002691 if (why != WHY_NOT)
2692 break;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002693 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002694
Guido van Rossum374a9221991-04-04 10:40:29 +00002695 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002696
Tim Peters8a5c3c72004-04-05 19:36:21 +00002697 assert(why != WHY_YIELD);
2698 /* Pop remaining stack entries. */
2699 while (!EMPTY()) {
2700 v = POP();
2701 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002702 }
2703
Tim Peters8a5c3c72004-04-05 19:36:21 +00002704 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002705 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002706
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002707fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002708 if (tstate->use_tracing) {
Barry Warsawe2eca0b2005-08-15 18:14:19 +00002709 if (tstate->c_tracefunc) {
2710 if (why == WHY_RETURN || why == WHY_YIELD) {
2711 if (call_trace(tstate->c_tracefunc,
2712 tstate->c_traceobj, f,
2713 PyTrace_RETURN, retval)) {
2714 Py_XDECREF(retval);
2715 retval = NULL;
2716 why = WHY_EXCEPTION;
2717 }
2718 }
2719 else if (why == WHY_EXCEPTION) {
2720 call_trace_protected(tstate->c_tracefunc,
2721 tstate->c_traceobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002722 PyTrace_RETURN, NULL);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002723 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002724 }
Fred Drake8f51f542001-10-04 14:48:42 +00002725 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002726 if (why == WHY_EXCEPTION)
2727 call_trace_protected(tstate->c_profilefunc,
2728 tstate->c_profileobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002729 PyTrace_RETURN, NULL);
Fred Drake4ec5d562001-10-04 19:26:43 +00002730 else if (call_trace(tstate->c_profilefunc,
2731 tstate->c_profileobj, f,
2732 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002733 Py_XDECREF(retval);
2734 retval = NULL;
2735 why = WHY_EXCEPTION;
2736 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002737 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002738 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002739
Tim Peters7df5e7f2006-05-26 23:14:37 +00002740 if (tstate->frame->f_exc_type != NULL)
2741 reset_exc_info(tstate);
2742 else {
2743 assert(tstate->frame->f_exc_value == NULL);
2744 assert(tstate->frame->f_exc_traceback == NULL);
2745 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00002746
Tim Peters5ca576e2001-06-18 22:08:13 +00002747 /* pop frame */
Thomas Woutersae406c62007-09-19 17:27:43 +00002748exit_eval_frame:
Armin Rigo2b3eb402003-10-28 12:05:48 +00002749 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002750 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002751
Guido van Rossum96a42c81992-01-12 02:29:51 +00002752 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002753}
2754
Guido van Rossumc2e20742006-02-27 22:32:47 +00002755/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002756 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00002757 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002758
Tim Peters6d6c1a32001-08-02 04:15:00 +00002759PyObject *
2760PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002761 PyObject **args, int argcount, PyObject **kws, int kwcount,
2762 PyObject **defs, int defcount, PyObject *closure)
2763{
2764 register PyFrameObject *f;
2765 register PyObject *retval = NULL;
2766 register PyObject **fastlocals, **freevars;
2767 PyThreadState *tstate = PyThreadState_GET();
2768 PyObject *x, *u;
2769
2770 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00002771 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002772 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002773 return NULL;
2774 }
2775
Neal Norwitzdf6a6492006-08-13 18:10:10 +00002776 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00002777 assert(globals != NULL);
2778 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002779 if (f == NULL)
2780 return NULL;
2781
2782 fastlocals = f->f_localsplus;
Richard Jonescebbefc2006-05-23 18:28:17 +00002783 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00002784
2785 if (co->co_argcount > 0 ||
2786 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2787 int i;
2788 int n = argcount;
2789 PyObject *kwdict = NULL;
2790 if (co->co_flags & CO_VARKEYWORDS) {
2791 kwdict = PyDict_New();
2792 if (kwdict == NULL)
2793 goto fail;
2794 i = co->co_argcount;
2795 if (co->co_flags & CO_VARARGS)
2796 i++;
2797 SETLOCAL(i, kwdict);
2798 }
2799 if (argcount > co->co_argcount) {
2800 if (!(co->co_flags & CO_VARARGS)) {
2801 PyErr_Format(PyExc_TypeError,
2802 "%.200s() takes %s %d "
2803 "%sargument%s (%d given)",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002804 PyString_AsString(co->co_name),
Tim Peters5ca576e2001-06-18 22:08:13 +00002805 defcount ? "at most" : "exactly",
2806 co->co_argcount,
2807 kwcount ? "non-keyword " : "",
2808 co->co_argcount == 1 ? "" : "s",
2809 argcount);
2810 goto fail;
2811 }
2812 n = co->co_argcount;
2813 }
2814 for (i = 0; i < n; i++) {
2815 x = args[i];
2816 Py_INCREF(x);
2817 SETLOCAL(i, x);
2818 }
2819 if (co->co_flags & CO_VARARGS) {
2820 u = PyTuple_New(argcount - n);
2821 if (u == NULL)
2822 goto fail;
2823 SETLOCAL(co->co_argcount, u);
2824 for (i = n; i < argcount; i++) {
2825 x = args[i];
2826 Py_INCREF(x);
2827 PyTuple_SET_ITEM(u, i-n, x);
2828 }
2829 }
2830 for (i = 0; i < kwcount; i++) {
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00002831 PyObject **co_varnames;
Tim Peters5ca576e2001-06-18 22:08:13 +00002832 PyObject *keyword = kws[2*i];
2833 PyObject *value = kws[2*i + 1];
2834 int j;
Benjamin Peterson004f3dc2010-02-06 19:16:33 +00002835 if (keyword == NULL || !(PyString_Check(keyword) ||
2836 PyUnicode_Check(keyword))) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002837 PyErr_Format(PyExc_TypeError,
2838 "%.200s() keywords must be strings",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002839 PyString_AsString(co->co_name));
Tim Peters5ca576e2001-06-18 22:08:13 +00002840 goto fail;
2841 }
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00002842 /* Speed hack: do raw pointer compares. As names are
2843 normally interned this should almost always hit. */
2844 co_varnames = PySequence_Fast_ITEMS(co->co_varnames);
Tim Peters5ca576e2001-06-18 22:08:13 +00002845 for (j = 0; j < co->co_argcount; j++) {
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00002846 PyObject *nm = co_varnames[j];
2847 if (nm == keyword)
2848 goto kw_found;
2849 }
2850 /* Slow fallback, just in case */
2851 for (j = 0; j < co->co_argcount; j++) {
2852 PyObject *nm = co_varnames[j];
Tim Peters5ca576e2001-06-18 22:08:13 +00002853 int cmp = PyObject_RichCompareBool(
2854 keyword, nm, Py_EQ);
2855 if (cmp > 0)
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00002856 goto kw_found;
Tim Peters5ca576e2001-06-18 22:08:13 +00002857 else if (cmp < 0)
2858 goto fail;
2859 }
2860 /* Check errors from Compare */
2861 if (PyErr_Occurred())
2862 goto fail;
2863 if (j >= co->co_argcount) {
2864 if (kwdict == NULL) {
Benjamin Peterson004f3dc2010-02-06 19:16:33 +00002865 PyObject *kwd_str = kwd_as_string(keyword);
2866 if (kwd_str) {
2867 PyErr_Format(PyExc_TypeError,
2868 "%.200s() got an unexpected "
2869 "keyword argument '%.400s'",
2870 PyString_AsString(co->co_name),
2871 PyString_AsString(kwd_str));
2872 Py_DECREF(kwd_str);
2873 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002874 goto fail;
2875 }
2876 PyDict_SetItem(kwdict, keyword, value);
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00002877 continue;
Tim Peters5ca576e2001-06-18 22:08:13 +00002878 }
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00002879kw_found:
2880 if (GETLOCAL(j) != NULL) {
Benjamin Peterson004f3dc2010-02-06 19:16:33 +00002881 PyObject *kwd_str = kwd_as_string(keyword);
2882 if (kwd_str) {
2883 PyErr_Format(PyExc_TypeError,
2884 "%.200s() got multiple "
2885 "values for keyword "
2886 "argument '%.400s'",
2887 PyString_AsString(co->co_name),
2888 PyString_AsString(kwd_str));
2889 Py_DECREF(kwd_str);
2890 }
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00002891 goto fail;
Tim Peters5ca576e2001-06-18 22:08:13 +00002892 }
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00002893 Py_INCREF(value);
2894 SETLOCAL(j, value);
Tim Peters5ca576e2001-06-18 22:08:13 +00002895 }
2896 if (argcount < co->co_argcount) {
2897 int m = co->co_argcount - defcount;
2898 for (i = argcount; i < m; i++) {
2899 if (GETLOCAL(i) == NULL) {
2900 PyErr_Format(PyExc_TypeError,
2901 "%.200s() takes %s %d "
2902 "%sargument%s (%d given)",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002903 PyString_AsString(co->co_name),
Tim Peters5ca576e2001-06-18 22:08:13 +00002904 ((co->co_flags & CO_VARARGS) ||
2905 defcount) ? "at least"
2906 : "exactly",
2907 m, kwcount ? "non-keyword " : "",
2908 m == 1 ? "" : "s", i);
2909 goto fail;
2910 }
2911 }
2912 if (n > m)
2913 i = n - m;
2914 else
2915 i = 0;
2916 for (; i < defcount; i++) {
2917 if (GETLOCAL(m+i) == NULL) {
2918 PyObject *def = defs[i];
2919 Py_INCREF(def);
2920 SETLOCAL(m+i, def);
2921 }
2922 }
2923 }
2924 }
2925 else {
2926 if (argcount > 0 || kwcount > 0) {
2927 PyErr_Format(PyExc_TypeError,
2928 "%.200s() takes no arguments (%d given)",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002929 PyString_AsString(co->co_name),
Tim Peters5ca576e2001-06-18 22:08:13 +00002930 argcount + kwcount);
2931 goto fail;
2932 }
2933 }
2934 /* Allocate and initialize storage for cell vars, and copy free
2935 vars into frame. This isn't too efficient right now. */
Richard Jonescebbefc2006-05-23 18:28:17 +00002936 if (PyTuple_GET_SIZE(co->co_cellvars)) {
Neal Norwitz245ce8d2006-06-12 02:16:10 +00002937 int i, j, nargs, found;
Tim Peters5ca576e2001-06-18 22:08:13 +00002938 char *cellname, *argname;
2939 PyObject *c;
2940
2941 nargs = co->co_argcount;
2942 if (co->co_flags & CO_VARARGS)
2943 nargs++;
2944 if (co->co_flags & CO_VARKEYWORDS)
2945 nargs++;
2946
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002947 /* Initialize each cell var, taking into account
2948 cell vars that are initialized from arguments.
2949
2950 Should arrange for the compiler to put cellvars
2951 that are arguments at the beginning of the cellvars
2952 list so that we can march over it more efficiently?
2953 */
Richard Jonescebbefc2006-05-23 18:28:17 +00002954 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002955 cellname = PyString_AS_STRING(
Tim Peters5ca576e2001-06-18 22:08:13 +00002956 PyTuple_GET_ITEM(co->co_cellvars, i));
2957 found = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002958 for (j = 0; j < nargs; j++) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002959 argname = PyString_AS_STRING(
Tim Peters5ca576e2001-06-18 22:08:13 +00002960 PyTuple_GET_ITEM(co->co_varnames, j));
2961 if (strcmp(cellname, argname) == 0) {
2962 c = PyCell_New(GETLOCAL(j));
2963 if (c == NULL)
2964 goto fail;
Richard Jonescebbefc2006-05-23 18:28:17 +00002965 GETLOCAL(co->co_nlocals + i) = c;
Tim Peters5ca576e2001-06-18 22:08:13 +00002966 found = 1;
2967 break;
2968 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002969 }
2970 if (found == 0) {
2971 c = PyCell_New(NULL);
2972 if (c == NULL)
2973 goto fail;
Richard Jonescebbefc2006-05-23 18:28:17 +00002974 SETLOCAL(co->co_nlocals + i, c);
Tim Peters5ca576e2001-06-18 22:08:13 +00002975 }
2976 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002977 }
Richard Jonescebbefc2006-05-23 18:28:17 +00002978 if (PyTuple_GET_SIZE(co->co_freevars)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002979 int i;
Richard Jonescebbefc2006-05-23 18:28:17 +00002980 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002981 PyObject *o = PyTuple_GET_ITEM(closure, i);
2982 Py_INCREF(o);
Richard Jonescebbefc2006-05-23 18:28:17 +00002983 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Tim Peters5ca576e2001-06-18 22:08:13 +00002984 }
2985 }
2986
Tim Peters5ca576e2001-06-18 22:08:13 +00002987 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002988 /* Don't need to keep the reference to f_back, it will be set
2989 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002990 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002991 f->f_back = NULL;
2992
Jeremy Hylton985eba52003-02-05 23:13:00 +00002993 PCALL(PCALL_GENERATOR);
2994
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002995 /* Create a new generator that owns the ready to run frame
2996 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00002997 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00002998 }
2999
Thomas Woutersae406c62007-09-19 17:27:43 +00003000 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003001
Thomas Woutersae406c62007-09-19 17:27:43 +00003002fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003003
Tim Petersb13680b2001-11-27 23:29:29 +00003004 /* decref'ing the frame can cause __del__ methods to get invoked,
3005 which can call back into Python. While we're done with the
3006 current Python frame (f), the associated C stack is still in use,
3007 so recursion_depth must be boosted for the duration.
3008 */
3009 assert(tstate != NULL);
3010 ++tstate->recursion_depth;
Thomas Woutersae406c62007-09-19 17:27:43 +00003011 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00003012 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00003013 return retval;
3014}
3015
3016
Benjamin Peterson004f3dc2010-02-06 19:16:33 +00003017static PyObject *
3018kwd_as_string(PyObject *kwd) {
3019 if (PyString_Check(kwd)) {
3020 Py_INCREF(kwd);
3021 return kwd;
3022 }
3023 else
3024 return _PyUnicode_AsDefaultEncodedString(kwd, "replace");
3025}
3026
3027
Guido van Rossumc9fbb722003-03-01 03:36:33 +00003028/* Implementation notes for set_exc_info() and reset_exc_info():
3029
3030- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
3031 'exc_traceback'. These always travel together.
3032
3033- tstate->curexc_ZZZ is the "hot" exception that is set by
3034 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
3035
3036- Once an exception is caught by an except clause, it is transferred
3037 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
3038 can pick it up. This is the primary task of set_exc_info().
Tim Peters7df5e7f2006-05-26 23:14:37 +00003039 XXX That can't be right: set_exc_info() doesn't look at tstate->curexc_ZZZ.
Guido van Rossumc9fbb722003-03-01 03:36:33 +00003040
3041- Now let me explain the complicated dance with frame->f_exc_ZZZ.
3042
3043 Long ago, when none of this existed, there were just a few globals:
3044 one set corresponding to the "hot" exception, and one set
3045 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
3046 globals; they were simply stored as sys.exc_ZZZ. For backwards
3047 compatibility, they still are!) The problem was that in code like
3048 this:
3049
3050 try:
3051 "something that may fail"
3052 except "some exception":
3053 "do something else first"
3054 "print the exception from sys.exc_ZZZ."
3055
3056 if "do something else first" invoked something that raised and caught
3057 an exception, sys.exc_ZZZ were overwritten. That was a frequent
3058 cause of subtle bugs. I fixed this by changing the semantics as
3059 follows:
3060
3061 - Within one frame, sys.exc_ZZZ will hold the last exception caught
3062 *in that frame*.
3063
3064 - But initially, and as long as no exception is caught in a given
3065 frame, sys.exc_ZZZ will hold the last exception caught in the
3066 previous frame (or the frame before that, etc.).
3067
3068 The first bullet fixed the bug in the above example. The second
3069 bullet was for backwards compatibility: it was (and is) common to
3070 have a function that is called when an exception is caught, and to
3071 have that function access the caught exception via sys.exc_ZZZ.
3072 (Example: traceback.print_exc()).
3073
3074 At the same time I fixed the problem that sys.exc_ZZZ weren't
3075 thread-safe, by introducing sys.exc_info() which gets it from tstate;
3076 but that's really a separate improvement.
3077
3078 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
3079 variables to what they were before the current frame was called. The
3080 set_exc_info() function saves them on the frame so that
3081 reset_exc_info() can restore them. The invariant is that
3082 frame->f_exc_ZZZ is NULL iff the current frame never caught an
3083 exception (where "catching" an exception applies only to successful
3084 except clauses); and if the current frame ever caught an exception,
3085 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
3086 at the start of the current frame.
3087
3088*/
3089
Fredrik Lundh7a830892006-05-27 10:39:48 +00003090static void
Guido van Rossumac7be682001-01-17 15:42:30 +00003091set_exc_info(PyThreadState *tstate,
3092 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00003093{
Tim Peters7df5e7f2006-05-26 23:14:37 +00003094 PyFrameObject *frame = tstate->frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003095 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00003096
Tim Peters7df5e7f2006-05-26 23:14:37 +00003097 assert(type != NULL);
3098 assert(frame != NULL);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003099 if (frame->f_exc_type == NULL) {
Tim Peters7df5e7f2006-05-26 23:14:37 +00003100 assert(frame->f_exc_value == NULL);
3101 assert(frame->f_exc_traceback == NULL);
3102 /* This frame didn't catch an exception before. */
3103 /* Save previous exception of this thread in this frame. */
Guido van Rossuma027efa1997-05-05 20:56:21 +00003104 if (tstate->exc_type == NULL) {
Tim Peters7df5e7f2006-05-26 23:14:37 +00003105 /* XXX Why is this set to Py_None? */
Guido van Rossuma027efa1997-05-05 20:56:21 +00003106 Py_INCREF(Py_None);
3107 tstate->exc_type = Py_None;
3108 }
Tim Peters7df5e7f2006-05-26 23:14:37 +00003109 Py_INCREF(tstate->exc_type);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003110 Py_XINCREF(tstate->exc_value);
3111 Py_XINCREF(tstate->exc_traceback);
3112 frame->f_exc_type = tstate->exc_type;
3113 frame->f_exc_value = tstate->exc_value;
3114 frame->f_exc_traceback = tstate->exc_traceback;
3115 }
Tim Peters7df5e7f2006-05-26 23:14:37 +00003116 /* Set new exception for this thread. */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003117 tmp_type = tstate->exc_type;
3118 tmp_value = tstate->exc_value;
3119 tmp_tb = tstate->exc_traceback;
Tim Peters7df5e7f2006-05-26 23:14:37 +00003120 Py_INCREF(type);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003121 Py_XINCREF(value);
3122 Py_XINCREF(tb);
3123 tstate->exc_type = type;
3124 tstate->exc_value = value;
3125 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003126 Py_XDECREF(tmp_type);
3127 Py_XDECREF(tmp_value);
3128 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003129 /* For b/w compatibility */
3130 PySys_SetObject("exc_type", type);
3131 PySys_SetObject("exc_value", value);
3132 PySys_SetObject("exc_traceback", tb);
3133}
3134
Fredrik Lundh7a830892006-05-27 10:39:48 +00003135static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003136reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00003137{
3138 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003139 PyObject *tmp_type, *tmp_value, *tmp_tb;
Tim Peters7df5e7f2006-05-26 23:14:37 +00003140
3141 /* It's a precondition that the thread state's frame caught an
3142 * exception -- verify in a debug build.
3143 */
3144 assert(tstate != NULL);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003145 frame = tstate->frame;
Tim Peters7df5e7f2006-05-26 23:14:37 +00003146 assert(frame != NULL);
3147 assert(frame->f_exc_type != NULL);
3148
3149 /* Copy the frame's exception info back to the thread state. */
3150 tmp_type = tstate->exc_type;
3151 tmp_value = tstate->exc_value;
3152 tmp_tb = tstate->exc_traceback;
3153 Py_INCREF(frame->f_exc_type);
3154 Py_XINCREF(frame->f_exc_value);
3155 Py_XINCREF(frame->f_exc_traceback);
3156 tstate->exc_type = frame->f_exc_type;
3157 tstate->exc_value = frame->f_exc_value;
3158 tstate->exc_traceback = frame->f_exc_traceback;
3159 Py_XDECREF(tmp_type);
3160 Py_XDECREF(tmp_value);
3161 Py_XDECREF(tmp_tb);
3162
3163 /* For b/w compatibility */
3164 PySys_SetObject("exc_type", frame->f_exc_type);
3165 PySys_SetObject("exc_value", frame->f_exc_value);
3166 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
3167
3168 /* Clear the frame's exception info. */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003169 tmp_type = frame->f_exc_type;
3170 tmp_value = frame->f_exc_value;
3171 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003172 frame->f_exc_type = NULL;
3173 frame->f_exc_value = NULL;
3174 frame->f_exc_traceback = NULL;
Tim Peters7df5e7f2006-05-26 23:14:37 +00003175 Py_DECREF(tmp_type);
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003176 Py_XDECREF(tmp_value);
3177 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003178}
3179
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003180/* Logic for the raise statement (too complicated for inlining).
3181 This *consumes* a reference count to each of its arguments. */
Fredrik Lundh7a830892006-05-27 10:39:48 +00003182static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003183do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003184{
Guido van Rossumd295f121998-04-09 21:39:57 +00003185 if (type == NULL) {
3186 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003187 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumd295f121998-04-09 21:39:57 +00003188 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
3189 value = tstate->exc_value;
3190 tb = tstate->exc_traceback;
3191 Py_XINCREF(type);
3192 Py_XINCREF(value);
3193 Py_XINCREF(tb);
3194 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003195
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003196 /* We support the following forms of raise:
3197 raise <class>, <classinstance>
3198 raise <class>, <argument tuple>
3199 raise <class>, None
3200 raise <class>, <argument>
3201 raise <classinstance>, None
3202 raise <string>, <object>
3203 raise <string>, None
3204
3205 An omitted second argument is the same as None.
3206
3207 In addition, raise <tuple>, <anything> is the same as
3208 raising the tuple's first item (and it better have one!);
3209 this rule is applied recursively.
3210
3211 Finally, an optional third argument can be supplied, which
3212 gives the traceback to be substituted (useful when
3213 re-raising an exception after examining it). */
3214
3215 /* First, check the traceback argument, replacing None with
3216 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00003217 if (tb == Py_None) {
3218 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003219 tb = NULL;
3220 }
3221 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003222 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003223 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003224 goto raise_error;
3225 }
3226
3227 /* Next, replace a missing value with None */
3228 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003229 value = Py_None;
3230 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003231 }
3232
3233 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00003234 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
3235 PyObject *tmp = type;
3236 type = PyTuple_GET_ITEM(type, 0);
3237 Py_INCREF(type);
3238 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003239 }
3240
Brett Cannon129bd522007-01-30 21:34:36 +00003241 if (PyExceptionClass_Check(type))
Barry Warsaw4249f541997-08-22 21:26:19 +00003242 PyErr_NormalizeException(&type, &value, &tb);
3243
Brett Cannonbf364092006-03-01 04:25:17 +00003244 else if (PyExceptionInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003245 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00003246 if (value != Py_None) {
3247 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003248 "instance exception may not have a separate value");
3249 goto raise_error;
3250 }
3251 else {
3252 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00003253 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003254 value = type;
Brett Cannonbf364092006-03-01 04:25:17 +00003255 type = PyExceptionInstance_Class(type);
Guido van Rossumb209a111997-04-29 18:18:01 +00003256 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003257 }
3258 }
3259 else {
3260 /* Not something you can raise. You get an exception
3261 anyway, just not what you specified :-) */
Jeremy Hylton960d9482001-04-27 02:25:33 +00003262 PyErr_Format(PyExc_TypeError,
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00003263 "exceptions must be classes or instances, not %s",
3264 type->ob_type->tp_name);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003265 goto raise_error;
3266 }
Guido van Rossum504153d2008-03-18 04:26:48 +00003267
3268 assert(PyExceptionClass_Check(type));
3269 if (Py_Py3kWarningFlag && PyClass_Check(type)) {
Benjamin Peterson9f4f4812008-04-27 03:01:45 +00003270 if (PyErr_WarnEx(PyExc_DeprecationWarning,
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00003271 "exceptions must derive from BaseException "
3272 "in 3.x", 1) < 0)
Guido van Rossum504153d2008-03-18 04:26:48 +00003273 goto raise_error;
3274 }
3275
Guido van Rossumb209a111997-04-29 18:18:01 +00003276 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003277 if (tb == NULL)
3278 return WHY_EXCEPTION;
3279 else
3280 return WHY_RERAISE;
3281 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00003282 Py_XDECREF(value);
3283 Py_XDECREF(type);
3284 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003285 return WHY_EXCEPTION;
3286}
3287
Tim Petersd6d010b2001-06-21 02:49:55 +00003288/* Iterate v argcnt times and store the results on the stack (via decreasing
3289 sp). Return 1 for success, 0 if error. */
3290
Fredrik Lundh7a830892006-05-27 10:39:48 +00003291static int
Tim Petersd6d010b2001-06-21 02:49:55 +00003292unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003293{
Tim Petersd6d010b2001-06-21 02:49:55 +00003294 int i = 0;
3295 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00003296 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00003297
Tim Petersd6d010b2001-06-21 02:49:55 +00003298 assert(v != NULL);
3299
3300 it = PyObject_GetIter(v);
3301 if (it == NULL)
3302 goto Error;
3303
3304 for (; i < argcnt; i++) {
3305 w = PyIter_Next(it);
3306 if (w == NULL) {
3307 /* Iterator done, via error or exhaustion. */
3308 if (!PyErr_Occurred()) {
3309 PyErr_Format(PyExc_ValueError,
3310 "need more than %d value%s to unpack",
3311 i, i == 1 ? "" : "s");
3312 }
3313 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003314 }
3315 *--sp = w;
3316 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003317
3318 /* We better have exhausted the iterator now. */
3319 w = PyIter_Next(it);
3320 if (w == NULL) {
3321 if (PyErr_Occurred())
3322 goto Error;
3323 Py_DECREF(it);
3324 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003325 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00003326 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00003327 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00003328 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00003329Error:
Barry Warsaw91010551997-08-25 22:30:51 +00003330 for (; i > 0; i--, sp++)
3331 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00003332 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00003333 return 0;
3334}
3335
3336
Guido van Rossum96a42c81992-01-12 02:29:51 +00003337#ifdef LLTRACE
Fredrik Lundh7a830892006-05-27 10:39:48 +00003338static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003339prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003340{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003341 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003342 if (PyObject_Print(v, stdout, 0) != 0)
3343 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003344 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003345 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003346}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003347#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003348
Fredrik Lundh7a830892006-05-27 10:39:48 +00003349static void
Fred Drake5755ce62001-06-27 19:19:46 +00003350call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003351{
Guido van Rossumb209a111997-04-29 18:18:01 +00003352 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003353 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003354 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003355 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003356 value = Py_None;
3357 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003358 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003359 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003360 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003361 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003362 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003363 }
Fred Drake5755ce62001-06-27 19:19:46 +00003364 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003365 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003366 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003367 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003368 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003369 Py_XDECREF(type);
3370 Py_XDECREF(value);
3371 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003372 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003373}
3374
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +00003375static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003376call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003377 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003378{
3379 PyObject *type, *value, *traceback;
3380 int err;
3381 PyErr_Fetch(&type, &value, &traceback);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003382 err = call_trace(func, obj, frame, what, arg);
Fred Drake4ec5d562001-10-04 19:26:43 +00003383 if (err == 0)
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +00003384 {
Fred Drake4ec5d562001-10-04 19:26:43 +00003385 PyErr_Restore(type, value, traceback);
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +00003386 return 0;
3387 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003388 else {
3389 Py_XDECREF(type);
3390 Py_XDECREF(value);
3391 Py_XDECREF(traceback);
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +00003392 return -1;
Fred Drake4ec5d562001-10-04 19:26:43 +00003393 }
3394}
3395
Fredrik Lundh7a830892006-05-27 10:39:48 +00003396static int
Fred Drake5755ce62001-06-27 19:19:46 +00003397call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3398 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003399{
Fred Drake5755ce62001-06-27 19:19:46 +00003400 register PyThreadState *tstate = frame->f_tstate;
3401 int result;
3402 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003403 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003404 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003405 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003406 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003407 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3408 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003409 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003410 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003411}
3412
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003413PyObject *
3414_PyEval_CallTracing(PyObject *func, PyObject *args)
3415{
3416 PyFrameObject *frame = PyEval_GetFrame();
3417 PyThreadState *tstate = frame->f_tstate;
3418 int save_tracing = tstate->tracing;
3419 int save_use_tracing = tstate->use_tracing;
3420 PyObject *result;
3421
3422 tstate->tracing = 0;
3423 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3424 || (tstate->c_profilefunc != NULL));
3425 result = PyObject_Call(func, args, NULL);
3426 tstate->tracing = save_tracing;
3427 tstate->use_tracing = save_use_tracing;
3428 return result;
3429}
3430
Fredrik Lundh7a830892006-05-27 10:39:48 +00003431static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003432maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003433 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3434 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003435{
Michael W. Hudson006c7522002-11-08 13:08:46 +00003436 int result = 0;
3437
Jeremy Hyltona4ebc132006-04-18 14:47:00 +00003438 /* If the last instruction executed isn't in the current
3439 instruction window, reset the window. If the last
3440 instruction happens to fall at the start of a line or if it
3441 represents a jump backwards, call the trace function.
3442 */
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003443 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Thomas Woutersae406c62007-09-19 17:27:43 +00003444 int line;
3445 PyAddrPair bounds;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003446
Thomas Woutersae406c62007-09-19 17:27:43 +00003447 line = PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3448 &bounds);
3449 if (line >= 0) {
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003450 frame->f_lineno = line;
Tim Peters8a5c3c72004-04-05 19:36:21 +00003451 result = call_trace(func, obj, frame,
Michael W. Hudson006c7522002-11-08 13:08:46 +00003452 PyTrace_LINE, Py_None);
Thomas Woutersae406c62007-09-19 17:27:43 +00003453 }
3454 *instr_lb = bounds.ap_lower;
3455 *instr_ub = bounds.ap_upper;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003456 }
Armin Rigobf57a142004-03-22 19:24:58 +00003457 else if (frame->f_lasti <= *instr_prev) {
Jeremy Hyltona4ebc132006-04-18 14:47:00 +00003458 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
Armin Rigobf57a142004-03-22 19:24:58 +00003459 }
3460 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003461 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003462}
3463
Fred Drake5755ce62001-06-27 19:19:46 +00003464void
3465PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003466{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003467 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003468 PyObject *temp = tstate->c_profileobj;
3469 Py_XINCREF(arg);
3470 tstate->c_profilefunc = NULL;
3471 tstate->c_profileobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003472 /* Must make sure that tracing is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003473 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003474 Py_XDECREF(temp);
3475 tstate->c_profilefunc = func;
3476 tstate->c_profileobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003477 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003478 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003479}
3480
3481void
3482PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3483{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003484 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003485 PyObject *temp = tstate->c_traceobj;
Jeffrey Yasskind0a70d42008-12-12 21:25:13 +00003486 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003487 Py_XINCREF(arg);
3488 tstate->c_tracefunc = NULL;
3489 tstate->c_traceobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003490 /* Must make sure that profiling is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003491 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003492 Py_XDECREF(temp);
3493 tstate->c_tracefunc = func;
3494 tstate->c_traceobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003495 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003496 tstate->use_tracing = ((func != NULL)
3497 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003498}
3499
Guido van Rossumb209a111997-04-29 18:18:01 +00003500PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003501PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003502{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003503 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003504 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003505 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003506 else
3507 return current_frame->f_builtins;
3508}
3509
Guido van Rossumb209a111997-04-29 18:18:01 +00003510PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003511PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003512{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003513 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003514 if (current_frame == NULL)
3515 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003516 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003517 return current_frame->f_locals;
3518}
3519
Guido van Rossumb209a111997-04-29 18:18:01 +00003520PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003521PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003522{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003523 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003524 if (current_frame == NULL)
3525 return NULL;
3526 else
3527 return current_frame->f_globals;
3528}
3529
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003530PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003531PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003532{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003533 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003534 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003535}
3536
Guido van Rossum6135a871995-01-09 17:53:26 +00003537int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003538PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003539{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003540 PyFrameObject *current_frame = PyEval_GetFrame();
Neal Norwitzb9845e72006-06-12 02:11:18 +00003541 return current_frame == NULL ? 0 : PyFrame_IsRestricted(current_frame);
Guido van Rossum6135a871995-01-09 17:53:26 +00003542}
3543
Guido van Rossumbe270261997-05-22 22:26:18 +00003544int
Tim Peters5ba58662001-07-16 02:29:45 +00003545PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003546{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003547 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003548 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003549
3550 if (current_frame != NULL) {
3551 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003552 const int compilerflags = codeflags & PyCF_MASK;
3553 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003554 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003555 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003556 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003557#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003558 if (codeflags & CO_GENERATOR_ALLOWED) {
3559 result = 1;
3560 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3561 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003562#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003563 }
3564 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003565}
3566
3567int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003568Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003569{
Guido van Rossumb209a111997-04-29 18:18:01 +00003570 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003571 if (f == NULL)
3572 return 0;
3573 if (!PyFile_SoftSpace(f, 0))
3574 return 0;
3575 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003576}
3577
Guido van Rossum3f5da241990-12-20 15:06:42 +00003578
Guido van Rossum681d79a1995-07-18 14:51:37 +00003579/* External interface to call any callable object.
3580 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003581
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003582#undef PyEval_CallObject
3583/* for backward compatibility: export this interface */
3584
Guido van Rossumb209a111997-04-29 18:18:01 +00003585PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003586PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003587{
Guido van Rossumb209a111997-04-29 18:18:01 +00003588 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003589}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003590#define PyEval_CallObject(func,arg) \
3591 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003592
Guido van Rossumb209a111997-04-29 18:18:01 +00003593PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003594PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003595{
Jeremy Hylton52820442001-01-03 23:52:36 +00003596 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003597
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003598 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003599 arg = PyTuple_New(0);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003600 if (arg == NULL)
3601 return NULL;
3602 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003603 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003604 PyErr_SetString(PyExc_TypeError,
3605 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003606 return NULL;
3607 }
3608 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003609 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003610
Guido van Rossumb209a111997-04-29 18:18:01 +00003611 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003612 PyErr_SetString(PyExc_TypeError,
3613 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003614 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003615 return NULL;
3616 }
3617
Tim Peters6d6c1a32001-08-02 04:15:00 +00003618 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003619 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003620 return result;
3621}
3622
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003623const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003624PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003625{
3626 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003627 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003628 else if (PyFunction_Check(func))
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003629 return PyString_AsString(((PyFunctionObject*)func)->func_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00003630 else if (PyCFunction_Check(func))
3631 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3632 else if (PyClass_Check(func))
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003633 return PyString_AsString(((PyClassObject*)func)->cl_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00003634 else if (PyInstance_Check(func)) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003635 return PyString_AsString(
Jeremy Hylton512a2372001-04-11 13:52:29 +00003636 ((PyInstanceObject*)func)->in_class->cl_name);
3637 } else {
3638 return func->ob_type->tp_name;
3639 }
3640}
3641
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003642const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003643PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003644{
3645 if (PyMethod_Check(func))
3646 return "()";
3647 else if (PyFunction_Check(func))
3648 return "()";
3649 else if (PyCFunction_Check(func))
3650 return "()";
3651 else if (PyClass_Check(func))
3652 return " constructor";
3653 else if (PyInstance_Check(func)) {
3654 return " instance";
3655 } else {
3656 return " object";
3657 }
3658}
3659
Fredrik Lundh7a830892006-05-27 10:39:48 +00003660static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003661err_args(PyObject *func, int flags, int nargs)
3662{
3663 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003664 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003665 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003666 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003667 nargs);
3668 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003669 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003670 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003671 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003672 nargs);
3673}
3674
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003675#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003676if (tstate->use_tracing && tstate->c_profilefunc) { \
3677 if (call_trace(tstate->c_profilefunc, \
3678 tstate->c_profileobj, \
3679 tstate->frame, PyTrace_C_CALL, \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003680 func)) { \
3681 x = NULL; \
3682 } \
3683 else { \
3684 x = call; \
3685 if (tstate->c_profilefunc != NULL) { \
3686 if (x == NULL) { \
3687 call_trace_protected(tstate->c_profilefunc, \
3688 tstate->c_profileobj, \
3689 tstate->frame, PyTrace_C_EXCEPTION, \
3690 func); \
3691 /* XXX should pass (type, value, tb) */ \
3692 } else { \
3693 if (call_trace(tstate->c_profilefunc, \
3694 tstate->c_profileobj, \
3695 tstate->frame, PyTrace_C_RETURN, \
3696 func)) { \
3697 Py_DECREF(x); \
3698 x = NULL; \
3699 } \
3700 } \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003701 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003702 } \
3703} else { \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003704 x = call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003705 }
3706
Fredrik Lundh7a830892006-05-27 10:39:48 +00003707static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003708call_function(PyObject ***pp_stack, int oparg
3709#ifdef WITH_TSC
3710 , uint64* pintr0, uint64* pintr1
3711#endif
3712 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003713{
3714 int na = oparg & 0xff;
3715 int nk = (oparg>>8) & 0xff;
3716 int n = na + 2 * nk;
3717 PyObject **pfunc = (*pp_stack) - n - 1;
3718 PyObject *func = *pfunc;
3719 PyObject *x, *w;
3720
Jeremy Hylton985eba52003-02-05 23:13:00 +00003721 /* Always dispatch PyCFunction first, because these are
3722 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003723 */
3724 if (PyCFunction_Check(func) && nk == 0) {
3725 int flags = PyCFunction_GET_FLAGS(func);
Nicholas Bastind858a772004-06-25 23:31:06 +00003726 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003727
3728 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003729 if (flags & (METH_NOARGS | METH_O)) {
3730 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3731 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003732 if (flags & METH_NOARGS && na == 0) {
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003733 C_TRACE(x, (*meth)(self,NULL));
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003734 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003735 else if (flags & METH_O && na == 1) {
3736 PyObject *arg = EXT_POP(*pp_stack);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003737 C_TRACE(x, (*meth)(self,arg));
Jeremy Hylton192690e2002-08-16 18:36:11 +00003738 Py_DECREF(arg);
3739 }
3740 else {
3741 err_args(func, flags, na);
3742 x = NULL;
3743 }
3744 }
3745 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003746 PyObject *callargs;
3747 callargs = load_args(pp_stack, na);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003748 READ_TIMESTAMP(*pintr0);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003749 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003750 READ_TIMESTAMP(*pintr1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003751 Py_XDECREF(callargs);
3752 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003753 } else {
3754 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3755 /* optimize access to bound methods */
3756 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003757 PCALL(PCALL_METHOD);
3758 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003759 Py_INCREF(self);
3760 func = PyMethod_GET_FUNCTION(func);
3761 Py_INCREF(func);
3762 Py_DECREF(*pfunc);
3763 *pfunc = self;
3764 na++;
3765 n++;
3766 } else
3767 Py_INCREF(func);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003768 READ_TIMESTAMP(*pintr0);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003769 if (PyFunction_Check(func))
3770 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003771 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003772 x = do_call(func, pp_stack, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003773 READ_TIMESTAMP(*pintr1);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003774 Py_DECREF(func);
3775 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003776
Armin Rigod34fa522006-03-28 19:10:40 +00003777 /* Clear the stack of the function object. Also removes
3778 the arguments in case they weren't consumed already
3779 (fast_function() and err_args() leave them on the stack).
Thomas Wouters7f597322006-03-01 05:32:33 +00003780 */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003781 while ((*pp_stack) > pfunc) {
3782 w = EXT_POP(*pp_stack);
3783 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003784 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003785 }
3786 return x;
3787}
3788
Jeremy Hylton192690e2002-08-16 18:36:11 +00003789/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003790 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003791 For the simplest case -- a function that takes only positional
3792 arguments and is called with only positional arguments -- it
3793 inlines the most primitive frame setup code from
3794 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3795 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003796*/
3797
Fredrik Lundh7a830892006-05-27 10:39:48 +00003798static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003799fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003800{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003801 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003802 PyObject *globals = PyFunction_GET_GLOBALS(func);
3803 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3804 PyObject **d = NULL;
3805 int nd = 0;
3806
Jeremy Hylton985eba52003-02-05 23:13:00 +00003807 PCALL(PCALL_FUNCTION);
3808 PCALL(PCALL_FAST_FUNCTION);
Raymond Hettinger40174c32003-05-31 07:04:16 +00003809 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003810 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3811 PyFrameObject *f;
3812 PyObject *retval = NULL;
3813 PyThreadState *tstate = PyThreadState_GET();
3814 PyObject **fastlocals, **stack;
3815 int i;
3816
3817 PCALL(PCALL_FASTER_FUNCTION);
3818 assert(globals != NULL);
3819 /* XXX Perhaps we should create a specialized
3820 PyFrame_New() that doesn't take locals, but does
3821 take builtins without sanity checking them.
3822 */
Neal Norwitzdf6a6492006-08-13 18:10:10 +00003823 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003824 f = PyFrame_New(tstate, co, globals, NULL);
3825 if (f == NULL)
3826 return NULL;
3827
3828 fastlocals = f->f_localsplus;
3829 stack = (*pp_stack) - n;
3830
3831 for (i = 0; i < n; i++) {
3832 Py_INCREF(*stack);
3833 fastlocals[i] = *stack++;
3834 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003835 retval = PyEval_EvalFrameEx(f,0);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003836 ++tstate->recursion_depth;
3837 Py_DECREF(f);
3838 --tstate->recursion_depth;
3839 return retval;
3840 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003841 if (argdefs != NULL) {
3842 d = &PyTuple_GET_ITEM(argdefs, 0);
Christian Heimese93237d2007-12-19 02:37:44 +00003843 nd = Py_SIZE(argdefs);
Jeremy Hylton52820442001-01-03 23:52:36 +00003844 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003845 return PyEval_EvalCodeEx(co, globals,
3846 (PyObject *)NULL, (*pp_stack)-n, na,
3847 (*pp_stack)-2*nk, nk, d, nd,
3848 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003849}
3850
Fredrik Lundh7a830892006-05-27 10:39:48 +00003851static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003852update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3853 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003854{
3855 PyObject *kwdict = NULL;
3856 if (orig_kwdict == NULL)
3857 kwdict = PyDict_New();
3858 else {
3859 kwdict = PyDict_Copy(orig_kwdict);
3860 Py_DECREF(orig_kwdict);
3861 }
3862 if (kwdict == NULL)
3863 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003864 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003865 int err;
3866 PyObject *value = EXT_POP(*pp_stack);
3867 PyObject *key = EXT_POP(*pp_stack);
3868 if (PyDict_GetItem(kwdict, key) != NULL) {
Thomas Woutersae406c62007-09-19 17:27:43 +00003869 PyErr_Format(PyExc_TypeError,
3870 "%.200s%s got multiple values "
3871 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003872 PyEval_GetFuncName(func),
3873 PyEval_GetFuncDesc(func),
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003874 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003875 Py_DECREF(key);
3876 Py_DECREF(value);
3877 Py_DECREF(kwdict);
3878 return NULL;
3879 }
3880 err = PyDict_SetItem(kwdict, key, value);
3881 Py_DECREF(key);
3882 Py_DECREF(value);
3883 if (err) {
3884 Py_DECREF(kwdict);
3885 return NULL;
3886 }
3887 }
3888 return kwdict;
3889}
3890
Fredrik Lundh7a830892006-05-27 10:39:48 +00003891static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00003892update_star_args(int nstack, int nstar, PyObject *stararg,
3893 PyObject ***pp_stack)
3894{
3895 PyObject *callargs, *w;
3896
3897 callargs = PyTuple_New(nstack + nstar);
3898 if (callargs == NULL) {
3899 return NULL;
3900 }
3901 if (nstar) {
3902 int i;
3903 for (i = 0; i < nstar; i++) {
3904 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3905 Py_INCREF(a);
3906 PyTuple_SET_ITEM(callargs, nstack + i, a);
3907 }
3908 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003909 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003910 w = EXT_POP(*pp_stack);
3911 PyTuple_SET_ITEM(callargs, nstack, w);
3912 }
3913 return callargs;
3914}
3915
Fredrik Lundh7a830892006-05-27 10:39:48 +00003916static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00003917load_args(PyObject ***pp_stack, int na)
3918{
3919 PyObject *args = PyTuple_New(na);
3920 PyObject *w;
3921
3922 if (args == NULL)
3923 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003924 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003925 w = EXT_POP(*pp_stack);
3926 PyTuple_SET_ITEM(args, na, w);
3927 }
3928 return args;
3929}
3930
Fredrik Lundh7a830892006-05-27 10:39:48 +00003931static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00003932do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3933{
3934 PyObject *callargs = NULL;
3935 PyObject *kwdict = NULL;
3936 PyObject *result = NULL;
3937
3938 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003939 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003940 if (kwdict == NULL)
3941 goto call_fail;
3942 }
3943 callargs = load_args(pp_stack, na);
3944 if (callargs == NULL)
3945 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003946#ifdef CALL_PROFILE
3947 /* At this point, we have to look at the type of func to
3948 update the call stats properly. Do it here so as to avoid
3949 exposing the call stats machinery outside ceval.c
3950 */
3951 if (PyFunction_Check(func))
3952 PCALL(PCALL_FUNCTION);
3953 else if (PyMethod_Check(func))
3954 PCALL(PCALL_METHOD);
3955 else if (PyType_Check(func))
3956 PCALL(PCALL_TYPE);
Antoine Pitrou6987d542009-05-30 21:43:48 +00003957 else if (PyCFunction_Check(func))
3958 PCALL(PCALL_CFUNCTION);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003959 else
3960 PCALL(PCALL_OTHER);
3961#endif
Antoine Pitrou6987d542009-05-30 21:43:48 +00003962 if (PyCFunction_Check(func)) {
3963 PyThreadState *tstate = PyThreadState_GET();
3964 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
3965 }
3966 else
3967 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003968 call_fail:
3969 Py_XDECREF(callargs);
3970 Py_XDECREF(kwdict);
3971 return result;
3972}
3973
Fredrik Lundh7a830892006-05-27 10:39:48 +00003974static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00003975ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3976{
3977 int nstar = 0;
3978 PyObject *callargs = NULL;
3979 PyObject *stararg = NULL;
3980 PyObject *kwdict = NULL;
3981 PyObject *result = NULL;
3982
3983 if (flags & CALL_FLAG_KW) {
3984 kwdict = EXT_POP(*pp_stack);
Georg Brandl2134e752007-05-21 20:34:16 +00003985 if (!PyDict_Check(kwdict)) {
3986 PyObject *d;
3987 d = PyDict_New();
3988 if (d == NULL)
3989 goto ext_call_fail;
3990 if (PyDict_Update(d, kwdict) != 0) {
3991 Py_DECREF(d);
3992 /* PyDict_Update raises attribute
3993 * error (percolated from an attempt
3994 * to get 'keys' attribute) instead of
3995 * a type error if its second argument
3996 * is not a mapping.
3997 */
3998 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
3999 PyErr_Format(PyExc_TypeError,
4000 "%.200s%.200s argument after ** "
4001 "must be a mapping, not %.200s",
4002 PyEval_GetFuncName(func),
4003 PyEval_GetFuncDesc(func),
4004 kwdict->ob_type->tp_name);
4005 }
4006 goto ext_call_fail;
4007 }
4008 Py_DECREF(kwdict);
4009 kwdict = d;
Jeremy Hylton52820442001-01-03 23:52:36 +00004010 }
4011 }
4012 if (flags & CALL_FLAG_VAR) {
4013 stararg = EXT_POP(*pp_stack);
4014 if (!PyTuple_Check(stararg)) {
4015 PyObject *t = NULL;
4016 t = PySequence_Tuple(stararg);
4017 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00004018 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4019 PyErr_Format(PyExc_TypeError,
Georg Brandl2134e752007-05-21 20:34:16 +00004020 "%.200s%.200s argument after * "
4021 "must be a sequence, not %200s",
Tim Peters6d6c1a32001-08-02 04:15:00 +00004022 PyEval_GetFuncName(func),
Georg Brandl2134e752007-05-21 20:34:16 +00004023 PyEval_GetFuncDesc(func),
4024 stararg->ob_type->tp_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00004025 }
Jeremy Hylton52820442001-01-03 23:52:36 +00004026 goto ext_call_fail;
4027 }
4028 Py_DECREF(stararg);
4029 stararg = t;
4030 }
4031 nstar = PyTuple_GET_SIZE(stararg);
4032 }
4033 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00004034 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00004035 if (kwdict == NULL)
4036 goto ext_call_fail;
4037 }
4038 callargs = update_star_args(na, nstar, stararg, pp_stack);
4039 if (callargs == NULL)
4040 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004041#ifdef CALL_PROFILE
4042 /* At this point, we have to look at the type of func to
4043 update the call stats properly. Do it here so as to avoid
4044 exposing the call stats machinery outside ceval.c
4045 */
4046 if (PyFunction_Check(func))
4047 PCALL(PCALL_FUNCTION);
4048 else if (PyMethod_Check(func))
4049 PCALL(PCALL_METHOD);
4050 else if (PyType_Check(func))
4051 PCALL(PCALL_TYPE);
Antoine Pitrou6987d542009-05-30 21:43:48 +00004052 else if (PyCFunction_Check(func))
4053 PCALL(PCALL_CFUNCTION);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004054 else
4055 PCALL(PCALL_OTHER);
4056#endif
Antoine Pitrou6987d542009-05-30 21:43:48 +00004057 if (PyCFunction_Check(func)) {
4058 PyThreadState *tstate = PyThreadState_GET();
4059 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4060 }
4061 else
4062 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersae406c62007-09-19 17:27:43 +00004063ext_call_fail:
Jeremy Hylton52820442001-01-03 23:52:36 +00004064 Py_XDECREF(callargs);
4065 Py_XDECREF(kwdict);
4066 Py_XDECREF(stararg);
4067 return result;
4068}
4069
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004070/* Extract a slice index from a PyInt or PyLong or an object with the
4071 nb_index slot defined, and store in *pi.
4072 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4073 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 +00004074 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004075*/
Tim Petersb5196382001-12-16 19:44:20 +00004076/* Note: If v is NULL, return success without storing into *pi. This
4077 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4078 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004079*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004080int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004081_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004082{
Tim Petersb5196382001-12-16 19:44:20 +00004083 if (v != NULL) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004084 Py_ssize_t x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00004085 if (PyInt_Check(v)) {
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00004086 /* XXX(nnorwitz): I think PyInt_AS_LONG is correct,
4087 however, it looks like it should be AsSsize_t.
4088 There should be a comment here explaining why.
4089 */
4090 x = PyInt_AS_LONG(v);
Tim Peters7df5e7f2006-05-26 23:14:37 +00004091 }
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00004092 else if (PyIndex_Check(v)) {
4093 x = PyNumber_AsSsize_t(v, NULL);
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004094 if (x == -1 && PyErr_Occurred())
4095 return 0;
4096 }
4097 else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00004098 PyErr_SetString(PyExc_TypeError,
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004099 "slice indices must be integers or "
4100 "None or have an __index__ method");
Guido van Rossum20c6add2000-05-08 14:06:50 +00004101 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004102 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00004103 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004104 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00004105 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004106}
4107
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004108#undef ISINDEX
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00004109#define ISINDEX(x) ((x) == NULL || \
4110 PyInt_Check(x) || PyLong_Check(x) || PyIndex_Check(x))
Guido van Rossum50d756e2001-08-18 17:43:36 +00004111
Fredrik Lundh7a830892006-05-27 10:39:48 +00004112static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004113apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004114{
Guido van Rossum50d756e2001-08-18 17:43:36 +00004115 PyTypeObject *tp = u->ob_type;
4116 PySequenceMethods *sq = tp->tp_as_sequence;
4117
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004118 if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004119 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
Guido van Rossum50d756e2001-08-18 17:43:36 +00004120 if (!_PyEval_SliceIndex(v, &ilow))
4121 return NULL;
4122 if (!_PyEval_SliceIndex(w, &ihigh))
4123 return NULL;
4124 return PySequence_GetSlice(u, ilow, ihigh);
4125 }
4126 else {
4127 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00004128 if (slice != NULL) {
4129 PyObject *res = PyObject_GetItem(u, slice);
4130 Py_DECREF(slice);
4131 return res;
4132 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00004133 else
4134 return NULL;
4135 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004136}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004137
Fredrik Lundh7a830892006-05-27 10:39:48 +00004138static int
Guido van Rossumac7be682001-01-17 15:42:30 +00004139assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
4140 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004141{
Guido van Rossum50d756e2001-08-18 17:43:36 +00004142 PyTypeObject *tp = u->ob_type;
4143 PySequenceMethods *sq = tp->tp_as_sequence;
4144
Georg Brandl0fca97a2007-03-05 22:28:08 +00004145 if (sq && sq->sq_ass_slice && ISINDEX(v) && ISINDEX(w)) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004146 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
Guido van Rossum50d756e2001-08-18 17:43:36 +00004147 if (!_PyEval_SliceIndex(v, &ilow))
4148 return -1;
4149 if (!_PyEval_SliceIndex(w, &ihigh))
4150 return -1;
4151 if (x == NULL)
4152 return PySequence_DelSlice(u, ilow, ihigh);
4153 else
4154 return PySequence_SetSlice(u, ilow, ihigh, x);
4155 }
4156 else {
4157 PyObject *slice = PySlice_New(v, w, NULL);
4158 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00004159 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00004160 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00004161 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00004162 else
Guido van Rossum354797c2001-12-03 19:45:06 +00004163 res = PyObject_DelItem(u, slice);
4164 Py_DECREF(slice);
4165 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00004166 }
4167 else
4168 return -1;
4169 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004170}
4171
Guido van Rossum04edb522008-03-18 02:49:46 +00004172#define Py3kExceptionClass_Check(x) \
4173 (PyType_Check((x)) && \
4174 PyType_FastSubclass((PyTypeObject*)(x), Py_TPFLAGS_BASE_EXC_SUBCLASS))
4175
4176#define CANNOT_CATCH_MSG "catching classes that don't inherit from " \
Georg Brandld5b635f2008-03-25 08:29:14 +00004177 "BaseException is not allowed in 3.x"
Guido van Rossum04edb522008-03-18 02:49:46 +00004178
Fredrik Lundh7a830892006-05-27 10:39:48 +00004179static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004180cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004181{
Guido van Rossumac7be682001-01-17 15:42:30 +00004182 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004183 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00004184 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00004185 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004186 break;
4187 case PyCmp_IS_NOT:
4188 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004189 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004190 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004191 res = PySequence_Contains(w, v);
4192 if (res < 0)
4193 return NULL;
4194 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004195 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00004196 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004197 if (res < 0)
4198 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004199 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004200 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004201 case PyCmp_EXC_MATCH:
Brett Cannon129bd522007-01-30 21:34:36 +00004202 if (PyTuple_Check(w)) {
4203 Py_ssize_t i, length;
4204 length = PyTuple_Size(w);
4205 for (i = 0; i < length; i += 1) {
4206 PyObject *exc = PyTuple_GET_ITEM(w, i);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004207 if (PyString_Check(exc)) {
Brett Cannon129bd522007-01-30 21:34:36 +00004208 int ret_val;
4209 ret_val = PyErr_WarnEx(
Thomas Wouterse2176022007-09-20 17:35:10 +00004210 PyExc_DeprecationWarning,
4211 "catching of string "
4212 "exceptions is deprecated", 1);
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00004213 if (ret_val < 0)
Brett Cannon129bd522007-01-30 21:34:36 +00004214 return NULL;
4215 }
Guido van Rossum20bda582008-03-18 03:15:05 +00004216 else if (Py_Py3kWarningFlag &&
4217 !PyTuple_Check(exc) &&
4218 !Py3kExceptionClass_Check(exc))
Guido van Rossum04edb522008-03-18 02:49:46 +00004219 {
4220 int ret_val;
4221 ret_val = PyErr_WarnEx(
4222 PyExc_DeprecationWarning,
4223 CANNOT_CATCH_MSG, 1);
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00004224 if (ret_val < 0)
Guido van Rossum04edb522008-03-18 02:49:46 +00004225 return NULL;
4226 }
Brett Cannon129bd522007-01-30 21:34:36 +00004227 }
4228 }
4229 else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004230 if (PyString_Check(w)) {
Brett Cannon129bd522007-01-30 21:34:36 +00004231 int ret_val;
4232 ret_val = PyErr_WarnEx(
4233 PyExc_DeprecationWarning,
4234 "catching of string "
Thomas Wouterse2176022007-09-20 17:35:10 +00004235 "exceptions is deprecated", 1);
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00004236 if (ret_val < 0)
Brett Cannon129bd522007-01-30 21:34:36 +00004237 return NULL;
4238 }
Guido van Rossum20bda582008-03-18 03:15:05 +00004239 else if (Py_Py3kWarningFlag &&
4240 !PyTuple_Check(w) &&
4241 !Py3kExceptionClass_Check(w))
Guido van Rossum04edb522008-03-18 02:49:46 +00004242 {
4243 int ret_val;
4244 ret_val = PyErr_WarnEx(
4245 PyExc_DeprecationWarning,
4246 CANNOT_CATCH_MSG, 1);
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00004247 if (ret_val < 0)
Guido van Rossum04edb522008-03-18 02:49:46 +00004248 return NULL;
4249 }
Brett Cannon129bd522007-01-30 21:34:36 +00004250 }
Barry Warsaw4249f541997-08-22 21:26:19 +00004251 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004252 break;
4253 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00004254 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004255 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004256 v = res ? Py_True : Py_False;
4257 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004258 return v;
4259}
4260
Fredrik Lundh7a830892006-05-27 10:39:48 +00004261static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00004262import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004263{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004264 PyObject *x;
4265
4266 x = PyObject_GetAttr(v, name);
4267 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00004268 PyErr_Format(PyExc_ImportError,
4269 "cannot import name %.230s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004270 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004271 }
Thomas Wouters52152252000-08-17 22:55:00 +00004272 return x;
4273}
Guido van Rossumac7be682001-01-17 15:42:30 +00004274
Fredrik Lundh7a830892006-05-27 10:39:48 +00004275static int
Thomas Wouters52152252000-08-17 22:55:00 +00004276import_all_from(PyObject *locals, PyObject *v)
4277{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004278 PyObject *all = PyObject_GetAttrString(v, "__all__");
4279 PyObject *dict, *name, *value;
4280 int skip_leading_underscores = 0;
4281 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004282
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004283 if (all == NULL) {
4284 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4285 return -1; /* Unexpected error */
4286 PyErr_Clear();
4287 dict = PyObject_GetAttrString(v, "__dict__");
4288 if (dict == NULL) {
4289 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4290 return -1;
4291 PyErr_SetString(PyExc_ImportError,
4292 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00004293 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004294 }
4295 all = PyMapping_Keys(dict);
4296 Py_DECREF(dict);
4297 if (all == NULL)
4298 return -1;
4299 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004300 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004301
4302 for (pos = 0, err = 0; ; pos++) {
4303 name = PySequence_GetItem(all, pos);
4304 if (name == NULL) {
4305 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4306 err = -1;
4307 else
4308 PyErr_Clear();
4309 break;
4310 }
4311 if (skip_leading_underscores &&
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004312 PyString_Check(name) &&
4313 PyString_AS_STRING(name)[0] == '_')
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004314 {
4315 Py_DECREF(name);
4316 continue;
4317 }
4318 value = PyObject_GetAttr(v, name);
4319 if (value == NULL)
4320 err = -1;
Armin Rigo70370852006-11-29 21:59:22 +00004321 else if (PyDict_CheckExact(locals))
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004322 err = PyDict_SetItem(locals, name, value);
Armin Rigo70370852006-11-29 21:59:22 +00004323 else
4324 err = PyObject_SetItem(locals, name, value);
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004325 Py_DECREF(name);
4326 Py_XDECREF(value);
4327 if (err != 0)
4328 break;
4329 }
4330 Py_DECREF(all);
4331 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004332}
4333
Fredrik Lundh7a830892006-05-27 10:39:48 +00004334static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004335build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004336{
Guido van Rossum7851eea2001-09-12 19:19:18 +00004337 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004338
4339 if (PyDict_Check(methods))
4340 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00004341 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00004342 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004343 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4344 base = PyTuple_GET_ITEM(bases, 0);
4345 metaclass = PyObject_GetAttrString(base, "__class__");
4346 if (metaclass == NULL) {
4347 PyErr_Clear();
4348 metaclass = (PyObject *)base->ob_type;
4349 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00004350 }
4351 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004352 else {
4353 PyObject *g = PyEval_GetGlobals();
4354 if (g != NULL && PyDict_Check(g))
4355 metaclass = PyDict_GetItemString(g, "__metaclass__");
4356 if (metaclass == NULL)
4357 metaclass = (PyObject *) &PyClass_Type;
4358 Py_INCREF(metaclass);
4359 }
Jeremy Hylton7c1e3472007-02-26 16:14:51 +00004360 result = PyObject_CallFunctionObjArgs(metaclass, name, bases, methods,
Thomas Woutersae406c62007-09-19 17:27:43 +00004361 NULL);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004362 Py_DECREF(metaclass);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004363 if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
Tim Peters7df5e7f2006-05-26 23:14:37 +00004364 /* A type error here likely means that the user passed
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004365 in a base that was not a class (such the random module
4366 instead of the random.random type). Help them out with
Raymond Hettingercfc31922004-09-16 16:41:57 +00004367 by augmenting the error message with more information.*/
4368
4369 PyObject *ptype, *pvalue, *ptraceback;
4370
4371 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004372 if (PyString_Check(pvalue)) {
Raymond Hettingercfc31922004-09-16 16:41:57 +00004373 PyObject *newmsg;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004374 newmsg = PyString_FromFormat(
Jeremy Hylton7c1e3472007-02-26 16:14:51 +00004375 "Error when calling the metaclass bases\n"
Thomas Woutersae406c62007-09-19 17:27:43 +00004376 " %s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004377 PyString_AS_STRING(pvalue));
Raymond Hettingercfc31922004-09-16 16:41:57 +00004378 if (newmsg != NULL) {
4379 Py_DECREF(pvalue);
4380 pvalue = newmsg;
4381 }
4382 }
4383 PyErr_Restore(ptype, pvalue, ptraceback);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004384 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004385 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00004386}
4387
Fredrik Lundh7a830892006-05-27 10:39:48 +00004388static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004389exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
4390 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004391{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004392 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00004393 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004394 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004395
Guido van Rossumb209a111997-04-29 18:18:01 +00004396 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
4397 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004398 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00004399 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004400 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00004401 locals = PyTuple_GetItem(prog, 2);
4402 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004403 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004404 if (globals == Py_None) {
4405 globals = PyEval_GetGlobals();
4406 if (locals == Py_None) {
4407 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00004408 plain = 1;
4409 }
Neal Norwitzdf6a6492006-08-13 18:10:10 +00004410 if (!globals || !locals) {
4411 PyErr_SetString(PyExc_SystemError,
4412 "globals and locals cannot be NULL");
4413 return -1;
4414 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004415 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004416 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004417 locals = globals;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004418 if (!PyString_Check(prog) &&
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004419 !PyUnicode_Check(prog) &&
Guido van Rossumb209a111997-04-29 18:18:01 +00004420 !PyCode_Check(prog) &&
4421 !PyFile_Check(prog)) {
4422 PyErr_SetString(PyExc_TypeError,
Guido van Rossumac7be682001-01-17 15:42:30 +00004423 "exec: arg 1 must be a string, file, or code object");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004424 return -1;
4425 }
Fred Drake661ea262000-10-24 19:57:45 +00004426 if (!PyDict_Check(globals)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004427 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00004428 "exec: arg 2 must be a dictionary or None");
4429 return -1;
4430 }
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004431 if (!PyMapping_Check(locals)) {
Fred Drake661ea262000-10-24 19:57:45 +00004432 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004433 "exec: arg 3 must be a mapping or None");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004434 return -1;
4435 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004436 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00004437 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00004438 if (PyCode_Check(prog)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +00004439 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4440 PyErr_SetString(PyExc_TypeError,
4441 "code object passed to exec may not contain free variables");
4442 return -1;
4443 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004444 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004445 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004446 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004447 FILE *fp = PyFile_AsFile(prog);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004448 char *name = PyString_AsString(PyFile_Name(prog));
Jeremy Hylton714b1122007-02-25 16:01:58 +00004449 PyCompilerFlags cf;
Thomas Woutersae406c62007-09-19 17:27:43 +00004450 if (name == NULL)
4451 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004452 cf.cf_flags = 0;
4453 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004454 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004455 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004456 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004457 v = PyRun_File(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004458 locals);
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004459 }
4460 else {
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004461 PyObject *tmp = NULL;
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004462 char *str;
Tim Peters5ba58662001-07-16 02:29:45 +00004463 PyCompilerFlags cf;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004464 cf.cf_flags = 0;
4465#ifdef Py_USING_UNICODE
4466 if (PyUnicode_Check(prog)) {
4467 tmp = PyUnicode_AsUTF8String(prog);
4468 if (tmp == NULL)
4469 return -1;
4470 prog = tmp;
4471 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4472 }
4473#endif
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004474 if (PyString_AsStringAndSize(prog, &str, NULL))
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004475 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004476 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters8a5c3c72004-04-05 19:36:21 +00004477 v = PyRun_StringFlags(str, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004478 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004479 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004480 v = PyRun_String(str, Py_file_input, globals, locals);
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004481 Py_XDECREF(tmp);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004482 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004483 if (plain)
4484 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004485 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004486 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00004487 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004488 return 0;
4489}
Guido van Rossum24c13741995-02-14 09:42:43 +00004490
Fredrik Lundh7a830892006-05-27 10:39:48 +00004491static void
Paul Prescode68140d2000-08-30 20:25:01 +00004492format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4493{
4494 char *obj_str;
4495
4496 if (!obj)
4497 return;
4498
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004499 obj_str = PyString_AsString(obj);
Paul Prescode68140d2000-08-30 20:25:01 +00004500 if (!obj_str)
4501 return;
4502
4503 PyErr_Format(exc, format_str, obj_str);
4504}
Guido van Rossum950361c1997-01-24 13:49:28 +00004505
Fredrik Lundh7a830892006-05-27 10:39:48 +00004506static PyObject *
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004507string_concatenate(PyObject *v, PyObject *w,
4508 PyFrameObject *f, unsigned char *next_instr)
4509{
4510 /* This function implements 'variable += expr' when both arguments
4511 are strings. */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004512 Py_ssize_t v_len = PyString_GET_SIZE(v);
4513 Py_ssize_t w_len = PyString_GET_SIZE(w);
Armin Rigo97ff0472006-08-09 15:37:26 +00004514 Py_ssize_t new_len = v_len + w_len;
4515 if (new_len < 0) {
4516 PyErr_SetString(PyExc_OverflowError,
4517 "strings are too large to concat");
4518 return NULL;
4519 }
Tim Peters7df5e7f2006-05-26 23:14:37 +00004520
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004521 if (v->ob_refcnt == 2) {
4522 /* In the common case, there are 2 references to the value
4523 * stored in 'variable' when the += is performed: one on the
Thomas Wouterse2176022007-09-20 17:35:10 +00004524 * value stack (in 'v') and one still stored in the
4525 * 'variable'. We try to delete the variable now to reduce
4526 * the refcnt to 1.
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004527 */
4528 switch (*next_instr) {
4529 case STORE_FAST:
4530 {
4531 int oparg = PEEKARG();
4532 PyObject **fastlocals = f->f_localsplus;
4533 if (GETLOCAL(oparg) == v)
4534 SETLOCAL(oparg, NULL);
4535 break;
4536 }
4537 case STORE_DEREF:
4538 {
Thomas Wouterse2176022007-09-20 17:35:10 +00004539 PyObject **freevars = (f->f_localsplus +
4540 f->f_code->co_nlocals);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004541 PyObject *c = freevars[PEEKARG()];
4542 if (PyCell_GET(c) == v)
4543 PyCell_Set(c, NULL);
4544 break;
4545 }
4546 case STORE_NAME:
4547 {
4548 PyObject *names = f->f_code->co_names;
4549 PyObject *name = GETITEM(names, PEEKARG());
4550 PyObject *locals = f->f_locals;
4551 if (PyDict_CheckExact(locals) &&
4552 PyDict_GetItem(locals, name) == v) {
4553 if (PyDict_DelItem(locals, name) != 0) {
4554 PyErr_Clear();
4555 }
4556 }
4557 break;
4558 }
4559 }
4560 }
4561
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004562 if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004563 /* Now we own the last reference to 'v', so we can resize it
4564 * in-place.
4565 */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004566 if (_PyString_Resize(&v, new_len) != 0) {
4567 /* XXX if _PyString_Resize() fails, 'v' has been
Thomas Wouterse2176022007-09-20 17:35:10 +00004568 * deallocated so it cannot be put back into
4569 * 'variable'. The MemoryError is raised when there
4570 * is no value in 'variable', which might (very
4571 * remotely) be a cause of incompatibilities.
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004572 */
4573 return NULL;
4574 }
4575 /* copy 'w' into the newly allocated area of 'v' */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004576 memcpy(PyString_AS_STRING(v) + v_len,
4577 PyString_AS_STRING(w), w_len);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004578 return v;
4579 }
4580 else {
4581 /* When in-place resizing is not an option. */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004582 PyString_Concat(&v, w);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004583 return v;
4584 }
4585}
4586
Guido van Rossum950361c1997-01-24 13:49:28 +00004587#ifdef DYNAMIC_EXECUTION_PROFILE
4588
Fredrik Lundh7a830892006-05-27 10:39:48 +00004589static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004590getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004591{
4592 int i;
4593 PyObject *l = PyList_New(256);
4594 if (l == NULL) return NULL;
4595 for (i = 0; i < 256; i++) {
4596 PyObject *x = PyInt_FromLong(a[i]);
4597 if (x == NULL) {
4598 Py_DECREF(l);
4599 return NULL;
4600 }
4601 PyList_SetItem(l, i, x);
4602 }
4603 for (i = 0; i < 256; i++)
4604 a[i] = 0;
4605 return l;
4606}
4607
4608PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004609_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004610{
4611#ifndef DXPAIRS
4612 return getarray(dxp);
4613#else
4614 int i;
4615 PyObject *l = PyList_New(257);
4616 if (l == NULL) return NULL;
4617 for (i = 0; i < 257; i++) {
4618 PyObject *x = getarray(dxpairs[i]);
4619 if (x == NULL) {
4620 Py_DECREF(l);
4621 return NULL;
4622 }
4623 PyList_SetItem(l, i, x);
4624 }
4625 return l;
4626#endif
4627}
4628
4629#endif