blob: 73743dedd4497e5f30251fb711f09d945a6d4897 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003
Guido van Rossum681d79a1995-07-18 14:51:37 +00004/* XXX TO DO:
Guido van Rossum681d79a1995-07-18 14:51:37 +00005 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +00006 XXX document it!
7 */
8
Guido van Rossumb209a111997-04-29 18:18:01 +00009#include "Python.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000010
Guido van Rossum10dc2e81990-11-18 17:27:39 +000011#include "compile.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000012#include "frameobject.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000013#include "eval.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000014#include "opcode.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000015#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000016
Jack Jansencbf630f2000-07-11 21:59:16 +000017#ifdef macintosh
18#include "macglue.h"
19#endif
20
Guido van Rossumc6004111993-11-05 10:22:19 +000021#include <ctype.h>
22
Guido van Rossum04691fc1992-08-12 15:35:34 +000023/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000024/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000025
Guido van Rossum408027e1996-12-30 16:17:54 +000026#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000027/* For debugging the interpreter: */
28#define LLTRACE 1 /* Low-level trace feature */
29#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000030#endif
31
Jeremy Hylton52820442001-01-03 23:52:36 +000032typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000033
Guido van Rossum374a9221991-04-04 10:40:29 +000034/* Forward declarations */
Tim Peters5ca576e2001-06-18 22:08:13 +000035static PyObject *eval_frame(PyFrameObject *);
Jeremy Hyltone8c04322002-08-16 17:47:26 +000036static PyObject *call_function(PyObject ***, int);
Jeremy Hylton52820442001-01-03 23:52:36 +000037static PyObject *fast_function(PyObject *, PyObject ***, int, int, int);
Jeremy Hylton52820442001-01-03 23:52:36 +000038static PyObject *do_call(PyObject *, PyObject ***, int, int);
39static PyObject *ext_do_call(PyObject *, PyObject ***, int, int, int);
Guido van Rossumac7be682001-01-17 15:42:30 +000040static PyObject *update_keyword_args(PyObject *, int, PyObject ***,PyObject *);
Ka-Ping Yee20579702001-01-15 22:14:16 +000041static PyObject *update_star_args(int, int, PyObject *, PyObject ***);
Jeremy Hylton52820442001-01-03 23:52:36 +000042static PyObject *load_args(PyObject ***, int);
43#define CALL_FLAG_VAR 1
44#define CALL_FLAG_KW 2
45
Guido van Rossum0a066c01992-03-27 17:29:15 +000046#ifdef LLTRACE
Tim Petersdbd9ba62000-07-09 03:09:57 +000047static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000048#endif
Fred Drake5755ce62001-06-27 19:19:46 +000049static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
50 int, PyObject *);
Fred Drake4ec5d562001-10-04 19:26:43 +000051static void call_trace_protected(Py_tracefunc, PyObject *,
52 PyFrameObject *, int);
Fred Drake5755ce62001-06-27 19:19:46 +000053static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
Michael W. Hudson006c7522002-11-08 13:08:46 +000054static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Michael W. Hudsondd32a912002-08-15 14:59:02 +000055 PyFrameObject *, int *, int *);
56
Tim Petersdbd9ba62000-07-09 03:09:57 +000057static PyObject *apply_slice(PyObject *, PyObject *, PyObject *);
58static int assign_slice(PyObject *, PyObject *,
59 PyObject *, PyObject *);
60static PyObject *cmp_outcome(int, PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +000061static PyObject *import_from(PyObject *, PyObject *);
62static int import_all_from(PyObject *, PyObject *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000063static PyObject *build_class(PyObject *, PyObject *, PyObject *);
64static int exec_statement(PyFrameObject *,
65 PyObject *, PyObject *, PyObject *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000066static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *);
67static void reset_exc_info(PyThreadState *);
Paul Prescode68140d2000-08-30 20:25:01 +000068static void format_exc_check_arg(PyObject *, char *, PyObject *);
Guido van Rossum374a9221991-04-04 10:40:29 +000069
Paul Prescode68140d2000-08-30 20:25:01 +000070#define NAME_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +000071 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +000072#define GLOBAL_NAME_ERROR_MSG \
73 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000074#define UNBOUNDLOCAL_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +000075 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000076#define UNBOUNDFREE_ERROR_MSG \
77 "free variable '%.200s' referenced before assignment" \
78 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000079
Guido van Rossum950361c1997-01-24 13:49:28 +000080/* Dynamic execution profile */
81#ifdef DYNAMIC_EXECUTION_PROFILE
82#ifdef DXPAIRS
83static long dxpairs[257][256];
84#define dxp dxpairs[256]
85#else
86static long dxp[256];
87#endif
88#endif
89
Jeremy Hylton985eba52003-02-05 23:13:00 +000090/* Function call profile */
91#ifdef CALL_PROFILE
92#define PCALL_NUM 11
93static int pcall[PCALL_NUM];
94
95#define PCALL_ALL 0
96#define PCALL_FUNCTION 1
97#define PCALL_FAST_FUNCTION 2
98#define PCALL_FASTER_FUNCTION 3
99#define PCALL_METHOD 4
100#define PCALL_BOUND_METHOD 5
101#define PCALL_CFUNCTION 6
102#define PCALL_TYPE 7
103#define PCALL_GENERATOR 8
104#define PCALL_OTHER 9
105#define PCALL_POP 10
106
107/* Notes about the statistics
108
109 PCALL_FAST stats
110
111 FAST_FUNCTION means no argument tuple needs to be created.
112 FASTER_FUNCTION means that the fast-path frame setup code is used.
113
114 If there is a method call where the call can be optimized by changing
115 the argument tuple and calling the function directly, it gets recorded
116 twice.
117
118 As a result, the relationship among the statistics appears to be
119 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
120 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
121 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
122 PCALL_METHOD > PCALL_BOUND_METHOD
123*/
124
125#define PCALL(POS) pcall[POS]++
126
127PyObject *
128PyEval_GetCallStats(PyObject *self)
129{
130 return Py_BuildValue("iiiiiiiiii",
131 pcall[0], pcall[1], pcall[2], pcall[3],
132 pcall[4], pcall[5], pcall[6], pcall[7],
133 pcall[8], pcall[9]);
134}
135#else
136#define PCALL(O)
137
138PyObject *
139PyEval_GetCallStats(PyObject *self)
140{
141 Py_INCREF(Py_None);
142 return Py_None;
143}
144#endif
145
Jeremy Hylton938ace62002-07-17 16:30:39 +0000146static PyTypeObject gentype;
Tim Peters5ca576e2001-06-18 22:08:13 +0000147
148typedef struct {
149 PyObject_HEAD
Tim Petersd8e1c9e2001-06-26 20:58:58 +0000150 /* The gi_ prefix is intended to remind of generator-iterator. */
151
152 PyFrameObject *gi_frame;
153
Tim Peterse77f2e22001-06-26 22:24:51 +0000154 /* True if generator is being executed. */
155 int gi_running;
Fred Drake72bc4562002-08-09 18:35:52 +0000156
157 /* List of weak reference. */
158 PyObject *gi_weakreflist;
Tim Peters5ca576e2001-06-18 22:08:13 +0000159} genobject;
160
161static PyObject *
162gen_new(PyFrameObject *f)
163{
Neil Schemenauer08de92a2002-03-18 20:45:09 +0000164 genobject *gen = PyObject_GC_New(genobject, &gentype);
Tim Peters5ca576e2001-06-18 22:08:13 +0000165 if (gen == NULL) {
166 Py_DECREF(f);
167 return NULL;
168 }
Tim Petersd8e1c9e2001-06-26 20:58:58 +0000169 gen->gi_frame = f;
170 gen->gi_running = 0;
Fred Drake72bc4562002-08-09 18:35:52 +0000171 gen->gi_weakreflist = NULL;
Neil Schemenauer08de92a2002-03-18 20:45:09 +0000172 _PyObject_GC_TRACK(gen);
Tim Peters5ca576e2001-06-18 22:08:13 +0000173 return (PyObject *)gen;
174}
175
Neil Schemenauerf8c7c202001-07-12 13:27:49 +0000176static int
177gen_traverse(genobject *gen, visitproc visit, void *arg)
178{
179 return visit((PyObject *)gen->gi_frame, arg);
180}
181
Tim Peters5ca576e2001-06-18 22:08:13 +0000182static void
183gen_dealloc(genobject *gen)
184{
Neil Schemenauer08de92a2002-03-18 20:45:09 +0000185 _PyObject_GC_UNTRACK(gen);
Fred Drake72bc4562002-08-09 18:35:52 +0000186 if (gen->gi_weakreflist != NULL)
187 PyObject_ClearWeakRefs((PyObject *) gen);
Tim Petersd8e1c9e2001-06-26 20:58:58 +0000188 Py_DECREF(gen->gi_frame);
Neil Schemenauer08de92a2002-03-18 20:45:09 +0000189 PyObject_GC_Del(gen);
Tim Peters5ca576e2001-06-18 22:08:13 +0000190}
191
192static PyObject *
193gen_iternext(genobject *gen)
194{
Neil Schemenauer2b13ce82001-06-21 02:41:10 +0000195 PyThreadState *tstate = PyThreadState_GET();
Tim Petersd8e1c9e2001-06-26 20:58:58 +0000196 PyFrameObject *f = gen->gi_frame;
Tim Peters5ca576e2001-06-18 22:08:13 +0000197 PyObject *result;
198
Tim Petersd8e1c9e2001-06-26 20:58:58 +0000199 if (gen->gi_running) {
Tim Peters5ca576e2001-06-18 22:08:13 +0000200 PyErr_SetString(PyExc_ValueError,
201 "generator already executing");
202 return NULL;
203 }
Tim Peters8c963692001-06-23 05:26:56 +0000204 if (f->f_stacktop == NULL)
Tim Peters5ca576e2001-06-18 22:08:13 +0000205 return NULL;
Neil Schemenauer2b13ce82001-06-21 02:41:10 +0000206
207 /* Generators always return to their most recent caller, not
208 * necessarily their creator. */
Tim Peters5eb4b872001-06-23 05:47:56 +0000209 Py_XINCREF(tstate->frame);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +0000210 assert(f->f_back == NULL);
211 f->f_back = tstate->frame;
212
Tim Petersd8e1c9e2001-06-26 20:58:58 +0000213 gen->gi_running = 1;
Tim Peters5ca576e2001-06-18 22:08:13 +0000214 result = eval_frame(f);
Tim Petersd8e1c9e2001-06-26 20:58:58 +0000215 gen->gi_running = 0;
Neil Schemenauer2b13ce82001-06-21 02:41:10 +0000216
217 /* Don't keep the reference to f_back any longer than necessary. It
218 * may keep a chain of frames alive or it could create a reference
219 * cycle. */
Tim Peters5eb4b872001-06-23 05:47:56 +0000220 Py_XDECREF(f->f_back);
Tim Peters6302ec62001-06-20 06:57:32 +0000221 f->f_back = NULL;
Neil Schemenauer2b13ce82001-06-21 02:41:10 +0000222
Tim Petersad1a18b2001-06-23 06:19:16 +0000223 /* If the generator just returned (as opposed to yielding), signal
224 * that the generator is exhausted. */
225 if (result == Py_None && f->f_stacktop == NULL) {
226 Py_DECREF(result);
227 result = NULL;
228 }
229
Neil Schemenauer2b13ce82001-06-21 02:41:10 +0000230 return result;
Tim Peters5ca576e2001-06-18 22:08:13 +0000231}
232
233static PyObject *
Tim Peters5ca576e2001-06-18 22:08:13 +0000234gen_getiter(PyObject *gen)
235{
236 Py_INCREF(gen);
237 return gen;
238}
239
Guido van Rossum6f799372001-09-20 20:46:19 +0000240static PyMemberDef gen_memberlist[] = {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000241 {"gi_frame", T_OBJECT, offsetof(genobject, gi_frame), RO},
242 {"gi_running", T_INT, offsetof(genobject, gi_running), RO},
243 {NULL} /* Sentinel */
244};
Tim Peters5ca576e2001-06-18 22:08:13 +0000245
Tim Peters0c322792002-07-17 16:49:03 +0000246static PyTypeObject gentype = {
Tim Peters5ca576e2001-06-18 22:08:13 +0000247 PyObject_HEAD_INIT(&PyType_Type)
248 0, /* ob_size */
249 "generator", /* tp_name */
Neil Schemenauer08de92a2002-03-18 20:45:09 +0000250 sizeof(genobject), /* tp_basicsize */
Tim Peters5ca576e2001-06-18 22:08:13 +0000251 0, /* tp_itemsize */
252 /* methods */
253 (destructor)gen_dealloc, /* tp_dealloc */
254 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000255 0, /* tp_getattr */
Tim Peters5ca576e2001-06-18 22:08:13 +0000256 0, /* tp_setattr */
257 0, /* tp_compare */
258 0, /* tp_repr */
259 0, /* tp_as_number */
260 0, /* tp_as_sequence */
261 0, /* tp_as_mapping */
262 0, /* tp_hash */
263 0, /* tp_call */
264 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000265 PyObject_GenericGetAttr, /* tp_getattro */
Tim Peters5ca576e2001-06-18 22:08:13 +0000266 0, /* tp_setattro */
267 0, /* tp_as_buffer */
Neil Schemenauer08de92a2002-03-18 20:45:09 +0000268 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
Tim Peters5ca576e2001-06-18 22:08:13 +0000269 0, /* tp_doc */
Neil Schemenauerf8c7c202001-07-12 13:27:49 +0000270 (traverseproc)gen_traverse, /* tp_traverse */
Tim Peters5ca576e2001-06-18 22:08:13 +0000271 0, /* tp_clear */
272 0, /* tp_richcompare */
Fred Drake72bc4562002-08-09 18:35:52 +0000273 offsetof(genobject, gi_weakreflist), /* tp_weaklistoffset */
Tim Peters5ca576e2001-06-18 22:08:13 +0000274 (getiterfunc)gen_getiter, /* tp_iter */
275 (iternextfunc)gen_iternext, /* tp_iternext */
Tim Petersa64295b2002-07-17 00:15:22 +0000276 0, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000277 gen_memberlist, /* tp_members */
278 0, /* tp_getset */
279 0, /* tp_base */
280 0, /* tp_dict */
Tim Peters5ca576e2001-06-18 22:08:13 +0000281};
282
283
Guido van Rossume59214e1994-08-30 08:01:59 +0000284#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000285
Guido van Rossum2571cc81999-04-07 16:07:23 +0000286#ifndef DONT_HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000287#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000288#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000289#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000290
Guido van Rossuma027efa1997-05-05 20:56:21 +0000291extern int _PyThread_Started; /* Flag for Py_Exit */
292
Guido van Rossum65d5b571998-12-21 19:32:43 +0000293static PyThread_type_lock interpreter_lock = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000294static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000295
296void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000297PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000298{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000299 if (interpreter_lock)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000300 return;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000301 _PyThread_Started = 1;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000302 interpreter_lock = PyThread_allocate_lock();
303 PyThread_acquire_lock(interpreter_lock, 1);
304 main_thread = PyThread_get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000305}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000306
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000307void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000308PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000309{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000310 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000311}
312
313void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000314PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000315{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000316 PyThread_release_lock(interpreter_lock);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000317}
318
319void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000320PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000321{
322 if (tstate == NULL)
323 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000324 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000325 if (PyThreadState_Swap(tstate) != NULL)
326 Py_FatalError(
327 "PyEval_AcquireThread: non-NULL old thread state");
328}
329
330void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000331PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000332{
333 if (tstate == NULL)
334 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
335 if (PyThreadState_Swap(NULL) != tstate)
336 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000337 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000338}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000339
340/* This function is called from PyOS_AfterFork to ensure that newly
341 created child processes don't hold locks referring to threads which
342 are not running in the child process. (This could also be done using
343 pthread_atfork mechanism, at least for the pthreads implementation.) */
344
345void
346PyEval_ReInitThreads(void)
347{
348 if (!interpreter_lock)
349 return;
350 /*XXX Can't use PyThread_free_lock here because it does too
351 much error-checking. Doing this cleanly would require
352 adding a new function to each thread_*.h. Instead, just
353 create a new lock and waste a little bit of memory */
354 interpreter_lock = PyThread_allocate_lock();
355 PyThread_acquire_lock(interpreter_lock, 1);
356 main_thread = PyThread_get_thread_ident();
357}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000358#endif
359
Guido van Rossumff4949e1992-08-05 19:58:53 +0000360/* Functions save_thread and restore_thread are always defined so
361 dynamically loaded modules needn't be compiled separately for use
362 with and without threads: */
363
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000364PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000365PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000366{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000367 PyThreadState *tstate = PyThreadState_Swap(NULL);
368 if (tstate == NULL)
369 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000370#ifdef WITH_THREAD
Guido van Rossumb74eca91997-09-30 22:03:16 +0000371 if (interpreter_lock)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000372 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000373#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000374 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000375}
376
377void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000378PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000379{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000380 if (tstate == NULL)
381 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000382#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000383 if (interpreter_lock) {
Guido van Rossumb74eca91997-09-30 22:03:16 +0000384 int err = errno;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000385 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000386 errno = err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000387 }
388#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000389 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000390}
391
392
Guido van Rossuma9672091994-09-14 13:31:22 +0000393/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
394 signal handlers or Mac I/O completion routines) can schedule calls
395 to a function to be called synchronously.
396 The synchronous function is called with one void* argument.
397 It should return 0 for success or -1 for failure -- failure should
398 be accompanied by an exception.
399
400 If registry succeeds, the registry function returns 0; if it fails
401 (e.g. due to too many pending calls) it returns -1 (without setting
402 an exception condition).
403
404 Note that because registry may occur from within signal handlers,
405 or other asynchronous events, calling malloc() is unsafe!
406
407#ifdef WITH_THREAD
408 Any thread can schedule pending calls, but only the main thread
409 will execute them.
410#endif
411
412 XXX WARNING! ASYNCHRONOUSLY EXECUTING CODE!
413 There are two possible race conditions:
414 (1) nested asynchronous registry calls;
415 (2) registry calls made while pending calls are being processed.
416 While (1) is very unlikely, (2) is a real possibility.
417 The current code is safe against (2), but not against (1).
418 The safety against (2) is derived from the fact that only one
419 thread (the main thread) ever takes things out of the queue.
Guido van Rossuma9672091994-09-14 13:31:22 +0000420
Guido van Rossuma027efa1997-05-05 20:56:21 +0000421 XXX Darn! With the advent of thread state, we should have an array
422 of pending calls per thread in the thread state! Later...
423*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000424
Guido van Rossuma9672091994-09-14 13:31:22 +0000425#define NPENDINGCALLS 32
426static struct {
Thomas Wouters334fb892000-07-25 12:56:38 +0000427 int (*func)(void *);
428 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000429} pendingcalls[NPENDINGCALLS];
430static volatile int pendingfirst = 0;
431static volatile int pendinglast = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000432static volatile int things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000433
434int
Thomas Wouters334fb892000-07-25 12:56:38 +0000435Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000436{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000437 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000438 int i, j;
439 /* XXX Begin critical section */
440 /* XXX If you want this to be safe against nested
441 XXX asynchronous calls, you'll have to work harder! */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000442 if (busy)
443 return -1;
444 busy = 1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000445 i = pendinglast;
446 j = (i + 1) % NPENDINGCALLS;
Guido van Rossum04e70322002-07-17 16:57:13 +0000447 if (j == pendingfirst) {
448 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000449 return -1; /* Queue full */
Guido van Rossum04e70322002-07-17 16:57:13 +0000450 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000451 pendingcalls[i].func = func;
452 pendingcalls[i].arg = arg;
453 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000454
455 _Py_Ticker = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000456 things_to_do = 1; /* Signal main loop */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000457 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000458 /* XXX End critical section */
459 return 0;
460}
461
Guido van Rossum180d7b41994-09-29 09:45:57 +0000462int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000463Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000464{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000465 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000466#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000467 if (main_thread && PyThread_get_thread_ident() != main_thread)
Guido van Rossuma9672091994-09-14 13:31:22 +0000468 return 0;
469#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000470 if (busy)
Guido van Rossum180d7b41994-09-29 09:45:57 +0000471 return 0;
472 busy = 1;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000473 things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000474 for (;;) {
475 int i;
Thomas Wouters334fb892000-07-25 12:56:38 +0000476 int (*func)(void *);
477 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000478 i = pendingfirst;
479 if (i == pendinglast)
480 break; /* Queue empty */
481 func = pendingcalls[i].func;
482 arg = pendingcalls[i].arg;
483 pendingfirst = (i + 1) % NPENDINGCALLS;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000484 if (func(arg) < 0) {
485 busy = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000486 things_to_do = 1; /* We're not done yet */
Guido van Rossuma9672091994-09-14 13:31:22 +0000487 return -1;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000488 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000489 }
Guido van Rossum180d7b41994-09-29 09:45:57 +0000490 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000491 return 0;
492}
493
494
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000495/* The interpreter's recursion limit */
496
Guido van Rossum349ff6f2000-09-01 01:52:08 +0000497static int recursion_limit = 1000;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000498
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000499int
500Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000501{
502 return recursion_limit;
503}
504
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000505void
506Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000507{
508 recursion_limit = new_limit;
509}
510
Guido van Rossum374a9221991-04-04 10:40:29 +0000511/* Status code for main loop (reason for stack unwind) */
512
513enum why_code {
514 WHY_NOT, /* No error */
515 WHY_EXCEPTION, /* Exception occurred */
516 WHY_RERAISE, /* Exception re-raised by 'finally' */
517 WHY_RETURN, /* 'return' statement */
Jeremy Hylton3faa52e2001-02-01 22:48:12 +0000518 WHY_BREAK, /* 'break' statement */
Tim Peters5ca576e2001-06-18 22:08:13 +0000519 WHY_CONTINUE, /* 'continue' statement */
Tim Peters6e6a63f2001-10-18 20:49:35 +0000520 WHY_YIELD /* 'yield' operator */
Guido van Rossum374a9221991-04-04 10:40:29 +0000521};
522
Tim Petersdbd9ba62000-07-09 03:09:57 +0000523static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
Tim Petersd6d010b2001-06-21 02:49:55 +0000524static int unpack_iterable(PyObject *, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000525
Skip Montanarod581d772002-09-03 20:10:45 +0000526/* for manipulating the thread switch and periodic "stuff" - used to be
527 per thread, now just a pair o' globals */
Skip Montanaro99dba272002-09-03 20:19:06 +0000528int _Py_CheckInterval = 100;
529volatile int _Py_Ticker = 100;
Guido van Rossum374a9221991-04-04 10:40:29 +0000530
Guido van Rossumb209a111997-04-29 18:18:01 +0000531PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000532PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000533{
Jeremy Hylton985eba52003-02-05 23:13:00 +0000534 /* XXX raise SystemError if globals is NULL */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000535 return PyEval_EvalCodeEx(co,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000536 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000537 (PyObject **)NULL, 0,
538 (PyObject **)NULL, 0,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000539 (PyObject **)NULL, 0,
540 NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000541}
542
543
544/* Interpreter main loop */
545
Tim Peters6d6c1a32001-08-02 04:15:00 +0000546static PyObject *
Tim Peters5ca576e2001-06-18 22:08:13 +0000547eval_frame(PyFrameObject *f)
Guido van Rossum374a9221991-04-04 10:40:29 +0000548{
Guido van Rossum950361c1997-01-24 13:49:28 +0000549#ifdef DXPAIRS
550 int lastopcode = 0;
551#endif
Tim Petersb6d14da2001-12-19 04:11:07 +0000552 PyObject **stack_pointer; /* Next free slot in value stack */
Guido van Rossum374a9221991-04-04 10:40:29 +0000553 register unsigned char *next_instr;
Moshe Zadkaaa39a7e2000-08-07 06:34:45 +0000554 register int opcode=0; /* Current opcode */
555 register int oparg=0; /* Current opcode argument, if any */
Guido van Rossum374a9221991-04-04 10:40:29 +0000556 register enum why_code why; /* Reason for block stack unwind */
557 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000558 register PyObject *x; /* Result object -- NULL if error */
559 register PyObject *v; /* Temporary objects popped off stack */
560 register PyObject *w;
561 register PyObject *u;
562 register PyObject *t;
Barry Warsaw23c9ec82000-08-21 15:44:01 +0000563 register PyObject *stream = NULL; /* for PRINT opcodes */
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000564 register PyObject **fastlocals, **freevars;
Guido van Rossum014518f1998-11-23 21:09:51 +0000565 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000566 PyThreadState *tstate = PyThreadState_GET();
Tim Peters5ca576e2001-06-18 22:08:13 +0000567 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000568
569 /* when tracing we set things up so that
570
571 not (instr_lb <= current_bytecode_offset < instr_ub)
572
573 is true when the line being executed has changed. The
574 initial values are such as to make this false the first
575 time it is tested. */
576 int instr_ub = -1, instr_lb = 0;
577
Guido van Rossumd076c731998-10-07 19:42:25 +0000578 unsigned char *first_instr;
Skip Montanaro04d80f82002-08-04 21:03:35 +0000579 PyObject *names;
580 PyObject *consts;
Guido van Rossum96a42c81992-01-12 02:29:51 +0000581#ifdef LLTRACE
Guido van Rossumacbe8da1993-04-15 15:33:52 +0000582 int lltrace;
Guido van Rossum374a9221991-04-04 10:40:29 +0000583#endif
Guido van Rossum408027e1996-12-30 16:17:54 +0000584#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000585 /* Make it easier to find out where we are with a debugger */
Tim Peters5ca576e2001-06-18 22:08:13 +0000586 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000587#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000588
Neal Norwitza81d2202002-07-14 00:27:26 +0000589/* Tuple access macros */
590
591#ifndef Py_DEBUG
592#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
593#else
594#define GETITEM(v, i) PyTuple_GetItem((v), (i))
595#endif
596
Guido van Rossum374a9221991-04-04 10:40:29 +0000597/* Code access macros */
598
Guido van Rossumd076c731998-10-07 19:42:25 +0000599#define INSTR_OFFSET() (next_instr - first_instr)
Guido van Rossum374a9221991-04-04 10:40:29 +0000600#define NEXTOP() (*next_instr++)
601#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Guido van Rossumd076c731998-10-07 19:42:25 +0000602#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000603#define JUMPBY(x) (next_instr += (x))
604
605/* Stack manipulation macros */
606
607#define STACK_LEVEL() (stack_pointer - f->f_valuestack)
608#define EMPTY() (STACK_LEVEL() == 0)
609#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000610#define SECOND() (stack_pointer[-2])
611#define THIRD() (stack_pointer[-3])
612#define FOURTH() (stack_pointer[-4])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000613#define SET_TOP(v) (stack_pointer[-1] = (v))
614#define SET_SECOND(v) (stack_pointer[-2] = (v))
615#define SET_THIRD(v) (stack_pointer[-3] = (v))
616#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000617#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000618#define BASIC_PUSH(v) (*stack_pointer++ = (v))
619#define BASIC_POP() (*--stack_pointer)
620
Guido van Rossum96a42c81992-01-12 02:29:51 +0000621#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000622#define PUSH(v) { (void)(BASIC_PUSH(v), \
623 lltrace && prtrace(TOP(), "push")); \
624 assert(STACK_LEVEL() <= f->f_stacksize); }
Fred Drakede26cfc2001-10-13 06:11:28 +0000625#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000626#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
627 lltrace && prtrace(TOP(), "stackadj")); \
628 assert(STACK_LEVEL() <= f->f_stacksize); }
Guido van Rossum374a9221991-04-04 10:40:29 +0000629#else
630#define PUSH(v) BASIC_PUSH(v)
631#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000632#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000633#endif
634
Guido van Rossum681d79a1995-07-18 14:51:37 +0000635/* Local variable macros */
636
637#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000638
639/* The SETLOCAL() macro must not DECREF the local variable in-place and
640 then store the new value; it must copy the old value to a temporary
641 value, then store the new value, and then DECREF the temporary value.
642 This is because it is possible that during the DECREF the frame is
643 accessed by other code (e.g. a __del__ method or gc.collect()) and the
644 variable would be pointing to already-freed memory. */
645#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
646 GETLOCAL(i) = value; \
647 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000648
Guido van Rossuma027efa1997-05-05 20:56:21 +0000649/* Start of code */
650
Tim Peters5ca576e2001-06-18 22:08:13 +0000651 if (f == NULL)
652 return NULL;
653
Guido van Rossum8861b741996-07-30 16:49:37 +0000654#ifdef USE_STACKCHECK
Guido van Rossuma027efa1997-05-05 20:56:21 +0000655 if (tstate->recursion_depth%10 == 0 && PyOS_CheckStack()) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000656 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
Guido van Rossum8861b741996-07-30 16:49:37 +0000657 return NULL;
658 }
659#endif
660
Tim Peters5ca576e2001-06-18 22:08:13 +0000661 /* push frame */
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000662 if (++tstate->recursion_depth > recursion_limit) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000663 --tstate->recursion_depth;
664 PyErr_SetString(PyExc_RuntimeError,
Fred Drake661ea262000-10-24 19:57:45 +0000665 "maximum recursion depth exceeded");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000666 tstate->frame = f->f_back;
Guido van Rossum8861b741996-07-30 16:49:37 +0000667 return NULL;
668 }
669
Tim Peters5ca576e2001-06-18 22:08:13 +0000670 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000671
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000672 if (tstate->use_tracing) {
673 if (tstate->c_tracefunc != NULL) {
674 /* tstate->c_tracefunc, if defined, is a
675 function that will be called on *every* entry
676 to a code block. Its return value, if not
677 None, is a function that will be called at
678 the start of each executed line of code.
679 (Actually, the function must return itself
680 in order to continue tracing.) The trace
681 functions are called with three arguments:
682 a pointer to the current frame, a string
683 indicating why the function is called, and
684 an argument which depends on the situation.
685 The global trace function is also called
686 whenever an exception is detected. */
687 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
688 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000689 /* Trace function raised an error */
Michael W. Hudsonfb4d6ec2002-10-02 13:13:45 +0000690 --tstate->recursion_depth;
691 tstate->frame = f->f_back;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000692 return NULL;
693 }
694 }
695 if (tstate->c_profilefunc != NULL) {
696 /* Similar for c_profilefunc, except it needn't
697 return itself and isn't called for "line" events */
698 if (call_trace(tstate->c_profilefunc,
699 tstate->c_profileobj,
700 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000701 /* Profile function raised an error */
Michael W. Hudsonfb4d6ec2002-10-02 13:13:45 +0000702 --tstate->recursion_depth;
703 tstate->frame = f->f_back;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000704 return NULL;
705 }
706 }
707 }
708
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000709 co = f->f_code;
710 names = co->co_names;
711 consts = co->co_consts;
712 fastlocals = f->f_localsplus;
713 freevars = f->f_localsplus + f->f_nlocals;
714 _PyCode_GETCODEPTR(co, &first_instr);
715 /* An explanation is in order for the next line.
716
717 f->f_lasti now refers to the index of the last instruction
718 executed. You might think this was obvious from the name, but
719 this wasn't always true before 2.3! PyFrame_New now sets
720 f->f_lasti to -1 (i.e. the index *before* the first instruction)
721 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
722 does work. Promise. */
723 next_instr = first_instr + f->f_lasti + 1;
724 stack_pointer = f->f_stacktop;
725 assert(stack_pointer != NULL);
726 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
727
Tim Peters5ca576e2001-06-18 22:08:13 +0000728#ifdef LLTRACE
729 lltrace = PyDict_GetItemString(f->f_globals,"__lltrace__") != NULL;
730#endif
731#if defined(Py_DEBUG) || defined(LLTRACE)
732 filename = PyString_AsString(co->co_filename);
733#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000734
Guido van Rossum374a9221991-04-04 10:40:29 +0000735 why = WHY_NOT;
736 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +0000737 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +0000738 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +0000739
Guido van Rossum374a9221991-04-04 10:40:29 +0000740 for (;;) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000741 assert(stack_pointer >= f->f_valuestack); /* else underflow */
742 assert(STACK_LEVEL() <= f->f_stacksize); /* else overflow */
743
Guido van Rossuma027efa1997-05-05 20:56:21 +0000744 /* Do periodic things. Doing this every time through
745 the loop would add too much overhead, so we do it
746 only every Nth instruction. We also do it if
747 ``things_to_do'' is set, i.e. when an asynchronous
748 event needs attention (e.g. a signal handler or
749 async I/O handler); see Py_AddPendingCall() and
750 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000751
Skip Montanarod581d772002-09-03 20:10:45 +0000752 if (--_Py_Ticker < 0) {
753 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000754 tstate->tick_counter++;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000755 if (things_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +0000756 if (Py_MakePendingCalls() < 0) {
757 why = WHY_EXCEPTION;
758 goto on_error;
759 }
760 }
Guido van Rossumdf0d00e1997-05-20 15:57:49 +0000761#if !defined(HAVE_SIGNAL_H) || defined(macintosh)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000762 /* If we have true signals, the signal handler
763 will call Py_AddPendingCall() so we don't
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000764 have to call PyErr_CheckSignals(). On the
765 Mac and DOS, alas, we have to call it. */
Guido van Rossumb209a111997-04-29 18:18:01 +0000766 if (PyErr_CheckSignals()) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000767 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +0000768 goto on_error;
769 }
Guido van Rossum70d44781997-01-21 06:15:24 +0000770#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000771
Guido van Rossume59214e1994-08-30 08:01:59 +0000772#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000773 if (interpreter_lock) {
774 /* Give another thread a chance */
775
Guido van Rossum25ce5661997-08-02 03:10:38 +0000776 if (PyThreadState_Swap(NULL) != tstate)
777 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000778 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000779
780 /* Other threads may run now */
781
Guido van Rossum65d5b571998-12-21 19:32:43 +0000782 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000783 if (PyThreadState_Swap(tstate) != NULL)
784 Py_FatalError("ceval: orphan tstate");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000785 }
786#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000787 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000788
Neil Schemenauer63543862002-02-17 19:10:14 +0000789 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +0000790 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +0000791
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000792 /* line-by-line tracing support */
793
794 if (tstate->c_tracefunc != NULL && !tstate->tracing) {
795 /* see maybe_call_line_trace
796 for expository comments */
797 f->f_stacktop = stack_pointer;
Michael W. Hudson006c7522002-11-08 13:08:46 +0000798
799 if (maybe_call_line_trace(tstate->c_tracefunc,
800 tstate->c_traceobj,
801 f, &instr_lb, &instr_ub)) {
802 /* trace function raised an exception */
803 why = WHY_EXCEPTION;
804 goto on_error;
805 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000806 /* Reload possibly changed frame fields */
807 JUMPTO(f->f_lasti);
808 stack_pointer = f->f_stacktop;
809 assert(stack_pointer != NULL);
810 f->f_stacktop = NULL;
811 }
812
813 /* Extract opcode and argument */
814
Guido van Rossum374a9221991-04-04 10:40:29 +0000815 opcode = NEXTOP();
816 if (HAS_ARG(opcode))
817 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +0000818 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +0000819#ifdef DYNAMIC_EXECUTION_PROFILE
820#ifdef DXPAIRS
821 dxpairs[lastopcode][opcode]++;
822 lastopcode = opcode;
823#endif
824 dxp[opcode]++;
825#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000826
Guido van Rossum96a42c81992-01-12 02:29:51 +0000827#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +0000828 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +0000829
Guido van Rossum96a42c81992-01-12 02:29:51 +0000830 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000831 if (HAS_ARG(opcode)) {
832 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000833 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +0000834 }
835 else {
836 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000837 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +0000838 }
839 }
840#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000841
Guido van Rossum374a9221991-04-04 10:40:29 +0000842 /* Main switch on opcode */
Jeremy Hylton52820442001-01-03 23:52:36 +0000843
Guido van Rossum374a9221991-04-04 10:40:29 +0000844 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +0000845
Guido van Rossum374a9221991-04-04 10:40:29 +0000846 /* BEWARE!
847 It is essential that any operation that fails sets either
848 x to NULL, err to nonzero, or why to anything but WHY_NOT,
849 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000850
Guido van Rossum374a9221991-04-04 10:40:29 +0000851 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000852
Neil Schemenauer63543862002-02-17 19:10:14 +0000853 case LOAD_FAST:
854 x = GETLOCAL(oparg);
855 if (x != NULL) {
856 Py_INCREF(x);
857 PUSH(x);
858 goto fast_next_opcode;
859 }
860 format_exc_check_arg(PyExc_UnboundLocalError,
861 UNBOUNDLOCAL_ERROR_MSG,
862 PyTuple_GetItem(co->co_varnames, oparg));
863 break;
864
865 case LOAD_CONST:
Skip Montanaro04d80f82002-08-04 21:03:35 +0000866 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +0000867 Py_INCREF(x);
868 PUSH(x);
869 goto fast_next_opcode;
870
871 case STORE_FAST:
872 v = POP();
873 SETLOCAL(oparg, v);
874 goto fast_next_opcode;
875
Guido van Rossum374a9221991-04-04 10:40:29 +0000876 case POP_TOP:
877 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000878 Py_DECREF(v);
Neil Schemenauer63543862002-02-17 19:10:14 +0000879 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000880
Guido van Rossum374a9221991-04-04 10:40:29 +0000881 case ROT_TWO:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000882 v = TOP();
883 w = SECOND();
884 SET_TOP(w);
885 SET_SECOND(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000886 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +0000887
Guido van Rossum374a9221991-04-04 10:40:29 +0000888 case ROT_THREE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000889 v = TOP();
890 w = SECOND();
891 x = THIRD();
892 SET_TOP(w);
893 SET_SECOND(x);
894 SET_THIRD(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000895 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +0000896
Thomas Wouters434d0822000-08-24 20:11:32 +0000897 case ROT_FOUR:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000898 u = TOP();
899 v = SECOND();
900 w = THIRD();
901 x = FOURTH();
902 SET_TOP(v);
903 SET_SECOND(w);
904 SET_THIRD(x);
905 SET_FOURTH(u);
Thomas Wouters434d0822000-08-24 20:11:32 +0000906 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +0000907
Guido van Rossum374a9221991-04-04 10:40:29 +0000908 case DUP_TOP:
909 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000910 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000911 PUSH(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000912 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +0000913
Thomas Wouters434d0822000-08-24 20:11:32 +0000914 case DUP_TOPX:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000915 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000916 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000917 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000918 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000919 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000920 STACKADJ(2);
921 SET_TOP(x);
922 SET_SECOND(w);
Tim Peters35ba6892000-10-11 07:04:49 +0000923 continue;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000924 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000925 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000926 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000927 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000928 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000929 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +0000930 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000931 STACKADJ(3);
932 SET_TOP(x);
933 SET_SECOND(w);
934 SET_THIRD(v);
Tim Peters35ba6892000-10-11 07:04:49 +0000935 continue;
Thomas Wouters434d0822000-08-24 20:11:32 +0000936 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000937 Py_FatalError("invalid argument to DUP_TOPX"
938 " (bytecode corruption?)");
Tim Peters35ba6892000-10-11 07:04:49 +0000939 break;
Thomas Wouters434d0822000-08-24 20:11:32 +0000940
Guido van Rossum374a9221991-04-04 10:40:29 +0000941 case UNARY_POSITIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000942 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000943 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000944 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000945 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000946 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000947 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000948
Guido van Rossum374a9221991-04-04 10:40:29 +0000949 case UNARY_NEGATIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000950 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000951 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000952 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000953 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000954 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000955 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000956
Guido van Rossum374a9221991-04-04 10:40:29 +0000957 case UNARY_NOT:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000958 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000959 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000960 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +0000961 if (err == 0) {
962 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000963 SET_TOP(Py_True);
Guido van Rossumfc490731997-05-06 15:06:49 +0000964 continue;
965 }
966 else if (err > 0) {
967 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000968 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +0000969 err = 0;
970 continue;
971 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +0000972 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +0000973 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000974
Guido van Rossum374a9221991-04-04 10:40:29 +0000975 case UNARY_CONVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000976 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000977 x = PyObject_Repr(v);
978 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000979 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000980 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000981 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000982
Guido van Rossum7928cd71991-10-24 14:59:31 +0000983 case UNARY_INVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000984 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000985 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000986 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000987 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000988 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000989 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000990
Guido van Rossum50564e81996-01-12 01:13:16 +0000991 case BINARY_POWER:
992 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +0000993 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000994 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +0000995 Py_DECREF(v);
996 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000997 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000998 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +0000999 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001000
Guido van Rossum374a9221991-04-04 10:40:29 +00001001 case BINARY_MULTIPLY:
1002 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001003 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001004 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001005 Py_DECREF(v);
1006 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001007 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001008 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001009 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001010
Guido van Rossum374a9221991-04-04 10:40:29 +00001011 case BINARY_DIVIDE:
Tim Peters3caca232001-12-06 06:23:26 +00001012 if (!_Py_QnewFlag) {
1013 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001014 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001015 x = PyNumber_Divide(v, w);
1016 Py_DECREF(v);
1017 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001018 SET_TOP(x);
Tim Peters3caca232001-12-06 06:23:26 +00001019 if (x != NULL) continue;
1020 break;
1021 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001022 /* -Qnew is in effect: fall through to
Tim Peters3caca232001-12-06 06:23:26 +00001023 BINARY_TRUE_DIVIDE */
1024 case BINARY_TRUE_DIVIDE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001025 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001026 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001027 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001028 Py_DECREF(v);
1029 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001030 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001031 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001032 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001033
Guido van Rossum4668b002001-08-08 05:00:18 +00001034 case BINARY_FLOOR_DIVIDE:
1035 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001036 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001037 x = PyNumber_FloorDivide(v, w);
1038 Py_DECREF(v);
1039 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001040 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001041 if (x != NULL) continue;
1042 break;
1043
Guido van Rossum374a9221991-04-04 10:40:29 +00001044 case BINARY_MODULO:
1045 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001046 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001047 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001048 Py_DECREF(v);
1049 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001050 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001051 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001052 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001053
Guido van Rossum374a9221991-04-04 10:40:29 +00001054 case BINARY_ADD:
1055 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001056 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001057 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001058 /* INLINE: int + int */
1059 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001060 a = PyInt_AS_LONG(v);
1061 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001062 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001063 if ((i^a) < 0 && (i^b) < 0)
1064 goto slow_add;
1065 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001066 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001067 else {
1068 slow_add:
Guido van Rossumc12da691997-07-17 23:12:42 +00001069 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001070 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001071 Py_DECREF(v);
1072 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001073 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001074 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001075 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001076
Guido van Rossum374a9221991-04-04 10:40:29 +00001077 case BINARY_SUBTRACT:
1078 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001079 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001080 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001081 /* INLINE: int - int */
1082 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001083 a = PyInt_AS_LONG(v);
1084 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001085 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001086 if ((i^a) < 0 && (i^~b) < 0)
1087 goto slow_sub;
1088 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001089 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001090 else {
1091 slow_sub:
Guido van Rossumc12da691997-07-17 23:12:42 +00001092 x = PyNumber_Subtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001093 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001094 Py_DECREF(v);
1095 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001096 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001097 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001098 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001099
Guido van Rossum374a9221991-04-04 10:40:29 +00001100 case BINARY_SUBSCR:
1101 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001102 v = TOP();
Tim Petersb1c46982001-10-05 20:41:38 +00001103 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001104 /* INLINE: list[int] */
1105 long i = PyInt_AsLong(w);
1106 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +00001107 i += PyList_GET_SIZE(v);
Guido van Rossumc12da691997-07-17 23:12:42 +00001108 if (i < 0 ||
Guido van Rossumfa00e951998-07-08 15:02:37 +00001109 i >= PyList_GET_SIZE(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001110 PyErr_SetString(PyExc_IndexError,
1111 "list index out of range");
1112 x = NULL;
1113 }
1114 else {
Guido van Rossumfa00e951998-07-08 15:02:37 +00001115 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001116 Py_INCREF(x);
1117 }
1118 }
1119 else
1120 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001121 Py_DECREF(v);
1122 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001123 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001124 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001125 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001126
Guido van Rossum7928cd71991-10-24 14:59:31 +00001127 case BINARY_LSHIFT:
1128 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001129 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001130 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001131 Py_DECREF(v);
1132 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001133 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001134 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001135 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001136
Guido van Rossum7928cd71991-10-24 14:59:31 +00001137 case BINARY_RSHIFT:
1138 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001139 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001140 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001141 Py_DECREF(v);
1142 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001143 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001144 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001145 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001146
Guido van Rossum7928cd71991-10-24 14:59:31 +00001147 case BINARY_AND:
1148 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001149 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001150 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001151 Py_DECREF(v);
1152 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001153 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001154 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001155 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001156
Guido van Rossum7928cd71991-10-24 14:59:31 +00001157 case BINARY_XOR:
1158 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001159 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001160 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001161 Py_DECREF(v);
1162 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001163 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001164 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001165 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001166
Guido van Rossum7928cd71991-10-24 14:59:31 +00001167 case BINARY_OR:
1168 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001169 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001170 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001171 Py_DECREF(v);
1172 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001173 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001174 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001175 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001176
1177 case INPLACE_POWER:
1178 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001179 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001180 x = PyNumber_InPlacePower(v, w, Py_None);
1181 Py_DECREF(v);
1182 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001183 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001184 if (x != NULL) continue;
1185 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001186
Thomas Wouters434d0822000-08-24 20:11:32 +00001187 case INPLACE_MULTIPLY:
1188 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001189 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001190 x = PyNumber_InPlaceMultiply(v, w);
1191 Py_DECREF(v);
1192 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001193 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001194 if (x != NULL) continue;
1195 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001196
Thomas Wouters434d0822000-08-24 20:11:32 +00001197 case INPLACE_DIVIDE:
Tim Peters54b11912001-12-25 18:49:11 +00001198 if (!_Py_QnewFlag) {
1199 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001200 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001201 x = PyNumber_InPlaceDivide(v, w);
1202 Py_DECREF(v);
1203 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001204 SET_TOP(x);
Tim Peters54b11912001-12-25 18:49:11 +00001205 if (x != NULL) continue;
1206 break;
1207 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001208 /* -Qnew is in effect: fall through to
Tim Peters54b11912001-12-25 18:49:11 +00001209 INPLACE_TRUE_DIVIDE */
1210 case INPLACE_TRUE_DIVIDE:
Thomas Wouters434d0822000-08-24 20:11:32 +00001211 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001212 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001213 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001214 Py_DECREF(v);
1215 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001216 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001217 if (x != NULL) continue;
1218 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001219
Guido van Rossum4668b002001-08-08 05:00:18 +00001220 case INPLACE_FLOOR_DIVIDE:
1221 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001222 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001223 x = PyNumber_InPlaceFloorDivide(v, w);
1224 Py_DECREF(v);
1225 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001226 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001227 if (x != NULL) continue;
1228 break;
1229
Thomas Wouters434d0822000-08-24 20:11:32 +00001230 case INPLACE_MODULO:
1231 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001232 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001233 x = PyNumber_InPlaceRemainder(v, w);
1234 Py_DECREF(v);
1235 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001236 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001237 if (x != NULL) continue;
1238 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001239
Thomas Wouters434d0822000-08-24 20:11:32 +00001240 case INPLACE_ADD:
1241 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001242 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001243 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001244 /* INLINE: int + int */
1245 register long a, b, i;
1246 a = PyInt_AS_LONG(v);
1247 b = PyInt_AS_LONG(w);
1248 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001249 if ((i^a) < 0 && (i^b) < 0)
1250 goto slow_iadd;
1251 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001252 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001253 else {
1254 slow_iadd:
Thomas Wouters434d0822000-08-24 20:11:32 +00001255 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001256 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001257 Py_DECREF(v);
1258 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001259 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001260 if (x != NULL) continue;
1261 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001262
Thomas Wouters434d0822000-08-24 20:11:32 +00001263 case INPLACE_SUBTRACT:
1264 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001265 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001266 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001267 /* INLINE: int - int */
1268 register long a, b, i;
1269 a = PyInt_AS_LONG(v);
1270 b = PyInt_AS_LONG(w);
1271 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001272 if ((i^a) < 0 && (i^~b) < 0)
1273 goto slow_isub;
1274 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001275 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001276 else {
1277 slow_isub:
Thomas Wouters434d0822000-08-24 20:11:32 +00001278 x = PyNumber_InPlaceSubtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001279 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001280 Py_DECREF(v);
1281 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001282 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001283 if (x != NULL) continue;
1284 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001285
Thomas Wouters434d0822000-08-24 20:11:32 +00001286 case INPLACE_LSHIFT:
1287 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001288 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001289 x = PyNumber_InPlaceLshift(v, w);
1290 Py_DECREF(v);
1291 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001292 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001293 if (x != NULL) continue;
1294 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001295
Thomas Wouters434d0822000-08-24 20:11:32 +00001296 case INPLACE_RSHIFT:
1297 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001298 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001299 x = PyNumber_InPlaceRshift(v, w);
1300 Py_DECREF(v);
1301 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001302 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001303 if (x != NULL) continue;
1304 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001305
Thomas Wouters434d0822000-08-24 20:11:32 +00001306 case INPLACE_AND:
1307 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001308 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001309 x = PyNumber_InPlaceAnd(v, w);
1310 Py_DECREF(v);
1311 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001312 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001313 if (x != NULL) continue;
1314 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001315
Thomas Wouters434d0822000-08-24 20:11:32 +00001316 case INPLACE_XOR:
1317 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001318 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001319 x = PyNumber_InPlaceXor(v, w);
1320 Py_DECREF(v);
1321 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001322 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001323 if (x != NULL) continue;
1324 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001325
Thomas Wouters434d0822000-08-24 20:11:32 +00001326 case INPLACE_OR:
1327 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001328 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001329 x = PyNumber_InPlaceOr(v, w);
1330 Py_DECREF(v);
1331 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001332 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001333 if (x != NULL) continue;
1334 break;
1335
Guido van Rossum374a9221991-04-04 10:40:29 +00001336 case SLICE+0:
1337 case SLICE+1:
1338 case SLICE+2:
1339 case SLICE+3:
1340 if ((opcode-SLICE) & 2)
1341 w = POP();
1342 else
1343 w = NULL;
1344 if ((opcode-SLICE) & 1)
1345 v = POP();
1346 else
1347 v = NULL;
Raymond Hettinger663004b2003-01-09 15:24:30 +00001348 u = TOP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001349 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001350 Py_DECREF(u);
1351 Py_XDECREF(v);
1352 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001353 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001354 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001355 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001356
Guido van Rossum374a9221991-04-04 10:40:29 +00001357 case STORE_SLICE+0:
1358 case STORE_SLICE+1:
1359 case STORE_SLICE+2:
1360 case STORE_SLICE+3:
1361 if ((opcode-STORE_SLICE) & 2)
1362 w = POP();
1363 else
1364 w = NULL;
1365 if ((opcode-STORE_SLICE) & 1)
1366 v = POP();
1367 else
1368 v = NULL;
1369 u = POP();
1370 t = POP();
1371 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +00001372 Py_DECREF(t);
1373 Py_DECREF(u);
1374 Py_XDECREF(v);
1375 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001376 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001377 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001378
Guido van Rossum374a9221991-04-04 10:40:29 +00001379 case DELETE_SLICE+0:
1380 case DELETE_SLICE+1:
1381 case DELETE_SLICE+2:
1382 case DELETE_SLICE+3:
1383 if ((opcode-DELETE_SLICE) & 2)
1384 w = POP();
1385 else
1386 w = NULL;
1387 if ((opcode-DELETE_SLICE) & 1)
1388 v = POP();
1389 else
1390 v = NULL;
1391 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001392 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +00001393 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00001394 Py_DECREF(u);
1395 Py_XDECREF(v);
1396 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001397 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001398 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001399
Guido van Rossum374a9221991-04-04 10:40:29 +00001400 case STORE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001401 w = TOP();
1402 v = SECOND();
1403 u = THIRD();
1404 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001405 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001406 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001407 Py_DECREF(u);
1408 Py_DECREF(v);
1409 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001410 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001411 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001412
Guido van Rossum374a9221991-04-04 10:40:29 +00001413 case DELETE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001414 w = TOP();
1415 v = SECOND();
1416 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001417 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001418 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001419 Py_DECREF(v);
1420 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001421 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001422 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001423
Guido van Rossum374a9221991-04-04 10:40:29 +00001424 case PRINT_EXPR:
1425 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001426 w = PySys_GetObject("displayhook");
1427 if (w == NULL) {
1428 PyErr_SetString(PyExc_RuntimeError,
1429 "lost sys.displayhook");
1430 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001431 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001432 }
1433 if (err == 0) {
1434 x = Py_BuildValue("(O)", v);
1435 if (x == NULL)
1436 err = -1;
1437 }
1438 if (err == 0) {
1439 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001440 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001441 if (w == NULL)
1442 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001443 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001444 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001445 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001446 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001447
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001448 case PRINT_ITEM_TO:
1449 w = stream = POP();
1450 /* fall through to PRINT_ITEM */
1451
Guido van Rossum374a9221991-04-04 10:40:29 +00001452 case PRINT_ITEM:
1453 v = POP();
Barry Warsaw093abe02000-08-29 04:56:13 +00001454 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001455 w = PySys_GetObject("stdout");
1456 if (w == NULL) {
1457 PyErr_SetString(PyExc_RuntimeError,
1458 "lost sys.stdout");
1459 err = -1;
1460 }
Guido van Rossum8f183201997-12-31 05:53:15 +00001461 }
Tim Peters8e5fd532002-03-24 19:25:00 +00001462 if (w != NULL && PyFile_SoftSpace(w, 0))
Guido van Rossumbe270261997-05-22 22:26:18 +00001463 err = PyFile_WriteString(" ", w);
1464 if (err == 0)
1465 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001466 if (err == 0) {
Tim Peters8e5fd532002-03-24 19:25:00 +00001467 /* XXX move into writeobject() ? */
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001468 if (PyString_Check(v)) {
1469 char *s = PyString_AS_STRING(v);
1470 int len = PyString_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001471 if (len == 0 ||
1472 !isspace(Py_CHARMASK(s[len-1])) ||
1473 s[len-1] == ' ')
1474 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001475 }
Martin v. Löwis8d3ce5a2001-12-18 22:36:40 +00001476#ifdef Py_USING_UNICODE
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001477 else if (PyUnicode_Check(v)) {
1478 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
1479 int len = PyUnicode_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001480 if (len == 0 ||
1481 !Py_UNICODE_ISSPACE(s[len-1]) ||
1482 s[len-1] == ' ')
1483 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001484 }
Michael W. Hudsond95c8282002-05-20 13:56:11 +00001485#endif
Tim Peters8e5fd532002-03-24 19:25:00 +00001486 else
1487 PyFile_SoftSpace(w, 1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001488 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001489 Py_DECREF(v);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001490 Py_XDECREF(stream);
1491 stream = NULL;
1492 if (err == 0)
1493 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001494 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001495
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001496 case PRINT_NEWLINE_TO:
1497 w = stream = POP();
1498 /* fall through to PRINT_NEWLINE */
1499
Guido van Rossum374a9221991-04-04 10:40:29 +00001500 case PRINT_NEWLINE:
Barry Warsaw093abe02000-08-29 04:56:13 +00001501 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001502 w = PySys_GetObject("stdout");
1503 if (w == NULL)
1504 PyErr_SetString(PyExc_RuntimeError,
1505 "lost sys.stdout");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001506 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001507 if (w != NULL) {
1508 err = PyFile_WriteString("\n", w);
1509 if (err == 0)
1510 PyFile_SoftSpace(w, 0);
1511 }
1512 Py_XDECREF(stream);
1513 stream = NULL;
Guido van Rossum374a9221991-04-04 10:40:29 +00001514 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001515
Thomas Wouters434d0822000-08-24 20:11:32 +00001516
1517#ifdef CASE_TOO_BIG
1518 default: switch (opcode) {
1519#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001520 case BREAK_LOOP:
1521 why = WHY_BREAK;
1522 break;
Guido van Rossum66b0e9c2001-03-21 19:17:22 +00001523
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00001524 case CONTINUE_LOOP:
1525 retval = PyInt_FromLong(oparg);
1526 why = WHY_CONTINUE;
1527 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001528
Guido van Rossumf10570b1995-07-07 22:53:21 +00001529 case RAISE_VARARGS:
1530 u = v = w = NULL;
1531 switch (oparg) {
1532 case 3:
1533 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001534 /* Fallthrough */
1535 case 2:
1536 v = POP(); /* value */
1537 /* Fallthrough */
1538 case 1:
1539 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001540 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001541 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001542 break;
1543 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001544 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001545 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001546 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001547 break;
1548 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001549 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001550
Guido van Rossum374a9221991-04-04 10:40:29 +00001551 case LOAD_LOCALS:
Guido van Rossum681d79a1995-07-18 14:51:37 +00001552 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001553 PyErr_SetString(PyExc_SystemError,
1554 "no locals");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001555 break;
1556 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001557 Py_INCREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001558 PUSH(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001559 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001560
Guido van Rossum374a9221991-04-04 10:40:29 +00001561 case RETURN_VALUE:
1562 retval = POP();
1563 why = WHY_RETURN;
1564 break;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001565
Tim Peters5ca576e2001-06-18 22:08:13 +00001566 case YIELD_VALUE:
1567 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001568 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001569 why = WHY_YIELD;
1570 break;
1571
1572
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001573 case EXEC_STMT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001574 w = TOP();
1575 v = SECOND();
1576 u = THIRD();
1577 STACKADJ(-3);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001578 err = exec_statement(f, u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001579 Py_DECREF(u);
1580 Py_DECREF(v);
1581 Py_DECREF(w);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001582 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001583
Guido van Rossum374a9221991-04-04 10:40:29 +00001584 case POP_BLOCK:
1585 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001586 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001587 while (STACK_LEVEL() > b->b_level) {
1588 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001589 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001590 }
1591 }
1592 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001593
Guido van Rossum374a9221991-04-04 10:40:29 +00001594 case END_FINALLY:
1595 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001596 if (PyInt_Check(v)) {
1597 why = (enum why_code) PyInt_AsLong(v);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00001598 if (why == WHY_RETURN ||
Tim Peters5ca576e2001-06-18 22:08:13 +00001599 why == WHY_YIELD ||
Guido van Rossumc5fe5eb2002-06-12 03:45:21 +00001600 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001601 retval = POP();
1602 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001603 else if (PyString_Check(v) || PyClass_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001604 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001605 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001606 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001607 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001608 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001609 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001610 else if (v != Py_None) {
1611 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001612 "'finally' pops bad exception");
1613 why = WHY_EXCEPTION;
1614 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001615 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001616 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001617
Guido van Rossum374a9221991-04-04 10:40:29 +00001618 case BUILD_CLASS:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001619 u = TOP();
1620 v = SECOND();
1621 w = THIRD();
1622 STACKADJ(-2);
Guido van Rossum25831651993-05-19 14:50:45 +00001623 x = build_class(u, v, w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001624 SET_TOP(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001625 Py_DECREF(u);
1626 Py_DECREF(v);
1627 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001628 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001629
Guido van Rossum374a9221991-04-04 10:40:29 +00001630 case STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001631 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001632 v = POP();
Guido van Rossum681d79a1995-07-18 14:51:37 +00001633 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001634 PyErr_Format(PyExc_SystemError,
1635 "no locals found when storing %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001636 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001637 break;
1638 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001639 err = PyDict_SetItem(x, w, v);
1640 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001641 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001642
Guido van Rossum374a9221991-04-04 10:40:29 +00001643 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001644 w = GETITEM(names, oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001645 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001646 PyErr_Format(PyExc_SystemError,
1647 "no locals when deleting %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001648 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001649 break;
1650 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001651 if ((err = PyDict_DelItem(x, w)) != 0)
Guido van Rossumac7be682001-01-17 15:42:30 +00001652 format_exc_check_arg(PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001653 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001654 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001655
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001656 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001657 v = POP();
Raymond Hettinger21012b82003-02-26 18:11:50 +00001658 if (PyTuple_CheckExact(v)) {
Barry Warsawe42b18f1997-08-25 22:13:04 +00001659 if (PyTuple_Size(v) != oparg) {
1660 PyErr_SetString(PyExc_ValueError,
1661 "unpack tuple of wrong size");
1662 why = WHY_EXCEPTION;
1663 }
1664 else {
1665 for (; --oparg >= 0; ) {
1666 w = PyTuple_GET_ITEM(v, oparg);
1667 Py_INCREF(w);
1668 PUSH(w);
1669 }
1670 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001671 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00001672 else if (PyList_CheckExact(v)) {
Barry Warsawe42b18f1997-08-25 22:13:04 +00001673 if (PyList_Size(v) != oparg) {
1674 PyErr_SetString(PyExc_ValueError,
1675 "unpack list of wrong size");
1676 why = WHY_EXCEPTION;
1677 }
1678 else {
1679 for (; --oparg >= 0; ) {
1680 w = PyList_GET_ITEM(v, oparg);
1681 Py_INCREF(w);
1682 PUSH(w);
1683 }
1684 }
1685 }
Tim Petersd6d010b2001-06-21 02:49:55 +00001686 else if (unpack_iterable(v, oparg,
1687 stack_pointer + oparg))
1688 stack_pointer += oparg;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001689 else {
1690 if (PyErr_ExceptionMatches(PyExc_TypeError))
1691 PyErr_SetString(PyExc_TypeError,
1692 "unpack non-sequence");
Barry Warsawe42b18f1997-08-25 22:13:04 +00001693 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001694 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001695 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001696 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001697
Guido van Rossum374a9221991-04-04 10:40:29 +00001698 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001699 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001700 v = TOP();
1701 u = SECOND();
1702 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001703 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1704 Py_DECREF(v);
1705 Py_DECREF(u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001706 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001707
Guido van Rossum374a9221991-04-04 10:40:29 +00001708 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001709 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001710 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001711 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1712 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001713 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001714 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001715
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001716 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001717 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001718 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001719 err = PyDict_SetItem(f->f_globals, w, v);
1720 Py_DECREF(v);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001721 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001722
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001723 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001724 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001725 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001726 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001727 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001728 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001729
Guido van Rossum374a9221991-04-04 10:40:29 +00001730 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001731 w = GETITEM(names, oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001732 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001733 PyErr_Format(PyExc_SystemError,
1734 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001735 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001736 break;
1737 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001738 x = PyDict_GetItem(x, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001739 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001740 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001741 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001742 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001743 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001744 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001745 PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001746 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001747 break;
1748 }
1749 }
1750 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001751 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001752 PUSH(x);
1753 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001754
Guido van Rossum374a9221991-04-04 10:40:29 +00001755 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001756 w = GETITEM(names, oparg);
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001757 if (PyString_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00001758 /* Inline the PyDict_GetItem() calls.
1759 WARNING: this is an extreme speed hack.
1760 Do not try this at home. */
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001761 long hash = ((PyStringObject *)w)->ob_shash;
1762 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001763 PyDictObject *d;
1764 d = (PyDictObject *)(f->f_globals);
1765 x = d->ma_lookup(d, w, hash)->me_value;
1766 if (x != NULL) {
1767 Py_INCREF(x);
1768 PUSH(x);
1769 continue;
1770 }
1771 d = (PyDictObject *)(f->f_builtins);
1772 x = d->ma_lookup(d, w, hash)->me_value;
1773 if (x != NULL) {
1774 Py_INCREF(x);
1775 PUSH(x);
1776 continue;
1777 }
1778 goto load_global_error;
1779 }
1780 }
1781 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00001782 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001783 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001784 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001785 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001786 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00001787 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001788 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001789 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001790 break;
1791 }
1792 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001793 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001794 PUSH(x);
1795 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001796
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001797 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001798 x = GETLOCAL(oparg);
1799 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001800 format_exc_check_arg(
1801 PyExc_UnboundLocalError,
1802 UNBOUNDLOCAL_ERROR_MSG,
1803 PyTuple_GetItem(co->co_varnames, oparg)
1804 );
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001805 break;
1806 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00001807 SETLOCAL(oparg, NULL);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001808 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001809
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001810 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001811 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001812 Py_INCREF(x);
1813 PUSH(x);
1814 break;
1815
1816 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001817 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001818 w = PyCell_Get(x);
Jeremy Hylton2524d692001-02-05 17:23:16 +00001819 if (w == NULL) {
Jeremy Hylton76c81ee2002-07-11 16:56:38 +00001820 err = -1;
1821 /* Don't stomp existing exception */
1822 if (PyErr_Occurred())
1823 break;
Jeremy Hyltonc76770c2001-04-13 16:51:46 +00001824 if (oparg < f->f_ncells) {
Jeremy Hylton2524d692001-02-05 17:23:16 +00001825 v = PyTuple_GetItem(co->co_cellvars,
1826 oparg);
Jeremy Hyltonc76770c2001-04-13 16:51:46 +00001827 format_exc_check_arg(
1828 PyExc_UnboundLocalError,
1829 UNBOUNDLOCAL_ERROR_MSG,
1830 v);
1831 } else {
Jeremy Hylton2524d692001-02-05 17:23:16 +00001832 v = PyTuple_GetItem(
1833 co->co_freevars,
1834 oparg - f->f_ncells);
Jeremy Hyltonc76770c2001-04-13 16:51:46 +00001835 format_exc_check_arg(
1836 PyExc_NameError,
1837 UNBOUNDFREE_ERROR_MSG,
1838 v);
1839 }
Jeremy Hylton2524d692001-02-05 17:23:16 +00001840 break;
1841 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001842 PUSH(w);
1843 break;
1844
1845 case STORE_DEREF:
1846 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001847 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001848 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00001849 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001850 continue;
1851
Guido van Rossum374a9221991-04-04 10:40:29 +00001852 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001853 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001854 if (x != NULL) {
1855 for (; --oparg >= 0;) {
1856 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001857 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001858 }
1859 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001860 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001861 }
1862 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001863
Guido van Rossum374a9221991-04-04 10:40:29 +00001864 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00001865 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001866 if (x != NULL) {
1867 for (; --oparg >= 0;) {
1868 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00001869 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001870 }
1871 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001872 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001873 }
1874 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001875
Guido van Rossum374a9221991-04-04 10:40:29 +00001876 case BUILD_MAP:
Guido van Rossumb209a111997-04-29 18:18:01 +00001877 x = PyDict_New();
Guido van Rossum374a9221991-04-04 10:40:29 +00001878 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001879 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001880 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001881
Guido van Rossum374a9221991-04-04 10:40:29 +00001882 case LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001883 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001884 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001885 x = PyObject_GetAttr(v, w);
1886 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001887 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001888 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001889 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001890
Guido van Rossum374a9221991-04-04 10:40:29 +00001891 case COMPARE_OP:
1892 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001893 v = TOP();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001894 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001895 /* INLINE: cmp(int, int) */
1896 register long a, b;
1897 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001898 a = PyInt_AS_LONG(v);
1899 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001900 switch (oparg) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00001901 case PyCmp_LT: res = a < b; break;
1902 case PyCmp_LE: res = a <= b; break;
1903 case PyCmp_EQ: res = a == b; break;
1904 case PyCmp_NE: res = a != b; break;
1905 case PyCmp_GT: res = a > b; break;
1906 case PyCmp_GE: res = a >= b; break;
1907 case PyCmp_IS: res = v == w; break;
1908 case PyCmp_IS_NOT: res = v != w; break;
Guido van Rossumc12da691997-07-17 23:12:42 +00001909 default: goto slow_compare;
1910 }
1911 x = res ? Py_True : Py_False;
1912 Py_INCREF(x);
1913 }
1914 else {
1915 slow_compare:
1916 x = cmp_outcome(oparg, v, w);
1917 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001918 Py_DECREF(v);
1919 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001920 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001921 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001922 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001923
Guido van Rossum374a9221991-04-04 10:40:29 +00001924 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001925 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001926 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001927 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001928 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00001929 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001930 break;
1931 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001932 u = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001933 w = Py_BuildValue("(OOOO)",
Guido van Rossum681d79a1995-07-18 14:51:37 +00001934 w,
1935 f->f_globals,
Guido van Rossuma027efa1997-05-05 20:56:21 +00001936 f->f_locals == NULL ?
1937 Py_None : f->f_locals,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001938 u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001939 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001940 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001941 u = POP();
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001942 x = NULL;
1943 break;
1944 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001945 x = PyEval_CallObject(x, w);
1946 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001947 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001948 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001949 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001950
Thomas Wouters52152252000-08-17 22:55:00 +00001951 case IMPORT_STAR:
1952 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001953 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001954 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001955 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001956 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001957 break;
1958 }
Thomas Wouters52152252000-08-17 22:55:00 +00001959 err = import_all_from(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001960 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00001961 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001962 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001963 break;
Guido van Rossum25831651993-05-19 14:50:45 +00001964
Thomas Wouters52152252000-08-17 22:55:00 +00001965 case IMPORT_FROM:
Skip Montanaro496e6582002-08-06 17:47:40 +00001966 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00001967 v = TOP();
1968 x = import_from(v, w);
1969 PUSH(x);
1970 if (x != NULL) continue;
1971 break;
1972
Guido van Rossum374a9221991-04-04 10:40:29 +00001973 case JUMP_FORWARD:
1974 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001975 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001976
Guido van Rossum374a9221991-04-04 10:40:29 +00001977 case JUMP_IF_FALSE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00001978 w = TOP();
1979 if (w == Py_True)
1980 continue;
1981 if (w == Py_False) {
1982 JUMPBY(oparg);
1983 continue;
1984 }
1985 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001986 if (err > 0)
1987 err = 0;
1988 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00001989 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001990 else
1991 break;
1992 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001993
Guido van Rossum374a9221991-04-04 10:40:29 +00001994 case JUMP_IF_TRUE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00001995 w = TOP();
1996 if (w == Py_False)
1997 continue;
1998 if (w == Py_True) {
1999 JUMPBY(oparg);
2000 continue;
2001 }
2002 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002003 if (err > 0) {
2004 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00002005 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002006 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002007 else if (err == 0)
2008 ;
2009 else
2010 break;
2011 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002012
Guido van Rossum374a9221991-04-04 10:40:29 +00002013 case JUMP_ABSOLUTE:
2014 JUMPTO(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002015 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002016
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002017 case GET_ITER:
2018 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002019 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002020 x = PyObject_GetIter(v);
2021 Py_DECREF(v);
2022 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002023 SET_TOP(x);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002024 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002025 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002026 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002027 break;
2028
2029 case FOR_ITER:
2030 /* before: [iter]; after: [iter, iter()] *or* [] */
2031 v = TOP();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002032 x = PyIter_Next(v);
2033 if (x != NULL) {
2034 PUSH(x);
2035 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002036 }
Tim Petersf4848da2001-05-05 00:14:56 +00002037 if (!PyErr_Occurred()) {
2038 /* iterator ended normally */
2039 x = v = POP();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002040 Py_DECREF(v);
2041 JUMPBY(oparg);
2042 continue;
2043 }
2044 break;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002045
Guido van Rossum374a9221991-04-04 10:40:29 +00002046 case SETUP_LOOP:
2047 case SETUP_EXCEPT:
2048 case SETUP_FINALLY:
Guido van Rossumb209a111997-04-29 18:18:01 +00002049 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002050 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002051 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002052
Guido van Rossumf10570b1995-07-07 22:53:21 +00002053 case CALL_FUNCTION:
Jeremy Hylton985eba52003-02-05 23:13:00 +00002054 PCALL(PCALL_ALL);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002055 x = call_function(&stack_pointer, oparg);
2056 PUSH(x);
2057 if (x != NULL)
2058 continue;
2059 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002060
Jeremy Hylton76901512000-03-28 23:49:17 +00002061 case CALL_FUNCTION_VAR:
2062 case CALL_FUNCTION_KW:
2063 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002064 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002065 int na = oparg & 0xff;
2066 int nk = (oparg>>8) & 0xff;
2067 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002068 int n = na + 2 * nk;
2069 PyObject **pfunc, *func;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002070 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002071 if (flags & CALL_FLAG_VAR)
2072 n++;
2073 if (flags & CALL_FLAG_KW)
2074 n++;
2075 pfunc = stack_pointer - n - 1;
2076 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002077
Guido van Rossumac7be682001-01-17 15:42:30 +00002078 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002079 && PyMethod_GET_SELF(func) != NULL) {
2080 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002081 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002082 func = PyMethod_GET_FUNCTION(func);
2083 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002084 Py_DECREF(*pfunc);
2085 *pfunc = self;
2086 na++;
2087 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002088 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002089 Py_INCREF(func);
2090 x = ext_do_call(func, &stack_pointer, flags, na, nk);
Jeremy Hylton76901512000-03-28 23:49:17 +00002091 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002092
Jeremy Hylton76901512000-03-28 23:49:17 +00002093 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002094 w = POP();
2095 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002096 }
2097 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002098 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002099 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002100 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002101 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002102
Guido van Rossum681d79a1995-07-18 14:51:37 +00002103 case MAKE_FUNCTION:
2104 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002105 x = PyFunction_New(v, f->f_globals);
2106 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002107 /* XXX Maybe this should be a separate opcode? */
2108 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002109 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002110 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002111 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002112 x = NULL;
2113 break;
2114 }
2115 while (--oparg >= 0) {
2116 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002117 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002118 }
2119 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002120 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002121 }
2122 PUSH(x);
2123 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002124
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002125 case MAKE_CLOSURE:
2126 {
2127 int nfree;
2128 v = POP(); /* code object */
2129 x = PyFunction_New(v, f->f_globals);
Jeremy Hylton733c8932001-12-13 19:51:56 +00002130 nfree = PyCode_GetNumFree((PyCodeObject *)v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002131 Py_DECREF(v);
2132 /* XXX Maybe this should be a separate opcode? */
2133 if (x != NULL && nfree > 0) {
2134 v = PyTuple_New(nfree);
2135 if (v == NULL) {
2136 Py_DECREF(x);
2137 x = NULL;
2138 break;
2139 }
2140 while (--nfree >= 0) {
2141 w = POP();
2142 PyTuple_SET_ITEM(v, nfree, w);
2143 }
2144 err = PyFunction_SetClosure(x, v);
2145 Py_DECREF(v);
2146 }
2147 if (x != NULL && oparg > 0) {
2148 v = PyTuple_New(oparg);
2149 if (v == NULL) {
2150 Py_DECREF(x);
2151 x = NULL;
2152 break;
2153 }
2154 while (--oparg >= 0) {
2155 w = POP();
2156 PyTuple_SET_ITEM(v, oparg, w);
2157 }
2158 err = PyFunction_SetDefaults(x, v);
2159 Py_DECREF(v);
2160 }
2161 PUSH(x);
2162 break;
2163 }
2164
Guido van Rossum8861b741996-07-30 16:49:37 +00002165 case BUILD_SLICE:
2166 if (oparg == 3)
2167 w = POP();
2168 else
2169 w = NULL;
2170 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002171 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002172 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002173 Py_DECREF(u);
2174 Py_DECREF(v);
2175 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002176 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002177 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002178 break;
2179
Fred Drakeef8ace32000-08-24 00:32:09 +00002180 case EXTENDED_ARG:
2181 opcode = NEXTOP();
2182 oparg = oparg<<16 | NEXTARG();
2183 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002184
Guido van Rossum374a9221991-04-04 10:40:29 +00002185 default:
2186 fprintf(stderr,
2187 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002188 PyCode_Addr2Line(f->f_code, f->f_lasti),
2189 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002190 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002191 why = WHY_EXCEPTION;
2192 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002193
2194#ifdef CASE_TOO_BIG
2195 }
2196#endif
2197
Guido van Rossum374a9221991-04-04 10:40:29 +00002198 } /* switch */
2199
2200 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002201
Guido van Rossum374a9221991-04-04 10:40:29 +00002202 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002203
Guido van Rossum374a9221991-04-04 10:40:29 +00002204 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002205 if (err == 0 && x != NULL) {
2206#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002207 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002208 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002209 fprintf(stderr,
2210 "XXX undetected error\n");
2211 else
2212#endif
2213 continue; /* Normal, fast path */
2214 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002215 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002216 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002217 err = 0;
2218 }
2219
Guido van Rossum374a9221991-04-04 10:40:29 +00002220 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002221
Guido van Rossum374a9221991-04-04 10:40:29 +00002222 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002223 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002224 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002225 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002226 why = WHY_EXCEPTION;
2227 }
2228 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002229#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002230 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002231 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002232 if (PyErr_Occurred()) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002233 fprintf(stderr,
2234 "XXX undetected error (why=%d)\n",
2235 why);
2236 why = WHY_EXCEPTION;
2237 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002238 }
2239#endif
2240
2241 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002242
Guido van Rossum374a9221991-04-04 10:40:29 +00002243 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002244 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002245
Fred Drake8f51f542001-10-04 14:48:42 +00002246 if (tstate->c_tracefunc != NULL)
2247 call_exc_trace(tstate->c_tracefunc,
2248 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002249 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002250
Guido van Rossum374a9221991-04-04 10:40:29 +00002251 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002252
Guido van Rossum374a9221991-04-04 10:40:29 +00002253 if (why == WHY_RERAISE)
2254 why = WHY_EXCEPTION;
2255
2256 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002257
Tim Peters5ca576e2001-06-18 22:08:13 +00002258 while (why != WHY_NOT && why != WHY_YIELD && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002259 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002260
2261 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2262 /* For a continue inside a try block,
2263 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002264 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2265 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002266 why = WHY_NOT;
2267 JUMPTO(PyInt_AS_LONG(retval));
2268 Py_DECREF(retval);
2269 break;
2270 }
2271
Guido van Rossum374a9221991-04-04 10:40:29 +00002272 while (STACK_LEVEL() > b->b_level) {
2273 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002274 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002275 }
2276 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2277 why = WHY_NOT;
2278 JUMPTO(b->b_handler);
2279 break;
2280 }
2281 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002282 (b->b_type == SETUP_EXCEPT &&
2283 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002284 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002285 PyObject *exc, *val, *tb;
2286 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002287 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002288 val = Py_None;
2289 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002290 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002291 /* Make the raw exception data
2292 available to the handler,
2293 so a program can emulate the
2294 Python main loop. Don't do
2295 this for 'finally'. */
2296 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002297 PyErr_NormalizeException(
2298 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002299 set_exc_info(tstate,
2300 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002301 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002302 if (tb == NULL) {
2303 Py_INCREF(Py_None);
2304 PUSH(Py_None);
2305 } else
2306 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002307 PUSH(val);
2308 PUSH(exc);
2309 }
2310 else {
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002311 if (why == WHY_RETURN ||
Guido van Rossumc5fe5eb2002-06-12 03:45:21 +00002312 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00002313 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002314 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002315 PUSH(v);
2316 }
2317 why = WHY_NOT;
2318 JUMPTO(b->b_handler);
2319 break;
2320 }
2321 } /* unwind stack */
2322
2323 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002324
Guido van Rossum374a9221991-04-04 10:40:29 +00002325 if (why != WHY_NOT)
2326 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002327
Guido van Rossum374a9221991-04-04 10:40:29 +00002328 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002329
Guido van Rossum35974fb2001-12-06 21:28:18 +00002330 if (why != WHY_YIELD) {
2331 /* Pop remaining stack entries -- but when yielding */
2332 while (!EMPTY()) {
2333 v = POP();
2334 Py_XDECREF(v);
2335 }
2336 }
2337
Tim Peters5ca576e2001-06-18 22:08:13 +00002338 if (why != WHY_RETURN && why != WHY_YIELD)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002339 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002340
Fred Drake9e3ad782001-07-03 23:39:52 +00002341 if (tstate->use_tracing) {
2342 if (tstate->c_tracefunc
2343 && (why == WHY_RETURN || why == WHY_YIELD)) {
2344 if (call_trace(tstate->c_tracefunc,
2345 tstate->c_traceobj, f,
2346 PyTrace_RETURN, retval)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002347 Py_XDECREF(retval);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002348 retval = NULL;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002349 why = WHY_EXCEPTION;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002350 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002351 }
Fred Drake8f51f542001-10-04 14:48:42 +00002352 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002353 if (why == WHY_EXCEPTION)
2354 call_trace_protected(tstate->c_profilefunc,
2355 tstate->c_profileobj, f,
2356 PyTrace_RETURN);
2357 else if (call_trace(tstate->c_profilefunc,
2358 tstate->c_profileobj, f,
2359 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002360 Py_XDECREF(retval);
2361 retval = NULL;
2362 why = WHY_EXCEPTION;
2363 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002364 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002365 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002366
Guido van Rossuma027efa1997-05-05 20:56:21 +00002367 reset_exc_info(tstate);
2368
Tim Peters5ca576e2001-06-18 22:08:13 +00002369 /* pop frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002370 --tstate->recursion_depth;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002371 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002372
Guido van Rossum96a42c81992-01-12 02:29:51 +00002373 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002374}
2375
Tim Peters6d6c1a32001-08-02 04:15:00 +00002376PyObject *
2377PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002378 PyObject **args, int argcount, PyObject **kws, int kwcount,
2379 PyObject **defs, int defcount, PyObject *closure)
2380{
2381 register PyFrameObject *f;
2382 register PyObject *retval = NULL;
2383 register PyObject **fastlocals, **freevars;
2384 PyThreadState *tstate = PyThreadState_GET();
2385 PyObject *x, *u;
2386
2387 if (globals == NULL) {
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002388 PyErr_SetString(PyExc_SystemError,
2389 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002390 return NULL;
2391 }
2392
Jeremy Hylton985eba52003-02-05 23:13:00 +00002393 assert(globals != NULL);
2394 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002395 if (f == NULL)
2396 return NULL;
2397
2398 fastlocals = f->f_localsplus;
2399 freevars = f->f_localsplus + f->f_nlocals;
2400
2401 if (co->co_argcount > 0 ||
2402 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2403 int i;
2404 int n = argcount;
2405 PyObject *kwdict = NULL;
2406 if (co->co_flags & CO_VARKEYWORDS) {
2407 kwdict = PyDict_New();
2408 if (kwdict == NULL)
2409 goto fail;
2410 i = co->co_argcount;
2411 if (co->co_flags & CO_VARARGS)
2412 i++;
2413 SETLOCAL(i, kwdict);
2414 }
2415 if (argcount > co->co_argcount) {
2416 if (!(co->co_flags & CO_VARARGS)) {
2417 PyErr_Format(PyExc_TypeError,
2418 "%.200s() takes %s %d "
2419 "%sargument%s (%d given)",
2420 PyString_AsString(co->co_name),
2421 defcount ? "at most" : "exactly",
2422 co->co_argcount,
2423 kwcount ? "non-keyword " : "",
2424 co->co_argcount == 1 ? "" : "s",
2425 argcount);
2426 goto fail;
2427 }
2428 n = co->co_argcount;
2429 }
2430 for (i = 0; i < n; i++) {
2431 x = args[i];
2432 Py_INCREF(x);
2433 SETLOCAL(i, x);
2434 }
2435 if (co->co_flags & CO_VARARGS) {
2436 u = PyTuple_New(argcount - n);
2437 if (u == NULL)
2438 goto fail;
2439 SETLOCAL(co->co_argcount, u);
2440 for (i = n; i < argcount; i++) {
2441 x = args[i];
2442 Py_INCREF(x);
2443 PyTuple_SET_ITEM(u, i-n, x);
2444 }
2445 }
2446 for (i = 0; i < kwcount; i++) {
2447 PyObject *keyword = kws[2*i];
2448 PyObject *value = kws[2*i + 1];
2449 int j;
2450 if (keyword == NULL || !PyString_Check(keyword)) {
2451 PyErr_Format(PyExc_TypeError,
2452 "%.200s() keywords must be strings",
2453 PyString_AsString(co->co_name));
2454 goto fail;
2455 }
2456 /* XXX slow -- speed up using dictionary? */
2457 for (j = 0; j < co->co_argcount; j++) {
2458 PyObject *nm = PyTuple_GET_ITEM(
2459 co->co_varnames, j);
2460 int cmp = PyObject_RichCompareBool(
2461 keyword, nm, Py_EQ);
2462 if (cmp > 0)
2463 break;
2464 else if (cmp < 0)
2465 goto fail;
2466 }
2467 /* Check errors from Compare */
2468 if (PyErr_Occurred())
2469 goto fail;
2470 if (j >= co->co_argcount) {
2471 if (kwdict == NULL) {
2472 PyErr_Format(PyExc_TypeError,
2473 "%.200s() got an unexpected "
2474 "keyword argument '%.400s'",
2475 PyString_AsString(co->co_name),
2476 PyString_AsString(keyword));
2477 goto fail;
2478 }
2479 PyDict_SetItem(kwdict, keyword, value);
2480 }
2481 else {
2482 if (GETLOCAL(j) != NULL) {
2483 PyErr_Format(PyExc_TypeError,
2484 "%.200s() got multiple "
2485 "values for keyword "
2486 "argument '%.400s'",
2487 PyString_AsString(co->co_name),
2488 PyString_AsString(keyword));
2489 goto fail;
2490 }
2491 Py_INCREF(value);
2492 SETLOCAL(j, value);
2493 }
2494 }
2495 if (argcount < co->co_argcount) {
2496 int m = co->co_argcount - defcount;
2497 for (i = argcount; i < m; i++) {
2498 if (GETLOCAL(i) == NULL) {
2499 PyErr_Format(PyExc_TypeError,
2500 "%.200s() takes %s %d "
2501 "%sargument%s (%d given)",
2502 PyString_AsString(co->co_name),
2503 ((co->co_flags & CO_VARARGS) ||
2504 defcount) ? "at least"
2505 : "exactly",
2506 m, kwcount ? "non-keyword " : "",
2507 m == 1 ? "" : "s", i);
2508 goto fail;
2509 }
2510 }
2511 if (n > m)
2512 i = n - m;
2513 else
2514 i = 0;
2515 for (; i < defcount; i++) {
2516 if (GETLOCAL(m+i) == NULL) {
2517 PyObject *def = defs[i];
2518 Py_INCREF(def);
2519 SETLOCAL(m+i, def);
2520 }
2521 }
2522 }
2523 }
2524 else {
2525 if (argcount > 0 || kwcount > 0) {
2526 PyErr_Format(PyExc_TypeError,
2527 "%.200s() takes no arguments (%d given)",
2528 PyString_AsString(co->co_name),
2529 argcount + kwcount);
2530 goto fail;
2531 }
2532 }
2533 /* Allocate and initialize storage for cell vars, and copy free
2534 vars into frame. This isn't too efficient right now. */
2535 if (f->f_ncells) {
2536 int i = 0, j = 0, nargs, found;
2537 char *cellname, *argname;
2538 PyObject *c;
2539
2540 nargs = co->co_argcount;
2541 if (co->co_flags & CO_VARARGS)
2542 nargs++;
2543 if (co->co_flags & CO_VARKEYWORDS)
2544 nargs++;
2545
2546 /* Check for cells that shadow args */
2547 for (i = 0; i < f->f_ncells && j < nargs; ++i) {
2548 cellname = PyString_AS_STRING(
2549 PyTuple_GET_ITEM(co->co_cellvars, i));
2550 found = 0;
2551 while (j < nargs) {
2552 argname = PyString_AS_STRING(
2553 PyTuple_GET_ITEM(co->co_varnames, j));
2554 if (strcmp(cellname, argname) == 0) {
2555 c = PyCell_New(GETLOCAL(j));
2556 if (c == NULL)
2557 goto fail;
2558 GETLOCAL(f->f_nlocals + i) = c;
2559 found = 1;
2560 break;
2561 }
2562 j++;
2563 }
2564 if (found == 0) {
2565 c = PyCell_New(NULL);
2566 if (c == NULL)
2567 goto fail;
2568 SETLOCAL(f->f_nlocals + i, c);
2569 }
2570 }
2571 /* Initialize any that are left */
2572 while (i < f->f_ncells) {
2573 c = PyCell_New(NULL);
2574 if (c == NULL)
2575 goto fail;
2576 SETLOCAL(f->f_nlocals + i, c);
2577 i++;
2578 }
2579 }
2580 if (f->f_nfreevars) {
2581 int i;
2582 for (i = 0; i < f->f_nfreevars; ++i) {
2583 PyObject *o = PyTuple_GET_ITEM(closure, i);
2584 Py_INCREF(o);
2585 freevars[f->f_ncells + i] = o;
2586 }
2587 }
2588
Tim Peters5ca576e2001-06-18 22:08:13 +00002589 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002590 /* Don't need to keep the reference to f_back, it will be set
2591 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002592 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002593 f->f_back = NULL;
2594
Jeremy Hylton985eba52003-02-05 23:13:00 +00002595 PCALL(PCALL_GENERATOR);
2596
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002597 /* Create a new generator that owns the ready to run frame
2598 * and return that as the value. */
Tim Peters5ca576e2001-06-18 22:08:13 +00002599 return gen_new(f);
2600 }
2601
2602 retval = eval_frame(f);
2603
2604 fail: /* Jump here from prelude on failure */
2605
Tim Petersb13680b2001-11-27 23:29:29 +00002606 /* decref'ing the frame can cause __del__ methods to get invoked,
2607 which can call back into Python. While we're done with the
2608 current Python frame (f), the associated C stack is still in use,
2609 so recursion_depth must be boosted for the duration.
2610 */
2611 assert(tstate != NULL);
2612 ++tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002613 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00002614 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002615 return retval;
2616}
2617
2618
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002619/* Implementation notes for set_exc_info() and reset_exc_info():
2620
2621- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2622 'exc_traceback'. These always travel together.
2623
2624- tstate->curexc_ZZZ is the "hot" exception that is set by
2625 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2626
2627- Once an exception is caught by an except clause, it is transferred
2628 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2629 can pick it up. This is the primary task of set_exc_info().
2630
2631- Now let me explain the complicated dance with frame->f_exc_ZZZ.
2632
2633 Long ago, when none of this existed, there were just a few globals:
2634 one set corresponding to the "hot" exception, and one set
2635 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2636 globals; they were simply stored as sys.exc_ZZZ. For backwards
2637 compatibility, they still are!) The problem was that in code like
2638 this:
2639
2640 try:
2641 "something that may fail"
2642 except "some exception":
2643 "do something else first"
2644 "print the exception from sys.exc_ZZZ."
2645
2646 if "do something else first" invoked something that raised and caught
2647 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2648 cause of subtle bugs. I fixed this by changing the semantics as
2649 follows:
2650
2651 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2652 *in that frame*.
2653
2654 - But initially, and as long as no exception is caught in a given
2655 frame, sys.exc_ZZZ will hold the last exception caught in the
2656 previous frame (or the frame before that, etc.).
2657
2658 The first bullet fixed the bug in the above example. The second
2659 bullet was for backwards compatibility: it was (and is) common to
2660 have a function that is called when an exception is caught, and to
2661 have that function access the caught exception via sys.exc_ZZZ.
2662 (Example: traceback.print_exc()).
2663
2664 At the same time I fixed the problem that sys.exc_ZZZ weren't
2665 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2666 but that's really a separate improvement.
2667
2668 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2669 variables to what they were before the current frame was called. The
2670 set_exc_info() function saves them on the frame so that
2671 reset_exc_info() can restore them. The invariant is that
2672 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2673 exception (where "catching" an exception applies only to successful
2674 except clauses); and if the current frame ever caught an exception,
2675 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2676 at the start of the current frame.
2677
2678*/
2679
Guido van Rossuma027efa1997-05-05 20:56:21 +00002680static void
Guido van Rossumac7be682001-01-17 15:42:30 +00002681set_exc_info(PyThreadState *tstate,
2682 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002683{
2684 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002685 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00002686
Guido van Rossuma027efa1997-05-05 20:56:21 +00002687 frame = tstate->frame;
2688 if (frame->f_exc_type == NULL) {
2689 /* This frame didn't catch an exception before */
2690 /* Save previous exception of this thread in this frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002691 if (tstate->exc_type == NULL) {
2692 Py_INCREF(Py_None);
2693 tstate->exc_type = Py_None;
2694 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002695 tmp_type = frame->f_exc_type;
2696 tmp_value = frame->f_exc_value;
2697 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002698 Py_XINCREF(tstate->exc_type);
2699 Py_XINCREF(tstate->exc_value);
2700 Py_XINCREF(tstate->exc_traceback);
2701 frame->f_exc_type = tstate->exc_type;
2702 frame->f_exc_value = tstate->exc_value;
2703 frame->f_exc_traceback = tstate->exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002704 Py_XDECREF(tmp_type);
2705 Py_XDECREF(tmp_value);
2706 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002707 }
2708 /* Set new exception for this thread */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002709 tmp_type = tstate->exc_type;
2710 tmp_value = tstate->exc_value;
2711 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002712 Py_XINCREF(type);
2713 Py_XINCREF(value);
2714 Py_XINCREF(tb);
2715 tstate->exc_type = type;
2716 tstate->exc_value = value;
2717 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002718 Py_XDECREF(tmp_type);
2719 Py_XDECREF(tmp_value);
2720 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002721 /* For b/w compatibility */
2722 PySys_SetObject("exc_type", type);
2723 PySys_SetObject("exc_value", value);
2724 PySys_SetObject("exc_traceback", tb);
2725}
2726
2727static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002728reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002729{
2730 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002731 PyObject *tmp_type, *tmp_value, *tmp_tb;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002732 frame = tstate->frame;
2733 if (frame->f_exc_type != NULL) {
2734 /* This frame caught an exception */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002735 tmp_type = tstate->exc_type;
2736 tmp_value = tstate->exc_value;
2737 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002738 Py_XINCREF(frame->f_exc_type);
2739 Py_XINCREF(frame->f_exc_value);
2740 Py_XINCREF(frame->f_exc_traceback);
2741 tstate->exc_type = frame->f_exc_type;
2742 tstate->exc_value = frame->f_exc_value;
2743 tstate->exc_traceback = frame->f_exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002744 Py_XDECREF(tmp_type);
2745 Py_XDECREF(tmp_value);
2746 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002747 /* For b/w compatibility */
2748 PySys_SetObject("exc_type", frame->f_exc_type);
2749 PySys_SetObject("exc_value", frame->f_exc_value);
2750 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
2751 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002752 tmp_type = frame->f_exc_type;
2753 tmp_value = frame->f_exc_value;
2754 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002755 frame->f_exc_type = NULL;
2756 frame->f_exc_value = NULL;
2757 frame->f_exc_traceback = NULL;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002758 Py_XDECREF(tmp_type);
2759 Py_XDECREF(tmp_value);
2760 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002761}
2762
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002763/* Logic for the raise statement (too complicated for inlining).
2764 This *consumes* a reference count to each of its arguments. */
Guido van Rossum1aa14831997-01-21 05:34:20 +00002765static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002766do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002767{
Guido van Rossumd295f121998-04-09 21:39:57 +00002768 if (type == NULL) {
2769 /* Reraise */
2770 PyThreadState *tstate = PyThreadState_Get();
2771 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
2772 value = tstate->exc_value;
2773 tb = tstate->exc_traceback;
2774 Py_XINCREF(type);
2775 Py_XINCREF(value);
2776 Py_XINCREF(tb);
2777 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002778
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002779 /* We support the following forms of raise:
2780 raise <class>, <classinstance>
2781 raise <class>, <argument tuple>
2782 raise <class>, None
2783 raise <class>, <argument>
2784 raise <classinstance>, None
2785 raise <string>, <object>
2786 raise <string>, None
2787
2788 An omitted second argument is the same as None.
2789
2790 In addition, raise <tuple>, <anything> is the same as
2791 raising the tuple's first item (and it better have one!);
2792 this rule is applied recursively.
2793
2794 Finally, an optional third argument can be supplied, which
2795 gives the traceback to be substituted (useful when
2796 re-raising an exception after examining it). */
2797
2798 /* First, check the traceback argument, replacing None with
2799 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002800 if (tb == Py_None) {
2801 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002802 tb = NULL;
2803 }
2804 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002805 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002806 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002807 goto raise_error;
2808 }
2809
2810 /* Next, replace a missing value with None */
2811 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002812 value = Py_None;
2813 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002814 }
2815
2816 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00002817 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
2818 PyObject *tmp = type;
2819 type = PyTuple_GET_ITEM(type, 0);
2820 Py_INCREF(type);
2821 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002822 }
2823
Tim Petersafb2c802002-04-18 18:06:20 +00002824 if (PyString_CheckExact(type))
2825 /* Raising builtin string is deprecated but still allowed --
2826 * do nothing. Raising an instance of a new-style str
2827 * subclass is right out. */
Neal Norwitz37aa0662003-01-10 15:31:15 +00002828 PyErr_Warn(PyExc_PendingDeprecationWarning,
2829 "raising a string exception is deprecated");
Barry Warsaw4249f541997-08-22 21:26:19 +00002830
2831 else if (PyClass_Check(type))
2832 PyErr_NormalizeException(&type, &value, &tb);
2833
Guido van Rossumb209a111997-04-29 18:18:01 +00002834 else if (PyInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002835 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002836 if (value != Py_None) {
2837 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002838 "instance exception may not have a separate value");
2839 goto raise_error;
2840 }
2841 else {
2842 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00002843 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002844 value = type;
Guido van Rossumb209a111997-04-29 18:18:01 +00002845 type = (PyObject*) ((PyInstanceObject*)type)->in_class;
2846 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002847 }
2848 }
2849 else {
2850 /* Not something you can raise. You get an exception
2851 anyway, just not what you specified :-) */
Jeremy Hylton960d9482001-04-27 02:25:33 +00002852 PyErr_Format(PyExc_TypeError,
Neal Norwitz37aa0662003-01-10 15:31:15 +00002853 "exceptions must be classes, instances, or "
2854 "strings (deprecated), not %s",
2855 type->ob_type->tp_name);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002856 goto raise_error;
2857 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002858 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002859 if (tb == NULL)
2860 return WHY_EXCEPTION;
2861 else
2862 return WHY_RERAISE;
2863 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00002864 Py_XDECREF(value);
2865 Py_XDECREF(type);
2866 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002867 return WHY_EXCEPTION;
2868}
2869
Tim Petersd6d010b2001-06-21 02:49:55 +00002870/* Iterate v argcnt times and store the results on the stack (via decreasing
2871 sp). Return 1 for success, 0 if error. */
2872
Barry Warsawe42b18f1997-08-25 22:13:04 +00002873static int
Tim Petersd6d010b2001-06-21 02:49:55 +00002874unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00002875{
Tim Petersd6d010b2001-06-21 02:49:55 +00002876 int i = 0;
2877 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00002878 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00002879
Tim Petersd6d010b2001-06-21 02:49:55 +00002880 assert(v != NULL);
2881
2882 it = PyObject_GetIter(v);
2883 if (it == NULL)
2884 goto Error;
2885
2886 for (; i < argcnt; i++) {
2887 w = PyIter_Next(it);
2888 if (w == NULL) {
2889 /* Iterator done, via error or exhaustion. */
2890 if (!PyErr_Occurred()) {
2891 PyErr_Format(PyExc_ValueError,
2892 "need more than %d value%s to unpack",
2893 i, i == 1 ? "" : "s");
2894 }
2895 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00002896 }
2897 *--sp = w;
2898 }
Tim Petersd6d010b2001-06-21 02:49:55 +00002899
2900 /* We better have exhausted the iterator now. */
2901 w = PyIter_Next(it);
2902 if (w == NULL) {
2903 if (PyErr_Occurred())
2904 goto Error;
2905 Py_DECREF(it);
2906 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00002907 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00002908 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00002909 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00002910 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00002911Error:
Barry Warsaw91010551997-08-25 22:30:51 +00002912 for (; i > 0; i--, sp++)
2913 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00002914 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00002915 return 0;
2916}
2917
2918
Guido van Rossum96a42c81992-01-12 02:29:51 +00002919#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00002920static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002921prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002922{
Guido van Rossum3f5da241990-12-20 15:06:42 +00002923 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00002924 if (PyObject_Print(v, stdout, 0) != 0)
2925 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00002926 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00002927 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002928}
Guido van Rossum3f5da241990-12-20 15:06:42 +00002929#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002930
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002931static void
Fred Drake5755ce62001-06-27 19:19:46 +00002932call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002933{
Guido van Rossumb209a111997-04-29 18:18:01 +00002934 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002935 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00002936 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00002937 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002938 value = Py_None;
2939 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00002940 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002941 arg = Py_BuildValue("(OOO)", type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002942 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002943 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002944 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002945 }
Fred Drake5755ce62001-06-27 19:19:46 +00002946 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002947 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002948 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00002949 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002950 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00002951 Py_XDECREF(type);
2952 Py_XDECREF(value);
2953 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002954 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002955}
2956
Fred Drake4ec5d562001-10-04 19:26:43 +00002957static void
2958call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
2959 int what)
2960{
2961 PyObject *type, *value, *traceback;
2962 int err;
2963 PyErr_Fetch(&type, &value, &traceback);
2964 err = call_trace(func, obj, frame, what, NULL);
2965 if (err == 0)
2966 PyErr_Restore(type, value, traceback);
2967 else {
2968 Py_XDECREF(type);
2969 Py_XDECREF(value);
2970 Py_XDECREF(traceback);
2971 }
2972}
2973
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002974static int
Fred Drake5755ce62001-06-27 19:19:46 +00002975call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
2976 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002977{
Fred Drake5755ce62001-06-27 19:19:46 +00002978 register PyThreadState *tstate = frame->f_tstate;
2979 int result;
2980 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002981 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002982 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00002983 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00002984 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00002985 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
2986 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00002987 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00002988 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002989}
2990
Michael W. Hudson006c7522002-11-08 13:08:46 +00002991static int
Michael W. Hudson019a78e2002-11-08 12:53:11 +00002992maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002993 PyFrameObject *frame, int *instr_lb, int *instr_ub)
2994{
2995 /* The theory of SET_LINENO-less tracing.
2996
2997 In a nutshell, we use the co_lnotab field of the code object
2998 to tell when execution has moved onto a different line.
2999
3000 As mentioned above, the basic idea is so set things up so
3001 that
3002
3003 *instr_lb <= frame->f_lasti < *instr_ub
3004
3005 is true so long as execution does not change lines.
3006
3007 This is all fairly simple. Digging the information out of
3008 co_lnotab takes some work, but is conceptually clear.
3009
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003010 Somewhat harder to explain is why we don't *always* call the
3011 line trace function when the above test fails.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003012
3013 Consider this code:
3014
3015 1: def f(a):
3016 2: if a:
3017 3: print 1
3018 4: else:
3019 5: print 2
3020
3021 which compiles to this:
3022
3023 2 0 LOAD_FAST 0 (a)
3024 3 JUMP_IF_FALSE 9 (to 15)
3025 6 POP_TOP
3026
3027 3 7 LOAD_CONST 1 (1)
3028 10 PRINT_ITEM
3029 11 PRINT_NEWLINE
3030 12 JUMP_FORWARD 6 (to 21)
3031 >> 15 POP_TOP
3032
3033 5 16 LOAD_CONST 2 (2)
3034 19 PRINT_ITEM
3035 20 PRINT_NEWLINE
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003036 >> 21 LOAD_CONST 0 (None)
3037 24 RETURN_VALUE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003038
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003039 If 'a' is false, execution will jump to instruction at offset
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003040 15 and the co_lnotab will claim that execution has moved to
3041 line 3. This is at best misleading. In this case we could
3042 associate the POP_TOP with line 4, but that doesn't make
3043 sense in all cases (I think).
3044
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003045 What we do is only call the line trace function if the co_lnotab
3046 indicates we have jumped to the *start* of a line, i.e. if the
3047 current instruction offset matches the offset given for the
3048 start of a line by the co_lnotab.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003049
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003050 This also takes care of the situation where 'a' is true.
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003051 Execution will jump from instruction offset 12 to offset 21.
3052 Then the co_lnotab would imply that execution has moved to line
3053 5, which is again misleading.
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003054
3055 Why do we set f_lineno when tracing? Well, consider the code
3056 above when 'a' is true. If stepping through this with 'n' in
3057 pdb, you would stop at line 1 with a "call" type event, then
3058 line events on lines 2 and 3, then a "return" type event -- but
3059 you would be shown line 5 during this event. This is a change
3060 from the behaviour in 2.2 and before, and I've found it
3061 confusing in practice. By setting and using f_lineno when
3062 tracing, one can report a line number different from that
3063 suggested by f_lasti on this one occasion where it's desirable.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003064 */
3065
Michael W. Hudson006c7522002-11-08 13:08:46 +00003066 int result = 0;
3067
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003068 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003069 PyCodeObject* co = frame->f_code;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003070 int size, addr, line;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003071 unsigned char* p;
3072
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003073 size = PyString_GET_SIZE(co->co_lnotab) / 2;
3074 p = (unsigned char*)PyString_AS_STRING(co->co_lnotab);
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003075
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003076 addr = 0;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003077 line = co->co_firstlineno;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003078
3079 /* possible optimization: if f->f_lasti == instr_ub
3080 (likely to be a common case) then we already know
3081 instr_lb -- if we stored the matching value of p
3082 somwhere we could skip the first while loop. */
3083
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003084 /* see comments in compile.c for the description of
3085 co_lnotab. A point to remember: increments to p
3086 should come in pairs -- although we don't care about
3087 the line increments here, treating them as byte
3088 increments gets confusing, to say the least. */
3089
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003090 while (size > 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003091 if (addr + *p > frame->f_lasti)
3092 break;
3093 addr += *p++;
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003094 if (*p) *instr_lb = addr;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003095 line += *p++;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003096 --size;
3097 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003098
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003099 if (addr == frame->f_lasti) {
3100 frame->f_lineno = line;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003101 result = call_trace(func, obj, frame,
3102 PyTrace_LINE, Py_None);
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003103 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003104
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003105 if (size > 0) {
3106 while (--size >= 0) {
3107 addr += *p++;
3108 if (*p++)
3109 break;
3110 }
3111 *instr_ub = addr;
3112 }
3113 else {
3114 *instr_ub = INT_MAX;
3115 }
3116 }
Michael W. Hudson006c7522002-11-08 13:08:46 +00003117
3118 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003119}
3120
Fred Drake5755ce62001-06-27 19:19:46 +00003121void
3122PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003123{
Fred Drake5755ce62001-06-27 19:19:46 +00003124 PyThreadState *tstate = PyThreadState_Get();
3125 PyObject *temp = tstate->c_profileobj;
3126 Py_XINCREF(arg);
3127 tstate->c_profilefunc = NULL;
3128 tstate->c_profileobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003129 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003130 Py_XDECREF(temp);
3131 tstate->c_profilefunc = func;
3132 tstate->c_profileobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003133 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003134}
3135
3136void
3137PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3138{
3139 PyThreadState *tstate = PyThreadState_Get();
3140 PyObject *temp = tstate->c_traceobj;
3141 Py_XINCREF(arg);
3142 tstate->c_tracefunc = NULL;
3143 tstate->c_traceobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003144 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003145 Py_XDECREF(temp);
3146 tstate->c_tracefunc = func;
3147 tstate->c_traceobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003148 tstate->use_tracing = ((func != NULL)
3149 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003150}
3151
Guido van Rossumb209a111997-04-29 18:18:01 +00003152PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003153PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003154{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003155 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003156 if (current_frame == NULL)
Michael W. Hudson019a78e2002-11-08 12:53:11 +00003157 return PyThreadState_Get()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003158 else
3159 return current_frame->f_builtins;
3160}
3161
Guido van Rossumb209a111997-04-29 18:18:01 +00003162PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003163PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003164{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003165 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003166 if (current_frame == NULL)
3167 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003168 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003169 return current_frame->f_locals;
3170}
3171
Guido van Rossumb209a111997-04-29 18:18:01 +00003172PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003173PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003174{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003175 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003176 if (current_frame == NULL)
3177 return NULL;
3178 else
3179 return current_frame->f_globals;
3180}
3181
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003182PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003183PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003184{
Michael W. Hudson019a78e2002-11-08 12:53:11 +00003185 PyThreadState *tstate = PyThreadState_Get();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003186 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003187}
3188
Guido van Rossum6135a871995-01-09 17:53:26 +00003189int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003190PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003191{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003192 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003193 return current_frame == NULL ? 0 : current_frame->f_restricted;
3194}
3195
Guido van Rossumbe270261997-05-22 22:26:18 +00003196int
Tim Peters5ba58662001-07-16 02:29:45 +00003197PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003198{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003199 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003200 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003201
3202 if (current_frame != NULL) {
3203 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003204 const int compilerflags = codeflags & PyCF_MASK;
3205 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003206 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003207 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003208 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003209#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003210 if (codeflags & CO_GENERATOR_ALLOWED) {
3211 result = 1;
3212 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3213 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003214#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003215 }
3216 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003217}
3218
3219int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003220Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003221{
Guido van Rossumb209a111997-04-29 18:18:01 +00003222 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003223 if (f == NULL)
3224 return 0;
3225 if (!PyFile_SoftSpace(f, 0))
3226 return 0;
3227 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003228}
3229
Guido van Rossum3f5da241990-12-20 15:06:42 +00003230
Guido van Rossum681d79a1995-07-18 14:51:37 +00003231/* External interface to call any callable object.
3232 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003233
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003234#undef PyEval_CallObject
3235/* for backward compatibility: export this interface */
3236
Guido van Rossumb209a111997-04-29 18:18:01 +00003237PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003238PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003239{
Guido van Rossumb209a111997-04-29 18:18:01 +00003240 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003241}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003242#define PyEval_CallObject(func,arg) \
3243 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003244
Guido van Rossumb209a111997-04-29 18:18:01 +00003245PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003246PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003247{
Jeremy Hylton52820442001-01-03 23:52:36 +00003248 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003249
3250 if (arg == NULL)
Guido van Rossumb209a111997-04-29 18:18:01 +00003251 arg = PyTuple_New(0);
3252 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003253 PyErr_SetString(PyExc_TypeError,
3254 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003255 return NULL;
3256 }
3257 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003258 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003259
Guido van Rossumb209a111997-04-29 18:18:01 +00003260 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003261 PyErr_SetString(PyExc_TypeError,
3262 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003263 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003264 return NULL;
3265 }
3266
Tim Peters6d6c1a32001-08-02 04:15:00 +00003267 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003268 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003269 return result;
3270}
3271
Tim Peters6d6c1a32001-08-02 04:15:00 +00003272char *
3273PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003274{
3275 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003276 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003277 else if (PyFunction_Check(func))
3278 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3279 else if (PyCFunction_Check(func))
3280 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3281 else if (PyClass_Check(func))
3282 return PyString_AsString(((PyClassObject*)func)->cl_name);
3283 else if (PyInstance_Check(func)) {
3284 return PyString_AsString(
3285 ((PyInstanceObject*)func)->in_class->cl_name);
3286 } else {
3287 return func->ob_type->tp_name;
3288 }
3289}
3290
Tim Peters6d6c1a32001-08-02 04:15:00 +00003291char *
3292PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003293{
3294 if (PyMethod_Check(func))
3295 return "()";
3296 else if (PyFunction_Check(func))
3297 return "()";
3298 else if (PyCFunction_Check(func))
3299 return "()";
3300 else if (PyClass_Check(func))
3301 return " constructor";
3302 else if (PyInstance_Check(func)) {
3303 return " instance";
3304 } else {
3305 return " object";
3306 }
3307}
3308
Jeremy Hylton52820442001-01-03 23:52:36 +00003309#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
3310
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003311static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003312err_args(PyObject *func, int flags, int nargs)
3313{
3314 if (flags & METH_NOARGS)
3315 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003316 "%.200s() takes no arguments (%d given)",
Jeremy Hylton192690e2002-08-16 18:36:11 +00003317 ((PyCFunctionObject *)func)->m_ml->ml_name,
3318 nargs);
3319 else
3320 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003321 "%.200s() takes exactly one argument (%d given)",
Jeremy Hylton192690e2002-08-16 18:36:11 +00003322 ((PyCFunctionObject *)func)->m_ml->ml_name,
3323 nargs);
3324}
3325
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003326static PyObject *
3327call_function(PyObject ***pp_stack, int oparg)
3328{
3329 int na = oparg & 0xff;
3330 int nk = (oparg>>8) & 0xff;
3331 int n = na + 2 * nk;
3332 PyObject **pfunc = (*pp_stack) - n - 1;
3333 PyObject *func = *pfunc;
3334 PyObject *x, *w;
3335
Jeremy Hylton985eba52003-02-05 23:13:00 +00003336 /* Always dispatch PyCFunction first, because these are
3337 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003338 */
3339 if (PyCFunction_Check(func) && nk == 0) {
3340 int flags = PyCFunction_GET_FLAGS(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003341 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003342 if (flags & (METH_NOARGS | METH_O)) {
3343 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3344 PyObject *self = PyCFunction_GET_SELF(func);
3345 if (flags & METH_NOARGS && na == 0)
3346 x = (*meth)(self, NULL);
3347 else if (flags & METH_O && na == 1) {
3348 PyObject *arg = EXT_POP(*pp_stack);
3349 x = (*meth)(self, arg);
3350 Py_DECREF(arg);
3351 }
3352 else {
3353 err_args(func, flags, na);
3354 x = NULL;
3355 }
3356 }
3357 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003358 PyObject *callargs;
3359 callargs = load_args(pp_stack, na);
3360 x = PyCFunction_Call(func, callargs, NULL);
3361 Py_XDECREF(callargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003362 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003363 } else {
3364 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3365 /* optimize access to bound methods */
3366 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003367 PCALL(PCALL_METHOD);
3368 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003369 Py_INCREF(self);
3370 func = PyMethod_GET_FUNCTION(func);
3371 Py_INCREF(func);
3372 Py_DECREF(*pfunc);
3373 *pfunc = self;
3374 na++;
3375 n++;
3376 } else
3377 Py_INCREF(func);
3378 if (PyFunction_Check(func))
3379 x = fast_function(func, pp_stack, n, na, nk);
3380 else
3381 x = do_call(func, pp_stack, na, nk);
3382 Py_DECREF(func);
3383 }
3384
Jeremy Hylton985eba52003-02-05 23:13:00 +00003385 /* What does this do? */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003386 while ((*pp_stack) > pfunc) {
3387 w = EXT_POP(*pp_stack);
3388 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003389 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003390 }
3391 return x;
3392}
3393
Jeremy Hylton192690e2002-08-16 18:36:11 +00003394/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003395 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003396 For the simplest case -- a function that takes only positional
3397 arguments and is called with only positional arguments -- it
3398 inlines the most primitive frame setup code from
3399 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3400 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003401*/
3402
3403static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003404fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003405{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003406 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003407 PyObject *globals = PyFunction_GET_GLOBALS(func);
3408 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3409 PyObject **d = NULL;
3410 int nd = 0;
3411
Jeremy Hylton985eba52003-02-05 23:13:00 +00003412 PCALL(PCALL_FUNCTION);
3413 PCALL(PCALL_FAST_FUNCTION);
3414 if (argdefs == NULL && co->co_argcount == n &&
3415 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3416 PyFrameObject *f;
3417 PyObject *retval = NULL;
3418 PyThreadState *tstate = PyThreadState_GET();
3419 PyObject **fastlocals, **stack;
3420 int i;
3421
3422 PCALL(PCALL_FASTER_FUNCTION);
3423 assert(globals != NULL);
3424 /* XXX Perhaps we should create a specialized
3425 PyFrame_New() that doesn't take locals, but does
3426 take builtins without sanity checking them.
3427 */
3428 f = PyFrame_New(tstate, co, globals, NULL);
3429 if (f == NULL)
3430 return NULL;
3431
3432 fastlocals = f->f_localsplus;
3433 stack = (*pp_stack) - n;
3434
3435 for (i = 0; i < n; i++) {
3436 Py_INCREF(*stack);
3437 fastlocals[i] = *stack++;
3438 }
3439 retval = eval_frame(f);
3440 assert(tstate != NULL);
3441 ++tstate->recursion_depth;
3442 Py_DECREF(f);
3443 --tstate->recursion_depth;
3444 return retval;
3445 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003446 if (argdefs != NULL) {
3447 d = &PyTuple_GET_ITEM(argdefs, 0);
3448 nd = ((PyTupleObject *)argdefs)->ob_size;
3449 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003450 return PyEval_EvalCodeEx(co, globals,
3451 (PyObject *)NULL, (*pp_stack)-n, na,
3452 (*pp_stack)-2*nk, nk, d, nd,
3453 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003454}
3455
3456static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003457update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3458 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003459{
3460 PyObject *kwdict = NULL;
3461 if (orig_kwdict == NULL)
3462 kwdict = PyDict_New();
3463 else {
3464 kwdict = PyDict_Copy(orig_kwdict);
3465 Py_DECREF(orig_kwdict);
3466 }
3467 if (kwdict == NULL)
3468 return NULL;
3469 while (--nk >= 0) {
3470 int err;
3471 PyObject *value = EXT_POP(*pp_stack);
3472 PyObject *key = EXT_POP(*pp_stack);
3473 if (PyDict_GetItem(kwdict, key) != NULL) {
Guido van Rossumac7be682001-01-17 15:42:30 +00003474 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +00003475 "%.200s%s got multiple values "
Jeremy Hylton512a2372001-04-11 13:52:29 +00003476 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003477 PyEval_GetFuncName(func),
3478 PyEval_GetFuncDesc(func),
Jeremy Hylton512a2372001-04-11 13:52:29 +00003479 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003480 Py_DECREF(key);
3481 Py_DECREF(value);
3482 Py_DECREF(kwdict);
3483 return NULL;
3484 }
3485 err = PyDict_SetItem(kwdict, key, value);
3486 Py_DECREF(key);
3487 Py_DECREF(value);
3488 if (err) {
3489 Py_DECREF(kwdict);
3490 return NULL;
3491 }
3492 }
3493 return kwdict;
3494}
3495
3496static PyObject *
3497update_star_args(int nstack, int nstar, PyObject *stararg,
3498 PyObject ***pp_stack)
3499{
3500 PyObject *callargs, *w;
3501
3502 callargs = PyTuple_New(nstack + nstar);
3503 if (callargs == NULL) {
3504 return NULL;
3505 }
3506 if (nstar) {
3507 int i;
3508 for (i = 0; i < nstar; i++) {
3509 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3510 Py_INCREF(a);
3511 PyTuple_SET_ITEM(callargs, nstack + i, a);
3512 }
3513 }
3514 while (--nstack >= 0) {
3515 w = EXT_POP(*pp_stack);
3516 PyTuple_SET_ITEM(callargs, nstack, w);
3517 }
3518 return callargs;
3519}
3520
3521static PyObject *
3522load_args(PyObject ***pp_stack, int na)
3523{
3524 PyObject *args = PyTuple_New(na);
3525 PyObject *w;
3526
3527 if (args == NULL)
3528 return NULL;
3529 while (--na >= 0) {
3530 w = EXT_POP(*pp_stack);
3531 PyTuple_SET_ITEM(args, na, w);
3532 }
3533 return args;
3534}
3535
3536static PyObject *
3537do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3538{
3539 PyObject *callargs = NULL;
3540 PyObject *kwdict = NULL;
3541 PyObject *result = NULL;
3542
3543 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003544 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003545 if (kwdict == NULL)
3546 goto call_fail;
3547 }
3548 callargs = load_args(pp_stack, na);
3549 if (callargs == NULL)
3550 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003551#ifdef CALL_PROFILE
3552 /* At this point, we have to look at the type of func to
3553 update the call stats properly. Do it here so as to avoid
3554 exposing the call stats machinery outside ceval.c
3555 */
3556 if (PyFunction_Check(func))
3557 PCALL(PCALL_FUNCTION);
3558 else if (PyMethod_Check(func))
3559 PCALL(PCALL_METHOD);
3560 else if (PyType_Check(func))
3561 PCALL(PCALL_TYPE);
3562 else
3563 PCALL(PCALL_OTHER);
3564#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003565 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003566 call_fail:
3567 Py_XDECREF(callargs);
3568 Py_XDECREF(kwdict);
3569 return result;
3570}
3571
3572static PyObject *
3573ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3574{
3575 int nstar = 0;
3576 PyObject *callargs = NULL;
3577 PyObject *stararg = NULL;
3578 PyObject *kwdict = NULL;
3579 PyObject *result = NULL;
3580
3581 if (flags & CALL_FLAG_KW) {
3582 kwdict = EXT_POP(*pp_stack);
3583 if (!(kwdict && PyDict_Check(kwdict))) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003584 PyErr_Format(PyExc_TypeError,
Jeremy Hylton512a2372001-04-11 13:52:29 +00003585 "%s%s argument after ** "
3586 "must be a dictionary",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003587 PyEval_GetFuncName(func),
3588 PyEval_GetFuncDesc(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003589 goto ext_call_fail;
3590 }
3591 }
3592 if (flags & CALL_FLAG_VAR) {
3593 stararg = EXT_POP(*pp_stack);
3594 if (!PyTuple_Check(stararg)) {
3595 PyObject *t = NULL;
3596 t = PySequence_Tuple(stararg);
3597 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003598 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3599 PyErr_Format(PyExc_TypeError,
3600 "%s%s argument after * "
3601 "must be a sequence",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003602 PyEval_GetFuncName(func),
3603 PyEval_GetFuncDesc(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003604 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003605 goto ext_call_fail;
3606 }
3607 Py_DECREF(stararg);
3608 stararg = t;
3609 }
3610 nstar = PyTuple_GET_SIZE(stararg);
3611 }
3612 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003613 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003614 if (kwdict == NULL)
3615 goto ext_call_fail;
3616 }
3617 callargs = update_star_args(na, nstar, stararg, pp_stack);
3618 if (callargs == NULL)
3619 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003620#ifdef CALL_PROFILE
3621 /* At this point, we have to look at the type of func to
3622 update the call stats properly. Do it here so as to avoid
3623 exposing the call stats machinery outside ceval.c
3624 */
3625 if (PyFunction_Check(func))
3626 PCALL(PCALL_FUNCTION);
3627 else if (PyMethod_Check(func))
3628 PCALL(PCALL_METHOD);
3629 else if (PyType_Check(func))
3630 PCALL(PCALL_TYPE);
3631 else
3632 PCALL(PCALL_OTHER);
3633#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003634 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003635 ext_call_fail:
3636 Py_XDECREF(callargs);
3637 Py_XDECREF(kwdict);
3638 Py_XDECREF(stararg);
3639 return result;
3640}
3641
Guido van Rossum3b9c6671996-07-30 18:40:29 +00003642#define SLICE_ERROR_MSG \
3643 "standard sequence type does not support step size other than one"
3644
Tim Peterscb479e72001-12-16 19:11:44 +00003645/* Extract a slice index from a PyInt or PyLong, and store in *pi.
3646 Silently reduce values larger than INT_MAX to INT_MAX, and silently
3647 boost values less than -INT_MAX to 0. Return 0 on error, 1 on success.
3648*/
Tim Petersb5196382001-12-16 19:44:20 +00003649/* Note: If v is NULL, return success without storing into *pi. This
3650 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3651 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00003652*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00003653int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003654_PyEval_SliceIndex(PyObject *v, int *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003655{
Tim Petersb5196382001-12-16 19:44:20 +00003656 if (v != NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003657 long x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003658 if (PyInt_Check(v)) {
3659 x = PyInt_AsLong(v);
3660 } else if (PyLong_Check(v)) {
3661 x = PyLong_AsLong(v);
3662 if (x==-1 && PyErr_Occurred()) {
3663 PyObject *long_zero;
Guido van Rossumac7be682001-01-17 15:42:30 +00003664 int cmp;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003665
Guido van Rossumac7be682001-01-17 15:42:30 +00003666 if (!PyErr_ExceptionMatches(
3667 PyExc_OverflowError)) {
3668 /* It's not an overflow error, so just
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003669 signal an error */
Guido van Rossum20c6add2000-05-08 14:06:50 +00003670 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003671 }
3672
Guido van Rossumac7be682001-01-17 15:42:30 +00003673 /* Clear the OverflowError */
3674 PyErr_Clear();
3675
3676 /* It's an overflow error, so we need to
3677 check the sign of the long integer,
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003678 set the value to INT_MAX or -INT_MAX,
3679 and clear the error. */
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003680
3681 /* Create a long integer with a value of 0 */
Guido van Rossumac7be682001-01-17 15:42:30 +00003682 long_zero = PyLong_FromLong(0L);
Tim Peterscb479e72001-12-16 19:11:44 +00003683 if (long_zero == NULL)
3684 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003685
3686 /* Check sign */
Guido van Rossumac7be682001-01-17 15:42:30 +00003687 cmp = PyObject_RichCompareBool(v, long_zero,
3688 Py_GT);
3689 Py_DECREF(long_zero);
3690 if (cmp < 0)
3691 return 0;
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003692 else if (cmp)
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003693 x = INT_MAX;
3694 else
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003695 x = -INT_MAX;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003696 }
3697 } else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003698 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003699 "slice indices must be integers");
Guido van Rossum20c6add2000-05-08 14:06:50 +00003700 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003701 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003702 /* Truncate -- very long indices are truncated anyway */
3703 if (x > INT_MAX)
3704 x = INT_MAX;
3705 else if (x < -INT_MAX)
Michael W. Hudsoncbd6fb92002-11-06 15:17:32 +00003706 x = -INT_MAX;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003707 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003708 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00003709 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003710}
3711
Guido van Rossum50d756e2001-08-18 17:43:36 +00003712#undef ISINT
3713#define ISINT(x) ((x) == NULL || PyInt_Check(x) || PyLong_Check(x))
3714
Guido van Rossumb209a111997-04-29 18:18:01 +00003715static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003716apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003717{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003718 PyTypeObject *tp = u->ob_type;
3719 PySequenceMethods *sq = tp->tp_as_sequence;
3720
3721 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3722 int ilow = 0, ihigh = INT_MAX;
3723 if (!_PyEval_SliceIndex(v, &ilow))
3724 return NULL;
3725 if (!_PyEval_SliceIndex(w, &ihigh))
3726 return NULL;
3727 return PySequence_GetSlice(u, ilow, ihigh);
3728 }
3729 else {
3730 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00003731 if (slice != NULL) {
3732 PyObject *res = PyObject_GetItem(u, slice);
3733 Py_DECREF(slice);
3734 return res;
3735 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00003736 else
3737 return NULL;
3738 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003739}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003740
3741static int
Guido van Rossumac7be682001-01-17 15:42:30 +00003742assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
3743 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003744{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003745 PyTypeObject *tp = u->ob_type;
3746 PySequenceMethods *sq = tp->tp_as_sequence;
3747
3748 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3749 int ilow = 0, ihigh = INT_MAX;
3750 if (!_PyEval_SliceIndex(v, &ilow))
3751 return -1;
3752 if (!_PyEval_SliceIndex(w, &ihigh))
3753 return -1;
3754 if (x == NULL)
3755 return PySequence_DelSlice(u, ilow, ihigh);
3756 else
3757 return PySequence_SetSlice(u, ilow, ihigh, x);
3758 }
3759 else {
3760 PyObject *slice = PySlice_New(v, w, NULL);
3761 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00003762 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003763 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00003764 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00003765 else
Guido van Rossum354797c2001-12-03 19:45:06 +00003766 res = PyObject_DelItem(u, slice);
3767 Py_DECREF(slice);
3768 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003769 }
3770 else
3771 return -1;
3772 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003773}
3774
Guido van Rossumb209a111997-04-29 18:18:01 +00003775static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003776cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003777{
Guido van Rossumac7be682001-01-17 15:42:30 +00003778 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003779 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00003780 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003781 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003782 break;
3783 case PyCmp_IS_NOT:
3784 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003785 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003786 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003787 res = PySequence_Contains(w, v);
3788 if (res < 0)
3789 return NULL;
3790 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003791 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00003792 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003793 if (res < 0)
3794 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003795 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003796 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003797 case PyCmp_EXC_MATCH:
Barry Warsaw4249f541997-08-22 21:26:19 +00003798 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003799 break;
3800 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00003801 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003802 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003803 v = res ? Py_True : Py_False;
3804 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003805 return v;
3806}
3807
Thomas Wouters52152252000-08-17 22:55:00 +00003808static PyObject *
3809import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003810{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003811 PyObject *x;
3812
3813 x = PyObject_GetAttr(v, name);
3814 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00003815 PyErr_Format(PyExc_ImportError,
3816 "cannot import name %.230s",
3817 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003818 }
Thomas Wouters52152252000-08-17 22:55:00 +00003819 return x;
3820}
Guido van Rossumac7be682001-01-17 15:42:30 +00003821
Thomas Wouters52152252000-08-17 22:55:00 +00003822static int
3823import_all_from(PyObject *locals, PyObject *v)
3824{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003825 PyObject *all = PyObject_GetAttrString(v, "__all__");
3826 PyObject *dict, *name, *value;
3827 int skip_leading_underscores = 0;
3828 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00003829
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003830 if (all == NULL) {
3831 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3832 return -1; /* Unexpected error */
3833 PyErr_Clear();
3834 dict = PyObject_GetAttrString(v, "__dict__");
3835 if (dict == NULL) {
3836 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3837 return -1;
3838 PyErr_SetString(PyExc_ImportError,
3839 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00003840 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003841 }
3842 all = PyMapping_Keys(dict);
3843 Py_DECREF(dict);
3844 if (all == NULL)
3845 return -1;
3846 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00003847 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003848
3849 for (pos = 0, err = 0; ; pos++) {
3850 name = PySequence_GetItem(all, pos);
3851 if (name == NULL) {
3852 if (!PyErr_ExceptionMatches(PyExc_IndexError))
3853 err = -1;
3854 else
3855 PyErr_Clear();
3856 break;
3857 }
3858 if (skip_leading_underscores &&
3859 PyString_Check(name) &&
3860 PyString_AS_STRING(name)[0] == '_')
3861 {
3862 Py_DECREF(name);
3863 continue;
3864 }
3865 value = PyObject_GetAttr(v, name);
3866 if (value == NULL)
3867 err = -1;
3868 else
3869 err = PyDict_SetItem(locals, name, value);
3870 Py_DECREF(name);
3871 Py_XDECREF(value);
3872 if (err != 0)
3873 break;
3874 }
3875 Py_DECREF(all);
3876 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00003877}
3878
Guido van Rossumb209a111997-04-29 18:18:01 +00003879static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003880build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003881{
Guido van Rossum7851eea2001-09-12 19:19:18 +00003882 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003883
3884 if (PyDict_Check(methods))
3885 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00003886 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00003887 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00003888 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
3889 base = PyTuple_GET_ITEM(bases, 0);
3890 metaclass = PyObject_GetAttrString(base, "__class__");
3891 if (metaclass == NULL) {
3892 PyErr_Clear();
3893 metaclass = (PyObject *)base->ob_type;
3894 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00003895 }
3896 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00003897 else {
3898 PyObject *g = PyEval_GetGlobals();
3899 if (g != NULL && PyDict_Check(g))
3900 metaclass = PyDict_GetItemString(g, "__metaclass__");
3901 if (metaclass == NULL)
3902 metaclass = (PyObject *) &PyClass_Type;
3903 Py_INCREF(metaclass);
3904 }
3905 result = PyObject_CallFunction(metaclass, "OOO", name, bases, methods);
3906 Py_DECREF(metaclass);
3907 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00003908}
3909
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003910static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003911exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
3912 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003913{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003914 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00003915 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003916 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003917
Guido van Rossumb209a111997-04-29 18:18:01 +00003918 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
3919 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003920 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00003921 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003922 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00003923 locals = PyTuple_GetItem(prog, 2);
3924 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003925 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003926 if (globals == Py_None) {
3927 globals = PyEval_GetGlobals();
3928 if (locals == Py_None) {
3929 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00003930 plain = 1;
3931 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003932 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003933 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003934 locals = globals;
Guido van Rossumb209a111997-04-29 18:18:01 +00003935 if (!PyString_Check(prog) &&
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00003936 !PyUnicode_Check(prog) &&
Guido van Rossumb209a111997-04-29 18:18:01 +00003937 !PyCode_Check(prog) &&
3938 !PyFile_Check(prog)) {
3939 PyErr_SetString(PyExc_TypeError,
Guido van Rossumac7be682001-01-17 15:42:30 +00003940 "exec: arg 1 must be a string, file, or code object");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003941 return -1;
3942 }
Fred Drake661ea262000-10-24 19:57:45 +00003943 if (!PyDict_Check(globals)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003944 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003945 "exec: arg 2 must be a dictionary or None");
3946 return -1;
3947 }
3948 if (!PyDict_Check(locals)) {
3949 PyErr_SetString(PyExc_TypeError,
3950 "exec: arg 3 must be a dictionary or None");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003951 return -1;
3952 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003953 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00003954 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00003955 if (PyCode_Check(prog)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +00003956 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
3957 PyErr_SetString(PyExc_TypeError,
3958 "code object passed to exec may not contain free variables");
3959 return -1;
3960 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00003961 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003962 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00003963 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003964 FILE *fp = PyFile_AsFile(prog);
3965 char *name = PyString_AsString(PyFile_Name(prog));
Tim Peters5ba58662001-07-16 02:29:45 +00003966 PyCompilerFlags cf;
3967 cf.cf_flags = 0;
3968 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00003969 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
3970 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00003971 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00003972 v = PyRun_File(fp, name, Py_file_input, globals,
3973 locals);
Guido van Rossuma400d8a2000-01-12 22:45:54 +00003974 }
3975 else {
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003976 PyObject *tmp = NULL;
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00003977 char *str;
Tim Peters5ba58662001-07-16 02:29:45 +00003978 PyCompilerFlags cf;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003979 cf.cf_flags = 0;
3980#ifdef Py_USING_UNICODE
3981 if (PyUnicode_Check(prog)) {
3982 tmp = PyUnicode_AsUTF8String(prog);
3983 if (tmp == NULL)
3984 return -1;
3985 prog = tmp;
3986 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
3987 }
3988#endif
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00003989 if (PyString_AsStringAndSize(prog, &str, NULL))
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003990 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00003991 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00003992 v = PyRun_StringFlags(str, Py_file_input, globals,
3993 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00003994 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00003995 v = PyRun_String(str, Py_file_input, globals, locals);
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003996 Py_XDECREF(tmp);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003997 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00003998 if (plain)
3999 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004000 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004001 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00004002 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004003 return 0;
4004}
Guido van Rossum24c13741995-02-14 09:42:43 +00004005
Guido van Rossumac7be682001-01-17 15:42:30 +00004006static void
Paul Prescode68140d2000-08-30 20:25:01 +00004007format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4008{
4009 char *obj_str;
4010
4011 if (!obj)
4012 return;
4013
4014 obj_str = PyString_AsString(obj);
4015 if (!obj_str)
4016 return;
4017
4018 PyErr_Format(exc, format_str, obj_str);
4019}
Guido van Rossum950361c1997-01-24 13:49:28 +00004020
4021#ifdef DYNAMIC_EXECUTION_PROFILE
4022
Skip Montanarof118cb12001-10-15 20:51:38 +00004023static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004024getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004025{
4026 int i;
4027 PyObject *l = PyList_New(256);
4028 if (l == NULL) return NULL;
4029 for (i = 0; i < 256; i++) {
4030 PyObject *x = PyInt_FromLong(a[i]);
4031 if (x == NULL) {
4032 Py_DECREF(l);
4033 return NULL;
4034 }
4035 PyList_SetItem(l, i, x);
4036 }
4037 for (i = 0; i < 256; i++)
4038 a[i] = 0;
4039 return l;
4040}
4041
4042PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004043_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004044{
4045#ifndef DXPAIRS
4046 return getarray(dxp);
4047#else
4048 int i;
4049 PyObject *l = PyList_New(257);
4050 if (l == NULL) return NULL;
4051 for (i = 0; i < 257; i++) {
4052 PyObject *x = getarray(dxpairs[i]);
4053 if (x == NULL) {
4054 Py_DECREF(l);
4055 return NULL;
4056 }
4057 PyList_SetItem(l, i, x);
4058 }
4059 return l;
4060#endif
4061}
4062
4063#endif