blob: c6e04702443e23e13c042b864aeb7d184a32bfd3 [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
Guido van Rossumc6004111993-11-05 10:22:19 +000017#include <ctype.h>
18
Guido van Rossum04691fc1992-08-12 15:35:34 +000019/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000020/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000021
Guido van Rossum408027e1996-12-30 16:17:54 +000022#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000023/* For debugging the interpreter: */
24#define LLTRACE 1 /* Low-level trace feature */
25#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000026#endif
27
Jeremy Hylton52820442001-01-03 23:52:36 +000028typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000029
Guido van Rossum374a9221991-04-04 10:40:29 +000030/* Forward declarations */
Tim Peters5ca576e2001-06-18 22:08:13 +000031static PyObject *eval_frame(PyFrameObject *);
Jeremy Hyltone8c04322002-08-16 17:47:26 +000032static PyObject *call_function(PyObject ***, int);
Jeremy Hylton52820442001-01-03 23:52:36 +000033static PyObject *fast_function(PyObject *, PyObject ***, int, int, int);
Jeremy Hylton52820442001-01-03 23:52:36 +000034static PyObject *do_call(PyObject *, PyObject ***, int, int);
35static PyObject *ext_do_call(PyObject *, PyObject ***, int, int, int);
Guido van Rossumac7be682001-01-17 15:42:30 +000036static PyObject *update_keyword_args(PyObject *, int, PyObject ***,PyObject *);
Ka-Ping Yee20579702001-01-15 22:14:16 +000037static PyObject *update_star_args(int, int, PyObject *, PyObject ***);
Jeremy Hylton52820442001-01-03 23:52:36 +000038static PyObject *load_args(PyObject ***, int);
39#define CALL_FLAG_VAR 1
40#define CALL_FLAG_KW 2
41
Guido van Rossum0a066c01992-03-27 17:29:15 +000042#ifdef LLTRACE
Tim Petersdbd9ba62000-07-09 03:09:57 +000043static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000044#endif
Fred Drake5755ce62001-06-27 19:19:46 +000045static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
46 int, PyObject *);
Fred Drake4ec5d562001-10-04 19:26:43 +000047static void call_trace_protected(Py_tracefunc, PyObject *,
48 PyFrameObject *, int);
Fred Drake5755ce62001-06-27 19:19:46 +000049static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
Michael W. Hudson006c7522002-11-08 13:08:46 +000050static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Michael W. Hudsondd32a912002-08-15 14:59:02 +000051 PyFrameObject *, int *, int *);
52
Tim Petersdbd9ba62000-07-09 03:09:57 +000053static PyObject *apply_slice(PyObject *, PyObject *, PyObject *);
54static int assign_slice(PyObject *, PyObject *,
55 PyObject *, PyObject *);
56static PyObject *cmp_outcome(int, PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +000057static PyObject *import_from(PyObject *, PyObject *);
58static int import_all_from(PyObject *, PyObject *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000059static PyObject *build_class(PyObject *, PyObject *, PyObject *);
60static int exec_statement(PyFrameObject *,
61 PyObject *, PyObject *, PyObject *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000062static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *);
63static void reset_exc_info(PyThreadState *);
Paul Prescode68140d2000-08-30 20:25:01 +000064static void format_exc_check_arg(PyObject *, char *, PyObject *);
Guido van Rossum374a9221991-04-04 10:40:29 +000065
Paul Prescode68140d2000-08-30 20:25:01 +000066#define NAME_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +000067 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +000068#define GLOBAL_NAME_ERROR_MSG \
69 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000070#define UNBOUNDLOCAL_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +000071 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000072#define UNBOUNDFREE_ERROR_MSG \
73 "free variable '%.200s' referenced before assignment" \
74 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000075
Guido van Rossum950361c1997-01-24 13:49:28 +000076/* Dynamic execution profile */
77#ifdef DYNAMIC_EXECUTION_PROFILE
78#ifdef DXPAIRS
79static long dxpairs[257][256];
80#define dxp dxpairs[256]
81#else
82static long dxp[256];
83#endif
84#endif
85
Jeremy Hylton985eba52003-02-05 23:13:00 +000086/* Function call profile */
87#ifdef CALL_PROFILE
88#define PCALL_NUM 11
89static int pcall[PCALL_NUM];
90
91#define PCALL_ALL 0
92#define PCALL_FUNCTION 1
93#define PCALL_FAST_FUNCTION 2
94#define PCALL_FASTER_FUNCTION 3
95#define PCALL_METHOD 4
96#define PCALL_BOUND_METHOD 5
97#define PCALL_CFUNCTION 6
98#define PCALL_TYPE 7
99#define PCALL_GENERATOR 8
100#define PCALL_OTHER 9
101#define PCALL_POP 10
102
103/* Notes about the statistics
104
105 PCALL_FAST stats
106
107 FAST_FUNCTION means no argument tuple needs to be created.
108 FASTER_FUNCTION means that the fast-path frame setup code is used.
109
110 If there is a method call where the call can be optimized by changing
111 the argument tuple and calling the function directly, it gets recorded
112 twice.
113
114 As a result, the relationship among the statistics appears to be
115 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
116 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
117 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
118 PCALL_METHOD > PCALL_BOUND_METHOD
119*/
120
121#define PCALL(POS) pcall[POS]++
122
123PyObject *
124PyEval_GetCallStats(PyObject *self)
125{
126 return Py_BuildValue("iiiiiiiiii",
127 pcall[0], pcall[1], pcall[2], pcall[3],
128 pcall[4], pcall[5], pcall[6], pcall[7],
129 pcall[8], pcall[9]);
130}
131#else
132#define PCALL(O)
133
134PyObject *
135PyEval_GetCallStats(PyObject *self)
136{
137 Py_INCREF(Py_None);
138 return Py_None;
139}
140#endif
141
Jeremy Hylton938ace62002-07-17 16:30:39 +0000142static PyTypeObject gentype;
Tim Peters5ca576e2001-06-18 22:08:13 +0000143
144typedef struct {
145 PyObject_HEAD
Tim Petersd8e1c9e2001-06-26 20:58:58 +0000146 /* The gi_ prefix is intended to remind of generator-iterator. */
147
148 PyFrameObject *gi_frame;
149
Tim Peterse77f2e22001-06-26 22:24:51 +0000150 /* True if generator is being executed. */
151 int gi_running;
Fred Drake72bc4562002-08-09 18:35:52 +0000152
153 /* List of weak reference. */
154 PyObject *gi_weakreflist;
Tim Peters5ca576e2001-06-18 22:08:13 +0000155} genobject;
156
157static PyObject *
158gen_new(PyFrameObject *f)
159{
Neil Schemenauer08de92a2002-03-18 20:45:09 +0000160 genobject *gen = PyObject_GC_New(genobject, &gentype);
Tim Peters5ca576e2001-06-18 22:08:13 +0000161 if (gen == NULL) {
162 Py_DECREF(f);
163 return NULL;
164 }
Tim Petersd8e1c9e2001-06-26 20:58:58 +0000165 gen->gi_frame = f;
166 gen->gi_running = 0;
Fred Drake72bc4562002-08-09 18:35:52 +0000167 gen->gi_weakreflist = NULL;
Neil Schemenauer08de92a2002-03-18 20:45:09 +0000168 _PyObject_GC_TRACK(gen);
Tim Peters5ca576e2001-06-18 22:08:13 +0000169 return (PyObject *)gen;
170}
171
Neil Schemenauerf8c7c202001-07-12 13:27:49 +0000172static int
173gen_traverse(genobject *gen, visitproc visit, void *arg)
174{
175 return visit((PyObject *)gen->gi_frame, arg);
176}
177
Tim Peters5ca576e2001-06-18 22:08:13 +0000178static void
179gen_dealloc(genobject *gen)
180{
Neil Schemenauer08de92a2002-03-18 20:45:09 +0000181 _PyObject_GC_UNTRACK(gen);
Fred Drake72bc4562002-08-09 18:35:52 +0000182 if (gen->gi_weakreflist != NULL)
183 PyObject_ClearWeakRefs((PyObject *) gen);
Tim Petersd8e1c9e2001-06-26 20:58:58 +0000184 Py_DECREF(gen->gi_frame);
Neil Schemenauer08de92a2002-03-18 20:45:09 +0000185 PyObject_GC_Del(gen);
Tim Peters5ca576e2001-06-18 22:08:13 +0000186}
187
188static PyObject *
189gen_iternext(genobject *gen)
190{
Neil Schemenauer2b13ce82001-06-21 02:41:10 +0000191 PyThreadState *tstate = PyThreadState_GET();
Tim Petersd8e1c9e2001-06-26 20:58:58 +0000192 PyFrameObject *f = gen->gi_frame;
Tim Peters5ca576e2001-06-18 22:08:13 +0000193 PyObject *result;
194
Tim Petersd8e1c9e2001-06-26 20:58:58 +0000195 if (gen->gi_running) {
Tim Peters5ca576e2001-06-18 22:08:13 +0000196 PyErr_SetString(PyExc_ValueError,
197 "generator already executing");
198 return NULL;
199 }
Tim Peters8c963692001-06-23 05:26:56 +0000200 if (f->f_stacktop == NULL)
Tim Peters5ca576e2001-06-18 22:08:13 +0000201 return NULL;
Neil Schemenauer2b13ce82001-06-21 02:41:10 +0000202
203 /* Generators always return to their most recent caller, not
204 * necessarily their creator. */
Tim Peters5eb4b872001-06-23 05:47:56 +0000205 Py_XINCREF(tstate->frame);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +0000206 assert(f->f_back == NULL);
207 f->f_back = tstate->frame;
208
Tim Petersd8e1c9e2001-06-26 20:58:58 +0000209 gen->gi_running = 1;
Tim Peters5ca576e2001-06-18 22:08:13 +0000210 result = eval_frame(f);
Tim Petersd8e1c9e2001-06-26 20:58:58 +0000211 gen->gi_running = 0;
Neil Schemenauer2b13ce82001-06-21 02:41:10 +0000212
213 /* Don't keep the reference to f_back any longer than necessary. It
214 * may keep a chain of frames alive or it could create a reference
215 * cycle. */
Tim Peters5eb4b872001-06-23 05:47:56 +0000216 Py_XDECREF(f->f_back);
Tim Peters6302ec62001-06-20 06:57:32 +0000217 f->f_back = NULL;
Neil Schemenauer2b13ce82001-06-21 02:41:10 +0000218
Tim Petersad1a18b2001-06-23 06:19:16 +0000219 /* If the generator just returned (as opposed to yielding), signal
220 * that the generator is exhausted. */
221 if (result == Py_None && f->f_stacktop == NULL) {
222 Py_DECREF(result);
223 result = NULL;
224 }
225
Neil Schemenauer2b13ce82001-06-21 02:41:10 +0000226 return result;
Tim Peters5ca576e2001-06-18 22:08:13 +0000227}
228
229static PyObject *
Tim Peters5ca576e2001-06-18 22:08:13 +0000230gen_getiter(PyObject *gen)
231{
232 Py_INCREF(gen);
233 return gen;
234}
235
Guido van Rossum6f799372001-09-20 20:46:19 +0000236static PyMemberDef gen_memberlist[] = {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000237 {"gi_frame", T_OBJECT, offsetof(genobject, gi_frame), RO},
238 {"gi_running", T_INT, offsetof(genobject, gi_running), RO},
239 {NULL} /* Sentinel */
240};
Tim Peters5ca576e2001-06-18 22:08:13 +0000241
Tim Peters0c322792002-07-17 16:49:03 +0000242static PyTypeObject gentype = {
Tim Peters5ca576e2001-06-18 22:08:13 +0000243 PyObject_HEAD_INIT(&PyType_Type)
244 0, /* ob_size */
245 "generator", /* tp_name */
Neil Schemenauer08de92a2002-03-18 20:45:09 +0000246 sizeof(genobject), /* tp_basicsize */
Tim Peters5ca576e2001-06-18 22:08:13 +0000247 0, /* tp_itemsize */
248 /* methods */
249 (destructor)gen_dealloc, /* tp_dealloc */
250 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000251 0, /* tp_getattr */
Tim Peters5ca576e2001-06-18 22:08:13 +0000252 0, /* tp_setattr */
253 0, /* tp_compare */
254 0, /* tp_repr */
255 0, /* tp_as_number */
256 0, /* tp_as_sequence */
257 0, /* tp_as_mapping */
258 0, /* tp_hash */
259 0, /* tp_call */
260 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000261 PyObject_GenericGetAttr, /* tp_getattro */
Tim Peters5ca576e2001-06-18 22:08:13 +0000262 0, /* tp_setattro */
263 0, /* tp_as_buffer */
Neil Schemenauer08de92a2002-03-18 20:45:09 +0000264 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
Tim Peters5ca576e2001-06-18 22:08:13 +0000265 0, /* tp_doc */
Neil Schemenauerf8c7c202001-07-12 13:27:49 +0000266 (traverseproc)gen_traverse, /* tp_traverse */
Tim Peters5ca576e2001-06-18 22:08:13 +0000267 0, /* tp_clear */
268 0, /* tp_richcompare */
Fred Drake72bc4562002-08-09 18:35:52 +0000269 offsetof(genobject, gi_weakreflist), /* tp_weaklistoffset */
Tim Peters5ca576e2001-06-18 22:08:13 +0000270 (getiterfunc)gen_getiter, /* tp_iter */
271 (iternextfunc)gen_iternext, /* tp_iternext */
Tim Petersa64295b2002-07-17 00:15:22 +0000272 0, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000273 gen_memberlist, /* tp_members */
274 0, /* tp_getset */
275 0, /* tp_base */
276 0, /* tp_dict */
Tim Peters5ca576e2001-06-18 22:08:13 +0000277};
278
279
Guido van Rossume59214e1994-08-30 08:01:59 +0000280#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000281
Guido van Rossum2571cc81999-04-07 16:07:23 +0000282#ifndef DONT_HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000283#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000284#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000285#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000286
Guido van Rossuma027efa1997-05-05 20:56:21 +0000287extern int _PyThread_Started; /* Flag for Py_Exit */
288
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000289static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
Guido van Rossuma9672091994-09-14 13:31:22 +0000290static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000291
292void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000293PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000294{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000295 if (interpreter_lock)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000296 return;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000297 _PyThread_Started = 1;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000298 interpreter_lock = PyThread_allocate_lock();
299 PyThread_acquire_lock(interpreter_lock, 1);
300 main_thread = PyThread_get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000301}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000302
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000303void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000304PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000305{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000306 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000307}
308
309void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000310PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000311{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000312 PyThread_release_lock(interpreter_lock);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000313}
314
315void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000316PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000317{
318 if (tstate == NULL)
319 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000320 /* Check someone has called PyEval_InitThreads() to create the lock */
321 assert(interpreter_lock);
Guido van Rossum65d5b571998-12-21 19:32:43 +0000322 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000323 if (PyThreadState_Swap(tstate) != NULL)
324 Py_FatalError(
325 "PyEval_AcquireThread: non-NULL old thread state");
326}
327
328void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000329PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000330{
331 if (tstate == NULL)
332 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
333 if (PyThreadState_Swap(NULL) != tstate)
334 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000335 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000336}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000337
338/* This function is called from PyOS_AfterFork to ensure that newly
339 created child processes don't hold locks referring to threads which
340 are not running in the child process. (This could also be done using
341 pthread_atfork mechanism, at least for the pthreads implementation.) */
342
343void
344PyEval_ReInitThreads(void)
345{
346 if (!interpreter_lock)
347 return;
348 /*XXX Can't use PyThread_free_lock here because it does too
349 much error-checking. Doing this cleanly would require
350 adding a new function to each thread_*.h. Instead, just
351 create a new lock and waste a little bit of memory */
352 interpreter_lock = PyThread_allocate_lock();
353 PyThread_acquire_lock(interpreter_lock, 1);
354 main_thread = PyThread_get_thread_ident();
355}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000356#endif
357
Guido van Rossumff4949e1992-08-05 19:58:53 +0000358/* Functions save_thread and restore_thread are always defined so
359 dynamically loaded modules needn't be compiled separately for use
360 with and without threads: */
361
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000362PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000363PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000364{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000365 PyThreadState *tstate = PyThreadState_Swap(NULL);
366 if (tstate == NULL)
367 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000368#ifdef WITH_THREAD
Guido van Rossumb74eca91997-09-30 22:03:16 +0000369 if (interpreter_lock)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000370 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000371#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000372 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000373}
374
375void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000376PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000377{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000378 if (tstate == NULL)
379 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000380#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000381 if (interpreter_lock) {
Guido van Rossumb74eca91997-09-30 22:03:16 +0000382 int err = errno;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000383 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000384 errno = err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000385 }
386#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000387 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000388}
389
390
Guido van Rossuma9672091994-09-14 13:31:22 +0000391/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
392 signal handlers or Mac I/O completion routines) can schedule calls
393 to a function to be called synchronously.
394 The synchronous function is called with one void* argument.
395 It should return 0 for success or -1 for failure -- failure should
396 be accompanied by an exception.
397
398 If registry succeeds, the registry function returns 0; if it fails
399 (e.g. due to too many pending calls) it returns -1 (without setting
400 an exception condition).
401
402 Note that because registry may occur from within signal handlers,
403 or other asynchronous events, calling malloc() is unsafe!
404
405#ifdef WITH_THREAD
406 Any thread can schedule pending calls, but only the main thread
407 will execute them.
408#endif
409
410 XXX WARNING! ASYNCHRONOUSLY EXECUTING CODE!
411 There are two possible race conditions:
412 (1) nested asynchronous registry calls;
413 (2) registry calls made while pending calls are being processed.
414 While (1) is very unlikely, (2) is a real possibility.
415 The current code is safe against (2), but not against (1).
416 The safety against (2) is derived from the fact that only one
417 thread (the main thread) ever takes things out of the queue.
Guido van Rossuma9672091994-09-14 13:31:22 +0000418
Guido van Rossuma027efa1997-05-05 20:56:21 +0000419 XXX Darn! With the advent of thread state, we should have an array
420 of pending calls per thread in the thread state! Later...
421*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000422
Guido van Rossuma9672091994-09-14 13:31:22 +0000423#define NPENDINGCALLS 32
424static struct {
Thomas Wouters334fb892000-07-25 12:56:38 +0000425 int (*func)(void *);
426 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000427} pendingcalls[NPENDINGCALLS];
428static volatile int pendingfirst = 0;
429static volatile int pendinglast = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000430static volatile int things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000431
432int
Thomas Wouters334fb892000-07-25 12:56:38 +0000433Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000434{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000435 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000436 int i, j;
437 /* XXX Begin critical section */
438 /* XXX If you want this to be safe against nested
439 XXX asynchronous calls, you'll have to work harder! */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000440 if (busy)
441 return -1;
442 busy = 1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000443 i = pendinglast;
444 j = (i + 1) % NPENDINGCALLS;
Guido van Rossum04e70322002-07-17 16:57:13 +0000445 if (j == pendingfirst) {
446 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000447 return -1; /* Queue full */
Guido van Rossum04e70322002-07-17 16:57:13 +0000448 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000449 pendingcalls[i].func = func;
450 pendingcalls[i].arg = arg;
451 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000452
453 _Py_Ticker = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000454 things_to_do = 1; /* Signal main loop */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000455 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000456 /* XXX End critical section */
457 return 0;
458}
459
Guido van Rossum180d7b41994-09-29 09:45:57 +0000460int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000461Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000462{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000463 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000464#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000465 if (main_thread && PyThread_get_thread_ident() != main_thread)
Guido van Rossuma9672091994-09-14 13:31:22 +0000466 return 0;
467#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000468 if (busy)
Guido van Rossum180d7b41994-09-29 09:45:57 +0000469 return 0;
470 busy = 1;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000471 things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000472 for (;;) {
473 int i;
Thomas Wouters334fb892000-07-25 12:56:38 +0000474 int (*func)(void *);
475 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000476 i = pendingfirst;
477 if (i == pendinglast)
478 break; /* Queue empty */
479 func = pendingcalls[i].func;
480 arg = pendingcalls[i].arg;
481 pendingfirst = (i + 1) % NPENDINGCALLS;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000482 if (func(arg) < 0) {
483 busy = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000484 things_to_do = 1; /* We're not done yet */
Guido van Rossuma9672091994-09-14 13:31:22 +0000485 return -1;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000486 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000487 }
Guido van Rossum180d7b41994-09-29 09:45:57 +0000488 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000489 return 0;
490}
491
492
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000493/* The interpreter's recursion limit */
494
Guido van Rossum349ff6f2000-09-01 01:52:08 +0000495static int recursion_limit = 1000;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000496int _Py_CheckRecursionLimit = 1000;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000497
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000498int
499Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000500{
501 return recursion_limit;
502}
503
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000504void
505Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000506{
507 recursion_limit = new_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000508 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000509}
510
Armin Rigo2b3eb402003-10-28 12:05:48 +0000511/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
512 if the recursion_depth reaches _Py_CheckRecursionLimit.
513 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
514 to guarantee that _Py_CheckRecursiveCall() is regularly called.
515 Without USE_STACKCHECK, there is no need for this. */
516int
517_Py_CheckRecursiveCall(char *where)
518{
519 PyThreadState *tstate = PyThreadState_GET();
520
521#ifdef USE_STACKCHECK
522 if (PyOS_CheckStack()) {
523 --tstate->recursion_depth;
524 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
525 return -1;
526 }
527#endif
528 if (tstate->recursion_depth > recursion_limit) {
529 --tstate->recursion_depth;
530 PyErr_Format(PyExc_RuntimeError,
531 "maximum recursion depth exceeded%s",
532 where);
533 return -1;
534 }
535 _Py_CheckRecursionLimit = recursion_limit;
536 return 0;
537}
538
539
Guido van Rossum374a9221991-04-04 10:40:29 +0000540/* Status code for main loop (reason for stack unwind) */
541
542enum why_code {
543 WHY_NOT, /* No error */
544 WHY_EXCEPTION, /* Exception occurred */
545 WHY_RERAISE, /* Exception re-raised by 'finally' */
546 WHY_RETURN, /* 'return' statement */
Jeremy Hylton3faa52e2001-02-01 22:48:12 +0000547 WHY_BREAK, /* 'break' statement */
Tim Peters5ca576e2001-06-18 22:08:13 +0000548 WHY_CONTINUE, /* 'continue' statement */
Tim Peters6e6a63f2001-10-18 20:49:35 +0000549 WHY_YIELD /* 'yield' operator */
Guido van Rossum374a9221991-04-04 10:40:29 +0000550};
551
Tim Petersdbd9ba62000-07-09 03:09:57 +0000552static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
Tim Petersd6d010b2001-06-21 02:49:55 +0000553static int unpack_iterable(PyObject *, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000554
Skip Montanarod581d772002-09-03 20:10:45 +0000555/* for manipulating the thread switch and periodic "stuff" - used to be
556 per thread, now just a pair o' globals */
Skip Montanaro99dba272002-09-03 20:19:06 +0000557int _Py_CheckInterval = 100;
558volatile int _Py_Ticker = 100;
Guido van Rossum374a9221991-04-04 10:40:29 +0000559
Guido van Rossumb209a111997-04-29 18:18:01 +0000560PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000561PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000562{
Jeremy Hylton985eba52003-02-05 23:13:00 +0000563 /* XXX raise SystemError if globals is NULL */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000564 return PyEval_EvalCodeEx(co,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000565 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000566 (PyObject **)NULL, 0,
567 (PyObject **)NULL, 0,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000568 (PyObject **)NULL, 0,
569 NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000570}
571
572
573/* Interpreter main loop */
574
Tim Peters6d6c1a32001-08-02 04:15:00 +0000575static PyObject *
Tim Peters5ca576e2001-06-18 22:08:13 +0000576eval_frame(PyFrameObject *f)
Guido van Rossum374a9221991-04-04 10:40:29 +0000577{
Guido van Rossum950361c1997-01-24 13:49:28 +0000578#ifdef DXPAIRS
579 int lastopcode = 0;
580#endif
Tim Petersb6d14da2001-12-19 04:11:07 +0000581 PyObject **stack_pointer; /* Next free slot in value stack */
Guido van Rossum374a9221991-04-04 10:40:29 +0000582 register unsigned char *next_instr;
Moshe Zadkaaa39a7e2000-08-07 06:34:45 +0000583 register int opcode=0; /* Current opcode */
584 register int oparg=0; /* Current opcode argument, if any */
Guido van Rossum374a9221991-04-04 10:40:29 +0000585 register enum why_code why; /* Reason for block stack unwind */
586 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000587 register PyObject *x; /* Result object -- NULL if error */
588 register PyObject *v; /* Temporary objects popped off stack */
589 register PyObject *w;
590 register PyObject *u;
591 register PyObject *t;
Barry Warsaw23c9ec82000-08-21 15:44:01 +0000592 register PyObject *stream = NULL; /* for PRINT opcodes */
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000593 register PyObject **fastlocals, **freevars;
Guido van Rossum014518f1998-11-23 21:09:51 +0000594 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000595 PyThreadState *tstate = PyThreadState_GET();
Tim Peters5ca576e2001-06-18 22:08:13 +0000596 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000597
598 /* when tracing we set things up so that
599
600 not (instr_lb <= current_bytecode_offset < instr_ub)
601
602 is true when the line being executed has changed. The
603 initial values are such as to make this false the first
604 time it is tested. */
605 int instr_ub = -1, instr_lb = 0;
606
Guido van Rossumd076c731998-10-07 19:42:25 +0000607 unsigned char *first_instr;
Skip Montanaro04d80f82002-08-04 21:03:35 +0000608 PyObject *names;
609 PyObject *consts;
Guido van Rossum96a42c81992-01-12 02:29:51 +0000610#ifdef LLTRACE
Guido van Rossumacbe8da1993-04-15 15:33:52 +0000611 int lltrace;
Guido van Rossum374a9221991-04-04 10:40:29 +0000612#endif
Guido van Rossum408027e1996-12-30 16:17:54 +0000613#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000614 /* Make it easier to find out where we are with a debugger */
Tim Peters5ca576e2001-06-18 22:08:13 +0000615 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000616#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000617
Neal Norwitza81d2202002-07-14 00:27:26 +0000618/* Tuple access macros */
619
620#ifndef Py_DEBUG
621#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
622#else
623#define GETITEM(v, i) PyTuple_GetItem((v), (i))
624#endif
625
Guido van Rossum374a9221991-04-04 10:40:29 +0000626/* Code access macros */
627
Guido van Rossumd076c731998-10-07 19:42:25 +0000628#define INSTR_OFFSET() (next_instr - first_instr)
Guido van Rossum374a9221991-04-04 10:40:29 +0000629#define NEXTOP() (*next_instr++)
630#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Guido van Rossumd076c731998-10-07 19:42:25 +0000631#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000632#define JUMPBY(x) (next_instr += (x))
633
Raymond Hettingerf606f872003-03-16 03:11:04 +0000634/* OpCode prediction macros
635 Some opcodes tend to come in pairs thus making it possible to predict
636 the second code when the first is run. For example, COMPARE_OP is often
637 followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And, those opcodes are often
638 followed by a POP_TOP.
639
640 Verifying the prediction costs a single high-speed test of register
Raymond Hettingerac2072922003-03-16 15:41:11 +0000641 variable against a constant. If the pairing was good, then the
Raymond Hettingerf606f872003-03-16 03:11:04 +0000642 processor has a high likelihood of making its own successful branch
643 prediction which results in a nearly zero overhead transition to the
644 next opcode.
645
646 A successful prediction saves a trip through the eval-loop including
647 its two unpredictable branches, the HASARG test and the switch-case.
648*/
649
Raymond Hettingerac2072922003-03-16 15:41:11 +0000650#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingerf606f872003-03-16 03:11:04 +0000651#define PREDICTED(op) PRED_##op: next_instr++
Raymond Hettinger7dc52212003-03-16 20:14:44 +0000652#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = (next_instr[2]<<8) + \
653 next_instr[1]; next_instr += 3
Raymond Hettingerf606f872003-03-16 03:11:04 +0000654
Guido van Rossum374a9221991-04-04 10:40:29 +0000655/* Stack manipulation macros */
656
657#define STACK_LEVEL() (stack_pointer - f->f_valuestack)
658#define EMPTY() (STACK_LEVEL() == 0)
659#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000660#define SECOND() (stack_pointer[-2])
661#define THIRD() (stack_pointer[-3])
662#define FOURTH() (stack_pointer[-4])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000663#define SET_TOP(v) (stack_pointer[-1] = (v))
664#define SET_SECOND(v) (stack_pointer[-2] = (v))
665#define SET_THIRD(v) (stack_pointer[-3] = (v))
666#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000667#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000668#define BASIC_PUSH(v) (*stack_pointer++ = (v))
669#define BASIC_POP() (*--stack_pointer)
670
Guido van Rossum96a42c81992-01-12 02:29:51 +0000671#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000672#define PUSH(v) { (void)(BASIC_PUSH(v), \
673 lltrace && prtrace(TOP(), "push")); \
674 assert(STACK_LEVEL() <= f->f_stacksize); }
Fred Drakede26cfc2001-10-13 06:11:28 +0000675#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000676#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
677 lltrace && prtrace(TOP(), "stackadj")); \
678 assert(STACK_LEVEL() <= f->f_stacksize); }
Guido van Rossum374a9221991-04-04 10:40:29 +0000679#else
680#define PUSH(v) BASIC_PUSH(v)
681#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000682#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000683#endif
684
Guido van Rossum681d79a1995-07-18 14:51:37 +0000685/* Local variable macros */
686
687#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000688
689/* The SETLOCAL() macro must not DECREF the local variable in-place and
690 then store the new value; it must copy the old value to a temporary
691 value, then store the new value, and then DECREF the temporary value.
692 This is because it is possible that during the DECREF the frame is
693 accessed by other code (e.g. a __del__ method or gc.collect()) and the
694 variable would be pointing to already-freed memory. */
695#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
696 GETLOCAL(i) = value; \
697 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000698
Guido van Rossuma027efa1997-05-05 20:56:21 +0000699/* Start of code */
700
Tim Peters5ca576e2001-06-18 22:08:13 +0000701 if (f == NULL)
702 return NULL;
703
Armin Rigo1d313ab2003-10-25 14:33:09 +0000704 /* push frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000705 if (Py_EnterRecursiveCall(""))
Armin Rigo1d313ab2003-10-25 14:33:09 +0000706 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000707
Tim Peters5ca576e2001-06-18 22:08:13 +0000708 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000709
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000710 if (tstate->use_tracing) {
711 if (tstate->c_tracefunc != NULL) {
712 /* tstate->c_tracefunc, if defined, is a
713 function that will be called on *every* entry
714 to a code block. Its return value, if not
715 None, is a function that will be called at
716 the start of each executed line of code.
717 (Actually, the function must return itself
718 in order to continue tracing.) The trace
719 functions are called with three arguments:
720 a pointer to the current frame, a string
721 indicating why the function is called, and
722 an argument which depends on the situation.
723 The global trace function is also called
724 whenever an exception is detected. */
725 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
726 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000727 /* Trace function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000728 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000729 }
730 }
731 if (tstate->c_profilefunc != NULL) {
732 /* Similar for c_profilefunc, except it needn't
733 return itself and isn't called for "line" events */
734 if (call_trace(tstate->c_profilefunc,
735 tstate->c_profileobj,
736 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000737 /* Profile function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000738 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000739 }
740 }
741 }
742
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000743 co = f->f_code;
744 names = co->co_names;
745 consts = co->co_consts;
746 fastlocals = f->f_localsplus;
747 freevars = f->f_localsplus + f->f_nlocals;
748 _PyCode_GETCODEPTR(co, &first_instr);
749 /* An explanation is in order for the next line.
750
751 f->f_lasti now refers to the index of the last instruction
752 executed. You might think this was obvious from the name, but
753 this wasn't always true before 2.3! PyFrame_New now sets
754 f->f_lasti to -1 (i.e. the index *before* the first instruction)
755 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
756 does work. Promise. */
757 next_instr = first_instr + f->f_lasti + 1;
758 stack_pointer = f->f_stacktop;
759 assert(stack_pointer != NULL);
760 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
761
Tim Peters5ca576e2001-06-18 22:08:13 +0000762#ifdef LLTRACE
763 lltrace = PyDict_GetItemString(f->f_globals,"__lltrace__") != NULL;
764#endif
765#if defined(Py_DEBUG) || defined(LLTRACE)
766 filename = PyString_AsString(co->co_filename);
767#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000768
Guido van Rossum374a9221991-04-04 10:40:29 +0000769 why = WHY_NOT;
770 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +0000771 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +0000772 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +0000773
Guido van Rossum374a9221991-04-04 10:40:29 +0000774 for (;;) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000775 assert(stack_pointer >= f->f_valuestack); /* else underflow */
776 assert(STACK_LEVEL() <= f->f_stacksize); /* else overflow */
777
Guido van Rossuma027efa1997-05-05 20:56:21 +0000778 /* Do periodic things. Doing this every time through
779 the loop would add too much overhead, so we do it
780 only every Nth instruction. We also do it if
781 ``things_to_do'' is set, i.e. when an asynchronous
782 event needs attention (e.g. a signal handler or
783 async I/O handler); see Py_AddPendingCall() and
784 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000785
Skip Montanarod581d772002-09-03 20:10:45 +0000786 if (--_Py_Ticker < 0) {
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000787 if (*next_instr == SETUP_FINALLY) {
788 /* Make the last opcode before
789 a try: finally: block uninterruptable. */
790 goto fast_next_opcode;
791 }
Skip Montanarod581d772002-09-03 20:10:45 +0000792 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000793 tstate->tick_counter++;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000794 if (things_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +0000795 if (Py_MakePendingCalls() < 0) {
796 why = WHY_EXCEPTION;
797 goto on_error;
798 }
799 }
Jack Janseneddc1442003-11-20 01:44:59 +0000800#if !defined(HAVE_SIGNAL_H)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000801 /* If we have true signals, the signal handler
802 will call Py_AddPendingCall() so we don't
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000803 have to call PyErr_CheckSignals(). On the
804 Mac and DOS, alas, we have to call it. */
Guido van Rossumb209a111997-04-29 18:18:01 +0000805 if (PyErr_CheckSignals()) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000806 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +0000807 goto on_error;
808 }
Guido van Rossum70d44781997-01-21 06:15:24 +0000809#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000810
Guido van Rossume59214e1994-08-30 08:01:59 +0000811#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000812 if (interpreter_lock) {
813 /* Give another thread a chance */
814
Guido van Rossum25ce5661997-08-02 03:10:38 +0000815 if (PyThreadState_Swap(NULL) != tstate)
816 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000817 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000818
819 /* Other threads may run now */
820
Guido van Rossum65d5b571998-12-21 19:32:43 +0000821 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000822 if (PyThreadState_Swap(tstate) != NULL)
823 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000824
825 /* Check for thread interrupts */
826
827 if (tstate->async_exc != NULL) {
828 x = tstate->async_exc;
829 tstate->async_exc = NULL;
830 PyErr_SetNone(x);
831 Py_DECREF(x);
832 why = WHY_EXCEPTION;
833 goto on_error;
834 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000835 }
836#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000837 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000838
Neil Schemenauer63543862002-02-17 19:10:14 +0000839 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +0000840 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +0000841
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000842 /* line-by-line tracing support */
843
844 if (tstate->c_tracefunc != NULL && !tstate->tracing) {
845 /* see maybe_call_line_trace
846 for expository comments */
847 f->f_stacktop = stack_pointer;
Michael W. Hudson006c7522002-11-08 13:08:46 +0000848
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000849 err = maybe_call_line_trace(tstate->c_tracefunc,
850 tstate->c_traceobj,
851 f, &instr_lb, &instr_ub);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000852 /* Reload possibly changed frame fields */
853 JUMPTO(f->f_lasti);
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000854 if (f->f_stacktop != NULL) {
855 stack_pointer = f->f_stacktop;
856 f->f_stacktop = NULL;
857 }
858 if (err) {
859 /* trace function raised an exception */
860 goto on_error;
861 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000862 }
863
864 /* Extract opcode and argument */
865
Guido van Rossum374a9221991-04-04 10:40:29 +0000866 opcode = NEXTOP();
867 if (HAS_ARG(opcode))
868 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +0000869 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +0000870#ifdef DYNAMIC_EXECUTION_PROFILE
871#ifdef DXPAIRS
872 dxpairs[lastopcode][opcode]++;
873 lastopcode = opcode;
874#endif
875 dxp[opcode]++;
876#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000877
Guido van Rossum96a42c81992-01-12 02:29:51 +0000878#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +0000879 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +0000880
Guido van Rossum96a42c81992-01-12 02:29:51 +0000881 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000882 if (HAS_ARG(opcode)) {
883 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000884 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +0000885 }
886 else {
887 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000888 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +0000889 }
890 }
891#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000892
Guido van Rossum374a9221991-04-04 10:40:29 +0000893 /* Main switch on opcode */
Jeremy Hylton52820442001-01-03 23:52:36 +0000894
Guido van Rossum374a9221991-04-04 10:40:29 +0000895 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +0000896
Guido van Rossum374a9221991-04-04 10:40:29 +0000897 /* BEWARE!
898 It is essential that any operation that fails sets either
899 x to NULL, err to nonzero, or why to anything but WHY_NOT,
900 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000901
Guido van Rossum374a9221991-04-04 10:40:29 +0000902 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000903
Neil Schemenauer63543862002-02-17 19:10:14 +0000904 case LOAD_FAST:
905 x = GETLOCAL(oparg);
906 if (x != NULL) {
907 Py_INCREF(x);
908 PUSH(x);
909 goto fast_next_opcode;
910 }
911 format_exc_check_arg(PyExc_UnboundLocalError,
912 UNBOUNDLOCAL_ERROR_MSG,
913 PyTuple_GetItem(co->co_varnames, oparg));
914 break;
915
916 case LOAD_CONST:
Skip Montanaro04d80f82002-08-04 21:03:35 +0000917 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +0000918 Py_INCREF(x);
919 PUSH(x);
920 goto fast_next_opcode;
921
Raymond Hettinger7dc52212003-03-16 20:14:44 +0000922 PREDICTED_WITH_ARG(STORE_FAST);
Neil Schemenauer63543862002-02-17 19:10:14 +0000923 case STORE_FAST:
924 v = POP();
925 SETLOCAL(oparg, v);
926 goto fast_next_opcode;
927
Raymond Hettingerf606f872003-03-16 03:11:04 +0000928 PREDICTED(POP_TOP);
Guido van Rossum374a9221991-04-04 10:40:29 +0000929 case POP_TOP:
930 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000931 Py_DECREF(v);
Neil Schemenauer63543862002-02-17 19:10:14 +0000932 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000933
Guido van Rossum374a9221991-04-04 10:40:29 +0000934 case ROT_TWO:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000935 v = TOP();
936 w = SECOND();
937 SET_TOP(w);
938 SET_SECOND(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000939 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000940
Guido van Rossum374a9221991-04-04 10:40:29 +0000941 case ROT_THREE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000942 v = TOP();
943 w = SECOND();
944 x = THIRD();
945 SET_TOP(w);
946 SET_SECOND(x);
947 SET_THIRD(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000948 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000949
Thomas Wouters434d0822000-08-24 20:11:32 +0000950 case ROT_FOUR:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000951 u = TOP();
952 v = SECOND();
953 w = THIRD();
954 x = FOURTH();
955 SET_TOP(v);
956 SET_SECOND(w);
957 SET_THIRD(x);
958 SET_FOURTH(u);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000959 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000960
Guido van Rossum374a9221991-04-04 10:40:29 +0000961 case DUP_TOP:
962 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000963 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000964 PUSH(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000965 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000966
Thomas Wouters434d0822000-08-24 20:11:32 +0000967 case DUP_TOPX:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000968 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000969 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000970 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000971 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000972 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000973 STACKADJ(2);
974 SET_TOP(x);
975 SET_SECOND(w);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000976 goto fast_next_opcode;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000977 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000978 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000979 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000980 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000981 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000982 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +0000983 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000984 STACKADJ(3);
985 SET_TOP(x);
986 SET_SECOND(w);
987 SET_THIRD(v);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000988 goto fast_next_opcode;
Thomas Wouters434d0822000-08-24 20:11:32 +0000989 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000990 Py_FatalError("invalid argument to DUP_TOPX"
991 " (bytecode corruption?)");
Tim Peters35ba6892000-10-11 07:04:49 +0000992 break;
Thomas Wouters434d0822000-08-24 20:11:32 +0000993
Guido van Rossum374a9221991-04-04 10:40:29 +0000994 case UNARY_POSITIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000995 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000996 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000997 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000998 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000999 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001000 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001001
Guido van Rossum374a9221991-04-04 10:40:29 +00001002 case UNARY_NEGATIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001003 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001004 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001005 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001006 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001007 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001008 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001009
Guido van Rossum374a9221991-04-04 10:40:29 +00001010 case UNARY_NOT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001011 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001012 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001013 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +00001014 if (err == 0) {
1015 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001016 SET_TOP(Py_True);
Guido van Rossumfc490731997-05-06 15:06:49 +00001017 continue;
1018 }
1019 else if (err > 0) {
1020 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001021 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +00001022 err = 0;
1023 continue;
1024 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00001025 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001026 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001027
Guido van Rossum374a9221991-04-04 10:40:29 +00001028 case UNARY_CONVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001029 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001030 x = PyObject_Repr(v);
1031 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001032 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001033 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001034 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001035
Guido van Rossum7928cd71991-10-24 14:59:31 +00001036 case UNARY_INVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001037 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001038 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001039 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001040 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001041 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001042 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001043
Guido van Rossum50564e81996-01-12 01:13:16 +00001044 case BINARY_POWER:
1045 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001046 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001047 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +00001048 Py_DECREF(v);
1049 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001050 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001051 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +00001052 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001053
Guido van Rossum374a9221991-04-04 10:40:29 +00001054 case BINARY_MULTIPLY:
1055 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001056 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001057 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001058 Py_DECREF(v);
1059 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001060 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001061 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001062 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001063
Guido van Rossum374a9221991-04-04 10:40:29 +00001064 case BINARY_DIVIDE:
Tim Peters3caca232001-12-06 06:23:26 +00001065 if (!_Py_QnewFlag) {
1066 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001067 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001068 x = PyNumber_Divide(v, w);
1069 Py_DECREF(v);
1070 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001071 SET_TOP(x);
Tim Peters3caca232001-12-06 06:23:26 +00001072 if (x != NULL) continue;
1073 break;
1074 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001075 /* -Qnew is in effect: fall through to
Tim Peters3caca232001-12-06 06:23:26 +00001076 BINARY_TRUE_DIVIDE */
1077 case BINARY_TRUE_DIVIDE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001078 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001079 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001080 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001081 Py_DECREF(v);
1082 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001083 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001084 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001085 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001086
Guido van Rossum4668b002001-08-08 05:00:18 +00001087 case BINARY_FLOOR_DIVIDE:
1088 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001089 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001090 x = PyNumber_FloorDivide(v, w);
1091 Py_DECREF(v);
1092 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001093 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001094 if (x != NULL) continue;
1095 break;
1096
Guido van Rossum374a9221991-04-04 10:40:29 +00001097 case BINARY_MODULO:
1098 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001099 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001100 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001101 Py_DECREF(v);
1102 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001103 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001104 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001105 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001106
Guido van Rossum374a9221991-04-04 10:40:29 +00001107 case BINARY_ADD:
1108 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001109 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001110 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001111 /* INLINE: int + int */
1112 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001113 a = PyInt_AS_LONG(v);
1114 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001115 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001116 if ((i^a) < 0 && (i^b) < 0)
1117 goto slow_add;
1118 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001119 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001120 else {
1121 slow_add:
Guido van Rossumc12da691997-07-17 23:12:42 +00001122 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001123 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001124 Py_DECREF(v);
1125 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001126 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001127 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001128 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001129
Guido van Rossum374a9221991-04-04 10:40:29 +00001130 case BINARY_SUBTRACT:
1131 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001132 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001133 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001134 /* INLINE: int - int */
1135 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001136 a = PyInt_AS_LONG(v);
1137 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001138 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001139 if ((i^a) < 0 && (i^~b) < 0)
1140 goto slow_sub;
1141 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001142 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001143 else {
1144 slow_sub:
Guido van Rossumc12da691997-07-17 23:12:42 +00001145 x = PyNumber_Subtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001146 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001147 Py_DECREF(v);
1148 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001149 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001150 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001151 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001152
Guido van Rossum374a9221991-04-04 10:40:29 +00001153 case BINARY_SUBSCR:
1154 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001155 v = TOP();
Tim Petersb1c46982001-10-05 20:41:38 +00001156 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001157 /* INLINE: list[int] */
1158 long i = PyInt_AsLong(w);
1159 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +00001160 i += PyList_GET_SIZE(v);
Guido van Rossumc12da691997-07-17 23:12:42 +00001161 if (i < 0 ||
Guido van Rossumfa00e951998-07-08 15:02:37 +00001162 i >= PyList_GET_SIZE(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001163 PyErr_SetString(PyExc_IndexError,
1164 "list index out of range");
1165 x = NULL;
1166 }
1167 else {
Guido van Rossumfa00e951998-07-08 15:02:37 +00001168 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001169 Py_INCREF(x);
1170 }
1171 }
1172 else
1173 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001174 Py_DECREF(v);
1175 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001176 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001177 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001178 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001179
Guido van Rossum7928cd71991-10-24 14:59:31 +00001180 case BINARY_LSHIFT:
1181 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001182 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001183 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001184 Py_DECREF(v);
1185 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001186 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001187 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001188 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001189
Guido van Rossum7928cd71991-10-24 14:59:31 +00001190 case BINARY_RSHIFT:
1191 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001192 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001193 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001194 Py_DECREF(v);
1195 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001196 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001197 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001198 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001199
Guido van Rossum7928cd71991-10-24 14:59:31 +00001200 case BINARY_AND:
1201 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001202 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001203 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001204 Py_DECREF(v);
1205 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001206 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001207 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001208 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001209
Guido van Rossum7928cd71991-10-24 14:59:31 +00001210 case BINARY_XOR:
1211 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001212 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001213 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001214 Py_DECREF(v);
1215 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001216 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001217 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001218 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001219
Guido van Rossum7928cd71991-10-24 14:59:31 +00001220 case BINARY_OR:
1221 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001222 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001223 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001224 Py_DECREF(v);
1225 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001226 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001227 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001228 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001229
1230 case INPLACE_POWER:
1231 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001232 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001233 x = PyNumber_InPlacePower(v, w, Py_None);
1234 Py_DECREF(v);
1235 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001236 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001237 if (x != NULL) continue;
1238 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001239
Thomas Wouters434d0822000-08-24 20:11:32 +00001240 case INPLACE_MULTIPLY:
1241 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001242 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001243 x = PyNumber_InPlaceMultiply(v, w);
1244 Py_DECREF(v);
1245 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001246 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001247 if (x != NULL) continue;
1248 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001249
Thomas Wouters434d0822000-08-24 20:11:32 +00001250 case INPLACE_DIVIDE:
Tim Peters54b11912001-12-25 18:49:11 +00001251 if (!_Py_QnewFlag) {
1252 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001253 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001254 x = PyNumber_InPlaceDivide(v, w);
1255 Py_DECREF(v);
1256 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001257 SET_TOP(x);
Tim Peters54b11912001-12-25 18:49:11 +00001258 if (x != NULL) continue;
1259 break;
1260 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001261 /* -Qnew is in effect: fall through to
Tim Peters54b11912001-12-25 18:49:11 +00001262 INPLACE_TRUE_DIVIDE */
1263 case INPLACE_TRUE_DIVIDE:
Thomas Wouters434d0822000-08-24 20:11:32 +00001264 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001265 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001266 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001267 Py_DECREF(v);
1268 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001269 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001270 if (x != NULL) continue;
1271 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001272
Guido van Rossum4668b002001-08-08 05:00:18 +00001273 case INPLACE_FLOOR_DIVIDE:
1274 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001275 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001276 x = PyNumber_InPlaceFloorDivide(v, w);
1277 Py_DECREF(v);
1278 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001279 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001280 if (x != NULL) continue;
1281 break;
1282
Thomas Wouters434d0822000-08-24 20:11:32 +00001283 case INPLACE_MODULO:
1284 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001285 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001286 x = PyNumber_InPlaceRemainder(v, w);
1287 Py_DECREF(v);
1288 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001289 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001290 if (x != NULL) continue;
1291 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001292
Thomas Wouters434d0822000-08-24 20:11:32 +00001293 case INPLACE_ADD:
1294 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001295 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001296 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001297 /* INLINE: int + int */
1298 register long a, b, i;
1299 a = PyInt_AS_LONG(v);
1300 b = PyInt_AS_LONG(w);
1301 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001302 if ((i^a) < 0 && (i^b) < 0)
1303 goto slow_iadd;
1304 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001305 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001306 else {
1307 slow_iadd:
Thomas Wouters434d0822000-08-24 20:11:32 +00001308 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001309 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001310 Py_DECREF(v);
1311 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001312 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001313 if (x != NULL) continue;
1314 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001315
Thomas Wouters434d0822000-08-24 20:11:32 +00001316 case INPLACE_SUBTRACT:
1317 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001318 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001319 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001320 /* INLINE: int - int */
1321 register long a, b, i;
1322 a = PyInt_AS_LONG(v);
1323 b = PyInt_AS_LONG(w);
1324 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001325 if ((i^a) < 0 && (i^~b) < 0)
1326 goto slow_isub;
1327 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001328 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001329 else {
1330 slow_isub:
Thomas Wouters434d0822000-08-24 20:11:32 +00001331 x = PyNumber_InPlaceSubtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001332 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001333 Py_DECREF(v);
1334 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001335 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001336 if (x != NULL) continue;
1337 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001338
Thomas Wouters434d0822000-08-24 20:11:32 +00001339 case INPLACE_LSHIFT:
1340 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001341 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001342 x = PyNumber_InPlaceLshift(v, w);
1343 Py_DECREF(v);
1344 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001345 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001346 if (x != NULL) continue;
1347 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001348
Thomas Wouters434d0822000-08-24 20:11:32 +00001349 case INPLACE_RSHIFT:
1350 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001351 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001352 x = PyNumber_InPlaceRshift(v, w);
1353 Py_DECREF(v);
1354 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001355 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001356 if (x != NULL) continue;
1357 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001358
Thomas Wouters434d0822000-08-24 20:11:32 +00001359 case INPLACE_AND:
1360 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001361 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001362 x = PyNumber_InPlaceAnd(v, w);
1363 Py_DECREF(v);
1364 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001365 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001366 if (x != NULL) continue;
1367 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001368
Thomas Wouters434d0822000-08-24 20:11:32 +00001369 case INPLACE_XOR:
1370 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001371 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001372 x = PyNumber_InPlaceXor(v, w);
1373 Py_DECREF(v);
1374 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001375 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001376 if (x != NULL) continue;
1377 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001378
Thomas Wouters434d0822000-08-24 20:11:32 +00001379 case INPLACE_OR:
1380 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001381 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001382 x = PyNumber_InPlaceOr(v, w);
1383 Py_DECREF(v);
1384 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001385 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001386 if (x != NULL) continue;
1387 break;
1388
Guido van Rossum374a9221991-04-04 10:40:29 +00001389 case SLICE+0:
1390 case SLICE+1:
1391 case SLICE+2:
1392 case SLICE+3:
1393 if ((opcode-SLICE) & 2)
1394 w = POP();
1395 else
1396 w = NULL;
1397 if ((opcode-SLICE) & 1)
1398 v = POP();
1399 else
1400 v = NULL;
Raymond Hettinger663004b2003-01-09 15:24:30 +00001401 u = TOP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001402 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001403 Py_DECREF(u);
1404 Py_XDECREF(v);
1405 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001406 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001407 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001408 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001409
Guido van Rossum374a9221991-04-04 10:40:29 +00001410 case STORE_SLICE+0:
1411 case STORE_SLICE+1:
1412 case STORE_SLICE+2:
1413 case STORE_SLICE+3:
1414 if ((opcode-STORE_SLICE) & 2)
1415 w = POP();
1416 else
1417 w = NULL;
1418 if ((opcode-STORE_SLICE) & 1)
1419 v = POP();
1420 else
1421 v = NULL;
1422 u = POP();
1423 t = POP();
1424 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +00001425 Py_DECREF(t);
1426 Py_DECREF(u);
1427 Py_XDECREF(v);
1428 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001429 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001430 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001431
Guido van Rossum374a9221991-04-04 10:40:29 +00001432 case DELETE_SLICE+0:
1433 case DELETE_SLICE+1:
1434 case DELETE_SLICE+2:
1435 case DELETE_SLICE+3:
1436 if ((opcode-DELETE_SLICE) & 2)
1437 w = POP();
1438 else
1439 w = NULL;
1440 if ((opcode-DELETE_SLICE) & 1)
1441 v = POP();
1442 else
1443 v = NULL;
1444 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001445 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +00001446 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00001447 Py_DECREF(u);
1448 Py_XDECREF(v);
1449 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001450 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001451 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001452
Guido van Rossum374a9221991-04-04 10:40:29 +00001453 case STORE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001454 w = TOP();
1455 v = SECOND();
1456 u = THIRD();
1457 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001458 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001459 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001460 Py_DECREF(u);
1461 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;
Guido van Rossumac7be682001-01-17 15:42:30 +00001465
Guido van Rossum374a9221991-04-04 10:40:29 +00001466 case DELETE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001467 w = TOP();
1468 v = SECOND();
1469 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001470 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001471 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001472 Py_DECREF(v);
1473 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001474 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001475 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001476
Guido van Rossum374a9221991-04-04 10:40:29 +00001477 case PRINT_EXPR:
1478 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001479 w = PySys_GetObject("displayhook");
1480 if (w == NULL) {
1481 PyErr_SetString(PyExc_RuntimeError,
1482 "lost sys.displayhook");
1483 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001484 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001485 }
1486 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001487 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001488 if (x == NULL)
1489 err = -1;
1490 }
1491 if (err == 0) {
1492 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001493 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001494 if (w == NULL)
1495 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001496 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001497 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001498 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001499 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001500
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001501 case PRINT_ITEM_TO:
1502 w = stream = POP();
1503 /* fall through to PRINT_ITEM */
1504
Guido van Rossum374a9221991-04-04 10:40:29 +00001505 case PRINT_ITEM:
1506 v = POP();
Barry Warsaw093abe02000-08-29 04:56:13 +00001507 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001508 w = PySys_GetObject("stdout");
1509 if (w == NULL) {
1510 PyErr_SetString(PyExc_RuntimeError,
1511 "lost sys.stdout");
1512 err = -1;
1513 }
Guido van Rossum8f183201997-12-31 05:53:15 +00001514 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001515 /* PyFile_SoftSpace() can exececute arbitrary code
1516 if sys.stdout is an instance with a __getattr__.
1517 If __getattr__ raises an exception, w will
1518 be freed, so we need to prevent that temporarily. */
1519 Py_XINCREF(w);
Tim Peters8e5fd532002-03-24 19:25:00 +00001520 if (w != NULL && PyFile_SoftSpace(w, 0))
Guido van Rossumbe270261997-05-22 22:26:18 +00001521 err = PyFile_WriteString(" ", w);
1522 if (err == 0)
1523 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001524 if (err == 0) {
Tim Peters8e5fd532002-03-24 19:25:00 +00001525 /* XXX move into writeobject() ? */
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001526 if (PyString_Check(v)) {
1527 char *s = PyString_AS_STRING(v);
1528 int len = PyString_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001529 if (len == 0 ||
1530 !isspace(Py_CHARMASK(s[len-1])) ||
1531 s[len-1] == ' ')
1532 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001533 }
Martin v. Löwis8d3ce5a2001-12-18 22:36:40 +00001534#ifdef Py_USING_UNICODE
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001535 else if (PyUnicode_Check(v)) {
1536 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
1537 int len = PyUnicode_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001538 if (len == 0 ||
1539 !Py_UNICODE_ISSPACE(s[len-1]) ||
1540 s[len-1] == ' ')
1541 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001542 }
Michael W. Hudsond95c8282002-05-20 13:56:11 +00001543#endif
Tim Peters8e5fd532002-03-24 19:25:00 +00001544 else
1545 PyFile_SoftSpace(w, 1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001546 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001547 Py_XDECREF(w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001548 Py_DECREF(v);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001549 Py_XDECREF(stream);
1550 stream = NULL;
1551 if (err == 0)
1552 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001553 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001554
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001555 case PRINT_NEWLINE_TO:
1556 w = stream = POP();
1557 /* fall through to PRINT_NEWLINE */
1558
Guido van Rossum374a9221991-04-04 10:40:29 +00001559 case PRINT_NEWLINE:
Barry Warsaw093abe02000-08-29 04:56:13 +00001560 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001561 w = PySys_GetObject("stdout");
1562 if (w == NULL)
1563 PyErr_SetString(PyExc_RuntimeError,
1564 "lost sys.stdout");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001565 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001566 if (w != NULL) {
1567 err = PyFile_WriteString("\n", w);
1568 if (err == 0)
1569 PyFile_SoftSpace(w, 0);
1570 }
1571 Py_XDECREF(stream);
1572 stream = NULL;
Guido van Rossum374a9221991-04-04 10:40:29 +00001573 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001574
Thomas Wouters434d0822000-08-24 20:11:32 +00001575
1576#ifdef CASE_TOO_BIG
1577 default: switch (opcode) {
1578#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001579 case BREAK_LOOP:
1580 why = WHY_BREAK;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001581 goto fast_block_end;
Guido van Rossum66b0e9c2001-03-21 19:17:22 +00001582
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00001583 case CONTINUE_LOOP:
1584 retval = PyInt_FromLong(oparg);
1585 why = WHY_CONTINUE;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001586 goto fast_block_end;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001587
Guido van Rossumf10570b1995-07-07 22:53:21 +00001588 case RAISE_VARARGS:
1589 u = v = w = NULL;
1590 switch (oparg) {
1591 case 3:
1592 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001593 /* Fallthrough */
1594 case 2:
1595 v = POP(); /* value */
1596 /* Fallthrough */
1597 case 1:
1598 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001599 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001600 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001601 break;
1602 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001603 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001604 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001605 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001606 break;
1607 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001608 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001609
Guido van Rossum374a9221991-04-04 10:40:29 +00001610 case LOAD_LOCALS:
Guido van Rossum681d79a1995-07-18 14:51:37 +00001611 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001612 PyErr_SetString(PyExc_SystemError,
1613 "no locals");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001614 break;
1615 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001616 Py_INCREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001617 PUSH(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001618 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001619
Guido van Rossum374a9221991-04-04 10:40:29 +00001620 case RETURN_VALUE:
1621 retval = POP();
1622 why = WHY_RETURN;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001623 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001624
Tim Peters5ca576e2001-06-18 22:08:13 +00001625 case YIELD_VALUE:
1626 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001627 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001628 why = WHY_YIELD;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001629 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001630
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001631 case EXEC_STMT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001632 w = TOP();
1633 v = SECOND();
1634 u = THIRD();
1635 STACKADJ(-3);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001636 err = exec_statement(f, u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001637 Py_DECREF(u);
1638 Py_DECREF(v);
1639 Py_DECREF(w);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001640 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001641
Guido van Rossum374a9221991-04-04 10:40:29 +00001642 case POP_BLOCK:
1643 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001644 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001645 while (STACK_LEVEL() > b->b_level) {
1646 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001647 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001648 }
1649 }
1650 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001651
Guido van Rossum374a9221991-04-04 10:40:29 +00001652 case END_FINALLY:
1653 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001654 if (PyInt_Check(v)) {
Raymond Hettinger080cb322003-03-14 01:37:42 +00001655 why = (enum why_code) PyInt_AS_LONG(v);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00001656 if (why == WHY_RETURN ||
Tim Peters5ca576e2001-06-18 22:08:13 +00001657 why == WHY_YIELD ||
Guido van Rossumc5fe5eb2002-06-12 03:45:21 +00001658 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001659 retval = POP();
1660 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001661 else if (PyString_Check(v) || PyClass_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001662 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001663 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001664 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001665 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001666 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001667 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001668 else if (v != Py_None) {
1669 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001670 "'finally' pops bad exception");
1671 why = WHY_EXCEPTION;
1672 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001673 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001674 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001675
Guido van Rossum374a9221991-04-04 10:40:29 +00001676 case BUILD_CLASS:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001677 u = TOP();
1678 v = SECOND();
1679 w = THIRD();
1680 STACKADJ(-2);
Guido van Rossum25831651993-05-19 14:50:45 +00001681 x = build_class(u, v, w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001682 SET_TOP(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001683 Py_DECREF(u);
1684 Py_DECREF(v);
1685 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001686 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001687
Guido van Rossum374a9221991-04-04 10:40:29 +00001688 case STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001689 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001690 v = POP();
Guido van Rossum681d79a1995-07-18 14:51:37 +00001691 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001692 PyErr_Format(PyExc_SystemError,
1693 "no locals found when storing %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001694 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001695 break;
1696 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001697 err = PyDict_SetItem(x, w, v);
1698 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001699 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001700
Guido van Rossum374a9221991-04-04 10:40:29 +00001701 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001702 w = GETITEM(names, oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001703 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001704 PyErr_Format(PyExc_SystemError,
1705 "no locals when deleting %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001706 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001707 break;
1708 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001709 if ((err = PyDict_DelItem(x, w)) != 0)
Guido van Rossumac7be682001-01-17 15:42:30 +00001710 format_exc_check_arg(PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001711 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001712 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001713
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001714 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001715 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001716 v = POP();
Raymond Hettinger21012b82003-02-26 18:11:50 +00001717 if (PyTuple_CheckExact(v)) {
Barry Warsawe42b18f1997-08-25 22:13:04 +00001718 if (PyTuple_Size(v) != oparg) {
1719 PyErr_SetString(PyExc_ValueError,
1720 "unpack tuple of wrong size");
1721 why = WHY_EXCEPTION;
1722 }
1723 else {
1724 for (; --oparg >= 0; ) {
1725 w = PyTuple_GET_ITEM(v, oparg);
1726 Py_INCREF(w);
1727 PUSH(w);
1728 }
1729 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001730 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00001731 else if (PyList_CheckExact(v)) {
Barry Warsawe42b18f1997-08-25 22:13:04 +00001732 if (PyList_Size(v) != oparg) {
1733 PyErr_SetString(PyExc_ValueError,
1734 "unpack list of wrong size");
1735 why = WHY_EXCEPTION;
1736 }
1737 else {
1738 for (; --oparg >= 0; ) {
1739 w = PyList_GET_ITEM(v, oparg);
1740 Py_INCREF(w);
1741 PUSH(w);
1742 }
1743 }
1744 }
Tim Petersd6d010b2001-06-21 02:49:55 +00001745 else if (unpack_iterable(v, oparg,
1746 stack_pointer + oparg))
1747 stack_pointer += oparg;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001748 else {
1749 if (PyErr_ExceptionMatches(PyExc_TypeError))
1750 PyErr_SetString(PyExc_TypeError,
1751 "unpack non-sequence");
Barry Warsawe42b18f1997-08-25 22:13:04 +00001752 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001753 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001754 Py_DECREF(v);
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 STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001758 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001759 v = TOP();
1760 u = SECOND();
1761 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001762 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1763 Py_DECREF(v);
1764 Py_DECREF(u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001765 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001766
Guido van Rossum374a9221991-04-04 10:40:29 +00001767 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001768 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001769 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001770 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1771 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001772 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001773 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001774
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001775 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001776 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001777 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001778 err = PyDict_SetItem(f->f_globals, w, v);
1779 Py_DECREF(v);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001780 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001781
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001782 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001783 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001784 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001785 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001786 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001787 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001788
Guido van Rossum374a9221991-04-04 10:40:29 +00001789 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001790 w = GETITEM(names, oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001791 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001792 PyErr_Format(PyExc_SystemError,
1793 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001794 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001795 break;
1796 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001797 x = PyDict_GetItem(x, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001798 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001799 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001800 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001801 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001802 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001803 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001804 PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001805 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001806 break;
1807 }
1808 }
1809 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001810 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001811 PUSH(x);
1812 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001813
Guido van Rossum374a9221991-04-04 10:40:29 +00001814 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001815 w = GETITEM(names, oparg);
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001816 if (PyString_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00001817 /* Inline the PyDict_GetItem() calls.
1818 WARNING: this is an extreme speed hack.
1819 Do not try this at home. */
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001820 long hash = ((PyStringObject *)w)->ob_shash;
1821 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001822 PyDictObject *d;
1823 d = (PyDictObject *)(f->f_globals);
1824 x = d->ma_lookup(d, w, hash)->me_value;
1825 if (x != NULL) {
1826 Py_INCREF(x);
1827 PUSH(x);
1828 continue;
1829 }
1830 d = (PyDictObject *)(f->f_builtins);
1831 x = d->ma_lookup(d, w, hash)->me_value;
1832 if (x != NULL) {
1833 Py_INCREF(x);
1834 PUSH(x);
1835 continue;
1836 }
1837 goto load_global_error;
1838 }
1839 }
1840 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00001841 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001842 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001843 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001844 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001845 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00001846 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001847 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001848 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001849 break;
1850 }
1851 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001852 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001853 PUSH(x);
1854 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001855
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001856 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001857 x = GETLOCAL(oparg);
1858 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001859 format_exc_check_arg(
1860 PyExc_UnboundLocalError,
1861 UNBOUNDLOCAL_ERROR_MSG,
1862 PyTuple_GetItem(co->co_varnames, oparg)
1863 );
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001864 break;
1865 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00001866 SETLOCAL(oparg, NULL);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001867 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001868
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001869 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001870 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001871 Py_INCREF(x);
1872 PUSH(x);
1873 break;
1874
1875 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001876 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001877 w = PyCell_Get(x);
Jeremy Hylton2524d692001-02-05 17:23:16 +00001878 if (w == NULL) {
Jeremy Hylton76c81ee2002-07-11 16:56:38 +00001879 err = -1;
1880 /* Don't stomp existing exception */
1881 if (PyErr_Occurred())
1882 break;
Jeremy Hyltonc76770c2001-04-13 16:51:46 +00001883 if (oparg < f->f_ncells) {
Jeremy Hylton2524d692001-02-05 17:23:16 +00001884 v = PyTuple_GetItem(co->co_cellvars,
1885 oparg);
Jeremy Hyltonc76770c2001-04-13 16:51:46 +00001886 format_exc_check_arg(
1887 PyExc_UnboundLocalError,
1888 UNBOUNDLOCAL_ERROR_MSG,
1889 v);
1890 } else {
Jeremy Hylton2524d692001-02-05 17:23:16 +00001891 v = PyTuple_GetItem(
1892 co->co_freevars,
1893 oparg - f->f_ncells);
Jeremy Hyltonc76770c2001-04-13 16:51:46 +00001894 format_exc_check_arg(
1895 PyExc_NameError,
1896 UNBOUNDFREE_ERROR_MSG,
1897 v);
1898 }
Jeremy Hylton2524d692001-02-05 17:23:16 +00001899 break;
1900 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001901 PUSH(w);
1902 break;
1903
1904 case STORE_DEREF:
1905 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001906 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001907 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00001908 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001909 continue;
1910
Guido van Rossum374a9221991-04-04 10:40:29 +00001911 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001912 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001913 if (x != NULL) {
1914 for (; --oparg >= 0;) {
1915 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001916 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001917 }
1918 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001919 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001920 }
1921 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001922
Guido van Rossum374a9221991-04-04 10:40:29 +00001923 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00001924 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001925 if (x != NULL) {
1926 for (; --oparg >= 0;) {
1927 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00001928 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001929 }
1930 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001931 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001932 }
1933 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001934
Guido van Rossum374a9221991-04-04 10:40:29 +00001935 case BUILD_MAP:
Guido van Rossumb209a111997-04-29 18:18:01 +00001936 x = PyDict_New();
Guido van Rossum374a9221991-04-04 10:40:29 +00001937 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001938 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001939 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001940
Guido van Rossum374a9221991-04-04 10:40:29 +00001941 case LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001942 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001943 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001944 x = PyObject_GetAttr(v, w);
1945 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001946 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001947 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001948 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001949
Guido van Rossum374a9221991-04-04 10:40:29 +00001950 case COMPARE_OP:
1951 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001952 v = TOP();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001953 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001954 /* INLINE: cmp(int, int) */
1955 register long a, b;
1956 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001957 a = PyInt_AS_LONG(v);
1958 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001959 switch (oparg) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00001960 case PyCmp_LT: res = a < b; break;
1961 case PyCmp_LE: res = a <= b; break;
1962 case PyCmp_EQ: res = a == b; break;
1963 case PyCmp_NE: res = a != b; break;
1964 case PyCmp_GT: res = a > b; break;
1965 case PyCmp_GE: res = a >= b; break;
1966 case PyCmp_IS: res = v == w; break;
1967 case PyCmp_IS_NOT: res = v != w; break;
Guido van Rossumc12da691997-07-17 23:12:42 +00001968 default: goto slow_compare;
1969 }
1970 x = res ? Py_True : Py_False;
1971 Py_INCREF(x);
1972 }
1973 else {
1974 slow_compare:
1975 x = cmp_outcome(oparg, v, w);
1976 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001977 Py_DECREF(v);
1978 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001979 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001980 if (x == NULL) break;
1981 PREDICT(JUMP_IF_FALSE);
1982 PREDICT(JUMP_IF_TRUE);
1983 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001984
Guido van Rossum374a9221991-04-04 10:40:29 +00001985 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001986 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001987 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001988 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001989 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00001990 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001991 break;
1992 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001993 u = TOP();
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001994 w = PyTuple_Pack(4,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001995 w,
1996 f->f_globals,
Guido van Rossuma027efa1997-05-05 20:56:21 +00001997 f->f_locals == NULL ?
1998 Py_None : f->f_locals,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001999 u);
Guido van Rossumb209a111997-04-29 18:18:01 +00002000 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002001 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002002 u = POP();
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002003 x = NULL;
2004 break;
2005 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002006 x = PyEval_CallObject(x, w);
2007 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002008 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002009 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002010 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002011
Thomas Wouters52152252000-08-17 22:55:00 +00002012 case IMPORT_STAR:
2013 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002014 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002015 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002016 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00002017 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002018 break;
2019 }
Thomas Wouters52152252000-08-17 22:55:00 +00002020 err = import_all_from(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002021 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00002022 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002023 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002024 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002025
Thomas Wouters52152252000-08-17 22:55:00 +00002026 case IMPORT_FROM:
Skip Montanaro496e6582002-08-06 17:47:40 +00002027 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00002028 v = TOP();
2029 x = import_from(v, w);
2030 PUSH(x);
2031 if (x != NULL) continue;
2032 break;
2033
Guido van Rossum374a9221991-04-04 10:40:29 +00002034 case JUMP_FORWARD:
2035 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002036 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002037
Raymond Hettingerf606f872003-03-16 03:11:04 +00002038 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002039 case JUMP_IF_FALSE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002040 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002041 if (w == Py_True) {
2042 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002043 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002044 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002045 if (w == Py_False) {
2046 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002047 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002048 }
2049 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002050 if (err > 0)
2051 err = 0;
2052 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00002053 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002054 else
2055 break;
2056 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002057
Raymond Hettingerf606f872003-03-16 03:11:04 +00002058 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002059 case JUMP_IF_TRUE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002060 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002061 if (w == Py_False) {
2062 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002063 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002064 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002065 if (w == Py_True) {
2066 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002067 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002068 }
2069 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002070 if (err > 0) {
2071 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00002072 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002073 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002074 else if (err == 0)
2075 ;
2076 else
2077 break;
2078 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002079
Guido van Rossum374a9221991-04-04 10:40:29 +00002080 case JUMP_ABSOLUTE:
2081 JUMPTO(oparg);
Neil Schemenauerca2a2f12003-05-30 23:59:44 +00002082 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002083
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002084 case GET_ITER:
2085 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002086 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002087 x = PyObject_GetIter(v);
2088 Py_DECREF(v);
2089 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002090 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002091 PREDICT(FOR_ITER);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002092 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002093 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002094 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002095 break;
2096
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002097 PREDICTED_WITH_ARG(FOR_ITER);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002098 case FOR_ITER:
2099 /* before: [iter]; after: [iter, iter()] *or* [] */
2100 v = TOP();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002101 x = PyIter_Next(v);
2102 if (x != NULL) {
2103 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002104 PREDICT(STORE_FAST);
2105 PREDICT(UNPACK_SEQUENCE);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002106 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002107 }
Tim Petersf4848da2001-05-05 00:14:56 +00002108 if (!PyErr_Occurred()) {
2109 /* iterator ended normally */
2110 x = v = POP();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002111 Py_DECREF(v);
2112 JUMPBY(oparg);
2113 continue;
2114 }
2115 break;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002116
Guido van Rossum374a9221991-04-04 10:40:29 +00002117 case SETUP_LOOP:
2118 case SETUP_EXCEPT:
2119 case SETUP_FINALLY:
Guido van Rossumb209a111997-04-29 18:18:01 +00002120 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002121 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002122 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002123
Guido van Rossumf10570b1995-07-07 22:53:21 +00002124 case CALL_FUNCTION:
Jeremy Hylton985eba52003-02-05 23:13:00 +00002125 PCALL(PCALL_ALL);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002126 x = call_function(&stack_pointer, oparg);
2127 PUSH(x);
2128 if (x != NULL)
2129 continue;
2130 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002131
Jeremy Hylton76901512000-03-28 23:49:17 +00002132 case CALL_FUNCTION_VAR:
2133 case CALL_FUNCTION_KW:
2134 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002135 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002136 int na = oparg & 0xff;
2137 int nk = (oparg>>8) & 0xff;
2138 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002139 int n = na + 2 * nk;
2140 PyObject **pfunc, *func;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002141 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002142 if (flags & CALL_FLAG_VAR)
2143 n++;
2144 if (flags & CALL_FLAG_KW)
2145 n++;
2146 pfunc = stack_pointer - n - 1;
2147 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002148
Guido van Rossumac7be682001-01-17 15:42:30 +00002149 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002150 && PyMethod_GET_SELF(func) != NULL) {
2151 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002152 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002153 func = PyMethod_GET_FUNCTION(func);
2154 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002155 Py_DECREF(*pfunc);
2156 *pfunc = self;
2157 na++;
2158 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002159 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002160 Py_INCREF(func);
2161 x = ext_do_call(func, &stack_pointer, flags, na, nk);
Jeremy Hylton76901512000-03-28 23:49:17 +00002162 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002163
Jeremy Hylton76901512000-03-28 23:49:17 +00002164 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002165 w = POP();
2166 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002167 }
2168 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002169 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002170 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002171 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002172 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002173
Guido van Rossum681d79a1995-07-18 14:51:37 +00002174 case MAKE_FUNCTION:
2175 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002176 x = PyFunction_New(v, f->f_globals);
2177 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002178 /* XXX Maybe this should be a separate opcode? */
2179 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002180 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002181 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002182 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002183 x = NULL;
2184 break;
2185 }
2186 while (--oparg >= 0) {
2187 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002188 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002189 }
2190 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002191 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002192 }
2193 PUSH(x);
2194 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002195
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002196 case MAKE_CLOSURE:
2197 {
2198 int nfree;
2199 v = POP(); /* code object */
2200 x = PyFunction_New(v, f->f_globals);
Jeremy Hylton733c8932001-12-13 19:51:56 +00002201 nfree = PyCode_GetNumFree((PyCodeObject *)v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002202 Py_DECREF(v);
2203 /* XXX Maybe this should be a separate opcode? */
2204 if (x != NULL && nfree > 0) {
2205 v = PyTuple_New(nfree);
2206 if (v == NULL) {
2207 Py_DECREF(x);
2208 x = NULL;
2209 break;
2210 }
2211 while (--nfree >= 0) {
2212 w = POP();
2213 PyTuple_SET_ITEM(v, nfree, w);
2214 }
2215 err = PyFunction_SetClosure(x, v);
2216 Py_DECREF(v);
2217 }
2218 if (x != NULL && oparg > 0) {
2219 v = PyTuple_New(oparg);
2220 if (v == NULL) {
2221 Py_DECREF(x);
2222 x = NULL;
2223 break;
2224 }
2225 while (--oparg >= 0) {
2226 w = POP();
2227 PyTuple_SET_ITEM(v, oparg, w);
2228 }
2229 err = PyFunction_SetDefaults(x, v);
2230 Py_DECREF(v);
2231 }
2232 PUSH(x);
2233 break;
2234 }
2235
Guido van Rossum8861b741996-07-30 16:49:37 +00002236 case BUILD_SLICE:
2237 if (oparg == 3)
2238 w = POP();
2239 else
2240 w = NULL;
2241 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002242 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002243 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002244 Py_DECREF(u);
2245 Py_DECREF(v);
2246 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002247 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002248 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002249 break;
2250
Fred Drakeef8ace32000-08-24 00:32:09 +00002251 case EXTENDED_ARG:
2252 opcode = NEXTOP();
2253 oparg = oparg<<16 | NEXTARG();
2254 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002255
Guido van Rossum374a9221991-04-04 10:40:29 +00002256 default:
2257 fprintf(stderr,
2258 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002259 PyCode_Addr2Line(f->f_code, f->f_lasti),
2260 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002261 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002262 why = WHY_EXCEPTION;
2263 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002264
2265#ifdef CASE_TOO_BIG
2266 }
2267#endif
2268
Guido van Rossum374a9221991-04-04 10:40:29 +00002269 } /* switch */
2270
2271 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002272
Guido van Rossum374a9221991-04-04 10:40:29 +00002273 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002274
Guido van Rossum374a9221991-04-04 10:40:29 +00002275 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002276 if (err == 0 && x != NULL) {
2277#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002278 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002279 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002280 fprintf(stderr,
2281 "XXX undetected error\n");
2282 else
2283#endif
2284 continue; /* Normal, fast path */
2285 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002286 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002287 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002288 err = 0;
2289 }
2290
Guido van Rossum374a9221991-04-04 10:40:29 +00002291 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002292
Guido van Rossum374a9221991-04-04 10:40:29 +00002293 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002294 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002295 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002296 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002297 why = WHY_EXCEPTION;
2298 }
2299 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002300#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002301 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002302 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002303 if (PyErr_Occurred()) {
Jeremy Hylton904ed862003-11-05 17:29:35 +00002304 char buf[1024];
2305 sprintf(buf, "Stack unwind with exception "
2306 "set and why=%d", why);
2307 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002308 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002309 }
2310#endif
2311
2312 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002313
Guido van Rossum374a9221991-04-04 10:40:29 +00002314 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002315 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002316
Fred Drake8f51f542001-10-04 14:48:42 +00002317 if (tstate->c_tracefunc != NULL)
2318 call_exc_trace(tstate->c_tracefunc,
2319 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002320 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002321
Guido van Rossum374a9221991-04-04 10:40:29 +00002322 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002323
Guido van Rossum374a9221991-04-04 10:40:29 +00002324 if (why == WHY_RERAISE)
2325 why = WHY_EXCEPTION;
2326
2327 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002328
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002329fast_block_end:
Tim Peters5ca576e2001-06-18 22:08:13 +00002330 while (why != WHY_NOT && why != WHY_YIELD && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002331 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002332
2333 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2334 /* For a continue inside a try block,
2335 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002336 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2337 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002338 why = WHY_NOT;
2339 JUMPTO(PyInt_AS_LONG(retval));
2340 Py_DECREF(retval);
2341 break;
2342 }
2343
Guido van Rossum374a9221991-04-04 10:40:29 +00002344 while (STACK_LEVEL() > b->b_level) {
2345 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002346 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002347 }
2348 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2349 why = WHY_NOT;
2350 JUMPTO(b->b_handler);
2351 break;
2352 }
2353 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002354 (b->b_type == SETUP_EXCEPT &&
2355 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002356 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002357 PyObject *exc, *val, *tb;
2358 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002359 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002360 val = Py_None;
2361 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002362 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002363 /* Make the raw exception data
2364 available to the handler,
2365 so a program can emulate the
2366 Python main loop. Don't do
2367 this for 'finally'. */
2368 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002369 PyErr_NormalizeException(
2370 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002371 set_exc_info(tstate,
2372 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002373 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002374 if (tb == NULL) {
2375 Py_INCREF(Py_None);
2376 PUSH(Py_None);
2377 } else
2378 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002379 PUSH(val);
2380 PUSH(exc);
2381 }
2382 else {
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002383 if (why == WHY_RETURN ||
Guido van Rossumc5fe5eb2002-06-12 03:45:21 +00002384 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00002385 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002386 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002387 PUSH(v);
2388 }
2389 why = WHY_NOT;
2390 JUMPTO(b->b_handler);
2391 break;
2392 }
2393 } /* unwind stack */
2394
2395 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002396
Guido van Rossum374a9221991-04-04 10:40:29 +00002397 if (why != WHY_NOT)
2398 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002399
Guido van Rossum374a9221991-04-04 10:40:29 +00002400 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002401
Guido van Rossum35974fb2001-12-06 21:28:18 +00002402 if (why != WHY_YIELD) {
2403 /* Pop remaining stack entries -- but when yielding */
2404 while (!EMPTY()) {
2405 v = POP();
2406 Py_XDECREF(v);
2407 }
2408 }
2409
Tim Peters5ca576e2001-06-18 22:08:13 +00002410 if (why != WHY_RETURN && why != WHY_YIELD)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002411 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002412
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002413fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002414 if (tstate->use_tracing) {
2415 if (tstate->c_tracefunc
2416 && (why == WHY_RETURN || why == WHY_YIELD)) {
2417 if (call_trace(tstate->c_tracefunc,
2418 tstate->c_traceobj, f,
2419 PyTrace_RETURN, retval)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002420 Py_XDECREF(retval);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002421 retval = NULL;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002422 why = WHY_EXCEPTION;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002423 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002424 }
Fred Drake8f51f542001-10-04 14:48:42 +00002425 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002426 if (why == WHY_EXCEPTION)
2427 call_trace_protected(tstate->c_profilefunc,
2428 tstate->c_profileobj, f,
2429 PyTrace_RETURN);
2430 else if (call_trace(tstate->c_profilefunc,
2431 tstate->c_profileobj, f,
2432 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002433 Py_XDECREF(retval);
2434 retval = NULL;
2435 why = WHY_EXCEPTION;
2436 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002437 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002438 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002439
Guido van Rossuma027efa1997-05-05 20:56:21 +00002440 reset_exc_info(tstate);
2441
Tim Peters5ca576e2001-06-18 22:08:13 +00002442 /* pop frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +00002443 exit_eval_frame:
2444 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002445 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002446
Guido van Rossum96a42c81992-01-12 02:29:51 +00002447 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002448}
2449
Tim Peters6d6c1a32001-08-02 04:15:00 +00002450PyObject *
2451PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002452 PyObject **args, int argcount, PyObject **kws, int kwcount,
2453 PyObject **defs, int defcount, PyObject *closure)
2454{
2455 register PyFrameObject *f;
2456 register PyObject *retval = NULL;
2457 register PyObject **fastlocals, **freevars;
2458 PyThreadState *tstate = PyThreadState_GET();
2459 PyObject *x, *u;
2460
2461 if (globals == NULL) {
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002462 PyErr_SetString(PyExc_SystemError,
2463 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002464 return NULL;
2465 }
2466
Jeremy Hylton985eba52003-02-05 23:13:00 +00002467 assert(globals != NULL);
2468 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002469 if (f == NULL)
2470 return NULL;
2471
2472 fastlocals = f->f_localsplus;
2473 freevars = f->f_localsplus + f->f_nlocals;
2474
2475 if (co->co_argcount > 0 ||
2476 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2477 int i;
2478 int n = argcount;
2479 PyObject *kwdict = NULL;
2480 if (co->co_flags & CO_VARKEYWORDS) {
2481 kwdict = PyDict_New();
2482 if (kwdict == NULL)
2483 goto fail;
2484 i = co->co_argcount;
2485 if (co->co_flags & CO_VARARGS)
2486 i++;
2487 SETLOCAL(i, kwdict);
2488 }
2489 if (argcount > co->co_argcount) {
2490 if (!(co->co_flags & CO_VARARGS)) {
2491 PyErr_Format(PyExc_TypeError,
2492 "%.200s() takes %s %d "
2493 "%sargument%s (%d given)",
2494 PyString_AsString(co->co_name),
2495 defcount ? "at most" : "exactly",
2496 co->co_argcount,
2497 kwcount ? "non-keyword " : "",
2498 co->co_argcount == 1 ? "" : "s",
2499 argcount);
2500 goto fail;
2501 }
2502 n = co->co_argcount;
2503 }
2504 for (i = 0; i < n; i++) {
2505 x = args[i];
2506 Py_INCREF(x);
2507 SETLOCAL(i, x);
2508 }
2509 if (co->co_flags & CO_VARARGS) {
2510 u = PyTuple_New(argcount - n);
2511 if (u == NULL)
2512 goto fail;
2513 SETLOCAL(co->co_argcount, u);
2514 for (i = n; i < argcount; i++) {
2515 x = args[i];
2516 Py_INCREF(x);
2517 PyTuple_SET_ITEM(u, i-n, x);
2518 }
2519 }
2520 for (i = 0; i < kwcount; i++) {
2521 PyObject *keyword = kws[2*i];
2522 PyObject *value = kws[2*i + 1];
2523 int j;
2524 if (keyword == NULL || !PyString_Check(keyword)) {
2525 PyErr_Format(PyExc_TypeError,
2526 "%.200s() keywords must be strings",
2527 PyString_AsString(co->co_name));
2528 goto fail;
2529 }
2530 /* XXX slow -- speed up using dictionary? */
2531 for (j = 0; j < co->co_argcount; j++) {
2532 PyObject *nm = PyTuple_GET_ITEM(
2533 co->co_varnames, j);
2534 int cmp = PyObject_RichCompareBool(
2535 keyword, nm, Py_EQ);
2536 if (cmp > 0)
2537 break;
2538 else if (cmp < 0)
2539 goto fail;
2540 }
2541 /* Check errors from Compare */
2542 if (PyErr_Occurred())
2543 goto fail;
2544 if (j >= co->co_argcount) {
2545 if (kwdict == NULL) {
2546 PyErr_Format(PyExc_TypeError,
2547 "%.200s() got an unexpected "
2548 "keyword argument '%.400s'",
2549 PyString_AsString(co->co_name),
2550 PyString_AsString(keyword));
2551 goto fail;
2552 }
2553 PyDict_SetItem(kwdict, keyword, value);
2554 }
2555 else {
2556 if (GETLOCAL(j) != NULL) {
2557 PyErr_Format(PyExc_TypeError,
2558 "%.200s() got multiple "
2559 "values for keyword "
2560 "argument '%.400s'",
2561 PyString_AsString(co->co_name),
2562 PyString_AsString(keyword));
2563 goto fail;
2564 }
2565 Py_INCREF(value);
2566 SETLOCAL(j, value);
2567 }
2568 }
2569 if (argcount < co->co_argcount) {
2570 int m = co->co_argcount - defcount;
2571 for (i = argcount; i < m; i++) {
2572 if (GETLOCAL(i) == NULL) {
2573 PyErr_Format(PyExc_TypeError,
2574 "%.200s() takes %s %d "
2575 "%sargument%s (%d given)",
2576 PyString_AsString(co->co_name),
2577 ((co->co_flags & CO_VARARGS) ||
2578 defcount) ? "at least"
2579 : "exactly",
2580 m, kwcount ? "non-keyword " : "",
2581 m == 1 ? "" : "s", i);
2582 goto fail;
2583 }
2584 }
2585 if (n > m)
2586 i = n - m;
2587 else
2588 i = 0;
2589 for (; i < defcount; i++) {
2590 if (GETLOCAL(m+i) == NULL) {
2591 PyObject *def = defs[i];
2592 Py_INCREF(def);
2593 SETLOCAL(m+i, def);
2594 }
2595 }
2596 }
2597 }
2598 else {
2599 if (argcount > 0 || kwcount > 0) {
2600 PyErr_Format(PyExc_TypeError,
2601 "%.200s() takes no arguments (%d given)",
2602 PyString_AsString(co->co_name),
2603 argcount + kwcount);
2604 goto fail;
2605 }
2606 }
2607 /* Allocate and initialize storage for cell vars, and copy free
2608 vars into frame. This isn't too efficient right now. */
2609 if (f->f_ncells) {
2610 int i = 0, j = 0, nargs, found;
2611 char *cellname, *argname;
2612 PyObject *c;
2613
2614 nargs = co->co_argcount;
2615 if (co->co_flags & CO_VARARGS)
2616 nargs++;
2617 if (co->co_flags & CO_VARKEYWORDS)
2618 nargs++;
2619
2620 /* Check for cells that shadow args */
2621 for (i = 0; i < f->f_ncells && j < nargs; ++i) {
2622 cellname = PyString_AS_STRING(
2623 PyTuple_GET_ITEM(co->co_cellvars, i));
2624 found = 0;
2625 while (j < nargs) {
2626 argname = PyString_AS_STRING(
2627 PyTuple_GET_ITEM(co->co_varnames, j));
2628 if (strcmp(cellname, argname) == 0) {
2629 c = PyCell_New(GETLOCAL(j));
2630 if (c == NULL)
2631 goto fail;
2632 GETLOCAL(f->f_nlocals + i) = c;
2633 found = 1;
2634 break;
2635 }
2636 j++;
2637 }
2638 if (found == 0) {
2639 c = PyCell_New(NULL);
2640 if (c == NULL)
2641 goto fail;
2642 SETLOCAL(f->f_nlocals + i, c);
2643 }
2644 }
2645 /* Initialize any that are left */
2646 while (i < f->f_ncells) {
2647 c = PyCell_New(NULL);
2648 if (c == NULL)
2649 goto fail;
2650 SETLOCAL(f->f_nlocals + i, c);
2651 i++;
2652 }
2653 }
2654 if (f->f_nfreevars) {
2655 int i;
2656 for (i = 0; i < f->f_nfreevars; ++i) {
2657 PyObject *o = PyTuple_GET_ITEM(closure, i);
2658 Py_INCREF(o);
2659 freevars[f->f_ncells + i] = o;
2660 }
2661 }
2662
Tim Peters5ca576e2001-06-18 22:08:13 +00002663 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002664 /* Don't need to keep the reference to f_back, it will be set
2665 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002666 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002667 f->f_back = NULL;
2668
Jeremy Hylton985eba52003-02-05 23:13:00 +00002669 PCALL(PCALL_GENERATOR);
2670
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002671 /* Create a new generator that owns the ready to run frame
2672 * and return that as the value. */
Tim Peters5ca576e2001-06-18 22:08:13 +00002673 return gen_new(f);
2674 }
2675
2676 retval = eval_frame(f);
2677
2678 fail: /* Jump here from prelude on failure */
2679
Tim Petersb13680b2001-11-27 23:29:29 +00002680 /* decref'ing the frame can cause __del__ methods to get invoked,
2681 which can call back into Python. While we're done with the
2682 current Python frame (f), the associated C stack is still in use,
2683 so recursion_depth must be boosted for the duration.
2684 */
2685 assert(tstate != NULL);
2686 ++tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002687 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00002688 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002689 return retval;
2690}
2691
2692
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002693/* Implementation notes for set_exc_info() and reset_exc_info():
2694
2695- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2696 'exc_traceback'. These always travel together.
2697
2698- tstate->curexc_ZZZ is the "hot" exception that is set by
2699 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2700
2701- Once an exception is caught by an except clause, it is transferred
2702 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2703 can pick it up. This is the primary task of set_exc_info().
2704
2705- Now let me explain the complicated dance with frame->f_exc_ZZZ.
2706
2707 Long ago, when none of this existed, there were just a few globals:
2708 one set corresponding to the "hot" exception, and one set
2709 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2710 globals; they were simply stored as sys.exc_ZZZ. For backwards
2711 compatibility, they still are!) The problem was that in code like
2712 this:
2713
2714 try:
2715 "something that may fail"
2716 except "some exception":
2717 "do something else first"
2718 "print the exception from sys.exc_ZZZ."
2719
2720 if "do something else first" invoked something that raised and caught
2721 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2722 cause of subtle bugs. I fixed this by changing the semantics as
2723 follows:
2724
2725 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2726 *in that frame*.
2727
2728 - But initially, and as long as no exception is caught in a given
2729 frame, sys.exc_ZZZ will hold the last exception caught in the
2730 previous frame (or the frame before that, etc.).
2731
2732 The first bullet fixed the bug in the above example. The second
2733 bullet was for backwards compatibility: it was (and is) common to
2734 have a function that is called when an exception is caught, and to
2735 have that function access the caught exception via sys.exc_ZZZ.
2736 (Example: traceback.print_exc()).
2737
2738 At the same time I fixed the problem that sys.exc_ZZZ weren't
2739 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2740 but that's really a separate improvement.
2741
2742 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2743 variables to what they were before the current frame was called. The
2744 set_exc_info() function saves them on the frame so that
2745 reset_exc_info() can restore them. The invariant is that
2746 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2747 exception (where "catching" an exception applies only to successful
2748 except clauses); and if the current frame ever caught an exception,
2749 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2750 at the start of the current frame.
2751
2752*/
2753
Guido van Rossuma027efa1997-05-05 20:56:21 +00002754static void
Guido van Rossumac7be682001-01-17 15:42:30 +00002755set_exc_info(PyThreadState *tstate,
2756 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002757{
2758 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002759 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00002760
Guido van Rossuma027efa1997-05-05 20:56:21 +00002761 frame = tstate->frame;
2762 if (frame->f_exc_type == NULL) {
2763 /* This frame didn't catch an exception before */
2764 /* Save previous exception of this thread in this frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002765 if (tstate->exc_type == NULL) {
2766 Py_INCREF(Py_None);
2767 tstate->exc_type = Py_None;
2768 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002769 tmp_type = frame->f_exc_type;
2770 tmp_value = frame->f_exc_value;
2771 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002772 Py_XINCREF(tstate->exc_type);
2773 Py_XINCREF(tstate->exc_value);
2774 Py_XINCREF(tstate->exc_traceback);
2775 frame->f_exc_type = tstate->exc_type;
2776 frame->f_exc_value = tstate->exc_value;
2777 frame->f_exc_traceback = tstate->exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002778 Py_XDECREF(tmp_type);
2779 Py_XDECREF(tmp_value);
2780 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002781 }
2782 /* Set new exception for this thread */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002783 tmp_type = tstate->exc_type;
2784 tmp_value = tstate->exc_value;
2785 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002786 Py_XINCREF(type);
2787 Py_XINCREF(value);
2788 Py_XINCREF(tb);
2789 tstate->exc_type = type;
2790 tstate->exc_value = value;
2791 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002792 Py_XDECREF(tmp_type);
2793 Py_XDECREF(tmp_value);
2794 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002795 /* For b/w compatibility */
2796 PySys_SetObject("exc_type", type);
2797 PySys_SetObject("exc_value", value);
2798 PySys_SetObject("exc_traceback", tb);
2799}
2800
2801static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002802reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002803{
2804 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002805 PyObject *tmp_type, *tmp_value, *tmp_tb;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002806 frame = tstate->frame;
2807 if (frame->f_exc_type != NULL) {
2808 /* This frame caught an exception */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002809 tmp_type = tstate->exc_type;
2810 tmp_value = tstate->exc_value;
2811 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002812 Py_XINCREF(frame->f_exc_type);
2813 Py_XINCREF(frame->f_exc_value);
2814 Py_XINCREF(frame->f_exc_traceback);
2815 tstate->exc_type = frame->f_exc_type;
2816 tstate->exc_value = frame->f_exc_value;
2817 tstate->exc_traceback = frame->f_exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002818 Py_XDECREF(tmp_type);
2819 Py_XDECREF(tmp_value);
2820 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002821 /* For b/w compatibility */
2822 PySys_SetObject("exc_type", frame->f_exc_type);
2823 PySys_SetObject("exc_value", frame->f_exc_value);
2824 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
2825 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002826 tmp_type = frame->f_exc_type;
2827 tmp_value = frame->f_exc_value;
2828 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002829 frame->f_exc_type = NULL;
2830 frame->f_exc_value = NULL;
2831 frame->f_exc_traceback = NULL;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002832 Py_XDECREF(tmp_type);
2833 Py_XDECREF(tmp_value);
2834 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002835}
2836
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002837/* Logic for the raise statement (too complicated for inlining).
2838 This *consumes* a reference count to each of its arguments. */
Guido van Rossum1aa14831997-01-21 05:34:20 +00002839static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002840do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002841{
Guido van Rossumd295f121998-04-09 21:39:57 +00002842 if (type == NULL) {
2843 /* Reraise */
2844 PyThreadState *tstate = PyThreadState_Get();
2845 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
2846 value = tstate->exc_value;
2847 tb = tstate->exc_traceback;
2848 Py_XINCREF(type);
2849 Py_XINCREF(value);
2850 Py_XINCREF(tb);
2851 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002852
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002853 /* We support the following forms of raise:
2854 raise <class>, <classinstance>
2855 raise <class>, <argument tuple>
2856 raise <class>, None
2857 raise <class>, <argument>
2858 raise <classinstance>, None
2859 raise <string>, <object>
2860 raise <string>, None
2861
2862 An omitted second argument is the same as None.
2863
2864 In addition, raise <tuple>, <anything> is the same as
2865 raising the tuple's first item (and it better have one!);
2866 this rule is applied recursively.
2867
2868 Finally, an optional third argument can be supplied, which
2869 gives the traceback to be substituted (useful when
2870 re-raising an exception after examining it). */
2871
2872 /* First, check the traceback argument, replacing None with
2873 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002874 if (tb == Py_None) {
2875 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002876 tb = NULL;
2877 }
2878 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002879 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002880 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002881 goto raise_error;
2882 }
2883
2884 /* Next, replace a missing value with None */
2885 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002886 value = Py_None;
2887 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002888 }
2889
2890 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00002891 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
2892 PyObject *tmp = type;
2893 type = PyTuple_GET_ITEM(type, 0);
2894 Py_INCREF(type);
2895 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002896 }
2897
Tim Petersafb2c802002-04-18 18:06:20 +00002898 if (PyString_CheckExact(type))
2899 /* Raising builtin string is deprecated but still allowed --
2900 * do nothing. Raising an instance of a new-style str
2901 * subclass is right out. */
Neal Norwitz37aa0662003-01-10 15:31:15 +00002902 PyErr_Warn(PyExc_PendingDeprecationWarning,
2903 "raising a string exception is deprecated");
Barry Warsaw4249f541997-08-22 21:26:19 +00002904
2905 else if (PyClass_Check(type))
2906 PyErr_NormalizeException(&type, &value, &tb);
2907
Guido van Rossumb209a111997-04-29 18:18:01 +00002908 else if (PyInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002909 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002910 if (value != Py_None) {
2911 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002912 "instance exception may not have a separate value");
2913 goto raise_error;
2914 }
2915 else {
2916 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00002917 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002918 value = type;
Guido van Rossumb209a111997-04-29 18:18:01 +00002919 type = (PyObject*) ((PyInstanceObject*)type)->in_class;
2920 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002921 }
2922 }
2923 else {
2924 /* Not something you can raise. You get an exception
2925 anyway, just not what you specified :-) */
Jeremy Hylton960d9482001-04-27 02:25:33 +00002926 PyErr_Format(PyExc_TypeError,
Neal Norwitz37aa0662003-01-10 15:31:15 +00002927 "exceptions must be classes, instances, or "
2928 "strings (deprecated), not %s",
2929 type->ob_type->tp_name);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002930 goto raise_error;
2931 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002932 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002933 if (tb == NULL)
2934 return WHY_EXCEPTION;
2935 else
2936 return WHY_RERAISE;
2937 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00002938 Py_XDECREF(value);
2939 Py_XDECREF(type);
2940 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002941 return WHY_EXCEPTION;
2942}
2943
Tim Petersd6d010b2001-06-21 02:49:55 +00002944/* Iterate v argcnt times and store the results on the stack (via decreasing
2945 sp). Return 1 for success, 0 if error. */
2946
Barry Warsawe42b18f1997-08-25 22:13:04 +00002947static int
Tim Petersd6d010b2001-06-21 02:49:55 +00002948unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00002949{
Tim Petersd6d010b2001-06-21 02:49:55 +00002950 int i = 0;
2951 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00002952 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00002953
Tim Petersd6d010b2001-06-21 02:49:55 +00002954 assert(v != NULL);
2955
2956 it = PyObject_GetIter(v);
2957 if (it == NULL)
2958 goto Error;
2959
2960 for (; i < argcnt; i++) {
2961 w = PyIter_Next(it);
2962 if (w == NULL) {
2963 /* Iterator done, via error or exhaustion. */
2964 if (!PyErr_Occurred()) {
2965 PyErr_Format(PyExc_ValueError,
2966 "need more than %d value%s to unpack",
2967 i, i == 1 ? "" : "s");
2968 }
2969 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00002970 }
2971 *--sp = w;
2972 }
Tim Petersd6d010b2001-06-21 02:49:55 +00002973
2974 /* We better have exhausted the iterator now. */
2975 w = PyIter_Next(it);
2976 if (w == NULL) {
2977 if (PyErr_Occurred())
2978 goto Error;
2979 Py_DECREF(it);
2980 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00002981 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00002982 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00002983 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00002984 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00002985Error:
Barry Warsaw91010551997-08-25 22:30:51 +00002986 for (; i > 0; i--, sp++)
2987 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00002988 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00002989 return 0;
2990}
2991
2992
Guido van Rossum96a42c81992-01-12 02:29:51 +00002993#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00002994static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002995prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002996{
Guido van Rossum3f5da241990-12-20 15:06:42 +00002997 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00002998 if (PyObject_Print(v, stdout, 0) != 0)
2999 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003000 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003001 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003002}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003003#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003004
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003005static void
Fred Drake5755ce62001-06-27 19:19:46 +00003006call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003007{
Guido van Rossumb209a111997-04-29 18:18:01 +00003008 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003009 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003010 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003011 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003012 value = Py_None;
3013 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003014 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003015 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003016 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003017 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003018 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003019 }
Fred Drake5755ce62001-06-27 19:19:46 +00003020 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003021 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003022 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003023 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003024 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003025 Py_XDECREF(type);
3026 Py_XDECREF(value);
3027 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003028 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003029}
3030
Fred Drake4ec5d562001-10-04 19:26:43 +00003031static void
3032call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3033 int what)
3034{
3035 PyObject *type, *value, *traceback;
3036 int err;
3037 PyErr_Fetch(&type, &value, &traceback);
3038 err = call_trace(func, obj, frame, what, NULL);
3039 if (err == 0)
3040 PyErr_Restore(type, value, traceback);
3041 else {
3042 Py_XDECREF(type);
3043 Py_XDECREF(value);
3044 Py_XDECREF(traceback);
3045 }
3046}
3047
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003048static int
Fred Drake5755ce62001-06-27 19:19:46 +00003049call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3050 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003051{
Fred Drake5755ce62001-06-27 19:19:46 +00003052 register PyThreadState *tstate = frame->f_tstate;
3053 int result;
3054 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003055 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003056 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003057 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003058 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003059 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3060 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003061 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003062 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003063}
3064
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003065PyObject *
3066_PyEval_CallTracing(PyObject *func, PyObject *args)
3067{
3068 PyFrameObject *frame = PyEval_GetFrame();
3069 PyThreadState *tstate = frame->f_tstate;
3070 int save_tracing = tstate->tracing;
3071 int save_use_tracing = tstate->use_tracing;
3072 PyObject *result;
3073
3074 tstate->tracing = 0;
3075 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3076 || (tstate->c_profilefunc != NULL));
3077 result = PyObject_Call(func, args, NULL);
3078 tstate->tracing = save_tracing;
3079 tstate->use_tracing = save_use_tracing;
3080 return result;
3081}
3082
Michael W. Hudson006c7522002-11-08 13:08:46 +00003083static int
Michael W. Hudson019a78e2002-11-08 12:53:11 +00003084maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003085 PyFrameObject *frame, int *instr_lb, int *instr_ub)
3086{
3087 /* The theory of SET_LINENO-less tracing.
3088
3089 In a nutshell, we use the co_lnotab field of the code object
3090 to tell when execution has moved onto a different line.
3091
3092 As mentioned above, the basic idea is so set things up so
3093 that
3094
3095 *instr_lb <= frame->f_lasti < *instr_ub
3096
3097 is true so long as execution does not change lines.
3098
3099 This is all fairly simple. Digging the information out of
3100 co_lnotab takes some work, but is conceptually clear.
3101
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003102 Somewhat harder to explain is why we don't *always* call the
3103 line trace function when the above test fails.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003104
3105 Consider this code:
3106
3107 1: def f(a):
3108 2: if a:
3109 3: print 1
3110 4: else:
3111 5: print 2
3112
3113 which compiles to this:
3114
3115 2 0 LOAD_FAST 0 (a)
3116 3 JUMP_IF_FALSE 9 (to 15)
3117 6 POP_TOP
3118
3119 3 7 LOAD_CONST 1 (1)
3120 10 PRINT_ITEM
3121 11 PRINT_NEWLINE
3122 12 JUMP_FORWARD 6 (to 21)
3123 >> 15 POP_TOP
3124
3125 5 16 LOAD_CONST 2 (2)
3126 19 PRINT_ITEM
3127 20 PRINT_NEWLINE
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003128 >> 21 LOAD_CONST 0 (None)
3129 24 RETURN_VALUE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003130
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003131 If 'a' is false, execution will jump to instruction at offset
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003132 15 and the co_lnotab will claim that execution has moved to
3133 line 3. This is at best misleading. In this case we could
3134 associate the POP_TOP with line 4, but that doesn't make
3135 sense in all cases (I think).
3136
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003137 What we do is only call the line trace function if the co_lnotab
3138 indicates we have jumped to the *start* of a line, i.e. if the
3139 current instruction offset matches the offset given for the
3140 start of a line by the co_lnotab.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003141
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003142 This also takes care of the situation where 'a' is true.
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003143 Execution will jump from instruction offset 12 to offset 21.
3144 Then the co_lnotab would imply that execution has moved to line
3145 5, which is again misleading.
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003146
3147 Why do we set f_lineno when tracing? Well, consider the code
3148 above when 'a' is true. If stepping through this with 'n' in
3149 pdb, you would stop at line 1 with a "call" type event, then
3150 line events on lines 2 and 3, then a "return" type event -- but
3151 you would be shown line 5 during this event. This is a change
3152 from the behaviour in 2.2 and before, and I've found it
3153 confusing in practice. By setting and using f_lineno when
3154 tracing, one can report a line number different from that
3155 suggested by f_lasti on this one occasion where it's desirable.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003156 */
3157
Michael W. Hudson006c7522002-11-08 13:08:46 +00003158 int result = 0;
3159
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003160 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003161 PyCodeObject* co = frame->f_code;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003162 int size, addr, line;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003163 unsigned char* p;
3164
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003165 size = PyString_GET_SIZE(co->co_lnotab) / 2;
3166 p = (unsigned char*)PyString_AS_STRING(co->co_lnotab);
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003167
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003168 addr = 0;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003169 line = co->co_firstlineno;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003170
3171 /* possible optimization: if f->f_lasti == instr_ub
3172 (likely to be a common case) then we already know
3173 instr_lb -- if we stored the matching value of p
3174 somwhere we could skip the first while loop. */
3175
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003176 /* see comments in compile.c for the description of
3177 co_lnotab. A point to remember: increments to p
3178 should come in pairs -- although we don't care about
3179 the line increments here, treating them as byte
3180 increments gets confusing, to say the least. */
3181
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003182 while (size > 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003183 if (addr + *p > frame->f_lasti)
3184 break;
3185 addr += *p++;
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003186 if (*p) *instr_lb = addr;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003187 line += *p++;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003188 --size;
3189 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003190
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003191 if (addr == frame->f_lasti) {
3192 frame->f_lineno = line;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003193 result = call_trace(func, obj, frame,
3194 PyTrace_LINE, Py_None);
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003195 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003196
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003197 if (size > 0) {
3198 while (--size >= 0) {
3199 addr += *p++;
3200 if (*p++)
3201 break;
3202 }
3203 *instr_ub = addr;
3204 }
3205 else {
3206 *instr_ub = INT_MAX;
3207 }
3208 }
Michael W. Hudson006c7522002-11-08 13:08:46 +00003209
3210 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003211}
3212
Fred Drake5755ce62001-06-27 19:19:46 +00003213void
3214PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003215{
Fred Drake5755ce62001-06-27 19:19:46 +00003216 PyThreadState *tstate = PyThreadState_Get();
3217 PyObject *temp = tstate->c_profileobj;
3218 Py_XINCREF(arg);
3219 tstate->c_profilefunc = NULL;
3220 tstate->c_profileobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003221 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003222 Py_XDECREF(temp);
3223 tstate->c_profilefunc = func;
3224 tstate->c_profileobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003225 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003226}
3227
3228void
3229PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3230{
3231 PyThreadState *tstate = PyThreadState_Get();
3232 PyObject *temp = tstate->c_traceobj;
3233 Py_XINCREF(arg);
3234 tstate->c_tracefunc = NULL;
3235 tstate->c_traceobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003236 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003237 Py_XDECREF(temp);
3238 tstate->c_tracefunc = func;
3239 tstate->c_traceobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003240 tstate->use_tracing = ((func != NULL)
3241 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003242}
3243
Guido van Rossumb209a111997-04-29 18:18:01 +00003244PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003245PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003246{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003247 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003248 if (current_frame == NULL)
Michael W. Hudson019a78e2002-11-08 12:53:11 +00003249 return PyThreadState_Get()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003250 else
3251 return current_frame->f_builtins;
3252}
3253
Guido van Rossumb209a111997-04-29 18:18:01 +00003254PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003255PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003256{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003257 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003258 if (current_frame == NULL)
3259 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003260 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003261 return current_frame->f_locals;
3262}
3263
Guido van Rossumb209a111997-04-29 18:18:01 +00003264PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003265PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003266{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003267 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003268 if (current_frame == NULL)
3269 return NULL;
3270 else
3271 return current_frame->f_globals;
3272}
3273
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003274PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003275PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003276{
Michael W. Hudson019a78e2002-11-08 12:53:11 +00003277 PyThreadState *tstate = PyThreadState_Get();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003278 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003279}
3280
Guido van Rossum6135a871995-01-09 17:53:26 +00003281int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003282PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003283{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003284 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003285 return current_frame == NULL ? 0 : current_frame->f_restricted;
3286}
3287
Guido van Rossumbe270261997-05-22 22:26:18 +00003288int
Tim Peters5ba58662001-07-16 02:29:45 +00003289PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003290{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003291 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003292 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003293
3294 if (current_frame != NULL) {
3295 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003296 const int compilerflags = codeflags & PyCF_MASK;
3297 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003298 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003299 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003300 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003301#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003302 if (codeflags & CO_GENERATOR_ALLOWED) {
3303 result = 1;
3304 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3305 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003306#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003307 }
3308 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003309}
3310
3311int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003312Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003313{
Guido van Rossumb209a111997-04-29 18:18:01 +00003314 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003315 if (f == NULL)
3316 return 0;
3317 if (!PyFile_SoftSpace(f, 0))
3318 return 0;
3319 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003320}
3321
Guido van Rossum3f5da241990-12-20 15:06:42 +00003322
Guido van Rossum681d79a1995-07-18 14:51:37 +00003323/* External interface to call any callable object.
3324 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003325
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003326#undef PyEval_CallObject
3327/* for backward compatibility: export this interface */
3328
Guido van Rossumb209a111997-04-29 18:18:01 +00003329PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003330PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003331{
Guido van Rossumb209a111997-04-29 18:18:01 +00003332 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003333}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003334#define PyEval_CallObject(func,arg) \
3335 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003336
Guido van Rossumb209a111997-04-29 18:18:01 +00003337PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003338PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003339{
Jeremy Hylton52820442001-01-03 23:52:36 +00003340 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003341
3342 if (arg == NULL)
Guido van Rossumb209a111997-04-29 18:18:01 +00003343 arg = PyTuple_New(0);
3344 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003345 PyErr_SetString(PyExc_TypeError,
3346 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003347 return NULL;
3348 }
3349 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003350 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003351
Guido van Rossumb209a111997-04-29 18:18:01 +00003352 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003353 PyErr_SetString(PyExc_TypeError,
3354 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003355 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003356 return NULL;
3357 }
3358
Tim Peters6d6c1a32001-08-02 04:15:00 +00003359 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003360 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003361 return result;
3362}
3363
Tim Peters6d6c1a32001-08-02 04:15:00 +00003364char *
3365PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003366{
3367 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003368 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003369 else if (PyFunction_Check(func))
3370 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3371 else if (PyCFunction_Check(func))
3372 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3373 else if (PyClass_Check(func))
3374 return PyString_AsString(((PyClassObject*)func)->cl_name);
3375 else if (PyInstance_Check(func)) {
3376 return PyString_AsString(
3377 ((PyInstanceObject*)func)->in_class->cl_name);
3378 } else {
3379 return func->ob_type->tp_name;
3380 }
3381}
3382
Tim Peters6d6c1a32001-08-02 04:15:00 +00003383char *
3384PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003385{
3386 if (PyMethod_Check(func))
3387 return "()";
3388 else if (PyFunction_Check(func))
3389 return "()";
3390 else if (PyCFunction_Check(func))
3391 return "()";
3392 else if (PyClass_Check(func))
3393 return " constructor";
3394 else if (PyInstance_Check(func)) {
3395 return " instance";
3396 } else {
3397 return " object";
3398 }
3399}
3400
Jeremy Hylton52820442001-01-03 23:52:36 +00003401#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
3402
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003403static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003404err_args(PyObject *func, int flags, int nargs)
3405{
3406 if (flags & METH_NOARGS)
3407 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003408 "%.200s() takes no arguments (%d given)",
Jeremy Hylton192690e2002-08-16 18:36:11 +00003409 ((PyCFunctionObject *)func)->m_ml->ml_name,
3410 nargs);
3411 else
3412 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003413 "%.200s() takes exactly one argument (%d given)",
Jeremy Hylton192690e2002-08-16 18:36:11 +00003414 ((PyCFunctionObject *)func)->m_ml->ml_name,
3415 nargs);
3416}
3417
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003418static PyObject *
3419call_function(PyObject ***pp_stack, int oparg)
3420{
3421 int na = oparg & 0xff;
3422 int nk = (oparg>>8) & 0xff;
3423 int n = na + 2 * nk;
3424 PyObject **pfunc = (*pp_stack) - n - 1;
3425 PyObject *func = *pfunc;
3426 PyObject *x, *w;
3427
Jeremy Hylton985eba52003-02-05 23:13:00 +00003428 /* Always dispatch PyCFunction first, because these are
3429 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003430 */
3431 if (PyCFunction_Check(func) && nk == 0) {
3432 int flags = PyCFunction_GET_FLAGS(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003433 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003434 if (flags & (METH_NOARGS | METH_O)) {
3435 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3436 PyObject *self = PyCFunction_GET_SELF(func);
3437 if (flags & METH_NOARGS && na == 0)
3438 x = (*meth)(self, NULL);
3439 else if (flags & METH_O && na == 1) {
3440 PyObject *arg = EXT_POP(*pp_stack);
3441 x = (*meth)(self, arg);
3442 Py_DECREF(arg);
3443 }
3444 else {
3445 err_args(func, flags, na);
3446 x = NULL;
3447 }
3448 }
3449 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003450 PyObject *callargs;
3451 callargs = load_args(pp_stack, na);
3452 x = PyCFunction_Call(func, callargs, NULL);
3453 Py_XDECREF(callargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003454 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003455 } else {
3456 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3457 /* optimize access to bound methods */
3458 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003459 PCALL(PCALL_METHOD);
3460 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003461 Py_INCREF(self);
3462 func = PyMethod_GET_FUNCTION(func);
3463 Py_INCREF(func);
3464 Py_DECREF(*pfunc);
3465 *pfunc = self;
3466 na++;
3467 n++;
3468 } else
3469 Py_INCREF(func);
3470 if (PyFunction_Check(func))
3471 x = fast_function(func, pp_stack, n, na, nk);
3472 else
3473 x = do_call(func, pp_stack, na, nk);
3474 Py_DECREF(func);
3475 }
3476
Jeremy Hylton985eba52003-02-05 23:13:00 +00003477 /* What does this do? */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003478 while ((*pp_stack) > pfunc) {
3479 w = EXT_POP(*pp_stack);
3480 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003481 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003482 }
3483 return x;
3484}
3485
Jeremy Hylton192690e2002-08-16 18:36:11 +00003486/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003487 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003488 For the simplest case -- a function that takes only positional
3489 arguments and is called with only positional arguments -- it
3490 inlines the most primitive frame setup code from
3491 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3492 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003493*/
3494
3495static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003496fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003497{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003498 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003499 PyObject *globals = PyFunction_GET_GLOBALS(func);
3500 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3501 PyObject **d = NULL;
3502 int nd = 0;
3503
Jeremy Hylton985eba52003-02-05 23:13:00 +00003504 PCALL(PCALL_FUNCTION);
3505 PCALL(PCALL_FAST_FUNCTION);
Raymond Hettinger40174c32003-05-31 07:04:16 +00003506 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003507 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3508 PyFrameObject *f;
3509 PyObject *retval = NULL;
3510 PyThreadState *tstate = PyThreadState_GET();
3511 PyObject **fastlocals, **stack;
3512 int i;
3513
3514 PCALL(PCALL_FASTER_FUNCTION);
3515 assert(globals != NULL);
3516 /* XXX Perhaps we should create a specialized
3517 PyFrame_New() that doesn't take locals, but does
3518 take builtins without sanity checking them.
3519 */
3520 f = PyFrame_New(tstate, co, globals, NULL);
3521 if (f == NULL)
3522 return NULL;
3523
3524 fastlocals = f->f_localsplus;
3525 stack = (*pp_stack) - n;
3526
3527 for (i = 0; i < n; i++) {
3528 Py_INCREF(*stack);
3529 fastlocals[i] = *stack++;
3530 }
3531 retval = eval_frame(f);
3532 assert(tstate != NULL);
3533 ++tstate->recursion_depth;
3534 Py_DECREF(f);
3535 --tstate->recursion_depth;
3536 return retval;
3537 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003538 if (argdefs != NULL) {
3539 d = &PyTuple_GET_ITEM(argdefs, 0);
3540 nd = ((PyTupleObject *)argdefs)->ob_size;
3541 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003542 return PyEval_EvalCodeEx(co, globals,
3543 (PyObject *)NULL, (*pp_stack)-n, na,
3544 (*pp_stack)-2*nk, nk, d, nd,
3545 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003546}
3547
3548static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003549update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3550 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003551{
3552 PyObject *kwdict = NULL;
3553 if (orig_kwdict == NULL)
3554 kwdict = PyDict_New();
3555 else {
3556 kwdict = PyDict_Copy(orig_kwdict);
3557 Py_DECREF(orig_kwdict);
3558 }
3559 if (kwdict == NULL)
3560 return NULL;
3561 while (--nk >= 0) {
3562 int err;
3563 PyObject *value = EXT_POP(*pp_stack);
3564 PyObject *key = EXT_POP(*pp_stack);
3565 if (PyDict_GetItem(kwdict, key) != NULL) {
Guido van Rossumac7be682001-01-17 15:42:30 +00003566 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +00003567 "%.200s%s got multiple values "
Jeremy Hylton512a2372001-04-11 13:52:29 +00003568 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003569 PyEval_GetFuncName(func),
3570 PyEval_GetFuncDesc(func),
Jeremy Hylton512a2372001-04-11 13:52:29 +00003571 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003572 Py_DECREF(key);
3573 Py_DECREF(value);
3574 Py_DECREF(kwdict);
3575 return NULL;
3576 }
3577 err = PyDict_SetItem(kwdict, key, value);
3578 Py_DECREF(key);
3579 Py_DECREF(value);
3580 if (err) {
3581 Py_DECREF(kwdict);
3582 return NULL;
3583 }
3584 }
3585 return kwdict;
3586}
3587
3588static PyObject *
3589update_star_args(int nstack, int nstar, PyObject *stararg,
3590 PyObject ***pp_stack)
3591{
3592 PyObject *callargs, *w;
3593
3594 callargs = PyTuple_New(nstack + nstar);
3595 if (callargs == NULL) {
3596 return NULL;
3597 }
3598 if (nstar) {
3599 int i;
3600 for (i = 0; i < nstar; i++) {
3601 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3602 Py_INCREF(a);
3603 PyTuple_SET_ITEM(callargs, nstack + i, a);
3604 }
3605 }
3606 while (--nstack >= 0) {
3607 w = EXT_POP(*pp_stack);
3608 PyTuple_SET_ITEM(callargs, nstack, w);
3609 }
3610 return callargs;
3611}
3612
3613static PyObject *
3614load_args(PyObject ***pp_stack, int na)
3615{
3616 PyObject *args = PyTuple_New(na);
3617 PyObject *w;
3618
3619 if (args == NULL)
3620 return NULL;
3621 while (--na >= 0) {
3622 w = EXT_POP(*pp_stack);
3623 PyTuple_SET_ITEM(args, na, w);
3624 }
3625 return args;
3626}
3627
3628static PyObject *
3629do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3630{
3631 PyObject *callargs = NULL;
3632 PyObject *kwdict = NULL;
3633 PyObject *result = NULL;
3634
3635 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003636 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003637 if (kwdict == NULL)
3638 goto call_fail;
3639 }
3640 callargs = load_args(pp_stack, na);
3641 if (callargs == NULL)
3642 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003643#ifdef CALL_PROFILE
3644 /* At this point, we have to look at the type of func to
3645 update the call stats properly. Do it here so as to avoid
3646 exposing the call stats machinery outside ceval.c
3647 */
3648 if (PyFunction_Check(func))
3649 PCALL(PCALL_FUNCTION);
3650 else if (PyMethod_Check(func))
3651 PCALL(PCALL_METHOD);
3652 else if (PyType_Check(func))
3653 PCALL(PCALL_TYPE);
3654 else
3655 PCALL(PCALL_OTHER);
3656#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003657 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003658 call_fail:
3659 Py_XDECREF(callargs);
3660 Py_XDECREF(kwdict);
3661 return result;
3662}
3663
3664static PyObject *
3665ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3666{
3667 int nstar = 0;
3668 PyObject *callargs = NULL;
3669 PyObject *stararg = NULL;
3670 PyObject *kwdict = NULL;
3671 PyObject *result = NULL;
3672
3673 if (flags & CALL_FLAG_KW) {
3674 kwdict = EXT_POP(*pp_stack);
3675 if (!(kwdict && PyDict_Check(kwdict))) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003676 PyErr_Format(PyExc_TypeError,
Jeremy Hylton512a2372001-04-11 13:52:29 +00003677 "%s%s argument after ** "
3678 "must be a dictionary",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003679 PyEval_GetFuncName(func),
3680 PyEval_GetFuncDesc(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003681 goto ext_call_fail;
3682 }
3683 }
3684 if (flags & CALL_FLAG_VAR) {
3685 stararg = EXT_POP(*pp_stack);
3686 if (!PyTuple_Check(stararg)) {
3687 PyObject *t = NULL;
3688 t = PySequence_Tuple(stararg);
3689 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003690 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3691 PyErr_Format(PyExc_TypeError,
3692 "%s%s argument after * "
3693 "must be a sequence",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003694 PyEval_GetFuncName(func),
3695 PyEval_GetFuncDesc(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003696 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003697 goto ext_call_fail;
3698 }
3699 Py_DECREF(stararg);
3700 stararg = t;
3701 }
3702 nstar = PyTuple_GET_SIZE(stararg);
3703 }
3704 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003705 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003706 if (kwdict == NULL)
3707 goto ext_call_fail;
3708 }
3709 callargs = update_star_args(na, nstar, stararg, pp_stack);
3710 if (callargs == NULL)
3711 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003712#ifdef CALL_PROFILE
3713 /* At this point, we have to look at the type of func to
3714 update the call stats properly. Do it here so as to avoid
3715 exposing the call stats machinery outside ceval.c
3716 */
3717 if (PyFunction_Check(func))
3718 PCALL(PCALL_FUNCTION);
3719 else if (PyMethod_Check(func))
3720 PCALL(PCALL_METHOD);
3721 else if (PyType_Check(func))
3722 PCALL(PCALL_TYPE);
3723 else
3724 PCALL(PCALL_OTHER);
3725#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003726 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003727 ext_call_fail:
3728 Py_XDECREF(callargs);
3729 Py_XDECREF(kwdict);
3730 Py_XDECREF(stararg);
3731 return result;
3732}
3733
Guido van Rossum3b9c6671996-07-30 18:40:29 +00003734#define SLICE_ERROR_MSG \
3735 "standard sequence type does not support step size other than one"
3736
Tim Peterscb479e72001-12-16 19:11:44 +00003737/* Extract a slice index from a PyInt or PyLong, and store in *pi.
3738 Silently reduce values larger than INT_MAX to INT_MAX, and silently
3739 boost values less than -INT_MAX to 0. Return 0 on error, 1 on success.
3740*/
Tim Petersb5196382001-12-16 19:44:20 +00003741/* Note: If v is NULL, return success without storing into *pi. This
3742 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3743 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00003744*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00003745int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003746_PyEval_SliceIndex(PyObject *v, int *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003747{
Tim Petersb5196382001-12-16 19:44:20 +00003748 if (v != NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003749 long x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003750 if (PyInt_Check(v)) {
3751 x = PyInt_AsLong(v);
3752 } else if (PyLong_Check(v)) {
3753 x = PyLong_AsLong(v);
3754 if (x==-1 && PyErr_Occurred()) {
3755 PyObject *long_zero;
Guido van Rossumac7be682001-01-17 15:42:30 +00003756 int cmp;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003757
Guido van Rossumac7be682001-01-17 15:42:30 +00003758 if (!PyErr_ExceptionMatches(
3759 PyExc_OverflowError)) {
3760 /* It's not an overflow error, so just
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003761 signal an error */
Guido van Rossum20c6add2000-05-08 14:06:50 +00003762 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003763 }
3764
Guido van Rossumac7be682001-01-17 15:42:30 +00003765 /* Clear the OverflowError */
3766 PyErr_Clear();
3767
3768 /* It's an overflow error, so we need to
3769 check the sign of the long integer,
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003770 set the value to INT_MAX or -INT_MAX,
3771 and clear the error. */
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003772
3773 /* Create a long integer with a value of 0 */
Guido van Rossumac7be682001-01-17 15:42:30 +00003774 long_zero = PyLong_FromLong(0L);
Tim Peterscb479e72001-12-16 19:11:44 +00003775 if (long_zero == NULL)
3776 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003777
3778 /* Check sign */
Guido van Rossumac7be682001-01-17 15:42:30 +00003779 cmp = PyObject_RichCompareBool(v, long_zero,
3780 Py_GT);
3781 Py_DECREF(long_zero);
3782 if (cmp < 0)
3783 return 0;
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003784 else if (cmp)
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003785 x = INT_MAX;
3786 else
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003787 x = -INT_MAX;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003788 }
3789 } else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003790 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003791 "slice indices must be integers");
Guido van Rossum20c6add2000-05-08 14:06:50 +00003792 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003793 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003794 /* Truncate -- very long indices are truncated anyway */
3795 if (x > INT_MAX)
3796 x = INT_MAX;
3797 else if (x < -INT_MAX)
Michael W. Hudsoncbd6fb92002-11-06 15:17:32 +00003798 x = -INT_MAX;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003799 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003800 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00003801 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003802}
3803
Guido van Rossum50d756e2001-08-18 17:43:36 +00003804#undef ISINT
3805#define ISINT(x) ((x) == NULL || PyInt_Check(x) || PyLong_Check(x))
3806
Guido van Rossumb209a111997-04-29 18:18:01 +00003807static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003808apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003809{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003810 PyTypeObject *tp = u->ob_type;
3811 PySequenceMethods *sq = tp->tp_as_sequence;
3812
3813 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3814 int ilow = 0, ihigh = INT_MAX;
3815 if (!_PyEval_SliceIndex(v, &ilow))
3816 return NULL;
3817 if (!_PyEval_SliceIndex(w, &ihigh))
3818 return NULL;
3819 return PySequence_GetSlice(u, ilow, ihigh);
3820 }
3821 else {
3822 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00003823 if (slice != NULL) {
3824 PyObject *res = PyObject_GetItem(u, slice);
3825 Py_DECREF(slice);
3826 return res;
3827 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00003828 else
3829 return NULL;
3830 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003831}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003832
3833static int
Guido van Rossumac7be682001-01-17 15:42:30 +00003834assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
3835 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003836{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003837 PyTypeObject *tp = u->ob_type;
3838 PySequenceMethods *sq = tp->tp_as_sequence;
3839
3840 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3841 int ilow = 0, ihigh = INT_MAX;
3842 if (!_PyEval_SliceIndex(v, &ilow))
3843 return -1;
3844 if (!_PyEval_SliceIndex(w, &ihigh))
3845 return -1;
3846 if (x == NULL)
3847 return PySequence_DelSlice(u, ilow, ihigh);
3848 else
3849 return PySequence_SetSlice(u, ilow, ihigh, x);
3850 }
3851 else {
3852 PyObject *slice = PySlice_New(v, w, NULL);
3853 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00003854 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003855 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00003856 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00003857 else
Guido van Rossum354797c2001-12-03 19:45:06 +00003858 res = PyObject_DelItem(u, slice);
3859 Py_DECREF(slice);
3860 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003861 }
3862 else
3863 return -1;
3864 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003865}
3866
Guido van Rossumb209a111997-04-29 18:18:01 +00003867static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003868cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003869{
Guido van Rossumac7be682001-01-17 15:42:30 +00003870 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003871 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00003872 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003873 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003874 break;
3875 case PyCmp_IS_NOT:
3876 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003877 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003878 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003879 res = PySequence_Contains(w, v);
3880 if (res < 0)
3881 return NULL;
3882 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003883 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00003884 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003885 if (res < 0)
3886 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003887 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003888 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003889 case PyCmp_EXC_MATCH:
Barry Warsaw4249f541997-08-22 21:26:19 +00003890 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003891 break;
3892 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00003893 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003894 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003895 v = res ? Py_True : Py_False;
3896 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003897 return v;
3898}
3899
Thomas Wouters52152252000-08-17 22:55:00 +00003900static PyObject *
3901import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003902{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003903 PyObject *x;
3904
3905 x = PyObject_GetAttr(v, name);
3906 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00003907 PyErr_Format(PyExc_ImportError,
3908 "cannot import name %.230s",
3909 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003910 }
Thomas Wouters52152252000-08-17 22:55:00 +00003911 return x;
3912}
Guido van Rossumac7be682001-01-17 15:42:30 +00003913
Thomas Wouters52152252000-08-17 22:55:00 +00003914static int
3915import_all_from(PyObject *locals, PyObject *v)
3916{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003917 PyObject *all = PyObject_GetAttrString(v, "__all__");
3918 PyObject *dict, *name, *value;
3919 int skip_leading_underscores = 0;
3920 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00003921
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003922 if (all == NULL) {
3923 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3924 return -1; /* Unexpected error */
3925 PyErr_Clear();
3926 dict = PyObject_GetAttrString(v, "__dict__");
3927 if (dict == NULL) {
3928 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3929 return -1;
3930 PyErr_SetString(PyExc_ImportError,
3931 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00003932 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003933 }
3934 all = PyMapping_Keys(dict);
3935 Py_DECREF(dict);
3936 if (all == NULL)
3937 return -1;
3938 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00003939 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003940
3941 for (pos = 0, err = 0; ; pos++) {
3942 name = PySequence_GetItem(all, pos);
3943 if (name == NULL) {
3944 if (!PyErr_ExceptionMatches(PyExc_IndexError))
3945 err = -1;
3946 else
3947 PyErr_Clear();
3948 break;
3949 }
3950 if (skip_leading_underscores &&
3951 PyString_Check(name) &&
3952 PyString_AS_STRING(name)[0] == '_')
3953 {
3954 Py_DECREF(name);
3955 continue;
3956 }
3957 value = PyObject_GetAttr(v, name);
3958 if (value == NULL)
3959 err = -1;
3960 else
3961 err = PyDict_SetItem(locals, name, value);
3962 Py_DECREF(name);
3963 Py_XDECREF(value);
3964 if (err != 0)
3965 break;
3966 }
3967 Py_DECREF(all);
3968 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00003969}
3970
Guido van Rossumb209a111997-04-29 18:18:01 +00003971static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003972build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003973{
Guido van Rossum7851eea2001-09-12 19:19:18 +00003974 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003975
3976 if (PyDict_Check(methods))
3977 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00003978 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00003979 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00003980 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
3981 base = PyTuple_GET_ITEM(bases, 0);
3982 metaclass = PyObject_GetAttrString(base, "__class__");
3983 if (metaclass == NULL) {
3984 PyErr_Clear();
3985 metaclass = (PyObject *)base->ob_type;
3986 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00003987 }
3988 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00003989 else {
3990 PyObject *g = PyEval_GetGlobals();
3991 if (g != NULL && PyDict_Check(g))
3992 metaclass = PyDict_GetItemString(g, "__metaclass__");
3993 if (metaclass == NULL)
3994 metaclass = (PyObject *) &PyClass_Type;
3995 Py_INCREF(metaclass);
3996 }
3997 result = PyObject_CallFunction(metaclass, "OOO", name, bases, methods);
3998 Py_DECREF(metaclass);
3999 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00004000}
4001
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004002static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004003exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
4004 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004005{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004006 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00004007 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004008 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004009
Guido van Rossumb209a111997-04-29 18:18:01 +00004010 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
4011 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004012 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00004013 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004014 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00004015 locals = PyTuple_GetItem(prog, 2);
4016 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004017 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004018 if (globals == Py_None) {
4019 globals = PyEval_GetGlobals();
4020 if (locals == Py_None) {
4021 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00004022 plain = 1;
4023 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004024 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004025 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004026 locals = globals;
Guido van Rossumb209a111997-04-29 18:18:01 +00004027 if (!PyString_Check(prog) &&
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004028 !PyUnicode_Check(prog) &&
Guido van Rossumb209a111997-04-29 18:18:01 +00004029 !PyCode_Check(prog) &&
4030 !PyFile_Check(prog)) {
4031 PyErr_SetString(PyExc_TypeError,
Guido van Rossumac7be682001-01-17 15:42:30 +00004032 "exec: arg 1 must be a string, file, or code object");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004033 return -1;
4034 }
Fred Drake661ea262000-10-24 19:57:45 +00004035 if (!PyDict_Check(globals)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004036 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00004037 "exec: arg 2 must be a dictionary or None");
4038 return -1;
4039 }
4040 if (!PyDict_Check(locals)) {
4041 PyErr_SetString(PyExc_TypeError,
4042 "exec: arg 3 must be a dictionary or None");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004043 return -1;
4044 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004045 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00004046 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00004047 if (PyCode_Check(prog)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +00004048 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4049 PyErr_SetString(PyExc_TypeError,
4050 "code object passed to exec may not contain free variables");
4051 return -1;
4052 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004053 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004054 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004055 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004056 FILE *fp = PyFile_AsFile(prog);
4057 char *name = PyString_AsString(PyFile_Name(prog));
Tim Peters5ba58662001-07-16 02:29:45 +00004058 PyCompilerFlags cf;
4059 cf.cf_flags = 0;
4060 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004061 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
4062 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004063 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004064 v = PyRun_File(fp, name, Py_file_input, globals,
4065 locals);
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004066 }
4067 else {
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004068 PyObject *tmp = NULL;
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004069 char *str;
Tim Peters5ba58662001-07-16 02:29:45 +00004070 PyCompilerFlags cf;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004071 cf.cf_flags = 0;
4072#ifdef Py_USING_UNICODE
4073 if (PyUnicode_Check(prog)) {
4074 tmp = PyUnicode_AsUTF8String(prog);
4075 if (tmp == NULL)
4076 return -1;
4077 prog = tmp;
4078 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4079 }
4080#endif
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004081 if (PyString_AsStringAndSize(prog, &str, NULL))
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004082 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004083 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004084 v = PyRun_StringFlags(str, Py_file_input, globals,
4085 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004086 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004087 v = PyRun_String(str, Py_file_input, globals, locals);
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004088 Py_XDECREF(tmp);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004089 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004090 if (plain)
4091 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004092 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004093 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00004094 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004095 return 0;
4096}
Guido van Rossum24c13741995-02-14 09:42:43 +00004097
Guido van Rossumac7be682001-01-17 15:42:30 +00004098static void
Paul Prescode68140d2000-08-30 20:25:01 +00004099format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4100{
4101 char *obj_str;
4102
4103 if (!obj)
4104 return;
4105
4106 obj_str = PyString_AsString(obj);
4107 if (!obj_str)
4108 return;
4109
4110 PyErr_Format(exc, format_str, obj_str);
4111}
Guido van Rossum950361c1997-01-24 13:49:28 +00004112
4113#ifdef DYNAMIC_EXECUTION_PROFILE
4114
Skip Montanarof118cb12001-10-15 20:51:38 +00004115static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004116getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004117{
4118 int i;
4119 PyObject *l = PyList_New(256);
4120 if (l == NULL) return NULL;
4121 for (i = 0; i < 256; i++) {
4122 PyObject *x = PyInt_FromLong(a[i]);
4123 if (x == NULL) {
4124 Py_DECREF(l);
4125 return NULL;
4126 }
4127 PyList_SetItem(l, i, x);
4128 }
4129 for (i = 0; i < 256; i++)
4130 a[i] = 0;
4131 return l;
4132}
4133
4134PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004135_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004136{
4137#ifndef DXPAIRS
4138 return getarray(dxp);
4139#else
4140 int i;
4141 PyObject *l = PyList_New(257);
4142 if (l == NULL) return NULL;
4143 for (i = 0; i < 257; i++) {
4144 PyObject *x = getarray(dxpairs[i]);
4145 if (x == NULL) {
4146 Py_DECREF(l);
4147 return NULL;
4148 }
4149 PyList_SetItem(l, i, x);
4150 }
4151 return l;
4152#endif
4153}
4154
4155#endif