blob: 48b567865296ff15ea6d769f130669262b194ad9 [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
Antoine Pitrou1df15362010-09-13 14:16:46 +0000316_PyEval_FiniThreads(void)
317{
318 if (!gil_created())
319 return;
320 destroy_gil();
321 assert(!gil_created());
322}
323
324void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000325PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000326{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 PyThreadState *tstate = PyThreadState_GET();
328 if (tstate == NULL)
329 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
330 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000331}
332
333void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000334PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000335{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 /* This function must succeed when the current thread state is NULL.
337 We therefore avoid PyThreadState_GET() which dumps a fatal error
338 in debug mode.
339 */
340 drop_gil((PyThreadState*)_Py_atomic_load_relaxed(
341 &_PyThreadState_Current));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000342}
343
344void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000345PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000346{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 if (tstate == NULL)
348 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
349 /* Check someone has called PyEval_InitThreads() to create the lock */
350 assert(gil_created());
351 take_gil(tstate);
352 if (PyThreadState_Swap(tstate) != NULL)
353 Py_FatalError(
354 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000355}
356
357void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000358PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000359{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360 if (tstate == NULL)
361 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
362 if (PyThreadState_Swap(NULL) != tstate)
363 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
364 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000365}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000366
367/* This function is called from PyOS_AfterFork to ensure that newly
368 created child processes don't hold locks referring to threads which
369 are not running in the child process. (This could also be done using
370 pthread_atfork mechanism, at least for the pthreads implementation.) */
371
372void
373PyEval_ReInitThreads(void)
374{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 PyObject *threading, *result;
376 PyThreadState *tstate = PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000377
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 if (!gil_created())
379 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 recreate_gil();
381 pending_lock = PyThread_allocate_lock();
382 take_gil(tstate);
383 main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 /* Update the threading module with the new state.
386 */
387 tstate = PyThreadState_GET();
388 threading = PyMapping_GetItemString(tstate->interp->modules,
389 "threading");
390 if (threading == NULL) {
391 /* threading not imported */
392 PyErr_Clear();
393 return;
394 }
395 result = PyObject_CallMethod(threading, "_after_fork", NULL);
396 if (result == NULL)
397 PyErr_WriteUnraisable(threading);
398 else
399 Py_DECREF(result);
400 Py_DECREF(threading);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000401}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000402
403#else
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000404static _Py_atomic_int eval_breaker = {0};
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000405static int pending_async_exc = 0;
406#endif /* WITH_THREAD */
407
408/* This function is used to signal that async exceptions are waiting to be
409 raised, therefore it is also useful in non-threaded builds. */
410
411void
412_PyEval_SignalAsyncExc(void)
413{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000415}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000416
Guido van Rossumff4949e1992-08-05 19:58:53 +0000417/* Functions save_thread and restore_thread are always defined so
418 dynamically loaded modules needn't be compiled separately for use
419 with and without threads: */
420
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000421PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000422PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000423{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 PyThreadState *tstate = PyThreadState_Swap(NULL);
425 if (tstate == NULL)
426 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000427#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 if (gil_created())
429 drop_gil(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000430#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000432}
433
434void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000435PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000436{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000437 if (tstate == NULL)
438 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000439#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 if (gil_created()) {
441 int err = errno;
442 take_gil(tstate);
443 errno = err;
444 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000445#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000447}
448
449
Guido van Rossuma9672091994-09-14 13:31:22 +0000450/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
451 signal handlers or Mac I/O completion routines) can schedule calls
452 to a function to be called synchronously.
453 The synchronous function is called with one void* argument.
454 It should return 0 for success or -1 for failure -- failure should
455 be accompanied by an exception.
456
457 If registry succeeds, the registry function returns 0; if it fails
458 (e.g. due to too many pending calls) it returns -1 (without setting
459 an exception condition).
460
461 Note that because registry may occur from within signal handlers,
462 or other asynchronous events, calling malloc() is unsafe!
463
464#ifdef WITH_THREAD
465 Any thread can schedule pending calls, but only the main thread
466 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000467 There is no facility to schedule calls to a particular thread, but
468 that should be easy to change, should that ever be required. In
469 that case, the static variables here should go into the python
470 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000471#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000472*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000473
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000474#ifdef WITH_THREAD
475
476/* The WITH_THREAD implementation is thread-safe. It allows
477 scheduling to be made from any thread, and even from an executing
478 callback.
479 */
480
481#define NPENDINGCALLS 32
482static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 int (*func)(void *);
484 void *arg;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000485} pendingcalls[NPENDINGCALLS];
486static int pendingfirst = 0;
487static int pendinglast = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000488static char pendingbusy = 0;
489
490int
491Py_AddPendingCall(int (*func)(void *), void *arg)
492{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 int i, j, result=0;
494 PyThread_type_lock lock = pending_lock;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 /* try a few times for the lock. Since this mechanism is used
497 * for signal handling (on the main thread), there is a (slim)
498 * chance that a signal is delivered on the same thread while we
499 * hold the lock during the Py_MakePendingCalls() function.
500 * This avoids a deadlock in that case.
501 * Note that signals can be delivered on any thread. In particular,
502 * on Windows, a SIGINT is delivered on a system-created worker
503 * thread.
504 * We also check for lock being NULL, in the unlikely case that
505 * this function is called before any bytecode evaluation takes place.
506 */
507 if (lock != NULL) {
508 for (i = 0; i<100; i++) {
509 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
510 break;
511 }
512 if (i == 100)
513 return -1;
514 }
515
516 i = pendinglast;
517 j = (i + 1) % NPENDINGCALLS;
518 if (j == pendingfirst) {
519 result = -1; /* Queue full */
520 } else {
521 pendingcalls[i].func = func;
522 pendingcalls[i].arg = arg;
523 pendinglast = j;
524 }
525 /* signal main loop */
526 SIGNAL_PENDING_CALLS();
527 if (lock != NULL)
528 PyThread_release_lock(lock);
529 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000530}
531
532int
533Py_MakePendingCalls(void)
534{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 int i;
536 int r = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000537
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 if (!pending_lock) {
539 /* initial allocation of the lock */
540 pending_lock = PyThread_allocate_lock();
541 if (pending_lock == NULL)
542 return -1;
543 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 /* only service pending calls on main thread */
546 if (main_thread && PyThread_get_thread_ident() != main_thread)
547 return 0;
548 /* don't perform recursive pending calls */
549 if (pendingbusy)
550 return 0;
551 pendingbusy = 1;
552 /* perform a bounded number of calls, in case of recursion */
553 for (i=0; i<NPENDINGCALLS; i++) {
554 int j;
555 int (*func)(void *);
556 void *arg = NULL;
557
558 /* pop one item off the queue while holding the lock */
559 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
560 j = pendingfirst;
561 if (j == pendinglast) {
562 func = NULL; /* Queue empty */
563 } else {
564 func = pendingcalls[j].func;
565 arg = pendingcalls[j].arg;
566 pendingfirst = (j + 1) % NPENDINGCALLS;
567 }
568 if (pendingfirst != pendinglast)
569 SIGNAL_PENDING_CALLS();
570 else
571 UNSIGNAL_PENDING_CALLS();
572 PyThread_release_lock(pending_lock);
573 /* having released the lock, perform the callback */
574 if (func == NULL)
575 break;
576 r = func(arg);
577 if (r)
578 break;
579 }
580 pendingbusy = 0;
581 return r;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000582}
583
584#else /* if ! defined WITH_THREAD */
585
586/*
587 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
588 This code is used for signal handling in python that isn't built
589 with WITH_THREAD.
590 Don't use this implementation when Py_AddPendingCalls() can happen
591 on a different thread!
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000592
Guido van Rossuma9672091994-09-14 13:31:22 +0000593 There are two possible race conditions:
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000594 (1) nested asynchronous calls to Py_AddPendingCall()
595 (2) AddPendingCall() calls made while pending calls are being processed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000597 (1) is very unlikely because typically signal delivery
598 is blocked during signal handling. So it should be impossible.
599 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000600 The current code is safe against (2), but not against (1).
601 The safety against (2) is derived from the fact that only one
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000602 thread is present, interrupted by signals, and that the critical
603 section is protected with the "busy" variable. On Windows, which
604 delivers SIGINT on a system thread, this does not hold and therefore
605 Windows really shouldn't use this version.
606 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000607*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000608
Guido van Rossuma9672091994-09-14 13:31:22 +0000609#define NPENDINGCALLS 32
610static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000611 int (*func)(void *);
612 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000613} pendingcalls[NPENDINGCALLS];
614static volatile int pendingfirst = 0;
615static volatile int pendinglast = 0;
Benjamin Peterson08ec84c2010-05-30 14:49:32 +0000616static _Py_atomic_int pendingcalls_to_do = {0};
Guido van Rossuma9672091994-09-14 13:31:22 +0000617
618int
Thomas Wouters334fb892000-07-25 12:56:38 +0000619Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000620{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 static volatile int busy = 0;
622 int i, j;
623 /* XXX Begin critical section */
624 if (busy)
625 return -1;
626 busy = 1;
627 i = pendinglast;
628 j = (i + 1) % NPENDINGCALLS;
629 if (j == pendingfirst) {
630 busy = 0;
631 return -1; /* Queue full */
632 }
633 pendingcalls[i].func = func;
634 pendingcalls[i].arg = arg;
635 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 SIGNAL_PENDING_CALLS();
638 busy = 0;
639 /* XXX End critical section */
640 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000641}
642
Guido van Rossum180d7b41994-09-29 09:45:57 +0000643int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000644Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000645{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 static int busy = 0;
647 if (busy)
648 return 0;
649 busy = 1;
650 UNSIGNAL_PENDING_CALLS();
651 for (;;) {
652 int i;
653 int (*func)(void *);
654 void *arg;
655 i = pendingfirst;
656 if (i == pendinglast)
657 break; /* Queue empty */
658 func = pendingcalls[i].func;
659 arg = pendingcalls[i].arg;
660 pendingfirst = (i + 1) % NPENDINGCALLS;
661 if (func(arg) < 0) {
662 busy = 0;
663 SIGNAL_PENDING_CALLS(); /* We're not done yet */
664 return -1;
665 }
666 }
667 busy = 0;
668 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000669}
670
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000671#endif /* WITH_THREAD */
672
Guido van Rossuma9672091994-09-14 13:31:22 +0000673
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000674/* The interpreter's recursion limit */
675
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000676#ifndef Py_DEFAULT_RECURSION_LIMIT
677#define Py_DEFAULT_RECURSION_LIMIT 1000
678#endif
679static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
680int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000681
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000682int
683Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000684{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000685 return recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000686}
687
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000688void
689Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000690{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 recursion_limit = new_limit;
692 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000693}
694
Armin Rigo2b3eb402003-10-28 12:05:48 +0000695/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
696 if the recursion_depth reaches _Py_CheckRecursionLimit.
697 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
698 to guarantee that _Py_CheckRecursiveCall() is regularly called.
699 Without USE_STACKCHECK, there is no need for this. */
700int
701_Py_CheckRecursiveCall(char *where)
702{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 PyThreadState *tstate = PyThreadState_GET();
Armin Rigo2b3eb402003-10-28 12:05:48 +0000704
705#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 if (PyOS_CheckStack()) {
707 --tstate->recursion_depth;
708 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
709 return -1;
710 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000711#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000712 _Py_CheckRecursionLimit = recursion_limit;
713 if (tstate->recursion_critical)
714 /* Somebody asked that we don't check for recursion. */
715 return 0;
716 if (tstate->overflowed) {
717 if (tstate->recursion_depth > recursion_limit + 50) {
718 /* Overflowing while handling an overflow. Give up. */
719 Py_FatalError("Cannot recover from stack overflow.");
720 }
721 return 0;
722 }
723 if (tstate->recursion_depth > recursion_limit) {
724 --tstate->recursion_depth;
725 tstate->overflowed = 1;
726 PyErr_Format(PyExc_RuntimeError,
727 "maximum recursion depth exceeded%s",
728 where);
729 return -1;
730 }
731 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000732}
733
Guido van Rossum374a9221991-04-04 10:40:29 +0000734/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000735enum why_code {
Stefan Krahb7e10102010-06-23 18:42:39 +0000736 WHY_NOT = 0x0001, /* No error */
737 WHY_EXCEPTION = 0x0002, /* Exception occurred */
738 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
739 WHY_RETURN = 0x0008, /* 'return' statement */
740 WHY_BREAK = 0x0010, /* 'break' statement */
741 WHY_CONTINUE = 0x0020, /* 'continue' statement */
742 WHY_YIELD = 0x0040, /* 'yield' operator */
743 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000744};
Guido van Rossum374a9221991-04-04 10:40:29 +0000745
Collin Winter828f04a2007-08-31 00:04:24 +0000746static enum why_code do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000747static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000748
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +0000749/* Records whether tracing is on for any thread. Counts the number of
750 threads for which tstate->c_tracefunc is non-NULL, so if the value
751 is 0, we know we don't have to check this thread's c_tracefunc.
752 This speeds up the if statement in PyEval_EvalFrameEx() after
753 fast_next_opcode*/
754static int _Py_TracingPossible = 0;
755
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000756
Guido van Rossum374a9221991-04-04 10:40:29 +0000757
Guido van Rossumb209a111997-04-29 18:18:01 +0000758PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000759PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000760{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 return PyEval_EvalCodeEx(co,
762 globals, locals,
763 (PyObject **)NULL, 0,
764 (PyObject **)NULL, 0,
765 (PyObject **)NULL, 0,
766 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000767}
768
769
770/* Interpreter main loop */
771
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000772PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000773PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 /* This is for backward compatibility with extension modules that
775 used this API; core interpreter code should call
776 PyEval_EvalFrameEx() */
777 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000778}
779
780PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000781PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000782{
Guido van Rossum950361c1997-01-24 13:49:28 +0000783#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000785#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 register PyObject **stack_pointer; /* Next free slot in value stack */
787 register unsigned char *next_instr;
788 register int opcode; /* Current opcode */
789 register int oparg; /* Current opcode argument, if any */
790 register enum why_code why; /* Reason for block stack unwind */
791 register int err; /* Error status -- nonzero if error */
792 register PyObject *x; /* Result object -- NULL if error */
793 register PyObject *v; /* Temporary objects popped off stack */
794 register PyObject *w;
795 register PyObject *u;
796 register PyObject *t;
797 register PyObject **fastlocals, **freevars;
798 PyObject *retval = NULL; /* Return value */
799 PyThreadState *tstate = PyThreadState_GET();
800 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000801
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000803
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000805
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 is true when the line being executed has changed. The
807 initial values are such as to make this false the first
808 time it is tested. */
809 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000810
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 unsigned char *first_instr;
812 PyObject *names;
813 PyObject *consts;
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000814#if defined(Py_DEBUG) || defined(LLTRACE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 /* Make it easier to find out where we are with a debugger */
816 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000817#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000818
Antoine Pitroub52ec782009-01-25 16:34:23 +0000819/* Computed GOTOs, or
820 the-optimization-commonly-but-improperly-known-as-"threaded code"
821 using gcc's labels-as-values extension
822 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
823
824 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000826 combined with a lookup table of jump addresses. However, since the
827 indirect jump instruction is shared by all opcodes, the CPU will have a
828 hard time making the right prediction for where to jump next (actually,
829 it will be always wrong except in the uncommon case of a sequence of
830 several identical opcodes).
831
832 "Threaded code" in contrast, uses an explicit jump table and an explicit
833 indirect jump instruction at the end of each opcode. Since the jump
834 instruction is at a different address for each opcode, the CPU will make a
835 separate prediction for each of these instructions, which is equivalent to
836 predicting the second opcode of each opcode pair. These predictions have
837 a much better chance to turn out valid, especially in small bytecode loops.
838
839 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000841 and potentially many more instructions (depending on the pipeline width).
842 A correctly predicted branch, however, is nearly free.
843
844 At the time of this writing, the "threaded code" version is up to 15-20%
845 faster than the normal "switch" version, depending on the compiler and the
846 CPU architecture.
847
848 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
849 because it would render the measurements invalid.
850
851
852 NOTE: care must be taken that the compiler doesn't try to "optimize" the
853 indirect jumps by sharing them between all opcodes. Such optimizations
854 can be disabled on gcc by using the -fno-gcse flag (or possibly
855 -fno-crossjumping).
856*/
857
Antoine Pitrou042b1282010-08-13 21:15:58 +0000858#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000859#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000860#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000861#endif
862
Antoine Pitrou042b1282010-08-13 21:15:58 +0000863#ifdef HAVE_COMPUTED_GOTOS
864 #ifndef USE_COMPUTED_GOTOS
865 #define USE_COMPUTED_GOTOS 1
866 #endif
867#else
868 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
869 #error "Computed gotos are not supported on this compiler."
870 #endif
871 #undef USE_COMPUTED_GOTOS
872 #define USE_COMPUTED_GOTOS 0
873#endif
874
875#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000876/* Import the static jump table */
877#include "opcode_targets.h"
878
879/* This macro is used when several opcodes defer to the same implementation
880 (e.g. SETUP_LOOP, SETUP_FINALLY) */
881#define TARGET_WITH_IMPL(op, impl) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 TARGET_##op: \
883 opcode = op; \
884 if (HAS_ARG(op)) \
885 oparg = NEXTARG(); \
886 case op: \
887 goto impl; \
Antoine Pitroub52ec782009-01-25 16:34:23 +0000888
889#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 TARGET_##op: \
891 opcode = op; \
892 if (HAS_ARG(op)) \
893 oparg = NEXTARG(); \
894 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000895
896
897#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 { \
899 if (!_Py_atomic_load_relaxed(&eval_breaker)) { \
900 FAST_DISPATCH(); \
901 } \
902 continue; \
903 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000904
905#ifdef LLTRACE
906#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 { \
908 if (!lltrace && !_Py_TracingPossible) { \
909 f->f_lasti = INSTR_OFFSET(); \
910 goto *opcode_targets[*next_instr++]; \
911 } \
912 goto fast_next_opcode; \
913 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000914#else
915#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 { \
917 if (!_Py_TracingPossible) { \
918 f->f_lasti = INSTR_OFFSET(); \
919 goto *opcode_targets[*next_instr++]; \
920 } \
921 goto fast_next_opcode; \
922 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000923#endif
924
925#else
926#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000928#define TARGET_WITH_IMPL(op, impl) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 /* silence compiler warnings about `impl` unused */ \
930 if (0) goto impl; \
931 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000932#define DISPATCH() continue
933#define FAST_DISPATCH() goto fast_next_opcode
934#endif
935
936
Neal Norwitza81d2202002-07-14 00:27:26 +0000937/* Tuple access macros */
938
939#ifndef Py_DEBUG
940#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
941#else
942#define GETITEM(v, i) PyTuple_GetItem((v), (i))
943#endif
944
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000945#ifdef WITH_TSC
946/* Use Pentium timestamp counter to mark certain events:
947 inst0 -- beginning of switch statement for opcode dispatch
948 inst1 -- end of switch statement (may be skipped)
949 loop0 -- the top of the mainloop
Thomas Wouters477c8d52006-05-27 19:21:47 +0000950 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000951 (may be skipped)
952 intr1 -- beginning of long interruption
953 intr2 -- end of long interruption
954
955 Many opcodes call out to helper C functions. In some cases, the
956 time in those functions should be counted towards the time for the
957 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
958 calls another Python function; there's no point in charge all the
959 bytecode executed by the called function to the caller.
960
961 It's hard to make a useful judgement statically. In the presence
962 of operator overloading, it's impossible to tell if a call will
963 execute new Python code or not.
964
965 It's a case-by-case judgement. I'll use intr1 for the following
966 cases:
967
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000968 IMPORT_STAR
969 IMPORT_FROM
970 CALL_FUNCTION (and friends)
971
972 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
974 int ticked = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000975
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 READ_TIMESTAMP(inst0);
977 READ_TIMESTAMP(inst1);
978 READ_TIMESTAMP(loop0);
979 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 /* shut up the compiler */
982 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000983#endif
984
Guido van Rossum374a9221991-04-04 10:40:29 +0000985/* Code access macros */
986
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987#define INSTR_OFFSET() ((int)(next_instr - first_instr))
988#define NEXTOP() (*next_instr++)
989#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
990#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
991#define JUMPTO(x) (next_instr = first_instr + (x))
992#define JUMPBY(x) (next_instr += (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000993
Raymond Hettingerf606f872003-03-16 03:11:04 +0000994/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 Some opcodes tend to come in pairs thus making it possible to
996 predict the second code when the first is run. For example,
997 COMPARE_OP is often followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And,
998 those opcodes are often followed by a POP_TOP.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000999
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 Verifying the prediction costs a single high-speed test of a register
1001 variable against a constant. If the pairing was good, then the
1002 processor's own internal branch predication has a high likelihood of
1003 success, resulting in a nearly zero-overhead transition to the
1004 next opcode. A successful prediction saves a trip through the eval-loop
1005 including its two unpredictable branches, the HAS_ARG test and the
1006 switch-case. Combined with the processor's internal branch prediction,
1007 a successful PREDICT has the effect of making the two opcodes run as if
1008 they were a single new opcode with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001009
Georg Brandl86b2fb92008-07-16 03:43:04 +00001010 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 predictions turned-on and interpret the results as if some opcodes
1012 had been combined or turn-off predictions so that the opcode frequency
1013 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001014
1015 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 the CPU to record separate branch prediction information for each
1017 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001018
Raymond Hettingerf606f872003-03-16 03:11:04 +00001019*/
1020
Antoine Pitrou042b1282010-08-13 21:15:58 +00001021#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022#define PREDICT(op) if (0) goto PRED_##op
1023#define PREDICTED(op) PRED_##op:
1024#define PREDICTED_WITH_ARG(op) PRED_##op:
Raymond Hettingera7216982004-02-08 19:59:27 +00001025#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026#define PREDICT(op) if (*next_instr == op) goto PRED_##op
1027#define PREDICTED(op) PRED_##op: next_instr++
1028#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Antoine Pitroub52ec782009-01-25 16:34:23 +00001029#endif
1030
Raymond Hettingerf606f872003-03-16 03:11:04 +00001031
Guido van Rossum374a9221991-04-04 10:40:29 +00001032/* Stack manipulation macros */
1033
Martin v. Löwis18e16552006-02-15 17:27:45 +00001034/* The stack can grow at most MAXINT deep, as co_nlocals and
1035 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001036#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1037#define EMPTY() (STACK_LEVEL() == 0)
1038#define TOP() (stack_pointer[-1])
1039#define SECOND() (stack_pointer[-2])
1040#define THIRD() (stack_pointer[-3])
1041#define FOURTH() (stack_pointer[-4])
1042#define PEEK(n) (stack_pointer[-(n)])
1043#define SET_TOP(v) (stack_pointer[-1] = (v))
1044#define SET_SECOND(v) (stack_pointer[-2] = (v))
1045#define SET_THIRD(v) (stack_pointer[-3] = (v))
1046#define SET_FOURTH(v) (stack_pointer[-4] = (v))
1047#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
1048#define BASIC_STACKADJ(n) (stack_pointer += n)
1049#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1050#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001051
Guido van Rossum96a42c81992-01-12 02:29:51 +00001052#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001054 lltrace && prtrace(TOP(), "push")); \
1055 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001057 BASIC_POP())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001059 lltrace && prtrace(TOP(), "stackadj")); \
1060 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +00001061#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +00001062 prtrace((STACK_POINTER)[-1], "ext_pop")), \
1063 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001064#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001065#define PUSH(v) BASIC_PUSH(v)
1066#define POP() BASIC_POP()
1067#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001068#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001069#endif
1070
Guido van Rossum681d79a1995-07-18 14:51:37 +00001071/* Local variable macros */
1072
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001074
1075/* The SETLOCAL() macro must not DECREF the local variable in-place and
1076 then store the new value; it must copy the old value to a temporary
1077 value, then store the new value, and then DECREF the temporary value.
1078 This is because it is possible that during the DECREF the frame is
1079 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1080 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001081#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001082 GETLOCAL(i) = value; \
1083 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001084
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001085
1086#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001087 while (STACK_LEVEL() > (b)->b_level) { \
1088 PyObject *v = POP(); \
1089 Py_XDECREF(v); \
1090 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001091
1092#define UNWIND_EXCEPT_HANDLER(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 { \
1094 PyObject *type, *value, *traceback; \
1095 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1096 while (STACK_LEVEL() > (b)->b_level + 3) { \
1097 value = POP(); \
1098 Py_XDECREF(value); \
1099 } \
1100 type = tstate->exc_type; \
1101 value = tstate->exc_value; \
1102 traceback = tstate->exc_traceback; \
1103 tstate->exc_type = POP(); \
1104 tstate->exc_value = POP(); \
1105 tstate->exc_traceback = POP(); \
1106 Py_XDECREF(type); \
1107 Py_XDECREF(value); \
1108 Py_XDECREF(traceback); \
1109 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001110
1111#define SAVE_EXC_STATE() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001112 { \
1113 PyObject *type, *value, *traceback; \
1114 Py_XINCREF(tstate->exc_type); \
1115 Py_XINCREF(tstate->exc_value); \
1116 Py_XINCREF(tstate->exc_traceback); \
1117 type = f->f_exc_type; \
1118 value = f->f_exc_value; \
1119 traceback = f->f_exc_traceback; \
1120 f->f_exc_type = tstate->exc_type; \
1121 f->f_exc_value = tstate->exc_value; \
1122 f->f_exc_traceback = tstate->exc_traceback; \
1123 Py_XDECREF(type); \
1124 Py_XDECREF(value); \
1125 Py_XDECREF(traceback); \
1126 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001127
1128#define SWAP_EXC_STATE() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 { \
1130 PyObject *tmp; \
1131 tmp = tstate->exc_type; \
1132 tstate->exc_type = f->f_exc_type; \
1133 f->f_exc_type = tmp; \
1134 tmp = tstate->exc_value; \
1135 tstate->exc_value = f->f_exc_value; \
1136 f->f_exc_value = tmp; \
1137 tmp = tstate->exc_traceback; \
1138 tstate->exc_traceback = f->f_exc_traceback; \
1139 f->f_exc_traceback = tmp; \
1140 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001141
Guido van Rossuma027efa1997-05-05 20:56:21 +00001142/* Start of code */
1143
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001144 if (f == NULL)
1145 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001146
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 /* push frame */
1148 if (Py_EnterRecursiveCall(""))
1149 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001150
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001152
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 if (tstate->use_tracing) {
1154 if (tstate->c_tracefunc != NULL) {
1155 /* tstate->c_tracefunc, if defined, is a
1156 function that will be called on *every* entry
1157 to a code block. Its return value, if not
1158 None, is a function that will be called at
1159 the start of each executed line of code.
1160 (Actually, the function must return itself
1161 in order to continue tracing.) The trace
1162 functions are called with three arguments:
1163 a pointer to the current frame, a string
1164 indicating why the function is called, and
1165 an argument which depends on the situation.
1166 The global trace function is also called
1167 whenever an exception is detected. */
1168 if (call_trace_protected(tstate->c_tracefunc,
1169 tstate->c_traceobj,
1170 f, PyTrace_CALL, Py_None)) {
1171 /* Trace function raised an error */
1172 goto exit_eval_frame;
1173 }
1174 }
1175 if (tstate->c_profilefunc != NULL) {
1176 /* Similar for c_profilefunc, except it needn't
1177 return itself and isn't called for "line" events */
1178 if (call_trace_protected(tstate->c_profilefunc,
1179 tstate->c_profileobj,
1180 f, PyTrace_CALL, Py_None)) {
1181 /* Profile function raised an error */
1182 goto exit_eval_frame;
1183 }
1184 }
1185 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001186
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 co = f->f_code;
1188 names = co->co_names;
1189 consts = co->co_consts;
1190 fastlocals = f->f_localsplus;
1191 freevars = f->f_localsplus + co->co_nlocals;
1192 first_instr = (unsigned char*) PyBytes_AS_STRING(co->co_code);
1193 /* An explanation is in order for the next line.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001194
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 f->f_lasti now refers to the index of the last instruction
1196 executed. You might think this was obvious from the name, but
1197 this wasn't always true before 2.3! PyFrame_New now sets
1198 f->f_lasti to -1 (i.e. the index *before* the first instruction)
1199 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
1200 does work. Promise.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001201
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 When the PREDICT() macros are enabled, some opcode pairs follow in
1203 direct succession without updating f->f_lasti. A successful
1204 prediction effectively links the two codes together as if they
1205 were a single new opcode; accordingly,f->f_lasti will point to
1206 the first code in the pair (for instance, GET_ITER followed by
1207 FOR_ITER is effectively a single opcode and f->f_lasti will point
1208 at to the beginning of the combined pair.)
1209 */
1210 next_instr = first_instr + f->f_lasti + 1;
1211 stack_pointer = f->f_stacktop;
1212 assert(stack_pointer != NULL);
1213 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001214
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 if (co->co_flags & CO_GENERATOR && !throwflag) {
1216 if (f->f_exc_type != NULL && f->f_exc_type != Py_None) {
1217 /* We were in an except handler when we left,
1218 restore the exception state which was put aside
1219 (see YIELD_VALUE). */
1220 SWAP_EXC_STATE();
1221 }
1222 else {
1223 SAVE_EXC_STATE();
1224 }
1225 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001226
Tim Peters5ca576e2001-06-18 22:08:13 +00001227#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001228 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001229#endif
Neal Norwitz5f5153e2005-10-21 04:28:38 +00001230#if defined(Py_DEBUG) || defined(LLTRACE)
Victor Stinner4a3733d2010-08-17 00:39:57 +00001231 {
1232 PyObject *error_type, *error_value, *error_traceback;
1233 PyErr_Fetch(&error_type, &error_value, &error_traceback);
1234 filename = _PyUnicode_AsString(co->co_filename);
1235 PyErr_Restore(error_type, error_value, error_traceback);
1236 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001237#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 why = WHY_NOT;
1240 err = 0;
1241 x = Py_None; /* Not a reference, just anything non-NULL */
1242 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00001243
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244 if (throwflag) { /* support for generator.throw() */
1245 why = WHY_EXCEPTION;
1246 goto on_error;
1247 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001248
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001250#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 if (inst1 == 0) {
1252 /* Almost surely, the opcode executed a break
1253 or a continue, preventing inst1 from being set
1254 on the way out of the loop.
1255 */
1256 READ_TIMESTAMP(inst1);
1257 loop1 = inst1;
1258 }
1259 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
1260 intr0, intr1);
1261 ticked = 0;
1262 inst1 = 0;
1263 intr0 = 0;
1264 intr1 = 0;
1265 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001266#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1268 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001270 /* Do periodic things. Doing this every time through
1271 the loop would add too much overhead, so we do it
1272 only every Nth instruction. We also do it if
1273 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1274 event needs attention (e.g. a signal handler or
1275 async I/O handler); see Py_AddPendingCall() and
1276 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001278 if (_Py_atomic_load_relaxed(&eval_breaker)) {
1279 if (*next_instr == SETUP_FINALLY) {
1280 /* Make the last opcode before
1281 a try: finally: block uninterruptable. */
1282 goto fast_next_opcode;
1283 }
1284 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001285#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001286 ticked = 1;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001287#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) {
1289 if (Py_MakePendingCalls() < 0) {
1290 why = WHY_EXCEPTION;
1291 goto on_error;
1292 }
1293 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001294#ifdef WITH_THREAD
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001295 if (_Py_atomic_load_relaxed(&gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296 /* Give another thread a chance */
1297 if (PyThreadState_Swap(NULL) != tstate)
1298 Py_FatalError("ceval: tstate mix-up");
1299 drop_gil(tstate);
1300
1301 /* Other threads may run now */
1302
1303 take_gil(tstate);
1304 if (PyThreadState_Swap(tstate) != NULL)
1305 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 }
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001307#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 /* Check for asynchronous exceptions. */
1309 if (tstate->async_exc != NULL) {
1310 x = tstate->async_exc;
1311 tstate->async_exc = NULL;
1312 UNSIGNAL_ASYNC_EXC();
1313 PyErr_SetNone(x);
1314 Py_DECREF(x);
1315 why = WHY_EXCEPTION;
1316 goto on_error;
1317 }
1318 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001320 fast_next_opcode:
1321 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001323 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001325 if (_Py_TracingPossible &&
1326 tstate->c_tracefunc != NULL && !tstate->tracing) {
1327 /* see maybe_call_line_trace
1328 for expository comments */
1329 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001331 err = maybe_call_line_trace(tstate->c_tracefunc,
1332 tstate->c_traceobj,
1333 f, &instr_lb, &instr_ub,
1334 &instr_prev);
1335 /* Reload possibly changed frame fields */
1336 JUMPTO(f->f_lasti);
1337 if (f->f_stacktop != NULL) {
1338 stack_pointer = f->f_stacktop;
1339 f->f_stacktop = NULL;
1340 }
1341 if (err) {
1342 /* trace function raised an exception */
1343 goto on_error;
1344 }
1345 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001347 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001348
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001349 opcode = NEXTOP();
1350 oparg = 0; /* allows oparg to be stored in a register because
1351 it doesn't have to be remembered across a full loop */
1352 if (HAS_ARG(opcode))
1353 oparg = NEXTARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001354 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001355#ifdef DYNAMIC_EXECUTION_PROFILE
1356#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001357 dxpairs[lastopcode][opcode]++;
1358 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001359#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001361#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001362
Guido van Rossum96a42c81992-01-12 02:29:51 +00001363#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001364 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 if (lltrace) {
1367 if (HAS_ARG(opcode)) {
1368 printf("%d: %d, %d\n",
1369 f->f_lasti, opcode, oparg);
1370 }
1371 else {
1372 printf("%d: %d\n",
1373 f->f_lasti, opcode);
1374 }
1375 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001376#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001377
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 /* Main switch on opcode */
1379 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001381 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 /* BEWARE!
1384 It is essential that any operation that fails sets either
1385 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1386 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 TARGET(NOP)
1391 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 TARGET(LOAD_FAST)
1394 x = GETLOCAL(oparg);
1395 if (x != NULL) {
1396 Py_INCREF(x);
1397 PUSH(x);
1398 FAST_DISPATCH();
1399 }
1400 format_exc_check_arg(PyExc_UnboundLocalError,
1401 UNBOUNDLOCAL_ERROR_MSG,
1402 PyTuple_GetItem(co->co_varnames, oparg));
1403 break;
Neil Schemenauer63543862002-02-17 19:10:14 +00001404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 TARGET(LOAD_CONST)
1406 x = GETITEM(consts, oparg);
1407 Py_INCREF(x);
1408 PUSH(x);
1409 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001411 PREDICTED_WITH_ARG(STORE_FAST);
1412 TARGET(STORE_FAST)
1413 v = POP();
1414 SETLOCAL(oparg, v);
1415 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001416
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001417 TARGET(POP_TOP)
1418 v = POP();
1419 Py_DECREF(v);
1420 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001421
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001422 TARGET(ROT_TWO)
1423 v = TOP();
1424 w = SECOND();
1425 SET_TOP(w);
1426 SET_SECOND(v);
1427 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001429 TARGET(ROT_THREE)
1430 v = TOP();
1431 w = SECOND();
1432 x = THIRD();
1433 SET_TOP(w);
1434 SET_SECOND(x);
1435 SET_THIRD(v);
1436 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001437
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001438 TARGET(DUP_TOP)
1439 v = TOP();
1440 Py_INCREF(v);
1441 PUSH(v);
1442 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001443
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001444 TARGET(DUP_TOP_TWO)
1445 x = TOP();
1446 Py_INCREF(x);
1447 w = SECOND();
1448 Py_INCREF(w);
1449 STACKADJ(2);
1450 SET_TOP(x);
1451 SET_SECOND(w);
1452 FAST_DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001454 TARGET(UNARY_POSITIVE)
1455 v = TOP();
1456 x = PyNumber_Positive(v);
1457 Py_DECREF(v);
1458 SET_TOP(x);
1459 if (x != NULL) DISPATCH();
1460 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001462 TARGET(UNARY_NEGATIVE)
1463 v = TOP();
1464 x = PyNumber_Negative(v);
1465 Py_DECREF(v);
1466 SET_TOP(x);
1467 if (x != NULL) DISPATCH();
1468 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001470 TARGET(UNARY_NOT)
1471 v = TOP();
1472 err = PyObject_IsTrue(v);
1473 Py_DECREF(v);
1474 if (err == 0) {
1475 Py_INCREF(Py_True);
1476 SET_TOP(Py_True);
1477 DISPATCH();
1478 }
1479 else if (err > 0) {
1480 Py_INCREF(Py_False);
1481 SET_TOP(Py_False);
1482 err = 0;
1483 DISPATCH();
1484 }
1485 STACKADJ(-1);
1486 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001488 TARGET(UNARY_INVERT)
1489 v = TOP();
1490 x = PyNumber_Invert(v);
1491 Py_DECREF(v);
1492 SET_TOP(x);
1493 if (x != NULL) DISPATCH();
1494 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001496 TARGET(BINARY_POWER)
1497 w = POP();
1498 v = TOP();
1499 x = PyNumber_Power(v, w, Py_None);
1500 Py_DECREF(v);
1501 Py_DECREF(w);
1502 SET_TOP(x);
1503 if (x != NULL) DISPATCH();
1504 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001506 TARGET(BINARY_MULTIPLY)
1507 w = POP();
1508 v = TOP();
1509 x = PyNumber_Multiply(v, w);
1510 Py_DECREF(v);
1511 Py_DECREF(w);
1512 SET_TOP(x);
1513 if (x != NULL) DISPATCH();
1514 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001516 TARGET(BINARY_TRUE_DIVIDE)
1517 w = POP();
1518 v = TOP();
1519 x = PyNumber_TrueDivide(v, w);
1520 Py_DECREF(v);
1521 Py_DECREF(w);
1522 SET_TOP(x);
1523 if (x != NULL) DISPATCH();
1524 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001525
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001526 TARGET(BINARY_FLOOR_DIVIDE)
1527 w = POP();
1528 v = TOP();
1529 x = PyNumber_FloorDivide(v, w);
1530 Py_DECREF(v);
1531 Py_DECREF(w);
1532 SET_TOP(x);
1533 if (x != NULL) DISPATCH();
1534 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001535
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001536 TARGET(BINARY_MODULO)
1537 w = POP();
1538 v = TOP();
1539 if (PyUnicode_CheckExact(v))
1540 x = PyUnicode_Format(v, w);
1541 else
1542 x = PyNumber_Remainder(v, w);
1543 Py_DECREF(v);
1544 Py_DECREF(w);
1545 SET_TOP(x);
1546 if (x != NULL) DISPATCH();
1547 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001549 TARGET(BINARY_ADD)
1550 w = POP();
1551 v = TOP();
1552 if (PyUnicode_CheckExact(v) &&
1553 PyUnicode_CheckExact(w)) {
1554 x = unicode_concatenate(v, w, f, next_instr);
1555 /* unicode_concatenate consumed the ref to v */
1556 goto skip_decref_vx;
1557 }
1558 else {
1559 x = PyNumber_Add(v, w);
1560 }
1561 Py_DECREF(v);
1562 skip_decref_vx:
1563 Py_DECREF(w);
1564 SET_TOP(x);
1565 if (x != NULL) DISPATCH();
1566 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001568 TARGET(BINARY_SUBTRACT)
1569 w = POP();
1570 v = TOP();
1571 x = PyNumber_Subtract(v, w);
1572 Py_DECREF(v);
1573 Py_DECREF(w);
1574 SET_TOP(x);
1575 if (x != NULL) DISPATCH();
1576 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001577
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001578 TARGET(BINARY_SUBSCR)
1579 w = POP();
1580 v = TOP();
1581 x = PyObject_GetItem(v, w);
1582 Py_DECREF(v);
1583 Py_DECREF(w);
1584 SET_TOP(x);
1585 if (x != NULL) DISPATCH();
1586 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001588 TARGET(BINARY_LSHIFT)
1589 w = POP();
1590 v = TOP();
1591 x = PyNumber_Lshift(v, w);
1592 Py_DECREF(v);
1593 Py_DECREF(w);
1594 SET_TOP(x);
1595 if (x != NULL) DISPATCH();
1596 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001598 TARGET(BINARY_RSHIFT)
1599 w = POP();
1600 v = TOP();
1601 x = PyNumber_Rshift(v, w);
1602 Py_DECREF(v);
1603 Py_DECREF(w);
1604 SET_TOP(x);
1605 if (x != NULL) DISPATCH();
1606 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001607
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001608 TARGET(BINARY_AND)
1609 w = POP();
1610 v = TOP();
1611 x = PyNumber_And(v, w);
1612 Py_DECREF(v);
1613 Py_DECREF(w);
1614 SET_TOP(x);
1615 if (x != NULL) DISPATCH();
1616 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001617
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001618 TARGET(BINARY_XOR)
1619 w = POP();
1620 v = TOP();
1621 x = PyNumber_Xor(v, w);
1622 Py_DECREF(v);
1623 Py_DECREF(w);
1624 SET_TOP(x);
1625 if (x != NULL) DISPATCH();
1626 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001627
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001628 TARGET(BINARY_OR)
1629 w = POP();
1630 v = TOP();
1631 x = PyNumber_Or(v, w);
1632 Py_DECREF(v);
1633 Py_DECREF(w);
1634 SET_TOP(x);
1635 if (x != NULL) DISPATCH();
1636 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 TARGET(LIST_APPEND)
1639 w = POP();
1640 v = PEEK(oparg);
1641 err = PyList_Append(v, w);
1642 Py_DECREF(w);
1643 if (err == 0) {
1644 PREDICT(JUMP_ABSOLUTE);
1645 DISPATCH();
1646 }
1647 break;
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001648
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001649 TARGET(SET_ADD)
1650 w = POP();
1651 v = stack_pointer[-oparg];
1652 err = PySet_Add(v, w);
1653 Py_DECREF(w);
1654 if (err == 0) {
1655 PREDICT(JUMP_ABSOLUTE);
1656 DISPATCH();
1657 }
1658 break;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 TARGET(INPLACE_POWER)
1661 w = POP();
1662 v = TOP();
1663 x = PyNumber_InPlacePower(v, w, Py_None);
1664 Py_DECREF(v);
1665 Py_DECREF(w);
1666 SET_TOP(x);
1667 if (x != NULL) DISPATCH();
1668 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001669
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001670 TARGET(INPLACE_MULTIPLY)
1671 w = POP();
1672 v = TOP();
1673 x = PyNumber_InPlaceMultiply(v, w);
1674 Py_DECREF(v);
1675 Py_DECREF(w);
1676 SET_TOP(x);
1677 if (x != NULL) DISPATCH();
1678 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001679
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001680 TARGET(INPLACE_TRUE_DIVIDE)
1681 w = POP();
1682 v = TOP();
1683 x = PyNumber_InPlaceTrueDivide(v, w);
1684 Py_DECREF(v);
1685 Py_DECREF(w);
1686 SET_TOP(x);
1687 if (x != NULL) DISPATCH();
1688 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001689
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001690 TARGET(INPLACE_FLOOR_DIVIDE)
1691 w = POP();
1692 v = TOP();
1693 x = PyNumber_InPlaceFloorDivide(v, w);
1694 Py_DECREF(v);
1695 Py_DECREF(w);
1696 SET_TOP(x);
1697 if (x != NULL) DISPATCH();
1698 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001699
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001700 TARGET(INPLACE_MODULO)
1701 w = POP();
1702 v = TOP();
1703 x = PyNumber_InPlaceRemainder(v, w);
1704 Py_DECREF(v);
1705 Py_DECREF(w);
1706 SET_TOP(x);
1707 if (x != NULL) DISPATCH();
1708 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001709
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001710 TARGET(INPLACE_ADD)
1711 w = POP();
1712 v = TOP();
1713 if (PyUnicode_CheckExact(v) &&
1714 PyUnicode_CheckExact(w)) {
1715 x = unicode_concatenate(v, w, f, next_instr);
1716 /* unicode_concatenate consumed the ref to v */
1717 goto skip_decref_v;
1718 }
1719 else {
1720 x = PyNumber_InPlaceAdd(v, w);
1721 }
1722 Py_DECREF(v);
1723 skip_decref_v:
1724 Py_DECREF(w);
1725 SET_TOP(x);
1726 if (x != NULL) DISPATCH();
1727 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001728
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001729 TARGET(INPLACE_SUBTRACT)
1730 w = POP();
1731 v = TOP();
1732 x = PyNumber_InPlaceSubtract(v, w);
1733 Py_DECREF(v);
1734 Py_DECREF(w);
1735 SET_TOP(x);
1736 if (x != NULL) DISPATCH();
1737 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001738
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001739 TARGET(INPLACE_LSHIFT)
1740 w = POP();
1741 v = TOP();
1742 x = PyNumber_InPlaceLshift(v, w);
1743 Py_DECREF(v);
1744 Py_DECREF(w);
1745 SET_TOP(x);
1746 if (x != NULL) DISPATCH();
1747 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001748
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001749 TARGET(INPLACE_RSHIFT)
1750 w = POP();
1751 v = TOP();
1752 x = PyNumber_InPlaceRshift(v, w);
1753 Py_DECREF(v);
1754 Py_DECREF(w);
1755 SET_TOP(x);
1756 if (x != NULL) DISPATCH();
1757 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001758
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001759 TARGET(INPLACE_AND)
1760 w = POP();
1761 v = TOP();
1762 x = PyNumber_InPlaceAnd(v, w);
1763 Py_DECREF(v);
1764 Py_DECREF(w);
1765 SET_TOP(x);
1766 if (x != NULL) DISPATCH();
1767 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001768
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001769 TARGET(INPLACE_XOR)
1770 w = POP();
1771 v = TOP();
1772 x = PyNumber_InPlaceXor(v, w);
1773 Py_DECREF(v);
1774 Py_DECREF(w);
1775 SET_TOP(x);
1776 if (x != NULL) DISPATCH();
1777 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001778
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001779 TARGET(INPLACE_OR)
1780 w = POP();
1781 v = TOP();
1782 x = PyNumber_InPlaceOr(v, w);
1783 Py_DECREF(v);
1784 Py_DECREF(w);
1785 SET_TOP(x);
1786 if (x != NULL) DISPATCH();
1787 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001788
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001789 TARGET(STORE_SUBSCR)
1790 w = TOP();
1791 v = SECOND();
1792 u = THIRD();
1793 STACKADJ(-3);
1794 /* v[w] = u */
1795 err = PyObject_SetItem(v, w, u);
1796 Py_DECREF(u);
1797 Py_DECREF(v);
1798 Py_DECREF(w);
1799 if (err == 0) DISPATCH();
1800 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001801
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001802 TARGET(DELETE_SUBSCR)
1803 w = TOP();
1804 v = SECOND();
1805 STACKADJ(-2);
1806 /* del v[w] */
1807 err = PyObject_DelItem(v, w);
1808 Py_DECREF(v);
1809 Py_DECREF(w);
1810 if (err == 0) DISPATCH();
1811 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001812
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001813 TARGET(PRINT_EXPR)
1814 v = POP();
1815 w = PySys_GetObject("displayhook");
1816 if (w == NULL) {
1817 PyErr_SetString(PyExc_RuntimeError,
1818 "lost sys.displayhook");
1819 err = -1;
1820 x = NULL;
1821 }
1822 if (err == 0) {
1823 x = PyTuple_Pack(1, v);
1824 if (x == NULL)
1825 err = -1;
1826 }
1827 if (err == 0) {
1828 w = PyEval_CallObject(w, x);
1829 Py_XDECREF(w);
1830 if (w == NULL)
1831 err = -1;
1832 }
1833 Py_DECREF(v);
1834 Py_XDECREF(x);
1835 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001836
Thomas Wouters434d0822000-08-24 20:11:32 +00001837#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001838 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001839#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001840 TARGET(RAISE_VARARGS)
1841 v = w = NULL;
1842 switch (oparg) {
1843 case 2:
1844 v = POP(); /* cause */
1845 case 1:
1846 w = POP(); /* exc */
1847 case 0: /* Fallthrough */
1848 why = do_raise(w, v);
1849 break;
1850 default:
1851 PyErr_SetString(PyExc_SystemError,
1852 "bad RAISE_VARARGS oparg");
1853 why = WHY_EXCEPTION;
1854 break;
1855 }
1856 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001857
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001858 TARGET(STORE_LOCALS)
1859 x = POP();
1860 v = f->f_locals;
1861 Py_XDECREF(v);
1862 f->f_locals = x;
1863 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001864
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001865 TARGET(RETURN_VALUE)
1866 retval = POP();
1867 why = WHY_RETURN;
1868 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001869
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001870 TARGET(YIELD_VALUE)
1871 retval = POP();
1872 f->f_stacktop = stack_pointer;
1873 why = WHY_YIELD;
1874 /* Put aside the current exception state and restore
1875 that of the calling frame. This only serves when
1876 "yield" is used inside an except handler. */
1877 SWAP_EXC_STATE();
1878 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001879
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001880 TARGET(POP_EXCEPT)
1881 {
1882 PyTryBlock *b = PyFrame_BlockPop(f);
1883 if (b->b_type != EXCEPT_HANDLER) {
1884 PyErr_SetString(PyExc_SystemError,
1885 "popped block is not an except handler");
1886 why = WHY_EXCEPTION;
1887 break;
1888 }
1889 UNWIND_EXCEPT_HANDLER(b);
1890 }
1891 DISPATCH();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001892
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001893 TARGET(POP_BLOCK)
1894 {
1895 PyTryBlock *b = PyFrame_BlockPop(f);
1896 UNWIND_BLOCK(b);
1897 }
1898 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001899
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001900 PREDICTED(END_FINALLY);
1901 TARGET(END_FINALLY)
1902 v = POP();
1903 if (PyLong_Check(v)) {
1904 why = (enum why_code) PyLong_AS_LONG(v);
1905 assert(why != WHY_YIELD);
1906 if (why == WHY_RETURN ||
1907 why == WHY_CONTINUE)
1908 retval = POP();
1909 if (why == WHY_SILENCED) {
1910 /* An exception was silenced by 'with', we must
1911 manually unwind the EXCEPT_HANDLER block which was
1912 created when the exception was caught, otherwise
1913 the stack will be in an inconsistent state. */
1914 PyTryBlock *b = PyFrame_BlockPop(f);
1915 assert(b->b_type == EXCEPT_HANDLER);
1916 UNWIND_EXCEPT_HANDLER(b);
1917 why = WHY_NOT;
1918 }
1919 }
1920 else if (PyExceptionClass_Check(v)) {
1921 w = POP();
1922 u = POP();
1923 PyErr_Restore(v, w, u);
1924 why = WHY_RERAISE;
1925 break;
1926 }
1927 else if (v != Py_None) {
1928 PyErr_SetString(PyExc_SystemError,
1929 "'finally' pops bad exception");
1930 why = WHY_EXCEPTION;
1931 }
1932 Py_DECREF(v);
1933 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001934
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001935 TARGET(LOAD_BUILD_CLASS)
1936 x = PyDict_GetItemString(f->f_builtins,
1937 "__build_class__");
1938 if (x == NULL) {
1939 PyErr_SetString(PyExc_ImportError,
1940 "__build_class__ not found");
1941 break;
1942 }
1943 Py_INCREF(x);
1944 PUSH(x);
1945 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001946
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001947 TARGET(STORE_NAME)
1948 w = GETITEM(names, oparg);
1949 v = POP();
1950 if ((x = f->f_locals) != NULL) {
1951 if (PyDict_CheckExact(x))
1952 err = PyDict_SetItem(x, w, v);
1953 else
1954 err = PyObject_SetItem(x, w, v);
1955 Py_DECREF(v);
1956 if (err == 0) DISPATCH();
1957 break;
1958 }
1959 PyErr_Format(PyExc_SystemError,
1960 "no locals found when storing %R", w);
1961 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001962
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001963 TARGET(DELETE_NAME)
1964 w = GETITEM(names, oparg);
1965 if ((x = f->f_locals) != NULL) {
1966 if ((err = PyObject_DelItem(x, w)) != 0)
1967 format_exc_check_arg(PyExc_NameError,
1968 NAME_ERROR_MSG,
1969 w);
1970 break;
1971 }
1972 PyErr_Format(PyExc_SystemError,
1973 "no locals when deleting %R", w);
1974 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001975
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001976 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
1977 TARGET(UNPACK_SEQUENCE)
1978 v = POP();
1979 if (PyTuple_CheckExact(v) &&
1980 PyTuple_GET_SIZE(v) == oparg) {
1981 PyObject **items = \
1982 ((PyTupleObject *)v)->ob_item;
1983 while (oparg--) {
1984 w = items[oparg];
1985 Py_INCREF(w);
1986 PUSH(w);
1987 }
1988 Py_DECREF(v);
1989 DISPATCH();
1990 } else if (PyList_CheckExact(v) &&
1991 PyList_GET_SIZE(v) == oparg) {
1992 PyObject **items = \
1993 ((PyListObject *)v)->ob_item;
1994 while (oparg--) {
1995 w = items[oparg];
1996 Py_INCREF(w);
1997 PUSH(w);
1998 }
1999 } else if (unpack_iterable(v, oparg, -1,
2000 stack_pointer + oparg)) {
2001 STACKADJ(oparg);
2002 } else {
2003 /* unpack_iterable() raised an exception */
2004 why = WHY_EXCEPTION;
2005 }
2006 Py_DECREF(v);
2007 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002008
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002009 TARGET(UNPACK_EX)
2010 {
2011 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2012 v = POP();
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002013
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002014 if (unpack_iterable(v, oparg & 0xFF, oparg >> 8,
2015 stack_pointer + totalargs)) {
2016 stack_pointer += totalargs;
2017 } else {
2018 why = WHY_EXCEPTION;
2019 }
2020 Py_DECREF(v);
2021 break;
2022 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002023
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002024 TARGET(STORE_ATTR)
2025 w = GETITEM(names, oparg);
2026 v = TOP();
2027 u = SECOND();
2028 STACKADJ(-2);
2029 err = PyObject_SetAttr(v, w, u); /* v.w = u */
2030 Py_DECREF(v);
2031 Py_DECREF(u);
2032 if (err == 0) DISPATCH();
2033 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002034
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002035 TARGET(DELETE_ATTR)
2036 w = GETITEM(names, oparg);
2037 v = POP();
2038 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
2039 /* del v.w */
2040 Py_DECREF(v);
2041 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002042
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002043 TARGET(STORE_GLOBAL)
2044 w = GETITEM(names, oparg);
2045 v = POP();
2046 err = PyDict_SetItem(f->f_globals, w, v);
2047 Py_DECREF(v);
2048 if (err == 0) DISPATCH();
2049 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002050
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002051 TARGET(DELETE_GLOBAL)
2052 w = GETITEM(names, oparg);
2053 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
2054 format_exc_check_arg(
2055 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
2056 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002058 TARGET(LOAD_NAME)
2059 w = GETITEM(names, oparg);
2060 if ((v = f->f_locals) == NULL) {
2061 PyErr_Format(PyExc_SystemError,
2062 "no locals when loading %R", w);
2063 why = WHY_EXCEPTION;
2064 break;
2065 }
2066 if (PyDict_CheckExact(v)) {
2067 x = PyDict_GetItem(v, w);
2068 Py_XINCREF(x);
2069 }
2070 else {
2071 x = PyObject_GetItem(v, w);
2072 if (x == NULL && PyErr_Occurred()) {
2073 if (!PyErr_ExceptionMatches(
2074 PyExc_KeyError))
2075 break;
2076 PyErr_Clear();
2077 }
2078 }
2079 if (x == NULL) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002080 x = PyDict_GetItem(f->f_globals, w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002081 if (x == NULL) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002082 x = PyDict_GetItem(f->f_builtins, w);
2083 if (x == NULL) {
2084 format_exc_check_arg(
2085 PyExc_NameError,
2086 NAME_ERROR_MSG, w);
2087 break;
2088 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002089 }
2090 Py_INCREF(x);
2091 }
2092 PUSH(x);
2093 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002094
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002095 TARGET(LOAD_GLOBAL)
2096 w = GETITEM(names, oparg);
2097 if (PyUnicode_CheckExact(w)) {
2098 /* Inline the PyDict_GetItem() calls.
2099 WARNING: this is an extreme speed hack.
2100 Do not try this at home. */
2101 long hash = ((PyUnicodeObject *)w)->hash;
2102 if (hash != -1) {
2103 PyDictObject *d;
2104 PyDictEntry *e;
2105 d = (PyDictObject *)(f->f_globals);
2106 e = d->ma_lookup(d, w, hash);
2107 if (e == NULL) {
2108 x = NULL;
2109 break;
2110 }
2111 x = e->me_value;
2112 if (x != NULL) {
2113 Py_INCREF(x);
2114 PUSH(x);
2115 DISPATCH();
2116 }
2117 d = (PyDictObject *)(f->f_builtins);
2118 e = d->ma_lookup(d, w, hash);
2119 if (e == NULL) {
2120 x = NULL;
2121 break;
2122 }
2123 x = e->me_value;
2124 if (x != NULL) {
2125 Py_INCREF(x);
2126 PUSH(x);
2127 DISPATCH();
2128 }
2129 goto load_global_error;
2130 }
2131 }
2132 /* This is the un-inlined version of the code above */
2133 x = PyDict_GetItem(f->f_globals, w);
2134 if (x == NULL) {
2135 x = PyDict_GetItem(f->f_builtins, w);
2136 if (x == NULL) {
2137 load_global_error:
2138 format_exc_check_arg(
2139 PyExc_NameError,
2140 GLOBAL_NAME_ERROR_MSG, w);
2141 break;
2142 }
2143 }
2144 Py_INCREF(x);
2145 PUSH(x);
2146 DISPATCH();
Guido van Rossum681d79a1995-07-18 14:51:37 +00002147
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002148 TARGET(DELETE_FAST)
2149 x = GETLOCAL(oparg);
2150 if (x != NULL) {
2151 SETLOCAL(oparg, NULL);
2152 DISPATCH();
2153 }
2154 format_exc_check_arg(
2155 PyExc_UnboundLocalError,
2156 UNBOUNDLOCAL_ERROR_MSG,
2157 PyTuple_GetItem(co->co_varnames, oparg)
2158 );
2159 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002160
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002161 TARGET(DELETE_DEREF)
2162 x = freevars[oparg];
2163 if (PyCell_GET(x) != NULL) {
2164 PyCell_Set(x, NULL);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002165 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002166 }
2167 err = -1;
2168 format_exc_unbound(co, oparg);
2169 break;
2170
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002171 TARGET(LOAD_CLOSURE)
2172 x = freevars[oparg];
2173 Py_INCREF(x);
2174 PUSH(x);
2175 if (x != NULL) DISPATCH();
2176 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002178 TARGET(LOAD_DEREF)
2179 x = freevars[oparg];
2180 w = PyCell_Get(x);
2181 if (w != NULL) {
2182 PUSH(w);
2183 DISPATCH();
2184 }
2185 err = -1;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002186 format_exc_unbound(co, oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002187 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002188
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002189 TARGET(STORE_DEREF)
2190 w = POP();
2191 x = freevars[oparg];
2192 PyCell_Set(x, w);
2193 Py_DECREF(w);
2194 DISPATCH();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002195
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002196 TARGET(BUILD_TUPLE)
2197 x = PyTuple_New(oparg);
2198 if (x != NULL) {
2199 for (; --oparg >= 0;) {
2200 w = POP();
2201 PyTuple_SET_ITEM(x, oparg, w);
2202 }
2203 PUSH(x);
2204 DISPATCH();
2205 }
2206 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002208 TARGET(BUILD_LIST)
2209 x = PyList_New(oparg);
2210 if (x != NULL) {
2211 for (; --oparg >= 0;) {
2212 w = POP();
2213 PyList_SET_ITEM(x, oparg, w);
2214 }
2215 PUSH(x);
2216 DISPATCH();
2217 }
2218 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002219
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002220 TARGET(BUILD_SET)
2221 x = PySet_New(NULL);
2222 if (x != NULL) {
2223 for (; --oparg >= 0;) {
2224 w = POP();
2225 if (err == 0)
2226 err = PySet_Add(x, w);
2227 Py_DECREF(w);
2228 }
2229 if (err != 0) {
2230 Py_DECREF(x);
2231 break;
2232 }
2233 PUSH(x);
2234 DISPATCH();
2235 }
2236 break;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002237
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002238 TARGET(BUILD_MAP)
2239 x = _PyDict_NewPresized((Py_ssize_t)oparg);
2240 PUSH(x);
2241 if (x != NULL) DISPATCH();
2242 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002243
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002244 TARGET(STORE_MAP)
2245 w = TOP(); /* key */
2246 u = SECOND(); /* value */
2247 v = THIRD(); /* dict */
2248 STACKADJ(-2);
2249 assert (PyDict_CheckExact(v));
2250 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2251 Py_DECREF(u);
2252 Py_DECREF(w);
2253 if (err == 0) DISPATCH();
2254 break;
Christian Heimes99170a52007-12-19 02:07:34 +00002255
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002256 TARGET(MAP_ADD)
2257 w = TOP(); /* key */
2258 u = SECOND(); /* value */
2259 STACKADJ(-2);
2260 v = stack_pointer[-oparg]; /* dict */
2261 assert (PyDict_CheckExact(v));
2262 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2263 Py_DECREF(u);
2264 Py_DECREF(w);
2265 if (err == 0) {
2266 PREDICT(JUMP_ABSOLUTE);
2267 DISPATCH();
2268 }
2269 break;
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002270
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002271 TARGET(LOAD_ATTR)
2272 w = GETITEM(names, oparg);
2273 v = TOP();
2274 x = PyObject_GetAttr(v, w);
2275 Py_DECREF(v);
2276 SET_TOP(x);
2277 if (x != NULL) DISPATCH();
2278 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002280 TARGET(COMPARE_OP)
2281 w = POP();
2282 v = TOP();
2283 x = cmp_outcome(oparg, v, w);
2284 Py_DECREF(v);
2285 Py_DECREF(w);
2286 SET_TOP(x);
2287 if (x == NULL) break;
2288 PREDICT(POP_JUMP_IF_FALSE);
2289 PREDICT(POP_JUMP_IF_TRUE);
2290 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002292 TARGET(IMPORT_NAME)
2293 w = GETITEM(names, oparg);
2294 x = PyDict_GetItemString(f->f_builtins, "__import__");
2295 if (x == NULL) {
2296 PyErr_SetString(PyExc_ImportError,
2297 "__import__ not found");
2298 break;
2299 }
2300 Py_INCREF(x);
2301 v = POP();
2302 u = TOP();
2303 if (PyLong_AsLong(u) != -1 || PyErr_Occurred())
2304 w = PyTuple_Pack(5,
2305 w,
2306 f->f_globals,
2307 f->f_locals == NULL ?
2308 Py_None : f->f_locals,
2309 v,
2310 u);
2311 else
2312 w = PyTuple_Pack(4,
2313 w,
2314 f->f_globals,
2315 f->f_locals == NULL ?
2316 Py_None : f->f_locals,
2317 v);
2318 Py_DECREF(v);
2319 Py_DECREF(u);
2320 if (w == NULL) {
2321 u = POP();
2322 Py_DECREF(x);
2323 x = NULL;
2324 break;
2325 }
2326 READ_TIMESTAMP(intr0);
2327 v = x;
2328 x = PyEval_CallObject(v, w);
2329 Py_DECREF(v);
2330 READ_TIMESTAMP(intr1);
2331 Py_DECREF(w);
2332 SET_TOP(x);
2333 if (x != NULL) DISPATCH();
2334 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002336 TARGET(IMPORT_STAR)
2337 v = POP();
2338 PyFrame_FastToLocals(f);
2339 if ((x = f->f_locals) == NULL) {
2340 PyErr_SetString(PyExc_SystemError,
2341 "no locals found during 'import *'");
2342 break;
2343 }
2344 READ_TIMESTAMP(intr0);
2345 err = import_all_from(x, v);
2346 READ_TIMESTAMP(intr1);
2347 PyFrame_LocalsToFast(f, 0);
2348 Py_DECREF(v);
2349 if (err == 0) DISPATCH();
2350 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002352 TARGET(IMPORT_FROM)
2353 w = GETITEM(names, oparg);
2354 v = TOP();
2355 READ_TIMESTAMP(intr0);
2356 x = import_from(v, w);
2357 READ_TIMESTAMP(intr1);
2358 PUSH(x);
2359 if (x != NULL) DISPATCH();
2360 break;
Thomas Wouters52152252000-08-17 22:55:00 +00002361
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002362 TARGET(JUMP_FORWARD)
2363 JUMPBY(oparg);
2364 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002366 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
2367 TARGET(POP_JUMP_IF_FALSE)
2368 w = POP();
2369 if (w == Py_True) {
2370 Py_DECREF(w);
2371 FAST_DISPATCH();
2372 }
2373 if (w == Py_False) {
2374 Py_DECREF(w);
2375 JUMPTO(oparg);
2376 FAST_DISPATCH();
2377 }
2378 err = PyObject_IsTrue(w);
2379 Py_DECREF(w);
2380 if (err > 0)
2381 err = 0;
2382 else if (err == 0)
2383 JUMPTO(oparg);
2384 else
2385 break;
2386 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002388 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
2389 TARGET(POP_JUMP_IF_TRUE)
2390 w = POP();
2391 if (w == Py_False) {
2392 Py_DECREF(w);
2393 FAST_DISPATCH();
2394 }
2395 if (w == Py_True) {
2396 Py_DECREF(w);
2397 JUMPTO(oparg);
2398 FAST_DISPATCH();
2399 }
2400 err = PyObject_IsTrue(w);
2401 Py_DECREF(w);
2402 if (err > 0) {
2403 err = 0;
2404 JUMPTO(oparg);
2405 }
2406 else if (err == 0)
2407 ;
2408 else
2409 break;
2410 DISPATCH();
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002412 TARGET(JUMP_IF_FALSE_OR_POP)
2413 w = TOP();
2414 if (w == Py_True) {
2415 STACKADJ(-1);
2416 Py_DECREF(w);
2417 FAST_DISPATCH();
2418 }
2419 if (w == Py_False) {
2420 JUMPTO(oparg);
2421 FAST_DISPATCH();
2422 }
2423 err = PyObject_IsTrue(w);
2424 if (err > 0) {
2425 STACKADJ(-1);
2426 Py_DECREF(w);
2427 err = 0;
2428 }
2429 else if (err == 0)
2430 JUMPTO(oparg);
2431 else
2432 break;
2433 DISPATCH();
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002435 TARGET(JUMP_IF_TRUE_OR_POP)
2436 w = TOP();
2437 if (w == Py_False) {
2438 STACKADJ(-1);
2439 Py_DECREF(w);
2440 FAST_DISPATCH();
2441 }
2442 if (w == Py_True) {
2443 JUMPTO(oparg);
2444 FAST_DISPATCH();
2445 }
2446 err = PyObject_IsTrue(w);
2447 if (err > 0) {
2448 err = 0;
2449 JUMPTO(oparg);
2450 }
2451 else if (err == 0) {
2452 STACKADJ(-1);
2453 Py_DECREF(w);
2454 }
2455 else
2456 break;
2457 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002458
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002459 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
2460 TARGET(JUMP_ABSOLUTE)
2461 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002462#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002463 /* Enabling this path speeds-up all while and for-loops by bypassing
2464 the per-loop checks for signals. By default, this should be turned-off
2465 because it prevents detection of a control-break in tight loops like
2466 "while 1: pass". Compile with this option turned-on when you need
2467 the speed-up and do not need break checking inside tight loops (ones
2468 that contain only instructions ending with FAST_DISPATCH).
2469 */
2470 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002471#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002472 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002473#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002475 TARGET(GET_ITER)
2476 /* before: [obj]; after [getiter(obj)] */
2477 v = TOP();
2478 x = PyObject_GetIter(v);
2479 Py_DECREF(v);
2480 if (x != NULL) {
2481 SET_TOP(x);
2482 PREDICT(FOR_ITER);
2483 DISPATCH();
2484 }
2485 STACKADJ(-1);
2486 break;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002488 PREDICTED_WITH_ARG(FOR_ITER);
2489 TARGET(FOR_ITER)
2490 /* before: [iter]; after: [iter, iter()] *or* [] */
2491 v = TOP();
2492 x = (*v->ob_type->tp_iternext)(v);
2493 if (x != NULL) {
2494 PUSH(x);
2495 PREDICT(STORE_FAST);
2496 PREDICT(UNPACK_SEQUENCE);
2497 DISPATCH();
2498 }
2499 if (PyErr_Occurred()) {
2500 if (!PyErr_ExceptionMatches(
2501 PyExc_StopIteration))
2502 break;
2503 PyErr_Clear();
2504 }
2505 /* iterator ended normally */
2506 x = v = POP();
2507 Py_DECREF(v);
2508 JUMPBY(oparg);
2509 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002510
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002511 TARGET(BREAK_LOOP)
2512 why = WHY_BREAK;
2513 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002515 TARGET(CONTINUE_LOOP)
2516 retval = PyLong_FromLong(oparg);
2517 if (!retval) {
2518 x = NULL;
2519 break;
2520 }
2521 why = WHY_CONTINUE;
2522 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002523
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002524 TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
2525 TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
2526 TARGET(SETUP_FINALLY)
2527 _setup_finally:
2528 /* NOTE: If you add any new block-setup opcodes that
2529 are not try/except/finally handlers, you may need
2530 to update the PyGen_NeedsFinalizing() function.
2531 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002532
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002533 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2534 STACK_LEVEL());
2535 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002536
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002537 TARGET(SETUP_WITH)
2538 {
2539 static PyObject *exit, *enter;
2540 w = TOP();
2541 x = special_lookup(w, "__exit__", &exit);
2542 if (!x)
2543 break;
2544 SET_TOP(x);
2545 u = special_lookup(w, "__enter__", &enter);
2546 Py_DECREF(w);
2547 if (!u) {
2548 x = NULL;
2549 break;
2550 }
2551 x = PyObject_CallFunctionObjArgs(u, NULL);
2552 Py_DECREF(u);
2553 if (!x)
2554 break;
2555 /* Setup the finally block before pushing the result
2556 of __enter__ on the stack. */
2557 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2558 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002559
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002560 PUSH(x);
2561 DISPATCH();
2562 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002564 TARGET(WITH_CLEANUP)
2565 {
2566 /* At the top of the stack are 1-3 values indicating
2567 how/why we entered the finally clause:
2568 - TOP = None
2569 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2570 - TOP = WHY_*; no retval below it
2571 - (TOP, SECOND, THIRD) = exc_info()
2572 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2573 Below them is EXIT, the context.__exit__ bound method.
2574 In the last case, we must call
2575 EXIT(TOP, SECOND, THIRD)
2576 otherwise we must call
2577 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002579 In the first two cases, we remove EXIT from the
2580 stack, leaving the rest in the same order. In the
2581 third case, we shift the bottom 3 values of the
2582 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002583
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002584 In addition, if the stack represents an exception,
2585 *and* the function call returns a 'true' value, we
2586 push WHY_SILENCED onto the stack. END_FINALLY will
2587 then not re-raise the exception. (But non-local
2588 gotos should still be resumed.)
2589 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002591 PyObject *exit_func;
2592 u = TOP();
2593 if (u == Py_None) {
2594 (void)POP();
2595 exit_func = TOP();
2596 SET_TOP(u);
2597 v = w = Py_None;
2598 }
2599 else if (PyLong_Check(u)) {
2600 (void)POP();
2601 switch(PyLong_AsLong(u)) {
2602 case WHY_RETURN:
2603 case WHY_CONTINUE:
2604 /* Retval in TOP. */
2605 exit_func = SECOND();
2606 SET_SECOND(TOP());
2607 SET_TOP(u);
2608 break;
2609 default:
2610 exit_func = TOP();
2611 SET_TOP(u);
2612 break;
2613 }
2614 u = v = w = Py_None;
2615 }
2616 else {
2617 PyObject *tp, *exc, *tb;
2618 PyTryBlock *block;
2619 v = SECOND();
2620 w = THIRD();
2621 tp = FOURTH();
2622 exc = PEEK(5);
2623 tb = PEEK(6);
2624 exit_func = PEEK(7);
2625 SET_VALUE(7, tb);
2626 SET_VALUE(6, exc);
2627 SET_VALUE(5, tp);
2628 /* UNWIND_EXCEPT_HANDLER will pop this off. */
2629 SET_FOURTH(NULL);
2630 /* We just shifted the stack down, so we have
2631 to tell the except handler block that the
2632 values are lower than it expects. */
2633 block = &f->f_blockstack[f->f_iblock - 1];
2634 assert(block->b_type == EXCEPT_HANDLER);
2635 block->b_level--;
2636 }
2637 /* XXX Not the fastest way to call it... */
2638 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2639 NULL);
2640 Py_DECREF(exit_func);
2641 if (x == NULL)
2642 break; /* Go to error exit */
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002643
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002644 if (u != Py_None)
2645 err = PyObject_IsTrue(x);
2646 else
2647 err = 0;
2648 Py_DECREF(x);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002649
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002650 if (err < 0)
2651 break; /* Go to error exit */
2652 else if (err > 0) {
2653 err = 0;
2654 /* There was an exception and a True return */
2655 PUSH(PyLong_FromLong((long) WHY_SILENCED));
2656 }
2657 PREDICT(END_FINALLY);
2658 break;
2659 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002660
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002661 TARGET(CALL_FUNCTION)
2662 {
2663 PyObject **sp;
2664 PCALL(PCALL_ALL);
2665 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002666#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002667 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002668#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002669 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002670#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002671 stack_pointer = sp;
2672 PUSH(x);
2673 if (x != NULL)
2674 DISPATCH();
2675 break;
2676 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002677
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002678 TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw)
2679 TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw)
2680 TARGET(CALL_FUNCTION_VAR_KW)
2681 _call_function_var_kw:
2682 {
2683 int na = oparg & 0xff;
2684 int nk = (oparg>>8) & 0xff;
2685 int flags = (opcode - CALL_FUNCTION) & 3;
2686 int n = na + 2 * nk;
2687 PyObject **pfunc, *func, **sp;
2688 PCALL(PCALL_ALL);
2689 if (flags & CALL_FLAG_VAR)
2690 n++;
2691 if (flags & CALL_FLAG_KW)
2692 n++;
2693 pfunc = stack_pointer - n - 1;
2694 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002695
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002696 if (PyMethod_Check(func)
Stefan Krahb7e10102010-06-23 18:42:39 +00002697 && PyMethod_GET_SELF(func) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002698 PyObject *self = PyMethod_GET_SELF(func);
2699 Py_INCREF(self);
2700 func = PyMethod_GET_FUNCTION(func);
2701 Py_INCREF(func);
2702 Py_DECREF(*pfunc);
2703 *pfunc = self;
2704 na++;
2705 n++;
2706 } else
2707 Py_INCREF(func);
2708 sp = stack_pointer;
2709 READ_TIMESTAMP(intr0);
2710 x = ext_do_call(func, &sp, flags, na, nk);
2711 READ_TIMESTAMP(intr1);
2712 stack_pointer = sp;
2713 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002714
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002715 while (stack_pointer > pfunc) {
2716 w = POP();
2717 Py_DECREF(w);
2718 }
2719 PUSH(x);
2720 if (x != NULL)
2721 DISPATCH();
2722 break;
2723 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002724
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002725 TARGET_WITH_IMPL(MAKE_CLOSURE, _make_function)
2726 TARGET(MAKE_FUNCTION)
2727 _make_function:
2728 {
2729 int posdefaults = oparg & 0xff;
2730 int kwdefaults = (oparg>>8) & 0xff;
2731 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002732
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002733 v = POP(); /* code object */
2734 x = PyFunction_New(v, f->f_globals);
2735 Py_DECREF(v);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002736
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002737 if (x != NULL && opcode == MAKE_CLOSURE) {
2738 v = POP();
2739 if (PyFunction_SetClosure(x, v) != 0) {
2740 /* Can't happen unless bytecode is corrupt. */
2741 why = WHY_EXCEPTION;
2742 }
2743 Py_DECREF(v);
2744 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002745
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002746 if (x != NULL && num_annotations > 0) {
2747 Py_ssize_t name_ix;
2748 u = POP(); /* names of args with annotations */
2749 v = PyDict_New();
2750 if (v == NULL) {
2751 Py_DECREF(x);
2752 x = NULL;
2753 break;
2754 }
2755 name_ix = PyTuple_Size(u);
2756 assert(num_annotations == name_ix+1);
2757 while (name_ix > 0) {
2758 --name_ix;
2759 t = PyTuple_GET_ITEM(u, name_ix);
2760 w = POP();
2761 /* XXX(nnorwitz): check for errors */
2762 PyDict_SetItem(v, t, w);
2763 Py_DECREF(w);
2764 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002765
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002766 if (PyFunction_SetAnnotations(x, v) != 0) {
2767 /* Can't happen unless
2768 PyFunction_SetAnnotations changes. */
2769 why = WHY_EXCEPTION;
2770 }
2771 Py_DECREF(v);
2772 Py_DECREF(u);
2773 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002774
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002775 /* XXX Maybe this should be a separate opcode? */
2776 if (x != NULL && posdefaults > 0) {
2777 v = PyTuple_New(posdefaults);
2778 if (v == NULL) {
2779 Py_DECREF(x);
2780 x = NULL;
2781 break;
2782 }
2783 while (--posdefaults >= 0) {
2784 w = POP();
2785 PyTuple_SET_ITEM(v, posdefaults, w);
2786 }
2787 if (PyFunction_SetDefaults(x, v) != 0) {
2788 /* Can't happen unless
2789 PyFunction_SetDefaults changes. */
2790 why = WHY_EXCEPTION;
2791 }
2792 Py_DECREF(v);
2793 }
2794 if (x != NULL && kwdefaults > 0) {
2795 v = PyDict_New();
2796 if (v == NULL) {
2797 Py_DECREF(x);
2798 x = NULL;
2799 break;
2800 }
2801 while (--kwdefaults >= 0) {
2802 w = POP(); /* default value */
2803 u = POP(); /* kw only arg name */
2804 /* XXX(nnorwitz): check for errors */
2805 PyDict_SetItem(v, u, w);
2806 Py_DECREF(w);
2807 Py_DECREF(u);
2808 }
2809 if (PyFunction_SetKwDefaults(x, v) != 0) {
2810 /* Can't happen unless
2811 PyFunction_SetKwDefaults changes. */
2812 why = WHY_EXCEPTION;
2813 }
2814 Py_DECREF(v);
2815 }
2816 PUSH(x);
2817 break;
2818 }
Guido van Rossum8861b741996-07-30 16:49:37 +00002819
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002820 TARGET(BUILD_SLICE)
2821 if (oparg == 3)
2822 w = POP();
2823 else
2824 w = NULL;
2825 v = POP();
2826 u = TOP();
2827 x = PySlice_New(u, v, w);
2828 Py_DECREF(u);
2829 Py_DECREF(v);
2830 Py_XDECREF(w);
2831 SET_TOP(x);
2832 if (x != NULL) DISPATCH();
2833 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002834
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002835 TARGET(EXTENDED_ARG)
2836 opcode = NEXTOP();
2837 oparg = oparg<<16 | NEXTARG();
2838 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002839
Antoine Pitrou042b1282010-08-13 21:15:58 +00002840#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002841 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00002842#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002843 default:
2844 fprintf(stderr,
2845 "XXX lineno: %d, opcode: %d\n",
2846 PyFrame_GetLineNumber(f),
2847 opcode);
2848 PyErr_SetString(PyExc_SystemError, "unknown opcode");
2849 why = WHY_EXCEPTION;
2850 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002851
2852#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002853 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002854#endif
2855
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002856 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00002857
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002858 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002859
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002860 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002861
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002862 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002863
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002864 if (why == WHY_NOT) {
2865 if (err == 0 && x != NULL) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002866#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002867 /* This check is expensive! */
2868 if (PyErr_Occurred())
2869 fprintf(stderr,
2870 "XXX undetected error\n");
2871 else {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002872#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002873 READ_TIMESTAMP(loop1);
2874 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002875#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002876 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002877#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002878 }
2879 why = WHY_EXCEPTION;
2880 x = Py_None;
2881 err = 0;
2882 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002883
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002884 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002885
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002886 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
2887 if (!PyErr_Occurred()) {
2888 PyErr_SetString(PyExc_SystemError,
2889 "error return without exception set");
2890 why = WHY_EXCEPTION;
2891 }
2892 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002893#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002894 else {
2895 /* This check is expensive! */
2896 if (PyErr_Occurred()) {
2897 char buf[128];
2898 sprintf(buf, "Stack unwind with exception "
2899 "set and why=%d", why);
2900 Py_FatalError(buf);
2901 }
2902 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002903#endif
2904
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002905 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002906
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002907 if (why == WHY_EXCEPTION) {
2908 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002909
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002910 if (tstate->c_tracefunc != NULL)
2911 call_exc_trace(tstate->c_tracefunc,
2912 tstate->c_traceobj, f);
2913 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002914
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002915 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002916
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002917 if (why == WHY_RERAISE)
2918 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +00002919
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002920 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002921
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002922fast_block_end:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002923 while (why != WHY_NOT && f->f_iblock > 0) {
2924 /* Peek at the current block. */
2925 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002926
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002927 assert(why != WHY_YIELD);
2928 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2929 why = WHY_NOT;
2930 JUMPTO(PyLong_AS_LONG(retval));
2931 Py_DECREF(retval);
2932 break;
2933 }
2934 /* Now we have to pop the block. */
2935 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002936
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002937 if (b->b_type == EXCEPT_HANDLER) {
2938 UNWIND_EXCEPT_HANDLER(b);
2939 continue;
2940 }
2941 UNWIND_BLOCK(b);
2942 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2943 why = WHY_NOT;
2944 JUMPTO(b->b_handler);
2945 break;
2946 }
2947 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
2948 || b->b_type == SETUP_FINALLY)) {
2949 PyObject *exc, *val, *tb;
2950 int handler = b->b_handler;
2951 /* Beware, this invalidates all b->b_* fields */
2952 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
2953 PUSH(tstate->exc_traceback);
2954 PUSH(tstate->exc_value);
2955 if (tstate->exc_type != NULL) {
2956 PUSH(tstate->exc_type);
2957 }
2958 else {
2959 Py_INCREF(Py_None);
2960 PUSH(Py_None);
2961 }
2962 PyErr_Fetch(&exc, &val, &tb);
2963 /* Make the raw exception data
2964 available to the handler,
2965 so a program can emulate the
2966 Python main loop. */
2967 PyErr_NormalizeException(
2968 &exc, &val, &tb);
2969 PyException_SetTraceback(val, tb);
2970 Py_INCREF(exc);
2971 tstate->exc_type = exc;
2972 Py_INCREF(val);
2973 tstate->exc_value = val;
2974 tstate->exc_traceback = tb;
2975 if (tb == NULL)
2976 tb = Py_None;
2977 Py_INCREF(tb);
2978 PUSH(tb);
2979 PUSH(val);
2980 PUSH(exc);
2981 why = WHY_NOT;
2982 JUMPTO(handler);
2983 break;
2984 }
2985 if (b->b_type == SETUP_FINALLY) {
2986 if (why & (WHY_RETURN | WHY_CONTINUE))
2987 PUSH(retval);
2988 PUSH(PyLong_FromLong((long)why));
2989 why = WHY_NOT;
2990 JUMPTO(b->b_handler);
2991 break;
2992 }
2993 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00002994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002995 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002996
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002997 if (why != WHY_NOT)
2998 break;
2999 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00003000
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003001 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003002
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003003 assert(why != WHY_YIELD);
3004 /* Pop remaining stack entries. */
3005 while (!EMPTY()) {
3006 v = POP();
3007 Py_XDECREF(v);
3008 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003009
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003010 if (why != WHY_RETURN)
3011 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003012
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003013fast_yield:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003014 if (tstate->use_tracing) {
3015 if (tstate->c_tracefunc) {
3016 if (why == WHY_RETURN || why == WHY_YIELD) {
3017 if (call_trace(tstate->c_tracefunc,
3018 tstate->c_traceobj, f,
3019 PyTrace_RETURN, retval)) {
3020 Py_XDECREF(retval);
3021 retval = NULL;
3022 why = WHY_EXCEPTION;
3023 }
3024 }
3025 else if (why == WHY_EXCEPTION) {
3026 call_trace_protected(tstate->c_tracefunc,
3027 tstate->c_traceobj, f,
3028 PyTrace_RETURN, NULL);
3029 }
3030 }
3031 if (tstate->c_profilefunc) {
3032 if (why == WHY_EXCEPTION)
3033 call_trace_protected(tstate->c_profilefunc,
3034 tstate->c_profileobj, f,
3035 PyTrace_RETURN, NULL);
3036 else if (call_trace(tstate->c_profilefunc,
3037 tstate->c_profileobj, f,
3038 PyTrace_RETURN, retval)) {
3039 Py_XDECREF(retval);
3040 retval = NULL;
3041 why = WHY_EXCEPTION;
3042 }
3043 }
3044 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003045
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003046 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003047exit_eval_frame:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003048 Py_LeaveRecursiveCall();
3049 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003050
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003051 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00003052}
3053
Guido van Rossumc2e20742006-02-27 22:32:47 +00003054/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003055 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003056 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003057
Tim Peters6d6c1a32001-08-02 04:15:00 +00003058PyObject *
3059PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003060 PyObject **args, int argcount, PyObject **kws, int kwcount,
3061 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00003062{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003063 register PyFrameObject *f;
3064 register PyObject *retval = NULL;
3065 register PyObject **fastlocals, **freevars;
3066 PyThreadState *tstate = PyThreadState_GET();
3067 PyObject *x, *u;
3068 int total_args = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00003069
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003070 if (globals == NULL) {
3071 PyErr_SetString(PyExc_SystemError,
3072 "PyEval_EvalCodeEx: NULL globals");
3073 return NULL;
3074 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003075
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003076 assert(tstate != NULL);
3077 assert(globals != NULL);
3078 f = PyFrame_New(tstate, co, globals, locals);
3079 if (f == NULL)
3080 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003081
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003082 fastlocals = f->f_localsplus;
3083 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003085 if (total_args || co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
3086 int i;
3087 int n = argcount;
3088 PyObject *kwdict = NULL;
3089 if (co->co_flags & CO_VARKEYWORDS) {
3090 kwdict = PyDict_New();
3091 if (kwdict == NULL)
3092 goto fail;
3093 i = total_args;
3094 if (co->co_flags & CO_VARARGS)
3095 i++;
3096 SETLOCAL(i, kwdict);
3097 }
3098 if (argcount > co->co_argcount) {
3099 if (!(co->co_flags & CO_VARARGS)) {
3100 PyErr_Format(PyExc_TypeError,
3101 "%U() takes %s %d "
Benjamin Peterson88968ad2010-06-25 19:30:21 +00003102 "positional argument%s (%d given)",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003103 co->co_name,
3104 defcount ? "at most" : "exactly",
Benjamin Peterson88968ad2010-06-25 19:30:21 +00003105 co->co_argcount,
3106 co->co_argcount == 1 ? "" : "s",
Benjamin Petersonaa7fbd92010-09-25 03:25:42 +00003107 argcount + kwcount);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003108 goto fail;
3109 }
3110 n = co->co_argcount;
3111 }
3112 for (i = 0; i < n; i++) {
3113 x = args[i];
3114 Py_INCREF(x);
3115 SETLOCAL(i, x);
3116 }
3117 if (co->co_flags & CO_VARARGS) {
3118 u = PyTuple_New(argcount - n);
3119 if (u == NULL)
3120 goto fail;
3121 SETLOCAL(total_args, u);
3122 for (i = n; i < argcount; i++) {
3123 x = args[i];
3124 Py_INCREF(x);
3125 PyTuple_SET_ITEM(u, i-n, x);
3126 }
3127 }
3128 for (i = 0; i < kwcount; i++) {
3129 PyObject **co_varnames;
3130 PyObject *keyword = kws[2*i];
3131 PyObject *value = kws[2*i + 1];
3132 int j;
3133 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3134 PyErr_Format(PyExc_TypeError,
3135 "%U() keywords must be strings",
3136 co->co_name);
3137 goto fail;
3138 }
3139 /* Speed hack: do raw pointer compares. As names are
3140 normally interned this should almost always hit. */
3141 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3142 for (j = 0; j < total_args; j++) {
3143 PyObject *nm = co_varnames[j];
3144 if (nm == keyword)
3145 goto kw_found;
3146 }
3147 /* Slow fallback, just in case */
3148 for (j = 0; j < total_args; j++) {
3149 PyObject *nm = co_varnames[j];
3150 int cmp = PyObject_RichCompareBool(
3151 keyword, nm, Py_EQ);
3152 if (cmp > 0)
3153 goto kw_found;
3154 else if (cmp < 0)
3155 goto fail;
3156 }
3157 if (j >= total_args && kwdict == NULL) {
3158 PyErr_Format(PyExc_TypeError,
3159 "%U() got an unexpected "
3160 "keyword argument '%S'",
3161 co->co_name,
3162 keyword);
3163 goto fail;
3164 }
3165 PyDict_SetItem(kwdict, keyword, value);
3166 continue;
3167 kw_found:
3168 if (GETLOCAL(j) != NULL) {
3169 PyErr_Format(PyExc_TypeError,
3170 "%U() got multiple "
3171 "values for keyword "
3172 "argument '%S'",
3173 co->co_name,
3174 keyword);
3175 goto fail;
3176 }
3177 Py_INCREF(value);
3178 SETLOCAL(j, value);
3179 }
3180 if (co->co_kwonlyargcount > 0) {
3181 for (i = co->co_argcount; i < total_args; i++) {
3182 PyObject *name;
3183 if (GETLOCAL(i) != NULL)
3184 continue;
3185 name = PyTuple_GET_ITEM(co->co_varnames, i);
3186 if (kwdefs != NULL) {
3187 PyObject *def = PyDict_GetItem(kwdefs, name);
3188 if (def) {
3189 Py_INCREF(def);
3190 SETLOCAL(i, def);
3191 continue;
3192 }
3193 }
3194 PyErr_Format(PyExc_TypeError,
3195 "%U() needs keyword-only argument %S",
3196 co->co_name, name);
3197 goto fail;
3198 }
3199 }
3200 if (argcount < co->co_argcount) {
3201 int m = co->co_argcount - defcount;
3202 for (i = argcount; i < m; i++) {
3203 if (GETLOCAL(i) == NULL) {
3204 int j, given = 0;
3205 for (j = 0; j < co->co_argcount; j++)
3206 if (GETLOCAL(j))
3207 given++;
3208 PyErr_Format(PyExc_TypeError,
3209 "%U() takes %s %d "
3210 "argument%s "
3211 "(%d given)",
3212 co->co_name,
3213 ((co->co_flags & CO_VARARGS) ||
3214 defcount) ? "at least"
3215 : "exactly",
3216 m, m == 1 ? "" : "s", given);
3217 goto fail;
3218 }
3219 }
3220 if (n > m)
3221 i = n - m;
3222 else
3223 i = 0;
3224 for (; i < defcount; i++) {
3225 if (GETLOCAL(m+i) == NULL) {
3226 PyObject *def = defs[i];
3227 Py_INCREF(def);
3228 SETLOCAL(m+i, def);
3229 }
3230 }
3231 }
3232 }
3233 else if (argcount > 0 || kwcount > 0) {
3234 PyErr_Format(PyExc_TypeError,
3235 "%U() takes no arguments (%d given)",
3236 co->co_name,
3237 argcount + kwcount);
3238 goto fail;
3239 }
3240 /* Allocate and initialize storage for cell vars, and copy free
3241 vars into frame. This isn't too efficient right now. */
3242 if (PyTuple_GET_SIZE(co->co_cellvars)) {
3243 int i, j, nargs, found;
3244 Py_UNICODE *cellname, *argname;
3245 PyObject *c;
Tim Peters5ca576e2001-06-18 22:08:13 +00003246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003247 nargs = total_args;
3248 if (co->co_flags & CO_VARARGS)
3249 nargs++;
3250 if (co->co_flags & CO_VARKEYWORDS)
3251 nargs++;
Tim Peters5ca576e2001-06-18 22:08:13 +00003252
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003253 /* Initialize each cell var, taking into account
3254 cell vars that are initialized from arguments.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003255
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003256 Should arrange for the compiler to put cellvars
3257 that are arguments at the beginning of the cellvars
3258 list so that we can march over it more efficiently?
3259 */
3260 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
3261 cellname = PyUnicode_AS_UNICODE(
3262 PyTuple_GET_ITEM(co->co_cellvars, i));
3263 found = 0;
3264 for (j = 0; j < nargs; j++) {
3265 argname = PyUnicode_AS_UNICODE(
3266 PyTuple_GET_ITEM(co->co_varnames, j));
3267 if (Py_UNICODE_strcmp(cellname, argname) == 0) {
3268 c = PyCell_New(GETLOCAL(j));
3269 if (c == NULL)
3270 goto fail;
3271 GETLOCAL(co->co_nlocals + i) = c;
3272 found = 1;
3273 break;
3274 }
3275 }
3276 if (found == 0) {
3277 c = PyCell_New(NULL);
3278 if (c == NULL)
3279 goto fail;
3280 SETLOCAL(co->co_nlocals + i, c);
3281 }
3282 }
3283 }
3284 if (PyTuple_GET_SIZE(co->co_freevars)) {
3285 int i;
3286 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3287 PyObject *o = PyTuple_GET_ITEM(closure, i);
3288 Py_INCREF(o);
3289 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
3290 }
3291 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003293 if (co->co_flags & CO_GENERATOR) {
3294 /* Don't need to keep the reference to f_back, it will be set
3295 * when the generator is resumed. */
3296 Py_XDECREF(f->f_back);
3297 f->f_back = NULL;
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003299 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003300
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003301 /* Create a new generator that owns the ready to run frame
3302 * and return that as the value. */
3303 return PyGen_New(f);
3304 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003306 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003307
Thomas Woutersce272b62007-09-19 21:19:28 +00003308fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003310 /* decref'ing the frame can cause __del__ methods to get invoked,
3311 which can call back into Python. While we're done with the
3312 current Python frame (f), the associated C stack is still in use,
3313 so recursion_depth must be boosted for the duration.
3314 */
3315 assert(tstate != NULL);
3316 ++tstate->recursion_depth;
3317 Py_DECREF(f);
3318 --tstate->recursion_depth;
3319 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003320}
3321
3322
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003323static PyObject *
3324special_lookup(PyObject *o, char *meth, PyObject **cache)
3325{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003326 PyObject *res;
3327 res = _PyObject_LookupSpecial(o, meth, cache);
3328 if (res == NULL && !PyErr_Occurred()) {
3329 PyErr_SetObject(PyExc_AttributeError, *cache);
3330 return NULL;
3331 }
3332 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003333}
3334
3335
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003336/* Logic for the raise statement (too complicated for inlining).
3337 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00003338static enum why_code
Collin Winter828f04a2007-08-31 00:04:24 +00003339do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003340{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003341 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003343 if (exc == NULL) {
3344 /* Reraise */
3345 PyThreadState *tstate = PyThreadState_GET();
3346 PyObject *tb;
3347 type = tstate->exc_type;
3348 value = tstate->exc_value;
3349 tb = tstate->exc_traceback;
3350 if (type == Py_None) {
3351 PyErr_SetString(PyExc_RuntimeError,
3352 "No active exception to reraise");
3353 return WHY_EXCEPTION;
3354 }
3355 Py_XINCREF(type);
3356 Py_XINCREF(value);
3357 Py_XINCREF(tb);
3358 PyErr_Restore(type, value, tb);
3359 return WHY_RERAISE;
3360 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003361
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003362 /* We support the following forms of raise:
3363 raise
Collin Winter828f04a2007-08-31 00:04:24 +00003364 raise <instance>
3365 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003366
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003367 if (PyExceptionClass_Check(exc)) {
3368 type = exc;
3369 value = PyObject_CallObject(exc, NULL);
3370 if (value == NULL)
3371 goto raise_error;
3372 }
3373 else if (PyExceptionInstance_Check(exc)) {
3374 value = exc;
3375 type = PyExceptionInstance_Class(exc);
3376 Py_INCREF(type);
3377 }
3378 else {
3379 /* Not something you can raise. You get an exception
3380 anyway, just not what you specified :-) */
3381 Py_DECREF(exc);
3382 PyErr_SetString(PyExc_TypeError,
3383 "exceptions must derive from BaseException");
3384 goto raise_error;
3385 }
Collin Winter828f04a2007-08-31 00:04:24 +00003386
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003387 if (cause) {
3388 PyObject *fixed_cause;
3389 if (PyExceptionClass_Check(cause)) {
3390 fixed_cause = PyObject_CallObject(cause, NULL);
3391 if (fixed_cause == NULL)
3392 goto raise_error;
3393 Py_DECREF(cause);
3394 }
3395 else if (PyExceptionInstance_Check(cause)) {
3396 fixed_cause = cause;
3397 }
3398 else {
3399 PyErr_SetString(PyExc_TypeError,
3400 "exception causes must derive from "
3401 "BaseException");
3402 goto raise_error;
3403 }
3404 PyException_SetCause(value, fixed_cause);
3405 }
Collin Winter828f04a2007-08-31 00:04:24 +00003406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003407 PyErr_SetObject(type, value);
3408 /* PyErr_SetObject incref's its arguments */
3409 Py_XDECREF(value);
3410 Py_XDECREF(type);
3411 return WHY_EXCEPTION;
Collin Winter828f04a2007-08-31 00:04:24 +00003412
3413raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003414 Py_XDECREF(value);
3415 Py_XDECREF(type);
3416 Py_XDECREF(cause);
3417 return WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003418}
3419
Tim Petersd6d010b2001-06-21 02:49:55 +00003420/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00003421 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003422
Guido van Rossum0368b722007-05-11 16:50:42 +00003423 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
3424 with a variable target.
3425*/
Tim Petersd6d010b2001-06-21 02:49:55 +00003426
Barry Warsawe42b18f1997-08-25 22:13:04 +00003427static int
Guido van Rossum0368b722007-05-11 16:50:42 +00003428unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003429{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003430 int i = 0, j = 0;
3431 Py_ssize_t ll = 0;
3432 PyObject *it; /* iter(v) */
3433 PyObject *w;
3434 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00003435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003436 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00003437
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003438 it = PyObject_GetIter(v);
3439 if (it == NULL)
3440 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00003441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003442 for (; i < argcnt; i++) {
3443 w = PyIter_Next(it);
3444 if (w == NULL) {
3445 /* Iterator done, via error or exhaustion. */
3446 if (!PyErr_Occurred()) {
3447 PyErr_Format(PyExc_ValueError,
3448 "need more than %d value%s to unpack",
3449 i, i == 1 ? "" : "s");
3450 }
3451 goto Error;
3452 }
3453 *--sp = w;
3454 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003455
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003456 if (argcntafter == -1) {
3457 /* We better have exhausted the iterator now. */
3458 w = PyIter_Next(it);
3459 if (w == NULL) {
3460 if (PyErr_Occurred())
3461 goto Error;
3462 Py_DECREF(it);
3463 return 1;
3464 }
3465 Py_DECREF(w);
Georg Brandl0310a832010-07-10 10:32:36 +00003466 PyErr_Format(PyExc_ValueError, "too many values to unpack "
3467 "(expected %d)", argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003468 goto Error;
3469 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003471 l = PySequence_List(it);
3472 if (l == NULL)
3473 goto Error;
3474 *--sp = l;
3475 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00003476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003477 ll = PyList_GET_SIZE(l);
3478 if (ll < argcntafter) {
3479 PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack",
3480 argcnt + ll);
3481 goto Error;
3482 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003484 /* Pop the "after-variable" args off the list. */
3485 for (j = argcntafter; j > 0; j--, i++) {
3486 *--sp = PyList_GET_ITEM(l, ll - j);
3487 }
3488 /* Resize the list. */
3489 Py_SIZE(l) = ll - argcntafter;
3490 Py_DECREF(it);
3491 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00003492
Tim Petersd6d010b2001-06-21 02:49:55 +00003493Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003494 for (; i > 0; i--, sp++)
3495 Py_DECREF(*sp);
3496 Py_XDECREF(it);
3497 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003498}
3499
3500
Guido van Rossum96a42c81992-01-12 02:29:51 +00003501#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003502static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003503prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003504{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003505 printf("%s ", str);
3506 if (PyObject_Print(v, stdout, 0) != 0)
3507 PyErr_Clear(); /* Don't know what else to do */
3508 printf("\n");
3509 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003510}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003511#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003512
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003513static void
Fred Drake5755ce62001-06-27 19:19:46 +00003514call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003515{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003516 PyObject *type, *value, *traceback, *arg;
3517 int err;
3518 PyErr_Fetch(&type, &value, &traceback);
3519 if (value == NULL) {
3520 value = Py_None;
3521 Py_INCREF(value);
3522 }
3523 arg = PyTuple_Pack(3, type, value, traceback);
3524 if (arg == NULL) {
3525 PyErr_Restore(type, value, traceback);
3526 return;
3527 }
3528 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
3529 Py_DECREF(arg);
3530 if (err == 0)
3531 PyErr_Restore(type, value, traceback);
3532 else {
3533 Py_XDECREF(type);
3534 Py_XDECREF(value);
3535 Py_XDECREF(traceback);
3536 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003537}
3538
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003539static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003540call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003541 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003542{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003543 PyObject *type, *value, *traceback;
3544 int err;
3545 PyErr_Fetch(&type, &value, &traceback);
3546 err = call_trace(func, obj, frame, what, arg);
3547 if (err == 0)
3548 {
3549 PyErr_Restore(type, value, traceback);
3550 return 0;
3551 }
3552 else {
3553 Py_XDECREF(type);
3554 Py_XDECREF(value);
3555 Py_XDECREF(traceback);
3556 return -1;
3557 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003558}
3559
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003560static int
Fred Drake5755ce62001-06-27 19:19:46 +00003561call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003562 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003563{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003564 register PyThreadState *tstate = frame->f_tstate;
3565 int result;
3566 if (tstate->tracing)
3567 return 0;
3568 tstate->tracing++;
3569 tstate->use_tracing = 0;
3570 result = func(obj, frame, what, arg);
3571 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3572 || (tstate->c_profilefunc != NULL));
3573 tstate->tracing--;
3574 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003575}
3576
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003577PyObject *
3578_PyEval_CallTracing(PyObject *func, PyObject *args)
3579{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003580 PyFrameObject *frame = PyEval_GetFrame();
3581 PyThreadState *tstate = frame->f_tstate;
3582 int save_tracing = tstate->tracing;
3583 int save_use_tracing = tstate->use_tracing;
3584 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003586 tstate->tracing = 0;
3587 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3588 || (tstate->c_profilefunc != NULL));
3589 result = PyObject_Call(func, args, NULL);
3590 tstate->tracing = save_tracing;
3591 tstate->use_tracing = save_use_tracing;
3592 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003593}
3594
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003595/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00003596static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003597maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003598 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3599 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003600{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003601 int result = 0;
3602 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003603
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003604 /* If the last instruction executed isn't in the current
3605 instruction window, reset the window.
3606 */
3607 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
3608 PyAddrPair bounds;
3609 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3610 &bounds);
3611 *instr_lb = bounds.ap_lower;
3612 *instr_ub = bounds.ap_upper;
3613 }
3614 /* If the last instruction falls at the start of a line or if
3615 it represents a jump backwards, update the frame's line
3616 number and call the trace function. */
3617 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
3618 frame->f_lineno = line;
3619 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
3620 }
3621 *instr_prev = frame->f_lasti;
3622 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003623}
3624
Fred Drake5755ce62001-06-27 19:19:46 +00003625void
3626PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003627{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003628 PyThreadState *tstate = PyThreadState_GET();
3629 PyObject *temp = tstate->c_profileobj;
3630 Py_XINCREF(arg);
3631 tstate->c_profilefunc = NULL;
3632 tstate->c_profileobj = NULL;
3633 /* Must make sure that tracing is not ignored if 'temp' is freed */
3634 tstate->use_tracing = tstate->c_tracefunc != NULL;
3635 Py_XDECREF(temp);
3636 tstate->c_profilefunc = func;
3637 tstate->c_profileobj = arg;
3638 /* Flag that tracing or profiling is turned on */
3639 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003640}
3641
3642void
3643PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3644{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003645 PyThreadState *tstate = PyThreadState_GET();
3646 PyObject *temp = tstate->c_traceobj;
3647 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
3648 Py_XINCREF(arg);
3649 tstate->c_tracefunc = NULL;
3650 tstate->c_traceobj = NULL;
3651 /* Must make sure that profiling is not ignored if 'temp' is freed */
3652 tstate->use_tracing = tstate->c_profilefunc != NULL;
3653 Py_XDECREF(temp);
3654 tstate->c_tracefunc = func;
3655 tstate->c_traceobj = arg;
3656 /* Flag that tracing or profiling is turned on */
3657 tstate->use_tracing = ((func != NULL)
3658 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003659}
3660
Guido van Rossumb209a111997-04-29 18:18:01 +00003661PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003662PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003663{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003664 PyFrameObject *current_frame = PyEval_GetFrame();
3665 if (current_frame == NULL)
3666 return PyThreadState_GET()->interp->builtins;
3667 else
3668 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003669}
3670
Guido van Rossumb209a111997-04-29 18:18:01 +00003671PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003672PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003673{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003674 PyFrameObject *current_frame = PyEval_GetFrame();
3675 if (current_frame == NULL)
3676 return NULL;
3677 PyFrame_FastToLocals(current_frame);
3678 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00003679}
3680
Guido van Rossumb209a111997-04-29 18:18:01 +00003681PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003682PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003683{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003684 PyFrameObject *current_frame = PyEval_GetFrame();
3685 if (current_frame == NULL)
3686 return NULL;
3687 else
3688 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003689}
3690
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003691PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003692PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003693{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003694 PyThreadState *tstate = PyThreadState_GET();
3695 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003696}
3697
Guido van Rossum6135a871995-01-09 17:53:26 +00003698int
Tim Peters5ba58662001-07-16 02:29:45 +00003699PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003700{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003701 PyFrameObject *current_frame = PyEval_GetFrame();
3702 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003703
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003704 if (current_frame != NULL) {
3705 const int codeflags = current_frame->f_code->co_flags;
3706 const int compilerflags = codeflags & PyCF_MASK;
3707 if (compilerflags) {
3708 result = 1;
3709 cf->cf_flags |= compilerflags;
3710 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003711#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003712 if (codeflags & CO_GENERATOR_ALLOWED) {
3713 result = 1;
3714 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3715 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003716#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003717 }
3718 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003719}
3720
Guido van Rossum3f5da241990-12-20 15:06:42 +00003721
Guido van Rossum681d79a1995-07-18 14:51:37 +00003722/* External interface to call any callable object.
Antoine Pitrou8689a102010-04-01 16:53:15 +00003723 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00003724
Guido van Rossumb209a111997-04-29 18:18:01 +00003725PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003726PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003727{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003728 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003729
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003730 if (arg == NULL) {
3731 arg = PyTuple_New(0);
3732 if (arg == NULL)
3733 return NULL;
3734 }
3735 else if (!PyTuple_Check(arg)) {
3736 PyErr_SetString(PyExc_TypeError,
3737 "argument list must be a tuple");
3738 return NULL;
3739 }
3740 else
3741 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003743 if (kw != NULL && !PyDict_Check(kw)) {
3744 PyErr_SetString(PyExc_TypeError,
3745 "keyword list must be a dictionary");
3746 Py_DECREF(arg);
3747 return NULL;
3748 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00003749
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003750 result = PyObject_Call(func, arg, kw);
3751 Py_DECREF(arg);
3752 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00003753}
3754
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003755const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003756PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003757{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003758 if (PyMethod_Check(func))
3759 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
3760 else if (PyFunction_Check(func))
3761 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
3762 else if (PyCFunction_Check(func))
3763 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3764 else
3765 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00003766}
3767
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003768const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003769PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003770{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003771 if (PyMethod_Check(func))
3772 return "()";
3773 else if (PyFunction_Check(func))
3774 return "()";
3775 else if (PyCFunction_Check(func))
3776 return "()";
3777 else
3778 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00003779}
3780
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003781static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003782err_args(PyObject *func, int flags, int nargs)
3783{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003784 if (flags & METH_NOARGS)
3785 PyErr_Format(PyExc_TypeError,
3786 "%.200s() takes no arguments (%d given)",
3787 ((PyCFunctionObject *)func)->m_ml->ml_name,
3788 nargs);
3789 else
3790 PyErr_Format(PyExc_TypeError,
3791 "%.200s() takes exactly one argument (%d given)",
3792 ((PyCFunctionObject *)func)->m_ml->ml_name,
3793 nargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003794}
3795
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003796#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003797if (tstate->use_tracing && tstate->c_profilefunc) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003798 if (call_trace(tstate->c_profilefunc, \
3799 tstate->c_profileobj, \
3800 tstate->frame, PyTrace_C_CALL, \
3801 func)) { \
3802 x = NULL; \
3803 } \
3804 else { \
3805 x = call; \
3806 if (tstate->c_profilefunc != NULL) { \
3807 if (x == NULL) { \
3808 call_trace_protected(tstate->c_profilefunc, \
3809 tstate->c_profileobj, \
3810 tstate->frame, PyTrace_C_EXCEPTION, \
3811 func); \
3812 /* XXX should pass (type, value, tb) */ \
3813 } else { \
3814 if (call_trace(tstate->c_profilefunc, \
3815 tstate->c_profileobj, \
3816 tstate->frame, PyTrace_C_RETURN, \
3817 func)) { \
3818 Py_DECREF(x); \
3819 x = NULL; \
3820 } \
3821 } \
3822 } \
3823 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003824} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003825 x = call; \
3826 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003827
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003828static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003829call_function(PyObject ***pp_stack, int oparg
3830#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003831 , uint64* pintr0, uint64* pintr1
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003832#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003833 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003834{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003835 int na = oparg & 0xff;
3836 int nk = (oparg>>8) & 0xff;
3837 int n = na + 2 * nk;
3838 PyObject **pfunc = (*pp_stack) - n - 1;
3839 PyObject *func = *pfunc;
3840 PyObject *x, *w;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003841
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003842 /* Always dispatch PyCFunction first, because these are
3843 presumed to be the most frequent callable object.
3844 */
3845 if (PyCFunction_Check(func) && nk == 0) {
3846 int flags = PyCFunction_GET_FLAGS(func);
3847 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003848
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003849 PCALL(PCALL_CFUNCTION);
3850 if (flags & (METH_NOARGS | METH_O)) {
3851 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3852 PyObject *self = PyCFunction_GET_SELF(func);
3853 if (flags & METH_NOARGS && na == 0) {
3854 C_TRACE(x, (*meth)(self,NULL));
3855 }
3856 else if (flags & METH_O && na == 1) {
3857 PyObject *arg = EXT_POP(*pp_stack);
3858 C_TRACE(x, (*meth)(self,arg));
3859 Py_DECREF(arg);
3860 }
3861 else {
3862 err_args(func, flags, na);
3863 x = NULL;
3864 }
3865 }
3866 else {
3867 PyObject *callargs;
3868 callargs = load_args(pp_stack, na);
3869 READ_TIMESTAMP(*pintr0);
3870 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
3871 READ_TIMESTAMP(*pintr1);
3872 Py_XDECREF(callargs);
3873 }
3874 } else {
3875 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3876 /* optimize access to bound methods */
3877 PyObject *self = PyMethod_GET_SELF(func);
3878 PCALL(PCALL_METHOD);
3879 PCALL(PCALL_BOUND_METHOD);
3880 Py_INCREF(self);
3881 func = PyMethod_GET_FUNCTION(func);
3882 Py_INCREF(func);
3883 Py_DECREF(*pfunc);
3884 *pfunc = self;
3885 na++;
3886 n++;
3887 } else
3888 Py_INCREF(func);
3889 READ_TIMESTAMP(*pintr0);
3890 if (PyFunction_Check(func))
3891 x = fast_function(func, pp_stack, n, na, nk);
3892 else
3893 x = do_call(func, pp_stack, na, nk);
3894 READ_TIMESTAMP(*pintr1);
3895 Py_DECREF(func);
3896 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003897
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003898 /* Clear the stack of the function object. Also removes
3899 the arguments in case they weren't consumed already
3900 (fast_function() and err_args() leave them on the stack).
3901 */
3902 while ((*pp_stack) > pfunc) {
3903 w = EXT_POP(*pp_stack);
3904 Py_DECREF(w);
3905 PCALL(PCALL_POP);
3906 }
3907 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003908}
3909
Jeremy Hylton192690e2002-08-16 18:36:11 +00003910/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003911 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003912 For the simplest case -- a function that takes only positional
3913 arguments and is called with only positional arguments -- it
3914 inlines the most primitive frame setup code from
3915 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3916 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003917*/
3918
3919static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003920fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003921{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003922 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
3923 PyObject *globals = PyFunction_GET_GLOBALS(func);
3924 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3925 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
3926 PyObject **d = NULL;
3927 int nd = 0;
Jeremy Hylton52820442001-01-03 23:52:36 +00003928
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003929 PCALL(PCALL_FUNCTION);
3930 PCALL(PCALL_FAST_FUNCTION);
3931 if (argdefs == NULL && co->co_argcount == n &&
3932 co->co_kwonlyargcount == 0 && nk==0 &&
3933 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3934 PyFrameObject *f;
3935 PyObject *retval = NULL;
3936 PyThreadState *tstate = PyThreadState_GET();
3937 PyObject **fastlocals, **stack;
3938 int i;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003939
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003940 PCALL(PCALL_FASTER_FUNCTION);
3941 assert(globals != NULL);
3942 /* XXX Perhaps we should create a specialized
3943 PyFrame_New() that doesn't take locals, but does
3944 take builtins without sanity checking them.
3945 */
3946 assert(tstate != NULL);
3947 f = PyFrame_New(tstate, co, globals, NULL);
3948 if (f == NULL)
3949 return NULL;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003950
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003951 fastlocals = f->f_localsplus;
3952 stack = (*pp_stack) - n;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003953
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003954 for (i = 0; i < n; i++) {
3955 Py_INCREF(*stack);
3956 fastlocals[i] = *stack++;
3957 }
3958 retval = PyEval_EvalFrameEx(f,0);
3959 ++tstate->recursion_depth;
3960 Py_DECREF(f);
3961 --tstate->recursion_depth;
3962 return retval;
3963 }
3964 if (argdefs != NULL) {
3965 d = &PyTuple_GET_ITEM(argdefs, 0);
3966 nd = Py_SIZE(argdefs);
3967 }
3968 return PyEval_EvalCodeEx(co, globals,
3969 (PyObject *)NULL, (*pp_stack)-n, na,
3970 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
3971 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003972}
3973
3974static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003975update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3976 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003977{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003978 PyObject *kwdict = NULL;
3979 if (orig_kwdict == NULL)
3980 kwdict = PyDict_New();
3981 else {
3982 kwdict = PyDict_Copy(orig_kwdict);
3983 Py_DECREF(orig_kwdict);
3984 }
3985 if (kwdict == NULL)
3986 return NULL;
3987 while (--nk >= 0) {
3988 int err;
3989 PyObject *value = EXT_POP(*pp_stack);
3990 PyObject *key = EXT_POP(*pp_stack);
3991 if (PyDict_GetItem(kwdict, key) != NULL) {
3992 PyErr_Format(PyExc_TypeError,
3993 "%.200s%s got multiple values "
3994 "for keyword argument '%U'",
3995 PyEval_GetFuncName(func),
3996 PyEval_GetFuncDesc(func),
3997 key);
3998 Py_DECREF(key);
3999 Py_DECREF(value);
4000 Py_DECREF(kwdict);
4001 return NULL;
4002 }
4003 err = PyDict_SetItem(kwdict, key, value);
4004 Py_DECREF(key);
4005 Py_DECREF(value);
4006 if (err) {
4007 Py_DECREF(kwdict);
4008 return NULL;
4009 }
4010 }
4011 return kwdict;
Jeremy Hylton52820442001-01-03 23:52:36 +00004012}
4013
4014static PyObject *
4015update_star_args(int nstack, int nstar, PyObject *stararg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004016 PyObject ***pp_stack)
Jeremy Hylton52820442001-01-03 23:52:36 +00004017{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004018 PyObject *callargs, *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004019
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004020 callargs = PyTuple_New(nstack + nstar);
4021 if (callargs == NULL) {
4022 return NULL;
4023 }
4024 if (nstar) {
4025 int i;
4026 for (i = 0; i < nstar; i++) {
4027 PyObject *a = PyTuple_GET_ITEM(stararg, i);
4028 Py_INCREF(a);
4029 PyTuple_SET_ITEM(callargs, nstack + i, a);
4030 }
4031 }
4032 while (--nstack >= 0) {
4033 w = EXT_POP(*pp_stack);
4034 PyTuple_SET_ITEM(callargs, nstack, w);
4035 }
4036 return callargs;
Jeremy Hylton52820442001-01-03 23:52:36 +00004037}
4038
4039static PyObject *
4040load_args(PyObject ***pp_stack, int na)
4041{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004042 PyObject *args = PyTuple_New(na);
4043 PyObject *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004045 if (args == NULL)
4046 return NULL;
4047 while (--na >= 0) {
4048 w = EXT_POP(*pp_stack);
4049 PyTuple_SET_ITEM(args, na, w);
4050 }
4051 return args;
Jeremy Hylton52820442001-01-03 23:52:36 +00004052}
4053
4054static PyObject *
4055do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4056{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004057 PyObject *callargs = NULL;
4058 PyObject *kwdict = NULL;
4059 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004060
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004061 if (nk > 0) {
4062 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
4063 if (kwdict == NULL)
4064 goto call_fail;
4065 }
4066 callargs = load_args(pp_stack, na);
4067 if (callargs == NULL)
4068 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004069#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004070 /* At this point, we have to look at the type of func to
4071 update the call stats properly. Do it here so as to avoid
4072 exposing the call stats machinery outside ceval.c
4073 */
4074 if (PyFunction_Check(func))
4075 PCALL(PCALL_FUNCTION);
4076 else if (PyMethod_Check(func))
4077 PCALL(PCALL_METHOD);
4078 else if (PyType_Check(func))
4079 PCALL(PCALL_TYPE);
4080 else if (PyCFunction_Check(func))
4081 PCALL(PCALL_CFUNCTION);
4082 else
4083 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004084#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004085 if (PyCFunction_Check(func)) {
4086 PyThreadState *tstate = PyThreadState_GET();
4087 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4088 }
4089 else
4090 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00004091call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004092 Py_XDECREF(callargs);
4093 Py_XDECREF(kwdict);
4094 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004095}
4096
4097static PyObject *
4098ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4099{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004100 int nstar = 0;
4101 PyObject *callargs = NULL;
4102 PyObject *stararg = NULL;
4103 PyObject *kwdict = NULL;
4104 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004105
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004106 if (flags & CALL_FLAG_KW) {
4107 kwdict = EXT_POP(*pp_stack);
4108 if (!PyDict_Check(kwdict)) {
4109 PyObject *d;
4110 d = PyDict_New();
4111 if (d == NULL)
4112 goto ext_call_fail;
4113 if (PyDict_Update(d, kwdict) != 0) {
4114 Py_DECREF(d);
4115 /* PyDict_Update raises attribute
4116 * error (percolated from an attempt
4117 * to get 'keys' attribute) instead of
4118 * a type error if its second argument
4119 * is not a mapping.
4120 */
4121 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4122 PyErr_Format(PyExc_TypeError,
4123 "%.200s%.200s argument after ** "
4124 "must be a mapping, not %.200s",
4125 PyEval_GetFuncName(func),
4126 PyEval_GetFuncDesc(func),
4127 kwdict->ob_type->tp_name);
4128 }
4129 goto ext_call_fail;
4130 }
4131 Py_DECREF(kwdict);
4132 kwdict = d;
4133 }
4134 }
4135 if (flags & CALL_FLAG_VAR) {
4136 stararg = EXT_POP(*pp_stack);
4137 if (!PyTuple_Check(stararg)) {
4138 PyObject *t = NULL;
4139 t = PySequence_Tuple(stararg);
4140 if (t == NULL) {
4141 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4142 PyErr_Format(PyExc_TypeError,
4143 "%.200s%.200s argument after * "
4144 "must be a sequence, not %200s",
4145 PyEval_GetFuncName(func),
4146 PyEval_GetFuncDesc(func),
4147 stararg->ob_type->tp_name);
4148 }
4149 goto ext_call_fail;
4150 }
4151 Py_DECREF(stararg);
4152 stararg = t;
4153 }
4154 nstar = PyTuple_GET_SIZE(stararg);
4155 }
4156 if (nk > 0) {
4157 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
4158 if (kwdict == NULL)
4159 goto ext_call_fail;
4160 }
4161 callargs = update_star_args(na, nstar, stararg, pp_stack);
4162 if (callargs == NULL)
4163 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004164#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004165 /* At this point, we have to look at the type of func to
4166 update the call stats properly. Do it here so as to avoid
4167 exposing the call stats machinery outside ceval.c
4168 */
4169 if (PyFunction_Check(func))
4170 PCALL(PCALL_FUNCTION);
4171 else if (PyMethod_Check(func))
4172 PCALL(PCALL_METHOD);
4173 else if (PyType_Check(func))
4174 PCALL(PCALL_TYPE);
4175 else if (PyCFunction_Check(func))
4176 PCALL(PCALL_CFUNCTION);
4177 else
4178 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004179#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004180 if (PyCFunction_Check(func)) {
4181 PyThreadState *tstate = PyThreadState_GET();
4182 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4183 }
4184 else
4185 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00004186ext_call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004187 Py_XDECREF(callargs);
4188 Py_XDECREF(kwdict);
4189 Py_XDECREF(stararg);
4190 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004191}
4192
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004193/* Extract a slice index from a PyInt or PyLong or an object with the
4194 nb_index slot defined, and store in *pi.
4195 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4196 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 +00004197 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004198*/
Tim Petersb5196382001-12-16 19:44:20 +00004199/* Note: If v is NULL, return success without storing into *pi. This
4200 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4201 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004202*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004203int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004204_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004205{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004206 if (v != NULL) {
4207 Py_ssize_t x;
4208 if (PyIndex_Check(v)) {
4209 x = PyNumber_AsSsize_t(v, NULL);
4210 if (x == -1 && PyErr_Occurred())
4211 return 0;
4212 }
4213 else {
4214 PyErr_SetString(PyExc_TypeError,
4215 "slice indices must be integers or "
4216 "None or have an __index__ method");
4217 return 0;
4218 }
4219 *pi = x;
4220 }
4221 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004222}
4223
Guido van Rossum486364b2007-06-30 05:01:58 +00004224#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004225 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004226
Guido van Rossumb209a111997-04-29 18:18:01 +00004227static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004228cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004229{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004230 int res = 0;
4231 switch (op) {
4232 case PyCmp_IS:
4233 res = (v == w);
4234 break;
4235 case PyCmp_IS_NOT:
4236 res = (v != w);
4237 break;
4238 case PyCmp_IN:
4239 res = PySequence_Contains(w, v);
4240 if (res < 0)
4241 return NULL;
4242 break;
4243 case PyCmp_NOT_IN:
4244 res = PySequence_Contains(w, v);
4245 if (res < 0)
4246 return NULL;
4247 res = !res;
4248 break;
4249 case PyCmp_EXC_MATCH:
4250 if (PyTuple_Check(w)) {
4251 Py_ssize_t i, length;
4252 length = PyTuple_Size(w);
4253 for (i = 0; i < length; i += 1) {
4254 PyObject *exc = PyTuple_GET_ITEM(w, i);
4255 if (!PyExceptionClass_Check(exc)) {
4256 PyErr_SetString(PyExc_TypeError,
4257 CANNOT_CATCH_MSG);
4258 return NULL;
4259 }
4260 }
4261 }
4262 else {
4263 if (!PyExceptionClass_Check(w)) {
4264 PyErr_SetString(PyExc_TypeError,
4265 CANNOT_CATCH_MSG);
4266 return NULL;
4267 }
4268 }
4269 res = PyErr_GivenExceptionMatches(v, w);
4270 break;
4271 default:
4272 return PyObject_RichCompare(v, w, op);
4273 }
4274 v = res ? Py_True : Py_False;
4275 Py_INCREF(v);
4276 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004277}
4278
Thomas Wouters52152252000-08-17 22:55:00 +00004279static PyObject *
4280import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004281{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004282 PyObject *x;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004284 x = PyObject_GetAttr(v, name);
4285 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
4286 PyErr_Format(PyExc_ImportError, "cannot import name %S", name);
4287 }
4288 return x;
Thomas Wouters52152252000-08-17 22:55:00 +00004289}
Guido van Rossumac7be682001-01-17 15:42:30 +00004290
Thomas Wouters52152252000-08-17 22:55:00 +00004291static int
4292import_all_from(PyObject *locals, PyObject *v)
4293{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004294 PyObject *all = PyObject_GetAttrString(v, "__all__");
4295 PyObject *dict, *name, *value;
4296 int skip_leading_underscores = 0;
4297 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004299 if (all == NULL) {
4300 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4301 return -1; /* Unexpected error */
4302 PyErr_Clear();
4303 dict = PyObject_GetAttrString(v, "__dict__");
4304 if (dict == NULL) {
4305 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4306 return -1;
4307 PyErr_SetString(PyExc_ImportError,
4308 "from-import-* object has no __dict__ and no __all__");
4309 return -1;
4310 }
4311 all = PyMapping_Keys(dict);
4312 Py_DECREF(dict);
4313 if (all == NULL)
4314 return -1;
4315 skip_leading_underscores = 1;
4316 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004318 for (pos = 0, err = 0; ; pos++) {
4319 name = PySequence_GetItem(all, pos);
4320 if (name == NULL) {
4321 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4322 err = -1;
4323 else
4324 PyErr_Clear();
4325 break;
4326 }
4327 if (skip_leading_underscores &&
4328 PyUnicode_Check(name) &&
4329 PyUnicode_AS_UNICODE(name)[0] == '_')
4330 {
4331 Py_DECREF(name);
4332 continue;
4333 }
4334 value = PyObject_GetAttr(v, name);
4335 if (value == NULL)
4336 err = -1;
4337 else if (PyDict_CheckExact(locals))
4338 err = PyDict_SetItem(locals, name, value);
4339 else
4340 err = PyObject_SetItem(locals, name, value);
4341 Py_DECREF(name);
4342 Py_XDECREF(value);
4343 if (err != 0)
4344 break;
4345 }
4346 Py_DECREF(all);
4347 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004348}
4349
Guido van Rossumac7be682001-01-17 15:42:30 +00004350static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004351format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004352{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004353 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004354
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004355 if (!obj)
4356 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004358 obj_str = _PyUnicode_AsString(obj);
4359 if (!obj_str)
4360 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004361
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004362 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00004363}
Guido van Rossum950361c1997-01-24 13:49:28 +00004364
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00004365static void
4366format_exc_unbound(PyCodeObject *co, int oparg)
4367{
4368 PyObject *name;
4369 /* Don't stomp existing exception */
4370 if (PyErr_Occurred())
4371 return;
4372 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
4373 name = PyTuple_GET_ITEM(co->co_cellvars,
4374 oparg);
4375 format_exc_check_arg(
4376 PyExc_UnboundLocalError,
4377 UNBOUNDLOCAL_ERROR_MSG,
4378 name);
4379 } else {
4380 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
4381 PyTuple_GET_SIZE(co->co_cellvars));
4382 format_exc_check_arg(PyExc_NameError,
4383 UNBOUNDFREE_ERROR_MSG, name);
4384 }
4385}
4386
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004387static PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +00004388unicode_concatenate(PyObject *v, PyObject *w,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004389 PyFrameObject *f, unsigned char *next_instr)
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004390{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004391 /* This function implements 'variable += expr' when both arguments
4392 are (Unicode) strings. */
4393 Py_ssize_t v_len = PyUnicode_GET_SIZE(v);
4394 Py_ssize_t w_len = PyUnicode_GET_SIZE(w);
4395 Py_ssize_t new_len = v_len + w_len;
4396 if (new_len < 0) {
4397 PyErr_SetString(PyExc_OverflowError,
4398 "strings are too large to concat");
4399 return NULL;
4400 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00004401
Benjamin Petersone208b7c2010-09-10 23:53:14 +00004402 if (Py_REFCNT(v) == 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004403 /* In the common case, there are 2 references to the value
4404 * stored in 'variable' when the += is performed: one on the
4405 * value stack (in 'v') and one still stored in the
4406 * 'variable'. We try to delete the variable now to reduce
4407 * the refcnt to 1.
4408 */
4409 switch (*next_instr) {
4410 case STORE_FAST:
4411 {
4412 int oparg = PEEKARG();
4413 PyObject **fastlocals = f->f_localsplus;
4414 if (GETLOCAL(oparg) == v)
4415 SETLOCAL(oparg, NULL);
4416 break;
4417 }
4418 case STORE_DEREF:
4419 {
4420 PyObject **freevars = (f->f_localsplus +
4421 f->f_code->co_nlocals);
4422 PyObject *c = freevars[PEEKARG()];
4423 if (PyCell_GET(c) == v)
4424 PyCell_Set(c, NULL);
4425 break;
4426 }
4427 case STORE_NAME:
4428 {
4429 PyObject *names = f->f_code->co_names;
4430 PyObject *name = GETITEM(names, PEEKARG());
4431 PyObject *locals = f->f_locals;
4432 if (PyDict_CheckExact(locals) &&
4433 PyDict_GetItem(locals, name) == v) {
4434 if (PyDict_DelItem(locals, name) != 0) {
4435 PyErr_Clear();
4436 }
4437 }
4438 break;
4439 }
4440 }
4441 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004442
Benjamin Petersone208b7c2010-09-10 23:53:14 +00004443 if (Py_REFCNT(v) == 1 && !PyUnicode_CHECK_INTERNED(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004444 /* Now we own the last reference to 'v', so we can resize it
4445 * in-place.
4446 */
4447 if (PyUnicode_Resize(&v, new_len) != 0) {
4448 /* XXX if PyUnicode_Resize() fails, 'v' has been
4449 * deallocated so it cannot be put back into
4450 * 'variable'. The MemoryError is raised when there
4451 * is no value in 'variable', which might (very
4452 * remotely) be a cause of incompatibilities.
4453 */
4454 return NULL;
4455 }
4456 /* copy 'w' into the newly allocated area of 'v' */
4457 memcpy(PyUnicode_AS_UNICODE(v) + v_len,
4458 PyUnicode_AS_UNICODE(w), w_len*sizeof(Py_UNICODE));
4459 return v;
4460 }
4461 else {
4462 /* When in-place resizing is not an option. */
4463 w = PyUnicode_Concat(v, w);
4464 Py_DECREF(v);
4465 return w;
4466 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004467}
4468
Guido van Rossum950361c1997-01-24 13:49:28 +00004469#ifdef DYNAMIC_EXECUTION_PROFILE
4470
Skip Montanarof118cb12001-10-15 20:51:38 +00004471static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004472getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004473{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004474 int i;
4475 PyObject *l = PyList_New(256);
4476 if (l == NULL) return NULL;
4477 for (i = 0; i < 256; i++) {
4478 PyObject *x = PyLong_FromLong(a[i]);
4479 if (x == NULL) {
4480 Py_DECREF(l);
4481 return NULL;
4482 }
4483 PyList_SetItem(l, i, x);
4484 }
4485 for (i = 0; i < 256; i++)
4486 a[i] = 0;
4487 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004488}
4489
4490PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004491_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004492{
4493#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004494 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00004495#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004496 int i;
4497 PyObject *l = PyList_New(257);
4498 if (l == NULL) return NULL;
4499 for (i = 0; i < 257; i++) {
4500 PyObject *x = getarray(dxpairs[i]);
4501 if (x == NULL) {
4502 Py_DECREF(l);
4503 return NULL;
4504 }
4505 PyList_SetItem(l, i, x);
4506 }
4507 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004508#endif
4509}
4510
4511#endif