blob: 07862d0efed57c592fa1b3c063e98db363abb70b [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 Rossumb8b6d0c2003-06-28 21:53:52 +0000293static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
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) {
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000776 if (*next_instr == SETUP_FINALLY) {
777 /* Make the last opcode before
778 a try: finally: block uninterruptable. */
779 goto fast_next_opcode;
780 }
Skip Montanarod581d772002-09-03 20:10:45 +0000781 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000782 tstate->tick_counter++;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000783 if (things_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +0000784 if (Py_MakePendingCalls() < 0) {
785 why = WHY_EXCEPTION;
786 goto on_error;
787 }
788 }
Guido van Rossumdf0d00e1997-05-20 15:57:49 +0000789#if !defined(HAVE_SIGNAL_H) || defined(macintosh)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000790 /* If we have true signals, the signal handler
791 will call Py_AddPendingCall() so we don't
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000792 have to call PyErr_CheckSignals(). On the
793 Mac and DOS, alas, we have to call it. */
Guido van Rossumb209a111997-04-29 18:18:01 +0000794 if (PyErr_CheckSignals()) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000795 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +0000796 goto on_error;
797 }
Guido van Rossum70d44781997-01-21 06:15:24 +0000798#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000799
Guido van Rossume59214e1994-08-30 08:01:59 +0000800#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000801 if (interpreter_lock) {
802 /* Give another thread a chance */
803
Guido van Rossum25ce5661997-08-02 03:10:38 +0000804 if (PyThreadState_Swap(NULL) != tstate)
805 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000806 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000807
808 /* Other threads may run now */
809
Guido van Rossum65d5b571998-12-21 19:32:43 +0000810 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000811 if (PyThreadState_Swap(tstate) != NULL)
812 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000813
814 /* Check for thread interrupts */
815
816 if (tstate->async_exc != NULL) {
817 x = tstate->async_exc;
818 tstate->async_exc = NULL;
819 PyErr_SetNone(x);
820 Py_DECREF(x);
821 why = WHY_EXCEPTION;
822 goto on_error;
823 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000824 }
825#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000826 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000827
Neil Schemenauer63543862002-02-17 19:10:14 +0000828 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +0000829 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +0000830
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000831 /* line-by-line tracing support */
832
833 if (tstate->c_tracefunc != NULL && !tstate->tracing) {
834 /* see maybe_call_line_trace
835 for expository comments */
836 f->f_stacktop = stack_pointer;
Michael W. Hudson006c7522002-11-08 13:08:46 +0000837
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000838 err = maybe_call_line_trace(tstate->c_tracefunc,
839 tstate->c_traceobj,
840 f, &instr_lb, &instr_ub);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000841 /* Reload possibly changed frame fields */
842 JUMPTO(f->f_lasti);
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000843 if (f->f_stacktop != NULL) {
844 stack_pointer = f->f_stacktop;
845 f->f_stacktop = NULL;
846 }
847 if (err) {
848 /* trace function raised an exception */
849 goto on_error;
850 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000851 }
852
853 /* Extract opcode and argument */
854
Guido van Rossum374a9221991-04-04 10:40:29 +0000855 opcode = NEXTOP();
856 if (HAS_ARG(opcode))
857 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +0000858 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +0000859#ifdef DYNAMIC_EXECUTION_PROFILE
860#ifdef DXPAIRS
861 dxpairs[lastopcode][opcode]++;
862 lastopcode = opcode;
863#endif
864 dxp[opcode]++;
865#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000866
Guido van Rossum96a42c81992-01-12 02:29:51 +0000867#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +0000868 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +0000869
Guido van Rossum96a42c81992-01-12 02:29:51 +0000870 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000871 if (HAS_ARG(opcode)) {
872 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000873 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +0000874 }
875 else {
876 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000877 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +0000878 }
879 }
880#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000881
Guido van Rossum374a9221991-04-04 10:40:29 +0000882 /* Main switch on opcode */
Jeremy Hylton52820442001-01-03 23:52:36 +0000883
Guido van Rossum374a9221991-04-04 10:40:29 +0000884 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +0000885
Guido van Rossum374a9221991-04-04 10:40:29 +0000886 /* BEWARE!
887 It is essential that any operation that fails sets either
888 x to NULL, err to nonzero, or why to anything but WHY_NOT,
889 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000890
Guido van Rossum374a9221991-04-04 10:40:29 +0000891 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000892
Neil Schemenauer63543862002-02-17 19:10:14 +0000893 case LOAD_FAST:
894 x = GETLOCAL(oparg);
895 if (x != NULL) {
896 Py_INCREF(x);
897 PUSH(x);
898 goto fast_next_opcode;
899 }
900 format_exc_check_arg(PyExc_UnboundLocalError,
901 UNBOUNDLOCAL_ERROR_MSG,
902 PyTuple_GetItem(co->co_varnames, oparg));
903 break;
904
905 case LOAD_CONST:
Skip Montanaro04d80f82002-08-04 21:03:35 +0000906 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +0000907 Py_INCREF(x);
908 PUSH(x);
909 goto fast_next_opcode;
910
Raymond Hettinger7dc52212003-03-16 20:14:44 +0000911 PREDICTED_WITH_ARG(STORE_FAST);
Neil Schemenauer63543862002-02-17 19:10:14 +0000912 case STORE_FAST:
913 v = POP();
914 SETLOCAL(oparg, v);
915 goto fast_next_opcode;
916
Raymond Hettingerf606f872003-03-16 03:11:04 +0000917 PREDICTED(POP_TOP);
Guido van Rossum374a9221991-04-04 10:40:29 +0000918 case POP_TOP:
919 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000920 Py_DECREF(v);
Neil Schemenauer63543862002-02-17 19:10:14 +0000921 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000922
Guido van Rossum374a9221991-04-04 10:40:29 +0000923 case ROT_TWO:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000924 v = TOP();
925 w = SECOND();
926 SET_TOP(w);
927 SET_SECOND(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000928 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000929
Guido van Rossum374a9221991-04-04 10:40:29 +0000930 case ROT_THREE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000931 v = TOP();
932 w = SECOND();
933 x = THIRD();
934 SET_TOP(w);
935 SET_SECOND(x);
936 SET_THIRD(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000937 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000938
Thomas Wouters434d0822000-08-24 20:11:32 +0000939 case ROT_FOUR:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000940 u = TOP();
941 v = SECOND();
942 w = THIRD();
943 x = FOURTH();
944 SET_TOP(v);
945 SET_SECOND(w);
946 SET_THIRD(x);
947 SET_FOURTH(u);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000948 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000949
Guido van Rossum374a9221991-04-04 10:40:29 +0000950 case DUP_TOP:
951 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000952 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000953 PUSH(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000954 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000955
Thomas Wouters434d0822000-08-24 20:11:32 +0000956 case DUP_TOPX:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000957 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000958 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000959 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000960 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000961 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000962 STACKADJ(2);
963 SET_TOP(x);
964 SET_SECOND(w);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000965 goto fast_next_opcode;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000966 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000967 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000968 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000969 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000970 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000971 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +0000972 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000973 STACKADJ(3);
974 SET_TOP(x);
975 SET_SECOND(w);
976 SET_THIRD(v);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000977 goto fast_next_opcode;
Thomas Wouters434d0822000-08-24 20:11:32 +0000978 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000979 Py_FatalError("invalid argument to DUP_TOPX"
980 " (bytecode corruption?)");
Tim Peters35ba6892000-10-11 07:04:49 +0000981 break;
Thomas Wouters434d0822000-08-24 20:11:32 +0000982
Guido van Rossum374a9221991-04-04 10:40:29 +0000983 case UNARY_POSITIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000984 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000985 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000986 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000987 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000988 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000989 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000990
Guido van Rossum374a9221991-04-04 10:40:29 +0000991 case UNARY_NEGATIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000992 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000993 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000994 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000995 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000996 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000997 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000998
Guido van Rossum374a9221991-04-04 10:40:29 +0000999 case UNARY_NOT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001000 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001001 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001002 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +00001003 if (err == 0) {
1004 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001005 SET_TOP(Py_True);
Guido van Rossumfc490731997-05-06 15:06:49 +00001006 continue;
1007 }
1008 else if (err > 0) {
1009 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001010 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +00001011 err = 0;
1012 continue;
1013 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00001014 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001015 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001016
Guido van Rossum374a9221991-04-04 10:40:29 +00001017 case UNARY_CONVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001018 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001019 x = PyObject_Repr(v);
1020 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001021 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001022 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001023 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001024
Guido van Rossum7928cd71991-10-24 14:59:31 +00001025 case UNARY_INVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001026 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001027 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001028 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001029 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001030 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001031 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001032
Guido van Rossum50564e81996-01-12 01:13:16 +00001033 case BINARY_POWER:
1034 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001035 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001036 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +00001037 Py_DECREF(v);
1038 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001039 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001040 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +00001041 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001042
Guido van Rossum374a9221991-04-04 10:40:29 +00001043 case BINARY_MULTIPLY:
1044 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001045 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001046 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001047 Py_DECREF(v);
1048 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001049 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001050 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001051 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001052
Guido van Rossum374a9221991-04-04 10:40:29 +00001053 case BINARY_DIVIDE:
Tim Peters3caca232001-12-06 06:23:26 +00001054 if (!_Py_QnewFlag) {
1055 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001056 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001057 x = PyNumber_Divide(v, w);
1058 Py_DECREF(v);
1059 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001060 SET_TOP(x);
Tim Peters3caca232001-12-06 06:23:26 +00001061 if (x != NULL) continue;
1062 break;
1063 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001064 /* -Qnew is in effect: fall through to
Tim Peters3caca232001-12-06 06:23:26 +00001065 BINARY_TRUE_DIVIDE */
1066 case BINARY_TRUE_DIVIDE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001067 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001068 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001069 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001070 Py_DECREF(v);
1071 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001072 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001073 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001074 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001075
Guido van Rossum4668b002001-08-08 05:00:18 +00001076 case BINARY_FLOOR_DIVIDE:
1077 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001078 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001079 x = PyNumber_FloorDivide(v, w);
1080 Py_DECREF(v);
1081 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001082 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001083 if (x != NULL) continue;
1084 break;
1085
Guido van Rossum374a9221991-04-04 10:40:29 +00001086 case BINARY_MODULO:
1087 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001088 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001089 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001090 Py_DECREF(v);
1091 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001092 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001093 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001094 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001095
Guido van Rossum374a9221991-04-04 10:40:29 +00001096 case BINARY_ADD:
1097 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001098 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001099 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001100 /* INLINE: int + int */
1101 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001102 a = PyInt_AS_LONG(v);
1103 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001104 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001105 if ((i^a) < 0 && (i^b) < 0)
1106 goto slow_add;
1107 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001108 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001109 else {
1110 slow_add:
Guido van Rossumc12da691997-07-17 23:12:42 +00001111 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001112 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001113 Py_DECREF(v);
1114 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001115 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001116 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001117 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001118
Guido van Rossum374a9221991-04-04 10:40:29 +00001119 case BINARY_SUBTRACT:
1120 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001121 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001122 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001123 /* INLINE: int - int */
1124 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001125 a = PyInt_AS_LONG(v);
1126 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001127 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001128 if ((i^a) < 0 && (i^~b) < 0)
1129 goto slow_sub;
1130 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001131 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001132 else {
1133 slow_sub:
Guido van Rossumc12da691997-07-17 23:12:42 +00001134 x = PyNumber_Subtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001135 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001136 Py_DECREF(v);
1137 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001138 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001139 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001140 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001141
Guido van Rossum374a9221991-04-04 10:40:29 +00001142 case BINARY_SUBSCR:
1143 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001144 v = TOP();
Tim Petersb1c46982001-10-05 20:41:38 +00001145 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001146 /* INLINE: list[int] */
1147 long i = PyInt_AsLong(w);
1148 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +00001149 i += PyList_GET_SIZE(v);
Guido van Rossumc12da691997-07-17 23:12:42 +00001150 if (i < 0 ||
Guido van Rossumfa00e951998-07-08 15:02:37 +00001151 i >= PyList_GET_SIZE(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001152 PyErr_SetString(PyExc_IndexError,
1153 "list index out of range");
1154 x = NULL;
1155 }
1156 else {
Guido van Rossumfa00e951998-07-08 15:02:37 +00001157 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001158 Py_INCREF(x);
1159 }
1160 }
1161 else
1162 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001163 Py_DECREF(v);
1164 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001165 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001166 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001167 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001168
Guido van Rossum7928cd71991-10-24 14:59:31 +00001169 case BINARY_LSHIFT:
1170 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001171 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001172 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001173 Py_DECREF(v);
1174 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001175 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001176 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001177 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001178
Guido van Rossum7928cd71991-10-24 14:59:31 +00001179 case BINARY_RSHIFT:
1180 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001181 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001182 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001183 Py_DECREF(v);
1184 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001185 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001186 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001187 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001188
Guido van Rossum7928cd71991-10-24 14:59:31 +00001189 case BINARY_AND:
1190 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001191 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001192 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001193 Py_DECREF(v);
1194 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001195 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001196 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001197 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001198
Guido van Rossum7928cd71991-10-24 14:59:31 +00001199 case BINARY_XOR:
1200 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001201 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001202 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001203 Py_DECREF(v);
1204 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001205 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001206 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001207 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001208
Guido van Rossum7928cd71991-10-24 14:59:31 +00001209 case BINARY_OR:
1210 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001211 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001212 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001213 Py_DECREF(v);
1214 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001215 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001216 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001217 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001218
1219 case INPLACE_POWER:
1220 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001221 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001222 x = PyNumber_InPlacePower(v, w, Py_None);
1223 Py_DECREF(v);
1224 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001225 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001226 if (x != NULL) continue;
1227 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001228
Thomas Wouters434d0822000-08-24 20:11:32 +00001229 case INPLACE_MULTIPLY:
1230 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001231 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001232 x = PyNumber_InPlaceMultiply(v, w);
1233 Py_DECREF(v);
1234 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001235 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001236 if (x != NULL) continue;
1237 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001238
Thomas Wouters434d0822000-08-24 20:11:32 +00001239 case INPLACE_DIVIDE:
Tim Peters54b11912001-12-25 18:49:11 +00001240 if (!_Py_QnewFlag) {
1241 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001242 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001243 x = PyNumber_InPlaceDivide(v, w);
1244 Py_DECREF(v);
1245 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001246 SET_TOP(x);
Tim Peters54b11912001-12-25 18:49:11 +00001247 if (x != NULL) continue;
1248 break;
1249 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001250 /* -Qnew is in effect: fall through to
Tim Peters54b11912001-12-25 18:49:11 +00001251 INPLACE_TRUE_DIVIDE */
1252 case INPLACE_TRUE_DIVIDE:
Thomas Wouters434d0822000-08-24 20:11:32 +00001253 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001254 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001255 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001256 Py_DECREF(v);
1257 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001258 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001259 if (x != NULL) continue;
1260 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001261
Guido van Rossum4668b002001-08-08 05:00:18 +00001262 case INPLACE_FLOOR_DIVIDE:
1263 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001264 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001265 x = PyNumber_InPlaceFloorDivide(v, w);
1266 Py_DECREF(v);
1267 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001268 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001269 if (x != NULL) continue;
1270 break;
1271
Thomas Wouters434d0822000-08-24 20:11:32 +00001272 case INPLACE_MODULO:
1273 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001274 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001275 x = PyNumber_InPlaceRemainder(v, w);
1276 Py_DECREF(v);
1277 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001278 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001279 if (x != NULL) continue;
1280 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001281
Thomas Wouters434d0822000-08-24 20:11:32 +00001282 case INPLACE_ADD:
1283 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001284 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001285 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001286 /* INLINE: int + int */
1287 register long a, b, i;
1288 a = PyInt_AS_LONG(v);
1289 b = PyInt_AS_LONG(w);
1290 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001291 if ((i^a) < 0 && (i^b) < 0)
1292 goto slow_iadd;
1293 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001294 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001295 else {
1296 slow_iadd:
Thomas Wouters434d0822000-08-24 20:11:32 +00001297 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001298 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001299 Py_DECREF(v);
1300 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001301 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001302 if (x != NULL) continue;
1303 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001304
Thomas Wouters434d0822000-08-24 20:11:32 +00001305 case INPLACE_SUBTRACT:
1306 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001307 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001308 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001309 /* INLINE: int - int */
1310 register long a, b, i;
1311 a = PyInt_AS_LONG(v);
1312 b = PyInt_AS_LONG(w);
1313 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001314 if ((i^a) < 0 && (i^~b) < 0)
1315 goto slow_isub;
1316 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001317 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001318 else {
1319 slow_isub:
Thomas Wouters434d0822000-08-24 20:11:32 +00001320 x = PyNumber_InPlaceSubtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001321 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001322 Py_DECREF(v);
1323 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001324 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001325 if (x != NULL) continue;
1326 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001327
Thomas Wouters434d0822000-08-24 20:11:32 +00001328 case INPLACE_LSHIFT:
1329 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001330 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001331 x = PyNumber_InPlaceLshift(v, w);
1332 Py_DECREF(v);
1333 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001334 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001335 if (x != NULL) continue;
1336 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001337
Thomas Wouters434d0822000-08-24 20:11:32 +00001338 case INPLACE_RSHIFT:
1339 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001340 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001341 x = PyNumber_InPlaceRshift(v, w);
1342 Py_DECREF(v);
1343 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001344 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001345 if (x != NULL) continue;
1346 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001347
Thomas Wouters434d0822000-08-24 20:11:32 +00001348 case INPLACE_AND:
1349 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001350 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001351 x = PyNumber_InPlaceAnd(v, w);
1352 Py_DECREF(v);
1353 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001354 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001355 if (x != NULL) continue;
1356 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001357
Thomas Wouters434d0822000-08-24 20:11:32 +00001358 case INPLACE_XOR:
1359 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001360 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001361 x = PyNumber_InPlaceXor(v, w);
1362 Py_DECREF(v);
1363 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001364 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001365 if (x != NULL) continue;
1366 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001367
Thomas Wouters434d0822000-08-24 20:11:32 +00001368 case INPLACE_OR:
1369 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001370 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001371 x = PyNumber_InPlaceOr(v, w);
1372 Py_DECREF(v);
1373 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001374 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001375 if (x != NULL) continue;
1376 break;
1377
Guido van Rossum374a9221991-04-04 10:40:29 +00001378 case SLICE+0:
1379 case SLICE+1:
1380 case SLICE+2:
1381 case SLICE+3:
1382 if ((opcode-SLICE) & 2)
1383 w = POP();
1384 else
1385 w = NULL;
1386 if ((opcode-SLICE) & 1)
1387 v = POP();
1388 else
1389 v = NULL;
Raymond Hettinger663004b2003-01-09 15:24:30 +00001390 u = TOP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001391 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001392 Py_DECREF(u);
1393 Py_XDECREF(v);
1394 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001395 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001396 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001397 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001398
Guido van Rossum374a9221991-04-04 10:40:29 +00001399 case STORE_SLICE+0:
1400 case STORE_SLICE+1:
1401 case STORE_SLICE+2:
1402 case STORE_SLICE+3:
1403 if ((opcode-STORE_SLICE) & 2)
1404 w = POP();
1405 else
1406 w = NULL;
1407 if ((opcode-STORE_SLICE) & 1)
1408 v = POP();
1409 else
1410 v = NULL;
1411 u = POP();
1412 t = POP();
1413 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +00001414 Py_DECREF(t);
1415 Py_DECREF(u);
1416 Py_XDECREF(v);
1417 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001418 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001419 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001420
Guido van Rossum374a9221991-04-04 10:40:29 +00001421 case DELETE_SLICE+0:
1422 case DELETE_SLICE+1:
1423 case DELETE_SLICE+2:
1424 case DELETE_SLICE+3:
1425 if ((opcode-DELETE_SLICE) & 2)
1426 w = POP();
1427 else
1428 w = NULL;
1429 if ((opcode-DELETE_SLICE) & 1)
1430 v = POP();
1431 else
1432 v = NULL;
1433 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001434 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +00001435 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00001436 Py_DECREF(u);
1437 Py_XDECREF(v);
1438 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001439 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001440 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001441
Guido van Rossum374a9221991-04-04 10:40:29 +00001442 case STORE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001443 w = TOP();
1444 v = SECOND();
1445 u = THIRD();
1446 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001447 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001448 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001449 Py_DECREF(u);
1450 Py_DECREF(v);
1451 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001452 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001453 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001454
Guido van Rossum374a9221991-04-04 10:40:29 +00001455 case DELETE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001456 w = TOP();
1457 v = SECOND();
1458 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001459 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001460 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001461 Py_DECREF(v);
1462 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001463 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001464 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001465
Guido van Rossum374a9221991-04-04 10:40:29 +00001466 case PRINT_EXPR:
1467 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001468 w = PySys_GetObject("displayhook");
1469 if (w == NULL) {
1470 PyErr_SetString(PyExc_RuntimeError,
1471 "lost sys.displayhook");
1472 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001473 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001474 }
1475 if (err == 0) {
1476 x = Py_BuildValue("(O)", v);
1477 if (x == NULL)
1478 err = -1;
1479 }
1480 if (err == 0) {
1481 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001482 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001483 if (w == NULL)
1484 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001485 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001486 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001487 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001488 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001489
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001490 case PRINT_ITEM_TO:
1491 w = stream = POP();
1492 /* fall through to PRINT_ITEM */
1493
Guido van Rossum374a9221991-04-04 10:40:29 +00001494 case PRINT_ITEM:
1495 v = POP();
Barry Warsaw093abe02000-08-29 04:56:13 +00001496 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001497 w = PySys_GetObject("stdout");
1498 if (w == NULL) {
1499 PyErr_SetString(PyExc_RuntimeError,
1500 "lost sys.stdout");
1501 err = -1;
1502 }
Guido van Rossum8f183201997-12-31 05:53:15 +00001503 }
Tim Peters8e5fd532002-03-24 19:25:00 +00001504 if (w != NULL && PyFile_SoftSpace(w, 0))
Guido van Rossumbe270261997-05-22 22:26:18 +00001505 err = PyFile_WriteString(" ", w);
1506 if (err == 0)
1507 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001508 if (err == 0) {
Tim Peters8e5fd532002-03-24 19:25:00 +00001509 /* XXX move into writeobject() ? */
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001510 if (PyString_Check(v)) {
1511 char *s = PyString_AS_STRING(v);
1512 int len = PyString_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001513 if (len == 0 ||
1514 !isspace(Py_CHARMASK(s[len-1])) ||
1515 s[len-1] == ' ')
1516 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001517 }
Martin v. Löwis8d3ce5a2001-12-18 22:36:40 +00001518#ifdef Py_USING_UNICODE
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001519 else if (PyUnicode_Check(v)) {
1520 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
1521 int len = PyUnicode_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001522 if (len == 0 ||
1523 !Py_UNICODE_ISSPACE(s[len-1]) ||
1524 s[len-1] == ' ')
1525 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001526 }
Michael W. Hudsond95c8282002-05-20 13:56:11 +00001527#endif
Tim Peters8e5fd532002-03-24 19:25:00 +00001528 else
1529 PyFile_SoftSpace(w, 1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001530 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001531 Py_DECREF(v);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001532 Py_XDECREF(stream);
1533 stream = NULL;
1534 if (err == 0)
1535 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001536 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001537
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001538 case PRINT_NEWLINE_TO:
1539 w = stream = POP();
1540 /* fall through to PRINT_NEWLINE */
1541
Guido van Rossum374a9221991-04-04 10:40:29 +00001542 case PRINT_NEWLINE:
Barry Warsaw093abe02000-08-29 04:56:13 +00001543 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001544 w = PySys_GetObject("stdout");
1545 if (w == NULL)
1546 PyErr_SetString(PyExc_RuntimeError,
1547 "lost sys.stdout");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001548 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001549 if (w != NULL) {
1550 err = PyFile_WriteString("\n", w);
1551 if (err == 0)
1552 PyFile_SoftSpace(w, 0);
1553 }
1554 Py_XDECREF(stream);
1555 stream = NULL;
Guido van Rossum374a9221991-04-04 10:40:29 +00001556 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001557
Thomas Wouters434d0822000-08-24 20:11:32 +00001558
1559#ifdef CASE_TOO_BIG
1560 default: switch (opcode) {
1561#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001562 case BREAK_LOOP:
1563 why = WHY_BREAK;
1564 break;
Guido van Rossum66b0e9c2001-03-21 19:17:22 +00001565
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00001566 case CONTINUE_LOOP:
1567 retval = PyInt_FromLong(oparg);
1568 why = WHY_CONTINUE;
1569 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001570
Guido van Rossumf10570b1995-07-07 22:53:21 +00001571 case RAISE_VARARGS:
1572 u = v = w = NULL;
1573 switch (oparg) {
1574 case 3:
1575 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001576 /* Fallthrough */
1577 case 2:
1578 v = POP(); /* value */
1579 /* Fallthrough */
1580 case 1:
1581 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001582 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001583 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001584 break;
1585 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001586 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001587 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001588 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001589 break;
1590 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001591 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001592
Guido van Rossum374a9221991-04-04 10:40:29 +00001593 case LOAD_LOCALS:
Guido van Rossum681d79a1995-07-18 14:51:37 +00001594 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001595 PyErr_SetString(PyExc_SystemError,
1596 "no locals");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001597 break;
1598 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001599 Py_INCREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001600 PUSH(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001601 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001602
Guido van Rossum374a9221991-04-04 10:40:29 +00001603 case RETURN_VALUE:
1604 retval = POP();
1605 why = WHY_RETURN;
1606 break;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001607
Tim Peters5ca576e2001-06-18 22:08:13 +00001608 case YIELD_VALUE:
1609 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001610 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001611 why = WHY_YIELD;
1612 break;
1613
1614
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001615 case EXEC_STMT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001616 w = TOP();
1617 v = SECOND();
1618 u = THIRD();
1619 STACKADJ(-3);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001620 err = exec_statement(f, u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001621 Py_DECREF(u);
1622 Py_DECREF(v);
1623 Py_DECREF(w);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001624 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001625
Guido van Rossum374a9221991-04-04 10:40:29 +00001626 case POP_BLOCK:
1627 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001628 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001629 while (STACK_LEVEL() > b->b_level) {
1630 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001631 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001632 }
1633 }
1634 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001635
Guido van Rossum374a9221991-04-04 10:40:29 +00001636 case END_FINALLY:
1637 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001638 if (PyInt_Check(v)) {
Raymond Hettinger080cb322003-03-14 01:37:42 +00001639 why = (enum why_code) PyInt_AS_LONG(v);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00001640 if (why == WHY_RETURN ||
Tim Peters5ca576e2001-06-18 22:08:13 +00001641 why == WHY_YIELD ||
Guido van Rossumc5fe5eb2002-06-12 03:45:21 +00001642 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001643 retval = POP();
1644 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001645 else if (PyString_Check(v) || PyClass_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001646 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001647 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001648 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001649 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001650 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001651 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001652 else if (v != Py_None) {
1653 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001654 "'finally' pops bad exception");
1655 why = WHY_EXCEPTION;
1656 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001657 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001658 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001659
Guido van Rossum374a9221991-04-04 10:40:29 +00001660 case BUILD_CLASS:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001661 u = TOP();
1662 v = SECOND();
1663 w = THIRD();
1664 STACKADJ(-2);
Guido van Rossum25831651993-05-19 14:50:45 +00001665 x = build_class(u, v, w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001666 SET_TOP(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001667 Py_DECREF(u);
1668 Py_DECREF(v);
1669 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001670 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001671
Guido van Rossum374a9221991-04-04 10:40:29 +00001672 case STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001673 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001674 v = POP();
Guido van Rossum681d79a1995-07-18 14:51:37 +00001675 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001676 PyErr_Format(PyExc_SystemError,
1677 "no locals found when storing %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001678 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001679 break;
1680 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001681 err = PyDict_SetItem(x, w, v);
1682 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001683 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001684
Guido van Rossum374a9221991-04-04 10:40:29 +00001685 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001686 w = GETITEM(names, oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001687 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001688 PyErr_Format(PyExc_SystemError,
1689 "no locals when deleting %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001690 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001691 break;
1692 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001693 if ((err = PyDict_DelItem(x, w)) != 0)
Guido van Rossumac7be682001-01-17 15:42:30 +00001694 format_exc_check_arg(PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001695 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001696 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001697
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001698 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001699 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001700 v = POP();
Raymond Hettinger21012b82003-02-26 18:11:50 +00001701 if (PyTuple_CheckExact(v)) {
Barry Warsawe42b18f1997-08-25 22:13:04 +00001702 if (PyTuple_Size(v) != oparg) {
1703 PyErr_SetString(PyExc_ValueError,
1704 "unpack tuple of wrong size");
1705 why = WHY_EXCEPTION;
1706 }
1707 else {
1708 for (; --oparg >= 0; ) {
1709 w = PyTuple_GET_ITEM(v, oparg);
1710 Py_INCREF(w);
1711 PUSH(w);
1712 }
1713 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001714 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00001715 else if (PyList_CheckExact(v)) {
Barry Warsawe42b18f1997-08-25 22:13:04 +00001716 if (PyList_Size(v) != oparg) {
1717 PyErr_SetString(PyExc_ValueError,
1718 "unpack list of wrong size");
1719 why = WHY_EXCEPTION;
1720 }
1721 else {
1722 for (; --oparg >= 0; ) {
1723 w = PyList_GET_ITEM(v, oparg);
1724 Py_INCREF(w);
1725 PUSH(w);
1726 }
1727 }
1728 }
Tim Petersd6d010b2001-06-21 02:49:55 +00001729 else if (unpack_iterable(v, oparg,
1730 stack_pointer + oparg))
1731 stack_pointer += oparg;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001732 else {
1733 if (PyErr_ExceptionMatches(PyExc_TypeError))
1734 PyErr_SetString(PyExc_TypeError,
1735 "unpack non-sequence");
Barry Warsawe42b18f1997-08-25 22:13:04 +00001736 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001737 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001738 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001739 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001740
Guido van Rossum374a9221991-04-04 10:40:29 +00001741 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001742 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001743 v = TOP();
1744 u = SECOND();
1745 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001746 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1747 Py_DECREF(v);
1748 Py_DECREF(u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001749 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001750
Guido van Rossum374a9221991-04-04 10:40:29 +00001751 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001752 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001753 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001754 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1755 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001756 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001757 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001758
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001759 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001760 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001761 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001762 err = PyDict_SetItem(f->f_globals, w, v);
1763 Py_DECREF(v);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001764 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001765
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001766 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001767 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001768 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001769 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001770 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001771 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001772
Guido van Rossum374a9221991-04-04 10:40:29 +00001773 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001774 w = GETITEM(names, oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001775 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001776 PyErr_Format(PyExc_SystemError,
1777 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001778 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001779 break;
1780 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001781 x = PyDict_GetItem(x, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001782 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001783 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001784 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001785 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001786 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001787 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001788 PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001789 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001790 break;
1791 }
1792 }
1793 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001794 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001795 PUSH(x);
1796 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001797
Guido van Rossum374a9221991-04-04 10:40:29 +00001798 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001799 w = GETITEM(names, oparg);
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001800 if (PyString_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00001801 /* Inline the PyDict_GetItem() calls.
1802 WARNING: this is an extreme speed hack.
1803 Do not try this at home. */
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001804 long hash = ((PyStringObject *)w)->ob_shash;
1805 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001806 PyDictObject *d;
1807 d = (PyDictObject *)(f->f_globals);
1808 x = d->ma_lookup(d, w, hash)->me_value;
1809 if (x != NULL) {
1810 Py_INCREF(x);
1811 PUSH(x);
1812 continue;
1813 }
1814 d = (PyDictObject *)(f->f_builtins);
1815 x = d->ma_lookup(d, w, hash)->me_value;
1816 if (x != NULL) {
1817 Py_INCREF(x);
1818 PUSH(x);
1819 continue;
1820 }
1821 goto load_global_error;
1822 }
1823 }
1824 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00001825 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001826 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001827 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001828 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001829 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00001830 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001831 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001832 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001833 break;
1834 }
1835 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001836 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001837 PUSH(x);
1838 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001839
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001840 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001841 x = GETLOCAL(oparg);
1842 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001843 format_exc_check_arg(
1844 PyExc_UnboundLocalError,
1845 UNBOUNDLOCAL_ERROR_MSG,
1846 PyTuple_GetItem(co->co_varnames, oparg)
1847 );
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001848 break;
1849 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00001850 SETLOCAL(oparg, NULL);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001851 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001852
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001853 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001854 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001855 Py_INCREF(x);
1856 PUSH(x);
1857 break;
1858
1859 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001860 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001861 w = PyCell_Get(x);
Jeremy Hylton2524d692001-02-05 17:23:16 +00001862 if (w == NULL) {
Jeremy Hylton76c81ee2002-07-11 16:56:38 +00001863 err = -1;
1864 /* Don't stomp existing exception */
1865 if (PyErr_Occurred())
1866 break;
Jeremy Hyltonc76770c2001-04-13 16:51:46 +00001867 if (oparg < f->f_ncells) {
Jeremy Hylton2524d692001-02-05 17:23:16 +00001868 v = PyTuple_GetItem(co->co_cellvars,
1869 oparg);
Jeremy Hyltonc76770c2001-04-13 16:51:46 +00001870 format_exc_check_arg(
1871 PyExc_UnboundLocalError,
1872 UNBOUNDLOCAL_ERROR_MSG,
1873 v);
1874 } else {
Jeremy Hylton2524d692001-02-05 17:23:16 +00001875 v = PyTuple_GetItem(
1876 co->co_freevars,
1877 oparg - f->f_ncells);
Jeremy Hyltonc76770c2001-04-13 16:51:46 +00001878 format_exc_check_arg(
1879 PyExc_NameError,
1880 UNBOUNDFREE_ERROR_MSG,
1881 v);
1882 }
Jeremy Hylton2524d692001-02-05 17:23:16 +00001883 break;
1884 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001885 PUSH(w);
1886 break;
1887
1888 case STORE_DEREF:
1889 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001890 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001891 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00001892 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001893 continue;
1894
Guido van Rossum374a9221991-04-04 10:40:29 +00001895 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001896 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001897 if (x != NULL) {
1898 for (; --oparg >= 0;) {
1899 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001900 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001901 }
1902 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001903 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001904 }
1905 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001906
Guido van Rossum374a9221991-04-04 10:40:29 +00001907 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00001908 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001909 if (x != NULL) {
1910 for (; --oparg >= 0;) {
1911 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00001912 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001913 }
1914 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001915 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001916 }
1917 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001918
Guido van Rossum374a9221991-04-04 10:40:29 +00001919 case BUILD_MAP:
Guido van Rossumb209a111997-04-29 18:18:01 +00001920 x = PyDict_New();
Guido van Rossum374a9221991-04-04 10:40:29 +00001921 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001922 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001923 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001924
Guido van Rossum374a9221991-04-04 10:40:29 +00001925 case LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001926 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001927 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001928 x = PyObject_GetAttr(v, w);
1929 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001930 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001931 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001932 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001933
Guido van Rossum374a9221991-04-04 10:40:29 +00001934 case COMPARE_OP:
1935 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001936 v = TOP();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001937 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001938 /* INLINE: cmp(int, int) */
1939 register long a, b;
1940 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001941 a = PyInt_AS_LONG(v);
1942 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001943 switch (oparg) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00001944 case PyCmp_LT: res = a < b; break;
1945 case PyCmp_LE: res = a <= b; break;
1946 case PyCmp_EQ: res = a == b; break;
1947 case PyCmp_NE: res = a != b; break;
1948 case PyCmp_GT: res = a > b; break;
1949 case PyCmp_GE: res = a >= b; break;
1950 case PyCmp_IS: res = v == w; break;
1951 case PyCmp_IS_NOT: res = v != w; break;
Guido van Rossumc12da691997-07-17 23:12:42 +00001952 default: goto slow_compare;
1953 }
1954 x = res ? Py_True : Py_False;
1955 Py_INCREF(x);
1956 }
1957 else {
1958 slow_compare:
1959 x = cmp_outcome(oparg, v, w);
1960 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001961 Py_DECREF(v);
1962 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001963 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001964 if (x == NULL) break;
1965 PREDICT(JUMP_IF_FALSE);
1966 PREDICT(JUMP_IF_TRUE);
1967 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001968
Guido van Rossum374a9221991-04-04 10:40:29 +00001969 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001970 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001971 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001972 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001973 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00001974 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001975 break;
1976 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001977 u = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001978 w = Py_BuildValue("(OOOO)",
Guido van Rossum681d79a1995-07-18 14:51:37 +00001979 w,
1980 f->f_globals,
Guido van Rossuma027efa1997-05-05 20:56:21 +00001981 f->f_locals == NULL ?
1982 Py_None : f->f_locals,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001983 u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001984 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001985 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001986 u = POP();
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001987 x = NULL;
1988 break;
1989 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001990 x = PyEval_CallObject(x, w);
1991 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001992 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001993 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001994 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001995
Thomas Wouters52152252000-08-17 22:55:00 +00001996 case IMPORT_STAR:
1997 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001998 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001999 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002000 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00002001 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002002 break;
2003 }
Thomas Wouters52152252000-08-17 22:55:00 +00002004 err = import_all_from(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002005 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00002006 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002007 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002008 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002009
Thomas Wouters52152252000-08-17 22:55:00 +00002010 case IMPORT_FROM:
Skip Montanaro496e6582002-08-06 17:47:40 +00002011 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00002012 v = TOP();
2013 x = import_from(v, w);
2014 PUSH(x);
2015 if (x != NULL) continue;
2016 break;
2017
Guido van Rossum374a9221991-04-04 10:40:29 +00002018 case JUMP_FORWARD:
2019 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002020 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002021
Raymond Hettingerf606f872003-03-16 03:11:04 +00002022 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002023 case JUMP_IF_FALSE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002024 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002025 if (w == Py_True) {
2026 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002027 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002028 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002029 if (w == Py_False) {
2030 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002031 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002032 }
2033 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002034 if (err > 0)
2035 err = 0;
2036 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00002037 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002038 else
2039 break;
2040 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002041
Raymond Hettingerf606f872003-03-16 03:11:04 +00002042 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002043 case JUMP_IF_TRUE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002044 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002045 if (w == Py_False) {
2046 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002047 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002048 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002049 if (w == Py_True) {
2050 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002051 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002052 }
2053 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002054 if (err > 0) {
2055 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00002056 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002057 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002058 else if (err == 0)
2059 ;
2060 else
2061 break;
2062 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002063
Guido van Rossum374a9221991-04-04 10:40:29 +00002064 case JUMP_ABSOLUTE:
2065 JUMPTO(oparg);
Neil Schemenauerca2a2f12003-05-30 23:59:44 +00002066 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002067
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002068 case GET_ITER:
2069 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002070 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002071 x = PyObject_GetIter(v);
2072 Py_DECREF(v);
2073 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002074 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002075 PREDICT(FOR_ITER);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002076 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002077 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002078 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002079 break;
2080
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002081 PREDICTED_WITH_ARG(FOR_ITER);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002082 case FOR_ITER:
2083 /* before: [iter]; after: [iter, iter()] *or* [] */
2084 v = TOP();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002085 x = PyIter_Next(v);
2086 if (x != NULL) {
2087 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002088 PREDICT(STORE_FAST);
2089 PREDICT(UNPACK_SEQUENCE);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002090 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002091 }
Tim Petersf4848da2001-05-05 00:14:56 +00002092 if (!PyErr_Occurred()) {
2093 /* iterator ended normally */
2094 x = v = POP();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002095 Py_DECREF(v);
2096 JUMPBY(oparg);
2097 continue;
2098 }
2099 break;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002100
Guido van Rossum374a9221991-04-04 10:40:29 +00002101 case SETUP_LOOP:
2102 case SETUP_EXCEPT:
2103 case SETUP_FINALLY:
Guido van Rossumb209a111997-04-29 18:18:01 +00002104 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002105 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002106 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002107
Guido van Rossumf10570b1995-07-07 22:53:21 +00002108 case CALL_FUNCTION:
Jeremy Hylton985eba52003-02-05 23:13:00 +00002109 PCALL(PCALL_ALL);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002110 x = call_function(&stack_pointer, oparg);
2111 PUSH(x);
2112 if (x != NULL)
2113 continue;
2114 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002115
Jeremy Hylton76901512000-03-28 23:49:17 +00002116 case CALL_FUNCTION_VAR:
2117 case CALL_FUNCTION_KW:
2118 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002119 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002120 int na = oparg & 0xff;
2121 int nk = (oparg>>8) & 0xff;
2122 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002123 int n = na + 2 * nk;
2124 PyObject **pfunc, *func;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002125 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002126 if (flags & CALL_FLAG_VAR)
2127 n++;
2128 if (flags & CALL_FLAG_KW)
2129 n++;
2130 pfunc = stack_pointer - n - 1;
2131 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002132
Guido van Rossumac7be682001-01-17 15:42:30 +00002133 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002134 && PyMethod_GET_SELF(func) != NULL) {
2135 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002136 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002137 func = PyMethod_GET_FUNCTION(func);
2138 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002139 Py_DECREF(*pfunc);
2140 *pfunc = self;
2141 na++;
2142 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002143 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002144 Py_INCREF(func);
2145 x = ext_do_call(func, &stack_pointer, flags, na, nk);
Jeremy Hylton76901512000-03-28 23:49:17 +00002146 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002147
Jeremy Hylton76901512000-03-28 23:49:17 +00002148 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002149 w = POP();
2150 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002151 }
2152 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002153 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002154 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002155 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002156 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002157
Guido van Rossum681d79a1995-07-18 14:51:37 +00002158 case MAKE_FUNCTION:
2159 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002160 x = PyFunction_New(v, f->f_globals);
2161 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002162 /* XXX Maybe this should be a separate opcode? */
2163 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002164 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002165 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002166 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002167 x = NULL;
2168 break;
2169 }
2170 while (--oparg >= 0) {
2171 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002172 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002173 }
2174 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002175 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002176 }
2177 PUSH(x);
2178 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002179
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002180 case MAKE_CLOSURE:
2181 {
2182 int nfree;
2183 v = POP(); /* code object */
2184 x = PyFunction_New(v, f->f_globals);
Jeremy Hylton733c8932001-12-13 19:51:56 +00002185 nfree = PyCode_GetNumFree((PyCodeObject *)v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002186 Py_DECREF(v);
2187 /* XXX Maybe this should be a separate opcode? */
2188 if (x != NULL && nfree > 0) {
2189 v = PyTuple_New(nfree);
2190 if (v == NULL) {
2191 Py_DECREF(x);
2192 x = NULL;
2193 break;
2194 }
2195 while (--nfree >= 0) {
2196 w = POP();
2197 PyTuple_SET_ITEM(v, nfree, w);
2198 }
2199 err = PyFunction_SetClosure(x, v);
2200 Py_DECREF(v);
2201 }
2202 if (x != NULL && oparg > 0) {
2203 v = PyTuple_New(oparg);
2204 if (v == NULL) {
2205 Py_DECREF(x);
2206 x = NULL;
2207 break;
2208 }
2209 while (--oparg >= 0) {
2210 w = POP();
2211 PyTuple_SET_ITEM(v, oparg, w);
2212 }
2213 err = PyFunction_SetDefaults(x, v);
2214 Py_DECREF(v);
2215 }
2216 PUSH(x);
2217 break;
2218 }
2219
Guido van Rossum8861b741996-07-30 16:49:37 +00002220 case BUILD_SLICE:
2221 if (oparg == 3)
2222 w = POP();
2223 else
2224 w = NULL;
2225 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002226 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002227 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002228 Py_DECREF(u);
2229 Py_DECREF(v);
2230 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002231 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002232 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002233 break;
2234
Fred Drakeef8ace32000-08-24 00:32:09 +00002235 case EXTENDED_ARG:
2236 opcode = NEXTOP();
2237 oparg = oparg<<16 | NEXTARG();
2238 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002239
Guido van Rossum374a9221991-04-04 10:40:29 +00002240 default:
2241 fprintf(stderr,
2242 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002243 PyCode_Addr2Line(f->f_code, f->f_lasti),
2244 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002245 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002246 why = WHY_EXCEPTION;
2247 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002248
2249#ifdef CASE_TOO_BIG
2250 }
2251#endif
2252
Guido van Rossum374a9221991-04-04 10:40:29 +00002253 } /* switch */
2254
2255 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002256
Guido van Rossum374a9221991-04-04 10:40:29 +00002257 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002258
Guido van Rossum374a9221991-04-04 10:40:29 +00002259 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002260 if (err == 0 && x != NULL) {
2261#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002262 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002263 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002264 fprintf(stderr,
2265 "XXX undetected error\n");
2266 else
2267#endif
2268 continue; /* Normal, fast path */
2269 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002270 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002271 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002272 err = 0;
2273 }
2274
Guido van Rossum374a9221991-04-04 10:40:29 +00002275 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002276
Guido van Rossum374a9221991-04-04 10:40:29 +00002277 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002278 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002279 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002280 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002281 why = WHY_EXCEPTION;
2282 }
2283 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002284#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002285 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002286 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002287 if (PyErr_Occurred()) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002288 fprintf(stderr,
2289 "XXX undetected error (why=%d)\n",
2290 why);
2291 why = WHY_EXCEPTION;
2292 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002293 }
2294#endif
2295
2296 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002297
Guido van Rossum374a9221991-04-04 10:40:29 +00002298 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002299 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002300
Fred Drake8f51f542001-10-04 14:48:42 +00002301 if (tstate->c_tracefunc != NULL)
2302 call_exc_trace(tstate->c_tracefunc,
2303 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002304 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002305
Guido van Rossum374a9221991-04-04 10:40:29 +00002306 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002307
Guido van Rossum374a9221991-04-04 10:40:29 +00002308 if (why == WHY_RERAISE)
2309 why = WHY_EXCEPTION;
2310
2311 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002312
Tim Peters5ca576e2001-06-18 22:08:13 +00002313 while (why != WHY_NOT && why != WHY_YIELD && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002314 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002315
2316 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2317 /* For a continue inside a try block,
2318 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002319 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2320 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002321 why = WHY_NOT;
2322 JUMPTO(PyInt_AS_LONG(retval));
2323 Py_DECREF(retval);
2324 break;
2325 }
2326
Guido van Rossum374a9221991-04-04 10:40:29 +00002327 while (STACK_LEVEL() > b->b_level) {
2328 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002329 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002330 }
2331 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2332 why = WHY_NOT;
2333 JUMPTO(b->b_handler);
2334 break;
2335 }
2336 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002337 (b->b_type == SETUP_EXCEPT &&
2338 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002339 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002340 PyObject *exc, *val, *tb;
2341 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002342 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002343 val = Py_None;
2344 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002345 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002346 /* Make the raw exception data
2347 available to the handler,
2348 so a program can emulate the
2349 Python main loop. Don't do
2350 this for 'finally'. */
2351 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002352 PyErr_NormalizeException(
2353 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002354 set_exc_info(tstate,
2355 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002356 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002357 if (tb == NULL) {
2358 Py_INCREF(Py_None);
2359 PUSH(Py_None);
2360 } else
2361 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002362 PUSH(val);
2363 PUSH(exc);
2364 }
2365 else {
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002366 if (why == WHY_RETURN ||
Guido van Rossumc5fe5eb2002-06-12 03:45:21 +00002367 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00002368 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002369 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002370 PUSH(v);
2371 }
2372 why = WHY_NOT;
2373 JUMPTO(b->b_handler);
2374 break;
2375 }
2376 } /* unwind stack */
2377
2378 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002379
Guido van Rossum374a9221991-04-04 10:40:29 +00002380 if (why != WHY_NOT)
2381 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002382
Guido van Rossum374a9221991-04-04 10:40:29 +00002383 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002384
Guido van Rossum35974fb2001-12-06 21:28:18 +00002385 if (why != WHY_YIELD) {
2386 /* Pop remaining stack entries -- but when yielding */
2387 while (!EMPTY()) {
2388 v = POP();
2389 Py_XDECREF(v);
2390 }
2391 }
2392
Tim Peters5ca576e2001-06-18 22:08:13 +00002393 if (why != WHY_RETURN && why != WHY_YIELD)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002394 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002395
Fred Drake9e3ad782001-07-03 23:39:52 +00002396 if (tstate->use_tracing) {
2397 if (tstate->c_tracefunc
2398 && (why == WHY_RETURN || why == WHY_YIELD)) {
2399 if (call_trace(tstate->c_tracefunc,
2400 tstate->c_traceobj, f,
2401 PyTrace_RETURN, retval)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002402 Py_XDECREF(retval);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002403 retval = NULL;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002404 why = WHY_EXCEPTION;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002405 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002406 }
Fred Drake8f51f542001-10-04 14:48:42 +00002407 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002408 if (why == WHY_EXCEPTION)
2409 call_trace_protected(tstate->c_profilefunc,
2410 tstate->c_profileobj, f,
2411 PyTrace_RETURN);
2412 else if (call_trace(tstate->c_profilefunc,
2413 tstate->c_profileobj, f,
2414 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002415 Py_XDECREF(retval);
2416 retval = NULL;
2417 why = WHY_EXCEPTION;
2418 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002419 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002420 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002421
Guido van Rossuma027efa1997-05-05 20:56:21 +00002422 reset_exc_info(tstate);
2423
Tim Peters5ca576e2001-06-18 22:08:13 +00002424 /* pop frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002425 --tstate->recursion_depth;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002426 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002427
Guido van Rossum96a42c81992-01-12 02:29:51 +00002428 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002429}
2430
Tim Peters6d6c1a32001-08-02 04:15:00 +00002431PyObject *
2432PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002433 PyObject **args, int argcount, PyObject **kws, int kwcount,
2434 PyObject **defs, int defcount, PyObject *closure)
2435{
2436 register PyFrameObject *f;
2437 register PyObject *retval = NULL;
2438 register PyObject **fastlocals, **freevars;
2439 PyThreadState *tstate = PyThreadState_GET();
2440 PyObject *x, *u;
2441
2442 if (globals == NULL) {
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002443 PyErr_SetString(PyExc_SystemError,
2444 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002445 return NULL;
2446 }
2447
Jeremy Hylton985eba52003-02-05 23:13:00 +00002448 assert(globals != NULL);
2449 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002450 if (f == NULL)
2451 return NULL;
2452
2453 fastlocals = f->f_localsplus;
2454 freevars = f->f_localsplus + f->f_nlocals;
2455
2456 if (co->co_argcount > 0 ||
2457 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2458 int i;
2459 int n = argcount;
2460 PyObject *kwdict = NULL;
2461 if (co->co_flags & CO_VARKEYWORDS) {
2462 kwdict = PyDict_New();
2463 if (kwdict == NULL)
2464 goto fail;
2465 i = co->co_argcount;
2466 if (co->co_flags & CO_VARARGS)
2467 i++;
2468 SETLOCAL(i, kwdict);
2469 }
2470 if (argcount > co->co_argcount) {
2471 if (!(co->co_flags & CO_VARARGS)) {
2472 PyErr_Format(PyExc_TypeError,
2473 "%.200s() takes %s %d "
2474 "%sargument%s (%d given)",
2475 PyString_AsString(co->co_name),
2476 defcount ? "at most" : "exactly",
2477 co->co_argcount,
2478 kwcount ? "non-keyword " : "",
2479 co->co_argcount == 1 ? "" : "s",
2480 argcount);
2481 goto fail;
2482 }
2483 n = co->co_argcount;
2484 }
2485 for (i = 0; i < n; i++) {
2486 x = args[i];
2487 Py_INCREF(x);
2488 SETLOCAL(i, x);
2489 }
2490 if (co->co_flags & CO_VARARGS) {
2491 u = PyTuple_New(argcount - n);
2492 if (u == NULL)
2493 goto fail;
2494 SETLOCAL(co->co_argcount, u);
2495 for (i = n; i < argcount; i++) {
2496 x = args[i];
2497 Py_INCREF(x);
2498 PyTuple_SET_ITEM(u, i-n, x);
2499 }
2500 }
2501 for (i = 0; i < kwcount; i++) {
2502 PyObject *keyword = kws[2*i];
2503 PyObject *value = kws[2*i + 1];
2504 int j;
2505 if (keyword == NULL || !PyString_Check(keyword)) {
2506 PyErr_Format(PyExc_TypeError,
2507 "%.200s() keywords must be strings",
2508 PyString_AsString(co->co_name));
2509 goto fail;
2510 }
2511 /* XXX slow -- speed up using dictionary? */
2512 for (j = 0; j < co->co_argcount; j++) {
2513 PyObject *nm = PyTuple_GET_ITEM(
2514 co->co_varnames, j);
2515 int cmp = PyObject_RichCompareBool(
2516 keyword, nm, Py_EQ);
2517 if (cmp > 0)
2518 break;
2519 else if (cmp < 0)
2520 goto fail;
2521 }
2522 /* Check errors from Compare */
2523 if (PyErr_Occurred())
2524 goto fail;
2525 if (j >= co->co_argcount) {
2526 if (kwdict == NULL) {
2527 PyErr_Format(PyExc_TypeError,
2528 "%.200s() got an unexpected "
2529 "keyword argument '%.400s'",
2530 PyString_AsString(co->co_name),
2531 PyString_AsString(keyword));
2532 goto fail;
2533 }
2534 PyDict_SetItem(kwdict, keyword, value);
2535 }
2536 else {
2537 if (GETLOCAL(j) != NULL) {
2538 PyErr_Format(PyExc_TypeError,
2539 "%.200s() got multiple "
2540 "values for keyword "
2541 "argument '%.400s'",
2542 PyString_AsString(co->co_name),
2543 PyString_AsString(keyword));
2544 goto fail;
2545 }
2546 Py_INCREF(value);
2547 SETLOCAL(j, value);
2548 }
2549 }
2550 if (argcount < co->co_argcount) {
2551 int m = co->co_argcount - defcount;
2552 for (i = argcount; i < m; i++) {
2553 if (GETLOCAL(i) == NULL) {
2554 PyErr_Format(PyExc_TypeError,
2555 "%.200s() takes %s %d "
2556 "%sargument%s (%d given)",
2557 PyString_AsString(co->co_name),
2558 ((co->co_flags & CO_VARARGS) ||
2559 defcount) ? "at least"
2560 : "exactly",
2561 m, kwcount ? "non-keyword " : "",
2562 m == 1 ? "" : "s", i);
2563 goto fail;
2564 }
2565 }
2566 if (n > m)
2567 i = n - m;
2568 else
2569 i = 0;
2570 for (; i < defcount; i++) {
2571 if (GETLOCAL(m+i) == NULL) {
2572 PyObject *def = defs[i];
2573 Py_INCREF(def);
2574 SETLOCAL(m+i, def);
2575 }
2576 }
2577 }
2578 }
2579 else {
2580 if (argcount > 0 || kwcount > 0) {
2581 PyErr_Format(PyExc_TypeError,
2582 "%.200s() takes no arguments (%d given)",
2583 PyString_AsString(co->co_name),
2584 argcount + kwcount);
2585 goto fail;
2586 }
2587 }
2588 /* Allocate and initialize storage for cell vars, and copy free
2589 vars into frame. This isn't too efficient right now. */
2590 if (f->f_ncells) {
2591 int i = 0, j = 0, nargs, found;
2592 char *cellname, *argname;
2593 PyObject *c;
2594
2595 nargs = co->co_argcount;
2596 if (co->co_flags & CO_VARARGS)
2597 nargs++;
2598 if (co->co_flags & CO_VARKEYWORDS)
2599 nargs++;
2600
2601 /* Check for cells that shadow args */
2602 for (i = 0; i < f->f_ncells && j < nargs; ++i) {
2603 cellname = PyString_AS_STRING(
2604 PyTuple_GET_ITEM(co->co_cellvars, i));
2605 found = 0;
2606 while (j < nargs) {
2607 argname = PyString_AS_STRING(
2608 PyTuple_GET_ITEM(co->co_varnames, j));
2609 if (strcmp(cellname, argname) == 0) {
2610 c = PyCell_New(GETLOCAL(j));
2611 if (c == NULL)
2612 goto fail;
2613 GETLOCAL(f->f_nlocals + i) = c;
2614 found = 1;
2615 break;
2616 }
2617 j++;
2618 }
2619 if (found == 0) {
2620 c = PyCell_New(NULL);
2621 if (c == NULL)
2622 goto fail;
2623 SETLOCAL(f->f_nlocals + i, c);
2624 }
2625 }
2626 /* Initialize any that are left */
2627 while (i < f->f_ncells) {
2628 c = PyCell_New(NULL);
2629 if (c == NULL)
2630 goto fail;
2631 SETLOCAL(f->f_nlocals + i, c);
2632 i++;
2633 }
2634 }
2635 if (f->f_nfreevars) {
2636 int i;
2637 for (i = 0; i < f->f_nfreevars; ++i) {
2638 PyObject *o = PyTuple_GET_ITEM(closure, i);
2639 Py_INCREF(o);
2640 freevars[f->f_ncells + i] = o;
2641 }
2642 }
2643
Tim Peters5ca576e2001-06-18 22:08:13 +00002644 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002645 /* Don't need to keep the reference to f_back, it will be set
2646 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002647 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002648 f->f_back = NULL;
2649
Jeremy Hylton985eba52003-02-05 23:13:00 +00002650 PCALL(PCALL_GENERATOR);
2651
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002652 /* Create a new generator that owns the ready to run frame
2653 * and return that as the value. */
Tim Peters5ca576e2001-06-18 22:08:13 +00002654 return gen_new(f);
2655 }
2656
2657 retval = eval_frame(f);
2658
2659 fail: /* Jump here from prelude on failure */
2660
Tim Petersb13680b2001-11-27 23:29:29 +00002661 /* decref'ing the frame can cause __del__ methods to get invoked,
2662 which can call back into Python. While we're done with the
2663 current Python frame (f), the associated C stack is still in use,
2664 so recursion_depth must be boosted for the duration.
2665 */
2666 assert(tstate != NULL);
2667 ++tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002668 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00002669 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002670 return retval;
2671}
2672
2673
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002674/* Implementation notes for set_exc_info() and reset_exc_info():
2675
2676- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2677 'exc_traceback'. These always travel together.
2678
2679- tstate->curexc_ZZZ is the "hot" exception that is set by
2680 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2681
2682- Once an exception is caught by an except clause, it is transferred
2683 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2684 can pick it up. This is the primary task of set_exc_info().
2685
2686- Now let me explain the complicated dance with frame->f_exc_ZZZ.
2687
2688 Long ago, when none of this existed, there were just a few globals:
2689 one set corresponding to the "hot" exception, and one set
2690 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2691 globals; they were simply stored as sys.exc_ZZZ. For backwards
2692 compatibility, they still are!) The problem was that in code like
2693 this:
2694
2695 try:
2696 "something that may fail"
2697 except "some exception":
2698 "do something else first"
2699 "print the exception from sys.exc_ZZZ."
2700
2701 if "do something else first" invoked something that raised and caught
2702 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2703 cause of subtle bugs. I fixed this by changing the semantics as
2704 follows:
2705
2706 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2707 *in that frame*.
2708
2709 - But initially, and as long as no exception is caught in a given
2710 frame, sys.exc_ZZZ will hold the last exception caught in the
2711 previous frame (or the frame before that, etc.).
2712
2713 The first bullet fixed the bug in the above example. The second
2714 bullet was for backwards compatibility: it was (and is) common to
2715 have a function that is called when an exception is caught, and to
2716 have that function access the caught exception via sys.exc_ZZZ.
2717 (Example: traceback.print_exc()).
2718
2719 At the same time I fixed the problem that sys.exc_ZZZ weren't
2720 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2721 but that's really a separate improvement.
2722
2723 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2724 variables to what they were before the current frame was called. The
2725 set_exc_info() function saves them on the frame so that
2726 reset_exc_info() can restore them. The invariant is that
2727 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2728 exception (where "catching" an exception applies only to successful
2729 except clauses); and if the current frame ever caught an exception,
2730 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2731 at the start of the current frame.
2732
2733*/
2734
Guido van Rossuma027efa1997-05-05 20:56:21 +00002735static void
Guido van Rossumac7be682001-01-17 15:42:30 +00002736set_exc_info(PyThreadState *tstate,
2737 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002738{
2739 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002740 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00002741
Guido van Rossuma027efa1997-05-05 20:56:21 +00002742 frame = tstate->frame;
2743 if (frame->f_exc_type == NULL) {
2744 /* This frame didn't catch an exception before */
2745 /* Save previous exception of this thread in this frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002746 if (tstate->exc_type == NULL) {
2747 Py_INCREF(Py_None);
2748 tstate->exc_type = Py_None;
2749 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002750 tmp_type = frame->f_exc_type;
2751 tmp_value = frame->f_exc_value;
2752 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002753 Py_XINCREF(tstate->exc_type);
2754 Py_XINCREF(tstate->exc_value);
2755 Py_XINCREF(tstate->exc_traceback);
2756 frame->f_exc_type = tstate->exc_type;
2757 frame->f_exc_value = tstate->exc_value;
2758 frame->f_exc_traceback = tstate->exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002759 Py_XDECREF(tmp_type);
2760 Py_XDECREF(tmp_value);
2761 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002762 }
2763 /* Set new exception for this thread */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002764 tmp_type = tstate->exc_type;
2765 tmp_value = tstate->exc_value;
2766 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002767 Py_XINCREF(type);
2768 Py_XINCREF(value);
2769 Py_XINCREF(tb);
2770 tstate->exc_type = type;
2771 tstate->exc_value = value;
2772 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002773 Py_XDECREF(tmp_type);
2774 Py_XDECREF(tmp_value);
2775 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002776 /* For b/w compatibility */
2777 PySys_SetObject("exc_type", type);
2778 PySys_SetObject("exc_value", value);
2779 PySys_SetObject("exc_traceback", tb);
2780}
2781
2782static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002783reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002784{
2785 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002786 PyObject *tmp_type, *tmp_value, *tmp_tb;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002787 frame = tstate->frame;
2788 if (frame->f_exc_type != NULL) {
2789 /* This frame caught an exception */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002790 tmp_type = tstate->exc_type;
2791 tmp_value = tstate->exc_value;
2792 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002793 Py_XINCREF(frame->f_exc_type);
2794 Py_XINCREF(frame->f_exc_value);
2795 Py_XINCREF(frame->f_exc_traceback);
2796 tstate->exc_type = frame->f_exc_type;
2797 tstate->exc_value = frame->f_exc_value;
2798 tstate->exc_traceback = frame->f_exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002799 Py_XDECREF(tmp_type);
2800 Py_XDECREF(tmp_value);
2801 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002802 /* For b/w compatibility */
2803 PySys_SetObject("exc_type", frame->f_exc_type);
2804 PySys_SetObject("exc_value", frame->f_exc_value);
2805 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
2806 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002807 tmp_type = frame->f_exc_type;
2808 tmp_value = frame->f_exc_value;
2809 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002810 frame->f_exc_type = NULL;
2811 frame->f_exc_value = NULL;
2812 frame->f_exc_traceback = NULL;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002813 Py_XDECREF(tmp_type);
2814 Py_XDECREF(tmp_value);
2815 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002816}
2817
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002818/* Logic for the raise statement (too complicated for inlining).
2819 This *consumes* a reference count to each of its arguments. */
Guido van Rossum1aa14831997-01-21 05:34:20 +00002820static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002821do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002822{
Guido van Rossumd295f121998-04-09 21:39:57 +00002823 if (type == NULL) {
2824 /* Reraise */
2825 PyThreadState *tstate = PyThreadState_Get();
2826 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
2827 value = tstate->exc_value;
2828 tb = tstate->exc_traceback;
2829 Py_XINCREF(type);
2830 Py_XINCREF(value);
2831 Py_XINCREF(tb);
2832 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002833
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002834 /* We support the following forms of raise:
2835 raise <class>, <classinstance>
2836 raise <class>, <argument tuple>
2837 raise <class>, None
2838 raise <class>, <argument>
2839 raise <classinstance>, None
2840 raise <string>, <object>
2841 raise <string>, None
2842
2843 An omitted second argument is the same as None.
2844
2845 In addition, raise <tuple>, <anything> is the same as
2846 raising the tuple's first item (and it better have one!);
2847 this rule is applied recursively.
2848
2849 Finally, an optional third argument can be supplied, which
2850 gives the traceback to be substituted (useful when
2851 re-raising an exception after examining it). */
2852
2853 /* First, check the traceback argument, replacing None with
2854 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002855 if (tb == Py_None) {
2856 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002857 tb = NULL;
2858 }
2859 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002860 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002861 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002862 goto raise_error;
2863 }
2864
2865 /* Next, replace a missing value with None */
2866 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002867 value = Py_None;
2868 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002869 }
2870
2871 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00002872 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
2873 PyObject *tmp = type;
2874 type = PyTuple_GET_ITEM(type, 0);
2875 Py_INCREF(type);
2876 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002877 }
2878
Tim Petersafb2c802002-04-18 18:06:20 +00002879 if (PyString_CheckExact(type))
2880 /* Raising builtin string is deprecated but still allowed --
2881 * do nothing. Raising an instance of a new-style str
2882 * subclass is right out. */
Neal Norwitz37aa0662003-01-10 15:31:15 +00002883 PyErr_Warn(PyExc_PendingDeprecationWarning,
2884 "raising a string exception is deprecated");
Barry Warsaw4249f541997-08-22 21:26:19 +00002885
2886 else if (PyClass_Check(type))
2887 PyErr_NormalizeException(&type, &value, &tb);
2888
Guido van Rossumb209a111997-04-29 18:18:01 +00002889 else if (PyInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002890 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002891 if (value != Py_None) {
2892 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002893 "instance exception may not have a separate value");
2894 goto raise_error;
2895 }
2896 else {
2897 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00002898 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002899 value = type;
Guido van Rossumb209a111997-04-29 18:18:01 +00002900 type = (PyObject*) ((PyInstanceObject*)type)->in_class;
2901 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002902 }
2903 }
2904 else {
2905 /* Not something you can raise. You get an exception
2906 anyway, just not what you specified :-) */
Jeremy Hylton960d9482001-04-27 02:25:33 +00002907 PyErr_Format(PyExc_TypeError,
Neal Norwitz37aa0662003-01-10 15:31:15 +00002908 "exceptions must be classes, instances, or "
2909 "strings (deprecated), not %s",
2910 type->ob_type->tp_name);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002911 goto raise_error;
2912 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002913 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002914 if (tb == NULL)
2915 return WHY_EXCEPTION;
2916 else
2917 return WHY_RERAISE;
2918 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00002919 Py_XDECREF(value);
2920 Py_XDECREF(type);
2921 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002922 return WHY_EXCEPTION;
2923}
2924
Tim Petersd6d010b2001-06-21 02:49:55 +00002925/* Iterate v argcnt times and store the results on the stack (via decreasing
2926 sp). Return 1 for success, 0 if error. */
2927
Barry Warsawe42b18f1997-08-25 22:13:04 +00002928static int
Tim Petersd6d010b2001-06-21 02:49:55 +00002929unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00002930{
Tim Petersd6d010b2001-06-21 02:49:55 +00002931 int i = 0;
2932 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00002933 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00002934
Tim Petersd6d010b2001-06-21 02:49:55 +00002935 assert(v != NULL);
2936
2937 it = PyObject_GetIter(v);
2938 if (it == NULL)
2939 goto Error;
2940
2941 for (; i < argcnt; i++) {
2942 w = PyIter_Next(it);
2943 if (w == NULL) {
2944 /* Iterator done, via error or exhaustion. */
2945 if (!PyErr_Occurred()) {
2946 PyErr_Format(PyExc_ValueError,
2947 "need more than %d value%s to unpack",
2948 i, i == 1 ? "" : "s");
2949 }
2950 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00002951 }
2952 *--sp = w;
2953 }
Tim Petersd6d010b2001-06-21 02:49:55 +00002954
2955 /* We better have exhausted the iterator now. */
2956 w = PyIter_Next(it);
2957 if (w == NULL) {
2958 if (PyErr_Occurred())
2959 goto Error;
2960 Py_DECREF(it);
2961 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00002962 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00002963 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00002964 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00002965 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00002966Error:
Barry Warsaw91010551997-08-25 22:30:51 +00002967 for (; i > 0; i--, sp++)
2968 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00002969 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00002970 return 0;
2971}
2972
2973
Guido van Rossum96a42c81992-01-12 02:29:51 +00002974#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00002975static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002976prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002977{
Guido van Rossum3f5da241990-12-20 15:06:42 +00002978 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00002979 if (PyObject_Print(v, stdout, 0) != 0)
2980 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00002981 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00002982 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002983}
Guido van Rossum3f5da241990-12-20 15:06:42 +00002984#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002985
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002986static void
Fred Drake5755ce62001-06-27 19:19:46 +00002987call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002988{
Guido van Rossumb209a111997-04-29 18:18:01 +00002989 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002990 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00002991 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00002992 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002993 value = Py_None;
2994 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00002995 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002996 arg = Py_BuildValue("(OOO)", type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002997 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002998 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002999 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003000 }
Fred Drake5755ce62001-06-27 19:19:46 +00003001 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003002 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003003 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003004 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003005 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003006 Py_XDECREF(type);
3007 Py_XDECREF(value);
3008 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003009 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003010}
3011
Fred Drake4ec5d562001-10-04 19:26:43 +00003012static void
3013call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3014 int what)
3015{
3016 PyObject *type, *value, *traceback;
3017 int err;
3018 PyErr_Fetch(&type, &value, &traceback);
3019 err = call_trace(func, obj, frame, what, NULL);
3020 if (err == 0)
3021 PyErr_Restore(type, value, traceback);
3022 else {
3023 Py_XDECREF(type);
3024 Py_XDECREF(value);
3025 Py_XDECREF(traceback);
3026 }
3027}
3028
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003029static int
Fred Drake5755ce62001-06-27 19:19:46 +00003030call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3031 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003032{
Fred Drake5755ce62001-06-27 19:19:46 +00003033 register PyThreadState *tstate = frame->f_tstate;
3034 int result;
3035 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003036 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003037 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003038 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003039 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003040 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3041 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003042 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003043 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003044}
3045
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003046PyObject *
3047_PyEval_CallTracing(PyObject *func, PyObject *args)
3048{
3049 PyFrameObject *frame = PyEval_GetFrame();
3050 PyThreadState *tstate = frame->f_tstate;
3051 int save_tracing = tstate->tracing;
3052 int save_use_tracing = tstate->use_tracing;
3053 PyObject *result;
3054
3055 tstate->tracing = 0;
3056 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3057 || (tstate->c_profilefunc != NULL));
3058 result = PyObject_Call(func, args, NULL);
3059 tstate->tracing = save_tracing;
3060 tstate->use_tracing = save_use_tracing;
3061 return result;
3062}
3063
Michael W. Hudson006c7522002-11-08 13:08:46 +00003064static int
Michael W. Hudson019a78e2002-11-08 12:53:11 +00003065maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003066 PyFrameObject *frame, int *instr_lb, int *instr_ub)
3067{
3068 /* The theory of SET_LINENO-less tracing.
3069
3070 In a nutshell, we use the co_lnotab field of the code object
3071 to tell when execution has moved onto a different line.
3072
3073 As mentioned above, the basic idea is so set things up so
3074 that
3075
3076 *instr_lb <= frame->f_lasti < *instr_ub
3077
3078 is true so long as execution does not change lines.
3079
3080 This is all fairly simple. Digging the information out of
3081 co_lnotab takes some work, but is conceptually clear.
3082
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003083 Somewhat harder to explain is why we don't *always* call the
3084 line trace function when the above test fails.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003085
3086 Consider this code:
3087
3088 1: def f(a):
3089 2: if a:
3090 3: print 1
3091 4: else:
3092 5: print 2
3093
3094 which compiles to this:
3095
3096 2 0 LOAD_FAST 0 (a)
3097 3 JUMP_IF_FALSE 9 (to 15)
3098 6 POP_TOP
3099
3100 3 7 LOAD_CONST 1 (1)
3101 10 PRINT_ITEM
3102 11 PRINT_NEWLINE
3103 12 JUMP_FORWARD 6 (to 21)
3104 >> 15 POP_TOP
3105
3106 5 16 LOAD_CONST 2 (2)
3107 19 PRINT_ITEM
3108 20 PRINT_NEWLINE
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003109 >> 21 LOAD_CONST 0 (None)
3110 24 RETURN_VALUE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003111
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003112 If 'a' is false, execution will jump to instruction at offset
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003113 15 and the co_lnotab will claim that execution has moved to
3114 line 3. This is at best misleading. In this case we could
3115 associate the POP_TOP with line 4, but that doesn't make
3116 sense in all cases (I think).
3117
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003118 What we do is only call the line trace function if the co_lnotab
3119 indicates we have jumped to the *start* of a line, i.e. if the
3120 current instruction offset matches the offset given for the
3121 start of a line by the co_lnotab.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003122
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003123 This also takes care of the situation where 'a' is true.
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003124 Execution will jump from instruction offset 12 to offset 21.
3125 Then the co_lnotab would imply that execution has moved to line
3126 5, which is again misleading.
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003127
3128 Why do we set f_lineno when tracing? Well, consider the code
3129 above when 'a' is true. If stepping through this with 'n' in
3130 pdb, you would stop at line 1 with a "call" type event, then
3131 line events on lines 2 and 3, then a "return" type event -- but
3132 you would be shown line 5 during this event. This is a change
3133 from the behaviour in 2.2 and before, and I've found it
3134 confusing in practice. By setting and using f_lineno when
3135 tracing, one can report a line number different from that
3136 suggested by f_lasti on this one occasion where it's desirable.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003137 */
3138
Michael W. Hudson006c7522002-11-08 13:08:46 +00003139 int result = 0;
3140
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003141 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003142 PyCodeObject* co = frame->f_code;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003143 int size, addr, line;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003144 unsigned char* p;
3145
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003146 size = PyString_GET_SIZE(co->co_lnotab) / 2;
3147 p = (unsigned char*)PyString_AS_STRING(co->co_lnotab);
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003148
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003149 addr = 0;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003150 line = co->co_firstlineno;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003151
3152 /* possible optimization: if f->f_lasti == instr_ub
3153 (likely to be a common case) then we already know
3154 instr_lb -- if we stored the matching value of p
3155 somwhere we could skip the first while loop. */
3156
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003157 /* see comments in compile.c for the description of
3158 co_lnotab. A point to remember: increments to p
3159 should come in pairs -- although we don't care about
3160 the line increments here, treating them as byte
3161 increments gets confusing, to say the least. */
3162
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003163 while (size > 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003164 if (addr + *p > frame->f_lasti)
3165 break;
3166 addr += *p++;
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003167 if (*p) *instr_lb = addr;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003168 line += *p++;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003169 --size;
3170 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003171
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003172 if (addr == frame->f_lasti) {
3173 frame->f_lineno = line;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003174 result = call_trace(func, obj, frame,
3175 PyTrace_LINE, Py_None);
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003176 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003177
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003178 if (size > 0) {
3179 while (--size >= 0) {
3180 addr += *p++;
3181 if (*p++)
3182 break;
3183 }
3184 *instr_ub = addr;
3185 }
3186 else {
3187 *instr_ub = INT_MAX;
3188 }
3189 }
Michael W. Hudson006c7522002-11-08 13:08:46 +00003190
3191 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003192}
3193
Fred Drake5755ce62001-06-27 19:19:46 +00003194void
3195PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003196{
Fred Drake5755ce62001-06-27 19:19:46 +00003197 PyThreadState *tstate = PyThreadState_Get();
3198 PyObject *temp = tstate->c_profileobj;
3199 Py_XINCREF(arg);
3200 tstate->c_profilefunc = NULL;
3201 tstate->c_profileobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003202 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003203 Py_XDECREF(temp);
3204 tstate->c_profilefunc = func;
3205 tstate->c_profileobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003206 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003207}
3208
3209void
3210PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3211{
3212 PyThreadState *tstate = PyThreadState_Get();
3213 PyObject *temp = tstate->c_traceobj;
3214 Py_XINCREF(arg);
3215 tstate->c_tracefunc = NULL;
3216 tstate->c_traceobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003217 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003218 Py_XDECREF(temp);
3219 tstate->c_tracefunc = func;
3220 tstate->c_traceobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003221 tstate->use_tracing = ((func != NULL)
3222 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003223}
3224
Guido van Rossumb209a111997-04-29 18:18:01 +00003225PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003226PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003227{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003228 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003229 if (current_frame == NULL)
Michael W. Hudson019a78e2002-11-08 12:53:11 +00003230 return PyThreadState_Get()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003231 else
3232 return current_frame->f_builtins;
3233}
3234
Guido van Rossumb209a111997-04-29 18:18:01 +00003235PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003236PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003237{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003238 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003239 if (current_frame == NULL)
3240 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003241 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003242 return current_frame->f_locals;
3243}
3244
Guido van Rossumb209a111997-04-29 18:18:01 +00003245PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003246PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003247{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003248 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003249 if (current_frame == NULL)
3250 return NULL;
3251 else
3252 return current_frame->f_globals;
3253}
3254
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003255PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003256PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003257{
Michael W. Hudson019a78e2002-11-08 12:53:11 +00003258 PyThreadState *tstate = PyThreadState_Get();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003259 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003260}
3261
Guido van Rossum6135a871995-01-09 17:53:26 +00003262int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003263PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003264{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003265 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003266 return current_frame == NULL ? 0 : current_frame->f_restricted;
3267}
3268
Guido van Rossumbe270261997-05-22 22:26:18 +00003269int
Tim Peters5ba58662001-07-16 02:29:45 +00003270PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003271{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003272 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003273 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003274
3275 if (current_frame != NULL) {
3276 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003277 const int compilerflags = codeflags & PyCF_MASK;
3278 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003279 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003280 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003281 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003282#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003283 if (codeflags & CO_GENERATOR_ALLOWED) {
3284 result = 1;
3285 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3286 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003287#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003288 }
3289 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003290}
3291
3292int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003293Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003294{
Guido van Rossumb209a111997-04-29 18:18:01 +00003295 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003296 if (f == NULL)
3297 return 0;
3298 if (!PyFile_SoftSpace(f, 0))
3299 return 0;
3300 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003301}
3302
Guido van Rossum3f5da241990-12-20 15:06:42 +00003303
Guido van Rossum681d79a1995-07-18 14:51:37 +00003304/* External interface to call any callable object.
3305 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003306
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003307#undef PyEval_CallObject
3308/* for backward compatibility: export this interface */
3309
Guido van Rossumb209a111997-04-29 18:18:01 +00003310PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003311PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003312{
Guido van Rossumb209a111997-04-29 18:18:01 +00003313 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003314}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003315#define PyEval_CallObject(func,arg) \
3316 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003317
Guido van Rossumb209a111997-04-29 18:18:01 +00003318PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003319PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003320{
Jeremy Hylton52820442001-01-03 23:52:36 +00003321 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003322
3323 if (arg == NULL)
Guido van Rossumb209a111997-04-29 18:18:01 +00003324 arg = PyTuple_New(0);
3325 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003326 PyErr_SetString(PyExc_TypeError,
3327 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003328 return NULL;
3329 }
3330 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003331 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003332
Guido van Rossumb209a111997-04-29 18:18:01 +00003333 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003334 PyErr_SetString(PyExc_TypeError,
3335 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003336 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003337 return NULL;
3338 }
3339
Tim Peters6d6c1a32001-08-02 04:15:00 +00003340 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003341 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003342 return result;
3343}
3344
Tim Peters6d6c1a32001-08-02 04:15:00 +00003345char *
3346PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003347{
3348 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003349 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003350 else if (PyFunction_Check(func))
3351 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3352 else if (PyCFunction_Check(func))
3353 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3354 else if (PyClass_Check(func))
3355 return PyString_AsString(((PyClassObject*)func)->cl_name);
3356 else if (PyInstance_Check(func)) {
3357 return PyString_AsString(
3358 ((PyInstanceObject*)func)->in_class->cl_name);
3359 } else {
3360 return func->ob_type->tp_name;
3361 }
3362}
3363
Tim Peters6d6c1a32001-08-02 04:15:00 +00003364char *
3365PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003366{
3367 if (PyMethod_Check(func))
3368 return "()";
3369 else if (PyFunction_Check(func))
3370 return "()";
3371 else if (PyCFunction_Check(func))
3372 return "()";
3373 else if (PyClass_Check(func))
3374 return " constructor";
3375 else if (PyInstance_Check(func)) {
3376 return " instance";
3377 } else {
3378 return " object";
3379 }
3380}
3381
Jeremy Hylton52820442001-01-03 23:52:36 +00003382#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
3383
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003384static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003385err_args(PyObject *func, int flags, int nargs)
3386{
3387 if (flags & METH_NOARGS)
3388 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003389 "%.200s() takes no arguments (%d given)",
Jeremy Hylton192690e2002-08-16 18:36:11 +00003390 ((PyCFunctionObject *)func)->m_ml->ml_name,
3391 nargs);
3392 else
3393 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003394 "%.200s() takes exactly one argument (%d given)",
Jeremy Hylton192690e2002-08-16 18:36:11 +00003395 ((PyCFunctionObject *)func)->m_ml->ml_name,
3396 nargs);
3397}
3398
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003399static PyObject *
3400call_function(PyObject ***pp_stack, int oparg)
3401{
3402 int na = oparg & 0xff;
3403 int nk = (oparg>>8) & 0xff;
3404 int n = na + 2 * nk;
3405 PyObject **pfunc = (*pp_stack) - n - 1;
3406 PyObject *func = *pfunc;
3407 PyObject *x, *w;
3408
Jeremy Hylton985eba52003-02-05 23:13:00 +00003409 /* Always dispatch PyCFunction first, because these are
3410 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003411 */
3412 if (PyCFunction_Check(func) && nk == 0) {
3413 int flags = PyCFunction_GET_FLAGS(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003414 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003415 if (flags & (METH_NOARGS | METH_O)) {
3416 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3417 PyObject *self = PyCFunction_GET_SELF(func);
3418 if (flags & METH_NOARGS && na == 0)
3419 x = (*meth)(self, NULL);
3420 else if (flags & METH_O && na == 1) {
3421 PyObject *arg = EXT_POP(*pp_stack);
3422 x = (*meth)(self, arg);
3423 Py_DECREF(arg);
3424 }
3425 else {
3426 err_args(func, flags, na);
3427 x = NULL;
3428 }
3429 }
3430 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003431 PyObject *callargs;
3432 callargs = load_args(pp_stack, na);
3433 x = PyCFunction_Call(func, callargs, NULL);
3434 Py_XDECREF(callargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003435 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003436 } else {
3437 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3438 /* optimize access to bound methods */
3439 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003440 PCALL(PCALL_METHOD);
3441 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003442 Py_INCREF(self);
3443 func = PyMethod_GET_FUNCTION(func);
3444 Py_INCREF(func);
3445 Py_DECREF(*pfunc);
3446 *pfunc = self;
3447 na++;
3448 n++;
3449 } else
3450 Py_INCREF(func);
3451 if (PyFunction_Check(func))
3452 x = fast_function(func, pp_stack, n, na, nk);
3453 else
3454 x = do_call(func, pp_stack, na, nk);
3455 Py_DECREF(func);
3456 }
3457
Jeremy Hylton985eba52003-02-05 23:13:00 +00003458 /* What does this do? */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003459 while ((*pp_stack) > pfunc) {
3460 w = EXT_POP(*pp_stack);
3461 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003462 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003463 }
3464 return x;
3465}
3466
Jeremy Hylton192690e2002-08-16 18:36:11 +00003467/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003468 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003469 For the simplest case -- a function that takes only positional
3470 arguments and is called with only positional arguments -- it
3471 inlines the most primitive frame setup code from
3472 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3473 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003474*/
3475
3476static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003477fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003478{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003479 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003480 PyObject *globals = PyFunction_GET_GLOBALS(func);
3481 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3482 PyObject **d = NULL;
3483 int nd = 0;
3484
Jeremy Hylton985eba52003-02-05 23:13:00 +00003485 PCALL(PCALL_FUNCTION);
3486 PCALL(PCALL_FAST_FUNCTION);
Raymond Hettinger40174c32003-05-31 07:04:16 +00003487 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003488 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3489 PyFrameObject *f;
3490 PyObject *retval = NULL;
3491 PyThreadState *tstate = PyThreadState_GET();
3492 PyObject **fastlocals, **stack;
3493 int i;
3494
3495 PCALL(PCALL_FASTER_FUNCTION);
3496 assert(globals != NULL);
3497 /* XXX Perhaps we should create a specialized
3498 PyFrame_New() that doesn't take locals, but does
3499 take builtins without sanity checking them.
3500 */
3501 f = PyFrame_New(tstate, co, globals, NULL);
3502 if (f == NULL)
3503 return NULL;
3504
3505 fastlocals = f->f_localsplus;
3506 stack = (*pp_stack) - n;
3507
3508 for (i = 0; i < n; i++) {
3509 Py_INCREF(*stack);
3510 fastlocals[i] = *stack++;
3511 }
3512 retval = eval_frame(f);
3513 assert(tstate != NULL);
3514 ++tstate->recursion_depth;
3515 Py_DECREF(f);
3516 --tstate->recursion_depth;
3517 return retval;
3518 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003519 if (argdefs != NULL) {
3520 d = &PyTuple_GET_ITEM(argdefs, 0);
3521 nd = ((PyTupleObject *)argdefs)->ob_size;
3522 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003523 return PyEval_EvalCodeEx(co, globals,
3524 (PyObject *)NULL, (*pp_stack)-n, na,
3525 (*pp_stack)-2*nk, nk, d, nd,
3526 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003527}
3528
3529static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003530update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3531 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003532{
3533 PyObject *kwdict = NULL;
3534 if (orig_kwdict == NULL)
3535 kwdict = PyDict_New();
3536 else {
3537 kwdict = PyDict_Copy(orig_kwdict);
3538 Py_DECREF(orig_kwdict);
3539 }
3540 if (kwdict == NULL)
3541 return NULL;
3542 while (--nk >= 0) {
3543 int err;
3544 PyObject *value = EXT_POP(*pp_stack);
3545 PyObject *key = EXT_POP(*pp_stack);
3546 if (PyDict_GetItem(kwdict, key) != NULL) {
Guido van Rossumac7be682001-01-17 15:42:30 +00003547 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +00003548 "%.200s%s got multiple values "
Jeremy Hylton512a2372001-04-11 13:52:29 +00003549 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003550 PyEval_GetFuncName(func),
3551 PyEval_GetFuncDesc(func),
Jeremy Hylton512a2372001-04-11 13:52:29 +00003552 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003553 Py_DECREF(key);
3554 Py_DECREF(value);
3555 Py_DECREF(kwdict);
3556 return NULL;
3557 }
3558 err = PyDict_SetItem(kwdict, key, value);
3559 Py_DECREF(key);
3560 Py_DECREF(value);
3561 if (err) {
3562 Py_DECREF(kwdict);
3563 return NULL;
3564 }
3565 }
3566 return kwdict;
3567}
3568
3569static PyObject *
3570update_star_args(int nstack, int nstar, PyObject *stararg,
3571 PyObject ***pp_stack)
3572{
3573 PyObject *callargs, *w;
3574
3575 callargs = PyTuple_New(nstack + nstar);
3576 if (callargs == NULL) {
3577 return NULL;
3578 }
3579 if (nstar) {
3580 int i;
3581 for (i = 0; i < nstar; i++) {
3582 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3583 Py_INCREF(a);
3584 PyTuple_SET_ITEM(callargs, nstack + i, a);
3585 }
3586 }
3587 while (--nstack >= 0) {
3588 w = EXT_POP(*pp_stack);
3589 PyTuple_SET_ITEM(callargs, nstack, w);
3590 }
3591 return callargs;
3592}
3593
3594static PyObject *
3595load_args(PyObject ***pp_stack, int na)
3596{
3597 PyObject *args = PyTuple_New(na);
3598 PyObject *w;
3599
3600 if (args == NULL)
3601 return NULL;
3602 while (--na >= 0) {
3603 w = EXT_POP(*pp_stack);
3604 PyTuple_SET_ITEM(args, na, w);
3605 }
3606 return args;
3607}
3608
3609static PyObject *
3610do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3611{
3612 PyObject *callargs = NULL;
3613 PyObject *kwdict = NULL;
3614 PyObject *result = NULL;
3615
3616 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003617 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003618 if (kwdict == NULL)
3619 goto call_fail;
3620 }
3621 callargs = load_args(pp_stack, na);
3622 if (callargs == NULL)
3623 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003624#ifdef CALL_PROFILE
3625 /* At this point, we have to look at the type of func to
3626 update the call stats properly. Do it here so as to avoid
3627 exposing the call stats machinery outside ceval.c
3628 */
3629 if (PyFunction_Check(func))
3630 PCALL(PCALL_FUNCTION);
3631 else if (PyMethod_Check(func))
3632 PCALL(PCALL_METHOD);
3633 else if (PyType_Check(func))
3634 PCALL(PCALL_TYPE);
3635 else
3636 PCALL(PCALL_OTHER);
3637#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003638 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003639 call_fail:
3640 Py_XDECREF(callargs);
3641 Py_XDECREF(kwdict);
3642 return result;
3643}
3644
3645static PyObject *
3646ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3647{
3648 int nstar = 0;
3649 PyObject *callargs = NULL;
3650 PyObject *stararg = NULL;
3651 PyObject *kwdict = NULL;
3652 PyObject *result = NULL;
3653
3654 if (flags & CALL_FLAG_KW) {
3655 kwdict = EXT_POP(*pp_stack);
3656 if (!(kwdict && PyDict_Check(kwdict))) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003657 PyErr_Format(PyExc_TypeError,
Jeremy Hylton512a2372001-04-11 13:52:29 +00003658 "%s%s argument after ** "
3659 "must be a dictionary",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003660 PyEval_GetFuncName(func),
3661 PyEval_GetFuncDesc(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003662 goto ext_call_fail;
3663 }
3664 }
3665 if (flags & CALL_FLAG_VAR) {
3666 stararg = EXT_POP(*pp_stack);
3667 if (!PyTuple_Check(stararg)) {
3668 PyObject *t = NULL;
3669 t = PySequence_Tuple(stararg);
3670 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003671 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3672 PyErr_Format(PyExc_TypeError,
3673 "%s%s argument after * "
3674 "must be a sequence",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003675 PyEval_GetFuncName(func),
3676 PyEval_GetFuncDesc(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003677 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003678 goto ext_call_fail;
3679 }
3680 Py_DECREF(stararg);
3681 stararg = t;
3682 }
3683 nstar = PyTuple_GET_SIZE(stararg);
3684 }
3685 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003686 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003687 if (kwdict == NULL)
3688 goto ext_call_fail;
3689 }
3690 callargs = update_star_args(na, nstar, stararg, pp_stack);
3691 if (callargs == NULL)
3692 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003693#ifdef CALL_PROFILE
3694 /* At this point, we have to look at the type of func to
3695 update the call stats properly. Do it here so as to avoid
3696 exposing the call stats machinery outside ceval.c
3697 */
3698 if (PyFunction_Check(func))
3699 PCALL(PCALL_FUNCTION);
3700 else if (PyMethod_Check(func))
3701 PCALL(PCALL_METHOD);
3702 else if (PyType_Check(func))
3703 PCALL(PCALL_TYPE);
3704 else
3705 PCALL(PCALL_OTHER);
3706#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003707 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003708 ext_call_fail:
3709 Py_XDECREF(callargs);
3710 Py_XDECREF(kwdict);
3711 Py_XDECREF(stararg);
3712 return result;
3713}
3714
Guido van Rossum3b9c6671996-07-30 18:40:29 +00003715#define SLICE_ERROR_MSG \
3716 "standard sequence type does not support step size other than one"
3717
Tim Peterscb479e72001-12-16 19:11:44 +00003718/* Extract a slice index from a PyInt or PyLong, and store in *pi.
3719 Silently reduce values larger than INT_MAX to INT_MAX, and silently
3720 boost values less than -INT_MAX to 0. Return 0 on error, 1 on success.
3721*/
Tim Petersb5196382001-12-16 19:44:20 +00003722/* Note: If v is NULL, return success without storing into *pi. This
3723 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3724 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00003725*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00003726int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003727_PyEval_SliceIndex(PyObject *v, int *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003728{
Tim Petersb5196382001-12-16 19:44:20 +00003729 if (v != NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003730 long x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003731 if (PyInt_Check(v)) {
3732 x = PyInt_AsLong(v);
3733 } else if (PyLong_Check(v)) {
3734 x = PyLong_AsLong(v);
3735 if (x==-1 && PyErr_Occurred()) {
3736 PyObject *long_zero;
Guido van Rossumac7be682001-01-17 15:42:30 +00003737 int cmp;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003738
Guido van Rossumac7be682001-01-17 15:42:30 +00003739 if (!PyErr_ExceptionMatches(
3740 PyExc_OverflowError)) {
3741 /* It's not an overflow error, so just
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003742 signal an error */
Guido van Rossum20c6add2000-05-08 14:06:50 +00003743 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003744 }
3745
Guido van Rossumac7be682001-01-17 15:42:30 +00003746 /* Clear the OverflowError */
3747 PyErr_Clear();
3748
3749 /* It's an overflow error, so we need to
3750 check the sign of the long integer,
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003751 set the value to INT_MAX or -INT_MAX,
3752 and clear the error. */
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003753
3754 /* Create a long integer with a value of 0 */
Guido van Rossumac7be682001-01-17 15:42:30 +00003755 long_zero = PyLong_FromLong(0L);
Tim Peterscb479e72001-12-16 19:11:44 +00003756 if (long_zero == NULL)
3757 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003758
3759 /* Check sign */
Guido van Rossumac7be682001-01-17 15:42:30 +00003760 cmp = PyObject_RichCompareBool(v, long_zero,
3761 Py_GT);
3762 Py_DECREF(long_zero);
3763 if (cmp < 0)
3764 return 0;
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003765 else if (cmp)
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003766 x = INT_MAX;
3767 else
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003768 x = -INT_MAX;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003769 }
3770 } else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003771 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003772 "slice indices must be integers");
Guido van Rossum20c6add2000-05-08 14:06:50 +00003773 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003774 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003775 /* Truncate -- very long indices are truncated anyway */
3776 if (x > INT_MAX)
3777 x = INT_MAX;
3778 else if (x < -INT_MAX)
Michael W. Hudsoncbd6fb92002-11-06 15:17:32 +00003779 x = -INT_MAX;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003780 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003781 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00003782 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003783}
3784
Guido van Rossum50d756e2001-08-18 17:43:36 +00003785#undef ISINT
3786#define ISINT(x) ((x) == NULL || PyInt_Check(x) || PyLong_Check(x))
3787
Guido van Rossumb209a111997-04-29 18:18:01 +00003788static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003789apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003790{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003791 PyTypeObject *tp = u->ob_type;
3792 PySequenceMethods *sq = tp->tp_as_sequence;
3793
3794 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3795 int ilow = 0, ihigh = INT_MAX;
3796 if (!_PyEval_SliceIndex(v, &ilow))
3797 return NULL;
3798 if (!_PyEval_SliceIndex(w, &ihigh))
3799 return NULL;
3800 return PySequence_GetSlice(u, ilow, ihigh);
3801 }
3802 else {
3803 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00003804 if (slice != NULL) {
3805 PyObject *res = PyObject_GetItem(u, slice);
3806 Py_DECREF(slice);
3807 return res;
3808 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00003809 else
3810 return NULL;
3811 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003812}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003813
3814static int
Guido van Rossumac7be682001-01-17 15:42:30 +00003815assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
3816 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003817{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003818 PyTypeObject *tp = u->ob_type;
3819 PySequenceMethods *sq = tp->tp_as_sequence;
3820
3821 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3822 int ilow = 0, ihigh = INT_MAX;
3823 if (!_PyEval_SliceIndex(v, &ilow))
3824 return -1;
3825 if (!_PyEval_SliceIndex(w, &ihigh))
3826 return -1;
3827 if (x == NULL)
3828 return PySequence_DelSlice(u, ilow, ihigh);
3829 else
3830 return PySequence_SetSlice(u, ilow, ihigh, x);
3831 }
3832 else {
3833 PyObject *slice = PySlice_New(v, w, NULL);
3834 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00003835 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003836 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00003837 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00003838 else
Guido van Rossum354797c2001-12-03 19:45:06 +00003839 res = PyObject_DelItem(u, slice);
3840 Py_DECREF(slice);
3841 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003842 }
3843 else
3844 return -1;
3845 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003846}
3847
Guido van Rossumb209a111997-04-29 18:18:01 +00003848static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003849cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003850{
Guido van Rossumac7be682001-01-17 15:42:30 +00003851 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003852 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00003853 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003854 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003855 break;
3856 case PyCmp_IS_NOT:
3857 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003858 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003859 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003860 res = PySequence_Contains(w, v);
3861 if (res < 0)
3862 return NULL;
3863 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003864 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00003865 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003866 if (res < 0)
3867 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003868 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003869 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003870 case PyCmp_EXC_MATCH:
Barry Warsaw4249f541997-08-22 21:26:19 +00003871 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003872 break;
3873 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00003874 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003875 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003876 v = res ? Py_True : Py_False;
3877 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003878 return v;
3879}
3880
Thomas Wouters52152252000-08-17 22:55:00 +00003881static PyObject *
3882import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003883{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003884 PyObject *x;
3885
3886 x = PyObject_GetAttr(v, name);
3887 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00003888 PyErr_Format(PyExc_ImportError,
3889 "cannot import name %.230s",
3890 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003891 }
Thomas Wouters52152252000-08-17 22:55:00 +00003892 return x;
3893}
Guido van Rossumac7be682001-01-17 15:42:30 +00003894
Thomas Wouters52152252000-08-17 22:55:00 +00003895static int
3896import_all_from(PyObject *locals, PyObject *v)
3897{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003898 PyObject *all = PyObject_GetAttrString(v, "__all__");
3899 PyObject *dict, *name, *value;
3900 int skip_leading_underscores = 0;
3901 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00003902
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003903 if (all == NULL) {
3904 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3905 return -1; /* Unexpected error */
3906 PyErr_Clear();
3907 dict = PyObject_GetAttrString(v, "__dict__");
3908 if (dict == NULL) {
3909 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3910 return -1;
3911 PyErr_SetString(PyExc_ImportError,
3912 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00003913 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003914 }
3915 all = PyMapping_Keys(dict);
3916 Py_DECREF(dict);
3917 if (all == NULL)
3918 return -1;
3919 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00003920 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003921
3922 for (pos = 0, err = 0; ; pos++) {
3923 name = PySequence_GetItem(all, pos);
3924 if (name == NULL) {
3925 if (!PyErr_ExceptionMatches(PyExc_IndexError))
3926 err = -1;
3927 else
3928 PyErr_Clear();
3929 break;
3930 }
3931 if (skip_leading_underscores &&
3932 PyString_Check(name) &&
3933 PyString_AS_STRING(name)[0] == '_')
3934 {
3935 Py_DECREF(name);
3936 continue;
3937 }
3938 value = PyObject_GetAttr(v, name);
3939 if (value == NULL)
3940 err = -1;
3941 else
3942 err = PyDict_SetItem(locals, name, value);
3943 Py_DECREF(name);
3944 Py_XDECREF(value);
3945 if (err != 0)
3946 break;
3947 }
3948 Py_DECREF(all);
3949 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00003950}
3951
Guido van Rossumb209a111997-04-29 18:18:01 +00003952static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003953build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003954{
Guido van Rossum7851eea2001-09-12 19:19:18 +00003955 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003956
3957 if (PyDict_Check(methods))
3958 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00003959 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00003960 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00003961 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
3962 base = PyTuple_GET_ITEM(bases, 0);
3963 metaclass = PyObject_GetAttrString(base, "__class__");
3964 if (metaclass == NULL) {
3965 PyErr_Clear();
3966 metaclass = (PyObject *)base->ob_type;
3967 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00003968 }
3969 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00003970 else {
3971 PyObject *g = PyEval_GetGlobals();
3972 if (g != NULL && PyDict_Check(g))
3973 metaclass = PyDict_GetItemString(g, "__metaclass__");
3974 if (metaclass == NULL)
3975 metaclass = (PyObject *) &PyClass_Type;
3976 Py_INCREF(metaclass);
3977 }
3978 result = PyObject_CallFunction(metaclass, "OOO", name, bases, methods);
3979 Py_DECREF(metaclass);
3980 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00003981}
3982
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003983static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003984exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
3985 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003986{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003987 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00003988 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003989 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003990
Guido van Rossumb209a111997-04-29 18:18:01 +00003991 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
3992 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003993 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00003994 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003995 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00003996 locals = PyTuple_GetItem(prog, 2);
3997 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003998 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003999 if (globals == Py_None) {
4000 globals = PyEval_GetGlobals();
4001 if (locals == Py_None) {
4002 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00004003 plain = 1;
4004 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004005 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004006 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004007 locals = globals;
Guido van Rossumb209a111997-04-29 18:18:01 +00004008 if (!PyString_Check(prog) &&
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004009 !PyUnicode_Check(prog) &&
Guido van Rossumb209a111997-04-29 18:18:01 +00004010 !PyCode_Check(prog) &&
4011 !PyFile_Check(prog)) {
4012 PyErr_SetString(PyExc_TypeError,
Guido van Rossumac7be682001-01-17 15:42:30 +00004013 "exec: arg 1 must be a string, file, or code object");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004014 return -1;
4015 }
Fred Drake661ea262000-10-24 19:57:45 +00004016 if (!PyDict_Check(globals)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004017 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00004018 "exec: arg 2 must be a dictionary or None");
4019 return -1;
4020 }
4021 if (!PyDict_Check(locals)) {
4022 PyErr_SetString(PyExc_TypeError,
4023 "exec: arg 3 must be a dictionary or None");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004024 return -1;
4025 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004026 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00004027 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00004028 if (PyCode_Check(prog)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +00004029 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4030 PyErr_SetString(PyExc_TypeError,
4031 "code object passed to exec may not contain free variables");
4032 return -1;
4033 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004034 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004035 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004036 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004037 FILE *fp = PyFile_AsFile(prog);
4038 char *name = PyString_AsString(PyFile_Name(prog));
Tim Peters5ba58662001-07-16 02:29:45 +00004039 PyCompilerFlags cf;
4040 cf.cf_flags = 0;
4041 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004042 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
4043 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004044 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004045 v = PyRun_File(fp, name, Py_file_input, globals,
4046 locals);
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004047 }
4048 else {
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004049 PyObject *tmp = NULL;
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004050 char *str;
Tim Peters5ba58662001-07-16 02:29:45 +00004051 PyCompilerFlags cf;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004052 cf.cf_flags = 0;
4053#ifdef Py_USING_UNICODE
4054 if (PyUnicode_Check(prog)) {
4055 tmp = PyUnicode_AsUTF8String(prog);
4056 if (tmp == NULL)
4057 return -1;
4058 prog = tmp;
4059 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4060 }
4061#endif
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004062 if (PyString_AsStringAndSize(prog, &str, NULL))
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004063 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004064 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004065 v = PyRun_StringFlags(str, Py_file_input, globals,
4066 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004067 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004068 v = PyRun_String(str, Py_file_input, globals, locals);
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004069 Py_XDECREF(tmp);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004070 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004071 if (plain)
4072 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004073 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004074 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00004075 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004076 return 0;
4077}
Guido van Rossum24c13741995-02-14 09:42:43 +00004078
Guido van Rossumac7be682001-01-17 15:42:30 +00004079static void
Paul Prescode68140d2000-08-30 20:25:01 +00004080format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4081{
4082 char *obj_str;
4083
4084 if (!obj)
4085 return;
4086
4087 obj_str = PyString_AsString(obj);
4088 if (!obj_str)
4089 return;
4090
4091 PyErr_Format(exc, format_str, obj_str);
4092}
Guido van Rossum950361c1997-01-24 13:49:28 +00004093
4094#ifdef DYNAMIC_EXECUTION_PROFILE
4095
Skip Montanarof118cb12001-10-15 20:51:38 +00004096static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004097getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004098{
4099 int i;
4100 PyObject *l = PyList_New(256);
4101 if (l == NULL) return NULL;
4102 for (i = 0; i < 256; i++) {
4103 PyObject *x = PyInt_FromLong(a[i]);
4104 if (x == NULL) {
4105 Py_DECREF(l);
4106 return NULL;
4107 }
4108 PyList_SetItem(l, i, x);
4109 }
4110 for (i = 0; i < 256; i++)
4111 a[i] = 0;
4112 return l;
4113}
4114
4115PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004116_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004117{
4118#ifndef DXPAIRS
4119 return getarray(dxp);
4120#else
4121 int i;
4122 PyObject *l = PyList_New(257);
4123 if (l == NULL) return NULL;
4124 for (i = 0; i < 257; i++) {
4125 PyObject *x = getarray(dxpairs[i]);
4126 if (x == NULL) {
4127 Py_DECREF(l);
4128 return NULL;
4129 }
4130 PyList_SetItem(l, i, x);
4131 }
4132 return l;
4133#endif
4134}
4135
4136#endif