blob: dd91f5d0dc30325f791415340cca58658955e922 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003
Guido van Rossum681d79a1995-07-18 14:51:37 +00004/* XXX TO DO:
Guido van Rossum681d79a1995-07-18 14:51:37 +00005 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +00006 XXX document it!
7 */
8
Fredrik Lundh7a830892006-05-27 10:39:48 +00009/* enable more aggressive intra-module optimizations, where available */
Fredrik Lundh57640f52006-05-26 11:54:04 +000010#define PY_LOCAL_AGGRESSIVE
11
Guido van Rossumb209a111997-04-29 18:18:01 +000012#include "Python.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000013
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000014#include "code.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000015#include "frameobject.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000016#include "eval.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000017#include "opcode.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000018#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000019
Guido van Rossumc6004111993-11-05 10:22:19 +000020#include <ctype.h>
21
Tim Peters7df5e7f2006-05-26 23:14:37 +000022#ifndef WITH_TSC
Michael W. Hudson75eabd22005-01-18 15:56:11 +000023
24#define READ_TIMESTAMP(var)
25
26#else
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000027
28typedef unsigned long long uint64;
29
Michael W. Hudson800ba232004-08-12 18:19:17 +000030#if defined(__ppc__) /* <- Don't know if this is the correct symbol; this
Thomas Wouterse2176022007-09-20 17:35:10 +000031 section should work for GCC on any PowerPC
32 platform, irrespective of OS.
33 POWER? Who knows :-) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000034
Michael W. Hudson75eabd22005-01-18 15:56:11 +000035#define READ_TIMESTAMP(var) ppc_getcounter(&var)
Michael W. Hudson800ba232004-08-12 18:19:17 +000036
Fredrik Lundh7a830892006-05-27 10:39:48 +000037static void
Michael W. Hudson800ba232004-08-12 18:19:17 +000038ppc_getcounter(uint64 *v)
39{
40 register unsigned long tbu, tb, tbu2;
41
42 loop:
43 asm volatile ("mftbu %0" : "=r" (tbu) );
44 asm volatile ("mftb %0" : "=r" (tb) );
45 asm volatile ("mftbu %0" : "=r" (tbu2));
46 if (__builtin_expect(tbu != tbu2, 0)) goto loop;
47
Tim Peters7df5e7f2006-05-26 23:14:37 +000048 /* The slightly peculiar way of writing the next lines is
Michael W. Hudson800ba232004-08-12 18:19:17 +000049 compiled better by GCC than any other way I tried. */
50 ((long*)(v))[0] = tbu;
51 ((long*)(v))[1] = tb;
52}
53
Michael W. Hudson75eabd22005-01-18 15:56:11 +000054#else /* this is for linux/x86 (and probably any other GCC/x86 combo) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000055
Michael W. Hudson75eabd22005-01-18 15:56:11 +000056#define READ_TIMESTAMP(val) \
57 __asm__ __volatile__("rdtsc" : "=A" (val))
Michael W. Hudson800ba232004-08-12 18:19:17 +000058
59#endif
60
Tim Peters7df5e7f2006-05-26 23:14:37 +000061void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000062 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
63{
64 uint64 intr, inst, loop;
65 PyThreadState *tstate = PyThreadState_Get();
66 if (!tstate->interp->tscdump)
67 return;
68 intr = intr1 - intr0;
69 inst = inst1 - inst0 - intr;
70 loop = loop1 - loop0 - intr;
71 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
72 opcode, ticked, inst, loop);
73}
Michael W. Hudson800ba232004-08-12 18:19:17 +000074
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000075#endif
76
Guido van Rossum04691fc1992-08-12 15:35:34 +000077/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000078/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000079
Guido van Rossum408027e1996-12-30 16:17:54 +000080#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000081/* For debugging the interpreter: */
82#define LLTRACE 1 /* Low-level trace feature */
83#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000084#endif
85
Jeremy Hylton52820442001-01-03 23:52:36 +000086typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000087
Guido van Rossum374a9221991-04-04 10:40:29 +000088/* Forward declarations */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000089#ifdef WITH_TSC
Fredrik Lundh7a830892006-05-27 10:39:48 +000090static PyObject * call_function(PyObject ***, int, uint64*, uint64*);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000091#else
Fredrik Lundh7a830892006-05-27 10:39:48 +000092static PyObject * call_function(PyObject ***, int);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000093#endif
Fredrik Lundh7a830892006-05-27 10:39:48 +000094static PyObject * fast_function(PyObject *, PyObject ***, int, int, int);
95static PyObject * do_call(PyObject *, PyObject ***, int, int);
96static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int);
Thomas Wouterse2176022007-09-20 17:35:10 +000097static PyObject * update_keyword_args(PyObject *, int, PyObject ***,
98 PyObject *);
Fredrik Lundh7a830892006-05-27 10:39:48 +000099static PyObject * update_star_args(int, int, PyObject *, PyObject ***);
100static PyObject * load_args(PyObject ***, int);
Jeremy Hylton52820442001-01-03 23:52:36 +0000101#define CALL_FLAG_VAR 1
102#define CALL_FLAG_KW 2
103
Guido van Rossum0a066c01992-03-27 17:29:15 +0000104#ifdef LLTRACE
Fredrik Lundh1b949402006-05-26 12:01:49 +0000105static int lltrace;
Fredrik Lundh7a830892006-05-27 10:39:48 +0000106static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +0000107#endif
Fredrik Lundh7a830892006-05-27 10:39:48 +0000108static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
Fred Drake5755ce62001-06-27 19:19:46 +0000109 int, PyObject *);
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +0000110static int call_trace_protected(Py_tracefunc, PyObject *,
Armin Rigo1c2d7e52005-09-20 18:34:01 +0000111 PyFrameObject *, int, PyObject *);
Fredrik Lundh7a830892006-05-27 10:39:48 +0000112static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
113static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Armin Rigobf57a142004-03-22 19:24:58 +0000114 PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000115
Fredrik Lundh7a830892006-05-27 10:39:48 +0000116static PyObject * apply_slice(PyObject *, PyObject *, PyObject *);
117static int assign_slice(PyObject *, PyObject *,
Tim Petersdbd9ba62000-07-09 03:09:57 +0000118 PyObject *, PyObject *);
Fredrik Lundh7a830892006-05-27 10:39:48 +0000119static PyObject * cmp_outcome(int, PyObject *, PyObject *);
120static PyObject * import_from(PyObject *, PyObject *);
121static int import_all_from(PyObject *, PyObject *);
122static PyObject * build_class(PyObject *, PyObject *, PyObject *);
123static int exec_statement(PyFrameObject *,
Tim Petersdbd9ba62000-07-09 03:09:57 +0000124 PyObject *, PyObject *, PyObject *);
Fredrik Lundh7a830892006-05-27 10:39:48 +0000125static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *);
126static void reset_exc_info(PyThreadState *);
127static void format_exc_check_arg(PyObject *, char *, PyObject *);
128static PyObject * string_concatenate(PyObject *, PyObject *,
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000129 PyFrameObject *, unsigned char *);
Benjamin Petersone18ef192009-01-20 14:21:16 +0000130static PyObject * kwd_as_string(PyObject *);
Benjamin Peterson1880d8b2009-05-25 13:13:44 +0000131static PyObject * special_lookup(PyObject *, char *, PyObject **);
Guido van Rossum374a9221991-04-04 10:40:29 +0000132
Paul Prescode68140d2000-08-30 20:25:01 +0000133#define NAME_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000134 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000135#define GLOBAL_NAME_ERROR_MSG \
136 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000137#define UNBOUNDLOCAL_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000138 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000139#define UNBOUNDFREE_ERROR_MSG \
140 "free variable '%.200s' referenced before assignment" \
141 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000142
Guido van Rossum950361c1997-01-24 13:49:28 +0000143/* Dynamic execution profile */
144#ifdef DYNAMIC_EXECUTION_PROFILE
145#ifdef DXPAIRS
146static long dxpairs[257][256];
147#define dxp dxpairs[256]
148#else
149static long dxp[256];
150#endif
151#endif
152
Jeremy Hylton985eba52003-02-05 23:13:00 +0000153/* Function call profile */
154#ifdef CALL_PROFILE
155#define PCALL_NUM 11
156static int pcall[PCALL_NUM];
157
158#define PCALL_ALL 0
159#define PCALL_FUNCTION 1
160#define PCALL_FAST_FUNCTION 2
161#define PCALL_FASTER_FUNCTION 3
162#define PCALL_METHOD 4
163#define PCALL_BOUND_METHOD 5
164#define PCALL_CFUNCTION 6
165#define PCALL_TYPE 7
166#define PCALL_GENERATOR 8
167#define PCALL_OTHER 9
168#define PCALL_POP 10
169
170/* Notes about the statistics
171
172 PCALL_FAST stats
173
174 FAST_FUNCTION means no argument tuple needs to be created.
175 FASTER_FUNCTION means that the fast-path frame setup code is used.
176
177 If there is a method call where the call can be optimized by changing
178 the argument tuple and calling the function directly, it gets recorded
179 twice.
180
181 As a result, the relationship among the statistics appears to be
182 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
183 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
184 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
185 PCALL_METHOD > PCALL_BOUND_METHOD
186*/
187
188#define PCALL(POS) pcall[POS]++
189
190PyObject *
191PyEval_GetCallStats(PyObject *self)
192{
Andrew M. Kuchling1f3ebe02006-10-27 13:22:46 +0000193 return Py_BuildValue("iiiiiiiiiii",
Jeremy Hylton985eba52003-02-05 23:13:00 +0000194 pcall[0], pcall[1], pcall[2], pcall[3],
195 pcall[4], pcall[5], pcall[6], pcall[7],
Andrew M. Kuchling1f3ebe02006-10-27 13:22:46 +0000196 pcall[8], pcall[9], pcall[10]);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000197}
198#else
199#define PCALL(O)
200
201PyObject *
202PyEval_GetCallStats(PyObject *self)
203{
204 Py_INCREF(Py_None);
205 return Py_None;
206}
207#endif
208
Tim Peters5ca576e2001-06-18 22:08:13 +0000209
Guido van Rossume59214e1994-08-30 08:01:59 +0000210#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000211
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000212#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000213#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000214#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000215#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000216
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000217static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000218static PyThread_type_lock pending_lock = 0; /* for pending calls */
Guido van Rossuma9672091994-09-14 13:31:22 +0000219static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000220
Tim Peters7f468f22004-10-11 02:40:51 +0000221int
222PyEval_ThreadsInitialized(void)
223{
224 return interpreter_lock != 0;
225}
226
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000227void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000228PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000229{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000230 if (interpreter_lock)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000231 return;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000232 interpreter_lock = PyThread_allocate_lock();
233 PyThread_acquire_lock(interpreter_lock, 1);
234 main_thread = PyThread_get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000235}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000236
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000237void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000238PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000239{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000240 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000241}
242
243void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000244PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000245{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000246 PyThread_release_lock(interpreter_lock);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000247}
248
249void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000250PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000251{
252 if (tstate == NULL)
253 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000254 /* Check someone has called PyEval_InitThreads() to create the lock */
255 assert(interpreter_lock);
Guido van Rossum65d5b571998-12-21 19:32:43 +0000256 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000257 if (PyThreadState_Swap(tstate) != NULL)
258 Py_FatalError(
259 "PyEval_AcquireThread: non-NULL old thread state");
260}
261
262void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000263PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000264{
265 if (tstate == NULL)
266 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
267 if (PyThreadState_Swap(NULL) != tstate)
268 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000269 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000270}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000271
272/* This function is called from PyOS_AfterFork to ensure that newly
273 created child processes don't hold locks referring to threads which
274 are not running in the child process. (This could also be done using
275 pthread_atfork mechanism, at least for the pthreads implementation.) */
276
277void
278PyEval_ReInitThreads(void)
279{
Jesse Noller5e62ca42008-07-16 20:03:47 +0000280 PyObject *threading, *result;
281 PyThreadState *tstate;
282
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000283 if (!interpreter_lock)
284 return;
285 /*XXX Can't use PyThread_free_lock here because it does too
286 much error-checking. Doing this cleanly would require
287 adding a new function to each thread_*.h. Instead, just
288 create a new lock and waste a little bit of memory */
289 interpreter_lock = PyThread_allocate_lock();
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000290 pending_lock = PyThread_allocate_lock();
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000291 PyThread_acquire_lock(interpreter_lock, 1);
292 main_thread = PyThread_get_thread_ident();
Jesse Noller5e62ca42008-07-16 20:03:47 +0000293
294 /* Update the threading module with the new state.
295 */
296 tstate = PyThreadState_GET();
297 threading = PyMapping_GetItemString(tstate->interp->modules,
298 "threading");
299 if (threading == NULL) {
300 /* threading not imported */
301 PyErr_Clear();
302 return;
303 }
304 result = PyObject_CallMethod(threading, "_after_fork", NULL);
305 if (result == NULL)
306 PyErr_WriteUnraisable(threading);
307 else
308 Py_DECREF(result);
309 Py_DECREF(threading);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000310}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000311#endif
312
Guido van Rossumff4949e1992-08-05 19:58:53 +0000313/* Functions save_thread and restore_thread are always defined so
314 dynamically loaded modules needn't be compiled separately for use
315 with and without threads: */
316
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000317PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000318PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000319{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000320 PyThreadState *tstate = PyThreadState_Swap(NULL);
321 if (tstate == NULL)
322 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000323#ifdef WITH_THREAD
Guido van Rossumb74eca91997-09-30 22:03:16 +0000324 if (interpreter_lock)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000325 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000326#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000327 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000328}
329
330void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000331PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000332{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000333 if (tstate == NULL)
334 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000335#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000336 if (interpreter_lock) {
Guido van Rossumb74eca91997-09-30 22:03:16 +0000337 int err = errno;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000338 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000339 errno = err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000340 }
341#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000342 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000343}
344
345
Guido van Rossuma9672091994-09-14 13:31:22 +0000346/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
347 signal handlers or Mac I/O completion routines) can schedule calls
348 to a function to be called synchronously.
349 The synchronous function is called with one void* argument.
350 It should return 0 for success or -1 for failure -- failure should
351 be accompanied by an exception.
352
353 If registry succeeds, the registry function returns 0; if it fails
354 (e.g. due to too many pending calls) it returns -1 (without setting
355 an exception condition).
356
357 Note that because registry may occur from within signal handlers,
358 or other asynchronous events, calling malloc() is unsafe!
359
360#ifdef WITH_THREAD
361 Any thread can schedule pending calls, but only the main thread
362 will execute them.
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000363 There is no facility to schedule calls to a particular thread, but
364 that should be easy to change, should that ever be required. In
365 that case, the static variables here should go into the python
366 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000367#endif
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000368*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000369
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000370#ifdef WITH_THREAD
371
372/* The WITH_THREAD implementation is thread-safe. It allows
373 scheduling to be made from any thread, and even from an executing
374 callback.
375 */
376
377#define NPENDINGCALLS 32
378static struct {
379 int (*func)(void *);
380 void *arg;
381} pendingcalls[NPENDINGCALLS];
382static int pendingfirst = 0;
383static int pendinglast = 0;
384static volatile int pendingcalls_to_do = 1; /* trigger initialization of lock */
385static char pendingbusy = 0;
386
387int
388Py_AddPendingCall(int (*func)(void *), void *arg)
389{
390 int i, j, result=0;
391 PyThread_type_lock lock = pending_lock;
392
393 /* try a few times for the lock. Since this mechanism is used
394 * for signal handling (on the main thread), there is a (slim)
395 * chance that a signal is delivered on the same thread while we
396 * hold the lock during the Py_MakePendingCalls() function.
397 * This avoids a deadlock in that case.
398 * Note that signals can be delivered on any thread. In particular,
399 * on Windows, a SIGINT is delivered on a system-created worker
400 * thread.
401 * We also check for lock being NULL, in the unlikely case that
402 * this function is called before any bytecode evaluation takes place.
403 */
404 if (lock != NULL) {
405 for (i = 0; i<100; i++) {
406 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
407 break;
408 }
409 if (i == 100)
410 return -1;
411 }
412
413 i = pendinglast;
414 j = (i + 1) % NPENDINGCALLS;
415 if (j == pendingfirst) {
416 result = -1; /* Queue full */
417 } else {
418 pendingcalls[i].func = func;
419 pendingcalls[i].arg = arg;
420 pendinglast = j;
421 }
422 /* signal main loop */
423 _Py_Ticker = 0;
424 pendingcalls_to_do = 1;
425 if (lock != NULL)
426 PyThread_release_lock(lock);
427 return result;
428}
429
430int
431Py_MakePendingCalls(void)
432{
433 int i;
434 int r = 0;
435
436 if (!pending_lock) {
437 /* initial allocation of the lock */
438 pending_lock = PyThread_allocate_lock();
439 if (pending_lock == NULL)
440 return -1;
441 }
442
443 /* only service pending calls on main thread */
444 if (main_thread && PyThread_get_thread_ident() != main_thread)
445 return 0;
446 /* don't perform recursive pending calls */
447 if (pendingbusy)
448 return 0;
449 pendingbusy = 1;
450 /* perform a bounded number of calls, in case of recursion */
451 for (i=0; i<NPENDINGCALLS; i++) {
452 int j;
453 int (*func)(void *);
Mark Dickinson9e58a372009-02-08 17:33:11 +0000454 void *arg = NULL;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000455
456 /* pop one item off the queue while holding the lock */
457 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
458 j = pendingfirst;
459 if (j == pendinglast) {
460 func = NULL; /* Queue empty */
461 } else {
462 func = pendingcalls[j].func;
463 arg = pendingcalls[j].arg;
464 pendingfirst = (j + 1) % NPENDINGCALLS;
465 }
466 pendingcalls_to_do = pendingfirst != pendinglast;
467 PyThread_release_lock(pending_lock);
468 /* having released the lock, perform the callback */
469 if (func == NULL)
470 break;
471 r = func(arg);
472 if (r)
473 break;
474 }
475 pendingbusy = 0;
476 return r;
477}
478
479#else /* if ! defined WITH_THREAD */
480
481/*
482 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
483 This code is used for signal handling in python that isn't built
484 with WITH_THREAD.
485 Don't use this implementation when Py_AddPendingCalls() can happen
486 on a different thread!
487
Guido van Rossuma9672091994-09-14 13:31:22 +0000488 There are two possible race conditions:
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000489 (1) nested asynchronous calls to Py_AddPendingCall()
490 (2) AddPendingCall() calls made while pending calls are being processed.
491
492 (1) is very unlikely because typically signal delivery
493 is blocked during signal handling. So it should be impossible.
494 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000495 The current code is safe against (2), but not against (1).
496 The safety against (2) is derived from the fact that only one
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000497 thread is present, interrupted by signals, and that the critical
498 section is protected with the "busy" variable. On Windows, which
499 delivers SIGINT on a system thread, this does not hold and therefore
500 Windows really shouldn't use this version.
501 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000502*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000503
Guido van Rossuma9672091994-09-14 13:31:22 +0000504#define NPENDINGCALLS 32
505static struct {
Thomas Wouters334fb892000-07-25 12:56:38 +0000506 int (*func)(void *);
507 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000508} pendingcalls[NPENDINGCALLS];
509static volatile int pendingfirst = 0;
510static volatile int pendinglast = 0;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000511static volatile int pendingcalls_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000512
513int
Thomas Wouters334fb892000-07-25 12:56:38 +0000514Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000515{
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000516 static volatile int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000517 int i, j;
518 /* XXX Begin critical section */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000519 if (busy)
520 return -1;
521 busy = 1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000522 i = pendinglast;
523 j = (i + 1) % NPENDINGCALLS;
Guido van Rossum04e70322002-07-17 16:57:13 +0000524 if (j == pendingfirst) {
525 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000526 return -1; /* Queue full */
Guido van Rossum04e70322002-07-17 16:57:13 +0000527 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000528 pendingcalls[i].func = func;
529 pendingcalls[i].arg = arg;
530 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000531
532 _Py_Ticker = 0;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000533 pendingcalls_to_do = 1; /* Signal main loop */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000534 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000535 /* XXX End critical section */
536 return 0;
537}
538
Guido van Rossum180d7b41994-09-29 09:45:57 +0000539int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000540Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000541{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000542 static int busy = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000543 if (busy)
Guido van Rossum180d7b41994-09-29 09:45:57 +0000544 return 0;
545 busy = 1;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000546 pendingcalls_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000547 for (;;) {
548 int i;
Thomas Wouters334fb892000-07-25 12:56:38 +0000549 int (*func)(void *);
550 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000551 i = pendingfirst;
552 if (i == pendinglast)
553 break; /* Queue empty */
554 func = pendingcalls[i].func;
555 arg = pendingcalls[i].arg;
556 pendingfirst = (i + 1) % NPENDINGCALLS;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000557 if (func(arg) < 0) {
558 busy = 0;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000559 pendingcalls_to_do = 1; /* We're not done yet */
Guido van Rossuma9672091994-09-14 13:31:22 +0000560 return -1;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000561 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000562 }
Guido van Rossum180d7b41994-09-29 09:45:57 +0000563 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000564 return 0;
565}
566
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000567#endif /* WITH_THREAD */
568
Guido van Rossuma9672091994-09-14 13:31:22 +0000569
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000570/* The interpreter's recursion limit */
571
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000572#ifndef Py_DEFAULT_RECURSION_LIMIT
573#define Py_DEFAULT_RECURSION_LIMIT 1000
574#endif
575static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
576int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000577
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000578int
579Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000580{
581 return recursion_limit;
582}
583
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000584void
585Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000586{
587 recursion_limit = new_limit;
Thomas Woutersae406c62007-09-19 17:27:43 +0000588 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000589}
590
Armin Rigo2b3eb402003-10-28 12:05:48 +0000591/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
592 if the recursion_depth reaches _Py_CheckRecursionLimit.
593 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
594 to guarantee that _Py_CheckRecursiveCall() is regularly called.
595 Without USE_STACKCHECK, there is no need for this. */
596int
597_Py_CheckRecursiveCall(char *where)
598{
599 PyThreadState *tstate = PyThreadState_GET();
600
601#ifdef USE_STACKCHECK
602 if (PyOS_CheckStack()) {
603 --tstate->recursion_depth;
604 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
605 return -1;
606 }
607#endif
608 if (tstate->recursion_depth > recursion_limit) {
609 --tstate->recursion_depth;
610 PyErr_Format(PyExc_RuntimeError,
611 "maximum recursion depth exceeded%s",
612 where);
613 return -1;
614 }
Thomas Woutersae406c62007-09-19 17:27:43 +0000615 _Py_CheckRecursionLimit = recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000616 return 0;
617}
618
Guido van Rossum374a9221991-04-04 10:40:29 +0000619/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000620enum why_code {
621 WHY_NOT = 0x0001, /* No error */
622 WHY_EXCEPTION = 0x0002, /* Exception occurred */
623 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
624 WHY_RETURN = 0x0008, /* 'return' statement */
625 WHY_BREAK = 0x0010, /* 'break' statement */
626 WHY_CONTINUE = 0x0020, /* 'continue' statement */
627 WHY_YIELD = 0x0040 /* 'yield' operator */
628};
Guido van Rossum374a9221991-04-04 10:40:29 +0000629
Fredrik Lundh7a830892006-05-27 10:39:48 +0000630static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
631static int unpack_iterable(PyObject *, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000632
Jeffrey Yasskinfd8a1ec2008-12-03 06:46:45 +0000633/* Records whether tracing is on for any thread. Counts the number of
634 threads for which tstate->c_tracefunc is non-NULL, so if the value
635 is 0, we know we don't have to check this thread's c_tracefunc.
636 This speeds up the if statement in PyEval_EvalFrameEx() after
637 fast_next_opcode*/
638static int _Py_TracingPossible = 0;
639
Skip Montanarod581d772002-09-03 20:10:45 +0000640/* for manipulating the thread switch and periodic "stuff" - used to be
641 per thread, now just a pair o' globals */
Skip Montanaro99dba272002-09-03 20:19:06 +0000642int _Py_CheckInterval = 100;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000643volatile int _Py_Ticker = 0; /* so that we hit a "tick" first thing */
Guido van Rossum374a9221991-04-04 10:40:29 +0000644
Guido van Rossumb209a111997-04-29 18:18:01 +0000645PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000646PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000647{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000648 return PyEval_EvalCodeEx(co,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000649 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000650 (PyObject **)NULL, 0,
651 (PyObject **)NULL, 0,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000652 (PyObject **)NULL, 0,
653 NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000654}
655
656
657/* Interpreter main loop */
658
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000659PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000660PyEval_EvalFrame(PyFrameObject *f) {
661 /* This is for backward compatibility with extension modules that
Thomas Wouterse2176022007-09-20 17:35:10 +0000662 used this API; core interpreter code should call
663 PyEval_EvalFrameEx() */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000664 return PyEval_EvalFrameEx(f, 0);
665}
666
667PyObject *
Anthony Baxtera863d332006-04-11 07:43:46 +0000668PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000669{
Guido van Rossum950361c1997-01-24 13:49:28 +0000670#ifdef DXPAIRS
671 int lastopcode = 0;
672#endif
Thomas Wouterse2176022007-09-20 17:35:10 +0000673 register PyObject **stack_pointer; /* Next free slot in value stack */
Guido van Rossum374a9221991-04-04 10:40:29 +0000674 register unsigned char *next_instr;
Armin Rigo8817fcd2004-06-17 10:22:40 +0000675 register int opcode; /* Current opcode */
676 register int oparg; /* Current opcode argument, if any */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000677 register enum why_code why; /* Reason for block stack unwind */
Guido van Rossum374a9221991-04-04 10:40:29 +0000678 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000679 register PyObject *x; /* Result object -- NULL if error */
680 register PyObject *v; /* Temporary objects popped off stack */
681 register PyObject *w;
682 register PyObject *u;
683 register PyObject *t;
Barry Warsaw23c9ec82000-08-21 15:44:01 +0000684 register PyObject *stream = NULL; /* for PRINT opcodes */
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000685 register PyObject **fastlocals, **freevars;
Guido van Rossum014518f1998-11-23 21:09:51 +0000686 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000687 PyThreadState *tstate = PyThreadState_GET();
Tim Peters5ca576e2001-06-18 22:08:13 +0000688 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000689
Tim Peters8a5c3c72004-04-05 19:36:21 +0000690 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000691
692 not (instr_lb <= current_bytecode_offset < instr_ub)
693
Tim Peters8a5c3c72004-04-05 19:36:21 +0000694 is true when the line being executed has changed. The
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000695 initial values are such as to make this false the first
696 time it is tested. */
Armin Rigobf57a142004-03-22 19:24:58 +0000697 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000698
Guido van Rossumd076c731998-10-07 19:42:25 +0000699 unsigned char *first_instr;
Skip Montanaro04d80f82002-08-04 21:03:35 +0000700 PyObject *names;
701 PyObject *consts;
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000702#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000703 /* Make it easier to find out where we are with a debugger */
Tim Peters5ca576e2001-06-18 22:08:13 +0000704 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000705#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000706
Neal Norwitza81d2202002-07-14 00:27:26 +0000707/* Tuple access macros */
708
709#ifndef Py_DEBUG
710#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
711#else
712#define GETITEM(v, i) PyTuple_GetItem((v), (i))
713#endif
714
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000715#ifdef WITH_TSC
716/* Use Pentium timestamp counter to mark certain events:
717 inst0 -- beginning of switch statement for opcode dispatch
718 inst1 -- end of switch statement (may be skipped)
719 loop0 -- the top of the mainloop
Tim Peters7df5e7f2006-05-26 23:14:37 +0000720 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000721 (may be skipped)
722 intr1 -- beginning of long interruption
723 intr2 -- end of long interruption
724
725 Many opcodes call out to helper C functions. In some cases, the
726 time in those functions should be counted towards the time for the
727 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
728 calls another Python function; there's no point in charge all the
729 bytecode executed by the called function to the caller.
730
731 It's hard to make a useful judgement statically. In the presence
732 of operator overloading, it's impossible to tell if a call will
733 execute new Python code or not.
734
735 It's a case-by-case judgement. I'll use intr1 for the following
736 cases:
737
738 EXEC_STMT
739 IMPORT_STAR
740 IMPORT_FROM
741 CALL_FUNCTION (and friends)
742
743 */
744 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
745 int ticked = 0;
746
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000747 READ_TIMESTAMP(inst0);
748 READ_TIMESTAMP(inst1);
749 READ_TIMESTAMP(loop0);
750 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000751
752 /* shut up the compiler */
753 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000754#endif
755
Guido van Rossum374a9221991-04-04 10:40:29 +0000756/* Code access macros */
757
Martin v. Löwis18e16552006-02-15 17:27:45 +0000758#define INSTR_OFFSET() ((int)(next_instr - first_instr))
Guido van Rossum374a9221991-04-04 10:40:29 +0000759#define NEXTOP() (*next_instr++)
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000760#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000761#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
Guido van Rossumd076c731998-10-07 19:42:25 +0000762#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000763#define JUMPBY(x) (next_instr += (x))
764
Raymond Hettingerf606f872003-03-16 03:11:04 +0000765/* OpCode prediction macros
Thomas Wouterse2176022007-09-20 17:35:10 +0000766 Some opcodes tend to come in pairs thus making it possible to
767 predict the second code when the first is run. For example,
Jeffrey Yasskin68d68522009-02-28 19:03:21 +0000768 GET_ITER is often followed by FOR_ITER. And FOR_ITER is often
769 followed by STORE_FAST or UNPACK_SEQUENCE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000770
Raymond Hettingerafae11e2008-07-05 02:11:55 +0000771 Verifying the prediction costs a single high-speed test of a register
Raymond Hettingerac2072922003-03-16 15:41:11 +0000772 variable against a constant. If the pairing was good, then the
Raymond Hettingerafae11e2008-07-05 02:11:55 +0000773 processor's own internal branch predication has a high likelihood of
774 success, resulting in a nearly zero-overhead transition to the
775 next opcode. A successful prediction saves a trip through the eval-loop
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +0000776 including its two unpredictable branches, the HAS_ARG test and the
Raymond Hettingerafae11e2008-07-05 02:11:55 +0000777 switch-case. Combined with the processor's internal branch prediction,
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +0000778 a successful PREDICT has the effect of making the two opcodes run as if
Raymond Hettingerafae11e2008-07-05 02:11:55 +0000779 they were a single new opcode with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000780
Raymond Hettingerafae11e2008-07-05 02:11:55 +0000781 If collecting opcode statistics, your choices are to either keep the
782 predictions turned-on and interpret the results as if some opcodes
783 had been combined or turn-off predictions so that the opcode frequency
784 counter updates for both opcodes.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000785*/
786
Raymond Hettingera7216982004-02-08 19:59:27 +0000787#ifdef DYNAMIC_EXECUTION_PROFILE
788#define PREDICT(op) if (0) goto PRED_##op
789#else
Raymond Hettingerac2072922003-03-16 15:41:11 +0000790#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000791#endif
792
Raymond Hettingerf606f872003-03-16 03:11:04 +0000793#define PREDICTED(op) PRED_##op: next_instr++
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000794#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Raymond Hettingerf606f872003-03-16 03:11:04 +0000795
Guido van Rossum374a9221991-04-04 10:40:29 +0000796/* Stack manipulation macros */
797
Martin v. Löwis18e16552006-02-15 17:27:45 +0000798/* The stack can grow at most MAXINT deep, as co_nlocals and
799 co_stacksize are ints. */
800#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
Guido van Rossum374a9221991-04-04 10:40:29 +0000801#define EMPTY() (STACK_LEVEL() == 0)
802#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000803#define SECOND() (stack_pointer[-2])
804#define THIRD() (stack_pointer[-3])
805#define FOURTH() (stack_pointer[-4])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000806#define SET_TOP(v) (stack_pointer[-1] = (v))
807#define SET_SECOND(v) (stack_pointer[-2] = (v))
808#define SET_THIRD(v) (stack_pointer[-3] = (v))
809#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000810#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000811#define BASIC_PUSH(v) (*stack_pointer++ = (v))
812#define BASIC_POP() (*--stack_pointer)
813
Guido van Rossum96a42c81992-01-12 02:29:51 +0000814#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000815#define PUSH(v) { (void)(BASIC_PUSH(v), \
816 lltrace && prtrace(TOP(), "push")); \
Richard Jonescebbefc2006-05-23 18:28:17 +0000817 assert(STACK_LEVEL() <= co->co_stacksize); }
Thomas Wouterse2176022007-09-20 17:35:10 +0000818#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
819 BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000820#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
821 lltrace && prtrace(TOP(), "stackadj")); \
Richard Jonescebbefc2006-05-23 18:28:17 +0000822 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes52729ac2007-12-14 02:33:57 +0000823#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
824 prtrace((STACK_POINTER)[-1], "ext_pop")), \
825 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000826#else
827#define PUSH(v) BASIC_PUSH(v)
828#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000829#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000830#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000831#endif
832
Guido van Rossum681d79a1995-07-18 14:51:37 +0000833/* Local variable macros */
834
835#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000836
837/* The SETLOCAL() macro must not DECREF the local variable in-place and
838 then store the new value; it must copy the old value to a temporary
839 value, then store the new value, and then DECREF the temporary value.
840 This is because it is possible that during the DECREF the frame is
841 accessed by other code (e.g. a __del__ method or gc.collect()) and the
842 variable would be pointing to already-freed memory. */
843#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
844 GETLOCAL(i) = value; \
845 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000846
Guido van Rossuma027efa1997-05-05 20:56:21 +0000847/* Start of code */
848
Tim Peters5ca576e2001-06-18 22:08:13 +0000849 if (f == NULL)
850 return NULL;
851
Armin Rigo1d313ab2003-10-25 14:33:09 +0000852 /* push frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000853 if (Py_EnterRecursiveCall(""))
Armin Rigo1d313ab2003-10-25 14:33:09 +0000854 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000855
Tim Peters5ca576e2001-06-18 22:08:13 +0000856 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000857
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000858 if (tstate->use_tracing) {
859 if (tstate->c_tracefunc != NULL) {
860 /* tstate->c_tracefunc, if defined, is a
861 function that will be called on *every* entry
862 to a code block. Its return value, if not
863 None, is a function that will be called at
864 the start of each executed line of code.
865 (Actually, the function must return itself
866 in order to continue tracing.) The trace
867 functions are called with three arguments:
868 a pointer to the current frame, a string
869 indicating why the function is called, and
870 an argument which depends on the situation.
871 The global trace function is also called
872 whenever an exception is detected. */
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +0000873 if (call_trace_protected(tstate->c_tracefunc,
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +0000874 tstate->c_traceobj,
875 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000876 /* Trace function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000877 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000878 }
879 }
880 if (tstate->c_profilefunc != NULL) {
881 /* Similar for c_profilefunc, except it needn't
882 return itself and isn't called for "line" events */
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +0000883 if (call_trace_protected(tstate->c_profilefunc,
884 tstate->c_profileobj,
885 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000886 /* Profile function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000887 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000888 }
889 }
890 }
891
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000892 co = f->f_code;
893 names = co->co_names;
894 consts = co->co_consts;
895 fastlocals = f->f_localsplus;
Richard Jonescebbefc2006-05-23 18:28:17 +0000896 freevars = f->f_localsplus + co->co_nlocals;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000897 first_instr = (unsigned char*) PyString_AS_STRING(co->co_code);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000898 /* An explanation is in order for the next line.
899
900 f->f_lasti now refers to the index of the last instruction
901 executed. You might think this was obvious from the name, but
902 this wasn't always true before 2.3! PyFrame_New now sets
903 f->f_lasti to -1 (i.e. the index *before* the first instruction)
904 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +0000905 does work. Promise.
Raymond Hettinger4bd97d42007-01-06 01:14:41 +0000906
907 When the PREDICT() macros are enabled, some opcode pairs follow in
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +0000908 direct succession without updating f->f_lasti. A successful
Raymond Hettinger4bd97d42007-01-06 01:14:41 +0000909 prediction effectively links the two codes together as if they
910 were a single new opcode; accordingly,f->f_lasti will point to
911 the first code in the pair (for instance, GET_ITER followed by
912 FOR_ITER is effectively a single opcode and f->f_lasti will point
913 at to the beginning of the combined pair.)
914 */
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000915 next_instr = first_instr + f->f_lasti + 1;
916 stack_pointer = f->f_stacktop;
917 assert(stack_pointer != NULL);
918 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
919
Tim Peters5ca576e2001-06-18 22:08:13 +0000920#ifdef LLTRACE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000921 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +0000922#endif
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000923#if defined(Py_DEBUG) || defined(LLTRACE)
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000924 filename = PyString_AsString(co->co_filename);
Tim Peters5ca576e2001-06-18 22:08:13 +0000925#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000926
Guido van Rossum374a9221991-04-04 10:40:29 +0000927 why = WHY_NOT;
928 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +0000929 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +0000930 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +0000931
Anthony Baxtera863d332006-04-11 07:43:46 +0000932 if (throwflag) { /* support for generator.throw() */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000933 why = WHY_EXCEPTION;
934 goto on_error;
935 }
Tim Peters7df5e7f2006-05-26 23:14:37 +0000936
Guido van Rossum374a9221991-04-04 10:40:29 +0000937 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000938#ifdef WITH_TSC
939 if (inst1 == 0) {
940 /* Almost surely, the opcode executed a break
941 or a continue, preventing inst1 from being set
942 on the way out of the loop.
943 */
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000944 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000945 loop1 = inst1;
946 }
947 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
948 intr0, intr1);
949 ticked = 0;
950 inst1 = 0;
951 intr0 = 0;
952 intr1 = 0;
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000953 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000954#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000955 assert(stack_pointer >= f->f_valuestack); /* else underflow */
Richard Jonescebbefc2006-05-23 18:28:17 +0000956 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000957
Guido van Rossuma027efa1997-05-05 20:56:21 +0000958 /* Do periodic things. Doing this every time through
959 the loop would add too much overhead, so we do it
960 only every Nth instruction. We also do it if
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000961 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
Guido van Rossuma027efa1997-05-05 20:56:21 +0000962 event needs attention (e.g. a signal handler or
963 async I/O handler); see Py_AddPendingCall() and
964 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000965
Skip Montanarod581d772002-09-03 20:10:45 +0000966 if (--_Py_Ticker < 0) {
Thomas Woutersae406c62007-09-19 17:27:43 +0000967 if (*next_instr == SETUP_FINALLY) {
968 /* Make the last opcode before
969 a try: finally: block uninterruptable. */
970 goto fast_next_opcode;
971 }
Skip Montanarod581d772002-09-03 20:10:45 +0000972 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000973 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000974#ifdef WITH_TSC
975 ticked = 1;
976#endif
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000977 if (pendingcalls_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +0000978 if (Py_MakePendingCalls() < 0) {
979 why = WHY_EXCEPTION;
980 goto on_error;
981 }
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000982 if (pendingcalls_to_do)
Kurt B. Kaiser4c79a832004-11-23 18:06:08 +0000983 /* MakePendingCalls() didn't succeed.
984 Force early re-execution of this
985 "periodic" code, possibly after
986 a thread switch */
987 _Py_Ticker = 0;
Guido van Rossum8861b741996-07-30 16:49:37 +0000988 }
Guido van Rossume59214e1994-08-30 08:01:59 +0000989#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000990 if (interpreter_lock) {
991 /* Give another thread a chance */
992
Guido van Rossum25ce5661997-08-02 03:10:38 +0000993 if (PyThreadState_Swap(NULL) != tstate)
994 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000995 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000996
997 /* Other threads may run now */
998
Guido van Rossum65d5b571998-12-21 19:32:43 +0000999 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001000 if (PyThreadState_Swap(tstate) != NULL)
1001 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +00001002
1003 /* Check for thread interrupts */
1004
1005 if (tstate->async_exc != NULL) {
1006 x = tstate->async_exc;
1007 tstate->async_exc = NULL;
1008 PyErr_SetNone(x);
1009 Py_DECREF(x);
1010 why = WHY_EXCEPTION;
1011 goto on_error;
1012 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001013 }
1014#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001015 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001016
Neil Schemenauer63543862002-02-17 19:10:14 +00001017 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +00001018 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001019
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001020 /* line-by-line tracing support */
1021
Jeffrey Yasskinfd8a1ec2008-12-03 06:46:45 +00001022 if (_Py_TracingPossible &&
1023 tstate->c_tracefunc != NULL && !tstate->tracing) {
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001024 /* see maybe_call_line_trace
1025 for expository comments */
1026 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001027
Michael W. Hudson58ee2af2003-04-29 16:18:47 +00001028 err = maybe_call_line_trace(tstate->c_tracefunc,
1029 tstate->c_traceobj,
Armin Rigobf57a142004-03-22 19:24:58 +00001030 f, &instr_lb, &instr_ub,
1031 &instr_prev);
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001032 /* Reload possibly changed frame fields */
1033 JUMPTO(f->f_lasti);
Michael W. Hudson58ee2af2003-04-29 16:18:47 +00001034 if (f->f_stacktop != NULL) {
1035 stack_pointer = f->f_stacktop;
1036 f->f_stacktop = NULL;
1037 }
1038 if (err) {
1039 /* trace function raised an exception */
1040 goto on_error;
1041 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001042 }
1043
1044 /* Extract opcode and argument */
1045
Guido van Rossum374a9221991-04-04 10:40:29 +00001046 opcode = NEXTOP();
Armin Rigo8817fcd2004-06-17 10:22:40 +00001047 oparg = 0; /* allows oparg to be stored in a register because
1048 it doesn't have to be remembered across a full loop */
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001049 if (HAS_ARG(opcode))
1050 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00001051 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001052#ifdef DYNAMIC_EXECUTION_PROFILE
1053#ifdef DXPAIRS
1054 dxpairs[lastopcode][opcode]++;
1055 lastopcode = opcode;
1056#endif
1057 dxp[opcode]++;
1058#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001059
Guido van Rossum96a42c81992-01-12 02:29:51 +00001060#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +00001061 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001062
Guido van Rossum96a42c81992-01-12 02:29:51 +00001063 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001064 if (HAS_ARG(opcode)) {
1065 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001066 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001067 }
1068 else {
1069 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001070 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +00001071 }
1072 }
1073#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001074
Guido van Rossum374a9221991-04-04 10:40:29 +00001075 /* Main switch on opcode */
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001076 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001077
Guido van Rossum374a9221991-04-04 10:40:29 +00001078 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001079
Guido van Rossum374a9221991-04-04 10:40:29 +00001080 /* BEWARE!
1081 It is essential that any operation that fails sets either
1082 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1083 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001084
Guido van Rossum374a9221991-04-04 10:40:29 +00001085 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001086
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001087 case NOP:
1088 goto fast_next_opcode;
1089
Neil Schemenauer63543862002-02-17 19:10:14 +00001090 case LOAD_FAST:
1091 x = GETLOCAL(oparg);
1092 if (x != NULL) {
1093 Py_INCREF(x);
1094 PUSH(x);
1095 goto fast_next_opcode;
1096 }
1097 format_exc_check_arg(PyExc_UnboundLocalError,
1098 UNBOUNDLOCAL_ERROR_MSG,
1099 PyTuple_GetItem(co->co_varnames, oparg));
1100 break;
1101
1102 case LOAD_CONST:
Skip Montanaro04d80f82002-08-04 21:03:35 +00001103 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +00001104 Py_INCREF(x);
1105 PUSH(x);
1106 goto fast_next_opcode;
1107
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001108 PREDICTED_WITH_ARG(STORE_FAST);
Neil Schemenauer63543862002-02-17 19:10:14 +00001109 case STORE_FAST:
1110 v = POP();
1111 SETLOCAL(oparg, v);
1112 goto fast_next_opcode;
1113
Guido van Rossum374a9221991-04-04 10:40:29 +00001114 case POP_TOP:
1115 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001116 Py_DECREF(v);
Neil Schemenauer63543862002-02-17 19:10:14 +00001117 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001118
Guido van Rossum374a9221991-04-04 10:40:29 +00001119 case ROT_TWO:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001120 v = TOP();
1121 w = SECOND();
1122 SET_TOP(w);
1123 SET_SECOND(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +00001124 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001125
Guido van Rossum374a9221991-04-04 10:40:29 +00001126 case ROT_THREE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001127 v = TOP();
1128 w = SECOND();
1129 x = THIRD();
1130 SET_TOP(w);
1131 SET_SECOND(x);
1132 SET_THIRD(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +00001133 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001134
Thomas Wouters434d0822000-08-24 20:11:32 +00001135 case ROT_FOUR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001136 u = TOP();
1137 v = SECOND();
1138 w = THIRD();
1139 x = FOURTH();
1140 SET_TOP(v);
1141 SET_SECOND(w);
1142 SET_THIRD(x);
1143 SET_FOURTH(u);
Raymond Hettinger080cb322003-03-14 01:37:42 +00001144 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001145
Guido van Rossum374a9221991-04-04 10:40:29 +00001146 case DUP_TOP:
1147 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001148 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001149 PUSH(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +00001150 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001151
Thomas Wouters434d0822000-08-24 20:11:32 +00001152 case DUP_TOPX:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001153 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001154 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +00001155 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001156 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +00001157 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001158 STACKADJ(2);
1159 SET_TOP(x);
1160 SET_SECOND(w);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001161 goto fast_next_opcode;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001162 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001163 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +00001164 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001165 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +00001166 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001167 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +00001168 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001169 STACKADJ(3);
1170 SET_TOP(x);
1171 SET_SECOND(w);
1172 SET_THIRD(v);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001173 goto fast_next_opcode;
Thomas Wouters434d0822000-08-24 20:11:32 +00001174 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001175 Py_FatalError("invalid argument to DUP_TOPX"
1176 " (bytecode corruption?)");
Jeffrey Yasskin2d873bd2008-12-08 18:55:24 +00001177 /* Never returns, so don't bother to set why. */
Tim Peters35ba6892000-10-11 07:04:49 +00001178 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001179
Guido van Rossum374a9221991-04-04 10:40:29 +00001180 case UNARY_POSITIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001181 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001182 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001183 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001184 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001185 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001186 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001187
Guido van Rossum374a9221991-04-04 10:40:29 +00001188 case UNARY_NEGATIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001189 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001190 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001191 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001192 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001193 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001194 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001195
Guido van Rossum374a9221991-04-04 10:40:29 +00001196 case UNARY_NOT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001197 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001198 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001199 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +00001200 if (err == 0) {
1201 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001202 SET_TOP(Py_True);
Guido van Rossumfc490731997-05-06 15:06:49 +00001203 continue;
1204 }
1205 else if (err > 0) {
1206 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001207 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +00001208 err = 0;
1209 continue;
1210 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00001211 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001212 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001213
Guido van Rossum374a9221991-04-04 10:40:29 +00001214 case UNARY_CONVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001215 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001216 x = PyObject_Repr(v);
1217 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001218 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001219 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001220 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001221
Guido van Rossum7928cd71991-10-24 14:59:31 +00001222 case UNARY_INVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001223 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001224 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001225 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001226 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001227 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001228 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001229
Guido van Rossum50564e81996-01-12 01:13:16 +00001230 case BINARY_POWER:
1231 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001232 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001233 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +00001234 Py_DECREF(v);
1235 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001236 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001237 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +00001238 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001239
Guido van Rossum374a9221991-04-04 10:40:29 +00001240 case BINARY_MULTIPLY:
1241 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001242 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001243 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001244 Py_DECREF(v);
1245 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001246 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001247 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001248 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001249
Guido van Rossum374a9221991-04-04 10:40:29 +00001250 case BINARY_DIVIDE:
Tim Peters3caca232001-12-06 06:23:26 +00001251 if (!_Py_QnewFlag) {
1252 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001253 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001254 x = PyNumber_Divide(v, w);
1255 Py_DECREF(v);
1256 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001257 SET_TOP(x);
Tim Peters3caca232001-12-06 06:23:26 +00001258 if (x != NULL) continue;
1259 break;
1260 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001261 /* -Qnew is in effect: fall through to
Tim Peters3caca232001-12-06 06:23:26 +00001262 BINARY_TRUE_DIVIDE */
1263 case BINARY_TRUE_DIVIDE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001264 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001265 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001266 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001267 Py_DECREF(v);
1268 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001269 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001270 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001271 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001272
Guido van Rossum4668b002001-08-08 05:00:18 +00001273 case BINARY_FLOOR_DIVIDE:
1274 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001275 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001276 x = PyNumber_FloorDivide(v, w);
1277 Py_DECREF(v);
1278 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001279 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001280 if (x != NULL) continue;
1281 break;
1282
Guido van Rossum374a9221991-04-04 10:40:29 +00001283 case BINARY_MODULO:
1284 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001285 v = TOP();
Collin Winter8725dce2009-02-20 19:30:41 +00001286 if (PyString_CheckExact(v))
1287 x = PyString_Format(v, w);
1288 else
1289 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001290 Py_DECREF(v);
1291 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001292 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001293 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001294 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001295
Guido van Rossum374a9221991-04-04 10:40:29 +00001296 case BINARY_ADD:
1297 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001298 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001299 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001300 /* INLINE: int + int */
1301 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001302 a = PyInt_AS_LONG(v);
1303 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001304 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001305 if ((i^a) < 0 && (i^b) < 0)
1306 goto slow_add;
1307 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001308 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001309 else if (PyString_CheckExact(v) &&
1310 PyString_CheckExact(w)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001311 x = string_concatenate(v, w, f, next_instr);
1312 /* string_concatenate consumed the ref to v */
1313 goto skip_decref_vx;
1314 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001315 else {
1316 slow_add:
Guido van Rossumc12da691997-07-17 23:12:42 +00001317 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001318 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001319 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001320 skip_decref_vx:
Guido van Rossumb209a111997-04-29 18:18:01 +00001321 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001322 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001323 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001324 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001325
Guido van Rossum374a9221991-04-04 10:40:29 +00001326 case BINARY_SUBTRACT:
1327 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001328 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001329 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001330 /* INLINE: int - int */
1331 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001332 a = PyInt_AS_LONG(v);
1333 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001334 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001335 if ((i^a) < 0 && (i^~b) < 0)
1336 goto slow_sub;
1337 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001338 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001339 else {
1340 slow_sub:
Guido van Rossumc12da691997-07-17 23:12:42 +00001341 x = PyNumber_Subtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001342 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001343 Py_DECREF(v);
1344 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001345 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001346 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001347 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001348
Guido van Rossum374a9221991-04-04 10:40:29 +00001349 case BINARY_SUBSCR:
1350 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001351 v = TOP();
Tim Petersb1c46982001-10-05 20:41:38 +00001352 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001353 /* INLINE: list[int] */
Neal Norwitz814e9382006-03-02 07:54:28 +00001354 Py_ssize_t i = PyInt_AsSsize_t(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001355 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +00001356 i += PyList_GET_SIZE(v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001357 if (i >= 0 && i < PyList_GET_SIZE(v)) {
Guido van Rossumfa00e951998-07-08 15:02:37 +00001358 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001359 Py_INCREF(x);
1360 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001361 else
1362 goto slow_get;
Guido van Rossumc12da691997-07-17 23:12:42 +00001363 }
1364 else
Raymond Hettinger467a6982004-04-07 11:39:21 +00001365 slow_get:
Guido van Rossumc12da691997-07-17 23:12:42 +00001366 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001367 Py_DECREF(v);
1368 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001369 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001370 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001371 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001372
Guido van Rossum7928cd71991-10-24 14:59:31 +00001373 case BINARY_LSHIFT:
1374 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001375 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001376 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001377 Py_DECREF(v);
1378 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001379 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001380 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001381 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001382
Guido van Rossum7928cd71991-10-24 14:59:31 +00001383 case BINARY_RSHIFT:
1384 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001385 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001386 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001387 Py_DECREF(v);
1388 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001389 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001390 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001391 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001392
Guido van Rossum7928cd71991-10-24 14:59:31 +00001393 case BINARY_AND:
1394 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001395 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001396 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001397 Py_DECREF(v);
1398 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001399 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001400 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001401 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001402
Guido van Rossum7928cd71991-10-24 14:59:31 +00001403 case BINARY_XOR:
1404 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001405 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001406 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001407 Py_DECREF(v);
1408 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001409 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001410 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001411 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001412
Guido van Rossum7928cd71991-10-24 14:59:31 +00001413 case BINARY_OR:
1414 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001415 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001416 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001417 Py_DECREF(v);
1418 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001419 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001420 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001421 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001422
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001423 case LIST_APPEND:
1424 w = POP();
Antoine Pitroud0c35152008-12-17 00:38:28 +00001425 v = stack_pointer[-oparg];
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001426 err = PyList_Append(v, w);
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001427 Py_DECREF(w);
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001428 if (err == 0) {
1429 PREDICT(JUMP_ABSOLUTE);
1430 continue;
1431 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001432 break;
1433
Thomas Wouters434d0822000-08-24 20:11:32 +00001434 case INPLACE_POWER:
1435 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001436 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001437 x = PyNumber_InPlacePower(v, w, Py_None);
1438 Py_DECREF(v);
1439 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001440 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001441 if (x != NULL) continue;
1442 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001443
Thomas Wouters434d0822000-08-24 20:11:32 +00001444 case INPLACE_MULTIPLY:
1445 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001446 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001447 x = PyNumber_InPlaceMultiply(v, w);
1448 Py_DECREF(v);
1449 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001450 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001451 if (x != NULL) continue;
1452 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001453
Thomas Wouters434d0822000-08-24 20:11:32 +00001454 case INPLACE_DIVIDE:
Tim Peters54b11912001-12-25 18:49:11 +00001455 if (!_Py_QnewFlag) {
1456 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001457 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001458 x = PyNumber_InPlaceDivide(v, w);
1459 Py_DECREF(v);
1460 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001461 SET_TOP(x);
Tim Peters54b11912001-12-25 18:49:11 +00001462 if (x != NULL) continue;
1463 break;
1464 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001465 /* -Qnew is in effect: fall through to
Tim Peters54b11912001-12-25 18:49:11 +00001466 INPLACE_TRUE_DIVIDE */
1467 case INPLACE_TRUE_DIVIDE:
Thomas Wouters434d0822000-08-24 20:11:32 +00001468 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001469 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001470 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001471 Py_DECREF(v);
1472 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001473 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001474 if (x != NULL) continue;
1475 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001476
Guido van Rossum4668b002001-08-08 05:00:18 +00001477 case INPLACE_FLOOR_DIVIDE:
1478 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001479 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001480 x = PyNumber_InPlaceFloorDivide(v, w);
1481 Py_DECREF(v);
1482 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001483 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001484 if (x != NULL) continue;
1485 break;
1486
Thomas Wouters434d0822000-08-24 20:11:32 +00001487 case INPLACE_MODULO:
1488 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001489 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001490 x = PyNumber_InPlaceRemainder(v, w);
1491 Py_DECREF(v);
1492 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001493 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001494 if (x != NULL) continue;
1495 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001496
Thomas Wouters434d0822000-08-24 20:11:32 +00001497 case INPLACE_ADD:
1498 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001499 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001500 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001501 /* INLINE: int + int */
1502 register long a, b, i;
1503 a = PyInt_AS_LONG(v);
1504 b = PyInt_AS_LONG(w);
1505 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001506 if ((i^a) < 0 && (i^b) < 0)
1507 goto slow_iadd;
1508 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001509 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001510 else if (PyString_CheckExact(v) &&
1511 PyString_CheckExact(w)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001512 x = string_concatenate(v, w, f, next_instr);
1513 /* string_concatenate consumed the ref to v */
1514 goto skip_decref_v;
1515 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001516 else {
1517 slow_iadd:
Thomas Wouters434d0822000-08-24 20:11:32 +00001518 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001519 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001520 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001521 skip_decref_v:
Thomas Wouters434d0822000-08-24 20:11:32 +00001522 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001523 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001524 if (x != NULL) continue;
1525 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001526
Thomas Wouters434d0822000-08-24 20:11:32 +00001527 case INPLACE_SUBTRACT:
1528 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001529 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001530 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001531 /* INLINE: int - int */
1532 register long a, b, i;
1533 a = PyInt_AS_LONG(v);
1534 b = PyInt_AS_LONG(w);
1535 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001536 if ((i^a) < 0 && (i^~b) < 0)
1537 goto slow_isub;
1538 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001539 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001540 else {
1541 slow_isub:
Thomas Wouters434d0822000-08-24 20:11:32 +00001542 x = PyNumber_InPlaceSubtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001543 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001544 Py_DECREF(v);
1545 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001546 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001547 if (x != NULL) continue;
1548 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001549
Thomas Wouters434d0822000-08-24 20:11:32 +00001550 case INPLACE_LSHIFT:
1551 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001552 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001553 x = PyNumber_InPlaceLshift(v, w);
1554 Py_DECREF(v);
1555 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001556 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001557 if (x != NULL) continue;
1558 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001559
Thomas Wouters434d0822000-08-24 20:11:32 +00001560 case INPLACE_RSHIFT:
1561 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001562 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001563 x = PyNumber_InPlaceRshift(v, w);
1564 Py_DECREF(v);
1565 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001566 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001567 if (x != NULL) continue;
1568 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001569
Thomas Wouters434d0822000-08-24 20:11:32 +00001570 case INPLACE_AND:
1571 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001572 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001573 x = PyNumber_InPlaceAnd(v, w);
1574 Py_DECREF(v);
1575 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001576 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001577 if (x != NULL) continue;
1578 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001579
Thomas Wouters434d0822000-08-24 20:11:32 +00001580 case INPLACE_XOR:
1581 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001582 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001583 x = PyNumber_InPlaceXor(v, w);
1584 Py_DECREF(v);
1585 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001586 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001587 if (x != NULL) continue;
1588 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001589
Thomas Wouters434d0822000-08-24 20:11:32 +00001590 case INPLACE_OR:
1591 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001592 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001593 x = PyNumber_InPlaceOr(v, w);
1594 Py_DECREF(v);
1595 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001596 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001597 if (x != NULL) continue;
1598 break;
1599
Guido van Rossum374a9221991-04-04 10:40:29 +00001600 case SLICE+0:
1601 case SLICE+1:
1602 case SLICE+2:
1603 case SLICE+3:
1604 if ((opcode-SLICE) & 2)
1605 w = POP();
1606 else
1607 w = NULL;
1608 if ((opcode-SLICE) & 1)
1609 v = POP();
1610 else
1611 v = NULL;
Raymond Hettinger663004b2003-01-09 15:24:30 +00001612 u = TOP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001613 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001614 Py_DECREF(u);
1615 Py_XDECREF(v);
1616 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001617 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001618 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001619 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001620
Guido van Rossum374a9221991-04-04 10:40:29 +00001621 case STORE_SLICE+0:
1622 case STORE_SLICE+1:
1623 case STORE_SLICE+2:
1624 case STORE_SLICE+3:
1625 if ((opcode-STORE_SLICE) & 2)
1626 w = POP();
1627 else
1628 w = NULL;
1629 if ((opcode-STORE_SLICE) & 1)
1630 v = POP();
1631 else
1632 v = NULL;
1633 u = POP();
1634 t = POP();
1635 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +00001636 Py_DECREF(t);
1637 Py_DECREF(u);
1638 Py_XDECREF(v);
1639 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001640 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001641 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001642
Guido van Rossum374a9221991-04-04 10:40:29 +00001643 case DELETE_SLICE+0:
1644 case DELETE_SLICE+1:
1645 case DELETE_SLICE+2:
1646 case DELETE_SLICE+3:
1647 if ((opcode-DELETE_SLICE) & 2)
1648 w = POP();
1649 else
1650 w = NULL;
1651 if ((opcode-DELETE_SLICE) & 1)
1652 v = POP();
1653 else
1654 v = NULL;
1655 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001656 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +00001657 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00001658 Py_DECREF(u);
1659 Py_XDECREF(v);
1660 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001661 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001662 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001663
Guido van Rossum374a9221991-04-04 10:40:29 +00001664 case STORE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001665 w = TOP();
1666 v = SECOND();
1667 u = THIRD();
1668 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001669 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001670 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001671 Py_DECREF(u);
1672 Py_DECREF(v);
1673 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001674 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001675 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001676
Guido van Rossum374a9221991-04-04 10:40:29 +00001677 case DELETE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001678 w = TOP();
1679 v = SECOND();
1680 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001681 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001682 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001683 Py_DECREF(v);
1684 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001685 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001686 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001687
Guido van Rossum374a9221991-04-04 10:40:29 +00001688 case PRINT_EXPR:
1689 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001690 w = PySys_GetObject("displayhook");
1691 if (w == NULL) {
1692 PyErr_SetString(PyExc_RuntimeError,
1693 "lost sys.displayhook");
1694 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001695 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001696 }
1697 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001698 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001699 if (x == NULL)
1700 err = -1;
1701 }
1702 if (err == 0) {
1703 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001704 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001705 if (w == NULL)
1706 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001707 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001708 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001709 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001710 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001711
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001712 case PRINT_ITEM_TO:
1713 w = stream = POP();
1714 /* fall through to PRINT_ITEM */
1715
Guido van Rossum374a9221991-04-04 10:40:29 +00001716 case PRINT_ITEM:
1717 v = POP();
Barry Warsaw093abe02000-08-29 04:56:13 +00001718 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001719 w = PySys_GetObject("stdout");
1720 if (w == NULL) {
1721 PyErr_SetString(PyExc_RuntimeError,
1722 "lost sys.stdout");
1723 err = -1;
1724 }
Guido van Rossum8f183201997-12-31 05:53:15 +00001725 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001726 /* PyFile_SoftSpace() can exececute arbitrary code
1727 if sys.stdout is an instance with a __getattr__.
1728 If __getattr__ raises an exception, w will
1729 be freed, so we need to prevent that temporarily. */
1730 Py_XINCREF(w);
Tim Peters8e5fd532002-03-24 19:25:00 +00001731 if (w != NULL && PyFile_SoftSpace(w, 0))
Guido van Rossumbe270261997-05-22 22:26:18 +00001732 err = PyFile_WriteString(" ", w);
1733 if (err == 0)
1734 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001735 if (err == 0) {
Tim Peters8e5fd532002-03-24 19:25:00 +00001736 /* XXX move into writeobject() ? */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001737 if (PyString_Check(v)) {
1738 char *s = PyString_AS_STRING(v);
1739 Py_ssize_t len = PyString_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001740 if (len == 0 ||
1741 !isspace(Py_CHARMASK(s[len-1])) ||
1742 s[len-1] == ' ')
1743 PyFile_SoftSpace(w, 1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001744 }
Martin v. Löwis8d3ce5a2001-12-18 22:36:40 +00001745#ifdef Py_USING_UNICODE
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001746 else if (PyUnicode_Check(v)) {
1747 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
Martin v. Löwis66851282006-04-22 11:40:03 +00001748 Py_ssize_t len = PyUnicode_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001749 if (len == 0 ||
1750 !Py_UNICODE_ISSPACE(s[len-1]) ||
1751 s[len-1] == ' ')
1752 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001753 }
Michael W. Hudsond95c8282002-05-20 13:56:11 +00001754#endif
Tim Peters8e5fd532002-03-24 19:25:00 +00001755 else
1756 PyFile_SoftSpace(w, 1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001757 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001758 Py_XDECREF(w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001759 Py_DECREF(v);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001760 Py_XDECREF(stream);
1761 stream = NULL;
1762 if (err == 0)
1763 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001764 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001765
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001766 case PRINT_NEWLINE_TO:
1767 w = stream = POP();
1768 /* fall through to PRINT_NEWLINE */
1769
Guido van Rossum374a9221991-04-04 10:40:29 +00001770 case PRINT_NEWLINE:
Barry Warsaw093abe02000-08-29 04:56:13 +00001771 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001772 w = PySys_GetObject("stdout");
Jeffrey Yasskin2d873bd2008-12-08 18:55:24 +00001773 if (w == NULL) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001774 PyErr_SetString(PyExc_RuntimeError,
1775 "lost sys.stdout");
Jeffrey Yasskin2d873bd2008-12-08 18:55:24 +00001776 why = WHY_EXCEPTION;
1777 }
Guido van Rossum3165fe61992-09-25 21:59:05 +00001778 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001779 if (w != NULL) {
Georg Brandlaa76d772008-07-01 20:56:03 +00001780 /* w.write() may replace sys.stdout, so we
1781 * have to keep our reference to it */
Amaury Forgeot d'Arcbdd941f2008-07-01 20:38:04 +00001782 Py_INCREF(w);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001783 err = PyFile_WriteString("\n", w);
1784 if (err == 0)
1785 PyFile_SoftSpace(w, 0);
Amaury Forgeot d'Arcbdd941f2008-07-01 20:38:04 +00001786 Py_DECREF(w);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001787 }
1788 Py_XDECREF(stream);
1789 stream = NULL;
Guido van Rossum374a9221991-04-04 10:40:29 +00001790 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001791
Thomas Wouters434d0822000-08-24 20:11:32 +00001792
1793#ifdef CASE_TOO_BIG
1794 default: switch (opcode) {
1795#endif
Guido van Rossumf10570b1995-07-07 22:53:21 +00001796 case RAISE_VARARGS:
1797 u = v = w = NULL;
1798 switch (oparg) {
1799 case 3:
1800 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001801 /* Fallthrough */
1802 case 2:
1803 v = POP(); /* value */
1804 /* Fallthrough */
1805 case 1:
1806 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001807 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001808 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001809 break;
1810 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001811 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001812 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001813 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001814 break;
1815 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001816 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001817
Guido van Rossum374a9221991-04-04 10:40:29 +00001818 case LOAD_LOCALS:
Raymond Hettinger467a6982004-04-07 11:39:21 +00001819 if ((x = f->f_locals) != NULL) {
1820 Py_INCREF(x);
1821 PUSH(x);
1822 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001823 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001824 PyErr_SetString(PyExc_SystemError, "no locals");
Guido van Rossum374a9221991-04-04 10:40:29 +00001825 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001826
Guido van Rossum374a9221991-04-04 10:40:29 +00001827 case RETURN_VALUE:
1828 retval = POP();
1829 why = WHY_RETURN;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001830 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001831
Tim Peters5ca576e2001-06-18 22:08:13 +00001832 case YIELD_VALUE:
1833 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001834 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001835 why = WHY_YIELD;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001836 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001837
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001838 case EXEC_STMT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001839 w = TOP();
1840 v = SECOND();
1841 u = THIRD();
1842 STACKADJ(-3);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001843 READ_TIMESTAMP(intr0);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001844 err = exec_statement(f, u, v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001845 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00001846 Py_DECREF(u);
1847 Py_DECREF(v);
1848 Py_DECREF(w);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001849 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001850
Guido van Rossum374a9221991-04-04 10:40:29 +00001851 case POP_BLOCK:
1852 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001853 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001854 while (STACK_LEVEL() > b->b_level) {
1855 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001856 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001857 }
1858 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001859 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001860
Jeffrey Yasskin9063a992008-03-03 01:27:03 +00001861 PREDICTED(END_FINALLY);
Guido van Rossum374a9221991-04-04 10:40:29 +00001862 case END_FINALLY:
1863 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001864 if (PyInt_Check(v)) {
Raymond Hettinger7c958652004-04-06 10:11:10 +00001865 why = (enum why_code) PyInt_AS_LONG(v);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001866 assert(why != WHY_YIELD);
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00001867 if (why == WHY_RETURN ||
1868 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001869 retval = POP();
1870 }
Thomas Wouterse2176022007-09-20 17:35:10 +00001871 else if (PyExceptionClass_Check(v) ||
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001872 PyString_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001873 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001874 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001875 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001876 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001877 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001878 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001879 else if (v != Py_None) {
1880 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001881 "'finally' pops bad exception");
1882 why = WHY_EXCEPTION;
1883 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001884 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001885 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001886
Guido van Rossum374a9221991-04-04 10:40:29 +00001887 case BUILD_CLASS:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001888 u = TOP();
1889 v = SECOND();
1890 w = THIRD();
1891 STACKADJ(-2);
Guido van Rossum25831651993-05-19 14:50:45 +00001892 x = build_class(u, v, w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001893 SET_TOP(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001894 Py_DECREF(u);
1895 Py_DECREF(v);
1896 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001897 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001898
Guido van Rossum374a9221991-04-04 10:40:29 +00001899 case STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001900 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001901 v = POP();
Raymond Hettinger467a6982004-04-07 11:39:21 +00001902 if ((x = f->f_locals) != NULL) {
Raymond Hettinger66bd2332004-08-02 08:30:07 +00001903 if (PyDict_CheckExact(x))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001904 err = PyDict_SetItem(x, w, v);
1905 else
1906 err = PyObject_SetItem(x, w, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001907 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001908 if (err == 0) continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001909 break;
1910 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001911 PyErr_Format(PyExc_SystemError,
1912 "no locals found when storing %s",
1913 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001914 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001915
Guido van Rossum374a9221991-04-04 10:40:29 +00001916 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001917 w = GETITEM(names, oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001918 if ((x = f->f_locals) != NULL) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001919 if ((err = PyObject_DelItem(x, w)) != 0)
Raymond Hettinger467a6982004-04-07 11:39:21 +00001920 format_exc_check_arg(PyExc_NameError,
Thomas Wouterse2176022007-09-20 17:35:10 +00001921 NAME_ERROR_MSG,
1922 w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001923 break;
1924 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001925 PyErr_Format(PyExc_SystemError,
1926 "no locals when deleting %s",
1927 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001928 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001929
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001930 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001931 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001932 v = POP();
Thomas Wouterse2176022007-09-20 17:35:10 +00001933 if (PyTuple_CheckExact(v) &&
1934 PyTuple_GET_SIZE(v) == oparg) {
1935 PyObject **items = \
1936 ((PyTupleObject *)v)->ob_item;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001937 while (oparg--) {
1938 w = items[oparg];
1939 Py_INCREF(w);
1940 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001941 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001942 Py_DECREF(v);
1943 continue;
Thomas Wouterse2176022007-09-20 17:35:10 +00001944 } else if (PyList_CheckExact(v) &&
1945 PyList_GET_SIZE(v) == oparg) {
1946 PyObject **items = \
1947 ((PyListObject *)v)->ob_item;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001948 while (oparg--) {
1949 w = items[oparg];
1950 Py_INCREF(w);
1951 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001952 }
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001953 } else if (unpack_iterable(v, oparg,
Thomas Wouterse2176022007-09-20 17:35:10 +00001954 stack_pointer + oparg)) {
Tim Petersd6d010b2001-06-21 02:49:55 +00001955 stack_pointer += oparg;
Georg Brandl5cb76c12007-03-21 09:00:39 +00001956 } else {
1957 /* unpack_iterable() raised an exception */
Barry Warsawe42b18f1997-08-25 22:13:04 +00001958 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001959 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001960 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001961 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001962
Guido van Rossum374a9221991-04-04 10:40:29 +00001963 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001964 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001965 v = TOP();
1966 u = SECOND();
1967 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001968 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1969 Py_DECREF(v);
1970 Py_DECREF(u);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001971 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001972 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001973
Guido van Rossum374a9221991-04-04 10:40:29 +00001974 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001975 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001976 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001977 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1978 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001979 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001980 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001981
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001982 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001983 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001984 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001985 err = PyDict_SetItem(f->f_globals, w, v);
1986 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001987 if (err == 0) continue;
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001988 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001989
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001990 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001991 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001992 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001993 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001994 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001995 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001996
Guido van Rossum374a9221991-04-04 10:40:29 +00001997 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001998 w = GETITEM(names, oparg);
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001999 if ((v = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00002000 PyErr_Format(PyExc_SystemError,
2001 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00002002 PyObject_REPR(w));
Jeffrey Yasskin2d873bd2008-12-08 18:55:24 +00002003 why = WHY_EXCEPTION;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002004 break;
2005 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00002006 if (PyDict_CheckExact(v)) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00002007 x = PyDict_GetItem(v, w);
Michael W. Hudsona3711f72004-08-02 14:50:43 +00002008 Py_XINCREF(x);
2009 }
Raymond Hettinger214b1c32004-07-02 06:41:07 +00002010 else {
2011 x = PyObject_GetItem(v, w);
2012 if (x == NULL && PyErr_Occurred()) {
Thomas Wouterse2176022007-09-20 17:35:10 +00002013 if (!PyErr_ExceptionMatches(
2014 PyExc_KeyError))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00002015 break;
2016 PyErr_Clear();
2017 }
2018 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002019 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002020 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002021 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002022 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002023 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00002024 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00002025 PyExc_NameError,
Thomas Wouterse2176022007-09-20 17:35:10 +00002026 NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002027 break;
2028 }
2029 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00002030 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00002031 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002032 PUSH(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002033 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002034
Guido van Rossum374a9221991-04-04 10:40:29 +00002035 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00002036 w = GETITEM(names, oparg);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002037 if (PyString_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00002038 /* Inline the PyDict_GetItem() calls.
2039 WARNING: this is an extreme speed hack.
2040 Do not try this at home. */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002041 long hash = ((PyStringObject *)w)->ob_shash;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002042 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002043 PyDictObject *d;
Armin Rigo35f6d362006-06-01 13:19:12 +00002044 PyDictEntry *e;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002045 d = (PyDictObject *)(f->f_globals);
Armin Rigo35f6d362006-06-01 13:19:12 +00002046 e = d->ma_lookup(d, w, hash);
2047 if (e == NULL) {
2048 x = NULL;
2049 break;
2050 }
2051 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002052 if (x != NULL) {
2053 Py_INCREF(x);
2054 PUSH(x);
2055 continue;
2056 }
2057 d = (PyDictObject *)(f->f_builtins);
Armin Rigo35f6d362006-06-01 13:19:12 +00002058 e = d->ma_lookup(d, w, hash);
2059 if (e == NULL) {
2060 x = NULL;
2061 break;
2062 }
2063 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002064 if (x != NULL) {
2065 Py_INCREF(x);
2066 PUSH(x);
2067 continue;
2068 }
2069 goto load_global_error;
2070 }
2071 }
2072 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00002073 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002074 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002075 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002076 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002077 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00002078 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00002079 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002080 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002081 break;
2082 }
2083 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002084 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00002085 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00002086 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002087
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002088 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00002089 x = GETLOCAL(oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002090 if (x != NULL) {
2091 SETLOCAL(oparg, NULL);
2092 continue;
Guido van Rossum2e4c8991998-05-12 20:27:36 +00002093 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00002094 format_exc_check_arg(
2095 PyExc_UnboundLocalError,
2096 UNBOUNDLOCAL_ERROR_MSG,
2097 PyTuple_GetItem(co->co_varnames, oparg)
2098 );
2099 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002100
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002101 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00002102 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002103 Py_INCREF(x);
2104 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00002105 if (x != NULL) continue;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002106 break;
2107
2108 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00002109 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002110 w = PyCell_Get(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002111 if (w != NULL) {
2112 PUSH(w);
2113 continue;
Jeremy Hylton2524d692001-02-05 17:23:16 +00002114 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00002115 err = -1;
2116 /* Don't stomp existing exception */
2117 if (PyErr_Occurred())
2118 break;
Richard Jonescebbefc2006-05-23 18:28:17 +00002119 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
2120 v = PyTuple_GET_ITEM(co->co_cellvars,
Raymond Hettinger467a6982004-04-07 11:39:21 +00002121 oparg);
2122 format_exc_check_arg(
2123 PyExc_UnboundLocalError,
2124 UNBOUNDLOCAL_ERROR_MSG,
2125 v);
2126 } else {
Thomas Wouterse2176022007-09-20 17:35:10 +00002127 v = PyTuple_GET_ITEM(co->co_freevars, oparg -
2128 PyTuple_GET_SIZE(co->co_cellvars));
2129 format_exc_check_arg(PyExc_NameError,
2130 UNBOUNDFREE_ERROR_MSG, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002131 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002132 break;
2133
2134 case STORE_DEREF:
2135 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00002136 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002137 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00002138 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002139 continue;
2140
Guido van Rossum374a9221991-04-04 10:40:29 +00002141 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00002142 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002143 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002144 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002145 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002146 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002147 }
2148 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002149 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002150 }
2151 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002152
Guido van Rossum374a9221991-04-04 10:40:29 +00002153 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00002154 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002155 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002156 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002157 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00002158 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002159 }
2160 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002161 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002162 }
2163 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002164
Guido van Rossum374a9221991-04-04 10:40:29 +00002165 case BUILD_MAP:
Raymond Hettingerfd7ed402007-12-18 21:24:09 +00002166 x = _PyDict_NewPresized((Py_ssize_t)oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002167 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002168 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002169 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002170
Raymond Hettingereffde122007-12-18 18:26:18 +00002171 case STORE_MAP:
2172 w = TOP(); /* key */
2173 u = SECOND(); /* value */
2174 v = THIRD(); /* dict */
2175 STACKADJ(-2);
2176 assert (PyDict_CheckExact(v));
2177 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2178 Py_DECREF(u);
2179 Py_DECREF(w);
2180 if (err == 0) continue;
2181 break;
2182
Guido van Rossum374a9221991-04-04 10:40:29 +00002183 case LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00002184 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002185 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002186 x = PyObject_GetAttr(v, w);
2187 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002188 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002189 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002190 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002191
Guido van Rossum374a9221991-04-04 10:40:29 +00002192 case COMPARE_OP:
2193 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002194 v = TOP();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00002195 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00002196 /* INLINE: cmp(int, int) */
2197 register long a, b;
2198 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00002199 a = PyInt_AS_LONG(v);
2200 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00002201 switch (oparg) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00002202 case PyCmp_LT: res = a < b; break;
2203 case PyCmp_LE: res = a <= b; break;
2204 case PyCmp_EQ: res = a == b; break;
2205 case PyCmp_NE: res = a != b; break;
2206 case PyCmp_GT: res = a > b; break;
2207 case PyCmp_GE: res = a >= b; break;
2208 case PyCmp_IS: res = v == w; break;
2209 case PyCmp_IS_NOT: res = v != w; break;
Guido van Rossumc12da691997-07-17 23:12:42 +00002210 default: goto slow_compare;
2211 }
2212 x = res ? Py_True : Py_False;
2213 Py_INCREF(x);
2214 }
2215 else {
2216 slow_compare:
2217 x = cmp_outcome(oparg, v, w);
2218 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002219 Py_DECREF(v);
2220 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002221 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00002222 if (x == NULL) break;
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002223 PREDICT(POP_JUMP_IF_FALSE);
2224 PREDICT(POP_JUMP_IF_TRUE);
Raymond Hettingerf606f872003-03-16 03:11:04 +00002225 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002226
Guido van Rossum374a9221991-04-04 10:40:29 +00002227 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00002228 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002229 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002230 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002231 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00002232 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002233 break;
2234 }
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00002235 Py_INCREF(x);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002236 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002237 u = TOP();
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002238 if (PyInt_AsLong(u) != -1 || PyErr_Occurred())
2239 w = PyTuple_Pack(5,
2240 w,
2241 f->f_globals,
2242 f->f_locals == NULL ?
2243 Py_None : f->f_locals,
2244 v,
2245 u);
2246 else
2247 w = PyTuple_Pack(4,
2248 w,
2249 f->f_globals,
2250 f->f_locals == NULL ?
2251 Py_None : f->f_locals,
2252 v);
2253 Py_DECREF(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002254 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002255 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002256 u = POP();
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00002257 Py_DECREF(x);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002258 x = NULL;
2259 break;
2260 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002261 READ_TIMESTAMP(intr0);
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00002262 v = x;
2263 x = PyEval_CallObject(v, w);
2264 Py_DECREF(v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002265 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002266 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002267 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002268 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002269 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002270
Thomas Wouters52152252000-08-17 22:55:00 +00002271 case IMPORT_STAR:
2272 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002273 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002274 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002275 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00002276 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002277 break;
2278 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002279 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002280 err = import_all_from(x, v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002281 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002282 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00002283 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002284 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002285 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002286
Thomas Wouters52152252000-08-17 22:55:00 +00002287 case IMPORT_FROM:
Skip Montanaro496e6582002-08-06 17:47:40 +00002288 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00002289 v = TOP();
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002290 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002291 x = import_from(v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002292 READ_TIMESTAMP(intr1);
Thomas Wouters52152252000-08-17 22:55:00 +00002293 PUSH(x);
2294 if (x != NULL) continue;
2295 break;
2296
Guido van Rossum374a9221991-04-04 10:40:29 +00002297 case JUMP_FORWARD:
2298 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002299 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002300
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002301 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
2302 case POP_JUMP_IF_FALSE:
2303 w = POP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002304 if (w == Py_True) {
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002305 Py_DECREF(w);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002306 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002307 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002308 if (w == Py_False) {
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002309 Py_DECREF(w);
2310 JUMPTO(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002311 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002312 }
2313 err = PyObject_IsTrue(w);
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002314 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002315 if (err > 0)
2316 err = 0;
2317 else if (err == 0)
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002318 JUMPTO(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002319 else
2320 break;
2321 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002322
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002323 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
2324 case POP_JUMP_IF_TRUE:
2325 w = POP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002326 if (w == Py_False) {
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002327 Py_DECREF(w);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002328 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002329 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002330 if (w == Py_True) {
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002331 Py_DECREF(w);
2332 JUMPTO(oparg);
2333 goto fast_next_opcode;
2334 }
2335 err = PyObject_IsTrue(w);
2336 Py_DECREF(w);
2337 if (err > 0) {
2338 err = 0;
2339 JUMPTO(oparg);
2340 }
2341 else if (err == 0)
2342 ;
2343 else
2344 break;
2345 continue;
2346
2347 case JUMP_IF_FALSE_OR_POP:
2348 w = TOP();
2349 if (w == Py_True) {
2350 STACKADJ(-1);
2351 Py_DECREF(w);
2352 goto fast_next_opcode;
2353 }
2354 if (w == Py_False) {
2355 JUMPTO(oparg);
2356 goto fast_next_opcode;
2357 }
2358 err = PyObject_IsTrue(w);
2359 if (err > 0) {
2360 STACKADJ(-1);
2361 Py_DECREF(w);
2362 err = 0;
2363 }
2364 else if (err == 0)
2365 JUMPTO(oparg);
2366 else
2367 break;
2368 continue;
2369
2370 case JUMP_IF_TRUE_OR_POP:
2371 w = TOP();
2372 if (w == Py_False) {
2373 STACKADJ(-1);
2374 Py_DECREF(w);
2375 goto fast_next_opcode;
2376 }
2377 if (w == Py_True) {
2378 JUMPTO(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002379 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002380 }
2381 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002382 if (err > 0) {
2383 err = 0;
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002384 JUMPTO(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002385 }
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002386 else if (err == 0) {
2387 STACKADJ(-1);
2388 Py_DECREF(w);
2389 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002390 else
2391 break;
2392 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002393
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00002394 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002395 case JUMP_ABSOLUTE:
2396 JUMPTO(oparg);
Raymond Hettingerdc1d1ba2007-11-07 02:45:46 +00002397#if FAST_LOOPS
2398 /* Enabling this path speeds-up all while and for-loops by bypassing
2399 the per-loop checks for signals. By default, this should be turned-off
2400 because it prevents detection of a control-break in tight loops like
2401 "while 1: pass". Compile with this option turned-on when you need
2402 the speed-up and do not need break checking inside tight loops (ones
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00002403 that contain only instructions ending with goto fast_next_opcode).
Raymond Hettingerdc1d1ba2007-11-07 02:45:46 +00002404 */
2405 goto fast_next_opcode;
2406#else
Neil Schemenauerca2a2f12003-05-30 23:59:44 +00002407 continue;
Raymond Hettingerdc1d1ba2007-11-07 02:45:46 +00002408#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002409
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002410 case GET_ITER:
2411 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002412 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002413 x = PyObject_GetIter(v);
2414 Py_DECREF(v);
2415 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002416 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002417 PREDICT(FOR_ITER);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002418 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002419 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002420 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002421 break;
2422
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002423 PREDICTED_WITH_ARG(FOR_ITER);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002424 case FOR_ITER:
2425 /* before: [iter]; after: [iter, iter()] *or* [] */
2426 v = TOP();
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002427 x = (*v->ob_type->tp_iternext)(v);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002428 if (x != NULL) {
2429 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002430 PREDICT(STORE_FAST);
2431 PREDICT(UNPACK_SEQUENCE);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002432 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002433 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002434 if (PyErr_Occurred()) {
Thomas Wouterse2176022007-09-20 17:35:10 +00002435 if (!PyErr_ExceptionMatches(
2436 PyExc_StopIteration))
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002437 break;
2438 PyErr_Clear();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002439 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002440 /* iterator ended normally */
2441 x = v = POP();
2442 Py_DECREF(v);
2443 JUMPBY(oparg);
2444 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002445
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002446 case BREAK_LOOP:
2447 why = WHY_BREAK;
2448 goto fast_block_end;
2449
2450 case CONTINUE_LOOP:
2451 retval = PyInt_FromLong(oparg);
Neal Norwitz02104df2006-05-19 06:31:23 +00002452 if (!retval) {
2453 x = NULL;
2454 break;
2455 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002456 why = WHY_CONTINUE;
2457 goto fast_block_end;
2458
Guido van Rossum374a9221991-04-04 10:40:29 +00002459 case SETUP_LOOP:
2460 case SETUP_EXCEPT:
2461 case SETUP_FINALLY:
Thomas Wouterse2176022007-09-20 17:35:10 +00002462 /* NOTE: If you add any new block-setup opcodes that
2463 are not try/except/finally handlers, you may need
2464 to update the PyGen_NeedsFinalizing() function.
2465 */
Phillip J. Eby2ba96612006-04-10 17:51:05 +00002466
Guido van Rossumb209a111997-04-29 18:18:01 +00002467 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002468 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002469 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002470
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00002471 case SETUP_WITH:
2472 {
2473 static PyObject *exit, *enter;
2474 w = TOP();
2475 x = special_lookup(w, "__exit__", &exit);
2476 if (!x)
2477 break;
2478 SET_TOP(x);
2479 u = special_lookup(w, "__enter__", &enter);
2480 Py_DECREF(w);
2481 if (!u) {
2482 x = NULL;
2483 break;
2484 }
2485 x = PyObject_CallFunctionObjArgs(u, NULL);
2486 Py_DECREF(u);
2487 if (!x)
2488 break;
2489 /* Setup the finally block before pushing the result
2490 of __enter__ on the stack. */
2491 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2492 STACK_LEVEL());
2493
2494 PUSH(x);
2495 continue;
2496 }
2497
Guido van Rossumc2e20742006-02-27 22:32:47 +00002498 case WITH_CLEANUP:
2499 {
Nick Coghlan7af53be2008-03-07 14:13:28 +00002500 /* At the top of the stack are 1-3 values indicating
2501 how/why we entered the finally clause:
2502 - TOP = None
2503 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2504 - TOP = WHY_*; no retval below it
2505 - (TOP, SECOND, THIRD) = exc_info()
2506 Below them is EXIT, the context.__exit__ bound method.
Guido van Rossumc2e20742006-02-27 22:32:47 +00002507 In the last case, we must call
Nick Coghlan7af53be2008-03-07 14:13:28 +00002508 EXIT(TOP, SECOND, THIRD)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002509 otherwise we must call
Nick Coghlan7af53be2008-03-07 14:13:28 +00002510 EXIT(None, None, None)
2511
2512 In all cases, we remove EXIT from the stack, leaving
2513 the rest in the same order.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002514
2515 In addition, if the stack represents an exception,
Guido van Rossumf6694362006-03-10 02:28:35 +00002516 *and* the function call returns a 'true' value, we
2517 "zap" this information, to prevent END_FINALLY from
2518 re-raising the exception. (But non-local gotos
2519 should still be resumed.)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002520 */
Tim Peters7df5e7f2006-05-26 23:14:37 +00002521
Nick Coghlan7af53be2008-03-07 14:13:28 +00002522 PyObject *exit_func;
2523
2524 u = POP();
2525 if (u == Py_None) {
2526 exit_func = TOP();
2527 SET_TOP(u);
2528 v = w = Py_None;
2529 }
2530 else if (PyInt_Check(u)) {
2531 switch(PyInt_AS_LONG(u)) {
2532 case WHY_RETURN:
2533 case WHY_CONTINUE:
2534 /* Retval in TOP. */
2535 exit_func = SECOND();
2536 SET_SECOND(TOP());
2537 SET_TOP(u);
2538 break;
2539 default:
2540 exit_func = TOP();
2541 SET_TOP(u);
2542 break;
2543 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002544 u = v = w = Py_None;
2545 }
2546 else {
Nick Coghlan7af53be2008-03-07 14:13:28 +00002547 v = TOP();
2548 w = SECOND();
2549 exit_func = THIRD();
2550 SET_TOP(u);
2551 SET_SECOND(v);
2552 SET_THIRD(w);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002553 }
Guido van Rossumf6694362006-03-10 02:28:35 +00002554 /* XXX Not the fastest way to call it... */
Nick Coghlan7af53be2008-03-07 14:13:28 +00002555 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2556 NULL);
Amaury Forgeot d'Arcad9b5992008-12-10 23:22:49 +00002557 Py_DECREF(exit_func);
2558 if (x == NULL)
Guido van Rossumf6694362006-03-10 02:28:35 +00002559 break; /* Go to error exit */
Amaury Forgeot d'Arcad9b5992008-12-10 23:22:49 +00002560
2561 if (u != Py_None)
2562 err = PyObject_IsTrue(x);
2563 else
2564 err = 0;
2565 Py_DECREF(x);
2566
2567 if (err < 0)
2568 break; /* Go to error exit */
2569 else if (err > 0) {
2570 err = 0;
Guido van Rossumf6694362006-03-10 02:28:35 +00002571 /* There was an exception and a true return */
Nick Coghlan7af53be2008-03-07 14:13:28 +00002572 STACKADJ(-2);
Guido van Rossumf6694362006-03-10 02:28:35 +00002573 Py_INCREF(Py_None);
2574 SET_TOP(Py_None);
Guido van Rossumf6694362006-03-10 02:28:35 +00002575 Py_DECREF(u);
2576 Py_DECREF(v);
2577 Py_DECREF(w);
2578 } else {
Nick Coghlan7af53be2008-03-07 14:13:28 +00002579 /* The stack was rearranged to remove EXIT
2580 above. Let END_FINALLY do its thing */
Guido van Rossumf6694362006-03-10 02:28:35 +00002581 }
Jeffrey Yasskin9063a992008-03-03 01:27:03 +00002582 PREDICT(END_FINALLY);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002583 break;
2584 }
2585
Guido van Rossumf10570b1995-07-07 22:53:21 +00002586 case CALL_FUNCTION:
Armin Rigo8817fcd2004-06-17 10:22:40 +00002587 {
2588 PyObject **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002589 PCALL(PCALL_ALL);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002590 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002591#ifdef WITH_TSC
Armin Rigo8817fcd2004-06-17 10:22:40 +00002592 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002593#else
Armin Rigo8817fcd2004-06-17 10:22:40 +00002594 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002595#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002596 stack_pointer = sp;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002597 PUSH(x);
2598 if (x != NULL)
2599 continue;
2600 break;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002601 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002602
Jeremy Hylton76901512000-03-28 23:49:17 +00002603 case CALL_FUNCTION_VAR:
2604 case CALL_FUNCTION_KW:
2605 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002606 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002607 int na = oparg & 0xff;
2608 int nk = (oparg>>8) & 0xff;
2609 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002610 int n = na + 2 * nk;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002611 PyObject **pfunc, *func, **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002612 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002613 if (flags & CALL_FLAG_VAR)
2614 n++;
2615 if (flags & CALL_FLAG_KW)
2616 n++;
2617 pfunc = stack_pointer - n - 1;
2618 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002619
Guido van Rossumac7be682001-01-17 15:42:30 +00002620 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002621 && PyMethod_GET_SELF(func) != NULL) {
2622 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002623 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002624 func = PyMethod_GET_FUNCTION(func);
2625 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002626 Py_DECREF(*pfunc);
2627 *pfunc = self;
2628 na++;
2629 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002630 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002631 Py_INCREF(func);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002632 sp = stack_pointer;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002633 READ_TIMESTAMP(intr0);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002634 x = ext_do_call(func, &sp, flags, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002635 READ_TIMESTAMP(intr1);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002636 stack_pointer = sp;
Jeremy Hylton76901512000-03-28 23:49:17 +00002637 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002638
Jeremy Hylton76901512000-03-28 23:49:17 +00002639 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002640 w = POP();
2641 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002642 }
2643 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002644 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002645 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002646 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002647 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002648
Guido van Rossum681d79a1995-07-18 14:51:37 +00002649 case MAKE_FUNCTION:
2650 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002651 x = PyFunction_New(v, f->f_globals);
2652 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002653 /* XXX Maybe this should be a separate opcode? */
2654 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002655 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002656 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002657 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002658 x = NULL;
2659 break;
2660 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002661 while (--oparg >= 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002662 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002663 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002664 }
2665 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002666 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002667 }
2668 PUSH(x);
2669 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002670
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002671 case MAKE_CLOSURE:
2672 {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002673 v = POP(); /* code object */
2674 x = PyFunction_New(v, f->f_globals);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002675 Py_DECREF(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002676 if (x != NULL) {
2677 v = POP();
Jeffrey Yasskin2d873bd2008-12-08 18:55:24 +00002678 if (PyFunction_SetClosure(x, v) != 0) {
2679 /* Can't happen unless bytecode is corrupt. */
2680 why = WHY_EXCEPTION;
2681 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002682 Py_DECREF(v);
2683 }
2684 if (x != NULL && oparg > 0) {
2685 v = PyTuple_New(oparg);
2686 if (v == NULL) {
2687 Py_DECREF(x);
2688 x = NULL;
2689 break;
2690 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002691 while (--oparg >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002692 w = POP();
2693 PyTuple_SET_ITEM(v, oparg, w);
2694 }
Jeffrey Yasskin2d873bd2008-12-08 18:55:24 +00002695 if (PyFunction_SetDefaults(x, v) != 0) {
2696 /* Can't happen unless
2697 PyFunction_SetDefaults changes. */
2698 why = WHY_EXCEPTION;
2699 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002700 Py_DECREF(v);
2701 }
2702 PUSH(x);
2703 break;
2704 }
2705
Guido van Rossum8861b741996-07-30 16:49:37 +00002706 case BUILD_SLICE:
2707 if (oparg == 3)
2708 w = POP();
2709 else
2710 w = NULL;
2711 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002712 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002713 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002714 Py_DECREF(u);
2715 Py_DECREF(v);
2716 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002717 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002718 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002719 break;
2720
Fred Drakeef8ace32000-08-24 00:32:09 +00002721 case EXTENDED_ARG:
2722 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002723 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002724 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002725
Guido van Rossum374a9221991-04-04 10:40:29 +00002726 default:
2727 fprintf(stderr,
2728 "XXX lineno: %d, opcode: %d\n",
Jeffrey Yasskinf7f858d2009-05-08 22:23:21 +00002729 PyFrame_GetLineNumber(f),
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002730 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002731 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002732 why = WHY_EXCEPTION;
2733 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002734
2735#ifdef CASE_TOO_BIG
2736 }
2737#endif
2738
Guido van Rossum374a9221991-04-04 10:40:29 +00002739 } /* switch */
2740
2741 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002742
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002743 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002744
Guido van Rossum374a9221991-04-04 10:40:29 +00002745 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002746
Guido van Rossum374a9221991-04-04 10:40:29 +00002747 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002748 if (err == 0 && x != NULL) {
2749#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002750 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002751 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002752 fprintf(stderr,
2753 "XXX undetected error\n");
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002754 else {
2755#endif
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002756 READ_TIMESTAMP(loop1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002757 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002758#ifdef CHECKEXC
2759 }
2760#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +00002761 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002762 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002763 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002764 err = 0;
2765 }
2766
Guido van Rossum374a9221991-04-04 10:40:29 +00002767 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002768
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002769 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002770 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002771 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002772 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002773 why = WHY_EXCEPTION;
2774 }
2775 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002776#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002777 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002778 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002779 if (PyErr_Occurred()) {
Neal Norwitz8250fbe2008-01-27 17:12:15 +00002780 char buf[128];
Jeremy Hylton904ed862003-11-05 17:29:35 +00002781 sprintf(buf, "Stack unwind with exception "
2782 "set and why=%d", why);
2783 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002784 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002785 }
2786#endif
2787
2788 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002789
Guido van Rossum374a9221991-04-04 10:40:29 +00002790 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002791 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002792
Fred Drake8f51f542001-10-04 14:48:42 +00002793 if (tstate->c_tracefunc != NULL)
2794 call_exc_trace(tstate->c_tracefunc,
2795 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002796 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002797
Guido van Rossum374a9221991-04-04 10:40:29 +00002798 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002799
Guido van Rossum374a9221991-04-04 10:40:29 +00002800 if (why == WHY_RERAISE)
2801 why = WHY_EXCEPTION;
2802
2803 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002804
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002805fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002806 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002807 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002808
Tim Peters8a5c3c72004-04-05 19:36:21 +00002809 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002810 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2811 /* For a continue inside a try block,
2812 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002813 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2814 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002815 why = WHY_NOT;
2816 JUMPTO(PyInt_AS_LONG(retval));
2817 Py_DECREF(retval);
2818 break;
2819 }
2820
Guido van Rossum374a9221991-04-04 10:40:29 +00002821 while (STACK_LEVEL() > b->b_level) {
2822 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002823 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002824 }
2825 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2826 why = WHY_NOT;
2827 JUMPTO(b->b_handler);
2828 break;
2829 }
2830 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002831 (b->b_type == SETUP_EXCEPT &&
2832 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002833 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002834 PyObject *exc, *val, *tb;
2835 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002836 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002837 val = Py_None;
2838 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002839 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002840 /* Make the raw exception data
2841 available to the handler,
2842 so a program can emulate the
2843 Python main loop. Don't do
2844 this for 'finally'. */
2845 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002846 PyErr_NormalizeException(
2847 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002848 set_exc_info(tstate,
2849 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002850 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002851 if (tb == NULL) {
2852 Py_INCREF(Py_None);
2853 PUSH(Py_None);
2854 } else
2855 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002856 PUSH(val);
2857 PUSH(exc);
2858 }
2859 else {
Raymond Hettinger06032cb2004-04-06 09:37:35 +00002860 if (why & (WHY_RETURN | WHY_CONTINUE))
Guido van Rossum374a9221991-04-04 10:40:29 +00002861 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002862 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002863 PUSH(v);
2864 }
2865 why = WHY_NOT;
2866 JUMPTO(b->b_handler);
2867 break;
2868 }
2869 } /* unwind stack */
2870
2871 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002872
Guido van Rossum374a9221991-04-04 10:40:29 +00002873 if (why != WHY_NOT)
2874 break;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002875 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002876
Guido van Rossum374a9221991-04-04 10:40:29 +00002877 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002878
Tim Peters8a5c3c72004-04-05 19:36:21 +00002879 assert(why != WHY_YIELD);
2880 /* Pop remaining stack entries. */
2881 while (!EMPTY()) {
2882 v = POP();
2883 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002884 }
2885
Tim Peters8a5c3c72004-04-05 19:36:21 +00002886 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002887 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002888
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002889fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002890 if (tstate->use_tracing) {
Barry Warsawe2eca0b2005-08-15 18:14:19 +00002891 if (tstate->c_tracefunc) {
2892 if (why == WHY_RETURN || why == WHY_YIELD) {
2893 if (call_trace(tstate->c_tracefunc,
2894 tstate->c_traceobj, f,
2895 PyTrace_RETURN, retval)) {
2896 Py_XDECREF(retval);
2897 retval = NULL;
2898 why = WHY_EXCEPTION;
2899 }
2900 }
2901 else if (why == WHY_EXCEPTION) {
2902 call_trace_protected(tstate->c_tracefunc,
2903 tstate->c_traceobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002904 PyTrace_RETURN, NULL);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002905 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002906 }
Fred Drake8f51f542001-10-04 14:48:42 +00002907 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002908 if (why == WHY_EXCEPTION)
2909 call_trace_protected(tstate->c_profilefunc,
2910 tstate->c_profileobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002911 PyTrace_RETURN, NULL);
Fred Drake4ec5d562001-10-04 19:26:43 +00002912 else if (call_trace(tstate->c_profilefunc,
2913 tstate->c_profileobj, f,
2914 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002915 Py_XDECREF(retval);
2916 retval = NULL;
2917 why = WHY_EXCEPTION;
2918 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002919 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002920 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002921
Tim Peters7df5e7f2006-05-26 23:14:37 +00002922 if (tstate->frame->f_exc_type != NULL)
2923 reset_exc_info(tstate);
2924 else {
2925 assert(tstate->frame->f_exc_value == NULL);
2926 assert(tstate->frame->f_exc_traceback == NULL);
2927 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00002928
Tim Peters5ca576e2001-06-18 22:08:13 +00002929 /* pop frame */
Thomas Woutersae406c62007-09-19 17:27:43 +00002930exit_eval_frame:
Armin Rigo2b3eb402003-10-28 12:05:48 +00002931 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002932 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002933
Guido van Rossum96a42c81992-01-12 02:29:51 +00002934 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002935}
2936
Guido van Rossumc2e20742006-02-27 22:32:47 +00002937/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002938 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00002939 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002940
Tim Peters6d6c1a32001-08-02 04:15:00 +00002941PyObject *
2942PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002943 PyObject **args, int argcount, PyObject **kws, int kwcount,
2944 PyObject **defs, int defcount, PyObject *closure)
2945{
2946 register PyFrameObject *f;
2947 register PyObject *retval = NULL;
2948 register PyObject **fastlocals, **freevars;
2949 PyThreadState *tstate = PyThreadState_GET();
2950 PyObject *x, *u;
2951
2952 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00002953 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002954 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002955 return NULL;
2956 }
2957
Neal Norwitzdf6a6492006-08-13 18:10:10 +00002958 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00002959 assert(globals != NULL);
2960 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002961 if (f == NULL)
2962 return NULL;
2963
2964 fastlocals = f->f_localsplus;
Richard Jonescebbefc2006-05-23 18:28:17 +00002965 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00002966
2967 if (co->co_argcount > 0 ||
2968 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2969 int i;
2970 int n = argcount;
2971 PyObject *kwdict = NULL;
2972 if (co->co_flags & CO_VARKEYWORDS) {
2973 kwdict = PyDict_New();
2974 if (kwdict == NULL)
2975 goto fail;
2976 i = co->co_argcount;
2977 if (co->co_flags & CO_VARARGS)
2978 i++;
2979 SETLOCAL(i, kwdict);
2980 }
2981 if (argcount > co->co_argcount) {
2982 if (!(co->co_flags & CO_VARARGS)) {
2983 PyErr_Format(PyExc_TypeError,
2984 "%.200s() takes %s %d "
2985 "%sargument%s (%d given)",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002986 PyString_AsString(co->co_name),
Tim Peters5ca576e2001-06-18 22:08:13 +00002987 defcount ? "at most" : "exactly",
2988 co->co_argcount,
2989 kwcount ? "non-keyword " : "",
2990 co->co_argcount == 1 ? "" : "s",
2991 argcount);
2992 goto fail;
2993 }
2994 n = co->co_argcount;
2995 }
2996 for (i = 0; i < n; i++) {
2997 x = args[i];
2998 Py_INCREF(x);
2999 SETLOCAL(i, x);
3000 }
3001 if (co->co_flags & CO_VARARGS) {
3002 u = PyTuple_New(argcount - n);
3003 if (u == NULL)
3004 goto fail;
3005 SETLOCAL(co->co_argcount, u);
3006 for (i = n; i < argcount; i++) {
3007 x = args[i];
3008 Py_INCREF(x);
3009 PyTuple_SET_ITEM(u, i-n, x);
3010 }
3011 }
3012 for (i = 0; i < kwcount; i++) {
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00003013 PyObject **co_varnames;
Tim Peters5ca576e2001-06-18 22:08:13 +00003014 PyObject *keyword = kws[2*i];
3015 PyObject *value = kws[2*i + 1];
3016 int j;
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003017 if (keyword == NULL || !(PyString_Check(keyword)
3018#ifdef Py_USING_UNICODE
3019 || PyUnicode_Check(keyword)
3020#endif
3021 )) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003022 PyErr_Format(PyExc_TypeError,
3023 "%.200s() keywords must be strings",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003024 PyString_AsString(co->co_name));
Tim Peters5ca576e2001-06-18 22:08:13 +00003025 goto fail;
3026 }
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00003027 /* Speed hack: do raw pointer compares. As names are
3028 normally interned this should almost always hit. */
3029 co_varnames = PySequence_Fast_ITEMS(co->co_varnames);
Tim Peters5ca576e2001-06-18 22:08:13 +00003030 for (j = 0; j < co->co_argcount; j++) {
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00003031 PyObject *nm = co_varnames[j];
3032 if (nm == keyword)
3033 goto kw_found;
3034 }
3035 /* Slow fallback, just in case */
3036 for (j = 0; j < co->co_argcount; j++) {
3037 PyObject *nm = co_varnames[j];
Tim Peters5ca576e2001-06-18 22:08:13 +00003038 int cmp = PyObject_RichCompareBool(
3039 keyword, nm, Py_EQ);
3040 if (cmp > 0)
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00003041 goto kw_found;
Tim Peters5ca576e2001-06-18 22:08:13 +00003042 else if (cmp < 0)
3043 goto fail;
3044 }
3045 /* Check errors from Compare */
3046 if (PyErr_Occurred())
3047 goto fail;
3048 if (j >= co->co_argcount) {
3049 if (kwdict == NULL) {
Benjamin Petersone18ef192009-01-20 14:21:16 +00003050 PyObject *kwd_str = kwd_as_string(keyword);
3051 if (kwd_str) {
3052 PyErr_Format(PyExc_TypeError,
3053 "%.200s() got an unexpected "
3054 "keyword argument '%.400s'",
3055 PyString_AsString(co->co_name),
3056 PyString_AsString(kwd_str));
3057 Py_DECREF(kwd_str);
3058 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003059 goto fail;
3060 }
3061 PyDict_SetItem(kwdict, keyword, value);
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00003062 continue;
Tim Peters5ca576e2001-06-18 22:08:13 +00003063 }
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00003064kw_found:
3065 if (GETLOCAL(j) != NULL) {
Benjamin Petersone18ef192009-01-20 14:21:16 +00003066 PyObject *kwd_str = kwd_as_string(keyword);
3067 if (kwd_str) {
3068 PyErr_Format(PyExc_TypeError,
3069 "%.200s() got multiple "
3070 "values for keyword "
3071 "argument '%.400s'",
3072 PyString_AsString(co->co_name),
3073 PyString_AsString(kwd_str));
3074 Py_DECREF(kwd_str);
3075 }
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00003076 goto fail;
Tim Peters5ca576e2001-06-18 22:08:13 +00003077 }
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00003078 Py_INCREF(value);
3079 SETLOCAL(j, value);
Tim Peters5ca576e2001-06-18 22:08:13 +00003080 }
3081 if (argcount < co->co_argcount) {
3082 int m = co->co_argcount - defcount;
3083 for (i = argcount; i < m; i++) {
3084 if (GETLOCAL(i) == NULL) {
3085 PyErr_Format(PyExc_TypeError,
3086 "%.200s() takes %s %d "
3087 "%sargument%s (%d given)",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003088 PyString_AsString(co->co_name),
Tim Peters5ca576e2001-06-18 22:08:13 +00003089 ((co->co_flags & CO_VARARGS) ||
3090 defcount) ? "at least"
3091 : "exactly",
3092 m, kwcount ? "non-keyword " : "",
3093 m == 1 ? "" : "s", i);
3094 goto fail;
3095 }
3096 }
3097 if (n > m)
3098 i = n - m;
3099 else
3100 i = 0;
3101 for (; i < defcount; i++) {
3102 if (GETLOCAL(m+i) == NULL) {
3103 PyObject *def = defs[i];
3104 Py_INCREF(def);
3105 SETLOCAL(m+i, def);
3106 }
3107 }
3108 }
3109 }
3110 else {
3111 if (argcount > 0 || kwcount > 0) {
3112 PyErr_Format(PyExc_TypeError,
3113 "%.200s() takes no arguments (%d given)",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003114 PyString_AsString(co->co_name),
Tim Peters5ca576e2001-06-18 22:08:13 +00003115 argcount + kwcount);
3116 goto fail;
3117 }
3118 }
3119 /* Allocate and initialize storage for cell vars, and copy free
3120 vars into frame. This isn't too efficient right now. */
Richard Jonescebbefc2006-05-23 18:28:17 +00003121 if (PyTuple_GET_SIZE(co->co_cellvars)) {
Neal Norwitz245ce8d2006-06-12 02:16:10 +00003122 int i, j, nargs, found;
Tim Peters5ca576e2001-06-18 22:08:13 +00003123 char *cellname, *argname;
3124 PyObject *c;
3125
3126 nargs = co->co_argcount;
3127 if (co->co_flags & CO_VARARGS)
3128 nargs++;
3129 if (co->co_flags & CO_VARKEYWORDS)
3130 nargs++;
3131
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003132 /* Initialize each cell var, taking into account
3133 cell vars that are initialized from arguments.
3134
3135 Should arrange for the compiler to put cellvars
3136 that are arguments at the beginning of the cellvars
3137 list so that we can march over it more efficiently?
3138 */
Richard Jonescebbefc2006-05-23 18:28:17 +00003139 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003140 cellname = PyString_AS_STRING(
Tim Peters5ca576e2001-06-18 22:08:13 +00003141 PyTuple_GET_ITEM(co->co_cellvars, i));
3142 found = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003143 for (j = 0; j < nargs; j++) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003144 argname = PyString_AS_STRING(
Tim Peters5ca576e2001-06-18 22:08:13 +00003145 PyTuple_GET_ITEM(co->co_varnames, j));
3146 if (strcmp(cellname, argname) == 0) {
3147 c = PyCell_New(GETLOCAL(j));
3148 if (c == NULL)
3149 goto fail;
Richard Jonescebbefc2006-05-23 18:28:17 +00003150 GETLOCAL(co->co_nlocals + i) = c;
Tim Peters5ca576e2001-06-18 22:08:13 +00003151 found = 1;
3152 break;
3153 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003154 }
3155 if (found == 0) {
3156 c = PyCell_New(NULL);
3157 if (c == NULL)
3158 goto fail;
Richard Jonescebbefc2006-05-23 18:28:17 +00003159 SETLOCAL(co->co_nlocals + i, c);
Tim Peters5ca576e2001-06-18 22:08:13 +00003160 }
3161 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003162 }
Richard Jonescebbefc2006-05-23 18:28:17 +00003163 if (PyTuple_GET_SIZE(co->co_freevars)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003164 int i;
Richard Jonescebbefc2006-05-23 18:28:17 +00003165 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003166 PyObject *o = PyTuple_GET_ITEM(closure, i);
3167 Py_INCREF(o);
Richard Jonescebbefc2006-05-23 18:28:17 +00003168 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Tim Peters5ca576e2001-06-18 22:08:13 +00003169 }
3170 }
3171
Tim Peters5ca576e2001-06-18 22:08:13 +00003172 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003173 /* Don't need to keep the reference to f_back, it will be set
3174 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00003175 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003176 f->f_back = NULL;
3177
Jeremy Hylton985eba52003-02-05 23:13:00 +00003178 PCALL(PCALL_GENERATOR);
3179
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003180 /* Create a new generator that owns the ready to run frame
3181 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00003182 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00003183 }
3184
Thomas Woutersae406c62007-09-19 17:27:43 +00003185 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003186
Thomas Woutersae406c62007-09-19 17:27:43 +00003187fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003188
Tim Petersb13680b2001-11-27 23:29:29 +00003189 /* decref'ing the frame can cause __del__ methods to get invoked,
3190 which can call back into Python. While we're done with the
3191 current Python frame (f), the associated C stack is still in use,
3192 so recursion_depth must be boosted for the duration.
3193 */
3194 assert(tstate != NULL);
3195 ++tstate->recursion_depth;
Thomas Woutersae406c62007-09-19 17:27:43 +00003196 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00003197 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00003198 return retval;
3199}
3200
3201
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00003202static PyObject *
3203special_lookup(PyObject *o, char *meth, PyObject **cache)
3204{
3205 PyObject *res;
3206 if (PyInstance_Check(o)) {
3207 if (!*cache)
3208 return PyObject_GetAttrString(o, meth);
3209 else
3210 return PyObject_GetAttr(o, *cache);
3211 }
3212 res = _PyObject_LookupSpecial(o, meth, cache);
3213 if (res == NULL && !PyErr_Occurred()) {
3214 PyErr_SetObject(PyExc_AttributeError, *cache);
3215 return NULL;
3216 }
3217 return res;
3218}
3219
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003220
Benjamin Petersone18ef192009-01-20 14:21:16 +00003221static PyObject *
3222kwd_as_string(PyObject *kwd) {
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003223#ifdef Py_USING_UNICODE
Benjamin Petersone18ef192009-01-20 14:21:16 +00003224 if (PyString_Check(kwd)) {
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003225#else
3226 assert(PyString_Check(kwd));
3227#endif
Benjamin Petersone18ef192009-01-20 14:21:16 +00003228 Py_INCREF(kwd);
3229 return kwd;
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003230#ifdef Py_USING_UNICODE
Benjamin Petersone18ef192009-01-20 14:21:16 +00003231 }
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003232 return _PyUnicode_AsDefaultEncodedString(kwd, "replace");
3233#endif
Benjamin Petersone18ef192009-01-20 14:21:16 +00003234}
3235
3236
Guido van Rossumc9fbb722003-03-01 03:36:33 +00003237/* Implementation notes for set_exc_info() and reset_exc_info():
3238
3239- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
3240 'exc_traceback'. These always travel together.
3241
3242- tstate->curexc_ZZZ is the "hot" exception that is set by
3243 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
3244
3245- Once an exception is caught by an except clause, it is transferred
3246 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
3247 can pick it up. This is the primary task of set_exc_info().
Tim Peters7df5e7f2006-05-26 23:14:37 +00003248 XXX That can't be right: set_exc_info() doesn't look at tstate->curexc_ZZZ.
Guido van Rossumc9fbb722003-03-01 03:36:33 +00003249
3250- Now let me explain the complicated dance with frame->f_exc_ZZZ.
3251
3252 Long ago, when none of this existed, there were just a few globals:
3253 one set corresponding to the "hot" exception, and one set
3254 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
3255 globals; they were simply stored as sys.exc_ZZZ. For backwards
3256 compatibility, they still are!) The problem was that in code like
3257 this:
3258
3259 try:
3260 "something that may fail"
3261 except "some exception":
3262 "do something else first"
3263 "print the exception from sys.exc_ZZZ."
3264
3265 if "do something else first" invoked something that raised and caught
3266 an exception, sys.exc_ZZZ were overwritten. That was a frequent
3267 cause of subtle bugs. I fixed this by changing the semantics as
3268 follows:
3269
3270 - Within one frame, sys.exc_ZZZ will hold the last exception caught
3271 *in that frame*.
3272
3273 - But initially, and as long as no exception is caught in a given
3274 frame, sys.exc_ZZZ will hold the last exception caught in the
3275 previous frame (or the frame before that, etc.).
3276
3277 The first bullet fixed the bug in the above example. The second
3278 bullet was for backwards compatibility: it was (and is) common to
3279 have a function that is called when an exception is caught, and to
3280 have that function access the caught exception via sys.exc_ZZZ.
3281 (Example: traceback.print_exc()).
3282
3283 At the same time I fixed the problem that sys.exc_ZZZ weren't
3284 thread-safe, by introducing sys.exc_info() which gets it from tstate;
3285 but that's really a separate improvement.
3286
3287 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
3288 variables to what they were before the current frame was called. The
3289 set_exc_info() function saves them on the frame so that
3290 reset_exc_info() can restore them. The invariant is that
3291 frame->f_exc_ZZZ is NULL iff the current frame never caught an
3292 exception (where "catching" an exception applies only to successful
3293 except clauses); and if the current frame ever caught an exception,
3294 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
3295 at the start of the current frame.
3296
3297*/
3298
Fredrik Lundh7a830892006-05-27 10:39:48 +00003299static void
Guido van Rossumac7be682001-01-17 15:42:30 +00003300set_exc_info(PyThreadState *tstate,
3301 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00003302{
Tim Peters7df5e7f2006-05-26 23:14:37 +00003303 PyFrameObject *frame = tstate->frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003304 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00003305
Tim Peters7df5e7f2006-05-26 23:14:37 +00003306 assert(type != NULL);
3307 assert(frame != NULL);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003308 if (frame->f_exc_type == NULL) {
Tim Peters7df5e7f2006-05-26 23:14:37 +00003309 assert(frame->f_exc_value == NULL);
3310 assert(frame->f_exc_traceback == NULL);
3311 /* This frame didn't catch an exception before. */
3312 /* Save previous exception of this thread in this frame. */
Guido van Rossuma027efa1997-05-05 20:56:21 +00003313 if (tstate->exc_type == NULL) {
Tim Peters7df5e7f2006-05-26 23:14:37 +00003314 /* XXX Why is this set to Py_None? */
Guido van Rossuma027efa1997-05-05 20:56:21 +00003315 Py_INCREF(Py_None);
3316 tstate->exc_type = Py_None;
3317 }
Tim Peters7df5e7f2006-05-26 23:14:37 +00003318 Py_INCREF(tstate->exc_type);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003319 Py_XINCREF(tstate->exc_value);
3320 Py_XINCREF(tstate->exc_traceback);
3321 frame->f_exc_type = tstate->exc_type;
3322 frame->f_exc_value = tstate->exc_value;
3323 frame->f_exc_traceback = tstate->exc_traceback;
3324 }
Tim Peters7df5e7f2006-05-26 23:14:37 +00003325 /* Set new exception for this thread. */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003326 tmp_type = tstate->exc_type;
3327 tmp_value = tstate->exc_value;
3328 tmp_tb = tstate->exc_traceback;
Tim Peters7df5e7f2006-05-26 23:14:37 +00003329 Py_INCREF(type);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003330 Py_XINCREF(value);
3331 Py_XINCREF(tb);
3332 tstate->exc_type = type;
3333 tstate->exc_value = value;
3334 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003335 Py_XDECREF(tmp_type);
3336 Py_XDECREF(tmp_value);
3337 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003338 /* For b/w compatibility */
3339 PySys_SetObject("exc_type", type);
3340 PySys_SetObject("exc_value", value);
3341 PySys_SetObject("exc_traceback", tb);
3342}
3343
Fredrik Lundh7a830892006-05-27 10:39:48 +00003344static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003345reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00003346{
3347 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003348 PyObject *tmp_type, *tmp_value, *tmp_tb;
Tim Peters7df5e7f2006-05-26 23:14:37 +00003349
3350 /* It's a precondition that the thread state's frame caught an
3351 * exception -- verify in a debug build.
3352 */
3353 assert(tstate != NULL);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003354 frame = tstate->frame;
Tim Peters7df5e7f2006-05-26 23:14:37 +00003355 assert(frame != NULL);
3356 assert(frame->f_exc_type != NULL);
3357
3358 /* Copy the frame's exception info back to the thread state. */
3359 tmp_type = tstate->exc_type;
3360 tmp_value = tstate->exc_value;
3361 tmp_tb = tstate->exc_traceback;
3362 Py_INCREF(frame->f_exc_type);
3363 Py_XINCREF(frame->f_exc_value);
3364 Py_XINCREF(frame->f_exc_traceback);
3365 tstate->exc_type = frame->f_exc_type;
3366 tstate->exc_value = frame->f_exc_value;
3367 tstate->exc_traceback = frame->f_exc_traceback;
3368 Py_XDECREF(tmp_type);
3369 Py_XDECREF(tmp_value);
3370 Py_XDECREF(tmp_tb);
3371
3372 /* For b/w compatibility */
3373 PySys_SetObject("exc_type", frame->f_exc_type);
3374 PySys_SetObject("exc_value", frame->f_exc_value);
3375 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
3376
3377 /* Clear the frame's exception info. */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003378 tmp_type = frame->f_exc_type;
3379 tmp_value = frame->f_exc_value;
3380 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003381 frame->f_exc_type = NULL;
3382 frame->f_exc_value = NULL;
3383 frame->f_exc_traceback = NULL;
Tim Peters7df5e7f2006-05-26 23:14:37 +00003384 Py_DECREF(tmp_type);
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003385 Py_XDECREF(tmp_value);
3386 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003387}
3388
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003389/* Logic for the raise statement (too complicated for inlining).
3390 This *consumes* a reference count to each of its arguments. */
Fredrik Lundh7a830892006-05-27 10:39:48 +00003391static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003392do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003393{
Guido van Rossumd295f121998-04-09 21:39:57 +00003394 if (type == NULL) {
3395 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003396 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumd295f121998-04-09 21:39:57 +00003397 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
3398 value = tstate->exc_value;
3399 tb = tstate->exc_traceback;
3400 Py_XINCREF(type);
3401 Py_XINCREF(value);
3402 Py_XINCREF(tb);
3403 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003404
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003405 /* We support the following forms of raise:
3406 raise <class>, <classinstance>
3407 raise <class>, <argument tuple>
3408 raise <class>, None
3409 raise <class>, <argument>
3410 raise <classinstance>, None
3411 raise <string>, <object>
3412 raise <string>, None
3413
3414 An omitted second argument is the same as None.
3415
3416 In addition, raise <tuple>, <anything> is the same as
3417 raising the tuple's first item (and it better have one!);
3418 this rule is applied recursively.
3419
3420 Finally, an optional third argument can be supplied, which
3421 gives the traceback to be substituted (useful when
3422 re-raising an exception after examining it). */
3423
3424 /* First, check the traceback argument, replacing None with
3425 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00003426 if (tb == Py_None) {
3427 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003428 tb = NULL;
3429 }
3430 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003431 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003432 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003433 goto raise_error;
3434 }
3435
3436 /* Next, replace a missing value with None */
3437 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003438 value = Py_None;
3439 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003440 }
3441
3442 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00003443 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
3444 PyObject *tmp = type;
3445 type = PyTuple_GET_ITEM(type, 0);
3446 Py_INCREF(type);
3447 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003448 }
3449
Brett Cannon129bd522007-01-30 21:34:36 +00003450 if (PyExceptionClass_Check(type))
Barry Warsaw4249f541997-08-22 21:26:19 +00003451 PyErr_NormalizeException(&type, &value, &tb);
3452
Brett Cannonbf364092006-03-01 04:25:17 +00003453 else if (PyExceptionInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003454 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00003455 if (value != Py_None) {
3456 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003457 "instance exception may not have a separate value");
3458 goto raise_error;
3459 }
3460 else {
3461 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00003462 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003463 value = type;
Brett Cannonbf364092006-03-01 04:25:17 +00003464 type = PyExceptionInstance_Class(type);
Guido van Rossumb209a111997-04-29 18:18:01 +00003465 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003466 }
3467 }
3468 else {
3469 /* Not something you can raise. You get an exception
3470 anyway, just not what you specified :-) */
Jeremy Hylton960d9482001-04-27 02:25:33 +00003471 PyErr_Format(PyExc_TypeError,
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00003472 "exceptions must be classes or instances, not %s",
3473 type->ob_type->tp_name);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003474 goto raise_error;
3475 }
Guido van Rossum504153d2008-03-18 04:26:48 +00003476
3477 assert(PyExceptionClass_Check(type));
3478 if (Py_Py3kWarningFlag && PyClass_Check(type)) {
Benjamin Peterson9f4f4812008-04-27 03:01:45 +00003479 if (PyErr_WarnEx(PyExc_DeprecationWarning,
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00003480 "exceptions must derive from BaseException "
3481 "in 3.x", 1) < 0)
Guido van Rossum504153d2008-03-18 04:26:48 +00003482 goto raise_error;
3483 }
3484
Guido van Rossumb209a111997-04-29 18:18:01 +00003485 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003486 if (tb == NULL)
3487 return WHY_EXCEPTION;
3488 else
3489 return WHY_RERAISE;
3490 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00003491 Py_XDECREF(value);
3492 Py_XDECREF(type);
3493 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003494 return WHY_EXCEPTION;
3495}
3496
Tim Petersd6d010b2001-06-21 02:49:55 +00003497/* Iterate v argcnt times and store the results on the stack (via decreasing
3498 sp). Return 1 for success, 0 if error. */
3499
Fredrik Lundh7a830892006-05-27 10:39:48 +00003500static int
Tim Petersd6d010b2001-06-21 02:49:55 +00003501unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003502{
Tim Petersd6d010b2001-06-21 02:49:55 +00003503 int i = 0;
3504 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00003505 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00003506
Tim Petersd6d010b2001-06-21 02:49:55 +00003507 assert(v != NULL);
3508
3509 it = PyObject_GetIter(v);
3510 if (it == NULL)
3511 goto Error;
3512
3513 for (; i < argcnt; i++) {
3514 w = PyIter_Next(it);
3515 if (w == NULL) {
3516 /* Iterator done, via error or exhaustion. */
3517 if (!PyErr_Occurred()) {
3518 PyErr_Format(PyExc_ValueError,
3519 "need more than %d value%s to unpack",
3520 i, i == 1 ? "" : "s");
3521 }
3522 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003523 }
3524 *--sp = w;
3525 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003526
3527 /* We better have exhausted the iterator now. */
3528 w = PyIter_Next(it);
3529 if (w == NULL) {
3530 if (PyErr_Occurred())
3531 goto Error;
3532 Py_DECREF(it);
3533 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003534 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00003535 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00003536 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00003537 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00003538Error:
Barry Warsaw91010551997-08-25 22:30:51 +00003539 for (; i > 0; i--, sp++)
3540 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00003541 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00003542 return 0;
3543}
3544
3545
Guido van Rossum96a42c81992-01-12 02:29:51 +00003546#ifdef LLTRACE
Fredrik Lundh7a830892006-05-27 10:39:48 +00003547static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003548prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003549{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003550 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003551 if (PyObject_Print(v, stdout, 0) != 0)
3552 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003553 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003554 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003555}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003556#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003557
Fredrik Lundh7a830892006-05-27 10:39:48 +00003558static void
Fred Drake5755ce62001-06-27 19:19:46 +00003559call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003560{
Guido van Rossumb209a111997-04-29 18:18:01 +00003561 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003562 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003563 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003564 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003565 value = Py_None;
3566 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003567 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003568 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003569 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003570 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003571 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003572 }
Fred Drake5755ce62001-06-27 19:19:46 +00003573 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003574 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003575 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003576 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003577 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003578 Py_XDECREF(type);
3579 Py_XDECREF(value);
3580 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003581 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003582}
3583
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +00003584static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003585call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003586 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003587{
3588 PyObject *type, *value, *traceback;
3589 int err;
3590 PyErr_Fetch(&type, &value, &traceback);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003591 err = call_trace(func, obj, frame, what, arg);
Fred Drake4ec5d562001-10-04 19:26:43 +00003592 if (err == 0)
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +00003593 {
Fred Drake4ec5d562001-10-04 19:26:43 +00003594 PyErr_Restore(type, value, traceback);
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +00003595 return 0;
3596 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003597 else {
3598 Py_XDECREF(type);
3599 Py_XDECREF(value);
3600 Py_XDECREF(traceback);
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +00003601 return -1;
Fred Drake4ec5d562001-10-04 19:26:43 +00003602 }
3603}
3604
Fredrik Lundh7a830892006-05-27 10:39:48 +00003605static int
Fred Drake5755ce62001-06-27 19:19:46 +00003606call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3607 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003608{
Fred Drake5755ce62001-06-27 19:19:46 +00003609 register PyThreadState *tstate = frame->f_tstate;
3610 int result;
3611 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003612 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003613 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003614 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003615 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003616 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3617 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003618 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003619 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003620}
3621
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003622PyObject *
3623_PyEval_CallTracing(PyObject *func, PyObject *args)
3624{
3625 PyFrameObject *frame = PyEval_GetFrame();
3626 PyThreadState *tstate = frame->f_tstate;
3627 int save_tracing = tstate->tracing;
3628 int save_use_tracing = tstate->use_tracing;
3629 PyObject *result;
3630
3631 tstate->tracing = 0;
3632 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3633 || (tstate->c_profilefunc != NULL));
3634 result = PyObject_Call(func, args, NULL);
3635 tstate->tracing = save_tracing;
3636 tstate->use_tracing = save_use_tracing;
3637 return result;
3638}
3639
Jeffrey Yasskin655d8352009-05-23 23:23:01 +00003640/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Fredrik Lundh7a830892006-05-27 10:39:48 +00003641static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003642maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003643 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3644 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003645{
Michael W. Hudson006c7522002-11-08 13:08:46 +00003646 int result = 0;
Jeffrey Yasskin655d8352009-05-23 23:23:01 +00003647 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003648
Jeremy Hyltona4ebc132006-04-18 14:47:00 +00003649 /* If the last instruction executed isn't in the current
Jeffrey Yasskin655d8352009-05-23 23:23:01 +00003650 instruction window, reset the window.
Jeremy Hyltona4ebc132006-04-18 14:47:00 +00003651 */
Jeffrey Yasskin655d8352009-05-23 23:23:01 +00003652 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
Thomas Woutersae406c62007-09-19 17:27:43 +00003653 PyAddrPair bounds;
Jeffrey Yasskin655d8352009-05-23 23:23:01 +00003654 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3655 &bounds);
Thomas Woutersae406c62007-09-19 17:27:43 +00003656 *instr_lb = bounds.ap_lower;
3657 *instr_ub = bounds.ap_upper;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003658 }
Jeffrey Yasskin655d8352009-05-23 23:23:01 +00003659 /* If the last instruction falls at the start of a line or if
3660 it represents a jump backwards, update the frame's line
3661 number and call the trace function. */
3662 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
3663 frame->f_lineno = line;
Jeremy Hyltona4ebc132006-04-18 14:47:00 +00003664 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
Armin Rigobf57a142004-03-22 19:24:58 +00003665 }
3666 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003667 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003668}
3669
Fred Drake5755ce62001-06-27 19:19:46 +00003670void
3671PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003672{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003673 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003674 PyObject *temp = tstate->c_profileobj;
3675 Py_XINCREF(arg);
3676 tstate->c_profilefunc = NULL;
3677 tstate->c_profileobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003678 /* Must make sure that tracing is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003679 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003680 Py_XDECREF(temp);
3681 tstate->c_profilefunc = func;
3682 tstate->c_profileobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003683 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003684 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003685}
3686
3687void
3688PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3689{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003690 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003691 PyObject *temp = tstate->c_traceobj;
Jeffrey Yasskinfd8a1ec2008-12-03 06:46:45 +00003692 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003693 Py_XINCREF(arg);
3694 tstate->c_tracefunc = NULL;
3695 tstate->c_traceobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003696 /* Must make sure that profiling is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003697 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003698 Py_XDECREF(temp);
3699 tstate->c_tracefunc = func;
3700 tstate->c_traceobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003701 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003702 tstate->use_tracing = ((func != NULL)
3703 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003704}
3705
Guido van Rossumb209a111997-04-29 18:18:01 +00003706PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003707PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003708{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003709 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003710 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003711 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003712 else
3713 return current_frame->f_builtins;
3714}
3715
Guido van Rossumb209a111997-04-29 18:18:01 +00003716PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003717PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003718{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003719 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003720 if (current_frame == NULL)
3721 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003722 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003723 return current_frame->f_locals;
3724}
3725
Guido van Rossumb209a111997-04-29 18:18:01 +00003726PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003727PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003728{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003729 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003730 if (current_frame == NULL)
3731 return NULL;
3732 else
3733 return current_frame->f_globals;
3734}
3735
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003736PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003737PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003738{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003739 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003740 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003741}
3742
Guido van Rossum6135a871995-01-09 17:53:26 +00003743int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003744PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003745{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003746 PyFrameObject *current_frame = PyEval_GetFrame();
Neal Norwitzb9845e72006-06-12 02:11:18 +00003747 return current_frame == NULL ? 0 : PyFrame_IsRestricted(current_frame);
Guido van Rossum6135a871995-01-09 17:53:26 +00003748}
3749
Guido van Rossumbe270261997-05-22 22:26:18 +00003750int
Tim Peters5ba58662001-07-16 02:29:45 +00003751PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003752{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003753 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003754 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003755
3756 if (current_frame != NULL) {
3757 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003758 const int compilerflags = codeflags & PyCF_MASK;
3759 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003760 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003761 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003762 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003763#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003764 if (codeflags & CO_GENERATOR_ALLOWED) {
3765 result = 1;
3766 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3767 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003768#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003769 }
3770 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003771}
3772
3773int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003774Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003775{
Guido van Rossumb209a111997-04-29 18:18:01 +00003776 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003777 if (f == NULL)
3778 return 0;
3779 if (!PyFile_SoftSpace(f, 0))
3780 return 0;
3781 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003782}
3783
Guido van Rossum3f5da241990-12-20 15:06:42 +00003784
Guido van Rossum681d79a1995-07-18 14:51:37 +00003785/* External interface to call any callable object.
3786 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003787
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003788#undef PyEval_CallObject
3789/* for backward compatibility: export this interface */
3790
Guido van Rossumb209a111997-04-29 18:18:01 +00003791PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003792PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003793{
Guido van Rossumb209a111997-04-29 18:18:01 +00003794 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003795}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003796#define PyEval_CallObject(func,arg) \
3797 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003798
Guido van Rossumb209a111997-04-29 18:18:01 +00003799PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003800PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003801{
Jeremy Hylton52820442001-01-03 23:52:36 +00003802 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003803
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003804 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003805 arg = PyTuple_New(0);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003806 if (arg == NULL)
3807 return NULL;
3808 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003809 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003810 PyErr_SetString(PyExc_TypeError,
3811 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003812 return NULL;
3813 }
3814 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003815 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003816
Guido van Rossumb209a111997-04-29 18:18:01 +00003817 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003818 PyErr_SetString(PyExc_TypeError,
3819 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003820 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003821 return NULL;
3822 }
3823
Tim Peters6d6c1a32001-08-02 04:15:00 +00003824 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003825 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003826 return result;
3827}
3828
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003829const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003830PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003831{
3832 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003833 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003834 else if (PyFunction_Check(func))
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003835 return PyString_AsString(((PyFunctionObject*)func)->func_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00003836 else if (PyCFunction_Check(func))
3837 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3838 else if (PyClass_Check(func))
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003839 return PyString_AsString(((PyClassObject*)func)->cl_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00003840 else if (PyInstance_Check(func)) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003841 return PyString_AsString(
Jeremy Hylton512a2372001-04-11 13:52:29 +00003842 ((PyInstanceObject*)func)->in_class->cl_name);
3843 } else {
3844 return func->ob_type->tp_name;
3845 }
3846}
3847
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003848const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003849PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003850{
3851 if (PyMethod_Check(func))
3852 return "()";
3853 else if (PyFunction_Check(func))
3854 return "()";
3855 else if (PyCFunction_Check(func))
3856 return "()";
3857 else if (PyClass_Check(func))
3858 return " constructor";
3859 else if (PyInstance_Check(func)) {
3860 return " instance";
3861 } else {
3862 return " object";
3863 }
3864}
3865
Fredrik Lundh7a830892006-05-27 10:39:48 +00003866static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003867err_args(PyObject *func, int flags, int nargs)
3868{
3869 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003870 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003871 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003872 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003873 nargs);
3874 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003875 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003876 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003877 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003878 nargs);
3879}
3880
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003881#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003882if (tstate->use_tracing && tstate->c_profilefunc) { \
3883 if (call_trace(tstate->c_profilefunc, \
3884 tstate->c_profileobj, \
3885 tstate->frame, PyTrace_C_CALL, \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003886 func)) { \
3887 x = NULL; \
3888 } \
3889 else { \
3890 x = call; \
3891 if (tstate->c_profilefunc != NULL) { \
3892 if (x == NULL) { \
3893 call_trace_protected(tstate->c_profilefunc, \
3894 tstate->c_profileobj, \
3895 tstate->frame, PyTrace_C_EXCEPTION, \
3896 func); \
3897 /* XXX should pass (type, value, tb) */ \
3898 } else { \
3899 if (call_trace(tstate->c_profilefunc, \
3900 tstate->c_profileobj, \
3901 tstate->frame, PyTrace_C_RETURN, \
3902 func)) { \
3903 Py_DECREF(x); \
3904 x = NULL; \
3905 } \
3906 } \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003907 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003908 } \
3909} else { \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003910 x = call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003911 }
3912
Fredrik Lundh7a830892006-05-27 10:39:48 +00003913static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003914call_function(PyObject ***pp_stack, int oparg
3915#ifdef WITH_TSC
3916 , uint64* pintr0, uint64* pintr1
3917#endif
3918 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003919{
3920 int na = oparg & 0xff;
3921 int nk = (oparg>>8) & 0xff;
3922 int n = na + 2 * nk;
3923 PyObject **pfunc = (*pp_stack) - n - 1;
3924 PyObject *func = *pfunc;
3925 PyObject *x, *w;
3926
Jeremy Hylton985eba52003-02-05 23:13:00 +00003927 /* Always dispatch PyCFunction first, because these are
3928 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003929 */
3930 if (PyCFunction_Check(func) && nk == 0) {
3931 int flags = PyCFunction_GET_FLAGS(func);
Nicholas Bastind858a772004-06-25 23:31:06 +00003932 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003933
3934 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003935 if (flags & (METH_NOARGS | METH_O)) {
3936 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3937 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003938 if (flags & METH_NOARGS && na == 0) {
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003939 C_TRACE(x, (*meth)(self,NULL));
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003940 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003941 else if (flags & METH_O && na == 1) {
3942 PyObject *arg = EXT_POP(*pp_stack);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003943 C_TRACE(x, (*meth)(self,arg));
Jeremy Hylton192690e2002-08-16 18:36:11 +00003944 Py_DECREF(arg);
3945 }
3946 else {
3947 err_args(func, flags, na);
3948 x = NULL;
3949 }
3950 }
3951 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003952 PyObject *callargs;
3953 callargs = load_args(pp_stack, na);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003954 READ_TIMESTAMP(*pintr0);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003955 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003956 READ_TIMESTAMP(*pintr1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003957 Py_XDECREF(callargs);
3958 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003959 } else {
3960 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3961 /* optimize access to bound methods */
3962 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003963 PCALL(PCALL_METHOD);
3964 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003965 Py_INCREF(self);
3966 func = PyMethod_GET_FUNCTION(func);
3967 Py_INCREF(func);
3968 Py_DECREF(*pfunc);
3969 *pfunc = self;
3970 na++;
3971 n++;
3972 } else
3973 Py_INCREF(func);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003974 READ_TIMESTAMP(*pintr0);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003975 if (PyFunction_Check(func))
3976 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003977 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003978 x = do_call(func, pp_stack, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003979 READ_TIMESTAMP(*pintr1);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003980 Py_DECREF(func);
3981 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003982
Armin Rigod34fa522006-03-28 19:10:40 +00003983 /* Clear the stack of the function object. Also removes
3984 the arguments in case they weren't consumed already
3985 (fast_function() and err_args() leave them on the stack).
Thomas Wouters7f597322006-03-01 05:32:33 +00003986 */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003987 while ((*pp_stack) > pfunc) {
3988 w = EXT_POP(*pp_stack);
3989 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003990 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003991 }
3992 return x;
3993}
3994
Jeremy Hylton192690e2002-08-16 18:36:11 +00003995/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003996 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003997 For the simplest case -- a function that takes only positional
3998 arguments and is called with only positional arguments -- it
3999 inlines the most primitive frame setup code from
4000 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
4001 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00004002*/
4003
Fredrik Lundh7a830892006-05-27 10:39:48 +00004004static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00004005fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00004006{
Jeremy Hylton985eba52003-02-05 23:13:00 +00004007 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00004008 PyObject *globals = PyFunction_GET_GLOBALS(func);
4009 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
4010 PyObject **d = NULL;
4011 int nd = 0;
4012
Jeremy Hylton985eba52003-02-05 23:13:00 +00004013 PCALL(PCALL_FUNCTION);
4014 PCALL(PCALL_FAST_FUNCTION);
Raymond Hettinger40174c32003-05-31 07:04:16 +00004015 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00004016 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
4017 PyFrameObject *f;
4018 PyObject *retval = NULL;
4019 PyThreadState *tstate = PyThreadState_GET();
4020 PyObject **fastlocals, **stack;
4021 int i;
4022
4023 PCALL(PCALL_FASTER_FUNCTION);
4024 assert(globals != NULL);
4025 /* XXX Perhaps we should create a specialized
4026 PyFrame_New() that doesn't take locals, but does
4027 take builtins without sanity checking them.
4028 */
Neal Norwitzdf6a6492006-08-13 18:10:10 +00004029 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004030 f = PyFrame_New(tstate, co, globals, NULL);
4031 if (f == NULL)
4032 return NULL;
4033
4034 fastlocals = f->f_localsplus;
4035 stack = (*pp_stack) - n;
4036
4037 for (i = 0; i < n; i++) {
4038 Py_INCREF(*stack);
4039 fastlocals[i] = *stack++;
4040 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00004041 retval = PyEval_EvalFrameEx(f,0);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004042 ++tstate->recursion_depth;
4043 Py_DECREF(f);
4044 --tstate->recursion_depth;
4045 return retval;
4046 }
Jeremy Hylton52820442001-01-03 23:52:36 +00004047 if (argdefs != NULL) {
4048 d = &PyTuple_GET_ITEM(argdefs, 0);
Christian Heimese93237d2007-12-19 02:37:44 +00004049 nd = Py_SIZE(argdefs);
Jeremy Hylton52820442001-01-03 23:52:36 +00004050 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00004051 return PyEval_EvalCodeEx(co, globals,
4052 (PyObject *)NULL, (*pp_stack)-n, na,
4053 (*pp_stack)-2*nk, nk, d, nd,
4054 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00004055}
4056
Fredrik Lundh7a830892006-05-27 10:39:48 +00004057static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00004058update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
4059 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00004060{
4061 PyObject *kwdict = NULL;
4062 if (orig_kwdict == NULL)
4063 kwdict = PyDict_New();
4064 else {
4065 kwdict = PyDict_Copy(orig_kwdict);
4066 Py_DECREF(orig_kwdict);
4067 }
4068 if (kwdict == NULL)
4069 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00004070 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00004071 int err;
4072 PyObject *value = EXT_POP(*pp_stack);
4073 PyObject *key = EXT_POP(*pp_stack);
4074 if (PyDict_GetItem(kwdict, key) != NULL) {
Thomas Woutersae406c62007-09-19 17:27:43 +00004075 PyErr_Format(PyExc_TypeError,
4076 "%.200s%s got multiple values "
4077 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00004078 PyEval_GetFuncName(func),
4079 PyEval_GetFuncDesc(func),
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004080 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00004081 Py_DECREF(key);
4082 Py_DECREF(value);
4083 Py_DECREF(kwdict);
4084 return NULL;
4085 }
4086 err = PyDict_SetItem(kwdict, key, value);
4087 Py_DECREF(key);
4088 Py_DECREF(value);
4089 if (err) {
4090 Py_DECREF(kwdict);
4091 return NULL;
4092 }
4093 }
4094 return kwdict;
4095}
4096
Fredrik Lundh7a830892006-05-27 10:39:48 +00004097static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00004098update_star_args(int nstack, int nstar, PyObject *stararg,
4099 PyObject ***pp_stack)
4100{
4101 PyObject *callargs, *w;
4102
4103 callargs = PyTuple_New(nstack + nstar);
4104 if (callargs == NULL) {
4105 return NULL;
4106 }
4107 if (nstar) {
4108 int i;
4109 for (i = 0; i < nstar; i++) {
4110 PyObject *a = PyTuple_GET_ITEM(stararg, i);
4111 Py_INCREF(a);
4112 PyTuple_SET_ITEM(callargs, nstack + i, a);
4113 }
4114 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00004115 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00004116 w = EXT_POP(*pp_stack);
4117 PyTuple_SET_ITEM(callargs, nstack, w);
4118 }
4119 return callargs;
4120}
4121
Fredrik Lundh7a830892006-05-27 10:39:48 +00004122static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00004123load_args(PyObject ***pp_stack, int na)
4124{
4125 PyObject *args = PyTuple_New(na);
4126 PyObject *w;
4127
4128 if (args == NULL)
4129 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00004130 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00004131 w = EXT_POP(*pp_stack);
4132 PyTuple_SET_ITEM(args, na, w);
4133 }
4134 return args;
4135}
4136
Fredrik Lundh7a830892006-05-27 10:39:48 +00004137static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00004138do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4139{
4140 PyObject *callargs = NULL;
4141 PyObject *kwdict = NULL;
4142 PyObject *result = NULL;
4143
4144 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00004145 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00004146 if (kwdict == NULL)
4147 goto call_fail;
4148 }
4149 callargs = load_args(pp_stack, na);
4150 if (callargs == NULL)
4151 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004152#ifdef CALL_PROFILE
4153 /* At this point, we have to look at the type of func to
4154 update the call stats properly. Do it here so as to avoid
4155 exposing the call stats machinery outside ceval.c
4156 */
4157 if (PyFunction_Check(func))
4158 PCALL(PCALL_FUNCTION);
4159 else if (PyMethod_Check(func))
4160 PCALL(PCALL_METHOD);
4161 else if (PyType_Check(func))
4162 PCALL(PCALL_TYPE);
Antoine Pitrou46dbe272009-05-30 21:27:00 +00004163 else if (PyCFunction_Check(func))
4164 PCALL(PCALL_CFUNCTION);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004165 else
4166 PCALL(PCALL_OTHER);
4167#endif
Antoine Pitrou46dbe272009-05-30 21:27:00 +00004168 if (PyCFunction_Check(func)) {
4169 PyThreadState *tstate = PyThreadState_GET();
4170 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4171 }
4172 else
4173 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00004174 call_fail:
4175 Py_XDECREF(callargs);
4176 Py_XDECREF(kwdict);
4177 return result;
4178}
4179
Fredrik Lundh7a830892006-05-27 10:39:48 +00004180static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00004181ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4182{
4183 int nstar = 0;
4184 PyObject *callargs = NULL;
4185 PyObject *stararg = NULL;
4186 PyObject *kwdict = NULL;
4187 PyObject *result = NULL;
4188
4189 if (flags & CALL_FLAG_KW) {
4190 kwdict = EXT_POP(*pp_stack);
Georg Brandl2134e752007-05-21 20:34:16 +00004191 if (!PyDict_Check(kwdict)) {
4192 PyObject *d;
4193 d = PyDict_New();
4194 if (d == NULL)
4195 goto ext_call_fail;
4196 if (PyDict_Update(d, kwdict) != 0) {
4197 Py_DECREF(d);
4198 /* PyDict_Update raises attribute
4199 * error (percolated from an attempt
4200 * to get 'keys' attribute) instead of
4201 * a type error if its second argument
4202 * is not a mapping.
4203 */
4204 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4205 PyErr_Format(PyExc_TypeError,
4206 "%.200s%.200s argument after ** "
4207 "must be a mapping, not %.200s",
4208 PyEval_GetFuncName(func),
4209 PyEval_GetFuncDesc(func),
4210 kwdict->ob_type->tp_name);
4211 }
4212 goto ext_call_fail;
4213 }
4214 Py_DECREF(kwdict);
4215 kwdict = d;
Jeremy Hylton52820442001-01-03 23:52:36 +00004216 }
4217 }
4218 if (flags & CALL_FLAG_VAR) {
4219 stararg = EXT_POP(*pp_stack);
4220 if (!PyTuple_Check(stararg)) {
4221 PyObject *t = NULL;
4222 t = PySequence_Tuple(stararg);
4223 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00004224 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4225 PyErr_Format(PyExc_TypeError,
Georg Brandl2134e752007-05-21 20:34:16 +00004226 "%.200s%.200s argument after * "
4227 "must be a sequence, not %200s",
Tim Peters6d6c1a32001-08-02 04:15:00 +00004228 PyEval_GetFuncName(func),
Georg Brandl2134e752007-05-21 20:34:16 +00004229 PyEval_GetFuncDesc(func),
4230 stararg->ob_type->tp_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00004231 }
Jeremy Hylton52820442001-01-03 23:52:36 +00004232 goto ext_call_fail;
4233 }
4234 Py_DECREF(stararg);
4235 stararg = t;
4236 }
4237 nstar = PyTuple_GET_SIZE(stararg);
4238 }
4239 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00004240 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00004241 if (kwdict == NULL)
4242 goto ext_call_fail;
4243 }
4244 callargs = update_star_args(na, nstar, stararg, pp_stack);
4245 if (callargs == NULL)
4246 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004247#ifdef CALL_PROFILE
4248 /* At this point, we have to look at the type of func to
4249 update the call stats properly. Do it here so as to avoid
4250 exposing the call stats machinery outside ceval.c
4251 */
4252 if (PyFunction_Check(func))
4253 PCALL(PCALL_FUNCTION);
4254 else if (PyMethod_Check(func))
4255 PCALL(PCALL_METHOD);
4256 else if (PyType_Check(func))
4257 PCALL(PCALL_TYPE);
Antoine Pitrou46dbe272009-05-30 21:27:00 +00004258 else if (PyCFunction_Check(func))
4259 PCALL(PCALL_CFUNCTION);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004260 else
4261 PCALL(PCALL_OTHER);
4262#endif
Antoine Pitrou46dbe272009-05-30 21:27:00 +00004263 if (PyCFunction_Check(func)) {
4264 PyThreadState *tstate = PyThreadState_GET();
4265 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4266 }
4267 else
4268 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersae406c62007-09-19 17:27:43 +00004269ext_call_fail:
Jeremy Hylton52820442001-01-03 23:52:36 +00004270 Py_XDECREF(callargs);
4271 Py_XDECREF(kwdict);
4272 Py_XDECREF(stararg);
4273 return result;
4274}
4275
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004276/* Extract a slice index from a PyInt or PyLong or an object with the
4277 nb_index slot defined, and store in *pi.
4278 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4279 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 +00004280 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004281*/
Tim Petersb5196382001-12-16 19:44:20 +00004282/* Note: If v is NULL, return success without storing into *pi. This
4283 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4284 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004285*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004286int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004287_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004288{
Tim Petersb5196382001-12-16 19:44:20 +00004289 if (v != NULL) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004290 Py_ssize_t x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00004291 if (PyInt_Check(v)) {
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00004292 /* XXX(nnorwitz): I think PyInt_AS_LONG is correct,
4293 however, it looks like it should be AsSsize_t.
4294 There should be a comment here explaining why.
4295 */
4296 x = PyInt_AS_LONG(v);
Tim Peters7df5e7f2006-05-26 23:14:37 +00004297 }
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00004298 else if (PyIndex_Check(v)) {
4299 x = PyNumber_AsSsize_t(v, NULL);
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004300 if (x == -1 && PyErr_Occurred())
4301 return 0;
4302 }
4303 else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00004304 PyErr_SetString(PyExc_TypeError,
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004305 "slice indices must be integers or "
4306 "None or have an __index__ method");
Guido van Rossum20c6add2000-05-08 14:06:50 +00004307 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004308 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00004309 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004310 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00004311 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004312}
4313
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004314#undef ISINDEX
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00004315#define ISINDEX(x) ((x) == NULL || \
4316 PyInt_Check(x) || PyLong_Check(x) || PyIndex_Check(x))
Guido van Rossum50d756e2001-08-18 17:43:36 +00004317
Fredrik Lundh7a830892006-05-27 10:39:48 +00004318static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004319apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004320{
Guido van Rossum50d756e2001-08-18 17:43:36 +00004321 PyTypeObject *tp = u->ob_type;
4322 PySequenceMethods *sq = tp->tp_as_sequence;
4323
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004324 if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004325 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
Guido van Rossum50d756e2001-08-18 17:43:36 +00004326 if (!_PyEval_SliceIndex(v, &ilow))
4327 return NULL;
4328 if (!_PyEval_SliceIndex(w, &ihigh))
4329 return NULL;
4330 return PySequence_GetSlice(u, ilow, ihigh);
4331 }
4332 else {
4333 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00004334 if (slice != NULL) {
4335 PyObject *res = PyObject_GetItem(u, slice);
4336 Py_DECREF(slice);
4337 return res;
4338 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00004339 else
4340 return NULL;
4341 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004342}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004343
Fredrik Lundh7a830892006-05-27 10:39:48 +00004344static int
Guido van Rossumac7be682001-01-17 15:42:30 +00004345assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
4346 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004347{
Guido van Rossum50d756e2001-08-18 17:43:36 +00004348 PyTypeObject *tp = u->ob_type;
4349 PySequenceMethods *sq = tp->tp_as_sequence;
4350
Georg Brandl0fca97a2007-03-05 22:28:08 +00004351 if (sq && sq->sq_ass_slice && ISINDEX(v) && ISINDEX(w)) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004352 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
Guido van Rossum50d756e2001-08-18 17:43:36 +00004353 if (!_PyEval_SliceIndex(v, &ilow))
4354 return -1;
4355 if (!_PyEval_SliceIndex(w, &ihigh))
4356 return -1;
4357 if (x == NULL)
4358 return PySequence_DelSlice(u, ilow, ihigh);
4359 else
4360 return PySequence_SetSlice(u, ilow, ihigh, x);
4361 }
4362 else {
4363 PyObject *slice = PySlice_New(v, w, NULL);
4364 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00004365 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00004366 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00004367 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00004368 else
Guido van Rossum354797c2001-12-03 19:45:06 +00004369 res = PyObject_DelItem(u, slice);
4370 Py_DECREF(slice);
4371 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00004372 }
4373 else
4374 return -1;
4375 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004376}
4377
Guido van Rossum04edb522008-03-18 02:49:46 +00004378#define Py3kExceptionClass_Check(x) \
4379 (PyType_Check((x)) && \
4380 PyType_FastSubclass((PyTypeObject*)(x), Py_TPFLAGS_BASE_EXC_SUBCLASS))
4381
4382#define CANNOT_CATCH_MSG "catching classes that don't inherit from " \
Georg Brandld5b635f2008-03-25 08:29:14 +00004383 "BaseException is not allowed in 3.x"
Guido van Rossum04edb522008-03-18 02:49:46 +00004384
Fredrik Lundh7a830892006-05-27 10:39:48 +00004385static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004386cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004387{
Guido van Rossumac7be682001-01-17 15:42:30 +00004388 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004389 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00004390 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00004391 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004392 break;
4393 case PyCmp_IS_NOT:
4394 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004395 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004396 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004397 res = PySequence_Contains(w, v);
4398 if (res < 0)
4399 return NULL;
4400 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004401 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00004402 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004403 if (res < 0)
4404 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004405 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004406 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004407 case PyCmp_EXC_MATCH:
Brett Cannon129bd522007-01-30 21:34:36 +00004408 if (PyTuple_Check(w)) {
4409 Py_ssize_t i, length;
4410 length = PyTuple_Size(w);
4411 for (i = 0; i < length; i += 1) {
4412 PyObject *exc = PyTuple_GET_ITEM(w, i);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004413 if (PyString_Check(exc)) {
Brett Cannon129bd522007-01-30 21:34:36 +00004414 int ret_val;
4415 ret_val = PyErr_WarnEx(
Thomas Wouterse2176022007-09-20 17:35:10 +00004416 PyExc_DeprecationWarning,
4417 "catching of string "
4418 "exceptions is deprecated", 1);
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00004419 if (ret_val < 0)
Brett Cannon129bd522007-01-30 21:34:36 +00004420 return NULL;
4421 }
Guido van Rossum20bda582008-03-18 03:15:05 +00004422 else if (Py_Py3kWarningFlag &&
4423 !PyTuple_Check(exc) &&
4424 !Py3kExceptionClass_Check(exc))
Guido van Rossum04edb522008-03-18 02:49:46 +00004425 {
4426 int ret_val;
4427 ret_val = PyErr_WarnEx(
4428 PyExc_DeprecationWarning,
4429 CANNOT_CATCH_MSG, 1);
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00004430 if (ret_val < 0)
Guido van Rossum04edb522008-03-18 02:49:46 +00004431 return NULL;
4432 }
Brett Cannon129bd522007-01-30 21:34:36 +00004433 }
4434 }
4435 else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004436 if (PyString_Check(w)) {
Brett Cannon129bd522007-01-30 21:34:36 +00004437 int ret_val;
4438 ret_val = PyErr_WarnEx(
4439 PyExc_DeprecationWarning,
4440 "catching of string "
Thomas Wouterse2176022007-09-20 17:35:10 +00004441 "exceptions is deprecated", 1);
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00004442 if (ret_val < 0)
Brett Cannon129bd522007-01-30 21:34:36 +00004443 return NULL;
4444 }
Guido van Rossum20bda582008-03-18 03:15:05 +00004445 else if (Py_Py3kWarningFlag &&
4446 !PyTuple_Check(w) &&
4447 !Py3kExceptionClass_Check(w))
Guido van Rossum04edb522008-03-18 02:49:46 +00004448 {
4449 int ret_val;
4450 ret_val = PyErr_WarnEx(
4451 PyExc_DeprecationWarning,
4452 CANNOT_CATCH_MSG, 1);
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00004453 if (ret_val < 0)
Guido van Rossum04edb522008-03-18 02:49:46 +00004454 return NULL;
4455 }
Brett Cannon129bd522007-01-30 21:34:36 +00004456 }
Barry Warsaw4249f541997-08-22 21:26:19 +00004457 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004458 break;
4459 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00004460 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004461 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004462 v = res ? Py_True : Py_False;
4463 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004464 return v;
4465}
4466
Fredrik Lundh7a830892006-05-27 10:39:48 +00004467static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00004468import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004469{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004470 PyObject *x;
4471
4472 x = PyObject_GetAttr(v, name);
4473 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00004474 PyErr_Format(PyExc_ImportError,
4475 "cannot import name %.230s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004476 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004477 }
Thomas Wouters52152252000-08-17 22:55:00 +00004478 return x;
4479}
Guido van Rossumac7be682001-01-17 15:42:30 +00004480
Fredrik Lundh7a830892006-05-27 10:39:48 +00004481static int
Thomas Wouters52152252000-08-17 22:55:00 +00004482import_all_from(PyObject *locals, PyObject *v)
4483{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004484 PyObject *all = PyObject_GetAttrString(v, "__all__");
4485 PyObject *dict, *name, *value;
4486 int skip_leading_underscores = 0;
4487 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004488
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004489 if (all == NULL) {
4490 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4491 return -1; /* Unexpected error */
4492 PyErr_Clear();
4493 dict = PyObject_GetAttrString(v, "__dict__");
4494 if (dict == NULL) {
4495 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4496 return -1;
4497 PyErr_SetString(PyExc_ImportError,
4498 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00004499 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004500 }
4501 all = PyMapping_Keys(dict);
4502 Py_DECREF(dict);
4503 if (all == NULL)
4504 return -1;
4505 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004506 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004507
4508 for (pos = 0, err = 0; ; pos++) {
4509 name = PySequence_GetItem(all, pos);
4510 if (name == NULL) {
4511 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4512 err = -1;
4513 else
4514 PyErr_Clear();
4515 break;
4516 }
4517 if (skip_leading_underscores &&
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004518 PyString_Check(name) &&
4519 PyString_AS_STRING(name)[0] == '_')
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004520 {
4521 Py_DECREF(name);
4522 continue;
4523 }
4524 value = PyObject_GetAttr(v, name);
4525 if (value == NULL)
4526 err = -1;
Armin Rigo70370852006-11-29 21:59:22 +00004527 else if (PyDict_CheckExact(locals))
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004528 err = PyDict_SetItem(locals, name, value);
Armin Rigo70370852006-11-29 21:59:22 +00004529 else
4530 err = PyObject_SetItem(locals, name, value);
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004531 Py_DECREF(name);
4532 Py_XDECREF(value);
4533 if (err != 0)
4534 break;
4535 }
4536 Py_DECREF(all);
4537 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004538}
4539
Fredrik Lundh7a830892006-05-27 10:39:48 +00004540static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004541build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004542{
Guido van Rossum7851eea2001-09-12 19:19:18 +00004543 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004544
4545 if (PyDict_Check(methods))
4546 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00004547 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00004548 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004549 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4550 base = PyTuple_GET_ITEM(bases, 0);
4551 metaclass = PyObject_GetAttrString(base, "__class__");
4552 if (metaclass == NULL) {
4553 PyErr_Clear();
4554 metaclass = (PyObject *)base->ob_type;
4555 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00004556 }
4557 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004558 else {
4559 PyObject *g = PyEval_GetGlobals();
4560 if (g != NULL && PyDict_Check(g))
4561 metaclass = PyDict_GetItemString(g, "__metaclass__");
4562 if (metaclass == NULL)
4563 metaclass = (PyObject *) &PyClass_Type;
4564 Py_INCREF(metaclass);
4565 }
Jeremy Hylton7c1e3472007-02-26 16:14:51 +00004566 result = PyObject_CallFunctionObjArgs(metaclass, name, bases, methods,
Thomas Woutersae406c62007-09-19 17:27:43 +00004567 NULL);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004568 Py_DECREF(metaclass);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004569 if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
Tim Peters7df5e7f2006-05-26 23:14:37 +00004570 /* A type error here likely means that the user passed
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004571 in a base that was not a class (such the random module
4572 instead of the random.random type). Help them out with
Raymond Hettingercfc31922004-09-16 16:41:57 +00004573 by augmenting the error message with more information.*/
4574
4575 PyObject *ptype, *pvalue, *ptraceback;
4576
4577 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004578 if (PyString_Check(pvalue)) {
Raymond Hettingercfc31922004-09-16 16:41:57 +00004579 PyObject *newmsg;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004580 newmsg = PyString_FromFormat(
Jeremy Hylton7c1e3472007-02-26 16:14:51 +00004581 "Error when calling the metaclass bases\n"
Thomas Woutersae406c62007-09-19 17:27:43 +00004582 " %s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004583 PyString_AS_STRING(pvalue));
Raymond Hettingercfc31922004-09-16 16:41:57 +00004584 if (newmsg != NULL) {
4585 Py_DECREF(pvalue);
4586 pvalue = newmsg;
4587 }
4588 }
4589 PyErr_Restore(ptype, pvalue, ptraceback);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004590 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004591 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00004592}
4593
Fredrik Lundh7a830892006-05-27 10:39:48 +00004594static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004595exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
4596 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004597{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004598 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00004599 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004600 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004601
Guido van Rossumb209a111997-04-29 18:18:01 +00004602 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
4603 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004604 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00004605 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004606 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00004607 locals = PyTuple_GetItem(prog, 2);
4608 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004609 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004610 if (globals == Py_None) {
4611 globals = PyEval_GetGlobals();
4612 if (locals == Py_None) {
4613 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00004614 plain = 1;
4615 }
Neal Norwitzdf6a6492006-08-13 18:10:10 +00004616 if (!globals || !locals) {
4617 PyErr_SetString(PyExc_SystemError,
4618 "globals and locals cannot be NULL");
4619 return -1;
4620 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004621 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004622 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004623 locals = globals;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004624 if (!PyString_Check(prog) &&
Benjamin Peterson78821dd2009-01-25 17:15:10 +00004625#ifdef Py_USING_UNICODE
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004626 !PyUnicode_Check(prog) &&
Benjamin Peterson78821dd2009-01-25 17:15:10 +00004627#endif
Guido van Rossumb209a111997-04-29 18:18:01 +00004628 !PyCode_Check(prog) &&
4629 !PyFile_Check(prog)) {
4630 PyErr_SetString(PyExc_TypeError,
Guido van Rossumac7be682001-01-17 15:42:30 +00004631 "exec: arg 1 must be a string, file, or code object");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004632 return -1;
4633 }
Fred Drake661ea262000-10-24 19:57:45 +00004634 if (!PyDict_Check(globals)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004635 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00004636 "exec: arg 2 must be a dictionary or None");
4637 return -1;
4638 }
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004639 if (!PyMapping_Check(locals)) {
Fred Drake661ea262000-10-24 19:57:45 +00004640 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004641 "exec: arg 3 must be a mapping or None");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004642 return -1;
4643 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004644 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00004645 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00004646 if (PyCode_Check(prog)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +00004647 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4648 PyErr_SetString(PyExc_TypeError,
4649 "code object passed to exec may not contain free variables");
4650 return -1;
4651 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004652 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004653 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004654 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004655 FILE *fp = PyFile_AsFile(prog);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004656 char *name = PyString_AsString(PyFile_Name(prog));
Jeremy Hylton714b1122007-02-25 16:01:58 +00004657 PyCompilerFlags cf;
Thomas Woutersae406c62007-09-19 17:27:43 +00004658 if (name == NULL)
4659 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004660 cf.cf_flags = 0;
4661 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004662 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004663 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004664 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004665 v = PyRun_File(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004666 locals);
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004667 }
4668 else {
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004669 PyObject *tmp = NULL;
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004670 char *str;
Tim Peters5ba58662001-07-16 02:29:45 +00004671 PyCompilerFlags cf;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004672 cf.cf_flags = 0;
4673#ifdef Py_USING_UNICODE
4674 if (PyUnicode_Check(prog)) {
4675 tmp = PyUnicode_AsUTF8String(prog);
4676 if (tmp == NULL)
4677 return -1;
4678 prog = tmp;
4679 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4680 }
4681#endif
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004682 if (PyString_AsStringAndSize(prog, &str, NULL))
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004683 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004684 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters8a5c3c72004-04-05 19:36:21 +00004685 v = PyRun_StringFlags(str, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004686 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004687 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004688 v = PyRun_String(str, Py_file_input, globals, locals);
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004689 Py_XDECREF(tmp);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004690 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004691 if (plain)
4692 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004693 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004694 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00004695 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004696 return 0;
4697}
Guido van Rossum24c13741995-02-14 09:42:43 +00004698
Fredrik Lundh7a830892006-05-27 10:39:48 +00004699static void
Paul Prescode68140d2000-08-30 20:25:01 +00004700format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4701{
4702 char *obj_str;
4703
4704 if (!obj)
4705 return;
4706
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004707 obj_str = PyString_AsString(obj);
Paul Prescode68140d2000-08-30 20:25:01 +00004708 if (!obj_str)
4709 return;
4710
4711 PyErr_Format(exc, format_str, obj_str);
4712}
Guido van Rossum950361c1997-01-24 13:49:28 +00004713
Fredrik Lundh7a830892006-05-27 10:39:48 +00004714static PyObject *
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004715string_concatenate(PyObject *v, PyObject *w,
4716 PyFrameObject *f, unsigned char *next_instr)
4717{
4718 /* This function implements 'variable += expr' when both arguments
4719 are strings. */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004720 Py_ssize_t v_len = PyString_GET_SIZE(v);
4721 Py_ssize_t w_len = PyString_GET_SIZE(w);
Armin Rigo97ff0472006-08-09 15:37:26 +00004722 Py_ssize_t new_len = v_len + w_len;
4723 if (new_len < 0) {
4724 PyErr_SetString(PyExc_OverflowError,
4725 "strings are too large to concat");
4726 return NULL;
4727 }
Tim Peters7df5e7f2006-05-26 23:14:37 +00004728
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004729 if (v->ob_refcnt == 2) {
4730 /* In the common case, there are 2 references to the value
4731 * stored in 'variable' when the += is performed: one on the
Thomas Wouterse2176022007-09-20 17:35:10 +00004732 * value stack (in 'v') and one still stored in the
4733 * 'variable'. We try to delete the variable now to reduce
4734 * the refcnt to 1.
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004735 */
4736 switch (*next_instr) {
4737 case STORE_FAST:
4738 {
4739 int oparg = PEEKARG();
4740 PyObject **fastlocals = f->f_localsplus;
4741 if (GETLOCAL(oparg) == v)
4742 SETLOCAL(oparg, NULL);
4743 break;
4744 }
4745 case STORE_DEREF:
4746 {
Thomas Wouterse2176022007-09-20 17:35:10 +00004747 PyObject **freevars = (f->f_localsplus +
4748 f->f_code->co_nlocals);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004749 PyObject *c = freevars[PEEKARG()];
4750 if (PyCell_GET(c) == v)
4751 PyCell_Set(c, NULL);
4752 break;
4753 }
4754 case STORE_NAME:
4755 {
4756 PyObject *names = f->f_code->co_names;
4757 PyObject *name = GETITEM(names, PEEKARG());
4758 PyObject *locals = f->f_locals;
4759 if (PyDict_CheckExact(locals) &&
4760 PyDict_GetItem(locals, name) == v) {
4761 if (PyDict_DelItem(locals, name) != 0) {
4762 PyErr_Clear();
4763 }
4764 }
4765 break;
4766 }
4767 }
4768 }
4769
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004770 if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004771 /* Now we own the last reference to 'v', so we can resize it
4772 * in-place.
4773 */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004774 if (_PyString_Resize(&v, new_len) != 0) {
4775 /* XXX if _PyString_Resize() fails, 'v' has been
Thomas Wouterse2176022007-09-20 17:35:10 +00004776 * deallocated so it cannot be put back into
4777 * 'variable'. The MemoryError is raised when there
4778 * is no value in 'variable', which might (very
4779 * remotely) be a cause of incompatibilities.
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004780 */
4781 return NULL;
4782 }
4783 /* copy 'w' into the newly allocated area of 'v' */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004784 memcpy(PyString_AS_STRING(v) + v_len,
4785 PyString_AS_STRING(w), w_len);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004786 return v;
4787 }
4788 else {
4789 /* When in-place resizing is not an option. */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004790 PyString_Concat(&v, w);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004791 return v;
4792 }
4793}
4794
Guido van Rossum950361c1997-01-24 13:49:28 +00004795#ifdef DYNAMIC_EXECUTION_PROFILE
4796
Fredrik Lundh7a830892006-05-27 10:39:48 +00004797static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004798getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004799{
4800 int i;
4801 PyObject *l = PyList_New(256);
4802 if (l == NULL) return NULL;
4803 for (i = 0; i < 256; i++) {
4804 PyObject *x = PyInt_FromLong(a[i]);
4805 if (x == NULL) {
4806 Py_DECREF(l);
4807 return NULL;
4808 }
4809 PyList_SetItem(l, i, x);
4810 }
4811 for (i = 0; i < 256; i++)
4812 a[i] = 0;
4813 return l;
4814}
4815
4816PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004817_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004818{
4819#ifndef DXPAIRS
4820 return getarray(dxp);
4821#else
4822 int i;
4823 PyObject *l = PyList_New(257);
4824 if (l == NULL) return NULL;
4825 for (i = 0; i < 257; i++) {
4826 PyObject *x = getarray(dxpairs[i]);
4827 if (x == NULL) {
4828 Py_DECREF(l);
4829 return NULL;
4830 }
4831 PyList_SetItem(l, i, x);
4832 }
4833 return l;
4834#endif
4835}
4836
4837#endif