blob: 49582dcd31d71cd445ec0f00cf5db3a9d5add849 [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 *);
Tim Peters8a5c3c72004-04-05 19:36:21 +000050static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Armin Rigobf57a142004-03-22 19:24:58 +000051 PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000052
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{
Tim Peters8a5c3c72004-04-05 19:36:21 +0000126 return Py_BuildValue("iiiiiiiiii",
Jeremy Hylton985eba52003-02-05 23:13:00 +0000127 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 Peters8a5c3c72004-04-05 19:36:21 +0000150 /* True if generator is being executed. */
Tim Peterse77f2e22001-06-26 22:24:51 +0000151 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
Guido van Rossum374a9221991-04-04 10:40:29 +0000539/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000540enum why_code {
541 WHY_NOT = 0x0001, /* No error */
542 WHY_EXCEPTION = 0x0002, /* Exception occurred */
543 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
544 WHY_RETURN = 0x0008, /* 'return' statement */
545 WHY_BREAK = 0x0010, /* 'break' statement */
546 WHY_CONTINUE = 0x0020, /* 'continue' statement */
547 WHY_YIELD = 0x0040 /* 'yield' operator */
548};
Guido van Rossum374a9221991-04-04 10:40:29 +0000549
Raymond Hettinger7c958652004-04-06 10:11:10 +0000550static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
Tim Petersd6d010b2001-06-21 02:49:55 +0000551static int unpack_iterable(PyObject *, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000552
Skip Montanarod581d772002-09-03 20:10:45 +0000553/* for manipulating the thread switch and periodic "stuff" - used to be
554 per thread, now just a pair o' globals */
Skip Montanaro99dba272002-09-03 20:19:06 +0000555int _Py_CheckInterval = 100;
556volatile int _Py_Ticker = 100;
Guido van Rossum374a9221991-04-04 10:40:29 +0000557
Guido van Rossumb209a111997-04-29 18:18:01 +0000558PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000559PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000560{
Jeremy Hylton985eba52003-02-05 23:13:00 +0000561 /* XXX raise SystemError if globals is NULL */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000562 return PyEval_EvalCodeEx(co,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000563 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000564 (PyObject **)NULL, 0,
565 (PyObject **)NULL, 0,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000566 (PyObject **)NULL, 0,
567 NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000568}
569
570
571/* Interpreter main loop */
572
Tim Peters6d6c1a32001-08-02 04:15:00 +0000573static PyObject *
Tim Peters5ca576e2001-06-18 22:08:13 +0000574eval_frame(PyFrameObject *f)
Guido van Rossum374a9221991-04-04 10:40:29 +0000575{
Guido van Rossum950361c1997-01-24 13:49:28 +0000576#ifdef DXPAIRS
577 int lastopcode = 0;
578#endif
Tim Petersb6d14da2001-12-19 04:11:07 +0000579 PyObject **stack_pointer; /* Next free slot in value stack */
Guido van Rossum374a9221991-04-04 10:40:29 +0000580 register unsigned char *next_instr;
Moshe Zadkaaa39a7e2000-08-07 06:34:45 +0000581 register int opcode=0; /* Current opcode */
582 register int oparg=0; /* Current opcode argument, if any */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000583 register enum why_code why; /* Reason for block stack unwind */
Guido van Rossum374a9221991-04-04 10:40:29 +0000584 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000585 register PyObject *x; /* Result object -- NULL if error */
586 register PyObject *v; /* Temporary objects popped off stack */
587 register PyObject *w;
588 register PyObject *u;
589 register PyObject *t;
Barry Warsaw23c9ec82000-08-21 15:44:01 +0000590 register PyObject *stream = NULL; /* for PRINT opcodes */
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000591 register PyObject **fastlocals, **freevars;
Guido van Rossum014518f1998-11-23 21:09:51 +0000592 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000593 PyThreadState *tstate = PyThreadState_GET();
Tim Peters5ca576e2001-06-18 22:08:13 +0000594 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000595
Tim Peters8a5c3c72004-04-05 19:36:21 +0000596 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000597
598 not (instr_lb <= current_bytecode_offset < instr_ub)
599
Tim Peters8a5c3c72004-04-05 19:36:21 +0000600 is true when the line being executed has changed. The
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000601 initial values are such as to make this false the first
602 time it is tested. */
Armin Rigobf57a142004-03-22 19:24:58 +0000603 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000604
Guido van Rossumd076c731998-10-07 19:42:25 +0000605 unsigned char *first_instr;
Skip Montanaro04d80f82002-08-04 21:03:35 +0000606 PyObject *names;
607 PyObject *consts;
Guido van Rossum96a42c81992-01-12 02:29:51 +0000608#ifdef LLTRACE
Guido van Rossumacbe8da1993-04-15 15:33:52 +0000609 int lltrace;
Guido van Rossum374a9221991-04-04 10:40:29 +0000610#endif
Guido van Rossum408027e1996-12-30 16:17:54 +0000611#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000612 /* Make it easier to find out where we are with a debugger */
Tim Peters5ca576e2001-06-18 22:08:13 +0000613 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000614#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000615
Neal Norwitza81d2202002-07-14 00:27:26 +0000616/* Tuple access macros */
617
618#ifndef Py_DEBUG
619#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
620#else
621#define GETITEM(v, i) PyTuple_GetItem((v), (i))
622#endif
623
Guido van Rossum374a9221991-04-04 10:40:29 +0000624/* Code access macros */
625
Guido van Rossumd076c731998-10-07 19:42:25 +0000626#define INSTR_OFFSET() (next_instr - first_instr)
Guido van Rossum374a9221991-04-04 10:40:29 +0000627#define NEXTOP() (*next_instr++)
Raymond Hettingerd3b836d2004-04-07 13:17:27 +0000628#define OPARG() (next_instr[0] + (next_instr[1]<<8))
629#define SKIPARG() (next_instr += 2)
Guido van Rossumd076c731998-10-07 19:42:25 +0000630#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000631#define JUMPBY(x) (next_instr += (x))
632
Raymond Hettingerf606f872003-03-16 03:11:04 +0000633/* OpCode prediction macros
634 Some opcodes tend to come in pairs thus making it possible to predict
635 the second code when the first is run. For example, COMPARE_OP is often
636 followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And, those opcodes are often
637 followed by a POP_TOP.
638
639 Verifying the prediction costs a single high-speed test of register
Raymond Hettingerac2072922003-03-16 15:41:11 +0000640 variable against a constant. If the pairing was good, then the
Raymond Hettingerf606f872003-03-16 03:11:04 +0000641 processor has a high likelihood of making its own successful branch
642 prediction which results in a nearly zero overhead transition to the
643 next opcode.
644
645 A successful prediction saves a trip through the eval-loop including
646 its two unpredictable branches, the HASARG test and the switch-case.
Raymond Hettingera7216982004-02-08 19:59:27 +0000647
Tim Peters8a5c3c72004-04-05 19:36:21 +0000648 If collecting opcode statistics, turn off prediction so that
649 statistics are accurately maintained (the predictions bypass
Raymond Hettingera7216982004-02-08 19:59:27 +0000650 the opcode frequency counter updates).
Raymond Hettingerf606f872003-03-16 03:11:04 +0000651*/
652
Raymond Hettingera7216982004-02-08 19:59:27 +0000653#ifdef DYNAMIC_EXECUTION_PROFILE
654#define PREDICT(op) if (0) goto PRED_##op
655#else
Raymond Hettingerac2072922003-03-16 15:41:11 +0000656#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000657#endif
658
Raymond Hettingerf606f872003-03-16 03:11:04 +0000659#define PREDICTED(op) PRED_##op: next_instr++
Armin Rigo9dbf9082004-03-20 21:50:13 +0000660#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = (next_instr[2]<<8) + \
661 next_instr[1]; next_instr += 3
Raymond Hettingerf606f872003-03-16 03:11:04 +0000662
Guido van Rossum374a9221991-04-04 10:40:29 +0000663/* Stack manipulation macros */
664
665#define STACK_LEVEL() (stack_pointer - f->f_valuestack)
666#define EMPTY() (STACK_LEVEL() == 0)
667#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000668#define SECOND() (stack_pointer[-2])
669#define THIRD() (stack_pointer[-3])
670#define FOURTH() (stack_pointer[-4])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000671#define SET_TOP(v) (stack_pointer[-1] = (v))
672#define SET_SECOND(v) (stack_pointer[-2] = (v))
673#define SET_THIRD(v) (stack_pointer[-3] = (v))
674#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000675#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000676#define BASIC_PUSH(v) (*stack_pointer++ = (v))
677#define BASIC_POP() (*--stack_pointer)
678
Guido van Rossum96a42c81992-01-12 02:29:51 +0000679#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000680#define PUSH(v) { (void)(BASIC_PUSH(v), \
681 lltrace && prtrace(TOP(), "push")); \
682 assert(STACK_LEVEL() <= f->f_stacksize); }
Fred Drakede26cfc2001-10-13 06:11:28 +0000683#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000684#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
685 lltrace && prtrace(TOP(), "stackadj")); \
686 assert(STACK_LEVEL() <= f->f_stacksize); }
Guido van Rossum374a9221991-04-04 10:40:29 +0000687#else
688#define PUSH(v) BASIC_PUSH(v)
689#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000690#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000691#endif
692
Guido van Rossum681d79a1995-07-18 14:51:37 +0000693/* Local variable macros */
694
695#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000696
697/* The SETLOCAL() macro must not DECREF the local variable in-place and
698 then store the new value; it must copy the old value to a temporary
699 value, then store the new value, and then DECREF the temporary value.
700 This is because it is possible that during the DECREF the frame is
701 accessed by other code (e.g. a __del__ method or gc.collect()) and the
702 variable would be pointing to already-freed memory. */
703#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
704 GETLOCAL(i) = value; \
705 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000706
Guido van Rossuma027efa1997-05-05 20:56:21 +0000707/* Start of code */
708
Tim Peters5ca576e2001-06-18 22:08:13 +0000709 if (f == NULL)
710 return NULL;
711
Armin Rigo1d313ab2003-10-25 14:33:09 +0000712 /* push frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000713 if (Py_EnterRecursiveCall(""))
Armin Rigo1d313ab2003-10-25 14:33:09 +0000714 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000715
Tim Peters5ca576e2001-06-18 22:08:13 +0000716 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000717
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000718 if (tstate->use_tracing) {
719 if (tstate->c_tracefunc != NULL) {
720 /* tstate->c_tracefunc, if defined, is a
721 function that will be called on *every* entry
722 to a code block. Its return value, if not
723 None, is a function that will be called at
724 the start of each executed line of code.
725 (Actually, the function must return itself
726 in order to continue tracing.) The trace
727 functions are called with three arguments:
728 a pointer to the current frame, a string
729 indicating why the function is called, and
730 an argument which depends on the situation.
731 The global trace function is also called
732 whenever an exception is detected. */
733 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
734 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000735 /* Trace function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000736 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000737 }
738 }
739 if (tstate->c_profilefunc != NULL) {
740 /* Similar for c_profilefunc, except it needn't
741 return itself and isn't called for "line" events */
742 if (call_trace(tstate->c_profilefunc,
743 tstate->c_profileobj,
744 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000745 /* Profile function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000746 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000747 }
748 }
749 }
750
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000751 co = f->f_code;
752 names = co->co_names;
753 consts = co->co_consts;
754 fastlocals = f->f_localsplus;
755 freevars = f->f_localsplus + f->f_nlocals;
Michael W. Hudsonecfeb7f2004-02-12 15:28:27 +0000756 first_instr = PyString_AS_STRING(co->co_code);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000757 /* An explanation is in order for the next line.
758
759 f->f_lasti now refers to the index of the last instruction
760 executed. You might think this was obvious from the name, but
761 this wasn't always true before 2.3! PyFrame_New now sets
762 f->f_lasti to -1 (i.e. the index *before* the first instruction)
763 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
764 does work. Promise. */
765 next_instr = first_instr + f->f_lasti + 1;
766 stack_pointer = f->f_stacktop;
767 assert(stack_pointer != NULL);
768 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
769
Tim Peters5ca576e2001-06-18 22:08:13 +0000770#ifdef LLTRACE
771 lltrace = PyDict_GetItemString(f->f_globals,"__lltrace__") != NULL;
772#endif
773#if defined(Py_DEBUG) || defined(LLTRACE)
774 filename = PyString_AsString(co->co_filename);
775#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000776
Guido van Rossum374a9221991-04-04 10:40:29 +0000777 why = WHY_NOT;
778 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +0000779 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +0000780 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +0000781
Guido van Rossum374a9221991-04-04 10:40:29 +0000782 for (;;) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000783 assert(stack_pointer >= f->f_valuestack); /* else underflow */
784 assert(STACK_LEVEL() <= f->f_stacksize); /* else overflow */
785
Guido van Rossuma027efa1997-05-05 20:56:21 +0000786 /* Do periodic things. Doing this every time through
787 the loop would add too much overhead, so we do it
788 only every Nth instruction. We also do it if
789 ``things_to_do'' is set, i.e. when an asynchronous
790 event needs attention (e.g. a signal handler or
791 async I/O handler); see Py_AddPendingCall() and
792 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000793
Skip Montanarod581d772002-09-03 20:10:45 +0000794 if (--_Py_Ticker < 0) {
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000795 if (*next_instr == SETUP_FINALLY) {
796 /* Make the last opcode before
797 a try: finally: block uninterruptable. */
798 goto fast_next_opcode;
799 }
Skip Montanarod581d772002-09-03 20:10:45 +0000800 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000801 tstate->tick_counter++;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000802 if (things_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +0000803 if (Py_MakePendingCalls() < 0) {
804 why = WHY_EXCEPTION;
805 goto on_error;
806 }
807 }
Guido van Rossume59214e1994-08-30 08:01:59 +0000808#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000809 if (interpreter_lock) {
810 /* Give another thread a chance */
811
Guido van Rossum25ce5661997-08-02 03:10:38 +0000812 if (PyThreadState_Swap(NULL) != tstate)
813 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000814 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000815
816 /* Other threads may run now */
817
Guido van Rossum65d5b571998-12-21 19:32:43 +0000818 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000819 if (PyThreadState_Swap(tstate) != NULL)
820 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000821
822 /* Check for thread interrupts */
823
824 if (tstate->async_exc != NULL) {
825 x = tstate->async_exc;
826 tstate->async_exc = NULL;
827 PyErr_SetNone(x);
828 Py_DECREF(x);
829 why = WHY_EXCEPTION;
830 goto on_error;
831 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000832 }
833#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000834 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000835
Neil Schemenauer63543862002-02-17 19:10:14 +0000836 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +0000837 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +0000838
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000839 /* line-by-line tracing support */
840
841 if (tstate->c_tracefunc != NULL && !tstate->tracing) {
842 /* see maybe_call_line_trace
843 for expository comments */
844 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +0000845
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000846 err = maybe_call_line_trace(tstate->c_tracefunc,
847 tstate->c_traceobj,
Armin Rigobf57a142004-03-22 19:24:58 +0000848 f, &instr_lb, &instr_ub,
849 &instr_prev);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000850 /* Reload possibly changed frame fields */
851 JUMPTO(f->f_lasti);
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000852 if (f->f_stacktop != NULL) {
853 stack_pointer = f->f_stacktop;
854 f->f_stacktop = NULL;
855 }
856 if (err) {
857 /* trace function raised an exception */
858 goto on_error;
859 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000860 }
861
862 /* Extract opcode and argument */
863
Guido van Rossum374a9221991-04-04 10:40:29 +0000864 opcode = NEXTOP();
Raymond Hettingerd3b836d2004-04-07 13:17:27 +0000865 if (HAS_ARG(opcode)) {
866 oparg = OPARG();
867 SKIPARG();
868 }
Fred Drakeef8ace32000-08-24 00:32:09 +0000869 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +0000870#ifdef DYNAMIC_EXECUTION_PROFILE
871#ifdef DXPAIRS
872 dxpairs[lastopcode][opcode]++;
873 lastopcode = opcode;
874#endif
875 dxp[opcode]++;
876#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000877
Guido van Rossum96a42c81992-01-12 02:29:51 +0000878#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +0000879 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +0000880
Guido van Rossum96a42c81992-01-12 02:29:51 +0000881 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000882 if (HAS_ARG(opcode)) {
883 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000884 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +0000885 }
886 else {
887 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000888 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +0000889 }
890 }
891#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000892
Guido van Rossum374a9221991-04-04 10:40:29 +0000893 /* Main switch on opcode */
Jeremy Hylton52820442001-01-03 23:52:36 +0000894
Guido van Rossum374a9221991-04-04 10:40:29 +0000895 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +0000896
Guido van Rossum374a9221991-04-04 10:40:29 +0000897 /* BEWARE!
898 It is essential that any operation that fails sets either
899 x to NULL, err to nonzero, or why to anything but WHY_NOT,
900 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000901
Guido van Rossum374a9221991-04-04 10:40:29 +0000902 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000903
Neil Schemenauer63543862002-02-17 19:10:14 +0000904 case LOAD_FAST:
905 x = GETLOCAL(oparg);
906 if (x != NULL) {
907 Py_INCREF(x);
908 PUSH(x);
909 goto fast_next_opcode;
910 }
911 format_exc_check_arg(PyExc_UnboundLocalError,
912 UNBOUNDLOCAL_ERROR_MSG,
913 PyTuple_GetItem(co->co_varnames, oparg));
914 break;
915
916 case LOAD_CONST:
Skip Montanaro04d80f82002-08-04 21:03:35 +0000917 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +0000918 Py_INCREF(x);
919 PUSH(x);
920 goto fast_next_opcode;
921
Raymond Hettinger7dc52212003-03-16 20:14:44 +0000922 PREDICTED_WITH_ARG(STORE_FAST);
Neil Schemenauer63543862002-02-17 19:10:14 +0000923 case STORE_FAST:
924 v = POP();
925 SETLOCAL(oparg, v);
926 goto fast_next_opcode;
927
Raymond Hettingerf606f872003-03-16 03:11:04 +0000928 PREDICTED(POP_TOP);
Guido van Rossum374a9221991-04-04 10:40:29 +0000929 case POP_TOP:
930 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000931 Py_DECREF(v);
Neil Schemenauer63543862002-02-17 19:10:14 +0000932 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000933
Guido van Rossum374a9221991-04-04 10:40:29 +0000934 case ROT_TWO:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000935 v = TOP();
936 w = SECOND();
937 SET_TOP(w);
938 SET_SECOND(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000939 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000940
Guido van Rossum374a9221991-04-04 10:40:29 +0000941 case ROT_THREE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000942 v = TOP();
943 w = SECOND();
944 x = THIRD();
945 SET_TOP(w);
946 SET_SECOND(x);
947 SET_THIRD(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000948 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000949
Thomas Wouters434d0822000-08-24 20:11:32 +0000950 case ROT_FOUR:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000951 u = TOP();
952 v = SECOND();
953 w = THIRD();
954 x = FOURTH();
955 SET_TOP(v);
956 SET_SECOND(w);
957 SET_THIRD(x);
958 SET_FOURTH(u);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000959 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000960
Guido van Rossum374a9221991-04-04 10:40:29 +0000961 case DUP_TOP:
962 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000963 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000964 PUSH(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000965 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000966
Thomas Wouters434d0822000-08-24 20:11:32 +0000967 case DUP_TOPX:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000968 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000969 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000970 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000971 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000972 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000973 STACKADJ(2);
974 SET_TOP(x);
975 SET_SECOND(w);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000976 goto fast_next_opcode;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000977 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000978 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000979 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000980 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000981 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000982 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +0000983 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000984 STACKADJ(3);
985 SET_TOP(x);
986 SET_SECOND(w);
987 SET_THIRD(v);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000988 goto fast_next_opcode;
Thomas Wouters434d0822000-08-24 20:11:32 +0000989 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000990 Py_FatalError("invalid argument to DUP_TOPX"
991 " (bytecode corruption?)");
Tim Peters35ba6892000-10-11 07:04:49 +0000992 break;
Thomas Wouters434d0822000-08-24 20:11:32 +0000993
Guido van Rossum374a9221991-04-04 10:40:29 +0000994 case UNARY_POSITIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000995 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000996 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000997 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000998 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000999 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001000 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001001
Guido van Rossum374a9221991-04-04 10:40:29 +00001002 case UNARY_NEGATIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001003 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001004 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001005 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001006 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001007 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001008 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001009
Guido van Rossum374a9221991-04-04 10:40:29 +00001010 case UNARY_NOT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001011 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001012 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001013 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +00001014 if (err == 0) {
1015 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001016 SET_TOP(Py_True);
Guido van Rossumfc490731997-05-06 15:06:49 +00001017 continue;
1018 }
1019 else if (err > 0) {
1020 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001021 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +00001022 err = 0;
1023 continue;
1024 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00001025 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001026 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001027
Guido van Rossum374a9221991-04-04 10:40:29 +00001028 case UNARY_CONVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001029 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001030 x = PyObject_Repr(v);
1031 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001032 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001033 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001034 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001035
Guido van Rossum7928cd71991-10-24 14:59:31 +00001036 case UNARY_INVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001037 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001038 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001039 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001040 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001041 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001042 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001043
Guido van Rossum50564e81996-01-12 01:13:16 +00001044 case BINARY_POWER:
1045 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001046 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001047 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +00001048 Py_DECREF(v);
1049 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001050 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001051 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +00001052 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001053
Guido van Rossum374a9221991-04-04 10:40:29 +00001054 case BINARY_MULTIPLY:
1055 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001056 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001057 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001058 Py_DECREF(v);
1059 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001060 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001061 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001062 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001063
Guido van Rossum374a9221991-04-04 10:40:29 +00001064 case BINARY_DIVIDE:
Tim Peters3caca232001-12-06 06:23:26 +00001065 if (!_Py_QnewFlag) {
1066 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001067 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001068 x = PyNumber_Divide(v, w);
1069 Py_DECREF(v);
1070 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001071 SET_TOP(x);
Tim Peters3caca232001-12-06 06:23:26 +00001072 if (x != NULL) continue;
1073 break;
1074 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001075 /* -Qnew is in effect: fall through to
Tim Peters3caca232001-12-06 06:23:26 +00001076 BINARY_TRUE_DIVIDE */
1077 case BINARY_TRUE_DIVIDE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001078 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001079 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001080 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001081 Py_DECREF(v);
1082 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001083 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001084 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001085 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001086
Guido van Rossum4668b002001-08-08 05:00:18 +00001087 case BINARY_FLOOR_DIVIDE:
1088 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001089 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001090 x = PyNumber_FloorDivide(v, w);
1091 Py_DECREF(v);
1092 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001093 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001094 if (x != NULL) continue;
1095 break;
1096
Guido van Rossum374a9221991-04-04 10:40:29 +00001097 case BINARY_MODULO:
1098 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001099 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001100 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001101 Py_DECREF(v);
1102 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001103 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001104 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001105 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001106
Guido van Rossum374a9221991-04-04 10:40:29 +00001107 case BINARY_ADD:
1108 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001109 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001110 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001111 /* INLINE: int + int */
1112 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001113 a = PyInt_AS_LONG(v);
1114 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001115 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001116 if ((i^a) < 0 && (i^b) < 0)
1117 goto slow_add;
1118 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001119 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001120 else {
1121 slow_add:
Guido van Rossumc12da691997-07-17 23:12:42 +00001122 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001123 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001124 Py_DECREF(v);
1125 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001126 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001127 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001128 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001129
Guido van Rossum374a9221991-04-04 10:40:29 +00001130 case BINARY_SUBTRACT:
1131 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001132 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001133 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001134 /* INLINE: int - int */
1135 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001136 a = PyInt_AS_LONG(v);
1137 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001138 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001139 if ((i^a) < 0 && (i^~b) < 0)
1140 goto slow_sub;
1141 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001142 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001143 else {
1144 slow_sub:
Guido van Rossumc12da691997-07-17 23:12:42 +00001145 x = PyNumber_Subtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001146 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001147 Py_DECREF(v);
1148 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001149 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001150 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001151 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001152
Guido van Rossum374a9221991-04-04 10:40:29 +00001153 case BINARY_SUBSCR:
1154 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001155 v = TOP();
Tim Petersb1c46982001-10-05 20:41:38 +00001156 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001157 /* INLINE: list[int] */
1158 long i = PyInt_AsLong(w);
1159 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +00001160 i += PyList_GET_SIZE(v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001161 if (i >= 0 && i < PyList_GET_SIZE(v)) {
Guido van Rossumfa00e951998-07-08 15:02:37 +00001162 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001163 Py_INCREF(x);
1164 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001165 else
1166 goto slow_get;
Guido van Rossumc12da691997-07-17 23:12:42 +00001167 }
1168 else
Raymond Hettinger467a6982004-04-07 11:39:21 +00001169 slow_get:
Guido van Rossumc12da691997-07-17 23:12:42 +00001170 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001171 Py_DECREF(v);
1172 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001173 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001174 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001175 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001176
Guido van Rossum7928cd71991-10-24 14:59:31 +00001177 case BINARY_LSHIFT:
1178 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001179 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001180 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001181 Py_DECREF(v);
1182 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001183 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001184 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001185 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001186
Guido van Rossum7928cd71991-10-24 14:59:31 +00001187 case BINARY_RSHIFT:
1188 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001189 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001190 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001191 Py_DECREF(v);
1192 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001193 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001194 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001195 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001196
Guido van Rossum7928cd71991-10-24 14:59:31 +00001197 case BINARY_AND:
1198 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001199 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001200 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001201 Py_DECREF(v);
1202 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001203 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001204 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001205 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001206
Guido van Rossum7928cd71991-10-24 14:59:31 +00001207 case BINARY_XOR:
1208 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001209 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001210 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001211 Py_DECREF(v);
1212 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001213 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001214 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001215 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001216
Guido van Rossum7928cd71991-10-24 14:59:31 +00001217 case BINARY_OR:
1218 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001219 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001220 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001221 Py_DECREF(v);
1222 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001223 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001224 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001225 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001226
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001227 case LIST_APPEND:
1228 w = POP();
1229 v = POP();
1230 err = PyList_Append(v, w);
1231 Py_DECREF(v);
1232 Py_DECREF(w);
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001233 if (err == 0) {
1234 PREDICT(JUMP_ABSOLUTE);
1235 continue;
1236 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001237 break;
1238
Thomas Wouters434d0822000-08-24 20:11:32 +00001239 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);
Tim Peters8a5c3c72004-04-05 19:36:21 +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 Rossumf10570b1995-07-07 22:53:21 +00001588 case RAISE_VARARGS:
1589 u = v = w = NULL;
1590 switch (oparg) {
1591 case 3:
1592 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001593 /* Fallthrough */
1594 case 2:
1595 v = POP(); /* value */
1596 /* Fallthrough */
1597 case 1:
1598 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001599 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001600 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001601 break;
1602 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001603 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001604 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001605 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001606 break;
1607 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001608 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001609
Guido van Rossum374a9221991-04-04 10:40:29 +00001610 case LOAD_LOCALS:
Raymond Hettinger467a6982004-04-07 11:39:21 +00001611 if ((x = f->f_locals) != NULL) {
1612 Py_INCREF(x);
1613 PUSH(x);
1614 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001615 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001616 PyErr_SetString(PyExc_SystemError, "no locals");
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 RETURN_VALUE:
1620 retval = POP();
1621 why = WHY_RETURN;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001622 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001623
Tim Peters5ca576e2001-06-18 22:08:13 +00001624 case YIELD_VALUE:
1625 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001626 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001627 why = WHY_YIELD;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001628 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001629
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001630 case EXEC_STMT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001631 w = TOP();
1632 v = SECOND();
1633 u = THIRD();
1634 STACKADJ(-3);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001635 err = exec_statement(f, u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001636 Py_DECREF(u);
1637 Py_DECREF(v);
1638 Py_DECREF(w);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001639 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001640
Guido van Rossum374a9221991-04-04 10:40:29 +00001641 case POP_BLOCK:
1642 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001643 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001644 while (STACK_LEVEL() > b->b_level) {
1645 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001646 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001647 }
1648 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001649 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001650
Guido van Rossum374a9221991-04-04 10:40:29 +00001651 case END_FINALLY:
1652 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001653 if (PyInt_Check(v)) {
Raymond Hettinger7c958652004-04-06 10:11:10 +00001654 why = (enum why_code) PyInt_AS_LONG(v);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001655 assert(why != WHY_YIELD);
Raymond Hettinger06032cb2004-04-06 09:37:35 +00001656 if (why & (WHY_RETURN | WHY_CONTINUE))
Guido van Rossum374a9221991-04-04 10:40:29 +00001657 retval = POP();
1658 }
Raymond Hettingerd3b836d2004-04-07 13:17:27 +00001659 else if (PyClass_Check(v) || PyString_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001660 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001661 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001662 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001663 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001664 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001665 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001666 else if (v != Py_None) {
1667 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001668 "'finally' pops bad exception");
1669 why = WHY_EXCEPTION;
1670 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001671 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001672 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001673
Guido van Rossum374a9221991-04-04 10:40:29 +00001674 case BUILD_CLASS:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001675 u = TOP();
1676 v = SECOND();
1677 w = THIRD();
1678 STACKADJ(-2);
Guido van Rossum25831651993-05-19 14:50:45 +00001679 x = build_class(u, v, w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001680 SET_TOP(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001681 Py_DECREF(u);
1682 Py_DECREF(v);
1683 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001684 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001685
Guido van Rossum374a9221991-04-04 10:40:29 +00001686 case STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001687 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001688 v = POP();
Raymond Hettinger467a6982004-04-07 11:39:21 +00001689 if ((x = f->f_locals) != NULL) {
1690 err = PyDict_SetItem(x, w, v);
1691 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001692 if (err == 0) continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001693 break;
1694 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001695 PyErr_Format(PyExc_SystemError,
1696 "no locals found when storing %s",
1697 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001698 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001699
Guido van Rossum374a9221991-04-04 10:40:29 +00001700 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001701 w = GETITEM(names, oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001702 if ((x = f->f_locals) != NULL) {
1703 if ((err = PyDict_DelItem(x, w)) != 0)
1704 format_exc_check_arg(PyExc_NameError,
1705 NAME_ERROR_MSG ,w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001706 break;
1707 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001708 PyErr_Format(PyExc_SystemError,
1709 "no locals when deleting %s",
1710 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001711 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001712
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001713 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001714 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001715 v = POP();
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001716 if (PyTuple_CheckExact(v) && PyTuple_GET_SIZE(v) == oparg) {
1717 PyObject **items = ((PyTupleObject *)v)->ob_item;
1718 while (oparg--) {
1719 w = items[oparg];
1720 Py_INCREF(w);
1721 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001722 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001723 Py_DECREF(v);
1724 continue;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001725 } else if (PyList_CheckExact(v) && PyList_GET_SIZE(v) == oparg) {
1726 PyObject **items = ((PyListObject *)v)->ob_item;
1727 while (oparg--) {
1728 w = items[oparg];
1729 Py_INCREF(w);
1730 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001731 }
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001732 } else if (unpack_iterable(v, oparg,
Tim Petersd6d010b2001-06-21 02:49:55 +00001733 stack_pointer + oparg))
1734 stack_pointer += oparg;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001735 else {
1736 if (PyErr_ExceptionMatches(PyExc_TypeError))
1737 PyErr_SetString(PyExc_TypeError,
1738 "unpack non-sequence");
Barry Warsawe42b18f1997-08-25 22:13:04 +00001739 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001740 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001741 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001742 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001743
Guido van Rossum374a9221991-04-04 10:40:29 +00001744 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001745 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001746 v = TOP();
1747 u = SECOND();
1748 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001749 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1750 Py_DECREF(v);
1751 Py_DECREF(u);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001752 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001753 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001754
Guido van Rossum374a9221991-04-04 10:40:29 +00001755 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001756 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001757 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001758 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1759 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001760 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001761 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001762
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001763 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001764 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001765 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001766 err = PyDict_SetItem(f->f_globals, w, v);
1767 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001768 if (err == 0) continue;
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001769 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001770
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001771 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001772 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001773 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001774 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001775 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001776 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001777
Guido van Rossum374a9221991-04-04 10:40:29 +00001778 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001779 w = GETITEM(names, oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001780 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001781 PyErr_Format(PyExc_SystemError,
1782 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001783 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001784 break;
1785 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001786 x = PyDict_GetItem(x, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001787 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001788 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001789 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001790 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001791 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001792 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001793 PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001794 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001795 break;
1796 }
1797 }
1798 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001799 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001800 PUSH(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001801 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001802
Guido van Rossum374a9221991-04-04 10:40:29 +00001803 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001804 w = GETITEM(names, oparg);
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001805 if (PyString_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00001806 /* Inline the PyDict_GetItem() calls.
1807 WARNING: this is an extreme speed hack.
1808 Do not try this at home. */
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001809 long hash = ((PyStringObject *)w)->ob_shash;
1810 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001811 PyDictObject *d;
1812 d = (PyDictObject *)(f->f_globals);
1813 x = d->ma_lookup(d, w, hash)->me_value;
1814 if (x != NULL) {
1815 Py_INCREF(x);
1816 PUSH(x);
1817 continue;
1818 }
1819 d = (PyDictObject *)(f->f_builtins);
1820 x = d->ma_lookup(d, w, hash)->me_value;
1821 if (x != NULL) {
1822 Py_INCREF(x);
1823 PUSH(x);
1824 continue;
1825 }
1826 goto load_global_error;
1827 }
1828 }
1829 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00001830 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001831 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001832 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001833 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001834 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00001835 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001836 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001837 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001838 break;
1839 }
1840 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001841 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001842 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001843 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001844
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001845 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001846 x = GETLOCAL(oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001847 if (x != NULL) {
1848 SETLOCAL(oparg, NULL);
1849 continue;
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001850 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001851 format_exc_check_arg(
1852 PyExc_UnboundLocalError,
1853 UNBOUNDLOCAL_ERROR_MSG,
1854 PyTuple_GetItem(co->co_varnames, oparg)
1855 );
1856 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001857
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001858 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001859 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001860 Py_INCREF(x);
1861 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001862 if (x != NULL) continue;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001863 break;
1864
1865 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001866 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001867 w = PyCell_Get(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001868 if (w != NULL) {
1869 PUSH(w);
1870 continue;
Jeremy Hylton2524d692001-02-05 17:23:16 +00001871 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001872 err = -1;
1873 /* Don't stomp existing exception */
1874 if (PyErr_Occurred())
1875 break;
1876 if (oparg < f->f_ncells) {
1877 v = PyTuple_GetItem(co->co_cellvars,
1878 oparg);
1879 format_exc_check_arg(
1880 PyExc_UnboundLocalError,
1881 UNBOUNDLOCAL_ERROR_MSG,
1882 v);
1883 } else {
1884 v = PyTuple_GetItem(
1885 co->co_freevars,
1886 oparg - f->f_ncells);
1887 format_exc_check_arg(
1888 PyExc_NameError,
1889 UNBOUNDFREE_ERROR_MSG,
1890 v);
1891 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001892 break;
1893
1894 case STORE_DEREF:
1895 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001896 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001897 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00001898 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001899 continue;
1900
Guido van Rossum374a9221991-04-04 10:40:29 +00001901 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001902 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001903 if (x != NULL) {
Raymond Hettingerd3b836d2004-04-07 13:17:27 +00001904 while (oparg--) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001905 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001906 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001907 }
1908 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001909 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001910 }
1911 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001912
Guido van Rossum374a9221991-04-04 10:40:29 +00001913 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00001914 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001915 if (x != NULL) {
Raymond Hettingerd3b836d2004-04-07 13:17:27 +00001916 while (oparg--) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001917 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00001918 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001919 }
1920 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001921 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001922 }
1923 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001924
Guido van Rossum374a9221991-04-04 10:40:29 +00001925 case BUILD_MAP:
Guido van Rossumb209a111997-04-29 18:18:01 +00001926 x = PyDict_New();
Guido van Rossum374a9221991-04-04 10:40:29 +00001927 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001928 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001929 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001930
Guido van Rossum374a9221991-04-04 10:40:29 +00001931 case LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001932 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001933 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001934 x = PyObject_GetAttr(v, w);
1935 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001936 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001937 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001938 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001939
Guido van Rossum374a9221991-04-04 10:40:29 +00001940 case COMPARE_OP:
1941 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001942 v = TOP();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001943 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001944 /* INLINE: cmp(int, int) */
1945 register long a, b;
1946 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001947 a = PyInt_AS_LONG(v);
1948 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001949 switch (oparg) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00001950 case PyCmp_LT: res = a < b; break;
1951 case PyCmp_LE: res = a <= b; break;
1952 case PyCmp_EQ: res = a == b; break;
1953 case PyCmp_NE: res = a != b; break;
1954 case PyCmp_GT: res = a > b; break;
1955 case PyCmp_GE: res = a >= b; break;
1956 case PyCmp_IS: res = v == w; break;
1957 case PyCmp_IS_NOT: res = v != w; break;
Guido van Rossumc12da691997-07-17 23:12:42 +00001958 default: goto slow_compare;
1959 }
1960 x = res ? Py_True : Py_False;
1961 Py_INCREF(x);
1962 }
1963 else {
1964 slow_compare:
1965 x = cmp_outcome(oparg, v, w);
1966 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001967 Py_DECREF(v);
1968 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001969 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001970 if (x == NULL) break;
1971 PREDICT(JUMP_IF_FALSE);
1972 PREDICT(JUMP_IF_TRUE);
1973 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001974
Guido van Rossum374a9221991-04-04 10:40:29 +00001975 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001976 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001977 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001978 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001979 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00001980 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001981 break;
1982 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001983 u = TOP();
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001984 w = PyTuple_Pack(4,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001985 w,
1986 f->f_globals,
Guido van Rossuma027efa1997-05-05 20:56:21 +00001987 f->f_locals == NULL ?
1988 Py_None : f->f_locals,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001989 u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001990 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001991 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001992 u = POP();
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001993 x = NULL;
1994 break;
1995 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001996 x = PyEval_CallObject(x, w);
1997 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001998 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001999 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002000 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002001
Thomas Wouters52152252000-08-17 22:55:00 +00002002 case IMPORT_STAR:
2003 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002004 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002005 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002006 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00002007 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002008 break;
2009 }
Thomas Wouters52152252000-08-17 22:55:00 +00002010 err = import_all_from(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002011 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00002012 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002013 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002014 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002015
Thomas Wouters52152252000-08-17 22:55:00 +00002016 case IMPORT_FROM:
Skip Montanaro496e6582002-08-06 17:47:40 +00002017 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00002018 v = TOP();
2019 x = import_from(v, w);
2020 PUSH(x);
2021 if (x != NULL) continue;
2022 break;
2023
Guido van Rossum374a9221991-04-04 10:40:29 +00002024 case JUMP_FORWARD:
2025 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002026 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002027
Raymond Hettingerf606f872003-03-16 03:11:04 +00002028 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002029 case JUMP_IF_FALSE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002030 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002031 if (w == Py_True) {
2032 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002033 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002034 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002035 if (w == Py_False) {
2036 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002037 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002038 }
2039 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002040 if (err > 0)
2041 err = 0;
2042 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00002043 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002044 else
2045 break;
2046 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002047
Raymond Hettingerf606f872003-03-16 03:11:04 +00002048 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002049 case JUMP_IF_TRUE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002050 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002051 if (w == Py_False) {
2052 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002053 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002054 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002055 if (w == Py_True) {
2056 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002057 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002058 }
2059 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002060 if (err > 0) {
2061 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00002062 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002063 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002064 else if (err == 0)
2065 ;
2066 else
2067 break;
2068 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002069
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00002070 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002071 case JUMP_ABSOLUTE:
2072 JUMPTO(oparg);
Neil Schemenauerca2a2f12003-05-30 23:59:44 +00002073 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002074
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002075 case GET_ITER:
2076 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002077 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002078 x = PyObject_GetIter(v);
2079 Py_DECREF(v);
2080 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002081 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002082 PREDICT(FOR_ITER);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002083 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002084 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002085 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002086 break;
2087
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002088 PREDICTED_WITH_ARG(FOR_ITER);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002089 case FOR_ITER:
2090 /* before: [iter]; after: [iter, iter()] *or* [] */
2091 v = TOP();
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002092 x = (*v->ob_type->tp_iternext)(v);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002093 if (x != NULL) {
2094 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002095 PREDICT(STORE_FAST);
2096 PREDICT(UNPACK_SEQUENCE);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002097 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002098 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002099 if (PyErr_Occurred()) {
2100 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2101 break;
2102 PyErr_Clear();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002103 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002104 /* iterator ended normally */
2105 x = v = POP();
2106 Py_DECREF(v);
2107 JUMPBY(oparg);
2108 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002109
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002110 case BREAK_LOOP:
2111 why = WHY_BREAK;
2112 goto fast_block_end;
2113
2114 case CONTINUE_LOOP:
2115 retval = PyInt_FromLong(oparg);
2116 why = WHY_CONTINUE;
2117 goto fast_block_end;
2118
Guido van Rossum374a9221991-04-04 10:40:29 +00002119 case SETUP_LOOP:
2120 case SETUP_EXCEPT:
2121 case SETUP_FINALLY:
Guido van Rossumb209a111997-04-29 18:18:01 +00002122 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002123 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002124 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002125
Guido van Rossumf10570b1995-07-07 22:53:21 +00002126 case CALL_FUNCTION:
Jeremy Hylton985eba52003-02-05 23:13:00 +00002127 PCALL(PCALL_ALL);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002128 x = call_function(&stack_pointer, oparg);
2129 PUSH(x);
2130 if (x != NULL)
2131 continue;
2132 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002133
Jeremy Hylton76901512000-03-28 23:49:17 +00002134 case CALL_FUNCTION_VAR:
2135 case CALL_FUNCTION_KW:
2136 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002137 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002138 int na = oparg & 0xff;
2139 int nk = (oparg>>8) & 0xff;
2140 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002141 int n = na + 2 * nk;
2142 PyObject **pfunc, *func;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002143 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002144 if (flags & CALL_FLAG_VAR)
2145 n++;
2146 if (flags & CALL_FLAG_KW)
2147 n++;
2148 pfunc = stack_pointer - n - 1;
2149 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002150
Guido van Rossumac7be682001-01-17 15:42:30 +00002151 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002152 && PyMethod_GET_SELF(func) != NULL) {
2153 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002154 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002155 func = PyMethod_GET_FUNCTION(func);
2156 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002157 Py_DECREF(*pfunc);
2158 *pfunc = self;
2159 na++;
2160 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002161 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002162 Py_INCREF(func);
2163 x = ext_do_call(func, &stack_pointer, flags, na, nk);
Jeremy Hylton76901512000-03-28 23:49:17 +00002164 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002165
Jeremy Hylton76901512000-03-28 23:49:17 +00002166 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002167 w = POP();
2168 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002169 }
2170 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002171 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002172 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002173 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002174 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002175
Guido van Rossum681d79a1995-07-18 14:51:37 +00002176 case MAKE_FUNCTION:
2177 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002178 x = PyFunction_New(v, f->f_globals);
2179 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002180 /* XXX Maybe this should be a separate opcode? */
2181 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002182 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002183 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002184 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002185 x = NULL;
2186 break;
2187 }
Raymond Hettingerd3b836d2004-04-07 13:17:27 +00002188 while (oparg--) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002189 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002190 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002191 }
2192 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002193 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002194 }
2195 PUSH(x);
2196 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002197
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002198 case MAKE_CLOSURE:
2199 {
2200 int nfree;
2201 v = POP(); /* code object */
2202 x = PyFunction_New(v, f->f_globals);
Jeremy Hylton733c8932001-12-13 19:51:56 +00002203 nfree = PyCode_GetNumFree((PyCodeObject *)v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002204 Py_DECREF(v);
2205 /* XXX Maybe this should be a separate opcode? */
2206 if (x != NULL && nfree > 0) {
2207 v = PyTuple_New(nfree);
2208 if (v == NULL) {
2209 Py_DECREF(x);
2210 x = NULL;
2211 break;
2212 }
Raymond Hettingerd3b836d2004-04-07 13:17:27 +00002213 while (nfree--) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002214 w = POP();
2215 PyTuple_SET_ITEM(v, nfree, w);
2216 }
2217 err = PyFunction_SetClosure(x, v);
2218 Py_DECREF(v);
2219 }
2220 if (x != NULL && oparg > 0) {
2221 v = PyTuple_New(oparg);
2222 if (v == NULL) {
2223 Py_DECREF(x);
2224 x = NULL;
2225 break;
2226 }
Raymond Hettingerd3b836d2004-04-07 13:17:27 +00002227 while (oparg--) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002228 w = POP();
2229 PyTuple_SET_ITEM(v, oparg, w);
2230 }
2231 err = PyFunction_SetDefaults(x, v);
2232 Py_DECREF(v);
2233 }
2234 PUSH(x);
2235 break;
2236 }
2237
Guido van Rossum8861b741996-07-30 16:49:37 +00002238 case BUILD_SLICE:
2239 if (oparg == 3)
2240 w = POP();
2241 else
2242 w = NULL;
2243 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002244 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002245 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002246 Py_DECREF(u);
2247 Py_DECREF(v);
2248 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002249 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002250 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002251 break;
2252
Fred Drakeef8ace32000-08-24 00:32:09 +00002253 case EXTENDED_ARG:
2254 opcode = NEXTOP();
Raymond Hettingerd3b836d2004-04-07 13:17:27 +00002255 oparg = oparg<<16 | OPARG();
2256 SKIPARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002257 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002258
Guido van Rossum374a9221991-04-04 10:40:29 +00002259 default:
2260 fprintf(stderr,
2261 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002262 PyCode_Addr2Line(f->f_code, f->f_lasti),
2263 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002264 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002265 why = WHY_EXCEPTION;
2266 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002267
2268#ifdef CASE_TOO_BIG
2269 }
2270#endif
2271
Guido van Rossum374a9221991-04-04 10:40:29 +00002272 } /* switch */
2273
2274 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002275
Guido van Rossum374a9221991-04-04 10:40:29 +00002276 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002277
Guido van Rossum374a9221991-04-04 10:40:29 +00002278 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002279 if (err == 0 && x != NULL) {
2280#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002281 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002282 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002283 fprintf(stderr,
2284 "XXX undetected error\n");
2285 else
2286#endif
2287 continue; /* Normal, fast path */
2288 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002289 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002290 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002291 err = 0;
2292 }
2293
Guido van Rossum374a9221991-04-04 10:40:29 +00002294 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002295
Raymond Hettinger06032cb2004-04-06 09:37:35 +00002296 if (why & (WHY_EXCEPTION | WHY_RERAISE)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002297 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002298 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002299 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002300 why = WHY_EXCEPTION;
2301 }
2302 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002303#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002304 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002305 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002306 if (PyErr_Occurred()) {
Jeremy Hylton904ed862003-11-05 17:29:35 +00002307 char buf[1024];
2308 sprintf(buf, "Stack unwind with exception "
2309 "set and why=%d", why);
2310 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002311 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002312 }
2313#endif
2314
2315 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002316
Guido van Rossum374a9221991-04-04 10:40:29 +00002317 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002318 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002319
Fred Drake8f51f542001-10-04 14:48:42 +00002320 if (tstate->c_tracefunc != NULL)
2321 call_exc_trace(tstate->c_tracefunc,
2322 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002323 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002324
Guido van Rossum374a9221991-04-04 10:40:29 +00002325 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002326
Guido van Rossum374a9221991-04-04 10:40:29 +00002327 if (why == WHY_RERAISE)
2328 why = WHY_EXCEPTION;
2329
2330 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002331
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002332fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002333 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002334 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002335
Tim Peters8a5c3c72004-04-05 19:36:21 +00002336 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002337 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2338 /* For a continue inside a try block,
2339 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002340 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2341 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002342 why = WHY_NOT;
2343 JUMPTO(PyInt_AS_LONG(retval));
2344 Py_DECREF(retval);
2345 break;
2346 }
2347
Guido van Rossum374a9221991-04-04 10:40:29 +00002348 while (STACK_LEVEL() > b->b_level) {
2349 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002350 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002351 }
2352 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2353 why = WHY_NOT;
2354 JUMPTO(b->b_handler);
2355 break;
2356 }
2357 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002358 (b->b_type == SETUP_EXCEPT &&
2359 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002360 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002361 PyObject *exc, *val, *tb;
2362 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002363 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002364 val = Py_None;
2365 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002366 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002367 /* Make the raw exception data
2368 available to the handler,
2369 so a program can emulate the
2370 Python main loop. Don't do
2371 this for 'finally'. */
2372 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002373 PyErr_NormalizeException(
2374 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002375 set_exc_info(tstate,
2376 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002377 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002378 if (tb == NULL) {
2379 Py_INCREF(Py_None);
2380 PUSH(Py_None);
2381 } else
2382 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002383 PUSH(val);
2384 PUSH(exc);
2385 }
2386 else {
Raymond Hettinger06032cb2004-04-06 09:37:35 +00002387 if (why & (WHY_RETURN | WHY_CONTINUE))
Guido van Rossum374a9221991-04-04 10:40:29 +00002388 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002389 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002390 PUSH(v);
2391 }
2392 why = WHY_NOT;
2393 JUMPTO(b->b_handler);
2394 break;
2395 }
2396 } /* unwind stack */
2397
2398 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002399
Guido van Rossum374a9221991-04-04 10:40:29 +00002400 if (why != WHY_NOT)
2401 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002402
Guido van Rossum374a9221991-04-04 10:40:29 +00002403 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002404
Tim Peters8a5c3c72004-04-05 19:36:21 +00002405 assert(why != WHY_YIELD);
2406 /* Pop remaining stack entries. */
2407 while (!EMPTY()) {
2408 v = POP();
2409 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002410 }
2411
Tim Peters8a5c3c72004-04-05 19:36:21 +00002412 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002413 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002414
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002415fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002416 if (tstate->use_tracing) {
2417 if (tstate->c_tracefunc
Raymond Hettinger06032cb2004-04-06 09:37:35 +00002418 && (why & (WHY_RETURN | WHY_YIELD))) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002419 if (call_trace(tstate->c_tracefunc,
2420 tstate->c_traceobj, f,
2421 PyTrace_RETURN, retval)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002422 Py_XDECREF(retval);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002423 retval = NULL;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002424 why = WHY_EXCEPTION;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002425 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002426 }
Fred Drake8f51f542001-10-04 14:48:42 +00002427 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002428 if (why == WHY_EXCEPTION)
2429 call_trace_protected(tstate->c_profilefunc,
2430 tstate->c_profileobj, f,
2431 PyTrace_RETURN);
2432 else if (call_trace(tstate->c_profilefunc,
2433 tstate->c_profileobj, f,
2434 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002435 Py_XDECREF(retval);
2436 retval = NULL;
2437 why = WHY_EXCEPTION;
2438 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002439 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002440 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002441
Guido van Rossuma027efa1997-05-05 20:56:21 +00002442 reset_exc_info(tstate);
2443
Tim Peters5ca576e2001-06-18 22:08:13 +00002444 /* pop frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +00002445 exit_eval_frame:
2446 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002447 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002448
Guido van Rossum96a42c81992-01-12 02:29:51 +00002449 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002450}
2451
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002452/* this is gonna seem *real weird*, but if you put some other code between
2453 eval_frame() and PyEval_EvalCodeEx() you will need to adjust the test in
2454 the if statement in Misc/gdbinit:ppystack */
2455
Tim Peters6d6c1a32001-08-02 04:15:00 +00002456PyObject *
2457PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002458 PyObject **args, int argcount, PyObject **kws, int kwcount,
2459 PyObject **defs, int defcount, PyObject *closure)
2460{
2461 register PyFrameObject *f;
2462 register PyObject *retval = NULL;
2463 register PyObject **fastlocals, **freevars;
2464 PyThreadState *tstate = PyThreadState_GET();
2465 PyObject *x, *u;
2466
2467 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00002468 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002469 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002470 return NULL;
2471 }
2472
Jeremy Hylton985eba52003-02-05 23:13:00 +00002473 assert(globals != NULL);
2474 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002475 if (f == NULL)
2476 return NULL;
2477
2478 fastlocals = f->f_localsplus;
2479 freevars = f->f_localsplus + f->f_nlocals;
2480
2481 if (co->co_argcount > 0 ||
2482 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2483 int i;
2484 int n = argcount;
2485 PyObject *kwdict = NULL;
2486 if (co->co_flags & CO_VARKEYWORDS) {
2487 kwdict = PyDict_New();
2488 if (kwdict == NULL)
2489 goto fail;
2490 i = co->co_argcount;
2491 if (co->co_flags & CO_VARARGS)
2492 i++;
2493 SETLOCAL(i, kwdict);
2494 }
2495 if (argcount > co->co_argcount) {
2496 if (!(co->co_flags & CO_VARARGS)) {
2497 PyErr_Format(PyExc_TypeError,
2498 "%.200s() takes %s %d "
2499 "%sargument%s (%d given)",
2500 PyString_AsString(co->co_name),
2501 defcount ? "at most" : "exactly",
2502 co->co_argcount,
2503 kwcount ? "non-keyword " : "",
2504 co->co_argcount == 1 ? "" : "s",
2505 argcount);
2506 goto fail;
2507 }
2508 n = co->co_argcount;
2509 }
2510 for (i = 0; i < n; i++) {
2511 x = args[i];
2512 Py_INCREF(x);
2513 SETLOCAL(i, x);
2514 }
2515 if (co->co_flags & CO_VARARGS) {
2516 u = PyTuple_New(argcount - n);
2517 if (u == NULL)
2518 goto fail;
2519 SETLOCAL(co->co_argcount, u);
2520 for (i = n; i < argcount; i++) {
2521 x = args[i];
2522 Py_INCREF(x);
2523 PyTuple_SET_ITEM(u, i-n, x);
2524 }
2525 }
2526 for (i = 0; i < kwcount; i++) {
2527 PyObject *keyword = kws[2*i];
2528 PyObject *value = kws[2*i + 1];
2529 int j;
2530 if (keyword == NULL || !PyString_Check(keyword)) {
2531 PyErr_Format(PyExc_TypeError,
2532 "%.200s() keywords must be strings",
2533 PyString_AsString(co->co_name));
2534 goto fail;
2535 }
2536 /* XXX slow -- speed up using dictionary? */
2537 for (j = 0; j < co->co_argcount; j++) {
2538 PyObject *nm = PyTuple_GET_ITEM(
2539 co->co_varnames, j);
2540 int cmp = PyObject_RichCompareBool(
2541 keyword, nm, Py_EQ);
2542 if (cmp > 0)
2543 break;
2544 else if (cmp < 0)
2545 goto fail;
2546 }
2547 /* Check errors from Compare */
2548 if (PyErr_Occurred())
2549 goto fail;
2550 if (j >= co->co_argcount) {
2551 if (kwdict == NULL) {
2552 PyErr_Format(PyExc_TypeError,
2553 "%.200s() got an unexpected "
2554 "keyword argument '%.400s'",
2555 PyString_AsString(co->co_name),
2556 PyString_AsString(keyword));
2557 goto fail;
2558 }
2559 PyDict_SetItem(kwdict, keyword, value);
2560 }
2561 else {
2562 if (GETLOCAL(j) != NULL) {
2563 PyErr_Format(PyExc_TypeError,
2564 "%.200s() got multiple "
2565 "values for keyword "
2566 "argument '%.400s'",
2567 PyString_AsString(co->co_name),
2568 PyString_AsString(keyword));
2569 goto fail;
2570 }
2571 Py_INCREF(value);
2572 SETLOCAL(j, value);
2573 }
2574 }
2575 if (argcount < co->co_argcount) {
2576 int m = co->co_argcount - defcount;
2577 for (i = argcount; i < m; i++) {
2578 if (GETLOCAL(i) == NULL) {
2579 PyErr_Format(PyExc_TypeError,
2580 "%.200s() takes %s %d "
2581 "%sargument%s (%d given)",
2582 PyString_AsString(co->co_name),
2583 ((co->co_flags & CO_VARARGS) ||
2584 defcount) ? "at least"
2585 : "exactly",
2586 m, kwcount ? "non-keyword " : "",
2587 m == 1 ? "" : "s", i);
2588 goto fail;
2589 }
2590 }
2591 if (n > m)
2592 i = n - m;
2593 else
2594 i = 0;
2595 for (; i < defcount; i++) {
2596 if (GETLOCAL(m+i) == NULL) {
2597 PyObject *def = defs[i];
2598 Py_INCREF(def);
2599 SETLOCAL(m+i, def);
2600 }
2601 }
2602 }
2603 }
2604 else {
2605 if (argcount > 0 || kwcount > 0) {
2606 PyErr_Format(PyExc_TypeError,
2607 "%.200s() takes no arguments (%d given)",
2608 PyString_AsString(co->co_name),
2609 argcount + kwcount);
2610 goto fail;
2611 }
2612 }
2613 /* Allocate and initialize storage for cell vars, and copy free
2614 vars into frame. This isn't too efficient right now. */
2615 if (f->f_ncells) {
2616 int i = 0, j = 0, nargs, found;
2617 char *cellname, *argname;
2618 PyObject *c;
2619
2620 nargs = co->co_argcount;
2621 if (co->co_flags & CO_VARARGS)
2622 nargs++;
2623 if (co->co_flags & CO_VARKEYWORDS)
2624 nargs++;
2625
2626 /* Check for cells that shadow args */
2627 for (i = 0; i < f->f_ncells && j < nargs; ++i) {
2628 cellname = PyString_AS_STRING(
2629 PyTuple_GET_ITEM(co->co_cellvars, i));
2630 found = 0;
2631 while (j < nargs) {
2632 argname = PyString_AS_STRING(
2633 PyTuple_GET_ITEM(co->co_varnames, j));
2634 if (strcmp(cellname, argname) == 0) {
2635 c = PyCell_New(GETLOCAL(j));
2636 if (c == NULL)
2637 goto fail;
2638 GETLOCAL(f->f_nlocals + i) = c;
2639 found = 1;
2640 break;
2641 }
2642 j++;
2643 }
2644 if (found == 0) {
2645 c = PyCell_New(NULL);
2646 if (c == NULL)
2647 goto fail;
2648 SETLOCAL(f->f_nlocals + i, c);
2649 }
2650 }
2651 /* Initialize any that are left */
2652 while (i < f->f_ncells) {
2653 c = PyCell_New(NULL);
2654 if (c == NULL)
2655 goto fail;
2656 SETLOCAL(f->f_nlocals + i, c);
2657 i++;
2658 }
2659 }
2660 if (f->f_nfreevars) {
2661 int i;
2662 for (i = 0; i < f->f_nfreevars; ++i) {
2663 PyObject *o = PyTuple_GET_ITEM(closure, i);
2664 Py_INCREF(o);
2665 freevars[f->f_ncells + i] = o;
2666 }
2667 }
2668
Tim Peters5ca576e2001-06-18 22:08:13 +00002669 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002670 /* Don't need to keep the reference to f_back, it will be set
2671 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002672 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002673 f->f_back = NULL;
2674
Jeremy Hylton985eba52003-02-05 23:13:00 +00002675 PCALL(PCALL_GENERATOR);
2676
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002677 /* Create a new generator that owns the ready to run frame
2678 * and return that as the value. */
Tim Peters5ca576e2001-06-18 22:08:13 +00002679 return gen_new(f);
2680 }
2681
2682 retval = eval_frame(f);
2683
2684 fail: /* Jump here from prelude on failure */
2685
Tim Petersb13680b2001-11-27 23:29:29 +00002686 /* decref'ing the frame can cause __del__ methods to get invoked,
2687 which can call back into Python. While we're done with the
2688 current Python frame (f), the associated C stack is still in use,
2689 so recursion_depth must be boosted for the duration.
2690 */
2691 assert(tstate != NULL);
2692 ++tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002693 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00002694 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002695 return retval;
2696}
2697
2698
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002699/* Implementation notes for set_exc_info() and reset_exc_info():
2700
2701- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2702 'exc_traceback'. These always travel together.
2703
2704- tstate->curexc_ZZZ is the "hot" exception that is set by
2705 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2706
2707- Once an exception is caught by an except clause, it is transferred
2708 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2709 can pick it up. This is the primary task of set_exc_info().
2710
2711- Now let me explain the complicated dance with frame->f_exc_ZZZ.
2712
2713 Long ago, when none of this existed, there were just a few globals:
2714 one set corresponding to the "hot" exception, and one set
2715 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2716 globals; they were simply stored as sys.exc_ZZZ. For backwards
2717 compatibility, they still are!) The problem was that in code like
2718 this:
2719
2720 try:
2721 "something that may fail"
2722 except "some exception":
2723 "do something else first"
2724 "print the exception from sys.exc_ZZZ."
2725
2726 if "do something else first" invoked something that raised and caught
2727 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2728 cause of subtle bugs. I fixed this by changing the semantics as
2729 follows:
2730
2731 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2732 *in that frame*.
2733
2734 - But initially, and as long as no exception is caught in a given
2735 frame, sys.exc_ZZZ will hold the last exception caught in the
2736 previous frame (or the frame before that, etc.).
2737
2738 The first bullet fixed the bug in the above example. The second
2739 bullet was for backwards compatibility: it was (and is) common to
2740 have a function that is called when an exception is caught, and to
2741 have that function access the caught exception via sys.exc_ZZZ.
2742 (Example: traceback.print_exc()).
2743
2744 At the same time I fixed the problem that sys.exc_ZZZ weren't
2745 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2746 but that's really a separate improvement.
2747
2748 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2749 variables to what they were before the current frame was called. The
2750 set_exc_info() function saves them on the frame so that
2751 reset_exc_info() can restore them. The invariant is that
2752 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2753 exception (where "catching" an exception applies only to successful
2754 except clauses); and if the current frame ever caught an exception,
2755 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2756 at the start of the current frame.
2757
2758*/
2759
Guido van Rossuma027efa1997-05-05 20:56:21 +00002760static void
Guido van Rossumac7be682001-01-17 15:42:30 +00002761set_exc_info(PyThreadState *tstate,
2762 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002763{
2764 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002765 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00002766
Guido van Rossuma027efa1997-05-05 20:56:21 +00002767 frame = tstate->frame;
2768 if (frame->f_exc_type == NULL) {
2769 /* This frame didn't catch an exception before */
2770 /* Save previous exception of this thread in this frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002771 if (tstate->exc_type == NULL) {
2772 Py_INCREF(Py_None);
2773 tstate->exc_type = Py_None;
2774 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002775 tmp_type = frame->f_exc_type;
2776 tmp_value = frame->f_exc_value;
2777 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002778 Py_XINCREF(tstate->exc_type);
2779 Py_XINCREF(tstate->exc_value);
2780 Py_XINCREF(tstate->exc_traceback);
2781 frame->f_exc_type = tstate->exc_type;
2782 frame->f_exc_value = tstate->exc_value;
2783 frame->f_exc_traceback = tstate->exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002784 Py_XDECREF(tmp_type);
2785 Py_XDECREF(tmp_value);
2786 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002787 }
2788 /* Set new exception for this thread */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002789 tmp_type = tstate->exc_type;
2790 tmp_value = tstate->exc_value;
2791 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002792 Py_XINCREF(type);
2793 Py_XINCREF(value);
2794 Py_XINCREF(tb);
2795 tstate->exc_type = type;
2796 tstate->exc_value = value;
2797 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002798 Py_XDECREF(tmp_type);
2799 Py_XDECREF(tmp_value);
2800 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002801 /* For b/w compatibility */
2802 PySys_SetObject("exc_type", type);
2803 PySys_SetObject("exc_value", value);
2804 PySys_SetObject("exc_traceback", tb);
2805}
2806
2807static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002808reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002809{
2810 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002811 PyObject *tmp_type, *tmp_value, *tmp_tb;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002812 frame = tstate->frame;
2813 if (frame->f_exc_type != NULL) {
2814 /* This frame caught an exception */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002815 tmp_type = tstate->exc_type;
2816 tmp_value = tstate->exc_value;
2817 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002818 Py_XINCREF(frame->f_exc_type);
2819 Py_XINCREF(frame->f_exc_value);
2820 Py_XINCREF(frame->f_exc_traceback);
2821 tstate->exc_type = frame->f_exc_type;
2822 tstate->exc_value = frame->f_exc_value;
2823 tstate->exc_traceback = frame->f_exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002824 Py_XDECREF(tmp_type);
2825 Py_XDECREF(tmp_value);
2826 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002827 /* For b/w compatibility */
2828 PySys_SetObject("exc_type", frame->f_exc_type);
2829 PySys_SetObject("exc_value", frame->f_exc_value);
2830 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
2831 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002832 tmp_type = frame->f_exc_type;
2833 tmp_value = frame->f_exc_value;
2834 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002835 frame->f_exc_type = NULL;
2836 frame->f_exc_value = NULL;
2837 frame->f_exc_traceback = NULL;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002838 Py_XDECREF(tmp_type);
2839 Py_XDECREF(tmp_value);
2840 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002841}
2842
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002843/* Logic for the raise statement (too complicated for inlining).
2844 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00002845static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002846do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002847{
Guido van Rossumd295f121998-04-09 21:39:57 +00002848 if (type == NULL) {
2849 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00002850 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumd295f121998-04-09 21:39:57 +00002851 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
2852 value = tstate->exc_value;
2853 tb = tstate->exc_traceback;
2854 Py_XINCREF(type);
2855 Py_XINCREF(value);
2856 Py_XINCREF(tb);
2857 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002858
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002859 /* We support the following forms of raise:
2860 raise <class>, <classinstance>
2861 raise <class>, <argument tuple>
2862 raise <class>, None
2863 raise <class>, <argument>
2864 raise <classinstance>, None
2865 raise <string>, <object>
2866 raise <string>, None
2867
2868 An omitted second argument is the same as None.
2869
2870 In addition, raise <tuple>, <anything> is the same as
2871 raising the tuple's first item (and it better have one!);
2872 this rule is applied recursively.
2873
2874 Finally, an optional third argument can be supplied, which
2875 gives the traceback to be substituted (useful when
2876 re-raising an exception after examining it). */
2877
2878 /* First, check the traceback argument, replacing None with
2879 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002880 if (tb == Py_None) {
2881 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002882 tb = NULL;
2883 }
2884 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002885 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002886 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002887 goto raise_error;
2888 }
2889
2890 /* Next, replace a missing value with None */
2891 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002892 value = Py_None;
2893 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002894 }
2895
2896 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00002897 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
2898 PyObject *tmp = type;
2899 type = PyTuple_GET_ITEM(type, 0);
2900 Py_INCREF(type);
2901 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002902 }
2903
Tim Petersafb2c802002-04-18 18:06:20 +00002904 if (PyString_CheckExact(type))
2905 /* Raising builtin string is deprecated but still allowed --
2906 * do nothing. Raising an instance of a new-style str
2907 * subclass is right out. */
Neal Norwitz37aa0662003-01-10 15:31:15 +00002908 PyErr_Warn(PyExc_PendingDeprecationWarning,
2909 "raising a string exception is deprecated");
Barry Warsaw4249f541997-08-22 21:26:19 +00002910
2911 else if (PyClass_Check(type))
2912 PyErr_NormalizeException(&type, &value, &tb);
2913
Guido van Rossumb209a111997-04-29 18:18:01 +00002914 else if (PyInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002915 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002916 if (value != Py_None) {
2917 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002918 "instance exception may not have a separate value");
2919 goto raise_error;
2920 }
2921 else {
2922 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00002923 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002924 value = type;
Guido van Rossumb209a111997-04-29 18:18:01 +00002925 type = (PyObject*) ((PyInstanceObject*)type)->in_class;
2926 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002927 }
2928 }
2929 else {
2930 /* Not something you can raise. You get an exception
2931 anyway, just not what you specified :-) */
Jeremy Hylton960d9482001-04-27 02:25:33 +00002932 PyErr_Format(PyExc_TypeError,
Neal Norwitz37aa0662003-01-10 15:31:15 +00002933 "exceptions must be classes, instances, or "
2934 "strings (deprecated), not %s",
2935 type->ob_type->tp_name);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002936 goto raise_error;
2937 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002938 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002939 if (tb == NULL)
2940 return WHY_EXCEPTION;
2941 else
2942 return WHY_RERAISE;
2943 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00002944 Py_XDECREF(value);
2945 Py_XDECREF(type);
2946 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002947 return WHY_EXCEPTION;
2948}
2949
Tim Petersd6d010b2001-06-21 02:49:55 +00002950/* Iterate v argcnt times and store the results on the stack (via decreasing
2951 sp). Return 1 for success, 0 if error. */
2952
Barry Warsawe42b18f1997-08-25 22:13:04 +00002953static int
Tim Petersd6d010b2001-06-21 02:49:55 +00002954unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00002955{
Tim Petersd6d010b2001-06-21 02:49:55 +00002956 int i = 0;
2957 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00002958 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00002959
Tim Petersd6d010b2001-06-21 02:49:55 +00002960 assert(v != NULL);
2961
2962 it = PyObject_GetIter(v);
2963 if (it == NULL)
2964 goto Error;
2965
2966 for (; i < argcnt; i++) {
2967 w = PyIter_Next(it);
2968 if (w == NULL) {
2969 /* Iterator done, via error or exhaustion. */
2970 if (!PyErr_Occurred()) {
2971 PyErr_Format(PyExc_ValueError,
2972 "need more than %d value%s to unpack",
2973 i, i == 1 ? "" : "s");
2974 }
2975 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00002976 }
2977 *--sp = w;
2978 }
Tim Petersd6d010b2001-06-21 02:49:55 +00002979
2980 /* We better have exhausted the iterator now. */
2981 w = PyIter_Next(it);
2982 if (w == NULL) {
2983 if (PyErr_Occurred())
2984 goto Error;
2985 Py_DECREF(it);
2986 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00002987 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00002988 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00002989 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00002990 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00002991Error:
Barry Warsaw91010551997-08-25 22:30:51 +00002992 for (; i > 0; i--, sp++)
2993 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00002994 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00002995 return 0;
2996}
2997
2998
Guido van Rossum96a42c81992-01-12 02:29:51 +00002999#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003000static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003001prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003002{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003003 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003004 if (PyObject_Print(v, stdout, 0) != 0)
3005 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003006 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003007 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003008}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003009#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003010
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003011static void
Fred Drake5755ce62001-06-27 19:19:46 +00003012call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003013{
Guido van Rossumb209a111997-04-29 18:18:01 +00003014 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003015 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003016 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003017 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003018 value = Py_None;
3019 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003020 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003021 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003022 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003023 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003024 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003025 }
Fred Drake5755ce62001-06-27 19:19:46 +00003026 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003027 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003028 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003029 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003030 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003031 Py_XDECREF(type);
3032 Py_XDECREF(value);
3033 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003034 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003035}
3036
Fred Drake4ec5d562001-10-04 19:26:43 +00003037static void
3038call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3039 int what)
3040{
3041 PyObject *type, *value, *traceback;
3042 int err;
3043 PyErr_Fetch(&type, &value, &traceback);
3044 err = call_trace(func, obj, frame, what, NULL);
3045 if (err == 0)
3046 PyErr_Restore(type, value, traceback);
3047 else {
3048 Py_XDECREF(type);
3049 Py_XDECREF(value);
3050 Py_XDECREF(traceback);
3051 }
3052}
3053
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003054static int
Fred Drake5755ce62001-06-27 19:19:46 +00003055call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3056 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003057{
Fred Drake5755ce62001-06-27 19:19:46 +00003058 register PyThreadState *tstate = frame->f_tstate;
3059 int result;
3060 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003061 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003062 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003063 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003064 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003065 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3066 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003067 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003068 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003069}
3070
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003071PyObject *
3072_PyEval_CallTracing(PyObject *func, PyObject *args)
3073{
3074 PyFrameObject *frame = PyEval_GetFrame();
3075 PyThreadState *tstate = frame->f_tstate;
3076 int save_tracing = tstate->tracing;
3077 int save_use_tracing = tstate->use_tracing;
3078 PyObject *result;
3079
3080 tstate->tracing = 0;
3081 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3082 || (tstate->c_profilefunc != NULL));
3083 result = PyObject_Call(func, args, NULL);
3084 tstate->tracing = save_tracing;
3085 tstate->use_tracing = save_use_tracing;
3086 return result;
3087}
3088
Michael W. Hudson006c7522002-11-08 13:08:46 +00003089static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003090maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003091 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3092 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003093{
3094 /* The theory of SET_LINENO-less tracing.
Tim Peters8a5c3c72004-04-05 19:36:21 +00003095
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003096 In a nutshell, we use the co_lnotab field of the code object
3097 to tell when execution has moved onto a different line.
3098
3099 As mentioned above, the basic idea is so set things up so
3100 that
3101
3102 *instr_lb <= frame->f_lasti < *instr_ub
3103
3104 is true so long as execution does not change lines.
3105
3106 This is all fairly simple. Digging the information out of
3107 co_lnotab takes some work, but is conceptually clear.
3108
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003109 Somewhat harder to explain is why we don't *always* call the
3110 line trace function when the above test fails.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003111
3112 Consider this code:
3113
3114 1: def f(a):
3115 2: if a:
3116 3: print 1
3117 4: else:
3118 5: print 2
3119
3120 which compiles to this:
3121
3122 2 0 LOAD_FAST 0 (a)
3123 3 JUMP_IF_FALSE 9 (to 15)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003124 6 POP_TOP
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003125
3126 3 7 LOAD_CONST 1 (1)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003127 10 PRINT_ITEM
3128 11 PRINT_NEWLINE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003129 12 JUMP_FORWARD 6 (to 21)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003130 >> 15 POP_TOP
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003131
3132 5 16 LOAD_CONST 2 (2)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003133 19 PRINT_ITEM
3134 20 PRINT_NEWLINE
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003135 >> 21 LOAD_CONST 0 (None)
3136 24 RETURN_VALUE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003137
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003138 If 'a' is false, execution will jump to instruction at offset
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003139 15 and the co_lnotab will claim that execution has moved to
3140 line 3. This is at best misleading. In this case we could
3141 associate the POP_TOP with line 4, but that doesn't make
3142 sense in all cases (I think).
3143
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003144 What we do is only call the line trace function if the co_lnotab
3145 indicates we have jumped to the *start* of a line, i.e. if the
3146 current instruction offset matches the offset given for the
3147 start of a line by the co_lnotab.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003148
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003149 This also takes care of the situation where 'a' is true.
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003150 Execution will jump from instruction offset 12 to offset 21.
3151 Then the co_lnotab would imply that execution has moved to line
3152 5, which is again misleading.
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003153
3154 Why do we set f_lineno when tracing? Well, consider the code
3155 above when 'a' is true. If stepping through this with 'n' in
3156 pdb, you would stop at line 1 with a "call" type event, then
3157 line events on lines 2 and 3, then a "return" type event -- but
3158 you would be shown line 5 during this event. This is a change
3159 from the behaviour in 2.2 and before, and I've found it
3160 confusing in practice. By setting and using f_lineno when
3161 tracing, one can report a line number different from that
3162 suggested by f_lasti on this one occasion where it's desirable.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003163 */
3164
Michael W. Hudson006c7522002-11-08 13:08:46 +00003165 int result = 0;
3166
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003167 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003168 PyCodeObject* co = frame->f_code;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003169 int size, addr, line;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003170 unsigned char* p;
3171
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003172 size = PyString_GET_SIZE(co->co_lnotab) / 2;
3173 p = (unsigned char*)PyString_AS_STRING(co->co_lnotab);
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003174
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003175 addr = 0;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003176 line = co->co_firstlineno;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003177
3178 /* possible optimization: if f->f_lasti == instr_ub
3179 (likely to be a common case) then we already know
3180 instr_lb -- if we stored the matching value of p
3181 somwhere we could skip the first while loop. */
3182
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003183 /* see comments in compile.c for the description of
3184 co_lnotab. A point to remember: increments to p
3185 should come in pairs -- although we don't care about
3186 the line increments here, treating them as byte
3187 increments gets confusing, to say the least. */
3188
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003189 while (size > 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003190 if (addr + *p > frame->f_lasti)
3191 break;
3192 addr += *p++;
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003193 if (*p) *instr_lb = addr;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003194 line += *p++;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003195 --size;
3196 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003197
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003198 if (addr == frame->f_lasti) {
3199 frame->f_lineno = line;
Tim Peters8a5c3c72004-04-05 19:36:21 +00003200 result = call_trace(func, obj, frame,
Michael W. Hudson006c7522002-11-08 13:08:46 +00003201 PyTrace_LINE, Py_None);
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003202 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003203
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003204 if (size > 0) {
Raymond Hettingerd3b836d2004-04-07 13:17:27 +00003205 while (size--) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003206 addr += *p++;
3207 if (*p++)
3208 break;
3209 }
3210 *instr_ub = addr;
3211 }
3212 else {
3213 *instr_ub = INT_MAX;
3214 }
3215 }
Armin Rigobf57a142004-03-22 19:24:58 +00003216 else if (frame->f_lasti <= *instr_prev) {
3217 /* jumping back in the same line forces a trace event */
Tim Peters8a5c3c72004-04-05 19:36:21 +00003218 result = call_trace(func, obj, frame,
Armin Rigobf57a142004-03-22 19:24:58 +00003219 PyTrace_LINE, Py_None);
3220 }
3221 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003222 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003223}
3224
Fred Drake5755ce62001-06-27 19:19:46 +00003225void
3226PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003227{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003228 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003229 PyObject *temp = tstate->c_profileobj;
3230 Py_XINCREF(arg);
3231 tstate->c_profilefunc = NULL;
3232 tstate->c_profileobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003233 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003234 Py_XDECREF(temp);
3235 tstate->c_profilefunc = func;
3236 tstate->c_profileobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003237 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003238}
3239
3240void
3241PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3242{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003243 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003244 PyObject *temp = tstate->c_traceobj;
3245 Py_XINCREF(arg);
3246 tstate->c_tracefunc = NULL;
3247 tstate->c_traceobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003248 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003249 Py_XDECREF(temp);
3250 tstate->c_tracefunc = func;
3251 tstate->c_traceobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003252 tstate->use_tracing = ((func != NULL)
3253 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003254}
3255
Guido van Rossumb209a111997-04-29 18:18:01 +00003256PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003257PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003258{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003259 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003260 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003261 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003262 else
3263 return current_frame->f_builtins;
3264}
3265
Guido van Rossumb209a111997-04-29 18:18:01 +00003266PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003267PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003268{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003269 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003270 if (current_frame == NULL)
3271 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003272 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003273 return current_frame->f_locals;
3274}
3275
Guido van Rossumb209a111997-04-29 18:18:01 +00003276PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003277PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003278{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003279 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003280 if (current_frame == NULL)
3281 return NULL;
3282 else
3283 return current_frame->f_globals;
3284}
3285
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003286PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003287PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003288{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003289 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003290 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003291}
3292
Guido van Rossum6135a871995-01-09 17:53:26 +00003293int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003294PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003295{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003296 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003297 return current_frame == NULL ? 0 : current_frame->f_restricted;
3298}
3299
Guido van Rossumbe270261997-05-22 22:26:18 +00003300int
Tim Peters5ba58662001-07-16 02:29:45 +00003301PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003302{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003303 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003304 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003305
3306 if (current_frame != NULL) {
3307 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003308 const int compilerflags = codeflags & PyCF_MASK;
3309 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003310 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003311 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003312 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003313#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003314 if (codeflags & CO_GENERATOR_ALLOWED) {
3315 result = 1;
3316 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3317 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003318#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003319 }
3320 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003321}
3322
3323int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003324Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003325{
Guido van Rossumb209a111997-04-29 18:18:01 +00003326 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003327 if (f == NULL)
3328 return 0;
3329 if (!PyFile_SoftSpace(f, 0))
3330 return 0;
3331 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003332}
3333
Guido van Rossum3f5da241990-12-20 15:06:42 +00003334
Guido van Rossum681d79a1995-07-18 14:51:37 +00003335/* External interface to call any callable object.
3336 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003337
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003338#undef PyEval_CallObject
3339/* for backward compatibility: export this interface */
3340
Guido van Rossumb209a111997-04-29 18:18:01 +00003341PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003342PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003343{
Guido van Rossumb209a111997-04-29 18:18:01 +00003344 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003345}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003346#define PyEval_CallObject(func,arg) \
3347 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003348
Guido van Rossumb209a111997-04-29 18:18:01 +00003349PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003350PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003351{
Jeremy Hylton52820442001-01-03 23:52:36 +00003352 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003353
3354 if (arg == NULL)
Guido van Rossumb209a111997-04-29 18:18:01 +00003355 arg = PyTuple_New(0);
3356 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003357 PyErr_SetString(PyExc_TypeError,
3358 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003359 return NULL;
3360 }
3361 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003362 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003363
Guido van Rossumb209a111997-04-29 18:18:01 +00003364 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003365 PyErr_SetString(PyExc_TypeError,
3366 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003367 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003368 return NULL;
3369 }
3370
Tim Peters6d6c1a32001-08-02 04:15:00 +00003371 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003372 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003373 return result;
3374}
3375
Tim Peters6d6c1a32001-08-02 04:15:00 +00003376char *
3377PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003378{
3379 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003380 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003381 else if (PyFunction_Check(func))
3382 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3383 else if (PyCFunction_Check(func))
3384 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3385 else if (PyClass_Check(func))
3386 return PyString_AsString(((PyClassObject*)func)->cl_name);
3387 else if (PyInstance_Check(func)) {
3388 return PyString_AsString(
3389 ((PyInstanceObject*)func)->in_class->cl_name);
3390 } else {
3391 return func->ob_type->tp_name;
3392 }
3393}
3394
Tim Peters6d6c1a32001-08-02 04:15:00 +00003395char *
3396PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003397{
3398 if (PyMethod_Check(func))
3399 return "()";
3400 else if (PyFunction_Check(func))
3401 return "()";
3402 else if (PyCFunction_Check(func))
3403 return "()";
3404 else if (PyClass_Check(func))
3405 return " constructor";
3406 else if (PyInstance_Check(func)) {
3407 return " instance";
3408 } else {
3409 return " object";
3410 }
3411}
3412
Jeremy Hylton52820442001-01-03 23:52:36 +00003413#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
3414
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003415static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003416err_args(PyObject *func, int flags, int nargs)
3417{
3418 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003419 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003420 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003421 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003422 nargs);
3423 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003424 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003425 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003426 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003427 nargs);
3428}
3429
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003430#define BEGIN_C_TRACE \
3431if (tstate->use_tracing) { \
3432 if (tstate->c_profilefunc != NULL) { \
3433 PyObject *func_name = \
3434 PyString_FromString (((PyCFunctionObject *) \
3435 func)->m_ml->ml_name); \
3436 are_tracing = 1; \
3437 if (call_trace(tstate->c_profilefunc, \
3438 tstate->c_profileobj, \
3439 tstate->frame, PyTrace_C_CALL, \
3440 func_name)) \
3441 { return NULL; } \
3442 Py_DECREF (func_name); \
3443 } \
3444 }
3445
3446#define END_C_TRACE \
3447 if (tstate->use_tracing && are_tracing) { \
3448 if (tstate->c_profilefunc != NULL) { \
3449 if (x == NULL) { \
3450 if (call_trace (tstate->c_profilefunc, \
3451 tstate->c_profileobj, \
3452 tstate->frame, PyTrace_C_EXCEPTION, \
3453 NULL)) \
3454 { return NULL; } \
3455 } else { \
3456 if (call_trace(tstate->c_profilefunc, \
3457 tstate->c_profileobj, \
3458 tstate->frame, PyTrace_C_RETURN, \
3459 NULL)) \
3460 { return NULL; } \
3461 } \
3462 } \
3463 }
3464
3465
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003466static PyObject *
3467call_function(PyObject ***pp_stack, int oparg)
3468{
3469 int na = oparg & 0xff;
3470 int nk = (oparg>>8) & 0xff;
3471 int n = na + 2 * nk;
3472 PyObject **pfunc = (*pp_stack) - n - 1;
3473 PyObject *func = *pfunc;
3474 PyObject *x, *w;
3475
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003476 int are_tracing = 0;
3477
3478 PyThreadState *tstate = PyThreadState_GET();
3479
Jeremy Hylton985eba52003-02-05 23:13:00 +00003480 /* Always dispatch PyCFunction first, because these are
3481 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003482 */
3483 if (PyCFunction_Check(func) && nk == 0) {
3484 int flags = PyCFunction_GET_FLAGS(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003485 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003486 if (flags & (METH_NOARGS | METH_O)) {
3487 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3488 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003489 if (flags & METH_NOARGS && na == 0) {
3490 BEGIN_C_TRACE
Jeremy Hylton192690e2002-08-16 18:36:11 +00003491 x = (*meth)(self, NULL);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003492 END_C_TRACE
3493 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003494 else if (flags & METH_O && na == 1) {
3495 PyObject *arg = EXT_POP(*pp_stack);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003496 BEGIN_C_TRACE
Jeremy Hylton192690e2002-08-16 18:36:11 +00003497 x = (*meth)(self, arg);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003498 END_C_TRACE
Jeremy Hylton192690e2002-08-16 18:36:11 +00003499 Py_DECREF(arg);
3500 }
3501 else {
3502 err_args(func, flags, na);
3503 x = NULL;
3504 }
3505 }
3506 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003507 PyObject *callargs;
3508 callargs = load_args(pp_stack, na);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003509 BEGIN_C_TRACE
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003510 x = PyCFunction_Call(func, callargs, NULL);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003511 END_C_TRACE
Tim Peters8a5c3c72004-04-05 19:36:21 +00003512 Py_XDECREF(callargs);
3513 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003514 } else {
3515 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3516 /* optimize access to bound methods */
3517 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003518 PCALL(PCALL_METHOD);
3519 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003520 Py_INCREF(self);
3521 func = PyMethod_GET_FUNCTION(func);
3522 Py_INCREF(func);
3523 Py_DECREF(*pfunc);
3524 *pfunc = self;
3525 na++;
3526 n++;
3527 } else
3528 Py_INCREF(func);
3529 if (PyFunction_Check(func))
3530 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003531 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003532 x = do_call(func, pp_stack, na, nk);
3533 Py_DECREF(func);
3534 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003535
Jeremy Hylton985eba52003-02-05 23:13:00 +00003536 /* What does this do? */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003537 while ((*pp_stack) > pfunc) {
3538 w = EXT_POP(*pp_stack);
3539 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003540 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003541 }
3542 return x;
3543}
3544
Jeremy Hylton192690e2002-08-16 18:36:11 +00003545/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003546 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003547 For the simplest case -- a function that takes only positional
3548 arguments and is called with only positional arguments -- it
3549 inlines the most primitive frame setup code from
3550 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3551 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003552*/
3553
3554static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003555fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003556{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003557 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003558 PyObject *globals = PyFunction_GET_GLOBALS(func);
3559 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3560 PyObject **d = NULL;
3561 int nd = 0;
3562
Jeremy Hylton985eba52003-02-05 23:13:00 +00003563 PCALL(PCALL_FUNCTION);
3564 PCALL(PCALL_FAST_FUNCTION);
Raymond Hettinger40174c32003-05-31 07:04:16 +00003565 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003566 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3567 PyFrameObject *f;
3568 PyObject *retval = NULL;
3569 PyThreadState *tstate = PyThreadState_GET();
3570 PyObject **fastlocals, **stack;
3571 int i;
3572
3573 PCALL(PCALL_FASTER_FUNCTION);
3574 assert(globals != NULL);
3575 /* XXX Perhaps we should create a specialized
3576 PyFrame_New() that doesn't take locals, but does
3577 take builtins without sanity checking them.
3578 */
3579 f = PyFrame_New(tstate, co, globals, NULL);
3580 if (f == NULL)
3581 return NULL;
3582
3583 fastlocals = f->f_localsplus;
3584 stack = (*pp_stack) - n;
3585
3586 for (i = 0; i < n; i++) {
3587 Py_INCREF(*stack);
3588 fastlocals[i] = *stack++;
3589 }
3590 retval = eval_frame(f);
3591 assert(tstate != NULL);
3592 ++tstate->recursion_depth;
3593 Py_DECREF(f);
3594 --tstate->recursion_depth;
3595 return retval;
3596 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003597 if (argdefs != NULL) {
3598 d = &PyTuple_GET_ITEM(argdefs, 0);
3599 nd = ((PyTupleObject *)argdefs)->ob_size;
3600 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003601 return PyEval_EvalCodeEx(co, globals,
3602 (PyObject *)NULL, (*pp_stack)-n, na,
3603 (*pp_stack)-2*nk, nk, d, nd,
3604 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003605}
3606
3607static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003608update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3609 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003610{
3611 PyObject *kwdict = NULL;
3612 if (orig_kwdict == NULL)
3613 kwdict = PyDict_New();
3614 else {
3615 kwdict = PyDict_Copy(orig_kwdict);
3616 Py_DECREF(orig_kwdict);
3617 }
3618 if (kwdict == NULL)
3619 return NULL;
Raymond Hettingerd3b836d2004-04-07 13:17:27 +00003620 while (nk--) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003621 int err;
3622 PyObject *value = EXT_POP(*pp_stack);
3623 PyObject *key = EXT_POP(*pp_stack);
3624 if (PyDict_GetItem(kwdict, key) != NULL) {
Guido van Rossumac7be682001-01-17 15:42:30 +00003625 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +00003626 "%.200s%s got multiple values "
Jeremy Hylton512a2372001-04-11 13:52:29 +00003627 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003628 PyEval_GetFuncName(func),
3629 PyEval_GetFuncDesc(func),
Jeremy Hylton512a2372001-04-11 13:52:29 +00003630 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003631 Py_DECREF(key);
3632 Py_DECREF(value);
3633 Py_DECREF(kwdict);
3634 return NULL;
3635 }
3636 err = PyDict_SetItem(kwdict, key, value);
3637 Py_DECREF(key);
3638 Py_DECREF(value);
3639 if (err) {
3640 Py_DECREF(kwdict);
3641 return NULL;
3642 }
3643 }
3644 return kwdict;
3645}
3646
3647static PyObject *
3648update_star_args(int nstack, int nstar, PyObject *stararg,
3649 PyObject ***pp_stack)
3650{
3651 PyObject *callargs, *w;
3652
3653 callargs = PyTuple_New(nstack + nstar);
3654 if (callargs == NULL) {
3655 return NULL;
3656 }
3657 if (nstar) {
3658 int i;
3659 for (i = 0; i < nstar; i++) {
3660 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3661 Py_INCREF(a);
3662 PyTuple_SET_ITEM(callargs, nstack + i, a);
3663 }
3664 }
Raymond Hettingerd3b836d2004-04-07 13:17:27 +00003665 while (nstack--) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003666 w = EXT_POP(*pp_stack);
3667 PyTuple_SET_ITEM(callargs, nstack, w);
3668 }
3669 return callargs;
3670}
3671
3672static PyObject *
3673load_args(PyObject ***pp_stack, int na)
3674{
3675 PyObject *args = PyTuple_New(na);
3676 PyObject *w;
3677
3678 if (args == NULL)
3679 return NULL;
Raymond Hettingerd3b836d2004-04-07 13:17:27 +00003680 while (na--) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003681 w = EXT_POP(*pp_stack);
3682 PyTuple_SET_ITEM(args, na, w);
3683 }
3684 return args;
3685}
3686
3687static PyObject *
3688do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3689{
3690 PyObject *callargs = NULL;
3691 PyObject *kwdict = NULL;
3692 PyObject *result = NULL;
3693
3694 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003695 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003696 if (kwdict == NULL)
3697 goto call_fail;
3698 }
3699 callargs = load_args(pp_stack, na);
3700 if (callargs == NULL)
3701 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003702#ifdef CALL_PROFILE
3703 /* At this point, we have to look at the type of func to
3704 update the call stats properly. Do it here so as to avoid
3705 exposing the call stats machinery outside ceval.c
3706 */
3707 if (PyFunction_Check(func))
3708 PCALL(PCALL_FUNCTION);
3709 else if (PyMethod_Check(func))
3710 PCALL(PCALL_METHOD);
3711 else if (PyType_Check(func))
3712 PCALL(PCALL_TYPE);
3713 else
3714 PCALL(PCALL_OTHER);
3715#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003716 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003717 call_fail:
3718 Py_XDECREF(callargs);
3719 Py_XDECREF(kwdict);
3720 return result;
3721}
3722
3723static PyObject *
3724ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3725{
3726 int nstar = 0;
3727 PyObject *callargs = NULL;
3728 PyObject *stararg = NULL;
3729 PyObject *kwdict = NULL;
3730 PyObject *result = NULL;
3731
3732 if (flags & CALL_FLAG_KW) {
3733 kwdict = EXT_POP(*pp_stack);
3734 if (!(kwdict && PyDict_Check(kwdict))) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003735 PyErr_Format(PyExc_TypeError,
Jeremy Hylton512a2372001-04-11 13:52:29 +00003736 "%s%s argument after ** "
3737 "must be a dictionary",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003738 PyEval_GetFuncName(func),
3739 PyEval_GetFuncDesc(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003740 goto ext_call_fail;
3741 }
3742 }
3743 if (flags & CALL_FLAG_VAR) {
3744 stararg = EXT_POP(*pp_stack);
3745 if (!PyTuple_Check(stararg)) {
3746 PyObject *t = NULL;
3747 t = PySequence_Tuple(stararg);
3748 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003749 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3750 PyErr_Format(PyExc_TypeError,
3751 "%s%s argument after * "
3752 "must be a sequence",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003753 PyEval_GetFuncName(func),
3754 PyEval_GetFuncDesc(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003755 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003756 goto ext_call_fail;
3757 }
3758 Py_DECREF(stararg);
3759 stararg = t;
3760 }
3761 nstar = PyTuple_GET_SIZE(stararg);
3762 }
3763 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003764 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003765 if (kwdict == NULL)
3766 goto ext_call_fail;
3767 }
3768 callargs = update_star_args(na, nstar, stararg, pp_stack);
3769 if (callargs == NULL)
3770 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003771#ifdef CALL_PROFILE
3772 /* At this point, we have to look at the type of func to
3773 update the call stats properly. Do it here so as to avoid
3774 exposing the call stats machinery outside ceval.c
3775 */
3776 if (PyFunction_Check(func))
3777 PCALL(PCALL_FUNCTION);
3778 else if (PyMethod_Check(func))
3779 PCALL(PCALL_METHOD);
3780 else if (PyType_Check(func))
3781 PCALL(PCALL_TYPE);
3782 else
3783 PCALL(PCALL_OTHER);
3784#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003785 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003786 ext_call_fail:
3787 Py_XDECREF(callargs);
3788 Py_XDECREF(kwdict);
3789 Py_XDECREF(stararg);
3790 return result;
3791}
3792
Guido van Rossum3b9c6671996-07-30 18:40:29 +00003793#define SLICE_ERROR_MSG \
3794 "standard sequence type does not support step size other than one"
3795
Tim Peterscb479e72001-12-16 19:11:44 +00003796/* Extract a slice index from a PyInt or PyLong, and store in *pi.
3797 Silently reduce values larger than INT_MAX to INT_MAX, and silently
3798 boost values less than -INT_MAX to 0. Return 0 on error, 1 on success.
3799*/
Tim Petersb5196382001-12-16 19:44:20 +00003800/* Note: If v is NULL, return success without storing into *pi. This
3801 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3802 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00003803*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00003804int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003805_PyEval_SliceIndex(PyObject *v, int *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003806{
Tim Petersb5196382001-12-16 19:44:20 +00003807 if (v != NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003808 long x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003809 if (PyInt_Check(v)) {
3810 x = PyInt_AsLong(v);
3811 } else if (PyLong_Check(v)) {
3812 x = PyLong_AsLong(v);
3813 if (x==-1 && PyErr_Occurred()) {
3814 PyObject *long_zero;
Guido van Rossumac7be682001-01-17 15:42:30 +00003815 int cmp;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003816
Guido van Rossumac7be682001-01-17 15:42:30 +00003817 if (!PyErr_ExceptionMatches(
3818 PyExc_OverflowError)) {
3819 /* It's not an overflow error, so just
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003820 signal an error */
Guido van Rossum20c6add2000-05-08 14:06:50 +00003821 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003822 }
3823
Guido van Rossumac7be682001-01-17 15:42:30 +00003824 /* Clear the OverflowError */
3825 PyErr_Clear();
3826
3827 /* It's an overflow error, so we need to
3828 check the sign of the long integer,
Tim Peters8a5c3c72004-04-05 19:36:21 +00003829 set the value to INT_MAX or -INT_MAX,
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003830 and clear the error. */
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003831
3832 /* Create a long integer with a value of 0 */
Guido van Rossumac7be682001-01-17 15:42:30 +00003833 long_zero = PyLong_FromLong(0L);
Tim Peterscb479e72001-12-16 19:11:44 +00003834 if (long_zero == NULL)
3835 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003836
3837 /* Check sign */
Guido van Rossumac7be682001-01-17 15:42:30 +00003838 cmp = PyObject_RichCompareBool(v, long_zero,
3839 Py_GT);
3840 Py_DECREF(long_zero);
3841 if (cmp < 0)
3842 return 0;
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003843 else if (cmp)
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003844 x = INT_MAX;
3845 else
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003846 x = -INT_MAX;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003847 }
3848 } else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003849 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003850 "slice indices must be integers");
Guido van Rossum20c6add2000-05-08 14:06:50 +00003851 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003852 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003853 /* Truncate -- very long indices are truncated anyway */
3854 if (x > INT_MAX)
3855 x = INT_MAX;
3856 else if (x < -INT_MAX)
Michael W. Hudsoncbd6fb92002-11-06 15:17:32 +00003857 x = -INT_MAX;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003858 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003859 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00003860 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003861}
3862
Guido van Rossum50d756e2001-08-18 17:43:36 +00003863#undef ISINT
3864#define ISINT(x) ((x) == NULL || PyInt_Check(x) || PyLong_Check(x))
3865
Guido van Rossumb209a111997-04-29 18:18:01 +00003866static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003867apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003868{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003869 PyTypeObject *tp = u->ob_type;
3870 PySequenceMethods *sq = tp->tp_as_sequence;
3871
3872 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3873 int ilow = 0, ihigh = INT_MAX;
3874 if (!_PyEval_SliceIndex(v, &ilow))
3875 return NULL;
3876 if (!_PyEval_SliceIndex(w, &ihigh))
3877 return NULL;
3878 return PySequence_GetSlice(u, ilow, ihigh);
3879 }
3880 else {
3881 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00003882 if (slice != NULL) {
3883 PyObject *res = PyObject_GetItem(u, slice);
3884 Py_DECREF(slice);
3885 return res;
3886 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00003887 else
3888 return NULL;
3889 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003890}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003891
3892static int
Guido van Rossumac7be682001-01-17 15:42:30 +00003893assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
3894 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003895{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003896 PyTypeObject *tp = u->ob_type;
3897 PySequenceMethods *sq = tp->tp_as_sequence;
3898
3899 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3900 int ilow = 0, ihigh = INT_MAX;
3901 if (!_PyEval_SliceIndex(v, &ilow))
3902 return -1;
3903 if (!_PyEval_SliceIndex(w, &ihigh))
3904 return -1;
3905 if (x == NULL)
3906 return PySequence_DelSlice(u, ilow, ihigh);
3907 else
3908 return PySequence_SetSlice(u, ilow, ihigh, x);
3909 }
3910 else {
3911 PyObject *slice = PySlice_New(v, w, NULL);
3912 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00003913 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003914 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00003915 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00003916 else
Guido van Rossum354797c2001-12-03 19:45:06 +00003917 res = PyObject_DelItem(u, slice);
3918 Py_DECREF(slice);
3919 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003920 }
3921 else
3922 return -1;
3923 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003924}
3925
Guido van Rossumb209a111997-04-29 18:18:01 +00003926static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003927cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003928{
Guido van Rossumac7be682001-01-17 15:42:30 +00003929 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003930 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00003931 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003932 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003933 break;
3934 case PyCmp_IS_NOT:
3935 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003936 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003937 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003938 res = PySequence_Contains(w, v);
3939 if (res < 0)
3940 return NULL;
3941 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003942 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00003943 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003944 if (res < 0)
3945 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003946 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003947 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003948 case PyCmp_EXC_MATCH:
Barry Warsaw4249f541997-08-22 21:26:19 +00003949 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003950 break;
3951 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00003952 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003953 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003954 v = res ? Py_True : Py_False;
3955 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003956 return v;
3957}
3958
Thomas Wouters52152252000-08-17 22:55:00 +00003959static PyObject *
3960import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003961{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003962 PyObject *x;
3963
3964 x = PyObject_GetAttr(v, name);
3965 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00003966 PyErr_Format(PyExc_ImportError,
3967 "cannot import name %.230s",
3968 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003969 }
Thomas Wouters52152252000-08-17 22:55:00 +00003970 return x;
3971}
Guido van Rossumac7be682001-01-17 15:42:30 +00003972
Thomas Wouters52152252000-08-17 22:55:00 +00003973static int
3974import_all_from(PyObject *locals, PyObject *v)
3975{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003976 PyObject *all = PyObject_GetAttrString(v, "__all__");
3977 PyObject *dict, *name, *value;
3978 int skip_leading_underscores = 0;
3979 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00003980
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003981 if (all == NULL) {
3982 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3983 return -1; /* Unexpected error */
3984 PyErr_Clear();
3985 dict = PyObject_GetAttrString(v, "__dict__");
3986 if (dict == NULL) {
3987 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3988 return -1;
3989 PyErr_SetString(PyExc_ImportError,
3990 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00003991 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003992 }
3993 all = PyMapping_Keys(dict);
3994 Py_DECREF(dict);
3995 if (all == NULL)
3996 return -1;
3997 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00003998 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003999
4000 for (pos = 0, err = 0; ; pos++) {
4001 name = PySequence_GetItem(all, pos);
4002 if (name == NULL) {
4003 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4004 err = -1;
4005 else
4006 PyErr_Clear();
4007 break;
4008 }
4009 if (skip_leading_underscores &&
4010 PyString_Check(name) &&
4011 PyString_AS_STRING(name)[0] == '_')
4012 {
4013 Py_DECREF(name);
4014 continue;
4015 }
4016 value = PyObject_GetAttr(v, name);
4017 if (value == NULL)
4018 err = -1;
4019 else
4020 err = PyDict_SetItem(locals, name, value);
4021 Py_DECREF(name);
4022 Py_XDECREF(value);
4023 if (err != 0)
4024 break;
4025 }
4026 Py_DECREF(all);
4027 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004028}
4029
Guido van Rossumb209a111997-04-29 18:18:01 +00004030static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004031build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004032{
Guido van Rossum7851eea2001-09-12 19:19:18 +00004033 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004034
4035 if (PyDict_Check(methods))
4036 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00004037 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00004038 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004039 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4040 base = PyTuple_GET_ITEM(bases, 0);
4041 metaclass = PyObject_GetAttrString(base, "__class__");
4042 if (metaclass == NULL) {
4043 PyErr_Clear();
4044 metaclass = (PyObject *)base->ob_type;
4045 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00004046 }
4047 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004048 else {
4049 PyObject *g = PyEval_GetGlobals();
4050 if (g != NULL && PyDict_Check(g))
4051 metaclass = PyDict_GetItemString(g, "__metaclass__");
4052 if (metaclass == NULL)
4053 metaclass = (PyObject *) &PyClass_Type;
4054 Py_INCREF(metaclass);
4055 }
4056 result = PyObject_CallFunction(metaclass, "OOO", name, bases, methods);
4057 Py_DECREF(metaclass);
4058 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00004059}
4060
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004061static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004062exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
4063 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004064{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004065 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00004066 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004067 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004068
Guido van Rossumb209a111997-04-29 18:18:01 +00004069 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
4070 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004071 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00004072 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004073 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00004074 locals = PyTuple_GetItem(prog, 2);
4075 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004076 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004077 if (globals == Py_None) {
4078 globals = PyEval_GetGlobals();
4079 if (locals == Py_None) {
4080 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00004081 plain = 1;
4082 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004083 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004084 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004085 locals = globals;
Guido van Rossumb209a111997-04-29 18:18:01 +00004086 if (!PyString_Check(prog) &&
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004087 !PyUnicode_Check(prog) &&
Guido van Rossumb209a111997-04-29 18:18:01 +00004088 !PyCode_Check(prog) &&
4089 !PyFile_Check(prog)) {
4090 PyErr_SetString(PyExc_TypeError,
Guido van Rossumac7be682001-01-17 15:42:30 +00004091 "exec: arg 1 must be a string, file, or code object");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004092 return -1;
4093 }
Fred Drake661ea262000-10-24 19:57:45 +00004094 if (!PyDict_Check(globals)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004095 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00004096 "exec: arg 2 must be a dictionary or None");
4097 return -1;
4098 }
4099 if (!PyDict_Check(locals)) {
4100 PyErr_SetString(PyExc_TypeError,
4101 "exec: arg 3 must be a dictionary or None");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004102 return -1;
4103 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004104 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00004105 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00004106 if (PyCode_Check(prog)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +00004107 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4108 PyErr_SetString(PyExc_TypeError,
4109 "code object passed to exec may not contain free variables");
4110 return -1;
4111 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004112 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004113 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004114 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004115 FILE *fp = PyFile_AsFile(prog);
4116 char *name = PyString_AsString(PyFile_Name(prog));
Tim Peters5ba58662001-07-16 02:29:45 +00004117 PyCompilerFlags cf;
4118 cf.cf_flags = 0;
4119 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004120 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004121 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004122 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004123 v = PyRun_File(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004124 locals);
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004125 }
4126 else {
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004127 PyObject *tmp = NULL;
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004128 char *str;
Tim Peters5ba58662001-07-16 02:29:45 +00004129 PyCompilerFlags cf;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004130 cf.cf_flags = 0;
4131#ifdef Py_USING_UNICODE
4132 if (PyUnicode_Check(prog)) {
4133 tmp = PyUnicode_AsUTF8String(prog);
4134 if (tmp == NULL)
4135 return -1;
4136 prog = tmp;
4137 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4138 }
4139#endif
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004140 if (PyString_AsStringAndSize(prog, &str, NULL))
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004141 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004142 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters8a5c3c72004-04-05 19:36:21 +00004143 v = PyRun_StringFlags(str, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004144 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004145 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004146 v = PyRun_String(str, Py_file_input, globals, locals);
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004147 Py_XDECREF(tmp);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004148 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004149 if (plain)
4150 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004151 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004152 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00004153 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004154 return 0;
4155}
Guido van Rossum24c13741995-02-14 09:42:43 +00004156
Guido van Rossumac7be682001-01-17 15:42:30 +00004157static void
Paul Prescode68140d2000-08-30 20:25:01 +00004158format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4159{
4160 char *obj_str;
4161
4162 if (!obj)
4163 return;
4164
4165 obj_str = PyString_AsString(obj);
4166 if (!obj_str)
4167 return;
4168
4169 PyErr_Format(exc, format_str, obj_str);
4170}
Guido van Rossum950361c1997-01-24 13:49:28 +00004171
4172#ifdef DYNAMIC_EXECUTION_PROFILE
4173
Skip Montanarof118cb12001-10-15 20:51:38 +00004174static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004175getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004176{
4177 int i;
4178 PyObject *l = PyList_New(256);
4179 if (l == NULL) return NULL;
4180 for (i = 0; i < 256; i++) {
4181 PyObject *x = PyInt_FromLong(a[i]);
4182 if (x == NULL) {
4183 Py_DECREF(l);
4184 return NULL;
4185 }
4186 PyList_SetItem(l, i, x);
4187 }
4188 for (i = 0; i < 256; i++)
4189 a[i] = 0;
4190 return l;
4191}
4192
4193PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004194_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004195{
4196#ifndef DXPAIRS
4197 return getarray(dxp);
4198#else
4199 int i;
4200 PyObject *l = PyList_New(257);
4201 if (l == NULL) return NULL;
4202 for (i = 0; i < 257; i++) {
4203 PyObject *x = getarray(dxpairs[i]);
4204 if (x == NULL) {
4205 Py_DECREF(l);
4206 return NULL;
4207 }
4208 PyList_SetItem(l, i, x);
4209 }
4210 return l;
4211#endif
4212}
4213
4214#endif