blob: 88483a5f36b72126e902b23fc386a2ce21c43fd6 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003
Guido van Rossum681d79a1995-07-18 14:51:37 +00004/* XXX TO DO:
Guido van Rossum681d79a1995-07-18 14:51:37 +00005 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +00006 XXX document it!
7 */
8
Fredrik Lundh7a830892006-05-27 10:39:48 +00009/* enable more aggressive intra-module optimizations, where available */
Fredrik Lundh57640f52006-05-26 11:54:04 +000010#define PY_LOCAL_AGGRESSIVE
11
Guido van Rossumb209a111997-04-29 18:18:01 +000012#include "Python.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000013
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000014#include "code.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000015#include "frameobject.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000016#include "eval.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000017#include "opcode.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000018#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000019
Guido van Rossumc6004111993-11-05 10:22:19 +000020#include <ctype.h>
21
Tim Peters7df5e7f2006-05-26 23:14:37 +000022#ifndef WITH_TSC
Michael W. Hudson75eabd22005-01-18 15:56:11 +000023
24#define READ_TIMESTAMP(var)
25
26#else
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000027
28typedef unsigned long long uint64;
29
Michael W. Hudson800ba232004-08-12 18:19:17 +000030#if defined(__ppc__) /* <- Don't know if this is the correct symbol; this
Thomas Wouterse2176022007-09-20 17:35:10 +000031 section should work for GCC on any PowerPC
32 platform, irrespective of OS.
33 POWER? Who knows :-) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000034
Michael W. Hudson75eabd22005-01-18 15:56:11 +000035#define READ_TIMESTAMP(var) ppc_getcounter(&var)
Michael W. Hudson800ba232004-08-12 18:19:17 +000036
Fredrik Lundh7a830892006-05-27 10:39:48 +000037static void
Michael W. Hudson800ba232004-08-12 18:19:17 +000038ppc_getcounter(uint64 *v)
39{
40 register unsigned long tbu, tb, tbu2;
41
42 loop:
43 asm volatile ("mftbu %0" : "=r" (tbu) );
44 asm volatile ("mftb %0" : "=r" (tb) );
45 asm volatile ("mftbu %0" : "=r" (tbu2));
46 if (__builtin_expect(tbu != tbu2, 0)) goto loop;
47
Tim Peters7df5e7f2006-05-26 23:14:37 +000048 /* The slightly peculiar way of writing the next lines is
Michael W. Hudson800ba232004-08-12 18:19:17 +000049 compiled better by GCC than any other way I tried. */
50 ((long*)(v))[0] = tbu;
51 ((long*)(v))[1] = tb;
52}
53
Michael W. Hudson75eabd22005-01-18 15:56:11 +000054#else /* this is for linux/x86 (and probably any other GCC/x86 combo) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000055
Michael W. Hudson75eabd22005-01-18 15:56:11 +000056#define READ_TIMESTAMP(val) \
57 __asm__ __volatile__("rdtsc" : "=A" (val))
Michael W. Hudson800ba232004-08-12 18:19:17 +000058
59#endif
60
Tim Peters7df5e7f2006-05-26 23:14:37 +000061void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000062 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
63{
64 uint64 intr, inst, loop;
65 PyThreadState *tstate = PyThreadState_Get();
66 if (!tstate->interp->tscdump)
67 return;
68 intr = intr1 - intr0;
69 inst = inst1 - inst0 - intr;
70 loop = loop1 - loop0 - intr;
71 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
72 opcode, ticked, inst, loop);
73}
Michael W. Hudson800ba232004-08-12 18:19:17 +000074
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000075#endif
76
Guido van Rossum04691fc1992-08-12 15:35:34 +000077/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000078/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000079
Guido van Rossum408027e1996-12-30 16:17:54 +000080#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000081/* For debugging the interpreter: */
82#define LLTRACE 1 /* Low-level trace feature */
83#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000084#endif
85
Jeremy Hylton52820442001-01-03 23:52:36 +000086typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000087
Guido van Rossum374a9221991-04-04 10:40:29 +000088/* Forward declarations */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000089#ifdef WITH_TSC
Fredrik Lundh7a830892006-05-27 10:39:48 +000090static PyObject * call_function(PyObject ***, int, uint64*, uint64*);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000091#else
Fredrik Lundh7a830892006-05-27 10:39:48 +000092static PyObject * call_function(PyObject ***, int);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000093#endif
Fredrik Lundh7a830892006-05-27 10:39:48 +000094static PyObject * fast_function(PyObject *, PyObject ***, int, int, int);
95static PyObject * do_call(PyObject *, PyObject ***, int, int);
96static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int);
Thomas Wouterse2176022007-09-20 17:35:10 +000097static PyObject * update_keyword_args(PyObject *, int, PyObject ***,
98 PyObject *);
Fredrik Lundh7a830892006-05-27 10:39:48 +000099static PyObject * update_star_args(int, int, PyObject *, PyObject ***);
100static PyObject * load_args(PyObject ***, int);
Jeremy Hylton52820442001-01-03 23:52:36 +0000101#define CALL_FLAG_VAR 1
102#define CALL_FLAG_KW 2
103
Guido van Rossum0a066c01992-03-27 17:29:15 +0000104#ifdef LLTRACE
Fredrik Lundh1b949402006-05-26 12:01:49 +0000105static int lltrace;
Fredrik Lundh7a830892006-05-27 10:39:48 +0000106static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +0000107#endif
Fredrik Lundh7a830892006-05-27 10:39:48 +0000108static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
Fred Drake5755ce62001-06-27 19:19:46 +0000109 int, PyObject *);
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +0000110static int call_trace_protected(Py_tracefunc, PyObject *,
Armin Rigo1c2d7e52005-09-20 18:34:01 +0000111 PyFrameObject *, int, PyObject *);
Fredrik Lundh7a830892006-05-27 10:39:48 +0000112static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
113static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Armin Rigobf57a142004-03-22 19:24:58 +0000114 PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000115
Fredrik Lundh7a830892006-05-27 10:39:48 +0000116static PyObject * apply_slice(PyObject *, PyObject *, PyObject *);
117static int assign_slice(PyObject *, PyObject *,
Tim Petersdbd9ba62000-07-09 03:09:57 +0000118 PyObject *, PyObject *);
Fredrik Lundh7a830892006-05-27 10:39:48 +0000119static PyObject * cmp_outcome(int, PyObject *, PyObject *);
120static PyObject * import_from(PyObject *, PyObject *);
121static int import_all_from(PyObject *, PyObject *);
122static PyObject * build_class(PyObject *, PyObject *, PyObject *);
123static int exec_statement(PyFrameObject *,
Tim Petersdbd9ba62000-07-09 03:09:57 +0000124 PyObject *, PyObject *, PyObject *);
Fredrik Lundh7a830892006-05-27 10:39:48 +0000125static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *);
126static void reset_exc_info(PyThreadState *);
127static void format_exc_check_arg(PyObject *, char *, PyObject *);
128static PyObject * string_concatenate(PyObject *, PyObject *,
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000129 PyFrameObject *, unsigned char *);
Guido van Rossum374a9221991-04-04 10:40:29 +0000130
Paul Prescode68140d2000-08-30 20:25:01 +0000131#define NAME_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000132 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000133#define GLOBAL_NAME_ERROR_MSG \
134 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000135#define UNBOUNDLOCAL_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000136 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000137#define UNBOUNDFREE_ERROR_MSG \
138 "free variable '%.200s' referenced before assignment" \
139 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000140
Guido van Rossum950361c1997-01-24 13:49:28 +0000141/* Dynamic execution profile */
142#ifdef DYNAMIC_EXECUTION_PROFILE
143#ifdef DXPAIRS
144static long dxpairs[257][256];
145#define dxp dxpairs[256]
146#else
147static long dxp[256];
148#endif
149#endif
150
Jeremy Hylton985eba52003-02-05 23:13:00 +0000151/* Function call profile */
152#ifdef CALL_PROFILE
153#define PCALL_NUM 11
154static int pcall[PCALL_NUM];
155
156#define PCALL_ALL 0
157#define PCALL_FUNCTION 1
158#define PCALL_FAST_FUNCTION 2
159#define PCALL_FASTER_FUNCTION 3
160#define PCALL_METHOD 4
161#define PCALL_BOUND_METHOD 5
162#define PCALL_CFUNCTION 6
163#define PCALL_TYPE 7
164#define PCALL_GENERATOR 8
165#define PCALL_OTHER 9
166#define PCALL_POP 10
167
168/* Notes about the statistics
169
170 PCALL_FAST stats
171
172 FAST_FUNCTION means no argument tuple needs to be created.
173 FASTER_FUNCTION means that the fast-path frame setup code is used.
174
175 If there is a method call where the call can be optimized by changing
176 the argument tuple and calling the function directly, it gets recorded
177 twice.
178
179 As a result, the relationship among the statistics appears to be
180 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
181 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
182 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
183 PCALL_METHOD > PCALL_BOUND_METHOD
184*/
185
186#define PCALL(POS) pcall[POS]++
187
188PyObject *
189PyEval_GetCallStats(PyObject *self)
190{
Andrew M. Kuchling1f3ebe02006-10-27 13:22:46 +0000191 return Py_BuildValue("iiiiiiiiiii",
Jeremy Hylton985eba52003-02-05 23:13:00 +0000192 pcall[0], pcall[1], pcall[2], pcall[3],
193 pcall[4], pcall[5], pcall[6], pcall[7],
Andrew M. Kuchling1f3ebe02006-10-27 13:22:46 +0000194 pcall[8], pcall[9], pcall[10]);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000195}
196#else
197#define PCALL(O)
198
199PyObject *
200PyEval_GetCallStats(PyObject *self)
201{
202 Py_INCREF(Py_None);
203 return Py_None;
204}
205#endif
206
Tim Peters5ca576e2001-06-18 22:08:13 +0000207
Guido van Rossume59214e1994-08-30 08:01:59 +0000208#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000209
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000210#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000211#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000212#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000213#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000214
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000215static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000216static PyThread_type_lock pending_lock = 0; /* for pending calls */
Guido van Rossuma9672091994-09-14 13:31:22 +0000217static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000218
Tim Peters7f468f22004-10-11 02:40:51 +0000219int
220PyEval_ThreadsInitialized(void)
221{
222 return interpreter_lock != 0;
223}
224
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000225void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000226PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000227{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000228 if (interpreter_lock)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000229 return;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000230 interpreter_lock = PyThread_allocate_lock();
231 PyThread_acquire_lock(interpreter_lock, 1);
232 main_thread = PyThread_get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000233}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000234
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000235void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000236PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000237{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000238 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000239}
240
241void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000242PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000243{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000244 PyThread_release_lock(interpreter_lock);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000245}
246
247void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000248PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000249{
250 if (tstate == NULL)
251 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000252 /* Check someone has called PyEval_InitThreads() to create the lock */
253 assert(interpreter_lock);
Guido van Rossum65d5b571998-12-21 19:32:43 +0000254 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000255 if (PyThreadState_Swap(tstate) != NULL)
256 Py_FatalError(
257 "PyEval_AcquireThread: non-NULL old thread state");
258}
259
260void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000261PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000262{
263 if (tstate == NULL)
264 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
265 if (PyThreadState_Swap(NULL) != tstate)
266 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000267 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000268}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000269
270/* This function is called from PyOS_AfterFork to ensure that newly
271 created child processes don't hold locks referring to threads which
272 are not running in the child process. (This could also be done using
273 pthread_atfork mechanism, at least for the pthreads implementation.) */
274
275void
276PyEval_ReInitThreads(void)
277{
Jesse Noller5e62ca42008-07-16 20:03:47 +0000278 PyObject *threading, *result;
279 PyThreadState *tstate;
280
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000281 if (!interpreter_lock)
282 return;
283 /*XXX Can't use PyThread_free_lock here because it does too
284 much error-checking. Doing this cleanly would require
285 adding a new function to each thread_*.h. Instead, just
286 create a new lock and waste a little bit of memory */
287 interpreter_lock = PyThread_allocate_lock();
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000288 pending_lock = PyThread_allocate_lock();
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000289 PyThread_acquire_lock(interpreter_lock, 1);
290 main_thread = PyThread_get_thread_ident();
Jesse Noller5e62ca42008-07-16 20:03:47 +0000291
292 /* Update the threading module with the new state.
293 */
294 tstate = PyThreadState_GET();
295 threading = PyMapping_GetItemString(tstate->interp->modules,
296 "threading");
297 if (threading == NULL) {
298 /* threading not imported */
299 PyErr_Clear();
300 return;
301 }
302 result = PyObject_CallMethod(threading, "_after_fork", NULL);
303 if (result == NULL)
304 PyErr_WriteUnraisable(threading);
305 else
306 Py_DECREF(result);
307 Py_DECREF(threading);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000308}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000309#endif
310
Guido van Rossumff4949e1992-08-05 19:58:53 +0000311/* Functions save_thread and restore_thread are always defined so
312 dynamically loaded modules needn't be compiled separately for use
313 with and without threads: */
314
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000315PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000316PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000317{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000318 PyThreadState *tstate = PyThreadState_Swap(NULL);
319 if (tstate == NULL)
320 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000321#ifdef WITH_THREAD
Guido van Rossumb74eca91997-09-30 22:03:16 +0000322 if (interpreter_lock)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000323 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000324#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000325 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000326}
327
328void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000329PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000330{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000331 if (tstate == NULL)
332 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000333#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000334 if (interpreter_lock) {
Guido van Rossumb74eca91997-09-30 22:03:16 +0000335 int err = errno;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000336 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000337 errno = err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000338 }
339#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000340 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000341}
342
343
Guido van Rossuma9672091994-09-14 13:31:22 +0000344/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
345 signal handlers or Mac I/O completion routines) can schedule calls
346 to a function to be called synchronously.
347 The synchronous function is called with one void* argument.
348 It should return 0 for success or -1 for failure -- failure should
349 be accompanied by an exception.
350
351 If registry succeeds, the registry function returns 0; if it fails
352 (e.g. due to too many pending calls) it returns -1 (without setting
353 an exception condition).
354
355 Note that because registry may occur from within signal handlers,
356 or other asynchronous events, calling malloc() is unsafe!
357
358#ifdef WITH_THREAD
359 Any thread can schedule pending calls, but only the main thread
360 will execute them.
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000361 There is no facility to schedule calls to a particular thread, but
362 that should be easy to change, should that ever be required. In
363 that case, the static variables here should go into the python
364 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000365#endif
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000366*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000367
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000368#ifdef WITH_THREAD
369
370/* The WITH_THREAD implementation is thread-safe. It allows
371 scheduling to be made from any thread, and even from an executing
372 callback.
373 */
374
375#define NPENDINGCALLS 32
376static struct {
377 int (*func)(void *);
378 void *arg;
379} pendingcalls[NPENDINGCALLS];
380static int pendingfirst = 0;
381static int pendinglast = 0;
382static volatile int pendingcalls_to_do = 1; /* trigger initialization of lock */
383static char pendingbusy = 0;
384
385int
386Py_AddPendingCall(int (*func)(void *), void *arg)
387{
388 int i, j, result=0;
389 PyThread_type_lock lock = pending_lock;
390
391 /* try a few times for the lock. Since this mechanism is used
392 * for signal handling (on the main thread), there is a (slim)
393 * chance that a signal is delivered on the same thread while we
394 * hold the lock during the Py_MakePendingCalls() function.
395 * This avoids a deadlock in that case.
396 * Note that signals can be delivered on any thread. In particular,
397 * on Windows, a SIGINT is delivered on a system-created worker
398 * thread.
399 * We also check for lock being NULL, in the unlikely case that
400 * this function is called before any bytecode evaluation takes place.
401 */
402 if (lock != NULL) {
403 for (i = 0; i<100; i++) {
404 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
405 break;
406 }
407 if (i == 100)
408 return -1;
409 }
410
411 i = pendinglast;
412 j = (i + 1) % NPENDINGCALLS;
413 if (j == pendingfirst) {
414 result = -1; /* Queue full */
415 } else {
416 pendingcalls[i].func = func;
417 pendingcalls[i].arg = arg;
418 pendinglast = j;
419 }
420 /* signal main loop */
421 _Py_Ticker = 0;
422 pendingcalls_to_do = 1;
423 if (lock != NULL)
424 PyThread_release_lock(lock);
425 return result;
426}
427
428int
429Py_MakePendingCalls(void)
430{
431 int i;
432 int r = 0;
433
434 if (!pending_lock) {
435 /* initial allocation of the lock */
436 pending_lock = PyThread_allocate_lock();
437 if (pending_lock == NULL)
438 return -1;
439 }
440
441 /* only service pending calls on main thread */
442 if (main_thread && PyThread_get_thread_ident() != main_thread)
443 return 0;
444 /* don't perform recursive pending calls */
445 if (pendingbusy)
446 return 0;
447 pendingbusy = 1;
448 /* perform a bounded number of calls, in case of recursion */
449 for (i=0; i<NPENDINGCALLS; i++) {
450 int j;
451 int (*func)(void *);
452 void *arg;
453
454 /* pop one item off the queue while holding the lock */
455 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
456 j = pendingfirst;
457 if (j == pendinglast) {
458 func = NULL; /* Queue empty */
459 } else {
460 func = pendingcalls[j].func;
461 arg = pendingcalls[j].arg;
462 pendingfirst = (j + 1) % NPENDINGCALLS;
463 }
464 pendingcalls_to_do = pendingfirst != pendinglast;
465 PyThread_release_lock(pending_lock);
466 /* having released the lock, perform the callback */
467 if (func == NULL)
468 break;
469 r = func(arg);
470 if (r)
471 break;
472 }
473 pendingbusy = 0;
474 return r;
475}
476
477#else /* if ! defined WITH_THREAD */
478
479/*
480 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
481 This code is used for signal handling in python that isn't built
482 with WITH_THREAD.
483 Don't use this implementation when Py_AddPendingCalls() can happen
484 on a different thread!
485
Guido van Rossuma9672091994-09-14 13:31:22 +0000486 There are two possible race conditions:
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000487 (1) nested asynchronous calls to Py_AddPendingCall()
488 (2) AddPendingCall() calls made while pending calls are being processed.
489
490 (1) is very unlikely because typically signal delivery
491 is blocked during signal handling. So it should be impossible.
492 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000493 The current code is safe against (2), but not against (1).
494 The safety against (2) is derived from the fact that only one
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000495 thread is present, interrupted by signals, and that the critical
496 section is protected with the "busy" variable. On Windows, which
497 delivers SIGINT on a system thread, this does not hold and therefore
498 Windows really shouldn't use this version.
499 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000500*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000501
Guido van Rossuma9672091994-09-14 13:31:22 +0000502#define NPENDINGCALLS 32
503static struct {
Thomas Wouters334fb892000-07-25 12:56:38 +0000504 int (*func)(void *);
505 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000506} pendingcalls[NPENDINGCALLS];
507static volatile int pendingfirst = 0;
508static volatile int pendinglast = 0;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000509static volatile int pendingcalls_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000510
511int
Thomas Wouters334fb892000-07-25 12:56:38 +0000512Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000513{
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000514 static volatile int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000515 int i, j;
516 /* XXX Begin critical section */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000517 if (busy)
518 return -1;
519 busy = 1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000520 i = pendinglast;
521 j = (i + 1) % NPENDINGCALLS;
Guido van Rossum04e70322002-07-17 16:57:13 +0000522 if (j == pendingfirst) {
523 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000524 return -1; /* Queue full */
Guido van Rossum04e70322002-07-17 16:57:13 +0000525 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000526 pendingcalls[i].func = func;
527 pendingcalls[i].arg = arg;
528 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000529
530 _Py_Ticker = 0;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000531 pendingcalls_to_do = 1; /* Signal main loop */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000532 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000533 /* XXX End critical section */
534 return 0;
535}
536
Guido van Rossum180d7b41994-09-29 09:45:57 +0000537int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000538Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000539{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000540 static int busy = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000541 if (busy)
Guido van Rossum180d7b41994-09-29 09:45:57 +0000542 return 0;
543 busy = 1;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000544 pendingcalls_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000545 for (;;) {
546 int i;
Thomas Wouters334fb892000-07-25 12:56:38 +0000547 int (*func)(void *);
548 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000549 i = pendingfirst;
550 if (i == pendinglast)
551 break; /* Queue empty */
552 func = pendingcalls[i].func;
553 arg = pendingcalls[i].arg;
554 pendingfirst = (i + 1) % NPENDINGCALLS;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000555 if (func(arg) < 0) {
556 busy = 0;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000557 pendingcalls_to_do = 1; /* We're not done yet */
Guido van Rossuma9672091994-09-14 13:31:22 +0000558 return -1;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000559 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000560 }
Guido van Rossum180d7b41994-09-29 09:45:57 +0000561 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000562 return 0;
563}
564
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000565#endif /* WITH_THREAD */
566
Guido van Rossuma9672091994-09-14 13:31:22 +0000567
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000568/* The interpreter's recursion limit */
569
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000570#ifndef Py_DEFAULT_RECURSION_LIMIT
571#define Py_DEFAULT_RECURSION_LIMIT 1000
572#endif
573static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
574int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000575
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000576int
577Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000578{
579 return recursion_limit;
580}
581
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000582void
583Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000584{
585 recursion_limit = new_limit;
Thomas Woutersae406c62007-09-19 17:27:43 +0000586 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000587}
588
Armin Rigo2b3eb402003-10-28 12:05:48 +0000589/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
590 if the recursion_depth reaches _Py_CheckRecursionLimit.
591 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
592 to guarantee that _Py_CheckRecursiveCall() is regularly called.
593 Without USE_STACKCHECK, there is no need for this. */
594int
595_Py_CheckRecursiveCall(char *where)
596{
597 PyThreadState *tstate = PyThreadState_GET();
598
599#ifdef USE_STACKCHECK
600 if (PyOS_CheckStack()) {
601 --tstate->recursion_depth;
602 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
603 return -1;
604 }
605#endif
606 if (tstate->recursion_depth > recursion_limit) {
607 --tstate->recursion_depth;
608 PyErr_Format(PyExc_RuntimeError,
609 "maximum recursion depth exceeded%s",
610 where);
611 return -1;
612 }
Thomas Woutersae406c62007-09-19 17:27:43 +0000613 _Py_CheckRecursionLimit = recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000614 return 0;
615}
616
Guido van Rossum374a9221991-04-04 10:40:29 +0000617/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000618enum why_code {
619 WHY_NOT = 0x0001, /* No error */
620 WHY_EXCEPTION = 0x0002, /* Exception occurred */
621 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
622 WHY_RETURN = 0x0008, /* 'return' statement */
623 WHY_BREAK = 0x0010, /* 'break' statement */
624 WHY_CONTINUE = 0x0020, /* 'continue' statement */
625 WHY_YIELD = 0x0040 /* 'yield' operator */
626};
Guido van Rossum374a9221991-04-04 10:40:29 +0000627
Fredrik Lundh7a830892006-05-27 10:39:48 +0000628static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
629static int unpack_iterable(PyObject *, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000630
Jeffrey Yasskinfd8a1ec2008-12-03 06:46:45 +0000631/* Records whether tracing is on for any thread. Counts the number of
632 threads for which tstate->c_tracefunc is non-NULL, so if the value
633 is 0, we know we don't have to check this thread's c_tracefunc.
634 This speeds up the if statement in PyEval_EvalFrameEx() after
635 fast_next_opcode*/
636static int _Py_TracingPossible = 0;
637
Skip Montanarod581d772002-09-03 20:10:45 +0000638/* for manipulating the thread switch and periodic "stuff" - used to be
639 per thread, now just a pair o' globals */
Skip Montanaro99dba272002-09-03 20:19:06 +0000640int _Py_CheckInterval = 100;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000641volatile int _Py_Ticker = 0; /* so that we hit a "tick" first thing */
Guido van Rossum374a9221991-04-04 10:40:29 +0000642
Guido van Rossumb209a111997-04-29 18:18:01 +0000643PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000644PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000645{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000646 return PyEval_EvalCodeEx(co,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000647 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000648 (PyObject **)NULL, 0,
649 (PyObject **)NULL, 0,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000650 (PyObject **)NULL, 0,
651 NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000652}
653
654
655/* Interpreter main loop */
656
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000657PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000658PyEval_EvalFrame(PyFrameObject *f) {
659 /* This is for backward compatibility with extension modules that
Thomas Wouterse2176022007-09-20 17:35:10 +0000660 used this API; core interpreter code should call
661 PyEval_EvalFrameEx() */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000662 return PyEval_EvalFrameEx(f, 0);
663}
664
665PyObject *
Anthony Baxtera863d332006-04-11 07:43:46 +0000666PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000667{
Guido van Rossum950361c1997-01-24 13:49:28 +0000668#ifdef DXPAIRS
669 int lastopcode = 0;
670#endif
Thomas Wouterse2176022007-09-20 17:35:10 +0000671 register PyObject **stack_pointer; /* Next free slot in value stack */
Guido van Rossum374a9221991-04-04 10:40:29 +0000672 register unsigned char *next_instr;
Armin Rigo8817fcd2004-06-17 10:22:40 +0000673 register int opcode; /* Current opcode */
674 register int oparg; /* Current opcode argument, if any */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000675 register enum why_code why; /* Reason for block stack unwind */
Guido van Rossum374a9221991-04-04 10:40:29 +0000676 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000677 register PyObject *x; /* Result object -- NULL if error */
678 register PyObject *v; /* Temporary objects popped off stack */
679 register PyObject *w;
680 register PyObject *u;
681 register PyObject *t;
Barry Warsaw23c9ec82000-08-21 15:44:01 +0000682 register PyObject *stream = NULL; /* for PRINT opcodes */
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000683 register PyObject **fastlocals, **freevars;
Guido van Rossum014518f1998-11-23 21:09:51 +0000684 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000685 PyThreadState *tstate = PyThreadState_GET();
Tim Peters5ca576e2001-06-18 22:08:13 +0000686 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000687
Tim Peters8a5c3c72004-04-05 19:36:21 +0000688 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000689
690 not (instr_lb <= current_bytecode_offset < instr_ub)
691
Tim Peters8a5c3c72004-04-05 19:36:21 +0000692 is true when the line being executed has changed. The
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000693 initial values are such as to make this false the first
694 time it is tested. */
Armin Rigobf57a142004-03-22 19:24:58 +0000695 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000696
Guido van Rossumd076c731998-10-07 19:42:25 +0000697 unsigned char *first_instr;
Skip Montanaro04d80f82002-08-04 21:03:35 +0000698 PyObject *names;
699 PyObject *consts;
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000700#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000701 /* Make it easier to find out where we are with a debugger */
Tim Peters5ca576e2001-06-18 22:08:13 +0000702 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000703#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000704
Neal Norwitza81d2202002-07-14 00:27:26 +0000705/* Tuple access macros */
706
707#ifndef Py_DEBUG
708#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
709#else
710#define GETITEM(v, i) PyTuple_GetItem((v), (i))
711#endif
712
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000713#ifdef WITH_TSC
714/* Use Pentium timestamp counter to mark certain events:
715 inst0 -- beginning of switch statement for opcode dispatch
716 inst1 -- end of switch statement (may be skipped)
717 loop0 -- the top of the mainloop
Tim Peters7df5e7f2006-05-26 23:14:37 +0000718 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000719 (may be skipped)
720 intr1 -- beginning of long interruption
721 intr2 -- end of long interruption
722
723 Many opcodes call out to helper C functions. In some cases, the
724 time in those functions should be counted towards the time for the
725 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
726 calls another Python function; there's no point in charge all the
727 bytecode executed by the called function to the caller.
728
729 It's hard to make a useful judgement statically. In the presence
730 of operator overloading, it's impossible to tell if a call will
731 execute new Python code or not.
732
733 It's a case-by-case judgement. I'll use intr1 for the following
734 cases:
735
736 EXEC_STMT
737 IMPORT_STAR
738 IMPORT_FROM
739 CALL_FUNCTION (and friends)
740
741 */
742 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
743 int ticked = 0;
744
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000745 READ_TIMESTAMP(inst0);
746 READ_TIMESTAMP(inst1);
747 READ_TIMESTAMP(loop0);
748 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000749
750 /* shut up the compiler */
751 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000752#endif
753
Guido van Rossum374a9221991-04-04 10:40:29 +0000754/* Code access macros */
755
Martin v. Löwis18e16552006-02-15 17:27:45 +0000756#define INSTR_OFFSET() ((int)(next_instr - first_instr))
Guido van Rossum374a9221991-04-04 10:40:29 +0000757#define NEXTOP() (*next_instr++)
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000758#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000759#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
Guido van Rossumd076c731998-10-07 19:42:25 +0000760#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000761#define JUMPBY(x) (next_instr += (x))
762
Raymond Hettingerf606f872003-03-16 03:11:04 +0000763/* OpCode prediction macros
Thomas Wouterse2176022007-09-20 17:35:10 +0000764 Some opcodes tend to come in pairs thus making it possible to
765 predict the second code when the first is run. For example,
766 COMPARE_OP is often followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And,
767 those opcodes are often followed by a POP_TOP.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000768
Raymond Hettingerafae11e2008-07-05 02:11:55 +0000769 Verifying the prediction costs a single high-speed test of a register
Raymond Hettingerac2072922003-03-16 15:41:11 +0000770 variable against a constant. If the pairing was good, then the
Raymond Hettingerafae11e2008-07-05 02:11:55 +0000771 processor's own internal branch predication has a high likelihood of
772 success, resulting in a nearly zero-overhead transition to the
773 next opcode. A successful prediction saves a trip through the eval-loop
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +0000774 including its two unpredictable branches, the HAS_ARG test and the
Raymond Hettingerafae11e2008-07-05 02:11:55 +0000775 switch-case. Combined with the processor's internal branch prediction,
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +0000776 a successful PREDICT has the effect of making the two opcodes run as if
Raymond Hettingerafae11e2008-07-05 02:11:55 +0000777 they were a single new opcode with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000778
Raymond Hettingerafae11e2008-07-05 02:11:55 +0000779 If collecting opcode statistics, your choices are to either keep the
780 predictions turned-on and interpret the results as if some opcodes
781 had been combined or turn-off predictions so that the opcode frequency
782 counter updates for both opcodes.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000783*/
784
Raymond Hettingera7216982004-02-08 19:59:27 +0000785#ifdef DYNAMIC_EXECUTION_PROFILE
786#define PREDICT(op) if (0) goto PRED_##op
787#else
Raymond Hettingerac2072922003-03-16 15:41:11 +0000788#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000789#endif
790
Raymond Hettingerf606f872003-03-16 03:11:04 +0000791#define PREDICTED(op) PRED_##op: next_instr++
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000792#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Raymond Hettingerf606f872003-03-16 03:11:04 +0000793
Guido van Rossum374a9221991-04-04 10:40:29 +0000794/* Stack manipulation macros */
795
Martin v. Löwis18e16552006-02-15 17:27:45 +0000796/* The stack can grow at most MAXINT deep, as co_nlocals and
797 co_stacksize are ints. */
798#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
Guido van Rossum374a9221991-04-04 10:40:29 +0000799#define EMPTY() (STACK_LEVEL() == 0)
800#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000801#define SECOND() (stack_pointer[-2])
802#define THIRD() (stack_pointer[-3])
803#define FOURTH() (stack_pointer[-4])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000804#define SET_TOP(v) (stack_pointer[-1] = (v))
805#define SET_SECOND(v) (stack_pointer[-2] = (v))
806#define SET_THIRD(v) (stack_pointer[-3] = (v))
807#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000808#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000809#define BASIC_PUSH(v) (*stack_pointer++ = (v))
810#define BASIC_POP() (*--stack_pointer)
811
Guido van Rossum96a42c81992-01-12 02:29:51 +0000812#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000813#define PUSH(v) { (void)(BASIC_PUSH(v), \
814 lltrace && prtrace(TOP(), "push")); \
Richard Jonescebbefc2006-05-23 18:28:17 +0000815 assert(STACK_LEVEL() <= co->co_stacksize); }
Thomas Wouterse2176022007-09-20 17:35:10 +0000816#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
817 BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000818#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
819 lltrace && prtrace(TOP(), "stackadj")); \
Richard Jonescebbefc2006-05-23 18:28:17 +0000820 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes52729ac2007-12-14 02:33:57 +0000821#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
822 prtrace((STACK_POINTER)[-1], "ext_pop")), \
823 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000824#else
825#define PUSH(v) BASIC_PUSH(v)
826#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000827#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000828#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000829#endif
830
Guido van Rossum681d79a1995-07-18 14:51:37 +0000831/* Local variable macros */
832
833#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000834
835/* The SETLOCAL() macro must not DECREF the local variable in-place and
836 then store the new value; it must copy the old value to a temporary
837 value, then store the new value, and then DECREF the temporary value.
838 This is because it is possible that during the DECREF the frame is
839 accessed by other code (e.g. a __del__ method or gc.collect()) and the
840 variable would be pointing to already-freed memory. */
841#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
842 GETLOCAL(i) = value; \
843 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000844
Guido van Rossuma027efa1997-05-05 20:56:21 +0000845/* Start of code */
846
Tim Peters5ca576e2001-06-18 22:08:13 +0000847 if (f == NULL)
848 return NULL;
849
Armin Rigo1d313ab2003-10-25 14:33:09 +0000850 /* push frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000851 if (Py_EnterRecursiveCall(""))
Armin Rigo1d313ab2003-10-25 14:33:09 +0000852 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000853
Tim Peters5ca576e2001-06-18 22:08:13 +0000854 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000855
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000856 if (tstate->use_tracing) {
857 if (tstate->c_tracefunc != NULL) {
858 /* tstate->c_tracefunc, if defined, is a
859 function that will be called on *every* entry
860 to a code block. Its return value, if not
861 None, is a function that will be called at
862 the start of each executed line of code.
863 (Actually, the function must return itself
864 in order to continue tracing.) The trace
865 functions are called with three arguments:
866 a pointer to the current frame, a string
867 indicating why the function is called, and
868 an argument which depends on the situation.
869 The global trace function is also called
870 whenever an exception is detected. */
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +0000871 if (call_trace_protected(tstate->c_tracefunc,
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +0000872 tstate->c_traceobj,
873 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000874 /* Trace function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000875 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000876 }
877 }
878 if (tstate->c_profilefunc != NULL) {
879 /* Similar for c_profilefunc, except it needn't
880 return itself and isn't called for "line" events */
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +0000881 if (call_trace_protected(tstate->c_profilefunc,
882 tstate->c_profileobj,
883 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000884 /* Profile function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000885 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000886 }
887 }
888 }
889
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000890 co = f->f_code;
891 names = co->co_names;
892 consts = co->co_consts;
893 fastlocals = f->f_localsplus;
Richard Jonescebbefc2006-05-23 18:28:17 +0000894 freevars = f->f_localsplus + co->co_nlocals;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000895 first_instr = (unsigned char*) PyString_AS_STRING(co->co_code);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000896 /* An explanation is in order for the next line.
897
898 f->f_lasti now refers to the index of the last instruction
899 executed. You might think this was obvious from the name, but
900 this wasn't always true before 2.3! PyFrame_New now sets
901 f->f_lasti to -1 (i.e. the index *before* the first instruction)
902 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +0000903 does work. Promise.
Raymond Hettinger4bd97d42007-01-06 01:14:41 +0000904
905 When the PREDICT() macros are enabled, some opcode pairs follow in
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +0000906 direct succession without updating f->f_lasti. A successful
Raymond Hettinger4bd97d42007-01-06 01:14:41 +0000907 prediction effectively links the two codes together as if they
908 were a single new opcode; accordingly,f->f_lasti will point to
909 the first code in the pair (for instance, GET_ITER followed by
910 FOR_ITER is effectively a single opcode and f->f_lasti will point
911 at to the beginning of the combined pair.)
912 */
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000913 next_instr = first_instr + f->f_lasti + 1;
914 stack_pointer = f->f_stacktop;
915 assert(stack_pointer != NULL);
916 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
917
Tim Peters5ca576e2001-06-18 22:08:13 +0000918#ifdef LLTRACE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000919 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +0000920#endif
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000921#if defined(Py_DEBUG) || defined(LLTRACE)
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000922 filename = PyString_AsString(co->co_filename);
Tim Peters5ca576e2001-06-18 22:08:13 +0000923#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000924
Guido van Rossum374a9221991-04-04 10:40:29 +0000925 why = WHY_NOT;
926 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +0000927 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +0000928 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +0000929
Anthony Baxtera863d332006-04-11 07:43:46 +0000930 if (throwflag) { /* support for generator.throw() */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000931 why = WHY_EXCEPTION;
932 goto on_error;
933 }
Tim Peters7df5e7f2006-05-26 23:14:37 +0000934
Guido van Rossum374a9221991-04-04 10:40:29 +0000935 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000936#ifdef WITH_TSC
937 if (inst1 == 0) {
938 /* Almost surely, the opcode executed a break
939 or a continue, preventing inst1 from being set
940 on the way out of the loop.
941 */
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000942 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000943 loop1 = inst1;
944 }
945 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
946 intr0, intr1);
947 ticked = 0;
948 inst1 = 0;
949 intr0 = 0;
950 intr1 = 0;
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000951 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000952#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000953 assert(stack_pointer >= f->f_valuestack); /* else underflow */
Richard Jonescebbefc2006-05-23 18:28:17 +0000954 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000955
Guido van Rossuma027efa1997-05-05 20:56:21 +0000956 /* Do periodic things. Doing this every time through
957 the loop would add too much overhead, so we do it
958 only every Nth instruction. We also do it if
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000959 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
Guido van Rossuma027efa1997-05-05 20:56:21 +0000960 event needs attention (e.g. a signal handler or
961 async I/O handler); see Py_AddPendingCall() and
962 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000963
Skip Montanarod581d772002-09-03 20:10:45 +0000964 if (--_Py_Ticker < 0) {
Thomas Woutersae406c62007-09-19 17:27:43 +0000965 if (*next_instr == SETUP_FINALLY) {
966 /* Make the last opcode before
967 a try: finally: block uninterruptable. */
968 goto fast_next_opcode;
969 }
Skip Montanarod581d772002-09-03 20:10:45 +0000970 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000971 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000972#ifdef WITH_TSC
973 ticked = 1;
974#endif
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000975 if (pendingcalls_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +0000976 if (Py_MakePendingCalls() < 0) {
977 why = WHY_EXCEPTION;
978 goto on_error;
979 }
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000980 if (pendingcalls_to_do)
Kurt B. Kaiser4c79a832004-11-23 18:06:08 +0000981 /* MakePendingCalls() didn't succeed.
982 Force early re-execution of this
983 "periodic" code, possibly after
984 a thread switch */
985 _Py_Ticker = 0;
Guido van Rossum8861b741996-07-30 16:49:37 +0000986 }
Guido van Rossume59214e1994-08-30 08:01:59 +0000987#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000988 if (interpreter_lock) {
989 /* Give another thread a chance */
990
Guido van Rossum25ce5661997-08-02 03:10:38 +0000991 if (PyThreadState_Swap(NULL) != tstate)
992 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000993 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000994
995 /* Other threads may run now */
996
Guido van Rossum65d5b571998-12-21 19:32:43 +0000997 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000998 if (PyThreadState_Swap(tstate) != NULL)
999 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +00001000
1001 /* Check for thread interrupts */
1002
1003 if (tstate->async_exc != NULL) {
1004 x = tstate->async_exc;
1005 tstate->async_exc = NULL;
1006 PyErr_SetNone(x);
1007 Py_DECREF(x);
1008 why = WHY_EXCEPTION;
1009 goto on_error;
1010 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001011 }
1012#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001013 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001014
Neil Schemenauer63543862002-02-17 19:10:14 +00001015 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +00001016 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001017
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001018 /* line-by-line tracing support */
1019
Jeffrey Yasskinfd8a1ec2008-12-03 06:46:45 +00001020 if (_Py_TracingPossible &&
1021 tstate->c_tracefunc != NULL && !tstate->tracing) {
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001022 /* see maybe_call_line_trace
1023 for expository comments */
1024 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001025
Michael W. Hudson58ee2af2003-04-29 16:18:47 +00001026 err = maybe_call_line_trace(tstate->c_tracefunc,
1027 tstate->c_traceobj,
Armin Rigobf57a142004-03-22 19:24:58 +00001028 f, &instr_lb, &instr_ub,
1029 &instr_prev);
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001030 /* Reload possibly changed frame fields */
1031 JUMPTO(f->f_lasti);
Michael W. Hudson58ee2af2003-04-29 16:18:47 +00001032 if (f->f_stacktop != NULL) {
1033 stack_pointer = f->f_stacktop;
1034 f->f_stacktop = NULL;
1035 }
1036 if (err) {
1037 /* trace function raised an exception */
1038 goto on_error;
1039 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001040 }
1041
1042 /* Extract opcode and argument */
1043
Guido van Rossum374a9221991-04-04 10:40:29 +00001044 opcode = NEXTOP();
Armin Rigo8817fcd2004-06-17 10:22:40 +00001045 oparg = 0; /* allows oparg to be stored in a register because
1046 it doesn't have to be remembered across a full loop */
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001047 if (HAS_ARG(opcode))
1048 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00001049 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001050#ifdef DYNAMIC_EXECUTION_PROFILE
1051#ifdef DXPAIRS
1052 dxpairs[lastopcode][opcode]++;
1053 lastopcode = opcode;
1054#endif
1055 dxp[opcode]++;
1056#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001057
Guido van Rossum96a42c81992-01-12 02:29:51 +00001058#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +00001059 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001060
Guido van Rossum96a42c81992-01-12 02:29:51 +00001061 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001062 if (HAS_ARG(opcode)) {
1063 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001064 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001065 }
1066 else {
1067 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001068 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +00001069 }
1070 }
1071#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001072
Guido van Rossum374a9221991-04-04 10:40:29 +00001073 /* Main switch on opcode */
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001074 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001075
Guido van Rossum374a9221991-04-04 10:40:29 +00001076 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001077
Guido van Rossum374a9221991-04-04 10:40:29 +00001078 /* BEWARE!
1079 It is essential that any operation that fails sets either
1080 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1081 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001082
Guido van Rossum374a9221991-04-04 10:40:29 +00001083 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001084
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001085 case NOP:
1086 goto fast_next_opcode;
1087
Neil Schemenauer63543862002-02-17 19:10:14 +00001088 case LOAD_FAST:
1089 x = GETLOCAL(oparg);
1090 if (x != NULL) {
1091 Py_INCREF(x);
1092 PUSH(x);
1093 goto fast_next_opcode;
1094 }
1095 format_exc_check_arg(PyExc_UnboundLocalError,
1096 UNBOUNDLOCAL_ERROR_MSG,
1097 PyTuple_GetItem(co->co_varnames, oparg));
1098 break;
1099
1100 case LOAD_CONST:
Skip Montanaro04d80f82002-08-04 21:03:35 +00001101 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +00001102 Py_INCREF(x);
1103 PUSH(x);
1104 goto fast_next_opcode;
1105
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001106 PREDICTED_WITH_ARG(STORE_FAST);
Neil Schemenauer63543862002-02-17 19:10:14 +00001107 case STORE_FAST:
1108 v = POP();
1109 SETLOCAL(oparg, v);
1110 goto fast_next_opcode;
1111
Raymond Hettingerf606f872003-03-16 03:11:04 +00001112 PREDICTED(POP_TOP);
Guido van Rossum374a9221991-04-04 10:40:29 +00001113 case POP_TOP:
1114 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001115 Py_DECREF(v);
Neil Schemenauer63543862002-02-17 19:10:14 +00001116 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001117
Guido van Rossum374a9221991-04-04 10:40:29 +00001118 case ROT_TWO:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001119 v = TOP();
1120 w = SECOND();
1121 SET_TOP(w);
1122 SET_SECOND(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +00001123 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001124
Guido van Rossum374a9221991-04-04 10:40:29 +00001125 case ROT_THREE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001126 v = TOP();
1127 w = SECOND();
1128 x = THIRD();
1129 SET_TOP(w);
1130 SET_SECOND(x);
1131 SET_THIRD(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +00001132 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001133
Thomas Wouters434d0822000-08-24 20:11:32 +00001134 case ROT_FOUR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001135 u = TOP();
1136 v = SECOND();
1137 w = THIRD();
1138 x = FOURTH();
1139 SET_TOP(v);
1140 SET_SECOND(w);
1141 SET_THIRD(x);
1142 SET_FOURTH(u);
Raymond Hettinger080cb322003-03-14 01:37:42 +00001143 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001144
Guido van Rossum374a9221991-04-04 10:40:29 +00001145 case DUP_TOP:
1146 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001147 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001148 PUSH(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +00001149 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001150
Thomas Wouters434d0822000-08-24 20:11:32 +00001151 case DUP_TOPX:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001152 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001153 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +00001154 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001155 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +00001156 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001157 STACKADJ(2);
1158 SET_TOP(x);
1159 SET_SECOND(w);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001160 goto fast_next_opcode;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001161 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001162 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +00001163 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001164 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +00001165 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001166 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +00001167 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001168 STACKADJ(3);
1169 SET_TOP(x);
1170 SET_SECOND(w);
1171 SET_THIRD(v);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001172 goto fast_next_opcode;
Thomas Wouters434d0822000-08-24 20:11:32 +00001173 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001174 Py_FatalError("invalid argument to DUP_TOPX"
1175 " (bytecode corruption?)");
Jeffrey Yasskin2d873bd2008-12-08 18:55:24 +00001176 /* Never returns, so don't bother to set why. */
Tim Peters35ba6892000-10-11 07:04:49 +00001177 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001178
Guido van Rossum374a9221991-04-04 10:40:29 +00001179 case UNARY_POSITIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001180 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001181 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001182 Py_DECREF(v);
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 UNARY_NEGATIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001188 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001189 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001190 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001191 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001192 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001193 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001194
Guido van Rossum374a9221991-04-04 10:40:29 +00001195 case UNARY_NOT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001196 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001197 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001198 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +00001199 if (err == 0) {
1200 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001201 SET_TOP(Py_True);
Guido van Rossumfc490731997-05-06 15:06:49 +00001202 continue;
1203 }
1204 else if (err > 0) {
1205 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001206 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +00001207 err = 0;
1208 continue;
1209 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00001210 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001211 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001212
Guido van Rossum374a9221991-04-04 10:40:29 +00001213 case UNARY_CONVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001214 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001215 x = PyObject_Repr(v);
1216 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001217 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001218 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001219 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001220
Guido van Rossum7928cd71991-10-24 14:59:31 +00001221 case UNARY_INVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001222 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001223 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001224 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001225 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001226 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001227 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001228
Guido van Rossum50564e81996-01-12 01:13:16 +00001229 case BINARY_POWER:
1230 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001231 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001232 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +00001233 Py_DECREF(v);
1234 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001235 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001236 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +00001237 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001238
Guido van Rossum374a9221991-04-04 10:40:29 +00001239 case BINARY_MULTIPLY:
1240 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001241 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001242 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001243 Py_DECREF(v);
1244 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001245 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001246 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001247 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001248
Guido van Rossum374a9221991-04-04 10:40:29 +00001249 case BINARY_DIVIDE:
Tim Peters3caca232001-12-06 06:23:26 +00001250 if (!_Py_QnewFlag) {
1251 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001252 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001253 x = PyNumber_Divide(v, w);
1254 Py_DECREF(v);
1255 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001256 SET_TOP(x);
Tim Peters3caca232001-12-06 06:23:26 +00001257 if (x != NULL) continue;
1258 break;
1259 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001260 /* -Qnew is in effect: fall through to
Tim Peters3caca232001-12-06 06:23:26 +00001261 BINARY_TRUE_DIVIDE */
1262 case BINARY_TRUE_DIVIDE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001263 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001264 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001265 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001266 Py_DECREF(v);
1267 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001268 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001269 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001270 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001271
Guido van Rossum4668b002001-08-08 05:00:18 +00001272 case BINARY_FLOOR_DIVIDE:
1273 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001274 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001275 x = PyNumber_FloorDivide(v, w);
1276 Py_DECREF(v);
1277 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001278 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001279 if (x != NULL) continue;
1280 break;
1281
Guido van Rossum374a9221991-04-04 10:40:29 +00001282 case BINARY_MODULO:
1283 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001284 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001285 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001286 Py_DECREF(v);
1287 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001288 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001289 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001290 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001291
Guido van Rossum374a9221991-04-04 10:40:29 +00001292 case BINARY_ADD:
1293 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001294 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001295 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001296 /* INLINE: int + int */
1297 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001298 a = PyInt_AS_LONG(v);
1299 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001300 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001301 if ((i^a) < 0 && (i^b) < 0)
1302 goto slow_add;
1303 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001304 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001305 else if (PyString_CheckExact(v) &&
1306 PyString_CheckExact(w)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001307 x = string_concatenate(v, w, f, next_instr);
1308 /* string_concatenate consumed the ref to v */
1309 goto skip_decref_vx;
1310 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001311 else {
1312 slow_add:
Guido van Rossumc12da691997-07-17 23:12:42 +00001313 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001314 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001315 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001316 skip_decref_vx:
Guido van Rossumb209a111997-04-29 18:18:01 +00001317 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001318 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001319 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001320 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001321
Guido van Rossum374a9221991-04-04 10:40:29 +00001322 case BINARY_SUBTRACT:
1323 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001324 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001325 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001326 /* INLINE: int - int */
1327 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001328 a = PyInt_AS_LONG(v);
1329 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001330 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001331 if ((i^a) < 0 && (i^~b) < 0)
1332 goto slow_sub;
1333 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001334 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001335 else {
1336 slow_sub:
Guido van Rossumc12da691997-07-17 23:12:42 +00001337 x = PyNumber_Subtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001338 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001339 Py_DECREF(v);
1340 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001341 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001342 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001343 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001344
Guido van Rossum374a9221991-04-04 10:40:29 +00001345 case BINARY_SUBSCR:
1346 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001347 v = TOP();
Tim Petersb1c46982001-10-05 20:41:38 +00001348 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001349 /* INLINE: list[int] */
Neal Norwitz814e9382006-03-02 07:54:28 +00001350 Py_ssize_t i = PyInt_AsSsize_t(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001351 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +00001352 i += PyList_GET_SIZE(v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001353 if (i >= 0 && i < PyList_GET_SIZE(v)) {
Guido van Rossumfa00e951998-07-08 15:02:37 +00001354 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001355 Py_INCREF(x);
1356 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001357 else
1358 goto slow_get;
Guido van Rossumc12da691997-07-17 23:12:42 +00001359 }
1360 else
Raymond Hettinger467a6982004-04-07 11:39:21 +00001361 slow_get:
Guido van Rossumc12da691997-07-17 23:12:42 +00001362 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001363 Py_DECREF(v);
1364 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001365 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001366 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001367 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001368
Guido van Rossum7928cd71991-10-24 14:59:31 +00001369 case BINARY_LSHIFT:
1370 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001371 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001372 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001373 Py_DECREF(v);
1374 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001375 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001376 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001377 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001378
Guido van Rossum7928cd71991-10-24 14:59:31 +00001379 case BINARY_RSHIFT:
1380 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001381 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001382 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001383 Py_DECREF(v);
1384 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001385 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001386 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001387 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001388
Guido van Rossum7928cd71991-10-24 14:59:31 +00001389 case BINARY_AND:
1390 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001391 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001392 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001393 Py_DECREF(v);
1394 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001395 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001396 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001397 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001398
Guido van Rossum7928cd71991-10-24 14:59:31 +00001399 case BINARY_XOR:
1400 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001401 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001402 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001403 Py_DECREF(v);
1404 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001405 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001406 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001407 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001408
Guido van Rossum7928cd71991-10-24 14:59:31 +00001409 case BINARY_OR:
1410 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001411 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001412 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001413 Py_DECREF(v);
1414 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001415 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001416 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001417 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001418
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001419 case LIST_APPEND:
1420 w = POP();
Antoine Pitroud0c35152008-12-17 00:38:28 +00001421 v = stack_pointer[-oparg];
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001422 err = PyList_Append(v, w);
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001423 Py_DECREF(w);
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001424 if (err == 0) {
1425 PREDICT(JUMP_ABSOLUTE);
1426 continue;
1427 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001428 break;
1429
Thomas Wouters434d0822000-08-24 20:11:32 +00001430 case INPLACE_POWER:
1431 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001432 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001433 x = PyNumber_InPlacePower(v, w, Py_None);
1434 Py_DECREF(v);
1435 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001436 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001437 if (x != NULL) continue;
1438 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001439
Thomas Wouters434d0822000-08-24 20:11:32 +00001440 case INPLACE_MULTIPLY:
1441 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001442 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001443 x = PyNumber_InPlaceMultiply(v, w);
1444 Py_DECREF(v);
1445 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001446 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001447 if (x != NULL) continue;
1448 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001449
Thomas Wouters434d0822000-08-24 20:11:32 +00001450 case INPLACE_DIVIDE:
Tim Peters54b11912001-12-25 18:49:11 +00001451 if (!_Py_QnewFlag) {
1452 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001453 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001454 x = PyNumber_InPlaceDivide(v, w);
1455 Py_DECREF(v);
1456 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001457 SET_TOP(x);
Tim Peters54b11912001-12-25 18:49:11 +00001458 if (x != NULL) continue;
1459 break;
1460 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001461 /* -Qnew is in effect: fall through to
Tim Peters54b11912001-12-25 18:49:11 +00001462 INPLACE_TRUE_DIVIDE */
1463 case INPLACE_TRUE_DIVIDE:
Thomas Wouters434d0822000-08-24 20:11:32 +00001464 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001465 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001466 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001467 Py_DECREF(v);
1468 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001469 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001470 if (x != NULL) continue;
1471 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001472
Guido van Rossum4668b002001-08-08 05:00:18 +00001473 case INPLACE_FLOOR_DIVIDE:
1474 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001475 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001476 x = PyNumber_InPlaceFloorDivide(v, w);
1477 Py_DECREF(v);
1478 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001479 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001480 if (x != NULL) continue;
1481 break;
1482
Thomas Wouters434d0822000-08-24 20:11:32 +00001483 case INPLACE_MODULO:
1484 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001485 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001486 x = PyNumber_InPlaceRemainder(v, w);
1487 Py_DECREF(v);
1488 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001489 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001490 if (x != NULL) continue;
1491 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001492
Thomas Wouters434d0822000-08-24 20:11:32 +00001493 case INPLACE_ADD:
1494 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001495 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001496 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001497 /* INLINE: int + int */
1498 register long a, b, i;
1499 a = PyInt_AS_LONG(v);
1500 b = PyInt_AS_LONG(w);
1501 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001502 if ((i^a) < 0 && (i^b) < 0)
1503 goto slow_iadd;
1504 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001505 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001506 else if (PyString_CheckExact(v) &&
1507 PyString_CheckExact(w)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001508 x = string_concatenate(v, w, f, next_instr);
1509 /* string_concatenate consumed the ref to v */
1510 goto skip_decref_v;
1511 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001512 else {
1513 slow_iadd:
Thomas Wouters434d0822000-08-24 20:11:32 +00001514 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001515 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001516 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001517 skip_decref_v:
Thomas Wouters434d0822000-08-24 20:11:32 +00001518 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001519 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001520 if (x != NULL) continue;
1521 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001522
Thomas Wouters434d0822000-08-24 20:11:32 +00001523 case INPLACE_SUBTRACT:
1524 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001525 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001526 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001527 /* INLINE: int - int */
1528 register long a, b, i;
1529 a = PyInt_AS_LONG(v);
1530 b = PyInt_AS_LONG(w);
1531 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001532 if ((i^a) < 0 && (i^~b) < 0)
1533 goto slow_isub;
1534 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001535 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001536 else {
1537 slow_isub:
Thomas Wouters434d0822000-08-24 20:11:32 +00001538 x = PyNumber_InPlaceSubtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001539 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001540 Py_DECREF(v);
1541 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001542 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001543 if (x != NULL) continue;
1544 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001545
Thomas Wouters434d0822000-08-24 20:11:32 +00001546 case INPLACE_LSHIFT:
1547 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001548 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001549 x = PyNumber_InPlaceLshift(v, w);
1550 Py_DECREF(v);
1551 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001552 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001553 if (x != NULL) continue;
1554 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001555
Thomas Wouters434d0822000-08-24 20:11:32 +00001556 case INPLACE_RSHIFT:
1557 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001558 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001559 x = PyNumber_InPlaceRshift(v, w);
1560 Py_DECREF(v);
1561 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001562 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001563 if (x != NULL) continue;
1564 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001565
Thomas Wouters434d0822000-08-24 20:11:32 +00001566 case INPLACE_AND:
1567 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001568 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001569 x = PyNumber_InPlaceAnd(v, w);
1570 Py_DECREF(v);
1571 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001572 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001573 if (x != NULL) continue;
1574 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001575
Thomas Wouters434d0822000-08-24 20:11:32 +00001576 case INPLACE_XOR:
1577 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001578 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001579 x = PyNumber_InPlaceXor(v, w);
1580 Py_DECREF(v);
1581 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001582 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001583 if (x != NULL) continue;
1584 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001585
Thomas Wouters434d0822000-08-24 20:11:32 +00001586 case INPLACE_OR:
1587 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001588 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001589 x = PyNumber_InPlaceOr(v, w);
1590 Py_DECREF(v);
1591 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001592 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001593 if (x != NULL) continue;
1594 break;
1595
Guido van Rossum374a9221991-04-04 10:40:29 +00001596 case SLICE+0:
1597 case SLICE+1:
1598 case SLICE+2:
1599 case SLICE+3:
1600 if ((opcode-SLICE) & 2)
1601 w = POP();
1602 else
1603 w = NULL;
1604 if ((opcode-SLICE) & 1)
1605 v = POP();
1606 else
1607 v = NULL;
Raymond Hettinger663004b2003-01-09 15:24:30 +00001608 u = TOP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001609 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001610 Py_DECREF(u);
1611 Py_XDECREF(v);
1612 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001613 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001614 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001615 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001616
Guido van Rossum374a9221991-04-04 10:40:29 +00001617 case STORE_SLICE+0:
1618 case STORE_SLICE+1:
1619 case STORE_SLICE+2:
1620 case STORE_SLICE+3:
1621 if ((opcode-STORE_SLICE) & 2)
1622 w = POP();
1623 else
1624 w = NULL;
1625 if ((opcode-STORE_SLICE) & 1)
1626 v = POP();
1627 else
1628 v = NULL;
1629 u = POP();
1630 t = POP();
1631 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +00001632 Py_DECREF(t);
1633 Py_DECREF(u);
1634 Py_XDECREF(v);
1635 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001636 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001637 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001638
Guido van Rossum374a9221991-04-04 10:40:29 +00001639 case DELETE_SLICE+0:
1640 case DELETE_SLICE+1:
1641 case DELETE_SLICE+2:
1642 case DELETE_SLICE+3:
1643 if ((opcode-DELETE_SLICE) & 2)
1644 w = POP();
1645 else
1646 w = NULL;
1647 if ((opcode-DELETE_SLICE) & 1)
1648 v = POP();
1649 else
1650 v = NULL;
1651 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001652 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +00001653 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00001654 Py_DECREF(u);
1655 Py_XDECREF(v);
1656 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001657 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001658 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001659
Guido van Rossum374a9221991-04-04 10:40:29 +00001660 case STORE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001661 w = TOP();
1662 v = SECOND();
1663 u = THIRD();
1664 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001665 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001666 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001667 Py_DECREF(u);
1668 Py_DECREF(v);
1669 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001670 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001671 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001672
Guido van Rossum374a9221991-04-04 10:40:29 +00001673 case DELETE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001674 w = TOP();
1675 v = SECOND();
1676 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001677 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001678 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001679 Py_DECREF(v);
1680 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001681 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001682 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001683
Guido van Rossum374a9221991-04-04 10:40:29 +00001684 case PRINT_EXPR:
1685 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001686 w = PySys_GetObject("displayhook");
1687 if (w == NULL) {
1688 PyErr_SetString(PyExc_RuntimeError,
1689 "lost sys.displayhook");
1690 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001691 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001692 }
1693 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001694 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001695 if (x == NULL)
1696 err = -1;
1697 }
1698 if (err == 0) {
1699 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001700 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001701 if (w == NULL)
1702 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001703 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001704 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001705 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001706 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001707
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001708 case PRINT_ITEM_TO:
1709 w = stream = POP();
1710 /* fall through to PRINT_ITEM */
1711
Guido van Rossum374a9221991-04-04 10:40:29 +00001712 case PRINT_ITEM:
1713 v = POP();
Barry Warsaw093abe02000-08-29 04:56:13 +00001714 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001715 w = PySys_GetObject("stdout");
1716 if (w == NULL) {
1717 PyErr_SetString(PyExc_RuntimeError,
1718 "lost sys.stdout");
1719 err = -1;
1720 }
Guido van Rossum8f183201997-12-31 05:53:15 +00001721 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001722 /* PyFile_SoftSpace() can exececute arbitrary code
1723 if sys.stdout is an instance with a __getattr__.
1724 If __getattr__ raises an exception, w will
1725 be freed, so we need to prevent that temporarily. */
1726 Py_XINCREF(w);
Tim Peters8e5fd532002-03-24 19:25:00 +00001727 if (w != NULL && PyFile_SoftSpace(w, 0))
Guido van Rossumbe270261997-05-22 22:26:18 +00001728 err = PyFile_WriteString(" ", w);
1729 if (err == 0)
1730 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001731 if (err == 0) {
Tim Peters8e5fd532002-03-24 19:25:00 +00001732 /* XXX move into writeobject() ? */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001733 if (PyString_Check(v)) {
1734 char *s = PyString_AS_STRING(v);
1735 Py_ssize_t len = PyString_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001736 if (len == 0 ||
1737 !isspace(Py_CHARMASK(s[len-1])) ||
1738 s[len-1] == ' ')
1739 PyFile_SoftSpace(w, 1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001740 }
Martin v. Löwis8d3ce5a2001-12-18 22:36:40 +00001741#ifdef Py_USING_UNICODE
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001742 else if (PyUnicode_Check(v)) {
1743 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
Martin v. Löwis66851282006-04-22 11:40:03 +00001744 Py_ssize_t len = PyUnicode_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001745 if (len == 0 ||
1746 !Py_UNICODE_ISSPACE(s[len-1]) ||
1747 s[len-1] == ' ')
1748 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001749 }
Michael W. Hudsond95c8282002-05-20 13:56:11 +00001750#endif
Tim Peters8e5fd532002-03-24 19:25:00 +00001751 else
1752 PyFile_SoftSpace(w, 1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001753 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001754 Py_XDECREF(w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001755 Py_DECREF(v);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001756 Py_XDECREF(stream);
1757 stream = NULL;
1758 if (err == 0)
1759 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001760 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001761
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001762 case PRINT_NEWLINE_TO:
1763 w = stream = POP();
1764 /* fall through to PRINT_NEWLINE */
1765
Guido van Rossum374a9221991-04-04 10:40:29 +00001766 case PRINT_NEWLINE:
Barry Warsaw093abe02000-08-29 04:56:13 +00001767 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001768 w = PySys_GetObject("stdout");
Jeffrey Yasskin2d873bd2008-12-08 18:55:24 +00001769 if (w == NULL) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001770 PyErr_SetString(PyExc_RuntimeError,
1771 "lost sys.stdout");
Jeffrey Yasskin2d873bd2008-12-08 18:55:24 +00001772 why = WHY_EXCEPTION;
1773 }
Guido van Rossum3165fe61992-09-25 21:59:05 +00001774 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001775 if (w != NULL) {
Georg Brandlaa76d772008-07-01 20:56:03 +00001776 /* w.write() may replace sys.stdout, so we
1777 * have to keep our reference to it */
Amaury Forgeot d'Arcbdd941f2008-07-01 20:38:04 +00001778 Py_INCREF(w);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001779 err = PyFile_WriteString("\n", w);
1780 if (err == 0)
1781 PyFile_SoftSpace(w, 0);
Amaury Forgeot d'Arcbdd941f2008-07-01 20:38:04 +00001782 Py_DECREF(w);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001783 }
1784 Py_XDECREF(stream);
1785 stream = NULL;
Guido van Rossum374a9221991-04-04 10:40:29 +00001786 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001787
Thomas Wouters434d0822000-08-24 20:11:32 +00001788
1789#ifdef CASE_TOO_BIG
1790 default: switch (opcode) {
1791#endif
Guido van Rossumf10570b1995-07-07 22:53:21 +00001792 case RAISE_VARARGS:
1793 u = v = w = NULL;
1794 switch (oparg) {
1795 case 3:
1796 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001797 /* Fallthrough */
1798 case 2:
1799 v = POP(); /* value */
1800 /* Fallthrough */
1801 case 1:
1802 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001803 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001804 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001805 break;
1806 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001807 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001808 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001809 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001810 break;
1811 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001812 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001813
Guido van Rossum374a9221991-04-04 10:40:29 +00001814 case LOAD_LOCALS:
Raymond Hettinger467a6982004-04-07 11:39:21 +00001815 if ((x = f->f_locals) != NULL) {
1816 Py_INCREF(x);
1817 PUSH(x);
1818 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001819 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001820 PyErr_SetString(PyExc_SystemError, "no locals");
Guido van Rossum374a9221991-04-04 10:40:29 +00001821 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001822
Guido van Rossum374a9221991-04-04 10:40:29 +00001823 case RETURN_VALUE:
1824 retval = POP();
1825 why = WHY_RETURN;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001826 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001827
Tim Peters5ca576e2001-06-18 22:08:13 +00001828 case YIELD_VALUE:
1829 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001830 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001831 why = WHY_YIELD;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001832 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001833
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001834 case EXEC_STMT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001835 w = TOP();
1836 v = SECOND();
1837 u = THIRD();
1838 STACKADJ(-3);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001839 READ_TIMESTAMP(intr0);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001840 err = exec_statement(f, u, v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001841 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00001842 Py_DECREF(u);
1843 Py_DECREF(v);
1844 Py_DECREF(w);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001845 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001846
Guido van Rossum374a9221991-04-04 10:40:29 +00001847 case POP_BLOCK:
1848 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001849 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001850 while (STACK_LEVEL() > b->b_level) {
1851 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001852 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001853 }
1854 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001855 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001856
Jeffrey Yasskin9063a992008-03-03 01:27:03 +00001857 PREDICTED(END_FINALLY);
Guido van Rossum374a9221991-04-04 10:40:29 +00001858 case END_FINALLY:
1859 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001860 if (PyInt_Check(v)) {
Raymond Hettinger7c958652004-04-06 10:11:10 +00001861 why = (enum why_code) PyInt_AS_LONG(v);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001862 assert(why != WHY_YIELD);
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00001863 if (why == WHY_RETURN ||
1864 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001865 retval = POP();
1866 }
Thomas Wouterse2176022007-09-20 17:35:10 +00001867 else if (PyExceptionClass_Check(v) ||
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001868 PyString_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001869 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001870 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001871 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001872 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001873 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001874 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001875 else if (v != Py_None) {
1876 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001877 "'finally' pops bad exception");
1878 why = WHY_EXCEPTION;
1879 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001880 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001881 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001882
Guido van Rossum374a9221991-04-04 10:40:29 +00001883 case BUILD_CLASS:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001884 u = TOP();
1885 v = SECOND();
1886 w = THIRD();
1887 STACKADJ(-2);
Guido van Rossum25831651993-05-19 14:50:45 +00001888 x = build_class(u, v, w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001889 SET_TOP(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001890 Py_DECREF(u);
1891 Py_DECREF(v);
1892 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001893 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001894
Guido van Rossum374a9221991-04-04 10:40:29 +00001895 case STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001896 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001897 v = POP();
Raymond Hettinger467a6982004-04-07 11:39:21 +00001898 if ((x = f->f_locals) != NULL) {
Raymond Hettinger66bd2332004-08-02 08:30:07 +00001899 if (PyDict_CheckExact(x))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001900 err = PyDict_SetItem(x, w, v);
1901 else
1902 err = PyObject_SetItem(x, w, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001903 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001904 if (err == 0) continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001905 break;
1906 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001907 PyErr_Format(PyExc_SystemError,
1908 "no locals found when storing %s",
1909 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001910 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001911
Guido van Rossum374a9221991-04-04 10:40:29 +00001912 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001913 w = GETITEM(names, oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001914 if ((x = f->f_locals) != NULL) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001915 if ((err = PyObject_DelItem(x, w)) != 0)
Raymond Hettinger467a6982004-04-07 11:39:21 +00001916 format_exc_check_arg(PyExc_NameError,
Thomas Wouterse2176022007-09-20 17:35:10 +00001917 NAME_ERROR_MSG,
1918 w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001919 break;
1920 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001921 PyErr_Format(PyExc_SystemError,
1922 "no locals when deleting %s",
1923 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001924 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001925
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001926 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001927 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001928 v = POP();
Thomas Wouterse2176022007-09-20 17:35:10 +00001929 if (PyTuple_CheckExact(v) &&
1930 PyTuple_GET_SIZE(v) == oparg) {
1931 PyObject **items = \
1932 ((PyTupleObject *)v)->ob_item;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001933 while (oparg--) {
1934 w = items[oparg];
1935 Py_INCREF(w);
1936 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001937 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001938 Py_DECREF(v);
1939 continue;
Thomas Wouterse2176022007-09-20 17:35:10 +00001940 } else if (PyList_CheckExact(v) &&
1941 PyList_GET_SIZE(v) == oparg) {
1942 PyObject **items = \
1943 ((PyListObject *)v)->ob_item;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001944 while (oparg--) {
1945 w = items[oparg];
1946 Py_INCREF(w);
1947 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001948 }
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001949 } else if (unpack_iterable(v, oparg,
Thomas Wouterse2176022007-09-20 17:35:10 +00001950 stack_pointer + oparg)) {
Tim Petersd6d010b2001-06-21 02:49:55 +00001951 stack_pointer += oparg;
Georg Brandl5cb76c12007-03-21 09:00:39 +00001952 } else {
1953 /* unpack_iterable() raised an exception */
Barry Warsawe42b18f1997-08-25 22:13:04 +00001954 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001955 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001956 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001957 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001958
Guido van Rossum374a9221991-04-04 10:40:29 +00001959 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001960 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001961 v = TOP();
1962 u = SECOND();
1963 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001964 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1965 Py_DECREF(v);
1966 Py_DECREF(u);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001967 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001968 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001969
Guido van Rossum374a9221991-04-04 10:40:29 +00001970 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001971 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001972 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001973 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1974 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001975 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001976 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001977
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001978 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001979 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001980 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001981 err = PyDict_SetItem(f->f_globals, w, v);
1982 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001983 if (err == 0) continue;
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001984 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001985
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001986 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001987 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001988 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001989 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001990 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001991 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001992
Guido van Rossum374a9221991-04-04 10:40:29 +00001993 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001994 w = GETITEM(names, oparg);
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001995 if ((v = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001996 PyErr_Format(PyExc_SystemError,
1997 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001998 PyObject_REPR(w));
Jeffrey Yasskin2d873bd2008-12-08 18:55:24 +00001999 why = WHY_EXCEPTION;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002000 break;
2001 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00002002 if (PyDict_CheckExact(v)) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00002003 x = PyDict_GetItem(v, w);
Michael W. Hudsona3711f72004-08-02 14:50:43 +00002004 Py_XINCREF(x);
2005 }
Raymond Hettinger214b1c32004-07-02 06:41:07 +00002006 else {
2007 x = PyObject_GetItem(v, w);
2008 if (x == NULL && PyErr_Occurred()) {
Thomas Wouterse2176022007-09-20 17:35:10 +00002009 if (!PyErr_ExceptionMatches(
2010 PyExc_KeyError))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00002011 break;
2012 PyErr_Clear();
2013 }
2014 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002015 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002016 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002017 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002018 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002019 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00002020 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00002021 PyExc_NameError,
Thomas Wouterse2176022007-09-20 17:35:10 +00002022 NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002023 break;
2024 }
2025 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00002026 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00002027 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002028 PUSH(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002029 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002030
Guido van Rossum374a9221991-04-04 10:40:29 +00002031 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00002032 w = GETITEM(names, oparg);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002033 if (PyString_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00002034 /* Inline the PyDict_GetItem() calls.
2035 WARNING: this is an extreme speed hack.
2036 Do not try this at home. */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002037 long hash = ((PyStringObject *)w)->ob_shash;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002038 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002039 PyDictObject *d;
Armin Rigo35f6d362006-06-01 13:19:12 +00002040 PyDictEntry *e;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002041 d = (PyDictObject *)(f->f_globals);
Armin Rigo35f6d362006-06-01 13:19:12 +00002042 e = d->ma_lookup(d, w, hash);
2043 if (e == NULL) {
2044 x = NULL;
2045 break;
2046 }
2047 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002048 if (x != NULL) {
2049 Py_INCREF(x);
2050 PUSH(x);
2051 continue;
2052 }
2053 d = (PyDictObject *)(f->f_builtins);
Armin Rigo35f6d362006-06-01 13:19:12 +00002054 e = d->ma_lookup(d, w, hash);
2055 if (e == NULL) {
2056 x = NULL;
2057 break;
2058 }
2059 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002060 if (x != NULL) {
2061 Py_INCREF(x);
2062 PUSH(x);
2063 continue;
2064 }
2065 goto load_global_error;
2066 }
2067 }
2068 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00002069 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002070 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002071 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002072 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002073 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00002074 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00002075 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002076 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002077 break;
2078 }
2079 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002080 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00002081 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00002082 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002083
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002084 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00002085 x = GETLOCAL(oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002086 if (x != NULL) {
2087 SETLOCAL(oparg, NULL);
2088 continue;
Guido van Rossum2e4c8991998-05-12 20:27:36 +00002089 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00002090 format_exc_check_arg(
2091 PyExc_UnboundLocalError,
2092 UNBOUNDLOCAL_ERROR_MSG,
2093 PyTuple_GetItem(co->co_varnames, oparg)
2094 );
2095 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002096
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002097 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00002098 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002099 Py_INCREF(x);
2100 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00002101 if (x != NULL) continue;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002102 break;
2103
2104 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00002105 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002106 w = PyCell_Get(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002107 if (w != NULL) {
2108 PUSH(w);
2109 continue;
Jeremy Hylton2524d692001-02-05 17:23:16 +00002110 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00002111 err = -1;
2112 /* Don't stomp existing exception */
2113 if (PyErr_Occurred())
2114 break;
Richard Jonescebbefc2006-05-23 18:28:17 +00002115 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
2116 v = PyTuple_GET_ITEM(co->co_cellvars,
Raymond Hettinger467a6982004-04-07 11:39:21 +00002117 oparg);
2118 format_exc_check_arg(
2119 PyExc_UnboundLocalError,
2120 UNBOUNDLOCAL_ERROR_MSG,
2121 v);
2122 } else {
Thomas Wouterse2176022007-09-20 17:35:10 +00002123 v = PyTuple_GET_ITEM(co->co_freevars, oparg -
2124 PyTuple_GET_SIZE(co->co_cellvars));
2125 format_exc_check_arg(PyExc_NameError,
2126 UNBOUNDFREE_ERROR_MSG, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002127 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002128 break;
2129
2130 case STORE_DEREF:
2131 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00002132 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002133 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00002134 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002135 continue;
2136
Guido van Rossum374a9221991-04-04 10:40:29 +00002137 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00002138 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002139 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002140 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002141 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002142 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002143 }
2144 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002145 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002146 }
2147 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002148
Guido van Rossum374a9221991-04-04 10:40:29 +00002149 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00002150 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002151 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002152 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002153 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00002154 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002155 }
2156 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002157 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002158 }
2159 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002160
Guido van Rossum374a9221991-04-04 10:40:29 +00002161 case BUILD_MAP:
Raymond Hettingerfd7ed402007-12-18 21:24:09 +00002162 x = _PyDict_NewPresized((Py_ssize_t)oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002163 PUSH(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
Raymond Hettingereffde122007-12-18 18:26:18 +00002167 case STORE_MAP:
2168 w = TOP(); /* key */
2169 u = SECOND(); /* value */
2170 v = THIRD(); /* dict */
2171 STACKADJ(-2);
2172 assert (PyDict_CheckExact(v));
2173 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2174 Py_DECREF(u);
2175 Py_DECREF(w);
2176 if (err == 0) continue;
2177 break;
2178
Guido van Rossum374a9221991-04-04 10:40:29 +00002179 case LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00002180 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002181 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002182 x = PyObject_GetAttr(v, w);
2183 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002184 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002185 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002186 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002187
Guido van Rossum374a9221991-04-04 10:40:29 +00002188 case COMPARE_OP:
2189 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002190 v = TOP();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00002191 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00002192 /* INLINE: cmp(int, int) */
2193 register long a, b;
2194 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00002195 a = PyInt_AS_LONG(v);
2196 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00002197 switch (oparg) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00002198 case PyCmp_LT: res = a < b; break;
2199 case PyCmp_LE: res = a <= b; break;
2200 case PyCmp_EQ: res = a == b; break;
2201 case PyCmp_NE: res = a != b; break;
2202 case PyCmp_GT: res = a > b; break;
2203 case PyCmp_GE: res = a >= b; break;
2204 case PyCmp_IS: res = v == w; break;
2205 case PyCmp_IS_NOT: res = v != w; break;
Guido van Rossumc12da691997-07-17 23:12:42 +00002206 default: goto slow_compare;
2207 }
2208 x = res ? Py_True : Py_False;
2209 Py_INCREF(x);
2210 }
2211 else {
2212 slow_compare:
2213 x = cmp_outcome(oparg, v, w);
2214 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002215 Py_DECREF(v);
2216 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002217 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00002218 if (x == NULL) break;
2219 PREDICT(JUMP_IF_FALSE);
2220 PREDICT(JUMP_IF_TRUE);
2221 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002222
Guido van Rossum374a9221991-04-04 10:40:29 +00002223 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00002224 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002225 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002226 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002227 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00002228 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002229 break;
2230 }
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00002231 Py_INCREF(x);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002232 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002233 u = TOP();
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002234 if (PyInt_AsLong(u) != -1 || PyErr_Occurred())
2235 w = PyTuple_Pack(5,
2236 w,
2237 f->f_globals,
2238 f->f_locals == NULL ?
2239 Py_None : f->f_locals,
2240 v,
2241 u);
2242 else
2243 w = PyTuple_Pack(4,
2244 w,
2245 f->f_globals,
2246 f->f_locals == NULL ?
2247 Py_None : f->f_locals,
2248 v);
2249 Py_DECREF(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002250 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002251 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002252 u = POP();
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00002253 Py_DECREF(x);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002254 x = NULL;
2255 break;
2256 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002257 READ_TIMESTAMP(intr0);
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00002258 v = x;
2259 x = PyEval_CallObject(v, w);
2260 Py_DECREF(v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002261 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002262 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002263 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002264 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002265 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002266
Thomas Wouters52152252000-08-17 22:55:00 +00002267 case IMPORT_STAR:
2268 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002269 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002270 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002271 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00002272 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002273 break;
2274 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002275 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002276 err = import_all_from(x, v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002277 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002278 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00002279 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002280 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002281 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002282
Thomas Wouters52152252000-08-17 22:55:00 +00002283 case IMPORT_FROM:
Skip Montanaro496e6582002-08-06 17:47:40 +00002284 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00002285 v = TOP();
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002286 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002287 x = import_from(v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002288 READ_TIMESTAMP(intr1);
Thomas Wouters52152252000-08-17 22:55:00 +00002289 PUSH(x);
2290 if (x != NULL) continue;
2291 break;
2292
Guido van Rossum374a9221991-04-04 10:40:29 +00002293 case JUMP_FORWARD:
2294 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002295 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002296
Raymond Hettingerf606f872003-03-16 03:11:04 +00002297 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002298 case JUMP_IF_FALSE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002299 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002300 if (w == Py_True) {
2301 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002302 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002303 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002304 if (w == Py_False) {
2305 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002306 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002307 }
2308 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002309 if (err > 0)
2310 err = 0;
2311 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00002312 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002313 else
2314 break;
2315 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002316
Raymond Hettingerf606f872003-03-16 03:11:04 +00002317 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002318 case JUMP_IF_TRUE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002319 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002320 if (w == Py_False) {
2321 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002322 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002323 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002324 if (w == Py_True) {
2325 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002326 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002327 }
2328 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002329 if (err > 0) {
2330 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00002331 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002332 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002333 else if (err == 0)
2334 ;
2335 else
2336 break;
2337 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002338
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00002339 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002340 case JUMP_ABSOLUTE:
2341 JUMPTO(oparg);
Raymond Hettingerdc1d1ba2007-11-07 02:45:46 +00002342#if FAST_LOOPS
2343 /* Enabling this path speeds-up all while and for-loops by bypassing
2344 the per-loop checks for signals. By default, this should be turned-off
2345 because it prevents detection of a control-break in tight loops like
2346 "while 1: pass". Compile with this option turned-on when you need
2347 the speed-up and do not need break checking inside tight loops (ones
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00002348 that contain only instructions ending with goto fast_next_opcode).
Raymond Hettingerdc1d1ba2007-11-07 02:45:46 +00002349 */
2350 goto fast_next_opcode;
2351#else
Neil Schemenauerca2a2f12003-05-30 23:59:44 +00002352 continue;
Raymond Hettingerdc1d1ba2007-11-07 02:45:46 +00002353#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002354
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002355 case GET_ITER:
2356 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002357 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002358 x = PyObject_GetIter(v);
2359 Py_DECREF(v);
2360 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002361 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002362 PREDICT(FOR_ITER);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002363 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002364 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002365 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002366 break;
2367
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002368 PREDICTED_WITH_ARG(FOR_ITER);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002369 case FOR_ITER:
2370 /* before: [iter]; after: [iter, iter()] *or* [] */
2371 v = TOP();
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002372 x = (*v->ob_type->tp_iternext)(v);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002373 if (x != NULL) {
2374 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002375 PREDICT(STORE_FAST);
2376 PREDICT(UNPACK_SEQUENCE);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002377 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002378 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002379 if (PyErr_Occurred()) {
Thomas Wouterse2176022007-09-20 17:35:10 +00002380 if (!PyErr_ExceptionMatches(
2381 PyExc_StopIteration))
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002382 break;
2383 PyErr_Clear();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002384 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002385 /* iterator ended normally */
2386 x = v = POP();
2387 Py_DECREF(v);
2388 JUMPBY(oparg);
2389 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002390
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002391 case BREAK_LOOP:
2392 why = WHY_BREAK;
2393 goto fast_block_end;
2394
2395 case CONTINUE_LOOP:
2396 retval = PyInt_FromLong(oparg);
Neal Norwitz02104df2006-05-19 06:31:23 +00002397 if (!retval) {
2398 x = NULL;
2399 break;
2400 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002401 why = WHY_CONTINUE;
2402 goto fast_block_end;
2403
Guido van Rossum374a9221991-04-04 10:40:29 +00002404 case SETUP_LOOP:
2405 case SETUP_EXCEPT:
2406 case SETUP_FINALLY:
Thomas Wouterse2176022007-09-20 17:35:10 +00002407 /* NOTE: If you add any new block-setup opcodes that
2408 are not try/except/finally handlers, you may need
2409 to update the PyGen_NeedsFinalizing() function.
2410 */
Phillip J. Eby2ba96612006-04-10 17:51:05 +00002411
Guido van Rossumb209a111997-04-29 18:18:01 +00002412 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002413 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002414 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002415
Guido van Rossumc2e20742006-02-27 22:32:47 +00002416 case WITH_CLEANUP:
2417 {
Nick Coghlan7af53be2008-03-07 14:13:28 +00002418 /* At the top of the stack are 1-3 values indicating
2419 how/why we entered the finally clause:
2420 - TOP = None
2421 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2422 - TOP = WHY_*; no retval below it
2423 - (TOP, SECOND, THIRD) = exc_info()
2424 Below them is EXIT, the context.__exit__ bound method.
Guido van Rossumc2e20742006-02-27 22:32:47 +00002425 In the last case, we must call
Nick Coghlan7af53be2008-03-07 14:13:28 +00002426 EXIT(TOP, SECOND, THIRD)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002427 otherwise we must call
Nick Coghlan7af53be2008-03-07 14:13:28 +00002428 EXIT(None, None, None)
2429
2430 In all cases, we remove EXIT from the stack, leaving
2431 the rest in the same order.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002432
2433 In addition, if the stack represents an exception,
Guido van Rossumf6694362006-03-10 02:28:35 +00002434 *and* the function call returns a 'true' value, we
2435 "zap" this information, to prevent END_FINALLY from
2436 re-raising the exception. (But non-local gotos
2437 should still be resumed.)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002438 */
Tim Peters7df5e7f2006-05-26 23:14:37 +00002439
Nick Coghlan7af53be2008-03-07 14:13:28 +00002440 PyObject *exit_func;
2441
2442 u = POP();
2443 if (u == Py_None) {
2444 exit_func = TOP();
2445 SET_TOP(u);
2446 v = w = Py_None;
2447 }
2448 else if (PyInt_Check(u)) {
2449 switch(PyInt_AS_LONG(u)) {
2450 case WHY_RETURN:
2451 case WHY_CONTINUE:
2452 /* Retval in TOP. */
2453 exit_func = SECOND();
2454 SET_SECOND(TOP());
2455 SET_TOP(u);
2456 break;
2457 default:
2458 exit_func = TOP();
2459 SET_TOP(u);
2460 break;
2461 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002462 u = v = w = Py_None;
2463 }
2464 else {
Nick Coghlan7af53be2008-03-07 14:13:28 +00002465 v = TOP();
2466 w = SECOND();
2467 exit_func = THIRD();
2468 SET_TOP(u);
2469 SET_SECOND(v);
2470 SET_THIRD(w);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002471 }
Guido van Rossumf6694362006-03-10 02:28:35 +00002472 /* XXX Not the fastest way to call it... */
Nick Coghlan7af53be2008-03-07 14:13:28 +00002473 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2474 NULL);
Amaury Forgeot d'Arcad9b5992008-12-10 23:22:49 +00002475 Py_DECREF(exit_func);
2476 if (x == NULL)
Guido van Rossumf6694362006-03-10 02:28:35 +00002477 break; /* Go to error exit */
Amaury Forgeot d'Arcad9b5992008-12-10 23:22:49 +00002478
2479 if (u != Py_None)
2480 err = PyObject_IsTrue(x);
2481 else
2482 err = 0;
2483 Py_DECREF(x);
2484
2485 if (err < 0)
2486 break; /* Go to error exit */
2487 else if (err > 0) {
2488 err = 0;
Guido van Rossumf6694362006-03-10 02:28:35 +00002489 /* There was an exception and a true return */
Nick Coghlan7af53be2008-03-07 14:13:28 +00002490 STACKADJ(-2);
Guido van Rossumf6694362006-03-10 02:28:35 +00002491 Py_INCREF(Py_None);
2492 SET_TOP(Py_None);
Guido van Rossumf6694362006-03-10 02:28:35 +00002493 Py_DECREF(u);
2494 Py_DECREF(v);
2495 Py_DECREF(w);
2496 } else {
Nick Coghlan7af53be2008-03-07 14:13:28 +00002497 /* The stack was rearranged to remove EXIT
2498 above. Let END_FINALLY do its thing */
Guido van Rossumf6694362006-03-10 02:28:35 +00002499 }
Jeffrey Yasskin9063a992008-03-03 01:27:03 +00002500 PREDICT(END_FINALLY);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002501 break;
2502 }
2503
Guido van Rossumf10570b1995-07-07 22:53:21 +00002504 case CALL_FUNCTION:
Armin Rigo8817fcd2004-06-17 10:22:40 +00002505 {
2506 PyObject **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002507 PCALL(PCALL_ALL);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002508 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002509#ifdef WITH_TSC
Armin Rigo8817fcd2004-06-17 10:22:40 +00002510 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002511#else
Armin Rigo8817fcd2004-06-17 10:22:40 +00002512 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002513#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002514 stack_pointer = sp;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002515 PUSH(x);
2516 if (x != NULL)
2517 continue;
2518 break;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002519 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002520
Jeremy Hylton76901512000-03-28 23:49:17 +00002521 case CALL_FUNCTION_VAR:
2522 case CALL_FUNCTION_KW:
2523 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002524 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002525 int na = oparg & 0xff;
2526 int nk = (oparg>>8) & 0xff;
2527 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002528 int n = na + 2 * nk;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002529 PyObject **pfunc, *func, **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002530 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002531 if (flags & CALL_FLAG_VAR)
2532 n++;
2533 if (flags & CALL_FLAG_KW)
2534 n++;
2535 pfunc = stack_pointer - n - 1;
2536 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002537
Guido van Rossumac7be682001-01-17 15:42:30 +00002538 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002539 && PyMethod_GET_SELF(func) != NULL) {
2540 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002541 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002542 func = PyMethod_GET_FUNCTION(func);
2543 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002544 Py_DECREF(*pfunc);
2545 *pfunc = self;
2546 na++;
2547 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002548 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002549 Py_INCREF(func);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002550 sp = stack_pointer;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002551 READ_TIMESTAMP(intr0);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002552 x = ext_do_call(func, &sp, flags, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002553 READ_TIMESTAMP(intr1);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002554 stack_pointer = sp;
Jeremy Hylton76901512000-03-28 23:49:17 +00002555 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002556
Jeremy Hylton76901512000-03-28 23:49:17 +00002557 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002558 w = POP();
2559 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002560 }
2561 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002562 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002563 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002564 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002565 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002566
Guido van Rossum681d79a1995-07-18 14:51:37 +00002567 case MAKE_FUNCTION:
2568 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002569 x = PyFunction_New(v, f->f_globals);
2570 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002571 /* XXX Maybe this should be a separate opcode? */
2572 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002573 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002574 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002575 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002576 x = NULL;
2577 break;
2578 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002579 while (--oparg >= 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002580 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002581 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002582 }
2583 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002584 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002585 }
2586 PUSH(x);
2587 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002588
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002589 case MAKE_CLOSURE:
2590 {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002591 v = POP(); /* code object */
2592 x = PyFunction_New(v, f->f_globals);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002593 Py_DECREF(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002594 if (x != NULL) {
2595 v = POP();
Jeffrey Yasskin2d873bd2008-12-08 18:55:24 +00002596 if (PyFunction_SetClosure(x, v) != 0) {
2597 /* Can't happen unless bytecode is corrupt. */
2598 why = WHY_EXCEPTION;
2599 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002600 Py_DECREF(v);
2601 }
2602 if (x != NULL && oparg > 0) {
2603 v = PyTuple_New(oparg);
2604 if (v == NULL) {
2605 Py_DECREF(x);
2606 x = NULL;
2607 break;
2608 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002609 while (--oparg >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002610 w = POP();
2611 PyTuple_SET_ITEM(v, oparg, w);
2612 }
Jeffrey Yasskin2d873bd2008-12-08 18:55:24 +00002613 if (PyFunction_SetDefaults(x, v) != 0) {
2614 /* Can't happen unless
2615 PyFunction_SetDefaults changes. */
2616 why = WHY_EXCEPTION;
2617 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002618 Py_DECREF(v);
2619 }
2620 PUSH(x);
2621 break;
2622 }
2623
Guido van Rossum8861b741996-07-30 16:49:37 +00002624 case BUILD_SLICE:
2625 if (oparg == 3)
2626 w = POP();
2627 else
2628 w = NULL;
2629 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002630 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002631 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002632 Py_DECREF(u);
2633 Py_DECREF(v);
2634 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002635 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002636 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002637 break;
2638
Fred Drakeef8ace32000-08-24 00:32:09 +00002639 case EXTENDED_ARG:
2640 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002641 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002642 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002643
Guido van Rossum374a9221991-04-04 10:40:29 +00002644 default:
2645 fprintf(stderr,
2646 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002647 PyCode_Addr2Line(f->f_code, f->f_lasti),
2648 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002649 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002650 why = WHY_EXCEPTION;
2651 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002652
2653#ifdef CASE_TOO_BIG
2654 }
2655#endif
2656
Guido van Rossum374a9221991-04-04 10:40:29 +00002657 } /* switch */
2658
2659 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002660
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002661 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002662
Guido van Rossum374a9221991-04-04 10:40:29 +00002663 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002664
Guido van Rossum374a9221991-04-04 10:40:29 +00002665 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002666 if (err == 0 && x != NULL) {
2667#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002668 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002669 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002670 fprintf(stderr,
2671 "XXX undetected error\n");
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002672 else {
2673#endif
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002674 READ_TIMESTAMP(loop1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002675 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002676#ifdef CHECKEXC
2677 }
2678#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +00002679 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002680 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002681 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002682 err = 0;
2683 }
2684
Guido van Rossum374a9221991-04-04 10:40:29 +00002685 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002686
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002687 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002688 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002689 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002690 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002691 why = WHY_EXCEPTION;
2692 }
2693 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002694#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002695 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002696 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002697 if (PyErr_Occurred()) {
Neal Norwitz8250fbe2008-01-27 17:12:15 +00002698 char buf[128];
Jeremy Hylton904ed862003-11-05 17:29:35 +00002699 sprintf(buf, "Stack unwind with exception "
2700 "set and why=%d", why);
2701 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002702 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002703 }
2704#endif
2705
2706 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002707
Guido van Rossum374a9221991-04-04 10:40:29 +00002708 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002709 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002710
Fred Drake8f51f542001-10-04 14:48:42 +00002711 if (tstate->c_tracefunc != NULL)
2712 call_exc_trace(tstate->c_tracefunc,
2713 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002714 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002715
Guido van Rossum374a9221991-04-04 10:40:29 +00002716 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002717
Guido van Rossum374a9221991-04-04 10:40:29 +00002718 if (why == WHY_RERAISE)
2719 why = WHY_EXCEPTION;
2720
2721 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002722
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002723fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002724 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002725 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002726
Tim Peters8a5c3c72004-04-05 19:36:21 +00002727 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002728 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2729 /* For a continue inside a try block,
2730 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002731 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2732 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002733 why = WHY_NOT;
2734 JUMPTO(PyInt_AS_LONG(retval));
2735 Py_DECREF(retval);
2736 break;
2737 }
2738
Guido van Rossum374a9221991-04-04 10:40:29 +00002739 while (STACK_LEVEL() > b->b_level) {
2740 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002741 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002742 }
2743 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2744 why = WHY_NOT;
2745 JUMPTO(b->b_handler);
2746 break;
2747 }
2748 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002749 (b->b_type == SETUP_EXCEPT &&
2750 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002751 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002752 PyObject *exc, *val, *tb;
2753 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002754 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002755 val = Py_None;
2756 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002757 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002758 /* Make the raw exception data
2759 available to the handler,
2760 so a program can emulate the
2761 Python main loop. Don't do
2762 this for 'finally'. */
2763 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002764 PyErr_NormalizeException(
2765 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002766 set_exc_info(tstate,
2767 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002768 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002769 if (tb == NULL) {
2770 Py_INCREF(Py_None);
2771 PUSH(Py_None);
2772 } else
2773 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002774 PUSH(val);
2775 PUSH(exc);
2776 }
2777 else {
Raymond Hettinger06032cb2004-04-06 09:37:35 +00002778 if (why & (WHY_RETURN | WHY_CONTINUE))
Guido van Rossum374a9221991-04-04 10:40:29 +00002779 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002780 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002781 PUSH(v);
2782 }
2783 why = WHY_NOT;
2784 JUMPTO(b->b_handler);
2785 break;
2786 }
2787 } /* unwind stack */
2788
2789 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002790
Guido van Rossum374a9221991-04-04 10:40:29 +00002791 if (why != WHY_NOT)
2792 break;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002793 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002794
Guido van Rossum374a9221991-04-04 10:40:29 +00002795 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002796
Tim Peters8a5c3c72004-04-05 19:36:21 +00002797 assert(why != WHY_YIELD);
2798 /* Pop remaining stack entries. */
2799 while (!EMPTY()) {
2800 v = POP();
2801 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002802 }
2803
Tim Peters8a5c3c72004-04-05 19:36:21 +00002804 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002805 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002806
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002807fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002808 if (tstate->use_tracing) {
Barry Warsawe2eca0b2005-08-15 18:14:19 +00002809 if (tstate->c_tracefunc) {
2810 if (why == WHY_RETURN || why == WHY_YIELD) {
2811 if (call_trace(tstate->c_tracefunc,
2812 tstate->c_traceobj, f,
2813 PyTrace_RETURN, retval)) {
2814 Py_XDECREF(retval);
2815 retval = NULL;
2816 why = WHY_EXCEPTION;
2817 }
2818 }
2819 else if (why == WHY_EXCEPTION) {
2820 call_trace_protected(tstate->c_tracefunc,
2821 tstate->c_traceobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002822 PyTrace_RETURN, NULL);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002823 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002824 }
Fred Drake8f51f542001-10-04 14:48:42 +00002825 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002826 if (why == WHY_EXCEPTION)
2827 call_trace_protected(tstate->c_profilefunc,
2828 tstate->c_profileobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002829 PyTrace_RETURN, NULL);
Fred Drake4ec5d562001-10-04 19:26:43 +00002830 else if (call_trace(tstate->c_profilefunc,
2831 tstate->c_profileobj, f,
2832 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002833 Py_XDECREF(retval);
2834 retval = NULL;
2835 why = WHY_EXCEPTION;
2836 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002837 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002838 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002839
Tim Peters7df5e7f2006-05-26 23:14:37 +00002840 if (tstate->frame->f_exc_type != NULL)
2841 reset_exc_info(tstate);
2842 else {
2843 assert(tstate->frame->f_exc_value == NULL);
2844 assert(tstate->frame->f_exc_traceback == NULL);
2845 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00002846
Tim Peters5ca576e2001-06-18 22:08:13 +00002847 /* pop frame */
Thomas Woutersae406c62007-09-19 17:27:43 +00002848exit_eval_frame:
Armin Rigo2b3eb402003-10-28 12:05:48 +00002849 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002850 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002851
Guido van Rossum96a42c81992-01-12 02:29:51 +00002852 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002853}
2854
Guido van Rossumc2e20742006-02-27 22:32:47 +00002855/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002856 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00002857 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002858
Tim Peters6d6c1a32001-08-02 04:15:00 +00002859PyObject *
2860PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002861 PyObject **args, int argcount, PyObject **kws, int kwcount,
2862 PyObject **defs, int defcount, PyObject *closure)
2863{
2864 register PyFrameObject *f;
2865 register PyObject *retval = NULL;
2866 register PyObject **fastlocals, **freevars;
2867 PyThreadState *tstate = PyThreadState_GET();
2868 PyObject *x, *u;
2869
2870 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00002871 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002872 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002873 return NULL;
2874 }
2875
Neal Norwitzdf6a6492006-08-13 18:10:10 +00002876 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00002877 assert(globals != NULL);
2878 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002879 if (f == NULL)
2880 return NULL;
2881
2882 fastlocals = f->f_localsplus;
Richard Jonescebbefc2006-05-23 18:28:17 +00002883 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00002884
2885 if (co->co_argcount > 0 ||
2886 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2887 int i;
2888 int n = argcount;
2889 PyObject *kwdict = NULL;
2890 if (co->co_flags & CO_VARKEYWORDS) {
2891 kwdict = PyDict_New();
2892 if (kwdict == NULL)
2893 goto fail;
2894 i = co->co_argcount;
2895 if (co->co_flags & CO_VARARGS)
2896 i++;
2897 SETLOCAL(i, kwdict);
2898 }
2899 if (argcount > co->co_argcount) {
2900 if (!(co->co_flags & CO_VARARGS)) {
2901 PyErr_Format(PyExc_TypeError,
2902 "%.200s() takes %s %d "
2903 "%sargument%s (%d given)",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002904 PyString_AsString(co->co_name),
Tim Peters5ca576e2001-06-18 22:08:13 +00002905 defcount ? "at most" : "exactly",
2906 co->co_argcount,
2907 kwcount ? "non-keyword " : "",
2908 co->co_argcount == 1 ? "" : "s",
2909 argcount);
2910 goto fail;
2911 }
2912 n = co->co_argcount;
2913 }
2914 for (i = 0; i < n; i++) {
2915 x = args[i];
2916 Py_INCREF(x);
2917 SETLOCAL(i, x);
2918 }
2919 if (co->co_flags & CO_VARARGS) {
2920 u = PyTuple_New(argcount - n);
2921 if (u == NULL)
2922 goto fail;
2923 SETLOCAL(co->co_argcount, u);
2924 for (i = n; i < argcount; i++) {
2925 x = args[i];
2926 Py_INCREF(x);
2927 PyTuple_SET_ITEM(u, i-n, x);
2928 }
2929 }
2930 for (i = 0; i < kwcount; i++) {
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00002931 PyObject **co_varnames;
Tim Peters5ca576e2001-06-18 22:08:13 +00002932 PyObject *keyword = kws[2*i];
2933 PyObject *value = kws[2*i + 1];
2934 int j;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002935 if (keyword == NULL || !PyString_Check(keyword)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002936 PyErr_Format(PyExc_TypeError,
2937 "%.200s() keywords must be strings",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002938 PyString_AsString(co->co_name));
Tim Peters5ca576e2001-06-18 22:08:13 +00002939 goto fail;
2940 }
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00002941 /* Speed hack: do raw pointer compares. As names are
2942 normally interned this should almost always hit. */
2943 co_varnames = PySequence_Fast_ITEMS(co->co_varnames);
Tim Peters5ca576e2001-06-18 22:08:13 +00002944 for (j = 0; j < co->co_argcount; j++) {
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00002945 PyObject *nm = co_varnames[j];
2946 if (nm == keyword)
2947 goto kw_found;
2948 }
2949 /* Slow fallback, just in case */
2950 for (j = 0; j < co->co_argcount; j++) {
2951 PyObject *nm = co_varnames[j];
Tim Peters5ca576e2001-06-18 22:08:13 +00002952 int cmp = PyObject_RichCompareBool(
2953 keyword, nm, Py_EQ);
2954 if (cmp > 0)
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00002955 goto kw_found;
Tim Peters5ca576e2001-06-18 22:08:13 +00002956 else if (cmp < 0)
2957 goto fail;
2958 }
2959 /* Check errors from Compare */
2960 if (PyErr_Occurred())
2961 goto fail;
2962 if (j >= co->co_argcount) {
2963 if (kwdict == NULL) {
2964 PyErr_Format(PyExc_TypeError,
2965 "%.200s() got an unexpected "
2966 "keyword argument '%.400s'",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002967 PyString_AsString(co->co_name),
2968 PyString_AsString(keyword));
Tim Peters5ca576e2001-06-18 22:08:13 +00002969 goto fail;
2970 }
2971 PyDict_SetItem(kwdict, keyword, value);
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00002972 continue;
Tim Peters5ca576e2001-06-18 22:08:13 +00002973 }
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00002974kw_found:
2975 if (GETLOCAL(j) != NULL) {
2976 PyErr_Format(PyExc_TypeError,
2977 "%.200s() got multiple "
2978 "values for keyword "
2979 "argument '%.400s'",
2980 PyString_AsString(co->co_name),
2981 PyString_AsString(keyword));
2982 goto fail;
Tim Peters5ca576e2001-06-18 22:08:13 +00002983 }
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00002984 Py_INCREF(value);
2985 SETLOCAL(j, value);
Tim Peters5ca576e2001-06-18 22:08:13 +00002986 }
2987 if (argcount < co->co_argcount) {
2988 int m = co->co_argcount - defcount;
2989 for (i = argcount; i < m; i++) {
2990 if (GETLOCAL(i) == NULL) {
2991 PyErr_Format(PyExc_TypeError,
2992 "%.200s() takes %s %d "
2993 "%sargument%s (%d given)",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002994 PyString_AsString(co->co_name),
Tim Peters5ca576e2001-06-18 22:08:13 +00002995 ((co->co_flags & CO_VARARGS) ||
2996 defcount) ? "at least"
2997 : "exactly",
2998 m, kwcount ? "non-keyword " : "",
2999 m == 1 ? "" : "s", i);
3000 goto fail;
3001 }
3002 }
3003 if (n > m)
3004 i = n - m;
3005 else
3006 i = 0;
3007 for (; i < defcount; i++) {
3008 if (GETLOCAL(m+i) == NULL) {
3009 PyObject *def = defs[i];
3010 Py_INCREF(def);
3011 SETLOCAL(m+i, def);
3012 }
3013 }
3014 }
3015 }
3016 else {
3017 if (argcount > 0 || kwcount > 0) {
3018 PyErr_Format(PyExc_TypeError,
3019 "%.200s() takes no arguments (%d given)",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003020 PyString_AsString(co->co_name),
Tim Peters5ca576e2001-06-18 22:08:13 +00003021 argcount + kwcount);
3022 goto fail;
3023 }
3024 }
3025 /* Allocate and initialize storage for cell vars, and copy free
3026 vars into frame. This isn't too efficient right now. */
Richard Jonescebbefc2006-05-23 18:28:17 +00003027 if (PyTuple_GET_SIZE(co->co_cellvars)) {
Neal Norwitz245ce8d2006-06-12 02:16:10 +00003028 int i, j, nargs, found;
Tim Peters5ca576e2001-06-18 22:08:13 +00003029 char *cellname, *argname;
3030 PyObject *c;
3031
3032 nargs = co->co_argcount;
3033 if (co->co_flags & CO_VARARGS)
3034 nargs++;
3035 if (co->co_flags & CO_VARKEYWORDS)
3036 nargs++;
3037
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003038 /* Initialize each cell var, taking into account
3039 cell vars that are initialized from arguments.
3040
3041 Should arrange for the compiler to put cellvars
3042 that are arguments at the beginning of the cellvars
3043 list so that we can march over it more efficiently?
3044 */
Richard Jonescebbefc2006-05-23 18:28:17 +00003045 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003046 cellname = PyString_AS_STRING(
Tim Peters5ca576e2001-06-18 22:08:13 +00003047 PyTuple_GET_ITEM(co->co_cellvars, i));
3048 found = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003049 for (j = 0; j < nargs; j++) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003050 argname = PyString_AS_STRING(
Tim Peters5ca576e2001-06-18 22:08:13 +00003051 PyTuple_GET_ITEM(co->co_varnames, j));
3052 if (strcmp(cellname, argname) == 0) {
3053 c = PyCell_New(GETLOCAL(j));
3054 if (c == NULL)
3055 goto fail;
Richard Jonescebbefc2006-05-23 18:28:17 +00003056 GETLOCAL(co->co_nlocals + i) = c;
Tim Peters5ca576e2001-06-18 22:08:13 +00003057 found = 1;
3058 break;
3059 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003060 }
3061 if (found == 0) {
3062 c = PyCell_New(NULL);
3063 if (c == NULL)
3064 goto fail;
Richard Jonescebbefc2006-05-23 18:28:17 +00003065 SETLOCAL(co->co_nlocals + i, c);
Tim Peters5ca576e2001-06-18 22:08:13 +00003066 }
3067 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003068 }
Richard Jonescebbefc2006-05-23 18:28:17 +00003069 if (PyTuple_GET_SIZE(co->co_freevars)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003070 int i;
Richard Jonescebbefc2006-05-23 18:28:17 +00003071 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003072 PyObject *o = PyTuple_GET_ITEM(closure, i);
3073 Py_INCREF(o);
Richard Jonescebbefc2006-05-23 18:28:17 +00003074 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Tim Peters5ca576e2001-06-18 22:08:13 +00003075 }
3076 }
3077
Tim Peters5ca576e2001-06-18 22:08:13 +00003078 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003079 /* Don't need to keep the reference to f_back, it will be set
3080 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00003081 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003082 f->f_back = NULL;
3083
Jeremy Hylton985eba52003-02-05 23:13:00 +00003084 PCALL(PCALL_GENERATOR);
3085
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003086 /* Create a new generator that owns the ready to run frame
3087 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00003088 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00003089 }
3090
Thomas Woutersae406c62007-09-19 17:27:43 +00003091 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003092
Thomas Woutersae406c62007-09-19 17:27:43 +00003093fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003094
Tim Petersb13680b2001-11-27 23:29:29 +00003095 /* decref'ing the frame can cause __del__ methods to get invoked,
3096 which can call back into Python. While we're done with the
3097 current Python frame (f), the associated C stack is still in use,
3098 so recursion_depth must be boosted for the duration.
3099 */
3100 assert(tstate != NULL);
3101 ++tstate->recursion_depth;
Thomas Woutersae406c62007-09-19 17:27:43 +00003102 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00003103 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00003104 return retval;
3105}
3106
3107
Guido van Rossumc9fbb722003-03-01 03:36:33 +00003108/* Implementation notes for set_exc_info() and reset_exc_info():
3109
3110- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
3111 'exc_traceback'. These always travel together.
3112
3113- tstate->curexc_ZZZ is the "hot" exception that is set by
3114 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
3115
3116- Once an exception is caught by an except clause, it is transferred
3117 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
3118 can pick it up. This is the primary task of set_exc_info().
Tim Peters7df5e7f2006-05-26 23:14:37 +00003119 XXX That can't be right: set_exc_info() doesn't look at tstate->curexc_ZZZ.
Guido van Rossumc9fbb722003-03-01 03:36:33 +00003120
3121- Now let me explain the complicated dance with frame->f_exc_ZZZ.
3122
3123 Long ago, when none of this existed, there were just a few globals:
3124 one set corresponding to the "hot" exception, and one set
3125 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
3126 globals; they were simply stored as sys.exc_ZZZ. For backwards
3127 compatibility, they still are!) The problem was that in code like
3128 this:
3129
3130 try:
3131 "something that may fail"
3132 except "some exception":
3133 "do something else first"
3134 "print the exception from sys.exc_ZZZ."
3135
3136 if "do something else first" invoked something that raised and caught
3137 an exception, sys.exc_ZZZ were overwritten. That was a frequent
3138 cause of subtle bugs. I fixed this by changing the semantics as
3139 follows:
3140
3141 - Within one frame, sys.exc_ZZZ will hold the last exception caught
3142 *in that frame*.
3143
3144 - But initially, and as long as no exception is caught in a given
3145 frame, sys.exc_ZZZ will hold the last exception caught in the
3146 previous frame (or the frame before that, etc.).
3147
3148 The first bullet fixed the bug in the above example. The second
3149 bullet was for backwards compatibility: it was (and is) common to
3150 have a function that is called when an exception is caught, and to
3151 have that function access the caught exception via sys.exc_ZZZ.
3152 (Example: traceback.print_exc()).
3153
3154 At the same time I fixed the problem that sys.exc_ZZZ weren't
3155 thread-safe, by introducing sys.exc_info() which gets it from tstate;
3156 but that's really a separate improvement.
3157
3158 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
3159 variables to what they were before the current frame was called. The
3160 set_exc_info() function saves them on the frame so that
3161 reset_exc_info() can restore them. The invariant is that
3162 frame->f_exc_ZZZ is NULL iff the current frame never caught an
3163 exception (where "catching" an exception applies only to successful
3164 except clauses); and if the current frame ever caught an exception,
3165 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
3166 at the start of the current frame.
3167
3168*/
3169
Fredrik Lundh7a830892006-05-27 10:39:48 +00003170static void
Guido van Rossumac7be682001-01-17 15:42:30 +00003171set_exc_info(PyThreadState *tstate,
3172 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00003173{
Tim Peters7df5e7f2006-05-26 23:14:37 +00003174 PyFrameObject *frame = tstate->frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003175 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00003176
Tim Peters7df5e7f2006-05-26 23:14:37 +00003177 assert(type != NULL);
3178 assert(frame != NULL);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003179 if (frame->f_exc_type == NULL) {
Tim Peters7df5e7f2006-05-26 23:14:37 +00003180 assert(frame->f_exc_value == NULL);
3181 assert(frame->f_exc_traceback == NULL);
3182 /* This frame didn't catch an exception before. */
3183 /* Save previous exception of this thread in this frame. */
Guido van Rossuma027efa1997-05-05 20:56:21 +00003184 if (tstate->exc_type == NULL) {
Tim Peters7df5e7f2006-05-26 23:14:37 +00003185 /* XXX Why is this set to Py_None? */
Guido van Rossuma027efa1997-05-05 20:56:21 +00003186 Py_INCREF(Py_None);
3187 tstate->exc_type = Py_None;
3188 }
Tim Peters7df5e7f2006-05-26 23:14:37 +00003189 Py_INCREF(tstate->exc_type);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003190 Py_XINCREF(tstate->exc_value);
3191 Py_XINCREF(tstate->exc_traceback);
3192 frame->f_exc_type = tstate->exc_type;
3193 frame->f_exc_value = tstate->exc_value;
3194 frame->f_exc_traceback = tstate->exc_traceback;
3195 }
Tim Peters7df5e7f2006-05-26 23:14:37 +00003196 /* Set new exception for this thread. */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003197 tmp_type = tstate->exc_type;
3198 tmp_value = tstate->exc_value;
3199 tmp_tb = tstate->exc_traceback;
Tim Peters7df5e7f2006-05-26 23:14:37 +00003200 Py_INCREF(type);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003201 Py_XINCREF(value);
3202 Py_XINCREF(tb);
3203 tstate->exc_type = type;
3204 tstate->exc_value = value;
3205 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003206 Py_XDECREF(tmp_type);
3207 Py_XDECREF(tmp_value);
3208 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003209 /* For b/w compatibility */
3210 PySys_SetObject("exc_type", type);
3211 PySys_SetObject("exc_value", value);
3212 PySys_SetObject("exc_traceback", tb);
3213}
3214
Fredrik Lundh7a830892006-05-27 10:39:48 +00003215static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003216reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00003217{
3218 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003219 PyObject *tmp_type, *tmp_value, *tmp_tb;
Tim Peters7df5e7f2006-05-26 23:14:37 +00003220
3221 /* It's a precondition that the thread state's frame caught an
3222 * exception -- verify in a debug build.
3223 */
3224 assert(tstate != NULL);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003225 frame = tstate->frame;
Tim Peters7df5e7f2006-05-26 23:14:37 +00003226 assert(frame != NULL);
3227 assert(frame->f_exc_type != NULL);
3228
3229 /* Copy the frame's exception info back to the thread state. */
3230 tmp_type = tstate->exc_type;
3231 tmp_value = tstate->exc_value;
3232 tmp_tb = tstate->exc_traceback;
3233 Py_INCREF(frame->f_exc_type);
3234 Py_XINCREF(frame->f_exc_value);
3235 Py_XINCREF(frame->f_exc_traceback);
3236 tstate->exc_type = frame->f_exc_type;
3237 tstate->exc_value = frame->f_exc_value;
3238 tstate->exc_traceback = frame->f_exc_traceback;
3239 Py_XDECREF(tmp_type);
3240 Py_XDECREF(tmp_value);
3241 Py_XDECREF(tmp_tb);
3242
3243 /* For b/w compatibility */
3244 PySys_SetObject("exc_type", frame->f_exc_type);
3245 PySys_SetObject("exc_value", frame->f_exc_value);
3246 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
3247
3248 /* Clear the frame's exception info. */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003249 tmp_type = frame->f_exc_type;
3250 tmp_value = frame->f_exc_value;
3251 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003252 frame->f_exc_type = NULL;
3253 frame->f_exc_value = NULL;
3254 frame->f_exc_traceback = NULL;
Tim Peters7df5e7f2006-05-26 23:14:37 +00003255 Py_DECREF(tmp_type);
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003256 Py_XDECREF(tmp_value);
3257 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003258}
3259
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003260/* Logic for the raise statement (too complicated for inlining).
3261 This *consumes* a reference count to each of its arguments. */
Fredrik Lundh7a830892006-05-27 10:39:48 +00003262static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003263do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003264{
Guido van Rossumd295f121998-04-09 21:39:57 +00003265 if (type == NULL) {
3266 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003267 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumd295f121998-04-09 21:39:57 +00003268 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
3269 value = tstate->exc_value;
3270 tb = tstate->exc_traceback;
3271 Py_XINCREF(type);
3272 Py_XINCREF(value);
3273 Py_XINCREF(tb);
3274 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003275
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003276 /* We support the following forms of raise:
3277 raise <class>, <classinstance>
3278 raise <class>, <argument tuple>
3279 raise <class>, None
3280 raise <class>, <argument>
3281 raise <classinstance>, None
3282 raise <string>, <object>
3283 raise <string>, None
3284
3285 An omitted second argument is the same as None.
3286
3287 In addition, raise <tuple>, <anything> is the same as
3288 raising the tuple's first item (and it better have one!);
3289 this rule is applied recursively.
3290
3291 Finally, an optional third argument can be supplied, which
3292 gives the traceback to be substituted (useful when
3293 re-raising an exception after examining it). */
3294
3295 /* First, check the traceback argument, replacing None with
3296 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00003297 if (tb == Py_None) {
3298 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003299 tb = NULL;
3300 }
3301 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003302 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003303 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003304 goto raise_error;
3305 }
3306
3307 /* Next, replace a missing value with None */
3308 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003309 value = Py_None;
3310 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003311 }
3312
3313 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00003314 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
3315 PyObject *tmp = type;
3316 type = PyTuple_GET_ITEM(type, 0);
3317 Py_INCREF(type);
3318 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003319 }
3320
Brett Cannon129bd522007-01-30 21:34:36 +00003321 if (PyExceptionClass_Check(type))
Barry Warsaw4249f541997-08-22 21:26:19 +00003322 PyErr_NormalizeException(&type, &value, &tb);
3323
Brett Cannonbf364092006-03-01 04:25:17 +00003324 else if (PyExceptionInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003325 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00003326 if (value != Py_None) {
3327 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003328 "instance exception may not have a separate value");
3329 goto raise_error;
3330 }
3331 else {
3332 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00003333 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003334 value = type;
Brett Cannonbf364092006-03-01 04:25:17 +00003335 type = PyExceptionInstance_Class(type);
Guido van Rossumb209a111997-04-29 18:18:01 +00003336 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003337 }
3338 }
3339 else {
3340 /* Not something you can raise. You get an exception
3341 anyway, just not what you specified :-) */
Jeremy Hylton960d9482001-04-27 02:25:33 +00003342 PyErr_Format(PyExc_TypeError,
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00003343 "exceptions must be classes or instances, not %s",
3344 type->ob_type->tp_name);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003345 goto raise_error;
3346 }
Guido van Rossum504153d2008-03-18 04:26:48 +00003347
3348 assert(PyExceptionClass_Check(type));
3349 if (Py_Py3kWarningFlag && PyClass_Check(type)) {
Benjamin Peterson9f4f4812008-04-27 03:01:45 +00003350 if (PyErr_WarnEx(PyExc_DeprecationWarning,
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00003351 "exceptions must derive from BaseException "
3352 "in 3.x", 1) < 0)
Guido van Rossum504153d2008-03-18 04:26:48 +00003353 goto raise_error;
3354 }
3355
Guido van Rossumb209a111997-04-29 18:18:01 +00003356 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003357 if (tb == NULL)
3358 return WHY_EXCEPTION;
3359 else
3360 return WHY_RERAISE;
3361 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00003362 Py_XDECREF(value);
3363 Py_XDECREF(type);
3364 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003365 return WHY_EXCEPTION;
3366}
3367
Tim Petersd6d010b2001-06-21 02:49:55 +00003368/* Iterate v argcnt times and store the results on the stack (via decreasing
3369 sp). Return 1 for success, 0 if error. */
3370
Fredrik Lundh7a830892006-05-27 10:39:48 +00003371static int
Tim Petersd6d010b2001-06-21 02:49:55 +00003372unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003373{
Tim Petersd6d010b2001-06-21 02:49:55 +00003374 int i = 0;
3375 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00003376 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00003377
Tim Petersd6d010b2001-06-21 02:49:55 +00003378 assert(v != NULL);
3379
3380 it = PyObject_GetIter(v);
3381 if (it == NULL)
3382 goto Error;
3383
3384 for (; i < argcnt; i++) {
3385 w = PyIter_Next(it);
3386 if (w == NULL) {
3387 /* Iterator done, via error or exhaustion. */
3388 if (!PyErr_Occurred()) {
3389 PyErr_Format(PyExc_ValueError,
3390 "need more than %d value%s to unpack",
3391 i, i == 1 ? "" : "s");
3392 }
3393 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003394 }
3395 *--sp = w;
3396 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003397
3398 /* We better have exhausted the iterator now. */
3399 w = PyIter_Next(it);
3400 if (w == NULL) {
3401 if (PyErr_Occurred())
3402 goto Error;
3403 Py_DECREF(it);
3404 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003405 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00003406 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00003407 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00003408 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00003409Error:
Barry Warsaw91010551997-08-25 22:30:51 +00003410 for (; i > 0; i--, sp++)
3411 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00003412 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00003413 return 0;
3414}
3415
3416
Guido van Rossum96a42c81992-01-12 02:29:51 +00003417#ifdef LLTRACE
Fredrik Lundh7a830892006-05-27 10:39:48 +00003418static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003419prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003420{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003421 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003422 if (PyObject_Print(v, stdout, 0) != 0)
3423 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003424 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003425 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003426}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003427#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003428
Fredrik Lundh7a830892006-05-27 10:39:48 +00003429static void
Fred Drake5755ce62001-06-27 19:19:46 +00003430call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003431{
Guido van Rossumb209a111997-04-29 18:18:01 +00003432 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003433 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003434 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003435 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003436 value = Py_None;
3437 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003438 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003439 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003440 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003441 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003442 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003443 }
Fred Drake5755ce62001-06-27 19:19:46 +00003444 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003445 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003446 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003447 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003448 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003449 Py_XDECREF(type);
3450 Py_XDECREF(value);
3451 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003452 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003453}
3454
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +00003455static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003456call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003457 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003458{
3459 PyObject *type, *value, *traceback;
3460 int err;
3461 PyErr_Fetch(&type, &value, &traceback);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003462 err = call_trace(func, obj, frame, what, arg);
Fred Drake4ec5d562001-10-04 19:26:43 +00003463 if (err == 0)
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +00003464 {
Fred Drake4ec5d562001-10-04 19:26:43 +00003465 PyErr_Restore(type, value, traceback);
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +00003466 return 0;
3467 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003468 else {
3469 Py_XDECREF(type);
3470 Py_XDECREF(value);
3471 Py_XDECREF(traceback);
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +00003472 return -1;
Fred Drake4ec5d562001-10-04 19:26:43 +00003473 }
3474}
3475
Fredrik Lundh7a830892006-05-27 10:39:48 +00003476static int
Fred Drake5755ce62001-06-27 19:19:46 +00003477call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3478 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003479{
Fred Drake5755ce62001-06-27 19:19:46 +00003480 register PyThreadState *tstate = frame->f_tstate;
3481 int result;
3482 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003483 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003484 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003485 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003486 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003487 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3488 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003489 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003490 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003491}
3492
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003493PyObject *
3494_PyEval_CallTracing(PyObject *func, PyObject *args)
3495{
3496 PyFrameObject *frame = PyEval_GetFrame();
3497 PyThreadState *tstate = frame->f_tstate;
3498 int save_tracing = tstate->tracing;
3499 int save_use_tracing = tstate->use_tracing;
3500 PyObject *result;
3501
3502 tstate->tracing = 0;
3503 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3504 || (tstate->c_profilefunc != NULL));
3505 result = PyObject_Call(func, args, NULL);
3506 tstate->tracing = save_tracing;
3507 tstate->use_tracing = save_use_tracing;
3508 return result;
3509}
3510
Fredrik Lundh7a830892006-05-27 10:39:48 +00003511static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003512maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003513 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3514 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003515{
Michael W. Hudson006c7522002-11-08 13:08:46 +00003516 int result = 0;
3517
Jeremy Hyltona4ebc132006-04-18 14:47:00 +00003518 /* If the last instruction executed isn't in the current
3519 instruction window, reset the window. If the last
3520 instruction happens to fall at the start of a line or if it
3521 represents a jump backwards, call the trace function.
3522 */
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003523 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Thomas Woutersae406c62007-09-19 17:27:43 +00003524 int line;
3525 PyAddrPair bounds;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003526
Thomas Woutersae406c62007-09-19 17:27:43 +00003527 line = PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3528 &bounds);
3529 if (line >= 0) {
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003530 frame->f_lineno = line;
Tim Peters8a5c3c72004-04-05 19:36:21 +00003531 result = call_trace(func, obj, frame,
Michael W. Hudson006c7522002-11-08 13:08:46 +00003532 PyTrace_LINE, Py_None);
Thomas Woutersae406c62007-09-19 17:27:43 +00003533 }
3534 *instr_lb = bounds.ap_lower;
3535 *instr_ub = bounds.ap_upper;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003536 }
Armin Rigobf57a142004-03-22 19:24:58 +00003537 else if (frame->f_lasti <= *instr_prev) {
Jeremy Hyltona4ebc132006-04-18 14:47:00 +00003538 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
Armin Rigobf57a142004-03-22 19:24:58 +00003539 }
3540 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003541 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003542}
3543
Fred Drake5755ce62001-06-27 19:19:46 +00003544void
3545PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003546{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003547 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003548 PyObject *temp = tstate->c_profileobj;
3549 Py_XINCREF(arg);
3550 tstate->c_profilefunc = NULL;
3551 tstate->c_profileobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003552 /* Must make sure that tracing is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003553 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003554 Py_XDECREF(temp);
3555 tstate->c_profilefunc = func;
3556 tstate->c_profileobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003557 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003558 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003559}
3560
3561void
3562PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3563{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003564 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003565 PyObject *temp = tstate->c_traceobj;
Jeffrey Yasskinfd8a1ec2008-12-03 06:46:45 +00003566 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003567 Py_XINCREF(arg);
3568 tstate->c_tracefunc = NULL;
3569 tstate->c_traceobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003570 /* Must make sure that profiling is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003571 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003572 Py_XDECREF(temp);
3573 tstate->c_tracefunc = func;
3574 tstate->c_traceobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003575 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003576 tstate->use_tracing = ((func != NULL)
3577 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003578}
3579
Guido van Rossumb209a111997-04-29 18:18:01 +00003580PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003581PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003582{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003583 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003584 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003585 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003586 else
3587 return current_frame->f_builtins;
3588}
3589
Guido van Rossumb209a111997-04-29 18:18:01 +00003590PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003591PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003592{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003593 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003594 if (current_frame == NULL)
3595 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003596 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003597 return current_frame->f_locals;
3598}
3599
Guido van Rossumb209a111997-04-29 18:18:01 +00003600PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003601PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003602{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003603 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003604 if (current_frame == NULL)
3605 return NULL;
3606 else
3607 return current_frame->f_globals;
3608}
3609
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003610PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003611PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003612{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003613 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003614 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003615}
3616
Guido van Rossum6135a871995-01-09 17:53:26 +00003617int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003618PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003619{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003620 PyFrameObject *current_frame = PyEval_GetFrame();
Neal Norwitzb9845e72006-06-12 02:11:18 +00003621 return current_frame == NULL ? 0 : PyFrame_IsRestricted(current_frame);
Guido van Rossum6135a871995-01-09 17:53:26 +00003622}
3623
Guido van Rossumbe270261997-05-22 22:26:18 +00003624int
Tim Peters5ba58662001-07-16 02:29:45 +00003625PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003626{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003627 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003628 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003629
3630 if (current_frame != NULL) {
3631 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003632 const int compilerflags = codeflags & PyCF_MASK;
3633 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003634 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003635 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003636 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003637#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003638 if (codeflags & CO_GENERATOR_ALLOWED) {
3639 result = 1;
3640 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3641 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003642#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003643 }
3644 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003645}
3646
3647int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003648Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003649{
Guido van Rossumb209a111997-04-29 18:18:01 +00003650 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003651 if (f == NULL)
3652 return 0;
3653 if (!PyFile_SoftSpace(f, 0))
3654 return 0;
3655 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003656}
3657
Guido van Rossum3f5da241990-12-20 15:06:42 +00003658
Guido van Rossum681d79a1995-07-18 14:51:37 +00003659/* External interface to call any callable object.
3660 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003661
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003662#undef PyEval_CallObject
3663/* for backward compatibility: export this interface */
3664
Guido van Rossumb209a111997-04-29 18:18:01 +00003665PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003666PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003667{
Guido van Rossumb209a111997-04-29 18:18:01 +00003668 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003669}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003670#define PyEval_CallObject(func,arg) \
3671 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003672
Guido van Rossumb209a111997-04-29 18:18:01 +00003673PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003674PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003675{
Jeremy Hylton52820442001-01-03 23:52:36 +00003676 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003677
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003678 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003679 arg = PyTuple_New(0);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003680 if (arg == NULL)
3681 return NULL;
3682 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003683 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003684 PyErr_SetString(PyExc_TypeError,
3685 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003686 return NULL;
3687 }
3688 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003689 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003690
Guido van Rossumb209a111997-04-29 18:18:01 +00003691 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003692 PyErr_SetString(PyExc_TypeError,
3693 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003694 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003695 return NULL;
3696 }
3697
Tim Peters6d6c1a32001-08-02 04:15:00 +00003698 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003699 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003700 return result;
3701}
3702
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003703const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003704PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003705{
3706 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003707 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003708 else if (PyFunction_Check(func))
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003709 return PyString_AsString(((PyFunctionObject*)func)->func_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00003710 else if (PyCFunction_Check(func))
3711 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3712 else if (PyClass_Check(func))
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003713 return PyString_AsString(((PyClassObject*)func)->cl_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00003714 else if (PyInstance_Check(func)) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003715 return PyString_AsString(
Jeremy Hylton512a2372001-04-11 13:52:29 +00003716 ((PyInstanceObject*)func)->in_class->cl_name);
3717 } else {
3718 return func->ob_type->tp_name;
3719 }
3720}
3721
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003722const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003723PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003724{
3725 if (PyMethod_Check(func))
3726 return "()";
3727 else if (PyFunction_Check(func))
3728 return "()";
3729 else if (PyCFunction_Check(func))
3730 return "()";
3731 else if (PyClass_Check(func))
3732 return " constructor";
3733 else if (PyInstance_Check(func)) {
3734 return " instance";
3735 } else {
3736 return " object";
3737 }
3738}
3739
Fredrik Lundh7a830892006-05-27 10:39:48 +00003740static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003741err_args(PyObject *func, int flags, int nargs)
3742{
3743 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003744 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003745 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003746 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003747 nargs);
3748 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003749 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003750 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003751 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003752 nargs);
3753}
3754
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003755#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003756if (tstate->use_tracing && tstate->c_profilefunc) { \
3757 if (call_trace(tstate->c_profilefunc, \
3758 tstate->c_profileobj, \
3759 tstate->frame, PyTrace_C_CALL, \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003760 func)) { \
3761 x = NULL; \
3762 } \
3763 else { \
3764 x = call; \
3765 if (tstate->c_profilefunc != NULL) { \
3766 if (x == NULL) { \
3767 call_trace_protected(tstate->c_profilefunc, \
3768 tstate->c_profileobj, \
3769 tstate->frame, PyTrace_C_EXCEPTION, \
3770 func); \
3771 /* XXX should pass (type, value, tb) */ \
3772 } else { \
3773 if (call_trace(tstate->c_profilefunc, \
3774 tstate->c_profileobj, \
3775 tstate->frame, PyTrace_C_RETURN, \
3776 func)) { \
3777 Py_DECREF(x); \
3778 x = NULL; \
3779 } \
3780 } \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003781 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003782 } \
3783} else { \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003784 x = call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003785 }
3786
Fredrik Lundh7a830892006-05-27 10:39:48 +00003787static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003788call_function(PyObject ***pp_stack, int oparg
3789#ifdef WITH_TSC
3790 , uint64* pintr0, uint64* pintr1
3791#endif
3792 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003793{
3794 int na = oparg & 0xff;
3795 int nk = (oparg>>8) & 0xff;
3796 int n = na + 2 * nk;
3797 PyObject **pfunc = (*pp_stack) - n - 1;
3798 PyObject *func = *pfunc;
3799 PyObject *x, *w;
3800
Jeremy Hylton985eba52003-02-05 23:13:00 +00003801 /* Always dispatch PyCFunction first, because these are
3802 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003803 */
3804 if (PyCFunction_Check(func) && nk == 0) {
3805 int flags = PyCFunction_GET_FLAGS(func);
Nicholas Bastind858a772004-06-25 23:31:06 +00003806 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003807
3808 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003809 if (flags & (METH_NOARGS | METH_O)) {
3810 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3811 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003812 if (flags & METH_NOARGS && na == 0) {
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003813 C_TRACE(x, (*meth)(self,NULL));
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003814 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003815 else if (flags & METH_O && na == 1) {
3816 PyObject *arg = EXT_POP(*pp_stack);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003817 C_TRACE(x, (*meth)(self,arg));
Jeremy Hylton192690e2002-08-16 18:36:11 +00003818 Py_DECREF(arg);
3819 }
3820 else {
3821 err_args(func, flags, na);
3822 x = NULL;
3823 }
3824 }
3825 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003826 PyObject *callargs;
3827 callargs = load_args(pp_stack, na);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003828 READ_TIMESTAMP(*pintr0);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003829 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003830 READ_TIMESTAMP(*pintr1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003831 Py_XDECREF(callargs);
3832 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003833 } else {
3834 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3835 /* optimize access to bound methods */
3836 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003837 PCALL(PCALL_METHOD);
3838 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003839 Py_INCREF(self);
3840 func = PyMethod_GET_FUNCTION(func);
3841 Py_INCREF(func);
3842 Py_DECREF(*pfunc);
3843 *pfunc = self;
3844 na++;
3845 n++;
3846 } else
3847 Py_INCREF(func);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003848 READ_TIMESTAMP(*pintr0);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003849 if (PyFunction_Check(func))
3850 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003851 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003852 x = do_call(func, pp_stack, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003853 READ_TIMESTAMP(*pintr1);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003854 Py_DECREF(func);
3855 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003856
Armin Rigod34fa522006-03-28 19:10:40 +00003857 /* Clear the stack of the function object. Also removes
3858 the arguments in case they weren't consumed already
3859 (fast_function() and err_args() leave them on the stack).
Thomas Wouters7f597322006-03-01 05:32:33 +00003860 */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003861 while ((*pp_stack) > pfunc) {
3862 w = EXT_POP(*pp_stack);
3863 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003864 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003865 }
3866 return x;
3867}
3868
Jeremy Hylton192690e2002-08-16 18:36:11 +00003869/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003870 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003871 For the simplest case -- a function that takes only positional
3872 arguments and is called with only positional arguments -- it
3873 inlines the most primitive frame setup code from
3874 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3875 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003876*/
3877
Fredrik Lundh7a830892006-05-27 10:39:48 +00003878static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003879fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003880{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003881 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003882 PyObject *globals = PyFunction_GET_GLOBALS(func);
3883 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3884 PyObject **d = NULL;
3885 int nd = 0;
3886
Jeremy Hylton985eba52003-02-05 23:13:00 +00003887 PCALL(PCALL_FUNCTION);
3888 PCALL(PCALL_FAST_FUNCTION);
Raymond Hettinger40174c32003-05-31 07:04:16 +00003889 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003890 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3891 PyFrameObject *f;
3892 PyObject *retval = NULL;
3893 PyThreadState *tstate = PyThreadState_GET();
3894 PyObject **fastlocals, **stack;
3895 int i;
3896
3897 PCALL(PCALL_FASTER_FUNCTION);
3898 assert(globals != NULL);
3899 /* XXX Perhaps we should create a specialized
3900 PyFrame_New() that doesn't take locals, but does
3901 take builtins without sanity checking them.
3902 */
Neal Norwitzdf6a6492006-08-13 18:10:10 +00003903 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003904 f = PyFrame_New(tstate, co, globals, NULL);
3905 if (f == NULL)
3906 return NULL;
3907
3908 fastlocals = f->f_localsplus;
3909 stack = (*pp_stack) - n;
3910
3911 for (i = 0; i < n; i++) {
3912 Py_INCREF(*stack);
3913 fastlocals[i] = *stack++;
3914 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003915 retval = PyEval_EvalFrameEx(f,0);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003916 ++tstate->recursion_depth;
3917 Py_DECREF(f);
3918 --tstate->recursion_depth;
3919 return retval;
3920 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003921 if (argdefs != NULL) {
3922 d = &PyTuple_GET_ITEM(argdefs, 0);
Christian Heimese93237d2007-12-19 02:37:44 +00003923 nd = Py_SIZE(argdefs);
Jeremy Hylton52820442001-01-03 23:52:36 +00003924 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003925 return PyEval_EvalCodeEx(co, globals,
3926 (PyObject *)NULL, (*pp_stack)-n, na,
3927 (*pp_stack)-2*nk, nk, d, nd,
3928 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003929}
3930
Fredrik Lundh7a830892006-05-27 10:39:48 +00003931static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003932update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3933 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003934{
3935 PyObject *kwdict = NULL;
3936 if (orig_kwdict == NULL)
3937 kwdict = PyDict_New();
3938 else {
3939 kwdict = PyDict_Copy(orig_kwdict);
3940 Py_DECREF(orig_kwdict);
3941 }
3942 if (kwdict == NULL)
3943 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003944 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003945 int err;
3946 PyObject *value = EXT_POP(*pp_stack);
3947 PyObject *key = EXT_POP(*pp_stack);
3948 if (PyDict_GetItem(kwdict, key) != NULL) {
Thomas Woutersae406c62007-09-19 17:27:43 +00003949 PyErr_Format(PyExc_TypeError,
3950 "%.200s%s got multiple values "
3951 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003952 PyEval_GetFuncName(func),
3953 PyEval_GetFuncDesc(func),
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003954 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003955 Py_DECREF(key);
3956 Py_DECREF(value);
3957 Py_DECREF(kwdict);
3958 return NULL;
3959 }
3960 err = PyDict_SetItem(kwdict, key, value);
3961 Py_DECREF(key);
3962 Py_DECREF(value);
3963 if (err) {
3964 Py_DECREF(kwdict);
3965 return NULL;
3966 }
3967 }
3968 return kwdict;
3969}
3970
Fredrik Lundh7a830892006-05-27 10:39:48 +00003971static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00003972update_star_args(int nstack, int nstar, PyObject *stararg,
3973 PyObject ***pp_stack)
3974{
3975 PyObject *callargs, *w;
3976
3977 callargs = PyTuple_New(nstack + nstar);
3978 if (callargs == NULL) {
3979 return NULL;
3980 }
3981 if (nstar) {
3982 int i;
3983 for (i = 0; i < nstar; i++) {
3984 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3985 Py_INCREF(a);
3986 PyTuple_SET_ITEM(callargs, nstack + i, a);
3987 }
3988 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003989 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003990 w = EXT_POP(*pp_stack);
3991 PyTuple_SET_ITEM(callargs, nstack, w);
3992 }
3993 return callargs;
3994}
3995
Fredrik Lundh7a830892006-05-27 10:39:48 +00003996static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00003997load_args(PyObject ***pp_stack, int na)
3998{
3999 PyObject *args = PyTuple_New(na);
4000 PyObject *w;
4001
4002 if (args == NULL)
4003 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00004004 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00004005 w = EXT_POP(*pp_stack);
4006 PyTuple_SET_ITEM(args, na, w);
4007 }
4008 return args;
4009}
4010
Fredrik Lundh7a830892006-05-27 10:39:48 +00004011static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00004012do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4013{
4014 PyObject *callargs = NULL;
4015 PyObject *kwdict = NULL;
4016 PyObject *result = NULL;
4017
4018 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00004019 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00004020 if (kwdict == NULL)
4021 goto call_fail;
4022 }
4023 callargs = load_args(pp_stack, na);
4024 if (callargs == NULL)
4025 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004026#ifdef CALL_PROFILE
4027 /* At this point, we have to look at the type of func to
4028 update the call stats properly. Do it here so as to avoid
4029 exposing the call stats machinery outside ceval.c
4030 */
4031 if (PyFunction_Check(func))
4032 PCALL(PCALL_FUNCTION);
4033 else if (PyMethod_Check(func))
4034 PCALL(PCALL_METHOD);
4035 else if (PyType_Check(func))
4036 PCALL(PCALL_TYPE);
4037 else
4038 PCALL(PCALL_OTHER);
4039#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00004040 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00004041 call_fail:
4042 Py_XDECREF(callargs);
4043 Py_XDECREF(kwdict);
4044 return result;
4045}
4046
Fredrik Lundh7a830892006-05-27 10:39:48 +00004047static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00004048ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4049{
4050 int nstar = 0;
4051 PyObject *callargs = NULL;
4052 PyObject *stararg = NULL;
4053 PyObject *kwdict = NULL;
4054 PyObject *result = NULL;
4055
4056 if (flags & CALL_FLAG_KW) {
4057 kwdict = EXT_POP(*pp_stack);
Georg Brandl2134e752007-05-21 20:34:16 +00004058 if (!PyDict_Check(kwdict)) {
4059 PyObject *d;
4060 d = PyDict_New();
4061 if (d == NULL)
4062 goto ext_call_fail;
4063 if (PyDict_Update(d, kwdict) != 0) {
4064 Py_DECREF(d);
4065 /* PyDict_Update raises attribute
4066 * error (percolated from an attempt
4067 * to get 'keys' attribute) instead of
4068 * a type error if its second argument
4069 * is not a mapping.
4070 */
4071 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4072 PyErr_Format(PyExc_TypeError,
4073 "%.200s%.200s argument after ** "
4074 "must be a mapping, not %.200s",
4075 PyEval_GetFuncName(func),
4076 PyEval_GetFuncDesc(func),
4077 kwdict->ob_type->tp_name);
4078 }
4079 goto ext_call_fail;
4080 }
4081 Py_DECREF(kwdict);
4082 kwdict = d;
Jeremy Hylton52820442001-01-03 23:52:36 +00004083 }
4084 }
4085 if (flags & CALL_FLAG_VAR) {
4086 stararg = EXT_POP(*pp_stack);
4087 if (!PyTuple_Check(stararg)) {
4088 PyObject *t = NULL;
4089 t = PySequence_Tuple(stararg);
4090 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00004091 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4092 PyErr_Format(PyExc_TypeError,
Georg Brandl2134e752007-05-21 20:34:16 +00004093 "%.200s%.200s argument after * "
4094 "must be a sequence, not %200s",
Tim Peters6d6c1a32001-08-02 04:15:00 +00004095 PyEval_GetFuncName(func),
Georg Brandl2134e752007-05-21 20:34:16 +00004096 PyEval_GetFuncDesc(func),
4097 stararg->ob_type->tp_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00004098 }
Jeremy Hylton52820442001-01-03 23:52:36 +00004099 goto ext_call_fail;
4100 }
4101 Py_DECREF(stararg);
4102 stararg = t;
4103 }
4104 nstar = PyTuple_GET_SIZE(stararg);
4105 }
4106 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00004107 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00004108 if (kwdict == NULL)
4109 goto ext_call_fail;
4110 }
4111 callargs = update_star_args(na, nstar, stararg, pp_stack);
4112 if (callargs == NULL)
4113 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004114#ifdef CALL_PROFILE
4115 /* At this point, we have to look at the type of func to
4116 update the call stats properly. Do it here so as to avoid
4117 exposing the call stats machinery outside ceval.c
4118 */
4119 if (PyFunction_Check(func))
4120 PCALL(PCALL_FUNCTION);
4121 else if (PyMethod_Check(func))
4122 PCALL(PCALL_METHOD);
4123 else if (PyType_Check(func))
4124 PCALL(PCALL_TYPE);
4125 else
4126 PCALL(PCALL_OTHER);
4127#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00004128 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersae406c62007-09-19 17:27:43 +00004129ext_call_fail:
Jeremy Hylton52820442001-01-03 23:52:36 +00004130 Py_XDECREF(callargs);
4131 Py_XDECREF(kwdict);
4132 Py_XDECREF(stararg);
4133 return result;
4134}
4135
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004136/* Extract a slice index from a PyInt or PyLong or an object with the
4137 nb_index slot defined, and store in *pi.
4138 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4139 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 +00004140 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004141*/
Tim Petersb5196382001-12-16 19:44:20 +00004142/* Note: If v is NULL, return success without storing into *pi. This
4143 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4144 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004145*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004146int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004147_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004148{
Tim Petersb5196382001-12-16 19:44:20 +00004149 if (v != NULL) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004150 Py_ssize_t x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00004151 if (PyInt_Check(v)) {
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00004152 /* XXX(nnorwitz): I think PyInt_AS_LONG is correct,
4153 however, it looks like it should be AsSsize_t.
4154 There should be a comment here explaining why.
4155 */
4156 x = PyInt_AS_LONG(v);
Tim Peters7df5e7f2006-05-26 23:14:37 +00004157 }
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00004158 else if (PyIndex_Check(v)) {
4159 x = PyNumber_AsSsize_t(v, NULL);
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004160 if (x == -1 && PyErr_Occurred())
4161 return 0;
4162 }
4163 else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00004164 PyErr_SetString(PyExc_TypeError,
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004165 "slice indices must be integers or "
4166 "None or have an __index__ method");
Guido van Rossum20c6add2000-05-08 14:06:50 +00004167 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004168 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00004169 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004170 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00004171 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004172}
4173
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004174#undef ISINDEX
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00004175#define ISINDEX(x) ((x) == NULL || \
4176 PyInt_Check(x) || PyLong_Check(x) || PyIndex_Check(x))
Guido van Rossum50d756e2001-08-18 17:43:36 +00004177
Fredrik Lundh7a830892006-05-27 10:39:48 +00004178static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004179apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004180{
Guido van Rossum50d756e2001-08-18 17:43:36 +00004181 PyTypeObject *tp = u->ob_type;
4182 PySequenceMethods *sq = tp->tp_as_sequence;
4183
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004184 if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004185 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
Guido van Rossum50d756e2001-08-18 17:43:36 +00004186 if (!_PyEval_SliceIndex(v, &ilow))
4187 return NULL;
4188 if (!_PyEval_SliceIndex(w, &ihigh))
4189 return NULL;
4190 return PySequence_GetSlice(u, ilow, ihigh);
4191 }
4192 else {
4193 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00004194 if (slice != NULL) {
4195 PyObject *res = PyObject_GetItem(u, slice);
4196 Py_DECREF(slice);
4197 return res;
4198 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00004199 else
4200 return NULL;
4201 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004202}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004203
Fredrik Lundh7a830892006-05-27 10:39:48 +00004204static int
Guido van Rossumac7be682001-01-17 15:42:30 +00004205assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
4206 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004207{
Guido van Rossum50d756e2001-08-18 17:43:36 +00004208 PyTypeObject *tp = u->ob_type;
4209 PySequenceMethods *sq = tp->tp_as_sequence;
4210
Georg Brandl0fca97a2007-03-05 22:28:08 +00004211 if (sq && sq->sq_ass_slice && ISINDEX(v) && ISINDEX(w)) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004212 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
Guido van Rossum50d756e2001-08-18 17:43:36 +00004213 if (!_PyEval_SliceIndex(v, &ilow))
4214 return -1;
4215 if (!_PyEval_SliceIndex(w, &ihigh))
4216 return -1;
4217 if (x == NULL)
4218 return PySequence_DelSlice(u, ilow, ihigh);
4219 else
4220 return PySequence_SetSlice(u, ilow, ihigh, x);
4221 }
4222 else {
4223 PyObject *slice = PySlice_New(v, w, NULL);
4224 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00004225 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00004226 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00004227 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00004228 else
Guido van Rossum354797c2001-12-03 19:45:06 +00004229 res = PyObject_DelItem(u, slice);
4230 Py_DECREF(slice);
4231 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00004232 }
4233 else
4234 return -1;
4235 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004236}
4237
Guido van Rossum04edb522008-03-18 02:49:46 +00004238#define Py3kExceptionClass_Check(x) \
4239 (PyType_Check((x)) && \
4240 PyType_FastSubclass((PyTypeObject*)(x), Py_TPFLAGS_BASE_EXC_SUBCLASS))
4241
4242#define CANNOT_CATCH_MSG "catching classes that don't inherit from " \
Georg Brandld5b635f2008-03-25 08:29:14 +00004243 "BaseException is not allowed in 3.x"
Guido van Rossum04edb522008-03-18 02:49:46 +00004244
Fredrik Lundh7a830892006-05-27 10:39:48 +00004245static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004246cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004247{
Guido van Rossumac7be682001-01-17 15:42:30 +00004248 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004249 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00004250 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00004251 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004252 break;
4253 case PyCmp_IS_NOT:
4254 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004255 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004256 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004257 res = PySequence_Contains(w, v);
4258 if (res < 0)
4259 return NULL;
4260 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004261 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00004262 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004263 if (res < 0)
4264 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004265 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004266 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004267 case PyCmp_EXC_MATCH:
Brett Cannon129bd522007-01-30 21:34:36 +00004268 if (PyTuple_Check(w)) {
4269 Py_ssize_t i, length;
4270 length = PyTuple_Size(w);
4271 for (i = 0; i < length; i += 1) {
4272 PyObject *exc = PyTuple_GET_ITEM(w, i);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004273 if (PyString_Check(exc)) {
Brett Cannon129bd522007-01-30 21:34:36 +00004274 int ret_val;
4275 ret_val = PyErr_WarnEx(
Thomas Wouterse2176022007-09-20 17:35:10 +00004276 PyExc_DeprecationWarning,
4277 "catching of string "
4278 "exceptions is deprecated", 1);
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00004279 if (ret_val < 0)
Brett Cannon129bd522007-01-30 21:34:36 +00004280 return NULL;
4281 }
Guido van Rossum20bda582008-03-18 03:15:05 +00004282 else if (Py_Py3kWarningFlag &&
4283 !PyTuple_Check(exc) &&
4284 !Py3kExceptionClass_Check(exc))
Guido van Rossum04edb522008-03-18 02:49:46 +00004285 {
4286 int ret_val;
4287 ret_val = PyErr_WarnEx(
4288 PyExc_DeprecationWarning,
4289 CANNOT_CATCH_MSG, 1);
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00004290 if (ret_val < 0)
Guido van Rossum04edb522008-03-18 02:49:46 +00004291 return NULL;
4292 }
Brett Cannon129bd522007-01-30 21:34:36 +00004293 }
4294 }
4295 else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004296 if (PyString_Check(w)) {
Brett Cannon129bd522007-01-30 21:34:36 +00004297 int ret_val;
4298 ret_val = PyErr_WarnEx(
4299 PyExc_DeprecationWarning,
4300 "catching of string "
Thomas Wouterse2176022007-09-20 17:35:10 +00004301 "exceptions is deprecated", 1);
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00004302 if (ret_val < 0)
Brett Cannon129bd522007-01-30 21:34:36 +00004303 return NULL;
4304 }
Guido van Rossum20bda582008-03-18 03:15:05 +00004305 else if (Py_Py3kWarningFlag &&
4306 !PyTuple_Check(w) &&
4307 !Py3kExceptionClass_Check(w))
Guido van Rossum04edb522008-03-18 02:49:46 +00004308 {
4309 int ret_val;
4310 ret_val = PyErr_WarnEx(
4311 PyExc_DeprecationWarning,
4312 CANNOT_CATCH_MSG, 1);
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00004313 if (ret_val < 0)
Guido van Rossum04edb522008-03-18 02:49:46 +00004314 return NULL;
4315 }
Brett Cannon129bd522007-01-30 21:34:36 +00004316 }
Barry Warsaw4249f541997-08-22 21:26:19 +00004317 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004318 break;
4319 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00004320 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004321 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004322 v = res ? Py_True : Py_False;
4323 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004324 return v;
4325}
4326
Fredrik Lundh7a830892006-05-27 10:39:48 +00004327static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00004328import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004329{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004330 PyObject *x;
4331
4332 x = PyObject_GetAttr(v, name);
4333 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00004334 PyErr_Format(PyExc_ImportError,
4335 "cannot import name %.230s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004336 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004337 }
Thomas Wouters52152252000-08-17 22:55:00 +00004338 return x;
4339}
Guido van Rossumac7be682001-01-17 15:42:30 +00004340
Fredrik Lundh7a830892006-05-27 10:39:48 +00004341static int
Thomas Wouters52152252000-08-17 22:55:00 +00004342import_all_from(PyObject *locals, PyObject *v)
4343{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004344 PyObject *all = PyObject_GetAttrString(v, "__all__");
4345 PyObject *dict, *name, *value;
4346 int skip_leading_underscores = 0;
4347 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004348
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004349 if (all == NULL) {
4350 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4351 return -1; /* Unexpected error */
4352 PyErr_Clear();
4353 dict = PyObject_GetAttrString(v, "__dict__");
4354 if (dict == NULL) {
4355 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4356 return -1;
4357 PyErr_SetString(PyExc_ImportError,
4358 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00004359 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004360 }
4361 all = PyMapping_Keys(dict);
4362 Py_DECREF(dict);
4363 if (all == NULL)
4364 return -1;
4365 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004366 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004367
4368 for (pos = 0, err = 0; ; pos++) {
4369 name = PySequence_GetItem(all, pos);
4370 if (name == NULL) {
4371 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4372 err = -1;
4373 else
4374 PyErr_Clear();
4375 break;
4376 }
4377 if (skip_leading_underscores &&
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004378 PyString_Check(name) &&
4379 PyString_AS_STRING(name)[0] == '_')
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004380 {
4381 Py_DECREF(name);
4382 continue;
4383 }
4384 value = PyObject_GetAttr(v, name);
4385 if (value == NULL)
4386 err = -1;
Armin Rigo70370852006-11-29 21:59:22 +00004387 else if (PyDict_CheckExact(locals))
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004388 err = PyDict_SetItem(locals, name, value);
Armin Rigo70370852006-11-29 21:59:22 +00004389 else
4390 err = PyObject_SetItem(locals, name, value);
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004391 Py_DECREF(name);
4392 Py_XDECREF(value);
4393 if (err != 0)
4394 break;
4395 }
4396 Py_DECREF(all);
4397 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004398}
4399
Fredrik Lundh7a830892006-05-27 10:39:48 +00004400static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004401build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004402{
Guido van Rossum7851eea2001-09-12 19:19:18 +00004403 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004404
4405 if (PyDict_Check(methods))
4406 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00004407 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00004408 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004409 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4410 base = PyTuple_GET_ITEM(bases, 0);
4411 metaclass = PyObject_GetAttrString(base, "__class__");
4412 if (metaclass == NULL) {
4413 PyErr_Clear();
4414 metaclass = (PyObject *)base->ob_type;
4415 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00004416 }
4417 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004418 else {
4419 PyObject *g = PyEval_GetGlobals();
4420 if (g != NULL && PyDict_Check(g))
4421 metaclass = PyDict_GetItemString(g, "__metaclass__");
4422 if (metaclass == NULL)
4423 metaclass = (PyObject *) &PyClass_Type;
4424 Py_INCREF(metaclass);
4425 }
Jeremy Hylton7c1e3472007-02-26 16:14:51 +00004426 result = PyObject_CallFunctionObjArgs(metaclass, name, bases, methods,
Thomas Woutersae406c62007-09-19 17:27:43 +00004427 NULL);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004428 Py_DECREF(metaclass);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004429 if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
Tim Peters7df5e7f2006-05-26 23:14:37 +00004430 /* A type error here likely means that the user passed
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004431 in a base that was not a class (such the random module
4432 instead of the random.random type). Help them out with
Raymond Hettingercfc31922004-09-16 16:41:57 +00004433 by augmenting the error message with more information.*/
4434
4435 PyObject *ptype, *pvalue, *ptraceback;
4436
4437 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004438 if (PyString_Check(pvalue)) {
Raymond Hettingercfc31922004-09-16 16:41:57 +00004439 PyObject *newmsg;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004440 newmsg = PyString_FromFormat(
Jeremy Hylton7c1e3472007-02-26 16:14:51 +00004441 "Error when calling the metaclass bases\n"
Thomas Woutersae406c62007-09-19 17:27:43 +00004442 " %s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004443 PyString_AS_STRING(pvalue));
Raymond Hettingercfc31922004-09-16 16:41:57 +00004444 if (newmsg != NULL) {
4445 Py_DECREF(pvalue);
4446 pvalue = newmsg;
4447 }
4448 }
4449 PyErr_Restore(ptype, pvalue, ptraceback);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004450 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004451 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00004452}
4453
Fredrik Lundh7a830892006-05-27 10:39:48 +00004454static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004455exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
4456 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004457{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004458 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00004459 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004460 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004461
Guido van Rossumb209a111997-04-29 18:18:01 +00004462 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
4463 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004464 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00004465 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004466 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00004467 locals = PyTuple_GetItem(prog, 2);
4468 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004469 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004470 if (globals == Py_None) {
4471 globals = PyEval_GetGlobals();
4472 if (locals == Py_None) {
4473 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00004474 plain = 1;
4475 }
Neal Norwitzdf6a6492006-08-13 18:10:10 +00004476 if (!globals || !locals) {
4477 PyErr_SetString(PyExc_SystemError,
4478 "globals and locals cannot be NULL");
4479 return -1;
4480 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004481 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004482 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004483 locals = globals;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004484 if (!PyString_Check(prog) &&
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004485 !PyUnicode_Check(prog) &&
Guido van Rossumb209a111997-04-29 18:18:01 +00004486 !PyCode_Check(prog) &&
4487 !PyFile_Check(prog)) {
4488 PyErr_SetString(PyExc_TypeError,
Guido van Rossumac7be682001-01-17 15:42:30 +00004489 "exec: arg 1 must be a string, file, or code object");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004490 return -1;
4491 }
Fred Drake661ea262000-10-24 19:57:45 +00004492 if (!PyDict_Check(globals)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004493 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00004494 "exec: arg 2 must be a dictionary or None");
4495 return -1;
4496 }
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004497 if (!PyMapping_Check(locals)) {
Fred Drake661ea262000-10-24 19:57:45 +00004498 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004499 "exec: arg 3 must be a mapping or None");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004500 return -1;
4501 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004502 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00004503 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00004504 if (PyCode_Check(prog)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +00004505 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4506 PyErr_SetString(PyExc_TypeError,
4507 "code object passed to exec may not contain free variables");
4508 return -1;
4509 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004510 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004511 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004512 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004513 FILE *fp = PyFile_AsFile(prog);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004514 char *name = PyString_AsString(PyFile_Name(prog));
Jeremy Hylton714b1122007-02-25 16:01:58 +00004515 PyCompilerFlags cf;
Thomas Woutersae406c62007-09-19 17:27:43 +00004516 if (name == NULL)
4517 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004518 cf.cf_flags = 0;
4519 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004520 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004521 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004522 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004523 v = PyRun_File(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004524 locals);
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004525 }
4526 else {
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004527 PyObject *tmp = NULL;
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004528 char *str;
Tim Peters5ba58662001-07-16 02:29:45 +00004529 PyCompilerFlags cf;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004530 cf.cf_flags = 0;
4531#ifdef Py_USING_UNICODE
4532 if (PyUnicode_Check(prog)) {
4533 tmp = PyUnicode_AsUTF8String(prog);
4534 if (tmp == NULL)
4535 return -1;
4536 prog = tmp;
4537 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4538 }
4539#endif
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004540 if (PyString_AsStringAndSize(prog, &str, NULL))
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004541 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004542 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters8a5c3c72004-04-05 19:36:21 +00004543 v = PyRun_StringFlags(str, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004544 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004545 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004546 v = PyRun_String(str, Py_file_input, globals, locals);
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004547 Py_XDECREF(tmp);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004548 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004549 if (plain)
4550 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004551 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004552 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00004553 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004554 return 0;
4555}
Guido van Rossum24c13741995-02-14 09:42:43 +00004556
Fredrik Lundh7a830892006-05-27 10:39:48 +00004557static void
Paul Prescode68140d2000-08-30 20:25:01 +00004558format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4559{
4560 char *obj_str;
4561
4562 if (!obj)
4563 return;
4564
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004565 obj_str = PyString_AsString(obj);
Paul Prescode68140d2000-08-30 20:25:01 +00004566 if (!obj_str)
4567 return;
4568
4569 PyErr_Format(exc, format_str, obj_str);
4570}
Guido van Rossum950361c1997-01-24 13:49:28 +00004571
Fredrik Lundh7a830892006-05-27 10:39:48 +00004572static PyObject *
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004573string_concatenate(PyObject *v, PyObject *w,
4574 PyFrameObject *f, unsigned char *next_instr)
4575{
4576 /* This function implements 'variable += expr' when both arguments
4577 are strings. */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004578 Py_ssize_t v_len = PyString_GET_SIZE(v);
4579 Py_ssize_t w_len = PyString_GET_SIZE(w);
Armin Rigo97ff0472006-08-09 15:37:26 +00004580 Py_ssize_t new_len = v_len + w_len;
4581 if (new_len < 0) {
4582 PyErr_SetString(PyExc_OverflowError,
4583 "strings are too large to concat");
4584 return NULL;
4585 }
Tim Peters7df5e7f2006-05-26 23:14:37 +00004586
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004587 if (v->ob_refcnt == 2) {
4588 /* In the common case, there are 2 references to the value
4589 * stored in 'variable' when the += is performed: one on the
Thomas Wouterse2176022007-09-20 17:35:10 +00004590 * value stack (in 'v') and one still stored in the
4591 * 'variable'. We try to delete the variable now to reduce
4592 * the refcnt to 1.
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004593 */
4594 switch (*next_instr) {
4595 case STORE_FAST:
4596 {
4597 int oparg = PEEKARG();
4598 PyObject **fastlocals = f->f_localsplus;
4599 if (GETLOCAL(oparg) == v)
4600 SETLOCAL(oparg, NULL);
4601 break;
4602 }
4603 case STORE_DEREF:
4604 {
Thomas Wouterse2176022007-09-20 17:35:10 +00004605 PyObject **freevars = (f->f_localsplus +
4606 f->f_code->co_nlocals);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004607 PyObject *c = freevars[PEEKARG()];
4608 if (PyCell_GET(c) == v)
4609 PyCell_Set(c, NULL);
4610 break;
4611 }
4612 case STORE_NAME:
4613 {
4614 PyObject *names = f->f_code->co_names;
4615 PyObject *name = GETITEM(names, PEEKARG());
4616 PyObject *locals = f->f_locals;
4617 if (PyDict_CheckExact(locals) &&
4618 PyDict_GetItem(locals, name) == v) {
4619 if (PyDict_DelItem(locals, name) != 0) {
4620 PyErr_Clear();
4621 }
4622 }
4623 break;
4624 }
4625 }
4626 }
4627
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004628 if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004629 /* Now we own the last reference to 'v', so we can resize it
4630 * in-place.
4631 */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004632 if (_PyString_Resize(&v, new_len) != 0) {
4633 /* XXX if _PyString_Resize() fails, 'v' has been
Thomas Wouterse2176022007-09-20 17:35:10 +00004634 * deallocated so it cannot be put back into
4635 * 'variable'. The MemoryError is raised when there
4636 * is no value in 'variable', which might (very
4637 * remotely) be a cause of incompatibilities.
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004638 */
4639 return NULL;
4640 }
4641 /* copy 'w' into the newly allocated area of 'v' */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004642 memcpy(PyString_AS_STRING(v) + v_len,
4643 PyString_AS_STRING(w), w_len);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004644 return v;
4645 }
4646 else {
4647 /* When in-place resizing is not an option. */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004648 PyString_Concat(&v, w);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004649 return v;
4650 }
4651}
4652
Guido van Rossum950361c1997-01-24 13:49:28 +00004653#ifdef DYNAMIC_EXECUTION_PROFILE
4654
Fredrik Lundh7a830892006-05-27 10:39:48 +00004655static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004656getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004657{
4658 int i;
4659 PyObject *l = PyList_New(256);
4660 if (l == NULL) return NULL;
4661 for (i = 0; i < 256; i++) {
4662 PyObject *x = PyInt_FromLong(a[i]);
4663 if (x == NULL) {
4664 Py_DECREF(l);
4665 return NULL;
4666 }
4667 PyList_SetItem(l, i, x);
4668 }
4669 for (i = 0; i < 256; i++)
4670 a[i] = 0;
4671 return l;
4672}
4673
4674PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004675_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004676{
4677#ifndef DXPAIRS
4678 return getarray(dxp);
4679#else
4680 int i;
4681 PyObject *l = PyList_New(257);
4682 if (l == NULL) return NULL;
4683 for (i = 0; i < 257; i++) {
4684 PyObject *x = getarray(dxpairs[i]);
4685 if (x == NULL) {
4686 Py_DECREF(l);
4687 return NULL;
4688 }
4689 PyList_SetItem(l, i, x);
4690 }
4691 return l;
4692#endif
4693}
4694
4695#endif