blob: 035520a593b5b14a6a9872ff2946cbb2a5276b60 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003
Guido van Rossum681d79a1995-07-18 14:51:37 +00004/* XXX TO DO:
Guido van Rossum681d79a1995-07-18 14:51:37 +00005 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +00006 XXX document it!
7 */
8
Guido van Rossumb209a111997-04-29 18:18:01 +00009#include "Python.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000010
Guido van Rossum10dc2e81990-11-18 17:27:39 +000011#include "compile.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000012#include "frameobject.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000013#include "eval.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000014#include "opcode.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000015#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000016
Jack Jansencbf630f2000-07-11 21:59:16 +000017#ifdef macintosh
18#include "macglue.h"
19#endif
20
Guido van Rossumc6004111993-11-05 10:22:19 +000021#include <ctype.h>
22
Guido van Rossum04691fc1992-08-12 15:35:34 +000023/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000024/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000025
Guido van Rossum408027e1996-12-30 16:17:54 +000026#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000027/* For debugging the interpreter: */
28#define LLTRACE 1 /* Low-level trace feature */
29#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000030#endif
31
Jeremy Hylton52820442001-01-03 23:52:36 +000032typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000033
Guido van Rossum374a9221991-04-04 10:40:29 +000034/* Forward declarations */
Tim Peters5ca576e2001-06-18 22:08:13 +000035static PyObject *eval_frame(PyFrameObject *);
Jeremy Hyltone8c04322002-08-16 17:47:26 +000036static PyObject *call_function(PyObject ***, int);
Jeremy Hylton52820442001-01-03 23:52:36 +000037static PyObject *fast_function(PyObject *, PyObject ***, int, int, int);
Jeremy Hylton52820442001-01-03 23:52:36 +000038static PyObject *do_call(PyObject *, PyObject ***, int, int);
39static PyObject *ext_do_call(PyObject *, PyObject ***, int, int, int);
Guido van Rossumac7be682001-01-17 15:42:30 +000040static PyObject *update_keyword_args(PyObject *, int, PyObject ***,PyObject *);
Ka-Ping Yee20579702001-01-15 22:14:16 +000041static PyObject *update_star_args(int, int, PyObject *, PyObject ***);
Jeremy Hylton52820442001-01-03 23:52:36 +000042static PyObject *load_args(PyObject ***, int);
43#define CALL_FLAG_VAR 1
44#define CALL_FLAG_KW 2
45
Guido van Rossum0a066c01992-03-27 17:29:15 +000046#ifdef LLTRACE
Tim Petersdbd9ba62000-07-09 03:09:57 +000047static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000048#endif
Fred Drake5755ce62001-06-27 19:19:46 +000049static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
50 int, PyObject *);
Fred Drake4ec5d562001-10-04 19:26:43 +000051static void call_trace_protected(Py_tracefunc, PyObject *,
52 PyFrameObject *, int);
Fred Drake5755ce62001-06-27 19:19:46 +000053static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
Michael W. Hudson006c7522002-11-08 13:08:46 +000054static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Michael W. Hudsondd32a912002-08-15 14:59:02 +000055 PyFrameObject *, int *, int *);
56
Tim Petersdbd9ba62000-07-09 03:09:57 +000057static PyObject *apply_slice(PyObject *, PyObject *, PyObject *);
58static int assign_slice(PyObject *, PyObject *,
59 PyObject *, PyObject *);
60static PyObject *cmp_outcome(int, PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +000061static PyObject *import_from(PyObject *, PyObject *);
62static int import_all_from(PyObject *, PyObject *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000063static PyObject *build_class(PyObject *, PyObject *, PyObject *);
64static int exec_statement(PyFrameObject *,
65 PyObject *, PyObject *, PyObject *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000066static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *);
67static void reset_exc_info(PyThreadState *);
Paul Prescode68140d2000-08-30 20:25:01 +000068static void format_exc_check_arg(PyObject *, char *, PyObject *);
Guido van Rossum374a9221991-04-04 10:40:29 +000069
Paul Prescode68140d2000-08-30 20:25:01 +000070#define NAME_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +000071 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +000072#define GLOBAL_NAME_ERROR_MSG \
73 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000074#define UNBOUNDLOCAL_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +000075 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000076#define UNBOUNDFREE_ERROR_MSG \
77 "free variable '%.200s' referenced before assignment" \
78 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000079
Guido van Rossum950361c1997-01-24 13:49:28 +000080/* Dynamic execution profile */
81#ifdef DYNAMIC_EXECUTION_PROFILE
82#ifdef DXPAIRS
83static long dxpairs[257][256];
84#define dxp dxpairs[256]
85#else
86static long dxp[256];
87#endif
88#endif
89
Jeremy Hylton985eba52003-02-05 23:13:00 +000090/* Function call profile */
91#ifdef CALL_PROFILE
92#define PCALL_NUM 11
93static int pcall[PCALL_NUM];
94
95#define PCALL_ALL 0
96#define PCALL_FUNCTION 1
97#define PCALL_FAST_FUNCTION 2
98#define PCALL_FASTER_FUNCTION 3
99#define PCALL_METHOD 4
100#define PCALL_BOUND_METHOD 5
101#define PCALL_CFUNCTION 6
102#define PCALL_TYPE 7
103#define PCALL_GENERATOR 8
104#define PCALL_OTHER 9
105#define PCALL_POP 10
106
107/* Notes about the statistics
108
109 PCALL_FAST stats
110
111 FAST_FUNCTION means no argument tuple needs to be created.
112 FASTER_FUNCTION means that the fast-path frame setup code is used.
113
114 If there is a method call where the call can be optimized by changing
115 the argument tuple and calling the function directly, it gets recorded
116 twice.
117
118 As a result, the relationship among the statistics appears to be
119 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
120 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
121 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
122 PCALL_METHOD > PCALL_BOUND_METHOD
123*/
124
125#define PCALL(POS) pcall[POS]++
126
127PyObject *
128PyEval_GetCallStats(PyObject *self)
129{
130 return Py_BuildValue("iiiiiiiiii",
131 pcall[0], pcall[1], pcall[2], pcall[3],
132 pcall[4], pcall[5], pcall[6], pcall[7],
133 pcall[8], pcall[9]);
134}
135#else
136#define PCALL(O)
137
138PyObject *
139PyEval_GetCallStats(PyObject *self)
140{
141 Py_INCREF(Py_None);
142 return Py_None;
143}
144#endif
145
Jeremy Hylton938ace62002-07-17 16:30:39 +0000146static PyTypeObject gentype;
Tim Peters5ca576e2001-06-18 22:08:13 +0000147
148typedef struct {
149 PyObject_HEAD
Tim Petersd8e1c9e2001-06-26 20:58:58 +0000150 /* The gi_ prefix is intended to remind of generator-iterator. */
151
152 PyFrameObject *gi_frame;
153
Tim Peterse77f2e22001-06-26 22:24:51 +0000154 /* True if generator is being executed. */
155 int gi_running;
Fred Drake72bc4562002-08-09 18:35:52 +0000156
157 /* List of weak reference. */
158 PyObject *gi_weakreflist;
Tim Peters5ca576e2001-06-18 22:08:13 +0000159} genobject;
160
161static PyObject *
162gen_new(PyFrameObject *f)
163{
Neil Schemenauer08de92a2002-03-18 20:45:09 +0000164 genobject *gen = PyObject_GC_New(genobject, &gentype);
Tim Peters5ca576e2001-06-18 22:08:13 +0000165 if (gen == NULL) {
166 Py_DECREF(f);
167 return NULL;
168 }
Tim Petersd8e1c9e2001-06-26 20:58:58 +0000169 gen->gi_frame = f;
170 gen->gi_running = 0;
Fred Drake72bc4562002-08-09 18:35:52 +0000171 gen->gi_weakreflist = NULL;
Neil Schemenauer08de92a2002-03-18 20:45:09 +0000172 _PyObject_GC_TRACK(gen);
Tim Peters5ca576e2001-06-18 22:08:13 +0000173 return (PyObject *)gen;
174}
175
Neil Schemenauerf8c7c202001-07-12 13:27:49 +0000176static int
177gen_traverse(genobject *gen, visitproc visit, void *arg)
178{
179 return visit((PyObject *)gen->gi_frame, arg);
180}
181
Tim Peters5ca576e2001-06-18 22:08:13 +0000182static void
183gen_dealloc(genobject *gen)
184{
Neil Schemenauer08de92a2002-03-18 20:45:09 +0000185 _PyObject_GC_UNTRACK(gen);
Fred Drake72bc4562002-08-09 18:35:52 +0000186 if (gen->gi_weakreflist != NULL)
187 PyObject_ClearWeakRefs((PyObject *) gen);
Tim Petersd8e1c9e2001-06-26 20:58:58 +0000188 Py_DECREF(gen->gi_frame);
Neil Schemenauer08de92a2002-03-18 20:45:09 +0000189 PyObject_GC_Del(gen);
Tim Peters5ca576e2001-06-18 22:08:13 +0000190}
191
192static PyObject *
193gen_iternext(genobject *gen)
194{
Neil Schemenauer2b13ce82001-06-21 02:41:10 +0000195 PyThreadState *tstate = PyThreadState_GET();
Tim Petersd8e1c9e2001-06-26 20:58:58 +0000196 PyFrameObject *f = gen->gi_frame;
Tim Peters5ca576e2001-06-18 22:08:13 +0000197 PyObject *result;
198
Tim Petersd8e1c9e2001-06-26 20:58:58 +0000199 if (gen->gi_running) {
Tim Peters5ca576e2001-06-18 22:08:13 +0000200 PyErr_SetString(PyExc_ValueError,
201 "generator already executing");
202 return NULL;
203 }
Tim Peters8c963692001-06-23 05:26:56 +0000204 if (f->f_stacktop == NULL)
Tim Peters5ca576e2001-06-18 22:08:13 +0000205 return NULL;
Neil Schemenauer2b13ce82001-06-21 02:41:10 +0000206
207 /* Generators always return to their most recent caller, not
208 * necessarily their creator. */
Tim Peters5eb4b872001-06-23 05:47:56 +0000209 Py_XINCREF(tstate->frame);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +0000210 assert(f->f_back == NULL);
211 f->f_back = tstate->frame;
212
Tim Petersd8e1c9e2001-06-26 20:58:58 +0000213 gen->gi_running = 1;
Tim Peters5ca576e2001-06-18 22:08:13 +0000214 result = eval_frame(f);
Tim Petersd8e1c9e2001-06-26 20:58:58 +0000215 gen->gi_running = 0;
Neil Schemenauer2b13ce82001-06-21 02:41:10 +0000216
217 /* Don't keep the reference to f_back any longer than necessary. It
218 * may keep a chain of frames alive or it could create a reference
219 * cycle. */
Tim Peters5eb4b872001-06-23 05:47:56 +0000220 Py_XDECREF(f->f_back);
Tim Peters6302ec62001-06-20 06:57:32 +0000221 f->f_back = NULL;
Neil Schemenauer2b13ce82001-06-21 02:41:10 +0000222
Tim Petersad1a18b2001-06-23 06:19:16 +0000223 /* If the generator just returned (as opposed to yielding), signal
224 * that the generator is exhausted. */
225 if (result == Py_None && f->f_stacktop == NULL) {
226 Py_DECREF(result);
227 result = NULL;
228 }
229
Neil Schemenauer2b13ce82001-06-21 02:41:10 +0000230 return result;
Tim Peters5ca576e2001-06-18 22:08:13 +0000231}
232
233static PyObject *
Tim Peters5ca576e2001-06-18 22:08:13 +0000234gen_getiter(PyObject *gen)
235{
236 Py_INCREF(gen);
237 return gen;
238}
239
Guido van Rossum6f799372001-09-20 20:46:19 +0000240static PyMemberDef gen_memberlist[] = {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000241 {"gi_frame", T_OBJECT, offsetof(genobject, gi_frame), RO},
242 {"gi_running", T_INT, offsetof(genobject, gi_running), RO},
243 {NULL} /* Sentinel */
244};
Tim Peters5ca576e2001-06-18 22:08:13 +0000245
Tim Peters0c322792002-07-17 16:49:03 +0000246static PyTypeObject gentype = {
Tim Peters5ca576e2001-06-18 22:08:13 +0000247 PyObject_HEAD_INIT(&PyType_Type)
248 0, /* ob_size */
249 "generator", /* tp_name */
Neil Schemenauer08de92a2002-03-18 20:45:09 +0000250 sizeof(genobject), /* tp_basicsize */
Tim Peters5ca576e2001-06-18 22:08:13 +0000251 0, /* tp_itemsize */
252 /* methods */
253 (destructor)gen_dealloc, /* tp_dealloc */
254 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000255 0, /* tp_getattr */
Tim Peters5ca576e2001-06-18 22:08:13 +0000256 0, /* tp_setattr */
257 0, /* tp_compare */
258 0, /* tp_repr */
259 0, /* tp_as_number */
260 0, /* tp_as_sequence */
261 0, /* tp_as_mapping */
262 0, /* tp_hash */
263 0, /* tp_call */
264 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000265 PyObject_GenericGetAttr, /* tp_getattro */
Tim Peters5ca576e2001-06-18 22:08:13 +0000266 0, /* tp_setattro */
267 0, /* tp_as_buffer */
Neil Schemenauer08de92a2002-03-18 20:45:09 +0000268 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
Tim Peters5ca576e2001-06-18 22:08:13 +0000269 0, /* tp_doc */
Neil Schemenauerf8c7c202001-07-12 13:27:49 +0000270 (traverseproc)gen_traverse, /* tp_traverse */
Tim Peters5ca576e2001-06-18 22:08:13 +0000271 0, /* tp_clear */
272 0, /* tp_richcompare */
Fred Drake72bc4562002-08-09 18:35:52 +0000273 offsetof(genobject, gi_weakreflist), /* tp_weaklistoffset */
Tim Peters5ca576e2001-06-18 22:08:13 +0000274 (getiterfunc)gen_getiter, /* tp_iter */
275 (iternextfunc)gen_iternext, /* tp_iternext */
Tim Petersa64295b2002-07-17 00:15:22 +0000276 0, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000277 gen_memberlist, /* tp_members */
278 0, /* tp_getset */
279 0, /* tp_base */
280 0, /* tp_dict */
Tim Peters5ca576e2001-06-18 22:08:13 +0000281};
282
283
Guido van Rossume59214e1994-08-30 08:01:59 +0000284#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000285
Guido van Rossum2571cc81999-04-07 16:07:23 +0000286#ifndef DONT_HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000287#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000288#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000289#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000290
Guido van Rossuma027efa1997-05-05 20:56:21 +0000291extern int _PyThread_Started; /* Flag for Py_Exit */
292
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000293static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
Guido van Rossuma9672091994-09-14 13:31:22 +0000294static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000295
296void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000297PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000298{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000299 if (interpreter_lock)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000300 return;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000301 _PyThread_Started = 1;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000302 interpreter_lock = PyThread_allocate_lock();
303 PyThread_acquire_lock(interpreter_lock, 1);
304 main_thread = PyThread_get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000305}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000306
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000307void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000308PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000309{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000310 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000311}
312
313void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000314PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000315{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000316 PyThread_release_lock(interpreter_lock);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000317}
318
319void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000320PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000321{
322 if (tstate == NULL)
323 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000324 /* Check someone has called PyEval_InitThreads() to create the lock */
325 assert(interpreter_lock);
Guido van Rossum65d5b571998-12-21 19:32:43 +0000326 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000327 if (PyThreadState_Swap(tstate) != NULL)
328 Py_FatalError(
329 "PyEval_AcquireThread: non-NULL old thread state");
330}
331
332void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000333PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000334{
335 if (tstate == NULL)
336 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
337 if (PyThreadState_Swap(NULL) != tstate)
338 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000339 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000340}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000341
342/* This function is called from PyOS_AfterFork to ensure that newly
343 created child processes don't hold locks referring to threads which
344 are not running in the child process. (This could also be done using
345 pthread_atfork mechanism, at least for the pthreads implementation.) */
346
347void
348PyEval_ReInitThreads(void)
349{
350 if (!interpreter_lock)
351 return;
352 /*XXX Can't use PyThread_free_lock here because it does too
353 much error-checking. Doing this cleanly would require
354 adding a new function to each thread_*.h. Instead, just
355 create a new lock and waste a little bit of memory */
356 interpreter_lock = PyThread_allocate_lock();
357 PyThread_acquire_lock(interpreter_lock, 1);
358 main_thread = PyThread_get_thread_ident();
359}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000360#endif
361
Guido van Rossumff4949e1992-08-05 19:58:53 +0000362/* Functions save_thread and restore_thread are always defined so
363 dynamically loaded modules needn't be compiled separately for use
364 with and without threads: */
365
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000366PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000367PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000368{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000369 PyThreadState *tstate = PyThreadState_Swap(NULL);
370 if (tstate == NULL)
371 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000372#ifdef WITH_THREAD
Guido van Rossumb74eca91997-09-30 22:03:16 +0000373 if (interpreter_lock)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000374 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000375#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000376 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000377}
378
379void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000380PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000381{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000382 if (tstate == NULL)
383 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000384#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000385 if (interpreter_lock) {
Guido van Rossumb74eca91997-09-30 22:03:16 +0000386 int err = errno;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000387 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000388 errno = err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000389 }
390#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000391 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000392}
393
394
Guido van Rossuma9672091994-09-14 13:31:22 +0000395/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
396 signal handlers or Mac I/O completion routines) can schedule calls
397 to a function to be called synchronously.
398 The synchronous function is called with one void* argument.
399 It should return 0 for success or -1 for failure -- failure should
400 be accompanied by an exception.
401
402 If registry succeeds, the registry function returns 0; if it fails
403 (e.g. due to too many pending calls) it returns -1 (without setting
404 an exception condition).
405
406 Note that because registry may occur from within signal handlers,
407 or other asynchronous events, calling malloc() is unsafe!
408
409#ifdef WITH_THREAD
410 Any thread can schedule pending calls, but only the main thread
411 will execute them.
412#endif
413
414 XXX WARNING! ASYNCHRONOUSLY EXECUTING CODE!
415 There are two possible race conditions:
416 (1) nested asynchronous registry calls;
417 (2) registry calls made while pending calls are being processed.
418 While (1) is very unlikely, (2) is a real possibility.
419 The current code is safe against (2), but not against (1).
420 The safety against (2) is derived from the fact that only one
421 thread (the main thread) ever takes things out of the queue.
Guido van Rossuma9672091994-09-14 13:31:22 +0000422
Guido van Rossuma027efa1997-05-05 20:56:21 +0000423 XXX Darn! With the advent of thread state, we should have an array
424 of pending calls per thread in the thread state! Later...
425*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000426
Guido van Rossuma9672091994-09-14 13:31:22 +0000427#define NPENDINGCALLS 32
428static struct {
Thomas Wouters334fb892000-07-25 12:56:38 +0000429 int (*func)(void *);
430 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000431} pendingcalls[NPENDINGCALLS];
432static volatile int pendingfirst = 0;
433static volatile int pendinglast = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000434static volatile int things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000435
436int
Thomas Wouters334fb892000-07-25 12:56:38 +0000437Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000438{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000439 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000440 int i, j;
441 /* XXX Begin critical section */
442 /* XXX If you want this to be safe against nested
443 XXX asynchronous calls, you'll have to work harder! */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000444 if (busy)
445 return -1;
446 busy = 1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000447 i = pendinglast;
448 j = (i + 1) % NPENDINGCALLS;
Guido van Rossum04e70322002-07-17 16:57:13 +0000449 if (j == pendingfirst) {
450 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000451 return -1; /* Queue full */
Guido van Rossum04e70322002-07-17 16:57:13 +0000452 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000453 pendingcalls[i].func = func;
454 pendingcalls[i].arg = arg;
455 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000456
457 _Py_Ticker = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000458 things_to_do = 1; /* Signal main loop */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000459 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000460 /* XXX End critical section */
461 return 0;
462}
463
Guido van Rossum180d7b41994-09-29 09:45:57 +0000464int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000465Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000466{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000467 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000468#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000469 if (main_thread && PyThread_get_thread_ident() != main_thread)
Guido van Rossuma9672091994-09-14 13:31:22 +0000470 return 0;
471#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000472 if (busy)
Guido van Rossum180d7b41994-09-29 09:45:57 +0000473 return 0;
474 busy = 1;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000475 things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000476 for (;;) {
477 int i;
Thomas Wouters334fb892000-07-25 12:56:38 +0000478 int (*func)(void *);
479 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000480 i = pendingfirst;
481 if (i == pendinglast)
482 break; /* Queue empty */
483 func = pendingcalls[i].func;
484 arg = pendingcalls[i].arg;
485 pendingfirst = (i + 1) % NPENDINGCALLS;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000486 if (func(arg) < 0) {
487 busy = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000488 things_to_do = 1; /* We're not done yet */
Guido van Rossuma9672091994-09-14 13:31:22 +0000489 return -1;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000490 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000491 }
Guido van Rossum180d7b41994-09-29 09:45:57 +0000492 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000493 return 0;
494}
495
496
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000497/* The interpreter's recursion limit */
498
Guido van Rossum349ff6f2000-09-01 01:52:08 +0000499static int recursion_limit = 1000;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000500
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000501int
502Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000503{
504 return recursion_limit;
505}
506
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000507void
508Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000509{
510 recursion_limit = new_limit;
511}
512
Guido van Rossum374a9221991-04-04 10:40:29 +0000513/* Status code for main loop (reason for stack unwind) */
514
515enum why_code {
516 WHY_NOT, /* No error */
517 WHY_EXCEPTION, /* Exception occurred */
518 WHY_RERAISE, /* Exception re-raised by 'finally' */
519 WHY_RETURN, /* 'return' statement */
Jeremy Hylton3faa52e2001-02-01 22:48:12 +0000520 WHY_BREAK, /* 'break' statement */
Tim Peters5ca576e2001-06-18 22:08:13 +0000521 WHY_CONTINUE, /* 'continue' statement */
Tim Peters6e6a63f2001-10-18 20:49:35 +0000522 WHY_YIELD /* 'yield' operator */
Guido van Rossum374a9221991-04-04 10:40:29 +0000523};
524
Tim Petersdbd9ba62000-07-09 03:09:57 +0000525static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
Tim Petersd6d010b2001-06-21 02:49:55 +0000526static int unpack_iterable(PyObject *, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000527
Skip Montanarod581d772002-09-03 20:10:45 +0000528/* for manipulating the thread switch and periodic "stuff" - used to be
529 per thread, now just a pair o' globals */
Skip Montanaro99dba272002-09-03 20:19:06 +0000530int _Py_CheckInterval = 100;
531volatile int _Py_Ticker = 100;
Guido van Rossum374a9221991-04-04 10:40:29 +0000532
Guido van Rossumb209a111997-04-29 18:18:01 +0000533PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000534PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000535{
Jeremy Hylton985eba52003-02-05 23:13:00 +0000536 /* XXX raise SystemError if globals is NULL */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000537 return PyEval_EvalCodeEx(co,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000538 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000539 (PyObject **)NULL, 0,
540 (PyObject **)NULL, 0,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000541 (PyObject **)NULL, 0,
542 NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000543}
544
545
546/* Interpreter main loop */
547
Tim Peters6d6c1a32001-08-02 04:15:00 +0000548static PyObject *
Tim Peters5ca576e2001-06-18 22:08:13 +0000549eval_frame(PyFrameObject *f)
Guido van Rossum374a9221991-04-04 10:40:29 +0000550{
Guido van Rossum950361c1997-01-24 13:49:28 +0000551#ifdef DXPAIRS
552 int lastopcode = 0;
553#endif
Tim Petersb6d14da2001-12-19 04:11:07 +0000554 PyObject **stack_pointer; /* Next free slot in value stack */
Guido van Rossum374a9221991-04-04 10:40:29 +0000555 register unsigned char *next_instr;
Moshe Zadkaaa39a7e2000-08-07 06:34:45 +0000556 register int opcode=0; /* Current opcode */
557 register int oparg=0; /* Current opcode argument, if any */
Guido van Rossum374a9221991-04-04 10:40:29 +0000558 register enum why_code why; /* Reason for block stack unwind */
559 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000560 register PyObject *x; /* Result object -- NULL if error */
561 register PyObject *v; /* Temporary objects popped off stack */
562 register PyObject *w;
563 register PyObject *u;
564 register PyObject *t;
Barry Warsaw23c9ec82000-08-21 15:44:01 +0000565 register PyObject *stream = NULL; /* for PRINT opcodes */
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000566 register PyObject **fastlocals, **freevars;
Guido van Rossum014518f1998-11-23 21:09:51 +0000567 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000568 PyThreadState *tstate = PyThreadState_GET();
Tim Peters5ca576e2001-06-18 22:08:13 +0000569 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000570
571 /* when tracing we set things up so that
572
573 not (instr_lb <= current_bytecode_offset < instr_ub)
574
575 is true when the line being executed has changed. The
576 initial values are such as to make this false the first
577 time it is tested. */
578 int instr_ub = -1, instr_lb = 0;
579
Guido van Rossumd076c731998-10-07 19:42:25 +0000580 unsigned char *first_instr;
Skip Montanaro04d80f82002-08-04 21:03:35 +0000581 PyObject *names;
582 PyObject *consts;
Guido van Rossum96a42c81992-01-12 02:29:51 +0000583#ifdef LLTRACE
Guido van Rossumacbe8da1993-04-15 15:33:52 +0000584 int lltrace;
Guido van Rossum374a9221991-04-04 10:40:29 +0000585#endif
Guido van Rossum408027e1996-12-30 16:17:54 +0000586#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000587 /* Make it easier to find out where we are with a debugger */
Tim Peters5ca576e2001-06-18 22:08:13 +0000588 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000589#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000590
Neal Norwitza81d2202002-07-14 00:27:26 +0000591/* Tuple access macros */
592
593#ifndef Py_DEBUG
594#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
595#else
596#define GETITEM(v, i) PyTuple_GetItem((v), (i))
597#endif
598
Guido van Rossum374a9221991-04-04 10:40:29 +0000599/* Code access macros */
600
Guido van Rossumd076c731998-10-07 19:42:25 +0000601#define INSTR_OFFSET() (next_instr - first_instr)
Guido van Rossum374a9221991-04-04 10:40:29 +0000602#define NEXTOP() (*next_instr++)
603#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Guido van Rossumd076c731998-10-07 19:42:25 +0000604#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000605#define JUMPBY(x) (next_instr += (x))
606
Raymond Hettingerf606f872003-03-16 03:11:04 +0000607/* OpCode prediction macros
608 Some opcodes tend to come in pairs thus making it possible to predict
609 the second code when the first is run. For example, COMPARE_OP is often
610 followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And, those opcodes are often
611 followed by a POP_TOP.
612
613 Verifying the prediction costs a single high-speed test of register
Raymond Hettingerac2072922003-03-16 15:41:11 +0000614 variable against a constant. If the pairing was good, then the
Raymond Hettingerf606f872003-03-16 03:11:04 +0000615 processor has a high likelihood of making its own successful branch
616 prediction which results in a nearly zero overhead transition to the
617 next opcode.
618
619 A successful prediction saves a trip through the eval-loop including
620 its two unpredictable branches, the HASARG test and the switch-case.
621*/
622
Raymond Hettingerac2072922003-03-16 15:41:11 +0000623#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingerf606f872003-03-16 03:11:04 +0000624#define PREDICTED(op) PRED_##op: next_instr++
Raymond Hettinger7dc52212003-03-16 20:14:44 +0000625#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = (next_instr[2]<<8) + \
626 next_instr[1]; next_instr += 3
Raymond Hettingerf606f872003-03-16 03:11:04 +0000627
Guido van Rossum374a9221991-04-04 10:40:29 +0000628/* Stack manipulation macros */
629
630#define STACK_LEVEL() (stack_pointer - f->f_valuestack)
631#define EMPTY() (STACK_LEVEL() == 0)
632#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000633#define SECOND() (stack_pointer[-2])
634#define THIRD() (stack_pointer[-3])
635#define FOURTH() (stack_pointer[-4])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000636#define SET_TOP(v) (stack_pointer[-1] = (v))
637#define SET_SECOND(v) (stack_pointer[-2] = (v))
638#define SET_THIRD(v) (stack_pointer[-3] = (v))
639#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000640#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000641#define BASIC_PUSH(v) (*stack_pointer++ = (v))
642#define BASIC_POP() (*--stack_pointer)
643
Guido van Rossum96a42c81992-01-12 02:29:51 +0000644#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000645#define PUSH(v) { (void)(BASIC_PUSH(v), \
646 lltrace && prtrace(TOP(), "push")); \
647 assert(STACK_LEVEL() <= f->f_stacksize); }
Fred Drakede26cfc2001-10-13 06:11:28 +0000648#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000649#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
650 lltrace && prtrace(TOP(), "stackadj")); \
651 assert(STACK_LEVEL() <= f->f_stacksize); }
Guido van Rossum374a9221991-04-04 10:40:29 +0000652#else
653#define PUSH(v) BASIC_PUSH(v)
654#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000655#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000656#endif
657
Guido van Rossum681d79a1995-07-18 14:51:37 +0000658/* Local variable macros */
659
660#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000661
662/* The SETLOCAL() macro must not DECREF the local variable in-place and
663 then store the new value; it must copy the old value to a temporary
664 value, then store the new value, and then DECREF the temporary value.
665 This is because it is possible that during the DECREF the frame is
666 accessed by other code (e.g. a __del__ method or gc.collect()) and the
667 variable would be pointing to already-freed memory. */
668#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
669 GETLOCAL(i) = value; \
670 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000671
Guido van Rossuma027efa1997-05-05 20:56:21 +0000672/* Start of code */
673
Tim Peters5ca576e2001-06-18 22:08:13 +0000674 if (f == NULL)
675 return NULL;
676
Guido van Rossum8861b741996-07-30 16:49:37 +0000677#ifdef USE_STACKCHECK
Guido van Rossuma027efa1997-05-05 20:56:21 +0000678 if (tstate->recursion_depth%10 == 0 && PyOS_CheckStack()) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000679 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
Guido van Rossum8861b741996-07-30 16:49:37 +0000680 return NULL;
681 }
682#endif
683
Tim Peters5ca576e2001-06-18 22:08:13 +0000684 /* push frame */
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000685 if (++tstate->recursion_depth > recursion_limit) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000686 --tstate->recursion_depth;
687 PyErr_SetString(PyExc_RuntimeError,
Fred Drake661ea262000-10-24 19:57:45 +0000688 "maximum recursion depth exceeded");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000689 tstate->frame = f->f_back;
Guido van Rossum8861b741996-07-30 16:49:37 +0000690 return NULL;
691 }
692
Tim Peters5ca576e2001-06-18 22:08:13 +0000693 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000694
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000695 if (tstate->use_tracing) {
696 if (tstate->c_tracefunc != NULL) {
697 /* tstate->c_tracefunc, if defined, is a
698 function that will be called on *every* entry
699 to a code block. Its return value, if not
700 None, is a function that will be called at
701 the start of each executed line of code.
702 (Actually, the function must return itself
703 in order to continue tracing.) The trace
704 functions are called with three arguments:
705 a pointer to the current frame, a string
706 indicating why the function is called, and
707 an argument which depends on the situation.
708 The global trace function is also called
709 whenever an exception is detected. */
710 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
711 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000712 /* Trace function raised an error */
Michael W. Hudsonfb4d6ec2002-10-02 13:13:45 +0000713 --tstate->recursion_depth;
714 tstate->frame = f->f_back;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000715 return NULL;
716 }
717 }
718 if (tstate->c_profilefunc != NULL) {
719 /* Similar for c_profilefunc, except it needn't
720 return itself and isn't called for "line" events */
721 if (call_trace(tstate->c_profilefunc,
722 tstate->c_profileobj,
723 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000724 /* Profile function raised an error */
Michael W. Hudsonfb4d6ec2002-10-02 13:13:45 +0000725 --tstate->recursion_depth;
726 tstate->frame = f->f_back;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000727 return NULL;
728 }
729 }
730 }
731
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000732 co = f->f_code;
733 names = co->co_names;
734 consts = co->co_consts;
735 fastlocals = f->f_localsplus;
736 freevars = f->f_localsplus + f->f_nlocals;
737 _PyCode_GETCODEPTR(co, &first_instr);
738 /* An explanation is in order for the next line.
739
740 f->f_lasti now refers to the index of the last instruction
741 executed. You might think this was obvious from the name, but
742 this wasn't always true before 2.3! PyFrame_New now sets
743 f->f_lasti to -1 (i.e. the index *before* the first instruction)
744 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
745 does work. Promise. */
746 next_instr = first_instr + f->f_lasti + 1;
747 stack_pointer = f->f_stacktop;
748 assert(stack_pointer != NULL);
749 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
750
Tim Peters5ca576e2001-06-18 22:08:13 +0000751#ifdef LLTRACE
752 lltrace = PyDict_GetItemString(f->f_globals,"__lltrace__") != NULL;
753#endif
754#if defined(Py_DEBUG) || defined(LLTRACE)
755 filename = PyString_AsString(co->co_filename);
756#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000757
Guido van Rossum374a9221991-04-04 10:40:29 +0000758 why = WHY_NOT;
759 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +0000760 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +0000761 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +0000762
Guido van Rossum374a9221991-04-04 10:40:29 +0000763 for (;;) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000764 assert(stack_pointer >= f->f_valuestack); /* else underflow */
765 assert(STACK_LEVEL() <= f->f_stacksize); /* else overflow */
766
Guido van Rossuma027efa1997-05-05 20:56:21 +0000767 /* Do periodic things. Doing this every time through
768 the loop would add too much overhead, so we do it
769 only every Nth instruction. We also do it if
770 ``things_to_do'' is set, i.e. when an asynchronous
771 event needs attention (e.g. a signal handler or
772 async I/O handler); see Py_AddPendingCall() and
773 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000774
Skip Montanarod581d772002-09-03 20:10:45 +0000775 if (--_Py_Ticker < 0) {
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000776 if (*next_instr == SETUP_FINALLY) {
777 /* Make the last opcode before
778 a try: finally: block uninterruptable. */
779 goto fast_next_opcode;
780 }
Skip Montanarod581d772002-09-03 20:10:45 +0000781 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000782 tstate->tick_counter++;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000783 if (things_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +0000784 if (Py_MakePendingCalls() < 0) {
785 why = WHY_EXCEPTION;
786 goto on_error;
787 }
788 }
Guido van Rossumdf0d00e1997-05-20 15:57:49 +0000789#if !defined(HAVE_SIGNAL_H) || defined(macintosh)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000790 /* If we have true signals, the signal handler
791 will call Py_AddPendingCall() so we don't
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000792 have to call PyErr_CheckSignals(). On the
793 Mac and DOS, alas, we have to call it. */
Guido van Rossumb209a111997-04-29 18:18:01 +0000794 if (PyErr_CheckSignals()) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000795 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +0000796 goto on_error;
797 }
Guido van Rossum70d44781997-01-21 06:15:24 +0000798#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000799
Guido van Rossume59214e1994-08-30 08:01:59 +0000800#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000801 if (interpreter_lock) {
802 /* Give another thread a chance */
803
Guido van Rossum25ce5661997-08-02 03:10:38 +0000804 if (PyThreadState_Swap(NULL) != tstate)
805 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000806 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000807
808 /* Other threads may run now */
809
Guido van Rossum65d5b571998-12-21 19:32:43 +0000810 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000811 if (PyThreadState_Swap(tstate) != NULL)
812 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000813
814 /* Check for thread interrupts */
815
816 if (tstate->async_exc != NULL) {
817 x = tstate->async_exc;
818 tstate->async_exc = NULL;
819 PyErr_SetNone(x);
820 Py_DECREF(x);
821 why = WHY_EXCEPTION;
822 goto on_error;
823 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000824 }
825#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000826 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000827
Neil Schemenauer63543862002-02-17 19:10:14 +0000828 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +0000829 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +0000830
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000831 /* line-by-line tracing support */
832
833 if (tstate->c_tracefunc != NULL && !tstate->tracing) {
834 /* see maybe_call_line_trace
835 for expository comments */
836 f->f_stacktop = stack_pointer;
Michael W. Hudson006c7522002-11-08 13:08:46 +0000837
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000838 err = maybe_call_line_trace(tstate->c_tracefunc,
839 tstate->c_traceobj,
840 f, &instr_lb, &instr_ub);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000841 /* Reload possibly changed frame fields */
842 JUMPTO(f->f_lasti);
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000843 if (f->f_stacktop != NULL) {
844 stack_pointer = f->f_stacktop;
845 f->f_stacktop = NULL;
846 }
847 if (err) {
848 /* trace function raised an exception */
849 goto on_error;
850 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000851 }
852
853 /* Extract opcode and argument */
854
Guido van Rossum374a9221991-04-04 10:40:29 +0000855 opcode = NEXTOP();
856 if (HAS_ARG(opcode))
857 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +0000858 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +0000859#ifdef DYNAMIC_EXECUTION_PROFILE
860#ifdef DXPAIRS
861 dxpairs[lastopcode][opcode]++;
862 lastopcode = opcode;
863#endif
864 dxp[opcode]++;
865#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000866
Guido van Rossum96a42c81992-01-12 02:29:51 +0000867#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +0000868 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +0000869
Guido van Rossum96a42c81992-01-12 02:29:51 +0000870 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000871 if (HAS_ARG(opcode)) {
872 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000873 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +0000874 }
875 else {
876 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000877 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +0000878 }
879 }
880#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000881
Guido van Rossum374a9221991-04-04 10:40:29 +0000882 /* Main switch on opcode */
Jeremy Hylton52820442001-01-03 23:52:36 +0000883
Guido van Rossum374a9221991-04-04 10:40:29 +0000884 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +0000885
Guido van Rossum374a9221991-04-04 10:40:29 +0000886 /* BEWARE!
887 It is essential that any operation that fails sets either
888 x to NULL, err to nonzero, or why to anything but WHY_NOT,
889 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000890
Guido van Rossum374a9221991-04-04 10:40:29 +0000891 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000892
Neil Schemenauer63543862002-02-17 19:10:14 +0000893 case LOAD_FAST:
894 x = GETLOCAL(oparg);
895 if (x != NULL) {
896 Py_INCREF(x);
897 PUSH(x);
898 goto fast_next_opcode;
899 }
900 format_exc_check_arg(PyExc_UnboundLocalError,
901 UNBOUNDLOCAL_ERROR_MSG,
902 PyTuple_GetItem(co->co_varnames, oparg));
903 break;
904
905 case LOAD_CONST:
Skip Montanaro04d80f82002-08-04 21:03:35 +0000906 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +0000907 Py_INCREF(x);
908 PUSH(x);
909 goto fast_next_opcode;
910
Raymond Hettinger7dc52212003-03-16 20:14:44 +0000911 PREDICTED_WITH_ARG(STORE_FAST);
Neil Schemenauer63543862002-02-17 19:10:14 +0000912 case STORE_FAST:
913 v = POP();
914 SETLOCAL(oparg, v);
915 goto fast_next_opcode;
916
Raymond Hettingerf606f872003-03-16 03:11:04 +0000917 PREDICTED(POP_TOP);
Guido van Rossum374a9221991-04-04 10:40:29 +0000918 case POP_TOP:
919 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000920 Py_DECREF(v);
Neil Schemenauer63543862002-02-17 19:10:14 +0000921 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000922
Guido van Rossum374a9221991-04-04 10:40:29 +0000923 case ROT_TWO:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000924 v = TOP();
925 w = SECOND();
926 SET_TOP(w);
927 SET_SECOND(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000928 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000929
Guido van Rossum374a9221991-04-04 10:40:29 +0000930 case ROT_THREE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000931 v = TOP();
932 w = SECOND();
933 x = THIRD();
934 SET_TOP(w);
935 SET_SECOND(x);
936 SET_THIRD(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000937 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000938
Thomas Wouters434d0822000-08-24 20:11:32 +0000939 case ROT_FOUR:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000940 u = TOP();
941 v = SECOND();
942 w = THIRD();
943 x = FOURTH();
944 SET_TOP(v);
945 SET_SECOND(w);
946 SET_THIRD(x);
947 SET_FOURTH(u);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000948 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000949
Guido van Rossum374a9221991-04-04 10:40:29 +0000950 case DUP_TOP:
951 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000952 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000953 PUSH(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000954 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000955
Thomas Wouters434d0822000-08-24 20:11:32 +0000956 case DUP_TOPX:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000957 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000958 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000959 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000960 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000961 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000962 STACKADJ(2);
963 SET_TOP(x);
964 SET_SECOND(w);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000965 goto fast_next_opcode;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000966 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000967 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000968 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000969 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000970 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000971 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +0000972 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000973 STACKADJ(3);
974 SET_TOP(x);
975 SET_SECOND(w);
976 SET_THIRD(v);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000977 goto fast_next_opcode;
Thomas Wouters434d0822000-08-24 20:11:32 +0000978 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000979 Py_FatalError("invalid argument to DUP_TOPX"
980 " (bytecode corruption?)");
Tim Peters35ba6892000-10-11 07:04:49 +0000981 break;
Thomas Wouters434d0822000-08-24 20:11:32 +0000982
Guido van Rossum374a9221991-04-04 10:40:29 +0000983 case UNARY_POSITIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000984 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000985 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000986 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000987 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000988 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000989 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000990
Guido van Rossum374a9221991-04-04 10:40:29 +0000991 case UNARY_NEGATIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000992 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000993 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000994 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000995 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000996 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000997 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000998
Guido van Rossum374a9221991-04-04 10:40:29 +0000999 case UNARY_NOT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001000 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001001 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001002 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +00001003 if (err == 0) {
1004 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001005 SET_TOP(Py_True);
Guido van Rossumfc490731997-05-06 15:06:49 +00001006 continue;
1007 }
1008 else if (err > 0) {
1009 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001010 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +00001011 err = 0;
1012 continue;
1013 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00001014 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001015 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001016
Guido van Rossum374a9221991-04-04 10:40:29 +00001017 case UNARY_CONVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001018 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001019 x = PyObject_Repr(v);
1020 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001021 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001022 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001023 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001024
Guido van Rossum7928cd71991-10-24 14:59:31 +00001025 case UNARY_INVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001026 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001027 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001028 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001029 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001030 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001031 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001032
Guido van Rossum50564e81996-01-12 01:13:16 +00001033 case BINARY_POWER:
1034 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001035 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001036 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +00001037 Py_DECREF(v);
1038 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001039 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001040 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +00001041 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001042
Guido van Rossum374a9221991-04-04 10:40:29 +00001043 case BINARY_MULTIPLY:
1044 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001045 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001046 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001047 Py_DECREF(v);
1048 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001049 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001050 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001051 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001052
Guido van Rossum374a9221991-04-04 10:40:29 +00001053 case BINARY_DIVIDE:
Tim Peters3caca232001-12-06 06:23:26 +00001054 if (!_Py_QnewFlag) {
1055 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001056 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001057 x = PyNumber_Divide(v, w);
1058 Py_DECREF(v);
1059 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001060 SET_TOP(x);
Tim Peters3caca232001-12-06 06:23:26 +00001061 if (x != NULL) continue;
1062 break;
1063 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001064 /* -Qnew is in effect: fall through to
Tim Peters3caca232001-12-06 06:23:26 +00001065 BINARY_TRUE_DIVIDE */
1066 case BINARY_TRUE_DIVIDE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001067 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001068 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001069 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001070 Py_DECREF(v);
1071 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001072 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001073 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001074 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001075
Guido van Rossum4668b002001-08-08 05:00:18 +00001076 case BINARY_FLOOR_DIVIDE:
1077 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001078 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001079 x = PyNumber_FloorDivide(v, w);
1080 Py_DECREF(v);
1081 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001082 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001083 if (x != NULL) continue;
1084 break;
1085
Guido van Rossum374a9221991-04-04 10:40:29 +00001086 case BINARY_MODULO:
1087 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001088 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001089 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001090 Py_DECREF(v);
1091 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001092 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001093 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001094 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001095
Guido van Rossum374a9221991-04-04 10:40:29 +00001096 case BINARY_ADD:
1097 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001098 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001099 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001100 /* INLINE: int + int */
1101 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001102 a = PyInt_AS_LONG(v);
1103 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001104 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001105 if ((i^a) < 0 && (i^b) < 0)
1106 goto slow_add;
1107 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001108 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001109 else {
1110 slow_add:
Guido van Rossumc12da691997-07-17 23:12:42 +00001111 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001112 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001113 Py_DECREF(v);
1114 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001115 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001116 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001117 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001118
Guido van Rossum374a9221991-04-04 10:40:29 +00001119 case BINARY_SUBTRACT:
1120 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001121 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001122 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001123 /* INLINE: int - int */
1124 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001125 a = PyInt_AS_LONG(v);
1126 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001127 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001128 if ((i^a) < 0 && (i^~b) < 0)
1129 goto slow_sub;
1130 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001131 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001132 else {
1133 slow_sub:
Guido van Rossumc12da691997-07-17 23:12:42 +00001134 x = PyNumber_Subtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001135 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001136 Py_DECREF(v);
1137 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001138 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001139 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001140 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001141
Guido van Rossum374a9221991-04-04 10:40:29 +00001142 case BINARY_SUBSCR:
1143 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001144 v = TOP();
Tim Petersb1c46982001-10-05 20:41:38 +00001145 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001146 /* INLINE: list[int] */
1147 long i = PyInt_AsLong(w);
1148 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +00001149 i += PyList_GET_SIZE(v);
Guido van Rossumc12da691997-07-17 23:12:42 +00001150 if (i < 0 ||
Guido van Rossumfa00e951998-07-08 15:02:37 +00001151 i >= PyList_GET_SIZE(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001152 PyErr_SetString(PyExc_IndexError,
1153 "list index out of range");
1154 x = NULL;
1155 }
1156 else {
Guido van Rossumfa00e951998-07-08 15:02:37 +00001157 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001158 Py_INCREF(x);
1159 }
1160 }
1161 else
1162 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001163 Py_DECREF(v);
1164 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001165 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001166 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001167 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001168
Guido van Rossum7928cd71991-10-24 14:59:31 +00001169 case BINARY_LSHIFT:
1170 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001171 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001172 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001173 Py_DECREF(v);
1174 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001175 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001176 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001177 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001178
Guido van Rossum7928cd71991-10-24 14:59:31 +00001179 case BINARY_RSHIFT:
1180 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001181 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001182 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001183 Py_DECREF(v);
1184 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001185 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001186 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001187 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001188
Guido van Rossum7928cd71991-10-24 14:59:31 +00001189 case BINARY_AND:
1190 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001191 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001192 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001193 Py_DECREF(v);
1194 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001195 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001196 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001197 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001198
Guido van Rossum7928cd71991-10-24 14:59:31 +00001199 case BINARY_XOR:
1200 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001201 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001202 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001203 Py_DECREF(v);
1204 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001205 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001206 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001207 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001208
Guido van Rossum7928cd71991-10-24 14:59:31 +00001209 case BINARY_OR:
1210 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001211 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001212 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001213 Py_DECREF(v);
1214 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001215 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001216 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001217 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001218
1219 case INPLACE_POWER:
1220 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001221 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001222 x = PyNumber_InPlacePower(v, w, Py_None);
1223 Py_DECREF(v);
1224 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001225 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001226 if (x != NULL) continue;
1227 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001228
Thomas Wouters434d0822000-08-24 20:11:32 +00001229 case INPLACE_MULTIPLY:
1230 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001231 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001232 x = PyNumber_InPlaceMultiply(v, w);
1233 Py_DECREF(v);
1234 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001235 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001236 if (x != NULL) continue;
1237 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001238
Thomas Wouters434d0822000-08-24 20:11:32 +00001239 case INPLACE_DIVIDE:
Tim Peters54b11912001-12-25 18:49:11 +00001240 if (!_Py_QnewFlag) {
1241 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001242 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001243 x = PyNumber_InPlaceDivide(v, w);
1244 Py_DECREF(v);
1245 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001246 SET_TOP(x);
Tim Peters54b11912001-12-25 18:49:11 +00001247 if (x != NULL) continue;
1248 break;
1249 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001250 /* -Qnew is in effect: fall through to
Tim Peters54b11912001-12-25 18:49:11 +00001251 INPLACE_TRUE_DIVIDE */
1252 case INPLACE_TRUE_DIVIDE:
Thomas Wouters434d0822000-08-24 20:11:32 +00001253 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001254 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001255 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001256 Py_DECREF(v);
1257 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001258 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001259 if (x != NULL) continue;
1260 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001261
Guido van Rossum4668b002001-08-08 05:00:18 +00001262 case INPLACE_FLOOR_DIVIDE:
1263 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001264 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001265 x = PyNumber_InPlaceFloorDivide(v, w);
1266 Py_DECREF(v);
1267 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001268 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001269 if (x != NULL) continue;
1270 break;
1271
Thomas Wouters434d0822000-08-24 20:11:32 +00001272 case INPLACE_MODULO:
1273 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001274 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001275 x = PyNumber_InPlaceRemainder(v, w);
1276 Py_DECREF(v);
1277 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001278 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001279 if (x != NULL) continue;
1280 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001281
Thomas Wouters434d0822000-08-24 20:11:32 +00001282 case INPLACE_ADD:
1283 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001284 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001285 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001286 /* INLINE: int + int */
1287 register long a, b, i;
1288 a = PyInt_AS_LONG(v);
1289 b = PyInt_AS_LONG(w);
1290 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001291 if ((i^a) < 0 && (i^b) < 0)
1292 goto slow_iadd;
1293 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001294 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001295 else {
1296 slow_iadd:
Thomas Wouters434d0822000-08-24 20:11:32 +00001297 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001298 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001299 Py_DECREF(v);
1300 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001301 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001302 if (x != NULL) continue;
1303 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001304
Thomas Wouters434d0822000-08-24 20:11:32 +00001305 case INPLACE_SUBTRACT:
1306 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001307 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001308 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001309 /* INLINE: int - int */
1310 register long a, b, i;
1311 a = PyInt_AS_LONG(v);
1312 b = PyInt_AS_LONG(w);
1313 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001314 if ((i^a) < 0 && (i^~b) < 0)
1315 goto slow_isub;
1316 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001317 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001318 else {
1319 slow_isub:
Thomas Wouters434d0822000-08-24 20:11:32 +00001320 x = PyNumber_InPlaceSubtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001321 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001322 Py_DECREF(v);
1323 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001324 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001325 if (x != NULL) continue;
1326 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001327
Thomas Wouters434d0822000-08-24 20:11:32 +00001328 case INPLACE_LSHIFT:
1329 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001330 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001331 x = PyNumber_InPlaceLshift(v, w);
1332 Py_DECREF(v);
1333 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001334 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001335 if (x != NULL) continue;
1336 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001337
Thomas Wouters434d0822000-08-24 20:11:32 +00001338 case INPLACE_RSHIFT:
1339 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001340 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001341 x = PyNumber_InPlaceRshift(v, w);
1342 Py_DECREF(v);
1343 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001344 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001345 if (x != NULL) continue;
1346 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001347
Thomas Wouters434d0822000-08-24 20:11:32 +00001348 case INPLACE_AND:
1349 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001350 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001351 x = PyNumber_InPlaceAnd(v, w);
1352 Py_DECREF(v);
1353 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001354 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001355 if (x != NULL) continue;
1356 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001357
Thomas Wouters434d0822000-08-24 20:11:32 +00001358 case INPLACE_XOR:
1359 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001360 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001361 x = PyNumber_InPlaceXor(v, w);
1362 Py_DECREF(v);
1363 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001364 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001365 if (x != NULL) continue;
1366 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001367
Thomas Wouters434d0822000-08-24 20:11:32 +00001368 case INPLACE_OR:
1369 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001370 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001371 x = PyNumber_InPlaceOr(v, w);
1372 Py_DECREF(v);
1373 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001374 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001375 if (x != NULL) continue;
1376 break;
1377
Guido van Rossum374a9221991-04-04 10:40:29 +00001378 case SLICE+0:
1379 case SLICE+1:
1380 case SLICE+2:
1381 case SLICE+3:
1382 if ((opcode-SLICE) & 2)
1383 w = POP();
1384 else
1385 w = NULL;
1386 if ((opcode-SLICE) & 1)
1387 v = POP();
1388 else
1389 v = NULL;
Raymond Hettinger663004b2003-01-09 15:24:30 +00001390 u = TOP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001391 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001392 Py_DECREF(u);
1393 Py_XDECREF(v);
1394 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001395 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001396 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001397 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001398
Guido van Rossum374a9221991-04-04 10:40:29 +00001399 case STORE_SLICE+0:
1400 case STORE_SLICE+1:
1401 case STORE_SLICE+2:
1402 case STORE_SLICE+3:
1403 if ((opcode-STORE_SLICE) & 2)
1404 w = POP();
1405 else
1406 w = NULL;
1407 if ((opcode-STORE_SLICE) & 1)
1408 v = POP();
1409 else
1410 v = NULL;
1411 u = POP();
1412 t = POP();
1413 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +00001414 Py_DECREF(t);
1415 Py_DECREF(u);
1416 Py_XDECREF(v);
1417 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001418 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001419 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001420
Guido van Rossum374a9221991-04-04 10:40:29 +00001421 case DELETE_SLICE+0:
1422 case DELETE_SLICE+1:
1423 case DELETE_SLICE+2:
1424 case DELETE_SLICE+3:
1425 if ((opcode-DELETE_SLICE) & 2)
1426 w = POP();
1427 else
1428 w = NULL;
1429 if ((opcode-DELETE_SLICE) & 1)
1430 v = POP();
1431 else
1432 v = NULL;
1433 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001434 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +00001435 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00001436 Py_DECREF(u);
1437 Py_XDECREF(v);
1438 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001439 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001440 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001441
Guido van Rossum374a9221991-04-04 10:40:29 +00001442 case STORE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001443 w = TOP();
1444 v = SECOND();
1445 u = THIRD();
1446 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001447 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001448 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001449 Py_DECREF(u);
1450 Py_DECREF(v);
1451 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001452 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001453 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001454
Guido van Rossum374a9221991-04-04 10:40:29 +00001455 case DELETE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001456 w = TOP();
1457 v = SECOND();
1458 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001459 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001460 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001461 Py_DECREF(v);
1462 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001463 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001464 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001465
Guido van Rossum374a9221991-04-04 10:40:29 +00001466 case PRINT_EXPR:
1467 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001468 w = PySys_GetObject("displayhook");
1469 if (w == NULL) {
1470 PyErr_SetString(PyExc_RuntimeError,
1471 "lost sys.displayhook");
1472 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001473 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001474 }
1475 if (err == 0) {
1476 x = Py_BuildValue("(O)", v);
1477 if (x == NULL)
1478 err = -1;
1479 }
1480 if (err == 0) {
1481 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001482 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001483 if (w == NULL)
1484 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001485 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001486 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001487 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001488 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001489
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001490 case PRINT_ITEM_TO:
1491 w = stream = POP();
1492 /* fall through to PRINT_ITEM */
1493
Guido van Rossum374a9221991-04-04 10:40:29 +00001494 case PRINT_ITEM:
1495 v = POP();
Barry Warsaw093abe02000-08-29 04:56:13 +00001496 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001497 w = PySys_GetObject("stdout");
1498 if (w == NULL) {
1499 PyErr_SetString(PyExc_RuntimeError,
1500 "lost sys.stdout");
1501 err = -1;
1502 }
Guido van Rossum8f183201997-12-31 05:53:15 +00001503 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001504 /* PyFile_SoftSpace() can exececute arbitrary code
1505 if sys.stdout is an instance with a __getattr__.
1506 If __getattr__ raises an exception, w will
1507 be freed, so we need to prevent that temporarily. */
1508 Py_XINCREF(w);
Tim Peters8e5fd532002-03-24 19:25:00 +00001509 if (w != NULL && PyFile_SoftSpace(w, 0))
Guido van Rossumbe270261997-05-22 22:26:18 +00001510 err = PyFile_WriteString(" ", w);
1511 if (err == 0)
1512 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001513 if (err == 0) {
Tim Peters8e5fd532002-03-24 19:25:00 +00001514 /* XXX move into writeobject() ? */
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001515 if (PyString_Check(v)) {
1516 char *s = PyString_AS_STRING(v);
1517 int len = PyString_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001518 if (len == 0 ||
1519 !isspace(Py_CHARMASK(s[len-1])) ||
1520 s[len-1] == ' ')
1521 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001522 }
Martin v. Löwis8d3ce5a2001-12-18 22:36:40 +00001523#ifdef Py_USING_UNICODE
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001524 else if (PyUnicode_Check(v)) {
1525 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
1526 int len = PyUnicode_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001527 if (len == 0 ||
1528 !Py_UNICODE_ISSPACE(s[len-1]) ||
1529 s[len-1] == ' ')
1530 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001531 }
Michael W. Hudsond95c8282002-05-20 13:56:11 +00001532#endif
Tim Peters8e5fd532002-03-24 19:25:00 +00001533 else
1534 PyFile_SoftSpace(w, 1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001535 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001536 Py_XDECREF(w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001537 Py_DECREF(v);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001538 Py_XDECREF(stream);
1539 stream = NULL;
1540 if (err == 0)
1541 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001542 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001543
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001544 case PRINT_NEWLINE_TO:
1545 w = stream = POP();
1546 /* fall through to PRINT_NEWLINE */
1547
Guido van Rossum374a9221991-04-04 10:40:29 +00001548 case PRINT_NEWLINE:
Barry Warsaw093abe02000-08-29 04:56:13 +00001549 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001550 w = PySys_GetObject("stdout");
1551 if (w == NULL)
1552 PyErr_SetString(PyExc_RuntimeError,
1553 "lost sys.stdout");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001554 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001555 if (w != NULL) {
1556 err = PyFile_WriteString("\n", w);
1557 if (err == 0)
1558 PyFile_SoftSpace(w, 0);
1559 }
1560 Py_XDECREF(stream);
1561 stream = NULL;
Guido van Rossum374a9221991-04-04 10:40:29 +00001562 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001563
Thomas Wouters434d0822000-08-24 20:11:32 +00001564
1565#ifdef CASE_TOO_BIG
1566 default: switch (opcode) {
1567#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001568 case BREAK_LOOP:
1569 why = WHY_BREAK;
1570 break;
Guido van Rossum66b0e9c2001-03-21 19:17:22 +00001571
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00001572 case CONTINUE_LOOP:
1573 retval = PyInt_FromLong(oparg);
1574 why = WHY_CONTINUE;
1575 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001576
Guido van Rossumf10570b1995-07-07 22:53:21 +00001577 case RAISE_VARARGS:
1578 u = v = w = NULL;
1579 switch (oparg) {
1580 case 3:
1581 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001582 /* Fallthrough */
1583 case 2:
1584 v = POP(); /* value */
1585 /* Fallthrough */
1586 case 1:
1587 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001588 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001589 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001590 break;
1591 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001592 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001593 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001594 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001595 break;
1596 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001597 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001598
Guido van Rossum374a9221991-04-04 10:40:29 +00001599 case LOAD_LOCALS:
Guido van Rossum681d79a1995-07-18 14:51:37 +00001600 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001601 PyErr_SetString(PyExc_SystemError,
1602 "no locals");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001603 break;
1604 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001605 Py_INCREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001606 PUSH(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001607 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001608
Guido van Rossum374a9221991-04-04 10:40:29 +00001609 case RETURN_VALUE:
1610 retval = POP();
1611 why = WHY_RETURN;
1612 break;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001613
Tim Peters5ca576e2001-06-18 22:08:13 +00001614 case YIELD_VALUE:
1615 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001616 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001617 why = WHY_YIELD;
1618 break;
1619
1620
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001621 case EXEC_STMT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001622 w = TOP();
1623 v = SECOND();
1624 u = THIRD();
1625 STACKADJ(-3);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001626 err = exec_statement(f, u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001627 Py_DECREF(u);
1628 Py_DECREF(v);
1629 Py_DECREF(w);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001630 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001631
Guido van Rossum374a9221991-04-04 10:40:29 +00001632 case POP_BLOCK:
1633 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001634 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001635 while (STACK_LEVEL() > b->b_level) {
1636 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001637 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001638 }
1639 }
1640 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001641
Guido van Rossum374a9221991-04-04 10:40:29 +00001642 case END_FINALLY:
1643 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001644 if (PyInt_Check(v)) {
Raymond Hettinger080cb322003-03-14 01:37:42 +00001645 why = (enum why_code) PyInt_AS_LONG(v);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00001646 if (why == WHY_RETURN ||
Tim Peters5ca576e2001-06-18 22:08:13 +00001647 why == WHY_YIELD ||
Guido van Rossumc5fe5eb2002-06-12 03:45:21 +00001648 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001649 retval = POP();
1650 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001651 else if (PyString_Check(v) || PyClass_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001652 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001653 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001654 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001655 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001656 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001657 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001658 else if (v != Py_None) {
1659 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001660 "'finally' pops bad exception");
1661 why = WHY_EXCEPTION;
1662 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001663 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001664 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001665
Guido van Rossum374a9221991-04-04 10:40:29 +00001666 case BUILD_CLASS:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001667 u = TOP();
1668 v = SECOND();
1669 w = THIRD();
1670 STACKADJ(-2);
Guido van Rossum25831651993-05-19 14:50:45 +00001671 x = build_class(u, v, w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001672 SET_TOP(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001673 Py_DECREF(u);
1674 Py_DECREF(v);
1675 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001676 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001677
Guido van Rossum374a9221991-04-04 10:40:29 +00001678 case STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001679 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001680 v = POP();
Guido van Rossum681d79a1995-07-18 14:51:37 +00001681 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001682 PyErr_Format(PyExc_SystemError,
1683 "no locals found when storing %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001684 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001685 break;
1686 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001687 err = PyDict_SetItem(x, w, v);
1688 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001689 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001690
Guido van Rossum374a9221991-04-04 10:40:29 +00001691 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001692 w = GETITEM(names, oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001693 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001694 PyErr_Format(PyExc_SystemError,
1695 "no locals when deleting %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001696 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001697 break;
1698 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001699 if ((err = PyDict_DelItem(x, w)) != 0)
Guido van Rossumac7be682001-01-17 15:42:30 +00001700 format_exc_check_arg(PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001701 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001702 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001703
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001704 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001705 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001706 v = POP();
Raymond Hettinger21012b82003-02-26 18:11:50 +00001707 if (PyTuple_CheckExact(v)) {
Barry Warsawe42b18f1997-08-25 22:13:04 +00001708 if (PyTuple_Size(v) != oparg) {
1709 PyErr_SetString(PyExc_ValueError,
1710 "unpack tuple of wrong size");
1711 why = WHY_EXCEPTION;
1712 }
1713 else {
1714 for (; --oparg >= 0; ) {
1715 w = PyTuple_GET_ITEM(v, oparg);
1716 Py_INCREF(w);
1717 PUSH(w);
1718 }
1719 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001720 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00001721 else if (PyList_CheckExact(v)) {
Barry Warsawe42b18f1997-08-25 22:13:04 +00001722 if (PyList_Size(v) != oparg) {
1723 PyErr_SetString(PyExc_ValueError,
1724 "unpack list of wrong size");
1725 why = WHY_EXCEPTION;
1726 }
1727 else {
1728 for (; --oparg >= 0; ) {
1729 w = PyList_GET_ITEM(v, oparg);
1730 Py_INCREF(w);
1731 PUSH(w);
1732 }
1733 }
1734 }
Tim Petersd6d010b2001-06-21 02:49:55 +00001735 else if (unpack_iterable(v, oparg,
1736 stack_pointer + oparg))
1737 stack_pointer += oparg;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001738 else {
1739 if (PyErr_ExceptionMatches(PyExc_TypeError))
1740 PyErr_SetString(PyExc_TypeError,
1741 "unpack non-sequence");
Barry Warsawe42b18f1997-08-25 22:13:04 +00001742 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001743 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001744 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001745 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001746
Guido van Rossum374a9221991-04-04 10:40:29 +00001747 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001748 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001749 v = TOP();
1750 u = SECOND();
1751 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001752 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1753 Py_DECREF(v);
1754 Py_DECREF(u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001755 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001756
Guido van Rossum374a9221991-04-04 10:40:29 +00001757 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001758 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001759 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001760 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1761 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001762 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001763 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001764
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001765 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001766 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001767 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001768 err = PyDict_SetItem(f->f_globals, w, v);
1769 Py_DECREF(v);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001770 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001771
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001772 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001773 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001774 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001775 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001776 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001777 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001778
Guido van Rossum374a9221991-04-04 10:40:29 +00001779 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001780 w = GETITEM(names, oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001781 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001782 PyErr_Format(PyExc_SystemError,
1783 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001784 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001785 break;
1786 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001787 x = PyDict_GetItem(x, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001788 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001789 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001790 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001791 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001792 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001793 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001794 PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001795 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001796 break;
1797 }
1798 }
1799 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001800 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001801 PUSH(x);
1802 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001803
Guido van Rossum374a9221991-04-04 10:40:29 +00001804 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001805 w = GETITEM(names, oparg);
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001806 if (PyString_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00001807 /* Inline the PyDict_GetItem() calls.
1808 WARNING: this is an extreme speed hack.
1809 Do not try this at home. */
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001810 long hash = ((PyStringObject *)w)->ob_shash;
1811 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001812 PyDictObject *d;
1813 d = (PyDictObject *)(f->f_globals);
1814 x = d->ma_lookup(d, w, hash)->me_value;
1815 if (x != NULL) {
1816 Py_INCREF(x);
1817 PUSH(x);
1818 continue;
1819 }
1820 d = (PyDictObject *)(f->f_builtins);
1821 x = d->ma_lookup(d, w, hash)->me_value;
1822 if (x != NULL) {
1823 Py_INCREF(x);
1824 PUSH(x);
1825 continue;
1826 }
1827 goto load_global_error;
1828 }
1829 }
1830 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00001831 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001832 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001833 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001834 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001835 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00001836 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001837 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001838 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001839 break;
1840 }
1841 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001842 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001843 PUSH(x);
1844 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001845
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001846 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001847 x = GETLOCAL(oparg);
1848 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001849 format_exc_check_arg(
1850 PyExc_UnboundLocalError,
1851 UNBOUNDLOCAL_ERROR_MSG,
1852 PyTuple_GetItem(co->co_varnames, oparg)
1853 );
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001854 break;
1855 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00001856 SETLOCAL(oparg, NULL);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001857 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001858
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001859 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001860 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001861 Py_INCREF(x);
1862 PUSH(x);
1863 break;
1864
1865 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001866 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001867 w = PyCell_Get(x);
Jeremy Hylton2524d692001-02-05 17:23:16 +00001868 if (w == NULL) {
Jeremy Hylton76c81ee2002-07-11 16:56:38 +00001869 err = -1;
1870 /* Don't stomp existing exception */
1871 if (PyErr_Occurred())
1872 break;
Jeremy Hyltonc76770c2001-04-13 16:51:46 +00001873 if (oparg < f->f_ncells) {
Jeremy Hylton2524d692001-02-05 17:23:16 +00001874 v = PyTuple_GetItem(co->co_cellvars,
1875 oparg);
Jeremy Hyltonc76770c2001-04-13 16:51:46 +00001876 format_exc_check_arg(
1877 PyExc_UnboundLocalError,
1878 UNBOUNDLOCAL_ERROR_MSG,
1879 v);
1880 } else {
Jeremy Hylton2524d692001-02-05 17:23:16 +00001881 v = PyTuple_GetItem(
1882 co->co_freevars,
1883 oparg - f->f_ncells);
Jeremy Hyltonc76770c2001-04-13 16:51:46 +00001884 format_exc_check_arg(
1885 PyExc_NameError,
1886 UNBOUNDFREE_ERROR_MSG,
1887 v);
1888 }
Jeremy Hylton2524d692001-02-05 17:23:16 +00001889 break;
1890 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001891 PUSH(w);
1892 break;
1893
1894 case STORE_DEREF:
1895 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001896 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001897 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00001898 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001899 continue;
1900
Guido van Rossum374a9221991-04-04 10:40:29 +00001901 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001902 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001903 if (x != NULL) {
1904 for (; --oparg >= 0;) {
1905 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001906 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001907 }
1908 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001909 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001910 }
1911 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001912
Guido van Rossum374a9221991-04-04 10:40:29 +00001913 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00001914 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001915 if (x != NULL) {
1916 for (; --oparg >= 0;) {
1917 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00001918 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001919 }
1920 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001921 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001922 }
1923 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001924
Guido van Rossum374a9221991-04-04 10:40:29 +00001925 case BUILD_MAP:
Guido van Rossumb209a111997-04-29 18:18:01 +00001926 x = PyDict_New();
Guido van Rossum374a9221991-04-04 10:40:29 +00001927 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001928 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001929 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001930
Guido van Rossum374a9221991-04-04 10:40:29 +00001931 case LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001932 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001933 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001934 x = PyObject_GetAttr(v, w);
1935 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001936 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001937 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001938 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001939
Guido van Rossum374a9221991-04-04 10:40:29 +00001940 case COMPARE_OP:
1941 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001942 v = TOP();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001943 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001944 /* INLINE: cmp(int, int) */
1945 register long a, b;
1946 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001947 a = PyInt_AS_LONG(v);
1948 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001949 switch (oparg) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00001950 case PyCmp_LT: res = a < b; break;
1951 case PyCmp_LE: res = a <= b; break;
1952 case PyCmp_EQ: res = a == b; break;
1953 case PyCmp_NE: res = a != b; break;
1954 case PyCmp_GT: res = a > b; break;
1955 case PyCmp_GE: res = a >= b; break;
1956 case PyCmp_IS: res = v == w; break;
1957 case PyCmp_IS_NOT: res = v != w; break;
Guido van Rossumc12da691997-07-17 23:12:42 +00001958 default: goto slow_compare;
1959 }
1960 x = res ? Py_True : Py_False;
1961 Py_INCREF(x);
1962 }
1963 else {
1964 slow_compare:
1965 x = cmp_outcome(oparg, v, w);
1966 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001967 Py_DECREF(v);
1968 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001969 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001970 if (x == NULL) break;
1971 PREDICT(JUMP_IF_FALSE);
1972 PREDICT(JUMP_IF_TRUE);
1973 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001974
Guido van Rossum374a9221991-04-04 10:40:29 +00001975 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001976 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001977 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001978 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001979 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00001980 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001981 break;
1982 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001983 u = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001984 w = Py_BuildValue("(OOOO)",
Guido van Rossum681d79a1995-07-18 14:51:37 +00001985 w,
1986 f->f_globals,
Guido van Rossuma027efa1997-05-05 20:56:21 +00001987 f->f_locals == NULL ?
1988 Py_None : f->f_locals,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001989 u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001990 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001991 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001992 u = POP();
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001993 x = NULL;
1994 break;
1995 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001996 x = PyEval_CallObject(x, w);
1997 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001998 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001999 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002000 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002001
Thomas Wouters52152252000-08-17 22:55:00 +00002002 case IMPORT_STAR:
2003 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002004 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002005 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002006 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00002007 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002008 break;
2009 }
Thomas Wouters52152252000-08-17 22:55:00 +00002010 err = import_all_from(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002011 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00002012 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002013 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002014 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002015
Thomas Wouters52152252000-08-17 22:55:00 +00002016 case IMPORT_FROM:
Skip Montanaro496e6582002-08-06 17:47:40 +00002017 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00002018 v = TOP();
2019 x = import_from(v, w);
2020 PUSH(x);
2021 if (x != NULL) continue;
2022 break;
2023
Guido van Rossum374a9221991-04-04 10:40:29 +00002024 case JUMP_FORWARD:
2025 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002026 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002027
Raymond Hettingerf606f872003-03-16 03:11:04 +00002028 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002029 case JUMP_IF_FALSE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002030 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002031 if (w == Py_True) {
2032 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002033 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002034 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002035 if (w == Py_False) {
2036 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002037 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002038 }
2039 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002040 if (err > 0)
2041 err = 0;
2042 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00002043 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002044 else
2045 break;
2046 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002047
Raymond Hettingerf606f872003-03-16 03:11:04 +00002048 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002049 case JUMP_IF_TRUE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002050 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002051 if (w == Py_False) {
2052 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002053 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002054 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002055 if (w == Py_True) {
2056 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002057 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002058 }
2059 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002060 if (err > 0) {
2061 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00002062 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002063 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002064 else if (err == 0)
2065 ;
2066 else
2067 break;
2068 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002069
Guido van Rossum374a9221991-04-04 10:40:29 +00002070 case JUMP_ABSOLUTE:
2071 JUMPTO(oparg);
Neil Schemenauerca2a2f12003-05-30 23:59:44 +00002072 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002073
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002074 case GET_ITER:
2075 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002076 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002077 x = PyObject_GetIter(v);
2078 Py_DECREF(v);
2079 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002080 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002081 PREDICT(FOR_ITER);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002082 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002083 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002084 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002085 break;
2086
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002087 PREDICTED_WITH_ARG(FOR_ITER);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002088 case FOR_ITER:
2089 /* before: [iter]; after: [iter, iter()] *or* [] */
2090 v = TOP();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002091 x = PyIter_Next(v);
2092 if (x != NULL) {
2093 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002094 PREDICT(STORE_FAST);
2095 PREDICT(UNPACK_SEQUENCE);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002096 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002097 }
Tim Petersf4848da2001-05-05 00:14:56 +00002098 if (!PyErr_Occurred()) {
2099 /* iterator ended normally */
2100 x = v = POP();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002101 Py_DECREF(v);
2102 JUMPBY(oparg);
2103 continue;
2104 }
2105 break;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002106
Guido van Rossum374a9221991-04-04 10:40:29 +00002107 case SETUP_LOOP:
2108 case SETUP_EXCEPT:
2109 case SETUP_FINALLY:
Guido van Rossumb209a111997-04-29 18:18:01 +00002110 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002111 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002112 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002113
Guido van Rossumf10570b1995-07-07 22:53:21 +00002114 case CALL_FUNCTION:
Jeremy Hylton985eba52003-02-05 23:13:00 +00002115 PCALL(PCALL_ALL);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002116 x = call_function(&stack_pointer, oparg);
2117 PUSH(x);
2118 if (x != NULL)
2119 continue;
2120 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002121
Jeremy Hylton76901512000-03-28 23:49:17 +00002122 case CALL_FUNCTION_VAR:
2123 case CALL_FUNCTION_KW:
2124 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002125 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002126 int na = oparg & 0xff;
2127 int nk = (oparg>>8) & 0xff;
2128 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002129 int n = na + 2 * nk;
2130 PyObject **pfunc, *func;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002131 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002132 if (flags & CALL_FLAG_VAR)
2133 n++;
2134 if (flags & CALL_FLAG_KW)
2135 n++;
2136 pfunc = stack_pointer - n - 1;
2137 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002138
Guido van Rossumac7be682001-01-17 15:42:30 +00002139 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002140 && PyMethod_GET_SELF(func) != NULL) {
2141 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002142 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002143 func = PyMethod_GET_FUNCTION(func);
2144 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002145 Py_DECREF(*pfunc);
2146 *pfunc = self;
2147 na++;
2148 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002149 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002150 Py_INCREF(func);
2151 x = ext_do_call(func, &stack_pointer, flags, na, nk);
Jeremy Hylton76901512000-03-28 23:49:17 +00002152 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002153
Jeremy Hylton76901512000-03-28 23:49:17 +00002154 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002155 w = POP();
2156 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002157 }
2158 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002159 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002160 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002161 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002162 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002163
Guido van Rossum681d79a1995-07-18 14:51:37 +00002164 case MAKE_FUNCTION:
2165 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002166 x = PyFunction_New(v, f->f_globals);
2167 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002168 /* XXX Maybe this should be a separate opcode? */
2169 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002170 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002171 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002172 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002173 x = NULL;
2174 break;
2175 }
2176 while (--oparg >= 0) {
2177 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002178 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002179 }
2180 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002181 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002182 }
2183 PUSH(x);
2184 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002185
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002186 case MAKE_CLOSURE:
2187 {
2188 int nfree;
2189 v = POP(); /* code object */
2190 x = PyFunction_New(v, f->f_globals);
Jeremy Hylton733c8932001-12-13 19:51:56 +00002191 nfree = PyCode_GetNumFree((PyCodeObject *)v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002192 Py_DECREF(v);
2193 /* XXX Maybe this should be a separate opcode? */
2194 if (x != NULL && nfree > 0) {
2195 v = PyTuple_New(nfree);
2196 if (v == NULL) {
2197 Py_DECREF(x);
2198 x = NULL;
2199 break;
2200 }
2201 while (--nfree >= 0) {
2202 w = POP();
2203 PyTuple_SET_ITEM(v, nfree, w);
2204 }
2205 err = PyFunction_SetClosure(x, v);
2206 Py_DECREF(v);
2207 }
2208 if (x != NULL && oparg > 0) {
2209 v = PyTuple_New(oparg);
2210 if (v == NULL) {
2211 Py_DECREF(x);
2212 x = NULL;
2213 break;
2214 }
2215 while (--oparg >= 0) {
2216 w = POP();
2217 PyTuple_SET_ITEM(v, oparg, w);
2218 }
2219 err = PyFunction_SetDefaults(x, v);
2220 Py_DECREF(v);
2221 }
2222 PUSH(x);
2223 break;
2224 }
2225
Guido van Rossum8861b741996-07-30 16:49:37 +00002226 case BUILD_SLICE:
2227 if (oparg == 3)
2228 w = POP();
2229 else
2230 w = NULL;
2231 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002232 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002233 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002234 Py_DECREF(u);
2235 Py_DECREF(v);
2236 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002237 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002238 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002239 break;
2240
Fred Drakeef8ace32000-08-24 00:32:09 +00002241 case EXTENDED_ARG:
2242 opcode = NEXTOP();
2243 oparg = oparg<<16 | NEXTARG();
2244 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002245
Guido van Rossum374a9221991-04-04 10:40:29 +00002246 default:
2247 fprintf(stderr,
2248 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002249 PyCode_Addr2Line(f->f_code, f->f_lasti),
2250 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002251 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002252 why = WHY_EXCEPTION;
2253 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002254
2255#ifdef CASE_TOO_BIG
2256 }
2257#endif
2258
Guido van Rossum374a9221991-04-04 10:40:29 +00002259 } /* switch */
2260
2261 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002262
Guido van Rossum374a9221991-04-04 10:40:29 +00002263 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002264
Guido van Rossum374a9221991-04-04 10:40:29 +00002265 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002266 if (err == 0 && x != NULL) {
2267#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002268 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002269 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002270 fprintf(stderr,
2271 "XXX undetected error\n");
2272 else
2273#endif
2274 continue; /* Normal, fast path */
2275 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002276 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002277 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002278 err = 0;
2279 }
2280
Guido van Rossum374a9221991-04-04 10:40:29 +00002281 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002282
Guido van Rossum374a9221991-04-04 10:40:29 +00002283 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002284 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002285 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002286 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002287 why = WHY_EXCEPTION;
2288 }
2289 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002290#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002291 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002292 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002293 if (PyErr_Occurred()) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002294 fprintf(stderr,
2295 "XXX undetected error (why=%d)\n",
2296 why);
2297 why = WHY_EXCEPTION;
2298 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002299 }
2300#endif
2301
2302 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002303
Guido van Rossum374a9221991-04-04 10:40:29 +00002304 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002305 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002306
Fred Drake8f51f542001-10-04 14:48:42 +00002307 if (tstate->c_tracefunc != NULL)
2308 call_exc_trace(tstate->c_tracefunc,
2309 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002310 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002311
Guido van Rossum374a9221991-04-04 10:40:29 +00002312 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002313
Guido van Rossum374a9221991-04-04 10:40:29 +00002314 if (why == WHY_RERAISE)
2315 why = WHY_EXCEPTION;
2316
2317 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002318
Tim Peters5ca576e2001-06-18 22:08:13 +00002319 while (why != WHY_NOT && why != WHY_YIELD && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002320 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002321
2322 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2323 /* For a continue inside a try block,
2324 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002325 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2326 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002327 why = WHY_NOT;
2328 JUMPTO(PyInt_AS_LONG(retval));
2329 Py_DECREF(retval);
2330 break;
2331 }
2332
Guido van Rossum374a9221991-04-04 10:40:29 +00002333 while (STACK_LEVEL() > b->b_level) {
2334 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002335 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002336 }
2337 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2338 why = WHY_NOT;
2339 JUMPTO(b->b_handler);
2340 break;
2341 }
2342 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002343 (b->b_type == SETUP_EXCEPT &&
2344 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002345 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002346 PyObject *exc, *val, *tb;
2347 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002348 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002349 val = Py_None;
2350 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002351 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002352 /* Make the raw exception data
2353 available to the handler,
2354 so a program can emulate the
2355 Python main loop. Don't do
2356 this for 'finally'. */
2357 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002358 PyErr_NormalizeException(
2359 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002360 set_exc_info(tstate,
2361 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002362 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002363 if (tb == NULL) {
2364 Py_INCREF(Py_None);
2365 PUSH(Py_None);
2366 } else
2367 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002368 PUSH(val);
2369 PUSH(exc);
2370 }
2371 else {
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002372 if (why == WHY_RETURN ||
Guido van Rossumc5fe5eb2002-06-12 03:45:21 +00002373 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00002374 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002375 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002376 PUSH(v);
2377 }
2378 why = WHY_NOT;
2379 JUMPTO(b->b_handler);
2380 break;
2381 }
2382 } /* unwind stack */
2383
2384 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002385
Guido van Rossum374a9221991-04-04 10:40:29 +00002386 if (why != WHY_NOT)
2387 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002388
Guido van Rossum374a9221991-04-04 10:40:29 +00002389 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002390
Guido van Rossum35974fb2001-12-06 21:28:18 +00002391 if (why != WHY_YIELD) {
2392 /* Pop remaining stack entries -- but when yielding */
2393 while (!EMPTY()) {
2394 v = POP();
2395 Py_XDECREF(v);
2396 }
2397 }
2398
Tim Peters5ca576e2001-06-18 22:08:13 +00002399 if (why != WHY_RETURN && why != WHY_YIELD)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002400 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002401
Fred Drake9e3ad782001-07-03 23:39:52 +00002402 if (tstate->use_tracing) {
2403 if (tstate->c_tracefunc
2404 && (why == WHY_RETURN || why == WHY_YIELD)) {
2405 if (call_trace(tstate->c_tracefunc,
2406 tstate->c_traceobj, f,
2407 PyTrace_RETURN, retval)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002408 Py_XDECREF(retval);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002409 retval = NULL;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002410 why = WHY_EXCEPTION;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002411 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002412 }
Fred Drake8f51f542001-10-04 14:48:42 +00002413 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002414 if (why == WHY_EXCEPTION)
2415 call_trace_protected(tstate->c_profilefunc,
2416 tstate->c_profileobj, f,
2417 PyTrace_RETURN);
2418 else if (call_trace(tstate->c_profilefunc,
2419 tstate->c_profileobj, f,
2420 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002421 Py_XDECREF(retval);
2422 retval = NULL;
2423 why = WHY_EXCEPTION;
2424 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002425 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002426 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002427
Guido van Rossuma027efa1997-05-05 20:56:21 +00002428 reset_exc_info(tstate);
2429
Tim Peters5ca576e2001-06-18 22:08:13 +00002430 /* pop frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002431 --tstate->recursion_depth;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002432 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002433
Guido van Rossum96a42c81992-01-12 02:29:51 +00002434 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002435}
2436
Tim Peters6d6c1a32001-08-02 04:15:00 +00002437PyObject *
2438PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002439 PyObject **args, int argcount, PyObject **kws, int kwcount,
2440 PyObject **defs, int defcount, PyObject *closure)
2441{
2442 register PyFrameObject *f;
2443 register PyObject *retval = NULL;
2444 register PyObject **fastlocals, **freevars;
2445 PyThreadState *tstate = PyThreadState_GET();
2446 PyObject *x, *u;
2447
2448 if (globals == NULL) {
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002449 PyErr_SetString(PyExc_SystemError,
2450 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002451 return NULL;
2452 }
2453
Jeremy Hylton985eba52003-02-05 23:13:00 +00002454 assert(globals != NULL);
2455 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002456 if (f == NULL)
2457 return NULL;
2458
2459 fastlocals = f->f_localsplus;
2460 freevars = f->f_localsplus + f->f_nlocals;
2461
2462 if (co->co_argcount > 0 ||
2463 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2464 int i;
2465 int n = argcount;
2466 PyObject *kwdict = NULL;
2467 if (co->co_flags & CO_VARKEYWORDS) {
2468 kwdict = PyDict_New();
2469 if (kwdict == NULL)
2470 goto fail;
2471 i = co->co_argcount;
2472 if (co->co_flags & CO_VARARGS)
2473 i++;
2474 SETLOCAL(i, kwdict);
2475 }
2476 if (argcount > co->co_argcount) {
2477 if (!(co->co_flags & CO_VARARGS)) {
2478 PyErr_Format(PyExc_TypeError,
2479 "%.200s() takes %s %d "
2480 "%sargument%s (%d given)",
2481 PyString_AsString(co->co_name),
2482 defcount ? "at most" : "exactly",
2483 co->co_argcount,
2484 kwcount ? "non-keyword " : "",
2485 co->co_argcount == 1 ? "" : "s",
2486 argcount);
2487 goto fail;
2488 }
2489 n = co->co_argcount;
2490 }
2491 for (i = 0; i < n; i++) {
2492 x = args[i];
2493 Py_INCREF(x);
2494 SETLOCAL(i, x);
2495 }
2496 if (co->co_flags & CO_VARARGS) {
2497 u = PyTuple_New(argcount - n);
2498 if (u == NULL)
2499 goto fail;
2500 SETLOCAL(co->co_argcount, u);
2501 for (i = n; i < argcount; i++) {
2502 x = args[i];
2503 Py_INCREF(x);
2504 PyTuple_SET_ITEM(u, i-n, x);
2505 }
2506 }
2507 for (i = 0; i < kwcount; i++) {
2508 PyObject *keyword = kws[2*i];
2509 PyObject *value = kws[2*i + 1];
2510 int j;
2511 if (keyword == NULL || !PyString_Check(keyword)) {
2512 PyErr_Format(PyExc_TypeError,
2513 "%.200s() keywords must be strings",
2514 PyString_AsString(co->co_name));
2515 goto fail;
2516 }
2517 /* XXX slow -- speed up using dictionary? */
2518 for (j = 0; j < co->co_argcount; j++) {
2519 PyObject *nm = PyTuple_GET_ITEM(
2520 co->co_varnames, j);
2521 int cmp = PyObject_RichCompareBool(
2522 keyword, nm, Py_EQ);
2523 if (cmp > 0)
2524 break;
2525 else if (cmp < 0)
2526 goto fail;
2527 }
2528 /* Check errors from Compare */
2529 if (PyErr_Occurred())
2530 goto fail;
2531 if (j >= co->co_argcount) {
2532 if (kwdict == NULL) {
2533 PyErr_Format(PyExc_TypeError,
2534 "%.200s() got an unexpected "
2535 "keyword argument '%.400s'",
2536 PyString_AsString(co->co_name),
2537 PyString_AsString(keyword));
2538 goto fail;
2539 }
2540 PyDict_SetItem(kwdict, keyword, value);
2541 }
2542 else {
2543 if (GETLOCAL(j) != NULL) {
2544 PyErr_Format(PyExc_TypeError,
2545 "%.200s() got multiple "
2546 "values for keyword "
2547 "argument '%.400s'",
2548 PyString_AsString(co->co_name),
2549 PyString_AsString(keyword));
2550 goto fail;
2551 }
2552 Py_INCREF(value);
2553 SETLOCAL(j, value);
2554 }
2555 }
2556 if (argcount < co->co_argcount) {
2557 int m = co->co_argcount - defcount;
2558 for (i = argcount; i < m; i++) {
2559 if (GETLOCAL(i) == NULL) {
2560 PyErr_Format(PyExc_TypeError,
2561 "%.200s() takes %s %d "
2562 "%sargument%s (%d given)",
2563 PyString_AsString(co->co_name),
2564 ((co->co_flags & CO_VARARGS) ||
2565 defcount) ? "at least"
2566 : "exactly",
2567 m, kwcount ? "non-keyword " : "",
2568 m == 1 ? "" : "s", i);
2569 goto fail;
2570 }
2571 }
2572 if (n > m)
2573 i = n - m;
2574 else
2575 i = 0;
2576 for (; i < defcount; i++) {
2577 if (GETLOCAL(m+i) == NULL) {
2578 PyObject *def = defs[i];
2579 Py_INCREF(def);
2580 SETLOCAL(m+i, def);
2581 }
2582 }
2583 }
2584 }
2585 else {
2586 if (argcount > 0 || kwcount > 0) {
2587 PyErr_Format(PyExc_TypeError,
2588 "%.200s() takes no arguments (%d given)",
2589 PyString_AsString(co->co_name),
2590 argcount + kwcount);
2591 goto fail;
2592 }
2593 }
2594 /* Allocate and initialize storage for cell vars, and copy free
2595 vars into frame. This isn't too efficient right now. */
2596 if (f->f_ncells) {
2597 int i = 0, j = 0, nargs, found;
2598 char *cellname, *argname;
2599 PyObject *c;
2600
2601 nargs = co->co_argcount;
2602 if (co->co_flags & CO_VARARGS)
2603 nargs++;
2604 if (co->co_flags & CO_VARKEYWORDS)
2605 nargs++;
2606
2607 /* Check for cells that shadow args */
2608 for (i = 0; i < f->f_ncells && j < nargs; ++i) {
2609 cellname = PyString_AS_STRING(
2610 PyTuple_GET_ITEM(co->co_cellvars, i));
2611 found = 0;
2612 while (j < nargs) {
2613 argname = PyString_AS_STRING(
2614 PyTuple_GET_ITEM(co->co_varnames, j));
2615 if (strcmp(cellname, argname) == 0) {
2616 c = PyCell_New(GETLOCAL(j));
2617 if (c == NULL)
2618 goto fail;
2619 GETLOCAL(f->f_nlocals + i) = c;
2620 found = 1;
2621 break;
2622 }
2623 j++;
2624 }
2625 if (found == 0) {
2626 c = PyCell_New(NULL);
2627 if (c == NULL)
2628 goto fail;
2629 SETLOCAL(f->f_nlocals + i, c);
2630 }
2631 }
2632 /* Initialize any that are left */
2633 while (i < f->f_ncells) {
2634 c = PyCell_New(NULL);
2635 if (c == NULL)
2636 goto fail;
2637 SETLOCAL(f->f_nlocals + i, c);
2638 i++;
2639 }
2640 }
2641 if (f->f_nfreevars) {
2642 int i;
2643 for (i = 0; i < f->f_nfreevars; ++i) {
2644 PyObject *o = PyTuple_GET_ITEM(closure, i);
2645 Py_INCREF(o);
2646 freevars[f->f_ncells + i] = o;
2647 }
2648 }
2649
Tim Peters5ca576e2001-06-18 22:08:13 +00002650 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002651 /* Don't need to keep the reference to f_back, it will be set
2652 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002653 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002654 f->f_back = NULL;
2655
Jeremy Hylton985eba52003-02-05 23:13:00 +00002656 PCALL(PCALL_GENERATOR);
2657
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002658 /* Create a new generator that owns the ready to run frame
2659 * and return that as the value. */
Tim Peters5ca576e2001-06-18 22:08:13 +00002660 return gen_new(f);
2661 }
2662
2663 retval = eval_frame(f);
2664
2665 fail: /* Jump here from prelude on failure */
2666
Tim Petersb13680b2001-11-27 23:29:29 +00002667 /* decref'ing the frame can cause __del__ methods to get invoked,
2668 which can call back into Python. While we're done with the
2669 current Python frame (f), the associated C stack is still in use,
2670 so recursion_depth must be boosted for the duration.
2671 */
2672 assert(tstate != NULL);
2673 ++tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002674 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00002675 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002676 return retval;
2677}
2678
2679
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002680/* Implementation notes for set_exc_info() and reset_exc_info():
2681
2682- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2683 'exc_traceback'. These always travel together.
2684
2685- tstate->curexc_ZZZ is the "hot" exception that is set by
2686 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2687
2688- Once an exception is caught by an except clause, it is transferred
2689 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2690 can pick it up. This is the primary task of set_exc_info().
2691
2692- Now let me explain the complicated dance with frame->f_exc_ZZZ.
2693
2694 Long ago, when none of this existed, there were just a few globals:
2695 one set corresponding to the "hot" exception, and one set
2696 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2697 globals; they were simply stored as sys.exc_ZZZ. For backwards
2698 compatibility, they still are!) The problem was that in code like
2699 this:
2700
2701 try:
2702 "something that may fail"
2703 except "some exception":
2704 "do something else first"
2705 "print the exception from sys.exc_ZZZ."
2706
2707 if "do something else first" invoked something that raised and caught
2708 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2709 cause of subtle bugs. I fixed this by changing the semantics as
2710 follows:
2711
2712 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2713 *in that frame*.
2714
2715 - But initially, and as long as no exception is caught in a given
2716 frame, sys.exc_ZZZ will hold the last exception caught in the
2717 previous frame (or the frame before that, etc.).
2718
2719 The first bullet fixed the bug in the above example. The second
2720 bullet was for backwards compatibility: it was (and is) common to
2721 have a function that is called when an exception is caught, and to
2722 have that function access the caught exception via sys.exc_ZZZ.
2723 (Example: traceback.print_exc()).
2724
2725 At the same time I fixed the problem that sys.exc_ZZZ weren't
2726 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2727 but that's really a separate improvement.
2728
2729 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2730 variables to what they were before the current frame was called. The
2731 set_exc_info() function saves them on the frame so that
2732 reset_exc_info() can restore them. The invariant is that
2733 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2734 exception (where "catching" an exception applies only to successful
2735 except clauses); and if the current frame ever caught an exception,
2736 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2737 at the start of the current frame.
2738
2739*/
2740
Guido van Rossuma027efa1997-05-05 20:56:21 +00002741static void
Guido van Rossumac7be682001-01-17 15:42:30 +00002742set_exc_info(PyThreadState *tstate,
2743 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002744{
2745 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002746 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00002747
Guido van Rossuma027efa1997-05-05 20:56:21 +00002748 frame = tstate->frame;
2749 if (frame->f_exc_type == NULL) {
2750 /* This frame didn't catch an exception before */
2751 /* Save previous exception of this thread in this frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002752 if (tstate->exc_type == NULL) {
2753 Py_INCREF(Py_None);
2754 tstate->exc_type = Py_None;
2755 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002756 tmp_type = frame->f_exc_type;
2757 tmp_value = frame->f_exc_value;
2758 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002759 Py_XINCREF(tstate->exc_type);
2760 Py_XINCREF(tstate->exc_value);
2761 Py_XINCREF(tstate->exc_traceback);
2762 frame->f_exc_type = tstate->exc_type;
2763 frame->f_exc_value = tstate->exc_value;
2764 frame->f_exc_traceback = tstate->exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002765 Py_XDECREF(tmp_type);
2766 Py_XDECREF(tmp_value);
2767 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002768 }
2769 /* Set new exception for this thread */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002770 tmp_type = tstate->exc_type;
2771 tmp_value = tstate->exc_value;
2772 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002773 Py_XINCREF(type);
2774 Py_XINCREF(value);
2775 Py_XINCREF(tb);
2776 tstate->exc_type = type;
2777 tstate->exc_value = value;
2778 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002779 Py_XDECREF(tmp_type);
2780 Py_XDECREF(tmp_value);
2781 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002782 /* For b/w compatibility */
2783 PySys_SetObject("exc_type", type);
2784 PySys_SetObject("exc_value", value);
2785 PySys_SetObject("exc_traceback", tb);
2786}
2787
2788static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002789reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002790{
2791 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002792 PyObject *tmp_type, *tmp_value, *tmp_tb;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002793 frame = tstate->frame;
2794 if (frame->f_exc_type != NULL) {
2795 /* This frame caught an exception */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002796 tmp_type = tstate->exc_type;
2797 tmp_value = tstate->exc_value;
2798 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002799 Py_XINCREF(frame->f_exc_type);
2800 Py_XINCREF(frame->f_exc_value);
2801 Py_XINCREF(frame->f_exc_traceback);
2802 tstate->exc_type = frame->f_exc_type;
2803 tstate->exc_value = frame->f_exc_value;
2804 tstate->exc_traceback = frame->f_exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002805 Py_XDECREF(tmp_type);
2806 Py_XDECREF(tmp_value);
2807 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002808 /* For b/w compatibility */
2809 PySys_SetObject("exc_type", frame->f_exc_type);
2810 PySys_SetObject("exc_value", frame->f_exc_value);
2811 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
2812 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002813 tmp_type = frame->f_exc_type;
2814 tmp_value = frame->f_exc_value;
2815 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002816 frame->f_exc_type = NULL;
2817 frame->f_exc_value = NULL;
2818 frame->f_exc_traceback = NULL;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002819 Py_XDECREF(tmp_type);
2820 Py_XDECREF(tmp_value);
2821 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002822}
2823
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002824/* Logic for the raise statement (too complicated for inlining).
2825 This *consumes* a reference count to each of its arguments. */
Guido van Rossum1aa14831997-01-21 05:34:20 +00002826static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002827do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002828{
Guido van Rossumd295f121998-04-09 21:39:57 +00002829 if (type == NULL) {
2830 /* Reraise */
2831 PyThreadState *tstate = PyThreadState_Get();
2832 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
2833 value = tstate->exc_value;
2834 tb = tstate->exc_traceback;
2835 Py_XINCREF(type);
2836 Py_XINCREF(value);
2837 Py_XINCREF(tb);
2838 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002839
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002840 /* We support the following forms of raise:
2841 raise <class>, <classinstance>
2842 raise <class>, <argument tuple>
2843 raise <class>, None
2844 raise <class>, <argument>
2845 raise <classinstance>, None
2846 raise <string>, <object>
2847 raise <string>, None
2848
2849 An omitted second argument is the same as None.
2850
2851 In addition, raise <tuple>, <anything> is the same as
2852 raising the tuple's first item (and it better have one!);
2853 this rule is applied recursively.
2854
2855 Finally, an optional third argument can be supplied, which
2856 gives the traceback to be substituted (useful when
2857 re-raising an exception after examining it). */
2858
2859 /* First, check the traceback argument, replacing None with
2860 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002861 if (tb == Py_None) {
2862 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002863 tb = NULL;
2864 }
2865 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002866 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002867 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002868 goto raise_error;
2869 }
2870
2871 /* Next, replace a missing value with None */
2872 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002873 value = Py_None;
2874 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002875 }
2876
2877 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00002878 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
2879 PyObject *tmp = type;
2880 type = PyTuple_GET_ITEM(type, 0);
2881 Py_INCREF(type);
2882 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002883 }
2884
Tim Petersafb2c802002-04-18 18:06:20 +00002885 if (PyString_CheckExact(type))
2886 /* Raising builtin string is deprecated but still allowed --
2887 * do nothing. Raising an instance of a new-style str
2888 * subclass is right out. */
Neal Norwitz37aa0662003-01-10 15:31:15 +00002889 PyErr_Warn(PyExc_PendingDeprecationWarning,
2890 "raising a string exception is deprecated");
Barry Warsaw4249f541997-08-22 21:26:19 +00002891
2892 else if (PyClass_Check(type))
2893 PyErr_NormalizeException(&type, &value, &tb);
2894
Guido van Rossumb209a111997-04-29 18:18:01 +00002895 else if (PyInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002896 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002897 if (value != Py_None) {
2898 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002899 "instance exception may not have a separate value");
2900 goto raise_error;
2901 }
2902 else {
2903 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00002904 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002905 value = type;
Guido van Rossumb209a111997-04-29 18:18:01 +00002906 type = (PyObject*) ((PyInstanceObject*)type)->in_class;
2907 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002908 }
2909 }
2910 else {
2911 /* Not something you can raise. You get an exception
2912 anyway, just not what you specified :-) */
Jeremy Hylton960d9482001-04-27 02:25:33 +00002913 PyErr_Format(PyExc_TypeError,
Neal Norwitz37aa0662003-01-10 15:31:15 +00002914 "exceptions must be classes, instances, or "
2915 "strings (deprecated), not %s",
2916 type->ob_type->tp_name);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002917 goto raise_error;
2918 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002919 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002920 if (tb == NULL)
2921 return WHY_EXCEPTION;
2922 else
2923 return WHY_RERAISE;
2924 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00002925 Py_XDECREF(value);
2926 Py_XDECREF(type);
2927 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002928 return WHY_EXCEPTION;
2929}
2930
Tim Petersd6d010b2001-06-21 02:49:55 +00002931/* Iterate v argcnt times and store the results on the stack (via decreasing
2932 sp). Return 1 for success, 0 if error. */
2933
Barry Warsawe42b18f1997-08-25 22:13:04 +00002934static int
Tim Petersd6d010b2001-06-21 02:49:55 +00002935unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00002936{
Tim Petersd6d010b2001-06-21 02:49:55 +00002937 int i = 0;
2938 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00002939 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00002940
Tim Petersd6d010b2001-06-21 02:49:55 +00002941 assert(v != NULL);
2942
2943 it = PyObject_GetIter(v);
2944 if (it == NULL)
2945 goto Error;
2946
2947 for (; i < argcnt; i++) {
2948 w = PyIter_Next(it);
2949 if (w == NULL) {
2950 /* Iterator done, via error or exhaustion. */
2951 if (!PyErr_Occurred()) {
2952 PyErr_Format(PyExc_ValueError,
2953 "need more than %d value%s to unpack",
2954 i, i == 1 ? "" : "s");
2955 }
2956 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00002957 }
2958 *--sp = w;
2959 }
Tim Petersd6d010b2001-06-21 02:49:55 +00002960
2961 /* We better have exhausted the iterator now. */
2962 w = PyIter_Next(it);
2963 if (w == NULL) {
2964 if (PyErr_Occurred())
2965 goto Error;
2966 Py_DECREF(it);
2967 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00002968 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00002969 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00002970 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00002971 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00002972Error:
Barry Warsaw91010551997-08-25 22:30:51 +00002973 for (; i > 0; i--, sp++)
2974 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00002975 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00002976 return 0;
2977}
2978
2979
Guido van Rossum96a42c81992-01-12 02:29:51 +00002980#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00002981static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002982prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002983{
Guido van Rossum3f5da241990-12-20 15:06:42 +00002984 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00002985 if (PyObject_Print(v, stdout, 0) != 0)
2986 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00002987 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00002988 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002989}
Guido van Rossum3f5da241990-12-20 15:06:42 +00002990#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002991
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002992static void
Fred Drake5755ce62001-06-27 19:19:46 +00002993call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002994{
Guido van Rossumb209a111997-04-29 18:18:01 +00002995 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002996 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00002997 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00002998 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002999 value = Py_None;
3000 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003001 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003002 arg = Py_BuildValue("(OOO)", type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003003 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003004 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003005 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003006 }
Fred Drake5755ce62001-06-27 19:19:46 +00003007 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003008 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003009 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003010 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003011 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003012 Py_XDECREF(type);
3013 Py_XDECREF(value);
3014 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003015 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003016}
3017
Fred Drake4ec5d562001-10-04 19:26:43 +00003018static void
3019call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3020 int what)
3021{
3022 PyObject *type, *value, *traceback;
3023 int err;
3024 PyErr_Fetch(&type, &value, &traceback);
3025 err = call_trace(func, obj, frame, what, NULL);
3026 if (err == 0)
3027 PyErr_Restore(type, value, traceback);
3028 else {
3029 Py_XDECREF(type);
3030 Py_XDECREF(value);
3031 Py_XDECREF(traceback);
3032 }
3033}
3034
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003035static int
Fred Drake5755ce62001-06-27 19:19:46 +00003036call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3037 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003038{
Fred Drake5755ce62001-06-27 19:19:46 +00003039 register PyThreadState *tstate = frame->f_tstate;
3040 int result;
3041 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003042 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003043 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003044 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003045 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003046 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3047 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003048 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003049 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003050}
3051
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003052PyObject *
3053_PyEval_CallTracing(PyObject *func, PyObject *args)
3054{
3055 PyFrameObject *frame = PyEval_GetFrame();
3056 PyThreadState *tstate = frame->f_tstate;
3057 int save_tracing = tstate->tracing;
3058 int save_use_tracing = tstate->use_tracing;
3059 PyObject *result;
3060
3061 tstate->tracing = 0;
3062 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3063 || (tstate->c_profilefunc != NULL));
3064 result = PyObject_Call(func, args, NULL);
3065 tstate->tracing = save_tracing;
3066 tstate->use_tracing = save_use_tracing;
3067 return result;
3068}
3069
Michael W. Hudson006c7522002-11-08 13:08:46 +00003070static int
Michael W. Hudson019a78e2002-11-08 12:53:11 +00003071maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003072 PyFrameObject *frame, int *instr_lb, int *instr_ub)
3073{
3074 /* The theory of SET_LINENO-less tracing.
3075
3076 In a nutshell, we use the co_lnotab field of the code object
3077 to tell when execution has moved onto a different line.
3078
3079 As mentioned above, the basic idea is so set things up so
3080 that
3081
3082 *instr_lb <= frame->f_lasti < *instr_ub
3083
3084 is true so long as execution does not change lines.
3085
3086 This is all fairly simple. Digging the information out of
3087 co_lnotab takes some work, but is conceptually clear.
3088
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003089 Somewhat harder to explain is why we don't *always* call the
3090 line trace function when the above test fails.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003091
3092 Consider this code:
3093
3094 1: def f(a):
3095 2: if a:
3096 3: print 1
3097 4: else:
3098 5: print 2
3099
3100 which compiles to this:
3101
3102 2 0 LOAD_FAST 0 (a)
3103 3 JUMP_IF_FALSE 9 (to 15)
3104 6 POP_TOP
3105
3106 3 7 LOAD_CONST 1 (1)
3107 10 PRINT_ITEM
3108 11 PRINT_NEWLINE
3109 12 JUMP_FORWARD 6 (to 21)
3110 >> 15 POP_TOP
3111
3112 5 16 LOAD_CONST 2 (2)
3113 19 PRINT_ITEM
3114 20 PRINT_NEWLINE
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003115 >> 21 LOAD_CONST 0 (None)
3116 24 RETURN_VALUE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003117
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003118 If 'a' is false, execution will jump to instruction at offset
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003119 15 and the co_lnotab will claim that execution has moved to
3120 line 3. This is at best misleading. In this case we could
3121 associate the POP_TOP with line 4, but that doesn't make
3122 sense in all cases (I think).
3123
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003124 What we do is only call the line trace function if the co_lnotab
3125 indicates we have jumped to the *start* of a line, i.e. if the
3126 current instruction offset matches the offset given for the
3127 start of a line by the co_lnotab.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003128
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003129 This also takes care of the situation where 'a' is true.
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003130 Execution will jump from instruction offset 12 to offset 21.
3131 Then the co_lnotab would imply that execution has moved to line
3132 5, which is again misleading.
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003133
3134 Why do we set f_lineno when tracing? Well, consider the code
3135 above when 'a' is true. If stepping through this with 'n' in
3136 pdb, you would stop at line 1 with a "call" type event, then
3137 line events on lines 2 and 3, then a "return" type event -- but
3138 you would be shown line 5 during this event. This is a change
3139 from the behaviour in 2.2 and before, and I've found it
3140 confusing in practice. By setting and using f_lineno when
3141 tracing, one can report a line number different from that
3142 suggested by f_lasti on this one occasion where it's desirable.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003143 */
3144
Michael W. Hudson006c7522002-11-08 13:08:46 +00003145 int result = 0;
3146
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003147 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003148 PyCodeObject* co = frame->f_code;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003149 int size, addr, line;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003150 unsigned char* p;
3151
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003152 size = PyString_GET_SIZE(co->co_lnotab) / 2;
3153 p = (unsigned char*)PyString_AS_STRING(co->co_lnotab);
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003154
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003155 addr = 0;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003156 line = co->co_firstlineno;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003157
3158 /* possible optimization: if f->f_lasti == instr_ub
3159 (likely to be a common case) then we already know
3160 instr_lb -- if we stored the matching value of p
3161 somwhere we could skip the first while loop. */
3162
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003163 /* see comments in compile.c for the description of
3164 co_lnotab. A point to remember: increments to p
3165 should come in pairs -- although we don't care about
3166 the line increments here, treating them as byte
3167 increments gets confusing, to say the least. */
3168
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003169 while (size > 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003170 if (addr + *p > frame->f_lasti)
3171 break;
3172 addr += *p++;
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003173 if (*p) *instr_lb = addr;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003174 line += *p++;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003175 --size;
3176 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003177
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003178 if (addr == frame->f_lasti) {
3179 frame->f_lineno = line;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003180 result = call_trace(func, obj, frame,
3181 PyTrace_LINE, Py_None);
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003182 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003183
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003184 if (size > 0) {
3185 while (--size >= 0) {
3186 addr += *p++;
3187 if (*p++)
3188 break;
3189 }
3190 *instr_ub = addr;
3191 }
3192 else {
3193 *instr_ub = INT_MAX;
3194 }
3195 }
Michael W. Hudson006c7522002-11-08 13:08:46 +00003196
3197 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003198}
3199
Fred Drake5755ce62001-06-27 19:19:46 +00003200void
3201PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003202{
Fred Drake5755ce62001-06-27 19:19:46 +00003203 PyThreadState *tstate = PyThreadState_Get();
3204 PyObject *temp = tstate->c_profileobj;
3205 Py_XINCREF(arg);
3206 tstate->c_profilefunc = NULL;
3207 tstate->c_profileobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003208 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003209 Py_XDECREF(temp);
3210 tstate->c_profilefunc = func;
3211 tstate->c_profileobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003212 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003213}
3214
3215void
3216PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3217{
3218 PyThreadState *tstate = PyThreadState_Get();
3219 PyObject *temp = tstate->c_traceobj;
3220 Py_XINCREF(arg);
3221 tstate->c_tracefunc = NULL;
3222 tstate->c_traceobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003223 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003224 Py_XDECREF(temp);
3225 tstate->c_tracefunc = func;
3226 tstate->c_traceobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003227 tstate->use_tracing = ((func != NULL)
3228 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003229}
3230
Guido van Rossumb209a111997-04-29 18:18:01 +00003231PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003232PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003233{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003234 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003235 if (current_frame == NULL)
Michael W. Hudson019a78e2002-11-08 12:53:11 +00003236 return PyThreadState_Get()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003237 else
3238 return current_frame->f_builtins;
3239}
3240
Guido van Rossumb209a111997-04-29 18:18:01 +00003241PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003242PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003243{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003244 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003245 if (current_frame == NULL)
3246 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003247 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003248 return current_frame->f_locals;
3249}
3250
Guido van Rossumb209a111997-04-29 18:18:01 +00003251PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003252PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003253{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003254 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003255 if (current_frame == NULL)
3256 return NULL;
3257 else
3258 return current_frame->f_globals;
3259}
3260
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003261PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003262PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003263{
Michael W. Hudson019a78e2002-11-08 12:53:11 +00003264 PyThreadState *tstate = PyThreadState_Get();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003265 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003266}
3267
Guido van Rossum6135a871995-01-09 17:53:26 +00003268int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003269PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003270{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003271 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003272 return current_frame == NULL ? 0 : current_frame->f_restricted;
3273}
3274
Guido van Rossumbe270261997-05-22 22:26:18 +00003275int
Tim Peters5ba58662001-07-16 02:29:45 +00003276PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003277{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003278 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003279 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003280
3281 if (current_frame != NULL) {
3282 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003283 const int compilerflags = codeflags & PyCF_MASK;
3284 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003285 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003286 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003287 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003288#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003289 if (codeflags & CO_GENERATOR_ALLOWED) {
3290 result = 1;
3291 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3292 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003293#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003294 }
3295 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003296}
3297
3298int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003299Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003300{
Guido van Rossumb209a111997-04-29 18:18:01 +00003301 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003302 if (f == NULL)
3303 return 0;
3304 if (!PyFile_SoftSpace(f, 0))
3305 return 0;
3306 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003307}
3308
Guido van Rossum3f5da241990-12-20 15:06:42 +00003309
Guido van Rossum681d79a1995-07-18 14:51:37 +00003310/* External interface to call any callable object.
3311 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003312
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003313#undef PyEval_CallObject
3314/* for backward compatibility: export this interface */
3315
Guido van Rossumb209a111997-04-29 18:18:01 +00003316PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003317PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003318{
Guido van Rossumb209a111997-04-29 18:18:01 +00003319 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003320}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003321#define PyEval_CallObject(func,arg) \
3322 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003323
Guido van Rossumb209a111997-04-29 18:18:01 +00003324PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003325PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003326{
Jeremy Hylton52820442001-01-03 23:52:36 +00003327 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003328
3329 if (arg == NULL)
Guido van Rossumb209a111997-04-29 18:18:01 +00003330 arg = PyTuple_New(0);
3331 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003332 PyErr_SetString(PyExc_TypeError,
3333 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003334 return NULL;
3335 }
3336 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003337 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003338
Guido van Rossumb209a111997-04-29 18:18:01 +00003339 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003340 PyErr_SetString(PyExc_TypeError,
3341 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003342 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003343 return NULL;
3344 }
3345
Tim Peters6d6c1a32001-08-02 04:15:00 +00003346 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003347 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003348 return result;
3349}
3350
Tim Peters6d6c1a32001-08-02 04:15:00 +00003351char *
3352PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003353{
3354 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003355 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003356 else if (PyFunction_Check(func))
3357 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3358 else if (PyCFunction_Check(func))
3359 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3360 else if (PyClass_Check(func))
3361 return PyString_AsString(((PyClassObject*)func)->cl_name);
3362 else if (PyInstance_Check(func)) {
3363 return PyString_AsString(
3364 ((PyInstanceObject*)func)->in_class->cl_name);
3365 } else {
3366 return func->ob_type->tp_name;
3367 }
3368}
3369
Tim Peters6d6c1a32001-08-02 04:15:00 +00003370char *
3371PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003372{
3373 if (PyMethod_Check(func))
3374 return "()";
3375 else if (PyFunction_Check(func))
3376 return "()";
3377 else if (PyCFunction_Check(func))
3378 return "()";
3379 else if (PyClass_Check(func))
3380 return " constructor";
3381 else if (PyInstance_Check(func)) {
3382 return " instance";
3383 } else {
3384 return " object";
3385 }
3386}
3387
Jeremy Hylton52820442001-01-03 23:52:36 +00003388#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
3389
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003390static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003391err_args(PyObject *func, int flags, int nargs)
3392{
3393 if (flags & METH_NOARGS)
3394 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003395 "%.200s() takes no arguments (%d given)",
Jeremy Hylton192690e2002-08-16 18:36:11 +00003396 ((PyCFunctionObject *)func)->m_ml->ml_name,
3397 nargs);
3398 else
3399 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003400 "%.200s() takes exactly one argument (%d given)",
Jeremy Hylton192690e2002-08-16 18:36:11 +00003401 ((PyCFunctionObject *)func)->m_ml->ml_name,
3402 nargs);
3403}
3404
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003405static PyObject *
3406call_function(PyObject ***pp_stack, int oparg)
3407{
3408 int na = oparg & 0xff;
3409 int nk = (oparg>>8) & 0xff;
3410 int n = na + 2 * nk;
3411 PyObject **pfunc = (*pp_stack) - n - 1;
3412 PyObject *func = *pfunc;
3413 PyObject *x, *w;
3414
Jeremy Hylton985eba52003-02-05 23:13:00 +00003415 /* Always dispatch PyCFunction first, because these are
3416 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003417 */
3418 if (PyCFunction_Check(func) && nk == 0) {
3419 int flags = PyCFunction_GET_FLAGS(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003420 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003421 if (flags & (METH_NOARGS | METH_O)) {
3422 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3423 PyObject *self = PyCFunction_GET_SELF(func);
3424 if (flags & METH_NOARGS && na == 0)
3425 x = (*meth)(self, NULL);
3426 else if (flags & METH_O && na == 1) {
3427 PyObject *arg = EXT_POP(*pp_stack);
3428 x = (*meth)(self, arg);
3429 Py_DECREF(arg);
3430 }
3431 else {
3432 err_args(func, flags, na);
3433 x = NULL;
3434 }
3435 }
3436 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003437 PyObject *callargs;
3438 callargs = load_args(pp_stack, na);
3439 x = PyCFunction_Call(func, callargs, NULL);
3440 Py_XDECREF(callargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003441 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003442 } else {
3443 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3444 /* optimize access to bound methods */
3445 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003446 PCALL(PCALL_METHOD);
3447 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003448 Py_INCREF(self);
3449 func = PyMethod_GET_FUNCTION(func);
3450 Py_INCREF(func);
3451 Py_DECREF(*pfunc);
3452 *pfunc = self;
3453 na++;
3454 n++;
3455 } else
3456 Py_INCREF(func);
3457 if (PyFunction_Check(func))
3458 x = fast_function(func, pp_stack, n, na, nk);
3459 else
3460 x = do_call(func, pp_stack, na, nk);
3461 Py_DECREF(func);
3462 }
3463
Jeremy Hylton985eba52003-02-05 23:13:00 +00003464 /* What does this do? */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003465 while ((*pp_stack) > pfunc) {
3466 w = EXT_POP(*pp_stack);
3467 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003468 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003469 }
3470 return x;
3471}
3472
Jeremy Hylton192690e2002-08-16 18:36:11 +00003473/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003474 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003475 For the simplest case -- a function that takes only positional
3476 arguments and is called with only positional arguments -- it
3477 inlines the most primitive frame setup code from
3478 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3479 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003480*/
3481
3482static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003483fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003484{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003485 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003486 PyObject *globals = PyFunction_GET_GLOBALS(func);
3487 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3488 PyObject **d = NULL;
3489 int nd = 0;
3490
Jeremy Hylton985eba52003-02-05 23:13:00 +00003491 PCALL(PCALL_FUNCTION);
3492 PCALL(PCALL_FAST_FUNCTION);
Raymond Hettinger40174c32003-05-31 07:04:16 +00003493 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003494 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3495 PyFrameObject *f;
3496 PyObject *retval = NULL;
3497 PyThreadState *tstate = PyThreadState_GET();
3498 PyObject **fastlocals, **stack;
3499 int i;
3500
3501 PCALL(PCALL_FASTER_FUNCTION);
3502 assert(globals != NULL);
3503 /* XXX Perhaps we should create a specialized
3504 PyFrame_New() that doesn't take locals, but does
3505 take builtins without sanity checking them.
3506 */
3507 f = PyFrame_New(tstate, co, globals, NULL);
3508 if (f == NULL)
3509 return NULL;
3510
3511 fastlocals = f->f_localsplus;
3512 stack = (*pp_stack) - n;
3513
3514 for (i = 0; i < n; i++) {
3515 Py_INCREF(*stack);
3516 fastlocals[i] = *stack++;
3517 }
3518 retval = eval_frame(f);
3519 assert(tstate != NULL);
3520 ++tstate->recursion_depth;
3521 Py_DECREF(f);
3522 --tstate->recursion_depth;
3523 return retval;
3524 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003525 if (argdefs != NULL) {
3526 d = &PyTuple_GET_ITEM(argdefs, 0);
3527 nd = ((PyTupleObject *)argdefs)->ob_size;
3528 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003529 return PyEval_EvalCodeEx(co, globals,
3530 (PyObject *)NULL, (*pp_stack)-n, na,
3531 (*pp_stack)-2*nk, nk, d, nd,
3532 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003533}
3534
3535static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003536update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3537 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003538{
3539 PyObject *kwdict = NULL;
3540 if (orig_kwdict == NULL)
3541 kwdict = PyDict_New();
3542 else {
3543 kwdict = PyDict_Copy(orig_kwdict);
3544 Py_DECREF(orig_kwdict);
3545 }
3546 if (kwdict == NULL)
3547 return NULL;
3548 while (--nk >= 0) {
3549 int err;
3550 PyObject *value = EXT_POP(*pp_stack);
3551 PyObject *key = EXT_POP(*pp_stack);
3552 if (PyDict_GetItem(kwdict, key) != NULL) {
Guido van Rossumac7be682001-01-17 15:42:30 +00003553 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +00003554 "%.200s%s got multiple values "
Jeremy Hylton512a2372001-04-11 13:52:29 +00003555 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003556 PyEval_GetFuncName(func),
3557 PyEval_GetFuncDesc(func),
Jeremy Hylton512a2372001-04-11 13:52:29 +00003558 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003559 Py_DECREF(key);
3560 Py_DECREF(value);
3561 Py_DECREF(kwdict);
3562 return NULL;
3563 }
3564 err = PyDict_SetItem(kwdict, key, value);
3565 Py_DECREF(key);
3566 Py_DECREF(value);
3567 if (err) {
3568 Py_DECREF(kwdict);
3569 return NULL;
3570 }
3571 }
3572 return kwdict;
3573}
3574
3575static PyObject *
3576update_star_args(int nstack, int nstar, PyObject *stararg,
3577 PyObject ***pp_stack)
3578{
3579 PyObject *callargs, *w;
3580
3581 callargs = PyTuple_New(nstack + nstar);
3582 if (callargs == NULL) {
3583 return NULL;
3584 }
3585 if (nstar) {
3586 int i;
3587 for (i = 0; i < nstar; i++) {
3588 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3589 Py_INCREF(a);
3590 PyTuple_SET_ITEM(callargs, nstack + i, a);
3591 }
3592 }
3593 while (--nstack >= 0) {
3594 w = EXT_POP(*pp_stack);
3595 PyTuple_SET_ITEM(callargs, nstack, w);
3596 }
3597 return callargs;
3598}
3599
3600static PyObject *
3601load_args(PyObject ***pp_stack, int na)
3602{
3603 PyObject *args = PyTuple_New(na);
3604 PyObject *w;
3605
3606 if (args == NULL)
3607 return NULL;
3608 while (--na >= 0) {
3609 w = EXT_POP(*pp_stack);
3610 PyTuple_SET_ITEM(args, na, w);
3611 }
3612 return args;
3613}
3614
3615static PyObject *
3616do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3617{
3618 PyObject *callargs = NULL;
3619 PyObject *kwdict = NULL;
3620 PyObject *result = NULL;
3621
3622 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003623 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003624 if (kwdict == NULL)
3625 goto call_fail;
3626 }
3627 callargs = load_args(pp_stack, na);
3628 if (callargs == NULL)
3629 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003630#ifdef CALL_PROFILE
3631 /* At this point, we have to look at the type of func to
3632 update the call stats properly. Do it here so as to avoid
3633 exposing the call stats machinery outside ceval.c
3634 */
3635 if (PyFunction_Check(func))
3636 PCALL(PCALL_FUNCTION);
3637 else if (PyMethod_Check(func))
3638 PCALL(PCALL_METHOD);
3639 else if (PyType_Check(func))
3640 PCALL(PCALL_TYPE);
3641 else
3642 PCALL(PCALL_OTHER);
3643#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003644 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003645 call_fail:
3646 Py_XDECREF(callargs);
3647 Py_XDECREF(kwdict);
3648 return result;
3649}
3650
3651static PyObject *
3652ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3653{
3654 int nstar = 0;
3655 PyObject *callargs = NULL;
3656 PyObject *stararg = NULL;
3657 PyObject *kwdict = NULL;
3658 PyObject *result = NULL;
3659
3660 if (flags & CALL_FLAG_KW) {
3661 kwdict = EXT_POP(*pp_stack);
3662 if (!(kwdict && PyDict_Check(kwdict))) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003663 PyErr_Format(PyExc_TypeError,
Jeremy Hylton512a2372001-04-11 13:52:29 +00003664 "%s%s argument after ** "
3665 "must be a dictionary",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003666 PyEval_GetFuncName(func),
3667 PyEval_GetFuncDesc(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003668 goto ext_call_fail;
3669 }
3670 }
3671 if (flags & CALL_FLAG_VAR) {
3672 stararg = EXT_POP(*pp_stack);
3673 if (!PyTuple_Check(stararg)) {
3674 PyObject *t = NULL;
3675 t = PySequence_Tuple(stararg);
3676 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003677 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3678 PyErr_Format(PyExc_TypeError,
3679 "%s%s argument after * "
3680 "must be a sequence",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003681 PyEval_GetFuncName(func),
3682 PyEval_GetFuncDesc(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003683 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003684 goto ext_call_fail;
3685 }
3686 Py_DECREF(stararg);
3687 stararg = t;
3688 }
3689 nstar = PyTuple_GET_SIZE(stararg);
3690 }
3691 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003692 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003693 if (kwdict == NULL)
3694 goto ext_call_fail;
3695 }
3696 callargs = update_star_args(na, nstar, stararg, pp_stack);
3697 if (callargs == NULL)
3698 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003699#ifdef CALL_PROFILE
3700 /* At this point, we have to look at the type of func to
3701 update the call stats properly. Do it here so as to avoid
3702 exposing the call stats machinery outside ceval.c
3703 */
3704 if (PyFunction_Check(func))
3705 PCALL(PCALL_FUNCTION);
3706 else if (PyMethod_Check(func))
3707 PCALL(PCALL_METHOD);
3708 else if (PyType_Check(func))
3709 PCALL(PCALL_TYPE);
3710 else
3711 PCALL(PCALL_OTHER);
3712#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003713 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003714 ext_call_fail:
3715 Py_XDECREF(callargs);
3716 Py_XDECREF(kwdict);
3717 Py_XDECREF(stararg);
3718 return result;
3719}
3720
Guido van Rossum3b9c6671996-07-30 18:40:29 +00003721#define SLICE_ERROR_MSG \
3722 "standard sequence type does not support step size other than one"
3723
Tim Peterscb479e72001-12-16 19:11:44 +00003724/* Extract a slice index from a PyInt or PyLong, and store in *pi.
3725 Silently reduce values larger than INT_MAX to INT_MAX, and silently
3726 boost values less than -INT_MAX to 0. Return 0 on error, 1 on success.
3727*/
Tim Petersb5196382001-12-16 19:44:20 +00003728/* Note: If v is NULL, return success without storing into *pi. This
3729 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3730 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00003731*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00003732int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003733_PyEval_SliceIndex(PyObject *v, int *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003734{
Tim Petersb5196382001-12-16 19:44:20 +00003735 if (v != NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003736 long x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003737 if (PyInt_Check(v)) {
3738 x = PyInt_AsLong(v);
3739 } else if (PyLong_Check(v)) {
3740 x = PyLong_AsLong(v);
3741 if (x==-1 && PyErr_Occurred()) {
3742 PyObject *long_zero;
Guido van Rossumac7be682001-01-17 15:42:30 +00003743 int cmp;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003744
Guido van Rossumac7be682001-01-17 15:42:30 +00003745 if (!PyErr_ExceptionMatches(
3746 PyExc_OverflowError)) {
3747 /* It's not an overflow error, so just
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003748 signal an error */
Guido van Rossum20c6add2000-05-08 14:06:50 +00003749 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003750 }
3751
Guido van Rossumac7be682001-01-17 15:42:30 +00003752 /* Clear the OverflowError */
3753 PyErr_Clear();
3754
3755 /* It's an overflow error, so we need to
3756 check the sign of the long integer,
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003757 set the value to INT_MAX or -INT_MAX,
3758 and clear the error. */
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003759
3760 /* Create a long integer with a value of 0 */
Guido van Rossumac7be682001-01-17 15:42:30 +00003761 long_zero = PyLong_FromLong(0L);
Tim Peterscb479e72001-12-16 19:11:44 +00003762 if (long_zero == NULL)
3763 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003764
3765 /* Check sign */
Guido van Rossumac7be682001-01-17 15:42:30 +00003766 cmp = PyObject_RichCompareBool(v, long_zero,
3767 Py_GT);
3768 Py_DECREF(long_zero);
3769 if (cmp < 0)
3770 return 0;
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003771 else if (cmp)
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003772 x = INT_MAX;
3773 else
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003774 x = -INT_MAX;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003775 }
3776 } else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003777 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003778 "slice indices must be integers");
Guido van Rossum20c6add2000-05-08 14:06:50 +00003779 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003780 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003781 /* Truncate -- very long indices are truncated anyway */
3782 if (x > INT_MAX)
3783 x = INT_MAX;
3784 else if (x < -INT_MAX)
Michael W. Hudsoncbd6fb92002-11-06 15:17:32 +00003785 x = -INT_MAX;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003786 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003787 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00003788 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003789}
3790
Guido van Rossum50d756e2001-08-18 17:43:36 +00003791#undef ISINT
3792#define ISINT(x) ((x) == NULL || PyInt_Check(x) || PyLong_Check(x))
3793
Guido van Rossumb209a111997-04-29 18:18:01 +00003794static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003795apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003796{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003797 PyTypeObject *tp = u->ob_type;
3798 PySequenceMethods *sq = tp->tp_as_sequence;
3799
3800 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3801 int ilow = 0, ihigh = INT_MAX;
3802 if (!_PyEval_SliceIndex(v, &ilow))
3803 return NULL;
3804 if (!_PyEval_SliceIndex(w, &ihigh))
3805 return NULL;
3806 return PySequence_GetSlice(u, ilow, ihigh);
3807 }
3808 else {
3809 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00003810 if (slice != NULL) {
3811 PyObject *res = PyObject_GetItem(u, slice);
3812 Py_DECREF(slice);
3813 return res;
3814 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00003815 else
3816 return NULL;
3817 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003818}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003819
3820static int
Guido van Rossumac7be682001-01-17 15:42:30 +00003821assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
3822 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003823{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003824 PyTypeObject *tp = u->ob_type;
3825 PySequenceMethods *sq = tp->tp_as_sequence;
3826
3827 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3828 int ilow = 0, ihigh = INT_MAX;
3829 if (!_PyEval_SliceIndex(v, &ilow))
3830 return -1;
3831 if (!_PyEval_SliceIndex(w, &ihigh))
3832 return -1;
3833 if (x == NULL)
3834 return PySequence_DelSlice(u, ilow, ihigh);
3835 else
3836 return PySequence_SetSlice(u, ilow, ihigh, x);
3837 }
3838 else {
3839 PyObject *slice = PySlice_New(v, w, NULL);
3840 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00003841 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003842 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00003843 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00003844 else
Guido van Rossum354797c2001-12-03 19:45:06 +00003845 res = PyObject_DelItem(u, slice);
3846 Py_DECREF(slice);
3847 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003848 }
3849 else
3850 return -1;
3851 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003852}
3853
Guido van Rossumb209a111997-04-29 18:18:01 +00003854static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003855cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003856{
Guido van Rossumac7be682001-01-17 15:42:30 +00003857 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003858 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00003859 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003860 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003861 break;
3862 case PyCmp_IS_NOT:
3863 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003864 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003865 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003866 res = PySequence_Contains(w, v);
3867 if (res < 0)
3868 return NULL;
3869 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003870 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00003871 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003872 if (res < 0)
3873 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003874 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003875 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003876 case PyCmp_EXC_MATCH:
Barry Warsaw4249f541997-08-22 21:26:19 +00003877 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003878 break;
3879 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00003880 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003881 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003882 v = res ? Py_True : Py_False;
3883 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003884 return v;
3885}
3886
Thomas Wouters52152252000-08-17 22:55:00 +00003887static PyObject *
3888import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003889{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003890 PyObject *x;
3891
3892 x = PyObject_GetAttr(v, name);
3893 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00003894 PyErr_Format(PyExc_ImportError,
3895 "cannot import name %.230s",
3896 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003897 }
Thomas Wouters52152252000-08-17 22:55:00 +00003898 return x;
3899}
Guido van Rossumac7be682001-01-17 15:42:30 +00003900
Thomas Wouters52152252000-08-17 22:55:00 +00003901static int
3902import_all_from(PyObject *locals, PyObject *v)
3903{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003904 PyObject *all = PyObject_GetAttrString(v, "__all__");
3905 PyObject *dict, *name, *value;
3906 int skip_leading_underscores = 0;
3907 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00003908
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003909 if (all == NULL) {
3910 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3911 return -1; /* Unexpected error */
3912 PyErr_Clear();
3913 dict = PyObject_GetAttrString(v, "__dict__");
3914 if (dict == NULL) {
3915 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3916 return -1;
3917 PyErr_SetString(PyExc_ImportError,
3918 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00003919 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003920 }
3921 all = PyMapping_Keys(dict);
3922 Py_DECREF(dict);
3923 if (all == NULL)
3924 return -1;
3925 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00003926 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003927
3928 for (pos = 0, err = 0; ; pos++) {
3929 name = PySequence_GetItem(all, pos);
3930 if (name == NULL) {
3931 if (!PyErr_ExceptionMatches(PyExc_IndexError))
3932 err = -1;
3933 else
3934 PyErr_Clear();
3935 break;
3936 }
3937 if (skip_leading_underscores &&
3938 PyString_Check(name) &&
3939 PyString_AS_STRING(name)[0] == '_')
3940 {
3941 Py_DECREF(name);
3942 continue;
3943 }
3944 value = PyObject_GetAttr(v, name);
3945 if (value == NULL)
3946 err = -1;
3947 else
3948 err = PyDict_SetItem(locals, name, value);
3949 Py_DECREF(name);
3950 Py_XDECREF(value);
3951 if (err != 0)
3952 break;
3953 }
3954 Py_DECREF(all);
3955 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00003956}
3957
Guido van Rossumb209a111997-04-29 18:18:01 +00003958static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003959build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003960{
Guido van Rossum7851eea2001-09-12 19:19:18 +00003961 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003962
3963 if (PyDict_Check(methods))
3964 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00003965 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00003966 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00003967 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
3968 base = PyTuple_GET_ITEM(bases, 0);
3969 metaclass = PyObject_GetAttrString(base, "__class__");
3970 if (metaclass == NULL) {
3971 PyErr_Clear();
3972 metaclass = (PyObject *)base->ob_type;
3973 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00003974 }
3975 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00003976 else {
3977 PyObject *g = PyEval_GetGlobals();
3978 if (g != NULL && PyDict_Check(g))
3979 metaclass = PyDict_GetItemString(g, "__metaclass__");
3980 if (metaclass == NULL)
3981 metaclass = (PyObject *) &PyClass_Type;
3982 Py_INCREF(metaclass);
3983 }
3984 result = PyObject_CallFunction(metaclass, "OOO", name, bases, methods);
3985 Py_DECREF(metaclass);
3986 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00003987}
3988
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003989static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003990exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
3991 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003992{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003993 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00003994 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003995 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003996
Guido van Rossumb209a111997-04-29 18:18:01 +00003997 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
3998 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003999 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00004000 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004001 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00004002 locals = PyTuple_GetItem(prog, 2);
4003 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004004 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004005 if (globals == Py_None) {
4006 globals = PyEval_GetGlobals();
4007 if (locals == Py_None) {
4008 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00004009 plain = 1;
4010 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004011 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004012 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004013 locals = globals;
Guido van Rossumb209a111997-04-29 18:18:01 +00004014 if (!PyString_Check(prog) &&
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004015 !PyUnicode_Check(prog) &&
Guido van Rossumb209a111997-04-29 18:18:01 +00004016 !PyCode_Check(prog) &&
4017 !PyFile_Check(prog)) {
4018 PyErr_SetString(PyExc_TypeError,
Guido van Rossumac7be682001-01-17 15:42:30 +00004019 "exec: arg 1 must be a string, file, or code object");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004020 return -1;
4021 }
Fred Drake661ea262000-10-24 19:57:45 +00004022 if (!PyDict_Check(globals)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004023 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00004024 "exec: arg 2 must be a dictionary or None");
4025 return -1;
4026 }
4027 if (!PyDict_Check(locals)) {
4028 PyErr_SetString(PyExc_TypeError,
4029 "exec: arg 3 must be a dictionary or None");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004030 return -1;
4031 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004032 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00004033 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00004034 if (PyCode_Check(prog)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +00004035 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4036 PyErr_SetString(PyExc_TypeError,
4037 "code object passed to exec may not contain free variables");
4038 return -1;
4039 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004040 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004041 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004042 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004043 FILE *fp = PyFile_AsFile(prog);
4044 char *name = PyString_AsString(PyFile_Name(prog));
Tim Peters5ba58662001-07-16 02:29:45 +00004045 PyCompilerFlags cf;
4046 cf.cf_flags = 0;
4047 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004048 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
4049 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004050 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004051 v = PyRun_File(fp, name, Py_file_input, globals,
4052 locals);
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004053 }
4054 else {
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004055 PyObject *tmp = NULL;
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004056 char *str;
Tim Peters5ba58662001-07-16 02:29:45 +00004057 PyCompilerFlags cf;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004058 cf.cf_flags = 0;
4059#ifdef Py_USING_UNICODE
4060 if (PyUnicode_Check(prog)) {
4061 tmp = PyUnicode_AsUTF8String(prog);
4062 if (tmp == NULL)
4063 return -1;
4064 prog = tmp;
4065 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4066 }
4067#endif
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004068 if (PyString_AsStringAndSize(prog, &str, NULL))
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004069 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004070 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004071 v = PyRun_StringFlags(str, Py_file_input, globals,
4072 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004073 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004074 v = PyRun_String(str, Py_file_input, globals, locals);
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004075 Py_XDECREF(tmp);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004076 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004077 if (plain)
4078 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004079 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004080 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00004081 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004082 return 0;
4083}
Guido van Rossum24c13741995-02-14 09:42:43 +00004084
Guido van Rossumac7be682001-01-17 15:42:30 +00004085static void
Paul Prescode68140d2000-08-30 20:25:01 +00004086format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4087{
4088 char *obj_str;
4089
4090 if (!obj)
4091 return;
4092
4093 obj_str = PyString_AsString(obj);
4094 if (!obj_str)
4095 return;
4096
4097 PyErr_Format(exc, format_str, obj_str);
4098}
Guido van Rossum950361c1997-01-24 13:49:28 +00004099
4100#ifdef DYNAMIC_EXECUTION_PROFILE
4101
Skip Montanarof118cb12001-10-15 20:51:38 +00004102static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004103getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004104{
4105 int i;
4106 PyObject *l = PyList_New(256);
4107 if (l == NULL) return NULL;
4108 for (i = 0; i < 256; i++) {
4109 PyObject *x = PyInt_FromLong(a[i]);
4110 if (x == NULL) {
4111 Py_DECREF(l);
4112 return NULL;
4113 }
4114 PyList_SetItem(l, i, x);
4115 }
4116 for (i = 0; i < 256; i++)
4117 a[i] = 0;
4118 return l;
4119}
4120
4121PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004122_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004123{
4124#ifndef DXPAIRS
4125 return getarray(dxp);
4126#else
4127 int i;
4128 PyObject *l = PyList_New(257);
4129 if (l == NULL) return NULL;
4130 for (i = 0; i < 257; i++) {
4131 PyObject *x = getarray(dxpairs[i]);
4132 if (x == NULL) {
4133 Py_DECREF(l);
4134 return NULL;
4135 }
4136 PyList_SetItem(l, i, x);
4137 }
4138 return l;
4139#endif
4140}
4141
4142#endif