blob: 4e5b4722991c0e4374e4017765665025a802ce35 [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");
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000324 /* Check someone has called PyEval_InitThreads() to create the lock */
325 assert(interpreter_lock);
Guido van Rossum65d5b571998-12-21 19:32:43 +0000326 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000327 if (PyThreadState_Swap(tstate) != NULL)
328 Py_FatalError(
329 "PyEval_AcquireThread: non-NULL old thread state");
330}
331
332void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000333PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000334{
335 if (tstate == NULL)
336 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
337 if (PyThreadState_Swap(NULL) != tstate)
338 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000339 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000340}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000341
342/* This function is called from PyOS_AfterFork to ensure that newly
343 created child processes don't hold locks referring to threads which
344 are not running in the child process. (This could also be done using
345 pthread_atfork mechanism, at least for the pthreads implementation.) */
346
347void
348PyEval_ReInitThreads(void)
349{
350 if (!interpreter_lock)
351 return;
352 /*XXX Can't use PyThread_free_lock here because it does too
353 much error-checking. Doing this cleanly would require
354 adding a new function to each thread_*.h. Instead, just
355 create a new lock and waste a little bit of memory */
356 interpreter_lock = PyThread_allocate_lock();
357 PyThread_acquire_lock(interpreter_lock, 1);
358 main_thread = PyThread_get_thread_ident();
359}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000360#endif
361
Guido van Rossumff4949e1992-08-05 19:58:53 +0000362/* Functions save_thread and restore_thread are always defined so
363 dynamically loaded modules needn't be compiled separately for use
364 with and without threads: */
365
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000366PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000367PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000368{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000369 PyThreadState *tstate = PyThreadState_Swap(NULL);
370 if (tstate == NULL)
371 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000372#ifdef WITH_THREAD
Guido van Rossumb74eca91997-09-30 22:03:16 +0000373 if (interpreter_lock)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000374 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000375#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000376 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000377}
378
379void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000380PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000381{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000382 if (tstate == NULL)
383 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000384#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000385 if (interpreter_lock) {
Guido van Rossumb74eca91997-09-30 22:03:16 +0000386 int err = errno;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000387 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000388 errno = err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000389 }
390#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000391 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000392}
393
394
Guido van Rossuma9672091994-09-14 13:31:22 +0000395/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
396 signal handlers or Mac I/O completion routines) can schedule calls
397 to a function to be called synchronously.
398 The synchronous function is called with one void* argument.
399 It should return 0 for success or -1 for failure -- failure should
400 be accompanied by an exception.
401
402 If registry succeeds, the registry function returns 0; if it fails
403 (e.g. due to too many pending calls) it returns -1 (without setting
404 an exception condition).
405
406 Note that because registry may occur from within signal handlers,
407 or other asynchronous events, calling malloc() is unsafe!
408
409#ifdef WITH_THREAD
410 Any thread can schedule pending calls, but only the main thread
411 will execute them.
412#endif
413
414 XXX WARNING! ASYNCHRONOUSLY EXECUTING CODE!
415 There are two possible race conditions:
416 (1) nested asynchronous registry calls;
417 (2) registry calls made while pending calls are being processed.
418 While (1) is very unlikely, (2) is a real possibility.
419 The current code is safe against (2), but not against (1).
420 The safety against (2) is derived from the fact that only one
421 thread (the main thread) ever takes things out of the queue.
Guido van Rossuma9672091994-09-14 13:31:22 +0000422
Guido van Rossuma027efa1997-05-05 20:56:21 +0000423 XXX Darn! With the advent of thread state, we should have an array
424 of pending calls per thread in the thread state! Later...
425*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000426
Guido van Rossuma9672091994-09-14 13:31:22 +0000427#define NPENDINGCALLS 32
428static struct {
Thomas Wouters334fb892000-07-25 12:56:38 +0000429 int (*func)(void *);
430 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000431} pendingcalls[NPENDINGCALLS];
432static volatile int pendingfirst = 0;
433static volatile int pendinglast = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000434static volatile int things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000435
436int
Thomas Wouters334fb892000-07-25 12:56:38 +0000437Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000438{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000439 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000440 int i, j;
441 /* XXX Begin critical section */
442 /* XXX If you want this to be safe against nested
443 XXX asynchronous calls, you'll have to work harder! */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000444 if (busy)
445 return -1;
446 busy = 1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000447 i = pendinglast;
448 j = (i + 1) % NPENDINGCALLS;
Guido van Rossum04e70322002-07-17 16:57:13 +0000449 if (j == pendingfirst) {
450 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000451 return -1; /* Queue full */
Guido van Rossum04e70322002-07-17 16:57:13 +0000452 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000453 pendingcalls[i].func = func;
454 pendingcalls[i].arg = arg;
455 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000456
457 _Py_Ticker = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000458 things_to_do = 1; /* Signal main loop */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000459 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000460 /* XXX End critical section */
461 return 0;
462}
463
Guido van Rossum180d7b41994-09-29 09:45:57 +0000464int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000465Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000466{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000467 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000468#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000469 if (main_thread && PyThread_get_thread_ident() != main_thread)
Guido van Rossuma9672091994-09-14 13:31:22 +0000470 return 0;
471#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000472 if (busy)
Guido van Rossum180d7b41994-09-29 09:45:57 +0000473 return 0;
474 busy = 1;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000475 things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000476 for (;;) {
477 int i;
Thomas Wouters334fb892000-07-25 12:56:38 +0000478 int (*func)(void *);
479 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000480 i = pendingfirst;
481 if (i == pendinglast)
482 break; /* Queue empty */
483 func = pendingcalls[i].func;
484 arg = pendingcalls[i].arg;
485 pendingfirst = (i + 1) % NPENDINGCALLS;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000486 if (func(arg) < 0) {
487 busy = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000488 things_to_do = 1; /* We're not done yet */
Guido van Rossuma9672091994-09-14 13:31:22 +0000489 return -1;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000490 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000491 }
Guido van Rossum180d7b41994-09-29 09:45:57 +0000492 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000493 return 0;
494}
495
496
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000497/* The interpreter's recursion limit */
498
Guido van Rossum349ff6f2000-09-01 01:52:08 +0000499static int recursion_limit = 1000;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000500
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000501int
502Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000503{
504 return recursion_limit;
505}
506
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000507void
508Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000509{
510 recursion_limit = new_limit;
511}
512
Guido van Rossum374a9221991-04-04 10:40:29 +0000513/* Status code for main loop (reason for stack unwind) */
514
515enum why_code {
516 WHY_NOT, /* No error */
517 WHY_EXCEPTION, /* Exception occurred */
518 WHY_RERAISE, /* Exception re-raised by 'finally' */
519 WHY_RETURN, /* 'return' statement */
Jeremy Hylton3faa52e2001-02-01 22:48:12 +0000520 WHY_BREAK, /* 'break' statement */
Tim Peters5ca576e2001-06-18 22:08:13 +0000521 WHY_CONTINUE, /* 'continue' statement */
Tim Peters6e6a63f2001-10-18 20:49:35 +0000522 WHY_YIELD /* 'yield' operator */
Guido van Rossum374a9221991-04-04 10:40:29 +0000523};
524
Tim Petersdbd9ba62000-07-09 03:09:57 +0000525static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
Tim Petersd6d010b2001-06-21 02:49:55 +0000526static int unpack_iterable(PyObject *, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000527
Skip Montanarod581d772002-09-03 20:10:45 +0000528/* for manipulating the thread switch and periodic "stuff" - used to be
529 per thread, now just a pair o' globals */
Skip Montanaro99dba272002-09-03 20:19:06 +0000530int _Py_CheckInterval = 100;
531volatile int _Py_Ticker = 100;
Guido van Rossum374a9221991-04-04 10:40:29 +0000532
Guido van Rossumb209a111997-04-29 18:18:01 +0000533PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000534PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000535{
Jeremy Hylton985eba52003-02-05 23:13:00 +0000536 /* XXX raise SystemError if globals is NULL */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000537 return PyEval_EvalCodeEx(co,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000538 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000539 (PyObject **)NULL, 0,
540 (PyObject **)NULL, 0,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000541 (PyObject **)NULL, 0,
542 NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000543}
544
545
546/* Interpreter main loop */
547
Tim Peters6d6c1a32001-08-02 04:15:00 +0000548static PyObject *
Tim Peters5ca576e2001-06-18 22:08:13 +0000549eval_frame(PyFrameObject *f)
Guido van Rossum374a9221991-04-04 10:40:29 +0000550{
Guido van Rossum950361c1997-01-24 13:49:28 +0000551#ifdef DXPAIRS
552 int lastopcode = 0;
553#endif
Tim Petersb6d14da2001-12-19 04:11:07 +0000554 PyObject **stack_pointer; /* Next free slot in value stack */
Guido van Rossum374a9221991-04-04 10:40:29 +0000555 register unsigned char *next_instr;
Moshe Zadkaaa39a7e2000-08-07 06:34:45 +0000556 register int opcode=0; /* Current opcode */
557 register int oparg=0; /* Current opcode argument, if any */
Guido van Rossum374a9221991-04-04 10:40:29 +0000558 register enum why_code why; /* Reason for block stack unwind */
559 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000560 register PyObject *x; /* Result object -- NULL if error */
561 register PyObject *v; /* Temporary objects popped off stack */
562 register PyObject *w;
563 register PyObject *u;
564 register PyObject *t;
Barry Warsaw23c9ec82000-08-21 15:44:01 +0000565 register PyObject *stream = NULL; /* for PRINT opcodes */
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000566 register PyObject **fastlocals, **freevars;
Guido van Rossum014518f1998-11-23 21:09:51 +0000567 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000568 PyThreadState *tstate = PyThreadState_GET();
Tim Peters5ca576e2001-06-18 22:08:13 +0000569 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000570
571 /* when tracing we set things up so that
572
573 not (instr_lb <= current_bytecode_offset < instr_ub)
574
575 is true when the line being executed has changed. The
576 initial values are such as to make this false the first
577 time it is tested. */
578 int instr_ub = -1, instr_lb = 0;
579
Guido van Rossumd076c731998-10-07 19:42:25 +0000580 unsigned char *first_instr;
Skip Montanaro04d80f82002-08-04 21:03:35 +0000581 PyObject *names;
582 PyObject *consts;
Guido van Rossum96a42c81992-01-12 02:29:51 +0000583#ifdef LLTRACE
Guido van Rossumacbe8da1993-04-15 15:33:52 +0000584 int lltrace;
Guido van Rossum374a9221991-04-04 10:40:29 +0000585#endif
Guido van Rossum408027e1996-12-30 16:17:54 +0000586#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000587 /* Make it easier to find out where we are with a debugger */
Tim Peters5ca576e2001-06-18 22:08:13 +0000588 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000589#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000590
Neal Norwitza81d2202002-07-14 00:27:26 +0000591/* Tuple access macros */
592
593#ifndef Py_DEBUG
594#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
595#else
596#define GETITEM(v, i) PyTuple_GetItem((v), (i))
597#endif
598
Guido van Rossum374a9221991-04-04 10:40:29 +0000599/* Code access macros */
600
Guido van Rossumd076c731998-10-07 19:42:25 +0000601#define INSTR_OFFSET() (next_instr - first_instr)
Guido van Rossum374a9221991-04-04 10:40:29 +0000602#define NEXTOP() (*next_instr++)
603#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Guido van Rossumd076c731998-10-07 19:42:25 +0000604#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000605#define JUMPBY(x) (next_instr += (x))
606
Raymond Hettingerf606f872003-03-16 03:11:04 +0000607/* OpCode prediction macros
608 Some opcodes tend to come in pairs thus making it possible to predict
609 the second code when the first is run. For example, COMPARE_OP is often
610 followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And, those opcodes are often
611 followed by a POP_TOP.
612
613 Verifying the prediction costs a single high-speed test of register
Raymond Hettingerac2072922003-03-16 15:41:11 +0000614 variable against a constant. If the pairing was good, then the
Raymond Hettingerf606f872003-03-16 03:11:04 +0000615 processor has a high likelihood of making its own successful branch
616 prediction which results in a nearly zero overhead transition to the
617 next opcode.
618
619 A successful prediction saves a trip through the eval-loop including
620 its two unpredictable branches, the HASARG test and the switch-case.
621*/
622
Raymond Hettingerac2072922003-03-16 15:41:11 +0000623#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingerf606f872003-03-16 03:11:04 +0000624#define PREDICTED(op) PRED_##op: next_instr++
Raymond Hettinger7dc52212003-03-16 20:14:44 +0000625#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = (next_instr[2]<<8) + \
626 next_instr[1]; next_instr += 3
Raymond Hettingerf606f872003-03-16 03:11:04 +0000627
Guido van Rossum374a9221991-04-04 10:40:29 +0000628/* Stack manipulation macros */
629
630#define STACK_LEVEL() (stack_pointer - f->f_valuestack)
631#define EMPTY() (STACK_LEVEL() == 0)
632#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000633#define SECOND() (stack_pointer[-2])
634#define THIRD() (stack_pointer[-3])
635#define FOURTH() (stack_pointer[-4])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000636#define SET_TOP(v) (stack_pointer[-1] = (v))
637#define SET_SECOND(v) (stack_pointer[-2] = (v))
638#define SET_THIRD(v) (stack_pointer[-3] = (v))
639#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000640#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000641#define BASIC_PUSH(v) (*stack_pointer++ = (v))
642#define BASIC_POP() (*--stack_pointer)
643
Guido van Rossum96a42c81992-01-12 02:29:51 +0000644#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000645#define PUSH(v) { (void)(BASIC_PUSH(v), \
646 lltrace && prtrace(TOP(), "push")); \
647 assert(STACK_LEVEL() <= f->f_stacksize); }
Fred Drakede26cfc2001-10-13 06:11:28 +0000648#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000649#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
650 lltrace && prtrace(TOP(), "stackadj")); \
651 assert(STACK_LEVEL() <= f->f_stacksize); }
Guido van Rossum374a9221991-04-04 10:40:29 +0000652#else
653#define PUSH(v) BASIC_PUSH(v)
654#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000655#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000656#endif
657
Guido van Rossum681d79a1995-07-18 14:51:37 +0000658/* Local variable macros */
659
660#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000661
662/* The SETLOCAL() macro must not DECREF the local variable in-place and
663 then store the new value; it must copy the old value to a temporary
664 value, then store the new value, and then DECREF the temporary value.
665 This is because it is possible that during the DECREF the frame is
666 accessed by other code (e.g. a __del__ method or gc.collect()) and the
667 variable would be pointing to already-freed memory. */
668#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
669 GETLOCAL(i) = value; \
670 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000671
Guido van Rossuma027efa1997-05-05 20:56:21 +0000672/* Start of code */
673
Tim Peters5ca576e2001-06-18 22:08:13 +0000674 if (f == NULL)
675 return NULL;
676
Guido van Rossum8861b741996-07-30 16:49:37 +0000677#ifdef USE_STACKCHECK
Guido van Rossuma027efa1997-05-05 20:56:21 +0000678 if (tstate->recursion_depth%10 == 0 && PyOS_CheckStack()) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000679 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
Guido van Rossum8861b741996-07-30 16:49:37 +0000680 return NULL;
681 }
682#endif
683
Tim Peters5ca576e2001-06-18 22:08:13 +0000684 /* push frame */
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000685 if (++tstate->recursion_depth > recursion_limit) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000686 --tstate->recursion_depth;
687 PyErr_SetString(PyExc_RuntimeError,
Fred Drake661ea262000-10-24 19:57:45 +0000688 "maximum recursion depth exceeded");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000689 tstate->frame = f->f_back;
Guido van Rossum8861b741996-07-30 16:49:37 +0000690 return NULL;
691 }
692
Tim Peters5ca576e2001-06-18 22:08:13 +0000693 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000694
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000695 if (tstate->use_tracing) {
696 if (tstate->c_tracefunc != NULL) {
697 /* tstate->c_tracefunc, if defined, is a
698 function that will be called on *every* entry
699 to a code block. Its return value, if not
700 None, is a function that will be called at
701 the start of each executed line of code.
702 (Actually, the function must return itself
703 in order to continue tracing.) The trace
704 functions are called with three arguments:
705 a pointer to the current frame, a string
706 indicating why the function is called, and
707 an argument which depends on the situation.
708 The global trace function is also called
709 whenever an exception is detected. */
710 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
711 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000712 /* Trace function raised an error */
Michael W. Hudsonfb4d6ec2002-10-02 13:13:45 +0000713 --tstate->recursion_depth;
714 tstate->frame = f->f_back;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000715 return NULL;
716 }
717 }
718 if (tstate->c_profilefunc != NULL) {
719 /* Similar for c_profilefunc, except it needn't
720 return itself and isn't called for "line" events */
721 if (call_trace(tstate->c_profilefunc,
722 tstate->c_profileobj,
723 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000724 /* Profile function raised an error */
Michael W. Hudsonfb4d6ec2002-10-02 13:13:45 +0000725 --tstate->recursion_depth;
726 tstate->frame = f->f_back;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000727 return NULL;
728 }
729 }
730 }
731
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000732 co = f->f_code;
733 names = co->co_names;
734 consts = co->co_consts;
735 fastlocals = f->f_localsplus;
736 freevars = f->f_localsplus + f->f_nlocals;
737 _PyCode_GETCODEPTR(co, &first_instr);
738 /* An explanation is in order for the next line.
739
740 f->f_lasti now refers to the index of the last instruction
741 executed. You might think this was obvious from the name, but
742 this wasn't always true before 2.3! PyFrame_New now sets
743 f->f_lasti to -1 (i.e. the index *before* the first instruction)
744 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
745 does work. Promise. */
746 next_instr = first_instr + f->f_lasti + 1;
747 stack_pointer = f->f_stacktop;
748 assert(stack_pointer != NULL);
749 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
750
Tim Peters5ca576e2001-06-18 22:08:13 +0000751#ifdef LLTRACE
752 lltrace = PyDict_GetItemString(f->f_globals,"__lltrace__") != NULL;
753#endif
754#if defined(Py_DEBUG) || defined(LLTRACE)
755 filename = PyString_AsString(co->co_filename);
756#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000757
Guido van Rossum374a9221991-04-04 10:40:29 +0000758 why = WHY_NOT;
759 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +0000760 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +0000761 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +0000762
Guido van Rossum374a9221991-04-04 10:40:29 +0000763 for (;;) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000764 assert(stack_pointer >= f->f_valuestack); /* else underflow */
765 assert(STACK_LEVEL() <= f->f_stacksize); /* else overflow */
766
Guido van Rossuma027efa1997-05-05 20:56:21 +0000767 /* Do periodic things. Doing this every time through
768 the loop would add too much overhead, so we do it
769 only every Nth instruction. We also do it if
770 ``things_to_do'' is set, i.e. when an asynchronous
771 event needs attention (e.g. a signal handler or
772 async I/O handler); see Py_AddPendingCall() and
773 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000774
Skip Montanarod581d772002-09-03 20:10:45 +0000775 if (--_Py_Ticker < 0) {
776 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000777 tstate->tick_counter++;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000778 if (things_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +0000779 if (Py_MakePendingCalls() < 0) {
780 why = WHY_EXCEPTION;
781 goto on_error;
782 }
783 }
Guido van Rossumdf0d00e1997-05-20 15:57:49 +0000784#if !defined(HAVE_SIGNAL_H) || defined(macintosh)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000785 /* If we have true signals, the signal handler
786 will call Py_AddPendingCall() so we don't
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000787 have to call PyErr_CheckSignals(). On the
788 Mac and DOS, alas, we have to call it. */
Guido van Rossumb209a111997-04-29 18:18:01 +0000789 if (PyErr_CheckSignals()) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000790 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +0000791 goto on_error;
792 }
Guido van Rossum70d44781997-01-21 06:15:24 +0000793#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000794
Guido van Rossume59214e1994-08-30 08:01:59 +0000795#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000796 if (interpreter_lock) {
797 /* Give another thread a chance */
798
Guido van Rossum25ce5661997-08-02 03:10:38 +0000799 if (PyThreadState_Swap(NULL) != tstate)
800 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000801 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000802
803 /* Other threads may run now */
804
Guido van Rossum65d5b571998-12-21 19:32:43 +0000805 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000806 if (PyThreadState_Swap(tstate) != NULL)
807 Py_FatalError("ceval: orphan tstate");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000808 }
809#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000810 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000811
Neil Schemenauer63543862002-02-17 19:10:14 +0000812 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +0000813 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +0000814
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000815 /* line-by-line tracing support */
816
817 if (tstate->c_tracefunc != NULL && !tstate->tracing) {
818 /* see maybe_call_line_trace
819 for expository comments */
820 f->f_stacktop = stack_pointer;
Michael W. Hudson006c7522002-11-08 13:08:46 +0000821
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000822 err = maybe_call_line_trace(tstate->c_tracefunc,
823 tstate->c_traceobj,
824 f, &instr_lb, &instr_ub);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000825 /* Reload possibly changed frame fields */
826 JUMPTO(f->f_lasti);
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000827 if (f->f_stacktop != NULL) {
828 stack_pointer = f->f_stacktop;
829 f->f_stacktop = NULL;
830 }
831 if (err) {
832 /* trace function raised an exception */
833 goto on_error;
834 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000835 }
836
837 /* Extract opcode and argument */
838
Guido van Rossum374a9221991-04-04 10:40:29 +0000839 opcode = NEXTOP();
840 if (HAS_ARG(opcode))
841 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +0000842 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +0000843#ifdef DYNAMIC_EXECUTION_PROFILE
844#ifdef DXPAIRS
845 dxpairs[lastopcode][opcode]++;
846 lastopcode = opcode;
847#endif
848 dxp[opcode]++;
849#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000850
Guido van Rossum96a42c81992-01-12 02:29:51 +0000851#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +0000852 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +0000853
Guido van Rossum96a42c81992-01-12 02:29:51 +0000854 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000855 if (HAS_ARG(opcode)) {
856 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000857 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +0000858 }
859 else {
860 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000861 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +0000862 }
863 }
864#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000865
Guido van Rossum374a9221991-04-04 10:40:29 +0000866 /* Main switch on opcode */
Jeremy Hylton52820442001-01-03 23:52:36 +0000867
Guido van Rossum374a9221991-04-04 10:40:29 +0000868 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +0000869
Guido van Rossum374a9221991-04-04 10:40:29 +0000870 /* BEWARE!
871 It is essential that any operation that fails sets either
872 x to NULL, err to nonzero, or why to anything but WHY_NOT,
873 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000874
Guido van Rossum374a9221991-04-04 10:40:29 +0000875 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000876
Neil Schemenauer63543862002-02-17 19:10:14 +0000877 case LOAD_FAST:
878 x = GETLOCAL(oparg);
879 if (x != NULL) {
880 Py_INCREF(x);
881 PUSH(x);
882 goto fast_next_opcode;
883 }
884 format_exc_check_arg(PyExc_UnboundLocalError,
885 UNBOUNDLOCAL_ERROR_MSG,
886 PyTuple_GetItem(co->co_varnames, oparg));
887 break;
888
889 case LOAD_CONST:
Skip Montanaro04d80f82002-08-04 21:03:35 +0000890 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +0000891 Py_INCREF(x);
892 PUSH(x);
893 goto fast_next_opcode;
894
Raymond Hettinger7dc52212003-03-16 20:14:44 +0000895 PREDICTED_WITH_ARG(STORE_FAST);
Neil Schemenauer63543862002-02-17 19:10:14 +0000896 case STORE_FAST:
897 v = POP();
898 SETLOCAL(oparg, v);
899 goto fast_next_opcode;
900
Raymond Hettingerf606f872003-03-16 03:11:04 +0000901 PREDICTED(POP_TOP);
Guido van Rossum374a9221991-04-04 10:40:29 +0000902 case POP_TOP:
903 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000904 Py_DECREF(v);
Neil Schemenauer63543862002-02-17 19:10:14 +0000905 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000906
Guido van Rossum374a9221991-04-04 10:40:29 +0000907 case ROT_TWO:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000908 v = TOP();
909 w = SECOND();
910 SET_TOP(w);
911 SET_SECOND(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000912 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000913
Guido van Rossum374a9221991-04-04 10:40:29 +0000914 case ROT_THREE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000915 v = TOP();
916 w = SECOND();
917 x = THIRD();
918 SET_TOP(w);
919 SET_SECOND(x);
920 SET_THIRD(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000921 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000922
Thomas Wouters434d0822000-08-24 20:11:32 +0000923 case ROT_FOUR:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000924 u = TOP();
925 v = SECOND();
926 w = THIRD();
927 x = FOURTH();
928 SET_TOP(v);
929 SET_SECOND(w);
930 SET_THIRD(x);
931 SET_FOURTH(u);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000932 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000933
Guido van Rossum374a9221991-04-04 10:40:29 +0000934 case DUP_TOP:
935 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000936 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000937 PUSH(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000938 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000939
Thomas Wouters434d0822000-08-24 20:11:32 +0000940 case DUP_TOPX:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000941 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000942 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000943 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000944 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000945 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000946 STACKADJ(2);
947 SET_TOP(x);
948 SET_SECOND(w);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000949 goto fast_next_opcode;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000950 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000951 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000952 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000953 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000954 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000955 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +0000956 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000957 STACKADJ(3);
958 SET_TOP(x);
959 SET_SECOND(w);
960 SET_THIRD(v);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000961 goto fast_next_opcode;
Thomas Wouters434d0822000-08-24 20:11:32 +0000962 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000963 Py_FatalError("invalid argument to DUP_TOPX"
964 " (bytecode corruption?)");
Tim Peters35ba6892000-10-11 07:04:49 +0000965 break;
Thomas Wouters434d0822000-08-24 20:11:32 +0000966
Guido van Rossum374a9221991-04-04 10:40:29 +0000967 case UNARY_POSITIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000968 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000969 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000970 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000971 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000972 if (x != NULL) continue;
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_NEGATIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000976 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000977 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000978 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 Rossum374a9221991-04-04 10:40:29 +0000983 case UNARY_NOT:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000984 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000985 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000986 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +0000987 if (err == 0) {
988 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000989 SET_TOP(Py_True);
Guido van Rossumfc490731997-05-06 15:06:49 +0000990 continue;
991 }
992 else if (err > 0) {
993 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000994 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +0000995 err = 0;
996 continue;
997 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +0000998 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +0000999 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001000
Guido van Rossum374a9221991-04-04 10:40:29 +00001001 case UNARY_CONVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001002 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001003 x = PyObject_Repr(v);
1004 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001005 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001006 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001007 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001008
Guido van Rossum7928cd71991-10-24 14:59:31 +00001009 case UNARY_INVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001010 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001011 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001012 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001013 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001014 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001015 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001016
Guido van Rossum50564e81996-01-12 01:13:16 +00001017 case BINARY_POWER:
1018 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001019 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001020 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +00001021 Py_DECREF(v);
1022 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001023 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001024 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +00001025 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001026
Guido van Rossum374a9221991-04-04 10:40:29 +00001027 case BINARY_MULTIPLY:
1028 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001029 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001030 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001031 Py_DECREF(v);
1032 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001033 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001034 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001035 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001036
Guido van Rossum374a9221991-04-04 10:40:29 +00001037 case BINARY_DIVIDE:
Tim Peters3caca232001-12-06 06:23:26 +00001038 if (!_Py_QnewFlag) {
1039 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001040 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001041 x = PyNumber_Divide(v, w);
1042 Py_DECREF(v);
1043 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001044 SET_TOP(x);
Tim Peters3caca232001-12-06 06:23:26 +00001045 if (x != NULL) continue;
1046 break;
1047 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001048 /* -Qnew is in effect: fall through to
Tim Peters3caca232001-12-06 06:23:26 +00001049 BINARY_TRUE_DIVIDE */
1050 case BINARY_TRUE_DIVIDE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001051 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001052 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001053 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001054 Py_DECREF(v);
1055 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001056 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001057 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001058 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001059
Guido van Rossum4668b002001-08-08 05:00:18 +00001060 case BINARY_FLOOR_DIVIDE:
1061 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001062 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001063 x = PyNumber_FloorDivide(v, w);
1064 Py_DECREF(v);
1065 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001066 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001067 if (x != NULL) continue;
1068 break;
1069
Guido van Rossum374a9221991-04-04 10:40:29 +00001070 case BINARY_MODULO:
1071 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001072 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001073 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001074 Py_DECREF(v);
1075 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001076 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001077 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001078 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001079
Guido van Rossum374a9221991-04-04 10:40:29 +00001080 case BINARY_ADD:
1081 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001082 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001083 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001084 /* INLINE: int + int */
1085 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001086 a = PyInt_AS_LONG(v);
1087 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001088 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001089 if ((i^a) < 0 && (i^b) < 0)
1090 goto slow_add;
1091 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001092 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001093 else {
1094 slow_add:
Guido van Rossumc12da691997-07-17 23:12:42 +00001095 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001096 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001097 Py_DECREF(v);
1098 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001099 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001100 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001101 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001102
Guido van Rossum374a9221991-04-04 10:40:29 +00001103 case BINARY_SUBTRACT:
1104 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001105 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001106 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001107 /* INLINE: int - int */
1108 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001109 a = PyInt_AS_LONG(v);
1110 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001111 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001112 if ((i^a) < 0 && (i^~b) < 0)
1113 goto slow_sub;
1114 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001115 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001116 else {
1117 slow_sub:
Guido van Rossumc12da691997-07-17 23:12:42 +00001118 x = PyNumber_Subtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001119 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001120 Py_DECREF(v);
1121 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001122 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001123 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001124 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001125
Guido van Rossum374a9221991-04-04 10:40:29 +00001126 case BINARY_SUBSCR:
1127 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001128 v = TOP();
Tim Petersb1c46982001-10-05 20:41:38 +00001129 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001130 /* INLINE: list[int] */
1131 long i = PyInt_AsLong(w);
1132 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +00001133 i += PyList_GET_SIZE(v);
Guido van Rossumc12da691997-07-17 23:12:42 +00001134 if (i < 0 ||
Guido van Rossumfa00e951998-07-08 15:02:37 +00001135 i >= PyList_GET_SIZE(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001136 PyErr_SetString(PyExc_IndexError,
1137 "list index out of range");
1138 x = NULL;
1139 }
1140 else {
Guido van Rossumfa00e951998-07-08 15:02:37 +00001141 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001142 Py_INCREF(x);
1143 }
1144 }
1145 else
1146 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001147 Py_DECREF(v);
1148 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001149 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001150 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001151 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001152
Guido van Rossum7928cd71991-10-24 14:59:31 +00001153 case BINARY_LSHIFT:
1154 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001155 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001156 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001157 Py_DECREF(v);
1158 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001159 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001160 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001161 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001162
Guido van Rossum7928cd71991-10-24 14:59:31 +00001163 case BINARY_RSHIFT:
1164 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001165 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001166 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001167 Py_DECREF(v);
1168 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001169 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001170 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001171 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001172
Guido van Rossum7928cd71991-10-24 14:59:31 +00001173 case BINARY_AND:
1174 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001175 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001176 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001177 Py_DECREF(v);
1178 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001179 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001180 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001181 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001182
Guido van Rossum7928cd71991-10-24 14:59:31 +00001183 case BINARY_XOR:
1184 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001185 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001186 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001187 Py_DECREF(v);
1188 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001189 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001190 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001191 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001192
Guido van Rossum7928cd71991-10-24 14:59:31 +00001193 case BINARY_OR:
1194 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001195 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001196 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001197 Py_DECREF(v);
1198 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001199 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001200 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001201 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001202
1203 case INPLACE_POWER:
1204 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001205 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001206 x = PyNumber_InPlacePower(v, w, Py_None);
1207 Py_DECREF(v);
1208 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001209 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001210 if (x != NULL) continue;
1211 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001212
Thomas Wouters434d0822000-08-24 20:11:32 +00001213 case INPLACE_MULTIPLY:
1214 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001215 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001216 x = PyNumber_InPlaceMultiply(v, w);
1217 Py_DECREF(v);
1218 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001219 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001220 if (x != NULL) continue;
1221 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001222
Thomas Wouters434d0822000-08-24 20:11:32 +00001223 case INPLACE_DIVIDE:
Tim Peters54b11912001-12-25 18:49:11 +00001224 if (!_Py_QnewFlag) {
1225 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001226 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001227 x = PyNumber_InPlaceDivide(v, w);
1228 Py_DECREF(v);
1229 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001230 SET_TOP(x);
Tim Peters54b11912001-12-25 18:49:11 +00001231 if (x != NULL) continue;
1232 break;
1233 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001234 /* -Qnew is in effect: fall through to
Tim Peters54b11912001-12-25 18:49:11 +00001235 INPLACE_TRUE_DIVIDE */
1236 case INPLACE_TRUE_DIVIDE:
Thomas Wouters434d0822000-08-24 20:11:32 +00001237 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001238 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001239 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001240 Py_DECREF(v);
1241 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001242 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001243 if (x != NULL) continue;
1244 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001245
Guido van Rossum4668b002001-08-08 05:00:18 +00001246 case INPLACE_FLOOR_DIVIDE:
1247 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001248 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001249 x = PyNumber_InPlaceFloorDivide(v, w);
1250 Py_DECREF(v);
1251 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001252 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001253 if (x != NULL) continue;
1254 break;
1255
Thomas Wouters434d0822000-08-24 20:11:32 +00001256 case INPLACE_MODULO:
1257 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001258 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001259 x = PyNumber_InPlaceRemainder(v, w);
1260 Py_DECREF(v);
1261 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001262 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001263 if (x != NULL) continue;
1264 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001265
Thomas Wouters434d0822000-08-24 20:11:32 +00001266 case INPLACE_ADD:
1267 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001268 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001269 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001270 /* INLINE: int + int */
1271 register long a, b, i;
1272 a = PyInt_AS_LONG(v);
1273 b = PyInt_AS_LONG(w);
1274 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001275 if ((i^a) < 0 && (i^b) < 0)
1276 goto slow_iadd;
1277 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001278 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001279 else {
1280 slow_iadd:
Thomas Wouters434d0822000-08-24 20:11:32 +00001281 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001282 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001283 Py_DECREF(v);
1284 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001285 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001286 if (x != NULL) continue;
1287 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001288
Thomas Wouters434d0822000-08-24 20:11:32 +00001289 case INPLACE_SUBTRACT:
1290 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001291 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001292 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001293 /* INLINE: int - int */
1294 register long a, b, i;
1295 a = PyInt_AS_LONG(v);
1296 b = PyInt_AS_LONG(w);
1297 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001298 if ((i^a) < 0 && (i^~b) < 0)
1299 goto slow_isub;
1300 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001301 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001302 else {
1303 slow_isub:
Thomas Wouters434d0822000-08-24 20:11:32 +00001304 x = PyNumber_InPlaceSubtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001305 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001306 Py_DECREF(v);
1307 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001308 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001309 if (x != NULL) continue;
1310 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001311
Thomas Wouters434d0822000-08-24 20:11:32 +00001312 case INPLACE_LSHIFT:
1313 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001314 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001315 x = PyNumber_InPlaceLshift(v, w);
1316 Py_DECREF(v);
1317 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001318 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001319 if (x != NULL) continue;
1320 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001321
Thomas Wouters434d0822000-08-24 20:11:32 +00001322 case INPLACE_RSHIFT:
1323 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001324 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001325 x = PyNumber_InPlaceRshift(v, w);
1326 Py_DECREF(v);
1327 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001328 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001329 if (x != NULL) continue;
1330 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001331
Thomas Wouters434d0822000-08-24 20:11:32 +00001332 case INPLACE_AND:
1333 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001334 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001335 x = PyNumber_InPlaceAnd(v, w);
1336 Py_DECREF(v);
1337 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001338 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001339 if (x != NULL) continue;
1340 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001341
Thomas Wouters434d0822000-08-24 20:11:32 +00001342 case INPLACE_XOR:
1343 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001344 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001345 x = PyNumber_InPlaceXor(v, w);
1346 Py_DECREF(v);
1347 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001348 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001349 if (x != NULL) continue;
1350 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001351
Thomas Wouters434d0822000-08-24 20:11:32 +00001352 case INPLACE_OR:
1353 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001354 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001355 x = PyNumber_InPlaceOr(v, w);
1356 Py_DECREF(v);
1357 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001358 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001359 if (x != NULL) continue;
1360 break;
1361
Guido van Rossum374a9221991-04-04 10:40:29 +00001362 case SLICE+0:
1363 case SLICE+1:
1364 case SLICE+2:
1365 case SLICE+3:
1366 if ((opcode-SLICE) & 2)
1367 w = POP();
1368 else
1369 w = NULL;
1370 if ((opcode-SLICE) & 1)
1371 v = POP();
1372 else
1373 v = NULL;
Raymond Hettinger663004b2003-01-09 15:24:30 +00001374 u = TOP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001375 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001376 Py_DECREF(u);
1377 Py_XDECREF(v);
1378 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001379 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001380 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001381 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001382
Guido van Rossum374a9221991-04-04 10:40:29 +00001383 case STORE_SLICE+0:
1384 case STORE_SLICE+1:
1385 case STORE_SLICE+2:
1386 case STORE_SLICE+3:
1387 if ((opcode-STORE_SLICE) & 2)
1388 w = POP();
1389 else
1390 w = NULL;
1391 if ((opcode-STORE_SLICE) & 1)
1392 v = POP();
1393 else
1394 v = NULL;
1395 u = POP();
1396 t = POP();
1397 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +00001398 Py_DECREF(t);
1399 Py_DECREF(u);
1400 Py_XDECREF(v);
1401 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001402 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001403 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001404
Guido van Rossum374a9221991-04-04 10:40:29 +00001405 case DELETE_SLICE+0:
1406 case DELETE_SLICE+1:
1407 case DELETE_SLICE+2:
1408 case DELETE_SLICE+3:
1409 if ((opcode-DELETE_SLICE) & 2)
1410 w = POP();
1411 else
1412 w = NULL;
1413 if ((opcode-DELETE_SLICE) & 1)
1414 v = POP();
1415 else
1416 v = NULL;
1417 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001418 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +00001419 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00001420 Py_DECREF(u);
1421 Py_XDECREF(v);
1422 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001423 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001424 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001425
Guido van Rossum374a9221991-04-04 10:40:29 +00001426 case STORE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001427 w = TOP();
1428 v = SECOND();
1429 u = THIRD();
1430 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001431 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001432 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001433 Py_DECREF(u);
1434 Py_DECREF(v);
1435 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001436 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001437 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001438
Guido van Rossum374a9221991-04-04 10:40:29 +00001439 case DELETE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001440 w = TOP();
1441 v = SECOND();
1442 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001443 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001444 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001445 Py_DECREF(v);
1446 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001447 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001448 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001449
Guido van Rossum374a9221991-04-04 10:40:29 +00001450 case PRINT_EXPR:
1451 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001452 w = PySys_GetObject("displayhook");
1453 if (w == NULL) {
1454 PyErr_SetString(PyExc_RuntimeError,
1455 "lost sys.displayhook");
1456 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001457 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001458 }
1459 if (err == 0) {
1460 x = Py_BuildValue("(O)", v);
1461 if (x == NULL)
1462 err = -1;
1463 }
1464 if (err == 0) {
1465 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001466 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001467 if (w == NULL)
1468 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001469 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001470 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001471 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001472 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001473
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001474 case PRINT_ITEM_TO:
1475 w = stream = POP();
1476 /* fall through to PRINT_ITEM */
1477
Guido van Rossum374a9221991-04-04 10:40:29 +00001478 case PRINT_ITEM:
1479 v = POP();
Barry Warsaw093abe02000-08-29 04:56:13 +00001480 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001481 w = PySys_GetObject("stdout");
1482 if (w == NULL) {
1483 PyErr_SetString(PyExc_RuntimeError,
1484 "lost sys.stdout");
1485 err = -1;
1486 }
Guido van Rossum8f183201997-12-31 05:53:15 +00001487 }
Tim Peters8e5fd532002-03-24 19:25:00 +00001488 if (w != NULL && PyFile_SoftSpace(w, 0))
Guido van Rossumbe270261997-05-22 22:26:18 +00001489 err = PyFile_WriteString(" ", w);
1490 if (err == 0)
1491 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001492 if (err == 0) {
Tim Peters8e5fd532002-03-24 19:25:00 +00001493 /* XXX move into writeobject() ? */
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001494 if (PyString_Check(v)) {
1495 char *s = PyString_AS_STRING(v);
1496 int len = PyString_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001497 if (len == 0 ||
1498 !isspace(Py_CHARMASK(s[len-1])) ||
1499 s[len-1] == ' ')
1500 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001501 }
Martin v. Löwis8d3ce5a2001-12-18 22:36:40 +00001502#ifdef Py_USING_UNICODE
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001503 else if (PyUnicode_Check(v)) {
1504 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
1505 int len = PyUnicode_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001506 if (len == 0 ||
1507 !Py_UNICODE_ISSPACE(s[len-1]) ||
1508 s[len-1] == ' ')
1509 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001510 }
Michael W. Hudsond95c8282002-05-20 13:56:11 +00001511#endif
Tim Peters8e5fd532002-03-24 19:25:00 +00001512 else
1513 PyFile_SoftSpace(w, 1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001514 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001515 Py_DECREF(v);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001516 Py_XDECREF(stream);
1517 stream = NULL;
1518 if (err == 0)
1519 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001520 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001521
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001522 case PRINT_NEWLINE_TO:
1523 w = stream = POP();
1524 /* fall through to PRINT_NEWLINE */
1525
Guido van Rossum374a9221991-04-04 10:40:29 +00001526 case PRINT_NEWLINE:
Barry Warsaw093abe02000-08-29 04:56:13 +00001527 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001528 w = PySys_GetObject("stdout");
1529 if (w == NULL)
1530 PyErr_SetString(PyExc_RuntimeError,
1531 "lost sys.stdout");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001532 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001533 if (w != NULL) {
1534 err = PyFile_WriteString("\n", w);
1535 if (err == 0)
1536 PyFile_SoftSpace(w, 0);
1537 }
1538 Py_XDECREF(stream);
1539 stream = NULL;
Guido van Rossum374a9221991-04-04 10:40:29 +00001540 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001541
Thomas Wouters434d0822000-08-24 20:11:32 +00001542
1543#ifdef CASE_TOO_BIG
1544 default: switch (opcode) {
1545#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001546 case BREAK_LOOP:
1547 why = WHY_BREAK;
1548 break;
Guido van Rossum66b0e9c2001-03-21 19:17:22 +00001549
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00001550 case CONTINUE_LOOP:
1551 retval = PyInt_FromLong(oparg);
1552 why = WHY_CONTINUE;
1553 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001554
Guido van Rossumf10570b1995-07-07 22:53:21 +00001555 case RAISE_VARARGS:
1556 u = v = w = NULL;
1557 switch (oparg) {
1558 case 3:
1559 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001560 /* Fallthrough */
1561 case 2:
1562 v = POP(); /* value */
1563 /* Fallthrough */
1564 case 1:
1565 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001566 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001567 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001568 break;
1569 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001570 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001571 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001572 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001573 break;
1574 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001575 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001576
Guido van Rossum374a9221991-04-04 10:40:29 +00001577 case LOAD_LOCALS:
Guido van Rossum681d79a1995-07-18 14:51:37 +00001578 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001579 PyErr_SetString(PyExc_SystemError,
1580 "no locals");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001581 break;
1582 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001583 Py_INCREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001584 PUSH(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001585 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001586
Guido van Rossum374a9221991-04-04 10:40:29 +00001587 case RETURN_VALUE:
1588 retval = POP();
1589 why = WHY_RETURN;
1590 break;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001591
Tim Peters5ca576e2001-06-18 22:08:13 +00001592 case YIELD_VALUE:
1593 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001594 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001595 why = WHY_YIELD;
1596 break;
1597
1598
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001599 case EXEC_STMT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001600 w = TOP();
1601 v = SECOND();
1602 u = THIRD();
1603 STACKADJ(-3);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001604 err = exec_statement(f, u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001605 Py_DECREF(u);
1606 Py_DECREF(v);
1607 Py_DECREF(w);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001608 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001609
Guido van Rossum374a9221991-04-04 10:40:29 +00001610 case POP_BLOCK:
1611 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001612 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001613 while (STACK_LEVEL() > b->b_level) {
1614 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001615 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001616 }
1617 }
1618 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001619
Guido van Rossum374a9221991-04-04 10:40:29 +00001620 case END_FINALLY:
1621 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001622 if (PyInt_Check(v)) {
Raymond Hettinger080cb322003-03-14 01:37:42 +00001623 why = (enum why_code) PyInt_AS_LONG(v);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00001624 if (why == WHY_RETURN ||
Tim Peters5ca576e2001-06-18 22:08:13 +00001625 why == WHY_YIELD ||
Guido van Rossumc5fe5eb2002-06-12 03:45:21 +00001626 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001627 retval = POP();
1628 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001629 else if (PyString_Check(v) || PyClass_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001630 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001631 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001632 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001633 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001634 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001635 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001636 else if (v != Py_None) {
1637 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001638 "'finally' pops bad exception");
1639 why = WHY_EXCEPTION;
1640 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001641 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001642 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001643
Guido van Rossum374a9221991-04-04 10:40:29 +00001644 case BUILD_CLASS:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001645 u = TOP();
1646 v = SECOND();
1647 w = THIRD();
1648 STACKADJ(-2);
Guido van Rossum25831651993-05-19 14:50:45 +00001649 x = build_class(u, v, w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001650 SET_TOP(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001651 Py_DECREF(u);
1652 Py_DECREF(v);
1653 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001654 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001655
Guido van Rossum374a9221991-04-04 10:40:29 +00001656 case STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001657 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001658 v = POP();
Guido van Rossum681d79a1995-07-18 14:51:37 +00001659 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001660 PyErr_Format(PyExc_SystemError,
1661 "no locals found when storing %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001662 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001663 break;
1664 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001665 err = PyDict_SetItem(x, w, v);
1666 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001667 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001668
Guido van Rossum374a9221991-04-04 10:40:29 +00001669 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001670 w = GETITEM(names, oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001671 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001672 PyErr_Format(PyExc_SystemError,
1673 "no locals when deleting %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001674 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001675 break;
1676 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001677 if ((err = PyDict_DelItem(x, w)) != 0)
Guido van Rossumac7be682001-01-17 15:42:30 +00001678 format_exc_check_arg(PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001679 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001680 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001681
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001682 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001683 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001684 v = POP();
Raymond Hettinger21012b82003-02-26 18:11:50 +00001685 if (PyTuple_CheckExact(v)) {
Barry Warsawe42b18f1997-08-25 22:13:04 +00001686 if (PyTuple_Size(v) != oparg) {
1687 PyErr_SetString(PyExc_ValueError,
1688 "unpack tuple of wrong size");
1689 why = WHY_EXCEPTION;
1690 }
1691 else {
1692 for (; --oparg >= 0; ) {
1693 w = PyTuple_GET_ITEM(v, oparg);
1694 Py_INCREF(w);
1695 PUSH(w);
1696 }
1697 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001698 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00001699 else if (PyList_CheckExact(v)) {
Barry Warsawe42b18f1997-08-25 22:13:04 +00001700 if (PyList_Size(v) != oparg) {
1701 PyErr_SetString(PyExc_ValueError,
1702 "unpack list of wrong size");
1703 why = WHY_EXCEPTION;
1704 }
1705 else {
1706 for (; --oparg >= 0; ) {
1707 w = PyList_GET_ITEM(v, oparg);
1708 Py_INCREF(w);
1709 PUSH(w);
1710 }
1711 }
1712 }
Tim Petersd6d010b2001-06-21 02:49:55 +00001713 else if (unpack_iterable(v, oparg,
1714 stack_pointer + oparg))
1715 stack_pointer += oparg;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001716 else {
1717 if (PyErr_ExceptionMatches(PyExc_TypeError))
1718 PyErr_SetString(PyExc_TypeError,
1719 "unpack non-sequence");
Barry Warsawe42b18f1997-08-25 22:13:04 +00001720 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001721 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001722 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001723 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001724
Guido van Rossum374a9221991-04-04 10:40:29 +00001725 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001726 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001727 v = TOP();
1728 u = SECOND();
1729 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001730 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1731 Py_DECREF(v);
1732 Py_DECREF(u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001733 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001734
Guido van Rossum374a9221991-04-04 10:40:29 +00001735 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001736 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001737 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001738 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1739 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001740 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001741 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001742
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001743 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001744 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001745 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001746 err = PyDict_SetItem(f->f_globals, w, v);
1747 Py_DECREF(v);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001748 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001749
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001750 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001751 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001752 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001753 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001754 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001755 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001756
Guido van Rossum374a9221991-04-04 10:40:29 +00001757 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001758 w = GETITEM(names, oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001759 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001760 PyErr_Format(PyExc_SystemError,
1761 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001762 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001763 break;
1764 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001765 x = PyDict_GetItem(x, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001766 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001767 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001768 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001769 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001770 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001771 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001772 PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001773 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001774 break;
1775 }
1776 }
1777 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001778 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001779 PUSH(x);
1780 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001781
Guido van Rossum374a9221991-04-04 10:40:29 +00001782 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001783 w = GETITEM(names, oparg);
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001784 if (PyString_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00001785 /* Inline the PyDict_GetItem() calls.
1786 WARNING: this is an extreme speed hack.
1787 Do not try this at home. */
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001788 long hash = ((PyStringObject *)w)->ob_shash;
1789 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001790 PyDictObject *d;
1791 d = (PyDictObject *)(f->f_globals);
1792 x = d->ma_lookup(d, w, hash)->me_value;
1793 if (x != NULL) {
1794 Py_INCREF(x);
1795 PUSH(x);
1796 continue;
1797 }
1798 d = (PyDictObject *)(f->f_builtins);
1799 x = d->ma_lookup(d, w, hash)->me_value;
1800 if (x != NULL) {
1801 Py_INCREF(x);
1802 PUSH(x);
1803 continue;
1804 }
1805 goto load_global_error;
1806 }
1807 }
1808 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00001809 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001810 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001811 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001812 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001813 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00001814 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001815 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001816 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001817 break;
1818 }
1819 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001820 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001821 PUSH(x);
1822 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001823
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001824 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001825 x = GETLOCAL(oparg);
1826 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001827 format_exc_check_arg(
1828 PyExc_UnboundLocalError,
1829 UNBOUNDLOCAL_ERROR_MSG,
1830 PyTuple_GetItem(co->co_varnames, oparg)
1831 );
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001832 break;
1833 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00001834 SETLOCAL(oparg, NULL);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001835 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001836
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001837 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001838 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001839 Py_INCREF(x);
1840 PUSH(x);
1841 break;
1842
1843 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001844 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001845 w = PyCell_Get(x);
Jeremy Hylton2524d692001-02-05 17:23:16 +00001846 if (w == NULL) {
Jeremy Hylton76c81ee2002-07-11 16:56:38 +00001847 err = -1;
1848 /* Don't stomp existing exception */
1849 if (PyErr_Occurred())
1850 break;
Jeremy Hyltonc76770c2001-04-13 16:51:46 +00001851 if (oparg < f->f_ncells) {
Jeremy Hylton2524d692001-02-05 17:23:16 +00001852 v = PyTuple_GetItem(co->co_cellvars,
1853 oparg);
Jeremy Hyltonc76770c2001-04-13 16:51:46 +00001854 format_exc_check_arg(
1855 PyExc_UnboundLocalError,
1856 UNBOUNDLOCAL_ERROR_MSG,
1857 v);
1858 } else {
Jeremy Hylton2524d692001-02-05 17:23:16 +00001859 v = PyTuple_GetItem(
1860 co->co_freevars,
1861 oparg - f->f_ncells);
Jeremy Hyltonc76770c2001-04-13 16:51:46 +00001862 format_exc_check_arg(
1863 PyExc_NameError,
1864 UNBOUNDFREE_ERROR_MSG,
1865 v);
1866 }
Jeremy Hylton2524d692001-02-05 17:23:16 +00001867 break;
1868 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001869 PUSH(w);
1870 break;
1871
1872 case STORE_DEREF:
1873 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001874 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001875 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00001876 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001877 continue;
1878
Guido van Rossum374a9221991-04-04 10:40:29 +00001879 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001880 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001881 if (x != NULL) {
1882 for (; --oparg >= 0;) {
1883 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001884 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001885 }
1886 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001887 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001888 }
1889 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001890
Guido van Rossum374a9221991-04-04 10:40:29 +00001891 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00001892 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001893 if (x != NULL) {
1894 for (; --oparg >= 0;) {
1895 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00001896 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001897 }
1898 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001899 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001900 }
1901 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001902
Guido van Rossum374a9221991-04-04 10:40:29 +00001903 case BUILD_MAP:
Guido van Rossumb209a111997-04-29 18:18:01 +00001904 x = PyDict_New();
Guido van Rossum374a9221991-04-04 10:40:29 +00001905 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001906 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001907 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001908
Guido van Rossum374a9221991-04-04 10:40:29 +00001909 case LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001910 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001911 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001912 x = PyObject_GetAttr(v, w);
1913 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001914 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001915 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001916 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001917
Guido van Rossum374a9221991-04-04 10:40:29 +00001918 case COMPARE_OP:
1919 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001920 v = TOP();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001921 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001922 /* INLINE: cmp(int, int) */
1923 register long a, b;
1924 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001925 a = PyInt_AS_LONG(v);
1926 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001927 switch (oparg) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00001928 case PyCmp_LT: res = a < b; break;
1929 case PyCmp_LE: res = a <= b; break;
1930 case PyCmp_EQ: res = a == b; break;
1931 case PyCmp_NE: res = a != b; break;
1932 case PyCmp_GT: res = a > b; break;
1933 case PyCmp_GE: res = a >= b; break;
1934 case PyCmp_IS: res = v == w; break;
1935 case PyCmp_IS_NOT: res = v != w; break;
Guido van Rossumc12da691997-07-17 23:12:42 +00001936 default: goto slow_compare;
1937 }
1938 x = res ? Py_True : Py_False;
1939 Py_INCREF(x);
1940 }
1941 else {
1942 slow_compare:
1943 x = cmp_outcome(oparg, v, w);
1944 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001945 Py_DECREF(v);
1946 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001947 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001948 if (x == NULL) break;
1949 PREDICT(JUMP_IF_FALSE);
1950 PREDICT(JUMP_IF_TRUE);
1951 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001952
Guido van Rossum374a9221991-04-04 10:40:29 +00001953 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001954 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001955 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001956 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001957 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00001958 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001959 break;
1960 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001961 u = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001962 w = Py_BuildValue("(OOOO)",
Guido van Rossum681d79a1995-07-18 14:51:37 +00001963 w,
1964 f->f_globals,
Guido van Rossuma027efa1997-05-05 20:56:21 +00001965 f->f_locals == NULL ?
1966 Py_None : f->f_locals,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001967 u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001968 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001969 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001970 u = POP();
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001971 x = NULL;
1972 break;
1973 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001974 x = PyEval_CallObject(x, w);
1975 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001976 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001977 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001978 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001979
Thomas Wouters52152252000-08-17 22:55:00 +00001980 case IMPORT_STAR:
1981 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001982 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001983 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001984 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001985 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001986 break;
1987 }
Thomas Wouters52152252000-08-17 22:55:00 +00001988 err = import_all_from(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001989 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00001990 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001991 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001992 break;
Guido van Rossum25831651993-05-19 14:50:45 +00001993
Thomas Wouters52152252000-08-17 22:55:00 +00001994 case IMPORT_FROM:
Skip Montanaro496e6582002-08-06 17:47:40 +00001995 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00001996 v = TOP();
1997 x = import_from(v, w);
1998 PUSH(x);
1999 if (x != NULL) continue;
2000 break;
2001
Guido van Rossum374a9221991-04-04 10:40:29 +00002002 case JUMP_FORWARD:
2003 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002004 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002005
Raymond Hettingerf606f872003-03-16 03:11:04 +00002006 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002007 case JUMP_IF_FALSE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002008 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002009 if (w == Py_True) {
2010 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002011 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002012 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002013 if (w == Py_False) {
2014 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002015 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002016 }
2017 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002018 if (err > 0)
2019 err = 0;
2020 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00002021 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002022 else
2023 break;
2024 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002025
Raymond Hettingerf606f872003-03-16 03:11:04 +00002026 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002027 case JUMP_IF_TRUE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002028 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002029 if (w == Py_False) {
2030 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002031 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002032 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002033 if (w == Py_True) {
2034 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002035 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002036 }
2037 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002038 if (err > 0) {
2039 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00002040 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002041 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002042 else if (err == 0)
2043 ;
2044 else
2045 break;
2046 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002047
Guido van Rossum374a9221991-04-04 10:40:29 +00002048 case JUMP_ABSOLUTE:
2049 JUMPTO(oparg);
Neil Schemenauerca2a2f12003-05-30 23:59:44 +00002050 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002051
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002052 case GET_ITER:
2053 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002054 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002055 x = PyObject_GetIter(v);
2056 Py_DECREF(v);
2057 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002058 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002059 PREDICT(FOR_ITER);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002060 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002061 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002062 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002063 break;
2064
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002065 PREDICTED_WITH_ARG(FOR_ITER);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002066 case FOR_ITER:
2067 /* before: [iter]; after: [iter, iter()] *or* [] */
2068 v = TOP();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002069 x = PyIter_Next(v);
2070 if (x != NULL) {
2071 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002072 PREDICT(STORE_FAST);
2073 PREDICT(UNPACK_SEQUENCE);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002074 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002075 }
Tim Petersf4848da2001-05-05 00:14:56 +00002076 if (!PyErr_Occurred()) {
2077 /* iterator ended normally */
2078 x = v = POP();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002079 Py_DECREF(v);
2080 JUMPBY(oparg);
2081 continue;
2082 }
2083 break;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002084
Guido van Rossum374a9221991-04-04 10:40:29 +00002085 case SETUP_LOOP:
2086 case SETUP_EXCEPT:
2087 case SETUP_FINALLY:
Guido van Rossumb209a111997-04-29 18:18:01 +00002088 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002089 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002090 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002091
Guido van Rossumf10570b1995-07-07 22:53:21 +00002092 case CALL_FUNCTION:
Jeremy Hylton985eba52003-02-05 23:13:00 +00002093 PCALL(PCALL_ALL);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002094 x = call_function(&stack_pointer, oparg);
2095 PUSH(x);
2096 if (x != NULL)
2097 continue;
2098 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002099
Jeremy Hylton76901512000-03-28 23:49:17 +00002100 case CALL_FUNCTION_VAR:
2101 case CALL_FUNCTION_KW:
2102 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002103 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002104 int na = oparg & 0xff;
2105 int nk = (oparg>>8) & 0xff;
2106 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002107 int n = na + 2 * nk;
2108 PyObject **pfunc, *func;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002109 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002110 if (flags & CALL_FLAG_VAR)
2111 n++;
2112 if (flags & CALL_FLAG_KW)
2113 n++;
2114 pfunc = stack_pointer - n - 1;
2115 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002116
Guido van Rossumac7be682001-01-17 15:42:30 +00002117 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002118 && PyMethod_GET_SELF(func) != NULL) {
2119 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002120 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002121 func = PyMethod_GET_FUNCTION(func);
2122 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002123 Py_DECREF(*pfunc);
2124 *pfunc = self;
2125 na++;
2126 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002127 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002128 Py_INCREF(func);
2129 x = ext_do_call(func, &stack_pointer, flags, na, nk);
Jeremy Hylton76901512000-03-28 23:49:17 +00002130 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002131
Jeremy Hylton76901512000-03-28 23:49:17 +00002132 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002133 w = POP();
2134 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002135 }
2136 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002137 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002138 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002139 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002140 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002141
Guido van Rossum681d79a1995-07-18 14:51:37 +00002142 case MAKE_FUNCTION:
2143 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002144 x = PyFunction_New(v, f->f_globals);
2145 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002146 /* XXX Maybe this should be a separate opcode? */
2147 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002148 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002149 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002150 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002151 x = NULL;
2152 break;
2153 }
2154 while (--oparg >= 0) {
2155 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002156 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002157 }
2158 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002159 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002160 }
2161 PUSH(x);
2162 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002163
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002164 case MAKE_CLOSURE:
2165 {
2166 int nfree;
2167 v = POP(); /* code object */
2168 x = PyFunction_New(v, f->f_globals);
Jeremy Hylton733c8932001-12-13 19:51:56 +00002169 nfree = PyCode_GetNumFree((PyCodeObject *)v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002170 Py_DECREF(v);
2171 /* XXX Maybe this should be a separate opcode? */
2172 if (x != NULL && nfree > 0) {
2173 v = PyTuple_New(nfree);
2174 if (v == NULL) {
2175 Py_DECREF(x);
2176 x = NULL;
2177 break;
2178 }
2179 while (--nfree >= 0) {
2180 w = POP();
2181 PyTuple_SET_ITEM(v, nfree, w);
2182 }
2183 err = PyFunction_SetClosure(x, v);
2184 Py_DECREF(v);
2185 }
2186 if (x != NULL && oparg > 0) {
2187 v = PyTuple_New(oparg);
2188 if (v == NULL) {
2189 Py_DECREF(x);
2190 x = NULL;
2191 break;
2192 }
2193 while (--oparg >= 0) {
2194 w = POP();
2195 PyTuple_SET_ITEM(v, oparg, w);
2196 }
2197 err = PyFunction_SetDefaults(x, v);
2198 Py_DECREF(v);
2199 }
2200 PUSH(x);
2201 break;
2202 }
2203
Guido van Rossum8861b741996-07-30 16:49:37 +00002204 case BUILD_SLICE:
2205 if (oparg == 3)
2206 w = POP();
2207 else
2208 w = NULL;
2209 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002210 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002211 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002212 Py_DECREF(u);
2213 Py_DECREF(v);
2214 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002215 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002216 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002217 break;
2218
Fred Drakeef8ace32000-08-24 00:32:09 +00002219 case EXTENDED_ARG:
2220 opcode = NEXTOP();
2221 oparg = oparg<<16 | NEXTARG();
2222 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002223
Guido van Rossum374a9221991-04-04 10:40:29 +00002224 default:
2225 fprintf(stderr,
2226 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002227 PyCode_Addr2Line(f->f_code, f->f_lasti),
2228 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002229 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002230 why = WHY_EXCEPTION;
2231 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002232
2233#ifdef CASE_TOO_BIG
2234 }
2235#endif
2236
Guido van Rossum374a9221991-04-04 10:40:29 +00002237 } /* switch */
2238
2239 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002240
Guido van Rossum374a9221991-04-04 10:40:29 +00002241 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002242
Guido van Rossum374a9221991-04-04 10:40:29 +00002243 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002244 if (err == 0 && x != NULL) {
2245#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002246 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002247 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002248 fprintf(stderr,
2249 "XXX undetected error\n");
2250 else
2251#endif
2252 continue; /* Normal, fast path */
2253 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002254 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002255 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002256 err = 0;
2257 }
2258
Guido van Rossum374a9221991-04-04 10:40:29 +00002259 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002260
Guido van Rossum374a9221991-04-04 10:40:29 +00002261 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002262 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002263 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002264 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002265 why = WHY_EXCEPTION;
2266 }
2267 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002268#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002269 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002270 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002271 if (PyErr_Occurred()) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002272 fprintf(stderr,
2273 "XXX undetected error (why=%d)\n",
2274 why);
2275 why = WHY_EXCEPTION;
2276 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002277 }
2278#endif
2279
2280 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002281
Guido van Rossum374a9221991-04-04 10:40:29 +00002282 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002283 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002284
Fred Drake8f51f542001-10-04 14:48:42 +00002285 if (tstate->c_tracefunc != NULL)
2286 call_exc_trace(tstate->c_tracefunc,
2287 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002288 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002289
Guido van Rossum374a9221991-04-04 10:40:29 +00002290 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002291
Guido van Rossum374a9221991-04-04 10:40:29 +00002292 if (why == WHY_RERAISE)
2293 why = WHY_EXCEPTION;
2294
2295 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002296
Tim Peters5ca576e2001-06-18 22:08:13 +00002297 while (why != WHY_NOT && why != WHY_YIELD && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002298 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002299
2300 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2301 /* For a continue inside a try block,
2302 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002303 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2304 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002305 why = WHY_NOT;
2306 JUMPTO(PyInt_AS_LONG(retval));
2307 Py_DECREF(retval);
2308 break;
2309 }
2310
Guido van Rossum374a9221991-04-04 10:40:29 +00002311 while (STACK_LEVEL() > b->b_level) {
2312 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002313 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002314 }
2315 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2316 why = WHY_NOT;
2317 JUMPTO(b->b_handler);
2318 break;
2319 }
2320 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002321 (b->b_type == SETUP_EXCEPT &&
2322 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002323 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002324 PyObject *exc, *val, *tb;
2325 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002326 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002327 val = Py_None;
2328 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002329 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002330 /* Make the raw exception data
2331 available to the handler,
2332 so a program can emulate the
2333 Python main loop. Don't do
2334 this for 'finally'. */
2335 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002336 PyErr_NormalizeException(
2337 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002338 set_exc_info(tstate,
2339 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002340 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002341 if (tb == NULL) {
2342 Py_INCREF(Py_None);
2343 PUSH(Py_None);
2344 } else
2345 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002346 PUSH(val);
2347 PUSH(exc);
2348 }
2349 else {
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002350 if (why == WHY_RETURN ||
Guido van Rossumc5fe5eb2002-06-12 03:45:21 +00002351 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00002352 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002353 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002354 PUSH(v);
2355 }
2356 why = WHY_NOT;
2357 JUMPTO(b->b_handler);
2358 break;
2359 }
2360 } /* unwind stack */
2361
2362 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002363
Guido van Rossum374a9221991-04-04 10:40:29 +00002364 if (why != WHY_NOT)
2365 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002366
Guido van Rossum374a9221991-04-04 10:40:29 +00002367 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002368
Guido van Rossum35974fb2001-12-06 21:28:18 +00002369 if (why != WHY_YIELD) {
2370 /* Pop remaining stack entries -- but when yielding */
2371 while (!EMPTY()) {
2372 v = POP();
2373 Py_XDECREF(v);
2374 }
2375 }
2376
Tim Peters5ca576e2001-06-18 22:08:13 +00002377 if (why != WHY_RETURN && why != WHY_YIELD)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002378 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002379
Fred Drake9e3ad782001-07-03 23:39:52 +00002380 if (tstate->use_tracing) {
2381 if (tstate->c_tracefunc
2382 && (why == WHY_RETURN || why == WHY_YIELD)) {
2383 if (call_trace(tstate->c_tracefunc,
2384 tstate->c_traceobj, f,
2385 PyTrace_RETURN, retval)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002386 Py_XDECREF(retval);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002387 retval = NULL;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002388 why = WHY_EXCEPTION;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002389 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002390 }
Fred Drake8f51f542001-10-04 14:48:42 +00002391 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002392 if (why == WHY_EXCEPTION)
2393 call_trace_protected(tstate->c_profilefunc,
2394 tstate->c_profileobj, f,
2395 PyTrace_RETURN);
2396 else if (call_trace(tstate->c_profilefunc,
2397 tstate->c_profileobj, f,
2398 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002399 Py_XDECREF(retval);
2400 retval = NULL;
2401 why = WHY_EXCEPTION;
2402 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002403 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002404 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002405
Guido van Rossuma027efa1997-05-05 20:56:21 +00002406 reset_exc_info(tstate);
2407
Tim Peters5ca576e2001-06-18 22:08:13 +00002408 /* pop frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002409 --tstate->recursion_depth;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002410 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002411
Guido van Rossum96a42c81992-01-12 02:29:51 +00002412 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002413}
2414
Tim Peters6d6c1a32001-08-02 04:15:00 +00002415PyObject *
2416PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002417 PyObject **args, int argcount, PyObject **kws, int kwcount,
2418 PyObject **defs, int defcount, PyObject *closure)
2419{
2420 register PyFrameObject *f;
2421 register PyObject *retval = NULL;
2422 register PyObject **fastlocals, **freevars;
2423 PyThreadState *tstate = PyThreadState_GET();
2424 PyObject *x, *u;
2425
2426 if (globals == NULL) {
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002427 PyErr_SetString(PyExc_SystemError,
2428 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002429 return NULL;
2430 }
2431
Jeremy Hylton985eba52003-02-05 23:13:00 +00002432 assert(globals != NULL);
2433 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002434 if (f == NULL)
2435 return NULL;
2436
2437 fastlocals = f->f_localsplus;
2438 freevars = f->f_localsplus + f->f_nlocals;
2439
2440 if (co->co_argcount > 0 ||
2441 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2442 int i;
2443 int n = argcount;
2444 PyObject *kwdict = NULL;
2445 if (co->co_flags & CO_VARKEYWORDS) {
2446 kwdict = PyDict_New();
2447 if (kwdict == NULL)
2448 goto fail;
2449 i = co->co_argcount;
2450 if (co->co_flags & CO_VARARGS)
2451 i++;
2452 SETLOCAL(i, kwdict);
2453 }
2454 if (argcount > co->co_argcount) {
2455 if (!(co->co_flags & CO_VARARGS)) {
2456 PyErr_Format(PyExc_TypeError,
2457 "%.200s() takes %s %d "
2458 "%sargument%s (%d given)",
2459 PyString_AsString(co->co_name),
2460 defcount ? "at most" : "exactly",
2461 co->co_argcount,
2462 kwcount ? "non-keyword " : "",
2463 co->co_argcount == 1 ? "" : "s",
2464 argcount);
2465 goto fail;
2466 }
2467 n = co->co_argcount;
2468 }
2469 for (i = 0; i < n; i++) {
2470 x = args[i];
2471 Py_INCREF(x);
2472 SETLOCAL(i, x);
2473 }
2474 if (co->co_flags & CO_VARARGS) {
2475 u = PyTuple_New(argcount - n);
2476 if (u == NULL)
2477 goto fail;
2478 SETLOCAL(co->co_argcount, u);
2479 for (i = n; i < argcount; i++) {
2480 x = args[i];
2481 Py_INCREF(x);
2482 PyTuple_SET_ITEM(u, i-n, x);
2483 }
2484 }
2485 for (i = 0; i < kwcount; i++) {
2486 PyObject *keyword = kws[2*i];
2487 PyObject *value = kws[2*i + 1];
2488 int j;
2489 if (keyword == NULL || !PyString_Check(keyword)) {
2490 PyErr_Format(PyExc_TypeError,
2491 "%.200s() keywords must be strings",
2492 PyString_AsString(co->co_name));
2493 goto fail;
2494 }
2495 /* XXX slow -- speed up using dictionary? */
2496 for (j = 0; j < co->co_argcount; j++) {
2497 PyObject *nm = PyTuple_GET_ITEM(
2498 co->co_varnames, j);
2499 int cmp = PyObject_RichCompareBool(
2500 keyword, nm, Py_EQ);
2501 if (cmp > 0)
2502 break;
2503 else if (cmp < 0)
2504 goto fail;
2505 }
2506 /* Check errors from Compare */
2507 if (PyErr_Occurred())
2508 goto fail;
2509 if (j >= co->co_argcount) {
2510 if (kwdict == NULL) {
2511 PyErr_Format(PyExc_TypeError,
2512 "%.200s() got an unexpected "
2513 "keyword argument '%.400s'",
2514 PyString_AsString(co->co_name),
2515 PyString_AsString(keyword));
2516 goto fail;
2517 }
2518 PyDict_SetItem(kwdict, keyword, value);
2519 }
2520 else {
2521 if (GETLOCAL(j) != NULL) {
2522 PyErr_Format(PyExc_TypeError,
2523 "%.200s() got multiple "
2524 "values for keyword "
2525 "argument '%.400s'",
2526 PyString_AsString(co->co_name),
2527 PyString_AsString(keyword));
2528 goto fail;
2529 }
2530 Py_INCREF(value);
2531 SETLOCAL(j, value);
2532 }
2533 }
2534 if (argcount < co->co_argcount) {
2535 int m = co->co_argcount - defcount;
2536 for (i = argcount; i < m; i++) {
2537 if (GETLOCAL(i) == NULL) {
2538 PyErr_Format(PyExc_TypeError,
2539 "%.200s() takes %s %d "
2540 "%sargument%s (%d given)",
2541 PyString_AsString(co->co_name),
2542 ((co->co_flags & CO_VARARGS) ||
2543 defcount) ? "at least"
2544 : "exactly",
2545 m, kwcount ? "non-keyword " : "",
2546 m == 1 ? "" : "s", i);
2547 goto fail;
2548 }
2549 }
2550 if (n > m)
2551 i = n - m;
2552 else
2553 i = 0;
2554 for (; i < defcount; i++) {
2555 if (GETLOCAL(m+i) == NULL) {
2556 PyObject *def = defs[i];
2557 Py_INCREF(def);
2558 SETLOCAL(m+i, def);
2559 }
2560 }
2561 }
2562 }
2563 else {
2564 if (argcount > 0 || kwcount > 0) {
2565 PyErr_Format(PyExc_TypeError,
2566 "%.200s() takes no arguments (%d given)",
2567 PyString_AsString(co->co_name),
2568 argcount + kwcount);
2569 goto fail;
2570 }
2571 }
2572 /* Allocate and initialize storage for cell vars, and copy free
2573 vars into frame. This isn't too efficient right now. */
2574 if (f->f_ncells) {
2575 int i = 0, j = 0, nargs, found;
2576 char *cellname, *argname;
2577 PyObject *c;
2578
2579 nargs = co->co_argcount;
2580 if (co->co_flags & CO_VARARGS)
2581 nargs++;
2582 if (co->co_flags & CO_VARKEYWORDS)
2583 nargs++;
2584
2585 /* Check for cells that shadow args */
2586 for (i = 0; i < f->f_ncells && j < nargs; ++i) {
2587 cellname = PyString_AS_STRING(
2588 PyTuple_GET_ITEM(co->co_cellvars, i));
2589 found = 0;
2590 while (j < nargs) {
2591 argname = PyString_AS_STRING(
2592 PyTuple_GET_ITEM(co->co_varnames, j));
2593 if (strcmp(cellname, argname) == 0) {
2594 c = PyCell_New(GETLOCAL(j));
2595 if (c == NULL)
2596 goto fail;
2597 GETLOCAL(f->f_nlocals + i) = c;
2598 found = 1;
2599 break;
2600 }
2601 j++;
2602 }
2603 if (found == 0) {
2604 c = PyCell_New(NULL);
2605 if (c == NULL)
2606 goto fail;
2607 SETLOCAL(f->f_nlocals + i, c);
2608 }
2609 }
2610 /* Initialize any that are left */
2611 while (i < f->f_ncells) {
2612 c = PyCell_New(NULL);
2613 if (c == NULL)
2614 goto fail;
2615 SETLOCAL(f->f_nlocals + i, c);
2616 i++;
2617 }
2618 }
2619 if (f->f_nfreevars) {
2620 int i;
2621 for (i = 0; i < f->f_nfreevars; ++i) {
2622 PyObject *o = PyTuple_GET_ITEM(closure, i);
2623 Py_INCREF(o);
2624 freevars[f->f_ncells + i] = o;
2625 }
2626 }
2627
Tim Peters5ca576e2001-06-18 22:08:13 +00002628 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002629 /* Don't need to keep the reference to f_back, it will be set
2630 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002631 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002632 f->f_back = NULL;
2633
Jeremy Hylton985eba52003-02-05 23:13:00 +00002634 PCALL(PCALL_GENERATOR);
2635
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002636 /* Create a new generator that owns the ready to run frame
2637 * and return that as the value. */
Tim Peters5ca576e2001-06-18 22:08:13 +00002638 return gen_new(f);
2639 }
2640
2641 retval = eval_frame(f);
2642
2643 fail: /* Jump here from prelude on failure */
2644
Tim Petersb13680b2001-11-27 23:29:29 +00002645 /* decref'ing the frame can cause __del__ methods to get invoked,
2646 which can call back into Python. While we're done with the
2647 current Python frame (f), the associated C stack is still in use,
2648 so recursion_depth must be boosted for the duration.
2649 */
2650 assert(tstate != NULL);
2651 ++tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002652 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00002653 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002654 return retval;
2655}
2656
2657
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002658/* Implementation notes for set_exc_info() and reset_exc_info():
2659
2660- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2661 'exc_traceback'. These always travel together.
2662
2663- tstate->curexc_ZZZ is the "hot" exception that is set by
2664 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2665
2666- Once an exception is caught by an except clause, it is transferred
2667 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2668 can pick it up. This is the primary task of set_exc_info().
2669
2670- Now let me explain the complicated dance with frame->f_exc_ZZZ.
2671
2672 Long ago, when none of this existed, there were just a few globals:
2673 one set corresponding to the "hot" exception, and one set
2674 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2675 globals; they were simply stored as sys.exc_ZZZ. For backwards
2676 compatibility, they still are!) The problem was that in code like
2677 this:
2678
2679 try:
2680 "something that may fail"
2681 except "some exception":
2682 "do something else first"
2683 "print the exception from sys.exc_ZZZ."
2684
2685 if "do something else first" invoked something that raised and caught
2686 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2687 cause of subtle bugs. I fixed this by changing the semantics as
2688 follows:
2689
2690 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2691 *in that frame*.
2692
2693 - But initially, and as long as no exception is caught in a given
2694 frame, sys.exc_ZZZ will hold the last exception caught in the
2695 previous frame (or the frame before that, etc.).
2696
2697 The first bullet fixed the bug in the above example. The second
2698 bullet was for backwards compatibility: it was (and is) common to
2699 have a function that is called when an exception is caught, and to
2700 have that function access the caught exception via sys.exc_ZZZ.
2701 (Example: traceback.print_exc()).
2702
2703 At the same time I fixed the problem that sys.exc_ZZZ weren't
2704 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2705 but that's really a separate improvement.
2706
2707 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2708 variables to what they were before the current frame was called. The
2709 set_exc_info() function saves them on the frame so that
2710 reset_exc_info() can restore them. The invariant is that
2711 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2712 exception (where "catching" an exception applies only to successful
2713 except clauses); and if the current frame ever caught an exception,
2714 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2715 at the start of the current frame.
2716
2717*/
2718
Guido van Rossuma027efa1997-05-05 20:56:21 +00002719static void
Guido van Rossumac7be682001-01-17 15:42:30 +00002720set_exc_info(PyThreadState *tstate,
2721 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002722{
2723 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002724 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00002725
Guido van Rossuma027efa1997-05-05 20:56:21 +00002726 frame = tstate->frame;
2727 if (frame->f_exc_type == NULL) {
2728 /* This frame didn't catch an exception before */
2729 /* Save previous exception of this thread in this frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002730 if (tstate->exc_type == NULL) {
2731 Py_INCREF(Py_None);
2732 tstate->exc_type = Py_None;
2733 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002734 tmp_type = frame->f_exc_type;
2735 tmp_value = frame->f_exc_value;
2736 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002737 Py_XINCREF(tstate->exc_type);
2738 Py_XINCREF(tstate->exc_value);
2739 Py_XINCREF(tstate->exc_traceback);
2740 frame->f_exc_type = tstate->exc_type;
2741 frame->f_exc_value = tstate->exc_value;
2742 frame->f_exc_traceback = tstate->exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002743 Py_XDECREF(tmp_type);
2744 Py_XDECREF(tmp_value);
2745 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002746 }
2747 /* Set new exception for this thread */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002748 tmp_type = tstate->exc_type;
2749 tmp_value = tstate->exc_value;
2750 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002751 Py_XINCREF(type);
2752 Py_XINCREF(value);
2753 Py_XINCREF(tb);
2754 tstate->exc_type = type;
2755 tstate->exc_value = value;
2756 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002757 Py_XDECREF(tmp_type);
2758 Py_XDECREF(tmp_value);
2759 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002760 /* For b/w compatibility */
2761 PySys_SetObject("exc_type", type);
2762 PySys_SetObject("exc_value", value);
2763 PySys_SetObject("exc_traceback", tb);
2764}
2765
2766static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002767reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002768{
2769 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002770 PyObject *tmp_type, *tmp_value, *tmp_tb;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002771 frame = tstate->frame;
2772 if (frame->f_exc_type != NULL) {
2773 /* This frame caught an exception */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002774 tmp_type = tstate->exc_type;
2775 tmp_value = tstate->exc_value;
2776 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002777 Py_XINCREF(frame->f_exc_type);
2778 Py_XINCREF(frame->f_exc_value);
2779 Py_XINCREF(frame->f_exc_traceback);
2780 tstate->exc_type = frame->f_exc_type;
2781 tstate->exc_value = frame->f_exc_value;
2782 tstate->exc_traceback = frame->f_exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002783 Py_XDECREF(tmp_type);
2784 Py_XDECREF(tmp_value);
2785 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002786 /* For b/w compatibility */
2787 PySys_SetObject("exc_type", frame->f_exc_type);
2788 PySys_SetObject("exc_value", frame->f_exc_value);
2789 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
2790 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002791 tmp_type = frame->f_exc_type;
2792 tmp_value = frame->f_exc_value;
2793 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002794 frame->f_exc_type = NULL;
2795 frame->f_exc_value = NULL;
2796 frame->f_exc_traceback = NULL;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002797 Py_XDECREF(tmp_type);
2798 Py_XDECREF(tmp_value);
2799 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002800}
2801
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002802/* Logic for the raise statement (too complicated for inlining).
2803 This *consumes* a reference count to each of its arguments. */
Guido van Rossum1aa14831997-01-21 05:34:20 +00002804static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002805do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002806{
Guido van Rossumd295f121998-04-09 21:39:57 +00002807 if (type == NULL) {
2808 /* Reraise */
2809 PyThreadState *tstate = PyThreadState_Get();
2810 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
2811 value = tstate->exc_value;
2812 tb = tstate->exc_traceback;
2813 Py_XINCREF(type);
2814 Py_XINCREF(value);
2815 Py_XINCREF(tb);
2816 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002817
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002818 /* We support the following forms of raise:
2819 raise <class>, <classinstance>
2820 raise <class>, <argument tuple>
2821 raise <class>, None
2822 raise <class>, <argument>
2823 raise <classinstance>, None
2824 raise <string>, <object>
2825 raise <string>, None
2826
2827 An omitted second argument is the same as None.
2828
2829 In addition, raise <tuple>, <anything> is the same as
2830 raising the tuple's first item (and it better have one!);
2831 this rule is applied recursively.
2832
2833 Finally, an optional third argument can be supplied, which
2834 gives the traceback to be substituted (useful when
2835 re-raising an exception after examining it). */
2836
2837 /* First, check the traceback argument, replacing None with
2838 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002839 if (tb == Py_None) {
2840 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002841 tb = NULL;
2842 }
2843 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002844 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002845 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002846 goto raise_error;
2847 }
2848
2849 /* Next, replace a missing value with None */
2850 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002851 value = Py_None;
2852 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002853 }
2854
2855 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00002856 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
2857 PyObject *tmp = type;
2858 type = PyTuple_GET_ITEM(type, 0);
2859 Py_INCREF(type);
2860 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002861 }
2862
Tim Petersafb2c802002-04-18 18:06:20 +00002863 if (PyString_CheckExact(type))
2864 /* Raising builtin string is deprecated but still allowed --
2865 * do nothing. Raising an instance of a new-style str
2866 * subclass is right out. */
Neal Norwitz37aa0662003-01-10 15:31:15 +00002867 PyErr_Warn(PyExc_PendingDeprecationWarning,
2868 "raising a string exception is deprecated");
Barry Warsaw4249f541997-08-22 21:26:19 +00002869
2870 else if (PyClass_Check(type))
2871 PyErr_NormalizeException(&type, &value, &tb);
2872
Guido van Rossumb209a111997-04-29 18:18:01 +00002873 else if (PyInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002874 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002875 if (value != Py_None) {
2876 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002877 "instance exception may not have a separate value");
2878 goto raise_error;
2879 }
2880 else {
2881 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00002882 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002883 value = type;
Guido van Rossumb209a111997-04-29 18:18:01 +00002884 type = (PyObject*) ((PyInstanceObject*)type)->in_class;
2885 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002886 }
2887 }
2888 else {
2889 /* Not something you can raise. You get an exception
2890 anyway, just not what you specified :-) */
Jeremy Hylton960d9482001-04-27 02:25:33 +00002891 PyErr_Format(PyExc_TypeError,
Neal Norwitz37aa0662003-01-10 15:31:15 +00002892 "exceptions must be classes, instances, or "
2893 "strings (deprecated), not %s",
2894 type->ob_type->tp_name);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002895 goto raise_error;
2896 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002897 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002898 if (tb == NULL)
2899 return WHY_EXCEPTION;
2900 else
2901 return WHY_RERAISE;
2902 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00002903 Py_XDECREF(value);
2904 Py_XDECREF(type);
2905 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002906 return WHY_EXCEPTION;
2907}
2908
Tim Petersd6d010b2001-06-21 02:49:55 +00002909/* Iterate v argcnt times and store the results on the stack (via decreasing
2910 sp). Return 1 for success, 0 if error. */
2911
Barry Warsawe42b18f1997-08-25 22:13:04 +00002912static int
Tim Petersd6d010b2001-06-21 02:49:55 +00002913unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00002914{
Tim Petersd6d010b2001-06-21 02:49:55 +00002915 int i = 0;
2916 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00002917 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00002918
Tim Petersd6d010b2001-06-21 02:49:55 +00002919 assert(v != NULL);
2920
2921 it = PyObject_GetIter(v);
2922 if (it == NULL)
2923 goto Error;
2924
2925 for (; i < argcnt; i++) {
2926 w = PyIter_Next(it);
2927 if (w == NULL) {
2928 /* Iterator done, via error or exhaustion. */
2929 if (!PyErr_Occurred()) {
2930 PyErr_Format(PyExc_ValueError,
2931 "need more than %d value%s to unpack",
2932 i, i == 1 ? "" : "s");
2933 }
2934 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00002935 }
2936 *--sp = w;
2937 }
Tim Petersd6d010b2001-06-21 02:49:55 +00002938
2939 /* We better have exhausted the iterator now. */
2940 w = PyIter_Next(it);
2941 if (w == NULL) {
2942 if (PyErr_Occurred())
2943 goto Error;
2944 Py_DECREF(it);
2945 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00002946 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00002947 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00002948 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00002949 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00002950Error:
Barry Warsaw91010551997-08-25 22:30:51 +00002951 for (; i > 0; i--, sp++)
2952 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00002953 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00002954 return 0;
2955}
2956
2957
Guido van Rossum96a42c81992-01-12 02:29:51 +00002958#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00002959static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002960prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002961{
Guido van Rossum3f5da241990-12-20 15:06:42 +00002962 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00002963 if (PyObject_Print(v, stdout, 0) != 0)
2964 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00002965 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00002966 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002967}
Guido van Rossum3f5da241990-12-20 15:06:42 +00002968#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002969
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002970static void
Fred Drake5755ce62001-06-27 19:19:46 +00002971call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002972{
Guido van Rossumb209a111997-04-29 18:18:01 +00002973 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002974 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00002975 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00002976 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002977 value = Py_None;
2978 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00002979 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002980 arg = Py_BuildValue("(OOO)", type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002981 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002982 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002983 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002984 }
Fred Drake5755ce62001-06-27 19:19:46 +00002985 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002986 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002987 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00002988 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002989 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00002990 Py_XDECREF(type);
2991 Py_XDECREF(value);
2992 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002993 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002994}
2995
Fred Drake4ec5d562001-10-04 19:26:43 +00002996static void
2997call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
2998 int what)
2999{
3000 PyObject *type, *value, *traceback;
3001 int err;
3002 PyErr_Fetch(&type, &value, &traceback);
3003 err = call_trace(func, obj, frame, what, NULL);
3004 if (err == 0)
3005 PyErr_Restore(type, value, traceback);
3006 else {
3007 Py_XDECREF(type);
3008 Py_XDECREF(value);
3009 Py_XDECREF(traceback);
3010 }
3011}
3012
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003013static int
Fred Drake5755ce62001-06-27 19:19:46 +00003014call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3015 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003016{
Fred Drake5755ce62001-06-27 19:19:46 +00003017 register PyThreadState *tstate = frame->f_tstate;
3018 int result;
3019 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003020 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003021 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003022 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003023 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003024 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3025 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003026 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003027 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003028}
3029
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003030PyObject *
3031_PyEval_CallTracing(PyObject *func, PyObject *args)
3032{
3033 PyFrameObject *frame = PyEval_GetFrame();
3034 PyThreadState *tstate = frame->f_tstate;
3035 int save_tracing = tstate->tracing;
3036 int save_use_tracing = tstate->use_tracing;
3037 PyObject *result;
3038
3039 tstate->tracing = 0;
3040 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3041 || (tstate->c_profilefunc != NULL));
3042 result = PyObject_Call(func, args, NULL);
3043 tstate->tracing = save_tracing;
3044 tstate->use_tracing = save_use_tracing;
3045 return result;
3046}
3047
Michael W. Hudson006c7522002-11-08 13:08:46 +00003048static int
Michael W. Hudson019a78e2002-11-08 12:53:11 +00003049maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003050 PyFrameObject *frame, int *instr_lb, int *instr_ub)
3051{
3052 /* The theory of SET_LINENO-less tracing.
3053
3054 In a nutshell, we use the co_lnotab field of the code object
3055 to tell when execution has moved onto a different line.
3056
3057 As mentioned above, the basic idea is so set things up so
3058 that
3059
3060 *instr_lb <= frame->f_lasti < *instr_ub
3061
3062 is true so long as execution does not change lines.
3063
3064 This is all fairly simple. Digging the information out of
3065 co_lnotab takes some work, but is conceptually clear.
3066
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003067 Somewhat harder to explain is why we don't *always* call the
3068 line trace function when the above test fails.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003069
3070 Consider this code:
3071
3072 1: def f(a):
3073 2: if a:
3074 3: print 1
3075 4: else:
3076 5: print 2
3077
3078 which compiles to this:
3079
3080 2 0 LOAD_FAST 0 (a)
3081 3 JUMP_IF_FALSE 9 (to 15)
3082 6 POP_TOP
3083
3084 3 7 LOAD_CONST 1 (1)
3085 10 PRINT_ITEM
3086 11 PRINT_NEWLINE
3087 12 JUMP_FORWARD 6 (to 21)
3088 >> 15 POP_TOP
3089
3090 5 16 LOAD_CONST 2 (2)
3091 19 PRINT_ITEM
3092 20 PRINT_NEWLINE
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003093 >> 21 LOAD_CONST 0 (None)
3094 24 RETURN_VALUE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003095
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003096 If 'a' is false, execution will jump to instruction at offset
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003097 15 and the co_lnotab will claim that execution has moved to
3098 line 3. This is at best misleading. In this case we could
3099 associate the POP_TOP with line 4, but that doesn't make
3100 sense in all cases (I think).
3101
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003102 What we do is only call the line trace function if the co_lnotab
3103 indicates we have jumped to the *start* of a line, i.e. if the
3104 current instruction offset matches the offset given for the
3105 start of a line by the co_lnotab.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003106
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003107 This also takes care of the situation where 'a' is true.
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003108 Execution will jump from instruction offset 12 to offset 21.
3109 Then the co_lnotab would imply that execution has moved to line
3110 5, which is again misleading.
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003111
3112 Why do we set f_lineno when tracing? Well, consider the code
3113 above when 'a' is true. If stepping through this with 'n' in
3114 pdb, you would stop at line 1 with a "call" type event, then
3115 line events on lines 2 and 3, then a "return" type event -- but
3116 you would be shown line 5 during this event. This is a change
3117 from the behaviour in 2.2 and before, and I've found it
3118 confusing in practice. By setting and using f_lineno when
3119 tracing, one can report a line number different from that
3120 suggested by f_lasti on this one occasion where it's desirable.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003121 */
3122
Michael W. Hudson006c7522002-11-08 13:08:46 +00003123 int result = 0;
3124
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003125 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003126 PyCodeObject* co = frame->f_code;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003127 int size, addr, line;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003128 unsigned char* p;
3129
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003130 size = PyString_GET_SIZE(co->co_lnotab) / 2;
3131 p = (unsigned char*)PyString_AS_STRING(co->co_lnotab);
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003132
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003133 addr = 0;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003134 line = co->co_firstlineno;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003135
3136 /* possible optimization: if f->f_lasti == instr_ub
3137 (likely to be a common case) then we already know
3138 instr_lb -- if we stored the matching value of p
3139 somwhere we could skip the first while loop. */
3140
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003141 /* see comments in compile.c for the description of
3142 co_lnotab. A point to remember: increments to p
3143 should come in pairs -- although we don't care about
3144 the line increments here, treating them as byte
3145 increments gets confusing, to say the least. */
3146
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003147 while (size > 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003148 if (addr + *p > frame->f_lasti)
3149 break;
3150 addr += *p++;
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003151 if (*p) *instr_lb = addr;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003152 line += *p++;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003153 --size;
3154 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003155
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003156 if (addr == frame->f_lasti) {
3157 frame->f_lineno = line;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003158 result = call_trace(func, obj, frame,
3159 PyTrace_LINE, Py_None);
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003160 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003161
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003162 if (size > 0) {
3163 while (--size >= 0) {
3164 addr += *p++;
3165 if (*p++)
3166 break;
3167 }
3168 *instr_ub = addr;
3169 }
3170 else {
3171 *instr_ub = INT_MAX;
3172 }
3173 }
Michael W. Hudson006c7522002-11-08 13:08:46 +00003174
3175 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003176}
3177
Fred Drake5755ce62001-06-27 19:19:46 +00003178void
3179PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003180{
Fred Drake5755ce62001-06-27 19:19:46 +00003181 PyThreadState *tstate = PyThreadState_Get();
3182 PyObject *temp = tstate->c_profileobj;
3183 Py_XINCREF(arg);
3184 tstate->c_profilefunc = NULL;
3185 tstate->c_profileobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003186 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003187 Py_XDECREF(temp);
3188 tstate->c_profilefunc = func;
3189 tstate->c_profileobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003190 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003191}
3192
3193void
3194PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3195{
3196 PyThreadState *tstate = PyThreadState_Get();
3197 PyObject *temp = tstate->c_traceobj;
3198 Py_XINCREF(arg);
3199 tstate->c_tracefunc = NULL;
3200 tstate->c_traceobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003201 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003202 Py_XDECREF(temp);
3203 tstate->c_tracefunc = func;
3204 tstate->c_traceobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003205 tstate->use_tracing = ((func != NULL)
3206 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003207}
3208
Guido van Rossumb209a111997-04-29 18:18:01 +00003209PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003210PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003211{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003212 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003213 if (current_frame == NULL)
Michael W. Hudson019a78e2002-11-08 12:53:11 +00003214 return PyThreadState_Get()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003215 else
3216 return current_frame->f_builtins;
3217}
3218
Guido van Rossumb209a111997-04-29 18:18:01 +00003219PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003220PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003221{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003222 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003223 if (current_frame == NULL)
3224 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003225 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003226 return current_frame->f_locals;
3227}
3228
Guido van Rossumb209a111997-04-29 18:18:01 +00003229PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003230PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003231{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003232 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003233 if (current_frame == NULL)
3234 return NULL;
3235 else
3236 return current_frame->f_globals;
3237}
3238
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003239PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003240PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003241{
Michael W. Hudson019a78e2002-11-08 12:53:11 +00003242 PyThreadState *tstate = PyThreadState_Get();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003243 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003244}
3245
Guido van Rossum6135a871995-01-09 17:53:26 +00003246int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003247PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003248{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003249 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003250 return current_frame == NULL ? 0 : current_frame->f_restricted;
3251}
3252
Guido van Rossumbe270261997-05-22 22:26:18 +00003253int
Tim Peters5ba58662001-07-16 02:29:45 +00003254PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003255{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003256 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003257 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003258
3259 if (current_frame != NULL) {
3260 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003261 const int compilerflags = codeflags & PyCF_MASK;
3262 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003263 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003264 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003265 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003266#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003267 if (codeflags & CO_GENERATOR_ALLOWED) {
3268 result = 1;
3269 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3270 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003271#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003272 }
3273 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003274}
3275
3276int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003277Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003278{
Guido van Rossumb209a111997-04-29 18:18:01 +00003279 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003280 if (f == NULL)
3281 return 0;
3282 if (!PyFile_SoftSpace(f, 0))
3283 return 0;
3284 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003285}
3286
Guido van Rossum3f5da241990-12-20 15:06:42 +00003287
Guido van Rossum681d79a1995-07-18 14:51:37 +00003288/* External interface to call any callable object.
3289 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003290
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003291#undef PyEval_CallObject
3292/* for backward compatibility: export this interface */
3293
Guido van Rossumb209a111997-04-29 18:18:01 +00003294PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003295PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003296{
Guido van Rossumb209a111997-04-29 18:18:01 +00003297 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003298}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003299#define PyEval_CallObject(func,arg) \
3300 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003301
Guido van Rossumb209a111997-04-29 18:18:01 +00003302PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003303PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003304{
Jeremy Hylton52820442001-01-03 23:52:36 +00003305 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003306
3307 if (arg == NULL)
Guido van Rossumb209a111997-04-29 18:18:01 +00003308 arg = PyTuple_New(0);
3309 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003310 PyErr_SetString(PyExc_TypeError,
3311 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003312 return NULL;
3313 }
3314 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003315 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003316
Guido van Rossumb209a111997-04-29 18:18:01 +00003317 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003318 PyErr_SetString(PyExc_TypeError,
3319 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003320 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003321 return NULL;
3322 }
3323
Tim Peters6d6c1a32001-08-02 04:15:00 +00003324 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003325 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003326 return result;
3327}
3328
Tim Peters6d6c1a32001-08-02 04:15:00 +00003329char *
3330PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003331{
3332 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003333 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003334 else if (PyFunction_Check(func))
3335 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3336 else if (PyCFunction_Check(func))
3337 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3338 else if (PyClass_Check(func))
3339 return PyString_AsString(((PyClassObject*)func)->cl_name);
3340 else if (PyInstance_Check(func)) {
3341 return PyString_AsString(
3342 ((PyInstanceObject*)func)->in_class->cl_name);
3343 } else {
3344 return func->ob_type->tp_name;
3345 }
3346}
3347
Tim Peters6d6c1a32001-08-02 04:15:00 +00003348char *
3349PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003350{
3351 if (PyMethod_Check(func))
3352 return "()";
3353 else if (PyFunction_Check(func))
3354 return "()";
3355 else if (PyCFunction_Check(func))
3356 return "()";
3357 else if (PyClass_Check(func))
3358 return " constructor";
3359 else if (PyInstance_Check(func)) {
3360 return " instance";
3361 } else {
3362 return " object";
3363 }
3364}
3365
Jeremy Hylton52820442001-01-03 23:52:36 +00003366#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
3367
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003368static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003369err_args(PyObject *func, int flags, int nargs)
3370{
3371 if (flags & METH_NOARGS)
3372 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003373 "%.200s() takes no arguments (%d given)",
Jeremy Hylton192690e2002-08-16 18:36:11 +00003374 ((PyCFunctionObject *)func)->m_ml->ml_name,
3375 nargs);
3376 else
3377 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003378 "%.200s() takes exactly one argument (%d given)",
Jeremy Hylton192690e2002-08-16 18:36:11 +00003379 ((PyCFunctionObject *)func)->m_ml->ml_name,
3380 nargs);
3381}
3382
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003383static PyObject *
3384call_function(PyObject ***pp_stack, int oparg)
3385{
3386 int na = oparg & 0xff;
3387 int nk = (oparg>>8) & 0xff;
3388 int n = na + 2 * nk;
3389 PyObject **pfunc = (*pp_stack) - n - 1;
3390 PyObject *func = *pfunc;
3391 PyObject *x, *w;
3392
Jeremy Hylton985eba52003-02-05 23:13:00 +00003393 /* Always dispatch PyCFunction first, because these are
3394 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003395 */
3396 if (PyCFunction_Check(func) && nk == 0) {
3397 int flags = PyCFunction_GET_FLAGS(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003398 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003399 if (flags & (METH_NOARGS | METH_O)) {
3400 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3401 PyObject *self = PyCFunction_GET_SELF(func);
3402 if (flags & METH_NOARGS && na == 0)
3403 x = (*meth)(self, NULL);
3404 else if (flags & METH_O && na == 1) {
3405 PyObject *arg = EXT_POP(*pp_stack);
3406 x = (*meth)(self, arg);
3407 Py_DECREF(arg);
3408 }
3409 else {
3410 err_args(func, flags, na);
3411 x = NULL;
3412 }
3413 }
3414 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003415 PyObject *callargs;
3416 callargs = load_args(pp_stack, na);
3417 x = PyCFunction_Call(func, callargs, NULL);
3418 Py_XDECREF(callargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003419 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003420 } else {
3421 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3422 /* optimize access to bound methods */
3423 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003424 PCALL(PCALL_METHOD);
3425 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003426 Py_INCREF(self);
3427 func = PyMethod_GET_FUNCTION(func);
3428 Py_INCREF(func);
3429 Py_DECREF(*pfunc);
3430 *pfunc = self;
3431 na++;
3432 n++;
3433 } else
3434 Py_INCREF(func);
3435 if (PyFunction_Check(func))
3436 x = fast_function(func, pp_stack, n, na, nk);
3437 else
3438 x = do_call(func, pp_stack, na, nk);
3439 Py_DECREF(func);
3440 }
3441
Jeremy Hylton985eba52003-02-05 23:13:00 +00003442 /* What does this do? */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003443 while ((*pp_stack) > pfunc) {
3444 w = EXT_POP(*pp_stack);
3445 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003446 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003447 }
3448 return x;
3449}
3450
Jeremy Hylton192690e2002-08-16 18:36:11 +00003451/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003452 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003453 For the simplest case -- a function that takes only positional
3454 arguments and is called with only positional arguments -- it
3455 inlines the most primitive frame setup code from
3456 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3457 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003458*/
3459
3460static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003461fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003462{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003463 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003464 PyObject *globals = PyFunction_GET_GLOBALS(func);
3465 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3466 PyObject **d = NULL;
3467 int nd = 0;
3468
Jeremy Hylton985eba52003-02-05 23:13:00 +00003469 PCALL(PCALL_FUNCTION);
3470 PCALL(PCALL_FAST_FUNCTION);
Raymond Hettinger40174c32003-05-31 07:04:16 +00003471 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003472 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3473 PyFrameObject *f;
3474 PyObject *retval = NULL;
3475 PyThreadState *tstate = PyThreadState_GET();
3476 PyObject **fastlocals, **stack;
3477 int i;
3478
3479 PCALL(PCALL_FASTER_FUNCTION);
3480 assert(globals != NULL);
3481 /* XXX Perhaps we should create a specialized
3482 PyFrame_New() that doesn't take locals, but does
3483 take builtins without sanity checking them.
3484 */
3485 f = PyFrame_New(tstate, co, globals, NULL);
3486 if (f == NULL)
3487 return NULL;
3488
3489 fastlocals = f->f_localsplus;
3490 stack = (*pp_stack) - n;
3491
3492 for (i = 0; i < n; i++) {
3493 Py_INCREF(*stack);
3494 fastlocals[i] = *stack++;
3495 }
3496 retval = eval_frame(f);
3497 assert(tstate != NULL);
3498 ++tstate->recursion_depth;
3499 Py_DECREF(f);
3500 --tstate->recursion_depth;
3501 return retval;
3502 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003503 if (argdefs != NULL) {
3504 d = &PyTuple_GET_ITEM(argdefs, 0);
3505 nd = ((PyTupleObject *)argdefs)->ob_size;
3506 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003507 return PyEval_EvalCodeEx(co, globals,
3508 (PyObject *)NULL, (*pp_stack)-n, na,
3509 (*pp_stack)-2*nk, nk, d, nd,
3510 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003511}
3512
3513static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003514update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3515 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003516{
3517 PyObject *kwdict = NULL;
3518 if (orig_kwdict == NULL)
3519 kwdict = PyDict_New();
3520 else {
3521 kwdict = PyDict_Copy(orig_kwdict);
3522 Py_DECREF(orig_kwdict);
3523 }
3524 if (kwdict == NULL)
3525 return NULL;
3526 while (--nk >= 0) {
3527 int err;
3528 PyObject *value = EXT_POP(*pp_stack);
3529 PyObject *key = EXT_POP(*pp_stack);
3530 if (PyDict_GetItem(kwdict, key) != NULL) {
Guido van Rossumac7be682001-01-17 15:42:30 +00003531 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +00003532 "%.200s%s got multiple values "
Jeremy Hylton512a2372001-04-11 13:52:29 +00003533 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003534 PyEval_GetFuncName(func),
3535 PyEval_GetFuncDesc(func),
Jeremy Hylton512a2372001-04-11 13:52:29 +00003536 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003537 Py_DECREF(key);
3538 Py_DECREF(value);
3539 Py_DECREF(kwdict);
3540 return NULL;
3541 }
3542 err = PyDict_SetItem(kwdict, key, value);
3543 Py_DECREF(key);
3544 Py_DECREF(value);
3545 if (err) {
3546 Py_DECREF(kwdict);
3547 return NULL;
3548 }
3549 }
3550 return kwdict;
3551}
3552
3553static PyObject *
3554update_star_args(int nstack, int nstar, PyObject *stararg,
3555 PyObject ***pp_stack)
3556{
3557 PyObject *callargs, *w;
3558
3559 callargs = PyTuple_New(nstack + nstar);
3560 if (callargs == NULL) {
3561 return NULL;
3562 }
3563 if (nstar) {
3564 int i;
3565 for (i = 0; i < nstar; i++) {
3566 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3567 Py_INCREF(a);
3568 PyTuple_SET_ITEM(callargs, nstack + i, a);
3569 }
3570 }
3571 while (--nstack >= 0) {
3572 w = EXT_POP(*pp_stack);
3573 PyTuple_SET_ITEM(callargs, nstack, w);
3574 }
3575 return callargs;
3576}
3577
3578static PyObject *
3579load_args(PyObject ***pp_stack, int na)
3580{
3581 PyObject *args = PyTuple_New(na);
3582 PyObject *w;
3583
3584 if (args == NULL)
3585 return NULL;
3586 while (--na >= 0) {
3587 w = EXT_POP(*pp_stack);
3588 PyTuple_SET_ITEM(args, na, w);
3589 }
3590 return args;
3591}
3592
3593static PyObject *
3594do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3595{
3596 PyObject *callargs = NULL;
3597 PyObject *kwdict = NULL;
3598 PyObject *result = NULL;
3599
3600 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003601 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003602 if (kwdict == NULL)
3603 goto call_fail;
3604 }
3605 callargs = load_args(pp_stack, na);
3606 if (callargs == NULL)
3607 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003608#ifdef CALL_PROFILE
3609 /* At this point, we have to look at the type of func to
3610 update the call stats properly. Do it here so as to avoid
3611 exposing the call stats machinery outside ceval.c
3612 */
3613 if (PyFunction_Check(func))
3614 PCALL(PCALL_FUNCTION);
3615 else if (PyMethod_Check(func))
3616 PCALL(PCALL_METHOD);
3617 else if (PyType_Check(func))
3618 PCALL(PCALL_TYPE);
3619 else
3620 PCALL(PCALL_OTHER);
3621#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003622 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003623 call_fail:
3624 Py_XDECREF(callargs);
3625 Py_XDECREF(kwdict);
3626 return result;
3627}
3628
3629static PyObject *
3630ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3631{
3632 int nstar = 0;
3633 PyObject *callargs = NULL;
3634 PyObject *stararg = NULL;
3635 PyObject *kwdict = NULL;
3636 PyObject *result = NULL;
3637
3638 if (flags & CALL_FLAG_KW) {
3639 kwdict = EXT_POP(*pp_stack);
3640 if (!(kwdict && PyDict_Check(kwdict))) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003641 PyErr_Format(PyExc_TypeError,
Jeremy Hylton512a2372001-04-11 13:52:29 +00003642 "%s%s argument after ** "
3643 "must be a dictionary",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003644 PyEval_GetFuncName(func),
3645 PyEval_GetFuncDesc(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003646 goto ext_call_fail;
3647 }
3648 }
3649 if (flags & CALL_FLAG_VAR) {
3650 stararg = EXT_POP(*pp_stack);
3651 if (!PyTuple_Check(stararg)) {
3652 PyObject *t = NULL;
3653 t = PySequence_Tuple(stararg);
3654 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003655 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3656 PyErr_Format(PyExc_TypeError,
3657 "%s%s argument after * "
3658 "must be a sequence",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003659 PyEval_GetFuncName(func),
3660 PyEval_GetFuncDesc(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003661 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003662 goto ext_call_fail;
3663 }
3664 Py_DECREF(stararg);
3665 stararg = t;
3666 }
3667 nstar = PyTuple_GET_SIZE(stararg);
3668 }
3669 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003670 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003671 if (kwdict == NULL)
3672 goto ext_call_fail;
3673 }
3674 callargs = update_star_args(na, nstar, stararg, pp_stack);
3675 if (callargs == NULL)
3676 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003677#ifdef CALL_PROFILE
3678 /* At this point, we have to look at the type of func to
3679 update the call stats properly. Do it here so as to avoid
3680 exposing the call stats machinery outside ceval.c
3681 */
3682 if (PyFunction_Check(func))
3683 PCALL(PCALL_FUNCTION);
3684 else if (PyMethod_Check(func))
3685 PCALL(PCALL_METHOD);
3686 else if (PyType_Check(func))
3687 PCALL(PCALL_TYPE);
3688 else
3689 PCALL(PCALL_OTHER);
3690#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003691 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003692 ext_call_fail:
3693 Py_XDECREF(callargs);
3694 Py_XDECREF(kwdict);
3695 Py_XDECREF(stararg);
3696 return result;
3697}
3698
Guido van Rossum3b9c6671996-07-30 18:40:29 +00003699#define SLICE_ERROR_MSG \
3700 "standard sequence type does not support step size other than one"
3701
Tim Peterscb479e72001-12-16 19:11:44 +00003702/* Extract a slice index from a PyInt or PyLong, and store in *pi.
3703 Silently reduce values larger than INT_MAX to INT_MAX, and silently
3704 boost values less than -INT_MAX to 0. Return 0 on error, 1 on success.
3705*/
Tim Petersb5196382001-12-16 19:44:20 +00003706/* Note: If v is NULL, return success without storing into *pi. This
3707 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3708 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00003709*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00003710int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003711_PyEval_SliceIndex(PyObject *v, int *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003712{
Tim Petersb5196382001-12-16 19:44:20 +00003713 if (v != NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003714 long x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003715 if (PyInt_Check(v)) {
3716 x = PyInt_AsLong(v);
3717 } else if (PyLong_Check(v)) {
3718 x = PyLong_AsLong(v);
3719 if (x==-1 && PyErr_Occurred()) {
3720 PyObject *long_zero;
Guido van Rossumac7be682001-01-17 15:42:30 +00003721 int cmp;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003722
Guido van Rossumac7be682001-01-17 15:42:30 +00003723 if (!PyErr_ExceptionMatches(
3724 PyExc_OverflowError)) {
3725 /* It's not an overflow error, so just
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003726 signal an error */
Guido van Rossum20c6add2000-05-08 14:06:50 +00003727 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003728 }
3729
Guido van Rossumac7be682001-01-17 15:42:30 +00003730 /* Clear the OverflowError */
3731 PyErr_Clear();
3732
3733 /* It's an overflow error, so we need to
3734 check the sign of the long integer,
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003735 set the value to INT_MAX or -INT_MAX,
3736 and clear the error. */
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003737
3738 /* Create a long integer with a value of 0 */
Guido van Rossumac7be682001-01-17 15:42:30 +00003739 long_zero = PyLong_FromLong(0L);
Tim Peterscb479e72001-12-16 19:11:44 +00003740 if (long_zero == NULL)
3741 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003742
3743 /* Check sign */
Guido van Rossumac7be682001-01-17 15:42:30 +00003744 cmp = PyObject_RichCompareBool(v, long_zero,
3745 Py_GT);
3746 Py_DECREF(long_zero);
3747 if (cmp < 0)
3748 return 0;
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003749 else if (cmp)
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003750 x = INT_MAX;
3751 else
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003752 x = -INT_MAX;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003753 }
3754 } else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003755 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003756 "slice indices must be integers");
Guido van Rossum20c6add2000-05-08 14:06:50 +00003757 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003758 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003759 /* Truncate -- very long indices are truncated anyway */
3760 if (x > INT_MAX)
3761 x = INT_MAX;
3762 else if (x < -INT_MAX)
Michael W. Hudsoncbd6fb92002-11-06 15:17:32 +00003763 x = -INT_MAX;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003764 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003765 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00003766 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003767}
3768
Guido van Rossum50d756e2001-08-18 17:43:36 +00003769#undef ISINT
3770#define ISINT(x) ((x) == NULL || PyInt_Check(x) || PyLong_Check(x))
3771
Guido van Rossumb209a111997-04-29 18:18:01 +00003772static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003773apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003774{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003775 PyTypeObject *tp = u->ob_type;
3776 PySequenceMethods *sq = tp->tp_as_sequence;
3777
3778 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3779 int ilow = 0, ihigh = INT_MAX;
3780 if (!_PyEval_SliceIndex(v, &ilow))
3781 return NULL;
3782 if (!_PyEval_SliceIndex(w, &ihigh))
3783 return NULL;
3784 return PySequence_GetSlice(u, ilow, ihigh);
3785 }
3786 else {
3787 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00003788 if (slice != NULL) {
3789 PyObject *res = PyObject_GetItem(u, slice);
3790 Py_DECREF(slice);
3791 return res;
3792 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00003793 else
3794 return NULL;
3795 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003796}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003797
3798static int
Guido van Rossumac7be682001-01-17 15:42:30 +00003799assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
3800 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003801{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003802 PyTypeObject *tp = u->ob_type;
3803 PySequenceMethods *sq = tp->tp_as_sequence;
3804
3805 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3806 int ilow = 0, ihigh = INT_MAX;
3807 if (!_PyEval_SliceIndex(v, &ilow))
3808 return -1;
3809 if (!_PyEval_SliceIndex(w, &ihigh))
3810 return -1;
3811 if (x == NULL)
3812 return PySequence_DelSlice(u, ilow, ihigh);
3813 else
3814 return PySequence_SetSlice(u, ilow, ihigh, x);
3815 }
3816 else {
3817 PyObject *slice = PySlice_New(v, w, NULL);
3818 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00003819 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003820 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00003821 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00003822 else
Guido van Rossum354797c2001-12-03 19:45:06 +00003823 res = PyObject_DelItem(u, slice);
3824 Py_DECREF(slice);
3825 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003826 }
3827 else
3828 return -1;
3829 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003830}
3831
Guido van Rossumb209a111997-04-29 18:18:01 +00003832static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003833cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003834{
Guido van Rossumac7be682001-01-17 15:42:30 +00003835 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003836 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00003837 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003838 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003839 break;
3840 case PyCmp_IS_NOT:
3841 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003842 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003843 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003844 res = PySequence_Contains(w, v);
3845 if (res < 0)
3846 return NULL;
3847 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003848 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00003849 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003850 if (res < 0)
3851 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003852 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003853 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003854 case PyCmp_EXC_MATCH:
Barry Warsaw4249f541997-08-22 21:26:19 +00003855 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003856 break;
3857 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00003858 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003859 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003860 v = res ? Py_True : Py_False;
3861 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003862 return v;
3863}
3864
Thomas Wouters52152252000-08-17 22:55:00 +00003865static PyObject *
3866import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003867{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003868 PyObject *x;
3869
3870 x = PyObject_GetAttr(v, name);
3871 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00003872 PyErr_Format(PyExc_ImportError,
3873 "cannot import name %.230s",
3874 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003875 }
Thomas Wouters52152252000-08-17 22:55:00 +00003876 return x;
3877}
Guido van Rossumac7be682001-01-17 15:42:30 +00003878
Thomas Wouters52152252000-08-17 22:55:00 +00003879static int
3880import_all_from(PyObject *locals, PyObject *v)
3881{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003882 PyObject *all = PyObject_GetAttrString(v, "__all__");
3883 PyObject *dict, *name, *value;
3884 int skip_leading_underscores = 0;
3885 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00003886
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003887 if (all == NULL) {
3888 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3889 return -1; /* Unexpected error */
3890 PyErr_Clear();
3891 dict = PyObject_GetAttrString(v, "__dict__");
3892 if (dict == NULL) {
3893 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3894 return -1;
3895 PyErr_SetString(PyExc_ImportError,
3896 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00003897 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003898 }
3899 all = PyMapping_Keys(dict);
3900 Py_DECREF(dict);
3901 if (all == NULL)
3902 return -1;
3903 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00003904 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003905
3906 for (pos = 0, err = 0; ; pos++) {
3907 name = PySequence_GetItem(all, pos);
3908 if (name == NULL) {
3909 if (!PyErr_ExceptionMatches(PyExc_IndexError))
3910 err = -1;
3911 else
3912 PyErr_Clear();
3913 break;
3914 }
3915 if (skip_leading_underscores &&
3916 PyString_Check(name) &&
3917 PyString_AS_STRING(name)[0] == '_')
3918 {
3919 Py_DECREF(name);
3920 continue;
3921 }
3922 value = PyObject_GetAttr(v, name);
3923 if (value == NULL)
3924 err = -1;
3925 else
3926 err = PyDict_SetItem(locals, name, value);
3927 Py_DECREF(name);
3928 Py_XDECREF(value);
3929 if (err != 0)
3930 break;
3931 }
3932 Py_DECREF(all);
3933 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00003934}
3935
Guido van Rossumb209a111997-04-29 18:18:01 +00003936static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003937build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003938{
Guido van Rossum7851eea2001-09-12 19:19:18 +00003939 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003940
3941 if (PyDict_Check(methods))
3942 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00003943 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00003944 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00003945 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
3946 base = PyTuple_GET_ITEM(bases, 0);
3947 metaclass = PyObject_GetAttrString(base, "__class__");
3948 if (metaclass == NULL) {
3949 PyErr_Clear();
3950 metaclass = (PyObject *)base->ob_type;
3951 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00003952 }
3953 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00003954 else {
3955 PyObject *g = PyEval_GetGlobals();
3956 if (g != NULL && PyDict_Check(g))
3957 metaclass = PyDict_GetItemString(g, "__metaclass__");
3958 if (metaclass == NULL)
3959 metaclass = (PyObject *) &PyClass_Type;
3960 Py_INCREF(metaclass);
3961 }
3962 result = PyObject_CallFunction(metaclass, "OOO", name, bases, methods);
3963 Py_DECREF(metaclass);
3964 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00003965}
3966
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003967static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003968exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
3969 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003970{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003971 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00003972 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003973 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003974
Guido van Rossumb209a111997-04-29 18:18:01 +00003975 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
3976 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003977 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00003978 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003979 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00003980 locals = PyTuple_GetItem(prog, 2);
3981 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003982 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003983 if (globals == Py_None) {
3984 globals = PyEval_GetGlobals();
3985 if (locals == Py_None) {
3986 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00003987 plain = 1;
3988 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003989 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003990 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003991 locals = globals;
Guido van Rossumb209a111997-04-29 18:18:01 +00003992 if (!PyString_Check(prog) &&
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00003993 !PyUnicode_Check(prog) &&
Guido van Rossumb209a111997-04-29 18:18:01 +00003994 !PyCode_Check(prog) &&
3995 !PyFile_Check(prog)) {
3996 PyErr_SetString(PyExc_TypeError,
Guido van Rossumac7be682001-01-17 15:42:30 +00003997 "exec: arg 1 must be a string, file, or code object");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003998 return -1;
3999 }
Fred Drake661ea262000-10-24 19:57:45 +00004000 if (!PyDict_Check(globals)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004001 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00004002 "exec: arg 2 must be a dictionary or None");
4003 return -1;
4004 }
4005 if (!PyDict_Check(locals)) {
4006 PyErr_SetString(PyExc_TypeError,
4007 "exec: arg 3 must be a dictionary or None");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004008 return -1;
4009 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004010 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00004011 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00004012 if (PyCode_Check(prog)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +00004013 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4014 PyErr_SetString(PyExc_TypeError,
4015 "code object passed to exec may not contain free variables");
4016 return -1;
4017 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004018 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004019 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004020 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004021 FILE *fp = PyFile_AsFile(prog);
4022 char *name = PyString_AsString(PyFile_Name(prog));
Tim Peters5ba58662001-07-16 02:29:45 +00004023 PyCompilerFlags cf;
4024 cf.cf_flags = 0;
4025 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004026 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
4027 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004028 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004029 v = PyRun_File(fp, name, Py_file_input, globals,
4030 locals);
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004031 }
4032 else {
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004033 PyObject *tmp = NULL;
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004034 char *str;
Tim Peters5ba58662001-07-16 02:29:45 +00004035 PyCompilerFlags cf;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004036 cf.cf_flags = 0;
4037#ifdef Py_USING_UNICODE
4038 if (PyUnicode_Check(prog)) {
4039 tmp = PyUnicode_AsUTF8String(prog);
4040 if (tmp == NULL)
4041 return -1;
4042 prog = tmp;
4043 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4044 }
4045#endif
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004046 if (PyString_AsStringAndSize(prog, &str, NULL))
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004047 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004048 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004049 v = PyRun_StringFlags(str, Py_file_input, globals,
4050 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004051 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004052 v = PyRun_String(str, Py_file_input, globals, locals);
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004053 Py_XDECREF(tmp);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004054 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004055 if (plain)
4056 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004057 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004058 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00004059 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004060 return 0;
4061}
Guido van Rossum24c13741995-02-14 09:42:43 +00004062
Guido van Rossumac7be682001-01-17 15:42:30 +00004063static void
Paul Prescode68140d2000-08-30 20:25:01 +00004064format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4065{
4066 char *obj_str;
4067
4068 if (!obj)
4069 return;
4070
4071 obj_str = PyString_AsString(obj);
4072 if (!obj_str)
4073 return;
4074
4075 PyErr_Format(exc, format_str, obj_str);
4076}
Guido van Rossum950361c1997-01-24 13:49:28 +00004077
4078#ifdef DYNAMIC_EXECUTION_PROFILE
4079
Skip Montanarof118cb12001-10-15 20:51:38 +00004080static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004081getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004082{
4083 int i;
4084 PyObject *l = PyList_New(256);
4085 if (l == NULL) return NULL;
4086 for (i = 0; i < 256; i++) {
4087 PyObject *x = PyInt_FromLong(a[i]);
4088 if (x == NULL) {
4089 Py_DECREF(l);
4090 return NULL;
4091 }
4092 PyList_SetItem(l, i, x);
4093 }
4094 for (i = 0; i < 256; i++)
4095 a[i] = 0;
4096 return l;
4097}
4098
4099PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004100_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004101{
4102#ifndef DXPAIRS
4103 return getarray(dxp);
4104#else
4105 int i;
4106 PyObject *l = PyList_New(257);
4107 if (l == NULL) return NULL;
4108 for (i = 0; i < 257; i++) {
4109 PyObject *x = getarray(dxpairs[i]);
4110 if (x == NULL) {
4111 Py_DECREF(l);
4112 return NULL;
4113 }
4114 PyList_SetItem(l, i, x);
4115 }
4116 return l;
4117#endif
4118}
4119
4120#endif