blob: 297b44973bfba404acbe4fc617f5aa335b895ffc [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",
90 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 *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 Petersone5bf3832009-01-17 23:43:58 +0000601static volatile 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 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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
843#if defined(USE_COMPUTED_GOTOS) && defined(DYNAMIC_EXECUTION_PROFILE)
844#undef USE_COMPUTED_GOTOS
845#endif
846
847#ifdef USE_COMPUTED_GOTOS
848/* Import the static jump table */
849#include "opcode_targets.h"
850
851/* This macro is used when several opcodes defer to the same implementation
852 (e.g. SETUP_LOOP, SETUP_FINALLY) */
853#define TARGET_WITH_IMPL(op, impl) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 TARGET_##op: \
855 opcode = op; \
856 if (HAS_ARG(op)) \
857 oparg = NEXTARG(); \
858 case op: \
859 goto impl; \
Antoine Pitroub52ec782009-01-25 16:34:23 +0000860
861#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 TARGET_##op: \
863 opcode = op; \
864 if (HAS_ARG(op)) \
865 oparg = NEXTARG(); \
866 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000867
868
869#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 { \
871 if (!_Py_atomic_load_relaxed(&eval_breaker)) { \
872 FAST_DISPATCH(); \
873 } \
874 continue; \
875 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000876
877#ifdef LLTRACE
878#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 { \
880 if (!lltrace && !_Py_TracingPossible) { \
881 f->f_lasti = INSTR_OFFSET(); \
882 goto *opcode_targets[*next_instr++]; \
883 } \
884 goto fast_next_opcode; \
885 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000886#else
887#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 { \
889 if (!_Py_TracingPossible) { \
890 f->f_lasti = INSTR_OFFSET(); \
891 goto *opcode_targets[*next_instr++]; \
892 } \
893 goto fast_next_opcode; \
894 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000895#endif
896
897#else
898#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000900#define TARGET_WITH_IMPL(op, impl) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 /* silence compiler warnings about `impl` unused */ \
902 if (0) goto impl; \
903 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000904#define DISPATCH() continue
905#define FAST_DISPATCH() goto fast_next_opcode
906#endif
907
908
Neal Norwitza81d2202002-07-14 00:27:26 +0000909/* Tuple access macros */
910
911#ifndef Py_DEBUG
912#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
913#else
914#define GETITEM(v, i) PyTuple_GetItem((v), (i))
915#endif
916
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000917#ifdef WITH_TSC
918/* Use Pentium timestamp counter to mark certain events:
919 inst0 -- beginning of switch statement for opcode dispatch
920 inst1 -- end of switch statement (may be skipped)
921 loop0 -- the top of the mainloop
Thomas Wouters477c8d52006-05-27 19:21:47 +0000922 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000923 (may be skipped)
924 intr1 -- beginning of long interruption
925 intr2 -- end of long interruption
926
927 Many opcodes call out to helper C functions. In some cases, the
928 time in those functions should be counted towards the time for the
929 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
930 calls another Python function; there's no point in charge all the
931 bytecode executed by the called function to the caller.
932
933 It's hard to make a useful judgement statically. In the presence
934 of operator overloading, it's impossible to tell if a call will
935 execute new Python code or not.
936
937 It's a case-by-case judgement. I'll use intr1 for the following
938 cases:
939
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000940 IMPORT_STAR
941 IMPORT_FROM
942 CALL_FUNCTION (and friends)
943
944 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000945 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
946 int ticked = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000947
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 READ_TIMESTAMP(inst0);
949 READ_TIMESTAMP(inst1);
950 READ_TIMESTAMP(loop0);
951 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000952
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 /* shut up the compiler */
954 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000955#endif
956
Guido van Rossum374a9221991-04-04 10:40:29 +0000957/* Code access macros */
958
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959#define INSTR_OFFSET() ((int)(next_instr - first_instr))
960#define NEXTOP() (*next_instr++)
961#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
962#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
963#define JUMPTO(x) (next_instr = first_instr + (x))
964#define JUMPBY(x) (next_instr += (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000965
Raymond Hettingerf606f872003-03-16 03:11:04 +0000966/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 Some opcodes tend to come in pairs thus making it possible to
968 predict the second code when the first is run. For example,
969 COMPARE_OP is often followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And,
970 those opcodes are often followed by a POP_TOP.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000971
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 Verifying the prediction costs a single high-speed test of a register
973 variable against a constant. If the pairing was good, then the
974 processor's own internal branch predication has a high likelihood of
975 success, resulting in a nearly zero-overhead transition to the
976 next opcode. A successful prediction saves a trip through the eval-loop
977 including its two unpredictable branches, the HAS_ARG test and the
978 switch-case. Combined with the processor's internal branch prediction,
979 a successful PREDICT has the effect of making the two opcodes run as if
980 they were a single new opcode with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000981
Georg Brandl86b2fb92008-07-16 03:43:04 +0000982 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000983 predictions turned-on and interpret the results as if some opcodes
984 had been combined or turn-off predictions so that the opcode frequency
985 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000986
987 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 the CPU to record separate branch prediction information for each
989 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000990
Raymond Hettingerf606f872003-03-16 03:11:04 +0000991*/
992
Antoine Pitroub52ec782009-01-25 16:34:23 +0000993#if defined(DYNAMIC_EXECUTION_PROFILE) || defined(USE_COMPUTED_GOTOS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994#define PREDICT(op) if (0) goto PRED_##op
995#define PREDICTED(op) PRED_##op:
996#define PREDICTED_WITH_ARG(op) PRED_##op:
Raymond Hettingera7216982004-02-08 19:59:27 +0000997#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998#define PREDICT(op) if (*next_instr == op) goto PRED_##op
999#define PREDICTED(op) PRED_##op: next_instr++
1000#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Antoine Pitroub52ec782009-01-25 16:34:23 +00001001#endif
1002
Raymond Hettingerf606f872003-03-16 03:11:04 +00001003
Guido van Rossum374a9221991-04-04 10:40:29 +00001004/* Stack manipulation macros */
1005
Martin v. Löwis18e16552006-02-15 17:27:45 +00001006/* The stack can grow at most MAXINT deep, as co_nlocals and
1007 co_stacksize are ints. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001008#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1009#define EMPTY() (STACK_LEVEL() == 0)
1010#define TOP() (stack_pointer[-1])
1011#define SECOND() (stack_pointer[-2])
1012#define THIRD() (stack_pointer[-3])
1013#define FOURTH() (stack_pointer[-4])
Benjamin Peterson6d46a912009-06-28 16:17:34 +00001014#define PEEK(n) (stack_pointer[-(n)])
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015#define SET_TOP(v) (stack_pointer[-1] = (v))
1016#define SET_SECOND(v) (stack_pointer[-2] = (v))
1017#define SET_THIRD(v) (stack_pointer[-3] = (v))
1018#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Benjamin Peterson6d46a912009-06-28 16:17:34 +00001019#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020#define BASIC_STACKADJ(n) (stack_pointer += n)
1021#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1022#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001023
Guido van Rossum96a42c81992-01-12 02:29:51 +00001024#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001025#define PUSH(v) { (void)(BASIC_PUSH(v), \
1026 lltrace && prtrace(TOP(), "push")); \
1027 assert(STACK_LEVEL() <= co->co_stacksize); }
1028#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
1029 BASIC_POP())
1030#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
1031 lltrace && prtrace(TOP(), "stackadj")); \
1032 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +00001033#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 prtrace((STACK_POINTER)[-1], "ext_pop")), \
1035 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001036#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001037#define PUSH(v) BASIC_PUSH(v)
1038#define POP() BASIC_POP()
1039#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001040#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001041#endif
1042
Guido van Rossum681d79a1995-07-18 14:51:37 +00001043/* Local variable macros */
1044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001046
1047/* The SETLOCAL() macro must not DECREF the local variable in-place and
1048 then store the new value; it must copy the old value to a temporary
1049 value, then store the new value, and then DECREF the temporary value.
1050 This is because it is possible that during the DECREF the frame is
1051 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1052 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
1054 GETLOCAL(i) = value; \
1055 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001056
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001057
1058#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 while (STACK_LEVEL() > (b)->b_level) { \
1060 PyObject *v = POP(); \
1061 Py_XDECREF(v); \
1062 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001063
1064#define UNWIND_EXCEPT_HANDLER(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 { \
1066 PyObject *type, *value, *traceback; \
1067 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1068 while (STACK_LEVEL() > (b)->b_level + 3) { \
1069 value = POP(); \
1070 Py_XDECREF(value); \
1071 } \
1072 type = tstate->exc_type; \
1073 value = tstate->exc_value; \
1074 traceback = tstate->exc_traceback; \
1075 tstate->exc_type = POP(); \
1076 tstate->exc_value = POP(); \
1077 tstate->exc_traceback = POP(); \
1078 Py_XDECREF(type); \
1079 Py_XDECREF(value); \
1080 Py_XDECREF(traceback); \
1081 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001082
1083#define SAVE_EXC_STATE() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 { \
1085 PyObject *type, *value, *traceback; \
1086 Py_XINCREF(tstate->exc_type); \
1087 Py_XINCREF(tstate->exc_value); \
1088 Py_XINCREF(tstate->exc_traceback); \
1089 type = f->f_exc_type; \
1090 value = f->f_exc_value; \
1091 traceback = f->f_exc_traceback; \
1092 f->f_exc_type = tstate->exc_type; \
1093 f->f_exc_value = tstate->exc_value; \
1094 f->f_exc_traceback = tstate->exc_traceback; \
1095 Py_XDECREF(type); \
1096 Py_XDECREF(value); \
1097 Py_XDECREF(traceback); \
1098 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001099
1100#define SWAP_EXC_STATE() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001101 { \
1102 PyObject *tmp; \
1103 tmp = tstate->exc_type; \
1104 tstate->exc_type = f->f_exc_type; \
1105 f->f_exc_type = tmp; \
1106 tmp = tstate->exc_value; \
1107 tstate->exc_value = f->f_exc_value; \
1108 f->f_exc_value = tmp; \
1109 tmp = tstate->exc_traceback; \
1110 tstate->exc_traceback = f->f_exc_traceback; \
1111 f->f_exc_traceback = tmp; \
1112 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001113
Guido van Rossuma027efa1997-05-05 20:56:21 +00001114/* Start of code */
1115
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 if (f == NULL)
1117 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001118
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 /* push frame */
1120 if (Py_EnterRecursiveCall(""))
1121 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001122
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001124
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 if (tstate->use_tracing) {
1126 if (tstate->c_tracefunc != NULL) {
1127 /* tstate->c_tracefunc, if defined, is a
1128 function that will be called on *every* entry
1129 to a code block. Its return value, if not
1130 None, is a function that will be called at
1131 the start of each executed line of code.
1132 (Actually, the function must return itself
1133 in order to continue tracing.) The trace
1134 functions are called with three arguments:
1135 a pointer to the current frame, a string
1136 indicating why the function is called, and
1137 an argument which depends on the situation.
1138 The global trace function is also called
1139 whenever an exception is detected. */
1140 if (call_trace_protected(tstate->c_tracefunc,
1141 tstate->c_traceobj,
1142 f, PyTrace_CALL, Py_None)) {
1143 /* Trace function raised an error */
1144 goto exit_eval_frame;
1145 }
1146 }
1147 if (tstate->c_profilefunc != NULL) {
1148 /* Similar for c_profilefunc, except it needn't
1149 return itself and isn't called for "line" events */
1150 if (call_trace_protected(tstate->c_profilefunc,
1151 tstate->c_profileobj,
1152 f, PyTrace_CALL, Py_None)) {
1153 /* Profile function raised an error */
1154 goto exit_eval_frame;
1155 }
1156 }
1157 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001158
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001159 co = f->f_code;
1160 names = co->co_names;
1161 consts = co->co_consts;
1162 fastlocals = f->f_localsplus;
1163 freevars = f->f_localsplus + co->co_nlocals;
1164 first_instr = (unsigned char*) PyBytes_AS_STRING(co->co_code);
1165 /* An explanation is in order for the next line.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001166
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 f->f_lasti now refers to the index of the last instruction
1168 executed. You might think this was obvious from the name, but
1169 this wasn't always true before 2.3! PyFrame_New now sets
1170 f->f_lasti to -1 (i.e. the index *before* the first instruction)
1171 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
1172 does work. Promise.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001173
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001174 When the PREDICT() macros are enabled, some opcode pairs follow in
1175 direct succession without updating f->f_lasti. A successful
1176 prediction effectively links the two codes together as if they
1177 were a single new opcode; accordingly,f->f_lasti will point to
1178 the first code in the pair (for instance, GET_ITER followed by
1179 FOR_ITER is effectively a single opcode and f->f_lasti will point
1180 at to the beginning of the combined pair.)
1181 */
1182 next_instr = first_instr + f->f_lasti + 1;
1183 stack_pointer = f->f_stacktop;
1184 assert(stack_pointer != NULL);
1185 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001186
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 if (co->co_flags & CO_GENERATOR && !throwflag) {
1188 if (f->f_exc_type != NULL && f->f_exc_type != Py_None) {
1189 /* We were in an except handler when we left,
1190 restore the exception state which was put aside
1191 (see YIELD_VALUE). */
1192 SWAP_EXC_STATE();
1193 }
1194 else {
1195 SAVE_EXC_STATE();
1196 }
1197 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001198
Tim Peters5ca576e2001-06-18 22:08:13 +00001199#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001200 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001201#endif
Neal Norwitz5f5153e2005-10-21 04:28:38 +00001202#if defined(Py_DEBUG) || defined(LLTRACE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001203 filename = _PyUnicode_AsString(co->co_filename);
Tim Peters5ca576e2001-06-18 22:08:13 +00001204#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001205
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001206 why = WHY_NOT;
1207 err = 0;
1208 x = Py_None; /* Not a reference, just anything non-NULL */
1209 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00001210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001211 if (throwflag) { /* support for generator.throw() */
1212 why = WHY_EXCEPTION;
1213 goto on_error;
1214 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001217#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001218 if (inst1 == 0) {
1219 /* Almost surely, the opcode executed a break
1220 or a continue, preventing inst1 from being set
1221 on the way out of the loop.
1222 */
1223 READ_TIMESTAMP(inst1);
1224 loop1 = inst1;
1225 }
1226 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
1227 intr0, intr1);
1228 ticked = 0;
1229 inst1 = 0;
1230 intr0 = 0;
1231 intr1 = 0;
1232 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001233#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1235 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001236
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 /* Do periodic things. Doing this every time through
1238 the loop would add too much overhead, so we do it
1239 only every Nth instruction. We also do it if
1240 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1241 event needs attention (e.g. a signal handler or
1242 async I/O handler); see Py_AddPendingCall() and
1243 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 if (_Py_atomic_load_relaxed(&eval_breaker)) {
1246 if (*next_instr == SETUP_FINALLY) {
1247 /* Make the last opcode before
1248 a try: finally: block uninterruptable. */
1249 goto fast_next_opcode;
1250 }
1251 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001252#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 ticked = 1;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001254#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) {
1256 if (Py_MakePendingCalls() < 0) {
1257 why = WHY_EXCEPTION;
1258 goto on_error;
1259 }
1260 }
1261 if (_Py_atomic_load_relaxed(&gil_drop_request)) {
Guido van Rossume59214e1994-08-30 08:01:59 +00001262#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001263 /* Give another thread a chance */
1264 if (PyThreadState_Swap(NULL) != tstate)
1265 Py_FatalError("ceval: tstate mix-up");
1266 drop_gil(tstate);
1267
1268 /* Other threads may run now */
1269
1270 take_gil(tstate);
1271 if (PyThreadState_Swap(tstate) != NULL)
1272 Py_FatalError("ceval: orphan tstate");
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001273#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001274 }
1275 /* Check for asynchronous exceptions. */
1276 if (tstate->async_exc != NULL) {
1277 x = tstate->async_exc;
1278 tstate->async_exc = NULL;
1279 UNSIGNAL_ASYNC_EXC();
1280 PyErr_SetNone(x);
1281 Py_DECREF(x);
1282 why = WHY_EXCEPTION;
1283 goto on_error;
1284 }
1285 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 fast_next_opcode:
1288 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 if (_Py_TracingPossible &&
1293 tstate->c_tracefunc != NULL && !tstate->tracing) {
1294 /* see maybe_call_line_trace
1295 for expository comments */
1296 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001298 err = maybe_call_line_trace(tstate->c_tracefunc,
1299 tstate->c_traceobj,
1300 f, &instr_lb, &instr_ub,
1301 &instr_prev);
1302 /* Reload possibly changed frame fields */
1303 JUMPTO(f->f_lasti);
1304 if (f->f_stacktop != NULL) {
1305 stack_pointer = f->f_stacktop;
1306 f->f_stacktop = NULL;
1307 }
1308 if (err) {
1309 /* trace function raised an exception */
1310 goto on_error;
1311 }
1312 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001314 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001315
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001316 opcode = NEXTOP();
1317 oparg = 0; /* allows oparg to be stored in a register because
1318 it doesn't have to be remembered across a full loop */
1319 if (HAS_ARG(opcode))
1320 oparg = NEXTARG();
1321 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001322#ifdef DYNAMIC_EXECUTION_PROFILE
1323#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 dxpairs[lastopcode][opcode]++;
1325 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001326#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001328#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001329
Guido van Rossum96a42c81992-01-12 02:29:51 +00001330#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001331 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 if (lltrace) {
1334 if (HAS_ARG(opcode)) {
1335 printf("%d: %d, %d\n",
1336 f->f_lasti, opcode, oparg);
1337 }
1338 else {
1339 printf("%d: %d\n",
1340 f->f_lasti, opcode);
1341 }
1342 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001343#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 /* Main switch on opcode */
1346 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001348 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 /* BEWARE!
1351 It is essential that any operation that fails sets either
1352 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1353 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001354
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001355 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001357 TARGET(NOP)
1358 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001359
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 TARGET(LOAD_FAST)
1361 x = GETLOCAL(oparg);
1362 if (x != NULL) {
1363 Py_INCREF(x);
1364 PUSH(x);
1365 FAST_DISPATCH();
1366 }
1367 format_exc_check_arg(PyExc_UnboundLocalError,
1368 UNBOUNDLOCAL_ERROR_MSG,
1369 PyTuple_GetItem(co->co_varnames, oparg));
1370 break;
Neil Schemenauer63543862002-02-17 19:10:14 +00001371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 TARGET(LOAD_CONST)
1373 x = GETITEM(consts, oparg);
1374 Py_INCREF(x);
1375 PUSH(x);
1376 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001377
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 PREDICTED_WITH_ARG(STORE_FAST);
1379 TARGET(STORE_FAST)
1380 v = POP();
1381 SETLOCAL(oparg, v);
1382 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001384 TARGET(POP_TOP)
1385 v = POP();
1386 Py_DECREF(v);
1387 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 TARGET(ROT_TWO)
1390 v = TOP();
1391 w = SECOND();
1392 SET_TOP(w);
1393 SET_SECOND(v);
1394 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001396 TARGET(ROT_THREE)
1397 v = TOP();
1398 w = SECOND();
1399 x = THIRD();
1400 SET_TOP(w);
1401 SET_SECOND(x);
1402 SET_THIRD(v);
1403 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 TARGET(ROT_FOUR)
1406 u = TOP();
1407 v = SECOND();
1408 w = THIRD();
1409 x = FOURTH();
1410 SET_TOP(v);
1411 SET_SECOND(w);
1412 SET_THIRD(x);
1413 SET_FOURTH(u);
1414 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001416 TARGET(DUP_TOP)
1417 v = TOP();
1418 Py_INCREF(v);
1419 PUSH(v);
1420 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001421
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001422 TARGET(DUP_TOPX)
1423 if (oparg == 2) {
1424 x = TOP();
1425 Py_INCREF(x);
1426 w = SECOND();
1427 Py_INCREF(w);
1428 STACKADJ(2);
1429 SET_TOP(x);
1430 SET_SECOND(w);
1431 FAST_DISPATCH();
1432 } else if (oparg == 3) {
1433 x = TOP();
1434 Py_INCREF(x);
1435 w = SECOND();
1436 Py_INCREF(w);
1437 v = THIRD();
1438 Py_INCREF(v);
1439 STACKADJ(3);
1440 SET_TOP(x);
1441 SET_SECOND(w);
1442 SET_THIRD(v);
1443 FAST_DISPATCH();
1444 }
1445 Py_FatalError("invalid argument to DUP_TOPX"
1446 " (bytecode corruption?)");
1447 /* Never returns, so don't bother to set why. */
1448 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001450 TARGET(UNARY_POSITIVE)
1451 v = TOP();
1452 x = PyNumber_Positive(v);
1453 Py_DECREF(v);
1454 SET_TOP(x);
1455 if (x != NULL) DISPATCH();
1456 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001458 TARGET(UNARY_NEGATIVE)
1459 v = TOP();
1460 x = PyNumber_Negative(v);
1461 Py_DECREF(v);
1462 SET_TOP(x);
1463 if (x != NULL) DISPATCH();
1464 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001466 TARGET(UNARY_NOT)
1467 v = TOP();
1468 err = PyObject_IsTrue(v);
1469 Py_DECREF(v);
1470 if (err == 0) {
1471 Py_INCREF(Py_True);
1472 SET_TOP(Py_True);
1473 DISPATCH();
1474 }
1475 else if (err > 0) {
1476 Py_INCREF(Py_False);
1477 SET_TOP(Py_False);
1478 err = 0;
1479 DISPATCH();
1480 }
1481 STACKADJ(-1);
1482 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 TARGET(UNARY_INVERT)
1485 v = TOP();
1486 x = PyNumber_Invert(v);
1487 Py_DECREF(v);
1488 SET_TOP(x);
1489 if (x != NULL) DISPATCH();
1490 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001491
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001492 TARGET(BINARY_POWER)
1493 w = POP();
1494 v = TOP();
1495 x = PyNumber_Power(v, w, Py_None);
1496 Py_DECREF(v);
1497 Py_DECREF(w);
1498 SET_TOP(x);
1499 if (x != NULL) DISPATCH();
1500 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001501
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001502 TARGET(BINARY_MULTIPLY)
1503 w = POP();
1504 v = TOP();
1505 x = PyNumber_Multiply(v, w);
1506 Py_DECREF(v);
1507 Py_DECREF(w);
1508 SET_TOP(x);
1509 if (x != NULL) DISPATCH();
1510 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001512 TARGET(BINARY_TRUE_DIVIDE)
1513 w = POP();
1514 v = TOP();
1515 x = PyNumber_TrueDivide(v, w);
1516 Py_DECREF(v);
1517 Py_DECREF(w);
1518 SET_TOP(x);
1519 if (x != NULL) DISPATCH();
1520 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001521
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001522 TARGET(BINARY_FLOOR_DIVIDE)
1523 w = POP();
1524 v = TOP();
1525 x = PyNumber_FloorDivide(v, w);
1526 Py_DECREF(v);
1527 Py_DECREF(w);
1528 SET_TOP(x);
1529 if (x != NULL) DISPATCH();
1530 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001531
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001532 TARGET(BINARY_MODULO)
1533 w = POP();
1534 v = TOP();
1535 if (PyUnicode_CheckExact(v))
1536 x = PyUnicode_Format(v, w);
1537 else
1538 x = PyNumber_Remainder(v, w);
1539 Py_DECREF(v);
1540 Py_DECREF(w);
1541 SET_TOP(x);
1542 if (x != NULL) DISPATCH();
1543 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001545 TARGET(BINARY_ADD)
1546 w = POP();
1547 v = TOP();
1548 if (PyUnicode_CheckExact(v) &&
1549 PyUnicode_CheckExact(w)) {
1550 x = unicode_concatenate(v, w, f, next_instr);
1551 /* unicode_concatenate consumed the ref to v */
1552 goto skip_decref_vx;
1553 }
1554 else {
1555 x = PyNumber_Add(v, w);
1556 }
1557 Py_DECREF(v);
1558 skip_decref_vx:
1559 Py_DECREF(w);
1560 SET_TOP(x);
1561 if (x != NULL) DISPATCH();
1562 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001564 TARGET(BINARY_SUBTRACT)
1565 w = POP();
1566 v = TOP();
1567 x = PyNumber_Subtract(v, w);
1568 Py_DECREF(v);
1569 Py_DECREF(w);
1570 SET_TOP(x);
1571 if (x != NULL) DISPATCH();
1572 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001573
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001574 TARGET(BINARY_SUBSCR)
1575 w = POP();
1576 v = TOP();
1577 x = PyObject_GetItem(v, w);
1578 Py_DECREF(v);
1579 Py_DECREF(w);
1580 SET_TOP(x);
1581 if (x != NULL) DISPATCH();
1582 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001583
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001584 TARGET(BINARY_LSHIFT)
1585 w = POP();
1586 v = TOP();
1587 x = PyNumber_Lshift(v, w);
1588 Py_DECREF(v);
1589 Py_DECREF(w);
1590 SET_TOP(x);
1591 if (x != NULL) DISPATCH();
1592 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001593
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001594 TARGET(BINARY_RSHIFT)
1595 w = POP();
1596 v = TOP();
1597 x = PyNumber_Rshift(v, w);
1598 Py_DECREF(v);
1599 Py_DECREF(w);
1600 SET_TOP(x);
1601 if (x != NULL) DISPATCH();
1602 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001603
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001604 TARGET(BINARY_AND)
1605 w = POP();
1606 v = TOP();
1607 x = PyNumber_And(v, w);
1608 Py_DECREF(v);
1609 Py_DECREF(w);
1610 SET_TOP(x);
1611 if (x != NULL) DISPATCH();
1612 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001613
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001614 TARGET(BINARY_XOR)
1615 w = POP();
1616 v = TOP();
1617 x = PyNumber_Xor(v, w);
1618 Py_DECREF(v);
1619 Py_DECREF(w);
1620 SET_TOP(x);
1621 if (x != NULL) DISPATCH();
1622 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001623
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001624 TARGET(BINARY_OR)
1625 w = POP();
1626 v = TOP();
1627 x = PyNumber_Or(v, w);
1628 Py_DECREF(v);
1629 Py_DECREF(w);
1630 SET_TOP(x);
1631 if (x != NULL) DISPATCH();
1632 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001634 TARGET(LIST_APPEND)
1635 w = POP();
1636 v = PEEK(oparg);
1637 err = PyList_Append(v, w);
1638 Py_DECREF(w);
1639 if (err == 0) {
1640 PREDICT(JUMP_ABSOLUTE);
1641 DISPATCH();
1642 }
1643 break;
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001645 TARGET(SET_ADD)
1646 w = POP();
1647 v = stack_pointer[-oparg];
1648 err = PySet_Add(v, w);
1649 Py_DECREF(w);
1650 if (err == 0) {
1651 PREDICT(JUMP_ABSOLUTE);
1652 DISPATCH();
1653 }
1654 break;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001655
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001656 TARGET(INPLACE_POWER)
1657 w = POP();
1658 v = TOP();
1659 x = PyNumber_InPlacePower(v, w, Py_None);
1660 Py_DECREF(v);
1661 Py_DECREF(w);
1662 SET_TOP(x);
1663 if (x != NULL) DISPATCH();
1664 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001665
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001666 TARGET(INPLACE_MULTIPLY)
1667 w = POP();
1668 v = TOP();
1669 x = PyNumber_InPlaceMultiply(v, w);
1670 Py_DECREF(v);
1671 Py_DECREF(w);
1672 SET_TOP(x);
1673 if (x != NULL) DISPATCH();
1674 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001675
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001676 TARGET(INPLACE_TRUE_DIVIDE)
1677 w = POP();
1678 v = TOP();
1679 x = PyNumber_InPlaceTrueDivide(v, w);
1680 Py_DECREF(v);
1681 Py_DECREF(w);
1682 SET_TOP(x);
1683 if (x != NULL) DISPATCH();
1684 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001685
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001686 TARGET(INPLACE_FLOOR_DIVIDE)
1687 w = POP();
1688 v = TOP();
1689 x = PyNumber_InPlaceFloorDivide(v, w);
1690 Py_DECREF(v);
1691 Py_DECREF(w);
1692 SET_TOP(x);
1693 if (x != NULL) DISPATCH();
1694 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001695
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001696 TARGET(INPLACE_MODULO)
1697 w = POP();
1698 v = TOP();
1699 x = PyNumber_InPlaceRemainder(v, w);
1700 Py_DECREF(v);
1701 Py_DECREF(w);
1702 SET_TOP(x);
1703 if (x != NULL) DISPATCH();
1704 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001705
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001706 TARGET(INPLACE_ADD)
1707 w = POP();
1708 v = TOP();
1709 if (PyUnicode_CheckExact(v) &&
1710 PyUnicode_CheckExact(w)) {
1711 x = unicode_concatenate(v, w, f, next_instr);
1712 /* unicode_concatenate consumed the ref to v */
1713 goto skip_decref_v;
1714 }
1715 else {
1716 x = PyNumber_InPlaceAdd(v, w);
1717 }
1718 Py_DECREF(v);
1719 skip_decref_v:
1720 Py_DECREF(w);
1721 SET_TOP(x);
1722 if (x != NULL) DISPATCH();
1723 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001724
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001725 TARGET(INPLACE_SUBTRACT)
1726 w = POP();
1727 v = TOP();
1728 x = PyNumber_InPlaceSubtract(v, w);
1729 Py_DECREF(v);
1730 Py_DECREF(w);
1731 SET_TOP(x);
1732 if (x != NULL) DISPATCH();
1733 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001734
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001735 TARGET(INPLACE_LSHIFT)
1736 w = POP();
1737 v = TOP();
1738 x = PyNumber_InPlaceLshift(v, w);
1739 Py_DECREF(v);
1740 Py_DECREF(w);
1741 SET_TOP(x);
1742 if (x != NULL) DISPATCH();
1743 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001744
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001745 TARGET(INPLACE_RSHIFT)
1746 w = POP();
1747 v = TOP();
1748 x = PyNumber_InPlaceRshift(v, w);
1749 Py_DECREF(v);
1750 Py_DECREF(w);
1751 SET_TOP(x);
1752 if (x != NULL) DISPATCH();
1753 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001754
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001755 TARGET(INPLACE_AND)
1756 w = POP();
1757 v = TOP();
1758 x = PyNumber_InPlaceAnd(v, w);
1759 Py_DECREF(v);
1760 Py_DECREF(w);
1761 SET_TOP(x);
1762 if (x != NULL) DISPATCH();
1763 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001764
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001765 TARGET(INPLACE_XOR)
1766 w = POP();
1767 v = TOP();
1768 x = PyNumber_InPlaceXor(v, w);
1769 Py_DECREF(v);
1770 Py_DECREF(w);
1771 SET_TOP(x);
1772 if (x != NULL) DISPATCH();
1773 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001774
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001775 TARGET(INPLACE_OR)
1776 w = POP();
1777 v = TOP();
1778 x = PyNumber_InPlaceOr(v, w);
1779 Py_DECREF(v);
1780 Py_DECREF(w);
1781 SET_TOP(x);
1782 if (x != NULL) DISPATCH();
1783 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001784
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001785 TARGET(STORE_SUBSCR)
1786 w = TOP();
1787 v = SECOND();
1788 u = THIRD();
1789 STACKADJ(-3);
1790 /* v[w] = u */
1791 err = PyObject_SetItem(v, w, u);
1792 Py_DECREF(u);
1793 Py_DECREF(v);
1794 Py_DECREF(w);
1795 if (err == 0) DISPATCH();
1796 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001797
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001798 TARGET(DELETE_SUBSCR)
1799 w = TOP();
1800 v = SECOND();
1801 STACKADJ(-2);
1802 /* del v[w] */
1803 err = PyObject_DelItem(v, w);
1804 Py_DECREF(v);
1805 Py_DECREF(w);
1806 if (err == 0) DISPATCH();
1807 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001808
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001809 TARGET(PRINT_EXPR)
1810 v = POP();
1811 w = PySys_GetObject("displayhook");
1812 if (w == NULL) {
1813 PyErr_SetString(PyExc_RuntimeError,
1814 "lost sys.displayhook");
1815 err = -1;
1816 x = NULL;
1817 }
1818 if (err == 0) {
1819 x = PyTuple_Pack(1, v);
1820 if (x == NULL)
1821 err = -1;
1822 }
1823 if (err == 0) {
1824 w = PyEval_CallObject(w, x);
1825 Py_XDECREF(w);
1826 if (w == NULL)
1827 err = -1;
1828 }
1829 Py_DECREF(v);
1830 Py_XDECREF(x);
1831 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001832
Thomas Wouters434d0822000-08-24 20:11:32 +00001833#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001834 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001835#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001836 TARGET(RAISE_VARARGS)
1837 v = w = NULL;
1838 switch (oparg) {
1839 case 2:
1840 v = POP(); /* cause */
1841 case 1:
1842 w = POP(); /* exc */
1843 case 0: /* Fallthrough */
1844 why = do_raise(w, v);
1845 break;
1846 default:
1847 PyErr_SetString(PyExc_SystemError,
1848 "bad RAISE_VARARGS oparg");
1849 why = WHY_EXCEPTION;
1850 break;
1851 }
1852 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001853
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001854 TARGET(STORE_LOCALS)
1855 x = POP();
1856 v = f->f_locals;
1857 Py_XDECREF(v);
1858 f->f_locals = x;
1859 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001860
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001861 TARGET(RETURN_VALUE)
1862 retval = POP();
1863 why = WHY_RETURN;
1864 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001865
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001866 TARGET(YIELD_VALUE)
1867 retval = POP();
1868 f->f_stacktop = stack_pointer;
1869 why = WHY_YIELD;
1870 /* Put aside the current exception state and restore
1871 that of the calling frame. This only serves when
1872 "yield" is used inside an except handler. */
1873 SWAP_EXC_STATE();
1874 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001875
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001876 TARGET(POP_EXCEPT)
1877 {
1878 PyTryBlock *b = PyFrame_BlockPop(f);
1879 if (b->b_type != EXCEPT_HANDLER) {
1880 PyErr_SetString(PyExc_SystemError,
1881 "popped block is not an except handler");
1882 why = WHY_EXCEPTION;
1883 break;
1884 }
1885 UNWIND_EXCEPT_HANDLER(b);
1886 }
1887 DISPATCH();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001888
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001889 TARGET(POP_BLOCK)
1890 {
1891 PyTryBlock *b = PyFrame_BlockPop(f);
1892 UNWIND_BLOCK(b);
1893 }
1894 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001895
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001896 PREDICTED(END_FINALLY);
1897 TARGET(END_FINALLY)
1898 v = POP();
1899 if (PyLong_Check(v)) {
1900 why = (enum why_code) PyLong_AS_LONG(v);
1901 assert(why != WHY_YIELD);
1902 if (why == WHY_RETURN ||
1903 why == WHY_CONTINUE)
1904 retval = POP();
1905 if (why == WHY_SILENCED) {
1906 /* An exception was silenced by 'with', we must
1907 manually unwind the EXCEPT_HANDLER block which was
1908 created when the exception was caught, otherwise
1909 the stack will be in an inconsistent state. */
1910 PyTryBlock *b = PyFrame_BlockPop(f);
1911 assert(b->b_type == EXCEPT_HANDLER);
1912 UNWIND_EXCEPT_HANDLER(b);
1913 why = WHY_NOT;
1914 }
1915 }
1916 else if (PyExceptionClass_Check(v)) {
1917 w = POP();
1918 u = POP();
1919 PyErr_Restore(v, w, u);
1920 why = WHY_RERAISE;
1921 break;
1922 }
1923 else if (v != Py_None) {
1924 PyErr_SetString(PyExc_SystemError,
1925 "'finally' pops bad exception");
1926 why = WHY_EXCEPTION;
1927 }
1928 Py_DECREF(v);
1929 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001930
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001931 TARGET(LOAD_BUILD_CLASS)
1932 x = PyDict_GetItemString(f->f_builtins,
1933 "__build_class__");
1934 if (x == NULL) {
1935 PyErr_SetString(PyExc_ImportError,
1936 "__build_class__ not found");
1937 break;
1938 }
1939 Py_INCREF(x);
1940 PUSH(x);
1941 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001942
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001943 TARGET(STORE_NAME)
1944 w = GETITEM(names, oparg);
1945 v = POP();
1946 if ((x = f->f_locals) != NULL) {
1947 if (PyDict_CheckExact(x))
1948 err = PyDict_SetItem(x, w, v);
1949 else
1950 err = PyObject_SetItem(x, w, v);
1951 Py_DECREF(v);
1952 if (err == 0) DISPATCH();
1953 break;
1954 }
1955 PyErr_Format(PyExc_SystemError,
1956 "no locals found when storing %R", w);
1957 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001958
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001959 TARGET(DELETE_NAME)
1960 w = GETITEM(names, oparg);
1961 if ((x = f->f_locals) != NULL) {
1962 if ((err = PyObject_DelItem(x, w)) != 0)
1963 format_exc_check_arg(PyExc_NameError,
1964 NAME_ERROR_MSG,
1965 w);
1966 break;
1967 }
1968 PyErr_Format(PyExc_SystemError,
1969 "no locals when deleting %R", w);
1970 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001971
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001972 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
1973 TARGET(UNPACK_SEQUENCE)
1974 v = POP();
1975 if (PyTuple_CheckExact(v) &&
1976 PyTuple_GET_SIZE(v) == oparg) {
1977 PyObject **items = \
1978 ((PyTupleObject *)v)->ob_item;
1979 while (oparg--) {
1980 w = items[oparg];
1981 Py_INCREF(w);
1982 PUSH(w);
1983 }
1984 Py_DECREF(v);
1985 DISPATCH();
1986 } else if (PyList_CheckExact(v) &&
1987 PyList_GET_SIZE(v) == oparg) {
1988 PyObject **items = \
1989 ((PyListObject *)v)->ob_item;
1990 while (oparg--) {
1991 w = items[oparg];
1992 Py_INCREF(w);
1993 PUSH(w);
1994 }
1995 } else if (unpack_iterable(v, oparg, -1,
1996 stack_pointer + oparg)) {
1997 STACKADJ(oparg);
1998 } else {
1999 /* unpack_iterable() raised an exception */
2000 why = WHY_EXCEPTION;
2001 }
2002 Py_DECREF(v);
2003 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002004
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002005 TARGET(UNPACK_EX)
2006 {
2007 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2008 v = POP();
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002009
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002010 if (unpack_iterable(v, oparg & 0xFF, oparg >> 8,
2011 stack_pointer + totalargs)) {
2012 stack_pointer += totalargs;
2013 } else {
2014 why = WHY_EXCEPTION;
2015 }
2016 Py_DECREF(v);
2017 break;
2018 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002019
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002020 TARGET(STORE_ATTR)
2021 w = GETITEM(names, oparg);
2022 v = TOP();
2023 u = SECOND();
2024 STACKADJ(-2);
2025 err = PyObject_SetAttr(v, w, u); /* v.w = u */
2026 Py_DECREF(v);
2027 Py_DECREF(u);
2028 if (err == 0) DISPATCH();
2029 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002030
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002031 TARGET(DELETE_ATTR)
2032 w = GETITEM(names, oparg);
2033 v = POP();
2034 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
2035 /* del v.w */
2036 Py_DECREF(v);
2037 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002038
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002039 TARGET(STORE_GLOBAL)
2040 w = GETITEM(names, oparg);
2041 v = POP();
2042 err = PyDict_SetItem(f->f_globals, w, v);
2043 Py_DECREF(v);
2044 if (err == 0) DISPATCH();
2045 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002046
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002047 TARGET(DELETE_GLOBAL)
2048 w = GETITEM(names, oparg);
2049 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
2050 format_exc_check_arg(
2051 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
2052 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002053
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002054 TARGET(LOAD_NAME)
2055 w = GETITEM(names, oparg);
2056 if ((v = f->f_locals) == NULL) {
2057 PyErr_Format(PyExc_SystemError,
2058 "no locals when loading %R", w);
2059 why = WHY_EXCEPTION;
2060 break;
2061 }
2062 if (PyDict_CheckExact(v)) {
2063 x = PyDict_GetItem(v, w);
2064 Py_XINCREF(x);
2065 }
2066 else {
2067 x = PyObject_GetItem(v, w);
2068 if (x == NULL && PyErr_Occurred()) {
2069 if (!PyErr_ExceptionMatches(
2070 PyExc_KeyError))
2071 break;
2072 PyErr_Clear();
2073 }
2074 }
2075 if (x == NULL) {
2076 x = PyDict_GetItem(f->f_globals, w);
2077 if (x == NULL) {
2078 x = PyDict_GetItem(f->f_builtins, w);
2079 if (x == NULL) {
2080 format_exc_check_arg(
2081 PyExc_NameError,
2082 NAME_ERROR_MSG, w);
2083 break;
2084 }
2085 }
2086 Py_INCREF(x);
2087 }
2088 PUSH(x);
2089 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002090
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002091 TARGET(LOAD_GLOBAL)
2092 w = GETITEM(names, oparg);
2093 if (PyUnicode_CheckExact(w)) {
2094 /* Inline the PyDict_GetItem() calls.
2095 WARNING: this is an extreme speed hack.
2096 Do not try this at home. */
2097 long hash = ((PyUnicodeObject *)w)->hash;
2098 if (hash != -1) {
2099 PyDictObject *d;
2100 PyDictEntry *e;
2101 d = (PyDictObject *)(f->f_globals);
2102 e = d->ma_lookup(d, w, hash);
2103 if (e == NULL) {
2104 x = NULL;
2105 break;
2106 }
2107 x = e->me_value;
2108 if (x != NULL) {
2109 Py_INCREF(x);
2110 PUSH(x);
2111 DISPATCH();
2112 }
2113 d = (PyDictObject *)(f->f_builtins);
2114 e = d->ma_lookup(d, w, hash);
2115 if (e == NULL) {
2116 x = NULL;
2117 break;
2118 }
2119 x = e->me_value;
2120 if (x != NULL) {
2121 Py_INCREF(x);
2122 PUSH(x);
2123 DISPATCH();
2124 }
2125 goto load_global_error;
2126 }
2127 }
2128 /* This is the un-inlined version of the code above */
2129 x = PyDict_GetItem(f->f_globals, w);
2130 if (x == NULL) {
2131 x = PyDict_GetItem(f->f_builtins, w);
2132 if (x == NULL) {
2133 load_global_error:
2134 format_exc_check_arg(
2135 PyExc_NameError,
2136 GLOBAL_NAME_ERROR_MSG, w);
2137 break;
2138 }
2139 }
2140 Py_INCREF(x);
2141 PUSH(x);
2142 DISPATCH();
Guido van Rossum681d79a1995-07-18 14:51:37 +00002143
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002144 TARGET(DELETE_FAST)
2145 x = GETLOCAL(oparg);
2146 if (x != NULL) {
2147 SETLOCAL(oparg, NULL);
2148 DISPATCH();
2149 }
2150 format_exc_check_arg(
2151 PyExc_UnboundLocalError,
2152 UNBOUNDLOCAL_ERROR_MSG,
2153 PyTuple_GetItem(co->co_varnames, oparg)
2154 );
2155 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002156
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002157 TARGET(LOAD_CLOSURE)
2158 x = freevars[oparg];
2159 Py_INCREF(x);
2160 PUSH(x);
2161 if (x != NULL) DISPATCH();
2162 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002163
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002164 TARGET(LOAD_DEREF)
2165 x = freevars[oparg];
2166 w = PyCell_Get(x);
2167 if (w != NULL) {
2168 PUSH(w);
2169 DISPATCH();
2170 }
2171 err = -1;
2172 /* Don't stomp existing exception */
2173 if (PyErr_Occurred())
2174 break;
2175 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
2176 v = PyTuple_GET_ITEM(co->co_cellvars,
2177 oparg);
2178 format_exc_check_arg(
2179 PyExc_UnboundLocalError,
2180 UNBOUNDLOCAL_ERROR_MSG,
2181 v);
2182 } else {
2183 v = PyTuple_GET_ITEM(co->co_freevars, oparg -
2184 PyTuple_GET_SIZE(co->co_cellvars));
2185 format_exc_check_arg(PyExc_NameError,
2186 UNBOUNDFREE_ERROR_MSG, v);
2187 }
2188 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002189
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002190 TARGET(STORE_DEREF)
2191 w = POP();
2192 x = freevars[oparg];
2193 PyCell_Set(x, w);
2194 Py_DECREF(w);
2195 DISPATCH();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002196
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002197 TARGET(BUILD_TUPLE)
2198 x = PyTuple_New(oparg);
2199 if (x != NULL) {
2200 for (; --oparg >= 0;) {
2201 w = POP();
2202 PyTuple_SET_ITEM(x, oparg, w);
2203 }
2204 PUSH(x);
2205 DISPATCH();
2206 }
2207 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002208
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002209 TARGET(BUILD_LIST)
2210 x = PyList_New(oparg);
2211 if (x != NULL) {
2212 for (; --oparg >= 0;) {
2213 w = POP();
2214 PyList_SET_ITEM(x, oparg, w);
2215 }
2216 PUSH(x);
2217 DISPATCH();
2218 }
2219 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002220
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002221 TARGET(BUILD_SET)
2222 x = PySet_New(NULL);
2223 if (x != NULL) {
2224 for (; --oparg >= 0;) {
2225 w = POP();
2226 if (err == 0)
2227 err = PySet_Add(x, w);
2228 Py_DECREF(w);
2229 }
2230 if (err != 0) {
2231 Py_DECREF(x);
2232 break;
2233 }
2234 PUSH(x);
2235 DISPATCH();
2236 }
2237 break;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002239 TARGET(BUILD_MAP)
2240 x = _PyDict_NewPresized((Py_ssize_t)oparg);
2241 PUSH(x);
2242 if (x != NULL) DISPATCH();
2243 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002245 TARGET(STORE_MAP)
2246 w = TOP(); /* key */
2247 u = SECOND(); /* value */
2248 v = THIRD(); /* dict */
2249 STACKADJ(-2);
2250 assert (PyDict_CheckExact(v));
2251 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2252 Py_DECREF(u);
2253 Py_DECREF(w);
2254 if (err == 0) DISPATCH();
2255 break;
Christian Heimes99170a52007-12-19 02:07:34 +00002256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002257 TARGET(MAP_ADD)
2258 w = TOP(); /* key */
2259 u = SECOND(); /* value */
2260 STACKADJ(-2);
2261 v = stack_pointer[-oparg]; /* dict */
2262 assert (PyDict_CheckExact(v));
2263 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2264 Py_DECREF(u);
2265 Py_DECREF(w);
2266 if (err == 0) {
2267 PREDICT(JUMP_ABSOLUTE);
2268 DISPATCH();
2269 }
2270 break;
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002272 TARGET(LOAD_ATTR)
2273 w = GETITEM(names, oparg);
2274 v = TOP();
2275 x = PyObject_GetAttr(v, w);
2276 Py_DECREF(v);
2277 SET_TOP(x);
2278 if (x != NULL) DISPATCH();
2279 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002281 TARGET(COMPARE_OP)
2282 w = POP();
2283 v = TOP();
2284 x = cmp_outcome(oparg, v, w);
2285 Py_DECREF(v);
2286 Py_DECREF(w);
2287 SET_TOP(x);
2288 if (x == NULL) break;
2289 PREDICT(POP_JUMP_IF_FALSE);
2290 PREDICT(POP_JUMP_IF_TRUE);
2291 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002293 TARGET(IMPORT_NAME)
2294 w = GETITEM(names, oparg);
2295 x = PyDict_GetItemString(f->f_builtins, "__import__");
2296 if (x == NULL) {
2297 PyErr_SetString(PyExc_ImportError,
2298 "__import__ not found");
2299 break;
2300 }
2301 Py_INCREF(x);
2302 v = POP();
2303 u = TOP();
2304 if (PyLong_AsLong(u) != -1 || PyErr_Occurred())
2305 w = PyTuple_Pack(5,
2306 w,
2307 f->f_globals,
2308 f->f_locals == NULL ?
2309 Py_None : f->f_locals,
2310 v,
2311 u);
2312 else
2313 w = PyTuple_Pack(4,
2314 w,
2315 f->f_globals,
2316 f->f_locals == NULL ?
2317 Py_None : f->f_locals,
2318 v);
2319 Py_DECREF(v);
2320 Py_DECREF(u);
2321 if (w == NULL) {
2322 u = POP();
2323 Py_DECREF(x);
2324 x = NULL;
2325 break;
2326 }
2327 READ_TIMESTAMP(intr0);
2328 v = x;
2329 x = PyEval_CallObject(v, w);
2330 Py_DECREF(v);
2331 READ_TIMESTAMP(intr1);
2332 Py_DECREF(w);
2333 SET_TOP(x);
2334 if (x != NULL) DISPATCH();
2335 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002336
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002337 TARGET(IMPORT_STAR)
2338 v = POP();
2339 PyFrame_FastToLocals(f);
2340 if ((x = f->f_locals) == NULL) {
2341 PyErr_SetString(PyExc_SystemError,
2342 "no locals found during 'import *'");
2343 break;
2344 }
2345 READ_TIMESTAMP(intr0);
2346 err = import_all_from(x, v);
2347 READ_TIMESTAMP(intr1);
2348 PyFrame_LocalsToFast(f, 0);
2349 Py_DECREF(v);
2350 if (err == 0) DISPATCH();
2351 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002353 TARGET(IMPORT_FROM)
2354 w = GETITEM(names, oparg);
2355 v = TOP();
2356 READ_TIMESTAMP(intr0);
2357 x = import_from(v, w);
2358 READ_TIMESTAMP(intr1);
2359 PUSH(x);
2360 if (x != NULL) DISPATCH();
2361 break;
Thomas Wouters52152252000-08-17 22:55:00 +00002362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002363 TARGET(JUMP_FORWARD)
2364 JUMPBY(oparg);
2365 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002366
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002367 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
2368 TARGET(POP_JUMP_IF_FALSE)
2369 w = POP();
2370 if (w == Py_True) {
2371 Py_DECREF(w);
2372 FAST_DISPATCH();
2373 }
2374 if (w == Py_False) {
2375 Py_DECREF(w);
2376 JUMPTO(oparg);
2377 FAST_DISPATCH();
2378 }
2379 err = PyObject_IsTrue(w);
2380 Py_DECREF(w);
2381 if (err > 0)
2382 err = 0;
2383 else if (err == 0)
2384 JUMPTO(oparg);
2385 else
2386 break;
2387 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002389 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
2390 TARGET(POP_JUMP_IF_TRUE)
2391 w = POP();
2392 if (w == Py_False) {
2393 Py_DECREF(w);
2394 FAST_DISPATCH();
2395 }
2396 if (w == Py_True) {
2397 Py_DECREF(w);
2398 JUMPTO(oparg);
2399 FAST_DISPATCH();
2400 }
2401 err = PyObject_IsTrue(w);
2402 Py_DECREF(w);
2403 if (err > 0) {
2404 err = 0;
2405 JUMPTO(oparg);
2406 }
2407 else if (err == 0)
2408 ;
2409 else
2410 break;
2411 DISPATCH();
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002413 TARGET(JUMP_IF_FALSE_OR_POP)
2414 w = TOP();
2415 if (w == Py_True) {
2416 STACKADJ(-1);
2417 Py_DECREF(w);
2418 FAST_DISPATCH();
2419 }
2420 if (w == Py_False) {
2421 JUMPTO(oparg);
2422 FAST_DISPATCH();
2423 }
2424 err = PyObject_IsTrue(w);
2425 if (err > 0) {
2426 STACKADJ(-1);
2427 Py_DECREF(w);
2428 err = 0;
2429 }
2430 else if (err == 0)
2431 JUMPTO(oparg);
2432 else
2433 break;
2434 DISPATCH();
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002436 TARGET(JUMP_IF_TRUE_OR_POP)
2437 w = TOP();
2438 if (w == Py_False) {
2439 STACKADJ(-1);
2440 Py_DECREF(w);
2441 FAST_DISPATCH();
2442 }
2443 if (w == Py_True) {
2444 JUMPTO(oparg);
2445 FAST_DISPATCH();
2446 }
2447 err = PyObject_IsTrue(w);
2448 if (err > 0) {
2449 err = 0;
2450 JUMPTO(oparg);
2451 }
2452 else if (err == 0) {
2453 STACKADJ(-1);
2454 Py_DECREF(w);
2455 }
2456 else
2457 break;
2458 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002460 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
2461 TARGET(JUMP_ABSOLUTE)
2462 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002463#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002464 /* Enabling this path speeds-up all while and for-loops by bypassing
2465 the per-loop checks for signals. By default, this should be turned-off
2466 because it prevents detection of a control-break in tight loops like
2467 "while 1: pass". Compile with this option turned-on when you need
2468 the speed-up and do not need break checking inside tight loops (ones
2469 that contain only instructions ending with FAST_DISPATCH).
2470 */
2471 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002472#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002473 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002474#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002475
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002476 TARGET(GET_ITER)
2477 /* before: [obj]; after [getiter(obj)] */
2478 v = TOP();
2479 x = PyObject_GetIter(v);
2480 Py_DECREF(v);
2481 if (x != NULL) {
2482 SET_TOP(x);
2483 PREDICT(FOR_ITER);
2484 DISPATCH();
2485 }
2486 STACKADJ(-1);
2487 break;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002489 PREDICTED_WITH_ARG(FOR_ITER);
2490 TARGET(FOR_ITER)
2491 /* before: [iter]; after: [iter, iter()] *or* [] */
2492 v = TOP();
2493 x = (*v->ob_type->tp_iternext)(v);
2494 if (x != NULL) {
2495 PUSH(x);
2496 PREDICT(STORE_FAST);
2497 PREDICT(UNPACK_SEQUENCE);
2498 DISPATCH();
2499 }
2500 if (PyErr_Occurred()) {
2501 if (!PyErr_ExceptionMatches(
2502 PyExc_StopIteration))
2503 break;
2504 PyErr_Clear();
2505 }
2506 /* iterator ended normally */
2507 x = v = POP();
2508 Py_DECREF(v);
2509 JUMPBY(oparg);
2510 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002512 TARGET(BREAK_LOOP)
2513 why = WHY_BREAK;
2514 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002516 TARGET(CONTINUE_LOOP)
2517 retval = PyLong_FromLong(oparg);
2518 if (!retval) {
2519 x = NULL;
2520 break;
2521 }
2522 why = WHY_CONTINUE;
2523 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002524
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002525 TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
2526 TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
2527 TARGET(SETUP_FINALLY)
2528 _setup_finally:
2529 /* NOTE: If you add any new block-setup opcodes that
2530 are not try/except/finally handlers, you may need
2531 to update the PyGen_NeedsFinalizing() function.
2532 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002533
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002534 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2535 STACK_LEVEL());
2536 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002537
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002538 TARGET(SETUP_WITH)
2539 {
2540 static PyObject *exit, *enter;
2541 w = TOP();
2542 x = special_lookup(w, "__exit__", &exit);
2543 if (!x)
2544 break;
2545 SET_TOP(x);
2546 u = special_lookup(w, "__enter__", &enter);
2547 Py_DECREF(w);
2548 if (!u) {
2549 x = NULL;
2550 break;
2551 }
2552 x = PyObject_CallFunctionObjArgs(u, NULL);
2553 Py_DECREF(u);
2554 if (!x)
2555 break;
2556 /* Setup the finally block before pushing the result
2557 of __enter__ on the stack. */
2558 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2559 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002561 PUSH(x);
2562 DISPATCH();
2563 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002564
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002565 TARGET(WITH_CLEANUP)
2566 {
2567 /* At the top of the stack are 1-3 values indicating
2568 how/why we entered the finally clause:
2569 - TOP = None
2570 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2571 - TOP = WHY_*; no retval below it
2572 - (TOP, SECOND, THIRD) = exc_info()
2573 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2574 Below them is EXIT, the context.__exit__ bound method.
2575 In the last case, we must call
2576 EXIT(TOP, SECOND, THIRD)
2577 otherwise we must call
2578 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002580 In the first two cases, we remove EXIT from the
2581 stack, leaving the rest in the same order. In the
2582 third case, we shift the bottom 3 values of the
2583 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002584
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002585 In addition, if the stack represents an exception,
2586 *and* the function call returns a 'true' value, we
2587 push WHY_SILENCED onto the stack. END_FINALLY will
2588 then not re-raise the exception. (But non-local
2589 gotos should still be resumed.)
2590 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002591
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002592 PyObject *exit_func;
2593 u = TOP();
2594 if (u == Py_None) {
2595 (void)POP();
2596 exit_func = TOP();
2597 SET_TOP(u);
2598 v = w = Py_None;
2599 }
2600 else if (PyLong_Check(u)) {
2601 (void)POP();
2602 switch(PyLong_AsLong(u)) {
2603 case WHY_RETURN:
2604 case WHY_CONTINUE:
2605 /* Retval in TOP. */
2606 exit_func = SECOND();
2607 SET_SECOND(TOP());
2608 SET_TOP(u);
2609 break;
2610 default:
2611 exit_func = TOP();
2612 SET_TOP(u);
2613 break;
2614 }
2615 u = v = w = Py_None;
2616 }
2617 else {
2618 PyObject *tp, *exc, *tb;
2619 PyTryBlock *block;
2620 v = SECOND();
2621 w = THIRD();
2622 tp = FOURTH();
2623 exc = PEEK(5);
2624 tb = PEEK(6);
2625 exit_func = PEEK(7);
2626 SET_VALUE(7, tb);
2627 SET_VALUE(6, exc);
2628 SET_VALUE(5, tp);
2629 /* UNWIND_EXCEPT_HANDLER will pop this off. */
2630 SET_FOURTH(NULL);
2631 /* We just shifted the stack down, so we have
2632 to tell the except handler block that the
2633 values are lower than it expects. */
2634 block = &f->f_blockstack[f->f_iblock - 1];
2635 assert(block->b_type == EXCEPT_HANDLER);
2636 block->b_level--;
2637 }
2638 /* XXX Not the fastest way to call it... */
2639 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2640 NULL);
2641 Py_DECREF(exit_func);
2642 if (x == NULL)
2643 break; /* Go to error exit */
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002645 if (u != Py_None)
2646 err = PyObject_IsTrue(x);
2647 else
2648 err = 0;
2649 Py_DECREF(x);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002650
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002651 if (err < 0)
2652 break; /* Go to error exit */
2653 else if (err > 0) {
2654 err = 0;
2655 /* There was an exception and a True return */
2656 PUSH(PyLong_FromLong((long) WHY_SILENCED));
2657 }
2658 PREDICT(END_FINALLY);
2659 break;
2660 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002661
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002662 TARGET(CALL_FUNCTION)
2663 {
2664 PyObject **sp;
2665 PCALL(PCALL_ALL);
2666 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002667#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002668 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002669#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002670 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002671#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002672 stack_pointer = sp;
2673 PUSH(x);
2674 if (x != NULL)
2675 DISPATCH();
2676 break;
2677 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002679 TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw)
2680 TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw)
2681 TARGET(CALL_FUNCTION_VAR_KW)
2682 _call_function_var_kw:
2683 {
2684 int na = oparg & 0xff;
2685 int nk = (oparg>>8) & 0xff;
2686 int flags = (opcode - CALL_FUNCTION) & 3;
2687 int n = na + 2 * nk;
2688 PyObject **pfunc, *func, **sp;
2689 PCALL(PCALL_ALL);
2690 if (flags & CALL_FLAG_VAR)
2691 n++;
2692 if (flags & CALL_FLAG_KW)
2693 n++;
2694 pfunc = stack_pointer - n - 1;
2695 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002696
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002697 if (PyMethod_Check(func)
2698 && PyMethod_GET_SELF(func) != NULL) {
2699 PyObject *self = PyMethod_GET_SELF(func);
2700 Py_INCREF(self);
2701 func = PyMethod_GET_FUNCTION(func);
2702 Py_INCREF(func);
2703 Py_DECREF(*pfunc);
2704 *pfunc = self;
2705 na++;
2706 n++;
2707 } else
2708 Py_INCREF(func);
2709 sp = stack_pointer;
2710 READ_TIMESTAMP(intr0);
2711 x = ext_do_call(func, &sp, flags, na, nk);
2712 READ_TIMESTAMP(intr1);
2713 stack_pointer = sp;
2714 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002715
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002716 while (stack_pointer > pfunc) {
2717 w = POP();
2718 Py_DECREF(w);
2719 }
2720 PUSH(x);
2721 if (x != NULL)
2722 DISPATCH();
2723 break;
2724 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002725
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002726 TARGET_WITH_IMPL(MAKE_CLOSURE, _make_function)
2727 TARGET(MAKE_FUNCTION)
2728 _make_function:
2729 {
2730 int posdefaults = oparg & 0xff;
2731 int kwdefaults = (oparg>>8) & 0xff;
2732 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002733
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002734 v = POP(); /* code object */
2735 x = PyFunction_New(v, f->f_globals);
2736 Py_DECREF(v);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002737
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002738 if (x != NULL && opcode == MAKE_CLOSURE) {
2739 v = POP();
2740 if (PyFunction_SetClosure(x, v) != 0) {
2741 /* Can't happen unless bytecode is corrupt. */
2742 why = WHY_EXCEPTION;
2743 }
2744 Py_DECREF(v);
2745 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002746
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002747 if (x != NULL && num_annotations > 0) {
2748 Py_ssize_t name_ix;
2749 u = POP(); /* names of args with annotations */
2750 v = PyDict_New();
2751 if (v == NULL) {
2752 Py_DECREF(x);
2753 x = NULL;
2754 break;
2755 }
2756 name_ix = PyTuple_Size(u);
2757 assert(num_annotations == name_ix+1);
2758 while (name_ix > 0) {
2759 --name_ix;
2760 t = PyTuple_GET_ITEM(u, name_ix);
2761 w = POP();
2762 /* XXX(nnorwitz): check for errors */
2763 PyDict_SetItem(v, t, w);
2764 Py_DECREF(w);
2765 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002766
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002767 if (PyFunction_SetAnnotations(x, v) != 0) {
2768 /* Can't happen unless
2769 PyFunction_SetAnnotations changes. */
2770 why = WHY_EXCEPTION;
2771 }
2772 Py_DECREF(v);
2773 Py_DECREF(u);
2774 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002775
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002776 /* XXX Maybe this should be a separate opcode? */
2777 if (x != NULL && posdefaults > 0) {
2778 v = PyTuple_New(posdefaults);
2779 if (v == NULL) {
2780 Py_DECREF(x);
2781 x = NULL;
2782 break;
2783 }
2784 while (--posdefaults >= 0) {
2785 w = POP();
2786 PyTuple_SET_ITEM(v, posdefaults, w);
2787 }
2788 if (PyFunction_SetDefaults(x, v) != 0) {
2789 /* Can't happen unless
2790 PyFunction_SetDefaults changes. */
2791 why = WHY_EXCEPTION;
2792 }
2793 Py_DECREF(v);
2794 }
2795 if (x != NULL && kwdefaults > 0) {
2796 v = PyDict_New();
2797 if (v == NULL) {
2798 Py_DECREF(x);
2799 x = NULL;
2800 break;
2801 }
2802 while (--kwdefaults >= 0) {
2803 w = POP(); /* default value */
2804 u = POP(); /* kw only arg name */
2805 /* XXX(nnorwitz): check for errors */
2806 PyDict_SetItem(v, u, w);
2807 Py_DECREF(w);
2808 Py_DECREF(u);
2809 }
2810 if (PyFunction_SetKwDefaults(x, v) != 0) {
2811 /* Can't happen unless
2812 PyFunction_SetKwDefaults changes. */
2813 why = WHY_EXCEPTION;
2814 }
2815 Py_DECREF(v);
2816 }
2817 PUSH(x);
2818 break;
2819 }
Guido van Rossum8861b741996-07-30 16:49:37 +00002820
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002821 TARGET(BUILD_SLICE)
2822 if (oparg == 3)
2823 w = POP();
2824 else
2825 w = NULL;
2826 v = POP();
2827 u = TOP();
2828 x = PySlice_New(u, v, w);
2829 Py_DECREF(u);
2830 Py_DECREF(v);
2831 Py_XDECREF(w);
2832 SET_TOP(x);
2833 if (x != NULL) DISPATCH();
2834 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002835
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002836 TARGET(EXTENDED_ARG)
2837 opcode = NEXTOP();
2838 oparg = oparg<<16 | NEXTARG();
2839 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002840
Antoine Pitroub52ec782009-01-25 16:34:23 +00002841#ifdef USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002842 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00002843#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002844 default:
2845 fprintf(stderr,
2846 "XXX lineno: %d, opcode: %d\n",
2847 PyFrame_GetLineNumber(f),
2848 opcode);
2849 PyErr_SetString(PyExc_SystemError, "unknown opcode");
2850 why = WHY_EXCEPTION;
2851 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002852
2853#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002854 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002855#endif
2856
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002857 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00002858
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002859 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002860
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002861 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002863 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002864
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002865 if (why == WHY_NOT) {
2866 if (err == 0 && x != NULL) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002867#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002868 /* This check is expensive! */
2869 if (PyErr_Occurred())
2870 fprintf(stderr,
2871 "XXX undetected error\n");
2872 else {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002873#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002874 READ_TIMESTAMP(loop1);
2875 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002876#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002877 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002878#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002879 }
2880 why = WHY_EXCEPTION;
2881 x = Py_None;
2882 err = 0;
2883 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002884
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002885 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002886
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002887 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
2888 if (!PyErr_Occurred()) {
2889 PyErr_SetString(PyExc_SystemError,
2890 "error return without exception set");
2891 why = WHY_EXCEPTION;
2892 }
2893 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002894#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002895 else {
2896 /* This check is expensive! */
2897 if (PyErr_Occurred()) {
2898 char buf[128];
2899 sprintf(buf, "Stack unwind with exception "
2900 "set and why=%d", why);
2901 Py_FatalError(buf);
2902 }
2903 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002904#endif
2905
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002906 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002907
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002908 if (why == WHY_EXCEPTION) {
2909 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002910
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002911 if (tstate->c_tracefunc != NULL)
2912 call_exc_trace(tstate->c_tracefunc,
2913 tstate->c_traceobj, f);
2914 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002915
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002916 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002917
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002918 if (why == WHY_RERAISE)
2919 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +00002920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002921 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002922
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002923fast_block_end:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002924 while (why != WHY_NOT && f->f_iblock > 0) {
2925 /* Peek at the current block. */
2926 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002927
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002928 assert(why != WHY_YIELD);
2929 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2930 why = WHY_NOT;
2931 JUMPTO(PyLong_AS_LONG(retval));
2932 Py_DECREF(retval);
2933 break;
2934 }
2935 /* Now we have to pop the block. */
2936 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002937
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002938 if (b->b_type == EXCEPT_HANDLER) {
2939 UNWIND_EXCEPT_HANDLER(b);
2940 continue;
2941 }
2942 UNWIND_BLOCK(b);
2943 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2944 why = WHY_NOT;
2945 JUMPTO(b->b_handler);
2946 break;
2947 }
2948 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
2949 || b->b_type == SETUP_FINALLY)) {
2950 PyObject *exc, *val, *tb;
2951 int handler = b->b_handler;
2952 /* Beware, this invalidates all b->b_* fields */
2953 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
2954 PUSH(tstate->exc_traceback);
2955 PUSH(tstate->exc_value);
2956 if (tstate->exc_type != NULL) {
2957 PUSH(tstate->exc_type);
2958 }
2959 else {
2960 Py_INCREF(Py_None);
2961 PUSH(Py_None);
2962 }
2963 PyErr_Fetch(&exc, &val, &tb);
2964 /* Make the raw exception data
2965 available to the handler,
2966 so a program can emulate the
2967 Python main loop. */
2968 PyErr_NormalizeException(
2969 &exc, &val, &tb);
2970 PyException_SetTraceback(val, tb);
2971 Py_INCREF(exc);
2972 tstate->exc_type = exc;
2973 Py_INCREF(val);
2974 tstate->exc_value = val;
2975 tstate->exc_traceback = tb;
2976 if (tb == NULL)
2977 tb = Py_None;
2978 Py_INCREF(tb);
2979 PUSH(tb);
2980 PUSH(val);
2981 PUSH(exc);
2982 why = WHY_NOT;
2983 JUMPTO(handler);
2984 break;
2985 }
2986 if (b->b_type == SETUP_FINALLY) {
2987 if (why & (WHY_RETURN | WHY_CONTINUE))
2988 PUSH(retval);
2989 PUSH(PyLong_FromLong((long)why));
2990 why = WHY_NOT;
2991 JUMPTO(b->b_handler);
2992 break;
2993 }
2994 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00002995
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002996 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002997
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002998 if (why != WHY_NOT)
2999 break;
3000 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00003001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003002 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003003
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003004 assert(why != WHY_YIELD);
3005 /* Pop remaining stack entries. */
3006 while (!EMPTY()) {
3007 v = POP();
3008 Py_XDECREF(v);
3009 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003010
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003011 if (why != WHY_RETURN)
3012 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003013
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003014fast_yield:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003015 if (tstate->use_tracing) {
3016 if (tstate->c_tracefunc) {
3017 if (why == WHY_RETURN || why == WHY_YIELD) {
3018 if (call_trace(tstate->c_tracefunc,
3019 tstate->c_traceobj, f,
3020 PyTrace_RETURN, retval)) {
3021 Py_XDECREF(retval);
3022 retval = NULL;
3023 why = WHY_EXCEPTION;
3024 }
3025 }
3026 else if (why == WHY_EXCEPTION) {
3027 call_trace_protected(tstate->c_tracefunc,
3028 tstate->c_traceobj, f,
3029 PyTrace_RETURN, NULL);
3030 }
3031 }
3032 if (tstate->c_profilefunc) {
3033 if (why == WHY_EXCEPTION)
3034 call_trace_protected(tstate->c_profilefunc,
3035 tstate->c_profileobj, f,
3036 PyTrace_RETURN, NULL);
3037 else if (call_trace(tstate->c_profilefunc,
3038 tstate->c_profileobj, f,
3039 PyTrace_RETURN, retval)) {
3040 Py_XDECREF(retval);
3041 retval = NULL;
3042 why = WHY_EXCEPTION;
3043 }
3044 }
3045 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003046
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003047 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003048exit_eval_frame:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003049 Py_LeaveRecursiveCall();
3050 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003052 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00003053}
3054
Guido van Rossumc2e20742006-02-27 22:32:47 +00003055/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003056 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003057 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003058
Tim Peters6d6c1a32001-08-02 04:15:00 +00003059PyObject *
3060PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003061 PyObject **args, int argcount, PyObject **kws, int kwcount,
3062 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00003063{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003064 register PyFrameObject *f;
3065 register PyObject *retval = NULL;
3066 register PyObject **fastlocals, **freevars;
3067 PyThreadState *tstate = PyThreadState_GET();
3068 PyObject *x, *u;
3069 int total_args = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00003070
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003071 if (globals == NULL) {
3072 PyErr_SetString(PyExc_SystemError,
3073 "PyEval_EvalCodeEx: NULL globals");
3074 return NULL;
3075 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003076
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003077 assert(tstate != NULL);
3078 assert(globals != NULL);
3079 f = PyFrame_New(tstate, co, globals, locals);
3080 if (f == NULL)
3081 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003082
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003083 fastlocals = f->f_localsplus;
3084 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003085
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003086 if (total_args || co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
3087 int i;
3088 int n = argcount;
3089 PyObject *kwdict = NULL;
3090 if (co->co_flags & CO_VARKEYWORDS) {
3091 kwdict = PyDict_New();
3092 if (kwdict == NULL)
3093 goto fail;
3094 i = total_args;
3095 if (co->co_flags & CO_VARARGS)
3096 i++;
3097 SETLOCAL(i, kwdict);
3098 }
3099 if (argcount > co->co_argcount) {
3100 if (!(co->co_flags & CO_VARARGS)) {
3101 PyErr_Format(PyExc_TypeError,
3102 "%U() takes %s %d "
3103 "argument%s (%d given)",
3104 co->co_name,
3105 defcount ? "at most" : "exactly",
3106 total_args,
3107 total_args == 1 ? "" : "s",
3108 argcount + kwcount);
3109 goto fail;
3110 }
3111 n = co->co_argcount;
3112 }
3113 for (i = 0; i < n; i++) {
3114 x = args[i];
3115 Py_INCREF(x);
3116 SETLOCAL(i, x);
3117 }
3118 if (co->co_flags & CO_VARARGS) {
3119 u = PyTuple_New(argcount - n);
3120 if (u == NULL)
3121 goto fail;
3122 SETLOCAL(total_args, u);
3123 for (i = n; i < argcount; i++) {
3124 x = args[i];
3125 Py_INCREF(x);
3126 PyTuple_SET_ITEM(u, i-n, x);
3127 }
3128 }
3129 for (i = 0; i < kwcount; i++) {
3130 PyObject **co_varnames;
3131 PyObject *keyword = kws[2*i];
3132 PyObject *value = kws[2*i + 1];
3133 int j;
3134 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3135 PyErr_Format(PyExc_TypeError,
3136 "%U() keywords must be strings",
3137 co->co_name);
3138 goto fail;
3139 }
3140 /* Speed hack: do raw pointer compares. As names are
3141 normally interned this should almost always hit. */
3142 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3143 for (j = 0; j < total_args; j++) {
3144 PyObject *nm = co_varnames[j];
3145 if (nm == keyword)
3146 goto kw_found;
3147 }
3148 /* Slow fallback, just in case */
3149 for (j = 0; j < total_args; j++) {
3150 PyObject *nm = co_varnames[j];
3151 int cmp = PyObject_RichCompareBool(
3152 keyword, nm, Py_EQ);
3153 if (cmp > 0)
3154 goto kw_found;
3155 else if (cmp < 0)
3156 goto fail;
3157 }
3158 if (j >= total_args && kwdict == NULL) {
3159 PyErr_Format(PyExc_TypeError,
3160 "%U() got an unexpected "
3161 "keyword argument '%S'",
3162 co->co_name,
3163 keyword);
3164 goto fail;
3165 }
3166 PyDict_SetItem(kwdict, keyword, value);
3167 continue;
3168 kw_found:
3169 if (GETLOCAL(j) != NULL) {
3170 PyErr_Format(PyExc_TypeError,
3171 "%U() got multiple "
3172 "values for keyword "
3173 "argument '%S'",
3174 co->co_name,
3175 keyword);
3176 goto fail;
3177 }
3178 Py_INCREF(value);
3179 SETLOCAL(j, value);
3180 }
3181 if (co->co_kwonlyargcount > 0) {
3182 for (i = co->co_argcount; i < total_args; i++) {
3183 PyObject *name;
3184 if (GETLOCAL(i) != NULL)
3185 continue;
3186 name = PyTuple_GET_ITEM(co->co_varnames, i);
3187 if (kwdefs != NULL) {
3188 PyObject *def = PyDict_GetItem(kwdefs, name);
3189 if (def) {
3190 Py_INCREF(def);
3191 SETLOCAL(i, def);
3192 continue;
3193 }
3194 }
3195 PyErr_Format(PyExc_TypeError,
3196 "%U() needs keyword-only argument %S",
3197 co->co_name, name);
3198 goto fail;
3199 }
3200 }
3201 if (argcount < co->co_argcount) {
3202 int m = co->co_argcount - defcount;
3203 for (i = argcount; i < m; i++) {
3204 if (GETLOCAL(i) == NULL) {
3205 int j, given = 0;
3206 for (j = 0; j < co->co_argcount; j++)
3207 if (GETLOCAL(j))
3208 given++;
3209 PyErr_Format(PyExc_TypeError,
3210 "%U() takes %s %d "
3211 "argument%s "
3212 "(%d given)",
3213 co->co_name,
3214 ((co->co_flags & CO_VARARGS) ||
3215 defcount) ? "at least"
3216 : "exactly",
3217 m, m == 1 ? "" : "s", given);
3218 goto fail;
3219 }
3220 }
3221 if (n > m)
3222 i = n - m;
3223 else
3224 i = 0;
3225 for (; i < defcount; i++) {
3226 if (GETLOCAL(m+i) == NULL) {
3227 PyObject *def = defs[i];
3228 Py_INCREF(def);
3229 SETLOCAL(m+i, def);
3230 }
3231 }
3232 }
3233 }
3234 else if (argcount > 0 || kwcount > 0) {
3235 PyErr_Format(PyExc_TypeError,
3236 "%U() takes no arguments (%d given)",
3237 co->co_name,
3238 argcount + kwcount);
3239 goto fail;
3240 }
3241 /* Allocate and initialize storage for cell vars, and copy free
3242 vars into frame. This isn't too efficient right now. */
3243 if (PyTuple_GET_SIZE(co->co_cellvars)) {
3244 int i, j, nargs, found;
3245 Py_UNICODE *cellname, *argname;
3246 PyObject *c;
Tim Peters5ca576e2001-06-18 22:08:13 +00003247
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003248 nargs = total_args;
3249 if (co->co_flags & CO_VARARGS)
3250 nargs++;
3251 if (co->co_flags & CO_VARKEYWORDS)
3252 nargs++;
Tim Peters5ca576e2001-06-18 22:08:13 +00003253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003254 /* Initialize each cell var, taking into account
3255 cell vars that are initialized from arguments.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003257 Should arrange for the compiler to put cellvars
3258 that are arguments at the beginning of the cellvars
3259 list so that we can march over it more efficiently?
3260 */
3261 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
3262 cellname = PyUnicode_AS_UNICODE(
3263 PyTuple_GET_ITEM(co->co_cellvars, i));
3264 found = 0;
3265 for (j = 0; j < nargs; j++) {
3266 argname = PyUnicode_AS_UNICODE(
3267 PyTuple_GET_ITEM(co->co_varnames, j));
3268 if (Py_UNICODE_strcmp(cellname, argname) == 0) {
3269 c = PyCell_New(GETLOCAL(j));
3270 if (c == NULL)
3271 goto fail;
3272 GETLOCAL(co->co_nlocals + i) = c;
3273 found = 1;
3274 break;
3275 }
3276 }
3277 if (found == 0) {
3278 c = PyCell_New(NULL);
3279 if (c == NULL)
3280 goto fail;
3281 SETLOCAL(co->co_nlocals + i, c);
3282 }
3283 }
3284 }
3285 if (PyTuple_GET_SIZE(co->co_freevars)) {
3286 int i;
3287 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3288 PyObject *o = PyTuple_GET_ITEM(closure, i);
3289 Py_INCREF(o);
3290 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
3291 }
3292 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003294 if (co->co_flags & CO_GENERATOR) {
3295 /* Don't need to keep the reference to f_back, it will be set
3296 * when the generator is resumed. */
3297 Py_XDECREF(f->f_back);
3298 f->f_back = NULL;
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003299
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003300 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003302 /* Create a new generator that owns the ready to run frame
3303 * and return that as the value. */
3304 return PyGen_New(f);
3305 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003307 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003308
Thomas Woutersce272b62007-09-19 21:19:28 +00003309fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003311 /* decref'ing the frame can cause __del__ methods to get invoked,
3312 which can call back into Python. While we're done with the
3313 current Python frame (f), the associated C stack is still in use,
3314 so recursion_depth must be boosted for the duration.
3315 */
3316 assert(tstate != NULL);
3317 ++tstate->recursion_depth;
3318 Py_DECREF(f);
3319 --tstate->recursion_depth;
3320 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003321}
3322
3323
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003324static PyObject *
3325special_lookup(PyObject *o, char *meth, PyObject **cache)
3326{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003327 PyObject *res;
3328 res = _PyObject_LookupSpecial(o, meth, cache);
3329 if (res == NULL && !PyErr_Occurred()) {
3330 PyErr_SetObject(PyExc_AttributeError, *cache);
3331 return NULL;
3332 }
3333 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003334}
3335
3336
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003337/* Logic for the raise statement (too complicated for inlining).
3338 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00003339static enum why_code
Collin Winter828f04a2007-08-31 00:04:24 +00003340do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003341{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003342 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003344 if (exc == NULL) {
3345 /* Reraise */
3346 PyThreadState *tstate = PyThreadState_GET();
3347 PyObject *tb;
3348 type = tstate->exc_type;
3349 value = tstate->exc_value;
3350 tb = tstate->exc_traceback;
3351 if (type == Py_None) {
3352 PyErr_SetString(PyExc_RuntimeError,
3353 "No active exception to reraise");
3354 return WHY_EXCEPTION;
3355 }
3356 Py_XINCREF(type);
3357 Py_XINCREF(value);
3358 Py_XINCREF(tb);
3359 PyErr_Restore(type, value, tb);
3360 return WHY_RERAISE;
3361 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003363 /* We support the following forms of raise:
3364 raise
Collin Winter828f04a2007-08-31 00:04:24 +00003365 raise <instance>
3366 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003368 if (PyExceptionClass_Check(exc)) {
3369 type = exc;
3370 value = PyObject_CallObject(exc, NULL);
3371 if (value == NULL)
3372 goto raise_error;
3373 }
3374 else if (PyExceptionInstance_Check(exc)) {
3375 value = exc;
3376 type = PyExceptionInstance_Class(exc);
3377 Py_INCREF(type);
3378 }
3379 else {
3380 /* Not something you can raise. You get an exception
3381 anyway, just not what you specified :-) */
3382 Py_DECREF(exc);
3383 PyErr_SetString(PyExc_TypeError,
3384 "exceptions must derive from BaseException");
3385 goto raise_error;
3386 }
Collin Winter828f04a2007-08-31 00:04:24 +00003387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003388 if (cause) {
3389 PyObject *fixed_cause;
3390 if (PyExceptionClass_Check(cause)) {
3391 fixed_cause = PyObject_CallObject(cause, NULL);
3392 if (fixed_cause == NULL)
3393 goto raise_error;
3394 Py_DECREF(cause);
3395 }
3396 else if (PyExceptionInstance_Check(cause)) {
3397 fixed_cause = cause;
3398 }
3399 else {
3400 PyErr_SetString(PyExc_TypeError,
3401 "exception causes must derive from "
3402 "BaseException");
3403 goto raise_error;
3404 }
3405 PyException_SetCause(value, fixed_cause);
3406 }
Collin Winter828f04a2007-08-31 00:04:24 +00003407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003408 PyErr_SetObject(type, value);
3409 /* PyErr_SetObject incref's its arguments */
3410 Py_XDECREF(value);
3411 Py_XDECREF(type);
3412 return WHY_EXCEPTION;
Collin Winter828f04a2007-08-31 00:04:24 +00003413
3414raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003415 Py_XDECREF(value);
3416 Py_XDECREF(type);
3417 Py_XDECREF(cause);
3418 return WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003419}
3420
Tim Petersd6d010b2001-06-21 02:49:55 +00003421/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00003422 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003423
Guido van Rossum0368b722007-05-11 16:50:42 +00003424 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
3425 with a variable target.
3426*/
Tim Petersd6d010b2001-06-21 02:49:55 +00003427
Barry Warsawe42b18f1997-08-25 22:13:04 +00003428static int
Guido van Rossum0368b722007-05-11 16:50:42 +00003429unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003430{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003431 int i = 0, j = 0;
3432 Py_ssize_t ll = 0;
3433 PyObject *it; /* iter(v) */
3434 PyObject *w;
3435 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00003436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003437 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00003438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003439 it = PyObject_GetIter(v);
3440 if (it == NULL)
3441 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00003442
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003443 for (; i < argcnt; i++) {
3444 w = PyIter_Next(it);
3445 if (w == NULL) {
3446 /* Iterator done, via error or exhaustion. */
3447 if (!PyErr_Occurred()) {
3448 PyErr_Format(PyExc_ValueError,
3449 "need more than %d value%s to unpack",
3450 i, i == 1 ? "" : "s");
3451 }
3452 goto Error;
3453 }
3454 *--sp = w;
3455 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003457 if (argcntafter == -1) {
3458 /* We better have exhausted the iterator now. */
3459 w = PyIter_Next(it);
3460 if (w == NULL) {
3461 if (PyErr_Occurred())
3462 goto Error;
3463 Py_DECREF(it);
3464 return 1;
3465 }
3466 Py_DECREF(w);
3467 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
3468 goto Error;
3469 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003471 l = PySequence_List(it);
3472 if (l == NULL)
3473 goto Error;
3474 *--sp = l;
3475 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00003476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003477 ll = PyList_GET_SIZE(l);
3478 if (ll < argcntafter) {
3479 PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack",
3480 argcnt + ll);
3481 goto Error;
3482 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003484 /* Pop the "after-variable" args off the list. */
3485 for (j = argcntafter; j > 0; j--, i++) {
3486 *--sp = PyList_GET_ITEM(l, ll - j);
3487 }
3488 /* Resize the list. */
3489 Py_SIZE(l) = ll - argcntafter;
3490 Py_DECREF(it);
3491 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00003492
Tim Petersd6d010b2001-06-21 02:49:55 +00003493Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003494 for (; i > 0; i--, sp++)
3495 Py_DECREF(*sp);
3496 Py_XDECREF(it);
3497 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003498}
3499
3500
Guido van Rossum96a42c81992-01-12 02:29:51 +00003501#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003502static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003503prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003504{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003505 printf("%s ", str);
3506 if (PyObject_Print(v, stdout, 0) != 0)
3507 PyErr_Clear(); /* Don't know what else to do */
3508 printf("\n");
3509 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003510}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003511#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003512
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003513static void
Fred Drake5755ce62001-06-27 19:19:46 +00003514call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003515{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003516 PyObject *type, *value, *traceback, *arg;
3517 int err;
3518 PyErr_Fetch(&type, &value, &traceback);
3519 if (value == NULL) {
3520 value = Py_None;
3521 Py_INCREF(value);
3522 }
3523 arg = PyTuple_Pack(3, type, value, traceback);
3524 if (arg == NULL) {
3525 PyErr_Restore(type, value, traceback);
3526 return;
3527 }
3528 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
3529 Py_DECREF(arg);
3530 if (err == 0)
3531 PyErr_Restore(type, value, traceback);
3532 else {
3533 Py_XDECREF(type);
3534 Py_XDECREF(value);
3535 Py_XDECREF(traceback);
3536 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003537}
3538
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003539static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003540call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003541 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003542{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003543 PyObject *type, *value, *traceback;
3544 int err;
3545 PyErr_Fetch(&type, &value, &traceback);
3546 err = call_trace(func, obj, frame, what, arg);
3547 if (err == 0)
3548 {
3549 PyErr_Restore(type, value, traceback);
3550 return 0;
3551 }
3552 else {
3553 Py_XDECREF(type);
3554 Py_XDECREF(value);
3555 Py_XDECREF(traceback);
3556 return -1;
3557 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003558}
3559
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003560static int
Fred Drake5755ce62001-06-27 19:19:46 +00003561call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003562 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003563{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003564 register PyThreadState *tstate = frame->f_tstate;
3565 int result;
3566 if (tstate->tracing)
3567 return 0;
3568 tstate->tracing++;
3569 tstate->use_tracing = 0;
3570 result = func(obj, frame, what, arg);
3571 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3572 || (tstate->c_profilefunc != NULL));
3573 tstate->tracing--;
3574 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003575}
3576
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003577PyObject *
3578_PyEval_CallTracing(PyObject *func, PyObject *args)
3579{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003580 PyFrameObject *frame = PyEval_GetFrame();
3581 PyThreadState *tstate = frame->f_tstate;
3582 int save_tracing = tstate->tracing;
3583 int save_use_tracing = tstate->use_tracing;
3584 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003586 tstate->tracing = 0;
3587 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3588 || (tstate->c_profilefunc != NULL));
3589 result = PyObject_Call(func, args, NULL);
3590 tstate->tracing = save_tracing;
3591 tstate->use_tracing = save_use_tracing;
3592 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003593}
3594
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003595/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00003596static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003597maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003598 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3599 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003600{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003601 int result = 0;
3602 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003603
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003604 /* If the last instruction executed isn't in the current
3605 instruction window, reset the window.
3606 */
3607 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
3608 PyAddrPair bounds;
3609 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3610 &bounds);
3611 *instr_lb = bounds.ap_lower;
3612 *instr_ub = bounds.ap_upper;
3613 }
3614 /* If the last instruction falls at the start of a line or if
3615 it represents a jump backwards, update the frame's line
3616 number and call the trace function. */
3617 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
3618 frame->f_lineno = line;
3619 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
3620 }
3621 *instr_prev = frame->f_lasti;
3622 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003623}
3624
Fred Drake5755ce62001-06-27 19:19:46 +00003625void
3626PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003627{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003628 PyThreadState *tstate = PyThreadState_GET();
3629 PyObject *temp = tstate->c_profileobj;
3630 Py_XINCREF(arg);
3631 tstate->c_profilefunc = NULL;
3632 tstate->c_profileobj = NULL;
3633 /* Must make sure that tracing is not ignored if 'temp' is freed */
3634 tstate->use_tracing = tstate->c_tracefunc != NULL;
3635 Py_XDECREF(temp);
3636 tstate->c_profilefunc = func;
3637 tstate->c_profileobj = arg;
3638 /* Flag that tracing or profiling is turned on */
3639 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003640}
3641
3642void
3643PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3644{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003645 PyThreadState *tstate = PyThreadState_GET();
3646 PyObject *temp = tstate->c_traceobj;
3647 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
3648 Py_XINCREF(arg);
3649 tstate->c_tracefunc = NULL;
3650 tstate->c_traceobj = NULL;
3651 /* Must make sure that profiling is not ignored if 'temp' is freed */
3652 tstate->use_tracing = tstate->c_profilefunc != NULL;
3653 Py_XDECREF(temp);
3654 tstate->c_tracefunc = func;
3655 tstate->c_traceobj = arg;
3656 /* Flag that tracing or profiling is turned on */
3657 tstate->use_tracing = ((func != NULL)
3658 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003659}
3660
Guido van Rossumb209a111997-04-29 18:18:01 +00003661PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003662PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003663{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003664 PyFrameObject *current_frame = PyEval_GetFrame();
3665 if (current_frame == NULL)
3666 return PyThreadState_GET()->interp->builtins;
3667 else
3668 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003669}
3670
Guido van Rossumb209a111997-04-29 18:18:01 +00003671PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003672PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003673{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003674 PyFrameObject *current_frame = PyEval_GetFrame();
3675 if (current_frame == NULL)
3676 return NULL;
3677 PyFrame_FastToLocals(current_frame);
3678 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00003679}
3680
Guido van Rossumb209a111997-04-29 18:18:01 +00003681PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003682PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003683{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003684 PyFrameObject *current_frame = PyEval_GetFrame();
3685 if (current_frame == NULL)
3686 return NULL;
3687 else
3688 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003689}
3690
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003691PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003692PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003693{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003694 PyThreadState *tstate = PyThreadState_GET();
3695 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003696}
3697
Guido van Rossum6135a871995-01-09 17:53:26 +00003698int
Tim Peters5ba58662001-07-16 02:29:45 +00003699PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003700{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003701 PyFrameObject *current_frame = PyEval_GetFrame();
3702 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003703
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003704 if (current_frame != NULL) {
3705 const int codeflags = current_frame->f_code->co_flags;
3706 const int compilerflags = codeflags & PyCF_MASK;
3707 if (compilerflags) {
3708 result = 1;
3709 cf->cf_flags |= compilerflags;
3710 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003711#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003712 if (codeflags & CO_GENERATOR_ALLOWED) {
3713 result = 1;
3714 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3715 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003716#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003717 }
3718 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003719}
3720
Guido van Rossum3f5da241990-12-20 15:06:42 +00003721
Guido van Rossum681d79a1995-07-18 14:51:37 +00003722/* External interface to call any callable object.
Antoine Pitrou8689a102010-04-01 16:53:15 +00003723 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00003724
Guido van Rossumb209a111997-04-29 18:18:01 +00003725PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003726PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003727{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003728 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003729
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003730 if (arg == NULL) {
3731 arg = PyTuple_New(0);
3732 if (arg == NULL)
3733 return NULL;
3734 }
3735 else if (!PyTuple_Check(arg)) {
3736 PyErr_SetString(PyExc_TypeError,
3737 "argument list must be a tuple");
3738 return NULL;
3739 }
3740 else
3741 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003743 if (kw != NULL && !PyDict_Check(kw)) {
3744 PyErr_SetString(PyExc_TypeError,
3745 "keyword list must be a dictionary");
3746 Py_DECREF(arg);
3747 return NULL;
3748 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00003749
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003750 result = PyObject_Call(func, arg, kw);
3751 Py_DECREF(arg);
3752 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00003753}
3754
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003755const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003756PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003757{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003758 if (PyMethod_Check(func))
3759 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
3760 else if (PyFunction_Check(func))
3761 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
3762 else if (PyCFunction_Check(func))
3763 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3764 else
3765 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00003766}
3767
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003768const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003769PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003770{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003771 if (PyMethod_Check(func))
3772 return "()";
3773 else if (PyFunction_Check(func))
3774 return "()";
3775 else if (PyCFunction_Check(func))
3776 return "()";
3777 else
3778 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00003779}
3780
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003781static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003782err_args(PyObject *func, int flags, int nargs)
3783{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003784 if (flags & METH_NOARGS)
3785 PyErr_Format(PyExc_TypeError,
3786 "%.200s() takes no arguments (%d given)",
3787 ((PyCFunctionObject *)func)->m_ml->ml_name,
3788 nargs);
3789 else
3790 PyErr_Format(PyExc_TypeError,
3791 "%.200s() takes exactly one argument (%d given)",
3792 ((PyCFunctionObject *)func)->m_ml->ml_name,
3793 nargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003794}
3795
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003796#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003797if (tstate->use_tracing && tstate->c_profilefunc) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003798 if (call_trace(tstate->c_profilefunc, \
3799 tstate->c_profileobj, \
3800 tstate->frame, PyTrace_C_CALL, \
3801 func)) { \
3802 x = NULL; \
3803 } \
3804 else { \
3805 x = call; \
3806 if (tstate->c_profilefunc != NULL) { \
3807 if (x == NULL) { \
3808 call_trace_protected(tstate->c_profilefunc, \
3809 tstate->c_profileobj, \
3810 tstate->frame, PyTrace_C_EXCEPTION, \
3811 func); \
3812 /* XXX should pass (type, value, tb) */ \
3813 } else { \
3814 if (call_trace(tstate->c_profilefunc, \
3815 tstate->c_profileobj, \
3816 tstate->frame, PyTrace_C_RETURN, \
3817 func)) { \
3818 Py_DECREF(x); \
3819 x = NULL; \
3820 } \
3821 } \
3822 } \
3823 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003824} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003825 x = call; \
3826 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003827
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003828static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003829call_function(PyObject ***pp_stack, int oparg
3830#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003831 , uint64* pintr0, uint64* pintr1
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003832#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003833 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003834{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003835 int na = oparg & 0xff;
3836 int nk = (oparg>>8) & 0xff;
3837 int n = na + 2 * nk;
3838 PyObject **pfunc = (*pp_stack) - n - 1;
3839 PyObject *func = *pfunc;
3840 PyObject *x, *w;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003841
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003842 /* Always dispatch PyCFunction first, because these are
3843 presumed to be the most frequent callable object.
3844 */
3845 if (PyCFunction_Check(func) && nk == 0) {
3846 int flags = PyCFunction_GET_FLAGS(func);
3847 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003848
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003849 PCALL(PCALL_CFUNCTION);
3850 if (flags & (METH_NOARGS | METH_O)) {
3851 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3852 PyObject *self = PyCFunction_GET_SELF(func);
3853 if (flags & METH_NOARGS && na == 0) {
3854 C_TRACE(x, (*meth)(self,NULL));
3855 }
3856 else if (flags & METH_O && na == 1) {
3857 PyObject *arg = EXT_POP(*pp_stack);
3858 C_TRACE(x, (*meth)(self,arg));
3859 Py_DECREF(arg);
3860 }
3861 else {
3862 err_args(func, flags, na);
3863 x = NULL;
3864 }
3865 }
3866 else {
3867 PyObject *callargs;
3868 callargs = load_args(pp_stack, na);
3869 READ_TIMESTAMP(*pintr0);
3870 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
3871 READ_TIMESTAMP(*pintr1);
3872 Py_XDECREF(callargs);
3873 }
3874 } else {
3875 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3876 /* optimize access to bound methods */
3877 PyObject *self = PyMethod_GET_SELF(func);
3878 PCALL(PCALL_METHOD);
3879 PCALL(PCALL_BOUND_METHOD);
3880 Py_INCREF(self);
3881 func = PyMethod_GET_FUNCTION(func);
3882 Py_INCREF(func);
3883 Py_DECREF(*pfunc);
3884 *pfunc = self;
3885 na++;
3886 n++;
3887 } else
3888 Py_INCREF(func);
3889 READ_TIMESTAMP(*pintr0);
3890 if (PyFunction_Check(func))
3891 x = fast_function(func, pp_stack, n, na, nk);
3892 else
3893 x = do_call(func, pp_stack, na, nk);
3894 READ_TIMESTAMP(*pintr1);
3895 Py_DECREF(func);
3896 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003897
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003898 /* Clear the stack of the function object. Also removes
3899 the arguments in case they weren't consumed already
3900 (fast_function() and err_args() leave them on the stack).
3901 */
3902 while ((*pp_stack) > pfunc) {
3903 w = EXT_POP(*pp_stack);
3904 Py_DECREF(w);
3905 PCALL(PCALL_POP);
3906 }
3907 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003908}
3909
Jeremy Hylton192690e2002-08-16 18:36:11 +00003910/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003911 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003912 For the simplest case -- a function that takes only positional
3913 arguments and is called with only positional arguments -- it
3914 inlines the most primitive frame setup code from
3915 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3916 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003917*/
3918
3919static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003920fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003921{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003922 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
3923 PyObject *globals = PyFunction_GET_GLOBALS(func);
3924 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3925 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
3926 PyObject **d = NULL;
3927 int nd = 0;
Jeremy Hylton52820442001-01-03 23:52:36 +00003928
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003929 PCALL(PCALL_FUNCTION);
3930 PCALL(PCALL_FAST_FUNCTION);
3931 if (argdefs == NULL && co->co_argcount == n &&
3932 co->co_kwonlyargcount == 0 && nk==0 &&
3933 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3934 PyFrameObject *f;
3935 PyObject *retval = NULL;
3936 PyThreadState *tstate = PyThreadState_GET();
3937 PyObject **fastlocals, **stack;
3938 int i;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003939
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003940 PCALL(PCALL_FASTER_FUNCTION);
3941 assert(globals != NULL);
3942 /* XXX Perhaps we should create a specialized
3943 PyFrame_New() that doesn't take locals, but does
3944 take builtins without sanity checking them.
3945 */
3946 assert(tstate != NULL);
3947 f = PyFrame_New(tstate, co, globals, NULL);
3948 if (f == NULL)
3949 return NULL;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003950
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003951 fastlocals = f->f_localsplus;
3952 stack = (*pp_stack) - n;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003953
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003954 for (i = 0; i < n; i++) {
3955 Py_INCREF(*stack);
3956 fastlocals[i] = *stack++;
3957 }
3958 retval = PyEval_EvalFrameEx(f,0);
3959 ++tstate->recursion_depth;
3960 Py_DECREF(f);
3961 --tstate->recursion_depth;
3962 return retval;
3963 }
3964 if (argdefs != NULL) {
3965 d = &PyTuple_GET_ITEM(argdefs, 0);
3966 nd = Py_SIZE(argdefs);
3967 }
3968 return PyEval_EvalCodeEx(co, globals,
3969 (PyObject *)NULL, (*pp_stack)-n, na,
3970 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
3971 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003972}
3973
3974static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003975update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3976 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003977{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003978 PyObject *kwdict = NULL;
3979 if (orig_kwdict == NULL)
3980 kwdict = PyDict_New();
3981 else {
3982 kwdict = PyDict_Copy(orig_kwdict);
3983 Py_DECREF(orig_kwdict);
3984 }
3985 if (kwdict == NULL)
3986 return NULL;
3987 while (--nk >= 0) {
3988 int err;
3989 PyObject *value = EXT_POP(*pp_stack);
3990 PyObject *key = EXT_POP(*pp_stack);
3991 if (PyDict_GetItem(kwdict, key) != NULL) {
3992 PyErr_Format(PyExc_TypeError,
3993 "%.200s%s got multiple values "
3994 "for keyword argument '%U'",
3995 PyEval_GetFuncName(func),
3996 PyEval_GetFuncDesc(func),
3997 key);
3998 Py_DECREF(key);
3999 Py_DECREF(value);
4000 Py_DECREF(kwdict);
4001 return NULL;
4002 }
4003 err = PyDict_SetItem(kwdict, key, value);
4004 Py_DECREF(key);
4005 Py_DECREF(value);
4006 if (err) {
4007 Py_DECREF(kwdict);
4008 return NULL;
4009 }
4010 }
4011 return kwdict;
Jeremy Hylton52820442001-01-03 23:52:36 +00004012}
4013
4014static PyObject *
4015update_star_args(int nstack, int nstar, PyObject *stararg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004016 PyObject ***pp_stack)
Jeremy Hylton52820442001-01-03 23:52:36 +00004017{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004018 PyObject *callargs, *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004019
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004020 callargs = PyTuple_New(nstack + nstar);
4021 if (callargs == NULL) {
4022 return NULL;
4023 }
4024 if (nstar) {
4025 int i;
4026 for (i = 0; i < nstar; i++) {
4027 PyObject *a = PyTuple_GET_ITEM(stararg, i);
4028 Py_INCREF(a);
4029 PyTuple_SET_ITEM(callargs, nstack + i, a);
4030 }
4031 }
4032 while (--nstack >= 0) {
4033 w = EXT_POP(*pp_stack);
4034 PyTuple_SET_ITEM(callargs, nstack, w);
4035 }
4036 return callargs;
Jeremy Hylton52820442001-01-03 23:52:36 +00004037}
4038
4039static PyObject *
4040load_args(PyObject ***pp_stack, int na)
4041{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004042 PyObject *args = PyTuple_New(na);
4043 PyObject *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004045 if (args == NULL)
4046 return NULL;
4047 while (--na >= 0) {
4048 w = EXT_POP(*pp_stack);
4049 PyTuple_SET_ITEM(args, na, w);
4050 }
4051 return args;
Jeremy Hylton52820442001-01-03 23:52:36 +00004052}
4053
4054static PyObject *
4055do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4056{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004057 PyObject *callargs = NULL;
4058 PyObject *kwdict = NULL;
4059 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004060
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004061 if (nk > 0) {
4062 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
4063 if (kwdict == NULL)
4064 goto call_fail;
4065 }
4066 callargs = load_args(pp_stack, na);
4067 if (callargs == NULL)
4068 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004069#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004070 /* At this point, we have to look at the type of func to
4071 update the call stats properly. Do it here so as to avoid
4072 exposing the call stats machinery outside ceval.c
4073 */
4074 if (PyFunction_Check(func))
4075 PCALL(PCALL_FUNCTION);
4076 else if (PyMethod_Check(func))
4077 PCALL(PCALL_METHOD);
4078 else if (PyType_Check(func))
4079 PCALL(PCALL_TYPE);
4080 else if (PyCFunction_Check(func))
4081 PCALL(PCALL_CFUNCTION);
4082 else
4083 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004084#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004085 if (PyCFunction_Check(func)) {
4086 PyThreadState *tstate = PyThreadState_GET();
4087 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4088 }
4089 else
4090 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00004091call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004092 Py_XDECREF(callargs);
4093 Py_XDECREF(kwdict);
4094 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004095}
4096
4097static PyObject *
4098ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4099{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004100 int nstar = 0;
4101 PyObject *callargs = NULL;
4102 PyObject *stararg = NULL;
4103 PyObject *kwdict = NULL;
4104 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004105
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004106 if (flags & CALL_FLAG_KW) {
4107 kwdict = EXT_POP(*pp_stack);
4108 if (!PyDict_Check(kwdict)) {
4109 PyObject *d;
4110 d = PyDict_New();
4111 if (d == NULL)
4112 goto ext_call_fail;
4113 if (PyDict_Update(d, kwdict) != 0) {
4114 Py_DECREF(d);
4115 /* PyDict_Update raises attribute
4116 * error (percolated from an attempt
4117 * to get 'keys' attribute) instead of
4118 * a type error if its second argument
4119 * is not a mapping.
4120 */
4121 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4122 PyErr_Format(PyExc_TypeError,
4123 "%.200s%.200s argument after ** "
4124 "must be a mapping, not %.200s",
4125 PyEval_GetFuncName(func),
4126 PyEval_GetFuncDesc(func),
4127 kwdict->ob_type->tp_name);
4128 }
4129 goto ext_call_fail;
4130 }
4131 Py_DECREF(kwdict);
4132 kwdict = d;
4133 }
4134 }
4135 if (flags & CALL_FLAG_VAR) {
4136 stararg = EXT_POP(*pp_stack);
4137 if (!PyTuple_Check(stararg)) {
4138 PyObject *t = NULL;
4139 t = PySequence_Tuple(stararg);
4140 if (t == NULL) {
4141 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4142 PyErr_Format(PyExc_TypeError,
4143 "%.200s%.200s argument after * "
4144 "must be a sequence, not %200s",
4145 PyEval_GetFuncName(func),
4146 PyEval_GetFuncDesc(func),
4147 stararg->ob_type->tp_name);
4148 }
4149 goto ext_call_fail;
4150 }
4151 Py_DECREF(stararg);
4152 stararg = t;
4153 }
4154 nstar = PyTuple_GET_SIZE(stararg);
4155 }
4156 if (nk > 0) {
4157 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
4158 if (kwdict == NULL)
4159 goto ext_call_fail;
4160 }
4161 callargs = update_star_args(na, nstar, stararg, pp_stack);
4162 if (callargs == NULL)
4163 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004164#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004165 /* At this point, we have to look at the type of func to
4166 update the call stats properly. Do it here so as to avoid
4167 exposing the call stats machinery outside ceval.c
4168 */
4169 if (PyFunction_Check(func))
4170 PCALL(PCALL_FUNCTION);
4171 else if (PyMethod_Check(func))
4172 PCALL(PCALL_METHOD);
4173 else if (PyType_Check(func))
4174 PCALL(PCALL_TYPE);
4175 else if (PyCFunction_Check(func))
4176 PCALL(PCALL_CFUNCTION);
4177 else
4178 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004179#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004180 if (PyCFunction_Check(func)) {
4181 PyThreadState *tstate = PyThreadState_GET();
4182 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4183 }
4184 else
4185 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00004186ext_call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004187 Py_XDECREF(callargs);
4188 Py_XDECREF(kwdict);
4189 Py_XDECREF(stararg);
4190 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004191}
4192
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004193/* Extract a slice index from a PyInt or PyLong or an object with the
4194 nb_index slot defined, and store in *pi.
4195 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4196 and silently boost values less than -PY_SSIZE_T_MAX-1 to -PY_SSIZE_T_MAX-1.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004197 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004198*/
Tim Petersb5196382001-12-16 19:44:20 +00004199/* Note: If v is NULL, return success without storing into *pi. This
4200 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4201 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004202*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004203int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004204_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004205{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004206 if (v != NULL) {
4207 Py_ssize_t x;
4208 if (PyIndex_Check(v)) {
4209 x = PyNumber_AsSsize_t(v, NULL);
4210 if (x == -1 && PyErr_Occurred())
4211 return 0;
4212 }
4213 else {
4214 PyErr_SetString(PyExc_TypeError,
4215 "slice indices must be integers or "
4216 "None or have an __index__ method");
4217 return 0;
4218 }
4219 *pi = x;
4220 }
4221 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004222}
4223
Guido van Rossum486364b2007-06-30 05:01:58 +00004224#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004225 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004226
Guido van Rossumb209a111997-04-29 18:18:01 +00004227static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004228cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004229{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004230 int res = 0;
4231 switch (op) {
4232 case PyCmp_IS:
4233 res = (v == w);
4234 break;
4235 case PyCmp_IS_NOT:
4236 res = (v != w);
4237 break;
4238 case PyCmp_IN:
4239 res = PySequence_Contains(w, v);
4240 if (res < 0)
4241 return NULL;
4242 break;
4243 case PyCmp_NOT_IN:
4244 res = PySequence_Contains(w, v);
4245 if (res < 0)
4246 return NULL;
4247 res = !res;
4248 break;
4249 case PyCmp_EXC_MATCH:
4250 if (PyTuple_Check(w)) {
4251 Py_ssize_t i, length;
4252 length = PyTuple_Size(w);
4253 for (i = 0; i < length; i += 1) {
4254 PyObject *exc = PyTuple_GET_ITEM(w, i);
4255 if (!PyExceptionClass_Check(exc)) {
4256 PyErr_SetString(PyExc_TypeError,
4257 CANNOT_CATCH_MSG);
4258 return NULL;
4259 }
4260 }
4261 }
4262 else {
4263 if (!PyExceptionClass_Check(w)) {
4264 PyErr_SetString(PyExc_TypeError,
4265 CANNOT_CATCH_MSG);
4266 return NULL;
4267 }
4268 }
4269 res = PyErr_GivenExceptionMatches(v, w);
4270 break;
4271 default:
4272 return PyObject_RichCompare(v, w, op);
4273 }
4274 v = res ? Py_True : Py_False;
4275 Py_INCREF(v);
4276 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004277}
4278
Thomas Wouters52152252000-08-17 22:55:00 +00004279static PyObject *
4280import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004281{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004282 PyObject *x;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004284 x = PyObject_GetAttr(v, name);
4285 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
4286 PyErr_Format(PyExc_ImportError, "cannot import name %S", name);
4287 }
4288 return x;
Thomas Wouters52152252000-08-17 22:55:00 +00004289}
Guido van Rossumac7be682001-01-17 15:42:30 +00004290
Thomas Wouters52152252000-08-17 22:55:00 +00004291static int
4292import_all_from(PyObject *locals, PyObject *v)
4293{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004294 PyObject *all = PyObject_GetAttrString(v, "__all__");
4295 PyObject *dict, *name, *value;
4296 int skip_leading_underscores = 0;
4297 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004299 if (all == NULL) {
4300 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4301 return -1; /* Unexpected error */
4302 PyErr_Clear();
4303 dict = PyObject_GetAttrString(v, "__dict__");
4304 if (dict == NULL) {
4305 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4306 return -1;
4307 PyErr_SetString(PyExc_ImportError,
4308 "from-import-* object has no __dict__ and no __all__");
4309 return -1;
4310 }
4311 all = PyMapping_Keys(dict);
4312 Py_DECREF(dict);
4313 if (all == NULL)
4314 return -1;
4315 skip_leading_underscores = 1;
4316 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004318 for (pos = 0, err = 0; ; pos++) {
4319 name = PySequence_GetItem(all, pos);
4320 if (name == NULL) {
4321 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4322 err = -1;
4323 else
4324 PyErr_Clear();
4325 break;
4326 }
4327 if (skip_leading_underscores &&
4328 PyUnicode_Check(name) &&
4329 PyUnicode_AS_UNICODE(name)[0] == '_')
4330 {
4331 Py_DECREF(name);
4332 continue;
4333 }
4334 value = PyObject_GetAttr(v, name);
4335 if (value == NULL)
4336 err = -1;
4337 else if (PyDict_CheckExact(locals))
4338 err = PyDict_SetItem(locals, name, value);
4339 else
4340 err = PyObject_SetItem(locals, name, value);
4341 Py_DECREF(name);
4342 Py_XDECREF(value);
4343 if (err != 0)
4344 break;
4345 }
4346 Py_DECREF(all);
4347 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004348}
4349
Guido van Rossumac7be682001-01-17 15:42:30 +00004350static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004351format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004352{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004353 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004354
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004355 if (!obj)
4356 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004358 obj_str = _PyUnicode_AsString(obj);
4359 if (!obj_str)
4360 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004361
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004362 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00004363}
Guido van Rossum950361c1997-01-24 13:49:28 +00004364
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004365static PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +00004366unicode_concatenate(PyObject *v, PyObject *w,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004367 PyFrameObject *f, unsigned char *next_instr)
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004368{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004369 /* This function implements 'variable += expr' when both arguments
4370 are (Unicode) strings. */
4371 Py_ssize_t v_len = PyUnicode_GET_SIZE(v);
4372 Py_ssize_t w_len = PyUnicode_GET_SIZE(w);
4373 Py_ssize_t new_len = v_len + w_len;
4374 if (new_len < 0) {
4375 PyErr_SetString(PyExc_OverflowError,
4376 "strings are too large to concat");
4377 return NULL;
4378 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00004379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004380 if (v->ob_refcnt == 2) {
4381 /* In the common case, there are 2 references to the value
4382 * stored in 'variable' when the += is performed: one on the
4383 * value stack (in 'v') and one still stored in the
4384 * 'variable'. We try to delete the variable now to reduce
4385 * the refcnt to 1.
4386 */
4387 switch (*next_instr) {
4388 case STORE_FAST:
4389 {
4390 int oparg = PEEKARG();
4391 PyObject **fastlocals = f->f_localsplus;
4392 if (GETLOCAL(oparg) == v)
4393 SETLOCAL(oparg, NULL);
4394 break;
4395 }
4396 case STORE_DEREF:
4397 {
4398 PyObject **freevars = (f->f_localsplus +
4399 f->f_code->co_nlocals);
4400 PyObject *c = freevars[PEEKARG()];
4401 if (PyCell_GET(c) == v)
4402 PyCell_Set(c, NULL);
4403 break;
4404 }
4405 case STORE_NAME:
4406 {
4407 PyObject *names = f->f_code->co_names;
4408 PyObject *name = GETITEM(names, PEEKARG());
4409 PyObject *locals = f->f_locals;
4410 if (PyDict_CheckExact(locals) &&
4411 PyDict_GetItem(locals, name) == v) {
4412 if (PyDict_DelItem(locals, name) != 0) {
4413 PyErr_Clear();
4414 }
4415 }
4416 break;
4417 }
4418 }
4419 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004421 if (v->ob_refcnt == 1 && !PyUnicode_CHECK_INTERNED(v)) {
4422 /* Now we own the last reference to 'v', so we can resize it
4423 * in-place.
4424 */
4425 if (PyUnicode_Resize(&v, new_len) != 0) {
4426 /* XXX if PyUnicode_Resize() fails, 'v' has been
4427 * deallocated so it cannot be put back into
4428 * 'variable'. The MemoryError is raised when there
4429 * is no value in 'variable', which might (very
4430 * remotely) be a cause of incompatibilities.
4431 */
4432 return NULL;
4433 }
4434 /* copy 'w' into the newly allocated area of 'v' */
4435 memcpy(PyUnicode_AS_UNICODE(v) + v_len,
4436 PyUnicode_AS_UNICODE(w), w_len*sizeof(Py_UNICODE));
4437 return v;
4438 }
4439 else {
4440 /* When in-place resizing is not an option. */
4441 w = PyUnicode_Concat(v, w);
4442 Py_DECREF(v);
4443 return w;
4444 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004445}
4446
Guido van Rossum950361c1997-01-24 13:49:28 +00004447#ifdef DYNAMIC_EXECUTION_PROFILE
4448
Skip Montanarof118cb12001-10-15 20:51:38 +00004449static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004450getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004451{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004452 int i;
4453 PyObject *l = PyList_New(256);
4454 if (l == NULL) return NULL;
4455 for (i = 0; i < 256; i++) {
4456 PyObject *x = PyLong_FromLong(a[i]);
4457 if (x == NULL) {
4458 Py_DECREF(l);
4459 return NULL;
4460 }
4461 PyList_SetItem(l, i, x);
4462 }
4463 for (i = 0; i < 256; i++)
4464 a[i] = 0;
4465 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004466}
4467
4468PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004469_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004470{
4471#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004472 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00004473#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004474 int i;
4475 PyObject *l = PyList_New(257);
4476 if (l == NULL) return NULL;
4477 for (i = 0; i < 257; i++) {
4478 PyObject *x = getarray(dxpairs[i]);
4479 if (x == NULL) {
4480 Py_DECREF(l);
4481 return NULL;
4482 }
4483 PyList_SetItem(l, i, x);
4484 }
4485 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004486#endif
4487}
4488
4489#endif