blob: 1f78f95b4996acb687a0690b8fa6eff6f46ead68 [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 *);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000138static PyObject * unicode_concatenate(PyObject *, PyObject *,
139 PyFrameObject *, unsigned char *);
Benjamin Peterson876b2f22009-06-28 03:18:59 +0000140static PyObject * special_lookup(PyObject *, char *, PyObject **);
Guido van Rossum374a9221991-04-04 10:40:29 +0000141
Paul Prescode68140d2000-08-30 20:25:01 +0000142#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000143 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000144#define GLOBAL_NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000146#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000148#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 "free variable '%.200s' referenced before assignment" \
150 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000151
Guido van Rossum950361c1997-01-24 13:49:28 +0000152/* Dynamic execution profile */
153#ifdef DYNAMIC_EXECUTION_PROFILE
154#ifdef DXPAIRS
155static long dxpairs[257][256];
156#define dxp dxpairs[256]
157#else
158static long dxp[256];
159#endif
160#endif
161
Jeremy Hylton985eba52003-02-05 23:13:00 +0000162/* Function call profile */
163#ifdef CALL_PROFILE
164#define PCALL_NUM 11
165static int pcall[PCALL_NUM];
166
167#define PCALL_ALL 0
168#define PCALL_FUNCTION 1
169#define PCALL_FAST_FUNCTION 2
170#define PCALL_FASTER_FUNCTION 3
171#define PCALL_METHOD 4
172#define PCALL_BOUND_METHOD 5
173#define PCALL_CFUNCTION 6
174#define PCALL_TYPE 7
175#define PCALL_GENERATOR 8
176#define PCALL_OTHER 9
177#define PCALL_POP 10
178
179/* Notes about the statistics
180
181 PCALL_FAST stats
182
183 FAST_FUNCTION means no argument tuple needs to be created.
184 FASTER_FUNCTION means that the fast-path frame setup code is used.
185
186 If there is a method call where the call can be optimized by changing
187 the argument tuple and calling the function directly, it gets recorded
188 twice.
189
190 As a result, the relationship among the statistics appears to be
191 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
192 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
193 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
194 PCALL_METHOD > PCALL_BOUND_METHOD
195*/
196
197#define PCALL(POS) pcall[POS]++
198
199PyObject *
200PyEval_GetCallStats(PyObject *self)
201{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 return Py_BuildValue("iiiiiiiiiii",
203 pcall[0], pcall[1], pcall[2], pcall[3],
204 pcall[4], pcall[5], pcall[6], pcall[7],
205 pcall[8], pcall[9], pcall[10]);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000206}
207#else
208#define PCALL(O)
209
210PyObject *
211PyEval_GetCallStats(PyObject *self)
212{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 Py_INCREF(Py_None);
214 return Py_None;
Jeremy Hylton985eba52003-02-05 23:13:00 +0000215}
216#endif
217
Tim Peters5ca576e2001-06-18 22:08:13 +0000218
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000219/* This can set eval_breaker to 0 even though gil_drop_request became
220 1. We believe this is all right because the eval loop will release
221 the GIL eventually anyway. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000222#define COMPUTE_EVAL_BREAKER() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 _Py_atomic_store_relaxed( \
224 &eval_breaker, \
225 _Py_atomic_load_relaxed(&gil_drop_request) | \
226 _Py_atomic_load_relaxed(&pendingcalls_to_do) | \
227 pending_async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000228
229#define SET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000230 do { \
231 _Py_atomic_store_relaxed(&gil_drop_request, 1); \
232 _Py_atomic_store_relaxed(&eval_breaker, 1); \
233 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000234
235#define RESET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000236 do { \
237 _Py_atomic_store_relaxed(&gil_drop_request, 0); \
238 COMPUTE_EVAL_BREAKER(); \
239 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000240
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000241/* Pending calls are only modified under pending_lock */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000242#define SIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000243 do { \
244 _Py_atomic_store_relaxed(&pendingcalls_to_do, 1); \
245 _Py_atomic_store_relaxed(&eval_breaker, 1); \
246 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000247
248#define UNSIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000249 do { \
250 _Py_atomic_store_relaxed(&pendingcalls_to_do, 0); \
251 COMPUTE_EVAL_BREAKER(); \
252 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000253
254#define SIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000255 do { \
256 pending_async_exc = 1; \
257 _Py_atomic_store_relaxed(&eval_breaker, 1); \
258 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000259
260#define UNSIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 do { pending_async_exc = 0; COMPUTE_EVAL_BREAKER(); } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000262
263
Guido van Rossume59214e1994-08-30 08:01:59 +0000264#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000265
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000266#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000267#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000268#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000269#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000270
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000271static PyThread_type_lock pending_lock = 0; /* for pending calls */
Guido van Rossuma9672091994-09-14 13:31:22 +0000272static long main_thread = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000273/* This single variable consolidates all requests to break out of the fast path
274 in the eval loop. */
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000275static _Py_atomic_int eval_breaker = {0};
276/* Request for dropping the GIL */
277static _Py_atomic_int gil_drop_request = {0};
278/* Request for running pending calls. */
279static _Py_atomic_int pendingcalls_to_do = {0};
280/* Request for looking at the `async_exc` field of the current thread state.
281 Guarded by the GIL. */
282static int pending_async_exc = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000283
284#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000285
Tim Peters7f468f22004-10-11 02:40:51 +0000286int
287PyEval_ThreadsInitialized(void)
288{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000289 return gil_created();
Tim Peters7f468f22004-10-11 02:40:51 +0000290}
291
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000292void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000293PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000294{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 if (gil_created())
296 return;
297 create_gil();
298 take_gil(PyThreadState_GET());
299 main_thread = PyThread_get_thread_ident();
300 if (!pending_lock)
301 pending_lock = PyThread_allocate_lock();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000302}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000303
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000304void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000305PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000306{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 PyThreadState *tstate = PyThreadState_GET();
308 if (tstate == NULL)
309 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
310 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000311}
312
313void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000314PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000315{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000316 /* This function must succeed when the current thread state is NULL.
317 We therefore avoid PyThreadState_GET() which dumps a fatal error
318 in debug mode.
319 */
320 drop_gil((PyThreadState*)_Py_atomic_load_relaxed(
321 &_PyThreadState_Current));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000322}
323
324void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000325PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000326{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 if (tstate == NULL)
328 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
329 /* Check someone has called PyEval_InitThreads() to create the lock */
330 assert(gil_created());
331 take_gil(tstate);
332 if (PyThreadState_Swap(tstate) != NULL)
333 Py_FatalError(
334 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000335}
336
337void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000338PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000339{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 if (tstate == NULL)
341 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
342 if (PyThreadState_Swap(NULL) != tstate)
343 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
344 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000345}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000346
347/* This function is called from PyOS_AfterFork to ensure that newly
348 created child processes don't hold locks referring to threads which
349 are not running in the child process. (This could also be done using
350 pthread_atfork mechanism, at least for the pthreads implementation.) */
351
352void
353PyEval_ReInitThreads(void)
354{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000355 PyObject *threading, *result;
356 PyThreadState *tstate = PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358 if (!gil_created())
359 return;
360 /*XXX Can't use PyThread_free_lock here because it does too
361 much error-checking. Doing this cleanly would require
362 adding a new function to each thread_*.h. Instead, just
363 create a new lock and waste a little bit of memory */
364 recreate_gil();
365 pending_lock = PyThread_allocate_lock();
366 take_gil(tstate);
367 main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 /* Update the threading module with the new state.
370 */
371 tstate = PyThreadState_GET();
372 threading = PyMapping_GetItemString(tstate->interp->modules,
373 "threading");
374 if (threading == NULL) {
375 /* threading not imported */
376 PyErr_Clear();
377 return;
378 }
379 result = PyObject_CallMethod(threading, "_after_fork", NULL);
380 if (result == NULL)
381 PyErr_WriteUnraisable(threading);
382 else
383 Py_DECREF(result);
384 Py_DECREF(threading);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000385}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000386
387#else
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000388static _Py_atomic_int eval_breaker = {0};
389static _Py_atomic_int gil_drop_request = {0};
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000390static int pending_async_exc = 0;
391#endif /* WITH_THREAD */
392
393/* This function is used to signal that async exceptions are waiting to be
394 raised, therefore it is also useful in non-threaded builds. */
395
396void
397_PyEval_SignalAsyncExc(void)
398{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000400}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000401
Guido van Rossumff4949e1992-08-05 19:58:53 +0000402/* Functions save_thread and restore_thread are always defined so
403 dynamically loaded modules needn't be compiled separately for use
404 with and without threads: */
405
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000406PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000407PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000408{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 PyThreadState *tstate = PyThreadState_Swap(NULL);
410 if (tstate == NULL)
411 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000412#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 if (gil_created())
414 drop_gil(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000415#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000417}
418
419void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000420PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000421{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422 if (tstate == NULL)
423 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000424#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000425 if (gil_created()) {
426 int err = errno;
427 take_gil(tstate);
428 errno = err;
429 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000430#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000432}
433
434
Guido van Rossuma9672091994-09-14 13:31:22 +0000435/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
436 signal handlers or Mac I/O completion routines) can schedule calls
437 to a function to be called synchronously.
438 The synchronous function is called with one void* argument.
439 It should return 0 for success or -1 for failure -- failure should
440 be accompanied by an exception.
441
442 If registry succeeds, the registry function returns 0; if it fails
443 (e.g. due to too many pending calls) it returns -1 (without setting
444 an exception condition).
445
446 Note that because registry may occur from within signal handlers,
447 or other asynchronous events, calling malloc() is unsafe!
448
449#ifdef WITH_THREAD
450 Any thread can schedule pending calls, but only the main thread
451 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000452 There is no facility to schedule calls to a particular thread, but
453 that should be easy to change, should that ever be required. In
454 that case, the static variables here should go into the python
455 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000456#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000457*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000458
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000459#ifdef WITH_THREAD
460
461/* The WITH_THREAD implementation is thread-safe. It allows
462 scheduling to be made from any thread, and even from an executing
463 callback.
464 */
465
466#define NPENDINGCALLS 32
467static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468 int (*func)(void *);
469 void *arg;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000470} pendingcalls[NPENDINGCALLS];
471static int pendingfirst = 0;
472static int pendinglast = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000473static char pendingbusy = 0;
474
475int
476Py_AddPendingCall(int (*func)(void *), void *arg)
477{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 int i, j, result=0;
479 PyThread_type_lock lock = pending_lock;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481 /* try a few times for the lock. Since this mechanism is used
482 * for signal handling (on the main thread), there is a (slim)
483 * chance that a signal is delivered on the same thread while we
484 * hold the lock during the Py_MakePendingCalls() function.
485 * This avoids a deadlock in that case.
486 * Note that signals can be delivered on any thread. In particular,
487 * on Windows, a SIGINT is delivered on a system-created worker
488 * thread.
489 * We also check for lock being NULL, in the unlikely case that
490 * this function is called before any bytecode evaluation takes place.
491 */
492 if (lock != NULL) {
493 for (i = 0; i<100; i++) {
494 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
495 break;
496 }
497 if (i == 100)
498 return -1;
499 }
500
501 i = pendinglast;
502 j = (i + 1) % NPENDINGCALLS;
503 if (j == pendingfirst) {
504 result = -1; /* Queue full */
505 } else {
506 pendingcalls[i].func = func;
507 pendingcalls[i].arg = arg;
508 pendinglast = j;
509 }
510 /* signal main loop */
511 SIGNAL_PENDING_CALLS();
512 if (lock != NULL)
513 PyThread_release_lock(lock);
514 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000515}
516
517int
518Py_MakePendingCalls(void)
519{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 int i;
521 int r = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000522
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 if (!pending_lock) {
524 /* initial allocation of the lock */
525 pending_lock = PyThread_allocate_lock();
526 if (pending_lock == NULL)
527 return -1;
528 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000529
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 /* only service pending calls on main thread */
531 if (main_thread && PyThread_get_thread_ident() != main_thread)
532 return 0;
533 /* don't perform recursive pending calls */
534 if (pendingbusy)
535 return 0;
536 pendingbusy = 1;
537 /* perform a bounded number of calls, in case of recursion */
538 for (i=0; i<NPENDINGCALLS; i++) {
539 int j;
540 int (*func)(void *);
541 void *arg = NULL;
542
543 /* pop one item off the queue while holding the lock */
544 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
545 j = pendingfirst;
546 if (j == pendinglast) {
547 func = NULL; /* Queue empty */
548 } else {
549 func = pendingcalls[j].func;
550 arg = pendingcalls[j].arg;
551 pendingfirst = (j + 1) % NPENDINGCALLS;
552 }
553 if (pendingfirst != pendinglast)
554 SIGNAL_PENDING_CALLS();
555 else
556 UNSIGNAL_PENDING_CALLS();
557 PyThread_release_lock(pending_lock);
558 /* having released the lock, perform the callback */
559 if (func == NULL)
560 break;
561 r = func(arg);
562 if (r)
563 break;
564 }
565 pendingbusy = 0;
566 return r;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000567}
568
569#else /* if ! defined WITH_THREAD */
570
571/*
572 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
573 This code is used for signal handling in python that isn't built
574 with WITH_THREAD.
575 Don't use this implementation when Py_AddPendingCalls() can happen
576 on a different thread!
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577
Guido van Rossuma9672091994-09-14 13:31:22 +0000578 There are two possible race conditions:
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000579 (1) nested asynchronous calls to Py_AddPendingCall()
580 (2) AddPendingCall() calls made while pending calls are being processed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000582 (1) is very unlikely because typically signal delivery
583 is blocked during signal handling. So it should be impossible.
584 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000585 The current code is safe against (2), but not against (1).
586 The safety against (2) is derived from the fact that only one
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000587 thread is present, interrupted by signals, and that the critical
588 section is protected with the "busy" variable. On Windows, which
589 delivers SIGINT on a system thread, this does not hold and therefore
590 Windows really shouldn't use this version.
591 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000592*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000593
Guido van Rossuma9672091994-09-14 13:31:22 +0000594#define NPENDINGCALLS 32
595static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596 int (*func)(void *);
597 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000598} pendingcalls[NPENDINGCALLS];
599static volatile int pendingfirst = 0;
600static volatile int pendinglast = 0;
Benjamin Peterson08ec84c2010-05-30 14:49:32 +0000601static _Py_atomic_int pendingcalls_to_do = {0};
Guido van Rossuma9672091994-09-14 13:31:22 +0000602
603int
Thomas Wouters334fb892000-07-25 12:56:38 +0000604Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000605{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 static volatile int busy = 0;
607 int i, j;
608 /* XXX Begin critical section */
609 if (busy)
610 return -1;
611 busy = 1;
612 i = pendinglast;
613 j = (i + 1) % NPENDINGCALLS;
614 if (j == pendingfirst) {
615 busy = 0;
616 return -1; /* Queue full */
617 }
618 pendingcalls[i].func = func;
619 pendingcalls[i].arg = arg;
620 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 SIGNAL_PENDING_CALLS();
623 busy = 0;
624 /* XXX End critical section */
625 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000626}
627
Guido van Rossum180d7b41994-09-29 09:45:57 +0000628int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000629Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000630{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631 static int busy = 0;
632 if (busy)
633 return 0;
634 busy = 1;
635 UNSIGNAL_PENDING_CALLS();
636 for (;;) {
637 int i;
638 int (*func)(void *);
639 void *arg;
640 i = pendingfirst;
641 if (i == pendinglast)
642 break; /* Queue empty */
643 func = pendingcalls[i].func;
644 arg = pendingcalls[i].arg;
645 pendingfirst = (i + 1) % NPENDINGCALLS;
646 if (func(arg) < 0) {
647 busy = 0;
648 SIGNAL_PENDING_CALLS(); /* We're not done yet */
649 return -1;
650 }
651 }
652 busy = 0;
653 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000654}
655
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000656#endif /* WITH_THREAD */
657
Guido van Rossuma9672091994-09-14 13:31:22 +0000658
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000659/* The interpreter's recursion limit */
660
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000661#ifndef Py_DEFAULT_RECURSION_LIMIT
662#define Py_DEFAULT_RECURSION_LIMIT 1000
663#endif
664static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
665int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000666
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000667int
668Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000669{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000670 return recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000671}
672
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000673void
674Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000675{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676 recursion_limit = new_limit;
677 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000678}
679
Armin Rigo2b3eb402003-10-28 12:05:48 +0000680/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
681 if the recursion_depth reaches _Py_CheckRecursionLimit.
682 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
683 to guarantee that _Py_CheckRecursiveCall() is regularly called.
684 Without USE_STACKCHECK, there is no need for this. */
685int
686_Py_CheckRecursiveCall(char *where)
687{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000688 PyThreadState *tstate = PyThreadState_GET();
Armin Rigo2b3eb402003-10-28 12:05:48 +0000689
690#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 if (PyOS_CheckStack()) {
692 --tstate->recursion_depth;
693 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
694 return -1;
695 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000696#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000697 _Py_CheckRecursionLimit = recursion_limit;
698 if (tstate->recursion_critical)
699 /* Somebody asked that we don't check for recursion. */
700 return 0;
701 if (tstate->overflowed) {
702 if (tstate->recursion_depth > recursion_limit + 50) {
703 /* Overflowing while handling an overflow. Give up. */
704 Py_FatalError("Cannot recover from stack overflow.");
705 }
706 return 0;
707 }
708 if (tstate->recursion_depth > recursion_limit) {
709 --tstate->recursion_depth;
710 tstate->overflowed = 1;
711 PyErr_Format(PyExc_RuntimeError,
712 "maximum recursion depth exceeded%s",
713 where);
714 return -1;
715 }
716 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000717}
718
Guido van Rossum374a9221991-04-04 10:40:29 +0000719/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000720enum why_code {
Stefan Krahb7e10102010-06-23 18:42:39 +0000721 WHY_NOT = 0x0001, /* No error */
722 WHY_EXCEPTION = 0x0002, /* Exception occurred */
723 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
724 WHY_RETURN = 0x0008, /* 'return' statement */
725 WHY_BREAK = 0x0010, /* 'break' statement */
726 WHY_CONTINUE = 0x0020, /* 'continue' statement */
727 WHY_YIELD = 0x0040, /* 'yield' operator */
728 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000729};
Guido van Rossum374a9221991-04-04 10:40:29 +0000730
Collin Winter828f04a2007-08-31 00:04:24 +0000731static enum why_code do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000732static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000733
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +0000734/* Records whether tracing is on for any thread. Counts the number of
735 threads for which tstate->c_tracefunc is non-NULL, so if the value
736 is 0, we know we don't have to check this thread's c_tracefunc.
737 This speeds up the if statement in PyEval_EvalFrameEx() after
738 fast_next_opcode*/
739static int _Py_TracingPossible = 0;
740
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000741
Guido van Rossum374a9221991-04-04 10:40:29 +0000742
Guido van Rossumb209a111997-04-29 18:18:01 +0000743PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000744PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000745{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000746 return PyEval_EvalCodeEx(co,
747 globals, locals,
748 (PyObject **)NULL, 0,
749 (PyObject **)NULL, 0,
750 (PyObject **)NULL, 0,
751 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000752}
753
754
755/* Interpreter main loop */
756
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000757PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000758PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759 /* This is for backward compatibility with extension modules that
760 used this API; core interpreter code should call
761 PyEval_EvalFrameEx() */
762 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000763}
764
765PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000766PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000767{
Guido van Rossum950361c1997-01-24 13:49:28 +0000768#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000770#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 register PyObject **stack_pointer; /* Next free slot in value stack */
772 register unsigned char *next_instr;
773 register int opcode; /* Current opcode */
774 register int oparg; /* Current opcode argument, if any */
775 register enum why_code why; /* Reason for block stack unwind */
776 register int err; /* Error status -- nonzero if error */
777 register PyObject *x; /* Result object -- NULL if error */
778 register PyObject *v; /* Temporary objects popped off stack */
779 register PyObject *w;
780 register PyObject *u;
781 register PyObject *t;
782 register PyObject **fastlocals, **freevars;
783 PyObject *retval = NULL; /* Return value */
784 PyThreadState *tstate = PyThreadState_GET();
785 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000786
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000787 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000788
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000790
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791 is true when the line being executed has changed. The
792 initial values are such as to make this false the first
793 time it is tested. */
794 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000795
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 unsigned char *first_instr;
797 PyObject *names;
798 PyObject *consts;
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000799#if defined(Py_DEBUG) || defined(LLTRACE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 /* Make it easier to find out where we are with a debugger */
801 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000802#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000803
Antoine Pitroub52ec782009-01-25 16:34:23 +0000804/* Computed GOTOs, or
805 the-optimization-commonly-but-improperly-known-as-"threaded code"
806 using gcc's labels-as-values extension
807 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
808
809 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000811 combined with a lookup table of jump addresses. However, since the
812 indirect jump instruction is shared by all opcodes, the CPU will have a
813 hard time making the right prediction for where to jump next (actually,
814 it will be always wrong except in the uncommon case of a sequence of
815 several identical opcodes).
816
817 "Threaded code" in contrast, uses an explicit jump table and an explicit
818 indirect jump instruction at the end of each opcode. Since the jump
819 instruction is at a different address for each opcode, the CPU will make a
820 separate prediction for each of these instructions, which is equivalent to
821 predicting the second opcode of each opcode pair. These predictions have
822 a much better chance to turn out valid, especially in small bytecode loops.
823
824 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000826 and potentially many more instructions (depending on the pipeline width).
827 A correctly predicted branch, however, is nearly free.
828
829 At the time of this writing, the "threaded code" version is up to 15-20%
830 faster than the normal "switch" version, depending on the compiler and the
831 CPU architecture.
832
833 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
834 because it would render the measurements invalid.
835
836
837 NOTE: care must be taken that the compiler doesn't try to "optimize" the
838 indirect jumps by sharing them between all opcodes. Such optimizations
839 can be disabled on gcc by using the -fno-gcse flag (or possibly
840 -fno-crossjumping).
841*/
842
Antoine Pitrou042b1282010-08-13 21:15:58 +0000843#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000844#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000845#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000846#endif
847
Antoine Pitrou042b1282010-08-13 21:15:58 +0000848#ifdef HAVE_COMPUTED_GOTOS
849 #ifndef USE_COMPUTED_GOTOS
850 #define USE_COMPUTED_GOTOS 1
851 #endif
852#else
853 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
854 #error "Computed gotos are not supported on this compiler."
855 #endif
856 #undef USE_COMPUTED_GOTOS
857 #define USE_COMPUTED_GOTOS 0
858#endif
859
860#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000861/* Import the static jump table */
862#include "opcode_targets.h"
863
864/* This macro is used when several opcodes defer to the same implementation
865 (e.g. SETUP_LOOP, SETUP_FINALLY) */
866#define TARGET_WITH_IMPL(op, impl) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 TARGET_##op: \
868 opcode = op; \
869 if (HAS_ARG(op)) \
870 oparg = NEXTARG(); \
871 case op: \
872 goto impl; \
Antoine Pitroub52ec782009-01-25 16:34:23 +0000873
874#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 TARGET_##op: \
876 opcode = op; \
877 if (HAS_ARG(op)) \
878 oparg = NEXTARG(); \
879 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000880
881
882#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 { \
884 if (!_Py_atomic_load_relaxed(&eval_breaker)) { \
885 FAST_DISPATCH(); \
886 } \
887 continue; \
888 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000889
890#ifdef LLTRACE
891#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 { \
893 if (!lltrace && !_Py_TracingPossible) { \
894 f->f_lasti = INSTR_OFFSET(); \
895 goto *opcode_targets[*next_instr++]; \
896 } \
897 goto fast_next_opcode; \
898 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000899#else
900#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 { \
902 if (!_Py_TracingPossible) { \
903 f->f_lasti = INSTR_OFFSET(); \
904 goto *opcode_targets[*next_instr++]; \
905 } \
906 goto fast_next_opcode; \
907 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000908#endif
909
910#else
911#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000913#define TARGET_WITH_IMPL(op, impl) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 /* silence compiler warnings about `impl` unused */ \
915 if (0) goto impl; \
916 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000917#define DISPATCH() continue
918#define FAST_DISPATCH() goto fast_next_opcode
919#endif
920
921
Neal Norwitza81d2202002-07-14 00:27:26 +0000922/* Tuple access macros */
923
924#ifndef Py_DEBUG
925#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
926#else
927#define GETITEM(v, i) PyTuple_GetItem((v), (i))
928#endif
929
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000930#ifdef WITH_TSC
931/* Use Pentium timestamp counter to mark certain events:
932 inst0 -- beginning of switch statement for opcode dispatch
933 inst1 -- end of switch statement (may be skipped)
934 loop0 -- the top of the mainloop
Thomas Wouters477c8d52006-05-27 19:21:47 +0000935 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000936 (may be skipped)
937 intr1 -- beginning of long interruption
938 intr2 -- end of long interruption
939
940 Many opcodes call out to helper C functions. In some cases, the
941 time in those functions should be counted towards the time for the
942 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
943 calls another Python function; there's no point in charge all the
944 bytecode executed by the called function to the caller.
945
946 It's hard to make a useful judgement statically. In the presence
947 of operator overloading, it's impossible to tell if a call will
948 execute new Python code or not.
949
950 It's a case-by-case judgement. I'll use intr1 for the following
951 cases:
952
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000953 IMPORT_STAR
954 IMPORT_FROM
955 CALL_FUNCTION (and friends)
956
957 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
959 int ticked = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 READ_TIMESTAMP(inst0);
962 READ_TIMESTAMP(inst1);
963 READ_TIMESTAMP(loop0);
964 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000965
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 /* shut up the compiler */
967 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000968#endif
969
Guido van Rossum374a9221991-04-04 10:40:29 +0000970/* Code access macros */
971
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972#define INSTR_OFFSET() ((int)(next_instr - first_instr))
973#define NEXTOP() (*next_instr++)
974#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
975#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
976#define JUMPTO(x) (next_instr = first_instr + (x))
977#define JUMPBY(x) (next_instr += (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000978
Raymond Hettingerf606f872003-03-16 03:11:04 +0000979/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980 Some opcodes tend to come in pairs thus making it possible to
981 predict the second code when the first is run. For example,
982 COMPARE_OP is often followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And,
983 those opcodes are often followed by a POP_TOP.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000984
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 Verifying the prediction costs a single high-speed test of a register
986 variable against a constant. If the pairing was good, then the
987 processor's own internal branch predication has a high likelihood of
988 success, resulting in a nearly zero-overhead transition to the
989 next opcode. A successful prediction saves a trip through the eval-loop
990 including its two unpredictable branches, the HAS_ARG test and the
991 switch-case. Combined with the processor's internal branch prediction,
992 a successful PREDICT has the effect of making the two opcodes run as if
993 they were a single new opcode with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000994
Georg Brandl86b2fb92008-07-16 03:43:04 +0000995 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 predictions turned-on and interpret the results as if some opcodes
997 had been combined or turn-off predictions so that the opcode frequency
998 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000999
1000 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 the CPU to record separate branch prediction information for each
1002 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001003
Raymond Hettingerf606f872003-03-16 03:11:04 +00001004*/
1005
Antoine Pitrou042b1282010-08-13 21:15:58 +00001006#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007#define PREDICT(op) if (0) goto PRED_##op
1008#define PREDICTED(op) PRED_##op:
1009#define PREDICTED_WITH_ARG(op) PRED_##op:
Raymond Hettingera7216982004-02-08 19:59:27 +00001010#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011#define PREDICT(op) if (*next_instr == op) goto PRED_##op
1012#define PREDICTED(op) PRED_##op: next_instr++
1013#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Antoine Pitroub52ec782009-01-25 16:34:23 +00001014#endif
1015
Raymond Hettingerf606f872003-03-16 03:11:04 +00001016
Guido van Rossum374a9221991-04-04 10:40:29 +00001017/* Stack manipulation macros */
1018
Martin v. Löwis18e16552006-02-15 17:27:45 +00001019/* The stack can grow at most MAXINT deep, as co_nlocals and
1020 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001021#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1022#define EMPTY() (STACK_LEVEL() == 0)
1023#define TOP() (stack_pointer[-1])
1024#define SECOND() (stack_pointer[-2])
1025#define THIRD() (stack_pointer[-3])
1026#define FOURTH() (stack_pointer[-4])
1027#define PEEK(n) (stack_pointer[-(n)])
1028#define SET_TOP(v) (stack_pointer[-1] = (v))
1029#define SET_SECOND(v) (stack_pointer[-2] = (v))
1030#define SET_THIRD(v) (stack_pointer[-3] = (v))
1031#define SET_FOURTH(v) (stack_pointer[-4] = (v))
1032#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
1033#define BASIC_STACKADJ(n) (stack_pointer += n)
1034#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1035#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001036
Guido van Rossum96a42c81992-01-12 02:29:51 +00001037#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001039 lltrace && prtrace(TOP(), "push")); \
1040 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001042 BASIC_POP())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001044 lltrace && prtrace(TOP(), "stackadj")); \
1045 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +00001046#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +00001047 prtrace((STACK_POINTER)[-1], "ext_pop")), \
1048 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001049#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001050#define PUSH(v) BASIC_PUSH(v)
1051#define POP() BASIC_POP()
1052#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001053#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001054#endif
1055
Guido van Rossum681d79a1995-07-18 14:51:37 +00001056/* Local variable macros */
1057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001059
1060/* The SETLOCAL() macro must not DECREF the local variable in-place and
1061 then store the new value; it must copy the old value to a temporary
1062 value, then store the new value, and then DECREF the temporary value.
1063 This is because it is possible that during the DECREF the frame is
1064 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1065 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001067 GETLOCAL(i) = value; \
1068 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001069
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001070
1071#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 while (STACK_LEVEL() > (b)->b_level) { \
1073 PyObject *v = POP(); \
1074 Py_XDECREF(v); \
1075 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001076
1077#define UNWIND_EXCEPT_HANDLER(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 { \
1079 PyObject *type, *value, *traceback; \
1080 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1081 while (STACK_LEVEL() > (b)->b_level + 3) { \
1082 value = POP(); \
1083 Py_XDECREF(value); \
1084 } \
1085 type = tstate->exc_type; \
1086 value = tstate->exc_value; \
1087 traceback = tstate->exc_traceback; \
1088 tstate->exc_type = POP(); \
1089 tstate->exc_value = POP(); \
1090 tstate->exc_traceback = POP(); \
1091 Py_XDECREF(type); \
1092 Py_XDECREF(value); \
1093 Py_XDECREF(traceback); \
1094 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001095
1096#define SAVE_EXC_STATE() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 { \
1098 PyObject *type, *value, *traceback; \
1099 Py_XINCREF(tstate->exc_type); \
1100 Py_XINCREF(tstate->exc_value); \
1101 Py_XINCREF(tstate->exc_traceback); \
1102 type = f->f_exc_type; \
1103 value = f->f_exc_value; \
1104 traceback = f->f_exc_traceback; \
1105 f->f_exc_type = tstate->exc_type; \
1106 f->f_exc_value = tstate->exc_value; \
1107 f->f_exc_traceback = tstate->exc_traceback; \
1108 Py_XDECREF(type); \
1109 Py_XDECREF(value); \
1110 Py_XDECREF(traceback); \
1111 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001112
1113#define SWAP_EXC_STATE() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 { \
1115 PyObject *tmp; \
1116 tmp = tstate->exc_type; \
1117 tstate->exc_type = f->f_exc_type; \
1118 f->f_exc_type = tmp; \
1119 tmp = tstate->exc_value; \
1120 tstate->exc_value = f->f_exc_value; \
1121 f->f_exc_value = tmp; \
1122 tmp = tstate->exc_traceback; \
1123 tstate->exc_traceback = f->f_exc_traceback; \
1124 f->f_exc_traceback = tmp; \
1125 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001126
Guido van Rossuma027efa1997-05-05 20:56:21 +00001127/* Start of code */
1128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 if (f == NULL)
1130 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001131
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001132 /* push frame */
1133 if (Py_EnterRecursiveCall(""))
1134 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001135
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001136 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001137
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001138 if (tstate->use_tracing) {
1139 if (tstate->c_tracefunc != NULL) {
1140 /* tstate->c_tracefunc, if defined, is a
1141 function that will be called on *every* entry
1142 to a code block. Its return value, if not
1143 None, is a function that will be called at
1144 the start of each executed line of code.
1145 (Actually, the function must return itself
1146 in order to continue tracing.) The trace
1147 functions are called with three arguments:
1148 a pointer to the current frame, a string
1149 indicating why the function is called, and
1150 an argument which depends on the situation.
1151 The global trace function is also called
1152 whenever an exception is detected. */
1153 if (call_trace_protected(tstate->c_tracefunc,
1154 tstate->c_traceobj,
1155 f, PyTrace_CALL, Py_None)) {
1156 /* Trace function raised an error */
1157 goto exit_eval_frame;
1158 }
1159 }
1160 if (tstate->c_profilefunc != NULL) {
1161 /* Similar for c_profilefunc, except it needn't
1162 return itself and isn't called for "line" events */
1163 if (call_trace_protected(tstate->c_profilefunc,
1164 tstate->c_profileobj,
1165 f, PyTrace_CALL, Py_None)) {
1166 /* Profile function raised an error */
1167 goto exit_eval_frame;
1168 }
1169 }
1170 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001171
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 co = f->f_code;
1173 names = co->co_names;
1174 consts = co->co_consts;
1175 fastlocals = f->f_localsplus;
1176 freevars = f->f_localsplus + co->co_nlocals;
1177 first_instr = (unsigned char*) PyBytes_AS_STRING(co->co_code);
1178 /* An explanation is in order for the next line.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001179
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180 f->f_lasti now refers to the index of the last instruction
1181 executed. You might think this was obvious from the name, but
1182 this wasn't always true before 2.3! PyFrame_New now sets
1183 f->f_lasti to -1 (i.e. the index *before* the first instruction)
1184 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
1185 does work. Promise.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001186
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 When the PREDICT() macros are enabled, some opcode pairs follow in
1188 direct succession without updating f->f_lasti. A successful
1189 prediction effectively links the two codes together as if they
1190 were a single new opcode; accordingly,f->f_lasti will point to
1191 the first code in the pair (for instance, GET_ITER followed by
1192 FOR_ITER is effectively a single opcode and f->f_lasti will point
1193 at to the beginning of the combined pair.)
1194 */
1195 next_instr = first_instr + f->f_lasti + 1;
1196 stack_pointer = f->f_stacktop;
1197 assert(stack_pointer != NULL);
1198 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001199
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001200 if (co->co_flags & CO_GENERATOR && !throwflag) {
1201 if (f->f_exc_type != NULL && f->f_exc_type != Py_None) {
1202 /* We were in an except handler when we left,
1203 restore the exception state which was put aside
1204 (see YIELD_VALUE). */
1205 SWAP_EXC_STATE();
1206 }
1207 else {
1208 SAVE_EXC_STATE();
1209 }
1210 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001211
Tim Peters5ca576e2001-06-18 22:08:13 +00001212#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001213 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001214#endif
Neal Norwitz5f5153e2005-10-21 04:28:38 +00001215#if defined(Py_DEBUG) || defined(LLTRACE)
Victor Stinner4a3733d2010-08-17 00:39:57 +00001216 {
1217 PyObject *error_type, *error_value, *error_traceback;
1218 PyErr_Fetch(&error_type, &error_value, &error_traceback);
1219 filename = _PyUnicode_AsString(co->co_filename);
1220 PyErr_Restore(error_type, error_value, error_traceback);
1221 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001222#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001223
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 why = WHY_NOT;
1225 err = 0;
1226 x = Py_None; /* Not a reference, just anything non-NULL */
1227 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00001228
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001229 if (throwflag) { /* support for generator.throw() */
1230 why = WHY_EXCEPTION;
1231 goto on_error;
1232 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001233
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001235#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 if (inst1 == 0) {
1237 /* Almost surely, the opcode executed a break
1238 or a continue, preventing inst1 from being set
1239 on the way out of the loop.
1240 */
1241 READ_TIMESTAMP(inst1);
1242 loop1 = inst1;
1243 }
1244 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
1245 intr0, intr1);
1246 ticked = 0;
1247 inst1 = 0;
1248 intr0 = 0;
1249 intr1 = 0;
1250 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001251#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001252 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1253 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001254
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 /* Do periodic things. Doing this every time through
1256 the loop would add too much overhead, so we do it
1257 only every Nth instruction. We also do it if
1258 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1259 event needs attention (e.g. a signal handler or
1260 async I/O handler); see Py_AddPendingCall() and
1261 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001262
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001263 if (_Py_atomic_load_relaxed(&eval_breaker)) {
1264 if (*next_instr == SETUP_FINALLY) {
1265 /* Make the last opcode before
1266 a try: finally: block uninterruptable. */
1267 goto fast_next_opcode;
1268 }
1269 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001270#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 ticked = 1;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001272#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001273 if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) {
1274 if (Py_MakePendingCalls() < 0) {
1275 why = WHY_EXCEPTION;
1276 goto on_error;
1277 }
1278 }
1279 if (_Py_atomic_load_relaxed(&gil_drop_request)) {
Guido van Rossume59214e1994-08-30 08:01:59 +00001280#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 /* Give another thread a chance */
1282 if (PyThreadState_Swap(NULL) != tstate)
1283 Py_FatalError("ceval: tstate mix-up");
1284 drop_gil(tstate);
1285
1286 /* Other threads may run now */
1287
1288 take_gil(tstate);
1289 if (PyThreadState_Swap(tstate) != NULL)
1290 Py_FatalError("ceval: orphan tstate");
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001291#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 }
1293 /* Check for asynchronous exceptions. */
1294 if (tstate->async_exc != NULL) {
1295 x = tstate->async_exc;
1296 tstate->async_exc = NULL;
1297 UNSIGNAL_ASYNC_EXC();
1298 PyErr_SetNone(x);
1299 Py_DECREF(x);
1300 why = WHY_EXCEPTION;
1301 goto on_error;
1302 }
1303 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001304
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001305 fast_next_opcode:
1306 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001307
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 if (_Py_TracingPossible &&
1311 tstate->c_tracefunc != NULL && !tstate->tracing) {
1312 /* see maybe_call_line_trace
1313 for expository comments */
1314 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001315
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001316 err = maybe_call_line_trace(tstate->c_tracefunc,
1317 tstate->c_traceobj,
1318 f, &instr_lb, &instr_ub,
1319 &instr_prev);
1320 /* Reload possibly changed frame fields */
1321 JUMPTO(f->f_lasti);
1322 if (f->f_stacktop != NULL) {
1323 stack_pointer = f->f_stacktop;
1324 f->f_stacktop = NULL;
1325 }
1326 if (err) {
1327 /* trace function raised an exception */
1328 goto on_error;
1329 }
1330 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001331
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001332 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 opcode = NEXTOP();
1335 oparg = 0; /* allows oparg to be stored in a register because
1336 it doesn't have to be remembered across a full loop */
1337 if (HAS_ARG(opcode))
1338 oparg = NEXTARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001339 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001340#ifdef DYNAMIC_EXECUTION_PROFILE
1341#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 dxpairs[lastopcode][opcode]++;
1343 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001344#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001346#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001347
Guido van Rossum96a42c81992-01-12 02:29:51 +00001348#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001349 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001350
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001351 if (lltrace) {
1352 if (HAS_ARG(opcode)) {
1353 printf("%d: %d, %d\n",
1354 f->f_lasti, opcode, oparg);
1355 }
1356 else {
1357 printf("%d: %d\n",
1358 f->f_lasti, opcode);
1359 }
1360 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001361#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 /* Main switch on opcode */
1364 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001368 /* BEWARE!
1369 It is essential that any operation that fails sets either
1370 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1371 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 TARGET(NOP)
1376 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001377
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 TARGET(LOAD_FAST)
1379 x = GETLOCAL(oparg);
1380 if (x != NULL) {
1381 Py_INCREF(x);
1382 PUSH(x);
1383 FAST_DISPATCH();
1384 }
1385 format_exc_check_arg(PyExc_UnboundLocalError,
1386 UNBOUNDLOCAL_ERROR_MSG,
1387 PyTuple_GetItem(co->co_varnames, oparg));
1388 break;
Neil Schemenauer63543862002-02-17 19:10:14 +00001389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 TARGET(LOAD_CONST)
1391 x = GETITEM(consts, oparg);
1392 Py_INCREF(x);
1393 PUSH(x);
1394 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001396 PREDICTED_WITH_ARG(STORE_FAST);
1397 TARGET(STORE_FAST)
1398 v = POP();
1399 SETLOCAL(oparg, v);
1400 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001402 TARGET(POP_TOP)
1403 v = POP();
1404 Py_DECREF(v);
1405 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001407 TARGET(ROT_TWO)
1408 v = TOP();
1409 w = SECOND();
1410 SET_TOP(w);
1411 SET_SECOND(v);
1412 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001414 TARGET(ROT_THREE)
1415 v = TOP();
1416 w = SECOND();
1417 x = THIRD();
1418 SET_TOP(w);
1419 SET_SECOND(x);
1420 SET_THIRD(v);
1421 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001422
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001423 TARGET(DUP_TOP)
1424 v = TOP();
1425 Py_INCREF(v);
1426 PUSH(v);
1427 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001428
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001429 TARGET(DUP_TOP_TWO)
1430 x = TOP();
1431 Py_INCREF(x);
1432 w = SECOND();
1433 Py_INCREF(w);
1434 STACKADJ(2);
1435 SET_TOP(x);
1436 SET_SECOND(w);
1437 FAST_DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001439 TARGET(UNARY_POSITIVE)
1440 v = TOP();
1441 x = PyNumber_Positive(v);
1442 Py_DECREF(v);
1443 SET_TOP(x);
1444 if (x != NULL) DISPATCH();
1445 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001447 TARGET(UNARY_NEGATIVE)
1448 v = TOP();
1449 x = PyNumber_Negative(v);
1450 Py_DECREF(v);
1451 SET_TOP(x);
1452 if (x != NULL) DISPATCH();
1453 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001455 TARGET(UNARY_NOT)
1456 v = TOP();
1457 err = PyObject_IsTrue(v);
1458 Py_DECREF(v);
1459 if (err == 0) {
1460 Py_INCREF(Py_True);
1461 SET_TOP(Py_True);
1462 DISPATCH();
1463 }
1464 else if (err > 0) {
1465 Py_INCREF(Py_False);
1466 SET_TOP(Py_False);
1467 err = 0;
1468 DISPATCH();
1469 }
1470 STACKADJ(-1);
1471 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001472
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001473 TARGET(UNARY_INVERT)
1474 v = TOP();
1475 x = PyNumber_Invert(v);
1476 Py_DECREF(v);
1477 SET_TOP(x);
1478 if (x != NULL) DISPATCH();
1479 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001481 TARGET(BINARY_POWER)
1482 w = POP();
1483 v = TOP();
1484 x = PyNumber_Power(v, w, Py_None);
1485 Py_DECREF(v);
1486 Py_DECREF(w);
1487 SET_TOP(x);
1488 if (x != NULL) DISPATCH();
1489 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001490
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001491 TARGET(BINARY_MULTIPLY)
1492 w = POP();
1493 v = TOP();
1494 x = PyNumber_Multiply(v, w);
1495 Py_DECREF(v);
1496 Py_DECREF(w);
1497 SET_TOP(x);
1498 if (x != NULL) DISPATCH();
1499 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001500
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001501 TARGET(BINARY_TRUE_DIVIDE)
1502 w = POP();
1503 v = TOP();
1504 x = PyNumber_TrueDivide(v, w);
1505 Py_DECREF(v);
1506 Py_DECREF(w);
1507 SET_TOP(x);
1508 if (x != NULL) DISPATCH();
1509 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001510
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001511 TARGET(BINARY_FLOOR_DIVIDE)
1512 w = POP();
1513 v = TOP();
1514 x = PyNumber_FloorDivide(v, w);
1515 Py_DECREF(v);
1516 Py_DECREF(w);
1517 SET_TOP(x);
1518 if (x != NULL) DISPATCH();
1519 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001520
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001521 TARGET(BINARY_MODULO)
1522 w = POP();
1523 v = TOP();
1524 if (PyUnicode_CheckExact(v))
1525 x = PyUnicode_Format(v, w);
1526 else
1527 x = PyNumber_Remainder(v, w);
1528 Py_DECREF(v);
1529 Py_DECREF(w);
1530 SET_TOP(x);
1531 if (x != NULL) DISPATCH();
1532 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001533
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001534 TARGET(BINARY_ADD)
1535 w = POP();
1536 v = TOP();
1537 if (PyUnicode_CheckExact(v) &&
1538 PyUnicode_CheckExact(w)) {
1539 x = unicode_concatenate(v, w, f, next_instr);
1540 /* unicode_concatenate consumed the ref to v */
1541 goto skip_decref_vx;
1542 }
1543 else {
1544 x = PyNumber_Add(v, w);
1545 }
1546 Py_DECREF(v);
1547 skip_decref_vx:
1548 Py_DECREF(w);
1549 SET_TOP(x);
1550 if (x != NULL) DISPATCH();
1551 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001553 TARGET(BINARY_SUBTRACT)
1554 w = POP();
1555 v = TOP();
1556 x = PyNumber_Subtract(v, w);
1557 Py_DECREF(v);
1558 Py_DECREF(w);
1559 SET_TOP(x);
1560 if (x != NULL) DISPATCH();
1561 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 TARGET(BINARY_SUBSCR)
1564 w = POP();
1565 v = TOP();
1566 x = PyObject_GetItem(v, w);
1567 Py_DECREF(v);
1568 Py_DECREF(w);
1569 SET_TOP(x);
1570 if (x != NULL) DISPATCH();
1571 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001573 TARGET(BINARY_LSHIFT)
1574 w = POP();
1575 v = TOP();
1576 x = PyNumber_Lshift(v, w);
1577 Py_DECREF(v);
1578 Py_DECREF(w);
1579 SET_TOP(x);
1580 if (x != NULL) DISPATCH();
1581 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001582
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583 TARGET(BINARY_RSHIFT)
1584 w = POP();
1585 v = TOP();
1586 x = PyNumber_Rshift(v, w);
1587 Py_DECREF(v);
1588 Py_DECREF(w);
1589 SET_TOP(x);
1590 if (x != NULL) DISPATCH();
1591 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001592
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 TARGET(BINARY_AND)
1594 w = POP();
1595 v = TOP();
1596 x = PyNumber_And(v, w);
1597 Py_DECREF(v);
1598 Py_DECREF(w);
1599 SET_TOP(x);
1600 if (x != NULL) DISPATCH();
1601 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 TARGET(BINARY_XOR)
1604 w = POP();
1605 v = TOP();
1606 x = PyNumber_Xor(v, w);
1607 Py_DECREF(v);
1608 Py_DECREF(w);
1609 SET_TOP(x);
1610 if (x != NULL) DISPATCH();
1611 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001612
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613 TARGET(BINARY_OR)
1614 w = POP();
1615 v = TOP();
1616 x = PyNumber_Or(v, w);
1617 Py_DECREF(v);
1618 Py_DECREF(w);
1619 SET_TOP(x);
1620 if (x != NULL) DISPATCH();
1621 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001622
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001623 TARGET(LIST_APPEND)
1624 w = POP();
1625 v = PEEK(oparg);
1626 err = PyList_Append(v, w);
1627 Py_DECREF(w);
1628 if (err == 0) {
1629 PREDICT(JUMP_ABSOLUTE);
1630 DISPATCH();
1631 }
1632 break;
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001634 TARGET(SET_ADD)
1635 w = POP();
1636 v = stack_pointer[-oparg];
1637 err = PySet_Add(v, w);
1638 Py_DECREF(w);
1639 if (err == 0) {
1640 PREDICT(JUMP_ABSOLUTE);
1641 DISPATCH();
1642 }
1643 break;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001645 TARGET(INPLACE_POWER)
1646 w = POP();
1647 v = TOP();
1648 x = PyNumber_InPlacePower(v, w, Py_None);
1649 Py_DECREF(v);
1650 Py_DECREF(w);
1651 SET_TOP(x);
1652 if (x != NULL) DISPATCH();
1653 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001655 TARGET(INPLACE_MULTIPLY)
1656 w = POP();
1657 v = TOP();
1658 x = PyNumber_InPlaceMultiply(v, w);
1659 Py_DECREF(v);
1660 Py_DECREF(w);
1661 SET_TOP(x);
1662 if (x != NULL) DISPATCH();
1663 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001664
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001665 TARGET(INPLACE_TRUE_DIVIDE)
1666 w = POP();
1667 v = TOP();
1668 x = PyNumber_InPlaceTrueDivide(v, w);
1669 Py_DECREF(v);
1670 Py_DECREF(w);
1671 SET_TOP(x);
1672 if (x != NULL) DISPATCH();
1673 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001674
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001675 TARGET(INPLACE_FLOOR_DIVIDE)
1676 w = POP();
1677 v = TOP();
1678 x = PyNumber_InPlaceFloorDivide(v, w);
1679 Py_DECREF(v);
1680 Py_DECREF(w);
1681 SET_TOP(x);
1682 if (x != NULL) DISPATCH();
1683 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001684
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001685 TARGET(INPLACE_MODULO)
1686 w = POP();
1687 v = TOP();
1688 x = PyNumber_InPlaceRemainder(v, w);
1689 Py_DECREF(v);
1690 Py_DECREF(w);
1691 SET_TOP(x);
1692 if (x != NULL) DISPATCH();
1693 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001694
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001695 TARGET(INPLACE_ADD)
1696 w = POP();
1697 v = TOP();
1698 if (PyUnicode_CheckExact(v) &&
1699 PyUnicode_CheckExact(w)) {
1700 x = unicode_concatenate(v, w, f, next_instr);
1701 /* unicode_concatenate consumed the ref to v */
1702 goto skip_decref_v;
1703 }
1704 else {
1705 x = PyNumber_InPlaceAdd(v, w);
1706 }
1707 Py_DECREF(v);
1708 skip_decref_v:
1709 Py_DECREF(w);
1710 SET_TOP(x);
1711 if (x != NULL) DISPATCH();
1712 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001713
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001714 TARGET(INPLACE_SUBTRACT)
1715 w = POP();
1716 v = TOP();
1717 x = PyNumber_InPlaceSubtract(v, w);
1718 Py_DECREF(v);
1719 Py_DECREF(w);
1720 SET_TOP(x);
1721 if (x != NULL) DISPATCH();
1722 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001723
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001724 TARGET(INPLACE_LSHIFT)
1725 w = POP();
1726 v = TOP();
1727 x = PyNumber_InPlaceLshift(v, w);
1728 Py_DECREF(v);
1729 Py_DECREF(w);
1730 SET_TOP(x);
1731 if (x != NULL) DISPATCH();
1732 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001733
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001734 TARGET(INPLACE_RSHIFT)
1735 w = POP();
1736 v = TOP();
1737 x = PyNumber_InPlaceRshift(v, w);
1738 Py_DECREF(v);
1739 Py_DECREF(w);
1740 SET_TOP(x);
1741 if (x != NULL) DISPATCH();
1742 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001743
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001744 TARGET(INPLACE_AND)
1745 w = POP();
1746 v = TOP();
1747 x = PyNumber_InPlaceAnd(v, w);
1748 Py_DECREF(v);
1749 Py_DECREF(w);
1750 SET_TOP(x);
1751 if (x != NULL) DISPATCH();
1752 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001753
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001754 TARGET(INPLACE_XOR)
1755 w = POP();
1756 v = TOP();
1757 x = PyNumber_InPlaceXor(v, w);
1758 Py_DECREF(v);
1759 Py_DECREF(w);
1760 SET_TOP(x);
1761 if (x != NULL) DISPATCH();
1762 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001763
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001764 TARGET(INPLACE_OR)
1765 w = POP();
1766 v = TOP();
1767 x = PyNumber_InPlaceOr(v, w);
1768 Py_DECREF(v);
1769 Py_DECREF(w);
1770 SET_TOP(x);
1771 if (x != NULL) DISPATCH();
1772 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001773
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001774 TARGET(STORE_SUBSCR)
1775 w = TOP();
1776 v = SECOND();
1777 u = THIRD();
1778 STACKADJ(-3);
1779 /* v[w] = u */
1780 err = PyObject_SetItem(v, w, u);
1781 Py_DECREF(u);
1782 Py_DECREF(v);
1783 Py_DECREF(w);
1784 if (err == 0) DISPATCH();
1785 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001786
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001787 TARGET(DELETE_SUBSCR)
1788 w = TOP();
1789 v = SECOND();
1790 STACKADJ(-2);
1791 /* del v[w] */
1792 err = PyObject_DelItem(v, w);
1793 Py_DECREF(v);
1794 Py_DECREF(w);
1795 if (err == 0) DISPATCH();
1796 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001797
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001798 TARGET(PRINT_EXPR)
1799 v = POP();
1800 w = PySys_GetObject("displayhook");
1801 if (w == NULL) {
1802 PyErr_SetString(PyExc_RuntimeError,
1803 "lost sys.displayhook");
1804 err = -1;
1805 x = NULL;
1806 }
1807 if (err == 0) {
1808 x = PyTuple_Pack(1, v);
1809 if (x == NULL)
1810 err = -1;
1811 }
1812 if (err == 0) {
1813 w = PyEval_CallObject(w, x);
1814 Py_XDECREF(w);
1815 if (w == NULL)
1816 err = -1;
1817 }
1818 Py_DECREF(v);
1819 Py_XDECREF(x);
1820 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001821
Thomas Wouters434d0822000-08-24 20:11:32 +00001822#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001823 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001824#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001825 TARGET(RAISE_VARARGS)
1826 v = w = NULL;
1827 switch (oparg) {
1828 case 2:
1829 v = POP(); /* cause */
1830 case 1:
1831 w = POP(); /* exc */
1832 case 0: /* Fallthrough */
1833 why = do_raise(w, v);
1834 break;
1835 default:
1836 PyErr_SetString(PyExc_SystemError,
1837 "bad RAISE_VARARGS oparg");
1838 why = WHY_EXCEPTION;
1839 break;
1840 }
1841 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001842
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001843 TARGET(STORE_LOCALS)
1844 x = POP();
1845 v = f->f_locals;
1846 Py_XDECREF(v);
1847 f->f_locals = x;
1848 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001849
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001850 TARGET(RETURN_VALUE)
1851 retval = POP();
1852 why = WHY_RETURN;
1853 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001854
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001855 TARGET(YIELD_VALUE)
1856 retval = POP();
1857 f->f_stacktop = stack_pointer;
1858 why = WHY_YIELD;
1859 /* Put aside the current exception state and restore
1860 that of the calling frame. This only serves when
1861 "yield" is used inside an except handler. */
1862 SWAP_EXC_STATE();
1863 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001864
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001865 TARGET(POP_EXCEPT)
1866 {
1867 PyTryBlock *b = PyFrame_BlockPop(f);
1868 if (b->b_type != EXCEPT_HANDLER) {
1869 PyErr_SetString(PyExc_SystemError,
1870 "popped block is not an except handler");
1871 why = WHY_EXCEPTION;
1872 break;
1873 }
1874 UNWIND_EXCEPT_HANDLER(b);
1875 }
1876 DISPATCH();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001877
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001878 TARGET(POP_BLOCK)
1879 {
1880 PyTryBlock *b = PyFrame_BlockPop(f);
1881 UNWIND_BLOCK(b);
1882 }
1883 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001884
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001885 PREDICTED(END_FINALLY);
1886 TARGET(END_FINALLY)
1887 v = POP();
1888 if (PyLong_Check(v)) {
1889 why = (enum why_code) PyLong_AS_LONG(v);
1890 assert(why != WHY_YIELD);
1891 if (why == WHY_RETURN ||
1892 why == WHY_CONTINUE)
1893 retval = POP();
1894 if (why == WHY_SILENCED) {
1895 /* An exception was silenced by 'with', we must
1896 manually unwind the EXCEPT_HANDLER block which was
1897 created when the exception was caught, otherwise
1898 the stack will be in an inconsistent state. */
1899 PyTryBlock *b = PyFrame_BlockPop(f);
1900 assert(b->b_type == EXCEPT_HANDLER);
1901 UNWIND_EXCEPT_HANDLER(b);
1902 why = WHY_NOT;
1903 }
1904 }
1905 else if (PyExceptionClass_Check(v)) {
1906 w = POP();
1907 u = POP();
1908 PyErr_Restore(v, w, u);
1909 why = WHY_RERAISE;
1910 break;
1911 }
1912 else if (v != Py_None) {
1913 PyErr_SetString(PyExc_SystemError,
1914 "'finally' pops bad exception");
1915 why = WHY_EXCEPTION;
1916 }
1917 Py_DECREF(v);
1918 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001919
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001920 TARGET(LOAD_BUILD_CLASS)
1921 x = PyDict_GetItemString(f->f_builtins,
1922 "__build_class__");
1923 if (x == NULL) {
1924 PyErr_SetString(PyExc_ImportError,
1925 "__build_class__ not found");
1926 break;
1927 }
1928 Py_INCREF(x);
1929 PUSH(x);
1930 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001931
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001932 TARGET(STORE_NAME)
1933 w = GETITEM(names, oparg);
1934 v = POP();
1935 if ((x = f->f_locals) != NULL) {
1936 if (PyDict_CheckExact(x))
1937 err = PyDict_SetItem(x, w, v);
1938 else
1939 err = PyObject_SetItem(x, w, v);
1940 Py_DECREF(v);
1941 if (err == 0) DISPATCH();
1942 break;
1943 }
1944 PyErr_Format(PyExc_SystemError,
1945 "no locals found when storing %R", w);
1946 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001947
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001948 TARGET(DELETE_NAME)
1949 w = GETITEM(names, oparg);
1950 if ((x = f->f_locals) != NULL) {
1951 if ((err = PyObject_DelItem(x, w)) != 0)
1952 format_exc_check_arg(PyExc_NameError,
1953 NAME_ERROR_MSG,
1954 w);
1955 break;
1956 }
1957 PyErr_Format(PyExc_SystemError,
1958 "no locals when deleting %R", w);
1959 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001961 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
1962 TARGET(UNPACK_SEQUENCE)
1963 v = POP();
1964 if (PyTuple_CheckExact(v) &&
1965 PyTuple_GET_SIZE(v) == oparg) {
1966 PyObject **items = \
1967 ((PyTupleObject *)v)->ob_item;
1968 while (oparg--) {
1969 w = items[oparg];
1970 Py_INCREF(w);
1971 PUSH(w);
1972 }
1973 Py_DECREF(v);
1974 DISPATCH();
1975 } else if (PyList_CheckExact(v) &&
1976 PyList_GET_SIZE(v) == oparg) {
1977 PyObject **items = \
1978 ((PyListObject *)v)->ob_item;
1979 while (oparg--) {
1980 w = items[oparg];
1981 Py_INCREF(w);
1982 PUSH(w);
1983 }
1984 } else if (unpack_iterable(v, oparg, -1,
1985 stack_pointer + oparg)) {
1986 STACKADJ(oparg);
1987 } else {
1988 /* unpack_iterable() raised an exception */
1989 why = WHY_EXCEPTION;
1990 }
1991 Py_DECREF(v);
1992 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001993
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001994 TARGET(UNPACK_EX)
1995 {
1996 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
1997 v = POP();
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00001998
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001999 if (unpack_iterable(v, oparg & 0xFF, oparg >> 8,
2000 stack_pointer + totalargs)) {
2001 stack_pointer += totalargs;
2002 } else {
2003 why = WHY_EXCEPTION;
2004 }
2005 Py_DECREF(v);
2006 break;
2007 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002008
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002009 TARGET(STORE_ATTR)
2010 w = GETITEM(names, oparg);
2011 v = TOP();
2012 u = SECOND();
2013 STACKADJ(-2);
2014 err = PyObject_SetAttr(v, w, u); /* v.w = u */
2015 Py_DECREF(v);
2016 Py_DECREF(u);
2017 if (err == 0) DISPATCH();
2018 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002019
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002020 TARGET(DELETE_ATTR)
2021 w = GETITEM(names, oparg);
2022 v = POP();
2023 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
2024 /* del v.w */
2025 Py_DECREF(v);
2026 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002027
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002028 TARGET(STORE_GLOBAL)
2029 w = GETITEM(names, oparg);
2030 v = POP();
2031 err = PyDict_SetItem(f->f_globals, w, v);
2032 Py_DECREF(v);
2033 if (err == 0) DISPATCH();
2034 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002035
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002036 TARGET(DELETE_GLOBAL)
2037 w = GETITEM(names, oparg);
2038 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
2039 format_exc_check_arg(
2040 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
2041 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002042
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002043 TARGET(LOAD_NAME)
2044 w = GETITEM(names, oparg);
2045 if ((v = f->f_locals) == NULL) {
2046 PyErr_Format(PyExc_SystemError,
2047 "no locals when loading %R", w);
2048 why = WHY_EXCEPTION;
2049 break;
2050 }
2051 if (PyDict_CheckExact(v)) {
2052 x = PyDict_GetItem(v, w);
2053 Py_XINCREF(x);
2054 }
2055 else {
2056 x = PyObject_GetItem(v, w);
2057 if (x == NULL && PyErr_Occurred()) {
2058 if (!PyErr_ExceptionMatches(
2059 PyExc_KeyError))
2060 break;
2061 PyErr_Clear();
2062 }
2063 }
2064 if (x == NULL) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002065 x = PyDict_GetItem(f->f_globals, w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002066 if (x == NULL) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002067 x = PyDict_GetItem(f->f_builtins, w);
2068 if (x == NULL) {
2069 format_exc_check_arg(
2070 PyExc_NameError,
2071 NAME_ERROR_MSG, w);
2072 break;
2073 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002074 }
2075 Py_INCREF(x);
2076 }
2077 PUSH(x);
2078 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002079
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002080 TARGET(LOAD_GLOBAL)
2081 w = GETITEM(names, oparg);
2082 if (PyUnicode_CheckExact(w)) {
2083 /* Inline the PyDict_GetItem() calls.
2084 WARNING: this is an extreme speed hack.
2085 Do not try this at home. */
2086 long hash = ((PyUnicodeObject *)w)->hash;
2087 if (hash != -1) {
2088 PyDictObject *d;
2089 PyDictEntry *e;
2090 d = (PyDictObject *)(f->f_globals);
2091 e = d->ma_lookup(d, w, hash);
2092 if (e == NULL) {
2093 x = NULL;
2094 break;
2095 }
2096 x = e->me_value;
2097 if (x != NULL) {
2098 Py_INCREF(x);
2099 PUSH(x);
2100 DISPATCH();
2101 }
2102 d = (PyDictObject *)(f->f_builtins);
2103 e = d->ma_lookup(d, w, hash);
2104 if (e == NULL) {
2105 x = NULL;
2106 break;
2107 }
2108 x = e->me_value;
2109 if (x != NULL) {
2110 Py_INCREF(x);
2111 PUSH(x);
2112 DISPATCH();
2113 }
2114 goto load_global_error;
2115 }
2116 }
2117 /* This is the un-inlined version of the code above */
2118 x = PyDict_GetItem(f->f_globals, w);
2119 if (x == NULL) {
2120 x = PyDict_GetItem(f->f_builtins, w);
2121 if (x == NULL) {
2122 load_global_error:
2123 format_exc_check_arg(
2124 PyExc_NameError,
2125 GLOBAL_NAME_ERROR_MSG, w);
2126 break;
2127 }
2128 }
2129 Py_INCREF(x);
2130 PUSH(x);
2131 DISPATCH();
Guido van Rossum681d79a1995-07-18 14:51:37 +00002132
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002133 TARGET(DELETE_FAST)
2134 x = GETLOCAL(oparg);
2135 if (x != NULL) {
2136 SETLOCAL(oparg, NULL);
2137 DISPATCH();
2138 }
2139 format_exc_check_arg(
2140 PyExc_UnboundLocalError,
2141 UNBOUNDLOCAL_ERROR_MSG,
2142 PyTuple_GetItem(co->co_varnames, oparg)
2143 );
2144 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002146 TARGET(LOAD_CLOSURE)
2147 x = freevars[oparg];
2148 Py_INCREF(x);
2149 PUSH(x);
2150 if (x != NULL) DISPATCH();
2151 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002152
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002153 TARGET(LOAD_DEREF)
2154 x = freevars[oparg];
2155 w = PyCell_Get(x);
2156 if (w != NULL) {
2157 PUSH(w);
2158 DISPATCH();
2159 }
2160 err = -1;
2161 /* Don't stomp existing exception */
2162 if (PyErr_Occurred())
2163 break;
2164 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
2165 v = PyTuple_GET_ITEM(co->co_cellvars,
Stefan Krahb7e10102010-06-23 18:42:39 +00002166 oparg);
2167 format_exc_check_arg(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002168 PyExc_UnboundLocalError,
2169 UNBOUNDLOCAL_ERROR_MSG,
2170 v);
2171 } else {
2172 v = PyTuple_GET_ITEM(co->co_freevars, oparg -
2173 PyTuple_GET_SIZE(co->co_cellvars));
2174 format_exc_check_arg(PyExc_NameError,
2175 UNBOUNDFREE_ERROR_MSG, v);
2176 }
2177 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002178
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002179 TARGET(STORE_DEREF)
2180 w = POP();
2181 x = freevars[oparg];
2182 PyCell_Set(x, w);
2183 Py_DECREF(w);
2184 DISPATCH();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002185
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002186 TARGET(BUILD_TUPLE)
2187 x = PyTuple_New(oparg);
2188 if (x != NULL) {
2189 for (; --oparg >= 0;) {
2190 w = POP();
2191 PyTuple_SET_ITEM(x, oparg, w);
2192 }
2193 PUSH(x);
2194 DISPATCH();
2195 }
2196 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002197
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002198 TARGET(BUILD_LIST)
2199 x = PyList_New(oparg);
2200 if (x != NULL) {
2201 for (; --oparg >= 0;) {
2202 w = POP();
2203 PyList_SET_ITEM(x, oparg, w);
2204 }
2205 PUSH(x);
2206 DISPATCH();
2207 }
2208 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002209
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002210 TARGET(BUILD_SET)
2211 x = PySet_New(NULL);
2212 if (x != NULL) {
2213 for (; --oparg >= 0;) {
2214 w = POP();
2215 if (err == 0)
2216 err = PySet_Add(x, w);
2217 Py_DECREF(w);
2218 }
2219 if (err != 0) {
2220 Py_DECREF(x);
2221 break;
2222 }
2223 PUSH(x);
2224 DISPATCH();
2225 }
2226 break;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002227
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002228 TARGET(BUILD_MAP)
2229 x = _PyDict_NewPresized((Py_ssize_t)oparg);
2230 PUSH(x);
2231 if (x != NULL) DISPATCH();
2232 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002233
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002234 TARGET(STORE_MAP)
2235 w = TOP(); /* key */
2236 u = SECOND(); /* value */
2237 v = THIRD(); /* dict */
2238 STACKADJ(-2);
2239 assert (PyDict_CheckExact(v));
2240 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2241 Py_DECREF(u);
2242 Py_DECREF(w);
2243 if (err == 0) DISPATCH();
2244 break;
Christian Heimes99170a52007-12-19 02:07:34 +00002245
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002246 TARGET(MAP_ADD)
2247 w = TOP(); /* key */
2248 u = SECOND(); /* value */
2249 STACKADJ(-2);
2250 v = stack_pointer[-oparg]; /* dict */
2251 assert (PyDict_CheckExact(v));
2252 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2253 Py_DECREF(u);
2254 Py_DECREF(w);
2255 if (err == 0) {
2256 PREDICT(JUMP_ABSOLUTE);
2257 DISPATCH();
2258 }
2259 break;
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002261 TARGET(LOAD_ATTR)
2262 w = GETITEM(names, oparg);
2263 v = TOP();
2264 x = PyObject_GetAttr(v, w);
2265 Py_DECREF(v);
2266 SET_TOP(x);
2267 if (x != NULL) DISPATCH();
2268 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002270 TARGET(COMPARE_OP)
2271 w = POP();
2272 v = TOP();
2273 x = cmp_outcome(oparg, v, w);
2274 Py_DECREF(v);
2275 Py_DECREF(w);
2276 SET_TOP(x);
2277 if (x == NULL) break;
2278 PREDICT(POP_JUMP_IF_FALSE);
2279 PREDICT(POP_JUMP_IF_TRUE);
2280 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002282 TARGET(IMPORT_NAME)
2283 w = GETITEM(names, oparg);
2284 x = PyDict_GetItemString(f->f_builtins, "__import__");
2285 if (x == NULL) {
2286 PyErr_SetString(PyExc_ImportError,
2287 "__import__ not found");
2288 break;
2289 }
2290 Py_INCREF(x);
2291 v = POP();
2292 u = TOP();
2293 if (PyLong_AsLong(u) != -1 || PyErr_Occurred())
2294 w = PyTuple_Pack(5,
2295 w,
2296 f->f_globals,
2297 f->f_locals == NULL ?
2298 Py_None : f->f_locals,
2299 v,
2300 u);
2301 else
2302 w = PyTuple_Pack(4,
2303 w,
2304 f->f_globals,
2305 f->f_locals == NULL ?
2306 Py_None : f->f_locals,
2307 v);
2308 Py_DECREF(v);
2309 Py_DECREF(u);
2310 if (w == NULL) {
2311 u = POP();
2312 Py_DECREF(x);
2313 x = NULL;
2314 break;
2315 }
2316 READ_TIMESTAMP(intr0);
2317 v = x;
2318 x = PyEval_CallObject(v, w);
2319 Py_DECREF(v);
2320 READ_TIMESTAMP(intr1);
2321 Py_DECREF(w);
2322 SET_TOP(x);
2323 if (x != NULL) DISPATCH();
2324 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002326 TARGET(IMPORT_STAR)
2327 v = POP();
2328 PyFrame_FastToLocals(f);
2329 if ((x = f->f_locals) == NULL) {
2330 PyErr_SetString(PyExc_SystemError,
2331 "no locals found during 'import *'");
2332 break;
2333 }
2334 READ_TIMESTAMP(intr0);
2335 err = import_all_from(x, v);
2336 READ_TIMESTAMP(intr1);
2337 PyFrame_LocalsToFast(f, 0);
2338 Py_DECREF(v);
2339 if (err == 0) DISPATCH();
2340 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002341
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002342 TARGET(IMPORT_FROM)
2343 w = GETITEM(names, oparg);
2344 v = TOP();
2345 READ_TIMESTAMP(intr0);
2346 x = import_from(v, w);
2347 READ_TIMESTAMP(intr1);
2348 PUSH(x);
2349 if (x != NULL) DISPATCH();
2350 break;
Thomas Wouters52152252000-08-17 22:55:00 +00002351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002352 TARGET(JUMP_FORWARD)
2353 JUMPBY(oparg);
2354 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002356 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
2357 TARGET(POP_JUMP_IF_FALSE)
2358 w = POP();
2359 if (w == Py_True) {
2360 Py_DECREF(w);
2361 FAST_DISPATCH();
2362 }
2363 if (w == Py_False) {
2364 Py_DECREF(w);
2365 JUMPTO(oparg);
2366 FAST_DISPATCH();
2367 }
2368 err = PyObject_IsTrue(w);
2369 Py_DECREF(w);
2370 if (err > 0)
2371 err = 0;
2372 else if (err == 0)
2373 JUMPTO(oparg);
2374 else
2375 break;
2376 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002377
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002378 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
2379 TARGET(POP_JUMP_IF_TRUE)
2380 w = POP();
2381 if (w == Py_False) {
2382 Py_DECREF(w);
2383 FAST_DISPATCH();
2384 }
2385 if (w == Py_True) {
2386 Py_DECREF(w);
2387 JUMPTO(oparg);
2388 FAST_DISPATCH();
2389 }
2390 err = PyObject_IsTrue(w);
2391 Py_DECREF(w);
2392 if (err > 0) {
2393 err = 0;
2394 JUMPTO(oparg);
2395 }
2396 else if (err == 0)
2397 ;
2398 else
2399 break;
2400 DISPATCH();
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002402 TARGET(JUMP_IF_FALSE_OR_POP)
2403 w = TOP();
2404 if (w == Py_True) {
2405 STACKADJ(-1);
2406 Py_DECREF(w);
2407 FAST_DISPATCH();
2408 }
2409 if (w == Py_False) {
2410 JUMPTO(oparg);
2411 FAST_DISPATCH();
2412 }
2413 err = PyObject_IsTrue(w);
2414 if (err > 0) {
2415 STACKADJ(-1);
2416 Py_DECREF(w);
2417 err = 0;
2418 }
2419 else if (err == 0)
2420 JUMPTO(oparg);
2421 else
2422 break;
2423 DISPATCH();
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002424
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002425 TARGET(JUMP_IF_TRUE_OR_POP)
2426 w = TOP();
2427 if (w == Py_False) {
2428 STACKADJ(-1);
2429 Py_DECREF(w);
2430 FAST_DISPATCH();
2431 }
2432 if (w == Py_True) {
2433 JUMPTO(oparg);
2434 FAST_DISPATCH();
2435 }
2436 err = PyObject_IsTrue(w);
2437 if (err > 0) {
2438 err = 0;
2439 JUMPTO(oparg);
2440 }
2441 else if (err == 0) {
2442 STACKADJ(-1);
2443 Py_DECREF(w);
2444 }
2445 else
2446 break;
2447 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002449 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
2450 TARGET(JUMP_ABSOLUTE)
2451 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002452#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002453 /* Enabling this path speeds-up all while and for-loops by bypassing
2454 the per-loop checks for signals. By default, this should be turned-off
2455 because it prevents detection of a control-break in tight loops like
2456 "while 1: pass". Compile with this option turned-on when you need
2457 the speed-up and do not need break checking inside tight loops (ones
2458 that contain only instructions ending with FAST_DISPATCH).
2459 */
2460 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002461#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002462 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002463#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002465 TARGET(GET_ITER)
2466 /* before: [obj]; after [getiter(obj)] */
2467 v = TOP();
2468 x = PyObject_GetIter(v);
2469 Py_DECREF(v);
2470 if (x != NULL) {
2471 SET_TOP(x);
2472 PREDICT(FOR_ITER);
2473 DISPATCH();
2474 }
2475 STACKADJ(-1);
2476 break;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002478 PREDICTED_WITH_ARG(FOR_ITER);
2479 TARGET(FOR_ITER)
2480 /* before: [iter]; after: [iter, iter()] *or* [] */
2481 v = TOP();
2482 x = (*v->ob_type->tp_iternext)(v);
2483 if (x != NULL) {
2484 PUSH(x);
2485 PREDICT(STORE_FAST);
2486 PREDICT(UNPACK_SEQUENCE);
2487 DISPATCH();
2488 }
2489 if (PyErr_Occurred()) {
2490 if (!PyErr_ExceptionMatches(
2491 PyExc_StopIteration))
2492 break;
2493 PyErr_Clear();
2494 }
2495 /* iterator ended normally */
2496 x = v = POP();
2497 Py_DECREF(v);
2498 JUMPBY(oparg);
2499 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002500
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002501 TARGET(BREAK_LOOP)
2502 why = WHY_BREAK;
2503 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002505 TARGET(CONTINUE_LOOP)
2506 retval = PyLong_FromLong(oparg);
2507 if (!retval) {
2508 x = NULL;
2509 break;
2510 }
2511 why = WHY_CONTINUE;
2512 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002513
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002514 TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
2515 TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
2516 TARGET(SETUP_FINALLY)
2517 _setup_finally:
2518 /* NOTE: If you add any new block-setup opcodes that
2519 are not try/except/finally handlers, you may need
2520 to update the PyGen_NeedsFinalizing() function.
2521 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002522
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002523 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2524 STACK_LEVEL());
2525 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002527 TARGET(SETUP_WITH)
2528 {
2529 static PyObject *exit, *enter;
2530 w = TOP();
2531 x = special_lookup(w, "__exit__", &exit);
2532 if (!x)
2533 break;
2534 SET_TOP(x);
2535 u = special_lookup(w, "__enter__", &enter);
2536 Py_DECREF(w);
2537 if (!u) {
2538 x = NULL;
2539 break;
2540 }
2541 x = PyObject_CallFunctionObjArgs(u, NULL);
2542 Py_DECREF(u);
2543 if (!x)
2544 break;
2545 /* Setup the finally block before pushing the result
2546 of __enter__ on the stack. */
2547 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2548 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002549
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002550 PUSH(x);
2551 DISPATCH();
2552 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002554 TARGET(WITH_CLEANUP)
2555 {
2556 /* At the top of the stack are 1-3 values indicating
2557 how/why we entered the finally clause:
2558 - TOP = None
2559 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2560 - TOP = WHY_*; no retval below it
2561 - (TOP, SECOND, THIRD) = exc_info()
2562 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2563 Below them is EXIT, the context.__exit__ bound method.
2564 In the last case, we must call
2565 EXIT(TOP, SECOND, THIRD)
2566 otherwise we must call
2567 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002568
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002569 In the first two cases, we remove EXIT from the
2570 stack, leaving the rest in the same order. In the
2571 third case, we shift the bottom 3 values of the
2572 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002573
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002574 In addition, if the stack represents an exception,
2575 *and* the function call returns a 'true' value, we
2576 push WHY_SILENCED onto the stack. END_FINALLY will
2577 then not re-raise the exception. (But non-local
2578 gotos should still be resumed.)
2579 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002580
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002581 PyObject *exit_func;
2582 u = TOP();
2583 if (u == Py_None) {
2584 (void)POP();
2585 exit_func = TOP();
2586 SET_TOP(u);
2587 v = w = Py_None;
2588 }
2589 else if (PyLong_Check(u)) {
2590 (void)POP();
2591 switch(PyLong_AsLong(u)) {
2592 case WHY_RETURN:
2593 case WHY_CONTINUE:
2594 /* Retval in TOP. */
2595 exit_func = SECOND();
2596 SET_SECOND(TOP());
2597 SET_TOP(u);
2598 break;
2599 default:
2600 exit_func = TOP();
2601 SET_TOP(u);
2602 break;
2603 }
2604 u = v = w = Py_None;
2605 }
2606 else {
2607 PyObject *tp, *exc, *tb;
2608 PyTryBlock *block;
2609 v = SECOND();
2610 w = THIRD();
2611 tp = FOURTH();
2612 exc = PEEK(5);
2613 tb = PEEK(6);
2614 exit_func = PEEK(7);
2615 SET_VALUE(7, tb);
2616 SET_VALUE(6, exc);
2617 SET_VALUE(5, tp);
2618 /* UNWIND_EXCEPT_HANDLER will pop this off. */
2619 SET_FOURTH(NULL);
2620 /* We just shifted the stack down, so we have
2621 to tell the except handler block that the
2622 values are lower than it expects. */
2623 block = &f->f_blockstack[f->f_iblock - 1];
2624 assert(block->b_type == EXCEPT_HANDLER);
2625 block->b_level--;
2626 }
2627 /* XXX Not the fastest way to call it... */
2628 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2629 NULL);
2630 Py_DECREF(exit_func);
2631 if (x == NULL)
2632 break; /* Go to error exit */
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002634 if (u != Py_None)
2635 err = PyObject_IsTrue(x);
2636 else
2637 err = 0;
2638 Py_DECREF(x);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002639
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002640 if (err < 0)
2641 break; /* Go to error exit */
2642 else if (err > 0) {
2643 err = 0;
2644 /* There was an exception and a True return */
2645 PUSH(PyLong_FromLong((long) WHY_SILENCED));
2646 }
2647 PREDICT(END_FINALLY);
2648 break;
2649 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002650
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002651 TARGET(CALL_FUNCTION)
2652 {
2653 PyObject **sp;
2654 PCALL(PCALL_ALL);
2655 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002656#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002657 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002658#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002659 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002660#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002661 stack_pointer = sp;
2662 PUSH(x);
2663 if (x != NULL)
2664 DISPATCH();
2665 break;
2666 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002667
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002668 TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw)
2669 TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw)
2670 TARGET(CALL_FUNCTION_VAR_KW)
2671 _call_function_var_kw:
2672 {
2673 int na = oparg & 0xff;
2674 int nk = (oparg>>8) & 0xff;
2675 int flags = (opcode - CALL_FUNCTION) & 3;
2676 int n = na + 2 * nk;
2677 PyObject **pfunc, *func, **sp;
2678 PCALL(PCALL_ALL);
2679 if (flags & CALL_FLAG_VAR)
2680 n++;
2681 if (flags & CALL_FLAG_KW)
2682 n++;
2683 pfunc = stack_pointer - n - 1;
2684 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002685
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002686 if (PyMethod_Check(func)
Stefan Krahb7e10102010-06-23 18:42:39 +00002687 && PyMethod_GET_SELF(func) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002688 PyObject *self = PyMethod_GET_SELF(func);
2689 Py_INCREF(self);
2690 func = PyMethod_GET_FUNCTION(func);
2691 Py_INCREF(func);
2692 Py_DECREF(*pfunc);
2693 *pfunc = self;
2694 na++;
2695 n++;
2696 } else
2697 Py_INCREF(func);
2698 sp = stack_pointer;
2699 READ_TIMESTAMP(intr0);
2700 x = ext_do_call(func, &sp, flags, na, nk);
2701 READ_TIMESTAMP(intr1);
2702 stack_pointer = sp;
2703 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002704
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002705 while (stack_pointer > pfunc) {
2706 w = POP();
2707 Py_DECREF(w);
2708 }
2709 PUSH(x);
2710 if (x != NULL)
2711 DISPATCH();
2712 break;
2713 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002714
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002715 TARGET_WITH_IMPL(MAKE_CLOSURE, _make_function)
2716 TARGET(MAKE_FUNCTION)
2717 _make_function:
2718 {
2719 int posdefaults = oparg & 0xff;
2720 int kwdefaults = (oparg>>8) & 0xff;
2721 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002722
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002723 v = POP(); /* code object */
2724 x = PyFunction_New(v, f->f_globals);
2725 Py_DECREF(v);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002726
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002727 if (x != NULL && opcode == MAKE_CLOSURE) {
2728 v = POP();
2729 if (PyFunction_SetClosure(x, v) != 0) {
2730 /* Can't happen unless bytecode is corrupt. */
2731 why = WHY_EXCEPTION;
2732 }
2733 Py_DECREF(v);
2734 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002735
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002736 if (x != NULL && num_annotations > 0) {
2737 Py_ssize_t name_ix;
2738 u = POP(); /* names of args with annotations */
2739 v = PyDict_New();
2740 if (v == NULL) {
2741 Py_DECREF(x);
2742 x = NULL;
2743 break;
2744 }
2745 name_ix = PyTuple_Size(u);
2746 assert(num_annotations == name_ix+1);
2747 while (name_ix > 0) {
2748 --name_ix;
2749 t = PyTuple_GET_ITEM(u, name_ix);
2750 w = POP();
2751 /* XXX(nnorwitz): check for errors */
2752 PyDict_SetItem(v, t, w);
2753 Py_DECREF(w);
2754 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002755
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002756 if (PyFunction_SetAnnotations(x, v) != 0) {
2757 /* Can't happen unless
2758 PyFunction_SetAnnotations changes. */
2759 why = WHY_EXCEPTION;
2760 }
2761 Py_DECREF(v);
2762 Py_DECREF(u);
2763 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002764
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002765 /* XXX Maybe this should be a separate opcode? */
2766 if (x != NULL && posdefaults > 0) {
2767 v = PyTuple_New(posdefaults);
2768 if (v == NULL) {
2769 Py_DECREF(x);
2770 x = NULL;
2771 break;
2772 }
2773 while (--posdefaults >= 0) {
2774 w = POP();
2775 PyTuple_SET_ITEM(v, posdefaults, w);
2776 }
2777 if (PyFunction_SetDefaults(x, v) != 0) {
2778 /* Can't happen unless
2779 PyFunction_SetDefaults changes. */
2780 why = WHY_EXCEPTION;
2781 }
2782 Py_DECREF(v);
2783 }
2784 if (x != NULL && kwdefaults > 0) {
2785 v = PyDict_New();
2786 if (v == NULL) {
2787 Py_DECREF(x);
2788 x = NULL;
2789 break;
2790 }
2791 while (--kwdefaults >= 0) {
2792 w = POP(); /* default value */
2793 u = POP(); /* kw only arg name */
2794 /* XXX(nnorwitz): check for errors */
2795 PyDict_SetItem(v, u, w);
2796 Py_DECREF(w);
2797 Py_DECREF(u);
2798 }
2799 if (PyFunction_SetKwDefaults(x, v) != 0) {
2800 /* Can't happen unless
2801 PyFunction_SetKwDefaults changes. */
2802 why = WHY_EXCEPTION;
2803 }
2804 Py_DECREF(v);
2805 }
2806 PUSH(x);
2807 break;
2808 }
Guido van Rossum8861b741996-07-30 16:49:37 +00002809
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002810 TARGET(BUILD_SLICE)
2811 if (oparg == 3)
2812 w = POP();
2813 else
2814 w = NULL;
2815 v = POP();
2816 u = TOP();
2817 x = PySlice_New(u, v, w);
2818 Py_DECREF(u);
2819 Py_DECREF(v);
2820 Py_XDECREF(w);
2821 SET_TOP(x);
2822 if (x != NULL) DISPATCH();
2823 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002824
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002825 TARGET(EXTENDED_ARG)
2826 opcode = NEXTOP();
2827 oparg = oparg<<16 | NEXTARG();
2828 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002829
Antoine Pitrou042b1282010-08-13 21:15:58 +00002830#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002831 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00002832#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002833 default:
2834 fprintf(stderr,
2835 "XXX lineno: %d, opcode: %d\n",
2836 PyFrame_GetLineNumber(f),
2837 opcode);
2838 PyErr_SetString(PyExc_SystemError, "unknown opcode");
2839 why = WHY_EXCEPTION;
2840 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002841
2842#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002843 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002844#endif
2845
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002846 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00002847
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002848 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002849
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002850 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002851
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002852 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002853
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002854 if (why == WHY_NOT) {
2855 if (err == 0 && x != NULL) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002856#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002857 /* This check is expensive! */
2858 if (PyErr_Occurred())
2859 fprintf(stderr,
2860 "XXX undetected error\n");
2861 else {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002862#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002863 READ_TIMESTAMP(loop1);
2864 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002865#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002866 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002867#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002868 }
2869 why = WHY_EXCEPTION;
2870 x = Py_None;
2871 err = 0;
2872 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002873
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002874 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002875
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002876 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
2877 if (!PyErr_Occurred()) {
2878 PyErr_SetString(PyExc_SystemError,
2879 "error return without exception set");
2880 why = WHY_EXCEPTION;
2881 }
2882 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002883#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002884 else {
2885 /* This check is expensive! */
2886 if (PyErr_Occurred()) {
2887 char buf[128];
2888 sprintf(buf, "Stack unwind with exception "
2889 "set and why=%d", why);
2890 Py_FatalError(buf);
2891 }
2892 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002893#endif
2894
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002895 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002896
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002897 if (why == WHY_EXCEPTION) {
2898 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002899
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002900 if (tstate->c_tracefunc != NULL)
2901 call_exc_trace(tstate->c_tracefunc,
2902 tstate->c_traceobj, f);
2903 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002904
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002905 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002906
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002907 if (why == WHY_RERAISE)
2908 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +00002909
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002910 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002911
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002912fast_block_end:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002913 while (why != WHY_NOT && f->f_iblock > 0) {
2914 /* Peek at the current block. */
2915 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002916
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002917 assert(why != WHY_YIELD);
2918 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2919 why = WHY_NOT;
2920 JUMPTO(PyLong_AS_LONG(retval));
2921 Py_DECREF(retval);
2922 break;
2923 }
2924 /* Now we have to pop the block. */
2925 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002926
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002927 if (b->b_type == EXCEPT_HANDLER) {
2928 UNWIND_EXCEPT_HANDLER(b);
2929 continue;
2930 }
2931 UNWIND_BLOCK(b);
2932 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2933 why = WHY_NOT;
2934 JUMPTO(b->b_handler);
2935 break;
2936 }
2937 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
2938 || b->b_type == SETUP_FINALLY)) {
2939 PyObject *exc, *val, *tb;
2940 int handler = b->b_handler;
2941 /* Beware, this invalidates all b->b_* fields */
2942 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
2943 PUSH(tstate->exc_traceback);
2944 PUSH(tstate->exc_value);
2945 if (tstate->exc_type != NULL) {
2946 PUSH(tstate->exc_type);
2947 }
2948 else {
2949 Py_INCREF(Py_None);
2950 PUSH(Py_None);
2951 }
2952 PyErr_Fetch(&exc, &val, &tb);
2953 /* Make the raw exception data
2954 available to the handler,
2955 so a program can emulate the
2956 Python main loop. */
2957 PyErr_NormalizeException(
2958 &exc, &val, &tb);
2959 PyException_SetTraceback(val, tb);
2960 Py_INCREF(exc);
2961 tstate->exc_type = exc;
2962 Py_INCREF(val);
2963 tstate->exc_value = val;
2964 tstate->exc_traceback = tb;
2965 if (tb == NULL)
2966 tb = Py_None;
2967 Py_INCREF(tb);
2968 PUSH(tb);
2969 PUSH(val);
2970 PUSH(exc);
2971 why = WHY_NOT;
2972 JUMPTO(handler);
2973 break;
2974 }
2975 if (b->b_type == SETUP_FINALLY) {
2976 if (why & (WHY_RETURN | WHY_CONTINUE))
2977 PUSH(retval);
2978 PUSH(PyLong_FromLong((long)why));
2979 why = WHY_NOT;
2980 JUMPTO(b->b_handler);
2981 break;
2982 }
2983 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00002984
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002985 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002986
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002987 if (why != WHY_NOT)
2988 break;
2989 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002990
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002991 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002992
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002993 assert(why != WHY_YIELD);
2994 /* Pop remaining stack entries. */
2995 while (!EMPTY()) {
2996 v = POP();
2997 Py_XDECREF(v);
2998 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00002999
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003000 if (why != WHY_RETURN)
3001 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003002
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003003fast_yield:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003004 if (tstate->use_tracing) {
3005 if (tstate->c_tracefunc) {
3006 if (why == WHY_RETURN || why == WHY_YIELD) {
3007 if (call_trace(tstate->c_tracefunc,
3008 tstate->c_traceobj, f,
3009 PyTrace_RETURN, retval)) {
3010 Py_XDECREF(retval);
3011 retval = NULL;
3012 why = WHY_EXCEPTION;
3013 }
3014 }
3015 else if (why == WHY_EXCEPTION) {
3016 call_trace_protected(tstate->c_tracefunc,
3017 tstate->c_traceobj, f,
3018 PyTrace_RETURN, NULL);
3019 }
3020 }
3021 if (tstate->c_profilefunc) {
3022 if (why == WHY_EXCEPTION)
3023 call_trace_protected(tstate->c_profilefunc,
3024 tstate->c_profileobj, f,
3025 PyTrace_RETURN, NULL);
3026 else if (call_trace(tstate->c_profilefunc,
3027 tstate->c_profileobj, f,
3028 PyTrace_RETURN, retval)) {
3029 Py_XDECREF(retval);
3030 retval = NULL;
3031 why = WHY_EXCEPTION;
3032 }
3033 }
3034 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003035
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003036 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003037exit_eval_frame:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003038 Py_LeaveRecursiveCall();
3039 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003040
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003041 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00003042}
3043
Guido van Rossumc2e20742006-02-27 22:32:47 +00003044/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003045 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003046 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003047
Tim Peters6d6c1a32001-08-02 04:15:00 +00003048PyObject *
3049PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003050 PyObject **args, int argcount, PyObject **kws, int kwcount,
3051 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00003052{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003053 register PyFrameObject *f;
3054 register PyObject *retval = NULL;
3055 register PyObject **fastlocals, **freevars;
3056 PyThreadState *tstate = PyThreadState_GET();
3057 PyObject *x, *u;
3058 int total_args = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00003059
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003060 if (globals == NULL) {
3061 PyErr_SetString(PyExc_SystemError,
3062 "PyEval_EvalCodeEx: NULL globals");
3063 return NULL;
3064 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003065
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003066 assert(tstate != NULL);
3067 assert(globals != NULL);
3068 f = PyFrame_New(tstate, co, globals, locals);
3069 if (f == NULL)
3070 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003071
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003072 fastlocals = f->f_localsplus;
3073 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003074
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003075 if (total_args || co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
3076 int i;
3077 int n = argcount;
3078 PyObject *kwdict = NULL;
3079 if (co->co_flags & CO_VARKEYWORDS) {
3080 kwdict = PyDict_New();
3081 if (kwdict == NULL)
3082 goto fail;
3083 i = total_args;
3084 if (co->co_flags & CO_VARARGS)
3085 i++;
3086 SETLOCAL(i, kwdict);
3087 }
3088 if (argcount > co->co_argcount) {
3089 if (!(co->co_flags & CO_VARARGS)) {
3090 PyErr_Format(PyExc_TypeError,
3091 "%U() takes %s %d "
Benjamin Peterson88968ad2010-06-25 19:30:21 +00003092 "positional argument%s (%d given)",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003093 co->co_name,
3094 defcount ? "at most" : "exactly",
Benjamin Peterson88968ad2010-06-25 19:30:21 +00003095 co->co_argcount,
3096 co->co_argcount == 1 ? "" : "s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003097 argcount + kwcount);
3098 goto fail;
3099 }
3100 n = co->co_argcount;
3101 }
3102 for (i = 0; i < n; i++) {
3103 x = args[i];
3104 Py_INCREF(x);
3105 SETLOCAL(i, x);
3106 }
3107 if (co->co_flags & CO_VARARGS) {
3108 u = PyTuple_New(argcount - n);
3109 if (u == NULL)
3110 goto fail;
3111 SETLOCAL(total_args, u);
3112 for (i = n; i < argcount; i++) {
3113 x = args[i];
3114 Py_INCREF(x);
3115 PyTuple_SET_ITEM(u, i-n, x);
3116 }
3117 }
3118 for (i = 0; i < kwcount; i++) {
3119 PyObject **co_varnames;
3120 PyObject *keyword = kws[2*i];
3121 PyObject *value = kws[2*i + 1];
3122 int j;
3123 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3124 PyErr_Format(PyExc_TypeError,
3125 "%U() keywords must be strings",
3126 co->co_name);
3127 goto fail;
3128 }
3129 /* Speed hack: do raw pointer compares. As names are
3130 normally interned this should almost always hit. */
3131 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3132 for (j = 0; j < total_args; j++) {
3133 PyObject *nm = co_varnames[j];
3134 if (nm == keyword)
3135 goto kw_found;
3136 }
3137 /* Slow fallback, just in case */
3138 for (j = 0; j < total_args; j++) {
3139 PyObject *nm = co_varnames[j];
3140 int cmp = PyObject_RichCompareBool(
3141 keyword, nm, Py_EQ);
3142 if (cmp > 0)
3143 goto kw_found;
3144 else if (cmp < 0)
3145 goto fail;
3146 }
3147 if (j >= total_args && kwdict == NULL) {
3148 PyErr_Format(PyExc_TypeError,
3149 "%U() got an unexpected "
3150 "keyword argument '%S'",
3151 co->co_name,
3152 keyword);
3153 goto fail;
3154 }
3155 PyDict_SetItem(kwdict, keyword, value);
3156 continue;
3157 kw_found:
3158 if (GETLOCAL(j) != NULL) {
3159 PyErr_Format(PyExc_TypeError,
3160 "%U() got multiple "
3161 "values for keyword "
3162 "argument '%S'",
3163 co->co_name,
3164 keyword);
3165 goto fail;
3166 }
3167 Py_INCREF(value);
3168 SETLOCAL(j, value);
3169 }
3170 if (co->co_kwonlyargcount > 0) {
3171 for (i = co->co_argcount; i < total_args; i++) {
3172 PyObject *name;
3173 if (GETLOCAL(i) != NULL)
3174 continue;
3175 name = PyTuple_GET_ITEM(co->co_varnames, i);
3176 if (kwdefs != NULL) {
3177 PyObject *def = PyDict_GetItem(kwdefs, name);
3178 if (def) {
3179 Py_INCREF(def);
3180 SETLOCAL(i, def);
3181 continue;
3182 }
3183 }
3184 PyErr_Format(PyExc_TypeError,
3185 "%U() needs keyword-only argument %S",
3186 co->co_name, name);
3187 goto fail;
3188 }
3189 }
3190 if (argcount < co->co_argcount) {
3191 int m = co->co_argcount - defcount;
3192 for (i = argcount; i < m; i++) {
3193 if (GETLOCAL(i) == NULL) {
3194 int j, given = 0;
3195 for (j = 0; j < co->co_argcount; j++)
3196 if (GETLOCAL(j))
3197 given++;
3198 PyErr_Format(PyExc_TypeError,
3199 "%U() takes %s %d "
3200 "argument%s "
3201 "(%d given)",
3202 co->co_name,
3203 ((co->co_flags & CO_VARARGS) ||
3204 defcount) ? "at least"
3205 : "exactly",
3206 m, m == 1 ? "" : "s", given);
3207 goto fail;
3208 }
3209 }
3210 if (n > m)
3211 i = n - m;
3212 else
3213 i = 0;
3214 for (; i < defcount; i++) {
3215 if (GETLOCAL(m+i) == NULL) {
3216 PyObject *def = defs[i];
3217 Py_INCREF(def);
3218 SETLOCAL(m+i, def);
3219 }
3220 }
3221 }
3222 }
3223 else if (argcount > 0 || kwcount > 0) {
3224 PyErr_Format(PyExc_TypeError,
3225 "%U() takes no arguments (%d given)",
3226 co->co_name,
3227 argcount + kwcount);
3228 goto fail;
3229 }
3230 /* Allocate and initialize storage for cell vars, and copy free
3231 vars into frame. This isn't too efficient right now. */
3232 if (PyTuple_GET_SIZE(co->co_cellvars)) {
3233 int i, j, nargs, found;
3234 Py_UNICODE *cellname, *argname;
3235 PyObject *c;
Tim Peters5ca576e2001-06-18 22:08:13 +00003236
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003237 nargs = total_args;
3238 if (co->co_flags & CO_VARARGS)
3239 nargs++;
3240 if (co->co_flags & CO_VARKEYWORDS)
3241 nargs++;
Tim Peters5ca576e2001-06-18 22:08:13 +00003242
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003243 /* Initialize each cell var, taking into account
3244 cell vars that are initialized from arguments.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003245
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003246 Should arrange for the compiler to put cellvars
3247 that are arguments at the beginning of the cellvars
3248 list so that we can march over it more efficiently?
3249 */
3250 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
3251 cellname = PyUnicode_AS_UNICODE(
3252 PyTuple_GET_ITEM(co->co_cellvars, i));
3253 found = 0;
3254 for (j = 0; j < nargs; j++) {
3255 argname = PyUnicode_AS_UNICODE(
3256 PyTuple_GET_ITEM(co->co_varnames, j));
3257 if (Py_UNICODE_strcmp(cellname, argname) == 0) {
3258 c = PyCell_New(GETLOCAL(j));
3259 if (c == NULL)
3260 goto fail;
3261 GETLOCAL(co->co_nlocals + i) = c;
3262 found = 1;
3263 break;
3264 }
3265 }
3266 if (found == 0) {
3267 c = PyCell_New(NULL);
3268 if (c == NULL)
3269 goto fail;
3270 SETLOCAL(co->co_nlocals + i, c);
3271 }
3272 }
3273 }
3274 if (PyTuple_GET_SIZE(co->co_freevars)) {
3275 int i;
3276 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3277 PyObject *o = PyTuple_GET_ITEM(closure, i);
3278 Py_INCREF(o);
3279 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
3280 }
3281 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003283 if (co->co_flags & CO_GENERATOR) {
3284 /* Don't need to keep the reference to f_back, it will be set
3285 * when the generator is resumed. */
3286 Py_XDECREF(f->f_back);
3287 f->f_back = NULL;
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003288
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003289 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003290
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003291 /* Create a new generator that owns the ready to run frame
3292 * and return that as the value. */
3293 return PyGen_New(f);
3294 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003296 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003297
Thomas Woutersce272b62007-09-19 21:19:28 +00003298fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003299
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003300 /* decref'ing the frame can cause __del__ methods to get invoked,
3301 which can call back into Python. While we're done with the
3302 current Python frame (f), the associated C stack is still in use,
3303 so recursion_depth must be boosted for the duration.
3304 */
3305 assert(tstate != NULL);
3306 ++tstate->recursion_depth;
3307 Py_DECREF(f);
3308 --tstate->recursion_depth;
3309 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003310}
3311
3312
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003313static PyObject *
3314special_lookup(PyObject *o, char *meth, PyObject **cache)
3315{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003316 PyObject *res;
3317 res = _PyObject_LookupSpecial(o, meth, cache);
3318 if (res == NULL && !PyErr_Occurred()) {
3319 PyErr_SetObject(PyExc_AttributeError, *cache);
3320 return NULL;
3321 }
3322 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003323}
3324
3325
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003326/* Logic for the raise statement (too complicated for inlining).
3327 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00003328static enum why_code
Collin Winter828f04a2007-08-31 00:04:24 +00003329do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003330{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003331 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003333 if (exc == NULL) {
3334 /* Reraise */
3335 PyThreadState *tstate = PyThreadState_GET();
3336 PyObject *tb;
3337 type = tstate->exc_type;
3338 value = tstate->exc_value;
3339 tb = tstate->exc_traceback;
3340 if (type == Py_None) {
3341 PyErr_SetString(PyExc_RuntimeError,
3342 "No active exception to reraise");
3343 return WHY_EXCEPTION;
3344 }
3345 Py_XINCREF(type);
3346 Py_XINCREF(value);
3347 Py_XINCREF(tb);
3348 PyErr_Restore(type, value, tb);
3349 return WHY_RERAISE;
3350 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003352 /* We support the following forms of raise:
3353 raise
Collin Winter828f04a2007-08-31 00:04:24 +00003354 raise <instance>
3355 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003357 if (PyExceptionClass_Check(exc)) {
3358 type = exc;
3359 value = PyObject_CallObject(exc, NULL);
3360 if (value == NULL)
3361 goto raise_error;
3362 }
3363 else if (PyExceptionInstance_Check(exc)) {
3364 value = exc;
3365 type = PyExceptionInstance_Class(exc);
3366 Py_INCREF(type);
3367 }
3368 else {
3369 /* Not something you can raise. You get an exception
3370 anyway, just not what you specified :-) */
3371 Py_DECREF(exc);
3372 PyErr_SetString(PyExc_TypeError,
3373 "exceptions must derive from BaseException");
3374 goto raise_error;
3375 }
Collin Winter828f04a2007-08-31 00:04:24 +00003376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003377 if (cause) {
3378 PyObject *fixed_cause;
3379 if (PyExceptionClass_Check(cause)) {
3380 fixed_cause = PyObject_CallObject(cause, NULL);
3381 if (fixed_cause == NULL)
3382 goto raise_error;
3383 Py_DECREF(cause);
3384 }
3385 else if (PyExceptionInstance_Check(cause)) {
3386 fixed_cause = cause;
3387 }
3388 else {
3389 PyErr_SetString(PyExc_TypeError,
3390 "exception causes must derive from "
3391 "BaseException");
3392 goto raise_error;
3393 }
3394 PyException_SetCause(value, fixed_cause);
3395 }
Collin Winter828f04a2007-08-31 00:04:24 +00003396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003397 PyErr_SetObject(type, value);
3398 /* PyErr_SetObject incref's its arguments */
3399 Py_XDECREF(value);
3400 Py_XDECREF(type);
3401 return WHY_EXCEPTION;
Collin Winter828f04a2007-08-31 00:04:24 +00003402
3403raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003404 Py_XDECREF(value);
3405 Py_XDECREF(type);
3406 Py_XDECREF(cause);
3407 return WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003408}
3409
Tim Petersd6d010b2001-06-21 02:49:55 +00003410/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00003411 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003412
Guido van Rossum0368b722007-05-11 16:50:42 +00003413 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
3414 with a variable target.
3415*/
Tim Petersd6d010b2001-06-21 02:49:55 +00003416
Barry Warsawe42b18f1997-08-25 22:13:04 +00003417static int
Guido van Rossum0368b722007-05-11 16:50:42 +00003418unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003419{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003420 int i = 0, j = 0;
3421 Py_ssize_t ll = 0;
3422 PyObject *it; /* iter(v) */
3423 PyObject *w;
3424 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00003425
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003426 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00003427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003428 it = PyObject_GetIter(v);
3429 if (it == NULL)
3430 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00003431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003432 for (; i < argcnt; i++) {
3433 w = PyIter_Next(it);
3434 if (w == NULL) {
3435 /* Iterator done, via error or exhaustion. */
3436 if (!PyErr_Occurred()) {
3437 PyErr_Format(PyExc_ValueError,
3438 "need more than %d value%s to unpack",
3439 i, i == 1 ? "" : "s");
3440 }
3441 goto Error;
3442 }
3443 *--sp = w;
3444 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003445
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003446 if (argcntafter == -1) {
3447 /* We better have exhausted the iterator now. */
3448 w = PyIter_Next(it);
3449 if (w == NULL) {
3450 if (PyErr_Occurred())
3451 goto Error;
3452 Py_DECREF(it);
3453 return 1;
3454 }
3455 Py_DECREF(w);
Georg Brandl0310a832010-07-10 10:32:36 +00003456 PyErr_Format(PyExc_ValueError, "too many values to unpack "
3457 "(expected %d)", argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003458 goto Error;
3459 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003461 l = PySequence_List(it);
3462 if (l == NULL)
3463 goto Error;
3464 *--sp = l;
3465 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00003466
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003467 ll = PyList_GET_SIZE(l);
3468 if (ll < argcntafter) {
3469 PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack",
3470 argcnt + ll);
3471 goto Error;
3472 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003474 /* Pop the "after-variable" args off the list. */
3475 for (j = argcntafter; j > 0; j--, i++) {
3476 *--sp = PyList_GET_ITEM(l, ll - j);
3477 }
3478 /* Resize the list. */
3479 Py_SIZE(l) = ll - argcntafter;
3480 Py_DECREF(it);
3481 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00003482
Tim Petersd6d010b2001-06-21 02:49:55 +00003483Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003484 for (; i > 0; i--, sp++)
3485 Py_DECREF(*sp);
3486 Py_XDECREF(it);
3487 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003488}
3489
3490
Guido van Rossum96a42c81992-01-12 02:29:51 +00003491#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003492static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003493prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003494{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003495 printf("%s ", str);
3496 if (PyObject_Print(v, stdout, 0) != 0)
3497 PyErr_Clear(); /* Don't know what else to do */
3498 printf("\n");
3499 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003500}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003501#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003502
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003503static void
Fred Drake5755ce62001-06-27 19:19:46 +00003504call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003505{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003506 PyObject *type, *value, *traceback, *arg;
3507 int err;
3508 PyErr_Fetch(&type, &value, &traceback);
3509 if (value == NULL) {
3510 value = Py_None;
3511 Py_INCREF(value);
3512 }
3513 arg = PyTuple_Pack(3, type, value, traceback);
3514 if (arg == NULL) {
3515 PyErr_Restore(type, value, traceback);
3516 return;
3517 }
3518 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
3519 Py_DECREF(arg);
3520 if (err == 0)
3521 PyErr_Restore(type, value, traceback);
3522 else {
3523 Py_XDECREF(type);
3524 Py_XDECREF(value);
3525 Py_XDECREF(traceback);
3526 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003527}
3528
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003529static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003530call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003531 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003532{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003533 PyObject *type, *value, *traceback;
3534 int err;
3535 PyErr_Fetch(&type, &value, &traceback);
3536 err = call_trace(func, obj, frame, what, arg);
3537 if (err == 0)
3538 {
3539 PyErr_Restore(type, value, traceback);
3540 return 0;
3541 }
3542 else {
3543 Py_XDECREF(type);
3544 Py_XDECREF(value);
3545 Py_XDECREF(traceback);
3546 return -1;
3547 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003548}
3549
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003550static int
Fred Drake5755ce62001-06-27 19:19:46 +00003551call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003552 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003553{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003554 register PyThreadState *tstate = frame->f_tstate;
3555 int result;
3556 if (tstate->tracing)
3557 return 0;
3558 tstate->tracing++;
3559 tstate->use_tracing = 0;
3560 result = func(obj, frame, what, arg);
3561 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3562 || (tstate->c_profilefunc != NULL));
3563 tstate->tracing--;
3564 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003565}
3566
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003567PyObject *
3568_PyEval_CallTracing(PyObject *func, PyObject *args)
3569{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003570 PyFrameObject *frame = PyEval_GetFrame();
3571 PyThreadState *tstate = frame->f_tstate;
3572 int save_tracing = tstate->tracing;
3573 int save_use_tracing = tstate->use_tracing;
3574 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003576 tstate->tracing = 0;
3577 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3578 || (tstate->c_profilefunc != NULL));
3579 result = PyObject_Call(func, args, NULL);
3580 tstate->tracing = save_tracing;
3581 tstate->use_tracing = save_use_tracing;
3582 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003583}
3584
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003585/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00003586static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003587maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003588 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3589 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003590{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003591 int result = 0;
3592 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003593
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003594 /* If the last instruction executed isn't in the current
3595 instruction window, reset the window.
3596 */
3597 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
3598 PyAddrPair bounds;
3599 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3600 &bounds);
3601 *instr_lb = bounds.ap_lower;
3602 *instr_ub = bounds.ap_upper;
3603 }
3604 /* If the last instruction falls at the start of a line or if
3605 it represents a jump backwards, update the frame's line
3606 number and call the trace function. */
3607 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
3608 frame->f_lineno = line;
3609 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
3610 }
3611 *instr_prev = frame->f_lasti;
3612 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003613}
3614
Fred Drake5755ce62001-06-27 19:19:46 +00003615void
3616PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003617{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003618 PyThreadState *tstate = PyThreadState_GET();
3619 PyObject *temp = tstate->c_profileobj;
3620 Py_XINCREF(arg);
3621 tstate->c_profilefunc = NULL;
3622 tstate->c_profileobj = NULL;
3623 /* Must make sure that tracing is not ignored if 'temp' is freed */
3624 tstate->use_tracing = tstate->c_tracefunc != NULL;
3625 Py_XDECREF(temp);
3626 tstate->c_profilefunc = func;
3627 tstate->c_profileobj = arg;
3628 /* Flag that tracing or profiling is turned on */
3629 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003630}
3631
3632void
3633PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3634{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003635 PyThreadState *tstate = PyThreadState_GET();
3636 PyObject *temp = tstate->c_traceobj;
3637 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
3638 Py_XINCREF(arg);
3639 tstate->c_tracefunc = NULL;
3640 tstate->c_traceobj = NULL;
3641 /* Must make sure that profiling is not ignored if 'temp' is freed */
3642 tstate->use_tracing = tstate->c_profilefunc != NULL;
3643 Py_XDECREF(temp);
3644 tstate->c_tracefunc = func;
3645 tstate->c_traceobj = arg;
3646 /* Flag that tracing or profiling is turned on */
3647 tstate->use_tracing = ((func != NULL)
3648 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003649}
3650
Guido van Rossumb209a111997-04-29 18:18:01 +00003651PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003652PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003653{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003654 PyFrameObject *current_frame = PyEval_GetFrame();
3655 if (current_frame == NULL)
3656 return PyThreadState_GET()->interp->builtins;
3657 else
3658 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003659}
3660
Guido van Rossumb209a111997-04-29 18:18:01 +00003661PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003662PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003663{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003664 PyFrameObject *current_frame = PyEval_GetFrame();
3665 if (current_frame == NULL)
3666 return NULL;
3667 PyFrame_FastToLocals(current_frame);
3668 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00003669}
3670
Guido van Rossumb209a111997-04-29 18:18:01 +00003671PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003672PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003673{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003674 PyFrameObject *current_frame = PyEval_GetFrame();
3675 if (current_frame == NULL)
3676 return NULL;
3677 else
3678 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003679}
3680
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003681PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003682PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003683{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003684 PyThreadState *tstate = PyThreadState_GET();
3685 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003686}
3687
Guido van Rossum6135a871995-01-09 17:53:26 +00003688int
Tim Peters5ba58662001-07-16 02:29:45 +00003689PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003690{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003691 PyFrameObject *current_frame = PyEval_GetFrame();
3692 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003693
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003694 if (current_frame != NULL) {
3695 const int codeflags = current_frame->f_code->co_flags;
3696 const int compilerflags = codeflags & PyCF_MASK;
3697 if (compilerflags) {
3698 result = 1;
3699 cf->cf_flags |= compilerflags;
3700 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003701#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003702 if (codeflags & CO_GENERATOR_ALLOWED) {
3703 result = 1;
3704 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3705 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003706#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003707 }
3708 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003709}
3710
Guido van Rossum3f5da241990-12-20 15:06:42 +00003711
Guido van Rossum681d79a1995-07-18 14:51:37 +00003712/* External interface to call any callable object.
Antoine Pitrou8689a102010-04-01 16:53:15 +00003713 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00003714
Guido van Rossumb209a111997-04-29 18:18:01 +00003715PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003716PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003717{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003718 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003719
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003720 if (arg == NULL) {
3721 arg = PyTuple_New(0);
3722 if (arg == NULL)
3723 return NULL;
3724 }
3725 else if (!PyTuple_Check(arg)) {
3726 PyErr_SetString(PyExc_TypeError,
3727 "argument list must be a tuple");
3728 return NULL;
3729 }
3730 else
3731 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003732
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003733 if (kw != NULL && !PyDict_Check(kw)) {
3734 PyErr_SetString(PyExc_TypeError,
3735 "keyword list must be a dictionary");
3736 Py_DECREF(arg);
3737 return NULL;
3738 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00003739
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003740 result = PyObject_Call(func, arg, kw);
3741 Py_DECREF(arg);
3742 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00003743}
3744
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003745const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003746PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003747{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003748 if (PyMethod_Check(func))
3749 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
3750 else if (PyFunction_Check(func))
3751 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
3752 else if (PyCFunction_Check(func))
3753 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3754 else
3755 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00003756}
3757
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003758const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003759PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003760{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003761 if (PyMethod_Check(func))
3762 return "()";
3763 else if (PyFunction_Check(func))
3764 return "()";
3765 else if (PyCFunction_Check(func))
3766 return "()";
3767 else
3768 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00003769}
3770
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003771static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003772err_args(PyObject *func, int flags, int nargs)
3773{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003774 if (flags & METH_NOARGS)
3775 PyErr_Format(PyExc_TypeError,
3776 "%.200s() takes no arguments (%d given)",
3777 ((PyCFunctionObject *)func)->m_ml->ml_name,
3778 nargs);
3779 else
3780 PyErr_Format(PyExc_TypeError,
3781 "%.200s() takes exactly one argument (%d given)",
3782 ((PyCFunctionObject *)func)->m_ml->ml_name,
3783 nargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003784}
3785
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003786#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003787if (tstate->use_tracing && tstate->c_profilefunc) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003788 if (call_trace(tstate->c_profilefunc, \
3789 tstate->c_profileobj, \
3790 tstate->frame, PyTrace_C_CALL, \
3791 func)) { \
3792 x = NULL; \
3793 } \
3794 else { \
3795 x = call; \
3796 if (tstate->c_profilefunc != NULL) { \
3797 if (x == NULL) { \
3798 call_trace_protected(tstate->c_profilefunc, \
3799 tstate->c_profileobj, \
3800 tstate->frame, PyTrace_C_EXCEPTION, \
3801 func); \
3802 /* XXX should pass (type, value, tb) */ \
3803 } else { \
3804 if (call_trace(tstate->c_profilefunc, \
3805 tstate->c_profileobj, \
3806 tstate->frame, PyTrace_C_RETURN, \
3807 func)) { \
3808 Py_DECREF(x); \
3809 x = NULL; \
3810 } \
3811 } \
3812 } \
3813 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003814} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003815 x = call; \
3816 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003817
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003818static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003819call_function(PyObject ***pp_stack, int oparg
3820#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003821 , uint64* pintr0, uint64* pintr1
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003822#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003823 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003824{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003825 int na = oparg & 0xff;
3826 int nk = (oparg>>8) & 0xff;
3827 int n = na + 2 * nk;
3828 PyObject **pfunc = (*pp_stack) - n - 1;
3829 PyObject *func = *pfunc;
3830 PyObject *x, *w;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003831
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003832 /* Always dispatch PyCFunction first, because these are
3833 presumed to be the most frequent callable object.
3834 */
3835 if (PyCFunction_Check(func) && nk == 0) {
3836 int flags = PyCFunction_GET_FLAGS(func);
3837 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003838
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003839 PCALL(PCALL_CFUNCTION);
3840 if (flags & (METH_NOARGS | METH_O)) {
3841 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3842 PyObject *self = PyCFunction_GET_SELF(func);
3843 if (flags & METH_NOARGS && na == 0) {
3844 C_TRACE(x, (*meth)(self,NULL));
3845 }
3846 else if (flags & METH_O && na == 1) {
3847 PyObject *arg = EXT_POP(*pp_stack);
3848 C_TRACE(x, (*meth)(self,arg));
3849 Py_DECREF(arg);
3850 }
3851 else {
3852 err_args(func, flags, na);
3853 x = NULL;
3854 }
3855 }
3856 else {
3857 PyObject *callargs;
3858 callargs = load_args(pp_stack, na);
3859 READ_TIMESTAMP(*pintr0);
3860 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
3861 READ_TIMESTAMP(*pintr1);
3862 Py_XDECREF(callargs);
3863 }
3864 } else {
3865 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3866 /* optimize access to bound methods */
3867 PyObject *self = PyMethod_GET_SELF(func);
3868 PCALL(PCALL_METHOD);
3869 PCALL(PCALL_BOUND_METHOD);
3870 Py_INCREF(self);
3871 func = PyMethod_GET_FUNCTION(func);
3872 Py_INCREF(func);
3873 Py_DECREF(*pfunc);
3874 *pfunc = self;
3875 na++;
3876 n++;
3877 } else
3878 Py_INCREF(func);
3879 READ_TIMESTAMP(*pintr0);
3880 if (PyFunction_Check(func))
3881 x = fast_function(func, pp_stack, n, na, nk);
3882 else
3883 x = do_call(func, pp_stack, na, nk);
3884 READ_TIMESTAMP(*pintr1);
3885 Py_DECREF(func);
3886 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003887
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003888 /* Clear the stack of the function object. Also removes
3889 the arguments in case they weren't consumed already
3890 (fast_function() and err_args() leave them on the stack).
3891 */
3892 while ((*pp_stack) > pfunc) {
3893 w = EXT_POP(*pp_stack);
3894 Py_DECREF(w);
3895 PCALL(PCALL_POP);
3896 }
3897 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003898}
3899
Jeremy Hylton192690e2002-08-16 18:36:11 +00003900/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003901 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003902 For the simplest case -- a function that takes only positional
3903 arguments and is called with only positional arguments -- it
3904 inlines the most primitive frame setup code from
3905 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3906 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003907*/
3908
3909static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003910fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003911{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003912 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
3913 PyObject *globals = PyFunction_GET_GLOBALS(func);
3914 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3915 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
3916 PyObject **d = NULL;
3917 int nd = 0;
Jeremy Hylton52820442001-01-03 23:52:36 +00003918
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003919 PCALL(PCALL_FUNCTION);
3920 PCALL(PCALL_FAST_FUNCTION);
3921 if (argdefs == NULL && co->co_argcount == n &&
3922 co->co_kwonlyargcount == 0 && nk==0 &&
3923 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3924 PyFrameObject *f;
3925 PyObject *retval = NULL;
3926 PyThreadState *tstate = PyThreadState_GET();
3927 PyObject **fastlocals, **stack;
3928 int i;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003929
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003930 PCALL(PCALL_FASTER_FUNCTION);
3931 assert(globals != NULL);
3932 /* XXX Perhaps we should create a specialized
3933 PyFrame_New() that doesn't take locals, but does
3934 take builtins without sanity checking them.
3935 */
3936 assert(tstate != NULL);
3937 f = PyFrame_New(tstate, co, globals, NULL);
3938 if (f == NULL)
3939 return NULL;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003940
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003941 fastlocals = f->f_localsplus;
3942 stack = (*pp_stack) - n;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003943
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003944 for (i = 0; i < n; i++) {
3945 Py_INCREF(*stack);
3946 fastlocals[i] = *stack++;
3947 }
3948 retval = PyEval_EvalFrameEx(f,0);
3949 ++tstate->recursion_depth;
3950 Py_DECREF(f);
3951 --tstate->recursion_depth;
3952 return retval;
3953 }
3954 if (argdefs != NULL) {
3955 d = &PyTuple_GET_ITEM(argdefs, 0);
3956 nd = Py_SIZE(argdefs);
3957 }
3958 return PyEval_EvalCodeEx(co, globals,
3959 (PyObject *)NULL, (*pp_stack)-n, na,
3960 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
3961 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003962}
3963
3964static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003965update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3966 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003967{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003968 PyObject *kwdict = NULL;
3969 if (orig_kwdict == NULL)
3970 kwdict = PyDict_New();
3971 else {
3972 kwdict = PyDict_Copy(orig_kwdict);
3973 Py_DECREF(orig_kwdict);
3974 }
3975 if (kwdict == NULL)
3976 return NULL;
3977 while (--nk >= 0) {
3978 int err;
3979 PyObject *value = EXT_POP(*pp_stack);
3980 PyObject *key = EXT_POP(*pp_stack);
3981 if (PyDict_GetItem(kwdict, key) != NULL) {
3982 PyErr_Format(PyExc_TypeError,
3983 "%.200s%s got multiple values "
3984 "for keyword argument '%U'",
3985 PyEval_GetFuncName(func),
3986 PyEval_GetFuncDesc(func),
3987 key);
3988 Py_DECREF(key);
3989 Py_DECREF(value);
3990 Py_DECREF(kwdict);
3991 return NULL;
3992 }
3993 err = PyDict_SetItem(kwdict, key, value);
3994 Py_DECREF(key);
3995 Py_DECREF(value);
3996 if (err) {
3997 Py_DECREF(kwdict);
3998 return NULL;
3999 }
4000 }
4001 return kwdict;
Jeremy Hylton52820442001-01-03 23:52:36 +00004002}
4003
4004static PyObject *
4005update_star_args(int nstack, int nstar, PyObject *stararg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004006 PyObject ***pp_stack)
Jeremy Hylton52820442001-01-03 23:52:36 +00004007{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004008 PyObject *callargs, *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004009
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004010 callargs = PyTuple_New(nstack + nstar);
4011 if (callargs == NULL) {
4012 return NULL;
4013 }
4014 if (nstar) {
4015 int i;
4016 for (i = 0; i < nstar; i++) {
4017 PyObject *a = PyTuple_GET_ITEM(stararg, i);
4018 Py_INCREF(a);
4019 PyTuple_SET_ITEM(callargs, nstack + i, a);
4020 }
4021 }
4022 while (--nstack >= 0) {
4023 w = EXT_POP(*pp_stack);
4024 PyTuple_SET_ITEM(callargs, nstack, w);
4025 }
4026 return callargs;
Jeremy Hylton52820442001-01-03 23:52:36 +00004027}
4028
4029static PyObject *
4030load_args(PyObject ***pp_stack, int na)
4031{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004032 PyObject *args = PyTuple_New(na);
4033 PyObject *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004034
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004035 if (args == NULL)
4036 return NULL;
4037 while (--na >= 0) {
4038 w = EXT_POP(*pp_stack);
4039 PyTuple_SET_ITEM(args, na, w);
4040 }
4041 return args;
Jeremy Hylton52820442001-01-03 23:52:36 +00004042}
4043
4044static PyObject *
4045do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4046{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004047 PyObject *callargs = NULL;
4048 PyObject *kwdict = NULL;
4049 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004050
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004051 if (nk > 0) {
4052 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
4053 if (kwdict == NULL)
4054 goto call_fail;
4055 }
4056 callargs = load_args(pp_stack, na);
4057 if (callargs == NULL)
4058 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004059#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004060 /* At this point, we have to look at the type of func to
4061 update the call stats properly. Do it here so as to avoid
4062 exposing the call stats machinery outside ceval.c
4063 */
4064 if (PyFunction_Check(func))
4065 PCALL(PCALL_FUNCTION);
4066 else if (PyMethod_Check(func))
4067 PCALL(PCALL_METHOD);
4068 else if (PyType_Check(func))
4069 PCALL(PCALL_TYPE);
4070 else if (PyCFunction_Check(func))
4071 PCALL(PCALL_CFUNCTION);
4072 else
4073 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004074#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004075 if (PyCFunction_Check(func)) {
4076 PyThreadState *tstate = PyThreadState_GET();
4077 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4078 }
4079 else
4080 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00004081call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004082 Py_XDECREF(callargs);
4083 Py_XDECREF(kwdict);
4084 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004085}
4086
4087static PyObject *
4088ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4089{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004090 int nstar = 0;
4091 PyObject *callargs = NULL;
4092 PyObject *stararg = NULL;
4093 PyObject *kwdict = NULL;
4094 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004095
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004096 if (flags & CALL_FLAG_KW) {
4097 kwdict = EXT_POP(*pp_stack);
4098 if (!PyDict_Check(kwdict)) {
4099 PyObject *d;
4100 d = PyDict_New();
4101 if (d == NULL)
4102 goto ext_call_fail;
4103 if (PyDict_Update(d, kwdict) != 0) {
4104 Py_DECREF(d);
4105 /* PyDict_Update raises attribute
4106 * error (percolated from an attempt
4107 * to get 'keys' attribute) instead of
4108 * a type error if its second argument
4109 * is not a mapping.
4110 */
4111 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4112 PyErr_Format(PyExc_TypeError,
4113 "%.200s%.200s argument after ** "
4114 "must be a mapping, not %.200s",
4115 PyEval_GetFuncName(func),
4116 PyEval_GetFuncDesc(func),
4117 kwdict->ob_type->tp_name);
4118 }
4119 goto ext_call_fail;
4120 }
4121 Py_DECREF(kwdict);
4122 kwdict = d;
4123 }
4124 }
4125 if (flags & CALL_FLAG_VAR) {
4126 stararg = EXT_POP(*pp_stack);
4127 if (!PyTuple_Check(stararg)) {
4128 PyObject *t = NULL;
4129 t = PySequence_Tuple(stararg);
4130 if (t == NULL) {
4131 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4132 PyErr_Format(PyExc_TypeError,
4133 "%.200s%.200s argument after * "
4134 "must be a sequence, not %200s",
4135 PyEval_GetFuncName(func),
4136 PyEval_GetFuncDesc(func),
4137 stararg->ob_type->tp_name);
4138 }
4139 goto ext_call_fail;
4140 }
4141 Py_DECREF(stararg);
4142 stararg = t;
4143 }
4144 nstar = PyTuple_GET_SIZE(stararg);
4145 }
4146 if (nk > 0) {
4147 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
4148 if (kwdict == NULL)
4149 goto ext_call_fail;
4150 }
4151 callargs = update_star_args(na, nstar, stararg, pp_stack);
4152 if (callargs == NULL)
4153 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004154#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004155 /* At this point, we have to look at the type of func to
4156 update the call stats properly. Do it here so as to avoid
4157 exposing the call stats machinery outside ceval.c
4158 */
4159 if (PyFunction_Check(func))
4160 PCALL(PCALL_FUNCTION);
4161 else if (PyMethod_Check(func))
4162 PCALL(PCALL_METHOD);
4163 else if (PyType_Check(func))
4164 PCALL(PCALL_TYPE);
4165 else if (PyCFunction_Check(func))
4166 PCALL(PCALL_CFUNCTION);
4167 else
4168 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004169#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004170 if (PyCFunction_Check(func)) {
4171 PyThreadState *tstate = PyThreadState_GET();
4172 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4173 }
4174 else
4175 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00004176ext_call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004177 Py_XDECREF(callargs);
4178 Py_XDECREF(kwdict);
4179 Py_XDECREF(stararg);
4180 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004181}
4182
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004183/* Extract a slice index from a PyInt or PyLong or an object with the
4184 nb_index slot defined, and store in *pi.
4185 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4186 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 +00004187 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004188*/
Tim Petersb5196382001-12-16 19:44:20 +00004189/* Note: If v is NULL, return success without storing into *pi. This
4190 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4191 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004192*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004193int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004194_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004195{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004196 if (v != NULL) {
4197 Py_ssize_t x;
4198 if (PyIndex_Check(v)) {
4199 x = PyNumber_AsSsize_t(v, NULL);
4200 if (x == -1 && PyErr_Occurred())
4201 return 0;
4202 }
4203 else {
4204 PyErr_SetString(PyExc_TypeError,
4205 "slice indices must be integers or "
4206 "None or have an __index__ method");
4207 return 0;
4208 }
4209 *pi = x;
4210 }
4211 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004212}
4213
Guido van Rossum486364b2007-06-30 05:01:58 +00004214#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004215 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004216
Guido van Rossumb209a111997-04-29 18:18:01 +00004217static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004218cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004219{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004220 int res = 0;
4221 switch (op) {
4222 case PyCmp_IS:
4223 res = (v == w);
4224 break;
4225 case PyCmp_IS_NOT:
4226 res = (v != w);
4227 break;
4228 case PyCmp_IN:
4229 res = PySequence_Contains(w, v);
4230 if (res < 0)
4231 return NULL;
4232 break;
4233 case PyCmp_NOT_IN:
4234 res = PySequence_Contains(w, v);
4235 if (res < 0)
4236 return NULL;
4237 res = !res;
4238 break;
4239 case PyCmp_EXC_MATCH:
4240 if (PyTuple_Check(w)) {
4241 Py_ssize_t i, length;
4242 length = PyTuple_Size(w);
4243 for (i = 0; i < length; i += 1) {
4244 PyObject *exc = PyTuple_GET_ITEM(w, i);
4245 if (!PyExceptionClass_Check(exc)) {
4246 PyErr_SetString(PyExc_TypeError,
4247 CANNOT_CATCH_MSG);
4248 return NULL;
4249 }
4250 }
4251 }
4252 else {
4253 if (!PyExceptionClass_Check(w)) {
4254 PyErr_SetString(PyExc_TypeError,
4255 CANNOT_CATCH_MSG);
4256 return NULL;
4257 }
4258 }
4259 res = PyErr_GivenExceptionMatches(v, w);
4260 break;
4261 default:
4262 return PyObject_RichCompare(v, w, op);
4263 }
4264 v = res ? Py_True : Py_False;
4265 Py_INCREF(v);
4266 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004267}
4268
Thomas Wouters52152252000-08-17 22:55:00 +00004269static PyObject *
4270import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004271{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004272 PyObject *x;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004274 x = PyObject_GetAttr(v, name);
4275 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
4276 PyErr_Format(PyExc_ImportError, "cannot import name %S", name);
4277 }
4278 return x;
Thomas Wouters52152252000-08-17 22:55:00 +00004279}
Guido van Rossumac7be682001-01-17 15:42:30 +00004280
Thomas Wouters52152252000-08-17 22:55:00 +00004281static int
4282import_all_from(PyObject *locals, PyObject *v)
4283{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004284 PyObject *all = PyObject_GetAttrString(v, "__all__");
4285 PyObject *dict, *name, *value;
4286 int skip_leading_underscores = 0;
4287 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004288
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004289 if (all == NULL) {
4290 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4291 return -1; /* Unexpected error */
4292 PyErr_Clear();
4293 dict = PyObject_GetAttrString(v, "__dict__");
4294 if (dict == NULL) {
4295 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4296 return -1;
4297 PyErr_SetString(PyExc_ImportError,
4298 "from-import-* object has no __dict__ and no __all__");
4299 return -1;
4300 }
4301 all = PyMapping_Keys(dict);
4302 Py_DECREF(dict);
4303 if (all == NULL)
4304 return -1;
4305 skip_leading_underscores = 1;
4306 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004307
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004308 for (pos = 0, err = 0; ; pos++) {
4309 name = PySequence_GetItem(all, pos);
4310 if (name == NULL) {
4311 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4312 err = -1;
4313 else
4314 PyErr_Clear();
4315 break;
4316 }
4317 if (skip_leading_underscores &&
4318 PyUnicode_Check(name) &&
4319 PyUnicode_AS_UNICODE(name)[0] == '_')
4320 {
4321 Py_DECREF(name);
4322 continue;
4323 }
4324 value = PyObject_GetAttr(v, name);
4325 if (value == NULL)
4326 err = -1;
4327 else if (PyDict_CheckExact(locals))
4328 err = PyDict_SetItem(locals, name, value);
4329 else
4330 err = PyObject_SetItem(locals, name, value);
4331 Py_DECREF(name);
4332 Py_XDECREF(value);
4333 if (err != 0)
4334 break;
4335 }
4336 Py_DECREF(all);
4337 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004338}
4339
Guido van Rossumac7be682001-01-17 15:42:30 +00004340static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004341format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004342{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004343 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004345 if (!obj)
4346 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004348 obj_str = _PyUnicode_AsString(obj);
4349 if (!obj_str)
4350 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004352 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00004353}
Guido van Rossum950361c1997-01-24 13:49:28 +00004354
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004355static PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +00004356unicode_concatenate(PyObject *v, PyObject *w,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004357 PyFrameObject *f, unsigned char *next_instr)
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004358{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004359 /* This function implements 'variable += expr' when both arguments
4360 are (Unicode) strings. */
4361 Py_ssize_t v_len = PyUnicode_GET_SIZE(v);
4362 Py_ssize_t w_len = PyUnicode_GET_SIZE(w);
4363 Py_ssize_t new_len = v_len + w_len;
4364 if (new_len < 0) {
4365 PyErr_SetString(PyExc_OverflowError,
4366 "strings are too large to concat");
4367 return NULL;
4368 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00004369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004370 if (v->ob_refcnt == 2) {
4371 /* In the common case, there are 2 references to the value
4372 * stored in 'variable' when the += is performed: one on the
4373 * value stack (in 'v') and one still stored in the
4374 * 'variable'. We try to delete the variable now to reduce
4375 * the refcnt to 1.
4376 */
4377 switch (*next_instr) {
4378 case STORE_FAST:
4379 {
4380 int oparg = PEEKARG();
4381 PyObject **fastlocals = f->f_localsplus;
4382 if (GETLOCAL(oparg) == v)
4383 SETLOCAL(oparg, NULL);
4384 break;
4385 }
4386 case STORE_DEREF:
4387 {
4388 PyObject **freevars = (f->f_localsplus +
4389 f->f_code->co_nlocals);
4390 PyObject *c = freevars[PEEKARG()];
4391 if (PyCell_GET(c) == v)
4392 PyCell_Set(c, NULL);
4393 break;
4394 }
4395 case STORE_NAME:
4396 {
4397 PyObject *names = f->f_code->co_names;
4398 PyObject *name = GETITEM(names, PEEKARG());
4399 PyObject *locals = f->f_locals;
4400 if (PyDict_CheckExact(locals) &&
4401 PyDict_GetItem(locals, name) == v) {
4402 if (PyDict_DelItem(locals, name) != 0) {
4403 PyErr_Clear();
4404 }
4405 }
4406 break;
4407 }
4408 }
4409 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004411 if (v->ob_refcnt == 1 && !PyUnicode_CHECK_INTERNED(v)) {
4412 /* Now we own the last reference to 'v', so we can resize it
4413 * in-place.
4414 */
4415 if (PyUnicode_Resize(&v, new_len) != 0) {
4416 /* XXX if PyUnicode_Resize() fails, 'v' has been
4417 * deallocated so it cannot be put back into
4418 * 'variable'. The MemoryError is raised when there
4419 * is no value in 'variable', which might (very
4420 * remotely) be a cause of incompatibilities.
4421 */
4422 return NULL;
4423 }
4424 /* copy 'w' into the newly allocated area of 'v' */
4425 memcpy(PyUnicode_AS_UNICODE(v) + v_len,
4426 PyUnicode_AS_UNICODE(w), w_len*sizeof(Py_UNICODE));
4427 return v;
4428 }
4429 else {
4430 /* When in-place resizing is not an option. */
4431 w = PyUnicode_Concat(v, w);
4432 Py_DECREF(v);
4433 return w;
4434 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004435}
4436
Guido van Rossum950361c1997-01-24 13:49:28 +00004437#ifdef DYNAMIC_EXECUTION_PROFILE
4438
Skip Montanarof118cb12001-10-15 20:51:38 +00004439static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004440getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004441{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004442 int i;
4443 PyObject *l = PyList_New(256);
4444 if (l == NULL) return NULL;
4445 for (i = 0; i < 256; i++) {
4446 PyObject *x = PyLong_FromLong(a[i]);
4447 if (x == NULL) {
4448 Py_DECREF(l);
4449 return NULL;
4450 }
4451 PyList_SetItem(l, i, x);
4452 }
4453 for (i = 0; i < 256; i++)
4454 a[i] = 0;
4455 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004456}
4457
4458PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004459_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004460{
4461#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004462 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00004463#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004464 int i;
4465 PyObject *l = PyList_New(257);
4466 if (l == NULL) return NULL;
4467 for (i = 0; i < 257; i++) {
4468 PyObject *x = getarray(dxpairs[i]);
4469 if (x == NULL) {
4470 Py_DECREF(l);
4471 return NULL;
4472 }
4473 PyList_SetItem(l, i, x);
4474 }
4475 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004476#endif
4477}
4478
4479#endif