blob: 080b3c16bc3c1710aeca349076d5f02db721e284 [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");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000324 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000325 if (PyThreadState_Swap(tstate) != NULL)
326 Py_FatalError(
327 "PyEval_AcquireThread: non-NULL old thread state");
328}
329
330void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000331PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000332{
333 if (tstate == NULL)
334 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
335 if (PyThreadState_Swap(NULL) != tstate)
336 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000337 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000338}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000339
340/* This function is called from PyOS_AfterFork to ensure that newly
341 created child processes don't hold locks referring to threads which
342 are not running in the child process. (This could also be done using
343 pthread_atfork mechanism, at least for the pthreads implementation.) */
344
345void
346PyEval_ReInitThreads(void)
347{
348 if (!interpreter_lock)
349 return;
350 /*XXX Can't use PyThread_free_lock here because it does too
351 much error-checking. Doing this cleanly would require
352 adding a new function to each thread_*.h. Instead, just
353 create a new lock and waste a little bit of memory */
354 interpreter_lock = PyThread_allocate_lock();
355 PyThread_acquire_lock(interpreter_lock, 1);
356 main_thread = PyThread_get_thread_ident();
357}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000358#endif
359
Guido van Rossumff4949e1992-08-05 19:58:53 +0000360/* Functions save_thread and restore_thread are always defined so
361 dynamically loaded modules needn't be compiled separately for use
362 with and without threads: */
363
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000364PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000365PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000366{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000367 PyThreadState *tstate = PyThreadState_Swap(NULL);
368 if (tstate == NULL)
369 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000370#ifdef WITH_THREAD
Guido van Rossumb74eca91997-09-30 22:03:16 +0000371 if (interpreter_lock)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000372 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000373#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000374 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000375}
376
377void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000378PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000379{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000380 if (tstate == NULL)
381 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000382#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000383 if (interpreter_lock) {
Guido van Rossumb74eca91997-09-30 22:03:16 +0000384 int err = errno;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000385 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000386 errno = err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000387 }
388#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000389 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000390}
391
392
Guido van Rossuma9672091994-09-14 13:31:22 +0000393/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
394 signal handlers or Mac I/O completion routines) can schedule calls
395 to a function to be called synchronously.
396 The synchronous function is called with one void* argument.
397 It should return 0 for success or -1 for failure -- failure should
398 be accompanied by an exception.
399
400 If registry succeeds, the registry function returns 0; if it fails
401 (e.g. due to too many pending calls) it returns -1 (without setting
402 an exception condition).
403
404 Note that because registry may occur from within signal handlers,
405 or other asynchronous events, calling malloc() is unsafe!
406
407#ifdef WITH_THREAD
408 Any thread can schedule pending calls, but only the main thread
409 will execute them.
410#endif
411
412 XXX WARNING! ASYNCHRONOUSLY EXECUTING CODE!
413 There are two possible race conditions:
414 (1) nested asynchronous registry calls;
415 (2) registry calls made while pending calls are being processed.
416 While (1) is very unlikely, (2) is a real possibility.
417 The current code is safe against (2), but not against (1).
418 The safety against (2) is derived from the fact that only one
419 thread (the main thread) ever takes things out of the queue.
Guido van Rossuma9672091994-09-14 13:31:22 +0000420
Guido van Rossuma027efa1997-05-05 20:56:21 +0000421 XXX Darn! With the advent of thread state, we should have an array
422 of pending calls per thread in the thread state! Later...
423*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000424
Guido van Rossuma9672091994-09-14 13:31:22 +0000425#define NPENDINGCALLS 32
426static struct {
Thomas Wouters334fb892000-07-25 12:56:38 +0000427 int (*func)(void *);
428 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000429} pendingcalls[NPENDINGCALLS];
430static volatile int pendingfirst = 0;
431static volatile int pendinglast = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000432static volatile int things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000433
434int
Thomas Wouters334fb892000-07-25 12:56:38 +0000435Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000436{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000437 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000438 int i, j;
439 /* XXX Begin critical section */
440 /* XXX If you want this to be safe against nested
441 XXX asynchronous calls, you'll have to work harder! */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000442 if (busy)
443 return -1;
444 busy = 1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000445 i = pendinglast;
446 j = (i + 1) % NPENDINGCALLS;
Guido van Rossum04e70322002-07-17 16:57:13 +0000447 if (j == pendingfirst) {
448 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000449 return -1; /* Queue full */
Guido van Rossum04e70322002-07-17 16:57:13 +0000450 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000451 pendingcalls[i].func = func;
452 pendingcalls[i].arg = arg;
453 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000454
455 _Py_Ticker = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000456 things_to_do = 1; /* Signal main loop */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000457 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000458 /* XXX End critical section */
459 return 0;
460}
461
Guido van Rossum180d7b41994-09-29 09:45:57 +0000462int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000463Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000464{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000465 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000466#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000467 if (main_thread && PyThread_get_thread_ident() != main_thread)
Guido van Rossuma9672091994-09-14 13:31:22 +0000468 return 0;
469#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000470 if (busy)
Guido van Rossum180d7b41994-09-29 09:45:57 +0000471 return 0;
472 busy = 1;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000473 things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000474 for (;;) {
475 int i;
Thomas Wouters334fb892000-07-25 12:56:38 +0000476 int (*func)(void *);
477 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000478 i = pendingfirst;
479 if (i == pendinglast)
480 break; /* Queue empty */
481 func = pendingcalls[i].func;
482 arg = pendingcalls[i].arg;
483 pendingfirst = (i + 1) % NPENDINGCALLS;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000484 if (func(arg) < 0) {
485 busy = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000486 things_to_do = 1; /* We're not done yet */
Guido van Rossuma9672091994-09-14 13:31:22 +0000487 return -1;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000488 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000489 }
Guido van Rossum180d7b41994-09-29 09:45:57 +0000490 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000491 return 0;
492}
493
494
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000495/* The interpreter's recursion limit */
496
Guido van Rossum349ff6f2000-09-01 01:52:08 +0000497static int recursion_limit = 1000;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000498
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000499int
500Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000501{
502 return recursion_limit;
503}
504
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000505void
506Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000507{
508 recursion_limit = new_limit;
509}
510
Guido van Rossum374a9221991-04-04 10:40:29 +0000511/* Status code for main loop (reason for stack unwind) */
512
513enum why_code {
514 WHY_NOT, /* No error */
515 WHY_EXCEPTION, /* Exception occurred */
516 WHY_RERAISE, /* Exception re-raised by 'finally' */
517 WHY_RETURN, /* 'return' statement */
Jeremy Hylton3faa52e2001-02-01 22:48:12 +0000518 WHY_BREAK, /* 'break' statement */
Tim Peters5ca576e2001-06-18 22:08:13 +0000519 WHY_CONTINUE, /* 'continue' statement */
Tim Peters6e6a63f2001-10-18 20:49:35 +0000520 WHY_YIELD /* 'yield' operator */
Guido van Rossum374a9221991-04-04 10:40:29 +0000521};
522
Tim Petersdbd9ba62000-07-09 03:09:57 +0000523static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
Tim Petersd6d010b2001-06-21 02:49:55 +0000524static int unpack_iterable(PyObject *, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000525
Skip Montanarod581d772002-09-03 20:10:45 +0000526/* for manipulating the thread switch and periodic "stuff" - used to be
527 per thread, now just a pair o' globals */
Skip Montanaro99dba272002-09-03 20:19:06 +0000528int _Py_CheckInterval = 100;
529volatile int _Py_Ticker = 100;
Guido van Rossum374a9221991-04-04 10:40:29 +0000530
Guido van Rossumb209a111997-04-29 18:18:01 +0000531PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000532PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000533{
Jeremy Hylton985eba52003-02-05 23:13:00 +0000534 /* XXX raise SystemError if globals is NULL */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000535 return PyEval_EvalCodeEx(co,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000536 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000537 (PyObject **)NULL, 0,
538 (PyObject **)NULL, 0,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000539 (PyObject **)NULL, 0,
540 NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000541}
542
543
544/* Interpreter main loop */
545
Tim Peters6d6c1a32001-08-02 04:15:00 +0000546static PyObject *
Tim Peters5ca576e2001-06-18 22:08:13 +0000547eval_frame(PyFrameObject *f)
Guido van Rossum374a9221991-04-04 10:40:29 +0000548{
Guido van Rossum950361c1997-01-24 13:49:28 +0000549#ifdef DXPAIRS
550 int lastopcode = 0;
551#endif
Tim Petersb6d14da2001-12-19 04:11:07 +0000552 PyObject **stack_pointer; /* Next free slot in value stack */
Guido van Rossum374a9221991-04-04 10:40:29 +0000553 register unsigned char *next_instr;
Moshe Zadkaaa39a7e2000-08-07 06:34:45 +0000554 register int opcode=0; /* Current opcode */
555 register int oparg=0; /* Current opcode argument, if any */
Guido van Rossum374a9221991-04-04 10:40:29 +0000556 register enum why_code why; /* Reason for block stack unwind */
557 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000558 register PyObject *x; /* Result object -- NULL if error */
559 register PyObject *v; /* Temporary objects popped off stack */
560 register PyObject *w;
561 register PyObject *u;
562 register PyObject *t;
Barry Warsaw23c9ec82000-08-21 15:44:01 +0000563 register PyObject *stream = NULL; /* for PRINT opcodes */
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000564 register PyObject **fastlocals, **freevars;
Guido van Rossum014518f1998-11-23 21:09:51 +0000565 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000566 PyThreadState *tstate = PyThreadState_GET();
Tim Peters5ca576e2001-06-18 22:08:13 +0000567 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000568
569 /* when tracing we set things up so that
570
571 not (instr_lb <= current_bytecode_offset < instr_ub)
572
573 is true when the line being executed has changed. The
574 initial values are such as to make this false the first
575 time it is tested. */
576 int instr_ub = -1, instr_lb = 0;
577
Guido van Rossumd076c731998-10-07 19:42:25 +0000578 unsigned char *first_instr;
Skip Montanaro04d80f82002-08-04 21:03:35 +0000579 PyObject *names;
580 PyObject *consts;
Guido van Rossum96a42c81992-01-12 02:29:51 +0000581#ifdef LLTRACE
Guido van Rossumacbe8da1993-04-15 15:33:52 +0000582 int lltrace;
Guido van Rossum374a9221991-04-04 10:40:29 +0000583#endif
Guido van Rossum408027e1996-12-30 16:17:54 +0000584#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000585 /* Make it easier to find out where we are with a debugger */
Tim Peters5ca576e2001-06-18 22:08:13 +0000586 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000587#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000588
Neal Norwitza81d2202002-07-14 00:27:26 +0000589/* Tuple access macros */
590
591#ifndef Py_DEBUG
592#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
593#else
594#define GETITEM(v, i) PyTuple_GetItem((v), (i))
595#endif
596
Guido van Rossum374a9221991-04-04 10:40:29 +0000597/* Code access macros */
598
Guido van Rossumd076c731998-10-07 19:42:25 +0000599#define INSTR_OFFSET() (next_instr - first_instr)
Guido van Rossum374a9221991-04-04 10:40:29 +0000600#define NEXTOP() (*next_instr++)
601#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Guido van Rossumd076c731998-10-07 19:42:25 +0000602#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000603#define JUMPBY(x) (next_instr += (x))
604
Raymond Hettingerf606f872003-03-16 03:11:04 +0000605/* OpCode prediction macros
606 Some opcodes tend to come in pairs thus making it possible to predict
607 the second code when the first is run. For example, COMPARE_OP is often
608 followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And, those opcodes are often
609 followed by a POP_TOP.
610
611 Verifying the prediction costs a single high-speed test of register
Raymond Hettingerac2072922003-03-16 15:41:11 +0000612 variable against a constant. If the pairing was good, then the
Raymond Hettingerf606f872003-03-16 03:11:04 +0000613 processor has a high likelihood of making its own successful branch
614 prediction which results in a nearly zero overhead transition to the
615 next opcode.
616
617 A successful prediction saves a trip through the eval-loop including
618 its two unpredictable branches, the HASARG test and the switch-case.
619*/
620
Raymond Hettingerac2072922003-03-16 15:41:11 +0000621#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingerf606f872003-03-16 03:11:04 +0000622#define PREDICTED(op) PRED_##op: next_instr++
Raymond Hettinger7dc52212003-03-16 20:14:44 +0000623#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = (next_instr[2]<<8) + \
624 next_instr[1]; next_instr += 3
Raymond Hettingerf606f872003-03-16 03:11:04 +0000625
Guido van Rossum374a9221991-04-04 10:40:29 +0000626/* Stack manipulation macros */
627
628#define STACK_LEVEL() (stack_pointer - f->f_valuestack)
629#define EMPTY() (STACK_LEVEL() == 0)
630#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000631#define SECOND() (stack_pointer[-2])
632#define THIRD() (stack_pointer[-3])
633#define FOURTH() (stack_pointer[-4])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000634#define SET_TOP(v) (stack_pointer[-1] = (v))
635#define SET_SECOND(v) (stack_pointer[-2] = (v))
636#define SET_THIRD(v) (stack_pointer[-3] = (v))
637#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000638#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000639#define BASIC_PUSH(v) (*stack_pointer++ = (v))
640#define BASIC_POP() (*--stack_pointer)
641
Guido van Rossum96a42c81992-01-12 02:29:51 +0000642#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000643#define PUSH(v) { (void)(BASIC_PUSH(v), \
644 lltrace && prtrace(TOP(), "push")); \
645 assert(STACK_LEVEL() <= f->f_stacksize); }
Fred Drakede26cfc2001-10-13 06:11:28 +0000646#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000647#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
648 lltrace && prtrace(TOP(), "stackadj")); \
649 assert(STACK_LEVEL() <= f->f_stacksize); }
Guido van Rossum374a9221991-04-04 10:40:29 +0000650#else
651#define PUSH(v) BASIC_PUSH(v)
652#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000653#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000654#endif
655
Guido van Rossum681d79a1995-07-18 14:51:37 +0000656/* Local variable macros */
657
658#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000659
660/* The SETLOCAL() macro must not DECREF the local variable in-place and
661 then store the new value; it must copy the old value to a temporary
662 value, then store the new value, and then DECREF the temporary value.
663 This is because it is possible that during the DECREF the frame is
664 accessed by other code (e.g. a __del__ method or gc.collect()) and the
665 variable would be pointing to already-freed memory. */
666#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
667 GETLOCAL(i) = value; \
668 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000669
Guido van Rossuma027efa1997-05-05 20:56:21 +0000670/* Start of code */
671
Tim Peters5ca576e2001-06-18 22:08:13 +0000672 if (f == NULL)
673 return NULL;
674
Guido van Rossum8861b741996-07-30 16:49:37 +0000675#ifdef USE_STACKCHECK
Guido van Rossuma027efa1997-05-05 20:56:21 +0000676 if (tstate->recursion_depth%10 == 0 && PyOS_CheckStack()) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000677 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
Guido van Rossum8861b741996-07-30 16:49:37 +0000678 return NULL;
679 }
680#endif
681
Tim Peters5ca576e2001-06-18 22:08:13 +0000682 /* push frame */
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000683 if (++tstate->recursion_depth > recursion_limit) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000684 --tstate->recursion_depth;
685 PyErr_SetString(PyExc_RuntimeError,
Fred Drake661ea262000-10-24 19:57:45 +0000686 "maximum recursion depth exceeded");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000687 tstate->frame = f->f_back;
Guido van Rossum8861b741996-07-30 16:49:37 +0000688 return NULL;
689 }
690
Tim Peters5ca576e2001-06-18 22:08:13 +0000691 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000692
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000693 if (tstate->use_tracing) {
694 if (tstate->c_tracefunc != NULL) {
695 /* tstate->c_tracefunc, if defined, is a
696 function that will be called on *every* entry
697 to a code block. Its return value, if not
698 None, is a function that will be called at
699 the start of each executed line of code.
700 (Actually, the function must return itself
701 in order to continue tracing.) The trace
702 functions are called with three arguments:
703 a pointer to the current frame, a string
704 indicating why the function is called, and
705 an argument which depends on the situation.
706 The global trace function is also called
707 whenever an exception is detected. */
708 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
709 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000710 /* Trace function raised an error */
Michael W. Hudsonfb4d6ec2002-10-02 13:13:45 +0000711 --tstate->recursion_depth;
712 tstate->frame = f->f_back;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000713 return NULL;
714 }
715 }
716 if (tstate->c_profilefunc != NULL) {
717 /* Similar for c_profilefunc, except it needn't
718 return itself and isn't called for "line" events */
719 if (call_trace(tstate->c_profilefunc,
720 tstate->c_profileobj,
721 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000722 /* Profile function raised an error */
Michael W. Hudsonfb4d6ec2002-10-02 13:13:45 +0000723 --tstate->recursion_depth;
724 tstate->frame = f->f_back;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000725 return NULL;
726 }
727 }
728 }
729
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000730 co = f->f_code;
731 names = co->co_names;
732 consts = co->co_consts;
733 fastlocals = f->f_localsplus;
734 freevars = f->f_localsplus + f->f_nlocals;
735 _PyCode_GETCODEPTR(co, &first_instr);
736 /* An explanation is in order for the next line.
737
738 f->f_lasti now refers to the index of the last instruction
739 executed. You might think this was obvious from the name, but
740 this wasn't always true before 2.3! PyFrame_New now sets
741 f->f_lasti to -1 (i.e. the index *before* the first instruction)
742 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
743 does work. Promise. */
744 next_instr = first_instr + f->f_lasti + 1;
745 stack_pointer = f->f_stacktop;
746 assert(stack_pointer != NULL);
747 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
748
Tim Peters5ca576e2001-06-18 22:08:13 +0000749#ifdef LLTRACE
750 lltrace = PyDict_GetItemString(f->f_globals,"__lltrace__") != NULL;
751#endif
752#if defined(Py_DEBUG) || defined(LLTRACE)
753 filename = PyString_AsString(co->co_filename);
754#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000755
Guido van Rossum374a9221991-04-04 10:40:29 +0000756 why = WHY_NOT;
757 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +0000758 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +0000759 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +0000760
Guido van Rossum374a9221991-04-04 10:40:29 +0000761 for (;;) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000762 assert(stack_pointer >= f->f_valuestack); /* else underflow */
763 assert(STACK_LEVEL() <= f->f_stacksize); /* else overflow */
764
Guido van Rossuma027efa1997-05-05 20:56:21 +0000765 /* Do periodic things. Doing this every time through
766 the loop would add too much overhead, so we do it
767 only every Nth instruction. We also do it if
768 ``things_to_do'' is set, i.e. when an asynchronous
769 event needs attention (e.g. a signal handler or
770 async I/O handler); see Py_AddPendingCall() and
771 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000772
Skip Montanarod581d772002-09-03 20:10:45 +0000773 if (--_Py_Ticker < 0) {
774 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000775 tstate->tick_counter++;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000776 if (things_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +0000777 if (Py_MakePendingCalls() < 0) {
778 why = WHY_EXCEPTION;
779 goto on_error;
780 }
781 }
Guido van Rossumdf0d00e1997-05-20 15:57:49 +0000782#if !defined(HAVE_SIGNAL_H) || defined(macintosh)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000783 /* If we have true signals, the signal handler
784 will call Py_AddPendingCall() so we don't
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000785 have to call PyErr_CheckSignals(). On the
786 Mac and DOS, alas, we have to call it. */
Guido van Rossumb209a111997-04-29 18:18:01 +0000787 if (PyErr_CheckSignals()) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000788 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +0000789 goto on_error;
790 }
Guido van Rossum70d44781997-01-21 06:15:24 +0000791#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000792
Guido van Rossume59214e1994-08-30 08:01:59 +0000793#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000794 if (interpreter_lock) {
795 /* Give another thread a chance */
796
Guido van Rossum25ce5661997-08-02 03:10:38 +0000797 if (PyThreadState_Swap(NULL) != tstate)
798 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000799 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000800
801 /* Other threads may run now */
802
Guido van Rossum65d5b571998-12-21 19:32:43 +0000803 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000804 if (PyThreadState_Swap(tstate) != NULL)
805 Py_FatalError("ceval: orphan tstate");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000806 }
807#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000808 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000809
Neil Schemenauer63543862002-02-17 19:10:14 +0000810 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +0000811 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +0000812
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000813 /* line-by-line tracing support */
814
815 if (tstate->c_tracefunc != NULL && !tstate->tracing) {
816 /* see maybe_call_line_trace
817 for expository comments */
818 f->f_stacktop = stack_pointer;
Michael W. Hudson006c7522002-11-08 13:08:46 +0000819
820 if (maybe_call_line_trace(tstate->c_tracefunc,
821 tstate->c_traceobj,
822 f, &instr_lb, &instr_ub)) {
823 /* trace function raised an exception */
824 why = WHY_EXCEPTION;
825 goto on_error;
826 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000827 /* Reload possibly changed frame fields */
828 JUMPTO(f->f_lasti);
829 stack_pointer = f->f_stacktop;
830 assert(stack_pointer != NULL);
831 f->f_stacktop = NULL;
832 }
833
834 /* Extract opcode and argument */
835
Guido van Rossum374a9221991-04-04 10:40:29 +0000836 opcode = NEXTOP();
837 if (HAS_ARG(opcode))
838 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +0000839 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +0000840#ifdef DYNAMIC_EXECUTION_PROFILE
841#ifdef DXPAIRS
842 dxpairs[lastopcode][opcode]++;
843 lastopcode = opcode;
844#endif
845 dxp[opcode]++;
846#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000847
Guido van Rossum96a42c81992-01-12 02:29:51 +0000848#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +0000849 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +0000850
Guido van Rossum96a42c81992-01-12 02:29:51 +0000851 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000852 if (HAS_ARG(opcode)) {
853 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000854 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +0000855 }
856 else {
857 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000858 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +0000859 }
860 }
861#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000862
Guido van Rossum374a9221991-04-04 10:40:29 +0000863 /* Main switch on opcode */
Jeremy Hylton52820442001-01-03 23:52:36 +0000864
Guido van Rossum374a9221991-04-04 10:40:29 +0000865 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +0000866
Guido van Rossum374a9221991-04-04 10:40:29 +0000867 /* BEWARE!
868 It is essential that any operation that fails sets either
869 x to NULL, err to nonzero, or why to anything but WHY_NOT,
870 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000871
Guido van Rossum374a9221991-04-04 10:40:29 +0000872 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000873
Neil Schemenauer63543862002-02-17 19:10:14 +0000874 case LOAD_FAST:
875 x = GETLOCAL(oparg);
876 if (x != NULL) {
877 Py_INCREF(x);
878 PUSH(x);
879 goto fast_next_opcode;
880 }
881 format_exc_check_arg(PyExc_UnboundLocalError,
882 UNBOUNDLOCAL_ERROR_MSG,
883 PyTuple_GetItem(co->co_varnames, oparg));
884 break;
885
886 case LOAD_CONST:
Skip Montanaro04d80f82002-08-04 21:03:35 +0000887 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +0000888 Py_INCREF(x);
889 PUSH(x);
890 goto fast_next_opcode;
891
Raymond Hettinger7dc52212003-03-16 20:14:44 +0000892 PREDICTED_WITH_ARG(STORE_FAST);
Neil Schemenauer63543862002-02-17 19:10:14 +0000893 case STORE_FAST:
894 v = POP();
895 SETLOCAL(oparg, v);
896 goto fast_next_opcode;
897
Raymond Hettingerf606f872003-03-16 03:11:04 +0000898 PREDICTED(POP_TOP);
Guido van Rossum374a9221991-04-04 10:40:29 +0000899 case POP_TOP:
900 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000901 Py_DECREF(v);
Neil Schemenauer63543862002-02-17 19:10:14 +0000902 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000903
Guido van Rossum374a9221991-04-04 10:40:29 +0000904 case ROT_TWO:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000905 v = TOP();
906 w = SECOND();
907 SET_TOP(w);
908 SET_SECOND(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000909 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000910
Guido van Rossum374a9221991-04-04 10:40:29 +0000911 case ROT_THREE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000912 v = TOP();
913 w = SECOND();
914 x = THIRD();
915 SET_TOP(w);
916 SET_SECOND(x);
917 SET_THIRD(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000918 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000919
Thomas Wouters434d0822000-08-24 20:11:32 +0000920 case ROT_FOUR:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000921 u = TOP();
922 v = SECOND();
923 w = THIRD();
924 x = FOURTH();
925 SET_TOP(v);
926 SET_SECOND(w);
927 SET_THIRD(x);
928 SET_FOURTH(u);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000929 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000930
Guido van Rossum374a9221991-04-04 10:40:29 +0000931 case DUP_TOP:
932 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000933 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000934 PUSH(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000935 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000936
Thomas Wouters434d0822000-08-24 20:11:32 +0000937 case DUP_TOPX:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000938 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000939 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000940 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000941 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000942 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000943 STACKADJ(2);
944 SET_TOP(x);
945 SET_SECOND(w);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000946 goto fast_next_opcode;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000947 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000948 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000949 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000950 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000951 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000952 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +0000953 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000954 STACKADJ(3);
955 SET_TOP(x);
956 SET_SECOND(w);
957 SET_THIRD(v);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000958 goto fast_next_opcode;
Thomas Wouters434d0822000-08-24 20:11:32 +0000959 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000960 Py_FatalError("invalid argument to DUP_TOPX"
961 " (bytecode corruption?)");
Tim Peters35ba6892000-10-11 07:04:49 +0000962 break;
Thomas Wouters434d0822000-08-24 20:11:32 +0000963
Guido van Rossum374a9221991-04-04 10:40:29 +0000964 case UNARY_POSITIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000965 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000966 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000967 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000968 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000969 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000970 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000971
Guido van Rossum374a9221991-04-04 10:40:29 +0000972 case UNARY_NEGATIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000973 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000974 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000975 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000976 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000977 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000978 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000979
Guido van Rossum374a9221991-04-04 10:40:29 +0000980 case UNARY_NOT:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000981 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000982 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000983 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +0000984 if (err == 0) {
985 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000986 SET_TOP(Py_True);
Guido van Rossumfc490731997-05-06 15:06:49 +0000987 continue;
988 }
989 else if (err > 0) {
990 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000991 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +0000992 err = 0;
993 continue;
994 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +0000995 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +0000996 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000997
Guido van Rossum374a9221991-04-04 10:40:29 +0000998 case UNARY_CONVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000999 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001000 x = PyObject_Repr(v);
1001 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001002 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001003 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001004 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001005
Guido van Rossum7928cd71991-10-24 14:59:31 +00001006 case UNARY_INVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001007 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001008 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001009 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001010 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001011 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001012 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001013
Guido van Rossum50564e81996-01-12 01:13:16 +00001014 case BINARY_POWER:
1015 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001016 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001017 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +00001018 Py_DECREF(v);
1019 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001020 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001021 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +00001022 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001023
Guido van Rossum374a9221991-04-04 10:40:29 +00001024 case BINARY_MULTIPLY:
1025 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001026 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001027 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001028 Py_DECREF(v);
1029 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001030 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001031 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001032 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001033
Guido van Rossum374a9221991-04-04 10:40:29 +00001034 case BINARY_DIVIDE:
Tim Peters3caca232001-12-06 06:23:26 +00001035 if (!_Py_QnewFlag) {
1036 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001037 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001038 x = PyNumber_Divide(v, w);
1039 Py_DECREF(v);
1040 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001041 SET_TOP(x);
Tim Peters3caca232001-12-06 06:23:26 +00001042 if (x != NULL) continue;
1043 break;
1044 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001045 /* -Qnew is in effect: fall through to
Tim Peters3caca232001-12-06 06:23:26 +00001046 BINARY_TRUE_DIVIDE */
1047 case BINARY_TRUE_DIVIDE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001048 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001049 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001050 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001051 Py_DECREF(v);
1052 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001053 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001054 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001055 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001056
Guido van Rossum4668b002001-08-08 05:00:18 +00001057 case BINARY_FLOOR_DIVIDE:
1058 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001059 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001060 x = PyNumber_FloorDivide(v, w);
1061 Py_DECREF(v);
1062 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001063 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001064 if (x != NULL) continue;
1065 break;
1066
Guido van Rossum374a9221991-04-04 10:40:29 +00001067 case BINARY_MODULO:
1068 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001069 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001070 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001071 Py_DECREF(v);
1072 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001073 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001074 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001075 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001076
Guido van Rossum374a9221991-04-04 10:40:29 +00001077 case BINARY_ADD:
1078 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001079 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001080 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001081 /* INLINE: int + int */
1082 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001083 a = PyInt_AS_LONG(v);
1084 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001085 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001086 if ((i^a) < 0 && (i^b) < 0)
1087 goto slow_add;
1088 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001089 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001090 else {
1091 slow_add:
Guido van Rossumc12da691997-07-17 23:12:42 +00001092 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001093 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001094 Py_DECREF(v);
1095 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001096 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001097 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001098 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001099
Guido van Rossum374a9221991-04-04 10:40:29 +00001100 case BINARY_SUBTRACT:
1101 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001102 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001103 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001104 /* INLINE: int - int */
1105 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001106 a = PyInt_AS_LONG(v);
1107 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001108 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001109 if ((i^a) < 0 && (i^~b) < 0)
1110 goto slow_sub;
1111 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001112 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001113 else {
1114 slow_sub:
Guido van Rossumc12da691997-07-17 23:12:42 +00001115 x = PyNumber_Subtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001116 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001117 Py_DECREF(v);
1118 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001119 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001120 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001121 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001122
Guido van Rossum374a9221991-04-04 10:40:29 +00001123 case BINARY_SUBSCR:
1124 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001125 v = TOP();
Tim Petersb1c46982001-10-05 20:41:38 +00001126 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001127 /* INLINE: list[int] */
1128 long i = PyInt_AsLong(w);
1129 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +00001130 i += PyList_GET_SIZE(v);
Guido van Rossumc12da691997-07-17 23:12:42 +00001131 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 PyErr_SetString(PyExc_IndexError,
1134 "list index out of range");
1135 x = NULL;
1136 }
1137 else {
Guido van Rossumfa00e951998-07-08 15:02:37 +00001138 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001139 Py_INCREF(x);
1140 }
1141 }
1142 else
1143 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001144 Py_DECREF(v);
1145 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001146 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001147 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001148 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001149
Guido van Rossum7928cd71991-10-24 14:59:31 +00001150 case BINARY_LSHIFT:
1151 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001152 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001153 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001154 Py_DECREF(v);
1155 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001156 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001157 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001158 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001159
Guido van Rossum7928cd71991-10-24 14:59:31 +00001160 case BINARY_RSHIFT:
1161 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001162 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001163 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001164 Py_DECREF(v);
1165 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001166 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001167 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001168 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001169
Guido van Rossum7928cd71991-10-24 14:59:31 +00001170 case BINARY_AND:
1171 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001172 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001173 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001174 Py_DECREF(v);
1175 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001176 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001177 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001178 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001179
Guido van Rossum7928cd71991-10-24 14:59:31 +00001180 case BINARY_XOR:
1181 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001182 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001183 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001184 Py_DECREF(v);
1185 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001186 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001187 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001188 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001189
Guido van Rossum7928cd71991-10-24 14:59:31 +00001190 case BINARY_OR:
1191 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001192 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001193 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001194 Py_DECREF(v);
1195 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001196 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001197 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001198 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001199
1200 case INPLACE_POWER:
1201 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001202 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001203 x = PyNumber_InPlacePower(v, w, Py_None);
1204 Py_DECREF(v);
1205 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001206 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001207 if (x != NULL) continue;
1208 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001209
Thomas Wouters434d0822000-08-24 20:11:32 +00001210 case INPLACE_MULTIPLY:
1211 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001212 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001213 x = PyNumber_InPlaceMultiply(v, w);
1214 Py_DECREF(v);
1215 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001216 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001217 if (x != NULL) continue;
1218 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001219
Thomas Wouters434d0822000-08-24 20:11:32 +00001220 case INPLACE_DIVIDE:
Tim Peters54b11912001-12-25 18:49:11 +00001221 if (!_Py_QnewFlag) {
1222 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001223 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001224 x = PyNumber_InPlaceDivide(v, w);
1225 Py_DECREF(v);
1226 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001227 SET_TOP(x);
Tim Peters54b11912001-12-25 18:49:11 +00001228 if (x != NULL) continue;
1229 break;
1230 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001231 /* -Qnew is in effect: fall through to
Tim Peters54b11912001-12-25 18:49:11 +00001232 INPLACE_TRUE_DIVIDE */
1233 case INPLACE_TRUE_DIVIDE:
Thomas Wouters434d0822000-08-24 20:11:32 +00001234 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001235 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001236 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001237 Py_DECREF(v);
1238 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001239 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001240 if (x != NULL) continue;
1241 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001242
Guido van Rossum4668b002001-08-08 05:00:18 +00001243 case INPLACE_FLOOR_DIVIDE:
1244 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001245 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001246 x = PyNumber_InPlaceFloorDivide(v, w);
1247 Py_DECREF(v);
1248 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001249 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001250 if (x != NULL) continue;
1251 break;
1252
Thomas Wouters434d0822000-08-24 20:11:32 +00001253 case INPLACE_MODULO:
1254 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001255 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001256 x = PyNumber_InPlaceRemainder(v, w);
1257 Py_DECREF(v);
1258 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001259 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001260 if (x != NULL) continue;
1261 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001262
Thomas Wouters434d0822000-08-24 20:11:32 +00001263 case INPLACE_ADD:
1264 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001265 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001266 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001267 /* INLINE: int + int */
1268 register long a, b, i;
1269 a = PyInt_AS_LONG(v);
1270 b = PyInt_AS_LONG(w);
1271 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001272 if ((i^a) < 0 && (i^b) < 0)
1273 goto slow_iadd;
1274 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001275 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001276 else {
1277 slow_iadd:
Thomas Wouters434d0822000-08-24 20:11:32 +00001278 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001279 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001280 Py_DECREF(v);
1281 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001282 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001283 if (x != NULL) continue;
1284 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001285
Thomas Wouters434d0822000-08-24 20:11:32 +00001286 case INPLACE_SUBTRACT:
1287 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001288 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001289 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001290 /* INLINE: int - int */
1291 register long a, b, i;
1292 a = PyInt_AS_LONG(v);
1293 b = PyInt_AS_LONG(w);
1294 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001295 if ((i^a) < 0 && (i^~b) < 0)
1296 goto slow_isub;
1297 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001298 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001299 else {
1300 slow_isub:
Thomas Wouters434d0822000-08-24 20:11:32 +00001301 x = PyNumber_InPlaceSubtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001302 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001303 Py_DECREF(v);
1304 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001305 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001306 if (x != NULL) continue;
1307 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001308
Thomas Wouters434d0822000-08-24 20:11:32 +00001309 case INPLACE_LSHIFT:
1310 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001311 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001312 x = PyNumber_InPlaceLshift(v, w);
1313 Py_DECREF(v);
1314 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001315 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001316 if (x != NULL) continue;
1317 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001318
Thomas Wouters434d0822000-08-24 20:11:32 +00001319 case INPLACE_RSHIFT:
1320 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001321 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001322 x = PyNumber_InPlaceRshift(v, w);
1323 Py_DECREF(v);
1324 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001325 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001326 if (x != NULL) continue;
1327 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001328
Thomas Wouters434d0822000-08-24 20:11:32 +00001329 case INPLACE_AND:
1330 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001331 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001332 x = PyNumber_InPlaceAnd(v, w);
1333 Py_DECREF(v);
1334 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001335 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001336 if (x != NULL) continue;
1337 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001338
Thomas Wouters434d0822000-08-24 20:11:32 +00001339 case INPLACE_XOR:
1340 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001341 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001342 x = PyNumber_InPlaceXor(v, w);
1343 Py_DECREF(v);
1344 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001345 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001346 if (x != NULL) continue;
1347 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001348
Thomas Wouters434d0822000-08-24 20:11:32 +00001349 case INPLACE_OR:
1350 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001351 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001352 x = PyNumber_InPlaceOr(v, w);
1353 Py_DECREF(v);
1354 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001355 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001356 if (x != NULL) continue;
1357 break;
1358
Guido van Rossum374a9221991-04-04 10:40:29 +00001359 case SLICE+0:
1360 case SLICE+1:
1361 case SLICE+2:
1362 case SLICE+3:
1363 if ((opcode-SLICE) & 2)
1364 w = POP();
1365 else
1366 w = NULL;
1367 if ((opcode-SLICE) & 1)
1368 v = POP();
1369 else
1370 v = NULL;
Raymond Hettinger663004b2003-01-09 15:24:30 +00001371 u = TOP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001372 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001373 Py_DECREF(u);
1374 Py_XDECREF(v);
1375 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001376 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001377 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001378 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001379
Guido van Rossum374a9221991-04-04 10:40:29 +00001380 case STORE_SLICE+0:
1381 case STORE_SLICE+1:
1382 case STORE_SLICE+2:
1383 case STORE_SLICE+3:
1384 if ((opcode-STORE_SLICE) & 2)
1385 w = POP();
1386 else
1387 w = NULL;
1388 if ((opcode-STORE_SLICE) & 1)
1389 v = POP();
1390 else
1391 v = NULL;
1392 u = POP();
1393 t = POP();
1394 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +00001395 Py_DECREF(t);
1396 Py_DECREF(u);
1397 Py_XDECREF(v);
1398 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001399 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001400 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001401
Guido van Rossum374a9221991-04-04 10:40:29 +00001402 case DELETE_SLICE+0:
1403 case DELETE_SLICE+1:
1404 case DELETE_SLICE+2:
1405 case DELETE_SLICE+3:
1406 if ((opcode-DELETE_SLICE) & 2)
1407 w = POP();
1408 else
1409 w = NULL;
1410 if ((opcode-DELETE_SLICE) & 1)
1411 v = POP();
1412 else
1413 v = NULL;
1414 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001415 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +00001416 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00001417 Py_DECREF(u);
1418 Py_XDECREF(v);
1419 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001420 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001421 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001422
Guido van Rossum374a9221991-04-04 10:40:29 +00001423 case STORE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001424 w = TOP();
1425 v = SECOND();
1426 u = THIRD();
1427 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001428 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001429 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001430 Py_DECREF(u);
1431 Py_DECREF(v);
1432 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001433 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001434 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001435
Guido van Rossum374a9221991-04-04 10:40:29 +00001436 case DELETE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001437 w = TOP();
1438 v = SECOND();
1439 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001440 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001441 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001442 Py_DECREF(v);
1443 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001444 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001445 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001446
Guido van Rossum374a9221991-04-04 10:40:29 +00001447 case PRINT_EXPR:
1448 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001449 w = PySys_GetObject("displayhook");
1450 if (w == NULL) {
1451 PyErr_SetString(PyExc_RuntimeError,
1452 "lost sys.displayhook");
1453 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001454 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001455 }
1456 if (err == 0) {
1457 x = Py_BuildValue("(O)", v);
1458 if (x == NULL)
1459 err = -1;
1460 }
1461 if (err == 0) {
1462 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001463 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001464 if (w == NULL)
1465 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001466 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001467 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001468 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001469 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001470
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001471 case PRINT_ITEM_TO:
1472 w = stream = POP();
1473 /* fall through to PRINT_ITEM */
1474
Guido van Rossum374a9221991-04-04 10:40:29 +00001475 case PRINT_ITEM:
1476 v = POP();
Barry Warsaw093abe02000-08-29 04:56:13 +00001477 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001478 w = PySys_GetObject("stdout");
1479 if (w == NULL) {
1480 PyErr_SetString(PyExc_RuntimeError,
1481 "lost sys.stdout");
1482 err = -1;
1483 }
Guido van Rossum8f183201997-12-31 05:53:15 +00001484 }
Tim Peters8e5fd532002-03-24 19:25:00 +00001485 if (w != NULL && PyFile_SoftSpace(w, 0))
Guido van Rossumbe270261997-05-22 22:26:18 +00001486 err = PyFile_WriteString(" ", w);
1487 if (err == 0)
1488 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001489 if (err == 0) {
Tim Peters8e5fd532002-03-24 19:25:00 +00001490 /* XXX move into writeobject() ? */
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001491 if (PyString_Check(v)) {
1492 char *s = PyString_AS_STRING(v);
1493 int len = PyString_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001494 if (len == 0 ||
1495 !isspace(Py_CHARMASK(s[len-1])) ||
1496 s[len-1] == ' ')
1497 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001498 }
Martin v. Löwis8d3ce5a2001-12-18 22:36:40 +00001499#ifdef Py_USING_UNICODE
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001500 else if (PyUnicode_Check(v)) {
1501 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
1502 int len = PyUnicode_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001503 if (len == 0 ||
1504 !Py_UNICODE_ISSPACE(s[len-1]) ||
1505 s[len-1] == ' ')
1506 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001507 }
Michael W. Hudsond95c8282002-05-20 13:56:11 +00001508#endif
Tim Peters8e5fd532002-03-24 19:25:00 +00001509 else
1510 PyFile_SoftSpace(w, 1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001511 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001512 Py_DECREF(v);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001513 Py_XDECREF(stream);
1514 stream = NULL;
1515 if (err == 0)
1516 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001517 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001518
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001519 case PRINT_NEWLINE_TO:
1520 w = stream = POP();
1521 /* fall through to PRINT_NEWLINE */
1522
Guido van Rossum374a9221991-04-04 10:40:29 +00001523 case PRINT_NEWLINE:
Barry Warsaw093abe02000-08-29 04:56:13 +00001524 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001525 w = PySys_GetObject("stdout");
1526 if (w == NULL)
1527 PyErr_SetString(PyExc_RuntimeError,
1528 "lost sys.stdout");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001529 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001530 if (w != NULL) {
1531 err = PyFile_WriteString("\n", w);
1532 if (err == 0)
1533 PyFile_SoftSpace(w, 0);
1534 }
1535 Py_XDECREF(stream);
1536 stream = NULL;
Guido van Rossum374a9221991-04-04 10:40:29 +00001537 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001538
Thomas Wouters434d0822000-08-24 20:11:32 +00001539
1540#ifdef CASE_TOO_BIG
1541 default: switch (opcode) {
1542#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001543 case BREAK_LOOP:
1544 why = WHY_BREAK;
1545 break;
Guido van Rossum66b0e9c2001-03-21 19:17:22 +00001546
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00001547 case CONTINUE_LOOP:
1548 retval = PyInt_FromLong(oparg);
1549 why = WHY_CONTINUE;
1550 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001551
Guido van Rossumf10570b1995-07-07 22:53:21 +00001552 case RAISE_VARARGS:
1553 u = v = w = NULL;
1554 switch (oparg) {
1555 case 3:
1556 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001557 /* Fallthrough */
1558 case 2:
1559 v = POP(); /* value */
1560 /* Fallthrough */
1561 case 1:
1562 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001563 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001564 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001565 break;
1566 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001567 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001568 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001569 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001570 break;
1571 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001572 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001573
Guido van Rossum374a9221991-04-04 10:40:29 +00001574 case LOAD_LOCALS:
Guido van Rossum681d79a1995-07-18 14:51:37 +00001575 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001576 PyErr_SetString(PyExc_SystemError,
1577 "no locals");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001578 break;
1579 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001580 Py_INCREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001581 PUSH(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001582 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001583
Guido van Rossum374a9221991-04-04 10:40:29 +00001584 case RETURN_VALUE:
1585 retval = POP();
1586 why = WHY_RETURN;
1587 break;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001588
Tim Peters5ca576e2001-06-18 22:08:13 +00001589 case YIELD_VALUE:
1590 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001591 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001592 why = WHY_YIELD;
1593 break;
1594
1595
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001596 case EXEC_STMT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001597 w = TOP();
1598 v = SECOND();
1599 u = THIRD();
1600 STACKADJ(-3);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001601 err = exec_statement(f, u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001602 Py_DECREF(u);
1603 Py_DECREF(v);
1604 Py_DECREF(w);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001605 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001606
Guido van Rossum374a9221991-04-04 10:40:29 +00001607 case POP_BLOCK:
1608 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001609 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001610 while (STACK_LEVEL() > b->b_level) {
1611 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001612 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001613 }
1614 }
1615 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001616
Guido van Rossum374a9221991-04-04 10:40:29 +00001617 case END_FINALLY:
1618 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001619 if (PyInt_Check(v)) {
Raymond Hettinger080cb322003-03-14 01:37:42 +00001620 why = (enum why_code) PyInt_AS_LONG(v);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00001621 if (why == WHY_RETURN ||
Tim Peters5ca576e2001-06-18 22:08:13 +00001622 why == WHY_YIELD ||
Guido van Rossumc5fe5eb2002-06-12 03:45:21 +00001623 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001624 retval = POP();
1625 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001626 else if (PyString_Check(v) || PyClass_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001627 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001628 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001629 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001630 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001631 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001632 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001633 else if (v != Py_None) {
1634 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001635 "'finally' pops bad exception");
1636 why = WHY_EXCEPTION;
1637 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001638 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001639 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001640
Guido van Rossum374a9221991-04-04 10:40:29 +00001641 case BUILD_CLASS:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001642 u = TOP();
1643 v = SECOND();
1644 w = THIRD();
1645 STACKADJ(-2);
Guido van Rossum25831651993-05-19 14:50:45 +00001646 x = build_class(u, v, w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001647 SET_TOP(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001648 Py_DECREF(u);
1649 Py_DECREF(v);
1650 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001651 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001652
Guido van Rossum374a9221991-04-04 10:40:29 +00001653 case STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001654 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001655 v = POP();
Guido van Rossum681d79a1995-07-18 14:51:37 +00001656 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001657 PyErr_Format(PyExc_SystemError,
1658 "no locals found when storing %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001659 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001660 break;
1661 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001662 err = PyDict_SetItem(x, w, v);
1663 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001664 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001665
Guido van Rossum374a9221991-04-04 10:40:29 +00001666 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001667 w = GETITEM(names, oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001668 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001669 PyErr_Format(PyExc_SystemError,
1670 "no locals when deleting %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001671 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001672 break;
1673 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001674 if ((err = PyDict_DelItem(x, w)) != 0)
Guido van Rossumac7be682001-01-17 15:42:30 +00001675 format_exc_check_arg(PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001676 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001677 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001678
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001679 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001680 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001681 v = POP();
Raymond Hettinger21012b82003-02-26 18:11:50 +00001682 if (PyTuple_CheckExact(v)) {
Barry Warsawe42b18f1997-08-25 22:13:04 +00001683 if (PyTuple_Size(v) != oparg) {
1684 PyErr_SetString(PyExc_ValueError,
1685 "unpack tuple of wrong size");
1686 why = WHY_EXCEPTION;
1687 }
1688 else {
1689 for (; --oparg >= 0; ) {
1690 w = PyTuple_GET_ITEM(v, oparg);
1691 Py_INCREF(w);
1692 PUSH(w);
1693 }
1694 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001695 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00001696 else if (PyList_CheckExact(v)) {
Barry Warsawe42b18f1997-08-25 22:13:04 +00001697 if (PyList_Size(v) != oparg) {
1698 PyErr_SetString(PyExc_ValueError,
1699 "unpack list of wrong size");
1700 why = WHY_EXCEPTION;
1701 }
1702 else {
1703 for (; --oparg >= 0; ) {
1704 w = PyList_GET_ITEM(v, oparg);
1705 Py_INCREF(w);
1706 PUSH(w);
1707 }
1708 }
1709 }
Tim Petersd6d010b2001-06-21 02:49:55 +00001710 else if (unpack_iterable(v, oparg,
1711 stack_pointer + oparg))
1712 stack_pointer += oparg;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001713 else {
1714 if (PyErr_ExceptionMatches(PyExc_TypeError))
1715 PyErr_SetString(PyExc_TypeError,
1716 "unpack non-sequence");
Barry Warsawe42b18f1997-08-25 22:13:04 +00001717 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001718 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001719 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001720 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001721
Guido van Rossum374a9221991-04-04 10:40:29 +00001722 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001723 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001724 v = TOP();
1725 u = SECOND();
1726 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001727 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1728 Py_DECREF(v);
1729 Py_DECREF(u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001730 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001731
Guido van Rossum374a9221991-04-04 10:40:29 +00001732 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001733 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001734 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001735 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1736 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001737 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001738 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001739
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001740 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001741 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001742 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001743 err = PyDict_SetItem(f->f_globals, w, v);
1744 Py_DECREF(v);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001745 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001746
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001747 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001748 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001749 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001750 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001751 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001752 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001753
Guido van Rossum374a9221991-04-04 10:40:29 +00001754 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001755 w = GETITEM(names, oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001756 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001757 PyErr_Format(PyExc_SystemError,
1758 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001759 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001760 break;
1761 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001762 x = PyDict_GetItem(x, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001763 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001764 x = PyDict_GetItem(f->f_globals, 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_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001767 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001768 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001769 PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001770 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001771 break;
1772 }
1773 }
1774 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001775 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001776 PUSH(x);
1777 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001778
Guido van Rossum374a9221991-04-04 10:40:29 +00001779 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001780 w = GETITEM(names, oparg);
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001781 if (PyString_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00001782 /* Inline the PyDict_GetItem() calls.
1783 WARNING: this is an extreme speed hack.
1784 Do not try this at home. */
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001785 long hash = ((PyStringObject *)w)->ob_shash;
1786 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001787 PyDictObject *d;
1788 d = (PyDictObject *)(f->f_globals);
1789 x = d->ma_lookup(d, w, hash)->me_value;
1790 if (x != NULL) {
1791 Py_INCREF(x);
1792 PUSH(x);
1793 continue;
1794 }
1795 d = (PyDictObject *)(f->f_builtins);
1796 x = d->ma_lookup(d, w, hash)->me_value;
1797 if (x != NULL) {
1798 Py_INCREF(x);
1799 PUSH(x);
1800 continue;
1801 }
1802 goto load_global_error;
1803 }
1804 }
1805 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00001806 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001807 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001808 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001809 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001810 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00001811 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001812 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001813 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001814 break;
1815 }
1816 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001817 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001818 PUSH(x);
1819 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001820
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001821 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001822 x = GETLOCAL(oparg);
1823 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001824 format_exc_check_arg(
1825 PyExc_UnboundLocalError,
1826 UNBOUNDLOCAL_ERROR_MSG,
1827 PyTuple_GetItem(co->co_varnames, oparg)
1828 );
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001829 break;
1830 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00001831 SETLOCAL(oparg, NULL);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001832 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001833
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001834 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001835 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001836 Py_INCREF(x);
1837 PUSH(x);
1838 break;
1839
1840 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001841 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001842 w = PyCell_Get(x);
Jeremy Hylton2524d692001-02-05 17:23:16 +00001843 if (w == NULL) {
Jeremy Hylton76c81ee2002-07-11 16:56:38 +00001844 err = -1;
1845 /* Don't stomp existing exception */
1846 if (PyErr_Occurred())
1847 break;
Jeremy Hyltonc76770c2001-04-13 16:51:46 +00001848 if (oparg < f->f_ncells) {
Jeremy Hylton2524d692001-02-05 17:23:16 +00001849 v = PyTuple_GetItem(co->co_cellvars,
1850 oparg);
Jeremy Hyltonc76770c2001-04-13 16:51:46 +00001851 format_exc_check_arg(
1852 PyExc_UnboundLocalError,
1853 UNBOUNDLOCAL_ERROR_MSG,
1854 v);
1855 } else {
Jeremy Hylton2524d692001-02-05 17:23:16 +00001856 v = PyTuple_GetItem(
1857 co->co_freevars,
1858 oparg - f->f_ncells);
Jeremy Hyltonc76770c2001-04-13 16:51:46 +00001859 format_exc_check_arg(
1860 PyExc_NameError,
1861 UNBOUNDFREE_ERROR_MSG,
1862 v);
1863 }
Jeremy Hylton2524d692001-02-05 17:23:16 +00001864 break;
1865 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001866 PUSH(w);
1867 break;
1868
1869 case STORE_DEREF:
1870 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001871 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001872 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00001873 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001874 continue;
1875
Guido van Rossum374a9221991-04-04 10:40:29 +00001876 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001877 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001878 if (x != NULL) {
1879 for (; --oparg >= 0;) {
1880 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001881 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001882 }
1883 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001884 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001885 }
1886 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001887
Guido van Rossum374a9221991-04-04 10:40:29 +00001888 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00001889 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001890 if (x != NULL) {
1891 for (; --oparg >= 0;) {
1892 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00001893 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001894 }
1895 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001896 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001897 }
1898 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001899
Guido van Rossum374a9221991-04-04 10:40:29 +00001900 case BUILD_MAP:
Guido van Rossumb209a111997-04-29 18:18:01 +00001901 x = PyDict_New();
Guido van Rossum374a9221991-04-04 10:40:29 +00001902 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001903 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001904 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001905
Guido van Rossum374a9221991-04-04 10:40:29 +00001906 case LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001907 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001908 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001909 x = PyObject_GetAttr(v, w);
1910 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001911 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001912 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001913 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001914
Guido van Rossum374a9221991-04-04 10:40:29 +00001915 case COMPARE_OP:
1916 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001917 v = TOP();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001918 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001919 /* INLINE: cmp(int, int) */
1920 register long a, b;
1921 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001922 a = PyInt_AS_LONG(v);
1923 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001924 switch (oparg) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00001925 case PyCmp_LT: res = a < b; break;
1926 case PyCmp_LE: res = a <= b; break;
1927 case PyCmp_EQ: res = a == b; break;
1928 case PyCmp_NE: res = a != b; break;
1929 case PyCmp_GT: res = a > b; break;
1930 case PyCmp_GE: res = a >= b; break;
1931 case PyCmp_IS: res = v == w; break;
1932 case PyCmp_IS_NOT: res = v != w; break;
Guido van Rossumc12da691997-07-17 23:12:42 +00001933 default: goto slow_compare;
1934 }
1935 x = res ? Py_True : Py_False;
1936 Py_INCREF(x);
1937 }
1938 else {
1939 slow_compare:
1940 x = cmp_outcome(oparg, v, w);
1941 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001942 Py_DECREF(v);
1943 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001944 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001945 if (x == NULL) break;
1946 PREDICT(JUMP_IF_FALSE);
1947 PREDICT(JUMP_IF_TRUE);
1948 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001949
Guido van Rossum374a9221991-04-04 10:40:29 +00001950 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001951 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001952 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001953 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001954 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00001955 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001956 break;
1957 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001958 u = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001959 w = Py_BuildValue("(OOOO)",
Guido van Rossum681d79a1995-07-18 14:51:37 +00001960 w,
1961 f->f_globals,
Guido van Rossuma027efa1997-05-05 20:56:21 +00001962 f->f_locals == NULL ?
1963 Py_None : f->f_locals,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001964 u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001965 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001966 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001967 u = POP();
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001968 x = NULL;
1969 break;
1970 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001971 x = PyEval_CallObject(x, w);
1972 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001973 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001974 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001975 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001976
Thomas Wouters52152252000-08-17 22:55:00 +00001977 case IMPORT_STAR:
1978 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001979 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001980 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001981 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001982 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001983 break;
1984 }
Thomas Wouters52152252000-08-17 22:55:00 +00001985 err = import_all_from(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001986 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00001987 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001988 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001989 break;
Guido van Rossum25831651993-05-19 14:50:45 +00001990
Thomas Wouters52152252000-08-17 22:55:00 +00001991 case IMPORT_FROM:
Skip Montanaro496e6582002-08-06 17:47:40 +00001992 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00001993 v = TOP();
1994 x = import_from(v, w);
1995 PUSH(x);
1996 if (x != NULL) continue;
1997 break;
1998
Guido van Rossum374a9221991-04-04 10:40:29 +00001999 case JUMP_FORWARD:
2000 JUMPBY(oparg);
Raymond Hettinger080cb322003-03-14 01:37:42 +00002001 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002002
Raymond Hettingerf606f872003-03-16 03:11:04 +00002003 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002004 case JUMP_IF_FALSE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002005 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002006 if (w == Py_True) {
2007 PREDICT(POP_TOP);
Raymond Hettinger080cb322003-03-14 01:37:42 +00002008 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002009 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002010 if (w == Py_False) {
2011 JUMPBY(oparg);
Raymond Hettinger080cb322003-03-14 01:37:42 +00002012 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002013 }
2014 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002015 if (err > 0)
2016 err = 0;
2017 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00002018 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002019 else
2020 break;
2021 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002022
Raymond Hettingerf606f872003-03-16 03:11:04 +00002023 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002024 case JUMP_IF_TRUE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002025 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002026 if (w == Py_False) {
2027 PREDICT(POP_TOP);
Raymond Hettinger080cb322003-03-14 01:37:42 +00002028 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002029 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002030 if (w == Py_True) {
2031 JUMPBY(oparg);
Raymond Hettinger080cb322003-03-14 01:37:42 +00002032 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002033 }
2034 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002035 if (err > 0) {
2036 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00002037 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002038 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002039 else if (err == 0)
2040 ;
2041 else
2042 break;
2043 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002044
Guido van Rossum374a9221991-04-04 10:40:29 +00002045 case JUMP_ABSOLUTE:
2046 JUMPTO(oparg);
Raymond Hettinger080cb322003-03-14 01:37:42 +00002047 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002048
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002049 case GET_ITER:
2050 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002051 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002052 x = PyObject_GetIter(v);
2053 Py_DECREF(v);
2054 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002055 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002056 PREDICT(FOR_ITER);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002057 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002058 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002059 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002060 break;
2061
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002062 PREDICTED_WITH_ARG(FOR_ITER);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002063 case FOR_ITER:
2064 /* before: [iter]; after: [iter, iter()] *or* [] */
2065 v = TOP();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002066 x = PyIter_Next(v);
2067 if (x != NULL) {
2068 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002069 PREDICT(STORE_FAST);
2070 PREDICT(UNPACK_SEQUENCE);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002071 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002072 }
Tim Petersf4848da2001-05-05 00:14:56 +00002073 if (!PyErr_Occurred()) {
2074 /* iterator ended normally */
2075 x = v = POP();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002076 Py_DECREF(v);
2077 JUMPBY(oparg);
2078 continue;
2079 }
2080 break;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002081
Guido van Rossum374a9221991-04-04 10:40:29 +00002082 case SETUP_LOOP:
2083 case SETUP_EXCEPT:
2084 case SETUP_FINALLY:
Guido van Rossumb209a111997-04-29 18:18:01 +00002085 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002086 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002087 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002088
Guido van Rossumf10570b1995-07-07 22:53:21 +00002089 case CALL_FUNCTION:
Jeremy Hylton985eba52003-02-05 23:13:00 +00002090 PCALL(PCALL_ALL);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002091 x = call_function(&stack_pointer, oparg);
2092 PUSH(x);
2093 if (x != NULL)
2094 continue;
2095 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002096
Jeremy Hylton76901512000-03-28 23:49:17 +00002097 case CALL_FUNCTION_VAR:
2098 case CALL_FUNCTION_KW:
2099 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002100 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002101 int na = oparg & 0xff;
2102 int nk = (oparg>>8) & 0xff;
2103 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002104 int n = na + 2 * nk;
2105 PyObject **pfunc, *func;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002106 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002107 if (flags & CALL_FLAG_VAR)
2108 n++;
2109 if (flags & CALL_FLAG_KW)
2110 n++;
2111 pfunc = stack_pointer - n - 1;
2112 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002113
Guido van Rossumac7be682001-01-17 15:42:30 +00002114 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002115 && PyMethod_GET_SELF(func) != NULL) {
2116 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002117 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002118 func = PyMethod_GET_FUNCTION(func);
2119 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002120 Py_DECREF(*pfunc);
2121 *pfunc = self;
2122 na++;
2123 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002124 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002125 Py_INCREF(func);
2126 x = ext_do_call(func, &stack_pointer, flags, na, nk);
Jeremy Hylton76901512000-03-28 23:49:17 +00002127 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002128
Jeremy Hylton76901512000-03-28 23:49:17 +00002129 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002130 w = POP();
2131 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002132 }
2133 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002134 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002135 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002136 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002137 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002138
Guido van Rossum681d79a1995-07-18 14:51:37 +00002139 case MAKE_FUNCTION:
2140 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002141 x = PyFunction_New(v, f->f_globals);
2142 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002143 /* XXX Maybe this should be a separate opcode? */
2144 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002145 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002146 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002147 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002148 x = NULL;
2149 break;
2150 }
2151 while (--oparg >= 0) {
2152 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002153 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002154 }
2155 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002156 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002157 }
2158 PUSH(x);
2159 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002160
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002161 case MAKE_CLOSURE:
2162 {
2163 int nfree;
2164 v = POP(); /* code object */
2165 x = PyFunction_New(v, f->f_globals);
Jeremy Hylton733c8932001-12-13 19:51:56 +00002166 nfree = PyCode_GetNumFree((PyCodeObject *)v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002167 Py_DECREF(v);
2168 /* XXX Maybe this should be a separate opcode? */
2169 if (x != NULL && nfree > 0) {
2170 v = PyTuple_New(nfree);
2171 if (v == NULL) {
2172 Py_DECREF(x);
2173 x = NULL;
2174 break;
2175 }
2176 while (--nfree >= 0) {
2177 w = POP();
2178 PyTuple_SET_ITEM(v, nfree, w);
2179 }
2180 err = PyFunction_SetClosure(x, v);
2181 Py_DECREF(v);
2182 }
2183 if (x != NULL && oparg > 0) {
2184 v = PyTuple_New(oparg);
2185 if (v == NULL) {
2186 Py_DECREF(x);
2187 x = NULL;
2188 break;
2189 }
2190 while (--oparg >= 0) {
2191 w = POP();
2192 PyTuple_SET_ITEM(v, oparg, w);
2193 }
2194 err = PyFunction_SetDefaults(x, v);
2195 Py_DECREF(v);
2196 }
2197 PUSH(x);
2198 break;
2199 }
2200
Guido van Rossum8861b741996-07-30 16:49:37 +00002201 case BUILD_SLICE:
2202 if (oparg == 3)
2203 w = POP();
2204 else
2205 w = NULL;
2206 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002207 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002208 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002209 Py_DECREF(u);
2210 Py_DECREF(v);
2211 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002212 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002213 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002214 break;
2215
Fred Drakeef8ace32000-08-24 00:32:09 +00002216 case EXTENDED_ARG:
2217 opcode = NEXTOP();
2218 oparg = oparg<<16 | NEXTARG();
2219 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002220
Guido van Rossum374a9221991-04-04 10:40:29 +00002221 default:
2222 fprintf(stderr,
2223 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002224 PyCode_Addr2Line(f->f_code, f->f_lasti),
2225 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002226 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002227 why = WHY_EXCEPTION;
2228 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002229
2230#ifdef CASE_TOO_BIG
2231 }
2232#endif
2233
Guido van Rossum374a9221991-04-04 10:40:29 +00002234 } /* switch */
2235
2236 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002237
Guido van Rossum374a9221991-04-04 10:40:29 +00002238 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002239
Guido van Rossum374a9221991-04-04 10:40:29 +00002240 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002241 if (err == 0 && x != NULL) {
2242#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002243 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002244 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002245 fprintf(stderr,
2246 "XXX undetected error\n");
2247 else
2248#endif
2249 continue; /* Normal, fast path */
2250 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002251 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002252 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002253 err = 0;
2254 }
2255
Guido van Rossum374a9221991-04-04 10:40:29 +00002256 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002257
Guido van Rossum374a9221991-04-04 10:40:29 +00002258 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002259 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002260 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002261 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002262 why = WHY_EXCEPTION;
2263 }
2264 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002265#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002266 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002267 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002268 if (PyErr_Occurred()) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002269 fprintf(stderr,
2270 "XXX undetected error (why=%d)\n",
2271 why);
2272 why = WHY_EXCEPTION;
2273 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002274 }
2275#endif
2276
2277 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002278
Guido van Rossum374a9221991-04-04 10:40:29 +00002279 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002280 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002281
Fred Drake8f51f542001-10-04 14:48:42 +00002282 if (tstate->c_tracefunc != NULL)
2283 call_exc_trace(tstate->c_tracefunc,
2284 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002285 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002286
Guido van Rossum374a9221991-04-04 10:40:29 +00002287 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002288
Guido van Rossum374a9221991-04-04 10:40:29 +00002289 if (why == WHY_RERAISE)
2290 why = WHY_EXCEPTION;
2291
2292 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002293
Tim Peters5ca576e2001-06-18 22:08:13 +00002294 while (why != WHY_NOT && why != WHY_YIELD && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002295 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002296
2297 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2298 /* For a continue inside a try block,
2299 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002300 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2301 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002302 why = WHY_NOT;
2303 JUMPTO(PyInt_AS_LONG(retval));
2304 Py_DECREF(retval);
2305 break;
2306 }
2307
Guido van Rossum374a9221991-04-04 10:40:29 +00002308 while (STACK_LEVEL() > b->b_level) {
2309 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002310 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002311 }
2312 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2313 why = WHY_NOT;
2314 JUMPTO(b->b_handler);
2315 break;
2316 }
2317 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002318 (b->b_type == SETUP_EXCEPT &&
2319 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002320 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002321 PyObject *exc, *val, *tb;
2322 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002323 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002324 val = Py_None;
2325 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002326 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002327 /* Make the raw exception data
2328 available to the handler,
2329 so a program can emulate the
2330 Python main loop. Don't do
2331 this for 'finally'. */
2332 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002333 PyErr_NormalizeException(
2334 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002335 set_exc_info(tstate,
2336 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002337 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002338 if (tb == NULL) {
2339 Py_INCREF(Py_None);
2340 PUSH(Py_None);
2341 } else
2342 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002343 PUSH(val);
2344 PUSH(exc);
2345 }
2346 else {
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002347 if (why == WHY_RETURN ||
Guido van Rossumc5fe5eb2002-06-12 03:45:21 +00002348 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00002349 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002350 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002351 PUSH(v);
2352 }
2353 why = WHY_NOT;
2354 JUMPTO(b->b_handler);
2355 break;
2356 }
2357 } /* unwind stack */
2358
2359 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002360
Guido van Rossum374a9221991-04-04 10:40:29 +00002361 if (why != WHY_NOT)
2362 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002363
Guido van Rossum374a9221991-04-04 10:40:29 +00002364 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002365
Guido van Rossum35974fb2001-12-06 21:28:18 +00002366 if (why != WHY_YIELD) {
2367 /* Pop remaining stack entries -- but when yielding */
2368 while (!EMPTY()) {
2369 v = POP();
2370 Py_XDECREF(v);
2371 }
2372 }
2373
Tim Peters5ca576e2001-06-18 22:08:13 +00002374 if (why != WHY_RETURN && why != WHY_YIELD)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002375 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002376
Fred Drake9e3ad782001-07-03 23:39:52 +00002377 if (tstate->use_tracing) {
2378 if (tstate->c_tracefunc
2379 && (why == WHY_RETURN || why == WHY_YIELD)) {
2380 if (call_trace(tstate->c_tracefunc,
2381 tstate->c_traceobj, f,
2382 PyTrace_RETURN, retval)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002383 Py_XDECREF(retval);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002384 retval = NULL;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002385 why = WHY_EXCEPTION;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002386 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002387 }
Fred Drake8f51f542001-10-04 14:48:42 +00002388 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002389 if (why == WHY_EXCEPTION)
2390 call_trace_protected(tstate->c_profilefunc,
2391 tstate->c_profileobj, f,
2392 PyTrace_RETURN);
2393 else if (call_trace(tstate->c_profilefunc,
2394 tstate->c_profileobj, f,
2395 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002396 Py_XDECREF(retval);
2397 retval = NULL;
2398 why = WHY_EXCEPTION;
2399 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002400 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002401 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002402
Guido van Rossuma027efa1997-05-05 20:56:21 +00002403 reset_exc_info(tstate);
2404
Tim Peters5ca576e2001-06-18 22:08:13 +00002405 /* pop frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002406 --tstate->recursion_depth;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002407 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002408
Guido van Rossum96a42c81992-01-12 02:29:51 +00002409 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002410}
2411
Tim Peters6d6c1a32001-08-02 04:15:00 +00002412PyObject *
2413PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002414 PyObject **args, int argcount, PyObject **kws, int kwcount,
2415 PyObject **defs, int defcount, PyObject *closure)
2416{
2417 register PyFrameObject *f;
2418 register PyObject *retval = NULL;
2419 register PyObject **fastlocals, **freevars;
2420 PyThreadState *tstate = PyThreadState_GET();
2421 PyObject *x, *u;
2422
2423 if (globals == NULL) {
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002424 PyErr_SetString(PyExc_SystemError,
2425 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002426 return NULL;
2427 }
2428
Jeremy Hylton985eba52003-02-05 23:13:00 +00002429 assert(globals != NULL);
2430 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002431 if (f == NULL)
2432 return NULL;
2433
2434 fastlocals = f->f_localsplus;
2435 freevars = f->f_localsplus + f->f_nlocals;
2436
2437 if (co->co_argcount > 0 ||
2438 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2439 int i;
2440 int n = argcount;
2441 PyObject *kwdict = NULL;
2442 if (co->co_flags & CO_VARKEYWORDS) {
2443 kwdict = PyDict_New();
2444 if (kwdict == NULL)
2445 goto fail;
2446 i = co->co_argcount;
2447 if (co->co_flags & CO_VARARGS)
2448 i++;
2449 SETLOCAL(i, kwdict);
2450 }
2451 if (argcount > co->co_argcount) {
2452 if (!(co->co_flags & CO_VARARGS)) {
2453 PyErr_Format(PyExc_TypeError,
2454 "%.200s() takes %s %d "
2455 "%sargument%s (%d given)",
2456 PyString_AsString(co->co_name),
2457 defcount ? "at most" : "exactly",
2458 co->co_argcount,
2459 kwcount ? "non-keyword " : "",
2460 co->co_argcount == 1 ? "" : "s",
2461 argcount);
2462 goto fail;
2463 }
2464 n = co->co_argcount;
2465 }
2466 for (i = 0; i < n; i++) {
2467 x = args[i];
2468 Py_INCREF(x);
2469 SETLOCAL(i, x);
2470 }
2471 if (co->co_flags & CO_VARARGS) {
2472 u = PyTuple_New(argcount - n);
2473 if (u == NULL)
2474 goto fail;
2475 SETLOCAL(co->co_argcount, u);
2476 for (i = n; i < argcount; i++) {
2477 x = args[i];
2478 Py_INCREF(x);
2479 PyTuple_SET_ITEM(u, i-n, x);
2480 }
2481 }
2482 for (i = 0; i < kwcount; i++) {
2483 PyObject *keyword = kws[2*i];
2484 PyObject *value = kws[2*i + 1];
2485 int j;
2486 if (keyword == NULL || !PyString_Check(keyword)) {
2487 PyErr_Format(PyExc_TypeError,
2488 "%.200s() keywords must be strings",
2489 PyString_AsString(co->co_name));
2490 goto fail;
2491 }
2492 /* XXX slow -- speed up using dictionary? */
2493 for (j = 0; j < co->co_argcount; j++) {
2494 PyObject *nm = PyTuple_GET_ITEM(
2495 co->co_varnames, j);
2496 int cmp = PyObject_RichCompareBool(
2497 keyword, nm, Py_EQ);
2498 if (cmp > 0)
2499 break;
2500 else if (cmp < 0)
2501 goto fail;
2502 }
2503 /* Check errors from Compare */
2504 if (PyErr_Occurred())
2505 goto fail;
2506 if (j >= co->co_argcount) {
2507 if (kwdict == NULL) {
2508 PyErr_Format(PyExc_TypeError,
2509 "%.200s() got an unexpected "
2510 "keyword argument '%.400s'",
2511 PyString_AsString(co->co_name),
2512 PyString_AsString(keyword));
2513 goto fail;
2514 }
2515 PyDict_SetItem(kwdict, keyword, value);
2516 }
2517 else {
2518 if (GETLOCAL(j) != NULL) {
2519 PyErr_Format(PyExc_TypeError,
2520 "%.200s() got multiple "
2521 "values for keyword "
2522 "argument '%.400s'",
2523 PyString_AsString(co->co_name),
2524 PyString_AsString(keyword));
2525 goto fail;
2526 }
2527 Py_INCREF(value);
2528 SETLOCAL(j, value);
2529 }
2530 }
2531 if (argcount < co->co_argcount) {
2532 int m = co->co_argcount - defcount;
2533 for (i = argcount; i < m; i++) {
2534 if (GETLOCAL(i) == NULL) {
2535 PyErr_Format(PyExc_TypeError,
2536 "%.200s() takes %s %d "
2537 "%sargument%s (%d given)",
2538 PyString_AsString(co->co_name),
2539 ((co->co_flags & CO_VARARGS) ||
2540 defcount) ? "at least"
2541 : "exactly",
2542 m, kwcount ? "non-keyword " : "",
2543 m == 1 ? "" : "s", i);
2544 goto fail;
2545 }
2546 }
2547 if (n > m)
2548 i = n - m;
2549 else
2550 i = 0;
2551 for (; i < defcount; i++) {
2552 if (GETLOCAL(m+i) == NULL) {
2553 PyObject *def = defs[i];
2554 Py_INCREF(def);
2555 SETLOCAL(m+i, def);
2556 }
2557 }
2558 }
2559 }
2560 else {
2561 if (argcount > 0 || kwcount > 0) {
2562 PyErr_Format(PyExc_TypeError,
2563 "%.200s() takes no arguments (%d given)",
2564 PyString_AsString(co->co_name),
2565 argcount + kwcount);
2566 goto fail;
2567 }
2568 }
2569 /* Allocate and initialize storage for cell vars, and copy free
2570 vars into frame. This isn't too efficient right now. */
2571 if (f->f_ncells) {
2572 int i = 0, j = 0, nargs, found;
2573 char *cellname, *argname;
2574 PyObject *c;
2575
2576 nargs = co->co_argcount;
2577 if (co->co_flags & CO_VARARGS)
2578 nargs++;
2579 if (co->co_flags & CO_VARKEYWORDS)
2580 nargs++;
2581
2582 /* Check for cells that shadow args */
2583 for (i = 0; i < f->f_ncells && j < nargs; ++i) {
2584 cellname = PyString_AS_STRING(
2585 PyTuple_GET_ITEM(co->co_cellvars, i));
2586 found = 0;
2587 while (j < nargs) {
2588 argname = PyString_AS_STRING(
2589 PyTuple_GET_ITEM(co->co_varnames, j));
2590 if (strcmp(cellname, argname) == 0) {
2591 c = PyCell_New(GETLOCAL(j));
2592 if (c == NULL)
2593 goto fail;
2594 GETLOCAL(f->f_nlocals + i) = c;
2595 found = 1;
2596 break;
2597 }
2598 j++;
2599 }
2600 if (found == 0) {
2601 c = PyCell_New(NULL);
2602 if (c == NULL)
2603 goto fail;
2604 SETLOCAL(f->f_nlocals + i, c);
2605 }
2606 }
2607 /* Initialize any that are left */
2608 while (i < f->f_ncells) {
2609 c = PyCell_New(NULL);
2610 if (c == NULL)
2611 goto fail;
2612 SETLOCAL(f->f_nlocals + i, c);
2613 i++;
2614 }
2615 }
2616 if (f->f_nfreevars) {
2617 int i;
2618 for (i = 0; i < f->f_nfreevars; ++i) {
2619 PyObject *o = PyTuple_GET_ITEM(closure, i);
2620 Py_INCREF(o);
2621 freevars[f->f_ncells + i] = o;
2622 }
2623 }
2624
Tim Peters5ca576e2001-06-18 22:08:13 +00002625 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002626 /* Don't need to keep the reference to f_back, it will be set
2627 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002628 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002629 f->f_back = NULL;
2630
Jeremy Hylton985eba52003-02-05 23:13:00 +00002631 PCALL(PCALL_GENERATOR);
2632
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002633 /* Create a new generator that owns the ready to run frame
2634 * and return that as the value. */
Tim Peters5ca576e2001-06-18 22:08:13 +00002635 return gen_new(f);
2636 }
2637
2638 retval = eval_frame(f);
2639
2640 fail: /* Jump here from prelude on failure */
2641
Tim Petersb13680b2001-11-27 23:29:29 +00002642 /* decref'ing the frame can cause __del__ methods to get invoked,
2643 which can call back into Python. While we're done with the
2644 current Python frame (f), the associated C stack is still in use,
2645 so recursion_depth must be boosted for the duration.
2646 */
2647 assert(tstate != NULL);
2648 ++tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002649 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00002650 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002651 return retval;
2652}
2653
2654
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002655/* Implementation notes for set_exc_info() and reset_exc_info():
2656
2657- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2658 'exc_traceback'. These always travel together.
2659
2660- tstate->curexc_ZZZ is the "hot" exception that is set by
2661 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2662
2663- Once an exception is caught by an except clause, it is transferred
2664 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2665 can pick it up. This is the primary task of set_exc_info().
2666
2667- Now let me explain the complicated dance with frame->f_exc_ZZZ.
2668
2669 Long ago, when none of this existed, there were just a few globals:
2670 one set corresponding to the "hot" exception, and one set
2671 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2672 globals; they were simply stored as sys.exc_ZZZ. For backwards
2673 compatibility, they still are!) The problem was that in code like
2674 this:
2675
2676 try:
2677 "something that may fail"
2678 except "some exception":
2679 "do something else first"
2680 "print the exception from sys.exc_ZZZ."
2681
2682 if "do something else first" invoked something that raised and caught
2683 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2684 cause of subtle bugs. I fixed this by changing the semantics as
2685 follows:
2686
2687 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2688 *in that frame*.
2689
2690 - But initially, and as long as no exception is caught in a given
2691 frame, sys.exc_ZZZ will hold the last exception caught in the
2692 previous frame (or the frame before that, etc.).
2693
2694 The first bullet fixed the bug in the above example. The second
2695 bullet was for backwards compatibility: it was (and is) common to
2696 have a function that is called when an exception is caught, and to
2697 have that function access the caught exception via sys.exc_ZZZ.
2698 (Example: traceback.print_exc()).
2699
2700 At the same time I fixed the problem that sys.exc_ZZZ weren't
2701 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2702 but that's really a separate improvement.
2703
2704 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2705 variables to what they were before the current frame was called. The
2706 set_exc_info() function saves them on the frame so that
2707 reset_exc_info() can restore them. The invariant is that
2708 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2709 exception (where "catching" an exception applies only to successful
2710 except clauses); and if the current frame ever caught an exception,
2711 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2712 at the start of the current frame.
2713
2714*/
2715
Guido van Rossuma027efa1997-05-05 20:56:21 +00002716static void
Guido van Rossumac7be682001-01-17 15:42:30 +00002717set_exc_info(PyThreadState *tstate,
2718 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002719{
2720 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002721 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00002722
Guido van Rossuma027efa1997-05-05 20:56:21 +00002723 frame = tstate->frame;
2724 if (frame->f_exc_type == NULL) {
2725 /* This frame didn't catch an exception before */
2726 /* Save previous exception of this thread in this frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002727 if (tstate->exc_type == NULL) {
2728 Py_INCREF(Py_None);
2729 tstate->exc_type = Py_None;
2730 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002731 tmp_type = frame->f_exc_type;
2732 tmp_value = frame->f_exc_value;
2733 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002734 Py_XINCREF(tstate->exc_type);
2735 Py_XINCREF(tstate->exc_value);
2736 Py_XINCREF(tstate->exc_traceback);
2737 frame->f_exc_type = tstate->exc_type;
2738 frame->f_exc_value = tstate->exc_value;
2739 frame->f_exc_traceback = tstate->exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002740 Py_XDECREF(tmp_type);
2741 Py_XDECREF(tmp_value);
2742 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002743 }
2744 /* Set new exception for this thread */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002745 tmp_type = tstate->exc_type;
2746 tmp_value = tstate->exc_value;
2747 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002748 Py_XINCREF(type);
2749 Py_XINCREF(value);
2750 Py_XINCREF(tb);
2751 tstate->exc_type = type;
2752 tstate->exc_value = value;
2753 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002754 Py_XDECREF(tmp_type);
2755 Py_XDECREF(tmp_value);
2756 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002757 /* For b/w compatibility */
2758 PySys_SetObject("exc_type", type);
2759 PySys_SetObject("exc_value", value);
2760 PySys_SetObject("exc_traceback", tb);
2761}
2762
2763static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002764reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002765{
2766 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002767 PyObject *tmp_type, *tmp_value, *tmp_tb;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002768 frame = tstate->frame;
2769 if (frame->f_exc_type != NULL) {
2770 /* This frame caught an exception */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002771 tmp_type = tstate->exc_type;
2772 tmp_value = tstate->exc_value;
2773 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002774 Py_XINCREF(frame->f_exc_type);
2775 Py_XINCREF(frame->f_exc_value);
2776 Py_XINCREF(frame->f_exc_traceback);
2777 tstate->exc_type = frame->f_exc_type;
2778 tstate->exc_value = frame->f_exc_value;
2779 tstate->exc_traceback = frame->f_exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002780 Py_XDECREF(tmp_type);
2781 Py_XDECREF(tmp_value);
2782 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002783 /* For b/w compatibility */
2784 PySys_SetObject("exc_type", frame->f_exc_type);
2785 PySys_SetObject("exc_value", frame->f_exc_value);
2786 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
2787 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002788 tmp_type = frame->f_exc_type;
2789 tmp_value = frame->f_exc_value;
2790 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002791 frame->f_exc_type = NULL;
2792 frame->f_exc_value = NULL;
2793 frame->f_exc_traceback = NULL;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002794 Py_XDECREF(tmp_type);
2795 Py_XDECREF(tmp_value);
2796 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002797}
2798
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002799/* Logic for the raise statement (too complicated for inlining).
2800 This *consumes* a reference count to each of its arguments. */
Guido van Rossum1aa14831997-01-21 05:34:20 +00002801static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002802do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002803{
Guido van Rossumd295f121998-04-09 21:39:57 +00002804 if (type == NULL) {
2805 /* Reraise */
2806 PyThreadState *tstate = PyThreadState_Get();
2807 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
2808 value = tstate->exc_value;
2809 tb = tstate->exc_traceback;
2810 Py_XINCREF(type);
2811 Py_XINCREF(value);
2812 Py_XINCREF(tb);
2813 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002814
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002815 /* We support the following forms of raise:
2816 raise <class>, <classinstance>
2817 raise <class>, <argument tuple>
2818 raise <class>, None
2819 raise <class>, <argument>
2820 raise <classinstance>, None
2821 raise <string>, <object>
2822 raise <string>, None
2823
2824 An omitted second argument is the same as None.
2825
2826 In addition, raise <tuple>, <anything> is the same as
2827 raising the tuple's first item (and it better have one!);
2828 this rule is applied recursively.
2829
2830 Finally, an optional third argument can be supplied, which
2831 gives the traceback to be substituted (useful when
2832 re-raising an exception after examining it). */
2833
2834 /* First, check the traceback argument, replacing None with
2835 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002836 if (tb == Py_None) {
2837 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002838 tb = NULL;
2839 }
2840 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002841 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002842 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002843 goto raise_error;
2844 }
2845
2846 /* Next, replace a missing value with None */
2847 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002848 value = Py_None;
2849 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002850 }
2851
2852 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00002853 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
2854 PyObject *tmp = type;
2855 type = PyTuple_GET_ITEM(type, 0);
2856 Py_INCREF(type);
2857 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002858 }
2859
Tim Petersafb2c802002-04-18 18:06:20 +00002860 if (PyString_CheckExact(type))
2861 /* Raising builtin string is deprecated but still allowed --
2862 * do nothing. Raising an instance of a new-style str
2863 * subclass is right out. */
Neal Norwitz37aa0662003-01-10 15:31:15 +00002864 PyErr_Warn(PyExc_PendingDeprecationWarning,
2865 "raising a string exception is deprecated");
Barry Warsaw4249f541997-08-22 21:26:19 +00002866
2867 else if (PyClass_Check(type))
2868 PyErr_NormalizeException(&type, &value, &tb);
2869
Guido van Rossumb209a111997-04-29 18:18:01 +00002870 else if (PyInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002871 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002872 if (value != Py_None) {
2873 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002874 "instance exception may not have a separate value");
2875 goto raise_error;
2876 }
2877 else {
2878 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00002879 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002880 value = type;
Guido van Rossumb209a111997-04-29 18:18:01 +00002881 type = (PyObject*) ((PyInstanceObject*)type)->in_class;
2882 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002883 }
2884 }
2885 else {
2886 /* Not something you can raise. You get an exception
2887 anyway, just not what you specified :-) */
Jeremy Hylton960d9482001-04-27 02:25:33 +00002888 PyErr_Format(PyExc_TypeError,
Neal Norwitz37aa0662003-01-10 15:31:15 +00002889 "exceptions must be classes, instances, or "
2890 "strings (deprecated), not %s",
2891 type->ob_type->tp_name);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002892 goto raise_error;
2893 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002894 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002895 if (tb == NULL)
2896 return WHY_EXCEPTION;
2897 else
2898 return WHY_RERAISE;
2899 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00002900 Py_XDECREF(value);
2901 Py_XDECREF(type);
2902 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002903 return WHY_EXCEPTION;
2904}
2905
Tim Petersd6d010b2001-06-21 02:49:55 +00002906/* Iterate v argcnt times and store the results on the stack (via decreasing
2907 sp). Return 1 for success, 0 if error. */
2908
Barry Warsawe42b18f1997-08-25 22:13:04 +00002909static int
Tim Petersd6d010b2001-06-21 02:49:55 +00002910unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00002911{
Tim Petersd6d010b2001-06-21 02:49:55 +00002912 int i = 0;
2913 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00002914 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00002915
Tim Petersd6d010b2001-06-21 02:49:55 +00002916 assert(v != NULL);
2917
2918 it = PyObject_GetIter(v);
2919 if (it == NULL)
2920 goto Error;
2921
2922 for (; i < argcnt; i++) {
2923 w = PyIter_Next(it);
2924 if (w == NULL) {
2925 /* Iterator done, via error or exhaustion. */
2926 if (!PyErr_Occurred()) {
2927 PyErr_Format(PyExc_ValueError,
2928 "need more than %d value%s to unpack",
2929 i, i == 1 ? "" : "s");
2930 }
2931 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00002932 }
2933 *--sp = w;
2934 }
Tim Petersd6d010b2001-06-21 02:49:55 +00002935
2936 /* We better have exhausted the iterator now. */
2937 w = PyIter_Next(it);
2938 if (w == NULL) {
2939 if (PyErr_Occurred())
2940 goto Error;
2941 Py_DECREF(it);
2942 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00002943 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00002944 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00002945 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00002946 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00002947Error:
Barry Warsaw91010551997-08-25 22:30:51 +00002948 for (; i > 0; i--, sp++)
2949 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00002950 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00002951 return 0;
2952}
2953
2954
Guido van Rossum96a42c81992-01-12 02:29:51 +00002955#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00002956static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002957prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002958{
Guido van Rossum3f5da241990-12-20 15:06:42 +00002959 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00002960 if (PyObject_Print(v, stdout, 0) != 0)
2961 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00002962 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00002963 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002964}
Guido van Rossum3f5da241990-12-20 15:06:42 +00002965#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002966
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002967static void
Fred Drake5755ce62001-06-27 19:19:46 +00002968call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002969{
Guido van Rossumb209a111997-04-29 18:18:01 +00002970 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002971 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00002972 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00002973 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002974 value = Py_None;
2975 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00002976 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002977 arg = Py_BuildValue("(OOO)", type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002978 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002979 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002980 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002981 }
Fred Drake5755ce62001-06-27 19:19:46 +00002982 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002983 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002984 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00002985 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002986 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00002987 Py_XDECREF(type);
2988 Py_XDECREF(value);
2989 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002990 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002991}
2992
Fred Drake4ec5d562001-10-04 19:26:43 +00002993static void
2994call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
2995 int what)
2996{
2997 PyObject *type, *value, *traceback;
2998 int err;
2999 PyErr_Fetch(&type, &value, &traceback);
3000 err = call_trace(func, obj, frame, what, NULL);
3001 if (err == 0)
3002 PyErr_Restore(type, value, traceback);
3003 else {
3004 Py_XDECREF(type);
3005 Py_XDECREF(value);
3006 Py_XDECREF(traceback);
3007 }
3008}
3009
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003010static int
Fred Drake5755ce62001-06-27 19:19:46 +00003011call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3012 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003013{
Fred Drake5755ce62001-06-27 19:19:46 +00003014 register PyThreadState *tstate = frame->f_tstate;
3015 int result;
3016 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003017 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003018 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003019 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003020 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003021 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3022 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003023 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003024 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003025}
3026
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003027PyObject *
3028_PyEval_CallTracing(PyObject *func, PyObject *args)
3029{
3030 PyFrameObject *frame = PyEval_GetFrame();
3031 PyThreadState *tstate = frame->f_tstate;
3032 int save_tracing = tstate->tracing;
3033 int save_use_tracing = tstate->use_tracing;
3034 PyObject *result;
3035
3036 tstate->tracing = 0;
3037 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3038 || (tstate->c_profilefunc != NULL));
3039 result = PyObject_Call(func, args, NULL);
3040 tstate->tracing = save_tracing;
3041 tstate->use_tracing = save_use_tracing;
3042 return result;
3043}
3044
Michael W. Hudson006c7522002-11-08 13:08:46 +00003045static int
Michael W. Hudson019a78e2002-11-08 12:53:11 +00003046maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003047 PyFrameObject *frame, int *instr_lb, int *instr_ub)
3048{
3049 /* The theory of SET_LINENO-less tracing.
3050
3051 In a nutshell, we use the co_lnotab field of the code object
3052 to tell when execution has moved onto a different line.
3053
3054 As mentioned above, the basic idea is so set things up so
3055 that
3056
3057 *instr_lb <= frame->f_lasti < *instr_ub
3058
3059 is true so long as execution does not change lines.
3060
3061 This is all fairly simple. Digging the information out of
3062 co_lnotab takes some work, but is conceptually clear.
3063
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003064 Somewhat harder to explain is why we don't *always* call the
3065 line trace function when the above test fails.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003066
3067 Consider this code:
3068
3069 1: def f(a):
3070 2: if a:
3071 3: print 1
3072 4: else:
3073 5: print 2
3074
3075 which compiles to this:
3076
3077 2 0 LOAD_FAST 0 (a)
3078 3 JUMP_IF_FALSE 9 (to 15)
3079 6 POP_TOP
3080
3081 3 7 LOAD_CONST 1 (1)
3082 10 PRINT_ITEM
3083 11 PRINT_NEWLINE
3084 12 JUMP_FORWARD 6 (to 21)
3085 >> 15 POP_TOP
3086
3087 5 16 LOAD_CONST 2 (2)
3088 19 PRINT_ITEM
3089 20 PRINT_NEWLINE
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003090 >> 21 LOAD_CONST 0 (None)
3091 24 RETURN_VALUE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003092
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003093 If 'a' is false, execution will jump to instruction at offset
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003094 15 and the co_lnotab will claim that execution has moved to
3095 line 3. This is at best misleading. In this case we could
3096 associate the POP_TOP with line 4, but that doesn't make
3097 sense in all cases (I think).
3098
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003099 What we do is only call the line trace function if the co_lnotab
3100 indicates we have jumped to the *start* of a line, i.e. if the
3101 current instruction offset matches the offset given for the
3102 start of a line by the co_lnotab.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003103
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003104 This also takes care of the situation where 'a' is true.
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003105 Execution will jump from instruction offset 12 to offset 21.
3106 Then the co_lnotab would imply that execution has moved to line
3107 5, which is again misleading.
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003108
3109 Why do we set f_lineno when tracing? Well, consider the code
3110 above when 'a' is true. If stepping through this with 'n' in
3111 pdb, you would stop at line 1 with a "call" type event, then
3112 line events on lines 2 and 3, then a "return" type event -- but
3113 you would be shown line 5 during this event. This is a change
3114 from the behaviour in 2.2 and before, and I've found it
3115 confusing in practice. By setting and using f_lineno when
3116 tracing, one can report a line number different from that
3117 suggested by f_lasti on this one occasion where it's desirable.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003118 */
3119
Michael W. Hudson006c7522002-11-08 13:08:46 +00003120 int result = 0;
3121
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003122 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003123 PyCodeObject* co = frame->f_code;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003124 int size, addr, line;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003125 unsigned char* p;
3126
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003127 size = PyString_GET_SIZE(co->co_lnotab) / 2;
3128 p = (unsigned char*)PyString_AS_STRING(co->co_lnotab);
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003129
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003130 addr = 0;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003131 line = co->co_firstlineno;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003132
3133 /* possible optimization: if f->f_lasti == instr_ub
3134 (likely to be a common case) then we already know
3135 instr_lb -- if we stored the matching value of p
3136 somwhere we could skip the first while loop. */
3137
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003138 /* see comments in compile.c for the description of
3139 co_lnotab. A point to remember: increments to p
3140 should come in pairs -- although we don't care about
3141 the line increments here, treating them as byte
3142 increments gets confusing, to say the least. */
3143
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003144 while (size > 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003145 if (addr + *p > frame->f_lasti)
3146 break;
3147 addr += *p++;
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003148 if (*p) *instr_lb = addr;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003149 line += *p++;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003150 --size;
3151 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003152
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003153 if (addr == frame->f_lasti) {
3154 frame->f_lineno = line;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003155 result = call_trace(func, obj, frame,
3156 PyTrace_LINE, Py_None);
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003157 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003158
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003159 if (size > 0) {
3160 while (--size >= 0) {
3161 addr += *p++;
3162 if (*p++)
3163 break;
3164 }
3165 *instr_ub = addr;
3166 }
3167 else {
3168 *instr_ub = INT_MAX;
3169 }
3170 }
Michael W. Hudson006c7522002-11-08 13:08:46 +00003171
3172 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003173}
3174
Fred Drake5755ce62001-06-27 19:19:46 +00003175void
3176PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003177{
Fred Drake5755ce62001-06-27 19:19:46 +00003178 PyThreadState *tstate = PyThreadState_Get();
3179 PyObject *temp = tstate->c_profileobj;
3180 Py_XINCREF(arg);
3181 tstate->c_profilefunc = NULL;
3182 tstate->c_profileobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003183 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003184 Py_XDECREF(temp);
3185 tstate->c_profilefunc = func;
3186 tstate->c_profileobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003187 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003188}
3189
3190void
3191PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3192{
3193 PyThreadState *tstate = PyThreadState_Get();
3194 PyObject *temp = tstate->c_traceobj;
3195 Py_XINCREF(arg);
3196 tstate->c_tracefunc = NULL;
3197 tstate->c_traceobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003198 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003199 Py_XDECREF(temp);
3200 tstate->c_tracefunc = func;
3201 tstate->c_traceobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003202 tstate->use_tracing = ((func != NULL)
3203 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003204}
3205
Guido van Rossumb209a111997-04-29 18:18:01 +00003206PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003207PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003208{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003209 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003210 if (current_frame == NULL)
Michael W. Hudson019a78e2002-11-08 12:53:11 +00003211 return PyThreadState_Get()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003212 else
3213 return current_frame->f_builtins;
3214}
3215
Guido van Rossumb209a111997-04-29 18:18:01 +00003216PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003217PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003218{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003219 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003220 if (current_frame == NULL)
3221 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003222 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003223 return current_frame->f_locals;
3224}
3225
Guido van Rossumb209a111997-04-29 18:18:01 +00003226PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003227PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003228{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003229 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003230 if (current_frame == NULL)
3231 return NULL;
3232 else
3233 return current_frame->f_globals;
3234}
3235
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003236PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003237PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003238{
Michael W. Hudson019a78e2002-11-08 12:53:11 +00003239 PyThreadState *tstate = PyThreadState_Get();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003240 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003241}
3242
Guido van Rossum6135a871995-01-09 17:53:26 +00003243int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003244PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003245{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003246 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003247 return current_frame == NULL ? 0 : current_frame->f_restricted;
3248}
3249
Guido van Rossumbe270261997-05-22 22:26:18 +00003250int
Tim Peters5ba58662001-07-16 02:29:45 +00003251PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003252{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003253 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003254 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003255
3256 if (current_frame != NULL) {
3257 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003258 const int compilerflags = codeflags & PyCF_MASK;
3259 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003260 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003261 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003262 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003263#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003264 if (codeflags & CO_GENERATOR_ALLOWED) {
3265 result = 1;
3266 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3267 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003268#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003269 }
3270 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003271}
3272
3273int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003274Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003275{
Guido van Rossumb209a111997-04-29 18:18:01 +00003276 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003277 if (f == NULL)
3278 return 0;
3279 if (!PyFile_SoftSpace(f, 0))
3280 return 0;
3281 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003282}
3283
Guido van Rossum3f5da241990-12-20 15:06:42 +00003284
Guido van Rossum681d79a1995-07-18 14:51:37 +00003285/* External interface to call any callable object.
3286 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003287
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003288#undef PyEval_CallObject
3289/* for backward compatibility: export this interface */
3290
Guido van Rossumb209a111997-04-29 18:18:01 +00003291PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003292PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003293{
Guido van Rossumb209a111997-04-29 18:18:01 +00003294 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003295}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003296#define PyEval_CallObject(func,arg) \
3297 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003298
Guido van Rossumb209a111997-04-29 18:18:01 +00003299PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003300PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003301{
Jeremy Hylton52820442001-01-03 23:52:36 +00003302 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003303
3304 if (arg == NULL)
Guido van Rossumb209a111997-04-29 18:18:01 +00003305 arg = PyTuple_New(0);
3306 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003307 PyErr_SetString(PyExc_TypeError,
3308 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003309 return NULL;
3310 }
3311 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003312 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003313
Guido van Rossumb209a111997-04-29 18:18:01 +00003314 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003315 PyErr_SetString(PyExc_TypeError,
3316 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003317 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003318 return NULL;
3319 }
3320
Tim Peters6d6c1a32001-08-02 04:15:00 +00003321 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003322 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003323 return result;
3324}
3325
Tim Peters6d6c1a32001-08-02 04:15:00 +00003326char *
3327PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003328{
3329 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003330 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003331 else if (PyFunction_Check(func))
3332 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3333 else if (PyCFunction_Check(func))
3334 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3335 else if (PyClass_Check(func))
3336 return PyString_AsString(((PyClassObject*)func)->cl_name);
3337 else if (PyInstance_Check(func)) {
3338 return PyString_AsString(
3339 ((PyInstanceObject*)func)->in_class->cl_name);
3340 } else {
3341 return func->ob_type->tp_name;
3342 }
3343}
3344
Tim Peters6d6c1a32001-08-02 04:15:00 +00003345char *
3346PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003347{
3348 if (PyMethod_Check(func))
3349 return "()";
3350 else if (PyFunction_Check(func))
3351 return "()";
3352 else if (PyCFunction_Check(func))
3353 return "()";
3354 else if (PyClass_Check(func))
3355 return " constructor";
3356 else if (PyInstance_Check(func)) {
3357 return " instance";
3358 } else {
3359 return " object";
3360 }
3361}
3362
Jeremy Hylton52820442001-01-03 23:52:36 +00003363#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
3364
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003365static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003366err_args(PyObject *func, int flags, int nargs)
3367{
3368 if (flags & METH_NOARGS)
3369 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003370 "%.200s() takes no arguments (%d given)",
Jeremy Hylton192690e2002-08-16 18:36:11 +00003371 ((PyCFunctionObject *)func)->m_ml->ml_name,
3372 nargs);
3373 else
3374 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003375 "%.200s() takes exactly one argument (%d given)",
Jeremy Hylton192690e2002-08-16 18:36:11 +00003376 ((PyCFunctionObject *)func)->m_ml->ml_name,
3377 nargs);
3378}
3379
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003380static PyObject *
3381call_function(PyObject ***pp_stack, int oparg)
3382{
3383 int na = oparg & 0xff;
3384 int nk = (oparg>>8) & 0xff;
3385 int n = na + 2 * nk;
3386 PyObject **pfunc = (*pp_stack) - n - 1;
3387 PyObject *func = *pfunc;
3388 PyObject *x, *w;
3389
Jeremy Hylton985eba52003-02-05 23:13:00 +00003390 /* Always dispatch PyCFunction first, because these are
3391 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003392 */
3393 if (PyCFunction_Check(func) && nk == 0) {
3394 int flags = PyCFunction_GET_FLAGS(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003395 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003396 if (flags & (METH_NOARGS | METH_O)) {
3397 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3398 PyObject *self = PyCFunction_GET_SELF(func);
3399 if (flags & METH_NOARGS && na == 0)
3400 x = (*meth)(self, NULL);
3401 else if (flags & METH_O && na == 1) {
3402 PyObject *arg = EXT_POP(*pp_stack);
3403 x = (*meth)(self, arg);
3404 Py_DECREF(arg);
3405 }
3406 else {
3407 err_args(func, flags, na);
3408 x = NULL;
3409 }
3410 }
3411 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003412 PyObject *callargs;
3413 callargs = load_args(pp_stack, na);
3414 x = PyCFunction_Call(func, callargs, NULL);
3415 Py_XDECREF(callargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003416 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003417 } else {
3418 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3419 /* optimize access to bound methods */
3420 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003421 PCALL(PCALL_METHOD);
3422 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003423 Py_INCREF(self);
3424 func = PyMethod_GET_FUNCTION(func);
3425 Py_INCREF(func);
3426 Py_DECREF(*pfunc);
3427 *pfunc = self;
3428 na++;
3429 n++;
3430 } else
3431 Py_INCREF(func);
3432 if (PyFunction_Check(func))
3433 x = fast_function(func, pp_stack, n, na, nk);
3434 else
3435 x = do_call(func, pp_stack, na, nk);
3436 Py_DECREF(func);
3437 }
3438
Jeremy Hylton985eba52003-02-05 23:13:00 +00003439 /* What does this do? */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003440 while ((*pp_stack) > pfunc) {
3441 w = EXT_POP(*pp_stack);
3442 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003443 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003444 }
3445 return x;
3446}
3447
Jeremy Hylton192690e2002-08-16 18:36:11 +00003448/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003449 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003450 For the simplest case -- a function that takes only positional
3451 arguments and is called with only positional arguments -- it
3452 inlines the most primitive frame setup code from
3453 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3454 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003455*/
3456
3457static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003458fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003459{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003460 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003461 PyObject *globals = PyFunction_GET_GLOBALS(func);
3462 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3463 PyObject **d = NULL;
3464 int nd = 0;
3465
Jeremy Hylton985eba52003-02-05 23:13:00 +00003466 PCALL(PCALL_FUNCTION);
3467 PCALL(PCALL_FAST_FUNCTION);
3468 if (argdefs == NULL && co->co_argcount == n &&
3469 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3470 PyFrameObject *f;
3471 PyObject *retval = NULL;
3472 PyThreadState *tstate = PyThreadState_GET();
3473 PyObject **fastlocals, **stack;
3474 int i;
3475
3476 PCALL(PCALL_FASTER_FUNCTION);
3477 assert(globals != NULL);
3478 /* XXX Perhaps we should create a specialized
3479 PyFrame_New() that doesn't take locals, but does
3480 take builtins without sanity checking them.
3481 */
3482 f = PyFrame_New(tstate, co, globals, NULL);
3483 if (f == NULL)
3484 return NULL;
3485
3486 fastlocals = f->f_localsplus;
3487 stack = (*pp_stack) - n;
3488
3489 for (i = 0; i < n; i++) {
3490 Py_INCREF(*stack);
3491 fastlocals[i] = *stack++;
3492 }
3493 retval = eval_frame(f);
3494 assert(tstate != NULL);
3495 ++tstate->recursion_depth;
3496 Py_DECREF(f);
3497 --tstate->recursion_depth;
3498 return retval;
3499 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003500 if (argdefs != NULL) {
3501 d = &PyTuple_GET_ITEM(argdefs, 0);
3502 nd = ((PyTupleObject *)argdefs)->ob_size;
3503 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003504 return PyEval_EvalCodeEx(co, globals,
3505 (PyObject *)NULL, (*pp_stack)-n, na,
3506 (*pp_stack)-2*nk, nk, d, nd,
3507 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003508}
3509
3510static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003511update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3512 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003513{
3514 PyObject *kwdict = NULL;
3515 if (orig_kwdict == NULL)
3516 kwdict = PyDict_New();
3517 else {
3518 kwdict = PyDict_Copy(orig_kwdict);
3519 Py_DECREF(orig_kwdict);
3520 }
3521 if (kwdict == NULL)
3522 return NULL;
3523 while (--nk >= 0) {
3524 int err;
3525 PyObject *value = EXT_POP(*pp_stack);
3526 PyObject *key = EXT_POP(*pp_stack);
3527 if (PyDict_GetItem(kwdict, key) != NULL) {
Guido van Rossumac7be682001-01-17 15:42:30 +00003528 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +00003529 "%.200s%s got multiple values "
Jeremy Hylton512a2372001-04-11 13:52:29 +00003530 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003531 PyEval_GetFuncName(func),
3532 PyEval_GetFuncDesc(func),
Jeremy Hylton512a2372001-04-11 13:52:29 +00003533 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003534 Py_DECREF(key);
3535 Py_DECREF(value);
3536 Py_DECREF(kwdict);
3537 return NULL;
3538 }
3539 err = PyDict_SetItem(kwdict, key, value);
3540 Py_DECREF(key);
3541 Py_DECREF(value);
3542 if (err) {
3543 Py_DECREF(kwdict);
3544 return NULL;
3545 }
3546 }
3547 return kwdict;
3548}
3549
3550static PyObject *
3551update_star_args(int nstack, int nstar, PyObject *stararg,
3552 PyObject ***pp_stack)
3553{
3554 PyObject *callargs, *w;
3555
3556 callargs = PyTuple_New(nstack + nstar);
3557 if (callargs == NULL) {
3558 return NULL;
3559 }
3560 if (nstar) {
3561 int i;
3562 for (i = 0; i < nstar; i++) {
3563 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3564 Py_INCREF(a);
3565 PyTuple_SET_ITEM(callargs, nstack + i, a);
3566 }
3567 }
3568 while (--nstack >= 0) {
3569 w = EXT_POP(*pp_stack);
3570 PyTuple_SET_ITEM(callargs, nstack, w);
3571 }
3572 return callargs;
3573}
3574
3575static PyObject *
3576load_args(PyObject ***pp_stack, int na)
3577{
3578 PyObject *args = PyTuple_New(na);
3579 PyObject *w;
3580
3581 if (args == NULL)
3582 return NULL;
3583 while (--na >= 0) {
3584 w = EXT_POP(*pp_stack);
3585 PyTuple_SET_ITEM(args, na, w);
3586 }
3587 return args;
3588}
3589
3590static PyObject *
3591do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3592{
3593 PyObject *callargs = NULL;
3594 PyObject *kwdict = NULL;
3595 PyObject *result = NULL;
3596
3597 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003598 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003599 if (kwdict == NULL)
3600 goto call_fail;
3601 }
3602 callargs = load_args(pp_stack, na);
3603 if (callargs == NULL)
3604 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003605#ifdef CALL_PROFILE
3606 /* At this point, we have to look at the type of func to
3607 update the call stats properly. Do it here so as to avoid
3608 exposing the call stats machinery outside ceval.c
3609 */
3610 if (PyFunction_Check(func))
3611 PCALL(PCALL_FUNCTION);
3612 else if (PyMethod_Check(func))
3613 PCALL(PCALL_METHOD);
3614 else if (PyType_Check(func))
3615 PCALL(PCALL_TYPE);
3616 else
3617 PCALL(PCALL_OTHER);
3618#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003619 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003620 call_fail:
3621 Py_XDECREF(callargs);
3622 Py_XDECREF(kwdict);
3623 return result;
3624}
3625
3626static PyObject *
3627ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3628{
3629 int nstar = 0;
3630 PyObject *callargs = NULL;
3631 PyObject *stararg = NULL;
3632 PyObject *kwdict = NULL;
3633 PyObject *result = NULL;
3634
3635 if (flags & CALL_FLAG_KW) {
3636 kwdict = EXT_POP(*pp_stack);
3637 if (!(kwdict && PyDict_Check(kwdict))) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003638 PyErr_Format(PyExc_TypeError,
Jeremy Hylton512a2372001-04-11 13:52:29 +00003639 "%s%s argument after ** "
3640 "must be a dictionary",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003641 PyEval_GetFuncName(func),
3642 PyEval_GetFuncDesc(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003643 goto ext_call_fail;
3644 }
3645 }
3646 if (flags & CALL_FLAG_VAR) {
3647 stararg = EXT_POP(*pp_stack);
3648 if (!PyTuple_Check(stararg)) {
3649 PyObject *t = NULL;
3650 t = PySequence_Tuple(stararg);
3651 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003652 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3653 PyErr_Format(PyExc_TypeError,
3654 "%s%s argument after * "
3655 "must be a sequence",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003656 PyEval_GetFuncName(func),
3657 PyEval_GetFuncDesc(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003658 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003659 goto ext_call_fail;
3660 }
3661 Py_DECREF(stararg);
3662 stararg = t;
3663 }
3664 nstar = PyTuple_GET_SIZE(stararg);
3665 }
3666 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003667 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003668 if (kwdict == NULL)
3669 goto ext_call_fail;
3670 }
3671 callargs = update_star_args(na, nstar, stararg, pp_stack);
3672 if (callargs == NULL)
3673 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003674#ifdef CALL_PROFILE
3675 /* At this point, we have to look at the type of func to
3676 update the call stats properly. Do it here so as to avoid
3677 exposing the call stats machinery outside ceval.c
3678 */
3679 if (PyFunction_Check(func))
3680 PCALL(PCALL_FUNCTION);
3681 else if (PyMethod_Check(func))
3682 PCALL(PCALL_METHOD);
3683 else if (PyType_Check(func))
3684 PCALL(PCALL_TYPE);
3685 else
3686 PCALL(PCALL_OTHER);
3687#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003688 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003689 ext_call_fail:
3690 Py_XDECREF(callargs);
3691 Py_XDECREF(kwdict);
3692 Py_XDECREF(stararg);
3693 return result;
3694}
3695
Guido van Rossum3b9c6671996-07-30 18:40:29 +00003696#define SLICE_ERROR_MSG \
3697 "standard sequence type does not support step size other than one"
3698
Tim Peterscb479e72001-12-16 19:11:44 +00003699/* Extract a slice index from a PyInt or PyLong, and store in *pi.
3700 Silently reduce values larger than INT_MAX to INT_MAX, and silently
3701 boost values less than -INT_MAX to 0. Return 0 on error, 1 on success.
3702*/
Tim Petersb5196382001-12-16 19:44:20 +00003703/* Note: If v is NULL, return success without storing into *pi. This
3704 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3705 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00003706*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00003707int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003708_PyEval_SliceIndex(PyObject *v, int *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003709{
Tim Petersb5196382001-12-16 19:44:20 +00003710 if (v != NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003711 long x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003712 if (PyInt_Check(v)) {
3713 x = PyInt_AsLong(v);
3714 } else if (PyLong_Check(v)) {
3715 x = PyLong_AsLong(v);
3716 if (x==-1 && PyErr_Occurred()) {
3717 PyObject *long_zero;
Guido van Rossumac7be682001-01-17 15:42:30 +00003718 int cmp;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003719
Guido van Rossumac7be682001-01-17 15:42:30 +00003720 if (!PyErr_ExceptionMatches(
3721 PyExc_OverflowError)) {
3722 /* It's not an overflow error, so just
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003723 signal an error */
Guido van Rossum20c6add2000-05-08 14:06:50 +00003724 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003725 }
3726
Guido van Rossumac7be682001-01-17 15:42:30 +00003727 /* Clear the OverflowError */
3728 PyErr_Clear();
3729
3730 /* It's an overflow error, so we need to
3731 check the sign of the long integer,
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003732 set the value to INT_MAX or -INT_MAX,
3733 and clear the error. */
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003734
3735 /* Create a long integer with a value of 0 */
Guido van Rossumac7be682001-01-17 15:42:30 +00003736 long_zero = PyLong_FromLong(0L);
Tim Peterscb479e72001-12-16 19:11:44 +00003737 if (long_zero == NULL)
3738 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003739
3740 /* Check sign */
Guido van Rossumac7be682001-01-17 15:42:30 +00003741 cmp = PyObject_RichCompareBool(v, long_zero,
3742 Py_GT);
3743 Py_DECREF(long_zero);
3744 if (cmp < 0)
3745 return 0;
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003746 else if (cmp)
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003747 x = INT_MAX;
3748 else
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003749 x = -INT_MAX;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003750 }
3751 } else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003752 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003753 "slice indices must be integers");
Guido van Rossum20c6add2000-05-08 14:06:50 +00003754 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003755 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003756 /* Truncate -- very long indices are truncated anyway */
3757 if (x > INT_MAX)
3758 x = INT_MAX;
3759 else if (x < -INT_MAX)
Michael W. Hudsoncbd6fb92002-11-06 15:17:32 +00003760 x = -INT_MAX;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003761 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003762 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00003763 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003764}
3765
Guido van Rossum50d756e2001-08-18 17:43:36 +00003766#undef ISINT
3767#define ISINT(x) ((x) == NULL || PyInt_Check(x) || PyLong_Check(x))
3768
Guido van Rossumb209a111997-04-29 18:18:01 +00003769static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003770apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003771{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003772 PyTypeObject *tp = u->ob_type;
3773 PySequenceMethods *sq = tp->tp_as_sequence;
3774
3775 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3776 int ilow = 0, ihigh = INT_MAX;
3777 if (!_PyEval_SliceIndex(v, &ilow))
3778 return NULL;
3779 if (!_PyEval_SliceIndex(w, &ihigh))
3780 return NULL;
3781 return PySequence_GetSlice(u, ilow, ihigh);
3782 }
3783 else {
3784 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00003785 if (slice != NULL) {
3786 PyObject *res = PyObject_GetItem(u, slice);
3787 Py_DECREF(slice);
3788 return res;
3789 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00003790 else
3791 return NULL;
3792 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003793}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003794
3795static int
Guido van Rossumac7be682001-01-17 15:42:30 +00003796assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
3797 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003798{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003799 PyTypeObject *tp = u->ob_type;
3800 PySequenceMethods *sq = tp->tp_as_sequence;
3801
3802 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3803 int ilow = 0, ihigh = INT_MAX;
3804 if (!_PyEval_SliceIndex(v, &ilow))
3805 return -1;
3806 if (!_PyEval_SliceIndex(w, &ihigh))
3807 return -1;
3808 if (x == NULL)
3809 return PySequence_DelSlice(u, ilow, ihigh);
3810 else
3811 return PySequence_SetSlice(u, ilow, ihigh, x);
3812 }
3813 else {
3814 PyObject *slice = PySlice_New(v, w, NULL);
3815 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00003816 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003817 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00003818 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00003819 else
Guido van Rossum354797c2001-12-03 19:45:06 +00003820 res = PyObject_DelItem(u, slice);
3821 Py_DECREF(slice);
3822 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003823 }
3824 else
3825 return -1;
3826 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003827}
3828
Guido van Rossumb209a111997-04-29 18:18:01 +00003829static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003830cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003831{
Guido van Rossumac7be682001-01-17 15:42:30 +00003832 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003833 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00003834 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003835 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003836 break;
3837 case PyCmp_IS_NOT:
3838 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003839 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003840 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003841 res = PySequence_Contains(w, v);
3842 if (res < 0)
3843 return NULL;
3844 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003845 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00003846 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003847 if (res < 0)
3848 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003849 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003850 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003851 case PyCmp_EXC_MATCH:
Barry Warsaw4249f541997-08-22 21:26:19 +00003852 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003853 break;
3854 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00003855 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003856 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003857 v = res ? Py_True : Py_False;
3858 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003859 return v;
3860}
3861
Thomas Wouters52152252000-08-17 22:55:00 +00003862static PyObject *
3863import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003864{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003865 PyObject *x;
3866
3867 x = PyObject_GetAttr(v, name);
3868 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00003869 PyErr_Format(PyExc_ImportError,
3870 "cannot import name %.230s",
3871 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003872 }
Thomas Wouters52152252000-08-17 22:55:00 +00003873 return x;
3874}
Guido van Rossumac7be682001-01-17 15:42:30 +00003875
Thomas Wouters52152252000-08-17 22:55:00 +00003876static int
3877import_all_from(PyObject *locals, PyObject *v)
3878{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003879 PyObject *all = PyObject_GetAttrString(v, "__all__");
3880 PyObject *dict, *name, *value;
3881 int skip_leading_underscores = 0;
3882 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00003883
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003884 if (all == NULL) {
3885 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3886 return -1; /* Unexpected error */
3887 PyErr_Clear();
3888 dict = PyObject_GetAttrString(v, "__dict__");
3889 if (dict == NULL) {
3890 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3891 return -1;
3892 PyErr_SetString(PyExc_ImportError,
3893 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00003894 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003895 }
3896 all = PyMapping_Keys(dict);
3897 Py_DECREF(dict);
3898 if (all == NULL)
3899 return -1;
3900 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00003901 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003902
3903 for (pos = 0, err = 0; ; pos++) {
3904 name = PySequence_GetItem(all, pos);
3905 if (name == NULL) {
3906 if (!PyErr_ExceptionMatches(PyExc_IndexError))
3907 err = -1;
3908 else
3909 PyErr_Clear();
3910 break;
3911 }
3912 if (skip_leading_underscores &&
3913 PyString_Check(name) &&
3914 PyString_AS_STRING(name)[0] == '_')
3915 {
3916 Py_DECREF(name);
3917 continue;
3918 }
3919 value = PyObject_GetAttr(v, name);
3920 if (value == NULL)
3921 err = -1;
3922 else
3923 err = PyDict_SetItem(locals, name, value);
3924 Py_DECREF(name);
3925 Py_XDECREF(value);
3926 if (err != 0)
3927 break;
3928 }
3929 Py_DECREF(all);
3930 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00003931}
3932
Guido van Rossumb209a111997-04-29 18:18:01 +00003933static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003934build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003935{
Guido van Rossum7851eea2001-09-12 19:19:18 +00003936 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003937
3938 if (PyDict_Check(methods))
3939 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00003940 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00003941 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00003942 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
3943 base = PyTuple_GET_ITEM(bases, 0);
3944 metaclass = PyObject_GetAttrString(base, "__class__");
3945 if (metaclass == NULL) {
3946 PyErr_Clear();
3947 metaclass = (PyObject *)base->ob_type;
3948 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00003949 }
3950 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00003951 else {
3952 PyObject *g = PyEval_GetGlobals();
3953 if (g != NULL && PyDict_Check(g))
3954 metaclass = PyDict_GetItemString(g, "__metaclass__");
3955 if (metaclass == NULL)
3956 metaclass = (PyObject *) &PyClass_Type;
3957 Py_INCREF(metaclass);
3958 }
3959 result = PyObject_CallFunction(metaclass, "OOO", name, bases, methods);
3960 Py_DECREF(metaclass);
3961 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00003962}
3963
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003964static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003965exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
3966 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003967{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003968 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00003969 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003970 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003971
Guido van Rossumb209a111997-04-29 18:18:01 +00003972 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
3973 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003974 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00003975 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003976 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00003977 locals = PyTuple_GetItem(prog, 2);
3978 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003979 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003980 if (globals == Py_None) {
3981 globals = PyEval_GetGlobals();
3982 if (locals == Py_None) {
3983 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00003984 plain = 1;
3985 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003986 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003987 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003988 locals = globals;
Guido van Rossumb209a111997-04-29 18:18:01 +00003989 if (!PyString_Check(prog) &&
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00003990 !PyUnicode_Check(prog) &&
Guido van Rossumb209a111997-04-29 18:18:01 +00003991 !PyCode_Check(prog) &&
3992 !PyFile_Check(prog)) {
3993 PyErr_SetString(PyExc_TypeError,
Guido van Rossumac7be682001-01-17 15:42:30 +00003994 "exec: arg 1 must be a string, file, or code object");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003995 return -1;
3996 }
Fred Drake661ea262000-10-24 19:57:45 +00003997 if (!PyDict_Check(globals)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003998 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003999 "exec: arg 2 must be a dictionary or None");
4000 return -1;
4001 }
4002 if (!PyDict_Check(locals)) {
4003 PyErr_SetString(PyExc_TypeError,
4004 "exec: arg 3 must be a dictionary or None");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004005 return -1;
4006 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004007 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00004008 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00004009 if (PyCode_Check(prog)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +00004010 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4011 PyErr_SetString(PyExc_TypeError,
4012 "code object passed to exec may not contain free variables");
4013 return -1;
4014 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004015 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004016 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004017 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004018 FILE *fp = PyFile_AsFile(prog);
4019 char *name = PyString_AsString(PyFile_Name(prog));
Tim Peters5ba58662001-07-16 02:29:45 +00004020 PyCompilerFlags cf;
4021 cf.cf_flags = 0;
4022 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004023 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
4024 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004025 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004026 v = PyRun_File(fp, name, Py_file_input, globals,
4027 locals);
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004028 }
4029 else {
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004030 PyObject *tmp = NULL;
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004031 char *str;
Tim Peters5ba58662001-07-16 02:29:45 +00004032 PyCompilerFlags cf;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004033 cf.cf_flags = 0;
4034#ifdef Py_USING_UNICODE
4035 if (PyUnicode_Check(prog)) {
4036 tmp = PyUnicode_AsUTF8String(prog);
4037 if (tmp == NULL)
4038 return -1;
4039 prog = tmp;
4040 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4041 }
4042#endif
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004043 if (PyString_AsStringAndSize(prog, &str, NULL))
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004044 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004045 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004046 v = PyRun_StringFlags(str, Py_file_input, globals,
4047 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004048 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004049 v = PyRun_String(str, Py_file_input, globals, locals);
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004050 Py_XDECREF(tmp);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004051 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004052 if (plain)
4053 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004054 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004055 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00004056 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004057 return 0;
4058}
Guido van Rossum24c13741995-02-14 09:42:43 +00004059
Guido van Rossumac7be682001-01-17 15:42:30 +00004060static void
Paul Prescode68140d2000-08-30 20:25:01 +00004061format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4062{
4063 char *obj_str;
4064
4065 if (!obj)
4066 return;
4067
4068 obj_str = PyString_AsString(obj);
4069 if (!obj_str)
4070 return;
4071
4072 PyErr_Format(exc, format_str, obj_str);
4073}
Guido van Rossum950361c1997-01-24 13:49:28 +00004074
4075#ifdef DYNAMIC_EXECUTION_PROFILE
4076
Skip Montanarof118cb12001-10-15 20:51:38 +00004077static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004078getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004079{
4080 int i;
4081 PyObject *l = PyList_New(256);
4082 if (l == NULL) return NULL;
4083 for (i = 0; i < 256; i++) {
4084 PyObject *x = PyInt_FromLong(a[i]);
4085 if (x == NULL) {
4086 Py_DECREF(l);
4087 return NULL;
4088 }
4089 PyList_SetItem(l, i, x);
4090 }
4091 for (i = 0; i < 256; i++)
4092 a[i] = 0;
4093 return l;
4094}
4095
4096PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004097_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004098{
4099#ifndef DXPAIRS
4100 return getarray(dxp);
4101#else
4102 int i;
4103 PyObject *l = PyList_New(257);
4104 if (l == NULL) return NULL;
4105 for (i = 0; i < 257; i++) {
4106 PyObject *x = getarray(dxpairs[i]);
4107 if (x == NULL) {
4108 Py_DECREF(l);
4109 return NULL;
4110 }
4111 PyList_SetItem(l, i, x);
4112 }
4113 return l;
4114#endif
4115}
4116
4117#endif