blob: c103f1433947cf057e32dfed0f1cbe4cc2db139f [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 Hettinger5bed4562004-04-10 23:34:17 +0000628#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Guido van Rossumd076c731998-10-07 19:42:25 +0000629#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000630#define JUMPBY(x) (next_instr += (x))
631
Raymond Hettingerf606f872003-03-16 03:11:04 +0000632/* OpCode prediction macros
633 Some opcodes tend to come in pairs thus making it possible to predict
634 the second code when the first is run. For example, COMPARE_OP is often
635 followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And, those opcodes are often
636 followed by a POP_TOP.
637
638 Verifying the prediction costs a single high-speed test of register
Raymond Hettingerac2072922003-03-16 15:41:11 +0000639 variable against a constant. If the pairing was good, then the
Raymond Hettingerf606f872003-03-16 03:11:04 +0000640 processor has a high likelihood of making its own successful branch
641 prediction which results in a nearly zero overhead transition to the
642 next opcode.
643
644 A successful prediction saves a trip through the eval-loop including
645 its two unpredictable branches, the HASARG test and the switch-case.
Raymond Hettingera7216982004-02-08 19:59:27 +0000646
Tim Peters8a5c3c72004-04-05 19:36:21 +0000647 If collecting opcode statistics, turn off prediction so that
648 statistics are accurately maintained (the predictions bypass
Raymond Hettingera7216982004-02-08 19:59:27 +0000649 the opcode frequency counter updates).
Raymond Hettingerf606f872003-03-16 03:11:04 +0000650*/
651
Raymond Hettingera7216982004-02-08 19:59:27 +0000652#ifdef DYNAMIC_EXECUTION_PROFILE
653#define PREDICT(op) if (0) goto PRED_##op
654#else
Raymond Hettingerac2072922003-03-16 15:41:11 +0000655#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000656#endif
657
Raymond Hettingerf606f872003-03-16 03:11:04 +0000658#define PREDICTED(op) PRED_##op: next_instr++
Armin Rigo9dbf9082004-03-20 21:50:13 +0000659#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = (next_instr[2]<<8) + \
660 next_instr[1]; next_instr += 3
Raymond Hettingerf606f872003-03-16 03:11:04 +0000661
Guido van Rossum374a9221991-04-04 10:40:29 +0000662/* Stack manipulation macros */
663
664#define STACK_LEVEL() (stack_pointer - f->f_valuestack)
665#define EMPTY() (STACK_LEVEL() == 0)
666#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000667#define SECOND() (stack_pointer[-2])
668#define THIRD() (stack_pointer[-3])
669#define FOURTH() (stack_pointer[-4])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000670#define SET_TOP(v) (stack_pointer[-1] = (v))
671#define SET_SECOND(v) (stack_pointer[-2] = (v))
672#define SET_THIRD(v) (stack_pointer[-3] = (v))
673#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000674#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000675#define BASIC_PUSH(v) (*stack_pointer++ = (v))
676#define BASIC_POP() (*--stack_pointer)
677
Guido van Rossum96a42c81992-01-12 02:29:51 +0000678#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000679#define PUSH(v) { (void)(BASIC_PUSH(v), \
680 lltrace && prtrace(TOP(), "push")); \
681 assert(STACK_LEVEL() <= f->f_stacksize); }
Fred Drakede26cfc2001-10-13 06:11:28 +0000682#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000683#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
684 lltrace && prtrace(TOP(), "stackadj")); \
685 assert(STACK_LEVEL() <= f->f_stacksize); }
Guido van Rossum374a9221991-04-04 10:40:29 +0000686#else
687#define PUSH(v) BASIC_PUSH(v)
688#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000689#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000690#endif
691
Guido van Rossum681d79a1995-07-18 14:51:37 +0000692/* Local variable macros */
693
694#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000695
696/* The SETLOCAL() macro must not DECREF the local variable in-place and
697 then store the new value; it must copy the old value to a temporary
698 value, then store the new value, and then DECREF the temporary value.
699 This is because it is possible that during the DECREF the frame is
700 accessed by other code (e.g. a __del__ method or gc.collect()) and the
701 variable would be pointing to already-freed memory. */
702#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
703 GETLOCAL(i) = value; \
704 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000705
Guido van Rossuma027efa1997-05-05 20:56:21 +0000706/* Start of code */
707
Tim Peters5ca576e2001-06-18 22:08:13 +0000708 if (f == NULL)
709 return NULL;
710
Armin Rigo1d313ab2003-10-25 14:33:09 +0000711 /* push frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000712 if (Py_EnterRecursiveCall(""))
Armin Rigo1d313ab2003-10-25 14:33:09 +0000713 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000714
Tim Peters5ca576e2001-06-18 22:08:13 +0000715 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000716
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000717 if (tstate->use_tracing) {
718 if (tstate->c_tracefunc != NULL) {
719 /* tstate->c_tracefunc, if defined, is a
720 function that will be called on *every* entry
721 to a code block. Its return value, if not
722 None, is a function that will be called at
723 the start of each executed line of code.
724 (Actually, the function must return itself
725 in order to continue tracing.) The trace
726 functions are called with three arguments:
727 a pointer to the current frame, a string
728 indicating why the function is called, and
729 an argument which depends on the situation.
730 The global trace function is also called
731 whenever an exception is detected. */
732 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
733 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000734 /* Trace function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000735 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000736 }
737 }
738 if (tstate->c_profilefunc != NULL) {
739 /* Similar for c_profilefunc, except it needn't
740 return itself and isn't called for "line" events */
741 if (call_trace(tstate->c_profilefunc,
742 tstate->c_profileobj,
743 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000744 /* Profile function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000745 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000746 }
747 }
748 }
749
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000750 co = f->f_code;
751 names = co->co_names;
752 consts = co->co_consts;
753 fastlocals = f->f_localsplus;
754 freevars = f->f_localsplus + f->f_nlocals;
Michael W. Hudsonecfeb7f2004-02-12 15:28:27 +0000755 first_instr = PyString_AS_STRING(co->co_code);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000756 /* An explanation is in order for the next line.
757
758 f->f_lasti now refers to the index of the last instruction
759 executed. You might think this was obvious from the name, but
760 this wasn't always true before 2.3! PyFrame_New now sets
761 f->f_lasti to -1 (i.e. the index *before* the first instruction)
762 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
763 does work. Promise. */
764 next_instr = first_instr + f->f_lasti + 1;
765 stack_pointer = f->f_stacktop;
766 assert(stack_pointer != NULL);
767 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
768
Tim Peters5ca576e2001-06-18 22:08:13 +0000769#ifdef LLTRACE
770 lltrace = PyDict_GetItemString(f->f_globals,"__lltrace__") != NULL;
771#endif
772#if defined(Py_DEBUG) || defined(LLTRACE)
773 filename = PyString_AsString(co->co_filename);
774#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000775
Guido van Rossum374a9221991-04-04 10:40:29 +0000776 why = WHY_NOT;
777 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +0000778 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +0000779 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +0000780
Guido van Rossum374a9221991-04-04 10:40:29 +0000781 for (;;) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000782 assert(stack_pointer >= f->f_valuestack); /* else underflow */
783 assert(STACK_LEVEL() <= f->f_stacksize); /* else overflow */
784
Guido van Rossuma027efa1997-05-05 20:56:21 +0000785 /* Do periodic things. Doing this every time through
786 the loop would add too much overhead, so we do it
787 only every Nth instruction. We also do it if
788 ``things_to_do'' is set, i.e. when an asynchronous
789 event needs attention (e.g. a signal handler or
790 async I/O handler); see Py_AddPendingCall() and
791 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000792
Skip Montanarod581d772002-09-03 20:10:45 +0000793 if (--_Py_Ticker < 0) {
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000794 if (*next_instr == SETUP_FINALLY) {
795 /* Make the last opcode before
796 a try: finally: block uninterruptable. */
797 goto fast_next_opcode;
798 }
Skip Montanarod581d772002-09-03 20:10:45 +0000799 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000800 tstate->tick_counter++;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000801 if (things_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +0000802 if (Py_MakePendingCalls() < 0) {
803 why = WHY_EXCEPTION;
804 goto on_error;
805 }
806 }
Guido van Rossume59214e1994-08-30 08:01:59 +0000807#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000808 if (interpreter_lock) {
809 /* Give another thread a chance */
810
Guido van Rossum25ce5661997-08-02 03:10:38 +0000811 if (PyThreadState_Swap(NULL) != tstate)
812 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000813 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000814
815 /* Other threads may run now */
816
Guido van Rossum65d5b571998-12-21 19:32:43 +0000817 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000818 if (PyThreadState_Swap(tstate) != NULL)
819 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000820
821 /* Check for thread interrupts */
822
823 if (tstate->async_exc != NULL) {
824 x = tstate->async_exc;
825 tstate->async_exc = NULL;
826 PyErr_SetNone(x);
827 Py_DECREF(x);
828 why = WHY_EXCEPTION;
829 goto on_error;
830 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000831 }
832#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000833 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000834
Neil Schemenauer63543862002-02-17 19:10:14 +0000835 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +0000836 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +0000837
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000838 /* line-by-line tracing support */
839
840 if (tstate->c_tracefunc != NULL && !tstate->tracing) {
841 /* see maybe_call_line_trace
842 for expository comments */
843 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +0000844
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000845 err = maybe_call_line_trace(tstate->c_tracefunc,
846 tstate->c_traceobj,
Armin Rigobf57a142004-03-22 19:24:58 +0000847 f, &instr_lb, &instr_ub,
848 &instr_prev);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000849 /* Reload possibly changed frame fields */
850 JUMPTO(f->f_lasti);
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000851 if (f->f_stacktop != NULL) {
852 stack_pointer = f->f_stacktop;
853 f->f_stacktop = NULL;
854 }
855 if (err) {
856 /* trace function raised an exception */
857 goto on_error;
858 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000859 }
860
861 /* Extract opcode and argument */
862
Guido van Rossum374a9221991-04-04 10:40:29 +0000863 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000864 if (HAS_ARG(opcode))
865 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +0000866 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +0000867#ifdef DYNAMIC_EXECUTION_PROFILE
868#ifdef DXPAIRS
869 dxpairs[lastopcode][opcode]++;
870 lastopcode = opcode;
871#endif
872 dxp[opcode]++;
873#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000874
Guido van Rossum96a42c81992-01-12 02:29:51 +0000875#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +0000876 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +0000877
Guido van Rossum96a42c81992-01-12 02:29:51 +0000878 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000879 if (HAS_ARG(opcode)) {
880 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000881 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +0000882 }
883 else {
884 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000885 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +0000886 }
887 }
888#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000889
Guido van Rossum374a9221991-04-04 10:40:29 +0000890 /* Main switch on opcode */
Jeremy Hylton52820442001-01-03 23:52:36 +0000891
Guido van Rossum374a9221991-04-04 10:40:29 +0000892 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +0000893
Guido van Rossum374a9221991-04-04 10:40:29 +0000894 /* BEWARE!
895 It is essential that any operation that fails sets either
896 x to NULL, err to nonzero, or why to anything but WHY_NOT,
897 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000898
Guido van Rossum374a9221991-04-04 10:40:29 +0000899 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000900
Neil Schemenauer63543862002-02-17 19:10:14 +0000901 case LOAD_FAST:
902 x = GETLOCAL(oparg);
903 if (x != NULL) {
904 Py_INCREF(x);
905 PUSH(x);
906 goto fast_next_opcode;
907 }
908 format_exc_check_arg(PyExc_UnboundLocalError,
909 UNBOUNDLOCAL_ERROR_MSG,
910 PyTuple_GetItem(co->co_varnames, oparg));
911 break;
912
913 case LOAD_CONST:
Skip Montanaro04d80f82002-08-04 21:03:35 +0000914 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +0000915 Py_INCREF(x);
916 PUSH(x);
917 goto fast_next_opcode;
918
Raymond Hettinger7dc52212003-03-16 20:14:44 +0000919 PREDICTED_WITH_ARG(STORE_FAST);
Neil Schemenauer63543862002-02-17 19:10:14 +0000920 case STORE_FAST:
921 v = POP();
922 SETLOCAL(oparg, v);
923 goto fast_next_opcode;
924
Raymond Hettingerf606f872003-03-16 03:11:04 +0000925 PREDICTED(POP_TOP);
Guido van Rossum374a9221991-04-04 10:40:29 +0000926 case POP_TOP:
927 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000928 Py_DECREF(v);
Neil Schemenauer63543862002-02-17 19:10:14 +0000929 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000930
Guido van Rossum374a9221991-04-04 10:40:29 +0000931 case ROT_TWO:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000932 v = TOP();
933 w = SECOND();
934 SET_TOP(w);
935 SET_SECOND(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000936 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000937
Guido van Rossum374a9221991-04-04 10:40:29 +0000938 case ROT_THREE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000939 v = TOP();
940 w = SECOND();
941 x = THIRD();
942 SET_TOP(w);
943 SET_SECOND(x);
944 SET_THIRD(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000945 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000946
Thomas Wouters434d0822000-08-24 20:11:32 +0000947 case ROT_FOUR:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000948 u = TOP();
949 v = SECOND();
950 w = THIRD();
951 x = FOURTH();
952 SET_TOP(v);
953 SET_SECOND(w);
954 SET_THIRD(x);
955 SET_FOURTH(u);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000956 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000957
Guido van Rossum374a9221991-04-04 10:40:29 +0000958 case DUP_TOP:
959 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000960 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000961 PUSH(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000962 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000963
Thomas Wouters434d0822000-08-24 20:11:32 +0000964 case DUP_TOPX:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000965 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000966 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000967 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000968 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000969 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000970 STACKADJ(2);
971 SET_TOP(x);
972 SET_SECOND(w);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000973 goto fast_next_opcode;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000974 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000975 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000976 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000977 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000978 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000979 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +0000980 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000981 STACKADJ(3);
982 SET_TOP(x);
983 SET_SECOND(w);
984 SET_THIRD(v);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000985 goto fast_next_opcode;
Thomas Wouters434d0822000-08-24 20:11:32 +0000986 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000987 Py_FatalError("invalid argument to DUP_TOPX"
988 " (bytecode corruption?)");
Tim Peters35ba6892000-10-11 07:04:49 +0000989 break;
Thomas Wouters434d0822000-08-24 20:11:32 +0000990
Guido van Rossum374a9221991-04-04 10:40:29 +0000991 case UNARY_POSITIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000992 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000993 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000994 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000995 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000996 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000997 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000998
Guido van Rossum374a9221991-04-04 10:40:29 +0000999 case UNARY_NEGATIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001000 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001001 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001002 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001003 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001004 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001005 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001006
Guido van Rossum374a9221991-04-04 10:40:29 +00001007 case UNARY_NOT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001008 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001009 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001010 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +00001011 if (err == 0) {
1012 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001013 SET_TOP(Py_True);
Guido van Rossumfc490731997-05-06 15:06:49 +00001014 continue;
1015 }
1016 else if (err > 0) {
1017 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001018 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +00001019 err = 0;
1020 continue;
1021 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00001022 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001023 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001024
Guido van Rossum374a9221991-04-04 10:40:29 +00001025 case UNARY_CONVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001026 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001027 x = PyObject_Repr(v);
1028 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001029 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001030 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001031 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001032
Guido van Rossum7928cd71991-10-24 14:59:31 +00001033 case UNARY_INVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001034 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001035 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001036 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001037 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001038 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001039 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001040
Guido van Rossum50564e81996-01-12 01:13:16 +00001041 case BINARY_POWER:
1042 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001043 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001044 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +00001045 Py_DECREF(v);
1046 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001047 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001048 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +00001049 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001050
Guido van Rossum374a9221991-04-04 10:40:29 +00001051 case BINARY_MULTIPLY:
1052 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001053 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001054 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001055 Py_DECREF(v);
1056 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001057 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001058 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001059 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001060
Guido van Rossum374a9221991-04-04 10:40:29 +00001061 case BINARY_DIVIDE:
Tim Peters3caca232001-12-06 06:23:26 +00001062 if (!_Py_QnewFlag) {
1063 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001064 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001065 x = PyNumber_Divide(v, w);
1066 Py_DECREF(v);
1067 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001068 SET_TOP(x);
Tim Peters3caca232001-12-06 06:23:26 +00001069 if (x != NULL) continue;
1070 break;
1071 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001072 /* -Qnew is in effect: fall through to
Tim Peters3caca232001-12-06 06:23:26 +00001073 BINARY_TRUE_DIVIDE */
1074 case BINARY_TRUE_DIVIDE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001075 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001076 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001077 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001078 Py_DECREF(v);
1079 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001080 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001081 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001082 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001083
Guido van Rossum4668b002001-08-08 05:00:18 +00001084 case BINARY_FLOOR_DIVIDE:
1085 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001086 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001087 x = PyNumber_FloorDivide(v, w);
1088 Py_DECREF(v);
1089 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001090 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001091 if (x != NULL) continue;
1092 break;
1093
Guido van Rossum374a9221991-04-04 10:40:29 +00001094 case BINARY_MODULO:
1095 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001096 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001097 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001098 Py_DECREF(v);
1099 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001100 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001101 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001102 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001103
Guido van Rossum374a9221991-04-04 10:40:29 +00001104 case BINARY_ADD:
1105 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001106 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001107 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001108 /* INLINE: int + int */
1109 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001110 a = PyInt_AS_LONG(v);
1111 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001112 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001113 if ((i^a) < 0 && (i^b) < 0)
1114 goto slow_add;
1115 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001116 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001117 else {
1118 slow_add:
Guido van Rossumc12da691997-07-17 23:12:42 +00001119 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001120 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001121 Py_DECREF(v);
1122 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001123 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001124 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001125 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001126
Guido van Rossum374a9221991-04-04 10:40:29 +00001127 case BINARY_SUBTRACT:
1128 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001129 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001130 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001131 /* INLINE: int - int */
1132 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001133 a = PyInt_AS_LONG(v);
1134 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001135 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001136 if ((i^a) < 0 && (i^~b) < 0)
1137 goto slow_sub;
1138 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001139 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001140 else {
1141 slow_sub:
Guido van Rossumc12da691997-07-17 23:12:42 +00001142 x = PyNumber_Subtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001143 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001144 Py_DECREF(v);
1145 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001146 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001147 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001148 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001149
Guido van Rossum374a9221991-04-04 10:40:29 +00001150 case BINARY_SUBSCR:
1151 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001152 v = TOP();
Tim Petersb1c46982001-10-05 20:41:38 +00001153 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001154 /* INLINE: list[int] */
1155 long i = PyInt_AsLong(w);
1156 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +00001157 i += PyList_GET_SIZE(v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001158 if (i >= 0 && i < PyList_GET_SIZE(v)) {
Guido van Rossumfa00e951998-07-08 15:02:37 +00001159 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001160 Py_INCREF(x);
1161 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001162 else
1163 goto slow_get;
Guido van Rossumc12da691997-07-17 23:12:42 +00001164 }
1165 else
Raymond Hettinger467a6982004-04-07 11:39:21 +00001166 slow_get:
Guido van Rossumc12da691997-07-17 23:12:42 +00001167 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001168 Py_DECREF(v);
1169 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001170 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001171 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001172 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001173
Guido van Rossum7928cd71991-10-24 14:59:31 +00001174 case BINARY_LSHIFT:
1175 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001176 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001177 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001178 Py_DECREF(v);
1179 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001180 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001181 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001182 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001183
Guido van Rossum7928cd71991-10-24 14:59:31 +00001184 case BINARY_RSHIFT:
1185 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001186 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001187 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001188 Py_DECREF(v);
1189 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001190 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001191 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001192 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001193
Guido van Rossum7928cd71991-10-24 14:59:31 +00001194 case BINARY_AND:
1195 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001196 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001197 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001198 Py_DECREF(v);
1199 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001200 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001201 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001202 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001203
Guido van Rossum7928cd71991-10-24 14:59:31 +00001204 case BINARY_XOR:
1205 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001206 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001207 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001208 Py_DECREF(v);
1209 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001210 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001211 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001212 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001213
Guido van Rossum7928cd71991-10-24 14:59:31 +00001214 case BINARY_OR:
1215 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001216 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001217 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001218 Py_DECREF(v);
1219 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001220 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001221 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001222 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001223
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001224 case LIST_APPEND:
1225 w = POP();
1226 v = POP();
1227 err = PyList_Append(v, w);
1228 Py_DECREF(v);
1229 Py_DECREF(w);
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001230 if (err == 0) {
1231 PREDICT(JUMP_ABSOLUTE);
1232 continue;
1233 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001234 break;
1235
Thomas Wouters434d0822000-08-24 20:11:32 +00001236 case INPLACE_POWER:
1237 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001238 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001239 x = PyNumber_InPlacePower(v, w, Py_None);
1240 Py_DECREF(v);
1241 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001242 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001243 if (x != NULL) continue;
1244 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001245
Thomas Wouters434d0822000-08-24 20:11:32 +00001246 case INPLACE_MULTIPLY:
1247 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001248 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001249 x = PyNumber_InPlaceMultiply(v, w);
1250 Py_DECREF(v);
1251 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001252 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001253 if (x != NULL) continue;
1254 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001255
Thomas Wouters434d0822000-08-24 20:11:32 +00001256 case INPLACE_DIVIDE:
Tim Peters54b11912001-12-25 18:49:11 +00001257 if (!_Py_QnewFlag) {
1258 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001259 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001260 x = PyNumber_InPlaceDivide(v, w);
1261 Py_DECREF(v);
1262 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001263 SET_TOP(x);
Tim Peters54b11912001-12-25 18:49:11 +00001264 if (x != NULL) continue;
1265 break;
1266 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001267 /* -Qnew is in effect: fall through to
Tim Peters54b11912001-12-25 18:49:11 +00001268 INPLACE_TRUE_DIVIDE */
1269 case INPLACE_TRUE_DIVIDE:
Thomas Wouters434d0822000-08-24 20:11:32 +00001270 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001271 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001272 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001273 Py_DECREF(v);
1274 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001275 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001276 if (x != NULL) continue;
1277 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001278
Guido van Rossum4668b002001-08-08 05:00:18 +00001279 case INPLACE_FLOOR_DIVIDE:
1280 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001281 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001282 x = PyNumber_InPlaceFloorDivide(v, w);
1283 Py_DECREF(v);
1284 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001285 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001286 if (x != NULL) continue;
1287 break;
1288
Thomas Wouters434d0822000-08-24 20:11:32 +00001289 case INPLACE_MODULO:
1290 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001291 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001292 x = PyNumber_InPlaceRemainder(v, w);
1293 Py_DECREF(v);
1294 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001295 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001296 if (x != NULL) continue;
1297 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001298
Thomas Wouters434d0822000-08-24 20:11:32 +00001299 case INPLACE_ADD:
1300 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001301 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001302 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001303 /* INLINE: int + int */
1304 register long a, b, i;
1305 a = PyInt_AS_LONG(v);
1306 b = PyInt_AS_LONG(w);
1307 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001308 if ((i^a) < 0 && (i^b) < 0)
1309 goto slow_iadd;
1310 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001311 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001312 else {
1313 slow_iadd:
Thomas Wouters434d0822000-08-24 20:11:32 +00001314 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001315 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001316 Py_DECREF(v);
1317 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001318 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001319 if (x != NULL) continue;
1320 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001321
Thomas Wouters434d0822000-08-24 20:11:32 +00001322 case INPLACE_SUBTRACT:
1323 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001324 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001325 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001326 /* INLINE: int - int */
1327 register long a, b, i;
1328 a = PyInt_AS_LONG(v);
1329 b = PyInt_AS_LONG(w);
1330 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001331 if ((i^a) < 0 && (i^~b) < 0)
1332 goto slow_isub;
1333 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001334 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001335 else {
1336 slow_isub:
Thomas Wouters434d0822000-08-24 20:11:32 +00001337 x = PyNumber_InPlaceSubtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001338 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001339 Py_DECREF(v);
1340 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001341 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001342 if (x != NULL) continue;
1343 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001344
Thomas Wouters434d0822000-08-24 20:11:32 +00001345 case INPLACE_LSHIFT:
1346 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001347 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001348 x = PyNumber_InPlaceLshift(v, w);
1349 Py_DECREF(v);
1350 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001351 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001352 if (x != NULL) continue;
1353 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001354
Thomas Wouters434d0822000-08-24 20:11:32 +00001355 case INPLACE_RSHIFT:
1356 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001357 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001358 x = PyNumber_InPlaceRshift(v, w);
1359 Py_DECREF(v);
1360 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001361 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001362 if (x != NULL) continue;
1363 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001364
Thomas Wouters434d0822000-08-24 20:11:32 +00001365 case INPLACE_AND:
1366 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001367 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001368 x = PyNumber_InPlaceAnd(v, w);
1369 Py_DECREF(v);
1370 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001371 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001372 if (x != NULL) continue;
1373 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001374
Thomas Wouters434d0822000-08-24 20:11:32 +00001375 case INPLACE_XOR:
1376 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001377 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001378 x = PyNumber_InPlaceXor(v, w);
1379 Py_DECREF(v);
1380 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001381 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001382 if (x != NULL) continue;
1383 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001384
Thomas Wouters434d0822000-08-24 20:11:32 +00001385 case INPLACE_OR:
1386 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001387 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001388 x = PyNumber_InPlaceOr(v, w);
1389 Py_DECREF(v);
1390 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001391 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001392 if (x != NULL) continue;
1393 break;
1394
Guido van Rossum374a9221991-04-04 10:40:29 +00001395 case SLICE+0:
1396 case SLICE+1:
1397 case SLICE+2:
1398 case SLICE+3:
1399 if ((opcode-SLICE) & 2)
1400 w = POP();
1401 else
1402 w = NULL;
1403 if ((opcode-SLICE) & 1)
1404 v = POP();
1405 else
1406 v = NULL;
Raymond Hettinger663004b2003-01-09 15:24:30 +00001407 u = TOP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001408 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001409 Py_DECREF(u);
1410 Py_XDECREF(v);
1411 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001412 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001413 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001414 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001415
Guido van Rossum374a9221991-04-04 10:40:29 +00001416 case STORE_SLICE+0:
1417 case STORE_SLICE+1:
1418 case STORE_SLICE+2:
1419 case STORE_SLICE+3:
1420 if ((opcode-STORE_SLICE) & 2)
1421 w = POP();
1422 else
1423 w = NULL;
1424 if ((opcode-STORE_SLICE) & 1)
1425 v = POP();
1426 else
1427 v = NULL;
1428 u = POP();
1429 t = POP();
1430 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +00001431 Py_DECREF(t);
1432 Py_DECREF(u);
1433 Py_XDECREF(v);
1434 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001435 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001436 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001437
Guido van Rossum374a9221991-04-04 10:40:29 +00001438 case DELETE_SLICE+0:
1439 case DELETE_SLICE+1:
1440 case DELETE_SLICE+2:
1441 case DELETE_SLICE+3:
1442 if ((opcode-DELETE_SLICE) & 2)
1443 w = POP();
1444 else
1445 w = NULL;
1446 if ((opcode-DELETE_SLICE) & 1)
1447 v = POP();
1448 else
1449 v = NULL;
1450 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001451 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +00001452 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00001453 Py_DECREF(u);
1454 Py_XDECREF(v);
1455 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001456 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001457 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001458
Guido van Rossum374a9221991-04-04 10:40:29 +00001459 case STORE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001460 w = TOP();
1461 v = SECOND();
1462 u = THIRD();
1463 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001464 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001465 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001466 Py_DECREF(u);
1467 Py_DECREF(v);
1468 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001469 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001470 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001471
Guido van Rossum374a9221991-04-04 10:40:29 +00001472 case DELETE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001473 w = TOP();
1474 v = SECOND();
1475 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001476 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001477 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001478 Py_DECREF(v);
1479 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001480 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001481 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001482
Guido van Rossum374a9221991-04-04 10:40:29 +00001483 case PRINT_EXPR:
1484 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001485 w = PySys_GetObject("displayhook");
1486 if (w == NULL) {
1487 PyErr_SetString(PyExc_RuntimeError,
1488 "lost sys.displayhook");
1489 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001490 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001491 }
1492 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001493 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001494 if (x == NULL)
1495 err = -1;
1496 }
1497 if (err == 0) {
1498 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001499 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001500 if (w == NULL)
1501 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001502 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001503 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001504 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001505 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001506
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001507 case PRINT_ITEM_TO:
1508 w = stream = POP();
1509 /* fall through to PRINT_ITEM */
1510
Guido van Rossum374a9221991-04-04 10:40:29 +00001511 case PRINT_ITEM:
1512 v = POP();
Barry Warsaw093abe02000-08-29 04:56:13 +00001513 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001514 w = PySys_GetObject("stdout");
1515 if (w == NULL) {
1516 PyErr_SetString(PyExc_RuntimeError,
1517 "lost sys.stdout");
1518 err = -1;
1519 }
Guido van Rossum8f183201997-12-31 05:53:15 +00001520 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001521 /* PyFile_SoftSpace() can exececute arbitrary code
1522 if sys.stdout is an instance with a __getattr__.
1523 If __getattr__ raises an exception, w will
1524 be freed, so we need to prevent that temporarily. */
1525 Py_XINCREF(w);
Tim Peters8e5fd532002-03-24 19:25:00 +00001526 if (w != NULL && PyFile_SoftSpace(w, 0))
Guido van Rossumbe270261997-05-22 22:26:18 +00001527 err = PyFile_WriteString(" ", w);
1528 if (err == 0)
1529 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001530 if (err == 0) {
Tim Peters8e5fd532002-03-24 19:25:00 +00001531 /* XXX move into writeobject() ? */
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001532 if (PyString_Check(v)) {
1533 char *s = PyString_AS_STRING(v);
1534 int len = PyString_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001535 if (len == 0 ||
1536 !isspace(Py_CHARMASK(s[len-1])) ||
1537 s[len-1] == ' ')
1538 PyFile_SoftSpace(w, 1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001539 }
Martin v. Löwis8d3ce5a2001-12-18 22:36:40 +00001540#ifdef Py_USING_UNICODE
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001541 else if (PyUnicode_Check(v)) {
1542 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
1543 int len = PyUnicode_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001544 if (len == 0 ||
1545 !Py_UNICODE_ISSPACE(s[len-1]) ||
1546 s[len-1] == ' ')
1547 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001548 }
Michael W. Hudsond95c8282002-05-20 13:56:11 +00001549#endif
Tim Peters8e5fd532002-03-24 19:25:00 +00001550 else
1551 PyFile_SoftSpace(w, 1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001552 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001553 Py_XDECREF(w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001554 Py_DECREF(v);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001555 Py_XDECREF(stream);
1556 stream = NULL;
1557 if (err == 0)
1558 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001559 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001560
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001561 case PRINT_NEWLINE_TO:
1562 w = stream = POP();
1563 /* fall through to PRINT_NEWLINE */
1564
Guido van Rossum374a9221991-04-04 10:40:29 +00001565 case PRINT_NEWLINE:
Barry Warsaw093abe02000-08-29 04:56:13 +00001566 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001567 w = PySys_GetObject("stdout");
1568 if (w == NULL)
1569 PyErr_SetString(PyExc_RuntimeError,
1570 "lost sys.stdout");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001571 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001572 if (w != NULL) {
1573 err = PyFile_WriteString("\n", w);
1574 if (err == 0)
1575 PyFile_SoftSpace(w, 0);
1576 }
1577 Py_XDECREF(stream);
1578 stream = NULL;
Guido van Rossum374a9221991-04-04 10:40:29 +00001579 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001580
Thomas Wouters434d0822000-08-24 20:11:32 +00001581
1582#ifdef CASE_TOO_BIG
1583 default: switch (opcode) {
1584#endif
Guido van Rossumf10570b1995-07-07 22:53:21 +00001585 case RAISE_VARARGS:
1586 u = v = w = NULL;
1587 switch (oparg) {
1588 case 3:
1589 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001590 /* Fallthrough */
1591 case 2:
1592 v = POP(); /* value */
1593 /* Fallthrough */
1594 case 1:
1595 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001596 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001597 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001598 break;
1599 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001600 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001601 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001602 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001603 break;
1604 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001605 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001606
Guido van Rossum374a9221991-04-04 10:40:29 +00001607 case LOAD_LOCALS:
Raymond Hettinger467a6982004-04-07 11:39:21 +00001608 if ((x = f->f_locals) != NULL) {
1609 Py_INCREF(x);
1610 PUSH(x);
1611 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001612 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001613 PyErr_SetString(PyExc_SystemError, "no locals");
Guido van Rossum374a9221991-04-04 10:40:29 +00001614 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001615
Guido van Rossum374a9221991-04-04 10:40:29 +00001616 case RETURN_VALUE:
1617 retval = POP();
1618 why = WHY_RETURN;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001619 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001620
Tim Peters5ca576e2001-06-18 22:08:13 +00001621 case YIELD_VALUE:
1622 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001623 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001624 why = WHY_YIELD;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001625 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001626
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001627 case EXEC_STMT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001628 w = TOP();
1629 v = SECOND();
1630 u = THIRD();
1631 STACKADJ(-3);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001632 err = exec_statement(f, u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001633 Py_DECREF(u);
1634 Py_DECREF(v);
1635 Py_DECREF(w);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001636 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001637
Guido van Rossum374a9221991-04-04 10:40:29 +00001638 case POP_BLOCK:
1639 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001640 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001641 while (STACK_LEVEL() > b->b_level) {
1642 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001643 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001644 }
1645 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001646 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001647
Guido van Rossum374a9221991-04-04 10:40:29 +00001648 case END_FINALLY:
1649 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001650 if (PyInt_Check(v)) {
Raymond Hettinger7c958652004-04-06 10:11:10 +00001651 why = (enum why_code) PyInt_AS_LONG(v);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001652 assert(why != WHY_YIELD);
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00001653 if (why == WHY_RETURN ||
1654 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001655 retval = POP();
1656 }
Raymond Hettingerd3b836d2004-04-07 13:17:27 +00001657 else if (PyClass_Check(v) || PyString_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001658 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001659 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001660 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001661 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001662 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001663 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001664 else if (v != Py_None) {
1665 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001666 "'finally' pops bad exception");
1667 why = WHY_EXCEPTION;
1668 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001669 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001670 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001671
Guido van Rossum374a9221991-04-04 10:40:29 +00001672 case BUILD_CLASS:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001673 u = TOP();
1674 v = SECOND();
1675 w = THIRD();
1676 STACKADJ(-2);
Guido van Rossum25831651993-05-19 14:50:45 +00001677 x = build_class(u, v, w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001678 SET_TOP(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001679 Py_DECREF(u);
1680 Py_DECREF(v);
1681 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001682 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001683
Guido van Rossum374a9221991-04-04 10:40:29 +00001684 case STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001685 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001686 v = POP();
Raymond Hettinger467a6982004-04-07 11:39:21 +00001687 if ((x = f->f_locals) != NULL) {
1688 err = PyDict_SetItem(x, w, v);
1689 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001690 if (err == 0) continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001691 break;
1692 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001693 PyErr_Format(PyExc_SystemError,
1694 "no locals found when storing %s",
1695 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001696 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001697
Guido van Rossum374a9221991-04-04 10:40:29 +00001698 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001699 w = GETITEM(names, oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001700 if ((x = f->f_locals) != NULL) {
1701 if ((err = PyDict_DelItem(x, w)) != 0)
1702 format_exc_check_arg(PyExc_NameError,
1703 NAME_ERROR_MSG ,w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001704 break;
1705 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001706 PyErr_Format(PyExc_SystemError,
1707 "no locals when deleting %s",
1708 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001709 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001710
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001711 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001712 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001713 v = POP();
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001714 if (PyTuple_CheckExact(v) && PyTuple_GET_SIZE(v) == oparg) {
1715 PyObject **items = ((PyTupleObject *)v)->ob_item;
1716 while (oparg--) {
1717 w = items[oparg];
1718 Py_INCREF(w);
1719 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001720 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001721 Py_DECREF(v);
1722 continue;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001723 } else if (PyList_CheckExact(v) && PyList_GET_SIZE(v) == oparg) {
1724 PyObject **items = ((PyListObject *)v)->ob_item;
1725 while (oparg--) {
1726 w = items[oparg];
1727 Py_INCREF(w);
1728 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001729 }
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001730 } else if (unpack_iterable(v, oparg,
Tim Petersd6d010b2001-06-21 02:49:55 +00001731 stack_pointer + oparg))
1732 stack_pointer += oparg;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001733 else {
1734 if (PyErr_ExceptionMatches(PyExc_TypeError))
1735 PyErr_SetString(PyExc_TypeError,
1736 "unpack non-sequence");
Barry Warsawe42b18f1997-08-25 22:13:04 +00001737 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001738 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001739 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001740 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001741
Guido van Rossum374a9221991-04-04 10:40:29 +00001742 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001743 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001744 v = TOP();
1745 u = SECOND();
1746 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001747 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1748 Py_DECREF(v);
1749 Py_DECREF(u);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001750 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001751 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001752
Guido van Rossum374a9221991-04-04 10:40:29 +00001753 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001754 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001755 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001756 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1757 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001758 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001759 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001760
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001761 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001762 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001763 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001764 err = PyDict_SetItem(f->f_globals, w, v);
1765 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001766 if (err == 0) continue;
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001767 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001768
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001769 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001770 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001771 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001772 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001773 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001774 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001775
Guido van Rossum374a9221991-04-04 10:40:29 +00001776 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001777 w = GETITEM(names, oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001778 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001779 PyErr_Format(PyExc_SystemError,
1780 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001781 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001782 break;
1783 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001784 x = PyDict_GetItem(x, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001785 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001786 x = PyDict_GetItem(f->f_globals, 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_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001789 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001790 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001791 PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001792 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001793 break;
1794 }
1795 }
1796 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001797 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001798 PUSH(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001799 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001800
Guido van Rossum374a9221991-04-04 10:40:29 +00001801 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001802 w = GETITEM(names, oparg);
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001803 if (PyString_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00001804 /* Inline the PyDict_GetItem() calls.
1805 WARNING: this is an extreme speed hack.
1806 Do not try this at home. */
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001807 long hash = ((PyStringObject *)w)->ob_shash;
1808 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001809 PyDictObject *d;
1810 d = (PyDictObject *)(f->f_globals);
1811 x = d->ma_lookup(d, w, hash)->me_value;
1812 if (x != NULL) {
1813 Py_INCREF(x);
1814 PUSH(x);
1815 continue;
1816 }
1817 d = (PyDictObject *)(f->f_builtins);
1818 x = d->ma_lookup(d, w, hash)->me_value;
1819 if (x != NULL) {
1820 Py_INCREF(x);
1821 PUSH(x);
1822 continue;
1823 }
1824 goto load_global_error;
1825 }
1826 }
1827 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00001828 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001829 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001830 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001831 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001832 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00001833 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001834 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001835 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001836 break;
1837 }
1838 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001839 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001840 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001841 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001842
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001843 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001844 x = GETLOCAL(oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001845 if (x != NULL) {
1846 SETLOCAL(oparg, NULL);
1847 continue;
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001848 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001849 format_exc_check_arg(
1850 PyExc_UnboundLocalError,
1851 UNBOUNDLOCAL_ERROR_MSG,
1852 PyTuple_GetItem(co->co_varnames, oparg)
1853 );
1854 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001855
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001856 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001857 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001858 Py_INCREF(x);
1859 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001860 if (x != NULL) continue;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001861 break;
1862
1863 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001864 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001865 w = PyCell_Get(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001866 if (w != NULL) {
1867 PUSH(w);
1868 continue;
Jeremy Hylton2524d692001-02-05 17:23:16 +00001869 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001870 err = -1;
1871 /* Don't stomp existing exception */
1872 if (PyErr_Occurred())
1873 break;
1874 if (oparg < f->f_ncells) {
1875 v = PyTuple_GetItem(co->co_cellvars,
1876 oparg);
1877 format_exc_check_arg(
1878 PyExc_UnboundLocalError,
1879 UNBOUNDLOCAL_ERROR_MSG,
1880 v);
1881 } else {
1882 v = PyTuple_GetItem(
1883 co->co_freevars,
1884 oparg - f->f_ncells);
1885 format_exc_check_arg(
1886 PyExc_NameError,
1887 UNBOUNDFREE_ERROR_MSG,
1888 v);
1889 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001890 break;
1891
1892 case STORE_DEREF:
1893 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001894 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001895 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00001896 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001897 continue;
1898
Guido van Rossum374a9221991-04-04 10:40:29 +00001899 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001900 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001901 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001902 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001903 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001904 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001905 }
1906 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001907 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001908 }
1909 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001910
Guido van Rossum374a9221991-04-04 10:40:29 +00001911 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00001912 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001913 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001914 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001915 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00001916 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001917 }
1918 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001919 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001920 }
1921 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001922
Guido van Rossum374a9221991-04-04 10:40:29 +00001923 case BUILD_MAP:
Guido van Rossumb209a111997-04-29 18:18:01 +00001924 x = PyDict_New();
Guido van Rossum374a9221991-04-04 10:40:29 +00001925 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001926 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001927 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001928
Guido van Rossum374a9221991-04-04 10:40:29 +00001929 case LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001930 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001931 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001932 x = PyObject_GetAttr(v, w);
1933 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001934 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001935 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001936 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001937
Guido van Rossum374a9221991-04-04 10:40:29 +00001938 case COMPARE_OP:
1939 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001940 v = TOP();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001941 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001942 /* INLINE: cmp(int, int) */
1943 register long a, b;
1944 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001945 a = PyInt_AS_LONG(v);
1946 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001947 switch (oparg) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00001948 case PyCmp_LT: res = a < b; break;
1949 case PyCmp_LE: res = a <= b; break;
1950 case PyCmp_EQ: res = a == b; break;
1951 case PyCmp_NE: res = a != b; break;
1952 case PyCmp_GT: res = a > b; break;
1953 case PyCmp_GE: res = a >= b; break;
1954 case PyCmp_IS: res = v == w; break;
1955 case PyCmp_IS_NOT: res = v != w; break;
Guido van Rossumc12da691997-07-17 23:12:42 +00001956 default: goto slow_compare;
1957 }
1958 x = res ? Py_True : Py_False;
1959 Py_INCREF(x);
1960 }
1961 else {
1962 slow_compare:
1963 x = cmp_outcome(oparg, v, w);
1964 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001965 Py_DECREF(v);
1966 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001967 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001968 if (x == NULL) break;
1969 PREDICT(JUMP_IF_FALSE);
1970 PREDICT(JUMP_IF_TRUE);
1971 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001972
Guido van Rossum374a9221991-04-04 10:40:29 +00001973 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001974 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001975 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001976 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001977 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00001978 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001979 break;
1980 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001981 u = TOP();
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001982 w = PyTuple_Pack(4,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001983 w,
1984 f->f_globals,
Guido van Rossuma027efa1997-05-05 20:56:21 +00001985 f->f_locals == NULL ?
1986 Py_None : f->f_locals,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001987 u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001988 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001989 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001990 u = POP();
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001991 x = NULL;
1992 break;
1993 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001994 x = PyEval_CallObject(x, w);
1995 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001996 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001997 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001998 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001999
Thomas Wouters52152252000-08-17 22:55:00 +00002000 case IMPORT_STAR:
2001 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002002 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002003 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002004 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00002005 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002006 break;
2007 }
Thomas Wouters52152252000-08-17 22:55:00 +00002008 err = import_all_from(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002009 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00002010 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002011 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002012 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002013
Thomas Wouters52152252000-08-17 22:55:00 +00002014 case IMPORT_FROM:
Skip Montanaro496e6582002-08-06 17:47:40 +00002015 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00002016 v = TOP();
2017 x = import_from(v, w);
2018 PUSH(x);
2019 if (x != NULL) continue;
2020 break;
2021
Guido van Rossum374a9221991-04-04 10:40:29 +00002022 case JUMP_FORWARD:
2023 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002024 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002025
Raymond Hettingerf606f872003-03-16 03:11:04 +00002026 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002027 case JUMP_IF_FALSE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002028 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002029 if (w == Py_True) {
2030 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002031 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002032 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002033 if (w == Py_False) {
2034 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002035 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002036 }
2037 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002038 if (err > 0)
2039 err = 0;
2040 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00002041 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002042 else
2043 break;
2044 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002045
Raymond Hettingerf606f872003-03-16 03:11:04 +00002046 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002047 case JUMP_IF_TRUE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002048 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002049 if (w == Py_False) {
2050 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002051 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002052 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002053 if (w == Py_True) {
2054 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002055 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002056 }
2057 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002058 if (err > 0) {
2059 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00002060 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002061 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002062 else if (err == 0)
2063 ;
2064 else
2065 break;
2066 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002067
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00002068 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002069 case JUMP_ABSOLUTE:
2070 JUMPTO(oparg);
Neil Schemenauerca2a2f12003-05-30 23:59:44 +00002071 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002072
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002073 case GET_ITER:
2074 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002075 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002076 x = PyObject_GetIter(v);
2077 Py_DECREF(v);
2078 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002079 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002080 PREDICT(FOR_ITER);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002081 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002082 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002083 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002084 break;
2085
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002086 PREDICTED_WITH_ARG(FOR_ITER);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002087 case FOR_ITER:
2088 /* before: [iter]; after: [iter, iter()] *or* [] */
2089 v = TOP();
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002090 x = (*v->ob_type->tp_iternext)(v);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002091 if (x != NULL) {
2092 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002093 PREDICT(STORE_FAST);
2094 PREDICT(UNPACK_SEQUENCE);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002095 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002096 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002097 if (PyErr_Occurred()) {
2098 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2099 break;
2100 PyErr_Clear();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002101 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002102 /* iterator ended normally */
2103 x = v = POP();
2104 Py_DECREF(v);
2105 JUMPBY(oparg);
2106 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002107
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002108 case BREAK_LOOP:
2109 why = WHY_BREAK;
2110 goto fast_block_end;
2111
2112 case CONTINUE_LOOP:
2113 retval = PyInt_FromLong(oparg);
2114 why = WHY_CONTINUE;
2115 goto fast_block_end;
2116
Guido van Rossum374a9221991-04-04 10:40:29 +00002117 case SETUP_LOOP:
2118 case SETUP_EXCEPT:
2119 case SETUP_FINALLY:
Guido van Rossumb209a111997-04-29 18:18:01 +00002120 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002121 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002122 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002123
Guido van Rossumf10570b1995-07-07 22:53:21 +00002124 case CALL_FUNCTION:
Jeremy Hylton985eba52003-02-05 23:13:00 +00002125 PCALL(PCALL_ALL);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002126 x = call_function(&stack_pointer, oparg);
2127 PUSH(x);
2128 if (x != NULL)
2129 continue;
2130 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002131
Jeremy Hylton76901512000-03-28 23:49:17 +00002132 case CALL_FUNCTION_VAR:
2133 case CALL_FUNCTION_KW:
2134 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002135 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002136 int na = oparg & 0xff;
2137 int nk = (oparg>>8) & 0xff;
2138 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002139 int n = na + 2 * nk;
2140 PyObject **pfunc, *func;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002141 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002142 if (flags & CALL_FLAG_VAR)
2143 n++;
2144 if (flags & CALL_FLAG_KW)
2145 n++;
2146 pfunc = stack_pointer - n - 1;
2147 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002148
Guido van Rossumac7be682001-01-17 15:42:30 +00002149 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002150 && PyMethod_GET_SELF(func) != NULL) {
2151 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002152 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002153 func = PyMethod_GET_FUNCTION(func);
2154 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002155 Py_DECREF(*pfunc);
2156 *pfunc = self;
2157 na++;
2158 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002159 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002160 Py_INCREF(func);
2161 x = ext_do_call(func, &stack_pointer, flags, na, nk);
Jeremy Hylton76901512000-03-28 23:49:17 +00002162 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002163
Jeremy Hylton76901512000-03-28 23:49:17 +00002164 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002165 w = POP();
2166 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002167 }
2168 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002169 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002170 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002171 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002172 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002173
Guido van Rossum681d79a1995-07-18 14:51:37 +00002174 case MAKE_FUNCTION:
2175 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002176 x = PyFunction_New(v, f->f_globals);
2177 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002178 /* XXX Maybe this should be a separate opcode? */
2179 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002180 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002181 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002182 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002183 x = NULL;
2184 break;
2185 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002186 while (--oparg >= 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002187 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002188 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002189 }
2190 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002191 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002192 }
2193 PUSH(x);
2194 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002195
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002196 case MAKE_CLOSURE:
2197 {
2198 int nfree;
2199 v = POP(); /* code object */
2200 x = PyFunction_New(v, f->f_globals);
Jeremy Hylton733c8932001-12-13 19:51:56 +00002201 nfree = PyCode_GetNumFree((PyCodeObject *)v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002202 Py_DECREF(v);
2203 /* XXX Maybe this should be a separate opcode? */
2204 if (x != NULL && nfree > 0) {
2205 v = PyTuple_New(nfree);
2206 if (v == NULL) {
2207 Py_DECREF(x);
2208 x = NULL;
2209 break;
2210 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002211 while (--nfree >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002212 w = POP();
2213 PyTuple_SET_ITEM(v, nfree, w);
2214 }
2215 err = PyFunction_SetClosure(x, v);
2216 Py_DECREF(v);
2217 }
2218 if (x != NULL && oparg > 0) {
2219 v = PyTuple_New(oparg);
2220 if (v == NULL) {
2221 Py_DECREF(x);
2222 x = NULL;
2223 break;
2224 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002225 while (--oparg >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002226 w = POP();
2227 PyTuple_SET_ITEM(v, oparg, w);
2228 }
2229 err = PyFunction_SetDefaults(x, v);
2230 Py_DECREF(v);
2231 }
2232 PUSH(x);
2233 break;
2234 }
2235
Guido van Rossum8861b741996-07-30 16:49:37 +00002236 case BUILD_SLICE:
2237 if (oparg == 3)
2238 w = POP();
2239 else
2240 w = NULL;
2241 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002242 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002243 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002244 Py_DECREF(u);
2245 Py_DECREF(v);
2246 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002247 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002248 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002249 break;
2250
Fred Drakeef8ace32000-08-24 00:32:09 +00002251 case EXTENDED_ARG:
2252 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002253 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002254 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002255
Guido van Rossum374a9221991-04-04 10:40:29 +00002256 default:
2257 fprintf(stderr,
2258 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002259 PyCode_Addr2Line(f->f_code, f->f_lasti),
2260 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002261 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002262 why = WHY_EXCEPTION;
2263 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002264
2265#ifdef CASE_TOO_BIG
2266 }
2267#endif
2268
Guido van Rossum374a9221991-04-04 10:40:29 +00002269 } /* switch */
2270
2271 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002272
Guido van Rossum374a9221991-04-04 10:40:29 +00002273 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002274
Guido van Rossum374a9221991-04-04 10:40:29 +00002275 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002276 if (err == 0 && x != NULL) {
2277#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002278 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002279 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002280 fprintf(stderr,
2281 "XXX undetected error\n");
2282 else
2283#endif
2284 continue; /* Normal, fast path */
2285 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002286 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002287 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002288 err = 0;
2289 }
2290
Guido van Rossum374a9221991-04-04 10:40:29 +00002291 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002292
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002293 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002294 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002295 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002296 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002297 why = WHY_EXCEPTION;
2298 }
2299 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002300#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002301 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002302 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002303 if (PyErr_Occurred()) {
Jeremy Hylton904ed862003-11-05 17:29:35 +00002304 char buf[1024];
2305 sprintf(buf, "Stack unwind with exception "
2306 "set and why=%d", why);
2307 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002308 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002309 }
2310#endif
2311
2312 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002313
Guido van Rossum374a9221991-04-04 10:40:29 +00002314 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002315 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002316
Fred Drake8f51f542001-10-04 14:48:42 +00002317 if (tstate->c_tracefunc != NULL)
2318 call_exc_trace(tstate->c_tracefunc,
2319 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002320 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002321
Guido van Rossum374a9221991-04-04 10:40:29 +00002322 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002323
Guido van Rossum374a9221991-04-04 10:40:29 +00002324 if (why == WHY_RERAISE)
2325 why = WHY_EXCEPTION;
2326
2327 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002328
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002329fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002330 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002331 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002332
Tim Peters8a5c3c72004-04-05 19:36:21 +00002333 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002334 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2335 /* For a continue inside a try block,
2336 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002337 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2338 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002339 why = WHY_NOT;
2340 JUMPTO(PyInt_AS_LONG(retval));
2341 Py_DECREF(retval);
2342 break;
2343 }
2344
Guido van Rossum374a9221991-04-04 10:40:29 +00002345 while (STACK_LEVEL() > b->b_level) {
2346 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002347 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002348 }
2349 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2350 why = WHY_NOT;
2351 JUMPTO(b->b_handler);
2352 break;
2353 }
2354 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002355 (b->b_type == SETUP_EXCEPT &&
2356 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002357 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002358 PyObject *exc, *val, *tb;
2359 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002360 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002361 val = Py_None;
2362 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002363 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002364 /* Make the raw exception data
2365 available to the handler,
2366 so a program can emulate the
2367 Python main loop. Don't do
2368 this for 'finally'. */
2369 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002370 PyErr_NormalizeException(
2371 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002372 set_exc_info(tstate,
2373 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002374 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002375 if (tb == NULL) {
2376 Py_INCREF(Py_None);
2377 PUSH(Py_None);
2378 } else
2379 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002380 PUSH(val);
2381 PUSH(exc);
2382 }
2383 else {
Raymond Hettinger06032cb2004-04-06 09:37:35 +00002384 if (why & (WHY_RETURN | WHY_CONTINUE))
Guido van Rossum374a9221991-04-04 10:40:29 +00002385 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002386 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002387 PUSH(v);
2388 }
2389 why = WHY_NOT;
2390 JUMPTO(b->b_handler);
2391 break;
2392 }
2393 } /* unwind stack */
2394
2395 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002396
Guido van Rossum374a9221991-04-04 10:40:29 +00002397 if (why != WHY_NOT)
2398 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002399
Guido van Rossum374a9221991-04-04 10:40:29 +00002400 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002401
Tim Peters8a5c3c72004-04-05 19:36:21 +00002402 assert(why != WHY_YIELD);
2403 /* Pop remaining stack entries. */
2404 while (!EMPTY()) {
2405 v = POP();
2406 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002407 }
2408
Tim Peters8a5c3c72004-04-05 19:36:21 +00002409 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002410 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002411
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002412fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002413 if (tstate->use_tracing) {
2414 if (tstate->c_tracefunc
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002415 && (why == WHY_RETURN || why == WHY_YIELD)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002416 if (call_trace(tstate->c_tracefunc,
2417 tstate->c_traceobj, f,
2418 PyTrace_RETURN, retval)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002419 Py_XDECREF(retval);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002420 retval = NULL;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002421 why = WHY_EXCEPTION;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002422 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002423 }
Fred Drake8f51f542001-10-04 14:48:42 +00002424 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002425 if (why == WHY_EXCEPTION)
2426 call_trace_protected(tstate->c_profilefunc,
2427 tstate->c_profileobj, f,
2428 PyTrace_RETURN);
2429 else if (call_trace(tstate->c_profilefunc,
2430 tstate->c_profileobj, f,
2431 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002432 Py_XDECREF(retval);
2433 retval = NULL;
2434 why = WHY_EXCEPTION;
2435 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002436 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002437 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002438
Guido van Rossuma027efa1997-05-05 20:56:21 +00002439 reset_exc_info(tstate);
2440
Tim Peters5ca576e2001-06-18 22:08:13 +00002441 /* pop frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +00002442 exit_eval_frame:
2443 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002444 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002445
Guido van Rossum96a42c81992-01-12 02:29:51 +00002446 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002447}
2448
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002449/* this is gonna seem *real weird*, but if you put some other code between
2450 eval_frame() and PyEval_EvalCodeEx() you will need to adjust the test in
2451 the if statement in Misc/gdbinit:ppystack */
2452
Tim Peters6d6c1a32001-08-02 04:15:00 +00002453PyObject *
2454PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002455 PyObject **args, int argcount, PyObject **kws, int kwcount,
2456 PyObject **defs, int defcount, PyObject *closure)
2457{
2458 register PyFrameObject *f;
2459 register PyObject *retval = NULL;
2460 register PyObject **fastlocals, **freevars;
2461 PyThreadState *tstate = PyThreadState_GET();
2462 PyObject *x, *u;
2463
2464 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00002465 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002466 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002467 return NULL;
2468 }
2469
Jeremy Hylton985eba52003-02-05 23:13:00 +00002470 assert(globals != NULL);
2471 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002472 if (f == NULL)
2473 return NULL;
2474
2475 fastlocals = f->f_localsplus;
2476 freevars = f->f_localsplus + f->f_nlocals;
2477
2478 if (co->co_argcount > 0 ||
2479 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2480 int i;
2481 int n = argcount;
2482 PyObject *kwdict = NULL;
2483 if (co->co_flags & CO_VARKEYWORDS) {
2484 kwdict = PyDict_New();
2485 if (kwdict == NULL)
2486 goto fail;
2487 i = co->co_argcount;
2488 if (co->co_flags & CO_VARARGS)
2489 i++;
2490 SETLOCAL(i, kwdict);
2491 }
2492 if (argcount > co->co_argcount) {
2493 if (!(co->co_flags & CO_VARARGS)) {
2494 PyErr_Format(PyExc_TypeError,
2495 "%.200s() takes %s %d "
2496 "%sargument%s (%d given)",
2497 PyString_AsString(co->co_name),
2498 defcount ? "at most" : "exactly",
2499 co->co_argcount,
2500 kwcount ? "non-keyword " : "",
2501 co->co_argcount == 1 ? "" : "s",
2502 argcount);
2503 goto fail;
2504 }
2505 n = co->co_argcount;
2506 }
2507 for (i = 0; i < n; i++) {
2508 x = args[i];
2509 Py_INCREF(x);
2510 SETLOCAL(i, x);
2511 }
2512 if (co->co_flags & CO_VARARGS) {
2513 u = PyTuple_New(argcount - n);
2514 if (u == NULL)
2515 goto fail;
2516 SETLOCAL(co->co_argcount, u);
2517 for (i = n; i < argcount; i++) {
2518 x = args[i];
2519 Py_INCREF(x);
2520 PyTuple_SET_ITEM(u, i-n, x);
2521 }
2522 }
2523 for (i = 0; i < kwcount; i++) {
2524 PyObject *keyword = kws[2*i];
2525 PyObject *value = kws[2*i + 1];
2526 int j;
2527 if (keyword == NULL || !PyString_Check(keyword)) {
2528 PyErr_Format(PyExc_TypeError,
2529 "%.200s() keywords must be strings",
2530 PyString_AsString(co->co_name));
2531 goto fail;
2532 }
2533 /* XXX slow -- speed up using dictionary? */
2534 for (j = 0; j < co->co_argcount; j++) {
2535 PyObject *nm = PyTuple_GET_ITEM(
2536 co->co_varnames, j);
2537 int cmp = PyObject_RichCompareBool(
2538 keyword, nm, Py_EQ);
2539 if (cmp > 0)
2540 break;
2541 else if (cmp < 0)
2542 goto fail;
2543 }
2544 /* Check errors from Compare */
2545 if (PyErr_Occurred())
2546 goto fail;
2547 if (j >= co->co_argcount) {
2548 if (kwdict == NULL) {
2549 PyErr_Format(PyExc_TypeError,
2550 "%.200s() got an unexpected "
2551 "keyword argument '%.400s'",
2552 PyString_AsString(co->co_name),
2553 PyString_AsString(keyword));
2554 goto fail;
2555 }
2556 PyDict_SetItem(kwdict, keyword, value);
2557 }
2558 else {
2559 if (GETLOCAL(j) != NULL) {
2560 PyErr_Format(PyExc_TypeError,
2561 "%.200s() got multiple "
2562 "values for keyword "
2563 "argument '%.400s'",
2564 PyString_AsString(co->co_name),
2565 PyString_AsString(keyword));
2566 goto fail;
2567 }
2568 Py_INCREF(value);
2569 SETLOCAL(j, value);
2570 }
2571 }
2572 if (argcount < co->co_argcount) {
2573 int m = co->co_argcount - defcount;
2574 for (i = argcount; i < m; i++) {
2575 if (GETLOCAL(i) == NULL) {
2576 PyErr_Format(PyExc_TypeError,
2577 "%.200s() takes %s %d "
2578 "%sargument%s (%d given)",
2579 PyString_AsString(co->co_name),
2580 ((co->co_flags & CO_VARARGS) ||
2581 defcount) ? "at least"
2582 : "exactly",
2583 m, kwcount ? "non-keyword " : "",
2584 m == 1 ? "" : "s", i);
2585 goto fail;
2586 }
2587 }
2588 if (n > m)
2589 i = n - m;
2590 else
2591 i = 0;
2592 for (; i < defcount; i++) {
2593 if (GETLOCAL(m+i) == NULL) {
2594 PyObject *def = defs[i];
2595 Py_INCREF(def);
2596 SETLOCAL(m+i, def);
2597 }
2598 }
2599 }
2600 }
2601 else {
2602 if (argcount > 0 || kwcount > 0) {
2603 PyErr_Format(PyExc_TypeError,
2604 "%.200s() takes no arguments (%d given)",
2605 PyString_AsString(co->co_name),
2606 argcount + kwcount);
2607 goto fail;
2608 }
2609 }
2610 /* Allocate and initialize storage for cell vars, and copy free
2611 vars into frame. This isn't too efficient right now. */
2612 if (f->f_ncells) {
2613 int i = 0, j = 0, nargs, found;
2614 char *cellname, *argname;
2615 PyObject *c;
2616
2617 nargs = co->co_argcount;
2618 if (co->co_flags & CO_VARARGS)
2619 nargs++;
2620 if (co->co_flags & CO_VARKEYWORDS)
2621 nargs++;
2622
2623 /* Check for cells that shadow args */
2624 for (i = 0; i < f->f_ncells && j < nargs; ++i) {
2625 cellname = PyString_AS_STRING(
2626 PyTuple_GET_ITEM(co->co_cellvars, i));
2627 found = 0;
2628 while (j < nargs) {
2629 argname = PyString_AS_STRING(
2630 PyTuple_GET_ITEM(co->co_varnames, j));
2631 if (strcmp(cellname, argname) == 0) {
2632 c = PyCell_New(GETLOCAL(j));
2633 if (c == NULL)
2634 goto fail;
2635 GETLOCAL(f->f_nlocals + i) = c;
2636 found = 1;
2637 break;
2638 }
2639 j++;
2640 }
2641 if (found == 0) {
2642 c = PyCell_New(NULL);
2643 if (c == NULL)
2644 goto fail;
2645 SETLOCAL(f->f_nlocals + i, c);
2646 }
2647 }
2648 /* Initialize any that are left */
2649 while (i < f->f_ncells) {
2650 c = PyCell_New(NULL);
2651 if (c == NULL)
2652 goto fail;
2653 SETLOCAL(f->f_nlocals + i, c);
2654 i++;
2655 }
2656 }
2657 if (f->f_nfreevars) {
2658 int i;
2659 for (i = 0; i < f->f_nfreevars; ++i) {
2660 PyObject *o = PyTuple_GET_ITEM(closure, i);
2661 Py_INCREF(o);
2662 freevars[f->f_ncells + i] = o;
2663 }
2664 }
2665
Tim Peters5ca576e2001-06-18 22:08:13 +00002666 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002667 /* Don't need to keep the reference to f_back, it will be set
2668 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002669 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002670 f->f_back = NULL;
2671
Jeremy Hylton985eba52003-02-05 23:13:00 +00002672 PCALL(PCALL_GENERATOR);
2673
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002674 /* Create a new generator that owns the ready to run frame
2675 * and return that as the value. */
Tim Peters5ca576e2001-06-18 22:08:13 +00002676 return gen_new(f);
2677 }
2678
2679 retval = eval_frame(f);
2680
2681 fail: /* Jump here from prelude on failure */
2682
Tim Petersb13680b2001-11-27 23:29:29 +00002683 /* decref'ing the frame can cause __del__ methods to get invoked,
2684 which can call back into Python. While we're done with the
2685 current Python frame (f), the associated C stack is still in use,
2686 so recursion_depth must be boosted for the duration.
2687 */
2688 assert(tstate != NULL);
2689 ++tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002690 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00002691 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002692 return retval;
2693}
2694
2695
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002696/* Implementation notes for set_exc_info() and reset_exc_info():
2697
2698- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2699 'exc_traceback'. These always travel together.
2700
2701- tstate->curexc_ZZZ is the "hot" exception that is set by
2702 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2703
2704- Once an exception is caught by an except clause, it is transferred
2705 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2706 can pick it up. This is the primary task of set_exc_info().
2707
2708- Now let me explain the complicated dance with frame->f_exc_ZZZ.
2709
2710 Long ago, when none of this existed, there were just a few globals:
2711 one set corresponding to the "hot" exception, and one set
2712 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2713 globals; they were simply stored as sys.exc_ZZZ. For backwards
2714 compatibility, they still are!) The problem was that in code like
2715 this:
2716
2717 try:
2718 "something that may fail"
2719 except "some exception":
2720 "do something else first"
2721 "print the exception from sys.exc_ZZZ."
2722
2723 if "do something else first" invoked something that raised and caught
2724 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2725 cause of subtle bugs. I fixed this by changing the semantics as
2726 follows:
2727
2728 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2729 *in that frame*.
2730
2731 - But initially, and as long as no exception is caught in a given
2732 frame, sys.exc_ZZZ will hold the last exception caught in the
2733 previous frame (or the frame before that, etc.).
2734
2735 The first bullet fixed the bug in the above example. The second
2736 bullet was for backwards compatibility: it was (and is) common to
2737 have a function that is called when an exception is caught, and to
2738 have that function access the caught exception via sys.exc_ZZZ.
2739 (Example: traceback.print_exc()).
2740
2741 At the same time I fixed the problem that sys.exc_ZZZ weren't
2742 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2743 but that's really a separate improvement.
2744
2745 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2746 variables to what they were before the current frame was called. The
2747 set_exc_info() function saves them on the frame so that
2748 reset_exc_info() can restore them. The invariant is that
2749 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2750 exception (where "catching" an exception applies only to successful
2751 except clauses); and if the current frame ever caught an exception,
2752 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2753 at the start of the current frame.
2754
2755*/
2756
Guido van Rossuma027efa1997-05-05 20:56:21 +00002757static void
Guido van Rossumac7be682001-01-17 15:42:30 +00002758set_exc_info(PyThreadState *tstate,
2759 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002760{
2761 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002762 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00002763
Guido van Rossuma027efa1997-05-05 20:56:21 +00002764 frame = tstate->frame;
2765 if (frame->f_exc_type == NULL) {
2766 /* This frame didn't catch an exception before */
2767 /* Save previous exception of this thread in this frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002768 if (tstate->exc_type == NULL) {
2769 Py_INCREF(Py_None);
2770 tstate->exc_type = Py_None;
2771 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002772 tmp_type = frame->f_exc_type;
2773 tmp_value = frame->f_exc_value;
2774 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002775 Py_XINCREF(tstate->exc_type);
2776 Py_XINCREF(tstate->exc_value);
2777 Py_XINCREF(tstate->exc_traceback);
2778 frame->f_exc_type = tstate->exc_type;
2779 frame->f_exc_value = tstate->exc_value;
2780 frame->f_exc_traceback = tstate->exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002781 Py_XDECREF(tmp_type);
2782 Py_XDECREF(tmp_value);
2783 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002784 }
2785 /* Set new exception for this thread */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002786 tmp_type = tstate->exc_type;
2787 tmp_value = tstate->exc_value;
2788 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002789 Py_XINCREF(type);
2790 Py_XINCREF(value);
2791 Py_XINCREF(tb);
2792 tstate->exc_type = type;
2793 tstate->exc_value = value;
2794 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002795 Py_XDECREF(tmp_type);
2796 Py_XDECREF(tmp_value);
2797 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002798 /* For b/w compatibility */
2799 PySys_SetObject("exc_type", type);
2800 PySys_SetObject("exc_value", value);
2801 PySys_SetObject("exc_traceback", tb);
2802}
2803
2804static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002805reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002806{
2807 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002808 PyObject *tmp_type, *tmp_value, *tmp_tb;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002809 frame = tstate->frame;
2810 if (frame->f_exc_type != NULL) {
2811 /* This frame caught an exception */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002812 tmp_type = tstate->exc_type;
2813 tmp_value = tstate->exc_value;
2814 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002815 Py_XINCREF(frame->f_exc_type);
2816 Py_XINCREF(frame->f_exc_value);
2817 Py_XINCREF(frame->f_exc_traceback);
2818 tstate->exc_type = frame->f_exc_type;
2819 tstate->exc_value = frame->f_exc_value;
2820 tstate->exc_traceback = frame->f_exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002821 Py_XDECREF(tmp_type);
2822 Py_XDECREF(tmp_value);
2823 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002824 /* For b/w compatibility */
2825 PySys_SetObject("exc_type", frame->f_exc_type);
2826 PySys_SetObject("exc_value", frame->f_exc_value);
2827 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
2828 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002829 tmp_type = frame->f_exc_type;
2830 tmp_value = frame->f_exc_value;
2831 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002832 frame->f_exc_type = NULL;
2833 frame->f_exc_value = NULL;
2834 frame->f_exc_traceback = NULL;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002835 Py_XDECREF(tmp_type);
2836 Py_XDECREF(tmp_value);
2837 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002838}
2839
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002840/* Logic for the raise statement (too complicated for inlining).
2841 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00002842static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002843do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002844{
Guido van Rossumd295f121998-04-09 21:39:57 +00002845 if (type == NULL) {
2846 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00002847 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumd295f121998-04-09 21:39:57 +00002848 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
2849 value = tstate->exc_value;
2850 tb = tstate->exc_traceback;
2851 Py_XINCREF(type);
2852 Py_XINCREF(value);
2853 Py_XINCREF(tb);
2854 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002855
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002856 /* We support the following forms of raise:
2857 raise <class>, <classinstance>
2858 raise <class>, <argument tuple>
2859 raise <class>, None
2860 raise <class>, <argument>
2861 raise <classinstance>, None
2862 raise <string>, <object>
2863 raise <string>, None
2864
2865 An omitted second argument is the same as None.
2866
2867 In addition, raise <tuple>, <anything> is the same as
2868 raising the tuple's first item (and it better have one!);
2869 this rule is applied recursively.
2870
2871 Finally, an optional third argument can be supplied, which
2872 gives the traceback to be substituted (useful when
2873 re-raising an exception after examining it). */
2874
2875 /* First, check the traceback argument, replacing None with
2876 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002877 if (tb == Py_None) {
2878 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002879 tb = NULL;
2880 }
2881 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002882 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002883 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002884 goto raise_error;
2885 }
2886
2887 /* Next, replace a missing value with None */
2888 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002889 value = Py_None;
2890 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002891 }
2892
2893 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00002894 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
2895 PyObject *tmp = type;
2896 type = PyTuple_GET_ITEM(type, 0);
2897 Py_INCREF(type);
2898 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002899 }
2900
Tim Petersafb2c802002-04-18 18:06:20 +00002901 if (PyString_CheckExact(type))
2902 /* Raising builtin string is deprecated but still allowed --
2903 * do nothing. Raising an instance of a new-style str
2904 * subclass is right out. */
Neal Norwitz37aa0662003-01-10 15:31:15 +00002905 PyErr_Warn(PyExc_PendingDeprecationWarning,
2906 "raising a string exception is deprecated");
Barry Warsaw4249f541997-08-22 21:26:19 +00002907
2908 else if (PyClass_Check(type))
2909 PyErr_NormalizeException(&type, &value, &tb);
2910
Guido van Rossumb209a111997-04-29 18:18:01 +00002911 else if (PyInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002912 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002913 if (value != Py_None) {
2914 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002915 "instance exception may not have a separate value");
2916 goto raise_error;
2917 }
2918 else {
2919 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00002920 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002921 value = type;
Guido van Rossumb209a111997-04-29 18:18:01 +00002922 type = (PyObject*) ((PyInstanceObject*)type)->in_class;
2923 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002924 }
2925 }
2926 else {
2927 /* Not something you can raise. You get an exception
2928 anyway, just not what you specified :-) */
Jeremy Hylton960d9482001-04-27 02:25:33 +00002929 PyErr_Format(PyExc_TypeError,
Neal Norwitz37aa0662003-01-10 15:31:15 +00002930 "exceptions must be classes, instances, or "
2931 "strings (deprecated), not %s",
2932 type->ob_type->tp_name);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002933 goto raise_error;
2934 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002935 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002936 if (tb == NULL)
2937 return WHY_EXCEPTION;
2938 else
2939 return WHY_RERAISE;
2940 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00002941 Py_XDECREF(value);
2942 Py_XDECREF(type);
2943 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002944 return WHY_EXCEPTION;
2945}
2946
Tim Petersd6d010b2001-06-21 02:49:55 +00002947/* Iterate v argcnt times and store the results on the stack (via decreasing
2948 sp). Return 1 for success, 0 if error. */
2949
Barry Warsawe42b18f1997-08-25 22:13:04 +00002950static int
Tim Petersd6d010b2001-06-21 02:49:55 +00002951unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00002952{
Tim Petersd6d010b2001-06-21 02:49:55 +00002953 int i = 0;
2954 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00002955 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00002956
Tim Petersd6d010b2001-06-21 02:49:55 +00002957 assert(v != NULL);
2958
2959 it = PyObject_GetIter(v);
2960 if (it == NULL)
2961 goto Error;
2962
2963 for (; i < argcnt; i++) {
2964 w = PyIter_Next(it);
2965 if (w == NULL) {
2966 /* Iterator done, via error or exhaustion. */
2967 if (!PyErr_Occurred()) {
2968 PyErr_Format(PyExc_ValueError,
2969 "need more than %d value%s to unpack",
2970 i, i == 1 ? "" : "s");
2971 }
2972 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00002973 }
2974 *--sp = w;
2975 }
Tim Petersd6d010b2001-06-21 02:49:55 +00002976
2977 /* We better have exhausted the iterator now. */
2978 w = PyIter_Next(it);
2979 if (w == NULL) {
2980 if (PyErr_Occurred())
2981 goto Error;
2982 Py_DECREF(it);
2983 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00002984 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00002985 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00002986 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00002987 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00002988Error:
Barry Warsaw91010551997-08-25 22:30:51 +00002989 for (; i > 0; i--, sp++)
2990 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00002991 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00002992 return 0;
2993}
2994
2995
Guido van Rossum96a42c81992-01-12 02:29:51 +00002996#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00002997static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002998prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002999{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003000 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003001 if (PyObject_Print(v, stdout, 0) != 0)
3002 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003003 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003004 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003005}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003006#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003007
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003008static void
Fred Drake5755ce62001-06-27 19:19:46 +00003009call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003010{
Guido van Rossumb209a111997-04-29 18:18:01 +00003011 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003012 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003013 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003014 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003015 value = Py_None;
3016 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003017 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003018 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003019 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003020 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003021 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003022 }
Fred Drake5755ce62001-06-27 19:19:46 +00003023 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003024 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003025 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003026 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003027 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003028 Py_XDECREF(type);
3029 Py_XDECREF(value);
3030 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003031 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003032}
3033
Fred Drake4ec5d562001-10-04 19:26:43 +00003034static void
3035call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3036 int what)
3037{
3038 PyObject *type, *value, *traceback;
3039 int err;
3040 PyErr_Fetch(&type, &value, &traceback);
3041 err = call_trace(func, obj, frame, what, NULL);
3042 if (err == 0)
3043 PyErr_Restore(type, value, traceback);
3044 else {
3045 Py_XDECREF(type);
3046 Py_XDECREF(value);
3047 Py_XDECREF(traceback);
3048 }
3049}
3050
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003051static int
Fred Drake5755ce62001-06-27 19:19:46 +00003052call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3053 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003054{
Fred Drake5755ce62001-06-27 19:19:46 +00003055 register PyThreadState *tstate = frame->f_tstate;
3056 int result;
3057 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003058 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003059 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003060 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003061 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003062 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3063 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003064 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003065 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003066}
3067
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003068PyObject *
3069_PyEval_CallTracing(PyObject *func, PyObject *args)
3070{
3071 PyFrameObject *frame = PyEval_GetFrame();
3072 PyThreadState *tstate = frame->f_tstate;
3073 int save_tracing = tstate->tracing;
3074 int save_use_tracing = tstate->use_tracing;
3075 PyObject *result;
3076
3077 tstate->tracing = 0;
3078 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3079 || (tstate->c_profilefunc != NULL));
3080 result = PyObject_Call(func, args, NULL);
3081 tstate->tracing = save_tracing;
3082 tstate->use_tracing = save_use_tracing;
3083 return result;
3084}
3085
Michael W. Hudson006c7522002-11-08 13:08:46 +00003086static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003087maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003088 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3089 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003090{
3091 /* The theory of SET_LINENO-less tracing.
Tim Peters8a5c3c72004-04-05 19:36:21 +00003092
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003093 In a nutshell, we use the co_lnotab field of the code object
3094 to tell when execution has moved onto a different line.
3095
3096 As mentioned above, the basic idea is so set things up so
3097 that
3098
3099 *instr_lb <= frame->f_lasti < *instr_ub
3100
3101 is true so long as execution does not change lines.
3102
3103 This is all fairly simple. Digging the information out of
3104 co_lnotab takes some work, but is conceptually clear.
3105
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003106 Somewhat harder to explain is why we don't *always* call the
3107 line trace function when the above test fails.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003108
3109 Consider this code:
3110
3111 1: def f(a):
3112 2: if a:
3113 3: print 1
3114 4: else:
3115 5: print 2
3116
3117 which compiles to this:
3118
3119 2 0 LOAD_FAST 0 (a)
3120 3 JUMP_IF_FALSE 9 (to 15)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003121 6 POP_TOP
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003122
3123 3 7 LOAD_CONST 1 (1)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003124 10 PRINT_ITEM
3125 11 PRINT_NEWLINE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003126 12 JUMP_FORWARD 6 (to 21)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003127 >> 15 POP_TOP
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003128
3129 5 16 LOAD_CONST 2 (2)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003130 19 PRINT_ITEM
3131 20 PRINT_NEWLINE
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003132 >> 21 LOAD_CONST 0 (None)
3133 24 RETURN_VALUE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003134
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003135 If 'a' is false, execution will jump to instruction at offset
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003136 15 and the co_lnotab will claim that execution has moved to
3137 line 3. This is at best misleading. In this case we could
3138 associate the POP_TOP with line 4, but that doesn't make
3139 sense in all cases (I think).
3140
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003141 What we do is only call the line trace function if the co_lnotab
3142 indicates we have jumped to the *start* of a line, i.e. if the
3143 current instruction offset matches the offset given for the
3144 start of a line by the co_lnotab.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003145
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003146 This also takes care of the situation where 'a' is true.
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003147 Execution will jump from instruction offset 12 to offset 21.
3148 Then the co_lnotab would imply that execution has moved to line
3149 5, which is again misleading.
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003150
3151 Why do we set f_lineno when tracing? Well, consider the code
3152 above when 'a' is true. If stepping through this with 'n' in
3153 pdb, you would stop at line 1 with a "call" type event, then
3154 line events on lines 2 and 3, then a "return" type event -- but
3155 you would be shown line 5 during this event. This is a change
3156 from the behaviour in 2.2 and before, and I've found it
3157 confusing in practice. By setting and using f_lineno when
3158 tracing, one can report a line number different from that
3159 suggested by f_lasti on this one occasion where it's desirable.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003160 */
3161
Michael W. Hudson006c7522002-11-08 13:08:46 +00003162 int result = 0;
3163
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003164 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003165 PyCodeObject* co = frame->f_code;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003166 int size, addr, line;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003167 unsigned char* p;
3168
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003169 size = PyString_GET_SIZE(co->co_lnotab) / 2;
3170 p = (unsigned char*)PyString_AS_STRING(co->co_lnotab);
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003171
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003172 addr = 0;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003173 line = co->co_firstlineno;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003174
3175 /* possible optimization: if f->f_lasti == instr_ub
3176 (likely to be a common case) then we already know
3177 instr_lb -- if we stored the matching value of p
3178 somwhere we could skip the first while loop. */
3179
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003180 /* see comments in compile.c for the description of
3181 co_lnotab. A point to remember: increments to p
3182 should come in pairs -- although we don't care about
3183 the line increments here, treating them as byte
3184 increments gets confusing, to say the least. */
3185
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003186 while (size > 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003187 if (addr + *p > frame->f_lasti)
3188 break;
3189 addr += *p++;
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003190 if (*p) *instr_lb = addr;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003191 line += *p++;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003192 --size;
3193 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003194
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003195 if (addr == frame->f_lasti) {
3196 frame->f_lineno = line;
Tim Peters8a5c3c72004-04-05 19:36:21 +00003197 result = call_trace(func, obj, frame,
Michael W. Hudson006c7522002-11-08 13:08:46 +00003198 PyTrace_LINE, Py_None);
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003199 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003200
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003201 if (size > 0) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003202 while (--size >= 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003203 addr += *p++;
3204 if (*p++)
3205 break;
3206 }
3207 *instr_ub = addr;
3208 }
3209 else {
3210 *instr_ub = INT_MAX;
3211 }
3212 }
Armin Rigobf57a142004-03-22 19:24:58 +00003213 else if (frame->f_lasti <= *instr_prev) {
3214 /* jumping back in the same line forces a trace event */
Tim Peters8a5c3c72004-04-05 19:36:21 +00003215 result = call_trace(func, obj, frame,
Armin Rigobf57a142004-03-22 19:24:58 +00003216 PyTrace_LINE, Py_None);
3217 }
3218 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003219 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003220}
3221
Fred Drake5755ce62001-06-27 19:19:46 +00003222void
3223PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003224{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003225 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003226 PyObject *temp = tstate->c_profileobj;
3227 Py_XINCREF(arg);
3228 tstate->c_profilefunc = NULL;
3229 tstate->c_profileobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003230 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003231 Py_XDECREF(temp);
3232 tstate->c_profilefunc = func;
3233 tstate->c_profileobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003234 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003235}
3236
3237void
3238PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3239{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003240 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003241 PyObject *temp = tstate->c_traceobj;
3242 Py_XINCREF(arg);
3243 tstate->c_tracefunc = NULL;
3244 tstate->c_traceobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003245 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003246 Py_XDECREF(temp);
3247 tstate->c_tracefunc = func;
3248 tstate->c_traceobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003249 tstate->use_tracing = ((func != NULL)
3250 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003251}
3252
Guido van Rossumb209a111997-04-29 18:18:01 +00003253PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003254PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003255{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003256 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003257 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003258 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003259 else
3260 return current_frame->f_builtins;
3261}
3262
Guido van Rossumb209a111997-04-29 18:18:01 +00003263PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003264PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003265{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003266 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003267 if (current_frame == NULL)
3268 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003269 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003270 return current_frame->f_locals;
3271}
3272
Guido van Rossumb209a111997-04-29 18:18:01 +00003273PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003274PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003275{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003276 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003277 if (current_frame == NULL)
3278 return NULL;
3279 else
3280 return current_frame->f_globals;
3281}
3282
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003283PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003284PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003285{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003286 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003287 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003288}
3289
Guido van Rossum6135a871995-01-09 17:53:26 +00003290int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003291PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003292{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003293 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003294 return current_frame == NULL ? 0 : current_frame->f_restricted;
3295}
3296
Guido van Rossumbe270261997-05-22 22:26:18 +00003297int
Tim Peters5ba58662001-07-16 02:29:45 +00003298PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003299{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003300 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003301 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003302
3303 if (current_frame != NULL) {
3304 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003305 const int compilerflags = codeflags & PyCF_MASK;
3306 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003307 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003308 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003309 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003310#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003311 if (codeflags & CO_GENERATOR_ALLOWED) {
3312 result = 1;
3313 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3314 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003315#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003316 }
3317 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003318}
3319
3320int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003321Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003322{
Guido van Rossumb209a111997-04-29 18:18:01 +00003323 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003324 if (f == NULL)
3325 return 0;
3326 if (!PyFile_SoftSpace(f, 0))
3327 return 0;
3328 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003329}
3330
Guido van Rossum3f5da241990-12-20 15:06:42 +00003331
Guido van Rossum681d79a1995-07-18 14:51:37 +00003332/* External interface to call any callable object.
3333 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003334
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003335#undef PyEval_CallObject
3336/* for backward compatibility: export this interface */
3337
Guido van Rossumb209a111997-04-29 18:18:01 +00003338PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003339PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003340{
Guido van Rossumb209a111997-04-29 18:18:01 +00003341 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003342}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003343#define PyEval_CallObject(func,arg) \
3344 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003345
Guido van Rossumb209a111997-04-29 18:18:01 +00003346PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003347PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003348{
Jeremy Hylton52820442001-01-03 23:52:36 +00003349 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003350
3351 if (arg == NULL)
Guido van Rossumb209a111997-04-29 18:18:01 +00003352 arg = PyTuple_New(0);
3353 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003354 PyErr_SetString(PyExc_TypeError,
3355 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003356 return NULL;
3357 }
3358 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003359 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003360
Guido van Rossumb209a111997-04-29 18:18:01 +00003361 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003362 PyErr_SetString(PyExc_TypeError,
3363 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003364 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003365 return NULL;
3366 }
3367
Tim Peters6d6c1a32001-08-02 04:15:00 +00003368 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003369 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003370 return result;
3371}
3372
Tim Peters6d6c1a32001-08-02 04:15:00 +00003373char *
3374PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003375{
3376 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003377 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003378 else if (PyFunction_Check(func))
3379 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3380 else if (PyCFunction_Check(func))
3381 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3382 else if (PyClass_Check(func))
3383 return PyString_AsString(((PyClassObject*)func)->cl_name);
3384 else if (PyInstance_Check(func)) {
3385 return PyString_AsString(
3386 ((PyInstanceObject*)func)->in_class->cl_name);
3387 } else {
3388 return func->ob_type->tp_name;
3389 }
3390}
3391
Tim Peters6d6c1a32001-08-02 04:15:00 +00003392char *
3393PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003394{
3395 if (PyMethod_Check(func))
3396 return "()";
3397 else if (PyFunction_Check(func))
3398 return "()";
3399 else if (PyCFunction_Check(func))
3400 return "()";
3401 else if (PyClass_Check(func))
3402 return " constructor";
3403 else if (PyInstance_Check(func)) {
3404 return " instance";
3405 } else {
3406 return " object";
3407 }
3408}
3409
Jeremy Hylton52820442001-01-03 23:52:36 +00003410#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
3411
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003412static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003413err_args(PyObject *func, int flags, int nargs)
3414{
3415 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003416 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003417 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003418 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003419 nargs);
3420 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003421 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003422 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003423 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003424 nargs);
3425}
3426
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003427#define BEGIN_C_TRACE \
3428if (tstate->use_tracing) { \
3429 if (tstate->c_profilefunc != NULL) { \
3430 PyObject *func_name = \
3431 PyString_FromString (((PyCFunctionObject *) \
3432 func)->m_ml->ml_name); \
3433 are_tracing = 1; \
3434 if (call_trace(tstate->c_profilefunc, \
3435 tstate->c_profileobj, \
3436 tstate->frame, PyTrace_C_CALL, \
3437 func_name)) \
3438 { return NULL; } \
3439 Py_DECREF (func_name); \
3440 } \
3441 }
3442
3443#define END_C_TRACE \
3444 if (tstate->use_tracing && are_tracing) { \
3445 if (tstate->c_profilefunc != NULL) { \
3446 if (x == NULL) { \
3447 if (call_trace (tstate->c_profilefunc, \
3448 tstate->c_profileobj, \
3449 tstate->frame, PyTrace_C_EXCEPTION, \
3450 NULL)) \
3451 { return NULL; } \
3452 } else { \
3453 if (call_trace(tstate->c_profilefunc, \
3454 tstate->c_profileobj, \
3455 tstate->frame, PyTrace_C_RETURN, \
3456 NULL)) \
3457 { return NULL; } \
3458 } \
3459 } \
3460 }
3461
3462
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003463static PyObject *
3464call_function(PyObject ***pp_stack, int oparg)
3465{
3466 int na = oparg & 0xff;
3467 int nk = (oparg>>8) & 0xff;
3468 int n = na + 2 * nk;
3469 PyObject **pfunc = (*pp_stack) - n - 1;
3470 PyObject *func = *pfunc;
3471 PyObject *x, *w;
3472
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003473 int are_tracing = 0;
3474
3475 PyThreadState *tstate = PyThreadState_GET();
3476
Jeremy Hylton985eba52003-02-05 23:13:00 +00003477 /* Always dispatch PyCFunction first, because these are
3478 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003479 */
3480 if (PyCFunction_Check(func) && nk == 0) {
3481 int flags = PyCFunction_GET_FLAGS(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003482 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003483 if (flags & (METH_NOARGS | METH_O)) {
3484 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3485 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003486 if (flags & METH_NOARGS && na == 0) {
3487 BEGIN_C_TRACE
Jeremy Hylton192690e2002-08-16 18:36:11 +00003488 x = (*meth)(self, NULL);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003489 END_C_TRACE
3490 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003491 else if (flags & METH_O && na == 1) {
3492 PyObject *arg = EXT_POP(*pp_stack);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003493 BEGIN_C_TRACE
Jeremy Hylton192690e2002-08-16 18:36:11 +00003494 x = (*meth)(self, arg);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003495 END_C_TRACE
Jeremy Hylton192690e2002-08-16 18:36:11 +00003496 Py_DECREF(arg);
3497 }
3498 else {
3499 err_args(func, flags, na);
3500 x = NULL;
3501 }
3502 }
3503 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003504 PyObject *callargs;
3505 callargs = load_args(pp_stack, na);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003506 BEGIN_C_TRACE
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003507 x = PyCFunction_Call(func, callargs, NULL);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003508 END_C_TRACE
Tim Peters8a5c3c72004-04-05 19:36:21 +00003509 Py_XDECREF(callargs);
3510 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003511 } else {
3512 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3513 /* optimize access to bound methods */
3514 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003515 PCALL(PCALL_METHOD);
3516 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003517 Py_INCREF(self);
3518 func = PyMethod_GET_FUNCTION(func);
3519 Py_INCREF(func);
3520 Py_DECREF(*pfunc);
3521 *pfunc = self;
3522 na++;
3523 n++;
3524 } else
3525 Py_INCREF(func);
3526 if (PyFunction_Check(func))
3527 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003528 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003529 x = do_call(func, pp_stack, na, nk);
3530 Py_DECREF(func);
3531 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003532
Jeremy Hylton985eba52003-02-05 23:13:00 +00003533 /* What does this do? */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003534 while ((*pp_stack) > pfunc) {
3535 w = EXT_POP(*pp_stack);
3536 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003537 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003538 }
3539 return x;
3540}
3541
Jeremy Hylton192690e2002-08-16 18:36:11 +00003542/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003543 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003544 For the simplest case -- a function that takes only positional
3545 arguments and is called with only positional arguments -- it
3546 inlines the most primitive frame setup code from
3547 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3548 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003549*/
3550
3551static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003552fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003553{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003554 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003555 PyObject *globals = PyFunction_GET_GLOBALS(func);
3556 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3557 PyObject **d = NULL;
3558 int nd = 0;
3559
Jeremy Hylton985eba52003-02-05 23:13:00 +00003560 PCALL(PCALL_FUNCTION);
3561 PCALL(PCALL_FAST_FUNCTION);
Raymond Hettinger40174c32003-05-31 07:04:16 +00003562 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003563 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3564 PyFrameObject *f;
3565 PyObject *retval = NULL;
3566 PyThreadState *tstate = PyThreadState_GET();
3567 PyObject **fastlocals, **stack;
3568 int i;
3569
3570 PCALL(PCALL_FASTER_FUNCTION);
3571 assert(globals != NULL);
3572 /* XXX Perhaps we should create a specialized
3573 PyFrame_New() that doesn't take locals, but does
3574 take builtins without sanity checking them.
3575 */
3576 f = PyFrame_New(tstate, co, globals, NULL);
3577 if (f == NULL)
3578 return NULL;
3579
3580 fastlocals = f->f_localsplus;
3581 stack = (*pp_stack) - n;
3582
3583 for (i = 0; i < n; i++) {
3584 Py_INCREF(*stack);
3585 fastlocals[i] = *stack++;
3586 }
3587 retval = eval_frame(f);
3588 assert(tstate != NULL);
3589 ++tstate->recursion_depth;
3590 Py_DECREF(f);
3591 --tstate->recursion_depth;
3592 return retval;
3593 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003594 if (argdefs != NULL) {
3595 d = &PyTuple_GET_ITEM(argdefs, 0);
3596 nd = ((PyTupleObject *)argdefs)->ob_size;
3597 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003598 return PyEval_EvalCodeEx(co, globals,
3599 (PyObject *)NULL, (*pp_stack)-n, na,
3600 (*pp_stack)-2*nk, nk, d, nd,
3601 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003602}
3603
3604static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003605update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3606 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003607{
3608 PyObject *kwdict = NULL;
3609 if (orig_kwdict == NULL)
3610 kwdict = PyDict_New();
3611 else {
3612 kwdict = PyDict_Copy(orig_kwdict);
3613 Py_DECREF(orig_kwdict);
3614 }
3615 if (kwdict == NULL)
3616 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003617 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003618 int err;
3619 PyObject *value = EXT_POP(*pp_stack);
3620 PyObject *key = EXT_POP(*pp_stack);
3621 if (PyDict_GetItem(kwdict, key) != NULL) {
Guido van Rossumac7be682001-01-17 15:42:30 +00003622 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +00003623 "%.200s%s got multiple values "
Jeremy Hylton512a2372001-04-11 13:52:29 +00003624 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003625 PyEval_GetFuncName(func),
3626 PyEval_GetFuncDesc(func),
Jeremy Hylton512a2372001-04-11 13:52:29 +00003627 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003628 Py_DECREF(key);
3629 Py_DECREF(value);
3630 Py_DECREF(kwdict);
3631 return NULL;
3632 }
3633 err = PyDict_SetItem(kwdict, key, value);
3634 Py_DECREF(key);
3635 Py_DECREF(value);
3636 if (err) {
3637 Py_DECREF(kwdict);
3638 return NULL;
3639 }
3640 }
3641 return kwdict;
3642}
3643
3644static PyObject *
3645update_star_args(int nstack, int nstar, PyObject *stararg,
3646 PyObject ***pp_stack)
3647{
3648 PyObject *callargs, *w;
3649
3650 callargs = PyTuple_New(nstack + nstar);
3651 if (callargs == NULL) {
3652 return NULL;
3653 }
3654 if (nstar) {
3655 int i;
3656 for (i = 0; i < nstar; i++) {
3657 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3658 Py_INCREF(a);
3659 PyTuple_SET_ITEM(callargs, nstack + i, a);
3660 }
3661 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003662 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003663 w = EXT_POP(*pp_stack);
3664 PyTuple_SET_ITEM(callargs, nstack, w);
3665 }
3666 return callargs;
3667}
3668
3669static PyObject *
3670load_args(PyObject ***pp_stack, int na)
3671{
3672 PyObject *args = PyTuple_New(na);
3673 PyObject *w;
3674
3675 if (args == NULL)
3676 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003677 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003678 w = EXT_POP(*pp_stack);
3679 PyTuple_SET_ITEM(args, na, w);
3680 }
3681 return args;
3682}
3683
3684static PyObject *
3685do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3686{
3687 PyObject *callargs = NULL;
3688 PyObject *kwdict = NULL;
3689 PyObject *result = NULL;
3690
3691 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003692 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003693 if (kwdict == NULL)
3694 goto call_fail;
3695 }
3696 callargs = load_args(pp_stack, na);
3697 if (callargs == NULL)
3698 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003699#ifdef CALL_PROFILE
3700 /* At this point, we have to look at the type of func to
3701 update the call stats properly. Do it here so as to avoid
3702 exposing the call stats machinery outside ceval.c
3703 */
3704 if (PyFunction_Check(func))
3705 PCALL(PCALL_FUNCTION);
3706 else if (PyMethod_Check(func))
3707 PCALL(PCALL_METHOD);
3708 else if (PyType_Check(func))
3709 PCALL(PCALL_TYPE);
3710 else
3711 PCALL(PCALL_OTHER);
3712#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003713 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003714 call_fail:
3715 Py_XDECREF(callargs);
3716 Py_XDECREF(kwdict);
3717 return result;
3718}
3719
3720static PyObject *
3721ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3722{
3723 int nstar = 0;
3724 PyObject *callargs = NULL;
3725 PyObject *stararg = NULL;
3726 PyObject *kwdict = NULL;
3727 PyObject *result = NULL;
3728
3729 if (flags & CALL_FLAG_KW) {
3730 kwdict = EXT_POP(*pp_stack);
3731 if (!(kwdict && PyDict_Check(kwdict))) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003732 PyErr_Format(PyExc_TypeError,
Jeremy Hylton512a2372001-04-11 13:52:29 +00003733 "%s%s argument after ** "
3734 "must be a dictionary",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003735 PyEval_GetFuncName(func),
3736 PyEval_GetFuncDesc(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003737 goto ext_call_fail;
3738 }
3739 }
3740 if (flags & CALL_FLAG_VAR) {
3741 stararg = EXT_POP(*pp_stack);
3742 if (!PyTuple_Check(stararg)) {
3743 PyObject *t = NULL;
3744 t = PySequence_Tuple(stararg);
3745 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003746 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3747 PyErr_Format(PyExc_TypeError,
3748 "%s%s argument after * "
3749 "must be a sequence",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003750 PyEval_GetFuncName(func),
3751 PyEval_GetFuncDesc(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003752 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003753 goto ext_call_fail;
3754 }
3755 Py_DECREF(stararg);
3756 stararg = t;
3757 }
3758 nstar = PyTuple_GET_SIZE(stararg);
3759 }
3760 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003761 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003762 if (kwdict == NULL)
3763 goto ext_call_fail;
3764 }
3765 callargs = update_star_args(na, nstar, stararg, pp_stack);
3766 if (callargs == NULL)
3767 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003768#ifdef CALL_PROFILE
3769 /* At this point, we have to look at the type of func to
3770 update the call stats properly. Do it here so as to avoid
3771 exposing the call stats machinery outside ceval.c
3772 */
3773 if (PyFunction_Check(func))
3774 PCALL(PCALL_FUNCTION);
3775 else if (PyMethod_Check(func))
3776 PCALL(PCALL_METHOD);
3777 else if (PyType_Check(func))
3778 PCALL(PCALL_TYPE);
3779 else
3780 PCALL(PCALL_OTHER);
3781#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003782 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003783 ext_call_fail:
3784 Py_XDECREF(callargs);
3785 Py_XDECREF(kwdict);
3786 Py_XDECREF(stararg);
3787 return result;
3788}
3789
Guido van Rossum3b9c6671996-07-30 18:40:29 +00003790#define SLICE_ERROR_MSG \
3791 "standard sequence type does not support step size other than one"
3792
Tim Peterscb479e72001-12-16 19:11:44 +00003793/* Extract a slice index from a PyInt or PyLong, and store in *pi.
3794 Silently reduce values larger than INT_MAX to INT_MAX, and silently
3795 boost values less than -INT_MAX to 0. Return 0 on error, 1 on success.
3796*/
Tim Petersb5196382001-12-16 19:44:20 +00003797/* Note: If v is NULL, return success without storing into *pi. This
3798 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3799 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00003800*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00003801int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003802_PyEval_SliceIndex(PyObject *v, int *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003803{
Tim Petersb5196382001-12-16 19:44:20 +00003804 if (v != NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003805 long x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003806 if (PyInt_Check(v)) {
3807 x = PyInt_AsLong(v);
3808 } else if (PyLong_Check(v)) {
3809 x = PyLong_AsLong(v);
3810 if (x==-1 && PyErr_Occurred()) {
3811 PyObject *long_zero;
Guido van Rossumac7be682001-01-17 15:42:30 +00003812 int cmp;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003813
Guido van Rossumac7be682001-01-17 15:42:30 +00003814 if (!PyErr_ExceptionMatches(
3815 PyExc_OverflowError)) {
3816 /* It's not an overflow error, so just
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003817 signal an error */
Guido van Rossum20c6add2000-05-08 14:06:50 +00003818 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003819 }
3820
Guido van Rossumac7be682001-01-17 15:42:30 +00003821 /* Clear the OverflowError */
3822 PyErr_Clear();
3823
3824 /* It's an overflow error, so we need to
3825 check the sign of the long integer,
Tim Peters8a5c3c72004-04-05 19:36:21 +00003826 set the value to INT_MAX or -INT_MAX,
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003827 and clear the error. */
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003828
3829 /* Create a long integer with a value of 0 */
Guido van Rossumac7be682001-01-17 15:42:30 +00003830 long_zero = PyLong_FromLong(0L);
Tim Peterscb479e72001-12-16 19:11:44 +00003831 if (long_zero == NULL)
3832 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003833
3834 /* Check sign */
Guido van Rossumac7be682001-01-17 15:42:30 +00003835 cmp = PyObject_RichCompareBool(v, long_zero,
3836 Py_GT);
3837 Py_DECREF(long_zero);
3838 if (cmp < 0)
3839 return 0;
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003840 else if (cmp)
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003841 x = INT_MAX;
3842 else
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003843 x = -INT_MAX;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003844 }
3845 } else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003846 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003847 "slice indices must be integers");
Guido van Rossum20c6add2000-05-08 14:06:50 +00003848 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003849 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003850 /* Truncate -- very long indices are truncated anyway */
3851 if (x > INT_MAX)
3852 x = INT_MAX;
3853 else if (x < -INT_MAX)
Michael W. Hudsoncbd6fb92002-11-06 15:17:32 +00003854 x = -INT_MAX;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003855 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003856 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00003857 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003858}
3859
Guido van Rossum50d756e2001-08-18 17:43:36 +00003860#undef ISINT
3861#define ISINT(x) ((x) == NULL || PyInt_Check(x) || PyLong_Check(x))
3862
Guido van Rossumb209a111997-04-29 18:18:01 +00003863static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003864apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003865{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003866 PyTypeObject *tp = u->ob_type;
3867 PySequenceMethods *sq = tp->tp_as_sequence;
3868
3869 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3870 int ilow = 0, ihigh = INT_MAX;
3871 if (!_PyEval_SliceIndex(v, &ilow))
3872 return NULL;
3873 if (!_PyEval_SliceIndex(w, &ihigh))
3874 return NULL;
3875 return PySequence_GetSlice(u, ilow, ihigh);
3876 }
3877 else {
3878 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00003879 if (slice != NULL) {
3880 PyObject *res = PyObject_GetItem(u, slice);
3881 Py_DECREF(slice);
3882 return res;
3883 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00003884 else
3885 return NULL;
3886 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003887}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003888
3889static int
Guido van Rossumac7be682001-01-17 15:42:30 +00003890assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
3891 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003892{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003893 PyTypeObject *tp = u->ob_type;
3894 PySequenceMethods *sq = tp->tp_as_sequence;
3895
3896 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3897 int ilow = 0, ihigh = INT_MAX;
3898 if (!_PyEval_SliceIndex(v, &ilow))
3899 return -1;
3900 if (!_PyEval_SliceIndex(w, &ihigh))
3901 return -1;
3902 if (x == NULL)
3903 return PySequence_DelSlice(u, ilow, ihigh);
3904 else
3905 return PySequence_SetSlice(u, ilow, ihigh, x);
3906 }
3907 else {
3908 PyObject *slice = PySlice_New(v, w, NULL);
3909 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00003910 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003911 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00003912 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00003913 else
Guido van Rossum354797c2001-12-03 19:45:06 +00003914 res = PyObject_DelItem(u, slice);
3915 Py_DECREF(slice);
3916 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003917 }
3918 else
3919 return -1;
3920 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003921}
3922
Guido van Rossumb209a111997-04-29 18:18:01 +00003923static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003924cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003925{
Guido van Rossumac7be682001-01-17 15:42:30 +00003926 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003927 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00003928 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003929 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003930 break;
3931 case PyCmp_IS_NOT:
3932 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003933 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003934 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003935 res = PySequence_Contains(w, v);
3936 if (res < 0)
3937 return NULL;
3938 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003939 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00003940 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003941 if (res < 0)
3942 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003943 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003944 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003945 case PyCmp_EXC_MATCH:
Barry Warsaw4249f541997-08-22 21:26:19 +00003946 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003947 break;
3948 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00003949 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003950 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003951 v = res ? Py_True : Py_False;
3952 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003953 return v;
3954}
3955
Thomas Wouters52152252000-08-17 22:55:00 +00003956static PyObject *
3957import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003958{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003959 PyObject *x;
3960
3961 x = PyObject_GetAttr(v, name);
3962 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00003963 PyErr_Format(PyExc_ImportError,
3964 "cannot import name %.230s",
3965 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003966 }
Thomas Wouters52152252000-08-17 22:55:00 +00003967 return x;
3968}
Guido van Rossumac7be682001-01-17 15:42:30 +00003969
Thomas Wouters52152252000-08-17 22:55:00 +00003970static int
3971import_all_from(PyObject *locals, PyObject *v)
3972{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003973 PyObject *all = PyObject_GetAttrString(v, "__all__");
3974 PyObject *dict, *name, *value;
3975 int skip_leading_underscores = 0;
3976 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00003977
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003978 if (all == NULL) {
3979 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3980 return -1; /* Unexpected error */
3981 PyErr_Clear();
3982 dict = PyObject_GetAttrString(v, "__dict__");
3983 if (dict == NULL) {
3984 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3985 return -1;
3986 PyErr_SetString(PyExc_ImportError,
3987 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00003988 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003989 }
3990 all = PyMapping_Keys(dict);
3991 Py_DECREF(dict);
3992 if (all == NULL)
3993 return -1;
3994 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00003995 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003996
3997 for (pos = 0, err = 0; ; pos++) {
3998 name = PySequence_GetItem(all, pos);
3999 if (name == NULL) {
4000 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4001 err = -1;
4002 else
4003 PyErr_Clear();
4004 break;
4005 }
4006 if (skip_leading_underscores &&
4007 PyString_Check(name) &&
4008 PyString_AS_STRING(name)[0] == '_')
4009 {
4010 Py_DECREF(name);
4011 continue;
4012 }
4013 value = PyObject_GetAttr(v, name);
4014 if (value == NULL)
4015 err = -1;
4016 else
4017 err = PyDict_SetItem(locals, name, value);
4018 Py_DECREF(name);
4019 Py_XDECREF(value);
4020 if (err != 0)
4021 break;
4022 }
4023 Py_DECREF(all);
4024 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004025}
4026
Guido van Rossumb209a111997-04-29 18:18:01 +00004027static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004028build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004029{
Guido van Rossum7851eea2001-09-12 19:19:18 +00004030 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004031
4032 if (PyDict_Check(methods))
4033 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00004034 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00004035 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004036 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4037 base = PyTuple_GET_ITEM(bases, 0);
4038 metaclass = PyObject_GetAttrString(base, "__class__");
4039 if (metaclass == NULL) {
4040 PyErr_Clear();
4041 metaclass = (PyObject *)base->ob_type;
4042 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00004043 }
4044 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004045 else {
4046 PyObject *g = PyEval_GetGlobals();
4047 if (g != NULL && PyDict_Check(g))
4048 metaclass = PyDict_GetItemString(g, "__metaclass__");
4049 if (metaclass == NULL)
4050 metaclass = (PyObject *) &PyClass_Type;
4051 Py_INCREF(metaclass);
4052 }
4053 result = PyObject_CallFunction(metaclass, "OOO", name, bases, methods);
4054 Py_DECREF(metaclass);
4055 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00004056}
4057
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004058static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004059exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
4060 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004061{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004062 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00004063 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004064 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004065
Guido van Rossumb209a111997-04-29 18:18:01 +00004066 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
4067 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004068 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00004069 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004070 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00004071 locals = PyTuple_GetItem(prog, 2);
4072 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004073 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004074 if (globals == Py_None) {
4075 globals = PyEval_GetGlobals();
4076 if (locals == Py_None) {
4077 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00004078 plain = 1;
4079 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004080 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004081 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004082 locals = globals;
Guido van Rossumb209a111997-04-29 18:18:01 +00004083 if (!PyString_Check(prog) &&
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004084 !PyUnicode_Check(prog) &&
Guido van Rossumb209a111997-04-29 18:18:01 +00004085 !PyCode_Check(prog) &&
4086 !PyFile_Check(prog)) {
4087 PyErr_SetString(PyExc_TypeError,
Guido van Rossumac7be682001-01-17 15:42:30 +00004088 "exec: arg 1 must be a string, file, or code object");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004089 return -1;
4090 }
Fred Drake661ea262000-10-24 19:57:45 +00004091 if (!PyDict_Check(globals)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004092 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00004093 "exec: arg 2 must be a dictionary or None");
4094 return -1;
4095 }
4096 if (!PyDict_Check(locals)) {
4097 PyErr_SetString(PyExc_TypeError,
4098 "exec: arg 3 must be a dictionary or None");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004099 return -1;
4100 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004101 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00004102 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00004103 if (PyCode_Check(prog)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +00004104 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4105 PyErr_SetString(PyExc_TypeError,
4106 "code object passed to exec may not contain free variables");
4107 return -1;
4108 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004109 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004110 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004111 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004112 FILE *fp = PyFile_AsFile(prog);
4113 char *name = PyString_AsString(PyFile_Name(prog));
Tim Peters5ba58662001-07-16 02:29:45 +00004114 PyCompilerFlags cf;
4115 cf.cf_flags = 0;
4116 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004117 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004118 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004119 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004120 v = PyRun_File(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004121 locals);
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004122 }
4123 else {
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004124 PyObject *tmp = NULL;
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004125 char *str;
Tim Peters5ba58662001-07-16 02:29:45 +00004126 PyCompilerFlags cf;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004127 cf.cf_flags = 0;
4128#ifdef Py_USING_UNICODE
4129 if (PyUnicode_Check(prog)) {
4130 tmp = PyUnicode_AsUTF8String(prog);
4131 if (tmp == NULL)
4132 return -1;
4133 prog = tmp;
4134 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4135 }
4136#endif
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004137 if (PyString_AsStringAndSize(prog, &str, NULL))
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004138 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004139 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters8a5c3c72004-04-05 19:36:21 +00004140 v = PyRun_StringFlags(str, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004141 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004142 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004143 v = PyRun_String(str, Py_file_input, globals, locals);
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004144 Py_XDECREF(tmp);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004145 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004146 if (plain)
4147 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004148 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004149 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00004150 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004151 return 0;
4152}
Guido van Rossum24c13741995-02-14 09:42:43 +00004153
Guido van Rossumac7be682001-01-17 15:42:30 +00004154static void
Paul Prescode68140d2000-08-30 20:25:01 +00004155format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4156{
4157 char *obj_str;
4158
4159 if (!obj)
4160 return;
4161
4162 obj_str = PyString_AsString(obj);
4163 if (!obj_str)
4164 return;
4165
4166 PyErr_Format(exc, format_str, obj_str);
4167}
Guido van Rossum950361c1997-01-24 13:49:28 +00004168
4169#ifdef DYNAMIC_EXECUTION_PROFILE
4170
Skip Montanarof118cb12001-10-15 20:51:38 +00004171static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004172getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004173{
4174 int i;
4175 PyObject *l = PyList_New(256);
4176 if (l == NULL) return NULL;
4177 for (i = 0; i < 256; i++) {
4178 PyObject *x = PyInt_FromLong(a[i]);
4179 if (x == NULL) {
4180 Py_DECREF(l);
4181 return NULL;
4182 }
4183 PyList_SetItem(l, i, x);
4184 }
4185 for (i = 0; i < 256; i++)
4186 a[i] = 0;
4187 return l;
4188}
4189
4190PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004191_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004192{
4193#ifndef DXPAIRS
4194 return getarray(dxp);
4195#else
4196 int i;
4197 PyObject *l = PyList_New(257);
4198 if (l == NULL) return NULL;
4199 for (i = 0; i < 257; i++) {
4200 PyObject *x = getarray(dxpairs[i]);
4201 if (x == NULL) {
4202 Py_DECREF(l);
4203 return NULL;
4204 }
4205 PyList_SetItem(l, i, x);
4206 }
4207 return l;
4208#endif
4209}
4210
4211#endif