blob: 8c246f6a07b72e1368aadbfb7ed680b26cc6ad9b [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
605/* Stack manipulation macros */
606
607#define STACK_LEVEL() (stack_pointer - f->f_valuestack)
608#define EMPTY() (STACK_LEVEL() == 0)
609#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000610#define SECOND() (stack_pointer[-2])
611#define THIRD() (stack_pointer[-3])
612#define FOURTH() (stack_pointer[-4])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000613#define SET_TOP(v) (stack_pointer[-1] = (v))
614#define SET_SECOND(v) (stack_pointer[-2] = (v))
615#define SET_THIRD(v) (stack_pointer[-3] = (v))
616#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000617#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000618#define BASIC_PUSH(v) (*stack_pointer++ = (v))
619#define BASIC_POP() (*--stack_pointer)
620
Guido van Rossum96a42c81992-01-12 02:29:51 +0000621#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000622#define PUSH(v) { (void)(BASIC_PUSH(v), \
623 lltrace && prtrace(TOP(), "push")); \
624 assert(STACK_LEVEL() <= f->f_stacksize); }
Fred Drakede26cfc2001-10-13 06:11:28 +0000625#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000626#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
627 lltrace && prtrace(TOP(), "stackadj")); \
628 assert(STACK_LEVEL() <= f->f_stacksize); }
Guido van Rossum374a9221991-04-04 10:40:29 +0000629#else
630#define PUSH(v) BASIC_PUSH(v)
631#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000632#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000633#endif
634
Guido van Rossum681d79a1995-07-18 14:51:37 +0000635/* Local variable macros */
636
637#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000638
639/* The SETLOCAL() macro must not DECREF the local variable in-place and
640 then store the new value; it must copy the old value to a temporary
641 value, then store the new value, and then DECREF the temporary value.
642 This is because it is possible that during the DECREF the frame is
643 accessed by other code (e.g. a __del__ method or gc.collect()) and the
644 variable would be pointing to already-freed memory. */
645#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
646 GETLOCAL(i) = value; \
647 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000648
Guido van Rossuma027efa1997-05-05 20:56:21 +0000649/* Start of code */
650
Tim Peters5ca576e2001-06-18 22:08:13 +0000651 if (f == NULL)
652 return NULL;
653
Guido van Rossum8861b741996-07-30 16:49:37 +0000654#ifdef USE_STACKCHECK
Guido van Rossuma027efa1997-05-05 20:56:21 +0000655 if (tstate->recursion_depth%10 == 0 && PyOS_CheckStack()) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000656 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
Guido van Rossum8861b741996-07-30 16:49:37 +0000657 return NULL;
658 }
659#endif
660
Tim Peters5ca576e2001-06-18 22:08:13 +0000661 /* push frame */
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000662 if (++tstate->recursion_depth > recursion_limit) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000663 --tstate->recursion_depth;
664 PyErr_SetString(PyExc_RuntimeError,
Fred Drake661ea262000-10-24 19:57:45 +0000665 "maximum recursion depth exceeded");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000666 tstate->frame = f->f_back;
Guido van Rossum8861b741996-07-30 16:49:37 +0000667 return NULL;
668 }
669
Tim Peters5ca576e2001-06-18 22:08:13 +0000670 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000671
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000672 if (tstate->use_tracing) {
673 if (tstate->c_tracefunc != NULL) {
674 /* tstate->c_tracefunc, if defined, is a
675 function that will be called on *every* entry
676 to a code block. Its return value, if not
677 None, is a function that will be called at
678 the start of each executed line of code.
679 (Actually, the function must return itself
680 in order to continue tracing.) The trace
681 functions are called with three arguments:
682 a pointer to the current frame, a string
683 indicating why the function is called, and
684 an argument which depends on the situation.
685 The global trace function is also called
686 whenever an exception is detected. */
687 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
688 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000689 /* Trace function raised an error */
Michael W. Hudsonfb4d6ec2002-10-02 13:13:45 +0000690 --tstate->recursion_depth;
691 tstate->frame = f->f_back;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000692 return NULL;
693 }
694 }
695 if (tstate->c_profilefunc != NULL) {
696 /* Similar for c_profilefunc, except it needn't
697 return itself and isn't called for "line" events */
698 if (call_trace(tstate->c_profilefunc,
699 tstate->c_profileobj,
700 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000701 /* Profile function raised an error */
Michael W. Hudsonfb4d6ec2002-10-02 13:13:45 +0000702 --tstate->recursion_depth;
703 tstate->frame = f->f_back;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000704 return NULL;
705 }
706 }
707 }
708
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000709 co = f->f_code;
710 names = co->co_names;
711 consts = co->co_consts;
712 fastlocals = f->f_localsplus;
713 freevars = f->f_localsplus + f->f_nlocals;
714 _PyCode_GETCODEPTR(co, &first_instr);
715 /* An explanation is in order for the next line.
716
717 f->f_lasti now refers to the index of the last instruction
718 executed. You might think this was obvious from the name, but
719 this wasn't always true before 2.3! PyFrame_New now sets
720 f->f_lasti to -1 (i.e. the index *before* the first instruction)
721 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
722 does work. Promise. */
723 next_instr = first_instr + f->f_lasti + 1;
724 stack_pointer = f->f_stacktop;
725 assert(stack_pointer != NULL);
726 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
727
Tim Peters5ca576e2001-06-18 22:08:13 +0000728#ifdef LLTRACE
729 lltrace = PyDict_GetItemString(f->f_globals,"__lltrace__") != NULL;
730#endif
731#if defined(Py_DEBUG) || defined(LLTRACE)
732 filename = PyString_AsString(co->co_filename);
733#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000734
Guido van Rossum374a9221991-04-04 10:40:29 +0000735 why = WHY_NOT;
736 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +0000737 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +0000738 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +0000739
Guido van Rossum374a9221991-04-04 10:40:29 +0000740 for (;;) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000741 assert(stack_pointer >= f->f_valuestack); /* else underflow */
742 assert(STACK_LEVEL() <= f->f_stacksize); /* else overflow */
743
Guido van Rossuma027efa1997-05-05 20:56:21 +0000744 /* Do periodic things. Doing this every time through
745 the loop would add too much overhead, so we do it
746 only every Nth instruction. We also do it if
747 ``things_to_do'' is set, i.e. when an asynchronous
748 event needs attention (e.g. a signal handler or
749 async I/O handler); see Py_AddPendingCall() and
750 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000751
Skip Montanarod581d772002-09-03 20:10:45 +0000752 if (--_Py_Ticker < 0) {
753 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000754 tstate->tick_counter++;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000755 if (things_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +0000756 if (Py_MakePendingCalls() < 0) {
757 why = WHY_EXCEPTION;
758 goto on_error;
759 }
760 }
Guido van Rossumdf0d00e1997-05-20 15:57:49 +0000761#if !defined(HAVE_SIGNAL_H) || defined(macintosh)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000762 /* If we have true signals, the signal handler
763 will call Py_AddPendingCall() so we don't
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000764 have to call PyErr_CheckSignals(). On the
765 Mac and DOS, alas, we have to call it. */
Guido van Rossumb209a111997-04-29 18:18:01 +0000766 if (PyErr_CheckSignals()) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000767 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +0000768 goto on_error;
769 }
Guido van Rossum70d44781997-01-21 06:15:24 +0000770#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000771
Guido van Rossume59214e1994-08-30 08:01:59 +0000772#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000773 if (interpreter_lock) {
774 /* Give another thread a chance */
775
Guido van Rossum25ce5661997-08-02 03:10:38 +0000776 if (PyThreadState_Swap(NULL) != tstate)
777 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000778 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000779
780 /* Other threads may run now */
781
Guido van Rossum65d5b571998-12-21 19:32:43 +0000782 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000783 if (PyThreadState_Swap(tstate) != NULL)
784 Py_FatalError("ceval: orphan tstate");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000785 }
786#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000787 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000788
Neil Schemenauer63543862002-02-17 19:10:14 +0000789 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +0000790 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +0000791
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000792 /* line-by-line tracing support */
793
794 if (tstate->c_tracefunc != NULL && !tstate->tracing) {
795 /* see maybe_call_line_trace
796 for expository comments */
797 f->f_stacktop = stack_pointer;
Michael W. Hudson006c7522002-11-08 13:08:46 +0000798
799 if (maybe_call_line_trace(tstate->c_tracefunc,
800 tstate->c_traceobj,
801 f, &instr_lb, &instr_ub)) {
802 /* trace function raised an exception */
803 why = WHY_EXCEPTION;
804 goto on_error;
805 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000806 /* Reload possibly changed frame fields */
807 JUMPTO(f->f_lasti);
808 stack_pointer = f->f_stacktop;
809 assert(stack_pointer != NULL);
810 f->f_stacktop = NULL;
811 }
812
813 /* Extract opcode and argument */
814
Guido van Rossum374a9221991-04-04 10:40:29 +0000815 opcode = NEXTOP();
816 if (HAS_ARG(opcode))
817 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +0000818 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +0000819#ifdef DYNAMIC_EXECUTION_PROFILE
820#ifdef DXPAIRS
821 dxpairs[lastopcode][opcode]++;
822 lastopcode = opcode;
823#endif
824 dxp[opcode]++;
825#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000826
Guido van Rossum96a42c81992-01-12 02:29:51 +0000827#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +0000828 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +0000829
Guido van Rossum96a42c81992-01-12 02:29:51 +0000830 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000831 if (HAS_ARG(opcode)) {
832 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000833 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +0000834 }
835 else {
836 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000837 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +0000838 }
839 }
840#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000841
Guido van Rossum374a9221991-04-04 10:40:29 +0000842 /* Main switch on opcode */
Jeremy Hylton52820442001-01-03 23:52:36 +0000843
Guido van Rossum374a9221991-04-04 10:40:29 +0000844 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +0000845
Guido van Rossum374a9221991-04-04 10:40:29 +0000846 /* BEWARE!
847 It is essential that any operation that fails sets either
848 x to NULL, err to nonzero, or why to anything but WHY_NOT,
849 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000850
Guido van Rossum374a9221991-04-04 10:40:29 +0000851 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000852
Neil Schemenauer63543862002-02-17 19:10:14 +0000853 case LOAD_FAST:
854 x = GETLOCAL(oparg);
855 if (x != NULL) {
856 Py_INCREF(x);
857 PUSH(x);
858 goto fast_next_opcode;
859 }
860 format_exc_check_arg(PyExc_UnboundLocalError,
861 UNBOUNDLOCAL_ERROR_MSG,
862 PyTuple_GetItem(co->co_varnames, oparg));
863 break;
864
865 case LOAD_CONST:
Skip Montanaro04d80f82002-08-04 21:03:35 +0000866 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +0000867 Py_INCREF(x);
868 PUSH(x);
869 goto fast_next_opcode;
870
871 case STORE_FAST:
872 v = POP();
873 SETLOCAL(oparg, v);
874 goto fast_next_opcode;
875
Guido van Rossum374a9221991-04-04 10:40:29 +0000876 case POP_TOP:
877 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000878 Py_DECREF(v);
Neil Schemenauer63543862002-02-17 19:10:14 +0000879 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000880
Guido van Rossum374a9221991-04-04 10:40:29 +0000881 case ROT_TWO:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000882 v = TOP();
883 w = SECOND();
884 SET_TOP(w);
885 SET_SECOND(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000886 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +0000887
Guido van Rossum374a9221991-04-04 10:40:29 +0000888 case ROT_THREE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000889 v = TOP();
890 w = SECOND();
891 x = THIRD();
892 SET_TOP(w);
893 SET_SECOND(x);
894 SET_THIRD(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000895 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +0000896
Thomas Wouters434d0822000-08-24 20:11:32 +0000897 case ROT_FOUR:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000898 u = TOP();
899 v = SECOND();
900 w = THIRD();
901 x = FOURTH();
902 SET_TOP(v);
903 SET_SECOND(w);
904 SET_THIRD(x);
905 SET_FOURTH(u);
Thomas Wouters434d0822000-08-24 20:11:32 +0000906 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +0000907
Guido van Rossum374a9221991-04-04 10:40:29 +0000908 case DUP_TOP:
909 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000910 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000911 PUSH(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000912 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +0000913
Thomas Wouters434d0822000-08-24 20:11:32 +0000914 case DUP_TOPX:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000915 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000916 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000917 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000918 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000919 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000920 STACKADJ(2);
921 SET_TOP(x);
922 SET_SECOND(w);
Tim Peters35ba6892000-10-11 07:04:49 +0000923 continue;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000924 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000925 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000926 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000927 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000928 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000929 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +0000930 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000931 STACKADJ(3);
932 SET_TOP(x);
933 SET_SECOND(w);
934 SET_THIRD(v);
Tim Peters35ba6892000-10-11 07:04:49 +0000935 continue;
Thomas Wouters434d0822000-08-24 20:11:32 +0000936 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000937 Py_FatalError("invalid argument to DUP_TOPX"
938 " (bytecode corruption?)");
Tim Peters35ba6892000-10-11 07:04:49 +0000939 break;
Thomas Wouters434d0822000-08-24 20:11:32 +0000940
Guido van Rossum374a9221991-04-04 10:40:29 +0000941 case UNARY_POSITIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000942 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000943 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000944 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000945 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000946 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000947 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000948
Guido van Rossum374a9221991-04-04 10:40:29 +0000949 case UNARY_NEGATIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000950 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000951 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000952 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000953 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000954 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000955 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000956
Guido van Rossum374a9221991-04-04 10:40:29 +0000957 case UNARY_NOT:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000958 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000959 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000960 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +0000961 if (err == 0) {
962 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000963 SET_TOP(Py_True);
Guido van Rossumfc490731997-05-06 15:06:49 +0000964 continue;
965 }
966 else if (err > 0) {
967 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000968 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +0000969 err = 0;
970 continue;
971 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +0000972 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +0000973 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000974
Guido van Rossum374a9221991-04-04 10:40:29 +0000975 case UNARY_CONVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000976 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000977 x = PyObject_Repr(v);
978 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000979 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000980 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000981 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000982
Guido van Rossum7928cd71991-10-24 14:59:31 +0000983 case UNARY_INVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000984 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000985 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000986 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000987 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000988 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000989 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000990
Guido van Rossum50564e81996-01-12 01:13:16 +0000991 case BINARY_POWER:
992 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +0000993 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000994 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +0000995 Py_DECREF(v);
996 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000997 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000998 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +0000999 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001000
Guido van Rossum374a9221991-04-04 10:40:29 +00001001 case BINARY_MULTIPLY:
1002 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001003 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001004 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001005 Py_DECREF(v);
1006 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001007 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001008 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001009 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001010
Guido van Rossum374a9221991-04-04 10:40:29 +00001011 case BINARY_DIVIDE:
Tim Peters3caca232001-12-06 06:23:26 +00001012 if (!_Py_QnewFlag) {
1013 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001014 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001015 x = PyNumber_Divide(v, w);
1016 Py_DECREF(v);
1017 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001018 SET_TOP(x);
Tim Peters3caca232001-12-06 06:23:26 +00001019 if (x != NULL) continue;
1020 break;
1021 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001022 /* -Qnew is in effect: fall through to
Tim Peters3caca232001-12-06 06:23:26 +00001023 BINARY_TRUE_DIVIDE */
1024 case BINARY_TRUE_DIVIDE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001025 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001026 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001027 x = PyNumber_TrueDivide(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 Rossum4668b002001-08-08 05:00:18 +00001034 case BINARY_FLOOR_DIVIDE:
1035 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001036 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001037 x = PyNumber_FloorDivide(v, w);
1038 Py_DECREF(v);
1039 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001040 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001041 if (x != NULL) continue;
1042 break;
1043
Guido van Rossum374a9221991-04-04 10:40:29 +00001044 case BINARY_MODULO:
1045 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001046 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001047 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001048 Py_DECREF(v);
1049 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001050 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001051 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001052 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001053
Guido van Rossum374a9221991-04-04 10:40:29 +00001054 case BINARY_ADD:
1055 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001056 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001057 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001058 /* INLINE: int + int */
1059 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001060 a = PyInt_AS_LONG(v);
1061 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001062 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001063 if ((i^a) < 0 && (i^b) < 0)
1064 goto slow_add;
1065 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001066 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001067 else {
1068 slow_add:
Guido van Rossumc12da691997-07-17 23:12:42 +00001069 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001070 }
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_SUBTRACT:
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_sub;
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_sub:
Guido van Rossumc12da691997-07-17 23:12:42 +00001092 x = PyNumber_Subtract(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_SUBSCR:
1101 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001102 v = TOP();
Tim Petersb1c46982001-10-05 20:41:38 +00001103 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001104 /* INLINE: list[int] */
1105 long i = PyInt_AsLong(w);
1106 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +00001107 i += PyList_GET_SIZE(v);
Guido van Rossumc12da691997-07-17 23:12:42 +00001108 if (i < 0 ||
Guido van Rossumfa00e951998-07-08 15:02:37 +00001109 i >= PyList_GET_SIZE(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001110 PyErr_SetString(PyExc_IndexError,
1111 "list index out of range");
1112 x = NULL;
1113 }
1114 else {
Guido van Rossumfa00e951998-07-08 15:02:37 +00001115 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001116 Py_INCREF(x);
1117 }
1118 }
1119 else
1120 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001121 Py_DECREF(v);
1122 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001123 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001124 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001125 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001126
Guido van Rossum7928cd71991-10-24 14:59:31 +00001127 case BINARY_LSHIFT:
1128 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001129 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001130 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001131 Py_DECREF(v);
1132 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001133 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001134 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001135 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001136
Guido van Rossum7928cd71991-10-24 14:59:31 +00001137 case BINARY_RSHIFT:
1138 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001139 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001140 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001141 Py_DECREF(v);
1142 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001143 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001144 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001145 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001146
Guido van Rossum7928cd71991-10-24 14:59:31 +00001147 case BINARY_AND:
1148 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001149 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001150 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001151 Py_DECREF(v);
1152 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001153 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001154 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001155 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001156
Guido van Rossum7928cd71991-10-24 14:59:31 +00001157 case BINARY_XOR:
1158 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001159 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001160 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001161 Py_DECREF(v);
1162 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001163 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001164 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001165 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001166
Guido van Rossum7928cd71991-10-24 14:59:31 +00001167 case BINARY_OR:
1168 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001169 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001170 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001171 Py_DECREF(v);
1172 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001173 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001174 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001175 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001176
1177 case INPLACE_POWER:
1178 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001179 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001180 x = PyNumber_InPlacePower(v, w, Py_None);
1181 Py_DECREF(v);
1182 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001183 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001184 if (x != NULL) continue;
1185 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001186
Thomas Wouters434d0822000-08-24 20:11:32 +00001187 case INPLACE_MULTIPLY:
1188 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001189 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001190 x = PyNumber_InPlaceMultiply(v, w);
1191 Py_DECREF(v);
1192 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001193 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001194 if (x != NULL) continue;
1195 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001196
Thomas Wouters434d0822000-08-24 20:11:32 +00001197 case INPLACE_DIVIDE:
Tim Peters54b11912001-12-25 18:49:11 +00001198 if (!_Py_QnewFlag) {
1199 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001200 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001201 x = PyNumber_InPlaceDivide(v, w);
1202 Py_DECREF(v);
1203 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001204 SET_TOP(x);
Tim Peters54b11912001-12-25 18:49:11 +00001205 if (x != NULL) continue;
1206 break;
1207 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001208 /* -Qnew is in effect: fall through to
Tim Peters54b11912001-12-25 18:49:11 +00001209 INPLACE_TRUE_DIVIDE */
1210 case INPLACE_TRUE_DIVIDE:
Thomas Wouters434d0822000-08-24 20:11:32 +00001211 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001212 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001213 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001214 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
Guido van Rossum4668b002001-08-08 05:00:18 +00001220 case INPLACE_FLOOR_DIVIDE:
1221 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001222 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001223 x = PyNumber_InPlaceFloorDivide(v, w);
1224 Py_DECREF(v);
1225 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001226 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001227 if (x != NULL) continue;
1228 break;
1229
Thomas Wouters434d0822000-08-24 20:11:32 +00001230 case INPLACE_MODULO:
1231 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001232 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001233 x = PyNumber_InPlaceRemainder(v, w);
1234 Py_DECREF(v);
1235 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001236 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001237 if (x != NULL) continue;
1238 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001239
Thomas Wouters434d0822000-08-24 20:11:32 +00001240 case INPLACE_ADD:
1241 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001242 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001243 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001244 /* INLINE: int + int */
1245 register long a, b, i;
1246 a = PyInt_AS_LONG(v);
1247 b = PyInt_AS_LONG(w);
1248 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001249 if ((i^a) < 0 && (i^b) < 0)
1250 goto slow_iadd;
1251 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001252 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001253 else {
1254 slow_iadd:
Thomas Wouters434d0822000-08-24 20:11:32 +00001255 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001256 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001257 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_SUBTRACT:
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_isub;
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_isub:
Thomas Wouters434d0822000-08-24 20:11:32 +00001278 x = PyNumber_InPlaceSubtract(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_LSHIFT:
1287 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001288 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001289 x = PyNumber_InPlaceLshift(v, w);
1290 Py_DECREF(v);
1291 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001292 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001293 if (x != NULL) continue;
1294 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001295
Thomas Wouters434d0822000-08-24 20:11:32 +00001296 case INPLACE_RSHIFT:
1297 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001298 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001299 x = PyNumber_InPlaceRshift(v, w);
1300 Py_DECREF(v);
1301 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001302 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001303 if (x != NULL) continue;
1304 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001305
Thomas Wouters434d0822000-08-24 20:11:32 +00001306 case INPLACE_AND:
1307 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001308 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001309 x = PyNumber_InPlaceAnd(v, w);
1310 Py_DECREF(v);
1311 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001312 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001313 if (x != NULL) continue;
1314 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001315
Thomas Wouters434d0822000-08-24 20:11:32 +00001316 case INPLACE_XOR:
1317 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001318 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001319 x = PyNumber_InPlaceXor(v, w);
1320 Py_DECREF(v);
1321 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001322 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001323 if (x != NULL) continue;
1324 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001325
Thomas Wouters434d0822000-08-24 20:11:32 +00001326 case INPLACE_OR:
1327 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001328 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001329 x = PyNumber_InPlaceOr(v, w);
1330 Py_DECREF(v);
1331 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001332 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001333 if (x != NULL) continue;
1334 break;
1335
Guido van Rossum374a9221991-04-04 10:40:29 +00001336 case SLICE+0:
1337 case SLICE+1:
1338 case SLICE+2:
1339 case SLICE+3:
1340 if ((opcode-SLICE) & 2)
1341 w = POP();
1342 else
1343 w = NULL;
1344 if ((opcode-SLICE) & 1)
1345 v = POP();
1346 else
1347 v = NULL;
Raymond Hettinger663004b2003-01-09 15:24:30 +00001348 u = TOP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001349 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001350 Py_DECREF(u);
1351 Py_XDECREF(v);
1352 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001353 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001354 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001355 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001356
Guido van Rossum374a9221991-04-04 10:40:29 +00001357 case STORE_SLICE+0:
1358 case STORE_SLICE+1:
1359 case STORE_SLICE+2:
1360 case STORE_SLICE+3:
1361 if ((opcode-STORE_SLICE) & 2)
1362 w = POP();
1363 else
1364 w = NULL;
1365 if ((opcode-STORE_SLICE) & 1)
1366 v = POP();
1367 else
1368 v = NULL;
1369 u = POP();
1370 t = POP();
1371 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +00001372 Py_DECREF(t);
1373 Py_DECREF(u);
1374 Py_XDECREF(v);
1375 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001376 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001377 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001378
Guido van Rossum374a9221991-04-04 10:40:29 +00001379 case DELETE_SLICE+0:
1380 case DELETE_SLICE+1:
1381 case DELETE_SLICE+2:
1382 case DELETE_SLICE+3:
1383 if ((opcode-DELETE_SLICE) & 2)
1384 w = POP();
1385 else
1386 w = NULL;
1387 if ((opcode-DELETE_SLICE) & 1)
1388 v = POP();
1389 else
1390 v = NULL;
1391 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001392 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +00001393 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00001394 Py_DECREF(u);
1395 Py_XDECREF(v);
1396 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001397 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001398 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001399
Guido van Rossum374a9221991-04-04 10:40:29 +00001400 case STORE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001401 w = TOP();
1402 v = SECOND();
1403 u = THIRD();
1404 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001405 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001406 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001407 Py_DECREF(u);
1408 Py_DECREF(v);
1409 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001410 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001411 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001412
Guido van Rossum374a9221991-04-04 10:40:29 +00001413 case DELETE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001414 w = TOP();
1415 v = SECOND();
1416 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001417 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001418 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001419 Py_DECREF(v);
1420 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001421 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001422 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001423
Guido van Rossum374a9221991-04-04 10:40:29 +00001424 case PRINT_EXPR:
1425 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001426 w = PySys_GetObject("displayhook");
1427 if (w == NULL) {
1428 PyErr_SetString(PyExc_RuntimeError,
1429 "lost sys.displayhook");
1430 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001431 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001432 }
1433 if (err == 0) {
1434 x = Py_BuildValue("(O)", v);
1435 if (x == NULL)
1436 err = -1;
1437 }
1438 if (err == 0) {
1439 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001440 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001441 if (w == NULL)
1442 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001443 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001444 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001445 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001446 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001447
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001448 case PRINT_ITEM_TO:
1449 w = stream = POP();
1450 /* fall through to PRINT_ITEM */
1451
Guido van Rossum374a9221991-04-04 10:40:29 +00001452 case PRINT_ITEM:
1453 v = POP();
Barry Warsaw093abe02000-08-29 04:56:13 +00001454 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001455 w = PySys_GetObject("stdout");
1456 if (w == NULL) {
1457 PyErr_SetString(PyExc_RuntimeError,
1458 "lost sys.stdout");
1459 err = -1;
1460 }
Guido van Rossum8f183201997-12-31 05:53:15 +00001461 }
Tim Peters8e5fd532002-03-24 19:25:00 +00001462 if (w != NULL && PyFile_SoftSpace(w, 0))
Guido van Rossumbe270261997-05-22 22:26:18 +00001463 err = PyFile_WriteString(" ", w);
1464 if (err == 0)
1465 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001466 if (err == 0) {
Tim Peters8e5fd532002-03-24 19:25:00 +00001467 /* XXX move into writeobject() ? */
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001468 if (PyString_Check(v)) {
1469 char *s = PyString_AS_STRING(v);
1470 int len = PyString_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001471 if (len == 0 ||
1472 !isspace(Py_CHARMASK(s[len-1])) ||
1473 s[len-1] == ' ')
1474 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001475 }
Martin v. Löwis8d3ce5a2001-12-18 22:36:40 +00001476#ifdef Py_USING_UNICODE
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001477 else if (PyUnicode_Check(v)) {
1478 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
1479 int len = PyUnicode_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001480 if (len == 0 ||
1481 !Py_UNICODE_ISSPACE(s[len-1]) ||
1482 s[len-1] == ' ')
1483 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001484 }
Michael W. Hudsond95c8282002-05-20 13:56:11 +00001485#endif
Tim Peters8e5fd532002-03-24 19:25:00 +00001486 else
1487 PyFile_SoftSpace(w, 1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001488 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001489 Py_DECREF(v);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001490 Py_XDECREF(stream);
1491 stream = NULL;
1492 if (err == 0)
1493 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001494 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001495
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001496 case PRINT_NEWLINE_TO:
1497 w = stream = POP();
1498 /* fall through to PRINT_NEWLINE */
1499
Guido van Rossum374a9221991-04-04 10:40:29 +00001500 case PRINT_NEWLINE:
Barry Warsaw093abe02000-08-29 04:56:13 +00001501 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001502 w = PySys_GetObject("stdout");
1503 if (w == NULL)
1504 PyErr_SetString(PyExc_RuntimeError,
1505 "lost sys.stdout");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001506 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001507 if (w != NULL) {
1508 err = PyFile_WriteString("\n", w);
1509 if (err == 0)
1510 PyFile_SoftSpace(w, 0);
1511 }
1512 Py_XDECREF(stream);
1513 stream = NULL;
Guido van Rossum374a9221991-04-04 10:40:29 +00001514 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001515
Thomas Wouters434d0822000-08-24 20:11:32 +00001516
1517#ifdef CASE_TOO_BIG
1518 default: switch (opcode) {
1519#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001520 case BREAK_LOOP:
1521 why = WHY_BREAK;
1522 break;
Guido van Rossum66b0e9c2001-03-21 19:17:22 +00001523
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00001524 case CONTINUE_LOOP:
1525 retval = PyInt_FromLong(oparg);
1526 why = WHY_CONTINUE;
1527 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001528
Guido van Rossumf10570b1995-07-07 22:53:21 +00001529 case RAISE_VARARGS:
1530 u = v = w = NULL;
1531 switch (oparg) {
1532 case 3:
1533 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001534 /* Fallthrough */
1535 case 2:
1536 v = POP(); /* value */
1537 /* Fallthrough */
1538 case 1:
1539 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001540 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001541 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001542 break;
1543 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001544 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001545 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001546 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001547 break;
1548 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001549 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001550
Guido van Rossum374a9221991-04-04 10:40:29 +00001551 case LOAD_LOCALS:
Guido van Rossum681d79a1995-07-18 14:51:37 +00001552 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001553 PyErr_SetString(PyExc_SystemError,
1554 "no locals");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001555 break;
1556 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001557 Py_INCREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001558 PUSH(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001559 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001560
Guido van Rossum374a9221991-04-04 10:40:29 +00001561 case RETURN_VALUE:
1562 retval = POP();
1563 why = WHY_RETURN;
1564 break;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001565
Tim Peters5ca576e2001-06-18 22:08:13 +00001566 case YIELD_VALUE:
1567 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001568 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001569 why = WHY_YIELD;
1570 break;
1571
1572
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001573 case EXEC_STMT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001574 w = TOP();
1575 v = SECOND();
1576 u = THIRD();
1577 STACKADJ(-3);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001578 err = exec_statement(f, u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001579 Py_DECREF(u);
1580 Py_DECREF(v);
1581 Py_DECREF(w);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001582 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001583
Guido van Rossum374a9221991-04-04 10:40:29 +00001584 case POP_BLOCK:
1585 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001586 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001587 while (STACK_LEVEL() > b->b_level) {
1588 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001589 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001590 }
1591 }
1592 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001593
Guido van Rossum374a9221991-04-04 10:40:29 +00001594 case END_FINALLY:
1595 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001596 if (PyInt_Check(v)) {
1597 why = (enum why_code) PyInt_AsLong(v);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00001598 if (why == WHY_RETURN ||
Tim Peters5ca576e2001-06-18 22:08:13 +00001599 why == WHY_YIELD ||
Guido van Rossumc5fe5eb2002-06-12 03:45:21 +00001600 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001601 retval = POP();
1602 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001603 else if (PyString_Check(v) || PyClass_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001604 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001605 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001606 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001607 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001608 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001609 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001610 else if (v != Py_None) {
1611 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001612 "'finally' pops bad exception");
1613 why = WHY_EXCEPTION;
1614 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001615 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001616 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001617
Guido van Rossum374a9221991-04-04 10:40:29 +00001618 case BUILD_CLASS:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001619 u = TOP();
1620 v = SECOND();
1621 w = THIRD();
1622 STACKADJ(-2);
Guido van Rossum25831651993-05-19 14:50:45 +00001623 x = build_class(u, v, w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001624 SET_TOP(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001625 Py_DECREF(u);
1626 Py_DECREF(v);
1627 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001628 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001629
Guido van Rossum374a9221991-04-04 10:40:29 +00001630 case STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001631 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001632 v = POP();
Guido van Rossum681d79a1995-07-18 14:51:37 +00001633 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001634 PyErr_Format(PyExc_SystemError,
1635 "no locals found when storing %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001636 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001637 break;
1638 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001639 err = PyDict_SetItem(x, w, v);
1640 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001641 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001642
Guido van Rossum374a9221991-04-04 10:40:29 +00001643 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001644 w = GETITEM(names, oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001645 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001646 PyErr_Format(PyExc_SystemError,
1647 "no locals when deleting %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001648 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001649 break;
1650 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001651 if ((err = PyDict_DelItem(x, w)) != 0)
Guido van Rossumac7be682001-01-17 15:42:30 +00001652 format_exc_check_arg(PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001653 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001654 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001655
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001656 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001657 v = POP();
Raymond Hettinger21012b82003-02-26 18:11:50 +00001658 if (PyTuple_CheckExact(v)) {
Barry Warsawe42b18f1997-08-25 22:13:04 +00001659 if (PyTuple_Size(v) != oparg) {
1660 PyErr_SetString(PyExc_ValueError,
1661 "unpack tuple of wrong size");
1662 why = WHY_EXCEPTION;
1663 }
1664 else {
1665 for (; --oparg >= 0; ) {
1666 w = PyTuple_GET_ITEM(v, oparg);
1667 Py_INCREF(w);
1668 PUSH(w);
1669 }
1670 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001671 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00001672 else if (PyList_CheckExact(v)) {
Barry Warsawe42b18f1997-08-25 22:13:04 +00001673 if (PyList_Size(v) != oparg) {
1674 PyErr_SetString(PyExc_ValueError,
1675 "unpack list of wrong size");
1676 why = WHY_EXCEPTION;
1677 }
1678 else {
1679 for (; --oparg >= 0; ) {
1680 w = PyList_GET_ITEM(v, oparg);
1681 Py_INCREF(w);
1682 PUSH(w);
1683 }
1684 }
1685 }
Tim Petersd6d010b2001-06-21 02:49:55 +00001686 else if (unpack_iterable(v, oparg,
1687 stack_pointer + oparg))
1688 stack_pointer += oparg;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001689 else {
1690 if (PyErr_ExceptionMatches(PyExc_TypeError))
1691 PyErr_SetString(PyExc_TypeError,
1692 "unpack non-sequence");
Barry Warsawe42b18f1997-08-25 22:13:04 +00001693 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001694 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001695 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001696 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001697
Guido van Rossum374a9221991-04-04 10:40:29 +00001698 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001699 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001700 v = TOP();
1701 u = SECOND();
1702 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001703 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1704 Py_DECREF(v);
1705 Py_DECREF(u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001706 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001707
Guido van Rossum374a9221991-04-04 10:40:29 +00001708 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001709 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001710 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001711 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1712 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001713 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001714 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001715
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001716 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001717 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001718 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001719 err = PyDict_SetItem(f->f_globals, w, v);
1720 Py_DECREF(v);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001721 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001722
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001723 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001724 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001725 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001726 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001727 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001728 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001729
Guido van Rossum374a9221991-04-04 10:40:29 +00001730 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001731 w = GETITEM(names, oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001732 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001733 PyErr_Format(PyExc_SystemError,
1734 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001735 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001736 break;
1737 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001738 x = PyDict_GetItem(x, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001739 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001740 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001741 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001742 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001743 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001744 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001745 PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001746 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001747 break;
1748 }
1749 }
1750 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001751 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001752 PUSH(x);
1753 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001754
Guido van Rossum374a9221991-04-04 10:40:29 +00001755 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001756 w = GETITEM(names, oparg);
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001757 if (PyString_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00001758 /* Inline the PyDict_GetItem() calls.
1759 WARNING: this is an extreme speed hack.
1760 Do not try this at home. */
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001761 long hash = ((PyStringObject *)w)->ob_shash;
1762 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001763 PyDictObject *d;
1764 d = (PyDictObject *)(f->f_globals);
1765 x = d->ma_lookup(d, w, hash)->me_value;
1766 if (x != NULL) {
1767 Py_INCREF(x);
1768 PUSH(x);
1769 continue;
1770 }
1771 d = (PyDictObject *)(f->f_builtins);
1772 x = d->ma_lookup(d, w, hash)->me_value;
1773 if (x != NULL) {
1774 Py_INCREF(x);
1775 PUSH(x);
1776 continue;
1777 }
1778 goto load_global_error;
1779 }
1780 }
1781 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00001782 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001783 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001784 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001785 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001786 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00001787 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001788 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001789 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001790 break;
1791 }
1792 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001793 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001794 PUSH(x);
1795 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001796
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001797 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001798 x = GETLOCAL(oparg);
1799 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001800 format_exc_check_arg(
1801 PyExc_UnboundLocalError,
1802 UNBOUNDLOCAL_ERROR_MSG,
1803 PyTuple_GetItem(co->co_varnames, oparg)
1804 );
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001805 break;
1806 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00001807 SETLOCAL(oparg, NULL);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001808 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001809
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001810 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001811 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001812 Py_INCREF(x);
1813 PUSH(x);
1814 break;
1815
1816 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001817 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001818 w = PyCell_Get(x);
Jeremy Hylton2524d692001-02-05 17:23:16 +00001819 if (w == NULL) {
Jeremy Hylton76c81ee2002-07-11 16:56:38 +00001820 err = -1;
1821 /* Don't stomp existing exception */
1822 if (PyErr_Occurred())
1823 break;
Jeremy Hyltonc76770c2001-04-13 16:51:46 +00001824 if (oparg < f->f_ncells) {
Jeremy Hylton2524d692001-02-05 17:23:16 +00001825 v = PyTuple_GetItem(co->co_cellvars,
1826 oparg);
Jeremy Hyltonc76770c2001-04-13 16:51:46 +00001827 format_exc_check_arg(
1828 PyExc_UnboundLocalError,
1829 UNBOUNDLOCAL_ERROR_MSG,
1830 v);
1831 } else {
Jeremy Hylton2524d692001-02-05 17:23:16 +00001832 v = PyTuple_GetItem(
1833 co->co_freevars,
1834 oparg - f->f_ncells);
Jeremy Hyltonc76770c2001-04-13 16:51:46 +00001835 format_exc_check_arg(
1836 PyExc_NameError,
1837 UNBOUNDFREE_ERROR_MSG,
1838 v);
1839 }
Jeremy Hylton2524d692001-02-05 17:23:16 +00001840 break;
1841 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001842 PUSH(w);
1843 break;
1844
1845 case STORE_DEREF:
1846 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001847 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001848 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00001849 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001850 continue;
1851
Guido van Rossum374a9221991-04-04 10:40:29 +00001852 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001853 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001854 if (x != NULL) {
1855 for (; --oparg >= 0;) {
1856 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001857 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001858 }
1859 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001860 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001861 }
1862 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001863
Guido van Rossum374a9221991-04-04 10:40:29 +00001864 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00001865 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001866 if (x != NULL) {
1867 for (; --oparg >= 0;) {
1868 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00001869 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001870 }
1871 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001872 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001873 }
1874 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001875
Guido van Rossum374a9221991-04-04 10:40:29 +00001876 case BUILD_MAP:
Guido van Rossumb209a111997-04-29 18:18:01 +00001877 x = PyDict_New();
Guido van Rossum374a9221991-04-04 10:40:29 +00001878 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001879 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001880 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001881
Guido van Rossum374a9221991-04-04 10:40:29 +00001882 case LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001883 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001884 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001885 x = PyObject_GetAttr(v, w);
1886 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001887 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001888 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001889 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001890
Guido van Rossum374a9221991-04-04 10:40:29 +00001891 case COMPARE_OP:
1892 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001893 v = TOP();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001894 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001895 /* INLINE: cmp(int, int) */
1896 register long a, b;
1897 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001898 a = PyInt_AS_LONG(v);
1899 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001900 switch (oparg) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00001901 case PyCmp_LT: res = a < b; break;
1902 case PyCmp_LE: res = a <= b; break;
1903 case PyCmp_EQ: res = a == b; break;
1904 case PyCmp_NE: res = a != b; break;
1905 case PyCmp_GT: res = a > b; break;
1906 case PyCmp_GE: res = a >= b; break;
1907 case PyCmp_IS: res = v == w; break;
1908 case PyCmp_IS_NOT: res = v != w; break;
Guido van Rossumc12da691997-07-17 23:12:42 +00001909 default: goto slow_compare;
1910 }
1911 x = res ? Py_True : Py_False;
1912 Py_INCREF(x);
1913 }
1914 else {
1915 slow_compare:
1916 x = cmp_outcome(oparg, v, w);
1917 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001918 Py_DECREF(v);
1919 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001920 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001921 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001922 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001923
Guido van Rossum374a9221991-04-04 10:40:29 +00001924 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001925 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001926 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001927 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001928 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00001929 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001930 break;
1931 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001932 u = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001933 w = Py_BuildValue("(OOOO)",
Guido van Rossum681d79a1995-07-18 14:51:37 +00001934 w,
1935 f->f_globals,
Guido van Rossuma027efa1997-05-05 20:56:21 +00001936 f->f_locals == NULL ?
1937 Py_None : f->f_locals,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001938 u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001939 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001940 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001941 u = POP();
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001942 x = NULL;
1943 break;
1944 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001945 x = PyEval_CallObject(x, w);
1946 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001947 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001948 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001949 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001950
Thomas Wouters52152252000-08-17 22:55:00 +00001951 case IMPORT_STAR:
1952 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001953 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001954 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001955 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001956 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001957 break;
1958 }
Thomas Wouters52152252000-08-17 22:55:00 +00001959 err = import_all_from(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001960 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00001961 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001962 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001963 break;
Guido van Rossum25831651993-05-19 14:50:45 +00001964
Thomas Wouters52152252000-08-17 22:55:00 +00001965 case IMPORT_FROM:
Skip Montanaro496e6582002-08-06 17:47:40 +00001966 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00001967 v = TOP();
1968 x = import_from(v, w);
1969 PUSH(x);
1970 if (x != NULL) continue;
1971 break;
1972
Guido van Rossum374a9221991-04-04 10:40:29 +00001973 case JUMP_FORWARD:
1974 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001975 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001976
Guido van Rossum374a9221991-04-04 10:40:29 +00001977 case JUMP_IF_FALSE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00001978 w = TOP();
1979 if (w == Py_True)
1980 continue;
1981 if (w == Py_False) {
1982 JUMPBY(oparg);
1983 continue;
1984 }
1985 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001986 if (err > 0)
1987 err = 0;
1988 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00001989 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001990 else
1991 break;
1992 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001993
Guido van Rossum374a9221991-04-04 10:40:29 +00001994 case JUMP_IF_TRUE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00001995 w = TOP();
1996 if (w == Py_False)
1997 continue;
1998 if (w == Py_True) {
1999 JUMPBY(oparg);
2000 continue;
2001 }
2002 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002003 if (err > 0) {
2004 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00002005 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002006 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002007 else if (err == 0)
2008 ;
2009 else
2010 break;
2011 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002012
Guido van Rossum374a9221991-04-04 10:40:29 +00002013 case JUMP_ABSOLUTE:
2014 JUMPTO(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002015 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002016
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002017 case GET_ITER:
2018 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002019 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002020 x = PyObject_GetIter(v);
2021 Py_DECREF(v);
2022 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002023 SET_TOP(x);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002024 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002025 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002026 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002027 break;
2028
2029 case FOR_ITER:
2030 /* before: [iter]; after: [iter, iter()] *or* [] */
2031 v = TOP();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002032 x = PyIter_Next(v);
2033 if (x != NULL) {
2034 PUSH(x);
2035 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002036 }
Tim Petersf4848da2001-05-05 00:14:56 +00002037 if (!PyErr_Occurred()) {
2038 /* iterator ended normally */
2039 x = v = POP();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002040 Py_DECREF(v);
2041 JUMPBY(oparg);
2042 continue;
2043 }
2044 break;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002045
Guido van Rossum374a9221991-04-04 10:40:29 +00002046 case SETUP_LOOP:
2047 case SETUP_EXCEPT:
2048 case SETUP_FINALLY:
Guido van Rossumb209a111997-04-29 18:18:01 +00002049 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002050 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002051 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002052
Guido van Rossumf10570b1995-07-07 22:53:21 +00002053 case CALL_FUNCTION:
Jeremy Hylton985eba52003-02-05 23:13:00 +00002054 PCALL(PCALL_ALL);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002055 x = call_function(&stack_pointer, oparg);
2056 PUSH(x);
2057 if (x != NULL)
2058 continue;
2059 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002060
Jeremy Hylton76901512000-03-28 23:49:17 +00002061 case CALL_FUNCTION_VAR:
2062 case CALL_FUNCTION_KW:
2063 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002064 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002065 int na = oparg & 0xff;
2066 int nk = (oparg>>8) & 0xff;
2067 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002068 int n = na + 2 * nk;
2069 PyObject **pfunc, *func;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002070 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002071 if (flags & CALL_FLAG_VAR)
2072 n++;
2073 if (flags & CALL_FLAG_KW)
2074 n++;
2075 pfunc = stack_pointer - n - 1;
2076 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002077
Guido van Rossumac7be682001-01-17 15:42:30 +00002078 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002079 && PyMethod_GET_SELF(func) != NULL) {
2080 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002081 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002082 func = PyMethod_GET_FUNCTION(func);
2083 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002084 Py_DECREF(*pfunc);
2085 *pfunc = self;
2086 na++;
2087 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002088 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002089 Py_INCREF(func);
2090 x = ext_do_call(func, &stack_pointer, flags, na, nk);
Jeremy Hylton76901512000-03-28 23:49:17 +00002091 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002092
Jeremy Hylton76901512000-03-28 23:49:17 +00002093 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002094 w = POP();
2095 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002096 }
2097 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002098 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002099 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002100 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002101 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002102
Guido van Rossum681d79a1995-07-18 14:51:37 +00002103 case MAKE_FUNCTION:
2104 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002105 x = PyFunction_New(v, f->f_globals);
2106 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002107 /* XXX Maybe this should be a separate opcode? */
2108 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002109 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002110 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002111 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002112 x = NULL;
2113 break;
2114 }
2115 while (--oparg >= 0) {
2116 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002117 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002118 }
2119 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002120 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002121 }
2122 PUSH(x);
2123 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002124
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002125 case MAKE_CLOSURE:
2126 {
2127 int nfree;
2128 v = POP(); /* code object */
2129 x = PyFunction_New(v, f->f_globals);
Jeremy Hylton733c8932001-12-13 19:51:56 +00002130 nfree = PyCode_GetNumFree((PyCodeObject *)v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002131 Py_DECREF(v);
2132 /* XXX Maybe this should be a separate opcode? */
2133 if (x != NULL && nfree > 0) {
2134 v = PyTuple_New(nfree);
2135 if (v == NULL) {
2136 Py_DECREF(x);
2137 x = NULL;
2138 break;
2139 }
2140 while (--nfree >= 0) {
2141 w = POP();
2142 PyTuple_SET_ITEM(v, nfree, w);
2143 }
2144 err = PyFunction_SetClosure(x, v);
2145 Py_DECREF(v);
2146 }
2147 if (x != NULL && oparg > 0) {
2148 v = PyTuple_New(oparg);
2149 if (v == NULL) {
2150 Py_DECREF(x);
2151 x = NULL;
2152 break;
2153 }
2154 while (--oparg >= 0) {
2155 w = POP();
2156 PyTuple_SET_ITEM(v, oparg, w);
2157 }
2158 err = PyFunction_SetDefaults(x, v);
2159 Py_DECREF(v);
2160 }
2161 PUSH(x);
2162 break;
2163 }
2164
Guido van Rossum8861b741996-07-30 16:49:37 +00002165 case BUILD_SLICE:
2166 if (oparg == 3)
2167 w = POP();
2168 else
2169 w = NULL;
2170 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002171 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002172 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002173 Py_DECREF(u);
2174 Py_DECREF(v);
2175 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002176 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002177 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002178 break;
2179
Fred Drakeef8ace32000-08-24 00:32:09 +00002180 case EXTENDED_ARG:
2181 opcode = NEXTOP();
2182 oparg = oparg<<16 | NEXTARG();
2183 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002184
Guido van Rossum374a9221991-04-04 10:40:29 +00002185 default:
2186 fprintf(stderr,
2187 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002188 PyCode_Addr2Line(f->f_code, f->f_lasti),
2189 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002190 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002191 why = WHY_EXCEPTION;
2192 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002193
2194#ifdef CASE_TOO_BIG
2195 }
2196#endif
2197
Guido van Rossum374a9221991-04-04 10:40:29 +00002198 } /* switch */
2199
2200 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002201
Guido van Rossum374a9221991-04-04 10:40:29 +00002202 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002203
Guido van Rossum374a9221991-04-04 10:40:29 +00002204 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002205 if (err == 0 && x != NULL) {
2206#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002207 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002208 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002209 fprintf(stderr,
2210 "XXX undetected error\n");
2211 else
2212#endif
2213 continue; /* Normal, fast path */
2214 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002215 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002216 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002217 err = 0;
2218 }
2219
Guido van Rossum374a9221991-04-04 10:40:29 +00002220 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002221
Guido van Rossum374a9221991-04-04 10:40:29 +00002222 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002223 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002224 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002225 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002226 why = WHY_EXCEPTION;
2227 }
2228 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002229#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002230 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002231 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002232 if (PyErr_Occurred()) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002233 fprintf(stderr,
2234 "XXX undetected error (why=%d)\n",
2235 why);
2236 why = WHY_EXCEPTION;
2237 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002238 }
2239#endif
2240
2241 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002242
Guido van Rossum374a9221991-04-04 10:40:29 +00002243 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002244 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002245
Fred Drake8f51f542001-10-04 14:48:42 +00002246 if (tstate->c_tracefunc != NULL)
2247 call_exc_trace(tstate->c_tracefunc,
2248 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002249 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002250
Guido van Rossum374a9221991-04-04 10:40:29 +00002251 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002252
Guido van Rossum374a9221991-04-04 10:40:29 +00002253 if (why == WHY_RERAISE)
2254 why = WHY_EXCEPTION;
2255
2256 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002257
Tim Peters5ca576e2001-06-18 22:08:13 +00002258 while (why != WHY_NOT && why != WHY_YIELD && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002259 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002260
2261 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2262 /* For a continue inside a try block,
2263 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002264 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2265 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002266 why = WHY_NOT;
2267 JUMPTO(PyInt_AS_LONG(retval));
2268 Py_DECREF(retval);
2269 break;
2270 }
2271
Guido van Rossum374a9221991-04-04 10:40:29 +00002272 while (STACK_LEVEL() > b->b_level) {
2273 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002274 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002275 }
2276 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2277 why = WHY_NOT;
2278 JUMPTO(b->b_handler);
2279 break;
2280 }
2281 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002282 (b->b_type == SETUP_EXCEPT &&
2283 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002284 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002285 PyObject *exc, *val, *tb;
2286 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002287 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002288 val = Py_None;
2289 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002290 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002291 /* Make the raw exception data
2292 available to the handler,
2293 so a program can emulate the
2294 Python main loop. Don't do
2295 this for 'finally'. */
2296 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002297 PyErr_NormalizeException(
2298 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002299 set_exc_info(tstate,
2300 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002301 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002302 if (tb == NULL) {
2303 Py_INCREF(Py_None);
2304 PUSH(Py_None);
2305 } else
2306 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002307 PUSH(val);
2308 PUSH(exc);
2309 }
2310 else {
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002311 if (why == WHY_RETURN ||
Guido van Rossumc5fe5eb2002-06-12 03:45:21 +00002312 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00002313 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002314 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002315 PUSH(v);
2316 }
2317 why = WHY_NOT;
2318 JUMPTO(b->b_handler);
2319 break;
2320 }
2321 } /* unwind stack */
2322
2323 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002324
Guido van Rossum374a9221991-04-04 10:40:29 +00002325 if (why != WHY_NOT)
2326 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002327
Guido van Rossum374a9221991-04-04 10:40:29 +00002328 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002329
Guido van Rossum35974fb2001-12-06 21:28:18 +00002330 if (why != WHY_YIELD) {
2331 /* Pop remaining stack entries -- but when yielding */
2332 while (!EMPTY()) {
2333 v = POP();
2334 Py_XDECREF(v);
2335 }
2336 }
2337
Tim Peters5ca576e2001-06-18 22:08:13 +00002338 if (why != WHY_RETURN && why != WHY_YIELD)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002339 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002340
Fred Drake9e3ad782001-07-03 23:39:52 +00002341 if (tstate->use_tracing) {
2342 if (tstate->c_tracefunc
2343 && (why == WHY_RETURN || why == WHY_YIELD)) {
2344 if (call_trace(tstate->c_tracefunc,
2345 tstate->c_traceobj, f,
2346 PyTrace_RETURN, retval)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002347 Py_XDECREF(retval);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002348 retval = NULL;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002349 why = WHY_EXCEPTION;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002350 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002351 }
Fred Drake8f51f542001-10-04 14:48:42 +00002352 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002353 if (why == WHY_EXCEPTION)
2354 call_trace_protected(tstate->c_profilefunc,
2355 tstate->c_profileobj, f,
2356 PyTrace_RETURN);
2357 else if (call_trace(tstate->c_profilefunc,
2358 tstate->c_profileobj, f,
2359 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002360 Py_XDECREF(retval);
2361 retval = NULL;
2362 why = WHY_EXCEPTION;
2363 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002364 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002365 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002366
Guido van Rossuma027efa1997-05-05 20:56:21 +00002367 reset_exc_info(tstate);
2368
Tim Peters5ca576e2001-06-18 22:08:13 +00002369 /* pop frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002370 --tstate->recursion_depth;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002371 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002372
Guido van Rossum96a42c81992-01-12 02:29:51 +00002373 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002374}
2375
Tim Peters6d6c1a32001-08-02 04:15:00 +00002376PyObject *
2377PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002378 PyObject **args, int argcount, PyObject **kws, int kwcount,
2379 PyObject **defs, int defcount, PyObject *closure)
2380{
2381 register PyFrameObject *f;
2382 register PyObject *retval = NULL;
2383 register PyObject **fastlocals, **freevars;
2384 PyThreadState *tstate = PyThreadState_GET();
2385 PyObject *x, *u;
2386
2387 if (globals == NULL) {
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002388 PyErr_SetString(PyExc_SystemError,
2389 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002390 return NULL;
2391 }
2392
Jeremy Hylton985eba52003-02-05 23:13:00 +00002393 assert(globals != NULL);
2394 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002395 if (f == NULL)
2396 return NULL;
2397
2398 fastlocals = f->f_localsplus;
2399 freevars = f->f_localsplus + f->f_nlocals;
2400
2401 if (co->co_argcount > 0 ||
2402 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2403 int i;
2404 int n = argcount;
2405 PyObject *kwdict = NULL;
2406 if (co->co_flags & CO_VARKEYWORDS) {
2407 kwdict = PyDict_New();
2408 if (kwdict == NULL)
2409 goto fail;
2410 i = co->co_argcount;
2411 if (co->co_flags & CO_VARARGS)
2412 i++;
2413 SETLOCAL(i, kwdict);
2414 }
2415 if (argcount > co->co_argcount) {
2416 if (!(co->co_flags & CO_VARARGS)) {
2417 PyErr_Format(PyExc_TypeError,
2418 "%.200s() takes %s %d "
2419 "%sargument%s (%d given)",
2420 PyString_AsString(co->co_name),
2421 defcount ? "at most" : "exactly",
2422 co->co_argcount,
2423 kwcount ? "non-keyword " : "",
2424 co->co_argcount == 1 ? "" : "s",
2425 argcount);
2426 goto fail;
2427 }
2428 n = co->co_argcount;
2429 }
2430 for (i = 0; i < n; i++) {
2431 x = args[i];
2432 Py_INCREF(x);
2433 SETLOCAL(i, x);
2434 }
2435 if (co->co_flags & CO_VARARGS) {
2436 u = PyTuple_New(argcount - n);
2437 if (u == NULL)
2438 goto fail;
2439 SETLOCAL(co->co_argcount, u);
2440 for (i = n; i < argcount; i++) {
2441 x = args[i];
2442 Py_INCREF(x);
2443 PyTuple_SET_ITEM(u, i-n, x);
2444 }
2445 }
2446 for (i = 0; i < kwcount; i++) {
2447 PyObject *keyword = kws[2*i];
2448 PyObject *value = kws[2*i + 1];
2449 int j;
2450 if (keyword == NULL || !PyString_Check(keyword)) {
2451 PyErr_Format(PyExc_TypeError,
2452 "%.200s() keywords must be strings",
2453 PyString_AsString(co->co_name));
2454 goto fail;
2455 }
2456 /* XXX slow -- speed up using dictionary? */
2457 for (j = 0; j < co->co_argcount; j++) {
2458 PyObject *nm = PyTuple_GET_ITEM(
2459 co->co_varnames, j);
2460 int cmp = PyObject_RichCompareBool(
2461 keyword, nm, Py_EQ);
2462 if (cmp > 0)
2463 break;
2464 else if (cmp < 0)
2465 goto fail;
2466 }
2467 /* Check errors from Compare */
2468 if (PyErr_Occurred())
2469 goto fail;
2470 if (j >= co->co_argcount) {
2471 if (kwdict == NULL) {
2472 PyErr_Format(PyExc_TypeError,
2473 "%.200s() got an unexpected "
2474 "keyword argument '%.400s'",
2475 PyString_AsString(co->co_name),
2476 PyString_AsString(keyword));
2477 goto fail;
2478 }
2479 PyDict_SetItem(kwdict, keyword, value);
2480 }
2481 else {
2482 if (GETLOCAL(j) != NULL) {
2483 PyErr_Format(PyExc_TypeError,
2484 "%.200s() got multiple "
2485 "values for keyword "
2486 "argument '%.400s'",
2487 PyString_AsString(co->co_name),
2488 PyString_AsString(keyword));
2489 goto fail;
2490 }
2491 Py_INCREF(value);
2492 SETLOCAL(j, value);
2493 }
2494 }
2495 if (argcount < co->co_argcount) {
2496 int m = co->co_argcount - defcount;
2497 for (i = argcount; i < m; i++) {
2498 if (GETLOCAL(i) == NULL) {
2499 PyErr_Format(PyExc_TypeError,
2500 "%.200s() takes %s %d "
2501 "%sargument%s (%d given)",
2502 PyString_AsString(co->co_name),
2503 ((co->co_flags & CO_VARARGS) ||
2504 defcount) ? "at least"
2505 : "exactly",
2506 m, kwcount ? "non-keyword " : "",
2507 m == 1 ? "" : "s", i);
2508 goto fail;
2509 }
2510 }
2511 if (n > m)
2512 i = n - m;
2513 else
2514 i = 0;
2515 for (; i < defcount; i++) {
2516 if (GETLOCAL(m+i) == NULL) {
2517 PyObject *def = defs[i];
2518 Py_INCREF(def);
2519 SETLOCAL(m+i, def);
2520 }
2521 }
2522 }
2523 }
2524 else {
2525 if (argcount > 0 || kwcount > 0) {
2526 PyErr_Format(PyExc_TypeError,
2527 "%.200s() takes no arguments (%d given)",
2528 PyString_AsString(co->co_name),
2529 argcount + kwcount);
2530 goto fail;
2531 }
2532 }
2533 /* Allocate and initialize storage for cell vars, and copy free
2534 vars into frame. This isn't too efficient right now. */
2535 if (f->f_ncells) {
2536 int i = 0, j = 0, nargs, found;
2537 char *cellname, *argname;
2538 PyObject *c;
2539
2540 nargs = co->co_argcount;
2541 if (co->co_flags & CO_VARARGS)
2542 nargs++;
2543 if (co->co_flags & CO_VARKEYWORDS)
2544 nargs++;
2545
2546 /* Check for cells that shadow args */
2547 for (i = 0; i < f->f_ncells && j < nargs; ++i) {
2548 cellname = PyString_AS_STRING(
2549 PyTuple_GET_ITEM(co->co_cellvars, i));
2550 found = 0;
2551 while (j < nargs) {
2552 argname = PyString_AS_STRING(
2553 PyTuple_GET_ITEM(co->co_varnames, j));
2554 if (strcmp(cellname, argname) == 0) {
2555 c = PyCell_New(GETLOCAL(j));
2556 if (c == NULL)
2557 goto fail;
2558 GETLOCAL(f->f_nlocals + i) = c;
2559 found = 1;
2560 break;
2561 }
2562 j++;
2563 }
2564 if (found == 0) {
2565 c = PyCell_New(NULL);
2566 if (c == NULL)
2567 goto fail;
2568 SETLOCAL(f->f_nlocals + i, c);
2569 }
2570 }
2571 /* Initialize any that are left */
2572 while (i < f->f_ncells) {
2573 c = PyCell_New(NULL);
2574 if (c == NULL)
2575 goto fail;
2576 SETLOCAL(f->f_nlocals + i, c);
2577 i++;
2578 }
2579 }
2580 if (f->f_nfreevars) {
2581 int i;
2582 for (i = 0; i < f->f_nfreevars; ++i) {
2583 PyObject *o = PyTuple_GET_ITEM(closure, i);
2584 Py_INCREF(o);
2585 freevars[f->f_ncells + i] = o;
2586 }
2587 }
2588
Tim Peters5ca576e2001-06-18 22:08:13 +00002589 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002590 /* Don't need to keep the reference to f_back, it will be set
2591 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002592 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002593 f->f_back = NULL;
2594
Jeremy Hylton985eba52003-02-05 23:13:00 +00002595 PCALL(PCALL_GENERATOR);
2596
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002597 /* Create a new generator that owns the ready to run frame
2598 * and return that as the value. */
Tim Peters5ca576e2001-06-18 22:08:13 +00002599 return gen_new(f);
2600 }
2601
2602 retval = eval_frame(f);
2603
2604 fail: /* Jump here from prelude on failure */
2605
Tim Petersb13680b2001-11-27 23:29:29 +00002606 /* decref'ing the frame can cause __del__ methods to get invoked,
2607 which can call back into Python. While we're done with the
2608 current Python frame (f), the associated C stack is still in use,
2609 so recursion_depth must be boosted for the duration.
2610 */
2611 assert(tstate != NULL);
2612 ++tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002613 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00002614 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002615 return retval;
2616}
2617
2618
Guido van Rossuma027efa1997-05-05 20:56:21 +00002619static void
Guido van Rossumac7be682001-01-17 15:42:30 +00002620set_exc_info(PyThreadState *tstate,
2621 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002622{
2623 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002624 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00002625
Guido van Rossuma027efa1997-05-05 20:56:21 +00002626 frame = tstate->frame;
2627 if (frame->f_exc_type == NULL) {
2628 /* This frame didn't catch an exception before */
2629 /* Save previous exception of this thread in this frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002630 if (tstate->exc_type == NULL) {
2631 Py_INCREF(Py_None);
2632 tstate->exc_type = Py_None;
2633 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002634 tmp_type = frame->f_exc_type;
2635 tmp_value = frame->f_exc_value;
2636 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002637 Py_XINCREF(tstate->exc_type);
2638 Py_XINCREF(tstate->exc_value);
2639 Py_XINCREF(tstate->exc_traceback);
2640 frame->f_exc_type = tstate->exc_type;
2641 frame->f_exc_value = tstate->exc_value;
2642 frame->f_exc_traceback = tstate->exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002643 Py_XDECREF(tmp_type);
2644 Py_XDECREF(tmp_value);
2645 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002646 }
2647 /* Set new exception for this thread */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002648 tmp_type = tstate->exc_type;
2649 tmp_value = tstate->exc_value;
2650 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002651 Py_XINCREF(type);
2652 Py_XINCREF(value);
2653 Py_XINCREF(tb);
2654 tstate->exc_type = type;
2655 tstate->exc_value = value;
2656 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002657 Py_XDECREF(tmp_type);
2658 Py_XDECREF(tmp_value);
2659 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002660 /* For b/w compatibility */
2661 PySys_SetObject("exc_type", type);
2662 PySys_SetObject("exc_value", value);
2663 PySys_SetObject("exc_traceback", tb);
2664}
2665
2666static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002667reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002668{
2669 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002670 PyObject *tmp_type, *tmp_value, *tmp_tb;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002671 frame = tstate->frame;
2672 if (frame->f_exc_type != NULL) {
2673 /* This frame caught an exception */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002674 tmp_type = tstate->exc_type;
2675 tmp_value = tstate->exc_value;
2676 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002677 Py_XINCREF(frame->f_exc_type);
2678 Py_XINCREF(frame->f_exc_value);
2679 Py_XINCREF(frame->f_exc_traceback);
2680 tstate->exc_type = frame->f_exc_type;
2681 tstate->exc_value = frame->f_exc_value;
2682 tstate->exc_traceback = frame->f_exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002683 Py_XDECREF(tmp_type);
2684 Py_XDECREF(tmp_value);
2685 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002686 /* For b/w compatibility */
2687 PySys_SetObject("exc_type", frame->f_exc_type);
2688 PySys_SetObject("exc_value", frame->f_exc_value);
2689 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
2690 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002691 tmp_type = frame->f_exc_type;
2692 tmp_value = frame->f_exc_value;
2693 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002694 frame->f_exc_type = NULL;
2695 frame->f_exc_value = NULL;
2696 frame->f_exc_traceback = NULL;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002697 Py_XDECREF(tmp_type);
2698 Py_XDECREF(tmp_value);
2699 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002700}
2701
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002702/* Logic for the raise statement (too complicated for inlining).
2703 This *consumes* a reference count to each of its arguments. */
Guido van Rossum1aa14831997-01-21 05:34:20 +00002704static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002705do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002706{
Guido van Rossumd295f121998-04-09 21:39:57 +00002707 if (type == NULL) {
2708 /* Reraise */
2709 PyThreadState *tstate = PyThreadState_Get();
2710 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
2711 value = tstate->exc_value;
2712 tb = tstate->exc_traceback;
2713 Py_XINCREF(type);
2714 Py_XINCREF(value);
2715 Py_XINCREF(tb);
2716 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002717
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002718 /* We support the following forms of raise:
2719 raise <class>, <classinstance>
2720 raise <class>, <argument tuple>
2721 raise <class>, None
2722 raise <class>, <argument>
2723 raise <classinstance>, None
2724 raise <string>, <object>
2725 raise <string>, None
2726
2727 An omitted second argument is the same as None.
2728
2729 In addition, raise <tuple>, <anything> is the same as
2730 raising the tuple's first item (and it better have one!);
2731 this rule is applied recursively.
2732
2733 Finally, an optional third argument can be supplied, which
2734 gives the traceback to be substituted (useful when
2735 re-raising an exception after examining it). */
2736
2737 /* First, check the traceback argument, replacing None with
2738 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002739 if (tb == Py_None) {
2740 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002741 tb = NULL;
2742 }
2743 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002744 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002745 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002746 goto raise_error;
2747 }
2748
2749 /* Next, replace a missing value with None */
2750 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002751 value = Py_None;
2752 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002753 }
2754
2755 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00002756 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
2757 PyObject *tmp = type;
2758 type = PyTuple_GET_ITEM(type, 0);
2759 Py_INCREF(type);
2760 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002761 }
2762
Tim Petersafb2c802002-04-18 18:06:20 +00002763 if (PyString_CheckExact(type))
2764 /* Raising builtin string is deprecated but still allowed --
2765 * do nothing. Raising an instance of a new-style str
2766 * subclass is right out. */
Neal Norwitz37aa0662003-01-10 15:31:15 +00002767 PyErr_Warn(PyExc_PendingDeprecationWarning,
2768 "raising a string exception is deprecated");
Barry Warsaw4249f541997-08-22 21:26:19 +00002769
2770 else if (PyClass_Check(type))
2771 PyErr_NormalizeException(&type, &value, &tb);
2772
Guido van Rossumb209a111997-04-29 18:18:01 +00002773 else if (PyInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002774 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002775 if (value != Py_None) {
2776 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002777 "instance exception may not have a separate value");
2778 goto raise_error;
2779 }
2780 else {
2781 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00002782 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002783 value = type;
Guido van Rossumb209a111997-04-29 18:18:01 +00002784 type = (PyObject*) ((PyInstanceObject*)type)->in_class;
2785 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002786 }
2787 }
2788 else {
2789 /* Not something you can raise. You get an exception
2790 anyway, just not what you specified :-) */
Jeremy Hylton960d9482001-04-27 02:25:33 +00002791 PyErr_Format(PyExc_TypeError,
Neal Norwitz37aa0662003-01-10 15:31:15 +00002792 "exceptions must be classes, instances, or "
2793 "strings (deprecated), not %s",
2794 type->ob_type->tp_name);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002795 goto raise_error;
2796 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002797 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002798 if (tb == NULL)
2799 return WHY_EXCEPTION;
2800 else
2801 return WHY_RERAISE;
2802 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00002803 Py_XDECREF(value);
2804 Py_XDECREF(type);
2805 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002806 return WHY_EXCEPTION;
2807}
2808
Tim Petersd6d010b2001-06-21 02:49:55 +00002809/* Iterate v argcnt times and store the results on the stack (via decreasing
2810 sp). Return 1 for success, 0 if error. */
2811
Barry Warsawe42b18f1997-08-25 22:13:04 +00002812static int
Tim Petersd6d010b2001-06-21 02:49:55 +00002813unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00002814{
Tim Petersd6d010b2001-06-21 02:49:55 +00002815 int i = 0;
2816 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00002817 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00002818
Tim Petersd6d010b2001-06-21 02:49:55 +00002819 assert(v != NULL);
2820
2821 it = PyObject_GetIter(v);
2822 if (it == NULL)
2823 goto Error;
2824
2825 for (; i < argcnt; i++) {
2826 w = PyIter_Next(it);
2827 if (w == NULL) {
2828 /* Iterator done, via error or exhaustion. */
2829 if (!PyErr_Occurred()) {
2830 PyErr_Format(PyExc_ValueError,
2831 "need more than %d value%s to unpack",
2832 i, i == 1 ? "" : "s");
2833 }
2834 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00002835 }
2836 *--sp = w;
2837 }
Tim Petersd6d010b2001-06-21 02:49:55 +00002838
2839 /* We better have exhausted the iterator now. */
2840 w = PyIter_Next(it);
2841 if (w == NULL) {
2842 if (PyErr_Occurred())
2843 goto Error;
2844 Py_DECREF(it);
2845 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00002846 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00002847 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00002848 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00002849 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00002850Error:
Barry Warsaw91010551997-08-25 22:30:51 +00002851 for (; i > 0; i--, sp++)
2852 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00002853 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00002854 return 0;
2855}
2856
2857
Guido van Rossum96a42c81992-01-12 02:29:51 +00002858#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00002859static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002860prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002861{
Guido van Rossum3f5da241990-12-20 15:06:42 +00002862 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00002863 if (PyObject_Print(v, stdout, 0) != 0)
2864 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00002865 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00002866 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002867}
Guido van Rossum3f5da241990-12-20 15:06:42 +00002868#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002869
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002870static void
Fred Drake5755ce62001-06-27 19:19:46 +00002871call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002872{
Guido van Rossumb209a111997-04-29 18:18:01 +00002873 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002874 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00002875 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00002876 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002877 value = Py_None;
2878 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00002879 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002880 arg = Py_BuildValue("(OOO)", type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002881 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002882 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002883 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002884 }
Fred Drake5755ce62001-06-27 19:19:46 +00002885 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002886 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002887 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00002888 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002889 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00002890 Py_XDECREF(type);
2891 Py_XDECREF(value);
2892 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002893 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002894}
2895
Fred Drake4ec5d562001-10-04 19:26:43 +00002896static void
2897call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
2898 int what)
2899{
2900 PyObject *type, *value, *traceback;
2901 int err;
2902 PyErr_Fetch(&type, &value, &traceback);
2903 err = call_trace(func, obj, frame, what, NULL);
2904 if (err == 0)
2905 PyErr_Restore(type, value, traceback);
2906 else {
2907 Py_XDECREF(type);
2908 Py_XDECREF(value);
2909 Py_XDECREF(traceback);
2910 }
2911}
2912
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002913static int
Fred Drake5755ce62001-06-27 19:19:46 +00002914call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
2915 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002916{
Fred Drake5755ce62001-06-27 19:19:46 +00002917 register PyThreadState *tstate = frame->f_tstate;
2918 int result;
2919 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002920 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002921 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00002922 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00002923 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00002924 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
2925 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00002926 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00002927 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002928}
2929
Michael W. Hudson006c7522002-11-08 13:08:46 +00002930static int
Michael W. Hudson019a78e2002-11-08 12:53:11 +00002931maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002932 PyFrameObject *frame, int *instr_lb, int *instr_ub)
2933{
2934 /* The theory of SET_LINENO-less tracing.
2935
2936 In a nutshell, we use the co_lnotab field of the code object
2937 to tell when execution has moved onto a different line.
2938
2939 As mentioned above, the basic idea is so set things up so
2940 that
2941
2942 *instr_lb <= frame->f_lasti < *instr_ub
2943
2944 is true so long as execution does not change lines.
2945
2946 This is all fairly simple. Digging the information out of
2947 co_lnotab takes some work, but is conceptually clear.
2948
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00002949 Somewhat harder to explain is why we don't *always* call the
2950 line trace function when the above test fails.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002951
2952 Consider this code:
2953
2954 1: def f(a):
2955 2: if a:
2956 3: print 1
2957 4: else:
2958 5: print 2
2959
2960 which compiles to this:
2961
2962 2 0 LOAD_FAST 0 (a)
2963 3 JUMP_IF_FALSE 9 (to 15)
2964 6 POP_TOP
2965
2966 3 7 LOAD_CONST 1 (1)
2967 10 PRINT_ITEM
2968 11 PRINT_NEWLINE
2969 12 JUMP_FORWARD 6 (to 21)
2970 >> 15 POP_TOP
2971
2972 5 16 LOAD_CONST 2 (2)
2973 19 PRINT_ITEM
2974 20 PRINT_NEWLINE
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00002975 >> 21 LOAD_CONST 0 (None)
2976 24 RETURN_VALUE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002977
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00002978 If 'a' is false, execution will jump to instruction at offset
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002979 15 and the co_lnotab will claim that execution has moved to
2980 line 3. This is at best misleading. In this case we could
2981 associate the POP_TOP with line 4, but that doesn't make
2982 sense in all cases (I think).
2983
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00002984 What we do is only call the line trace function if the co_lnotab
2985 indicates we have jumped to the *start* of a line, i.e. if the
2986 current instruction offset matches the offset given for the
2987 start of a line by the co_lnotab.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002988
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00002989 This also takes care of the situation where 'a' is true.
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00002990 Execution will jump from instruction offset 12 to offset 21.
2991 Then the co_lnotab would imply that execution has moved to line
2992 5, which is again misleading.
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00002993
2994 Why do we set f_lineno when tracing? Well, consider the code
2995 above when 'a' is true. If stepping through this with 'n' in
2996 pdb, you would stop at line 1 with a "call" type event, then
2997 line events on lines 2 and 3, then a "return" type event -- but
2998 you would be shown line 5 during this event. This is a change
2999 from the behaviour in 2.2 and before, and I've found it
3000 confusing in practice. By setting and using f_lineno when
3001 tracing, one can report a line number different from that
3002 suggested by f_lasti on this one occasion where it's desirable.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003003 */
3004
Michael W. Hudson006c7522002-11-08 13:08:46 +00003005 int result = 0;
3006
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003007 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003008 PyCodeObject* co = frame->f_code;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003009 int size, addr, line;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003010 unsigned char* p;
3011
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003012 size = PyString_GET_SIZE(co->co_lnotab) / 2;
3013 p = (unsigned char*)PyString_AS_STRING(co->co_lnotab);
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003014
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003015 addr = 0;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003016 line = co->co_firstlineno;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003017
3018 /* possible optimization: if f->f_lasti == instr_ub
3019 (likely to be a common case) then we already know
3020 instr_lb -- if we stored the matching value of p
3021 somwhere we could skip the first while loop. */
3022
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003023 /* see comments in compile.c for the description of
3024 co_lnotab. A point to remember: increments to p
3025 should come in pairs -- although we don't care about
3026 the line increments here, treating them as byte
3027 increments gets confusing, to say the least. */
3028
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003029 while (size > 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003030 if (addr + *p > frame->f_lasti)
3031 break;
3032 addr += *p++;
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003033 if (*p) *instr_lb = addr;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003034 line += *p++;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003035 --size;
3036 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003037
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003038 if (addr == frame->f_lasti) {
3039 frame->f_lineno = line;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003040 result = call_trace(func, obj, frame,
3041 PyTrace_LINE, Py_None);
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003042 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003043
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003044 if (size > 0) {
3045 while (--size >= 0) {
3046 addr += *p++;
3047 if (*p++)
3048 break;
3049 }
3050 *instr_ub = addr;
3051 }
3052 else {
3053 *instr_ub = INT_MAX;
3054 }
3055 }
Michael W. Hudson006c7522002-11-08 13:08:46 +00003056
3057 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003058}
3059
Fred Drake5755ce62001-06-27 19:19:46 +00003060void
3061PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003062{
Fred Drake5755ce62001-06-27 19:19:46 +00003063 PyThreadState *tstate = PyThreadState_Get();
3064 PyObject *temp = tstate->c_profileobj;
3065 Py_XINCREF(arg);
3066 tstate->c_profilefunc = NULL;
3067 tstate->c_profileobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003068 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003069 Py_XDECREF(temp);
3070 tstate->c_profilefunc = func;
3071 tstate->c_profileobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003072 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003073}
3074
3075void
3076PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3077{
3078 PyThreadState *tstate = PyThreadState_Get();
3079 PyObject *temp = tstate->c_traceobj;
3080 Py_XINCREF(arg);
3081 tstate->c_tracefunc = NULL;
3082 tstate->c_traceobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003083 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003084 Py_XDECREF(temp);
3085 tstate->c_tracefunc = func;
3086 tstate->c_traceobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003087 tstate->use_tracing = ((func != NULL)
3088 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003089}
3090
Guido van Rossumb209a111997-04-29 18:18:01 +00003091PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003092PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003093{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003094 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003095 if (current_frame == NULL)
Michael W. Hudson019a78e2002-11-08 12:53:11 +00003096 return PyThreadState_Get()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003097 else
3098 return current_frame->f_builtins;
3099}
3100
Guido van Rossumb209a111997-04-29 18:18:01 +00003101PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003102PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003103{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003104 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003105 if (current_frame == NULL)
3106 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003107 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003108 return current_frame->f_locals;
3109}
3110
Guido van Rossumb209a111997-04-29 18:18:01 +00003111PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003112PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003113{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003114 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003115 if (current_frame == NULL)
3116 return NULL;
3117 else
3118 return current_frame->f_globals;
3119}
3120
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003121PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003122PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003123{
Michael W. Hudson019a78e2002-11-08 12:53:11 +00003124 PyThreadState *tstate = PyThreadState_Get();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003125 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003126}
3127
Guido van Rossum6135a871995-01-09 17:53:26 +00003128int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003129PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003130{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003131 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003132 return current_frame == NULL ? 0 : current_frame->f_restricted;
3133}
3134
Guido van Rossumbe270261997-05-22 22:26:18 +00003135int
Tim Peters5ba58662001-07-16 02:29:45 +00003136PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003137{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003138 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003139 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003140
3141 if (current_frame != NULL) {
3142 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003143 const int compilerflags = codeflags & PyCF_MASK;
3144 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003145 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003146 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003147 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003148#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003149 if (codeflags & CO_GENERATOR_ALLOWED) {
3150 result = 1;
3151 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3152 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003153#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003154 }
3155 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003156}
3157
3158int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003159Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003160{
Guido van Rossumb209a111997-04-29 18:18:01 +00003161 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003162 if (f == NULL)
3163 return 0;
3164 if (!PyFile_SoftSpace(f, 0))
3165 return 0;
3166 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003167}
3168
Guido van Rossum3f5da241990-12-20 15:06:42 +00003169
Guido van Rossum681d79a1995-07-18 14:51:37 +00003170/* External interface to call any callable object.
3171 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003172
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003173#undef PyEval_CallObject
3174/* for backward compatibility: export this interface */
3175
Guido van Rossumb209a111997-04-29 18:18:01 +00003176PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003177PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003178{
Guido van Rossumb209a111997-04-29 18:18:01 +00003179 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003180}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003181#define PyEval_CallObject(func,arg) \
3182 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003183
Guido van Rossumb209a111997-04-29 18:18:01 +00003184PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003185PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003186{
Jeremy Hylton52820442001-01-03 23:52:36 +00003187 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003188
3189 if (arg == NULL)
Guido van Rossumb209a111997-04-29 18:18:01 +00003190 arg = PyTuple_New(0);
3191 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003192 PyErr_SetString(PyExc_TypeError,
3193 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003194 return NULL;
3195 }
3196 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003197 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003198
Guido van Rossumb209a111997-04-29 18:18:01 +00003199 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003200 PyErr_SetString(PyExc_TypeError,
3201 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003202 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003203 return NULL;
3204 }
3205
Tim Peters6d6c1a32001-08-02 04:15:00 +00003206 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003207 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003208 return result;
3209}
3210
Tim Peters6d6c1a32001-08-02 04:15:00 +00003211char *
3212PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003213{
3214 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003215 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003216 else if (PyFunction_Check(func))
3217 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3218 else if (PyCFunction_Check(func))
3219 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3220 else if (PyClass_Check(func))
3221 return PyString_AsString(((PyClassObject*)func)->cl_name);
3222 else if (PyInstance_Check(func)) {
3223 return PyString_AsString(
3224 ((PyInstanceObject*)func)->in_class->cl_name);
3225 } else {
3226 return func->ob_type->tp_name;
3227 }
3228}
3229
Tim Peters6d6c1a32001-08-02 04:15:00 +00003230char *
3231PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003232{
3233 if (PyMethod_Check(func))
3234 return "()";
3235 else if (PyFunction_Check(func))
3236 return "()";
3237 else if (PyCFunction_Check(func))
3238 return "()";
3239 else if (PyClass_Check(func))
3240 return " constructor";
3241 else if (PyInstance_Check(func)) {
3242 return " instance";
3243 } else {
3244 return " object";
3245 }
3246}
3247
Jeremy Hylton52820442001-01-03 23:52:36 +00003248#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
3249
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003250static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003251err_args(PyObject *func, int flags, int nargs)
3252{
3253 if (flags & METH_NOARGS)
3254 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003255 "%.200s() takes no arguments (%d given)",
Jeremy Hylton192690e2002-08-16 18:36:11 +00003256 ((PyCFunctionObject *)func)->m_ml->ml_name,
3257 nargs);
3258 else
3259 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003260 "%.200s() takes exactly one argument (%d given)",
Jeremy Hylton192690e2002-08-16 18:36:11 +00003261 ((PyCFunctionObject *)func)->m_ml->ml_name,
3262 nargs);
3263}
3264
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003265static PyObject *
3266call_function(PyObject ***pp_stack, int oparg)
3267{
3268 int na = oparg & 0xff;
3269 int nk = (oparg>>8) & 0xff;
3270 int n = na + 2 * nk;
3271 PyObject **pfunc = (*pp_stack) - n - 1;
3272 PyObject *func = *pfunc;
3273 PyObject *x, *w;
3274
Jeremy Hylton985eba52003-02-05 23:13:00 +00003275 /* Always dispatch PyCFunction first, because these are
3276 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003277 */
3278 if (PyCFunction_Check(func) && nk == 0) {
3279 int flags = PyCFunction_GET_FLAGS(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003280 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003281 if (flags & (METH_NOARGS | METH_O)) {
3282 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3283 PyObject *self = PyCFunction_GET_SELF(func);
3284 if (flags & METH_NOARGS && na == 0)
3285 x = (*meth)(self, NULL);
3286 else if (flags & METH_O && na == 1) {
3287 PyObject *arg = EXT_POP(*pp_stack);
3288 x = (*meth)(self, arg);
3289 Py_DECREF(arg);
3290 }
3291 else {
3292 err_args(func, flags, na);
3293 x = NULL;
3294 }
3295 }
3296 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003297 PyObject *callargs;
3298 callargs = load_args(pp_stack, na);
3299 x = PyCFunction_Call(func, callargs, NULL);
3300 Py_XDECREF(callargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003301 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003302 } else {
3303 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3304 /* optimize access to bound methods */
3305 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003306 PCALL(PCALL_METHOD);
3307 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003308 Py_INCREF(self);
3309 func = PyMethod_GET_FUNCTION(func);
3310 Py_INCREF(func);
3311 Py_DECREF(*pfunc);
3312 *pfunc = self;
3313 na++;
3314 n++;
3315 } else
3316 Py_INCREF(func);
3317 if (PyFunction_Check(func))
3318 x = fast_function(func, pp_stack, n, na, nk);
3319 else
3320 x = do_call(func, pp_stack, na, nk);
3321 Py_DECREF(func);
3322 }
3323
Jeremy Hylton985eba52003-02-05 23:13:00 +00003324 /* What does this do? */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003325 while ((*pp_stack) > pfunc) {
3326 w = EXT_POP(*pp_stack);
3327 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003328 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003329 }
3330 return x;
3331}
3332
Jeremy Hylton192690e2002-08-16 18:36:11 +00003333/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003334 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003335 For the simplest case -- a function that takes only positional
3336 arguments and is called with only positional arguments -- it
3337 inlines the most primitive frame setup code from
3338 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3339 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003340*/
3341
3342static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003343fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003344{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003345 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003346 PyObject *globals = PyFunction_GET_GLOBALS(func);
3347 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3348 PyObject **d = NULL;
3349 int nd = 0;
3350
Jeremy Hylton985eba52003-02-05 23:13:00 +00003351 PCALL(PCALL_FUNCTION);
3352 PCALL(PCALL_FAST_FUNCTION);
3353 if (argdefs == NULL && co->co_argcount == n &&
3354 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3355 PyFrameObject *f;
3356 PyObject *retval = NULL;
3357 PyThreadState *tstate = PyThreadState_GET();
3358 PyObject **fastlocals, **stack;
3359 int i;
3360
3361 PCALL(PCALL_FASTER_FUNCTION);
3362 assert(globals != NULL);
3363 /* XXX Perhaps we should create a specialized
3364 PyFrame_New() that doesn't take locals, but does
3365 take builtins without sanity checking them.
3366 */
3367 f = PyFrame_New(tstate, co, globals, NULL);
3368 if (f == NULL)
3369 return NULL;
3370
3371 fastlocals = f->f_localsplus;
3372 stack = (*pp_stack) - n;
3373
3374 for (i = 0; i < n; i++) {
3375 Py_INCREF(*stack);
3376 fastlocals[i] = *stack++;
3377 }
3378 retval = eval_frame(f);
3379 assert(tstate != NULL);
3380 ++tstate->recursion_depth;
3381 Py_DECREF(f);
3382 --tstate->recursion_depth;
3383 return retval;
3384 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003385 if (argdefs != NULL) {
3386 d = &PyTuple_GET_ITEM(argdefs, 0);
3387 nd = ((PyTupleObject *)argdefs)->ob_size;
3388 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003389 return PyEval_EvalCodeEx(co, globals,
3390 (PyObject *)NULL, (*pp_stack)-n, na,
3391 (*pp_stack)-2*nk, nk, d, nd,
3392 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003393}
3394
3395static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003396update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3397 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003398{
3399 PyObject *kwdict = NULL;
3400 if (orig_kwdict == NULL)
3401 kwdict = PyDict_New();
3402 else {
3403 kwdict = PyDict_Copy(orig_kwdict);
3404 Py_DECREF(orig_kwdict);
3405 }
3406 if (kwdict == NULL)
3407 return NULL;
3408 while (--nk >= 0) {
3409 int err;
3410 PyObject *value = EXT_POP(*pp_stack);
3411 PyObject *key = EXT_POP(*pp_stack);
3412 if (PyDict_GetItem(kwdict, key) != NULL) {
Guido van Rossumac7be682001-01-17 15:42:30 +00003413 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +00003414 "%.200s%s got multiple values "
Jeremy Hylton512a2372001-04-11 13:52:29 +00003415 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003416 PyEval_GetFuncName(func),
3417 PyEval_GetFuncDesc(func),
Jeremy Hylton512a2372001-04-11 13:52:29 +00003418 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003419 Py_DECREF(key);
3420 Py_DECREF(value);
3421 Py_DECREF(kwdict);
3422 return NULL;
3423 }
3424 err = PyDict_SetItem(kwdict, key, value);
3425 Py_DECREF(key);
3426 Py_DECREF(value);
3427 if (err) {
3428 Py_DECREF(kwdict);
3429 return NULL;
3430 }
3431 }
3432 return kwdict;
3433}
3434
3435static PyObject *
3436update_star_args(int nstack, int nstar, PyObject *stararg,
3437 PyObject ***pp_stack)
3438{
3439 PyObject *callargs, *w;
3440
3441 callargs = PyTuple_New(nstack + nstar);
3442 if (callargs == NULL) {
3443 return NULL;
3444 }
3445 if (nstar) {
3446 int i;
3447 for (i = 0; i < nstar; i++) {
3448 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3449 Py_INCREF(a);
3450 PyTuple_SET_ITEM(callargs, nstack + i, a);
3451 }
3452 }
3453 while (--nstack >= 0) {
3454 w = EXT_POP(*pp_stack);
3455 PyTuple_SET_ITEM(callargs, nstack, w);
3456 }
3457 return callargs;
3458}
3459
3460static PyObject *
3461load_args(PyObject ***pp_stack, int na)
3462{
3463 PyObject *args = PyTuple_New(na);
3464 PyObject *w;
3465
3466 if (args == NULL)
3467 return NULL;
3468 while (--na >= 0) {
3469 w = EXT_POP(*pp_stack);
3470 PyTuple_SET_ITEM(args, na, w);
3471 }
3472 return args;
3473}
3474
3475static PyObject *
3476do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3477{
3478 PyObject *callargs = NULL;
3479 PyObject *kwdict = NULL;
3480 PyObject *result = NULL;
3481
3482 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003483 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003484 if (kwdict == NULL)
3485 goto call_fail;
3486 }
3487 callargs = load_args(pp_stack, na);
3488 if (callargs == NULL)
3489 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003490#ifdef CALL_PROFILE
3491 /* At this point, we have to look at the type of func to
3492 update the call stats properly. Do it here so as to avoid
3493 exposing the call stats machinery outside ceval.c
3494 */
3495 if (PyFunction_Check(func))
3496 PCALL(PCALL_FUNCTION);
3497 else if (PyMethod_Check(func))
3498 PCALL(PCALL_METHOD);
3499 else if (PyType_Check(func))
3500 PCALL(PCALL_TYPE);
3501 else
3502 PCALL(PCALL_OTHER);
3503#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003504 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003505 call_fail:
3506 Py_XDECREF(callargs);
3507 Py_XDECREF(kwdict);
3508 return result;
3509}
3510
3511static PyObject *
3512ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3513{
3514 int nstar = 0;
3515 PyObject *callargs = NULL;
3516 PyObject *stararg = NULL;
3517 PyObject *kwdict = NULL;
3518 PyObject *result = NULL;
3519
3520 if (flags & CALL_FLAG_KW) {
3521 kwdict = EXT_POP(*pp_stack);
3522 if (!(kwdict && PyDict_Check(kwdict))) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003523 PyErr_Format(PyExc_TypeError,
Jeremy Hylton512a2372001-04-11 13:52:29 +00003524 "%s%s argument after ** "
3525 "must be a dictionary",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003526 PyEval_GetFuncName(func),
3527 PyEval_GetFuncDesc(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003528 goto ext_call_fail;
3529 }
3530 }
3531 if (flags & CALL_FLAG_VAR) {
3532 stararg = EXT_POP(*pp_stack);
3533 if (!PyTuple_Check(stararg)) {
3534 PyObject *t = NULL;
3535 t = PySequence_Tuple(stararg);
3536 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003537 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3538 PyErr_Format(PyExc_TypeError,
3539 "%s%s argument after * "
3540 "must be a sequence",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003541 PyEval_GetFuncName(func),
3542 PyEval_GetFuncDesc(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003543 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003544 goto ext_call_fail;
3545 }
3546 Py_DECREF(stararg);
3547 stararg = t;
3548 }
3549 nstar = PyTuple_GET_SIZE(stararg);
3550 }
3551 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003552 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003553 if (kwdict == NULL)
3554 goto ext_call_fail;
3555 }
3556 callargs = update_star_args(na, nstar, stararg, pp_stack);
3557 if (callargs == NULL)
3558 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003559#ifdef CALL_PROFILE
3560 /* At this point, we have to look at the type of func to
3561 update the call stats properly. Do it here so as to avoid
3562 exposing the call stats machinery outside ceval.c
3563 */
3564 if (PyFunction_Check(func))
3565 PCALL(PCALL_FUNCTION);
3566 else if (PyMethod_Check(func))
3567 PCALL(PCALL_METHOD);
3568 else if (PyType_Check(func))
3569 PCALL(PCALL_TYPE);
3570 else
3571 PCALL(PCALL_OTHER);
3572#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003573 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003574 ext_call_fail:
3575 Py_XDECREF(callargs);
3576 Py_XDECREF(kwdict);
3577 Py_XDECREF(stararg);
3578 return result;
3579}
3580
Guido van Rossum3b9c6671996-07-30 18:40:29 +00003581#define SLICE_ERROR_MSG \
3582 "standard sequence type does not support step size other than one"
3583
Tim Peterscb479e72001-12-16 19:11:44 +00003584/* Extract a slice index from a PyInt or PyLong, and store in *pi.
3585 Silently reduce values larger than INT_MAX to INT_MAX, and silently
3586 boost values less than -INT_MAX to 0. Return 0 on error, 1 on success.
3587*/
Tim Petersb5196382001-12-16 19:44:20 +00003588/* Note: If v is NULL, return success without storing into *pi. This
3589 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3590 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00003591*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00003592int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003593_PyEval_SliceIndex(PyObject *v, int *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003594{
Tim Petersb5196382001-12-16 19:44:20 +00003595 if (v != NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003596 long x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003597 if (PyInt_Check(v)) {
3598 x = PyInt_AsLong(v);
3599 } else if (PyLong_Check(v)) {
3600 x = PyLong_AsLong(v);
3601 if (x==-1 && PyErr_Occurred()) {
3602 PyObject *long_zero;
Guido van Rossumac7be682001-01-17 15:42:30 +00003603 int cmp;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003604
Guido van Rossumac7be682001-01-17 15:42:30 +00003605 if (!PyErr_ExceptionMatches(
3606 PyExc_OverflowError)) {
3607 /* It's not an overflow error, so just
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003608 signal an error */
Guido van Rossum20c6add2000-05-08 14:06:50 +00003609 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003610 }
3611
Guido van Rossumac7be682001-01-17 15:42:30 +00003612 /* Clear the OverflowError */
3613 PyErr_Clear();
3614
3615 /* It's an overflow error, so we need to
3616 check the sign of the long integer,
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003617 set the value to INT_MAX or -INT_MAX,
3618 and clear the error. */
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003619
3620 /* Create a long integer with a value of 0 */
Guido van Rossumac7be682001-01-17 15:42:30 +00003621 long_zero = PyLong_FromLong(0L);
Tim Peterscb479e72001-12-16 19:11:44 +00003622 if (long_zero == NULL)
3623 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003624
3625 /* Check sign */
Guido van Rossumac7be682001-01-17 15:42:30 +00003626 cmp = PyObject_RichCompareBool(v, long_zero,
3627 Py_GT);
3628 Py_DECREF(long_zero);
3629 if (cmp < 0)
3630 return 0;
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003631 else if (cmp)
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003632 x = INT_MAX;
3633 else
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003634 x = -INT_MAX;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003635 }
3636 } else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003637 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003638 "slice indices must be integers");
Guido van Rossum20c6add2000-05-08 14:06:50 +00003639 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003640 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003641 /* Truncate -- very long indices are truncated anyway */
3642 if (x > INT_MAX)
3643 x = INT_MAX;
3644 else if (x < -INT_MAX)
Michael W. Hudsoncbd6fb92002-11-06 15:17:32 +00003645 x = -INT_MAX;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003646 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003647 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00003648 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003649}
3650
Guido van Rossum50d756e2001-08-18 17:43:36 +00003651#undef ISINT
3652#define ISINT(x) ((x) == NULL || PyInt_Check(x) || PyLong_Check(x))
3653
Guido van Rossumb209a111997-04-29 18:18:01 +00003654static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003655apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003656{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003657 PyTypeObject *tp = u->ob_type;
3658 PySequenceMethods *sq = tp->tp_as_sequence;
3659
3660 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3661 int ilow = 0, ihigh = INT_MAX;
3662 if (!_PyEval_SliceIndex(v, &ilow))
3663 return NULL;
3664 if (!_PyEval_SliceIndex(w, &ihigh))
3665 return NULL;
3666 return PySequence_GetSlice(u, ilow, ihigh);
3667 }
3668 else {
3669 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00003670 if (slice != NULL) {
3671 PyObject *res = PyObject_GetItem(u, slice);
3672 Py_DECREF(slice);
3673 return res;
3674 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00003675 else
3676 return NULL;
3677 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003678}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003679
3680static int
Guido van Rossumac7be682001-01-17 15:42:30 +00003681assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
3682 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003683{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003684 PyTypeObject *tp = u->ob_type;
3685 PySequenceMethods *sq = tp->tp_as_sequence;
3686
3687 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3688 int ilow = 0, ihigh = INT_MAX;
3689 if (!_PyEval_SliceIndex(v, &ilow))
3690 return -1;
3691 if (!_PyEval_SliceIndex(w, &ihigh))
3692 return -1;
3693 if (x == NULL)
3694 return PySequence_DelSlice(u, ilow, ihigh);
3695 else
3696 return PySequence_SetSlice(u, ilow, ihigh, x);
3697 }
3698 else {
3699 PyObject *slice = PySlice_New(v, w, NULL);
3700 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00003701 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003702 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00003703 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00003704 else
Guido van Rossum354797c2001-12-03 19:45:06 +00003705 res = PyObject_DelItem(u, slice);
3706 Py_DECREF(slice);
3707 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003708 }
3709 else
3710 return -1;
3711 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003712}
3713
Guido van Rossumb209a111997-04-29 18:18:01 +00003714static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003715cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003716{
Guido van Rossumac7be682001-01-17 15:42:30 +00003717 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003718 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00003719 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003720 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003721 break;
3722 case PyCmp_IS_NOT:
3723 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003724 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003725 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003726 res = PySequence_Contains(w, v);
3727 if (res < 0)
3728 return NULL;
3729 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003730 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00003731 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003732 if (res < 0)
3733 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003734 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003735 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003736 case PyCmp_EXC_MATCH:
Barry Warsaw4249f541997-08-22 21:26:19 +00003737 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003738 break;
3739 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00003740 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003741 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003742 v = res ? Py_True : Py_False;
3743 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003744 return v;
3745}
3746
Thomas Wouters52152252000-08-17 22:55:00 +00003747static PyObject *
3748import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003749{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003750 PyObject *x;
3751
3752 x = PyObject_GetAttr(v, name);
3753 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00003754 PyErr_Format(PyExc_ImportError,
3755 "cannot import name %.230s",
3756 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003757 }
Thomas Wouters52152252000-08-17 22:55:00 +00003758 return x;
3759}
Guido van Rossumac7be682001-01-17 15:42:30 +00003760
Thomas Wouters52152252000-08-17 22:55:00 +00003761static int
3762import_all_from(PyObject *locals, PyObject *v)
3763{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003764 PyObject *all = PyObject_GetAttrString(v, "__all__");
3765 PyObject *dict, *name, *value;
3766 int skip_leading_underscores = 0;
3767 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00003768
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003769 if (all == NULL) {
3770 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3771 return -1; /* Unexpected error */
3772 PyErr_Clear();
3773 dict = PyObject_GetAttrString(v, "__dict__");
3774 if (dict == NULL) {
3775 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3776 return -1;
3777 PyErr_SetString(PyExc_ImportError,
3778 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00003779 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003780 }
3781 all = PyMapping_Keys(dict);
3782 Py_DECREF(dict);
3783 if (all == NULL)
3784 return -1;
3785 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00003786 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003787
3788 for (pos = 0, err = 0; ; pos++) {
3789 name = PySequence_GetItem(all, pos);
3790 if (name == NULL) {
3791 if (!PyErr_ExceptionMatches(PyExc_IndexError))
3792 err = -1;
3793 else
3794 PyErr_Clear();
3795 break;
3796 }
3797 if (skip_leading_underscores &&
3798 PyString_Check(name) &&
3799 PyString_AS_STRING(name)[0] == '_')
3800 {
3801 Py_DECREF(name);
3802 continue;
3803 }
3804 value = PyObject_GetAttr(v, name);
3805 if (value == NULL)
3806 err = -1;
3807 else
3808 err = PyDict_SetItem(locals, name, value);
3809 Py_DECREF(name);
3810 Py_XDECREF(value);
3811 if (err != 0)
3812 break;
3813 }
3814 Py_DECREF(all);
3815 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00003816}
3817
Guido van Rossumb209a111997-04-29 18:18:01 +00003818static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003819build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003820{
Guido van Rossum7851eea2001-09-12 19:19:18 +00003821 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003822
3823 if (PyDict_Check(methods))
3824 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00003825 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00003826 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00003827 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
3828 base = PyTuple_GET_ITEM(bases, 0);
3829 metaclass = PyObject_GetAttrString(base, "__class__");
3830 if (metaclass == NULL) {
3831 PyErr_Clear();
3832 metaclass = (PyObject *)base->ob_type;
3833 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00003834 }
3835 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00003836 else {
3837 PyObject *g = PyEval_GetGlobals();
3838 if (g != NULL && PyDict_Check(g))
3839 metaclass = PyDict_GetItemString(g, "__metaclass__");
3840 if (metaclass == NULL)
3841 metaclass = (PyObject *) &PyClass_Type;
3842 Py_INCREF(metaclass);
3843 }
3844 result = PyObject_CallFunction(metaclass, "OOO", name, bases, methods);
3845 Py_DECREF(metaclass);
3846 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00003847}
3848
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003849static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003850exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
3851 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003852{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003853 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00003854 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003855 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003856
Guido van Rossumb209a111997-04-29 18:18:01 +00003857 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
3858 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003859 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00003860 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003861 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00003862 locals = PyTuple_GetItem(prog, 2);
3863 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003864 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003865 if (globals == Py_None) {
3866 globals = PyEval_GetGlobals();
3867 if (locals == Py_None) {
3868 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00003869 plain = 1;
3870 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003871 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003872 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003873 locals = globals;
Guido van Rossumb209a111997-04-29 18:18:01 +00003874 if (!PyString_Check(prog) &&
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00003875 !PyUnicode_Check(prog) &&
Guido van Rossumb209a111997-04-29 18:18:01 +00003876 !PyCode_Check(prog) &&
3877 !PyFile_Check(prog)) {
3878 PyErr_SetString(PyExc_TypeError,
Guido van Rossumac7be682001-01-17 15:42:30 +00003879 "exec: arg 1 must be a string, file, or code object");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003880 return -1;
3881 }
Fred Drake661ea262000-10-24 19:57:45 +00003882 if (!PyDict_Check(globals)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003883 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003884 "exec: arg 2 must be a dictionary or None");
3885 return -1;
3886 }
3887 if (!PyDict_Check(locals)) {
3888 PyErr_SetString(PyExc_TypeError,
3889 "exec: arg 3 must be a dictionary or None");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003890 return -1;
3891 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003892 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00003893 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00003894 if (PyCode_Check(prog)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +00003895 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
3896 PyErr_SetString(PyExc_TypeError,
3897 "code object passed to exec may not contain free variables");
3898 return -1;
3899 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00003900 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003901 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00003902 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003903 FILE *fp = PyFile_AsFile(prog);
3904 char *name = PyString_AsString(PyFile_Name(prog));
Tim Peters5ba58662001-07-16 02:29:45 +00003905 PyCompilerFlags cf;
3906 cf.cf_flags = 0;
3907 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00003908 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
3909 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00003910 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00003911 v = PyRun_File(fp, name, Py_file_input, globals,
3912 locals);
Guido van Rossuma400d8a2000-01-12 22:45:54 +00003913 }
3914 else {
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003915 PyObject *tmp = NULL;
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00003916 char *str;
Tim Peters5ba58662001-07-16 02:29:45 +00003917 PyCompilerFlags cf;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003918 cf.cf_flags = 0;
3919#ifdef Py_USING_UNICODE
3920 if (PyUnicode_Check(prog)) {
3921 tmp = PyUnicode_AsUTF8String(prog);
3922 if (tmp == NULL)
3923 return -1;
3924 prog = tmp;
3925 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
3926 }
3927#endif
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00003928 if (PyString_AsStringAndSize(prog, &str, NULL))
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003929 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00003930 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00003931 v = PyRun_StringFlags(str, Py_file_input, globals,
3932 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00003933 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00003934 v = PyRun_String(str, Py_file_input, globals, locals);
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003935 Py_XDECREF(tmp);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003936 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00003937 if (plain)
3938 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003939 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003940 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00003941 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003942 return 0;
3943}
Guido van Rossum24c13741995-02-14 09:42:43 +00003944
Guido van Rossumac7be682001-01-17 15:42:30 +00003945static void
Paul Prescode68140d2000-08-30 20:25:01 +00003946format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
3947{
3948 char *obj_str;
3949
3950 if (!obj)
3951 return;
3952
3953 obj_str = PyString_AsString(obj);
3954 if (!obj_str)
3955 return;
3956
3957 PyErr_Format(exc, format_str, obj_str);
3958}
Guido van Rossum950361c1997-01-24 13:49:28 +00003959
3960#ifdef DYNAMIC_EXECUTION_PROFILE
3961
Skip Montanarof118cb12001-10-15 20:51:38 +00003962static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003963getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00003964{
3965 int i;
3966 PyObject *l = PyList_New(256);
3967 if (l == NULL) return NULL;
3968 for (i = 0; i < 256; i++) {
3969 PyObject *x = PyInt_FromLong(a[i]);
3970 if (x == NULL) {
3971 Py_DECREF(l);
3972 return NULL;
3973 }
3974 PyList_SetItem(l, i, x);
3975 }
3976 for (i = 0; i < 256; i++)
3977 a[i] = 0;
3978 return l;
3979}
3980
3981PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003982_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00003983{
3984#ifndef DXPAIRS
3985 return getarray(dxp);
3986#else
3987 int i;
3988 PyObject *l = PyList_New(257);
3989 if (l == NULL) return NULL;
3990 for (i = 0; i < 257; i++) {
3991 PyObject *x = getarray(dxpairs[i]);
3992 if (x == NULL) {
3993 Py_DECREF(l);
3994 return NULL;
3995 }
3996 PyList_SetItem(l, i, x);
3997 }
3998 return l;
3999#endif
4000}
4001
4002#endif