blob: 4d583a57deec71a6ce4d98e88c7b184512bb6bf0 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003
Guido van Rossum681d79a1995-07-18 14:51:37 +00004/* XXX TO DO:
Guido van Rossum681d79a1995-07-18 14:51:37 +00005 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +00006 XXX document it!
7 */
8
Thomas Wouters477c8d52006-05-27 19:21:47 +00009/* enable more aggressive intra-module optimizations, where available */
10#define PY_LOCAL_AGGRESSIVE
11
Guido van Rossumb209a111997-04-29 18:18:01 +000012#include "Python.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000013
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000014#include "code.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000015#include "frameobject.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000016#include "eval.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000017#include "opcode.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000018#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000019
Guido van Rossumc6004111993-11-05 10:22:19 +000020#include <ctype.h>
21
Thomas Wouters477c8d52006-05-27 19:21:47 +000022#ifndef WITH_TSC
Michael W. Hudson75eabd22005-01-18 15:56:11 +000023
24#define READ_TIMESTAMP(var)
25
26#else
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000027
28typedef unsigned long long uint64;
29
Michael W. Hudson800ba232004-08-12 18:19:17 +000030#if defined(__ppc__) /* <- Don't know if this is the correct symbol; this
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000031 section should work for GCC on any PowerPC
32 platform, irrespective of OS.
33 POWER? Who knows :-) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000034
Michael W. Hudson75eabd22005-01-18 15:56:11 +000035#define READ_TIMESTAMP(var) ppc_getcounter(&var)
Michael W. Hudson800ba232004-08-12 18:19:17 +000036
37static void
38ppc_getcounter(uint64 *v)
39{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000040 register unsigned long tbu, tb, tbu2;
Michael W. Hudson800ba232004-08-12 18:19:17 +000041
42 loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000043 asm volatile ("mftbu %0" : "=r" (tbu) );
44 asm volatile ("mftb %0" : "=r" (tb) );
45 asm volatile ("mftbu %0" : "=r" (tbu2));
46 if (__builtin_expect(tbu != tbu2, 0)) goto loop;
Michael W. Hudson800ba232004-08-12 18:19:17 +000047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048 /* The slightly peculiar way of writing the next lines is
49 compiled better by GCC than any other way I tried. */
50 ((long*)(v))[0] = tbu;
51 ((long*)(v))[1] = tb;
Michael W. Hudson800ba232004-08-12 18:19:17 +000052}
53
Mark Dickinsona25b1312009-10-31 10:18:44 +000054#elif defined(__i386__)
55
56/* this is for linux/x86 (and probably any other GCC/x86 combo) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000057
Michael W. Hudson75eabd22005-01-18 15:56:11 +000058#define READ_TIMESTAMP(val) \
59 __asm__ __volatile__("rdtsc" : "=A" (val))
Michael W. Hudson800ba232004-08-12 18:19:17 +000060
Mark Dickinsona25b1312009-10-31 10:18:44 +000061#elif defined(__x86_64__)
62
63/* for gcc/x86_64, the "A" constraint in DI mode means *either* rax *or* rdx;
64 not edx:eax as it does for i386. Since rdtsc puts its result in edx:eax
65 even in 64-bit mode, we need to use "a" and "d" for the lower and upper
66 32-bit pieces of the result. */
67
68#define READ_TIMESTAMP(val) \
69 __asm__ __volatile__("rdtsc" : \
70 "=a" (((int*)&(val))[0]), "=d" (((int*)&(val))[1]));
71
72
73#else
74
75#error "Don't know how to implement timestamp counter for this architecture"
76
Michael W. Hudson800ba232004-08-12 18:19:17 +000077#endif
78
Thomas Wouters477c8d52006-05-27 19:21:47 +000079void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000080 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000081{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 uint64 intr, inst, loop;
83 PyThreadState *tstate = PyThreadState_Get();
84 if (!tstate->interp->tscdump)
85 return;
86 intr = intr1 - intr0;
87 inst = inst1 - inst0 - intr;
88 loop = loop1 - loop0 - intr;
89 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
Stefan Krahb7e10102010-06-23 18:42:39 +000090 opcode, ticked, inst, loop);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000091}
Michael W. Hudson800ba232004-08-12 18:19:17 +000092
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000093#endif
94
Guido van Rossum04691fc1992-08-12 15:35:34 +000095/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000096/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000097
Guido van Rossum408027e1996-12-30 16:17:54 +000098#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000099/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100#define LLTRACE 1 /* Low-level trace feature */
101#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000102#endif
103
Jeremy Hylton52820442001-01-03 23:52:36 +0000104typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +0000105
Guido van Rossum374a9221991-04-04 10:40:29 +0000106/* Forward declarations */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000107#ifdef WITH_TSC
Thomas Wouters477c8d52006-05-27 19:21:47 +0000108static PyObject * call_function(PyObject ***, int, uint64*, uint64*);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000109#else
Thomas Wouters477c8d52006-05-27 19:21:47 +0000110static PyObject * call_function(PyObject ***, int);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000111#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000112static PyObject * fast_function(PyObject *, PyObject ***, int, int, int);
113static PyObject * do_call(PyObject *, PyObject ***, int, int);
114static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int);
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000115static PyObject * update_keyword_args(PyObject *, int, PyObject ***,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000117static PyObject * update_star_args(int, int, PyObject *, PyObject ***);
118static PyObject * load_args(PyObject ***, int);
Jeremy Hylton52820442001-01-03 23:52:36 +0000119#define CALL_FLAG_VAR 1
120#define CALL_FLAG_KW 2
121
Guido van Rossum0a066c01992-03-27 17:29:15 +0000122#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +0000123static int lltrace;
Tim Petersdbd9ba62000-07-09 03:09:57 +0000124static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +0000125#endif
Fred Drake5755ce62001-06-27 19:19:46 +0000126static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +0000128static int call_trace_protected(Py_tracefunc, PyObject *,
Stefan Krahb7e10102010-06-23 18:42:39 +0000129 PyFrameObject *, int, PyObject *);
Fred Drake5755ce62001-06-27 19:19:46 +0000130static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +0000131static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Stefan Krahb7e10102010-06-23 18:42:39 +0000132 PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000133
Thomas Wouters477c8d52006-05-27 19:21:47 +0000134static PyObject * cmp_outcome(int, PyObject *, PyObject *);
135static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +0000136static int import_all_from(PyObject *, PyObject *);
Neal Norwitzda059e32007-08-26 05:33:45 +0000137static void format_exc_check_arg(PyObject *, const char *, PyObject *);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000138static PyObject * unicode_concatenate(PyObject *, PyObject *,
139 PyFrameObject *, unsigned char *);
Benjamin Peterson876b2f22009-06-28 03:18:59 +0000140static PyObject * special_lookup(PyObject *, char *, PyObject **);
Guido van Rossum374a9221991-04-04 10:40:29 +0000141
Paul Prescode68140d2000-08-30 20:25:01 +0000142#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000143 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000144#define GLOBAL_NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000146#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000148#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 "free variable '%.200s' referenced before assignment" \
150 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000151
Guido van Rossum950361c1997-01-24 13:49:28 +0000152/* Dynamic execution profile */
153#ifdef DYNAMIC_EXECUTION_PROFILE
154#ifdef DXPAIRS
155static long dxpairs[257][256];
156#define dxp dxpairs[256]
157#else
158static long dxp[256];
159#endif
160#endif
161
Jeremy Hylton985eba52003-02-05 23:13:00 +0000162/* Function call profile */
163#ifdef CALL_PROFILE
164#define PCALL_NUM 11
165static int pcall[PCALL_NUM];
166
167#define PCALL_ALL 0
168#define PCALL_FUNCTION 1
169#define PCALL_FAST_FUNCTION 2
170#define PCALL_FASTER_FUNCTION 3
171#define PCALL_METHOD 4
172#define PCALL_BOUND_METHOD 5
173#define PCALL_CFUNCTION 6
174#define PCALL_TYPE 7
175#define PCALL_GENERATOR 8
176#define PCALL_OTHER 9
177#define PCALL_POP 10
178
179/* Notes about the statistics
180
181 PCALL_FAST stats
182
183 FAST_FUNCTION means no argument tuple needs to be created.
184 FASTER_FUNCTION means that the fast-path frame setup code is used.
185
186 If there is a method call where the call can be optimized by changing
187 the argument tuple and calling the function directly, it gets recorded
188 twice.
189
190 As a result, the relationship among the statistics appears to be
191 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
192 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
193 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
194 PCALL_METHOD > PCALL_BOUND_METHOD
195*/
196
197#define PCALL(POS) pcall[POS]++
198
199PyObject *
200PyEval_GetCallStats(PyObject *self)
201{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 return Py_BuildValue("iiiiiiiiiii",
203 pcall[0], pcall[1], pcall[2], pcall[3],
204 pcall[4], pcall[5], pcall[6], pcall[7],
205 pcall[8], pcall[9], pcall[10]);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000206}
207#else
208#define PCALL(O)
209
210PyObject *
211PyEval_GetCallStats(PyObject *self)
212{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 Py_INCREF(Py_None);
214 return Py_None;
Jeremy Hylton985eba52003-02-05 23:13:00 +0000215}
216#endif
217
Tim Peters5ca576e2001-06-18 22:08:13 +0000218
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000219/* This can set eval_breaker to 0 even though gil_drop_request became
220 1. We believe this is all right because the eval loop will release
221 the GIL eventually anyway. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000222#define COMPUTE_EVAL_BREAKER() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 _Py_atomic_store_relaxed( \
224 &eval_breaker, \
225 _Py_atomic_load_relaxed(&gil_drop_request) | \
226 _Py_atomic_load_relaxed(&pendingcalls_to_do) | \
227 pending_async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000228
229#define SET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000230 do { \
231 _Py_atomic_store_relaxed(&gil_drop_request, 1); \
232 _Py_atomic_store_relaxed(&eval_breaker, 1); \
233 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000234
235#define RESET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000236 do { \
237 _Py_atomic_store_relaxed(&gil_drop_request, 0); \
238 COMPUTE_EVAL_BREAKER(); \
239 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000240
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000241/* Pending calls are only modified under pending_lock */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000242#define SIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000243 do { \
244 _Py_atomic_store_relaxed(&pendingcalls_to_do, 1); \
245 _Py_atomic_store_relaxed(&eval_breaker, 1); \
246 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000247
248#define UNSIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000249 do { \
250 _Py_atomic_store_relaxed(&pendingcalls_to_do, 0); \
251 COMPUTE_EVAL_BREAKER(); \
252 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000253
254#define SIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000255 do { \
256 pending_async_exc = 1; \
257 _Py_atomic_store_relaxed(&eval_breaker, 1); \
258 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000259
260#define UNSIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 do { pending_async_exc = 0; COMPUTE_EVAL_BREAKER(); } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000262
263
Guido van Rossume59214e1994-08-30 08:01:59 +0000264#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000265
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000266#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000267#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000268#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000269#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000270
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000271static PyThread_type_lock pending_lock = 0; /* for pending calls */
Guido van Rossuma9672091994-09-14 13:31:22 +0000272static long main_thread = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000273/* This single variable consolidates all requests to break out of the fast path
274 in the eval loop. */
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000275static _Py_atomic_int eval_breaker = {0};
276/* Request for dropping the GIL */
277static _Py_atomic_int gil_drop_request = {0};
278/* Request for running pending calls. */
279static _Py_atomic_int pendingcalls_to_do = {0};
280/* Request for looking at the `async_exc` field of the current thread state.
281 Guarded by the GIL. */
282static int pending_async_exc = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000283
284#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000285
Tim Peters7f468f22004-10-11 02:40:51 +0000286int
287PyEval_ThreadsInitialized(void)
288{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000289 return gil_created();
Tim Peters7f468f22004-10-11 02:40:51 +0000290}
291
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000292void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000293PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000294{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 if (gil_created())
296 return;
297 create_gil();
298 take_gil(PyThreadState_GET());
299 main_thread = PyThread_get_thread_ident();
300 if (!pending_lock)
301 pending_lock = PyThread_allocate_lock();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000302}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000303
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000304void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000305PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000306{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 PyThreadState *tstate = PyThreadState_GET();
308 if (tstate == NULL)
309 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
310 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000311}
312
313void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000314PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000315{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000316 /* This function must succeed when the current thread state is NULL.
317 We therefore avoid PyThreadState_GET() which dumps a fatal error
318 in debug mode.
319 */
320 drop_gil((PyThreadState*)_Py_atomic_load_relaxed(
321 &_PyThreadState_Current));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000322}
323
324void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000325PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000326{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 if (tstate == NULL)
328 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
329 /* Check someone has called PyEval_InitThreads() to create the lock */
330 assert(gil_created());
331 take_gil(tstate);
332 if (PyThreadState_Swap(tstate) != NULL)
333 Py_FatalError(
334 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000335}
336
337void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000338PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000339{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 if (tstate == NULL)
341 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
342 if (PyThreadState_Swap(NULL) != tstate)
343 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
344 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000345}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000346
347/* This function is called from PyOS_AfterFork to ensure that newly
348 created child processes don't hold locks referring to threads which
349 are not running in the child process. (This could also be done using
350 pthread_atfork mechanism, at least for the pthreads implementation.) */
351
352void
353PyEval_ReInitThreads(void)
354{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000355 PyObject *threading, *result;
356 PyThreadState *tstate = PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358 if (!gil_created())
359 return;
360 /*XXX Can't use PyThread_free_lock here because it does too
361 much error-checking. Doing this cleanly would require
362 adding a new function to each thread_*.h. Instead, just
363 create a new lock and waste a little bit of memory */
364 recreate_gil();
365 pending_lock = PyThread_allocate_lock();
366 take_gil(tstate);
367 main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 /* Update the threading module with the new state.
370 */
371 tstate = PyThreadState_GET();
372 threading = PyMapping_GetItemString(tstate->interp->modules,
373 "threading");
374 if (threading == NULL) {
375 /* threading not imported */
376 PyErr_Clear();
377 return;
378 }
379 result = PyObject_CallMethod(threading, "_after_fork", NULL);
380 if (result == NULL)
381 PyErr_WriteUnraisable(threading);
382 else
383 Py_DECREF(result);
384 Py_DECREF(threading);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000385}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000386
387#else
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000388static _Py_atomic_int eval_breaker = {0};
389static _Py_atomic_int gil_drop_request = {0};
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000390static int pending_async_exc = 0;
391#endif /* WITH_THREAD */
392
393/* This function is used to signal that async exceptions are waiting to be
394 raised, therefore it is also useful in non-threaded builds. */
395
396void
397_PyEval_SignalAsyncExc(void)
398{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000400}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000401
Guido van Rossumff4949e1992-08-05 19:58:53 +0000402/* Functions save_thread and restore_thread are always defined so
403 dynamically loaded modules needn't be compiled separately for use
404 with and without threads: */
405
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000406PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000407PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000408{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 PyThreadState *tstate = PyThreadState_Swap(NULL);
410 if (tstate == NULL)
411 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000412#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 if (gil_created())
414 drop_gil(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000415#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000417}
418
419void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000420PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000421{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422 if (tstate == NULL)
423 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000424#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000425 if (gil_created()) {
426 int err = errno;
427 take_gil(tstate);
428 errno = err;
429 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000430#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000432}
433
434
Guido van Rossuma9672091994-09-14 13:31:22 +0000435/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
436 signal handlers or Mac I/O completion routines) can schedule calls
437 to a function to be called synchronously.
438 The synchronous function is called with one void* argument.
439 It should return 0 for success or -1 for failure -- failure should
440 be accompanied by an exception.
441
442 If registry succeeds, the registry function returns 0; if it fails
443 (e.g. due to too many pending calls) it returns -1 (without setting
444 an exception condition).
445
446 Note that because registry may occur from within signal handlers,
447 or other asynchronous events, calling malloc() is unsafe!
448
449#ifdef WITH_THREAD
450 Any thread can schedule pending calls, but only the main thread
451 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000452 There is no facility to schedule calls to a particular thread, but
453 that should be easy to change, should that ever be required. In
454 that case, the static variables here should go into the python
455 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000456#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000457*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000458
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000459#ifdef WITH_THREAD
460
461/* The WITH_THREAD implementation is thread-safe. It allows
462 scheduling to be made from any thread, and even from an executing
463 callback.
464 */
465
466#define NPENDINGCALLS 32
467static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468 int (*func)(void *);
469 void *arg;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000470} pendingcalls[NPENDINGCALLS];
471static int pendingfirst = 0;
472static int pendinglast = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000473static char pendingbusy = 0;
474
475int
476Py_AddPendingCall(int (*func)(void *), void *arg)
477{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 int i, j, result=0;
479 PyThread_type_lock lock = pending_lock;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481 /* try a few times for the lock. Since this mechanism is used
482 * for signal handling (on the main thread), there is a (slim)
483 * chance that a signal is delivered on the same thread while we
484 * hold the lock during the Py_MakePendingCalls() function.
485 * This avoids a deadlock in that case.
486 * Note that signals can be delivered on any thread. In particular,
487 * on Windows, a SIGINT is delivered on a system-created worker
488 * thread.
489 * We also check for lock being NULL, in the unlikely case that
490 * this function is called before any bytecode evaluation takes place.
491 */
492 if (lock != NULL) {
493 for (i = 0; i<100; i++) {
494 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
495 break;
496 }
497 if (i == 100)
498 return -1;
499 }
500
501 i = pendinglast;
502 j = (i + 1) % NPENDINGCALLS;
503 if (j == pendingfirst) {
504 result = -1; /* Queue full */
505 } else {
506 pendingcalls[i].func = func;
507 pendingcalls[i].arg = arg;
508 pendinglast = j;
509 }
510 /* signal main loop */
511 SIGNAL_PENDING_CALLS();
512 if (lock != NULL)
513 PyThread_release_lock(lock);
514 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000515}
516
517int
518Py_MakePendingCalls(void)
519{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 int i;
521 int r = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000522
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 if (!pending_lock) {
524 /* initial allocation of the lock */
525 pending_lock = PyThread_allocate_lock();
526 if (pending_lock == NULL)
527 return -1;
528 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000529
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 /* only service pending calls on main thread */
531 if (main_thread && PyThread_get_thread_ident() != main_thread)
532 return 0;
533 /* don't perform recursive pending calls */
534 if (pendingbusy)
535 return 0;
536 pendingbusy = 1;
537 /* perform a bounded number of calls, in case of recursion */
538 for (i=0; i<NPENDINGCALLS; i++) {
539 int j;
540 int (*func)(void *);
541 void *arg = NULL;
542
543 /* pop one item off the queue while holding the lock */
544 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
545 j = pendingfirst;
546 if (j == pendinglast) {
547 func = NULL; /* Queue empty */
548 } else {
549 func = pendingcalls[j].func;
550 arg = pendingcalls[j].arg;
551 pendingfirst = (j + 1) % NPENDINGCALLS;
552 }
553 if (pendingfirst != pendinglast)
554 SIGNAL_PENDING_CALLS();
555 else
556 UNSIGNAL_PENDING_CALLS();
557 PyThread_release_lock(pending_lock);
558 /* having released the lock, perform the callback */
559 if (func == NULL)
560 break;
561 r = func(arg);
562 if (r)
563 break;
564 }
565 pendingbusy = 0;
566 return r;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000567}
568
569#else /* if ! defined WITH_THREAD */
570
571/*
572 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
573 This code is used for signal handling in python that isn't built
574 with WITH_THREAD.
575 Don't use this implementation when Py_AddPendingCalls() can happen
576 on a different thread!
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577
Guido van Rossuma9672091994-09-14 13:31:22 +0000578 There are two possible race conditions:
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000579 (1) nested asynchronous calls to Py_AddPendingCall()
580 (2) AddPendingCall() calls made while pending calls are being processed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000582 (1) is very unlikely because typically signal delivery
583 is blocked during signal handling. So it should be impossible.
584 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000585 The current code is safe against (2), but not against (1).
586 The safety against (2) is derived from the fact that only one
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000587 thread is present, interrupted by signals, and that the critical
588 section is protected with the "busy" variable. On Windows, which
589 delivers SIGINT on a system thread, this does not hold and therefore
590 Windows really shouldn't use this version.
591 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000592*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000593
Guido van Rossuma9672091994-09-14 13:31:22 +0000594#define NPENDINGCALLS 32
595static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596 int (*func)(void *);
597 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000598} pendingcalls[NPENDINGCALLS];
599static volatile int pendingfirst = 0;
600static volatile int pendinglast = 0;
Benjamin Peterson08ec84c2010-05-30 14:49:32 +0000601static _Py_atomic_int pendingcalls_to_do = {0};
Guido van Rossuma9672091994-09-14 13:31:22 +0000602
603int
Thomas Wouters334fb892000-07-25 12:56:38 +0000604Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000605{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 static volatile int busy = 0;
607 int i, j;
608 /* XXX Begin critical section */
609 if (busy)
610 return -1;
611 busy = 1;
612 i = pendinglast;
613 j = (i + 1) % NPENDINGCALLS;
614 if (j == pendingfirst) {
615 busy = 0;
616 return -1; /* Queue full */
617 }
618 pendingcalls[i].func = func;
619 pendingcalls[i].arg = arg;
620 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 SIGNAL_PENDING_CALLS();
623 busy = 0;
624 /* XXX End critical section */
625 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000626}
627
Guido van Rossum180d7b41994-09-29 09:45:57 +0000628int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000629Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000630{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631 static int busy = 0;
632 if (busy)
633 return 0;
634 busy = 1;
635 UNSIGNAL_PENDING_CALLS();
636 for (;;) {
637 int i;
638 int (*func)(void *);
639 void *arg;
640 i = pendingfirst;
641 if (i == pendinglast)
642 break; /* Queue empty */
643 func = pendingcalls[i].func;
644 arg = pendingcalls[i].arg;
645 pendingfirst = (i + 1) % NPENDINGCALLS;
646 if (func(arg) < 0) {
647 busy = 0;
648 SIGNAL_PENDING_CALLS(); /* We're not done yet */
649 return -1;
650 }
651 }
652 busy = 0;
653 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000654}
655
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000656#endif /* WITH_THREAD */
657
Guido van Rossuma9672091994-09-14 13:31:22 +0000658
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000659/* The interpreter's recursion limit */
660
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000661#ifndef Py_DEFAULT_RECURSION_LIMIT
662#define Py_DEFAULT_RECURSION_LIMIT 1000
663#endif
664static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
665int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000666
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000667int
668Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000669{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000670 return recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000671}
672
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000673void
674Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000675{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676 recursion_limit = new_limit;
677 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000678}
679
Armin Rigo2b3eb402003-10-28 12:05:48 +0000680/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
681 if the recursion_depth reaches _Py_CheckRecursionLimit.
682 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
683 to guarantee that _Py_CheckRecursiveCall() is regularly called.
684 Without USE_STACKCHECK, there is no need for this. */
685int
686_Py_CheckRecursiveCall(char *where)
687{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000688 PyThreadState *tstate = PyThreadState_GET();
Armin Rigo2b3eb402003-10-28 12:05:48 +0000689
690#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 if (PyOS_CheckStack()) {
692 --tstate->recursion_depth;
693 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
694 return -1;
695 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000696#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000697 _Py_CheckRecursionLimit = recursion_limit;
698 if (tstate->recursion_critical)
699 /* Somebody asked that we don't check for recursion. */
700 return 0;
701 if (tstate->overflowed) {
702 if (tstate->recursion_depth > recursion_limit + 50) {
703 /* Overflowing while handling an overflow. Give up. */
704 Py_FatalError("Cannot recover from stack overflow.");
705 }
706 return 0;
707 }
708 if (tstate->recursion_depth > recursion_limit) {
709 --tstate->recursion_depth;
710 tstate->overflowed = 1;
711 PyErr_Format(PyExc_RuntimeError,
712 "maximum recursion depth exceeded%s",
713 where);
714 return -1;
715 }
716 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000717}
718
Guido van Rossum374a9221991-04-04 10:40:29 +0000719/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000720enum why_code {
Stefan Krahb7e10102010-06-23 18:42:39 +0000721 WHY_NOT = 0x0001, /* No error */
722 WHY_EXCEPTION = 0x0002, /* Exception occurred */
723 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
724 WHY_RETURN = 0x0008, /* 'return' statement */
725 WHY_BREAK = 0x0010, /* 'break' statement */
726 WHY_CONTINUE = 0x0020, /* 'continue' statement */
727 WHY_YIELD = 0x0040, /* 'yield' operator */
728 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000729};
Guido van Rossum374a9221991-04-04 10:40:29 +0000730
Collin Winter828f04a2007-08-31 00:04:24 +0000731static enum why_code do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000732static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000733
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +0000734/* Records whether tracing is on for any thread. Counts the number of
735 threads for which tstate->c_tracefunc is non-NULL, so if the value
736 is 0, we know we don't have to check this thread's c_tracefunc.
737 This speeds up the if statement in PyEval_EvalFrameEx() after
738 fast_next_opcode*/
739static int _Py_TracingPossible = 0;
740
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000741
Guido van Rossum374a9221991-04-04 10:40:29 +0000742
Guido van Rossumb209a111997-04-29 18:18:01 +0000743PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000744PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000745{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000746 return PyEval_EvalCodeEx(co,
747 globals, locals,
748 (PyObject **)NULL, 0,
749 (PyObject **)NULL, 0,
750 (PyObject **)NULL, 0,
751 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000752}
753
754
755/* Interpreter main loop */
756
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000757PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000758PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759 /* This is for backward compatibility with extension modules that
760 used this API; core interpreter code should call
761 PyEval_EvalFrameEx() */
762 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000763}
764
765PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000766PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000767{
Guido van Rossum950361c1997-01-24 13:49:28 +0000768#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000770#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 register PyObject **stack_pointer; /* Next free slot in value stack */
772 register unsigned char *next_instr;
773 register int opcode; /* Current opcode */
774 register int oparg; /* Current opcode argument, if any */
775 register enum why_code why; /* Reason for block stack unwind */
776 register int err; /* Error status -- nonzero if error */
777 register PyObject *x; /* Result object -- NULL if error */
778 register PyObject *v; /* Temporary objects popped off stack */
779 register PyObject *w;
780 register PyObject *u;
781 register PyObject *t;
782 register PyObject **fastlocals, **freevars;
783 PyObject *retval = NULL; /* Return value */
784 PyThreadState *tstate = PyThreadState_GET();
785 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000786
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000787 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000788
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000790
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791 is true when the line being executed has changed. The
792 initial values are such as to make this false the first
793 time it is tested. */
794 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000795
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 unsigned char *first_instr;
797 PyObject *names;
798 PyObject *consts;
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000799#if defined(Py_DEBUG) || defined(LLTRACE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 /* Make it easier to find out where we are with a debugger */
801 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000802#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000803
Antoine Pitroub52ec782009-01-25 16:34:23 +0000804/* Computed GOTOs, or
805 the-optimization-commonly-but-improperly-known-as-"threaded code"
806 using gcc's labels-as-values extension
807 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
808
809 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000811 combined with a lookup table of jump addresses. However, since the
812 indirect jump instruction is shared by all opcodes, the CPU will have a
813 hard time making the right prediction for where to jump next (actually,
814 it will be always wrong except in the uncommon case of a sequence of
815 several identical opcodes).
816
817 "Threaded code" in contrast, uses an explicit jump table and an explicit
818 indirect jump instruction at the end of each opcode. Since the jump
819 instruction is at a different address for each opcode, the CPU will make a
820 separate prediction for each of these instructions, which is equivalent to
821 predicting the second opcode of each opcode pair. These predictions have
822 a much better chance to turn out valid, especially in small bytecode loops.
823
824 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000826 and potentially many more instructions (depending on the pipeline width).
827 A correctly predicted branch, however, is nearly free.
828
829 At the time of this writing, the "threaded code" version is up to 15-20%
830 faster than the normal "switch" version, depending on the compiler and the
831 CPU architecture.
832
833 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
834 because it would render the measurements invalid.
835
836
837 NOTE: care must be taken that the compiler doesn't try to "optimize" the
838 indirect jumps by sharing them between all opcodes. Such optimizations
839 can be disabled on gcc by using the -fno-gcse flag (or possibly
840 -fno-crossjumping).
841*/
842
Antoine Pitrou042b1282010-08-13 21:15:58 +0000843#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000844#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000845#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000846#endif
847
Antoine Pitrou042b1282010-08-13 21:15:58 +0000848#ifdef HAVE_COMPUTED_GOTOS
849 #ifndef USE_COMPUTED_GOTOS
850 #define USE_COMPUTED_GOTOS 1
851 #endif
852#else
853 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
854 #error "Computed gotos are not supported on this compiler."
855 #endif
856 #undef USE_COMPUTED_GOTOS
857 #define USE_COMPUTED_GOTOS 0
858#endif
859
860#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000861/* Import the static jump table */
862#include "opcode_targets.h"
863
864/* This macro is used when several opcodes defer to the same implementation
865 (e.g. SETUP_LOOP, SETUP_FINALLY) */
866#define TARGET_WITH_IMPL(op, impl) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 TARGET_##op: \
868 opcode = op; \
869 if (HAS_ARG(op)) \
870 oparg = NEXTARG(); \
871 case op: \
872 goto impl; \
Antoine Pitroub52ec782009-01-25 16:34:23 +0000873
874#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 TARGET_##op: \
876 opcode = op; \
877 if (HAS_ARG(op)) \
878 oparg = NEXTARG(); \
879 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000880
881
882#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 { \
884 if (!_Py_atomic_load_relaxed(&eval_breaker)) { \
885 FAST_DISPATCH(); \
886 } \
887 continue; \
888 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000889
890#ifdef LLTRACE
891#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 { \
893 if (!lltrace && !_Py_TracingPossible) { \
894 f->f_lasti = INSTR_OFFSET(); \
895 goto *opcode_targets[*next_instr++]; \
896 } \
897 goto fast_next_opcode; \
898 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000899#else
900#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 { \
902 if (!_Py_TracingPossible) { \
903 f->f_lasti = INSTR_OFFSET(); \
904 goto *opcode_targets[*next_instr++]; \
905 } \
906 goto fast_next_opcode; \
907 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000908#endif
909
910#else
911#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000913#define TARGET_WITH_IMPL(op, impl) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 /* silence compiler warnings about `impl` unused */ \
915 if (0) goto impl; \
916 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000917#define DISPATCH() continue
918#define FAST_DISPATCH() goto fast_next_opcode
919#endif
920
921
Neal Norwitza81d2202002-07-14 00:27:26 +0000922/* Tuple access macros */
923
924#ifndef Py_DEBUG
925#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
926#else
927#define GETITEM(v, i) PyTuple_GetItem((v), (i))
928#endif
929
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000930#ifdef WITH_TSC
931/* Use Pentium timestamp counter to mark certain events:
932 inst0 -- beginning of switch statement for opcode dispatch
933 inst1 -- end of switch statement (may be skipped)
934 loop0 -- the top of the mainloop
Thomas Wouters477c8d52006-05-27 19:21:47 +0000935 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000936 (may be skipped)
937 intr1 -- beginning of long interruption
938 intr2 -- end of long interruption
939
940 Many opcodes call out to helper C functions. In some cases, the
941 time in those functions should be counted towards the time for the
942 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
943 calls another Python function; there's no point in charge all the
944 bytecode executed by the called function to the caller.
945
946 It's hard to make a useful judgement statically. In the presence
947 of operator overloading, it's impossible to tell if a call will
948 execute new Python code or not.
949
950 It's a case-by-case judgement. I'll use intr1 for the following
951 cases:
952
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000953 IMPORT_STAR
954 IMPORT_FROM
955 CALL_FUNCTION (and friends)
956
957 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
959 int ticked = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 READ_TIMESTAMP(inst0);
962 READ_TIMESTAMP(inst1);
963 READ_TIMESTAMP(loop0);
964 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000965
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 /* shut up the compiler */
967 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000968#endif
969
Guido van Rossum374a9221991-04-04 10:40:29 +0000970/* Code access macros */
971
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972#define INSTR_OFFSET() ((int)(next_instr - first_instr))
973#define NEXTOP() (*next_instr++)
974#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
975#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
976#define JUMPTO(x) (next_instr = first_instr + (x))
977#define JUMPBY(x) (next_instr += (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000978
Raymond Hettingerf606f872003-03-16 03:11:04 +0000979/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980 Some opcodes tend to come in pairs thus making it possible to
981 predict the second code when the first is run. For example,
982 COMPARE_OP is often followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And,
983 those opcodes are often followed by a POP_TOP.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000984
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 Verifying the prediction costs a single high-speed test of a register
986 variable against a constant. If the pairing was good, then the
987 processor's own internal branch predication has a high likelihood of
988 success, resulting in a nearly zero-overhead transition to the
989 next opcode. A successful prediction saves a trip through the eval-loop
990 including its two unpredictable branches, the HAS_ARG test and the
991 switch-case. Combined with the processor's internal branch prediction,
992 a successful PREDICT has the effect of making the two opcodes run as if
993 they were a single new opcode with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000994
Georg Brandl86b2fb92008-07-16 03:43:04 +0000995 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 predictions turned-on and interpret the results as if some opcodes
997 had been combined or turn-off predictions so that the opcode frequency
998 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000999
1000 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 the CPU to record separate branch prediction information for each
1002 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001003
Raymond Hettingerf606f872003-03-16 03:11:04 +00001004*/
1005
Antoine Pitrou042b1282010-08-13 21:15:58 +00001006#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007#define PREDICT(op) if (0) goto PRED_##op
1008#define PREDICTED(op) PRED_##op:
1009#define PREDICTED_WITH_ARG(op) PRED_##op:
Raymond Hettingera7216982004-02-08 19:59:27 +00001010#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011#define PREDICT(op) if (*next_instr == op) goto PRED_##op
1012#define PREDICTED(op) PRED_##op: next_instr++
1013#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Antoine Pitroub52ec782009-01-25 16:34:23 +00001014#endif
1015
Raymond Hettingerf606f872003-03-16 03:11:04 +00001016
Guido van Rossum374a9221991-04-04 10:40:29 +00001017/* Stack manipulation macros */
1018
Martin v. Löwis18e16552006-02-15 17:27:45 +00001019/* The stack can grow at most MAXINT deep, as co_nlocals and
1020 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001021#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1022#define EMPTY() (STACK_LEVEL() == 0)
1023#define TOP() (stack_pointer[-1])
1024#define SECOND() (stack_pointer[-2])
1025#define THIRD() (stack_pointer[-3])
1026#define FOURTH() (stack_pointer[-4])
1027#define PEEK(n) (stack_pointer[-(n)])
1028#define SET_TOP(v) (stack_pointer[-1] = (v))
1029#define SET_SECOND(v) (stack_pointer[-2] = (v))
1030#define SET_THIRD(v) (stack_pointer[-3] = (v))
1031#define SET_FOURTH(v) (stack_pointer[-4] = (v))
1032#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
1033#define BASIC_STACKADJ(n) (stack_pointer += n)
1034#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1035#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001036
Guido van Rossum96a42c81992-01-12 02:29:51 +00001037#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001039 lltrace && prtrace(TOP(), "push")); \
1040 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001042 BASIC_POP())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001044 lltrace && prtrace(TOP(), "stackadj")); \
1045 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +00001046#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +00001047 prtrace((STACK_POINTER)[-1], "ext_pop")), \
1048 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001049#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001050#define PUSH(v) BASIC_PUSH(v)
1051#define POP() BASIC_POP()
1052#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001053#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001054#endif
1055
Guido van Rossum681d79a1995-07-18 14:51:37 +00001056/* Local variable macros */
1057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001059
1060/* The SETLOCAL() macro must not DECREF the local variable in-place and
1061 then store the new value; it must copy the old value to a temporary
1062 value, then store the new value, and then DECREF the temporary value.
1063 This is because it is possible that during the DECREF the frame is
1064 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1065 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001067 GETLOCAL(i) = value; \
1068 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001069
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001070
1071#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 while (STACK_LEVEL() > (b)->b_level) { \
1073 PyObject *v = POP(); \
1074 Py_XDECREF(v); \
1075 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001076
1077#define UNWIND_EXCEPT_HANDLER(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 { \
1079 PyObject *type, *value, *traceback; \
1080 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1081 while (STACK_LEVEL() > (b)->b_level + 3) { \
1082 value = POP(); \
1083 Py_XDECREF(value); \
1084 } \
1085 type = tstate->exc_type; \
1086 value = tstate->exc_value; \
1087 traceback = tstate->exc_traceback; \
1088 tstate->exc_type = POP(); \
1089 tstate->exc_value = POP(); \
1090 tstate->exc_traceback = POP(); \
1091 Py_XDECREF(type); \
1092 Py_XDECREF(value); \
1093 Py_XDECREF(traceback); \
1094 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001095
1096#define SAVE_EXC_STATE() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 { \
1098 PyObject *type, *value, *traceback; \
1099 Py_XINCREF(tstate->exc_type); \
1100 Py_XINCREF(tstate->exc_value); \
1101 Py_XINCREF(tstate->exc_traceback); \
1102 type = f->f_exc_type; \
1103 value = f->f_exc_value; \
1104 traceback = f->f_exc_traceback; \
1105 f->f_exc_type = tstate->exc_type; \
1106 f->f_exc_value = tstate->exc_value; \
1107 f->f_exc_traceback = tstate->exc_traceback; \
1108 Py_XDECREF(type); \
1109 Py_XDECREF(value); \
1110 Py_XDECREF(traceback); \
1111 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001112
1113#define SWAP_EXC_STATE() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 { \
1115 PyObject *tmp; \
1116 tmp = tstate->exc_type; \
1117 tstate->exc_type = f->f_exc_type; \
1118 f->f_exc_type = tmp; \
1119 tmp = tstate->exc_value; \
1120 tstate->exc_value = f->f_exc_value; \
1121 f->f_exc_value = tmp; \
1122 tmp = tstate->exc_traceback; \
1123 tstate->exc_traceback = f->f_exc_traceback; \
1124 f->f_exc_traceback = tmp; \
1125 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001126
Guido van Rossuma027efa1997-05-05 20:56:21 +00001127/* Start of code */
1128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 if (f == NULL)
1130 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001131
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001132 /* push frame */
1133 if (Py_EnterRecursiveCall(""))
1134 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001135
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001136 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001137
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001138 if (tstate->use_tracing) {
1139 if (tstate->c_tracefunc != NULL) {
1140 /* tstate->c_tracefunc, if defined, is a
1141 function that will be called on *every* entry
1142 to a code block. Its return value, if not
1143 None, is a function that will be called at
1144 the start of each executed line of code.
1145 (Actually, the function must return itself
1146 in order to continue tracing.) The trace
1147 functions are called with three arguments:
1148 a pointer to the current frame, a string
1149 indicating why the function is called, and
1150 an argument which depends on the situation.
1151 The global trace function is also called
1152 whenever an exception is detected. */
1153 if (call_trace_protected(tstate->c_tracefunc,
1154 tstate->c_traceobj,
1155 f, PyTrace_CALL, Py_None)) {
1156 /* Trace function raised an error */
1157 goto exit_eval_frame;
1158 }
1159 }
1160 if (tstate->c_profilefunc != NULL) {
1161 /* Similar for c_profilefunc, except it needn't
1162 return itself and isn't called for "line" events */
1163 if (call_trace_protected(tstate->c_profilefunc,
1164 tstate->c_profileobj,
1165 f, PyTrace_CALL, Py_None)) {
1166 /* Profile function raised an error */
1167 goto exit_eval_frame;
1168 }
1169 }
1170 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001171
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 co = f->f_code;
1173 names = co->co_names;
1174 consts = co->co_consts;
1175 fastlocals = f->f_localsplus;
1176 freevars = f->f_localsplus + co->co_nlocals;
1177 first_instr = (unsigned char*) PyBytes_AS_STRING(co->co_code);
1178 /* An explanation is in order for the next line.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001179
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180 f->f_lasti now refers to the index of the last instruction
1181 executed. You might think this was obvious from the name, but
1182 this wasn't always true before 2.3! PyFrame_New now sets
1183 f->f_lasti to -1 (i.e. the index *before* the first instruction)
1184 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
1185 does work. Promise.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001186
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 When the PREDICT() macros are enabled, some opcode pairs follow in
1188 direct succession without updating f->f_lasti. A successful
1189 prediction effectively links the two codes together as if they
1190 were a single new opcode; accordingly,f->f_lasti will point to
1191 the first code in the pair (for instance, GET_ITER followed by
1192 FOR_ITER is effectively a single opcode and f->f_lasti will point
1193 at to the beginning of the combined pair.)
1194 */
1195 next_instr = first_instr + f->f_lasti + 1;
1196 stack_pointer = f->f_stacktop;
1197 assert(stack_pointer != NULL);
1198 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001199
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001200 if (co->co_flags & CO_GENERATOR && !throwflag) {
1201 if (f->f_exc_type != NULL && f->f_exc_type != Py_None) {
1202 /* We were in an except handler when we left,
1203 restore the exception state which was put aside
1204 (see YIELD_VALUE). */
1205 SWAP_EXC_STATE();
1206 }
1207 else {
1208 SAVE_EXC_STATE();
1209 }
1210 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001211
Tim Peters5ca576e2001-06-18 22:08:13 +00001212#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001213 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001214#endif
Neal Norwitz5f5153e2005-10-21 04:28:38 +00001215#if defined(Py_DEBUG) || defined(LLTRACE)
Victor Stinner4a3733d2010-08-17 00:39:57 +00001216 {
1217 PyObject *error_type, *error_value, *error_traceback;
1218 PyErr_Fetch(&error_type, &error_value, &error_traceback);
1219 filename = _PyUnicode_AsString(co->co_filename);
1220 PyErr_Restore(error_type, error_value, error_traceback);
1221 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001222#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001223
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 why = WHY_NOT;
1225 err = 0;
1226 x = Py_None; /* Not a reference, just anything non-NULL */
1227 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00001228
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001229 if (throwflag) { /* support for generator.throw() */
1230 why = WHY_EXCEPTION;
1231 goto on_error;
1232 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001233
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001235#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 if (inst1 == 0) {
1237 /* Almost surely, the opcode executed a break
1238 or a continue, preventing inst1 from being set
1239 on the way out of the loop.
1240 */
1241 READ_TIMESTAMP(inst1);
1242 loop1 = inst1;
1243 }
1244 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
1245 intr0, intr1);
1246 ticked = 0;
1247 inst1 = 0;
1248 intr0 = 0;
1249 intr1 = 0;
1250 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001251#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001252 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1253 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001254
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 /* Do periodic things. Doing this every time through
1256 the loop would add too much overhead, so we do it
1257 only every Nth instruction. We also do it if
1258 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1259 event needs attention (e.g. a signal handler or
1260 async I/O handler); see Py_AddPendingCall() and
1261 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001262
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001263 if (_Py_atomic_load_relaxed(&eval_breaker)) {
1264 if (*next_instr == SETUP_FINALLY) {
1265 /* Make the last opcode before
1266 a try: finally: block uninterruptable. */
1267 goto fast_next_opcode;
1268 }
1269 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001270#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 ticked = 1;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001272#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001273 if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) {
1274 if (Py_MakePendingCalls() < 0) {
1275 why = WHY_EXCEPTION;
1276 goto on_error;
1277 }
1278 }
1279 if (_Py_atomic_load_relaxed(&gil_drop_request)) {
Guido van Rossume59214e1994-08-30 08:01:59 +00001280#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 /* Give another thread a chance */
1282 if (PyThreadState_Swap(NULL) != tstate)
1283 Py_FatalError("ceval: tstate mix-up");
1284 drop_gil(tstate);
1285
1286 /* Other threads may run now */
1287
1288 take_gil(tstate);
1289 if (PyThreadState_Swap(tstate) != NULL)
1290 Py_FatalError("ceval: orphan tstate");
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001291#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 }
1293 /* Check for asynchronous exceptions. */
1294 if (tstate->async_exc != NULL) {
1295 x = tstate->async_exc;
1296 tstate->async_exc = NULL;
1297 UNSIGNAL_ASYNC_EXC();
1298 PyErr_SetNone(x);
1299 Py_DECREF(x);
1300 why = WHY_EXCEPTION;
1301 goto on_error;
1302 }
1303 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001304
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001305 fast_next_opcode:
1306 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001307
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 if (_Py_TracingPossible &&
1311 tstate->c_tracefunc != NULL && !tstate->tracing) {
1312 /* see maybe_call_line_trace
1313 for expository comments */
1314 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001315
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001316 err = maybe_call_line_trace(tstate->c_tracefunc,
1317 tstate->c_traceobj,
1318 f, &instr_lb, &instr_ub,
1319 &instr_prev);
1320 /* Reload possibly changed frame fields */
1321 JUMPTO(f->f_lasti);
1322 if (f->f_stacktop != NULL) {
1323 stack_pointer = f->f_stacktop;
1324 f->f_stacktop = NULL;
1325 }
1326 if (err) {
1327 /* trace function raised an exception */
1328 goto on_error;
1329 }
1330 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001331
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001332 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 opcode = NEXTOP();
1335 oparg = 0; /* allows oparg to be stored in a register because
1336 it doesn't have to be remembered across a full loop */
1337 if (HAS_ARG(opcode))
1338 oparg = NEXTARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001339 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001340#ifdef DYNAMIC_EXECUTION_PROFILE
1341#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 dxpairs[lastopcode][opcode]++;
1343 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001344#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001346#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001347
Guido van Rossum96a42c81992-01-12 02:29:51 +00001348#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001349 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001350
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001351 if (lltrace) {
1352 if (HAS_ARG(opcode)) {
1353 printf("%d: %d, %d\n",
1354 f->f_lasti, opcode, oparg);
1355 }
1356 else {
1357 printf("%d: %d\n",
1358 f->f_lasti, opcode);
1359 }
1360 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001361#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 /* Main switch on opcode */
1364 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001368 /* BEWARE!
1369 It is essential that any operation that fails sets either
1370 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1371 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 TARGET(NOP)
1376 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001377
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 TARGET(LOAD_FAST)
1379 x = GETLOCAL(oparg);
1380 if (x != NULL) {
1381 Py_INCREF(x);
1382 PUSH(x);
1383 FAST_DISPATCH();
1384 }
1385 format_exc_check_arg(PyExc_UnboundLocalError,
1386 UNBOUNDLOCAL_ERROR_MSG,
1387 PyTuple_GetItem(co->co_varnames, oparg));
1388 break;
Neil Schemenauer63543862002-02-17 19:10:14 +00001389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 TARGET(LOAD_CONST)
1391 x = GETITEM(consts, oparg);
1392 Py_INCREF(x);
1393 PUSH(x);
1394 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001396 PREDICTED_WITH_ARG(STORE_FAST);
1397 TARGET(STORE_FAST)
1398 v = POP();
1399 SETLOCAL(oparg, v);
1400 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001402 TARGET(POP_TOP)
1403 v = POP();
1404 Py_DECREF(v);
1405 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001407 TARGET(ROT_TWO)
1408 v = TOP();
1409 w = SECOND();
1410 SET_TOP(w);
1411 SET_SECOND(v);
1412 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001414 TARGET(ROT_THREE)
1415 v = TOP();
1416 w = SECOND();
1417 x = THIRD();
1418 SET_TOP(w);
1419 SET_SECOND(x);
1420 SET_THIRD(v);
1421 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001422
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001423 TARGET(ROT_FOUR)
1424 u = TOP();
1425 v = SECOND();
1426 w = THIRD();
1427 x = FOURTH();
1428 SET_TOP(v);
1429 SET_SECOND(w);
1430 SET_THIRD(x);
1431 SET_FOURTH(u);
1432 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001434 TARGET(DUP_TOP)
1435 v = TOP();
1436 Py_INCREF(v);
1437 PUSH(v);
1438 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001440 TARGET(DUP_TOPX)
1441 if (oparg == 2) {
1442 x = TOP();
1443 Py_INCREF(x);
1444 w = SECOND();
1445 Py_INCREF(w);
1446 STACKADJ(2);
1447 SET_TOP(x);
1448 SET_SECOND(w);
1449 FAST_DISPATCH();
1450 } else if (oparg == 3) {
1451 x = TOP();
1452 Py_INCREF(x);
1453 w = SECOND();
1454 Py_INCREF(w);
1455 v = THIRD();
1456 Py_INCREF(v);
1457 STACKADJ(3);
1458 SET_TOP(x);
1459 SET_SECOND(w);
1460 SET_THIRD(v);
1461 FAST_DISPATCH();
1462 }
1463 Py_FatalError("invalid argument to DUP_TOPX"
1464 " (bytecode corruption?)");
1465 /* Never returns, so don't bother to set why. */
1466 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001467
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001468 TARGET(UNARY_POSITIVE)
1469 v = TOP();
1470 x = PyNumber_Positive(v);
1471 Py_DECREF(v);
1472 SET_TOP(x);
1473 if (x != NULL) DISPATCH();
1474 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001475
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001476 TARGET(UNARY_NEGATIVE)
1477 v = TOP();
1478 x = PyNumber_Negative(v);
1479 Py_DECREF(v);
1480 SET_TOP(x);
1481 if (x != NULL) DISPATCH();
1482 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 TARGET(UNARY_NOT)
1485 v = TOP();
1486 err = PyObject_IsTrue(v);
1487 Py_DECREF(v);
1488 if (err == 0) {
1489 Py_INCREF(Py_True);
1490 SET_TOP(Py_True);
1491 DISPATCH();
1492 }
1493 else if (err > 0) {
1494 Py_INCREF(Py_False);
1495 SET_TOP(Py_False);
1496 err = 0;
1497 DISPATCH();
1498 }
1499 STACKADJ(-1);
1500 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001501
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001502 TARGET(UNARY_INVERT)
1503 v = TOP();
1504 x = PyNumber_Invert(v);
1505 Py_DECREF(v);
1506 SET_TOP(x);
1507 if (x != NULL) DISPATCH();
1508 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001509
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001510 TARGET(BINARY_POWER)
1511 w = POP();
1512 v = TOP();
1513 x = PyNumber_Power(v, w, Py_None);
1514 Py_DECREF(v);
1515 Py_DECREF(w);
1516 SET_TOP(x);
1517 if (x != NULL) DISPATCH();
1518 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001519
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001520 TARGET(BINARY_MULTIPLY)
1521 w = POP();
1522 v = TOP();
1523 x = PyNumber_Multiply(v, w);
1524 Py_DECREF(v);
1525 Py_DECREF(w);
1526 SET_TOP(x);
1527 if (x != NULL) DISPATCH();
1528 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001529
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001530 TARGET(BINARY_TRUE_DIVIDE)
1531 w = POP();
1532 v = TOP();
1533 x = PyNumber_TrueDivide(v, w);
1534 Py_DECREF(v);
1535 Py_DECREF(w);
1536 SET_TOP(x);
1537 if (x != NULL) DISPATCH();
1538 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001539
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001540 TARGET(BINARY_FLOOR_DIVIDE)
1541 w = POP();
1542 v = TOP();
1543 x = PyNumber_FloorDivide(v, w);
1544 Py_DECREF(v);
1545 Py_DECREF(w);
1546 SET_TOP(x);
1547 if (x != NULL) DISPATCH();
1548 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001549
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001550 TARGET(BINARY_MODULO)
1551 w = POP();
1552 v = TOP();
1553 if (PyUnicode_CheckExact(v))
1554 x = PyUnicode_Format(v, w);
1555 else
1556 x = PyNumber_Remainder(v, w);
1557 Py_DECREF(v);
1558 Py_DECREF(w);
1559 SET_TOP(x);
1560 if (x != NULL) DISPATCH();
1561 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 TARGET(BINARY_ADD)
1564 w = POP();
1565 v = TOP();
1566 if (PyUnicode_CheckExact(v) &&
1567 PyUnicode_CheckExact(w)) {
1568 x = unicode_concatenate(v, w, f, next_instr);
1569 /* unicode_concatenate consumed the ref to v */
1570 goto skip_decref_vx;
1571 }
1572 else {
1573 x = PyNumber_Add(v, w);
1574 }
1575 Py_DECREF(v);
1576 skip_decref_vx:
1577 Py_DECREF(w);
1578 SET_TOP(x);
1579 if (x != NULL) DISPATCH();
1580 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001582 TARGET(BINARY_SUBTRACT)
1583 w = POP();
1584 v = TOP();
1585 x = PyNumber_Subtract(v, w);
1586 Py_DECREF(v);
1587 Py_DECREF(w);
1588 SET_TOP(x);
1589 if (x != NULL) DISPATCH();
1590 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001591
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001592 TARGET(BINARY_SUBSCR)
1593 w = POP();
1594 v = TOP();
1595 x = PyObject_GetItem(v, w);
1596 Py_DECREF(v);
1597 Py_DECREF(w);
1598 SET_TOP(x);
1599 if (x != NULL) DISPATCH();
1600 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001601
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001602 TARGET(BINARY_LSHIFT)
1603 w = POP();
1604 v = TOP();
1605 x = PyNumber_Lshift(v, w);
1606 Py_DECREF(v);
1607 Py_DECREF(w);
1608 SET_TOP(x);
1609 if (x != NULL) DISPATCH();
1610 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001611
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001612 TARGET(BINARY_RSHIFT)
1613 w = POP();
1614 v = TOP();
1615 x = PyNumber_Rshift(v, w);
1616 Py_DECREF(v);
1617 Py_DECREF(w);
1618 SET_TOP(x);
1619 if (x != NULL) DISPATCH();
1620 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001622 TARGET(BINARY_AND)
1623 w = POP();
1624 v = TOP();
1625 x = PyNumber_And(v, w);
1626 Py_DECREF(v);
1627 Py_DECREF(w);
1628 SET_TOP(x);
1629 if (x != NULL) DISPATCH();
1630 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001631
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001632 TARGET(BINARY_XOR)
1633 w = POP();
1634 v = TOP();
1635 x = PyNumber_Xor(v, w);
1636 Py_DECREF(v);
1637 Py_DECREF(w);
1638 SET_TOP(x);
1639 if (x != NULL) DISPATCH();
1640 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001642 TARGET(BINARY_OR)
1643 w = POP();
1644 v = TOP();
1645 x = PyNumber_Or(v, w);
1646 Py_DECREF(v);
1647 Py_DECREF(w);
1648 SET_TOP(x);
1649 if (x != NULL) DISPATCH();
1650 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001651
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001652 TARGET(LIST_APPEND)
1653 w = POP();
1654 v = PEEK(oparg);
1655 err = PyList_Append(v, w);
1656 Py_DECREF(w);
1657 if (err == 0) {
1658 PREDICT(JUMP_ABSOLUTE);
1659 DISPATCH();
1660 }
1661 break;
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001662
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 TARGET(SET_ADD)
1664 w = POP();
1665 v = stack_pointer[-oparg];
1666 err = PySet_Add(v, w);
1667 Py_DECREF(w);
1668 if (err == 0) {
1669 PREDICT(JUMP_ABSOLUTE);
1670 DISPATCH();
1671 }
1672 break;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001673
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 TARGET(INPLACE_POWER)
1675 w = POP();
1676 v = TOP();
1677 x = PyNumber_InPlacePower(v, w, Py_None);
1678 Py_DECREF(v);
1679 Py_DECREF(w);
1680 SET_TOP(x);
1681 if (x != NULL) DISPATCH();
1682 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001683
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001684 TARGET(INPLACE_MULTIPLY)
1685 w = POP();
1686 v = TOP();
1687 x = PyNumber_InPlaceMultiply(v, w);
1688 Py_DECREF(v);
1689 Py_DECREF(w);
1690 SET_TOP(x);
1691 if (x != NULL) DISPATCH();
1692 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001693
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001694 TARGET(INPLACE_TRUE_DIVIDE)
1695 w = POP();
1696 v = TOP();
1697 x = PyNumber_InPlaceTrueDivide(v, w);
1698 Py_DECREF(v);
1699 Py_DECREF(w);
1700 SET_TOP(x);
1701 if (x != NULL) DISPATCH();
1702 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001703
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001704 TARGET(INPLACE_FLOOR_DIVIDE)
1705 w = POP();
1706 v = TOP();
1707 x = PyNumber_InPlaceFloorDivide(v, w);
1708 Py_DECREF(v);
1709 Py_DECREF(w);
1710 SET_TOP(x);
1711 if (x != NULL) DISPATCH();
1712 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001713
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001714 TARGET(INPLACE_MODULO)
1715 w = POP();
1716 v = TOP();
1717 x = PyNumber_InPlaceRemainder(v, w);
1718 Py_DECREF(v);
1719 Py_DECREF(w);
1720 SET_TOP(x);
1721 if (x != NULL) DISPATCH();
1722 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001723
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001724 TARGET(INPLACE_ADD)
1725 w = POP();
1726 v = TOP();
1727 if (PyUnicode_CheckExact(v) &&
1728 PyUnicode_CheckExact(w)) {
1729 x = unicode_concatenate(v, w, f, next_instr);
1730 /* unicode_concatenate consumed the ref to v */
1731 goto skip_decref_v;
1732 }
1733 else {
1734 x = PyNumber_InPlaceAdd(v, w);
1735 }
1736 Py_DECREF(v);
1737 skip_decref_v:
1738 Py_DECREF(w);
1739 SET_TOP(x);
1740 if (x != NULL) DISPATCH();
1741 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001743 TARGET(INPLACE_SUBTRACT)
1744 w = POP();
1745 v = TOP();
1746 x = PyNumber_InPlaceSubtract(v, w);
1747 Py_DECREF(v);
1748 Py_DECREF(w);
1749 SET_TOP(x);
1750 if (x != NULL) DISPATCH();
1751 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001752
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001753 TARGET(INPLACE_LSHIFT)
1754 w = POP();
1755 v = TOP();
1756 x = PyNumber_InPlaceLshift(v, w);
1757 Py_DECREF(v);
1758 Py_DECREF(w);
1759 SET_TOP(x);
1760 if (x != NULL) DISPATCH();
1761 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001762
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001763 TARGET(INPLACE_RSHIFT)
1764 w = POP();
1765 v = TOP();
1766 x = PyNumber_InPlaceRshift(v, w);
1767 Py_DECREF(v);
1768 Py_DECREF(w);
1769 SET_TOP(x);
1770 if (x != NULL) DISPATCH();
1771 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001772
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001773 TARGET(INPLACE_AND)
1774 w = POP();
1775 v = TOP();
1776 x = PyNumber_InPlaceAnd(v, w);
1777 Py_DECREF(v);
1778 Py_DECREF(w);
1779 SET_TOP(x);
1780 if (x != NULL) DISPATCH();
1781 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001782
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001783 TARGET(INPLACE_XOR)
1784 w = POP();
1785 v = TOP();
1786 x = PyNumber_InPlaceXor(v, w);
1787 Py_DECREF(v);
1788 Py_DECREF(w);
1789 SET_TOP(x);
1790 if (x != NULL) DISPATCH();
1791 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001792
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001793 TARGET(INPLACE_OR)
1794 w = POP();
1795 v = TOP();
1796 x = PyNumber_InPlaceOr(v, w);
1797 Py_DECREF(v);
1798 Py_DECREF(w);
1799 SET_TOP(x);
1800 if (x != NULL) DISPATCH();
1801 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001802
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001803 TARGET(STORE_SUBSCR)
1804 w = TOP();
1805 v = SECOND();
1806 u = THIRD();
1807 STACKADJ(-3);
1808 /* v[w] = u */
1809 err = PyObject_SetItem(v, w, u);
1810 Py_DECREF(u);
1811 Py_DECREF(v);
1812 Py_DECREF(w);
1813 if (err == 0) DISPATCH();
1814 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001816 TARGET(DELETE_SUBSCR)
1817 w = TOP();
1818 v = SECOND();
1819 STACKADJ(-2);
1820 /* del v[w] */
1821 err = PyObject_DelItem(v, w);
1822 Py_DECREF(v);
1823 Py_DECREF(w);
1824 if (err == 0) DISPATCH();
1825 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001826
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001827 TARGET(PRINT_EXPR)
1828 v = POP();
1829 w = PySys_GetObject("displayhook");
1830 if (w == NULL) {
1831 PyErr_SetString(PyExc_RuntimeError,
1832 "lost sys.displayhook");
1833 err = -1;
1834 x = NULL;
1835 }
1836 if (err == 0) {
1837 x = PyTuple_Pack(1, v);
1838 if (x == NULL)
1839 err = -1;
1840 }
1841 if (err == 0) {
1842 w = PyEval_CallObject(w, x);
1843 Py_XDECREF(w);
1844 if (w == NULL)
1845 err = -1;
1846 }
1847 Py_DECREF(v);
1848 Py_XDECREF(x);
1849 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001850
Thomas Wouters434d0822000-08-24 20:11:32 +00001851#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001852 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001853#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001854 TARGET(RAISE_VARARGS)
1855 v = w = NULL;
1856 switch (oparg) {
1857 case 2:
1858 v = POP(); /* cause */
1859 case 1:
1860 w = POP(); /* exc */
1861 case 0: /* Fallthrough */
1862 why = do_raise(w, v);
1863 break;
1864 default:
1865 PyErr_SetString(PyExc_SystemError,
1866 "bad RAISE_VARARGS oparg");
1867 why = WHY_EXCEPTION;
1868 break;
1869 }
1870 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001871
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001872 TARGET(STORE_LOCALS)
1873 x = POP();
1874 v = f->f_locals;
1875 Py_XDECREF(v);
1876 f->f_locals = x;
1877 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001878
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001879 TARGET(RETURN_VALUE)
1880 retval = POP();
1881 why = WHY_RETURN;
1882 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001883
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001884 TARGET(YIELD_VALUE)
1885 retval = POP();
1886 f->f_stacktop = stack_pointer;
1887 why = WHY_YIELD;
1888 /* Put aside the current exception state and restore
1889 that of the calling frame. This only serves when
1890 "yield" is used inside an except handler. */
1891 SWAP_EXC_STATE();
1892 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001893
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001894 TARGET(POP_EXCEPT)
1895 {
1896 PyTryBlock *b = PyFrame_BlockPop(f);
1897 if (b->b_type != EXCEPT_HANDLER) {
1898 PyErr_SetString(PyExc_SystemError,
1899 "popped block is not an except handler");
1900 why = WHY_EXCEPTION;
1901 break;
1902 }
1903 UNWIND_EXCEPT_HANDLER(b);
1904 }
1905 DISPATCH();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001906
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001907 TARGET(POP_BLOCK)
1908 {
1909 PyTryBlock *b = PyFrame_BlockPop(f);
1910 UNWIND_BLOCK(b);
1911 }
1912 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001913
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001914 PREDICTED(END_FINALLY);
1915 TARGET(END_FINALLY)
1916 v = POP();
1917 if (PyLong_Check(v)) {
1918 why = (enum why_code) PyLong_AS_LONG(v);
1919 assert(why != WHY_YIELD);
1920 if (why == WHY_RETURN ||
1921 why == WHY_CONTINUE)
1922 retval = POP();
1923 if (why == WHY_SILENCED) {
1924 /* An exception was silenced by 'with', we must
1925 manually unwind the EXCEPT_HANDLER block which was
1926 created when the exception was caught, otherwise
1927 the stack will be in an inconsistent state. */
1928 PyTryBlock *b = PyFrame_BlockPop(f);
1929 assert(b->b_type == EXCEPT_HANDLER);
1930 UNWIND_EXCEPT_HANDLER(b);
1931 why = WHY_NOT;
1932 }
1933 }
1934 else if (PyExceptionClass_Check(v)) {
1935 w = POP();
1936 u = POP();
1937 PyErr_Restore(v, w, u);
1938 why = WHY_RERAISE;
1939 break;
1940 }
1941 else if (v != Py_None) {
1942 PyErr_SetString(PyExc_SystemError,
1943 "'finally' pops bad exception");
1944 why = WHY_EXCEPTION;
1945 }
1946 Py_DECREF(v);
1947 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001948
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001949 TARGET(LOAD_BUILD_CLASS)
1950 x = PyDict_GetItemString(f->f_builtins,
1951 "__build_class__");
1952 if (x == NULL) {
1953 PyErr_SetString(PyExc_ImportError,
1954 "__build_class__ not found");
1955 break;
1956 }
1957 Py_INCREF(x);
1958 PUSH(x);
1959 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001961 TARGET(STORE_NAME)
1962 w = GETITEM(names, oparg);
1963 v = POP();
1964 if ((x = f->f_locals) != NULL) {
1965 if (PyDict_CheckExact(x))
1966 err = PyDict_SetItem(x, w, v);
1967 else
1968 err = PyObject_SetItem(x, w, v);
1969 Py_DECREF(v);
1970 if (err == 0) DISPATCH();
1971 break;
1972 }
1973 PyErr_Format(PyExc_SystemError,
1974 "no locals found when storing %R", w);
1975 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001976
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001977 TARGET(DELETE_NAME)
1978 w = GETITEM(names, oparg);
1979 if ((x = f->f_locals) != NULL) {
1980 if ((err = PyObject_DelItem(x, w)) != 0)
1981 format_exc_check_arg(PyExc_NameError,
1982 NAME_ERROR_MSG,
1983 w);
1984 break;
1985 }
1986 PyErr_Format(PyExc_SystemError,
1987 "no locals when deleting %R", w);
1988 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001989
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001990 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
1991 TARGET(UNPACK_SEQUENCE)
1992 v = POP();
1993 if (PyTuple_CheckExact(v) &&
1994 PyTuple_GET_SIZE(v) == oparg) {
1995 PyObject **items = \
1996 ((PyTupleObject *)v)->ob_item;
1997 while (oparg--) {
1998 w = items[oparg];
1999 Py_INCREF(w);
2000 PUSH(w);
2001 }
2002 Py_DECREF(v);
2003 DISPATCH();
2004 } else if (PyList_CheckExact(v) &&
2005 PyList_GET_SIZE(v) == oparg) {
2006 PyObject **items = \
2007 ((PyListObject *)v)->ob_item;
2008 while (oparg--) {
2009 w = items[oparg];
2010 Py_INCREF(w);
2011 PUSH(w);
2012 }
2013 } else if (unpack_iterable(v, oparg, -1,
2014 stack_pointer + oparg)) {
2015 STACKADJ(oparg);
2016 } else {
2017 /* unpack_iterable() raised an exception */
2018 why = WHY_EXCEPTION;
2019 }
2020 Py_DECREF(v);
2021 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002022
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002023 TARGET(UNPACK_EX)
2024 {
2025 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2026 v = POP();
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002027
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002028 if (unpack_iterable(v, oparg & 0xFF, oparg >> 8,
2029 stack_pointer + totalargs)) {
2030 stack_pointer += totalargs;
2031 } else {
2032 why = WHY_EXCEPTION;
2033 }
2034 Py_DECREF(v);
2035 break;
2036 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002037
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002038 TARGET(STORE_ATTR)
2039 w = GETITEM(names, oparg);
2040 v = TOP();
2041 u = SECOND();
2042 STACKADJ(-2);
2043 err = PyObject_SetAttr(v, w, u); /* v.w = u */
2044 Py_DECREF(v);
2045 Py_DECREF(u);
2046 if (err == 0) DISPATCH();
2047 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002049 TARGET(DELETE_ATTR)
2050 w = GETITEM(names, oparg);
2051 v = POP();
2052 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
2053 /* del v.w */
2054 Py_DECREF(v);
2055 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002056
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002057 TARGET(STORE_GLOBAL)
2058 w = GETITEM(names, oparg);
2059 v = POP();
2060 err = PyDict_SetItem(f->f_globals, w, v);
2061 Py_DECREF(v);
2062 if (err == 0) DISPATCH();
2063 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002065 TARGET(DELETE_GLOBAL)
2066 w = GETITEM(names, oparg);
2067 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
2068 format_exc_check_arg(
2069 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
2070 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002071
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002072 TARGET(LOAD_NAME)
2073 w = GETITEM(names, oparg);
2074 if ((v = f->f_locals) == NULL) {
2075 PyErr_Format(PyExc_SystemError,
2076 "no locals when loading %R", w);
2077 why = WHY_EXCEPTION;
2078 break;
2079 }
2080 if (PyDict_CheckExact(v)) {
2081 x = PyDict_GetItem(v, w);
2082 Py_XINCREF(x);
2083 }
2084 else {
2085 x = PyObject_GetItem(v, w);
2086 if (x == NULL && PyErr_Occurred()) {
2087 if (!PyErr_ExceptionMatches(
2088 PyExc_KeyError))
2089 break;
2090 PyErr_Clear();
2091 }
2092 }
2093 if (x == NULL) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002094 x = PyDict_GetItem(f->f_globals, w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002095 if (x == NULL) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002096 x = PyDict_GetItem(f->f_builtins, w);
2097 if (x == NULL) {
2098 format_exc_check_arg(
2099 PyExc_NameError,
2100 NAME_ERROR_MSG, w);
2101 break;
2102 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002103 }
2104 Py_INCREF(x);
2105 }
2106 PUSH(x);
2107 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002108
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002109 TARGET(LOAD_GLOBAL)
2110 w = GETITEM(names, oparg);
2111 if (PyUnicode_CheckExact(w)) {
2112 /* Inline the PyDict_GetItem() calls.
2113 WARNING: this is an extreme speed hack.
2114 Do not try this at home. */
2115 long hash = ((PyUnicodeObject *)w)->hash;
2116 if (hash != -1) {
2117 PyDictObject *d;
2118 PyDictEntry *e;
2119 d = (PyDictObject *)(f->f_globals);
2120 e = d->ma_lookup(d, w, hash);
2121 if (e == NULL) {
2122 x = NULL;
2123 break;
2124 }
2125 x = e->me_value;
2126 if (x != NULL) {
2127 Py_INCREF(x);
2128 PUSH(x);
2129 DISPATCH();
2130 }
2131 d = (PyDictObject *)(f->f_builtins);
2132 e = d->ma_lookup(d, w, hash);
2133 if (e == NULL) {
2134 x = NULL;
2135 break;
2136 }
2137 x = e->me_value;
2138 if (x != NULL) {
2139 Py_INCREF(x);
2140 PUSH(x);
2141 DISPATCH();
2142 }
2143 goto load_global_error;
2144 }
2145 }
2146 /* This is the un-inlined version of the code above */
2147 x = PyDict_GetItem(f->f_globals, w);
2148 if (x == NULL) {
2149 x = PyDict_GetItem(f->f_builtins, w);
2150 if (x == NULL) {
2151 load_global_error:
2152 format_exc_check_arg(
2153 PyExc_NameError,
2154 GLOBAL_NAME_ERROR_MSG, w);
2155 break;
2156 }
2157 }
2158 Py_INCREF(x);
2159 PUSH(x);
2160 DISPATCH();
Guido van Rossum681d79a1995-07-18 14:51:37 +00002161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002162 TARGET(DELETE_FAST)
2163 x = GETLOCAL(oparg);
2164 if (x != NULL) {
2165 SETLOCAL(oparg, NULL);
2166 DISPATCH();
2167 }
2168 format_exc_check_arg(
2169 PyExc_UnboundLocalError,
2170 UNBOUNDLOCAL_ERROR_MSG,
2171 PyTuple_GetItem(co->co_varnames, oparg)
2172 );
2173 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002174
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002175 TARGET(LOAD_CLOSURE)
2176 x = freevars[oparg];
2177 Py_INCREF(x);
2178 PUSH(x);
2179 if (x != NULL) DISPATCH();
2180 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002181
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002182 TARGET(LOAD_DEREF)
2183 x = freevars[oparg];
2184 w = PyCell_Get(x);
2185 if (w != NULL) {
2186 PUSH(w);
2187 DISPATCH();
2188 }
2189 err = -1;
2190 /* Don't stomp existing exception */
2191 if (PyErr_Occurred())
2192 break;
2193 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
2194 v = PyTuple_GET_ITEM(co->co_cellvars,
Stefan Krahb7e10102010-06-23 18:42:39 +00002195 oparg);
2196 format_exc_check_arg(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002197 PyExc_UnboundLocalError,
2198 UNBOUNDLOCAL_ERROR_MSG,
2199 v);
2200 } else {
2201 v = PyTuple_GET_ITEM(co->co_freevars, oparg -
2202 PyTuple_GET_SIZE(co->co_cellvars));
2203 format_exc_check_arg(PyExc_NameError,
2204 UNBOUNDFREE_ERROR_MSG, v);
2205 }
2206 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002208 TARGET(STORE_DEREF)
2209 w = POP();
2210 x = freevars[oparg];
2211 PyCell_Set(x, w);
2212 Py_DECREF(w);
2213 DISPATCH();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002214
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002215 TARGET(BUILD_TUPLE)
2216 x = PyTuple_New(oparg);
2217 if (x != NULL) {
2218 for (; --oparg >= 0;) {
2219 w = POP();
2220 PyTuple_SET_ITEM(x, oparg, w);
2221 }
2222 PUSH(x);
2223 DISPATCH();
2224 }
2225 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002227 TARGET(BUILD_LIST)
2228 x = PyList_New(oparg);
2229 if (x != NULL) {
2230 for (; --oparg >= 0;) {
2231 w = POP();
2232 PyList_SET_ITEM(x, oparg, w);
2233 }
2234 PUSH(x);
2235 DISPATCH();
2236 }
2237 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002239 TARGET(BUILD_SET)
2240 x = PySet_New(NULL);
2241 if (x != NULL) {
2242 for (; --oparg >= 0;) {
2243 w = POP();
2244 if (err == 0)
2245 err = PySet_Add(x, w);
2246 Py_DECREF(w);
2247 }
2248 if (err != 0) {
2249 Py_DECREF(x);
2250 break;
2251 }
2252 PUSH(x);
2253 DISPATCH();
2254 }
2255 break;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002257 TARGET(BUILD_MAP)
2258 x = _PyDict_NewPresized((Py_ssize_t)oparg);
2259 PUSH(x);
2260 if (x != NULL) DISPATCH();
2261 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002262
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002263 TARGET(STORE_MAP)
2264 w = TOP(); /* key */
2265 u = SECOND(); /* value */
2266 v = THIRD(); /* dict */
2267 STACKADJ(-2);
2268 assert (PyDict_CheckExact(v));
2269 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2270 Py_DECREF(u);
2271 Py_DECREF(w);
2272 if (err == 0) DISPATCH();
2273 break;
Christian Heimes99170a52007-12-19 02:07:34 +00002274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002275 TARGET(MAP_ADD)
2276 w = TOP(); /* key */
2277 u = SECOND(); /* value */
2278 STACKADJ(-2);
2279 v = stack_pointer[-oparg]; /* dict */
2280 assert (PyDict_CheckExact(v));
2281 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2282 Py_DECREF(u);
2283 Py_DECREF(w);
2284 if (err == 0) {
2285 PREDICT(JUMP_ABSOLUTE);
2286 DISPATCH();
2287 }
2288 break;
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002290 TARGET(LOAD_ATTR)
2291 w = GETITEM(names, oparg);
2292 v = TOP();
2293 x = PyObject_GetAttr(v, w);
2294 Py_DECREF(v);
2295 SET_TOP(x);
2296 if (x != NULL) DISPATCH();
2297 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002299 TARGET(COMPARE_OP)
2300 w = POP();
2301 v = TOP();
2302 x = cmp_outcome(oparg, v, w);
2303 Py_DECREF(v);
2304 Py_DECREF(w);
2305 SET_TOP(x);
2306 if (x == NULL) break;
2307 PREDICT(POP_JUMP_IF_FALSE);
2308 PREDICT(POP_JUMP_IF_TRUE);
2309 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002311 TARGET(IMPORT_NAME)
2312 w = GETITEM(names, oparg);
2313 x = PyDict_GetItemString(f->f_builtins, "__import__");
2314 if (x == NULL) {
2315 PyErr_SetString(PyExc_ImportError,
2316 "__import__ not found");
2317 break;
2318 }
2319 Py_INCREF(x);
2320 v = POP();
2321 u = TOP();
2322 if (PyLong_AsLong(u) != -1 || PyErr_Occurred())
2323 w = PyTuple_Pack(5,
2324 w,
2325 f->f_globals,
2326 f->f_locals == NULL ?
2327 Py_None : f->f_locals,
2328 v,
2329 u);
2330 else
2331 w = PyTuple_Pack(4,
2332 w,
2333 f->f_globals,
2334 f->f_locals == NULL ?
2335 Py_None : f->f_locals,
2336 v);
2337 Py_DECREF(v);
2338 Py_DECREF(u);
2339 if (w == NULL) {
2340 u = POP();
2341 Py_DECREF(x);
2342 x = NULL;
2343 break;
2344 }
2345 READ_TIMESTAMP(intr0);
2346 v = x;
2347 x = PyEval_CallObject(v, w);
2348 Py_DECREF(v);
2349 READ_TIMESTAMP(intr1);
2350 Py_DECREF(w);
2351 SET_TOP(x);
2352 if (x != NULL) DISPATCH();
2353 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002354
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002355 TARGET(IMPORT_STAR)
2356 v = POP();
2357 PyFrame_FastToLocals(f);
2358 if ((x = f->f_locals) == NULL) {
2359 PyErr_SetString(PyExc_SystemError,
2360 "no locals found during 'import *'");
2361 break;
2362 }
2363 READ_TIMESTAMP(intr0);
2364 err = import_all_from(x, v);
2365 READ_TIMESTAMP(intr1);
2366 PyFrame_LocalsToFast(f, 0);
2367 Py_DECREF(v);
2368 if (err == 0) DISPATCH();
2369 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002371 TARGET(IMPORT_FROM)
2372 w = GETITEM(names, oparg);
2373 v = TOP();
2374 READ_TIMESTAMP(intr0);
2375 x = import_from(v, w);
2376 READ_TIMESTAMP(intr1);
2377 PUSH(x);
2378 if (x != NULL) DISPATCH();
2379 break;
Thomas Wouters52152252000-08-17 22:55:00 +00002380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002381 TARGET(JUMP_FORWARD)
2382 JUMPBY(oparg);
2383 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002385 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
2386 TARGET(POP_JUMP_IF_FALSE)
2387 w = POP();
2388 if (w == Py_True) {
2389 Py_DECREF(w);
2390 FAST_DISPATCH();
2391 }
2392 if (w == Py_False) {
2393 Py_DECREF(w);
2394 JUMPTO(oparg);
2395 FAST_DISPATCH();
2396 }
2397 err = PyObject_IsTrue(w);
2398 Py_DECREF(w);
2399 if (err > 0)
2400 err = 0;
2401 else if (err == 0)
2402 JUMPTO(oparg);
2403 else
2404 break;
2405 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002407 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
2408 TARGET(POP_JUMP_IF_TRUE)
2409 w = POP();
2410 if (w == Py_False) {
2411 Py_DECREF(w);
2412 FAST_DISPATCH();
2413 }
2414 if (w == Py_True) {
2415 Py_DECREF(w);
2416 JUMPTO(oparg);
2417 FAST_DISPATCH();
2418 }
2419 err = PyObject_IsTrue(w);
2420 Py_DECREF(w);
2421 if (err > 0) {
2422 err = 0;
2423 JUMPTO(oparg);
2424 }
2425 else if (err == 0)
2426 ;
2427 else
2428 break;
2429 DISPATCH();
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002430
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002431 TARGET(JUMP_IF_FALSE_OR_POP)
2432 w = TOP();
2433 if (w == Py_True) {
2434 STACKADJ(-1);
2435 Py_DECREF(w);
2436 FAST_DISPATCH();
2437 }
2438 if (w == Py_False) {
2439 JUMPTO(oparg);
2440 FAST_DISPATCH();
2441 }
2442 err = PyObject_IsTrue(w);
2443 if (err > 0) {
2444 STACKADJ(-1);
2445 Py_DECREF(w);
2446 err = 0;
2447 }
2448 else if (err == 0)
2449 JUMPTO(oparg);
2450 else
2451 break;
2452 DISPATCH();
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002454 TARGET(JUMP_IF_TRUE_OR_POP)
2455 w = TOP();
2456 if (w == Py_False) {
2457 STACKADJ(-1);
2458 Py_DECREF(w);
2459 FAST_DISPATCH();
2460 }
2461 if (w == Py_True) {
2462 JUMPTO(oparg);
2463 FAST_DISPATCH();
2464 }
2465 err = PyObject_IsTrue(w);
2466 if (err > 0) {
2467 err = 0;
2468 JUMPTO(oparg);
2469 }
2470 else if (err == 0) {
2471 STACKADJ(-1);
2472 Py_DECREF(w);
2473 }
2474 else
2475 break;
2476 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002478 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
2479 TARGET(JUMP_ABSOLUTE)
2480 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002481#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002482 /* Enabling this path speeds-up all while and for-loops by bypassing
2483 the per-loop checks for signals. By default, this should be turned-off
2484 because it prevents detection of a control-break in tight loops like
2485 "while 1: pass". Compile with this option turned-on when you need
2486 the speed-up and do not need break checking inside tight loops (ones
2487 that contain only instructions ending with FAST_DISPATCH).
2488 */
2489 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002490#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002491 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002492#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002493
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002494 TARGET(GET_ITER)
2495 /* before: [obj]; after [getiter(obj)] */
2496 v = TOP();
2497 x = PyObject_GetIter(v);
2498 Py_DECREF(v);
2499 if (x != NULL) {
2500 SET_TOP(x);
2501 PREDICT(FOR_ITER);
2502 DISPATCH();
2503 }
2504 STACKADJ(-1);
2505 break;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002507 PREDICTED_WITH_ARG(FOR_ITER);
2508 TARGET(FOR_ITER)
2509 /* before: [iter]; after: [iter, iter()] *or* [] */
2510 v = TOP();
2511 x = (*v->ob_type->tp_iternext)(v);
2512 if (x != NULL) {
2513 PUSH(x);
2514 PREDICT(STORE_FAST);
2515 PREDICT(UNPACK_SEQUENCE);
2516 DISPATCH();
2517 }
2518 if (PyErr_Occurred()) {
2519 if (!PyErr_ExceptionMatches(
2520 PyExc_StopIteration))
2521 break;
2522 PyErr_Clear();
2523 }
2524 /* iterator ended normally */
2525 x = v = POP();
2526 Py_DECREF(v);
2527 JUMPBY(oparg);
2528 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002529
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002530 TARGET(BREAK_LOOP)
2531 why = WHY_BREAK;
2532 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002533
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002534 TARGET(CONTINUE_LOOP)
2535 retval = PyLong_FromLong(oparg);
2536 if (!retval) {
2537 x = NULL;
2538 break;
2539 }
2540 why = WHY_CONTINUE;
2541 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002542
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002543 TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
2544 TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
2545 TARGET(SETUP_FINALLY)
2546 _setup_finally:
2547 /* NOTE: If you add any new block-setup opcodes that
2548 are not try/except/finally handlers, you may need
2549 to update the PyGen_NeedsFinalizing() function.
2550 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002552 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2553 STACK_LEVEL());
2554 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002555
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002556 TARGET(SETUP_WITH)
2557 {
2558 static PyObject *exit, *enter;
2559 w = TOP();
2560 x = special_lookup(w, "__exit__", &exit);
2561 if (!x)
2562 break;
2563 SET_TOP(x);
2564 u = special_lookup(w, "__enter__", &enter);
2565 Py_DECREF(w);
2566 if (!u) {
2567 x = NULL;
2568 break;
2569 }
2570 x = PyObject_CallFunctionObjArgs(u, NULL);
2571 Py_DECREF(u);
2572 if (!x)
2573 break;
2574 /* Setup the finally block before pushing the result
2575 of __enter__ on the stack. */
2576 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2577 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002579 PUSH(x);
2580 DISPATCH();
2581 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002582
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002583 TARGET(WITH_CLEANUP)
2584 {
2585 /* At the top of the stack are 1-3 values indicating
2586 how/why we entered the finally clause:
2587 - TOP = None
2588 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2589 - TOP = WHY_*; no retval below it
2590 - (TOP, SECOND, THIRD) = exc_info()
2591 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2592 Below them is EXIT, the context.__exit__ bound method.
2593 In the last case, we must call
2594 EXIT(TOP, SECOND, THIRD)
2595 otherwise we must call
2596 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002598 In the first two cases, we remove EXIT from the
2599 stack, leaving the rest in the same order. In the
2600 third case, we shift the bottom 3 values of the
2601 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002603 In addition, if the stack represents an exception,
2604 *and* the function call returns a 'true' value, we
2605 push WHY_SILENCED onto the stack. END_FINALLY will
2606 then not re-raise the exception. (But non-local
2607 gotos should still be resumed.)
2608 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002609
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002610 PyObject *exit_func;
2611 u = TOP();
2612 if (u == Py_None) {
2613 (void)POP();
2614 exit_func = TOP();
2615 SET_TOP(u);
2616 v = w = Py_None;
2617 }
2618 else if (PyLong_Check(u)) {
2619 (void)POP();
2620 switch(PyLong_AsLong(u)) {
2621 case WHY_RETURN:
2622 case WHY_CONTINUE:
2623 /* Retval in TOP. */
2624 exit_func = SECOND();
2625 SET_SECOND(TOP());
2626 SET_TOP(u);
2627 break;
2628 default:
2629 exit_func = TOP();
2630 SET_TOP(u);
2631 break;
2632 }
2633 u = v = w = Py_None;
2634 }
2635 else {
2636 PyObject *tp, *exc, *tb;
2637 PyTryBlock *block;
2638 v = SECOND();
2639 w = THIRD();
2640 tp = FOURTH();
2641 exc = PEEK(5);
2642 tb = PEEK(6);
2643 exit_func = PEEK(7);
2644 SET_VALUE(7, tb);
2645 SET_VALUE(6, exc);
2646 SET_VALUE(5, tp);
2647 /* UNWIND_EXCEPT_HANDLER will pop this off. */
2648 SET_FOURTH(NULL);
2649 /* We just shifted the stack down, so we have
2650 to tell the except handler block that the
2651 values are lower than it expects. */
2652 block = &f->f_blockstack[f->f_iblock - 1];
2653 assert(block->b_type == EXCEPT_HANDLER);
2654 block->b_level--;
2655 }
2656 /* XXX Not the fastest way to call it... */
2657 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2658 NULL);
2659 Py_DECREF(exit_func);
2660 if (x == NULL)
2661 break; /* Go to error exit */
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002662
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002663 if (u != Py_None)
2664 err = PyObject_IsTrue(x);
2665 else
2666 err = 0;
2667 Py_DECREF(x);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002669 if (err < 0)
2670 break; /* Go to error exit */
2671 else if (err > 0) {
2672 err = 0;
2673 /* There was an exception and a True return */
2674 PUSH(PyLong_FromLong((long) WHY_SILENCED));
2675 }
2676 PREDICT(END_FINALLY);
2677 break;
2678 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002679
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002680 TARGET(CALL_FUNCTION)
2681 {
2682 PyObject **sp;
2683 PCALL(PCALL_ALL);
2684 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002685#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002686 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002687#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002688 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002689#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002690 stack_pointer = sp;
2691 PUSH(x);
2692 if (x != NULL)
2693 DISPATCH();
2694 break;
2695 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002696
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002697 TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw)
2698 TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw)
2699 TARGET(CALL_FUNCTION_VAR_KW)
2700 _call_function_var_kw:
2701 {
2702 int na = oparg & 0xff;
2703 int nk = (oparg>>8) & 0xff;
2704 int flags = (opcode - CALL_FUNCTION) & 3;
2705 int n = na + 2 * nk;
2706 PyObject **pfunc, *func, **sp;
2707 PCALL(PCALL_ALL);
2708 if (flags & CALL_FLAG_VAR)
2709 n++;
2710 if (flags & CALL_FLAG_KW)
2711 n++;
2712 pfunc = stack_pointer - n - 1;
2713 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002714
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002715 if (PyMethod_Check(func)
Stefan Krahb7e10102010-06-23 18:42:39 +00002716 && PyMethod_GET_SELF(func) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002717 PyObject *self = PyMethod_GET_SELF(func);
2718 Py_INCREF(self);
2719 func = PyMethod_GET_FUNCTION(func);
2720 Py_INCREF(func);
2721 Py_DECREF(*pfunc);
2722 *pfunc = self;
2723 na++;
2724 n++;
2725 } else
2726 Py_INCREF(func);
2727 sp = stack_pointer;
2728 READ_TIMESTAMP(intr0);
2729 x = ext_do_call(func, &sp, flags, na, nk);
2730 READ_TIMESTAMP(intr1);
2731 stack_pointer = sp;
2732 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002733
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002734 while (stack_pointer > pfunc) {
2735 w = POP();
2736 Py_DECREF(w);
2737 }
2738 PUSH(x);
2739 if (x != NULL)
2740 DISPATCH();
2741 break;
2742 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002743
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002744 TARGET_WITH_IMPL(MAKE_CLOSURE, _make_function)
2745 TARGET(MAKE_FUNCTION)
2746 _make_function:
2747 {
2748 int posdefaults = oparg & 0xff;
2749 int kwdefaults = (oparg>>8) & 0xff;
2750 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002751
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002752 v = POP(); /* code object */
2753 x = PyFunction_New(v, f->f_globals);
2754 Py_DECREF(v);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002755
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002756 if (x != NULL && opcode == MAKE_CLOSURE) {
2757 v = POP();
2758 if (PyFunction_SetClosure(x, v) != 0) {
2759 /* Can't happen unless bytecode is corrupt. */
2760 why = WHY_EXCEPTION;
2761 }
2762 Py_DECREF(v);
2763 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002764
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002765 if (x != NULL && num_annotations > 0) {
2766 Py_ssize_t name_ix;
2767 u = POP(); /* names of args with annotations */
2768 v = PyDict_New();
2769 if (v == NULL) {
2770 Py_DECREF(x);
2771 x = NULL;
2772 break;
2773 }
2774 name_ix = PyTuple_Size(u);
2775 assert(num_annotations == name_ix+1);
2776 while (name_ix > 0) {
2777 --name_ix;
2778 t = PyTuple_GET_ITEM(u, name_ix);
2779 w = POP();
2780 /* XXX(nnorwitz): check for errors */
2781 PyDict_SetItem(v, t, w);
2782 Py_DECREF(w);
2783 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002784
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002785 if (PyFunction_SetAnnotations(x, v) != 0) {
2786 /* Can't happen unless
2787 PyFunction_SetAnnotations changes. */
2788 why = WHY_EXCEPTION;
2789 }
2790 Py_DECREF(v);
2791 Py_DECREF(u);
2792 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002793
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002794 /* XXX Maybe this should be a separate opcode? */
2795 if (x != NULL && posdefaults > 0) {
2796 v = PyTuple_New(posdefaults);
2797 if (v == NULL) {
2798 Py_DECREF(x);
2799 x = NULL;
2800 break;
2801 }
2802 while (--posdefaults >= 0) {
2803 w = POP();
2804 PyTuple_SET_ITEM(v, posdefaults, w);
2805 }
2806 if (PyFunction_SetDefaults(x, v) != 0) {
2807 /* Can't happen unless
2808 PyFunction_SetDefaults changes. */
2809 why = WHY_EXCEPTION;
2810 }
2811 Py_DECREF(v);
2812 }
2813 if (x != NULL && kwdefaults > 0) {
2814 v = PyDict_New();
2815 if (v == NULL) {
2816 Py_DECREF(x);
2817 x = NULL;
2818 break;
2819 }
2820 while (--kwdefaults >= 0) {
2821 w = POP(); /* default value */
2822 u = POP(); /* kw only arg name */
2823 /* XXX(nnorwitz): check for errors */
2824 PyDict_SetItem(v, u, w);
2825 Py_DECREF(w);
2826 Py_DECREF(u);
2827 }
2828 if (PyFunction_SetKwDefaults(x, v) != 0) {
2829 /* Can't happen unless
2830 PyFunction_SetKwDefaults changes. */
2831 why = WHY_EXCEPTION;
2832 }
2833 Py_DECREF(v);
2834 }
2835 PUSH(x);
2836 break;
2837 }
Guido van Rossum8861b741996-07-30 16:49:37 +00002838
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002839 TARGET(BUILD_SLICE)
2840 if (oparg == 3)
2841 w = POP();
2842 else
2843 w = NULL;
2844 v = POP();
2845 u = TOP();
2846 x = PySlice_New(u, v, w);
2847 Py_DECREF(u);
2848 Py_DECREF(v);
2849 Py_XDECREF(w);
2850 SET_TOP(x);
2851 if (x != NULL) DISPATCH();
2852 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002853
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002854 TARGET(EXTENDED_ARG)
2855 opcode = NEXTOP();
2856 oparg = oparg<<16 | NEXTARG();
2857 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002858
Antoine Pitrou042b1282010-08-13 21:15:58 +00002859#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002860 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00002861#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002862 default:
2863 fprintf(stderr,
2864 "XXX lineno: %d, opcode: %d\n",
2865 PyFrame_GetLineNumber(f),
2866 opcode);
2867 PyErr_SetString(PyExc_SystemError, "unknown opcode");
2868 why = WHY_EXCEPTION;
2869 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002870
2871#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002872 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002873#endif
2874
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002875 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00002876
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002877 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002878
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002879 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002880
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002881 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002882
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002883 if (why == WHY_NOT) {
2884 if (err == 0 && x != NULL) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002885#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002886 /* This check is expensive! */
2887 if (PyErr_Occurred())
2888 fprintf(stderr,
2889 "XXX undetected error\n");
2890 else {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002891#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002892 READ_TIMESTAMP(loop1);
2893 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002894#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002895 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002896#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002897 }
2898 why = WHY_EXCEPTION;
2899 x = Py_None;
2900 err = 0;
2901 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002903 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002904
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002905 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
2906 if (!PyErr_Occurred()) {
2907 PyErr_SetString(PyExc_SystemError,
2908 "error return without exception set");
2909 why = WHY_EXCEPTION;
2910 }
2911 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002912#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002913 else {
2914 /* This check is expensive! */
2915 if (PyErr_Occurred()) {
2916 char buf[128];
2917 sprintf(buf, "Stack unwind with exception "
2918 "set and why=%d", why);
2919 Py_FatalError(buf);
2920 }
2921 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002922#endif
2923
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002924 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002925
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002926 if (why == WHY_EXCEPTION) {
2927 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002928
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002929 if (tstate->c_tracefunc != NULL)
2930 call_exc_trace(tstate->c_tracefunc,
2931 tstate->c_traceobj, f);
2932 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002933
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002934 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002935
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002936 if (why == WHY_RERAISE)
2937 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +00002938
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002939 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002940
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002941fast_block_end:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002942 while (why != WHY_NOT && f->f_iblock > 0) {
2943 /* Peek at the current block. */
2944 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002945
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002946 assert(why != WHY_YIELD);
2947 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2948 why = WHY_NOT;
2949 JUMPTO(PyLong_AS_LONG(retval));
2950 Py_DECREF(retval);
2951 break;
2952 }
2953 /* Now we have to pop the block. */
2954 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002955
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002956 if (b->b_type == EXCEPT_HANDLER) {
2957 UNWIND_EXCEPT_HANDLER(b);
2958 continue;
2959 }
2960 UNWIND_BLOCK(b);
2961 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2962 why = WHY_NOT;
2963 JUMPTO(b->b_handler);
2964 break;
2965 }
2966 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
2967 || b->b_type == SETUP_FINALLY)) {
2968 PyObject *exc, *val, *tb;
2969 int handler = b->b_handler;
2970 /* Beware, this invalidates all b->b_* fields */
2971 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
2972 PUSH(tstate->exc_traceback);
2973 PUSH(tstate->exc_value);
2974 if (tstate->exc_type != NULL) {
2975 PUSH(tstate->exc_type);
2976 }
2977 else {
2978 Py_INCREF(Py_None);
2979 PUSH(Py_None);
2980 }
2981 PyErr_Fetch(&exc, &val, &tb);
2982 /* Make the raw exception data
2983 available to the handler,
2984 so a program can emulate the
2985 Python main loop. */
2986 PyErr_NormalizeException(
2987 &exc, &val, &tb);
2988 PyException_SetTraceback(val, tb);
2989 Py_INCREF(exc);
2990 tstate->exc_type = exc;
2991 Py_INCREF(val);
2992 tstate->exc_value = val;
2993 tstate->exc_traceback = tb;
2994 if (tb == NULL)
2995 tb = Py_None;
2996 Py_INCREF(tb);
2997 PUSH(tb);
2998 PUSH(val);
2999 PUSH(exc);
3000 why = WHY_NOT;
3001 JUMPTO(handler);
3002 break;
3003 }
3004 if (b->b_type == SETUP_FINALLY) {
3005 if (why & (WHY_RETURN | WHY_CONTINUE))
3006 PUSH(retval);
3007 PUSH(PyLong_FromLong((long)why));
3008 why = WHY_NOT;
3009 JUMPTO(b->b_handler);
3010 break;
3011 }
3012 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003013
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003014 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00003015
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003016 if (why != WHY_NOT)
3017 break;
3018 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00003019
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003020 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003021
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003022 assert(why != WHY_YIELD);
3023 /* Pop remaining stack entries. */
3024 while (!EMPTY()) {
3025 v = POP();
3026 Py_XDECREF(v);
3027 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003028
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003029 if (why != WHY_RETURN)
3030 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003031
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003032fast_yield:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003033 if (tstate->use_tracing) {
3034 if (tstate->c_tracefunc) {
3035 if (why == WHY_RETURN || why == WHY_YIELD) {
3036 if (call_trace(tstate->c_tracefunc,
3037 tstate->c_traceobj, f,
3038 PyTrace_RETURN, retval)) {
3039 Py_XDECREF(retval);
3040 retval = NULL;
3041 why = WHY_EXCEPTION;
3042 }
3043 }
3044 else if (why == WHY_EXCEPTION) {
3045 call_trace_protected(tstate->c_tracefunc,
3046 tstate->c_traceobj, f,
3047 PyTrace_RETURN, NULL);
3048 }
3049 }
3050 if (tstate->c_profilefunc) {
3051 if (why == WHY_EXCEPTION)
3052 call_trace_protected(tstate->c_profilefunc,
3053 tstate->c_profileobj, f,
3054 PyTrace_RETURN, NULL);
3055 else if (call_trace(tstate->c_profilefunc,
3056 tstate->c_profileobj, f,
3057 PyTrace_RETURN, retval)) {
3058 Py_XDECREF(retval);
3059 retval = NULL;
3060 why = WHY_EXCEPTION;
3061 }
3062 }
3063 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003065 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003066exit_eval_frame:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003067 Py_LeaveRecursiveCall();
3068 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003069
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003070 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00003071}
3072
Guido van Rossumc2e20742006-02-27 22:32:47 +00003073/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003074 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003075 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003076
Tim Peters6d6c1a32001-08-02 04:15:00 +00003077PyObject *
3078PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003079 PyObject **args, int argcount, PyObject **kws, int kwcount,
3080 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00003081{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003082 register PyFrameObject *f;
3083 register PyObject *retval = NULL;
3084 register PyObject **fastlocals, **freevars;
3085 PyThreadState *tstate = PyThreadState_GET();
3086 PyObject *x, *u;
3087 int total_args = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00003088
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003089 if (globals == NULL) {
3090 PyErr_SetString(PyExc_SystemError,
3091 "PyEval_EvalCodeEx: NULL globals");
3092 return NULL;
3093 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003094
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003095 assert(tstate != NULL);
3096 assert(globals != NULL);
3097 f = PyFrame_New(tstate, co, globals, locals);
3098 if (f == NULL)
3099 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003100
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003101 fastlocals = f->f_localsplus;
3102 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003103
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003104 if (total_args || co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
3105 int i;
3106 int n = argcount;
3107 PyObject *kwdict = NULL;
3108 if (co->co_flags & CO_VARKEYWORDS) {
3109 kwdict = PyDict_New();
3110 if (kwdict == NULL)
3111 goto fail;
3112 i = total_args;
3113 if (co->co_flags & CO_VARARGS)
3114 i++;
3115 SETLOCAL(i, kwdict);
3116 }
3117 if (argcount > co->co_argcount) {
3118 if (!(co->co_flags & CO_VARARGS)) {
3119 PyErr_Format(PyExc_TypeError,
3120 "%U() takes %s %d "
Benjamin Peterson88968ad2010-06-25 19:30:21 +00003121 "positional argument%s (%d given)",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003122 co->co_name,
3123 defcount ? "at most" : "exactly",
Benjamin Peterson88968ad2010-06-25 19:30:21 +00003124 co->co_argcount,
3125 co->co_argcount == 1 ? "" : "s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003126 argcount + kwcount);
3127 goto fail;
3128 }
3129 n = co->co_argcount;
3130 }
3131 for (i = 0; i < n; i++) {
3132 x = args[i];
3133 Py_INCREF(x);
3134 SETLOCAL(i, x);
3135 }
3136 if (co->co_flags & CO_VARARGS) {
3137 u = PyTuple_New(argcount - n);
3138 if (u == NULL)
3139 goto fail;
3140 SETLOCAL(total_args, u);
3141 for (i = n; i < argcount; i++) {
3142 x = args[i];
3143 Py_INCREF(x);
3144 PyTuple_SET_ITEM(u, i-n, x);
3145 }
3146 }
3147 for (i = 0; i < kwcount; i++) {
3148 PyObject **co_varnames;
3149 PyObject *keyword = kws[2*i];
3150 PyObject *value = kws[2*i + 1];
3151 int j;
3152 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3153 PyErr_Format(PyExc_TypeError,
3154 "%U() keywords must be strings",
3155 co->co_name);
3156 goto fail;
3157 }
3158 /* Speed hack: do raw pointer compares. As names are
3159 normally interned this should almost always hit. */
3160 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3161 for (j = 0; j < total_args; j++) {
3162 PyObject *nm = co_varnames[j];
3163 if (nm == keyword)
3164 goto kw_found;
3165 }
3166 /* Slow fallback, just in case */
3167 for (j = 0; j < total_args; j++) {
3168 PyObject *nm = co_varnames[j];
3169 int cmp = PyObject_RichCompareBool(
3170 keyword, nm, Py_EQ);
3171 if (cmp > 0)
3172 goto kw_found;
3173 else if (cmp < 0)
3174 goto fail;
3175 }
3176 if (j >= total_args && kwdict == NULL) {
3177 PyErr_Format(PyExc_TypeError,
3178 "%U() got an unexpected "
3179 "keyword argument '%S'",
3180 co->co_name,
3181 keyword);
3182 goto fail;
3183 }
3184 PyDict_SetItem(kwdict, keyword, value);
3185 continue;
3186 kw_found:
3187 if (GETLOCAL(j) != NULL) {
3188 PyErr_Format(PyExc_TypeError,
3189 "%U() got multiple "
3190 "values for keyword "
3191 "argument '%S'",
3192 co->co_name,
3193 keyword);
3194 goto fail;
3195 }
3196 Py_INCREF(value);
3197 SETLOCAL(j, value);
3198 }
3199 if (co->co_kwonlyargcount > 0) {
3200 for (i = co->co_argcount; i < total_args; i++) {
3201 PyObject *name;
3202 if (GETLOCAL(i) != NULL)
3203 continue;
3204 name = PyTuple_GET_ITEM(co->co_varnames, i);
3205 if (kwdefs != NULL) {
3206 PyObject *def = PyDict_GetItem(kwdefs, name);
3207 if (def) {
3208 Py_INCREF(def);
3209 SETLOCAL(i, def);
3210 continue;
3211 }
3212 }
3213 PyErr_Format(PyExc_TypeError,
3214 "%U() needs keyword-only argument %S",
3215 co->co_name, name);
3216 goto fail;
3217 }
3218 }
3219 if (argcount < co->co_argcount) {
3220 int m = co->co_argcount - defcount;
3221 for (i = argcount; i < m; i++) {
3222 if (GETLOCAL(i) == NULL) {
3223 int j, given = 0;
3224 for (j = 0; j < co->co_argcount; j++)
3225 if (GETLOCAL(j))
3226 given++;
3227 PyErr_Format(PyExc_TypeError,
3228 "%U() takes %s %d "
3229 "argument%s "
3230 "(%d given)",
3231 co->co_name,
3232 ((co->co_flags & CO_VARARGS) ||
3233 defcount) ? "at least"
3234 : "exactly",
3235 m, m == 1 ? "" : "s", given);
3236 goto fail;
3237 }
3238 }
3239 if (n > m)
3240 i = n - m;
3241 else
3242 i = 0;
3243 for (; i < defcount; i++) {
3244 if (GETLOCAL(m+i) == NULL) {
3245 PyObject *def = defs[i];
3246 Py_INCREF(def);
3247 SETLOCAL(m+i, def);
3248 }
3249 }
3250 }
3251 }
3252 else if (argcount > 0 || kwcount > 0) {
3253 PyErr_Format(PyExc_TypeError,
3254 "%U() takes no arguments (%d given)",
3255 co->co_name,
3256 argcount + kwcount);
3257 goto fail;
3258 }
3259 /* Allocate and initialize storage for cell vars, and copy free
3260 vars into frame. This isn't too efficient right now. */
3261 if (PyTuple_GET_SIZE(co->co_cellvars)) {
3262 int i, j, nargs, found;
3263 Py_UNICODE *cellname, *argname;
3264 PyObject *c;
Tim Peters5ca576e2001-06-18 22:08:13 +00003265
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003266 nargs = total_args;
3267 if (co->co_flags & CO_VARARGS)
3268 nargs++;
3269 if (co->co_flags & CO_VARKEYWORDS)
3270 nargs++;
Tim Peters5ca576e2001-06-18 22:08:13 +00003271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003272 /* Initialize each cell var, taking into account
3273 cell vars that are initialized from arguments.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003275 Should arrange for the compiler to put cellvars
3276 that are arguments at the beginning of the cellvars
3277 list so that we can march over it more efficiently?
3278 */
3279 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
3280 cellname = PyUnicode_AS_UNICODE(
3281 PyTuple_GET_ITEM(co->co_cellvars, i));
3282 found = 0;
3283 for (j = 0; j < nargs; j++) {
3284 argname = PyUnicode_AS_UNICODE(
3285 PyTuple_GET_ITEM(co->co_varnames, j));
3286 if (Py_UNICODE_strcmp(cellname, argname) == 0) {
3287 c = PyCell_New(GETLOCAL(j));
3288 if (c == NULL)
3289 goto fail;
3290 GETLOCAL(co->co_nlocals + i) = c;
3291 found = 1;
3292 break;
3293 }
3294 }
3295 if (found == 0) {
3296 c = PyCell_New(NULL);
3297 if (c == NULL)
3298 goto fail;
3299 SETLOCAL(co->co_nlocals + i, c);
3300 }
3301 }
3302 }
3303 if (PyTuple_GET_SIZE(co->co_freevars)) {
3304 int i;
3305 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3306 PyObject *o = PyTuple_GET_ITEM(closure, i);
3307 Py_INCREF(o);
3308 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
3309 }
3310 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003311
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003312 if (co->co_flags & CO_GENERATOR) {
3313 /* Don't need to keep the reference to f_back, it will be set
3314 * when the generator is resumed. */
3315 Py_XDECREF(f->f_back);
3316 f->f_back = NULL;
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003318 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003320 /* Create a new generator that owns the ready to run frame
3321 * and return that as the value. */
3322 return PyGen_New(f);
3323 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003325 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003326
Thomas Woutersce272b62007-09-19 21:19:28 +00003327fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003329 /* decref'ing the frame can cause __del__ methods to get invoked,
3330 which can call back into Python. While we're done with the
3331 current Python frame (f), the associated C stack is still in use,
3332 so recursion_depth must be boosted for the duration.
3333 */
3334 assert(tstate != NULL);
3335 ++tstate->recursion_depth;
3336 Py_DECREF(f);
3337 --tstate->recursion_depth;
3338 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003339}
3340
3341
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003342static PyObject *
3343special_lookup(PyObject *o, char *meth, PyObject **cache)
3344{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003345 PyObject *res;
3346 res = _PyObject_LookupSpecial(o, meth, cache);
3347 if (res == NULL && !PyErr_Occurred()) {
3348 PyErr_SetObject(PyExc_AttributeError, *cache);
3349 return NULL;
3350 }
3351 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003352}
3353
3354
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003355/* Logic for the raise statement (too complicated for inlining).
3356 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00003357static enum why_code
Collin Winter828f04a2007-08-31 00:04:24 +00003358do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003359{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003360 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003361
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003362 if (exc == NULL) {
3363 /* Reraise */
3364 PyThreadState *tstate = PyThreadState_GET();
3365 PyObject *tb;
3366 type = tstate->exc_type;
3367 value = tstate->exc_value;
3368 tb = tstate->exc_traceback;
3369 if (type == Py_None) {
3370 PyErr_SetString(PyExc_RuntimeError,
3371 "No active exception to reraise");
3372 return WHY_EXCEPTION;
3373 }
3374 Py_XINCREF(type);
3375 Py_XINCREF(value);
3376 Py_XINCREF(tb);
3377 PyErr_Restore(type, value, tb);
3378 return WHY_RERAISE;
3379 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003381 /* We support the following forms of raise:
3382 raise
Collin Winter828f04a2007-08-31 00:04:24 +00003383 raise <instance>
3384 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003386 if (PyExceptionClass_Check(exc)) {
3387 type = exc;
3388 value = PyObject_CallObject(exc, NULL);
3389 if (value == NULL)
3390 goto raise_error;
3391 }
3392 else if (PyExceptionInstance_Check(exc)) {
3393 value = exc;
3394 type = PyExceptionInstance_Class(exc);
3395 Py_INCREF(type);
3396 }
3397 else {
3398 /* Not something you can raise. You get an exception
3399 anyway, just not what you specified :-) */
3400 Py_DECREF(exc);
3401 PyErr_SetString(PyExc_TypeError,
3402 "exceptions must derive from BaseException");
3403 goto raise_error;
3404 }
Collin Winter828f04a2007-08-31 00:04:24 +00003405
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003406 if (cause) {
3407 PyObject *fixed_cause;
3408 if (PyExceptionClass_Check(cause)) {
3409 fixed_cause = PyObject_CallObject(cause, NULL);
3410 if (fixed_cause == NULL)
3411 goto raise_error;
3412 Py_DECREF(cause);
3413 }
3414 else if (PyExceptionInstance_Check(cause)) {
3415 fixed_cause = cause;
3416 }
3417 else {
3418 PyErr_SetString(PyExc_TypeError,
3419 "exception causes must derive from "
3420 "BaseException");
3421 goto raise_error;
3422 }
3423 PyException_SetCause(value, fixed_cause);
3424 }
Collin Winter828f04a2007-08-31 00:04:24 +00003425
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003426 PyErr_SetObject(type, value);
3427 /* PyErr_SetObject incref's its arguments */
3428 Py_XDECREF(value);
3429 Py_XDECREF(type);
3430 return WHY_EXCEPTION;
Collin Winter828f04a2007-08-31 00:04:24 +00003431
3432raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003433 Py_XDECREF(value);
3434 Py_XDECREF(type);
3435 Py_XDECREF(cause);
3436 return WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003437}
3438
Tim Petersd6d010b2001-06-21 02:49:55 +00003439/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00003440 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003441
Guido van Rossum0368b722007-05-11 16:50:42 +00003442 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
3443 with a variable target.
3444*/
Tim Petersd6d010b2001-06-21 02:49:55 +00003445
Barry Warsawe42b18f1997-08-25 22:13:04 +00003446static int
Guido van Rossum0368b722007-05-11 16:50:42 +00003447unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003448{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003449 int i = 0, j = 0;
3450 Py_ssize_t ll = 0;
3451 PyObject *it; /* iter(v) */
3452 PyObject *w;
3453 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00003454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003455 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00003456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003457 it = PyObject_GetIter(v);
3458 if (it == NULL)
3459 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00003460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003461 for (; i < argcnt; i++) {
3462 w = PyIter_Next(it);
3463 if (w == NULL) {
3464 /* Iterator done, via error or exhaustion. */
3465 if (!PyErr_Occurred()) {
3466 PyErr_Format(PyExc_ValueError,
3467 "need more than %d value%s to unpack",
3468 i, i == 1 ? "" : "s");
3469 }
3470 goto Error;
3471 }
3472 *--sp = w;
3473 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003475 if (argcntafter == -1) {
3476 /* We better have exhausted the iterator now. */
3477 w = PyIter_Next(it);
3478 if (w == NULL) {
3479 if (PyErr_Occurred())
3480 goto Error;
3481 Py_DECREF(it);
3482 return 1;
3483 }
3484 Py_DECREF(w);
Georg Brandl0310a832010-07-10 10:32:36 +00003485 PyErr_Format(PyExc_ValueError, "too many values to unpack "
3486 "(expected %d)", argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003487 goto Error;
3488 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003489
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003490 l = PySequence_List(it);
3491 if (l == NULL)
3492 goto Error;
3493 *--sp = l;
3494 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00003495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003496 ll = PyList_GET_SIZE(l);
3497 if (ll < argcntafter) {
3498 PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack",
3499 argcnt + ll);
3500 goto Error;
3501 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003503 /* Pop the "after-variable" args off the list. */
3504 for (j = argcntafter; j > 0; j--, i++) {
3505 *--sp = PyList_GET_ITEM(l, ll - j);
3506 }
3507 /* Resize the list. */
3508 Py_SIZE(l) = ll - argcntafter;
3509 Py_DECREF(it);
3510 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00003511
Tim Petersd6d010b2001-06-21 02:49:55 +00003512Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003513 for (; i > 0; i--, sp++)
3514 Py_DECREF(*sp);
3515 Py_XDECREF(it);
3516 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003517}
3518
3519
Guido van Rossum96a42c81992-01-12 02:29:51 +00003520#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003521static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003522prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003523{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003524 printf("%s ", str);
3525 if (PyObject_Print(v, stdout, 0) != 0)
3526 PyErr_Clear(); /* Don't know what else to do */
3527 printf("\n");
3528 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003529}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003530#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003531
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003532static void
Fred Drake5755ce62001-06-27 19:19:46 +00003533call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003534{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003535 PyObject *type, *value, *traceback, *arg;
3536 int err;
3537 PyErr_Fetch(&type, &value, &traceback);
3538 if (value == NULL) {
3539 value = Py_None;
3540 Py_INCREF(value);
3541 }
3542 arg = PyTuple_Pack(3, type, value, traceback);
3543 if (arg == NULL) {
3544 PyErr_Restore(type, value, traceback);
3545 return;
3546 }
3547 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
3548 Py_DECREF(arg);
3549 if (err == 0)
3550 PyErr_Restore(type, value, traceback);
3551 else {
3552 Py_XDECREF(type);
3553 Py_XDECREF(value);
3554 Py_XDECREF(traceback);
3555 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003556}
3557
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003558static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003559call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003560 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003561{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003562 PyObject *type, *value, *traceback;
3563 int err;
3564 PyErr_Fetch(&type, &value, &traceback);
3565 err = call_trace(func, obj, frame, what, arg);
3566 if (err == 0)
3567 {
3568 PyErr_Restore(type, value, traceback);
3569 return 0;
3570 }
3571 else {
3572 Py_XDECREF(type);
3573 Py_XDECREF(value);
3574 Py_XDECREF(traceback);
3575 return -1;
3576 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003577}
3578
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003579static int
Fred Drake5755ce62001-06-27 19:19:46 +00003580call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003581 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003582{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003583 register PyThreadState *tstate = frame->f_tstate;
3584 int result;
3585 if (tstate->tracing)
3586 return 0;
3587 tstate->tracing++;
3588 tstate->use_tracing = 0;
3589 result = func(obj, frame, what, arg);
3590 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3591 || (tstate->c_profilefunc != NULL));
3592 tstate->tracing--;
3593 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003594}
3595
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003596PyObject *
3597_PyEval_CallTracing(PyObject *func, PyObject *args)
3598{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003599 PyFrameObject *frame = PyEval_GetFrame();
3600 PyThreadState *tstate = frame->f_tstate;
3601 int save_tracing = tstate->tracing;
3602 int save_use_tracing = tstate->use_tracing;
3603 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003605 tstate->tracing = 0;
3606 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3607 || (tstate->c_profilefunc != NULL));
3608 result = PyObject_Call(func, args, NULL);
3609 tstate->tracing = save_tracing;
3610 tstate->use_tracing = save_use_tracing;
3611 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003612}
3613
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003614/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00003615static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003616maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003617 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3618 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003619{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003620 int result = 0;
3621 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003622
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003623 /* If the last instruction executed isn't in the current
3624 instruction window, reset the window.
3625 */
3626 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
3627 PyAddrPair bounds;
3628 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3629 &bounds);
3630 *instr_lb = bounds.ap_lower;
3631 *instr_ub = bounds.ap_upper;
3632 }
3633 /* If the last instruction falls at the start of a line or if
3634 it represents a jump backwards, update the frame's line
3635 number and call the trace function. */
3636 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
3637 frame->f_lineno = line;
3638 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
3639 }
3640 *instr_prev = frame->f_lasti;
3641 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003642}
3643
Fred Drake5755ce62001-06-27 19:19:46 +00003644void
3645PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003646{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003647 PyThreadState *tstate = PyThreadState_GET();
3648 PyObject *temp = tstate->c_profileobj;
3649 Py_XINCREF(arg);
3650 tstate->c_profilefunc = NULL;
3651 tstate->c_profileobj = NULL;
3652 /* Must make sure that tracing is not ignored if 'temp' is freed */
3653 tstate->use_tracing = tstate->c_tracefunc != NULL;
3654 Py_XDECREF(temp);
3655 tstate->c_profilefunc = func;
3656 tstate->c_profileobj = arg;
3657 /* Flag that tracing or profiling is turned on */
3658 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003659}
3660
3661void
3662PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3663{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003664 PyThreadState *tstate = PyThreadState_GET();
3665 PyObject *temp = tstate->c_traceobj;
3666 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
3667 Py_XINCREF(arg);
3668 tstate->c_tracefunc = NULL;
3669 tstate->c_traceobj = NULL;
3670 /* Must make sure that profiling is not ignored if 'temp' is freed */
3671 tstate->use_tracing = tstate->c_profilefunc != NULL;
3672 Py_XDECREF(temp);
3673 tstate->c_tracefunc = func;
3674 tstate->c_traceobj = arg;
3675 /* Flag that tracing or profiling is turned on */
3676 tstate->use_tracing = ((func != NULL)
3677 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003678}
3679
Guido van Rossumb209a111997-04-29 18:18:01 +00003680PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003681PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003682{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003683 PyFrameObject *current_frame = PyEval_GetFrame();
3684 if (current_frame == NULL)
3685 return PyThreadState_GET()->interp->builtins;
3686 else
3687 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003688}
3689
Guido van Rossumb209a111997-04-29 18:18:01 +00003690PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003691PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003692{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003693 PyFrameObject *current_frame = PyEval_GetFrame();
3694 if (current_frame == NULL)
3695 return NULL;
3696 PyFrame_FastToLocals(current_frame);
3697 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00003698}
3699
Guido van Rossumb209a111997-04-29 18:18:01 +00003700PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003701PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003702{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003703 PyFrameObject *current_frame = PyEval_GetFrame();
3704 if (current_frame == NULL)
3705 return NULL;
3706 else
3707 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003708}
3709
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003710PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003711PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003712{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003713 PyThreadState *tstate = PyThreadState_GET();
3714 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003715}
3716
Guido van Rossum6135a871995-01-09 17:53:26 +00003717int
Tim Peters5ba58662001-07-16 02:29:45 +00003718PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003719{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003720 PyFrameObject *current_frame = PyEval_GetFrame();
3721 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003722
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003723 if (current_frame != NULL) {
3724 const int codeflags = current_frame->f_code->co_flags;
3725 const int compilerflags = codeflags & PyCF_MASK;
3726 if (compilerflags) {
3727 result = 1;
3728 cf->cf_flags |= compilerflags;
3729 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003730#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003731 if (codeflags & CO_GENERATOR_ALLOWED) {
3732 result = 1;
3733 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3734 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003735#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003736 }
3737 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003738}
3739
Guido van Rossum3f5da241990-12-20 15:06:42 +00003740
Guido van Rossum681d79a1995-07-18 14:51:37 +00003741/* External interface to call any callable object.
Antoine Pitrou8689a102010-04-01 16:53:15 +00003742 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00003743
Guido van Rossumb209a111997-04-29 18:18:01 +00003744PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003745PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003746{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003747 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003748
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003749 if (arg == NULL) {
3750 arg = PyTuple_New(0);
3751 if (arg == NULL)
3752 return NULL;
3753 }
3754 else if (!PyTuple_Check(arg)) {
3755 PyErr_SetString(PyExc_TypeError,
3756 "argument list must be a tuple");
3757 return NULL;
3758 }
3759 else
3760 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003762 if (kw != NULL && !PyDict_Check(kw)) {
3763 PyErr_SetString(PyExc_TypeError,
3764 "keyword list must be a dictionary");
3765 Py_DECREF(arg);
3766 return NULL;
3767 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00003768
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003769 result = PyObject_Call(func, arg, kw);
3770 Py_DECREF(arg);
3771 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00003772}
3773
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003774const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003775PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003776{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003777 if (PyMethod_Check(func))
3778 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
3779 else if (PyFunction_Check(func))
3780 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
3781 else if (PyCFunction_Check(func))
3782 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3783 else
3784 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00003785}
3786
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003787const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003788PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003789{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003790 if (PyMethod_Check(func))
3791 return "()";
3792 else if (PyFunction_Check(func))
3793 return "()";
3794 else if (PyCFunction_Check(func))
3795 return "()";
3796 else
3797 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00003798}
3799
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003800static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003801err_args(PyObject *func, int flags, int nargs)
3802{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003803 if (flags & METH_NOARGS)
3804 PyErr_Format(PyExc_TypeError,
3805 "%.200s() takes no arguments (%d given)",
3806 ((PyCFunctionObject *)func)->m_ml->ml_name,
3807 nargs);
3808 else
3809 PyErr_Format(PyExc_TypeError,
3810 "%.200s() takes exactly one argument (%d given)",
3811 ((PyCFunctionObject *)func)->m_ml->ml_name,
3812 nargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003813}
3814
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003815#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003816if (tstate->use_tracing && tstate->c_profilefunc) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003817 if (call_trace(tstate->c_profilefunc, \
3818 tstate->c_profileobj, \
3819 tstate->frame, PyTrace_C_CALL, \
3820 func)) { \
3821 x = NULL; \
3822 } \
3823 else { \
3824 x = call; \
3825 if (tstate->c_profilefunc != NULL) { \
3826 if (x == NULL) { \
3827 call_trace_protected(tstate->c_profilefunc, \
3828 tstate->c_profileobj, \
3829 tstate->frame, PyTrace_C_EXCEPTION, \
3830 func); \
3831 /* XXX should pass (type, value, tb) */ \
3832 } else { \
3833 if (call_trace(tstate->c_profilefunc, \
3834 tstate->c_profileobj, \
3835 tstate->frame, PyTrace_C_RETURN, \
3836 func)) { \
3837 Py_DECREF(x); \
3838 x = NULL; \
3839 } \
3840 } \
3841 } \
3842 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003843} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003844 x = call; \
3845 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003846
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003847static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003848call_function(PyObject ***pp_stack, int oparg
3849#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003850 , uint64* pintr0, uint64* pintr1
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003851#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003852 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003853{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003854 int na = oparg & 0xff;
3855 int nk = (oparg>>8) & 0xff;
3856 int n = na + 2 * nk;
3857 PyObject **pfunc = (*pp_stack) - n - 1;
3858 PyObject *func = *pfunc;
3859 PyObject *x, *w;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003860
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003861 /* Always dispatch PyCFunction first, because these are
3862 presumed to be the most frequent callable object.
3863 */
3864 if (PyCFunction_Check(func) && nk == 0) {
3865 int flags = PyCFunction_GET_FLAGS(func);
3866 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003867
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003868 PCALL(PCALL_CFUNCTION);
3869 if (flags & (METH_NOARGS | METH_O)) {
3870 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3871 PyObject *self = PyCFunction_GET_SELF(func);
3872 if (flags & METH_NOARGS && na == 0) {
3873 C_TRACE(x, (*meth)(self,NULL));
3874 }
3875 else if (flags & METH_O && na == 1) {
3876 PyObject *arg = EXT_POP(*pp_stack);
3877 C_TRACE(x, (*meth)(self,arg));
3878 Py_DECREF(arg);
3879 }
3880 else {
3881 err_args(func, flags, na);
3882 x = NULL;
3883 }
3884 }
3885 else {
3886 PyObject *callargs;
3887 callargs = load_args(pp_stack, na);
3888 READ_TIMESTAMP(*pintr0);
3889 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
3890 READ_TIMESTAMP(*pintr1);
3891 Py_XDECREF(callargs);
3892 }
3893 } else {
3894 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3895 /* optimize access to bound methods */
3896 PyObject *self = PyMethod_GET_SELF(func);
3897 PCALL(PCALL_METHOD);
3898 PCALL(PCALL_BOUND_METHOD);
3899 Py_INCREF(self);
3900 func = PyMethod_GET_FUNCTION(func);
3901 Py_INCREF(func);
3902 Py_DECREF(*pfunc);
3903 *pfunc = self;
3904 na++;
3905 n++;
3906 } else
3907 Py_INCREF(func);
3908 READ_TIMESTAMP(*pintr0);
3909 if (PyFunction_Check(func))
3910 x = fast_function(func, pp_stack, n, na, nk);
3911 else
3912 x = do_call(func, pp_stack, na, nk);
3913 READ_TIMESTAMP(*pintr1);
3914 Py_DECREF(func);
3915 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003916
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003917 /* Clear the stack of the function object. Also removes
3918 the arguments in case they weren't consumed already
3919 (fast_function() and err_args() leave them on the stack).
3920 */
3921 while ((*pp_stack) > pfunc) {
3922 w = EXT_POP(*pp_stack);
3923 Py_DECREF(w);
3924 PCALL(PCALL_POP);
3925 }
3926 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003927}
3928
Jeremy Hylton192690e2002-08-16 18:36:11 +00003929/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003930 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003931 For the simplest case -- a function that takes only positional
3932 arguments and is called with only positional arguments -- it
3933 inlines the most primitive frame setup code from
3934 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3935 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003936*/
3937
3938static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003939fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003940{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003941 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
3942 PyObject *globals = PyFunction_GET_GLOBALS(func);
3943 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3944 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
3945 PyObject **d = NULL;
3946 int nd = 0;
Jeremy Hylton52820442001-01-03 23:52:36 +00003947
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003948 PCALL(PCALL_FUNCTION);
3949 PCALL(PCALL_FAST_FUNCTION);
3950 if (argdefs == NULL && co->co_argcount == n &&
3951 co->co_kwonlyargcount == 0 && nk==0 &&
3952 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3953 PyFrameObject *f;
3954 PyObject *retval = NULL;
3955 PyThreadState *tstate = PyThreadState_GET();
3956 PyObject **fastlocals, **stack;
3957 int i;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003958
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003959 PCALL(PCALL_FASTER_FUNCTION);
3960 assert(globals != NULL);
3961 /* XXX Perhaps we should create a specialized
3962 PyFrame_New() that doesn't take locals, but does
3963 take builtins without sanity checking them.
3964 */
3965 assert(tstate != NULL);
3966 f = PyFrame_New(tstate, co, globals, NULL);
3967 if (f == NULL)
3968 return NULL;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003969
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003970 fastlocals = f->f_localsplus;
3971 stack = (*pp_stack) - n;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003973 for (i = 0; i < n; i++) {
3974 Py_INCREF(*stack);
3975 fastlocals[i] = *stack++;
3976 }
3977 retval = PyEval_EvalFrameEx(f,0);
3978 ++tstate->recursion_depth;
3979 Py_DECREF(f);
3980 --tstate->recursion_depth;
3981 return retval;
3982 }
3983 if (argdefs != NULL) {
3984 d = &PyTuple_GET_ITEM(argdefs, 0);
3985 nd = Py_SIZE(argdefs);
3986 }
3987 return PyEval_EvalCodeEx(co, globals,
3988 (PyObject *)NULL, (*pp_stack)-n, na,
3989 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
3990 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003991}
3992
3993static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003994update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3995 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003996{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003997 PyObject *kwdict = NULL;
3998 if (orig_kwdict == NULL)
3999 kwdict = PyDict_New();
4000 else {
4001 kwdict = PyDict_Copy(orig_kwdict);
4002 Py_DECREF(orig_kwdict);
4003 }
4004 if (kwdict == NULL)
4005 return NULL;
4006 while (--nk >= 0) {
4007 int err;
4008 PyObject *value = EXT_POP(*pp_stack);
4009 PyObject *key = EXT_POP(*pp_stack);
4010 if (PyDict_GetItem(kwdict, key) != NULL) {
4011 PyErr_Format(PyExc_TypeError,
4012 "%.200s%s got multiple values "
4013 "for keyword argument '%U'",
4014 PyEval_GetFuncName(func),
4015 PyEval_GetFuncDesc(func),
4016 key);
4017 Py_DECREF(key);
4018 Py_DECREF(value);
4019 Py_DECREF(kwdict);
4020 return NULL;
4021 }
4022 err = PyDict_SetItem(kwdict, key, value);
4023 Py_DECREF(key);
4024 Py_DECREF(value);
4025 if (err) {
4026 Py_DECREF(kwdict);
4027 return NULL;
4028 }
4029 }
4030 return kwdict;
Jeremy Hylton52820442001-01-03 23:52:36 +00004031}
4032
4033static PyObject *
4034update_star_args(int nstack, int nstar, PyObject *stararg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004035 PyObject ***pp_stack)
Jeremy Hylton52820442001-01-03 23:52:36 +00004036{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004037 PyObject *callargs, *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004038
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004039 callargs = PyTuple_New(nstack + nstar);
4040 if (callargs == NULL) {
4041 return NULL;
4042 }
4043 if (nstar) {
4044 int i;
4045 for (i = 0; i < nstar; i++) {
4046 PyObject *a = PyTuple_GET_ITEM(stararg, i);
4047 Py_INCREF(a);
4048 PyTuple_SET_ITEM(callargs, nstack + i, a);
4049 }
4050 }
4051 while (--nstack >= 0) {
4052 w = EXT_POP(*pp_stack);
4053 PyTuple_SET_ITEM(callargs, nstack, w);
4054 }
4055 return callargs;
Jeremy Hylton52820442001-01-03 23:52:36 +00004056}
4057
4058static PyObject *
4059load_args(PyObject ***pp_stack, int na)
4060{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004061 PyObject *args = PyTuple_New(na);
4062 PyObject *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004063
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004064 if (args == NULL)
4065 return NULL;
4066 while (--na >= 0) {
4067 w = EXT_POP(*pp_stack);
4068 PyTuple_SET_ITEM(args, na, w);
4069 }
4070 return args;
Jeremy Hylton52820442001-01-03 23:52:36 +00004071}
4072
4073static PyObject *
4074do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4075{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004076 PyObject *callargs = NULL;
4077 PyObject *kwdict = NULL;
4078 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004079
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004080 if (nk > 0) {
4081 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
4082 if (kwdict == NULL)
4083 goto call_fail;
4084 }
4085 callargs = load_args(pp_stack, na);
4086 if (callargs == NULL)
4087 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004088#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004089 /* At this point, we have to look at the type of func to
4090 update the call stats properly. Do it here so as to avoid
4091 exposing the call stats machinery outside ceval.c
4092 */
4093 if (PyFunction_Check(func))
4094 PCALL(PCALL_FUNCTION);
4095 else if (PyMethod_Check(func))
4096 PCALL(PCALL_METHOD);
4097 else if (PyType_Check(func))
4098 PCALL(PCALL_TYPE);
4099 else if (PyCFunction_Check(func))
4100 PCALL(PCALL_CFUNCTION);
4101 else
4102 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004103#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004104 if (PyCFunction_Check(func)) {
4105 PyThreadState *tstate = PyThreadState_GET();
4106 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4107 }
4108 else
4109 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00004110call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004111 Py_XDECREF(callargs);
4112 Py_XDECREF(kwdict);
4113 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004114}
4115
4116static PyObject *
4117ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4118{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004119 int nstar = 0;
4120 PyObject *callargs = NULL;
4121 PyObject *stararg = NULL;
4122 PyObject *kwdict = NULL;
4123 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004124
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004125 if (flags & CALL_FLAG_KW) {
4126 kwdict = EXT_POP(*pp_stack);
4127 if (!PyDict_Check(kwdict)) {
4128 PyObject *d;
4129 d = PyDict_New();
4130 if (d == NULL)
4131 goto ext_call_fail;
4132 if (PyDict_Update(d, kwdict) != 0) {
4133 Py_DECREF(d);
4134 /* PyDict_Update raises attribute
4135 * error (percolated from an attempt
4136 * to get 'keys' attribute) instead of
4137 * a type error if its second argument
4138 * is not a mapping.
4139 */
4140 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4141 PyErr_Format(PyExc_TypeError,
4142 "%.200s%.200s argument after ** "
4143 "must be a mapping, not %.200s",
4144 PyEval_GetFuncName(func),
4145 PyEval_GetFuncDesc(func),
4146 kwdict->ob_type->tp_name);
4147 }
4148 goto ext_call_fail;
4149 }
4150 Py_DECREF(kwdict);
4151 kwdict = d;
4152 }
4153 }
4154 if (flags & CALL_FLAG_VAR) {
4155 stararg = EXT_POP(*pp_stack);
4156 if (!PyTuple_Check(stararg)) {
4157 PyObject *t = NULL;
4158 t = PySequence_Tuple(stararg);
4159 if (t == NULL) {
4160 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4161 PyErr_Format(PyExc_TypeError,
4162 "%.200s%.200s argument after * "
4163 "must be a sequence, not %200s",
4164 PyEval_GetFuncName(func),
4165 PyEval_GetFuncDesc(func),
4166 stararg->ob_type->tp_name);
4167 }
4168 goto ext_call_fail;
4169 }
4170 Py_DECREF(stararg);
4171 stararg = t;
4172 }
4173 nstar = PyTuple_GET_SIZE(stararg);
4174 }
4175 if (nk > 0) {
4176 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
4177 if (kwdict == NULL)
4178 goto ext_call_fail;
4179 }
4180 callargs = update_star_args(na, nstar, stararg, pp_stack);
4181 if (callargs == NULL)
4182 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004183#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004184 /* At this point, we have to look at the type of func to
4185 update the call stats properly. Do it here so as to avoid
4186 exposing the call stats machinery outside ceval.c
4187 */
4188 if (PyFunction_Check(func))
4189 PCALL(PCALL_FUNCTION);
4190 else if (PyMethod_Check(func))
4191 PCALL(PCALL_METHOD);
4192 else if (PyType_Check(func))
4193 PCALL(PCALL_TYPE);
4194 else if (PyCFunction_Check(func))
4195 PCALL(PCALL_CFUNCTION);
4196 else
4197 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004198#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004199 if (PyCFunction_Check(func)) {
4200 PyThreadState *tstate = PyThreadState_GET();
4201 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4202 }
4203 else
4204 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00004205ext_call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004206 Py_XDECREF(callargs);
4207 Py_XDECREF(kwdict);
4208 Py_XDECREF(stararg);
4209 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004210}
4211
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004212/* Extract a slice index from a PyInt or PyLong or an object with the
4213 nb_index slot defined, and store in *pi.
4214 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4215 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 +00004216 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004217*/
Tim Petersb5196382001-12-16 19:44:20 +00004218/* Note: If v is NULL, return success without storing into *pi. This
4219 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4220 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004221*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004222int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004223_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004224{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004225 if (v != NULL) {
4226 Py_ssize_t x;
4227 if (PyIndex_Check(v)) {
4228 x = PyNumber_AsSsize_t(v, NULL);
4229 if (x == -1 && PyErr_Occurred())
4230 return 0;
4231 }
4232 else {
4233 PyErr_SetString(PyExc_TypeError,
4234 "slice indices must be integers or "
4235 "None or have an __index__ method");
4236 return 0;
4237 }
4238 *pi = x;
4239 }
4240 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004241}
4242
Guido van Rossum486364b2007-06-30 05:01:58 +00004243#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004244 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004245
Guido van Rossumb209a111997-04-29 18:18:01 +00004246static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004247cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004248{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004249 int res = 0;
4250 switch (op) {
4251 case PyCmp_IS:
4252 res = (v == w);
4253 break;
4254 case PyCmp_IS_NOT:
4255 res = (v != w);
4256 break;
4257 case PyCmp_IN:
4258 res = PySequence_Contains(w, v);
4259 if (res < 0)
4260 return NULL;
4261 break;
4262 case PyCmp_NOT_IN:
4263 res = PySequence_Contains(w, v);
4264 if (res < 0)
4265 return NULL;
4266 res = !res;
4267 break;
4268 case PyCmp_EXC_MATCH:
4269 if (PyTuple_Check(w)) {
4270 Py_ssize_t i, length;
4271 length = PyTuple_Size(w);
4272 for (i = 0; i < length; i += 1) {
4273 PyObject *exc = PyTuple_GET_ITEM(w, i);
4274 if (!PyExceptionClass_Check(exc)) {
4275 PyErr_SetString(PyExc_TypeError,
4276 CANNOT_CATCH_MSG);
4277 return NULL;
4278 }
4279 }
4280 }
4281 else {
4282 if (!PyExceptionClass_Check(w)) {
4283 PyErr_SetString(PyExc_TypeError,
4284 CANNOT_CATCH_MSG);
4285 return NULL;
4286 }
4287 }
4288 res = PyErr_GivenExceptionMatches(v, w);
4289 break;
4290 default:
4291 return PyObject_RichCompare(v, w, op);
4292 }
4293 v = res ? Py_True : Py_False;
4294 Py_INCREF(v);
4295 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004296}
4297
Thomas Wouters52152252000-08-17 22:55:00 +00004298static PyObject *
4299import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004300{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004301 PyObject *x;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004303 x = PyObject_GetAttr(v, name);
4304 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
4305 PyErr_Format(PyExc_ImportError, "cannot import name %S", name);
4306 }
4307 return x;
Thomas Wouters52152252000-08-17 22:55:00 +00004308}
Guido van Rossumac7be682001-01-17 15:42:30 +00004309
Thomas Wouters52152252000-08-17 22:55:00 +00004310static int
4311import_all_from(PyObject *locals, PyObject *v)
4312{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004313 PyObject *all = PyObject_GetAttrString(v, "__all__");
4314 PyObject *dict, *name, *value;
4315 int skip_leading_underscores = 0;
4316 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004318 if (all == NULL) {
4319 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4320 return -1; /* Unexpected error */
4321 PyErr_Clear();
4322 dict = PyObject_GetAttrString(v, "__dict__");
4323 if (dict == NULL) {
4324 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4325 return -1;
4326 PyErr_SetString(PyExc_ImportError,
4327 "from-import-* object has no __dict__ and no __all__");
4328 return -1;
4329 }
4330 all = PyMapping_Keys(dict);
4331 Py_DECREF(dict);
4332 if (all == NULL)
4333 return -1;
4334 skip_leading_underscores = 1;
4335 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004336
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004337 for (pos = 0, err = 0; ; pos++) {
4338 name = PySequence_GetItem(all, pos);
4339 if (name == NULL) {
4340 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4341 err = -1;
4342 else
4343 PyErr_Clear();
4344 break;
4345 }
4346 if (skip_leading_underscores &&
4347 PyUnicode_Check(name) &&
4348 PyUnicode_AS_UNICODE(name)[0] == '_')
4349 {
4350 Py_DECREF(name);
4351 continue;
4352 }
4353 value = PyObject_GetAttr(v, name);
4354 if (value == NULL)
4355 err = -1;
4356 else if (PyDict_CheckExact(locals))
4357 err = PyDict_SetItem(locals, name, value);
4358 else
4359 err = PyObject_SetItem(locals, name, value);
4360 Py_DECREF(name);
4361 Py_XDECREF(value);
4362 if (err != 0)
4363 break;
4364 }
4365 Py_DECREF(all);
4366 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004367}
4368
Guido van Rossumac7be682001-01-17 15:42:30 +00004369static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004370format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004371{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004372 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004373
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004374 if (!obj)
4375 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004377 obj_str = _PyUnicode_AsString(obj);
4378 if (!obj_str)
4379 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004381 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00004382}
Guido van Rossum950361c1997-01-24 13:49:28 +00004383
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004384static PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +00004385unicode_concatenate(PyObject *v, PyObject *w,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004386 PyFrameObject *f, unsigned char *next_instr)
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004387{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004388 /* This function implements 'variable += expr' when both arguments
4389 are (Unicode) strings. */
4390 Py_ssize_t v_len = PyUnicode_GET_SIZE(v);
4391 Py_ssize_t w_len = PyUnicode_GET_SIZE(w);
4392 Py_ssize_t new_len = v_len + w_len;
4393 if (new_len < 0) {
4394 PyErr_SetString(PyExc_OverflowError,
4395 "strings are too large to concat");
4396 return NULL;
4397 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00004398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004399 if (v->ob_refcnt == 2) {
4400 /* In the common case, there are 2 references to the value
4401 * stored in 'variable' when the += is performed: one on the
4402 * value stack (in 'v') and one still stored in the
4403 * 'variable'. We try to delete the variable now to reduce
4404 * the refcnt to 1.
4405 */
4406 switch (*next_instr) {
4407 case STORE_FAST:
4408 {
4409 int oparg = PEEKARG();
4410 PyObject **fastlocals = f->f_localsplus;
4411 if (GETLOCAL(oparg) == v)
4412 SETLOCAL(oparg, NULL);
4413 break;
4414 }
4415 case STORE_DEREF:
4416 {
4417 PyObject **freevars = (f->f_localsplus +
4418 f->f_code->co_nlocals);
4419 PyObject *c = freevars[PEEKARG()];
4420 if (PyCell_GET(c) == v)
4421 PyCell_Set(c, NULL);
4422 break;
4423 }
4424 case STORE_NAME:
4425 {
4426 PyObject *names = f->f_code->co_names;
4427 PyObject *name = GETITEM(names, PEEKARG());
4428 PyObject *locals = f->f_locals;
4429 if (PyDict_CheckExact(locals) &&
4430 PyDict_GetItem(locals, name) == v) {
4431 if (PyDict_DelItem(locals, name) != 0) {
4432 PyErr_Clear();
4433 }
4434 }
4435 break;
4436 }
4437 }
4438 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004440 if (v->ob_refcnt == 1 && !PyUnicode_CHECK_INTERNED(v)) {
4441 /* Now we own the last reference to 'v', so we can resize it
4442 * in-place.
4443 */
4444 if (PyUnicode_Resize(&v, new_len) != 0) {
4445 /* XXX if PyUnicode_Resize() fails, 'v' has been
4446 * deallocated so it cannot be put back into
4447 * 'variable'. The MemoryError is raised when there
4448 * is no value in 'variable', which might (very
4449 * remotely) be a cause of incompatibilities.
4450 */
4451 return NULL;
4452 }
4453 /* copy 'w' into the newly allocated area of 'v' */
4454 memcpy(PyUnicode_AS_UNICODE(v) + v_len,
4455 PyUnicode_AS_UNICODE(w), w_len*sizeof(Py_UNICODE));
4456 return v;
4457 }
4458 else {
4459 /* When in-place resizing is not an option. */
4460 w = PyUnicode_Concat(v, w);
4461 Py_DECREF(v);
4462 return w;
4463 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004464}
4465
Guido van Rossum950361c1997-01-24 13:49:28 +00004466#ifdef DYNAMIC_EXECUTION_PROFILE
4467
Skip Montanarof118cb12001-10-15 20:51:38 +00004468static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004469getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004470{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004471 int i;
4472 PyObject *l = PyList_New(256);
4473 if (l == NULL) return NULL;
4474 for (i = 0; i < 256; i++) {
4475 PyObject *x = PyLong_FromLong(a[i]);
4476 if (x == NULL) {
4477 Py_DECREF(l);
4478 return NULL;
4479 }
4480 PyList_SetItem(l, i, x);
4481 }
4482 for (i = 0; i < 256; i++)
4483 a[i] = 0;
4484 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004485}
4486
4487PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004488_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004489{
4490#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004491 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00004492#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004493 int i;
4494 PyObject *l = PyList_New(257);
4495 if (l == NULL) return NULL;
4496 for (i = 0; i < 257; i++) {
4497 PyObject *x = getarray(dxpairs[i]);
4498 if (x == NULL) {
4499 Py_DECREF(l);
4500 return NULL;
4501 }
4502 PyList_SetItem(l, i, x);
4503 }
4504 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004505#endif
4506}
4507
4508#endif