blob: c2c4e78f19e58220ebabdb9d5c2f02eac8dc4fb9 [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)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 filename = _PyUnicode_AsString(co->co_filename);
Tim Peters5ca576e2001-06-18 22:08:13 +00001217#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 why = WHY_NOT;
1220 err = 0;
1221 x = Py_None; /* Not a reference, just anything non-NULL */
1222 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00001223
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 if (throwflag) { /* support for generator.throw() */
1225 why = WHY_EXCEPTION;
1226 goto on_error;
1227 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001228
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001229 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001230#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001231 if (inst1 == 0) {
1232 /* Almost surely, the opcode executed a break
1233 or a continue, preventing inst1 from being set
1234 on the way out of the loop.
1235 */
1236 READ_TIMESTAMP(inst1);
1237 loop1 = inst1;
1238 }
1239 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
1240 intr0, intr1);
1241 ticked = 0;
1242 inst1 = 0;
1243 intr0 = 0;
1244 intr1 = 0;
1245 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001246#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1248 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 /* Do periodic things. Doing this every time through
1251 the loop would add too much overhead, so we do it
1252 only every Nth instruction. We also do it if
1253 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1254 event needs attention (e.g. a signal handler or
1255 async I/O handler); see Py_AddPendingCall() and
1256 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001257
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 if (_Py_atomic_load_relaxed(&eval_breaker)) {
1259 if (*next_instr == SETUP_FINALLY) {
1260 /* Make the last opcode before
1261 a try: finally: block uninterruptable. */
1262 goto fast_next_opcode;
1263 }
1264 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001265#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001266 ticked = 1;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001267#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) {
1269 if (Py_MakePendingCalls() < 0) {
1270 why = WHY_EXCEPTION;
1271 goto on_error;
1272 }
1273 }
1274 if (_Py_atomic_load_relaxed(&gil_drop_request)) {
Guido van Rossume59214e1994-08-30 08:01:59 +00001275#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001276 /* Give another thread a chance */
1277 if (PyThreadState_Swap(NULL) != tstate)
1278 Py_FatalError("ceval: tstate mix-up");
1279 drop_gil(tstate);
1280
1281 /* Other threads may run now */
1282
1283 take_gil(tstate);
1284 if (PyThreadState_Swap(tstate) != NULL)
1285 Py_FatalError("ceval: orphan tstate");
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001286#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 }
1288 /* Check for asynchronous exceptions. */
1289 if (tstate->async_exc != NULL) {
1290 x = tstate->async_exc;
1291 tstate->async_exc = NULL;
1292 UNSIGNAL_ASYNC_EXC();
1293 PyErr_SetNone(x);
1294 Py_DECREF(x);
1295 why = WHY_EXCEPTION;
1296 goto on_error;
1297 }
1298 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001299
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001300 fast_next_opcode:
1301 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001304
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001305 if (_Py_TracingPossible &&
1306 tstate->c_tracefunc != NULL && !tstate->tracing) {
1307 /* see maybe_call_line_trace
1308 for expository comments */
1309 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 err = maybe_call_line_trace(tstate->c_tracefunc,
1312 tstate->c_traceobj,
1313 f, &instr_lb, &instr_ub,
1314 &instr_prev);
1315 /* Reload possibly changed frame fields */
1316 JUMPTO(f->f_lasti);
1317 if (f->f_stacktop != NULL) {
1318 stack_pointer = f->f_stacktop;
1319 f->f_stacktop = NULL;
1320 }
1321 if (err) {
1322 /* trace function raised an exception */
1323 goto on_error;
1324 }
1325 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001329 opcode = NEXTOP();
1330 oparg = 0; /* allows oparg to be stored in a register because
1331 it doesn't have to be remembered across a full loop */
1332 if (HAS_ARG(opcode))
1333 oparg = NEXTARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001334 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001335#ifdef DYNAMIC_EXECUTION_PROFILE
1336#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001337 dxpairs[lastopcode][opcode]++;
1338 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001339#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001341#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001342
Guido van Rossum96a42c81992-01-12 02:29:51 +00001343#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001344 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001345
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 if (lltrace) {
1347 if (HAS_ARG(opcode)) {
1348 printf("%d: %d, %d\n",
1349 f->f_lasti, opcode, oparg);
1350 }
1351 else {
1352 printf("%d: %d\n",
1353 f->f_lasti, opcode);
1354 }
1355 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001356#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001358 /* Main switch on opcode */
1359 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001361 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 /* BEWARE!
1364 It is essential that any operation that fails sets either
1365 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1366 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001368 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001370 TARGET(NOP)
1371 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 TARGET(LOAD_FAST)
1374 x = GETLOCAL(oparg);
1375 if (x != NULL) {
1376 Py_INCREF(x);
1377 PUSH(x);
1378 FAST_DISPATCH();
1379 }
1380 format_exc_check_arg(PyExc_UnboundLocalError,
1381 UNBOUNDLOCAL_ERROR_MSG,
1382 PyTuple_GetItem(co->co_varnames, oparg));
1383 break;
Neil Schemenauer63543862002-02-17 19:10:14 +00001384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 TARGET(LOAD_CONST)
1386 x = GETITEM(consts, oparg);
1387 Py_INCREF(x);
1388 PUSH(x);
1389 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001391 PREDICTED_WITH_ARG(STORE_FAST);
1392 TARGET(STORE_FAST)
1393 v = POP();
1394 SETLOCAL(oparg, v);
1395 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 TARGET(POP_TOP)
1398 v = POP();
1399 Py_DECREF(v);
1400 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001402 TARGET(ROT_TWO)
1403 v = TOP();
1404 w = SECOND();
1405 SET_TOP(w);
1406 SET_SECOND(v);
1407 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001409 TARGET(ROT_THREE)
1410 v = TOP();
1411 w = SECOND();
1412 x = THIRD();
1413 SET_TOP(w);
1414 SET_SECOND(x);
1415 SET_THIRD(v);
1416 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001417
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001418 TARGET(ROT_FOUR)
1419 u = TOP();
1420 v = SECOND();
1421 w = THIRD();
1422 x = FOURTH();
1423 SET_TOP(v);
1424 SET_SECOND(w);
1425 SET_THIRD(x);
1426 SET_FOURTH(u);
1427 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001429 TARGET(DUP_TOP)
1430 v = TOP();
1431 Py_INCREF(v);
1432 PUSH(v);
1433 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001435 TARGET(DUP_TOPX)
1436 if (oparg == 2) {
1437 x = TOP();
1438 Py_INCREF(x);
1439 w = SECOND();
1440 Py_INCREF(w);
1441 STACKADJ(2);
1442 SET_TOP(x);
1443 SET_SECOND(w);
1444 FAST_DISPATCH();
1445 } else if (oparg == 3) {
1446 x = TOP();
1447 Py_INCREF(x);
1448 w = SECOND();
1449 Py_INCREF(w);
1450 v = THIRD();
1451 Py_INCREF(v);
1452 STACKADJ(3);
1453 SET_TOP(x);
1454 SET_SECOND(w);
1455 SET_THIRD(v);
1456 FAST_DISPATCH();
1457 }
1458 Py_FatalError("invalid argument to DUP_TOPX"
1459 " (bytecode corruption?)");
1460 /* Never returns, so don't bother to set why. */
1461 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001463 TARGET(UNARY_POSITIVE)
1464 v = TOP();
1465 x = PyNumber_Positive(v);
1466 Py_DECREF(v);
1467 SET_TOP(x);
1468 if (x != NULL) DISPATCH();
1469 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001471 TARGET(UNARY_NEGATIVE)
1472 v = TOP();
1473 x = PyNumber_Negative(v);
1474 Py_DECREF(v);
1475 SET_TOP(x);
1476 if (x != NULL) DISPATCH();
1477 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001478
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001479 TARGET(UNARY_NOT)
1480 v = TOP();
1481 err = PyObject_IsTrue(v);
1482 Py_DECREF(v);
1483 if (err == 0) {
1484 Py_INCREF(Py_True);
1485 SET_TOP(Py_True);
1486 DISPATCH();
1487 }
1488 else if (err > 0) {
1489 Py_INCREF(Py_False);
1490 SET_TOP(Py_False);
1491 err = 0;
1492 DISPATCH();
1493 }
1494 STACKADJ(-1);
1495 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001497 TARGET(UNARY_INVERT)
1498 v = TOP();
1499 x = PyNumber_Invert(v);
1500 Py_DECREF(v);
1501 SET_TOP(x);
1502 if (x != NULL) DISPATCH();
1503 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001505 TARGET(BINARY_POWER)
1506 w = POP();
1507 v = TOP();
1508 x = PyNumber_Power(v, w, Py_None);
1509 Py_DECREF(v);
1510 Py_DECREF(w);
1511 SET_TOP(x);
1512 if (x != NULL) DISPATCH();
1513 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001515 TARGET(BINARY_MULTIPLY)
1516 w = POP();
1517 v = TOP();
1518 x = PyNumber_Multiply(v, w);
1519 Py_DECREF(v);
1520 Py_DECREF(w);
1521 SET_TOP(x);
1522 if (x != NULL) DISPATCH();
1523 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001524
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001525 TARGET(BINARY_TRUE_DIVIDE)
1526 w = POP();
1527 v = TOP();
1528 x = PyNumber_TrueDivide(v, w);
1529 Py_DECREF(v);
1530 Py_DECREF(w);
1531 SET_TOP(x);
1532 if (x != NULL) DISPATCH();
1533 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001535 TARGET(BINARY_FLOOR_DIVIDE)
1536 w = POP();
1537 v = TOP();
1538 x = PyNumber_FloorDivide(v, w);
1539 Py_DECREF(v);
1540 Py_DECREF(w);
1541 SET_TOP(x);
1542 if (x != NULL) DISPATCH();
1543 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001545 TARGET(BINARY_MODULO)
1546 w = POP();
1547 v = TOP();
1548 if (PyUnicode_CheckExact(v))
1549 x = PyUnicode_Format(v, w);
1550 else
1551 x = PyNumber_Remainder(v, w);
1552 Py_DECREF(v);
1553 Py_DECREF(w);
1554 SET_TOP(x);
1555 if (x != NULL) DISPATCH();
1556 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001557
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001558 TARGET(BINARY_ADD)
1559 w = POP();
1560 v = TOP();
1561 if (PyUnicode_CheckExact(v) &&
1562 PyUnicode_CheckExact(w)) {
1563 x = unicode_concatenate(v, w, f, next_instr);
1564 /* unicode_concatenate consumed the ref to v */
1565 goto skip_decref_vx;
1566 }
1567 else {
1568 x = PyNumber_Add(v, w);
1569 }
1570 Py_DECREF(v);
1571 skip_decref_vx:
1572 Py_DECREF(w);
1573 SET_TOP(x);
1574 if (x != NULL) DISPATCH();
1575 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001576
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001577 TARGET(BINARY_SUBTRACT)
1578 w = POP();
1579 v = TOP();
1580 x = PyNumber_Subtract(v, w);
1581 Py_DECREF(v);
1582 Py_DECREF(w);
1583 SET_TOP(x);
1584 if (x != NULL) DISPATCH();
1585 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001587 TARGET(BINARY_SUBSCR)
1588 w = POP();
1589 v = TOP();
1590 x = PyObject_GetItem(v, w);
1591 Py_DECREF(v);
1592 Py_DECREF(w);
1593 SET_TOP(x);
1594 if (x != NULL) DISPATCH();
1595 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001597 TARGET(BINARY_LSHIFT)
1598 w = POP();
1599 v = TOP();
1600 x = PyNumber_Lshift(v, w);
1601 Py_DECREF(v);
1602 Py_DECREF(w);
1603 SET_TOP(x);
1604 if (x != NULL) DISPATCH();
1605 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001606
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001607 TARGET(BINARY_RSHIFT)
1608 w = POP();
1609 v = TOP();
1610 x = PyNumber_Rshift(v, w);
1611 Py_DECREF(v);
1612 Py_DECREF(w);
1613 SET_TOP(x);
1614 if (x != NULL) DISPATCH();
1615 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001616
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 TARGET(BINARY_AND)
1618 w = POP();
1619 v = TOP();
1620 x = PyNumber_And(v, w);
1621 Py_DECREF(v);
1622 Py_DECREF(w);
1623 SET_TOP(x);
1624 if (x != NULL) DISPATCH();
1625 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001626
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001627 TARGET(BINARY_XOR)
1628 w = POP();
1629 v = TOP();
1630 x = PyNumber_Xor(v, w);
1631 Py_DECREF(v);
1632 Py_DECREF(w);
1633 SET_TOP(x);
1634 if (x != NULL) DISPATCH();
1635 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001637 TARGET(BINARY_OR)
1638 w = POP();
1639 v = TOP();
1640 x = PyNumber_Or(v, w);
1641 Py_DECREF(v);
1642 Py_DECREF(w);
1643 SET_TOP(x);
1644 if (x != NULL) DISPATCH();
1645 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001646
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001647 TARGET(LIST_APPEND)
1648 w = POP();
1649 v = PEEK(oparg);
1650 err = PyList_Append(v, w);
1651 Py_DECREF(w);
1652 if (err == 0) {
1653 PREDICT(JUMP_ABSOLUTE);
1654 DISPATCH();
1655 }
1656 break;
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001657
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001658 TARGET(SET_ADD)
1659 w = POP();
1660 v = stack_pointer[-oparg];
1661 err = PySet_Add(v, w);
1662 Py_DECREF(w);
1663 if (err == 0) {
1664 PREDICT(JUMP_ABSOLUTE);
1665 DISPATCH();
1666 }
1667 break;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 TARGET(INPLACE_POWER)
1670 w = POP();
1671 v = TOP();
1672 x = PyNumber_InPlacePower(v, w, Py_None);
1673 Py_DECREF(v);
1674 Py_DECREF(w);
1675 SET_TOP(x);
1676 if (x != NULL) DISPATCH();
1677 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001679 TARGET(INPLACE_MULTIPLY)
1680 w = POP();
1681 v = TOP();
1682 x = PyNumber_InPlaceMultiply(v, w);
1683 Py_DECREF(v);
1684 Py_DECREF(w);
1685 SET_TOP(x);
1686 if (x != NULL) DISPATCH();
1687 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001688
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001689 TARGET(INPLACE_TRUE_DIVIDE)
1690 w = POP();
1691 v = TOP();
1692 x = PyNumber_InPlaceTrueDivide(v, w);
1693 Py_DECREF(v);
1694 Py_DECREF(w);
1695 SET_TOP(x);
1696 if (x != NULL) DISPATCH();
1697 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001698
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001699 TARGET(INPLACE_FLOOR_DIVIDE)
1700 w = POP();
1701 v = TOP();
1702 x = PyNumber_InPlaceFloorDivide(v, w);
1703 Py_DECREF(v);
1704 Py_DECREF(w);
1705 SET_TOP(x);
1706 if (x != NULL) DISPATCH();
1707 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001708
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001709 TARGET(INPLACE_MODULO)
1710 w = POP();
1711 v = TOP();
1712 x = PyNumber_InPlaceRemainder(v, w);
1713 Py_DECREF(v);
1714 Py_DECREF(w);
1715 SET_TOP(x);
1716 if (x != NULL) DISPATCH();
1717 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001718
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001719 TARGET(INPLACE_ADD)
1720 w = POP();
1721 v = TOP();
1722 if (PyUnicode_CheckExact(v) &&
1723 PyUnicode_CheckExact(w)) {
1724 x = unicode_concatenate(v, w, f, next_instr);
1725 /* unicode_concatenate consumed the ref to v */
1726 goto skip_decref_v;
1727 }
1728 else {
1729 x = PyNumber_InPlaceAdd(v, w);
1730 }
1731 Py_DECREF(v);
1732 skip_decref_v:
1733 Py_DECREF(w);
1734 SET_TOP(x);
1735 if (x != NULL) DISPATCH();
1736 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001737
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001738 TARGET(INPLACE_SUBTRACT)
1739 w = POP();
1740 v = TOP();
1741 x = PyNumber_InPlaceSubtract(v, w);
1742 Py_DECREF(v);
1743 Py_DECREF(w);
1744 SET_TOP(x);
1745 if (x != NULL) DISPATCH();
1746 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001747
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001748 TARGET(INPLACE_LSHIFT)
1749 w = POP();
1750 v = TOP();
1751 x = PyNumber_InPlaceLshift(v, w);
1752 Py_DECREF(v);
1753 Py_DECREF(w);
1754 SET_TOP(x);
1755 if (x != NULL) DISPATCH();
1756 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001757
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001758 TARGET(INPLACE_RSHIFT)
1759 w = POP();
1760 v = TOP();
1761 x = PyNumber_InPlaceRshift(v, w);
1762 Py_DECREF(v);
1763 Py_DECREF(w);
1764 SET_TOP(x);
1765 if (x != NULL) DISPATCH();
1766 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001767
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001768 TARGET(INPLACE_AND)
1769 w = POP();
1770 v = TOP();
1771 x = PyNumber_InPlaceAnd(v, w);
1772 Py_DECREF(v);
1773 Py_DECREF(w);
1774 SET_TOP(x);
1775 if (x != NULL) DISPATCH();
1776 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001777
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001778 TARGET(INPLACE_XOR)
1779 w = POP();
1780 v = TOP();
1781 x = PyNumber_InPlaceXor(v, w);
1782 Py_DECREF(v);
1783 Py_DECREF(w);
1784 SET_TOP(x);
1785 if (x != NULL) DISPATCH();
1786 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001787
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001788 TARGET(INPLACE_OR)
1789 w = POP();
1790 v = TOP();
1791 x = PyNumber_InPlaceOr(v, w);
1792 Py_DECREF(v);
1793 Py_DECREF(w);
1794 SET_TOP(x);
1795 if (x != NULL) DISPATCH();
1796 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001797
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001798 TARGET(STORE_SUBSCR)
1799 w = TOP();
1800 v = SECOND();
1801 u = THIRD();
1802 STACKADJ(-3);
1803 /* v[w] = u */
1804 err = PyObject_SetItem(v, w, u);
1805 Py_DECREF(u);
1806 Py_DECREF(v);
1807 Py_DECREF(w);
1808 if (err == 0) DISPATCH();
1809 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001810
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001811 TARGET(DELETE_SUBSCR)
1812 w = TOP();
1813 v = SECOND();
1814 STACKADJ(-2);
1815 /* del v[w] */
1816 err = PyObject_DelItem(v, w);
1817 Py_DECREF(v);
1818 Py_DECREF(w);
1819 if (err == 0) DISPATCH();
1820 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001821
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001822 TARGET(PRINT_EXPR)
1823 v = POP();
1824 w = PySys_GetObject("displayhook");
1825 if (w == NULL) {
1826 PyErr_SetString(PyExc_RuntimeError,
1827 "lost sys.displayhook");
1828 err = -1;
1829 x = NULL;
1830 }
1831 if (err == 0) {
1832 x = PyTuple_Pack(1, v);
1833 if (x == NULL)
1834 err = -1;
1835 }
1836 if (err == 0) {
1837 w = PyEval_CallObject(w, x);
1838 Py_XDECREF(w);
1839 if (w == NULL)
1840 err = -1;
1841 }
1842 Py_DECREF(v);
1843 Py_XDECREF(x);
1844 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001845
Thomas Wouters434d0822000-08-24 20:11:32 +00001846#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001847 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001848#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001849 TARGET(RAISE_VARARGS)
1850 v = w = NULL;
1851 switch (oparg) {
1852 case 2:
1853 v = POP(); /* cause */
1854 case 1:
1855 w = POP(); /* exc */
1856 case 0: /* Fallthrough */
1857 why = do_raise(w, v);
1858 break;
1859 default:
1860 PyErr_SetString(PyExc_SystemError,
1861 "bad RAISE_VARARGS oparg");
1862 why = WHY_EXCEPTION;
1863 break;
1864 }
1865 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001866
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001867 TARGET(STORE_LOCALS)
1868 x = POP();
1869 v = f->f_locals;
1870 Py_XDECREF(v);
1871 f->f_locals = x;
1872 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001873
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001874 TARGET(RETURN_VALUE)
1875 retval = POP();
1876 why = WHY_RETURN;
1877 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001878
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001879 TARGET(YIELD_VALUE)
1880 retval = POP();
1881 f->f_stacktop = stack_pointer;
1882 why = WHY_YIELD;
1883 /* Put aside the current exception state and restore
1884 that of the calling frame. This only serves when
1885 "yield" is used inside an except handler. */
1886 SWAP_EXC_STATE();
1887 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001888
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001889 TARGET(POP_EXCEPT)
1890 {
1891 PyTryBlock *b = PyFrame_BlockPop(f);
1892 if (b->b_type != EXCEPT_HANDLER) {
1893 PyErr_SetString(PyExc_SystemError,
1894 "popped block is not an except handler");
1895 why = WHY_EXCEPTION;
1896 break;
1897 }
1898 UNWIND_EXCEPT_HANDLER(b);
1899 }
1900 DISPATCH();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001901
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001902 TARGET(POP_BLOCK)
1903 {
1904 PyTryBlock *b = PyFrame_BlockPop(f);
1905 UNWIND_BLOCK(b);
1906 }
1907 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001908
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001909 PREDICTED(END_FINALLY);
1910 TARGET(END_FINALLY)
1911 v = POP();
1912 if (PyLong_Check(v)) {
1913 why = (enum why_code) PyLong_AS_LONG(v);
1914 assert(why != WHY_YIELD);
1915 if (why == WHY_RETURN ||
1916 why == WHY_CONTINUE)
1917 retval = POP();
1918 if (why == WHY_SILENCED) {
1919 /* An exception was silenced by 'with', we must
1920 manually unwind the EXCEPT_HANDLER block which was
1921 created when the exception was caught, otherwise
1922 the stack will be in an inconsistent state. */
1923 PyTryBlock *b = PyFrame_BlockPop(f);
1924 assert(b->b_type == EXCEPT_HANDLER);
1925 UNWIND_EXCEPT_HANDLER(b);
1926 why = WHY_NOT;
1927 }
1928 }
1929 else if (PyExceptionClass_Check(v)) {
1930 w = POP();
1931 u = POP();
1932 PyErr_Restore(v, w, u);
1933 why = WHY_RERAISE;
1934 break;
1935 }
1936 else if (v != Py_None) {
1937 PyErr_SetString(PyExc_SystemError,
1938 "'finally' pops bad exception");
1939 why = WHY_EXCEPTION;
1940 }
1941 Py_DECREF(v);
1942 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001943
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001944 TARGET(LOAD_BUILD_CLASS)
1945 x = PyDict_GetItemString(f->f_builtins,
1946 "__build_class__");
1947 if (x == NULL) {
1948 PyErr_SetString(PyExc_ImportError,
1949 "__build_class__ not found");
1950 break;
1951 }
1952 Py_INCREF(x);
1953 PUSH(x);
1954 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001955
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001956 TARGET(STORE_NAME)
1957 w = GETITEM(names, oparg);
1958 v = POP();
1959 if ((x = f->f_locals) != NULL) {
1960 if (PyDict_CheckExact(x))
1961 err = PyDict_SetItem(x, w, v);
1962 else
1963 err = PyObject_SetItem(x, w, v);
1964 Py_DECREF(v);
1965 if (err == 0) DISPATCH();
1966 break;
1967 }
1968 PyErr_Format(PyExc_SystemError,
1969 "no locals found when storing %R", w);
1970 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001971
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001972 TARGET(DELETE_NAME)
1973 w = GETITEM(names, oparg);
1974 if ((x = f->f_locals) != NULL) {
1975 if ((err = PyObject_DelItem(x, w)) != 0)
1976 format_exc_check_arg(PyExc_NameError,
1977 NAME_ERROR_MSG,
1978 w);
1979 break;
1980 }
1981 PyErr_Format(PyExc_SystemError,
1982 "no locals when deleting %R", w);
1983 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001984
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001985 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
1986 TARGET(UNPACK_SEQUENCE)
1987 v = POP();
1988 if (PyTuple_CheckExact(v) &&
1989 PyTuple_GET_SIZE(v) == oparg) {
1990 PyObject **items = \
1991 ((PyTupleObject *)v)->ob_item;
1992 while (oparg--) {
1993 w = items[oparg];
1994 Py_INCREF(w);
1995 PUSH(w);
1996 }
1997 Py_DECREF(v);
1998 DISPATCH();
1999 } else if (PyList_CheckExact(v) &&
2000 PyList_GET_SIZE(v) == oparg) {
2001 PyObject **items = \
2002 ((PyListObject *)v)->ob_item;
2003 while (oparg--) {
2004 w = items[oparg];
2005 Py_INCREF(w);
2006 PUSH(w);
2007 }
2008 } else if (unpack_iterable(v, oparg, -1,
2009 stack_pointer + oparg)) {
2010 STACKADJ(oparg);
2011 } else {
2012 /* unpack_iterable() raised an exception */
2013 why = WHY_EXCEPTION;
2014 }
2015 Py_DECREF(v);
2016 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002017
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002018 TARGET(UNPACK_EX)
2019 {
2020 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2021 v = POP();
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002022
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002023 if (unpack_iterable(v, oparg & 0xFF, oparg >> 8,
2024 stack_pointer + totalargs)) {
2025 stack_pointer += totalargs;
2026 } else {
2027 why = WHY_EXCEPTION;
2028 }
2029 Py_DECREF(v);
2030 break;
2031 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002032
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002033 TARGET(STORE_ATTR)
2034 w = GETITEM(names, oparg);
2035 v = TOP();
2036 u = SECOND();
2037 STACKADJ(-2);
2038 err = PyObject_SetAttr(v, w, u); /* v.w = u */
2039 Py_DECREF(v);
2040 Py_DECREF(u);
2041 if (err == 0) DISPATCH();
2042 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002043
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002044 TARGET(DELETE_ATTR)
2045 w = GETITEM(names, oparg);
2046 v = POP();
2047 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
2048 /* del v.w */
2049 Py_DECREF(v);
2050 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002052 TARGET(STORE_GLOBAL)
2053 w = GETITEM(names, oparg);
2054 v = POP();
2055 err = PyDict_SetItem(f->f_globals, w, v);
2056 Py_DECREF(v);
2057 if (err == 0) DISPATCH();
2058 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002059
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002060 TARGET(DELETE_GLOBAL)
2061 w = GETITEM(names, oparg);
2062 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
2063 format_exc_check_arg(
2064 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
2065 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002066
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002067 TARGET(LOAD_NAME)
2068 w = GETITEM(names, oparg);
2069 if ((v = f->f_locals) == NULL) {
2070 PyErr_Format(PyExc_SystemError,
2071 "no locals when loading %R", w);
2072 why = WHY_EXCEPTION;
2073 break;
2074 }
2075 if (PyDict_CheckExact(v)) {
2076 x = PyDict_GetItem(v, w);
2077 Py_XINCREF(x);
2078 }
2079 else {
2080 x = PyObject_GetItem(v, w);
2081 if (x == NULL && PyErr_Occurred()) {
2082 if (!PyErr_ExceptionMatches(
2083 PyExc_KeyError))
2084 break;
2085 PyErr_Clear();
2086 }
2087 }
2088 if (x == NULL) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002089 x = PyDict_GetItem(f->f_globals, w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002090 if (x == NULL) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002091 x = PyDict_GetItem(f->f_builtins, w);
2092 if (x == NULL) {
2093 format_exc_check_arg(
2094 PyExc_NameError,
2095 NAME_ERROR_MSG, w);
2096 break;
2097 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002098 }
2099 Py_INCREF(x);
2100 }
2101 PUSH(x);
2102 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002103
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002104 TARGET(LOAD_GLOBAL)
2105 w = GETITEM(names, oparg);
2106 if (PyUnicode_CheckExact(w)) {
2107 /* Inline the PyDict_GetItem() calls.
2108 WARNING: this is an extreme speed hack.
2109 Do not try this at home. */
2110 long hash = ((PyUnicodeObject *)w)->hash;
2111 if (hash != -1) {
2112 PyDictObject *d;
2113 PyDictEntry *e;
2114 d = (PyDictObject *)(f->f_globals);
2115 e = d->ma_lookup(d, w, hash);
2116 if (e == NULL) {
2117 x = NULL;
2118 break;
2119 }
2120 x = e->me_value;
2121 if (x != NULL) {
2122 Py_INCREF(x);
2123 PUSH(x);
2124 DISPATCH();
2125 }
2126 d = (PyDictObject *)(f->f_builtins);
2127 e = d->ma_lookup(d, w, hash);
2128 if (e == NULL) {
2129 x = NULL;
2130 break;
2131 }
2132 x = e->me_value;
2133 if (x != NULL) {
2134 Py_INCREF(x);
2135 PUSH(x);
2136 DISPATCH();
2137 }
2138 goto load_global_error;
2139 }
2140 }
2141 /* This is the un-inlined version of the code above */
2142 x = PyDict_GetItem(f->f_globals, w);
2143 if (x == NULL) {
2144 x = PyDict_GetItem(f->f_builtins, w);
2145 if (x == NULL) {
2146 load_global_error:
2147 format_exc_check_arg(
2148 PyExc_NameError,
2149 GLOBAL_NAME_ERROR_MSG, w);
2150 break;
2151 }
2152 }
2153 Py_INCREF(x);
2154 PUSH(x);
2155 DISPATCH();
Guido van Rossum681d79a1995-07-18 14:51:37 +00002156
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002157 TARGET(DELETE_FAST)
2158 x = GETLOCAL(oparg);
2159 if (x != NULL) {
2160 SETLOCAL(oparg, NULL);
2161 DISPATCH();
2162 }
2163 format_exc_check_arg(
2164 PyExc_UnboundLocalError,
2165 UNBOUNDLOCAL_ERROR_MSG,
2166 PyTuple_GetItem(co->co_varnames, oparg)
2167 );
2168 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002169
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002170 TARGET(LOAD_CLOSURE)
2171 x = freevars[oparg];
2172 Py_INCREF(x);
2173 PUSH(x);
2174 if (x != NULL) DISPATCH();
2175 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002176
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002177 TARGET(LOAD_DEREF)
2178 x = freevars[oparg];
2179 w = PyCell_Get(x);
2180 if (w != NULL) {
2181 PUSH(w);
2182 DISPATCH();
2183 }
2184 err = -1;
2185 /* Don't stomp existing exception */
2186 if (PyErr_Occurred())
2187 break;
2188 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
2189 v = PyTuple_GET_ITEM(co->co_cellvars,
Stefan Krahb7e10102010-06-23 18:42:39 +00002190 oparg);
2191 format_exc_check_arg(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002192 PyExc_UnboundLocalError,
2193 UNBOUNDLOCAL_ERROR_MSG,
2194 v);
2195 } else {
2196 v = PyTuple_GET_ITEM(co->co_freevars, oparg -
2197 PyTuple_GET_SIZE(co->co_cellvars));
2198 format_exc_check_arg(PyExc_NameError,
2199 UNBOUNDFREE_ERROR_MSG, v);
2200 }
2201 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002202
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002203 TARGET(STORE_DEREF)
2204 w = POP();
2205 x = freevars[oparg];
2206 PyCell_Set(x, w);
2207 Py_DECREF(w);
2208 DISPATCH();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002209
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002210 TARGET(BUILD_TUPLE)
2211 x = PyTuple_New(oparg);
2212 if (x != NULL) {
2213 for (; --oparg >= 0;) {
2214 w = POP();
2215 PyTuple_SET_ITEM(x, oparg, w);
2216 }
2217 PUSH(x);
2218 DISPATCH();
2219 }
2220 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002221
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002222 TARGET(BUILD_LIST)
2223 x = PyList_New(oparg);
2224 if (x != NULL) {
2225 for (; --oparg >= 0;) {
2226 w = POP();
2227 PyList_SET_ITEM(x, oparg, w);
2228 }
2229 PUSH(x);
2230 DISPATCH();
2231 }
2232 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002233
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002234 TARGET(BUILD_SET)
2235 x = PySet_New(NULL);
2236 if (x != NULL) {
2237 for (; --oparg >= 0;) {
2238 w = POP();
2239 if (err == 0)
2240 err = PySet_Add(x, w);
2241 Py_DECREF(w);
2242 }
2243 if (err != 0) {
2244 Py_DECREF(x);
2245 break;
2246 }
2247 PUSH(x);
2248 DISPATCH();
2249 }
2250 break;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002251
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002252 TARGET(BUILD_MAP)
2253 x = _PyDict_NewPresized((Py_ssize_t)oparg);
2254 PUSH(x);
2255 if (x != NULL) DISPATCH();
2256 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002257
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002258 TARGET(STORE_MAP)
2259 w = TOP(); /* key */
2260 u = SECOND(); /* value */
2261 v = THIRD(); /* dict */
2262 STACKADJ(-2);
2263 assert (PyDict_CheckExact(v));
2264 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2265 Py_DECREF(u);
2266 Py_DECREF(w);
2267 if (err == 0) DISPATCH();
2268 break;
Christian Heimes99170a52007-12-19 02:07:34 +00002269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002270 TARGET(MAP_ADD)
2271 w = TOP(); /* key */
2272 u = SECOND(); /* value */
2273 STACKADJ(-2);
2274 v = stack_pointer[-oparg]; /* dict */
2275 assert (PyDict_CheckExact(v));
2276 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2277 Py_DECREF(u);
2278 Py_DECREF(w);
2279 if (err == 0) {
2280 PREDICT(JUMP_ABSOLUTE);
2281 DISPATCH();
2282 }
2283 break;
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002285 TARGET(LOAD_ATTR)
2286 w = GETITEM(names, oparg);
2287 v = TOP();
2288 x = PyObject_GetAttr(v, w);
2289 Py_DECREF(v);
2290 SET_TOP(x);
2291 if (x != NULL) DISPATCH();
2292 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002294 TARGET(COMPARE_OP)
2295 w = POP();
2296 v = TOP();
2297 x = cmp_outcome(oparg, v, w);
2298 Py_DECREF(v);
2299 Py_DECREF(w);
2300 SET_TOP(x);
2301 if (x == NULL) break;
2302 PREDICT(POP_JUMP_IF_FALSE);
2303 PREDICT(POP_JUMP_IF_TRUE);
2304 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002306 TARGET(IMPORT_NAME)
2307 w = GETITEM(names, oparg);
2308 x = PyDict_GetItemString(f->f_builtins, "__import__");
2309 if (x == NULL) {
2310 PyErr_SetString(PyExc_ImportError,
2311 "__import__ not found");
2312 break;
2313 }
2314 Py_INCREF(x);
2315 v = POP();
2316 u = TOP();
2317 if (PyLong_AsLong(u) != -1 || PyErr_Occurred())
2318 w = PyTuple_Pack(5,
2319 w,
2320 f->f_globals,
2321 f->f_locals == NULL ?
2322 Py_None : f->f_locals,
2323 v,
2324 u);
2325 else
2326 w = PyTuple_Pack(4,
2327 w,
2328 f->f_globals,
2329 f->f_locals == NULL ?
2330 Py_None : f->f_locals,
2331 v);
2332 Py_DECREF(v);
2333 Py_DECREF(u);
2334 if (w == NULL) {
2335 u = POP();
2336 Py_DECREF(x);
2337 x = NULL;
2338 break;
2339 }
2340 READ_TIMESTAMP(intr0);
2341 v = x;
2342 x = PyEval_CallObject(v, w);
2343 Py_DECREF(v);
2344 READ_TIMESTAMP(intr1);
2345 Py_DECREF(w);
2346 SET_TOP(x);
2347 if (x != NULL) DISPATCH();
2348 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002350 TARGET(IMPORT_STAR)
2351 v = POP();
2352 PyFrame_FastToLocals(f);
2353 if ((x = f->f_locals) == NULL) {
2354 PyErr_SetString(PyExc_SystemError,
2355 "no locals found during 'import *'");
2356 break;
2357 }
2358 READ_TIMESTAMP(intr0);
2359 err = import_all_from(x, v);
2360 READ_TIMESTAMP(intr1);
2361 PyFrame_LocalsToFast(f, 0);
2362 Py_DECREF(v);
2363 if (err == 0) DISPATCH();
2364 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002366 TARGET(IMPORT_FROM)
2367 w = GETITEM(names, oparg);
2368 v = TOP();
2369 READ_TIMESTAMP(intr0);
2370 x = import_from(v, w);
2371 READ_TIMESTAMP(intr1);
2372 PUSH(x);
2373 if (x != NULL) DISPATCH();
2374 break;
Thomas Wouters52152252000-08-17 22:55:00 +00002375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002376 TARGET(JUMP_FORWARD)
2377 JUMPBY(oparg);
2378 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002380 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
2381 TARGET(POP_JUMP_IF_FALSE)
2382 w = POP();
2383 if (w == Py_True) {
2384 Py_DECREF(w);
2385 FAST_DISPATCH();
2386 }
2387 if (w == Py_False) {
2388 Py_DECREF(w);
2389 JUMPTO(oparg);
2390 FAST_DISPATCH();
2391 }
2392 err = PyObject_IsTrue(w);
2393 Py_DECREF(w);
2394 if (err > 0)
2395 err = 0;
2396 else if (err == 0)
2397 JUMPTO(oparg);
2398 else
2399 break;
2400 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002402 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
2403 TARGET(POP_JUMP_IF_TRUE)
2404 w = POP();
2405 if (w == Py_False) {
2406 Py_DECREF(w);
2407 FAST_DISPATCH();
2408 }
2409 if (w == Py_True) {
2410 Py_DECREF(w);
2411 JUMPTO(oparg);
2412 FAST_DISPATCH();
2413 }
2414 err = PyObject_IsTrue(w);
2415 Py_DECREF(w);
2416 if (err > 0) {
2417 err = 0;
2418 JUMPTO(oparg);
2419 }
2420 else if (err == 0)
2421 ;
2422 else
2423 break;
2424 DISPATCH();
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002425
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002426 TARGET(JUMP_IF_FALSE_OR_POP)
2427 w = TOP();
2428 if (w == Py_True) {
2429 STACKADJ(-1);
2430 Py_DECREF(w);
2431 FAST_DISPATCH();
2432 }
2433 if (w == Py_False) {
2434 JUMPTO(oparg);
2435 FAST_DISPATCH();
2436 }
2437 err = PyObject_IsTrue(w);
2438 if (err > 0) {
2439 STACKADJ(-1);
2440 Py_DECREF(w);
2441 err = 0;
2442 }
2443 else if (err == 0)
2444 JUMPTO(oparg);
2445 else
2446 break;
2447 DISPATCH();
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002449 TARGET(JUMP_IF_TRUE_OR_POP)
2450 w = TOP();
2451 if (w == Py_False) {
2452 STACKADJ(-1);
2453 Py_DECREF(w);
2454 FAST_DISPATCH();
2455 }
2456 if (w == Py_True) {
2457 JUMPTO(oparg);
2458 FAST_DISPATCH();
2459 }
2460 err = PyObject_IsTrue(w);
2461 if (err > 0) {
2462 err = 0;
2463 JUMPTO(oparg);
2464 }
2465 else if (err == 0) {
2466 STACKADJ(-1);
2467 Py_DECREF(w);
2468 }
2469 else
2470 break;
2471 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002472
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002473 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
2474 TARGET(JUMP_ABSOLUTE)
2475 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002476#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002477 /* Enabling this path speeds-up all while and for-loops by bypassing
2478 the per-loop checks for signals. By default, this should be turned-off
2479 because it prevents detection of a control-break in tight loops like
2480 "while 1: pass". Compile with this option turned-on when you need
2481 the speed-up and do not need break checking inside tight loops (ones
2482 that contain only instructions ending with FAST_DISPATCH).
2483 */
2484 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002485#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002486 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002487#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002489 TARGET(GET_ITER)
2490 /* before: [obj]; after [getiter(obj)] */
2491 v = TOP();
2492 x = PyObject_GetIter(v);
2493 Py_DECREF(v);
2494 if (x != NULL) {
2495 SET_TOP(x);
2496 PREDICT(FOR_ITER);
2497 DISPATCH();
2498 }
2499 STACKADJ(-1);
2500 break;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002501
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002502 PREDICTED_WITH_ARG(FOR_ITER);
2503 TARGET(FOR_ITER)
2504 /* before: [iter]; after: [iter, iter()] *or* [] */
2505 v = TOP();
2506 x = (*v->ob_type->tp_iternext)(v);
2507 if (x != NULL) {
2508 PUSH(x);
2509 PREDICT(STORE_FAST);
2510 PREDICT(UNPACK_SEQUENCE);
2511 DISPATCH();
2512 }
2513 if (PyErr_Occurred()) {
2514 if (!PyErr_ExceptionMatches(
2515 PyExc_StopIteration))
2516 break;
2517 PyErr_Clear();
2518 }
2519 /* iterator ended normally */
2520 x = v = POP();
2521 Py_DECREF(v);
2522 JUMPBY(oparg);
2523 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002524
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002525 TARGET(BREAK_LOOP)
2526 why = WHY_BREAK;
2527 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002528
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002529 TARGET(CONTINUE_LOOP)
2530 retval = PyLong_FromLong(oparg);
2531 if (!retval) {
2532 x = NULL;
2533 break;
2534 }
2535 why = WHY_CONTINUE;
2536 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002537
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002538 TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
2539 TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
2540 TARGET(SETUP_FINALLY)
2541 _setup_finally:
2542 /* NOTE: If you add any new block-setup opcodes that
2543 are not try/except/finally handlers, you may need
2544 to update the PyGen_NeedsFinalizing() function.
2545 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002546
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002547 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2548 STACK_LEVEL());
2549 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002551 TARGET(SETUP_WITH)
2552 {
2553 static PyObject *exit, *enter;
2554 w = TOP();
2555 x = special_lookup(w, "__exit__", &exit);
2556 if (!x)
2557 break;
2558 SET_TOP(x);
2559 u = special_lookup(w, "__enter__", &enter);
2560 Py_DECREF(w);
2561 if (!u) {
2562 x = NULL;
2563 break;
2564 }
2565 x = PyObject_CallFunctionObjArgs(u, NULL);
2566 Py_DECREF(u);
2567 if (!x)
2568 break;
2569 /* Setup the finally block before pushing the result
2570 of __enter__ on the stack. */
2571 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2572 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002573
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002574 PUSH(x);
2575 DISPATCH();
2576 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002577
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002578 TARGET(WITH_CLEANUP)
2579 {
2580 /* At the top of the stack are 1-3 values indicating
2581 how/why we entered the finally clause:
2582 - TOP = None
2583 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2584 - TOP = WHY_*; no retval below it
2585 - (TOP, SECOND, THIRD) = exc_info()
2586 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2587 Below them is EXIT, the context.__exit__ bound method.
2588 In the last case, we must call
2589 EXIT(TOP, SECOND, THIRD)
2590 otherwise we must call
2591 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002592
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002593 In the first two cases, we remove EXIT from the
2594 stack, leaving the rest in the same order. In the
2595 third case, we shift the bottom 3 values of the
2596 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002598 In addition, if the stack represents an exception,
2599 *and* the function call returns a 'true' value, we
2600 push WHY_SILENCED onto the stack. END_FINALLY will
2601 then not re-raise the exception. (But non-local
2602 gotos should still be resumed.)
2603 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002605 PyObject *exit_func;
2606 u = TOP();
2607 if (u == Py_None) {
2608 (void)POP();
2609 exit_func = TOP();
2610 SET_TOP(u);
2611 v = w = Py_None;
2612 }
2613 else if (PyLong_Check(u)) {
2614 (void)POP();
2615 switch(PyLong_AsLong(u)) {
2616 case WHY_RETURN:
2617 case WHY_CONTINUE:
2618 /* Retval in TOP. */
2619 exit_func = SECOND();
2620 SET_SECOND(TOP());
2621 SET_TOP(u);
2622 break;
2623 default:
2624 exit_func = TOP();
2625 SET_TOP(u);
2626 break;
2627 }
2628 u = v = w = Py_None;
2629 }
2630 else {
2631 PyObject *tp, *exc, *tb;
2632 PyTryBlock *block;
2633 v = SECOND();
2634 w = THIRD();
2635 tp = FOURTH();
2636 exc = PEEK(5);
2637 tb = PEEK(6);
2638 exit_func = PEEK(7);
2639 SET_VALUE(7, tb);
2640 SET_VALUE(6, exc);
2641 SET_VALUE(5, tp);
2642 /* UNWIND_EXCEPT_HANDLER will pop this off. */
2643 SET_FOURTH(NULL);
2644 /* We just shifted the stack down, so we have
2645 to tell the except handler block that the
2646 values are lower than it expects. */
2647 block = &f->f_blockstack[f->f_iblock - 1];
2648 assert(block->b_type == EXCEPT_HANDLER);
2649 block->b_level--;
2650 }
2651 /* XXX Not the fastest way to call it... */
2652 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2653 NULL);
2654 Py_DECREF(exit_func);
2655 if (x == NULL)
2656 break; /* Go to error exit */
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002657
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002658 if (u != Py_None)
2659 err = PyObject_IsTrue(x);
2660 else
2661 err = 0;
2662 Py_DECREF(x);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002663
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002664 if (err < 0)
2665 break; /* Go to error exit */
2666 else if (err > 0) {
2667 err = 0;
2668 /* There was an exception and a True return */
2669 PUSH(PyLong_FromLong((long) WHY_SILENCED));
2670 }
2671 PREDICT(END_FINALLY);
2672 break;
2673 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002674
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002675 TARGET(CALL_FUNCTION)
2676 {
2677 PyObject **sp;
2678 PCALL(PCALL_ALL);
2679 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002680#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002681 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002682#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002683 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002684#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002685 stack_pointer = sp;
2686 PUSH(x);
2687 if (x != NULL)
2688 DISPATCH();
2689 break;
2690 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002691
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002692 TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw)
2693 TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw)
2694 TARGET(CALL_FUNCTION_VAR_KW)
2695 _call_function_var_kw:
2696 {
2697 int na = oparg & 0xff;
2698 int nk = (oparg>>8) & 0xff;
2699 int flags = (opcode - CALL_FUNCTION) & 3;
2700 int n = na + 2 * nk;
2701 PyObject **pfunc, *func, **sp;
2702 PCALL(PCALL_ALL);
2703 if (flags & CALL_FLAG_VAR)
2704 n++;
2705 if (flags & CALL_FLAG_KW)
2706 n++;
2707 pfunc = stack_pointer - n - 1;
2708 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002709
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002710 if (PyMethod_Check(func)
Stefan Krahb7e10102010-06-23 18:42:39 +00002711 && PyMethod_GET_SELF(func) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002712 PyObject *self = PyMethod_GET_SELF(func);
2713 Py_INCREF(self);
2714 func = PyMethod_GET_FUNCTION(func);
2715 Py_INCREF(func);
2716 Py_DECREF(*pfunc);
2717 *pfunc = self;
2718 na++;
2719 n++;
2720 } else
2721 Py_INCREF(func);
2722 sp = stack_pointer;
2723 READ_TIMESTAMP(intr0);
2724 x = ext_do_call(func, &sp, flags, na, nk);
2725 READ_TIMESTAMP(intr1);
2726 stack_pointer = sp;
2727 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002728
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002729 while (stack_pointer > pfunc) {
2730 w = POP();
2731 Py_DECREF(w);
2732 }
2733 PUSH(x);
2734 if (x != NULL)
2735 DISPATCH();
2736 break;
2737 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002738
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002739 TARGET_WITH_IMPL(MAKE_CLOSURE, _make_function)
2740 TARGET(MAKE_FUNCTION)
2741 _make_function:
2742 {
2743 int posdefaults = oparg & 0xff;
2744 int kwdefaults = (oparg>>8) & 0xff;
2745 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002746
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002747 v = POP(); /* code object */
2748 x = PyFunction_New(v, f->f_globals);
2749 Py_DECREF(v);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002750
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002751 if (x != NULL && opcode == MAKE_CLOSURE) {
2752 v = POP();
2753 if (PyFunction_SetClosure(x, v) != 0) {
2754 /* Can't happen unless bytecode is corrupt. */
2755 why = WHY_EXCEPTION;
2756 }
2757 Py_DECREF(v);
2758 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002759
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002760 if (x != NULL && num_annotations > 0) {
2761 Py_ssize_t name_ix;
2762 u = POP(); /* names of args with annotations */
2763 v = PyDict_New();
2764 if (v == NULL) {
2765 Py_DECREF(x);
2766 x = NULL;
2767 break;
2768 }
2769 name_ix = PyTuple_Size(u);
2770 assert(num_annotations == name_ix+1);
2771 while (name_ix > 0) {
2772 --name_ix;
2773 t = PyTuple_GET_ITEM(u, name_ix);
2774 w = POP();
2775 /* XXX(nnorwitz): check for errors */
2776 PyDict_SetItem(v, t, w);
2777 Py_DECREF(w);
2778 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002779
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002780 if (PyFunction_SetAnnotations(x, v) != 0) {
2781 /* Can't happen unless
2782 PyFunction_SetAnnotations changes. */
2783 why = WHY_EXCEPTION;
2784 }
2785 Py_DECREF(v);
2786 Py_DECREF(u);
2787 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002788
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002789 /* XXX Maybe this should be a separate opcode? */
2790 if (x != NULL && posdefaults > 0) {
2791 v = PyTuple_New(posdefaults);
2792 if (v == NULL) {
2793 Py_DECREF(x);
2794 x = NULL;
2795 break;
2796 }
2797 while (--posdefaults >= 0) {
2798 w = POP();
2799 PyTuple_SET_ITEM(v, posdefaults, w);
2800 }
2801 if (PyFunction_SetDefaults(x, v) != 0) {
2802 /* Can't happen unless
2803 PyFunction_SetDefaults changes. */
2804 why = WHY_EXCEPTION;
2805 }
2806 Py_DECREF(v);
2807 }
2808 if (x != NULL && kwdefaults > 0) {
2809 v = PyDict_New();
2810 if (v == NULL) {
2811 Py_DECREF(x);
2812 x = NULL;
2813 break;
2814 }
2815 while (--kwdefaults >= 0) {
2816 w = POP(); /* default value */
2817 u = POP(); /* kw only arg name */
2818 /* XXX(nnorwitz): check for errors */
2819 PyDict_SetItem(v, u, w);
2820 Py_DECREF(w);
2821 Py_DECREF(u);
2822 }
2823 if (PyFunction_SetKwDefaults(x, v) != 0) {
2824 /* Can't happen unless
2825 PyFunction_SetKwDefaults changes. */
2826 why = WHY_EXCEPTION;
2827 }
2828 Py_DECREF(v);
2829 }
2830 PUSH(x);
2831 break;
2832 }
Guido van Rossum8861b741996-07-30 16:49:37 +00002833
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002834 TARGET(BUILD_SLICE)
2835 if (oparg == 3)
2836 w = POP();
2837 else
2838 w = NULL;
2839 v = POP();
2840 u = TOP();
2841 x = PySlice_New(u, v, w);
2842 Py_DECREF(u);
2843 Py_DECREF(v);
2844 Py_XDECREF(w);
2845 SET_TOP(x);
2846 if (x != NULL) DISPATCH();
2847 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002848
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002849 TARGET(EXTENDED_ARG)
2850 opcode = NEXTOP();
2851 oparg = oparg<<16 | NEXTARG();
2852 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002853
Antoine Pitrou042b1282010-08-13 21:15:58 +00002854#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002855 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00002856#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002857 default:
2858 fprintf(stderr,
2859 "XXX lineno: %d, opcode: %d\n",
2860 PyFrame_GetLineNumber(f),
2861 opcode);
2862 PyErr_SetString(PyExc_SystemError, "unknown opcode");
2863 why = WHY_EXCEPTION;
2864 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002865
2866#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002867 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002868#endif
2869
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002870 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00002871
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002872 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002873
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002874 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002875
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002876 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002877
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002878 if (why == WHY_NOT) {
2879 if (err == 0 && x != NULL) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002880#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002881 /* This check is expensive! */
2882 if (PyErr_Occurred())
2883 fprintf(stderr,
2884 "XXX undetected error\n");
2885 else {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002886#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002887 READ_TIMESTAMP(loop1);
2888 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002889#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002890 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002891#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002892 }
2893 why = WHY_EXCEPTION;
2894 x = Py_None;
2895 err = 0;
2896 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002897
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002898 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002899
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002900 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
2901 if (!PyErr_Occurred()) {
2902 PyErr_SetString(PyExc_SystemError,
2903 "error return without exception set");
2904 why = WHY_EXCEPTION;
2905 }
2906 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002907#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002908 else {
2909 /* This check is expensive! */
2910 if (PyErr_Occurred()) {
2911 char buf[128];
2912 sprintf(buf, "Stack unwind with exception "
2913 "set and why=%d", why);
2914 Py_FatalError(buf);
2915 }
2916 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002917#endif
2918
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002919 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002921 if (why == WHY_EXCEPTION) {
2922 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002923
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002924 if (tstate->c_tracefunc != NULL)
2925 call_exc_trace(tstate->c_tracefunc,
2926 tstate->c_traceobj, f);
2927 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002928
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002929 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002930
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002931 if (why == WHY_RERAISE)
2932 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +00002933
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002934 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002935
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002936fast_block_end:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002937 while (why != WHY_NOT && f->f_iblock > 0) {
2938 /* Peek at the current block. */
2939 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002940
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002941 assert(why != WHY_YIELD);
2942 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2943 why = WHY_NOT;
2944 JUMPTO(PyLong_AS_LONG(retval));
2945 Py_DECREF(retval);
2946 break;
2947 }
2948 /* Now we have to pop the block. */
2949 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002950
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002951 if (b->b_type == EXCEPT_HANDLER) {
2952 UNWIND_EXCEPT_HANDLER(b);
2953 continue;
2954 }
2955 UNWIND_BLOCK(b);
2956 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2957 why = WHY_NOT;
2958 JUMPTO(b->b_handler);
2959 break;
2960 }
2961 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
2962 || b->b_type == SETUP_FINALLY)) {
2963 PyObject *exc, *val, *tb;
2964 int handler = b->b_handler;
2965 /* Beware, this invalidates all b->b_* fields */
2966 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
2967 PUSH(tstate->exc_traceback);
2968 PUSH(tstate->exc_value);
2969 if (tstate->exc_type != NULL) {
2970 PUSH(tstate->exc_type);
2971 }
2972 else {
2973 Py_INCREF(Py_None);
2974 PUSH(Py_None);
2975 }
2976 PyErr_Fetch(&exc, &val, &tb);
2977 /* Make the raw exception data
2978 available to the handler,
2979 so a program can emulate the
2980 Python main loop. */
2981 PyErr_NormalizeException(
2982 &exc, &val, &tb);
2983 PyException_SetTraceback(val, tb);
2984 Py_INCREF(exc);
2985 tstate->exc_type = exc;
2986 Py_INCREF(val);
2987 tstate->exc_value = val;
2988 tstate->exc_traceback = tb;
2989 if (tb == NULL)
2990 tb = Py_None;
2991 Py_INCREF(tb);
2992 PUSH(tb);
2993 PUSH(val);
2994 PUSH(exc);
2995 why = WHY_NOT;
2996 JUMPTO(handler);
2997 break;
2998 }
2999 if (b->b_type == SETUP_FINALLY) {
3000 if (why & (WHY_RETURN | WHY_CONTINUE))
3001 PUSH(retval);
3002 PUSH(PyLong_FromLong((long)why));
3003 why = WHY_NOT;
3004 JUMPTO(b->b_handler);
3005 break;
3006 }
3007 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003008
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003009 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00003010
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003011 if (why != WHY_NOT)
3012 break;
3013 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00003014
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003015 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003016
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003017 assert(why != WHY_YIELD);
3018 /* Pop remaining stack entries. */
3019 while (!EMPTY()) {
3020 v = POP();
3021 Py_XDECREF(v);
3022 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003023
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003024 if (why != WHY_RETURN)
3025 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003026
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003027fast_yield:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003028 if (tstate->use_tracing) {
3029 if (tstate->c_tracefunc) {
3030 if (why == WHY_RETURN || why == WHY_YIELD) {
3031 if (call_trace(tstate->c_tracefunc,
3032 tstate->c_traceobj, f,
3033 PyTrace_RETURN, retval)) {
3034 Py_XDECREF(retval);
3035 retval = NULL;
3036 why = WHY_EXCEPTION;
3037 }
3038 }
3039 else if (why == WHY_EXCEPTION) {
3040 call_trace_protected(tstate->c_tracefunc,
3041 tstate->c_traceobj, f,
3042 PyTrace_RETURN, NULL);
3043 }
3044 }
3045 if (tstate->c_profilefunc) {
3046 if (why == WHY_EXCEPTION)
3047 call_trace_protected(tstate->c_profilefunc,
3048 tstate->c_profileobj, f,
3049 PyTrace_RETURN, NULL);
3050 else if (call_trace(tstate->c_profilefunc,
3051 tstate->c_profileobj, f,
3052 PyTrace_RETURN, retval)) {
3053 Py_XDECREF(retval);
3054 retval = NULL;
3055 why = WHY_EXCEPTION;
3056 }
3057 }
3058 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003059
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003060 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003061exit_eval_frame:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003062 Py_LeaveRecursiveCall();
3063 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003065 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00003066}
3067
Guido van Rossumc2e20742006-02-27 22:32:47 +00003068/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003069 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003070 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003071
Tim Peters6d6c1a32001-08-02 04:15:00 +00003072PyObject *
3073PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003074 PyObject **args, int argcount, PyObject **kws, int kwcount,
3075 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00003076{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003077 register PyFrameObject *f;
3078 register PyObject *retval = NULL;
3079 register PyObject **fastlocals, **freevars;
3080 PyThreadState *tstate = PyThreadState_GET();
3081 PyObject *x, *u;
3082 int total_args = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00003083
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003084 if (globals == NULL) {
3085 PyErr_SetString(PyExc_SystemError,
3086 "PyEval_EvalCodeEx: NULL globals");
3087 return NULL;
3088 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003090 assert(tstate != NULL);
3091 assert(globals != NULL);
3092 f = PyFrame_New(tstate, co, globals, locals);
3093 if (f == NULL)
3094 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003095
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003096 fastlocals = f->f_localsplus;
3097 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003098
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003099 if (total_args || co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
3100 int i;
3101 int n = argcount;
3102 PyObject *kwdict = NULL;
3103 if (co->co_flags & CO_VARKEYWORDS) {
3104 kwdict = PyDict_New();
3105 if (kwdict == NULL)
3106 goto fail;
3107 i = total_args;
3108 if (co->co_flags & CO_VARARGS)
3109 i++;
3110 SETLOCAL(i, kwdict);
3111 }
3112 if (argcount > co->co_argcount) {
3113 if (!(co->co_flags & CO_VARARGS)) {
3114 PyErr_Format(PyExc_TypeError,
3115 "%U() takes %s %d "
Benjamin Peterson88968ad2010-06-25 19:30:21 +00003116 "positional argument%s (%d given)",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003117 co->co_name,
3118 defcount ? "at most" : "exactly",
Benjamin Peterson88968ad2010-06-25 19:30:21 +00003119 co->co_argcount,
3120 co->co_argcount == 1 ? "" : "s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003121 argcount + kwcount);
3122 goto fail;
3123 }
3124 n = co->co_argcount;
3125 }
3126 for (i = 0; i < n; i++) {
3127 x = args[i];
3128 Py_INCREF(x);
3129 SETLOCAL(i, x);
3130 }
3131 if (co->co_flags & CO_VARARGS) {
3132 u = PyTuple_New(argcount - n);
3133 if (u == NULL)
3134 goto fail;
3135 SETLOCAL(total_args, u);
3136 for (i = n; i < argcount; i++) {
3137 x = args[i];
3138 Py_INCREF(x);
3139 PyTuple_SET_ITEM(u, i-n, x);
3140 }
3141 }
3142 for (i = 0; i < kwcount; i++) {
3143 PyObject **co_varnames;
3144 PyObject *keyword = kws[2*i];
3145 PyObject *value = kws[2*i + 1];
3146 int j;
3147 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3148 PyErr_Format(PyExc_TypeError,
3149 "%U() keywords must be strings",
3150 co->co_name);
3151 goto fail;
3152 }
3153 /* Speed hack: do raw pointer compares. As names are
3154 normally interned this should almost always hit. */
3155 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3156 for (j = 0; j < total_args; j++) {
3157 PyObject *nm = co_varnames[j];
3158 if (nm == keyword)
3159 goto kw_found;
3160 }
3161 /* Slow fallback, just in case */
3162 for (j = 0; j < total_args; j++) {
3163 PyObject *nm = co_varnames[j];
3164 int cmp = PyObject_RichCompareBool(
3165 keyword, nm, Py_EQ);
3166 if (cmp > 0)
3167 goto kw_found;
3168 else if (cmp < 0)
3169 goto fail;
3170 }
3171 if (j >= total_args && kwdict == NULL) {
3172 PyErr_Format(PyExc_TypeError,
3173 "%U() got an unexpected "
3174 "keyword argument '%S'",
3175 co->co_name,
3176 keyword);
3177 goto fail;
3178 }
3179 PyDict_SetItem(kwdict, keyword, value);
3180 continue;
3181 kw_found:
3182 if (GETLOCAL(j) != NULL) {
3183 PyErr_Format(PyExc_TypeError,
3184 "%U() got multiple "
3185 "values for keyword "
3186 "argument '%S'",
3187 co->co_name,
3188 keyword);
3189 goto fail;
3190 }
3191 Py_INCREF(value);
3192 SETLOCAL(j, value);
3193 }
3194 if (co->co_kwonlyargcount > 0) {
3195 for (i = co->co_argcount; i < total_args; i++) {
3196 PyObject *name;
3197 if (GETLOCAL(i) != NULL)
3198 continue;
3199 name = PyTuple_GET_ITEM(co->co_varnames, i);
3200 if (kwdefs != NULL) {
3201 PyObject *def = PyDict_GetItem(kwdefs, name);
3202 if (def) {
3203 Py_INCREF(def);
3204 SETLOCAL(i, def);
3205 continue;
3206 }
3207 }
3208 PyErr_Format(PyExc_TypeError,
3209 "%U() needs keyword-only argument %S",
3210 co->co_name, name);
3211 goto fail;
3212 }
3213 }
3214 if (argcount < co->co_argcount) {
3215 int m = co->co_argcount - defcount;
3216 for (i = argcount; i < m; i++) {
3217 if (GETLOCAL(i) == NULL) {
3218 int j, given = 0;
3219 for (j = 0; j < co->co_argcount; j++)
3220 if (GETLOCAL(j))
3221 given++;
3222 PyErr_Format(PyExc_TypeError,
3223 "%U() takes %s %d "
3224 "argument%s "
3225 "(%d given)",
3226 co->co_name,
3227 ((co->co_flags & CO_VARARGS) ||
3228 defcount) ? "at least"
3229 : "exactly",
3230 m, m == 1 ? "" : "s", given);
3231 goto fail;
3232 }
3233 }
3234 if (n > m)
3235 i = n - m;
3236 else
3237 i = 0;
3238 for (; i < defcount; i++) {
3239 if (GETLOCAL(m+i) == NULL) {
3240 PyObject *def = defs[i];
3241 Py_INCREF(def);
3242 SETLOCAL(m+i, def);
3243 }
3244 }
3245 }
3246 }
3247 else if (argcount > 0 || kwcount > 0) {
3248 PyErr_Format(PyExc_TypeError,
3249 "%U() takes no arguments (%d given)",
3250 co->co_name,
3251 argcount + kwcount);
3252 goto fail;
3253 }
3254 /* Allocate and initialize storage for cell vars, and copy free
3255 vars into frame. This isn't too efficient right now. */
3256 if (PyTuple_GET_SIZE(co->co_cellvars)) {
3257 int i, j, nargs, found;
3258 Py_UNICODE *cellname, *argname;
3259 PyObject *c;
Tim Peters5ca576e2001-06-18 22:08:13 +00003260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003261 nargs = total_args;
3262 if (co->co_flags & CO_VARARGS)
3263 nargs++;
3264 if (co->co_flags & CO_VARKEYWORDS)
3265 nargs++;
Tim Peters5ca576e2001-06-18 22:08:13 +00003266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003267 /* Initialize each cell var, taking into account
3268 cell vars that are initialized from arguments.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003270 Should arrange for the compiler to put cellvars
3271 that are arguments at the beginning of the cellvars
3272 list so that we can march over it more efficiently?
3273 */
3274 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
3275 cellname = PyUnicode_AS_UNICODE(
3276 PyTuple_GET_ITEM(co->co_cellvars, i));
3277 found = 0;
3278 for (j = 0; j < nargs; j++) {
3279 argname = PyUnicode_AS_UNICODE(
3280 PyTuple_GET_ITEM(co->co_varnames, j));
3281 if (Py_UNICODE_strcmp(cellname, argname) == 0) {
3282 c = PyCell_New(GETLOCAL(j));
3283 if (c == NULL)
3284 goto fail;
3285 GETLOCAL(co->co_nlocals + i) = c;
3286 found = 1;
3287 break;
3288 }
3289 }
3290 if (found == 0) {
3291 c = PyCell_New(NULL);
3292 if (c == NULL)
3293 goto fail;
3294 SETLOCAL(co->co_nlocals + i, c);
3295 }
3296 }
3297 }
3298 if (PyTuple_GET_SIZE(co->co_freevars)) {
3299 int i;
3300 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3301 PyObject *o = PyTuple_GET_ITEM(closure, i);
3302 Py_INCREF(o);
3303 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
3304 }
3305 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003307 if (co->co_flags & CO_GENERATOR) {
3308 /* Don't need to keep the reference to f_back, it will be set
3309 * when the generator is resumed. */
3310 Py_XDECREF(f->f_back);
3311 f->f_back = NULL;
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003313 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003315 /* Create a new generator that owns the ready to run frame
3316 * and return that as the value. */
3317 return PyGen_New(f);
3318 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003320 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003321
Thomas Woutersce272b62007-09-19 21:19:28 +00003322fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003324 /* decref'ing the frame can cause __del__ methods to get invoked,
3325 which can call back into Python. While we're done with the
3326 current Python frame (f), the associated C stack is still in use,
3327 so recursion_depth must be boosted for the duration.
3328 */
3329 assert(tstate != NULL);
3330 ++tstate->recursion_depth;
3331 Py_DECREF(f);
3332 --tstate->recursion_depth;
3333 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003334}
3335
3336
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003337static PyObject *
3338special_lookup(PyObject *o, char *meth, PyObject **cache)
3339{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003340 PyObject *res;
3341 res = _PyObject_LookupSpecial(o, meth, cache);
3342 if (res == NULL && !PyErr_Occurred()) {
3343 PyErr_SetObject(PyExc_AttributeError, *cache);
3344 return NULL;
3345 }
3346 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003347}
3348
3349
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003350/* Logic for the raise statement (too complicated for inlining).
3351 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00003352static enum why_code
Collin Winter828f04a2007-08-31 00:04:24 +00003353do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003354{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003355 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003357 if (exc == NULL) {
3358 /* Reraise */
3359 PyThreadState *tstate = PyThreadState_GET();
3360 PyObject *tb;
3361 type = tstate->exc_type;
3362 value = tstate->exc_value;
3363 tb = tstate->exc_traceback;
3364 if (type == Py_None) {
3365 PyErr_SetString(PyExc_RuntimeError,
3366 "No active exception to reraise");
3367 return WHY_EXCEPTION;
3368 }
3369 Py_XINCREF(type);
3370 Py_XINCREF(value);
3371 Py_XINCREF(tb);
3372 PyErr_Restore(type, value, tb);
3373 return WHY_RERAISE;
3374 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003376 /* We support the following forms of raise:
3377 raise
Collin Winter828f04a2007-08-31 00:04:24 +00003378 raise <instance>
3379 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003381 if (PyExceptionClass_Check(exc)) {
3382 type = exc;
3383 value = PyObject_CallObject(exc, NULL);
3384 if (value == NULL)
3385 goto raise_error;
3386 }
3387 else if (PyExceptionInstance_Check(exc)) {
3388 value = exc;
3389 type = PyExceptionInstance_Class(exc);
3390 Py_INCREF(type);
3391 }
3392 else {
3393 /* Not something you can raise. You get an exception
3394 anyway, just not what you specified :-) */
3395 Py_DECREF(exc);
3396 PyErr_SetString(PyExc_TypeError,
3397 "exceptions must derive from BaseException");
3398 goto raise_error;
3399 }
Collin Winter828f04a2007-08-31 00:04:24 +00003400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003401 if (cause) {
3402 PyObject *fixed_cause;
3403 if (PyExceptionClass_Check(cause)) {
3404 fixed_cause = PyObject_CallObject(cause, NULL);
3405 if (fixed_cause == NULL)
3406 goto raise_error;
3407 Py_DECREF(cause);
3408 }
3409 else if (PyExceptionInstance_Check(cause)) {
3410 fixed_cause = cause;
3411 }
3412 else {
3413 PyErr_SetString(PyExc_TypeError,
3414 "exception causes must derive from "
3415 "BaseException");
3416 goto raise_error;
3417 }
3418 PyException_SetCause(value, fixed_cause);
3419 }
Collin Winter828f04a2007-08-31 00:04:24 +00003420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003421 PyErr_SetObject(type, value);
3422 /* PyErr_SetObject incref's its arguments */
3423 Py_XDECREF(value);
3424 Py_XDECREF(type);
3425 return WHY_EXCEPTION;
Collin Winter828f04a2007-08-31 00:04:24 +00003426
3427raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003428 Py_XDECREF(value);
3429 Py_XDECREF(type);
3430 Py_XDECREF(cause);
3431 return WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003432}
3433
Tim Petersd6d010b2001-06-21 02:49:55 +00003434/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00003435 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003436
Guido van Rossum0368b722007-05-11 16:50:42 +00003437 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
3438 with a variable target.
3439*/
Tim Petersd6d010b2001-06-21 02:49:55 +00003440
Barry Warsawe42b18f1997-08-25 22:13:04 +00003441static int
Guido van Rossum0368b722007-05-11 16:50:42 +00003442unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003443{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003444 int i = 0, j = 0;
3445 Py_ssize_t ll = 0;
3446 PyObject *it; /* iter(v) */
3447 PyObject *w;
3448 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00003449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003450 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00003451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003452 it = PyObject_GetIter(v);
3453 if (it == NULL)
3454 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00003455
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003456 for (; i < argcnt; i++) {
3457 w = PyIter_Next(it);
3458 if (w == NULL) {
3459 /* Iterator done, via error or exhaustion. */
3460 if (!PyErr_Occurred()) {
3461 PyErr_Format(PyExc_ValueError,
3462 "need more than %d value%s to unpack",
3463 i, i == 1 ? "" : "s");
3464 }
3465 goto Error;
3466 }
3467 *--sp = w;
3468 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003470 if (argcntafter == -1) {
3471 /* We better have exhausted the iterator now. */
3472 w = PyIter_Next(it);
3473 if (w == NULL) {
3474 if (PyErr_Occurred())
3475 goto Error;
3476 Py_DECREF(it);
3477 return 1;
3478 }
3479 Py_DECREF(w);
Georg Brandl0310a832010-07-10 10:32:36 +00003480 PyErr_Format(PyExc_ValueError, "too many values to unpack "
3481 "(expected %d)", argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003482 goto Error;
3483 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003485 l = PySequence_List(it);
3486 if (l == NULL)
3487 goto Error;
3488 *--sp = l;
3489 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00003490
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003491 ll = PyList_GET_SIZE(l);
3492 if (ll < argcntafter) {
3493 PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack",
3494 argcnt + ll);
3495 goto Error;
3496 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003497
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003498 /* Pop the "after-variable" args off the list. */
3499 for (j = argcntafter; j > 0; j--, i++) {
3500 *--sp = PyList_GET_ITEM(l, ll - j);
3501 }
3502 /* Resize the list. */
3503 Py_SIZE(l) = ll - argcntafter;
3504 Py_DECREF(it);
3505 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00003506
Tim Petersd6d010b2001-06-21 02:49:55 +00003507Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003508 for (; i > 0; i--, sp++)
3509 Py_DECREF(*sp);
3510 Py_XDECREF(it);
3511 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003512}
3513
3514
Guido van Rossum96a42c81992-01-12 02:29:51 +00003515#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003516static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003517prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003518{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003519 printf("%s ", str);
3520 if (PyObject_Print(v, stdout, 0) != 0)
3521 PyErr_Clear(); /* Don't know what else to do */
3522 printf("\n");
3523 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003524}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003525#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003526
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003527static void
Fred Drake5755ce62001-06-27 19:19:46 +00003528call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003529{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003530 PyObject *type, *value, *traceback, *arg;
3531 int err;
3532 PyErr_Fetch(&type, &value, &traceback);
3533 if (value == NULL) {
3534 value = Py_None;
3535 Py_INCREF(value);
3536 }
3537 arg = PyTuple_Pack(3, type, value, traceback);
3538 if (arg == NULL) {
3539 PyErr_Restore(type, value, traceback);
3540 return;
3541 }
3542 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
3543 Py_DECREF(arg);
3544 if (err == 0)
3545 PyErr_Restore(type, value, traceback);
3546 else {
3547 Py_XDECREF(type);
3548 Py_XDECREF(value);
3549 Py_XDECREF(traceback);
3550 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003551}
3552
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003553static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003554call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003555 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003556{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003557 PyObject *type, *value, *traceback;
3558 int err;
3559 PyErr_Fetch(&type, &value, &traceback);
3560 err = call_trace(func, obj, frame, what, arg);
3561 if (err == 0)
3562 {
3563 PyErr_Restore(type, value, traceback);
3564 return 0;
3565 }
3566 else {
3567 Py_XDECREF(type);
3568 Py_XDECREF(value);
3569 Py_XDECREF(traceback);
3570 return -1;
3571 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003572}
3573
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003574static int
Fred Drake5755ce62001-06-27 19:19:46 +00003575call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003576 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003577{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003578 register PyThreadState *tstate = frame->f_tstate;
3579 int result;
3580 if (tstate->tracing)
3581 return 0;
3582 tstate->tracing++;
3583 tstate->use_tracing = 0;
3584 result = func(obj, frame, what, arg);
3585 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3586 || (tstate->c_profilefunc != NULL));
3587 tstate->tracing--;
3588 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003589}
3590
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003591PyObject *
3592_PyEval_CallTracing(PyObject *func, PyObject *args)
3593{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003594 PyFrameObject *frame = PyEval_GetFrame();
3595 PyThreadState *tstate = frame->f_tstate;
3596 int save_tracing = tstate->tracing;
3597 int save_use_tracing = tstate->use_tracing;
3598 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003599
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003600 tstate->tracing = 0;
3601 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3602 || (tstate->c_profilefunc != NULL));
3603 result = PyObject_Call(func, args, NULL);
3604 tstate->tracing = save_tracing;
3605 tstate->use_tracing = save_use_tracing;
3606 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003607}
3608
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003609/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00003610static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003611maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003612 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3613 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003614{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003615 int result = 0;
3616 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003617
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003618 /* If the last instruction executed isn't in the current
3619 instruction window, reset the window.
3620 */
3621 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
3622 PyAddrPair bounds;
3623 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3624 &bounds);
3625 *instr_lb = bounds.ap_lower;
3626 *instr_ub = bounds.ap_upper;
3627 }
3628 /* If the last instruction falls at the start of a line or if
3629 it represents a jump backwards, update the frame's line
3630 number and call the trace function. */
3631 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
3632 frame->f_lineno = line;
3633 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
3634 }
3635 *instr_prev = frame->f_lasti;
3636 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003637}
3638
Fred Drake5755ce62001-06-27 19:19:46 +00003639void
3640PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003641{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003642 PyThreadState *tstate = PyThreadState_GET();
3643 PyObject *temp = tstate->c_profileobj;
3644 Py_XINCREF(arg);
3645 tstate->c_profilefunc = NULL;
3646 tstate->c_profileobj = NULL;
3647 /* Must make sure that tracing is not ignored if 'temp' is freed */
3648 tstate->use_tracing = tstate->c_tracefunc != NULL;
3649 Py_XDECREF(temp);
3650 tstate->c_profilefunc = func;
3651 tstate->c_profileobj = arg;
3652 /* Flag that tracing or profiling is turned on */
3653 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003654}
3655
3656void
3657PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3658{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003659 PyThreadState *tstate = PyThreadState_GET();
3660 PyObject *temp = tstate->c_traceobj;
3661 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
3662 Py_XINCREF(arg);
3663 tstate->c_tracefunc = NULL;
3664 tstate->c_traceobj = NULL;
3665 /* Must make sure that profiling is not ignored if 'temp' is freed */
3666 tstate->use_tracing = tstate->c_profilefunc != NULL;
3667 Py_XDECREF(temp);
3668 tstate->c_tracefunc = func;
3669 tstate->c_traceobj = arg;
3670 /* Flag that tracing or profiling is turned on */
3671 tstate->use_tracing = ((func != NULL)
3672 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003673}
3674
Guido van Rossumb209a111997-04-29 18:18:01 +00003675PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003676PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003677{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003678 PyFrameObject *current_frame = PyEval_GetFrame();
3679 if (current_frame == NULL)
3680 return PyThreadState_GET()->interp->builtins;
3681 else
3682 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003683}
3684
Guido van Rossumb209a111997-04-29 18:18:01 +00003685PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003686PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003687{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003688 PyFrameObject *current_frame = PyEval_GetFrame();
3689 if (current_frame == NULL)
3690 return NULL;
3691 PyFrame_FastToLocals(current_frame);
3692 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00003693}
3694
Guido van Rossumb209a111997-04-29 18:18:01 +00003695PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003696PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003697{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003698 PyFrameObject *current_frame = PyEval_GetFrame();
3699 if (current_frame == NULL)
3700 return NULL;
3701 else
3702 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003703}
3704
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003705PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003706PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003707{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003708 PyThreadState *tstate = PyThreadState_GET();
3709 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003710}
3711
Guido van Rossum6135a871995-01-09 17:53:26 +00003712int
Tim Peters5ba58662001-07-16 02:29:45 +00003713PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003714{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003715 PyFrameObject *current_frame = PyEval_GetFrame();
3716 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003717
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003718 if (current_frame != NULL) {
3719 const int codeflags = current_frame->f_code->co_flags;
3720 const int compilerflags = codeflags & PyCF_MASK;
3721 if (compilerflags) {
3722 result = 1;
3723 cf->cf_flags |= compilerflags;
3724 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003725#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003726 if (codeflags & CO_GENERATOR_ALLOWED) {
3727 result = 1;
3728 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3729 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003730#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003731 }
3732 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003733}
3734
Guido van Rossum3f5da241990-12-20 15:06:42 +00003735
Guido van Rossum681d79a1995-07-18 14:51:37 +00003736/* External interface to call any callable object.
Antoine Pitrou8689a102010-04-01 16:53:15 +00003737 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00003738
Guido van Rossumb209a111997-04-29 18:18:01 +00003739PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003740PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003741{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003742 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003743
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003744 if (arg == NULL) {
3745 arg = PyTuple_New(0);
3746 if (arg == NULL)
3747 return NULL;
3748 }
3749 else if (!PyTuple_Check(arg)) {
3750 PyErr_SetString(PyExc_TypeError,
3751 "argument list must be a tuple");
3752 return NULL;
3753 }
3754 else
3755 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003756
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003757 if (kw != NULL && !PyDict_Check(kw)) {
3758 PyErr_SetString(PyExc_TypeError,
3759 "keyword list must be a dictionary");
3760 Py_DECREF(arg);
3761 return NULL;
3762 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00003763
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003764 result = PyObject_Call(func, arg, kw);
3765 Py_DECREF(arg);
3766 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00003767}
3768
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003769const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003770PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003771{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003772 if (PyMethod_Check(func))
3773 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
3774 else if (PyFunction_Check(func))
3775 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
3776 else if (PyCFunction_Check(func))
3777 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3778 else
3779 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00003780}
3781
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003782const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003783PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003784{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003785 if (PyMethod_Check(func))
3786 return "()";
3787 else if (PyFunction_Check(func))
3788 return "()";
3789 else if (PyCFunction_Check(func))
3790 return "()";
3791 else
3792 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00003793}
3794
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003795static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003796err_args(PyObject *func, int flags, int nargs)
3797{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003798 if (flags & METH_NOARGS)
3799 PyErr_Format(PyExc_TypeError,
3800 "%.200s() takes no arguments (%d given)",
3801 ((PyCFunctionObject *)func)->m_ml->ml_name,
3802 nargs);
3803 else
3804 PyErr_Format(PyExc_TypeError,
3805 "%.200s() takes exactly one argument (%d given)",
3806 ((PyCFunctionObject *)func)->m_ml->ml_name,
3807 nargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003808}
3809
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003810#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003811if (tstate->use_tracing && tstate->c_profilefunc) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003812 if (call_trace(tstate->c_profilefunc, \
3813 tstate->c_profileobj, \
3814 tstate->frame, PyTrace_C_CALL, \
3815 func)) { \
3816 x = NULL; \
3817 } \
3818 else { \
3819 x = call; \
3820 if (tstate->c_profilefunc != NULL) { \
3821 if (x == NULL) { \
3822 call_trace_protected(tstate->c_profilefunc, \
3823 tstate->c_profileobj, \
3824 tstate->frame, PyTrace_C_EXCEPTION, \
3825 func); \
3826 /* XXX should pass (type, value, tb) */ \
3827 } else { \
3828 if (call_trace(tstate->c_profilefunc, \
3829 tstate->c_profileobj, \
3830 tstate->frame, PyTrace_C_RETURN, \
3831 func)) { \
3832 Py_DECREF(x); \
3833 x = NULL; \
3834 } \
3835 } \
3836 } \
3837 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003838} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003839 x = call; \
3840 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003841
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003842static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003843call_function(PyObject ***pp_stack, int oparg
3844#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003845 , uint64* pintr0, uint64* pintr1
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003846#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003847 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003848{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003849 int na = oparg & 0xff;
3850 int nk = (oparg>>8) & 0xff;
3851 int n = na + 2 * nk;
3852 PyObject **pfunc = (*pp_stack) - n - 1;
3853 PyObject *func = *pfunc;
3854 PyObject *x, *w;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003855
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003856 /* Always dispatch PyCFunction first, because these are
3857 presumed to be the most frequent callable object.
3858 */
3859 if (PyCFunction_Check(func) && nk == 0) {
3860 int flags = PyCFunction_GET_FLAGS(func);
3861 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003863 PCALL(PCALL_CFUNCTION);
3864 if (flags & (METH_NOARGS | METH_O)) {
3865 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3866 PyObject *self = PyCFunction_GET_SELF(func);
3867 if (flags & METH_NOARGS && na == 0) {
3868 C_TRACE(x, (*meth)(self,NULL));
3869 }
3870 else if (flags & METH_O && na == 1) {
3871 PyObject *arg = EXT_POP(*pp_stack);
3872 C_TRACE(x, (*meth)(self,arg));
3873 Py_DECREF(arg);
3874 }
3875 else {
3876 err_args(func, flags, na);
3877 x = NULL;
3878 }
3879 }
3880 else {
3881 PyObject *callargs;
3882 callargs = load_args(pp_stack, na);
3883 READ_TIMESTAMP(*pintr0);
3884 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
3885 READ_TIMESTAMP(*pintr1);
3886 Py_XDECREF(callargs);
3887 }
3888 } else {
3889 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3890 /* optimize access to bound methods */
3891 PyObject *self = PyMethod_GET_SELF(func);
3892 PCALL(PCALL_METHOD);
3893 PCALL(PCALL_BOUND_METHOD);
3894 Py_INCREF(self);
3895 func = PyMethod_GET_FUNCTION(func);
3896 Py_INCREF(func);
3897 Py_DECREF(*pfunc);
3898 *pfunc = self;
3899 na++;
3900 n++;
3901 } else
3902 Py_INCREF(func);
3903 READ_TIMESTAMP(*pintr0);
3904 if (PyFunction_Check(func))
3905 x = fast_function(func, pp_stack, n, na, nk);
3906 else
3907 x = do_call(func, pp_stack, na, nk);
3908 READ_TIMESTAMP(*pintr1);
3909 Py_DECREF(func);
3910 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003911
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003912 /* Clear the stack of the function object. Also removes
3913 the arguments in case they weren't consumed already
3914 (fast_function() and err_args() leave them on the stack).
3915 */
3916 while ((*pp_stack) > pfunc) {
3917 w = EXT_POP(*pp_stack);
3918 Py_DECREF(w);
3919 PCALL(PCALL_POP);
3920 }
3921 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003922}
3923
Jeremy Hylton192690e2002-08-16 18:36:11 +00003924/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003925 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003926 For the simplest case -- a function that takes only positional
3927 arguments and is called with only positional arguments -- it
3928 inlines the most primitive frame setup code from
3929 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3930 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003931*/
3932
3933static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003934fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003935{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003936 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
3937 PyObject *globals = PyFunction_GET_GLOBALS(func);
3938 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3939 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
3940 PyObject **d = NULL;
3941 int nd = 0;
Jeremy Hylton52820442001-01-03 23:52:36 +00003942
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003943 PCALL(PCALL_FUNCTION);
3944 PCALL(PCALL_FAST_FUNCTION);
3945 if (argdefs == NULL && co->co_argcount == n &&
3946 co->co_kwonlyargcount == 0 && nk==0 &&
3947 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3948 PyFrameObject *f;
3949 PyObject *retval = NULL;
3950 PyThreadState *tstate = PyThreadState_GET();
3951 PyObject **fastlocals, **stack;
3952 int i;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003953
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003954 PCALL(PCALL_FASTER_FUNCTION);
3955 assert(globals != NULL);
3956 /* XXX Perhaps we should create a specialized
3957 PyFrame_New() that doesn't take locals, but does
3958 take builtins without sanity checking them.
3959 */
3960 assert(tstate != NULL);
3961 f = PyFrame_New(tstate, co, globals, NULL);
3962 if (f == NULL)
3963 return NULL;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003964
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003965 fastlocals = f->f_localsplus;
3966 stack = (*pp_stack) - n;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003967
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003968 for (i = 0; i < n; i++) {
3969 Py_INCREF(*stack);
3970 fastlocals[i] = *stack++;
3971 }
3972 retval = PyEval_EvalFrameEx(f,0);
3973 ++tstate->recursion_depth;
3974 Py_DECREF(f);
3975 --tstate->recursion_depth;
3976 return retval;
3977 }
3978 if (argdefs != NULL) {
3979 d = &PyTuple_GET_ITEM(argdefs, 0);
3980 nd = Py_SIZE(argdefs);
3981 }
3982 return PyEval_EvalCodeEx(co, globals,
3983 (PyObject *)NULL, (*pp_stack)-n, na,
3984 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
3985 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003986}
3987
3988static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003989update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3990 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003991{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003992 PyObject *kwdict = NULL;
3993 if (orig_kwdict == NULL)
3994 kwdict = PyDict_New();
3995 else {
3996 kwdict = PyDict_Copy(orig_kwdict);
3997 Py_DECREF(orig_kwdict);
3998 }
3999 if (kwdict == NULL)
4000 return NULL;
4001 while (--nk >= 0) {
4002 int err;
4003 PyObject *value = EXT_POP(*pp_stack);
4004 PyObject *key = EXT_POP(*pp_stack);
4005 if (PyDict_GetItem(kwdict, key) != NULL) {
4006 PyErr_Format(PyExc_TypeError,
4007 "%.200s%s got multiple values "
4008 "for keyword argument '%U'",
4009 PyEval_GetFuncName(func),
4010 PyEval_GetFuncDesc(func),
4011 key);
4012 Py_DECREF(key);
4013 Py_DECREF(value);
4014 Py_DECREF(kwdict);
4015 return NULL;
4016 }
4017 err = PyDict_SetItem(kwdict, key, value);
4018 Py_DECREF(key);
4019 Py_DECREF(value);
4020 if (err) {
4021 Py_DECREF(kwdict);
4022 return NULL;
4023 }
4024 }
4025 return kwdict;
Jeremy Hylton52820442001-01-03 23:52:36 +00004026}
4027
4028static PyObject *
4029update_star_args(int nstack, int nstar, PyObject *stararg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004030 PyObject ***pp_stack)
Jeremy Hylton52820442001-01-03 23:52:36 +00004031{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004032 PyObject *callargs, *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004033
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004034 callargs = PyTuple_New(nstack + nstar);
4035 if (callargs == NULL) {
4036 return NULL;
4037 }
4038 if (nstar) {
4039 int i;
4040 for (i = 0; i < nstar; i++) {
4041 PyObject *a = PyTuple_GET_ITEM(stararg, i);
4042 Py_INCREF(a);
4043 PyTuple_SET_ITEM(callargs, nstack + i, a);
4044 }
4045 }
4046 while (--nstack >= 0) {
4047 w = EXT_POP(*pp_stack);
4048 PyTuple_SET_ITEM(callargs, nstack, w);
4049 }
4050 return callargs;
Jeremy Hylton52820442001-01-03 23:52:36 +00004051}
4052
4053static PyObject *
4054load_args(PyObject ***pp_stack, int na)
4055{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004056 PyObject *args = PyTuple_New(na);
4057 PyObject *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004058
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004059 if (args == NULL)
4060 return NULL;
4061 while (--na >= 0) {
4062 w = EXT_POP(*pp_stack);
4063 PyTuple_SET_ITEM(args, na, w);
4064 }
4065 return args;
Jeremy Hylton52820442001-01-03 23:52:36 +00004066}
4067
4068static PyObject *
4069do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4070{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004071 PyObject *callargs = NULL;
4072 PyObject *kwdict = NULL;
4073 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004074
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004075 if (nk > 0) {
4076 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
4077 if (kwdict == NULL)
4078 goto call_fail;
4079 }
4080 callargs = load_args(pp_stack, na);
4081 if (callargs == NULL)
4082 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004083#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004084 /* At this point, we have to look at the type of func to
4085 update the call stats properly. Do it here so as to avoid
4086 exposing the call stats machinery outside ceval.c
4087 */
4088 if (PyFunction_Check(func))
4089 PCALL(PCALL_FUNCTION);
4090 else if (PyMethod_Check(func))
4091 PCALL(PCALL_METHOD);
4092 else if (PyType_Check(func))
4093 PCALL(PCALL_TYPE);
4094 else if (PyCFunction_Check(func))
4095 PCALL(PCALL_CFUNCTION);
4096 else
4097 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004098#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004099 if (PyCFunction_Check(func)) {
4100 PyThreadState *tstate = PyThreadState_GET();
4101 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4102 }
4103 else
4104 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00004105call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004106 Py_XDECREF(callargs);
4107 Py_XDECREF(kwdict);
4108 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004109}
4110
4111static PyObject *
4112ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4113{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004114 int nstar = 0;
4115 PyObject *callargs = NULL;
4116 PyObject *stararg = NULL;
4117 PyObject *kwdict = NULL;
4118 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004119
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004120 if (flags & CALL_FLAG_KW) {
4121 kwdict = EXT_POP(*pp_stack);
4122 if (!PyDict_Check(kwdict)) {
4123 PyObject *d;
4124 d = PyDict_New();
4125 if (d == NULL)
4126 goto ext_call_fail;
4127 if (PyDict_Update(d, kwdict) != 0) {
4128 Py_DECREF(d);
4129 /* PyDict_Update raises attribute
4130 * error (percolated from an attempt
4131 * to get 'keys' attribute) instead of
4132 * a type error if its second argument
4133 * is not a mapping.
4134 */
4135 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4136 PyErr_Format(PyExc_TypeError,
4137 "%.200s%.200s argument after ** "
4138 "must be a mapping, not %.200s",
4139 PyEval_GetFuncName(func),
4140 PyEval_GetFuncDesc(func),
4141 kwdict->ob_type->tp_name);
4142 }
4143 goto ext_call_fail;
4144 }
4145 Py_DECREF(kwdict);
4146 kwdict = d;
4147 }
4148 }
4149 if (flags & CALL_FLAG_VAR) {
4150 stararg = EXT_POP(*pp_stack);
4151 if (!PyTuple_Check(stararg)) {
4152 PyObject *t = NULL;
4153 t = PySequence_Tuple(stararg);
4154 if (t == NULL) {
4155 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4156 PyErr_Format(PyExc_TypeError,
4157 "%.200s%.200s argument after * "
4158 "must be a sequence, not %200s",
4159 PyEval_GetFuncName(func),
4160 PyEval_GetFuncDesc(func),
4161 stararg->ob_type->tp_name);
4162 }
4163 goto ext_call_fail;
4164 }
4165 Py_DECREF(stararg);
4166 stararg = t;
4167 }
4168 nstar = PyTuple_GET_SIZE(stararg);
4169 }
4170 if (nk > 0) {
4171 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
4172 if (kwdict == NULL)
4173 goto ext_call_fail;
4174 }
4175 callargs = update_star_args(na, nstar, stararg, pp_stack);
4176 if (callargs == NULL)
4177 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004178#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004179 /* At this point, we have to look at the type of func to
4180 update the call stats properly. Do it here so as to avoid
4181 exposing the call stats machinery outside ceval.c
4182 */
4183 if (PyFunction_Check(func))
4184 PCALL(PCALL_FUNCTION);
4185 else if (PyMethod_Check(func))
4186 PCALL(PCALL_METHOD);
4187 else if (PyType_Check(func))
4188 PCALL(PCALL_TYPE);
4189 else if (PyCFunction_Check(func))
4190 PCALL(PCALL_CFUNCTION);
4191 else
4192 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004193#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004194 if (PyCFunction_Check(func)) {
4195 PyThreadState *tstate = PyThreadState_GET();
4196 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4197 }
4198 else
4199 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00004200ext_call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004201 Py_XDECREF(callargs);
4202 Py_XDECREF(kwdict);
4203 Py_XDECREF(stararg);
4204 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004205}
4206
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004207/* Extract a slice index from a PyInt or PyLong or an object with the
4208 nb_index slot defined, and store in *pi.
4209 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4210 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 +00004211 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004212*/
Tim Petersb5196382001-12-16 19:44:20 +00004213/* Note: If v is NULL, return success without storing into *pi. This
4214 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4215 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004216*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004217int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004218_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004219{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004220 if (v != NULL) {
4221 Py_ssize_t x;
4222 if (PyIndex_Check(v)) {
4223 x = PyNumber_AsSsize_t(v, NULL);
4224 if (x == -1 && PyErr_Occurred())
4225 return 0;
4226 }
4227 else {
4228 PyErr_SetString(PyExc_TypeError,
4229 "slice indices must be integers or "
4230 "None or have an __index__ method");
4231 return 0;
4232 }
4233 *pi = x;
4234 }
4235 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004236}
4237
Guido van Rossum486364b2007-06-30 05:01:58 +00004238#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004239 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004240
Guido van Rossumb209a111997-04-29 18:18:01 +00004241static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004242cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004243{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004244 int res = 0;
4245 switch (op) {
4246 case PyCmp_IS:
4247 res = (v == w);
4248 break;
4249 case PyCmp_IS_NOT:
4250 res = (v != w);
4251 break;
4252 case PyCmp_IN:
4253 res = PySequence_Contains(w, v);
4254 if (res < 0)
4255 return NULL;
4256 break;
4257 case PyCmp_NOT_IN:
4258 res = PySequence_Contains(w, v);
4259 if (res < 0)
4260 return NULL;
4261 res = !res;
4262 break;
4263 case PyCmp_EXC_MATCH:
4264 if (PyTuple_Check(w)) {
4265 Py_ssize_t i, length;
4266 length = PyTuple_Size(w);
4267 for (i = 0; i < length; i += 1) {
4268 PyObject *exc = PyTuple_GET_ITEM(w, i);
4269 if (!PyExceptionClass_Check(exc)) {
4270 PyErr_SetString(PyExc_TypeError,
4271 CANNOT_CATCH_MSG);
4272 return NULL;
4273 }
4274 }
4275 }
4276 else {
4277 if (!PyExceptionClass_Check(w)) {
4278 PyErr_SetString(PyExc_TypeError,
4279 CANNOT_CATCH_MSG);
4280 return NULL;
4281 }
4282 }
4283 res = PyErr_GivenExceptionMatches(v, w);
4284 break;
4285 default:
4286 return PyObject_RichCompare(v, w, op);
4287 }
4288 v = res ? Py_True : Py_False;
4289 Py_INCREF(v);
4290 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004291}
4292
Thomas Wouters52152252000-08-17 22:55:00 +00004293static PyObject *
4294import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004295{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004296 PyObject *x;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004298 x = PyObject_GetAttr(v, name);
4299 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
4300 PyErr_Format(PyExc_ImportError, "cannot import name %S", name);
4301 }
4302 return x;
Thomas Wouters52152252000-08-17 22:55:00 +00004303}
Guido van Rossumac7be682001-01-17 15:42:30 +00004304
Thomas Wouters52152252000-08-17 22:55:00 +00004305static int
4306import_all_from(PyObject *locals, PyObject *v)
4307{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004308 PyObject *all = PyObject_GetAttrString(v, "__all__");
4309 PyObject *dict, *name, *value;
4310 int skip_leading_underscores = 0;
4311 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004313 if (all == NULL) {
4314 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4315 return -1; /* Unexpected error */
4316 PyErr_Clear();
4317 dict = PyObject_GetAttrString(v, "__dict__");
4318 if (dict == NULL) {
4319 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4320 return -1;
4321 PyErr_SetString(PyExc_ImportError,
4322 "from-import-* object has no __dict__ and no __all__");
4323 return -1;
4324 }
4325 all = PyMapping_Keys(dict);
4326 Py_DECREF(dict);
4327 if (all == NULL)
4328 return -1;
4329 skip_leading_underscores = 1;
4330 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004331
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004332 for (pos = 0, err = 0; ; pos++) {
4333 name = PySequence_GetItem(all, pos);
4334 if (name == NULL) {
4335 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4336 err = -1;
4337 else
4338 PyErr_Clear();
4339 break;
4340 }
4341 if (skip_leading_underscores &&
4342 PyUnicode_Check(name) &&
4343 PyUnicode_AS_UNICODE(name)[0] == '_')
4344 {
4345 Py_DECREF(name);
4346 continue;
4347 }
4348 value = PyObject_GetAttr(v, name);
4349 if (value == NULL)
4350 err = -1;
4351 else if (PyDict_CheckExact(locals))
4352 err = PyDict_SetItem(locals, name, value);
4353 else
4354 err = PyObject_SetItem(locals, name, value);
4355 Py_DECREF(name);
4356 Py_XDECREF(value);
4357 if (err != 0)
4358 break;
4359 }
4360 Py_DECREF(all);
4361 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004362}
4363
Guido van Rossumac7be682001-01-17 15:42:30 +00004364static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004365format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004366{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004367 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004369 if (!obj)
4370 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004372 obj_str = _PyUnicode_AsString(obj);
4373 if (!obj_str)
4374 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004376 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00004377}
Guido van Rossum950361c1997-01-24 13:49:28 +00004378
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004379static PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +00004380unicode_concatenate(PyObject *v, PyObject *w,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004381 PyFrameObject *f, unsigned char *next_instr)
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004382{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004383 /* This function implements 'variable += expr' when both arguments
4384 are (Unicode) strings. */
4385 Py_ssize_t v_len = PyUnicode_GET_SIZE(v);
4386 Py_ssize_t w_len = PyUnicode_GET_SIZE(w);
4387 Py_ssize_t new_len = v_len + w_len;
4388 if (new_len < 0) {
4389 PyErr_SetString(PyExc_OverflowError,
4390 "strings are too large to concat");
4391 return NULL;
4392 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00004393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004394 if (v->ob_refcnt == 2) {
4395 /* In the common case, there are 2 references to the value
4396 * stored in 'variable' when the += is performed: one on the
4397 * value stack (in 'v') and one still stored in the
4398 * 'variable'. We try to delete the variable now to reduce
4399 * the refcnt to 1.
4400 */
4401 switch (*next_instr) {
4402 case STORE_FAST:
4403 {
4404 int oparg = PEEKARG();
4405 PyObject **fastlocals = f->f_localsplus;
4406 if (GETLOCAL(oparg) == v)
4407 SETLOCAL(oparg, NULL);
4408 break;
4409 }
4410 case STORE_DEREF:
4411 {
4412 PyObject **freevars = (f->f_localsplus +
4413 f->f_code->co_nlocals);
4414 PyObject *c = freevars[PEEKARG()];
4415 if (PyCell_GET(c) == v)
4416 PyCell_Set(c, NULL);
4417 break;
4418 }
4419 case STORE_NAME:
4420 {
4421 PyObject *names = f->f_code->co_names;
4422 PyObject *name = GETITEM(names, PEEKARG());
4423 PyObject *locals = f->f_locals;
4424 if (PyDict_CheckExact(locals) &&
4425 PyDict_GetItem(locals, name) == v) {
4426 if (PyDict_DelItem(locals, name) != 0) {
4427 PyErr_Clear();
4428 }
4429 }
4430 break;
4431 }
4432 }
4433 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004435 if (v->ob_refcnt == 1 && !PyUnicode_CHECK_INTERNED(v)) {
4436 /* Now we own the last reference to 'v', so we can resize it
4437 * in-place.
4438 */
4439 if (PyUnicode_Resize(&v, new_len) != 0) {
4440 /* XXX if PyUnicode_Resize() fails, 'v' has been
4441 * deallocated so it cannot be put back into
4442 * 'variable'. The MemoryError is raised when there
4443 * is no value in 'variable', which might (very
4444 * remotely) be a cause of incompatibilities.
4445 */
4446 return NULL;
4447 }
4448 /* copy 'w' into the newly allocated area of 'v' */
4449 memcpy(PyUnicode_AS_UNICODE(v) + v_len,
4450 PyUnicode_AS_UNICODE(w), w_len*sizeof(Py_UNICODE));
4451 return v;
4452 }
4453 else {
4454 /* When in-place resizing is not an option. */
4455 w = PyUnicode_Concat(v, w);
4456 Py_DECREF(v);
4457 return w;
4458 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004459}
4460
Guido van Rossum950361c1997-01-24 13:49:28 +00004461#ifdef DYNAMIC_EXECUTION_PROFILE
4462
Skip Montanarof118cb12001-10-15 20:51:38 +00004463static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004464getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004465{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004466 int i;
4467 PyObject *l = PyList_New(256);
4468 if (l == NULL) return NULL;
4469 for (i = 0; i < 256; i++) {
4470 PyObject *x = PyLong_FromLong(a[i]);
4471 if (x == NULL) {
4472 Py_DECREF(l);
4473 return NULL;
4474 }
4475 PyList_SetItem(l, i, x);
4476 }
4477 for (i = 0; i < 256; i++)
4478 a[i] = 0;
4479 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004480}
4481
4482PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004483_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004484{
4485#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004486 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00004487#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004488 int i;
4489 PyObject *l = PyList_New(257);
4490 if (l == NULL) return NULL;
4491 for (i = 0; i < 257; i++) {
4492 PyObject *x = getarray(dxpairs[i]);
4493 if (x == NULL) {
4494 Py_DECREF(l);
4495 return NULL;
4496 }
4497 PyList_SetItem(l, i, x);
4498 }
4499 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004500#endif
4501}
4502
4503#endif