blob: 0f52a0bb814e51f83654c47ed2a6cc89d31353cb [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();
Barry Warsawe42b18f1997-08-25 22:13:04 +00001658 if (PyTuple_Check(v)) {
1659 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 }
Barry Warsawe42b18f1997-08-25 22:13:04 +00001672 else if (PyList_Check(v)) {
1673 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:
Guido van Rossumb209a111997-04-29 18:18:01 +00001978 err = PyObject_IsTrue(TOP());
Guido van Rossum04691fc1992-08-12 15:35:34 +00001979 if (err > 0)
1980 err = 0;
1981 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00001982 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001983 else
1984 break;
1985 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001986
Guido van Rossum374a9221991-04-04 10:40:29 +00001987 case JUMP_IF_TRUE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001988 err = PyObject_IsTrue(TOP());
Guido van Rossum04691fc1992-08-12 15:35:34 +00001989 if (err > 0) {
1990 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00001991 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001992 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001993 else if (err == 0)
1994 ;
1995 else
1996 break;
1997 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001998
Guido van Rossum374a9221991-04-04 10:40:29 +00001999 case JUMP_ABSOLUTE:
2000 JUMPTO(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002001 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002002
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002003 case GET_ITER:
2004 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002005 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002006 x = PyObject_GetIter(v);
2007 Py_DECREF(v);
2008 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002009 SET_TOP(x);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002010 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002011 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002012 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002013 break;
2014
2015 case FOR_ITER:
2016 /* before: [iter]; after: [iter, iter()] *or* [] */
2017 v = TOP();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002018 x = PyIter_Next(v);
2019 if (x != NULL) {
2020 PUSH(x);
2021 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002022 }
Tim Petersf4848da2001-05-05 00:14:56 +00002023 if (!PyErr_Occurred()) {
2024 /* iterator ended normally */
2025 x = v = POP();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002026 Py_DECREF(v);
2027 JUMPBY(oparg);
2028 continue;
2029 }
2030 break;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002031
Guido van Rossum374a9221991-04-04 10:40:29 +00002032 case SETUP_LOOP:
2033 case SETUP_EXCEPT:
2034 case SETUP_FINALLY:
Guido van Rossumb209a111997-04-29 18:18:01 +00002035 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002036 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002037 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002038
Guido van Rossumf10570b1995-07-07 22:53:21 +00002039 case CALL_FUNCTION:
Jeremy Hylton985eba52003-02-05 23:13:00 +00002040 PCALL(PCALL_ALL);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002041 x = call_function(&stack_pointer, oparg);
2042 PUSH(x);
2043 if (x != NULL)
2044 continue;
2045 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002046
Jeremy Hylton76901512000-03-28 23:49:17 +00002047 case CALL_FUNCTION_VAR:
2048 case CALL_FUNCTION_KW:
2049 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002050 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002051 int na = oparg & 0xff;
2052 int nk = (oparg>>8) & 0xff;
2053 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002054 int n = na + 2 * nk;
2055 PyObject **pfunc, *func;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002056 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002057 if (flags & CALL_FLAG_VAR)
2058 n++;
2059 if (flags & CALL_FLAG_KW)
2060 n++;
2061 pfunc = stack_pointer - n - 1;
2062 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002063
Guido van Rossumac7be682001-01-17 15:42:30 +00002064 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002065 && PyMethod_GET_SELF(func) != NULL) {
2066 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002067 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002068 func = PyMethod_GET_FUNCTION(func);
2069 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002070 Py_DECREF(*pfunc);
2071 *pfunc = self;
2072 na++;
2073 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002074 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002075 Py_INCREF(func);
2076 x = ext_do_call(func, &stack_pointer, flags, na, nk);
Jeremy Hylton76901512000-03-28 23:49:17 +00002077 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002078
Jeremy Hylton76901512000-03-28 23:49:17 +00002079 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002080 w = POP();
2081 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002082 }
2083 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002084 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002085 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002086 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002087 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002088
Guido van Rossum681d79a1995-07-18 14:51:37 +00002089 case MAKE_FUNCTION:
2090 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002091 x = PyFunction_New(v, f->f_globals);
2092 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002093 /* XXX Maybe this should be a separate opcode? */
2094 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002095 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002096 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002097 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002098 x = NULL;
2099 break;
2100 }
2101 while (--oparg >= 0) {
2102 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002103 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002104 }
2105 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002106 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002107 }
2108 PUSH(x);
2109 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002110
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002111 case MAKE_CLOSURE:
2112 {
2113 int nfree;
2114 v = POP(); /* code object */
2115 x = PyFunction_New(v, f->f_globals);
Jeremy Hylton733c8932001-12-13 19:51:56 +00002116 nfree = PyCode_GetNumFree((PyCodeObject *)v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002117 Py_DECREF(v);
2118 /* XXX Maybe this should be a separate opcode? */
2119 if (x != NULL && nfree > 0) {
2120 v = PyTuple_New(nfree);
2121 if (v == NULL) {
2122 Py_DECREF(x);
2123 x = NULL;
2124 break;
2125 }
2126 while (--nfree >= 0) {
2127 w = POP();
2128 PyTuple_SET_ITEM(v, nfree, w);
2129 }
2130 err = PyFunction_SetClosure(x, v);
2131 Py_DECREF(v);
2132 }
2133 if (x != NULL && oparg > 0) {
2134 v = PyTuple_New(oparg);
2135 if (v == NULL) {
2136 Py_DECREF(x);
2137 x = NULL;
2138 break;
2139 }
2140 while (--oparg >= 0) {
2141 w = POP();
2142 PyTuple_SET_ITEM(v, oparg, w);
2143 }
2144 err = PyFunction_SetDefaults(x, v);
2145 Py_DECREF(v);
2146 }
2147 PUSH(x);
2148 break;
2149 }
2150
Guido van Rossum8861b741996-07-30 16:49:37 +00002151 case BUILD_SLICE:
2152 if (oparg == 3)
2153 w = POP();
2154 else
2155 w = NULL;
2156 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002157 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002158 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002159 Py_DECREF(u);
2160 Py_DECREF(v);
2161 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002162 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002163 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002164 break;
2165
Fred Drakeef8ace32000-08-24 00:32:09 +00002166 case EXTENDED_ARG:
2167 opcode = NEXTOP();
2168 oparg = oparg<<16 | NEXTARG();
2169 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002170
Guido van Rossum374a9221991-04-04 10:40:29 +00002171 default:
2172 fprintf(stderr,
2173 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002174 PyCode_Addr2Line(f->f_code, f->f_lasti),
2175 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002176 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002177 why = WHY_EXCEPTION;
2178 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002179
2180#ifdef CASE_TOO_BIG
2181 }
2182#endif
2183
Guido van Rossum374a9221991-04-04 10:40:29 +00002184 } /* switch */
2185
2186 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002187
Guido van Rossum374a9221991-04-04 10:40:29 +00002188 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002189
Guido van Rossum374a9221991-04-04 10:40:29 +00002190 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002191 if (err == 0 && x != NULL) {
2192#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002193 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002194 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002195 fprintf(stderr,
2196 "XXX undetected error\n");
2197 else
2198#endif
2199 continue; /* Normal, fast path */
2200 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002201 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002202 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002203 err = 0;
2204 }
2205
Guido van Rossum374a9221991-04-04 10:40:29 +00002206 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002207
Guido van Rossum374a9221991-04-04 10:40:29 +00002208 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002209 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002210 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002211 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002212 why = WHY_EXCEPTION;
2213 }
2214 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002215#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002216 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002217 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002218 if (PyErr_Occurred()) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002219 fprintf(stderr,
2220 "XXX undetected error (why=%d)\n",
2221 why);
2222 why = WHY_EXCEPTION;
2223 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002224 }
2225#endif
2226
2227 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002228
Guido van Rossum374a9221991-04-04 10:40:29 +00002229 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002230 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002231
Fred Drake8f51f542001-10-04 14:48:42 +00002232 if (tstate->c_tracefunc != NULL)
2233 call_exc_trace(tstate->c_tracefunc,
2234 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002235 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002236
Guido van Rossum374a9221991-04-04 10:40:29 +00002237 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002238
Guido van Rossum374a9221991-04-04 10:40:29 +00002239 if (why == WHY_RERAISE)
2240 why = WHY_EXCEPTION;
2241
2242 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002243
Tim Peters5ca576e2001-06-18 22:08:13 +00002244 while (why != WHY_NOT && why != WHY_YIELD && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002245 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002246
2247 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2248 /* For a continue inside a try block,
2249 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002250 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2251 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002252 why = WHY_NOT;
2253 JUMPTO(PyInt_AS_LONG(retval));
2254 Py_DECREF(retval);
2255 break;
2256 }
2257
Guido van Rossum374a9221991-04-04 10:40:29 +00002258 while (STACK_LEVEL() > b->b_level) {
2259 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002260 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002261 }
2262 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2263 why = WHY_NOT;
2264 JUMPTO(b->b_handler);
2265 break;
2266 }
2267 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002268 (b->b_type == SETUP_EXCEPT &&
2269 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002270 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002271 PyObject *exc, *val, *tb;
2272 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002273 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002274 val = Py_None;
2275 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002276 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002277 /* Make the raw exception data
2278 available to the handler,
2279 so a program can emulate the
2280 Python main loop. Don't do
2281 this for 'finally'. */
2282 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002283 PyErr_NormalizeException(
2284 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002285 set_exc_info(tstate,
2286 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002287 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002288 if (tb == NULL) {
2289 Py_INCREF(Py_None);
2290 PUSH(Py_None);
2291 } else
2292 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002293 PUSH(val);
2294 PUSH(exc);
2295 }
2296 else {
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002297 if (why == WHY_RETURN ||
Guido van Rossumc5fe5eb2002-06-12 03:45:21 +00002298 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00002299 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002300 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002301 PUSH(v);
2302 }
2303 why = WHY_NOT;
2304 JUMPTO(b->b_handler);
2305 break;
2306 }
2307 } /* unwind stack */
2308
2309 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002310
Guido van Rossum374a9221991-04-04 10:40:29 +00002311 if (why != WHY_NOT)
2312 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002313
Guido van Rossum374a9221991-04-04 10:40:29 +00002314 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002315
Guido van Rossum35974fb2001-12-06 21:28:18 +00002316 if (why != WHY_YIELD) {
2317 /* Pop remaining stack entries -- but when yielding */
2318 while (!EMPTY()) {
2319 v = POP();
2320 Py_XDECREF(v);
2321 }
2322 }
2323
Tim Peters5ca576e2001-06-18 22:08:13 +00002324 if (why != WHY_RETURN && why != WHY_YIELD)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002325 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002326
Fred Drake9e3ad782001-07-03 23:39:52 +00002327 if (tstate->use_tracing) {
2328 if (tstate->c_tracefunc
2329 && (why == WHY_RETURN || why == WHY_YIELD)) {
2330 if (call_trace(tstate->c_tracefunc,
2331 tstate->c_traceobj, f,
2332 PyTrace_RETURN, retval)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002333 Py_XDECREF(retval);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002334 retval = NULL;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002335 why = WHY_EXCEPTION;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002336 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002337 }
Fred Drake8f51f542001-10-04 14:48:42 +00002338 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002339 if (why == WHY_EXCEPTION)
2340 call_trace_protected(tstate->c_profilefunc,
2341 tstate->c_profileobj, f,
2342 PyTrace_RETURN);
2343 else if (call_trace(tstate->c_profilefunc,
2344 tstate->c_profileobj, f,
2345 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002346 Py_XDECREF(retval);
2347 retval = NULL;
2348 why = WHY_EXCEPTION;
2349 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002350 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002351 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002352
Guido van Rossuma027efa1997-05-05 20:56:21 +00002353 reset_exc_info(tstate);
2354
Tim Peters5ca576e2001-06-18 22:08:13 +00002355 /* pop frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002356 --tstate->recursion_depth;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002357 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002358
Guido van Rossum96a42c81992-01-12 02:29:51 +00002359 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002360}
2361
Tim Peters6d6c1a32001-08-02 04:15:00 +00002362PyObject *
2363PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002364 PyObject **args, int argcount, PyObject **kws, int kwcount,
2365 PyObject **defs, int defcount, PyObject *closure)
2366{
2367 register PyFrameObject *f;
2368 register PyObject *retval = NULL;
2369 register PyObject **fastlocals, **freevars;
2370 PyThreadState *tstate = PyThreadState_GET();
2371 PyObject *x, *u;
2372
2373 if (globals == NULL) {
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002374 PyErr_SetString(PyExc_SystemError,
2375 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002376 return NULL;
2377 }
2378
Jeremy Hylton985eba52003-02-05 23:13:00 +00002379 assert(globals != NULL);
2380 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002381 if (f == NULL)
2382 return NULL;
2383
2384 fastlocals = f->f_localsplus;
2385 freevars = f->f_localsplus + f->f_nlocals;
2386
2387 if (co->co_argcount > 0 ||
2388 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2389 int i;
2390 int n = argcount;
2391 PyObject *kwdict = NULL;
2392 if (co->co_flags & CO_VARKEYWORDS) {
2393 kwdict = PyDict_New();
2394 if (kwdict == NULL)
2395 goto fail;
2396 i = co->co_argcount;
2397 if (co->co_flags & CO_VARARGS)
2398 i++;
2399 SETLOCAL(i, kwdict);
2400 }
2401 if (argcount > co->co_argcount) {
2402 if (!(co->co_flags & CO_VARARGS)) {
2403 PyErr_Format(PyExc_TypeError,
2404 "%.200s() takes %s %d "
2405 "%sargument%s (%d given)",
2406 PyString_AsString(co->co_name),
2407 defcount ? "at most" : "exactly",
2408 co->co_argcount,
2409 kwcount ? "non-keyword " : "",
2410 co->co_argcount == 1 ? "" : "s",
2411 argcount);
2412 goto fail;
2413 }
2414 n = co->co_argcount;
2415 }
2416 for (i = 0; i < n; i++) {
2417 x = args[i];
2418 Py_INCREF(x);
2419 SETLOCAL(i, x);
2420 }
2421 if (co->co_flags & CO_VARARGS) {
2422 u = PyTuple_New(argcount - n);
2423 if (u == NULL)
2424 goto fail;
2425 SETLOCAL(co->co_argcount, u);
2426 for (i = n; i < argcount; i++) {
2427 x = args[i];
2428 Py_INCREF(x);
2429 PyTuple_SET_ITEM(u, i-n, x);
2430 }
2431 }
2432 for (i = 0; i < kwcount; i++) {
2433 PyObject *keyword = kws[2*i];
2434 PyObject *value = kws[2*i + 1];
2435 int j;
2436 if (keyword == NULL || !PyString_Check(keyword)) {
2437 PyErr_Format(PyExc_TypeError,
2438 "%.200s() keywords must be strings",
2439 PyString_AsString(co->co_name));
2440 goto fail;
2441 }
2442 /* XXX slow -- speed up using dictionary? */
2443 for (j = 0; j < co->co_argcount; j++) {
2444 PyObject *nm = PyTuple_GET_ITEM(
2445 co->co_varnames, j);
2446 int cmp = PyObject_RichCompareBool(
2447 keyword, nm, Py_EQ);
2448 if (cmp > 0)
2449 break;
2450 else if (cmp < 0)
2451 goto fail;
2452 }
2453 /* Check errors from Compare */
2454 if (PyErr_Occurred())
2455 goto fail;
2456 if (j >= co->co_argcount) {
2457 if (kwdict == NULL) {
2458 PyErr_Format(PyExc_TypeError,
2459 "%.200s() got an unexpected "
2460 "keyword argument '%.400s'",
2461 PyString_AsString(co->co_name),
2462 PyString_AsString(keyword));
2463 goto fail;
2464 }
2465 PyDict_SetItem(kwdict, keyword, value);
2466 }
2467 else {
2468 if (GETLOCAL(j) != NULL) {
2469 PyErr_Format(PyExc_TypeError,
2470 "%.200s() got multiple "
2471 "values for keyword "
2472 "argument '%.400s'",
2473 PyString_AsString(co->co_name),
2474 PyString_AsString(keyword));
2475 goto fail;
2476 }
2477 Py_INCREF(value);
2478 SETLOCAL(j, value);
2479 }
2480 }
2481 if (argcount < co->co_argcount) {
2482 int m = co->co_argcount - defcount;
2483 for (i = argcount; i < m; i++) {
2484 if (GETLOCAL(i) == NULL) {
2485 PyErr_Format(PyExc_TypeError,
2486 "%.200s() takes %s %d "
2487 "%sargument%s (%d given)",
2488 PyString_AsString(co->co_name),
2489 ((co->co_flags & CO_VARARGS) ||
2490 defcount) ? "at least"
2491 : "exactly",
2492 m, kwcount ? "non-keyword " : "",
2493 m == 1 ? "" : "s", i);
2494 goto fail;
2495 }
2496 }
2497 if (n > m)
2498 i = n - m;
2499 else
2500 i = 0;
2501 for (; i < defcount; i++) {
2502 if (GETLOCAL(m+i) == NULL) {
2503 PyObject *def = defs[i];
2504 Py_INCREF(def);
2505 SETLOCAL(m+i, def);
2506 }
2507 }
2508 }
2509 }
2510 else {
2511 if (argcount > 0 || kwcount > 0) {
2512 PyErr_Format(PyExc_TypeError,
2513 "%.200s() takes no arguments (%d given)",
2514 PyString_AsString(co->co_name),
2515 argcount + kwcount);
2516 goto fail;
2517 }
2518 }
2519 /* Allocate and initialize storage for cell vars, and copy free
2520 vars into frame. This isn't too efficient right now. */
2521 if (f->f_ncells) {
2522 int i = 0, j = 0, nargs, found;
2523 char *cellname, *argname;
2524 PyObject *c;
2525
2526 nargs = co->co_argcount;
2527 if (co->co_flags & CO_VARARGS)
2528 nargs++;
2529 if (co->co_flags & CO_VARKEYWORDS)
2530 nargs++;
2531
2532 /* Check for cells that shadow args */
2533 for (i = 0; i < f->f_ncells && j < nargs; ++i) {
2534 cellname = PyString_AS_STRING(
2535 PyTuple_GET_ITEM(co->co_cellvars, i));
2536 found = 0;
2537 while (j < nargs) {
2538 argname = PyString_AS_STRING(
2539 PyTuple_GET_ITEM(co->co_varnames, j));
2540 if (strcmp(cellname, argname) == 0) {
2541 c = PyCell_New(GETLOCAL(j));
2542 if (c == NULL)
2543 goto fail;
2544 GETLOCAL(f->f_nlocals + i) = c;
2545 found = 1;
2546 break;
2547 }
2548 j++;
2549 }
2550 if (found == 0) {
2551 c = PyCell_New(NULL);
2552 if (c == NULL)
2553 goto fail;
2554 SETLOCAL(f->f_nlocals + i, c);
2555 }
2556 }
2557 /* Initialize any that are left */
2558 while (i < f->f_ncells) {
2559 c = PyCell_New(NULL);
2560 if (c == NULL)
2561 goto fail;
2562 SETLOCAL(f->f_nlocals + i, c);
2563 i++;
2564 }
2565 }
2566 if (f->f_nfreevars) {
2567 int i;
2568 for (i = 0; i < f->f_nfreevars; ++i) {
2569 PyObject *o = PyTuple_GET_ITEM(closure, i);
2570 Py_INCREF(o);
2571 freevars[f->f_ncells + i] = o;
2572 }
2573 }
2574
Tim Peters5ca576e2001-06-18 22:08:13 +00002575 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002576 /* Don't need to keep the reference to f_back, it will be set
2577 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002578 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002579 f->f_back = NULL;
2580
Jeremy Hylton985eba52003-02-05 23:13:00 +00002581 PCALL(PCALL_GENERATOR);
2582
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002583 /* Create a new generator that owns the ready to run frame
2584 * and return that as the value. */
Tim Peters5ca576e2001-06-18 22:08:13 +00002585 return gen_new(f);
2586 }
2587
2588 retval = eval_frame(f);
2589
2590 fail: /* Jump here from prelude on failure */
2591
Tim Petersb13680b2001-11-27 23:29:29 +00002592 /* decref'ing the frame can cause __del__ methods to get invoked,
2593 which can call back into Python. While we're done with the
2594 current Python frame (f), the associated C stack is still in use,
2595 so recursion_depth must be boosted for the duration.
2596 */
2597 assert(tstate != NULL);
2598 ++tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002599 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00002600 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002601 return retval;
2602}
2603
2604
Guido van Rossuma027efa1997-05-05 20:56:21 +00002605static void
Guido van Rossumac7be682001-01-17 15:42:30 +00002606set_exc_info(PyThreadState *tstate,
2607 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002608{
2609 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002610 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00002611
Guido van Rossuma027efa1997-05-05 20:56:21 +00002612 frame = tstate->frame;
2613 if (frame->f_exc_type == NULL) {
2614 /* This frame didn't catch an exception before */
2615 /* Save previous exception of this thread in this frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002616 if (tstate->exc_type == NULL) {
2617 Py_INCREF(Py_None);
2618 tstate->exc_type = Py_None;
2619 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002620 tmp_type = frame->f_exc_type;
2621 tmp_value = frame->f_exc_value;
2622 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002623 Py_XINCREF(tstate->exc_type);
2624 Py_XINCREF(tstate->exc_value);
2625 Py_XINCREF(tstate->exc_traceback);
2626 frame->f_exc_type = tstate->exc_type;
2627 frame->f_exc_value = tstate->exc_value;
2628 frame->f_exc_traceback = tstate->exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002629 Py_XDECREF(tmp_type);
2630 Py_XDECREF(tmp_value);
2631 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002632 }
2633 /* Set new exception for this thread */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002634 tmp_type = tstate->exc_type;
2635 tmp_value = tstate->exc_value;
2636 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002637 Py_XINCREF(type);
2638 Py_XINCREF(value);
2639 Py_XINCREF(tb);
2640 tstate->exc_type = type;
2641 tstate->exc_value = value;
2642 tstate->exc_traceback = tb;
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 /* For b/w compatibility */
2647 PySys_SetObject("exc_type", type);
2648 PySys_SetObject("exc_value", value);
2649 PySys_SetObject("exc_traceback", tb);
2650}
2651
2652static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002653reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002654{
2655 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002656 PyObject *tmp_type, *tmp_value, *tmp_tb;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002657 frame = tstate->frame;
2658 if (frame->f_exc_type != NULL) {
2659 /* This frame caught an exception */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002660 tmp_type = tstate->exc_type;
2661 tmp_value = tstate->exc_value;
2662 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002663 Py_XINCREF(frame->f_exc_type);
2664 Py_XINCREF(frame->f_exc_value);
2665 Py_XINCREF(frame->f_exc_traceback);
2666 tstate->exc_type = frame->f_exc_type;
2667 tstate->exc_value = frame->f_exc_value;
2668 tstate->exc_traceback = frame->f_exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002669 Py_XDECREF(tmp_type);
2670 Py_XDECREF(tmp_value);
2671 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002672 /* For b/w compatibility */
2673 PySys_SetObject("exc_type", frame->f_exc_type);
2674 PySys_SetObject("exc_value", frame->f_exc_value);
2675 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
2676 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002677 tmp_type = frame->f_exc_type;
2678 tmp_value = frame->f_exc_value;
2679 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002680 frame->f_exc_type = NULL;
2681 frame->f_exc_value = NULL;
2682 frame->f_exc_traceback = NULL;
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}
2687
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002688/* Logic for the raise statement (too complicated for inlining).
2689 This *consumes* a reference count to each of its arguments. */
Guido van Rossum1aa14831997-01-21 05:34:20 +00002690static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002691do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002692{
Guido van Rossumd295f121998-04-09 21:39:57 +00002693 if (type == NULL) {
2694 /* Reraise */
2695 PyThreadState *tstate = PyThreadState_Get();
2696 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
2697 value = tstate->exc_value;
2698 tb = tstate->exc_traceback;
2699 Py_XINCREF(type);
2700 Py_XINCREF(value);
2701 Py_XINCREF(tb);
2702 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002703
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002704 /* We support the following forms of raise:
2705 raise <class>, <classinstance>
2706 raise <class>, <argument tuple>
2707 raise <class>, None
2708 raise <class>, <argument>
2709 raise <classinstance>, None
2710 raise <string>, <object>
2711 raise <string>, None
2712
2713 An omitted second argument is the same as None.
2714
2715 In addition, raise <tuple>, <anything> is the same as
2716 raising the tuple's first item (and it better have one!);
2717 this rule is applied recursively.
2718
2719 Finally, an optional third argument can be supplied, which
2720 gives the traceback to be substituted (useful when
2721 re-raising an exception after examining it). */
2722
2723 /* First, check the traceback argument, replacing None with
2724 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002725 if (tb == Py_None) {
2726 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002727 tb = NULL;
2728 }
2729 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002730 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002731 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002732 goto raise_error;
2733 }
2734
2735 /* Next, replace a missing value with None */
2736 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002737 value = Py_None;
2738 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002739 }
2740
2741 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00002742 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
2743 PyObject *tmp = type;
2744 type = PyTuple_GET_ITEM(type, 0);
2745 Py_INCREF(type);
2746 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002747 }
2748
Tim Petersafb2c802002-04-18 18:06:20 +00002749 if (PyString_CheckExact(type))
2750 /* Raising builtin string is deprecated but still allowed --
2751 * do nothing. Raising an instance of a new-style str
2752 * subclass is right out. */
Neal Norwitz37aa0662003-01-10 15:31:15 +00002753 PyErr_Warn(PyExc_PendingDeprecationWarning,
2754 "raising a string exception is deprecated");
Barry Warsaw4249f541997-08-22 21:26:19 +00002755
2756 else if (PyClass_Check(type))
2757 PyErr_NormalizeException(&type, &value, &tb);
2758
Guido van Rossumb209a111997-04-29 18:18:01 +00002759 else if (PyInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002760 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002761 if (value != Py_None) {
2762 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002763 "instance exception may not have a separate value");
2764 goto raise_error;
2765 }
2766 else {
2767 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00002768 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002769 value = type;
Guido van Rossumb209a111997-04-29 18:18:01 +00002770 type = (PyObject*) ((PyInstanceObject*)type)->in_class;
2771 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002772 }
2773 }
2774 else {
2775 /* Not something you can raise. You get an exception
2776 anyway, just not what you specified :-) */
Jeremy Hylton960d9482001-04-27 02:25:33 +00002777 PyErr_Format(PyExc_TypeError,
Neal Norwitz37aa0662003-01-10 15:31:15 +00002778 "exceptions must be classes, instances, or "
2779 "strings (deprecated), not %s",
2780 type->ob_type->tp_name);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002781 goto raise_error;
2782 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002783 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002784 if (tb == NULL)
2785 return WHY_EXCEPTION;
2786 else
2787 return WHY_RERAISE;
2788 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00002789 Py_XDECREF(value);
2790 Py_XDECREF(type);
2791 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002792 return WHY_EXCEPTION;
2793}
2794
Tim Petersd6d010b2001-06-21 02:49:55 +00002795/* Iterate v argcnt times and store the results on the stack (via decreasing
2796 sp). Return 1 for success, 0 if error. */
2797
Barry Warsawe42b18f1997-08-25 22:13:04 +00002798static int
Tim Petersd6d010b2001-06-21 02:49:55 +00002799unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00002800{
Tim Petersd6d010b2001-06-21 02:49:55 +00002801 int i = 0;
2802 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00002803 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00002804
Tim Petersd6d010b2001-06-21 02:49:55 +00002805 assert(v != NULL);
2806
2807 it = PyObject_GetIter(v);
2808 if (it == NULL)
2809 goto Error;
2810
2811 for (; i < argcnt; i++) {
2812 w = PyIter_Next(it);
2813 if (w == NULL) {
2814 /* Iterator done, via error or exhaustion. */
2815 if (!PyErr_Occurred()) {
2816 PyErr_Format(PyExc_ValueError,
2817 "need more than %d value%s to unpack",
2818 i, i == 1 ? "" : "s");
2819 }
2820 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00002821 }
2822 *--sp = w;
2823 }
Tim Petersd6d010b2001-06-21 02:49:55 +00002824
2825 /* We better have exhausted the iterator now. */
2826 w = PyIter_Next(it);
2827 if (w == NULL) {
2828 if (PyErr_Occurred())
2829 goto Error;
2830 Py_DECREF(it);
2831 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00002832 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00002833 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00002834 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00002835 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00002836Error:
Barry Warsaw91010551997-08-25 22:30:51 +00002837 for (; i > 0; i--, sp++)
2838 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00002839 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00002840 return 0;
2841}
2842
2843
Guido van Rossum96a42c81992-01-12 02:29:51 +00002844#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00002845static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002846prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002847{
Guido van Rossum3f5da241990-12-20 15:06:42 +00002848 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00002849 if (PyObject_Print(v, stdout, 0) != 0)
2850 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00002851 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00002852 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002853}
Guido van Rossum3f5da241990-12-20 15:06:42 +00002854#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002855
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002856static void
Fred Drake5755ce62001-06-27 19:19:46 +00002857call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002858{
Guido van Rossumb209a111997-04-29 18:18:01 +00002859 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002860 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00002861 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00002862 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002863 value = Py_None;
2864 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00002865 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002866 arg = Py_BuildValue("(OOO)", type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002867 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002868 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002869 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002870 }
Fred Drake5755ce62001-06-27 19:19:46 +00002871 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002872 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002873 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00002874 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002875 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00002876 Py_XDECREF(type);
2877 Py_XDECREF(value);
2878 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002879 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002880}
2881
Fred Drake4ec5d562001-10-04 19:26:43 +00002882static void
2883call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
2884 int what)
2885{
2886 PyObject *type, *value, *traceback;
2887 int err;
2888 PyErr_Fetch(&type, &value, &traceback);
2889 err = call_trace(func, obj, frame, what, NULL);
2890 if (err == 0)
2891 PyErr_Restore(type, value, traceback);
2892 else {
2893 Py_XDECREF(type);
2894 Py_XDECREF(value);
2895 Py_XDECREF(traceback);
2896 }
2897}
2898
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002899static int
Fred Drake5755ce62001-06-27 19:19:46 +00002900call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
2901 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002902{
Fred Drake5755ce62001-06-27 19:19:46 +00002903 register PyThreadState *tstate = frame->f_tstate;
2904 int result;
2905 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002906 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002907 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00002908 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00002909 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00002910 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
2911 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00002912 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00002913 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002914}
2915
Michael W. Hudson006c7522002-11-08 13:08:46 +00002916static int
Michael W. Hudson019a78e2002-11-08 12:53:11 +00002917maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002918 PyFrameObject *frame, int *instr_lb, int *instr_ub)
2919{
2920 /* The theory of SET_LINENO-less tracing.
2921
2922 In a nutshell, we use the co_lnotab field of the code object
2923 to tell when execution has moved onto a different line.
2924
2925 As mentioned above, the basic idea is so set things up so
2926 that
2927
2928 *instr_lb <= frame->f_lasti < *instr_ub
2929
2930 is true so long as execution does not change lines.
2931
2932 This is all fairly simple. Digging the information out of
2933 co_lnotab takes some work, but is conceptually clear.
2934
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00002935 Somewhat harder to explain is why we don't *always* call the
2936 line trace function when the above test fails.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002937
2938 Consider this code:
2939
2940 1: def f(a):
2941 2: if a:
2942 3: print 1
2943 4: else:
2944 5: print 2
2945
2946 which compiles to this:
2947
2948 2 0 LOAD_FAST 0 (a)
2949 3 JUMP_IF_FALSE 9 (to 15)
2950 6 POP_TOP
2951
2952 3 7 LOAD_CONST 1 (1)
2953 10 PRINT_ITEM
2954 11 PRINT_NEWLINE
2955 12 JUMP_FORWARD 6 (to 21)
2956 >> 15 POP_TOP
2957
2958 5 16 LOAD_CONST 2 (2)
2959 19 PRINT_ITEM
2960 20 PRINT_NEWLINE
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00002961 >> 21 LOAD_CONST 0 (None)
2962 24 RETURN_VALUE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002963
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00002964 If 'a' is false, execution will jump to instruction at offset
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002965 15 and the co_lnotab will claim that execution has moved to
2966 line 3. This is at best misleading. In this case we could
2967 associate the POP_TOP with line 4, but that doesn't make
2968 sense in all cases (I think).
2969
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00002970 What we do is only call the line trace function if the co_lnotab
2971 indicates we have jumped to the *start* of a line, i.e. if the
2972 current instruction offset matches the offset given for the
2973 start of a line by the co_lnotab.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002974
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00002975 This also takes care of the situation where 'a' is true.
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00002976 Execution will jump from instruction offset 12 to offset 21.
2977 Then the co_lnotab would imply that execution has moved to line
2978 5, which is again misleading.
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00002979
2980 Why do we set f_lineno when tracing? Well, consider the code
2981 above when 'a' is true. If stepping through this with 'n' in
2982 pdb, you would stop at line 1 with a "call" type event, then
2983 line events on lines 2 and 3, then a "return" type event -- but
2984 you would be shown line 5 during this event. This is a change
2985 from the behaviour in 2.2 and before, and I've found it
2986 confusing in practice. By setting and using f_lineno when
2987 tracing, one can report a line number different from that
2988 suggested by f_lasti on this one occasion where it's desirable.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002989 */
2990
Michael W. Hudson006c7522002-11-08 13:08:46 +00002991 int result = 0;
2992
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00002993 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002994 PyCodeObject* co = frame->f_code;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00002995 int size, addr, line;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002996 unsigned char* p;
2997
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00002998 size = PyString_GET_SIZE(co->co_lnotab) / 2;
2999 p = (unsigned char*)PyString_AS_STRING(co->co_lnotab);
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003000
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003001 addr = 0;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003002 line = co->co_firstlineno;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003003
3004 /* possible optimization: if f->f_lasti == instr_ub
3005 (likely to be a common case) then we already know
3006 instr_lb -- if we stored the matching value of p
3007 somwhere we could skip the first while loop. */
3008
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003009 /* see comments in compile.c for the description of
3010 co_lnotab. A point to remember: increments to p
3011 should come in pairs -- although we don't care about
3012 the line increments here, treating them as byte
3013 increments gets confusing, to say the least. */
3014
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003015 while (size > 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003016 if (addr + *p > frame->f_lasti)
3017 break;
3018 addr += *p++;
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003019 if (*p) *instr_lb = addr;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003020 line += *p++;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003021 --size;
3022 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003023
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003024 if (addr == frame->f_lasti) {
3025 frame->f_lineno = line;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003026 result = call_trace(func, obj, frame,
3027 PyTrace_LINE, Py_None);
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003028 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003029
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003030 if (size > 0) {
3031 while (--size >= 0) {
3032 addr += *p++;
3033 if (*p++)
3034 break;
3035 }
3036 *instr_ub = addr;
3037 }
3038 else {
3039 *instr_ub = INT_MAX;
3040 }
3041 }
Michael W. Hudson006c7522002-11-08 13:08:46 +00003042
3043 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003044}
3045
Fred Drake5755ce62001-06-27 19:19:46 +00003046void
3047PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003048{
Fred Drake5755ce62001-06-27 19:19:46 +00003049 PyThreadState *tstate = PyThreadState_Get();
3050 PyObject *temp = tstate->c_profileobj;
3051 Py_XINCREF(arg);
3052 tstate->c_profilefunc = NULL;
3053 tstate->c_profileobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003054 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003055 Py_XDECREF(temp);
3056 tstate->c_profilefunc = func;
3057 tstate->c_profileobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003058 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003059}
3060
3061void
3062PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3063{
3064 PyThreadState *tstate = PyThreadState_Get();
3065 PyObject *temp = tstate->c_traceobj;
3066 Py_XINCREF(arg);
3067 tstate->c_tracefunc = NULL;
3068 tstate->c_traceobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003069 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003070 Py_XDECREF(temp);
3071 tstate->c_tracefunc = func;
3072 tstate->c_traceobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003073 tstate->use_tracing = ((func != NULL)
3074 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003075}
3076
Guido van Rossumb209a111997-04-29 18:18:01 +00003077PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003078PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003079{
Michael W. Hudson019a78e2002-11-08 12:53:11 +00003080 PyFrameObject *current_frame = (PyFrameObject *)PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003081 if (current_frame == NULL)
Michael W. Hudson019a78e2002-11-08 12:53:11 +00003082 return PyThreadState_Get()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003083 else
3084 return current_frame->f_builtins;
3085}
3086
Guido van Rossumb209a111997-04-29 18:18:01 +00003087PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003088PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003089{
Michael W. Hudson019a78e2002-11-08 12:53:11 +00003090 PyFrameObject *current_frame = (PyFrameObject *)PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003091 if (current_frame == NULL)
3092 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003093 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003094 return current_frame->f_locals;
3095}
3096
Guido van Rossumb209a111997-04-29 18:18:01 +00003097PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003098PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003099{
Michael W. Hudson019a78e2002-11-08 12:53:11 +00003100 PyFrameObject *current_frame = (PyFrameObject *)PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003101 if (current_frame == NULL)
3102 return NULL;
3103 else
3104 return current_frame->f_globals;
3105}
3106
Guido van Rossumb209a111997-04-29 18:18:01 +00003107PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003108PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003109{
Michael W. Hudson019a78e2002-11-08 12:53:11 +00003110 PyThreadState *tstate = PyThreadState_Get();
3111 return _PyThreadState_GetFrame((PyObject *)tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003112}
3113
Guido van Rossum6135a871995-01-09 17:53:26 +00003114int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003115PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003116{
Michael W. Hudson019a78e2002-11-08 12:53:11 +00003117 PyFrameObject *current_frame = (PyFrameObject *)PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003118 return current_frame == NULL ? 0 : current_frame->f_restricted;
3119}
3120
Guido van Rossumbe270261997-05-22 22:26:18 +00003121int
Tim Peters5ba58662001-07-16 02:29:45 +00003122PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003123{
Michael W. Hudson019a78e2002-11-08 12:53:11 +00003124 PyFrameObject *current_frame = (PyFrameObject *)PyEval_GetFrame();
Tim Peters5ba58662001-07-16 02:29:45 +00003125 int result = 0;
3126
3127 if (current_frame != NULL) {
3128 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003129 const int compilerflags = codeflags & PyCF_MASK;
3130 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003131 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003132 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003133 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003134#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003135 if (codeflags & CO_GENERATOR_ALLOWED) {
3136 result = 1;
3137 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3138 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003139#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003140 }
3141 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003142}
3143
3144int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003145Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003146{
Guido van Rossumb209a111997-04-29 18:18:01 +00003147 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003148 if (f == NULL)
3149 return 0;
3150 if (!PyFile_SoftSpace(f, 0))
3151 return 0;
3152 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003153}
3154
Guido van Rossum3f5da241990-12-20 15:06:42 +00003155
Guido van Rossum681d79a1995-07-18 14:51:37 +00003156/* External interface to call any callable object.
3157 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003158
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003159#undef PyEval_CallObject
3160/* for backward compatibility: export this interface */
3161
Guido van Rossumb209a111997-04-29 18:18:01 +00003162PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003163PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003164{
Guido van Rossumb209a111997-04-29 18:18:01 +00003165 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003166}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003167#define PyEval_CallObject(func,arg) \
3168 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003169
Guido van Rossumb209a111997-04-29 18:18:01 +00003170PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003171PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003172{
Jeremy Hylton52820442001-01-03 23:52:36 +00003173 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003174
3175 if (arg == NULL)
Guido van Rossumb209a111997-04-29 18:18:01 +00003176 arg = PyTuple_New(0);
3177 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003178 PyErr_SetString(PyExc_TypeError,
3179 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003180 return NULL;
3181 }
3182 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003183 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003184
Guido van Rossumb209a111997-04-29 18:18:01 +00003185 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003186 PyErr_SetString(PyExc_TypeError,
3187 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003188 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003189 return NULL;
3190 }
3191
Tim Peters6d6c1a32001-08-02 04:15:00 +00003192 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003193 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003194 return result;
3195}
3196
Tim Peters6d6c1a32001-08-02 04:15:00 +00003197char *
3198PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003199{
3200 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003201 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003202 else if (PyFunction_Check(func))
3203 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3204 else if (PyCFunction_Check(func))
3205 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3206 else if (PyClass_Check(func))
3207 return PyString_AsString(((PyClassObject*)func)->cl_name);
3208 else if (PyInstance_Check(func)) {
3209 return PyString_AsString(
3210 ((PyInstanceObject*)func)->in_class->cl_name);
3211 } else {
3212 return func->ob_type->tp_name;
3213 }
3214}
3215
Tim Peters6d6c1a32001-08-02 04:15:00 +00003216char *
3217PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003218{
3219 if (PyMethod_Check(func))
3220 return "()";
3221 else if (PyFunction_Check(func))
3222 return "()";
3223 else if (PyCFunction_Check(func))
3224 return "()";
3225 else if (PyClass_Check(func))
3226 return " constructor";
3227 else if (PyInstance_Check(func)) {
3228 return " instance";
3229 } else {
3230 return " object";
3231 }
3232}
3233
Jeremy Hylton52820442001-01-03 23:52:36 +00003234#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
3235
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003236static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003237err_args(PyObject *func, int flags, int nargs)
3238{
3239 if (flags & METH_NOARGS)
3240 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003241 "%.200s() takes no arguments (%d given)",
Jeremy Hylton192690e2002-08-16 18:36:11 +00003242 ((PyCFunctionObject *)func)->m_ml->ml_name,
3243 nargs);
3244 else
3245 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003246 "%.200s() takes exactly one argument (%d given)",
Jeremy Hylton192690e2002-08-16 18:36:11 +00003247 ((PyCFunctionObject *)func)->m_ml->ml_name,
3248 nargs);
3249}
3250
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003251static PyObject *
3252call_function(PyObject ***pp_stack, int oparg)
3253{
3254 int na = oparg & 0xff;
3255 int nk = (oparg>>8) & 0xff;
3256 int n = na + 2 * nk;
3257 PyObject **pfunc = (*pp_stack) - n - 1;
3258 PyObject *func = *pfunc;
3259 PyObject *x, *w;
3260
Jeremy Hylton985eba52003-02-05 23:13:00 +00003261 /* Always dispatch PyCFunction first, because these are
3262 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003263 */
3264 if (PyCFunction_Check(func) && nk == 0) {
3265 int flags = PyCFunction_GET_FLAGS(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003266 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003267 if (flags & (METH_NOARGS | METH_O)) {
3268 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3269 PyObject *self = PyCFunction_GET_SELF(func);
3270 if (flags & METH_NOARGS && na == 0)
3271 x = (*meth)(self, NULL);
3272 else if (flags & METH_O && na == 1) {
3273 PyObject *arg = EXT_POP(*pp_stack);
3274 x = (*meth)(self, arg);
3275 Py_DECREF(arg);
3276 }
3277 else {
3278 err_args(func, flags, na);
3279 x = NULL;
3280 }
3281 }
3282 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003283 PyObject *callargs;
3284 callargs = load_args(pp_stack, na);
3285 x = PyCFunction_Call(func, callargs, NULL);
3286 Py_XDECREF(callargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003287 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003288 } else {
3289 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3290 /* optimize access to bound methods */
3291 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003292 PCALL(PCALL_METHOD);
3293 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003294 Py_INCREF(self);
3295 func = PyMethod_GET_FUNCTION(func);
3296 Py_INCREF(func);
3297 Py_DECREF(*pfunc);
3298 *pfunc = self;
3299 na++;
3300 n++;
3301 } else
3302 Py_INCREF(func);
3303 if (PyFunction_Check(func))
3304 x = fast_function(func, pp_stack, n, na, nk);
3305 else
3306 x = do_call(func, pp_stack, na, nk);
3307 Py_DECREF(func);
3308 }
3309
Jeremy Hylton985eba52003-02-05 23:13:00 +00003310 /* What does this do? */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003311 while ((*pp_stack) > pfunc) {
3312 w = EXT_POP(*pp_stack);
3313 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003314 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003315 }
3316 return x;
3317}
3318
Jeremy Hylton192690e2002-08-16 18:36:11 +00003319/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003320 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003321 For the simplest case -- a function that takes only positional
3322 arguments and is called with only positional arguments -- it
3323 inlines the most primitive frame setup code from
3324 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3325 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003326*/
3327
3328static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003329fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003330{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003331 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003332 PyObject *globals = PyFunction_GET_GLOBALS(func);
3333 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3334 PyObject **d = NULL;
3335 int nd = 0;
3336
Jeremy Hylton985eba52003-02-05 23:13:00 +00003337 PCALL(PCALL_FUNCTION);
3338 PCALL(PCALL_FAST_FUNCTION);
3339 if (argdefs == NULL && co->co_argcount == n &&
3340 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3341 PyFrameObject *f;
3342 PyObject *retval = NULL;
3343 PyThreadState *tstate = PyThreadState_GET();
3344 PyObject **fastlocals, **stack;
3345 int i;
3346
3347 PCALL(PCALL_FASTER_FUNCTION);
3348 assert(globals != NULL);
3349 /* XXX Perhaps we should create a specialized
3350 PyFrame_New() that doesn't take locals, but does
3351 take builtins without sanity checking them.
3352 */
3353 f = PyFrame_New(tstate, co, globals, NULL);
3354 if (f == NULL)
3355 return NULL;
3356
3357 fastlocals = f->f_localsplus;
3358 stack = (*pp_stack) - n;
3359
3360 for (i = 0; i < n; i++) {
3361 Py_INCREF(*stack);
3362 fastlocals[i] = *stack++;
3363 }
3364 retval = eval_frame(f);
3365 assert(tstate != NULL);
3366 ++tstate->recursion_depth;
3367 Py_DECREF(f);
3368 --tstate->recursion_depth;
3369 return retval;
3370 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003371 if (argdefs != NULL) {
3372 d = &PyTuple_GET_ITEM(argdefs, 0);
3373 nd = ((PyTupleObject *)argdefs)->ob_size;
3374 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003375 return PyEval_EvalCodeEx(co, globals,
3376 (PyObject *)NULL, (*pp_stack)-n, na,
3377 (*pp_stack)-2*nk, nk, d, nd,
3378 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003379}
3380
3381static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003382update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3383 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003384{
3385 PyObject *kwdict = NULL;
3386 if (orig_kwdict == NULL)
3387 kwdict = PyDict_New();
3388 else {
3389 kwdict = PyDict_Copy(orig_kwdict);
3390 Py_DECREF(orig_kwdict);
3391 }
3392 if (kwdict == NULL)
3393 return NULL;
3394 while (--nk >= 0) {
3395 int err;
3396 PyObject *value = EXT_POP(*pp_stack);
3397 PyObject *key = EXT_POP(*pp_stack);
3398 if (PyDict_GetItem(kwdict, key) != NULL) {
Guido van Rossumac7be682001-01-17 15:42:30 +00003399 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +00003400 "%.200s%s got multiple values "
Jeremy Hylton512a2372001-04-11 13:52:29 +00003401 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003402 PyEval_GetFuncName(func),
3403 PyEval_GetFuncDesc(func),
Jeremy Hylton512a2372001-04-11 13:52:29 +00003404 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003405 Py_DECREF(key);
3406 Py_DECREF(value);
3407 Py_DECREF(kwdict);
3408 return NULL;
3409 }
3410 err = PyDict_SetItem(kwdict, key, value);
3411 Py_DECREF(key);
3412 Py_DECREF(value);
3413 if (err) {
3414 Py_DECREF(kwdict);
3415 return NULL;
3416 }
3417 }
3418 return kwdict;
3419}
3420
3421static PyObject *
3422update_star_args(int nstack, int nstar, PyObject *stararg,
3423 PyObject ***pp_stack)
3424{
3425 PyObject *callargs, *w;
3426
3427 callargs = PyTuple_New(nstack + nstar);
3428 if (callargs == NULL) {
3429 return NULL;
3430 }
3431 if (nstar) {
3432 int i;
3433 for (i = 0; i < nstar; i++) {
3434 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3435 Py_INCREF(a);
3436 PyTuple_SET_ITEM(callargs, nstack + i, a);
3437 }
3438 }
3439 while (--nstack >= 0) {
3440 w = EXT_POP(*pp_stack);
3441 PyTuple_SET_ITEM(callargs, nstack, w);
3442 }
3443 return callargs;
3444}
3445
3446static PyObject *
3447load_args(PyObject ***pp_stack, int na)
3448{
3449 PyObject *args = PyTuple_New(na);
3450 PyObject *w;
3451
3452 if (args == NULL)
3453 return NULL;
3454 while (--na >= 0) {
3455 w = EXT_POP(*pp_stack);
3456 PyTuple_SET_ITEM(args, na, w);
3457 }
3458 return args;
3459}
3460
3461static PyObject *
3462do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3463{
3464 PyObject *callargs = NULL;
3465 PyObject *kwdict = NULL;
3466 PyObject *result = NULL;
3467
3468 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003469 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003470 if (kwdict == NULL)
3471 goto call_fail;
3472 }
3473 callargs = load_args(pp_stack, na);
3474 if (callargs == NULL)
3475 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003476#ifdef CALL_PROFILE
3477 /* At this point, we have to look at the type of func to
3478 update the call stats properly. Do it here so as to avoid
3479 exposing the call stats machinery outside ceval.c
3480 */
3481 if (PyFunction_Check(func))
3482 PCALL(PCALL_FUNCTION);
3483 else if (PyMethod_Check(func))
3484 PCALL(PCALL_METHOD);
3485 else if (PyType_Check(func))
3486 PCALL(PCALL_TYPE);
3487 else
3488 PCALL(PCALL_OTHER);
3489#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003490 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003491 call_fail:
3492 Py_XDECREF(callargs);
3493 Py_XDECREF(kwdict);
3494 return result;
3495}
3496
3497static PyObject *
3498ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3499{
3500 int nstar = 0;
3501 PyObject *callargs = NULL;
3502 PyObject *stararg = NULL;
3503 PyObject *kwdict = NULL;
3504 PyObject *result = NULL;
3505
3506 if (flags & CALL_FLAG_KW) {
3507 kwdict = EXT_POP(*pp_stack);
3508 if (!(kwdict && PyDict_Check(kwdict))) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003509 PyErr_Format(PyExc_TypeError,
Jeremy Hylton512a2372001-04-11 13:52:29 +00003510 "%s%s argument after ** "
3511 "must be a dictionary",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003512 PyEval_GetFuncName(func),
3513 PyEval_GetFuncDesc(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003514 goto ext_call_fail;
3515 }
3516 }
3517 if (flags & CALL_FLAG_VAR) {
3518 stararg = EXT_POP(*pp_stack);
3519 if (!PyTuple_Check(stararg)) {
3520 PyObject *t = NULL;
3521 t = PySequence_Tuple(stararg);
3522 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003523 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3524 PyErr_Format(PyExc_TypeError,
3525 "%s%s argument after * "
3526 "must be a sequence",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003527 PyEval_GetFuncName(func),
3528 PyEval_GetFuncDesc(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003529 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003530 goto ext_call_fail;
3531 }
3532 Py_DECREF(stararg);
3533 stararg = t;
3534 }
3535 nstar = PyTuple_GET_SIZE(stararg);
3536 }
3537 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003538 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003539 if (kwdict == NULL)
3540 goto ext_call_fail;
3541 }
3542 callargs = update_star_args(na, nstar, stararg, pp_stack);
3543 if (callargs == NULL)
3544 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003545#ifdef CALL_PROFILE
3546 /* At this point, we have to look at the type of func to
3547 update the call stats properly. Do it here so as to avoid
3548 exposing the call stats machinery outside ceval.c
3549 */
3550 if (PyFunction_Check(func))
3551 PCALL(PCALL_FUNCTION);
3552 else if (PyMethod_Check(func))
3553 PCALL(PCALL_METHOD);
3554 else if (PyType_Check(func))
3555 PCALL(PCALL_TYPE);
3556 else
3557 PCALL(PCALL_OTHER);
3558#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003559 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003560 ext_call_fail:
3561 Py_XDECREF(callargs);
3562 Py_XDECREF(kwdict);
3563 Py_XDECREF(stararg);
3564 return result;
3565}
3566
Guido van Rossum3b9c6671996-07-30 18:40:29 +00003567#define SLICE_ERROR_MSG \
3568 "standard sequence type does not support step size other than one"
3569
Tim Peterscb479e72001-12-16 19:11:44 +00003570/* Extract a slice index from a PyInt or PyLong, and store in *pi.
3571 Silently reduce values larger than INT_MAX to INT_MAX, and silently
3572 boost values less than -INT_MAX to 0. Return 0 on error, 1 on success.
3573*/
Tim Petersb5196382001-12-16 19:44:20 +00003574/* Note: If v is NULL, return success without storing into *pi. This
3575 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3576 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00003577*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00003578int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003579_PyEval_SliceIndex(PyObject *v, int *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003580{
Tim Petersb5196382001-12-16 19:44:20 +00003581 if (v != NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003582 long x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003583 if (PyInt_Check(v)) {
3584 x = PyInt_AsLong(v);
3585 } else if (PyLong_Check(v)) {
3586 x = PyLong_AsLong(v);
3587 if (x==-1 && PyErr_Occurred()) {
3588 PyObject *long_zero;
Guido van Rossumac7be682001-01-17 15:42:30 +00003589 int cmp;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003590
Guido van Rossumac7be682001-01-17 15:42:30 +00003591 if (!PyErr_ExceptionMatches(
3592 PyExc_OverflowError)) {
3593 /* It's not an overflow error, so just
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003594 signal an error */
Guido van Rossum20c6add2000-05-08 14:06:50 +00003595 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003596 }
3597
Guido van Rossumac7be682001-01-17 15:42:30 +00003598 /* Clear the OverflowError */
3599 PyErr_Clear();
3600
3601 /* It's an overflow error, so we need to
3602 check the sign of the long integer,
3603 set the value to INT_MAX or 0, and clear
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003604 the error. */
3605
3606 /* Create a long integer with a value of 0 */
Guido van Rossumac7be682001-01-17 15:42:30 +00003607 long_zero = PyLong_FromLong(0L);
Tim Peterscb479e72001-12-16 19:11:44 +00003608 if (long_zero == NULL)
3609 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003610
3611 /* Check sign */
Guido van Rossumac7be682001-01-17 15:42:30 +00003612 cmp = PyObject_RichCompareBool(v, long_zero,
3613 Py_GT);
3614 Py_DECREF(long_zero);
3615 if (cmp < 0)
3616 return 0;
3617 else if (cmp > 0)
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003618 x = INT_MAX;
3619 else
3620 x = 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003621 }
3622 } else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003623 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003624 "slice indices must be integers");
Guido van Rossum20c6add2000-05-08 14:06:50 +00003625 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003626 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003627 /* Truncate -- very long indices are truncated anyway */
3628 if (x > INT_MAX)
3629 x = INT_MAX;
3630 else if (x < -INT_MAX)
Michael W. Hudsoncbd6fb92002-11-06 15:17:32 +00003631 x = -INT_MAX;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003632 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003633 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00003634 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003635}
3636
Guido van Rossum50d756e2001-08-18 17:43:36 +00003637#undef ISINT
3638#define ISINT(x) ((x) == NULL || PyInt_Check(x) || PyLong_Check(x))
3639
Guido van Rossumb209a111997-04-29 18:18:01 +00003640static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003641apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003642{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003643 PyTypeObject *tp = u->ob_type;
3644 PySequenceMethods *sq = tp->tp_as_sequence;
3645
3646 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3647 int ilow = 0, ihigh = INT_MAX;
3648 if (!_PyEval_SliceIndex(v, &ilow))
3649 return NULL;
3650 if (!_PyEval_SliceIndex(w, &ihigh))
3651 return NULL;
3652 return PySequence_GetSlice(u, ilow, ihigh);
3653 }
3654 else {
3655 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00003656 if (slice != NULL) {
3657 PyObject *res = PyObject_GetItem(u, slice);
3658 Py_DECREF(slice);
3659 return res;
3660 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00003661 else
3662 return NULL;
3663 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003664}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003665
3666static int
Guido van Rossumac7be682001-01-17 15:42:30 +00003667assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
3668 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003669{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003670 PyTypeObject *tp = u->ob_type;
3671 PySequenceMethods *sq = tp->tp_as_sequence;
3672
3673 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3674 int ilow = 0, ihigh = INT_MAX;
3675 if (!_PyEval_SliceIndex(v, &ilow))
3676 return -1;
3677 if (!_PyEval_SliceIndex(w, &ihigh))
3678 return -1;
3679 if (x == NULL)
3680 return PySequence_DelSlice(u, ilow, ihigh);
3681 else
3682 return PySequence_SetSlice(u, ilow, ihigh, x);
3683 }
3684 else {
3685 PyObject *slice = PySlice_New(v, w, NULL);
3686 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00003687 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003688 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00003689 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00003690 else
Guido van Rossum354797c2001-12-03 19:45:06 +00003691 res = PyObject_DelItem(u, slice);
3692 Py_DECREF(slice);
3693 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003694 }
3695 else
3696 return -1;
3697 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003698}
3699
Guido van Rossumb209a111997-04-29 18:18:01 +00003700static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003701cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003702{
Guido van Rossumac7be682001-01-17 15:42:30 +00003703 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003704 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00003705 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003706 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003707 break;
3708 case PyCmp_IS_NOT:
3709 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003710 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003711 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003712 res = PySequence_Contains(w, v);
3713 if (res < 0)
3714 return NULL;
3715 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003716 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00003717 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003718 if (res < 0)
3719 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003720 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003721 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003722 case PyCmp_EXC_MATCH:
Barry Warsaw4249f541997-08-22 21:26:19 +00003723 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003724 break;
3725 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00003726 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003727 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003728 v = res ? Py_True : Py_False;
3729 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003730 return v;
3731}
3732
Thomas Wouters52152252000-08-17 22:55:00 +00003733static PyObject *
3734import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003735{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003736 PyObject *x;
3737
3738 x = PyObject_GetAttr(v, name);
3739 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00003740 PyErr_Format(PyExc_ImportError,
3741 "cannot import name %.230s",
3742 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003743 }
Thomas Wouters52152252000-08-17 22:55:00 +00003744 return x;
3745}
Guido van Rossumac7be682001-01-17 15:42:30 +00003746
Thomas Wouters52152252000-08-17 22:55:00 +00003747static int
3748import_all_from(PyObject *locals, PyObject *v)
3749{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003750 PyObject *all = PyObject_GetAttrString(v, "__all__");
3751 PyObject *dict, *name, *value;
3752 int skip_leading_underscores = 0;
3753 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00003754
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003755 if (all == NULL) {
3756 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3757 return -1; /* Unexpected error */
3758 PyErr_Clear();
3759 dict = PyObject_GetAttrString(v, "__dict__");
3760 if (dict == NULL) {
3761 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3762 return -1;
3763 PyErr_SetString(PyExc_ImportError,
3764 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00003765 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003766 }
3767 all = PyMapping_Keys(dict);
3768 Py_DECREF(dict);
3769 if (all == NULL)
3770 return -1;
3771 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00003772 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003773
3774 for (pos = 0, err = 0; ; pos++) {
3775 name = PySequence_GetItem(all, pos);
3776 if (name == NULL) {
3777 if (!PyErr_ExceptionMatches(PyExc_IndexError))
3778 err = -1;
3779 else
3780 PyErr_Clear();
3781 break;
3782 }
3783 if (skip_leading_underscores &&
3784 PyString_Check(name) &&
3785 PyString_AS_STRING(name)[0] == '_')
3786 {
3787 Py_DECREF(name);
3788 continue;
3789 }
3790 value = PyObject_GetAttr(v, name);
3791 if (value == NULL)
3792 err = -1;
3793 else
3794 err = PyDict_SetItem(locals, name, value);
3795 Py_DECREF(name);
3796 Py_XDECREF(value);
3797 if (err != 0)
3798 break;
3799 }
3800 Py_DECREF(all);
3801 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00003802}
3803
Guido van Rossumb209a111997-04-29 18:18:01 +00003804static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003805build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003806{
Guido van Rossum7851eea2001-09-12 19:19:18 +00003807 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003808
3809 if (PyDict_Check(methods))
3810 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00003811 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00003812 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00003813 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
3814 base = PyTuple_GET_ITEM(bases, 0);
3815 metaclass = PyObject_GetAttrString(base, "__class__");
3816 if (metaclass == NULL) {
3817 PyErr_Clear();
3818 metaclass = (PyObject *)base->ob_type;
3819 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00003820 }
3821 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00003822 else {
3823 PyObject *g = PyEval_GetGlobals();
3824 if (g != NULL && PyDict_Check(g))
3825 metaclass = PyDict_GetItemString(g, "__metaclass__");
3826 if (metaclass == NULL)
3827 metaclass = (PyObject *) &PyClass_Type;
3828 Py_INCREF(metaclass);
3829 }
3830 result = PyObject_CallFunction(metaclass, "OOO", name, bases, methods);
3831 Py_DECREF(metaclass);
3832 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00003833}
3834
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003835static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003836exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
3837 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003838{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003839 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00003840 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003841 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003842
Guido van Rossumb209a111997-04-29 18:18:01 +00003843 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
3844 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003845 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00003846 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003847 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00003848 locals = PyTuple_GetItem(prog, 2);
3849 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003850 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003851 if (globals == Py_None) {
3852 globals = PyEval_GetGlobals();
3853 if (locals == Py_None) {
3854 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00003855 plain = 1;
3856 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003857 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003858 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003859 locals = globals;
Guido van Rossumb209a111997-04-29 18:18:01 +00003860 if (!PyString_Check(prog) &&
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00003861 !PyUnicode_Check(prog) &&
Guido van Rossumb209a111997-04-29 18:18:01 +00003862 !PyCode_Check(prog) &&
3863 !PyFile_Check(prog)) {
3864 PyErr_SetString(PyExc_TypeError,
Guido van Rossumac7be682001-01-17 15:42:30 +00003865 "exec: arg 1 must be a string, file, or code object");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003866 return -1;
3867 }
Fred Drake661ea262000-10-24 19:57:45 +00003868 if (!PyDict_Check(globals)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003869 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003870 "exec: arg 2 must be a dictionary or None");
3871 return -1;
3872 }
3873 if (!PyDict_Check(locals)) {
3874 PyErr_SetString(PyExc_TypeError,
3875 "exec: arg 3 must be a dictionary or None");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003876 return -1;
3877 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003878 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00003879 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00003880 if (PyCode_Check(prog)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +00003881 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
3882 PyErr_SetString(PyExc_TypeError,
3883 "code object passed to exec may not contain free variables");
3884 return -1;
3885 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00003886 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003887 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00003888 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003889 FILE *fp = PyFile_AsFile(prog);
3890 char *name = PyString_AsString(PyFile_Name(prog));
Tim Peters5ba58662001-07-16 02:29:45 +00003891 PyCompilerFlags cf;
3892 cf.cf_flags = 0;
3893 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00003894 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
3895 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00003896 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00003897 v = PyRun_File(fp, name, Py_file_input, globals,
3898 locals);
Guido van Rossuma400d8a2000-01-12 22:45:54 +00003899 }
3900 else {
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00003901 char *str;
Tim Peters5ba58662001-07-16 02:29:45 +00003902 PyCompilerFlags cf;
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00003903 if (PyString_AsStringAndSize(prog, &str, NULL))
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003904 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00003905 cf.cf_flags = 0;
3906 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00003907 v = PyRun_StringFlags(str, Py_file_input, globals,
3908 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00003909 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00003910 v = PyRun_String(str, Py_file_input, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003911 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00003912 if (plain)
3913 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003914 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003915 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00003916 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003917 return 0;
3918}
Guido van Rossum24c13741995-02-14 09:42:43 +00003919
Guido van Rossumac7be682001-01-17 15:42:30 +00003920static void
Paul Prescode68140d2000-08-30 20:25:01 +00003921format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
3922{
3923 char *obj_str;
3924
3925 if (!obj)
3926 return;
3927
3928 obj_str = PyString_AsString(obj);
3929 if (!obj_str)
3930 return;
3931
3932 PyErr_Format(exc, format_str, obj_str);
3933}
Guido van Rossum950361c1997-01-24 13:49:28 +00003934
3935#ifdef DYNAMIC_EXECUTION_PROFILE
3936
Skip Montanarof118cb12001-10-15 20:51:38 +00003937static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003938getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00003939{
3940 int i;
3941 PyObject *l = PyList_New(256);
3942 if (l == NULL) return NULL;
3943 for (i = 0; i < 256; i++) {
3944 PyObject *x = PyInt_FromLong(a[i]);
3945 if (x == NULL) {
3946 Py_DECREF(l);
3947 return NULL;
3948 }
3949 PyList_SetItem(l, i, x);
3950 }
3951 for (i = 0; i < 256; i++)
3952 a[i] = 0;
3953 return l;
3954}
3955
3956PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003957_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00003958{
3959#ifndef DXPAIRS
3960 return getarray(dxp);
3961#else
3962 int i;
3963 PyObject *l = PyList_New(257);
3964 if (l == NULL) return NULL;
3965 for (i = 0; i < 257; i++) {
3966 PyObject *x = getarray(dxpairs[i]);
3967 if (x == NULL) {
3968 Py_DECREF(l);
3969 return NULL;
3970 }
3971 PyList_SetItem(l, i, x);
3972 }
3973 return l;
3974#endif
3975}
3976
3977#endif