blob: 59054a6e358da72bb6d6b7c28dfc8a33803c58f5 [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
Guido van Rossumb209a111997-04-29 18:18:01 +00009#include "Python.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000010
Guido van Rossum10dc2e81990-11-18 17:27:39 +000011#include "compile.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000012#include "frameobject.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000013#include "eval.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000014#include "opcode.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000015#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000016
Jack Jansencbf630f2000-07-11 21:59:16 +000017#ifdef macintosh
18#include "macglue.h"
19#endif
20
Guido van Rossumc6004111993-11-05 10:22:19 +000021#include <ctype.h>
22
Guido van Rossum04691fc1992-08-12 15:35:34 +000023/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000024/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000025
Guido van Rossum408027e1996-12-30 16:17:54 +000026#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000027/* For debugging the interpreter: */
28#define LLTRACE 1 /* Low-level trace feature */
29#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000030#endif
31
Jeremy Hylton52820442001-01-03 23:52:36 +000032typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000033
Guido van Rossum374a9221991-04-04 10:40:29 +000034/* Forward declarations */
Tim Peters5ca576e2001-06-18 22:08:13 +000035static PyObject *eval_frame(PyFrameObject *);
Jeremy Hyltone8c04322002-08-16 17:47:26 +000036static PyObject *call_function(PyObject ***, int);
Jeremy Hylton52820442001-01-03 23:52:36 +000037static PyObject *fast_function(PyObject *, PyObject ***, int, int, int);
Jeremy Hylton52820442001-01-03 23:52:36 +000038static PyObject *do_call(PyObject *, PyObject ***, int, int);
39static PyObject *ext_do_call(PyObject *, PyObject ***, int, int, int);
Guido van Rossumac7be682001-01-17 15:42:30 +000040static PyObject *update_keyword_args(PyObject *, int, PyObject ***,PyObject *);
Ka-Ping Yee20579702001-01-15 22:14:16 +000041static PyObject *update_star_args(int, int, PyObject *, PyObject ***);
Jeremy Hylton52820442001-01-03 23:52:36 +000042static PyObject *load_args(PyObject ***, int);
43#define CALL_FLAG_VAR 1
44#define CALL_FLAG_KW 2
45
Guido van Rossum0a066c01992-03-27 17:29:15 +000046#ifdef LLTRACE
Tim Petersdbd9ba62000-07-09 03:09:57 +000047static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000048#endif
Fred Drake5755ce62001-06-27 19:19:46 +000049static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
50 int, PyObject *);
Fred Drake4ec5d562001-10-04 19:26:43 +000051static void call_trace_protected(Py_tracefunc, PyObject *,
52 PyFrameObject *, int);
Fred Drake5755ce62001-06-27 19:19:46 +000053static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
Michael W. Hudson006c7522002-11-08 13:08:46 +000054static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Michael W. Hudsondd32a912002-08-15 14:59:02 +000055 PyFrameObject *, int *, int *);
56
Tim Petersdbd9ba62000-07-09 03:09:57 +000057static PyObject *apply_slice(PyObject *, PyObject *, PyObject *);
58static int assign_slice(PyObject *, PyObject *,
59 PyObject *, PyObject *);
60static PyObject *cmp_outcome(int, PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +000061static PyObject *import_from(PyObject *, PyObject *);
62static int import_all_from(PyObject *, PyObject *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000063static PyObject *build_class(PyObject *, PyObject *, PyObject *);
64static int exec_statement(PyFrameObject *,
65 PyObject *, PyObject *, PyObject *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000066static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *);
67static void reset_exc_info(PyThreadState *);
Paul Prescode68140d2000-08-30 20:25:01 +000068static void format_exc_check_arg(PyObject *, char *, PyObject *);
Guido van Rossum374a9221991-04-04 10:40:29 +000069
Paul Prescode68140d2000-08-30 20:25:01 +000070#define NAME_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +000071 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +000072#define GLOBAL_NAME_ERROR_MSG \
73 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000074#define UNBOUNDLOCAL_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +000075 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000076#define UNBOUNDFREE_ERROR_MSG \
77 "free variable '%.200s' referenced before assignment" \
78 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000079
Guido van Rossum950361c1997-01-24 13:49:28 +000080/* Dynamic execution profile */
81#ifdef DYNAMIC_EXECUTION_PROFILE
82#ifdef DXPAIRS
83static long dxpairs[257][256];
84#define dxp dxpairs[256]
85#else
86static long dxp[256];
87#endif
88#endif
89
Jeremy Hylton985eba52003-02-05 23:13:00 +000090/* Function call profile */
91#ifdef CALL_PROFILE
92#define PCALL_NUM 11
93static int pcall[PCALL_NUM];
94
95#define PCALL_ALL 0
96#define PCALL_FUNCTION 1
97#define PCALL_FAST_FUNCTION 2
98#define PCALL_FASTER_FUNCTION 3
99#define PCALL_METHOD 4
100#define PCALL_BOUND_METHOD 5
101#define PCALL_CFUNCTION 6
102#define PCALL_TYPE 7
103#define PCALL_GENERATOR 8
104#define PCALL_OTHER 9
105#define PCALL_POP 10
106
107/* Notes about the statistics
108
109 PCALL_FAST stats
110
111 FAST_FUNCTION means no argument tuple needs to be created.
112 FASTER_FUNCTION means that the fast-path frame setup code is used.
113
114 If there is a method call where the call can be optimized by changing
115 the argument tuple and calling the function directly, it gets recorded
116 twice.
117
118 As a result, the relationship among the statistics appears to be
119 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
120 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
121 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
122 PCALL_METHOD > PCALL_BOUND_METHOD
123*/
124
125#define PCALL(POS) pcall[POS]++
126
127PyObject *
128PyEval_GetCallStats(PyObject *self)
129{
130 return Py_BuildValue("iiiiiiiiii",
131 pcall[0], pcall[1], pcall[2], pcall[3],
132 pcall[4], pcall[5], pcall[6], pcall[7],
133 pcall[8], pcall[9]);
134}
135#else
136#define PCALL(O)
137
138PyObject *
139PyEval_GetCallStats(PyObject *self)
140{
141 Py_INCREF(Py_None);
142 return Py_None;
143}
144#endif
145
Jeremy Hylton938ace62002-07-17 16:30:39 +0000146static PyTypeObject gentype;
Tim Peters5ca576e2001-06-18 22:08:13 +0000147
148typedef struct {
149 PyObject_HEAD
Tim Petersd8e1c9e2001-06-26 20:58:58 +0000150 /* The gi_ prefix is intended to remind of generator-iterator. */
151
152 PyFrameObject *gi_frame;
153
Tim Peterse77f2e22001-06-26 22:24:51 +0000154 /* True if generator is being executed. */
155 int gi_running;
Fred Drake72bc4562002-08-09 18:35:52 +0000156
157 /* List of weak reference. */
158 PyObject *gi_weakreflist;
Tim Peters5ca576e2001-06-18 22:08:13 +0000159} genobject;
160
161static PyObject *
162gen_new(PyFrameObject *f)
163{
Neil Schemenauer08de92a2002-03-18 20:45:09 +0000164 genobject *gen = PyObject_GC_New(genobject, &gentype);
Tim Peters5ca576e2001-06-18 22:08:13 +0000165 if (gen == NULL) {
166 Py_DECREF(f);
167 return NULL;
168 }
Tim Petersd8e1c9e2001-06-26 20:58:58 +0000169 gen->gi_frame = f;
170 gen->gi_running = 0;
Fred Drake72bc4562002-08-09 18:35:52 +0000171 gen->gi_weakreflist = NULL;
Neil Schemenauer08de92a2002-03-18 20:45:09 +0000172 _PyObject_GC_TRACK(gen);
Tim Peters5ca576e2001-06-18 22:08:13 +0000173 return (PyObject *)gen;
174}
175
Neil Schemenauerf8c7c202001-07-12 13:27:49 +0000176static int
177gen_traverse(genobject *gen, visitproc visit, void *arg)
178{
179 return visit((PyObject *)gen->gi_frame, arg);
180}
181
Tim Peters5ca576e2001-06-18 22:08:13 +0000182static void
183gen_dealloc(genobject *gen)
184{
Neil Schemenauer08de92a2002-03-18 20:45:09 +0000185 _PyObject_GC_UNTRACK(gen);
Fred Drake72bc4562002-08-09 18:35:52 +0000186 if (gen->gi_weakreflist != NULL)
187 PyObject_ClearWeakRefs((PyObject *) gen);
Tim Petersd8e1c9e2001-06-26 20:58:58 +0000188 Py_DECREF(gen->gi_frame);
Neil Schemenauer08de92a2002-03-18 20:45:09 +0000189 PyObject_GC_Del(gen);
Tim Peters5ca576e2001-06-18 22:08:13 +0000190}
191
192static PyObject *
193gen_iternext(genobject *gen)
194{
Neil Schemenauer2b13ce82001-06-21 02:41:10 +0000195 PyThreadState *tstate = PyThreadState_GET();
Tim Petersd8e1c9e2001-06-26 20:58:58 +0000196 PyFrameObject *f = gen->gi_frame;
Tim Peters5ca576e2001-06-18 22:08:13 +0000197 PyObject *result;
198
Tim Petersd8e1c9e2001-06-26 20:58:58 +0000199 if (gen->gi_running) {
Tim Peters5ca576e2001-06-18 22:08:13 +0000200 PyErr_SetString(PyExc_ValueError,
201 "generator already executing");
202 return NULL;
203 }
Tim Peters8c963692001-06-23 05:26:56 +0000204 if (f->f_stacktop == NULL)
Tim Peters5ca576e2001-06-18 22:08:13 +0000205 return NULL;
Neil Schemenauer2b13ce82001-06-21 02:41:10 +0000206
207 /* Generators always return to their most recent caller, not
208 * necessarily their creator. */
Tim Peters5eb4b872001-06-23 05:47:56 +0000209 Py_XINCREF(tstate->frame);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +0000210 assert(f->f_back == NULL);
211 f->f_back = tstate->frame;
212
Tim Petersd8e1c9e2001-06-26 20:58:58 +0000213 gen->gi_running = 1;
Tim Peters5ca576e2001-06-18 22:08:13 +0000214 result = eval_frame(f);
Tim Petersd8e1c9e2001-06-26 20:58:58 +0000215 gen->gi_running = 0;
Neil Schemenauer2b13ce82001-06-21 02:41:10 +0000216
217 /* Don't keep the reference to f_back any longer than necessary. It
218 * may keep a chain of frames alive or it could create a reference
219 * cycle. */
Tim Peters5eb4b872001-06-23 05:47:56 +0000220 Py_XDECREF(f->f_back);
Tim Peters6302ec62001-06-20 06:57:32 +0000221 f->f_back = NULL;
Neil Schemenauer2b13ce82001-06-21 02:41:10 +0000222
Tim Petersad1a18b2001-06-23 06:19:16 +0000223 /* If the generator just returned (as opposed to yielding), signal
224 * that the generator is exhausted. */
225 if (result == Py_None && f->f_stacktop == NULL) {
226 Py_DECREF(result);
227 result = NULL;
228 }
229
Neil Schemenauer2b13ce82001-06-21 02:41:10 +0000230 return result;
Tim Peters5ca576e2001-06-18 22:08:13 +0000231}
232
233static PyObject *
Tim Peters5ca576e2001-06-18 22:08:13 +0000234gen_getiter(PyObject *gen)
235{
236 Py_INCREF(gen);
237 return gen;
238}
239
Guido van Rossum6f799372001-09-20 20:46:19 +0000240static PyMemberDef gen_memberlist[] = {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000241 {"gi_frame", T_OBJECT, offsetof(genobject, gi_frame), RO},
242 {"gi_running", T_INT, offsetof(genobject, gi_running), RO},
243 {NULL} /* Sentinel */
244};
Tim Peters5ca576e2001-06-18 22:08:13 +0000245
Tim Peters0c322792002-07-17 16:49:03 +0000246static PyTypeObject gentype = {
Tim Peters5ca576e2001-06-18 22:08:13 +0000247 PyObject_HEAD_INIT(&PyType_Type)
248 0, /* ob_size */
249 "generator", /* tp_name */
Neil Schemenauer08de92a2002-03-18 20:45:09 +0000250 sizeof(genobject), /* tp_basicsize */
Tim Peters5ca576e2001-06-18 22:08:13 +0000251 0, /* tp_itemsize */
252 /* methods */
253 (destructor)gen_dealloc, /* tp_dealloc */
254 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000255 0, /* tp_getattr */
Tim Peters5ca576e2001-06-18 22:08:13 +0000256 0, /* tp_setattr */
257 0, /* tp_compare */
258 0, /* tp_repr */
259 0, /* tp_as_number */
260 0, /* tp_as_sequence */
261 0, /* tp_as_mapping */
262 0, /* tp_hash */
263 0, /* tp_call */
264 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000265 PyObject_GenericGetAttr, /* tp_getattro */
Tim Peters5ca576e2001-06-18 22:08:13 +0000266 0, /* tp_setattro */
267 0, /* tp_as_buffer */
Neil Schemenauer08de92a2002-03-18 20:45:09 +0000268 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
Tim Peters5ca576e2001-06-18 22:08:13 +0000269 0, /* tp_doc */
Neil Schemenauerf8c7c202001-07-12 13:27:49 +0000270 (traverseproc)gen_traverse, /* tp_traverse */
Tim Peters5ca576e2001-06-18 22:08:13 +0000271 0, /* tp_clear */
272 0, /* tp_richcompare */
Fred Drake72bc4562002-08-09 18:35:52 +0000273 offsetof(genobject, gi_weakreflist), /* tp_weaklistoffset */
Tim Peters5ca576e2001-06-18 22:08:13 +0000274 (getiterfunc)gen_getiter, /* tp_iter */
275 (iternextfunc)gen_iternext, /* tp_iternext */
Tim Petersa64295b2002-07-17 00:15:22 +0000276 0, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000277 gen_memberlist, /* tp_members */
278 0, /* tp_getset */
279 0, /* tp_base */
280 0, /* tp_dict */
Tim Peters5ca576e2001-06-18 22:08:13 +0000281};
282
283
Guido van Rossume59214e1994-08-30 08:01:59 +0000284#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000285
Guido van Rossum2571cc81999-04-07 16:07:23 +0000286#ifndef DONT_HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000287#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000288#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000289#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000290
Guido van Rossuma027efa1997-05-05 20:56:21 +0000291extern int _PyThread_Started; /* Flag for Py_Exit */
292
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000293static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
Guido van Rossuma9672091994-09-14 13:31:22 +0000294static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000295
296void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000297PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000298{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000299 if (interpreter_lock)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000300 return;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000301 _PyThread_Started = 1;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000302 interpreter_lock = PyThread_allocate_lock();
303 PyThread_acquire_lock(interpreter_lock, 1);
304 main_thread = PyThread_get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000305}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000306
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000307void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000308PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000309{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000310 PyThread_acquire_lock(interpreter_lock, 1);
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{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000316 PyThread_release_lock(interpreter_lock);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000317}
318
319void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000320PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000321{
322 if (tstate == NULL)
323 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000324 /* Check someone has called PyEval_InitThreads() to create the lock */
325 assert(interpreter_lock);
Guido van Rossum65d5b571998-12-21 19:32:43 +0000326 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000327 if (PyThreadState_Swap(tstate) != NULL)
328 Py_FatalError(
329 "PyEval_AcquireThread: non-NULL old thread state");
330}
331
332void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000333PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000334{
335 if (tstate == NULL)
336 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
337 if (PyThreadState_Swap(NULL) != tstate)
338 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000339 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000340}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000341
342/* This function is called from PyOS_AfterFork to ensure that newly
343 created child processes don't hold locks referring to threads which
344 are not running in the child process. (This could also be done using
345 pthread_atfork mechanism, at least for the pthreads implementation.) */
346
347void
348PyEval_ReInitThreads(void)
349{
350 if (!interpreter_lock)
351 return;
352 /*XXX Can't use PyThread_free_lock here because it does too
353 much error-checking. Doing this cleanly would require
354 adding a new function to each thread_*.h. Instead, just
355 create a new lock and waste a little bit of memory */
356 interpreter_lock = PyThread_allocate_lock();
357 PyThread_acquire_lock(interpreter_lock, 1);
358 main_thread = PyThread_get_thread_ident();
359}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000360#endif
361
Guido van Rossumff4949e1992-08-05 19:58:53 +0000362/* Functions save_thread and restore_thread are always defined so
363 dynamically loaded modules needn't be compiled separately for use
364 with and without threads: */
365
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000366PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000367PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000368{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000369 PyThreadState *tstate = PyThreadState_Swap(NULL);
370 if (tstate == NULL)
371 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000372#ifdef WITH_THREAD
Guido van Rossumb74eca91997-09-30 22:03:16 +0000373 if (interpreter_lock)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000374 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000375#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000376 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000377}
378
379void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000380PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000381{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000382 if (tstate == NULL)
383 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000384#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000385 if (interpreter_lock) {
Guido van Rossumb74eca91997-09-30 22:03:16 +0000386 int err = errno;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000387 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000388 errno = err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000389 }
390#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000391 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000392}
393
394
Guido van Rossuma9672091994-09-14 13:31:22 +0000395/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
396 signal handlers or Mac I/O completion routines) can schedule calls
397 to a function to be called synchronously.
398 The synchronous function is called with one void* argument.
399 It should return 0 for success or -1 for failure -- failure should
400 be accompanied by an exception.
401
402 If registry succeeds, the registry function returns 0; if it fails
403 (e.g. due to too many pending calls) it returns -1 (without setting
404 an exception condition).
405
406 Note that because registry may occur from within signal handlers,
407 or other asynchronous events, calling malloc() is unsafe!
408
409#ifdef WITH_THREAD
410 Any thread can schedule pending calls, but only the main thread
411 will execute them.
412#endif
413
414 XXX WARNING! ASYNCHRONOUSLY EXECUTING CODE!
415 There are two possible race conditions:
416 (1) nested asynchronous registry calls;
417 (2) registry calls made while pending calls are being processed.
418 While (1) is very unlikely, (2) is a real possibility.
419 The current code is safe against (2), but not against (1).
420 The safety against (2) is derived from the fact that only one
421 thread (the main thread) ever takes things out of the queue.
Guido van Rossuma9672091994-09-14 13:31:22 +0000422
Guido van Rossuma027efa1997-05-05 20:56:21 +0000423 XXX Darn! With the advent of thread state, we should have an array
424 of pending calls per thread in the thread state! Later...
425*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000426
Guido van Rossuma9672091994-09-14 13:31:22 +0000427#define NPENDINGCALLS 32
428static struct {
Thomas Wouters334fb892000-07-25 12:56:38 +0000429 int (*func)(void *);
430 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000431} pendingcalls[NPENDINGCALLS];
432static volatile int pendingfirst = 0;
433static volatile int pendinglast = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000434static volatile int things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000435
436int
Thomas Wouters334fb892000-07-25 12:56:38 +0000437Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000438{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000439 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000440 int i, j;
441 /* XXX Begin critical section */
442 /* XXX If you want this to be safe against nested
443 XXX asynchronous calls, you'll have to work harder! */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000444 if (busy)
445 return -1;
446 busy = 1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000447 i = pendinglast;
448 j = (i + 1) % NPENDINGCALLS;
Guido van Rossum04e70322002-07-17 16:57:13 +0000449 if (j == pendingfirst) {
450 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000451 return -1; /* Queue full */
Guido van Rossum04e70322002-07-17 16:57:13 +0000452 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000453 pendingcalls[i].func = func;
454 pendingcalls[i].arg = arg;
455 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000456
457 _Py_Ticker = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000458 things_to_do = 1; /* Signal main loop */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000459 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000460 /* XXX End critical section */
461 return 0;
462}
463
Guido van Rossum180d7b41994-09-29 09:45:57 +0000464int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000465Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000466{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000467 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000468#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000469 if (main_thread && PyThread_get_thread_ident() != main_thread)
Guido van Rossuma9672091994-09-14 13:31:22 +0000470 return 0;
471#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000472 if (busy)
Guido van Rossum180d7b41994-09-29 09:45:57 +0000473 return 0;
474 busy = 1;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000475 things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000476 for (;;) {
477 int i;
Thomas Wouters334fb892000-07-25 12:56:38 +0000478 int (*func)(void *);
479 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000480 i = pendingfirst;
481 if (i == pendinglast)
482 break; /* Queue empty */
483 func = pendingcalls[i].func;
484 arg = pendingcalls[i].arg;
485 pendingfirst = (i + 1) % NPENDINGCALLS;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000486 if (func(arg) < 0) {
487 busy = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000488 things_to_do = 1; /* We're not done yet */
Guido van Rossuma9672091994-09-14 13:31:22 +0000489 return -1;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000490 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000491 }
Guido van Rossum180d7b41994-09-29 09:45:57 +0000492 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000493 return 0;
494}
495
496
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000497/* The interpreter's recursion limit */
498
Guido van Rossum349ff6f2000-09-01 01:52:08 +0000499static int recursion_limit = 1000;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000500
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000501int
502Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000503{
504 return recursion_limit;
505}
506
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000507void
508Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000509{
510 recursion_limit = new_limit;
511}
512
Armin Rigo092381a2003-10-25 14:29:27 +0000513int
514_Py_CheckRecursiveCall(char *where)
515{
516 PyThreadState *tstate = PyThreadState_GET();
517
518#ifdef USE_STACKCHECK
519 if (PyOS_CheckStack()) {
520 --tstate->recursion_depth;
521 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
522 return -1;
523 }
524#endif
525 if (tstate->recursion_depth > recursion_limit) {
526 --tstate->recursion_depth;
527 PyErr_Format(PyExc_RuntimeError,
528 "maximum recursion depth exceeded%s",
529 where);
530 return -1;
531 }
532 return 0;
533}
534
535
Guido van Rossum374a9221991-04-04 10:40:29 +0000536/* Status code for main loop (reason for stack unwind) */
537
538enum why_code {
539 WHY_NOT, /* No error */
540 WHY_EXCEPTION, /* Exception occurred */
541 WHY_RERAISE, /* Exception re-raised by 'finally' */
542 WHY_RETURN, /* 'return' statement */
Jeremy Hylton3faa52e2001-02-01 22:48:12 +0000543 WHY_BREAK, /* 'break' statement */
Tim Peters5ca576e2001-06-18 22:08:13 +0000544 WHY_CONTINUE, /* 'continue' statement */
Tim Peters6e6a63f2001-10-18 20:49:35 +0000545 WHY_YIELD /* 'yield' operator */
Guido van Rossum374a9221991-04-04 10:40:29 +0000546};
547
Tim Petersdbd9ba62000-07-09 03:09:57 +0000548static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
Tim Petersd6d010b2001-06-21 02:49:55 +0000549static int unpack_iterable(PyObject *, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000550
Skip Montanarod581d772002-09-03 20:10:45 +0000551/* for manipulating the thread switch and periodic "stuff" - used to be
552 per thread, now just a pair o' globals */
Skip Montanaro99dba272002-09-03 20:19:06 +0000553int _Py_CheckInterval = 100;
554volatile int _Py_Ticker = 100;
Guido van Rossum374a9221991-04-04 10:40:29 +0000555
Guido van Rossumb209a111997-04-29 18:18:01 +0000556PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000557PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000558{
Jeremy Hylton985eba52003-02-05 23:13:00 +0000559 /* XXX raise SystemError if globals is NULL */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000560 return PyEval_EvalCodeEx(co,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000561 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000562 (PyObject **)NULL, 0,
563 (PyObject **)NULL, 0,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000564 (PyObject **)NULL, 0,
565 NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000566}
567
568
569/* Interpreter main loop */
570
Tim Peters6d6c1a32001-08-02 04:15:00 +0000571static PyObject *
Tim Peters5ca576e2001-06-18 22:08:13 +0000572eval_frame(PyFrameObject *f)
Guido van Rossum374a9221991-04-04 10:40:29 +0000573{
Guido van Rossum950361c1997-01-24 13:49:28 +0000574#ifdef DXPAIRS
575 int lastopcode = 0;
576#endif
Tim Petersb6d14da2001-12-19 04:11:07 +0000577 PyObject **stack_pointer; /* Next free slot in value stack */
Guido van Rossum374a9221991-04-04 10:40:29 +0000578 register unsigned char *next_instr;
Moshe Zadkaaa39a7e2000-08-07 06:34:45 +0000579 register int opcode=0; /* Current opcode */
580 register int oparg=0; /* Current opcode argument, if any */
Guido van Rossum374a9221991-04-04 10:40:29 +0000581 register enum why_code why; /* Reason for block stack unwind */
582 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000583 register PyObject *x; /* Result object -- NULL if error */
584 register PyObject *v; /* Temporary objects popped off stack */
585 register PyObject *w;
586 register PyObject *u;
587 register PyObject *t;
Barry Warsaw23c9ec82000-08-21 15:44:01 +0000588 register PyObject *stream = NULL; /* for PRINT opcodes */
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000589 register PyObject **fastlocals, **freevars;
Guido van Rossum014518f1998-11-23 21:09:51 +0000590 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000591 PyThreadState *tstate = PyThreadState_GET();
Tim Peters5ca576e2001-06-18 22:08:13 +0000592 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000593
594 /* when tracing we set things up so that
595
596 not (instr_lb <= current_bytecode_offset < instr_ub)
597
598 is true when the line being executed has changed. The
599 initial values are such as to make this false the first
600 time it is tested. */
601 int instr_ub = -1, instr_lb = 0;
602
Guido van Rossumd076c731998-10-07 19:42:25 +0000603 unsigned char *first_instr;
Skip Montanaro04d80f82002-08-04 21:03:35 +0000604 PyObject *names;
605 PyObject *consts;
Guido van Rossum96a42c81992-01-12 02:29:51 +0000606#ifdef LLTRACE
Guido van Rossumacbe8da1993-04-15 15:33:52 +0000607 int lltrace;
Guido van Rossum374a9221991-04-04 10:40:29 +0000608#endif
Guido van Rossum408027e1996-12-30 16:17:54 +0000609#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000610 /* Make it easier to find out where we are with a debugger */
Tim Peters5ca576e2001-06-18 22:08:13 +0000611 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000612#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000613
Neal Norwitza81d2202002-07-14 00:27:26 +0000614/* Tuple access macros */
615
616#ifndef Py_DEBUG
617#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
618#else
619#define GETITEM(v, i) PyTuple_GetItem((v), (i))
620#endif
621
Guido van Rossum374a9221991-04-04 10:40:29 +0000622/* Code access macros */
623
Guido van Rossumd076c731998-10-07 19:42:25 +0000624#define INSTR_OFFSET() (next_instr - first_instr)
Guido van Rossum374a9221991-04-04 10:40:29 +0000625#define NEXTOP() (*next_instr++)
626#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Guido van Rossumd076c731998-10-07 19:42:25 +0000627#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000628#define JUMPBY(x) (next_instr += (x))
629
Raymond Hettingerf606f872003-03-16 03:11:04 +0000630/* OpCode prediction macros
631 Some opcodes tend to come in pairs thus making it possible to predict
632 the second code when the first is run. For example, COMPARE_OP is often
633 followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And, those opcodes are often
634 followed by a POP_TOP.
635
636 Verifying the prediction costs a single high-speed test of register
Raymond Hettingerac2072922003-03-16 15:41:11 +0000637 variable against a constant. If the pairing was good, then the
Raymond Hettingerf606f872003-03-16 03:11:04 +0000638 processor has a high likelihood of making its own successful branch
639 prediction which results in a nearly zero overhead transition to the
640 next opcode.
641
642 A successful prediction saves a trip through the eval-loop including
643 its two unpredictable branches, the HASARG test and the switch-case.
644*/
645
Raymond Hettingerac2072922003-03-16 15:41:11 +0000646#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingerf606f872003-03-16 03:11:04 +0000647#define PREDICTED(op) PRED_##op: next_instr++
Raymond Hettinger7dc52212003-03-16 20:14:44 +0000648#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = (next_instr[2]<<8) + \
649 next_instr[1]; next_instr += 3
Raymond Hettingerf606f872003-03-16 03:11:04 +0000650
Guido van Rossum374a9221991-04-04 10:40:29 +0000651/* Stack manipulation macros */
652
653#define STACK_LEVEL() (stack_pointer - f->f_valuestack)
654#define EMPTY() (STACK_LEVEL() == 0)
655#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000656#define SECOND() (stack_pointer[-2])
657#define THIRD() (stack_pointer[-3])
658#define FOURTH() (stack_pointer[-4])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000659#define SET_TOP(v) (stack_pointer[-1] = (v))
660#define SET_SECOND(v) (stack_pointer[-2] = (v))
661#define SET_THIRD(v) (stack_pointer[-3] = (v))
662#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000663#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000664#define BASIC_PUSH(v) (*stack_pointer++ = (v))
665#define BASIC_POP() (*--stack_pointer)
666
Guido van Rossum96a42c81992-01-12 02:29:51 +0000667#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000668#define PUSH(v) { (void)(BASIC_PUSH(v), \
669 lltrace && prtrace(TOP(), "push")); \
670 assert(STACK_LEVEL() <= f->f_stacksize); }
Fred Drakede26cfc2001-10-13 06:11:28 +0000671#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000672#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
673 lltrace && prtrace(TOP(), "stackadj")); \
674 assert(STACK_LEVEL() <= f->f_stacksize); }
Guido van Rossum374a9221991-04-04 10:40:29 +0000675#else
676#define PUSH(v) BASIC_PUSH(v)
677#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000678#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000679#endif
680
Guido van Rossum681d79a1995-07-18 14:51:37 +0000681/* Local variable macros */
682
683#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000684
685/* The SETLOCAL() macro must not DECREF the local variable in-place and
686 then store the new value; it must copy the old value to a temporary
687 value, then store the new value, and then DECREF the temporary value.
688 This is because it is possible that during the DECREF the frame is
689 accessed by other code (e.g. a __del__ method or gc.collect()) and the
690 variable would be pointing to already-freed memory. */
691#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
692 GETLOCAL(i) = value; \
693 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000694
Guido van Rossuma027efa1997-05-05 20:56:21 +0000695/* Start of code */
696
Tim Peters5ca576e2001-06-18 22:08:13 +0000697 if (f == NULL)
698 return NULL;
699
Tim Peters5ca576e2001-06-18 22:08:13 +0000700 /* push frame */
Armin Rigo092381a2003-10-25 14:29:27 +0000701 if (Py_EnterRecursiveCall(""))
Guido van Rossum8861b741996-07-30 16:49:37 +0000702 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000703
Tim Peters5ca576e2001-06-18 22:08:13 +0000704 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000705
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000706 if (tstate->use_tracing) {
707 if (tstate->c_tracefunc != NULL) {
708 /* tstate->c_tracefunc, if defined, is a
709 function that will be called on *every* entry
710 to a code block. Its return value, if not
711 None, is a function that will be called at
712 the start of each executed line of code.
713 (Actually, the function must return itself
714 in order to continue tracing.) The trace
715 functions are called with three arguments:
716 a pointer to the current frame, a string
717 indicating why the function is called, and
718 an argument which depends on the situation.
719 The global trace function is also called
720 whenever an exception is detected. */
721 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
722 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000723 /* Trace function raised an error */
Armin Rigo092381a2003-10-25 14:29:27 +0000724 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000725 }
726 }
727 if (tstate->c_profilefunc != NULL) {
728 /* Similar for c_profilefunc, except it needn't
729 return itself and isn't called for "line" events */
730 if (call_trace(tstate->c_profilefunc,
731 tstate->c_profileobj,
732 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000733 /* Profile function raised an error */
Armin Rigo092381a2003-10-25 14:29:27 +0000734 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000735 }
736 }
737 }
738
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000739 co = f->f_code;
740 names = co->co_names;
741 consts = co->co_consts;
742 fastlocals = f->f_localsplus;
743 freevars = f->f_localsplus + f->f_nlocals;
744 _PyCode_GETCODEPTR(co, &first_instr);
745 /* An explanation is in order for the next line.
746
747 f->f_lasti now refers to the index of the last instruction
748 executed. You might think this was obvious from the name, but
749 this wasn't always true before 2.3! PyFrame_New now sets
750 f->f_lasti to -1 (i.e. the index *before* the first instruction)
751 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
752 does work. Promise. */
753 next_instr = first_instr + f->f_lasti + 1;
754 stack_pointer = f->f_stacktop;
755 assert(stack_pointer != NULL);
756 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
757
Tim Peters5ca576e2001-06-18 22:08:13 +0000758#ifdef LLTRACE
759 lltrace = PyDict_GetItemString(f->f_globals,"__lltrace__") != NULL;
760#endif
761#if defined(Py_DEBUG) || defined(LLTRACE)
762 filename = PyString_AsString(co->co_filename);
763#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000764
Guido van Rossum374a9221991-04-04 10:40:29 +0000765 why = WHY_NOT;
766 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +0000767 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +0000768 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +0000769
Guido van Rossum374a9221991-04-04 10:40:29 +0000770 for (;;) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000771 assert(stack_pointer >= f->f_valuestack); /* else underflow */
772 assert(STACK_LEVEL() <= f->f_stacksize); /* else overflow */
773
Guido van Rossuma027efa1997-05-05 20:56:21 +0000774 /* Do periodic things. Doing this every time through
775 the loop would add too much overhead, so we do it
776 only every Nth instruction. We also do it if
777 ``things_to_do'' is set, i.e. when an asynchronous
778 event needs attention (e.g. a signal handler or
779 async I/O handler); see Py_AddPendingCall() and
780 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000781
Skip Montanarod581d772002-09-03 20:10:45 +0000782 if (--_Py_Ticker < 0) {
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000783 if (*next_instr == SETUP_FINALLY) {
784 /* Make the last opcode before
785 a try: finally: block uninterruptable. */
786 goto fast_next_opcode;
787 }
Skip Montanarod581d772002-09-03 20:10:45 +0000788 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000789 tstate->tick_counter++;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000790 if (things_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +0000791 if (Py_MakePendingCalls() < 0) {
792 why = WHY_EXCEPTION;
793 goto on_error;
794 }
795 }
Guido van Rossumdf0d00e1997-05-20 15:57:49 +0000796#if !defined(HAVE_SIGNAL_H) || defined(macintosh)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000797 /* If we have true signals, the signal handler
798 will call Py_AddPendingCall() so we don't
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000799 have to call PyErr_CheckSignals(). On the
800 Mac and DOS, alas, we have to call it. */
Guido van Rossumb209a111997-04-29 18:18:01 +0000801 if (PyErr_CheckSignals()) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000802 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +0000803 goto on_error;
804 }
Guido van Rossum70d44781997-01-21 06:15:24 +0000805#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000806
Guido van Rossume59214e1994-08-30 08:01:59 +0000807#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000808 if (interpreter_lock) {
809 /* Give another thread a chance */
810
Guido van Rossum25ce5661997-08-02 03:10:38 +0000811 if (PyThreadState_Swap(NULL) != tstate)
812 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000813 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000814
815 /* Other threads may run now */
816
Guido van Rossum65d5b571998-12-21 19:32:43 +0000817 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000818 if (PyThreadState_Swap(tstate) != NULL)
819 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000820
821 /* Check for thread interrupts */
822
823 if (tstate->async_exc != NULL) {
824 x = tstate->async_exc;
825 tstate->async_exc = NULL;
826 PyErr_SetNone(x);
827 Py_DECREF(x);
828 why = WHY_EXCEPTION;
829 goto on_error;
830 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000831 }
832#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000833 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000834
Neil Schemenauer63543862002-02-17 19:10:14 +0000835 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +0000836 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +0000837
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000838 /* line-by-line tracing support */
839
840 if (tstate->c_tracefunc != NULL && !tstate->tracing) {
841 /* see maybe_call_line_trace
842 for expository comments */
843 f->f_stacktop = stack_pointer;
Michael W. Hudson006c7522002-11-08 13:08:46 +0000844
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000845 err = maybe_call_line_trace(tstate->c_tracefunc,
846 tstate->c_traceobj,
847 f, &instr_lb, &instr_ub);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000848 /* Reload possibly changed frame fields */
849 JUMPTO(f->f_lasti);
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000850 if (f->f_stacktop != NULL) {
851 stack_pointer = f->f_stacktop;
852 f->f_stacktop = NULL;
853 }
854 if (err) {
855 /* trace function raised an exception */
856 goto on_error;
857 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000858 }
859
860 /* Extract opcode and argument */
861
Guido van Rossum374a9221991-04-04 10:40:29 +0000862 opcode = NEXTOP();
863 if (HAS_ARG(opcode))
864 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +0000865 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +0000866#ifdef DYNAMIC_EXECUTION_PROFILE
867#ifdef DXPAIRS
868 dxpairs[lastopcode][opcode]++;
869 lastopcode = opcode;
870#endif
871 dxp[opcode]++;
872#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000873
Guido van Rossum96a42c81992-01-12 02:29:51 +0000874#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +0000875 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +0000876
Guido van Rossum96a42c81992-01-12 02:29:51 +0000877 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000878 if (HAS_ARG(opcode)) {
879 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000880 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +0000881 }
882 else {
883 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000884 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +0000885 }
886 }
887#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000888
Guido van Rossum374a9221991-04-04 10:40:29 +0000889 /* Main switch on opcode */
Jeremy Hylton52820442001-01-03 23:52:36 +0000890
Guido van Rossum374a9221991-04-04 10:40:29 +0000891 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +0000892
Guido van Rossum374a9221991-04-04 10:40:29 +0000893 /* BEWARE!
894 It is essential that any operation that fails sets either
895 x to NULL, err to nonzero, or why to anything but WHY_NOT,
896 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000897
Guido van Rossum374a9221991-04-04 10:40:29 +0000898 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000899
Neil Schemenauer63543862002-02-17 19:10:14 +0000900 case LOAD_FAST:
901 x = GETLOCAL(oparg);
902 if (x != NULL) {
903 Py_INCREF(x);
904 PUSH(x);
905 goto fast_next_opcode;
906 }
907 format_exc_check_arg(PyExc_UnboundLocalError,
908 UNBOUNDLOCAL_ERROR_MSG,
909 PyTuple_GetItem(co->co_varnames, oparg));
910 break;
911
912 case LOAD_CONST:
Skip Montanaro04d80f82002-08-04 21:03:35 +0000913 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +0000914 Py_INCREF(x);
915 PUSH(x);
916 goto fast_next_opcode;
917
Raymond Hettinger7dc52212003-03-16 20:14:44 +0000918 PREDICTED_WITH_ARG(STORE_FAST);
Neil Schemenauer63543862002-02-17 19:10:14 +0000919 case STORE_FAST:
920 v = POP();
921 SETLOCAL(oparg, v);
922 goto fast_next_opcode;
923
Raymond Hettingerf606f872003-03-16 03:11:04 +0000924 PREDICTED(POP_TOP);
Guido van Rossum374a9221991-04-04 10:40:29 +0000925 case POP_TOP:
926 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000927 Py_DECREF(v);
Neil Schemenauer63543862002-02-17 19:10:14 +0000928 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000929
Guido van Rossum374a9221991-04-04 10:40:29 +0000930 case ROT_TWO:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000931 v = TOP();
932 w = SECOND();
933 SET_TOP(w);
934 SET_SECOND(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000935 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000936
Guido van Rossum374a9221991-04-04 10:40:29 +0000937 case ROT_THREE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000938 v = TOP();
939 w = SECOND();
940 x = THIRD();
941 SET_TOP(w);
942 SET_SECOND(x);
943 SET_THIRD(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000944 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000945
Thomas Wouters434d0822000-08-24 20:11:32 +0000946 case ROT_FOUR:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000947 u = TOP();
948 v = SECOND();
949 w = THIRD();
950 x = FOURTH();
951 SET_TOP(v);
952 SET_SECOND(w);
953 SET_THIRD(x);
954 SET_FOURTH(u);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000955 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000956
Guido van Rossum374a9221991-04-04 10:40:29 +0000957 case DUP_TOP:
958 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000959 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000960 PUSH(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000961 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000962
Thomas Wouters434d0822000-08-24 20:11:32 +0000963 case DUP_TOPX:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000964 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000965 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000966 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000967 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000968 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000969 STACKADJ(2);
970 SET_TOP(x);
971 SET_SECOND(w);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000972 goto fast_next_opcode;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000973 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000974 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000975 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000976 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000977 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000978 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +0000979 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000980 STACKADJ(3);
981 SET_TOP(x);
982 SET_SECOND(w);
983 SET_THIRD(v);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000984 goto fast_next_opcode;
Thomas Wouters434d0822000-08-24 20:11:32 +0000985 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000986 Py_FatalError("invalid argument to DUP_TOPX"
987 " (bytecode corruption?)");
Tim Peters35ba6892000-10-11 07:04:49 +0000988 break;
Thomas Wouters434d0822000-08-24 20:11:32 +0000989
Guido van Rossum374a9221991-04-04 10:40:29 +0000990 case UNARY_POSITIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000991 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000992 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000993 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000994 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000995 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000996 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000997
Guido van Rossum374a9221991-04-04 10:40:29 +0000998 case UNARY_NEGATIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000999 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001000 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001001 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001002 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001003 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001004 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001005
Guido van Rossum374a9221991-04-04 10:40:29 +00001006 case UNARY_NOT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001007 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001008 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001009 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +00001010 if (err == 0) {
1011 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001012 SET_TOP(Py_True);
Guido van Rossumfc490731997-05-06 15:06:49 +00001013 continue;
1014 }
1015 else if (err > 0) {
1016 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001017 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +00001018 err = 0;
1019 continue;
1020 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00001021 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001022 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001023
Guido van Rossum374a9221991-04-04 10:40:29 +00001024 case UNARY_CONVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001025 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001026 x = PyObject_Repr(v);
1027 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001028 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001029 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001030 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001031
Guido van Rossum7928cd71991-10-24 14:59:31 +00001032 case UNARY_INVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001033 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001034 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001035 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001036 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001037 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001038 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001039
Guido van Rossum50564e81996-01-12 01:13:16 +00001040 case BINARY_POWER:
1041 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001042 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001043 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +00001044 Py_DECREF(v);
1045 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001046 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001047 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +00001048 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001049
Guido van Rossum374a9221991-04-04 10:40:29 +00001050 case BINARY_MULTIPLY:
1051 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001052 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001053 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001054 Py_DECREF(v);
1055 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001056 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001057 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001058 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001059
Guido van Rossum374a9221991-04-04 10:40:29 +00001060 case BINARY_DIVIDE:
Tim Peters3caca232001-12-06 06:23:26 +00001061 if (!_Py_QnewFlag) {
1062 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001063 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001064 x = PyNumber_Divide(v, w);
1065 Py_DECREF(v);
1066 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001067 SET_TOP(x);
Tim Peters3caca232001-12-06 06:23:26 +00001068 if (x != NULL) continue;
1069 break;
1070 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001071 /* -Qnew is in effect: fall through to
Tim Peters3caca232001-12-06 06:23:26 +00001072 BINARY_TRUE_DIVIDE */
1073 case BINARY_TRUE_DIVIDE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001074 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001075 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001076 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001077 Py_DECREF(v);
1078 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001079 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001080 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001081 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001082
Guido van Rossum4668b002001-08-08 05:00:18 +00001083 case BINARY_FLOOR_DIVIDE:
1084 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001085 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001086 x = PyNumber_FloorDivide(v, w);
1087 Py_DECREF(v);
1088 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001089 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001090 if (x != NULL) continue;
1091 break;
1092
Guido van Rossum374a9221991-04-04 10:40:29 +00001093 case BINARY_MODULO:
1094 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001095 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001096 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001097 Py_DECREF(v);
1098 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001099 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001100 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001101 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001102
Guido van Rossum374a9221991-04-04 10:40:29 +00001103 case BINARY_ADD:
1104 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001105 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001106 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001107 /* INLINE: int + int */
1108 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001109 a = PyInt_AS_LONG(v);
1110 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001111 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001112 if ((i^a) < 0 && (i^b) < 0)
1113 goto slow_add;
1114 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001115 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001116 else {
1117 slow_add:
Guido van Rossumc12da691997-07-17 23:12:42 +00001118 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001119 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001120 Py_DECREF(v);
1121 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001122 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001123 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001124 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001125
Guido van Rossum374a9221991-04-04 10:40:29 +00001126 case BINARY_SUBTRACT:
1127 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001128 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001129 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001130 /* INLINE: int - int */
1131 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001132 a = PyInt_AS_LONG(v);
1133 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001134 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001135 if ((i^a) < 0 && (i^~b) < 0)
1136 goto slow_sub;
1137 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001138 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001139 else {
1140 slow_sub:
Guido van Rossumc12da691997-07-17 23:12:42 +00001141 x = PyNumber_Subtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001142 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001143 Py_DECREF(v);
1144 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001145 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001146 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001147 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001148
Guido van Rossum374a9221991-04-04 10:40:29 +00001149 case BINARY_SUBSCR:
1150 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001151 v = TOP();
Tim Petersb1c46982001-10-05 20:41:38 +00001152 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001153 /* INLINE: list[int] */
1154 long i = PyInt_AsLong(w);
1155 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +00001156 i += PyList_GET_SIZE(v);
Guido van Rossumc12da691997-07-17 23:12:42 +00001157 if (i < 0 ||
Guido van Rossumfa00e951998-07-08 15:02:37 +00001158 i >= PyList_GET_SIZE(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001159 PyErr_SetString(PyExc_IndexError,
1160 "list index out of range");
1161 x = NULL;
1162 }
1163 else {
Guido van Rossumfa00e951998-07-08 15:02:37 +00001164 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001165 Py_INCREF(x);
1166 }
1167 }
1168 else
1169 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001170 Py_DECREF(v);
1171 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001172 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001173 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001174 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001175
Guido van Rossum7928cd71991-10-24 14:59:31 +00001176 case BINARY_LSHIFT:
1177 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001178 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001179 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001180 Py_DECREF(v);
1181 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001182 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001183 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001184 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001185
Guido van Rossum7928cd71991-10-24 14:59:31 +00001186 case BINARY_RSHIFT:
1187 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001188 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001189 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001190 Py_DECREF(v);
1191 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001192 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001193 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001194 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001195
Guido van Rossum7928cd71991-10-24 14:59:31 +00001196 case BINARY_AND:
1197 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001198 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001199 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001200 Py_DECREF(v);
1201 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001202 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001203 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001204 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001205
Guido van Rossum7928cd71991-10-24 14:59:31 +00001206 case BINARY_XOR:
1207 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001208 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001209 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001210 Py_DECREF(v);
1211 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001212 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001213 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001214 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001215
Guido van Rossum7928cd71991-10-24 14:59:31 +00001216 case BINARY_OR:
1217 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001218 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001219 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001220 Py_DECREF(v);
1221 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001222 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001223 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001224 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001225
1226 case INPLACE_POWER:
1227 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001228 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001229 x = PyNumber_InPlacePower(v, w, Py_None);
1230 Py_DECREF(v);
1231 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001232 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001233 if (x != NULL) continue;
1234 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001235
Thomas Wouters434d0822000-08-24 20:11:32 +00001236 case INPLACE_MULTIPLY:
1237 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001238 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001239 x = PyNumber_InPlaceMultiply(v, w);
1240 Py_DECREF(v);
1241 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001242 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001243 if (x != NULL) continue;
1244 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001245
Thomas Wouters434d0822000-08-24 20:11:32 +00001246 case INPLACE_DIVIDE:
Tim Peters54b11912001-12-25 18:49:11 +00001247 if (!_Py_QnewFlag) {
1248 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001249 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001250 x = PyNumber_InPlaceDivide(v, w);
1251 Py_DECREF(v);
1252 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001253 SET_TOP(x);
Tim Peters54b11912001-12-25 18:49:11 +00001254 if (x != NULL) continue;
1255 break;
1256 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001257 /* -Qnew is in effect: fall through to
Tim Peters54b11912001-12-25 18:49:11 +00001258 INPLACE_TRUE_DIVIDE */
1259 case INPLACE_TRUE_DIVIDE:
Thomas Wouters434d0822000-08-24 20:11:32 +00001260 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001261 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001262 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001263 Py_DECREF(v);
1264 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001265 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001266 if (x != NULL) continue;
1267 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001268
Guido van Rossum4668b002001-08-08 05:00:18 +00001269 case INPLACE_FLOOR_DIVIDE:
1270 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001271 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001272 x = PyNumber_InPlaceFloorDivide(v, w);
1273 Py_DECREF(v);
1274 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001275 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001276 if (x != NULL) continue;
1277 break;
1278
Thomas Wouters434d0822000-08-24 20:11:32 +00001279 case INPLACE_MODULO:
1280 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001281 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001282 x = PyNumber_InPlaceRemainder(v, w);
1283 Py_DECREF(v);
1284 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001285 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001286 if (x != NULL) continue;
1287 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001288
Thomas Wouters434d0822000-08-24 20:11:32 +00001289 case INPLACE_ADD:
1290 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001291 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001292 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001293 /* INLINE: int + int */
1294 register long a, b, i;
1295 a = PyInt_AS_LONG(v);
1296 b = PyInt_AS_LONG(w);
1297 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001298 if ((i^a) < 0 && (i^b) < 0)
1299 goto slow_iadd;
1300 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001301 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001302 else {
1303 slow_iadd:
Thomas Wouters434d0822000-08-24 20:11:32 +00001304 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001305 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001306 Py_DECREF(v);
1307 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001308 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001309 if (x != NULL) continue;
1310 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001311
Thomas Wouters434d0822000-08-24 20:11:32 +00001312 case INPLACE_SUBTRACT:
1313 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001314 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001315 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001316 /* INLINE: int - int */
1317 register long a, b, i;
1318 a = PyInt_AS_LONG(v);
1319 b = PyInt_AS_LONG(w);
1320 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001321 if ((i^a) < 0 && (i^~b) < 0)
1322 goto slow_isub;
1323 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001324 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001325 else {
1326 slow_isub:
Thomas Wouters434d0822000-08-24 20:11:32 +00001327 x = PyNumber_InPlaceSubtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001328 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001329 Py_DECREF(v);
1330 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001331 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001332 if (x != NULL) continue;
1333 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001334
Thomas Wouters434d0822000-08-24 20:11:32 +00001335 case INPLACE_LSHIFT:
1336 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001337 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001338 x = PyNumber_InPlaceLshift(v, w);
1339 Py_DECREF(v);
1340 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001341 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001342 if (x != NULL) continue;
1343 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001344
Thomas Wouters434d0822000-08-24 20:11:32 +00001345 case INPLACE_RSHIFT:
1346 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001347 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001348 x = PyNumber_InPlaceRshift(v, w);
1349 Py_DECREF(v);
1350 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001351 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001352 if (x != NULL) continue;
1353 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001354
Thomas Wouters434d0822000-08-24 20:11:32 +00001355 case INPLACE_AND:
1356 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001357 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001358 x = PyNumber_InPlaceAnd(v, w);
1359 Py_DECREF(v);
1360 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001361 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001362 if (x != NULL) continue;
1363 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001364
Thomas Wouters434d0822000-08-24 20:11:32 +00001365 case INPLACE_XOR:
1366 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001367 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001368 x = PyNumber_InPlaceXor(v, w);
1369 Py_DECREF(v);
1370 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001371 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001372 if (x != NULL) continue;
1373 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001374
Thomas Wouters434d0822000-08-24 20:11:32 +00001375 case INPLACE_OR:
1376 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001377 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001378 x = PyNumber_InPlaceOr(v, w);
1379 Py_DECREF(v);
1380 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001381 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001382 if (x != NULL) continue;
1383 break;
1384
Guido van Rossum374a9221991-04-04 10:40:29 +00001385 case SLICE+0:
1386 case SLICE+1:
1387 case SLICE+2:
1388 case SLICE+3:
1389 if ((opcode-SLICE) & 2)
1390 w = POP();
1391 else
1392 w = NULL;
1393 if ((opcode-SLICE) & 1)
1394 v = POP();
1395 else
1396 v = NULL;
Raymond Hettinger663004b2003-01-09 15:24:30 +00001397 u = TOP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001398 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001399 Py_DECREF(u);
1400 Py_XDECREF(v);
1401 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001402 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001403 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001404 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001405
Guido van Rossum374a9221991-04-04 10:40:29 +00001406 case STORE_SLICE+0:
1407 case STORE_SLICE+1:
1408 case STORE_SLICE+2:
1409 case STORE_SLICE+3:
1410 if ((opcode-STORE_SLICE) & 2)
1411 w = POP();
1412 else
1413 w = NULL;
1414 if ((opcode-STORE_SLICE) & 1)
1415 v = POP();
1416 else
1417 v = NULL;
1418 u = POP();
1419 t = POP();
1420 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +00001421 Py_DECREF(t);
1422 Py_DECREF(u);
1423 Py_XDECREF(v);
1424 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001425 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001426 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001427
Guido van Rossum374a9221991-04-04 10:40:29 +00001428 case DELETE_SLICE+0:
1429 case DELETE_SLICE+1:
1430 case DELETE_SLICE+2:
1431 case DELETE_SLICE+3:
1432 if ((opcode-DELETE_SLICE) & 2)
1433 w = POP();
1434 else
1435 w = NULL;
1436 if ((opcode-DELETE_SLICE) & 1)
1437 v = POP();
1438 else
1439 v = NULL;
1440 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001441 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +00001442 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00001443 Py_DECREF(u);
1444 Py_XDECREF(v);
1445 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001446 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001447 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001448
Guido van Rossum374a9221991-04-04 10:40:29 +00001449 case STORE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001450 w = TOP();
1451 v = SECOND();
1452 u = THIRD();
1453 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001454 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001455 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001456 Py_DECREF(u);
1457 Py_DECREF(v);
1458 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001459 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001460 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001461
Guido van Rossum374a9221991-04-04 10:40:29 +00001462 case DELETE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001463 w = TOP();
1464 v = SECOND();
1465 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001466 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001467 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001468 Py_DECREF(v);
1469 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001470 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001471 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001472
Guido van Rossum374a9221991-04-04 10:40:29 +00001473 case PRINT_EXPR:
1474 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001475 w = PySys_GetObject("displayhook");
1476 if (w == NULL) {
1477 PyErr_SetString(PyExc_RuntimeError,
1478 "lost sys.displayhook");
1479 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001480 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001481 }
1482 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001483 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001484 if (x == NULL)
1485 err = -1;
1486 }
1487 if (err == 0) {
1488 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001489 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001490 if (w == NULL)
1491 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001492 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001493 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001494 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001495 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001496
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001497 case PRINT_ITEM_TO:
1498 w = stream = POP();
1499 /* fall through to PRINT_ITEM */
1500
Guido van Rossum374a9221991-04-04 10:40:29 +00001501 case PRINT_ITEM:
1502 v = POP();
Barry Warsaw093abe02000-08-29 04:56:13 +00001503 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001504 w = PySys_GetObject("stdout");
1505 if (w == NULL) {
1506 PyErr_SetString(PyExc_RuntimeError,
1507 "lost sys.stdout");
1508 err = -1;
1509 }
Guido van Rossum8f183201997-12-31 05:53:15 +00001510 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001511 /* PyFile_SoftSpace() can exececute arbitrary code
1512 if sys.stdout is an instance with a __getattr__.
1513 If __getattr__ raises an exception, w will
1514 be freed, so we need to prevent that temporarily. */
1515 Py_XINCREF(w);
Tim Peters8e5fd532002-03-24 19:25:00 +00001516 if (w != NULL && PyFile_SoftSpace(w, 0))
Guido van Rossumbe270261997-05-22 22:26:18 +00001517 err = PyFile_WriteString(" ", w);
1518 if (err == 0)
1519 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001520 if (err == 0) {
Tim Peters8e5fd532002-03-24 19:25:00 +00001521 /* XXX move into writeobject() ? */
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001522 if (PyString_Check(v)) {
1523 char *s = PyString_AS_STRING(v);
1524 int len = PyString_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001525 if (len == 0 ||
1526 !isspace(Py_CHARMASK(s[len-1])) ||
1527 s[len-1] == ' ')
1528 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001529 }
Martin v. Löwis8d3ce5a2001-12-18 22:36:40 +00001530#ifdef Py_USING_UNICODE
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001531 else if (PyUnicode_Check(v)) {
1532 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
1533 int len = PyUnicode_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001534 if (len == 0 ||
1535 !Py_UNICODE_ISSPACE(s[len-1]) ||
1536 s[len-1] == ' ')
1537 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001538 }
Michael W. Hudsond95c8282002-05-20 13:56:11 +00001539#endif
Tim Peters8e5fd532002-03-24 19:25:00 +00001540 else
1541 PyFile_SoftSpace(w, 1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001542 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001543 Py_XDECREF(w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001544 Py_DECREF(v);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001545 Py_XDECREF(stream);
1546 stream = NULL;
1547 if (err == 0)
1548 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001549 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001550
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001551 case PRINT_NEWLINE_TO:
1552 w = stream = POP();
1553 /* fall through to PRINT_NEWLINE */
1554
Guido van Rossum374a9221991-04-04 10:40:29 +00001555 case PRINT_NEWLINE:
Barry Warsaw093abe02000-08-29 04:56:13 +00001556 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001557 w = PySys_GetObject("stdout");
1558 if (w == NULL)
1559 PyErr_SetString(PyExc_RuntimeError,
1560 "lost sys.stdout");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001561 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001562 if (w != NULL) {
1563 err = PyFile_WriteString("\n", w);
1564 if (err == 0)
1565 PyFile_SoftSpace(w, 0);
1566 }
1567 Py_XDECREF(stream);
1568 stream = NULL;
Guido van Rossum374a9221991-04-04 10:40:29 +00001569 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001570
Thomas Wouters434d0822000-08-24 20:11:32 +00001571
1572#ifdef CASE_TOO_BIG
1573 default: switch (opcode) {
1574#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001575 case BREAK_LOOP:
1576 why = WHY_BREAK;
1577 break;
Guido van Rossum66b0e9c2001-03-21 19:17:22 +00001578
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00001579 case CONTINUE_LOOP:
1580 retval = PyInt_FromLong(oparg);
1581 why = WHY_CONTINUE;
1582 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001583
Guido van Rossumf10570b1995-07-07 22:53:21 +00001584 case RAISE_VARARGS:
1585 u = v = w = NULL;
1586 switch (oparg) {
1587 case 3:
1588 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001589 /* Fallthrough */
1590 case 2:
1591 v = POP(); /* value */
1592 /* Fallthrough */
1593 case 1:
1594 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001595 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001596 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001597 break;
1598 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001599 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001600 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001601 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001602 break;
1603 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001604 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001605
Guido van Rossum374a9221991-04-04 10:40:29 +00001606 case LOAD_LOCALS:
Guido van Rossum681d79a1995-07-18 14:51:37 +00001607 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001608 PyErr_SetString(PyExc_SystemError,
1609 "no locals");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001610 break;
1611 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001612 Py_INCREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001613 PUSH(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001614 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001615
Guido van Rossum374a9221991-04-04 10:40:29 +00001616 case RETURN_VALUE:
1617 retval = POP();
1618 why = WHY_RETURN;
1619 break;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001620
Tim Peters5ca576e2001-06-18 22:08:13 +00001621 case YIELD_VALUE:
1622 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001623 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001624 why = WHY_YIELD;
1625 break;
1626
1627
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001628 case EXEC_STMT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001629 w = TOP();
1630 v = SECOND();
1631 u = THIRD();
1632 STACKADJ(-3);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001633 err = exec_statement(f, u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001634 Py_DECREF(u);
1635 Py_DECREF(v);
1636 Py_DECREF(w);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001637 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001638
Guido van Rossum374a9221991-04-04 10:40:29 +00001639 case POP_BLOCK:
1640 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001641 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001642 while (STACK_LEVEL() > b->b_level) {
1643 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001644 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001645 }
1646 }
1647 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001648
Guido van Rossum374a9221991-04-04 10:40:29 +00001649 case END_FINALLY:
1650 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001651 if (PyInt_Check(v)) {
Raymond Hettinger080cb322003-03-14 01:37:42 +00001652 why = (enum why_code) PyInt_AS_LONG(v);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00001653 if (why == WHY_RETURN ||
Tim Peters5ca576e2001-06-18 22:08:13 +00001654 why == WHY_YIELD ||
Guido van Rossumc5fe5eb2002-06-12 03:45:21 +00001655 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001656 retval = POP();
1657 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001658 else if (PyString_Check(v) || PyClass_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001659 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001660 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001661 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001662 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001663 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001664 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001665 else if (v != Py_None) {
1666 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001667 "'finally' pops bad exception");
1668 why = WHY_EXCEPTION;
1669 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001670 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001671 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001672
Guido van Rossum374a9221991-04-04 10:40:29 +00001673 case BUILD_CLASS:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001674 u = TOP();
1675 v = SECOND();
1676 w = THIRD();
1677 STACKADJ(-2);
Guido van Rossum25831651993-05-19 14:50:45 +00001678 x = build_class(u, v, w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001679 SET_TOP(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001680 Py_DECREF(u);
1681 Py_DECREF(v);
1682 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001683 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001684
Guido van Rossum374a9221991-04-04 10:40:29 +00001685 case STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001686 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001687 v = POP();
Guido van Rossum681d79a1995-07-18 14:51:37 +00001688 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001689 PyErr_Format(PyExc_SystemError,
1690 "no locals found when storing %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001691 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001692 break;
1693 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001694 err = PyDict_SetItem(x, w, v);
1695 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001696 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001697
Guido van Rossum374a9221991-04-04 10:40:29 +00001698 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001699 w = GETITEM(names, oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001700 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001701 PyErr_Format(PyExc_SystemError,
1702 "no locals when deleting %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001703 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001704 break;
1705 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001706 if ((err = PyDict_DelItem(x, w)) != 0)
Guido van Rossumac7be682001-01-17 15:42:30 +00001707 format_exc_check_arg(PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001708 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001709 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001710
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001711 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001712 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001713 v = POP();
Raymond Hettinger21012b82003-02-26 18:11:50 +00001714 if (PyTuple_CheckExact(v)) {
Barry Warsawe42b18f1997-08-25 22:13:04 +00001715 if (PyTuple_Size(v) != oparg) {
1716 PyErr_SetString(PyExc_ValueError,
1717 "unpack tuple of wrong size");
1718 why = WHY_EXCEPTION;
1719 }
1720 else {
1721 for (; --oparg >= 0; ) {
1722 w = PyTuple_GET_ITEM(v, oparg);
1723 Py_INCREF(w);
1724 PUSH(w);
1725 }
1726 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001727 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00001728 else if (PyList_CheckExact(v)) {
Barry Warsawe42b18f1997-08-25 22:13:04 +00001729 if (PyList_Size(v) != oparg) {
1730 PyErr_SetString(PyExc_ValueError,
1731 "unpack list of wrong size");
1732 why = WHY_EXCEPTION;
1733 }
1734 else {
1735 for (; --oparg >= 0; ) {
1736 w = PyList_GET_ITEM(v, oparg);
1737 Py_INCREF(w);
1738 PUSH(w);
1739 }
1740 }
1741 }
Tim Petersd6d010b2001-06-21 02:49:55 +00001742 else if (unpack_iterable(v, oparg,
1743 stack_pointer + oparg))
1744 stack_pointer += oparg;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001745 else {
1746 if (PyErr_ExceptionMatches(PyExc_TypeError))
1747 PyErr_SetString(PyExc_TypeError,
1748 "unpack non-sequence");
Barry Warsawe42b18f1997-08-25 22:13:04 +00001749 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001750 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001751 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001752 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001753
Guido van Rossum374a9221991-04-04 10:40:29 +00001754 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001755 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001756 v = TOP();
1757 u = SECOND();
1758 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001759 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1760 Py_DECREF(v);
1761 Py_DECREF(u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001762 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001763
Guido van Rossum374a9221991-04-04 10:40:29 +00001764 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001765 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001766 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001767 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1768 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001769 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001770 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001771
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001772 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001773 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001774 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001775 err = PyDict_SetItem(f->f_globals, w, v);
1776 Py_DECREF(v);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001777 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001778
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001779 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001780 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001781 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001782 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001783 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001784 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001785
Guido van Rossum374a9221991-04-04 10:40:29 +00001786 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001787 w = GETITEM(names, oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001788 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001789 PyErr_Format(PyExc_SystemError,
1790 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001791 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001792 break;
1793 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001794 x = PyDict_GetItem(x, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001795 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001796 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001797 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001798 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001799 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001800 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001801 PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001802 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001803 break;
1804 }
1805 }
1806 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001807 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001808 PUSH(x);
1809 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001810
Guido van Rossum374a9221991-04-04 10:40:29 +00001811 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001812 w = GETITEM(names, oparg);
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001813 if (PyString_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00001814 /* Inline the PyDict_GetItem() calls.
1815 WARNING: this is an extreme speed hack.
1816 Do not try this at home. */
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001817 long hash = ((PyStringObject *)w)->ob_shash;
1818 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001819 PyDictObject *d;
1820 d = (PyDictObject *)(f->f_globals);
1821 x = d->ma_lookup(d, w, hash)->me_value;
1822 if (x != NULL) {
1823 Py_INCREF(x);
1824 PUSH(x);
1825 continue;
1826 }
1827 d = (PyDictObject *)(f->f_builtins);
1828 x = d->ma_lookup(d, w, hash)->me_value;
1829 if (x != NULL) {
1830 Py_INCREF(x);
1831 PUSH(x);
1832 continue;
1833 }
1834 goto load_global_error;
1835 }
1836 }
1837 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00001838 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001839 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001840 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001841 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001842 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00001843 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001844 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001845 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001846 break;
1847 }
1848 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001849 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001850 PUSH(x);
1851 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001852
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001853 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001854 x = GETLOCAL(oparg);
1855 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001856 format_exc_check_arg(
1857 PyExc_UnboundLocalError,
1858 UNBOUNDLOCAL_ERROR_MSG,
1859 PyTuple_GetItem(co->co_varnames, oparg)
1860 );
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001861 break;
1862 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00001863 SETLOCAL(oparg, NULL);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001864 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001865
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001866 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001867 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001868 Py_INCREF(x);
1869 PUSH(x);
1870 break;
1871
1872 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001873 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001874 w = PyCell_Get(x);
Jeremy Hylton2524d692001-02-05 17:23:16 +00001875 if (w == NULL) {
Jeremy Hylton76c81ee2002-07-11 16:56:38 +00001876 err = -1;
1877 /* Don't stomp existing exception */
1878 if (PyErr_Occurred())
1879 break;
Jeremy Hyltonc76770c2001-04-13 16:51:46 +00001880 if (oparg < f->f_ncells) {
Jeremy Hylton2524d692001-02-05 17:23:16 +00001881 v = PyTuple_GetItem(co->co_cellvars,
1882 oparg);
Jeremy Hyltonc76770c2001-04-13 16:51:46 +00001883 format_exc_check_arg(
1884 PyExc_UnboundLocalError,
1885 UNBOUNDLOCAL_ERROR_MSG,
1886 v);
1887 } else {
Jeremy Hylton2524d692001-02-05 17:23:16 +00001888 v = PyTuple_GetItem(
1889 co->co_freevars,
1890 oparg - f->f_ncells);
Jeremy Hyltonc76770c2001-04-13 16:51:46 +00001891 format_exc_check_arg(
1892 PyExc_NameError,
1893 UNBOUNDFREE_ERROR_MSG,
1894 v);
1895 }
Jeremy Hylton2524d692001-02-05 17:23:16 +00001896 break;
1897 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001898 PUSH(w);
1899 break;
1900
1901 case STORE_DEREF:
1902 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001903 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001904 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00001905 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001906 continue;
1907
Guido van Rossum374a9221991-04-04 10:40:29 +00001908 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001909 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001910 if (x != NULL) {
1911 for (; --oparg >= 0;) {
1912 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001913 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001914 }
1915 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001916 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001917 }
1918 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001919
Guido van Rossum374a9221991-04-04 10:40:29 +00001920 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00001921 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001922 if (x != NULL) {
1923 for (; --oparg >= 0;) {
1924 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00001925 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001926 }
1927 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001928 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001929 }
1930 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001931
Guido van Rossum374a9221991-04-04 10:40:29 +00001932 case BUILD_MAP:
Guido van Rossumb209a111997-04-29 18:18:01 +00001933 x = PyDict_New();
Guido van Rossum374a9221991-04-04 10:40:29 +00001934 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001935 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001936 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001937
Guido van Rossum374a9221991-04-04 10:40:29 +00001938 case LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001939 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001940 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001941 x = PyObject_GetAttr(v, w);
1942 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001943 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001944 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001945 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001946
Guido van Rossum374a9221991-04-04 10:40:29 +00001947 case COMPARE_OP:
1948 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001949 v = TOP();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001950 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001951 /* INLINE: cmp(int, int) */
1952 register long a, b;
1953 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001954 a = PyInt_AS_LONG(v);
1955 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001956 switch (oparg) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00001957 case PyCmp_LT: res = a < b; break;
1958 case PyCmp_LE: res = a <= b; break;
1959 case PyCmp_EQ: res = a == b; break;
1960 case PyCmp_NE: res = a != b; break;
1961 case PyCmp_GT: res = a > b; break;
1962 case PyCmp_GE: res = a >= b; break;
1963 case PyCmp_IS: res = v == w; break;
1964 case PyCmp_IS_NOT: res = v != w; break;
Guido van Rossumc12da691997-07-17 23:12:42 +00001965 default: goto slow_compare;
1966 }
1967 x = res ? Py_True : Py_False;
1968 Py_INCREF(x);
1969 }
1970 else {
1971 slow_compare:
1972 x = cmp_outcome(oparg, v, w);
1973 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001974 Py_DECREF(v);
1975 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001976 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001977 if (x == NULL) break;
1978 PREDICT(JUMP_IF_FALSE);
1979 PREDICT(JUMP_IF_TRUE);
1980 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001981
Guido van Rossum374a9221991-04-04 10:40:29 +00001982 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001983 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001984 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001985 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001986 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00001987 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001988 break;
1989 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001990 u = TOP();
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001991 w = PyTuple_Pack(4,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001992 w,
1993 f->f_globals,
Guido van Rossuma027efa1997-05-05 20:56:21 +00001994 f->f_locals == NULL ?
1995 Py_None : f->f_locals,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001996 u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001997 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001998 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001999 u = POP();
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002000 x = NULL;
2001 break;
2002 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002003 x = PyEval_CallObject(x, w);
2004 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002005 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002006 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002007 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002008
Thomas Wouters52152252000-08-17 22:55:00 +00002009 case IMPORT_STAR:
2010 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002011 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002012 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002013 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00002014 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002015 break;
2016 }
Thomas Wouters52152252000-08-17 22:55:00 +00002017 err = import_all_from(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002018 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00002019 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002020 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002021 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002022
Thomas Wouters52152252000-08-17 22:55:00 +00002023 case IMPORT_FROM:
Skip Montanaro496e6582002-08-06 17:47:40 +00002024 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00002025 v = TOP();
2026 x = import_from(v, w);
2027 PUSH(x);
2028 if (x != NULL) continue;
2029 break;
2030
Guido van Rossum374a9221991-04-04 10:40:29 +00002031 case JUMP_FORWARD:
2032 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002033 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002034
Raymond Hettingerf606f872003-03-16 03:11:04 +00002035 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002036 case JUMP_IF_FALSE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002037 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002038 if (w == Py_True) {
2039 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002040 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002041 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002042 if (w == Py_False) {
2043 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002044 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002045 }
2046 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002047 if (err > 0)
2048 err = 0;
2049 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00002050 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002051 else
2052 break;
2053 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002054
Raymond Hettingerf606f872003-03-16 03:11:04 +00002055 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002056 case JUMP_IF_TRUE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002057 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002058 if (w == Py_False) {
2059 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002060 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002061 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002062 if (w == Py_True) {
2063 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002064 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002065 }
2066 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002067 if (err > 0) {
2068 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00002069 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002070 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002071 else if (err == 0)
2072 ;
2073 else
2074 break;
2075 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002076
Guido van Rossum374a9221991-04-04 10:40:29 +00002077 case JUMP_ABSOLUTE:
2078 JUMPTO(oparg);
Neil Schemenauerca2a2f12003-05-30 23:59:44 +00002079 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002080
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002081 case GET_ITER:
2082 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002083 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002084 x = PyObject_GetIter(v);
2085 Py_DECREF(v);
2086 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002087 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002088 PREDICT(FOR_ITER);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002089 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002090 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002091 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002092 break;
2093
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002094 PREDICTED_WITH_ARG(FOR_ITER);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002095 case FOR_ITER:
2096 /* before: [iter]; after: [iter, iter()] *or* [] */
2097 v = TOP();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002098 x = PyIter_Next(v);
2099 if (x != NULL) {
2100 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002101 PREDICT(STORE_FAST);
2102 PREDICT(UNPACK_SEQUENCE);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002103 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002104 }
Tim Petersf4848da2001-05-05 00:14:56 +00002105 if (!PyErr_Occurred()) {
2106 /* iterator ended normally */
2107 x = v = POP();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002108 Py_DECREF(v);
2109 JUMPBY(oparg);
2110 continue;
2111 }
2112 break;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002113
Guido van Rossum374a9221991-04-04 10:40:29 +00002114 case SETUP_LOOP:
2115 case SETUP_EXCEPT:
2116 case SETUP_FINALLY:
Guido van Rossumb209a111997-04-29 18:18:01 +00002117 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002118 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002119 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002120
Guido van Rossumf10570b1995-07-07 22:53:21 +00002121 case CALL_FUNCTION:
Jeremy Hylton985eba52003-02-05 23:13:00 +00002122 PCALL(PCALL_ALL);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002123 x = call_function(&stack_pointer, oparg);
2124 PUSH(x);
2125 if (x != NULL)
2126 continue;
2127 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002128
Jeremy Hylton76901512000-03-28 23:49:17 +00002129 case CALL_FUNCTION_VAR:
2130 case CALL_FUNCTION_KW:
2131 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002132 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002133 int na = oparg & 0xff;
2134 int nk = (oparg>>8) & 0xff;
2135 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002136 int n = na + 2 * nk;
2137 PyObject **pfunc, *func;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002138 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002139 if (flags & CALL_FLAG_VAR)
2140 n++;
2141 if (flags & CALL_FLAG_KW)
2142 n++;
2143 pfunc = stack_pointer - n - 1;
2144 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002145
Guido van Rossumac7be682001-01-17 15:42:30 +00002146 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002147 && PyMethod_GET_SELF(func) != NULL) {
2148 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002149 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002150 func = PyMethod_GET_FUNCTION(func);
2151 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002152 Py_DECREF(*pfunc);
2153 *pfunc = self;
2154 na++;
2155 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002156 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002157 Py_INCREF(func);
2158 x = ext_do_call(func, &stack_pointer, flags, na, nk);
Jeremy Hylton76901512000-03-28 23:49:17 +00002159 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002160
Jeremy Hylton76901512000-03-28 23:49:17 +00002161 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002162 w = POP();
2163 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002164 }
2165 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002166 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002167 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002168 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002169 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002170
Guido van Rossum681d79a1995-07-18 14:51:37 +00002171 case MAKE_FUNCTION:
2172 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002173 x = PyFunction_New(v, f->f_globals);
2174 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002175 /* XXX Maybe this should be a separate opcode? */
2176 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002177 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002178 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002179 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002180 x = NULL;
2181 break;
2182 }
2183 while (--oparg >= 0) {
2184 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002185 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002186 }
2187 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002188 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002189 }
2190 PUSH(x);
2191 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002192
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002193 case MAKE_CLOSURE:
2194 {
2195 int nfree;
2196 v = POP(); /* code object */
2197 x = PyFunction_New(v, f->f_globals);
Jeremy Hylton733c8932001-12-13 19:51:56 +00002198 nfree = PyCode_GetNumFree((PyCodeObject *)v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002199 Py_DECREF(v);
2200 /* XXX Maybe this should be a separate opcode? */
2201 if (x != NULL && nfree > 0) {
2202 v = PyTuple_New(nfree);
2203 if (v == NULL) {
2204 Py_DECREF(x);
2205 x = NULL;
2206 break;
2207 }
2208 while (--nfree >= 0) {
2209 w = POP();
2210 PyTuple_SET_ITEM(v, nfree, w);
2211 }
2212 err = PyFunction_SetClosure(x, v);
2213 Py_DECREF(v);
2214 }
2215 if (x != NULL && oparg > 0) {
2216 v = PyTuple_New(oparg);
2217 if (v == NULL) {
2218 Py_DECREF(x);
2219 x = NULL;
2220 break;
2221 }
2222 while (--oparg >= 0) {
2223 w = POP();
2224 PyTuple_SET_ITEM(v, oparg, w);
2225 }
2226 err = PyFunction_SetDefaults(x, v);
2227 Py_DECREF(v);
2228 }
2229 PUSH(x);
2230 break;
2231 }
2232
Guido van Rossum8861b741996-07-30 16:49:37 +00002233 case BUILD_SLICE:
2234 if (oparg == 3)
2235 w = POP();
2236 else
2237 w = NULL;
2238 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002239 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002240 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002241 Py_DECREF(u);
2242 Py_DECREF(v);
2243 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002244 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002245 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002246 break;
2247
Fred Drakeef8ace32000-08-24 00:32:09 +00002248 case EXTENDED_ARG:
2249 opcode = NEXTOP();
2250 oparg = oparg<<16 | NEXTARG();
2251 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002252
Guido van Rossum374a9221991-04-04 10:40:29 +00002253 default:
2254 fprintf(stderr,
2255 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002256 PyCode_Addr2Line(f->f_code, f->f_lasti),
2257 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002258 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002259 why = WHY_EXCEPTION;
2260 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002261
2262#ifdef CASE_TOO_BIG
2263 }
2264#endif
2265
Guido van Rossum374a9221991-04-04 10:40:29 +00002266 } /* switch */
2267
2268 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002269
Guido van Rossum374a9221991-04-04 10:40:29 +00002270 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002271
Guido van Rossum374a9221991-04-04 10:40:29 +00002272 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002273 if (err == 0 && x != NULL) {
2274#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002275 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002276 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002277 fprintf(stderr,
2278 "XXX undetected error\n");
2279 else
2280#endif
2281 continue; /* Normal, fast path */
2282 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002283 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002284 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002285 err = 0;
2286 }
2287
Guido van Rossum374a9221991-04-04 10:40:29 +00002288 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002289
Guido van Rossum374a9221991-04-04 10:40:29 +00002290 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002291 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002292 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002293 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002294 why = WHY_EXCEPTION;
2295 }
2296 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002297#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002298 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002299 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002300 if (PyErr_Occurred()) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002301 fprintf(stderr,
2302 "XXX undetected error (why=%d)\n",
2303 why);
2304 why = WHY_EXCEPTION;
2305 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002306 }
2307#endif
2308
2309 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002310
Guido van Rossum374a9221991-04-04 10:40:29 +00002311 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002312 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002313
Fred Drake8f51f542001-10-04 14:48:42 +00002314 if (tstate->c_tracefunc != NULL)
2315 call_exc_trace(tstate->c_tracefunc,
2316 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002317 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002318
Guido van Rossum374a9221991-04-04 10:40:29 +00002319 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002320
Guido van Rossum374a9221991-04-04 10:40:29 +00002321 if (why == WHY_RERAISE)
2322 why = WHY_EXCEPTION;
2323
2324 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002325
Tim Peters5ca576e2001-06-18 22:08:13 +00002326 while (why != WHY_NOT && why != WHY_YIELD && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002327 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002328
2329 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2330 /* For a continue inside a try block,
2331 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002332 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2333 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002334 why = WHY_NOT;
2335 JUMPTO(PyInt_AS_LONG(retval));
2336 Py_DECREF(retval);
2337 break;
2338 }
2339
Guido van Rossum374a9221991-04-04 10:40:29 +00002340 while (STACK_LEVEL() > b->b_level) {
2341 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002342 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002343 }
2344 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2345 why = WHY_NOT;
2346 JUMPTO(b->b_handler);
2347 break;
2348 }
2349 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002350 (b->b_type == SETUP_EXCEPT &&
2351 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002352 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002353 PyObject *exc, *val, *tb;
2354 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002355 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002356 val = Py_None;
2357 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002358 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002359 /* Make the raw exception data
2360 available to the handler,
2361 so a program can emulate the
2362 Python main loop. Don't do
2363 this for 'finally'. */
2364 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002365 PyErr_NormalizeException(
2366 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002367 set_exc_info(tstate,
2368 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002369 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002370 if (tb == NULL) {
2371 Py_INCREF(Py_None);
2372 PUSH(Py_None);
2373 } else
2374 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002375 PUSH(val);
2376 PUSH(exc);
2377 }
2378 else {
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002379 if (why == WHY_RETURN ||
Guido van Rossumc5fe5eb2002-06-12 03:45:21 +00002380 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00002381 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002382 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002383 PUSH(v);
2384 }
2385 why = WHY_NOT;
2386 JUMPTO(b->b_handler);
2387 break;
2388 }
2389 } /* unwind stack */
2390
2391 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002392
Guido van Rossum374a9221991-04-04 10:40:29 +00002393 if (why != WHY_NOT)
2394 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002395
Guido van Rossum374a9221991-04-04 10:40:29 +00002396 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002397
Guido van Rossum35974fb2001-12-06 21:28:18 +00002398 if (why != WHY_YIELD) {
2399 /* Pop remaining stack entries -- but when yielding */
2400 while (!EMPTY()) {
2401 v = POP();
2402 Py_XDECREF(v);
2403 }
2404 }
2405
Tim Peters5ca576e2001-06-18 22:08:13 +00002406 if (why != WHY_RETURN && why != WHY_YIELD)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002407 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002408
Fred Drake9e3ad782001-07-03 23:39:52 +00002409 if (tstate->use_tracing) {
2410 if (tstate->c_tracefunc
2411 && (why == WHY_RETURN || why == WHY_YIELD)) {
2412 if (call_trace(tstate->c_tracefunc,
2413 tstate->c_traceobj, f,
2414 PyTrace_RETURN, retval)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002415 Py_XDECREF(retval);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002416 retval = NULL;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002417 why = WHY_EXCEPTION;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002418 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002419 }
Fred Drake8f51f542001-10-04 14:48:42 +00002420 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002421 if (why == WHY_EXCEPTION)
2422 call_trace_protected(tstate->c_profilefunc,
2423 tstate->c_profileobj, f,
2424 PyTrace_RETURN);
2425 else if (call_trace(tstate->c_profilefunc,
2426 tstate->c_profileobj, f,
2427 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002428 Py_XDECREF(retval);
2429 retval = NULL;
2430 why = WHY_EXCEPTION;
2431 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002432 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002433 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002434
Guido van Rossuma027efa1997-05-05 20:56:21 +00002435 reset_exc_info(tstate);
2436
Tim Peters5ca576e2001-06-18 22:08:13 +00002437 /* pop frame */
Armin Rigo092381a2003-10-25 14:29:27 +00002438 exit_eval_frame:
2439 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002440 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002441
Guido van Rossum96a42c81992-01-12 02:29:51 +00002442 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002443}
2444
Tim Peters6d6c1a32001-08-02 04:15:00 +00002445PyObject *
2446PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002447 PyObject **args, int argcount, PyObject **kws, int kwcount,
2448 PyObject **defs, int defcount, PyObject *closure)
2449{
2450 register PyFrameObject *f;
2451 register PyObject *retval = NULL;
2452 register PyObject **fastlocals, **freevars;
2453 PyThreadState *tstate = PyThreadState_GET();
2454 PyObject *x, *u;
2455
2456 if (globals == NULL) {
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002457 PyErr_SetString(PyExc_SystemError,
2458 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002459 return NULL;
2460 }
2461
Jeremy Hylton985eba52003-02-05 23:13:00 +00002462 assert(globals != NULL);
2463 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002464 if (f == NULL)
2465 return NULL;
2466
2467 fastlocals = f->f_localsplus;
2468 freevars = f->f_localsplus + f->f_nlocals;
2469
2470 if (co->co_argcount > 0 ||
2471 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2472 int i;
2473 int n = argcount;
2474 PyObject *kwdict = NULL;
2475 if (co->co_flags & CO_VARKEYWORDS) {
2476 kwdict = PyDict_New();
2477 if (kwdict == NULL)
2478 goto fail;
2479 i = co->co_argcount;
2480 if (co->co_flags & CO_VARARGS)
2481 i++;
2482 SETLOCAL(i, kwdict);
2483 }
2484 if (argcount > co->co_argcount) {
2485 if (!(co->co_flags & CO_VARARGS)) {
2486 PyErr_Format(PyExc_TypeError,
2487 "%.200s() takes %s %d "
2488 "%sargument%s (%d given)",
2489 PyString_AsString(co->co_name),
2490 defcount ? "at most" : "exactly",
2491 co->co_argcount,
2492 kwcount ? "non-keyword " : "",
2493 co->co_argcount == 1 ? "" : "s",
2494 argcount);
2495 goto fail;
2496 }
2497 n = co->co_argcount;
2498 }
2499 for (i = 0; i < n; i++) {
2500 x = args[i];
2501 Py_INCREF(x);
2502 SETLOCAL(i, x);
2503 }
2504 if (co->co_flags & CO_VARARGS) {
2505 u = PyTuple_New(argcount - n);
2506 if (u == NULL)
2507 goto fail;
2508 SETLOCAL(co->co_argcount, u);
2509 for (i = n; i < argcount; i++) {
2510 x = args[i];
2511 Py_INCREF(x);
2512 PyTuple_SET_ITEM(u, i-n, x);
2513 }
2514 }
2515 for (i = 0; i < kwcount; i++) {
2516 PyObject *keyword = kws[2*i];
2517 PyObject *value = kws[2*i + 1];
2518 int j;
2519 if (keyword == NULL || !PyString_Check(keyword)) {
2520 PyErr_Format(PyExc_TypeError,
2521 "%.200s() keywords must be strings",
2522 PyString_AsString(co->co_name));
2523 goto fail;
2524 }
2525 /* XXX slow -- speed up using dictionary? */
2526 for (j = 0; j < co->co_argcount; j++) {
2527 PyObject *nm = PyTuple_GET_ITEM(
2528 co->co_varnames, j);
2529 int cmp = PyObject_RichCompareBool(
2530 keyword, nm, Py_EQ);
2531 if (cmp > 0)
2532 break;
2533 else if (cmp < 0)
2534 goto fail;
2535 }
2536 /* Check errors from Compare */
2537 if (PyErr_Occurred())
2538 goto fail;
2539 if (j >= co->co_argcount) {
2540 if (kwdict == NULL) {
2541 PyErr_Format(PyExc_TypeError,
2542 "%.200s() got an unexpected "
2543 "keyword argument '%.400s'",
2544 PyString_AsString(co->co_name),
2545 PyString_AsString(keyword));
2546 goto fail;
2547 }
2548 PyDict_SetItem(kwdict, keyword, value);
2549 }
2550 else {
2551 if (GETLOCAL(j) != NULL) {
2552 PyErr_Format(PyExc_TypeError,
2553 "%.200s() got multiple "
2554 "values for keyword "
2555 "argument '%.400s'",
2556 PyString_AsString(co->co_name),
2557 PyString_AsString(keyword));
2558 goto fail;
2559 }
2560 Py_INCREF(value);
2561 SETLOCAL(j, value);
2562 }
2563 }
2564 if (argcount < co->co_argcount) {
2565 int m = co->co_argcount - defcount;
2566 for (i = argcount; i < m; i++) {
2567 if (GETLOCAL(i) == NULL) {
2568 PyErr_Format(PyExc_TypeError,
2569 "%.200s() takes %s %d "
2570 "%sargument%s (%d given)",
2571 PyString_AsString(co->co_name),
2572 ((co->co_flags & CO_VARARGS) ||
2573 defcount) ? "at least"
2574 : "exactly",
2575 m, kwcount ? "non-keyword " : "",
2576 m == 1 ? "" : "s", i);
2577 goto fail;
2578 }
2579 }
2580 if (n > m)
2581 i = n - m;
2582 else
2583 i = 0;
2584 for (; i < defcount; i++) {
2585 if (GETLOCAL(m+i) == NULL) {
2586 PyObject *def = defs[i];
2587 Py_INCREF(def);
2588 SETLOCAL(m+i, def);
2589 }
2590 }
2591 }
2592 }
2593 else {
2594 if (argcount > 0 || kwcount > 0) {
2595 PyErr_Format(PyExc_TypeError,
2596 "%.200s() takes no arguments (%d given)",
2597 PyString_AsString(co->co_name),
2598 argcount + kwcount);
2599 goto fail;
2600 }
2601 }
2602 /* Allocate and initialize storage for cell vars, and copy free
2603 vars into frame. This isn't too efficient right now. */
2604 if (f->f_ncells) {
2605 int i = 0, j = 0, nargs, found;
2606 char *cellname, *argname;
2607 PyObject *c;
2608
2609 nargs = co->co_argcount;
2610 if (co->co_flags & CO_VARARGS)
2611 nargs++;
2612 if (co->co_flags & CO_VARKEYWORDS)
2613 nargs++;
2614
2615 /* Check for cells that shadow args */
2616 for (i = 0; i < f->f_ncells && j < nargs; ++i) {
2617 cellname = PyString_AS_STRING(
2618 PyTuple_GET_ITEM(co->co_cellvars, i));
2619 found = 0;
2620 while (j < nargs) {
2621 argname = PyString_AS_STRING(
2622 PyTuple_GET_ITEM(co->co_varnames, j));
2623 if (strcmp(cellname, argname) == 0) {
2624 c = PyCell_New(GETLOCAL(j));
2625 if (c == NULL)
2626 goto fail;
2627 GETLOCAL(f->f_nlocals + i) = c;
2628 found = 1;
2629 break;
2630 }
2631 j++;
2632 }
2633 if (found == 0) {
2634 c = PyCell_New(NULL);
2635 if (c == NULL)
2636 goto fail;
2637 SETLOCAL(f->f_nlocals + i, c);
2638 }
2639 }
2640 /* Initialize any that are left */
2641 while (i < f->f_ncells) {
2642 c = PyCell_New(NULL);
2643 if (c == NULL)
2644 goto fail;
2645 SETLOCAL(f->f_nlocals + i, c);
2646 i++;
2647 }
2648 }
2649 if (f->f_nfreevars) {
2650 int i;
2651 for (i = 0; i < f->f_nfreevars; ++i) {
2652 PyObject *o = PyTuple_GET_ITEM(closure, i);
2653 Py_INCREF(o);
2654 freevars[f->f_ncells + i] = o;
2655 }
2656 }
2657
Tim Peters5ca576e2001-06-18 22:08:13 +00002658 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002659 /* Don't need to keep the reference to f_back, it will be set
2660 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002661 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002662 f->f_back = NULL;
2663
Jeremy Hylton985eba52003-02-05 23:13:00 +00002664 PCALL(PCALL_GENERATOR);
2665
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002666 /* Create a new generator that owns the ready to run frame
2667 * and return that as the value. */
Tim Peters5ca576e2001-06-18 22:08:13 +00002668 return gen_new(f);
2669 }
2670
2671 retval = eval_frame(f);
2672
2673 fail: /* Jump here from prelude on failure */
2674
Tim Petersb13680b2001-11-27 23:29:29 +00002675 /* decref'ing the frame can cause __del__ methods to get invoked,
2676 which can call back into Python. While we're done with the
2677 current Python frame (f), the associated C stack is still in use,
2678 so recursion_depth must be boosted for the duration.
2679 */
2680 assert(tstate != NULL);
2681 ++tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002682 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00002683 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002684 return retval;
2685}
2686
2687
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002688/* Implementation notes for set_exc_info() and reset_exc_info():
2689
2690- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2691 'exc_traceback'. These always travel together.
2692
2693- tstate->curexc_ZZZ is the "hot" exception that is set by
2694 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2695
2696- Once an exception is caught by an except clause, it is transferred
2697 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2698 can pick it up. This is the primary task of set_exc_info().
2699
2700- Now let me explain the complicated dance with frame->f_exc_ZZZ.
2701
2702 Long ago, when none of this existed, there were just a few globals:
2703 one set corresponding to the "hot" exception, and one set
2704 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2705 globals; they were simply stored as sys.exc_ZZZ. For backwards
2706 compatibility, they still are!) The problem was that in code like
2707 this:
2708
2709 try:
2710 "something that may fail"
2711 except "some exception":
2712 "do something else first"
2713 "print the exception from sys.exc_ZZZ."
2714
2715 if "do something else first" invoked something that raised and caught
2716 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2717 cause of subtle bugs. I fixed this by changing the semantics as
2718 follows:
2719
2720 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2721 *in that frame*.
2722
2723 - But initially, and as long as no exception is caught in a given
2724 frame, sys.exc_ZZZ will hold the last exception caught in the
2725 previous frame (or the frame before that, etc.).
2726
2727 The first bullet fixed the bug in the above example. The second
2728 bullet was for backwards compatibility: it was (and is) common to
2729 have a function that is called when an exception is caught, and to
2730 have that function access the caught exception via sys.exc_ZZZ.
2731 (Example: traceback.print_exc()).
2732
2733 At the same time I fixed the problem that sys.exc_ZZZ weren't
2734 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2735 but that's really a separate improvement.
2736
2737 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2738 variables to what they were before the current frame was called. The
2739 set_exc_info() function saves them on the frame so that
2740 reset_exc_info() can restore them. The invariant is that
2741 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2742 exception (where "catching" an exception applies only to successful
2743 except clauses); and if the current frame ever caught an exception,
2744 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2745 at the start of the current frame.
2746
2747*/
2748
Guido van Rossuma027efa1997-05-05 20:56:21 +00002749static void
Guido van Rossumac7be682001-01-17 15:42:30 +00002750set_exc_info(PyThreadState *tstate,
2751 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002752{
2753 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002754 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00002755
Guido van Rossuma027efa1997-05-05 20:56:21 +00002756 frame = tstate->frame;
2757 if (frame->f_exc_type == NULL) {
2758 /* This frame didn't catch an exception before */
2759 /* Save previous exception of this thread in this frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002760 if (tstate->exc_type == NULL) {
2761 Py_INCREF(Py_None);
2762 tstate->exc_type = Py_None;
2763 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002764 tmp_type = frame->f_exc_type;
2765 tmp_value = frame->f_exc_value;
2766 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002767 Py_XINCREF(tstate->exc_type);
2768 Py_XINCREF(tstate->exc_value);
2769 Py_XINCREF(tstate->exc_traceback);
2770 frame->f_exc_type = tstate->exc_type;
2771 frame->f_exc_value = tstate->exc_value;
2772 frame->f_exc_traceback = tstate->exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002773 Py_XDECREF(tmp_type);
2774 Py_XDECREF(tmp_value);
2775 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002776 }
2777 /* Set new exception for this thread */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002778 tmp_type = tstate->exc_type;
2779 tmp_value = tstate->exc_value;
2780 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002781 Py_XINCREF(type);
2782 Py_XINCREF(value);
2783 Py_XINCREF(tb);
2784 tstate->exc_type = type;
2785 tstate->exc_value = value;
2786 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002787 Py_XDECREF(tmp_type);
2788 Py_XDECREF(tmp_value);
2789 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002790 /* For b/w compatibility */
2791 PySys_SetObject("exc_type", type);
2792 PySys_SetObject("exc_value", value);
2793 PySys_SetObject("exc_traceback", tb);
2794}
2795
2796static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002797reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002798{
2799 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002800 PyObject *tmp_type, *tmp_value, *tmp_tb;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002801 frame = tstate->frame;
2802 if (frame->f_exc_type != NULL) {
2803 /* This frame caught an exception */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002804 tmp_type = tstate->exc_type;
2805 tmp_value = tstate->exc_value;
2806 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002807 Py_XINCREF(frame->f_exc_type);
2808 Py_XINCREF(frame->f_exc_value);
2809 Py_XINCREF(frame->f_exc_traceback);
2810 tstate->exc_type = frame->f_exc_type;
2811 tstate->exc_value = frame->f_exc_value;
2812 tstate->exc_traceback = frame->f_exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002813 Py_XDECREF(tmp_type);
2814 Py_XDECREF(tmp_value);
2815 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002816 /* For b/w compatibility */
2817 PySys_SetObject("exc_type", frame->f_exc_type);
2818 PySys_SetObject("exc_value", frame->f_exc_value);
2819 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
2820 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002821 tmp_type = frame->f_exc_type;
2822 tmp_value = frame->f_exc_value;
2823 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002824 frame->f_exc_type = NULL;
2825 frame->f_exc_value = NULL;
2826 frame->f_exc_traceback = NULL;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002827 Py_XDECREF(tmp_type);
2828 Py_XDECREF(tmp_value);
2829 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002830}
2831
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002832/* Logic for the raise statement (too complicated for inlining).
2833 This *consumes* a reference count to each of its arguments. */
Guido van Rossum1aa14831997-01-21 05:34:20 +00002834static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002835do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002836{
Guido van Rossumd295f121998-04-09 21:39:57 +00002837 if (type == NULL) {
2838 /* Reraise */
2839 PyThreadState *tstate = PyThreadState_Get();
2840 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
2841 value = tstate->exc_value;
2842 tb = tstate->exc_traceback;
2843 Py_XINCREF(type);
2844 Py_XINCREF(value);
2845 Py_XINCREF(tb);
2846 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002847
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002848 /* We support the following forms of raise:
2849 raise <class>, <classinstance>
2850 raise <class>, <argument tuple>
2851 raise <class>, None
2852 raise <class>, <argument>
2853 raise <classinstance>, None
2854 raise <string>, <object>
2855 raise <string>, None
2856
2857 An omitted second argument is the same as None.
2858
2859 In addition, raise <tuple>, <anything> is the same as
2860 raising the tuple's first item (and it better have one!);
2861 this rule is applied recursively.
2862
2863 Finally, an optional third argument can be supplied, which
2864 gives the traceback to be substituted (useful when
2865 re-raising an exception after examining it). */
2866
2867 /* First, check the traceback argument, replacing None with
2868 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002869 if (tb == Py_None) {
2870 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002871 tb = NULL;
2872 }
2873 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002874 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002875 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002876 goto raise_error;
2877 }
2878
2879 /* Next, replace a missing value with None */
2880 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002881 value = Py_None;
2882 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002883 }
2884
2885 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00002886 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
2887 PyObject *tmp = type;
2888 type = PyTuple_GET_ITEM(type, 0);
2889 Py_INCREF(type);
2890 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002891 }
2892
Tim Petersafb2c802002-04-18 18:06:20 +00002893 if (PyString_CheckExact(type))
2894 /* Raising builtin string is deprecated but still allowed --
2895 * do nothing. Raising an instance of a new-style str
2896 * subclass is right out. */
Neal Norwitz37aa0662003-01-10 15:31:15 +00002897 PyErr_Warn(PyExc_PendingDeprecationWarning,
2898 "raising a string exception is deprecated");
Barry Warsaw4249f541997-08-22 21:26:19 +00002899
2900 else if (PyClass_Check(type))
2901 PyErr_NormalizeException(&type, &value, &tb);
2902
Guido van Rossumb209a111997-04-29 18:18:01 +00002903 else if (PyInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002904 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002905 if (value != Py_None) {
2906 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002907 "instance exception may not have a separate value");
2908 goto raise_error;
2909 }
2910 else {
2911 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00002912 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002913 value = type;
Guido van Rossumb209a111997-04-29 18:18:01 +00002914 type = (PyObject*) ((PyInstanceObject*)type)->in_class;
2915 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002916 }
2917 }
2918 else {
2919 /* Not something you can raise. You get an exception
2920 anyway, just not what you specified :-) */
Jeremy Hylton960d9482001-04-27 02:25:33 +00002921 PyErr_Format(PyExc_TypeError,
Neal Norwitz37aa0662003-01-10 15:31:15 +00002922 "exceptions must be classes, instances, or "
2923 "strings (deprecated), not %s",
2924 type->ob_type->tp_name);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002925 goto raise_error;
2926 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002927 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002928 if (tb == NULL)
2929 return WHY_EXCEPTION;
2930 else
2931 return WHY_RERAISE;
2932 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00002933 Py_XDECREF(value);
2934 Py_XDECREF(type);
2935 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002936 return WHY_EXCEPTION;
2937}
2938
Tim Petersd6d010b2001-06-21 02:49:55 +00002939/* Iterate v argcnt times and store the results on the stack (via decreasing
2940 sp). Return 1 for success, 0 if error. */
2941
Barry Warsawe42b18f1997-08-25 22:13:04 +00002942static int
Tim Petersd6d010b2001-06-21 02:49:55 +00002943unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00002944{
Tim Petersd6d010b2001-06-21 02:49:55 +00002945 int i = 0;
2946 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00002947 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00002948
Tim Petersd6d010b2001-06-21 02:49:55 +00002949 assert(v != NULL);
2950
2951 it = PyObject_GetIter(v);
2952 if (it == NULL)
2953 goto Error;
2954
2955 for (; i < argcnt; i++) {
2956 w = PyIter_Next(it);
2957 if (w == NULL) {
2958 /* Iterator done, via error or exhaustion. */
2959 if (!PyErr_Occurred()) {
2960 PyErr_Format(PyExc_ValueError,
2961 "need more than %d value%s to unpack",
2962 i, i == 1 ? "" : "s");
2963 }
2964 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00002965 }
2966 *--sp = w;
2967 }
Tim Petersd6d010b2001-06-21 02:49:55 +00002968
2969 /* We better have exhausted the iterator now. */
2970 w = PyIter_Next(it);
2971 if (w == NULL) {
2972 if (PyErr_Occurred())
2973 goto Error;
2974 Py_DECREF(it);
2975 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00002976 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00002977 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00002978 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00002979 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00002980Error:
Barry Warsaw91010551997-08-25 22:30:51 +00002981 for (; i > 0; i--, sp++)
2982 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00002983 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00002984 return 0;
2985}
2986
2987
Guido van Rossum96a42c81992-01-12 02:29:51 +00002988#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00002989static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002990prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002991{
Guido van Rossum3f5da241990-12-20 15:06:42 +00002992 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00002993 if (PyObject_Print(v, stdout, 0) != 0)
2994 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00002995 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00002996 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002997}
Guido van Rossum3f5da241990-12-20 15:06:42 +00002998#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002999
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003000static void
Fred Drake5755ce62001-06-27 19:19:46 +00003001call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003002{
Guido van Rossumb209a111997-04-29 18:18:01 +00003003 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003004 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003005 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003006 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003007 value = Py_None;
3008 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003009 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003010 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003011 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003012 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003013 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003014 }
Fred Drake5755ce62001-06-27 19:19:46 +00003015 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003016 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003017 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003018 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003019 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003020 Py_XDECREF(type);
3021 Py_XDECREF(value);
3022 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003023 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003024}
3025
Fred Drake4ec5d562001-10-04 19:26:43 +00003026static void
3027call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3028 int what)
3029{
3030 PyObject *type, *value, *traceback;
3031 int err;
3032 PyErr_Fetch(&type, &value, &traceback);
3033 err = call_trace(func, obj, frame, what, NULL);
3034 if (err == 0)
3035 PyErr_Restore(type, value, traceback);
3036 else {
3037 Py_XDECREF(type);
3038 Py_XDECREF(value);
3039 Py_XDECREF(traceback);
3040 }
3041}
3042
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003043static int
Fred Drake5755ce62001-06-27 19:19:46 +00003044call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3045 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003046{
Fred Drake5755ce62001-06-27 19:19:46 +00003047 register PyThreadState *tstate = frame->f_tstate;
3048 int result;
3049 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003050 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003051 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003052 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003053 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003054 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3055 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003056 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003057 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003058}
3059
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003060PyObject *
3061_PyEval_CallTracing(PyObject *func, PyObject *args)
3062{
3063 PyFrameObject *frame = PyEval_GetFrame();
3064 PyThreadState *tstate = frame->f_tstate;
3065 int save_tracing = tstate->tracing;
3066 int save_use_tracing = tstate->use_tracing;
3067 PyObject *result;
3068
3069 tstate->tracing = 0;
3070 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3071 || (tstate->c_profilefunc != NULL));
3072 result = PyObject_Call(func, args, NULL);
3073 tstate->tracing = save_tracing;
3074 tstate->use_tracing = save_use_tracing;
3075 return result;
3076}
3077
Michael W. Hudson006c7522002-11-08 13:08:46 +00003078static int
Michael W. Hudson019a78e2002-11-08 12:53:11 +00003079maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003080 PyFrameObject *frame, int *instr_lb, int *instr_ub)
3081{
3082 /* The theory of SET_LINENO-less tracing.
3083
3084 In a nutshell, we use the co_lnotab field of the code object
3085 to tell when execution has moved onto a different line.
3086
3087 As mentioned above, the basic idea is so set things up so
3088 that
3089
3090 *instr_lb <= frame->f_lasti < *instr_ub
3091
3092 is true so long as execution does not change lines.
3093
3094 This is all fairly simple. Digging the information out of
3095 co_lnotab takes some work, but is conceptually clear.
3096
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003097 Somewhat harder to explain is why we don't *always* call the
3098 line trace function when the above test fails.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003099
3100 Consider this code:
3101
3102 1: def f(a):
3103 2: if a:
3104 3: print 1
3105 4: else:
3106 5: print 2
3107
3108 which compiles to this:
3109
3110 2 0 LOAD_FAST 0 (a)
3111 3 JUMP_IF_FALSE 9 (to 15)
3112 6 POP_TOP
3113
3114 3 7 LOAD_CONST 1 (1)
3115 10 PRINT_ITEM
3116 11 PRINT_NEWLINE
3117 12 JUMP_FORWARD 6 (to 21)
3118 >> 15 POP_TOP
3119
3120 5 16 LOAD_CONST 2 (2)
3121 19 PRINT_ITEM
3122 20 PRINT_NEWLINE
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003123 >> 21 LOAD_CONST 0 (None)
3124 24 RETURN_VALUE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003125
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003126 If 'a' is false, execution will jump to instruction at offset
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003127 15 and the co_lnotab will claim that execution has moved to
3128 line 3. This is at best misleading. In this case we could
3129 associate the POP_TOP with line 4, but that doesn't make
3130 sense in all cases (I think).
3131
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003132 What we do is only call the line trace function if the co_lnotab
3133 indicates we have jumped to the *start* of a line, i.e. if the
3134 current instruction offset matches the offset given for the
3135 start of a line by the co_lnotab.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003136
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003137 This also takes care of the situation where 'a' is true.
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003138 Execution will jump from instruction offset 12 to offset 21.
3139 Then the co_lnotab would imply that execution has moved to line
3140 5, which is again misleading.
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003141
3142 Why do we set f_lineno when tracing? Well, consider the code
3143 above when 'a' is true. If stepping through this with 'n' in
3144 pdb, you would stop at line 1 with a "call" type event, then
3145 line events on lines 2 and 3, then a "return" type event -- but
3146 you would be shown line 5 during this event. This is a change
3147 from the behaviour in 2.2 and before, and I've found it
3148 confusing in practice. By setting and using f_lineno when
3149 tracing, one can report a line number different from that
3150 suggested by f_lasti on this one occasion where it's desirable.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003151 */
3152
Michael W. Hudson006c7522002-11-08 13:08:46 +00003153 int result = 0;
3154
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003155 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003156 PyCodeObject* co = frame->f_code;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003157 int size, addr, line;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003158 unsigned char* p;
3159
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003160 size = PyString_GET_SIZE(co->co_lnotab) / 2;
3161 p = (unsigned char*)PyString_AS_STRING(co->co_lnotab);
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003162
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003163 addr = 0;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003164 line = co->co_firstlineno;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003165
3166 /* possible optimization: if f->f_lasti == instr_ub
3167 (likely to be a common case) then we already know
3168 instr_lb -- if we stored the matching value of p
3169 somwhere we could skip the first while loop. */
3170
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003171 /* see comments in compile.c for the description of
3172 co_lnotab. A point to remember: increments to p
3173 should come in pairs -- although we don't care about
3174 the line increments here, treating them as byte
3175 increments gets confusing, to say the least. */
3176
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003177 while (size > 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003178 if (addr + *p > frame->f_lasti)
3179 break;
3180 addr += *p++;
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003181 if (*p) *instr_lb = addr;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003182 line += *p++;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003183 --size;
3184 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003185
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003186 if (addr == frame->f_lasti) {
3187 frame->f_lineno = line;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003188 result = call_trace(func, obj, frame,
3189 PyTrace_LINE, Py_None);
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003190 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003191
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003192 if (size > 0) {
3193 while (--size >= 0) {
3194 addr += *p++;
3195 if (*p++)
3196 break;
3197 }
3198 *instr_ub = addr;
3199 }
3200 else {
3201 *instr_ub = INT_MAX;
3202 }
3203 }
Michael W. Hudson006c7522002-11-08 13:08:46 +00003204
3205 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003206}
3207
Fred Drake5755ce62001-06-27 19:19:46 +00003208void
3209PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003210{
Fred Drake5755ce62001-06-27 19:19:46 +00003211 PyThreadState *tstate = PyThreadState_Get();
3212 PyObject *temp = tstate->c_profileobj;
3213 Py_XINCREF(arg);
3214 tstate->c_profilefunc = NULL;
3215 tstate->c_profileobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003216 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003217 Py_XDECREF(temp);
3218 tstate->c_profilefunc = func;
3219 tstate->c_profileobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003220 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003221}
3222
3223void
3224PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3225{
3226 PyThreadState *tstate = PyThreadState_Get();
3227 PyObject *temp = tstate->c_traceobj;
3228 Py_XINCREF(arg);
3229 tstate->c_tracefunc = NULL;
3230 tstate->c_traceobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003231 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003232 Py_XDECREF(temp);
3233 tstate->c_tracefunc = func;
3234 tstate->c_traceobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003235 tstate->use_tracing = ((func != NULL)
3236 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003237}
3238
Guido van Rossumb209a111997-04-29 18:18:01 +00003239PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003240PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003241{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003242 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003243 if (current_frame == NULL)
Michael W. Hudson019a78e2002-11-08 12:53:11 +00003244 return PyThreadState_Get()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003245 else
3246 return current_frame->f_builtins;
3247}
3248
Guido van Rossumb209a111997-04-29 18:18:01 +00003249PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003250PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003251{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003252 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003253 if (current_frame == NULL)
3254 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003255 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003256 return current_frame->f_locals;
3257}
3258
Guido van Rossumb209a111997-04-29 18:18:01 +00003259PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003260PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003261{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003262 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003263 if (current_frame == NULL)
3264 return NULL;
3265 else
3266 return current_frame->f_globals;
3267}
3268
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003269PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003270PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003271{
Michael W. Hudson019a78e2002-11-08 12:53:11 +00003272 PyThreadState *tstate = PyThreadState_Get();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003273 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003274}
3275
Guido van Rossum6135a871995-01-09 17:53:26 +00003276int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003277PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003278{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003279 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003280 return current_frame == NULL ? 0 : current_frame->f_restricted;
3281}
3282
Guido van Rossumbe270261997-05-22 22:26:18 +00003283int
Tim Peters5ba58662001-07-16 02:29:45 +00003284PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003285{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003286 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003287 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003288
3289 if (current_frame != NULL) {
3290 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003291 const int compilerflags = codeflags & PyCF_MASK;
3292 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003293 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003294 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003295 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003296#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003297 if (codeflags & CO_GENERATOR_ALLOWED) {
3298 result = 1;
3299 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3300 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003301#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003302 }
3303 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003304}
3305
3306int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003307Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003308{
Guido van Rossumb209a111997-04-29 18:18:01 +00003309 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003310 if (f == NULL)
3311 return 0;
3312 if (!PyFile_SoftSpace(f, 0))
3313 return 0;
3314 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003315}
3316
Guido van Rossum3f5da241990-12-20 15:06:42 +00003317
Guido van Rossum681d79a1995-07-18 14:51:37 +00003318/* External interface to call any callable object.
3319 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003320
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003321#undef PyEval_CallObject
3322/* for backward compatibility: export this interface */
3323
Guido van Rossumb209a111997-04-29 18:18:01 +00003324PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003325PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003326{
Guido van Rossumb209a111997-04-29 18:18:01 +00003327 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003328}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003329#define PyEval_CallObject(func,arg) \
3330 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003331
Guido van Rossumb209a111997-04-29 18:18:01 +00003332PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003333PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003334{
Jeremy Hylton52820442001-01-03 23:52:36 +00003335 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003336
3337 if (arg == NULL)
Guido van Rossumb209a111997-04-29 18:18:01 +00003338 arg = PyTuple_New(0);
3339 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003340 PyErr_SetString(PyExc_TypeError,
3341 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003342 return NULL;
3343 }
3344 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003345 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003346
Guido van Rossumb209a111997-04-29 18:18:01 +00003347 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003348 PyErr_SetString(PyExc_TypeError,
3349 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003350 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003351 return NULL;
3352 }
3353
Tim Peters6d6c1a32001-08-02 04:15:00 +00003354 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003355 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003356 return result;
3357}
3358
Tim Peters6d6c1a32001-08-02 04:15:00 +00003359char *
3360PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003361{
3362 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003363 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003364 else if (PyFunction_Check(func))
3365 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3366 else if (PyCFunction_Check(func))
3367 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3368 else if (PyClass_Check(func))
3369 return PyString_AsString(((PyClassObject*)func)->cl_name);
3370 else if (PyInstance_Check(func)) {
3371 return PyString_AsString(
3372 ((PyInstanceObject*)func)->in_class->cl_name);
3373 } else {
3374 return func->ob_type->tp_name;
3375 }
3376}
3377
Tim Peters6d6c1a32001-08-02 04:15:00 +00003378char *
3379PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003380{
3381 if (PyMethod_Check(func))
3382 return "()";
3383 else if (PyFunction_Check(func))
3384 return "()";
3385 else if (PyCFunction_Check(func))
3386 return "()";
3387 else if (PyClass_Check(func))
3388 return " constructor";
3389 else if (PyInstance_Check(func)) {
3390 return " instance";
3391 } else {
3392 return " object";
3393 }
3394}
3395
Jeremy Hylton52820442001-01-03 23:52:36 +00003396#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
3397
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003398static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003399err_args(PyObject *func, int flags, int nargs)
3400{
3401 if (flags & METH_NOARGS)
3402 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003403 "%.200s() takes no arguments (%d given)",
Jeremy Hylton192690e2002-08-16 18:36:11 +00003404 ((PyCFunctionObject *)func)->m_ml->ml_name,
3405 nargs);
3406 else
3407 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003408 "%.200s() takes exactly one argument (%d given)",
Jeremy Hylton192690e2002-08-16 18:36:11 +00003409 ((PyCFunctionObject *)func)->m_ml->ml_name,
3410 nargs);
3411}
3412
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003413static PyObject *
3414call_function(PyObject ***pp_stack, int oparg)
3415{
3416 int na = oparg & 0xff;
3417 int nk = (oparg>>8) & 0xff;
3418 int n = na + 2 * nk;
3419 PyObject **pfunc = (*pp_stack) - n - 1;
3420 PyObject *func = *pfunc;
3421 PyObject *x, *w;
3422
Jeremy Hylton985eba52003-02-05 23:13:00 +00003423 /* Always dispatch PyCFunction first, because these are
3424 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003425 */
3426 if (PyCFunction_Check(func) && nk == 0) {
3427 int flags = PyCFunction_GET_FLAGS(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003428 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003429 if (flags & (METH_NOARGS | METH_O)) {
3430 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3431 PyObject *self = PyCFunction_GET_SELF(func);
3432 if (flags & METH_NOARGS && na == 0)
3433 x = (*meth)(self, NULL);
3434 else if (flags & METH_O && na == 1) {
3435 PyObject *arg = EXT_POP(*pp_stack);
3436 x = (*meth)(self, arg);
3437 Py_DECREF(arg);
3438 }
3439 else {
3440 err_args(func, flags, na);
3441 x = NULL;
3442 }
3443 }
3444 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003445 PyObject *callargs;
3446 callargs = load_args(pp_stack, na);
3447 x = PyCFunction_Call(func, callargs, NULL);
3448 Py_XDECREF(callargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003449 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003450 } else {
3451 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3452 /* optimize access to bound methods */
3453 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003454 PCALL(PCALL_METHOD);
3455 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003456 Py_INCREF(self);
3457 func = PyMethod_GET_FUNCTION(func);
3458 Py_INCREF(func);
3459 Py_DECREF(*pfunc);
3460 *pfunc = self;
3461 na++;
3462 n++;
3463 } else
3464 Py_INCREF(func);
3465 if (PyFunction_Check(func))
3466 x = fast_function(func, pp_stack, n, na, nk);
3467 else
3468 x = do_call(func, pp_stack, na, nk);
3469 Py_DECREF(func);
3470 }
3471
Jeremy Hylton985eba52003-02-05 23:13:00 +00003472 /* What does this do? */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003473 while ((*pp_stack) > pfunc) {
3474 w = EXT_POP(*pp_stack);
3475 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003476 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003477 }
3478 return x;
3479}
3480
Jeremy Hylton192690e2002-08-16 18:36:11 +00003481/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003482 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003483 For the simplest case -- a function that takes only positional
3484 arguments and is called with only positional arguments -- it
3485 inlines the most primitive frame setup code from
3486 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3487 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003488*/
3489
3490static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003491fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003492{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003493 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003494 PyObject *globals = PyFunction_GET_GLOBALS(func);
3495 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3496 PyObject **d = NULL;
3497 int nd = 0;
3498
Jeremy Hylton985eba52003-02-05 23:13:00 +00003499 PCALL(PCALL_FUNCTION);
3500 PCALL(PCALL_FAST_FUNCTION);
Raymond Hettinger40174c32003-05-31 07:04:16 +00003501 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003502 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3503 PyFrameObject *f;
3504 PyObject *retval = NULL;
3505 PyThreadState *tstate = PyThreadState_GET();
3506 PyObject **fastlocals, **stack;
3507 int i;
3508
3509 PCALL(PCALL_FASTER_FUNCTION);
3510 assert(globals != NULL);
3511 /* XXX Perhaps we should create a specialized
3512 PyFrame_New() that doesn't take locals, but does
3513 take builtins without sanity checking them.
3514 */
3515 f = PyFrame_New(tstate, co, globals, NULL);
3516 if (f == NULL)
3517 return NULL;
3518
3519 fastlocals = f->f_localsplus;
3520 stack = (*pp_stack) - n;
3521
3522 for (i = 0; i < n; i++) {
3523 Py_INCREF(*stack);
3524 fastlocals[i] = *stack++;
3525 }
3526 retval = eval_frame(f);
3527 assert(tstate != NULL);
3528 ++tstate->recursion_depth;
3529 Py_DECREF(f);
3530 --tstate->recursion_depth;
3531 return retval;
3532 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003533 if (argdefs != NULL) {
3534 d = &PyTuple_GET_ITEM(argdefs, 0);
3535 nd = ((PyTupleObject *)argdefs)->ob_size;
3536 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003537 return PyEval_EvalCodeEx(co, globals,
3538 (PyObject *)NULL, (*pp_stack)-n, na,
3539 (*pp_stack)-2*nk, nk, d, nd,
3540 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003541}
3542
3543static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003544update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3545 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003546{
3547 PyObject *kwdict = NULL;
3548 if (orig_kwdict == NULL)
3549 kwdict = PyDict_New();
3550 else {
3551 kwdict = PyDict_Copy(orig_kwdict);
3552 Py_DECREF(orig_kwdict);
3553 }
3554 if (kwdict == NULL)
3555 return NULL;
3556 while (--nk >= 0) {
3557 int err;
3558 PyObject *value = EXT_POP(*pp_stack);
3559 PyObject *key = EXT_POP(*pp_stack);
3560 if (PyDict_GetItem(kwdict, key) != NULL) {
Guido van Rossumac7be682001-01-17 15:42:30 +00003561 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +00003562 "%.200s%s got multiple values "
Jeremy Hylton512a2372001-04-11 13:52:29 +00003563 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003564 PyEval_GetFuncName(func),
3565 PyEval_GetFuncDesc(func),
Jeremy Hylton512a2372001-04-11 13:52:29 +00003566 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003567 Py_DECREF(key);
3568 Py_DECREF(value);
3569 Py_DECREF(kwdict);
3570 return NULL;
3571 }
3572 err = PyDict_SetItem(kwdict, key, value);
3573 Py_DECREF(key);
3574 Py_DECREF(value);
3575 if (err) {
3576 Py_DECREF(kwdict);
3577 return NULL;
3578 }
3579 }
3580 return kwdict;
3581}
3582
3583static PyObject *
3584update_star_args(int nstack, int nstar, PyObject *stararg,
3585 PyObject ***pp_stack)
3586{
3587 PyObject *callargs, *w;
3588
3589 callargs = PyTuple_New(nstack + nstar);
3590 if (callargs == NULL) {
3591 return NULL;
3592 }
3593 if (nstar) {
3594 int i;
3595 for (i = 0; i < nstar; i++) {
3596 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3597 Py_INCREF(a);
3598 PyTuple_SET_ITEM(callargs, nstack + i, a);
3599 }
3600 }
3601 while (--nstack >= 0) {
3602 w = EXT_POP(*pp_stack);
3603 PyTuple_SET_ITEM(callargs, nstack, w);
3604 }
3605 return callargs;
3606}
3607
3608static PyObject *
3609load_args(PyObject ***pp_stack, int na)
3610{
3611 PyObject *args = PyTuple_New(na);
3612 PyObject *w;
3613
3614 if (args == NULL)
3615 return NULL;
3616 while (--na >= 0) {
3617 w = EXT_POP(*pp_stack);
3618 PyTuple_SET_ITEM(args, na, w);
3619 }
3620 return args;
3621}
3622
3623static PyObject *
3624do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3625{
3626 PyObject *callargs = NULL;
3627 PyObject *kwdict = NULL;
3628 PyObject *result = NULL;
3629
3630 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003631 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003632 if (kwdict == NULL)
3633 goto call_fail;
3634 }
3635 callargs = load_args(pp_stack, na);
3636 if (callargs == NULL)
3637 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003638#ifdef CALL_PROFILE
3639 /* At this point, we have to look at the type of func to
3640 update the call stats properly. Do it here so as to avoid
3641 exposing the call stats machinery outside ceval.c
3642 */
3643 if (PyFunction_Check(func))
3644 PCALL(PCALL_FUNCTION);
3645 else if (PyMethod_Check(func))
3646 PCALL(PCALL_METHOD);
3647 else if (PyType_Check(func))
3648 PCALL(PCALL_TYPE);
3649 else
3650 PCALL(PCALL_OTHER);
3651#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003652 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003653 call_fail:
3654 Py_XDECREF(callargs);
3655 Py_XDECREF(kwdict);
3656 return result;
3657}
3658
3659static PyObject *
3660ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3661{
3662 int nstar = 0;
3663 PyObject *callargs = NULL;
3664 PyObject *stararg = NULL;
3665 PyObject *kwdict = NULL;
3666 PyObject *result = NULL;
3667
3668 if (flags & CALL_FLAG_KW) {
3669 kwdict = EXT_POP(*pp_stack);
3670 if (!(kwdict && PyDict_Check(kwdict))) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003671 PyErr_Format(PyExc_TypeError,
Jeremy Hylton512a2372001-04-11 13:52:29 +00003672 "%s%s argument after ** "
3673 "must be a dictionary",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003674 PyEval_GetFuncName(func),
3675 PyEval_GetFuncDesc(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003676 goto ext_call_fail;
3677 }
3678 }
3679 if (flags & CALL_FLAG_VAR) {
3680 stararg = EXT_POP(*pp_stack);
3681 if (!PyTuple_Check(stararg)) {
3682 PyObject *t = NULL;
3683 t = PySequence_Tuple(stararg);
3684 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003685 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3686 PyErr_Format(PyExc_TypeError,
3687 "%s%s argument after * "
3688 "must be a sequence",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003689 PyEval_GetFuncName(func),
3690 PyEval_GetFuncDesc(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003691 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003692 goto ext_call_fail;
3693 }
3694 Py_DECREF(stararg);
3695 stararg = t;
3696 }
3697 nstar = PyTuple_GET_SIZE(stararg);
3698 }
3699 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003700 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003701 if (kwdict == NULL)
3702 goto ext_call_fail;
3703 }
3704 callargs = update_star_args(na, nstar, stararg, pp_stack);
3705 if (callargs == NULL)
3706 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003707#ifdef CALL_PROFILE
3708 /* At this point, we have to look at the type of func to
3709 update the call stats properly. Do it here so as to avoid
3710 exposing the call stats machinery outside ceval.c
3711 */
3712 if (PyFunction_Check(func))
3713 PCALL(PCALL_FUNCTION);
3714 else if (PyMethod_Check(func))
3715 PCALL(PCALL_METHOD);
3716 else if (PyType_Check(func))
3717 PCALL(PCALL_TYPE);
3718 else
3719 PCALL(PCALL_OTHER);
3720#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003721 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003722 ext_call_fail:
3723 Py_XDECREF(callargs);
3724 Py_XDECREF(kwdict);
3725 Py_XDECREF(stararg);
3726 return result;
3727}
3728
Guido van Rossum3b9c6671996-07-30 18:40:29 +00003729#define SLICE_ERROR_MSG \
3730 "standard sequence type does not support step size other than one"
3731
Tim Peterscb479e72001-12-16 19:11:44 +00003732/* Extract a slice index from a PyInt or PyLong, and store in *pi.
3733 Silently reduce values larger than INT_MAX to INT_MAX, and silently
3734 boost values less than -INT_MAX to 0. Return 0 on error, 1 on success.
3735*/
Tim Petersb5196382001-12-16 19:44:20 +00003736/* Note: If v is NULL, return success without storing into *pi. This
3737 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3738 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00003739*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00003740int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003741_PyEval_SliceIndex(PyObject *v, int *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003742{
Tim Petersb5196382001-12-16 19:44:20 +00003743 if (v != NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003744 long x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003745 if (PyInt_Check(v)) {
3746 x = PyInt_AsLong(v);
3747 } else if (PyLong_Check(v)) {
3748 x = PyLong_AsLong(v);
3749 if (x==-1 && PyErr_Occurred()) {
3750 PyObject *long_zero;
Guido van Rossumac7be682001-01-17 15:42:30 +00003751 int cmp;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003752
Guido van Rossumac7be682001-01-17 15:42:30 +00003753 if (!PyErr_ExceptionMatches(
3754 PyExc_OverflowError)) {
3755 /* It's not an overflow error, so just
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003756 signal an error */
Guido van Rossum20c6add2000-05-08 14:06:50 +00003757 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003758 }
3759
Guido van Rossumac7be682001-01-17 15:42:30 +00003760 /* Clear the OverflowError */
3761 PyErr_Clear();
3762
3763 /* It's an overflow error, so we need to
3764 check the sign of the long integer,
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003765 set the value to INT_MAX or -INT_MAX,
3766 and clear the error. */
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003767
3768 /* Create a long integer with a value of 0 */
Guido van Rossumac7be682001-01-17 15:42:30 +00003769 long_zero = PyLong_FromLong(0L);
Tim Peterscb479e72001-12-16 19:11:44 +00003770 if (long_zero == NULL)
3771 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003772
3773 /* Check sign */
Guido van Rossumac7be682001-01-17 15:42:30 +00003774 cmp = PyObject_RichCompareBool(v, long_zero,
3775 Py_GT);
3776 Py_DECREF(long_zero);
3777 if (cmp < 0)
3778 return 0;
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003779 else if (cmp)
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003780 x = INT_MAX;
3781 else
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003782 x = -INT_MAX;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003783 }
3784 } else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003785 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003786 "slice indices must be integers");
Guido van Rossum20c6add2000-05-08 14:06:50 +00003787 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003788 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003789 /* Truncate -- very long indices are truncated anyway */
3790 if (x > INT_MAX)
3791 x = INT_MAX;
3792 else if (x < -INT_MAX)
Michael W. Hudsoncbd6fb92002-11-06 15:17:32 +00003793 x = -INT_MAX;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003794 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003795 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00003796 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003797}
3798
Guido van Rossum50d756e2001-08-18 17:43:36 +00003799#undef ISINT
3800#define ISINT(x) ((x) == NULL || PyInt_Check(x) || PyLong_Check(x))
3801
Guido van Rossumb209a111997-04-29 18:18:01 +00003802static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003803apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003804{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003805 PyTypeObject *tp = u->ob_type;
3806 PySequenceMethods *sq = tp->tp_as_sequence;
3807
3808 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3809 int ilow = 0, ihigh = INT_MAX;
3810 if (!_PyEval_SliceIndex(v, &ilow))
3811 return NULL;
3812 if (!_PyEval_SliceIndex(w, &ihigh))
3813 return NULL;
3814 return PySequence_GetSlice(u, ilow, ihigh);
3815 }
3816 else {
3817 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00003818 if (slice != NULL) {
3819 PyObject *res = PyObject_GetItem(u, slice);
3820 Py_DECREF(slice);
3821 return res;
3822 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00003823 else
3824 return NULL;
3825 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003826}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003827
3828static int
Guido van Rossumac7be682001-01-17 15:42:30 +00003829assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
3830 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003831{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003832 PyTypeObject *tp = u->ob_type;
3833 PySequenceMethods *sq = tp->tp_as_sequence;
3834
3835 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3836 int ilow = 0, ihigh = INT_MAX;
3837 if (!_PyEval_SliceIndex(v, &ilow))
3838 return -1;
3839 if (!_PyEval_SliceIndex(w, &ihigh))
3840 return -1;
3841 if (x == NULL)
3842 return PySequence_DelSlice(u, ilow, ihigh);
3843 else
3844 return PySequence_SetSlice(u, ilow, ihigh, x);
3845 }
3846 else {
3847 PyObject *slice = PySlice_New(v, w, NULL);
3848 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00003849 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003850 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00003851 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00003852 else
Guido van Rossum354797c2001-12-03 19:45:06 +00003853 res = PyObject_DelItem(u, slice);
3854 Py_DECREF(slice);
3855 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003856 }
3857 else
3858 return -1;
3859 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003860}
3861
Guido van Rossumb209a111997-04-29 18:18:01 +00003862static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003863cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003864{
Guido van Rossumac7be682001-01-17 15:42:30 +00003865 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003866 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00003867 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003868 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003869 break;
3870 case PyCmp_IS_NOT:
3871 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003872 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003873 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003874 res = PySequence_Contains(w, v);
3875 if (res < 0)
3876 return NULL;
3877 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003878 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00003879 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003880 if (res < 0)
3881 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003882 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003883 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003884 case PyCmp_EXC_MATCH:
Barry Warsaw4249f541997-08-22 21:26:19 +00003885 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003886 break;
3887 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00003888 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003889 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003890 v = res ? Py_True : Py_False;
3891 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003892 return v;
3893}
3894
Thomas Wouters52152252000-08-17 22:55:00 +00003895static PyObject *
3896import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003897{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003898 PyObject *x;
3899
3900 x = PyObject_GetAttr(v, name);
3901 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00003902 PyErr_Format(PyExc_ImportError,
3903 "cannot import name %.230s",
3904 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003905 }
Thomas Wouters52152252000-08-17 22:55:00 +00003906 return x;
3907}
Guido van Rossumac7be682001-01-17 15:42:30 +00003908
Thomas Wouters52152252000-08-17 22:55:00 +00003909static int
3910import_all_from(PyObject *locals, PyObject *v)
3911{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003912 PyObject *all = PyObject_GetAttrString(v, "__all__");
3913 PyObject *dict, *name, *value;
3914 int skip_leading_underscores = 0;
3915 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00003916
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003917 if (all == NULL) {
3918 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3919 return -1; /* Unexpected error */
3920 PyErr_Clear();
3921 dict = PyObject_GetAttrString(v, "__dict__");
3922 if (dict == NULL) {
3923 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3924 return -1;
3925 PyErr_SetString(PyExc_ImportError,
3926 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00003927 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003928 }
3929 all = PyMapping_Keys(dict);
3930 Py_DECREF(dict);
3931 if (all == NULL)
3932 return -1;
3933 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00003934 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003935
3936 for (pos = 0, err = 0; ; pos++) {
3937 name = PySequence_GetItem(all, pos);
3938 if (name == NULL) {
3939 if (!PyErr_ExceptionMatches(PyExc_IndexError))
3940 err = -1;
3941 else
3942 PyErr_Clear();
3943 break;
3944 }
3945 if (skip_leading_underscores &&
3946 PyString_Check(name) &&
3947 PyString_AS_STRING(name)[0] == '_')
3948 {
3949 Py_DECREF(name);
3950 continue;
3951 }
3952 value = PyObject_GetAttr(v, name);
3953 if (value == NULL)
3954 err = -1;
3955 else
3956 err = PyDict_SetItem(locals, name, value);
3957 Py_DECREF(name);
3958 Py_XDECREF(value);
3959 if (err != 0)
3960 break;
3961 }
3962 Py_DECREF(all);
3963 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00003964}
3965
Guido van Rossumb209a111997-04-29 18:18:01 +00003966static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003967build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003968{
Guido van Rossum7851eea2001-09-12 19:19:18 +00003969 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003970
3971 if (PyDict_Check(methods))
3972 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00003973 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00003974 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00003975 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
3976 base = PyTuple_GET_ITEM(bases, 0);
3977 metaclass = PyObject_GetAttrString(base, "__class__");
3978 if (metaclass == NULL) {
3979 PyErr_Clear();
3980 metaclass = (PyObject *)base->ob_type;
3981 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00003982 }
3983 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00003984 else {
3985 PyObject *g = PyEval_GetGlobals();
3986 if (g != NULL && PyDict_Check(g))
3987 metaclass = PyDict_GetItemString(g, "__metaclass__");
3988 if (metaclass == NULL)
3989 metaclass = (PyObject *) &PyClass_Type;
3990 Py_INCREF(metaclass);
3991 }
3992 result = PyObject_CallFunction(metaclass, "OOO", name, bases, methods);
3993 Py_DECREF(metaclass);
3994 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00003995}
3996
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003997static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003998exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
3999 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004000{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004001 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00004002 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004003 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004004
Guido van Rossumb209a111997-04-29 18:18:01 +00004005 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
4006 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004007 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00004008 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004009 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00004010 locals = PyTuple_GetItem(prog, 2);
4011 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004012 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004013 if (globals == Py_None) {
4014 globals = PyEval_GetGlobals();
4015 if (locals == Py_None) {
4016 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00004017 plain = 1;
4018 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004019 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004020 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004021 locals = globals;
Guido van Rossumb209a111997-04-29 18:18:01 +00004022 if (!PyString_Check(prog) &&
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004023 !PyUnicode_Check(prog) &&
Guido van Rossumb209a111997-04-29 18:18:01 +00004024 !PyCode_Check(prog) &&
4025 !PyFile_Check(prog)) {
4026 PyErr_SetString(PyExc_TypeError,
Guido van Rossumac7be682001-01-17 15:42:30 +00004027 "exec: arg 1 must be a string, file, or code object");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004028 return -1;
4029 }
Fred Drake661ea262000-10-24 19:57:45 +00004030 if (!PyDict_Check(globals)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004031 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00004032 "exec: arg 2 must be a dictionary or None");
4033 return -1;
4034 }
4035 if (!PyDict_Check(locals)) {
4036 PyErr_SetString(PyExc_TypeError,
4037 "exec: arg 3 must be a dictionary or None");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004038 return -1;
4039 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004040 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00004041 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00004042 if (PyCode_Check(prog)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +00004043 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4044 PyErr_SetString(PyExc_TypeError,
4045 "code object passed to exec may not contain free variables");
4046 return -1;
4047 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004048 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004049 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004050 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004051 FILE *fp = PyFile_AsFile(prog);
4052 char *name = PyString_AsString(PyFile_Name(prog));
Tim Peters5ba58662001-07-16 02:29:45 +00004053 PyCompilerFlags cf;
4054 cf.cf_flags = 0;
4055 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004056 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
4057 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004058 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004059 v = PyRun_File(fp, name, Py_file_input, globals,
4060 locals);
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004061 }
4062 else {
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004063 PyObject *tmp = NULL;
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004064 char *str;
Tim Peters5ba58662001-07-16 02:29:45 +00004065 PyCompilerFlags cf;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004066 cf.cf_flags = 0;
4067#ifdef Py_USING_UNICODE
4068 if (PyUnicode_Check(prog)) {
4069 tmp = PyUnicode_AsUTF8String(prog);
4070 if (tmp == NULL)
4071 return -1;
4072 prog = tmp;
4073 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4074 }
4075#endif
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004076 if (PyString_AsStringAndSize(prog, &str, NULL))
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004077 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004078 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004079 v = PyRun_StringFlags(str, Py_file_input, globals,
4080 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004081 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004082 v = PyRun_String(str, Py_file_input, globals, locals);
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004083 Py_XDECREF(tmp);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004084 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004085 if (plain)
4086 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004087 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004088 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00004089 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004090 return 0;
4091}
Guido van Rossum24c13741995-02-14 09:42:43 +00004092
Guido van Rossumac7be682001-01-17 15:42:30 +00004093static void
Paul Prescode68140d2000-08-30 20:25:01 +00004094format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4095{
4096 char *obj_str;
4097
4098 if (!obj)
4099 return;
4100
4101 obj_str = PyString_AsString(obj);
4102 if (!obj_str)
4103 return;
4104
4105 PyErr_Format(exc, format_str, obj_str);
4106}
Guido van Rossum950361c1997-01-24 13:49:28 +00004107
4108#ifdef DYNAMIC_EXECUTION_PROFILE
4109
Skip Montanarof118cb12001-10-15 20:51:38 +00004110static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004111getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004112{
4113 int i;
4114 PyObject *l = PyList_New(256);
4115 if (l == NULL) return NULL;
4116 for (i = 0; i < 256; i++) {
4117 PyObject *x = PyInt_FromLong(a[i]);
4118 if (x == NULL) {
4119 Py_DECREF(l);
4120 return NULL;
4121 }
4122 PyList_SetItem(l, i, x);
4123 }
4124 for (i = 0; i < 256; i++)
4125 a[i] = 0;
4126 return l;
4127}
4128
4129PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004130_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004131{
4132#ifndef DXPAIRS
4133 return getarray(dxp);
4134#else
4135 int i;
4136 PyObject *l = PyList_New(257);
4137 if (l == NULL) return NULL;
4138 for (i = 0; i < 257; i++) {
4139 PyObject *x = getarray(dxpairs[i]);
4140 if (x == NULL) {
4141 Py_DECREF(l);
4142 return NULL;
4143 }
4144 PyList_SetItem(l, i, x);
4145 }
4146 return l;
4147#endif
4148}
4149
4150#endif