blob: f2d79e77dfb72aaf1cdfc67a5edd2a91b19e3bc5 [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.
Raymond Hettingera7216982004-02-08 19:59:27 +0000648
649 If collecting opcode statistics, turn off prediction so that
650 statistics are accurately maintained (the predictions bypass
651 the opcode frequency counter updates).
Raymond Hettingerf606f872003-03-16 03:11:04 +0000652*/
653
Raymond Hettingera7216982004-02-08 19:59:27 +0000654#ifdef DYNAMIC_EXECUTION_PROFILE
655#define PREDICT(op) if (0) goto PRED_##op
656#else
Raymond Hettingerac2072922003-03-16 15:41:11 +0000657#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000658#endif
659
Raymond Hettingerf606f872003-03-16 03:11:04 +0000660#define PREDICTED(op) PRED_##op: next_instr++
Raymond Hettinger7dc52212003-03-16 20:14:44 +0000661#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = (next_instr[2]<<8) + \
662 next_instr[1]; next_instr += 3
Raymond Hettingerf606f872003-03-16 03:11:04 +0000663
Guido van Rossum374a9221991-04-04 10:40:29 +0000664/* Stack manipulation macros */
665
666#define STACK_LEVEL() (stack_pointer - f->f_valuestack)
667#define EMPTY() (STACK_LEVEL() == 0)
668#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000669#define SECOND() (stack_pointer[-2])
670#define THIRD() (stack_pointer[-3])
671#define FOURTH() (stack_pointer[-4])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000672#define SET_TOP(v) (stack_pointer[-1] = (v))
673#define SET_SECOND(v) (stack_pointer[-2] = (v))
674#define SET_THIRD(v) (stack_pointer[-3] = (v))
675#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000676#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000677#define BASIC_PUSH(v) (*stack_pointer++ = (v))
678#define BASIC_POP() (*--stack_pointer)
679
Guido van Rossum96a42c81992-01-12 02:29:51 +0000680#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000681#define PUSH(v) { (void)(BASIC_PUSH(v), \
682 lltrace && prtrace(TOP(), "push")); \
683 assert(STACK_LEVEL() <= f->f_stacksize); }
Fred Drakede26cfc2001-10-13 06:11:28 +0000684#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000685#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
686 lltrace && prtrace(TOP(), "stackadj")); \
687 assert(STACK_LEVEL() <= f->f_stacksize); }
Guido van Rossum374a9221991-04-04 10:40:29 +0000688#else
689#define PUSH(v) BASIC_PUSH(v)
690#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000691#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000692#endif
693
Guido van Rossum681d79a1995-07-18 14:51:37 +0000694/* Local variable macros */
695
696#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000697
698/* The SETLOCAL() macro must not DECREF the local variable in-place and
699 then store the new value; it must copy the old value to a temporary
700 value, then store the new value, and then DECREF the temporary value.
701 This is because it is possible that during the DECREF the frame is
702 accessed by other code (e.g. a __del__ method or gc.collect()) and the
703 variable would be pointing to already-freed memory. */
704#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
705 GETLOCAL(i) = value; \
706 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000707
Guido van Rossuma027efa1997-05-05 20:56:21 +0000708/* Start of code */
709
Tim Peters5ca576e2001-06-18 22:08:13 +0000710 if (f == NULL)
711 return NULL;
712
Armin Rigo1d313ab2003-10-25 14:33:09 +0000713 /* push frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000714 if (Py_EnterRecursiveCall(""))
Armin Rigo1d313ab2003-10-25 14:33:09 +0000715 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000716
Tim Peters5ca576e2001-06-18 22:08:13 +0000717 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000718
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000719 if (tstate->use_tracing) {
720 if (tstate->c_tracefunc != NULL) {
721 /* tstate->c_tracefunc, if defined, is a
722 function that will be called on *every* entry
723 to a code block. Its return value, if not
724 None, is a function that will be called at
725 the start of each executed line of code.
726 (Actually, the function must return itself
727 in order to continue tracing.) The trace
728 functions are called with three arguments:
729 a pointer to the current frame, a string
730 indicating why the function is called, and
731 an argument which depends on the situation.
732 The global trace function is also called
733 whenever an exception is detected. */
734 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
735 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000736 /* Trace function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000737 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000738 }
739 }
740 if (tstate->c_profilefunc != NULL) {
741 /* Similar for c_profilefunc, except it needn't
742 return itself and isn't called for "line" events */
743 if (call_trace(tstate->c_profilefunc,
744 tstate->c_profileobj,
745 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000746 /* Profile function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000747 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000748 }
749 }
750 }
751
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000752 co = f->f_code;
753 names = co->co_names;
754 consts = co->co_consts;
755 fastlocals = f->f_localsplus;
756 freevars = f->f_localsplus + f->f_nlocals;
757 _PyCode_GETCODEPTR(co, &first_instr);
758 /* An explanation is in order for the next line.
759
760 f->f_lasti now refers to the index of the last instruction
761 executed. You might think this was obvious from the name, but
762 this wasn't always true before 2.3! PyFrame_New now sets
763 f->f_lasti to -1 (i.e. the index *before* the first instruction)
764 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
765 does work. Promise. */
766 next_instr = first_instr + f->f_lasti + 1;
767 stack_pointer = f->f_stacktop;
768 assert(stack_pointer != NULL);
769 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
770
Tim Peters5ca576e2001-06-18 22:08:13 +0000771#ifdef LLTRACE
772 lltrace = PyDict_GetItemString(f->f_globals,"__lltrace__") != NULL;
773#endif
774#if defined(Py_DEBUG) || defined(LLTRACE)
775 filename = PyString_AsString(co->co_filename);
776#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000777
Guido van Rossum374a9221991-04-04 10:40:29 +0000778 why = WHY_NOT;
779 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +0000780 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +0000781 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +0000782
Guido van Rossum374a9221991-04-04 10:40:29 +0000783 for (;;) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000784 assert(stack_pointer >= f->f_valuestack); /* else underflow */
785 assert(STACK_LEVEL() <= f->f_stacksize); /* else overflow */
786
Guido van Rossuma027efa1997-05-05 20:56:21 +0000787 /* Do periodic things. Doing this every time through
788 the loop would add too much overhead, so we do it
789 only every Nth instruction. We also do it if
790 ``things_to_do'' is set, i.e. when an asynchronous
791 event needs attention (e.g. a signal handler or
792 async I/O handler); see Py_AddPendingCall() and
793 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000794
Skip Montanarod581d772002-09-03 20:10:45 +0000795 if (--_Py_Ticker < 0) {
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000796 if (*next_instr == SETUP_FINALLY) {
797 /* Make the last opcode before
798 a try: finally: block uninterruptable. */
799 goto fast_next_opcode;
800 }
Skip Montanarod581d772002-09-03 20:10:45 +0000801 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000802 tstate->tick_counter++;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000803 if (things_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +0000804 if (Py_MakePendingCalls() < 0) {
805 why = WHY_EXCEPTION;
806 goto on_error;
807 }
808 }
Jack Janseneddc1442003-11-20 01:44:59 +0000809#if !defined(HAVE_SIGNAL_H)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000810 /* If we have true signals, the signal handler
811 will call Py_AddPendingCall() so we don't
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000812 have to call PyErr_CheckSignals(). On the
813 Mac and DOS, alas, we have to call it. */
Guido van Rossumb209a111997-04-29 18:18:01 +0000814 if (PyErr_CheckSignals()) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000815 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +0000816 goto on_error;
817 }
Guido van Rossum70d44781997-01-21 06:15:24 +0000818#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000819
Guido van Rossume59214e1994-08-30 08:01:59 +0000820#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000821 if (interpreter_lock) {
822 /* Give another thread a chance */
823
Guido van Rossum25ce5661997-08-02 03:10:38 +0000824 if (PyThreadState_Swap(NULL) != tstate)
825 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000826 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000827
828 /* Other threads may run now */
829
Guido van Rossum65d5b571998-12-21 19:32:43 +0000830 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000831 if (PyThreadState_Swap(tstate) != NULL)
832 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000833
834 /* Check for thread interrupts */
835
836 if (tstate->async_exc != NULL) {
837 x = tstate->async_exc;
838 tstate->async_exc = NULL;
839 PyErr_SetNone(x);
840 Py_DECREF(x);
841 why = WHY_EXCEPTION;
842 goto on_error;
843 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000844 }
845#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000846 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000847
Neil Schemenauer63543862002-02-17 19:10:14 +0000848 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +0000849 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +0000850
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000851 /* line-by-line tracing support */
852
853 if (tstate->c_tracefunc != NULL && !tstate->tracing) {
854 /* see maybe_call_line_trace
855 for expository comments */
856 f->f_stacktop = stack_pointer;
Michael W. Hudson006c7522002-11-08 13:08:46 +0000857
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000858 err = maybe_call_line_trace(tstate->c_tracefunc,
859 tstate->c_traceobj,
860 f, &instr_lb, &instr_ub);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000861 /* Reload possibly changed frame fields */
862 JUMPTO(f->f_lasti);
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000863 if (f->f_stacktop != NULL) {
864 stack_pointer = f->f_stacktop;
865 f->f_stacktop = NULL;
866 }
867 if (err) {
868 /* trace function raised an exception */
869 goto on_error;
870 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000871 }
872
873 /* Extract opcode and argument */
874
Guido van Rossum374a9221991-04-04 10:40:29 +0000875 opcode = NEXTOP();
876 if (HAS_ARG(opcode))
877 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +0000878 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +0000879#ifdef DYNAMIC_EXECUTION_PROFILE
880#ifdef DXPAIRS
881 dxpairs[lastopcode][opcode]++;
882 lastopcode = opcode;
883#endif
884 dxp[opcode]++;
885#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000886
Guido van Rossum96a42c81992-01-12 02:29:51 +0000887#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +0000888 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +0000889
Guido van Rossum96a42c81992-01-12 02:29:51 +0000890 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000891 if (HAS_ARG(opcode)) {
892 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000893 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +0000894 }
895 else {
896 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000897 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +0000898 }
899 }
900#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000901
Guido van Rossum374a9221991-04-04 10:40:29 +0000902 /* Main switch on opcode */
Jeremy Hylton52820442001-01-03 23:52:36 +0000903
Guido van Rossum374a9221991-04-04 10:40:29 +0000904 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +0000905
Guido van Rossum374a9221991-04-04 10:40:29 +0000906 /* BEWARE!
907 It is essential that any operation that fails sets either
908 x to NULL, err to nonzero, or why to anything but WHY_NOT,
909 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000910
Guido van Rossum374a9221991-04-04 10:40:29 +0000911 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000912
Neil Schemenauer63543862002-02-17 19:10:14 +0000913 case LOAD_FAST:
914 x = GETLOCAL(oparg);
915 if (x != NULL) {
916 Py_INCREF(x);
917 PUSH(x);
918 goto fast_next_opcode;
919 }
920 format_exc_check_arg(PyExc_UnboundLocalError,
921 UNBOUNDLOCAL_ERROR_MSG,
922 PyTuple_GetItem(co->co_varnames, oparg));
923 break;
924
925 case LOAD_CONST:
Skip Montanaro04d80f82002-08-04 21:03:35 +0000926 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +0000927 Py_INCREF(x);
928 PUSH(x);
929 goto fast_next_opcode;
930
Raymond Hettinger7dc52212003-03-16 20:14:44 +0000931 PREDICTED_WITH_ARG(STORE_FAST);
Neil Schemenauer63543862002-02-17 19:10:14 +0000932 case STORE_FAST:
933 v = POP();
934 SETLOCAL(oparg, v);
935 goto fast_next_opcode;
936
Raymond Hettingerf606f872003-03-16 03:11:04 +0000937 PREDICTED(POP_TOP);
Guido van Rossum374a9221991-04-04 10:40:29 +0000938 case POP_TOP:
939 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000940 Py_DECREF(v);
Neil Schemenauer63543862002-02-17 19:10:14 +0000941 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000942
Guido van Rossum374a9221991-04-04 10:40:29 +0000943 case ROT_TWO:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000944 v = TOP();
945 w = SECOND();
946 SET_TOP(w);
947 SET_SECOND(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000948 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000949
Guido van Rossum374a9221991-04-04 10:40:29 +0000950 case ROT_THREE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000951 v = TOP();
952 w = SECOND();
953 x = THIRD();
954 SET_TOP(w);
955 SET_SECOND(x);
956 SET_THIRD(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000957 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000958
Thomas Wouters434d0822000-08-24 20:11:32 +0000959 case ROT_FOUR:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000960 u = TOP();
961 v = SECOND();
962 w = THIRD();
963 x = FOURTH();
964 SET_TOP(v);
965 SET_SECOND(w);
966 SET_THIRD(x);
967 SET_FOURTH(u);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000968 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000969
Guido van Rossum374a9221991-04-04 10:40:29 +0000970 case DUP_TOP:
971 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000972 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000973 PUSH(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000974 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000975
Thomas Wouters434d0822000-08-24 20:11:32 +0000976 case DUP_TOPX:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000977 if (oparg == 2) {
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 STACKADJ(2);
983 SET_TOP(x);
984 SET_SECOND(w);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000985 goto fast_next_opcode;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000986 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000987 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000988 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000989 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000990 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000991 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +0000992 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000993 STACKADJ(3);
994 SET_TOP(x);
995 SET_SECOND(w);
996 SET_THIRD(v);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000997 goto fast_next_opcode;
Thomas Wouters434d0822000-08-24 20:11:32 +0000998 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000999 Py_FatalError("invalid argument to DUP_TOPX"
1000 " (bytecode corruption?)");
Tim Peters35ba6892000-10-11 07:04:49 +00001001 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001002
Guido van Rossum374a9221991-04-04 10:40:29 +00001003 case UNARY_POSITIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001004 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001005 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001006 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001007 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001008 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001009 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001010
Guido van Rossum374a9221991-04-04 10:40:29 +00001011 case UNARY_NEGATIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001012 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001013 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001014 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001015 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001016 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001017 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001018
Guido van Rossum374a9221991-04-04 10:40:29 +00001019 case UNARY_NOT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001020 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001021 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001022 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +00001023 if (err == 0) {
1024 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001025 SET_TOP(Py_True);
Guido van Rossumfc490731997-05-06 15:06:49 +00001026 continue;
1027 }
1028 else if (err > 0) {
1029 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001030 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +00001031 err = 0;
1032 continue;
1033 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00001034 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001035 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001036
Guido van Rossum374a9221991-04-04 10:40:29 +00001037 case UNARY_CONVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001038 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001039 x = PyObject_Repr(v);
1040 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001041 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001042 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001043 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001044
Guido van Rossum7928cd71991-10-24 14:59:31 +00001045 case UNARY_INVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001046 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001047 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001048 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001049 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001050 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001051 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001052
Guido van Rossum50564e81996-01-12 01:13:16 +00001053 case BINARY_POWER:
1054 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001055 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001056 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +00001057 Py_DECREF(v);
1058 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001059 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001060 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +00001061 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001062
Guido van Rossum374a9221991-04-04 10:40:29 +00001063 case BINARY_MULTIPLY:
1064 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001065 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001066 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001067 Py_DECREF(v);
1068 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001069 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001070 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001071 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001072
Guido van Rossum374a9221991-04-04 10:40:29 +00001073 case BINARY_DIVIDE:
Tim Peters3caca232001-12-06 06:23:26 +00001074 if (!_Py_QnewFlag) {
1075 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001076 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001077 x = PyNumber_Divide(v, w);
1078 Py_DECREF(v);
1079 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001080 SET_TOP(x);
Tim Peters3caca232001-12-06 06:23:26 +00001081 if (x != NULL) continue;
1082 break;
1083 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001084 /* -Qnew is in effect: fall through to
Tim Peters3caca232001-12-06 06:23:26 +00001085 BINARY_TRUE_DIVIDE */
1086 case BINARY_TRUE_DIVIDE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001087 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001088 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001089 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001090 Py_DECREF(v);
1091 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001092 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001093 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001094 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001095
Guido van Rossum4668b002001-08-08 05:00:18 +00001096 case BINARY_FLOOR_DIVIDE:
1097 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001098 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001099 x = PyNumber_FloorDivide(v, w);
1100 Py_DECREF(v);
1101 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001102 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001103 if (x != NULL) continue;
1104 break;
1105
Guido van Rossum374a9221991-04-04 10:40:29 +00001106 case BINARY_MODULO:
1107 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001108 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001109 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001110 Py_DECREF(v);
1111 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001112 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001113 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001114 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001115
Guido van Rossum374a9221991-04-04 10:40:29 +00001116 case BINARY_ADD:
1117 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001118 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001119 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001120 /* INLINE: int + int */
1121 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001122 a = PyInt_AS_LONG(v);
1123 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001124 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001125 if ((i^a) < 0 && (i^b) < 0)
1126 goto slow_add;
1127 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001128 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001129 else {
1130 slow_add:
Guido van Rossumc12da691997-07-17 23:12:42 +00001131 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001132 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001133 Py_DECREF(v);
1134 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001135 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001136 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001137 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001138
Guido van Rossum374a9221991-04-04 10:40:29 +00001139 case BINARY_SUBTRACT:
1140 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001141 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001142 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001143 /* INLINE: int - int */
1144 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001145 a = PyInt_AS_LONG(v);
1146 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001147 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001148 if ((i^a) < 0 && (i^~b) < 0)
1149 goto slow_sub;
1150 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001151 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001152 else {
1153 slow_sub:
Guido van Rossumc12da691997-07-17 23:12:42 +00001154 x = PyNumber_Subtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001155 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001156 Py_DECREF(v);
1157 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001158 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001159 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001160 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001161
Guido van Rossum374a9221991-04-04 10:40:29 +00001162 case BINARY_SUBSCR:
1163 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001164 v = TOP();
Tim Petersb1c46982001-10-05 20:41:38 +00001165 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001166 /* INLINE: list[int] */
1167 long i = PyInt_AsLong(w);
1168 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +00001169 i += PyList_GET_SIZE(v);
Guido van Rossumc12da691997-07-17 23:12:42 +00001170 if (i < 0 ||
Guido van Rossumfa00e951998-07-08 15:02:37 +00001171 i >= PyList_GET_SIZE(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001172 PyErr_SetString(PyExc_IndexError,
1173 "list index out of range");
1174 x = NULL;
1175 }
1176 else {
Guido van Rossumfa00e951998-07-08 15:02:37 +00001177 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001178 Py_INCREF(x);
1179 }
1180 }
1181 else
1182 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001183 Py_DECREF(v);
1184 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001185 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001186 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001187 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001188
Guido van Rossum7928cd71991-10-24 14:59:31 +00001189 case BINARY_LSHIFT:
1190 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001191 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001192 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001193 Py_DECREF(v);
1194 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001195 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001196 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001197 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001198
Guido van Rossum7928cd71991-10-24 14:59:31 +00001199 case BINARY_RSHIFT:
1200 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001201 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001202 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001203 Py_DECREF(v);
1204 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001205 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001206 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001207 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001208
Guido van Rossum7928cd71991-10-24 14:59:31 +00001209 case BINARY_AND:
1210 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001211 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001212 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001213 Py_DECREF(v);
1214 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001215 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001216 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001217 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001218
Guido van Rossum7928cd71991-10-24 14:59:31 +00001219 case BINARY_XOR:
1220 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001221 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001222 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001223 Py_DECREF(v);
1224 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001225 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001226 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001227 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001228
Guido van Rossum7928cd71991-10-24 14:59:31 +00001229 case BINARY_OR:
1230 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001231 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001232 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001233 Py_DECREF(v);
1234 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001235 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001236 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001237 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001238
1239 case INPLACE_POWER:
1240 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001241 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001242 x = PyNumber_InPlacePower(v, w, Py_None);
1243 Py_DECREF(v);
1244 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001245 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001246 if (x != NULL) continue;
1247 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001248
Thomas Wouters434d0822000-08-24 20:11:32 +00001249 case INPLACE_MULTIPLY:
1250 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001251 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001252 x = PyNumber_InPlaceMultiply(v, w);
1253 Py_DECREF(v);
1254 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001255 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001256 if (x != NULL) continue;
1257 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001258
Thomas Wouters434d0822000-08-24 20:11:32 +00001259 case INPLACE_DIVIDE:
Tim Peters54b11912001-12-25 18:49:11 +00001260 if (!_Py_QnewFlag) {
1261 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001262 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001263 x = PyNumber_InPlaceDivide(v, w);
1264 Py_DECREF(v);
1265 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001266 SET_TOP(x);
Tim Peters54b11912001-12-25 18:49:11 +00001267 if (x != NULL) continue;
1268 break;
1269 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001270 /* -Qnew is in effect: fall through to
Tim Peters54b11912001-12-25 18:49:11 +00001271 INPLACE_TRUE_DIVIDE */
1272 case INPLACE_TRUE_DIVIDE:
Thomas Wouters434d0822000-08-24 20:11:32 +00001273 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001274 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001275 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001276 Py_DECREF(v);
1277 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001278 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001279 if (x != NULL) continue;
1280 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001281
Guido van Rossum4668b002001-08-08 05:00:18 +00001282 case INPLACE_FLOOR_DIVIDE:
1283 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001284 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001285 x = PyNumber_InPlaceFloorDivide(v, w);
1286 Py_DECREF(v);
1287 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001288 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001289 if (x != NULL) continue;
1290 break;
1291
Thomas Wouters434d0822000-08-24 20:11:32 +00001292 case INPLACE_MODULO:
1293 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001294 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001295 x = PyNumber_InPlaceRemainder(v, w);
1296 Py_DECREF(v);
1297 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001298 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001299 if (x != NULL) continue;
1300 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001301
Thomas Wouters434d0822000-08-24 20:11:32 +00001302 case INPLACE_ADD:
1303 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001304 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001305 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001306 /* INLINE: int + int */
1307 register long a, b, i;
1308 a = PyInt_AS_LONG(v);
1309 b = PyInt_AS_LONG(w);
1310 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001311 if ((i^a) < 0 && (i^b) < 0)
1312 goto slow_iadd;
1313 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001314 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001315 else {
1316 slow_iadd:
Thomas Wouters434d0822000-08-24 20:11:32 +00001317 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001318 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001319 Py_DECREF(v);
1320 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001321 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001322 if (x != NULL) continue;
1323 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001324
Thomas Wouters434d0822000-08-24 20:11:32 +00001325 case INPLACE_SUBTRACT:
1326 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001327 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001328 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001329 /* INLINE: int - int */
1330 register long a, b, i;
1331 a = PyInt_AS_LONG(v);
1332 b = PyInt_AS_LONG(w);
1333 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001334 if ((i^a) < 0 && (i^~b) < 0)
1335 goto slow_isub;
1336 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001337 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001338 else {
1339 slow_isub:
Thomas Wouters434d0822000-08-24 20:11:32 +00001340 x = PyNumber_InPlaceSubtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001341 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001342 Py_DECREF(v);
1343 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001344 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001345 if (x != NULL) continue;
1346 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001347
Thomas Wouters434d0822000-08-24 20:11:32 +00001348 case INPLACE_LSHIFT:
1349 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001350 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001351 x = PyNumber_InPlaceLshift(v, w);
1352 Py_DECREF(v);
1353 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001354 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001355 if (x != NULL) continue;
1356 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001357
Thomas Wouters434d0822000-08-24 20:11:32 +00001358 case INPLACE_RSHIFT:
1359 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001360 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001361 x = PyNumber_InPlaceRshift(v, w);
1362 Py_DECREF(v);
1363 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001364 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001365 if (x != NULL) continue;
1366 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001367
Thomas Wouters434d0822000-08-24 20:11:32 +00001368 case INPLACE_AND:
1369 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001370 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001371 x = PyNumber_InPlaceAnd(v, w);
1372 Py_DECREF(v);
1373 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001374 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001375 if (x != NULL) continue;
1376 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001377
Thomas Wouters434d0822000-08-24 20:11:32 +00001378 case INPLACE_XOR:
1379 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001380 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001381 x = PyNumber_InPlaceXor(v, w);
1382 Py_DECREF(v);
1383 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001384 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001385 if (x != NULL) continue;
1386 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001387
Thomas Wouters434d0822000-08-24 20:11:32 +00001388 case INPLACE_OR:
1389 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001390 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001391 x = PyNumber_InPlaceOr(v, w);
1392 Py_DECREF(v);
1393 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001394 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001395 if (x != NULL) continue;
1396 break;
1397
Guido van Rossum374a9221991-04-04 10:40:29 +00001398 case SLICE+0:
1399 case SLICE+1:
1400 case SLICE+2:
1401 case SLICE+3:
1402 if ((opcode-SLICE) & 2)
1403 w = POP();
1404 else
1405 w = NULL;
1406 if ((opcode-SLICE) & 1)
1407 v = POP();
1408 else
1409 v = NULL;
Raymond Hettinger663004b2003-01-09 15:24:30 +00001410 u = TOP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001411 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001412 Py_DECREF(u);
1413 Py_XDECREF(v);
1414 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001415 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001416 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001417 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001418
Guido van Rossum374a9221991-04-04 10:40:29 +00001419 case STORE_SLICE+0:
1420 case STORE_SLICE+1:
1421 case STORE_SLICE+2:
1422 case STORE_SLICE+3:
1423 if ((opcode-STORE_SLICE) & 2)
1424 w = POP();
1425 else
1426 w = NULL;
1427 if ((opcode-STORE_SLICE) & 1)
1428 v = POP();
1429 else
1430 v = NULL;
1431 u = POP();
1432 t = POP();
1433 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +00001434 Py_DECREF(t);
1435 Py_DECREF(u);
1436 Py_XDECREF(v);
1437 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001438 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001439 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001440
Guido van Rossum374a9221991-04-04 10:40:29 +00001441 case DELETE_SLICE+0:
1442 case DELETE_SLICE+1:
1443 case DELETE_SLICE+2:
1444 case DELETE_SLICE+3:
1445 if ((opcode-DELETE_SLICE) & 2)
1446 w = POP();
1447 else
1448 w = NULL;
1449 if ((opcode-DELETE_SLICE) & 1)
1450 v = POP();
1451 else
1452 v = NULL;
1453 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001454 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +00001455 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00001456 Py_DECREF(u);
1457 Py_XDECREF(v);
1458 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001459 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001460 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001461
Guido van Rossum374a9221991-04-04 10:40:29 +00001462 case STORE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001463 w = TOP();
1464 v = SECOND();
1465 u = THIRD();
1466 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001467 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001468 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001469 Py_DECREF(u);
1470 Py_DECREF(v);
1471 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001472 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001473 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001474
Guido van Rossum374a9221991-04-04 10:40:29 +00001475 case DELETE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001476 w = TOP();
1477 v = SECOND();
1478 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001479 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001480 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001481 Py_DECREF(v);
1482 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001483 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001484 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001485
Guido van Rossum374a9221991-04-04 10:40:29 +00001486 case PRINT_EXPR:
1487 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001488 w = PySys_GetObject("displayhook");
1489 if (w == NULL) {
1490 PyErr_SetString(PyExc_RuntimeError,
1491 "lost sys.displayhook");
1492 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001493 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001494 }
1495 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001496 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001497 if (x == NULL)
1498 err = -1;
1499 }
1500 if (err == 0) {
1501 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001502 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001503 if (w == NULL)
1504 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001505 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001506 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001507 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001508 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001509
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001510 case PRINT_ITEM_TO:
1511 w = stream = POP();
1512 /* fall through to PRINT_ITEM */
1513
Guido van Rossum374a9221991-04-04 10:40:29 +00001514 case PRINT_ITEM:
1515 v = POP();
Barry Warsaw093abe02000-08-29 04:56:13 +00001516 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001517 w = PySys_GetObject("stdout");
1518 if (w == NULL) {
1519 PyErr_SetString(PyExc_RuntimeError,
1520 "lost sys.stdout");
1521 err = -1;
1522 }
Guido van Rossum8f183201997-12-31 05:53:15 +00001523 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001524 /* PyFile_SoftSpace() can exececute arbitrary code
1525 if sys.stdout is an instance with a __getattr__.
1526 If __getattr__ raises an exception, w will
1527 be freed, so we need to prevent that temporarily. */
1528 Py_XINCREF(w);
Tim Peters8e5fd532002-03-24 19:25:00 +00001529 if (w != NULL && PyFile_SoftSpace(w, 0))
Guido van Rossumbe270261997-05-22 22:26:18 +00001530 err = PyFile_WriteString(" ", w);
1531 if (err == 0)
1532 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001533 if (err == 0) {
Tim Peters8e5fd532002-03-24 19:25:00 +00001534 /* XXX move into writeobject() ? */
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001535 if (PyString_Check(v)) {
1536 char *s = PyString_AS_STRING(v);
1537 int len = PyString_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001538 if (len == 0 ||
1539 !isspace(Py_CHARMASK(s[len-1])) ||
1540 s[len-1] == ' ')
1541 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001542 }
Martin v. Löwis8d3ce5a2001-12-18 22:36:40 +00001543#ifdef Py_USING_UNICODE
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001544 else if (PyUnicode_Check(v)) {
1545 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
1546 int len = PyUnicode_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001547 if (len == 0 ||
1548 !Py_UNICODE_ISSPACE(s[len-1]) ||
1549 s[len-1] == ' ')
1550 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001551 }
Michael W. Hudsond95c8282002-05-20 13:56:11 +00001552#endif
Tim Peters8e5fd532002-03-24 19:25:00 +00001553 else
1554 PyFile_SoftSpace(w, 1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001555 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001556 Py_XDECREF(w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001557 Py_DECREF(v);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001558 Py_XDECREF(stream);
1559 stream = NULL;
1560 if (err == 0)
1561 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001562 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001563
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001564 case PRINT_NEWLINE_TO:
1565 w = stream = POP();
1566 /* fall through to PRINT_NEWLINE */
1567
Guido van Rossum374a9221991-04-04 10:40:29 +00001568 case PRINT_NEWLINE:
Barry Warsaw093abe02000-08-29 04:56:13 +00001569 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001570 w = PySys_GetObject("stdout");
1571 if (w == NULL)
1572 PyErr_SetString(PyExc_RuntimeError,
1573 "lost sys.stdout");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001574 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001575 if (w != NULL) {
1576 err = PyFile_WriteString("\n", w);
1577 if (err == 0)
1578 PyFile_SoftSpace(w, 0);
1579 }
1580 Py_XDECREF(stream);
1581 stream = NULL;
Guido van Rossum374a9221991-04-04 10:40:29 +00001582 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001583
Thomas Wouters434d0822000-08-24 20:11:32 +00001584
1585#ifdef CASE_TOO_BIG
1586 default: switch (opcode) {
1587#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001588 case BREAK_LOOP:
1589 why = WHY_BREAK;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001590 goto fast_block_end;
Guido van Rossum66b0e9c2001-03-21 19:17:22 +00001591
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00001592 case CONTINUE_LOOP:
1593 retval = PyInt_FromLong(oparg);
1594 why = WHY_CONTINUE;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001595 goto fast_block_end;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001596
Guido van Rossumf10570b1995-07-07 22:53:21 +00001597 case RAISE_VARARGS:
1598 u = v = w = NULL;
1599 switch (oparg) {
1600 case 3:
1601 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001602 /* Fallthrough */
1603 case 2:
1604 v = POP(); /* value */
1605 /* Fallthrough */
1606 case 1:
1607 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001608 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001609 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001610 break;
1611 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001612 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001613 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001614 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001615 break;
1616 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001617 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001618
Guido van Rossum374a9221991-04-04 10:40:29 +00001619 case LOAD_LOCALS:
Guido van Rossum681d79a1995-07-18 14:51:37 +00001620 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001621 PyErr_SetString(PyExc_SystemError,
1622 "no locals");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001623 break;
1624 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001625 Py_INCREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001626 PUSH(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001627 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001628
Guido van Rossum374a9221991-04-04 10:40:29 +00001629 case RETURN_VALUE:
1630 retval = POP();
1631 why = WHY_RETURN;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001632 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001633
Tim Peters5ca576e2001-06-18 22:08:13 +00001634 case YIELD_VALUE:
1635 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001636 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001637 why = WHY_YIELD;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001638 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001639
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001640 case EXEC_STMT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001641 w = TOP();
1642 v = SECOND();
1643 u = THIRD();
1644 STACKADJ(-3);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001645 err = exec_statement(f, u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001646 Py_DECREF(u);
1647 Py_DECREF(v);
1648 Py_DECREF(w);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001649 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001650
Guido van Rossum374a9221991-04-04 10:40:29 +00001651 case POP_BLOCK:
1652 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001653 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001654 while (STACK_LEVEL() > b->b_level) {
1655 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001656 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001657 }
1658 }
1659 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001660
Guido van Rossum374a9221991-04-04 10:40:29 +00001661 case END_FINALLY:
1662 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001663 if (PyInt_Check(v)) {
Raymond Hettinger080cb322003-03-14 01:37:42 +00001664 why = (enum why_code) PyInt_AS_LONG(v);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00001665 if (why == WHY_RETURN ||
Tim Peters5ca576e2001-06-18 22:08:13 +00001666 why == WHY_YIELD ||
Guido van Rossumc5fe5eb2002-06-12 03:45:21 +00001667 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001668 retval = POP();
1669 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001670 else if (PyString_Check(v) || PyClass_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001671 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001672 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001673 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001674 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001675 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001676 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001677 else if (v != Py_None) {
1678 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001679 "'finally' pops bad exception");
1680 why = WHY_EXCEPTION;
1681 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001682 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001683 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001684
Guido van Rossum374a9221991-04-04 10:40:29 +00001685 case BUILD_CLASS:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001686 u = TOP();
1687 v = SECOND();
1688 w = THIRD();
1689 STACKADJ(-2);
Guido van Rossum25831651993-05-19 14:50:45 +00001690 x = build_class(u, v, w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001691 SET_TOP(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001692 Py_DECREF(u);
1693 Py_DECREF(v);
1694 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001695 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001696
Guido van Rossum374a9221991-04-04 10:40:29 +00001697 case STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001698 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001699 v = POP();
Guido van Rossum681d79a1995-07-18 14:51:37 +00001700 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001701 PyErr_Format(PyExc_SystemError,
1702 "no locals found when storing %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001703 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001704 break;
1705 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001706 err = PyDict_SetItem(x, w, v);
1707 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001708 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001709
Guido van Rossum374a9221991-04-04 10:40:29 +00001710 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001711 w = GETITEM(names, oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001712 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001713 PyErr_Format(PyExc_SystemError,
1714 "no locals when deleting %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001715 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001716 break;
1717 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001718 if ((err = PyDict_DelItem(x, w)) != 0)
Guido van Rossumac7be682001-01-17 15:42:30 +00001719 format_exc_check_arg(PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001720 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001721 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001722
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001723 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001724 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001725 v = POP();
Raymond Hettinger21012b82003-02-26 18:11:50 +00001726 if (PyTuple_CheckExact(v)) {
Barry Warsawe42b18f1997-08-25 22:13:04 +00001727 if (PyTuple_Size(v) != oparg) {
1728 PyErr_SetString(PyExc_ValueError,
1729 "unpack tuple of wrong size");
1730 why = WHY_EXCEPTION;
1731 }
1732 else {
1733 for (; --oparg >= 0; ) {
1734 w = PyTuple_GET_ITEM(v, oparg);
1735 Py_INCREF(w);
1736 PUSH(w);
1737 }
1738 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001739 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00001740 else if (PyList_CheckExact(v)) {
Barry Warsawe42b18f1997-08-25 22:13:04 +00001741 if (PyList_Size(v) != oparg) {
1742 PyErr_SetString(PyExc_ValueError,
1743 "unpack list of wrong size");
1744 why = WHY_EXCEPTION;
1745 }
1746 else {
1747 for (; --oparg >= 0; ) {
1748 w = PyList_GET_ITEM(v, oparg);
1749 Py_INCREF(w);
1750 PUSH(w);
1751 }
1752 }
1753 }
Tim Petersd6d010b2001-06-21 02:49:55 +00001754 else if (unpack_iterable(v, oparg,
1755 stack_pointer + oparg))
1756 stack_pointer += oparg;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001757 else {
1758 if (PyErr_ExceptionMatches(PyExc_TypeError))
1759 PyErr_SetString(PyExc_TypeError,
1760 "unpack non-sequence");
Barry Warsawe42b18f1997-08-25 22:13:04 +00001761 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001762 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001763 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001764 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001765
Guido van Rossum374a9221991-04-04 10:40:29 +00001766 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001767 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001768 v = TOP();
1769 u = SECOND();
1770 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001771 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1772 Py_DECREF(v);
1773 Py_DECREF(u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001774 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001775
Guido van Rossum374a9221991-04-04 10:40:29 +00001776 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001777 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001778 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001779 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1780 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001781 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001782 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001783
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001784 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001785 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001786 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001787 err = PyDict_SetItem(f->f_globals, w, v);
1788 Py_DECREF(v);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001789 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001790
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001791 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001792 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001793 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001794 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001795 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001796 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001797
Guido van Rossum374a9221991-04-04 10:40:29 +00001798 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001799 w = GETITEM(names, oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001800 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001801 PyErr_Format(PyExc_SystemError,
1802 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001803 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001804 break;
1805 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001806 x = PyDict_GetItem(x, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001807 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001808 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001809 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001810 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001811 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001812 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001813 PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001814 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001815 break;
1816 }
1817 }
1818 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001819 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001820 PUSH(x);
1821 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001822
Guido van Rossum374a9221991-04-04 10:40:29 +00001823 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001824 w = GETITEM(names, oparg);
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001825 if (PyString_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00001826 /* Inline the PyDict_GetItem() calls.
1827 WARNING: this is an extreme speed hack.
1828 Do not try this at home. */
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001829 long hash = ((PyStringObject *)w)->ob_shash;
1830 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001831 PyDictObject *d;
1832 d = (PyDictObject *)(f->f_globals);
1833 x = d->ma_lookup(d, w, hash)->me_value;
1834 if (x != NULL) {
1835 Py_INCREF(x);
1836 PUSH(x);
1837 continue;
1838 }
1839 d = (PyDictObject *)(f->f_builtins);
1840 x = d->ma_lookup(d, w, hash)->me_value;
1841 if (x != NULL) {
1842 Py_INCREF(x);
1843 PUSH(x);
1844 continue;
1845 }
1846 goto load_global_error;
1847 }
1848 }
1849 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00001850 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001851 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001852 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001853 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001854 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00001855 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001856 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001857 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001858 break;
1859 }
1860 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001861 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001862 PUSH(x);
1863 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001864
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001865 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001866 x = GETLOCAL(oparg);
1867 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001868 format_exc_check_arg(
1869 PyExc_UnboundLocalError,
1870 UNBOUNDLOCAL_ERROR_MSG,
1871 PyTuple_GetItem(co->co_varnames, oparg)
1872 );
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001873 break;
1874 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00001875 SETLOCAL(oparg, NULL);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001876 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001877
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001878 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001879 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001880 Py_INCREF(x);
1881 PUSH(x);
1882 break;
1883
1884 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001885 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001886 w = PyCell_Get(x);
Jeremy Hylton2524d692001-02-05 17:23:16 +00001887 if (w == NULL) {
Jeremy Hylton76c81ee2002-07-11 16:56:38 +00001888 err = -1;
1889 /* Don't stomp existing exception */
1890 if (PyErr_Occurred())
1891 break;
Jeremy Hyltonc76770c2001-04-13 16:51:46 +00001892 if (oparg < f->f_ncells) {
Jeremy Hylton2524d692001-02-05 17:23:16 +00001893 v = PyTuple_GetItem(co->co_cellvars,
1894 oparg);
Jeremy Hyltonc76770c2001-04-13 16:51:46 +00001895 format_exc_check_arg(
1896 PyExc_UnboundLocalError,
1897 UNBOUNDLOCAL_ERROR_MSG,
1898 v);
1899 } else {
Jeremy Hylton2524d692001-02-05 17:23:16 +00001900 v = PyTuple_GetItem(
1901 co->co_freevars,
1902 oparg - f->f_ncells);
Jeremy Hyltonc76770c2001-04-13 16:51:46 +00001903 format_exc_check_arg(
1904 PyExc_NameError,
1905 UNBOUNDFREE_ERROR_MSG,
1906 v);
1907 }
Jeremy Hylton2524d692001-02-05 17:23:16 +00001908 break;
1909 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001910 PUSH(w);
1911 break;
1912
1913 case STORE_DEREF:
1914 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001915 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001916 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00001917 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001918 continue;
1919
Guido van Rossum374a9221991-04-04 10:40:29 +00001920 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001921 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001922 if (x != NULL) {
1923 for (; --oparg >= 0;) {
1924 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001925 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001926 }
1927 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001928 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001929 }
1930 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001931
Guido van Rossum374a9221991-04-04 10:40:29 +00001932 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00001933 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001934 if (x != NULL) {
1935 for (; --oparg >= 0;) {
1936 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00001937 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001938 }
1939 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001940 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001941 }
1942 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001943
Guido van Rossum374a9221991-04-04 10:40:29 +00001944 case BUILD_MAP:
Guido van Rossumb209a111997-04-29 18:18:01 +00001945 x = PyDict_New();
Guido van Rossum374a9221991-04-04 10:40:29 +00001946 PUSH(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 LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001951 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001952 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001953 x = PyObject_GetAttr(v, w);
1954 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001955 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001956 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001957 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001958
Guido van Rossum374a9221991-04-04 10:40:29 +00001959 case COMPARE_OP:
1960 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001961 v = TOP();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001962 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001963 /* INLINE: cmp(int, int) */
1964 register long a, b;
1965 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001966 a = PyInt_AS_LONG(v);
1967 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001968 switch (oparg) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00001969 case PyCmp_LT: res = a < b; break;
1970 case PyCmp_LE: res = a <= b; break;
1971 case PyCmp_EQ: res = a == b; break;
1972 case PyCmp_NE: res = a != b; break;
1973 case PyCmp_GT: res = a > b; break;
1974 case PyCmp_GE: res = a >= b; break;
1975 case PyCmp_IS: res = v == w; break;
1976 case PyCmp_IS_NOT: res = v != w; break;
Guido van Rossumc12da691997-07-17 23:12:42 +00001977 default: goto slow_compare;
1978 }
1979 x = res ? Py_True : Py_False;
1980 Py_INCREF(x);
1981 }
1982 else {
1983 slow_compare:
1984 x = cmp_outcome(oparg, v, w);
1985 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001986 Py_DECREF(v);
1987 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001988 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001989 if (x == NULL) break;
1990 PREDICT(JUMP_IF_FALSE);
1991 PREDICT(JUMP_IF_TRUE);
1992 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001993
Guido van Rossum374a9221991-04-04 10:40:29 +00001994 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001995 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001996 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001997 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001998 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00001999 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002000 break;
2001 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00002002 u = TOP();
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002003 w = PyTuple_Pack(4,
Guido van Rossum681d79a1995-07-18 14:51:37 +00002004 w,
2005 f->f_globals,
Guido van Rossuma027efa1997-05-05 20:56:21 +00002006 f->f_locals == NULL ?
2007 Py_None : f->f_locals,
Guido van Rossum681d79a1995-07-18 14:51:37 +00002008 u);
Guido van Rossumb209a111997-04-29 18:18:01 +00002009 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002010 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002011 u = POP();
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002012 x = NULL;
2013 break;
2014 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002015 x = PyEval_CallObject(x, w);
2016 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002017 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002018 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002019 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002020
Thomas Wouters52152252000-08-17 22:55:00 +00002021 case IMPORT_STAR:
2022 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002023 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002024 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002025 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00002026 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002027 break;
2028 }
Thomas Wouters52152252000-08-17 22:55:00 +00002029 err = import_all_from(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002030 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00002031 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002032 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002033 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002034
Thomas Wouters52152252000-08-17 22:55:00 +00002035 case IMPORT_FROM:
Skip Montanaro496e6582002-08-06 17:47:40 +00002036 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00002037 v = TOP();
2038 x = import_from(v, w);
2039 PUSH(x);
2040 if (x != NULL) continue;
2041 break;
2042
Guido van Rossum374a9221991-04-04 10:40:29 +00002043 case JUMP_FORWARD:
2044 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002045 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002046
Raymond Hettingerf606f872003-03-16 03:11:04 +00002047 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002048 case JUMP_IF_FALSE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002049 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002050 if (w == Py_True) {
2051 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002052 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002053 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002054 if (w == Py_False) {
2055 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002056 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002057 }
2058 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002059 if (err > 0)
2060 err = 0;
2061 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00002062 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002063 else
2064 break;
2065 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002066
Raymond Hettingerf606f872003-03-16 03:11:04 +00002067 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002068 case JUMP_IF_TRUE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002069 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002070 if (w == Py_False) {
2071 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002072 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002073 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002074 if (w == Py_True) {
2075 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002076 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002077 }
2078 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002079 if (err > 0) {
2080 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00002081 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002082 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002083 else if (err == 0)
2084 ;
2085 else
2086 break;
2087 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002088
Guido van Rossum374a9221991-04-04 10:40:29 +00002089 case JUMP_ABSOLUTE:
2090 JUMPTO(oparg);
Neil Schemenauerca2a2f12003-05-30 23:59:44 +00002091 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002092
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002093 case GET_ITER:
2094 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002095 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002096 x = PyObject_GetIter(v);
2097 Py_DECREF(v);
2098 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002099 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002100 PREDICT(FOR_ITER);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002101 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002102 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002103 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002104 break;
2105
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002106 PREDICTED_WITH_ARG(FOR_ITER);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002107 case FOR_ITER:
2108 /* before: [iter]; after: [iter, iter()] *or* [] */
2109 v = TOP();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002110 x = PyIter_Next(v);
2111 if (x != NULL) {
2112 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002113 PREDICT(STORE_FAST);
2114 PREDICT(UNPACK_SEQUENCE);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002115 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002116 }
Tim Petersf4848da2001-05-05 00:14:56 +00002117 if (!PyErr_Occurred()) {
2118 /* iterator ended normally */
2119 x = v = POP();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002120 Py_DECREF(v);
2121 JUMPBY(oparg);
2122 continue;
2123 }
2124 break;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002125
Guido van Rossum374a9221991-04-04 10:40:29 +00002126 case SETUP_LOOP:
2127 case SETUP_EXCEPT:
2128 case SETUP_FINALLY:
Guido van Rossumb209a111997-04-29 18:18:01 +00002129 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002130 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002131 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002132
Guido van Rossumf10570b1995-07-07 22:53:21 +00002133 case CALL_FUNCTION:
Jeremy Hylton985eba52003-02-05 23:13:00 +00002134 PCALL(PCALL_ALL);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002135 x = call_function(&stack_pointer, oparg);
2136 PUSH(x);
2137 if (x != NULL)
2138 continue;
2139 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002140
Jeremy Hylton76901512000-03-28 23:49:17 +00002141 case CALL_FUNCTION_VAR:
2142 case CALL_FUNCTION_KW:
2143 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002144 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002145 int na = oparg & 0xff;
2146 int nk = (oparg>>8) & 0xff;
2147 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002148 int n = na + 2 * nk;
2149 PyObject **pfunc, *func;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002150 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002151 if (flags & CALL_FLAG_VAR)
2152 n++;
2153 if (flags & CALL_FLAG_KW)
2154 n++;
2155 pfunc = stack_pointer - n - 1;
2156 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002157
Guido van Rossumac7be682001-01-17 15:42:30 +00002158 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002159 && PyMethod_GET_SELF(func) != NULL) {
2160 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002161 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002162 func = PyMethod_GET_FUNCTION(func);
2163 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002164 Py_DECREF(*pfunc);
2165 *pfunc = self;
2166 na++;
2167 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002168 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002169 Py_INCREF(func);
2170 x = ext_do_call(func, &stack_pointer, flags, na, nk);
Jeremy Hylton76901512000-03-28 23:49:17 +00002171 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002172
Jeremy Hylton76901512000-03-28 23:49:17 +00002173 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002174 w = POP();
2175 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002176 }
2177 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002178 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002179 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002180 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002181 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002182
Guido van Rossum681d79a1995-07-18 14:51:37 +00002183 case MAKE_FUNCTION:
2184 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002185 x = PyFunction_New(v, f->f_globals);
2186 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002187 /* XXX Maybe this should be a separate opcode? */
2188 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002189 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002190 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002191 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002192 x = NULL;
2193 break;
2194 }
2195 while (--oparg >= 0) {
2196 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002197 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002198 }
2199 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002200 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002201 }
2202 PUSH(x);
2203 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002204
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002205 case MAKE_CLOSURE:
2206 {
2207 int nfree;
2208 v = POP(); /* code object */
2209 x = PyFunction_New(v, f->f_globals);
Jeremy Hylton733c8932001-12-13 19:51:56 +00002210 nfree = PyCode_GetNumFree((PyCodeObject *)v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002211 Py_DECREF(v);
2212 /* XXX Maybe this should be a separate opcode? */
2213 if (x != NULL && nfree > 0) {
2214 v = PyTuple_New(nfree);
2215 if (v == NULL) {
2216 Py_DECREF(x);
2217 x = NULL;
2218 break;
2219 }
2220 while (--nfree >= 0) {
2221 w = POP();
2222 PyTuple_SET_ITEM(v, nfree, w);
2223 }
2224 err = PyFunction_SetClosure(x, v);
2225 Py_DECREF(v);
2226 }
2227 if (x != NULL && oparg > 0) {
2228 v = PyTuple_New(oparg);
2229 if (v == NULL) {
2230 Py_DECREF(x);
2231 x = NULL;
2232 break;
2233 }
2234 while (--oparg >= 0) {
2235 w = POP();
2236 PyTuple_SET_ITEM(v, oparg, w);
2237 }
2238 err = PyFunction_SetDefaults(x, v);
2239 Py_DECREF(v);
2240 }
2241 PUSH(x);
2242 break;
2243 }
2244
Guido van Rossum8861b741996-07-30 16:49:37 +00002245 case BUILD_SLICE:
2246 if (oparg == 3)
2247 w = POP();
2248 else
2249 w = NULL;
2250 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002251 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002252 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002253 Py_DECREF(u);
2254 Py_DECREF(v);
2255 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002256 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002257 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002258 break;
2259
Fred Drakeef8ace32000-08-24 00:32:09 +00002260 case EXTENDED_ARG:
2261 opcode = NEXTOP();
2262 oparg = oparg<<16 | NEXTARG();
2263 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002264
Guido van Rossum374a9221991-04-04 10:40:29 +00002265 default:
2266 fprintf(stderr,
2267 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002268 PyCode_Addr2Line(f->f_code, f->f_lasti),
2269 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002270 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002271 why = WHY_EXCEPTION;
2272 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002273
2274#ifdef CASE_TOO_BIG
2275 }
2276#endif
2277
Guido van Rossum374a9221991-04-04 10:40:29 +00002278 } /* switch */
2279
2280 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002281
Guido van Rossum374a9221991-04-04 10:40:29 +00002282 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002283
Guido van Rossum374a9221991-04-04 10:40:29 +00002284 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002285 if (err == 0 && x != NULL) {
2286#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002287 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002288 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002289 fprintf(stderr,
2290 "XXX undetected error\n");
2291 else
2292#endif
2293 continue; /* Normal, fast path */
2294 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002295 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002296 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002297 err = 0;
2298 }
2299
Guido van Rossum374a9221991-04-04 10:40:29 +00002300 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002301
Guido van Rossum374a9221991-04-04 10:40:29 +00002302 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002303 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002304 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002305 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002306 why = WHY_EXCEPTION;
2307 }
2308 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002309#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002310 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002311 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002312 if (PyErr_Occurred()) {
Jeremy Hylton904ed862003-11-05 17:29:35 +00002313 char buf[1024];
2314 sprintf(buf, "Stack unwind with exception "
2315 "set and why=%d", why);
2316 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002317 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002318 }
2319#endif
2320
2321 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002322
Guido van Rossum374a9221991-04-04 10:40:29 +00002323 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002324 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002325
Fred Drake8f51f542001-10-04 14:48:42 +00002326 if (tstate->c_tracefunc != NULL)
2327 call_exc_trace(tstate->c_tracefunc,
2328 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002329 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002330
Guido van Rossum374a9221991-04-04 10:40:29 +00002331 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002332
Guido van Rossum374a9221991-04-04 10:40:29 +00002333 if (why == WHY_RERAISE)
2334 why = WHY_EXCEPTION;
2335
2336 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002337
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002338fast_block_end:
Tim Peters5ca576e2001-06-18 22:08:13 +00002339 while (why != WHY_NOT && why != WHY_YIELD && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002340 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002341
2342 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2343 /* For a continue inside a try block,
2344 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002345 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2346 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002347 why = WHY_NOT;
2348 JUMPTO(PyInt_AS_LONG(retval));
2349 Py_DECREF(retval);
2350 break;
2351 }
2352
Guido van Rossum374a9221991-04-04 10:40:29 +00002353 while (STACK_LEVEL() > b->b_level) {
2354 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002355 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002356 }
2357 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2358 why = WHY_NOT;
2359 JUMPTO(b->b_handler);
2360 break;
2361 }
2362 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002363 (b->b_type == SETUP_EXCEPT &&
2364 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002365 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002366 PyObject *exc, *val, *tb;
2367 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002368 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002369 val = Py_None;
2370 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002371 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002372 /* Make the raw exception data
2373 available to the handler,
2374 so a program can emulate the
2375 Python main loop. Don't do
2376 this for 'finally'. */
2377 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002378 PyErr_NormalizeException(
2379 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002380 set_exc_info(tstate,
2381 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002382 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002383 if (tb == NULL) {
2384 Py_INCREF(Py_None);
2385 PUSH(Py_None);
2386 } else
2387 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002388 PUSH(val);
2389 PUSH(exc);
2390 }
2391 else {
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002392 if (why == WHY_RETURN ||
Guido van Rossumc5fe5eb2002-06-12 03:45:21 +00002393 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00002394 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002395 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002396 PUSH(v);
2397 }
2398 why = WHY_NOT;
2399 JUMPTO(b->b_handler);
2400 break;
2401 }
2402 } /* unwind stack */
2403
2404 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002405
Guido van Rossum374a9221991-04-04 10:40:29 +00002406 if (why != WHY_NOT)
2407 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002408
Guido van Rossum374a9221991-04-04 10:40:29 +00002409 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002410
Guido van Rossum35974fb2001-12-06 21:28:18 +00002411 if (why != WHY_YIELD) {
2412 /* Pop remaining stack entries -- but when yielding */
2413 while (!EMPTY()) {
2414 v = POP();
2415 Py_XDECREF(v);
2416 }
2417 }
2418
Tim Peters5ca576e2001-06-18 22:08:13 +00002419 if (why != WHY_RETURN && why != WHY_YIELD)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002420 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002421
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002422fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002423 if (tstate->use_tracing) {
2424 if (tstate->c_tracefunc
2425 && (why == WHY_RETURN || why == WHY_YIELD)) {
2426 if (call_trace(tstate->c_tracefunc,
2427 tstate->c_traceobj, f,
2428 PyTrace_RETURN, retval)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002429 Py_XDECREF(retval);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002430 retval = NULL;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002431 why = WHY_EXCEPTION;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002432 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002433 }
Fred Drake8f51f542001-10-04 14:48:42 +00002434 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002435 if (why == WHY_EXCEPTION)
2436 call_trace_protected(tstate->c_profilefunc,
2437 tstate->c_profileobj, f,
2438 PyTrace_RETURN);
2439 else if (call_trace(tstate->c_profilefunc,
2440 tstate->c_profileobj, f,
2441 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002442 Py_XDECREF(retval);
2443 retval = NULL;
2444 why = WHY_EXCEPTION;
2445 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002446 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002447 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002448
Guido van Rossuma027efa1997-05-05 20:56:21 +00002449 reset_exc_info(tstate);
2450
Tim Peters5ca576e2001-06-18 22:08:13 +00002451 /* pop frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +00002452 exit_eval_frame:
2453 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002454 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002455
Guido van Rossum96a42c81992-01-12 02:29:51 +00002456 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002457}
2458
Tim Peters6d6c1a32001-08-02 04:15:00 +00002459PyObject *
2460PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002461 PyObject **args, int argcount, PyObject **kws, int kwcount,
2462 PyObject **defs, int defcount, PyObject *closure)
2463{
2464 register PyFrameObject *f;
2465 register PyObject *retval = NULL;
2466 register PyObject **fastlocals, **freevars;
2467 PyThreadState *tstate = PyThreadState_GET();
2468 PyObject *x, *u;
2469
2470 if (globals == NULL) {
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002471 PyErr_SetString(PyExc_SystemError,
2472 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002473 return NULL;
2474 }
2475
Jeremy Hylton985eba52003-02-05 23:13:00 +00002476 assert(globals != NULL);
2477 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002478 if (f == NULL)
2479 return NULL;
2480
2481 fastlocals = f->f_localsplus;
2482 freevars = f->f_localsplus + f->f_nlocals;
2483
2484 if (co->co_argcount > 0 ||
2485 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2486 int i;
2487 int n = argcount;
2488 PyObject *kwdict = NULL;
2489 if (co->co_flags & CO_VARKEYWORDS) {
2490 kwdict = PyDict_New();
2491 if (kwdict == NULL)
2492 goto fail;
2493 i = co->co_argcount;
2494 if (co->co_flags & CO_VARARGS)
2495 i++;
2496 SETLOCAL(i, kwdict);
2497 }
2498 if (argcount > co->co_argcount) {
2499 if (!(co->co_flags & CO_VARARGS)) {
2500 PyErr_Format(PyExc_TypeError,
2501 "%.200s() takes %s %d "
2502 "%sargument%s (%d given)",
2503 PyString_AsString(co->co_name),
2504 defcount ? "at most" : "exactly",
2505 co->co_argcount,
2506 kwcount ? "non-keyword " : "",
2507 co->co_argcount == 1 ? "" : "s",
2508 argcount);
2509 goto fail;
2510 }
2511 n = co->co_argcount;
2512 }
2513 for (i = 0; i < n; i++) {
2514 x = args[i];
2515 Py_INCREF(x);
2516 SETLOCAL(i, x);
2517 }
2518 if (co->co_flags & CO_VARARGS) {
2519 u = PyTuple_New(argcount - n);
2520 if (u == NULL)
2521 goto fail;
2522 SETLOCAL(co->co_argcount, u);
2523 for (i = n; i < argcount; i++) {
2524 x = args[i];
2525 Py_INCREF(x);
2526 PyTuple_SET_ITEM(u, i-n, x);
2527 }
2528 }
2529 for (i = 0; i < kwcount; i++) {
2530 PyObject *keyword = kws[2*i];
2531 PyObject *value = kws[2*i + 1];
2532 int j;
2533 if (keyword == NULL || !PyString_Check(keyword)) {
2534 PyErr_Format(PyExc_TypeError,
2535 "%.200s() keywords must be strings",
2536 PyString_AsString(co->co_name));
2537 goto fail;
2538 }
2539 /* XXX slow -- speed up using dictionary? */
2540 for (j = 0; j < co->co_argcount; j++) {
2541 PyObject *nm = PyTuple_GET_ITEM(
2542 co->co_varnames, j);
2543 int cmp = PyObject_RichCompareBool(
2544 keyword, nm, Py_EQ);
2545 if (cmp > 0)
2546 break;
2547 else if (cmp < 0)
2548 goto fail;
2549 }
2550 /* Check errors from Compare */
2551 if (PyErr_Occurred())
2552 goto fail;
2553 if (j >= co->co_argcount) {
2554 if (kwdict == NULL) {
2555 PyErr_Format(PyExc_TypeError,
2556 "%.200s() got an unexpected "
2557 "keyword argument '%.400s'",
2558 PyString_AsString(co->co_name),
2559 PyString_AsString(keyword));
2560 goto fail;
2561 }
2562 PyDict_SetItem(kwdict, keyword, value);
2563 }
2564 else {
2565 if (GETLOCAL(j) != NULL) {
2566 PyErr_Format(PyExc_TypeError,
2567 "%.200s() got multiple "
2568 "values for keyword "
2569 "argument '%.400s'",
2570 PyString_AsString(co->co_name),
2571 PyString_AsString(keyword));
2572 goto fail;
2573 }
2574 Py_INCREF(value);
2575 SETLOCAL(j, value);
2576 }
2577 }
2578 if (argcount < co->co_argcount) {
2579 int m = co->co_argcount - defcount;
2580 for (i = argcount; i < m; i++) {
2581 if (GETLOCAL(i) == NULL) {
2582 PyErr_Format(PyExc_TypeError,
2583 "%.200s() takes %s %d "
2584 "%sargument%s (%d given)",
2585 PyString_AsString(co->co_name),
2586 ((co->co_flags & CO_VARARGS) ||
2587 defcount) ? "at least"
2588 : "exactly",
2589 m, kwcount ? "non-keyword " : "",
2590 m == 1 ? "" : "s", i);
2591 goto fail;
2592 }
2593 }
2594 if (n > m)
2595 i = n - m;
2596 else
2597 i = 0;
2598 for (; i < defcount; i++) {
2599 if (GETLOCAL(m+i) == NULL) {
2600 PyObject *def = defs[i];
2601 Py_INCREF(def);
2602 SETLOCAL(m+i, def);
2603 }
2604 }
2605 }
2606 }
2607 else {
2608 if (argcount > 0 || kwcount > 0) {
2609 PyErr_Format(PyExc_TypeError,
2610 "%.200s() takes no arguments (%d given)",
2611 PyString_AsString(co->co_name),
2612 argcount + kwcount);
2613 goto fail;
2614 }
2615 }
2616 /* Allocate and initialize storage for cell vars, and copy free
2617 vars into frame. This isn't too efficient right now. */
2618 if (f->f_ncells) {
2619 int i = 0, j = 0, nargs, found;
2620 char *cellname, *argname;
2621 PyObject *c;
2622
2623 nargs = co->co_argcount;
2624 if (co->co_flags & CO_VARARGS)
2625 nargs++;
2626 if (co->co_flags & CO_VARKEYWORDS)
2627 nargs++;
2628
2629 /* Check for cells that shadow args */
2630 for (i = 0; i < f->f_ncells && j < nargs; ++i) {
2631 cellname = PyString_AS_STRING(
2632 PyTuple_GET_ITEM(co->co_cellvars, i));
2633 found = 0;
2634 while (j < nargs) {
2635 argname = PyString_AS_STRING(
2636 PyTuple_GET_ITEM(co->co_varnames, j));
2637 if (strcmp(cellname, argname) == 0) {
2638 c = PyCell_New(GETLOCAL(j));
2639 if (c == NULL)
2640 goto fail;
2641 GETLOCAL(f->f_nlocals + i) = c;
2642 found = 1;
2643 break;
2644 }
2645 j++;
2646 }
2647 if (found == 0) {
2648 c = PyCell_New(NULL);
2649 if (c == NULL)
2650 goto fail;
2651 SETLOCAL(f->f_nlocals + i, c);
2652 }
2653 }
2654 /* Initialize any that are left */
2655 while (i < f->f_ncells) {
2656 c = PyCell_New(NULL);
2657 if (c == NULL)
2658 goto fail;
2659 SETLOCAL(f->f_nlocals + i, c);
2660 i++;
2661 }
2662 }
2663 if (f->f_nfreevars) {
2664 int i;
2665 for (i = 0; i < f->f_nfreevars; ++i) {
2666 PyObject *o = PyTuple_GET_ITEM(closure, i);
2667 Py_INCREF(o);
2668 freevars[f->f_ncells + i] = o;
2669 }
2670 }
2671
Tim Peters5ca576e2001-06-18 22:08:13 +00002672 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002673 /* Don't need to keep the reference to f_back, it will be set
2674 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002675 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002676 f->f_back = NULL;
2677
Jeremy Hylton985eba52003-02-05 23:13:00 +00002678 PCALL(PCALL_GENERATOR);
2679
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002680 /* Create a new generator that owns the ready to run frame
2681 * and return that as the value. */
Tim Peters5ca576e2001-06-18 22:08:13 +00002682 return gen_new(f);
2683 }
2684
2685 retval = eval_frame(f);
2686
2687 fail: /* Jump here from prelude on failure */
2688
Tim Petersb13680b2001-11-27 23:29:29 +00002689 /* decref'ing the frame can cause __del__ methods to get invoked,
2690 which can call back into Python. While we're done with the
2691 current Python frame (f), the associated C stack is still in use,
2692 so recursion_depth must be boosted for the duration.
2693 */
2694 assert(tstate != NULL);
2695 ++tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002696 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00002697 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002698 return retval;
2699}
2700
2701
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002702/* Implementation notes for set_exc_info() and reset_exc_info():
2703
2704- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2705 'exc_traceback'. These always travel together.
2706
2707- tstate->curexc_ZZZ is the "hot" exception that is set by
2708 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2709
2710- Once an exception is caught by an except clause, it is transferred
2711 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2712 can pick it up. This is the primary task of set_exc_info().
2713
2714- Now let me explain the complicated dance with frame->f_exc_ZZZ.
2715
2716 Long ago, when none of this existed, there were just a few globals:
2717 one set corresponding to the "hot" exception, and one set
2718 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2719 globals; they were simply stored as sys.exc_ZZZ. For backwards
2720 compatibility, they still are!) The problem was that in code like
2721 this:
2722
2723 try:
2724 "something that may fail"
2725 except "some exception":
2726 "do something else first"
2727 "print the exception from sys.exc_ZZZ."
2728
2729 if "do something else first" invoked something that raised and caught
2730 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2731 cause of subtle bugs. I fixed this by changing the semantics as
2732 follows:
2733
2734 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2735 *in that frame*.
2736
2737 - But initially, and as long as no exception is caught in a given
2738 frame, sys.exc_ZZZ will hold the last exception caught in the
2739 previous frame (or the frame before that, etc.).
2740
2741 The first bullet fixed the bug in the above example. The second
2742 bullet was for backwards compatibility: it was (and is) common to
2743 have a function that is called when an exception is caught, and to
2744 have that function access the caught exception via sys.exc_ZZZ.
2745 (Example: traceback.print_exc()).
2746
2747 At the same time I fixed the problem that sys.exc_ZZZ weren't
2748 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2749 but that's really a separate improvement.
2750
2751 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2752 variables to what they were before the current frame was called. The
2753 set_exc_info() function saves them on the frame so that
2754 reset_exc_info() can restore them. The invariant is that
2755 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2756 exception (where "catching" an exception applies only to successful
2757 except clauses); and if the current frame ever caught an exception,
2758 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2759 at the start of the current frame.
2760
2761*/
2762
Guido van Rossuma027efa1997-05-05 20:56:21 +00002763static void
Guido van Rossumac7be682001-01-17 15:42:30 +00002764set_exc_info(PyThreadState *tstate,
2765 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002766{
2767 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002768 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00002769
Guido van Rossuma027efa1997-05-05 20:56:21 +00002770 frame = tstate->frame;
2771 if (frame->f_exc_type == NULL) {
2772 /* This frame didn't catch an exception before */
2773 /* Save previous exception of this thread in this frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002774 if (tstate->exc_type == NULL) {
2775 Py_INCREF(Py_None);
2776 tstate->exc_type = Py_None;
2777 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002778 tmp_type = frame->f_exc_type;
2779 tmp_value = frame->f_exc_value;
2780 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002781 Py_XINCREF(tstate->exc_type);
2782 Py_XINCREF(tstate->exc_value);
2783 Py_XINCREF(tstate->exc_traceback);
2784 frame->f_exc_type = tstate->exc_type;
2785 frame->f_exc_value = tstate->exc_value;
2786 frame->f_exc_traceback = tstate->exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002787 Py_XDECREF(tmp_type);
2788 Py_XDECREF(tmp_value);
2789 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002790 }
2791 /* Set new exception for this thread */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002792 tmp_type = tstate->exc_type;
2793 tmp_value = tstate->exc_value;
2794 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002795 Py_XINCREF(type);
2796 Py_XINCREF(value);
2797 Py_XINCREF(tb);
2798 tstate->exc_type = type;
2799 tstate->exc_value = value;
2800 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002801 Py_XDECREF(tmp_type);
2802 Py_XDECREF(tmp_value);
2803 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002804 /* For b/w compatibility */
2805 PySys_SetObject("exc_type", type);
2806 PySys_SetObject("exc_value", value);
2807 PySys_SetObject("exc_traceback", tb);
2808}
2809
2810static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002811reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002812{
2813 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002814 PyObject *tmp_type, *tmp_value, *tmp_tb;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002815 frame = tstate->frame;
2816 if (frame->f_exc_type != NULL) {
2817 /* This frame caught an exception */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002818 tmp_type = tstate->exc_type;
2819 tmp_value = tstate->exc_value;
2820 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002821 Py_XINCREF(frame->f_exc_type);
2822 Py_XINCREF(frame->f_exc_value);
2823 Py_XINCREF(frame->f_exc_traceback);
2824 tstate->exc_type = frame->f_exc_type;
2825 tstate->exc_value = frame->f_exc_value;
2826 tstate->exc_traceback = frame->f_exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002827 Py_XDECREF(tmp_type);
2828 Py_XDECREF(tmp_value);
2829 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002830 /* For b/w compatibility */
2831 PySys_SetObject("exc_type", frame->f_exc_type);
2832 PySys_SetObject("exc_value", frame->f_exc_value);
2833 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
2834 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002835 tmp_type = frame->f_exc_type;
2836 tmp_value = frame->f_exc_value;
2837 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002838 frame->f_exc_type = NULL;
2839 frame->f_exc_value = NULL;
2840 frame->f_exc_traceback = NULL;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002841 Py_XDECREF(tmp_type);
2842 Py_XDECREF(tmp_value);
2843 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002844}
2845
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002846/* Logic for the raise statement (too complicated for inlining).
2847 This *consumes* a reference count to each of its arguments. */
Guido van Rossum1aa14831997-01-21 05:34:20 +00002848static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002849do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002850{
Guido van Rossumd295f121998-04-09 21:39:57 +00002851 if (type == NULL) {
2852 /* Reraise */
2853 PyThreadState *tstate = PyThreadState_Get();
2854 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
2855 value = tstate->exc_value;
2856 tb = tstate->exc_traceback;
2857 Py_XINCREF(type);
2858 Py_XINCREF(value);
2859 Py_XINCREF(tb);
2860 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002861
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002862 /* We support the following forms of raise:
2863 raise <class>, <classinstance>
2864 raise <class>, <argument tuple>
2865 raise <class>, None
2866 raise <class>, <argument>
2867 raise <classinstance>, None
2868 raise <string>, <object>
2869 raise <string>, None
2870
2871 An omitted second argument is the same as None.
2872
2873 In addition, raise <tuple>, <anything> is the same as
2874 raising the tuple's first item (and it better have one!);
2875 this rule is applied recursively.
2876
2877 Finally, an optional third argument can be supplied, which
2878 gives the traceback to be substituted (useful when
2879 re-raising an exception after examining it). */
2880
2881 /* First, check the traceback argument, replacing None with
2882 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002883 if (tb == Py_None) {
2884 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002885 tb = NULL;
2886 }
2887 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002888 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002889 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002890 goto raise_error;
2891 }
2892
2893 /* Next, replace a missing value with None */
2894 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002895 value = Py_None;
2896 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002897 }
2898
2899 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00002900 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
2901 PyObject *tmp = type;
2902 type = PyTuple_GET_ITEM(type, 0);
2903 Py_INCREF(type);
2904 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002905 }
2906
Tim Petersafb2c802002-04-18 18:06:20 +00002907 if (PyString_CheckExact(type))
2908 /* Raising builtin string is deprecated but still allowed --
2909 * do nothing. Raising an instance of a new-style str
2910 * subclass is right out. */
Neal Norwitz37aa0662003-01-10 15:31:15 +00002911 PyErr_Warn(PyExc_PendingDeprecationWarning,
2912 "raising a string exception is deprecated");
Barry Warsaw4249f541997-08-22 21:26:19 +00002913
2914 else if (PyClass_Check(type))
2915 PyErr_NormalizeException(&type, &value, &tb);
2916
Guido van Rossumb209a111997-04-29 18:18:01 +00002917 else if (PyInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002918 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002919 if (value != Py_None) {
2920 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002921 "instance exception may not have a separate value");
2922 goto raise_error;
2923 }
2924 else {
2925 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00002926 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002927 value = type;
Guido van Rossumb209a111997-04-29 18:18:01 +00002928 type = (PyObject*) ((PyInstanceObject*)type)->in_class;
2929 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002930 }
2931 }
2932 else {
2933 /* Not something you can raise. You get an exception
2934 anyway, just not what you specified :-) */
Jeremy Hylton960d9482001-04-27 02:25:33 +00002935 PyErr_Format(PyExc_TypeError,
Neal Norwitz37aa0662003-01-10 15:31:15 +00002936 "exceptions must be classes, instances, or "
2937 "strings (deprecated), not %s",
2938 type->ob_type->tp_name);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002939 goto raise_error;
2940 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002941 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002942 if (tb == NULL)
2943 return WHY_EXCEPTION;
2944 else
2945 return WHY_RERAISE;
2946 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00002947 Py_XDECREF(value);
2948 Py_XDECREF(type);
2949 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002950 return WHY_EXCEPTION;
2951}
2952
Tim Petersd6d010b2001-06-21 02:49:55 +00002953/* Iterate v argcnt times and store the results on the stack (via decreasing
2954 sp). Return 1 for success, 0 if error. */
2955
Barry Warsawe42b18f1997-08-25 22:13:04 +00002956static int
Tim Petersd6d010b2001-06-21 02:49:55 +00002957unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00002958{
Tim Petersd6d010b2001-06-21 02:49:55 +00002959 int i = 0;
2960 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00002961 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00002962
Tim Petersd6d010b2001-06-21 02:49:55 +00002963 assert(v != NULL);
2964
2965 it = PyObject_GetIter(v);
2966 if (it == NULL)
2967 goto Error;
2968
2969 for (; i < argcnt; i++) {
2970 w = PyIter_Next(it);
2971 if (w == NULL) {
2972 /* Iterator done, via error or exhaustion. */
2973 if (!PyErr_Occurred()) {
2974 PyErr_Format(PyExc_ValueError,
2975 "need more than %d value%s to unpack",
2976 i, i == 1 ? "" : "s");
2977 }
2978 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00002979 }
2980 *--sp = w;
2981 }
Tim Petersd6d010b2001-06-21 02:49:55 +00002982
2983 /* We better have exhausted the iterator now. */
2984 w = PyIter_Next(it);
2985 if (w == NULL) {
2986 if (PyErr_Occurred())
2987 goto Error;
2988 Py_DECREF(it);
2989 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00002990 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00002991 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00002992 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00002993 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00002994Error:
Barry Warsaw91010551997-08-25 22:30:51 +00002995 for (; i > 0; i--, sp++)
2996 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00002997 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00002998 return 0;
2999}
3000
3001
Guido van Rossum96a42c81992-01-12 02:29:51 +00003002#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003003static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003004prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003005{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003006 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003007 if (PyObject_Print(v, stdout, 0) != 0)
3008 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003009 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003010 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003011}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003012#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003013
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003014static void
Fred Drake5755ce62001-06-27 19:19:46 +00003015call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003016{
Guido van Rossumb209a111997-04-29 18:18:01 +00003017 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003018 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003019 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003020 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003021 value = Py_None;
3022 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003023 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003024 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003025 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003026 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003027 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003028 }
Fred Drake5755ce62001-06-27 19:19:46 +00003029 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003030 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003031 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003032 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003033 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003034 Py_XDECREF(type);
3035 Py_XDECREF(value);
3036 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003037 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003038}
3039
Fred Drake4ec5d562001-10-04 19:26:43 +00003040static void
3041call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3042 int what)
3043{
3044 PyObject *type, *value, *traceback;
3045 int err;
3046 PyErr_Fetch(&type, &value, &traceback);
3047 err = call_trace(func, obj, frame, what, NULL);
3048 if (err == 0)
3049 PyErr_Restore(type, value, traceback);
3050 else {
3051 Py_XDECREF(type);
3052 Py_XDECREF(value);
3053 Py_XDECREF(traceback);
3054 }
3055}
3056
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003057static int
Fred Drake5755ce62001-06-27 19:19:46 +00003058call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3059 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003060{
Fred Drake5755ce62001-06-27 19:19:46 +00003061 register PyThreadState *tstate = frame->f_tstate;
3062 int result;
3063 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003064 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003065 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003066 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003067 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003068 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3069 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003070 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003071 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003072}
3073
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003074PyObject *
3075_PyEval_CallTracing(PyObject *func, PyObject *args)
3076{
3077 PyFrameObject *frame = PyEval_GetFrame();
3078 PyThreadState *tstate = frame->f_tstate;
3079 int save_tracing = tstate->tracing;
3080 int save_use_tracing = tstate->use_tracing;
3081 PyObject *result;
3082
3083 tstate->tracing = 0;
3084 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3085 || (tstate->c_profilefunc != NULL));
3086 result = PyObject_Call(func, args, NULL);
3087 tstate->tracing = save_tracing;
3088 tstate->use_tracing = save_use_tracing;
3089 return result;
3090}
3091
Michael W. Hudson006c7522002-11-08 13:08:46 +00003092static int
Michael W. Hudson019a78e2002-11-08 12:53:11 +00003093maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003094 PyFrameObject *frame, int *instr_lb, int *instr_ub)
3095{
3096 /* The theory of SET_LINENO-less tracing.
3097
3098 In a nutshell, we use the co_lnotab field of the code object
3099 to tell when execution has moved onto a different line.
3100
3101 As mentioned above, the basic idea is so set things up so
3102 that
3103
3104 *instr_lb <= frame->f_lasti < *instr_ub
3105
3106 is true so long as execution does not change lines.
3107
3108 This is all fairly simple. Digging the information out of
3109 co_lnotab takes some work, but is conceptually clear.
3110
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003111 Somewhat harder to explain is why we don't *always* call the
3112 line trace function when the above test fails.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003113
3114 Consider this code:
3115
3116 1: def f(a):
3117 2: if a:
3118 3: print 1
3119 4: else:
3120 5: print 2
3121
3122 which compiles to this:
3123
3124 2 0 LOAD_FAST 0 (a)
3125 3 JUMP_IF_FALSE 9 (to 15)
3126 6 POP_TOP
3127
3128 3 7 LOAD_CONST 1 (1)
3129 10 PRINT_ITEM
3130 11 PRINT_NEWLINE
3131 12 JUMP_FORWARD 6 (to 21)
3132 >> 15 POP_TOP
3133
3134 5 16 LOAD_CONST 2 (2)
3135 19 PRINT_ITEM
3136 20 PRINT_NEWLINE
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003137 >> 21 LOAD_CONST 0 (None)
3138 24 RETURN_VALUE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003139
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003140 If 'a' is false, execution will jump to instruction at offset
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003141 15 and the co_lnotab will claim that execution has moved to
3142 line 3. This is at best misleading. In this case we could
3143 associate the POP_TOP with line 4, but that doesn't make
3144 sense in all cases (I think).
3145
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003146 What we do is only call the line trace function if the co_lnotab
3147 indicates we have jumped to the *start* of a line, i.e. if the
3148 current instruction offset matches the offset given for the
3149 start of a line by the co_lnotab.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003150
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003151 This also takes care of the situation where 'a' is true.
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003152 Execution will jump from instruction offset 12 to offset 21.
3153 Then the co_lnotab would imply that execution has moved to line
3154 5, which is again misleading.
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003155
3156 Why do we set f_lineno when tracing? Well, consider the code
3157 above when 'a' is true. If stepping through this with 'n' in
3158 pdb, you would stop at line 1 with a "call" type event, then
3159 line events on lines 2 and 3, then a "return" type event -- but
3160 you would be shown line 5 during this event. This is a change
3161 from the behaviour in 2.2 and before, and I've found it
3162 confusing in practice. By setting and using f_lineno when
3163 tracing, one can report a line number different from that
3164 suggested by f_lasti on this one occasion where it's desirable.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003165 */
3166
Michael W. Hudson006c7522002-11-08 13:08:46 +00003167 int result = 0;
3168
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003169 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003170 PyCodeObject* co = frame->f_code;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003171 int size, addr, line;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003172 unsigned char* p;
3173
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003174 size = PyString_GET_SIZE(co->co_lnotab) / 2;
3175 p = (unsigned char*)PyString_AS_STRING(co->co_lnotab);
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003176
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003177 addr = 0;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003178 line = co->co_firstlineno;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003179
3180 /* possible optimization: if f->f_lasti == instr_ub
3181 (likely to be a common case) then we already know
3182 instr_lb -- if we stored the matching value of p
3183 somwhere we could skip the first while loop. */
3184
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003185 /* see comments in compile.c for the description of
3186 co_lnotab. A point to remember: increments to p
3187 should come in pairs -- although we don't care about
3188 the line increments here, treating them as byte
3189 increments gets confusing, to say the least. */
3190
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003191 while (size > 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003192 if (addr + *p > frame->f_lasti)
3193 break;
3194 addr += *p++;
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003195 if (*p) *instr_lb = addr;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003196 line += *p++;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003197 --size;
3198 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003199
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003200 if (addr == frame->f_lasti) {
3201 frame->f_lineno = line;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003202 result = call_trace(func, obj, frame,
3203 PyTrace_LINE, Py_None);
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003204 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003205
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003206 if (size > 0) {
3207 while (--size >= 0) {
3208 addr += *p++;
3209 if (*p++)
3210 break;
3211 }
3212 *instr_ub = addr;
3213 }
3214 else {
3215 *instr_ub = INT_MAX;
3216 }
3217 }
Michael W. Hudson006c7522002-11-08 13:08:46 +00003218
3219 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003220}
3221
Fred Drake5755ce62001-06-27 19:19:46 +00003222void
3223PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003224{
Fred Drake5755ce62001-06-27 19:19:46 +00003225 PyThreadState *tstate = PyThreadState_Get();
3226 PyObject *temp = tstate->c_profileobj;
3227 Py_XINCREF(arg);
3228 tstate->c_profilefunc = NULL;
3229 tstate->c_profileobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003230 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003231 Py_XDECREF(temp);
3232 tstate->c_profilefunc = func;
3233 tstate->c_profileobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003234 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003235}
3236
3237void
3238PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3239{
3240 PyThreadState *tstate = PyThreadState_Get();
3241 PyObject *temp = tstate->c_traceobj;
3242 Py_XINCREF(arg);
3243 tstate->c_tracefunc = NULL;
3244 tstate->c_traceobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003245 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003246 Py_XDECREF(temp);
3247 tstate->c_tracefunc = func;
3248 tstate->c_traceobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003249 tstate->use_tracing = ((func != NULL)
3250 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003251}
3252
Guido van Rossumb209a111997-04-29 18:18:01 +00003253PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003254PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003255{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003256 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003257 if (current_frame == NULL)
Michael W. Hudson019a78e2002-11-08 12:53:11 +00003258 return PyThreadState_Get()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003259 else
3260 return current_frame->f_builtins;
3261}
3262
Guido van Rossumb209a111997-04-29 18:18:01 +00003263PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003264PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003265{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003266 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003267 if (current_frame == NULL)
3268 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003269 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003270 return current_frame->f_locals;
3271}
3272
Guido van Rossumb209a111997-04-29 18:18:01 +00003273PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003274PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003275{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003276 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003277 if (current_frame == NULL)
3278 return NULL;
3279 else
3280 return current_frame->f_globals;
3281}
3282
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003283PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003284PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003285{
Michael W. Hudson019a78e2002-11-08 12:53:11 +00003286 PyThreadState *tstate = PyThreadState_Get();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003287 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003288}
3289
Guido van Rossum6135a871995-01-09 17:53:26 +00003290int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003291PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003292{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003293 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003294 return current_frame == NULL ? 0 : current_frame->f_restricted;
3295}
3296
Guido van Rossumbe270261997-05-22 22:26:18 +00003297int
Tim Peters5ba58662001-07-16 02:29:45 +00003298PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003299{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003300 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003301 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003302
3303 if (current_frame != NULL) {
3304 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003305 const int compilerflags = codeflags & PyCF_MASK;
3306 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003307 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003308 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003309 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003310#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003311 if (codeflags & CO_GENERATOR_ALLOWED) {
3312 result = 1;
3313 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3314 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003315#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003316 }
3317 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003318}
3319
3320int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003321Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003322{
Guido van Rossumb209a111997-04-29 18:18:01 +00003323 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003324 if (f == NULL)
3325 return 0;
3326 if (!PyFile_SoftSpace(f, 0))
3327 return 0;
3328 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003329}
3330
Guido van Rossum3f5da241990-12-20 15:06:42 +00003331
Guido van Rossum681d79a1995-07-18 14:51:37 +00003332/* External interface to call any callable object.
3333 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003334
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003335#undef PyEval_CallObject
3336/* for backward compatibility: export this interface */
3337
Guido van Rossumb209a111997-04-29 18:18:01 +00003338PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003339PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003340{
Guido van Rossumb209a111997-04-29 18:18:01 +00003341 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003342}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003343#define PyEval_CallObject(func,arg) \
3344 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003345
Guido van Rossumb209a111997-04-29 18:18:01 +00003346PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003347PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003348{
Jeremy Hylton52820442001-01-03 23:52:36 +00003349 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003350
3351 if (arg == NULL)
Guido van Rossumb209a111997-04-29 18:18:01 +00003352 arg = PyTuple_New(0);
3353 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003354 PyErr_SetString(PyExc_TypeError,
3355 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003356 return NULL;
3357 }
3358 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003359 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003360
Guido van Rossumb209a111997-04-29 18:18:01 +00003361 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003362 PyErr_SetString(PyExc_TypeError,
3363 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003364 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003365 return NULL;
3366 }
3367
Tim Peters6d6c1a32001-08-02 04:15:00 +00003368 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003369 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003370 return result;
3371}
3372
Tim Peters6d6c1a32001-08-02 04:15:00 +00003373char *
3374PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003375{
3376 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003377 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003378 else if (PyFunction_Check(func))
3379 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3380 else if (PyCFunction_Check(func))
3381 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3382 else if (PyClass_Check(func))
3383 return PyString_AsString(((PyClassObject*)func)->cl_name);
3384 else if (PyInstance_Check(func)) {
3385 return PyString_AsString(
3386 ((PyInstanceObject*)func)->in_class->cl_name);
3387 } else {
3388 return func->ob_type->tp_name;
3389 }
3390}
3391
Tim Peters6d6c1a32001-08-02 04:15:00 +00003392char *
3393PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003394{
3395 if (PyMethod_Check(func))
3396 return "()";
3397 else if (PyFunction_Check(func))
3398 return "()";
3399 else if (PyCFunction_Check(func))
3400 return "()";
3401 else if (PyClass_Check(func))
3402 return " constructor";
3403 else if (PyInstance_Check(func)) {
3404 return " instance";
3405 } else {
3406 return " object";
3407 }
3408}
3409
Jeremy Hylton52820442001-01-03 23:52:36 +00003410#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
3411
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003412static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003413err_args(PyObject *func, int flags, int nargs)
3414{
3415 if (flags & METH_NOARGS)
3416 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003417 "%.200s() takes no arguments (%d given)",
Jeremy Hylton192690e2002-08-16 18:36:11 +00003418 ((PyCFunctionObject *)func)->m_ml->ml_name,
3419 nargs);
3420 else
3421 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003422 "%.200s() takes exactly one argument (%d given)",
Jeremy Hylton192690e2002-08-16 18:36:11 +00003423 ((PyCFunctionObject *)func)->m_ml->ml_name,
3424 nargs);
3425}
3426
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003427static PyObject *
3428call_function(PyObject ***pp_stack, int oparg)
3429{
3430 int na = oparg & 0xff;
3431 int nk = (oparg>>8) & 0xff;
3432 int n = na + 2 * nk;
3433 PyObject **pfunc = (*pp_stack) - n - 1;
3434 PyObject *func = *pfunc;
3435 PyObject *x, *w;
3436
Jeremy Hylton985eba52003-02-05 23:13:00 +00003437 /* Always dispatch PyCFunction first, because these are
3438 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003439 */
3440 if (PyCFunction_Check(func) && nk == 0) {
3441 int flags = PyCFunction_GET_FLAGS(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003442 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003443 if (flags & (METH_NOARGS | METH_O)) {
3444 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3445 PyObject *self = PyCFunction_GET_SELF(func);
3446 if (flags & METH_NOARGS && na == 0)
3447 x = (*meth)(self, NULL);
3448 else if (flags & METH_O && na == 1) {
3449 PyObject *arg = EXT_POP(*pp_stack);
3450 x = (*meth)(self, arg);
3451 Py_DECREF(arg);
3452 }
3453 else {
3454 err_args(func, flags, na);
3455 x = NULL;
3456 }
3457 }
3458 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003459 PyObject *callargs;
3460 callargs = load_args(pp_stack, na);
3461 x = PyCFunction_Call(func, callargs, NULL);
3462 Py_XDECREF(callargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003463 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003464 } else {
3465 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3466 /* optimize access to bound methods */
3467 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003468 PCALL(PCALL_METHOD);
3469 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003470 Py_INCREF(self);
3471 func = PyMethod_GET_FUNCTION(func);
3472 Py_INCREF(func);
3473 Py_DECREF(*pfunc);
3474 *pfunc = self;
3475 na++;
3476 n++;
3477 } else
3478 Py_INCREF(func);
3479 if (PyFunction_Check(func))
3480 x = fast_function(func, pp_stack, n, na, nk);
3481 else
3482 x = do_call(func, pp_stack, na, nk);
3483 Py_DECREF(func);
3484 }
3485
Jeremy Hylton985eba52003-02-05 23:13:00 +00003486 /* What does this do? */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003487 while ((*pp_stack) > pfunc) {
3488 w = EXT_POP(*pp_stack);
3489 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003490 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003491 }
3492 return x;
3493}
3494
Jeremy Hylton192690e2002-08-16 18:36:11 +00003495/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003496 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003497 For the simplest case -- a function that takes only positional
3498 arguments and is called with only positional arguments -- it
3499 inlines the most primitive frame setup code from
3500 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3501 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003502*/
3503
3504static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003505fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003506{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003507 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003508 PyObject *globals = PyFunction_GET_GLOBALS(func);
3509 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3510 PyObject **d = NULL;
3511 int nd = 0;
3512
Jeremy Hylton985eba52003-02-05 23:13:00 +00003513 PCALL(PCALL_FUNCTION);
3514 PCALL(PCALL_FAST_FUNCTION);
Raymond Hettinger40174c32003-05-31 07:04:16 +00003515 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003516 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3517 PyFrameObject *f;
3518 PyObject *retval = NULL;
3519 PyThreadState *tstate = PyThreadState_GET();
3520 PyObject **fastlocals, **stack;
3521 int i;
3522
3523 PCALL(PCALL_FASTER_FUNCTION);
3524 assert(globals != NULL);
3525 /* XXX Perhaps we should create a specialized
3526 PyFrame_New() that doesn't take locals, but does
3527 take builtins without sanity checking them.
3528 */
3529 f = PyFrame_New(tstate, co, globals, NULL);
3530 if (f == NULL)
3531 return NULL;
3532
3533 fastlocals = f->f_localsplus;
3534 stack = (*pp_stack) - n;
3535
3536 for (i = 0; i < n; i++) {
3537 Py_INCREF(*stack);
3538 fastlocals[i] = *stack++;
3539 }
3540 retval = eval_frame(f);
3541 assert(tstate != NULL);
3542 ++tstate->recursion_depth;
3543 Py_DECREF(f);
3544 --tstate->recursion_depth;
3545 return retval;
3546 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003547 if (argdefs != NULL) {
3548 d = &PyTuple_GET_ITEM(argdefs, 0);
3549 nd = ((PyTupleObject *)argdefs)->ob_size;
3550 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003551 return PyEval_EvalCodeEx(co, globals,
3552 (PyObject *)NULL, (*pp_stack)-n, na,
3553 (*pp_stack)-2*nk, nk, d, nd,
3554 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003555}
3556
3557static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003558update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3559 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003560{
3561 PyObject *kwdict = NULL;
3562 if (orig_kwdict == NULL)
3563 kwdict = PyDict_New();
3564 else {
3565 kwdict = PyDict_Copy(orig_kwdict);
3566 Py_DECREF(orig_kwdict);
3567 }
3568 if (kwdict == NULL)
3569 return NULL;
3570 while (--nk >= 0) {
3571 int err;
3572 PyObject *value = EXT_POP(*pp_stack);
3573 PyObject *key = EXT_POP(*pp_stack);
3574 if (PyDict_GetItem(kwdict, key) != NULL) {
Guido van Rossumac7be682001-01-17 15:42:30 +00003575 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +00003576 "%.200s%s got multiple values "
Jeremy Hylton512a2372001-04-11 13:52:29 +00003577 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003578 PyEval_GetFuncName(func),
3579 PyEval_GetFuncDesc(func),
Jeremy Hylton512a2372001-04-11 13:52:29 +00003580 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003581 Py_DECREF(key);
3582 Py_DECREF(value);
3583 Py_DECREF(kwdict);
3584 return NULL;
3585 }
3586 err = PyDict_SetItem(kwdict, key, value);
3587 Py_DECREF(key);
3588 Py_DECREF(value);
3589 if (err) {
3590 Py_DECREF(kwdict);
3591 return NULL;
3592 }
3593 }
3594 return kwdict;
3595}
3596
3597static PyObject *
3598update_star_args(int nstack, int nstar, PyObject *stararg,
3599 PyObject ***pp_stack)
3600{
3601 PyObject *callargs, *w;
3602
3603 callargs = PyTuple_New(nstack + nstar);
3604 if (callargs == NULL) {
3605 return NULL;
3606 }
3607 if (nstar) {
3608 int i;
3609 for (i = 0; i < nstar; i++) {
3610 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3611 Py_INCREF(a);
3612 PyTuple_SET_ITEM(callargs, nstack + i, a);
3613 }
3614 }
3615 while (--nstack >= 0) {
3616 w = EXT_POP(*pp_stack);
3617 PyTuple_SET_ITEM(callargs, nstack, w);
3618 }
3619 return callargs;
3620}
3621
3622static PyObject *
3623load_args(PyObject ***pp_stack, int na)
3624{
3625 PyObject *args = PyTuple_New(na);
3626 PyObject *w;
3627
3628 if (args == NULL)
3629 return NULL;
3630 while (--na >= 0) {
3631 w = EXT_POP(*pp_stack);
3632 PyTuple_SET_ITEM(args, na, w);
3633 }
3634 return args;
3635}
3636
3637static PyObject *
3638do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3639{
3640 PyObject *callargs = NULL;
3641 PyObject *kwdict = NULL;
3642 PyObject *result = NULL;
3643
3644 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003645 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003646 if (kwdict == NULL)
3647 goto call_fail;
3648 }
3649 callargs = load_args(pp_stack, na);
3650 if (callargs == NULL)
3651 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003652#ifdef CALL_PROFILE
3653 /* At this point, we have to look at the type of func to
3654 update the call stats properly. Do it here so as to avoid
3655 exposing the call stats machinery outside ceval.c
3656 */
3657 if (PyFunction_Check(func))
3658 PCALL(PCALL_FUNCTION);
3659 else if (PyMethod_Check(func))
3660 PCALL(PCALL_METHOD);
3661 else if (PyType_Check(func))
3662 PCALL(PCALL_TYPE);
3663 else
3664 PCALL(PCALL_OTHER);
3665#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003666 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003667 call_fail:
3668 Py_XDECREF(callargs);
3669 Py_XDECREF(kwdict);
3670 return result;
3671}
3672
3673static PyObject *
3674ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3675{
3676 int nstar = 0;
3677 PyObject *callargs = NULL;
3678 PyObject *stararg = NULL;
3679 PyObject *kwdict = NULL;
3680 PyObject *result = NULL;
3681
3682 if (flags & CALL_FLAG_KW) {
3683 kwdict = EXT_POP(*pp_stack);
3684 if (!(kwdict && PyDict_Check(kwdict))) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003685 PyErr_Format(PyExc_TypeError,
Jeremy Hylton512a2372001-04-11 13:52:29 +00003686 "%s%s argument after ** "
3687 "must be a dictionary",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003688 PyEval_GetFuncName(func),
3689 PyEval_GetFuncDesc(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003690 goto ext_call_fail;
3691 }
3692 }
3693 if (flags & CALL_FLAG_VAR) {
3694 stararg = EXT_POP(*pp_stack);
3695 if (!PyTuple_Check(stararg)) {
3696 PyObject *t = NULL;
3697 t = PySequence_Tuple(stararg);
3698 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003699 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3700 PyErr_Format(PyExc_TypeError,
3701 "%s%s argument after * "
3702 "must be a sequence",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003703 PyEval_GetFuncName(func),
3704 PyEval_GetFuncDesc(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003705 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003706 goto ext_call_fail;
3707 }
3708 Py_DECREF(stararg);
3709 stararg = t;
3710 }
3711 nstar = PyTuple_GET_SIZE(stararg);
3712 }
3713 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003714 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003715 if (kwdict == NULL)
3716 goto ext_call_fail;
3717 }
3718 callargs = update_star_args(na, nstar, stararg, pp_stack);
3719 if (callargs == NULL)
3720 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003721#ifdef CALL_PROFILE
3722 /* At this point, we have to look at the type of func to
3723 update the call stats properly. Do it here so as to avoid
3724 exposing the call stats machinery outside ceval.c
3725 */
3726 if (PyFunction_Check(func))
3727 PCALL(PCALL_FUNCTION);
3728 else if (PyMethod_Check(func))
3729 PCALL(PCALL_METHOD);
3730 else if (PyType_Check(func))
3731 PCALL(PCALL_TYPE);
3732 else
3733 PCALL(PCALL_OTHER);
3734#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003735 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003736 ext_call_fail:
3737 Py_XDECREF(callargs);
3738 Py_XDECREF(kwdict);
3739 Py_XDECREF(stararg);
3740 return result;
3741}
3742
Guido van Rossum3b9c6671996-07-30 18:40:29 +00003743#define SLICE_ERROR_MSG \
3744 "standard sequence type does not support step size other than one"
3745
Tim Peterscb479e72001-12-16 19:11:44 +00003746/* Extract a slice index from a PyInt or PyLong, and store in *pi.
3747 Silently reduce values larger than INT_MAX to INT_MAX, and silently
3748 boost values less than -INT_MAX to 0. Return 0 on error, 1 on success.
3749*/
Tim Petersb5196382001-12-16 19:44:20 +00003750/* Note: If v is NULL, return success without storing into *pi. This
3751 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3752 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00003753*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00003754int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003755_PyEval_SliceIndex(PyObject *v, int *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003756{
Tim Petersb5196382001-12-16 19:44:20 +00003757 if (v != NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003758 long x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003759 if (PyInt_Check(v)) {
3760 x = PyInt_AsLong(v);
3761 } else if (PyLong_Check(v)) {
3762 x = PyLong_AsLong(v);
3763 if (x==-1 && PyErr_Occurred()) {
3764 PyObject *long_zero;
Guido van Rossumac7be682001-01-17 15:42:30 +00003765 int cmp;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003766
Guido van Rossumac7be682001-01-17 15:42:30 +00003767 if (!PyErr_ExceptionMatches(
3768 PyExc_OverflowError)) {
3769 /* It's not an overflow error, so just
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003770 signal an error */
Guido van Rossum20c6add2000-05-08 14:06:50 +00003771 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003772 }
3773
Guido van Rossumac7be682001-01-17 15:42:30 +00003774 /* Clear the OverflowError */
3775 PyErr_Clear();
3776
3777 /* It's an overflow error, so we need to
3778 check the sign of the long integer,
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003779 set the value to INT_MAX or -INT_MAX,
3780 and clear the error. */
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003781
3782 /* Create a long integer with a value of 0 */
Guido van Rossumac7be682001-01-17 15:42:30 +00003783 long_zero = PyLong_FromLong(0L);
Tim Peterscb479e72001-12-16 19:11:44 +00003784 if (long_zero == NULL)
3785 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003786
3787 /* Check sign */
Guido van Rossumac7be682001-01-17 15:42:30 +00003788 cmp = PyObject_RichCompareBool(v, long_zero,
3789 Py_GT);
3790 Py_DECREF(long_zero);
3791 if (cmp < 0)
3792 return 0;
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003793 else if (cmp)
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003794 x = INT_MAX;
3795 else
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003796 x = -INT_MAX;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003797 }
3798 } else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003799 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003800 "slice indices must be integers");
Guido van Rossum20c6add2000-05-08 14:06:50 +00003801 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003802 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003803 /* Truncate -- very long indices are truncated anyway */
3804 if (x > INT_MAX)
3805 x = INT_MAX;
3806 else if (x < -INT_MAX)
Michael W. Hudsoncbd6fb92002-11-06 15:17:32 +00003807 x = -INT_MAX;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003808 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003809 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00003810 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003811}
3812
Guido van Rossum50d756e2001-08-18 17:43:36 +00003813#undef ISINT
3814#define ISINT(x) ((x) == NULL || PyInt_Check(x) || PyLong_Check(x))
3815
Guido van Rossumb209a111997-04-29 18:18:01 +00003816static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003817apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003818{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003819 PyTypeObject *tp = u->ob_type;
3820 PySequenceMethods *sq = tp->tp_as_sequence;
3821
3822 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3823 int ilow = 0, ihigh = INT_MAX;
3824 if (!_PyEval_SliceIndex(v, &ilow))
3825 return NULL;
3826 if (!_PyEval_SliceIndex(w, &ihigh))
3827 return NULL;
3828 return PySequence_GetSlice(u, ilow, ihigh);
3829 }
3830 else {
3831 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00003832 if (slice != NULL) {
3833 PyObject *res = PyObject_GetItem(u, slice);
3834 Py_DECREF(slice);
3835 return res;
3836 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00003837 else
3838 return NULL;
3839 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003840}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003841
3842static int
Guido van Rossumac7be682001-01-17 15:42:30 +00003843assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
3844 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003845{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003846 PyTypeObject *tp = u->ob_type;
3847 PySequenceMethods *sq = tp->tp_as_sequence;
3848
3849 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3850 int ilow = 0, ihigh = INT_MAX;
3851 if (!_PyEval_SliceIndex(v, &ilow))
3852 return -1;
3853 if (!_PyEval_SliceIndex(w, &ihigh))
3854 return -1;
3855 if (x == NULL)
3856 return PySequence_DelSlice(u, ilow, ihigh);
3857 else
3858 return PySequence_SetSlice(u, ilow, ihigh, x);
3859 }
3860 else {
3861 PyObject *slice = PySlice_New(v, w, NULL);
3862 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00003863 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003864 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00003865 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00003866 else
Guido van Rossum354797c2001-12-03 19:45:06 +00003867 res = PyObject_DelItem(u, slice);
3868 Py_DECREF(slice);
3869 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003870 }
3871 else
3872 return -1;
3873 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003874}
3875
Guido van Rossumb209a111997-04-29 18:18:01 +00003876static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003877cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003878{
Guido van Rossumac7be682001-01-17 15:42:30 +00003879 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003880 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00003881 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003882 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003883 break;
3884 case PyCmp_IS_NOT:
3885 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003886 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003887 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003888 res = PySequence_Contains(w, v);
3889 if (res < 0)
3890 return NULL;
3891 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003892 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00003893 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003894 if (res < 0)
3895 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003896 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003897 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003898 case PyCmp_EXC_MATCH:
Barry Warsaw4249f541997-08-22 21:26:19 +00003899 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003900 break;
3901 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00003902 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003903 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003904 v = res ? Py_True : Py_False;
3905 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003906 return v;
3907}
3908
Thomas Wouters52152252000-08-17 22:55:00 +00003909static PyObject *
3910import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003911{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003912 PyObject *x;
3913
3914 x = PyObject_GetAttr(v, name);
3915 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00003916 PyErr_Format(PyExc_ImportError,
3917 "cannot import name %.230s",
3918 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003919 }
Thomas Wouters52152252000-08-17 22:55:00 +00003920 return x;
3921}
Guido van Rossumac7be682001-01-17 15:42:30 +00003922
Thomas Wouters52152252000-08-17 22:55:00 +00003923static int
3924import_all_from(PyObject *locals, PyObject *v)
3925{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003926 PyObject *all = PyObject_GetAttrString(v, "__all__");
3927 PyObject *dict, *name, *value;
3928 int skip_leading_underscores = 0;
3929 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00003930
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003931 if (all == NULL) {
3932 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3933 return -1; /* Unexpected error */
3934 PyErr_Clear();
3935 dict = PyObject_GetAttrString(v, "__dict__");
3936 if (dict == NULL) {
3937 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3938 return -1;
3939 PyErr_SetString(PyExc_ImportError,
3940 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00003941 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003942 }
3943 all = PyMapping_Keys(dict);
3944 Py_DECREF(dict);
3945 if (all == NULL)
3946 return -1;
3947 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00003948 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003949
3950 for (pos = 0, err = 0; ; pos++) {
3951 name = PySequence_GetItem(all, pos);
3952 if (name == NULL) {
3953 if (!PyErr_ExceptionMatches(PyExc_IndexError))
3954 err = -1;
3955 else
3956 PyErr_Clear();
3957 break;
3958 }
3959 if (skip_leading_underscores &&
3960 PyString_Check(name) &&
3961 PyString_AS_STRING(name)[0] == '_')
3962 {
3963 Py_DECREF(name);
3964 continue;
3965 }
3966 value = PyObject_GetAttr(v, name);
3967 if (value == NULL)
3968 err = -1;
3969 else
3970 err = PyDict_SetItem(locals, name, value);
3971 Py_DECREF(name);
3972 Py_XDECREF(value);
3973 if (err != 0)
3974 break;
3975 }
3976 Py_DECREF(all);
3977 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00003978}
3979
Guido van Rossumb209a111997-04-29 18:18:01 +00003980static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003981build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003982{
Guido van Rossum7851eea2001-09-12 19:19:18 +00003983 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003984
3985 if (PyDict_Check(methods))
3986 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00003987 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00003988 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00003989 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
3990 base = PyTuple_GET_ITEM(bases, 0);
3991 metaclass = PyObject_GetAttrString(base, "__class__");
3992 if (metaclass == NULL) {
3993 PyErr_Clear();
3994 metaclass = (PyObject *)base->ob_type;
3995 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00003996 }
3997 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00003998 else {
3999 PyObject *g = PyEval_GetGlobals();
4000 if (g != NULL && PyDict_Check(g))
4001 metaclass = PyDict_GetItemString(g, "__metaclass__");
4002 if (metaclass == NULL)
4003 metaclass = (PyObject *) &PyClass_Type;
4004 Py_INCREF(metaclass);
4005 }
4006 result = PyObject_CallFunction(metaclass, "OOO", name, bases, methods);
4007 Py_DECREF(metaclass);
4008 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00004009}
4010
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004011static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004012exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
4013 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004014{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004015 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00004016 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004017 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004018
Guido van Rossumb209a111997-04-29 18:18:01 +00004019 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
4020 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004021 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00004022 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004023 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00004024 locals = PyTuple_GetItem(prog, 2);
4025 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004026 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004027 if (globals == Py_None) {
4028 globals = PyEval_GetGlobals();
4029 if (locals == Py_None) {
4030 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00004031 plain = 1;
4032 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004033 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004034 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004035 locals = globals;
Guido van Rossumb209a111997-04-29 18:18:01 +00004036 if (!PyString_Check(prog) &&
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004037 !PyUnicode_Check(prog) &&
Guido van Rossumb209a111997-04-29 18:18:01 +00004038 !PyCode_Check(prog) &&
4039 !PyFile_Check(prog)) {
4040 PyErr_SetString(PyExc_TypeError,
Guido van Rossumac7be682001-01-17 15:42:30 +00004041 "exec: arg 1 must be a string, file, or code object");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004042 return -1;
4043 }
Fred Drake661ea262000-10-24 19:57:45 +00004044 if (!PyDict_Check(globals)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004045 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00004046 "exec: arg 2 must be a dictionary or None");
4047 return -1;
4048 }
4049 if (!PyDict_Check(locals)) {
4050 PyErr_SetString(PyExc_TypeError,
4051 "exec: arg 3 must be a dictionary or None");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004052 return -1;
4053 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004054 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00004055 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00004056 if (PyCode_Check(prog)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +00004057 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4058 PyErr_SetString(PyExc_TypeError,
4059 "code object passed to exec may not contain free variables");
4060 return -1;
4061 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004062 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004063 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004064 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004065 FILE *fp = PyFile_AsFile(prog);
4066 char *name = PyString_AsString(PyFile_Name(prog));
Tim Peters5ba58662001-07-16 02:29:45 +00004067 PyCompilerFlags cf;
4068 cf.cf_flags = 0;
4069 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004070 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
4071 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004072 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004073 v = PyRun_File(fp, name, Py_file_input, globals,
4074 locals);
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004075 }
4076 else {
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004077 PyObject *tmp = NULL;
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004078 char *str;
Tim Peters5ba58662001-07-16 02:29:45 +00004079 PyCompilerFlags cf;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004080 cf.cf_flags = 0;
4081#ifdef Py_USING_UNICODE
4082 if (PyUnicode_Check(prog)) {
4083 tmp = PyUnicode_AsUTF8String(prog);
4084 if (tmp == NULL)
4085 return -1;
4086 prog = tmp;
4087 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4088 }
4089#endif
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004090 if (PyString_AsStringAndSize(prog, &str, NULL))
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004091 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004092 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004093 v = PyRun_StringFlags(str, Py_file_input, globals,
4094 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004095 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004096 v = PyRun_String(str, Py_file_input, globals, locals);
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004097 Py_XDECREF(tmp);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004098 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004099 if (plain)
4100 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004101 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004102 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00004103 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004104 return 0;
4105}
Guido van Rossum24c13741995-02-14 09:42:43 +00004106
Guido van Rossumac7be682001-01-17 15:42:30 +00004107static void
Paul Prescode68140d2000-08-30 20:25:01 +00004108format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4109{
4110 char *obj_str;
4111
4112 if (!obj)
4113 return;
4114
4115 obj_str = PyString_AsString(obj);
4116 if (!obj_str)
4117 return;
4118
4119 PyErr_Format(exc, format_str, obj_str);
4120}
Guido van Rossum950361c1997-01-24 13:49:28 +00004121
4122#ifdef DYNAMIC_EXECUTION_PROFILE
4123
Skip Montanarof118cb12001-10-15 20:51:38 +00004124static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004125getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004126{
4127 int i;
4128 PyObject *l = PyList_New(256);
4129 if (l == NULL) return NULL;
4130 for (i = 0; i < 256; i++) {
4131 PyObject *x = PyInt_FromLong(a[i]);
4132 if (x == NULL) {
4133 Py_DECREF(l);
4134 return NULL;
4135 }
4136 PyList_SetItem(l, i, x);
4137 }
4138 for (i = 0; i < 256; i++)
4139 a[i] = 0;
4140 return l;
4141}
4142
4143PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004144_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004145{
4146#ifndef DXPAIRS
4147 return getarray(dxp);
4148#else
4149 int i;
4150 PyObject *l = PyList_New(257);
4151 if (l == NULL) return NULL;
4152 for (i = 0; i < 257; i++) {
4153 PyObject *x = getarray(dxpairs[i]);
4154 if (x == NULL) {
4155 Py_DECREF(l);
4156 return NULL;
4157 }
4158 PyList_SetItem(l, i, x);
4159 }
4160 return l;
4161#endif
4162}
4163
4164#endif