blob: fe8aca5a1dcfa669acadaa47b5fc48e527c218a8 [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;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000500int _Py_CheckRecursionLimit = 1000;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000501
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000502int
503Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000504{
505 return recursion_limit;
506}
507
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000508void
509Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000510{
511 recursion_limit = new_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000512 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000513}
514
Armin Rigo2b3eb402003-10-28 12:05:48 +0000515/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
516 if the recursion_depth reaches _Py_CheckRecursionLimit.
517 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
518 to guarantee that _Py_CheckRecursiveCall() is regularly called.
519 Without USE_STACKCHECK, there is no need for this. */
520int
521_Py_CheckRecursiveCall(char *where)
522{
523 PyThreadState *tstate = PyThreadState_GET();
524
525#ifdef USE_STACKCHECK
526 if (PyOS_CheckStack()) {
527 --tstate->recursion_depth;
528 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
529 return -1;
530 }
531#endif
532 if (tstate->recursion_depth > recursion_limit) {
533 --tstate->recursion_depth;
534 PyErr_Format(PyExc_RuntimeError,
535 "maximum recursion depth exceeded%s",
536 where);
537 return -1;
538 }
539 _Py_CheckRecursionLimit = recursion_limit;
540 return 0;
541}
542
543
Guido van Rossum374a9221991-04-04 10:40:29 +0000544/* Status code for main loop (reason for stack unwind) */
545
546enum why_code {
547 WHY_NOT, /* No error */
548 WHY_EXCEPTION, /* Exception occurred */
549 WHY_RERAISE, /* Exception re-raised by 'finally' */
550 WHY_RETURN, /* 'return' statement */
Jeremy Hylton3faa52e2001-02-01 22:48:12 +0000551 WHY_BREAK, /* 'break' statement */
Tim Peters5ca576e2001-06-18 22:08:13 +0000552 WHY_CONTINUE, /* 'continue' statement */
Tim Peters6e6a63f2001-10-18 20:49:35 +0000553 WHY_YIELD /* 'yield' operator */
Guido van Rossum374a9221991-04-04 10:40:29 +0000554};
555
Tim Petersdbd9ba62000-07-09 03:09:57 +0000556static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
Tim Petersd6d010b2001-06-21 02:49:55 +0000557static int unpack_iterable(PyObject *, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000558
Skip Montanarod581d772002-09-03 20:10:45 +0000559/* for manipulating the thread switch and periodic "stuff" - used to be
560 per thread, now just a pair o' globals */
Skip Montanaro99dba272002-09-03 20:19:06 +0000561int _Py_CheckInterval = 100;
562volatile int _Py_Ticker = 100;
Guido van Rossum374a9221991-04-04 10:40:29 +0000563
Guido van Rossumb209a111997-04-29 18:18:01 +0000564PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000565PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000566{
Jeremy Hylton985eba52003-02-05 23:13:00 +0000567 /* XXX raise SystemError if globals is NULL */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000568 return PyEval_EvalCodeEx(co,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000569 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000570 (PyObject **)NULL, 0,
571 (PyObject **)NULL, 0,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000572 (PyObject **)NULL, 0,
573 NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000574}
575
576
577/* Interpreter main loop */
578
Tim Peters6d6c1a32001-08-02 04:15:00 +0000579static PyObject *
Tim Peters5ca576e2001-06-18 22:08:13 +0000580eval_frame(PyFrameObject *f)
Guido van Rossum374a9221991-04-04 10:40:29 +0000581{
Guido van Rossum950361c1997-01-24 13:49:28 +0000582#ifdef DXPAIRS
583 int lastopcode = 0;
584#endif
Tim Petersb6d14da2001-12-19 04:11:07 +0000585 PyObject **stack_pointer; /* Next free slot in value stack */
Guido van Rossum374a9221991-04-04 10:40:29 +0000586 register unsigned char *next_instr;
Moshe Zadkaaa39a7e2000-08-07 06:34:45 +0000587 register int opcode=0; /* Current opcode */
588 register int oparg=0; /* Current opcode argument, if any */
Guido van Rossum374a9221991-04-04 10:40:29 +0000589 register enum why_code why; /* Reason for block stack unwind */
590 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000591 register PyObject *x; /* Result object -- NULL if error */
592 register PyObject *v; /* Temporary objects popped off stack */
593 register PyObject *w;
594 register PyObject *u;
595 register PyObject *t;
Barry Warsaw23c9ec82000-08-21 15:44:01 +0000596 register PyObject *stream = NULL; /* for PRINT opcodes */
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000597 register PyObject **fastlocals, **freevars;
Guido van Rossum014518f1998-11-23 21:09:51 +0000598 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000599 PyThreadState *tstate = PyThreadState_GET();
Tim Peters5ca576e2001-06-18 22:08:13 +0000600 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000601
602 /* when tracing we set things up so that
603
604 not (instr_lb <= current_bytecode_offset < instr_ub)
605
606 is true when the line being executed has changed. The
607 initial values are such as to make this false the first
608 time it is tested. */
609 int instr_ub = -1, instr_lb = 0;
610
Guido van Rossumd076c731998-10-07 19:42:25 +0000611 unsigned char *first_instr;
Skip Montanaro04d80f82002-08-04 21:03:35 +0000612 PyObject *names;
613 PyObject *consts;
Guido van Rossum96a42c81992-01-12 02:29:51 +0000614#ifdef LLTRACE
Guido van Rossumacbe8da1993-04-15 15:33:52 +0000615 int lltrace;
Guido van Rossum374a9221991-04-04 10:40:29 +0000616#endif
Guido van Rossum408027e1996-12-30 16:17:54 +0000617#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000618 /* Make it easier to find out where we are with a debugger */
Tim Peters5ca576e2001-06-18 22:08:13 +0000619 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000620#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000621
Neal Norwitza81d2202002-07-14 00:27:26 +0000622/* Tuple access macros */
623
624#ifndef Py_DEBUG
625#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
626#else
627#define GETITEM(v, i) PyTuple_GetItem((v), (i))
628#endif
629
Guido van Rossum374a9221991-04-04 10:40:29 +0000630/* Code access macros */
631
Guido van Rossumd076c731998-10-07 19:42:25 +0000632#define INSTR_OFFSET() (next_instr - first_instr)
Guido van Rossum374a9221991-04-04 10:40:29 +0000633#define NEXTOP() (*next_instr++)
634#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Guido van Rossumd076c731998-10-07 19:42:25 +0000635#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000636#define JUMPBY(x) (next_instr += (x))
637
Raymond Hettingerf606f872003-03-16 03:11:04 +0000638/* OpCode prediction macros
639 Some opcodes tend to come in pairs thus making it possible to predict
640 the second code when the first is run. For example, COMPARE_OP is often
641 followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And, those opcodes are often
642 followed by a POP_TOP.
643
644 Verifying the prediction costs a single high-speed test of register
Raymond Hettingerac2072922003-03-16 15:41:11 +0000645 variable against a constant. If the pairing was good, then the
Raymond Hettingerf606f872003-03-16 03:11:04 +0000646 processor has a high likelihood of making its own successful branch
647 prediction which results in a nearly zero overhead transition to the
648 next opcode.
649
650 A successful prediction saves a trip through the eval-loop including
651 its two unpredictable branches, the HASARG test and the switch-case.
652*/
653
Raymond Hettingerac2072922003-03-16 15:41:11 +0000654#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingerf606f872003-03-16 03:11:04 +0000655#define PREDICTED(op) PRED_##op: next_instr++
Raymond Hettinger7dc52212003-03-16 20:14:44 +0000656#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = (next_instr[2]<<8) + \
657 next_instr[1]; next_instr += 3
Raymond Hettingerf606f872003-03-16 03:11:04 +0000658
Guido van Rossum374a9221991-04-04 10:40:29 +0000659/* Stack manipulation macros */
660
661#define STACK_LEVEL() (stack_pointer - f->f_valuestack)
662#define EMPTY() (STACK_LEVEL() == 0)
663#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000664#define SECOND() (stack_pointer[-2])
665#define THIRD() (stack_pointer[-3])
666#define FOURTH() (stack_pointer[-4])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000667#define SET_TOP(v) (stack_pointer[-1] = (v))
668#define SET_SECOND(v) (stack_pointer[-2] = (v))
669#define SET_THIRD(v) (stack_pointer[-3] = (v))
670#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000671#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000672#define BASIC_PUSH(v) (*stack_pointer++ = (v))
673#define BASIC_POP() (*--stack_pointer)
674
Guido van Rossum96a42c81992-01-12 02:29:51 +0000675#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000676#define PUSH(v) { (void)(BASIC_PUSH(v), \
677 lltrace && prtrace(TOP(), "push")); \
678 assert(STACK_LEVEL() <= f->f_stacksize); }
Fred Drakede26cfc2001-10-13 06:11:28 +0000679#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000680#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
681 lltrace && prtrace(TOP(), "stackadj")); \
682 assert(STACK_LEVEL() <= f->f_stacksize); }
Guido van Rossum374a9221991-04-04 10:40:29 +0000683#else
684#define PUSH(v) BASIC_PUSH(v)
685#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000686#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000687#endif
688
Guido van Rossum681d79a1995-07-18 14:51:37 +0000689/* Local variable macros */
690
691#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000692
693/* The SETLOCAL() macro must not DECREF the local variable in-place and
694 then store the new value; it must copy the old value to a temporary
695 value, then store the new value, and then DECREF the temporary value.
696 This is because it is possible that during the DECREF the frame is
697 accessed by other code (e.g. a __del__ method or gc.collect()) and the
698 variable would be pointing to already-freed memory. */
699#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
700 GETLOCAL(i) = value; \
701 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000702
Guido van Rossuma027efa1997-05-05 20:56:21 +0000703/* Start of code */
704
Tim Peters5ca576e2001-06-18 22:08:13 +0000705 if (f == NULL)
706 return NULL;
707
Armin Rigo1d313ab2003-10-25 14:33:09 +0000708 /* push frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000709 if (Py_EnterRecursiveCall(""))
Armin Rigo1d313ab2003-10-25 14:33:09 +0000710 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000711
Tim Peters5ca576e2001-06-18 22:08:13 +0000712 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000713
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000714 if (tstate->use_tracing) {
715 if (tstate->c_tracefunc != NULL) {
716 /* tstate->c_tracefunc, if defined, is a
717 function that will be called on *every* entry
718 to a code block. Its return value, if not
719 None, is a function that will be called at
720 the start of each executed line of code.
721 (Actually, the function must return itself
722 in order to continue tracing.) The trace
723 functions are called with three arguments:
724 a pointer to the current frame, a string
725 indicating why the function is called, and
726 an argument which depends on the situation.
727 The global trace function is also called
728 whenever an exception is detected. */
729 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
730 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000731 /* Trace function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000732 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000733 }
734 }
735 if (tstate->c_profilefunc != NULL) {
736 /* Similar for c_profilefunc, except it needn't
737 return itself and isn't called for "line" events */
738 if (call_trace(tstate->c_profilefunc,
739 tstate->c_profileobj,
740 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000741 /* Profile function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000742 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000743 }
744 }
745 }
746
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000747 co = f->f_code;
748 names = co->co_names;
749 consts = co->co_consts;
750 fastlocals = f->f_localsplus;
751 freevars = f->f_localsplus + f->f_nlocals;
752 _PyCode_GETCODEPTR(co, &first_instr);
753 /* An explanation is in order for the next line.
754
755 f->f_lasti now refers to the index of the last instruction
756 executed. You might think this was obvious from the name, but
757 this wasn't always true before 2.3! PyFrame_New now sets
758 f->f_lasti to -1 (i.e. the index *before* the first instruction)
759 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
760 does work. Promise. */
761 next_instr = first_instr + f->f_lasti + 1;
762 stack_pointer = f->f_stacktop;
763 assert(stack_pointer != NULL);
764 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
765
Tim Peters5ca576e2001-06-18 22:08:13 +0000766#ifdef LLTRACE
767 lltrace = PyDict_GetItemString(f->f_globals,"__lltrace__") != NULL;
768#endif
769#if defined(Py_DEBUG) || defined(LLTRACE)
770 filename = PyString_AsString(co->co_filename);
771#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000772
Guido van Rossum374a9221991-04-04 10:40:29 +0000773 why = WHY_NOT;
774 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +0000775 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +0000776 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +0000777
Guido van Rossum374a9221991-04-04 10:40:29 +0000778 for (;;) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000779 assert(stack_pointer >= f->f_valuestack); /* else underflow */
780 assert(STACK_LEVEL() <= f->f_stacksize); /* else overflow */
781
Guido van Rossuma027efa1997-05-05 20:56:21 +0000782 /* Do periodic things. Doing this every time through
783 the loop would add too much overhead, so we do it
784 only every Nth instruction. We also do it if
785 ``things_to_do'' is set, i.e. when an asynchronous
786 event needs attention (e.g. a signal handler or
787 async I/O handler); see Py_AddPendingCall() and
788 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000789
Skip Montanarod581d772002-09-03 20:10:45 +0000790 if (--_Py_Ticker < 0) {
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000791 if (*next_instr == SETUP_FINALLY) {
792 /* Make the last opcode before
793 a try: finally: block uninterruptable. */
794 goto fast_next_opcode;
795 }
Skip Montanarod581d772002-09-03 20:10:45 +0000796 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000797 tstate->tick_counter++;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000798 if (things_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +0000799 if (Py_MakePendingCalls() < 0) {
800 why = WHY_EXCEPTION;
801 goto on_error;
802 }
803 }
Guido van Rossumdf0d00e1997-05-20 15:57:49 +0000804#if !defined(HAVE_SIGNAL_H) || defined(macintosh)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000805 /* If we have true signals, the signal handler
806 will call Py_AddPendingCall() so we don't
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000807 have to call PyErr_CheckSignals(). On the
808 Mac and DOS, alas, we have to call it. */
Guido van Rossumb209a111997-04-29 18:18:01 +0000809 if (PyErr_CheckSignals()) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000810 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +0000811 goto on_error;
812 }
Guido van Rossum70d44781997-01-21 06:15:24 +0000813#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000814
Guido van Rossume59214e1994-08-30 08:01:59 +0000815#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000816 if (interpreter_lock) {
817 /* Give another thread a chance */
818
Guido van Rossum25ce5661997-08-02 03:10:38 +0000819 if (PyThreadState_Swap(NULL) != tstate)
820 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000821 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000822
823 /* Other threads may run now */
824
Guido van Rossum65d5b571998-12-21 19:32:43 +0000825 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000826 if (PyThreadState_Swap(tstate) != NULL)
827 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000828
829 /* Check for thread interrupts */
830
831 if (tstate->async_exc != NULL) {
832 x = tstate->async_exc;
833 tstate->async_exc = NULL;
834 PyErr_SetNone(x);
835 Py_DECREF(x);
836 why = WHY_EXCEPTION;
837 goto on_error;
838 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000839 }
840#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000841 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000842
Neil Schemenauer63543862002-02-17 19:10:14 +0000843 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +0000844 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +0000845
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000846 /* line-by-line tracing support */
847
848 if (tstate->c_tracefunc != NULL && !tstate->tracing) {
849 /* see maybe_call_line_trace
850 for expository comments */
851 f->f_stacktop = stack_pointer;
Michael W. Hudson006c7522002-11-08 13:08:46 +0000852
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000853 err = maybe_call_line_trace(tstate->c_tracefunc,
854 tstate->c_traceobj,
855 f, &instr_lb, &instr_ub);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000856 /* Reload possibly changed frame fields */
857 JUMPTO(f->f_lasti);
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000858 if (f->f_stacktop != NULL) {
859 stack_pointer = f->f_stacktop;
860 f->f_stacktop = NULL;
861 }
862 if (err) {
863 /* trace function raised an exception */
864 goto on_error;
865 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000866 }
867
868 /* Extract opcode and argument */
869
Guido van Rossum374a9221991-04-04 10:40:29 +0000870 opcode = NEXTOP();
871 if (HAS_ARG(opcode))
872 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +0000873 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +0000874#ifdef DYNAMIC_EXECUTION_PROFILE
875#ifdef DXPAIRS
876 dxpairs[lastopcode][opcode]++;
877 lastopcode = opcode;
878#endif
879 dxp[opcode]++;
880#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000881
Guido van Rossum96a42c81992-01-12 02:29:51 +0000882#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +0000883 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +0000884
Guido van Rossum96a42c81992-01-12 02:29:51 +0000885 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000886 if (HAS_ARG(opcode)) {
887 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000888 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +0000889 }
890 else {
891 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000892 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +0000893 }
894 }
895#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000896
Guido van Rossum374a9221991-04-04 10:40:29 +0000897 /* Main switch on opcode */
Jeremy Hylton52820442001-01-03 23:52:36 +0000898
Guido van Rossum374a9221991-04-04 10:40:29 +0000899 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +0000900
Guido van Rossum374a9221991-04-04 10:40:29 +0000901 /* BEWARE!
902 It is essential that any operation that fails sets either
903 x to NULL, err to nonzero, or why to anything but WHY_NOT,
904 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000905
Guido van Rossum374a9221991-04-04 10:40:29 +0000906 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000907
Neil Schemenauer63543862002-02-17 19:10:14 +0000908 case LOAD_FAST:
909 x = GETLOCAL(oparg);
910 if (x != NULL) {
911 Py_INCREF(x);
912 PUSH(x);
913 goto fast_next_opcode;
914 }
915 format_exc_check_arg(PyExc_UnboundLocalError,
916 UNBOUNDLOCAL_ERROR_MSG,
917 PyTuple_GetItem(co->co_varnames, oparg));
918 break;
919
920 case LOAD_CONST:
Skip Montanaro04d80f82002-08-04 21:03:35 +0000921 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +0000922 Py_INCREF(x);
923 PUSH(x);
924 goto fast_next_opcode;
925
Raymond Hettinger7dc52212003-03-16 20:14:44 +0000926 PREDICTED_WITH_ARG(STORE_FAST);
Neil Schemenauer63543862002-02-17 19:10:14 +0000927 case STORE_FAST:
928 v = POP();
929 SETLOCAL(oparg, v);
930 goto fast_next_opcode;
931
Raymond Hettingerf606f872003-03-16 03:11:04 +0000932 PREDICTED(POP_TOP);
Guido van Rossum374a9221991-04-04 10:40:29 +0000933 case POP_TOP:
934 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000935 Py_DECREF(v);
Neil Schemenauer63543862002-02-17 19:10:14 +0000936 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000937
Guido van Rossum374a9221991-04-04 10:40:29 +0000938 case ROT_TWO:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000939 v = TOP();
940 w = SECOND();
941 SET_TOP(w);
942 SET_SECOND(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000943 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000944
Guido van Rossum374a9221991-04-04 10:40:29 +0000945 case ROT_THREE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000946 v = TOP();
947 w = SECOND();
948 x = THIRD();
949 SET_TOP(w);
950 SET_SECOND(x);
951 SET_THIRD(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000952 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000953
Thomas Wouters434d0822000-08-24 20:11:32 +0000954 case ROT_FOUR:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000955 u = TOP();
956 v = SECOND();
957 w = THIRD();
958 x = FOURTH();
959 SET_TOP(v);
960 SET_SECOND(w);
961 SET_THIRD(x);
962 SET_FOURTH(u);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000963 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000964
Guido van Rossum374a9221991-04-04 10:40:29 +0000965 case DUP_TOP:
966 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000967 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000968 PUSH(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000969 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000970
Thomas Wouters434d0822000-08-24 20:11:32 +0000971 case DUP_TOPX:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000972 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000973 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000974 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000975 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000976 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000977 STACKADJ(2);
978 SET_TOP(x);
979 SET_SECOND(w);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000980 goto fast_next_opcode;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000981 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000982 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000983 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000984 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000985 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000986 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +0000987 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000988 STACKADJ(3);
989 SET_TOP(x);
990 SET_SECOND(w);
991 SET_THIRD(v);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000992 goto fast_next_opcode;
Thomas Wouters434d0822000-08-24 20:11:32 +0000993 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000994 Py_FatalError("invalid argument to DUP_TOPX"
995 " (bytecode corruption?)");
Tim Peters35ba6892000-10-11 07:04:49 +0000996 break;
Thomas Wouters434d0822000-08-24 20:11:32 +0000997
Guido van Rossum374a9221991-04-04 10:40:29 +0000998 case UNARY_POSITIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000999 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001000 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001001 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001002 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001003 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001004 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001005
Guido van Rossum374a9221991-04-04 10:40:29 +00001006 case UNARY_NEGATIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001007 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001008 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001009 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001010 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001011 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001012 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001013
Guido van Rossum374a9221991-04-04 10:40:29 +00001014 case UNARY_NOT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001015 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001016 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001017 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +00001018 if (err == 0) {
1019 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001020 SET_TOP(Py_True);
Guido van Rossumfc490731997-05-06 15:06:49 +00001021 continue;
1022 }
1023 else if (err > 0) {
1024 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001025 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +00001026 err = 0;
1027 continue;
1028 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00001029 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001030 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001031
Guido van Rossum374a9221991-04-04 10:40:29 +00001032 case UNARY_CONVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001033 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001034 x = PyObject_Repr(v);
1035 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001036 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001037 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001038 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001039
Guido van Rossum7928cd71991-10-24 14:59:31 +00001040 case UNARY_INVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001041 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001042 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001043 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001044 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001045 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001046 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001047
Guido van Rossum50564e81996-01-12 01:13:16 +00001048 case BINARY_POWER:
1049 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001050 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001051 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +00001052 Py_DECREF(v);
1053 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001054 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001055 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +00001056 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001057
Guido van Rossum374a9221991-04-04 10:40:29 +00001058 case BINARY_MULTIPLY:
1059 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001060 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001061 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001062 Py_DECREF(v);
1063 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001064 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001065 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001066 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001067
Guido van Rossum374a9221991-04-04 10:40:29 +00001068 case BINARY_DIVIDE:
Tim Peters3caca232001-12-06 06:23:26 +00001069 if (!_Py_QnewFlag) {
1070 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001071 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001072 x = PyNumber_Divide(v, w);
1073 Py_DECREF(v);
1074 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001075 SET_TOP(x);
Tim Peters3caca232001-12-06 06:23:26 +00001076 if (x != NULL) continue;
1077 break;
1078 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001079 /* -Qnew is in effect: fall through to
Tim Peters3caca232001-12-06 06:23:26 +00001080 BINARY_TRUE_DIVIDE */
1081 case BINARY_TRUE_DIVIDE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001082 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001083 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001084 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001085 Py_DECREF(v);
1086 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001087 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001088 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001089 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001090
Guido van Rossum4668b002001-08-08 05:00:18 +00001091 case BINARY_FLOOR_DIVIDE:
1092 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001093 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001094 x = PyNumber_FloorDivide(v, w);
1095 Py_DECREF(v);
1096 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001097 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001098 if (x != NULL) continue;
1099 break;
1100
Guido van Rossum374a9221991-04-04 10:40:29 +00001101 case BINARY_MODULO:
1102 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001103 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001104 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001105 Py_DECREF(v);
1106 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001107 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001108 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001109 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001110
Guido van Rossum374a9221991-04-04 10:40:29 +00001111 case BINARY_ADD:
1112 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001113 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001114 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001115 /* INLINE: int + int */
1116 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001117 a = PyInt_AS_LONG(v);
1118 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001119 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001120 if ((i^a) < 0 && (i^b) < 0)
1121 goto slow_add;
1122 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001123 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001124 else {
1125 slow_add:
Guido van Rossumc12da691997-07-17 23:12:42 +00001126 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001127 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001128 Py_DECREF(v);
1129 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001130 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001131 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001132 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001133
Guido van Rossum374a9221991-04-04 10:40:29 +00001134 case BINARY_SUBTRACT:
1135 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001136 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001137 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001138 /* INLINE: int - int */
1139 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001140 a = PyInt_AS_LONG(v);
1141 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001142 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001143 if ((i^a) < 0 && (i^~b) < 0)
1144 goto slow_sub;
1145 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001146 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001147 else {
1148 slow_sub:
Guido van Rossumc12da691997-07-17 23:12:42 +00001149 x = PyNumber_Subtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001150 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001151 Py_DECREF(v);
1152 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001153 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001154 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001155 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001156
Guido van Rossum374a9221991-04-04 10:40:29 +00001157 case BINARY_SUBSCR:
1158 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001159 v = TOP();
Tim Petersb1c46982001-10-05 20:41:38 +00001160 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001161 /* INLINE: list[int] */
1162 long i = PyInt_AsLong(w);
1163 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +00001164 i += PyList_GET_SIZE(v);
Guido van Rossumc12da691997-07-17 23:12:42 +00001165 if (i < 0 ||
Guido van Rossumfa00e951998-07-08 15:02:37 +00001166 i >= PyList_GET_SIZE(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001167 PyErr_SetString(PyExc_IndexError,
1168 "list index out of range");
1169 x = NULL;
1170 }
1171 else {
Guido van Rossumfa00e951998-07-08 15:02:37 +00001172 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001173 Py_INCREF(x);
1174 }
1175 }
1176 else
1177 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001178 Py_DECREF(v);
1179 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001180 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001181 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001182 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001183
Guido van Rossum7928cd71991-10-24 14:59:31 +00001184 case BINARY_LSHIFT:
1185 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001186 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001187 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001188 Py_DECREF(v);
1189 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001190 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001191 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001192 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001193
Guido van Rossum7928cd71991-10-24 14:59:31 +00001194 case BINARY_RSHIFT:
1195 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001196 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001197 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001198 Py_DECREF(v);
1199 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001200 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001201 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001202 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001203
Guido van Rossum7928cd71991-10-24 14:59:31 +00001204 case BINARY_AND:
1205 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001206 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001207 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001208 Py_DECREF(v);
1209 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001210 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001211 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001212 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001213
Guido van Rossum7928cd71991-10-24 14:59:31 +00001214 case BINARY_XOR:
1215 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001216 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001217 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001218 Py_DECREF(v);
1219 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001220 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001221 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001222 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001223
Guido van Rossum7928cd71991-10-24 14:59:31 +00001224 case BINARY_OR:
1225 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001226 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001227 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001228 Py_DECREF(v);
1229 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001230 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001231 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001232 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001233
1234 case INPLACE_POWER:
1235 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001236 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001237 x = PyNumber_InPlacePower(v, w, Py_None);
1238 Py_DECREF(v);
1239 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001240 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001241 if (x != NULL) continue;
1242 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001243
Thomas Wouters434d0822000-08-24 20:11:32 +00001244 case INPLACE_MULTIPLY:
1245 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001246 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001247 x = PyNumber_InPlaceMultiply(v, w);
1248 Py_DECREF(v);
1249 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001250 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001251 if (x != NULL) continue;
1252 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001253
Thomas Wouters434d0822000-08-24 20:11:32 +00001254 case INPLACE_DIVIDE:
Tim Peters54b11912001-12-25 18:49:11 +00001255 if (!_Py_QnewFlag) {
1256 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001257 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001258 x = PyNumber_InPlaceDivide(v, w);
1259 Py_DECREF(v);
1260 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001261 SET_TOP(x);
Tim Peters54b11912001-12-25 18:49:11 +00001262 if (x != NULL) continue;
1263 break;
1264 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001265 /* -Qnew is in effect: fall through to
Tim Peters54b11912001-12-25 18:49:11 +00001266 INPLACE_TRUE_DIVIDE */
1267 case INPLACE_TRUE_DIVIDE:
Thomas Wouters434d0822000-08-24 20:11:32 +00001268 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001269 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001270 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001271 Py_DECREF(v);
1272 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001273 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001274 if (x != NULL) continue;
1275 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001276
Guido van Rossum4668b002001-08-08 05:00:18 +00001277 case INPLACE_FLOOR_DIVIDE:
1278 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001279 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001280 x = PyNumber_InPlaceFloorDivide(v, w);
1281 Py_DECREF(v);
1282 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001283 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001284 if (x != NULL) continue;
1285 break;
1286
Thomas Wouters434d0822000-08-24 20:11:32 +00001287 case INPLACE_MODULO:
1288 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001289 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001290 x = PyNumber_InPlaceRemainder(v, w);
1291 Py_DECREF(v);
1292 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001293 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001294 if (x != NULL) continue;
1295 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001296
Thomas Wouters434d0822000-08-24 20:11:32 +00001297 case INPLACE_ADD:
1298 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001299 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001300 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001301 /* INLINE: int + int */
1302 register long a, b, i;
1303 a = PyInt_AS_LONG(v);
1304 b = PyInt_AS_LONG(w);
1305 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001306 if ((i^a) < 0 && (i^b) < 0)
1307 goto slow_iadd;
1308 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001309 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001310 else {
1311 slow_iadd:
Thomas Wouters434d0822000-08-24 20:11:32 +00001312 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001313 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001314 Py_DECREF(v);
1315 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001316 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001317 if (x != NULL) continue;
1318 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001319
Thomas Wouters434d0822000-08-24 20:11:32 +00001320 case INPLACE_SUBTRACT:
1321 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001322 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001323 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001324 /* INLINE: int - int */
1325 register long a, b, i;
1326 a = PyInt_AS_LONG(v);
1327 b = PyInt_AS_LONG(w);
1328 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001329 if ((i^a) < 0 && (i^~b) < 0)
1330 goto slow_isub;
1331 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001332 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001333 else {
1334 slow_isub:
Thomas Wouters434d0822000-08-24 20:11:32 +00001335 x = PyNumber_InPlaceSubtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001336 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001337 Py_DECREF(v);
1338 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001339 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001340 if (x != NULL) continue;
1341 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001342
Thomas Wouters434d0822000-08-24 20:11:32 +00001343 case INPLACE_LSHIFT:
1344 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001345 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001346 x = PyNumber_InPlaceLshift(v, w);
1347 Py_DECREF(v);
1348 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001349 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001350 if (x != NULL) continue;
1351 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001352
Thomas Wouters434d0822000-08-24 20:11:32 +00001353 case INPLACE_RSHIFT:
1354 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001355 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001356 x = PyNumber_InPlaceRshift(v, w);
1357 Py_DECREF(v);
1358 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001359 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001360 if (x != NULL) continue;
1361 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001362
Thomas Wouters434d0822000-08-24 20:11:32 +00001363 case INPLACE_AND:
1364 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001365 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001366 x = PyNumber_InPlaceAnd(v, w);
1367 Py_DECREF(v);
1368 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001369 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001370 if (x != NULL) continue;
1371 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001372
Thomas Wouters434d0822000-08-24 20:11:32 +00001373 case INPLACE_XOR:
1374 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001375 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001376 x = PyNumber_InPlaceXor(v, w);
1377 Py_DECREF(v);
1378 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001379 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001380 if (x != NULL) continue;
1381 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001382
Thomas Wouters434d0822000-08-24 20:11:32 +00001383 case INPLACE_OR:
1384 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001385 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001386 x = PyNumber_InPlaceOr(v, w);
1387 Py_DECREF(v);
1388 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001389 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001390 if (x != NULL) continue;
1391 break;
1392
Guido van Rossum374a9221991-04-04 10:40:29 +00001393 case SLICE+0:
1394 case SLICE+1:
1395 case SLICE+2:
1396 case SLICE+3:
1397 if ((opcode-SLICE) & 2)
1398 w = POP();
1399 else
1400 w = NULL;
1401 if ((opcode-SLICE) & 1)
1402 v = POP();
1403 else
1404 v = NULL;
Raymond Hettinger663004b2003-01-09 15:24:30 +00001405 u = TOP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001406 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001407 Py_DECREF(u);
1408 Py_XDECREF(v);
1409 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001410 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001411 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001412 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001413
Guido van Rossum374a9221991-04-04 10:40:29 +00001414 case STORE_SLICE+0:
1415 case STORE_SLICE+1:
1416 case STORE_SLICE+2:
1417 case STORE_SLICE+3:
1418 if ((opcode-STORE_SLICE) & 2)
1419 w = POP();
1420 else
1421 w = NULL;
1422 if ((opcode-STORE_SLICE) & 1)
1423 v = POP();
1424 else
1425 v = NULL;
1426 u = POP();
1427 t = POP();
1428 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +00001429 Py_DECREF(t);
1430 Py_DECREF(u);
1431 Py_XDECREF(v);
1432 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001433 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001434 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001435
Guido van Rossum374a9221991-04-04 10:40:29 +00001436 case DELETE_SLICE+0:
1437 case DELETE_SLICE+1:
1438 case DELETE_SLICE+2:
1439 case DELETE_SLICE+3:
1440 if ((opcode-DELETE_SLICE) & 2)
1441 w = POP();
1442 else
1443 w = NULL;
1444 if ((opcode-DELETE_SLICE) & 1)
1445 v = POP();
1446 else
1447 v = NULL;
1448 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001449 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +00001450 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00001451 Py_DECREF(u);
1452 Py_XDECREF(v);
1453 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001454 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001455 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001456
Guido van Rossum374a9221991-04-04 10:40:29 +00001457 case STORE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001458 w = TOP();
1459 v = SECOND();
1460 u = THIRD();
1461 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001462 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001463 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001464 Py_DECREF(u);
1465 Py_DECREF(v);
1466 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001467 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001468 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001469
Guido van Rossum374a9221991-04-04 10:40:29 +00001470 case DELETE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001471 w = TOP();
1472 v = SECOND();
1473 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001474 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001475 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001476 Py_DECREF(v);
1477 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001478 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001479 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001480
Guido van Rossum374a9221991-04-04 10:40:29 +00001481 case PRINT_EXPR:
1482 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001483 w = PySys_GetObject("displayhook");
1484 if (w == NULL) {
1485 PyErr_SetString(PyExc_RuntimeError,
1486 "lost sys.displayhook");
1487 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001488 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001489 }
1490 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001491 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001492 if (x == NULL)
1493 err = -1;
1494 }
1495 if (err == 0) {
1496 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001497 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001498 if (w == NULL)
1499 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001500 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001501 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001502 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001503 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001504
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001505 case PRINT_ITEM_TO:
1506 w = stream = POP();
1507 /* fall through to PRINT_ITEM */
1508
Guido van Rossum374a9221991-04-04 10:40:29 +00001509 case PRINT_ITEM:
1510 v = POP();
Barry Warsaw093abe02000-08-29 04:56:13 +00001511 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001512 w = PySys_GetObject("stdout");
1513 if (w == NULL) {
1514 PyErr_SetString(PyExc_RuntimeError,
1515 "lost sys.stdout");
1516 err = -1;
1517 }
Guido van Rossum8f183201997-12-31 05:53:15 +00001518 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001519 /* PyFile_SoftSpace() can exececute arbitrary code
1520 if sys.stdout is an instance with a __getattr__.
1521 If __getattr__ raises an exception, w will
1522 be freed, so we need to prevent that temporarily. */
1523 Py_XINCREF(w);
Tim Peters8e5fd532002-03-24 19:25:00 +00001524 if (w != NULL && PyFile_SoftSpace(w, 0))
Guido van Rossumbe270261997-05-22 22:26:18 +00001525 err = PyFile_WriteString(" ", w);
1526 if (err == 0)
1527 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001528 if (err == 0) {
Tim Peters8e5fd532002-03-24 19:25:00 +00001529 /* XXX move into writeobject() ? */
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001530 if (PyString_Check(v)) {
1531 char *s = PyString_AS_STRING(v);
1532 int len = PyString_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001533 if (len == 0 ||
1534 !isspace(Py_CHARMASK(s[len-1])) ||
1535 s[len-1] == ' ')
1536 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001537 }
Martin v. Löwis8d3ce5a2001-12-18 22:36:40 +00001538#ifdef Py_USING_UNICODE
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001539 else if (PyUnicode_Check(v)) {
1540 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
1541 int len = PyUnicode_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001542 if (len == 0 ||
1543 !Py_UNICODE_ISSPACE(s[len-1]) ||
1544 s[len-1] == ' ')
1545 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001546 }
Michael W. Hudsond95c8282002-05-20 13:56:11 +00001547#endif
Tim Peters8e5fd532002-03-24 19:25:00 +00001548 else
1549 PyFile_SoftSpace(w, 1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001550 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001551 Py_XDECREF(w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001552 Py_DECREF(v);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001553 Py_XDECREF(stream);
1554 stream = NULL;
1555 if (err == 0)
1556 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001557 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001558
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001559 case PRINT_NEWLINE_TO:
1560 w = stream = POP();
1561 /* fall through to PRINT_NEWLINE */
1562
Guido van Rossum374a9221991-04-04 10:40:29 +00001563 case PRINT_NEWLINE:
Barry Warsaw093abe02000-08-29 04:56:13 +00001564 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001565 w = PySys_GetObject("stdout");
1566 if (w == NULL)
1567 PyErr_SetString(PyExc_RuntimeError,
1568 "lost sys.stdout");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001569 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001570 if (w != NULL) {
1571 err = PyFile_WriteString("\n", w);
1572 if (err == 0)
1573 PyFile_SoftSpace(w, 0);
1574 }
1575 Py_XDECREF(stream);
1576 stream = NULL;
Guido van Rossum374a9221991-04-04 10:40:29 +00001577 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001578
Thomas Wouters434d0822000-08-24 20:11:32 +00001579
1580#ifdef CASE_TOO_BIG
1581 default: switch (opcode) {
1582#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001583 case BREAK_LOOP:
1584 why = WHY_BREAK;
1585 break;
Guido van Rossum66b0e9c2001-03-21 19:17:22 +00001586
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00001587 case CONTINUE_LOOP:
1588 retval = PyInt_FromLong(oparg);
1589 why = WHY_CONTINUE;
1590 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001591
Guido van Rossumf10570b1995-07-07 22:53:21 +00001592 case RAISE_VARARGS:
1593 u = v = w = NULL;
1594 switch (oparg) {
1595 case 3:
1596 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001597 /* Fallthrough */
1598 case 2:
1599 v = POP(); /* value */
1600 /* Fallthrough */
1601 case 1:
1602 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001603 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001604 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001605 break;
1606 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001607 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001608 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001609 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001610 break;
1611 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001612 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001613
Guido van Rossum374a9221991-04-04 10:40:29 +00001614 case LOAD_LOCALS:
Guido van Rossum681d79a1995-07-18 14:51:37 +00001615 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001616 PyErr_SetString(PyExc_SystemError,
1617 "no locals");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001618 break;
1619 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001620 Py_INCREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001621 PUSH(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001622 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001623
Guido van Rossum374a9221991-04-04 10:40:29 +00001624 case RETURN_VALUE:
1625 retval = POP();
1626 why = WHY_RETURN;
1627 break;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001628
Tim Peters5ca576e2001-06-18 22:08:13 +00001629 case YIELD_VALUE:
1630 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001631 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001632 why = WHY_YIELD;
1633 break;
1634
1635
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001636 case EXEC_STMT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001637 w = TOP();
1638 v = SECOND();
1639 u = THIRD();
1640 STACKADJ(-3);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001641 err = exec_statement(f, u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001642 Py_DECREF(u);
1643 Py_DECREF(v);
1644 Py_DECREF(w);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001645 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001646
Guido van Rossum374a9221991-04-04 10:40:29 +00001647 case POP_BLOCK:
1648 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001649 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001650 while (STACK_LEVEL() > b->b_level) {
1651 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001652 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001653 }
1654 }
1655 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001656
Guido van Rossum374a9221991-04-04 10:40:29 +00001657 case END_FINALLY:
1658 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001659 if (PyInt_Check(v)) {
Raymond Hettinger080cb322003-03-14 01:37:42 +00001660 why = (enum why_code) PyInt_AS_LONG(v);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00001661 if (why == WHY_RETURN ||
Tim Peters5ca576e2001-06-18 22:08:13 +00001662 why == WHY_YIELD ||
Guido van Rossumc5fe5eb2002-06-12 03:45:21 +00001663 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001664 retval = POP();
1665 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001666 else if (PyString_Check(v) || PyClass_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001667 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001668 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001669 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001670 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001671 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001672 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001673 else if (v != Py_None) {
1674 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001675 "'finally' pops bad exception");
1676 why = WHY_EXCEPTION;
1677 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001678 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001679 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001680
Guido van Rossum374a9221991-04-04 10:40:29 +00001681 case BUILD_CLASS:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001682 u = TOP();
1683 v = SECOND();
1684 w = THIRD();
1685 STACKADJ(-2);
Guido van Rossum25831651993-05-19 14:50:45 +00001686 x = build_class(u, v, w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001687 SET_TOP(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001688 Py_DECREF(u);
1689 Py_DECREF(v);
1690 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001691 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001692
Guido van Rossum374a9221991-04-04 10:40:29 +00001693 case STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001694 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001695 v = POP();
Guido van Rossum681d79a1995-07-18 14:51:37 +00001696 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001697 PyErr_Format(PyExc_SystemError,
1698 "no locals found when storing %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001699 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001700 break;
1701 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001702 err = PyDict_SetItem(x, w, v);
1703 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001704 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001705
Guido van Rossum374a9221991-04-04 10:40:29 +00001706 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001707 w = GETITEM(names, oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001708 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001709 PyErr_Format(PyExc_SystemError,
1710 "no locals when deleting %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001711 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001712 break;
1713 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001714 if ((err = PyDict_DelItem(x, w)) != 0)
Guido van Rossumac7be682001-01-17 15:42:30 +00001715 format_exc_check_arg(PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001716 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001717 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001718
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001719 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001720 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001721 v = POP();
Raymond Hettinger21012b82003-02-26 18:11:50 +00001722 if (PyTuple_CheckExact(v)) {
Barry Warsawe42b18f1997-08-25 22:13:04 +00001723 if (PyTuple_Size(v) != oparg) {
1724 PyErr_SetString(PyExc_ValueError,
1725 "unpack tuple of wrong size");
1726 why = WHY_EXCEPTION;
1727 }
1728 else {
1729 for (; --oparg >= 0; ) {
1730 w = PyTuple_GET_ITEM(v, oparg);
1731 Py_INCREF(w);
1732 PUSH(w);
1733 }
1734 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001735 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00001736 else if (PyList_CheckExact(v)) {
Barry Warsawe42b18f1997-08-25 22:13:04 +00001737 if (PyList_Size(v) != oparg) {
1738 PyErr_SetString(PyExc_ValueError,
1739 "unpack list of wrong size");
1740 why = WHY_EXCEPTION;
1741 }
1742 else {
1743 for (; --oparg >= 0; ) {
1744 w = PyList_GET_ITEM(v, oparg);
1745 Py_INCREF(w);
1746 PUSH(w);
1747 }
1748 }
1749 }
Tim Petersd6d010b2001-06-21 02:49:55 +00001750 else if (unpack_iterable(v, oparg,
1751 stack_pointer + oparg))
1752 stack_pointer += oparg;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001753 else {
1754 if (PyErr_ExceptionMatches(PyExc_TypeError))
1755 PyErr_SetString(PyExc_TypeError,
1756 "unpack non-sequence");
Barry Warsawe42b18f1997-08-25 22:13:04 +00001757 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001758 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001759 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001760 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001761
Guido van Rossum374a9221991-04-04 10:40:29 +00001762 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001763 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001764 v = TOP();
1765 u = SECOND();
1766 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001767 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1768 Py_DECREF(v);
1769 Py_DECREF(u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001770 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001771
Guido van Rossum374a9221991-04-04 10:40:29 +00001772 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001773 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001774 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001775 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1776 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001777 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001778 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001779
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001780 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001781 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001782 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001783 err = PyDict_SetItem(f->f_globals, w, v);
1784 Py_DECREF(v);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001785 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001786
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001787 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001788 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001789 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001790 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001791 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001792 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001793
Guido van Rossum374a9221991-04-04 10:40:29 +00001794 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001795 w = GETITEM(names, oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001796 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001797 PyErr_Format(PyExc_SystemError,
1798 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001799 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001800 break;
1801 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001802 x = PyDict_GetItem(x, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001803 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001804 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001805 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001806 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001807 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001808 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001809 PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001810 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001811 break;
1812 }
1813 }
1814 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001815 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001816 PUSH(x);
1817 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001818
Guido van Rossum374a9221991-04-04 10:40:29 +00001819 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001820 w = GETITEM(names, oparg);
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001821 if (PyString_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00001822 /* Inline the PyDict_GetItem() calls.
1823 WARNING: this is an extreme speed hack.
1824 Do not try this at home. */
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001825 long hash = ((PyStringObject *)w)->ob_shash;
1826 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001827 PyDictObject *d;
1828 d = (PyDictObject *)(f->f_globals);
1829 x = d->ma_lookup(d, w, hash)->me_value;
1830 if (x != NULL) {
1831 Py_INCREF(x);
1832 PUSH(x);
1833 continue;
1834 }
1835 d = (PyDictObject *)(f->f_builtins);
1836 x = d->ma_lookup(d, w, hash)->me_value;
1837 if (x != NULL) {
1838 Py_INCREF(x);
1839 PUSH(x);
1840 continue;
1841 }
1842 goto load_global_error;
1843 }
1844 }
1845 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00001846 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001847 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001848 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001849 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001850 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00001851 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001852 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001853 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001854 break;
1855 }
1856 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001857 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001858 PUSH(x);
1859 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001860
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001861 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001862 x = GETLOCAL(oparg);
1863 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001864 format_exc_check_arg(
1865 PyExc_UnboundLocalError,
1866 UNBOUNDLOCAL_ERROR_MSG,
1867 PyTuple_GetItem(co->co_varnames, oparg)
1868 );
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001869 break;
1870 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00001871 SETLOCAL(oparg, NULL);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001872 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001873
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001874 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001875 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001876 Py_INCREF(x);
1877 PUSH(x);
1878 break;
1879
1880 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001881 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001882 w = PyCell_Get(x);
Jeremy Hylton2524d692001-02-05 17:23:16 +00001883 if (w == NULL) {
Jeremy Hylton76c81ee2002-07-11 16:56:38 +00001884 err = -1;
1885 /* Don't stomp existing exception */
1886 if (PyErr_Occurred())
1887 break;
Jeremy Hyltonc76770c2001-04-13 16:51:46 +00001888 if (oparg < f->f_ncells) {
Jeremy Hylton2524d692001-02-05 17:23:16 +00001889 v = PyTuple_GetItem(co->co_cellvars,
1890 oparg);
Jeremy Hyltonc76770c2001-04-13 16:51:46 +00001891 format_exc_check_arg(
1892 PyExc_UnboundLocalError,
1893 UNBOUNDLOCAL_ERROR_MSG,
1894 v);
1895 } else {
Jeremy Hylton2524d692001-02-05 17:23:16 +00001896 v = PyTuple_GetItem(
1897 co->co_freevars,
1898 oparg - f->f_ncells);
Jeremy Hyltonc76770c2001-04-13 16:51:46 +00001899 format_exc_check_arg(
1900 PyExc_NameError,
1901 UNBOUNDFREE_ERROR_MSG,
1902 v);
1903 }
Jeremy Hylton2524d692001-02-05 17:23:16 +00001904 break;
1905 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001906 PUSH(w);
1907 break;
1908
1909 case STORE_DEREF:
1910 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001911 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001912 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00001913 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001914 continue;
1915
Guido van Rossum374a9221991-04-04 10:40:29 +00001916 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001917 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001918 if (x != NULL) {
1919 for (; --oparg >= 0;) {
1920 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001921 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001922 }
1923 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001924 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001925 }
1926 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001927
Guido van Rossum374a9221991-04-04 10:40:29 +00001928 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00001929 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001930 if (x != NULL) {
1931 for (; --oparg >= 0;) {
1932 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00001933 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001934 }
1935 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001936 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001937 }
1938 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001939
Guido van Rossum374a9221991-04-04 10:40:29 +00001940 case BUILD_MAP:
Guido van Rossumb209a111997-04-29 18:18:01 +00001941 x = PyDict_New();
Guido van Rossum374a9221991-04-04 10:40:29 +00001942 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001943 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001944 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001945
Guido van Rossum374a9221991-04-04 10:40:29 +00001946 case LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001947 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001948 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001949 x = PyObject_GetAttr(v, w);
1950 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001951 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001952 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001953 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001954
Guido van Rossum374a9221991-04-04 10:40:29 +00001955 case COMPARE_OP:
1956 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001957 v = TOP();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001958 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001959 /* INLINE: cmp(int, int) */
1960 register long a, b;
1961 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001962 a = PyInt_AS_LONG(v);
1963 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001964 switch (oparg) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00001965 case PyCmp_LT: res = a < b; break;
1966 case PyCmp_LE: res = a <= b; break;
1967 case PyCmp_EQ: res = a == b; break;
1968 case PyCmp_NE: res = a != b; break;
1969 case PyCmp_GT: res = a > b; break;
1970 case PyCmp_GE: res = a >= b; break;
1971 case PyCmp_IS: res = v == w; break;
1972 case PyCmp_IS_NOT: res = v != w; break;
Guido van Rossumc12da691997-07-17 23:12:42 +00001973 default: goto slow_compare;
1974 }
1975 x = res ? Py_True : Py_False;
1976 Py_INCREF(x);
1977 }
1978 else {
1979 slow_compare:
1980 x = cmp_outcome(oparg, v, w);
1981 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001982 Py_DECREF(v);
1983 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001984 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001985 if (x == NULL) break;
1986 PREDICT(JUMP_IF_FALSE);
1987 PREDICT(JUMP_IF_TRUE);
1988 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001989
Guido van Rossum374a9221991-04-04 10:40:29 +00001990 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001991 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001992 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001993 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001994 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00001995 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001996 break;
1997 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001998 u = TOP();
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001999 w = PyTuple_Pack(4,
Guido van Rossum681d79a1995-07-18 14:51:37 +00002000 w,
2001 f->f_globals,
Guido van Rossuma027efa1997-05-05 20:56:21 +00002002 f->f_locals == NULL ?
2003 Py_None : f->f_locals,
Guido van Rossum681d79a1995-07-18 14:51:37 +00002004 u);
Guido van Rossumb209a111997-04-29 18:18:01 +00002005 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002006 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002007 u = POP();
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002008 x = NULL;
2009 break;
2010 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002011 x = PyEval_CallObject(x, w);
2012 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002013 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002014 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002015 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002016
Thomas Wouters52152252000-08-17 22:55:00 +00002017 case IMPORT_STAR:
2018 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002019 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002020 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002021 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00002022 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002023 break;
2024 }
Thomas Wouters52152252000-08-17 22:55:00 +00002025 err = import_all_from(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002026 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00002027 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002028 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002029 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002030
Thomas Wouters52152252000-08-17 22:55:00 +00002031 case IMPORT_FROM:
Skip Montanaro496e6582002-08-06 17:47:40 +00002032 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00002033 v = TOP();
2034 x = import_from(v, w);
2035 PUSH(x);
2036 if (x != NULL) continue;
2037 break;
2038
Guido van Rossum374a9221991-04-04 10:40:29 +00002039 case JUMP_FORWARD:
2040 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002041 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002042
Raymond Hettingerf606f872003-03-16 03:11:04 +00002043 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002044 case JUMP_IF_FALSE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002045 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002046 if (w == Py_True) {
2047 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002048 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002049 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002050 if (w == Py_False) {
2051 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002052 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002053 }
2054 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002055 if (err > 0)
2056 err = 0;
2057 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00002058 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002059 else
2060 break;
2061 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002062
Raymond Hettingerf606f872003-03-16 03:11:04 +00002063 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002064 case JUMP_IF_TRUE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002065 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002066 if (w == Py_False) {
2067 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002068 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002069 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002070 if (w == Py_True) {
2071 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002072 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002073 }
2074 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002075 if (err > 0) {
2076 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00002077 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002078 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002079 else if (err == 0)
2080 ;
2081 else
2082 break;
2083 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002084
Guido van Rossum374a9221991-04-04 10:40:29 +00002085 case JUMP_ABSOLUTE:
2086 JUMPTO(oparg);
Neil Schemenauerca2a2f12003-05-30 23:59:44 +00002087 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002088
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002089 case GET_ITER:
2090 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002091 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002092 x = PyObject_GetIter(v);
2093 Py_DECREF(v);
2094 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002095 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002096 PREDICT(FOR_ITER);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002097 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002098 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002099 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002100 break;
2101
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002102 PREDICTED_WITH_ARG(FOR_ITER);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002103 case FOR_ITER:
2104 /* before: [iter]; after: [iter, iter()] *or* [] */
2105 v = TOP();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002106 x = PyIter_Next(v);
2107 if (x != NULL) {
2108 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002109 PREDICT(STORE_FAST);
2110 PREDICT(UNPACK_SEQUENCE);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002111 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002112 }
Tim Petersf4848da2001-05-05 00:14:56 +00002113 if (!PyErr_Occurred()) {
2114 /* iterator ended normally */
2115 x = v = POP();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002116 Py_DECREF(v);
2117 JUMPBY(oparg);
2118 continue;
2119 }
2120 break;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002121
Guido van Rossum374a9221991-04-04 10:40:29 +00002122 case SETUP_LOOP:
2123 case SETUP_EXCEPT:
2124 case SETUP_FINALLY:
Guido van Rossumb209a111997-04-29 18:18:01 +00002125 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002126 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002127 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002128
Guido van Rossumf10570b1995-07-07 22:53:21 +00002129 case CALL_FUNCTION:
Jeremy Hylton985eba52003-02-05 23:13:00 +00002130 PCALL(PCALL_ALL);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002131 x = call_function(&stack_pointer, oparg);
2132 PUSH(x);
2133 if (x != NULL)
2134 continue;
2135 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002136
Jeremy Hylton76901512000-03-28 23:49:17 +00002137 case CALL_FUNCTION_VAR:
2138 case CALL_FUNCTION_KW:
2139 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002140 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002141 int na = oparg & 0xff;
2142 int nk = (oparg>>8) & 0xff;
2143 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002144 int n = na + 2 * nk;
2145 PyObject **pfunc, *func;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002146 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002147 if (flags & CALL_FLAG_VAR)
2148 n++;
2149 if (flags & CALL_FLAG_KW)
2150 n++;
2151 pfunc = stack_pointer - n - 1;
2152 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002153
Guido van Rossumac7be682001-01-17 15:42:30 +00002154 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002155 && PyMethod_GET_SELF(func) != NULL) {
2156 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002157 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002158 func = PyMethod_GET_FUNCTION(func);
2159 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002160 Py_DECREF(*pfunc);
2161 *pfunc = self;
2162 na++;
2163 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002164 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002165 Py_INCREF(func);
2166 x = ext_do_call(func, &stack_pointer, flags, na, nk);
Jeremy Hylton76901512000-03-28 23:49:17 +00002167 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002168
Jeremy Hylton76901512000-03-28 23:49:17 +00002169 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002170 w = POP();
2171 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002172 }
2173 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002174 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002175 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002176 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002177 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002178
Guido van Rossum681d79a1995-07-18 14:51:37 +00002179 case MAKE_FUNCTION:
2180 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002181 x = PyFunction_New(v, f->f_globals);
2182 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002183 /* XXX Maybe this should be a separate opcode? */
2184 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002185 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002186 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002187 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002188 x = NULL;
2189 break;
2190 }
2191 while (--oparg >= 0) {
2192 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002193 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002194 }
2195 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002196 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002197 }
2198 PUSH(x);
2199 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002200
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002201 case MAKE_CLOSURE:
2202 {
2203 int nfree;
2204 v = POP(); /* code object */
2205 x = PyFunction_New(v, f->f_globals);
Jeremy Hylton733c8932001-12-13 19:51:56 +00002206 nfree = PyCode_GetNumFree((PyCodeObject *)v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002207 Py_DECREF(v);
2208 /* XXX Maybe this should be a separate opcode? */
2209 if (x != NULL && nfree > 0) {
2210 v = PyTuple_New(nfree);
2211 if (v == NULL) {
2212 Py_DECREF(x);
2213 x = NULL;
2214 break;
2215 }
2216 while (--nfree >= 0) {
2217 w = POP();
2218 PyTuple_SET_ITEM(v, nfree, w);
2219 }
2220 err = PyFunction_SetClosure(x, v);
2221 Py_DECREF(v);
2222 }
2223 if (x != NULL && oparg > 0) {
2224 v = PyTuple_New(oparg);
2225 if (v == NULL) {
2226 Py_DECREF(x);
2227 x = NULL;
2228 break;
2229 }
2230 while (--oparg >= 0) {
2231 w = POP();
2232 PyTuple_SET_ITEM(v, oparg, w);
2233 }
2234 err = PyFunction_SetDefaults(x, v);
2235 Py_DECREF(v);
2236 }
2237 PUSH(x);
2238 break;
2239 }
2240
Guido van Rossum8861b741996-07-30 16:49:37 +00002241 case BUILD_SLICE:
2242 if (oparg == 3)
2243 w = POP();
2244 else
2245 w = NULL;
2246 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002247 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002248 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002249 Py_DECREF(u);
2250 Py_DECREF(v);
2251 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002252 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002253 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002254 break;
2255
Fred Drakeef8ace32000-08-24 00:32:09 +00002256 case EXTENDED_ARG:
2257 opcode = NEXTOP();
2258 oparg = oparg<<16 | NEXTARG();
2259 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002260
Guido van Rossum374a9221991-04-04 10:40:29 +00002261 default:
2262 fprintf(stderr,
2263 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002264 PyCode_Addr2Line(f->f_code, f->f_lasti),
2265 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002266 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002267 why = WHY_EXCEPTION;
2268 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002269
2270#ifdef CASE_TOO_BIG
2271 }
2272#endif
2273
Guido van Rossum374a9221991-04-04 10:40:29 +00002274 } /* switch */
2275
2276 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002277
Guido van Rossum374a9221991-04-04 10:40:29 +00002278 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002279
Guido van Rossum374a9221991-04-04 10:40:29 +00002280 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002281 if (err == 0 && x != NULL) {
2282#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002283 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002284 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002285 fprintf(stderr,
2286 "XXX undetected error\n");
2287 else
2288#endif
2289 continue; /* Normal, fast path */
2290 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002291 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002292 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002293 err = 0;
2294 }
2295
Guido van Rossum374a9221991-04-04 10:40:29 +00002296 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002297
Guido van Rossum374a9221991-04-04 10:40:29 +00002298 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002299 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002300 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002301 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002302 why = WHY_EXCEPTION;
2303 }
2304 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002305#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002306 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002307 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002308 if (PyErr_Occurred()) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002309 fprintf(stderr,
2310 "XXX undetected error (why=%d)\n",
2311 why);
2312 why = WHY_EXCEPTION;
2313 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002314 }
2315#endif
2316
2317 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002318
Guido van Rossum374a9221991-04-04 10:40:29 +00002319 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002320 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002321
Fred Drake8f51f542001-10-04 14:48:42 +00002322 if (tstate->c_tracefunc != NULL)
2323 call_exc_trace(tstate->c_tracefunc,
2324 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002325 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002326
Guido van Rossum374a9221991-04-04 10:40:29 +00002327 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002328
Guido van Rossum374a9221991-04-04 10:40:29 +00002329 if (why == WHY_RERAISE)
2330 why = WHY_EXCEPTION;
2331
2332 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002333
Tim Peters5ca576e2001-06-18 22:08:13 +00002334 while (why != WHY_NOT && why != WHY_YIELD && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002335 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002336
2337 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2338 /* For a continue inside a try block,
2339 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002340 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2341 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002342 why = WHY_NOT;
2343 JUMPTO(PyInt_AS_LONG(retval));
2344 Py_DECREF(retval);
2345 break;
2346 }
2347
Guido van Rossum374a9221991-04-04 10:40:29 +00002348 while (STACK_LEVEL() > b->b_level) {
2349 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002350 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002351 }
2352 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2353 why = WHY_NOT;
2354 JUMPTO(b->b_handler);
2355 break;
2356 }
2357 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002358 (b->b_type == SETUP_EXCEPT &&
2359 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002360 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002361 PyObject *exc, *val, *tb;
2362 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002363 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002364 val = Py_None;
2365 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002366 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002367 /* Make the raw exception data
2368 available to the handler,
2369 so a program can emulate the
2370 Python main loop. Don't do
2371 this for 'finally'. */
2372 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002373 PyErr_NormalizeException(
2374 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002375 set_exc_info(tstate,
2376 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002377 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002378 if (tb == NULL) {
2379 Py_INCREF(Py_None);
2380 PUSH(Py_None);
2381 } else
2382 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002383 PUSH(val);
2384 PUSH(exc);
2385 }
2386 else {
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002387 if (why == WHY_RETURN ||
Guido van Rossumc5fe5eb2002-06-12 03:45:21 +00002388 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00002389 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002390 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002391 PUSH(v);
2392 }
2393 why = WHY_NOT;
2394 JUMPTO(b->b_handler);
2395 break;
2396 }
2397 } /* unwind stack */
2398
2399 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002400
Guido van Rossum374a9221991-04-04 10:40:29 +00002401 if (why != WHY_NOT)
2402 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002403
Guido van Rossum374a9221991-04-04 10:40:29 +00002404 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002405
Guido van Rossum35974fb2001-12-06 21:28:18 +00002406 if (why != WHY_YIELD) {
2407 /* Pop remaining stack entries -- but when yielding */
2408 while (!EMPTY()) {
2409 v = POP();
2410 Py_XDECREF(v);
2411 }
2412 }
2413
Tim Peters5ca576e2001-06-18 22:08:13 +00002414 if (why != WHY_RETURN && why != WHY_YIELD)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002415 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002416
Fred Drake9e3ad782001-07-03 23:39:52 +00002417 if (tstate->use_tracing) {
2418 if (tstate->c_tracefunc
2419 && (why == WHY_RETURN || why == WHY_YIELD)) {
2420 if (call_trace(tstate->c_tracefunc,
2421 tstate->c_traceobj, f,
2422 PyTrace_RETURN, retval)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002423 Py_XDECREF(retval);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002424 retval = NULL;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002425 why = WHY_EXCEPTION;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002426 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002427 }
Fred Drake8f51f542001-10-04 14:48:42 +00002428 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002429 if (why == WHY_EXCEPTION)
2430 call_trace_protected(tstate->c_profilefunc,
2431 tstate->c_profileobj, f,
2432 PyTrace_RETURN);
2433 else if (call_trace(tstate->c_profilefunc,
2434 tstate->c_profileobj, f,
2435 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002436 Py_XDECREF(retval);
2437 retval = NULL;
2438 why = WHY_EXCEPTION;
2439 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002440 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002441 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002442
Guido van Rossuma027efa1997-05-05 20:56:21 +00002443 reset_exc_info(tstate);
2444
Tim Peters5ca576e2001-06-18 22:08:13 +00002445 /* pop frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +00002446 exit_eval_frame:
2447 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002448 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002449
Guido van Rossum96a42c81992-01-12 02:29:51 +00002450 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002451}
2452
Tim Peters6d6c1a32001-08-02 04:15:00 +00002453PyObject *
2454PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002455 PyObject **args, int argcount, PyObject **kws, int kwcount,
2456 PyObject **defs, int defcount, PyObject *closure)
2457{
2458 register PyFrameObject *f;
2459 register PyObject *retval = NULL;
2460 register PyObject **fastlocals, **freevars;
2461 PyThreadState *tstate = PyThreadState_GET();
2462 PyObject *x, *u;
2463
2464 if (globals == NULL) {
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002465 PyErr_SetString(PyExc_SystemError,
2466 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002467 return NULL;
2468 }
2469
Jeremy Hylton985eba52003-02-05 23:13:00 +00002470 assert(globals != NULL);
2471 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002472 if (f == NULL)
2473 return NULL;
2474
2475 fastlocals = f->f_localsplus;
2476 freevars = f->f_localsplus + f->f_nlocals;
2477
2478 if (co->co_argcount > 0 ||
2479 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2480 int i;
2481 int n = argcount;
2482 PyObject *kwdict = NULL;
2483 if (co->co_flags & CO_VARKEYWORDS) {
2484 kwdict = PyDict_New();
2485 if (kwdict == NULL)
2486 goto fail;
2487 i = co->co_argcount;
2488 if (co->co_flags & CO_VARARGS)
2489 i++;
2490 SETLOCAL(i, kwdict);
2491 }
2492 if (argcount > co->co_argcount) {
2493 if (!(co->co_flags & CO_VARARGS)) {
2494 PyErr_Format(PyExc_TypeError,
2495 "%.200s() takes %s %d "
2496 "%sargument%s (%d given)",
2497 PyString_AsString(co->co_name),
2498 defcount ? "at most" : "exactly",
2499 co->co_argcount,
2500 kwcount ? "non-keyword " : "",
2501 co->co_argcount == 1 ? "" : "s",
2502 argcount);
2503 goto fail;
2504 }
2505 n = co->co_argcount;
2506 }
2507 for (i = 0; i < n; i++) {
2508 x = args[i];
2509 Py_INCREF(x);
2510 SETLOCAL(i, x);
2511 }
2512 if (co->co_flags & CO_VARARGS) {
2513 u = PyTuple_New(argcount - n);
2514 if (u == NULL)
2515 goto fail;
2516 SETLOCAL(co->co_argcount, u);
2517 for (i = n; i < argcount; i++) {
2518 x = args[i];
2519 Py_INCREF(x);
2520 PyTuple_SET_ITEM(u, i-n, x);
2521 }
2522 }
2523 for (i = 0; i < kwcount; i++) {
2524 PyObject *keyword = kws[2*i];
2525 PyObject *value = kws[2*i + 1];
2526 int j;
2527 if (keyword == NULL || !PyString_Check(keyword)) {
2528 PyErr_Format(PyExc_TypeError,
2529 "%.200s() keywords must be strings",
2530 PyString_AsString(co->co_name));
2531 goto fail;
2532 }
2533 /* XXX slow -- speed up using dictionary? */
2534 for (j = 0; j < co->co_argcount; j++) {
2535 PyObject *nm = PyTuple_GET_ITEM(
2536 co->co_varnames, j);
2537 int cmp = PyObject_RichCompareBool(
2538 keyword, nm, Py_EQ);
2539 if (cmp > 0)
2540 break;
2541 else if (cmp < 0)
2542 goto fail;
2543 }
2544 /* Check errors from Compare */
2545 if (PyErr_Occurred())
2546 goto fail;
2547 if (j >= co->co_argcount) {
2548 if (kwdict == NULL) {
2549 PyErr_Format(PyExc_TypeError,
2550 "%.200s() got an unexpected "
2551 "keyword argument '%.400s'",
2552 PyString_AsString(co->co_name),
2553 PyString_AsString(keyword));
2554 goto fail;
2555 }
2556 PyDict_SetItem(kwdict, keyword, value);
2557 }
2558 else {
2559 if (GETLOCAL(j) != NULL) {
2560 PyErr_Format(PyExc_TypeError,
2561 "%.200s() got multiple "
2562 "values for keyword "
2563 "argument '%.400s'",
2564 PyString_AsString(co->co_name),
2565 PyString_AsString(keyword));
2566 goto fail;
2567 }
2568 Py_INCREF(value);
2569 SETLOCAL(j, value);
2570 }
2571 }
2572 if (argcount < co->co_argcount) {
2573 int m = co->co_argcount - defcount;
2574 for (i = argcount; i < m; i++) {
2575 if (GETLOCAL(i) == NULL) {
2576 PyErr_Format(PyExc_TypeError,
2577 "%.200s() takes %s %d "
2578 "%sargument%s (%d given)",
2579 PyString_AsString(co->co_name),
2580 ((co->co_flags & CO_VARARGS) ||
2581 defcount) ? "at least"
2582 : "exactly",
2583 m, kwcount ? "non-keyword " : "",
2584 m == 1 ? "" : "s", i);
2585 goto fail;
2586 }
2587 }
2588 if (n > m)
2589 i = n - m;
2590 else
2591 i = 0;
2592 for (; i < defcount; i++) {
2593 if (GETLOCAL(m+i) == NULL) {
2594 PyObject *def = defs[i];
2595 Py_INCREF(def);
2596 SETLOCAL(m+i, def);
2597 }
2598 }
2599 }
2600 }
2601 else {
2602 if (argcount > 0 || kwcount > 0) {
2603 PyErr_Format(PyExc_TypeError,
2604 "%.200s() takes no arguments (%d given)",
2605 PyString_AsString(co->co_name),
2606 argcount + kwcount);
2607 goto fail;
2608 }
2609 }
2610 /* Allocate and initialize storage for cell vars, and copy free
2611 vars into frame. This isn't too efficient right now. */
2612 if (f->f_ncells) {
2613 int i = 0, j = 0, nargs, found;
2614 char *cellname, *argname;
2615 PyObject *c;
2616
2617 nargs = co->co_argcount;
2618 if (co->co_flags & CO_VARARGS)
2619 nargs++;
2620 if (co->co_flags & CO_VARKEYWORDS)
2621 nargs++;
2622
2623 /* Check for cells that shadow args */
2624 for (i = 0; i < f->f_ncells && j < nargs; ++i) {
2625 cellname = PyString_AS_STRING(
2626 PyTuple_GET_ITEM(co->co_cellvars, i));
2627 found = 0;
2628 while (j < nargs) {
2629 argname = PyString_AS_STRING(
2630 PyTuple_GET_ITEM(co->co_varnames, j));
2631 if (strcmp(cellname, argname) == 0) {
2632 c = PyCell_New(GETLOCAL(j));
2633 if (c == NULL)
2634 goto fail;
2635 GETLOCAL(f->f_nlocals + i) = c;
2636 found = 1;
2637 break;
2638 }
2639 j++;
2640 }
2641 if (found == 0) {
2642 c = PyCell_New(NULL);
2643 if (c == NULL)
2644 goto fail;
2645 SETLOCAL(f->f_nlocals + i, c);
2646 }
2647 }
2648 /* Initialize any that are left */
2649 while (i < f->f_ncells) {
2650 c = PyCell_New(NULL);
2651 if (c == NULL)
2652 goto fail;
2653 SETLOCAL(f->f_nlocals + i, c);
2654 i++;
2655 }
2656 }
2657 if (f->f_nfreevars) {
2658 int i;
2659 for (i = 0; i < f->f_nfreevars; ++i) {
2660 PyObject *o = PyTuple_GET_ITEM(closure, i);
2661 Py_INCREF(o);
2662 freevars[f->f_ncells + i] = o;
2663 }
2664 }
2665
Tim Peters5ca576e2001-06-18 22:08:13 +00002666 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002667 /* Don't need to keep the reference to f_back, it will be set
2668 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002669 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002670 f->f_back = NULL;
2671
Jeremy Hylton985eba52003-02-05 23:13:00 +00002672 PCALL(PCALL_GENERATOR);
2673
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002674 /* Create a new generator that owns the ready to run frame
2675 * and return that as the value. */
Tim Peters5ca576e2001-06-18 22:08:13 +00002676 return gen_new(f);
2677 }
2678
2679 retval = eval_frame(f);
2680
2681 fail: /* Jump here from prelude on failure */
2682
Tim Petersb13680b2001-11-27 23:29:29 +00002683 /* decref'ing the frame can cause __del__ methods to get invoked,
2684 which can call back into Python. While we're done with the
2685 current Python frame (f), the associated C stack is still in use,
2686 so recursion_depth must be boosted for the duration.
2687 */
2688 assert(tstate != NULL);
2689 ++tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002690 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00002691 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002692 return retval;
2693}
2694
2695
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002696/* Implementation notes for set_exc_info() and reset_exc_info():
2697
2698- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2699 'exc_traceback'. These always travel together.
2700
2701- tstate->curexc_ZZZ is the "hot" exception that is set by
2702 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2703
2704- Once an exception is caught by an except clause, it is transferred
2705 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2706 can pick it up. This is the primary task of set_exc_info().
2707
2708- Now let me explain the complicated dance with frame->f_exc_ZZZ.
2709
2710 Long ago, when none of this existed, there were just a few globals:
2711 one set corresponding to the "hot" exception, and one set
2712 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2713 globals; they were simply stored as sys.exc_ZZZ. For backwards
2714 compatibility, they still are!) The problem was that in code like
2715 this:
2716
2717 try:
2718 "something that may fail"
2719 except "some exception":
2720 "do something else first"
2721 "print the exception from sys.exc_ZZZ."
2722
2723 if "do something else first" invoked something that raised and caught
2724 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2725 cause of subtle bugs. I fixed this by changing the semantics as
2726 follows:
2727
2728 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2729 *in that frame*.
2730
2731 - But initially, and as long as no exception is caught in a given
2732 frame, sys.exc_ZZZ will hold the last exception caught in the
2733 previous frame (or the frame before that, etc.).
2734
2735 The first bullet fixed the bug in the above example. The second
2736 bullet was for backwards compatibility: it was (and is) common to
2737 have a function that is called when an exception is caught, and to
2738 have that function access the caught exception via sys.exc_ZZZ.
2739 (Example: traceback.print_exc()).
2740
2741 At the same time I fixed the problem that sys.exc_ZZZ weren't
2742 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2743 but that's really a separate improvement.
2744
2745 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2746 variables to what they were before the current frame was called. The
2747 set_exc_info() function saves them on the frame so that
2748 reset_exc_info() can restore them. The invariant is that
2749 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2750 exception (where "catching" an exception applies only to successful
2751 except clauses); and if the current frame ever caught an exception,
2752 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2753 at the start of the current frame.
2754
2755*/
2756
Guido van Rossuma027efa1997-05-05 20:56:21 +00002757static void
Guido van Rossumac7be682001-01-17 15:42:30 +00002758set_exc_info(PyThreadState *tstate,
2759 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002760{
2761 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002762 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00002763
Guido van Rossuma027efa1997-05-05 20:56:21 +00002764 frame = tstate->frame;
2765 if (frame->f_exc_type == NULL) {
2766 /* This frame didn't catch an exception before */
2767 /* Save previous exception of this thread in this frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002768 if (tstate->exc_type == NULL) {
2769 Py_INCREF(Py_None);
2770 tstate->exc_type = Py_None;
2771 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002772 tmp_type = frame->f_exc_type;
2773 tmp_value = frame->f_exc_value;
2774 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002775 Py_XINCREF(tstate->exc_type);
2776 Py_XINCREF(tstate->exc_value);
2777 Py_XINCREF(tstate->exc_traceback);
2778 frame->f_exc_type = tstate->exc_type;
2779 frame->f_exc_value = tstate->exc_value;
2780 frame->f_exc_traceback = tstate->exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002781 Py_XDECREF(tmp_type);
2782 Py_XDECREF(tmp_value);
2783 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002784 }
2785 /* Set new exception for this thread */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002786 tmp_type = tstate->exc_type;
2787 tmp_value = tstate->exc_value;
2788 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002789 Py_XINCREF(type);
2790 Py_XINCREF(value);
2791 Py_XINCREF(tb);
2792 tstate->exc_type = type;
2793 tstate->exc_value = value;
2794 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002795 Py_XDECREF(tmp_type);
2796 Py_XDECREF(tmp_value);
2797 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002798 /* For b/w compatibility */
2799 PySys_SetObject("exc_type", type);
2800 PySys_SetObject("exc_value", value);
2801 PySys_SetObject("exc_traceback", tb);
2802}
2803
2804static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002805reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002806{
2807 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002808 PyObject *tmp_type, *tmp_value, *tmp_tb;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002809 frame = tstate->frame;
2810 if (frame->f_exc_type != NULL) {
2811 /* This frame caught an exception */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002812 tmp_type = tstate->exc_type;
2813 tmp_value = tstate->exc_value;
2814 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002815 Py_XINCREF(frame->f_exc_type);
2816 Py_XINCREF(frame->f_exc_value);
2817 Py_XINCREF(frame->f_exc_traceback);
2818 tstate->exc_type = frame->f_exc_type;
2819 tstate->exc_value = frame->f_exc_value;
2820 tstate->exc_traceback = frame->f_exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002821 Py_XDECREF(tmp_type);
2822 Py_XDECREF(tmp_value);
2823 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002824 /* For b/w compatibility */
2825 PySys_SetObject("exc_type", frame->f_exc_type);
2826 PySys_SetObject("exc_value", frame->f_exc_value);
2827 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
2828 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002829 tmp_type = frame->f_exc_type;
2830 tmp_value = frame->f_exc_value;
2831 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002832 frame->f_exc_type = NULL;
2833 frame->f_exc_value = NULL;
2834 frame->f_exc_traceback = NULL;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002835 Py_XDECREF(tmp_type);
2836 Py_XDECREF(tmp_value);
2837 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002838}
2839
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002840/* Logic for the raise statement (too complicated for inlining).
2841 This *consumes* a reference count to each of its arguments. */
Guido van Rossum1aa14831997-01-21 05:34:20 +00002842static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002843do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002844{
Guido van Rossumd295f121998-04-09 21:39:57 +00002845 if (type == NULL) {
2846 /* Reraise */
2847 PyThreadState *tstate = PyThreadState_Get();
2848 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
2849 value = tstate->exc_value;
2850 tb = tstate->exc_traceback;
2851 Py_XINCREF(type);
2852 Py_XINCREF(value);
2853 Py_XINCREF(tb);
2854 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002855
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002856 /* We support the following forms of raise:
2857 raise <class>, <classinstance>
2858 raise <class>, <argument tuple>
2859 raise <class>, None
2860 raise <class>, <argument>
2861 raise <classinstance>, None
2862 raise <string>, <object>
2863 raise <string>, None
2864
2865 An omitted second argument is the same as None.
2866
2867 In addition, raise <tuple>, <anything> is the same as
2868 raising the tuple's first item (and it better have one!);
2869 this rule is applied recursively.
2870
2871 Finally, an optional third argument can be supplied, which
2872 gives the traceback to be substituted (useful when
2873 re-raising an exception after examining it). */
2874
2875 /* First, check the traceback argument, replacing None with
2876 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002877 if (tb == Py_None) {
2878 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002879 tb = NULL;
2880 }
2881 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002882 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002883 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002884 goto raise_error;
2885 }
2886
2887 /* Next, replace a missing value with None */
2888 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002889 value = Py_None;
2890 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002891 }
2892
2893 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00002894 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
2895 PyObject *tmp = type;
2896 type = PyTuple_GET_ITEM(type, 0);
2897 Py_INCREF(type);
2898 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002899 }
2900
Tim Petersafb2c802002-04-18 18:06:20 +00002901 if (PyString_CheckExact(type))
2902 /* Raising builtin string is deprecated but still allowed --
2903 * do nothing. Raising an instance of a new-style str
2904 * subclass is right out. */
Neal Norwitz37aa0662003-01-10 15:31:15 +00002905 PyErr_Warn(PyExc_PendingDeprecationWarning,
2906 "raising a string exception is deprecated");
Barry Warsaw4249f541997-08-22 21:26:19 +00002907
2908 else if (PyClass_Check(type))
2909 PyErr_NormalizeException(&type, &value, &tb);
2910
Guido van Rossumb209a111997-04-29 18:18:01 +00002911 else if (PyInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002912 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002913 if (value != Py_None) {
2914 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002915 "instance exception may not have a separate value");
2916 goto raise_error;
2917 }
2918 else {
2919 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00002920 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002921 value = type;
Guido van Rossumb209a111997-04-29 18:18:01 +00002922 type = (PyObject*) ((PyInstanceObject*)type)->in_class;
2923 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002924 }
2925 }
2926 else {
2927 /* Not something you can raise. You get an exception
2928 anyway, just not what you specified :-) */
Jeremy Hylton960d9482001-04-27 02:25:33 +00002929 PyErr_Format(PyExc_TypeError,
Neal Norwitz37aa0662003-01-10 15:31:15 +00002930 "exceptions must be classes, instances, or "
2931 "strings (deprecated), not %s",
2932 type->ob_type->tp_name);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002933 goto raise_error;
2934 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002935 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002936 if (tb == NULL)
2937 return WHY_EXCEPTION;
2938 else
2939 return WHY_RERAISE;
2940 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00002941 Py_XDECREF(value);
2942 Py_XDECREF(type);
2943 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002944 return WHY_EXCEPTION;
2945}
2946
Tim Petersd6d010b2001-06-21 02:49:55 +00002947/* Iterate v argcnt times and store the results on the stack (via decreasing
2948 sp). Return 1 for success, 0 if error. */
2949
Barry Warsawe42b18f1997-08-25 22:13:04 +00002950static int
Tim Petersd6d010b2001-06-21 02:49:55 +00002951unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00002952{
Tim Petersd6d010b2001-06-21 02:49:55 +00002953 int i = 0;
2954 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00002955 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00002956
Tim Petersd6d010b2001-06-21 02:49:55 +00002957 assert(v != NULL);
2958
2959 it = PyObject_GetIter(v);
2960 if (it == NULL)
2961 goto Error;
2962
2963 for (; i < argcnt; i++) {
2964 w = PyIter_Next(it);
2965 if (w == NULL) {
2966 /* Iterator done, via error or exhaustion. */
2967 if (!PyErr_Occurred()) {
2968 PyErr_Format(PyExc_ValueError,
2969 "need more than %d value%s to unpack",
2970 i, i == 1 ? "" : "s");
2971 }
2972 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00002973 }
2974 *--sp = w;
2975 }
Tim Petersd6d010b2001-06-21 02:49:55 +00002976
2977 /* We better have exhausted the iterator now. */
2978 w = PyIter_Next(it);
2979 if (w == NULL) {
2980 if (PyErr_Occurred())
2981 goto Error;
2982 Py_DECREF(it);
2983 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00002984 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00002985 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00002986 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00002987 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00002988Error:
Barry Warsaw91010551997-08-25 22:30:51 +00002989 for (; i > 0; i--, sp++)
2990 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00002991 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00002992 return 0;
2993}
2994
2995
Guido van Rossum96a42c81992-01-12 02:29:51 +00002996#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00002997static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002998prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002999{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003000 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003001 if (PyObject_Print(v, stdout, 0) != 0)
3002 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003003 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003004 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003005}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003006#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003007
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003008static void
Fred Drake5755ce62001-06-27 19:19:46 +00003009call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003010{
Guido van Rossumb209a111997-04-29 18:18:01 +00003011 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003012 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003013 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003014 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003015 value = Py_None;
3016 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003017 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003018 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003019 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003020 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003021 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003022 }
Fred Drake5755ce62001-06-27 19:19:46 +00003023 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003024 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003025 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003026 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003027 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003028 Py_XDECREF(type);
3029 Py_XDECREF(value);
3030 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003031 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003032}
3033
Fred Drake4ec5d562001-10-04 19:26:43 +00003034static void
3035call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3036 int what)
3037{
3038 PyObject *type, *value, *traceback;
3039 int err;
3040 PyErr_Fetch(&type, &value, &traceback);
3041 err = call_trace(func, obj, frame, what, NULL);
3042 if (err == 0)
3043 PyErr_Restore(type, value, traceback);
3044 else {
3045 Py_XDECREF(type);
3046 Py_XDECREF(value);
3047 Py_XDECREF(traceback);
3048 }
3049}
3050
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003051static int
Fred Drake5755ce62001-06-27 19:19:46 +00003052call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3053 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003054{
Fred Drake5755ce62001-06-27 19:19:46 +00003055 register PyThreadState *tstate = frame->f_tstate;
3056 int result;
3057 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003058 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003059 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003060 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003061 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003062 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3063 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003064 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003065 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003066}
3067
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003068PyObject *
3069_PyEval_CallTracing(PyObject *func, PyObject *args)
3070{
3071 PyFrameObject *frame = PyEval_GetFrame();
3072 PyThreadState *tstate = frame->f_tstate;
3073 int save_tracing = tstate->tracing;
3074 int save_use_tracing = tstate->use_tracing;
3075 PyObject *result;
3076
3077 tstate->tracing = 0;
3078 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3079 || (tstate->c_profilefunc != NULL));
3080 result = PyObject_Call(func, args, NULL);
3081 tstate->tracing = save_tracing;
3082 tstate->use_tracing = save_use_tracing;
3083 return result;
3084}
3085
Michael W. Hudson006c7522002-11-08 13:08:46 +00003086static int
Michael W. Hudson019a78e2002-11-08 12:53:11 +00003087maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003088 PyFrameObject *frame, int *instr_lb, int *instr_ub)
3089{
3090 /* The theory of SET_LINENO-less tracing.
3091
3092 In a nutshell, we use the co_lnotab field of the code object
3093 to tell when execution has moved onto a different line.
3094
3095 As mentioned above, the basic idea is so set things up so
3096 that
3097
3098 *instr_lb <= frame->f_lasti < *instr_ub
3099
3100 is true so long as execution does not change lines.
3101
3102 This is all fairly simple. Digging the information out of
3103 co_lnotab takes some work, but is conceptually clear.
3104
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003105 Somewhat harder to explain is why we don't *always* call the
3106 line trace function when the above test fails.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003107
3108 Consider this code:
3109
3110 1: def f(a):
3111 2: if a:
3112 3: print 1
3113 4: else:
3114 5: print 2
3115
3116 which compiles to this:
3117
3118 2 0 LOAD_FAST 0 (a)
3119 3 JUMP_IF_FALSE 9 (to 15)
3120 6 POP_TOP
3121
3122 3 7 LOAD_CONST 1 (1)
3123 10 PRINT_ITEM
3124 11 PRINT_NEWLINE
3125 12 JUMP_FORWARD 6 (to 21)
3126 >> 15 POP_TOP
3127
3128 5 16 LOAD_CONST 2 (2)
3129 19 PRINT_ITEM
3130 20 PRINT_NEWLINE
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003131 >> 21 LOAD_CONST 0 (None)
3132 24 RETURN_VALUE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003133
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003134 If 'a' is false, execution will jump to instruction at offset
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003135 15 and the co_lnotab will claim that execution has moved to
3136 line 3. This is at best misleading. In this case we could
3137 associate the POP_TOP with line 4, but that doesn't make
3138 sense in all cases (I think).
3139
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003140 What we do is only call the line trace function if the co_lnotab
3141 indicates we have jumped to the *start* of a line, i.e. if the
3142 current instruction offset matches the offset given for the
3143 start of a line by the co_lnotab.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003144
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003145 This also takes care of the situation where 'a' is true.
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003146 Execution will jump from instruction offset 12 to offset 21.
3147 Then the co_lnotab would imply that execution has moved to line
3148 5, which is again misleading.
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003149
3150 Why do we set f_lineno when tracing? Well, consider the code
3151 above when 'a' is true. If stepping through this with 'n' in
3152 pdb, you would stop at line 1 with a "call" type event, then
3153 line events on lines 2 and 3, then a "return" type event -- but
3154 you would be shown line 5 during this event. This is a change
3155 from the behaviour in 2.2 and before, and I've found it
3156 confusing in practice. By setting and using f_lineno when
3157 tracing, one can report a line number different from that
3158 suggested by f_lasti on this one occasion where it's desirable.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003159 */
3160
Michael W. Hudson006c7522002-11-08 13:08:46 +00003161 int result = 0;
3162
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003163 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003164 PyCodeObject* co = frame->f_code;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003165 int size, addr, line;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003166 unsigned char* p;
3167
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003168 size = PyString_GET_SIZE(co->co_lnotab) / 2;
3169 p = (unsigned char*)PyString_AS_STRING(co->co_lnotab);
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003170
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003171 addr = 0;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003172 line = co->co_firstlineno;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003173
3174 /* possible optimization: if f->f_lasti == instr_ub
3175 (likely to be a common case) then we already know
3176 instr_lb -- if we stored the matching value of p
3177 somwhere we could skip the first while loop. */
3178
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003179 /* see comments in compile.c for the description of
3180 co_lnotab. A point to remember: increments to p
3181 should come in pairs -- although we don't care about
3182 the line increments here, treating them as byte
3183 increments gets confusing, to say the least. */
3184
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003185 while (size > 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003186 if (addr + *p > frame->f_lasti)
3187 break;
3188 addr += *p++;
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003189 if (*p) *instr_lb = addr;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003190 line += *p++;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003191 --size;
3192 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003193
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003194 if (addr == frame->f_lasti) {
3195 frame->f_lineno = line;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003196 result = call_trace(func, obj, frame,
3197 PyTrace_LINE, Py_None);
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003198 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003199
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003200 if (size > 0) {
3201 while (--size >= 0) {
3202 addr += *p++;
3203 if (*p++)
3204 break;
3205 }
3206 *instr_ub = addr;
3207 }
3208 else {
3209 *instr_ub = INT_MAX;
3210 }
3211 }
Michael W. Hudson006c7522002-11-08 13:08:46 +00003212
3213 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003214}
3215
Fred Drake5755ce62001-06-27 19:19:46 +00003216void
3217PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003218{
Fred Drake5755ce62001-06-27 19:19:46 +00003219 PyThreadState *tstate = PyThreadState_Get();
3220 PyObject *temp = tstate->c_profileobj;
3221 Py_XINCREF(arg);
3222 tstate->c_profilefunc = NULL;
3223 tstate->c_profileobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003224 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003225 Py_XDECREF(temp);
3226 tstate->c_profilefunc = func;
3227 tstate->c_profileobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003228 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003229}
3230
3231void
3232PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3233{
3234 PyThreadState *tstate = PyThreadState_Get();
3235 PyObject *temp = tstate->c_traceobj;
3236 Py_XINCREF(arg);
3237 tstate->c_tracefunc = NULL;
3238 tstate->c_traceobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003239 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003240 Py_XDECREF(temp);
3241 tstate->c_tracefunc = func;
3242 tstate->c_traceobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003243 tstate->use_tracing = ((func != NULL)
3244 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003245}
3246
Guido van Rossumb209a111997-04-29 18:18:01 +00003247PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003248PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003249{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003250 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003251 if (current_frame == NULL)
Michael W. Hudson019a78e2002-11-08 12:53:11 +00003252 return PyThreadState_Get()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003253 else
3254 return current_frame->f_builtins;
3255}
3256
Guido van Rossumb209a111997-04-29 18:18:01 +00003257PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003258PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003259{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003260 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003261 if (current_frame == NULL)
3262 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003263 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003264 return current_frame->f_locals;
3265}
3266
Guido van Rossumb209a111997-04-29 18:18:01 +00003267PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003268PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003269{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003270 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003271 if (current_frame == NULL)
3272 return NULL;
3273 else
3274 return current_frame->f_globals;
3275}
3276
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003277PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003278PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003279{
Michael W. Hudson019a78e2002-11-08 12:53:11 +00003280 PyThreadState *tstate = PyThreadState_Get();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003281 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003282}
3283
Guido van Rossum6135a871995-01-09 17:53:26 +00003284int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003285PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003286{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003287 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003288 return current_frame == NULL ? 0 : current_frame->f_restricted;
3289}
3290
Guido van Rossumbe270261997-05-22 22:26:18 +00003291int
Tim Peters5ba58662001-07-16 02:29:45 +00003292PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003293{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003294 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003295 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003296
3297 if (current_frame != NULL) {
3298 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003299 const int compilerflags = codeflags & PyCF_MASK;
3300 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003301 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003302 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003303 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003304#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003305 if (codeflags & CO_GENERATOR_ALLOWED) {
3306 result = 1;
3307 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3308 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003309#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003310 }
3311 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003312}
3313
3314int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003315Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003316{
Guido van Rossumb209a111997-04-29 18:18:01 +00003317 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003318 if (f == NULL)
3319 return 0;
3320 if (!PyFile_SoftSpace(f, 0))
3321 return 0;
3322 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003323}
3324
Guido van Rossum3f5da241990-12-20 15:06:42 +00003325
Guido van Rossum681d79a1995-07-18 14:51:37 +00003326/* External interface to call any callable object.
3327 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003328
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003329#undef PyEval_CallObject
3330/* for backward compatibility: export this interface */
3331
Guido van Rossumb209a111997-04-29 18:18:01 +00003332PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003333PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003334{
Guido van Rossumb209a111997-04-29 18:18:01 +00003335 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003336}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003337#define PyEval_CallObject(func,arg) \
3338 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003339
Guido van Rossumb209a111997-04-29 18:18:01 +00003340PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003341PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003342{
Jeremy Hylton52820442001-01-03 23:52:36 +00003343 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003344
3345 if (arg == NULL)
Guido van Rossumb209a111997-04-29 18:18:01 +00003346 arg = PyTuple_New(0);
3347 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003348 PyErr_SetString(PyExc_TypeError,
3349 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003350 return NULL;
3351 }
3352 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003353 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003354
Guido van Rossumb209a111997-04-29 18:18:01 +00003355 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003356 PyErr_SetString(PyExc_TypeError,
3357 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003358 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003359 return NULL;
3360 }
3361
Tim Peters6d6c1a32001-08-02 04:15:00 +00003362 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003363 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003364 return result;
3365}
3366
Tim Peters6d6c1a32001-08-02 04:15:00 +00003367char *
3368PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003369{
3370 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003371 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003372 else if (PyFunction_Check(func))
3373 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3374 else if (PyCFunction_Check(func))
3375 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3376 else if (PyClass_Check(func))
3377 return PyString_AsString(((PyClassObject*)func)->cl_name);
3378 else if (PyInstance_Check(func)) {
3379 return PyString_AsString(
3380 ((PyInstanceObject*)func)->in_class->cl_name);
3381 } else {
3382 return func->ob_type->tp_name;
3383 }
3384}
3385
Tim Peters6d6c1a32001-08-02 04:15:00 +00003386char *
3387PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003388{
3389 if (PyMethod_Check(func))
3390 return "()";
3391 else if (PyFunction_Check(func))
3392 return "()";
3393 else if (PyCFunction_Check(func))
3394 return "()";
3395 else if (PyClass_Check(func))
3396 return " constructor";
3397 else if (PyInstance_Check(func)) {
3398 return " instance";
3399 } else {
3400 return " object";
3401 }
3402}
3403
Jeremy Hylton52820442001-01-03 23:52:36 +00003404#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
3405
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003406static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003407err_args(PyObject *func, int flags, int nargs)
3408{
3409 if (flags & METH_NOARGS)
3410 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003411 "%.200s() takes no arguments (%d given)",
Jeremy Hylton192690e2002-08-16 18:36:11 +00003412 ((PyCFunctionObject *)func)->m_ml->ml_name,
3413 nargs);
3414 else
3415 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003416 "%.200s() takes exactly one argument (%d given)",
Jeremy Hylton192690e2002-08-16 18:36:11 +00003417 ((PyCFunctionObject *)func)->m_ml->ml_name,
3418 nargs);
3419}
3420
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003421static PyObject *
3422call_function(PyObject ***pp_stack, int oparg)
3423{
3424 int na = oparg & 0xff;
3425 int nk = (oparg>>8) & 0xff;
3426 int n = na + 2 * nk;
3427 PyObject **pfunc = (*pp_stack) - n - 1;
3428 PyObject *func = *pfunc;
3429 PyObject *x, *w;
3430
Jeremy Hylton985eba52003-02-05 23:13:00 +00003431 /* Always dispatch PyCFunction first, because these are
3432 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003433 */
3434 if (PyCFunction_Check(func) && nk == 0) {
3435 int flags = PyCFunction_GET_FLAGS(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003436 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003437 if (flags & (METH_NOARGS | METH_O)) {
3438 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3439 PyObject *self = PyCFunction_GET_SELF(func);
3440 if (flags & METH_NOARGS && na == 0)
3441 x = (*meth)(self, NULL);
3442 else if (flags & METH_O && na == 1) {
3443 PyObject *arg = EXT_POP(*pp_stack);
3444 x = (*meth)(self, arg);
3445 Py_DECREF(arg);
3446 }
3447 else {
3448 err_args(func, flags, na);
3449 x = NULL;
3450 }
3451 }
3452 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003453 PyObject *callargs;
3454 callargs = load_args(pp_stack, na);
3455 x = PyCFunction_Call(func, callargs, NULL);
3456 Py_XDECREF(callargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003457 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003458 } else {
3459 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3460 /* optimize access to bound methods */
3461 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003462 PCALL(PCALL_METHOD);
3463 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003464 Py_INCREF(self);
3465 func = PyMethod_GET_FUNCTION(func);
3466 Py_INCREF(func);
3467 Py_DECREF(*pfunc);
3468 *pfunc = self;
3469 na++;
3470 n++;
3471 } else
3472 Py_INCREF(func);
3473 if (PyFunction_Check(func))
3474 x = fast_function(func, pp_stack, n, na, nk);
3475 else
3476 x = do_call(func, pp_stack, na, nk);
3477 Py_DECREF(func);
3478 }
3479
Jeremy Hylton985eba52003-02-05 23:13:00 +00003480 /* What does this do? */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003481 while ((*pp_stack) > pfunc) {
3482 w = EXT_POP(*pp_stack);
3483 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003484 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003485 }
3486 return x;
3487}
3488
Jeremy Hylton192690e2002-08-16 18:36:11 +00003489/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003490 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003491 For the simplest case -- a function that takes only positional
3492 arguments and is called with only positional arguments -- it
3493 inlines the most primitive frame setup code from
3494 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3495 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003496*/
3497
3498static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003499fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003500{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003501 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003502 PyObject *globals = PyFunction_GET_GLOBALS(func);
3503 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3504 PyObject **d = NULL;
3505 int nd = 0;
3506
Jeremy Hylton985eba52003-02-05 23:13:00 +00003507 PCALL(PCALL_FUNCTION);
3508 PCALL(PCALL_FAST_FUNCTION);
Raymond Hettinger40174c32003-05-31 07:04:16 +00003509 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003510 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3511 PyFrameObject *f;
3512 PyObject *retval = NULL;
3513 PyThreadState *tstate = PyThreadState_GET();
3514 PyObject **fastlocals, **stack;
3515 int i;
3516
3517 PCALL(PCALL_FASTER_FUNCTION);
3518 assert(globals != NULL);
3519 /* XXX Perhaps we should create a specialized
3520 PyFrame_New() that doesn't take locals, but does
3521 take builtins without sanity checking them.
3522 */
3523 f = PyFrame_New(tstate, co, globals, NULL);
3524 if (f == NULL)
3525 return NULL;
3526
3527 fastlocals = f->f_localsplus;
3528 stack = (*pp_stack) - n;
3529
3530 for (i = 0; i < n; i++) {
3531 Py_INCREF(*stack);
3532 fastlocals[i] = *stack++;
3533 }
3534 retval = eval_frame(f);
3535 assert(tstate != NULL);
3536 ++tstate->recursion_depth;
3537 Py_DECREF(f);
3538 --tstate->recursion_depth;
3539 return retval;
3540 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003541 if (argdefs != NULL) {
3542 d = &PyTuple_GET_ITEM(argdefs, 0);
3543 nd = ((PyTupleObject *)argdefs)->ob_size;
3544 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003545 return PyEval_EvalCodeEx(co, globals,
3546 (PyObject *)NULL, (*pp_stack)-n, na,
3547 (*pp_stack)-2*nk, nk, d, nd,
3548 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003549}
3550
3551static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003552update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3553 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003554{
3555 PyObject *kwdict = NULL;
3556 if (orig_kwdict == NULL)
3557 kwdict = PyDict_New();
3558 else {
3559 kwdict = PyDict_Copy(orig_kwdict);
3560 Py_DECREF(orig_kwdict);
3561 }
3562 if (kwdict == NULL)
3563 return NULL;
3564 while (--nk >= 0) {
3565 int err;
3566 PyObject *value = EXT_POP(*pp_stack);
3567 PyObject *key = EXT_POP(*pp_stack);
3568 if (PyDict_GetItem(kwdict, key) != NULL) {
Guido van Rossumac7be682001-01-17 15:42:30 +00003569 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +00003570 "%.200s%s got multiple values "
Jeremy Hylton512a2372001-04-11 13:52:29 +00003571 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003572 PyEval_GetFuncName(func),
3573 PyEval_GetFuncDesc(func),
Jeremy Hylton512a2372001-04-11 13:52:29 +00003574 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003575 Py_DECREF(key);
3576 Py_DECREF(value);
3577 Py_DECREF(kwdict);
3578 return NULL;
3579 }
3580 err = PyDict_SetItem(kwdict, key, value);
3581 Py_DECREF(key);
3582 Py_DECREF(value);
3583 if (err) {
3584 Py_DECREF(kwdict);
3585 return NULL;
3586 }
3587 }
3588 return kwdict;
3589}
3590
3591static PyObject *
3592update_star_args(int nstack, int nstar, PyObject *stararg,
3593 PyObject ***pp_stack)
3594{
3595 PyObject *callargs, *w;
3596
3597 callargs = PyTuple_New(nstack + nstar);
3598 if (callargs == NULL) {
3599 return NULL;
3600 }
3601 if (nstar) {
3602 int i;
3603 for (i = 0; i < nstar; i++) {
3604 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3605 Py_INCREF(a);
3606 PyTuple_SET_ITEM(callargs, nstack + i, a);
3607 }
3608 }
3609 while (--nstack >= 0) {
3610 w = EXT_POP(*pp_stack);
3611 PyTuple_SET_ITEM(callargs, nstack, w);
3612 }
3613 return callargs;
3614}
3615
3616static PyObject *
3617load_args(PyObject ***pp_stack, int na)
3618{
3619 PyObject *args = PyTuple_New(na);
3620 PyObject *w;
3621
3622 if (args == NULL)
3623 return NULL;
3624 while (--na >= 0) {
3625 w = EXT_POP(*pp_stack);
3626 PyTuple_SET_ITEM(args, na, w);
3627 }
3628 return args;
3629}
3630
3631static PyObject *
3632do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3633{
3634 PyObject *callargs = NULL;
3635 PyObject *kwdict = NULL;
3636 PyObject *result = NULL;
3637
3638 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003639 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003640 if (kwdict == NULL)
3641 goto call_fail;
3642 }
3643 callargs = load_args(pp_stack, na);
3644 if (callargs == NULL)
3645 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003646#ifdef CALL_PROFILE
3647 /* At this point, we have to look at the type of func to
3648 update the call stats properly. Do it here so as to avoid
3649 exposing the call stats machinery outside ceval.c
3650 */
3651 if (PyFunction_Check(func))
3652 PCALL(PCALL_FUNCTION);
3653 else if (PyMethod_Check(func))
3654 PCALL(PCALL_METHOD);
3655 else if (PyType_Check(func))
3656 PCALL(PCALL_TYPE);
3657 else
3658 PCALL(PCALL_OTHER);
3659#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003660 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003661 call_fail:
3662 Py_XDECREF(callargs);
3663 Py_XDECREF(kwdict);
3664 return result;
3665}
3666
3667static PyObject *
3668ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3669{
3670 int nstar = 0;
3671 PyObject *callargs = NULL;
3672 PyObject *stararg = NULL;
3673 PyObject *kwdict = NULL;
3674 PyObject *result = NULL;
3675
3676 if (flags & CALL_FLAG_KW) {
3677 kwdict = EXT_POP(*pp_stack);
3678 if (!(kwdict && PyDict_Check(kwdict))) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003679 PyErr_Format(PyExc_TypeError,
Jeremy Hylton512a2372001-04-11 13:52:29 +00003680 "%s%s argument after ** "
3681 "must be a dictionary",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003682 PyEval_GetFuncName(func),
3683 PyEval_GetFuncDesc(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003684 goto ext_call_fail;
3685 }
3686 }
3687 if (flags & CALL_FLAG_VAR) {
3688 stararg = EXT_POP(*pp_stack);
3689 if (!PyTuple_Check(stararg)) {
3690 PyObject *t = NULL;
3691 t = PySequence_Tuple(stararg);
3692 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003693 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3694 PyErr_Format(PyExc_TypeError,
3695 "%s%s argument after * "
3696 "must be a sequence",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003697 PyEval_GetFuncName(func),
3698 PyEval_GetFuncDesc(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003699 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003700 goto ext_call_fail;
3701 }
3702 Py_DECREF(stararg);
3703 stararg = t;
3704 }
3705 nstar = PyTuple_GET_SIZE(stararg);
3706 }
3707 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003708 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003709 if (kwdict == NULL)
3710 goto ext_call_fail;
3711 }
3712 callargs = update_star_args(na, nstar, stararg, pp_stack);
3713 if (callargs == NULL)
3714 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003715#ifdef CALL_PROFILE
3716 /* At this point, we have to look at the type of func to
3717 update the call stats properly. Do it here so as to avoid
3718 exposing the call stats machinery outside ceval.c
3719 */
3720 if (PyFunction_Check(func))
3721 PCALL(PCALL_FUNCTION);
3722 else if (PyMethod_Check(func))
3723 PCALL(PCALL_METHOD);
3724 else if (PyType_Check(func))
3725 PCALL(PCALL_TYPE);
3726 else
3727 PCALL(PCALL_OTHER);
3728#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003729 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003730 ext_call_fail:
3731 Py_XDECREF(callargs);
3732 Py_XDECREF(kwdict);
3733 Py_XDECREF(stararg);
3734 return result;
3735}
3736
Guido van Rossum3b9c6671996-07-30 18:40:29 +00003737#define SLICE_ERROR_MSG \
3738 "standard sequence type does not support step size other than one"
3739
Tim Peterscb479e72001-12-16 19:11:44 +00003740/* Extract a slice index from a PyInt or PyLong, and store in *pi.
3741 Silently reduce values larger than INT_MAX to INT_MAX, and silently
3742 boost values less than -INT_MAX to 0. Return 0 on error, 1 on success.
3743*/
Tim Petersb5196382001-12-16 19:44:20 +00003744/* Note: If v is NULL, return success without storing into *pi. This
3745 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3746 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00003747*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00003748int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003749_PyEval_SliceIndex(PyObject *v, int *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003750{
Tim Petersb5196382001-12-16 19:44:20 +00003751 if (v != NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003752 long x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003753 if (PyInt_Check(v)) {
3754 x = PyInt_AsLong(v);
3755 } else if (PyLong_Check(v)) {
3756 x = PyLong_AsLong(v);
3757 if (x==-1 && PyErr_Occurred()) {
3758 PyObject *long_zero;
Guido van Rossumac7be682001-01-17 15:42:30 +00003759 int cmp;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003760
Guido van Rossumac7be682001-01-17 15:42:30 +00003761 if (!PyErr_ExceptionMatches(
3762 PyExc_OverflowError)) {
3763 /* It's not an overflow error, so just
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003764 signal an error */
Guido van Rossum20c6add2000-05-08 14:06:50 +00003765 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003766 }
3767
Guido van Rossumac7be682001-01-17 15:42:30 +00003768 /* Clear the OverflowError */
3769 PyErr_Clear();
3770
3771 /* It's an overflow error, so we need to
3772 check the sign of the long integer,
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003773 set the value to INT_MAX or -INT_MAX,
3774 and clear the error. */
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003775
3776 /* Create a long integer with a value of 0 */
Guido van Rossumac7be682001-01-17 15:42:30 +00003777 long_zero = PyLong_FromLong(0L);
Tim Peterscb479e72001-12-16 19:11:44 +00003778 if (long_zero == NULL)
3779 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003780
3781 /* Check sign */
Guido van Rossumac7be682001-01-17 15:42:30 +00003782 cmp = PyObject_RichCompareBool(v, long_zero,
3783 Py_GT);
3784 Py_DECREF(long_zero);
3785 if (cmp < 0)
3786 return 0;
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003787 else if (cmp)
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003788 x = INT_MAX;
3789 else
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003790 x = -INT_MAX;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003791 }
3792 } else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003793 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003794 "slice indices must be integers");
Guido van Rossum20c6add2000-05-08 14:06:50 +00003795 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003796 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003797 /* Truncate -- very long indices are truncated anyway */
3798 if (x > INT_MAX)
3799 x = INT_MAX;
3800 else if (x < -INT_MAX)
Michael W. Hudsoncbd6fb92002-11-06 15:17:32 +00003801 x = -INT_MAX;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003802 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003803 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00003804 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003805}
3806
Guido van Rossum50d756e2001-08-18 17:43:36 +00003807#undef ISINT
3808#define ISINT(x) ((x) == NULL || PyInt_Check(x) || PyLong_Check(x))
3809
Guido van Rossumb209a111997-04-29 18:18:01 +00003810static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003811apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003812{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003813 PyTypeObject *tp = u->ob_type;
3814 PySequenceMethods *sq = tp->tp_as_sequence;
3815
3816 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3817 int ilow = 0, ihigh = INT_MAX;
3818 if (!_PyEval_SliceIndex(v, &ilow))
3819 return NULL;
3820 if (!_PyEval_SliceIndex(w, &ihigh))
3821 return NULL;
3822 return PySequence_GetSlice(u, ilow, ihigh);
3823 }
3824 else {
3825 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00003826 if (slice != NULL) {
3827 PyObject *res = PyObject_GetItem(u, slice);
3828 Py_DECREF(slice);
3829 return res;
3830 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00003831 else
3832 return NULL;
3833 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003834}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003835
3836static int
Guido van Rossumac7be682001-01-17 15:42:30 +00003837assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
3838 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003839{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003840 PyTypeObject *tp = u->ob_type;
3841 PySequenceMethods *sq = tp->tp_as_sequence;
3842
3843 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3844 int ilow = 0, ihigh = INT_MAX;
3845 if (!_PyEval_SliceIndex(v, &ilow))
3846 return -1;
3847 if (!_PyEval_SliceIndex(w, &ihigh))
3848 return -1;
3849 if (x == NULL)
3850 return PySequence_DelSlice(u, ilow, ihigh);
3851 else
3852 return PySequence_SetSlice(u, ilow, ihigh, x);
3853 }
3854 else {
3855 PyObject *slice = PySlice_New(v, w, NULL);
3856 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00003857 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003858 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00003859 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00003860 else
Guido van Rossum354797c2001-12-03 19:45:06 +00003861 res = PyObject_DelItem(u, slice);
3862 Py_DECREF(slice);
3863 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003864 }
3865 else
3866 return -1;
3867 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003868}
3869
Guido van Rossumb209a111997-04-29 18:18:01 +00003870static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003871cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003872{
Guido van Rossumac7be682001-01-17 15:42:30 +00003873 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003874 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00003875 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003876 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003877 break;
3878 case PyCmp_IS_NOT:
3879 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003880 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003881 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003882 res = PySequence_Contains(w, v);
3883 if (res < 0)
3884 return NULL;
3885 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003886 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00003887 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003888 if (res < 0)
3889 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003890 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003891 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003892 case PyCmp_EXC_MATCH:
Barry Warsaw4249f541997-08-22 21:26:19 +00003893 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003894 break;
3895 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00003896 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003897 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003898 v = res ? Py_True : Py_False;
3899 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003900 return v;
3901}
3902
Thomas Wouters52152252000-08-17 22:55:00 +00003903static PyObject *
3904import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003905{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003906 PyObject *x;
3907
3908 x = PyObject_GetAttr(v, name);
3909 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00003910 PyErr_Format(PyExc_ImportError,
3911 "cannot import name %.230s",
3912 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003913 }
Thomas Wouters52152252000-08-17 22:55:00 +00003914 return x;
3915}
Guido van Rossumac7be682001-01-17 15:42:30 +00003916
Thomas Wouters52152252000-08-17 22:55:00 +00003917static int
3918import_all_from(PyObject *locals, PyObject *v)
3919{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003920 PyObject *all = PyObject_GetAttrString(v, "__all__");
3921 PyObject *dict, *name, *value;
3922 int skip_leading_underscores = 0;
3923 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00003924
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003925 if (all == NULL) {
3926 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3927 return -1; /* Unexpected error */
3928 PyErr_Clear();
3929 dict = PyObject_GetAttrString(v, "__dict__");
3930 if (dict == NULL) {
3931 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3932 return -1;
3933 PyErr_SetString(PyExc_ImportError,
3934 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00003935 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003936 }
3937 all = PyMapping_Keys(dict);
3938 Py_DECREF(dict);
3939 if (all == NULL)
3940 return -1;
3941 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00003942 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003943
3944 for (pos = 0, err = 0; ; pos++) {
3945 name = PySequence_GetItem(all, pos);
3946 if (name == NULL) {
3947 if (!PyErr_ExceptionMatches(PyExc_IndexError))
3948 err = -1;
3949 else
3950 PyErr_Clear();
3951 break;
3952 }
3953 if (skip_leading_underscores &&
3954 PyString_Check(name) &&
3955 PyString_AS_STRING(name)[0] == '_')
3956 {
3957 Py_DECREF(name);
3958 continue;
3959 }
3960 value = PyObject_GetAttr(v, name);
3961 if (value == NULL)
3962 err = -1;
3963 else
3964 err = PyDict_SetItem(locals, name, value);
3965 Py_DECREF(name);
3966 Py_XDECREF(value);
3967 if (err != 0)
3968 break;
3969 }
3970 Py_DECREF(all);
3971 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00003972}
3973
Guido van Rossumb209a111997-04-29 18:18:01 +00003974static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003975build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003976{
Guido van Rossum7851eea2001-09-12 19:19:18 +00003977 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003978
3979 if (PyDict_Check(methods))
3980 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00003981 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00003982 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00003983 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
3984 base = PyTuple_GET_ITEM(bases, 0);
3985 metaclass = PyObject_GetAttrString(base, "__class__");
3986 if (metaclass == NULL) {
3987 PyErr_Clear();
3988 metaclass = (PyObject *)base->ob_type;
3989 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00003990 }
3991 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00003992 else {
3993 PyObject *g = PyEval_GetGlobals();
3994 if (g != NULL && PyDict_Check(g))
3995 metaclass = PyDict_GetItemString(g, "__metaclass__");
3996 if (metaclass == NULL)
3997 metaclass = (PyObject *) &PyClass_Type;
3998 Py_INCREF(metaclass);
3999 }
4000 result = PyObject_CallFunction(metaclass, "OOO", name, bases, methods);
4001 Py_DECREF(metaclass);
4002 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00004003}
4004
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004005static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004006exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
4007 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004008{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004009 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00004010 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004011 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004012
Guido van Rossumb209a111997-04-29 18:18:01 +00004013 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
4014 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004015 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00004016 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004017 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00004018 locals = PyTuple_GetItem(prog, 2);
4019 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004020 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004021 if (globals == Py_None) {
4022 globals = PyEval_GetGlobals();
4023 if (locals == Py_None) {
4024 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00004025 plain = 1;
4026 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004027 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004028 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004029 locals = globals;
Guido van Rossumb209a111997-04-29 18:18:01 +00004030 if (!PyString_Check(prog) &&
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004031 !PyUnicode_Check(prog) &&
Guido van Rossumb209a111997-04-29 18:18:01 +00004032 !PyCode_Check(prog) &&
4033 !PyFile_Check(prog)) {
4034 PyErr_SetString(PyExc_TypeError,
Guido van Rossumac7be682001-01-17 15:42:30 +00004035 "exec: arg 1 must be a string, file, or code object");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004036 return -1;
4037 }
Fred Drake661ea262000-10-24 19:57:45 +00004038 if (!PyDict_Check(globals)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004039 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00004040 "exec: arg 2 must be a dictionary or None");
4041 return -1;
4042 }
4043 if (!PyDict_Check(locals)) {
4044 PyErr_SetString(PyExc_TypeError,
4045 "exec: arg 3 must be a dictionary or None");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004046 return -1;
4047 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004048 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00004049 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00004050 if (PyCode_Check(prog)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +00004051 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4052 PyErr_SetString(PyExc_TypeError,
4053 "code object passed to exec may not contain free variables");
4054 return -1;
4055 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004056 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004057 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004058 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004059 FILE *fp = PyFile_AsFile(prog);
4060 char *name = PyString_AsString(PyFile_Name(prog));
Tim Peters5ba58662001-07-16 02:29:45 +00004061 PyCompilerFlags cf;
4062 cf.cf_flags = 0;
4063 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004064 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
4065 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004066 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004067 v = PyRun_File(fp, name, Py_file_input, globals,
4068 locals);
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004069 }
4070 else {
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004071 PyObject *tmp = NULL;
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004072 char *str;
Tim Peters5ba58662001-07-16 02:29:45 +00004073 PyCompilerFlags cf;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004074 cf.cf_flags = 0;
4075#ifdef Py_USING_UNICODE
4076 if (PyUnicode_Check(prog)) {
4077 tmp = PyUnicode_AsUTF8String(prog);
4078 if (tmp == NULL)
4079 return -1;
4080 prog = tmp;
4081 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4082 }
4083#endif
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004084 if (PyString_AsStringAndSize(prog, &str, NULL))
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004085 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004086 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004087 v = PyRun_StringFlags(str, Py_file_input, globals,
4088 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004089 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004090 v = PyRun_String(str, Py_file_input, globals, locals);
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004091 Py_XDECREF(tmp);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004092 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004093 if (plain)
4094 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004095 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004096 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00004097 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004098 return 0;
4099}
Guido van Rossum24c13741995-02-14 09:42:43 +00004100
Guido van Rossumac7be682001-01-17 15:42:30 +00004101static void
Paul Prescode68140d2000-08-30 20:25:01 +00004102format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4103{
4104 char *obj_str;
4105
4106 if (!obj)
4107 return;
4108
4109 obj_str = PyString_AsString(obj);
4110 if (!obj_str)
4111 return;
4112
4113 PyErr_Format(exc, format_str, obj_str);
4114}
Guido van Rossum950361c1997-01-24 13:49:28 +00004115
4116#ifdef DYNAMIC_EXECUTION_PROFILE
4117
Skip Montanarof118cb12001-10-15 20:51:38 +00004118static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004119getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004120{
4121 int i;
4122 PyObject *l = PyList_New(256);
4123 if (l == NULL) return NULL;
4124 for (i = 0; i < 256; i++) {
4125 PyObject *x = PyInt_FromLong(a[i]);
4126 if (x == NULL) {
4127 Py_DECREF(l);
4128 return NULL;
4129 }
4130 PyList_SetItem(l, i, x);
4131 }
4132 for (i = 0; i < 256; i++)
4133 a[i] = 0;
4134 return l;
4135}
4136
4137PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004138_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004139{
4140#ifndef DXPAIRS
4141 return getarray(dxp);
4142#else
4143 int i;
4144 PyObject *l = PyList_New(257);
4145 if (l == NULL) return NULL;
4146 for (i = 0; i < 257; i++) {
4147 PyObject *x = getarray(dxpairs[i]);
4148 if (x == NULL) {
4149 Py_DECREF(l);
4150 return NULL;
4151 }
4152 PyList_SetItem(l, i, x);
4153 }
4154 return l;
4155#endif
4156}
4157
4158#endif