This commit was manufactured by cvs2svn to create tag 'r234c1'.
[python/dscho.git] / Python / ceval.c
blob035520a593b5b14a6a9872ff2946cbb2a5276b60
2 /* Execute compiled code */
4 /* XXX TO DO:
5 XXX speed up searching for keywords by using a dictionary
6 XXX document it!
7 */
9 #include "Python.h"
11 #include "compile.h"
12 #include "frameobject.h"
13 #include "eval.h"
14 #include "opcode.h"
15 #include "structmember.h"
17 #ifdef macintosh
18 #include "macglue.h"
19 #endif
21 #include <ctype.h>
23 /* Turn this on if your compiler chokes on the big switch: */
24 /* #define CASE_TOO_BIG 1 */
26 #ifdef Py_DEBUG
27 /* For debugging the interpreter: */
28 #define LLTRACE 1 /* Low-level trace feature */
29 #define CHECKEXC 1 /* Double-check exception checking */
30 #endif
32 typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
34 /* Forward declarations */
35 static PyObject *eval_frame(PyFrameObject *);
36 static PyObject *call_function(PyObject ***, int);
37 static PyObject *fast_function(PyObject *, PyObject ***, int, int, int);
38 static PyObject *do_call(PyObject *, PyObject ***, int, int);
39 static PyObject *ext_do_call(PyObject *, PyObject ***, int, int, int);
40 static PyObject *update_keyword_args(PyObject *, int, PyObject ***,PyObject *);
41 static PyObject *update_star_args(int, int, PyObject *, PyObject ***);
42 static PyObject *load_args(PyObject ***, int);
43 #define CALL_FLAG_VAR 1
44 #define CALL_FLAG_KW 2
46 #ifdef LLTRACE
47 static int prtrace(PyObject *, char *);
48 #endif
49 static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
50 int, PyObject *);
51 static void call_trace_protected(Py_tracefunc, PyObject *,
52 PyFrameObject *, int);
53 static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
54 static int maybe_call_line_trace(Py_tracefunc, PyObject *,
55 PyFrameObject *, int *, int *);
57 static PyObject *apply_slice(PyObject *, PyObject *, PyObject *);
58 static int assign_slice(PyObject *, PyObject *,
59 PyObject *, PyObject *);
60 static PyObject *cmp_outcome(int, PyObject *, PyObject *);
61 static PyObject *import_from(PyObject *, PyObject *);
62 static int import_all_from(PyObject *, PyObject *);
63 static PyObject *build_class(PyObject *, PyObject *, PyObject *);
64 static int exec_statement(PyFrameObject *,
65 PyObject *, PyObject *, PyObject *);
66 static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *);
67 static void reset_exc_info(PyThreadState *);
68 static void format_exc_check_arg(PyObject *, char *, PyObject *);
70 #define NAME_ERROR_MSG \
71 "name '%.200s' is not defined"
72 #define GLOBAL_NAME_ERROR_MSG \
73 "global name '%.200s' is not defined"
74 #define UNBOUNDLOCAL_ERROR_MSG \
75 "local variable '%.200s' referenced before assignment"
76 #define UNBOUNDFREE_ERROR_MSG \
77 "free variable '%.200s' referenced before assignment" \
78 " in enclosing scope"
80 /* Dynamic execution profile */
81 #ifdef DYNAMIC_EXECUTION_PROFILE
82 #ifdef DXPAIRS
83 static long dxpairs[257][256];
84 #define dxp dxpairs[256]
85 #else
86 static long dxp[256];
87 #endif
88 #endif
90 /* Function call profile */
91 #ifdef CALL_PROFILE
92 #define PCALL_NUM 11
93 static int pcall[PCALL_NUM];
95 #define PCALL_ALL 0
96 #define PCALL_FUNCTION 1
97 #define PCALL_FAST_FUNCTION 2
98 #define PCALL_FASTER_FUNCTION 3
99 #define PCALL_METHOD 4
100 #define PCALL_BOUND_METHOD 5
101 #define PCALL_CFUNCTION 6
102 #define PCALL_TYPE 7
103 #define PCALL_GENERATOR 8
104 #define PCALL_OTHER 9
105 #define PCALL_POP 10
107 /* Notes about the statistics
109 PCALL_FAST stats
111 FAST_FUNCTION means no argument tuple needs to be created.
112 FASTER_FUNCTION means that the fast-path frame setup code is used.
114 If there is a method call where the call can be optimized by changing
115 the argument tuple and calling the function directly, it gets recorded
116 twice.
118 As a result, the relationship among the statistics appears to be
119 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
120 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
121 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
122 PCALL_METHOD > PCALL_BOUND_METHOD
125 #define PCALL(POS) pcall[POS]++
127 PyObject *
128 PyEval_GetCallStats(PyObject *self)
130 return Py_BuildValue("iiiiiiiiii",
131 pcall[0], pcall[1], pcall[2], pcall[3],
132 pcall[4], pcall[5], pcall[6], pcall[7],
133 pcall[8], pcall[9]);
135 #else
136 #define PCALL(O)
138 PyObject *
139 PyEval_GetCallStats(PyObject *self)
141 Py_INCREF(Py_None);
142 return Py_None;
144 #endif
146 static PyTypeObject gentype;
148 typedef struct {
149 PyObject_HEAD
150 /* The gi_ prefix is intended to remind of generator-iterator. */
152 PyFrameObject *gi_frame;
154 /* True if generator is being executed. */
155 int gi_running;
157 /* List of weak reference. */
158 PyObject *gi_weakreflist;
159 } genobject;
161 static PyObject *
162 gen_new(PyFrameObject *f)
164 genobject *gen = PyObject_GC_New(genobject, &gentype);
165 if (gen == NULL) {
166 Py_DECREF(f);
167 return NULL;
169 gen->gi_frame = f;
170 gen->gi_running = 0;
171 gen->gi_weakreflist = NULL;
172 _PyObject_GC_TRACK(gen);
173 return (PyObject *)gen;
176 static int
177 gen_traverse(genobject *gen, visitproc visit, void *arg)
179 return visit((PyObject *)gen->gi_frame, arg);
182 static void
183 gen_dealloc(genobject *gen)
185 _PyObject_GC_UNTRACK(gen);
186 if (gen->gi_weakreflist != NULL)
187 PyObject_ClearWeakRefs((PyObject *) gen);
188 Py_DECREF(gen->gi_frame);
189 PyObject_GC_Del(gen);
192 static PyObject *
193 gen_iternext(genobject *gen)
195 PyThreadState *tstate = PyThreadState_GET();
196 PyFrameObject *f = gen->gi_frame;
197 PyObject *result;
199 if (gen->gi_running) {
200 PyErr_SetString(PyExc_ValueError,
201 "generator already executing");
202 return NULL;
204 if (f->f_stacktop == NULL)
205 return NULL;
207 /* Generators always return to their most recent caller, not
208 * necessarily their creator. */
209 Py_XINCREF(tstate->frame);
210 assert(f->f_back == NULL);
211 f->f_back = tstate->frame;
213 gen->gi_running = 1;
214 result = eval_frame(f);
215 gen->gi_running = 0;
217 /* Don't keep the reference to f_back any longer than necessary. It
218 * may keep a chain of frames alive or it could create a reference
219 * cycle. */
220 Py_XDECREF(f->f_back);
221 f->f_back = NULL;
223 /* If the generator just returned (as opposed to yielding), signal
224 * that the generator is exhausted. */
225 if (result == Py_None && f->f_stacktop == NULL) {
226 Py_DECREF(result);
227 result = NULL;
230 return result;
233 static PyObject *
234 gen_getiter(PyObject *gen)
236 Py_INCREF(gen);
237 return gen;
240 static PyMemberDef gen_memberlist[] = {
241 {"gi_frame", T_OBJECT, offsetof(genobject, gi_frame), RO},
242 {"gi_running", T_INT, offsetof(genobject, gi_running), RO},
243 {NULL} /* Sentinel */
246 static PyTypeObject gentype = {
247 PyObject_HEAD_INIT(&PyType_Type)
248 0, /* ob_size */
249 "generator", /* tp_name */
250 sizeof(genobject), /* tp_basicsize */
251 0, /* tp_itemsize */
252 /* methods */
253 (destructor)gen_dealloc, /* tp_dealloc */
254 0, /* tp_print */
255 0, /* tp_getattr */
256 0, /* tp_setattr */
257 0, /* tp_compare */
258 0, /* tp_repr */
259 0, /* tp_as_number */
260 0, /* tp_as_sequence */
261 0, /* tp_as_mapping */
262 0, /* tp_hash */
263 0, /* tp_call */
264 0, /* tp_str */
265 PyObject_GenericGetAttr, /* tp_getattro */
266 0, /* tp_setattro */
267 0, /* tp_as_buffer */
268 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
269 0, /* tp_doc */
270 (traverseproc)gen_traverse, /* tp_traverse */
271 0, /* tp_clear */
272 0, /* tp_richcompare */
273 offsetof(genobject, gi_weakreflist), /* tp_weaklistoffset */
274 (getiterfunc)gen_getiter, /* tp_iter */
275 (iternextfunc)gen_iternext, /* tp_iternext */
276 0, /* tp_methods */
277 gen_memberlist, /* tp_members */
278 0, /* tp_getset */
279 0, /* tp_base */
280 0, /* tp_dict */
284 #ifdef WITH_THREAD
286 #ifndef DONT_HAVE_ERRNO_H
287 #include <errno.h>
288 #endif
289 #include "pythread.h"
291 extern int _PyThread_Started; /* Flag for Py_Exit */
293 static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
294 static long main_thread = 0;
296 void
297 PyEval_InitThreads(void)
299 if (interpreter_lock)
300 return;
301 _PyThread_Started = 1;
302 interpreter_lock = PyThread_allocate_lock();
303 PyThread_acquire_lock(interpreter_lock, 1);
304 main_thread = PyThread_get_thread_ident();
307 void
308 PyEval_AcquireLock(void)
310 PyThread_acquire_lock(interpreter_lock, 1);
313 void
314 PyEval_ReleaseLock(void)
316 PyThread_release_lock(interpreter_lock);
319 void
320 PyEval_AcquireThread(PyThreadState *tstate)
322 if (tstate == NULL)
323 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
324 /* Check someone has called PyEval_InitThreads() to create the lock */
325 assert(interpreter_lock);
326 PyThread_acquire_lock(interpreter_lock, 1);
327 if (PyThreadState_Swap(tstate) != NULL)
328 Py_FatalError(
329 "PyEval_AcquireThread: non-NULL old thread state");
332 void
333 PyEval_ReleaseThread(PyThreadState *tstate)
335 if (tstate == NULL)
336 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
337 if (PyThreadState_Swap(NULL) != tstate)
338 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
339 PyThread_release_lock(interpreter_lock);
342 /* This function is called from PyOS_AfterFork to ensure that newly
343 created child processes don't hold locks referring to threads which
344 are not running in the child process. (This could also be done using
345 pthread_atfork mechanism, at least for the pthreads implementation.) */
347 void
348 PyEval_ReInitThreads(void)
350 if (!interpreter_lock)
351 return;
352 /*XXX Can't use PyThread_free_lock here because it does too
353 much error-checking. Doing this cleanly would require
354 adding a new function to each thread_*.h. Instead, just
355 create a new lock and waste a little bit of memory */
356 interpreter_lock = PyThread_allocate_lock();
357 PyThread_acquire_lock(interpreter_lock, 1);
358 main_thread = PyThread_get_thread_ident();
360 #endif
362 /* Functions save_thread and restore_thread are always defined so
363 dynamically loaded modules needn't be compiled separately for use
364 with and without threads: */
366 PyThreadState *
367 PyEval_SaveThread(void)
369 PyThreadState *tstate = PyThreadState_Swap(NULL);
370 if (tstate == NULL)
371 Py_FatalError("PyEval_SaveThread: NULL tstate");
372 #ifdef WITH_THREAD
373 if (interpreter_lock)
374 PyThread_release_lock(interpreter_lock);
375 #endif
376 return tstate;
379 void
380 PyEval_RestoreThread(PyThreadState *tstate)
382 if (tstate == NULL)
383 Py_FatalError("PyEval_RestoreThread: NULL tstate");
384 #ifdef WITH_THREAD
385 if (interpreter_lock) {
386 int err = errno;
387 PyThread_acquire_lock(interpreter_lock, 1);
388 errno = err;
390 #endif
391 PyThreadState_Swap(tstate);
395 /* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
396 signal handlers or Mac I/O completion routines) can schedule calls
397 to a function to be called synchronously.
398 The synchronous function is called with one void* argument.
399 It should return 0 for success or -1 for failure -- failure should
400 be accompanied by an exception.
402 If registry succeeds, the registry function returns 0; if it fails
403 (e.g. due to too many pending calls) it returns -1 (without setting
404 an exception condition).
406 Note that because registry may occur from within signal handlers,
407 or other asynchronous events, calling malloc() is unsafe!
409 #ifdef WITH_THREAD
410 Any thread can schedule pending calls, but only the main thread
411 will execute them.
412 #endif
414 XXX WARNING! ASYNCHRONOUSLY EXECUTING CODE!
415 There are two possible race conditions:
416 (1) nested asynchronous registry calls;
417 (2) registry calls made while pending calls are being processed.
418 While (1) is very unlikely, (2) is a real possibility.
419 The current code is safe against (2), but not against (1).
420 The safety against (2) is derived from the fact that only one
421 thread (the main thread) ever takes things out of the queue.
423 XXX Darn! With the advent of thread state, we should have an array
424 of pending calls per thread in the thread state! Later...
427 #define NPENDINGCALLS 32
428 static struct {
429 int (*func)(void *);
430 void *arg;
431 } pendingcalls[NPENDINGCALLS];
432 static volatile int pendingfirst = 0;
433 static volatile int pendinglast = 0;
434 static volatile int things_to_do = 0;
437 Py_AddPendingCall(int (*func)(void *), void *arg)
439 static int busy = 0;
440 int i, j;
441 /* XXX Begin critical section */
442 /* XXX If you want this to be safe against nested
443 XXX asynchronous calls, you'll have to work harder! */
444 if (busy)
445 return -1;
446 busy = 1;
447 i = pendinglast;
448 j = (i + 1) % NPENDINGCALLS;
449 if (j == pendingfirst) {
450 busy = 0;
451 return -1; /* Queue full */
453 pendingcalls[i].func = func;
454 pendingcalls[i].arg = arg;
455 pendinglast = j;
457 _Py_Ticker = 0;
458 things_to_do = 1; /* Signal main loop */
459 busy = 0;
460 /* XXX End critical section */
461 return 0;
465 Py_MakePendingCalls(void)
467 static int busy = 0;
468 #ifdef WITH_THREAD
469 if (main_thread && PyThread_get_thread_ident() != main_thread)
470 return 0;
471 #endif
472 if (busy)
473 return 0;
474 busy = 1;
475 things_to_do = 0;
476 for (;;) {
477 int i;
478 int (*func)(void *);
479 void *arg;
480 i = pendingfirst;
481 if (i == pendinglast)
482 break; /* Queue empty */
483 func = pendingcalls[i].func;
484 arg = pendingcalls[i].arg;
485 pendingfirst = (i + 1) % NPENDINGCALLS;
486 if (func(arg) < 0) {
487 busy = 0;
488 things_to_do = 1; /* We're not done yet */
489 return -1;
492 busy = 0;
493 return 0;
497 /* The interpreter's recursion limit */
499 static int recursion_limit = 1000;
502 Py_GetRecursionLimit(void)
504 return recursion_limit;
507 void
508 Py_SetRecursionLimit(int new_limit)
510 recursion_limit = new_limit;
513 /* Status code for main loop (reason for stack unwind) */
515 enum why_code {
516 WHY_NOT, /* No error */
517 WHY_EXCEPTION, /* Exception occurred */
518 WHY_RERAISE, /* Exception re-raised by 'finally' */
519 WHY_RETURN, /* 'return' statement */
520 WHY_BREAK, /* 'break' statement */
521 WHY_CONTINUE, /* 'continue' statement */
522 WHY_YIELD /* 'yield' operator */
525 static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
526 static int unpack_iterable(PyObject *, int, PyObject **);
528 /* for manipulating the thread switch and periodic "stuff" - used to be
529 per thread, now just a pair o' globals */
530 int _Py_CheckInterval = 100;
531 volatile int _Py_Ticker = 100;
533 PyObject *
534 PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
536 /* XXX raise SystemError if globals is NULL */
537 return PyEval_EvalCodeEx(co,
538 globals, locals,
539 (PyObject **)NULL, 0,
540 (PyObject **)NULL, 0,
541 (PyObject **)NULL, 0,
542 NULL);
546 /* Interpreter main loop */
548 static PyObject *
549 eval_frame(PyFrameObject *f)
551 #ifdef DXPAIRS
552 int lastopcode = 0;
553 #endif
554 PyObject **stack_pointer; /* Next free slot in value stack */
555 register unsigned char *next_instr;
556 register int opcode=0; /* Current opcode */
557 register int oparg=0; /* Current opcode argument, if any */
558 register enum why_code why; /* Reason for block stack unwind */
559 register int err; /* Error status -- nonzero if error */
560 register PyObject *x; /* Result object -- NULL if error */
561 register PyObject *v; /* Temporary objects popped off stack */
562 register PyObject *w;
563 register PyObject *u;
564 register PyObject *t;
565 register PyObject *stream = NULL; /* for PRINT opcodes */
566 register PyObject **fastlocals, **freevars;
567 PyObject *retval = NULL; /* Return value */
568 PyThreadState *tstate = PyThreadState_GET();
569 PyCodeObject *co;
571 /* when tracing we set things up so that
573 not (instr_lb <= current_bytecode_offset < instr_ub)
575 is true when the line being executed has changed. The
576 initial values are such as to make this false the first
577 time it is tested. */
578 int instr_ub = -1, instr_lb = 0;
580 unsigned char *first_instr;
581 PyObject *names;
582 PyObject *consts;
583 #ifdef LLTRACE
584 int lltrace;
585 #endif
586 #if defined(Py_DEBUG) || defined(LLTRACE)
587 /* Make it easier to find out where we are with a debugger */
588 char *filename;
589 #endif
591 /* Tuple access macros */
593 #ifndef Py_DEBUG
594 #define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
595 #else
596 #define GETITEM(v, i) PyTuple_GetItem((v), (i))
597 #endif
599 /* Code access macros */
601 #define INSTR_OFFSET() (next_instr - first_instr)
602 #define NEXTOP() (*next_instr++)
603 #define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
604 #define JUMPTO(x) (next_instr = first_instr + (x))
605 #define JUMPBY(x) (next_instr += (x))
607 /* OpCode prediction macros
608 Some opcodes tend to come in pairs thus making it possible to predict
609 the second code when the first is run. For example, COMPARE_OP is often
610 followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And, those opcodes are often
611 followed by a POP_TOP.
613 Verifying the prediction costs a single high-speed test of register
614 variable against a constant. If the pairing was good, then the
615 processor has a high likelihood of making its own successful branch
616 prediction which results in a nearly zero overhead transition to the
617 next opcode.
619 A successful prediction saves a trip through the eval-loop including
620 its two unpredictable branches, the HASARG test and the switch-case.
623 #define PREDICT(op) if (*next_instr == op) goto PRED_##op
624 #define PREDICTED(op) PRED_##op: next_instr++
625 #define PREDICTED_WITH_ARG(op) PRED_##op: oparg = (next_instr[2]<<8) + \
626 next_instr[1]; next_instr += 3
628 /* Stack manipulation macros */
630 #define STACK_LEVEL() (stack_pointer - f->f_valuestack)
631 #define EMPTY() (STACK_LEVEL() == 0)
632 #define TOP() (stack_pointer[-1])
633 #define SECOND() (stack_pointer[-2])
634 #define THIRD() (stack_pointer[-3])
635 #define FOURTH() (stack_pointer[-4])
636 #define SET_TOP(v) (stack_pointer[-1] = (v))
637 #define SET_SECOND(v) (stack_pointer[-2] = (v))
638 #define SET_THIRD(v) (stack_pointer[-3] = (v))
639 #define SET_FOURTH(v) (stack_pointer[-4] = (v))
640 #define BASIC_STACKADJ(n) (stack_pointer += n)
641 #define BASIC_PUSH(v) (*stack_pointer++ = (v))
642 #define BASIC_POP() (*--stack_pointer)
644 #ifdef LLTRACE
645 #define PUSH(v) { (void)(BASIC_PUSH(v), \
646 lltrace && prtrace(TOP(), "push")); \
647 assert(STACK_LEVEL() <= f->f_stacksize); }
648 #define POP() ((void)(lltrace && prtrace(TOP(), "pop")), BASIC_POP())
649 #define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
650 lltrace && prtrace(TOP(), "stackadj")); \
651 assert(STACK_LEVEL() <= f->f_stacksize); }
652 #else
653 #define PUSH(v) BASIC_PUSH(v)
654 #define POP() BASIC_POP()
655 #define STACKADJ(n) BASIC_STACKADJ(n)
656 #endif
658 /* Local variable macros */
660 #define GETLOCAL(i) (fastlocals[i])
662 /* The SETLOCAL() macro must not DECREF the local variable in-place and
663 then store the new value; it must copy the old value to a temporary
664 value, then store the new value, and then DECREF the temporary value.
665 This is because it is possible that during the DECREF the frame is
666 accessed by other code (e.g. a __del__ method or gc.collect()) and the
667 variable would be pointing to already-freed memory. */
668 #define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
669 GETLOCAL(i) = value; \
670 Py_XDECREF(tmp); } while (0)
672 /* Start of code */
674 if (f == NULL)
675 return NULL;
677 #ifdef USE_STACKCHECK
678 if (tstate->recursion_depth%10 == 0 && PyOS_CheckStack()) {
679 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
680 return NULL;
682 #endif
684 /* push frame */
685 if (++tstate->recursion_depth > recursion_limit) {
686 --tstate->recursion_depth;
687 PyErr_SetString(PyExc_RuntimeError,
688 "maximum recursion depth exceeded");
689 tstate->frame = f->f_back;
690 return NULL;
693 tstate->frame = f;
695 if (tstate->use_tracing) {
696 if (tstate->c_tracefunc != NULL) {
697 /* tstate->c_tracefunc, if defined, is a
698 function that will be called on *every* entry
699 to a code block. Its return value, if not
700 None, is a function that will be called at
701 the start of each executed line of code.
702 (Actually, the function must return itself
703 in order to continue tracing.) The trace
704 functions are called with three arguments:
705 a pointer to the current frame, a string
706 indicating why the function is called, and
707 an argument which depends on the situation.
708 The global trace function is also called
709 whenever an exception is detected. */
710 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
711 f, PyTrace_CALL, Py_None)) {
712 /* Trace function raised an error */
713 --tstate->recursion_depth;
714 tstate->frame = f->f_back;
715 return NULL;
718 if (tstate->c_profilefunc != NULL) {
719 /* Similar for c_profilefunc, except it needn't
720 return itself and isn't called for "line" events */
721 if (call_trace(tstate->c_profilefunc,
722 tstate->c_profileobj,
723 f, PyTrace_CALL, Py_None)) {
724 /* Profile function raised an error */
725 --tstate->recursion_depth;
726 tstate->frame = f->f_back;
727 return NULL;
732 co = f->f_code;
733 names = co->co_names;
734 consts = co->co_consts;
735 fastlocals = f->f_localsplus;
736 freevars = f->f_localsplus + f->f_nlocals;
737 _PyCode_GETCODEPTR(co, &first_instr);
738 /* An explanation is in order for the next line.
740 f->f_lasti now refers to the index of the last instruction
741 executed. You might think this was obvious from the name, but
742 this wasn't always true before 2.3! PyFrame_New now sets
743 f->f_lasti to -1 (i.e. the index *before* the first instruction)
744 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
745 does work. Promise. */
746 next_instr = first_instr + f->f_lasti + 1;
747 stack_pointer = f->f_stacktop;
748 assert(stack_pointer != NULL);
749 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
751 #ifdef LLTRACE
752 lltrace = PyDict_GetItemString(f->f_globals,"__lltrace__") != NULL;
753 #endif
754 #if defined(Py_DEBUG) || defined(LLTRACE)
755 filename = PyString_AsString(co->co_filename);
756 #endif
758 why = WHY_NOT;
759 err = 0;
760 x = Py_None; /* Not a reference, just anything non-NULL */
761 w = NULL;
763 for (;;) {
764 assert(stack_pointer >= f->f_valuestack); /* else underflow */
765 assert(STACK_LEVEL() <= f->f_stacksize); /* else overflow */
767 /* Do periodic things. Doing this every time through
768 the loop would add too much overhead, so we do it
769 only every Nth instruction. We also do it if
770 ``things_to_do'' is set, i.e. when an asynchronous
771 event needs attention (e.g. a signal handler or
772 async I/O handler); see Py_AddPendingCall() and
773 Py_MakePendingCalls() above. */
775 if (--_Py_Ticker < 0) {
776 if (*next_instr == SETUP_FINALLY) {
777 /* Make the last opcode before
778 a try: finally: block uninterruptable. */
779 goto fast_next_opcode;
781 _Py_Ticker = _Py_CheckInterval;
782 tstate->tick_counter++;
783 if (things_to_do) {
784 if (Py_MakePendingCalls() < 0) {
785 why = WHY_EXCEPTION;
786 goto on_error;
789 #if !defined(HAVE_SIGNAL_H) || defined(macintosh)
790 /* If we have true signals, the signal handler
791 will call Py_AddPendingCall() so we don't
792 have to call PyErr_CheckSignals(). On the
793 Mac and DOS, alas, we have to call it. */
794 if (PyErr_CheckSignals()) {
795 why = WHY_EXCEPTION;
796 goto on_error;
798 #endif
800 #ifdef WITH_THREAD
801 if (interpreter_lock) {
802 /* Give another thread a chance */
804 if (PyThreadState_Swap(NULL) != tstate)
805 Py_FatalError("ceval: tstate mix-up");
806 PyThread_release_lock(interpreter_lock);
808 /* Other threads may run now */
810 PyThread_acquire_lock(interpreter_lock, 1);
811 if (PyThreadState_Swap(tstate) != NULL)
812 Py_FatalError("ceval: orphan tstate");
814 /* Check for thread interrupts */
816 if (tstate->async_exc != NULL) {
817 x = tstate->async_exc;
818 tstate->async_exc = NULL;
819 PyErr_SetNone(x);
820 Py_DECREF(x);
821 why = WHY_EXCEPTION;
822 goto on_error;
825 #endif
828 fast_next_opcode:
829 f->f_lasti = INSTR_OFFSET();
831 /* line-by-line tracing support */
833 if (tstate->c_tracefunc != NULL && !tstate->tracing) {
834 /* see maybe_call_line_trace
835 for expository comments */
836 f->f_stacktop = stack_pointer;
838 err = maybe_call_line_trace(tstate->c_tracefunc,
839 tstate->c_traceobj,
840 f, &instr_lb, &instr_ub);
841 /* Reload possibly changed frame fields */
842 JUMPTO(f->f_lasti);
843 if (f->f_stacktop != NULL) {
844 stack_pointer = f->f_stacktop;
845 f->f_stacktop = NULL;
847 if (err) {
848 /* trace function raised an exception */
849 goto on_error;
853 /* Extract opcode and argument */
855 opcode = NEXTOP();
856 if (HAS_ARG(opcode))
857 oparg = NEXTARG();
858 dispatch_opcode:
859 #ifdef DYNAMIC_EXECUTION_PROFILE
860 #ifdef DXPAIRS
861 dxpairs[lastopcode][opcode]++;
862 lastopcode = opcode;
863 #endif
864 dxp[opcode]++;
865 #endif
867 #ifdef LLTRACE
868 /* Instruction tracing */
870 if (lltrace) {
871 if (HAS_ARG(opcode)) {
872 printf("%d: %d, %d\n",
873 f->f_lasti, opcode, oparg);
875 else {
876 printf("%d: %d\n",
877 f->f_lasti, opcode);
880 #endif
882 /* Main switch on opcode */
884 switch (opcode) {
886 /* BEWARE!
887 It is essential that any operation that fails sets either
888 x to NULL, err to nonzero, or why to anything but WHY_NOT,
889 and that no operation that succeeds does this! */
891 /* case STOP_CODE: this is an error! */
893 case LOAD_FAST:
894 x = GETLOCAL(oparg);
895 if (x != NULL) {
896 Py_INCREF(x);
897 PUSH(x);
898 goto fast_next_opcode;
900 format_exc_check_arg(PyExc_UnboundLocalError,
901 UNBOUNDLOCAL_ERROR_MSG,
902 PyTuple_GetItem(co->co_varnames, oparg));
903 break;
905 case LOAD_CONST:
906 x = GETITEM(consts, oparg);
907 Py_INCREF(x);
908 PUSH(x);
909 goto fast_next_opcode;
911 PREDICTED_WITH_ARG(STORE_FAST);
912 case STORE_FAST:
913 v = POP();
914 SETLOCAL(oparg, v);
915 goto fast_next_opcode;
917 PREDICTED(POP_TOP);
918 case POP_TOP:
919 v = POP();
920 Py_DECREF(v);
921 goto fast_next_opcode;
923 case ROT_TWO:
924 v = TOP();
925 w = SECOND();
926 SET_TOP(w);
927 SET_SECOND(v);
928 goto fast_next_opcode;
930 case ROT_THREE:
931 v = TOP();
932 w = SECOND();
933 x = THIRD();
934 SET_TOP(w);
935 SET_SECOND(x);
936 SET_THIRD(v);
937 goto fast_next_opcode;
939 case ROT_FOUR:
940 u = TOP();
941 v = SECOND();
942 w = THIRD();
943 x = FOURTH();
944 SET_TOP(v);
945 SET_SECOND(w);
946 SET_THIRD(x);
947 SET_FOURTH(u);
948 goto fast_next_opcode;
950 case DUP_TOP:
951 v = TOP();
952 Py_INCREF(v);
953 PUSH(v);
954 goto fast_next_opcode;
956 case DUP_TOPX:
957 if (oparg == 2) {
958 x = TOP();
959 Py_INCREF(x);
960 w = SECOND();
961 Py_INCREF(w);
962 STACKADJ(2);
963 SET_TOP(x);
964 SET_SECOND(w);
965 goto fast_next_opcode;
966 } else if (oparg == 3) {
967 x = TOP();
968 Py_INCREF(x);
969 w = SECOND();
970 Py_INCREF(w);
971 v = THIRD();
972 Py_INCREF(v);
973 STACKADJ(3);
974 SET_TOP(x);
975 SET_SECOND(w);
976 SET_THIRD(v);
977 goto fast_next_opcode;
979 Py_FatalError("invalid argument to DUP_TOPX"
980 " (bytecode corruption?)");
981 break;
983 case UNARY_POSITIVE:
984 v = TOP();
985 x = PyNumber_Positive(v);
986 Py_DECREF(v);
987 SET_TOP(x);
988 if (x != NULL) continue;
989 break;
991 case UNARY_NEGATIVE:
992 v = TOP();
993 x = PyNumber_Negative(v);
994 Py_DECREF(v);
995 SET_TOP(x);
996 if (x != NULL) continue;
997 break;
999 case UNARY_NOT:
1000 v = TOP();
1001 err = PyObject_IsTrue(v);
1002 Py_DECREF(v);
1003 if (err == 0) {
1004 Py_INCREF(Py_True);
1005 SET_TOP(Py_True);
1006 continue;
1008 else if (err > 0) {
1009 Py_INCREF(Py_False);
1010 SET_TOP(Py_False);
1011 err = 0;
1012 continue;
1014 STACKADJ(-1);
1015 break;
1017 case UNARY_CONVERT:
1018 v = TOP();
1019 x = PyObject_Repr(v);
1020 Py_DECREF(v);
1021 SET_TOP(x);
1022 if (x != NULL) continue;
1023 break;
1025 case UNARY_INVERT:
1026 v = TOP();
1027 x = PyNumber_Invert(v);
1028 Py_DECREF(v);
1029 SET_TOP(x);
1030 if (x != NULL) continue;
1031 break;
1033 case BINARY_POWER:
1034 w = POP();
1035 v = TOP();
1036 x = PyNumber_Power(v, w, Py_None);
1037 Py_DECREF(v);
1038 Py_DECREF(w);
1039 SET_TOP(x);
1040 if (x != NULL) continue;
1041 break;
1043 case BINARY_MULTIPLY:
1044 w = POP();
1045 v = TOP();
1046 x = PyNumber_Multiply(v, w);
1047 Py_DECREF(v);
1048 Py_DECREF(w);
1049 SET_TOP(x);
1050 if (x != NULL) continue;
1051 break;
1053 case BINARY_DIVIDE:
1054 if (!_Py_QnewFlag) {
1055 w = POP();
1056 v = TOP();
1057 x = PyNumber_Divide(v, w);
1058 Py_DECREF(v);
1059 Py_DECREF(w);
1060 SET_TOP(x);
1061 if (x != NULL) continue;
1062 break;
1064 /* -Qnew is in effect: fall through to
1065 BINARY_TRUE_DIVIDE */
1066 case BINARY_TRUE_DIVIDE:
1067 w = POP();
1068 v = TOP();
1069 x = PyNumber_TrueDivide(v, w);
1070 Py_DECREF(v);
1071 Py_DECREF(w);
1072 SET_TOP(x);
1073 if (x != NULL) continue;
1074 break;
1076 case BINARY_FLOOR_DIVIDE:
1077 w = POP();
1078 v = TOP();
1079 x = PyNumber_FloorDivide(v, w);
1080 Py_DECREF(v);
1081 Py_DECREF(w);
1082 SET_TOP(x);
1083 if (x != NULL) continue;
1084 break;
1086 case BINARY_MODULO:
1087 w = POP();
1088 v = TOP();
1089 x = PyNumber_Remainder(v, w);
1090 Py_DECREF(v);
1091 Py_DECREF(w);
1092 SET_TOP(x);
1093 if (x != NULL) continue;
1094 break;
1096 case BINARY_ADD:
1097 w = POP();
1098 v = TOP();
1099 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1100 /* INLINE: int + int */
1101 register long a, b, i;
1102 a = PyInt_AS_LONG(v);
1103 b = PyInt_AS_LONG(w);
1104 i = a + b;
1105 if ((i^a) < 0 && (i^b) < 0)
1106 goto slow_add;
1107 x = PyInt_FromLong(i);
1109 else {
1110 slow_add:
1111 x = PyNumber_Add(v, w);
1113 Py_DECREF(v);
1114 Py_DECREF(w);
1115 SET_TOP(x);
1116 if (x != NULL) continue;
1117 break;
1119 case BINARY_SUBTRACT:
1120 w = POP();
1121 v = TOP();
1122 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1123 /* INLINE: int - int */
1124 register long a, b, i;
1125 a = PyInt_AS_LONG(v);
1126 b = PyInt_AS_LONG(w);
1127 i = a - b;
1128 if ((i^a) < 0 && (i^~b) < 0)
1129 goto slow_sub;
1130 x = PyInt_FromLong(i);
1132 else {
1133 slow_sub:
1134 x = PyNumber_Subtract(v, w);
1136 Py_DECREF(v);
1137 Py_DECREF(w);
1138 SET_TOP(x);
1139 if (x != NULL) continue;
1140 break;
1142 case BINARY_SUBSCR:
1143 w = POP();
1144 v = TOP();
1145 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
1146 /* INLINE: list[int] */
1147 long i = PyInt_AsLong(w);
1148 if (i < 0)
1149 i += PyList_GET_SIZE(v);
1150 if (i < 0 ||
1151 i >= PyList_GET_SIZE(v)) {
1152 PyErr_SetString(PyExc_IndexError,
1153 "list index out of range");
1154 x = NULL;
1156 else {
1157 x = PyList_GET_ITEM(v, i);
1158 Py_INCREF(x);
1161 else
1162 x = PyObject_GetItem(v, w);
1163 Py_DECREF(v);
1164 Py_DECREF(w);
1165 SET_TOP(x);
1166 if (x != NULL) continue;
1167 break;
1169 case BINARY_LSHIFT:
1170 w = POP();
1171 v = TOP();
1172 x = PyNumber_Lshift(v, w);
1173 Py_DECREF(v);
1174 Py_DECREF(w);
1175 SET_TOP(x);
1176 if (x != NULL) continue;
1177 break;
1179 case BINARY_RSHIFT:
1180 w = POP();
1181 v = TOP();
1182 x = PyNumber_Rshift(v, w);
1183 Py_DECREF(v);
1184 Py_DECREF(w);
1185 SET_TOP(x);
1186 if (x != NULL) continue;
1187 break;
1189 case BINARY_AND:
1190 w = POP();
1191 v = TOP();
1192 x = PyNumber_And(v, w);
1193 Py_DECREF(v);
1194 Py_DECREF(w);
1195 SET_TOP(x);
1196 if (x != NULL) continue;
1197 break;
1199 case BINARY_XOR:
1200 w = POP();
1201 v = TOP();
1202 x = PyNumber_Xor(v, w);
1203 Py_DECREF(v);
1204 Py_DECREF(w);
1205 SET_TOP(x);
1206 if (x != NULL) continue;
1207 break;
1209 case BINARY_OR:
1210 w = POP();
1211 v = TOP();
1212 x = PyNumber_Or(v, w);
1213 Py_DECREF(v);
1214 Py_DECREF(w);
1215 SET_TOP(x);
1216 if (x != NULL) continue;
1217 break;
1219 case INPLACE_POWER:
1220 w = POP();
1221 v = TOP();
1222 x = PyNumber_InPlacePower(v, w, Py_None);
1223 Py_DECREF(v);
1224 Py_DECREF(w);
1225 SET_TOP(x);
1226 if (x != NULL) continue;
1227 break;
1229 case INPLACE_MULTIPLY:
1230 w = POP();
1231 v = TOP();
1232 x = PyNumber_InPlaceMultiply(v, w);
1233 Py_DECREF(v);
1234 Py_DECREF(w);
1235 SET_TOP(x);
1236 if (x != NULL) continue;
1237 break;
1239 case INPLACE_DIVIDE:
1240 if (!_Py_QnewFlag) {
1241 w = POP();
1242 v = TOP();
1243 x = PyNumber_InPlaceDivide(v, w);
1244 Py_DECREF(v);
1245 Py_DECREF(w);
1246 SET_TOP(x);
1247 if (x != NULL) continue;
1248 break;
1250 /* -Qnew is in effect: fall through to
1251 INPLACE_TRUE_DIVIDE */
1252 case INPLACE_TRUE_DIVIDE:
1253 w = POP();
1254 v = TOP();
1255 x = PyNumber_InPlaceTrueDivide(v, w);
1256 Py_DECREF(v);
1257 Py_DECREF(w);
1258 SET_TOP(x);
1259 if (x != NULL) continue;
1260 break;
1262 case INPLACE_FLOOR_DIVIDE:
1263 w = POP();
1264 v = TOP();
1265 x = PyNumber_InPlaceFloorDivide(v, w);
1266 Py_DECREF(v);
1267 Py_DECREF(w);
1268 SET_TOP(x);
1269 if (x != NULL) continue;
1270 break;
1272 case INPLACE_MODULO:
1273 w = POP();
1274 v = TOP();
1275 x = PyNumber_InPlaceRemainder(v, w);
1276 Py_DECREF(v);
1277 Py_DECREF(w);
1278 SET_TOP(x);
1279 if (x != NULL) continue;
1280 break;
1282 case INPLACE_ADD:
1283 w = POP();
1284 v = TOP();
1285 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1286 /* INLINE: int + int */
1287 register long a, b, i;
1288 a = PyInt_AS_LONG(v);
1289 b = PyInt_AS_LONG(w);
1290 i = a + b;
1291 if ((i^a) < 0 && (i^b) < 0)
1292 goto slow_iadd;
1293 x = PyInt_FromLong(i);
1295 else {
1296 slow_iadd:
1297 x = PyNumber_InPlaceAdd(v, w);
1299 Py_DECREF(v);
1300 Py_DECREF(w);
1301 SET_TOP(x);
1302 if (x != NULL) continue;
1303 break;
1305 case INPLACE_SUBTRACT:
1306 w = POP();
1307 v = TOP();
1308 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1309 /* INLINE: int - int */
1310 register long a, b, i;
1311 a = PyInt_AS_LONG(v);
1312 b = PyInt_AS_LONG(w);
1313 i = a - b;
1314 if ((i^a) < 0 && (i^~b) < 0)
1315 goto slow_isub;
1316 x = PyInt_FromLong(i);
1318 else {
1319 slow_isub:
1320 x = PyNumber_InPlaceSubtract(v, w);
1322 Py_DECREF(v);
1323 Py_DECREF(w);
1324 SET_TOP(x);
1325 if (x != NULL) continue;
1326 break;
1328 case INPLACE_LSHIFT:
1329 w = POP();
1330 v = TOP();
1331 x = PyNumber_InPlaceLshift(v, w);
1332 Py_DECREF(v);
1333 Py_DECREF(w);
1334 SET_TOP(x);
1335 if (x != NULL) continue;
1336 break;
1338 case INPLACE_RSHIFT:
1339 w = POP();
1340 v = TOP();
1341 x = PyNumber_InPlaceRshift(v, w);
1342 Py_DECREF(v);
1343 Py_DECREF(w);
1344 SET_TOP(x);
1345 if (x != NULL) continue;
1346 break;
1348 case INPLACE_AND:
1349 w = POP();
1350 v = TOP();
1351 x = PyNumber_InPlaceAnd(v, w);
1352 Py_DECREF(v);
1353 Py_DECREF(w);
1354 SET_TOP(x);
1355 if (x != NULL) continue;
1356 break;
1358 case INPLACE_XOR:
1359 w = POP();
1360 v = TOP();
1361 x = PyNumber_InPlaceXor(v, w);
1362 Py_DECREF(v);
1363 Py_DECREF(w);
1364 SET_TOP(x);
1365 if (x != NULL) continue;
1366 break;
1368 case INPLACE_OR:
1369 w = POP();
1370 v = TOP();
1371 x = PyNumber_InPlaceOr(v, w);
1372 Py_DECREF(v);
1373 Py_DECREF(w);
1374 SET_TOP(x);
1375 if (x != NULL) continue;
1376 break;
1378 case SLICE+0:
1379 case SLICE+1:
1380 case SLICE+2:
1381 case SLICE+3:
1382 if ((opcode-SLICE) & 2)
1383 w = POP();
1384 else
1385 w = NULL;
1386 if ((opcode-SLICE) & 1)
1387 v = POP();
1388 else
1389 v = NULL;
1390 u = TOP();
1391 x = apply_slice(u, v, w);
1392 Py_DECREF(u);
1393 Py_XDECREF(v);
1394 Py_XDECREF(w);
1395 SET_TOP(x);
1396 if (x != NULL) continue;
1397 break;
1399 case STORE_SLICE+0:
1400 case STORE_SLICE+1:
1401 case STORE_SLICE+2:
1402 case STORE_SLICE+3:
1403 if ((opcode-STORE_SLICE) & 2)
1404 w = POP();
1405 else
1406 w = NULL;
1407 if ((opcode-STORE_SLICE) & 1)
1408 v = POP();
1409 else
1410 v = NULL;
1411 u = POP();
1412 t = POP();
1413 err = assign_slice(u, v, w, t); /* u[v:w] = t */
1414 Py_DECREF(t);
1415 Py_DECREF(u);
1416 Py_XDECREF(v);
1417 Py_XDECREF(w);
1418 if (err == 0) continue;
1419 break;
1421 case DELETE_SLICE+0:
1422 case DELETE_SLICE+1:
1423 case DELETE_SLICE+2:
1424 case DELETE_SLICE+3:
1425 if ((opcode-DELETE_SLICE) & 2)
1426 w = POP();
1427 else
1428 w = NULL;
1429 if ((opcode-DELETE_SLICE) & 1)
1430 v = POP();
1431 else
1432 v = NULL;
1433 u = POP();
1434 err = assign_slice(u, v, w, (PyObject *)NULL);
1435 /* del u[v:w] */
1436 Py_DECREF(u);
1437 Py_XDECREF(v);
1438 Py_XDECREF(w);
1439 if (err == 0) continue;
1440 break;
1442 case STORE_SUBSCR:
1443 w = TOP();
1444 v = SECOND();
1445 u = THIRD();
1446 STACKADJ(-3);
1447 /* v[w] = u */
1448 err = PyObject_SetItem(v, w, u);
1449 Py_DECREF(u);
1450 Py_DECREF(v);
1451 Py_DECREF(w);
1452 if (err == 0) continue;
1453 break;
1455 case DELETE_SUBSCR:
1456 w = TOP();
1457 v = SECOND();
1458 STACKADJ(-2);
1459 /* del v[w] */
1460 err = PyObject_DelItem(v, w);
1461 Py_DECREF(v);
1462 Py_DECREF(w);
1463 if (err == 0) continue;
1464 break;
1466 case PRINT_EXPR:
1467 v = POP();
1468 w = PySys_GetObject("displayhook");
1469 if (w == NULL) {
1470 PyErr_SetString(PyExc_RuntimeError,
1471 "lost sys.displayhook");
1472 err = -1;
1473 x = NULL;
1475 if (err == 0) {
1476 x = Py_BuildValue("(O)", v);
1477 if (x == NULL)
1478 err = -1;
1480 if (err == 0) {
1481 w = PyEval_CallObject(w, x);
1482 Py_XDECREF(w);
1483 if (w == NULL)
1484 err = -1;
1486 Py_DECREF(v);
1487 Py_XDECREF(x);
1488 break;
1490 case PRINT_ITEM_TO:
1491 w = stream = POP();
1492 /* fall through to PRINT_ITEM */
1494 case PRINT_ITEM:
1495 v = POP();
1496 if (stream == NULL || stream == Py_None) {
1497 w = PySys_GetObject("stdout");
1498 if (w == NULL) {
1499 PyErr_SetString(PyExc_RuntimeError,
1500 "lost sys.stdout");
1501 err = -1;
1504 /* PyFile_SoftSpace() can exececute arbitrary code
1505 if sys.stdout is an instance with a __getattr__.
1506 If __getattr__ raises an exception, w will
1507 be freed, so we need to prevent that temporarily. */
1508 Py_XINCREF(w);
1509 if (w != NULL && PyFile_SoftSpace(w, 0))
1510 err = PyFile_WriteString(" ", w);
1511 if (err == 0)
1512 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
1513 if (err == 0) {
1514 /* XXX move into writeobject() ? */
1515 if (PyString_Check(v)) {
1516 char *s = PyString_AS_STRING(v);
1517 int len = PyString_GET_SIZE(v);
1518 if (len == 0 ||
1519 !isspace(Py_CHARMASK(s[len-1])) ||
1520 s[len-1] == ' ')
1521 PyFile_SoftSpace(w, 1);
1523 #ifdef Py_USING_UNICODE
1524 else if (PyUnicode_Check(v)) {
1525 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
1526 int len = PyUnicode_GET_SIZE(v);
1527 if (len == 0 ||
1528 !Py_UNICODE_ISSPACE(s[len-1]) ||
1529 s[len-1] == ' ')
1530 PyFile_SoftSpace(w, 1);
1532 #endif
1533 else
1534 PyFile_SoftSpace(w, 1);
1536 Py_XDECREF(w);
1537 Py_DECREF(v);
1538 Py_XDECREF(stream);
1539 stream = NULL;
1540 if (err == 0)
1541 continue;
1542 break;
1544 case PRINT_NEWLINE_TO:
1545 w = stream = POP();
1546 /* fall through to PRINT_NEWLINE */
1548 case PRINT_NEWLINE:
1549 if (stream == NULL || stream == Py_None) {
1550 w = PySys_GetObject("stdout");
1551 if (w == NULL)
1552 PyErr_SetString(PyExc_RuntimeError,
1553 "lost sys.stdout");
1555 if (w != NULL) {
1556 err = PyFile_WriteString("\n", w);
1557 if (err == 0)
1558 PyFile_SoftSpace(w, 0);
1560 Py_XDECREF(stream);
1561 stream = NULL;
1562 break;
1565 #ifdef CASE_TOO_BIG
1566 default: switch (opcode) {
1567 #endif
1568 case BREAK_LOOP:
1569 why = WHY_BREAK;
1570 break;
1572 case CONTINUE_LOOP:
1573 retval = PyInt_FromLong(oparg);
1574 why = WHY_CONTINUE;
1575 break;
1577 case RAISE_VARARGS:
1578 u = v = w = NULL;
1579 switch (oparg) {
1580 case 3:
1581 u = POP(); /* traceback */
1582 /* Fallthrough */
1583 case 2:
1584 v = POP(); /* value */
1585 /* Fallthrough */
1586 case 1:
1587 w = POP(); /* exc */
1588 case 0: /* Fallthrough */
1589 why = do_raise(w, v, u);
1590 break;
1591 default:
1592 PyErr_SetString(PyExc_SystemError,
1593 "bad RAISE_VARARGS oparg");
1594 why = WHY_EXCEPTION;
1595 break;
1597 break;
1599 case LOAD_LOCALS:
1600 if ((x = f->f_locals) == NULL) {
1601 PyErr_SetString(PyExc_SystemError,
1602 "no locals");
1603 break;
1605 Py_INCREF(x);
1606 PUSH(x);
1607 break;
1609 case RETURN_VALUE:
1610 retval = POP();
1611 why = WHY_RETURN;
1612 break;
1614 case YIELD_VALUE:
1615 retval = POP();
1616 f->f_stacktop = stack_pointer;
1617 why = WHY_YIELD;
1618 break;
1621 case EXEC_STMT:
1622 w = TOP();
1623 v = SECOND();
1624 u = THIRD();
1625 STACKADJ(-3);
1626 err = exec_statement(f, u, v, w);
1627 Py_DECREF(u);
1628 Py_DECREF(v);
1629 Py_DECREF(w);
1630 break;
1632 case POP_BLOCK:
1634 PyTryBlock *b = PyFrame_BlockPop(f);
1635 while (STACK_LEVEL() > b->b_level) {
1636 v = POP();
1637 Py_DECREF(v);
1640 break;
1642 case END_FINALLY:
1643 v = POP();
1644 if (PyInt_Check(v)) {
1645 why = (enum why_code) PyInt_AS_LONG(v);
1646 if (why == WHY_RETURN ||
1647 why == WHY_YIELD ||
1648 why == WHY_CONTINUE)
1649 retval = POP();
1651 else if (PyString_Check(v) || PyClass_Check(v)) {
1652 w = POP();
1653 u = POP();
1654 PyErr_Restore(v, w, u);
1655 why = WHY_RERAISE;
1656 break;
1658 else if (v != Py_None) {
1659 PyErr_SetString(PyExc_SystemError,
1660 "'finally' pops bad exception");
1661 why = WHY_EXCEPTION;
1663 Py_DECREF(v);
1664 break;
1666 case BUILD_CLASS:
1667 u = TOP();
1668 v = SECOND();
1669 w = THIRD();
1670 STACKADJ(-2);
1671 x = build_class(u, v, w);
1672 SET_TOP(x);
1673 Py_DECREF(u);
1674 Py_DECREF(v);
1675 Py_DECREF(w);
1676 break;
1678 case STORE_NAME:
1679 w = GETITEM(names, oparg);
1680 v = POP();
1681 if ((x = f->f_locals) == NULL) {
1682 PyErr_Format(PyExc_SystemError,
1683 "no locals found when storing %s",
1684 PyObject_REPR(w));
1685 break;
1687 err = PyDict_SetItem(x, w, v);
1688 Py_DECREF(v);
1689 break;
1691 case DELETE_NAME:
1692 w = GETITEM(names, oparg);
1693 if ((x = f->f_locals) == NULL) {
1694 PyErr_Format(PyExc_SystemError,
1695 "no locals when deleting %s",
1696 PyObject_REPR(w));
1697 break;
1699 if ((err = PyDict_DelItem(x, w)) != 0)
1700 format_exc_check_arg(PyExc_NameError,
1701 NAME_ERROR_MSG ,w);
1702 break;
1704 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
1705 case UNPACK_SEQUENCE:
1706 v = POP();
1707 if (PyTuple_CheckExact(v)) {
1708 if (PyTuple_Size(v) != oparg) {
1709 PyErr_SetString(PyExc_ValueError,
1710 "unpack tuple of wrong size");
1711 why = WHY_EXCEPTION;
1713 else {
1714 for (; --oparg >= 0; ) {
1715 w = PyTuple_GET_ITEM(v, oparg);
1716 Py_INCREF(w);
1717 PUSH(w);
1721 else if (PyList_CheckExact(v)) {
1722 if (PyList_Size(v) != oparg) {
1723 PyErr_SetString(PyExc_ValueError,
1724 "unpack list of wrong size");
1725 why = WHY_EXCEPTION;
1727 else {
1728 for (; --oparg >= 0; ) {
1729 w = PyList_GET_ITEM(v, oparg);
1730 Py_INCREF(w);
1731 PUSH(w);
1735 else if (unpack_iterable(v, oparg,
1736 stack_pointer + oparg))
1737 stack_pointer += oparg;
1738 else {
1739 if (PyErr_ExceptionMatches(PyExc_TypeError))
1740 PyErr_SetString(PyExc_TypeError,
1741 "unpack non-sequence");
1742 why = WHY_EXCEPTION;
1744 Py_DECREF(v);
1745 break;
1747 case STORE_ATTR:
1748 w = GETITEM(names, oparg);
1749 v = TOP();
1750 u = SECOND();
1751 STACKADJ(-2);
1752 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1753 Py_DECREF(v);
1754 Py_DECREF(u);
1755 break;
1757 case DELETE_ATTR:
1758 w = GETITEM(names, oparg);
1759 v = POP();
1760 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1761 /* del v.w */
1762 Py_DECREF(v);
1763 break;
1765 case STORE_GLOBAL:
1766 w = GETITEM(names, oparg);
1767 v = POP();
1768 err = PyDict_SetItem(f->f_globals, w, v);
1769 Py_DECREF(v);
1770 break;
1772 case DELETE_GLOBAL:
1773 w = GETITEM(names, oparg);
1774 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
1775 format_exc_check_arg(
1776 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
1777 break;
1779 case LOAD_NAME:
1780 w = GETITEM(names, oparg);
1781 if ((x = f->f_locals) == NULL) {
1782 PyErr_Format(PyExc_SystemError,
1783 "no locals when loading %s",
1784 PyObject_REPR(w));
1785 break;
1787 x = PyDict_GetItem(x, w);
1788 if (x == NULL) {
1789 x = PyDict_GetItem(f->f_globals, w);
1790 if (x == NULL) {
1791 x = PyDict_GetItem(f->f_builtins, w);
1792 if (x == NULL) {
1793 format_exc_check_arg(
1794 PyExc_NameError,
1795 NAME_ERROR_MSG ,w);
1796 break;
1800 Py_INCREF(x);
1801 PUSH(x);
1802 break;
1804 case LOAD_GLOBAL:
1805 w = GETITEM(names, oparg);
1806 if (PyString_CheckExact(w)) {
1807 /* Inline the PyDict_GetItem() calls.
1808 WARNING: this is an extreme speed hack.
1809 Do not try this at home. */
1810 long hash = ((PyStringObject *)w)->ob_shash;
1811 if (hash != -1) {
1812 PyDictObject *d;
1813 d = (PyDictObject *)(f->f_globals);
1814 x = d->ma_lookup(d, w, hash)->me_value;
1815 if (x != NULL) {
1816 Py_INCREF(x);
1817 PUSH(x);
1818 continue;
1820 d = (PyDictObject *)(f->f_builtins);
1821 x = d->ma_lookup(d, w, hash)->me_value;
1822 if (x != NULL) {
1823 Py_INCREF(x);
1824 PUSH(x);
1825 continue;
1827 goto load_global_error;
1830 /* This is the un-inlined version of the code above */
1831 x = PyDict_GetItem(f->f_globals, w);
1832 if (x == NULL) {
1833 x = PyDict_GetItem(f->f_builtins, w);
1834 if (x == NULL) {
1835 load_global_error:
1836 format_exc_check_arg(
1837 PyExc_NameError,
1838 GLOBAL_NAME_ERROR_MSG, w);
1839 break;
1842 Py_INCREF(x);
1843 PUSH(x);
1844 break;
1846 case DELETE_FAST:
1847 x = GETLOCAL(oparg);
1848 if (x == NULL) {
1849 format_exc_check_arg(
1850 PyExc_UnboundLocalError,
1851 UNBOUNDLOCAL_ERROR_MSG,
1852 PyTuple_GetItem(co->co_varnames, oparg)
1854 break;
1856 SETLOCAL(oparg, NULL);
1857 continue;
1859 case LOAD_CLOSURE:
1860 x = freevars[oparg];
1861 Py_INCREF(x);
1862 PUSH(x);
1863 break;
1865 case LOAD_DEREF:
1866 x = freevars[oparg];
1867 w = PyCell_Get(x);
1868 if (w == NULL) {
1869 err = -1;
1870 /* Don't stomp existing exception */
1871 if (PyErr_Occurred())
1872 break;
1873 if (oparg < f->f_ncells) {
1874 v = PyTuple_GetItem(co->co_cellvars,
1875 oparg);
1876 format_exc_check_arg(
1877 PyExc_UnboundLocalError,
1878 UNBOUNDLOCAL_ERROR_MSG,
1880 } else {
1881 v = PyTuple_GetItem(
1882 co->co_freevars,
1883 oparg - f->f_ncells);
1884 format_exc_check_arg(
1885 PyExc_NameError,
1886 UNBOUNDFREE_ERROR_MSG,
1889 break;
1891 PUSH(w);
1892 break;
1894 case STORE_DEREF:
1895 w = POP();
1896 x = freevars[oparg];
1897 PyCell_Set(x, w);
1898 Py_DECREF(w);
1899 continue;
1901 case BUILD_TUPLE:
1902 x = PyTuple_New(oparg);
1903 if (x != NULL) {
1904 for (; --oparg >= 0;) {
1905 w = POP();
1906 PyTuple_SET_ITEM(x, oparg, w);
1908 PUSH(x);
1909 continue;
1911 break;
1913 case BUILD_LIST:
1914 x = PyList_New(oparg);
1915 if (x != NULL) {
1916 for (; --oparg >= 0;) {
1917 w = POP();
1918 PyList_SET_ITEM(x, oparg, w);
1920 PUSH(x);
1921 continue;
1923 break;
1925 case BUILD_MAP:
1926 x = PyDict_New();
1927 PUSH(x);
1928 if (x != NULL) continue;
1929 break;
1931 case LOAD_ATTR:
1932 w = GETITEM(names, oparg);
1933 v = TOP();
1934 x = PyObject_GetAttr(v, w);
1935 Py_DECREF(v);
1936 SET_TOP(x);
1937 if (x != NULL) continue;
1938 break;
1940 case COMPARE_OP:
1941 w = POP();
1942 v = TOP();
1943 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
1944 /* INLINE: cmp(int, int) */
1945 register long a, b;
1946 register int res;
1947 a = PyInt_AS_LONG(v);
1948 b = PyInt_AS_LONG(w);
1949 switch (oparg) {
1950 case PyCmp_LT: res = a < b; break;
1951 case PyCmp_LE: res = a <= b; break;
1952 case PyCmp_EQ: res = a == b; break;
1953 case PyCmp_NE: res = a != b; break;
1954 case PyCmp_GT: res = a > b; break;
1955 case PyCmp_GE: res = a >= b; break;
1956 case PyCmp_IS: res = v == w; break;
1957 case PyCmp_IS_NOT: res = v != w; break;
1958 default: goto slow_compare;
1960 x = res ? Py_True : Py_False;
1961 Py_INCREF(x);
1963 else {
1964 slow_compare:
1965 x = cmp_outcome(oparg, v, w);
1967 Py_DECREF(v);
1968 Py_DECREF(w);
1969 SET_TOP(x);
1970 if (x == NULL) break;
1971 PREDICT(JUMP_IF_FALSE);
1972 PREDICT(JUMP_IF_TRUE);
1973 continue;
1975 case IMPORT_NAME:
1976 w = GETITEM(names, oparg);
1977 x = PyDict_GetItemString(f->f_builtins, "__import__");
1978 if (x == NULL) {
1979 PyErr_SetString(PyExc_ImportError,
1980 "__import__ not found");
1981 break;
1983 u = TOP();
1984 w = Py_BuildValue("(OOOO)",
1986 f->f_globals,
1987 f->f_locals == NULL ?
1988 Py_None : f->f_locals,
1990 Py_DECREF(u);
1991 if (w == NULL) {
1992 u = POP();
1993 x = NULL;
1994 break;
1996 x = PyEval_CallObject(x, w);
1997 Py_DECREF(w);
1998 SET_TOP(x);
1999 if (x != NULL) continue;
2000 break;
2002 case IMPORT_STAR:
2003 v = POP();
2004 PyFrame_FastToLocals(f);
2005 if ((x = f->f_locals) == NULL) {
2006 PyErr_SetString(PyExc_SystemError,
2007 "no locals found during 'import *'");
2008 break;
2010 err = import_all_from(x, v);
2011 PyFrame_LocalsToFast(f, 0);
2012 Py_DECREF(v);
2013 if (err == 0) continue;
2014 break;
2016 case IMPORT_FROM:
2017 w = GETITEM(names, oparg);
2018 v = TOP();
2019 x = import_from(v, w);
2020 PUSH(x);
2021 if (x != NULL) continue;
2022 break;
2024 case JUMP_FORWARD:
2025 JUMPBY(oparg);
2026 goto fast_next_opcode;
2028 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
2029 case JUMP_IF_FALSE:
2030 w = TOP();
2031 if (w == Py_True) {
2032 PREDICT(POP_TOP);
2033 goto fast_next_opcode;
2035 if (w == Py_False) {
2036 JUMPBY(oparg);
2037 goto fast_next_opcode;
2039 err = PyObject_IsTrue(w);
2040 if (err > 0)
2041 err = 0;
2042 else if (err == 0)
2043 JUMPBY(oparg);
2044 else
2045 break;
2046 continue;
2048 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
2049 case JUMP_IF_TRUE:
2050 w = TOP();
2051 if (w == Py_False) {
2052 PREDICT(POP_TOP);
2053 goto fast_next_opcode;
2055 if (w == Py_True) {
2056 JUMPBY(oparg);
2057 goto fast_next_opcode;
2059 err = PyObject_IsTrue(w);
2060 if (err > 0) {
2061 err = 0;
2062 JUMPBY(oparg);
2064 else if (err == 0)
2066 else
2067 break;
2068 continue;
2070 case JUMP_ABSOLUTE:
2071 JUMPTO(oparg);
2072 continue;
2074 case GET_ITER:
2075 /* before: [obj]; after [getiter(obj)] */
2076 v = TOP();
2077 x = PyObject_GetIter(v);
2078 Py_DECREF(v);
2079 if (x != NULL) {
2080 SET_TOP(x);
2081 PREDICT(FOR_ITER);
2082 continue;
2084 STACKADJ(-1);
2085 break;
2087 PREDICTED_WITH_ARG(FOR_ITER);
2088 case FOR_ITER:
2089 /* before: [iter]; after: [iter, iter()] *or* [] */
2090 v = TOP();
2091 x = PyIter_Next(v);
2092 if (x != NULL) {
2093 PUSH(x);
2094 PREDICT(STORE_FAST);
2095 PREDICT(UNPACK_SEQUENCE);
2096 continue;
2098 if (!PyErr_Occurred()) {
2099 /* iterator ended normally */
2100 x = v = POP();
2101 Py_DECREF(v);
2102 JUMPBY(oparg);
2103 continue;
2105 break;
2107 case SETUP_LOOP:
2108 case SETUP_EXCEPT:
2109 case SETUP_FINALLY:
2110 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2111 STACK_LEVEL());
2112 continue;
2114 case CALL_FUNCTION:
2115 PCALL(PCALL_ALL);
2116 x = call_function(&stack_pointer, oparg);
2117 PUSH(x);
2118 if (x != NULL)
2119 continue;
2120 break;
2122 case CALL_FUNCTION_VAR:
2123 case CALL_FUNCTION_KW:
2124 case CALL_FUNCTION_VAR_KW:
2126 int na = oparg & 0xff;
2127 int nk = (oparg>>8) & 0xff;
2128 int flags = (opcode - CALL_FUNCTION) & 3;
2129 int n = na + 2 * nk;
2130 PyObject **pfunc, *func;
2131 PCALL(PCALL_ALL);
2132 if (flags & CALL_FLAG_VAR)
2133 n++;
2134 if (flags & CALL_FLAG_KW)
2135 n++;
2136 pfunc = stack_pointer - n - 1;
2137 func = *pfunc;
2139 if (PyMethod_Check(func)
2140 && PyMethod_GET_SELF(func) != NULL) {
2141 PyObject *self = PyMethod_GET_SELF(func);
2142 Py_INCREF(self);
2143 func = PyMethod_GET_FUNCTION(func);
2144 Py_INCREF(func);
2145 Py_DECREF(*pfunc);
2146 *pfunc = self;
2147 na++;
2148 n++;
2149 } else
2150 Py_INCREF(func);
2151 x = ext_do_call(func, &stack_pointer, flags, na, nk);
2152 Py_DECREF(func);
2154 while (stack_pointer > pfunc) {
2155 w = POP();
2156 Py_DECREF(w);
2158 PUSH(x);
2159 if (x != NULL)
2160 continue;
2161 break;
2164 case MAKE_FUNCTION:
2165 v = POP(); /* code object */
2166 x = PyFunction_New(v, f->f_globals);
2167 Py_DECREF(v);
2168 /* XXX Maybe this should be a separate opcode? */
2169 if (x != NULL && oparg > 0) {
2170 v = PyTuple_New(oparg);
2171 if (v == NULL) {
2172 Py_DECREF(x);
2173 x = NULL;
2174 break;
2176 while (--oparg >= 0) {
2177 w = POP();
2178 PyTuple_SET_ITEM(v, oparg, w);
2180 err = PyFunction_SetDefaults(x, v);
2181 Py_DECREF(v);
2183 PUSH(x);
2184 break;
2186 case MAKE_CLOSURE:
2188 int nfree;
2189 v = POP(); /* code object */
2190 x = PyFunction_New(v, f->f_globals);
2191 nfree = PyCode_GetNumFree((PyCodeObject *)v);
2192 Py_DECREF(v);
2193 /* XXX Maybe this should be a separate opcode? */
2194 if (x != NULL && nfree > 0) {
2195 v = PyTuple_New(nfree);
2196 if (v == NULL) {
2197 Py_DECREF(x);
2198 x = NULL;
2199 break;
2201 while (--nfree >= 0) {
2202 w = POP();
2203 PyTuple_SET_ITEM(v, nfree, w);
2205 err = PyFunction_SetClosure(x, v);
2206 Py_DECREF(v);
2208 if (x != NULL && oparg > 0) {
2209 v = PyTuple_New(oparg);
2210 if (v == NULL) {
2211 Py_DECREF(x);
2212 x = NULL;
2213 break;
2215 while (--oparg >= 0) {
2216 w = POP();
2217 PyTuple_SET_ITEM(v, oparg, w);
2219 err = PyFunction_SetDefaults(x, v);
2220 Py_DECREF(v);
2222 PUSH(x);
2223 break;
2226 case BUILD_SLICE:
2227 if (oparg == 3)
2228 w = POP();
2229 else
2230 w = NULL;
2231 v = POP();
2232 u = TOP();
2233 x = PySlice_New(u, v, w);
2234 Py_DECREF(u);
2235 Py_DECREF(v);
2236 Py_XDECREF(w);
2237 SET_TOP(x);
2238 if (x != NULL) continue;
2239 break;
2241 case EXTENDED_ARG:
2242 opcode = NEXTOP();
2243 oparg = oparg<<16 | NEXTARG();
2244 goto dispatch_opcode;
2246 default:
2247 fprintf(stderr,
2248 "XXX lineno: %d, opcode: %d\n",
2249 PyCode_Addr2Line(f->f_code, f->f_lasti),
2250 opcode);
2251 PyErr_SetString(PyExc_SystemError, "unknown opcode");
2252 why = WHY_EXCEPTION;
2253 break;
2255 #ifdef CASE_TOO_BIG
2257 #endif
2259 } /* switch */
2261 on_error:
2263 /* Quickly continue if no error occurred */
2265 if (why == WHY_NOT) {
2266 if (err == 0 && x != NULL) {
2267 #ifdef CHECKEXC
2268 /* This check is expensive! */
2269 if (PyErr_Occurred())
2270 fprintf(stderr,
2271 "XXX undetected error\n");
2272 else
2273 #endif
2274 continue; /* Normal, fast path */
2276 why = WHY_EXCEPTION;
2277 x = Py_None;
2278 err = 0;
2281 /* Double-check exception status */
2283 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
2284 if (!PyErr_Occurred()) {
2285 PyErr_SetString(PyExc_SystemError,
2286 "error return without exception set");
2287 why = WHY_EXCEPTION;
2290 #ifdef CHECKEXC
2291 else {
2292 /* This check is expensive! */
2293 if (PyErr_Occurred()) {
2294 fprintf(stderr,
2295 "XXX undetected error (why=%d)\n",
2296 why);
2297 why = WHY_EXCEPTION;
2300 #endif
2302 /* Log traceback info if this is a real exception */
2304 if (why == WHY_EXCEPTION) {
2305 PyTraceBack_Here(f);
2307 if (tstate->c_tracefunc != NULL)
2308 call_exc_trace(tstate->c_tracefunc,
2309 tstate->c_traceobj, f);
2312 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
2314 if (why == WHY_RERAISE)
2315 why = WHY_EXCEPTION;
2317 /* Unwind stacks if a (pseudo) exception occurred */
2319 while (why != WHY_NOT && why != WHY_YIELD && f->f_iblock > 0) {
2320 PyTryBlock *b = PyFrame_BlockPop(f);
2322 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2323 /* For a continue inside a try block,
2324 don't pop the block for the loop. */
2325 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2326 b->b_level);
2327 why = WHY_NOT;
2328 JUMPTO(PyInt_AS_LONG(retval));
2329 Py_DECREF(retval);
2330 break;
2333 while (STACK_LEVEL() > b->b_level) {
2334 v = POP();
2335 Py_XDECREF(v);
2337 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2338 why = WHY_NOT;
2339 JUMPTO(b->b_handler);
2340 break;
2342 if (b->b_type == SETUP_FINALLY ||
2343 (b->b_type == SETUP_EXCEPT &&
2344 why == WHY_EXCEPTION)) {
2345 if (why == WHY_EXCEPTION) {
2346 PyObject *exc, *val, *tb;
2347 PyErr_Fetch(&exc, &val, &tb);
2348 if (val == NULL) {
2349 val = Py_None;
2350 Py_INCREF(val);
2352 /* Make the raw exception data
2353 available to the handler,
2354 so a program can emulate the
2355 Python main loop. Don't do
2356 this for 'finally'. */
2357 if (b->b_type == SETUP_EXCEPT) {
2358 PyErr_NormalizeException(
2359 &exc, &val, &tb);
2360 set_exc_info(tstate,
2361 exc, val, tb);
2363 if (tb == NULL) {
2364 Py_INCREF(Py_None);
2365 PUSH(Py_None);
2366 } else
2367 PUSH(tb);
2368 PUSH(val);
2369 PUSH(exc);
2371 else {
2372 if (why == WHY_RETURN ||
2373 why == WHY_CONTINUE)
2374 PUSH(retval);
2375 v = PyInt_FromLong((long)why);
2376 PUSH(v);
2378 why = WHY_NOT;
2379 JUMPTO(b->b_handler);
2380 break;
2382 } /* unwind stack */
2384 /* End the loop if we still have an error (or return) */
2386 if (why != WHY_NOT)
2387 break;
2389 } /* main loop */
2391 if (why != WHY_YIELD) {
2392 /* Pop remaining stack entries -- but when yielding */
2393 while (!EMPTY()) {
2394 v = POP();
2395 Py_XDECREF(v);
2399 if (why != WHY_RETURN && why != WHY_YIELD)
2400 retval = NULL;
2402 if (tstate->use_tracing) {
2403 if (tstate->c_tracefunc
2404 && (why == WHY_RETURN || why == WHY_YIELD)) {
2405 if (call_trace(tstate->c_tracefunc,
2406 tstate->c_traceobj, f,
2407 PyTrace_RETURN, retval)) {
2408 Py_XDECREF(retval);
2409 retval = NULL;
2410 why = WHY_EXCEPTION;
2413 if (tstate->c_profilefunc) {
2414 if (why == WHY_EXCEPTION)
2415 call_trace_protected(tstate->c_profilefunc,
2416 tstate->c_profileobj, f,
2417 PyTrace_RETURN);
2418 else if (call_trace(tstate->c_profilefunc,
2419 tstate->c_profileobj, f,
2420 PyTrace_RETURN, retval)) {
2421 Py_XDECREF(retval);
2422 retval = NULL;
2423 why = WHY_EXCEPTION;
2428 reset_exc_info(tstate);
2430 /* pop frame */
2431 --tstate->recursion_depth;
2432 tstate->frame = f->f_back;
2434 return retval;
2437 PyObject *
2438 PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
2439 PyObject **args, int argcount, PyObject **kws, int kwcount,
2440 PyObject **defs, int defcount, PyObject *closure)
2442 register PyFrameObject *f;
2443 register PyObject *retval = NULL;
2444 register PyObject **fastlocals, **freevars;
2445 PyThreadState *tstate = PyThreadState_GET();
2446 PyObject *x, *u;
2448 if (globals == NULL) {
2449 PyErr_SetString(PyExc_SystemError,
2450 "PyEval_EvalCodeEx: NULL globals");
2451 return NULL;
2454 assert(globals != NULL);
2455 f = PyFrame_New(tstate, co, globals, locals);
2456 if (f == NULL)
2457 return NULL;
2459 fastlocals = f->f_localsplus;
2460 freevars = f->f_localsplus + f->f_nlocals;
2462 if (co->co_argcount > 0 ||
2463 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2464 int i;
2465 int n = argcount;
2466 PyObject *kwdict = NULL;
2467 if (co->co_flags & CO_VARKEYWORDS) {
2468 kwdict = PyDict_New();
2469 if (kwdict == NULL)
2470 goto fail;
2471 i = co->co_argcount;
2472 if (co->co_flags & CO_VARARGS)
2473 i++;
2474 SETLOCAL(i, kwdict);
2476 if (argcount > co->co_argcount) {
2477 if (!(co->co_flags & CO_VARARGS)) {
2478 PyErr_Format(PyExc_TypeError,
2479 "%.200s() takes %s %d "
2480 "%sargument%s (%d given)",
2481 PyString_AsString(co->co_name),
2482 defcount ? "at most" : "exactly",
2483 co->co_argcount,
2484 kwcount ? "non-keyword " : "",
2485 co->co_argcount == 1 ? "" : "s",
2486 argcount);
2487 goto fail;
2489 n = co->co_argcount;
2491 for (i = 0; i < n; i++) {
2492 x = args[i];
2493 Py_INCREF(x);
2494 SETLOCAL(i, x);
2496 if (co->co_flags & CO_VARARGS) {
2497 u = PyTuple_New(argcount - n);
2498 if (u == NULL)
2499 goto fail;
2500 SETLOCAL(co->co_argcount, u);
2501 for (i = n; i < argcount; i++) {
2502 x = args[i];
2503 Py_INCREF(x);
2504 PyTuple_SET_ITEM(u, i-n, x);
2507 for (i = 0; i < kwcount; i++) {
2508 PyObject *keyword = kws[2*i];
2509 PyObject *value = kws[2*i + 1];
2510 int j;
2511 if (keyword == NULL || !PyString_Check(keyword)) {
2512 PyErr_Format(PyExc_TypeError,
2513 "%.200s() keywords must be strings",
2514 PyString_AsString(co->co_name));
2515 goto fail;
2517 /* XXX slow -- speed up using dictionary? */
2518 for (j = 0; j < co->co_argcount; j++) {
2519 PyObject *nm = PyTuple_GET_ITEM(
2520 co->co_varnames, j);
2521 int cmp = PyObject_RichCompareBool(
2522 keyword, nm, Py_EQ);
2523 if (cmp > 0)
2524 break;
2525 else if (cmp < 0)
2526 goto fail;
2528 /* Check errors from Compare */
2529 if (PyErr_Occurred())
2530 goto fail;
2531 if (j >= co->co_argcount) {
2532 if (kwdict == NULL) {
2533 PyErr_Format(PyExc_TypeError,
2534 "%.200s() got an unexpected "
2535 "keyword argument '%.400s'",
2536 PyString_AsString(co->co_name),
2537 PyString_AsString(keyword));
2538 goto fail;
2540 PyDict_SetItem(kwdict, keyword, value);
2542 else {
2543 if (GETLOCAL(j) != NULL) {
2544 PyErr_Format(PyExc_TypeError,
2545 "%.200s() got multiple "
2546 "values for keyword "
2547 "argument '%.400s'",
2548 PyString_AsString(co->co_name),
2549 PyString_AsString(keyword));
2550 goto fail;
2552 Py_INCREF(value);
2553 SETLOCAL(j, value);
2556 if (argcount < co->co_argcount) {
2557 int m = co->co_argcount - defcount;
2558 for (i = argcount; i < m; i++) {
2559 if (GETLOCAL(i) == NULL) {
2560 PyErr_Format(PyExc_TypeError,
2561 "%.200s() takes %s %d "
2562 "%sargument%s (%d given)",
2563 PyString_AsString(co->co_name),
2564 ((co->co_flags & CO_VARARGS) ||
2565 defcount) ? "at least"
2566 : "exactly",
2567 m, kwcount ? "non-keyword " : "",
2568 m == 1 ? "" : "s", i);
2569 goto fail;
2572 if (n > m)
2573 i = n - m;
2574 else
2575 i = 0;
2576 for (; i < defcount; i++) {
2577 if (GETLOCAL(m+i) == NULL) {
2578 PyObject *def = defs[i];
2579 Py_INCREF(def);
2580 SETLOCAL(m+i, def);
2585 else {
2586 if (argcount > 0 || kwcount > 0) {
2587 PyErr_Format(PyExc_TypeError,
2588 "%.200s() takes no arguments (%d given)",
2589 PyString_AsString(co->co_name),
2590 argcount + kwcount);
2591 goto fail;
2594 /* Allocate and initialize storage for cell vars, and copy free
2595 vars into frame. This isn't too efficient right now. */
2596 if (f->f_ncells) {
2597 int i = 0, j = 0, nargs, found;
2598 char *cellname, *argname;
2599 PyObject *c;
2601 nargs = co->co_argcount;
2602 if (co->co_flags & CO_VARARGS)
2603 nargs++;
2604 if (co->co_flags & CO_VARKEYWORDS)
2605 nargs++;
2607 /* Check for cells that shadow args */
2608 for (i = 0; i < f->f_ncells && j < nargs; ++i) {
2609 cellname = PyString_AS_STRING(
2610 PyTuple_GET_ITEM(co->co_cellvars, i));
2611 found = 0;
2612 while (j < nargs) {
2613 argname = PyString_AS_STRING(
2614 PyTuple_GET_ITEM(co->co_varnames, j));
2615 if (strcmp(cellname, argname) == 0) {
2616 c = PyCell_New(GETLOCAL(j));
2617 if (c == NULL)
2618 goto fail;
2619 GETLOCAL(f->f_nlocals + i) = c;
2620 found = 1;
2621 break;
2623 j++;
2625 if (found == 0) {
2626 c = PyCell_New(NULL);
2627 if (c == NULL)
2628 goto fail;
2629 SETLOCAL(f->f_nlocals + i, c);
2632 /* Initialize any that are left */
2633 while (i < f->f_ncells) {
2634 c = PyCell_New(NULL);
2635 if (c == NULL)
2636 goto fail;
2637 SETLOCAL(f->f_nlocals + i, c);
2638 i++;
2641 if (f->f_nfreevars) {
2642 int i;
2643 for (i = 0; i < f->f_nfreevars; ++i) {
2644 PyObject *o = PyTuple_GET_ITEM(closure, i);
2645 Py_INCREF(o);
2646 freevars[f->f_ncells + i] = o;
2650 if (co->co_flags & CO_GENERATOR) {
2651 /* Don't need to keep the reference to f_back, it will be set
2652 * when the generator is resumed. */
2653 Py_XDECREF(f->f_back);
2654 f->f_back = NULL;
2656 PCALL(PCALL_GENERATOR);
2658 /* Create a new generator that owns the ready to run frame
2659 * and return that as the value. */
2660 return gen_new(f);
2663 retval = eval_frame(f);
2665 fail: /* Jump here from prelude on failure */
2667 /* decref'ing the frame can cause __del__ methods to get invoked,
2668 which can call back into Python. While we're done with the
2669 current Python frame (f), the associated C stack is still in use,
2670 so recursion_depth must be boosted for the duration.
2672 assert(tstate != NULL);
2673 ++tstate->recursion_depth;
2674 Py_DECREF(f);
2675 --tstate->recursion_depth;
2676 return retval;
2680 /* Implementation notes for set_exc_info() and reset_exc_info():
2682 - Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2683 'exc_traceback'. These always travel together.
2685 - tstate->curexc_ZZZ is the "hot" exception that is set by
2686 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2688 - Once an exception is caught by an except clause, it is transferred
2689 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2690 can pick it up. This is the primary task of set_exc_info().
2692 - Now let me explain the complicated dance with frame->f_exc_ZZZ.
2694 Long ago, when none of this existed, there were just a few globals:
2695 one set corresponding to the "hot" exception, and one set
2696 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2697 globals; they were simply stored as sys.exc_ZZZ. For backwards
2698 compatibility, they still are!) The problem was that in code like
2699 this:
2701 try:
2702 "something that may fail"
2703 except "some exception":
2704 "do something else first"
2705 "print the exception from sys.exc_ZZZ."
2707 if "do something else first" invoked something that raised and caught
2708 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2709 cause of subtle bugs. I fixed this by changing the semantics as
2710 follows:
2712 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2713 *in that frame*.
2715 - But initially, and as long as no exception is caught in a given
2716 frame, sys.exc_ZZZ will hold the last exception caught in the
2717 previous frame (or the frame before that, etc.).
2719 The first bullet fixed the bug in the above example. The second
2720 bullet was for backwards compatibility: it was (and is) common to
2721 have a function that is called when an exception is caught, and to
2722 have that function access the caught exception via sys.exc_ZZZ.
2723 (Example: traceback.print_exc()).
2725 At the same time I fixed the problem that sys.exc_ZZZ weren't
2726 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2727 but that's really a separate improvement.
2729 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2730 variables to what they were before the current frame was called. The
2731 set_exc_info() function saves them on the frame so that
2732 reset_exc_info() can restore them. The invariant is that
2733 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2734 exception (where "catching" an exception applies only to successful
2735 except clauses); and if the current frame ever caught an exception,
2736 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2737 at the start of the current frame.
2741 static void
2742 set_exc_info(PyThreadState *tstate,
2743 PyObject *type, PyObject *value, PyObject *tb)
2745 PyFrameObject *frame;
2746 PyObject *tmp_type, *tmp_value, *tmp_tb;
2748 frame = tstate->frame;
2749 if (frame->f_exc_type == NULL) {
2750 /* This frame didn't catch an exception before */
2751 /* Save previous exception of this thread in this frame */
2752 if (tstate->exc_type == NULL) {
2753 Py_INCREF(Py_None);
2754 tstate->exc_type = Py_None;
2756 tmp_type = frame->f_exc_type;
2757 tmp_value = frame->f_exc_value;
2758 tmp_tb = frame->f_exc_traceback;
2759 Py_XINCREF(tstate->exc_type);
2760 Py_XINCREF(tstate->exc_value);
2761 Py_XINCREF(tstate->exc_traceback);
2762 frame->f_exc_type = tstate->exc_type;
2763 frame->f_exc_value = tstate->exc_value;
2764 frame->f_exc_traceback = tstate->exc_traceback;
2765 Py_XDECREF(tmp_type);
2766 Py_XDECREF(tmp_value);
2767 Py_XDECREF(tmp_tb);
2769 /* Set new exception for this thread */
2770 tmp_type = tstate->exc_type;
2771 tmp_value = tstate->exc_value;
2772 tmp_tb = tstate->exc_traceback;
2773 Py_XINCREF(type);
2774 Py_XINCREF(value);
2775 Py_XINCREF(tb);
2776 tstate->exc_type = type;
2777 tstate->exc_value = value;
2778 tstate->exc_traceback = tb;
2779 Py_XDECREF(tmp_type);
2780 Py_XDECREF(tmp_value);
2781 Py_XDECREF(tmp_tb);
2782 /* For b/w compatibility */
2783 PySys_SetObject("exc_type", type);
2784 PySys_SetObject("exc_value", value);
2785 PySys_SetObject("exc_traceback", tb);
2788 static void
2789 reset_exc_info(PyThreadState *tstate)
2791 PyFrameObject *frame;
2792 PyObject *tmp_type, *tmp_value, *tmp_tb;
2793 frame = tstate->frame;
2794 if (frame->f_exc_type != NULL) {
2795 /* This frame caught an exception */
2796 tmp_type = tstate->exc_type;
2797 tmp_value = tstate->exc_value;
2798 tmp_tb = tstate->exc_traceback;
2799 Py_XINCREF(frame->f_exc_type);
2800 Py_XINCREF(frame->f_exc_value);
2801 Py_XINCREF(frame->f_exc_traceback);
2802 tstate->exc_type = frame->f_exc_type;
2803 tstate->exc_value = frame->f_exc_value;
2804 tstate->exc_traceback = frame->f_exc_traceback;
2805 Py_XDECREF(tmp_type);
2806 Py_XDECREF(tmp_value);
2807 Py_XDECREF(tmp_tb);
2808 /* For b/w compatibility */
2809 PySys_SetObject("exc_type", frame->f_exc_type);
2810 PySys_SetObject("exc_value", frame->f_exc_value);
2811 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
2813 tmp_type = frame->f_exc_type;
2814 tmp_value = frame->f_exc_value;
2815 tmp_tb = frame->f_exc_traceback;
2816 frame->f_exc_type = NULL;
2817 frame->f_exc_value = NULL;
2818 frame->f_exc_traceback = NULL;
2819 Py_XDECREF(tmp_type);
2820 Py_XDECREF(tmp_value);
2821 Py_XDECREF(tmp_tb);
2824 /* Logic for the raise statement (too complicated for inlining).
2825 This *consumes* a reference count to each of its arguments. */
2826 static enum why_code
2827 do_raise(PyObject *type, PyObject *value, PyObject *tb)
2829 if (type == NULL) {
2830 /* Reraise */
2831 PyThreadState *tstate = PyThreadState_Get();
2832 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
2833 value = tstate->exc_value;
2834 tb = tstate->exc_traceback;
2835 Py_XINCREF(type);
2836 Py_XINCREF(value);
2837 Py_XINCREF(tb);
2840 /* We support the following forms of raise:
2841 raise <class>, <classinstance>
2842 raise <class>, <argument tuple>
2843 raise <class>, None
2844 raise <class>, <argument>
2845 raise <classinstance>, None
2846 raise <string>, <object>
2847 raise <string>, None
2849 An omitted second argument is the same as None.
2851 In addition, raise <tuple>, <anything> is the same as
2852 raising the tuple's first item (and it better have one!);
2853 this rule is applied recursively.
2855 Finally, an optional third argument can be supplied, which
2856 gives the traceback to be substituted (useful when
2857 re-raising an exception after examining it). */
2859 /* First, check the traceback argument, replacing None with
2860 NULL. */
2861 if (tb == Py_None) {
2862 Py_DECREF(tb);
2863 tb = NULL;
2865 else if (tb != NULL && !PyTraceBack_Check(tb)) {
2866 PyErr_SetString(PyExc_TypeError,
2867 "raise: arg 3 must be a traceback or None");
2868 goto raise_error;
2871 /* Next, replace a missing value with None */
2872 if (value == NULL) {
2873 value = Py_None;
2874 Py_INCREF(value);
2877 /* Next, repeatedly, replace a tuple exception with its first item */
2878 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
2879 PyObject *tmp = type;
2880 type = PyTuple_GET_ITEM(type, 0);
2881 Py_INCREF(type);
2882 Py_DECREF(tmp);
2885 if (PyString_CheckExact(type))
2886 /* Raising builtin string is deprecated but still allowed --
2887 * do nothing. Raising an instance of a new-style str
2888 * subclass is right out. */
2889 PyErr_Warn(PyExc_PendingDeprecationWarning,
2890 "raising a string exception is deprecated");
2892 else if (PyClass_Check(type))
2893 PyErr_NormalizeException(&type, &value, &tb);
2895 else if (PyInstance_Check(type)) {
2896 /* Raising an instance. The value should be a dummy. */
2897 if (value != Py_None) {
2898 PyErr_SetString(PyExc_TypeError,
2899 "instance exception may not have a separate value");
2900 goto raise_error;
2902 else {
2903 /* Normalize to raise <class>, <instance> */
2904 Py_DECREF(value);
2905 value = type;
2906 type = (PyObject*) ((PyInstanceObject*)type)->in_class;
2907 Py_INCREF(type);
2910 else {
2911 /* Not something you can raise. You get an exception
2912 anyway, just not what you specified :-) */
2913 PyErr_Format(PyExc_TypeError,
2914 "exceptions must be classes, instances, or "
2915 "strings (deprecated), not %s",
2916 type->ob_type->tp_name);
2917 goto raise_error;
2919 PyErr_Restore(type, value, tb);
2920 if (tb == NULL)
2921 return WHY_EXCEPTION;
2922 else
2923 return WHY_RERAISE;
2924 raise_error:
2925 Py_XDECREF(value);
2926 Py_XDECREF(type);
2927 Py_XDECREF(tb);
2928 return WHY_EXCEPTION;
2931 /* Iterate v argcnt times and store the results on the stack (via decreasing
2932 sp). Return 1 for success, 0 if error. */
2934 static int
2935 unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
2937 int i = 0;
2938 PyObject *it; /* iter(v) */
2939 PyObject *w;
2941 assert(v != NULL);
2943 it = PyObject_GetIter(v);
2944 if (it == NULL)
2945 goto Error;
2947 for (; i < argcnt; i++) {
2948 w = PyIter_Next(it);
2949 if (w == NULL) {
2950 /* Iterator done, via error or exhaustion. */
2951 if (!PyErr_Occurred()) {
2952 PyErr_Format(PyExc_ValueError,
2953 "need more than %d value%s to unpack",
2954 i, i == 1 ? "" : "s");
2956 goto Error;
2958 *--sp = w;
2961 /* We better have exhausted the iterator now. */
2962 w = PyIter_Next(it);
2963 if (w == NULL) {
2964 if (PyErr_Occurred())
2965 goto Error;
2966 Py_DECREF(it);
2967 return 1;
2969 Py_DECREF(w);
2970 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
2971 /* fall through */
2972 Error:
2973 for (; i > 0; i--, sp++)
2974 Py_DECREF(*sp);
2975 Py_XDECREF(it);
2976 return 0;
2980 #ifdef LLTRACE
2981 static int
2982 prtrace(PyObject *v, char *str)
2984 printf("%s ", str);
2985 if (PyObject_Print(v, stdout, 0) != 0)
2986 PyErr_Clear(); /* Don't know what else to do */
2987 printf("\n");
2988 return 1;
2990 #endif
2992 static void
2993 call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
2995 PyObject *type, *value, *traceback, *arg;
2996 int err;
2997 PyErr_Fetch(&type, &value, &traceback);
2998 if (value == NULL) {
2999 value = Py_None;
3000 Py_INCREF(value);
3002 arg = Py_BuildValue("(OOO)", type, value, traceback);
3003 if (arg == NULL) {
3004 PyErr_Restore(type, value, traceback);
3005 return;
3007 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
3008 Py_DECREF(arg);
3009 if (err == 0)
3010 PyErr_Restore(type, value, traceback);
3011 else {
3012 Py_XDECREF(type);
3013 Py_XDECREF(value);
3014 Py_XDECREF(traceback);
3018 static void
3019 call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3020 int what)
3022 PyObject *type, *value, *traceback;
3023 int err;
3024 PyErr_Fetch(&type, &value, &traceback);
3025 err = call_trace(func, obj, frame, what, NULL);
3026 if (err == 0)
3027 PyErr_Restore(type, value, traceback);
3028 else {
3029 Py_XDECREF(type);
3030 Py_XDECREF(value);
3031 Py_XDECREF(traceback);
3035 static int
3036 call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3037 int what, PyObject *arg)
3039 register PyThreadState *tstate = frame->f_tstate;
3040 int result;
3041 if (tstate->tracing)
3042 return 0;
3043 tstate->tracing++;
3044 tstate->use_tracing = 0;
3045 result = func(obj, frame, what, arg);
3046 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3047 || (tstate->c_profilefunc != NULL));
3048 tstate->tracing--;
3049 return result;
3052 PyObject *
3053 _PyEval_CallTracing(PyObject *func, PyObject *args)
3055 PyFrameObject *frame = PyEval_GetFrame();
3056 PyThreadState *tstate = frame->f_tstate;
3057 int save_tracing = tstate->tracing;
3058 int save_use_tracing = tstate->use_tracing;
3059 PyObject *result;
3061 tstate->tracing = 0;
3062 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3063 || (tstate->c_profilefunc != NULL));
3064 result = PyObject_Call(func, args, NULL);
3065 tstate->tracing = save_tracing;
3066 tstate->use_tracing = save_use_tracing;
3067 return result;
3070 static int
3071 maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
3072 PyFrameObject *frame, int *instr_lb, int *instr_ub)
3074 /* The theory of SET_LINENO-less tracing.
3076 In a nutshell, we use the co_lnotab field of the code object
3077 to tell when execution has moved onto a different line.
3079 As mentioned above, the basic idea is so set things up so
3080 that
3082 *instr_lb <= frame->f_lasti < *instr_ub
3084 is true so long as execution does not change lines.
3086 This is all fairly simple. Digging the information out of
3087 co_lnotab takes some work, but is conceptually clear.
3089 Somewhat harder to explain is why we don't *always* call the
3090 line trace function when the above test fails.
3092 Consider this code:
3094 1: def f(a):
3095 2: if a:
3096 3: print 1
3097 4: else:
3098 5: print 2
3100 which compiles to this:
3102 2 0 LOAD_FAST 0 (a)
3103 3 JUMP_IF_FALSE 9 (to 15)
3104 6 POP_TOP
3106 3 7 LOAD_CONST 1 (1)
3107 10 PRINT_ITEM
3108 11 PRINT_NEWLINE
3109 12 JUMP_FORWARD 6 (to 21)
3110 >> 15 POP_TOP
3112 5 16 LOAD_CONST 2 (2)
3113 19 PRINT_ITEM
3114 20 PRINT_NEWLINE
3115 >> 21 LOAD_CONST 0 (None)
3116 24 RETURN_VALUE
3118 If 'a' is false, execution will jump to instruction at offset
3119 15 and the co_lnotab will claim that execution has moved to
3120 line 3. This is at best misleading. In this case we could
3121 associate the POP_TOP with line 4, but that doesn't make
3122 sense in all cases (I think).
3124 What we do is only call the line trace function if the co_lnotab
3125 indicates we have jumped to the *start* of a line, i.e. if the
3126 current instruction offset matches the offset given for the
3127 start of a line by the co_lnotab.
3129 This also takes care of the situation where 'a' is true.
3130 Execution will jump from instruction offset 12 to offset 21.
3131 Then the co_lnotab would imply that execution has moved to line
3132 5, which is again misleading.
3134 Why do we set f_lineno when tracing? Well, consider the code
3135 above when 'a' is true. If stepping through this with 'n' in
3136 pdb, you would stop at line 1 with a "call" type event, then
3137 line events on lines 2 and 3, then a "return" type event -- but
3138 you would be shown line 5 during this event. This is a change
3139 from the behaviour in 2.2 and before, and I've found it
3140 confusing in practice. By setting and using f_lineno when
3141 tracing, one can report a line number different from that
3142 suggested by f_lasti on this one occasion where it's desirable.
3145 int result = 0;
3147 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
3148 PyCodeObject* co = frame->f_code;
3149 int size, addr, line;
3150 unsigned char* p;
3152 size = PyString_GET_SIZE(co->co_lnotab) / 2;
3153 p = (unsigned char*)PyString_AS_STRING(co->co_lnotab);
3155 addr = 0;
3156 line = co->co_firstlineno;
3158 /* possible optimization: if f->f_lasti == instr_ub
3159 (likely to be a common case) then we already know
3160 instr_lb -- if we stored the matching value of p
3161 somwhere we could skip the first while loop. */
3163 /* see comments in compile.c for the description of
3164 co_lnotab. A point to remember: increments to p
3165 should come in pairs -- although we don't care about
3166 the line increments here, treating them as byte
3167 increments gets confusing, to say the least. */
3169 while (size > 0) {
3170 if (addr + *p > frame->f_lasti)
3171 break;
3172 addr += *p++;
3173 if (*p) *instr_lb = addr;
3174 line += *p++;
3175 --size;
3178 if (addr == frame->f_lasti) {
3179 frame->f_lineno = line;
3180 result = call_trace(func, obj, frame,
3181 PyTrace_LINE, Py_None);
3184 if (size > 0) {
3185 while (--size >= 0) {
3186 addr += *p++;
3187 if (*p++)
3188 break;
3190 *instr_ub = addr;
3192 else {
3193 *instr_ub = INT_MAX;
3197 return result;
3200 void
3201 PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
3203 PyThreadState *tstate = PyThreadState_Get();
3204 PyObject *temp = tstate->c_profileobj;
3205 Py_XINCREF(arg);
3206 tstate->c_profilefunc = NULL;
3207 tstate->c_profileobj = NULL;
3208 tstate->use_tracing = tstate->c_tracefunc != NULL;
3209 Py_XDECREF(temp);
3210 tstate->c_profilefunc = func;
3211 tstate->c_profileobj = arg;
3212 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
3215 void
3216 PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3218 PyThreadState *tstate = PyThreadState_Get();
3219 PyObject *temp = tstate->c_traceobj;
3220 Py_XINCREF(arg);
3221 tstate->c_tracefunc = NULL;
3222 tstate->c_traceobj = NULL;
3223 tstate->use_tracing = tstate->c_profilefunc != NULL;
3224 Py_XDECREF(temp);
3225 tstate->c_tracefunc = func;
3226 tstate->c_traceobj = arg;
3227 tstate->use_tracing = ((func != NULL)
3228 || (tstate->c_profilefunc != NULL));
3231 PyObject *
3232 PyEval_GetBuiltins(void)
3234 PyFrameObject *current_frame = PyEval_GetFrame();
3235 if (current_frame == NULL)
3236 return PyThreadState_Get()->interp->builtins;
3237 else
3238 return current_frame->f_builtins;
3241 PyObject *
3242 PyEval_GetLocals(void)
3244 PyFrameObject *current_frame = PyEval_GetFrame();
3245 if (current_frame == NULL)
3246 return NULL;
3247 PyFrame_FastToLocals(current_frame);
3248 return current_frame->f_locals;
3251 PyObject *
3252 PyEval_GetGlobals(void)
3254 PyFrameObject *current_frame = PyEval_GetFrame();
3255 if (current_frame == NULL)
3256 return NULL;
3257 else
3258 return current_frame->f_globals;
3261 PyFrameObject *
3262 PyEval_GetFrame(void)
3264 PyThreadState *tstate = PyThreadState_Get();
3265 return _PyThreadState_GetFrame(tstate);
3269 PyEval_GetRestricted(void)
3271 PyFrameObject *current_frame = PyEval_GetFrame();
3272 return current_frame == NULL ? 0 : current_frame->f_restricted;
3276 PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
3278 PyFrameObject *current_frame = PyEval_GetFrame();
3279 int result = cf->cf_flags != 0;
3281 if (current_frame != NULL) {
3282 const int codeflags = current_frame->f_code->co_flags;
3283 const int compilerflags = codeflags & PyCF_MASK;
3284 if (compilerflags) {
3285 result = 1;
3286 cf->cf_flags |= compilerflags;
3288 #if 0 /* future keyword */
3289 if (codeflags & CO_GENERATOR_ALLOWED) {
3290 result = 1;
3291 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3293 #endif
3295 return result;
3299 Py_FlushLine(void)
3301 PyObject *f = PySys_GetObject("stdout");
3302 if (f == NULL)
3303 return 0;
3304 if (!PyFile_SoftSpace(f, 0))
3305 return 0;
3306 return PyFile_WriteString("\n", f);
3310 /* External interface to call any callable object.
3311 The arg must be a tuple or NULL. */
3313 #undef PyEval_CallObject
3314 /* for backward compatibility: export this interface */
3316 PyObject *
3317 PyEval_CallObject(PyObject *func, PyObject *arg)
3319 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
3321 #define PyEval_CallObject(func,arg) \
3322 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
3324 PyObject *
3325 PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
3327 PyObject *result;
3329 if (arg == NULL)
3330 arg = PyTuple_New(0);
3331 else if (!PyTuple_Check(arg)) {
3332 PyErr_SetString(PyExc_TypeError,
3333 "argument list must be a tuple");
3334 return NULL;
3336 else
3337 Py_INCREF(arg);
3339 if (kw != NULL && !PyDict_Check(kw)) {
3340 PyErr_SetString(PyExc_TypeError,
3341 "keyword list must be a dictionary");
3342 Py_DECREF(arg);
3343 return NULL;
3346 result = PyObject_Call(func, arg, kw);
3347 Py_DECREF(arg);
3348 return result;
3351 char *
3352 PyEval_GetFuncName(PyObject *func)
3354 if (PyMethod_Check(func))
3355 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
3356 else if (PyFunction_Check(func))
3357 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3358 else if (PyCFunction_Check(func))
3359 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3360 else if (PyClass_Check(func))
3361 return PyString_AsString(((PyClassObject*)func)->cl_name);
3362 else if (PyInstance_Check(func)) {
3363 return PyString_AsString(
3364 ((PyInstanceObject*)func)->in_class->cl_name);
3365 } else {
3366 return func->ob_type->tp_name;
3370 char *
3371 PyEval_GetFuncDesc(PyObject *func)
3373 if (PyMethod_Check(func))
3374 return "()";
3375 else if (PyFunction_Check(func))
3376 return "()";
3377 else if (PyCFunction_Check(func))
3378 return "()";
3379 else if (PyClass_Check(func))
3380 return " constructor";
3381 else if (PyInstance_Check(func)) {
3382 return " instance";
3383 } else {
3384 return " object";
3388 #define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
3390 static void
3391 err_args(PyObject *func, int flags, int nargs)
3393 if (flags & METH_NOARGS)
3394 PyErr_Format(PyExc_TypeError,
3395 "%.200s() takes no arguments (%d given)",
3396 ((PyCFunctionObject *)func)->m_ml->ml_name,
3397 nargs);
3398 else
3399 PyErr_Format(PyExc_TypeError,
3400 "%.200s() takes exactly one argument (%d given)",
3401 ((PyCFunctionObject *)func)->m_ml->ml_name,
3402 nargs);
3405 static PyObject *
3406 call_function(PyObject ***pp_stack, int oparg)
3408 int na = oparg & 0xff;
3409 int nk = (oparg>>8) & 0xff;
3410 int n = na + 2 * nk;
3411 PyObject **pfunc = (*pp_stack) - n - 1;
3412 PyObject *func = *pfunc;
3413 PyObject *x, *w;
3415 /* Always dispatch PyCFunction first, because these are
3416 presumed to be the most frequent callable object.
3418 if (PyCFunction_Check(func) && nk == 0) {
3419 int flags = PyCFunction_GET_FLAGS(func);
3420 PCALL(PCALL_CFUNCTION);
3421 if (flags & (METH_NOARGS | METH_O)) {
3422 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3423 PyObject *self = PyCFunction_GET_SELF(func);
3424 if (flags & METH_NOARGS && na == 0)
3425 x = (*meth)(self, NULL);
3426 else if (flags & METH_O && na == 1) {
3427 PyObject *arg = EXT_POP(*pp_stack);
3428 x = (*meth)(self, arg);
3429 Py_DECREF(arg);
3431 else {
3432 err_args(func, flags, na);
3433 x = NULL;
3436 else {
3437 PyObject *callargs;
3438 callargs = load_args(pp_stack, na);
3439 x = PyCFunction_Call(func, callargs, NULL);
3440 Py_XDECREF(callargs);
3442 } else {
3443 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3444 /* optimize access to bound methods */
3445 PyObject *self = PyMethod_GET_SELF(func);
3446 PCALL(PCALL_METHOD);
3447 PCALL(PCALL_BOUND_METHOD);
3448 Py_INCREF(self);
3449 func = PyMethod_GET_FUNCTION(func);
3450 Py_INCREF(func);
3451 Py_DECREF(*pfunc);
3452 *pfunc = self;
3453 na++;
3454 n++;
3455 } else
3456 Py_INCREF(func);
3457 if (PyFunction_Check(func))
3458 x = fast_function(func, pp_stack, n, na, nk);
3459 else
3460 x = do_call(func, pp_stack, na, nk);
3461 Py_DECREF(func);
3464 /* What does this do? */
3465 while ((*pp_stack) > pfunc) {
3466 w = EXT_POP(*pp_stack);
3467 Py_DECREF(w);
3468 PCALL(PCALL_POP);
3470 return x;
3473 /* The fast_function() function optimize calls for which no argument
3474 tuple is necessary; the objects are passed directly from the stack.
3475 For the simplest case -- a function that takes only positional
3476 arguments and is called with only positional arguments -- it
3477 inlines the most primitive frame setup code from
3478 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3479 done before evaluating the frame.
3482 static PyObject *
3483 fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
3485 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
3486 PyObject *globals = PyFunction_GET_GLOBALS(func);
3487 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3488 PyObject **d = NULL;
3489 int nd = 0;
3491 PCALL(PCALL_FUNCTION);
3492 PCALL(PCALL_FAST_FUNCTION);
3493 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
3494 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3495 PyFrameObject *f;
3496 PyObject *retval = NULL;
3497 PyThreadState *tstate = PyThreadState_GET();
3498 PyObject **fastlocals, **stack;
3499 int i;
3501 PCALL(PCALL_FASTER_FUNCTION);
3502 assert(globals != NULL);
3503 /* XXX Perhaps we should create a specialized
3504 PyFrame_New() that doesn't take locals, but does
3505 take builtins without sanity checking them.
3507 f = PyFrame_New(tstate, co, globals, NULL);
3508 if (f == NULL)
3509 return NULL;
3511 fastlocals = f->f_localsplus;
3512 stack = (*pp_stack) - n;
3514 for (i = 0; i < n; i++) {
3515 Py_INCREF(*stack);
3516 fastlocals[i] = *stack++;
3518 retval = eval_frame(f);
3519 assert(tstate != NULL);
3520 ++tstate->recursion_depth;
3521 Py_DECREF(f);
3522 --tstate->recursion_depth;
3523 return retval;
3525 if (argdefs != NULL) {
3526 d = &PyTuple_GET_ITEM(argdefs, 0);
3527 nd = ((PyTupleObject *)argdefs)->ob_size;
3529 return PyEval_EvalCodeEx(co, globals,
3530 (PyObject *)NULL, (*pp_stack)-n, na,
3531 (*pp_stack)-2*nk, nk, d, nd,
3532 PyFunction_GET_CLOSURE(func));
3535 static PyObject *
3536 update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3537 PyObject *func)
3539 PyObject *kwdict = NULL;
3540 if (orig_kwdict == NULL)
3541 kwdict = PyDict_New();
3542 else {
3543 kwdict = PyDict_Copy(orig_kwdict);
3544 Py_DECREF(orig_kwdict);
3546 if (kwdict == NULL)
3547 return NULL;
3548 while (--nk >= 0) {
3549 int err;
3550 PyObject *value = EXT_POP(*pp_stack);
3551 PyObject *key = EXT_POP(*pp_stack);
3552 if (PyDict_GetItem(kwdict, key) != NULL) {
3553 PyErr_Format(PyExc_TypeError,
3554 "%.200s%s got multiple values "
3555 "for keyword argument '%.200s'",
3556 PyEval_GetFuncName(func),
3557 PyEval_GetFuncDesc(func),
3558 PyString_AsString(key));
3559 Py_DECREF(key);
3560 Py_DECREF(value);
3561 Py_DECREF(kwdict);
3562 return NULL;
3564 err = PyDict_SetItem(kwdict, key, value);
3565 Py_DECREF(key);
3566 Py_DECREF(value);
3567 if (err) {
3568 Py_DECREF(kwdict);
3569 return NULL;
3572 return kwdict;
3575 static PyObject *
3576 update_star_args(int nstack, int nstar, PyObject *stararg,
3577 PyObject ***pp_stack)
3579 PyObject *callargs, *w;
3581 callargs = PyTuple_New(nstack + nstar);
3582 if (callargs == NULL) {
3583 return NULL;
3585 if (nstar) {
3586 int i;
3587 for (i = 0; i < nstar; i++) {
3588 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3589 Py_INCREF(a);
3590 PyTuple_SET_ITEM(callargs, nstack + i, a);
3593 while (--nstack >= 0) {
3594 w = EXT_POP(*pp_stack);
3595 PyTuple_SET_ITEM(callargs, nstack, w);
3597 return callargs;
3600 static PyObject *
3601 load_args(PyObject ***pp_stack, int na)
3603 PyObject *args = PyTuple_New(na);
3604 PyObject *w;
3606 if (args == NULL)
3607 return NULL;
3608 while (--na >= 0) {
3609 w = EXT_POP(*pp_stack);
3610 PyTuple_SET_ITEM(args, na, w);
3612 return args;
3615 static PyObject *
3616 do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3618 PyObject *callargs = NULL;
3619 PyObject *kwdict = NULL;
3620 PyObject *result = NULL;
3622 if (nk > 0) {
3623 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
3624 if (kwdict == NULL)
3625 goto call_fail;
3627 callargs = load_args(pp_stack, na);
3628 if (callargs == NULL)
3629 goto call_fail;
3630 #ifdef CALL_PROFILE
3631 /* At this point, we have to look at the type of func to
3632 update the call stats properly. Do it here so as to avoid
3633 exposing the call stats machinery outside ceval.c
3635 if (PyFunction_Check(func))
3636 PCALL(PCALL_FUNCTION);
3637 else if (PyMethod_Check(func))
3638 PCALL(PCALL_METHOD);
3639 else if (PyType_Check(func))
3640 PCALL(PCALL_TYPE);
3641 else
3642 PCALL(PCALL_OTHER);
3643 #endif
3644 result = PyObject_Call(func, callargs, kwdict);
3645 call_fail:
3646 Py_XDECREF(callargs);
3647 Py_XDECREF(kwdict);
3648 return result;
3651 static PyObject *
3652 ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3654 int nstar = 0;
3655 PyObject *callargs = NULL;
3656 PyObject *stararg = NULL;
3657 PyObject *kwdict = NULL;
3658 PyObject *result = NULL;
3660 if (flags & CALL_FLAG_KW) {
3661 kwdict = EXT_POP(*pp_stack);
3662 if (!(kwdict && PyDict_Check(kwdict))) {
3663 PyErr_Format(PyExc_TypeError,
3664 "%s%s argument after ** "
3665 "must be a dictionary",
3666 PyEval_GetFuncName(func),
3667 PyEval_GetFuncDesc(func));
3668 goto ext_call_fail;
3671 if (flags & CALL_FLAG_VAR) {
3672 stararg = EXT_POP(*pp_stack);
3673 if (!PyTuple_Check(stararg)) {
3674 PyObject *t = NULL;
3675 t = PySequence_Tuple(stararg);
3676 if (t == NULL) {
3677 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3678 PyErr_Format(PyExc_TypeError,
3679 "%s%s argument after * "
3680 "must be a sequence",
3681 PyEval_GetFuncName(func),
3682 PyEval_GetFuncDesc(func));
3684 goto ext_call_fail;
3686 Py_DECREF(stararg);
3687 stararg = t;
3689 nstar = PyTuple_GET_SIZE(stararg);
3691 if (nk > 0) {
3692 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
3693 if (kwdict == NULL)
3694 goto ext_call_fail;
3696 callargs = update_star_args(na, nstar, stararg, pp_stack);
3697 if (callargs == NULL)
3698 goto ext_call_fail;
3699 #ifdef CALL_PROFILE
3700 /* At this point, we have to look at the type of func to
3701 update the call stats properly. Do it here so as to avoid
3702 exposing the call stats machinery outside ceval.c
3704 if (PyFunction_Check(func))
3705 PCALL(PCALL_FUNCTION);
3706 else if (PyMethod_Check(func))
3707 PCALL(PCALL_METHOD);
3708 else if (PyType_Check(func))
3709 PCALL(PCALL_TYPE);
3710 else
3711 PCALL(PCALL_OTHER);
3712 #endif
3713 result = PyObject_Call(func, callargs, kwdict);
3714 ext_call_fail:
3715 Py_XDECREF(callargs);
3716 Py_XDECREF(kwdict);
3717 Py_XDECREF(stararg);
3718 return result;
3721 #define SLICE_ERROR_MSG \
3722 "standard sequence type does not support step size other than one"
3724 /* Extract a slice index from a PyInt or PyLong, and store in *pi.
3725 Silently reduce values larger than INT_MAX to INT_MAX, and silently
3726 boost values less than -INT_MAX to 0. Return 0 on error, 1 on success.
3728 /* Note: If v is NULL, return success without storing into *pi. This
3729 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3730 called by the SLICE opcode with v and/or w equal to NULL.
3733 _PyEval_SliceIndex(PyObject *v, int *pi)
3735 if (v != NULL) {
3736 long x;
3737 if (PyInt_Check(v)) {
3738 x = PyInt_AsLong(v);
3739 } else if (PyLong_Check(v)) {
3740 x = PyLong_AsLong(v);
3741 if (x==-1 && PyErr_Occurred()) {
3742 PyObject *long_zero;
3743 int cmp;
3745 if (!PyErr_ExceptionMatches(
3746 PyExc_OverflowError)) {
3747 /* It's not an overflow error, so just
3748 signal an error */
3749 return 0;
3752 /* Clear the OverflowError */
3753 PyErr_Clear();
3755 /* It's an overflow error, so we need to
3756 check the sign of the long integer,
3757 set the value to INT_MAX or -INT_MAX,
3758 and clear the error. */
3760 /* Create a long integer with a value of 0 */
3761 long_zero = PyLong_FromLong(0L);
3762 if (long_zero == NULL)
3763 return 0;
3765 /* Check sign */
3766 cmp = PyObject_RichCompareBool(v, long_zero,
3767 Py_GT);
3768 Py_DECREF(long_zero);
3769 if (cmp < 0)
3770 return 0;
3771 else if (cmp)
3772 x = INT_MAX;
3773 else
3774 x = -INT_MAX;
3776 } else {
3777 PyErr_SetString(PyExc_TypeError,
3778 "slice indices must be integers");
3779 return 0;
3781 /* Truncate -- very long indices are truncated anyway */
3782 if (x > INT_MAX)
3783 x = INT_MAX;
3784 else if (x < -INT_MAX)
3785 x = -INT_MAX;
3786 *pi = x;
3788 return 1;
3791 #undef ISINT
3792 #define ISINT(x) ((x) == NULL || PyInt_Check(x) || PyLong_Check(x))
3794 static PyObject *
3795 apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
3797 PyTypeObject *tp = u->ob_type;
3798 PySequenceMethods *sq = tp->tp_as_sequence;
3800 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3801 int ilow = 0, ihigh = INT_MAX;
3802 if (!_PyEval_SliceIndex(v, &ilow))
3803 return NULL;
3804 if (!_PyEval_SliceIndex(w, &ihigh))
3805 return NULL;
3806 return PySequence_GetSlice(u, ilow, ihigh);
3808 else {
3809 PyObject *slice = PySlice_New(v, w, NULL);
3810 if (slice != NULL) {
3811 PyObject *res = PyObject_GetItem(u, slice);
3812 Py_DECREF(slice);
3813 return res;
3815 else
3816 return NULL;
3820 static int
3821 assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
3822 /* u[v:w] = x */
3824 PyTypeObject *tp = u->ob_type;
3825 PySequenceMethods *sq = tp->tp_as_sequence;
3827 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3828 int ilow = 0, ihigh = INT_MAX;
3829 if (!_PyEval_SliceIndex(v, &ilow))
3830 return -1;
3831 if (!_PyEval_SliceIndex(w, &ihigh))
3832 return -1;
3833 if (x == NULL)
3834 return PySequence_DelSlice(u, ilow, ihigh);
3835 else
3836 return PySequence_SetSlice(u, ilow, ihigh, x);
3838 else {
3839 PyObject *slice = PySlice_New(v, w, NULL);
3840 if (slice != NULL) {
3841 int res;
3842 if (x != NULL)
3843 res = PyObject_SetItem(u, slice, x);
3844 else
3845 res = PyObject_DelItem(u, slice);
3846 Py_DECREF(slice);
3847 return res;
3849 else
3850 return -1;
3854 static PyObject *
3855 cmp_outcome(int op, register PyObject *v, register PyObject *w)
3857 int res = 0;
3858 switch (op) {
3859 case PyCmp_IS:
3860 res = (v == w);
3861 break;
3862 case PyCmp_IS_NOT:
3863 res = (v != w);
3864 break;
3865 case PyCmp_IN:
3866 res = PySequence_Contains(w, v);
3867 if (res < 0)
3868 return NULL;
3869 break;
3870 case PyCmp_NOT_IN:
3871 res = PySequence_Contains(w, v);
3872 if (res < 0)
3873 return NULL;
3874 res = !res;
3875 break;
3876 case PyCmp_EXC_MATCH:
3877 res = PyErr_GivenExceptionMatches(v, w);
3878 break;
3879 default:
3880 return PyObject_RichCompare(v, w, op);
3882 v = res ? Py_True : Py_False;
3883 Py_INCREF(v);
3884 return v;
3887 static PyObject *
3888 import_from(PyObject *v, PyObject *name)
3890 PyObject *x;
3892 x = PyObject_GetAttr(v, name);
3893 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
3894 PyErr_Format(PyExc_ImportError,
3895 "cannot import name %.230s",
3896 PyString_AsString(name));
3898 return x;
3901 static int
3902 import_all_from(PyObject *locals, PyObject *v)
3904 PyObject *all = PyObject_GetAttrString(v, "__all__");
3905 PyObject *dict, *name, *value;
3906 int skip_leading_underscores = 0;
3907 int pos, err;
3909 if (all == NULL) {
3910 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3911 return -1; /* Unexpected error */
3912 PyErr_Clear();
3913 dict = PyObject_GetAttrString(v, "__dict__");
3914 if (dict == NULL) {
3915 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3916 return -1;
3917 PyErr_SetString(PyExc_ImportError,
3918 "from-import-* object has no __dict__ and no __all__");
3919 return -1;
3921 all = PyMapping_Keys(dict);
3922 Py_DECREF(dict);
3923 if (all == NULL)
3924 return -1;
3925 skip_leading_underscores = 1;
3928 for (pos = 0, err = 0; ; pos++) {
3929 name = PySequence_GetItem(all, pos);
3930 if (name == NULL) {
3931 if (!PyErr_ExceptionMatches(PyExc_IndexError))
3932 err = -1;
3933 else
3934 PyErr_Clear();
3935 break;
3937 if (skip_leading_underscores &&
3938 PyString_Check(name) &&
3939 PyString_AS_STRING(name)[0] == '_')
3941 Py_DECREF(name);
3942 continue;
3944 value = PyObject_GetAttr(v, name);
3945 if (value == NULL)
3946 err = -1;
3947 else
3948 err = PyDict_SetItem(locals, name, value);
3949 Py_DECREF(name);
3950 Py_XDECREF(value);
3951 if (err != 0)
3952 break;
3954 Py_DECREF(all);
3955 return err;
3958 static PyObject *
3959 build_class(PyObject *methods, PyObject *bases, PyObject *name)
3961 PyObject *metaclass = NULL, *result, *base;
3963 if (PyDict_Check(methods))
3964 metaclass = PyDict_GetItemString(methods, "__metaclass__");
3965 if (metaclass != NULL)
3966 Py_INCREF(metaclass);
3967 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
3968 base = PyTuple_GET_ITEM(bases, 0);
3969 metaclass = PyObject_GetAttrString(base, "__class__");
3970 if (metaclass == NULL) {
3971 PyErr_Clear();
3972 metaclass = (PyObject *)base->ob_type;
3973 Py_INCREF(metaclass);
3976 else {
3977 PyObject *g = PyEval_GetGlobals();
3978 if (g != NULL && PyDict_Check(g))
3979 metaclass = PyDict_GetItemString(g, "__metaclass__");
3980 if (metaclass == NULL)
3981 metaclass = (PyObject *) &PyClass_Type;
3982 Py_INCREF(metaclass);
3984 result = PyObject_CallFunction(metaclass, "OOO", name, bases, methods);
3985 Py_DECREF(metaclass);
3986 return result;
3989 static int
3990 exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
3991 PyObject *locals)
3993 int n;
3994 PyObject *v;
3995 int plain = 0;
3997 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
3998 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
3999 /* Backward compatibility hack */
4000 globals = PyTuple_GetItem(prog, 1);
4001 if (n == 3)
4002 locals = PyTuple_GetItem(prog, 2);
4003 prog = PyTuple_GetItem(prog, 0);
4005 if (globals == Py_None) {
4006 globals = PyEval_GetGlobals();
4007 if (locals == Py_None) {
4008 locals = PyEval_GetLocals();
4009 plain = 1;
4012 else if (locals == Py_None)
4013 locals = globals;
4014 if (!PyString_Check(prog) &&
4015 !PyUnicode_Check(prog) &&
4016 !PyCode_Check(prog) &&
4017 !PyFile_Check(prog)) {
4018 PyErr_SetString(PyExc_TypeError,
4019 "exec: arg 1 must be a string, file, or code object");
4020 return -1;
4022 if (!PyDict_Check(globals)) {
4023 PyErr_SetString(PyExc_TypeError,
4024 "exec: arg 2 must be a dictionary or None");
4025 return -1;
4027 if (!PyDict_Check(locals)) {
4028 PyErr_SetString(PyExc_TypeError,
4029 "exec: arg 3 must be a dictionary or None");
4030 return -1;
4032 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
4033 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
4034 if (PyCode_Check(prog)) {
4035 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4036 PyErr_SetString(PyExc_TypeError,
4037 "code object passed to exec may not contain free variables");
4038 return -1;
4040 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
4042 else if (PyFile_Check(prog)) {
4043 FILE *fp = PyFile_AsFile(prog);
4044 char *name = PyString_AsString(PyFile_Name(prog));
4045 PyCompilerFlags cf;
4046 cf.cf_flags = 0;
4047 if (PyEval_MergeCompilerFlags(&cf))
4048 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
4049 locals, &cf);
4050 else
4051 v = PyRun_File(fp, name, Py_file_input, globals,
4052 locals);
4054 else {
4055 PyObject *tmp = NULL;
4056 char *str;
4057 PyCompilerFlags cf;
4058 cf.cf_flags = 0;
4059 #ifdef Py_USING_UNICODE
4060 if (PyUnicode_Check(prog)) {
4061 tmp = PyUnicode_AsUTF8String(prog);
4062 if (tmp == NULL)
4063 return -1;
4064 prog = tmp;
4065 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4067 #endif
4068 if (PyString_AsStringAndSize(prog, &str, NULL))
4069 return -1;
4070 if (PyEval_MergeCompilerFlags(&cf))
4071 v = PyRun_StringFlags(str, Py_file_input, globals,
4072 locals, &cf);
4073 else
4074 v = PyRun_String(str, Py_file_input, globals, locals);
4075 Py_XDECREF(tmp);
4077 if (plain)
4078 PyFrame_LocalsToFast(f, 0);
4079 if (v == NULL)
4080 return -1;
4081 Py_DECREF(v);
4082 return 0;
4085 static void
4086 format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4088 char *obj_str;
4090 if (!obj)
4091 return;
4093 obj_str = PyString_AsString(obj);
4094 if (!obj_str)
4095 return;
4097 PyErr_Format(exc, format_str, obj_str);
4100 #ifdef DYNAMIC_EXECUTION_PROFILE
4102 static PyObject *
4103 getarray(long a[256])
4105 int i;
4106 PyObject *l = PyList_New(256);
4107 if (l == NULL) return NULL;
4108 for (i = 0; i < 256; i++) {
4109 PyObject *x = PyInt_FromLong(a[i]);
4110 if (x == NULL) {
4111 Py_DECREF(l);
4112 return NULL;
4114 PyList_SetItem(l, i, x);
4116 for (i = 0; i < 256; i++)
4117 a[i] = 0;
4118 return l;
4121 PyObject *
4122 _Py_GetDXProfile(PyObject *self, PyObject *args)
4124 #ifndef DXPAIRS
4125 return getarray(dxp);
4126 #else
4127 int i;
4128 PyObject *l = PyList_New(257);
4129 if (l == NULL) return NULL;
4130 for (i = 0; i < 257; i++) {
4131 PyObject *x = getarray(dxpairs[i]);
4132 if (x == NULL) {
4133 Py_DECREF(l);
4134 return NULL;
4136 PyList_SetItem(l, i, x);
4138 return l;
4139 #endif
4142 #endif