2 /* Execute compiled code */
5 XXX speed up searching for keywords by using a dictionary
12 #include "frameobject.h"
15 #include "structmember.h"
23 /* Turn this on if your compiler chokes on the big switch: */
24 /* #define CASE_TOO_BIG 1 */
27 /* For debugging the interpreter: */
28 #define LLTRACE 1 /* Low-level trace feature */
29 #define CHECKEXC 1 /* Double-check exception checking */
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
47 static int prtrace(PyObject
*, char *);
49 static int call_trace(Py_tracefunc
, PyObject
*, PyFrameObject
*,
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" \
80 /* Dynamic execution profile */
81 #ifdef DYNAMIC_EXECUTION_PROFILE
83 static long dxpairs
[257][256];
84 #define dxp dxpairs[256]
90 /* Function call profile */
93 static int pcall
[PCALL_NUM
];
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
103 #define PCALL_GENERATOR 8
104 #define PCALL_OTHER 9
107 /* Notes about the statistics
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
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]++
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],
139 PyEval_GetCallStats(PyObject
*self
)
146 static PyTypeObject gentype
;
150 /* The gi_ prefix is intended to remind of generator-iterator. */
152 PyFrameObject
*gi_frame
;
154 /* True if generator is being executed. */
157 /* List of weak reference. */
158 PyObject
*gi_weakreflist
;
162 gen_new(PyFrameObject
*f
)
164 genobject
*gen
= PyObject_GC_New(genobject
, &gentype
);
171 gen
->gi_weakreflist
= NULL
;
172 _PyObject_GC_TRACK(gen
);
173 return (PyObject
*)gen
;
177 gen_traverse(genobject
*gen
, visitproc visit
, void *arg
)
179 return visit((PyObject
*)gen
->gi_frame
, arg
);
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
);
193 gen_iternext(genobject
*gen
)
195 PyThreadState
*tstate
= PyThreadState_GET();
196 PyFrameObject
*f
= gen
->gi_frame
;
199 if (gen
->gi_running
) {
200 PyErr_SetString(PyExc_ValueError
,
201 "generator already executing");
204 if (f
->f_stacktop
== 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
;
214 result
= eval_frame(f
);
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
220 Py_XDECREF(f
->f_back
);
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
) {
234 gen_getiter(PyObject
*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
)
249 "generator", /* tp_name */
250 sizeof(genobject
), /* tp_basicsize */
253 (destructor
)gen_dealloc
, /* tp_dealloc */
259 0, /* tp_as_number */
260 0, /* tp_as_sequence */
261 0, /* tp_as_mapping */
265 PyObject_GenericGetAttr
, /* tp_getattro */
267 0, /* tp_as_buffer */
268 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
,/* tp_flags */
270 (traverseproc
)gen_traverse
, /* tp_traverse */
272 0, /* tp_richcompare */
273 offsetof(genobject
, gi_weakreflist
), /* tp_weaklistoffset */
274 (getiterfunc
)gen_getiter
, /* tp_iter */
275 (iternextfunc
)gen_iternext
, /* tp_iternext */
277 gen_memberlist
, /* tp_members */
286 #ifndef DONT_HAVE_ERRNO_H
289 #include "pythread.h"
291 extern int _PyThread_Started
; /* Flag for Py_Exit */
293 static PyThread_type_lock interpreter_lock
= 0;
294 static long main_thread
= 0;
297 PyEval_InitThreads(void)
299 if (interpreter_lock
)
301 _PyThread_Started
= 1;
302 interpreter_lock
= PyThread_allocate_lock();
303 PyThread_acquire_lock(interpreter_lock
, 1);
304 main_thread
= PyThread_get_thread_ident();
308 PyEval_AcquireLock(void)
310 PyThread_acquire_lock(interpreter_lock
, 1);
314 PyEval_ReleaseLock(void)
316 PyThread_release_lock(interpreter_lock
);
320 PyEval_AcquireThread(PyThreadState
*tstate
)
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
)
329 "PyEval_AcquireThread: non-NULL old thread state");
333 PyEval_ReleaseThread(PyThreadState
*tstate
)
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.) */
348 PyEval_ReInitThreads(void)
350 if (!interpreter_lock
)
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();
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: */
367 PyEval_SaveThread(void)
369 PyThreadState
*tstate
= PyThreadState_Swap(NULL
);
371 Py_FatalError("PyEval_SaveThread: NULL tstate");
373 if (interpreter_lock
)
374 PyThread_release_lock(interpreter_lock
);
380 PyEval_RestoreThread(PyThreadState
*tstate
)
383 Py_FatalError("PyEval_RestoreThread: NULL tstate");
385 if (interpreter_lock
) {
387 PyThread_acquire_lock(interpreter_lock
, 1);
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!
410 Any thread can schedule pending calls, but only the main thread
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
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
)
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! */
448 j
= (i
+ 1) % NPENDINGCALLS
;
449 if (j
== pendingfirst
) {
451 return -1; /* Queue full */
453 pendingcalls
[i
].func
= func
;
454 pendingcalls
[i
].arg
= arg
;
458 things_to_do
= 1; /* Signal main loop */
460 /* XXX End critical section */
465 Py_MakePendingCalls(void)
469 if (main_thread
&& PyThread_get_thread_ident() != main_thread
)
481 if (i
== pendinglast
)
482 break; /* Queue empty */
483 func
= pendingcalls
[i
].func
;
484 arg
= pendingcalls
[i
].arg
;
485 pendingfirst
= (i
+ 1) % NPENDINGCALLS
;
488 things_to_do
= 1; /* We're not done yet */
497 /* The interpreter's recursion limit */
499 static int recursion_limit
= 1000;
502 Py_GetRecursionLimit(void)
504 return recursion_limit
;
508 Py_SetRecursionLimit(int new_limit
)
510 recursion_limit
= new_limit
;
513 /* Status code for main loop (reason for stack unwind) */
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;
534 PyEval_EvalCode(PyCodeObject
*co
, PyObject
*globals
, PyObject
*locals
)
536 /* XXX raise SystemError if globals is NULL */
537 return PyEval_EvalCodeEx(co
,
539 (PyObject
**)NULL
, 0,
540 (PyObject
**)NULL
, 0,
541 (PyObject
**)NULL
, 0,
546 /* Interpreter main loop */
549 eval_frame(PyFrameObject
*f
)
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();
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
;
586 #if defined(Py_DEBUG) || defined(LLTRACE)
587 /* Make it easier to find out where we are with a debugger */
591 /* Tuple access macros */
594 #define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
596 #define GETITEM(v, i) PyTuple_GetItem((v), (i))
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
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)
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); }
653 #define PUSH(v) BASIC_PUSH(v)
654 #define POP() BASIC_POP()
655 #define STACKADJ(n) BASIC_STACKADJ(n)
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)
677 #ifdef USE_STACKCHECK
678 if (tstate
->recursion_depth
%10 == 0 && PyOS_CheckStack()) {
679 PyErr_SetString(PyExc_MemoryError
, "Stack overflow");
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
;
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
;
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
;
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 */
752 lltrace
= PyDict_GetItemString(f
->f_globals
,"__lltrace__") != NULL
;
754 #if defined(Py_DEBUG) || defined(LLTRACE)
755 filename
= PyString_AsString(co
->co_filename
);
760 x
= Py_None
; /* Not a reference, just anything non-NULL */
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 _Py_Ticker
= _Py_CheckInterval
;
777 tstate
->tick_counter
++;
779 if (Py_MakePendingCalls() < 0) {
784 #if !defined(HAVE_SIGNAL_H) || defined(macintosh)
785 /* If we have true signals, the signal handler
786 will call Py_AddPendingCall() so we don't
787 have to call PyErr_CheckSignals(). On the
788 Mac and DOS, alas, we have to call it. */
789 if (PyErr_CheckSignals()) {
796 if (interpreter_lock
) {
797 /* Give another thread a chance */
799 if (PyThreadState_Swap(NULL
) != tstate
)
800 Py_FatalError("ceval: tstate mix-up");
801 PyThread_release_lock(interpreter_lock
);
803 /* Other threads may run now */
805 PyThread_acquire_lock(interpreter_lock
, 1);
806 if (PyThreadState_Swap(tstate
) != NULL
)
807 Py_FatalError("ceval: orphan tstate");
813 f
->f_lasti
= INSTR_OFFSET();
815 /* line-by-line tracing support */
817 if (tstate
->c_tracefunc
!= NULL
&& !tstate
->tracing
) {
818 /* see maybe_call_line_trace
819 for expository comments */
820 f
->f_stacktop
= stack_pointer
;
822 err
= maybe_call_line_trace(tstate
->c_tracefunc
,
824 f
, &instr_lb
, &instr_ub
);
825 /* Reload possibly changed frame fields */
827 if (f
->f_stacktop
!= NULL
) {
828 stack_pointer
= f
->f_stacktop
;
829 f
->f_stacktop
= NULL
;
832 /* trace function raised an exception */
837 /* Extract opcode and argument */
843 #ifdef DYNAMIC_EXECUTION_PROFILE
845 dxpairs
[lastopcode
][opcode
]++;
852 /* Instruction tracing */
855 if (HAS_ARG(opcode
)) {
856 printf("%d: %d, %d\n",
857 f
->f_lasti
, opcode
, oparg
);
866 /* Main switch on opcode */
871 It is essential that any operation that fails sets either
872 x to NULL, err to nonzero, or why to anything but WHY_NOT,
873 and that no operation that succeeds does this! */
875 /* case STOP_CODE: this is an error! */
882 goto fast_next_opcode
;
884 format_exc_check_arg(PyExc_UnboundLocalError
,
885 UNBOUNDLOCAL_ERROR_MSG
,
886 PyTuple_GetItem(co
->co_varnames
, oparg
));
890 x
= GETITEM(consts
, oparg
);
893 goto fast_next_opcode
;
895 PREDICTED_WITH_ARG(STORE_FAST
);
899 goto fast_next_opcode
;
905 goto fast_next_opcode
;
912 goto fast_next_opcode
;
921 goto fast_next_opcode
;
932 goto fast_next_opcode
;
938 goto fast_next_opcode
;
949 goto fast_next_opcode
;
950 } else if (oparg
== 3) {
961 goto fast_next_opcode
;
963 Py_FatalError("invalid argument to DUP_TOPX"
964 " (bytecode corruption?)");
969 x
= PyNumber_Positive(v
);
972 if (x
!= NULL
) continue;
977 x
= PyNumber_Negative(v
);
980 if (x
!= NULL
) continue;
985 err
= PyObject_IsTrue(v
);
1003 x
= PyObject_Repr(v
);
1006 if (x
!= NULL
) continue;
1011 x
= PyNumber_Invert(v
);
1014 if (x
!= NULL
) continue;
1020 x
= PyNumber_Power(v
, w
, Py_None
);
1024 if (x
!= NULL
) continue;
1027 case BINARY_MULTIPLY
:
1030 x
= PyNumber_Multiply(v
, w
);
1034 if (x
!= NULL
) continue;
1038 if (!_Py_QnewFlag
) {
1041 x
= PyNumber_Divide(v
, w
);
1045 if (x
!= NULL
) continue;
1048 /* -Qnew is in effect: fall through to
1049 BINARY_TRUE_DIVIDE */
1050 case BINARY_TRUE_DIVIDE
:
1053 x
= PyNumber_TrueDivide(v
, w
);
1057 if (x
!= NULL
) continue;
1060 case BINARY_FLOOR_DIVIDE
:
1063 x
= PyNumber_FloorDivide(v
, w
);
1067 if (x
!= NULL
) continue;
1073 x
= PyNumber_Remainder(v
, w
);
1077 if (x
!= NULL
) continue;
1083 if (PyInt_CheckExact(v
) && PyInt_CheckExact(w
)) {
1084 /* INLINE: int + int */
1085 register long a
, b
, i
;
1086 a
= PyInt_AS_LONG(v
);
1087 b
= PyInt_AS_LONG(w
);
1089 if ((i
^a
) < 0 && (i
^b
) < 0)
1091 x
= PyInt_FromLong(i
);
1095 x
= PyNumber_Add(v
, w
);
1100 if (x
!= NULL
) continue;
1103 case BINARY_SUBTRACT
:
1106 if (PyInt_CheckExact(v
) && PyInt_CheckExact(w
)) {
1107 /* INLINE: int - int */
1108 register long a
, b
, i
;
1109 a
= PyInt_AS_LONG(v
);
1110 b
= PyInt_AS_LONG(w
);
1112 if ((i
^a
) < 0 && (i
^~b
) < 0)
1114 x
= PyInt_FromLong(i
);
1118 x
= PyNumber_Subtract(v
, w
);
1123 if (x
!= NULL
) continue;
1129 if (PyList_CheckExact(v
) && PyInt_CheckExact(w
)) {
1130 /* INLINE: list[int] */
1131 long i
= PyInt_AsLong(w
);
1133 i
+= PyList_GET_SIZE(v
);
1135 i
>= PyList_GET_SIZE(v
)) {
1136 PyErr_SetString(PyExc_IndexError
,
1137 "list index out of range");
1141 x
= PyList_GET_ITEM(v
, i
);
1146 x
= PyObject_GetItem(v
, w
);
1150 if (x
!= NULL
) continue;
1156 x
= PyNumber_Lshift(v
, w
);
1160 if (x
!= NULL
) continue;
1166 x
= PyNumber_Rshift(v
, w
);
1170 if (x
!= NULL
) continue;
1176 x
= PyNumber_And(v
, w
);
1180 if (x
!= NULL
) continue;
1186 x
= PyNumber_Xor(v
, w
);
1190 if (x
!= NULL
) continue;
1196 x
= PyNumber_Or(v
, w
);
1200 if (x
!= NULL
) continue;
1206 x
= PyNumber_InPlacePower(v
, w
, Py_None
);
1210 if (x
!= NULL
) continue;
1213 case INPLACE_MULTIPLY
:
1216 x
= PyNumber_InPlaceMultiply(v
, w
);
1220 if (x
!= NULL
) continue;
1223 case INPLACE_DIVIDE
:
1224 if (!_Py_QnewFlag
) {
1227 x
= PyNumber_InPlaceDivide(v
, w
);
1231 if (x
!= NULL
) continue;
1234 /* -Qnew is in effect: fall through to
1235 INPLACE_TRUE_DIVIDE */
1236 case INPLACE_TRUE_DIVIDE
:
1239 x
= PyNumber_InPlaceTrueDivide(v
, w
);
1243 if (x
!= NULL
) continue;
1246 case INPLACE_FLOOR_DIVIDE
:
1249 x
= PyNumber_InPlaceFloorDivide(v
, w
);
1253 if (x
!= NULL
) continue;
1256 case INPLACE_MODULO
:
1259 x
= PyNumber_InPlaceRemainder(v
, w
);
1263 if (x
!= NULL
) continue;
1269 if (PyInt_CheckExact(v
) && PyInt_CheckExact(w
)) {
1270 /* INLINE: int + int */
1271 register long a
, b
, i
;
1272 a
= PyInt_AS_LONG(v
);
1273 b
= PyInt_AS_LONG(w
);
1275 if ((i
^a
) < 0 && (i
^b
) < 0)
1277 x
= PyInt_FromLong(i
);
1281 x
= PyNumber_InPlaceAdd(v
, w
);
1286 if (x
!= NULL
) continue;
1289 case INPLACE_SUBTRACT
:
1292 if (PyInt_CheckExact(v
) && PyInt_CheckExact(w
)) {
1293 /* INLINE: int - int */
1294 register long a
, b
, i
;
1295 a
= PyInt_AS_LONG(v
);
1296 b
= PyInt_AS_LONG(w
);
1298 if ((i
^a
) < 0 && (i
^~b
) < 0)
1300 x
= PyInt_FromLong(i
);
1304 x
= PyNumber_InPlaceSubtract(v
, w
);
1309 if (x
!= NULL
) continue;
1312 case INPLACE_LSHIFT
:
1315 x
= PyNumber_InPlaceLshift(v
, w
);
1319 if (x
!= NULL
) continue;
1322 case INPLACE_RSHIFT
:
1325 x
= PyNumber_InPlaceRshift(v
, w
);
1329 if (x
!= NULL
) continue;
1335 x
= PyNumber_InPlaceAnd(v
, w
);
1339 if (x
!= NULL
) continue;
1345 x
= PyNumber_InPlaceXor(v
, w
);
1349 if (x
!= NULL
) continue;
1355 x
= PyNumber_InPlaceOr(v
, w
);
1359 if (x
!= NULL
) continue;
1366 if ((opcode
-SLICE
) & 2)
1370 if ((opcode
-SLICE
) & 1)
1375 x
= apply_slice(u
, v
, w
);
1380 if (x
!= NULL
) continue;
1387 if ((opcode
-STORE_SLICE
) & 2)
1391 if ((opcode
-STORE_SLICE
) & 1)
1397 err
= assign_slice(u
, v
, w
, t
); /* u[v:w] = t */
1402 if (err
== 0) continue;
1405 case DELETE_SLICE
+0:
1406 case DELETE_SLICE
+1:
1407 case DELETE_SLICE
+2:
1408 case DELETE_SLICE
+3:
1409 if ((opcode
-DELETE_SLICE
) & 2)
1413 if ((opcode
-DELETE_SLICE
) & 1)
1418 err
= assign_slice(u
, v
, w
, (PyObject
*)NULL
);
1423 if (err
== 0) continue;
1432 err
= PyObject_SetItem(v
, w
, u
);
1436 if (err
== 0) continue;
1444 err
= PyObject_DelItem(v
, w
);
1447 if (err
== 0) continue;
1452 w
= PySys_GetObject("displayhook");
1454 PyErr_SetString(PyExc_RuntimeError
,
1455 "lost sys.displayhook");
1460 x
= Py_BuildValue("(O)", v
);
1465 w
= PyEval_CallObject(w
, x
);
1476 /* fall through to PRINT_ITEM */
1480 if (stream
== NULL
|| stream
== Py_None
) {
1481 w
= PySys_GetObject("stdout");
1483 PyErr_SetString(PyExc_RuntimeError
,
1488 if (w
!= NULL
&& PyFile_SoftSpace(w
, 0))
1489 err
= PyFile_WriteString(" ", w
);
1491 err
= PyFile_WriteObject(v
, w
, Py_PRINT_RAW
);
1493 /* XXX move into writeobject() ? */
1494 if (PyString_Check(v
)) {
1495 char *s
= PyString_AS_STRING(v
);
1496 int len
= PyString_GET_SIZE(v
);
1498 !isspace(Py_CHARMASK(s
[len
-1])) ||
1500 PyFile_SoftSpace(w
, 1);
1502 #ifdef Py_USING_UNICODE
1503 else if (PyUnicode_Check(v
)) {
1504 Py_UNICODE
*s
= PyUnicode_AS_UNICODE(v
);
1505 int len
= PyUnicode_GET_SIZE(v
);
1507 !Py_UNICODE_ISSPACE(s
[len
-1]) ||
1509 PyFile_SoftSpace(w
, 1);
1513 PyFile_SoftSpace(w
, 1);
1522 case PRINT_NEWLINE_TO
:
1524 /* fall through to PRINT_NEWLINE */
1527 if (stream
== NULL
|| stream
== Py_None
) {
1528 w
= PySys_GetObject("stdout");
1530 PyErr_SetString(PyExc_RuntimeError
,
1534 err
= PyFile_WriteString("\n", w
);
1536 PyFile_SoftSpace(w
, 0);
1544 default: switch (opcode
) {
1551 retval
= PyInt_FromLong(oparg
);
1559 u
= POP(); /* traceback */
1562 v
= POP(); /* value */
1565 w
= POP(); /* exc */
1566 case 0: /* Fallthrough */
1567 why
= do_raise(w
, v
, u
);
1570 PyErr_SetString(PyExc_SystemError
,
1571 "bad RAISE_VARARGS oparg");
1572 why
= WHY_EXCEPTION
;
1578 if ((x
= f
->f_locals
) == NULL
) {
1579 PyErr_SetString(PyExc_SystemError
,
1594 f
->f_stacktop
= stack_pointer
;
1604 err
= exec_statement(f
, u
, v
, w
);
1612 PyTryBlock
*b
= PyFrame_BlockPop(f
);
1613 while (STACK_LEVEL() > b
->b_level
) {
1622 if (PyInt_Check(v
)) {
1623 why
= (enum why_code
) PyInt_AS_LONG(v
);
1624 if (why
== WHY_RETURN
||
1626 why
== WHY_CONTINUE
)
1629 else if (PyString_Check(v
) || PyClass_Check(v
)) {
1632 PyErr_Restore(v
, w
, u
);
1636 else if (v
!= Py_None
) {
1637 PyErr_SetString(PyExc_SystemError
,
1638 "'finally' pops bad exception");
1639 why
= WHY_EXCEPTION
;
1649 x
= build_class(u
, v
, w
);
1657 w
= GETITEM(names
, oparg
);
1659 if ((x
= f
->f_locals
) == NULL
) {
1660 PyErr_Format(PyExc_SystemError
,
1661 "no locals found when storing %s",
1665 err
= PyDict_SetItem(x
, w
, v
);
1670 w
= GETITEM(names
, oparg
);
1671 if ((x
= f
->f_locals
) == NULL
) {
1672 PyErr_Format(PyExc_SystemError
,
1673 "no locals when deleting %s",
1677 if ((err
= PyDict_DelItem(x
, w
)) != 0)
1678 format_exc_check_arg(PyExc_NameError
,
1682 PREDICTED_WITH_ARG(UNPACK_SEQUENCE
);
1683 case UNPACK_SEQUENCE
:
1685 if (PyTuple_CheckExact(v
)) {
1686 if (PyTuple_Size(v
) != oparg
) {
1687 PyErr_SetString(PyExc_ValueError
,
1688 "unpack tuple of wrong size");
1689 why
= WHY_EXCEPTION
;
1692 for (; --oparg
>= 0; ) {
1693 w
= PyTuple_GET_ITEM(v
, oparg
);
1699 else if (PyList_CheckExact(v
)) {
1700 if (PyList_Size(v
) != oparg
) {
1701 PyErr_SetString(PyExc_ValueError
,
1702 "unpack list of wrong size");
1703 why
= WHY_EXCEPTION
;
1706 for (; --oparg
>= 0; ) {
1707 w
= PyList_GET_ITEM(v
, oparg
);
1713 else if (unpack_iterable(v
, oparg
,
1714 stack_pointer
+ oparg
))
1715 stack_pointer
+= oparg
;
1717 if (PyErr_ExceptionMatches(PyExc_TypeError
))
1718 PyErr_SetString(PyExc_TypeError
,
1719 "unpack non-sequence");
1720 why
= WHY_EXCEPTION
;
1726 w
= GETITEM(names
, oparg
);
1730 err
= PyObject_SetAttr(v
, w
, u
); /* v.w = u */
1736 w
= GETITEM(names
, oparg
);
1738 err
= PyObject_SetAttr(v
, w
, (PyObject
*)NULL
);
1744 w
= GETITEM(names
, oparg
);
1746 err
= PyDict_SetItem(f
->f_globals
, w
, v
);
1751 w
= GETITEM(names
, oparg
);
1752 if ((err
= PyDict_DelItem(f
->f_globals
, w
)) != 0)
1753 format_exc_check_arg(
1754 PyExc_NameError
, GLOBAL_NAME_ERROR_MSG
, w
);
1758 w
= GETITEM(names
, oparg
);
1759 if ((x
= f
->f_locals
) == NULL
) {
1760 PyErr_Format(PyExc_SystemError
,
1761 "no locals when loading %s",
1765 x
= PyDict_GetItem(x
, w
);
1767 x
= PyDict_GetItem(f
->f_globals
, w
);
1769 x
= PyDict_GetItem(f
->f_builtins
, w
);
1771 format_exc_check_arg(
1783 w
= GETITEM(names
, oparg
);
1784 if (PyString_CheckExact(w
)) {
1785 /* Inline the PyDict_GetItem() calls.
1786 WARNING: this is an extreme speed hack.
1787 Do not try this at home. */
1788 long hash
= ((PyStringObject
*)w
)->ob_shash
;
1791 d
= (PyDictObject
*)(f
->f_globals
);
1792 x
= d
->ma_lookup(d
, w
, hash
)->me_value
;
1798 d
= (PyDictObject
*)(f
->f_builtins
);
1799 x
= d
->ma_lookup(d
, w
, hash
)->me_value
;
1805 goto load_global_error
;
1808 /* This is the un-inlined version of the code above */
1809 x
= PyDict_GetItem(f
->f_globals
, w
);
1811 x
= PyDict_GetItem(f
->f_builtins
, w
);
1814 format_exc_check_arg(
1816 GLOBAL_NAME_ERROR_MSG
, w
);
1825 x
= GETLOCAL(oparg
);
1827 format_exc_check_arg(
1828 PyExc_UnboundLocalError
,
1829 UNBOUNDLOCAL_ERROR_MSG
,
1830 PyTuple_GetItem(co
->co_varnames
, oparg
)
1834 SETLOCAL(oparg
, NULL
);
1838 x
= freevars
[oparg
];
1844 x
= freevars
[oparg
];
1848 /* Don't stomp existing exception */
1849 if (PyErr_Occurred())
1851 if (oparg
< f
->f_ncells
) {
1852 v
= PyTuple_GetItem(co
->co_cellvars
,
1854 format_exc_check_arg(
1855 PyExc_UnboundLocalError
,
1856 UNBOUNDLOCAL_ERROR_MSG
,
1859 v
= PyTuple_GetItem(
1861 oparg
- f
->f_ncells
);
1862 format_exc_check_arg(
1864 UNBOUNDFREE_ERROR_MSG
,
1874 x
= freevars
[oparg
];
1880 x
= PyTuple_New(oparg
);
1882 for (; --oparg
>= 0;) {
1884 PyTuple_SET_ITEM(x
, oparg
, w
);
1892 x
= PyList_New(oparg
);
1894 for (; --oparg
>= 0;) {
1896 PyList_SET_ITEM(x
, oparg
, w
);
1906 if (x
!= NULL
) continue;
1910 w
= GETITEM(names
, oparg
);
1912 x
= PyObject_GetAttr(v
, w
);
1915 if (x
!= NULL
) continue;
1921 if (PyInt_CheckExact(w
) && PyInt_CheckExact(v
)) {
1922 /* INLINE: cmp(int, int) */
1925 a
= PyInt_AS_LONG(v
);
1926 b
= PyInt_AS_LONG(w
);
1928 case PyCmp_LT
: res
= a
< b
; break;
1929 case PyCmp_LE
: res
= a
<= b
; break;
1930 case PyCmp_EQ
: res
= a
== b
; break;
1931 case PyCmp_NE
: res
= a
!= b
; break;
1932 case PyCmp_GT
: res
= a
> b
; break;
1933 case PyCmp_GE
: res
= a
>= b
; break;
1934 case PyCmp_IS
: res
= v
== w
; break;
1935 case PyCmp_IS_NOT
: res
= v
!= w
; break;
1936 default: goto slow_compare
;
1938 x
= res
? Py_True
: Py_False
;
1943 x
= cmp_outcome(oparg
, v
, w
);
1948 if (x
== NULL
) break;
1949 PREDICT(JUMP_IF_FALSE
);
1950 PREDICT(JUMP_IF_TRUE
);
1954 w
= GETITEM(names
, oparg
);
1955 x
= PyDict_GetItemString(f
->f_builtins
, "__import__");
1957 PyErr_SetString(PyExc_ImportError
,
1958 "__import__ not found");
1962 w
= Py_BuildValue("(OOOO)",
1965 f
->f_locals
== NULL
?
1966 Py_None
: f
->f_locals
,
1974 x
= PyEval_CallObject(x
, w
);
1977 if (x
!= NULL
) continue;
1982 PyFrame_FastToLocals(f
);
1983 if ((x
= f
->f_locals
) == NULL
) {
1984 PyErr_SetString(PyExc_SystemError
,
1985 "no locals found during 'import *'");
1988 err
= import_all_from(x
, v
);
1989 PyFrame_LocalsToFast(f
, 0);
1991 if (err
== 0) continue;
1995 w
= GETITEM(names
, oparg
);
1997 x
= import_from(v
, w
);
1999 if (x
!= NULL
) continue;
2004 goto fast_next_opcode
;
2006 PREDICTED_WITH_ARG(JUMP_IF_FALSE
);
2011 goto fast_next_opcode
;
2013 if (w
== Py_False
) {
2015 goto fast_next_opcode
;
2017 err
= PyObject_IsTrue(w
);
2026 PREDICTED_WITH_ARG(JUMP_IF_TRUE
);
2029 if (w
== Py_False
) {
2031 goto fast_next_opcode
;
2035 goto fast_next_opcode
;
2037 err
= PyObject_IsTrue(w
);
2050 goto fast_next_opcode
;
2053 /* before: [obj]; after [getiter(obj)] */
2055 x
= PyObject_GetIter(v
);
2065 PREDICTED_WITH_ARG(FOR_ITER
);
2067 /* before: [iter]; after: [iter, iter()] *or* [] */
2072 PREDICT(STORE_FAST
);
2073 PREDICT(UNPACK_SEQUENCE
);
2076 if (!PyErr_Occurred()) {
2077 /* iterator ended normally */
2088 PyFrame_BlockSetup(f
, opcode
, INSTR_OFFSET() + oparg
,
2094 x
= call_function(&stack_pointer
, oparg
);
2100 case CALL_FUNCTION_VAR
:
2101 case CALL_FUNCTION_KW
:
2102 case CALL_FUNCTION_VAR_KW
:
2104 int na
= oparg
& 0xff;
2105 int nk
= (oparg
>>8) & 0xff;
2106 int flags
= (opcode
- CALL_FUNCTION
) & 3;
2107 int n
= na
+ 2 * nk
;
2108 PyObject
**pfunc
, *func
;
2110 if (flags
& CALL_FLAG_VAR
)
2112 if (flags
& CALL_FLAG_KW
)
2114 pfunc
= stack_pointer
- n
- 1;
2117 if (PyMethod_Check(func
)
2118 && PyMethod_GET_SELF(func
) != NULL
) {
2119 PyObject
*self
= PyMethod_GET_SELF(func
);
2121 func
= PyMethod_GET_FUNCTION(func
);
2129 x
= ext_do_call(func
, &stack_pointer
, flags
, na
, nk
);
2132 while (stack_pointer
> pfunc
) {
2143 v
= POP(); /* code object */
2144 x
= PyFunction_New(v
, f
->f_globals
);
2146 /* XXX Maybe this should be a separate opcode? */
2147 if (x
!= NULL
&& oparg
> 0) {
2148 v
= PyTuple_New(oparg
);
2154 while (--oparg
>= 0) {
2156 PyTuple_SET_ITEM(v
, oparg
, w
);
2158 err
= PyFunction_SetDefaults(x
, v
);
2167 v
= POP(); /* code object */
2168 x
= PyFunction_New(v
, f
->f_globals
);
2169 nfree
= PyCode_GetNumFree((PyCodeObject
*)v
);
2171 /* XXX Maybe this should be a separate opcode? */
2172 if (x
!= NULL
&& nfree
> 0) {
2173 v
= PyTuple_New(nfree
);
2179 while (--nfree
>= 0) {
2181 PyTuple_SET_ITEM(v
, nfree
, w
);
2183 err
= PyFunction_SetClosure(x
, v
);
2186 if (x
!= NULL
&& oparg
> 0) {
2187 v
= PyTuple_New(oparg
);
2193 while (--oparg
>= 0) {
2195 PyTuple_SET_ITEM(v
, oparg
, w
);
2197 err
= PyFunction_SetDefaults(x
, v
);
2211 x
= PySlice_New(u
, v
, w
);
2216 if (x
!= NULL
) continue;
2221 oparg
= oparg
<<16 | NEXTARG();
2222 goto dispatch_opcode
;
2226 "XXX lineno: %d, opcode: %d\n",
2227 PyCode_Addr2Line(f
->f_code
, f
->f_lasti
),
2229 PyErr_SetString(PyExc_SystemError
, "unknown opcode");
2230 why
= WHY_EXCEPTION
;
2241 /* Quickly continue if no error occurred */
2243 if (why
== WHY_NOT
) {
2244 if (err
== 0 && x
!= NULL
) {
2246 /* This check is expensive! */
2247 if (PyErr_Occurred())
2249 "XXX undetected error\n");
2252 continue; /* Normal, fast path */
2254 why
= WHY_EXCEPTION
;
2259 /* Double-check exception status */
2261 if (why
== WHY_EXCEPTION
|| why
== WHY_RERAISE
) {
2262 if (!PyErr_Occurred()) {
2263 PyErr_SetString(PyExc_SystemError
,
2264 "error return without exception set");
2265 why
= WHY_EXCEPTION
;
2270 /* This check is expensive! */
2271 if (PyErr_Occurred()) {
2273 "XXX undetected error (why=%d)\n",
2275 why
= WHY_EXCEPTION
;
2280 /* Log traceback info if this is a real exception */
2282 if (why
== WHY_EXCEPTION
) {
2283 PyTraceBack_Here(f
);
2285 if (tstate
->c_tracefunc
!= NULL
)
2286 call_exc_trace(tstate
->c_tracefunc
,
2287 tstate
->c_traceobj
, f
);
2290 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
2292 if (why
== WHY_RERAISE
)
2293 why
= WHY_EXCEPTION
;
2295 /* Unwind stacks if a (pseudo) exception occurred */
2297 while (why
!= WHY_NOT
&& why
!= WHY_YIELD
&& f
->f_iblock
> 0) {
2298 PyTryBlock
*b
= PyFrame_BlockPop(f
);
2300 if (b
->b_type
== SETUP_LOOP
&& why
== WHY_CONTINUE
) {
2301 /* For a continue inside a try block,
2302 don't pop the block for the loop. */
2303 PyFrame_BlockSetup(f
, b
->b_type
, b
->b_handler
,
2306 JUMPTO(PyInt_AS_LONG(retval
));
2311 while (STACK_LEVEL() > b
->b_level
) {
2315 if (b
->b_type
== SETUP_LOOP
&& why
== WHY_BREAK
) {
2317 JUMPTO(b
->b_handler
);
2320 if (b
->b_type
== SETUP_FINALLY
||
2321 (b
->b_type
== SETUP_EXCEPT
&&
2322 why
== WHY_EXCEPTION
)) {
2323 if (why
== WHY_EXCEPTION
) {
2324 PyObject
*exc
, *val
, *tb
;
2325 PyErr_Fetch(&exc
, &val
, &tb
);
2330 /* Make the raw exception data
2331 available to the handler,
2332 so a program can emulate the
2333 Python main loop. Don't do
2334 this for 'finally'. */
2335 if (b
->b_type
== SETUP_EXCEPT
) {
2336 PyErr_NormalizeException(
2338 set_exc_info(tstate
,
2350 if (why
== WHY_RETURN
||
2351 why
== WHY_CONTINUE
)
2353 v
= PyInt_FromLong((long)why
);
2357 JUMPTO(b
->b_handler
);
2360 } /* unwind stack */
2362 /* End the loop if we still have an error (or return) */
2369 if (why
!= WHY_YIELD
) {
2370 /* Pop remaining stack entries -- but when yielding */
2377 if (why
!= WHY_RETURN
&& why
!= WHY_YIELD
)
2380 if (tstate
->use_tracing
) {
2381 if (tstate
->c_tracefunc
2382 && (why
== WHY_RETURN
|| why
== WHY_YIELD
)) {
2383 if (call_trace(tstate
->c_tracefunc
,
2384 tstate
->c_traceobj
, f
,
2385 PyTrace_RETURN
, retval
)) {
2388 why
= WHY_EXCEPTION
;
2391 if (tstate
->c_profilefunc
) {
2392 if (why
== WHY_EXCEPTION
)
2393 call_trace_protected(tstate
->c_profilefunc
,
2394 tstate
->c_profileobj
, f
,
2396 else if (call_trace(tstate
->c_profilefunc
,
2397 tstate
->c_profileobj
, f
,
2398 PyTrace_RETURN
, retval
)) {
2401 why
= WHY_EXCEPTION
;
2406 reset_exc_info(tstate
);
2409 --tstate
->recursion_depth
;
2410 tstate
->frame
= f
->f_back
;
2416 PyEval_EvalCodeEx(PyCodeObject
*co
, PyObject
*globals
, PyObject
*locals
,
2417 PyObject
**args
, int argcount
, PyObject
**kws
, int kwcount
,
2418 PyObject
**defs
, int defcount
, PyObject
*closure
)
2420 register PyFrameObject
*f
;
2421 register PyObject
*retval
= NULL
;
2422 register PyObject
**fastlocals
, **freevars
;
2423 PyThreadState
*tstate
= PyThreadState_GET();
2426 if (globals
== NULL
) {
2427 PyErr_SetString(PyExc_SystemError
,
2428 "PyEval_EvalCodeEx: NULL globals");
2432 assert(globals
!= NULL
);
2433 f
= PyFrame_New(tstate
, co
, globals
, locals
);
2437 fastlocals
= f
->f_localsplus
;
2438 freevars
= f
->f_localsplus
+ f
->f_nlocals
;
2440 if (co
->co_argcount
> 0 ||
2441 co
->co_flags
& (CO_VARARGS
| CO_VARKEYWORDS
)) {
2444 PyObject
*kwdict
= NULL
;
2445 if (co
->co_flags
& CO_VARKEYWORDS
) {
2446 kwdict
= PyDict_New();
2449 i
= co
->co_argcount
;
2450 if (co
->co_flags
& CO_VARARGS
)
2452 SETLOCAL(i
, kwdict
);
2454 if (argcount
> co
->co_argcount
) {
2455 if (!(co
->co_flags
& CO_VARARGS
)) {
2456 PyErr_Format(PyExc_TypeError
,
2457 "%.200s() takes %s %d "
2458 "%sargument%s (%d given)",
2459 PyString_AsString(co
->co_name
),
2460 defcount
? "at most" : "exactly",
2462 kwcount
? "non-keyword " : "",
2463 co
->co_argcount
== 1 ? "" : "s",
2467 n
= co
->co_argcount
;
2469 for (i
= 0; i
< n
; i
++) {
2474 if (co
->co_flags
& CO_VARARGS
) {
2475 u
= PyTuple_New(argcount
- n
);
2478 SETLOCAL(co
->co_argcount
, u
);
2479 for (i
= n
; i
< argcount
; i
++) {
2482 PyTuple_SET_ITEM(u
, i
-n
, x
);
2485 for (i
= 0; i
< kwcount
; i
++) {
2486 PyObject
*keyword
= kws
[2*i
];
2487 PyObject
*value
= kws
[2*i
+ 1];
2489 if (keyword
== NULL
|| !PyString_Check(keyword
)) {
2490 PyErr_Format(PyExc_TypeError
,
2491 "%.200s() keywords must be strings",
2492 PyString_AsString(co
->co_name
));
2495 /* XXX slow -- speed up using dictionary? */
2496 for (j
= 0; j
< co
->co_argcount
; j
++) {
2497 PyObject
*nm
= PyTuple_GET_ITEM(
2498 co
->co_varnames
, j
);
2499 int cmp
= PyObject_RichCompareBool(
2500 keyword
, nm
, Py_EQ
);
2506 /* Check errors from Compare */
2507 if (PyErr_Occurred())
2509 if (j
>= co
->co_argcount
) {
2510 if (kwdict
== NULL
) {
2511 PyErr_Format(PyExc_TypeError
,
2512 "%.200s() got an unexpected "
2513 "keyword argument '%.400s'",
2514 PyString_AsString(co
->co_name
),
2515 PyString_AsString(keyword
));
2518 PyDict_SetItem(kwdict
, keyword
, value
);
2521 if (GETLOCAL(j
) != NULL
) {
2522 PyErr_Format(PyExc_TypeError
,
2523 "%.200s() got multiple "
2524 "values for keyword "
2525 "argument '%.400s'",
2526 PyString_AsString(co
->co_name
),
2527 PyString_AsString(keyword
));
2534 if (argcount
< co
->co_argcount
) {
2535 int m
= co
->co_argcount
- defcount
;
2536 for (i
= argcount
; i
< m
; i
++) {
2537 if (GETLOCAL(i
) == NULL
) {
2538 PyErr_Format(PyExc_TypeError
,
2539 "%.200s() takes %s %d "
2540 "%sargument%s (%d given)",
2541 PyString_AsString(co
->co_name
),
2542 ((co
->co_flags
& CO_VARARGS
) ||
2543 defcount
) ? "at least"
2545 m
, kwcount
? "non-keyword " : "",
2546 m
== 1 ? "" : "s", i
);
2554 for (; i
< defcount
; i
++) {
2555 if (GETLOCAL(m
+i
) == NULL
) {
2556 PyObject
*def
= defs
[i
];
2564 if (argcount
> 0 || kwcount
> 0) {
2565 PyErr_Format(PyExc_TypeError
,
2566 "%.200s() takes no arguments (%d given)",
2567 PyString_AsString(co
->co_name
),
2568 argcount
+ kwcount
);
2572 /* Allocate and initialize storage for cell vars, and copy free
2573 vars into frame. This isn't too efficient right now. */
2575 int i
= 0, j
= 0, nargs
, found
;
2576 char *cellname
, *argname
;
2579 nargs
= co
->co_argcount
;
2580 if (co
->co_flags
& CO_VARARGS
)
2582 if (co
->co_flags
& CO_VARKEYWORDS
)
2585 /* Check for cells that shadow args */
2586 for (i
= 0; i
< f
->f_ncells
&& j
< nargs
; ++i
) {
2587 cellname
= PyString_AS_STRING(
2588 PyTuple_GET_ITEM(co
->co_cellvars
, i
));
2591 argname
= PyString_AS_STRING(
2592 PyTuple_GET_ITEM(co
->co_varnames
, j
));
2593 if (strcmp(cellname
, argname
) == 0) {
2594 c
= PyCell_New(GETLOCAL(j
));
2597 GETLOCAL(f
->f_nlocals
+ i
) = c
;
2604 c
= PyCell_New(NULL
);
2607 SETLOCAL(f
->f_nlocals
+ i
, c
);
2610 /* Initialize any that are left */
2611 while (i
< f
->f_ncells
) {
2612 c
= PyCell_New(NULL
);
2615 SETLOCAL(f
->f_nlocals
+ i
, c
);
2619 if (f
->f_nfreevars
) {
2621 for (i
= 0; i
< f
->f_nfreevars
; ++i
) {
2622 PyObject
*o
= PyTuple_GET_ITEM(closure
, i
);
2624 freevars
[f
->f_ncells
+ i
] = o
;
2628 if (co
->co_flags
& CO_GENERATOR
) {
2629 /* Don't need to keep the reference to f_back, it will be set
2630 * when the generator is resumed. */
2631 Py_XDECREF(f
->f_back
);
2634 PCALL(PCALL_GENERATOR
);
2636 /* Create a new generator that owns the ready to run frame
2637 * and return that as the value. */
2641 retval
= eval_frame(f
);
2643 fail
: /* Jump here from prelude on failure */
2645 /* decref'ing the frame can cause __del__ methods to get invoked,
2646 which can call back into Python. While we're done with the
2647 current Python frame (f), the associated C stack is still in use,
2648 so recursion_depth must be boosted for the duration.
2650 assert(tstate
!= NULL
);
2651 ++tstate
->recursion_depth
;
2653 --tstate
->recursion_depth
;
2658 /* Implementation notes for set_exc_info() and reset_exc_info():
2660 - Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2661 'exc_traceback'. These always travel together.
2663 - tstate->curexc_ZZZ is the "hot" exception that is set by
2664 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2666 - Once an exception is caught by an except clause, it is transferred
2667 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2668 can pick it up. This is the primary task of set_exc_info().
2670 - Now let me explain the complicated dance with frame->f_exc_ZZZ.
2672 Long ago, when none of this existed, there were just a few globals:
2673 one set corresponding to the "hot" exception, and one set
2674 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2675 globals; they were simply stored as sys.exc_ZZZ. For backwards
2676 compatibility, they still are!) The problem was that in code like
2680 "something that may fail"
2681 except "some exception":
2682 "do something else first"
2683 "print the exception from sys.exc_ZZZ."
2685 if "do something else first" invoked something that raised and caught
2686 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2687 cause of subtle bugs. I fixed this by changing the semantics as
2690 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2693 - But initially, and as long as no exception is caught in a given
2694 frame, sys.exc_ZZZ will hold the last exception caught in the
2695 previous frame (or the frame before that, etc.).
2697 The first bullet fixed the bug in the above example. The second
2698 bullet was for backwards compatibility: it was (and is) common to
2699 have a function that is called when an exception is caught, and to
2700 have that function access the caught exception via sys.exc_ZZZ.
2701 (Example: traceback.print_exc()).
2703 At the same time I fixed the problem that sys.exc_ZZZ weren't
2704 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2705 but that's really a separate improvement.
2707 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2708 variables to what they were before the current frame was called. The
2709 set_exc_info() function saves them on the frame so that
2710 reset_exc_info() can restore them. The invariant is that
2711 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2712 exception (where "catching" an exception applies only to successful
2713 except clauses); and if the current frame ever caught an exception,
2714 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2715 at the start of the current frame.
2720 set_exc_info(PyThreadState
*tstate
,
2721 PyObject
*type
, PyObject
*value
, PyObject
*tb
)
2723 PyFrameObject
*frame
;
2724 PyObject
*tmp_type
, *tmp_value
, *tmp_tb
;
2726 frame
= tstate
->frame
;
2727 if (frame
->f_exc_type
== NULL
) {
2728 /* This frame didn't catch an exception before */
2729 /* Save previous exception of this thread in this frame */
2730 if (tstate
->exc_type
== NULL
) {
2732 tstate
->exc_type
= Py_None
;
2734 tmp_type
= frame
->f_exc_type
;
2735 tmp_value
= frame
->f_exc_value
;
2736 tmp_tb
= frame
->f_exc_traceback
;
2737 Py_XINCREF(tstate
->exc_type
);
2738 Py_XINCREF(tstate
->exc_value
);
2739 Py_XINCREF(tstate
->exc_traceback
);
2740 frame
->f_exc_type
= tstate
->exc_type
;
2741 frame
->f_exc_value
= tstate
->exc_value
;
2742 frame
->f_exc_traceback
= tstate
->exc_traceback
;
2743 Py_XDECREF(tmp_type
);
2744 Py_XDECREF(tmp_value
);
2747 /* Set new exception for this thread */
2748 tmp_type
= tstate
->exc_type
;
2749 tmp_value
= tstate
->exc_value
;
2750 tmp_tb
= tstate
->exc_traceback
;
2754 tstate
->exc_type
= type
;
2755 tstate
->exc_value
= value
;
2756 tstate
->exc_traceback
= tb
;
2757 Py_XDECREF(tmp_type
);
2758 Py_XDECREF(tmp_value
);
2760 /* For b/w compatibility */
2761 PySys_SetObject("exc_type", type
);
2762 PySys_SetObject("exc_value", value
);
2763 PySys_SetObject("exc_traceback", tb
);
2767 reset_exc_info(PyThreadState
*tstate
)
2769 PyFrameObject
*frame
;
2770 PyObject
*tmp_type
, *tmp_value
, *tmp_tb
;
2771 frame
= tstate
->frame
;
2772 if (frame
->f_exc_type
!= NULL
) {
2773 /* This frame caught an exception */
2774 tmp_type
= tstate
->exc_type
;
2775 tmp_value
= tstate
->exc_value
;
2776 tmp_tb
= tstate
->exc_traceback
;
2777 Py_XINCREF(frame
->f_exc_type
);
2778 Py_XINCREF(frame
->f_exc_value
);
2779 Py_XINCREF(frame
->f_exc_traceback
);
2780 tstate
->exc_type
= frame
->f_exc_type
;
2781 tstate
->exc_value
= frame
->f_exc_value
;
2782 tstate
->exc_traceback
= frame
->f_exc_traceback
;
2783 Py_XDECREF(tmp_type
);
2784 Py_XDECREF(tmp_value
);
2786 /* For b/w compatibility */
2787 PySys_SetObject("exc_type", frame
->f_exc_type
);
2788 PySys_SetObject("exc_value", frame
->f_exc_value
);
2789 PySys_SetObject("exc_traceback", frame
->f_exc_traceback
);
2791 tmp_type
= frame
->f_exc_type
;
2792 tmp_value
= frame
->f_exc_value
;
2793 tmp_tb
= frame
->f_exc_traceback
;
2794 frame
->f_exc_type
= NULL
;
2795 frame
->f_exc_value
= NULL
;
2796 frame
->f_exc_traceback
= NULL
;
2797 Py_XDECREF(tmp_type
);
2798 Py_XDECREF(tmp_value
);
2802 /* Logic for the raise statement (too complicated for inlining).
2803 This *consumes* a reference count to each of its arguments. */
2804 static enum why_code
2805 do_raise(PyObject
*type
, PyObject
*value
, PyObject
*tb
)
2809 PyThreadState
*tstate
= PyThreadState_Get();
2810 type
= tstate
->exc_type
== NULL
? Py_None
: tstate
->exc_type
;
2811 value
= tstate
->exc_value
;
2812 tb
= tstate
->exc_traceback
;
2818 /* We support the following forms of raise:
2819 raise <class>, <classinstance>
2820 raise <class>, <argument tuple>
2822 raise <class>, <argument>
2823 raise <classinstance>, None
2824 raise <string>, <object>
2825 raise <string>, None
2827 An omitted second argument is the same as None.
2829 In addition, raise <tuple>, <anything> is the same as
2830 raising the tuple's first item (and it better have one!);
2831 this rule is applied recursively.
2833 Finally, an optional third argument can be supplied, which
2834 gives the traceback to be substituted (useful when
2835 re-raising an exception after examining it). */
2837 /* First, check the traceback argument, replacing None with
2839 if (tb
== Py_None
) {
2843 else if (tb
!= NULL
&& !PyTraceBack_Check(tb
)) {
2844 PyErr_SetString(PyExc_TypeError
,
2845 "raise: arg 3 must be a traceback or None");
2849 /* Next, replace a missing value with None */
2850 if (value
== NULL
) {
2855 /* Next, repeatedly, replace a tuple exception with its first item */
2856 while (PyTuple_Check(type
) && PyTuple_Size(type
) > 0) {
2857 PyObject
*tmp
= type
;
2858 type
= PyTuple_GET_ITEM(type
, 0);
2863 if (PyString_CheckExact(type
))
2864 /* Raising builtin string is deprecated but still allowed --
2865 * do nothing. Raising an instance of a new-style str
2866 * subclass is right out. */
2867 PyErr_Warn(PyExc_PendingDeprecationWarning
,
2868 "raising a string exception is deprecated");
2870 else if (PyClass_Check(type
))
2871 PyErr_NormalizeException(&type
, &value
, &tb
);
2873 else if (PyInstance_Check(type
)) {
2874 /* Raising an instance. The value should be a dummy. */
2875 if (value
!= Py_None
) {
2876 PyErr_SetString(PyExc_TypeError
,
2877 "instance exception may not have a separate value");
2881 /* Normalize to raise <class>, <instance> */
2884 type
= (PyObject
*) ((PyInstanceObject
*)type
)->in_class
;
2889 /* Not something you can raise. You get an exception
2890 anyway, just not what you specified :-) */
2891 PyErr_Format(PyExc_TypeError
,
2892 "exceptions must be classes, instances, or "
2893 "strings (deprecated), not %s",
2894 type
->ob_type
->tp_name
);
2897 PyErr_Restore(type
, value
, tb
);
2899 return WHY_EXCEPTION
;
2906 return WHY_EXCEPTION
;
2909 /* Iterate v argcnt times and store the results on the stack (via decreasing
2910 sp). Return 1 for success, 0 if error. */
2913 unpack_iterable(PyObject
*v
, int argcnt
, PyObject
**sp
)
2916 PyObject
*it
; /* iter(v) */
2921 it
= PyObject_GetIter(v
);
2925 for (; i
< argcnt
; i
++) {
2926 w
= PyIter_Next(it
);
2928 /* Iterator done, via error or exhaustion. */
2929 if (!PyErr_Occurred()) {
2930 PyErr_Format(PyExc_ValueError
,
2931 "need more than %d value%s to unpack",
2932 i
, i
== 1 ? "" : "s");
2939 /* We better have exhausted the iterator now. */
2940 w
= PyIter_Next(it
);
2942 if (PyErr_Occurred())
2948 PyErr_SetString(PyExc_ValueError
, "too many values to unpack");
2951 for (; i
> 0; i
--, sp
++)
2960 prtrace(PyObject
*v
, char *str
)
2963 if (PyObject_Print(v
, stdout
, 0) != 0)
2964 PyErr_Clear(); /* Don't know what else to do */
2971 call_exc_trace(Py_tracefunc func
, PyObject
*self
, PyFrameObject
*f
)
2973 PyObject
*type
, *value
, *traceback
, *arg
;
2975 PyErr_Fetch(&type
, &value
, &traceback
);
2976 if (value
== NULL
) {
2980 arg
= Py_BuildValue("(OOO)", type
, value
, traceback
);
2982 PyErr_Restore(type
, value
, traceback
);
2985 err
= call_trace(func
, self
, f
, PyTrace_EXCEPTION
, arg
);
2988 PyErr_Restore(type
, value
, traceback
);
2992 Py_XDECREF(traceback
);
2997 call_trace_protected(Py_tracefunc func
, PyObject
*obj
, PyFrameObject
*frame
,
3000 PyObject
*type
, *value
, *traceback
;
3002 PyErr_Fetch(&type
, &value
, &traceback
);
3003 err
= call_trace(func
, obj
, frame
, what
, NULL
);
3005 PyErr_Restore(type
, value
, traceback
);
3009 Py_XDECREF(traceback
);
3014 call_trace(Py_tracefunc func
, PyObject
*obj
, PyFrameObject
*frame
,
3015 int what
, PyObject
*arg
)
3017 register PyThreadState
*tstate
= frame
->f_tstate
;
3019 if (tstate
->tracing
)
3022 tstate
->use_tracing
= 0;
3023 result
= func(obj
, frame
, what
, arg
);
3024 tstate
->use_tracing
= ((tstate
->c_tracefunc
!= NULL
)
3025 || (tstate
->c_profilefunc
!= NULL
));
3031 _PyEval_CallTracing(PyObject
*func
, PyObject
*args
)
3033 PyFrameObject
*frame
= PyEval_GetFrame();
3034 PyThreadState
*tstate
= frame
->f_tstate
;
3035 int save_tracing
= tstate
->tracing
;
3036 int save_use_tracing
= tstate
->use_tracing
;
3039 tstate
->tracing
= 0;
3040 tstate
->use_tracing
= ((tstate
->c_tracefunc
!= NULL
)
3041 || (tstate
->c_profilefunc
!= NULL
));
3042 result
= PyObject_Call(func
, args
, NULL
);
3043 tstate
->tracing
= save_tracing
;
3044 tstate
->use_tracing
= save_use_tracing
;
3049 maybe_call_line_trace(Py_tracefunc func
, PyObject
*obj
,
3050 PyFrameObject
*frame
, int *instr_lb
, int *instr_ub
)
3052 /* The theory of SET_LINENO-less tracing.
3054 In a nutshell, we use the co_lnotab field of the code object
3055 to tell when execution has moved onto a different line.
3057 As mentioned above, the basic idea is so set things up so
3060 *instr_lb <= frame->f_lasti < *instr_ub
3062 is true so long as execution does not change lines.
3064 This is all fairly simple. Digging the information out of
3065 co_lnotab takes some work, but is conceptually clear.
3067 Somewhat harder to explain is why we don't *always* call the
3068 line trace function when the above test fails.
3078 which compiles to this:
3081 3 JUMP_IF_FALSE 9 (to 15)
3084 3 7 LOAD_CONST 1 (1)
3087 12 JUMP_FORWARD 6 (to 21)
3090 5 16 LOAD_CONST 2 (2)
3093 >> 21 LOAD_CONST 0 (None)
3096 If 'a' is false, execution will jump to instruction at offset
3097 15 and the co_lnotab will claim that execution has moved to
3098 line 3. This is at best misleading. In this case we could
3099 associate the POP_TOP with line 4, but that doesn't make
3100 sense in all cases (I think).
3102 What we do is only call the line trace function if the co_lnotab
3103 indicates we have jumped to the *start* of a line, i.e. if the
3104 current instruction offset matches the offset given for the
3105 start of a line by the co_lnotab.
3107 This also takes care of the situation where 'a' is true.
3108 Execution will jump from instruction offset 12 to offset 21.
3109 Then the co_lnotab would imply that execution has moved to line
3110 5, which is again misleading.
3112 Why do we set f_lineno when tracing? Well, consider the code
3113 above when 'a' is true. If stepping through this with 'n' in
3114 pdb, you would stop at line 1 with a "call" type event, then
3115 line events on lines 2 and 3, then a "return" type event -- but
3116 you would be shown line 5 during this event. This is a change
3117 from the behaviour in 2.2 and before, and I've found it
3118 confusing in practice. By setting and using f_lineno when
3119 tracing, one can report a line number different from that
3120 suggested by f_lasti on this one occasion where it's desirable.
3125 if ((frame
->f_lasti
< *instr_lb
|| frame
->f_lasti
>= *instr_ub
)) {
3126 PyCodeObject
* co
= frame
->f_code
;
3127 int size
, addr
, line
;
3130 size
= PyString_GET_SIZE(co
->co_lnotab
) / 2;
3131 p
= (unsigned char*)PyString_AS_STRING(co
->co_lnotab
);
3134 line
= co
->co_firstlineno
;
3136 /* possible optimization: if f->f_lasti == instr_ub
3137 (likely to be a common case) then we already know
3138 instr_lb -- if we stored the matching value of p
3139 somwhere we could skip the first while loop. */
3141 /* see comments in compile.c for the description of
3142 co_lnotab. A point to remember: increments to p
3143 should come in pairs -- although we don't care about
3144 the line increments here, treating them as byte
3145 increments gets confusing, to say the least. */
3148 if (addr
+ *p
> frame
->f_lasti
)
3151 if (*p
) *instr_lb
= addr
;
3156 if (addr
== frame
->f_lasti
) {
3157 frame
->f_lineno
= line
;
3158 result
= call_trace(func
, obj
, frame
,
3159 PyTrace_LINE
, Py_None
);
3163 while (--size
>= 0) {
3171 *instr_ub
= INT_MAX
;
3179 PyEval_SetProfile(Py_tracefunc func
, PyObject
*arg
)
3181 PyThreadState
*tstate
= PyThreadState_Get();
3182 PyObject
*temp
= tstate
->c_profileobj
;
3184 tstate
->c_profilefunc
= NULL
;
3185 tstate
->c_profileobj
= NULL
;
3186 tstate
->use_tracing
= tstate
->c_tracefunc
!= NULL
;
3188 tstate
->c_profilefunc
= func
;
3189 tstate
->c_profileobj
= arg
;
3190 tstate
->use_tracing
= (func
!= NULL
) || (tstate
->c_tracefunc
!= NULL
);
3194 PyEval_SetTrace(Py_tracefunc func
, PyObject
*arg
)
3196 PyThreadState
*tstate
= PyThreadState_Get();
3197 PyObject
*temp
= tstate
->c_traceobj
;
3199 tstate
->c_tracefunc
= NULL
;
3200 tstate
->c_traceobj
= NULL
;
3201 tstate
->use_tracing
= tstate
->c_profilefunc
!= NULL
;
3203 tstate
->c_tracefunc
= func
;
3204 tstate
->c_traceobj
= arg
;
3205 tstate
->use_tracing
= ((func
!= NULL
)
3206 || (tstate
->c_profilefunc
!= NULL
));
3210 PyEval_GetBuiltins(void)
3212 PyFrameObject
*current_frame
= PyEval_GetFrame();
3213 if (current_frame
== NULL
)
3214 return PyThreadState_Get()->interp
->builtins
;
3216 return current_frame
->f_builtins
;
3220 PyEval_GetLocals(void)
3222 PyFrameObject
*current_frame
= PyEval_GetFrame();
3223 if (current_frame
== NULL
)
3225 PyFrame_FastToLocals(current_frame
);
3226 return current_frame
->f_locals
;
3230 PyEval_GetGlobals(void)
3232 PyFrameObject
*current_frame
= PyEval_GetFrame();
3233 if (current_frame
== NULL
)
3236 return current_frame
->f_globals
;
3240 PyEval_GetFrame(void)
3242 PyThreadState
*tstate
= PyThreadState_Get();
3243 return _PyThreadState_GetFrame(tstate
);
3247 PyEval_GetRestricted(void)
3249 PyFrameObject
*current_frame
= PyEval_GetFrame();
3250 return current_frame
== NULL
? 0 : current_frame
->f_restricted
;
3254 PyEval_MergeCompilerFlags(PyCompilerFlags
*cf
)
3256 PyFrameObject
*current_frame
= PyEval_GetFrame();
3257 int result
= cf
->cf_flags
!= 0;
3259 if (current_frame
!= NULL
) {
3260 const int codeflags
= current_frame
->f_code
->co_flags
;
3261 const int compilerflags
= codeflags
& PyCF_MASK
;
3262 if (compilerflags
) {
3264 cf
->cf_flags
|= compilerflags
;
3266 #if 0 /* future keyword */
3267 if (codeflags
& CO_GENERATOR_ALLOWED
) {
3269 cf
->cf_flags
|= CO_GENERATOR_ALLOWED
;
3279 PyObject
*f
= PySys_GetObject("stdout");
3282 if (!PyFile_SoftSpace(f
, 0))
3284 return PyFile_WriteString("\n", f
);
3288 /* External interface to call any callable object.
3289 The arg must be a tuple or NULL. */
3291 #undef PyEval_CallObject
3292 /* for backward compatibility: export this interface */
3295 PyEval_CallObject(PyObject
*func
, PyObject
*arg
)
3297 return PyEval_CallObjectWithKeywords(func
, arg
, (PyObject
*)NULL
);
3299 #define PyEval_CallObject(func,arg) \
3300 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
3303 PyEval_CallObjectWithKeywords(PyObject
*func
, PyObject
*arg
, PyObject
*kw
)
3308 arg
= PyTuple_New(0);
3309 else if (!PyTuple_Check(arg
)) {
3310 PyErr_SetString(PyExc_TypeError
,
3311 "argument list must be a tuple");
3317 if (kw
!= NULL
&& !PyDict_Check(kw
)) {
3318 PyErr_SetString(PyExc_TypeError
,
3319 "keyword list must be a dictionary");
3324 result
= PyObject_Call(func
, arg
, kw
);
3330 PyEval_GetFuncName(PyObject
*func
)
3332 if (PyMethod_Check(func
))
3333 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func
));
3334 else if (PyFunction_Check(func
))
3335 return PyString_AsString(((PyFunctionObject
*)func
)->func_name
);
3336 else if (PyCFunction_Check(func
))
3337 return ((PyCFunctionObject
*)func
)->m_ml
->ml_name
;
3338 else if (PyClass_Check(func
))
3339 return PyString_AsString(((PyClassObject
*)func
)->cl_name
);
3340 else if (PyInstance_Check(func
)) {
3341 return PyString_AsString(
3342 ((PyInstanceObject
*)func
)->in_class
->cl_name
);
3344 return func
->ob_type
->tp_name
;
3349 PyEval_GetFuncDesc(PyObject
*func
)
3351 if (PyMethod_Check(func
))
3353 else if (PyFunction_Check(func
))
3355 else if (PyCFunction_Check(func
))
3357 else if (PyClass_Check(func
))
3358 return " constructor";
3359 else if (PyInstance_Check(func
)) {
3366 #define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
3369 err_args(PyObject
*func
, int flags
, int nargs
)
3371 if (flags
& METH_NOARGS
)
3372 PyErr_Format(PyExc_TypeError
,
3373 "%.200s() takes no arguments (%d given)",
3374 ((PyCFunctionObject
*)func
)->m_ml
->ml_name
,
3377 PyErr_Format(PyExc_TypeError
,
3378 "%.200s() takes exactly one argument (%d given)",
3379 ((PyCFunctionObject
*)func
)->m_ml
->ml_name
,
3384 call_function(PyObject
***pp_stack
, int oparg
)
3386 int na
= oparg
& 0xff;
3387 int nk
= (oparg
>>8) & 0xff;
3388 int n
= na
+ 2 * nk
;
3389 PyObject
**pfunc
= (*pp_stack
) - n
- 1;
3390 PyObject
*func
= *pfunc
;
3393 /* Always dispatch PyCFunction first, because these are
3394 presumed to be the most frequent callable object.
3396 if (PyCFunction_Check(func
) && nk
== 0) {
3397 int flags
= PyCFunction_GET_FLAGS(func
);
3398 PCALL(PCALL_CFUNCTION
);
3399 if (flags
& (METH_NOARGS
| METH_O
)) {
3400 PyCFunction meth
= PyCFunction_GET_FUNCTION(func
);
3401 PyObject
*self
= PyCFunction_GET_SELF(func
);
3402 if (flags
& METH_NOARGS
&& na
== 0)
3403 x
= (*meth
)(self
, NULL
);
3404 else if (flags
& METH_O
&& na
== 1) {
3405 PyObject
*arg
= EXT_POP(*pp_stack
);
3406 x
= (*meth
)(self
, arg
);
3410 err_args(func
, flags
, na
);
3416 callargs
= load_args(pp_stack
, na
);
3417 x
= PyCFunction_Call(func
, callargs
, NULL
);
3418 Py_XDECREF(callargs
);
3421 if (PyMethod_Check(func
) && PyMethod_GET_SELF(func
) != NULL
) {
3422 /* optimize access to bound methods */
3423 PyObject
*self
= PyMethod_GET_SELF(func
);
3424 PCALL(PCALL_METHOD
);
3425 PCALL(PCALL_BOUND_METHOD
);
3427 func
= PyMethod_GET_FUNCTION(func
);
3435 if (PyFunction_Check(func
))
3436 x
= fast_function(func
, pp_stack
, n
, na
, nk
);
3438 x
= do_call(func
, pp_stack
, na
, nk
);
3442 /* What does this do? */
3443 while ((*pp_stack
) > pfunc
) {
3444 w
= EXT_POP(*pp_stack
);
3451 /* The fast_function() function optimize calls for which no argument
3452 tuple is necessary; the objects are passed directly from the stack.
3453 For the simplest case -- a function that takes only positional
3454 arguments and is called with only positional arguments -- it
3455 inlines the most primitive frame setup code from
3456 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3457 done before evaluating the frame.
3461 fast_function(PyObject
*func
, PyObject
***pp_stack
, int n
, int na
, int nk
)
3463 PyCodeObject
*co
= (PyCodeObject
*)PyFunction_GET_CODE(func
);
3464 PyObject
*globals
= PyFunction_GET_GLOBALS(func
);
3465 PyObject
*argdefs
= PyFunction_GET_DEFAULTS(func
);
3466 PyObject
**d
= NULL
;
3469 PCALL(PCALL_FUNCTION
);
3470 PCALL(PCALL_FAST_FUNCTION
);
3471 if (argdefs
== NULL
&& co
->co_argcount
== n
&&
3472 co
->co_flags
== (CO_OPTIMIZED
| CO_NEWLOCALS
| CO_NOFREE
)) {
3474 PyObject
*retval
= NULL
;
3475 PyThreadState
*tstate
= PyThreadState_GET();
3476 PyObject
**fastlocals
, **stack
;
3479 PCALL(PCALL_FASTER_FUNCTION
);
3480 assert(globals
!= NULL
);
3481 /* XXX Perhaps we should create a specialized
3482 PyFrame_New() that doesn't take locals, but does
3483 take builtins without sanity checking them.
3485 f
= PyFrame_New(tstate
, co
, globals
, NULL
);
3489 fastlocals
= f
->f_localsplus
;
3490 stack
= (*pp_stack
) - n
;
3492 for (i
= 0; i
< n
; i
++) {
3494 fastlocals
[i
] = *stack
++;
3496 retval
= eval_frame(f
);
3497 assert(tstate
!= NULL
);
3498 ++tstate
->recursion_depth
;
3500 --tstate
->recursion_depth
;
3503 if (argdefs
!= NULL
) {
3504 d
= &PyTuple_GET_ITEM(argdefs
, 0);
3505 nd
= ((PyTupleObject
*)argdefs
)->ob_size
;
3507 return PyEval_EvalCodeEx(co
, globals
,
3508 (PyObject
*)NULL
, (*pp_stack
)-n
, na
,
3509 (*pp_stack
)-2*nk
, nk
, d
, nd
,
3510 PyFunction_GET_CLOSURE(func
));
3514 update_keyword_args(PyObject
*orig_kwdict
, int nk
, PyObject
***pp_stack
,
3517 PyObject
*kwdict
= NULL
;
3518 if (orig_kwdict
== NULL
)
3519 kwdict
= PyDict_New();
3521 kwdict
= PyDict_Copy(orig_kwdict
);
3522 Py_DECREF(orig_kwdict
);
3528 PyObject
*value
= EXT_POP(*pp_stack
);
3529 PyObject
*key
= EXT_POP(*pp_stack
);
3530 if (PyDict_GetItem(kwdict
, key
) != NULL
) {
3531 PyErr_Format(PyExc_TypeError
,
3532 "%.200s%s got multiple values "
3533 "for keyword argument '%.200s'",
3534 PyEval_GetFuncName(func
),
3535 PyEval_GetFuncDesc(func
),
3536 PyString_AsString(key
));
3542 err
= PyDict_SetItem(kwdict
, key
, value
);
3554 update_star_args(int nstack
, int nstar
, PyObject
*stararg
,
3555 PyObject
***pp_stack
)
3557 PyObject
*callargs
, *w
;
3559 callargs
= PyTuple_New(nstack
+ nstar
);
3560 if (callargs
== NULL
) {
3565 for (i
= 0; i
< nstar
; i
++) {
3566 PyObject
*a
= PyTuple_GET_ITEM(stararg
, i
);
3568 PyTuple_SET_ITEM(callargs
, nstack
+ i
, a
);
3571 while (--nstack
>= 0) {
3572 w
= EXT_POP(*pp_stack
);
3573 PyTuple_SET_ITEM(callargs
, nstack
, w
);
3579 load_args(PyObject
***pp_stack
, int na
)
3581 PyObject
*args
= PyTuple_New(na
);
3587 w
= EXT_POP(*pp_stack
);
3588 PyTuple_SET_ITEM(args
, na
, w
);
3594 do_call(PyObject
*func
, PyObject
***pp_stack
, int na
, int nk
)
3596 PyObject
*callargs
= NULL
;
3597 PyObject
*kwdict
= NULL
;
3598 PyObject
*result
= NULL
;
3601 kwdict
= update_keyword_args(NULL
, nk
, pp_stack
, func
);
3605 callargs
= load_args(pp_stack
, na
);
3606 if (callargs
== NULL
)
3609 /* At this point, we have to look at the type of func to
3610 update the call stats properly. Do it here so as to avoid
3611 exposing the call stats machinery outside ceval.c
3613 if (PyFunction_Check(func
))
3614 PCALL(PCALL_FUNCTION
);
3615 else if (PyMethod_Check(func
))
3616 PCALL(PCALL_METHOD
);
3617 else if (PyType_Check(func
))
3622 result
= PyObject_Call(func
, callargs
, kwdict
);
3624 Py_XDECREF(callargs
);
3630 ext_do_call(PyObject
*func
, PyObject
***pp_stack
, int flags
, int na
, int nk
)
3633 PyObject
*callargs
= NULL
;
3634 PyObject
*stararg
= NULL
;
3635 PyObject
*kwdict
= NULL
;
3636 PyObject
*result
= NULL
;
3638 if (flags
& CALL_FLAG_KW
) {
3639 kwdict
= EXT_POP(*pp_stack
);
3640 if (!(kwdict
&& PyDict_Check(kwdict
))) {
3641 PyErr_Format(PyExc_TypeError
,
3642 "%s%s argument after ** "
3643 "must be a dictionary",
3644 PyEval_GetFuncName(func
),
3645 PyEval_GetFuncDesc(func
));
3649 if (flags
& CALL_FLAG_VAR
) {
3650 stararg
= EXT_POP(*pp_stack
);
3651 if (!PyTuple_Check(stararg
)) {
3653 t
= PySequence_Tuple(stararg
);
3655 if (PyErr_ExceptionMatches(PyExc_TypeError
)) {
3656 PyErr_Format(PyExc_TypeError
,
3657 "%s%s argument after * "
3658 "must be a sequence",
3659 PyEval_GetFuncName(func
),
3660 PyEval_GetFuncDesc(func
));
3667 nstar
= PyTuple_GET_SIZE(stararg
);
3670 kwdict
= update_keyword_args(kwdict
, nk
, pp_stack
, func
);
3674 callargs
= update_star_args(na
, nstar
, stararg
, pp_stack
);
3675 if (callargs
== NULL
)
3678 /* At this point, we have to look at the type of func to
3679 update the call stats properly. Do it here so as to avoid
3680 exposing the call stats machinery outside ceval.c
3682 if (PyFunction_Check(func
))
3683 PCALL(PCALL_FUNCTION
);
3684 else if (PyMethod_Check(func
))
3685 PCALL(PCALL_METHOD
);
3686 else if (PyType_Check(func
))
3691 result
= PyObject_Call(func
, callargs
, kwdict
);
3693 Py_XDECREF(callargs
);
3695 Py_XDECREF(stararg
);
3699 #define SLICE_ERROR_MSG \
3700 "standard sequence type does not support step size other than one"
3702 /* Extract a slice index from a PyInt or PyLong, and store in *pi.
3703 Silently reduce values larger than INT_MAX to INT_MAX, and silently
3704 boost values less than -INT_MAX to 0. Return 0 on error, 1 on success.
3706 /* Note: If v is NULL, return success without storing into *pi. This
3707 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3708 called by the SLICE opcode with v and/or w equal to NULL.
3711 _PyEval_SliceIndex(PyObject
*v
, int *pi
)
3715 if (PyInt_Check(v
)) {
3716 x
= PyInt_AsLong(v
);
3717 } else if (PyLong_Check(v
)) {
3718 x
= PyLong_AsLong(v
);
3719 if (x
==-1 && PyErr_Occurred()) {
3720 PyObject
*long_zero
;
3723 if (!PyErr_ExceptionMatches(
3724 PyExc_OverflowError
)) {
3725 /* It's not an overflow error, so just
3730 /* Clear the OverflowError */
3733 /* It's an overflow error, so we need to
3734 check the sign of the long integer,
3735 set the value to INT_MAX or -INT_MAX,
3736 and clear the error. */
3738 /* Create a long integer with a value of 0 */
3739 long_zero
= PyLong_FromLong(0L);
3740 if (long_zero
== NULL
)
3744 cmp
= PyObject_RichCompareBool(v
, long_zero
,
3746 Py_DECREF(long_zero
);
3755 PyErr_SetString(PyExc_TypeError
,
3756 "slice indices must be integers");
3759 /* Truncate -- very long indices are truncated anyway */
3762 else if (x
< -INT_MAX
)
3770 #define ISINT(x) ((x) == NULL || PyInt_Check(x) || PyLong_Check(x))
3773 apply_slice(PyObject
*u
, PyObject
*v
, PyObject
*w
) /* return u[v:w] */
3775 PyTypeObject
*tp
= u
->ob_type
;
3776 PySequenceMethods
*sq
= tp
->tp_as_sequence
;
3778 if (sq
&& sq
->sq_slice
&& ISINT(v
) && ISINT(w
)) {
3779 int ilow
= 0, ihigh
= INT_MAX
;
3780 if (!_PyEval_SliceIndex(v
, &ilow
))
3782 if (!_PyEval_SliceIndex(w
, &ihigh
))
3784 return PySequence_GetSlice(u
, ilow
, ihigh
);
3787 PyObject
*slice
= PySlice_New(v
, w
, NULL
);
3788 if (slice
!= NULL
) {
3789 PyObject
*res
= PyObject_GetItem(u
, slice
);
3799 assign_slice(PyObject
*u
, PyObject
*v
, PyObject
*w
, PyObject
*x
)
3802 PyTypeObject
*tp
= u
->ob_type
;
3803 PySequenceMethods
*sq
= tp
->tp_as_sequence
;
3805 if (sq
&& sq
->sq_slice
&& ISINT(v
) && ISINT(w
)) {
3806 int ilow
= 0, ihigh
= INT_MAX
;
3807 if (!_PyEval_SliceIndex(v
, &ilow
))
3809 if (!_PyEval_SliceIndex(w
, &ihigh
))
3812 return PySequence_DelSlice(u
, ilow
, ihigh
);
3814 return PySequence_SetSlice(u
, ilow
, ihigh
, x
);
3817 PyObject
*slice
= PySlice_New(v
, w
, NULL
);
3818 if (slice
!= NULL
) {
3821 res
= PyObject_SetItem(u
, slice
, x
);
3823 res
= PyObject_DelItem(u
, slice
);
3833 cmp_outcome(int op
, register PyObject
*v
, register PyObject
*w
)
3844 res
= PySequence_Contains(w
, v
);
3849 res
= PySequence_Contains(w
, v
);
3854 case PyCmp_EXC_MATCH
:
3855 res
= PyErr_GivenExceptionMatches(v
, w
);
3858 return PyObject_RichCompare(v
, w
, op
);
3860 v
= res
? Py_True
: Py_False
;
3866 import_from(PyObject
*v
, PyObject
*name
)
3870 x
= PyObject_GetAttr(v
, name
);
3871 if (x
== NULL
&& PyErr_ExceptionMatches(PyExc_AttributeError
)) {
3872 PyErr_Format(PyExc_ImportError
,
3873 "cannot import name %.230s",
3874 PyString_AsString(name
));
3880 import_all_from(PyObject
*locals
, PyObject
*v
)
3882 PyObject
*all
= PyObject_GetAttrString(v
, "__all__");
3883 PyObject
*dict
, *name
, *value
;
3884 int skip_leading_underscores
= 0;
3888 if (!PyErr_ExceptionMatches(PyExc_AttributeError
))
3889 return -1; /* Unexpected error */
3891 dict
= PyObject_GetAttrString(v
, "__dict__");
3893 if (!PyErr_ExceptionMatches(PyExc_AttributeError
))
3895 PyErr_SetString(PyExc_ImportError
,
3896 "from-import-* object has no __dict__ and no __all__");
3899 all
= PyMapping_Keys(dict
);
3903 skip_leading_underscores
= 1;
3906 for (pos
= 0, err
= 0; ; pos
++) {
3907 name
= PySequence_GetItem(all
, pos
);
3909 if (!PyErr_ExceptionMatches(PyExc_IndexError
))
3915 if (skip_leading_underscores
&&
3916 PyString_Check(name
) &&
3917 PyString_AS_STRING(name
)[0] == '_')
3922 value
= PyObject_GetAttr(v
, name
);
3926 err
= PyDict_SetItem(locals
, name
, value
);
3937 build_class(PyObject
*methods
, PyObject
*bases
, PyObject
*name
)
3939 PyObject
*metaclass
= NULL
, *result
, *base
;
3941 if (PyDict_Check(methods
))
3942 metaclass
= PyDict_GetItemString(methods
, "__metaclass__");
3943 if (metaclass
!= NULL
)
3944 Py_INCREF(metaclass
);
3945 else if (PyTuple_Check(bases
) && PyTuple_GET_SIZE(bases
) > 0) {
3946 base
= PyTuple_GET_ITEM(bases
, 0);
3947 metaclass
= PyObject_GetAttrString(base
, "__class__");
3948 if (metaclass
== NULL
) {
3950 metaclass
= (PyObject
*)base
->ob_type
;
3951 Py_INCREF(metaclass
);
3955 PyObject
*g
= PyEval_GetGlobals();
3956 if (g
!= NULL
&& PyDict_Check(g
))
3957 metaclass
= PyDict_GetItemString(g
, "__metaclass__");
3958 if (metaclass
== NULL
)
3959 metaclass
= (PyObject
*) &PyClass_Type
;
3960 Py_INCREF(metaclass
);
3962 result
= PyObject_CallFunction(metaclass
, "OOO", name
, bases
, methods
);
3963 Py_DECREF(metaclass
);
3968 exec_statement(PyFrameObject
*f
, PyObject
*prog
, PyObject
*globals
,
3975 if (PyTuple_Check(prog
) && globals
== Py_None
&& locals
== Py_None
&&
3976 ((n
= PyTuple_Size(prog
)) == 2 || n
== 3)) {
3977 /* Backward compatibility hack */
3978 globals
= PyTuple_GetItem(prog
, 1);
3980 locals
= PyTuple_GetItem(prog
, 2);
3981 prog
= PyTuple_GetItem(prog
, 0);
3983 if (globals
== Py_None
) {
3984 globals
= PyEval_GetGlobals();
3985 if (locals
== Py_None
) {
3986 locals
= PyEval_GetLocals();
3990 else if (locals
== Py_None
)
3992 if (!PyString_Check(prog
) &&
3993 !PyUnicode_Check(prog
) &&
3994 !PyCode_Check(prog
) &&
3995 !PyFile_Check(prog
)) {
3996 PyErr_SetString(PyExc_TypeError
,
3997 "exec: arg 1 must be a string, file, or code object");
4000 if (!PyDict_Check(globals
)) {
4001 PyErr_SetString(PyExc_TypeError
,
4002 "exec: arg 2 must be a dictionary or None");
4005 if (!PyDict_Check(locals
)) {
4006 PyErr_SetString(PyExc_TypeError
,
4007 "exec: arg 3 must be a dictionary or None");
4010 if (PyDict_GetItemString(globals
, "__builtins__") == NULL
)
4011 PyDict_SetItemString(globals
, "__builtins__", f
->f_builtins
);
4012 if (PyCode_Check(prog
)) {
4013 if (PyCode_GetNumFree((PyCodeObject
*)prog
) > 0) {
4014 PyErr_SetString(PyExc_TypeError
,
4015 "code object passed to exec may not contain free variables");
4018 v
= PyEval_EvalCode((PyCodeObject
*) prog
, globals
, locals
);
4020 else if (PyFile_Check(prog
)) {
4021 FILE *fp
= PyFile_AsFile(prog
);
4022 char *name
= PyString_AsString(PyFile_Name(prog
));
4025 if (PyEval_MergeCompilerFlags(&cf
))
4026 v
= PyRun_FileFlags(fp
, name
, Py_file_input
, globals
,
4029 v
= PyRun_File(fp
, name
, Py_file_input
, globals
,
4033 PyObject
*tmp
= NULL
;
4037 #ifdef Py_USING_UNICODE
4038 if (PyUnicode_Check(prog
)) {
4039 tmp
= PyUnicode_AsUTF8String(prog
);
4043 cf
.cf_flags
|= PyCF_SOURCE_IS_UTF8
;
4046 if (PyString_AsStringAndSize(prog
, &str
, NULL
))
4048 if (PyEval_MergeCompilerFlags(&cf
))
4049 v
= PyRun_StringFlags(str
, Py_file_input
, globals
,
4052 v
= PyRun_String(str
, Py_file_input
, globals
, locals
);
4056 PyFrame_LocalsToFast(f
, 0);
4064 format_exc_check_arg(PyObject
*exc
, char *format_str
, PyObject
*obj
)
4071 obj_str
= PyString_AsString(obj
);
4075 PyErr_Format(exc
, format_str
, obj_str
);
4078 #ifdef DYNAMIC_EXECUTION_PROFILE
4081 getarray(long a
[256])
4084 PyObject
*l
= PyList_New(256);
4085 if (l
== NULL
) return NULL
;
4086 for (i
= 0; i
< 256; i
++) {
4087 PyObject
*x
= PyInt_FromLong(a
[i
]);
4092 PyList_SetItem(l
, i
, x
);
4094 for (i
= 0; i
< 256; i
++)
4100 _Py_GetDXProfile(PyObject
*self
, PyObject
*args
)
4103 return getarray(dxp
);
4106 PyObject
*l
= PyList_New(257);
4107 if (l
== NULL
) return NULL
;
4108 for (i
= 0; i
< 257; i
++) {
4109 PyObject
*x
= getarray(dxpairs
[i
]);
4114 PyList_SetItem(l
, i
, x
);