blob: 3ea1bdc966cbf2dd7b01b67fd6c7015065f07451 [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
Neil Schemenauer63543862002-02-17 19:10:14 +0000876 case LOAD_FAST:
877 x = GETLOCAL(oparg);
878 if (x != NULL) {
879 Py_INCREF(x);
880 PUSH(x);
881 goto fast_next_opcode;
882 }
883 format_exc_check_arg(PyExc_UnboundLocalError,
884 UNBOUNDLOCAL_ERROR_MSG,
885 PyTuple_GetItem(co->co_varnames, oparg));
886 break;
887
888 case LOAD_CONST:
Skip Montanaro04d80f82002-08-04 21:03:35 +0000889 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +0000890 Py_INCREF(x);
891 PUSH(x);
892 goto fast_next_opcode;
893
Raymond Hettinger7dc52212003-03-16 20:14:44 +0000894 PREDICTED_WITH_ARG(STORE_FAST);
Neil Schemenauer63543862002-02-17 19:10:14 +0000895 case STORE_FAST:
896 v = POP();
897 SETLOCAL(oparg, v);
898 goto fast_next_opcode;
899
Raymond Hettingerf606f872003-03-16 03:11:04 +0000900 PREDICTED(POP_TOP);
Guido van Rossum374a9221991-04-04 10:40:29 +0000901 case POP_TOP:
902 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000903 Py_DECREF(v);
Neil Schemenauer63543862002-02-17 19:10:14 +0000904 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000905
Guido van Rossum374a9221991-04-04 10:40:29 +0000906 case ROT_TWO:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000907 v = TOP();
908 w = SECOND();
909 SET_TOP(w);
910 SET_SECOND(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000911 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000912
Guido van Rossum374a9221991-04-04 10:40:29 +0000913 case ROT_THREE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000914 v = TOP();
915 w = SECOND();
916 x = THIRD();
917 SET_TOP(w);
918 SET_SECOND(x);
919 SET_THIRD(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000920 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000921
Thomas Wouters434d0822000-08-24 20:11:32 +0000922 case ROT_FOUR:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000923 u = TOP();
924 v = SECOND();
925 w = THIRD();
926 x = FOURTH();
927 SET_TOP(v);
928 SET_SECOND(w);
929 SET_THIRD(x);
930 SET_FOURTH(u);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000931 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000932
Guido van Rossum374a9221991-04-04 10:40:29 +0000933 case DUP_TOP:
934 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000935 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000936 PUSH(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000937 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000938
Thomas Wouters434d0822000-08-24 20:11:32 +0000939 case DUP_TOPX:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000940 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000941 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000942 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000943 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000944 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000945 STACKADJ(2);
946 SET_TOP(x);
947 SET_SECOND(w);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000948 goto fast_next_opcode;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000949 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000950 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000951 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000952 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000953 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000954 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +0000955 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000956 STACKADJ(3);
957 SET_TOP(x);
958 SET_SECOND(w);
959 SET_THIRD(v);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000960 goto fast_next_opcode;
Thomas Wouters434d0822000-08-24 20:11:32 +0000961 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000962 Py_FatalError("invalid argument to DUP_TOPX"
963 " (bytecode corruption?)");
Tim Peters35ba6892000-10-11 07:04:49 +0000964 break;
Thomas Wouters434d0822000-08-24 20:11:32 +0000965
Guido van Rossum374a9221991-04-04 10:40:29 +0000966 case UNARY_POSITIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000967 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000968 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000969 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000970 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000971 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000972 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000973
Guido van Rossum374a9221991-04-04 10:40:29 +0000974 case UNARY_NEGATIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000975 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000976 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000977 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000978 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000979 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000980 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000981
Guido van Rossum374a9221991-04-04 10:40:29 +0000982 case UNARY_NOT:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000983 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000984 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000985 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +0000986 if (err == 0) {
987 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000988 SET_TOP(Py_True);
Guido van Rossumfc490731997-05-06 15:06:49 +0000989 continue;
990 }
991 else if (err > 0) {
992 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000993 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +0000994 err = 0;
995 continue;
996 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +0000997 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +0000998 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000999
Guido van Rossum374a9221991-04-04 10:40:29 +00001000 case UNARY_CONVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001001 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001002 x = PyObject_Repr(v);
1003 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001004 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001005 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001006 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001007
Guido van Rossum7928cd71991-10-24 14:59:31 +00001008 case UNARY_INVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001009 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001010 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001011 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001012 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001013 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001014 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001015
Guido van Rossum50564e81996-01-12 01:13:16 +00001016 case BINARY_POWER:
1017 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001018 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001019 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +00001020 Py_DECREF(v);
1021 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001022 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001023 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +00001024 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001025
Guido van Rossum374a9221991-04-04 10:40:29 +00001026 case BINARY_MULTIPLY:
1027 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001028 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001029 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001030 Py_DECREF(v);
1031 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001032 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001033 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001034 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001035
Guido van Rossum374a9221991-04-04 10:40:29 +00001036 case BINARY_DIVIDE:
Tim Peters3caca232001-12-06 06:23:26 +00001037 if (!_Py_QnewFlag) {
1038 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001039 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001040 x = PyNumber_Divide(v, w);
1041 Py_DECREF(v);
1042 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001043 SET_TOP(x);
Tim Peters3caca232001-12-06 06:23:26 +00001044 if (x != NULL) continue;
1045 break;
1046 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001047 /* -Qnew is in effect: fall through to
Tim Peters3caca232001-12-06 06:23:26 +00001048 BINARY_TRUE_DIVIDE */
1049 case BINARY_TRUE_DIVIDE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001050 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001051 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001052 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001053 Py_DECREF(v);
1054 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001055 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001056 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001057 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001058
Guido van Rossum4668b002001-08-08 05:00:18 +00001059 case BINARY_FLOOR_DIVIDE:
1060 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001061 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001062 x = PyNumber_FloorDivide(v, w);
1063 Py_DECREF(v);
1064 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001065 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001066 if (x != NULL) continue;
1067 break;
1068
Guido van Rossum374a9221991-04-04 10:40:29 +00001069 case BINARY_MODULO:
1070 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001071 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001072 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001073 Py_DECREF(v);
1074 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001075 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001076 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001077 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001078
Guido van Rossum374a9221991-04-04 10:40:29 +00001079 case BINARY_ADD:
1080 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001081 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001082 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001083 /* INLINE: int + int */
1084 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001085 a = PyInt_AS_LONG(v);
1086 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001087 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001088 if ((i^a) < 0 && (i^b) < 0)
1089 goto slow_add;
1090 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001091 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001092 else {
1093 slow_add:
Guido van Rossumc12da691997-07-17 23:12:42 +00001094 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001095 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001096 Py_DECREF(v);
1097 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001098 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001099 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001100 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001101
Guido van Rossum374a9221991-04-04 10:40:29 +00001102 case BINARY_SUBTRACT:
1103 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001104 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001105 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001106 /* INLINE: int - int */
1107 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001108 a = PyInt_AS_LONG(v);
1109 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001110 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001111 if ((i^a) < 0 && (i^~b) < 0)
1112 goto slow_sub;
1113 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001114 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001115 else {
1116 slow_sub:
Guido van Rossumc12da691997-07-17 23:12:42 +00001117 x = PyNumber_Subtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001118 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001119 Py_DECREF(v);
1120 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001121 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001122 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001123 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001124
Guido van Rossum374a9221991-04-04 10:40:29 +00001125 case BINARY_SUBSCR:
1126 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001127 v = TOP();
Tim Petersb1c46982001-10-05 20:41:38 +00001128 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001129 /* INLINE: list[int] */
1130 long i = PyInt_AsLong(w);
1131 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +00001132 i += PyList_GET_SIZE(v);
Guido van Rossumc12da691997-07-17 23:12:42 +00001133 if (i < 0 ||
Guido van Rossumfa00e951998-07-08 15:02:37 +00001134 i >= PyList_GET_SIZE(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001135 PyErr_SetString(PyExc_IndexError,
1136 "list index out of range");
1137 x = NULL;
1138 }
1139 else {
Guido van Rossumfa00e951998-07-08 15:02:37 +00001140 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001141 Py_INCREF(x);
1142 }
1143 }
1144 else
1145 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001146 Py_DECREF(v);
1147 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001148 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001149 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001150 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001151
Guido van Rossum7928cd71991-10-24 14:59:31 +00001152 case BINARY_LSHIFT:
1153 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001154 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001155 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001156 Py_DECREF(v);
1157 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001158 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001159 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001160 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001161
Guido van Rossum7928cd71991-10-24 14:59:31 +00001162 case BINARY_RSHIFT:
1163 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001164 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001165 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001166 Py_DECREF(v);
1167 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001168 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001169 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001170 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001171
Guido van Rossum7928cd71991-10-24 14:59:31 +00001172 case BINARY_AND:
1173 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001174 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001175 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001176 Py_DECREF(v);
1177 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001178 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001179 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001180 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001181
Guido van Rossum7928cd71991-10-24 14:59:31 +00001182 case BINARY_XOR:
1183 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001184 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001185 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001186 Py_DECREF(v);
1187 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001188 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001189 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001190 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001191
Guido van Rossum7928cd71991-10-24 14:59:31 +00001192 case BINARY_OR:
1193 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001194 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001195 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001196 Py_DECREF(v);
1197 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001198 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001199 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001200 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001201
1202 case INPLACE_POWER:
1203 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001204 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001205 x = PyNumber_InPlacePower(v, w, Py_None);
1206 Py_DECREF(v);
1207 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001208 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001209 if (x != NULL) continue;
1210 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001211
Thomas Wouters434d0822000-08-24 20:11:32 +00001212 case INPLACE_MULTIPLY:
1213 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001214 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001215 x = PyNumber_InPlaceMultiply(v, w);
1216 Py_DECREF(v);
1217 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001218 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001219 if (x != NULL) continue;
1220 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001221
Thomas Wouters434d0822000-08-24 20:11:32 +00001222 case INPLACE_DIVIDE:
Tim Peters54b11912001-12-25 18:49:11 +00001223 if (!_Py_QnewFlag) {
1224 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001225 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001226 x = PyNumber_InPlaceDivide(v, w);
1227 Py_DECREF(v);
1228 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001229 SET_TOP(x);
Tim Peters54b11912001-12-25 18:49:11 +00001230 if (x != NULL) continue;
1231 break;
1232 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001233 /* -Qnew is in effect: fall through to
Tim Peters54b11912001-12-25 18:49:11 +00001234 INPLACE_TRUE_DIVIDE */
1235 case INPLACE_TRUE_DIVIDE:
Thomas Wouters434d0822000-08-24 20:11:32 +00001236 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001237 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001238 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001239 Py_DECREF(v);
1240 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001241 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001242 if (x != NULL) continue;
1243 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001244
Guido van Rossum4668b002001-08-08 05:00:18 +00001245 case INPLACE_FLOOR_DIVIDE:
1246 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001247 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001248 x = PyNumber_InPlaceFloorDivide(v, w);
1249 Py_DECREF(v);
1250 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001251 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001252 if (x != NULL) continue;
1253 break;
1254
Thomas Wouters434d0822000-08-24 20:11:32 +00001255 case INPLACE_MODULO:
1256 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001257 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001258 x = PyNumber_InPlaceRemainder(v, w);
1259 Py_DECREF(v);
1260 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001261 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001262 if (x != NULL) continue;
1263 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001264
Thomas Wouters434d0822000-08-24 20:11:32 +00001265 case INPLACE_ADD:
1266 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001267 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001268 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001269 /* INLINE: int + int */
1270 register long a, b, i;
1271 a = PyInt_AS_LONG(v);
1272 b = PyInt_AS_LONG(w);
1273 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001274 if ((i^a) < 0 && (i^b) < 0)
1275 goto slow_iadd;
1276 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001277 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001278 else {
1279 slow_iadd:
Thomas Wouters434d0822000-08-24 20:11:32 +00001280 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001281 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001282 Py_DECREF(v);
1283 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001284 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001285 if (x != NULL) continue;
1286 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001287
Thomas Wouters434d0822000-08-24 20:11:32 +00001288 case INPLACE_SUBTRACT:
1289 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001290 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001291 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001292 /* INLINE: int - int */
1293 register long a, b, i;
1294 a = PyInt_AS_LONG(v);
1295 b = PyInt_AS_LONG(w);
1296 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001297 if ((i^a) < 0 && (i^~b) < 0)
1298 goto slow_isub;
1299 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001300 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001301 else {
1302 slow_isub:
Thomas Wouters434d0822000-08-24 20:11:32 +00001303 x = PyNumber_InPlaceSubtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001304 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001305 Py_DECREF(v);
1306 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001307 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001308 if (x != NULL) continue;
1309 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001310
Thomas Wouters434d0822000-08-24 20:11:32 +00001311 case INPLACE_LSHIFT:
1312 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001313 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001314 x = PyNumber_InPlaceLshift(v, w);
1315 Py_DECREF(v);
1316 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001317 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001318 if (x != NULL) continue;
1319 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001320
Thomas Wouters434d0822000-08-24 20:11:32 +00001321 case INPLACE_RSHIFT:
1322 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001323 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001324 x = PyNumber_InPlaceRshift(v, w);
1325 Py_DECREF(v);
1326 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001327 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001328 if (x != NULL) continue;
1329 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001330
Thomas Wouters434d0822000-08-24 20:11:32 +00001331 case INPLACE_AND:
1332 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001333 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001334 x = PyNumber_InPlaceAnd(v, w);
1335 Py_DECREF(v);
1336 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001337 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001338 if (x != NULL) continue;
1339 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001340
Thomas Wouters434d0822000-08-24 20:11:32 +00001341 case INPLACE_XOR:
1342 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001343 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001344 x = PyNumber_InPlaceXor(v, w);
1345 Py_DECREF(v);
1346 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001347 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001348 if (x != NULL) continue;
1349 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001350
Thomas Wouters434d0822000-08-24 20:11:32 +00001351 case INPLACE_OR:
1352 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001353 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001354 x = PyNumber_InPlaceOr(v, w);
1355 Py_DECREF(v);
1356 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001357 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001358 if (x != NULL) continue;
1359 break;
1360
Guido van Rossum374a9221991-04-04 10:40:29 +00001361 case SLICE+0:
1362 case SLICE+1:
1363 case SLICE+2:
1364 case SLICE+3:
1365 if ((opcode-SLICE) & 2)
1366 w = POP();
1367 else
1368 w = NULL;
1369 if ((opcode-SLICE) & 1)
1370 v = POP();
1371 else
1372 v = NULL;
Raymond Hettinger663004b2003-01-09 15:24:30 +00001373 u = TOP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001374 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001375 Py_DECREF(u);
1376 Py_XDECREF(v);
1377 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001378 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001379 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001380 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001381
Guido van Rossum374a9221991-04-04 10:40:29 +00001382 case STORE_SLICE+0:
1383 case STORE_SLICE+1:
1384 case STORE_SLICE+2:
1385 case STORE_SLICE+3:
1386 if ((opcode-STORE_SLICE) & 2)
1387 w = POP();
1388 else
1389 w = NULL;
1390 if ((opcode-STORE_SLICE) & 1)
1391 v = POP();
1392 else
1393 v = NULL;
1394 u = POP();
1395 t = POP();
1396 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +00001397 Py_DECREF(t);
1398 Py_DECREF(u);
1399 Py_XDECREF(v);
1400 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001401 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001402 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001403
Guido van Rossum374a9221991-04-04 10:40:29 +00001404 case DELETE_SLICE+0:
1405 case DELETE_SLICE+1:
1406 case DELETE_SLICE+2:
1407 case DELETE_SLICE+3:
1408 if ((opcode-DELETE_SLICE) & 2)
1409 w = POP();
1410 else
1411 w = NULL;
1412 if ((opcode-DELETE_SLICE) & 1)
1413 v = POP();
1414 else
1415 v = NULL;
1416 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001417 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +00001418 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00001419 Py_DECREF(u);
1420 Py_XDECREF(v);
1421 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001422 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001423 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001424
Guido van Rossum374a9221991-04-04 10:40:29 +00001425 case STORE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001426 w = TOP();
1427 v = SECOND();
1428 u = THIRD();
1429 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001430 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001431 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001432 Py_DECREF(u);
1433 Py_DECREF(v);
1434 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001435 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001436 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001437
Guido van Rossum374a9221991-04-04 10:40:29 +00001438 case DELETE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001439 w = TOP();
1440 v = SECOND();
1441 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001442 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001443 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001444 Py_DECREF(v);
1445 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001446 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001447 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001448
Guido van Rossum374a9221991-04-04 10:40:29 +00001449 case PRINT_EXPR:
1450 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001451 w = PySys_GetObject("displayhook");
1452 if (w == NULL) {
1453 PyErr_SetString(PyExc_RuntimeError,
1454 "lost sys.displayhook");
1455 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001456 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001457 }
1458 if (err == 0) {
1459 x = Py_BuildValue("(O)", v);
1460 if (x == NULL)
1461 err = -1;
1462 }
1463 if (err == 0) {
1464 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001465 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001466 if (w == NULL)
1467 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001468 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001469 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001470 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001471 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001472
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001473 case PRINT_ITEM_TO:
1474 w = stream = POP();
1475 /* fall through to PRINT_ITEM */
1476
Guido van Rossum374a9221991-04-04 10:40:29 +00001477 case PRINT_ITEM:
1478 v = POP();
Barry Warsaw093abe02000-08-29 04:56:13 +00001479 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001480 w = PySys_GetObject("stdout");
1481 if (w == NULL) {
1482 PyErr_SetString(PyExc_RuntimeError,
1483 "lost sys.stdout");
1484 err = -1;
1485 }
Guido van Rossum8f183201997-12-31 05:53:15 +00001486 }
Tim Peters8e5fd532002-03-24 19:25:00 +00001487 if (w != NULL && PyFile_SoftSpace(w, 0))
Guido van Rossumbe270261997-05-22 22:26:18 +00001488 err = PyFile_WriteString(" ", w);
1489 if (err == 0)
1490 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001491 if (err == 0) {
Tim Peters8e5fd532002-03-24 19:25:00 +00001492 /* XXX move into writeobject() ? */
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001493 if (PyString_Check(v)) {
1494 char *s = PyString_AS_STRING(v);
1495 int len = PyString_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001496 if (len == 0 ||
1497 !isspace(Py_CHARMASK(s[len-1])) ||
1498 s[len-1] == ' ')
1499 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001500 }
Martin v. Löwis8d3ce5a2001-12-18 22:36:40 +00001501#ifdef Py_USING_UNICODE
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001502 else if (PyUnicode_Check(v)) {
1503 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
1504 int len = PyUnicode_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001505 if (len == 0 ||
1506 !Py_UNICODE_ISSPACE(s[len-1]) ||
1507 s[len-1] == ' ')
1508 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001509 }
Michael W. Hudsond95c8282002-05-20 13:56:11 +00001510#endif
Tim Peters8e5fd532002-03-24 19:25:00 +00001511 else
1512 PyFile_SoftSpace(w, 1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001513 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001514 Py_DECREF(v);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001515 Py_XDECREF(stream);
1516 stream = NULL;
1517 if (err == 0)
1518 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001519 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001520
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001521 case PRINT_NEWLINE_TO:
1522 w = stream = POP();
1523 /* fall through to PRINT_NEWLINE */
1524
Guido van Rossum374a9221991-04-04 10:40:29 +00001525 case PRINT_NEWLINE:
Barry Warsaw093abe02000-08-29 04:56:13 +00001526 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001527 w = PySys_GetObject("stdout");
1528 if (w == NULL)
1529 PyErr_SetString(PyExc_RuntimeError,
1530 "lost sys.stdout");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001531 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001532 if (w != NULL) {
1533 err = PyFile_WriteString("\n", w);
1534 if (err == 0)
1535 PyFile_SoftSpace(w, 0);
1536 }
1537 Py_XDECREF(stream);
1538 stream = NULL;
Guido van Rossum374a9221991-04-04 10:40:29 +00001539 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001540
Thomas Wouters434d0822000-08-24 20:11:32 +00001541
1542#ifdef CASE_TOO_BIG
1543 default: switch (opcode) {
1544#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001545 case BREAK_LOOP:
1546 why = WHY_BREAK;
1547 break;
Guido van Rossum66b0e9c2001-03-21 19:17:22 +00001548
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00001549 case CONTINUE_LOOP:
1550 retval = PyInt_FromLong(oparg);
1551 why = WHY_CONTINUE;
1552 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001553
Guido van Rossumf10570b1995-07-07 22:53:21 +00001554 case RAISE_VARARGS:
1555 u = v = w = NULL;
1556 switch (oparg) {
1557 case 3:
1558 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001559 /* Fallthrough */
1560 case 2:
1561 v = POP(); /* value */
1562 /* Fallthrough */
1563 case 1:
1564 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001565 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001566 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001567 break;
1568 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001569 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001570 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001571 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001572 break;
1573 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001574 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001575
Guido van Rossum374a9221991-04-04 10:40:29 +00001576 case LOAD_LOCALS:
Guido van Rossum681d79a1995-07-18 14:51:37 +00001577 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001578 PyErr_SetString(PyExc_SystemError,
1579 "no locals");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001580 break;
1581 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001582 Py_INCREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001583 PUSH(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001584 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001585
Guido van Rossum374a9221991-04-04 10:40:29 +00001586 case RETURN_VALUE:
1587 retval = POP();
1588 why = WHY_RETURN;
1589 break;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001590
Tim Peters5ca576e2001-06-18 22:08:13 +00001591 case YIELD_VALUE:
1592 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001593 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001594 why = WHY_YIELD;
1595 break;
1596
1597
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001598 case EXEC_STMT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001599 w = TOP();
1600 v = SECOND();
1601 u = THIRD();
1602 STACKADJ(-3);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001603 err = exec_statement(f, u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001604 Py_DECREF(u);
1605 Py_DECREF(v);
1606 Py_DECREF(w);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001607 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001608
Guido van Rossum374a9221991-04-04 10:40:29 +00001609 case POP_BLOCK:
1610 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001611 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001612 while (STACK_LEVEL() > b->b_level) {
1613 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001614 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001615 }
1616 }
1617 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001618
Guido van Rossum374a9221991-04-04 10:40:29 +00001619 case END_FINALLY:
1620 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001621 if (PyInt_Check(v)) {
Raymond Hettinger080cb322003-03-14 01:37:42 +00001622 why = (enum why_code) PyInt_AS_LONG(v);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00001623 if (why == WHY_RETURN ||
Tim Peters5ca576e2001-06-18 22:08:13 +00001624 why == WHY_YIELD ||
Guido van Rossumc5fe5eb2002-06-12 03:45:21 +00001625 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001626 retval = POP();
1627 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001628 else if (PyString_Check(v) || PyClass_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001629 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001630 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001631 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001632 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001633 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001634 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001635 else if (v != Py_None) {
1636 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001637 "'finally' pops bad exception");
1638 why = WHY_EXCEPTION;
1639 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001640 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001641 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001642
Guido van Rossum374a9221991-04-04 10:40:29 +00001643 case BUILD_CLASS:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001644 u = TOP();
1645 v = SECOND();
1646 w = THIRD();
1647 STACKADJ(-2);
Guido van Rossum25831651993-05-19 14:50:45 +00001648 x = build_class(u, v, w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001649 SET_TOP(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001650 Py_DECREF(u);
1651 Py_DECREF(v);
1652 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001653 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001654
Guido van Rossum374a9221991-04-04 10:40:29 +00001655 case STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001656 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001657 v = POP();
Guido van Rossum681d79a1995-07-18 14:51:37 +00001658 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001659 PyErr_Format(PyExc_SystemError,
1660 "no locals found when storing %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001661 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001662 break;
1663 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001664 err = PyDict_SetItem(x, w, v);
1665 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001666 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001667
Guido van Rossum374a9221991-04-04 10:40:29 +00001668 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001669 w = GETITEM(names, oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001670 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001671 PyErr_Format(PyExc_SystemError,
1672 "no locals when deleting %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001673 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001674 break;
1675 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001676 if ((err = PyDict_DelItem(x, w)) != 0)
Guido van Rossumac7be682001-01-17 15:42:30 +00001677 format_exc_check_arg(PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001678 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001679 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001680
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001681 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001682 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001683 v = POP();
Raymond Hettinger21012b82003-02-26 18:11:50 +00001684 if (PyTuple_CheckExact(v)) {
Barry Warsawe42b18f1997-08-25 22:13:04 +00001685 if (PyTuple_Size(v) != oparg) {
1686 PyErr_SetString(PyExc_ValueError,
1687 "unpack tuple of wrong size");
1688 why = WHY_EXCEPTION;
1689 }
1690 else {
1691 for (; --oparg >= 0; ) {
1692 w = PyTuple_GET_ITEM(v, oparg);
1693 Py_INCREF(w);
1694 PUSH(w);
1695 }
1696 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001697 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00001698 else if (PyList_CheckExact(v)) {
Barry Warsawe42b18f1997-08-25 22:13:04 +00001699 if (PyList_Size(v) != oparg) {
1700 PyErr_SetString(PyExc_ValueError,
1701 "unpack list of wrong size");
1702 why = WHY_EXCEPTION;
1703 }
1704 else {
1705 for (; --oparg >= 0; ) {
1706 w = PyList_GET_ITEM(v, oparg);
1707 Py_INCREF(w);
1708 PUSH(w);
1709 }
1710 }
1711 }
Tim Petersd6d010b2001-06-21 02:49:55 +00001712 else if (unpack_iterable(v, oparg,
1713 stack_pointer + oparg))
1714 stack_pointer += oparg;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001715 else {
1716 if (PyErr_ExceptionMatches(PyExc_TypeError))
1717 PyErr_SetString(PyExc_TypeError,
1718 "unpack non-sequence");
Barry Warsawe42b18f1997-08-25 22:13:04 +00001719 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001720 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001721 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001722 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001723
Guido van Rossum374a9221991-04-04 10:40:29 +00001724 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001725 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001726 v = TOP();
1727 u = SECOND();
1728 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001729 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1730 Py_DECREF(v);
1731 Py_DECREF(u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001732 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001733
Guido van Rossum374a9221991-04-04 10:40:29 +00001734 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001735 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001736 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001737 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1738 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001739 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001740 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001741
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001742 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001743 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001744 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001745 err = PyDict_SetItem(f->f_globals, w, v);
1746 Py_DECREF(v);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001747 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001748
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001749 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001750 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001751 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001752 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001753 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001754 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001755
Guido van Rossum374a9221991-04-04 10:40:29 +00001756 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001757 w = GETITEM(names, oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001758 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001759 PyErr_Format(PyExc_SystemError,
1760 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001761 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001762 break;
1763 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001764 x = PyDict_GetItem(x, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001765 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001766 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001767 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001768 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001769 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001770 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001771 PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001772 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001773 break;
1774 }
1775 }
1776 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001777 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001778 PUSH(x);
1779 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001780
Guido van Rossum374a9221991-04-04 10:40:29 +00001781 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001782 w = GETITEM(names, oparg);
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001783 if (PyString_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00001784 /* Inline the PyDict_GetItem() calls.
1785 WARNING: this is an extreme speed hack.
1786 Do not try this at home. */
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001787 long hash = ((PyStringObject *)w)->ob_shash;
1788 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001789 PyDictObject *d;
1790 d = (PyDictObject *)(f->f_globals);
1791 x = d->ma_lookup(d, w, hash)->me_value;
1792 if (x != NULL) {
1793 Py_INCREF(x);
1794 PUSH(x);
1795 continue;
1796 }
1797 d = (PyDictObject *)(f->f_builtins);
1798 x = d->ma_lookup(d, w, hash)->me_value;
1799 if (x != NULL) {
1800 Py_INCREF(x);
1801 PUSH(x);
1802 continue;
1803 }
1804 goto load_global_error;
1805 }
1806 }
1807 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00001808 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001809 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001810 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001811 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001812 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00001813 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001814 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001815 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001816 break;
1817 }
1818 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001819 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001820 PUSH(x);
1821 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001822
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001823 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001824 x = GETLOCAL(oparg);
1825 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001826 format_exc_check_arg(
1827 PyExc_UnboundLocalError,
1828 UNBOUNDLOCAL_ERROR_MSG,
1829 PyTuple_GetItem(co->co_varnames, oparg)
1830 );
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001831 break;
1832 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00001833 SETLOCAL(oparg, NULL);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001834 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001835
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001836 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001837 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001838 Py_INCREF(x);
1839 PUSH(x);
1840 break;
1841
1842 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001843 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001844 w = PyCell_Get(x);
Jeremy Hylton2524d692001-02-05 17:23:16 +00001845 if (w == NULL) {
Jeremy Hylton76c81ee2002-07-11 16:56:38 +00001846 err = -1;
1847 /* Don't stomp existing exception */
1848 if (PyErr_Occurred())
1849 break;
Jeremy Hyltonc76770c2001-04-13 16:51:46 +00001850 if (oparg < f->f_ncells) {
Jeremy Hylton2524d692001-02-05 17:23:16 +00001851 v = PyTuple_GetItem(co->co_cellvars,
1852 oparg);
Jeremy Hyltonc76770c2001-04-13 16:51:46 +00001853 format_exc_check_arg(
1854 PyExc_UnboundLocalError,
1855 UNBOUNDLOCAL_ERROR_MSG,
1856 v);
1857 } else {
Jeremy Hylton2524d692001-02-05 17:23:16 +00001858 v = PyTuple_GetItem(
1859 co->co_freevars,
1860 oparg - f->f_ncells);
Jeremy Hyltonc76770c2001-04-13 16:51:46 +00001861 format_exc_check_arg(
1862 PyExc_NameError,
1863 UNBOUNDFREE_ERROR_MSG,
1864 v);
1865 }
Jeremy Hylton2524d692001-02-05 17:23:16 +00001866 break;
1867 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001868 PUSH(w);
1869 break;
1870
1871 case STORE_DEREF:
1872 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001873 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001874 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00001875 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001876 continue;
1877
Guido van Rossum374a9221991-04-04 10:40:29 +00001878 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001879 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001880 if (x != NULL) {
1881 for (; --oparg >= 0;) {
1882 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001883 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001884 }
1885 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001886 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001887 }
1888 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001889
Guido van Rossum374a9221991-04-04 10:40:29 +00001890 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00001891 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001892 if (x != NULL) {
1893 for (; --oparg >= 0;) {
1894 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00001895 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001896 }
1897 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001898 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001899 }
1900 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001901
Guido van Rossum374a9221991-04-04 10:40:29 +00001902 case BUILD_MAP:
Guido van Rossumb209a111997-04-29 18:18:01 +00001903 x = PyDict_New();
Guido van Rossum374a9221991-04-04 10:40:29 +00001904 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001905 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001906 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001907
Guido van Rossum374a9221991-04-04 10:40:29 +00001908 case LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001909 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001910 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001911 x = PyObject_GetAttr(v, w);
1912 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001913 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001914 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001915 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001916
Guido van Rossum374a9221991-04-04 10:40:29 +00001917 case COMPARE_OP:
1918 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001919 v = TOP();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001920 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001921 /* INLINE: cmp(int, int) */
1922 register long a, b;
1923 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001924 a = PyInt_AS_LONG(v);
1925 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001926 switch (oparg) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00001927 case PyCmp_LT: res = a < b; break;
1928 case PyCmp_LE: res = a <= b; break;
1929 case PyCmp_EQ: res = a == b; break;
1930 case PyCmp_NE: res = a != b; break;
1931 case PyCmp_GT: res = a > b; break;
1932 case PyCmp_GE: res = a >= b; break;
1933 case PyCmp_IS: res = v == w; break;
1934 case PyCmp_IS_NOT: res = v != w; break;
Guido van Rossumc12da691997-07-17 23:12:42 +00001935 default: goto slow_compare;
1936 }
1937 x = res ? Py_True : Py_False;
1938 Py_INCREF(x);
1939 }
1940 else {
1941 slow_compare:
1942 x = cmp_outcome(oparg, v, w);
1943 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001944 Py_DECREF(v);
1945 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001946 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001947 if (x == NULL) break;
1948 PREDICT(JUMP_IF_FALSE);
1949 PREDICT(JUMP_IF_TRUE);
1950 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001951
Guido van Rossum374a9221991-04-04 10:40:29 +00001952 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001953 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001954 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001955 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001956 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00001957 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001958 break;
1959 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001960 u = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001961 w = Py_BuildValue("(OOOO)",
Guido van Rossum681d79a1995-07-18 14:51:37 +00001962 w,
1963 f->f_globals,
Guido van Rossuma027efa1997-05-05 20:56:21 +00001964 f->f_locals == NULL ?
1965 Py_None : f->f_locals,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001966 u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001967 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001968 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001969 u = POP();
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001970 x = NULL;
1971 break;
1972 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001973 x = PyEval_CallObject(x, w);
1974 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001975 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001976 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001977 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001978
Thomas Wouters52152252000-08-17 22:55:00 +00001979 case IMPORT_STAR:
1980 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001981 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001982 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001983 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001984 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001985 break;
1986 }
Thomas Wouters52152252000-08-17 22:55:00 +00001987 err = import_all_from(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001988 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00001989 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001990 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001991 break;
Guido van Rossum25831651993-05-19 14:50:45 +00001992
Thomas Wouters52152252000-08-17 22:55:00 +00001993 case IMPORT_FROM:
Skip Montanaro496e6582002-08-06 17:47:40 +00001994 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00001995 v = TOP();
1996 x = import_from(v, w);
1997 PUSH(x);
1998 if (x != NULL) continue;
1999 break;
2000
Guido van Rossum374a9221991-04-04 10:40:29 +00002001 case JUMP_FORWARD:
2002 JUMPBY(oparg);
Raymond Hettinger080cb322003-03-14 01:37:42 +00002003 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002004
Raymond Hettingerf606f872003-03-16 03:11:04 +00002005 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002006 case JUMP_IF_FALSE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002007 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002008 if (w == Py_True) {
2009 PREDICT(POP_TOP);
Raymond Hettinger080cb322003-03-14 01:37:42 +00002010 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002011 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002012 if (w == Py_False) {
2013 JUMPBY(oparg);
Raymond Hettinger080cb322003-03-14 01:37:42 +00002014 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002015 }
2016 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002017 if (err > 0)
2018 err = 0;
2019 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00002020 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002021 else
2022 break;
2023 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002024
Raymond Hettingerf606f872003-03-16 03:11:04 +00002025 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002026 case JUMP_IF_TRUE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002027 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002028 if (w == Py_False) {
2029 PREDICT(POP_TOP);
Raymond Hettinger080cb322003-03-14 01:37:42 +00002030 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002031 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002032 if (w == Py_True) {
2033 JUMPBY(oparg);
Raymond Hettinger080cb322003-03-14 01:37:42 +00002034 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002035 }
2036 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002037 if (err > 0) {
2038 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00002039 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002040 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002041 else if (err == 0)
2042 ;
2043 else
2044 break;
2045 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002046
Guido van Rossum374a9221991-04-04 10:40:29 +00002047 case JUMP_ABSOLUTE:
2048 JUMPTO(oparg);
Raymond Hettinger080cb322003-03-14 01:37:42 +00002049 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002050
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002051 case GET_ITER:
2052 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002053 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002054 x = PyObject_GetIter(v);
2055 Py_DECREF(v);
2056 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002057 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002058 PREDICT(FOR_ITER);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002059 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002060 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002061 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002062 break;
2063
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002064 PREDICTED_WITH_ARG(FOR_ITER);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002065 case FOR_ITER:
2066 /* before: [iter]; after: [iter, iter()] *or* [] */
2067 v = TOP();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002068 x = PyIter_Next(v);
2069 if (x != NULL) {
2070 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002071 PREDICT(STORE_FAST);
2072 PREDICT(UNPACK_SEQUENCE);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002073 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002074 }
Tim Petersf4848da2001-05-05 00:14:56 +00002075 if (!PyErr_Occurred()) {
2076 /* iterator ended normally */
2077 x = v = POP();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002078 Py_DECREF(v);
2079 JUMPBY(oparg);
2080 continue;
2081 }
2082 break;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002083
Guido van Rossum374a9221991-04-04 10:40:29 +00002084 case SETUP_LOOP:
2085 case SETUP_EXCEPT:
2086 case SETUP_FINALLY:
Guido van Rossumb209a111997-04-29 18:18:01 +00002087 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002088 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002089 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002090
Guido van Rossumf10570b1995-07-07 22:53:21 +00002091 case CALL_FUNCTION:
Jeremy Hylton985eba52003-02-05 23:13:00 +00002092 PCALL(PCALL_ALL);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002093 x = call_function(&stack_pointer, oparg);
2094 PUSH(x);
2095 if (x != NULL)
2096 continue;
2097 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002098
Jeremy Hylton76901512000-03-28 23:49:17 +00002099 case CALL_FUNCTION_VAR:
2100 case CALL_FUNCTION_KW:
2101 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002102 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002103 int na = oparg & 0xff;
2104 int nk = (oparg>>8) & 0xff;
2105 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002106 int n = na + 2 * nk;
2107 PyObject **pfunc, *func;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002108 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002109 if (flags & CALL_FLAG_VAR)
2110 n++;
2111 if (flags & CALL_FLAG_KW)
2112 n++;
2113 pfunc = stack_pointer - n - 1;
2114 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002115
Guido van Rossumac7be682001-01-17 15:42:30 +00002116 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002117 && PyMethod_GET_SELF(func) != NULL) {
2118 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002119 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002120 func = PyMethod_GET_FUNCTION(func);
2121 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002122 Py_DECREF(*pfunc);
2123 *pfunc = self;
2124 na++;
2125 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002126 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002127 Py_INCREF(func);
2128 x = ext_do_call(func, &stack_pointer, flags, na, nk);
Jeremy Hylton76901512000-03-28 23:49:17 +00002129 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002130
Jeremy Hylton76901512000-03-28 23:49:17 +00002131 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002132 w = POP();
2133 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002134 }
2135 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002136 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002137 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002138 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002139 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002140
Guido van Rossum681d79a1995-07-18 14:51:37 +00002141 case MAKE_FUNCTION:
2142 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002143 x = PyFunction_New(v, f->f_globals);
2144 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002145 /* XXX Maybe this should be a separate opcode? */
2146 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002147 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002148 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002149 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002150 x = NULL;
2151 break;
2152 }
2153 while (--oparg >= 0) {
2154 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002155 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002156 }
2157 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002158 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002159 }
2160 PUSH(x);
2161 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002162
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002163 case MAKE_CLOSURE:
2164 {
2165 int nfree;
2166 v = POP(); /* code object */
2167 x = PyFunction_New(v, f->f_globals);
Jeremy Hylton733c8932001-12-13 19:51:56 +00002168 nfree = PyCode_GetNumFree((PyCodeObject *)v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002169 Py_DECREF(v);
2170 /* XXX Maybe this should be a separate opcode? */
2171 if (x != NULL && nfree > 0) {
2172 v = PyTuple_New(nfree);
2173 if (v == NULL) {
2174 Py_DECREF(x);
2175 x = NULL;
2176 break;
2177 }
2178 while (--nfree >= 0) {
2179 w = POP();
2180 PyTuple_SET_ITEM(v, nfree, w);
2181 }
2182 err = PyFunction_SetClosure(x, v);
2183 Py_DECREF(v);
2184 }
2185 if (x != NULL && oparg > 0) {
2186 v = PyTuple_New(oparg);
2187 if (v == NULL) {
2188 Py_DECREF(x);
2189 x = NULL;
2190 break;
2191 }
2192 while (--oparg >= 0) {
2193 w = POP();
2194 PyTuple_SET_ITEM(v, oparg, w);
2195 }
2196 err = PyFunction_SetDefaults(x, v);
2197 Py_DECREF(v);
2198 }
2199 PUSH(x);
2200 break;
2201 }
2202
Guido van Rossum8861b741996-07-30 16:49:37 +00002203 case BUILD_SLICE:
2204 if (oparg == 3)
2205 w = POP();
2206 else
2207 w = NULL;
2208 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002209 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002210 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002211 Py_DECREF(u);
2212 Py_DECREF(v);
2213 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002214 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002215 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002216 break;
2217
Fred Drakeef8ace32000-08-24 00:32:09 +00002218 case EXTENDED_ARG:
2219 opcode = NEXTOP();
2220 oparg = oparg<<16 | NEXTARG();
2221 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002222
Guido van Rossum374a9221991-04-04 10:40:29 +00002223 default:
2224 fprintf(stderr,
2225 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002226 PyCode_Addr2Line(f->f_code, f->f_lasti),
2227 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002228 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002229 why = WHY_EXCEPTION;
2230 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002231
2232#ifdef CASE_TOO_BIG
2233 }
2234#endif
2235
Guido van Rossum374a9221991-04-04 10:40:29 +00002236 } /* switch */
2237
2238 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002239
Guido van Rossum374a9221991-04-04 10:40:29 +00002240 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002241
Guido van Rossum374a9221991-04-04 10:40:29 +00002242 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002243 if (err == 0 && x != NULL) {
2244#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002245 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002246 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002247 fprintf(stderr,
2248 "XXX undetected error\n");
2249 else
2250#endif
2251 continue; /* Normal, fast path */
2252 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002253 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002254 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002255 err = 0;
2256 }
2257
Guido van Rossum374a9221991-04-04 10:40:29 +00002258 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002259
Guido van Rossum374a9221991-04-04 10:40:29 +00002260 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002261 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002262 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002263 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002264 why = WHY_EXCEPTION;
2265 }
2266 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002267#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002268 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002269 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002270 if (PyErr_Occurred()) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002271 fprintf(stderr,
2272 "XXX undetected error (why=%d)\n",
2273 why);
2274 why = WHY_EXCEPTION;
2275 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002276 }
2277#endif
2278
2279 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002280
Guido van Rossum374a9221991-04-04 10:40:29 +00002281 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002282 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002283
Fred Drake8f51f542001-10-04 14:48:42 +00002284 if (tstate->c_tracefunc != NULL)
2285 call_exc_trace(tstate->c_tracefunc,
2286 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002287 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002288
Guido van Rossum374a9221991-04-04 10:40:29 +00002289 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002290
Guido van Rossum374a9221991-04-04 10:40:29 +00002291 if (why == WHY_RERAISE)
2292 why = WHY_EXCEPTION;
2293
2294 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002295
Tim Peters5ca576e2001-06-18 22:08:13 +00002296 while (why != WHY_NOT && why != WHY_YIELD && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002297 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002298
2299 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2300 /* For a continue inside a try block,
2301 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002302 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2303 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002304 why = WHY_NOT;
2305 JUMPTO(PyInt_AS_LONG(retval));
2306 Py_DECREF(retval);
2307 break;
2308 }
2309
Guido van Rossum374a9221991-04-04 10:40:29 +00002310 while (STACK_LEVEL() > b->b_level) {
2311 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002312 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002313 }
2314 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2315 why = WHY_NOT;
2316 JUMPTO(b->b_handler);
2317 break;
2318 }
2319 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002320 (b->b_type == SETUP_EXCEPT &&
2321 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002322 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002323 PyObject *exc, *val, *tb;
2324 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002325 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002326 val = Py_None;
2327 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002328 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002329 /* Make the raw exception data
2330 available to the handler,
2331 so a program can emulate the
2332 Python main loop. Don't do
2333 this for 'finally'. */
2334 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002335 PyErr_NormalizeException(
2336 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002337 set_exc_info(tstate,
2338 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002339 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002340 if (tb == NULL) {
2341 Py_INCREF(Py_None);
2342 PUSH(Py_None);
2343 } else
2344 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002345 PUSH(val);
2346 PUSH(exc);
2347 }
2348 else {
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002349 if (why == WHY_RETURN ||
Guido van Rossumc5fe5eb2002-06-12 03:45:21 +00002350 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00002351 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002352 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002353 PUSH(v);
2354 }
2355 why = WHY_NOT;
2356 JUMPTO(b->b_handler);
2357 break;
2358 }
2359 } /* unwind stack */
2360
2361 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002362
Guido van Rossum374a9221991-04-04 10:40:29 +00002363 if (why != WHY_NOT)
2364 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002365
Guido van Rossum374a9221991-04-04 10:40:29 +00002366 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002367
Guido van Rossum35974fb2001-12-06 21:28:18 +00002368 if (why != WHY_YIELD) {
2369 /* Pop remaining stack entries -- but when yielding */
2370 while (!EMPTY()) {
2371 v = POP();
2372 Py_XDECREF(v);
2373 }
2374 }
2375
Tim Peters5ca576e2001-06-18 22:08:13 +00002376 if (why != WHY_RETURN && why != WHY_YIELD)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002377 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002378
Fred Drake9e3ad782001-07-03 23:39:52 +00002379 if (tstate->use_tracing) {
2380 if (tstate->c_tracefunc
2381 && (why == WHY_RETURN || why == WHY_YIELD)) {
2382 if (call_trace(tstate->c_tracefunc,
2383 tstate->c_traceobj, f,
2384 PyTrace_RETURN, retval)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002385 Py_XDECREF(retval);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002386 retval = NULL;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002387 why = WHY_EXCEPTION;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002388 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002389 }
Fred Drake8f51f542001-10-04 14:48:42 +00002390 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002391 if (why == WHY_EXCEPTION)
2392 call_trace_protected(tstate->c_profilefunc,
2393 tstate->c_profileobj, f,
2394 PyTrace_RETURN);
2395 else if (call_trace(tstate->c_profilefunc,
2396 tstate->c_profileobj, f,
2397 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002398 Py_XDECREF(retval);
2399 retval = NULL;
2400 why = WHY_EXCEPTION;
2401 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002402 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002403 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002404
Guido van Rossuma027efa1997-05-05 20:56:21 +00002405 reset_exc_info(tstate);
2406
Tim Peters5ca576e2001-06-18 22:08:13 +00002407 /* pop frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002408 --tstate->recursion_depth;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002409 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002410
Guido van Rossum96a42c81992-01-12 02:29:51 +00002411 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002412}
2413
Tim Peters6d6c1a32001-08-02 04:15:00 +00002414PyObject *
2415PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002416 PyObject **args, int argcount, PyObject **kws, int kwcount,
2417 PyObject **defs, int defcount, PyObject *closure)
2418{
2419 register PyFrameObject *f;
2420 register PyObject *retval = NULL;
2421 register PyObject **fastlocals, **freevars;
2422 PyThreadState *tstate = PyThreadState_GET();
2423 PyObject *x, *u;
2424
2425 if (globals == NULL) {
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002426 PyErr_SetString(PyExc_SystemError,
2427 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002428 return NULL;
2429 }
2430
Jeremy Hylton985eba52003-02-05 23:13:00 +00002431 assert(globals != NULL);
2432 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002433 if (f == NULL)
2434 return NULL;
2435
2436 fastlocals = f->f_localsplus;
2437 freevars = f->f_localsplus + f->f_nlocals;
2438
2439 if (co->co_argcount > 0 ||
2440 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2441 int i;
2442 int n = argcount;
2443 PyObject *kwdict = NULL;
2444 if (co->co_flags & CO_VARKEYWORDS) {
2445 kwdict = PyDict_New();
2446 if (kwdict == NULL)
2447 goto fail;
2448 i = co->co_argcount;
2449 if (co->co_flags & CO_VARARGS)
2450 i++;
2451 SETLOCAL(i, kwdict);
2452 }
2453 if (argcount > co->co_argcount) {
2454 if (!(co->co_flags & CO_VARARGS)) {
2455 PyErr_Format(PyExc_TypeError,
2456 "%.200s() takes %s %d "
2457 "%sargument%s (%d given)",
2458 PyString_AsString(co->co_name),
2459 defcount ? "at most" : "exactly",
2460 co->co_argcount,
2461 kwcount ? "non-keyword " : "",
2462 co->co_argcount == 1 ? "" : "s",
2463 argcount);
2464 goto fail;
2465 }
2466 n = co->co_argcount;
2467 }
2468 for (i = 0; i < n; i++) {
2469 x = args[i];
2470 Py_INCREF(x);
2471 SETLOCAL(i, x);
2472 }
2473 if (co->co_flags & CO_VARARGS) {
2474 u = PyTuple_New(argcount - n);
2475 if (u == NULL)
2476 goto fail;
2477 SETLOCAL(co->co_argcount, u);
2478 for (i = n; i < argcount; i++) {
2479 x = args[i];
2480 Py_INCREF(x);
2481 PyTuple_SET_ITEM(u, i-n, x);
2482 }
2483 }
2484 for (i = 0; i < kwcount; i++) {
2485 PyObject *keyword = kws[2*i];
2486 PyObject *value = kws[2*i + 1];
2487 int j;
2488 if (keyword == NULL || !PyString_Check(keyword)) {
2489 PyErr_Format(PyExc_TypeError,
2490 "%.200s() keywords must be strings",
2491 PyString_AsString(co->co_name));
2492 goto fail;
2493 }
2494 /* XXX slow -- speed up using dictionary? */
2495 for (j = 0; j < co->co_argcount; j++) {
2496 PyObject *nm = PyTuple_GET_ITEM(
2497 co->co_varnames, j);
2498 int cmp = PyObject_RichCompareBool(
2499 keyword, nm, Py_EQ);
2500 if (cmp > 0)
2501 break;
2502 else if (cmp < 0)
2503 goto fail;
2504 }
2505 /* Check errors from Compare */
2506 if (PyErr_Occurred())
2507 goto fail;
2508 if (j >= co->co_argcount) {
2509 if (kwdict == NULL) {
2510 PyErr_Format(PyExc_TypeError,
2511 "%.200s() got an unexpected "
2512 "keyword argument '%.400s'",
2513 PyString_AsString(co->co_name),
2514 PyString_AsString(keyword));
2515 goto fail;
2516 }
2517 PyDict_SetItem(kwdict, keyword, value);
2518 }
2519 else {
2520 if (GETLOCAL(j) != NULL) {
2521 PyErr_Format(PyExc_TypeError,
2522 "%.200s() got multiple "
2523 "values for keyword "
2524 "argument '%.400s'",
2525 PyString_AsString(co->co_name),
2526 PyString_AsString(keyword));
2527 goto fail;
2528 }
2529 Py_INCREF(value);
2530 SETLOCAL(j, value);
2531 }
2532 }
2533 if (argcount < co->co_argcount) {
2534 int m = co->co_argcount - defcount;
2535 for (i = argcount; i < m; i++) {
2536 if (GETLOCAL(i) == NULL) {
2537 PyErr_Format(PyExc_TypeError,
2538 "%.200s() takes %s %d "
2539 "%sargument%s (%d given)",
2540 PyString_AsString(co->co_name),
2541 ((co->co_flags & CO_VARARGS) ||
2542 defcount) ? "at least"
2543 : "exactly",
2544 m, kwcount ? "non-keyword " : "",
2545 m == 1 ? "" : "s", i);
2546 goto fail;
2547 }
2548 }
2549 if (n > m)
2550 i = n - m;
2551 else
2552 i = 0;
2553 for (; i < defcount; i++) {
2554 if (GETLOCAL(m+i) == NULL) {
2555 PyObject *def = defs[i];
2556 Py_INCREF(def);
2557 SETLOCAL(m+i, def);
2558 }
2559 }
2560 }
2561 }
2562 else {
2563 if (argcount > 0 || kwcount > 0) {
2564 PyErr_Format(PyExc_TypeError,
2565 "%.200s() takes no arguments (%d given)",
2566 PyString_AsString(co->co_name),
2567 argcount + kwcount);
2568 goto fail;
2569 }
2570 }
2571 /* Allocate and initialize storage for cell vars, and copy free
2572 vars into frame. This isn't too efficient right now. */
2573 if (f->f_ncells) {
2574 int i = 0, j = 0, nargs, found;
2575 char *cellname, *argname;
2576 PyObject *c;
2577
2578 nargs = co->co_argcount;
2579 if (co->co_flags & CO_VARARGS)
2580 nargs++;
2581 if (co->co_flags & CO_VARKEYWORDS)
2582 nargs++;
2583
2584 /* Check for cells that shadow args */
2585 for (i = 0; i < f->f_ncells && j < nargs; ++i) {
2586 cellname = PyString_AS_STRING(
2587 PyTuple_GET_ITEM(co->co_cellvars, i));
2588 found = 0;
2589 while (j < nargs) {
2590 argname = PyString_AS_STRING(
2591 PyTuple_GET_ITEM(co->co_varnames, j));
2592 if (strcmp(cellname, argname) == 0) {
2593 c = PyCell_New(GETLOCAL(j));
2594 if (c == NULL)
2595 goto fail;
2596 GETLOCAL(f->f_nlocals + i) = c;
2597 found = 1;
2598 break;
2599 }
2600 j++;
2601 }
2602 if (found == 0) {
2603 c = PyCell_New(NULL);
2604 if (c == NULL)
2605 goto fail;
2606 SETLOCAL(f->f_nlocals + i, c);
2607 }
2608 }
2609 /* Initialize any that are left */
2610 while (i < f->f_ncells) {
2611 c = PyCell_New(NULL);
2612 if (c == NULL)
2613 goto fail;
2614 SETLOCAL(f->f_nlocals + i, c);
2615 i++;
2616 }
2617 }
2618 if (f->f_nfreevars) {
2619 int i;
2620 for (i = 0; i < f->f_nfreevars; ++i) {
2621 PyObject *o = PyTuple_GET_ITEM(closure, i);
2622 Py_INCREF(o);
2623 freevars[f->f_ncells + i] = o;
2624 }
2625 }
2626
Tim Peters5ca576e2001-06-18 22:08:13 +00002627 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002628 /* Don't need to keep the reference to f_back, it will be set
2629 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002630 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002631 f->f_back = NULL;
2632
Jeremy Hylton985eba52003-02-05 23:13:00 +00002633 PCALL(PCALL_GENERATOR);
2634
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002635 /* Create a new generator that owns the ready to run frame
2636 * and return that as the value. */
Tim Peters5ca576e2001-06-18 22:08:13 +00002637 return gen_new(f);
2638 }
2639
2640 retval = eval_frame(f);
2641
2642 fail: /* Jump here from prelude on failure */
2643
Tim Petersb13680b2001-11-27 23:29:29 +00002644 /* decref'ing the frame can cause __del__ methods to get invoked,
2645 which can call back into Python. While we're done with the
2646 current Python frame (f), the associated C stack is still in use,
2647 so recursion_depth must be boosted for the duration.
2648 */
2649 assert(tstate != NULL);
2650 ++tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002651 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00002652 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002653 return retval;
2654}
2655
2656
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002657/* Implementation notes for set_exc_info() and reset_exc_info():
2658
2659- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2660 'exc_traceback'. These always travel together.
2661
2662- tstate->curexc_ZZZ is the "hot" exception that is set by
2663 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2664
2665- Once an exception is caught by an except clause, it is transferred
2666 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2667 can pick it up. This is the primary task of set_exc_info().
2668
2669- Now let me explain the complicated dance with frame->f_exc_ZZZ.
2670
2671 Long ago, when none of this existed, there were just a few globals:
2672 one set corresponding to the "hot" exception, and one set
2673 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2674 globals; they were simply stored as sys.exc_ZZZ. For backwards
2675 compatibility, they still are!) The problem was that in code like
2676 this:
2677
2678 try:
2679 "something that may fail"
2680 except "some exception":
2681 "do something else first"
2682 "print the exception from sys.exc_ZZZ."
2683
2684 if "do something else first" invoked something that raised and caught
2685 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2686 cause of subtle bugs. I fixed this by changing the semantics as
2687 follows:
2688
2689 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2690 *in that frame*.
2691
2692 - But initially, and as long as no exception is caught in a given
2693 frame, sys.exc_ZZZ will hold the last exception caught in the
2694 previous frame (or the frame before that, etc.).
2695
2696 The first bullet fixed the bug in the above example. The second
2697 bullet was for backwards compatibility: it was (and is) common to
2698 have a function that is called when an exception is caught, and to
2699 have that function access the caught exception via sys.exc_ZZZ.
2700 (Example: traceback.print_exc()).
2701
2702 At the same time I fixed the problem that sys.exc_ZZZ weren't
2703 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2704 but that's really a separate improvement.
2705
2706 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2707 variables to what they were before the current frame was called. The
2708 set_exc_info() function saves them on the frame so that
2709 reset_exc_info() can restore them. The invariant is that
2710 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2711 exception (where "catching" an exception applies only to successful
2712 except clauses); and if the current frame ever caught an exception,
2713 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2714 at the start of the current frame.
2715
2716*/
2717
Guido van Rossuma027efa1997-05-05 20:56:21 +00002718static void
Guido van Rossumac7be682001-01-17 15:42:30 +00002719set_exc_info(PyThreadState *tstate,
2720 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002721{
2722 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002723 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00002724
Guido van Rossuma027efa1997-05-05 20:56:21 +00002725 frame = tstate->frame;
2726 if (frame->f_exc_type == NULL) {
2727 /* This frame didn't catch an exception before */
2728 /* Save previous exception of this thread in this frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002729 if (tstate->exc_type == NULL) {
2730 Py_INCREF(Py_None);
2731 tstate->exc_type = Py_None;
2732 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002733 tmp_type = frame->f_exc_type;
2734 tmp_value = frame->f_exc_value;
2735 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002736 Py_XINCREF(tstate->exc_type);
2737 Py_XINCREF(tstate->exc_value);
2738 Py_XINCREF(tstate->exc_traceback);
2739 frame->f_exc_type = tstate->exc_type;
2740 frame->f_exc_value = tstate->exc_value;
2741 frame->f_exc_traceback = tstate->exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002742 Py_XDECREF(tmp_type);
2743 Py_XDECREF(tmp_value);
2744 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002745 }
2746 /* Set new exception for this thread */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002747 tmp_type = tstate->exc_type;
2748 tmp_value = tstate->exc_value;
2749 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002750 Py_XINCREF(type);
2751 Py_XINCREF(value);
2752 Py_XINCREF(tb);
2753 tstate->exc_type = type;
2754 tstate->exc_value = value;
2755 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002756 Py_XDECREF(tmp_type);
2757 Py_XDECREF(tmp_value);
2758 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002759 /* For b/w compatibility */
2760 PySys_SetObject("exc_type", type);
2761 PySys_SetObject("exc_value", value);
2762 PySys_SetObject("exc_traceback", tb);
2763}
2764
2765static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002766reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002767{
2768 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002769 PyObject *tmp_type, *tmp_value, *tmp_tb;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002770 frame = tstate->frame;
2771 if (frame->f_exc_type != NULL) {
2772 /* This frame caught an exception */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002773 tmp_type = tstate->exc_type;
2774 tmp_value = tstate->exc_value;
2775 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002776 Py_XINCREF(frame->f_exc_type);
2777 Py_XINCREF(frame->f_exc_value);
2778 Py_XINCREF(frame->f_exc_traceback);
2779 tstate->exc_type = frame->f_exc_type;
2780 tstate->exc_value = frame->f_exc_value;
2781 tstate->exc_traceback = frame->f_exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002782 Py_XDECREF(tmp_type);
2783 Py_XDECREF(tmp_value);
2784 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002785 /* For b/w compatibility */
2786 PySys_SetObject("exc_type", frame->f_exc_type);
2787 PySys_SetObject("exc_value", frame->f_exc_value);
2788 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
2789 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002790 tmp_type = frame->f_exc_type;
2791 tmp_value = frame->f_exc_value;
2792 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002793 frame->f_exc_type = NULL;
2794 frame->f_exc_value = NULL;
2795 frame->f_exc_traceback = NULL;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002796 Py_XDECREF(tmp_type);
2797 Py_XDECREF(tmp_value);
2798 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002799}
2800
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002801/* Logic for the raise statement (too complicated for inlining).
2802 This *consumes* a reference count to each of its arguments. */
Guido van Rossum1aa14831997-01-21 05:34:20 +00002803static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002804do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002805{
Guido van Rossumd295f121998-04-09 21:39:57 +00002806 if (type == NULL) {
2807 /* Reraise */
2808 PyThreadState *tstate = PyThreadState_Get();
2809 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
2810 value = tstate->exc_value;
2811 tb = tstate->exc_traceback;
2812 Py_XINCREF(type);
2813 Py_XINCREF(value);
2814 Py_XINCREF(tb);
2815 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002816
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002817 /* We support the following forms of raise:
2818 raise <class>, <classinstance>
2819 raise <class>, <argument tuple>
2820 raise <class>, None
2821 raise <class>, <argument>
2822 raise <classinstance>, None
2823 raise <string>, <object>
2824 raise <string>, None
2825
2826 An omitted second argument is the same as None.
2827
2828 In addition, raise <tuple>, <anything> is the same as
2829 raising the tuple's first item (and it better have one!);
2830 this rule is applied recursively.
2831
2832 Finally, an optional third argument can be supplied, which
2833 gives the traceback to be substituted (useful when
2834 re-raising an exception after examining it). */
2835
2836 /* First, check the traceback argument, replacing None with
2837 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002838 if (tb == Py_None) {
2839 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002840 tb = NULL;
2841 }
2842 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002843 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002844 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002845 goto raise_error;
2846 }
2847
2848 /* Next, replace a missing value with None */
2849 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002850 value = Py_None;
2851 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002852 }
2853
2854 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00002855 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
2856 PyObject *tmp = type;
2857 type = PyTuple_GET_ITEM(type, 0);
2858 Py_INCREF(type);
2859 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002860 }
2861
Tim Petersafb2c802002-04-18 18:06:20 +00002862 if (PyString_CheckExact(type))
2863 /* Raising builtin string is deprecated but still allowed --
2864 * do nothing. Raising an instance of a new-style str
2865 * subclass is right out. */
Neal Norwitz37aa0662003-01-10 15:31:15 +00002866 PyErr_Warn(PyExc_PendingDeprecationWarning,
2867 "raising a string exception is deprecated");
Barry Warsaw4249f541997-08-22 21:26:19 +00002868
2869 else if (PyClass_Check(type))
2870 PyErr_NormalizeException(&type, &value, &tb);
2871
Guido van Rossumb209a111997-04-29 18:18:01 +00002872 else if (PyInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002873 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002874 if (value != Py_None) {
2875 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002876 "instance exception may not have a separate value");
2877 goto raise_error;
2878 }
2879 else {
2880 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00002881 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002882 value = type;
Guido van Rossumb209a111997-04-29 18:18:01 +00002883 type = (PyObject*) ((PyInstanceObject*)type)->in_class;
2884 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002885 }
2886 }
2887 else {
2888 /* Not something you can raise. You get an exception
2889 anyway, just not what you specified :-) */
Jeremy Hylton960d9482001-04-27 02:25:33 +00002890 PyErr_Format(PyExc_TypeError,
Neal Norwitz37aa0662003-01-10 15:31:15 +00002891 "exceptions must be classes, instances, or "
2892 "strings (deprecated), not %s",
2893 type->ob_type->tp_name);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002894 goto raise_error;
2895 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002896 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002897 if (tb == NULL)
2898 return WHY_EXCEPTION;
2899 else
2900 return WHY_RERAISE;
2901 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00002902 Py_XDECREF(value);
2903 Py_XDECREF(type);
2904 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002905 return WHY_EXCEPTION;
2906}
2907
Tim Petersd6d010b2001-06-21 02:49:55 +00002908/* Iterate v argcnt times and store the results on the stack (via decreasing
2909 sp). Return 1 for success, 0 if error. */
2910
Barry Warsawe42b18f1997-08-25 22:13:04 +00002911static int
Tim Petersd6d010b2001-06-21 02:49:55 +00002912unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00002913{
Tim Petersd6d010b2001-06-21 02:49:55 +00002914 int i = 0;
2915 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00002916 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00002917
Tim Petersd6d010b2001-06-21 02:49:55 +00002918 assert(v != NULL);
2919
2920 it = PyObject_GetIter(v);
2921 if (it == NULL)
2922 goto Error;
2923
2924 for (; i < argcnt; i++) {
2925 w = PyIter_Next(it);
2926 if (w == NULL) {
2927 /* Iterator done, via error or exhaustion. */
2928 if (!PyErr_Occurred()) {
2929 PyErr_Format(PyExc_ValueError,
2930 "need more than %d value%s to unpack",
2931 i, i == 1 ? "" : "s");
2932 }
2933 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00002934 }
2935 *--sp = w;
2936 }
Tim Petersd6d010b2001-06-21 02:49:55 +00002937
2938 /* We better have exhausted the iterator now. */
2939 w = PyIter_Next(it);
2940 if (w == NULL) {
2941 if (PyErr_Occurred())
2942 goto Error;
2943 Py_DECREF(it);
2944 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00002945 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00002946 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00002947 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00002948 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00002949Error:
Barry Warsaw91010551997-08-25 22:30:51 +00002950 for (; i > 0; i--, sp++)
2951 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00002952 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00002953 return 0;
2954}
2955
2956
Guido van Rossum96a42c81992-01-12 02:29:51 +00002957#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00002958static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002959prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002960{
Guido van Rossum3f5da241990-12-20 15:06:42 +00002961 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00002962 if (PyObject_Print(v, stdout, 0) != 0)
2963 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00002964 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00002965 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002966}
Guido van Rossum3f5da241990-12-20 15:06:42 +00002967#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002968
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002969static void
Fred Drake5755ce62001-06-27 19:19:46 +00002970call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002971{
Guido van Rossumb209a111997-04-29 18:18:01 +00002972 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002973 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00002974 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00002975 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002976 value = Py_None;
2977 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00002978 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002979 arg = Py_BuildValue("(OOO)", type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002980 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002981 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002982 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002983 }
Fred Drake5755ce62001-06-27 19:19:46 +00002984 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002985 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002986 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00002987 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002988 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00002989 Py_XDECREF(type);
2990 Py_XDECREF(value);
2991 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002992 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002993}
2994
Fred Drake4ec5d562001-10-04 19:26:43 +00002995static void
2996call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
2997 int what)
2998{
2999 PyObject *type, *value, *traceback;
3000 int err;
3001 PyErr_Fetch(&type, &value, &traceback);
3002 err = call_trace(func, obj, frame, what, NULL);
3003 if (err == 0)
3004 PyErr_Restore(type, value, traceback);
3005 else {
3006 Py_XDECREF(type);
3007 Py_XDECREF(value);
3008 Py_XDECREF(traceback);
3009 }
3010}
3011
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003012static int
Fred Drake5755ce62001-06-27 19:19:46 +00003013call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3014 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003015{
Fred Drake5755ce62001-06-27 19:19:46 +00003016 register PyThreadState *tstate = frame->f_tstate;
3017 int result;
3018 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003019 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003020 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003021 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003022 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003023 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3024 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003025 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003026 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003027}
3028
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003029PyObject *
3030_PyEval_CallTracing(PyObject *func, PyObject *args)
3031{
3032 PyFrameObject *frame = PyEval_GetFrame();
3033 PyThreadState *tstate = frame->f_tstate;
3034 int save_tracing = tstate->tracing;
3035 int save_use_tracing = tstate->use_tracing;
3036 PyObject *result;
3037
3038 tstate->tracing = 0;
3039 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3040 || (tstate->c_profilefunc != NULL));
3041 result = PyObject_Call(func, args, NULL);
3042 tstate->tracing = save_tracing;
3043 tstate->use_tracing = save_use_tracing;
3044 return result;
3045}
3046
Michael W. Hudson006c7522002-11-08 13:08:46 +00003047static int
Michael W. Hudson019a78e2002-11-08 12:53:11 +00003048maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003049 PyFrameObject *frame, int *instr_lb, int *instr_ub)
3050{
3051 /* The theory of SET_LINENO-less tracing.
3052
3053 In a nutshell, we use the co_lnotab field of the code object
3054 to tell when execution has moved onto a different line.
3055
3056 As mentioned above, the basic idea is so set things up so
3057 that
3058
3059 *instr_lb <= frame->f_lasti < *instr_ub
3060
3061 is true so long as execution does not change lines.
3062
3063 This is all fairly simple. Digging the information out of
3064 co_lnotab takes some work, but is conceptually clear.
3065
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003066 Somewhat harder to explain is why we don't *always* call the
3067 line trace function when the above test fails.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003068
3069 Consider this code:
3070
3071 1: def f(a):
3072 2: if a:
3073 3: print 1
3074 4: else:
3075 5: print 2
3076
3077 which compiles to this:
3078
3079 2 0 LOAD_FAST 0 (a)
3080 3 JUMP_IF_FALSE 9 (to 15)
3081 6 POP_TOP
3082
3083 3 7 LOAD_CONST 1 (1)
3084 10 PRINT_ITEM
3085 11 PRINT_NEWLINE
3086 12 JUMP_FORWARD 6 (to 21)
3087 >> 15 POP_TOP
3088
3089 5 16 LOAD_CONST 2 (2)
3090 19 PRINT_ITEM
3091 20 PRINT_NEWLINE
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003092 >> 21 LOAD_CONST 0 (None)
3093 24 RETURN_VALUE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003094
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003095 If 'a' is false, execution will jump to instruction at offset
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003096 15 and the co_lnotab will claim that execution has moved to
3097 line 3. This is at best misleading. In this case we could
3098 associate the POP_TOP with line 4, but that doesn't make
3099 sense in all cases (I think).
3100
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003101 What we do is only call the line trace function if the co_lnotab
3102 indicates we have jumped to the *start* of a line, i.e. if the
3103 current instruction offset matches the offset given for the
3104 start of a line by the co_lnotab.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003105
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003106 This also takes care of the situation where 'a' is true.
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003107 Execution will jump from instruction offset 12 to offset 21.
3108 Then the co_lnotab would imply that execution has moved to line
3109 5, which is again misleading.
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003110
3111 Why do we set f_lineno when tracing? Well, consider the code
3112 above when 'a' is true. If stepping through this with 'n' in
3113 pdb, you would stop at line 1 with a "call" type event, then
3114 line events on lines 2 and 3, then a "return" type event -- but
3115 you would be shown line 5 during this event. This is a change
3116 from the behaviour in 2.2 and before, and I've found it
3117 confusing in practice. By setting and using f_lineno when
3118 tracing, one can report a line number different from that
3119 suggested by f_lasti on this one occasion where it's desirable.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003120 */
3121
Michael W. Hudson006c7522002-11-08 13:08:46 +00003122 int result = 0;
3123
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003124 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003125 PyCodeObject* co = frame->f_code;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003126 int size, addr, line;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003127 unsigned char* p;
3128
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003129 size = PyString_GET_SIZE(co->co_lnotab) / 2;
3130 p = (unsigned char*)PyString_AS_STRING(co->co_lnotab);
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003131
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003132 addr = 0;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003133 line = co->co_firstlineno;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003134
3135 /* possible optimization: if f->f_lasti == instr_ub
3136 (likely to be a common case) then we already know
3137 instr_lb -- if we stored the matching value of p
3138 somwhere we could skip the first while loop. */
3139
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003140 /* see comments in compile.c for the description of
3141 co_lnotab. A point to remember: increments to p
3142 should come in pairs -- although we don't care about
3143 the line increments here, treating them as byte
3144 increments gets confusing, to say the least. */
3145
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003146 while (size > 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003147 if (addr + *p > frame->f_lasti)
3148 break;
3149 addr += *p++;
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003150 if (*p) *instr_lb = addr;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003151 line += *p++;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003152 --size;
3153 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003154
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003155 if (addr == frame->f_lasti) {
3156 frame->f_lineno = line;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003157 result = call_trace(func, obj, frame,
3158 PyTrace_LINE, Py_None);
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003159 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003160
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003161 if (size > 0) {
3162 while (--size >= 0) {
3163 addr += *p++;
3164 if (*p++)
3165 break;
3166 }
3167 *instr_ub = addr;
3168 }
3169 else {
3170 *instr_ub = INT_MAX;
3171 }
3172 }
Michael W. Hudson006c7522002-11-08 13:08:46 +00003173
3174 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003175}
3176
Fred Drake5755ce62001-06-27 19:19:46 +00003177void
3178PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003179{
Fred Drake5755ce62001-06-27 19:19:46 +00003180 PyThreadState *tstate = PyThreadState_Get();
3181 PyObject *temp = tstate->c_profileobj;
3182 Py_XINCREF(arg);
3183 tstate->c_profilefunc = NULL;
3184 tstate->c_profileobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003185 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003186 Py_XDECREF(temp);
3187 tstate->c_profilefunc = func;
3188 tstate->c_profileobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003189 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003190}
3191
3192void
3193PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3194{
3195 PyThreadState *tstate = PyThreadState_Get();
3196 PyObject *temp = tstate->c_traceobj;
3197 Py_XINCREF(arg);
3198 tstate->c_tracefunc = NULL;
3199 tstate->c_traceobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003200 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003201 Py_XDECREF(temp);
3202 tstate->c_tracefunc = func;
3203 tstate->c_traceobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003204 tstate->use_tracing = ((func != NULL)
3205 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003206}
3207
Guido van Rossumb209a111997-04-29 18:18:01 +00003208PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003209PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003210{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003211 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003212 if (current_frame == NULL)
Michael W. Hudson019a78e2002-11-08 12:53:11 +00003213 return PyThreadState_Get()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003214 else
3215 return current_frame->f_builtins;
3216}
3217
Guido van Rossumb209a111997-04-29 18:18:01 +00003218PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003219PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003220{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003221 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003222 if (current_frame == NULL)
3223 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003224 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003225 return current_frame->f_locals;
3226}
3227
Guido van Rossumb209a111997-04-29 18:18:01 +00003228PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003229PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003230{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003231 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003232 if (current_frame == NULL)
3233 return NULL;
3234 else
3235 return current_frame->f_globals;
3236}
3237
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003238PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003239PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003240{
Michael W. Hudson019a78e2002-11-08 12:53:11 +00003241 PyThreadState *tstate = PyThreadState_Get();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003242 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003243}
3244
Guido van Rossum6135a871995-01-09 17:53:26 +00003245int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003246PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003247{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003248 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003249 return current_frame == NULL ? 0 : current_frame->f_restricted;
3250}
3251
Guido van Rossumbe270261997-05-22 22:26:18 +00003252int
Tim Peters5ba58662001-07-16 02:29:45 +00003253PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003254{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003255 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003256 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003257
3258 if (current_frame != NULL) {
3259 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003260 const int compilerflags = codeflags & PyCF_MASK;
3261 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003262 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003263 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003264 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003265#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003266 if (codeflags & CO_GENERATOR_ALLOWED) {
3267 result = 1;
3268 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3269 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003270#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003271 }
3272 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003273}
3274
3275int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003276Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003277{
Guido van Rossumb209a111997-04-29 18:18:01 +00003278 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003279 if (f == NULL)
3280 return 0;
3281 if (!PyFile_SoftSpace(f, 0))
3282 return 0;
3283 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003284}
3285
Guido van Rossum3f5da241990-12-20 15:06:42 +00003286
Guido van Rossum681d79a1995-07-18 14:51:37 +00003287/* External interface to call any callable object.
3288 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003289
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003290#undef PyEval_CallObject
3291/* for backward compatibility: export this interface */
3292
Guido van Rossumb209a111997-04-29 18:18:01 +00003293PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003294PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003295{
Guido van Rossumb209a111997-04-29 18:18:01 +00003296 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003297}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003298#define PyEval_CallObject(func,arg) \
3299 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003300
Guido van Rossumb209a111997-04-29 18:18:01 +00003301PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003302PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003303{
Jeremy Hylton52820442001-01-03 23:52:36 +00003304 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003305
3306 if (arg == NULL)
Guido van Rossumb209a111997-04-29 18:18:01 +00003307 arg = PyTuple_New(0);
3308 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003309 PyErr_SetString(PyExc_TypeError,
3310 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003311 return NULL;
3312 }
3313 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003314 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003315
Guido van Rossumb209a111997-04-29 18:18:01 +00003316 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003317 PyErr_SetString(PyExc_TypeError,
3318 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003319 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003320 return NULL;
3321 }
3322
Tim Peters6d6c1a32001-08-02 04:15:00 +00003323 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003324 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003325 return result;
3326}
3327
Tim Peters6d6c1a32001-08-02 04:15:00 +00003328char *
3329PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003330{
3331 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003332 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003333 else if (PyFunction_Check(func))
3334 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3335 else if (PyCFunction_Check(func))
3336 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3337 else if (PyClass_Check(func))
3338 return PyString_AsString(((PyClassObject*)func)->cl_name);
3339 else if (PyInstance_Check(func)) {
3340 return PyString_AsString(
3341 ((PyInstanceObject*)func)->in_class->cl_name);
3342 } else {
3343 return func->ob_type->tp_name;
3344 }
3345}
3346
Tim Peters6d6c1a32001-08-02 04:15:00 +00003347char *
3348PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003349{
3350 if (PyMethod_Check(func))
3351 return "()";
3352 else if (PyFunction_Check(func))
3353 return "()";
3354 else if (PyCFunction_Check(func))
3355 return "()";
3356 else if (PyClass_Check(func))
3357 return " constructor";
3358 else if (PyInstance_Check(func)) {
3359 return " instance";
3360 } else {
3361 return " object";
3362 }
3363}
3364
Jeremy Hylton52820442001-01-03 23:52:36 +00003365#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
3366
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003367static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003368err_args(PyObject *func, int flags, int nargs)
3369{
3370 if (flags & METH_NOARGS)
3371 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003372 "%.200s() takes no arguments (%d given)",
Jeremy Hylton192690e2002-08-16 18:36:11 +00003373 ((PyCFunctionObject *)func)->m_ml->ml_name,
3374 nargs);
3375 else
3376 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003377 "%.200s() takes exactly one argument (%d given)",
Jeremy Hylton192690e2002-08-16 18:36:11 +00003378 ((PyCFunctionObject *)func)->m_ml->ml_name,
3379 nargs);
3380}
3381
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003382static PyObject *
3383call_function(PyObject ***pp_stack, int oparg)
3384{
3385 int na = oparg & 0xff;
3386 int nk = (oparg>>8) & 0xff;
3387 int n = na + 2 * nk;
3388 PyObject **pfunc = (*pp_stack) - n - 1;
3389 PyObject *func = *pfunc;
3390 PyObject *x, *w;
3391
Jeremy Hylton985eba52003-02-05 23:13:00 +00003392 /* Always dispatch PyCFunction first, because these are
3393 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003394 */
3395 if (PyCFunction_Check(func) && nk == 0) {
3396 int flags = PyCFunction_GET_FLAGS(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003397 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003398 if (flags & (METH_NOARGS | METH_O)) {
3399 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3400 PyObject *self = PyCFunction_GET_SELF(func);
3401 if (flags & METH_NOARGS && na == 0)
3402 x = (*meth)(self, NULL);
3403 else if (flags & METH_O && na == 1) {
3404 PyObject *arg = EXT_POP(*pp_stack);
3405 x = (*meth)(self, arg);
3406 Py_DECREF(arg);
3407 }
3408 else {
3409 err_args(func, flags, na);
3410 x = NULL;
3411 }
3412 }
3413 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003414 PyObject *callargs;
3415 callargs = load_args(pp_stack, na);
3416 x = PyCFunction_Call(func, callargs, NULL);
3417 Py_XDECREF(callargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003418 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003419 } else {
3420 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3421 /* optimize access to bound methods */
3422 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003423 PCALL(PCALL_METHOD);
3424 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003425 Py_INCREF(self);
3426 func = PyMethod_GET_FUNCTION(func);
3427 Py_INCREF(func);
3428 Py_DECREF(*pfunc);
3429 *pfunc = self;
3430 na++;
3431 n++;
3432 } else
3433 Py_INCREF(func);
3434 if (PyFunction_Check(func))
3435 x = fast_function(func, pp_stack, n, na, nk);
3436 else
3437 x = do_call(func, pp_stack, na, nk);
3438 Py_DECREF(func);
3439 }
3440
Jeremy Hylton985eba52003-02-05 23:13:00 +00003441 /* What does this do? */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003442 while ((*pp_stack) > pfunc) {
3443 w = EXT_POP(*pp_stack);
3444 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003445 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003446 }
3447 return x;
3448}
3449
Jeremy Hylton192690e2002-08-16 18:36:11 +00003450/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003451 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003452 For the simplest case -- a function that takes only positional
3453 arguments and is called with only positional arguments -- it
3454 inlines the most primitive frame setup code from
3455 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3456 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003457*/
3458
3459static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003460fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003461{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003462 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003463 PyObject *globals = PyFunction_GET_GLOBALS(func);
3464 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3465 PyObject **d = NULL;
3466 int nd = 0;
3467
Jeremy Hylton985eba52003-02-05 23:13:00 +00003468 PCALL(PCALL_FUNCTION);
3469 PCALL(PCALL_FAST_FUNCTION);
3470 if (argdefs == NULL && co->co_argcount == n &&
3471 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3472 PyFrameObject *f;
3473 PyObject *retval = NULL;
3474 PyThreadState *tstate = PyThreadState_GET();
3475 PyObject **fastlocals, **stack;
3476 int i;
3477
3478 PCALL(PCALL_FASTER_FUNCTION);
3479 assert(globals != NULL);
3480 /* XXX Perhaps we should create a specialized
3481 PyFrame_New() that doesn't take locals, but does
3482 take builtins without sanity checking them.
3483 */
3484 f = PyFrame_New(tstate, co, globals, NULL);
3485 if (f == NULL)
3486 return NULL;
3487
3488 fastlocals = f->f_localsplus;
3489 stack = (*pp_stack) - n;
3490
3491 for (i = 0; i < n; i++) {
3492 Py_INCREF(*stack);
3493 fastlocals[i] = *stack++;
3494 }
3495 retval = eval_frame(f);
3496 assert(tstate != NULL);
3497 ++tstate->recursion_depth;
3498 Py_DECREF(f);
3499 --tstate->recursion_depth;
3500 return retval;
3501 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003502 if (argdefs != NULL) {
3503 d = &PyTuple_GET_ITEM(argdefs, 0);
3504 nd = ((PyTupleObject *)argdefs)->ob_size;
3505 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003506 return PyEval_EvalCodeEx(co, globals,
3507 (PyObject *)NULL, (*pp_stack)-n, na,
3508 (*pp_stack)-2*nk, nk, d, nd,
3509 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003510}
3511
3512static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003513update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3514 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003515{
3516 PyObject *kwdict = NULL;
3517 if (orig_kwdict == NULL)
3518 kwdict = PyDict_New();
3519 else {
3520 kwdict = PyDict_Copy(orig_kwdict);
3521 Py_DECREF(orig_kwdict);
3522 }
3523 if (kwdict == NULL)
3524 return NULL;
3525 while (--nk >= 0) {
3526 int err;
3527 PyObject *value = EXT_POP(*pp_stack);
3528 PyObject *key = EXT_POP(*pp_stack);
3529 if (PyDict_GetItem(kwdict, key) != NULL) {
Guido van Rossumac7be682001-01-17 15:42:30 +00003530 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +00003531 "%.200s%s got multiple values "
Jeremy Hylton512a2372001-04-11 13:52:29 +00003532 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003533 PyEval_GetFuncName(func),
3534 PyEval_GetFuncDesc(func),
Jeremy Hylton512a2372001-04-11 13:52:29 +00003535 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003536 Py_DECREF(key);
3537 Py_DECREF(value);
3538 Py_DECREF(kwdict);
3539 return NULL;
3540 }
3541 err = PyDict_SetItem(kwdict, key, value);
3542 Py_DECREF(key);
3543 Py_DECREF(value);
3544 if (err) {
3545 Py_DECREF(kwdict);
3546 return NULL;
3547 }
3548 }
3549 return kwdict;
3550}
3551
3552static PyObject *
3553update_star_args(int nstack, int nstar, PyObject *stararg,
3554 PyObject ***pp_stack)
3555{
3556 PyObject *callargs, *w;
3557
3558 callargs = PyTuple_New(nstack + nstar);
3559 if (callargs == NULL) {
3560 return NULL;
3561 }
3562 if (nstar) {
3563 int i;
3564 for (i = 0; i < nstar; i++) {
3565 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3566 Py_INCREF(a);
3567 PyTuple_SET_ITEM(callargs, nstack + i, a);
3568 }
3569 }
3570 while (--nstack >= 0) {
3571 w = EXT_POP(*pp_stack);
3572 PyTuple_SET_ITEM(callargs, nstack, w);
3573 }
3574 return callargs;
3575}
3576
3577static PyObject *
3578load_args(PyObject ***pp_stack, int na)
3579{
3580 PyObject *args = PyTuple_New(na);
3581 PyObject *w;
3582
3583 if (args == NULL)
3584 return NULL;
3585 while (--na >= 0) {
3586 w = EXT_POP(*pp_stack);
3587 PyTuple_SET_ITEM(args, na, w);
3588 }
3589 return args;
3590}
3591
3592static PyObject *
3593do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3594{
3595 PyObject *callargs = NULL;
3596 PyObject *kwdict = NULL;
3597 PyObject *result = NULL;
3598
3599 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003600 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003601 if (kwdict == NULL)
3602 goto call_fail;
3603 }
3604 callargs = load_args(pp_stack, na);
3605 if (callargs == NULL)
3606 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003607#ifdef CALL_PROFILE
3608 /* At this point, we have to look at the type of func to
3609 update the call stats properly. Do it here so as to avoid
3610 exposing the call stats machinery outside ceval.c
3611 */
3612 if (PyFunction_Check(func))
3613 PCALL(PCALL_FUNCTION);
3614 else if (PyMethod_Check(func))
3615 PCALL(PCALL_METHOD);
3616 else if (PyType_Check(func))
3617 PCALL(PCALL_TYPE);
3618 else
3619 PCALL(PCALL_OTHER);
3620#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003621 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003622 call_fail:
3623 Py_XDECREF(callargs);
3624 Py_XDECREF(kwdict);
3625 return result;
3626}
3627
3628static PyObject *
3629ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3630{
3631 int nstar = 0;
3632 PyObject *callargs = NULL;
3633 PyObject *stararg = NULL;
3634 PyObject *kwdict = NULL;
3635 PyObject *result = NULL;
3636
3637 if (flags & CALL_FLAG_KW) {
3638 kwdict = EXT_POP(*pp_stack);
3639 if (!(kwdict && PyDict_Check(kwdict))) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003640 PyErr_Format(PyExc_TypeError,
Jeremy Hylton512a2372001-04-11 13:52:29 +00003641 "%s%s argument after ** "
3642 "must be a dictionary",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003643 PyEval_GetFuncName(func),
3644 PyEval_GetFuncDesc(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003645 goto ext_call_fail;
3646 }
3647 }
3648 if (flags & CALL_FLAG_VAR) {
3649 stararg = EXT_POP(*pp_stack);
3650 if (!PyTuple_Check(stararg)) {
3651 PyObject *t = NULL;
3652 t = PySequence_Tuple(stararg);
3653 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003654 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3655 PyErr_Format(PyExc_TypeError,
3656 "%s%s argument after * "
3657 "must be a sequence",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003658 PyEval_GetFuncName(func),
3659 PyEval_GetFuncDesc(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003660 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003661 goto ext_call_fail;
3662 }
3663 Py_DECREF(stararg);
3664 stararg = t;
3665 }
3666 nstar = PyTuple_GET_SIZE(stararg);
3667 }
3668 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003669 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003670 if (kwdict == NULL)
3671 goto ext_call_fail;
3672 }
3673 callargs = update_star_args(na, nstar, stararg, pp_stack);
3674 if (callargs == NULL)
3675 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003676#ifdef CALL_PROFILE
3677 /* At this point, we have to look at the type of func to
3678 update the call stats properly. Do it here so as to avoid
3679 exposing the call stats machinery outside ceval.c
3680 */
3681 if (PyFunction_Check(func))
3682 PCALL(PCALL_FUNCTION);
3683 else if (PyMethod_Check(func))
3684 PCALL(PCALL_METHOD);
3685 else if (PyType_Check(func))
3686 PCALL(PCALL_TYPE);
3687 else
3688 PCALL(PCALL_OTHER);
3689#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003690 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003691 ext_call_fail:
3692 Py_XDECREF(callargs);
3693 Py_XDECREF(kwdict);
3694 Py_XDECREF(stararg);
3695 return result;
3696}
3697
Guido van Rossum3b9c6671996-07-30 18:40:29 +00003698#define SLICE_ERROR_MSG \
3699 "standard sequence type does not support step size other than one"
3700
Tim Peterscb479e72001-12-16 19:11:44 +00003701/* Extract a slice index from a PyInt or PyLong, and store in *pi.
3702 Silently reduce values larger than INT_MAX to INT_MAX, and silently
3703 boost values less than -INT_MAX to 0. Return 0 on error, 1 on success.
3704*/
Tim Petersb5196382001-12-16 19:44:20 +00003705/* Note: If v is NULL, return success without storing into *pi. This
3706 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3707 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00003708*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00003709int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003710_PyEval_SliceIndex(PyObject *v, int *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003711{
Tim Petersb5196382001-12-16 19:44:20 +00003712 if (v != NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003713 long x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003714 if (PyInt_Check(v)) {
3715 x = PyInt_AsLong(v);
3716 } else if (PyLong_Check(v)) {
3717 x = PyLong_AsLong(v);
3718 if (x==-1 && PyErr_Occurred()) {
3719 PyObject *long_zero;
Guido van Rossumac7be682001-01-17 15:42:30 +00003720 int cmp;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003721
Guido van Rossumac7be682001-01-17 15:42:30 +00003722 if (!PyErr_ExceptionMatches(
3723 PyExc_OverflowError)) {
3724 /* It's not an overflow error, so just
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003725 signal an error */
Guido van Rossum20c6add2000-05-08 14:06:50 +00003726 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003727 }
3728
Guido van Rossumac7be682001-01-17 15:42:30 +00003729 /* Clear the OverflowError */
3730 PyErr_Clear();
3731
3732 /* It's an overflow error, so we need to
3733 check the sign of the long integer,
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003734 set the value to INT_MAX or -INT_MAX,
3735 and clear the error. */
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003736
3737 /* Create a long integer with a value of 0 */
Guido van Rossumac7be682001-01-17 15:42:30 +00003738 long_zero = PyLong_FromLong(0L);
Tim Peterscb479e72001-12-16 19:11:44 +00003739 if (long_zero == NULL)
3740 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003741
3742 /* Check sign */
Guido van Rossumac7be682001-01-17 15:42:30 +00003743 cmp = PyObject_RichCompareBool(v, long_zero,
3744 Py_GT);
3745 Py_DECREF(long_zero);
3746 if (cmp < 0)
3747 return 0;
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003748 else if (cmp)
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003749 x = INT_MAX;
3750 else
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003751 x = -INT_MAX;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003752 }
3753 } else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003754 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003755 "slice indices must be integers");
Guido van Rossum20c6add2000-05-08 14:06:50 +00003756 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003757 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003758 /* Truncate -- very long indices are truncated anyway */
3759 if (x > INT_MAX)
3760 x = INT_MAX;
3761 else if (x < -INT_MAX)
Michael W. Hudsoncbd6fb92002-11-06 15:17:32 +00003762 x = -INT_MAX;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003763 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003764 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00003765 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003766}
3767
Guido van Rossum50d756e2001-08-18 17:43:36 +00003768#undef ISINT
3769#define ISINT(x) ((x) == NULL || PyInt_Check(x) || PyLong_Check(x))
3770
Guido van Rossumb209a111997-04-29 18:18:01 +00003771static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003772apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003773{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003774 PyTypeObject *tp = u->ob_type;
3775 PySequenceMethods *sq = tp->tp_as_sequence;
3776
3777 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3778 int ilow = 0, ihigh = INT_MAX;
3779 if (!_PyEval_SliceIndex(v, &ilow))
3780 return NULL;
3781 if (!_PyEval_SliceIndex(w, &ihigh))
3782 return NULL;
3783 return PySequence_GetSlice(u, ilow, ihigh);
3784 }
3785 else {
3786 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00003787 if (slice != NULL) {
3788 PyObject *res = PyObject_GetItem(u, slice);
3789 Py_DECREF(slice);
3790 return res;
3791 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00003792 else
3793 return NULL;
3794 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003795}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003796
3797static int
Guido van Rossumac7be682001-01-17 15:42:30 +00003798assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
3799 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003800{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003801 PyTypeObject *tp = u->ob_type;
3802 PySequenceMethods *sq = tp->tp_as_sequence;
3803
3804 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3805 int ilow = 0, ihigh = INT_MAX;
3806 if (!_PyEval_SliceIndex(v, &ilow))
3807 return -1;
3808 if (!_PyEval_SliceIndex(w, &ihigh))
3809 return -1;
3810 if (x == NULL)
3811 return PySequence_DelSlice(u, ilow, ihigh);
3812 else
3813 return PySequence_SetSlice(u, ilow, ihigh, x);
3814 }
3815 else {
3816 PyObject *slice = PySlice_New(v, w, NULL);
3817 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00003818 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003819 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00003820 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00003821 else
Guido van Rossum354797c2001-12-03 19:45:06 +00003822 res = PyObject_DelItem(u, slice);
3823 Py_DECREF(slice);
3824 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003825 }
3826 else
3827 return -1;
3828 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003829}
3830
Guido van Rossumb209a111997-04-29 18:18:01 +00003831static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003832cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003833{
Guido van Rossumac7be682001-01-17 15:42:30 +00003834 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003835 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00003836 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003837 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003838 break;
3839 case PyCmp_IS_NOT:
3840 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003841 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003842 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003843 res = PySequence_Contains(w, v);
3844 if (res < 0)
3845 return NULL;
3846 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003847 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00003848 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003849 if (res < 0)
3850 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003851 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003852 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003853 case PyCmp_EXC_MATCH:
Barry Warsaw4249f541997-08-22 21:26:19 +00003854 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003855 break;
3856 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00003857 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003858 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003859 v = res ? Py_True : Py_False;
3860 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003861 return v;
3862}
3863
Thomas Wouters52152252000-08-17 22:55:00 +00003864static PyObject *
3865import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003866{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003867 PyObject *x;
3868
3869 x = PyObject_GetAttr(v, name);
3870 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00003871 PyErr_Format(PyExc_ImportError,
3872 "cannot import name %.230s",
3873 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003874 }
Thomas Wouters52152252000-08-17 22:55:00 +00003875 return x;
3876}
Guido van Rossumac7be682001-01-17 15:42:30 +00003877
Thomas Wouters52152252000-08-17 22:55:00 +00003878static int
3879import_all_from(PyObject *locals, PyObject *v)
3880{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003881 PyObject *all = PyObject_GetAttrString(v, "__all__");
3882 PyObject *dict, *name, *value;
3883 int skip_leading_underscores = 0;
3884 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00003885
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003886 if (all == NULL) {
3887 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3888 return -1; /* Unexpected error */
3889 PyErr_Clear();
3890 dict = PyObject_GetAttrString(v, "__dict__");
3891 if (dict == NULL) {
3892 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3893 return -1;
3894 PyErr_SetString(PyExc_ImportError,
3895 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00003896 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003897 }
3898 all = PyMapping_Keys(dict);
3899 Py_DECREF(dict);
3900 if (all == NULL)
3901 return -1;
3902 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00003903 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003904
3905 for (pos = 0, err = 0; ; pos++) {
3906 name = PySequence_GetItem(all, pos);
3907 if (name == NULL) {
3908 if (!PyErr_ExceptionMatches(PyExc_IndexError))
3909 err = -1;
3910 else
3911 PyErr_Clear();
3912 break;
3913 }
3914 if (skip_leading_underscores &&
3915 PyString_Check(name) &&
3916 PyString_AS_STRING(name)[0] == '_')
3917 {
3918 Py_DECREF(name);
3919 continue;
3920 }
3921 value = PyObject_GetAttr(v, name);
3922 if (value == NULL)
3923 err = -1;
3924 else
3925 err = PyDict_SetItem(locals, name, value);
3926 Py_DECREF(name);
3927 Py_XDECREF(value);
3928 if (err != 0)
3929 break;
3930 }
3931 Py_DECREF(all);
3932 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00003933}
3934
Guido van Rossumb209a111997-04-29 18:18:01 +00003935static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003936build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003937{
Guido van Rossum7851eea2001-09-12 19:19:18 +00003938 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003939
3940 if (PyDict_Check(methods))
3941 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00003942 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00003943 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00003944 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
3945 base = PyTuple_GET_ITEM(bases, 0);
3946 metaclass = PyObject_GetAttrString(base, "__class__");
3947 if (metaclass == NULL) {
3948 PyErr_Clear();
3949 metaclass = (PyObject *)base->ob_type;
3950 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00003951 }
3952 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00003953 else {
3954 PyObject *g = PyEval_GetGlobals();
3955 if (g != NULL && PyDict_Check(g))
3956 metaclass = PyDict_GetItemString(g, "__metaclass__");
3957 if (metaclass == NULL)
3958 metaclass = (PyObject *) &PyClass_Type;
3959 Py_INCREF(metaclass);
3960 }
3961 result = PyObject_CallFunction(metaclass, "OOO", name, bases, methods);
3962 Py_DECREF(metaclass);
3963 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00003964}
3965
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003966static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003967exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
3968 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003969{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003970 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00003971 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003972 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003973
Guido van Rossumb209a111997-04-29 18:18:01 +00003974 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
3975 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003976 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00003977 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003978 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00003979 locals = PyTuple_GetItem(prog, 2);
3980 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003981 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003982 if (globals == Py_None) {
3983 globals = PyEval_GetGlobals();
3984 if (locals == Py_None) {
3985 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00003986 plain = 1;
3987 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003988 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003989 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003990 locals = globals;
Guido van Rossumb209a111997-04-29 18:18:01 +00003991 if (!PyString_Check(prog) &&
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00003992 !PyUnicode_Check(prog) &&
Guido van Rossumb209a111997-04-29 18:18:01 +00003993 !PyCode_Check(prog) &&
3994 !PyFile_Check(prog)) {
3995 PyErr_SetString(PyExc_TypeError,
Guido van Rossumac7be682001-01-17 15:42:30 +00003996 "exec: arg 1 must be a string, file, or code object");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003997 return -1;
3998 }
Fred Drake661ea262000-10-24 19:57:45 +00003999 if (!PyDict_Check(globals)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004000 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00004001 "exec: arg 2 must be a dictionary or None");
4002 return -1;
4003 }
4004 if (!PyDict_Check(locals)) {
4005 PyErr_SetString(PyExc_TypeError,
4006 "exec: arg 3 must be a dictionary or None");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004007 return -1;
4008 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004009 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00004010 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00004011 if (PyCode_Check(prog)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +00004012 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4013 PyErr_SetString(PyExc_TypeError,
4014 "code object passed to exec may not contain free variables");
4015 return -1;
4016 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004017 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004018 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004019 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004020 FILE *fp = PyFile_AsFile(prog);
4021 char *name = PyString_AsString(PyFile_Name(prog));
Tim Peters5ba58662001-07-16 02:29:45 +00004022 PyCompilerFlags cf;
4023 cf.cf_flags = 0;
4024 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004025 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
4026 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004027 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004028 v = PyRun_File(fp, name, Py_file_input, globals,
4029 locals);
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004030 }
4031 else {
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004032 PyObject *tmp = NULL;
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004033 char *str;
Tim Peters5ba58662001-07-16 02:29:45 +00004034 PyCompilerFlags cf;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004035 cf.cf_flags = 0;
4036#ifdef Py_USING_UNICODE
4037 if (PyUnicode_Check(prog)) {
4038 tmp = PyUnicode_AsUTF8String(prog);
4039 if (tmp == NULL)
4040 return -1;
4041 prog = tmp;
4042 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4043 }
4044#endif
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004045 if (PyString_AsStringAndSize(prog, &str, NULL))
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004046 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004047 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004048 v = PyRun_StringFlags(str, Py_file_input, globals,
4049 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004050 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004051 v = PyRun_String(str, Py_file_input, globals, locals);
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004052 Py_XDECREF(tmp);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004053 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004054 if (plain)
4055 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004056 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004057 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00004058 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004059 return 0;
4060}
Guido van Rossum24c13741995-02-14 09:42:43 +00004061
Guido van Rossumac7be682001-01-17 15:42:30 +00004062static void
Paul Prescode68140d2000-08-30 20:25:01 +00004063format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4064{
4065 char *obj_str;
4066
4067 if (!obj)
4068 return;
4069
4070 obj_str = PyString_AsString(obj);
4071 if (!obj_str)
4072 return;
4073
4074 PyErr_Format(exc, format_str, obj_str);
4075}
Guido van Rossum950361c1997-01-24 13:49:28 +00004076
4077#ifdef DYNAMIC_EXECUTION_PROFILE
4078
Skip Montanarof118cb12001-10-15 20:51:38 +00004079static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004080getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004081{
4082 int i;
4083 PyObject *l = PyList_New(256);
4084 if (l == NULL) return NULL;
4085 for (i = 0; i < 256; i++) {
4086 PyObject *x = PyInt_FromLong(a[i]);
4087 if (x == NULL) {
4088 Py_DECREF(l);
4089 return NULL;
4090 }
4091 PyList_SetItem(l, i, x);
4092 }
4093 for (i = 0; i < 256; i++)
4094 a[i] = 0;
4095 return l;
4096}
4097
4098PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004099_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004100{
4101#ifndef DXPAIRS
4102 return getarray(dxp);
4103#else
4104 int i;
4105 PyObject *l = PyList_New(257);
4106 if (l == NULL) return NULL;
4107 for (i = 0; i < 257; i++) {
4108 PyObject *x = getarray(dxpairs[i]);
4109 if (x == NULL) {
4110 Py_DECREF(l);
4111 return NULL;
4112 }
4113 PyList_SetItem(l, i, x);
4114 }
4115 return l;
4116#endif
4117}
4118
4119#endif