blob: ef995948fb8e2891cdf6da153d1adabcfe599fd7 [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 Hylton52820442001-01-03 23:52:36 +000036static PyObject *fast_function(PyObject *, PyObject ***, int, int, int);
37static PyObject *fast_cfunction(PyObject *, PyObject ***, int);
38static 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 *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000054static PyObject *loop_subscript(PyObject *, PyObject *);
55static PyObject *apply_slice(PyObject *, PyObject *, PyObject *);
56static int assign_slice(PyObject *, PyObject *,
57 PyObject *, PyObject *);
58static PyObject *cmp_outcome(int, PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +000059static PyObject *import_from(PyObject *, PyObject *);
60static int import_all_from(PyObject *, PyObject *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000061static PyObject *build_class(PyObject *, PyObject *, PyObject *);
62static int exec_statement(PyFrameObject *,
63 PyObject *, PyObject *, PyObject *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000064static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *);
65static void reset_exc_info(PyThreadState *);
Paul Prescode68140d2000-08-30 20:25:01 +000066static void format_exc_check_arg(PyObject *, char *, PyObject *);
Guido van Rossum374a9221991-04-04 10:40:29 +000067
Paul Prescode68140d2000-08-30 20:25:01 +000068#define NAME_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +000069 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +000070#define GLOBAL_NAME_ERROR_MSG \
71 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000072#define UNBOUNDLOCAL_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +000073 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000074#define UNBOUNDFREE_ERROR_MSG \
75 "free variable '%.200s' referenced before assignment" \
76 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000077
Guido van Rossum950361c1997-01-24 13:49:28 +000078/* Dynamic execution profile */
79#ifdef DYNAMIC_EXECUTION_PROFILE
80#ifdef DXPAIRS
81static long dxpairs[257][256];
82#define dxp dxpairs[256]
83#else
84static long dxp[256];
85#endif
86#endif
87
Tim Peters5ca576e2001-06-18 22:08:13 +000088staticforward PyTypeObject gentype;
89
90typedef struct {
91 PyObject_HEAD
Tim Petersd8e1c9e2001-06-26 20:58:58 +000092 /* The gi_ prefix is intended to remind of generator-iterator. */
93
94 PyFrameObject *gi_frame;
95
Tim Peterse77f2e22001-06-26 22:24:51 +000096 /* True if generator is being executed. */
97 int gi_running;
Tim Peters5ca576e2001-06-18 22:08:13 +000098} genobject;
99
100static PyObject *
101gen_new(PyFrameObject *f)
102{
103 genobject *gen = PyObject_New(genobject, &gentype);
104 if (gen == NULL) {
105 Py_DECREF(f);
106 return NULL;
107 }
Tim Petersd8e1c9e2001-06-26 20:58:58 +0000108 gen->gi_frame = f;
109 gen->gi_running = 0;
Neil Schemenauerf8c7c202001-07-12 13:27:49 +0000110 PyObject_GC_Init(gen);
Tim Peters5ca576e2001-06-18 22:08:13 +0000111 return (PyObject *)gen;
112}
113
Neil Schemenauerf8c7c202001-07-12 13:27:49 +0000114static int
115gen_traverse(genobject *gen, visitproc visit, void *arg)
116{
117 return visit((PyObject *)gen->gi_frame, arg);
118}
119
Tim Peters5ca576e2001-06-18 22:08:13 +0000120static void
121gen_dealloc(genobject *gen)
122{
Neil Schemenauerf8c7c202001-07-12 13:27:49 +0000123 PyObject_GC_Fini(gen);
Tim Petersd8e1c9e2001-06-26 20:58:58 +0000124 Py_DECREF(gen->gi_frame);
Neil Schemenauerf8c7c202001-07-12 13:27:49 +0000125 PyObject_Del(gen);
Tim Peters5ca576e2001-06-18 22:08:13 +0000126}
127
128static PyObject *
129gen_iternext(genobject *gen)
130{
Neil Schemenauer2b13ce82001-06-21 02:41:10 +0000131 PyThreadState *tstate = PyThreadState_GET();
Tim Petersd8e1c9e2001-06-26 20:58:58 +0000132 PyFrameObject *f = gen->gi_frame;
Tim Peters5ca576e2001-06-18 22:08:13 +0000133 PyObject *result;
134
Tim Petersd8e1c9e2001-06-26 20:58:58 +0000135 if (gen->gi_running) {
Tim Peters5ca576e2001-06-18 22:08:13 +0000136 PyErr_SetString(PyExc_ValueError,
137 "generator already executing");
138 return NULL;
139 }
Tim Peters8c963692001-06-23 05:26:56 +0000140 if (f->f_stacktop == NULL)
Tim Peters5ca576e2001-06-18 22:08:13 +0000141 return NULL;
Neil Schemenauer2b13ce82001-06-21 02:41:10 +0000142
143 /* Generators always return to their most recent caller, not
144 * necessarily their creator. */
Tim Peters5eb4b872001-06-23 05:47:56 +0000145 Py_XINCREF(tstate->frame);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +0000146 assert(f->f_back == NULL);
147 f->f_back = tstate->frame;
148
Tim Petersd8e1c9e2001-06-26 20:58:58 +0000149 gen->gi_running = 1;
Tim Peters5ca576e2001-06-18 22:08:13 +0000150 result = eval_frame(f);
Tim Petersd8e1c9e2001-06-26 20:58:58 +0000151 gen->gi_running = 0;
Neil Schemenauer2b13ce82001-06-21 02:41:10 +0000152
153 /* Don't keep the reference to f_back any longer than necessary. It
154 * may keep a chain of frames alive or it could create a reference
155 * cycle. */
Tim Peters5eb4b872001-06-23 05:47:56 +0000156 Py_XDECREF(f->f_back);
Tim Peters6302ec62001-06-20 06:57:32 +0000157 f->f_back = NULL;
Neil Schemenauer2b13ce82001-06-21 02:41:10 +0000158
Tim Petersad1a18b2001-06-23 06:19:16 +0000159 /* If the generator just returned (as opposed to yielding), signal
160 * that the generator is exhausted. */
161 if (result == Py_None && f->f_stacktop == NULL) {
162 Py_DECREF(result);
163 result = NULL;
164 }
165
Neil Schemenauer2b13ce82001-06-21 02:41:10 +0000166 return result;
Tim Peters5ca576e2001-06-18 22:08:13 +0000167}
168
169static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000170gen_next(genobject *gen)
Tim Peters5ca576e2001-06-18 22:08:13 +0000171{
172 PyObject *result;
173
Neil Schemenauer2b13ce82001-06-21 02:41:10 +0000174 result = gen_iternext(gen);
Tim Peters5ca576e2001-06-18 22:08:13 +0000175
Neil Schemenauer2b13ce82001-06-21 02:41:10 +0000176 if (result == NULL && !PyErr_Occurred()) {
Tim Peters5ca576e2001-06-18 22:08:13 +0000177 PyErr_SetObject(PyExc_StopIteration, Py_None);
178 return NULL;
Neil Schemenauer2b13ce82001-06-21 02:41:10 +0000179 }
Tim Peters5ca576e2001-06-18 22:08:13 +0000180
181 return result;
182}
183
184static PyObject *
185gen_getiter(PyObject *gen)
186{
187 Py_INCREF(gen);
188 return gen;
189}
190
191static struct PyMethodDef gen_methods[] = {
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000192 {"next", (PyCFunction)gen_next, METH_NOARGS,
Tim Peterse77f2e22001-06-26 22:24:51 +0000193 "next() -- get the next value, or raise StopIteration"},
Tim Peters5ca576e2001-06-18 22:08:13 +0000194 {NULL, NULL} /* Sentinel */
195};
196
Guido van Rossum6f799372001-09-20 20:46:19 +0000197static PyMemberDef gen_memberlist[] = {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000198 {"gi_frame", T_OBJECT, offsetof(genobject, gi_frame), RO},
199 {"gi_running", T_INT, offsetof(genobject, gi_running), RO},
200 {NULL} /* Sentinel */
201};
Tim Peters5ca576e2001-06-18 22:08:13 +0000202
203statichere PyTypeObject gentype = {
204 PyObject_HEAD_INIT(&PyType_Type)
205 0, /* ob_size */
206 "generator", /* tp_name */
Neil Schemenauerf8c7c202001-07-12 13:27:49 +0000207 sizeof(genobject) + PyGC_HEAD_SIZE, /* tp_basicsize */
Tim Peters5ca576e2001-06-18 22:08:13 +0000208 0, /* tp_itemsize */
209 /* methods */
210 (destructor)gen_dealloc, /* tp_dealloc */
211 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000212 0, /* tp_getattr */
Tim Peters5ca576e2001-06-18 22:08:13 +0000213 0, /* tp_setattr */
214 0, /* tp_compare */
215 0, /* tp_repr */
216 0, /* tp_as_number */
217 0, /* tp_as_sequence */
218 0, /* tp_as_mapping */
219 0, /* tp_hash */
220 0, /* tp_call */
221 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000222 PyObject_GenericGetAttr, /* tp_getattro */
Tim Peters5ca576e2001-06-18 22:08:13 +0000223 0, /* tp_setattro */
224 0, /* tp_as_buffer */
Neil Schemenauerf8c7c202001-07-12 13:27:49 +0000225 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /* tp_flags */
Tim Peters5ca576e2001-06-18 22:08:13 +0000226 0, /* tp_doc */
Neil Schemenauerf8c7c202001-07-12 13:27:49 +0000227 (traverseproc)gen_traverse, /* tp_traverse */
Tim Peters5ca576e2001-06-18 22:08:13 +0000228 0, /* tp_clear */
229 0, /* tp_richcompare */
230 0, /* tp_weaklistoffset */
231 (getiterfunc)gen_getiter, /* tp_iter */
232 (iternextfunc)gen_iternext, /* tp_iternext */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000233 gen_methods, /* tp_methods */
234 gen_memberlist, /* tp_members */
235 0, /* tp_getset */
236 0, /* tp_base */
237 0, /* tp_dict */
Tim Peters5ca576e2001-06-18 22:08:13 +0000238};
239
240
Guido van Rossume59214e1994-08-30 08:01:59 +0000241#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000242
Guido van Rossum2571cc81999-04-07 16:07:23 +0000243#ifndef DONT_HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000244#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000245#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000246#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000247
Guido van Rossuma027efa1997-05-05 20:56:21 +0000248extern int _PyThread_Started; /* Flag for Py_Exit */
249
Guido van Rossum65d5b571998-12-21 19:32:43 +0000250static PyThread_type_lock interpreter_lock = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000251static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000252
253void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000254PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000255{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000256 if (interpreter_lock)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000257 return;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000258 _PyThread_Started = 1;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000259 interpreter_lock = PyThread_allocate_lock();
260 PyThread_acquire_lock(interpreter_lock, 1);
261 main_thread = PyThread_get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000262}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000263
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000264void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000265PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000266{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000267 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000268}
269
270void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000271PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000272{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000273 PyThread_release_lock(interpreter_lock);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000274}
275
276void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000277PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000278{
279 if (tstate == NULL)
280 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000281 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000282 if (PyThreadState_Swap(tstate) != NULL)
283 Py_FatalError(
284 "PyEval_AcquireThread: non-NULL old thread state");
285}
286
287void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000288PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000289{
290 if (tstate == NULL)
291 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
292 if (PyThreadState_Swap(NULL) != tstate)
293 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000294 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000295}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000296
297/* This function is called from PyOS_AfterFork to ensure that newly
298 created child processes don't hold locks referring to threads which
299 are not running in the child process. (This could also be done using
300 pthread_atfork mechanism, at least for the pthreads implementation.) */
301
302void
303PyEval_ReInitThreads(void)
304{
305 if (!interpreter_lock)
306 return;
307 /*XXX Can't use PyThread_free_lock here because it does too
308 much error-checking. Doing this cleanly would require
309 adding a new function to each thread_*.h. Instead, just
310 create a new lock and waste a little bit of memory */
311 interpreter_lock = PyThread_allocate_lock();
312 PyThread_acquire_lock(interpreter_lock, 1);
313 main_thread = PyThread_get_thread_ident();
314}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000315#endif
316
Guido van Rossumff4949e1992-08-05 19:58:53 +0000317/* Functions save_thread and restore_thread are always defined so
318 dynamically loaded modules needn't be compiled separately for use
319 with and without threads: */
320
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000321PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000322PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000323{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000324 PyThreadState *tstate = PyThreadState_Swap(NULL);
325 if (tstate == NULL)
326 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000327#ifdef WITH_THREAD
Guido van Rossumb74eca91997-09-30 22:03:16 +0000328 if (interpreter_lock)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000329 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000330#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000331 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000332}
333
334void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000335PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000336{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000337 if (tstate == NULL)
338 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000339#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000340 if (interpreter_lock) {
Guido van Rossumb74eca91997-09-30 22:03:16 +0000341 int err = errno;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000342 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000343 errno = err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000344 }
345#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000346 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000347}
348
349
Guido van Rossuma9672091994-09-14 13:31:22 +0000350/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
351 signal handlers or Mac I/O completion routines) can schedule calls
352 to a function to be called synchronously.
353 The synchronous function is called with one void* argument.
354 It should return 0 for success or -1 for failure -- failure should
355 be accompanied by an exception.
356
357 If registry succeeds, the registry function returns 0; if it fails
358 (e.g. due to too many pending calls) it returns -1 (without setting
359 an exception condition).
360
361 Note that because registry may occur from within signal handlers,
362 or other asynchronous events, calling malloc() is unsafe!
363
364#ifdef WITH_THREAD
365 Any thread can schedule pending calls, but only the main thread
366 will execute them.
367#endif
368
369 XXX WARNING! ASYNCHRONOUSLY EXECUTING CODE!
370 There are two possible race conditions:
371 (1) nested asynchronous registry calls;
372 (2) registry calls made while pending calls are being processed.
373 While (1) is very unlikely, (2) is a real possibility.
374 The current code is safe against (2), but not against (1).
375 The safety against (2) is derived from the fact that only one
376 thread (the main thread) ever takes things out of the queue.
Guido van Rossuma9672091994-09-14 13:31:22 +0000377
Guido van Rossuma027efa1997-05-05 20:56:21 +0000378 XXX Darn! With the advent of thread state, we should have an array
379 of pending calls per thread in the thread state! Later...
380*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000381
Guido van Rossuma9672091994-09-14 13:31:22 +0000382#define NPENDINGCALLS 32
383static struct {
Thomas Wouters334fb892000-07-25 12:56:38 +0000384 int (*func)(void *);
385 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000386} pendingcalls[NPENDINGCALLS];
387static volatile int pendingfirst = 0;
388static volatile int pendinglast = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000389static volatile int things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000390
391int
Thomas Wouters334fb892000-07-25 12:56:38 +0000392Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000393{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000394 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000395 int i, j;
396 /* XXX Begin critical section */
397 /* XXX If you want this to be safe against nested
398 XXX asynchronous calls, you'll have to work harder! */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000399 if (busy)
400 return -1;
401 busy = 1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000402 i = pendinglast;
403 j = (i + 1) % NPENDINGCALLS;
404 if (j == pendingfirst)
405 return -1; /* Queue full */
406 pendingcalls[i].func = func;
407 pendingcalls[i].arg = arg;
408 pendinglast = j;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000409 things_to_do = 1; /* Signal main loop */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000410 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000411 /* XXX End critical section */
412 return 0;
413}
414
Guido van Rossum180d7b41994-09-29 09:45:57 +0000415int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000416Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000417{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000418 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000419#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000420 if (main_thread && PyThread_get_thread_ident() != main_thread)
Guido van Rossuma9672091994-09-14 13:31:22 +0000421 return 0;
422#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000423 if (busy)
Guido van Rossum180d7b41994-09-29 09:45:57 +0000424 return 0;
425 busy = 1;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000426 things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000427 for (;;) {
428 int i;
Thomas Wouters334fb892000-07-25 12:56:38 +0000429 int (*func)(void *);
430 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000431 i = pendingfirst;
432 if (i == pendinglast)
433 break; /* Queue empty */
434 func = pendingcalls[i].func;
435 arg = pendingcalls[i].arg;
436 pendingfirst = (i + 1) % NPENDINGCALLS;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000437 if (func(arg) < 0) {
438 busy = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000439 things_to_do = 1; /* We're not done yet */
Guido van Rossuma9672091994-09-14 13:31:22 +0000440 return -1;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000441 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000442 }
Guido van Rossum180d7b41994-09-29 09:45:57 +0000443 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000444 return 0;
445}
446
447
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000448/* The interpreter's recursion limit */
449
Guido van Rossum349ff6f2000-09-01 01:52:08 +0000450static int recursion_limit = 1000;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000451
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000452int
453Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000454{
455 return recursion_limit;
456}
457
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000458void
459Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000460{
461 recursion_limit = new_limit;
462}
463
Guido van Rossum374a9221991-04-04 10:40:29 +0000464/* Status code for main loop (reason for stack unwind) */
465
466enum why_code {
467 WHY_NOT, /* No error */
468 WHY_EXCEPTION, /* Exception occurred */
469 WHY_RERAISE, /* Exception re-raised by 'finally' */
470 WHY_RETURN, /* 'return' statement */
Jeremy Hylton3faa52e2001-02-01 22:48:12 +0000471 WHY_BREAK, /* 'break' statement */
Tim Peters5ca576e2001-06-18 22:08:13 +0000472 WHY_CONTINUE, /* 'continue' statement */
473 WHY_YIELD, /* 'yield' operator */
Guido van Rossum374a9221991-04-04 10:40:29 +0000474};
475
Tim Petersdbd9ba62000-07-09 03:09:57 +0000476static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
Tim Petersd6d010b2001-06-21 02:49:55 +0000477static int unpack_iterable(PyObject *, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000478
Guido van Rossum374a9221991-04-04 10:40:29 +0000479
Guido van Rossumb209a111997-04-29 18:18:01 +0000480PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000481PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000482{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000483 return PyEval_EvalCodeEx(co,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000484 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000485 (PyObject **)NULL, 0,
486 (PyObject **)NULL, 0,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000487 (PyObject **)NULL, 0,
488 NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000489}
490
491
492/* Interpreter main loop */
493
Tim Peters6d6c1a32001-08-02 04:15:00 +0000494static PyObject *
Tim Peters5ca576e2001-06-18 22:08:13 +0000495eval_frame(PyFrameObject *f)
Guido van Rossum374a9221991-04-04 10:40:29 +0000496{
Guido van Rossum950361c1997-01-24 13:49:28 +0000497#ifdef DXPAIRS
498 int lastopcode = 0;
499#endif
Jeremy Hylton52820442001-01-03 23:52:36 +0000500 PyObject **stack_pointer;
Guido van Rossum374a9221991-04-04 10:40:29 +0000501 register unsigned char *next_instr;
Moshe Zadkaaa39a7e2000-08-07 06:34:45 +0000502 register int opcode=0; /* Current opcode */
503 register int oparg=0; /* Current opcode argument, if any */
Guido van Rossum374a9221991-04-04 10:40:29 +0000504 register enum why_code why; /* Reason for block stack unwind */
505 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000506 register PyObject *x; /* Result object -- NULL if error */
507 register PyObject *v; /* Temporary objects popped off stack */
508 register PyObject *w;
509 register PyObject *u;
510 register PyObject *t;
Barry Warsaw23c9ec82000-08-21 15:44:01 +0000511 register PyObject *stream = NULL; /* for PRINT opcodes */
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000512 register PyObject **fastlocals, **freevars;
Guido van Rossum014518f1998-11-23 21:09:51 +0000513 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000514 PyThreadState *tstate = PyThreadState_GET();
Tim Peters5ca576e2001-06-18 22:08:13 +0000515 PyCodeObject *co;
Guido van Rossumd076c731998-10-07 19:42:25 +0000516 unsigned char *first_instr;
Guido van Rossum96a42c81992-01-12 02:29:51 +0000517#ifdef LLTRACE
Guido van Rossumacbe8da1993-04-15 15:33:52 +0000518 int lltrace;
Guido van Rossum374a9221991-04-04 10:40:29 +0000519#endif
Guido van Rossum408027e1996-12-30 16:17:54 +0000520#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000521 /* Make it easier to find out where we are with a debugger */
Tim Peters5ca576e2001-06-18 22:08:13 +0000522 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000523#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000524
525/* Code access macros */
526
527#define GETCONST(i) Getconst(f, i)
528#define GETNAME(i) Getname(f, i)
529#define GETNAMEV(i) Getnamev(f, i)
Guido van Rossumd076c731998-10-07 19:42:25 +0000530#define INSTR_OFFSET() (next_instr - first_instr)
Guido van Rossum374a9221991-04-04 10:40:29 +0000531#define NEXTOP() (*next_instr++)
532#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Guido van Rossumd076c731998-10-07 19:42:25 +0000533#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000534#define JUMPBY(x) (next_instr += (x))
535
536/* Stack manipulation macros */
537
538#define STACK_LEVEL() (stack_pointer - f->f_valuestack)
539#define EMPTY() (STACK_LEVEL() == 0)
540#define TOP() (stack_pointer[-1])
541#define BASIC_PUSH(v) (*stack_pointer++ = (v))
542#define BASIC_POP() (*--stack_pointer)
543
Guido van Rossum96a42c81992-01-12 02:29:51 +0000544#ifdef LLTRACE
Fred Drakede26cfc2001-10-13 06:11:28 +0000545#define PUSH(v) (void)(BASIC_PUSH(v), lltrace && prtrace(TOP(), "push"))
546#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), BASIC_POP())
Guido van Rossum374a9221991-04-04 10:40:29 +0000547#else
548#define PUSH(v) BASIC_PUSH(v)
549#define POP() BASIC_POP()
550#endif
551
Guido van Rossum681d79a1995-07-18 14:51:37 +0000552/* Local variable macros */
553
554#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumb209a111997-04-29 18:18:01 +0000555#define SETLOCAL(i, value) do { Py_XDECREF(GETLOCAL(i)); \
Guido van Rossum681d79a1995-07-18 14:51:37 +0000556 GETLOCAL(i) = value; } while (0)
557
Guido van Rossuma027efa1997-05-05 20:56:21 +0000558/* Start of code */
559
Tim Peters5ca576e2001-06-18 22:08:13 +0000560 if (f == NULL)
561 return NULL;
562
Guido van Rossum8861b741996-07-30 16:49:37 +0000563#ifdef USE_STACKCHECK
Guido van Rossuma027efa1997-05-05 20:56:21 +0000564 if (tstate->recursion_depth%10 == 0 && PyOS_CheckStack()) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000565 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
Guido van Rossum8861b741996-07-30 16:49:37 +0000566 return NULL;
567 }
568#endif
569
Tim Peters5ca576e2001-06-18 22:08:13 +0000570 /* push frame */
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000571 if (++tstate->recursion_depth > recursion_limit) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000572 --tstate->recursion_depth;
573 PyErr_SetString(PyExc_RuntimeError,
Fred Drake661ea262000-10-24 19:57:45 +0000574 "maximum recursion depth exceeded");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000575 tstate->frame = f->f_back;
Guido van Rossum8861b741996-07-30 16:49:37 +0000576 return NULL;
577 }
578
Tim Peters5ca576e2001-06-18 22:08:13 +0000579 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000580 co = f->f_code;
581 fastlocals = f->f_localsplus;
582 freevars = f->f_localsplus + f->f_nlocals;
Guido van Rossumd076c731998-10-07 19:42:25 +0000583 _PyCode_GETCODEPTR(co, &first_instr);
Tim Peters5ca576e2001-06-18 22:08:13 +0000584 next_instr = first_instr + f->f_lasti;
Tim Peters8c963692001-06-23 05:26:56 +0000585 stack_pointer = f->f_stacktop;
586 assert(stack_pointer != NULL);
587 f->f_stacktop = NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +0000588
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000589 if (tstate->use_tracing) {
590 if (tstate->c_tracefunc != NULL) {
591 /* tstate->c_tracefunc, if defined, is a
592 function that will be called on *every* entry
593 to a code block. Its return value, if not
594 None, is a function that will be called at
595 the start of each executed line of code.
596 (Actually, the function must return itself
597 in order to continue tracing.) The trace
598 functions are called with three arguments:
599 a pointer to the current frame, a string
600 indicating why the function is called, and
601 an argument which depends on the situation.
602 The global trace function is also called
603 whenever an exception is detected. */
604 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
605 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000606 /* Trace function raised an error */
607 return NULL;
608 }
609 }
610 if (tstate->c_profilefunc != NULL) {
611 /* Similar for c_profilefunc, except it needn't
612 return itself and isn't called for "line" events */
613 if (call_trace(tstate->c_profilefunc,
614 tstate->c_profileobj,
615 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000616 /* Profile function raised an error */
617 return NULL;
618 }
619 }
620 }
621
Tim Peters5ca576e2001-06-18 22:08:13 +0000622#ifdef LLTRACE
623 lltrace = PyDict_GetItemString(f->f_globals,"__lltrace__") != NULL;
624#endif
625#if defined(Py_DEBUG) || defined(LLTRACE)
626 filename = PyString_AsString(co->co_filename);
627#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000628
Guido van Rossum374a9221991-04-04 10:40:29 +0000629 why = WHY_NOT;
630 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +0000631 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +0000632 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +0000633
Guido van Rossum374a9221991-04-04 10:40:29 +0000634 for (;;) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000635 /* Do periodic things. Doing this every time through
636 the loop would add too much overhead, so we do it
637 only every Nth instruction. We also do it if
638 ``things_to_do'' is set, i.e. when an asynchronous
639 event needs attention (e.g. a signal handler or
640 async I/O handler); see Py_AddPendingCall() and
641 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000642
Guido van Rossuma027efa1997-05-05 20:56:21 +0000643 if (things_to_do || --tstate->ticker < 0) {
Guido van Rossum25ce5661997-08-02 03:10:38 +0000644 tstate->ticker = tstate->interp->checkinterval;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000645 if (things_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +0000646 if (Py_MakePendingCalls() < 0) {
647 why = WHY_EXCEPTION;
648 goto on_error;
649 }
650 }
Guido van Rossumdf0d00e1997-05-20 15:57:49 +0000651#if !defined(HAVE_SIGNAL_H) || defined(macintosh)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000652 /* If we have true signals, the signal handler
653 will call Py_AddPendingCall() so we don't
654 have to call sigcheck(). On the Mac and
655 DOS, alas, we have to call it. */
Guido van Rossumb209a111997-04-29 18:18:01 +0000656 if (PyErr_CheckSignals()) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000657 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +0000658 goto on_error;
659 }
Guido van Rossum70d44781997-01-21 06:15:24 +0000660#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000661
Guido van Rossume59214e1994-08-30 08:01:59 +0000662#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000663 if (interpreter_lock) {
664 /* Give another thread a chance */
665
Guido van Rossum25ce5661997-08-02 03:10:38 +0000666 if (PyThreadState_Swap(NULL) != tstate)
667 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000668 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000669
670 /* Other threads may run now */
671
Guido van Rossum65d5b571998-12-21 19:32:43 +0000672 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000673 if (PyThreadState_Swap(tstate) != NULL)
674 Py_FatalError("ceval: orphan tstate");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000675 }
676#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000677 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000678
Guido van Rossum374a9221991-04-04 10:40:29 +0000679 /* Extract opcode and argument */
Guido van Rossum99bec951992-09-03 20:29:45 +0000680
Guido van Rossum408027e1996-12-30 16:17:54 +0000681#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum99bec951992-09-03 20:29:45 +0000682 f->f_lasti = INSTR_OFFSET();
683#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000684
Guido van Rossum374a9221991-04-04 10:40:29 +0000685 opcode = NEXTOP();
686 if (HAS_ARG(opcode))
687 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +0000688 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +0000689#ifdef DYNAMIC_EXECUTION_PROFILE
690#ifdef DXPAIRS
691 dxpairs[lastopcode][opcode]++;
692 lastopcode = opcode;
693#endif
694 dxp[opcode]++;
695#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000696
Guido van Rossum96a42c81992-01-12 02:29:51 +0000697#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +0000698 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +0000699
Guido van Rossum96a42c81992-01-12 02:29:51 +0000700 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000701 if (HAS_ARG(opcode)) {
702 printf("%d: %d, %d\n",
703 (int) (INSTR_OFFSET() - 3),
704 opcode, oparg);
705 }
706 else {
707 printf("%d: %d\n",
708 (int) (INSTR_OFFSET() - 1), opcode);
709 }
710 }
711#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000712 /* Main switch on opcode */
Jeremy Hylton52820442001-01-03 23:52:36 +0000713
Guido van Rossum374a9221991-04-04 10:40:29 +0000714 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +0000715
Guido van Rossum374a9221991-04-04 10:40:29 +0000716 /* BEWARE!
717 It is essential that any operation that fails sets either
718 x to NULL, err to nonzero, or why to anything but WHY_NOT,
719 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000720
Guido van Rossum374a9221991-04-04 10:40:29 +0000721 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000722
Guido van Rossum374a9221991-04-04 10:40:29 +0000723 case POP_TOP:
724 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000725 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000726 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +0000727
Guido van Rossum374a9221991-04-04 10:40:29 +0000728 case ROT_TWO:
729 v = POP();
730 w = POP();
731 PUSH(v);
732 PUSH(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000733 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +0000734
Guido van Rossum374a9221991-04-04 10:40:29 +0000735 case ROT_THREE:
736 v = POP();
737 w = POP();
738 x = POP();
739 PUSH(v);
740 PUSH(x);
741 PUSH(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000742 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +0000743
Thomas Wouters434d0822000-08-24 20:11:32 +0000744 case ROT_FOUR:
745 u = POP();
746 v = POP();
747 w = POP();
748 x = POP();
749 PUSH(u);
750 PUSH(x);
751 PUSH(w);
752 PUSH(v);
753 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +0000754
Guido van Rossum374a9221991-04-04 10:40:29 +0000755 case DUP_TOP:
756 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000757 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000758 PUSH(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000759 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +0000760
Thomas Wouters434d0822000-08-24 20:11:32 +0000761 case DUP_TOPX:
762 switch (oparg) {
Thomas Wouters434d0822000-08-24 20:11:32 +0000763 case 1:
Tim Peters35ba6892000-10-11 07:04:49 +0000764 x = TOP();
765 Py_INCREF(x);
766 PUSH(x);
767 continue;
768 case 2:
Thomas Wouters434d0822000-08-24 20:11:32 +0000769 x = POP();
Tim Peters35ba6892000-10-11 07:04:49 +0000770 Py_INCREF(x);
771 w = TOP();
772 Py_INCREF(w);
773 PUSH(x);
774 PUSH(w);
775 PUSH(x);
776 continue;
777 case 3:
778 x = POP();
779 Py_INCREF(x);
Thomas Wouters434d0822000-08-24 20:11:32 +0000780 w = POP();
Tim Peters35ba6892000-10-11 07:04:49 +0000781 Py_INCREF(w);
782 v = TOP();
783 Py_INCREF(v);
784 PUSH(w);
785 PUSH(x);
786 PUSH(v);
787 PUSH(w);
788 PUSH(x);
789 continue;
790 case 4:
791 x = POP();
792 Py_INCREF(x);
793 w = POP();
794 Py_INCREF(w);
Thomas Wouters434d0822000-08-24 20:11:32 +0000795 v = POP();
Tim Peters35ba6892000-10-11 07:04:49 +0000796 Py_INCREF(v);
797 u = TOP();
798 Py_INCREF(u);
799 PUSH(v);
800 PUSH(w);
801 PUSH(x);
802 PUSH(u);
803 PUSH(v);
804 PUSH(w);
805 PUSH(x);
806 continue;
807 case 5:
808 x = POP();
809 Py_INCREF(x);
810 w = POP();
811 Py_INCREF(w);
812 v = POP();
813 Py_INCREF(v);
Thomas Wouters434d0822000-08-24 20:11:32 +0000814 u = POP();
Tim Peters35ba6892000-10-11 07:04:49 +0000815 Py_INCREF(u);
816 t = TOP();
817 Py_INCREF(t);
818 PUSH(u);
819 PUSH(v);
820 PUSH(w);
821 PUSH(x);
822 PUSH(t);
823 PUSH(u);
824 PUSH(v);
825 PUSH(w);
826 PUSH(x);
827 continue;
Thomas Wouters434d0822000-08-24 20:11:32 +0000828 default:
Thomas Wouters0be483f2000-10-11 23:26:11 +0000829 Py_FatalError("invalid argument to DUP_TOPX"
830 " (bytecode corruption?)");
Thomas Wouters434d0822000-08-24 20:11:32 +0000831 }
Tim Peters35ba6892000-10-11 07:04:49 +0000832 break;
Thomas Wouters434d0822000-08-24 20:11:32 +0000833
Guido van Rossum374a9221991-04-04 10:40:29 +0000834 case UNARY_POSITIVE:
835 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000836 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000837 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000838 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000839 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000840 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000841
Guido van Rossum374a9221991-04-04 10:40:29 +0000842 case UNARY_NEGATIVE:
843 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000844 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000845 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000846 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000847 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000848 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000849
Guido van Rossum374a9221991-04-04 10:40:29 +0000850 case UNARY_NOT:
851 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000852 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000853 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +0000854 if (err == 0) {
855 Py_INCREF(Py_True);
856 PUSH(Py_True);
857 continue;
858 }
859 else if (err > 0) {
860 Py_INCREF(Py_False);
861 PUSH(Py_False);
862 err = 0;
863 continue;
864 }
Guido van Rossum374a9221991-04-04 10:40:29 +0000865 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000866
Guido van Rossum374a9221991-04-04 10:40:29 +0000867 case UNARY_CONVERT:
868 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000869 x = PyObject_Repr(v);
870 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000871 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000872 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000873 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000874
Guido van Rossum7928cd71991-10-24 14:59:31 +0000875 case UNARY_INVERT:
876 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000877 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000878 Py_DECREF(v);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000879 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000880 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000881 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000882
Guido van Rossum50564e81996-01-12 01:13:16 +0000883 case BINARY_POWER:
884 w = POP();
885 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000886 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +0000887 Py_DECREF(v);
888 Py_DECREF(w);
Guido van Rossum50564e81996-01-12 01:13:16 +0000889 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000890 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +0000891 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000892
Guido van Rossum374a9221991-04-04 10:40:29 +0000893 case BINARY_MULTIPLY:
894 w = POP();
895 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000896 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000897 Py_DECREF(v);
898 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +0000899 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000900 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000901 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000902
Guido van Rossum374a9221991-04-04 10:40:29 +0000903 case BINARY_DIVIDE:
904 w = POP();
905 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000906 x = PyNumber_Divide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000907 Py_DECREF(v);
908 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +0000909 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000910 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000911 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000912
Guido van Rossum4668b002001-08-08 05:00:18 +0000913 case BINARY_FLOOR_DIVIDE:
914 w = POP();
915 v = POP();
916 x = PyNumber_FloorDivide(v, w);
917 Py_DECREF(v);
918 Py_DECREF(w);
919 PUSH(x);
920 if (x != NULL) continue;
921 break;
922
923 case BINARY_TRUE_DIVIDE:
924 w = POP();
925 v = POP();
926 x = PyNumber_TrueDivide(v, w);
927 Py_DECREF(v);
928 Py_DECREF(w);
929 PUSH(x);
930 if (x != NULL) continue;
931 break;
932
Guido van Rossum374a9221991-04-04 10:40:29 +0000933 case BINARY_MODULO:
934 w = POP();
935 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000936 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000937 Py_DECREF(v);
938 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +0000939 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000940 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000941 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000942
Guido van Rossum374a9221991-04-04 10:40:29 +0000943 case BINARY_ADD:
944 w = POP();
945 v = POP();
Tim Petersc1e6d962001-10-05 20:21:03 +0000946 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +0000947 /* INLINE: int + int */
948 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +0000949 a = PyInt_AS_LONG(v);
950 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +0000951 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +0000952 if ((i^a) < 0 && (i^b) < 0)
953 goto slow_add;
954 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +0000955 }
Guido van Rossum87780df2001-08-23 02:58:07 +0000956 else {
957 slow_add:
Guido van Rossumc12da691997-07-17 23:12:42 +0000958 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +0000959 }
Guido van Rossumb209a111997-04-29 18:18:01 +0000960 Py_DECREF(v);
961 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +0000962 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000963 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000964 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000965
Guido van Rossum374a9221991-04-04 10:40:29 +0000966 case BINARY_SUBTRACT:
967 w = POP();
968 v = POP();
Tim Petersc1e6d962001-10-05 20:21:03 +0000969 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +0000970 /* INLINE: int - int */
971 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +0000972 a = PyInt_AS_LONG(v);
973 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +0000974 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +0000975 if ((i^a) < 0 && (i^~b) < 0)
976 goto slow_sub;
977 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +0000978 }
Guido van Rossum87780df2001-08-23 02:58:07 +0000979 else {
980 slow_sub:
Guido van Rossumc12da691997-07-17 23:12:42 +0000981 x = PyNumber_Subtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +0000982 }
Guido van Rossumb209a111997-04-29 18:18:01 +0000983 Py_DECREF(v);
984 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +0000985 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000986 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000987 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000988
Guido van Rossum374a9221991-04-04 10:40:29 +0000989 case BINARY_SUBSCR:
990 w = POP();
991 v = POP();
Tim Petersb1c46982001-10-05 20:41:38 +0000992 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +0000993 /* INLINE: list[int] */
994 long i = PyInt_AsLong(w);
995 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +0000996 i += PyList_GET_SIZE(v);
Guido van Rossumc12da691997-07-17 23:12:42 +0000997 if (i < 0 ||
Guido van Rossumfa00e951998-07-08 15:02:37 +0000998 i >= PyList_GET_SIZE(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +0000999 PyErr_SetString(PyExc_IndexError,
1000 "list index out of range");
1001 x = NULL;
1002 }
1003 else {
Guido van Rossumfa00e951998-07-08 15:02:37 +00001004 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001005 Py_INCREF(x);
1006 }
1007 }
1008 else
1009 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001010 Py_DECREF(v);
1011 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001012 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001013 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001014 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001015
Guido van Rossum7928cd71991-10-24 14:59:31 +00001016 case BINARY_LSHIFT:
1017 w = POP();
1018 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001019 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001020 Py_DECREF(v);
1021 Py_DECREF(w);
Guido van Rossum7928cd71991-10-24 14:59:31 +00001022 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001023 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001024 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001025
Guido van Rossum7928cd71991-10-24 14:59:31 +00001026 case BINARY_RSHIFT:
1027 w = POP();
1028 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001029 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001030 Py_DECREF(v);
1031 Py_DECREF(w);
Guido van Rossum7928cd71991-10-24 14:59:31 +00001032 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001033 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001034 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001035
Guido van Rossum7928cd71991-10-24 14:59:31 +00001036 case BINARY_AND:
1037 w = POP();
1038 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001039 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001040 Py_DECREF(v);
1041 Py_DECREF(w);
Guido van Rossum7928cd71991-10-24 14:59:31 +00001042 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001043 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001044 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001045
Guido van Rossum7928cd71991-10-24 14:59:31 +00001046 case BINARY_XOR:
1047 w = POP();
1048 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001049 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001050 Py_DECREF(v);
1051 Py_DECREF(w);
Guido van Rossum7928cd71991-10-24 14:59:31 +00001052 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001053 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001054 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001055
Guido van Rossum7928cd71991-10-24 14:59:31 +00001056 case BINARY_OR:
1057 w = POP();
1058 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001059 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001060 Py_DECREF(v);
1061 Py_DECREF(w);
Guido van Rossum7928cd71991-10-24 14:59:31 +00001062 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001063 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001064 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001065
1066 case INPLACE_POWER:
1067 w = POP();
1068 v = POP();
1069 x = PyNumber_InPlacePower(v, w, Py_None);
1070 Py_DECREF(v);
1071 Py_DECREF(w);
1072 PUSH(x);
1073 if (x != NULL) continue;
1074 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001075
Thomas Wouters434d0822000-08-24 20:11:32 +00001076 case INPLACE_MULTIPLY:
1077 w = POP();
1078 v = POP();
1079 x = PyNumber_InPlaceMultiply(v, w);
1080 Py_DECREF(v);
1081 Py_DECREF(w);
1082 PUSH(x);
1083 if (x != NULL) continue;
1084 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001085
Thomas Wouters434d0822000-08-24 20:11:32 +00001086 case INPLACE_DIVIDE:
1087 w = POP();
1088 v = POP();
1089 x = PyNumber_InPlaceDivide(v, w);
1090 Py_DECREF(v);
1091 Py_DECREF(w);
1092 PUSH(x);
1093 if (x != NULL) continue;
1094 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001095
Guido van Rossum4668b002001-08-08 05:00:18 +00001096 case INPLACE_FLOOR_DIVIDE:
1097 w = POP();
1098 v = POP();
1099 x = PyNumber_InPlaceFloorDivide(v, w);
1100 Py_DECREF(v);
1101 Py_DECREF(w);
1102 PUSH(x);
1103 if (x != NULL) continue;
1104 break;
1105
1106 case INPLACE_TRUE_DIVIDE:
1107 w = POP();
1108 v = POP();
1109 x = PyNumber_InPlaceTrueDivide(v, w);
1110 Py_DECREF(v);
1111 Py_DECREF(w);
1112 PUSH(x);
1113 if (x != NULL) continue;
1114 break;
1115
Thomas Wouters434d0822000-08-24 20:11:32 +00001116 case INPLACE_MODULO:
1117 w = POP();
1118 v = POP();
1119 x = PyNumber_InPlaceRemainder(v, w);
1120 Py_DECREF(v);
1121 Py_DECREF(w);
1122 PUSH(x);
1123 if (x != NULL) continue;
1124 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001125
Thomas Wouters434d0822000-08-24 20:11:32 +00001126 case INPLACE_ADD:
1127 w = POP();
1128 v = POP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001129 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001130 /* INLINE: int + int */
1131 register long a, b, i;
1132 a = PyInt_AS_LONG(v);
1133 b = PyInt_AS_LONG(w);
1134 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001135 if ((i^a) < 0 && (i^b) < 0)
1136 goto slow_iadd;
1137 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001138 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001139 else {
1140 slow_iadd:
Thomas Wouters434d0822000-08-24 20:11:32 +00001141 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001142 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001143 Py_DECREF(v);
1144 Py_DECREF(w);
1145 PUSH(x);
1146 if (x != NULL) continue;
1147 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001148
Thomas Wouters434d0822000-08-24 20:11:32 +00001149 case INPLACE_SUBTRACT:
1150 w = POP();
1151 v = POP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001152 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001153 /* INLINE: int - int */
1154 register long a, b, i;
1155 a = PyInt_AS_LONG(v);
1156 b = PyInt_AS_LONG(w);
1157 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001158 if ((i^a) < 0 && (i^~b) < 0)
1159 goto slow_isub;
1160 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001161 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001162 else {
1163 slow_isub:
Thomas Wouters434d0822000-08-24 20:11:32 +00001164 x = PyNumber_InPlaceSubtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001165 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001166 Py_DECREF(v);
1167 Py_DECREF(w);
1168 PUSH(x);
1169 if (x != NULL) continue;
1170 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001171
Thomas Wouters434d0822000-08-24 20:11:32 +00001172 case INPLACE_LSHIFT:
1173 w = POP();
1174 v = POP();
1175 x = PyNumber_InPlaceLshift(v, w);
1176 Py_DECREF(v);
1177 Py_DECREF(w);
1178 PUSH(x);
1179 if (x != NULL) continue;
1180 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001181
Thomas Wouters434d0822000-08-24 20:11:32 +00001182 case INPLACE_RSHIFT:
1183 w = POP();
1184 v = POP();
1185 x = PyNumber_InPlaceRshift(v, w);
1186 Py_DECREF(v);
1187 Py_DECREF(w);
1188 PUSH(x);
1189 if (x != NULL) continue;
1190 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001191
Thomas Wouters434d0822000-08-24 20:11:32 +00001192 case INPLACE_AND:
1193 w = POP();
1194 v = POP();
1195 x = PyNumber_InPlaceAnd(v, w);
1196 Py_DECREF(v);
1197 Py_DECREF(w);
1198 PUSH(x);
1199 if (x != NULL) continue;
1200 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001201
Thomas Wouters434d0822000-08-24 20:11:32 +00001202 case INPLACE_XOR:
1203 w = POP();
1204 v = POP();
1205 x = PyNumber_InPlaceXor(v, w);
1206 Py_DECREF(v);
1207 Py_DECREF(w);
1208 PUSH(x);
1209 if (x != NULL) continue;
1210 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001211
Thomas Wouters434d0822000-08-24 20:11:32 +00001212 case INPLACE_OR:
1213 w = POP();
1214 v = POP();
1215 x = PyNumber_InPlaceOr(v, w);
1216 Py_DECREF(v);
1217 Py_DECREF(w);
1218 PUSH(x);
1219 if (x != NULL) continue;
1220 break;
1221
Guido van Rossum374a9221991-04-04 10:40:29 +00001222 case SLICE+0:
1223 case SLICE+1:
1224 case SLICE+2:
1225 case SLICE+3:
1226 if ((opcode-SLICE) & 2)
1227 w = POP();
1228 else
1229 w = NULL;
1230 if ((opcode-SLICE) & 1)
1231 v = POP();
1232 else
1233 v = NULL;
1234 u = POP();
1235 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001236 Py_DECREF(u);
1237 Py_XDECREF(v);
1238 Py_XDECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001239 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001240 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001241 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001242
Guido van Rossum374a9221991-04-04 10:40:29 +00001243 case STORE_SLICE+0:
1244 case STORE_SLICE+1:
1245 case STORE_SLICE+2:
1246 case STORE_SLICE+3:
1247 if ((opcode-STORE_SLICE) & 2)
1248 w = POP();
1249 else
1250 w = NULL;
1251 if ((opcode-STORE_SLICE) & 1)
1252 v = POP();
1253 else
1254 v = NULL;
1255 u = POP();
1256 t = POP();
1257 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +00001258 Py_DECREF(t);
1259 Py_DECREF(u);
1260 Py_XDECREF(v);
1261 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001262 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001263 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001264
Guido van Rossum374a9221991-04-04 10:40:29 +00001265 case DELETE_SLICE+0:
1266 case DELETE_SLICE+1:
1267 case DELETE_SLICE+2:
1268 case DELETE_SLICE+3:
1269 if ((opcode-DELETE_SLICE) & 2)
1270 w = POP();
1271 else
1272 w = NULL;
1273 if ((opcode-DELETE_SLICE) & 1)
1274 v = POP();
1275 else
1276 v = NULL;
1277 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001278 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +00001279 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00001280 Py_DECREF(u);
1281 Py_XDECREF(v);
1282 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001283 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001284 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001285
Guido van Rossum374a9221991-04-04 10:40:29 +00001286 case STORE_SUBSCR:
1287 w = POP();
1288 v = POP();
1289 u = POP();
1290 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001291 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001292 Py_DECREF(u);
1293 Py_DECREF(v);
1294 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001295 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001296 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001297
Guido van Rossum374a9221991-04-04 10:40:29 +00001298 case DELETE_SUBSCR:
1299 w = POP();
1300 v = POP();
1301 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001302 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001303 Py_DECREF(v);
1304 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001305 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001306 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001307
Guido van Rossum374a9221991-04-04 10:40:29 +00001308 case PRINT_EXPR:
1309 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001310 w = PySys_GetObject("displayhook");
1311 if (w == NULL) {
1312 PyErr_SetString(PyExc_RuntimeError,
1313 "lost sys.displayhook");
1314 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001315 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001316 }
1317 if (err == 0) {
1318 x = Py_BuildValue("(O)", v);
1319 if (x == NULL)
1320 err = -1;
1321 }
1322 if (err == 0) {
1323 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001324 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001325 if (w == NULL)
1326 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001327 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001328 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001329 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001330 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001331
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001332 case PRINT_ITEM_TO:
1333 w = stream = POP();
1334 /* fall through to PRINT_ITEM */
1335
Guido van Rossum374a9221991-04-04 10:40:29 +00001336 case PRINT_ITEM:
1337 v = POP();
Barry Warsaw093abe02000-08-29 04:56:13 +00001338 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001339 w = PySys_GetObject("stdout");
1340 if (w == NULL) {
1341 PyErr_SetString(PyExc_RuntimeError,
1342 "lost sys.stdout");
1343 err = -1;
1344 }
Guido van Rossum8f183201997-12-31 05:53:15 +00001345 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001346 if (w != NULL && PyFile_SoftSpace(w, 1))
Guido van Rossumbe270261997-05-22 22:26:18 +00001347 err = PyFile_WriteString(" ", w);
1348 if (err == 0)
1349 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
Guido van Rossumb209a111997-04-29 18:18:01 +00001350 if (err == 0 && PyString_Check(v)) {
Guido van Rossumc6004111993-11-05 10:22:19 +00001351 /* XXX move into writeobject() ? */
Guido van Rossumb209a111997-04-29 18:18:01 +00001352 char *s = PyString_AsString(v);
1353 int len = PyString_Size(v);
Guido van Rossum7f7f2741995-02-10 17:01:56 +00001354 if (len > 0 &&
1355 isspace(Py_CHARMASK(s[len-1])) &&
Guido van Rossumc6004111993-11-05 10:22:19 +00001356 s[len-1] != ' ')
Guido van Rossumb209a111997-04-29 18:18:01 +00001357 PyFile_SoftSpace(w, 0);
Guido van Rossum374a9221991-04-04 10:40:29 +00001358 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001359 Py_DECREF(v);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001360 Py_XDECREF(stream);
1361 stream = NULL;
1362 if (err == 0)
1363 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001364 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001365
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001366 case PRINT_NEWLINE_TO:
1367 w = stream = POP();
1368 /* fall through to PRINT_NEWLINE */
1369
Guido van Rossum374a9221991-04-04 10:40:29 +00001370 case PRINT_NEWLINE:
Barry Warsaw093abe02000-08-29 04:56:13 +00001371 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001372 w = PySys_GetObject("stdout");
1373 if (w == NULL)
1374 PyErr_SetString(PyExc_RuntimeError,
1375 "lost sys.stdout");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001376 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001377 if (w != NULL) {
1378 err = PyFile_WriteString("\n", w);
1379 if (err == 0)
1380 PyFile_SoftSpace(w, 0);
1381 }
1382 Py_XDECREF(stream);
1383 stream = NULL;
Guido van Rossum374a9221991-04-04 10:40:29 +00001384 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001385
Thomas Wouters434d0822000-08-24 20:11:32 +00001386
1387#ifdef CASE_TOO_BIG
1388 default: switch (opcode) {
1389#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001390 case BREAK_LOOP:
1391 why = WHY_BREAK;
1392 break;
Guido van Rossum66b0e9c2001-03-21 19:17:22 +00001393
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00001394 case CONTINUE_LOOP:
1395 retval = PyInt_FromLong(oparg);
1396 why = WHY_CONTINUE;
1397 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001398
Guido van Rossumf10570b1995-07-07 22:53:21 +00001399 case RAISE_VARARGS:
1400 u = v = w = NULL;
1401 switch (oparg) {
1402 case 3:
1403 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001404 /* Fallthrough */
1405 case 2:
1406 v = POP(); /* value */
1407 /* Fallthrough */
1408 case 1:
1409 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001410 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001411 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001412 break;
1413 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001414 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001415 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001416 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001417 break;
1418 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001419 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001420
Guido van Rossum374a9221991-04-04 10:40:29 +00001421 case LOAD_LOCALS:
Guido van Rossum681d79a1995-07-18 14:51:37 +00001422 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001423 PyErr_SetString(PyExc_SystemError,
1424 "no locals");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001425 break;
1426 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001427 Py_INCREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001428 PUSH(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001429 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001430
Guido van Rossum374a9221991-04-04 10:40:29 +00001431 case RETURN_VALUE:
1432 retval = POP();
1433 why = WHY_RETURN;
1434 break;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001435
Tim Peters5ca576e2001-06-18 22:08:13 +00001436 case YIELD_VALUE:
1437 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001438 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001439 f->f_lasti = INSTR_OFFSET();
1440 why = WHY_YIELD;
1441 break;
1442
1443
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001444 case EXEC_STMT:
1445 w = POP();
1446 v = POP();
1447 u = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001448 err = exec_statement(f, u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001449 Py_DECREF(u);
1450 Py_DECREF(v);
1451 Py_DECREF(w);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001452 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001453
Guido van Rossum374a9221991-04-04 10:40:29 +00001454 case POP_BLOCK:
1455 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001456 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001457 while (STACK_LEVEL() > b->b_level) {
1458 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001459 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001460 }
1461 }
1462 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001463
Guido van Rossum374a9221991-04-04 10:40:29 +00001464 case END_FINALLY:
1465 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001466 if (PyInt_Check(v)) {
1467 why = (enum why_code) PyInt_AsLong(v);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00001468 if (why == WHY_RETURN ||
Tim Peters5ca576e2001-06-18 22:08:13 +00001469 why == WHY_YIELD ||
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00001470 why == CONTINUE_LOOP)
Guido van Rossum374a9221991-04-04 10:40:29 +00001471 retval = POP();
1472 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001473 else if (PyString_Check(v) || PyClass_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001474 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001475 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001476 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001477 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001478 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001479 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001480 else if (v != Py_None) {
1481 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001482 "'finally' pops bad exception");
1483 why = WHY_EXCEPTION;
1484 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001485 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001486 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001487
Guido van Rossum374a9221991-04-04 10:40:29 +00001488 case BUILD_CLASS:
Guido van Rossum25831651993-05-19 14:50:45 +00001489 u = POP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001490 v = POP();
Guido van Rossum25831651993-05-19 14:50:45 +00001491 w = POP();
1492 x = build_class(u, v, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001493 PUSH(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001494 Py_DECREF(u);
1495 Py_DECREF(v);
1496 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001497 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001498
Guido van Rossum374a9221991-04-04 10:40:29 +00001499 case STORE_NAME:
1500 w = GETNAMEV(oparg);
1501 v = POP();
Guido van Rossum681d79a1995-07-18 14:51:37 +00001502 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001503 PyErr_Format(PyExc_SystemError,
1504 "no locals found when storing %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001505 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001506 break;
1507 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001508 err = PyDict_SetItem(x, w, v);
1509 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001510 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001511
Guido van Rossum374a9221991-04-04 10:40:29 +00001512 case DELETE_NAME:
Guido van Rossumdf62e441991-08-16 08:56:04 +00001513 w = GETNAMEV(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001514 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001515 PyErr_Format(PyExc_SystemError,
1516 "no locals when deleting %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001517 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001518 break;
1519 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001520 if ((err = PyDict_DelItem(x, w)) != 0)
Guido van Rossumac7be682001-01-17 15:42:30 +00001521 format_exc_check_arg(PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001522 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001523 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001524
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001525 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001526 v = POP();
Barry Warsawe42b18f1997-08-25 22:13:04 +00001527 if (PyTuple_Check(v)) {
1528 if (PyTuple_Size(v) != oparg) {
1529 PyErr_SetString(PyExc_ValueError,
1530 "unpack tuple of wrong size");
1531 why = WHY_EXCEPTION;
1532 }
1533 else {
1534 for (; --oparg >= 0; ) {
1535 w = PyTuple_GET_ITEM(v, oparg);
1536 Py_INCREF(w);
1537 PUSH(w);
1538 }
1539 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001540 }
Barry Warsawe42b18f1997-08-25 22:13:04 +00001541 else if (PyList_Check(v)) {
1542 if (PyList_Size(v) != oparg) {
1543 PyErr_SetString(PyExc_ValueError,
1544 "unpack list of wrong size");
1545 why = WHY_EXCEPTION;
1546 }
1547 else {
1548 for (; --oparg >= 0; ) {
1549 w = PyList_GET_ITEM(v, oparg);
1550 Py_INCREF(w);
1551 PUSH(w);
1552 }
1553 }
1554 }
Tim Petersd6d010b2001-06-21 02:49:55 +00001555 else if (unpack_iterable(v, oparg,
1556 stack_pointer + oparg))
1557 stack_pointer += oparg;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001558 else {
1559 if (PyErr_ExceptionMatches(PyExc_TypeError))
1560 PyErr_SetString(PyExc_TypeError,
1561 "unpack non-sequence");
Barry Warsawe42b18f1997-08-25 22:13:04 +00001562 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001563 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001564 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001565 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001566
Guido van Rossum374a9221991-04-04 10:40:29 +00001567 case STORE_ATTR:
Guido van Rossum9bfef441993-03-29 10:43:31 +00001568 w = GETNAMEV(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001569 v = POP();
1570 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001571 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1572 Py_DECREF(v);
1573 Py_DECREF(u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001574 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001575
Guido van Rossum374a9221991-04-04 10:40:29 +00001576 case DELETE_ATTR:
Guido van Rossum9bfef441993-03-29 10:43:31 +00001577 w = GETNAMEV(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001578 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001579 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1580 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001581 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001582 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001583
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001584 case STORE_GLOBAL:
1585 w = GETNAMEV(oparg);
1586 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001587 err = PyDict_SetItem(f->f_globals, w, v);
1588 Py_DECREF(v);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001589 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001590
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001591 case DELETE_GLOBAL:
1592 w = GETNAMEV(oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001593 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001594 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001595 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001596 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001597
Guido van Rossum374a9221991-04-04 10:40:29 +00001598 case LOAD_CONST:
1599 x = GETCONST(oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001600 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001601 PUSH(x);
1602 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001603
Guido van Rossum374a9221991-04-04 10:40:29 +00001604 case LOAD_NAME:
Guido van Rossumdf62e441991-08-16 08:56:04 +00001605 w = GETNAMEV(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001606 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001607 PyErr_Format(PyExc_SystemError,
1608 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001609 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001610 break;
1611 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001612 x = PyDict_GetItem(x, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001613 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001614 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001615 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001616 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001617 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001618 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001619 PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001620 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001621 break;
1622 }
1623 }
1624 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001625 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001626 PUSH(x);
1627 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001628
Guido van Rossum374a9221991-04-04 10:40:29 +00001629 case LOAD_GLOBAL:
Guido van Rossumdf62e441991-08-16 08:56:04 +00001630 w = GETNAMEV(oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001631 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001632 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001633 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001634 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001635 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001636 PyExc_NameError,
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001637 GLOBAL_NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001638 break;
1639 }
1640 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001641 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001642 PUSH(x);
1643 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001644
Guido van Rossum9bfef441993-03-29 10:43:31 +00001645 case LOAD_FAST:
Guido van Rossum681d79a1995-07-18 14:51:37 +00001646 x = GETLOCAL(oparg);
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001647 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001648 format_exc_check_arg(
1649 PyExc_UnboundLocalError,
1650 UNBOUNDLOCAL_ERROR_MSG,
1651 PyTuple_GetItem(co->co_varnames, oparg)
1652 );
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001653 break;
1654 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001655 Py_INCREF(x);
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001656 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001657 if (x != NULL) continue;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001658 break;
1659
1660 case STORE_FAST:
Guido van Rossum25831651993-05-19 14:50:45 +00001661 v = POP();
Guido van Rossum681d79a1995-07-18 14:51:37 +00001662 SETLOCAL(oparg, v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001663 continue;
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001664
1665 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001666 x = GETLOCAL(oparg);
1667 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001668 format_exc_check_arg(
1669 PyExc_UnboundLocalError,
1670 UNBOUNDLOCAL_ERROR_MSG,
1671 PyTuple_GetItem(co->co_varnames, oparg)
1672 );
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001673 break;
1674 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00001675 SETLOCAL(oparg, NULL);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001676 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001677
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001678 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001679 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001680 Py_INCREF(x);
1681 PUSH(x);
1682 break;
1683
1684 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001685 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001686 w = PyCell_Get(x);
Jeremy Hylton2524d692001-02-05 17:23:16 +00001687 if (w == NULL) {
Jeremy Hyltonc76770c2001-04-13 16:51:46 +00001688 if (oparg < f->f_ncells) {
Jeremy Hylton2524d692001-02-05 17:23:16 +00001689 v = PyTuple_GetItem(co->co_cellvars,
1690 oparg);
Jeremy Hyltonc76770c2001-04-13 16:51:46 +00001691 format_exc_check_arg(
1692 PyExc_UnboundLocalError,
1693 UNBOUNDLOCAL_ERROR_MSG,
1694 v);
1695 } else {
Jeremy Hylton2524d692001-02-05 17:23:16 +00001696 v = PyTuple_GetItem(
1697 co->co_freevars,
1698 oparg - f->f_ncells);
Jeremy Hyltonc76770c2001-04-13 16:51:46 +00001699 format_exc_check_arg(
1700 PyExc_NameError,
1701 UNBOUNDFREE_ERROR_MSG,
1702 v);
1703 }
Jeremy Hylton2524d692001-02-05 17:23:16 +00001704 err = -1;
1705 break;
1706 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001707 PUSH(w);
1708 break;
1709
1710 case STORE_DEREF:
1711 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001712 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001713 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00001714 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001715 continue;
1716
Guido van Rossum374a9221991-04-04 10:40:29 +00001717 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001718 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001719 if (x != NULL) {
1720 for (; --oparg >= 0;) {
1721 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001722 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001723 }
1724 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001725 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001726 }
1727 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001728
Guido van Rossum374a9221991-04-04 10:40:29 +00001729 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00001730 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001731 if (x != NULL) {
1732 for (; --oparg >= 0;) {
1733 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00001734 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001735 }
1736 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001737 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001738 }
1739 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001740
Guido van Rossum374a9221991-04-04 10:40:29 +00001741 case BUILD_MAP:
Guido van Rossumb209a111997-04-29 18:18:01 +00001742 x = PyDict_New();
Guido van Rossum374a9221991-04-04 10:40:29 +00001743 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001744 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001745 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001746
Guido van Rossum374a9221991-04-04 10:40:29 +00001747 case LOAD_ATTR:
Guido van Rossum9bfef441993-03-29 10:43:31 +00001748 w = GETNAMEV(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001749 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001750 x = PyObject_GetAttr(v, w);
1751 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001752 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001753 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001754 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001755
Guido van Rossum374a9221991-04-04 10:40:29 +00001756 case COMPARE_OP:
1757 w = POP();
1758 v = POP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001759 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001760 /* INLINE: cmp(int, int) */
1761 register long a, b;
1762 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001763 a = PyInt_AS_LONG(v);
1764 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001765 switch (oparg) {
1766 case LT: res = a < b; break;
1767 case LE: res = a <= b; break;
1768 case EQ: res = a == b; break;
1769 case NE: res = a != b; break;
1770 case GT: res = a > b; break;
1771 case GE: res = a >= b; break;
1772 case IS: res = v == w; break;
1773 case IS_NOT: res = v != w; break;
1774 default: goto slow_compare;
1775 }
1776 x = res ? Py_True : Py_False;
1777 Py_INCREF(x);
1778 }
1779 else {
1780 slow_compare:
1781 x = cmp_outcome(oparg, v, w);
1782 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001783 Py_DECREF(v);
1784 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001785 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001786 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001787 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001788
Guido van Rossum374a9221991-04-04 10:40:29 +00001789 case IMPORT_NAME:
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001790 w = GETNAMEV(oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001791 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001792 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001793 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00001794 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001795 break;
1796 }
Thomas Woutersdd13e4f2000-08-27 20:31:27 +00001797 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001798 w = Py_BuildValue("(OOOO)",
Guido van Rossum681d79a1995-07-18 14:51:37 +00001799 w,
1800 f->f_globals,
Guido van Rossuma027efa1997-05-05 20:56:21 +00001801 f->f_locals == NULL ?
1802 Py_None : f->f_locals,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001803 u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001804 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001805 if (w == NULL) {
1806 x = NULL;
1807 break;
1808 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001809 x = PyEval_CallObject(x, w);
1810 Py_DECREF(w);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001811 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001812 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001813 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001814
Thomas Wouters52152252000-08-17 22:55:00 +00001815 case IMPORT_STAR:
1816 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001817 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001818 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001819 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001820 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001821 break;
1822 }
Thomas Wouters52152252000-08-17 22:55:00 +00001823 err = import_all_from(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001824 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00001825 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001826 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001827 break;
Guido van Rossum25831651993-05-19 14:50:45 +00001828
Thomas Wouters52152252000-08-17 22:55:00 +00001829 case IMPORT_FROM:
1830 w = GETNAMEV(oparg);
1831 v = TOP();
1832 x = import_from(v, w);
1833 PUSH(x);
1834 if (x != NULL) continue;
1835 break;
1836
Guido van Rossum374a9221991-04-04 10:40:29 +00001837 case JUMP_FORWARD:
1838 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001839 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001840
Guido van Rossum374a9221991-04-04 10:40:29 +00001841 case JUMP_IF_FALSE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001842 err = PyObject_IsTrue(TOP());
Guido van Rossum04691fc1992-08-12 15:35:34 +00001843 if (err > 0)
1844 err = 0;
1845 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00001846 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001847 else
1848 break;
1849 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001850
Guido van Rossum374a9221991-04-04 10:40:29 +00001851 case JUMP_IF_TRUE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001852 err = PyObject_IsTrue(TOP());
Guido van Rossum04691fc1992-08-12 15:35:34 +00001853 if (err > 0) {
1854 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00001855 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001856 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001857 else if (err == 0)
1858 ;
1859 else
1860 break;
1861 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001862
Guido van Rossum374a9221991-04-04 10:40:29 +00001863 case JUMP_ABSOLUTE:
1864 JUMPTO(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001865 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001866
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001867 case GET_ITER:
1868 /* before: [obj]; after [getiter(obj)] */
1869 v = POP();
1870 x = PyObject_GetIter(v);
1871 Py_DECREF(v);
1872 if (x != NULL) {
Guido van Rossum213c7a62001-04-23 14:08:49 +00001873 PUSH(x);
1874 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001875 }
1876 break;
1877
1878 case FOR_ITER:
1879 /* before: [iter]; after: [iter, iter()] *or* [] */
1880 v = TOP();
Guido van Rossum213c7a62001-04-23 14:08:49 +00001881 x = PyIter_Next(v);
1882 if (x != NULL) {
1883 PUSH(x);
1884 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001885 }
Tim Petersf4848da2001-05-05 00:14:56 +00001886 if (!PyErr_Occurred()) {
1887 /* iterator ended normally */
1888 x = v = POP();
Guido van Rossum213c7a62001-04-23 14:08:49 +00001889 Py_DECREF(v);
1890 JUMPBY(oparg);
1891 continue;
1892 }
1893 break;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001894
Guido van Rossum374a9221991-04-04 10:40:29 +00001895 case FOR_LOOP:
1896 /* for v in s: ...
1897 On entry: stack contains s, i.
1898 On exit: stack contains s, i+1, s[i];
1899 but if loop exhausted:
1900 s, i are popped, and we jump */
1901 w = POP(); /* Loop index */
1902 v = POP(); /* Sequence object */
1903 u = loop_subscript(v, w);
1904 if (u != NULL) {
1905 PUSH(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001906 x = PyInt_FromLong(PyInt_AsLong(w)+1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001907 PUSH(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001908 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001909 PUSH(u);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001910 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001911 }
1912 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00001913 Py_DECREF(v);
1914 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001915 /* A NULL can mean "s exhausted"
1916 but also an error: */
Guido van Rossumb209a111997-04-29 18:18:01 +00001917 if (PyErr_Occurred())
Guido van Rossum374a9221991-04-04 10:40:29 +00001918 why = WHY_EXCEPTION;
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001919 else {
Guido van Rossum374a9221991-04-04 10:40:29 +00001920 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001921 continue;
1922 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001923 }
1924 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001925
Guido van Rossum374a9221991-04-04 10:40:29 +00001926 case SETUP_LOOP:
1927 case SETUP_EXCEPT:
1928 case SETUP_FINALLY:
Guido van Rossumb209a111997-04-29 18:18:01 +00001929 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00001930 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001931 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001932
Guido van Rossum374a9221991-04-04 10:40:29 +00001933 case SET_LINENO:
Guido van Rossum96a42c81992-01-12 02:29:51 +00001934#ifdef LLTRACE
1935 if (lltrace)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001936 printf("--- %s:%d \n", filename, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001937#endif
Guido van Rossum96a42c81992-01-12 02:29:51 +00001938 f->f_lineno = oparg;
Fred Drake5755ce62001-06-27 19:19:46 +00001939 if (tstate->c_tracefunc == NULL || tstate->tracing)
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001940 continue;
1941 /* Trace each line of code reached */
1942 f->f_lasti = INSTR_OFFSET();
Fred Drake5755ce62001-06-27 19:19:46 +00001943 /* Inline call_trace() for performance: */
1944 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00001945 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00001946 err = (tstate->c_tracefunc)(tstate->c_traceobj, f,
1947 PyTrace_LINE, Py_None);
Fred Drake9e3ad782001-07-03 23:39:52 +00001948 tstate->use_tracing = (tstate->c_tracefunc
1949 || tstate->c_profilefunc);
Fred Drake5755ce62001-06-27 19:19:46 +00001950 tstate->tracing--;
Guido van Rossum374a9221991-04-04 10:40:29 +00001951 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001952
1953 case CALL_FUNCTION:
Jeremy Hylton52820442001-01-03 23:52:36 +00001954 {
1955 int na = oparg & 0xff;
1956 int nk = (oparg>>8) & 0xff;
1957 int n = na + 2 * nk;
1958 PyObject **pfunc = stack_pointer - n - 1;
1959 PyObject *func = *pfunc;
1960 f->f_lasti = INSTR_OFFSET() - 3; /* For tracing */
1961
1962 /* Always dispatch PyCFunction first, because
1963 these are presumed to be the most frequent
1964 callable object.
1965 */
1966 if (PyCFunction_Check(func)) {
Jeremy Hyltonda20fce2001-05-18 20:53:14 +00001967 int flags = PyCFunction_GET_FLAGS(func);
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001968 if (nk != 0 || (flags & METH_KEYWORDS))
Jeremy Hylton4c9dace2001-05-29 16:23:26 +00001969 x = do_call(func, &stack_pointer,
1970 na, nk);
1971 else if (flags == METH_VARARGS) {
Jeremy Hyltonda20fce2001-05-18 20:53:14 +00001972 PyObject *callargs;
1973 callargs = load_args(&stack_pointer, na);
Jeremy Hylton910d7d42001-08-12 21:52:24 +00001974 x = PyCFunction_Call(func, callargs, NULL);
Jeremy Hyltonda20fce2001-05-18 20:53:14 +00001975 Py_XDECREF(callargs);
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001976 } else
1977 x = fast_cfunction(func,
1978 &stack_pointer, na);
Jeremy Hylton52820442001-01-03 23:52:36 +00001979 } else {
Guido van Rossumac7be682001-01-17 15:42:30 +00001980 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00001981 && PyMethod_GET_SELF(func) != NULL) {
1982 /* optimize access to bound methods */
1983 PyObject *self = PyMethod_GET_SELF(func);
1984 Py_INCREF(self);
1985 func = PyMethod_GET_FUNCTION(func);
1986 Py_INCREF(func);
1987 Py_DECREF(*pfunc);
1988 *pfunc = self;
1989 na++;
1990 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00001991 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00001992 Py_INCREF(func);
1993 if (PyFunction_Check(func)) {
1994 x = fast_function(func, &stack_pointer,
Guido van Rossumac7be682001-01-17 15:42:30 +00001995 n, na, nk);
Jeremy Hylton52820442001-01-03 23:52:36 +00001996 } else {
1997 x = do_call(func, &stack_pointer,
1998 na, nk);
1999 }
2000 Py_DECREF(func);
2001 }
2002
2003 while (stack_pointer > pfunc) {
2004 w = POP();
2005 Py_DECREF(w);
2006 }
2007 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002008 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002009 continue;
2010 break;
2011 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002012
Jeremy Hylton76901512000-03-28 23:49:17 +00002013 case CALL_FUNCTION_VAR:
2014 case CALL_FUNCTION_KW:
2015 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002016 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002017 int na = oparg & 0xff;
2018 int nk = (oparg>>8) & 0xff;
2019 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002020 int n = na + 2 * nk;
2021 PyObject **pfunc, *func;
2022 if (flags & CALL_FLAG_VAR)
2023 n++;
2024 if (flags & CALL_FLAG_KW)
2025 n++;
2026 pfunc = stack_pointer - n - 1;
2027 func = *pfunc;
Jeremy Hylton76901512000-03-28 23:49:17 +00002028 f->f_lasti = INSTR_OFFSET() - 3; /* For tracing */
Jeremy Hylton52820442001-01-03 23:52:36 +00002029
Guido van Rossumac7be682001-01-17 15:42:30 +00002030 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002031 && PyMethod_GET_SELF(func) != NULL) {
2032 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002033 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002034 func = PyMethod_GET_FUNCTION(func);
2035 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002036 Py_DECREF(*pfunc);
2037 *pfunc = self;
2038 na++;
2039 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002040 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002041 Py_INCREF(func);
2042 x = ext_do_call(func, &stack_pointer, flags, na, nk);
Jeremy Hylton76901512000-03-28 23:49:17 +00002043 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002044
Jeremy Hylton76901512000-03-28 23:49:17 +00002045 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002046 w = POP();
2047 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002048 }
2049 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002050 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002051 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002052 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002053 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002054
Guido van Rossum681d79a1995-07-18 14:51:37 +00002055 case MAKE_FUNCTION:
2056 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002057 x = PyFunction_New(v, f->f_globals);
2058 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002059 /* XXX Maybe this should be a separate opcode? */
2060 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002061 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002062 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002063 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002064 x = NULL;
2065 break;
2066 }
2067 while (--oparg >= 0) {
2068 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002069 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002070 }
2071 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002072 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002073 }
2074 PUSH(x);
2075 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002076
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002077 case MAKE_CLOSURE:
2078 {
2079 int nfree;
2080 v = POP(); /* code object */
2081 x = PyFunction_New(v, f->f_globals);
2082 nfree = PyTuple_GET_SIZE(((PyCodeObject *)v)->co_freevars);
2083 Py_DECREF(v);
2084 /* XXX Maybe this should be a separate opcode? */
2085 if (x != NULL && nfree > 0) {
2086 v = PyTuple_New(nfree);
2087 if (v == NULL) {
2088 Py_DECREF(x);
2089 x = NULL;
2090 break;
2091 }
2092 while (--nfree >= 0) {
2093 w = POP();
2094 PyTuple_SET_ITEM(v, nfree, w);
2095 }
2096 err = PyFunction_SetClosure(x, v);
2097 Py_DECREF(v);
2098 }
2099 if (x != NULL && oparg > 0) {
2100 v = PyTuple_New(oparg);
2101 if (v == NULL) {
2102 Py_DECREF(x);
2103 x = NULL;
2104 break;
2105 }
2106 while (--oparg >= 0) {
2107 w = POP();
2108 PyTuple_SET_ITEM(v, oparg, w);
2109 }
2110 err = PyFunction_SetDefaults(x, v);
2111 Py_DECREF(v);
2112 }
2113 PUSH(x);
2114 break;
2115 }
2116
Guido van Rossum8861b741996-07-30 16:49:37 +00002117 case BUILD_SLICE:
2118 if (oparg == 3)
2119 w = POP();
2120 else
2121 w = NULL;
2122 v = POP();
2123 u = POP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002124 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002125 Py_DECREF(u);
2126 Py_DECREF(v);
2127 Py_XDECREF(w);
Guido van Rossum8861b741996-07-30 16:49:37 +00002128 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002129 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002130 break;
2131
Fred Drakeef8ace32000-08-24 00:32:09 +00002132 case EXTENDED_ARG:
2133 opcode = NEXTOP();
2134 oparg = oparg<<16 | NEXTARG();
2135 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002136
Guido van Rossum374a9221991-04-04 10:40:29 +00002137 default:
2138 fprintf(stderr,
2139 "XXX lineno: %d, opcode: %d\n",
Guido van Rossum96a42c81992-01-12 02:29:51 +00002140 f->f_lineno, opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002141 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002142 why = WHY_EXCEPTION;
2143 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002144
2145#ifdef CASE_TOO_BIG
2146 }
2147#endif
2148
Guido van Rossum374a9221991-04-04 10:40:29 +00002149 } /* switch */
2150
2151 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002152
Guido van Rossum374a9221991-04-04 10:40:29 +00002153 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002154
Guido van Rossum374a9221991-04-04 10:40:29 +00002155 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002156 if (err == 0 && x != NULL) {
2157#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002158 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002159 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002160 fprintf(stderr,
2161 "XXX undetected error\n");
2162 else
2163#endif
2164 continue; /* Normal, fast path */
2165 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002166 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002167 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002168 err = 0;
2169 }
2170
Guido van Rossum374a9221991-04-04 10:40:29 +00002171 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002172
Guido van Rossum374a9221991-04-04 10:40:29 +00002173 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002174 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002175 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002176 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002177 why = WHY_EXCEPTION;
2178 }
2179 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002180#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002181 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002182 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002183 if (PyErr_Occurred()) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002184 fprintf(stderr,
2185 "XXX undetected error (why=%d)\n",
2186 why);
2187 why = WHY_EXCEPTION;
2188 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002189 }
2190#endif
2191
2192 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002193
Guido van Rossum374a9221991-04-04 10:40:29 +00002194 if (why == WHY_EXCEPTION) {
Guido van Rossum96a42c81992-01-12 02:29:51 +00002195 f->f_lasti = INSTR_OFFSET() - 1;
Guido van Rossum374a9221991-04-04 10:40:29 +00002196 if (HAS_ARG(opcode))
Guido van Rossum96a42c81992-01-12 02:29:51 +00002197 f->f_lasti -= 2;
Guido van Rossumb209a111997-04-29 18:18:01 +00002198 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002199
Fred Drake8f51f542001-10-04 14:48:42 +00002200 if (tstate->c_tracefunc != NULL)
2201 call_exc_trace(tstate->c_tracefunc,
2202 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002203 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002204
Guido van Rossum374a9221991-04-04 10:40:29 +00002205 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002206
Guido van Rossum374a9221991-04-04 10:40:29 +00002207 if (why == WHY_RERAISE)
2208 why = WHY_EXCEPTION;
2209
2210 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002211
Tim Peters5ca576e2001-06-18 22:08:13 +00002212 while (why != WHY_NOT && why != WHY_YIELD && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002213 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002214
2215 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2216 /* For a continue inside a try block,
2217 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002218 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2219 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002220 why = WHY_NOT;
2221 JUMPTO(PyInt_AS_LONG(retval));
2222 Py_DECREF(retval);
2223 break;
2224 }
2225
Guido van Rossum374a9221991-04-04 10:40:29 +00002226 while (STACK_LEVEL() > b->b_level) {
2227 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002228 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002229 }
2230 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2231 why = WHY_NOT;
2232 JUMPTO(b->b_handler);
2233 break;
2234 }
2235 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002236 (b->b_type == SETUP_EXCEPT &&
2237 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002238 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002239 PyObject *exc, *val, *tb;
2240 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002241 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002242 val = Py_None;
2243 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002244 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002245 /* Make the raw exception data
2246 available to the handler,
2247 so a program can emulate the
2248 Python main loop. Don't do
2249 this for 'finally'. */
2250 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002251 PyErr_NormalizeException(
2252 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002253 set_exc_info(tstate,
2254 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002255 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002256 if (tb == NULL) {
2257 Py_INCREF(Py_None);
2258 PUSH(Py_None);
2259 } else
2260 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002261 PUSH(val);
2262 PUSH(exc);
2263 }
2264 else {
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002265 if (why == WHY_RETURN ||
2266 why == CONTINUE_LOOP)
Guido van Rossum374a9221991-04-04 10:40:29 +00002267 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002268 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002269 PUSH(v);
2270 }
2271 why = WHY_NOT;
2272 JUMPTO(b->b_handler);
2273 break;
2274 }
2275 } /* unwind stack */
2276
2277 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002278
Guido van Rossum374a9221991-04-04 10:40:29 +00002279 if (why != WHY_NOT)
2280 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002281
Guido van Rossum374a9221991-04-04 10:40:29 +00002282 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002283
Tim Peters5ca576e2001-06-18 22:08:13 +00002284 if (why != WHY_RETURN && why != WHY_YIELD)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002285 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002286
Fred Drake9e3ad782001-07-03 23:39:52 +00002287 if (tstate->use_tracing) {
2288 if (tstate->c_tracefunc
2289 && (why == WHY_RETURN || why == WHY_YIELD)) {
2290 if (call_trace(tstate->c_tracefunc,
2291 tstate->c_traceobj, f,
2292 PyTrace_RETURN, retval)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002293 Py_XDECREF(retval);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002294 retval = NULL;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002295 why = WHY_EXCEPTION;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002296 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002297 }
Fred Drake8f51f542001-10-04 14:48:42 +00002298 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002299 if (why == WHY_EXCEPTION)
2300 call_trace_protected(tstate->c_profilefunc,
2301 tstate->c_profileobj, f,
2302 PyTrace_RETURN);
2303 else if (call_trace(tstate->c_profilefunc,
2304 tstate->c_profileobj, f,
2305 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002306 Py_XDECREF(retval);
2307 retval = NULL;
2308 why = WHY_EXCEPTION;
2309 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002310 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002311 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002312
Guido van Rossuma027efa1997-05-05 20:56:21 +00002313 reset_exc_info(tstate);
2314
Tim Peters5ca576e2001-06-18 22:08:13 +00002315 /* pop frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002316 --tstate->recursion_depth;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002317 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002318
Guido van Rossum96a42c81992-01-12 02:29:51 +00002319 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002320}
2321
Tim Peters6d6c1a32001-08-02 04:15:00 +00002322PyObject *
2323PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002324 PyObject **args, int argcount, PyObject **kws, int kwcount,
2325 PyObject **defs, int defcount, PyObject *closure)
2326{
2327 register PyFrameObject *f;
2328 register PyObject *retval = NULL;
2329 register PyObject **fastlocals, **freevars;
2330 PyThreadState *tstate = PyThreadState_GET();
2331 PyObject *x, *u;
2332
2333 if (globals == NULL) {
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002334 PyErr_SetString(PyExc_SystemError,
2335 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002336 return NULL;
2337 }
2338
2339 f = PyFrame_New(tstate, /*back*/
2340 co, /*code*/
2341 globals, locals);
2342 if (f == NULL)
2343 return NULL;
2344
2345 fastlocals = f->f_localsplus;
2346 freevars = f->f_localsplus + f->f_nlocals;
2347
2348 if (co->co_argcount > 0 ||
2349 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2350 int i;
2351 int n = argcount;
2352 PyObject *kwdict = NULL;
2353 if (co->co_flags & CO_VARKEYWORDS) {
2354 kwdict = PyDict_New();
2355 if (kwdict == NULL)
2356 goto fail;
2357 i = co->co_argcount;
2358 if (co->co_flags & CO_VARARGS)
2359 i++;
2360 SETLOCAL(i, kwdict);
2361 }
2362 if (argcount > co->co_argcount) {
2363 if (!(co->co_flags & CO_VARARGS)) {
2364 PyErr_Format(PyExc_TypeError,
2365 "%.200s() takes %s %d "
2366 "%sargument%s (%d given)",
2367 PyString_AsString(co->co_name),
2368 defcount ? "at most" : "exactly",
2369 co->co_argcount,
2370 kwcount ? "non-keyword " : "",
2371 co->co_argcount == 1 ? "" : "s",
2372 argcount);
2373 goto fail;
2374 }
2375 n = co->co_argcount;
2376 }
2377 for (i = 0; i < n; i++) {
2378 x = args[i];
2379 Py_INCREF(x);
2380 SETLOCAL(i, x);
2381 }
2382 if (co->co_flags & CO_VARARGS) {
2383 u = PyTuple_New(argcount - n);
2384 if (u == NULL)
2385 goto fail;
2386 SETLOCAL(co->co_argcount, u);
2387 for (i = n; i < argcount; i++) {
2388 x = args[i];
2389 Py_INCREF(x);
2390 PyTuple_SET_ITEM(u, i-n, x);
2391 }
2392 }
2393 for (i = 0; i < kwcount; i++) {
2394 PyObject *keyword = kws[2*i];
2395 PyObject *value = kws[2*i + 1];
2396 int j;
2397 if (keyword == NULL || !PyString_Check(keyword)) {
2398 PyErr_Format(PyExc_TypeError,
2399 "%.200s() keywords must be strings",
2400 PyString_AsString(co->co_name));
2401 goto fail;
2402 }
2403 /* XXX slow -- speed up using dictionary? */
2404 for (j = 0; j < co->co_argcount; j++) {
2405 PyObject *nm = PyTuple_GET_ITEM(
2406 co->co_varnames, j);
2407 int cmp = PyObject_RichCompareBool(
2408 keyword, nm, Py_EQ);
2409 if (cmp > 0)
2410 break;
2411 else if (cmp < 0)
2412 goto fail;
2413 }
2414 /* Check errors from Compare */
2415 if (PyErr_Occurred())
2416 goto fail;
2417 if (j >= co->co_argcount) {
2418 if (kwdict == NULL) {
2419 PyErr_Format(PyExc_TypeError,
2420 "%.200s() got an unexpected "
2421 "keyword argument '%.400s'",
2422 PyString_AsString(co->co_name),
2423 PyString_AsString(keyword));
2424 goto fail;
2425 }
2426 PyDict_SetItem(kwdict, keyword, value);
2427 }
2428 else {
2429 if (GETLOCAL(j) != NULL) {
2430 PyErr_Format(PyExc_TypeError,
2431 "%.200s() got multiple "
2432 "values for keyword "
2433 "argument '%.400s'",
2434 PyString_AsString(co->co_name),
2435 PyString_AsString(keyword));
2436 goto fail;
2437 }
2438 Py_INCREF(value);
2439 SETLOCAL(j, value);
2440 }
2441 }
2442 if (argcount < co->co_argcount) {
2443 int m = co->co_argcount - defcount;
2444 for (i = argcount; i < m; i++) {
2445 if (GETLOCAL(i) == NULL) {
2446 PyErr_Format(PyExc_TypeError,
2447 "%.200s() takes %s %d "
2448 "%sargument%s (%d given)",
2449 PyString_AsString(co->co_name),
2450 ((co->co_flags & CO_VARARGS) ||
2451 defcount) ? "at least"
2452 : "exactly",
2453 m, kwcount ? "non-keyword " : "",
2454 m == 1 ? "" : "s", i);
2455 goto fail;
2456 }
2457 }
2458 if (n > m)
2459 i = n - m;
2460 else
2461 i = 0;
2462 for (; i < defcount; i++) {
2463 if (GETLOCAL(m+i) == NULL) {
2464 PyObject *def = defs[i];
2465 Py_INCREF(def);
2466 SETLOCAL(m+i, def);
2467 }
2468 }
2469 }
2470 }
2471 else {
2472 if (argcount > 0 || kwcount > 0) {
2473 PyErr_Format(PyExc_TypeError,
2474 "%.200s() takes no arguments (%d given)",
2475 PyString_AsString(co->co_name),
2476 argcount + kwcount);
2477 goto fail;
2478 }
2479 }
2480 /* Allocate and initialize storage for cell vars, and copy free
2481 vars into frame. This isn't too efficient right now. */
2482 if (f->f_ncells) {
2483 int i = 0, j = 0, nargs, found;
2484 char *cellname, *argname;
2485 PyObject *c;
2486
2487 nargs = co->co_argcount;
2488 if (co->co_flags & CO_VARARGS)
2489 nargs++;
2490 if (co->co_flags & CO_VARKEYWORDS)
2491 nargs++;
2492
2493 /* Check for cells that shadow args */
2494 for (i = 0; i < f->f_ncells && j < nargs; ++i) {
2495 cellname = PyString_AS_STRING(
2496 PyTuple_GET_ITEM(co->co_cellvars, i));
2497 found = 0;
2498 while (j < nargs) {
2499 argname = PyString_AS_STRING(
2500 PyTuple_GET_ITEM(co->co_varnames, j));
2501 if (strcmp(cellname, argname) == 0) {
2502 c = PyCell_New(GETLOCAL(j));
2503 if (c == NULL)
2504 goto fail;
2505 GETLOCAL(f->f_nlocals + i) = c;
2506 found = 1;
2507 break;
2508 }
2509 j++;
2510 }
2511 if (found == 0) {
2512 c = PyCell_New(NULL);
2513 if (c == NULL)
2514 goto fail;
2515 SETLOCAL(f->f_nlocals + i, c);
2516 }
2517 }
2518 /* Initialize any that are left */
2519 while (i < f->f_ncells) {
2520 c = PyCell_New(NULL);
2521 if (c == NULL)
2522 goto fail;
2523 SETLOCAL(f->f_nlocals + i, c);
2524 i++;
2525 }
2526 }
2527 if (f->f_nfreevars) {
2528 int i;
2529 for (i = 0; i < f->f_nfreevars; ++i) {
2530 PyObject *o = PyTuple_GET_ITEM(closure, i);
2531 Py_INCREF(o);
2532 freevars[f->f_ncells + i] = o;
2533 }
2534 }
2535
Tim Peters5ca576e2001-06-18 22:08:13 +00002536 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002537 /* Don't need to keep the reference to f_back, it will be set
2538 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002539 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002540 f->f_back = NULL;
2541
2542 /* Create a new generator that owns the ready to run frame
2543 * and return that as the value. */
Tim Peters5ca576e2001-06-18 22:08:13 +00002544 return gen_new(f);
2545 }
2546
2547 retval = eval_frame(f);
2548
2549 fail: /* Jump here from prelude on failure */
2550
2551 Py_DECREF(f);
2552 return retval;
2553}
2554
2555
Guido van Rossuma027efa1997-05-05 20:56:21 +00002556static void
Guido van Rossumac7be682001-01-17 15:42:30 +00002557set_exc_info(PyThreadState *tstate,
2558 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002559{
2560 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002561 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00002562
Guido van Rossuma027efa1997-05-05 20:56:21 +00002563 frame = tstate->frame;
2564 if (frame->f_exc_type == NULL) {
2565 /* This frame didn't catch an exception before */
2566 /* Save previous exception of this thread in this frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002567 if (tstate->exc_type == NULL) {
2568 Py_INCREF(Py_None);
2569 tstate->exc_type = Py_None;
2570 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002571 tmp_type = frame->f_exc_type;
2572 tmp_value = frame->f_exc_value;
2573 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002574 Py_XINCREF(tstate->exc_type);
2575 Py_XINCREF(tstate->exc_value);
2576 Py_XINCREF(tstate->exc_traceback);
2577 frame->f_exc_type = tstate->exc_type;
2578 frame->f_exc_value = tstate->exc_value;
2579 frame->f_exc_traceback = tstate->exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002580 Py_XDECREF(tmp_type);
2581 Py_XDECREF(tmp_value);
2582 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002583 }
2584 /* Set new exception for this thread */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002585 tmp_type = tstate->exc_type;
2586 tmp_value = tstate->exc_value;
2587 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002588 Py_XINCREF(type);
2589 Py_XINCREF(value);
2590 Py_XINCREF(tb);
2591 tstate->exc_type = type;
2592 tstate->exc_value = value;
2593 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002594 Py_XDECREF(tmp_type);
2595 Py_XDECREF(tmp_value);
2596 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002597 /* For b/w compatibility */
2598 PySys_SetObject("exc_type", type);
2599 PySys_SetObject("exc_value", value);
2600 PySys_SetObject("exc_traceback", tb);
2601}
2602
2603static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002604reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002605{
2606 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002607 PyObject *tmp_type, *tmp_value, *tmp_tb;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002608 frame = tstate->frame;
2609 if (frame->f_exc_type != NULL) {
2610 /* This frame caught an exception */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002611 tmp_type = tstate->exc_type;
2612 tmp_value = tstate->exc_value;
2613 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002614 Py_XINCREF(frame->f_exc_type);
2615 Py_XINCREF(frame->f_exc_value);
2616 Py_XINCREF(frame->f_exc_traceback);
2617 tstate->exc_type = frame->f_exc_type;
2618 tstate->exc_value = frame->f_exc_value;
2619 tstate->exc_traceback = frame->f_exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002620 Py_XDECREF(tmp_type);
2621 Py_XDECREF(tmp_value);
2622 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002623 /* For b/w compatibility */
2624 PySys_SetObject("exc_type", frame->f_exc_type);
2625 PySys_SetObject("exc_value", frame->f_exc_value);
2626 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
2627 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002628 tmp_type = frame->f_exc_type;
2629 tmp_value = frame->f_exc_value;
2630 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002631 frame->f_exc_type = NULL;
2632 frame->f_exc_value = NULL;
2633 frame->f_exc_traceback = NULL;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002634 Py_XDECREF(tmp_type);
2635 Py_XDECREF(tmp_value);
2636 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002637}
2638
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002639/* Logic for the raise statement (too complicated for inlining).
2640 This *consumes* a reference count to each of its arguments. */
Guido van Rossum1aa14831997-01-21 05:34:20 +00002641static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002642do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002643{
Guido van Rossumd295f121998-04-09 21:39:57 +00002644 if (type == NULL) {
2645 /* Reraise */
2646 PyThreadState *tstate = PyThreadState_Get();
2647 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
2648 value = tstate->exc_value;
2649 tb = tstate->exc_traceback;
2650 Py_XINCREF(type);
2651 Py_XINCREF(value);
2652 Py_XINCREF(tb);
2653 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002654
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002655 /* We support the following forms of raise:
2656 raise <class>, <classinstance>
2657 raise <class>, <argument tuple>
2658 raise <class>, None
2659 raise <class>, <argument>
2660 raise <classinstance>, None
2661 raise <string>, <object>
2662 raise <string>, None
2663
2664 An omitted second argument is the same as None.
2665
2666 In addition, raise <tuple>, <anything> is the same as
2667 raising the tuple's first item (and it better have one!);
2668 this rule is applied recursively.
2669
2670 Finally, an optional third argument can be supplied, which
2671 gives the traceback to be substituted (useful when
2672 re-raising an exception after examining it). */
2673
2674 /* First, check the traceback argument, replacing None with
2675 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002676 if (tb == Py_None) {
2677 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002678 tb = NULL;
2679 }
2680 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002681 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002682 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002683 goto raise_error;
2684 }
2685
2686 /* Next, replace a missing value with None */
2687 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002688 value = Py_None;
2689 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002690 }
2691
2692 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00002693 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
2694 PyObject *tmp = type;
2695 type = PyTuple_GET_ITEM(type, 0);
2696 Py_INCREF(type);
2697 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002698 }
2699
Barry Warsaw4249f541997-08-22 21:26:19 +00002700 if (PyString_Check(type))
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002701 ;
Barry Warsaw4249f541997-08-22 21:26:19 +00002702
2703 else if (PyClass_Check(type))
2704 PyErr_NormalizeException(&type, &value, &tb);
2705
Guido van Rossumb209a111997-04-29 18:18:01 +00002706 else if (PyInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002707 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002708 if (value != Py_None) {
2709 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002710 "instance exception may not have a separate value");
2711 goto raise_error;
2712 }
2713 else {
2714 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00002715 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002716 value = type;
Guido van Rossumb209a111997-04-29 18:18:01 +00002717 type = (PyObject*) ((PyInstanceObject*)type)->in_class;
2718 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002719 }
2720 }
2721 else {
2722 /* Not something you can raise. You get an exception
2723 anyway, just not what you specified :-) */
Jeremy Hylton960d9482001-04-27 02:25:33 +00002724 PyErr_Format(PyExc_TypeError,
2725 "exceptions must be strings, classes, or "
2726 "instances, not %s", type->ob_type->tp_name);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002727 goto raise_error;
2728 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002729 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002730 if (tb == NULL)
2731 return WHY_EXCEPTION;
2732 else
2733 return WHY_RERAISE;
2734 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00002735 Py_XDECREF(value);
2736 Py_XDECREF(type);
2737 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002738 return WHY_EXCEPTION;
2739}
2740
Tim Petersd6d010b2001-06-21 02:49:55 +00002741/* Iterate v argcnt times and store the results on the stack (via decreasing
2742 sp). Return 1 for success, 0 if error. */
2743
Barry Warsawe42b18f1997-08-25 22:13:04 +00002744static int
Tim Petersd6d010b2001-06-21 02:49:55 +00002745unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00002746{
Tim Petersd6d010b2001-06-21 02:49:55 +00002747 int i = 0;
2748 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00002749 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00002750
Tim Petersd6d010b2001-06-21 02:49:55 +00002751 assert(v != NULL);
2752
2753 it = PyObject_GetIter(v);
2754 if (it == NULL)
2755 goto Error;
2756
2757 for (; i < argcnt; i++) {
2758 w = PyIter_Next(it);
2759 if (w == NULL) {
2760 /* Iterator done, via error or exhaustion. */
2761 if (!PyErr_Occurred()) {
2762 PyErr_Format(PyExc_ValueError,
2763 "need more than %d value%s to unpack",
2764 i, i == 1 ? "" : "s");
2765 }
2766 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00002767 }
2768 *--sp = w;
2769 }
Tim Petersd6d010b2001-06-21 02:49:55 +00002770
2771 /* We better have exhausted the iterator now. */
2772 w = PyIter_Next(it);
2773 if (w == NULL) {
2774 if (PyErr_Occurred())
2775 goto Error;
2776 Py_DECREF(it);
2777 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00002778 }
Tim Petersd6d010b2001-06-21 02:49:55 +00002779 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00002780 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00002781Error:
Barry Warsaw91010551997-08-25 22:30:51 +00002782 for (; i > 0; i--, sp++)
2783 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00002784 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00002785 return 0;
2786}
2787
2788
Guido van Rossum96a42c81992-01-12 02:29:51 +00002789#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00002790static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002791prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002792{
Guido van Rossum3f5da241990-12-20 15:06:42 +00002793 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00002794 if (PyObject_Print(v, stdout, 0) != 0)
2795 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00002796 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00002797 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002798}
Guido van Rossum3f5da241990-12-20 15:06:42 +00002799#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002800
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002801static void
Fred Drake5755ce62001-06-27 19:19:46 +00002802call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002803{
Guido van Rossumb209a111997-04-29 18:18:01 +00002804 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002805 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00002806 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00002807 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002808 value = Py_None;
2809 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00002810 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002811 arg = Py_BuildValue("(OOO)", type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002812 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002813 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002814 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002815 }
Fred Drake5755ce62001-06-27 19:19:46 +00002816 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002817 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002818 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00002819 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002820 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00002821 Py_XDECREF(type);
2822 Py_XDECREF(value);
2823 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002824 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002825}
2826
Fred Drake4ec5d562001-10-04 19:26:43 +00002827static void
2828call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
2829 int what)
2830{
2831 PyObject *type, *value, *traceback;
2832 int err;
2833 PyErr_Fetch(&type, &value, &traceback);
2834 err = call_trace(func, obj, frame, what, NULL);
2835 if (err == 0)
2836 PyErr_Restore(type, value, traceback);
2837 else {
2838 Py_XDECREF(type);
2839 Py_XDECREF(value);
2840 Py_XDECREF(traceback);
2841 }
2842}
2843
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002844static int
Fred Drake5755ce62001-06-27 19:19:46 +00002845call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
2846 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002847{
Fred Drake5755ce62001-06-27 19:19:46 +00002848 register PyThreadState *tstate = frame->f_tstate;
2849 int result;
2850 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002851 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002852 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00002853 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00002854 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00002855 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
2856 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00002857 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00002858 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002859}
2860
Fred Drake5755ce62001-06-27 19:19:46 +00002861void
2862PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00002863{
Fred Drake5755ce62001-06-27 19:19:46 +00002864 PyThreadState *tstate = PyThreadState_Get();
2865 PyObject *temp = tstate->c_profileobj;
2866 Py_XINCREF(arg);
2867 tstate->c_profilefunc = NULL;
2868 tstate->c_profileobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00002869 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00002870 Py_XDECREF(temp);
2871 tstate->c_profilefunc = func;
2872 tstate->c_profileobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00002873 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00002874}
2875
2876void
2877PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
2878{
2879 PyThreadState *tstate = PyThreadState_Get();
2880 PyObject *temp = tstate->c_traceobj;
2881 Py_XINCREF(arg);
2882 tstate->c_tracefunc = NULL;
2883 tstate->c_traceobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00002884 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00002885 Py_XDECREF(temp);
2886 tstate->c_tracefunc = func;
2887 tstate->c_traceobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00002888 tstate->use_tracing = ((func != NULL)
2889 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00002890}
2891
Guido van Rossumb209a111997-04-29 18:18:01 +00002892PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002893PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00002894{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002895 PyThreadState *tstate = PyThreadState_Get();
2896 PyFrameObject *current_frame = tstate->frame;
Guido van Rossum6135a871995-01-09 17:53:26 +00002897 if (current_frame == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +00002898 return tstate->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00002899 else
2900 return current_frame->f_builtins;
2901}
2902
Guido van Rossumb209a111997-04-29 18:18:01 +00002903PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002904PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00002905{
Guido van Rossuma027efa1997-05-05 20:56:21 +00002906 PyFrameObject *current_frame = PyThreadState_Get()->frame;
Guido van Rossum5b722181993-03-30 17:46:03 +00002907 if (current_frame == NULL)
2908 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00002909 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00002910 return current_frame->f_locals;
2911}
2912
Guido van Rossumb209a111997-04-29 18:18:01 +00002913PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002914PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00002915{
Guido van Rossuma027efa1997-05-05 20:56:21 +00002916 PyFrameObject *current_frame = PyThreadState_Get()->frame;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002917 if (current_frame == NULL)
2918 return NULL;
2919 else
2920 return current_frame->f_globals;
2921}
2922
Guido van Rossumb209a111997-04-29 18:18:01 +00002923PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002924PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00002925{
Guido van Rossuma027efa1997-05-05 20:56:21 +00002926 PyFrameObject *current_frame = PyThreadState_Get()->frame;
Guido van Rossumb209a111997-04-29 18:18:01 +00002927 return (PyObject *)current_frame;
Guido van Rossume59214e1994-08-30 08:01:59 +00002928}
2929
Guido van Rossum6135a871995-01-09 17:53:26 +00002930int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002931PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00002932{
Guido van Rossuma027efa1997-05-05 20:56:21 +00002933 PyFrameObject *current_frame = PyThreadState_Get()->frame;
Guido van Rossum6135a871995-01-09 17:53:26 +00002934 return current_frame == NULL ? 0 : current_frame->f_restricted;
2935}
2936
Guido van Rossumbe270261997-05-22 22:26:18 +00002937int
Tim Peters5ba58662001-07-16 02:29:45 +00002938PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00002939{
2940 PyFrameObject *current_frame = PyThreadState_Get()->frame;
Tim Peters5ba58662001-07-16 02:29:45 +00002941 int result = 0;
2942
2943 if (current_frame != NULL) {
2944 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00002945 const int compilerflags = codeflags & PyCF_MASK;
2946 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00002947 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00002948 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00002949 }
2950 }
2951 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00002952}
2953
2954int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002955Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002956{
Guido van Rossumb209a111997-04-29 18:18:01 +00002957 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00002958 if (f == NULL)
2959 return 0;
2960 if (!PyFile_SoftSpace(f, 0))
2961 return 0;
2962 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002963}
2964
Guido van Rossum3f5da241990-12-20 15:06:42 +00002965
Guido van Rossum681d79a1995-07-18 14:51:37 +00002966/* External interface to call any callable object.
2967 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00002968
Guido van Rossumd7ed6831997-08-30 15:02:50 +00002969#undef PyEval_CallObject
2970/* for backward compatibility: export this interface */
2971
Guido van Rossumb209a111997-04-29 18:18:01 +00002972PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002973PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00002974{
Guido van Rossumb209a111997-04-29 18:18:01 +00002975 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002976}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00002977#define PyEval_CallObject(func,arg) \
2978 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00002979
Guido van Rossumb209a111997-04-29 18:18:01 +00002980PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002981PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00002982{
Jeremy Hylton52820442001-01-03 23:52:36 +00002983 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002984
2985 if (arg == NULL)
Guido van Rossumb209a111997-04-29 18:18:01 +00002986 arg = PyTuple_New(0);
2987 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002988 PyErr_SetString(PyExc_TypeError,
2989 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002990 return NULL;
2991 }
2992 else
Guido van Rossumb209a111997-04-29 18:18:01 +00002993 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002994
Guido van Rossumb209a111997-04-29 18:18:01 +00002995 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002996 PyErr_SetString(PyExc_TypeError,
2997 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00002998 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00002999 return NULL;
3000 }
3001
Tim Peters6d6c1a32001-08-02 04:15:00 +00003002 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003003 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003004 return result;
3005}
3006
Tim Peters6d6c1a32001-08-02 04:15:00 +00003007char *
3008PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003009{
3010 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003011 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003012 else if (PyFunction_Check(func))
3013 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3014 else if (PyCFunction_Check(func))
3015 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3016 else if (PyClass_Check(func))
3017 return PyString_AsString(((PyClassObject*)func)->cl_name);
3018 else if (PyInstance_Check(func)) {
3019 return PyString_AsString(
3020 ((PyInstanceObject*)func)->in_class->cl_name);
3021 } else {
3022 return func->ob_type->tp_name;
3023 }
3024}
3025
Tim Peters6d6c1a32001-08-02 04:15:00 +00003026char *
3027PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003028{
3029 if (PyMethod_Check(func))
3030 return "()";
3031 else if (PyFunction_Check(func))
3032 return "()";
3033 else if (PyCFunction_Check(func))
3034 return "()";
3035 else if (PyClass_Check(func))
3036 return " constructor";
3037 else if (PyInstance_Check(func)) {
3038 return " instance";
3039 } else {
3040 return " object";
3041 }
3042}
3043
Jeremy Hylton52820442001-01-03 23:52:36 +00003044#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
3045
3046/* The two fast_xxx() functions optimize calls for which no argument
3047 tuple is necessary; the objects are passed directly from the stack.
3048 fast_cfunction() is called for METH_OLDARGS functions.
3049 fast_function() is for functions with no special argument handling.
3050*/
3051
3052static PyObject *
3053fast_cfunction(PyObject *func, PyObject ***pp_stack, int na)
3054{
3055 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3056 PyObject *self = PyCFunction_GET_SELF(func);
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00003057 int flags = PyCFunction_GET_FLAGS(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003058
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00003059 switch (flags) {
3060 case METH_OLDARGS:
3061 if (na == 0)
3062 return (*meth)(self, NULL);
3063 else if (na == 1) {
3064 PyObject *arg = EXT_POP(*pp_stack);
3065 PyObject *result = (*meth)(self, arg);
3066 Py_DECREF(arg);
3067 return result;
3068 } else {
3069 PyObject *args = load_args(pp_stack, na);
3070 PyObject *result = (*meth)(self, args);
3071 Py_DECREF(args);
3072 return result;
3073 }
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00003074 case METH_NOARGS:
3075 if (na == 0)
3076 return (*meth)(self, NULL);
3077 PyErr_Format(PyExc_TypeError,
3078 "%.200s() takes no arguments (%d given)",
3079 ((PyCFunctionObject*)func)->m_ml->ml_name, na);
3080 return NULL;
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00003081 case METH_O:
3082 if (na == 1) {
3083 PyObject *arg = EXT_POP(*pp_stack);
3084 PyObject *result = (*meth)(self, arg);
3085 Py_DECREF(arg);
3086 return result;
3087 }
3088 PyErr_Format(PyExc_TypeError,
3089 "%.200s() takes exactly one argument (%d given)",
3090 ((PyCFunctionObject*)func)->m_ml->ml_name, na);
3091 return NULL;
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00003092 default:
3093 fprintf(stderr, "%.200s() flags = %d\n",
3094 ((PyCFunctionObject*)func)->m_ml->ml_name, flags);
3095 PyErr_BadInternalCall();
3096 return NULL;
3097 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003098}
3099
3100static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003101fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003102{
3103 PyObject *co = PyFunction_GET_CODE(func);
3104 PyObject *globals = PyFunction_GET_GLOBALS(func);
3105 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003106 PyObject *closure = PyFunction_GET_CLOSURE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003107 PyObject **d = NULL;
3108 int nd = 0;
3109
3110 if (argdefs != NULL) {
3111 d = &PyTuple_GET_ITEM(argdefs, 0);
3112 nd = ((PyTupleObject *)argdefs)->ob_size;
3113 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003114 return PyEval_EvalCodeEx((PyCodeObject *)co, globals,
Jeremy Hylton52820442001-01-03 23:52:36 +00003115 (PyObject *)NULL, (*pp_stack)-n, na,
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003116 (*pp_stack)-2*nk, nk, d, nd,
3117 closure);
Jeremy Hylton52820442001-01-03 23:52:36 +00003118}
3119
3120static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003121update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3122 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003123{
3124 PyObject *kwdict = NULL;
3125 if (orig_kwdict == NULL)
3126 kwdict = PyDict_New();
3127 else {
3128 kwdict = PyDict_Copy(orig_kwdict);
3129 Py_DECREF(orig_kwdict);
3130 }
3131 if (kwdict == NULL)
3132 return NULL;
3133 while (--nk >= 0) {
3134 int err;
3135 PyObject *value = EXT_POP(*pp_stack);
3136 PyObject *key = EXT_POP(*pp_stack);
3137 if (PyDict_GetItem(kwdict, key) != NULL) {
Guido van Rossumac7be682001-01-17 15:42:30 +00003138 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +00003139 "%.200s%s got multiple values "
Jeremy Hylton512a2372001-04-11 13:52:29 +00003140 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003141 PyEval_GetFuncName(func),
3142 PyEval_GetFuncDesc(func),
Jeremy Hylton512a2372001-04-11 13:52:29 +00003143 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003144 Py_DECREF(key);
3145 Py_DECREF(value);
3146 Py_DECREF(kwdict);
3147 return NULL;
3148 }
3149 err = PyDict_SetItem(kwdict, key, value);
3150 Py_DECREF(key);
3151 Py_DECREF(value);
3152 if (err) {
3153 Py_DECREF(kwdict);
3154 return NULL;
3155 }
3156 }
3157 return kwdict;
3158}
3159
3160static PyObject *
3161update_star_args(int nstack, int nstar, PyObject *stararg,
3162 PyObject ***pp_stack)
3163{
3164 PyObject *callargs, *w;
3165
3166 callargs = PyTuple_New(nstack + nstar);
3167 if (callargs == NULL) {
3168 return NULL;
3169 }
3170 if (nstar) {
3171 int i;
3172 for (i = 0; i < nstar; i++) {
3173 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3174 Py_INCREF(a);
3175 PyTuple_SET_ITEM(callargs, nstack + i, a);
3176 }
3177 }
3178 while (--nstack >= 0) {
3179 w = EXT_POP(*pp_stack);
3180 PyTuple_SET_ITEM(callargs, nstack, w);
3181 }
3182 return callargs;
3183}
3184
3185static PyObject *
3186load_args(PyObject ***pp_stack, int na)
3187{
3188 PyObject *args = PyTuple_New(na);
3189 PyObject *w;
3190
3191 if (args == NULL)
3192 return NULL;
3193 while (--na >= 0) {
3194 w = EXT_POP(*pp_stack);
3195 PyTuple_SET_ITEM(args, na, w);
3196 }
3197 return args;
3198}
3199
3200static PyObject *
3201do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3202{
3203 PyObject *callargs = NULL;
3204 PyObject *kwdict = NULL;
3205 PyObject *result = NULL;
3206
3207 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003208 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003209 if (kwdict == NULL)
3210 goto call_fail;
3211 }
3212 callargs = load_args(pp_stack, na);
3213 if (callargs == NULL)
3214 goto call_fail;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003215 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003216 call_fail:
3217 Py_XDECREF(callargs);
3218 Py_XDECREF(kwdict);
3219 return result;
3220}
3221
3222static PyObject *
3223ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3224{
3225 int nstar = 0;
3226 PyObject *callargs = NULL;
3227 PyObject *stararg = NULL;
3228 PyObject *kwdict = NULL;
3229 PyObject *result = NULL;
3230
3231 if (flags & CALL_FLAG_KW) {
3232 kwdict = EXT_POP(*pp_stack);
3233 if (!(kwdict && PyDict_Check(kwdict))) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003234 PyErr_Format(PyExc_TypeError,
Jeremy Hylton512a2372001-04-11 13:52:29 +00003235 "%s%s argument after ** "
3236 "must be a dictionary",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003237 PyEval_GetFuncName(func),
3238 PyEval_GetFuncDesc(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003239 goto ext_call_fail;
3240 }
3241 }
3242 if (flags & CALL_FLAG_VAR) {
3243 stararg = EXT_POP(*pp_stack);
3244 if (!PyTuple_Check(stararg)) {
3245 PyObject *t = NULL;
3246 t = PySequence_Tuple(stararg);
3247 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003248 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3249 PyErr_Format(PyExc_TypeError,
3250 "%s%s argument after * "
3251 "must be a sequence",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003252 PyEval_GetFuncName(func),
3253 PyEval_GetFuncDesc(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003254 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003255 goto ext_call_fail;
3256 }
3257 Py_DECREF(stararg);
3258 stararg = t;
3259 }
3260 nstar = PyTuple_GET_SIZE(stararg);
3261 }
3262 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003263 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003264 if (kwdict == NULL)
3265 goto ext_call_fail;
3266 }
3267 callargs = update_star_args(na, nstar, stararg, pp_stack);
3268 if (callargs == NULL)
3269 goto ext_call_fail;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003270 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003271 ext_call_fail:
3272 Py_XDECREF(callargs);
3273 Py_XDECREF(kwdict);
3274 Py_XDECREF(stararg);
3275 return result;
3276}
3277
Guido van Rossum3b9c6671996-07-30 18:40:29 +00003278#define SLICE_ERROR_MSG \
3279 "standard sequence type does not support step size other than one"
3280
Guido van Rossumb209a111997-04-29 18:18:01 +00003281static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003282loop_subscript(PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003283{
Guido van Rossumb209a111997-04-29 18:18:01 +00003284 PySequenceMethods *sq = v->ob_type->tp_as_sequence;
Guido van Rossume59214e1994-08-30 08:01:59 +00003285 int i;
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00003286 if (sq == NULL || sq->sq_item == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003287 PyErr_SetString(PyExc_TypeError, "loop over non-sequence");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003288 return NULL;
3289 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003290 i = PyInt_AsLong(w);
Guido van Rossume59214e1994-08-30 08:01:59 +00003291 v = (*sq->sq_item)(v, i);
3292 if (v)
3293 return v;
Barry Warsaw4249f541997-08-22 21:26:19 +00003294 if (PyErr_ExceptionMatches(PyExc_IndexError))
Guido van Rossumb209a111997-04-29 18:18:01 +00003295 PyErr_Clear();
Guido van Rossume59214e1994-08-30 08:01:59 +00003296 return NULL;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003297}
3298
Guido van Rossum20c6add2000-05-08 14:06:50 +00003299/* Extract a slice index from a PyInt or PyLong, the index is bound to
3300 the range [-INT_MAX+1, INTMAX]. Returns 0 and an exception if there is
3301 and error. Returns 1 on success.*/
3302
3303int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003304_PyEval_SliceIndex(PyObject *v, int *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003305{
3306 if (v != NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003307 long x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003308 if (PyInt_Check(v)) {
3309 x = PyInt_AsLong(v);
3310 } else if (PyLong_Check(v)) {
3311 x = PyLong_AsLong(v);
3312 if (x==-1 && PyErr_Occurred()) {
3313 PyObject *long_zero;
Guido van Rossumac7be682001-01-17 15:42:30 +00003314 int cmp;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003315
Guido van Rossumac7be682001-01-17 15:42:30 +00003316 if (!PyErr_ExceptionMatches(
3317 PyExc_OverflowError)) {
3318 /* It's not an overflow error, so just
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003319 signal an error */
Guido van Rossum20c6add2000-05-08 14:06:50 +00003320 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003321 }
3322
Guido van Rossumac7be682001-01-17 15:42:30 +00003323 /* Clear the OverflowError */
3324 PyErr_Clear();
3325
3326 /* It's an overflow error, so we need to
3327 check the sign of the long integer,
3328 set the value to INT_MAX or 0, and clear
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003329 the error. */
3330
3331 /* Create a long integer with a value of 0 */
Guido van Rossumac7be682001-01-17 15:42:30 +00003332 long_zero = PyLong_FromLong(0L);
Guido van Rossum20c6add2000-05-08 14:06:50 +00003333 if (long_zero == NULL) return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003334
3335 /* Check sign */
Guido van Rossumac7be682001-01-17 15:42:30 +00003336 cmp = PyObject_RichCompareBool(v, long_zero,
3337 Py_GT);
3338 Py_DECREF(long_zero);
3339 if (cmp < 0)
3340 return 0;
3341 else if (cmp > 0)
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003342 x = INT_MAX;
3343 else
3344 x = 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003345 }
3346 } else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003347 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003348 "slice indices must be integers");
Guido van Rossum20c6add2000-05-08 14:06:50 +00003349 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003350 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003351 /* Truncate -- very long indices are truncated anyway */
3352 if (x > INT_MAX)
3353 x = INT_MAX;
3354 else if (x < -INT_MAX)
3355 x = 0;
3356 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003357 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00003358 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003359}
3360
Guido van Rossum50d756e2001-08-18 17:43:36 +00003361#undef ISINT
3362#define ISINT(x) ((x) == NULL || PyInt_Check(x) || PyLong_Check(x))
3363
Guido van Rossumb209a111997-04-29 18:18:01 +00003364static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003365apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003366{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003367 PyTypeObject *tp = u->ob_type;
3368 PySequenceMethods *sq = tp->tp_as_sequence;
3369
3370 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3371 int ilow = 0, ihigh = INT_MAX;
3372 if (!_PyEval_SliceIndex(v, &ilow))
3373 return NULL;
3374 if (!_PyEval_SliceIndex(w, &ihigh))
3375 return NULL;
3376 return PySequence_GetSlice(u, ilow, ihigh);
3377 }
3378 else {
3379 PyObject *slice = PySlice_New(v, w, NULL);
3380 if (slice != NULL)
3381 return PyObject_GetItem(u, slice);
3382 else
3383 return NULL;
3384 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003385}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003386
3387static int
Guido van Rossumac7be682001-01-17 15:42:30 +00003388assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
3389 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003390{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003391 PyTypeObject *tp = u->ob_type;
3392 PySequenceMethods *sq = tp->tp_as_sequence;
3393
3394 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3395 int ilow = 0, ihigh = INT_MAX;
3396 if (!_PyEval_SliceIndex(v, &ilow))
3397 return -1;
3398 if (!_PyEval_SliceIndex(w, &ihigh))
3399 return -1;
3400 if (x == NULL)
3401 return PySequence_DelSlice(u, ilow, ihigh);
3402 else
3403 return PySequence_SetSlice(u, ilow, ihigh, x);
3404 }
3405 else {
3406 PyObject *slice = PySlice_New(v, w, NULL);
3407 if (slice != NULL) {
3408 if (x != NULL)
3409 return PyObject_SetItem(u, slice, x);
3410 else
3411 return PyObject_DelItem(u, slice);
3412 }
3413 else
3414 return -1;
3415 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003416}
3417
Guido van Rossumb209a111997-04-29 18:18:01 +00003418static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003419cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003420{
Guido van Rossumac7be682001-01-17 15:42:30 +00003421 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003422 switch (op) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00003423 case IS:
3424 case IS_NOT:
3425 res = (v == w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00003426 if (op == (int) IS_NOT)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003427 res = !res;
3428 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003429 case IN:
3430 case NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00003431 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003432 if (res < 0)
3433 return NULL;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003434 if (op == (int) NOT_IN)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003435 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003436 break;
3437 case EXC_MATCH:
Barry Warsaw4249f541997-08-22 21:26:19 +00003438 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003439 break;
3440 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00003441 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003442 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003443 v = res ? Py_True : Py_False;
3444 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003445 return v;
3446}
3447
Thomas Wouters52152252000-08-17 22:55:00 +00003448static PyObject *
3449import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003450{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003451 PyObject *x;
3452
3453 x = PyObject_GetAttr(v, name);
3454 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00003455 PyErr_Format(PyExc_ImportError,
3456 "cannot import name %.230s",
3457 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003458 }
Thomas Wouters52152252000-08-17 22:55:00 +00003459 return x;
3460}
Guido van Rossumac7be682001-01-17 15:42:30 +00003461
Thomas Wouters52152252000-08-17 22:55:00 +00003462static int
3463import_all_from(PyObject *locals, PyObject *v)
3464{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003465 PyObject *all = PyObject_GetAttrString(v, "__all__");
3466 PyObject *dict, *name, *value;
3467 int skip_leading_underscores = 0;
3468 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00003469
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003470 if (all == NULL) {
3471 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3472 return -1; /* Unexpected error */
3473 PyErr_Clear();
3474 dict = PyObject_GetAttrString(v, "__dict__");
3475 if (dict == NULL) {
3476 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3477 return -1;
3478 PyErr_SetString(PyExc_ImportError,
3479 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00003480 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003481 }
3482 all = PyMapping_Keys(dict);
3483 Py_DECREF(dict);
3484 if (all == NULL)
3485 return -1;
3486 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00003487 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003488
3489 for (pos = 0, err = 0; ; pos++) {
3490 name = PySequence_GetItem(all, pos);
3491 if (name == NULL) {
3492 if (!PyErr_ExceptionMatches(PyExc_IndexError))
3493 err = -1;
3494 else
3495 PyErr_Clear();
3496 break;
3497 }
3498 if (skip_leading_underscores &&
3499 PyString_Check(name) &&
3500 PyString_AS_STRING(name)[0] == '_')
3501 {
3502 Py_DECREF(name);
3503 continue;
3504 }
3505 value = PyObject_GetAttr(v, name);
3506 if (value == NULL)
3507 err = -1;
3508 else
3509 err = PyDict_SetItem(locals, name, value);
3510 Py_DECREF(name);
3511 Py_XDECREF(value);
3512 if (err != 0)
3513 break;
3514 }
3515 Py_DECREF(all);
3516 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00003517}
3518
Guido van Rossumb209a111997-04-29 18:18:01 +00003519static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003520build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003521{
Guido van Rossum7851eea2001-09-12 19:19:18 +00003522 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003523
3524 if (PyDict_Check(methods))
3525 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00003526 if (metaclass != NULL)
3527 Py_INCREF(methods);
3528 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
3529 base = PyTuple_GET_ITEM(bases, 0);
3530 metaclass = PyObject_GetAttrString(base, "__class__");
3531 if (metaclass == NULL) {
3532 PyErr_Clear();
3533 metaclass = (PyObject *)base->ob_type;
3534 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00003535 }
3536 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00003537 else {
3538 PyObject *g = PyEval_GetGlobals();
3539 if (g != NULL && PyDict_Check(g))
3540 metaclass = PyDict_GetItemString(g, "__metaclass__");
3541 if (metaclass == NULL)
3542 metaclass = (PyObject *) &PyClass_Type;
3543 Py_INCREF(metaclass);
3544 }
3545 result = PyObject_CallFunction(metaclass, "OOO", name, bases, methods);
3546 Py_DECREF(metaclass);
3547 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00003548}
3549
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003550static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003551exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
3552 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003553{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003554 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00003555 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003556 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003557
Guido van Rossumb209a111997-04-29 18:18:01 +00003558 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
3559 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003560 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00003561 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003562 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00003563 locals = PyTuple_GetItem(prog, 2);
3564 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003565 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003566 if (globals == Py_None) {
3567 globals = PyEval_GetGlobals();
3568 if (locals == Py_None) {
3569 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00003570 plain = 1;
3571 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003572 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003573 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003574 locals = globals;
Guido van Rossumb209a111997-04-29 18:18:01 +00003575 if (!PyString_Check(prog) &&
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00003576 !PyUnicode_Check(prog) &&
Guido van Rossumb209a111997-04-29 18:18:01 +00003577 !PyCode_Check(prog) &&
3578 !PyFile_Check(prog)) {
3579 PyErr_SetString(PyExc_TypeError,
Guido van Rossumac7be682001-01-17 15:42:30 +00003580 "exec: arg 1 must be a string, file, or code object");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003581 return -1;
3582 }
Fred Drake661ea262000-10-24 19:57:45 +00003583 if (!PyDict_Check(globals)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003584 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003585 "exec: arg 2 must be a dictionary or None");
3586 return -1;
3587 }
3588 if (!PyDict_Check(locals)) {
3589 PyErr_SetString(PyExc_TypeError,
3590 "exec: arg 3 must be a dictionary or None");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003591 return -1;
3592 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003593 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00003594 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00003595 if (PyCode_Check(prog)) {
Guido van Rossuma400d8a2000-01-12 22:45:54 +00003596 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003597 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00003598 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003599 FILE *fp = PyFile_AsFile(prog);
3600 char *name = PyString_AsString(PyFile_Name(prog));
Tim Peters5ba58662001-07-16 02:29:45 +00003601 PyCompilerFlags cf;
3602 cf.cf_flags = 0;
3603 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00003604 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
3605 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00003606 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00003607 v = PyRun_File(fp, name, Py_file_input, globals,
3608 locals);
Guido van Rossuma400d8a2000-01-12 22:45:54 +00003609 }
3610 else {
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00003611 char *str;
Tim Peters5ba58662001-07-16 02:29:45 +00003612 PyCompilerFlags cf;
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00003613 if (PyString_AsStringAndSize(prog, &str, NULL))
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003614 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00003615 cf.cf_flags = 0;
3616 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00003617 v = PyRun_StringFlags(str, Py_file_input, globals,
3618 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00003619 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00003620 v = PyRun_String(str, Py_file_input, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003621 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00003622 if (plain)
3623 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003624 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003625 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00003626 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003627 return 0;
3628}
Guido van Rossum24c13741995-02-14 09:42:43 +00003629
Guido van Rossumac7be682001-01-17 15:42:30 +00003630static void
Paul Prescode68140d2000-08-30 20:25:01 +00003631format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
3632{
3633 char *obj_str;
3634
3635 if (!obj)
3636 return;
3637
3638 obj_str = PyString_AsString(obj);
3639 if (!obj_str)
3640 return;
3641
3642 PyErr_Format(exc, format_str, obj_str);
3643}
Guido van Rossum950361c1997-01-24 13:49:28 +00003644
3645#ifdef DYNAMIC_EXECUTION_PROFILE
3646
Skip Montanarof118cb12001-10-15 20:51:38 +00003647static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003648getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00003649{
3650 int i;
3651 PyObject *l = PyList_New(256);
3652 if (l == NULL) return NULL;
3653 for (i = 0; i < 256; i++) {
3654 PyObject *x = PyInt_FromLong(a[i]);
3655 if (x == NULL) {
3656 Py_DECREF(l);
3657 return NULL;
3658 }
3659 PyList_SetItem(l, i, x);
3660 }
3661 for (i = 0; i < 256; i++)
3662 a[i] = 0;
3663 return l;
3664}
3665
3666PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003667_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00003668{
3669#ifndef DXPAIRS
3670 return getarray(dxp);
3671#else
3672 int i;
3673 PyObject *l = PyList_New(257);
3674 if (l == NULL) return NULL;
3675 for (i = 0; i < 257; i++) {
3676 PyObject *x = getarray(dxpairs[i]);
3677 if (x == NULL) {
3678 Py_DECREF(l);
3679 return NULL;
3680 }
3681 PyList_SetItem(l, i, x);
3682 }
3683 return l;
3684#endif
3685}
3686
3687#endif