blob: 7f8f65493bf87f23abdf6a37154ce2bb2916d0dd [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 Rossum65d5b571998-12-21 19:32:43 +0000293static PyThread_type_lock interpreter_lock = 0;
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
Guido van Rossum374a9221991-04-04 10:40:29 +0000513/* Status code for main loop (reason for stack unwind) */
514
515enum why_code {
516 WHY_NOT, /* No error */
517 WHY_EXCEPTION, /* Exception occurred */
518 WHY_RERAISE, /* Exception re-raised by 'finally' */
519 WHY_RETURN, /* 'return' statement */
Jeremy Hylton3faa52e2001-02-01 22:48:12 +0000520 WHY_BREAK, /* 'break' statement */
Tim Peters5ca576e2001-06-18 22:08:13 +0000521 WHY_CONTINUE, /* 'continue' statement */
Tim Peters6e6a63f2001-10-18 20:49:35 +0000522 WHY_YIELD /* 'yield' operator */
Guido van Rossum374a9221991-04-04 10:40:29 +0000523};
524
Tim Petersdbd9ba62000-07-09 03:09:57 +0000525static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
Tim Petersd6d010b2001-06-21 02:49:55 +0000526static int unpack_iterable(PyObject *, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000527
Skip Montanarod581d772002-09-03 20:10:45 +0000528/* for manipulating the thread switch and periodic "stuff" - used to be
529 per thread, now just a pair o' globals */
Skip Montanaro99dba272002-09-03 20:19:06 +0000530int _Py_CheckInterval = 100;
531volatile int _Py_Ticker = 100;
Guido van Rossum374a9221991-04-04 10:40:29 +0000532
Guido van Rossumb209a111997-04-29 18:18:01 +0000533PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000534PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000535{
Jeremy Hylton985eba52003-02-05 23:13:00 +0000536 /* XXX raise SystemError if globals is NULL */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000537 return PyEval_EvalCodeEx(co,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000538 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000539 (PyObject **)NULL, 0,
540 (PyObject **)NULL, 0,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000541 (PyObject **)NULL, 0,
542 NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000543}
544
545
546/* Interpreter main loop */
547
Tim Peters6d6c1a32001-08-02 04:15:00 +0000548static PyObject *
Tim Peters5ca576e2001-06-18 22:08:13 +0000549eval_frame(PyFrameObject *f)
Guido van Rossum374a9221991-04-04 10:40:29 +0000550{
Guido van Rossum950361c1997-01-24 13:49:28 +0000551#ifdef DXPAIRS
552 int lastopcode = 0;
553#endif
Tim Petersb6d14da2001-12-19 04:11:07 +0000554 PyObject **stack_pointer; /* Next free slot in value stack */
Guido van Rossum374a9221991-04-04 10:40:29 +0000555 register unsigned char *next_instr;
Moshe Zadkaaa39a7e2000-08-07 06:34:45 +0000556 register int opcode=0; /* Current opcode */
557 register int oparg=0; /* Current opcode argument, if any */
Guido van Rossum374a9221991-04-04 10:40:29 +0000558 register enum why_code why; /* Reason for block stack unwind */
559 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000560 register PyObject *x; /* Result object -- NULL if error */
561 register PyObject *v; /* Temporary objects popped off stack */
562 register PyObject *w;
563 register PyObject *u;
564 register PyObject *t;
Barry Warsaw23c9ec82000-08-21 15:44:01 +0000565 register PyObject *stream = NULL; /* for PRINT opcodes */
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000566 register PyObject **fastlocals, **freevars;
Guido van Rossum014518f1998-11-23 21:09:51 +0000567 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000568 PyThreadState *tstate = PyThreadState_GET();
Tim Peters5ca576e2001-06-18 22:08:13 +0000569 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000570
571 /* when tracing we set things up so that
572
573 not (instr_lb <= current_bytecode_offset < instr_ub)
574
575 is true when the line being executed has changed. The
576 initial values are such as to make this false the first
577 time it is tested. */
578 int instr_ub = -1, instr_lb = 0;
579
Guido van Rossumd076c731998-10-07 19:42:25 +0000580 unsigned char *first_instr;
Skip Montanaro04d80f82002-08-04 21:03:35 +0000581 PyObject *names;
582 PyObject *consts;
Guido van Rossum96a42c81992-01-12 02:29:51 +0000583#ifdef LLTRACE
Guido van Rossumacbe8da1993-04-15 15:33:52 +0000584 int lltrace;
Guido van Rossum374a9221991-04-04 10:40:29 +0000585#endif
Guido van Rossum408027e1996-12-30 16:17:54 +0000586#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000587 /* Make it easier to find out where we are with a debugger */
Tim Peters5ca576e2001-06-18 22:08:13 +0000588 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000589#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000590
Neal Norwitza81d2202002-07-14 00:27:26 +0000591/* Tuple access macros */
592
593#ifndef Py_DEBUG
594#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
595#else
596#define GETITEM(v, i) PyTuple_GetItem((v), (i))
597#endif
598
Guido van Rossum374a9221991-04-04 10:40:29 +0000599/* Code access macros */
600
Guido van Rossumd076c731998-10-07 19:42:25 +0000601#define INSTR_OFFSET() (next_instr - first_instr)
Guido van Rossum374a9221991-04-04 10:40:29 +0000602#define NEXTOP() (*next_instr++)
603#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Guido van Rossumd076c731998-10-07 19:42:25 +0000604#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000605#define JUMPBY(x) (next_instr += (x))
606
Raymond Hettingerf606f872003-03-16 03:11:04 +0000607/* OpCode prediction macros
608 Some opcodes tend to come in pairs thus making it possible to predict
609 the second code when the first is run. For example, COMPARE_OP is often
610 followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And, those opcodes are often
611 followed by a POP_TOP.
612
613 Verifying the prediction costs a single high-speed test of register
Raymond Hettingerac2072922003-03-16 15:41:11 +0000614 variable against a constant. If the pairing was good, then the
Raymond Hettingerf606f872003-03-16 03:11:04 +0000615 processor has a high likelihood of making its own successful branch
616 prediction which results in a nearly zero overhead transition to the
617 next opcode.
618
619 A successful prediction saves a trip through the eval-loop including
620 its two unpredictable branches, the HASARG test and the switch-case.
621*/
622
Raymond Hettingerac2072922003-03-16 15:41:11 +0000623#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingerf606f872003-03-16 03:11:04 +0000624#define PREDICTED(op) PRED_##op: next_instr++
Raymond Hettinger7dc52212003-03-16 20:14:44 +0000625#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = (next_instr[2]<<8) + \
626 next_instr[1]; next_instr += 3
Raymond Hettingerf606f872003-03-16 03:11:04 +0000627
Guido van Rossum374a9221991-04-04 10:40:29 +0000628/* Stack manipulation macros */
629
630#define STACK_LEVEL() (stack_pointer - f->f_valuestack)
631#define EMPTY() (STACK_LEVEL() == 0)
632#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000633#define SECOND() (stack_pointer[-2])
634#define THIRD() (stack_pointer[-3])
635#define FOURTH() (stack_pointer[-4])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000636#define SET_TOP(v) (stack_pointer[-1] = (v))
637#define SET_SECOND(v) (stack_pointer[-2] = (v))
638#define SET_THIRD(v) (stack_pointer[-3] = (v))
639#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000640#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000641#define BASIC_PUSH(v) (*stack_pointer++ = (v))
642#define BASIC_POP() (*--stack_pointer)
643
Guido van Rossum96a42c81992-01-12 02:29:51 +0000644#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000645#define PUSH(v) { (void)(BASIC_PUSH(v), \
646 lltrace && prtrace(TOP(), "push")); \
647 assert(STACK_LEVEL() <= f->f_stacksize); }
Fred Drakede26cfc2001-10-13 06:11:28 +0000648#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000649#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
650 lltrace && prtrace(TOP(), "stackadj")); \
651 assert(STACK_LEVEL() <= f->f_stacksize); }
Guido van Rossum374a9221991-04-04 10:40:29 +0000652#else
653#define PUSH(v) BASIC_PUSH(v)
654#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000655#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000656#endif
657
Guido van Rossum681d79a1995-07-18 14:51:37 +0000658/* Local variable macros */
659
660#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000661
662/* The SETLOCAL() macro must not DECREF the local variable in-place and
663 then store the new value; it must copy the old value to a temporary
664 value, then store the new value, and then DECREF the temporary value.
665 This is because it is possible that during the DECREF the frame is
666 accessed by other code (e.g. a __del__ method or gc.collect()) and the
667 variable would be pointing to already-freed memory. */
668#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
669 GETLOCAL(i) = value; \
670 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000671
Guido van Rossuma027efa1997-05-05 20:56:21 +0000672/* Start of code */
673
Tim Peters5ca576e2001-06-18 22:08:13 +0000674 if (f == NULL)
675 return NULL;
676
Guido van Rossum8861b741996-07-30 16:49:37 +0000677#ifdef USE_STACKCHECK
Guido van Rossuma027efa1997-05-05 20:56:21 +0000678 if (tstate->recursion_depth%10 == 0 && PyOS_CheckStack()) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000679 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
Guido van Rossum8861b741996-07-30 16:49:37 +0000680 return NULL;
681 }
682#endif
683
Tim Peters5ca576e2001-06-18 22:08:13 +0000684 /* push frame */
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000685 if (++tstate->recursion_depth > recursion_limit) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000686 --tstate->recursion_depth;
687 PyErr_SetString(PyExc_RuntimeError,
Fred Drake661ea262000-10-24 19:57:45 +0000688 "maximum recursion depth exceeded");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000689 tstate->frame = f->f_back;
Guido van Rossum8861b741996-07-30 16:49:37 +0000690 return NULL;
691 }
692
Tim Peters5ca576e2001-06-18 22:08:13 +0000693 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000694
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000695 if (tstate->use_tracing) {
696 if (tstate->c_tracefunc != NULL) {
697 /* tstate->c_tracefunc, if defined, is a
698 function that will be called on *every* entry
699 to a code block. Its return value, if not
700 None, is a function that will be called at
701 the start of each executed line of code.
702 (Actually, the function must return itself
703 in order to continue tracing.) The trace
704 functions are called with three arguments:
705 a pointer to the current frame, a string
706 indicating why the function is called, and
707 an argument which depends on the situation.
708 The global trace function is also called
709 whenever an exception is detected. */
710 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
711 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000712 /* Trace function raised an error */
Michael W. Hudsonfb4d6ec2002-10-02 13:13:45 +0000713 --tstate->recursion_depth;
714 tstate->frame = f->f_back;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000715 return NULL;
716 }
717 }
718 if (tstate->c_profilefunc != NULL) {
719 /* Similar for c_profilefunc, except it needn't
720 return itself and isn't called for "line" events */
721 if (call_trace(tstate->c_profilefunc,
722 tstate->c_profileobj,
723 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000724 /* Profile function raised an error */
Michael W. Hudsonfb4d6ec2002-10-02 13:13:45 +0000725 --tstate->recursion_depth;
726 tstate->frame = f->f_back;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000727 return NULL;
728 }
729 }
730 }
731
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000732 co = f->f_code;
733 names = co->co_names;
734 consts = co->co_consts;
735 fastlocals = f->f_localsplus;
736 freevars = f->f_localsplus + f->f_nlocals;
737 _PyCode_GETCODEPTR(co, &first_instr);
738 /* An explanation is in order for the next line.
739
740 f->f_lasti now refers to the index of the last instruction
741 executed. You might think this was obvious from the name, but
742 this wasn't always true before 2.3! PyFrame_New now sets
743 f->f_lasti to -1 (i.e. the index *before* the first instruction)
744 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
745 does work. Promise. */
746 next_instr = first_instr + f->f_lasti + 1;
747 stack_pointer = f->f_stacktop;
748 assert(stack_pointer != NULL);
749 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
750
Tim Peters5ca576e2001-06-18 22:08:13 +0000751#ifdef LLTRACE
752 lltrace = PyDict_GetItemString(f->f_globals,"__lltrace__") != NULL;
753#endif
754#if defined(Py_DEBUG) || defined(LLTRACE)
755 filename = PyString_AsString(co->co_filename);
756#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000757
Guido van Rossum374a9221991-04-04 10:40:29 +0000758 why = WHY_NOT;
759 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +0000760 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +0000761 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +0000762
Guido van Rossum374a9221991-04-04 10:40:29 +0000763 for (;;) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000764 assert(stack_pointer >= f->f_valuestack); /* else underflow */
765 assert(STACK_LEVEL() <= f->f_stacksize); /* else overflow */
766
Guido van Rossuma027efa1997-05-05 20:56:21 +0000767 /* Do periodic things. Doing this every time through
768 the loop would add too much overhead, so we do it
769 only every Nth instruction. We also do it if
770 ``things_to_do'' is set, i.e. when an asynchronous
771 event needs attention (e.g. a signal handler or
772 async I/O handler); see Py_AddPendingCall() and
773 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000774
Skip Montanarod581d772002-09-03 20:10:45 +0000775 if (--_Py_Ticker < 0) {
776 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000777 tstate->tick_counter++;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000778 if (things_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +0000779 if (Py_MakePendingCalls() < 0) {
780 why = WHY_EXCEPTION;
781 goto on_error;
782 }
783 }
Guido van Rossumdf0d00e1997-05-20 15:57:49 +0000784#if !defined(HAVE_SIGNAL_H) || defined(macintosh)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000785 /* If we have true signals, the signal handler
786 will call Py_AddPendingCall() so we don't
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000787 have to call PyErr_CheckSignals(). On the
788 Mac and DOS, alas, we have to call it. */
Guido van Rossumb209a111997-04-29 18:18:01 +0000789 if (PyErr_CheckSignals()) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000790 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +0000791 goto on_error;
792 }
Guido van Rossum70d44781997-01-21 06:15:24 +0000793#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000794
Guido van Rossume59214e1994-08-30 08:01:59 +0000795#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000796 if (interpreter_lock) {
797 /* Give another thread a chance */
798
Guido van Rossum25ce5661997-08-02 03:10:38 +0000799 if (PyThreadState_Swap(NULL) != tstate)
800 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000801 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000802
803 /* Other threads may run now */
804
Guido van Rossum65d5b571998-12-21 19:32:43 +0000805 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000806 if (PyThreadState_Swap(tstate) != NULL)
807 Py_FatalError("ceval: orphan tstate");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000808 }
809#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000810 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000811
Neil Schemenauer63543862002-02-17 19:10:14 +0000812 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +0000813 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +0000814
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000815 /* line-by-line tracing support */
816
817 if (tstate->c_tracefunc != NULL && !tstate->tracing) {
818 /* see maybe_call_line_trace
819 for expository comments */
820 f->f_stacktop = stack_pointer;
Michael W. Hudson006c7522002-11-08 13:08:46 +0000821
822 if (maybe_call_line_trace(tstate->c_tracefunc,
823 tstate->c_traceobj,
824 f, &instr_lb, &instr_ub)) {
825 /* trace function raised an exception */
826 why = WHY_EXCEPTION;
827 goto on_error;
828 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000829 /* Reload possibly changed frame fields */
830 JUMPTO(f->f_lasti);
831 stack_pointer = f->f_stacktop;
832 assert(stack_pointer != NULL);
833 f->f_stacktop = NULL;
834 }
835
836 /* Extract opcode and argument */
837
Guido van Rossum374a9221991-04-04 10:40:29 +0000838 opcode = NEXTOP();
839 if (HAS_ARG(opcode))
840 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +0000841 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +0000842#ifdef DYNAMIC_EXECUTION_PROFILE
843#ifdef DXPAIRS
844 dxpairs[lastopcode][opcode]++;
845 lastopcode = opcode;
846#endif
847 dxp[opcode]++;
848#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000849
Guido van Rossum96a42c81992-01-12 02:29:51 +0000850#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +0000851 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +0000852
Guido van Rossum96a42c81992-01-12 02:29:51 +0000853 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000854 if (HAS_ARG(opcode)) {
855 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000856 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +0000857 }
858 else {
859 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000860 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +0000861 }
862 }
863#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000864
Guido van Rossum374a9221991-04-04 10:40:29 +0000865 /* Main switch on opcode */
Jeremy Hylton52820442001-01-03 23:52:36 +0000866
Guido van Rossum374a9221991-04-04 10:40:29 +0000867 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +0000868
Guido van Rossum374a9221991-04-04 10:40:29 +0000869 /* BEWARE!
870 It is essential that any operation that fails sets either
871 x to NULL, err to nonzero, or why to anything but WHY_NOT,
872 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000873
Guido van Rossum374a9221991-04-04 10:40:29 +0000874 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000875
Raymond Hettinger060641d2003-04-22 06:49:11 +0000876 case NOP:
877 goto fast_next_opcode;
878
Neil Schemenauer63543862002-02-17 19:10:14 +0000879 case LOAD_FAST:
880 x = GETLOCAL(oparg);
881 if (x != NULL) {
882 Py_INCREF(x);
883 PUSH(x);
884 goto fast_next_opcode;
885 }
886 format_exc_check_arg(PyExc_UnboundLocalError,
887 UNBOUNDLOCAL_ERROR_MSG,
888 PyTuple_GetItem(co->co_varnames, oparg));
889 break;
890
891 case LOAD_CONST:
Skip Montanaro04d80f82002-08-04 21:03:35 +0000892 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +0000893 Py_INCREF(x);
894 PUSH(x);
895 goto fast_next_opcode;
896
Raymond Hettinger7dc52212003-03-16 20:14:44 +0000897 PREDICTED_WITH_ARG(STORE_FAST);
Neil Schemenauer63543862002-02-17 19:10:14 +0000898 case STORE_FAST:
899 v = POP();
900 SETLOCAL(oparg, v);
901 goto fast_next_opcode;
902
Raymond Hettingerf606f872003-03-16 03:11:04 +0000903 PREDICTED(POP_TOP);
Guido van Rossum374a9221991-04-04 10:40:29 +0000904 case POP_TOP:
905 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000906 Py_DECREF(v);
Neil Schemenauer63543862002-02-17 19:10:14 +0000907 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000908
Guido van Rossum374a9221991-04-04 10:40:29 +0000909 case ROT_TWO:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000910 v = TOP();
911 w = SECOND();
912 SET_TOP(w);
913 SET_SECOND(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000914 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000915
Guido van Rossum374a9221991-04-04 10:40:29 +0000916 case ROT_THREE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000917 v = TOP();
918 w = SECOND();
919 x = THIRD();
920 SET_TOP(w);
921 SET_SECOND(x);
922 SET_THIRD(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000923 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000924
Thomas Wouters434d0822000-08-24 20:11:32 +0000925 case ROT_FOUR:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000926 u = TOP();
927 v = SECOND();
928 w = THIRD();
929 x = FOURTH();
930 SET_TOP(v);
931 SET_SECOND(w);
932 SET_THIRD(x);
933 SET_FOURTH(u);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000934 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000935
Guido van Rossum374a9221991-04-04 10:40:29 +0000936 case DUP_TOP:
937 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000938 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000939 PUSH(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000940 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000941
Thomas Wouters434d0822000-08-24 20:11:32 +0000942 case DUP_TOPX:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000943 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000944 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000945 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000946 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000947 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000948 STACKADJ(2);
949 SET_TOP(x);
950 SET_SECOND(w);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000951 goto fast_next_opcode;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000952 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000953 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000954 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000955 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000956 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000957 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +0000958 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000959 STACKADJ(3);
960 SET_TOP(x);
961 SET_SECOND(w);
962 SET_THIRD(v);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000963 goto fast_next_opcode;
Thomas Wouters434d0822000-08-24 20:11:32 +0000964 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000965 Py_FatalError("invalid argument to DUP_TOPX"
966 " (bytecode corruption?)");
Tim Peters35ba6892000-10-11 07:04:49 +0000967 break;
Thomas Wouters434d0822000-08-24 20:11:32 +0000968
Guido van Rossum374a9221991-04-04 10:40:29 +0000969 case UNARY_POSITIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000970 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000971 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000972 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000973 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000974 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000975 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000976
Guido van Rossum374a9221991-04-04 10:40:29 +0000977 case UNARY_NEGATIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000978 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000979 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000980 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000981 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000982 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000983 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000984
Guido van Rossum374a9221991-04-04 10:40:29 +0000985 case UNARY_NOT:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000986 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000987 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000988 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +0000989 if (err == 0) {
990 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000991 SET_TOP(Py_True);
Guido van Rossumfc490731997-05-06 15:06:49 +0000992 continue;
993 }
994 else if (err > 0) {
995 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000996 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +0000997 err = 0;
998 continue;
999 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00001000 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001001 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001002
Guido van Rossum374a9221991-04-04 10:40:29 +00001003 case UNARY_CONVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001004 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001005 x = PyObject_Repr(v);
1006 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001007 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001008 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001009 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001010
Guido van Rossum7928cd71991-10-24 14:59:31 +00001011 case UNARY_INVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001012 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001013 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001014 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001015 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001016 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001017 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001018
Guido van Rossum50564e81996-01-12 01:13:16 +00001019 case BINARY_POWER:
1020 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001021 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001022 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +00001023 Py_DECREF(v);
1024 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001025 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001026 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +00001027 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001028
Guido van Rossum374a9221991-04-04 10:40:29 +00001029 case BINARY_MULTIPLY:
1030 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001031 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001032 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001033 Py_DECREF(v);
1034 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001035 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001036 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001037 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001038
Guido van Rossum374a9221991-04-04 10:40:29 +00001039 case BINARY_DIVIDE:
Tim Peters3caca232001-12-06 06:23:26 +00001040 if (!_Py_QnewFlag) {
1041 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001042 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001043 x = PyNumber_Divide(v, w);
1044 Py_DECREF(v);
1045 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001046 SET_TOP(x);
Tim Peters3caca232001-12-06 06:23:26 +00001047 if (x != NULL) continue;
1048 break;
1049 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001050 /* -Qnew is in effect: fall through to
Tim Peters3caca232001-12-06 06:23:26 +00001051 BINARY_TRUE_DIVIDE */
1052 case BINARY_TRUE_DIVIDE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001053 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001054 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001055 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001056 Py_DECREF(v);
1057 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001058 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001059 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001060 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001061
Guido van Rossum4668b002001-08-08 05:00:18 +00001062 case BINARY_FLOOR_DIVIDE:
1063 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001064 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001065 x = PyNumber_FloorDivide(v, w);
1066 Py_DECREF(v);
1067 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001068 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001069 if (x != NULL) continue;
1070 break;
1071
Guido van Rossum374a9221991-04-04 10:40:29 +00001072 case BINARY_MODULO:
1073 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001074 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001075 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001076 Py_DECREF(v);
1077 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001078 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001079 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001080 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001081
Guido van Rossum374a9221991-04-04 10:40:29 +00001082 case BINARY_ADD:
1083 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001084 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001085 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001086 /* INLINE: int + int */
1087 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001088 a = PyInt_AS_LONG(v);
1089 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001090 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001091 if ((i^a) < 0 && (i^b) < 0)
1092 goto slow_add;
1093 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001094 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001095 else {
1096 slow_add:
Guido van Rossumc12da691997-07-17 23:12:42 +00001097 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001098 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001099 Py_DECREF(v);
1100 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001101 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001102 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001103 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001104
Guido van Rossum374a9221991-04-04 10:40:29 +00001105 case BINARY_SUBTRACT:
1106 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001107 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001108 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001109 /* INLINE: int - int */
1110 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001111 a = PyInt_AS_LONG(v);
1112 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001113 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001114 if ((i^a) < 0 && (i^~b) < 0)
1115 goto slow_sub;
1116 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001117 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001118 else {
1119 slow_sub:
Guido van Rossumc12da691997-07-17 23:12:42 +00001120 x = PyNumber_Subtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001121 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001122 Py_DECREF(v);
1123 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001124 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001125 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001126 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001127
Guido van Rossum374a9221991-04-04 10:40:29 +00001128 case BINARY_SUBSCR:
1129 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001130 v = TOP();
Tim Petersb1c46982001-10-05 20:41:38 +00001131 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001132 /* INLINE: list[int] */
1133 long i = PyInt_AsLong(w);
1134 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +00001135 i += PyList_GET_SIZE(v);
Guido van Rossumc12da691997-07-17 23:12:42 +00001136 if (i < 0 ||
Guido van Rossumfa00e951998-07-08 15:02:37 +00001137 i >= PyList_GET_SIZE(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001138 PyErr_SetString(PyExc_IndexError,
1139 "list index out of range");
1140 x = NULL;
1141 }
1142 else {
Guido van Rossumfa00e951998-07-08 15:02:37 +00001143 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001144 Py_INCREF(x);
1145 }
1146 }
1147 else
1148 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001149 Py_DECREF(v);
1150 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001151 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001152 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001153 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001154
Guido van Rossum7928cd71991-10-24 14:59:31 +00001155 case BINARY_LSHIFT:
1156 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001157 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001158 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001159 Py_DECREF(v);
1160 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001161 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001162 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001163 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001164
Guido van Rossum7928cd71991-10-24 14:59:31 +00001165 case BINARY_RSHIFT:
1166 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001167 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001168 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001169 Py_DECREF(v);
1170 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001171 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001172 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001173 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001174
Guido van Rossum7928cd71991-10-24 14:59:31 +00001175 case BINARY_AND:
1176 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001177 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001178 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001179 Py_DECREF(v);
1180 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001181 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001182 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001183 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001184
Guido van Rossum7928cd71991-10-24 14:59:31 +00001185 case BINARY_XOR:
1186 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001187 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001188 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001189 Py_DECREF(v);
1190 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001191 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001192 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001193 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001194
Guido van Rossum7928cd71991-10-24 14:59:31 +00001195 case BINARY_OR:
1196 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001197 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001198 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001199 Py_DECREF(v);
1200 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001201 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001202 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001203 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001204
1205 case INPLACE_POWER:
1206 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001207 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001208 x = PyNumber_InPlacePower(v, w, Py_None);
1209 Py_DECREF(v);
1210 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001211 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001212 if (x != NULL) continue;
1213 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001214
Thomas Wouters434d0822000-08-24 20:11:32 +00001215 case INPLACE_MULTIPLY:
1216 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001217 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001218 x = PyNumber_InPlaceMultiply(v, w);
1219 Py_DECREF(v);
1220 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001221 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001222 if (x != NULL) continue;
1223 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001224
Thomas Wouters434d0822000-08-24 20:11:32 +00001225 case INPLACE_DIVIDE:
Tim Peters54b11912001-12-25 18:49:11 +00001226 if (!_Py_QnewFlag) {
1227 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001228 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001229 x = PyNumber_InPlaceDivide(v, w);
1230 Py_DECREF(v);
1231 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001232 SET_TOP(x);
Tim Peters54b11912001-12-25 18:49:11 +00001233 if (x != NULL) continue;
1234 break;
1235 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001236 /* -Qnew is in effect: fall through to
Tim Peters54b11912001-12-25 18:49:11 +00001237 INPLACE_TRUE_DIVIDE */
1238 case INPLACE_TRUE_DIVIDE:
Thomas Wouters434d0822000-08-24 20:11:32 +00001239 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001240 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001241 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001242 Py_DECREF(v);
1243 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001244 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001245 if (x != NULL) continue;
1246 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001247
Guido van Rossum4668b002001-08-08 05:00:18 +00001248 case INPLACE_FLOOR_DIVIDE:
1249 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001250 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001251 x = PyNumber_InPlaceFloorDivide(v, w);
1252 Py_DECREF(v);
1253 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001254 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001255 if (x != NULL) continue;
1256 break;
1257
Thomas Wouters434d0822000-08-24 20:11:32 +00001258 case INPLACE_MODULO:
1259 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001260 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001261 x = PyNumber_InPlaceRemainder(v, w);
1262 Py_DECREF(v);
1263 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001264 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001265 if (x != NULL) continue;
1266 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001267
Thomas Wouters434d0822000-08-24 20:11:32 +00001268 case INPLACE_ADD:
1269 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001270 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001271 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001272 /* INLINE: int + int */
1273 register long a, b, i;
1274 a = PyInt_AS_LONG(v);
1275 b = PyInt_AS_LONG(w);
1276 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001277 if ((i^a) < 0 && (i^b) < 0)
1278 goto slow_iadd;
1279 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001280 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001281 else {
1282 slow_iadd:
Thomas Wouters434d0822000-08-24 20:11:32 +00001283 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001284 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001285 Py_DECREF(v);
1286 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001287 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001288 if (x != NULL) continue;
1289 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001290
Thomas Wouters434d0822000-08-24 20:11:32 +00001291 case INPLACE_SUBTRACT:
1292 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001293 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001294 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001295 /* INLINE: int - int */
1296 register long a, b, i;
1297 a = PyInt_AS_LONG(v);
1298 b = PyInt_AS_LONG(w);
1299 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001300 if ((i^a) < 0 && (i^~b) < 0)
1301 goto slow_isub;
1302 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001303 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001304 else {
1305 slow_isub:
Thomas Wouters434d0822000-08-24 20:11:32 +00001306 x = PyNumber_InPlaceSubtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001307 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001308 Py_DECREF(v);
1309 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001310 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001311 if (x != NULL) continue;
1312 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001313
Thomas Wouters434d0822000-08-24 20:11:32 +00001314 case INPLACE_LSHIFT:
1315 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001316 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001317 x = PyNumber_InPlaceLshift(v, w);
1318 Py_DECREF(v);
1319 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001320 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001321 if (x != NULL) continue;
1322 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001323
Thomas Wouters434d0822000-08-24 20:11:32 +00001324 case INPLACE_RSHIFT:
1325 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001326 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001327 x = PyNumber_InPlaceRshift(v, w);
1328 Py_DECREF(v);
1329 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001330 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001331 if (x != NULL) continue;
1332 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001333
Thomas Wouters434d0822000-08-24 20:11:32 +00001334 case INPLACE_AND:
1335 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001336 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001337 x = PyNumber_InPlaceAnd(v, w);
1338 Py_DECREF(v);
1339 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001340 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001341 if (x != NULL) continue;
1342 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001343
Thomas Wouters434d0822000-08-24 20:11:32 +00001344 case INPLACE_XOR:
1345 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001346 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001347 x = PyNumber_InPlaceXor(v, w);
1348 Py_DECREF(v);
1349 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001350 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001351 if (x != NULL) continue;
1352 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001353
Thomas Wouters434d0822000-08-24 20:11:32 +00001354 case INPLACE_OR:
1355 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001356 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001357 x = PyNumber_InPlaceOr(v, w);
1358 Py_DECREF(v);
1359 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001360 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001361 if (x != NULL) continue;
1362 break;
1363
Guido van Rossum374a9221991-04-04 10:40:29 +00001364 case SLICE+0:
1365 case SLICE+1:
1366 case SLICE+2:
1367 case SLICE+3:
1368 if ((opcode-SLICE) & 2)
1369 w = POP();
1370 else
1371 w = NULL;
1372 if ((opcode-SLICE) & 1)
1373 v = POP();
1374 else
1375 v = NULL;
Raymond Hettinger663004b2003-01-09 15:24:30 +00001376 u = TOP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001377 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001378 Py_DECREF(u);
1379 Py_XDECREF(v);
1380 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001381 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001382 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001383 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001384
Guido van Rossum374a9221991-04-04 10:40:29 +00001385 case STORE_SLICE+0:
1386 case STORE_SLICE+1:
1387 case STORE_SLICE+2:
1388 case STORE_SLICE+3:
1389 if ((opcode-STORE_SLICE) & 2)
1390 w = POP();
1391 else
1392 w = NULL;
1393 if ((opcode-STORE_SLICE) & 1)
1394 v = POP();
1395 else
1396 v = NULL;
1397 u = POP();
1398 t = POP();
1399 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +00001400 Py_DECREF(t);
1401 Py_DECREF(u);
1402 Py_XDECREF(v);
1403 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001404 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001405 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001406
Guido van Rossum374a9221991-04-04 10:40:29 +00001407 case DELETE_SLICE+0:
1408 case DELETE_SLICE+1:
1409 case DELETE_SLICE+2:
1410 case DELETE_SLICE+3:
1411 if ((opcode-DELETE_SLICE) & 2)
1412 w = POP();
1413 else
1414 w = NULL;
1415 if ((opcode-DELETE_SLICE) & 1)
1416 v = POP();
1417 else
1418 v = NULL;
1419 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001420 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +00001421 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00001422 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 STORE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001429 w = TOP();
1430 v = SECOND();
1431 u = THIRD();
1432 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001433 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001434 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001435 Py_DECREF(u);
1436 Py_DECREF(v);
1437 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001438 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001439 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001440
Guido van Rossum374a9221991-04-04 10:40:29 +00001441 case DELETE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001442 w = TOP();
1443 v = SECOND();
1444 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001445 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001446 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001447 Py_DECREF(v);
1448 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001449 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001450 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001451
Guido van Rossum374a9221991-04-04 10:40:29 +00001452 case PRINT_EXPR:
1453 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001454 w = PySys_GetObject("displayhook");
1455 if (w == NULL) {
1456 PyErr_SetString(PyExc_RuntimeError,
1457 "lost sys.displayhook");
1458 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001459 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001460 }
1461 if (err == 0) {
1462 x = Py_BuildValue("(O)", v);
1463 if (x == NULL)
1464 err = -1;
1465 }
1466 if (err == 0) {
1467 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001468 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001469 if (w == NULL)
1470 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001471 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001472 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001473 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001474 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001475
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001476 case PRINT_ITEM_TO:
1477 w = stream = POP();
1478 /* fall through to PRINT_ITEM */
1479
Guido van Rossum374a9221991-04-04 10:40:29 +00001480 case PRINT_ITEM:
1481 v = POP();
Barry Warsaw093abe02000-08-29 04:56:13 +00001482 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001483 w = PySys_GetObject("stdout");
1484 if (w == NULL) {
1485 PyErr_SetString(PyExc_RuntimeError,
1486 "lost sys.stdout");
1487 err = -1;
1488 }
Guido van Rossum8f183201997-12-31 05:53:15 +00001489 }
Tim Peters8e5fd532002-03-24 19:25:00 +00001490 if (w != NULL && PyFile_SoftSpace(w, 0))
Guido van Rossumbe270261997-05-22 22:26:18 +00001491 err = PyFile_WriteString(" ", w);
1492 if (err == 0)
1493 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001494 if (err == 0) {
Tim Peters8e5fd532002-03-24 19:25:00 +00001495 /* XXX move into writeobject() ? */
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001496 if (PyString_Check(v)) {
1497 char *s = PyString_AS_STRING(v);
1498 int len = PyString_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001499 if (len == 0 ||
1500 !isspace(Py_CHARMASK(s[len-1])) ||
1501 s[len-1] == ' ')
1502 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001503 }
Martin v. Löwis8d3ce5a2001-12-18 22:36:40 +00001504#ifdef Py_USING_UNICODE
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001505 else if (PyUnicode_Check(v)) {
1506 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
1507 int len = PyUnicode_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001508 if (len == 0 ||
1509 !Py_UNICODE_ISSPACE(s[len-1]) ||
1510 s[len-1] == ' ')
1511 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001512 }
Michael W. Hudsond95c8282002-05-20 13:56:11 +00001513#endif
Tim Peters8e5fd532002-03-24 19:25:00 +00001514 else
1515 PyFile_SoftSpace(w, 1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001516 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001517 Py_DECREF(v);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001518 Py_XDECREF(stream);
1519 stream = NULL;
1520 if (err == 0)
1521 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001522 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001523
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001524 case PRINT_NEWLINE_TO:
1525 w = stream = POP();
1526 /* fall through to PRINT_NEWLINE */
1527
Guido van Rossum374a9221991-04-04 10:40:29 +00001528 case PRINT_NEWLINE:
Barry Warsaw093abe02000-08-29 04:56:13 +00001529 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001530 w = PySys_GetObject("stdout");
1531 if (w == NULL)
1532 PyErr_SetString(PyExc_RuntimeError,
1533 "lost sys.stdout");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001534 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001535 if (w != NULL) {
1536 err = PyFile_WriteString("\n", w);
1537 if (err == 0)
1538 PyFile_SoftSpace(w, 0);
1539 }
1540 Py_XDECREF(stream);
1541 stream = NULL;
Guido van Rossum374a9221991-04-04 10:40:29 +00001542 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001543
Thomas Wouters434d0822000-08-24 20:11:32 +00001544
1545#ifdef CASE_TOO_BIG
1546 default: switch (opcode) {
1547#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001548 case BREAK_LOOP:
1549 why = WHY_BREAK;
1550 break;
Guido van Rossum66b0e9c2001-03-21 19:17:22 +00001551
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00001552 case CONTINUE_LOOP:
1553 retval = PyInt_FromLong(oparg);
1554 why = WHY_CONTINUE;
1555 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001556
Guido van Rossumf10570b1995-07-07 22:53:21 +00001557 case RAISE_VARARGS:
1558 u = v = w = NULL;
1559 switch (oparg) {
1560 case 3:
1561 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001562 /* Fallthrough */
1563 case 2:
1564 v = POP(); /* value */
1565 /* Fallthrough */
1566 case 1:
1567 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001568 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001569 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001570 break;
1571 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001572 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001573 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001574 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001575 break;
1576 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001577 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001578
Guido van Rossum374a9221991-04-04 10:40:29 +00001579 case LOAD_LOCALS:
Guido van Rossum681d79a1995-07-18 14:51:37 +00001580 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001581 PyErr_SetString(PyExc_SystemError,
1582 "no locals");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001583 break;
1584 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001585 Py_INCREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001586 PUSH(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001587 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001588
Guido van Rossum374a9221991-04-04 10:40:29 +00001589 case RETURN_VALUE:
1590 retval = POP();
1591 why = WHY_RETURN;
1592 break;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001593
Tim Peters5ca576e2001-06-18 22:08:13 +00001594 case YIELD_VALUE:
1595 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001596 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001597 why = WHY_YIELD;
1598 break;
1599
1600
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001601 case EXEC_STMT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001602 w = TOP();
1603 v = SECOND();
1604 u = THIRD();
1605 STACKADJ(-3);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001606 err = exec_statement(f, u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001607 Py_DECREF(u);
1608 Py_DECREF(v);
1609 Py_DECREF(w);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001610 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001611
Guido van Rossum374a9221991-04-04 10:40:29 +00001612 case POP_BLOCK:
1613 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001614 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001615 while (STACK_LEVEL() > b->b_level) {
1616 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001617 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001618 }
1619 }
1620 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001621
Guido van Rossum374a9221991-04-04 10:40:29 +00001622 case END_FINALLY:
1623 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001624 if (PyInt_Check(v)) {
Raymond Hettinger080cb322003-03-14 01:37:42 +00001625 why = (enum why_code) PyInt_AS_LONG(v);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00001626 if (why == WHY_RETURN ||
Tim Peters5ca576e2001-06-18 22:08:13 +00001627 why == WHY_YIELD ||
Guido van Rossumc5fe5eb2002-06-12 03:45:21 +00001628 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001629 retval = POP();
1630 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001631 else if (PyString_Check(v) || PyClass_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001632 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001633 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001634 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001635 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001636 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001637 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001638 else if (v != Py_None) {
1639 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001640 "'finally' pops bad exception");
1641 why = WHY_EXCEPTION;
1642 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001643 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001644 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001645
Guido van Rossum374a9221991-04-04 10:40:29 +00001646 case BUILD_CLASS:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001647 u = TOP();
1648 v = SECOND();
1649 w = THIRD();
1650 STACKADJ(-2);
Guido van Rossum25831651993-05-19 14:50:45 +00001651 x = build_class(u, v, w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001652 SET_TOP(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001653 Py_DECREF(u);
1654 Py_DECREF(v);
1655 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001656 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001657
Guido van Rossum374a9221991-04-04 10:40:29 +00001658 case STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001659 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001660 v = POP();
Guido van Rossum681d79a1995-07-18 14:51:37 +00001661 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001662 PyErr_Format(PyExc_SystemError,
1663 "no locals found when storing %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001664 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001665 break;
1666 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001667 err = PyDict_SetItem(x, w, v);
1668 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001669 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001670
Guido van Rossum374a9221991-04-04 10:40:29 +00001671 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001672 w = GETITEM(names, oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001673 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001674 PyErr_Format(PyExc_SystemError,
1675 "no locals when deleting %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001676 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001677 break;
1678 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001679 if ((err = PyDict_DelItem(x, w)) != 0)
Guido van Rossumac7be682001-01-17 15:42:30 +00001680 format_exc_check_arg(PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001681 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001682 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001683
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001684 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001685 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001686 v = POP();
Raymond Hettinger21012b82003-02-26 18:11:50 +00001687 if (PyTuple_CheckExact(v)) {
Barry Warsawe42b18f1997-08-25 22:13:04 +00001688 if (PyTuple_Size(v) != oparg) {
1689 PyErr_SetString(PyExc_ValueError,
1690 "unpack tuple of wrong size");
1691 why = WHY_EXCEPTION;
1692 }
1693 else {
1694 for (; --oparg >= 0; ) {
1695 w = PyTuple_GET_ITEM(v, oparg);
1696 Py_INCREF(w);
1697 PUSH(w);
1698 }
1699 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001700 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00001701 else if (PyList_CheckExact(v)) {
Barry Warsawe42b18f1997-08-25 22:13:04 +00001702 if (PyList_Size(v) != oparg) {
1703 PyErr_SetString(PyExc_ValueError,
1704 "unpack list of wrong size");
1705 why = WHY_EXCEPTION;
1706 }
1707 else {
1708 for (; --oparg >= 0; ) {
1709 w = PyList_GET_ITEM(v, oparg);
1710 Py_INCREF(w);
1711 PUSH(w);
1712 }
1713 }
1714 }
Tim Petersd6d010b2001-06-21 02:49:55 +00001715 else if (unpack_iterable(v, oparg,
1716 stack_pointer + oparg))
1717 stack_pointer += oparg;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001718 else {
1719 if (PyErr_ExceptionMatches(PyExc_TypeError))
1720 PyErr_SetString(PyExc_TypeError,
1721 "unpack non-sequence");
Barry Warsawe42b18f1997-08-25 22:13:04 +00001722 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001723 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001724 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001725 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001726
Guido van Rossum374a9221991-04-04 10:40:29 +00001727 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001728 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001729 v = TOP();
1730 u = SECOND();
1731 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001732 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1733 Py_DECREF(v);
1734 Py_DECREF(u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001735 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001736
Guido van Rossum374a9221991-04-04 10:40:29 +00001737 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001738 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001739 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001740 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1741 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001742 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001743 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001744
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001745 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001746 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001747 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001748 err = PyDict_SetItem(f->f_globals, w, v);
1749 Py_DECREF(v);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001750 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001751
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001752 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001753 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001754 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001755 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001756 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001757 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001758
Guido van Rossum374a9221991-04-04 10:40:29 +00001759 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001760 w = GETITEM(names, oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001761 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001762 PyErr_Format(PyExc_SystemError,
1763 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001764 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001765 break;
1766 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001767 x = PyDict_GetItem(x, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001768 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001769 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001770 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001771 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001772 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001773 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001774 PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001775 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001776 break;
1777 }
1778 }
1779 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001780 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001781 PUSH(x);
1782 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001783
Guido van Rossum374a9221991-04-04 10:40:29 +00001784 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001785 w = GETITEM(names, oparg);
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001786 if (PyString_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00001787 /* Inline the PyDict_GetItem() calls.
1788 WARNING: this is an extreme speed hack.
1789 Do not try this at home. */
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001790 long hash = ((PyStringObject *)w)->ob_shash;
1791 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001792 PyDictObject *d;
1793 d = (PyDictObject *)(f->f_globals);
1794 x = d->ma_lookup(d, w, hash)->me_value;
1795 if (x != NULL) {
1796 Py_INCREF(x);
1797 PUSH(x);
1798 continue;
1799 }
1800 d = (PyDictObject *)(f->f_builtins);
1801 x = d->ma_lookup(d, w, hash)->me_value;
1802 if (x != NULL) {
1803 Py_INCREF(x);
1804 PUSH(x);
1805 continue;
1806 }
1807 goto load_global_error;
1808 }
1809 }
1810 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00001811 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001812 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001813 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001814 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001815 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00001816 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001817 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001818 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001819 break;
1820 }
1821 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001822 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001823 PUSH(x);
1824 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001825
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001826 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001827 x = GETLOCAL(oparg);
1828 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001829 format_exc_check_arg(
1830 PyExc_UnboundLocalError,
1831 UNBOUNDLOCAL_ERROR_MSG,
1832 PyTuple_GetItem(co->co_varnames, oparg)
1833 );
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001834 break;
1835 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00001836 SETLOCAL(oparg, NULL);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001837 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001838
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001839 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001840 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001841 Py_INCREF(x);
1842 PUSH(x);
1843 break;
1844
1845 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001846 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001847 w = PyCell_Get(x);
Jeremy Hylton2524d692001-02-05 17:23:16 +00001848 if (w == NULL) {
Jeremy Hylton76c81ee2002-07-11 16:56:38 +00001849 err = -1;
1850 /* Don't stomp existing exception */
1851 if (PyErr_Occurred())
1852 break;
Jeremy Hyltonc76770c2001-04-13 16:51:46 +00001853 if (oparg < f->f_ncells) {
Jeremy Hylton2524d692001-02-05 17:23:16 +00001854 v = PyTuple_GetItem(co->co_cellvars,
1855 oparg);
Jeremy Hyltonc76770c2001-04-13 16:51:46 +00001856 format_exc_check_arg(
1857 PyExc_UnboundLocalError,
1858 UNBOUNDLOCAL_ERROR_MSG,
1859 v);
1860 } else {
Jeremy Hylton2524d692001-02-05 17:23:16 +00001861 v = PyTuple_GetItem(
1862 co->co_freevars,
1863 oparg - f->f_ncells);
Jeremy Hyltonc76770c2001-04-13 16:51:46 +00001864 format_exc_check_arg(
1865 PyExc_NameError,
1866 UNBOUNDFREE_ERROR_MSG,
1867 v);
1868 }
Jeremy Hylton2524d692001-02-05 17:23:16 +00001869 break;
1870 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001871 PUSH(w);
1872 break;
1873
1874 case STORE_DEREF:
1875 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001876 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001877 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00001878 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001879 continue;
1880
Guido van Rossum374a9221991-04-04 10:40:29 +00001881 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001882 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001883 if (x != NULL) {
1884 for (; --oparg >= 0;) {
1885 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001886 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001887 }
1888 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001889 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001890 }
1891 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001892
Guido van Rossum374a9221991-04-04 10:40:29 +00001893 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00001894 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001895 if (x != NULL) {
1896 for (; --oparg >= 0;) {
1897 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00001898 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001899 }
1900 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001901 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001902 }
1903 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001904
Guido van Rossum374a9221991-04-04 10:40:29 +00001905 case BUILD_MAP:
Guido van Rossumb209a111997-04-29 18:18:01 +00001906 x = PyDict_New();
Guido van Rossum374a9221991-04-04 10:40:29 +00001907 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001908 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001909 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001910
Guido van Rossum374a9221991-04-04 10:40:29 +00001911 case LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001912 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001913 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001914 x = PyObject_GetAttr(v, w);
1915 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001916 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001917 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001918 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001919
Guido van Rossum374a9221991-04-04 10:40:29 +00001920 case COMPARE_OP:
1921 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001922 v = TOP();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001923 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001924 /* INLINE: cmp(int, int) */
1925 register long a, b;
1926 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001927 a = PyInt_AS_LONG(v);
1928 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001929 switch (oparg) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00001930 case PyCmp_LT: res = a < b; break;
1931 case PyCmp_LE: res = a <= b; break;
1932 case PyCmp_EQ: res = a == b; break;
1933 case PyCmp_NE: res = a != b; break;
1934 case PyCmp_GT: res = a > b; break;
1935 case PyCmp_GE: res = a >= b; break;
1936 case PyCmp_IS: res = v == w; break;
1937 case PyCmp_IS_NOT: res = v != w; break;
Guido van Rossumc12da691997-07-17 23:12:42 +00001938 default: goto slow_compare;
1939 }
1940 x = res ? Py_True : Py_False;
1941 Py_INCREF(x);
1942 }
1943 else {
1944 slow_compare:
1945 x = cmp_outcome(oparg, v, w);
1946 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001947 Py_DECREF(v);
1948 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001949 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001950 if (x == NULL) break;
1951 PREDICT(JUMP_IF_FALSE);
1952 PREDICT(JUMP_IF_TRUE);
1953 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001954
Guido van Rossum374a9221991-04-04 10:40:29 +00001955 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001956 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001957 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001958 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001959 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00001960 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001961 break;
1962 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001963 u = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001964 w = Py_BuildValue("(OOOO)",
Guido van Rossum681d79a1995-07-18 14:51:37 +00001965 w,
1966 f->f_globals,
Guido van Rossuma027efa1997-05-05 20:56:21 +00001967 f->f_locals == NULL ?
1968 Py_None : f->f_locals,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001969 u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001970 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001971 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001972 u = POP();
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001973 x = NULL;
1974 break;
1975 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001976 x = PyEval_CallObject(x, w);
1977 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001978 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001979 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001980 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001981
Thomas Wouters52152252000-08-17 22:55:00 +00001982 case IMPORT_STAR:
1983 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001984 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001985 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001986 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001987 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001988 break;
1989 }
Thomas Wouters52152252000-08-17 22:55:00 +00001990 err = import_all_from(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001991 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00001992 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001993 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001994 break;
Guido van Rossum25831651993-05-19 14:50:45 +00001995
Thomas Wouters52152252000-08-17 22:55:00 +00001996 case IMPORT_FROM:
Skip Montanaro496e6582002-08-06 17:47:40 +00001997 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00001998 v = TOP();
1999 x = import_from(v, w);
2000 PUSH(x);
2001 if (x != NULL) continue;
2002 break;
2003
Guido van Rossum374a9221991-04-04 10:40:29 +00002004 case JUMP_FORWARD:
2005 JUMPBY(oparg);
Raymond Hettinger080cb322003-03-14 01:37:42 +00002006 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002007
Raymond Hettingerf606f872003-03-16 03:11:04 +00002008 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002009 case JUMP_IF_FALSE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002010 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002011 if (w == Py_True) {
2012 PREDICT(POP_TOP);
Raymond Hettinger080cb322003-03-14 01:37:42 +00002013 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002014 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002015 if (w == Py_False) {
2016 JUMPBY(oparg);
Raymond Hettinger080cb322003-03-14 01:37:42 +00002017 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002018 }
2019 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002020 if (err > 0)
2021 err = 0;
2022 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00002023 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002024 else
2025 break;
2026 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002027
Raymond Hettingerf606f872003-03-16 03:11:04 +00002028 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002029 case JUMP_IF_TRUE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002030 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002031 if (w == Py_False) {
2032 PREDICT(POP_TOP);
Raymond Hettinger080cb322003-03-14 01:37:42 +00002033 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002034 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002035 if (w == Py_True) {
2036 JUMPBY(oparg);
Raymond Hettinger080cb322003-03-14 01:37:42 +00002037 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002038 }
2039 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002040 if (err > 0) {
2041 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00002042 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002043 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002044 else if (err == 0)
2045 ;
2046 else
2047 break;
2048 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002049
Guido van Rossum374a9221991-04-04 10:40:29 +00002050 case JUMP_ABSOLUTE:
2051 JUMPTO(oparg);
Raymond Hettinger080cb322003-03-14 01:37:42 +00002052 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002053
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002054 case GET_ITER:
2055 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002056 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002057 x = PyObject_GetIter(v);
2058 Py_DECREF(v);
2059 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002060 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002061 PREDICT(FOR_ITER);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002062 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002063 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002064 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002065 break;
2066
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002067 PREDICTED_WITH_ARG(FOR_ITER);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002068 case FOR_ITER:
2069 /* before: [iter]; after: [iter, iter()] *or* [] */
2070 v = TOP();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002071 x = PyIter_Next(v);
2072 if (x != NULL) {
2073 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002074 PREDICT(STORE_FAST);
2075 PREDICT(UNPACK_SEQUENCE);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002076 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002077 }
Tim Petersf4848da2001-05-05 00:14:56 +00002078 if (!PyErr_Occurred()) {
2079 /* iterator ended normally */
2080 x = v = POP();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002081 Py_DECREF(v);
2082 JUMPBY(oparg);
2083 continue;
2084 }
2085 break;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002086
Guido van Rossum374a9221991-04-04 10:40:29 +00002087 case SETUP_LOOP:
2088 case SETUP_EXCEPT:
2089 case SETUP_FINALLY:
Guido van Rossumb209a111997-04-29 18:18:01 +00002090 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002091 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002092 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002093
Guido van Rossumf10570b1995-07-07 22:53:21 +00002094 case CALL_FUNCTION:
Jeremy Hylton985eba52003-02-05 23:13:00 +00002095 PCALL(PCALL_ALL);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002096 x = call_function(&stack_pointer, oparg);
2097 PUSH(x);
2098 if (x != NULL)
2099 continue;
2100 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002101
Jeremy Hylton76901512000-03-28 23:49:17 +00002102 case CALL_FUNCTION_VAR:
2103 case CALL_FUNCTION_KW:
2104 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002105 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002106 int na = oparg & 0xff;
2107 int nk = (oparg>>8) & 0xff;
2108 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002109 int n = na + 2 * nk;
2110 PyObject **pfunc, *func;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002111 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002112 if (flags & CALL_FLAG_VAR)
2113 n++;
2114 if (flags & CALL_FLAG_KW)
2115 n++;
2116 pfunc = stack_pointer - n - 1;
2117 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002118
Guido van Rossumac7be682001-01-17 15:42:30 +00002119 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002120 && PyMethod_GET_SELF(func) != NULL) {
2121 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002122 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002123 func = PyMethod_GET_FUNCTION(func);
2124 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002125 Py_DECREF(*pfunc);
2126 *pfunc = self;
2127 na++;
2128 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002129 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002130 Py_INCREF(func);
2131 x = ext_do_call(func, &stack_pointer, flags, na, nk);
Jeremy Hylton76901512000-03-28 23:49:17 +00002132 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002133
Jeremy Hylton76901512000-03-28 23:49:17 +00002134 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002135 w = POP();
2136 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002137 }
2138 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002139 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002140 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002141 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002142 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002143
Guido van Rossum681d79a1995-07-18 14:51:37 +00002144 case MAKE_FUNCTION:
2145 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002146 x = PyFunction_New(v, f->f_globals);
2147 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002148 /* XXX Maybe this should be a separate opcode? */
2149 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002150 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002151 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002152 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002153 x = NULL;
2154 break;
2155 }
2156 while (--oparg >= 0) {
2157 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002158 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002159 }
2160 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002161 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002162 }
2163 PUSH(x);
2164 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002165
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002166 case MAKE_CLOSURE:
2167 {
2168 int nfree;
2169 v = POP(); /* code object */
2170 x = PyFunction_New(v, f->f_globals);
Jeremy Hylton733c8932001-12-13 19:51:56 +00002171 nfree = PyCode_GetNumFree((PyCodeObject *)v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002172 Py_DECREF(v);
2173 /* XXX Maybe this should be a separate opcode? */
2174 if (x != NULL && nfree > 0) {
2175 v = PyTuple_New(nfree);
2176 if (v == NULL) {
2177 Py_DECREF(x);
2178 x = NULL;
2179 break;
2180 }
2181 while (--nfree >= 0) {
2182 w = POP();
2183 PyTuple_SET_ITEM(v, nfree, w);
2184 }
2185 err = PyFunction_SetClosure(x, v);
2186 Py_DECREF(v);
2187 }
2188 if (x != NULL && oparg > 0) {
2189 v = PyTuple_New(oparg);
2190 if (v == NULL) {
2191 Py_DECREF(x);
2192 x = NULL;
2193 break;
2194 }
2195 while (--oparg >= 0) {
2196 w = POP();
2197 PyTuple_SET_ITEM(v, oparg, w);
2198 }
2199 err = PyFunction_SetDefaults(x, v);
2200 Py_DECREF(v);
2201 }
2202 PUSH(x);
2203 break;
2204 }
2205
Guido van Rossum8861b741996-07-30 16:49:37 +00002206 case BUILD_SLICE:
2207 if (oparg == 3)
2208 w = POP();
2209 else
2210 w = NULL;
2211 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002212 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002213 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002214 Py_DECREF(u);
2215 Py_DECREF(v);
2216 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002217 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002218 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002219 break;
2220
Fred Drakeef8ace32000-08-24 00:32:09 +00002221 case EXTENDED_ARG:
2222 opcode = NEXTOP();
2223 oparg = oparg<<16 | NEXTARG();
2224 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002225
Guido van Rossum374a9221991-04-04 10:40:29 +00002226 default:
2227 fprintf(stderr,
2228 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002229 PyCode_Addr2Line(f->f_code, f->f_lasti),
2230 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002231 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002232 why = WHY_EXCEPTION;
2233 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002234
2235#ifdef CASE_TOO_BIG
2236 }
2237#endif
2238
Guido van Rossum374a9221991-04-04 10:40:29 +00002239 } /* switch */
2240
2241 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002242
Guido van Rossum374a9221991-04-04 10:40:29 +00002243 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002244
Guido van Rossum374a9221991-04-04 10:40:29 +00002245 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002246 if (err == 0 && x != NULL) {
2247#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002248 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002249 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002250 fprintf(stderr,
2251 "XXX undetected error\n");
2252 else
2253#endif
2254 continue; /* Normal, fast path */
2255 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002256 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002257 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002258 err = 0;
2259 }
2260
Guido van Rossum374a9221991-04-04 10:40:29 +00002261 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002262
Guido van Rossum374a9221991-04-04 10:40:29 +00002263 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002264 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002265 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002266 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002267 why = WHY_EXCEPTION;
2268 }
2269 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002270#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002271 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002272 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002273 if (PyErr_Occurred()) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002274 fprintf(stderr,
2275 "XXX undetected error (why=%d)\n",
2276 why);
2277 why = WHY_EXCEPTION;
2278 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002279 }
2280#endif
2281
2282 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002283
Guido van Rossum374a9221991-04-04 10:40:29 +00002284 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002285 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002286
Fred Drake8f51f542001-10-04 14:48:42 +00002287 if (tstate->c_tracefunc != NULL)
2288 call_exc_trace(tstate->c_tracefunc,
2289 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002290 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002291
Guido van Rossum374a9221991-04-04 10:40:29 +00002292 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002293
Guido van Rossum374a9221991-04-04 10:40:29 +00002294 if (why == WHY_RERAISE)
2295 why = WHY_EXCEPTION;
2296
2297 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002298
Tim Peters5ca576e2001-06-18 22:08:13 +00002299 while (why != WHY_NOT && why != WHY_YIELD && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002300 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002301
2302 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2303 /* For a continue inside a try block,
2304 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002305 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2306 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002307 why = WHY_NOT;
2308 JUMPTO(PyInt_AS_LONG(retval));
2309 Py_DECREF(retval);
2310 break;
2311 }
2312
Guido van Rossum374a9221991-04-04 10:40:29 +00002313 while (STACK_LEVEL() > b->b_level) {
2314 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002315 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002316 }
2317 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2318 why = WHY_NOT;
2319 JUMPTO(b->b_handler);
2320 break;
2321 }
2322 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002323 (b->b_type == SETUP_EXCEPT &&
2324 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002325 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002326 PyObject *exc, *val, *tb;
2327 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002328 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002329 val = Py_None;
2330 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002331 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002332 /* Make the raw exception data
2333 available to the handler,
2334 so a program can emulate the
2335 Python main loop. Don't do
2336 this for 'finally'. */
2337 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002338 PyErr_NormalizeException(
2339 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002340 set_exc_info(tstate,
2341 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002342 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002343 if (tb == NULL) {
2344 Py_INCREF(Py_None);
2345 PUSH(Py_None);
2346 } else
2347 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002348 PUSH(val);
2349 PUSH(exc);
2350 }
2351 else {
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002352 if (why == WHY_RETURN ||
Guido van Rossumc5fe5eb2002-06-12 03:45:21 +00002353 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00002354 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002355 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002356 PUSH(v);
2357 }
2358 why = WHY_NOT;
2359 JUMPTO(b->b_handler);
2360 break;
2361 }
2362 } /* unwind stack */
2363
2364 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002365
Guido van Rossum374a9221991-04-04 10:40:29 +00002366 if (why != WHY_NOT)
2367 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002368
Guido van Rossum374a9221991-04-04 10:40:29 +00002369 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002370
Guido van Rossum35974fb2001-12-06 21:28:18 +00002371 if (why != WHY_YIELD) {
2372 /* Pop remaining stack entries -- but when yielding */
2373 while (!EMPTY()) {
2374 v = POP();
2375 Py_XDECREF(v);
2376 }
2377 }
2378
Tim Peters5ca576e2001-06-18 22:08:13 +00002379 if (why != WHY_RETURN && why != WHY_YIELD)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002380 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002381
Fred Drake9e3ad782001-07-03 23:39:52 +00002382 if (tstate->use_tracing) {
2383 if (tstate->c_tracefunc
2384 && (why == WHY_RETURN || why == WHY_YIELD)) {
2385 if (call_trace(tstate->c_tracefunc,
2386 tstate->c_traceobj, f,
2387 PyTrace_RETURN, retval)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002388 Py_XDECREF(retval);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002389 retval = NULL;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002390 why = WHY_EXCEPTION;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002391 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002392 }
Fred Drake8f51f542001-10-04 14:48:42 +00002393 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002394 if (why == WHY_EXCEPTION)
2395 call_trace_protected(tstate->c_profilefunc,
2396 tstate->c_profileobj, f,
2397 PyTrace_RETURN);
2398 else if (call_trace(tstate->c_profilefunc,
2399 tstate->c_profileobj, f,
2400 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002401 Py_XDECREF(retval);
2402 retval = NULL;
2403 why = WHY_EXCEPTION;
2404 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002405 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002406 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002407
Guido van Rossuma027efa1997-05-05 20:56:21 +00002408 reset_exc_info(tstate);
2409
Tim Peters5ca576e2001-06-18 22:08:13 +00002410 /* pop frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002411 --tstate->recursion_depth;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002412 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002413
Guido van Rossum96a42c81992-01-12 02:29:51 +00002414 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002415}
2416
Tim Peters6d6c1a32001-08-02 04:15:00 +00002417PyObject *
2418PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002419 PyObject **args, int argcount, PyObject **kws, int kwcount,
2420 PyObject **defs, int defcount, PyObject *closure)
2421{
2422 register PyFrameObject *f;
2423 register PyObject *retval = NULL;
2424 register PyObject **fastlocals, **freevars;
2425 PyThreadState *tstate = PyThreadState_GET();
2426 PyObject *x, *u;
2427
2428 if (globals == NULL) {
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002429 PyErr_SetString(PyExc_SystemError,
2430 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002431 return NULL;
2432 }
2433
Jeremy Hylton985eba52003-02-05 23:13:00 +00002434 assert(globals != NULL);
2435 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002436 if (f == NULL)
2437 return NULL;
2438
2439 fastlocals = f->f_localsplus;
2440 freevars = f->f_localsplus + f->f_nlocals;
2441
2442 if (co->co_argcount > 0 ||
2443 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2444 int i;
2445 int n = argcount;
2446 PyObject *kwdict = NULL;
2447 if (co->co_flags & CO_VARKEYWORDS) {
2448 kwdict = PyDict_New();
2449 if (kwdict == NULL)
2450 goto fail;
2451 i = co->co_argcount;
2452 if (co->co_flags & CO_VARARGS)
2453 i++;
2454 SETLOCAL(i, kwdict);
2455 }
2456 if (argcount > co->co_argcount) {
2457 if (!(co->co_flags & CO_VARARGS)) {
2458 PyErr_Format(PyExc_TypeError,
2459 "%.200s() takes %s %d "
2460 "%sargument%s (%d given)",
2461 PyString_AsString(co->co_name),
2462 defcount ? "at most" : "exactly",
2463 co->co_argcount,
2464 kwcount ? "non-keyword " : "",
2465 co->co_argcount == 1 ? "" : "s",
2466 argcount);
2467 goto fail;
2468 }
2469 n = co->co_argcount;
2470 }
2471 for (i = 0; i < n; i++) {
2472 x = args[i];
2473 Py_INCREF(x);
2474 SETLOCAL(i, x);
2475 }
2476 if (co->co_flags & CO_VARARGS) {
2477 u = PyTuple_New(argcount - n);
2478 if (u == NULL)
2479 goto fail;
2480 SETLOCAL(co->co_argcount, u);
2481 for (i = n; i < argcount; i++) {
2482 x = args[i];
2483 Py_INCREF(x);
2484 PyTuple_SET_ITEM(u, i-n, x);
2485 }
2486 }
2487 for (i = 0; i < kwcount; i++) {
2488 PyObject *keyword = kws[2*i];
2489 PyObject *value = kws[2*i + 1];
2490 int j;
2491 if (keyword == NULL || !PyString_Check(keyword)) {
2492 PyErr_Format(PyExc_TypeError,
2493 "%.200s() keywords must be strings",
2494 PyString_AsString(co->co_name));
2495 goto fail;
2496 }
2497 /* XXX slow -- speed up using dictionary? */
2498 for (j = 0; j < co->co_argcount; j++) {
2499 PyObject *nm = PyTuple_GET_ITEM(
2500 co->co_varnames, j);
2501 int cmp = PyObject_RichCompareBool(
2502 keyword, nm, Py_EQ);
2503 if (cmp > 0)
2504 break;
2505 else if (cmp < 0)
2506 goto fail;
2507 }
2508 /* Check errors from Compare */
2509 if (PyErr_Occurred())
2510 goto fail;
2511 if (j >= co->co_argcount) {
2512 if (kwdict == NULL) {
2513 PyErr_Format(PyExc_TypeError,
2514 "%.200s() got an unexpected "
2515 "keyword argument '%.400s'",
2516 PyString_AsString(co->co_name),
2517 PyString_AsString(keyword));
2518 goto fail;
2519 }
2520 PyDict_SetItem(kwdict, keyword, value);
2521 }
2522 else {
2523 if (GETLOCAL(j) != NULL) {
2524 PyErr_Format(PyExc_TypeError,
2525 "%.200s() got multiple "
2526 "values for keyword "
2527 "argument '%.400s'",
2528 PyString_AsString(co->co_name),
2529 PyString_AsString(keyword));
2530 goto fail;
2531 }
2532 Py_INCREF(value);
2533 SETLOCAL(j, value);
2534 }
2535 }
2536 if (argcount < co->co_argcount) {
2537 int m = co->co_argcount - defcount;
2538 for (i = argcount; i < m; i++) {
2539 if (GETLOCAL(i) == NULL) {
2540 PyErr_Format(PyExc_TypeError,
2541 "%.200s() takes %s %d "
2542 "%sargument%s (%d given)",
2543 PyString_AsString(co->co_name),
2544 ((co->co_flags & CO_VARARGS) ||
2545 defcount) ? "at least"
2546 : "exactly",
2547 m, kwcount ? "non-keyword " : "",
2548 m == 1 ? "" : "s", i);
2549 goto fail;
2550 }
2551 }
2552 if (n > m)
2553 i = n - m;
2554 else
2555 i = 0;
2556 for (; i < defcount; i++) {
2557 if (GETLOCAL(m+i) == NULL) {
2558 PyObject *def = defs[i];
2559 Py_INCREF(def);
2560 SETLOCAL(m+i, def);
2561 }
2562 }
2563 }
2564 }
2565 else {
2566 if (argcount > 0 || kwcount > 0) {
2567 PyErr_Format(PyExc_TypeError,
2568 "%.200s() takes no arguments (%d given)",
2569 PyString_AsString(co->co_name),
2570 argcount + kwcount);
2571 goto fail;
2572 }
2573 }
2574 /* Allocate and initialize storage for cell vars, and copy free
2575 vars into frame. This isn't too efficient right now. */
2576 if (f->f_ncells) {
2577 int i = 0, j = 0, nargs, found;
2578 char *cellname, *argname;
2579 PyObject *c;
2580
2581 nargs = co->co_argcount;
2582 if (co->co_flags & CO_VARARGS)
2583 nargs++;
2584 if (co->co_flags & CO_VARKEYWORDS)
2585 nargs++;
2586
2587 /* Check for cells that shadow args */
2588 for (i = 0; i < f->f_ncells && j < nargs; ++i) {
2589 cellname = PyString_AS_STRING(
2590 PyTuple_GET_ITEM(co->co_cellvars, i));
2591 found = 0;
2592 while (j < nargs) {
2593 argname = PyString_AS_STRING(
2594 PyTuple_GET_ITEM(co->co_varnames, j));
2595 if (strcmp(cellname, argname) == 0) {
2596 c = PyCell_New(GETLOCAL(j));
2597 if (c == NULL)
2598 goto fail;
2599 GETLOCAL(f->f_nlocals + i) = c;
2600 found = 1;
2601 break;
2602 }
2603 j++;
2604 }
2605 if (found == 0) {
2606 c = PyCell_New(NULL);
2607 if (c == NULL)
2608 goto fail;
2609 SETLOCAL(f->f_nlocals + i, c);
2610 }
2611 }
2612 /* Initialize any that are left */
2613 while (i < f->f_ncells) {
2614 c = PyCell_New(NULL);
2615 if (c == NULL)
2616 goto fail;
2617 SETLOCAL(f->f_nlocals + i, c);
2618 i++;
2619 }
2620 }
2621 if (f->f_nfreevars) {
2622 int i;
2623 for (i = 0; i < f->f_nfreevars; ++i) {
2624 PyObject *o = PyTuple_GET_ITEM(closure, i);
2625 Py_INCREF(o);
2626 freevars[f->f_ncells + i] = o;
2627 }
2628 }
2629
Tim Peters5ca576e2001-06-18 22:08:13 +00002630 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002631 /* Don't need to keep the reference to f_back, it will be set
2632 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002633 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002634 f->f_back = NULL;
2635
Jeremy Hylton985eba52003-02-05 23:13:00 +00002636 PCALL(PCALL_GENERATOR);
2637
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002638 /* Create a new generator that owns the ready to run frame
2639 * and return that as the value. */
Tim Peters5ca576e2001-06-18 22:08:13 +00002640 return gen_new(f);
2641 }
2642
2643 retval = eval_frame(f);
2644
2645 fail: /* Jump here from prelude on failure */
2646
Tim Petersb13680b2001-11-27 23:29:29 +00002647 /* decref'ing the frame can cause __del__ methods to get invoked,
2648 which can call back into Python. While we're done with the
2649 current Python frame (f), the associated C stack is still in use,
2650 so recursion_depth must be boosted for the duration.
2651 */
2652 assert(tstate != NULL);
2653 ++tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002654 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00002655 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002656 return retval;
2657}
2658
2659
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002660/* Implementation notes for set_exc_info() and reset_exc_info():
2661
2662- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2663 'exc_traceback'. These always travel together.
2664
2665- tstate->curexc_ZZZ is the "hot" exception that is set by
2666 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2667
2668- Once an exception is caught by an except clause, it is transferred
2669 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2670 can pick it up. This is the primary task of set_exc_info().
2671
2672- Now let me explain the complicated dance with frame->f_exc_ZZZ.
2673
2674 Long ago, when none of this existed, there were just a few globals:
2675 one set corresponding to the "hot" exception, and one set
2676 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2677 globals; they were simply stored as sys.exc_ZZZ. For backwards
2678 compatibility, they still are!) The problem was that in code like
2679 this:
2680
2681 try:
2682 "something that may fail"
2683 except "some exception":
2684 "do something else first"
2685 "print the exception from sys.exc_ZZZ."
2686
2687 if "do something else first" invoked something that raised and caught
2688 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2689 cause of subtle bugs. I fixed this by changing the semantics as
2690 follows:
2691
2692 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2693 *in that frame*.
2694
2695 - But initially, and as long as no exception is caught in a given
2696 frame, sys.exc_ZZZ will hold the last exception caught in the
2697 previous frame (or the frame before that, etc.).
2698
2699 The first bullet fixed the bug in the above example. The second
2700 bullet was for backwards compatibility: it was (and is) common to
2701 have a function that is called when an exception is caught, and to
2702 have that function access the caught exception via sys.exc_ZZZ.
2703 (Example: traceback.print_exc()).
2704
2705 At the same time I fixed the problem that sys.exc_ZZZ weren't
2706 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2707 but that's really a separate improvement.
2708
2709 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2710 variables to what they were before the current frame was called. The
2711 set_exc_info() function saves them on the frame so that
2712 reset_exc_info() can restore them. The invariant is that
2713 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2714 exception (where "catching" an exception applies only to successful
2715 except clauses); and if the current frame ever caught an exception,
2716 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2717 at the start of the current frame.
2718
2719*/
2720
Guido van Rossuma027efa1997-05-05 20:56:21 +00002721static void
Guido van Rossumac7be682001-01-17 15:42:30 +00002722set_exc_info(PyThreadState *tstate,
2723 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002724{
2725 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002726 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00002727
Guido van Rossuma027efa1997-05-05 20:56:21 +00002728 frame = tstate->frame;
2729 if (frame->f_exc_type == NULL) {
2730 /* This frame didn't catch an exception before */
2731 /* Save previous exception of this thread in this frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002732 if (tstate->exc_type == NULL) {
2733 Py_INCREF(Py_None);
2734 tstate->exc_type = Py_None;
2735 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002736 tmp_type = frame->f_exc_type;
2737 tmp_value = frame->f_exc_value;
2738 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002739 Py_XINCREF(tstate->exc_type);
2740 Py_XINCREF(tstate->exc_value);
2741 Py_XINCREF(tstate->exc_traceback);
2742 frame->f_exc_type = tstate->exc_type;
2743 frame->f_exc_value = tstate->exc_value;
2744 frame->f_exc_traceback = tstate->exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002745 Py_XDECREF(tmp_type);
2746 Py_XDECREF(tmp_value);
2747 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002748 }
2749 /* Set new exception for this thread */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002750 tmp_type = tstate->exc_type;
2751 tmp_value = tstate->exc_value;
2752 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002753 Py_XINCREF(type);
2754 Py_XINCREF(value);
2755 Py_XINCREF(tb);
2756 tstate->exc_type = type;
2757 tstate->exc_value = value;
2758 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002759 Py_XDECREF(tmp_type);
2760 Py_XDECREF(tmp_value);
2761 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002762 /* For b/w compatibility */
2763 PySys_SetObject("exc_type", type);
2764 PySys_SetObject("exc_value", value);
2765 PySys_SetObject("exc_traceback", tb);
2766}
2767
2768static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002769reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002770{
2771 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002772 PyObject *tmp_type, *tmp_value, *tmp_tb;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002773 frame = tstate->frame;
2774 if (frame->f_exc_type != NULL) {
2775 /* This frame caught an exception */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002776 tmp_type = tstate->exc_type;
2777 tmp_value = tstate->exc_value;
2778 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002779 Py_XINCREF(frame->f_exc_type);
2780 Py_XINCREF(frame->f_exc_value);
2781 Py_XINCREF(frame->f_exc_traceback);
2782 tstate->exc_type = frame->f_exc_type;
2783 tstate->exc_value = frame->f_exc_value;
2784 tstate->exc_traceback = frame->f_exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002785 Py_XDECREF(tmp_type);
2786 Py_XDECREF(tmp_value);
2787 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002788 /* For b/w compatibility */
2789 PySys_SetObject("exc_type", frame->f_exc_type);
2790 PySys_SetObject("exc_value", frame->f_exc_value);
2791 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
2792 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002793 tmp_type = frame->f_exc_type;
2794 tmp_value = frame->f_exc_value;
2795 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002796 frame->f_exc_type = NULL;
2797 frame->f_exc_value = NULL;
2798 frame->f_exc_traceback = NULL;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002799 Py_XDECREF(tmp_type);
2800 Py_XDECREF(tmp_value);
2801 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002802}
2803
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002804/* Logic for the raise statement (too complicated for inlining).
2805 This *consumes* a reference count to each of its arguments. */
Guido van Rossum1aa14831997-01-21 05:34:20 +00002806static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002807do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002808{
Guido van Rossumd295f121998-04-09 21:39:57 +00002809 if (type == NULL) {
2810 /* Reraise */
2811 PyThreadState *tstate = PyThreadState_Get();
2812 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
2813 value = tstate->exc_value;
2814 tb = tstate->exc_traceback;
2815 Py_XINCREF(type);
2816 Py_XINCREF(value);
2817 Py_XINCREF(tb);
2818 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002819
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002820 /* We support the following forms of raise:
2821 raise <class>, <classinstance>
2822 raise <class>, <argument tuple>
2823 raise <class>, None
2824 raise <class>, <argument>
2825 raise <classinstance>, None
2826 raise <string>, <object>
2827 raise <string>, None
2828
2829 An omitted second argument is the same as None.
2830
2831 In addition, raise <tuple>, <anything> is the same as
2832 raising the tuple's first item (and it better have one!);
2833 this rule is applied recursively.
2834
2835 Finally, an optional third argument can be supplied, which
2836 gives the traceback to be substituted (useful when
2837 re-raising an exception after examining it). */
2838
2839 /* First, check the traceback argument, replacing None with
2840 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002841 if (tb == Py_None) {
2842 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002843 tb = NULL;
2844 }
2845 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002846 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002847 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002848 goto raise_error;
2849 }
2850
2851 /* Next, replace a missing value with None */
2852 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002853 value = Py_None;
2854 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002855 }
2856
2857 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00002858 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
2859 PyObject *tmp = type;
2860 type = PyTuple_GET_ITEM(type, 0);
2861 Py_INCREF(type);
2862 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002863 }
2864
Tim Petersafb2c802002-04-18 18:06:20 +00002865 if (PyString_CheckExact(type))
2866 /* Raising builtin string is deprecated but still allowed --
2867 * do nothing. Raising an instance of a new-style str
2868 * subclass is right out. */
Neal Norwitz37aa0662003-01-10 15:31:15 +00002869 PyErr_Warn(PyExc_PendingDeprecationWarning,
2870 "raising a string exception is deprecated");
Barry Warsaw4249f541997-08-22 21:26:19 +00002871
2872 else if (PyClass_Check(type))
2873 PyErr_NormalizeException(&type, &value, &tb);
2874
Guido van Rossumb209a111997-04-29 18:18:01 +00002875 else if (PyInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002876 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002877 if (value != Py_None) {
2878 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002879 "instance exception may not have a separate value");
2880 goto raise_error;
2881 }
2882 else {
2883 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00002884 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002885 value = type;
Guido van Rossumb209a111997-04-29 18:18:01 +00002886 type = (PyObject*) ((PyInstanceObject*)type)->in_class;
2887 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002888 }
2889 }
2890 else {
2891 /* Not something you can raise. You get an exception
2892 anyway, just not what you specified :-) */
Jeremy Hylton960d9482001-04-27 02:25:33 +00002893 PyErr_Format(PyExc_TypeError,
Neal Norwitz37aa0662003-01-10 15:31:15 +00002894 "exceptions must be classes, instances, or "
2895 "strings (deprecated), not %s",
2896 type->ob_type->tp_name);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002897 goto raise_error;
2898 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002899 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002900 if (tb == NULL)
2901 return WHY_EXCEPTION;
2902 else
2903 return WHY_RERAISE;
2904 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00002905 Py_XDECREF(value);
2906 Py_XDECREF(type);
2907 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002908 return WHY_EXCEPTION;
2909}
2910
Tim Petersd6d010b2001-06-21 02:49:55 +00002911/* Iterate v argcnt times and store the results on the stack (via decreasing
2912 sp). Return 1 for success, 0 if error. */
2913
Barry Warsawe42b18f1997-08-25 22:13:04 +00002914static int
Tim Petersd6d010b2001-06-21 02:49:55 +00002915unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00002916{
Tim Petersd6d010b2001-06-21 02:49:55 +00002917 int i = 0;
2918 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00002919 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00002920
Tim Petersd6d010b2001-06-21 02:49:55 +00002921 assert(v != NULL);
2922
2923 it = PyObject_GetIter(v);
2924 if (it == NULL)
2925 goto Error;
2926
2927 for (; i < argcnt; i++) {
2928 w = PyIter_Next(it);
2929 if (w == NULL) {
2930 /* Iterator done, via error or exhaustion. */
2931 if (!PyErr_Occurred()) {
2932 PyErr_Format(PyExc_ValueError,
2933 "need more than %d value%s to unpack",
2934 i, i == 1 ? "" : "s");
2935 }
2936 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00002937 }
2938 *--sp = w;
2939 }
Tim Petersd6d010b2001-06-21 02:49:55 +00002940
2941 /* We better have exhausted the iterator now. */
2942 w = PyIter_Next(it);
2943 if (w == NULL) {
2944 if (PyErr_Occurred())
2945 goto Error;
2946 Py_DECREF(it);
2947 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00002948 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00002949 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00002950 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00002951 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00002952Error:
Barry Warsaw91010551997-08-25 22:30:51 +00002953 for (; i > 0; i--, sp++)
2954 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00002955 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00002956 return 0;
2957}
2958
2959
Guido van Rossum96a42c81992-01-12 02:29:51 +00002960#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00002961static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002962prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002963{
Guido van Rossum3f5da241990-12-20 15:06:42 +00002964 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00002965 if (PyObject_Print(v, stdout, 0) != 0)
2966 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00002967 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00002968 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002969}
Guido van Rossum3f5da241990-12-20 15:06:42 +00002970#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002971
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002972static void
Fred Drake5755ce62001-06-27 19:19:46 +00002973call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002974{
Guido van Rossumb209a111997-04-29 18:18:01 +00002975 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002976 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00002977 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00002978 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002979 value = Py_None;
2980 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00002981 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002982 arg = Py_BuildValue("(OOO)", type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002983 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002984 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002985 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002986 }
Fred Drake5755ce62001-06-27 19:19:46 +00002987 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002988 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002989 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00002990 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002991 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00002992 Py_XDECREF(type);
2993 Py_XDECREF(value);
2994 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002995 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002996}
2997
Fred Drake4ec5d562001-10-04 19:26:43 +00002998static void
2999call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3000 int what)
3001{
3002 PyObject *type, *value, *traceback;
3003 int err;
3004 PyErr_Fetch(&type, &value, &traceback);
3005 err = call_trace(func, obj, frame, what, NULL);
3006 if (err == 0)
3007 PyErr_Restore(type, value, traceback);
3008 else {
3009 Py_XDECREF(type);
3010 Py_XDECREF(value);
3011 Py_XDECREF(traceback);
3012 }
3013}
3014
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003015static int
Fred Drake5755ce62001-06-27 19:19:46 +00003016call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3017 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003018{
Fred Drake5755ce62001-06-27 19:19:46 +00003019 register PyThreadState *tstate = frame->f_tstate;
3020 int result;
3021 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003022 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003023 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003024 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003025 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003026 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3027 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003028 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003029 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003030}
3031
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003032PyObject *
3033_PyEval_CallTracing(PyObject *func, PyObject *args)
3034{
3035 PyFrameObject *frame = PyEval_GetFrame();
3036 PyThreadState *tstate = frame->f_tstate;
3037 int save_tracing = tstate->tracing;
3038 int save_use_tracing = tstate->use_tracing;
3039 PyObject *result;
3040
3041 tstate->tracing = 0;
3042 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3043 || (tstate->c_profilefunc != NULL));
3044 result = PyObject_Call(func, args, NULL);
3045 tstate->tracing = save_tracing;
3046 tstate->use_tracing = save_use_tracing;
3047 return result;
3048}
3049
Michael W. Hudson006c7522002-11-08 13:08:46 +00003050static int
Michael W. Hudson019a78e2002-11-08 12:53:11 +00003051maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003052 PyFrameObject *frame, int *instr_lb, int *instr_ub)
3053{
3054 /* The theory of SET_LINENO-less tracing.
3055
3056 In a nutshell, we use the co_lnotab field of the code object
3057 to tell when execution has moved onto a different line.
3058
3059 As mentioned above, the basic idea is so set things up so
3060 that
3061
3062 *instr_lb <= frame->f_lasti < *instr_ub
3063
3064 is true so long as execution does not change lines.
3065
3066 This is all fairly simple. Digging the information out of
3067 co_lnotab takes some work, but is conceptually clear.
3068
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003069 Somewhat harder to explain is why we don't *always* call the
3070 line trace function when the above test fails.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003071
3072 Consider this code:
3073
3074 1: def f(a):
3075 2: if a:
3076 3: print 1
3077 4: else:
3078 5: print 2
3079
3080 which compiles to this:
3081
3082 2 0 LOAD_FAST 0 (a)
3083 3 JUMP_IF_FALSE 9 (to 15)
3084 6 POP_TOP
3085
3086 3 7 LOAD_CONST 1 (1)
3087 10 PRINT_ITEM
3088 11 PRINT_NEWLINE
3089 12 JUMP_FORWARD 6 (to 21)
3090 >> 15 POP_TOP
3091
3092 5 16 LOAD_CONST 2 (2)
3093 19 PRINT_ITEM
3094 20 PRINT_NEWLINE
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003095 >> 21 LOAD_CONST 0 (None)
3096 24 RETURN_VALUE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003097
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003098 If 'a' is false, execution will jump to instruction at offset
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003099 15 and the co_lnotab will claim that execution has moved to
3100 line 3. This is at best misleading. In this case we could
3101 associate the POP_TOP with line 4, but that doesn't make
3102 sense in all cases (I think).
3103
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003104 What we do is only call the line trace function if the co_lnotab
3105 indicates we have jumped to the *start* of a line, i.e. if the
3106 current instruction offset matches the offset given for the
3107 start of a line by the co_lnotab.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003108
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003109 This also takes care of the situation where 'a' is true.
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003110 Execution will jump from instruction offset 12 to offset 21.
3111 Then the co_lnotab would imply that execution has moved to line
3112 5, which is again misleading.
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003113
3114 Why do we set f_lineno when tracing? Well, consider the code
3115 above when 'a' is true. If stepping through this with 'n' in
3116 pdb, you would stop at line 1 with a "call" type event, then
3117 line events on lines 2 and 3, then a "return" type event -- but
3118 you would be shown line 5 during this event. This is a change
3119 from the behaviour in 2.2 and before, and I've found it
3120 confusing in practice. By setting and using f_lineno when
3121 tracing, one can report a line number different from that
3122 suggested by f_lasti on this one occasion where it's desirable.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003123 */
3124
Michael W. Hudson006c7522002-11-08 13:08:46 +00003125 int result = 0;
3126
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003127 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003128 PyCodeObject* co = frame->f_code;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003129 int size, addr, line;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003130 unsigned char* p;
3131
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003132 size = PyString_GET_SIZE(co->co_lnotab) / 2;
3133 p = (unsigned char*)PyString_AS_STRING(co->co_lnotab);
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003134
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003135 addr = 0;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003136 line = co->co_firstlineno;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003137
3138 /* possible optimization: if f->f_lasti == instr_ub
3139 (likely to be a common case) then we already know
3140 instr_lb -- if we stored the matching value of p
3141 somwhere we could skip the first while loop. */
3142
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003143 /* see comments in compile.c for the description of
3144 co_lnotab. A point to remember: increments to p
3145 should come in pairs -- although we don't care about
3146 the line increments here, treating them as byte
3147 increments gets confusing, to say the least. */
3148
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003149 while (size > 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003150 if (addr + *p > frame->f_lasti)
3151 break;
3152 addr += *p++;
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003153 if (*p) *instr_lb = addr;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003154 line += *p++;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003155 --size;
3156 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003157
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003158 if (addr == frame->f_lasti) {
3159 frame->f_lineno = line;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003160 result = call_trace(func, obj, frame,
3161 PyTrace_LINE, Py_None);
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003162 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003163
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003164 if (size > 0) {
3165 while (--size >= 0) {
3166 addr += *p++;
3167 if (*p++)
3168 break;
3169 }
3170 *instr_ub = addr;
3171 }
3172 else {
3173 *instr_ub = INT_MAX;
3174 }
3175 }
Michael W. Hudson006c7522002-11-08 13:08:46 +00003176
3177 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003178}
3179
Fred Drake5755ce62001-06-27 19:19:46 +00003180void
3181PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003182{
Fred Drake5755ce62001-06-27 19:19:46 +00003183 PyThreadState *tstate = PyThreadState_Get();
3184 PyObject *temp = tstate->c_profileobj;
3185 Py_XINCREF(arg);
3186 tstate->c_profilefunc = NULL;
3187 tstate->c_profileobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003188 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003189 Py_XDECREF(temp);
3190 tstate->c_profilefunc = func;
3191 tstate->c_profileobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003192 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003193}
3194
3195void
3196PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3197{
3198 PyThreadState *tstate = PyThreadState_Get();
3199 PyObject *temp = tstate->c_traceobj;
3200 Py_XINCREF(arg);
3201 tstate->c_tracefunc = NULL;
3202 tstate->c_traceobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003203 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003204 Py_XDECREF(temp);
3205 tstate->c_tracefunc = func;
3206 tstate->c_traceobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003207 tstate->use_tracing = ((func != NULL)
3208 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003209}
3210
Guido van Rossumb209a111997-04-29 18:18:01 +00003211PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003212PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003213{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003214 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003215 if (current_frame == NULL)
Michael W. Hudson019a78e2002-11-08 12:53:11 +00003216 return PyThreadState_Get()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003217 else
3218 return current_frame->f_builtins;
3219}
3220
Guido van Rossumb209a111997-04-29 18:18:01 +00003221PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003222PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003223{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003224 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003225 if (current_frame == NULL)
3226 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003227 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003228 return current_frame->f_locals;
3229}
3230
Guido van Rossumb209a111997-04-29 18:18:01 +00003231PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003232PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003233{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003234 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003235 if (current_frame == NULL)
3236 return NULL;
3237 else
3238 return current_frame->f_globals;
3239}
3240
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003241PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003242PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003243{
Michael W. Hudson019a78e2002-11-08 12:53:11 +00003244 PyThreadState *tstate = PyThreadState_Get();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003245 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003246}
3247
Guido van Rossum6135a871995-01-09 17:53:26 +00003248int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003249PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003250{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003251 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003252 return current_frame == NULL ? 0 : current_frame->f_restricted;
3253}
3254
Guido van Rossumbe270261997-05-22 22:26:18 +00003255int
Tim Peters5ba58662001-07-16 02:29:45 +00003256PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003257{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003258 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003259 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003260
3261 if (current_frame != NULL) {
3262 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003263 const int compilerflags = codeflags & PyCF_MASK;
3264 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003265 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003266 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003267 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003268#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003269 if (codeflags & CO_GENERATOR_ALLOWED) {
3270 result = 1;
3271 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3272 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003273#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003274 }
3275 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003276}
3277
3278int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003279Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003280{
Guido van Rossumb209a111997-04-29 18:18:01 +00003281 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003282 if (f == NULL)
3283 return 0;
3284 if (!PyFile_SoftSpace(f, 0))
3285 return 0;
3286 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003287}
3288
Guido van Rossum3f5da241990-12-20 15:06:42 +00003289
Guido van Rossum681d79a1995-07-18 14:51:37 +00003290/* External interface to call any callable object.
3291 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003292
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003293#undef PyEval_CallObject
3294/* for backward compatibility: export this interface */
3295
Guido van Rossumb209a111997-04-29 18:18:01 +00003296PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003297PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003298{
Guido van Rossumb209a111997-04-29 18:18:01 +00003299 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003300}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003301#define PyEval_CallObject(func,arg) \
3302 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003303
Guido van Rossumb209a111997-04-29 18:18:01 +00003304PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003305PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003306{
Jeremy Hylton52820442001-01-03 23:52:36 +00003307 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003308
3309 if (arg == NULL)
Guido van Rossumb209a111997-04-29 18:18:01 +00003310 arg = PyTuple_New(0);
3311 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003312 PyErr_SetString(PyExc_TypeError,
3313 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003314 return NULL;
3315 }
3316 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003317 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003318
Guido van Rossumb209a111997-04-29 18:18:01 +00003319 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003320 PyErr_SetString(PyExc_TypeError,
3321 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003322 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003323 return NULL;
3324 }
3325
Tim Peters6d6c1a32001-08-02 04:15:00 +00003326 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003327 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003328 return result;
3329}
3330
Tim Peters6d6c1a32001-08-02 04:15:00 +00003331char *
3332PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003333{
3334 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003335 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003336 else if (PyFunction_Check(func))
3337 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3338 else if (PyCFunction_Check(func))
3339 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3340 else if (PyClass_Check(func))
3341 return PyString_AsString(((PyClassObject*)func)->cl_name);
3342 else if (PyInstance_Check(func)) {
3343 return PyString_AsString(
3344 ((PyInstanceObject*)func)->in_class->cl_name);
3345 } else {
3346 return func->ob_type->tp_name;
3347 }
3348}
3349
Tim Peters6d6c1a32001-08-02 04:15:00 +00003350char *
3351PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003352{
3353 if (PyMethod_Check(func))
3354 return "()";
3355 else if (PyFunction_Check(func))
3356 return "()";
3357 else if (PyCFunction_Check(func))
3358 return "()";
3359 else if (PyClass_Check(func))
3360 return " constructor";
3361 else if (PyInstance_Check(func)) {
3362 return " instance";
3363 } else {
3364 return " object";
3365 }
3366}
3367
Jeremy Hylton52820442001-01-03 23:52:36 +00003368#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
3369
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003370static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003371err_args(PyObject *func, int flags, int nargs)
3372{
3373 if (flags & METH_NOARGS)
3374 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003375 "%.200s() takes no arguments (%d given)",
Jeremy Hylton192690e2002-08-16 18:36:11 +00003376 ((PyCFunctionObject *)func)->m_ml->ml_name,
3377 nargs);
3378 else
3379 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003380 "%.200s() takes exactly one argument (%d given)",
Jeremy Hylton192690e2002-08-16 18:36:11 +00003381 ((PyCFunctionObject *)func)->m_ml->ml_name,
3382 nargs);
3383}
3384
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003385static PyObject *
3386call_function(PyObject ***pp_stack, int oparg)
3387{
3388 int na = oparg & 0xff;
3389 int nk = (oparg>>8) & 0xff;
3390 int n = na + 2 * nk;
3391 PyObject **pfunc = (*pp_stack) - n - 1;
3392 PyObject *func = *pfunc;
3393 PyObject *x, *w;
3394
Jeremy Hylton985eba52003-02-05 23:13:00 +00003395 /* Always dispatch PyCFunction first, because these are
3396 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003397 */
3398 if (PyCFunction_Check(func) && nk == 0) {
3399 int flags = PyCFunction_GET_FLAGS(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003400 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003401 if (flags & (METH_NOARGS | METH_O)) {
3402 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3403 PyObject *self = PyCFunction_GET_SELF(func);
3404 if (flags & METH_NOARGS && na == 0)
3405 x = (*meth)(self, NULL);
3406 else if (flags & METH_O && na == 1) {
3407 PyObject *arg = EXT_POP(*pp_stack);
3408 x = (*meth)(self, arg);
3409 Py_DECREF(arg);
3410 }
3411 else {
3412 err_args(func, flags, na);
3413 x = NULL;
3414 }
3415 }
3416 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003417 PyObject *callargs;
3418 callargs = load_args(pp_stack, na);
3419 x = PyCFunction_Call(func, callargs, NULL);
3420 Py_XDECREF(callargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003421 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003422 } else {
3423 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3424 /* optimize access to bound methods */
3425 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003426 PCALL(PCALL_METHOD);
3427 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003428 Py_INCREF(self);
3429 func = PyMethod_GET_FUNCTION(func);
3430 Py_INCREF(func);
3431 Py_DECREF(*pfunc);
3432 *pfunc = self;
3433 na++;
3434 n++;
3435 } else
3436 Py_INCREF(func);
3437 if (PyFunction_Check(func))
3438 x = fast_function(func, pp_stack, n, na, nk);
3439 else
3440 x = do_call(func, pp_stack, na, nk);
3441 Py_DECREF(func);
3442 }
3443
Jeremy Hylton985eba52003-02-05 23:13:00 +00003444 /* What does this do? */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003445 while ((*pp_stack) > pfunc) {
3446 w = EXT_POP(*pp_stack);
3447 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003448 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003449 }
3450 return x;
3451}
3452
Jeremy Hylton192690e2002-08-16 18:36:11 +00003453/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003454 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003455 For the simplest case -- a function that takes only positional
3456 arguments and is called with only positional arguments -- it
3457 inlines the most primitive frame setup code from
3458 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3459 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003460*/
3461
3462static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003463fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003464{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003465 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003466 PyObject *globals = PyFunction_GET_GLOBALS(func);
3467 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3468 PyObject **d = NULL;
3469 int nd = 0;
3470
Jeremy Hylton985eba52003-02-05 23:13:00 +00003471 PCALL(PCALL_FUNCTION);
3472 PCALL(PCALL_FAST_FUNCTION);
3473 if (argdefs == NULL && co->co_argcount == n &&
3474 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3475 PyFrameObject *f;
3476 PyObject *retval = NULL;
3477 PyThreadState *tstate = PyThreadState_GET();
3478 PyObject **fastlocals, **stack;
3479 int i;
3480
3481 PCALL(PCALL_FASTER_FUNCTION);
3482 assert(globals != NULL);
3483 /* XXX Perhaps we should create a specialized
3484 PyFrame_New() that doesn't take locals, but does
3485 take builtins without sanity checking them.
3486 */
3487 f = PyFrame_New(tstate, co, globals, NULL);
3488 if (f == NULL)
3489 return NULL;
3490
3491 fastlocals = f->f_localsplus;
3492 stack = (*pp_stack) - n;
3493
3494 for (i = 0; i < n; i++) {
3495 Py_INCREF(*stack);
3496 fastlocals[i] = *stack++;
3497 }
3498 retval = eval_frame(f);
3499 assert(tstate != NULL);
3500 ++tstate->recursion_depth;
3501 Py_DECREF(f);
3502 --tstate->recursion_depth;
3503 return retval;
3504 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003505 if (argdefs != NULL) {
3506 d = &PyTuple_GET_ITEM(argdefs, 0);
3507 nd = ((PyTupleObject *)argdefs)->ob_size;
3508 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003509 return PyEval_EvalCodeEx(co, globals,
3510 (PyObject *)NULL, (*pp_stack)-n, na,
3511 (*pp_stack)-2*nk, nk, d, nd,
3512 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003513}
3514
3515static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003516update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3517 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003518{
3519 PyObject *kwdict = NULL;
3520 if (orig_kwdict == NULL)
3521 kwdict = PyDict_New();
3522 else {
3523 kwdict = PyDict_Copy(orig_kwdict);
3524 Py_DECREF(orig_kwdict);
3525 }
3526 if (kwdict == NULL)
3527 return NULL;
3528 while (--nk >= 0) {
3529 int err;
3530 PyObject *value = EXT_POP(*pp_stack);
3531 PyObject *key = EXT_POP(*pp_stack);
3532 if (PyDict_GetItem(kwdict, key) != NULL) {
Guido van Rossumac7be682001-01-17 15:42:30 +00003533 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +00003534 "%.200s%s got multiple values "
Jeremy Hylton512a2372001-04-11 13:52:29 +00003535 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003536 PyEval_GetFuncName(func),
3537 PyEval_GetFuncDesc(func),
Jeremy Hylton512a2372001-04-11 13:52:29 +00003538 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003539 Py_DECREF(key);
3540 Py_DECREF(value);
3541 Py_DECREF(kwdict);
3542 return NULL;
3543 }
3544 err = PyDict_SetItem(kwdict, key, value);
3545 Py_DECREF(key);
3546 Py_DECREF(value);
3547 if (err) {
3548 Py_DECREF(kwdict);
3549 return NULL;
3550 }
3551 }
3552 return kwdict;
3553}
3554
3555static PyObject *
3556update_star_args(int nstack, int nstar, PyObject *stararg,
3557 PyObject ***pp_stack)
3558{
3559 PyObject *callargs, *w;
3560
3561 callargs = PyTuple_New(nstack + nstar);
3562 if (callargs == NULL) {
3563 return NULL;
3564 }
3565 if (nstar) {
3566 int i;
3567 for (i = 0; i < nstar; i++) {
3568 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3569 Py_INCREF(a);
3570 PyTuple_SET_ITEM(callargs, nstack + i, a);
3571 }
3572 }
3573 while (--nstack >= 0) {
3574 w = EXT_POP(*pp_stack);
3575 PyTuple_SET_ITEM(callargs, nstack, w);
3576 }
3577 return callargs;
3578}
3579
3580static PyObject *
3581load_args(PyObject ***pp_stack, int na)
3582{
3583 PyObject *args = PyTuple_New(na);
3584 PyObject *w;
3585
3586 if (args == NULL)
3587 return NULL;
3588 while (--na >= 0) {
3589 w = EXT_POP(*pp_stack);
3590 PyTuple_SET_ITEM(args, na, w);
3591 }
3592 return args;
3593}
3594
3595static PyObject *
3596do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3597{
3598 PyObject *callargs = NULL;
3599 PyObject *kwdict = NULL;
3600 PyObject *result = NULL;
3601
3602 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003603 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003604 if (kwdict == NULL)
3605 goto call_fail;
3606 }
3607 callargs = load_args(pp_stack, na);
3608 if (callargs == NULL)
3609 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003610#ifdef CALL_PROFILE
3611 /* At this point, we have to look at the type of func to
3612 update the call stats properly. Do it here so as to avoid
3613 exposing the call stats machinery outside ceval.c
3614 */
3615 if (PyFunction_Check(func))
3616 PCALL(PCALL_FUNCTION);
3617 else if (PyMethod_Check(func))
3618 PCALL(PCALL_METHOD);
3619 else if (PyType_Check(func))
3620 PCALL(PCALL_TYPE);
3621 else
3622 PCALL(PCALL_OTHER);
3623#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003624 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003625 call_fail:
3626 Py_XDECREF(callargs);
3627 Py_XDECREF(kwdict);
3628 return result;
3629}
3630
3631static PyObject *
3632ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3633{
3634 int nstar = 0;
3635 PyObject *callargs = NULL;
3636 PyObject *stararg = NULL;
3637 PyObject *kwdict = NULL;
3638 PyObject *result = NULL;
3639
3640 if (flags & CALL_FLAG_KW) {
3641 kwdict = EXT_POP(*pp_stack);
3642 if (!(kwdict && PyDict_Check(kwdict))) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003643 PyErr_Format(PyExc_TypeError,
Jeremy Hylton512a2372001-04-11 13:52:29 +00003644 "%s%s argument after ** "
3645 "must be a dictionary",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003646 PyEval_GetFuncName(func),
3647 PyEval_GetFuncDesc(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003648 goto ext_call_fail;
3649 }
3650 }
3651 if (flags & CALL_FLAG_VAR) {
3652 stararg = EXT_POP(*pp_stack);
3653 if (!PyTuple_Check(stararg)) {
3654 PyObject *t = NULL;
3655 t = PySequence_Tuple(stararg);
3656 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003657 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3658 PyErr_Format(PyExc_TypeError,
3659 "%s%s argument after * "
3660 "must be a sequence",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003661 PyEval_GetFuncName(func),
3662 PyEval_GetFuncDesc(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003663 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003664 goto ext_call_fail;
3665 }
3666 Py_DECREF(stararg);
3667 stararg = t;
3668 }
3669 nstar = PyTuple_GET_SIZE(stararg);
3670 }
3671 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003672 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003673 if (kwdict == NULL)
3674 goto ext_call_fail;
3675 }
3676 callargs = update_star_args(na, nstar, stararg, pp_stack);
3677 if (callargs == NULL)
3678 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003679#ifdef CALL_PROFILE
3680 /* At this point, we have to look at the type of func to
3681 update the call stats properly. Do it here so as to avoid
3682 exposing the call stats machinery outside ceval.c
3683 */
3684 if (PyFunction_Check(func))
3685 PCALL(PCALL_FUNCTION);
3686 else if (PyMethod_Check(func))
3687 PCALL(PCALL_METHOD);
3688 else if (PyType_Check(func))
3689 PCALL(PCALL_TYPE);
3690 else
3691 PCALL(PCALL_OTHER);
3692#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003693 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003694 ext_call_fail:
3695 Py_XDECREF(callargs);
3696 Py_XDECREF(kwdict);
3697 Py_XDECREF(stararg);
3698 return result;
3699}
3700
Guido van Rossum3b9c6671996-07-30 18:40:29 +00003701#define SLICE_ERROR_MSG \
3702 "standard sequence type does not support step size other than one"
3703
Tim Peterscb479e72001-12-16 19:11:44 +00003704/* Extract a slice index from a PyInt or PyLong, and store in *pi.
3705 Silently reduce values larger than INT_MAX to INT_MAX, and silently
3706 boost values less than -INT_MAX to 0. Return 0 on error, 1 on success.
3707*/
Tim Petersb5196382001-12-16 19:44:20 +00003708/* Note: If v is NULL, return success without storing into *pi. This
3709 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3710 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00003711*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00003712int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003713_PyEval_SliceIndex(PyObject *v, int *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003714{
Tim Petersb5196382001-12-16 19:44:20 +00003715 if (v != NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003716 long x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003717 if (PyInt_Check(v)) {
3718 x = PyInt_AsLong(v);
3719 } else if (PyLong_Check(v)) {
3720 x = PyLong_AsLong(v);
3721 if (x==-1 && PyErr_Occurred()) {
3722 PyObject *long_zero;
Guido van Rossumac7be682001-01-17 15:42:30 +00003723 int cmp;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003724
Guido van Rossumac7be682001-01-17 15:42:30 +00003725 if (!PyErr_ExceptionMatches(
3726 PyExc_OverflowError)) {
3727 /* It's not an overflow error, so just
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003728 signal an error */
Guido van Rossum20c6add2000-05-08 14:06:50 +00003729 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003730 }
3731
Guido van Rossumac7be682001-01-17 15:42:30 +00003732 /* Clear the OverflowError */
3733 PyErr_Clear();
3734
3735 /* It's an overflow error, so we need to
3736 check the sign of the long integer,
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003737 set the value to INT_MAX or -INT_MAX,
3738 and clear the error. */
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003739
3740 /* Create a long integer with a value of 0 */
Guido van Rossumac7be682001-01-17 15:42:30 +00003741 long_zero = PyLong_FromLong(0L);
Tim Peterscb479e72001-12-16 19:11:44 +00003742 if (long_zero == NULL)
3743 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003744
3745 /* Check sign */
Guido van Rossumac7be682001-01-17 15:42:30 +00003746 cmp = PyObject_RichCompareBool(v, long_zero,
3747 Py_GT);
3748 Py_DECREF(long_zero);
3749 if (cmp < 0)
3750 return 0;
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003751 else if (cmp)
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003752 x = INT_MAX;
3753 else
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003754 x = -INT_MAX;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003755 }
3756 } else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003757 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003758 "slice indices must be integers");
Guido van Rossum20c6add2000-05-08 14:06:50 +00003759 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003760 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003761 /* Truncate -- very long indices are truncated anyway */
3762 if (x > INT_MAX)
3763 x = INT_MAX;
3764 else if (x < -INT_MAX)
Michael W. Hudsoncbd6fb92002-11-06 15:17:32 +00003765 x = -INT_MAX;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003766 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003767 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00003768 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003769}
3770
Guido van Rossum50d756e2001-08-18 17:43:36 +00003771#undef ISINT
3772#define ISINT(x) ((x) == NULL || PyInt_Check(x) || PyLong_Check(x))
3773
Guido van Rossumb209a111997-04-29 18:18:01 +00003774static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003775apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003776{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003777 PyTypeObject *tp = u->ob_type;
3778 PySequenceMethods *sq = tp->tp_as_sequence;
3779
3780 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3781 int ilow = 0, ihigh = INT_MAX;
3782 if (!_PyEval_SliceIndex(v, &ilow))
3783 return NULL;
3784 if (!_PyEval_SliceIndex(w, &ihigh))
3785 return NULL;
3786 return PySequence_GetSlice(u, ilow, ihigh);
3787 }
3788 else {
3789 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00003790 if (slice != NULL) {
3791 PyObject *res = PyObject_GetItem(u, slice);
3792 Py_DECREF(slice);
3793 return res;
3794 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00003795 else
3796 return NULL;
3797 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003798}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003799
3800static int
Guido van Rossumac7be682001-01-17 15:42:30 +00003801assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
3802 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003803{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003804 PyTypeObject *tp = u->ob_type;
3805 PySequenceMethods *sq = tp->tp_as_sequence;
3806
3807 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3808 int ilow = 0, ihigh = INT_MAX;
3809 if (!_PyEval_SliceIndex(v, &ilow))
3810 return -1;
3811 if (!_PyEval_SliceIndex(w, &ihigh))
3812 return -1;
3813 if (x == NULL)
3814 return PySequence_DelSlice(u, ilow, ihigh);
3815 else
3816 return PySequence_SetSlice(u, ilow, ihigh, x);
3817 }
3818 else {
3819 PyObject *slice = PySlice_New(v, w, NULL);
3820 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00003821 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003822 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00003823 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00003824 else
Guido van Rossum354797c2001-12-03 19:45:06 +00003825 res = PyObject_DelItem(u, slice);
3826 Py_DECREF(slice);
3827 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003828 }
3829 else
3830 return -1;
3831 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003832}
3833
Guido van Rossumb209a111997-04-29 18:18:01 +00003834static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003835cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003836{
Guido van Rossumac7be682001-01-17 15:42:30 +00003837 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003838 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00003839 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003840 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003841 break;
3842 case PyCmp_IS_NOT:
3843 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003844 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003845 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003846 res = PySequence_Contains(w, v);
3847 if (res < 0)
3848 return NULL;
3849 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003850 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00003851 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003852 if (res < 0)
3853 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003854 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003855 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003856 case PyCmp_EXC_MATCH:
Barry Warsaw4249f541997-08-22 21:26:19 +00003857 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003858 break;
3859 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00003860 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003861 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003862 v = res ? Py_True : Py_False;
3863 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003864 return v;
3865}
3866
Thomas Wouters52152252000-08-17 22:55:00 +00003867static PyObject *
3868import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003869{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003870 PyObject *x;
3871
3872 x = PyObject_GetAttr(v, name);
3873 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00003874 PyErr_Format(PyExc_ImportError,
3875 "cannot import name %.230s",
3876 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003877 }
Thomas Wouters52152252000-08-17 22:55:00 +00003878 return x;
3879}
Guido van Rossumac7be682001-01-17 15:42:30 +00003880
Thomas Wouters52152252000-08-17 22:55:00 +00003881static int
3882import_all_from(PyObject *locals, PyObject *v)
3883{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003884 PyObject *all = PyObject_GetAttrString(v, "__all__");
3885 PyObject *dict, *name, *value;
3886 int skip_leading_underscores = 0;
3887 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00003888
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003889 if (all == NULL) {
3890 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3891 return -1; /* Unexpected error */
3892 PyErr_Clear();
3893 dict = PyObject_GetAttrString(v, "__dict__");
3894 if (dict == NULL) {
3895 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3896 return -1;
3897 PyErr_SetString(PyExc_ImportError,
3898 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00003899 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003900 }
3901 all = PyMapping_Keys(dict);
3902 Py_DECREF(dict);
3903 if (all == NULL)
3904 return -1;
3905 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00003906 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003907
3908 for (pos = 0, err = 0; ; pos++) {
3909 name = PySequence_GetItem(all, pos);
3910 if (name == NULL) {
3911 if (!PyErr_ExceptionMatches(PyExc_IndexError))
3912 err = -1;
3913 else
3914 PyErr_Clear();
3915 break;
3916 }
3917 if (skip_leading_underscores &&
3918 PyString_Check(name) &&
3919 PyString_AS_STRING(name)[0] == '_')
3920 {
3921 Py_DECREF(name);
3922 continue;
3923 }
3924 value = PyObject_GetAttr(v, name);
3925 if (value == NULL)
3926 err = -1;
3927 else
3928 err = PyDict_SetItem(locals, name, value);
3929 Py_DECREF(name);
3930 Py_XDECREF(value);
3931 if (err != 0)
3932 break;
3933 }
3934 Py_DECREF(all);
3935 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00003936}
3937
Guido van Rossumb209a111997-04-29 18:18:01 +00003938static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003939build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003940{
Guido van Rossum7851eea2001-09-12 19:19:18 +00003941 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003942
3943 if (PyDict_Check(methods))
3944 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00003945 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00003946 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00003947 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
3948 base = PyTuple_GET_ITEM(bases, 0);
3949 metaclass = PyObject_GetAttrString(base, "__class__");
3950 if (metaclass == NULL) {
3951 PyErr_Clear();
3952 metaclass = (PyObject *)base->ob_type;
3953 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00003954 }
3955 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00003956 else {
3957 PyObject *g = PyEval_GetGlobals();
3958 if (g != NULL && PyDict_Check(g))
3959 metaclass = PyDict_GetItemString(g, "__metaclass__");
3960 if (metaclass == NULL)
3961 metaclass = (PyObject *) &PyClass_Type;
3962 Py_INCREF(metaclass);
3963 }
3964 result = PyObject_CallFunction(metaclass, "OOO", name, bases, methods);
3965 Py_DECREF(metaclass);
3966 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00003967}
3968
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003969static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003970exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
3971 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003972{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003973 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00003974 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003975 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003976
Guido van Rossumb209a111997-04-29 18:18:01 +00003977 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
3978 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003979 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00003980 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003981 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00003982 locals = PyTuple_GetItem(prog, 2);
3983 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003984 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003985 if (globals == Py_None) {
3986 globals = PyEval_GetGlobals();
3987 if (locals == Py_None) {
3988 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00003989 plain = 1;
3990 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003991 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003992 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003993 locals = globals;
Guido van Rossumb209a111997-04-29 18:18:01 +00003994 if (!PyString_Check(prog) &&
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00003995 !PyUnicode_Check(prog) &&
Guido van Rossumb209a111997-04-29 18:18:01 +00003996 !PyCode_Check(prog) &&
3997 !PyFile_Check(prog)) {
3998 PyErr_SetString(PyExc_TypeError,
Guido van Rossumac7be682001-01-17 15:42:30 +00003999 "exec: arg 1 must be a string, file, or code object");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004000 return -1;
4001 }
Fred Drake661ea262000-10-24 19:57:45 +00004002 if (!PyDict_Check(globals)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004003 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00004004 "exec: arg 2 must be a dictionary or None");
4005 return -1;
4006 }
4007 if (!PyDict_Check(locals)) {
4008 PyErr_SetString(PyExc_TypeError,
4009 "exec: arg 3 must be a dictionary or None");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004010 return -1;
4011 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004012 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00004013 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00004014 if (PyCode_Check(prog)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +00004015 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4016 PyErr_SetString(PyExc_TypeError,
4017 "code object passed to exec may not contain free variables");
4018 return -1;
4019 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004020 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004021 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004022 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004023 FILE *fp = PyFile_AsFile(prog);
4024 char *name = PyString_AsString(PyFile_Name(prog));
Tim Peters5ba58662001-07-16 02:29:45 +00004025 PyCompilerFlags cf;
4026 cf.cf_flags = 0;
4027 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004028 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
4029 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004030 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004031 v = PyRun_File(fp, name, Py_file_input, globals,
4032 locals);
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004033 }
4034 else {
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004035 PyObject *tmp = NULL;
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004036 char *str;
Tim Peters5ba58662001-07-16 02:29:45 +00004037 PyCompilerFlags cf;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004038 cf.cf_flags = 0;
4039#ifdef Py_USING_UNICODE
4040 if (PyUnicode_Check(prog)) {
4041 tmp = PyUnicode_AsUTF8String(prog);
4042 if (tmp == NULL)
4043 return -1;
4044 prog = tmp;
4045 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4046 }
4047#endif
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004048 if (PyString_AsStringAndSize(prog, &str, NULL))
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004049 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004050 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004051 v = PyRun_StringFlags(str, Py_file_input, globals,
4052 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004053 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004054 v = PyRun_String(str, Py_file_input, globals, locals);
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004055 Py_XDECREF(tmp);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004056 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004057 if (plain)
4058 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004059 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004060 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00004061 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004062 return 0;
4063}
Guido van Rossum24c13741995-02-14 09:42:43 +00004064
Guido van Rossumac7be682001-01-17 15:42:30 +00004065static void
Paul Prescode68140d2000-08-30 20:25:01 +00004066format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4067{
4068 char *obj_str;
4069
4070 if (!obj)
4071 return;
4072
4073 obj_str = PyString_AsString(obj);
4074 if (!obj_str)
4075 return;
4076
4077 PyErr_Format(exc, format_str, obj_str);
4078}
Guido van Rossum950361c1997-01-24 13:49:28 +00004079
4080#ifdef DYNAMIC_EXECUTION_PROFILE
4081
Skip Montanarof118cb12001-10-15 20:51:38 +00004082static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004083getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004084{
4085 int i;
4086 PyObject *l = PyList_New(256);
4087 if (l == NULL) return NULL;
4088 for (i = 0; i < 256; i++) {
4089 PyObject *x = PyInt_FromLong(a[i]);
4090 if (x == NULL) {
4091 Py_DECREF(l);
4092 return NULL;
4093 }
4094 PyList_SetItem(l, i, x);
4095 }
4096 for (i = 0; i < 256; i++)
4097 a[i] = 0;
4098 return l;
4099}
4100
4101PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004102_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004103{
4104#ifndef DXPAIRS
4105 return getarray(dxp);
4106#else
4107 int i;
4108 PyObject *l = PyList_New(257);
4109 if (l == NULL) return NULL;
4110 for (i = 0; i < 257; i++) {
4111 PyObject *x = getarray(dxpairs[i]);
4112 if (x == NULL) {
4113 Py_DECREF(l);
4114 return NULL;
4115 }
4116 PyList_SetItem(l, i, x);
4117 }
4118 return l;
4119#endif
4120}
4121
4122#endif