blob: 2c7f57b56a1fa5fb0b165cd83e676bb8d49f124c [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003
Guido van Rossum681d79a1995-07-18 14:51:37 +00004/* XXX TO DO:
Guido van Rossum681d79a1995-07-18 14:51:37 +00005 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +00006 XXX document it!
7 */
8
Thomas Wouters477c8d52006-05-27 19:21:47 +00009/* enable more aggressive intra-module optimizations, where available */
10#define PY_LOCAL_AGGRESSIVE
11
Guido van Rossumb209a111997-04-29 18:18:01 +000012#include "Python.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000013
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000014#include "code.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000015#include "frameobject.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000016#include "eval.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000017#include "opcode.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000018#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000019
Guido van Rossumc6004111993-11-05 10:22:19 +000020#include <ctype.h>
21
Thomas Wouters477c8d52006-05-27 19:21:47 +000022#ifndef WITH_TSC
Michael W. Hudson75eabd22005-01-18 15:56:11 +000023
24#define READ_TIMESTAMP(var)
25
26#else
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000027
28typedef unsigned long long uint64;
29
Michael W. Hudson800ba232004-08-12 18:19:17 +000030#if defined(__ppc__) /* <- Don't know if this is the correct symbol; this
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000031 section should work for GCC on any PowerPC
32 platform, irrespective of OS.
33 POWER? Who knows :-) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000034
Michael W. Hudson75eabd22005-01-18 15:56:11 +000035#define READ_TIMESTAMP(var) ppc_getcounter(&var)
Michael W. Hudson800ba232004-08-12 18:19:17 +000036
37static void
38ppc_getcounter(uint64 *v)
39{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000040 register unsigned long tbu, tb, tbu2;
Michael W. Hudson800ba232004-08-12 18:19:17 +000041
42 loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000043 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;
Michael W. Hudson800ba232004-08-12 18:19:17 +000047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048 /* The slightly peculiar way of writing the next lines is
49 compiled better by GCC than any other way I tried. */
50 ((long*)(v))[0] = tbu;
51 ((long*)(v))[1] = tb;
Michael W. Hudson800ba232004-08-12 18:19:17 +000052}
53
Mark Dickinsona25b1312009-10-31 10:18:44 +000054#elif defined(__i386__)
55
56/* this is for linux/x86 (and probably any other GCC/x86 combo) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000057
Michael W. Hudson75eabd22005-01-18 15:56:11 +000058#define READ_TIMESTAMP(val) \
59 __asm__ __volatile__("rdtsc" : "=A" (val))
Michael W. Hudson800ba232004-08-12 18:19:17 +000060
Mark Dickinsona25b1312009-10-31 10:18:44 +000061#elif defined(__x86_64__)
62
63/* for gcc/x86_64, the "A" constraint in DI mode means *either* rax *or* rdx;
64 not edx:eax as it does for i386. Since rdtsc puts its result in edx:eax
65 even in 64-bit mode, we need to use "a" and "d" for the lower and upper
66 32-bit pieces of the result. */
67
68#define READ_TIMESTAMP(val) \
69 __asm__ __volatile__("rdtsc" : \
70 "=a" (((int*)&(val))[0]), "=d" (((int*)&(val))[1]));
71
72
73#else
74
75#error "Don't know how to implement timestamp counter for this architecture"
76
Michael W. Hudson800ba232004-08-12 18:19:17 +000077#endif
78
Thomas Wouters477c8d52006-05-27 19:21:47 +000079void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000080 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000081{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 uint64 intr, inst, loop;
83 PyThreadState *tstate = PyThreadState_Get();
84 if (!tstate->interp->tscdump)
85 return;
86 intr = intr1 - intr0;
87 inst = inst1 - inst0 - intr;
88 loop = loop1 - loop0 - intr;
89 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
Stefan Krahb7e10102010-06-23 18:42:39 +000090 opcode, ticked, inst, loop);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000091}
Michael W. Hudson800ba232004-08-12 18:19:17 +000092
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000093#endif
94
Guido van Rossum04691fc1992-08-12 15:35:34 +000095/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000096/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000097
Guido van Rossum408027e1996-12-30 16:17:54 +000098#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000099/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100#define LLTRACE 1 /* Low-level trace feature */
101#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000102#endif
103
Jeremy Hylton52820442001-01-03 23:52:36 +0000104typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +0000105
Guido van Rossum374a9221991-04-04 10:40:29 +0000106/* Forward declarations */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000107#ifdef WITH_TSC
Thomas Wouters477c8d52006-05-27 19:21:47 +0000108static PyObject * call_function(PyObject ***, int, uint64*, uint64*);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000109#else
Thomas Wouters477c8d52006-05-27 19:21:47 +0000110static PyObject * call_function(PyObject ***, int);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000111#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000112static PyObject * fast_function(PyObject *, PyObject ***, int, int, int);
113static PyObject * do_call(PyObject *, PyObject ***, int, int);
114static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int);
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000115static PyObject * update_keyword_args(PyObject *, int, PyObject ***,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000117static PyObject * update_star_args(int, int, PyObject *, PyObject ***);
118static PyObject * load_args(PyObject ***, int);
Jeremy Hylton52820442001-01-03 23:52:36 +0000119#define CALL_FLAG_VAR 1
120#define CALL_FLAG_KW 2
121
Guido van Rossum0a066c01992-03-27 17:29:15 +0000122#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +0000123static int lltrace;
Tim Petersdbd9ba62000-07-09 03:09:57 +0000124static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +0000125#endif
Fred Drake5755ce62001-06-27 19:19:46 +0000126static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +0000128static int call_trace_protected(Py_tracefunc, PyObject *,
Stefan Krahb7e10102010-06-23 18:42:39 +0000129 PyFrameObject *, int, PyObject *);
Fred Drake5755ce62001-06-27 19:19:46 +0000130static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +0000131static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Stefan Krahb7e10102010-06-23 18:42:39 +0000132 PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000133
Thomas Wouters477c8d52006-05-27 19:21:47 +0000134static PyObject * cmp_outcome(int, PyObject *, PyObject *);
135static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +0000136static int import_all_from(PyObject *, PyObject *);
Neal Norwitzda059e32007-08-26 05:33:45 +0000137static void format_exc_check_arg(PyObject *, const char *, PyObject *);
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +0000138static void format_exc_unbound(PyCodeObject *co, int oparg);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000139static PyObject * unicode_concatenate(PyObject *, PyObject *,
140 PyFrameObject *, unsigned char *);
Benjamin Peterson876b2f22009-06-28 03:18:59 +0000141static PyObject * special_lookup(PyObject *, char *, PyObject **);
Guido van Rossum374a9221991-04-04 10:40:29 +0000142
Paul Prescode68140d2000-08-30 20:25:01 +0000143#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000145#define GLOBAL_NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000146 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000147#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000149#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000150 "free variable '%.200s' referenced before assignment" \
151 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000152
Guido van Rossum950361c1997-01-24 13:49:28 +0000153/* Dynamic execution profile */
154#ifdef DYNAMIC_EXECUTION_PROFILE
155#ifdef DXPAIRS
156static long dxpairs[257][256];
157#define dxp dxpairs[256]
158#else
159static long dxp[256];
160#endif
161#endif
162
Jeremy Hylton985eba52003-02-05 23:13:00 +0000163/* Function call profile */
164#ifdef CALL_PROFILE
165#define PCALL_NUM 11
166static int pcall[PCALL_NUM];
167
168#define PCALL_ALL 0
169#define PCALL_FUNCTION 1
170#define PCALL_FAST_FUNCTION 2
171#define PCALL_FASTER_FUNCTION 3
172#define PCALL_METHOD 4
173#define PCALL_BOUND_METHOD 5
174#define PCALL_CFUNCTION 6
175#define PCALL_TYPE 7
176#define PCALL_GENERATOR 8
177#define PCALL_OTHER 9
178#define PCALL_POP 10
179
180/* Notes about the statistics
181
182 PCALL_FAST stats
183
184 FAST_FUNCTION means no argument tuple needs to be created.
185 FASTER_FUNCTION means that the fast-path frame setup code is used.
186
187 If there is a method call where the call can be optimized by changing
188 the argument tuple and calling the function directly, it gets recorded
189 twice.
190
191 As a result, the relationship among the statistics appears to be
192 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
193 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
194 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
195 PCALL_METHOD > PCALL_BOUND_METHOD
196*/
197
198#define PCALL(POS) pcall[POS]++
199
200PyObject *
201PyEval_GetCallStats(PyObject *self)
202{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000203 return Py_BuildValue("iiiiiiiiiii",
204 pcall[0], pcall[1], pcall[2], pcall[3],
205 pcall[4], pcall[5], pcall[6], pcall[7],
206 pcall[8], pcall[9], pcall[10]);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000207}
208#else
209#define PCALL(O)
210
211PyObject *
212PyEval_GetCallStats(PyObject *self)
213{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 Py_INCREF(Py_None);
215 return Py_None;
Jeremy Hylton985eba52003-02-05 23:13:00 +0000216}
217#endif
218
Tim Peters5ca576e2001-06-18 22:08:13 +0000219
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000220#ifdef WITH_THREAD
221#define GIL_REQUEST _Py_atomic_load_relaxed(&gil_drop_request)
222#else
223#define GIL_REQUEST 0
224#endif
225
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000226/* This can set eval_breaker to 0 even though gil_drop_request became
227 1. We believe this is all right because the eval loop will release
228 the GIL eventually anyway. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000229#define COMPUTE_EVAL_BREAKER() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000230 _Py_atomic_store_relaxed( \
231 &eval_breaker, \
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000232 GIL_REQUEST | \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 _Py_atomic_load_relaxed(&pendingcalls_to_do) | \
234 pending_async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000235
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000236#ifdef WITH_THREAD
237
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000238#define SET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 do { \
240 _Py_atomic_store_relaxed(&gil_drop_request, 1); \
241 _Py_atomic_store_relaxed(&eval_breaker, 1); \
242 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000243
244#define RESET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 do { \
246 _Py_atomic_store_relaxed(&gil_drop_request, 0); \
247 COMPUTE_EVAL_BREAKER(); \
248 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000249
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000250#endif
251
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000252/* Pending calls are only modified under pending_lock */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000253#define SIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 do { \
255 _Py_atomic_store_relaxed(&pendingcalls_to_do, 1); \
256 _Py_atomic_store_relaxed(&eval_breaker, 1); \
257 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000258
259#define UNSIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 do { \
261 _Py_atomic_store_relaxed(&pendingcalls_to_do, 0); \
262 COMPUTE_EVAL_BREAKER(); \
263 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000264
265#define SIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 do { \
267 pending_async_exc = 1; \
268 _Py_atomic_store_relaxed(&eval_breaker, 1); \
269 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000270
271#define UNSIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 do { pending_async_exc = 0; COMPUTE_EVAL_BREAKER(); } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000273
274
Guido van Rossume59214e1994-08-30 08:01:59 +0000275#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000276
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000277#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000278#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000279#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000280#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000281
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000282static PyThread_type_lock pending_lock = 0; /* for pending calls */
Guido van Rossuma9672091994-09-14 13:31:22 +0000283static long main_thread = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000284/* This single variable consolidates all requests to break out of the fast path
285 in the eval loop. */
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000286static _Py_atomic_int eval_breaker = {0};
287/* Request for dropping the GIL */
288static _Py_atomic_int gil_drop_request = {0};
289/* Request for running pending calls. */
290static _Py_atomic_int pendingcalls_to_do = {0};
291/* Request for looking at the `async_exc` field of the current thread state.
292 Guarded by the GIL. */
293static int pending_async_exc = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000294
295#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000296
Tim Peters7f468f22004-10-11 02:40:51 +0000297int
298PyEval_ThreadsInitialized(void)
299{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 return gil_created();
Tim Peters7f468f22004-10-11 02:40:51 +0000301}
302
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000303void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000304PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000305{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 if (gil_created())
307 return;
308 create_gil();
309 take_gil(PyThreadState_GET());
310 main_thread = PyThread_get_thread_ident();
311 if (!pending_lock)
312 pending_lock = PyThread_allocate_lock();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000313}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000314
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000315void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000316PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000317{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 PyThreadState *tstate = PyThreadState_GET();
319 if (tstate == NULL)
320 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
321 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000322}
323
324void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000325PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000326{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 /* This function must succeed when the current thread state is NULL.
328 We therefore avoid PyThreadState_GET() which dumps a fatal error
329 in debug mode.
330 */
331 drop_gil((PyThreadState*)_Py_atomic_load_relaxed(
332 &_PyThreadState_Current));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000333}
334
335void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000336PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000337{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 if (tstate == NULL)
339 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
340 /* Check someone has called PyEval_InitThreads() to create the lock */
341 assert(gil_created());
342 take_gil(tstate);
343 if (PyThreadState_Swap(tstate) != NULL)
344 Py_FatalError(
345 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000346}
347
348void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000349PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000350{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 if (tstate == NULL)
352 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
353 if (PyThreadState_Swap(NULL) != tstate)
354 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
355 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000356}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000357
358/* This function is called from PyOS_AfterFork to ensure that newly
359 created child processes don't hold locks referring to threads which
360 are not running in the child process. (This could also be done using
361 pthread_atfork mechanism, at least for the pthreads implementation.) */
362
363void
364PyEval_ReInitThreads(void)
365{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 PyObject *threading, *result;
367 PyThreadState *tstate = PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 if (!gil_created())
370 return;
371 /*XXX Can't use PyThread_free_lock here because it does too
372 much error-checking. Doing this cleanly would require
373 adding a new function to each thread_*.h. Instead, just
374 create a new lock and waste a little bit of memory */
375 recreate_gil();
376 pending_lock = PyThread_allocate_lock();
377 take_gil(tstate);
378 main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 /* Update the threading module with the new state.
381 */
382 tstate = PyThreadState_GET();
383 threading = PyMapping_GetItemString(tstate->interp->modules,
384 "threading");
385 if (threading == NULL) {
386 /* threading not imported */
387 PyErr_Clear();
388 return;
389 }
390 result = PyObject_CallMethod(threading, "_after_fork", NULL);
391 if (result == NULL)
392 PyErr_WriteUnraisable(threading);
393 else
394 Py_DECREF(result);
395 Py_DECREF(threading);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000396}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000397
398#else
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000399static _Py_atomic_int eval_breaker = {0};
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000400static int pending_async_exc = 0;
401#endif /* WITH_THREAD */
402
403/* This function is used to signal that async exceptions are waiting to be
404 raised, therefore it is also useful in non-threaded builds. */
405
406void
407_PyEval_SignalAsyncExc(void)
408{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000410}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000411
Guido van Rossumff4949e1992-08-05 19:58:53 +0000412/* Functions save_thread and restore_thread are always defined so
413 dynamically loaded modules needn't be compiled separately for use
414 with and without threads: */
415
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000416PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000417PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000418{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000419 PyThreadState *tstate = PyThreadState_Swap(NULL);
420 if (tstate == NULL)
421 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000422#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000423 if (gil_created())
424 drop_gil(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000425#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000427}
428
429void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000430PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000431{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 if (tstate == NULL)
433 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000434#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 if (gil_created()) {
436 int err = errno;
437 take_gil(tstate);
438 errno = err;
439 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000440#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000442}
443
444
Guido van Rossuma9672091994-09-14 13:31:22 +0000445/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
446 signal handlers or Mac I/O completion routines) can schedule calls
447 to a function to be called synchronously.
448 The synchronous function is called with one void* argument.
449 It should return 0 for success or -1 for failure -- failure should
450 be accompanied by an exception.
451
452 If registry succeeds, the registry function returns 0; if it fails
453 (e.g. due to too many pending calls) it returns -1 (without setting
454 an exception condition).
455
456 Note that because registry may occur from within signal handlers,
457 or other asynchronous events, calling malloc() is unsafe!
458
459#ifdef WITH_THREAD
460 Any thread can schedule pending calls, but only the main thread
461 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000462 There is no facility to schedule calls to a particular thread, but
463 that should be easy to change, should that ever be required. In
464 that case, the static variables here should go into the python
465 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000466#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000467*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000468
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000469#ifdef WITH_THREAD
470
471/* The WITH_THREAD implementation is thread-safe. It allows
472 scheduling to be made from any thread, and even from an executing
473 callback.
474 */
475
476#define NPENDINGCALLS 32
477static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 int (*func)(void *);
479 void *arg;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000480} pendingcalls[NPENDINGCALLS];
481static int pendingfirst = 0;
482static int pendinglast = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000483static char pendingbusy = 0;
484
485int
486Py_AddPendingCall(int (*func)(void *), void *arg)
487{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 int i, j, result=0;
489 PyThread_type_lock lock = pending_lock;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000490
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 /* try a few times for the lock. Since this mechanism is used
492 * for signal handling (on the main thread), there is a (slim)
493 * chance that a signal is delivered on the same thread while we
494 * hold the lock during the Py_MakePendingCalls() function.
495 * This avoids a deadlock in that case.
496 * Note that signals can be delivered on any thread. In particular,
497 * on Windows, a SIGINT is delivered on a system-created worker
498 * thread.
499 * We also check for lock being NULL, in the unlikely case that
500 * this function is called before any bytecode evaluation takes place.
501 */
502 if (lock != NULL) {
503 for (i = 0; i<100; i++) {
504 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
505 break;
506 }
507 if (i == 100)
508 return -1;
509 }
510
511 i = pendinglast;
512 j = (i + 1) % NPENDINGCALLS;
513 if (j == pendingfirst) {
514 result = -1; /* Queue full */
515 } else {
516 pendingcalls[i].func = func;
517 pendingcalls[i].arg = arg;
518 pendinglast = j;
519 }
520 /* signal main loop */
521 SIGNAL_PENDING_CALLS();
522 if (lock != NULL)
523 PyThread_release_lock(lock);
524 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000525}
526
527int
528Py_MakePendingCalls(void)
529{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 int i;
531 int r = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000532
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 if (!pending_lock) {
534 /* initial allocation of the lock */
535 pending_lock = PyThread_allocate_lock();
536 if (pending_lock == NULL)
537 return -1;
538 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000539
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 /* only service pending calls on main thread */
541 if (main_thread && PyThread_get_thread_ident() != main_thread)
542 return 0;
543 /* don't perform recursive pending calls */
544 if (pendingbusy)
545 return 0;
546 pendingbusy = 1;
547 /* perform a bounded number of calls, in case of recursion */
548 for (i=0; i<NPENDINGCALLS; i++) {
549 int j;
550 int (*func)(void *);
551 void *arg = NULL;
552
553 /* pop one item off the queue while holding the lock */
554 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
555 j = pendingfirst;
556 if (j == pendinglast) {
557 func = NULL; /* Queue empty */
558 } else {
559 func = pendingcalls[j].func;
560 arg = pendingcalls[j].arg;
561 pendingfirst = (j + 1) % NPENDINGCALLS;
562 }
563 if (pendingfirst != pendinglast)
564 SIGNAL_PENDING_CALLS();
565 else
566 UNSIGNAL_PENDING_CALLS();
567 PyThread_release_lock(pending_lock);
568 /* having released the lock, perform the callback */
569 if (func == NULL)
570 break;
571 r = func(arg);
572 if (r)
573 break;
574 }
575 pendingbusy = 0;
576 return r;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000577}
578
579#else /* if ! defined WITH_THREAD */
580
581/*
582 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
583 This code is used for signal handling in python that isn't built
584 with WITH_THREAD.
585 Don't use this implementation when Py_AddPendingCalls() can happen
586 on a different thread!
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587
Guido van Rossuma9672091994-09-14 13:31:22 +0000588 There are two possible race conditions:
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000589 (1) nested asynchronous calls to Py_AddPendingCall()
590 (2) AddPendingCall() calls made while pending calls are being processed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000592 (1) is very unlikely because typically signal delivery
593 is blocked during signal handling. So it should be impossible.
594 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000595 The current code is safe against (2), but not against (1).
596 The safety against (2) is derived from the fact that only one
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000597 thread is present, interrupted by signals, and that the critical
598 section is protected with the "busy" variable. On Windows, which
599 delivers SIGINT on a system thread, this does not hold and therefore
600 Windows really shouldn't use this version.
601 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000602*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000603
Guido van Rossuma9672091994-09-14 13:31:22 +0000604#define NPENDINGCALLS 32
605static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 int (*func)(void *);
607 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000608} pendingcalls[NPENDINGCALLS];
609static volatile int pendingfirst = 0;
610static volatile int pendinglast = 0;
Benjamin Peterson08ec84c2010-05-30 14:49:32 +0000611static _Py_atomic_int pendingcalls_to_do = {0};
Guido van Rossuma9672091994-09-14 13:31:22 +0000612
613int
Thomas Wouters334fb892000-07-25 12:56:38 +0000614Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000615{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 static volatile int busy = 0;
617 int i, j;
618 /* XXX Begin critical section */
619 if (busy)
620 return -1;
621 busy = 1;
622 i = pendinglast;
623 j = (i + 1) % NPENDINGCALLS;
624 if (j == pendingfirst) {
625 busy = 0;
626 return -1; /* Queue full */
627 }
628 pendingcalls[i].func = func;
629 pendingcalls[i].arg = arg;
630 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000631
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 SIGNAL_PENDING_CALLS();
633 busy = 0;
634 /* XXX End critical section */
635 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000636}
637
Guido van Rossum180d7b41994-09-29 09:45:57 +0000638int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000639Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000640{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 static int busy = 0;
642 if (busy)
643 return 0;
644 busy = 1;
645 UNSIGNAL_PENDING_CALLS();
646 for (;;) {
647 int i;
648 int (*func)(void *);
649 void *arg;
650 i = pendingfirst;
651 if (i == pendinglast)
652 break; /* Queue empty */
653 func = pendingcalls[i].func;
654 arg = pendingcalls[i].arg;
655 pendingfirst = (i + 1) % NPENDINGCALLS;
656 if (func(arg) < 0) {
657 busy = 0;
658 SIGNAL_PENDING_CALLS(); /* We're not done yet */
659 return -1;
660 }
661 }
662 busy = 0;
663 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000664}
665
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000666#endif /* WITH_THREAD */
667
Guido van Rossuma9672091994-09-14 13:31:22 +0000668
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000669/* The interpreter's recursion limit */
670
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000671#ifndef Py_DEFAULT_RECURSION_LIMIT
672#define Py_DEFAULT_RECURSION_LIMIT 1000
673#endif
674static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
675int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000676
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000677int
678Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000679{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 return recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000681}
682
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000683void
684Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000685{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 recursion_limit = new_limit;
687 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000688}
689
Armin Rigo2b3eb402003-10-28 12:05:48 +0000690/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
691 if the recursion_depth reaches _Py_CheckRecursionLimit.
692 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
693 to guarantee that _Py_CheckRecursiveCall() is regularly called.
694 Without USE_STACKCHECK, there is no need for this. */
695int
696_Py_CheckRecursiveCall(char *where)
697{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 PyThreadState *tstate = PyThreadState_GET();
Armin Rigo2b3eb402003-10-28 12:05:48 +0000699
700#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 if (PyOS_CheckStack()) {
702 --tstate->recursion_depth;
703 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
704 return -1;
705 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000706#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 _Py_CheckRecursionLimit = recursion_limit;
708 if (tstate->recursion_critical)
709 /* Somebody asked that we don't check for recursion. */
710 return 0;
711 if (tstate->overflowed) {
712 if (tstate->recursion_depth > recursion_limit + 50) {
713 /* Overflowing while handling an overflow. Give up. */
714 Py_FatalError("Cannot recover from stack overflow.");
715 }
716 return 0;
717 }
718 if (tstate->recursion_depth > recursion_limit) {
719 --tstate->recursion_depth;
720 tstate->overflowed = 1;
721 PyErr_Format(PyExc_RuntimeError,
722 "maximum recursion depth exceeded%s",
723 where);
724 return -1;
725 }
726 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000727}
728
Guido van Rossum374a9221991-04-04 10:40:29 +0000729/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000730enum why_code {
Stefan Krahb7e10102010-06-23 18:42:39 +0000731 WHY_NOT = 0x0001, /* No error */
732 WHY_EXCEPTION = 0x0002, /* Exception occurred */
733 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
734 WHY_RETURN = 0x0008, /* 'return' statement */
735 WHY_BREAK = 0x0010, /* 'break' statement */
736 WHY_CONTINUE = 0x0020, /* 'continue' statement */
737 WHY_YIELD = 0x0040, /* 'yield' operator */
738 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000739};
Guido van Rossum374a9221991-04-04 10:40:29 +0000740
Collin Winter828f04a2007-08-31 00:04:24 +0000741static enum why_code do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000742static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000743
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +0000744/* Records whether tracing is on for any thread. Counts the number of
745 threads for which tstate->c_tracefunc is non-NULL, so if the value
746 is 0, we know we don't have to check this thread's c_tracefunc.
747 This speeds up the if statement in PyEval_EvalFrameEx() after
748 fast_next_opcode*/
749static int _Py_TracingPossible = 0;
750
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000751
Guido van Rossum374a9221991-04-04 10:40:29 +0000752
Guido van Rossumb209a111997-04-29 18:18:01 +0000753PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000754PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000755{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 return PyEval_EvalCodeEx(co,
757 globals, locals,
758 (PyObject **)NULL, 0,
759 (PyObject **)NULL, 0,
760 (PyObject **)NULL, 0,
761 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000762}
763
764
765/* Interpreter main loop */
766
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000767PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000768PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 /* This is for backward compatibility with extension modules that
770 used this API; core interpreter code should call
771 PyEval_EvalFrameEx() */
772 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000773}
774
775PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000776PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000777{
Guido van Rossum950361c1997-01-24 13:49:28 +0000778#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000780#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 register PyObject **stack_pointer; /* Next free slot in value stack */
782 register unsigned char *next_instr;
783 register int opcode; /* Current opcode */
784 register int oparg; /* Current opcode argument, if any */
785 register enum why_code why; /* Reason for block stack unwind */
786 register int err; /* Error status -- nonzero if error */
787 register PyObject *x; /* Result object -- NULL if error */
788 register PyObject *v; /* Temporary objects popped off stack */
789 register PyObject *w;
790 register PyObject *u;
791 register PyObject *t;
792 register PyObject **fastlocals, **freevars;
793 PyObject *retval = NULL; /* Return value */
794 PyThreadState *tstate = PyThreadState_GET();
795 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000796
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000798
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000800
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 is true when the line being executed has changed. The
802 initial values are such as to make this false the first
803 time it is tested. */
804 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000805
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 unsigned char *first_instr;
807 PyObject *names;
808 PyObject *consts;
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000809#if defined(Py_DEBUG) || defined(LLTRACE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 /* Make it easier to find out where we are with a debugger */
811 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000812#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000813
Antoine Pitroub52ec782009-01-25 16:34:23 +0000814/* Computed GOTOs, or
815 the-optimization-commonly-but-improperly-known-as-"threaded code"
816 using gcc's labels-as-values extension
817 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
818
819 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000821 combined with a lookup table of jump addresses. However, since the
822 indirect jump instruction is shared by all opcodes, the CPU will have a
823 hard time making the right prediction for where to jump next (actually,
824 it will be always wrong except in the uncommon case of a sequence of
825 several identical opcodes).
826
827 "Threaded code" in contrast, uses an explicit jump table and an explicit
828 indirect jump instruction at the end of each opcode. Since the jump
829 instruction is at a different address for each opcode, the CPU will make a
830 separate prediction for each of these instructions, which is equivalent to
831 predicting the second opcode of each opcode pair. These predictions have
832 a much better chance to turn out valid, especially in small bytecode loops.
833
834 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000835 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000836 and potentially many more instructions (depending on the pipeline width).
837 A correctly predicted branch, however, is nearly free.
838
839 At the time of this writing, the "threaded code" version is up to 15-20%
840 faster than the normal "switch" version, depending on the compiler and the
841 CPU architecture.
842
843 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
844 because it would render the measurements invalid.
845
846
847 NOTE: care must be taken that the compiler doesn't try to "optimize" the
848 indirect jumps by sharing them between all opcodes. Such optimizations
849 can be disabled on gcc by using the -fno-gcse flag (or possibly
850 -fno-crossjumping).
851*/
852
Antoine Pitrou042b1282010-08-13 21:15:58 +0000853#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000854#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000855#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000856#endif
857
Antoine Pitrou042b1282010-08-13 21:15:58 +0000858#ifdef HAVE_COMPUTED_GOTOS
859 #ifndef USE_COMPUTED_GOTOS
860 #define USE_COMPUTED_GOTOS 1
861 #endif
862#else
863 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
864 #error "Computed gotos are not supported on this compiler."
865 #endif
866 #undef USE_COMPUTED_GOTOS
867 #define USE_COMPUTED_GOTOS 0
868#endif
869
870#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000871/* Import the static jump table */
872#include "opcode_targets.h"
873
874/* This macro is used when several opcodes defer to the same implementation
875 (e.g. SETUP_LOOP, SETUP_FINALLY) */
876#define TARGET_WITH_IMPL(op, impl) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 TARGET_##op: \
878 opcode = op; \
879 if (HAS_ARG(op)) \
880 oparg = NEXTARG(); \
881 case op: \
882 goto impl; \
Antoine Pitroub52ec782009-01-25 16:34:23 +0000883
884#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 TARGET_##op: \
886 opcode = op; \
887 if (HAS_ARG(op)) \
888 oparg = NEXTARG(); \
889 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000890
891
892#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 { \
894 if (!_Py_atomic_load_relaxed(&eval_breaker)) { \
895 FAST_DISPATCH(); \
896 } \
897 continue; \
898 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000899
900#ifdef LLTRACE
901#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 { \
903 if (!lltrace && !_Py_TracingPossible) { \
904 f->f_lasti = INSTR_OFFSET(); \
905 goto *opcode_targets[*next_instr++]; \
906 } \
907 goto fast_next_opcode; \
908 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000909#else
910#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 { \
912 if (!_Py_TracingPossible) { \
913 f->f_lasti = INSTR_OFFSET(); \
914 goto *opcode_targets[*next_instr++]; \
915 } \
916 goto fast_next_opcode; \
917 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000918#endif
919
920#else
921#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000923#define TARGET_WITH_IMPL(op, impl) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 /* silence compiler warnings about `impl` unused */ \
925 if (0) goto impl; \
926 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000927#define DISPATCH() continue
928#define FAST_DISPATCH() goto fast_next_opcode
929#endif
930
931
Neal Norwitza81d2202002-07-14 00:27:26 +0000932/* Tuple access macros */
933
934#ifndef Py_DEBUG
935#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
936#else
937#define GETITEM(v, i) PyTuple_GetItem((v), (i))
938#endif
939
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000940#ifdef WITH_TSC
941/* Use Pentium timestamp counter to mark certain events:
942 inst0 -- beginning of switch statement for opcode dispatch
943 inst1 -- end of switch statement (may be skipped)
944 loop0 -- the top of the mainloop
Thomas Wouters477c8d52006-05-27 19:21:47 +0000945 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000946 (may be skipped)
947 intr1 -- beginning of long interruption
948 intr2 -- end of long interruption
949
950 Many opcodes call out to helper C functions. In some cases, the
951 time in those functions should be counted towards the time for the
952 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
953 calls another Python function; there's no point in charge all the
954 bytecode executed by the called function to the caller.
955
956 It's hard to make a useful judgement statically. In the presence
957 of operator overloading, it's impossible to tell if a call will
958 execute new Python code or not.
959
960 It's a case-by-case judgement. I'll use intr1 for the following
961 cases:
962
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000963 IMPORT_STAR
964 IMPORT_FROM
965 CALL_FUNCTION (and friends)
966
967 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
969 int ticked = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000970
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 READ_TIMESTAMP(inst0);
972 READ_TIMESTAMP(inst1);
973 READ_TIMESTAMP(loop0);
974 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000975
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 /* shut up the compiler */
977 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000978#endif
979
Guido van Rossum374a9221991-04-04 10:40:29 +0000980/* Code access macros */
981
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982#define INSTR_OFFSET() ((int)(next_instr - first_instr))
983#define NEXTOP() (*next_instr++)
984#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
985#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
986#define JUMPTO(x) (next_instr = first_instr + (x))
987#define JUMPBY(x) (next_instr += (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000988
Raymond Hettingerf606f872003-03-16 03:11:04 +0000989/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 Some opcodes tend to come in pairs thus making it possible to
991 predict the second code when the first is run. For example,
992 COMPARE_OP is often followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And,
993 those opcodes are often followed by a POP_TOP.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 Verifying the prediction costs a single high-speed test of a register
996 variable against a constant. If the pairing was good, then the
997 processor's own internal branch predication has a high likelihood of
998 success, resulting in a nearly zero-overhead transition to the
999 next opcode. A successful prediction saves a trip through the eval-loop
1000 including its two unpredictable branches, the HAS_ARG test and the
1001 switch-case. Combined with the processor's internal branch prediction,
1002 a successful PREDICT has the effect of making the two opcodes run as if
1003 they were a single new opcode with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001004
Georg Brandl86b2fb92008-07-16 03:43:04 +00001005 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 predictions turned-on and interpret the results as if some opcodes
1007 had been combined or turn-off predictions so that the opcode frequency
1008 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001009
1010 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 the CPU to record separate branch prediction information for each
1012 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001013
Raymond Hettingerf606f872003-03-16 03:11:04 +00001014*/
1015
Antoine Pitrou042b1282010-08-13 21:15:58 +00001016#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017#define PREDICT(op) if (0) goto PRED_##op
1018#define PREDICTED(op) PRED_##op:
1019#define PREDICTED_WITH_ARG(op) PRED_##op:
Raymond Hettingera7216982004-02-08 19:59:27 +00001020#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021#define PREDICT(op) if (*next_instr == op) goto PRED_##op
1022#define PREDICTED(op) PRED_##op: next_instr++
1023#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Antoine Pitroub52ec782009-01-25 16:34:23 +00001024#endif
1025
Raymond Hettingerf606f872003-03-16 03:11:04 +00001026
Guido van Rossum374a9221991-04-04 10:40:29 +00001027/* Stack manipulation macros */
1028
Martin v. Löwis18e16552006-02-15 17:27:45 +00001029/* The stack can grow at most MAXINT deep, as co_nlocals and
1030 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001031#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1032#define EMPTY() (STACK_LEVEL() == 0)
1033#define TOP() (stack_pointer[-1])
1034#define SECOND() (stack_pointer[-2])
1035#define THIRD() (stack_pointer[-3])
1036#define FOURTH() (stack_pointer[-4])
1037#define PEEK(n) (stack_pointer[-(n)])
1038#define SET_TOP(v) (stack_pointer[-1] = (v))
1039#define SET_SECOND(v) (stack_pointer[-2] = (v))
1040#define SET_THIRD(v) (stack_pointer[-3] = (v))
1041#define SET_FOURTH(v) (stack_pointer[-4] = (v))
1042#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
1043#define BASIC_STACKADJ(n) (stack_pointer += n)
1044#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1045#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001046
Guido van Rossum96a42c81992-01-12 02:29:51 +00001047#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001049 lltrace && prtrace(TOP(), "push")); \
1050 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001051#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001052 BASIC_POP())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001054 lltrace && prtrace(TOP(), "stackadj")); \
1055 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +00001056#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +00001057 prtrace((STACK_POINTER)[-1], "ext_pop")), \
1058 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001059#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001060#define PUSH(v) BASIC_PUSH(v)
1061#define POP() BASIC_POP()
1062#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001063#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001064#endif
1065
Guido van Rossum681d79a1995-07-18 14:51:37 +00001066/* Local variable macros */
1067
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001069
1070/* The SETLOCAL() macro must not DECREF the local variable in-place and
1071 then store the new value; it must copy the old value to a temporary
1072 value, then store the new value, and then DECREF the temporary value.
1073 This is because it is possible that during the DECREF the frame is
1074 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1075 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001076#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001077 GETLOCAL(i) = value; \
1078 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001079
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001080
1081#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 while (STACK_LEVEL() > (b)->b_level) { \
1083 PyObject *v = POP(); \
1084 Py_XDECREF(v); \
1085 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001086
1087#define UNWIND_EXCEPT_HANDLER(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 { \
1089 PyObject *type, *value, *traceback; \
1090 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1091 while (STACK_LEVEL() > (b)->b_level + 3) { \
1092 value = POP(); \
1093 Py_XDECREF(value); \
1094 } \
1095 type = tstate->exc_type; \
1096 value = tstate->exc_value; \
1097 traceback = tstate->exc_traceback; \
1098 tstate->exc_type = POP(); \
1099 tstate->exc_value = POP(); \
1100 tstate->exc_traceback = POP(); \
1101 Py_XDECREF(type); \
1102 Py_XDECREF(value); \
1103 Py_XDECREF(traceback); \
1104 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001105
1106#define SAVE_EXC_STATE() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 { \
1108 PyObject *type, *value, *traceback; \
1109 Py_XINCREF(tstate->exc_type); \
1110 Py_XINCREF(tstate->exc_value); \
1111 Py_XINCREF(tstate->exc_traceback); \
1112 type = f->f_exc_type; \
1113 value = f->f_exc_value; \
1114 traceback = f->f_exc_traceback; \
1115 f->f_exc_type = tstate->exc_type; \
1116 f->f_exc_value = tstate->exc_value; \
1117 f->f_exc_traceback = tstate->exc_traceback; \
1118 Py_XDECREF(type); \
1119 Py_XDECREF(value); \
1120 Py_XDECREF(traceback); \
1121 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001122
1123#define SWAP_EXC_STATE() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001124 { \
1125 PyObject *tmp; \
1126 tmp = tstate->exc_type; \
1127 tstate->exc_type = f->f_exc_type; \
1128 f->f_exc_type = tmp; \
1129 tmp = tstate->exc_value; \
1130 tstate->exc_value = f->f_exc_value; \
1131 f->f_exc_value = tmp; \
1132 tmp = tstate->exc_traceback; \
1133 tstate->exc_traceback = f->f_exc_traceback; \
1134 f->f_exc_traceback = tmp; \
1135 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001136
Guido van Rossuma027efa1997-05-05 20:56:21 +00001137/* Start of code */
1138
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 if (f == NULL)
1140 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001141
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142 /* push frame */
1143 if (Py_EnterRecursiveCall(""))
1144 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001147
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001148 if (tstate->use_tracing) {
1149 if (tstate->c_tracefunc != NULL) {
1150 /* tstate->c_tracefunc, if defined, is a
1151 function that will be called on *every* entry
1152 to a code block. Its return value, if not
1153 None, is a function that will be called at
1154 the start of each executed line of code.
1155 (Actually, the function must return itself
1156 in order to continue tracing.) The trace
1157 functions are called with three arguments:
1158 a pointer to the current frame, a string
1159 indicating why the function is called, and
1160 an argument which depends on the situation.
1161 The global trace function is also called
1162 whenever an exception is detected. */
1163 if (call_trace_protected(tstate->c_tracefunc,
1164 tstate->c_traceobj,
1165 f, PyTrace_CALL, Py_None)) {
1166 /* Trace function raised an error */
1167 goto exit_eval_frame;
1168 }
1169 }
1170 if (tstate->c_profilefunc != NULL) {
1171 /* Similar for c_profilefunc, except it needn't
1172 return itself and isn't called for "line" events */
1173 if (call_trace_protected(tstate->c_profilefunc,
1174 tstate->c_profileobj,
1175 f, PyTrace_CALL, Py_None)) {
1176 /* Profile function raised an error */
1177 goto exit_eval_frame;
1178 }
1179 }
1180 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001181
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001182 co = f->f_code;
1183 names = co->co_names;
1184 consts = co->co_consts;
1185 fastlocals = f->f_localsplus;
1186 freevars = f->f_localsplus + co->co_nlocals;
1187 first_instr = (unsigned char*) PyBytes_AS_STRING(co->co_code);
1188 /* An explanation is in order for the next line.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001189
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 f->f_lasti now refers to the index of the last instruction
1191 executed. You might think this was obvious from the name, but
1192 this wasn't always true before 2.3! PyFrame_New now sets
1193 f->f_lasti to -1 (i.e. the index *before* the first instruction)
1194 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
1195 does work. Promise.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001196
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001197 When the PREDICT() macros are enabled, some opcode pairs follow in
1198 direct succession without updating f->f_lasti. A successful
1199 prediction effectively links the two codes together as if they
1200 were a single new opcode; accordingly,f->f_lasti will point to
1201 the first code in the pair (for instance, GET_ITER followed by
1202 FOR_ITER is effectively a single opcode and f->f_lasti will point
1203 at to the beginning of the combined pair.)
1204 */
1205 next_instr = first_instr + f->f_lasti + 1;
1206 stack_pointer = f->f_stacktop;
1207 assert(stack_pointer != NULL);
1208 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001209
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 if (co->co_flags & CO_GENERATOR && !throwflag) {
1211 if (f->f_exc_type != NULL && f->f_exc_type != Py_None) {
1212 /* We were in an except handler when we left,
1213 restore the exception state which was put aside
1214 (see YIELD_VALUE). */
1215 SWAP_EXC_STATE();
1216 }
1217 else {
1218 SAVE_EXC_STATE();
1219 }
1220 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001221
Tim Peters5ca576e2001-06-18 22:08:13 +00001222#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001223 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001224#endif
Neal Norwitz5f5153e2005-10-21 04:28:38 +00001225#if defined(Py_DEBUG) || defined(LLTRACE)
Victor Stinner4a3733d2010-08-17 00:39:57 +00001226 {
1227 PyObject *error_type, *error_value, *error_traceback;
1228 PyErr_Fetch(&error_type, &error_value, &error_traceback);
1229 filename = _PyUnicode_AsString(co->co_filename);
1230 PyErr_Restore(error_type, error_value, error_traceback);
1231 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001232#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001233
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 why = WHY_NOT;
1235 err = 0;
1236 x = Py_None; /* Not a reference, just anything non-NULL */
1237 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00001238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 if (throwflag) { /* support for generator.throw() */
1240 why = WHY_EXCEPTION;
1241 goto on_error;
1242 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001243
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001245#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001246 if (inst1 == 0) {
1247 /* Almost surely, the opcode executed a break
1248 or a continue, preventing inst1 from being set
1249 on the way out of the loop.
1250 */
1251 READ_TIMESTAMP(inst1);
1252 loop1 = inst1;
1253 }
1254 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
1255 intr0, intr1);
1256 ticked = 0;
1257 inst1 = 0;
1258 intr0 = 0;
1259 intr1 = 0;
1260 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001261#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001262 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1263 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 /* Do periodic things. Doing this every time through
1266 the loop would add too much overhead, so we do it
1267 only every Nth instruction. We also do it if
1268 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1269 event needs attention (e.g. a signal handler or
1270 async I/O handler); see Py_AddPendingCall() and
1271 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001273 if (_Py_atomic_load_relaxed(&eval_breaker)) {
1274 if (*next_instr == SETUP_FINALLY) {
1275 /* Make the last opcode before
1276 a try: finally: block uninterruptable. */
1277 goto fast_next_opcode;
1278 }
1279 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001280#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 ticked = 1;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001282#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) {
1284 if (Py_MakePendingCalls() < 0) {
1285 why = WHY_EXCEPTION;
1286 goto on_error;
1287 }
1288 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001289#ifdef WITH_THREAD
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001290 if (_Py_atomic_load_relaxed(&gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 /* Give another thread a chance */
1292 if (PyThreadState_Swap(NULL) != tstate)
1293 Py_FatalError("ceval: tstate mix-up");
1294 drop_gil(tstate);
1295
1296 /* Other threads may run now */
1297
1298 take_gil(tstate);
1299 if (PyThreadState_Swap(tstate) != NULL)
1300 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001301 }
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001302#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 /* Check for asynchronous exceptions. */
1304 if (tstate->async_exc != NULL) {
1305 x = tstate->async_exc;
1306 tstate->async_exc = NULL;
1307 UNSIGNAL_ASYNC_EXC();
1308 PyErr_SetNone(x);
1309 Py_DECREF(x);
1310 why = WHY_EXCEPTION;
1311 goto on_error;
1312 }
1313 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 fast_next_opcode:
1316 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001320 if (_Py_TracingPossible &&
1321 tstate->c_tracefunc != NULL && !tstate->tracing) {
1322 /* see maybe_call_line_trace
1323 for expository comments */
1324 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001326 err = maybe_call_line_trace(tstate->c_tracefunc,
1327 tstate->c_traceobj,
1328 f, &instr_lb, &instr_ub,
1329 &instr_prev);
1330 /* Reload possibly changed frame fields */
1331 JUMPTO(f->f_lasti);
1332 if (f->f_stacktop != NULL) {
1333 stack_pointer = f->f_stacktop;
1334 f->f_stacktop = NULL;
1335 }
1336 if (err) {
1337 /* trace function raised an exception */
1338 goto on_error;
1339 }
1340 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001341
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001344 opcode = NEXTOP();
1345 oparg = 0; /* allows oparg to be stored in a register because
1346 it doesn't have to be remembered across a full loop */
1347 if (HAS_ARG(opcode))
1348 oparg = NEXTARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001349 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001350#ifdef DYNAMIC_EXECUTION_PROFILE
1351#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 dxpairs[lastopcode][opcode]++;
1353 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001354#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001355 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001356#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001357
Guido van Rossum96a42c81992-01-12 02:29:51 +00001358#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001359 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001361 if (lltrace) {
1362 if (HAS_ARG(opcode)) {
1363 printf("%d: %d, %d\n",
1364 f->f_lasti, opcode, oparg);
1365 }
1366 else {
1367 printf("%d: %d\n",
1368 f->f_lasti, opcode);
1369 }
1370 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001371#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 /* Main switch on opcode */
1374 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001377
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 /* BEWARE!
1379 It is essential that any operation that fails sets either
1380 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1381 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 TARGET(NOP)
1386 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 TARGET(LOAD_FAST)
1389 x = GETLOCAL(oparg);
1390 if (x != NULL) {
1391 Py_INCREF(x);
1392 PUSH(x);
1393 FAST_DISPATCH();
1394 }
1395 format_exc_check_arg(PyExc_UnboundLocalError,
1396 UNBOUNDLOCAL_ERROR_MSG,
1397 PyTuple_GetItem(co->co_varnames, oparg));
1398 break;
Neil Schemenauer63543862002-02-17 19:10:14 +00001399
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 TARGET(LOAD_CONST)
1401 x = GETITEM(consts, oparg);
1402 Py_INCREF(x);
1403 PUSH(x);
1404 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001405
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001406 PREDICTED_WITH_ARG(STORE_FAST);
1407 TARGET(STORE_FAST)
1408 v = POP();
1409 SETLOCAL(oparg, v);
1410 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 TARGET(POP_TOP)
1413 v = POP();
1414 Py_DECREF(v);
1415 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001416
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001417 TARGET(ROT_TWO)
1418 v = TOP();
1419 w = SECOND();
1420 SET_TOP(w);
1421 SET_SECOND(v);
1422 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001423
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001424 TARGET(ROT_THREE)
1425 v = TOP();
1426 w = SECOND();
1427 x = THIRD();
1428 SET_TOP(w);
1429 SET_SECOND(x);
1430 SET_THIRD(v);
1431 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001432
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001433 TARGET(DUP_TOP)
1434 v = TOP();
1435 Py_INCREF(v);
1436 PUSH(v);
1437 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001438
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001439 TARGET(DUP_TOP_TWO)
1440 x = TOP();
1441 Py_INCREF(x);
1442 w = SECOND();
1443 Py_INCREF(w);
1444 STACKADJ(2);
1445 SET_TOP(x);
1446 SET_SECOND(w);
1447 FAST_DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001449 TARGET(UNARY_POSITIVE)
1450 v = TOP();
1451 x = PyNumber_Positive(v);
1452 Py_DECREF(v);
1453 SET_TOP(x);
1454 if (x != NULL) DISPATCH();
1455 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001457 TARGET(UNARY_NEGATIVE)
1458 v = TOP();
1459 x = PyNumber_Negative(v);
1460 Py_DECREF(v);
1461 SET_TOP(x);
1462 if (x != NULL) DISPATCH();
1463 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001465 TARGET(UNARY_NOT)
1466 v = TOP();
1467 err = PyObject_IsTrue(v);
1468 Py_DECREF(v);
1469 if (err == 0) {
1470 Py_INCREF(Py_True);
1471 SET_TOP(Py_True);
1472 DISPATCH();
1473 }
1474 else if (err > 0) {
1475 Py_INCREF(Py_False);
1476 SET_TOP(Py_False);
1477 err = 0;
1478 DISPATCH();
1479 }
1480 STACKADJ(-1);
1481 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001482
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001483 TARGET(UNARY_INVERT)
1484 v = TOP();
1485 x = PyNumber_Invert(v);
1486 Py_DECREF(v);
1487 SET_TOP(x);
1488 if (x != NULL) DISPATCH();
1489 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001490
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001491 TARGET(BINARY_POWER)
1492 w = POP();
1493 v = TOP();
1494 x = PyNumber_Power(v, w, Py_None);
1495 Py_DECREF(v);
1496 Py_DECREF(w);
1497 SET_TOP(x);
1498 if (x != NULL) DISPATCH();
1499 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001500
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001501 TARGET(BINARY_MULTIPLY)
1502 w = POP();
1503 v = TOP();
1504 x = PyNumber_Multiply(v, w);
1505 Py_DECREF(v);
1506 Py_DECREF(w);
1507 SET_TOP(x);
1508 if (x != NULL) DISPATCH();
1509 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001510
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001511 TARGET(BINARY_TRUE_DIVIDE)
1512 w = POP();
1513 v = TOP();
1514 x = PyNumber_TrueDivide(v, w);
1515 Py_DECREF(v);
1516 Py_DECREF(w);
1517 SET_TOP(x);
1518 if (x != NULL) DISPATCH();
1519 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001520
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001521 TARGET(BINARY_FLOOR_DIVIDE)
1522 w = POP();
1523 v = TOP();
1524 x = PyNumber_FloorDivide(v, w);
1525 Py_DECREF(v);
1526 Py_DECREF(w);
1527 SET_TOP(x);
1528 if (x != NULL) DISPATCH();
1529 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001530
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001531 TARGET(BINARY_MODULO)
1532 w = POP();
1533 v = TOP();
1534 if (PyUnicode_CheckExact(v))
1535 x = PyUnicode_Format(v, w);
1536 else
1537 x = PyNumber_Remainder(v, w);
1538 Py_DECREF(v);
1539 Py_DECREF(w);
1540 SET_TOP(x);
1541 if (x != NULL) DISPATCH();
1542 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001543
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001544 TARGET(BINARY_ADD)
1545 w = POP();
1546 v = TOP();
1547 if (PyUnicode_CheckExact(v) &&
1548 PyUnicode_CheckExact(w)) {
1549 x = unicode_concatenate(v, w, f, next_instr);
1550 /* unicode_concatenate consumed the ref to v */
1551 goto skip_decref_vx;
1552 }
1553 else {
1554 x = PyNumber_Add(v, w);
1555 }
1556 Py_DECREF(v);
1557 skip_decref_vx:
1558 Py_DECREF(w);
1559 SET_TOP(x);
1560 if (x != NULL) DISPATCH();
1561 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 TARGET(BINARY_SUBTRACT)
1564 w = POP();
1565 v = TOP();
1566 x = PyNumber_Subtract(v, w);
1567 Py_DECREF(v);
1568 Py_DECREF(w);
1569 SET_TOP(x);
1570 if (x != NULL) DISPATCH();
1571 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001573 TARGET(BINARY_SUBSCR)
1574 w = POP();
1575 v = TOP();
1576 x = PyObject_GetItem(v, w);
1577 Py_DECREF(v);
1578 Py_DECREF(w);
1579 SET_TOP(x);
1580 if (x != NULL) DISPATCH();
1581 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001582
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583 TARGET(BINARY_LSHIFT)
1584 w = POP();
1585 v = TOP();
1586 x = PyNumber_Lshift(v, w);
1587 Py_DECREF(v);
1588 Py_DECREF(w);
1589 SET_TOP(x);
1590 if (x != NULL) DISPATCH();
1591 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001592
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 TARGET(BINARY_RSHIFT)
1594 w = POP();
1595 v = TOP();
1596 x = PyNumber_Rshift(v, w);
1597 Py_DECREF(v);
1598 Py_DECREF(w);
1599 SET_TOP(x);
1600 if (x != NULL) DISPATCH();
1601 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 TARGET(BINARY_AND)
1604 w = POP();
1605 v = TOP();
1606 x = PyNumber_And(v, w);
1607 Py_DECREF(v);
1608 Py_DECREF(w);
1609 SET_TOP(x);
1610 if (x != NULL) DISPATCH();
1611 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001612
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613 TARGET(BINARY_XOR)
1614 w = POP();
1615 v = TOP();
1616 x = PyNumber_Xor(v, w);
1617 Py_DECREF(v);
1618 Py_DECREF(w);
1619 SET_TOP(x);
1620 if (x != NULL) DISPATCH();
1621 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001622
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001623 TARGET(BINARY_OR)
1624 w = POP();
1625 v = TOP();
1626 x = PyNumber_Or(v, w);
1627 Py_DECREF(v);
1628 Py_DECREF(w);
1629 SET_TOP(x);
1630 if (x != NULL) DISPATCH();
1631 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001632
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001633 TARGET(LIST_APPEND)
1634 w = POP();
1635 v = PEEK(oparg);
1636 err = PyList_Append(v, w);
1637 Py_DECREF(w);
1638 if (err == 0) {
1639 PREDICT(JUMP_ABSOLUTE);
1640 DISPATCH();
1641 }
1642 break;
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001643
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001644 TARGET(SET_ADD)
1645 w = POP();
1646 v = stack_pointer[-oparg];
1647 err = PySet_Add(v, w);
1648 Py_DECREF(w);
1649 if (err == 0) {
1650 PREDICT(JUMP_ABSOLUTE);
1651 DISPATCH();
1652 }
1653 break;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001655 TARGET(INPLACE_POWER)
1656 w = POP();
1657 v = TOP();
1658 x = PyNumber_InPlacePower(v, w, Py_None);
1659 Py_DECREF(v);
1660 Py_DECREF(w);
1661 SET_TOP(x);
1662 if (x != NULL) DISPATCH();
1663 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001664
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001665 TARGET(INPLACE_MULTIPLY)
1666 w = POP();
1667 v = TOP();
1668 x = PyNumber_InPlaceMultiply(v, w);
1669 Py_DECREF(v);
1670 Py_DECREF(w);
1671 SET_TOP(x);
1672 if (x != NULL) DISPATCH();
1673 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001674
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001675 TARGET(INPLACE_TRUE_DIVIDE)
1676 w = POP();
1677 v = TOP();
1678 x = PyNumber_InPlaceTrueDivide(v, w);
1679 Py_DECREF(v);
1680 Py_DECREF(w);
1681 SET_TOP(x);
1682 if (x != NULL) DISPATCH();
1683 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001684
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001685 TARGET(INPLACE_FLOOR_DIVIDE)
1686 w = POP();
1687 v = TOP();
1688 x = PyNumber_InPlaceFloorDivide(v, w);
1689 Py_DECREF(v);
1690 Py_DECREF(w);
1691 SET_TOP(x);
1692 if (x != NULL) DISPATCH();
1693 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001694
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001695 TARGET(INPLACE_MODULO)
1696 w = POP();
1697 v = TOP();
1698 x = PyNumber_InPlaceRemainder(v, w);
1699 Py_DECREF(v);
1700 Py_DECREF(w);
1701 SET_TOP(x);
1702 if (x != NULL) DISPATCH();
1703 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001704
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001705 TARGET(INPLACE_ADD)
1706 w = POP();
1707 v = TOP();
1708 if (PyUnicode_CheckExact(v) &&
1709 PyUnicode_CheckExact(w)) {
1710 x = unicode_concatenate(v, w, f, next_instr);
1711 /* unicode_concatenate consumed the ref to v */
1712 goto skip_decref_v;
1713 }
1714 else {
1715 x = PyNumber_InPlaceAdd(v, w);
1716 }
1717 Py_DECREF(v);
1718 skip_decref_v:
1719 Py_DECREF(w);
1720 SET_TOP(x);
1721 if (x != NULL) DISPATCH();
1722 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001723
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001724 TARGET(INPLACE_SUBTRACT)
1725 w = POP();
1726 v = TOP();
1727 x = PyNumber_InPlaceSubtract(v, w);
1728 Py_DECREF(v);
1729 Py_DECREF(w);
1730 SET_TOP(x);
1731 if (x != NULL) DISPATCH();
1732 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001733
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001734 TARGET(INPLACE_LSHIFT)
1735 w = POP();
1736 v = TOP();
1737 x = PyNumber_InPlaceLshift(v, w);
1738 Py_DECREF(v);
1739 Py_DECREF(w);
1740 SET_TOP(x);
1741 if (x != NULL) DISPATCH();
1742 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001743
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001744 TARGET(INPLACE_RSHIFT)
1745 w = POP();
1746 v = TOP();
1747 x = PyNumber_InPlaceRshift(v, w);
1748 Py_DECREF(v);
1749 Py_DECREF(w);
1750 SET_TOP(x);
1751 if (x != NULL) DISPATCH();
1752 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001753
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001754 TARGET(INPLACE_AND)
1755 w = POP();
1756 v = TOP();
1757 x = PyNumber_InPlaceAnd(v, w);
1758 Py_DECREF(v);
1759 Py_DECREF(w);
1760 SET_TOP(x);
1761 if (x != NULL) DISPATCH();
1762 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001763
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001764 TARGET(INPLACE_XOR)
1765 w = POP();
1766 v = TOP();
1767 x = PyNumber_InPlaceXor(v, w);
1768 Py_DECREF(v);
1769 Py_DECREF(w);
1770 SET_TOP(x);
1771 if (x != NULL) DISPATCH();
1772 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001773
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001774 TARGET(INPLACE_OR)
1775 w = POP();
1776 v = TOP();
1777 x = PyNumber_InPlaceOr(v, w);
1778 Py_DECREF(v);
1779 Py_DECREF(w);
1780 SET_TOP(x);
1781 if (x != NULL) DISPATCH();
1782 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001783
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001784 TARGET(STORE_SUBSCR)
1785 w = TOP();
1786 v = SECOND();
1787 u = THIRD();
1788 STACKADJ(-3);
1789 /* v[w] = u */
1790 err = PyObject_SetItem(v, w, u);
1791 Py_DECREF(u);
1792 Py_DECREF(v);
1793 Py_DECREF(w);
1794 if (err == 0) DISPATCH();
1795 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001796
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001797 TARGET(DELETE_SUBSCR)
1798 w = TOP();
1799 v = SECOND();
1800 STACKADJ(-2);
1801 /* del v[w] */
1802 err = PyObject_DelItem(v, w);
1803 Py_DECREF(v);
1804 Py_DECREF(w);
1805 if (err == 0) DISPATCH();
1806 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001808 TARGET(PRINT_EXPR)
1809 v = POP();
1810 w = PySys_GetObject("displayhook");
1811 if (w == NULL) {
1812 PyErr_SetString(PyExc_RuntimeError,
1813 "lost sys.displayhook");
1814 err = -1;
1815 x = NULL;
1816 }
1817 if (err == 0) {
1818 x = PyTuple_Pack(1, v);
1819 if (x == NULL)
1820 err = -1;
1821 }
1822 if (err == 0) {
1823 w = PyEval_CallObject(w, x);
1824 Py_XDECREF(w);
1825 if (w == NULL)
1826 err = -1;
1827 }
1828 Py_DECREF(v);
1829 Py_XDECREF(x);
1830 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001831
Thomas Wouters434d0822000-08-24 20:11:32 +00001832#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001833 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001834#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001835 TARGET(RAISE_VARARGS)
1836 v = w = NULL;
1837 switch (oparg) {
1838 case 2:
1839 v = POP(); /* cause */
1840 case 1:
1841 w = POP(); /* exc */
1842 case 0: /* Fallthrough */
1843 why = do_raise(w, v);
1844 break;
1845 default:
1846 PyErr_SetString(PyExc_SystemError,
1847 "bad RAISE_VARARGS oparg");
1848 why = WHY_EXCEPTION;
1849 break;
1850 }
1851 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001852
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001853 TARGET(STORE_LOCALS)
1854 x = POP();
1855 v = f->f_locals;
1856 Py_XDECREF(v);
1857 f->f_locals = x;
1858 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001859
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001860 TARGET(RETURN_VALUE)
1861 retval = POP();
1862 why = WHY_RETURN;
1863 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001864
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001865 TARGET(YIELD_VALUE)
1866 retval = POP();
1867 f->f_stacktop = stack_pointer;
1868 why = WHY_YIELD;
1869 /* Put aside the current exception state and restore
1870 that of the calling frame. This only serves when
1871 "yield" is used inside an except handler. */
1872 SWAP_EXC_STATE();
1873 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001874
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001875 TARGET(POP_EXCEPT)
1876 {
1877 PyTryBlock *b = PyFrame_BlockPop(f);
1878 if (b->b_type != EXCEPT_HANDLER) {
1879 PyErr_SetString(PyExc_SystemError,
1880 "popped block is not an except handler");
1881 why = WHY_EXCEPTION;
1882 break;
1883 }
1884 UNWIND_EXCEPT_HANDLER(b);
1885 }
1886 DISPATCH();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001887
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001888 TARGET(POP_BLOCK)
1889 {
1890 PyTryBlock *b = PyFrame_BlockPop(f);
1891 UNWIND_BLOCK(b);
1892 }
1893 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001894
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001895 PREDICTED(END_FINALLY);
1896 TARGET(END_FINALLY)
1897 v = POP();
1898 if (PyLong_Check(v)) {
1899 why = (enum why_code) PyLong_AS_LONG(v);
1900 assert(why != WHY_YIELD);
1901 if (why == WHY_RETURN ||
1902 why == WHY_CONTINUE)
1903 retval = POP();
1904 if (why == WHY_SILENCED) {
1905 /* An exception was silenced by 'with', we must
1906 manually unwind the EXCEPT_HANDLER block which was
1907 created when the exception was caught, otherwise
1908 the stack will be in an inconsistent state. */
1909 PyTryBlock *b = PyFrame_BlockPop(f);
1910 assert(b->b_type == EXCEPT_HANDLER);
1911 UNWIND_EXCEPT_HANDLER(b);
1912 why = WHY_NOT;
1913 }
1914 }
1915 else if (PyExceptionClass_Check(v)) {
1916 w = POP();
1917 u = POP();
1918 PyErr_Restore(v, w, u);
1919 why = WHY_RERAISE;
1920 break;
1921 }
1922 else if (v != Py_None) {
1923 PyErr_SetString(PyExc_SystemError,
1924 "'finally' pops bad exception");
1925 why = WHY_EXCEPTION;
1926 }
1927 Py_DECREF(v);
1928 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001929
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001930 TARGET(LOAD_BUILD_CLASS)
1931 x = PyDict_GetItemString(f->f_builtins,
1932 "__build_class__");
1933 if (x == NULL) {
1934 PyErr_SetString(PyExc_ImportError,
1935 "__build_class__ not found");
1936 break;
1937 }
1938 Py_INCREF(x);
1939 PUSH(x);
1940 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001941
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001942 TARGET(STORE_NAME)
1943 w = GETITEM(names, oparg);
1944 v = POP();
1945 if ((x = f->f_locals) != NULL) {
1946 if (PyDict_CheckExact(x))
1947 err = PyDict_SetItem(x, w, v);
1948 else
1949 err = PyObject_SetItem(x, w, v);
1950 Py_DECREF(v);
1951 if (err == 0) DISPATCH();
1952 break;
1953 }
1954 PyErr_Format(PyExc_SystemError,
1955 "no locals found when storing %R", w);
1956 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001957
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001958 TARGET(DELETE_NAME)
1959 w = GETITEM(names, oparg);
1960 if ((x = f->f_locals) != NULL) {
1961 if ((err = PyObject_DelItem(x, w)) != 0)
1962 format_exc_check_arg(PyExc_NameError,
1963 NAME_ERROR_MSG,
1964 w);
1965 break;
1966 }
1967 PyErr_Format(PyExc_SystemError,
1968 "no locals when deleting %R", w);
1969 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001970
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001971 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
1972 TARGET(UNPACK_SEQUENCE)
1973 v = POP();
1974 if (PyTuple_CheckExact(v) &&
1975 PyTuple_GET_SIZE(v) == oparg) {
1976 PyObject **items = \
1977 ((PyTupleObject *)v)->ob_item;
1978 while (oparg--) {
1979 w = items[oparg];
1980 Py_INCREF(w);
1981 PUSH(w);
1982 }
1983 Py_DECREF(v);
1984 DISPATCH();
1985 } else if (PyList_CheckExact(v) &&
1986 PyList_GET_SIZE(v) == oparg) {
1987 PyObject **items = \
1988 ((PyListObject *)v)->ob_item;
1989 while (oparg--) {
1990 w = items[oparg];
1991 Py_INCREF(w);
1992 PUSH(w);
1993 }
1994 } else if (unpack_iterable(v, oparg, -1,
1995 stack_pointer + oparg)) {
1996 STACKADJ(oparg);
1997 } else {
1998 /* unpack_iterable() raised an exception */
1999 why = WHY_EXCEPTION;
2000 }
2001 Py_DECREF(v);
2002 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002003
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002004 TARGET(UNPACK_EX)
2005 {
2006 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2007 v = POP();
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002008
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002009 if (unpack_iterable(v, oparg & 0xFF, oparg >> 8,
2010 stack_pointer + totalargs)) {
2011 stack_pointer += totalargs;
2012 } else {
2013 why = WHY_EXCEPTION;
2014 }
2015 Py_DECREF(v);
2016 break;
2017 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002018
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002019 TARGET(STORE_ATTR)
2020 w = GETITEM(names, oparg);
2021 v = TOP();
2022 u = SECOND();
2023 STACKADJ(-2);
2024 err = PyObject_SetAttr(v, w, u); /* v.w = u */
2025 Py_DECREF(v);
2026 Py_DECREF(u);
2027 if (err == 0) DISPATCH();
2028 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002029
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002030 TARGET(DELETE_ATTR)
2031 w = GETITEM(names, oparg);
2032 v = POP();
2033 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
2034 /* del v.w */
2035 Py_DECREF(v);
2036 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002037
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002038 TARGET(STORE_GLOBAL)
2039 w = GETITEM(names, oparg);
2040 v = POP();
2041 err = PyDict_SetItem(f->f_globals, w, v);
2042 Py_DECREF(v);
2043 if (err == 0) DISPATCH();
2044 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002045
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002046 TARGET(DELETE_GLOBAL)
2047 w = GETITEM(names, oparg);
2048 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
2049 format_exc_check_arg(
2050 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
2051 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002053 TARGET(LOAD_NAME)
2054 w = GETITEM(names, oparg);
2055 if ((v = f->f_locals) == NULL) {
2056 PyErr_Format(PyExc_SystemError,
2057 "no locals when loading %R", w);
2058 why = WHY_EXCEPTION;
2059 break;
2060 }
2061 if (PyDict_CheckExact(v)) {
2062 x = PyDict_GetItem(v, w);
2063 Py_XINCREF(x);
2064 }
2065 else {
2066 x = PyObject_GetItem(v, w);
2067 if (x == NULL && PyErr_Occurred()) {
2068 if (!PyErr_ExceptionMatches(
2069 PyExc_KeyError))
2070 break;
2071 PyErr_Clear();
2072 }
2073 }
2074 if (x == NULL) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002075 x = PyDict_GetItem(f->f_globals, w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002076 if (x == NULL) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002077 x = PyDict_GetItem(f->f_builtins, w);
2078 if (x == NULL) {
2079 format_exc_check_arg(
2080 PyExc_NameError,
2081 NAME_ERROR_MSG, w);
2082 break;
2083 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002084 }
2085 Py_INCREF(x);
2086 }
2087 PUSH(x);
2088 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002090 TARGET(LOAD_GLOBAL)
2091 w = GETITEM(names, oparg);
2092 if (PyUnicode_CheckExact(w)) {
2093 /* Inline the PyDict_GetItem() calls.
2094 WARNING: this is an extreme speed hack.
2095 Do not try this at home. */
2096 long hash = ((PyUnicodeObject *)w)->hash;
2097 if (hash != -1) {
2098 PyDictObject *d;
2099 PyDictEntry *e;
2100 d = (PyDictObject *)(f->f_globals);
2101 e = d->ma_lookup(d, w, hash);
2102 if (e == NULL) {
2103 x = NULL;
2104 break;
2105 }
2106 x = e->me_value;
2107 if (x != NULL) {
2108 Py_INCREF(x);
2109 PUSH(x);
2110 DISPATCH();
2111 }
2112 d = (PyDictObject *)(f->f_builtins);
2113 e = d->ma_lookup(d, w, hash);
2114 if (e == NULL) {
2115 x = NULL;
2116 break;
2117 }
2118 x = e->me_value;
2119 if (x != NULL) {
2120 Py_INCREF(x);
2121 PUSH(x);
2122 DISPATCH();
2123 }
2124 goto load_global_error;
2125 }
2126 }
2127 /* This is the un-inlined version of the code above */
2128 x = PyDict_GetItem(f->f_globals, w);
2129 if (x == NULL) {
2130 x = PyDict_GetItem(f->f_builtins, w);
2131 if (x == NULL) {
2132 load_global_error:
2133 format_exc_check_arg(
2134 PyExc_NameError,
2135 GLOBAL_NAME_ERROR_MSG, w);
2136 break;
2137 }
2138 }
2139 Py_INCREF(x);
2140 PUSH(x);
2141 DISPATCH();
Guido van Rossum681d79a1995-07-18 14:51:37 +00002142
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002143 TARGET(DELETE_FAST)
2144 x = GETLOCAL(oparg);
2145 if (x != NULL) {
2146 SETLOCAL(oparg, NULL);
2147 DISPATCH();
2148 }
2149 format_exc_check_arg(
2150 PyExc_UnboundLocalError,
2151 UNBOUNDLOCAL_ERROR_MSG,
2152 PyTuple_GetItem(co->co_varnames, oparg)
2153 );
2154 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002155
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002156 TARGET(DELETE_DEREF)
2157 x = freevars[oparg];
2158 if (PyCell_GET(x) != NULL) {
2159 PyCell_Set(x, NULL);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002160 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002161 }
2162 err = -1;
2163 format_exc_unbound(co, oparg);
2164 break;
2165
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002166 TARGET(LOAD_CLOSURE)
2167 x = freevars[oparg];
2168 Py_INCREF(x);
2169 PUSH(x);
2170 if (x != NULL) DISPATCH();
2171 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002172
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002173 TARGET(LOAD_DEREF)
2174 x = freevars[oparg];
2175 w = PyCell_Get(x);
2176 if (w != NULL) {
2177 PUSH(w);
2178 DISPATCH();
2179 }
2180 err = -1;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002181 format_exc_unbound(co, oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002182 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002183
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002184 TARGET(STORE_DEREF)
2185 w = POP();
2186 x = freevars[oparg];
2187 PyCell_Set(x, w);
2188 Py_DECREF(w);
2189 DISPATCH();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002190
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002191 TARGET(BUILD_TUPLE)
2192 x = PyTuple_New(oparg);
2193 if (x != NULL) {
2194 for (; --oparg >= 0;) {
2195 w = POP();
2196 PyTuple_SET_ITEM(x, oparg, w);
2197 }
2198 PUSH(x);
2199 DISPATCH();
2200 }
2201 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002202
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002203 TARGET(BUILD_LIST)
2204 x = PyList_New(oparg);
2205 if (x != NULL) {
2206 for (; --oparg >= 0;) {
2207 w = POP();
2208 PyList_SET_ITEM(x, oparg, w);
2209 }
2210 PUSH(x);
2211 DISPATCH();
2212 }
2213 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002214
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002215 TARGET(BUILD_SET)
2216 x = PySet_New(NULL);
2217 if (x != NULL) {
2218 for (; --oparg >= 0;) {
2219 w = POP();
2220 if (err == 0)
2221 err = PySet_Add(x, w);
2222 Py_DECREF(w);
2223 }
2224 if (err != 0) {
2225 Py_DECREF(x);
2226 break;
2227 }
2228 PUSH(x);
2229 DISPATCH();
2230 }
2231 break;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002232
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002233 TARGET(BUILD_MAP)
2234 x = _PyDict_NewPresized((Py_ssize_t)oparg);
2235 PUSH(x);
2236 if (x != NULL) DISPATCH();
2237 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002239 TARGET(STORE_MAP)
2240 w = TOP(); /* key */
2241 u = SECOND(); /* value */
2242 v = THIRD(); /* dict */
2243 STACKADJ(-2);
2244 assert (PyDict_CheckExact(v));
2245 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2246 Py_DECREF(u);
2247 Py_DECREF(w);
2248 if (err == 0) DISPATCH();
2249 break;
Christian Heimes99170a52007-12-19 02:07:34 +00002250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002251 TARGET(MAP_ADD)
2252 w = TOP(); /* key */
2253 u = SECOND(); /* value */
2254 STACKADJ(-2);
2255 v = stack_pointer[-oparg]; /* dict */
2256 assert (PyDict_CheckExact(v));
2257 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2258 Py_DECREF(u);
2259 Py_DECREF(w);
2260 if (err == 0) {
2261 PREDICT(JUMP_ABSOLUTE);
2262 DISPATCH();
2263 }
2264 break;
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002265
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002266 TARGET(LOAD_ATTR)
2267 w = GETITEM(names, oparg);
2268 v = TOP();
2269 x = PyObject_GetAttr(v, w);
2270 Py_DECREF(v);
2271 SET_TOP(x);
2272 if (x != NULL) DISPATCH();
2273 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002275 TARGET(COMPARE_OP)
2276 w = POP();
2277 v = TOP();
2278 x = cmp_outcome(oparg, v, w);
2279 Py_DECREF(v);
2280 Py_DECREF(w);
2281 SET_TOP(x);
2282 if (x == NULL) break;
2283 PREDICT(POP_JUMP_IF_FALSE);
2284 PREDICT(POP_JUMP_IF_TRUE);
2285 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002287 TARGET(IMPORT_NAME)
2288 w = GETITEM(names, oparg);
2289 x = PyDict_GetItemString(f->f_builtins, "__import__");
2290 if (x == NULL) {
2291 PyErr_SetString(PyExc_ImportError,
2292 "__import__ not found");
2293 break;
2294 }
2295 Py_INCREF(x);
2296 v = POP();
2297 u = TOP();
2298 if (PyLong_AsLong(u) != -1 || PyErr_Occurred())
2299 w = PyTuple_Pack(5,
2300 w,
2301 f->f_globals,
2302 f->f_locals == NULL ?
2303 Py_None : f->f_locals,
2304 v,
2305 u);
2306 else
2307 w = PyTuple_Pack(4,
2308 w,
2309 f->f_globals,
2310 f->f_locals == NULL ?
2311 Py_None : f->f_locals,
2312 v);
2313 Py_DECREF(v);
2314 Py_DECREF(u);
2315 if (w == NULL) {
2316 u = POP();
2317 Py_DECREF(x);
2318 x = NULL;
2319 break;
2320 }
2321 READ_TIMESTAMP(intr0);
2322 v = x;
2323 x = PyEval_CallObject(v, w);
2324 Py_DECREF(v);
2325 READ_TIMESTAMP(intr1);
2326 Py_DECREF(w);
2327 SET_TOP(x);
2328 if (x != NULL) DISPATCH();
2329 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002331 TARGET(IMPORT_STAR)
2332 v = POP();
2333 PyFrame_FastToLocals(f);
2334 if ((x = f->f_locals) == NULL) {
2335 PyErr_SetString(PyExc_SystemError,
2336 "no locals found during 'import *'");
2337 break;
2338 }
2339 READ_TIMESTAMP(intr0);
2340 err = import_all_from(x, v);
2341 READ_TIMESTAMP(intr1);
2342 PyFrame_LocalsToFast(f, 0);
2343 Py_DECREF(v);
2344 if (err == 0) DISPATCH();
2345 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002347 TARGET(IMPORT_FROM)
2348 w = GETITEM(names, oparg);
2349 v = TOP();
2350 READ_TIMESTAMP(intr0);
2351 x = import_from(v, w);
2352 READ_TIMESTAMP(intr1);
2353 PUSH(x);
2354 if (x != NULL) DISPATCH();
2355 break;
Thomas Wouters52152252000-08-17 22:55:00 +00002356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002357 TARGET(JUMP_FORWARD)
2358 JUMPBY(oparg);
2359 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002361 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
2362 TARGET(POP_JUMP_IF_FALSE)
2363 w = POP();
2364 if (w == Py_True) {
2365 Py_DECREF(w);
2366 FAST_DISPATCH();
2367 }
2368 if (w == Py_False) {
2369 Py_DECREF(w);
2370 JUMPTO(oparg);
2371 FAST_DISPATCH();
2372 }
2373 err = PyObject_IsTrue(w);
2374 Py_DECREF(w);
2375 if (err > 0)
2376 err = 0;
2377 else if (err == 0)
2378 JUMPTO(oparg);
2379 else
2380 break;
2381 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002383 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
2384 TARGET(POP_JUMP_IF_TRUE)
2385 w = POP();
2386 if (w == Py_False) {
2387 Py_DECREF(w);
2388 FAST_DISPATCH();
2389 }
2390 if (w == Py_True) {
2391 Py_DECREF(w);
2392 JUMPTO(oparg);
2393 FAST_DISPATCH();
2394 }
2395 err = PyObject_IsTrue(w);
2396 Py_DECREF(w);
2397 if (err > 0) {
2398 err = 0;
2399 JUMPTO(oparg);
2400 }
2401 else if (err == 0)
2402 ;
2403 else
2404 break;
2405 DISPATCH();
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002407 TARGET(JUMP_IF_FALSE_OR_POP)
2408 w = TOP();
2409 if (w == Py_True) {
2410 STACKADJ(-1);
2411 Py_DECREF(w);
2412 FAST_DISPATCH();
2413 }
2414 if (w == Py_False) {
2415 JUMPTO(oparg);
2416 FAST_DISPATCH();
2417 }
2418 err = PyObject_IsTrue(w);
2419 if (err > 0) {
2420 STACKADJ(-1);
2421 Py_DECREF(w);
2422 err = 0;
2423 }
2424 else if (err == 0)
2425 JUMPTO(oparg);
2426 else
2427 break;
2428 DISPATCH();
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002430 TARGET(JUMP_IF_TRUE_OR_POP)
2431 w = TOP();
2432 if (w == Py_False) {
2433 STACKADJ(-1);
2434 Py_DECREF(w);
2435 FAST_DISPATCH();
2436 }
2437 if (w == Py_True) {
2438 JUMPTO(oparg);
2439 FAST_DISPATCH();
2440 }
2441 err = PyObject_IsTrue(w);
2442 if (err > 0) {
2443 err = 0;
2444 JUMPTO(oparg);
2445 }
2446 else if (err == 0) {
2447 STACKADJ(-1);
2448 Py_DECREF(w);
2449 }
2450 else
2451 break;
2452 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002454 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
2455 TARGET(JUMP_ABSOLUTE)
2456 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002457#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002458 /* Enabling this path speeds-up all while and for-loops by bypassing
2459 the per-loop checks for signals. By default, this should be turned-off
2460 because it prevents detection of a control-break in tight loops like
2461 "while 1: pass". Compile with this option turned-on when you need
2462 the speed-up and do not need break checking inside tight loops (ones
2463 that contain only instructions ending with FAST_DISPATCH).
2464 */
2465 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002466#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002467 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002468#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002470 TARGET(GET_ITER)
2471 /* before: [obj]; after [getiter(obj)] */
2472 v = TOP();
2473 x = PyObject_GetIter(v);
2474 Py_DECREF(v);
2475 if (x != NULL) {
2476 SET_TOP(x);
2477 PREDICT(FOR_ITER);
2478 DISPATCH();
2479 }
2480 STACKADJ(-1);
2481 break;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002482
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002483 PREDICTED_WITH_ARG(FOR_ITER);
2484 TARGET(FOR_ITER)
2485 /* before: [iter]; after: [iter, iter()] *or* [] */
2486 v = TOP();
2487 x = (*v->ob_type->tp_iternext)(v);
2488 if (x != NULL) {
2489 PUSH(x);
2490 PREDICT(STORE_FAST);
2491 PREDICT(UNPACK_SEQUENCE);
2492 DISPATCH();
2493 }
2494 if (PyErr_Occurred()) {
2495 if (!PyErr_ExceptionMatches(
2496 PyExc_StopIteration))
2497 break;
2498 PyErr_Clear();
2499 }
2500 /* iterator ended normally */
2501 x = v = POP();
2502 Py_DECREF(v);
2503 JUMPBY(oparg);
2504 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002506 TARGET(BREAK_LOOP)
2507 why = WHY_BREAK;
2508 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002509
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002510 TARGET(CONTINUE_LOOP)
2511 retval = PyLong_FromLong(oparg);
2512 if (!retval) {
2513 x = NULL;
2514 break;
2515 }
2516 why = WHY_CONTINUE;
2517 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002519 TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
2520 TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
2521 TARGET(SETUP_FINALLY)
2522 _setup_finally:
2523 /* NOTE: If you add any new block-setup opcodes that
2524 are not try/except/finally handlers, you may need
2525 to update the PyGen_NeedsFinalizing() function.
2526 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002527
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002528 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2529 STACK_LEVEL());
2530 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002531
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002532 TARGET(SETUP_WITH)
2533 {
2534 static PyObject *exit, *enter;
2535 w = TOP();
2536 x = special_lookup(w, "__exit__", &exit);
2537 if (!x)
2538 break;
2539 SET_TOP(x);
2540 u = special_lookup(w, "__enter__", &enter);
2541 Py_DECREF(w);
2542 if (!u) {
2543 x = NULL;
2544 break;
2545 }
2546 x = PyObject_CallFunctionObjArgs(u, NULL);
2547 Py_DECREF(u);
2548 if (!x)
2549 break;
2550 /* Setup the finally block before pushing the result
2551 of __enter__ on the stack. */
2552 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2553 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002555 PUSH(x);
2556 DISPATCH();
2557 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002558
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002559 TARGET(WITH_CLEANUP)
2560 {
2561 /* At the top of the stack are 1-3 values indicating
2562 how/why we entered the finally clause:
2563 - TOP = None
2564 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2565 - TOP = WHY_*; no retval below it
2566 - (TOP, SECOND, THIRD) = exc_info()
2567 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2568 Below them is EXIT, the context.__exit__ bound method.
2569 In the last case, we must call
2570 EXIT(TOP, SECOND, THIRD)
2571 otherwise we must call
2572 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002573
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002574 In the first two cases, we remove EXIT from the
2575 stack, leaving the rest in the same order. In the
2576 third case, we shift the bottom 3 values of the
2577 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002579 In addition, if the stack represents an exception,
2580 *and* the function call returns a 'true' value, we
2581 push WHY_SILENCED onto the stack. END_FINALLY will
2582 then not re-raise the exception. (But non-local
2583 gotos should still be resumed.)
2584 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002586 PyObject *exit_func;
2587 u = TOP();
2588 if (u == Py_None) {
2589 (void)POP();
2590 exit_func = TOP();
2591 SET_TOP(u);
2592 v = w = Py_None;
2593 }
2594 else if (PyLong_Check(u)) {
2595 (void)POP();
2596 switch(PyLong_AsLong(u)) {
2597 case WHY_RETURN:
2598 case WHY_CONTINUE:
2599 /* Retval in TOP. */
2600 exit_func = SECOND();
2601 SET_SECOND(TOP());
2602 SET_TOP(u);
2603 break;
2604 default:
2605 exit_func = TOP();
2606 SET_TOP(u);
2607 break;
2608 }
2609 u = v = w = Py_None;
2610 }
2611 else {
2612 PyObject *tp, *exc, *tb;
2613 PyTryBlock *block;
2614 v = SECOND();
2615 w = THIRD();
2616 tp = FOURTH();
2617 exc = PEEK(5);
2618 tb = PEEK(6);
2619 exit_func = PEEK(7);
2620 SET_VALUE(7, tb);
2621 SET_VALUE(6, exc);
2622 SET_VALUE(5, tp);
2623 /* UNWIND_EXCEPT_HANDLER will pop this off. */
2624 SET_FOURTH(NULL);
2625 /* We just shifted the stack down, so we have
2626 to tell the except handler block that the
2627 values are lower than it expects. */
2628 block = &f->f_blockstack[f->f_iblock - 1];
2629 assert(block->b_type == EXCEPT_HANDLER);
2630 block->b_level--;
2631 }
2632 /* XXX Not the fastest way to call it... */
2633 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2634 NULL);
2635 Py_DECREF(exit_func);
2636 if (x == NULL)
2637 break; /* Go to error exit */
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002639 if (u != Py_None)
2640 err = PyObject_IsTrue(x);
2641 else
2642 err = 0;
2643 Py_DECREF(x);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002645 if (err < 0)
2646 break; /* Go to error exit */
2647 else if (err > 0) {
2648 err = 0;
2649 /* There was an exception and a True return */
2650 PUSH(PyLong_FromLong((long) WHY_SILENCED));
2651 }
2652 PREDICT(END_FINALLY);
2653 break;
2654 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002655
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002656 TARGET(CALL_FUNCTION)
2657 {
2658 PyObject **sp;
2659 PCALL(PCALL_ALL);
2660 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002661#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002662 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002663#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002664 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002665#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002666 stack_pointer = sp;
2667 PUSH(x);
2668 if (x != NULL)
2669 DISPATCH();
2670 break;
2671 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002672
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002673 TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw)
2674 TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw)
2675 TARGET(CALL_FUNCTION_VAR_KW)
2676 _call_function_var_kw:
2677 {
2678 int na = oparg & 0xff;
2679 int nk = (oparg>>8) & 0xff;
2680 int flags = (opcode - CALL_FUNCTION) & 3;
2681 int n = na + 2 * nk;
2682 PyObject **pfunc, *func, **sp;
2683 PCALL(PCALL_ALL);
2684 if (flags & CALL_FLAG_VAR)
2685 n++;
2686 if (flags & CALL_FLAG_KW)
2687 n++;
2688 pfunc = stack_pointer - n - 1;
2689 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002690
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002691 if (PyMethod_Check(func)
Stefan Krahb7e10102010-06-23 18:42:39 +00002692 && PyMethod_GET_SELF(func) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002693 PyObject *self = PyMethod_GET_SELF(func);
2694 Py_INCREF(self);
2695 func = PyMethod_GET_FUNCTION(func);
2696 Py_INCREF(func);
2697 Py_DECREF(*pfunc);
2698 *pfunc = self;
2699 na++;
2700 n++;
2701 } else
2702 Py_INCREF(func);
2703 sp = stack_pointer;
2704 READ_TIMESTAMP(intr0);
2705 x = ext_do_call(func, &sp, flags, na, nk);
2706 READ_TIMESTAMP(intr1);
2707 stack_pointer = sp;
2708 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002709
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002710 while (stack_pointer > pfunc) {
2711 w = POP();
2712 Py_DECREF(w);
2713 }
2714 PUSH(x);
2715 if (x != NULL)
2716 DISPATCH();
2717 break;
2718 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002719
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002720 TARGET_WITH_IMPL(MAKE_CLOSURE, _make_function)
2721 TARGET(MAKE_FUNCTION)
2722 _make_function:
2723 {
2724 int posdefaults = oparg & 0xff;
2725 int kwdefaults = (oparg>>8) & 0xff;
2726 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002727
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002728 v = POP(); /* code object */
2729 x = PyFunction_New(v, f->f_globals);
2730 Py_DECREF(v);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002731
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002732 if (x != NULL && opcode == MAKE_CLOSURE) {
2733 v = POP();
2734 if (PyFunction_SetClosure(x, v) != 0) {
2735 /* Can't happen unless bytecode is corrupt. */
2736 why = WHY_EXCEPTION;
2737 }
2738 Py_DECREF(v);
2739 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002740
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002741 if (x != NULL && num_annotations > 0) {
2742 Py_ssize_t name_ix;
2743 u = POP(); /* names of args with annotations */
2744 v = PyDict_New();
2745 if (v == NULL) {
2746 Py_DECREF(x);
2747 x = NULL;
2748 break;
2749 }
2750 name_ix = PyTuple_Size(u);
2751 assert(num_annotations == name_ix+1);
2752 while (name_ix > 0) {
2753 --name_ix;
2754 t = PyTuple_GET_ITEM(u, name_ix);
2755 w = POP();
2756 /* XXX(nnorwitz): check for errors */
2757 PyDict_SetItem(v, t, w);
2758 Py_DECREF(w);
2759 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002760
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002761 if (PyFunction_SetAnnotations(x, v) != 0) {
2762 /* Can't happen unless
2763 PyFunction_SetAnnotations changes. */
2764 why = WHY_EXCEPTION;
2765 }
2766 Py_DECREF(v);
2767 Py_DECREF(u);
2768 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002769
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002770 /* XXX Maybe this should be a separate opcode? */
2771 if (x != NULL && posdefaults > 0) {
2772 v = PyTuple_New(posdefaults);
2773 if (v == NULL) {
2774 Py_DECREF(x);
2775 x = NULL;
2776 break;
2777 }
2778 while (--posdefaults >= 0) {
2779 w = POP();
2780 PyTuple_SET_ITEM(v, posdefaults, w);
2781 }
2782 if (PyFunction_SetDefaults(x, v) != 0) {
2783 /* Can't happen unless
2784 PyFunction_SetDefaults changes. */
2785 why = WHY_EXCEPTION;
2786 }
2787 Py_DECREF(v);
2788 }
2789 if (x != NULL && kwdefaults > 0) {
2790 v = PyDict_New();
2791 if (v == NULL) {
2792 Py_DECREF(x);
2793 x = NULL;
2794 break;
2795 }
2796 while (--kwdefaults >= 0) {
2797 w = POP(); /* default value */
2798 u = POP(); /* kw only arg name */
2799 /* XXX(nnorwitz): check for errors */
2800 PyDict_SetItem(v, u, w);
2801 Py_DECREF(w);
2802 Py_DECREF(u);
2803 }
2804 if (PyFunction_SetKwDefaults(x, v) != 0) {
2805 /* Can't happen unless
2806 PyFunction_SetKwDefaults changes. */
2807 why = WHY_EXCEPTION;
2808 }
2809 Py_DECREF(v);
2810 }
2811 PUSH(x);
2812 break;
2813 }
Guido van Rossum8861b741996-07-30 16:49:37 +00002814
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002815 TARGET(BUILD_SLICE)
2816 if (oparg == 3)
2817 w = POP();
2818 else
2819 w = NULL;
2820 v = POP();
2821 u = TOP();
2822 x = PySlice_New(u, v, w);
2823 Py_DECREF(u);
2824 Py_DECREF(v);
2825 Py_XDECREF(w);
2826 SET_TOP(x);
2827 if (x != NULL) DISPATCH();
2828 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002830 TARGET(EXTENDED_ARG)
2831 opcode = NEXTOP();
2832 oparg = oparg<<16 | NEXTARG();
2833 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002834
Antoine Pitrou042b1282010-08-13 21:15:58 +00002835#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002836 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00002837#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002838 default:
2839 fprintf(stderr,
2840 "XXX lineno: %d, opcode: %d\n",
2841 PyFrame_GetLineNumber(f),
2842 opcode);
2843 PyErr_SetString(PyExc_SystemError, "unknown opcode");
2844 why = WHY_EXCEPTION;
2845 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002846
2847#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002848 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002849#endif
2850
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002851 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00002852
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002853 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002854
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002855 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002856
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002857 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002858
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002859 if (why == WHY_NOT) {
2860 if (err == 0 && x != NULL) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002861#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002862 /* This check is expensive! */
2863 if (PyErr_Occurred())
2864 fprintf(stderr,
2865 "XXX undetected error\n");
2866 else {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002867#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002868 READ_TIMESTAMP(loop1);
2869 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002870#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002871 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002872#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002873 }
2874 why = WHY_EXCEPTION;
2875 x = Py_None;
2876 err = 0;
2877 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002878
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002879 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002880
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002881 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
2882 if (!PyErr_Occurred()) {
2883 PyErr_SetString(PyExc_SystemError,
2884 "error return without exception set");
2885 why = WHY_EXCEPTION;
2886 }
2887 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002888#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002889 else {
2890 /* This check is expensive! */
2891 if (PyErr_Occurred()) {
2892 char buf[128];
2893 sprintf(buf, "Stack unwind with exception "
2894 "set and why=%d", why);
2895 Py_FatalError(buf);
2896 }
2897 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002898#endif
2899
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002900 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002901
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002902 if (why == WHY_EXCEPTION) {
2903 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002904
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002905 if (tstate->c_tracefunc != NULL)
2906 call_exc_trace(tstate->c_tracefunc,
2907 tstate->c_traceobj, f);
2908 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002909
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002910 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002911
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002912 if (why == WHY_RERAISE)
2913 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +00002914
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002915 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002916
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002917fast_block_end:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002918 while (why != WHY_NOT && f->f_iblock > 0) {
2919 /* Peek at the current block. */
2920 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002921
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002922 assert(why != WHY_YIELD);
2923 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2924 why = WHY_NOT;
2925 JUMPTO(PyLong_AS_LONG(retval));
2926 Py_DECREF(retval);
2927 break;
2928 }
2929 /* Now we have to pop the block. */
2930 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002931
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002932 if (b->b_type == EXCEPT_HANDLER) {
2933 UNWIND_EXCEPT_HANDLER(b);
2934 continue;
2935 }
2936 UNWIND_BLOCK(b);
2937 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2938 why = WHY_NOT;
2939 JUMPTO(b->b_handler);
2940 break;
2941 }
2942 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
2943 || b->b_type == SETUP_FINALLY)) {
2944 PyObject *exc, *val, *tb;
2945 int handler = b->b_handler;
2946 /* Beware, this invalidates all b->b_* fields */
2947 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
2948 PUSH(tstate->exc_traceback);
2949 PUSH(tstate->exc_value);
2950 if (tstate->exc_type != NULL) {
2951 PUSH(tstate->exc_type);
2952 }
2953 else {
2954 Py_INCREF(Py_None);
2955 PUSH(Py_None);
2956 }
2957 PyErr_Fetch(&exc, &val, &tb);
2958 /* Make the raw exception data
2959 available to the handler,
2960 so a program can emulate the
2961 Python main loop. */
2962 PyErr_NormalizeException(
2963 &exc, &val, &tb);
2964 PyException_SetTraceback(val, tb);
2965 Py_INCREF(exc);
2966 tstate->exc_type = exc;
2967 Py_INCREF(val);
2968 tstate->exc_value = val;
2969 tstate->exc_traceback = tb;
2970 if (tb == NULL)
2971 tb = Py_None;
2972 Py_INCREF(tb);
2973 PUSH(tb);
2974 PUSH(val);
2975 PUSH(exc);
2976 why = WHY_NOT;
2977 JUMPTO(handler);
2978 break;
2979 }
2980 if (b->b_type == SETUP_FINALLY) {
2981 if (why & (WHY_RETURN | WHY_CONTINUE))
2982 PUSH(retval);
2983 PUSH(PyLong_FromLong((long)why));
2984 why = WHY_NOT;
2985 JUMPTO(b->b_handler);
2986 break;
2987 }
2988 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00002989
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002990 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002991
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002992 if (why != WHY_NOT)
2993 break;
2994 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002995
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002996 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002997
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002998 assert(why != WHY_YIELD);
2999 /* Pop remaining stack entries. */
3000 while (!EMPTY()) {
3001 v = POP();
3002 Py_XDECREF(v);
3003 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003004
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003005 if (why != WHY_RETURN)
3006 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003007
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003008fast_yield:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003009 if (tstate->use_tracing) {
3010 if (tstate->c_tracefunc) {
3011 if (why == WHY_RETURN || why == WHY_YIELD) {
3012 if (call_trace(tstate->c_tracefunc,
3013 tstate->c_traceobj, f,
3014 PyTrace_RETURN, retval)) {
3015 Py_XDECREF(retval);
3016 retval = NULL;
3017 why = WHY_EXCEPTION;
3018 }
3019 }
3020 else if (why == WHY_EXCEPTION) {
3021 call_trace_protected(tstate->c_tracefunc,
3022 tstate->c_traceobj, f,
3023 PyTrace_RETURN, NULL);
3024 }
3025 }
3026 if (tstate->c_profilefunc) {
3027 if (why == WHY_EXCEPTION)
3028 call_trace_protected(tstate->c_profilefunc,
3029 tstate->c_profileobj, f,
3030 PyTrace_RETURN, NULL);
3031 else if (call_trace(tstate->c_profilefunc,
3032 tstate->c_profileobj, f,
3033 PyTrace_RETURN, retval)) {
3034 Py_XDECREF(retval);
3035 retval = NULL;
3036 why = WHY_EXCEPTION;
3037 }
3038 }
3039 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003040
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003041 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003042exit_eval_frame:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003043 Py_LeaveRecursiveCall();
3044 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003045
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003046 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00003047}
3048
Guido van Rossumc2e20742006-02-27 22:32:47 +00003049/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003050 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003051 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003052
Tim Peters6d6c1a32001-08-02 04:15:00 +00003053PyObject *
3054PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003055 PyObject **args, int argcount, PyObject **kws, int kwcount,
3056 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00003057{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003058 register PyFrameObject *f;
3059 register PyObject *retval = NULL;
3060 register PyObject **fastlocals, **freevars;
3061 PyThreadState *tstate = PyThreadState_GET();
3062 PyObject *x, *u;
3063 int total_args = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00003064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003065 if (globals == NULL) {
3066 PyErr_SetString(PyExc_SystemError,
3067 "PyEval_EvalCodeEx: NULL globals");
3068 return NULL;
3069 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003070
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003071 assert(tstate != NULL);
3072 assert(globals != NULL);
3073 f = PyFrame_New(tstate, co, globals, locals);
3074 if (f == NULL)
3075 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003076
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003077 fastlocals = f->f_localsplus;
3078 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003079
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003080 if (total_args || co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
3081 int i;
3082 int n = argcount;
3083 PyObject *kwdict = NULL;
3084 if (co->co_flags & CO_VARKEYWORDS) {
3085 kwdict = PyDict_New();
3086 if (kwdict == NULL)
3087 goto fail;
3088 i = total_args;
3089 if (co->co_flags & CO_VARARGS)
3090 i++;
3091 SETLOCAL(i, kwdict);
3092 }
3093 if (argcount > co->co_argcount) {
3094 if (!(co->co_flags & CO_VARARGS)) {
3095 PyErr_Format(PyExc_TypeError,
3096 "%U() takes %s %d "
Benjamin Peterson88968ad2010-06-25 19:30:21 +00003097 "positional argument%s (%d given)",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003098 co->co_name,
3099 defcount ? "at most" : "exactly",
Benjamin Peterson88968ad2010-06-25 19:30:21 +00003100 co->co_argcount,
3101 co->co_argcount == 1 ? "" : "s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003102 argcount + kwcount);
3103 goto fail;
3104 }
3105 n = co->co_argcount;
3106 }
3107 for (i = 0; i < n; i++) {
3108 x = args[i];
3109 Py_INCREF(x);
3110 SETLOCAL(i, x);
3111 }
3112 if (co->co_flags & CO_VARARGS) {
3113 u = PyTuple_New(argcount - n);
3114 if (u == NULL)
3115 goto fail;
3116 SETLOCAL(total_args, u);
3117 for (i = n; i < argcount; i++) {
3118 x = args[i];
3119 Py_INCREF(x);
3120 PyTuple_SET_ITEM(u, i-n, x);
3121 }
3122 }
3123 for (i = 0; i < kwcount; i++) {
3124 PyObject **co_varnames;
3125 PyObject *keyword = kws[2*i];
3126 PyObject *value = kws[2*i + 1];
3127 int j;
3128 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3129 PyErr_Format(PyExc_TypeError,
3130 "%U() keywords must be strings",
3131 co->co_name);
3132 goto fail;
3133 }
3134 /* Speed hack: do raw pointer compares. As names are
3135 normally interned this should almost always hit. */
3136 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3137 for (j = 0; j < total_args; j++) {
3138 PyObject *nm = co_varnames[j];
3139 if (nm == keyword)
3140 goto kw_found;
3141 }
3142 /* Slow fallback, just in case */
3143 for (j = 0; j < total_args; j++) {
3144 PyObject *nm = co_varnames[j];
3145 int cmp = PyObject_RichCompareBool(
3146 keyword, nm, Py_EQ);
3147 if (cmp > 0)
3148 goto kw_found;
3149 else if (cmp < 0)
3150 goto fail;
3151 }
3152 if (j >= total_args && kwdict == NULL) {
3153 PyErr_Format(PyExc_TypeError,
3154 "%U() got an unexpected "
3155 "keyword argument '%S'",
3156 co->co_name,
3157 keyword);
3158 goto fail;
3159 }
3160 PyDict_SetItem(kwdict, keyword, value);
3161 continue;
3162 kw_found:
3163 if (GETLOCAL(j) != NULL) {
3164 PyErr_Format(PyExc_TypeError,
3165 "%U() got multiple "
3166 "values for keyword "
3167 "argument '%S'",
3168 co->co_name,
3169 keyword);
3170 goto fail;
3171 }
3172 Py_INCREF(value);
3173 SETLOCAL(j, value);
3174 }
3175 if (co->co_kwonlyargcount > 0) {
3176 for (i = co->co_argcount; i < total_args; i++) {
3177 PyObject *name;
3178 if (GETLOCAL(i) != NULL)
3179 continue;
3180 name = PyTuple_GET_ITEM(co->co_varnames, i);
3181 if (kwdefs != NULL) {
3182 PyObject *def = PyDict_GetItem(kwdefs, name);
3183 if (def) {
3184 Py_INCREF(def);
3185 SETLOCAL(i, def);
3186 continue;
3187 }
3188 }
3189 PyErr_Format(PyExc_TypeError,
3190 "%U() needs keyword-only argument %S",
3191 co->co_name, name);
3192 goto fail;
3193 }
3194 }
3195 if (argcount < co->co_argcount) {
3196 int m = co->co_argcount - defcount;
3197 for (i = argcount; i < m; i++) {
3198 if (GETLOCAL(i) == NULL) {
3199 int j, given = 0;
3200 for (j = 0; j < co->co_argcount; j++)
3201 if (GETLOCAL(j))
3202 given++;
3203 PyErr_Format(PyExc_TypeError,
3204 "%U() takes %s %d "
3205 "argument%s "
3206 "(%d given)",
3207 co->co_name,
3208 ((co->co_flags & CO_VARARGS) ||
3209 defcount) ? "at least"
3210 : "exactly",
3211 m, m == 1 ? "" : "s", given);
3212 goto fail;
3213 }
3214 }
3215 if (n > m)
3216 i = n - m;
3217 else
3218 i = 0;
3219 for (; i < defcount; i++) {
3220 if (GETLOCAL(m+i) == NULL) {
3221 PyObject *def = defs[i];
3222 Py_INCREF(def);
3223 SETLOCAL(m+i, def);
3224 }
3225 }
3226 }
3227 }
3228 else if (argcount > 0 || kwcount > 0) {
3229 PyErr_Format(PyExc_TypeError,
3230 "%U() takes no arguments (%d given)",
3231 co->co_name,
3232 argcount + kwcount);
3233 goto fail;
3234 }
3235 /* Allocate and initialize storage for cell vars, and copy free
3236 vars into frame. This isn't too efficient right now. */
3237 if (PyTuple_GET_SIZE(co->co_cellvars)) {
3238 int i, j, nargs, found;
3239 Py_UNICODE *cellname, *argname;
3240 PyObject *c;
Tim Peters5ca576e2001-06-18 22:08:13 +00003241
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003242 nargs = total_args;
3243 if (co->co_flags & CO_VARARGS)
3244 nargs++;
3245 if (co->co_flags & CO_VARKEYWORDS)
3246 nargs++;
Tim Peters5ca576e2001-06-18 22:08:13 +00003247
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003248 /* Initialize each cell var, taking into account
3249 cell vars that are initialized from arguments.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003251 Should arrange for the compiler to put cellvars
3252 that are arguments at the beginning of the cellvars
3253 list so that we can march over it more efficiently?
3254 */
3255 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
3256 cellname = PyUnicode_AS_UNICODE(
3257 PyTuple_GET_ITEM(co->co_cellvars, i));
3258 found = 0;
3259 for (j = 0; j < nargs; j++) {
3260 argname = PyUnicode_AS_UNICODE(
3261 PyTuple_GET_ITEM(co->co_varnames, j));
3262 if (Py_UNICODE_strcmp(cellname, argname) == 0) {
3263 c = PyCell_New(GETLOCAL(j));
3264 if (c == NULL)
3265 goto fail;
3266 GETLOCAL(co->co_nlocals + i) = c;
3267 found = 1;
3268 break;
3269 }
3270 }
3271 if (found == 0) {
3272 c = PyCell_New(NULL);
3273 if (c == NULL)
3274 goto fail;
3275 SETLOCAL(co->co_nlocals + i, c);
3276 }
3277 }
3278 }
3279 if (PyTuple_GET_SIZE(co->co_freevars)) {
3280 int i;
3281 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3282 PyObject *o = PyTuple_GET_ITEM(closure, i);
3283 Py_INCREF(o);
3284 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
3285 }
3286 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003288 if (co->co_flags & CO_GENERATOR) {
3289 /* Don't need to keep the reference to f_back, it will be set
3290 * when the generator is resumed. */
3291 Py_XDECREF(f->f_back);
3292 f->f_back = NULL;
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003294 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003296 /* Create a new generator that owns the ready to run frame
3297 * and return that as the value. */
3298 return PyGen_New(f);
3299 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003300
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003301 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003302
Thomas Woutersce272b62007-09-19 21:19:28 +00003303fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003304
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003305 /* decref'ing the frame can cause __del__ methods to get invoked,
3306 which can call back into Python. While we're done with the
3307 current Python frame (f), the associated C stack is still in use,
3308 so recursion_depth must be boosted for the duration.
3309 */
3310 assert(tstate != NULL);
3311 ++tstate->recursion_depth;
3312 Py_DECREF(f);
3313 --tstate->recursion_depth;
3314 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003315}
3316
3317
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003318static PyObject *
3319special_lookup(PyObject *o, char *meth, PyObject **cache)
3320{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003321 PyObject *res;
3322 res = _PyObject_LookupSpecial(o, meth, cache);
3323 if (res == NULL && !PyErr_Occurred()) {
3324 PyErr_SetObject(PyExc_AttributeError, *cache);
3325 return NULL;
3326 }
3327 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003328}
3329
3330
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003331/* Logic for the raise statement (too complicated for inlining).
3332 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00003333static enum why_code
Collin Winter828f04a2007-08-31 00:04:24 +00003334do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003335{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003336 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003338 if (exc == NULL) {
3339 /* Reraise */
3340 PyThreadState *tstate = PyThreadState_GET();
3341 PyObject *tb;
3342 type = tstate->exc_type;
3343 value = tstate->exc_value;
3344 tb = tstate->exc_traceback;
3345 if (type == Py_None) {
3346 PyErr_SetString(PyExc_RuntimeError,
3347 "No active exception to reraise");
3348 return WHY_EXCEPTION;
3349 }
3350 Py_XINCREF(type);
3351 Py_XINCREF(value);
3352 Py_XINCREF(tb);
3353 PyErr_Restore(type, value, tb);
3354 return WHY_RERAISE;
3355 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003357 /* We support the following forms of raise:
3358 raise
Collin Winter828f04a2007-08-31 00:04:24 +00003359 raise <instance>
3360 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003361
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003362 if (PyExceptionClass_Check(exc)) {
3363 type = exc;
3364 value = PyObject_CallObject(exc, NULL);
3365 if (value == NULL)
3366 goto raise_error;
3367 }
3368 else if (PyExceptionInstance_Check(exc)) {
3369 value = exc;
3370 type = PyExceptionInstance_Class(exc);
3371 Py_INCREF(type);
3372 }
3373 else {
3374 /* Not something you can raise. You get an exception
3375 anyway, just not what you specified :-) */
3376 Py_DECREF(exc);
3377 PyErr_SetString(PyExc_TypeError,
3378 "exceptions must derive from BaseException");
3379 goto raise_error;
3380 }
Collin Winter828f04a2007-08-31 00:04:24 +00003381
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003382 if (cause) {
3383 PyObject *fixed_cause;
3384 if (PyExceptionClass_Check(cause)) {
3385 fixed_cause = PyObject_CallObject(cause, NULL);
3386 if (fixed_cause == NULL)
3387 goto raise_error;
3388 Py_DECREF(cause);
3389 }
3390 else if (PyExceptionInstance_Check(cause)) {
3391 fixed_cause = cause;
3392 }
3393 else {
3394 PyErr_SetString(PyExc_TypeError,
3395 "exception causes must derive from "
3396 "BaseException");
3397 goto raise_error;
3398 }
3399 PyException_SetCause(value, fixed_cause);
3400 }
Collin Winter828f04a2007-08-31 00:04:24 +00003401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003402 PyErr_SetObject(type, value);
3403 /* PyErr_SetObject incref's its arguments */
3404 Py_XDECREF(value);
3405 Py_XDECREF(type);
3406 return WHY_EXCEPTION;
Collin Winter828f04a2007-08-31 00:04:24 +00003407
3408raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003409 Py_XDECREF(value);
3410 Py_XDECREF(type);
3411 Py_XDECREF(cause);
3412 return WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003413}
3414
Tim Petersd6d010b2001-06-21 02:49:55 +00003415/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00003416 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003417
Guido van Rossum0368b722007-05-11 16:50:42 +00003418 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
3419 with a variable target.
3420*/
Tim Petersd6d010b2001-06-21 02:49:55 +00003421
Barry Warsawe42b18f1997-08-25 22:13:04 +00003422static int
Guido van Rossum0368b722007-05-11 16:50:42 +00003423unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003424{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003425 int i = 0, j = 0;
3426 Py_ssize_t ll = 0;
3427 PyObject *it; /* iter(v) */
3428 PyObject *w;
3429 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00003430
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003431 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00003432
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003433 it = PyObject_GetIter(v);
3434 if (it == NULL)
3435 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00003436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003437 for (; i < argcnt; i++) {
3438 w = PyIter_Next(it);
3439 if (w == NULL) {
3440 /* Iterator done, via error or exhaustion. */
3441 if (!PyErr_Occurred()) {
3442 PyErr_Format(PyExc_ValueError,
3443 "need more than %d value%s to unpack",
3444 i, i == 1 ? "" : "s");
3445 }
3446 goto Error;
3447 }
3448 *--sp = w;
3449 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003451 if (argcntafter == -1) {
3452 /* We better have exhausted the iterator now. */
3453 w = PyIter_Next(it);
3454 if (w == NULL) {
3455 if (PyErr_Occurred())
3456 goto Error;
3457 Py_DECREF(it);
3458 return 1;
3459 }
3460 Py_DECREF(w);
Georg Brandl0310a832010-07-10 10:32:36 +00003461 PyErr_Format(PyExc_ValueError, "too many values to unpack "
3462 "(expected %d)", argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003463 goto Error;
3464 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003466 l = PySequence_List(it);
3467 if (l == NULL)
3468 goto Error;
3469 *--sp = l;
3470 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00003471
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003472 ll = PyList_GET_SIZE(l);
3473 if (ll < argcntafter) {
3474 PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack",
3475 argcnt + ll);
3476 goto Error;
3477 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003478
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003479 /* Pop the "after-variable" args off the list. */
3480 for (j = argcntafter; j > 0; j--, i++) {
3481 *--sp = PyList_GET_ITEM(l, ll - j);
3482 }
3483 /* Resize the list. */
3484 Py_SIZE(l) = ll - argcntafter;
3485 Py_DECREF(it);
3486 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00003487
Tim Petersd6d010b2001-06-21 02:49:55 +00003488Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003489 for (; i > 0; i--, sp++)
3490 Py_DECREF(*sp);
3491 Py_XDECREF(it);
3492 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003493}
3494
3495
Guido van Rossum96a42c81992-01-12 02:29:51 +00003496#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003497static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003498prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003499{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003500 printf("%s ", str);
3501 if (PyObject_Print(v, stdout, 0) != 0)
3502 PyErr_Clear(); /* Don't know what else to do */
3503 printf("\n");
3504 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003505}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003506#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003507
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003508static void
Fred Drake5755ce62001-06-27 19:19:46 +00003509call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003510{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003511 PyObject *type, *value, *traceback, *arg;
3512 int err;
3513 PyErr_Fetch(&type, &value, &traceback);
3514 if (value == NULL) {
3515 value = Py_None;
3516 Py_INCREF(value);
3517 }
3518 arg = PyTuple_Pack(3, type, value, traceback);
3519 if (arg == NULL) {
3520 PyErr_Restore(type, value, traceback);
3521 return;
3522 }
3523 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
3524 Py_DECREF(arg);
3525 if (err == 0)
3526 PyErr_Restore(type, value, traceback);
3527 else {
3528 Py_XDECREF(type);
3529 Py_XDECREF(value);
3530 Py_XDECREF(traceback);
3531 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003532}
3533
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003534static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003535call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003536 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003537{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003538 PyObject *type, *value, *traceback;
3539 int err;
3540 PyErr_Fetch(&type, &value, &traceback);
3541 err = call_trace(func, obj, frame, what, arg);
3542 if (err == 0)
3543 {
3544 PyErr_Restore(type, value, traceback);
3545 return 0;
3546 }
3547 else {
3548 Py_XDECREF(type);
3549 Py_XDECREF(value);
3550 Py_XDECREF(traceback);
3551 return -1;
3552 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003553}
3554
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003555static int
Fred Drake5755ce62001-06-27 19:19:46 +00003556call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003557 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003558{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003559 register PyThreadState *tstate = frame->f_tstate;
3560 int result;
3561 if (tstate->tracing)
3562 return 0;
3563 tstate->tracing++;
3564 tstate->use_tracing = 0;
3565 result = func(obj, frame, what, arg);
3566 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3567 || (tstate->c_profilefunc != NULL));
3568 tstate->tracing--;
3569 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003570}
3571
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003572PyObject *
3573_PyEval_CallTracing(PyObject *func, PyObject *args)
3574{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003575 PyFrameObject *frame = PyEval_GetFrame();
3576 PyThreadState *tstate = frame->f_tstate;
3577 int save_tracing = tstate->tracing;
3578 int save_use_tracing = tstate->use_tracing;
3579 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003580
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003581 tstate->tracing = 0;
3582 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3583 || (tstate->c_profilefunc != NULL));
3584 result = PyObject_Call(func, args, NULL);
3585 tstate->tracing = save_tracing;
3586 tstate->use_tracing = save_use_tracing;
3587 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003588}
3589
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003590/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00003591static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003592maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003593 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3594 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003595{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003596 int result = 0;
3597 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003598
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003599 /* If the last instruction executed isn't in the current
3600 instruction window, reset the window.
3601 */
3602 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
3603 PyAddrPair bounds;
3604 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3605 &bounds);
3606 *instr_lb = bounds.ap_lower;
3607 *instr_ub = bounds.ap_upper;
3608 }
3609 /* If the last instruction falls at the start of a line or if
3610 it represents a jump backwards, update the frame's line
3611 number and call the trace function. */
3612 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
3613 frame->f_lineno = line;
3614 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
3615 }
3616 *instr_prev = frame->f_lasti;
3617 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003618}
3619
Fred Drake5755ce62001-06-27 19:19:46 +00003620void
3621PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003622{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003623 PyThreadState *tstate = PyThreadState_GET();
3624 PyObject *temp = tstate->c_profileobj;
3625 Py_XINCREF(arg);
3626 tstate->c_profilefunc = NULL;
3627 tstate->c_profileobj = NULL;
3628 /* Must make sure that tracing is not ignored if 'temp' is freed */
3629 tstate->use_tracing = tstate->c_tracefunc != NULL;
3630 Py_XDECREF(temp);
3631 tstate->c_profilefunc = func;
3632 tstate->c_profileobj = arg;
3633 /* Flag that tracing or profiling is turned on */
3634 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003635}
3636
3637void
3638PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3639{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003640 PyThreadState *tstate = PyThreadState_GET();
3641 PyObject *temp = tstate->c_traceobj;
3642 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
3643 Py_XINCREF(arg);
3644 tstate->c_tracefunc = NULL;
3645 tstate->c_traceobj = NULL;
3646 /* Must make sure that profiling is not ignored if 'temp' is freed */
3647 tstate->use_tracing = tstate->c_profilefunc != NULL;
3648 Py_XDECREF(temp);
3649 tstate->c_tracefunc = func;
3650 tstate->c_traceobj = arg;
3651 /* Flag that tracing or profiling is turned on */
3652 tstate->use_tracing = ((func != NULL)
3653 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003654}
3655
Guido van Rossumb209a111997-04-29 18:18:01 +00003656PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003657PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003658{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003659 PyFrameObject *current_frame = PyEval_GetFrame();
3660 if (current_frame == NULL)
3661 return PyThreadState_GET()->interp->builtins;
3662 else
3663 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003664}
3665
Guido van Rossumb209a111997-04-29 18:18:01 +00003666PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003667PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003668{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003669 PyFrameObject *current_frame = PyEval_GetFrame();
3670 if (current_frame == NULL)
3671 return NULL;
3672 PyFrame_FastToLocals(current_frame);
3673 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00003674}
3675
Guido van Rossumb209a111997-04-29 18:18:01 +00003676PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003677PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003678{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003679 PyFrameObject *current_frame = PyEval_GetFrame();
3680 if (current_frame == NULL)
3681 return NULL;
3682 else
3683 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003684}
3685
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003686PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003687PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003688{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003689 PyThreadState *tstate = PyThreadState_GET();
3690 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003691}
3692
Guido van Rossum6135a871995-01-09 17:53:26 +00003693int
Tim Peters5ba58662001-07-16 02:29:45 +00003694PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003695{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003696 PyFrameObject *current_frame = PyEval_GetFrame();
3697 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003698
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003699 if (current_frame != NULL) {
3700 const int codeflags = current_frame->f_code->co_flags;
3701 const int compilerflags = codeflags & PyCF_MASK;
3702 if (compilerflags) {
3703 result = 1;
3704 cf->cf_flags |= compilerflags;
3705 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003706#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003707 if (codeflags & CO_GENERATOR_ALLOWED) {
3708 result = 1;
3709 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3710 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003711#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003712 }
3713 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003714}
3715
Guido van Rossum3f5da241990-12-20 15:06:42 +00003716
Guido van Rossum681d79a1995-07-18 14:51:37 +00003717/* External interface to call any callable object.
Antoine Pitrou8689a102010-04-01 16:53:15 +00003718 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00003719
Guido van Rossumb209a111997-04-29 18:18:01 +00003720PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003721PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003722{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003723 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003724
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003725 if (arg == NULL) {
3726 arg = PyTuple_New(0);
3727 if (arg == NULL)
3728 return NULL;
3729 }
3730 else if (!PyTuple_Check(arg)) {
3731 PyErr_SetString(PyExc_TypeError,
3732 "argument list must be a tuple");
3733 return NULL;
3734 }
3735 else
3736 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003737
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003738 if (kw != NULL && !PyDict_Check(kw)) {
3739 PyErr_SetString(PyExc_TypeError,
3740 "keyword list must be a dictionary");
3741 Py_DECREF(arg);
3742 return NULL;
3743 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00003744
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003745 result = PyObject_Call(func, arg, kw);
3746 Py_DECREF(arg);
3747 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00003748}
3749
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003750const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003751PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003752{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003753 if (PyMethod_Check(func))
3754 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
3755 else if (PyFunction_Check(func))
3756 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
3757 else if (PyCFunction_Check(func))
3758 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3759 else
3760 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00003761}
3762
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003763const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003764PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003765{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003766 if (PyMethod_Check(func))
3767 return "()";
3768 else if (PyFunction_Check(func))
3769 return "()";
3770 else if (PyCFunction_Check(func))
3771 return "()";
3772 else
3773 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00003774}
3775
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003776static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003777err_args(PyObject *func, int flags, int nargs)
3778{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003779 if (flags & METH_NOARGS)
3780 PyErr_Format(PyExc_TypeError,
3781 "%.200s() takes no arguments (%d given)",
3782 ((PyCFunctionObject *)func)->m_ml->ml_name,
3783 nargs);
3784 else
3785 PyErr_Format(PyExc_TypeError,
3786 "%.200s() takes exactly one argument (%d given)",
3787 ((PyCFunctionObject *)func)->m_ml->ml_name,
3788 nargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003789}
3790
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003791#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003792if (tstate->use_tracing && tstate->c_profilefunc) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003793 if (call_trace(tstate->c_profilefunc, \
3794 tstate->c_profileobj, \
3795 tstate->frame, PyTrace_C_CALL, \
3796 func)) { \
3797 x = NULL; \
3798 } \
3799 else { \
3800 x = call; \
3801 if (tstate->c_profilefunc != NULL) { \
3802 if (x == NULL) { \
3803 call_trace_protected(tstate->c_profilefunc, \
3804 tstate->c_profileobj, \
3805 tstate->frame, PyTrace_C_EXCEPTION, \
3806 func); \
3807 /* XXX should pass (type, value, tb) */ \
3808 } else { \
3809 if (call_trace(tstate->c_profilefunc, \
3810 tstate->c_profileobj, \
3811 tstate->frame, PyTrace_C_RETURN, \
3812 func)) { \
3813 Py_DECREF(x); \
3814 x = NULL; \
3815 } \
3816 } \
3817 } \
3818 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003819} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003820 x = call; \
3821 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003822
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003823static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003824call_function(PyObject ***pp_stack, int oparg
3825#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003826 , uint64* pintr0, uint64* pintr1
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003827#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003828 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003829{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003830 int na = oparg & 0xff;
3831 int nk = (oparg>>8) & 0xff;
3832 int n = na + 2 * nk;
3833 PyObject **pfunc = (*pp_stack) - n - 1;
3834 PyObject *func = *pfunc;
3835 PyObject *x, *w;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003836
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003837 /* Always dispatch PyCFunction first, because these are
3838 presumed to be the most frequent callable object.
3839 */
3840 if (PyCFunction_Check(func) && nk == 0) {
3841 int flags = PyCFunction_GET_FLAGS(func);
3842 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003843
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003844 PCALL(PCALL_CFUNCTION);
3845 if (flags & (METH_NOARGS | METH_O)) {
3846 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3847 PyObject *self = PyCFunction_GET_SELF(func);
3848 if (flags & METH_NOARGS && na == 0) {
3849 C_TRACE(x, (*meth)(self,NULL));
3850 }
3851 else if (flags & METH_O && na == 1) {
3852 PyObject *arg = EXT_POP(*pp_stack);
3853 C_TRACE(x, (*meth)(self,arg));
3854 Py_DECREF(arg);
3855 }
3856 else {
3857 err_args(func, flags, na);
3858 x = NULL;
3859 }
3860 }
3861 else {
3862 PyObject *callargs;
3863 callargs = load_args(pp_stack, na);
3864 READ_TIMESTAMP(*pintr0);
3865 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
3866 READ_TIMESTAMP(*pintr1);
3867 Py_XDECREF(callargs);
3868 }
3869 } else {
3870 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3871 /* optimize access to bound methods */
3872 PyObject *self = PyMethod_GET_SELF(func);
3873 PCALL(PCALL_METHOD);
3874 PCALL(PCALL_BOUND_METHOD);
3875 Py_INCREF(self);
3876 func = PyMethod_GET_FUNCTION(func);
3877 Py_INCREF(func);
3878 Py_DECREF(*pfunc);
3879 *pfunc = self;
3880 na++;
3881 n++;
3882 } else
3883 Py_INCREF(func);
3884 READ_TIMESTAMP(*pintr0);
3885 if (PyFunction_Check(func))
3886 x = fast_function(func, pp_stack, n, na, nk);
3887 else
3888 x = do_call(func, pp_stack, na, nk);
3889 READ_TIMESTAMP(*pintr1);
3890 Py_DECREF(func);
3891 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003892
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003893 /* Clear the stack of the function object. Also removes
3894 the arguments in case they weren't consumed already
3895 (fast_function() and err_args() leave them on the stack).
3896 */
3897 while ((*pp_stack) > pfunc) {
3898 w = EXT_POP(*pp_stack);
3899 Py_DECREF(w);
3900 PCALL(PCALL_POP);
3901 }
3902 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003903}
3904
Jeremy Hylton192690e2002-08-16 18:36:11 +00003905/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003906 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003907 For the simplest case -- a function that takes only positional
3908 arguments and is called with only positional arguments -- it
3909 inlines the most primitive frame setup code from
3910 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3911 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003912*/
3913
3914static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003915fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003916{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003917 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
3918 PyObject *globals = PyFunction_GET_GLOBALS(func);
3919 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3920 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
3921 PyObject **d = NULL;
3922 int nd = 0;
Jeremy Hylton52820442001-01-03 23:52:36 +00003923
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003924 PCALL(PCALL_FUNCTION);
3925 PCALL(PCALL_FAST_FUNCTION);
3926 if (argdefs == NULL && co->co_argcount == n &&
3927 co->co_kwonlyargcount == 0 && nk==0 &&
3928 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3929 PyFrameObject *f;
3930 PyObject *retval = NULL;
3931 PyThreadState *tstate = PyThreadState_GET();
3932 PyObject **fastlocals, **stack;
3933 int i;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003934
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003935 PCALL(PCALL_FASTER_FUNCTION);
3936 assert(globals != NULL);
3937 /* XXX Perhaps we should create a specialized
3938 PyFrame_New() that doesn't take locals, but does
3939 take builtins without sanity checking them.
3940 */
3941 assert(tstate != NULL);
3942 f = PyFrame_New(tstate, co, globals, NULL);
3943 if (f == NULL)
3944 return NULL;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003945
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003946 fastlocals = f->f_localsplus;
3947 stack = (*pp_stack) - n;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003948
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003949 for (i = 0; i < n; i++) {
3950 Py_INCREF(*stack);
3951 fastlocals[i] = *stack++;
3952 }
3953 retval = PyEval_EvalFrameEx(f,0);
3954 ++tstate->recursion_depth;
3955 Py_DECREF(f);
3956 --tstate->recursion_depth;
3957 return retval;
3958 }
3959 if (argdefs != NULL) {
3960 d = &PyTuple_GET_ITEM(argdefs, 0);
3961 nd = Py_SIZE(argdefs);
3962 }
3963 return PyEval_EvalCodeEx(co, globals,
3964 (PyObject *)NULL, (*pp_stack)-n, na,
3965 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
3966 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003967}
3968
3969static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003970update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3971 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003972{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003973 PyObject *kwdict = NULL;
3974 if (orig_kwdict == NULL)
3975 kwdict = PyDict_New();
3976 else {
3977 kwdict = PyDict_Copy(orig_kwdict);
3978 Py_DECREF(orig_kwdict);
3979 }
3980 if (kwdict == NULL)
3981 return NULL;
3982 while (--nk >= 0) {
3983 int err;
3984 PyObject *value = EXT_POP(*pp_stack);
3985 PyObject *key = EXT_POP(*pp_stack);
3986 if (PyDict_GetItem(kwdict, key) != NULL) {
3987 PyErr_Format(PyExc_TypeError,
3988 "%.200s%s got multiple values "
3989 "for keyword argument '%U'",
3990 PyEval_GetFuncName(func),
3991 PyEval_GetFuncDesc(func),
3992 key);
3993 Py_DECREF(key);
3994 Py_DECREF(value);
3995 Py_DECREF(kwdict);
3996 return NULL;
3997 }
3998 err = PyDict_SetItem(kwdict, key, value);
3999 Py_DECREF(key);
4000 Py_DECREF(value);
4001 if (err) {
4002 Py_DECREF(kwdict);
4003 return NULL;
4004 }
4005 }
4006 return kwdict;
Jeremy Hylton52820442001-01-03 23:52:36 +00004007}
4008
4009static PyObject *
4010update_star_args(int nstack, int nstar, PyObject *stararg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004011 PyObject ***pp_stack)
Jeremy Hylton52820442001-01-03 23:52:36 +00004012{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004013 PyObject *callargs, *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004014
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004015 callargs = PyTuple_New(nstack + nstar);
4016 if (callargs == NULL) {
4017 return NULL;
4018 }
4019 if (nstar) {
4020 int i;
4021 for (i = 0; i < nstar; i++) {
4022 PyObject *a = PyTuple_GET_ITEM(stararg, i);
4023 Py_INCREF(a);
4024 PyTuple_SET_ITEM(callargs, nstack + i, a);
4025 }
4026 }
4027 while (--nstack >= 0) {
4028 w = EXT_POP(*pp_stack);
4029 PyTuple_SET_ITEM(callargs, nstack, w);
4030 }
4031 return callargs;
Jeremy Hylton52820442001-01-03 23:52:36 +00004032}
4033
4034static PyObject *
4035load_args(PyObject ***pp_stack, int na)
4036{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004037 PyObject *args = PyTuple_New(na);
4038 PyObject *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004040 if (args == NULL)
4041 return NULL;
4042 while (--na >= 0) {
4043 w = EXT_POP(*pp_stack);
4044 PyTuple_SET_ITEM(args, na, w);
4045 }
4046 return args;
Jeremy Hylton52820442001-01-03 23:52:36 +00004047}
4048
4049static PyObject *
4050do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4051{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004052 PyObject *callargs = NULL;
4053 PyObject *kwdict = NULL;
4054 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004055
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004056 if (nk > 0) {
4057 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
4058 if (kwdict == NULL)
4059 goto call_fail;
4060 }
4061 callargs = load_args(pp_stack, na);
4062 if (callargs == NULL)
4063 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004064#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004065 /* At this point, we have to look at the type of func to
4066 update the call stats properly. Do it here so as to avoid
4067 exposing the call stats machinery outside ceval.c
4068 */
4069 if (PyFunction_Check(func))
4070 PCALL(PCALL_FUNCTION);
4071 else if (PyMethod_Check(func))
4072 PCALL(PCALL_METHOD);
4073 else if (PyType_Check(func))
4074 PCALL(PCALL_TYPE);
4075 else if (PyCFunction_Check(func))
4076 PCALL(PCALL_CFUNCTION);
4077 else
4078 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004079#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004080 if (PyCFunction_Check(func)) {
4081 PyThreadState *tstate = PyThreadState_GET();
4082 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4083 }
4084 else
4085 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00004086call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004087 Py_XDECREF(callargs);
4088 Py_XDECREF(kwdict);
4089 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004090}
4091
4092static PyObject *
4093ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4094{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004095 int nstar = 0;
4096 PyObject *callargs = NULL;
4097 PyObject *stararg = NULL;
4098 PyObject *kwdict = NULL;
4099 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004100
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004101 if (flags & CALL_FLAG_KW) {
4102 kwdict = EXT_POP(*pp_stack);
4103 if (!PyDict_Check(kwdict)) {
4104 PyObject *d;
4105 d = PyDict_New();
4106 if (d == NULL)
4107 goto ext_call_fail;
4108 if (PyDict_Update(d, kwdict) != 0) {
4109 Py_DECREF(d);
4110 /* PyDict_Update raises attribute
4111 * error (percolated from an attempt
4112 * to get 'keys' attribute) instead of
4113 * a type error if its second argument
4114 * is not a mapping.
4115 */
4116 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4117 PyErr_Format(PyExc_TypeError,
4118 "%.200s%.200s argument after ** "
4119 "must be a mapping, not %.200s",
4120 PyEval_GetFuncName(func),
4121 PyEval_GetFuncDesc(func),
4122 kwdict->ob_type->tp_name);
4123 }
4124 goto ext_call_fail;
4125 }
4126 Py_DECREF(kwdict);
4127 kwdict = d;
4128 }
4129 }
4130 if (flags & CALL_FLAG_VAR) {
4131 stararg = EXT_POP(*pp_stack);
4132 if (!PyTuple_Check(stararg)) {
4133 PyObject *t = NULL;
4134 t = PySequence_Tuple(stararg);
4135 if (t == NULL) {
4136 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4137 PyErr_Format(PyExc_TypeError,
4138 "%.200s%.200s argument after * "
4139 "must be a sequence, not %200s",
4140 PyEval_GetFuncName(func),
4141 PyEval_GetFuncDesc(func),
4142 stararg->ob_type->tp_name);
4143 }
4144 goto ext_call_fail;
4145 }
4146 Py_DECREF(stararg);
4147 stararg = t;
4148 }
4149 nstar = PyTuple_GET_SIZE(stararg);
4150 }
4151 if (nk > 0) {
4152 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
4153 if (kwdict == NULL)
4154 goto ext_call_fail;
4155 }
4156 callargs = update_star_args(na, nstar, stararg, pp_stack);
4157 if (callargs == NULL)
4158 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004159#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004160 /* At this point, we have to look at the type of func to
4161 update the call stats properly. Do it here so as to avoid
4162 exposing the call stats machinery outside ceval.c
4163 */
4164 if (PyFunction_Check(func))
4165 PCALL(PCALL_FUNCTION);
4166 else if (PyMethod_Check(func))
4167 PCALL(PCALL_METHOD);
4168 else if (PyType_Check(func))
4169 PCALL(PCALL_TYPE);
4170 else if (PyCFunction_Check(func))
4171 PCALL(PCALL_CFUNCTION);
4172 else
4173 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004174#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004175 if (PyCFunction_Check(func)) {
4176 PyThreadState *tstate = PyThreadState_GET();
4177 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4178 }
4179 else
4180 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00004181ext_call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004182 Py_XDECREF(callargs);
4183 Py_XDECREF(kwdict);
4184 Py_XDECREF(stararg);
4185 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004186}
4187
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004188/* Extract a slice index from a PyInt or PyLong or an object with the
4189 nb_index slot defined, and store in *pi.
4190 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4191 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 +00004192 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004193*/
Tim Petersb5196382001-12-16 19:44:20 +00004194/* Note: If v is NULL, return success without storing into *pi. This
4195 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4196 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004197*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004198int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004199_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004200{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004201 if (v != NULL) {
4202 Py_ssize_t x;
4203 if (PyIndex_Check(v)) {
4204 x = PyNumber_AsSsize_t(v, NULL);
4205 if (x == -1 && PyErr_Occurred())
4206 return 0;
4207 }
4208 else {
4209 PyErr_SetString(PyExc_TypeError,
4210 "slice indices must be integers or "
4211 "None or have an __index__ method");
4212 return 0;
4213 }
4214 *pi = x;
4215 }
4216 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004217}
4218
Guido van Rossum486364b2007-06-30 05:01:58 +00004219#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004220 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004221
Guido van Rossumb209a111997-04-29 18:18:01 +00004222static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004223cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004224{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004225 int res = 0;
4226 switch (op) {
4227 case PyCmp_IS:
4228 res = (v == w);
4229 break;
4230 case PyCmp_IS_NOT:
4231 res = (v != w);
4232 break;
4233 case PyCmp_IN:
4234 res = PySequence_Contains(w, v);
4235 if (res < 0)
4236 return NULL;
4237 break;
4238 case PyCmp_NOT_IN:
4239 res = PySequence_Contains(w, v);
4240 if (res < 0)
4241 return NULL;
4242 res = !res;
4243 break;
4244 case PyCmp_EXC_MATCH:
4245 if (PyTuple_Check(w)) {
4246 Py_ssize_t i, length;
4247 length = PyTuple_Size(w);
4248 for (i = 0; i < length; i += 1) {
4249 PyObject *exc = PyTuple_GET_ITEM(w, i);
4250 if (!PyExceptionClass_Check(exc)) {
4251 PyErr_SetString(PyExc_TypeError,
4252 CANNOT_CATCH_MSG);
4253 return NULL;
4254 }
4255 }
4256 }
4257 else {
4258 if (!PyExceptionClass_Check(w)) {
4259 PyErr_SetString(PyExc_TypeError,
4260 CANNOT_CATCH_MSG);
4261 return NULL;
4262 }
4263 }
4264 res = PyErr_GivenExceptionMatches(v, w);
4265 break;
4266 default:
4267 return PyObject_RichCompare(v, w, op);
4268 }
4269 v = res ? Py_True : Py_False;
4270 Py_INCREF(v);
4271 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004272}
4273
Thomas Wouters52152252000-08-17 22:55:00 +00004274static PyObject *
4275import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004276{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004277 PyObject *x;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004279 x = PyObject_GetAttr(v, name);
4280 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
4281 PyErr_Format(PyExc_ImportError, "cannot import name %S", name);
4282 }
4283 return x;
Thomas Wouters52152252000-08-17 22:55:00 +00004284}
Guido van Rossumac7be682001-01-17 15:42:30 +00004285
Thomas Wouters52152252000-08-17 22:55:00 +00004286static int
4287import_all_from(PyObject *locals, PyObject *v)
4288{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004289 PyObject *all = PyObject_GetAttrString(v, "__all__");
4290 PyObject *dict, *name, *value;
4291 int skip_leading_underscores = 0;
4292 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004294 if (all == NULL) {
4295 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4296 return -1; /* Unexpected error */
4297 PyErr_Clear();
4298 dict = PyObject_GetAttrString(v, "__dict__");
4299 if (dict == NULL) {
4300 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4301 return -1;
4302 PyErr_SetString(PyExc_ImportError,
4303 "from-import-* object has no __dict__ and no __all__");
4304 return -1;
4305 }
4306 all = PyMapping_Keys(dict);
4307 Py_DECREF(dict);
4308 if (all == NULL)
4309 return -1;
4310 skip_leading_underscores = 1;
4311 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004313 for (pos = 0, err = 0; ; pos++) {
4314 name = PySequence_GetItem(all, pos);
4315 if (name == NULL) {
4316 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4317 err = -1;
4318 else
4319 PyErr_Clear();
4320 break;
4321 }
4322 if (skip_leading_underscores &&
4323 PyUnicode_Check(name) &&
4324 PyUnicode_AS_UNICODE(name)[0] == '_')
4325 {
4326 Py_DECREF(name);
4327 continue;
4328 }
4329 value = PyObject_GetAttr(v, name);
4330 if (value == NULL)
4331 err = -1;
4332 else if (PyDict_CheckExact(locals))
4333 err = PyDict_SetItem(locals, name, value);
4334 else
4335 err = PyObject_SetItem(locals, name, value);
4336 Py_DECREF(name);
4337 Py_XDECREF(value);
4338 if (err != 0)
4339 break;
4340 }
4341 Py_DECREF(all);
4342 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004343}
4344
Guido van Rossumac7be682001-01-17 15:42:30 +00004345static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004346format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004347{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004348 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004350 if (!obj)
4351 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004353 obj_str = _PyUnicode_AsString(obj);
4354 if (!obj_str)
4355 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004357 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00004358}
Guido van Rossum950361c1997-01-24 13:49:28 +00004359
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00004360static void
4361format_exc_unbound(PyCodeObject *co, int oparg)
4362{
4363 PyObject *name;
4364 /* Don't stomp existing exception */
4365 if (PyErr_Occurred())
4366 return;
4367 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
4368 name = PyTuple_GET_ITEM(co->co_cellvars,
4369 oparg);
4370 format_exc_check_arg(
4371 PyExc_UnboundLocalError,
4372 UNBOUNDLOCAL_ERROR_MSG,
4373 name);
4374 } else {
4375 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
4376 PyTuple_GET_SIZE(co->co_cellvars));
4377 format_exc_check_arg(PyExc_NameError,
4378 UNBOUNDFREE_ERROR_MSG, name);
4379 }
4380}
4381
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004382static PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +00004383unicode_concatenate(PyObject *v, PyObject *w,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004384 PyFrameObject *f, unsigned char *next_instr)
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004385{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004386 /* This function implements 'variable += expr' when both arguments
4387 are (Unicode) strings. */
4388 Py_ssize_t v_len = PyUnicode_GET_SIZE(v);
4389 Py_ssize_t w_len = PyUnicode_GET_SIZE(w);
4390 Py_ssize_t new_len = v_len + w_len;
4391 if (new_len < 0) {
4392 PyErr_SetString(PyExc_OverflowError,
4393 "strings are too large to concat");
4394 return NULL;
4395 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00004396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004397 if (v->ob_refcnt == 2) {
4398 /* In the common case, there are 2 references to the value
4399 * stored in 'variable' when the += is performed: one on the
4400 * value stack (in 'v') and one still stored in the
4401 * 'variable'. We try to delete the variable now to reduce
4402 * the refcnt to 1.
4403 */
4404 switch (*next_instr) {
4405 case STORE_FAST:
4406 {
4407 int oparg = PEEKARG();
4408 PyObject **fastlocals = f->f_localsplus;
4409 if (GETLOCAL(oparg) == v)
4410 SETLOCAL(oparg, NULL);
4411 break;
4412 }
4413 case STORE_DEREF:
4414 {
4415 PyObject **freevars = (f->f_localsplus +
4416 f->f_code->co_nlocals);
4417 PyObject *c = freevars[PEEKARG()];
4418 if (PyCell_GET(c) == v)
4419 PyCell_Set(c, NULL);
4420 break;
4421 }
4422 case STORE_NAME:
4423 {
4424 PyObject *names = f->f_code->co_names;
4425 PyObject *name = GETITEM(names, PEEKARG());
4426 PyObject *locals = f->f_locals;
4427 if (PyDict_CheckExact(locals) &&
4428 PyDict_GetItem(locals, name) == v) {
4429 if (PyDict_DelItem(locals, name) != 0) {
4430 PyErr_Clear();
4431 }
4432 }
4433 break;
4434 }
4435 }
4436 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004437
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004438 if (v->ob_refcnt == 1 && !PyUnicode_CHECK_INTERNED(v)) {
4439 /* Now we own the last reference to 'v', so we can resize it
4440 * in-place.
4441 */
4442 if (PyUnicode_Resize(&v, new_len) != 0) {
4443 /* XXX if PyUnicode_Resize() fails, 'v' has been
4444 * deallocated so it cannot be put back into
4445 * 'variable'. The MemoryError is raised when there
4446 * is no value in 'variable', which might (very
4447 * remotely) be a cause of incompatibilities.
4448 */
4449 return NULL;
4450 }
4451 /* copy 'w' into the newly allocated area of 'v' */
4452 memcpy(PyUnicode_AS_UNICODE(v) + v_len,
4453 PyUnicode_AS_UNICODE(w), w_len*sizeof(Py_UNICODE));
4454 return v;
4455 }
4456 else {
4457 /* When in-place resizing is not an option. */
4458 w = PyUnicode_Concat(v, w);
4459 Py_DECREF(v);
4460 return w;
4461 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004462}
4463
Guido van Rossum950361c1997-01-24 13:49:28 +00004464#ifdef DYNAMIC_EXECUTION_PROFILE
4465
Skip Montanarof118cb12001-10-15 20:51:38 +00004466static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004467getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004468{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004469 int i;
4470 PyObject *l = PyList_New(256);
4471 if (l == NULL) return NULL;
4472 for (i = 0; i < 256; i++) {
4473 PyObject *x = PyLong_FromLong(a[i]);
4474 if (x == NULL) {
4475 Py_DECREF(l);
4476 return NULL;
4477 }
4478 PyList_SetItem(l, i, x);
4479 }
4480 for (i = 0; i < 256; i++)
4481 a[i] = 0;
4482 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004483}
4484
4485PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004486_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004487{
4488#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004489 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00004490#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004491 int i;
4492 PyObject *l = PyList_New(257);
4493 if (l == NULL) return NULL;
4494 for (i = 0; i < 257; i++) {
4495 PyObject *x = getarray(dxpairs[i]);
4496 if (x == NULL) {
4497 Py_DECREF(l);
4498 return NULL;
4499 }
4500 PyList_SetItem(l, i, x);
4501 }
4502 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004503#endif
4504}
4505
4506#endif