blob: b48e042e208dd089bc50c5a8dabf6aa6e465fda8 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003
Guido van Rossum681d79a1995-07-18 14:51:37 +00004/* XXX TO DO:
Guido van Rossum681d79a1995-07-18 14:51:37 +00005 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +00006 XXX document it!
7 */
8
Guido van Rossumb209a111997-04-29 18:18:01 +00009#include "Python.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000010
Guido van Rossum10dc2e81990-11-18 17:27:39 +000011#include "compile.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000012#include "frameobject.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000013#include "eval.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000014#include "opcode.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000015#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000016
Guido van Rossumc6004111993-11-05 10:22:19 +000017#include <ctype.h>
18
Guido van Rossum04691fc1992-08-12 15:35:34 +000019/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000020/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000021
Guido van Rossum408027e1996-12-30 16:17:54 +000022#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000023/* For debugging the interpreter: */
24#define LLTRACE 1 /* Low-level trace feature */
25#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000026#endif
27
Jeremy Hylton52820442001-01-03 23:52:36 +000028typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000029
Guido van Rossum374a9221991-04-04 10:40:29 +000030/* Forward declarations */
Tim Peters5ca576e2001-06-18 22:08:13 +000031static PyObject *eval_frame(PyFrameObject *);
Jeremy Hyltone8c04322002-08-16 17:47:26 +000032static PyObject *call_function(PyObject ***, int);
Jeremy Hylton52820442001-01-03 23:52:36 +000033static PyObject *fast_function(PyObject *, PyObject ***, int, int, int);
Jeremy Hylton52820442001-01-03 23:52:36 +000034static PyObject *do_call(PyObject *, PyObject ***, int, int);
35static PyObject *ext_do_call(PyObject *, PyObject ***, int, int, int);
Guido van Rossumac7be682001-01-17 15:42:30 +000036static PyObject *update_keyword_args(PyObject *, int, PyObject ***,PyObject *);
Ka-Ping Yee20579702001-01-15 22:14:16 +000037static PyObject *update_star_args(int, int, PyObject *, PyObject ***);
Jeremy Hylton52820442001-01-03 23:52:36 +000038static PyObject *load_args(PyObject ***, int);
39#define CALL_FLAG_VAR 1
40#define CALL_FLAG_KW 2
41
Guido van Rossum0a066c01992-03-27 17:29:15 +000042#ifdef LLTRACE
Tim Petersdbd9ba62000-07-09 03:09:57 +000043static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000044#endif
Fred Drake5755ce62001-06-27 19:19:46 +000045static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
46 int, PyObject *);
Fred Drake4ec5d562001-10-04 19:26:43 +000047static void call_trace_protected(Py_tracefunc, PyObject *,
48 PyFrameObject *, int);
Fred Drake5755ce62001-06-27 19:19:46 +000049static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
Michael W. Hudson006c7522002-11-08 13:08:46 +000050static int maybe_call_line_trace(Py_tracefunc, PyObject *,
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{
126 return Py_BuildValue("iiiiiiiiii",
127 pcall[0], pcall[1], pcall[2], pcall[3],
128 pcall[4], pcall[5], pcall[6], pcall[7],
129 pcall[8], pcall[9]);
130}
131#else
132#define PCALL(O)
133
134PyObject *
135PyEval_GetCallStats(PyObject *self)
136{
137 Py_INCREF(Py_None);
138 return Py_None;
139}
140#endif
141
Jeremy Hylton938ace62002-07-17 16:30:39 +0000142static PyTypeObject gentype;
Tim Peters5ca576e2001-06-18 22:08:13 +0000143
144typedef struct {
145 PyObject_HEAD
Tim Petersd8e1c9e2001-06-26 20:58:58 +0000146 /* The gi_ prefix is intended to remind of generator-iterator. */
147
148 PyFrameObject *gi_frame;
149
Tim Peterse77f2e22001-06-26 22:24:51 +0000150 /* True if generator is being executed. */
151 int gi_running;
Fred Drake72bc4562002-08-09 18:35:52 +0000152
153 /* List of weak reference. */
154 PyObject *gi_weakreflist;
Tim Peters5ca576e2001-06-18 22:08:13 +0000155} genobject;
156
157static PyObject *
158gen_new(PyFrameObject *f)
159{
Neil Schemenauer08de92a2002-03-18 20:45:09 +0000160 genobject *gen = PyObject_GC_New(genobject, &gentype);
Tim Peters5ca576e2001-06-18 22:08:13 +0000161 if (gen == NULL) {
162 Py_DECREF(f);
163 return NULL;
164 }
Tim Petersd8e1c9e2001-06-26 20:58:58 +0000165 gen->gi_frame = f;
166 gen->gi_running = 0;
Fred Drake72bc4562002-08-09 18:35:52 +0000167 gen->gi_weakreflist = NULL;
Neil Schemenauer08de92a2002-03-18 20:45:09 +0000168 _PyObject_GC_TRACK(gen);
Tim Peters5ca576e2001-06-18 22:08:13 +0000169 return (PyObject *)gen;
170}
171
Neil Schemenauerf8c7c202001-07-12 13:27:49 +0000172static int
173gen_traverse(genobject *gen, visitproc visit, void *arg)
174{
175 return visit((PyObject *)gen->gi_frame, arg);
176}
177
Tim Peters5ca576e2001-06-18 22:08:13 +0000178static void
179gen_dealloc(genobject *gen)
180{
Neil Schemenauer08de92a2002-03-18 20:45:09 +0000181 _PyObject_GC_UNTRACK(gen);
Fred Drake72bc4562002-08-09 18:35:52 +0000182 if (gen->gi_weakreflist != NULL)
183 PyObject_ClearWeakRefs((PyObject *) gen);
Tim Petersd8e1c9e2001-06-26 20:58:58 +0000184 Py_DECREF(gen->gi_frame);
Neil Schemenauer08de92a2002-03-18 20:45:09 +0000185 PyObject_GC_Del(gen);
Tim Peters5ca576e2001-06-18 22:08:13 +0000186}
187
188static PyObject *
189gen_iternext(genobject *gen)
190{
Neil Schemenauer2b13ce82001-06-21 02:41:10 +0000191 PyThreadState *tstate = PyThreadState_GET();
Tim Petersd8e1c9e2001-06-26 20:58:58 +0000192 PyFrameObject *f = gen->gi_frame;
Tim Peters5ca576e2001-06-18 22:08:13 +0000193 PyObject *result;
194
Tim Petersd8e1c9e2001-06-26 20:58:58 +0000195 if (gen->gi_running) {
Tim Peters5ca576e2001-06-18 22:08:13 +0000196 PyErr_SetString(PyExc_ValueError,
197 "generator already executing");
198 return NULL;
199 }
Tim Peters8c963692001-06-23 05:26:56 +0000200 if (f->f_stacktop == NULL)
Tim Peters5ca576e2001-06-18 22:08:13 +0000201 return NULL;
Neil Schemenauer2b13ce82001-06-21 02:41:10 +0000202
203 /* Generators always return to their most recent caller, not
204 * necessarily their creator. */
Tim Peters5eb4b872001-06-23 05:47:56 +0000205 Py_XINCREF(tstate->frame);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +0000206 assert(f->f_back == NULL);
207 f->f_back = tstate->frame;
208
Tim Petersd8e1c9e2001-06-26 20:58:58 +0000209 gen->gi_running = 1;
Tim Peters5ca576e2001-06-18 22:08:13 +0000210 result = eval_frame(f);
Tim Petersd8e1c9e2001-06-26 20:58:58 +0000211 gen->gi_running = 0;
Neil Schemenauer2b13ce82001-06-21 02:41:10 +0000212
213 /* Don't keep the reference to f_back any longer than necessary. It
214 * may keep a chain of frames alive or it could create a reference
215 * cycle. */
Tim Peters5eb4b872001-06-23 05:47:56 +0000216 Py_XDECREF(f->f_back);
Tim Peters6302ec62001-06-20 06:57:32 +0000217 f->f_back = NULL;
Neil Schemenauer2b13ce82001-06-21 02:41:10 +0000218
Tim Petersad1a18b2001-06-23 06:19:16 +0000219 /* If the generator just returned (as opposed to yielding), signal
220 * that the generator is exhausted. */
221 if (result == Py_None && f->f_stacktop == NULL) {
222 Py_DECREF(result);
223 result = NULL;
224 }
225
Neil Schemenauer2b13ce82001-06-21 02:41:10 +0000226 return result;
Tim Peters5ca576e2001-06-18 22:08:13 +0000227}
228
229static PyObject *
Tim Peters5ca576e2001-06-18 22:08:13 +0000230gen_getiter(PyObject *gen)
231{
232 Py_INCREF(gen);
233 return gen;
234}
235
Guido van Rossum6f799372001-09-20 20:46:19 +0000236static PyMemberDef gen_memberlist[] = {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000237 {"gi_frame", T_OBJECT, offsetof(genobject, gi_frame), RO},
238 {"gi_running", T_INT, offsetof(genobject, gi_running), RO},
239 {NULL} /* Sentinel */
240};
Tim Peters5ca576e2001-06-18 22:08:13 +0000241
Tim Peters0c322792002-07-17 16:49:03 +0000242static PyTypeObject gentype = {
Tim Peters5ca576e2001-06-18 22:08:13 +0000243 PyObject_HEAD_INIT(&PyType_Type)
244 0, /* ob_size */
245 "generator", /* tp_name */
Neil Schemenauer08de92a2002-03-18 20:45:09 +0000246 sizeof(genobject), /* tp_basicsize */
Tim Peters5ca576e2001-06-18 22:08:13 +0000247 0, /* tp_itemsize */
248 /* methods */
249 (destructor)gen_dealloc, /* tp_dealloc */
250 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000251 0, /* tp_getattr */
Tim Peters5ca576e2001-06-18 22:08:13 +0000252 0, /* tp_setattr */
253 0, /* tp_compare */
254 0, /* tp_repr */
255 0, /* tp_as_number */
256 0, /* tp_as_sequence */
257 0, /* tp_as_mapping */
258 0, /* tp_hash */
259 0, /* tp_call */
260 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000261 PyObject_GenericGetAttr, /* tp_getattro */
Tim Peters5ca576e2001-06-18 22:08:13 +0000262 0, /* tp_setattro */
263 0, /* tp_as_buffer */
Neil Schemenauer08de92a2002-03-18 20:45:09 +0000264 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
Tim Peters5ca576e2001-06-18 22:08:13 +0000265 0, /* tp_doc */
Neil Schemenauerf8c7c202001-07-12 13:27:49 +0000266 (traverseproc)gen_traverse, /* tp_traverse */
Tim Peters5ca576e2001-06-18 22:08:13 +0000267 0, /* tp_clear */
268 0, /* tp_richcompare */
Fred Drake72bc4562002-08-09 18:35:52 +0000269 offsetof(genobject, gi_weakreflist), /* tp_weaklistoffset */
Tim Peters5ca576e2001-06-18 22:08:13 +0000270 (getiterfunc)gen_getiter, /* tp_iter */
271 (iternextfunc)gen_iternext, /* tp_iternext */
Tim Petersa64295b2002-07-17 00:15:22 +0000272 0, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000273 gen_memberlist, /* tp_members */
274 0, /* tp_getset */
275 0, /* tp_base */
276 0, /* tp_dict */
Tim Peters5ca576e2001-06-18 22:08:13 +0000277};
278
279
Guido van Rossume59214e1994-08-30 08:01:59 +0000280#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000281
Guido van Rossum2571cc81999-04-07 16:07:23 +0000282#ifndef DONT_HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000283#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000284#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000285#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000286
Guido van Rossuma027efa1997-05-05 20:56:21 +0000287extern int _PyThread_Started; /* Flag for Py_Exit */
288
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000289static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
Guido van Rossuma9672091994-09-14 13:31:22 +0000290static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000291
292void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000293PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000294{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000295 if (interpreter_lock)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000296 return;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000297 _PyThread_Started = 1;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000298 interpreter_lock = PyThread_allocate_lock();
299 PyThread_acquire_lock(interpreter_lock, 1);
300 main_thread = PyThread_get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000301}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000302
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000303void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000304PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000305{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000306 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000307}
308
309void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000310PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000311{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000312 PyThread_release_lock(interpreter_lock);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000313}
314
315void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000316PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000317{
318 if (tstate == NULL)
319 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000320 /* Check someone has called PyEval_InitThreads() to create the lock */
321 assert(interpreter_lock);
Guido van Rossum65d5b571998-12-21 19:32:43 +0000322 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000323 if (PyThreadState_Swap(tstate) != NULL)
324 Py_FatalError(
325 "PyEval_AcquireThread: non-NULL old thread state");
326}
327
328void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000329PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000330{
331 if (tstate == NULL)
332 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
333 if (PyThreadState_Swap(NULL) != tstate)
334 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000335 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000336}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000337
338/* This function is called from PyOS_AfterFork to ensure that newly
339 created child processes don't hold locks referring to threads which
340 are not running in the child process. (This could also be done using
341 pthread_atfork mechanism, at least for the pthreads implementation.) */
342
343void
344PyEval_ReInitThreads(void)
345{
346 if (!interpreter_lock)
347 return;
348 /*XXX Can't use PyThread_free_lock here because it does too
349 much error-checking. Doing this cleanly would require
350 adding a new function to each thread_*.h. Instead, just
351 create a new lock and waste a little bit of memory */
352 interpreter_lock = PyThread_allocate_lock();
353 PyThread_acquire_lock(interpreter_lock, 1);
354 main_thread = PyThread_get_thread_ident();
355}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000356#endif
357
Guido van Rossumff4949e1992-08-05 19:58:53 +0000358/* Functions save_thread and restore_thread are always defined so
359 dynamically loaded modules needn't be compiled separately for use
360 with and without threads: */
361
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000362PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000363PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000364{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000365 PyThreadState *tstate = PyThreadState_Swap(NULL);
366 if (tstate == NULL)
367 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000368#ifdef WITH_THREAD
Guido van Rossumb74eca91997-09-30 22:03:16 +0000369 if (interpreter_lock)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000370 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000371#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000372 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000373}
374
375void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000376PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000377{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000378 if (tstate == NULL)
379 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000380#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000381 if (interpreter_lock) {
Guido van Rossumb74eca91997-09-30 22:03:16 +0000382 int err = errno;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000383 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000384 errno = err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000385 }
386#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000387 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000388}
389
390
Guido van Rossuma9672091994-09-14 13:31:22 +0000391/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
392 signal handlers or Mac I/O completion routines) can schedule calls
393 to a function to be called synchronously.
394 The synchronous function is called with one void* argument.
395 It should return 0 for success or -1 for failure -- failure should
396 be accompanied by an exception.
397
398 If registry succeeds, the registry function returns 0; if it fails
399 (e.g. due to too many pending calls) it returns -1 (without setting
400 an exception condition).
401
402 Note that because registry may occur from within signal handlers,
403 or other asynchronous events, calling malloc() is unsafe!
404
405#ifdef WITH_THREAD
406 Any thread can schedule pending calls, but only the main thread
407 will execute them.
408#endif
409
410 XXX WARNING! ASYNCHRONOUSLY EXECUTING CODE!
411 There are two possible race conditions:
412 (1) nested asynchronous registry calls;
413 (2) registry calls made while pending calls are being processed.
414 While (1) is very unlikely, (2) is a real possibility.
415 The current code is safe against (2), but not against (1).
416 The safety against (2) is derived from the fact that only one
417 thread (the main thread) ever takes things out of the queue.
Guido van Rossuma9672091994-09-14 13:31:22 +0000418
Guido van Rossuma027efa1997-05-05 20:56:21 +0000419 XXX Darn! With the advent of thread state, we should have an array
420 of pending calls per thread in the thread state! Later...
421*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000422
Guido van Rossuma9672091994-09-14 13:31:22 +0000423#define NPENDINGCALLS 32
424static struct {
Thomas Wouters334fb892000-07-25 12:56:38 +0000425 int (*func)(void *);
426 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000427} pendingcalls[NPENDINGCALLS];
428static volatile int pendingfirst = 0;
429static volatile int pendinglast = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000430static volatile int things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000431
432int
Thomas Wouters334fb892000-07-25 12:56:38 +0000433Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000434{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000435 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000436 int i, j;
437 /* XXX Begin critical section */
438 /* XXX If you want this to be safe against nested
439 XXX asynchronous calls, you'll have to work harder! */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000440 if (busy)
441 return -1;
442 busy = 1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000443 i = pendinglast;
444 j = (i + 1) % NPENDINGCALLS;
Guido van Rossum04e70322002-07-17 16:57:13 +0000445 if (j == pendingfirst) {
446 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000447 return -1; /* Queue full */
Guido van Rossum04e70322002-07-17 16:57:13 +0000448 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000449 pendingcalls[i].func = func;
450 pendingcalls[i].arg = arg;
451 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000452
453 _Py_Ticker = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000454 things_to_do = 1; /* Signal main loop */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000455 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000456 /* XXX End critical section */
457 return 0;
458}
459
Guido van Rossum180d7b41994-09-29 09:45:57 +0000460int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000461Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000462{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000463 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000464#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000465 if (main_thread && PyThread_get_thread_ident() != main_thread)
Guido van Rossuma9672091994-09-14 13:31:22 +0000466 return 0;
467#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000468 if (busy)
Guido van Rossum180d7b41994-09-29 09:45:57 +0000469 return 0;
470 busy = 1;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000471 things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000472 for (;;) {
473 int i;
Thomas Wouters334fb892000-07-25 12:56:38 +0000474 int (*func)(void *);
475 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000476 i = pendingfirst;
477 if (i == pendinglast)
478 break; /* Queue empty */
479 func = pendingcalls[i].func;
480 arg = pendingcalls[i].arg;
481 pendingfirst = (i + 1) % NPENDINGCALLS;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000482 if (func(arg) < 0) {
483 busy = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000484 things_to_do = 1; /* We're not done yet */
Guido van Rossuma9672091994-09-14 13:31:22 +0000485 return -1;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000486 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000487 }
Guido van Rossum180d7b41994-09-29 09:45:57 +0000488 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000489 return 0;
490}
491
492
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000493/* The interpreter's recursion limit */
494
Guido van Rossum349ff6f2000-09-01 01:52:08 +0000495static int recursion_limit = 1000;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000496int _Py_CheckRecursionLimit = 1000;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000497
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000498int
499Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000500{
501 return recursion_limit;
502}
503
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000504void
505Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000506{
507 recursion_limit = new_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000508 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000509}
510
Armin Rigo2b3eb402003-10-28 12:05:48 +0000511/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
512 if the recursion_depth reaches _Py_CheckRecursionLimit.
513 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
514 to guarantee that _Py_CheckRecursiveCall() is regularly called.
515 Without USE_STACKCHECK, there is no need for this. */
516int
517_Py_CheckRecursiveCall(char *where)
518{
519 PyThreadState *tstate = PyThreadState_GET();
520
521#ifdef USE_STACKCHECK
522 if (PyOS_CheckStack()) {
523 --tstate->recursion_depth;
524 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
525 return -1;
526 }
527#endif
528 if (tstate->recursion_depth > recursion_limit) {
529 --tstate->recursion_depth;
530 PyErr_Format(PyExc_RuntimeError,
531 "maximum recursion depth exceeded%s",
532 where);
533 return -1;
534 }
535 _Py_CheckRecursionLimit = recursion_limit;
536 return 0;
537}
538
539
Guido van Rossum374a9221991-04-04 10:40:29 +0000540/* Status code for main loop (reason for stack unwind) */
541
542enum why_code {
543 WHY_NOT, /* No error */
544 WHY_EXCEPTION, /* Exception occurred */
545 WHY_RERAISE, /* Exception re-raised by 'finally' */
546 WHY_RETURN, /* 'return' statement */
Jeremy Hylton3faa52e2001-02-01 22:48:12 +0000547 WHY_BREAK, /* 'break' statement */
Tim Peters5ca576e2001-06-18 22:08:13 +0000548 WHY_CONTINUE, /* 'continue' statement */
Tim Peters6e6a63f2001-10-18 20:49:35 +0000549 WHY_YIELD /* 'yield' operator */
Guido van Rossum374a9221991-04-04 10:40:29 +0000550};
551
Tim Petersdbd9ba62000-07-09 03:09:57 +0000552static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
Tim Petersd6d010b2001-06-21 02:49:55 +0000553static int unpack_iterable(PyObject *, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000554
Skip Montanarod581d772002-09-03 20:10:45 +0000555/* for manipulating the thread switch and periodic "stuff" - used to be
556 per thread, now just a pair o' globals */
Skip Montanaro99dba272002-09-03 20:19:06 +0000557int _Py_CheckInterval = 100;
558volatile int _Py_Ticker = 100;
Guido van Rossum374a9221991-04-04 10:40:29 +0000559
Guido van Rossumb209a111997-04-29 18:18:01 +0000560PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000561PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000562{
Jeremy Hylton985eba52003-02-05 23:13:00 +0000563 /* XXX raise SystemError if globals is NULL */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000564 return PyEval_EvalCodeEx(co,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000565 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000566 (PyObject **)NULL, 0,
567 (PyObject **)NULL, 0,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000568 (PyObject **)NULL, 0,
569 NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000570}
571
572
573/* Interpreter main loop */
574
Tim Peters6d6c1a32001-08-02 04:15:00 +0000575static PyObject *
Tim Peters5ca576e2001-06-18 22:08:13 +0000576eval_frame(PyFrameObject *f)
Guido van Rossum374a9221991-04-04 10:40:29 +0000577{
Guido van Rossum950361c1997-01-24 13:49:28 +0000578#ifdef DXPAIRS
579 int lastopcode = 0;
580#endif
Tim Petersb6d14da2001-12-19 04:11:07 +0000581 PyObject **stack_pointer; /* Next free slot in value stack */
Guido van Rossum374a9221991-04-04 10:40:29 +0000582 register unsigned char *next_instr;
Moshe Zadkaaa39a7e2000-08-07 06:34:45 +0000583 register int opcode=0; /* Current opcode */
584 register int oparg=0; /* Current opcode argument, if any */
Guido van Rossum374a9221991-04-04 10:40:29 +0000585 register enum why_code why; /* Reason for block stack unwind */
586 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000587 register PyObject *x; /* Result object -- NULL if error */
588 register PyObject *v; /* Temporary objects popped off stack */
589 register PyObject *w;
590 register PyObject *u;
591 register PyObject *t;
Barry Warsaw23c9ec82000-08-21 15:44:01 +0000592 register PyObject *stream = NULL; /* for PRINT opcodes */
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000593 register PyObject **fastlocals, **freevars;
Guido van Rossum014518f1998-11-23 21:09:51 +0000594 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000595 PyThreadState *tstate = PyThreadState_GET();
Tim Peters5ca576e2001-06-18 22:08:13 +0000596 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000597
598 /* when tracing we set things up so that
599
600 not (instr_lb <= current_bytecode_offset < instr_ub)
601
602 is true when the line being executed has changed. The
603 initial values are such as to make this false the first
604 time it is tested. */
Armin Rigobf57a142004-03-22 19:24:58 +0000605 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000606
Guido van Rossumd076c731998-10-07 19:42:25 +0000607 unsigned char *first_instr;
Skip Montanaro04d80f82002-08-04 21:03:35 +0000608 PyObject *names;
609 PyObject *consts;
Guido van Rossum96a42c81992-01-12 02:29:51 +0000610#ifdef LLTRACE
Guido van Rossumacbe8da1993-04-15 15:33:52 +0000611 int lltrace;
Guido van Rossum374a9221991-04-04 10:40:29 +0000612#endif
Guido van Rossum408027e1996-12-30 16:17:54 +0000613#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000614 /* Make it easier to find out where we are with a debugger */
Tim Peters5ca576e2001-06-18 22:08:13 +0000615 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000616#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000617
Neal Norwitza81d2202002-07-14 00:27:26 +0000618/* Tuple access macros */
619
620#ifndef Py_DEBUG
621#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
622#else
623#define GETITEM(v, i) PyTuple_GetItem((v), (i))
624#endif
625
Guido van Rossum374a9221991-04-04 10:40:29 +0000626/* Code access macros */
627
Guido van Rossumd076c731998-10-07 19:42:25 +0000628#define INSTR_OFFSET() (next_instr - first_instr)
Guido van Rossum374a9221991-04-04 10:40:29 +0000629#define NEXTOP() (*next_instr++)
Armin Rigo9dbf9082004-03-20 21:50:13 +0000630#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Guido van Rossumd076c731998-10-07 19:42:25 +0000631#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000632#define JUMPBY(x) (next_instr += (x))
633
Raymond Hettingerf606f872003-03-16 03:11:04 +0000634/* OpCode prediction macros
635 Some opcodes tend to come in pairs thus making it possible to predict
636 the second code when the first is run. For example, COMPARE_OP is often
637 followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And, those opcodes are often
638 followed by a POP_TOP.
639
640 Verifying the prediction costs a single high-speed test of register
Raymond Hettingerac2072922003-03-16 15:41:11 +0000641 variable against a constant. If the pairing was good, then the
Raymond Hettingerf606f872003-03-16 03:11:04 +0000642 processor has a high likelihood of making its own successful branch
643 prediction which results in a nearly zero overhead transition to the
644 next opcode.
645
646 A successful prediction saves a trip through the eval-loop including
647 its two unpredictable branches, the HASARG test and the switch-case.
Raymond Hettingera7216982004-02-08 19:59:27 +0000648
649 If collecting opcode statistics, turn off prediction so that
650 statistics are accurately maintained (the predictions bypass
651 the opcode frequency counter updates).
Raymond Hettingerf606f872003-03-16 03:11:04 +0000652*/
653
Raymond Hettingera7216982004-02-08 19:59:27 +0000654#ifdef DYNAMIC_EXECUTION_PROFILE
655#define PREDICT(op) if (0) goto PRED_##op
656#else
Raymond Hettingerac2072922003-03-16 15:41:11 +0000657#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000658#endif
659
Raymond Hettingerf606f872003-03-16 03:11:04 +0000660#define PREDICTED(op) PRED_##op: next_instr++
Armin Rigo9dbf9082004-03-20 21:50:13 +0000661#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = (next_instr[2]<<8) + \
662 next_instr[1]; next_instr += 3
Raymond Hettingerf606f872003-03-16 03:11:04 +0000663
Guido van Rossum374a9221991-04-04 10:40:29 +0000664/* Stack manipulation macros */
665
666#define STACK_LEVEL() (stack_pointer - f->f_valuestack)
667#define EMPTY() (STACK_LEVEL() == 0)
668#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000669#define SECOND() (stack_pointer[-2])
670#define THIRD() (stack_pointer[-3])
671#define FOURTH() (stack_pointer[-4])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000672#define SET_TOP(v) (stack_pointer[-1] = (v))
673#define SET_SECOND(v) (stack_pointer[-2] = (v))
674#define SET_THIRD(v) (stack_pointer[-3] = (v))
675#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000676#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000677#define BASIC_PUSH(v) (*stack_pointer++ = (v))
678#define BASIC_POP() (*--stack_pointer)
679
Guido van Rossum96a42c81992-01-12 02:29:51 +0000680#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000681#define PUSH(v) { (void)(BASIC_PUSH(v), \
682 lltrace && prtrace(TOP(), "push")); \
683 assert(STACK_LEVEL() <= f->f_stacksize); }
Fred Drakede26cfc2001-10-13 06:11:28 +0000684#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000685#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
686 lltrace && prtrace(TOP(), "stackadj")); \
687 assert(STACK_LEVEL() <= f->f_stacksize); }
Guido van Rossum374a9221991-04-04 10:40:29 +0000688#else
689#define PUSH(v) BASIC_PUSH(v)
690#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000691#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000692#endif
693
Guido van Rossum681d79a1995-07-18 14:51:37 +0000694/* Local variable macros */
695
696#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000697
698/* The SETLOCAL() macro must not DECREF the local variable in-place and
699 then store the new value; it must copy the old value to a temporary
700 value, then store the new value, and then DECREF the temporary value.
701 This is because it is possible that during the DECREF the frame is
702 accessed by other code (e.g. a __del__ method or gc.collect()) and the
703 variable would be pointing to already-freed memory. */
704#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
705 GETLOCAL(i) = value; \
706 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000707
Guido van Rossuma027efa1997-05-05 20:56:21 +0000708/* Start of code */
709
Tim Peters5ca576e2001-06-18 22:08:13 +0000710 if (f == NULL)
711 return NULL;
712
Armin Rigo1d313ab2003-10-25 14:33:09 +0000713 /* push frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000714 if (Py_EnterRecursiveCall(""))
Armin Rigo1d313ab2003-10-25 14:33:09 +0000715 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000716
Tim Peters5ca576e2001-06-18 22:08:13 +0000717 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000718
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000719 if (tstate->use_tracing) {
720 if (tstate->c_tracefunc != NULL) {
721 /* tstate->c_tracefunc, if defined, is a
722 function that will be called on *every* entry
723 to a code block. Its return value, if not
724 None, is a function that will be called at
725 the start of each executed line of code.
726 (Actually, the function must return itself
727 in order to continue tracing.) The trace
728 functions are called with three arguments:
729 a pointer to the current frame, a string
730 indicating why the function is called, and
731 an argument which depends on the situation.
732 The global trace function is also called
733 whenever an exception is detected. */
734 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
735 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000736 /* Trace function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000737 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000738 }
739 }
740 if (tstate->c_profilefunc != NULL) {
741 /* Similar for c_profilefunc, except it needn't
742 return itself and isn't called for "line" events */
743 if (call_trace(tstate->c_profilefunc,
744 tstate->c_profileobj,
745 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000746 /* Profile function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000747 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000748 }
749 }
750 }
751
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000752 co = f->f_code;
753 names = co->co_names;
754 consts = co->co_consts;
755 fastlocals = f->f_localsplus;
756 freevars = f->f_localsplus + f->f_nlocals;
Michael W. Hudsonecfeb7f2004-02-12 15:28:27 +0000757 first_instr = PyString_AS_STRING(co->co_code);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000758 /* An explanation is in order for the next line.
759
760 f->f_lasti now refers to the index of the last instruction
761 executed. You might think this was obvious from the name, but
762 this wasn't always true before 2.3! PyFrame_New now sets
763 f->f_lasti to -1 (i.e. the index *before* the first instruction)
764 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
765 does work. Promise. */
766 next_instr = first_instr + f->f_lasti + 1;
767 stack_pointer = f->f_stacktop;
768 assert(stack_pointer != NULL);
769 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
770
Tim Peters5ca576e2001-06-18 22:08:13 +0000771#ifdef LLTRACE
772 lltrace = PyDict_GetItemString(f->f_globals,"__lltrace__") != NULL;
773#endif
774#if defined(Py_DEBUG) || defined(LLTRACE)
775 filename = PyString_AsString(co->co_filename);
776#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000777
Guido van Rossum374a9221991-04-04 10:40:29 +0000778 why = WHY_NOT;
779 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +0000780 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +0000781 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +0000782
Guido van Rossum374a9221991-04-04 10:40:29 +0000783 for (;;) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000784 assert(stack_pointer >= f->f_valuestack); /* else underflow */
785 assert(STACK_LEVEL() <= f->f_stacksize); /* else overflow */
786
Guido van Rossuma027efa1997-05-05 20:56:21 +0000787 /* Do periodic things. Doing this every time through
788 the loop would add too much overhead, so we do it
789 only every Nth instruction. We also do it if
790 ``things_to_do'' is set, i.e. when an asynchronous
791 event needs attention (e.g. a signal handler or
792 async I/O handler); see Py_AddPendingCall() and
793 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000794
Skip Montanarod581d772002-09-03 20:10:45 +0000795 if (--_Py_Ticker < 0) {
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000796 if (*next_instr == SETUP_FINALLY) {
797 /* Make the last opcode before
798 a try: finally: block uninterruptable. */
799 goto fast_next_opcode;
800 }
Skip Montanarod581d772002-09-03 20:10:45 +0000801 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000802 tstate->tick_counter++;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000803 if (things_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +0000804 if (Py_MakePendingCalls() < 0) {
805 why = WHY_EXCEPTION;
806 goto on_error;
807 }
808 }
Guido van Rossume59214e1994-08-30 08:01:59 +0000809#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000810 if (interpreter_lock) {
811 /* Give another thread a chance */
812
Guido van Rossum25ce5661997-08-02 03:10:38 +0000813 if (PyThreadState_Swap(NULL) != tstate)
814 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000815 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000816
817 /* Other threads may run now */
818
Guido van Rossum65d5b571998-12-21 19:32:43 +0000819 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000820 if (PyThreadState_Swap(tstate) != NULL)
821 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000822
823 /* Check for thread interrupts */
824
825 if (tstate->async_exc != NULL) {
826 x = tstate->async_exc;
827 tstate->async_exc = NULL;
828 PyErr_SetNone(x);
829 Py_DECREF(x);
830 why = WHY_EXCEPTION;
831 goto on_error;
832 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000833 }
834#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000835 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000836
Neil Schemenauer63543862002-02-17 19:10:14 +0000837 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +0000838 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +0000839
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000840 /* line-by-line tracing support */
841
842 if (tstate->c_tracefunc != NULL && !tstate->tracing) {
843 /* see maybe_call_line_trace
844 for expository comments */
845 f->f_stacktop = stack_pointer;
Michael W. Hudson006c7522002-11-08 13:08:46 +0000846
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000847 err = maybe_call_line_trace(tstate->c_tracefunc,
848 tstate->c_traceobj,
Armin Rigobf57a142004-03-22 19:24:58 +0000849 f, &instr_lb, &instr_ub,
850 &instr_prev);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000851 /* Reload possibly changed frame fields */
852 JUMPTO(f->f_lasti);
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000853 if (f->f_stacktop != NULL) {
854 stack_pointer = f->f_stacktop;
855 f->f_stacktop = NULL;
856 }
857 if (err) {
858 /* trace function raised an exception */
859 goto on_error;
860 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000861 }
862
863 /* Extract opcode and argument */
864
Guido van Rossum374a9221991-04-04 10:40:29 +0000865 opcode = NEXTOP();
Armin Rigo9dbf9082004-03-20 21:50:13 +0000866 if (HAS_ARG(opcode))
867 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +0000868 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +0000869#ifdef DYNAMIC_EXECUTION_PROFILE
870#ifdef DXPAIRS
871 dxpairs[lastopcode][opcode]++;
872 lastopcode = opcode;
873#endif
874 dxp[opcode]++;
875#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000876
Guido van Rossum96a42c81992-01-12 02:29:51 +0000877#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +0000878 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +0000879
Guido van Rossum96a42c81992-01-12 02:29:51 +0000880 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000881 if (HAS_ARG(opcode)) {
882 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000883 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +0000884 }
885 else {
886 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000887 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +0000888 }
889 }
890#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000891
Guido van Rossum374a9221991-04-04 10:40:29 +0000892 /* Main switch on opcode */
Jeremy Hylton52820442001-01-03 23:52:36 +0000893
Guido van Rossum374a9221991-04-04 10:40:29 +0000894 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +0000895
Guido van Rossum374a9221991-04-04 10:40:29 +0000896 /* BEWARE!
897 It is essential that any operation that fails sets either
898 x to NULL, err to nonzero, or why to anything but WHY_NOT,
899 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000900
Guido van Rossum374a9221991-04-04 10:40:29 +0000901 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000902
Neil Schemenauer63543862002-02-17 19:10:14 +0000903 case LOAD_FAST:
904 x = GETLOCAL(oparg);
905 if (x != NULL) {
906 Py_INCREF(x);
907 PUSH(x);
908 goto fast_next_opcode;
909 }
910 format_exc_check_arg(PyExc_UnboundLocalError,
911 UNBOUNDLOCAL_ERROR_MSG,
912 PyTuple_GetItem(co->co_varnames, oparg));
913 break;
914
915 case LOAD_CONST:
Skip Montanaro04d80f82002-08-04 21:03:35 +0000916 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +0000917 Py_INCREF(x);
918 PUSH(x);
919 goto fast_next_opcode;
920
Raymond Hettinger7dc52212003-03-16 20:14:44 +0000921 PREDICTED_WITH_ARG(STORE_FAST);
Neil Schemenauer63543862002-02-17 19:10:14 +0000922 case STORE_FAST:
923 v = POP();
924 SETLOCAL(oparg, v);
925 goto fast_next_opcode;
926
Raymond Hettingerf606f872003-03-16 03:11:04 +0000927 PREDICTED(POP_TOP);
Guido van Rossum374a9221991-04-04 10:40:29 +0000928 case POP_TOP:
929 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000930 Py_DECREF(v);
Neil Schemenauer63543862002-02-17 19:10:14 +0000931 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000932
Guido van Rossum374a9221991-04-04 10:40:29 +0000933 case ROT_TWO:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000934 v = TOP();
935 w = SECOND();
936 SET_TOP(w);
937 SET_SECOND(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000938 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000939
Guido van Rossum374a9221991-04-04 10:40:29 +0000940 case ROT_THREE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000941 v = TOP();
942 w = SECOND();
943 x = THIRD();
944 SET_TOP(w);
945 SET_SECOND(x);
946 SET_THIRD(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000947 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000948
Thomas Wouters434d0822000-08-24 20:11:32 +0000949 case ROT_FOUR:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000950 u = TOP();
951 v = SECOND();
952 w = THIRD();
953 x = FOURTH();
954 SET_TOP(v);
955 SET_SECOND(w);
956 SET_THIRD(x);
957 SET_FOURTH(u);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000958 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000959
Guido van Rossum374a9221991-04-04 10:40:29 +0000960 case DUP_TOP:
961 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000962 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000963 PUSH(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000964 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000965
Thomas Wouters434d0822000-08-24 20:11:32 +0000966 case DUP_TOPX:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000967 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000968 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000969 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000970 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000971 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000972 STACKADJ(2);
973 SET_TOP(x);
974 SET_SECOND(w);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000975 goto fast_next_opcode;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000976 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000977 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000978 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000979 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000980 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000981 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +0000982 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000983 STACKADJ(3);
984 SET_TOP(x);
985 SET_SECOND(w);
986 SET_THIRD(v);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000987 goto fast_next_opcode;
Thomas Wouters434d0822000-08-24 20:11:32 +0000988 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000989 Py_FatalError("invalid argument to DUP_TOPX"
990 " (bytecode corruption?)");
Tim Peters35ba6892000-10-11 07:04:49 +0000991 break;
Thomas Wouters434d0822000-08-24 20:11:32 +0000992
Guido van Rossum374a9221991-04-04 10:40:29 +0000993 case UNARY_POSITIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000994 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000995 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000996 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000997 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000998 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000999 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001000
Guido van Rossum374a9221991-04-04 10:40:29 +00001001 case UNARY_NEGATIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001002 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001003 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001004 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001005 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001006 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001007 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001008
Guido van Rossum374a9221991-04-04 10:40:29 +00001009 case UNARY_NOT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001010 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001011 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001012 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +00001013 if (err == 0) {
1014 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001015 SET_TOP(Py_True);
Guido van Rossumfc490731997-05-06 15:06:49 +00001016 continue;
1017 }
1018 else if (err > 0) {
1019 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001020 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +00001021 err = 0;
1022 continue;
1023 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00001024 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001025 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001026
Guido van Rossum374a9221991-04-04 10:40:29 +00001027 case UNARY_CONVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001028 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001029 x = PyObject_Repr(v);
1030 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001031 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001032 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001033 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001034
Guido van Rossum7928cd71991-10-24 14:59:31 +00001035 case UNARY_INVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001036 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001037 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001038 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001039 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001040 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001041 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001042
Guido van Rossum50564e81996-01-12 01:13:16 +00001043 case BINARY_POWER:
1044 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001045 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001046 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +00001047 Py_DECREF(v);
1048 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001049 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001050 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +00001051 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001052
Guido van Rossum374a9221991-04-04 10:40:29 +00001053 case BINARY_MULTIPLY:
1054 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001055 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001056 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001057 Py_DECREF(v);
1058 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001059 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001060 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001061 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001062
Guido van Rossum374a9221991-04-04 10:40:29 +00001063 case BINARY_DIVIDE:
Tim Peters3caca232001-12-06 06:23:26 +00001064 if (!_Py_QnewFlag) {
1065 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001066 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001067 x = PyNumber_Divide(v, w);
1068 Py_DECREF(v);
1069 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001070 SET_TOP(x);
Tim Peters3caca232001-12-06 06:23:26 +00001071 if (x != NULL) continue;
1072 break;
1073 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001074 /* -Qnew is in effect: fall through to
Tim Peters3caca232001-12-06 06:23:26 +00001075 BINARY_TRUE_DIVIDE */
1076 case BINARY_TRUE_DIVIDE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001077 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001078 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001079 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001080 Py_DECREF(v);
1081 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001082 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001083 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001084 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001085
Guido van Rossum4668b002001-08-08 05:00:18 +00001086 case BINARY_FLOOR_DIVIDE:
1087 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001088 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001089 x = PyNumber_FloorDivide(v, w);
1090 Py_DECREF(v);
1091 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001092 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001093 if (x != NULL) continue;
1094 break;
1095
Guido van Rossum374a9221991-04-04 10:40:29 +00001096 case BINARY_MODULO:
1097 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001098 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001099 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001100 Py_DECREF(v);
1101 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001102 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001103 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001104 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001105
Guido van Rossum374a9221991-04-04 10:40:29 +00001106 case BINARY_ADD:
1107 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001108 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001109 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001110 /* INLINE: int + int */
1111 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001112 a = PyInt_AS_LONG(v);
1113 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001114 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001115 if ((i^a) < 0 && (i^b) < 0)
1116 goto slow_add;
1117 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001118 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001119 else {
1120 slow_add:
Guido van Rossumc12da691997-07-17 23:12:42 +00001121 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001122 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001123 Py_DECREF(v);
1124 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001125 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001126 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001127 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001128
Guido van Rossum374a9221991-04-04 10:40:29 +00001129 case BINARY_SUBTRACT:
1130 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001131 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001132 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001133 /* INLINE: int - int */
1134 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001135 a = PyInt_AS_LONG(v);
1136 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001137 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001138 if ((i^a) < 0 && (i^~b) < 0)
1139 goto slow_sub;
1140 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001141 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001142 else {
1143 slow_sub:
Guido van Rossumc12da691997-07-17 23:12:42 +00001144 x = PyNumber_Subtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001145 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001146 Py_DECREF(v);
1147 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001148 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001149 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001150 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001151
Guido van Rossum374a9221991-04-04 10:40:29 +00001152 case BINARY_SUBSCR:
1153 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001154 v = TOP();
Tim Petersb1c46982001-10-05 20:41:38 +00001155 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001156 /* INLINE: list[int] */
1157 long i = PyInt_AsLong(w);
1158 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +00001159 i += PyList_GET_SIZE(v);
Guido van Rossumc12da691997-07-17 23:12:42 +00001160 if (i < 0 ||
Guido van Rossumfa00e951998-07-08 15:02:37 +00001161 i >= PyList_GET_SIZE(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001162 PyErr_SetString(PyExc_IndexError,
1163 "list index out of range");
1164 x = NULL;
1165 }
1166 else {
Guido van Rossumfa00e951998-07-08 15:02:37 +00001167 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001168 Py_INCREF(x);
1169 }
1170 }
1171 else
1172 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001173 Py_DECREF(v);
1174 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001175 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001176 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001177 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001178
Guido van Rossum7928cd71991-10-24 14:59:31 +00001179 case BINARY_LSHIFT:
1180 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001181 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001182 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001183 Py_DECREF(v);
1184 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001185 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001186 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001187 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001188
Guido van Rossum7928cd71991-10-24 14:59:31 +00001189 case BINARY_RSHIFT:
1190 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001191 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001192 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001193 Py_DECREF(v);
1194 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001195 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001196 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001197 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001198
Guido van Rossum7928cd71991-10-24 14:59:31 +00001199 case BINARY_AND:
1200 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001201 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001202 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001203 Py_DECREF(v);
1204 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001205 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001206 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001207 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001208
Guido van Rossum7928cd71991-10-24 14:59:31 +00001209 case BINARY_XOR:
1210 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001211 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001212 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001213 Py_DECREF(v);
1214 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001215 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001216 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001217 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001218
Guido van Rossum7928cd71991-10-24 14:59:31 +00001219 case BINARY_OR:
1220 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001221 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001222 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001223 Py_DECREF(v);
1224 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001225 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001226 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001227 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001228
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001229 case LIST_APPEND:
1230 w = POP();
1231 v = POP();
1232 err = PyList_Append(v, w);
1233 Py_DECREF(v);
1234 Py_DECREF(w);
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001235 if (err == 0) {
1236 PREDICT(JUMP_ABSOLUTE);
1237 continue;
1238 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001239 break;
1240
Thomas Wouters434d0822000-08-24 20:11:32 +00001241 case INPLACE_POWER:
1242 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001243 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001244 x = PyNumber_InPlacePower(v, w, Py_None);
1245 Py_DECREF(v);
1246 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001247 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001248 if (x != NULL) continue;
1249 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001250
Thomas Wouters434d0822000-08-24 20:11:32 +00001251 case INPLACE_MULTIPLY:
1252 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001253 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001254 x = PyNumber_InPlaceMultiply(v, w);
1255 Py_DECREF(v);
1256 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001257 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001258 if (x != NULL) continue;
1259 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001260
Thomas Wouters434d0822000-08-24 20:11:32 +00001261 case INPLACE_DIVIDE:
Tim Peters54b11912001-12-25 18:49:11 +00001262 if (!_Py_QnewFlag) {
1263 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001264 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001265 x = PyNumber_InPlaceDivide(v, w);
1266 Py_DECREF(v);
1267 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001268 SET_TOP(x);
Tim Peters54b11912001-12-25 18:49:11 +00001269 if (x != NULL) continue;
1270 break;
1271 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001272 /* -Qnew is in effect: fall through to
Tim Peters54b11912001-12-25 18:49:11 +00001273 INPLACE_TRUE_DIVIDE */
1274 case INPLACE_TRUE_DIVIDE:
Thomas Wouters434d0822000-08-24 20:11:32 +00001275 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001276 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001277 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001278 Py_DECREF(v);
1279 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001280 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001281 if (x != NULL) continue;
1282 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001283
Guido van Rossum4668b002001-08-08 05:00:18 +00001284 case INPLACE_FLOOR_DIVIDE:
1285 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001286 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001287 x = PyNumber_InPlaceFloorDivide(v, w);
1288 Py_DECREF(v);
1289 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001290 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001291 if (x != NULL) continue;
1292 break;
1293
Thomas Wouters434d0822000-08-24 20:11:32 +00001294 case INPLACE_MODULO:
1295 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001296 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001297 x = PyNumber_InPlaceRemainder(v, w);
1298 Py_DECREF(v);
1299 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001300 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001301 if (x != NULL) continue;
1302 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001303
Thomas Wouters434d0822000-08-24 20:11:32 +00001304 case INPLACE_ADD:
1305 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001306 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001307 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001308 /* INLINE: int + int */
1309 register long a, b, i;
1310 a = PyInt_AS_LONG(v);
1311 b = PyInt_AS_LONG(w);
1312 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001313 if ((i^a) < 0 && (i^b) < 0)
1314 goto slow_iadd;
1315 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001316 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001317 else {
1318 slow_iadd:
Thomas Wouters434d0822000-08-24 20:11:32 +00001319 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001320 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001321 Py_DECREF(v);
1322 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001323 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001324 if (x != NULL) continue;
1325 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001326
Thomas Wouters434d0822000-08-24 20:11:32 +00001327 case INPLACE_SUBTRACT:
1328 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001329 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001330 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001331 /* INLINE: int - int */
1332 register long a, b, i;
1333 a = PyInt_AS_LONG(v);
1334 b = PyInt_AS_LONG(w);
1335 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001336 if ((i^a) < 0 && (i^~b) < 0)
1337 goto slow_isub;
1338 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001339 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001340 else {
1341 slow_isub:
Thomas Wouters434d0822000-08-24 20:11:32 +00001342 x = PyNumber_InPlaceSubtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001343 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001344 Py_DECREF(v);
1345 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001346 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001347 if (x != NULL) continue;
1348 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001349
Thomas Wouters434d0822000-08-24 20:11:32 +00001350 case INPLACE_LSHIFT:
1351 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001352 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001353 x = PyNumber_InPlaceLshift(v, w);
1354 Py_DECREF(v);
1355 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001356 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001357 if (x != NULL) continue;
1358 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001359
Thomas Wouters434d0822000-08-24 20:11:32 +00001360 case INPLACE_RSHIFT:
1361 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001362 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001363 x = PyNumber_InPlaceRshift(v, w);
1364 Py_DECREF(v);
1365 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001366 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001367 if (x != NULL) continue;
1368 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001369
Thomas Wouters434d0822000-08-24 20:11:32 +00001370 case INPLACE_AND:
1371 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001372 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001373 x = PyNumber_InPlaceAnd(v, w);
1374 Py_DECREF(v);
1375 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001376 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001377 if (x != NULL) continue;
1378 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001379
Thomas Wouters434d0822000-08-24 20:11:32 +00001380 case INPLACE_XOR:
1381 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001382 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001383 x = PyNumber_InPlaceXor(v, w);
1384 Py_DECREF(v);
1385 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001386 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001387 if (x != NULL) continue;
1388 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001389
Thomas Wouters434d0822000-08-24 20:11:32 +00001390 case INPLACE_OR:
1391 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001392 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001393 x = PyNumber_InPlaceOr(v, w);
1394 Py_DECREF(v);
1395 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001396 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001397 if (x != NULL) continue;
1398 break;
1399
Guido van Rossum374a9221991-04-04 10:40:29 +00001400 case SLICE+0:
1401 case SLICE+1:
1402 case SLICE+2:
1403 case SLICE+3:
1404 if ((opcode-SLICE) & 2)
1405 w = POP();
1406 else
1407 w = NULL;
1408 if ((opcode-SLICE) & 1)
1409 v = POP();
1410 else
1411 v = NULL;
Raymond Hettinger663004b2003-01-09 15:24:30 +00001412 u = TOP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001413 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001414 Py_DECREF(u);
1415 Py_XDECREF(v);
1416 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001417 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001418 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001419 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001420
Guido van Rossum374a9221991-04-04 10:40:29 +00001421 case STORE_SLICE+0:
1422 case STORE_SLICE+1:
1423 case STORE_SLICE+2:
1424 case STORE_SLICE+3:
1425 if ((opcode-STORE_SLICE) & 2)
1426 w = POP();
1427 else
1428 w = NULL;
1429 if ((opcode-STORE_SLICE) & 1)
1430 v = POP();
1431 else
1432 v = NULL;
1433 u = POP();
1434 t = POP();
1435 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +00001436 Py_DECREF(t);
1437 Py_DECREF(u);
1438 Py_XDECREF(v);
1439 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001440 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001441 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001442
Guido van Rossum374a9221991-04-04 10:40:29 +00001443 case DELETE_SLICE+0:
1444 case DELETE_SLICE+1:
1445 case DELETE_SLICE+2:
1446 case DELETE_SLICE+3:
1447 if ((opcode-DELETE_SLICE) & 2)
1448 w = POP();
1449 else
1450 w = NULL;
1451 if ((opcode-DELETE_SLICE) & 1)
1452 v = POP();
1453 else
1454 v = NULL;
1455 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001456 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +00001457 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00001458 Py_DECREF(u);
1459 Py_XDECREF(v);
1460 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001461 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001462 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001463
Guido van Rossum374a9221991-04-04 10:40:29 +00001464 case STORE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001465 w = TOP();
1466 v = SECOND();
1467 u = THIRD();
1468 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001469 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001470 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001471 Py_DECREF(u);
1472 Py_DECREF(v);
1473 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001474 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001475 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001476
Guido van Rossum374a9221991-04-04 10:40:29 +00001477 case DELETE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001478 w = TOP();
1479 v = SECOND();
1480 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001481 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001482 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001483 Py_DECREF(v);
1484 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001485 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001486 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001487
Guido van Rossum374a9221991-04-04 10:40:29 +00001488 case PRINT_EXPR:
1489 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001490 w = PySys_GetObject("displayhook");
1491 if (w == NULL) {
1492 PyErr_SetString(PyExc_RuntimeError,
1493 "lost sys.displayhook");
1494 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001495 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001496 }
1497 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001498 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001499 if (x == NULL)
1500 err = -1;
1501 }
1502 if (err == 0) {
1503 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001504 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001505 if (w == NULL)
1506 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001507 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001508 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001509 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001510 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001511
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001512 case PRINT_ITEM_TO:
1513 w = stream = POP();
1514 /* fall through to PRINT_ITEM */
1515
Guido van Rossum374a9221991-04-04 10:40:29 +00001516 case PRINT_ITEM:
1517 v = POP();
Barry Warsaw093abe02000-08-29 04:56:13 +00001518 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001519 w = PySys_GetObject("stdout");
1520 if (w == NULL) {
1521 PyErr_SetString(PyExc_RuntimeError,
1522 "lost sys.stdout");
1523 err = -1;
1524 }
Guido van Rossum8f183201997-12-31 05:53:15 +00001525 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001526 /* PyFile_SoftSpace() can exececute arbitrary code
1527 if sys.stdout is an instance with a __getattr__.
1528 If __getattr__ raises an exception, w will
1529 be freed, so we need to prevent that temporarily. */
1530 Py_XINCREF(w);
Tim Peters8e5fd532002-03-24 19:25:00 +00001531 if (w != NULL && PyFile_SoftSpace(w, 0))
Guido van Rossumbe270261997-05-22 22:26:18 +00001532 err = PyFile_WriteString(" ", w);
1533 if (err == 0)
1534 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001535 if (err == 0) {
Tim Peters8e5fd532002-03-24 19:25:00 +00001536 /* XXX move into writeobject() ? */
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001537 if (PyString_Check(v)) {
1538 char *s = PyString_AS_STRING(v);
1539 int len = PyString_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001540 if (len == 0 ||
1541 !isspace(Py_CHARMASK(s[len-1])) ||
1542 s[len-1] == ' ')
1543 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001544 }
Martin v. Löwis8d3ce5a2001-12-18 22:36:40 +00001545#ifdef Py_USING_UNICODE
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001546 else if (PyUnicode_Check(v)) {
1547 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
1548 int len = PyUnicode_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001549 if (len == 0 ||
1550 !Py_UNICODE_ISSPACE(s[len-1]) ||
1551 s[len-1] == ' ')
1552 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001553 }
Michael W. Hudsond95c8282002-05-20 13:56:11 +00001554#endif
Tim Peters8e5fd532002-03-24 19:25:00 +00001555 else
1556 PyFile_SoftSpace(w, 1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001557 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001558 Py_XDECREF(w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001559 Py_DECREF(v);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001560 Py_XDECREF(stream);
1561 stream = NULL;
1562 if (err == 0)
1563 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001564 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001565
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001566 case PRINT_NEWLINE_TO:
1567 w = stream = POP();
1568 /* fall through to PRINT_NEWLINE */
1569
Guido van Rossum374a9221991-04-04 10:40:29 +00001570 case PRINT_NEWLINE:
Barry Warsaw093abe02000-08-29 04:56:13 +00001571 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001572 w = PySys_GetObject("stdout");
1573 if (w == NULL)
1574 PyErr_SetString(PyExc_RuntimeError,
1575 "lost sys.stdout");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001576 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001577 if (w != NULL) {
1578 err = PyFile_WriteString("\n", w);
1579 if (err == 0)
1580 PyFile_SoftSpace(w, 0);
1581 }
1582 Py_XDECREF(stream);
1583 stream = NULL;
Guido van Rossum374a9221991-04-04 10:40:29 +00001584 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001585
Thomas Wouters434d0822000-08-24 20:11:32 +00001586
1587#ifdef CASE_TOO_BIG
1588 default: switch (opcode) {
1589#endif
Guido van Rossumf10570b1995-07-07 22:53:21 +00001590 case RAISE_VARARGS:
1591 u = v = w = NULL;
1592 switch (oparg) {
1593 case 3:
1594 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001595 /* Fallthrough */
1596 case 2:
1597 v = POP(); /* value */
1598 /* Fallthrough */
1599 case 1:
1600 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001601 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001602 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001603 break;
1604 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001605 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001606 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001607 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001608 break;
1609 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001610 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001611
Guido van Rossum374a9221991-04-04 10:40:29 +00001612 case LOAD_LOCALS:
Guido van Rossum681d79a1995-07-18 14:51:37 +00001613 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001614 PyErr_SetString(PyExc_SystemError,
1615 "no locals");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001616 break;
1617 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001618 Py_INCREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001619 PUSH(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001620 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001621
Guido van Rossum374a9221991-04-04 10:40:29 +00001622 case RETURN_VALUE:
1623 retval = POP();
1624 why = WHY_RETURN;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001625 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001626
Tim Peters5ca576e2001-06-18 22:08:13 +00001627 case YIELD_VALUE:
1628 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001629 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001630 why = WHY_YIELD;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001631 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001632
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001633 case EXEC_STMT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001634 w = TOP();
1635 v = SECOND();
1636 u = THIRD();
1637 STACKADJ(-3);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001638 err = exec_statement(f, u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001639 Py_DECREF(u);
1640 Py_DECREF(v);
1641 Py_DECREF(w);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001642 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001643
Guido van Rossum374a9221991-04-04 10:40:29 +00001644 case POP_BLOCK:
1645 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001646 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001647 while (STACK_LEVEL() > b->b_level) {
1648 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001649 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001650 }
1651 }
1652 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001653
Guido van Rossum374a9221991-04-04 10:40:29 +00001654 case END_FINALLY:
1655 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001656 if (PyInt_Check(v)) {
Raymond Hettinger080cb322003-03-14 01:37:42 +00001657 why = (enum why_code) PyInt_AS_LONG(v);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00001658 if (why == WHY_RETURN ||
Tim Peters5ca576e2001-06-18 22:08:13 +00001659 why == WHY_YIELD ||
Guido van Rossumc5fe5eb2002-06-12 03:45:21 +00001660 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001661 retval = POP();
1662 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001663 else if (PyString_Check(v) || PyClass_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001664 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001665 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001666 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001667 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001668 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001669 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001670 else if (v != Py_None) {
1671 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001672 "'finally' pops bad exception");
1673 why = WHY_EXCEPTION;
1674 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001675 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001676 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001677
Guido van Rossum374a9221991-04-04 10:40:29 +00001678 case BUILD_CLASS:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001679 u = TOP();
1680 v = SECOND();
1681 w = THIRD();
1682 STACKADJ(-2);
Guido van Rossum25831651993-05-19 14:50:45 +00001683 x = build_class(u, v, w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001684 SET_TOP(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001685 Py_DECREF(u);
1686 Py_DECREF(v);
1687 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001688 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001689
Guido van Rossum374a9221991-04-04 10:40:29 +00001690 case STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001691 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001692 v = POP();
Guido van Rossum681d79a1995-07-18 14:51:37 +00001693 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001694 PyErr_Format(PyExc_SystemError,
1695 "no locals found when storing %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001696 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001697 break;
1698 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001699 err = PyDict_SetItem(x, w, v);
1700 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001701 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001702
Guido van Rossum374a9221991-04-04 10:40:29 +00001703 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001704 w = GETITEM(names, oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001705 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001706 PyErr_Format(PyExc_SystemError,
1707 "no locals when deleting %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001708 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001709 break;
1710 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001711 if ((err = PyDict_DelItem(x, w)) != 0)
Guido van Rossumac7be682001-01-17 15:42:30 +00001712 format_exc_check_arg(PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001713 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001714 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001715
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001716 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001717 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001718 v = POP();
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001719 if (PyTuple_CheckExact(v) && PyTuple_GET_SIZE(v) == oparg) {
1720 PyObject **items = ((PyTupleObject *)v)->ob_item;
1721 while (oparg--) {
1722 w = items[oparg];
1723 Py_INCREF(w);
1724 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001725 }
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001726 } else if (PyList_CheckExact(v) && PyList_GET_SIZE(v) == oparg) {
1727 PyObject **items = ((PyListObject *)v)->ob_item;
1728 while (oparg--) {
1729 w = items[oparg];
1730 Py_INCREF(w);
1731 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001732 }
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001733 } else if (unpack_iterable(v, oparg,
Tim Petersd6d010b2001-06-21 02:49:55 +00001734 stack_pointer + oparg))
1735 stack_pointer += oparg;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001736 else {
1737 if (PyErr_ExceptionMatches(PyExc_TypeError))
1738 PyErr_SetString(PyExc_TypeError,
1739 "unpack non-sequence");
Barry Warsawe42b18f1997-08-25 22:13:04 +00001740 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001741 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001742 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001743 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001744
Guido van Rossum374a9221991-04-04 10:40:29 +00001745 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001746 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001747 v = TOP();
1748 u = SECOND();
1749 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001750 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1751 Py_DECREF(v);
1752 Py_DECREF(u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001753 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001754
Guido van Rossum374a9221991-04-04 10:40:29 +00001755 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001756 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001757 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001758 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1759 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001760 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001761 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001762
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001763 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001764 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001765 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001766 err = PyDict_SetItem(f->f_globals, w, v);
1767 Py_DECREF(v);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001768 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001769
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001770 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001771 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001772 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001773 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001774 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001775 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001776
Guido van Rossum374a9221991-04-04 10:40:29 +00001777 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001778 w = GETITEM(names, oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001779 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001780 PyErr_Format(PyExc_SystemError,
1781 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001782 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001783 break;
1784 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001785 x = PyDict_GetItem(x, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001786 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001787 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001788 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001789 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001790 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001791 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001792 PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001793 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001794 break;
1795 }
1796 }
1797 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001798 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001799 PUSH(x);
1800 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001801
Guido van Rossum374a9221991-04-04 10:40:29 +00001802 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001803 w = GETITEM(names, oparg);
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001804 if (PyString_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00001805 /* Inline the PyDict_GetItem() calls.
1806 WARNING: this is an extreme speed hack.
1807 Do not try this at home. */
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001808 long hash = ((PyStringObject *)w)->ob_shash;
1809 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001810 PyDictObject *d;
1811 d = (PyDictObject *)(f->f_globals);
1812 x = d->ma_lookup(d, w, hash)->me_value;
1813 if (x != NULL) {
1814 Py_INCREF(x);
1815 PUSH(x);
1816 continue;
1817 }
1818 d = (PyDictObject *)(f->f_builtins);
1819 x = d->ma_lookup(d, w, hash)->me_value;
1820 if (x != NULL) {
1821 Py_INCREF(x);
1822 PUSH(x);
1823 continue;
1824 }
1825 goto load_global_error;
1826 }
1827 }
1828 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00001829 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001830 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001831 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001832 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001833 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00001834 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001835 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001836 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001837 break;
1838 }
1839 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001840 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001841 PUSH(x);
1842 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001843
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001844 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001845 x = GETLOCAL(oparg);
1846 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001847 format_exc_check_arg(
1848 PyExc_UnboundLocalError,
1849 UNBOUNDLOCAL_ERROR_MSG,
1850 PyTuple_GetItem(co->co_varnames, oparg)
1851 );
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001852 break;
1853 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00001854 SETLOCAL(oparg, NULL);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001855 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001856
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001857 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001858 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001859 Py_INCREF(x);
1860 PUSH(x);
1861 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);
Jeremy Hylton2524d692001-02-05 17:23:16 +00001866 if (w == NULL) {
Jeremy Hylton76c81ee2002-07-11 16:56:38 +00001867 err = -1;
1868 /* Don't stomp existing exception */
1869 if (PyErr_Occurred())
1870 break;
Jeremy Hyltonc76770c2001-04-13 16:51:46 +00001871 if (oparg < f->f_ncells) {
Jeremy Hylton2524d692001-02-05 17:23:16 +00001872 v = PyTuple_GetItem(co->co_cellvars,
1873 oparg);
Jeremy Hyltonc76770c2001-04-13 16:51:46 +00001874 format_exc_check_arg(
1875 PyExc_UnboundLocalError,
1876 UNBOUNDLOCAL_ERROR_MSG,
1877 v);
1878 } else {
Jeremy Hylton2524d692001-02-05 17:23:16 +00001879 v = PyTuple_GetItem(
1880 co->co_freevars,
1881 oparg - f->f_ncells);
Jeremy Hyltonc76770c2001-04-13 16:51:46 +00001882 format_exc_check_arg(
1883 PyExc_NameError,
1884 UNBOUNDFREE_ERROR_MSG,
1885 v);
1886 }
Jeremy Hylton2524d692001-02-05 17:23:16 +00001887 break;
1888 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001889 PUSH(w);
1890 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) {
1902 for (; --oparg >= 0;) {
1903 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) {
1914 for (; --oparg >= 0;) {
1915 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 }
2186 while (--oparg >= 0) {
2187 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002188 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002189 }
2190 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002191 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002192 }
2193 PUSH(x);
2194 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002195
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002196 case MAKE_CLOSURE:
2197 {
2198 int nfree;
2199 v = POP(); /* code object */
2200 x = PyFunction_New(v, f->f_globals);
Jeremy Hylton733c8932001-12-13 19:51:56 +00002201 nfree = PyCode_GetNumFree((PyCodeObject *)v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002202 Py_DECREF(v);
2203 /* XXX Maybe this should be a separate opcode? */
2204 if (x != NULL && nfree > 0) {
2205 v = PyTuple_New(nfree);
2206 if (v == NULL) {
2207 Py_DECREF(x);
2208 x = NULL;
2209 break;
2210 }
2211 while (--nfree >= 0) {
2212 w = POP();
2213 PyTuple_SET_ITEM(v, nfree, w);
2214 }
2215 err = PyFunction_SetClosure(x, v);
2216 Py_DECREF(v);
2217 }
2218 if (x != NULL && oparg > 0) {
2219 v = PyTuple_New(oparg);
2220 if (v == NULL) {
2221 Py_DECREF(x);
2222 x = NULL;
2223 break;
2224 }
2225 while (--oparg >= 0) {
2226 w = POP();
2227 PyTuple_SET_ITEM(v, oparg, w);
2228 }
2229 err = PyFunction_SetDefaults(x, v);
2230 Py_DECREF(v);
2231 }
2232 PUSH(x);
2233 break;
2234 }
2235
Guido van Rossum8861b741996-07-30 16:49:37 +00002236 case BUILD_SLICE:
2237 if (oparg == 3)
2238 w = POP();
2239 else
2240 w = NULL;
2241 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002242 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002243 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002244 Py_DECREF(u);
2245 Py_DECREF(v);
2246 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002247 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002248 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002249 break;
2250
Fred Drakeef8ace32000-08-24 00:32:09 +00002251 case EXTENDED_ARG:
2252 opcode = NEXTOP();
Armin Rigo9dbf9082004-03-20 21:50:13 +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
Guido van Rossum374a9221991-04-04 10:40:29 +00002293 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002294 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002295 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002296 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002297 why = WHY_EXCEPTION;
2298 }
2299 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002300#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002301 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002302 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002303 if (PyErr_Occurred()) {
Jeremy Hylton904ed862003-11-05 17:29:35 +00002304 char buf[1024];
2305 sprintf(buf, "Stack unwind with exception "
2306 "set and why=%d", why);
2307 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002308 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002309 }
2310#endif
2311
2312 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002313
Guido van Rossum374a9221991-04-04 10:40:29 +00002314 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002315 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002316
Fred Drake8f51f542001-10-04 14:48:42 +00002317 if (tstate->c_tracefunc != NULL)
2318 call_exc_trace(tstate->c_tracefunc,
2319 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002320 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002321
Guido van Rossum374a9221991-04-04 10:40:29 +00002322 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002323
Guido van Rossum374a9221991-04-04 10:40:29 +00002324 if (why == WHY_RERAISE)
2325 why = WHY_EXCEPTION;
2326
2327 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002328
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002329fast_block_end:
Tim Peters5ca576e2001-06-18 22:08:13 +00002330 while (why != WHY_NOT && why != WHY_YIELD && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002331 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002332
2333 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2334 /* For a continue inside a try block,
2335 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002336 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2337 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002338 why = WHY_NOT;
2339 JUMPTO(PyInt_AS_LONG(retval));
2340 Py_DECREF(retval);
2341 break;
2342 }
2343
Guido van Rossum374a9221991-04-04 10:40:29 +00002344 while (STACK_LEVEL() > b->b_level) {
2345 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002346 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002347 }
2348 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2349 why = WHY_NOT;
2350 JUMPTO(b->b_handler);
2351 break;
2352 }
2353 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002354 (b->b_type == SETUP_EXCEPT &&
2355 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002356 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002357 PyObject *exc, *val, *tb;
2358 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002359 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002360 val = Py_None;
2361 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002362 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002363 /* Make the raw exception data
2364 available to the handler,
2365 so a program can emulate the
2366 Python main loop. Don't do
2367 this for 'finally'. */
2368 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002369 PyErr_NormalizeException(
2370 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002371 set_exc_info(tstate,
2372 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002373 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002374 if (tb == NULL) {
2375 Py_INCREF(Py_None);
2376 PUSH(Py_None);
2377 } else
2378 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002379 PUSH(val);
2380 PUSH(exc);
2381 }
2382 else {
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002383 if (why == WHY_RETURN ||
Guido van Rossumc5fe5eb2002-06-12 03:45:21 +00002384 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00002385 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002386 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002387 PUSH(v);
2388 }
2389 why = WHY_NOT;
2390 JUMPTO(b->b_handler);
2391 break;
2392 }
2393 } /* unwind stack */
2394
2395 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002396
Guido van Rossum374a9221991-04-04 10:40:29 +00002397 if (why != WHY_NOT)
2398 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002399
Guido van Rossum374a9221991-04-04 10:40:29 +00002400 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002401
Guido van Rossum35974fb2001-12-06 21:28:18 +00002402 if (why != WHY_YIELD) {
2403 /* Pop remaining stack entries -- but when yielding */
2404 while (!EMPTY()) {
2405 v = POP();
2406 Py_XDECREF(v);
2407 }
2408 }
2409
Tim Peters5ca576e2001-06-18 22:08:13 +00002410 if (why != WHY_RETURN && why != WHY_YIELD)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002411 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002412
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002413fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002414 if (tstate->use_tracing) {
2415 if (tstate->c_tracefunc
2416 && (why == WHY_RETURN || why == WHY_YIELD)) {
2417 if (call_trace(tstate->c_tracefunc,
2418 tstate->c_traceobj, f,
2419 PyTrace_RETURN, retval)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002420 Py_XDECREF(retval);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002421 retval = NULL;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002422 why = WHY_EXCEPTION;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002423 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002424 }
Fred Drake8f51f542001-10-04 14:48:42 +00002425 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002426 if (why == WHY_EXCEPTION)
2427 call_trace_protected(tstate->c_profilefunc,
2428 tstate->c_profileobj, f,
2429 PyTrace_RETURN);
2430 else if (call_trace(tstate->c_profilefunc,
2431 tstate->c_profileobj, f,
2432 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002433 Py_XDECREF(retval);
2434 retval = NULL;
2435 why = WHY_EXCEPTION;
2436 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002437 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002438 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002439
Guido van Rossuma027efa1997-05-05 20:56:21 +00002440 reset_exc_info(tstate);
2441
Tim Peters5ca576e2001-06-18 22:08:13 +00002442 /* pop frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +00002443 exit_eval_frame:
2444 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002445 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002446
Guido van Rossum96a42c81992-01-12 02:29:51 +00002447 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002448}
2449
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002450/* this is gonna seem *real weird*, but if you put some other code between
2451 eval_frame() and PyEval_EvalCodeEx() you will need to adjust the test in
2452 the if statement in Misc/gdbinit:ppystack */
2453
Tim Peters6d6c1a32001-08-02 04:15:00 +00002454PyObject *
2455PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002456 PyObject **args, int argcount, PyObject **kws, int kwcount,
2457 PyObject **defs, int defcount, PyObject *closure)
2458{
2459 register PyFrameObject *f;
2460 register PyObject *retval = NULL;
2461 register PyObject **fastlocals, **freevars;
2462 PyThreadState *tstate = PyThreadState_GET();
2463 PyObject *x, *u;
2464
2465 if (globals == NULL) {
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002466 PyErr_SetString(PyExc_SystemError,
2467 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002468 return NULL;
2469 }
2470
Jeremy Hylton985eba52003-02-05 23:13:00 +00002471 assert(globals != NULL);
2472 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002473 if (f == NULL)
2474 return NULL;
2475
2476 fastlocals = f->f_localsplus;
2477 freevars = f->f_localsplus + f->f_nlocals;
2478
2479 if (co->co_argcount > 0 ||
2480 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2481 int i;
2482 int n = argcount;
2483 PyObject *kwdict = NULL;
2484 if (co->co_flags & CO_VARKEYWORDS) {
2485 kwdict = PyDict_New();
2486 if (kwdict == NULL)
2487 goto fail;
2488 i = co->co_argcount;
2489 if (co->co_flags & CO_VARARGS)
2490 i++;
2491 SETLOCAL(i, kwdict);
2492 }
2493 if (argcount > co->co_argcount) {
2494 if (!(co->co_flags & CO_VARARGS)) {
2495 PyErr_Format(PyExc_TypeError,
2496 "%.200s() takes %s %d "
2497 "%sargument%s (%d given)",
2498 PyString_AsString(co->co_name),
2499 defcount ? "at most" : "exactly",
2500 co->co_argcount,
2501 kwcount ? "non-keyword " : "",
2502 co->co_argcount == 1 ? "" : "s",
2503 argcount);
2504 goto fail;
2505 }
2506 n = co->co_argcount;
2507 }
2508 for (i = 0; i < n; i++) {
2509 x = args[i];
2510 Py_INCREF(x);
2511 SETLOCAL(i, x);
2512 }
2513 if (co->co_flags & CO_VARARGS) {
2514 u = PyTuple_New(argcount - n);
2515 if (u == NULL)
2516 goto fail;
2517 SETLOCAL(co->co_argcount, u);
2518 for (i = n; i < argcount; i++) {
2519 x = args[i];
2520 Py_INCREF(x);
2521 PyTuple_SET_ITEM(u, i-n, x);
2522 }
2523 }
2524 for (i = 0; i < kwcount; i++) {
2525 PyObject *keyword = kws[2*i];
2526 PyObject *value = kws[2*i + 1];
2527 int j;
2528 if (keyword == NULL || !PyString_Check(keyword)) {
2529 PyErr_Format(PyExc_TypeError,
2530 "%.200s() keywords must be strings",
2531 PyString_AsString(co->co_name));
2532 goto fail;
2533 }
2534 /* XXX slow -- speed up using dictionary? */
2535 for (j = 0; j < co->co_argcount; j++) {
2536 PyObject *nm = PyTuple_GET_ITEM(
2537 co->co_varnames, j);
2538 int cmp = PyObject_RichCompareBool(
2539 keyword, nm, Py_EQ);
2540 if (cmp > 0)
2541 break;
2542 else if (cmp < 0)
2543 goto fail;
2544 }
2545 /* Check errors from Compare */
2546 if (PyErr_Occurred())
2547 goto fail;
2548 if (j >= co->co_argcount) {
2549 if (kwdict == NULL) {
2550 PyErr_Format(PyExc_TypeError,
2551 "%.200s() got an unexpected "
2552 "keyword argument '%.400s'",
2553 PyString_AsString(co->co_name),
2554 PyString_AsString(keyword));
2555 goto fail;
2556 }
2557 PyDict_SetItem(kwdict, keyword, value);
2558 }
2559 else {
2560 if (GETLOCAL(j) != NULL) {
2561 PyErr_Format(PyExc_TypeError,
2562 "%.200s() got multiple "
2563 "values for keyword "
2564 "argument '%.400s'",
2565 PyString_AsString(co->co_name),
2566 PyString_AsString(keyword));
2567 goto fail;
2568 }
2569 Py_INCREF(value);
2570 SETLOCAL(j, value);
2571 }
2572 }
2573 if (argcount < co->co_argcount) {
2574 int m = co->co_argcount - defcount;
2575 for (i = argcount; i < m; i++) {
2576 if (GETLOCAL(i) == NULL) {
2577 PyErr_Format(PyExc_TypeError,
2578 "%.200s() takes %s %d "
2579 "%sargument%s (%d given)",
2580 PyString_AsString(co->co_name),
2581 ((co->co_flags & CO_VARARGS) ||
2582 defcount) ? "at least"
2583 : "exactly",
2584 m, kwcount ? "non-keyword " : "",
2585 m == 1 ? "" : "s", i);
2586 goto fail;
2587 }
2588 }
2589 if (n > m)
2590 i = n - m;
2591 else
2592 i = 0;
2593 for (; i < defcount; i++) {
2594 if (GETLOCAL(m+i) == NULL) {
2595 PyObject *def = defs[i];
2596 Py_INCREF(def);
2597 SETLOCAL(m+i, def);
2598 }
2599 }
2600 }
2601 }
2602 else {
2603 if (argcount > 0 || kwcount > 0) {
2604 PyErr_Format(PyExc_TypeError,
2605 "%.200s() takes no arguments (%d given)",
2606 PyString_AsString(co->co_name),
2607 argcount + kwcount);
2608 goto fail;
2609 }
2610 }
2611 /* Allocate and initialize storage for cell vars, and copy free
2612 vars into frame. This isn't too efficient right now. */
2613 if (f->f_ncells) {
2614 int i = 0, j = 0, nargs, found;
2615 char *cellname, *argname;
2616 PyObject *c;
2617
2618 nargs = co->co_argcount;
2619 if (co->co_flags & CO_VARARGS)
2620 nargs++;
2621 if (co->co_flags & CO_VARKEYWORDS)
2622 nargs++;
2623
2624 /* Check for cells that shadow args */
2625 for (i = 0; i < f->f_ncells && j < nargs; ++i) {
2626 cellname = PyString_AS_STRING(
2627 PyTuple_GET_ITEM(co->co_cellvars, i));
2628 found = 0;
2629 while (j < nargs) {
2630 argname = PyString_AS_STRING(
2631 PyTuple_GET_ITEM(co->co_varnames, j));
2632 if (strcmp(cellname, argname) == 0) {
2633 c = PyCell_New(GETLOCAL(j));
2634 if (c == NULL)
2635 goto fail;
2636 GETLOCAL(f->f_nlocals + i) = c;
2637 found = 1;
2638 break;
2639 }
2640 j++;
2641 }
2642 if (found == 0) {
2643 c = PyCell_New(NULL);
2644 if (c == NULL)
2645 goto fail;
2646 SETLOCAL(f->f_nlocals + i, c);
2647 }
2648 }
2649 /* Initialize any that are left */
2650 while (i < f->f_ncells) {
2651 c = PyCell_New(NULL);
2652 if (c == NULL)
2653 goto fail;
2654 SETLOCAL(f->f_nlocals + i, c);
2655 i++;
2656 }
2657 }
2658 if (f->f_nfreevars) {
2659 int i;
2660 for (i = 0; i < f->f_nfreevars; ++i) {
2661 PyObject *o = PyTuple_GET_ITEM(closure, i);
2662 Py_INCREF(o);
2663 freevars[f->f_ncells + i] = o;
2664 }
2665 }
2666
Tim Peters5ca576e2001-06-18 22:08:13 +00002667 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002668 /* Don't need to keep the reference to f_back, it will be set
2669 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002670 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002671 f->f_back = NULL;
2672
Jeremy Hylton985eba52003-02-05 23:13:00 +00002673 PCALL(PCALL_GENERATOR);
2674
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002675 /* Create a new generator that owns the ready to run frame
2676 * and return that as the value. */
Tim Peters5ca576e2001-06-18 22:08:13 +00002677 return gen_new(f);
2678 }
2679
2680 retval = eval_frame(f);
2681
2682 fail: /* Jump here from prelude on failure */
2683
Tim Petersb13680b2001-11-27 23:29:29 +00002684 /* decref'ing the frame can cause __del__ methods to get invoked,
2685 which can call back into Python. While we're done with the
2686 current Python frame (f), the associated C stack is still in use,
2687 so recursion_depth must be boosted for the duration.
2688 */
2689 assert(tstate != NULL);
2690 ++tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002691 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00002692 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002693 return retval;
2694}
2695
2696
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002697/* Implementation notes for set_exc_info() and reset_exc_info():
2698
2699- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2700 'exc_traceback'. These always travel together.
2701
2702- tstate->curexc_ZZZ is the "hot" exception that is set by
2703 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2704
2705- Once an exception is caught by an except clause, it is transferred
2706 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2707 can pick it up. This is the primary task of set_exc_info().
2708
2709- Now let me explain the complicated dance with frame->f_exc_ZZZ.
2710
2711 Long ago, when none of this existed, there were just a few globals:
2712 one set corresponding to the "hot" exception, and one set
2713 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2714 globals; they were simply stored as sys.exc_ZZZ. For backwards
2715 compatibility, they still are!) The problem was that in code like
2716 this:
2717
2718 try:
2719 "something that may fail"
2720 except "some exception":
2721 "do something else first"
2722 "print the exception from sys.exc_ZZZ."
2723
2724 if "do something else first" invoked something that raised and caught
2725 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2726 cause of subtle bugs. I fixed this by changing the semantics as
2727 follows:
2728
2729 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2730 *in that frame*.
2731
2732 - But initially, and as long as no exception is caught in a given
2733 frame, sys.exc_ZZZ will hold the last exception caught in the
2734 previous frame (or the frame before that, etc.).
2735
2736 The first bullet fixed the bug in the above example. The second
2737 bullet was for backwards compatibility: it was (and is) common to
2738 have a function that is called when an exception is caught, and to
2739 have that function access the caught exception via sys.exc_ZZZ.
2740 (Example: traceback.print_exc()).
2741
2742 At the same time I fixed the problem that sys.exc_ZZZ weren't
2743 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2744 but that's really a separate improvement.
2745
2746 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2747 variables to what they were before the current frame was called. The
2748 set_exc_info() function saves them on the frame so that
2749 reset_exc_info() can restore them. The invariant is that
2750 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2751 exception (where "catching" an exception applies only to successful
2752 except clauses); and if the current frame ever caught an exception,
2753 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2754 at the start of the current frame.
2755
2756*/
2757
Guido van Rossuma027efa1997-05-05 20:56:21 +00002758static void
Guido van Rossumac7be682001-01-17 15:42:30 +00002759set_exc_info(PyThreadState *tstate,
2760 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002761{
2762 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002763 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00002764
Guido van Rossuma027efa1997-05-05 20:56:21 +00002765 frame = tstate->frame;
2766 if (frame->f_exc_type == NULL) {
2767 /* This frame didn't catch an exception before */
2768 /* Save previous exception of this thread in this frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002769 if (tstate->exc_type == NULL) {
2770 Py_INCREF(Py_None);
2771 tstate->exc_type = Py_None;
2772 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002773 tmp_type = frame->f_exc_type;
2774 tmp_value = frame->f_exc_value;
2775 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002776 Py_XINCREF(tstate->exc_type);
2777 Py_XINCREF(tstate->exc_value);
2778 Py_XINCREF(tstate->exc_traceback);
2779 frame->f_exc_type = tstate->exc_type;
2780 frame->f_exc_value = tstate->exc_value;
2781 frame->f_exc_traceback = tstate->exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002782 Py_XDECREF(tmp_type);
2783 Py_XDECREF(tmp_value);
2784 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002785 }
2786 /* Set new exception for this thread */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002787 tmp_type = tstate->exc_type;
2788 tmp_value = tstate->exc_value;
2789 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002790 Py_XINCREF(type);
2791 Py_XINCREF(value);
2792 Py_XINCREF(tb);
2793 tstate->exc_type = type;
2794 tstate->exc_value = value;
2795 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002796 Py_XDECREF(tmp_type);
2797 Py_XDECREF(tmp_value);
2798 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002799 /* For b/w compatibility */
2800 PySys_SetObject("exc_type", type);
2801 PySys_SetObject("exc_value", value);
2802 PySys_SetObject("exc_traceback", tb);
2803}
2804
2805static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002806reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002807{
2808 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002809 PyObject *tmp_type, *tmp_value, *tmp_tb;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002810 frame = tstate->frame;
2811 if (frame->f_exc_type != NULL) {
2812 /* This frame caught an exception */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002813 tmp_type = tstate->exc_type;
2814 tmp_value = tstate->exc_value;
2815 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002816 Py_XINCREF(frame->f_exc_type);
2817 Py_XINCREF(frame->f_exc_value);
2818 Py_XINCREF(frame->f_exc_traceback);
2819 tstate->exc_type = frame->f_exc_type;
2820 tstate->exc_value = frame->f_exc_value;
2821 tstate->exc_traceback = frame->f_exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002822 Py_XDECREF(tmp_type);
2823 Py_XDECREF(tmp_value);
2824 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002825 /* For b/w compatibility */
2826 PySys_SetObject("exc_type", frame->f_exc_type);
2827 PySys_SetObject("exc_value", frame->f_exc_value);
2828 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
2829 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002830 tmp_type = frame->f_exc_type;
2831 tmp_value = frame->f_exc_value;
2832 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002833 frame->f_exc_type = NULL;
2834 frame->f_exc_value = NULL;
2835 frame->f_exc_traceback = NULL;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002836 Py_XDECREF(tmp_type);
2837 Py_XDECREF(tmp_value);
2838 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002839}
2840
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002841/* Logic for the raise statement (too complicated for inlining).
2842 This *consumes* a reference count to each of its arguments. */
Guido van Rossum1aa14831997-01-21 05:34:20 +00002843static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002844do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002845{
Guido van Rossumd295f121998-04-09 21:39:57 +00002846 if (type == NULL) {
2847 /* Reraise */
2848 PyThreadState *tstate = PyThreadState_Get();
2849 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
2850 value = tstate->exc_value;
2851 tb = tstate->exc_traceback;
2852 Py_XINCREF(type);
2853 Py_XINCREF(value);
2854 Py_XINCREF(tb);
2855 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002856
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002857 /* We support the following forms of raise:
2858 raise <class>, <classinstance>
2859 raise <class>, <argument tuple>
2860 raise <class>, None
2861 raise <class>, <argument>
2862 raise <classinstance>, None
2863 raise <string>, <object>
2864 raise <string>, None
2865
2866 An omitted second argument is the same as None.
2867
2868 In addition, raise <tuple>, <anything> is the same as
2869 raising the tuple's first item (and it better have one!);
2870 this rule is applied recursively.
2871
2872 Finally, an optional third argument can be supplied, which
2873 gives the traceback to be substituted (useful when
2874 re-raising an exception after examining it). */
2875
2876 /* First, check the traceback argument, replacing None with
2877 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002878 if (tb == Py_None) {
2879 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002880 tb = NULL;
2881 }
2882 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002883 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002884 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002885 goto raise_error;
2886 }
2887
2888 /* Next, replace a missing value with None */
2889 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002890 value = Py_None;
2891 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002892 }
2893
2894 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00002895 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
2896 PyObject *tmp = type;
2897 type = PyTuple_GET_ITEM(type, 0);
2898 Py_INCREF(type);
2899 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002900 }
2901
Tim Petersafb2c802002-04-18 18:06:20 +00002902 if (PyString_CheckExact(type))
2903 /* Raising builtin string is deprecated but still allowed --
2904 * do nothing. Raising an instance of a new-style str
2905 * subclass is right out. */
Neal Norwitz37aa0662003-01-10 15:31:15 +00002906 PyErr_Warn(PyExc_PendingDeprecationWarning,
2907 "raising a string exception is deprecated");
Barry Warsaw4249f541997-08-22 21:26:19 +00002908
2909 else if (PyClass_Check(type))
2910 PyErr_NormalizeException(&type, &value, &tb);
2911
Guido van Rossumb209a111997-04-29 18:18:01 +00002912 else if (PyInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002913 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002914 if (value != Py_None) {
2915 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002916 "instance exception may not have a separate value");
2917 goto raise_error;
2918 }
2919 else {
2920 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00002921 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002922 value = type;
Guido van Rossumb209a111997-04-29 18:18:01 +00002923 type = (PyObject*) ((PyInstanceObject*)type)->in_class;
2924 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002925 }
2926 }
2927 else {
2928 /* Not something you can raise. You get an exception
2929 anyway, just not what you specified :-) */
Jeremy Hylton960d9482001-04-27 02:25:33 +00002930 PyErr_Format(PyExc_TypeError,
Neal Norwitz37aa0662003-01-10 15:31:15 +00002931 "exceptions must be classes, instances, or "
2932 "strings (deprecated), not %s",
2933 type->ob_type->tp_name);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002934 goto raise_error;
2935 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002936 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002937 if (tb == NULL)
2938 return WHY_EXCEPTION;
2939 else
2940 return WHY_RERAISE;
2941 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00002942 Py_XDECREF(value);
2943 Py_XDECREF(type);
2944 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002945 return WHY_EXCEPTION;
2946}
2947
Tim Petersd6d010b2001-06-21 02:49:55 +00002948/* Iterate v argcnt times and store the results on the stack (via decreasing
2949 sp). Return 1 for success, 0 if error. */
2950
Barry Warsawe42b18f1997-08-25 22:13:04 +00002951static int
Tim Petersd6d010b2001-06-21 02:49:55 +00002952unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00002953{
Tim Petersd6d010b2001-06-21 02:49:55 +00002954 int i = 0;
2955 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00002956 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00002957
Tim Petersd6d010b2001-06-21 02:49:55 +00002958 assert(v != NULL);
2959
2960 it = PyObject_GetIter(v);
2961 if (it == NULL)
2962 goto Error;
2963
2964 for (; i < argcnt; i++) {
2965 w = PyIter_Next(it);
2966 if (w == NULL) {
2967 /* Iterator done, via error or exhaustion. */
2968 if (!PyErr_Occurred()) {
2969 PyErr_Format(PyExc_ValueError,
2970 "need more than %d value%s to unpack",
2971 i, i == 1 ? "" : "s");
2972 }
2973 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00002974 }
2975 *--sp = w;
2976 }
Tim Petersd6d010b2001-06-21 02:49:55 +00002977
2978 /* We better have exhausted the iterator now. */
2979 w = PyIter_Next(it);
2980 if (w == NULL) {
2981 if (PyErr_Occurred())
2982 goto Error;
2983 Py_DECREF(it);
2984 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00002985 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00002986 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00002987 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00002988 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00002989Error:
Barry Warsaw91010551997-08-25 22:30:51 +00002990 for (; i > 0; i--, sp++)
2991 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00002992 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00002993 return 0;
2994}
2995
2996
Guido van Rossum96a42c81992-01-12 02:29:51 +00002997#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00002998static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002999prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003000{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003001 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003002 if (PyObject_Print(v, stdout, 0) != 0)
3003 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003004 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003005 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003006}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003007#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003008
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003009static void
Fred Drake5755ce62001-06-27 19:19:46 +00003010call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003011{
Guido van Rossumb209a111997-04-29 18:18:01 +00003012 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003013 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003014 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003015 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003016 value = Py_None;
3017 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003018 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003019 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003020 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003021 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003022 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003023 }
Fred Drake5755ce62001-06-27 19:19:46 +00003024 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003025 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003026 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003027 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003028 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003029 Py_XDECREF(type);
3030 Py_XDECREF(value);
3031 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003032 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003033}
3034
Fred Drake4ec5d562001-10-04 19:26:43 +00003035static void
3036call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3037 int what)
3038{
3039 PyObject *type, *value, *traceback;
3040 int err;
3041 PyErr_Fetch(&type, &value, &traceback);
3042 err = call_trace(func, obj, frame, what, NULL);
3043 if (err == 0)
3044 PyErr_Restore(type, value, traceback);
3045 else {
3046 Py_XDECREF(type);
3047 Py_XDECREF(value);
3048 Py_XDECREF(traceback);
3049 }
3050}
3051
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003052static int
Fred Drake5755ce62001-06-27 19:19:46 +00003053call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3054 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003055{
Fred Drake5755ce62001-06-27 19:19:46 +00003056 register PyThreadState *tstate = frame->f_tstate;
3057 int result;
3058 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003059 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003060 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003061 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003062 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003063 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3064 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003065 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003066 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003067}
3068
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003069PyObject *
3070_PyEval_CallTracing(PyObject *func, PyObject *args)
3071{
3072 PyFrameObject *frame = PyEval_GetFrame();
3073 PyThreadState *tstate = frame->f_tstate;
3074 int save_tracing = tstate->tracing;
3075 int save_use_tracing = tstate->use_tracing;
3076 PyObject *result;
3077
3078 tstate->tracing = 0;
3079 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3080 || (tstate->c_profilefunc != NULL));
3081 result = PyObject_Call(func, args, NULL);
3082 tstate->tracing = save_tracing;
3083 tstate->use_tracing = save_use_tracing;
3084 return result;
3085}
3086
Michael W. Hudson006c7522002-11-08 13:08:46 +00003087static int
Michael W. Hudson019a78e2002-11-08 12:53:11 +00003088maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003089 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3090 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003091{
3092 /* The theory of SET_LINENO-less tracing.
3093
3094 In a nutshell, we use the co_lnotab field of the code object
3095 to tell when execution has moved onto a different line.
3096
3097 As mentioned above, the basic idea is so set things up so
3098 that
3099
3100 *instr_lb <= frame->f_lasti < *instr_ub
3101
3102 is true so long as execution does not change lines.
3103
3104 This is all fairly simple. Digging the information out of
3105 co_lnotab takes some work, but is conceptually clear.
3106
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003107 Somewhat harder to explain is why we don't *always* call the
3108 line trace function when the above test fails.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003109
3110 Consider this code:
3111
3112 1: def f(a):
3113 2: if a:
3114 3: print 1
3115 4: else:
3116 5: print 2
3117
3118 which compiles to this:
3119
3120 2 0 LOAD_FAST 0 (a)
3121 3 JUMP_IF_FALSE 9 (to 15)
3122 6 POP_TOP
3123
3124 3 7 LOAD_CONST 1 (1)
3125 10 PRINT_ITEM
3126 11 PRINT_NEWLINE
3127 12 JUMP_FORWARD 6 (to 21)
3128 >> 15 POP_TOP
3129
3130 5 16 LOAD_CONST 2 (2)
3131 19 PRINT_ITEM
3132 20 PRINT_NEWLINE
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003133 >> 21 LOAD_CONST 0 (None)
3134 24 RETURN_VALUE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003135
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003136 If 'a' is false, execution will jump to instruction at offset
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003137 15 and the co_lnotab will claim that execution has moved to
3138 line 3. This is at best misleading. In this case we could
3139 associate the POP_TOP with line 4, but that doesn't make
3140 sense in all cases (I think).
3141
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003142 What we do is only call the line trace function if the co_lnotab
3143 indicates we have jumped to the *start* of a line, i.e. if the
3144 current instruction offset matches the offset given for the
3145 start of a line by the co_lnotab.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003146
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003147 This also takes care of the situation where 'a' is true.
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003148 Execution will jump from instruction offset 12 to offset 21.
3149 Then the co_lnotab would imply that execution has moved to line
3150 5, which is again misleading.
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003151
3152 Why do we set f_lineno when tracing? Well, consider the code
3153 above when 'a' is true. If stepping through this with 'n' in
3154 pdb, you would stop at line 1 with a "call" type event, then
3155 line events on lines 2 and 3, then a "return" type event -- but
3156 you would be shown line 5 during this event. This is a change
3157 from the behaviour in 2.2 and before, and I've found it
3158 confusing in practice. By setting and using f_lineno when
3159 tracing, one can report a line number different from that
3160 suggested by f_lasti on this one occasion where it's desirable.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003161 */
3162
Michael W. Hudson006c7522002-11-08 13:08:46 +00003163 int result = 0;
3164
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003165 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003166 PyCodeObject* co = frame->f_code;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003167 int size, addr, line;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003168 unsigned char* p;
3169
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003170 size = PyString_GET_SIZE(co->co_lnotab) / 2;
3171 p = (unsigned char*)PyString_AS_STRING(co->co_lnotab);
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003172
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003173 addr = 0;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003174 line = co->co_firstlineno;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003175
3176 /* possible optimization: if f->f_lasti == instr_ub
3177 (likely to be a common case) then we already know
3178 instr_lb -- if we stored the matching value of p
3179 somwhere we could skip the first while loop. */
3180
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003181 /* see comments in compile.c for the description of
3182 co_lnotab. A point to remember: increments to p
3183 should come in pairs -- although we don't care about
3184 the line increments here, treating them as byte
3185 increments gets confusing, to say the least. */
3186
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003187 while (size > 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003188 if (addr + *p > frame->f_lasti)
3189 break;
3190 addr += *p++;
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003191 if (*p) *instr_lb = addr;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003192 line += *p++;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003193 --size;
3194 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003195
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003196 if (addr == frame->f_lasti) {
3197 frame->f_lineno = line;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003198 result = call_trace(func, obj, frame,
3199 PyTrace_LINE, Py_None);
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003200 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003201
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003202 if (size > 0) {
3203 while (--size >= 0) {
3204 addr += *p++;
3205 if (*p++)
3206 break;
3207 }
3208 *instr_ub = addr;
3209 }
3210 else {
3211 *instr_ub = INT_MAX;
3212 }
3213 }
Armin Rigobf57a142004-03-22 19:24:58 +00003214 else if (frame->f_lasti <= *instr_prev) {
3215 /* jumping back in the same line forces a trace event */
3216 result = call_trace(func, obj, frame,
3217 PyTrace_LINE, Py_None);
3218 }
3219 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003220 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003221}
3222
Fred Drake5755ce62001-06-27 19:19:46 +00003223void
3224PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003225{
Fred Drake5755ce62001-06-27 19:19:46 +00003226 PyThreadState *tstate = PyThreadState_Get();
3227 PyObject *temp = tstate->c_profileobj;
3228 Py_XINCREF(arg);
3229 tstate->c_profilefunc = NULL;
3230 tstate->c_profileobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003231 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003232 Py_XDECREF(temp);
3233 tstate->c_profilefunc = func;
3234 tstate->c_profileobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003235 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003236}
3237
3238void
3239PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3240{
3241 PyThreadState *tstate = PyThreadState_Get();
3242 PyObject *temp = tstate->c_traceobj;
3243 Py_XINCREF(arg);
3244 tstate->c_tracefunc = NULL;
3245 tstate->c_traceobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003246 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003247 Py_XDECREF(temp);
3248 tstate->c_tracefunc = func;
3249 tstate->c_traceobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003250 tstate->use_tracing = ((func != NULL)
3251 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003252}
3253
Guido van Rossumb209a111997-04-29 18:18:01 +00003254PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003255PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003256{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003257 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003258 if (current_frame == NULL)
Michael W. Hudson019a78e2002-11-08 12:53:11 +00003259 return PyThreadState_Get()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003260 else
3261 return current_frame->f_builtins;
3262}
3263
Guido van Rossumb209a111997-04-29 18:18:01 +00003264PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003265PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003266{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003267 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003268 if (current_frame == NULL)
3269 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003270 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003271 return current_frame->f_locals;
3272}
3273
Guido van Rossumb209a111997-04-29 18:18:01 +00003274PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003275PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003276{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003277 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003278 if (current_frame == NULL)
3279 return NULL;
3280 else
3281 return current_frame->f_globals;
3282}
3283
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003284PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003285PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003286{
Michael W. Hudson019a78e2002-11-08 12:53:11 +00003287 PyThreadState *tstate = PyThreadState_Get();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003288 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003289}
3290
Guido van Rossum6135a871995-01-09 17:53:26 +00003291int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003292PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003293{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003294 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003295 return current_frame == NULL ? 0 : current_frame->f_restricted;
3296}
3297
Guido van Rossumbe270261997-05-22 22:26:18 +00003298int
Tim Peters5ba58662001-07-16 02:29:45 +00003299PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003300{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003301 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003302 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003303
3304 if (current_frame != NULL) {
3305 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003306 const int compilerflags = codeflags & PyCF_MASK;
3307 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003308 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003309 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003310 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003311#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003312 if (codeflags & CO_GENERATOR_ALLOWED) {
3313 result = 1;
3314 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3315 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003316#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003317 }
3318 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003319}
3320
3321int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003322Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003323{
Guido van Rossumb209a111997-04-29 18:18:01 +00003324 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003325 if (f == NULL)
3326 return 0;
3327 if (!PyFile_SoftSpace(f, 0))
3328 return 0;
3329 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003330}
3331
Guido van Rossum3f5da241990-12-20 15:06:42 +00003332
Guido van Rossum681d79a1995-07-18 14:51:37 +00003333/* External interface to call any callable object.
3334 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003335
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003336#undef PyEval_CallObject
3337/* for backward compatibility: export this interface */
3338
Guido van Rossumb209a111997-04-29 18:18:01 +00003339PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003340PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003341{
Guido van Rossumb209a111997-04-29 18:18:01 +00003342 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003343}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003344#define PyEval_CallObject(func,arg) \
3345 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003346
Guido van Rossumb209a111997-04-29 18:18:01 +00003347PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003348PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003349{
Jeremy Hylton52820442001-01-03 23:52:36 +00003350 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003351
3352 if (arg == NULL)
Guido van Rossumb209a111997-04-29 18:18:01 +00003353 arg = PyTuple_New(0);
3354 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003355 PyErr_SetString(PyExc_TypeError,
3356 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003357 return NULL;
3358 }
3359 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003360 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003361
Guido van Rossumb209a111997-04-29 18:18:01 +00003362 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003363 PyErr_SetString(PyExc_TypeError,
3364 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003365 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003366 return NULL;
3367 }
3368
Tim Peters6d6c1a32001-08-02 04:15:00 +00003369 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003370 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003371 return result;
3372}
3373
Tim Peters6d6c1a32001-08-02 04:15:00 +00003374char *
3375PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003376{
3377 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003378 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003379 else if (PyFunction_Check(func))
3380 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3381 else if (PyCFunction_Check(func))
3382 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3383 else if (PyClass_Check(func))
3384 return PyString_AsString(((PyClassObject*)func)->cl_name);
3385 else if (PyInstance_Check(func)) {
3386 return PyString_AsString(
3387 ((PyInstanceObject*)func)->in_class->cl_name);
3388 } else {
3389 return func->ob_type->tp_name;
3390 }
3391}
3392
Tim Peters6d6c1a32001-08-02 04:15:00 +00003393char *
3394PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003395{
3396 if (PyMethod_Check(func))
3397 return "()";
3398 else if (PyFunction_Check(func))
3399 return "()";
3400 else if (PyCFunction_Check(func))
3401 return "()";
3402 else if (PyClass_Check(func))
3403 return " constructor";
3404 else if (PyInstance_Check(func)) {
3405 return " instance";
3406 } else {
3407 return " object";
3408 }
3409}
3410
Jeremy Hylton52820442001-01-03 23:52:36 +00003411#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
3412
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003413static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003414err_args(PyObject *func, int flags, int nargs)
3415{
3416 if (flags & METH_NOARGS)
3417 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003418 "%.200s() takes no arguments (%d given)",
Jeremy Hylton192690e2002-08-16 18:36:11 +00003419 ((PyCFunctionObject *)func)->m_ml->ml_name,
3420 nargs);
3421 else
3422 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003423 "%.200s() takes exactly one argument (%d given)",
Jeremy Hylton192690e2002-08-16 18:36:11 +00003424 ((PyCFunctionObject *)func)->m_ml->ml_name,
3425 nargs);
3426}
3427
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003428static PyObject *
3429call_function(PyObject ***pp_stack, int oparg)
3430{
3431 int na = oparg & 0xff;
3432 int nk = (oparg>>8) & 0xff;
3433 int n = na + 2 * nk;
3434 PyObject **pfunc = (*pp_stack) - n - 1;
3435 PyObject *func = *pfunc;
3436 PyObject *x, *w;
3437
Jeremy Hylton985eba52003-02-05 23:13:00 +00003438 /* Always dispatch PyCFunction first, because these are
3439 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003440 */
3441 if (PyCFunction_Check(func) && nk == 0) {
3442 int flags = PyCFunction_GET_FLAGS(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003443 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003444 if (flags & (METH_NOARGS | METH_O)) {
3445 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3446 PyObject *self = PyCFunction_GET_SELF(func);
3447 if (flags & METH_NOARGS && na == 0)
3448 x = (*meth)(self, NULL);
3449 else if (flags & METH_O && na == 1) {
3450 PyObject *arg = EXT_POP(*pp_stack);
3451 x = (*meth)(self, arg);
3452 Py_DECREF(arg);
3453 }
3454 else {
3455 err_args(func, flags, na);
3456 x = NULL;
3457 }
3458 }
3459 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003460 PyObject *callargs;
3461 callargs = load_args(pp_stack, na);
3462 x = PyCFunction_Call(func, callargs, NULL);
3463 Py_XDECREF(callargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003464 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003465 } else {
3466 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3467 /* optimize access to bound methods */
3468 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003469 PCALL(PCALL_METHOD);
3470 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003471 Py_INCREF(self);
3472 func = PyMethod_GET_FUNCTION(func);
3473 Py_INCREF(func);
3474 Py_DECREF(*pfunc);
3475 *pfunc = self;
3476 na++;
3477 n++;
3478 } else
3479 Py_INCREF(func);
3480 if (PyFunction_Check(func))
3481 x = fast_function(func, pp_stack, n, na, nk);
3482 else
3483 x = do_call(func, pp_stack, na, nk);
3484 Py_DECREF(func);
3485 }
3486
Jeremy Hylton985eba52003-02-05 23:13:00 +00003487 /* What does this do? */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003488 while ((*pp_stack) > pfunc) {
3489 w = EXT_POP(*pp_stack);
3490 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003491 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003492 }
3493 return x;
3494}
3495
Jeremy Hylton192690e2002-08-16 18:36:11 +00003496/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003497 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003498 For the simplest case -- a function that takes only positional
3499 arguments and is called with only positional arguments -- it
3500 inlines the most primitive frame setup code from
3501 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3502 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003503*/
3504
3505static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003506fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003507{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003508 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003509 PyObject *globals = PyFunction_GET_GLOBALS(func);
3510 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3511 PyObject **d = NULL;
3512 int nd = 0;
3513
Jeremy Hylton985eba52003-02-05 23:13:00 +00003514 PCALL(PCALL_FUNCTION);
3515 PCALL(PCALL_FAST_FUNCTION);
Raymond Hettinger40174c32003-05-31 07:04:16 +00003516 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003517 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3518 PyFrameObject *f;
3519 PyObject *retval = NULL;
3520 PyThreadState *tstate = PyThreadState_GET();
3521 PyObject **fastlocals, **stack;
3522 int i;
3523
3524 PCALL(PCALL_FASTER_FUNCTION);
3525 assert(globals != NULL);
3526 /* XXX Perhaps we should create a specialized
3527 PyFrame_New() that doesn't take locals, but does
3528 take builtins without sanity checking them.
3529 */
3530 f = PyFrame_New(tstate, co, globals, NULL);
3531 if (f == NULL)
3532 return NULL;
3533
3534 fastlocals = f->f_localsplus;
3535 stack = (*pp_stack) - n;
3536
3537 for (i = 0; i < n; i++) {
3538 Py_INCREF(*stack);
3539 fastlocals[i] = *stack++;
3540 }
3541 retval = eval_frame(f);
3542 assert(tstate != NULL);
3543 ++tstate->recursion_depth;
3544 Py_DECREF(f);
3545 --tstate->recursion_depth;
3546 return retval;
3547 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003548 if (argdefs != NULL) {
3549 d = &PyTuple_GET_ITEM(argdefs, 0);
3550 nd = ((PyTupleObject *)argdefs)->ob_size;
3551 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003552 return PyEval_EvalCodeEx(co, globals,
3553 (PyObject *)NULL, (*pp_stack)-n, na,
3554 (*pp_stack)-2*nk, nk, d, nd,
3555 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003556}
3557
3558static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003559update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3560 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003561{
3562 PyObject *kwdict = NULL;
3563 if (orig_kwdict == NULL)
3564 kwdict = PyDict_New();
3565 else {
3566 kwdict = PyDict_Copy(orig_kwdict);
3567 Py_DECREF(orig_kwdict);
3568 }
3569 if (kwdict == NULL)
3570 return NULL;
3571 while (--nk >= 0) {
3572 int err;
3573 PyObject *value = EXT_POP(*pp_stack);
3574 PyObject *key = EXT_POP(*pp_stack);
3575 if (PyDict_GetItem(kwdict, key) != NULL) {
Guido van Rossumac7be682001-01-17 15:42:30 +00003576 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +00003577 "%.200s%s got multiple values "
Jeremy Hylton512a2372001-04-11 13:52:29 +00003578 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003579 PyEval_GetFuncName(func),
3580 PyEval_GetFuncDesc(func),
Jeremy Hylton512a2372001-04-11 13:52:29 +00003581 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003582 Py_DECREF(key);
3583 Py_DECREF(value);
3584 Py_DECREF(kwdict);
3585 return NULL;
3586 }
3587 err = PyDict_SetItem(kwdict, key, value);
3588 Py_DECREF(key);
3589 Py_DECREF(value);
3590 if (err) {
3591 Py_DECREF(kwdict);
3592 return NULL;
3593 }
3594 }
3595 return kwdict;
3596}
3597
3598static PyObject *
3599update_star_args(int nstack, int nstar, PyObject *stararg,
3600 PyObject ***pp_stack)
3601{
3602 PyObject *callargs, *w;
3603
3604 callargs = PyTuple_New(nstack + nstar);
3605 if (callargs == NULL) {
3606 return NULL;
3607 }
3608 if (nstar) {
3609 int i;
3610 for (i = 0; i < nstar; i++) {
3611 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3612 Py_INCREF(a);
3613 PyTuple_SET_ITEM(callargs, nstack + i, a);
3614 }
3615 }
3616 while (--nstack >= 0) {
3617 w = EXT_POP(*pp_stack);
3618 PyTuple_SET_ITEM(callargs, nstack, w);
3619 }
3620 return callargs;
3621}
3622
3623static PyObject *
3624load_args(PyObject ***pp_stack, int na)
3625{
3626 PyObject *args = PyTuple_New(na);
3627 PyObject *w;
3628
3629 if (args == NULL)
3630 return NULL;
3631 while (--na >= 0) {
3632 w = EXT_POP(*pp_stack);
3633 PyTuple_SET_ITEM(args, na, w);
3634 }
3635 return args;
3636}
3637
3638static PyObject *
3639do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3640{
3641 PyObject *callargs = NULL;
3642 PyObject *kwdict = NULL;
3643 PyObject *result = NULL;
3644
3645 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003646 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003647 if (kwdict == NULL)
3648 goto call_fail;
3649 }
3650 callargs = load_args(pp_stack, na);
3651 if (callargs == NULL)
3652 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003653#ifdef CALL_PROFILE
3654 /* At this point, we have to look at the type of func to
3655 update the call stats properly. Do it here so as to avoid
3656 exposing the call stats machinery outside ceval.c
3657 */
3658 if (PyFunction_Check(func))
3659 PCALL(PCALL_FUNCTION);
3660 else if (PyMethod_Check(func))
3661 PCALL(PCALL_METHOD);
3662 else if (PyType_Check(func))
3663 PCALL(PCALL_TYPE);
3664 else
3665 PCALL(PCALL_OTHER);
3666#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003667 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003668 call_fail:
3669 Py_XDECREF(callargs);
3670 Py_XDECREF(kwdict);
3671 return result;
3672}
3673
3674static PyObject *
3675ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3676{
3677 int nstar = 0;
3678 PyObject *callargs = NULL;
3679 PyObject *stararg = NULL;
3680 PyObject *kwdict = NULL;
3681 PyObject *result = NULL;
3682
3683 if (flags & CALL_FLAG_KW) {
3684 kwdict = EXT_POP(*pp_stack);
3685 if (!(kwdict && PyDict_Check(kwdict))) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003686 PyErr_Format(PyExc_TypeError,
Jeremy Hylton512a2372001-04-11 13:52:29 +00003687 "%s%s argument after ** "
3688 "must be a dictionary",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003689 PyEval_GetFuncName(func),
3690 PyEval_GetFuncDesc(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003691 goto ext_call_fail;
3692 }
3693 }
3694 if (flags & CALL_FLAG_VAR) {
3695 stararg = EXT_POP(*pp_stack);
3696 if (!PyTuple_Check(stararg)) {
3697 PyObject *t = NULL;
3698 t = PySequence_Tuple(stararg);
3699 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003700 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3701 PyErr_Format(PyExc_TypeError,
3702 "%s%s argument after * "
3703 "must be a sequence",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003704 PyEval_GetFuncName(func),
3705 PyEval_GetFuncDesc(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003706 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003707 goto ext_call_fail;
3708 }
3709 Py_DECREF(stararg);
3710 stararg = t;
3711 }
3712 nstar = PyTuple_GET_SIZE(stararg);
3713 }
3714 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003715 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003716 if (kwdict == NULL)
3717 goto ext_call_fail;
3718 }
3719 callargs = update_star_args(na, nstar, stararg, pp_stack);
3720 if (callargs == NULL)
3721 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003722#ifdef CALL_PROFILE
3723 /* At this point, we have to look at the type of func to
3724 update the call stats properly. Do it here so as to avoid
3725 exposing the call stats machinery outside ceval.c
3726 */
3727 if (PyFunction_Check(func))
3728 PCALL(PCALL_FUNCTION);
3729 else if (PyMethod_Check(func))
3730 PCALL(PCALL_METHOD);
3731 else if (PyType_Check(func))
3732 PCALL(PCALL_TYPE);
3733 else
3734 PCALL(PCALL_OTHER);
3735#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003736 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003737 ext_call_fail:
3738 Py_XDECREF(callargs);
3739 Py_XDECREF(kwdict);
3740 Py_XDECREF(stararg);
3741 return result;
3742}
3743
Guido van Rossum3b9c6671996-07-30 18:40:29 +00003744#define SLICE_ERROR_MSG \
3745 "standard sequence type does not support step size other than one"
3746
Tim Peterscb479e72001-12-16 19:11:44 +00003747/* Extract a slice index from a PyInt or PyLong, and store in *pi.
3748 Silently reduce values larger than INT_MAX to INT_MAX, and silently
3749 boost values less than -INT_MAX to 0. Return 0 on error, 1 on success.
3750*/
Tim Petersb5196382001-12-16 19:44:20 +00003751/* Note: If v is NULL, return success without storing into *pi. This
3752 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3753 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00003754*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00003755int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003756_PyEval_SliceIndex(PyObject *v, int *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003757{
Tim Petersb5196382001-12-16 19:44:20 +00003758 if (v != NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003759 long x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003760 if (PyInt_Check(v)) {
3761 x = PyInt_AsLong(v);
3762 } else if (PyLong_Check(v)) {
3763 x = PyLong_AsLong(v);
3764 if (x==-1 && PyErr_Occurred()) {
3765 PyObject *long_zero;
Guido van Rossumac7be682001-01-17 15:42:30 +00003766 int cmp;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003767
Guido van Rossumac7be682001-01-17 15:42:30 +00003768 if (!PyErr_ExceptionMatches(
3769 PyExc_OverflowError)) {
3770 /* It's not an overflow error, so just
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003771 signal an error */
Guido van Rossum20c6add2000-05-08 14:06:50 +00003772 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003773 }
3774
Guido van Rossumac7be682001-01-17 15:42:30 +00003775 /* Clear the OverflowError */
3776 PyErr_Clear();
3777
3778 /* It's an overflow error, so we need to
3779 check the sign of the long integer,
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003780 set the value to INT_MAX or -INT_MAX,
3781 and clear the error. */
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003782
3783 /* Create a long integer with a value of 0 */
Guido van Rossumac7be682001-01-17 15:42:30 +00003784 long_zero = PyLong_FromLong(0L);
Tim Peterscb479e72001-12-16 19:11:44 +00003785 if (long_zero == NULL)
3786 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003787
3788 /* Check sign */
Guido van Rossumac7be682001-01-17 15:42:30 +00003789 cmp = PyObject_RichCompareBool(v, long_zero,
3790 Py_GT);
3791 Py_DECREF(long_zero);
3792 if (cmp < 0)
3793 return 0;
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003794 else if (cmp)
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003795 x = INT_MAX;
3796 else
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003797 x = -INT_MAX;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003798 }
3799 } else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003800 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003801 "slice indices must be integers");
Guido van Rossum20c6add2000-05-08 14:06:50 +00003802 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003803 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003804 /* Truncate -- very long indices are truncated anyway */
3805 if (x > INT_MAX)
3806 x = INT_MAX;
3807 else if (x < -INT_MAX)
Michael W. Hudsoncbd6fb92002-11-06 15:17:32 +00003808 x = -INT_MAX;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003809 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003810 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00003811 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003812}
3813
Guido van Rossum50d756e2001-08-18 17:43:36 +00003814#undef ISINT
3815#define ISINT(x) ((x) == NULL || PyInt_Check(x) || PyLong_Check(x))
3816
Guido van Rossumb209a111997-04-29 18:18:01 +00003817static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003818apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003819{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003820 PyTypeObject *tp = u->ob_type;
3821 PySequenceMethods *sq = tp->tp_as_sequence;
3822
3823 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3824 int ilow = 0, ihigh = INT_MAX;
3825 if (!_PyEval_SliceIndex(v, &ilow))
3826 return NULL;
3827 if (!_PyEval_SliceIndex(w, &ihigh))
3828 return NULL;
3829 return PySequence_GetSlice(u, ilow, ihigh);
3830 }
3831 else {
3832 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00003833 if (slice != NULL) {
3834 PyObject *res = PyObject_GetItem(u, slice);
3835 Py_DECREF(slice);
3836 return res;
3837 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00003838 else
3839 return NULL;
3840 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003841}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003842
3843static int
Guido van Rossumac7be682001-01-17 15:42:30 +00003844assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
3845 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003846{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003847 PyTypeObject *tp = u->ob_type;
3848 PySequenceMethods *sq = tp->tp_as_sequence;
3849
3850 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3851 int ilow = 0, ihigh = INT_MAX;
3852 if (!_PyEval_SliceIndex(v, &ilow))
3853 return -1;
3854 if (!_PyEval_SliceIndex(w, &ihigh))
3855 return -1;
3856 if (x == NULL)
3857 return PySequence_DelSlice(u, ilow, ihigh);
3858 else
3859 return PySequence_SetSlice(u, ilow, ihigh, x);
3860 }
3861 else {
3862 PyObject *slice = PySlice_New(v, w, NULL);
3863 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00003864 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003865 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00003866 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00003867 else
Guido van Rossum354797c2001-12-03 19:45:06 +00003868 res = PyObject_DelItem(u, slice);
3869 Py_DECREF(slice);
3870 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003871 }
3872 else
3873 return -1;
3874 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003875}
3876
Guido van Rossumb209a111997-04-29 18:18:01 +00003877static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003878cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003879{
Guido van Rossumac7be682001-01-17 15:42:30 +00003880 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003881 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00003882 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003883 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003884 break;
3885 case PyCmp_IS_NOT:
3886 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003887 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003888 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003889 res = PySequence_Contains(w, v);
3890 if (res < 0)
3891 return NULL;
3892 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003893 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00003894 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003895 if (res < 0)
3896 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003897 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003898 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003899 case PyCmp_EXC_MATCH:
Barry Warsaw4249f541997-08-22 21:26:19 +00003900 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003901 break;
3902 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00003903 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003904 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003905 v = res ? Py_True : Py_False;
3906 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003907 return v;
3908}
3909
Thomas Wouters52152252000-08-17 22:55:00 +00003910static PyObject *
3911import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003912{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003913 PyObject *x;
3914
3915 x = PyObject_GetAttr(v, name);
3916 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00003917 PyErr_Format(PyExc_ImportError,
3918 "cannot import name %.230s",
3919 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003920 }
Thomas Wouters52152252000-08-17 22:55:00 +00003921 return x;
3922}
Guido van Rossumac7be682001-01-17 15:42:30 +00003923
Thomas Wouters52152252000-08-17 22:55:00 +00003924static int
3925import_all_from(PyObject *locals, PyObject *v)
3926{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003927 PyObject *all = PyObject_GetAttrString(v, "__all__");
3928 PyObject *dict, *name, *value;
3929 int skip_leading_underscores = 0;
3930 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00003931
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003932 if (all == NULL) {
3933 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3934 return -1; /* Unexpected error */
3935 PyErr_Clear();
3936 dict = PyObject_GetAttrString(v, "__dict__");
3937 if (dict == NULL) {
3938 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3939 return -1;
3940 PyErr_SetString(PyExc_ImportError,
3941 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00003942 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003943 }
3944 all = PyMapping_Keys(dict);
3945 Py_DECREF(dict);
3946 if (all == NULL)
3947 return -1;
3948 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00003949 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003950
3951 for (pos = 0, err = 0; ; pos++) {
3952 name = PySequence_GetItem(all, pos);
3953 if (name == NULL) {
3954 if (!PyErr_ExceptionMatches(PyExc_IndexError))
3955 err = -1;
3956 else
3957 PyErr_Clear();
3958 break;
3959 }
3960 if (skip_leading_underscores &&
3961 PyString_Check(name) &&
3962 PyString_AS_STRING(name)[0] == '_')
3963 {
3964 Py_DECREF(name);
3965 continue;
3966 }
3967 value = PyObject_GetAttr(v, name);
3968 if (value == NULL)
3969 err = -1;
3970 else
3971 err = PyDict_SetItem(locals, name, value);
3972 Py_DECREF(name);
3973 Py_XDECREF(value);
3974 if (err != 0)
3975 break;
3976 }
3977 Py_DECREF(all);
3978 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00003979}
3980
Guido van Rossumb209a111997-04-29 18:18:01 +00003981static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003982build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003983{
Guido van Rossum7851eea2001-09-12 19:19:18 +00003984 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003985
3986 if (PyDict_Check(methods))
3987 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00003988 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00003989 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00003990 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
3991 base = PyTuple_GET_ITEM(bases, 0);
3992 metaclass = PyObject_GetAttrString(base, "__class__");
3993 if (metaclass == NULL) {
3994 PyErr_Clear();
3995 metaclass = (PyObject *)base->ob_type;
3996 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00003997 }
3998 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00003999 else {
4000 PyObject *g = PyEval_GetGlobals();
4001 if (g != NULL && PyDict_Check(g))
4002 metaclass = PyDict_GetItemString(g, "__metaclass__");
4003 if (metaclass == NULL)
4004 metaclass = (PyObject *) &PyClass_Type;
4005 Py_INCREF(metaclass);
4006 }
4007 result = PyObject_CallFunction(metaclass, "OOO", name, bases, methods);
4008 Py_DECREF(metaclass);
4009 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00004010}
4011
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004012static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004013exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
4014 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004015{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004016 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00004017 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004018 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004019
Guido van Rossumb209a111997-04-29 18:18:01 +00004020 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
4021 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004022 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00004023 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004024 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00004025 locals = PyTuple_GetItem(prog, 2);
4026 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004027 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004028 if (globals == Py_None) {
4029 globals = PyEval_GetGlobals();
4030 if (locals == Py_None) {
4031 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00004032 plain = 1;
4033 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004034 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004035 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004036 locals = globals;
Guido van Rossumb209a111997-04-29 18:18:01 +00004037 if (!PyString_Check(prog) &&
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004038 !PyUnicode_Check(prog) &&
Guido van Rossumb209a111997-04-29 18:18:01 +00004039 !PyCode_Check(prog) &&
4040 !PyFile_Check(prog)) {
4041 PyErr_SetString(PyExc_TypeError,
Guido van Rossumac7be682001-01-17 15:42:30 +00004042 "exec: arg 1 must be a string, file, or code object");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004043 return -1;
4044 }
Fred Drake661ea262000-10-24 19:57:45 +00004045 if (!PyDict_Check(globals)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004046 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00004047 "exec: arg 2 must be a dictionary or None");
4048 return -1;
4049 }
4050 if (!PyDict_Check(locals)) {
4051 PyErr_SetString(PyExc_TypeError,
4052 "exec: arg 3 must be a dictionary or None");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004053 return -1;
4054 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004055 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00004056 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00004057 if (PyCode_Check(prog)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +00004058 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4059 PyErr_SetString(PyExc_TypeError,
4060 "code object passed to exec may not contain free variables");
4061 return -1;
4062 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004063 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004064 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004065 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004066 FILE *fp = PyFile_AsFile(prog);
4067 char *name = PyString_AsString(PyFile_Name(prog));
Tim Peters5ba58662001-07-16 02:29:45 +00004068 PyCompilerFlags cf;
4069 cf.cf_flags = 0;
4070 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004071 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
4072 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004073 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004074 v = PyRun_File(fp, name, Py_file_input, globals,
4075 locals);
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004076 }
4077 else {
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004078 PyObject *tmp = NULL;
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004079 char *str;
Tim Peters5ba58662001-07-16 02:29:45 +00004080 PyCompilerFlags cf;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004081 cf.cf_flags = 0;
4082#ifdef Py_USING_UNICODE
4083 if (PyUnicode_Check(prog)) {
4084 tmp = PyUnicode_AsUTF8String(prog);
4085 if (tmp == NULL)
4086 return -1;
4087 prog = tmp;
4088 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4089 }
4090#endif
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004091 if (PyString_AsStringAndSize(prog, &str, NULL))
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004092 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004093 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004094 v = PyRun_StringFlags(str, Py_file_input, globals,
4095 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004096 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004097 v = PyRun_String(str, Py_file_input, globals, locals);
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004098 Py_XDECREF(tmp);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004099 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004100 if (plain)
4101 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004102 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004103 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00004104 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004105 return 0;
4106}
Guido van Rossum24c13741995-02-14 09:42:43 +00004107
Guido van Rossumac7be682001-01-17 15:42:30 +00004108static void
Paul Prescode68140d2000-08-30 20:25:01 +00004109format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4110{
4111 char *obj_str;
4112
4113 if (!obj)
4114 return;
4115
4116 obj_str = PyString_AsString(obj);
4117 if (!obj_str)
4118 return;
4119
4120 PyErr_Format(exc, format_str, obj_str);
4121}
Guido van Rossum950361c1997-01-24 13:49:28 +00004122
4123#ifdef DYNAMIC_EXECUTION_PROFILE
4124
Skip Montanarof118cb12001-10-15 20:51:38 +00004125static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004126getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004127{
4128 int i;
4129 PyObject *l = PyList_New(256);
4130 if (l == NULL) return NULL;
4131 for (i = 0; i < 256; i++) {
4132 PyObject *x = PyInt_FromLong(a[i]);
4133 if (x == NULL) {
4134 Py_DECREF(l);
4135 return NULL;
4136 }
4137 PyList_SetItem(l, i, x);
4138 }
4139 for (i = 0; i < 256; i++)
4140 a[i] = 0;
4141 return l;
4142}
4143
4144PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004145_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004146{
4147#ifndef DXPAIRS
4148 return getarray(dxp);
4149#else
4150 int i;
4151 PyObject *l = PyList_New(257);
4152 if (l == NULL) return NULL;
4153 for (i = 0; i < 257; i++) {
4154 PyObject *x = getarray(dxpairs[i]);
4155 if (x == NULL) {
4156 Py_DECREF(l);
4157 return NULL;
4158 }
4159 PyList_SetItem(l, i, x);
4160 }
4161 return l;
4162#endif
4163}
4164
4165#endif