Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2 | /* Execute compiled code */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4 | /* XXX TO DO: |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 5 | XXX speed up searching for keywords by using a dictionary |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 6 | XXX document it! |
| 7 | */ |
| 8 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 9 | /* enable more aggressive intra-module optimizations, where available */ |
Fredrik Lundh | 57640f5 | 2006-05-26 11:54:04 +0000 | [diff] [blame] | 10 | #define PY_LOCAL_AGGRESSIVE |
| 11 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 12 | #include "Python.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 13 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 14 | #include "code.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 15 | #include "frameobject.h" |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 16 | #include "eval.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 17 | #include "opcode.h" |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 18 | #include "structmember.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 19 | |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 20 | #include <ctype.h> |
| 21 | |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 22 | #ifndef WITH_TSC |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 23 | |
| 24 | #define READ_TIMESTAMP(var) |
| 25 | |
| 26 | #else |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 27 | |
| 28 | typedef unsigned long long uint64; |
| 29 | |
Michael W. Hudson | 800ba23 | 2004-08-12 18:19:17 +0000 | [diff] [blame] | 30 | #if defined(__ppc__) /* <- Don't know if this is the correct symbol; this |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 31 | section should work for GCC on any PowerPC |
| 32 | platform, irrespective of OS. |
| 33 | POWER? Who knows :-) */ |
Michael W. Hudson | 800ba23 | 2004-08-12 18:19:17 +0000 | [diff] [blame] | 34 | |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 35 | #define READ_TIMESTAMP(var) ppc_getcounter(&var) |
Michael W. Hudson | 800ba23 | 2004-08-12 18:19:17 +0000 | [diff] [blame] | 36 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 37 | static void |
Michael W. Hudson | 800ba23 | 2004-08-12 18:19:17 +0000 | [diff] [blame] | 38 | ppc_getcounter(uint64 *v) |
| 39 | { |
| 40 | register unsigned long tbu, tb, tbu2; |
| 41 | |
| 42 | loop: |
| 43 | asm volatile ("mftbu %0" : "=r" (tbu) ); |
| 44 | asm volatile ("mftb %0" : "=r" (tb) ); |
| 45 | asm volatile ("mftbu %0" : "=r" (tbu2)); |
| 46 | if (__builtin_expect(tbu != tbu2, 0)) goto loop; |
| 47 | |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 48 | /* The slightly peculiar way of writing the next lines is |
Michael W. Hudson | 800ba23 | 2004-08-12 18:19:17 +0000 | [diff] [blame] | 49 | compiled better by GCC than any other way I tried. */ |
| 50 | ((long*)(v))[0] = tbu; |
| 51 | ((long*)(v))[1] = tb; |
| 52 | } |
| 53 | |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 54 | #else /* this is for linux/x86 (and probably any other GCC/x86 combo) */ |
Michael W. Hudson | 800ba23 | 2004-08-12 18:19:17 +0000 | [diff] [blame] | 55 | |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 56 | #define READ_TIMESTAMP(val) \ |
| 57 | __asm__ __volatile__("rdtsc" : "=A" (val)) |
Michael W. Hudson | 800ba23 | 2004-08-12 18:19:17 +0000 | [diff] [blame] | 58 | |
| 59 | #endif |
| 60 | |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 61 | void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1, |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 62 | uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1) |
| 63 | { |
| 64 | uint64 intr, inst, loop; |
| 65 | PyThreadState *tstate = PyThreadState_Get(); |
| 66 | if (!tstate->interp->tscdump) |
| 67 | return; |
| 68 | intr = intr1 - intr0; |
| 69 | inst = inst1 - inst0 - intr; |
| 70 | loop = loop1 - loop0 - intr; |
| 71 | fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n", |
| 72 | opcode, ticked, inst, loop); |
| 73 | } |
Michael W. Hudson | 800ba23 | 2004-08-12 18:19:17 +0000 | [diff] [blame] | 74 | |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 75 | #endif |
| 76 | |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 77 | /* Turn this on if your compiler chokes on the big switch: */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 78 | /* #define CASE_TOO_BIG 1 */ |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 79 | |
Guido van Rossum | 408027e | 1996-12-30 16:17:54 +0000 | [diff] [blame] | 80 | #ifdef Py_DEBUG |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 81 | /* For debugging the interpreter: */ |
| 82 | #define LLTRACE 1 /* Low-level trace feature */ |
| 83 | #define CHECKEXC 1 /* Double-check exception checking */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 84 | #endif |
| 85 | |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 86 | typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *); |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 87 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 88 | /* Forward declarations */ |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 89 | #ifdef WITH_TSC |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 90 | static PyObject * call_function(PyObject ***, int, uint64*, uint64*); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 91 | #else |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 92 | static PyObject * call_function(PyObject ***, int); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 93 | #endif |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 94 | static PyObject * fast_function(PyObject *, PyObject ***, int, int, int); |
| 95 | static PyObject * do_call(PyObject *, PyObject ***, int, int); |
| 96 | static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int); |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 97 | static PyObject * update_keyword_args(PyObject *, int, PyObject ***, |
| 98 | PyObject *); |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 99 | static PyObject * update_star_args(int, int, PyObject *, PyObject ***); |
| 100 | static PyObject * load_args(PyObject ***, int); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 101 | #define CALL_FLAG_VAR 1 |
| 102 | #define CALL_FLAG_KW 2 |
| 103 | |
Guido van Rossum | 0a066c0 | 1992-03-27 17:29:15 +0000 | [diff] [blame] | 104 | #ifdef LLTRACE |
Fredrik Lundh | 1b94940 | 2006-05-26 12:01:49 +0000 | [diff] [blame] | 105 | static int lltrace; |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 106 | static int prtrace(PyObject *, char *); |
Guido van Rossum | 0a066c0 | 1992-03-27 17:29:15 +0000 | [diff] [blame] | 107 | #endif |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 108 | static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *, |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 109 | int, PyObject *); |
Amaury Forgeot d'Arc | 0d75f09 | 2007-11-13 21:54:28 +0000 | [diff] [blame] | 110 | static int call_trace_protected(Py_tracefunc, PyObject *, |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 111 | PyFrameObject *, int, PyObject *); |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 112 | static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *); |
| 113 | static int maybe_call_line_trace(Py_tracefunc, PyObject *, |
Armin Rigo | bf57a14 | 2004-03-22 19:24:58 +0000 | [diff] [blame] | 114 | PyFrameObject *, int *, int *, int *); |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 115 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 116 | static PyObject * apply_slice(PyObject *, PyObject *, PyObject *); |
| 117 | static int assign_slice(PyObject *, PyObject *, |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 118 | PyObject *, PyObject *); |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 119 | static PyObject * cmp_outcome(int, PyObject *, PyObject *); |
| 120 | static PyObject * import_from(PyObject *, PyObject *); |
| 121 | static int import_all_from(PyObject *, PyObject *); |
| 122 | static PyObject * build_class(PyObject *, PyObject *, PyObject *); |
| 123 | static int exec_statement(PyFrameObject *, |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 124 | PyObject *, PyObject *, PyObject *); |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 125 | static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *); |
| 126 | static void reset_exc_info(PyThreadState *); |
| 127 | static void format_exc_check_arg(PyObject *, char *, PyObject *); |
| 128 | static PyObject * string_concatenate(PyObject *, PyObject *, |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 129 | PyFrameObject *, unsigned char *); |
Benjamin Peterson | e18ef19 | 2009-01-20 14:21:16 +0000 | [diff] [blame] | 130 | static PyObject * kwd_as_string(PyObject *); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 131 | |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 132 | #define NAME_ERROR_MSG \ |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 133 | "name '%.200s' is not defined" |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 134 | #define GLOBAL_NAME_ERROR_MSG \ |
| 135 | "global name '%.200s' is not defined" |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 136 | #define UNBOUNDLOCAL_ERROR_MSG \ |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 137 | "local variable '%.200s' referenced before assignment" |
Jeremy Hylton | c76770c | 2001-04-13 16:51:46 +0000 | [diff] [blame] | 138 | #define UNBOUNDFREE_ERROR_MSG \ |
| 139 | "free variable '%.200s' referenced before assignment" \ |
| 140 | " in enclosing scope" |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 141 | |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 142 | /* Dynamic execution profile */ |
| 143 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 144 | #ifdef DXPAIRS |
| 145 | static long dxpairs[257][256]; |
| 146 | #define dxp dxpairs[256] |
| 147 | #else |
| 148 | static long dxp[256]; |
| 149 | #endif |
| 150 | #endif |
| 151 | |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 152 | /* Function call profile */ |
| 153 | #ifdef CALL_PROFILE |
| 154 | #define PCALL_NUM 11 |
| 155 | static int pcall[PCALL_NUM]; |
| 156 | |
| 157 | #define PCALL_ALL 0 |
| 158 | #define PCALL_FUNCTION 1 |
| 159 | #define PCALL_FAST_FUNCTION 2 |
| 160 | #define PCALL_FASTER_FUNCTION 3 |
| 161 | #define PCALL_METHOD 4 |
| 162 | #define PCALL_BOUND_METHOD 5 |
| 163 | #define PCALL_CFUNCTION 6 |
| 164 | #define PCALL_TYPE 7 |
| 165 | #define PCALL_GENERATOR 8 |
| 166 | #define PCALL_OTHER 9 |
| 167 | #define PCALL_POP 10 |
| 168 | |
| 169 | /* Notes about the statistics |
| 170 | |
| 171 | PCALL_FAST stats |
| 172 | |
| 173 | FAST_FUNCTION means no argument tuple needs to be created. |
| 174 | FASTER_FUNCTION means that the fast-path frame setup code is used. |
| 175 | |
| 176 | If there is a method call where the call can be optimized by changing |
| 177 | the argument tuple and calling the function directly, it gets recorded |
| 178 | twice. |
| 179 | |
| 180 | As a result, the relationship among the statistics appears to be |
| 181 | PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD + |
| 182 | PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER |
| 183 | PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION |
| 184 | PCALL_METHOD > PCALL_BOUND_METHOD |
| 185 | */ |
| 186 | |
| 187 | #define PCALL(POS) pcall[POS]++ |
| 188 | |
| 189 | PyObject * |
| 190 | PyEval_GetCallStats(PyObject *self) |
| 191 | { |
Andrew M. Kuchling | 1f3ebe0 | 2006-10-27 13:22:46 +0000 | [diff] [blame] | 192 | return Py_BuildValue("iiiiiiiiiii", |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 193 | pcall[0], pcall[1], pcall[2], pcall[3], |
| 194 | pcall[4], pcall[5], pcall[6], pcall[7], |
Andrew M. Kuchling | 1f3ebe0 | 2006-10-27 13:22:46 +0000 | [diff] [blame] | 195 | pcall[8], pcall[9], pcall[10]); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 196 | } |
| 197 | #else |
| 198 | #define PCALL(O) |
| 199 | |
| 200 | PyObject * |
| 201 | PyEval_GetCallStats(PyObject *self) |
| 202 | { |
| 203 | Py_INCREF(Py_None); |
| 204 | return Py_None; |
| 205 | } |
| 206 | #endif |
| 207 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 208 | |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 209 | #ifdef WITH_THREAD |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 210 | |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 211 | #ifdef HAVE_ERRNO_H |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 212 | #include <errno.h> |
Guido van Rossum | 2571cc8 | 1999-04-07 16:07:23 +0000 | [diff] [blame] | 213 | #endif |
Guido van Rossum | 49b5606 | 1998-10-01 20:42:43 +0000 | [diff] [blame] | 214 | #include "pythread.h" |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 215 | |
Guido van Rossum | b8b6d0c | 2003-06-28 21:53:52 +0000 | [diff] [blame] | 216 | static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */ |
Kristján Valur Jónsson | 0e91938 | 2009-01-09 20:31:26 +0000 | [diff] [blame] | 217 | static PyThread_type_lock pending_lock = 0; /* for pending calls */ |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 218 | static long main_thread = 0; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 219 | |
Tim Peters | 7f468f2 | 2004-10-11 02:40:51 +0000 | [diff] [blame] | 220 | int |
| 221 | PyEval_ThreadsInitialized(void) |
| 222 | { |
| 223 | return interpreter_lock != 0; |
| 224 | } |
| 225 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 226 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 227 | PyEval_InitThreads(void) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 228 | { |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 229 | if (interpreter_lock) |
Sjoerd Mullender | ed59d20 | 1993-01-06 13:36:38 +0000 | [diff] [blame] | 230 | return; |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 231 | interpreter_lock = PyThread_allocate_lock(); |
| 232 | PyThread_acquire_lock(interpreter_lock, 1); |
| 233 | main_thread = PyThread_get_thread_ident(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 234 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 235 | |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 236 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 237 | PyEval_AcquireLock(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 238 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 239 | PyThread_acquire_lock(interpreter_lock, 1); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 243 | PyEval_ReleaseLock(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 244 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 245 | PyThread_release_lock(interpreter_lock); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 249 | PyEval_AcquireThread(PyThreadState *tstate) |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 250 | { |
| 251 | if (tstate == NULL) |
| 252 | Py_FatalError("PyEval_AcquireThread: NULL new thread state"); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 253 | /* Check someone has called PyEval_InitThreads() to create the lock */ |
| 254 | assert(interpreter_lock); |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 255 | PyThread_acquire_lock(interpreter_lock, 1); |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 256 | if (PyThreadState_Swap(tstate) != NULL) |
| 257 | Py_FatalError( |
| 258 | "PyEval_AcquireThread: non-NULL old thread state"); |
| 259 | } |
| 260 | |
| 261 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 262 | PyEval_ReleaseThread(PyThreadState *tstate) |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 263 | { |
| 264 | if (tstate == NULL) |
| 265 | Py_FatalError("PyEval_ReleaseThread: NULL thread state"); |
| 266 | if (PyThreadState_Swap(NULL) != tstate) |
| 267 | Py_FatalError("PyEval_ReleaseThread: wrong thread state"); |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 268 | PyThread_release_lock(interpreter_lock); |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 269 | } |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 270 | |
| 271 | /* This function is called from PyOS_AfterFork to ensure that newly |
| 272 | created child processes don't hold locks referring to threads which |
| 273 | are not running in the child process. (This could also be done using |
| 274 | pthread_atfork mechanism, at least for the pthreads implementation.) */ |
| 275 | |
| 276 | void |
| 277 | PyEval_ReInitThreads(void) |
| 278 | { |
Jesse Noller | 5e62ca4 | 2008-07-16 20:03:47 +0000 | [diff] [blame] | 279 | PyObject *threading, *result; |
| 280 | PyThreadState *tstate; |
| 281 | |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 282 | if (!interpreter_lock) |
| 283 | return; |
| 284 | /*XXX Can't use PyThread_free_lock here because it does too |
| 285 | much error-checking. Doing this cleanly would require |
| 286 | adding a new function to each thread_*.h. Instead, just |
| 287 | create a new lock and waste a little bit of memory */ |
| 288 | interpreter_lock = PyThread_allocate_lock(); |
Kristján Valur Jónsson | 0e91938 | 2009-01-09 20:31:26 +0000 | [diff] [blame] | 289 | pending_lock = PyThread_allocate_lock(); |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 290 | PyThread_acquire_lock(interpreter_lock, 1); |
| 291 | main_thread = PyThread_get_thread_ident(); |
Jesse Noller | 5e62ca4 | 2008-07-16 20:03:47 +0000 | [diff] [blame] | 292 | |
| 293 | /* Update the threading module with the new state. |
| 294 | */ |
| 295 | tstate = PyThreadState_GET(); |
| 296 | threading = PyMapping_GetItemString(tstate->interp->modules, |
| 297 | "threading"); |
| 298 | if (threading == NULL) { |
| 299 | /* threading not imported */ |
| 300 | PyErr_Clear(); |
| 301 | return; |
| 302 | } |
| 303 | result = PyObject_CallMethod(threading, "_after_fork", NULL); |
| 304 | if (result == NULL) |
| 305 | PyErr_WriteUnraisable(threading); |
| 306 | else |
| 307 | Py_DECREF(result); |
| 308 | Py_DECREF(threading); |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 309 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 310 | #endif |
| 311 | |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 312 | /* Functions save_thread and restore_thread are always defined so |
| 313 | dynamically loaded modules needn't be compiled separately for use |
| 314 | with and without threads: */ |
| 315 | |
Guido van Rossum | 2fca21f7 | 1997-07-18 23:56:58 +0000 | [diff] [blame] | 316 | PyThreadState * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 317 | PyEval_SaveThread(void) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 318 | { |
Guido van Rossum | b74eca9 | 1997-09-30 22:03:16 +0000 | [diff] [blame] | 319 | PyThreadState *tstate = PyThreadState_Swap(NULL); |
| 320 | if (tstate == NULL) |
| 321 | Py_FatalError("PyEval_SaveThread: NULL tstate"); |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 322 | #ifdef WITH_THREAD |
Guido van Rossum | b74eca9 | 1997-09-30 22:03:16 +0000 | [diff] [blame] | 323 | if (interpreter_lock) |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 324 | PyThread_release_lock(interpreter_lock); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 325 | #endif |
Guido van Rossum | b74eca9 | 1997-09-30 22:03:16 +0000 | [diff] [blame] | 326 | return tstate; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 330 | PyEval_RestoreThread(PyThreadState *tstate) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 331 | { |
Guido van Rossum | b74eca9 | 1997-09-30 22:03:16 +0000 | [diff] [blame] | 332 | if (tstate == NULL) |
| 333 | Py_FatalError("PyEval_RestoreThread: NULL tstate"); |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 334 | #ifdef WITH_THREAD |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 335 | if (interpreter_lock) { |
Guido van Rossum | b74eca9 | 1997-09-30 22:03:16 +0000 | [diff] [blame] | 336 | int err = errno; |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 337 | PyThread_acquire_lock(interpreter_lock, 1); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 338 | errno = err; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 339 | } |
| 340 | #endif |
Guido van Rossum | b74eca9 | 1997-09-30 22:03:16 +0000 | [diff] [blame] | 341 | PyThreadState_Swap(tstate); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 345 | /* Mechanism whereby asynchronously executing callbacks (e.g. UNIX |
| 346 | signal handlers or Mac I/O completion routines) can schedule calls |
| 347 | to a function to be called synchronously. |
| 348 | The synchronous function is called with one void* argument. |
| 349 | It should return 0 for success or -1 for failure -- failure should |
| 350 | be accompanied by an exception. |
| 351 | |
| 352 | If registry succeeds, the registry function returns 0; if it fails |
| 353 | (e.g. due to too many pending calls) it returns -1 (without setting |
| 354 | an exception condition). |
| 355 | |
| 356 | Note that because registry may occur from within signal handlers, |
| 357 | or other asynchronous events, calling malloc() is unsafe! |
| 358 | |
| 359 | #ifdef WITH_THREAD |
| 360 | Any thread can schedule pending calls, but only the main thread |
| 361 | will execute them. |
Kristján Valur Jónsson | 0e91938 | 2009-01-09 20:31:26 +0000 | [diff] [blame] | 362 | There is no facility to schedule calls to a particular thread, but |
| 363 | that should be easy to change, should that ever be required. In |
| 364 | that case, the static variables here should go into the python |
| 365 | threadstate. |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 366 | #endif |
Kristján Valur Jónsson | 0e91938 | 2009-01-09 20:31:26 +0000 | [diff] [blame] | 367 | */ |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 368 | |
Kristján Valur Jónsson | 0e91938 | 2009-01-09 20:31:26 +0000 | [diff] [blame] | 369 | #ifdef WITH_THREAD |
| 370 | |
| 371 | /* The WITH_THREAD implementation is thread-safe. It allows |
| 372 | scheduling to be made from any thread, and even from an executing |
| 373 | callback. |
| 374 | */ |
| 375 | |
| 376 | #define NPENDINGCALLS 32 |
| 377 | static struct { |
| 378 | int (*func)(void *); |
| 379 | void *arg; |
| 380 | } pendingcalls[NPENDINGCALLS]; |
| 381 | static int pendingfirst = 0; |
| 382 | static int pendinglast = 0; |
| 383 | static volatile int pendingcalls_to_do = 1; /* trigger initialization of lock */ |
| 384 | static char pendingbusy = 0; |
| 385 | |
| 386 | int |
| 387 | Py_AddPendingCall(int (*func)(void *), void *arg) |
| 388 | { |
| 389 | int i, j, result=0; |
| 390 | PyThread_type_lock lock = pending_lock; |
| 391 | |
| 392 | /* try a few times for the lock. Since this mechanism is used |
| 393 | * for signal handling (on the main thread), there is a (slim) |
| 394 | * chance that a signal is delivered on the same thread while we |
| 395 | * hold the lock during the Py_MakePendingCalls() function. |
| 396 | * This avoids a deadlock in that case. |
| 397 | * Note that signals can be delivered on any thread. In particular, |
| 398 | * on Windows, a SIGINT is delivered on a system-created worker |
| 399 | * thread. |
| 400 | * We also check for lock being NULL, in the unlikely case that |
| 401 | * this function is called before any bytecode evaluation takes place. |
| 402 | */ |
| 403 | if (lock != NULL) { |
| 404 | for (i = 0; i<100; i++) { |
| 405 | if (PyThread_acquire_lock(lock, NOWAIT_LOCK)) |
| 406 | break; |
| 407 | } |
| 408 | if (i == 100) |
| 409 | return -1; |
| 410 | } |
| 411 | |
| 412 | i = pendinglast; |
| 413 | j = (i + 1) % NPENDINGCALLS; |
| 414 | if (j == pendingfirst) { |
| 415 | result = -1; /* Queue full */ |
| 416 | } else { |
| 417 | pendingcalls[i].func = func; |
| 418 | pendingcalls[i].arg = arg; |
| 419 | pendinglast = j; |
| 420 | } |
| 421 | /* signal main loop */ |
| 422 | _Py_Ticker = 0; |
| 423 | pendingcalls_to_do = 1; |
| 424 | if (lock != NULL) |
| 425 | PyThread_release_lock(lock); |
| 426 | return result; |
| 427 | } |
| 428 | |
| 429 | int |
| 430 | Py_MakePendingCalls(void) |
| 431 | { |
| 432 | int i; |
| 433 | int r = 0; |
| 434 | |
| 435 | if (!pending_lock) { |
| 436 | /* initial allocation of the lock */ |
| 437 | pending_lock = PyThread_allocate_lock(); |
| 438 | if (pending_lock == NULL) |
| 439 | return -1; |
| 440 | } |
| 441 | |
| 442 | /* only service pending calls on main thread */ |
| 443 | if (main_thread && PyThread_get_thread_ident() != main_thread) |
| 444 | return 0; |
| 445 | /* don't perform recursive pending calls */ |
| 446 | if (pendingbusy) |
| 447 | return 0; |
| 448 | pendingbusy = 1; |
| 449 | /* perform a bounded number of calls, in case of recursion */ |
| 450 | for (i=0; i<NPENDINGCALLS; i++) { |
| 451 | int j; |
| 452 | int (*func)(void *); |
Mark Dickinson | 9e58a37 | 2009-02-08 17:33:11 +0000 | [diff] [blame] | 453 | void *arg = NULL; |
Kristján Valur Jónsson | 0e91938 | 2009-01-09 20:31:26 +0000 | [diff] [blame] | 454 | |
| 455 | /* pop one item off the queue while holding the lock */ |
| 456 | PyThread_acquire_lock(pending_lock, WAIT_LOCK); |
| 457 | j = pendingfirst; |
| 458 | if (j == pendinglast) { |
| 459 | func = NULL; /* Queue empty */ |
| 460 | } else { |
| 461 | func = pendingcalls[j].func; |
| 462 | arg = pendingcalls[j].arg; |
| 463 | pendingfirst = (j + 1) % NPENDINGCALLS; |
| 464 | } |
| 465 | pendingcalls_to_do = pendingfirst != pendinglast; |
| 466 | PyThread_release_lock(pending_lock); |
| 467 | /* having released the lock, perform the callback */ |
| 468 | if (func == NULL) |
| 469 | break; |
| 470 | r = func(arg); |
| 471 | if (r) |
| 472 | break; |
| 473 | } |
| 474 | pendingbusy = 0; |
| 475 | return r; |
| 476 | } |
| 477 | |
| 478 | #else /* if ! defined WITH_THREAD */ |
| 479 | |
| 480 | /* |
| 481 | WARNING! ASYNCHRONOUSLY EXECUTING CODE! |
| 482 | This code is used for signal handling in python that isn't built |
| 483 | with WITH_THREAD. |
| 484 | Don't use this implementation when Py_AddPendingCalls() can happen |
| 485 | on a different thread! |
| 486 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 487 | There are two possible race conditions: |
Kristján Valur Jónsson | 0e91938 | 2009-01-09 20:31:26 +0000 | [diff] [blame] | 488 | (1) nested asynchronous calls to Py_AddPendingCall() |
| 489 | (2) AddPendingCall() calls made while pending calls are being processed. |
| 490 | |
| 491 | (1) is very unlikely because typically signal delivery |
| 492 | is blocked during signal handling. So it should be impossible. |
| 493 | (2) is a real possibility. |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 494 | The current code is safe against (2), but not against (1). |
| 495 | The safety against (2) is derived from the fact that only one |
Kristján Valur Jónsson | 0e91938 | 2009-01-09 20:31:26 +0000 | [diff] [blame] | 496 | thread is present, interrupted by signals, and that the critical |
| 497 | section is protected with the "busy" variable. On Windows, which |
| 498 | delivers SIGINT on a system thread, this does not hold and therefore |
| 499 | Windows really shouldn't use this version. |
| 500 | The two threads could theoretically wiggle around the "busy" variable. |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 501 | */ |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 502 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 503 | #define NPENDINGCALLS 32 |
| 504 | static struct { |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 505 | int (*func)(void *); |
| 506 | void *arg; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 507 | } pendingcalls[NPENDINGCALLS]; |
| 508 | static volatile int pendingfirst = 0; |
| 509 | static volatile int pendinglast = 0; |
Kristján Valur Jónsson | 0e91938 | 2009-01-09 20:31:26 +0000 | [diff] [blame] | 510 | static volatile int pendingcalls_to_do = 0; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 511 | |
| 512 | int |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 513 | Py_AddPendingCall(int (*func)(void *), void *arg) |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 514 | { |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 515 | static volatile int busy = 0; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 516 | int i, j; |
| 517 | /* XXX Begin critical section */ |
Guido van Rossum | 180d7b4 | 1994-09-29 09:45:57 +0000 | [diff] [blame] | 518 | if (busy) |
| 519 | return -1; |
| 520 | busy = 1; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 521 | i = pendinglast; |
| 522 | j = (i + 1) % NPENDINGCALLS; |
Guido van Rossum | 04e7032 | 2002-07-17 16:57:13 +0000 | [diff] [blame] | 523 | if (j == pendingfirst) { |
| 524 | busy = 0; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 525 | return -1; /* Queue full */ |
Guido van Rossum | 04e7032 | 2002-07-17 16:57:13 +0000 | [diff] [blame] | 526 | } |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 527 | pendingcalls[i].func = func; |
| 528 | pendingcalls[i].arg = arg; |
| 529 | pendinglast = j; |
Skip Montanaro | d581d77 | 2002-09-03 20:10:45 +0000 | [diff] [blame] | 530 | |
| 531 | _Py_Ticker = 0; |
Kristján Valur Jónsson | 0e91938 | 2009-01-09 20:31:26 +0000 | [diff] [blame] | 532 | pendingcalls_to_do = 1; /* Signal main loop */ |
Guido van Rossum | 180d7b4 | 1994-09-29 09:45:57 +0000 | [diff] [blame] | 533 | busy = 0; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 534 | /* XXX End critical section */ |
| 535 | return 0; |
| 536 | } |
| 537 | |
Guido van Rossum | 180d7b4 | 1994-09-29 09:45:57 +0000 | [diff] [blame] | 538 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 539 | Py_MakePendingCalls(void) |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 540 | { |
Guido van Rossum | 180d7b4 | 1994-09-29 09:45:57 +0000 | [diff] [blame] | 541 | static int busy = 0; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 542 | if (busy) |
Guido van Rossum | 180d7b4 | 1994-09-29 09:45:57 +0000 | [diff] [blame] | 543 | return 0; |
| 544 | busy = 1; |
Kristján Valur Jónsson | 0e91938 | 2009-01-09 20:31:26 +0000 | [diff] [blame] | 545 | pendingcalls_to_do = 0; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 546 | for (;;) { |
| 547 | int i; |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 548 | int (*func)(void *); |
| 549 | void *arg; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 550 | i = pendingfirst; |
| 551 | if (i == pendinglast) |
| 552 | break; /* Queue empty */ |
| 553 | func = pendingcalls[i].func; |
| 554 | arg = pendingcalls[i].arg; |
| 555 | pendingfirst = (i + 1) % NPENDINGCALLS; |
Guido van Rossum | 180d7b4 | 1994-09-29 09:45:57 +0000 | [diff] [blame] | 556 | if (func(arg) < 0) { |
| 557 | busy = 0; |
Kristján Valur Jónsson | 0e91938 | 2009-01-09 20:31:26 +0000 | [diff] [blame] | 558 | pendingcalls_to_do = 1; /* We're not done yet */ |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 559 | return -1; |
Guido van Rossum | 180d7b4 | 1994-09-29 09:45:57 +0000 | [diff] [blame] | 560 | } |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 561 | } |
Guido van Rossum | 180d7b4 | 1994-09-29 09:45:57 +0000 | [diff] [blame] | 562 | busy = 0; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 563 | return 0; |
| 564 | } |
| 565 | |
Kristján Valur Jónsson | 0e91938 | 2009-01-09 20:31:26 +0000 | [diff] [blame] | 566 | #endif /* WITH_THREAD */ |
| 567 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 568 | |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 569 | /* The interpreter's recursion limit */ |
| 570 | |
Hye-Shik Chang | b6fa281 | 2005-04-04 15:49:02 +0000 | [diff] [blame] | 571 | #ifndef Py_DEFAULT_RECURSION_LIMIT |
| 572 | #define Py_DEFAULT_RECURSION_LIMIT 1000 |
| 573 | #endif |
| 574 | static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT; |
| 575 | int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 576 | |
Vladimir Marangozov | 7bd25be | 2000-09-01 11:07:19 +0000 | [diff] [blame] | 577 | int |
| 578 | Py_GetRecursionLimit(void) |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 579 | { |
| 580 | return recursion_limit; |
| 581 | } |
| 582 | |
Vladimir Marangozov | 7bd25be | 2000-09-01 11:07:19 +0000 | [diff] [blame] | 583 | void |
| 584 | Py_SetRecursionLimit(int new_limit) |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 585 | { |
| 586 | recursion_limit = new_limit; |
Thomas Wouters | ae406c6 | 2007-09-19 17:27:43 +0000 | [diff] [blame] | 587 | _Py_CheckRecursionLimit = recursion_limit; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 588 | } |
| 589 | |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 590 | /* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall() |
| 591 | if the recursion_depth reaches _Py_CheckRecursionLimit. |
| 592 | If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit |
| 593 | to guarantee that _Py_CheckRecursiveCall() is regularly called. |
| 594 | Without USE_STACKCHECK, there is no need for this. */ |
| 595 | int |
| 596 | _Py_CheckRecursiveCall(char *where) |
| 597 | { |
| 598 | PyThreadState *tstate = PyThreadState_GET(); |
| 599 | |
| 600 | #ifdef USE_STACKCHECK |
| 601 | if (PyOS_CheckStack()) { |
| 602 | --tstate->recursion_depth; |
| 603 | PyErr_SetString(PyExc_MemoryError, "Stack overflow"); |
| 604 | return -1; |
| 605 | } |
| 606 | #endif |
| 607 | if (tstate->recursion_depth > recursion_limit) { |
| 608 | --tstate->recursion_depth; |
| 609 | PyErr_Format(PyExc_RuntimeError, |
| 610 | "maximum recursion depth exceeded%s", |
| 611 | where); |
| 612 | return -1; |
| 613 | } |
Thomas Wouters | ae406c6 | 2007-09-19 17:27:43 +0000 | [diff] [blame] | 614 | _Py_CheckRecursionLimit = recursion_limit; |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 615 | return 0; |
| 616 | } |
| 617 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 618 | /* Status code for main loop (reason for stack unwind) */ |
Raymond Hettinger | 7c95865 | 2004-04-06 10:11:10 +0000 | [diff] [blame] | 619 | enum why_code { |
| 620 | WHY_NOT = 0x0001, /* No error */ |
| 621 | WHY_EXCEPTION = 0x0002, /* Exception occurred */ |
| 622 | WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */ |
| 623 | WHY_RETURN = 0x0008, /* 'return' statement */ |
| 624 | WHY_BREAK = 0x0010, /* 'break' statement */ |
| 625 | WHY_CONTINUE = 0x0020, /* 'continue' statement */ |
| 626 | WHY_YIELD = 0x0040 /* 'yield' operator */ |
| 627 | }; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 628 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 629 | static enum why_code do_raise(PyObject *, PyObject *, PyObject *); |
| 630 | static int unpack_iterable(PyObject *, int, PyObject **); |
Guido van Rossum | 1aa1483 | 1997-01-21 05:34:20 +0000 | [diff] [blame] | 631 | |
Jeffrey Yasskin | fd8a1ec | 2008-12-03 06:46:45 +0000 | [diff] [blame] | 632 | /* Records whether tracing is on for any thread. Counts the number of |
| 633 | threads for which tstate->c_tracefunc is non-NULL, so if the value |
| 634 | is 0, we know we don't have to check this thread's c_tracefunc. |
| 635 | This speeds up the if statement in PyEval_EvalFrameEx() after |
| 636 | fast_next_opcode*/ |
| 637 | static int _Py_TracingPossible = 0; |
| 638 | |
Skip Montanaro | d581d77 | 2002-09-03 20:10:45 +0000 | [diff] [blame] | 639 | /* for manipulating the thread switch and periodic "stuff" - used to be |
| 640 | per thread, now just a pair o' globals */ |
Skip Montanaro | 99dba27 | 2002-09-03 20:19:06 +0000 | [diff] [blame] | 641 | int _Py_CheckInterval = 100; |
Kristján Valur Jónsson | 0e91938 | 2009-01-09 20:31:26 +0000 | [diff] [blame] | 642 | volatile int _Py_Ticker = 0; /* so that we hit a "tick" first thing */ |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 643 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 644 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 645 | PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 646 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 647 | return PyEval_EvalCodeEx(co, |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 648 | globals, locals, |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 649 | (PyObject **)NULL, 0, |
| 650 | (PyObject **)NULL, 0, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 651 | (PyObject **)NULL, 0, |
| 652 | NULL); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | |
| 656 | /* Interpreter main loop */ |
| 657 | |
Martin v. Löwis | 8d97e33 | 2004-06-27 15:43:12 +0000 | [diff] [blame] | 658 | PyObject * |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 659 | PyEval_EvalFrame(PyFrameObject *f) { |
| 660 | /* This is for backward compatibility with extension modules that |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 661 | used this API; core interpreter code should call |
| 662 | PyEval_EvalFrameEx() */ |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 663 | return PyEval_EvalFrameEx(f, 0); |
| 664 | } |
| 665 | |
| 666 | PyObject * |
Anthony Baxter | a863d33 | 2006-04-11 07:43:46 +0000 | [diff] [blame] | 667 | PyEval_EvalFrameEx(PyFrameObject *f, int throwflag) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 668 | { |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 669 | #ifdef DXPAIRS |
| 670 | int lastopcode = 0; |
| 671 | #endif |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 672 | register PyObject **stack_pointer; /* Next free slot in value stack */ |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 673 | register unsigned char *next_instr; |
Armin Rigo | 8817fcd | 2004-06-17 10:22:40 +0000 | [diff] [blame] | 674 | register int opcode; /* Current opcode */ |
| 675 | register int oparg; /* Current opcode argument, if any */ |
Raymond Hettinger | 7c95865 | 2004-04-06 10:11:10 +0000 | [diff] [blame] | 676 | register enum why_code why; /* Reason for block stack unwind */ |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 677 | register int err; /* Error status -- nonzero if error */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 678 | register PyObject *x; /* Result object -- NULL if error */ |
| 679 | register PyObject *v; /* Temporary objects popped off stack */ |
| 680 | register PyObject *w; |
| 681 | register PyObject *u; |
| 682 | register PyObject *t; |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 683 | register PyObject *stream = NULL; /* for PRINT opcodes */ |
Jeremy Hylton | 2b724da | 2001-01-29 22:51:52 +0000 | [diff] [blame] | 684 | register PyObject **fastlocals, **freevars; |
Guido van Rossum | 014518f | 1998-11-23 21:09:51 +0000 | [diff] [blame] | 685 | PyObject *retval = NULL; /* Return value */ |
Guido van Rossum | 885553e | 1998-12-21 18:33:30 +0000 | [diff] [blame] | 686 | PyThreadState *tstate = PyThreadState_GET(); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 687 | PyCodeObject *co; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 688 | |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 689 | /* when tracing we set things up so that |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 690 | |
| 691 | not (instr_lb <= current_bytecode_offset < instr_ub) |
| 692 | |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 693 | is true when the line being executed has changed. The |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 694 | initial values are such as to make this false the first |
| 695 | time it is tested. */ |
Armin Rigo | bf57a14 | 2004-03-22 19:24:58 +0000 | [diff] [blame] | 696 | int instr_ub = -1, instr_lb = 0, instr_prev = -1; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 697 | |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 698 | unsigned char *first_instr; |
Skip Montanaro | 04d80f8 | 2002-08-04 21:03:35 +0000 | [diff] [blame] | 699 | PyObject *names; |
| 700 | PyObject *consts; |
Neal Norwitz | 5f5153e | 2005-10-21 04:28:38 +0000 | [diff] [blame] | 701 | #if defined(Py_DEBUG) || defined(LLTRACE) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 702 | /* Make it easier to find out where we are with a debugger */ |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 703 | char *filename; |
Guido van Rossum | 99bec95 | 1992-09-03 20:29:45 +0000 | [diff] [blame] | 704 | #endif |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 705 | |
Neal Norwitz | a81d220 | 2002-07-14 00:27:26 +0000 | [diff] [blame] | 706 | /* Tuple access macros */ |
| 707 | |
| 708 | #ifndef Py_DEBUG |
| 709 | #define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i)) |
| 710 | #else |
| 711 | #define GETITEM(v, i) PyTuple_GetItem((v), (i)) |
| 712 | #endif |
| 713 | |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 714 | #ifdef WITH_TSC |
| 715 | /* Use Pentium timestamp counter to mark certain events: |
| 716 | inst0 -- beginning of switch statement for opcode dispatch |
| 717 | inst1 -- end of switch statement (may be skipped) |
| 718 | loop0 -- the top of the mainloop |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 719 | loop1 -- place where control returns again to top of mainloop |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 720 | (may be skipped) |
| 721 | intr1 -- beginning of long interruption |
| 722 | intr2 -- end of long interruption |
| 723 | |
| 724 | Many opcodes call out to helper C functions. In some cases, the |
| 725 | time in those functions should be counted towards the time for the |
| 726 | opcode, but not in all cases. For example, a CALL_FUNCTION opcode |
| 727 | calls another Python function; there's no point in charge all the |
| 728 | bytecode executed by the called function to the caller. |
| 729 | |
| 730 | It's hard to make a useful judgement statically. In the presence |
| 731 | of operator overloading, it's impossible to tell if a call will |
| 732 | execute new Python code or not. |
| 733 | |
| 734 | It's a case-by-case judgement. I'll use intr1 for the following |
| 735 | cases: |
| 736 | |
| 737 | EXEC_STMT |
| 738 | IMPORT_STAR |
| 739 | IMPORT_FROM |
| 740 | CALL_FUNCTION (and friends) |
| 741 | |
| 742 | */ |
| 743 | uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0; |
| 744 | int ticked = 0; |
| 745 | |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 746 | READ_TIMESTAMP(inst0); |
| 747 | READ_TIMESTAMP(inst1); |
| 748 | READ_TIMESTAMP(loop0); |
| 749 | READ_TIMESTAMP(loop1); |
Michael W. Hudson | 800ba23 | 2004-08-12 18:19:17 +0000 | [diff] [blame] | 750 | |
| 751 | /* shut up the compiler */ |
| 752 | opcode = 0; |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 753 | #endif |
| 754 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 755 | /* Code access macros */ |
| 756 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 757 | #define INSTR_OFFSET() ((int)(next_instr - first_instr)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 758 | #define NEXTOP() (*next_instr++) |
Raymond Hettinger | 5bed456 | 2004-04-10 23:34:17 +0000 | [diff] [blame] | 759 | #define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2]) |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 760 | #define PEEKARG() ((next_instr[2]<<8) + next_instr[1]) |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 761 | #define JUMPTO(x) (next_instr = first_instr + (x)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 762 | #define JUMPBY(x) (next_instr += (x)) |
| 763 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 764 | /* OpCode prediction macros |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 765 | Some opcodes tend to come in pairs thus making it possible to |
| 766 | predict the second code when the first is run. For example, |
Jeffrey Yasskin | 68d6852 | 2009-02-28 19:03:21 +0000 | [diff] [blame] | 767 | GET_ITER is often followed by FOR_ITER. And FOR_ITER is often |
| 768 | followed by STORE_FAST or UNPACK_SEQUENCE. |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 769 | |
Raymond Hettinger | afae11e | 2008-07-05 02:11:55 +0000 | [diff] [blame] | 770 | Verifying the prediction costs a single high-speed test of a register |
Raymond Hettinger | ac207292 | 2003-03-16 15:41:11 +0000 | [diff] [blame] | 771 | variable against a constant. If the pairing was good, then the |
Raymond Hettinger | afae11e | 2008-07-05 02:11:55 +0000 | [diff] [blame] | 772 | processor's own internal branch predication has a high likelihood of |
| 773 | success, resulting in a nearly zero-overhead transition to the |
| 774 | next opcode. A successful prediction saves a trip through the eval-loop |
Antoine Pitrou | c2cc80c | 2008-07-25 22:13:52 +0000 | [diff] [blame] | 775 | including its two unpredictable branches, the HAS_ARG test and the |
Raymond Hettinger | afae11e | 2008-07-05 02:11:55 +0000 | [diff] [blame] | 776 | switch-case. Combined with the processor's internal branch prediction, |
Antoine Pitrou | c2cc80c | 2008-07-25 22:13:52 +0000 | [diff] [blame] | 777 | a successful PREDICT has the effect of making the two opcodes run as if |
Raymond Hettinger | afae11e | 2008-07-05 02:11:55 +0000 | [diff] [blame] | 778 | they were a single new opcode with the bodies combined. |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 779 | |
Raymond Hettinger | afae11e | 2008-07-05 02:11:55 +0000 | [diff] [blame] | 780 | If collecting opcode statistics, your choices are to either keep the |
| 781 | predictions turned-on and interpret the results as if some opcodes |
| 782 | had been combined or turn-off predictions so that the opcode frequency |
| 783 | counter updates for both opcodes. |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 784 | */ |
| 785 | |
Raymond Hettinger | a721698 | 2004-02-08 19:59:27 +0000 | [diff] [blame] | 786 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 787 | #define PREDICT(op) if (0) goto PRED_##op |
| 788 | #else |
Raymond Hettinger | ac207292 | 2003-03-16 15:41:11 +0000 | [diff] [blame] | 789 | #define PREDICT(op) if (*next_instr == op) goto PRED_##op |
Raymond Hettinger | a721698 | 2004-02-08 19:59:27 +0000 | [diff] [blame] | 790 | #endif |
| 791 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 792 | #define PREDICTED(op) PRED_##op: next_instr++ |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 793 | #define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3 |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 794 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 795 | /* Stack manipulation macros */ |
| 796 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 797 | /* The stack can grow at most MAXINT deep, as co_nlocals and |
| 798 | co_stacksize are ints. */ |
| 799 | #define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 800 | #define EMPTY() (STACK_LEVEL() == 0) |
| 801 | #define TOP() (stack_pointer[-1]) |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 802 | #define SECOND() (stack_pointer[-2]) |
| 803 | #define THIRD() (stack_pointer[-3]) |
| 804 | #define FOURTH() (stack_pointer[-4]) |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 805 | #define SET_TOP(v) (stack_pointer[-1] = (v)) |
| 806 | #define SET_SECOND(v) (stack_pointer[-2] = (v)) |
| 807 | #define SET_THIRD(v) (stack_pointer[-3] = (v)) |
| 808 | #define SET_FOURTH(v) (stack_pointer[-4] = (v)) |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 809 | #define BASIC_STACKADJ(n) (stack_pointer += n) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 810 | #define BASIC_PUSH(v) (*stack_pointer++ = (v)) |
| 811 | #define BASIC_POP() (*--stack_pointer) |
| 812 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 813 | #ifdef LLTRACE |
Jeremy Hylton | 1436815 | 2001-10-17 13:29:30 +0000 | [diff] [blame] | 814 | #define PUSH(v) { (void)(BASIC_PUSH(v), \ |
| 815 | lltrace && prtrace(TOP(), "push")); \ |
Richard Jones | cebbefc | 2006-05-23 18:28:17 +0000 | [diff] [blame] | 816 | assert(STACK_LEVEL() <= co->co_stacksize); } |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 817 | #define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \ |
| 818 | BASIC_POP()) |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 819 | #define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \ |
| 820 | lltrace && prtrace(TOP(), "stackadj")); \ |
Richard Jones | cebbefc | 2006-05-23 18:28:17 +0000 | [diff] [blame] | 821 | assert(STACK_LEVEL() <= co->co_stacksize); } |
Christian Heimes | 52729ac | 2007-12-14 02:33:57 +0000 | [diff] [blame] | 822 | #define EXT_POP(STACK_POINTER) ((void)(lltrace && \ |
| 823 | prtrace((STACK_POINTER)[-1], "ext_pop")), \ |
| 824 | *--(STACK_POINTER)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 825 | #else |
| 826 | #define PUSH(v) BASIC_PUSH(v) |
| 827 | #define POP() BASIC_POP() |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 828 | #define STACKADJ(n) BASIC_STACKADJ(n) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 829 | #define EXT_POP(STACK_POINTER) (*--(STACK_POINTER)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 830 | #endif |
| 831 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 832 | /* Local variable macros */ |
| 833 | |
| 834 | #define GETLOCAL(i) (fastlocals[i]) |
Guido van Rossum | cfbf1a3 | 2002-03-28 20:17:52 +0000 | [diff] [blame] | 835 | |
| 836 | /* The SETLOCAL() macro must not DECREF the local variable in-place and |
| 837 | then store the new value; it must copy the old value to a temporary |
| 838 | value, then store the new value, and then DECREF the temporary value. |
| 839 | This is because it is possible that during the DECREF the frame is |
| 840 | accessed by other code (e.g. a __del__ method or gc.collect()) and the |
| 841 | variable would be pointing to already-freed memory. */ |
| 842 | #define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \ |
| 843 | GETLOCAL(i) = value; \ |
| 844 | Py_XDECREF(tmp); } while (0) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 845 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 846 | /* Start of code */ |
| 847 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 848 | if (f == NULL) |
| 849 | return NULL; |
| 850 | |
Armin Rigo | 1d313ab | 2003-10-25 14:33:09 +0000 | [diff] [blame] | 851 | /* push frame */ |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 852 | if (Py_EnterRecursiveCall("")) |
Armin Rigo | 1d313ab | 2003-10-25 14:33:09 +0000 | [diff] [blame] | 853 | return NULL; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 854 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 855 | tstate->frame = f; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 856 | |
Neil Schemenauer | 6c0f200 | 2001-09-04 19:03:35 +0000 | [diff] [blame] | 857 | if (tstate->use_tracing) { |
| 858 | if (tstate->c_tracefunc != NULL) { |
| 859 | /* tstate->c_tracefunc, if defined, is a |
| 860 | function that will be called on *every* entry |
| 861 | to a code block. Its return value, if not |
| 862 | None, is a function that will be called at |
| 863 | the start of each executed line of code. |
| 864 | (Actually, the function must return itself |
| 865 | in order to continue tracing.) The trace |
| 866 | functions are called with three arguments: |
| 867 | a pointer to the current frame, a string |
| 868 | indicating why the function is called, and |
| 869 | an argument which depends on the situation. |
| 870 | The global trace function is also called |
| 871 | whenever an exception is detected. */ |
Antoine Pitrou | c2cc80c | 2008-07-25 22:13:52 +0000 | [diff] [blame] | 872 | if (call_trace_protected(tstate->c_tracefunc, |
Amaury Forgeot d'Arc | 0d75f09 | 2007-11-13 21:54:28 +0000 | [diff] [blame] | 873 | tstate->c_traceobj, |
| 874 | f, PyTrace_CALL, Py_None)) { |
Neil Schemenauer | 6c0f200 | 2001-09-04 19:03:35 +0000 | [diff] [blame] | 875 | /* Trace function raised an error */ |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 876 | goto exit_eval_frame; |
Neil Schemenauer | 6c0f200 | 2001-09-04 19:03:35 +0000 | [diff] [blame] | 877 | } |
| 878 | } |
| 879 | if (tstate->c_profilefunc != NULL) { |
| 880 | /* Similar for c_profilefunc, except it needn't |
| 881 | return itself and isn't called for "line" events */ |
Amaury Forgeot d'Arc | 0d75f09 | 2007-11-13 21:54:28 +0000 | [diff] [blame] | 882 | if (call_trace_protected(tstate->c_profilefunc, |
| 883 | tstate->c_profileobj, |
| 884 | f, PyTrace_CALL, Py_None)) { |
Neil Schemenauer | 6c0f200 | 2001-09-04 19:03:35 +0000 | [diff] [blame] | 885 | /* Profile function raised an error */ |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 886 | goto exit_eval_frame; |
Neil Schemenauer | 6c0f200 | 2001-09-04 19:03:35 +0000 | [diff] [blame] | 887 | } |
| 888 | } |
| 889 | } |
| 890 | |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 891 | co = f->f_code; |
| 892 | names = co->co_names; |
| 893 | consts = co->co_consts; |
| 894 | fastlocals = f->f_localsplus; |
Richard Jones | cebbefc | 2006-05-23 18:28:17 +0000 | [diff] [blame] | 895 | freevars = f->f_localsplus + co->co_nlocals; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 896 | first_instr = (unsigned char*) PyString_AS_STRING(co->co_code); |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 897 | /* An explanation is in order for the next line. |
| 898 | |
| 899 | f->f_lasti now refers to the index of the last instruction |
| 900 | executed. You might think this was obvious from the name, but |
| 901 | this wasn't always true before 2.3! PyFrame_New now sets |
| 902 | f->f_lasti to -1 (i.e. the index *before* the first instruction) |
| 903 | and YIELD_VALUE doesn't fiddle with f_lasti any more. So this |
Antoine Pitrou | c2cc80c | 2008-07-25 22:13:52 +0000 | [diff] [blame] | 904 | does work. Promise. |
Raymond Hettinger | 4bd97d4 | 2007-01-06 01:14:41 +0000 | [diff] [blame] | 905 | |
| 906 | When the PREDICT() macros are enabled, some opcode pairs follow in |
Antoine Pitrou | c2cc80c | 2008-07-25 22:13:52 +0000 | [diff] [blame] | 907 | direct succession without updating f->f_lasti. A successful |
Raymond Hettinger | 4bd97d4 | 2007-01-06 01:14:41 +0000 | [diff] [blame] | 908 | prediction effectively links the two codes together as if they |
| 909 | were a single new opcode; accordingly,f->f_lasti will point to |
| 910 | the first code in the pair (for instance, GET_ITER followed by |
| 911 | FOR_ITER is effectively a single opcode and f->f_lasti will point |
| 912 | at to the beginning of the combined pair.) |
| 913 | */ |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 914 | next_instr = first_instr + f->f_lasti + 1; |
| 915 | stack_pointer = f->f_stacktop; |
| 916 | assert(stack_pointer != NULL); |
| 917 | f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */ |
| 918 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 919 | #ifdef LLTRACE |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 920 | lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 921 | #endif |
Neal Norwitz | 5f5153e | 2005-10-21 04:28:38 +0000 | [diff] [blame] | 922 | #if defined(Py_DEBUG) || defined(LLTRACE) |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 923 | filename = PyString_AsString(co->co_filename); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 924 | #endif |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 925 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 926 | why = WHY_NOT; |
| 927 | err = 0; |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 928 | x = Py_None; /* Not a reference, just anything non-NULL */ |
Fred Drake | 48fba73 | 2000-10-11 13:54:07 +0000 | [diff] [blame] | 929 | w = NULL; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 930 | |
Anthony Baxter | a863d33 | 2006-04-11 07:43:46 +0000 | [diff] [blame] | 931 | if (throwflag) { /* support for generator.throw() */ |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 932 | why = WHY_EXCEPTION; |
| 933 | goto on_error; |
| 934 | } |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 935 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 936 | for (;;) { |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 937 | #ifdef WITH_TSC |
| 938 | if (inst1 == 0) { |
| 939 | /* Almost surely, the opcode executed a break |
| 940 | or a continue, preventing inst1 from being set |
| 941 | on the way out of the loop. |
| 942 | */ |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 943 | READ_TIMESTAMP(inst1); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 944 | loop1 = inst1; |
| 945 | } |
| 946 | dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1, |
| 947 | intr0, intr1); |
| 948 | ticked = 0; |
| 949 | inst1 = 0; |
| 950 | intr0 = 0; |
| 951 | intr1 = 0; |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 952 | READ_TIMESTAMP(loop0); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 953 | #endif |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 954 | assert(stack_pointer >= f->f_valuestack); /* else underflow */ |
Richard Jones | cebbefc | 2006-05-23 18:28:17 +0000 | [diff] [blame] | 955 | assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */ |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 956 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 957 | /* Do periodic things. Doing this every time through |
| 958 | the loop would add too much overhead, so we do it |
| 959 | only every Nth instruction. We also do it if |
Kristján Valur Jónsson | 0e91938 | 2009-01-09 20:31:26 +0000 | [diff] [blame] | 960 | ``pendingcalls_to_do'' is set, i.e. when an asynchronous |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 961 | event needs attention (e.g. a signal handler or |
| 962 | async I/O handler); see Py_AddPendingCall() and |
| 963 | Py_MakePendingCalls() above. */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 964 | |
Skip Montanaro | d581d77 | 2002-09-03 20:10:45 +0000 | [diff] [blame] | 965 | if (--_Py_Ticker < 0) { |
Thomas Wouters | ae406c6 | 2007-09-19 17:27:43 +0000 | [diff] [blame] | 966 | if (*next_instr == SETUP_FINALLY) { |
| 967 | /* Make the last opcode before |
| 968 | a try: finally: block uninterruptable. */ |
| 969 | goto fast_next_opcode; |
| 970 | } |
Skip Montanaro | d581d77 | 2002-09-03 20:10:45 +0000 | [diff] [blame] | 971 | _Py_Ticker = _Py_CheckInterval; |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 972 | tstate->tick_counter++; |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 973 | #ifdef WITH_TSC |
| 974 | ticked = 1; |
| 975 | #endif |
Kristján Valur Jónsson | 0e91938 | 2009-01-09 20:31:26 +0000 | [diff] [blame] | 976 | if (pendingcalls_to_do) { |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 977 | if (Py_MakePendingCalls() < 0) { |
| 978 | why = WHY_EXCEPTION; |
| 979 | goto on_error; |
| 980 | } |
Kristján Valur Jónsson | 0e91938 | 2009-01-09 20:31:26 +0000 | [diff] [blame] | 981 | if (pendingcalls_to_do) |
Kurt B. Kaiser | 4c79a83 | 2004-11-23 18:06:08 +0000 | [diff] [blame] | 982 | /* MakePendingCalls() didn't succeed. |
| 983 | Force early re-execution of this |
| 984 | "periodic" code, possibly after |
| 985 | a thread switch */ |
| 986 | _Py_Ticker = 0; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 987 | } |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 988 | #ifdef WITH_THREAD |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 989 | if (interpreter_lock) { |
| 990 | /* Give another thread a chance */ |
| 991 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 992 | if (PyThreadState_Swap(NULL) != tstate) |
| 993 | Py_FatalError("ceval: tstate mix-up"); |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 994 | PyThread_release_lock(interpreter_lock); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 995 | |
| 996 | /* Other threads may run now */ |
| 997 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 998 | PyThread_acquire_lock(interpreter_lock, 1); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 999 | if (PyThreadState_Swap(tstate) != NULL) |
| 1000 | Py_FatalError("ceval: orphan tstate"); |
Guido van Rossum | b8b6d0c | 2003-06-28 21:53:52 +0000 | [diff] [blame] | 1001 | |
| 1002 | /* Check for thread interrupts */ |
| 1003 | |
| 1004 | if (tstate->async_exc != NULL) { |
| 1005 | x = tstate->async_exc; |
| 1006 | tstate->async_exc = NULL; |
| 1007 | PyErr_SetNone(x); |
| 1008 | Py_DECREF(x); |
| 1009 | why = WHY_EXCEPTION; |
| 1010 | goto on_error; |
| 1011 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1012 | } |
| 1013 | #endif |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1014 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1015 | |
Neil Schemenauer | 6354386 | 2002-02-17 19:10:14 +0000 | [diff] [blame] | 1016 | fast_next_opcode: |
Guido van Rossum | 99bec95 | 1992-09-03 20:29:45 +0000 | [diff] [blame] | 1017 | f->f_lasti = INSTR_OFFSET(); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1018 | |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1019 | /* line-by-line tracing support */ |
| 1020 | |
Jeffrey Yasskin | fd8a1ec | 2008-12-03 06:46:45 +0000 | [diff] [blame] | 1021 | if (_Py_TracingPossible && |
| 1022 | tstate->c_tracefunc != NULL && !tstate->tracing) { |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1023 | /* see maybe_call_line_trace |
| 1024 | for expository comments */ |
| 1025 | f->f_stacktop = stack_pointer; |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 1026 | |
Michael W. Hudson | 58ee2af | 2003-04-29 16:18:47 +0000 | [diff] [blame] | 1027 | err = maybe_call_line_trace(tstate->c_tracefunc, |
| 1028 | tstate->c_traceobj, |
Armin Rigo | bf57a14 | 2004-03-22 19:24:58 +0000 | [diff] [blame] | 1029 | f, &instr_lb, &instr_ub, |
| 1030 | &instr_prev); |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1031 | /* Reload possibly changed frame fields */ |
| 1032 | JUMPTO(f->f_lasti); |
Michael W. Hudson | 58ee2af | 2003-04-29 16:18:47 +0000 | [diff] [blame] | 1033 | if (f->f_stacktop != NULL) { |
| 1034 | stack_pointer = f->f_stacktop; |
| 1035 | f->f_stacktop = NULL; |
| 1036 | } |
| 1037 | if (err) { |
| 1038 | /* trace function raised an exception */ |
| 1039 | goto on_error; |
| 1040 | } |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
| 1043 | /* Extract opcode and argument */ |
| 1044 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1045 | opcode = NEXTOP(); |
Armin Rigo | 8817fcd | 2004-06-17 10:22:40 +0000 | [diff] [blame] | 1046 | oparg = 0; /* allows oparg to be stored in a register because |
| 1047 | it doesn't have to be remembered across a full loop */ |
Raymond Hettinger | 5bed456 | 2004-04-10 23:34:17 +0000 | [diff] [blame] | 1048 | if (HAS_ARG(opcode)) |
| 1049 | oparg = NEXTARG(); |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 1050 | dispatch_opcode: |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 1051 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 1052 | #ifdef DXPAIRS |
| 1053 | dxpairs[lastopcode][opcode]++; |
| 1054 | lastopcode = opcode; |
| 1055 | #endif |
| 1056 | dxp[opcode]++; |
| 1057 | #endif |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1058 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 1059 | #ifdef LLTRACE |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1060 | /* Instruction tracing */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1061 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 1062 | if (lltrace) { |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1063 | if (HAS_ARG(opcode)) { |
| 1064 | printf("%d: %d, %d\n", |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 1065 | f->f_lasti, opcode, oparg); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1066 | } |
| 1067 | else { |
| 1068 | printf("%d: %d\n", |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 1069 | f->f_lasti, opcode); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1070 | } |
| 1071 | } |
| 1072 | #endif |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 1073 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1074 | /* Main switch on opcode */ |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 1075 | READ_TIMESTAMP(inst0); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 1076 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1077 | switch (opcode) { |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1078 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1079 | /* BEWARE! |
| 1080 | It is essential that any operation that fails sets either |
| 1081 | x to NULL, err to nonzero, or why to anything but WHY_NOT, |
| 1082 | and that no operation that succeeds does this! */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1083 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1084 | /* case STOP_CODE: this is an error! */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1085 | |
Raymond Hettinger | 9c18e81 | 2004-06-21 16:31:15 +0000 | [diff] [blame] | 1086 | case NOP: |
| 1087 | goto fast_next_opcode; |
| 1088 | |
Neil Schemenauer | 6354386 | 2002-02-17 19:10:14 +0000 | [diff] [blame] | 1089 | case LOAD_FAST: |
| 1090 | x = GETLOCAL(oparg); |
| 1091 | if (x != NULL) { |
| 1092 | Py_INCREF(x); |
| 1093 | PUSH(x); |
| 1094 | goto fast_next_opcode; |
| 1095 | } |
| 1096 | format_exc_check_arg(PyExc_UnboundLocalError, |
| 1097 | UNBOUNDLOCAL_ERROR_MSG, |
| 1098 | PyTuple_GetItem(co->co_varnames, oparg)); |
| 1099 | break; |
| 1100 | |
| 1101 | case LOAD_CONST: |
Skip Montanaro | 04d80f8 | 2002-08-04 21:03:35 +0000 | [diff] [blame] | 1102 | x = GETITEM(consts, oparg); |
Neil Schemenauer | 6354386 | 2002-02-17 19:10:14 +0000 | [diff] [blame] | 1103 | Py_INCREF(x); |
| 1104 | PUSH(x); |
| 1105 | goto fast_next_opcode; |
| 1106 | |
Raymond Hettinger | 7dc5221 | 2003-03-16 20:14:44 +0000 | [diff] [blame] | 1107 | PREDICTED_WITH_ARG(STORE_FAST); |
Neil Schemenauer | 6354386 | 2002-02-17 19:10:14 +0000 | [diff] [blame] | 1108 | case STORE_FAST: |
| 1109 | v = POP(); |
| 1110 | SETLOCAL(oparg, v); |
| 1111 | goto fast_next_opcode; |
| 1112 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1113 | case POP_TOP: |
| 1114 | v = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1115 | Py_DECREF(v); |
Neil Schemenauer | 6354386 | 2002-02-17 19:10:14 +0000 | [diff] [blame] | 1116 | goto fast_next_opcode; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1117 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1118 | case ROT_TWO: |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1119 | v = TOP(); |
| 1120 | w = SECOND(); |
| 1121 | SET_TOP(w); |
| 1122 | SET_SECOND(v); |
Raymond Hettinger | 080cb32 | 2003-03-14 01:37:42 +0000 | [diff] [blame] | 1123 | goto fast_next_opcode; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1124 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1125 | case ROT_THREE: |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1126 | v = TOP(); |
| 1127 | w = SECOND(); |
| 1128 | x = THIRD(); |
| 1129 | SET_TOP(w); |
| 1130 | SET_SECOND(x); |
| 1131 | SET_THIRD(v); |
Raymond Hettinger | 080cb32 | 2003-03-14 01:37:42 +0000 | [diff] [blame] | 1132 | goto fast_next_opcode; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1133 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1134 | case ROT_FOUR: |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1135 | u = TOP(); |
| 1136 | v = SECOND(); |
| 1137 | w = THIRD(); |
| 1138 | x = FOURTH(); |
| 1139 | SET_TOP(v); |
| 1140 | SET_SECOND(w); |
| 1141 | SET_THIRD(x); |
| 1142 | SET_FOURTH(u); |
Raymond Hettinger | 080cb32 | 2003-03-14 01:37:42 +0000 | [diff] [blame] | 1143 | goto fast_next_opcode; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1144 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1145 | case DUP_TOP: |
| 1146 | v = TOP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1147 | Py_INCREF(v); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1148 | PUSH(v); |
Raymond Hettinger | 080cb32 | 2003-03-14 01:37:42 +0000 | [diff] [blame] | 1149 | goto fast_next_opcode; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1150 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1151 | case DUP_TOPX: |
Raymond Hettinger | 4bad9ba | 2003-01-19 05:08:13 +0000 | [diff] [blame] | 1152 | if (oparg == 2) { |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1153 | x = TOP(); |
Tim Peters | 35ba689 | 2000-10-11 07:04:49 +0000 | [diff] [blame] | 1154 | Py_INCREF(x); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1155 | w = SECOND(); |
Tim Peters | 35ba689 | 2000-10-11 07:04:49 +0000 | [diff] [blame] | 1156 | Py_INCREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1157 | STACKADJ(2); |
| 1158 | SET_TOP(x); |
| 1159 | SET_SECOND(w); |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 1160 | goto fast_next_opcode; |
Raymond Hettinger | 4bad9ba | 2003-01-19 05:08:13 +0000 | [diff] [blame] | 1161 | } else if (oparg == 3) { |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1162 | x = TOP(); |
Tim Peters | 35ba689 | 2000-10-11 07:04:49 +0000 | [diff] [blame] | 1163 | Py_INCREF(x); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1164 | w = SECOND(); |
Tim Peters | 35ba689 | 2000-10-11 07:04:49 +0000 | [diff] [blame] | 1165 | Py_INCREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1166 | v = THIRD(); |
Tim Peters | 35ba689 | 2000-10-11 07:04:49 +0000 | [diff] [blame] | 1167 | Py_INCREF(v); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1168 | STACKADJ(3); |
| 1169 | SET_TOP(x); |
| 1170 | SET_SECOND(w); |
| 1171 | SET_THIRD(v); |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 1172 | goto fast_next_opcode; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1173 | } |
Raymond Hettinger | 4bad9ba | 2003-01-19 05:08:13 +0000 | [diff] [blame] | 1174 | Py_FatalError("invalid argument to DUP_TOPX" |
| 1175 | " (bytecode corruption?)"); |
Jeffrey Yasskin | 2d873bd | 2008-12-08 18:55:24 +0000 | [diff] [blame] | 1176 | /* Never returns, so don't bother to set why. */ |
Tim Peters | 35ba689 | 2000-10-11 07:04:49 +0000 | [diff] [blame] | 1177 | break; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1178 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1179 | case UNARY_POSITIVE: |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1180 | v = TOP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1181 | x = PyNumber_Positive(v); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1182 | Py_DECREF(v); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1183 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1184 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1185 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1186 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1187 | case UNARY_NEGATIVE: |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1188 | v = TOP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1189 | x = PyNumber_Negative(v); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1190 | Py_DECREF(v); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1191 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1192 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1193 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1194 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1195 | case UNARY_NOT: |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1196 | v = TOP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1197 | err = PyObject_IsTrue(v); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1198 | Py_DECREF(v); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1199 | if (err == 0) { |
| 1200 | Py_INCREF(Py_True); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1201 | SET_TOP(Py_True); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1202 | continue; |
| 1203 | } |
| 1204 | else if (err > 0) { |
| 1205 | Py_INCREF(Py_False); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1206 | SET_TOP(Py_False); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1207 | err = 0; |
| 1208 | continue; |
| 1209 | } |
Raymond Hettinger | 8bb90a5 | 2003-01-14 12:43:10 +0000 | [diff] [blame] | 1210 | STACKADJ(-1); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1211 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1212 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1213 | case UNARY_CONVERT: |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1214 | v = TOP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1215 | x = PyObject_Repr(v); |
| 1216 | Py_DECREF(v); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1217 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1218 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1219 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1220 | |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1221 | case UNARY_INVERT: |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1222 | v = TOP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1223 | x = PyNumber_Invert(v); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1224 | Py_DECREF(v); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1225 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1226 | if (x != NULL) continue; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1227 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1228 | |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1229 | case BINARY_POWER: |
| 1230 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1231 | v = TOP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1232 | x = PyNumber_Power(v, w, Py_None); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1233 | Py_DECREF(v); |
| 1234 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1235 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1236 | if (x != NULL) continue; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1237 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1238 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1239 | case BINARY_MULTIPLY: |
| 1240 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1241 | v = TOP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1242 | x = PyNumber_Multiply(v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1243 | Py_DECREF(v); |
| 1244 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1245 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1246 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1247 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1248 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1249 | case BINARY_DIVIDE: |
Tim Peters | 3caca23 | 2001-12-06 06:23:26 +0000 | [diff] [blame] | 1250 | if (!_Py_QnewFlag) { |
| 1251 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1252 | v = TOP(); |
Tim Peters | 3caca23 | 2001-12-06 06:23:26 +0000 | [diff] [blame] | 1253 | x = PyNumber_Divide(v, w); |
| 1254 | Py_DECREF(v); |
| 1255 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1256 | SET_TOP(x); |
Tim Peters | 3caca23 | 2001-12-06 06:23:26 +0000 | [diff] [blame] | 1257 | if (x != NULL) continue; |
| 1258 | break; |
| 1259 | } |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1260 | /* -Qnew is in effect: fall through to |
Tim Peters | 3caca23 | 2001-12-06 06:23:26 +0000 | [diff] [blame] | 1261 | BINARY_TRUE_DIVIDE */ |
| 1262 | case BINARY_TRUE_DIVIDE: |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1263 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1264 | v = TOP(); |
Tim Peters | 3caca23 | 2001-12-06 06:23:26 +0000 | [diff] [blame] | 1265 | x = PyNumber_TrueDivide(v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1266 | Py_DECREF(v); |
| 1267 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1268 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1269 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1270 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1271 | |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1272 | case BINARY_FLOOR_DIVIDE: |
| 1273 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1274 | v = TOP(); |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1275 | x = PyNumber_FloorDivide(v, w); |
| 1276 | Py_DECREF(v); |
| 1277 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1278 | SET_TOP(x); |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1279 | if (x != NULL) continue; |
| 1280 | break; |
| 1281 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1282 | case BINARY_MODULO: |
| 1283 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1284 | v = TOP(); |
Collin Winter | 8725dce | 2009-02-20 19:30:41 +0000 | [diff] [blame] | 1285 | if (PyString_CheckExact(v)) |
| 1286 | x = PyString_Format(v, w); |
| 1287 | else |
| 1288 | x = PyNumber_Remainder(v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1289 | Py_DECREF(v); |
| 1290 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1291 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1292 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1293 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1294 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1295 | case BINARY_ADD: |
| 1296 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1297 | v = TOP(); |
Tim Peters | c1e6d96 | 2001-10-05 20:21:03 +0000 | [diff] [blame] | 1298 | if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) { |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1299 | /* INLINE: int + int */ |
| 1300 | register long a, b, i; |
Guido van Rossum | cf183ac | 1998-12-04 18:51:36 +0000 | [diff] [blame] | 1301 | a = PyInt_AS_LONG(v); |
| 1302 | b = PyInt_AS_LONG(w); |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1303 | i = a + b; |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1304 | if ((i^a) < 0 && (i^b) < 0) |
| 1305 | goto slow_add; |
| 1306 | x = PyInt_FromLong(i); |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1307 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1308 | else if (PyString_CheckExact(v) && |
| 1309 | PyString_CheckExact(w)) { |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 1310 | x = string_concatenate(v, w, f, next_instr); |
| 1311 | /* string_concatenate consumed the ref to v */ |
| 1312 | goto skip_decref_vx; |
| 1313 | } |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1314 | else { |
| 1315 | slow_add: |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1316 | x = PyNumber_Add(v, w); |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1317 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1318 | Py_DECREF(v); |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 1319 | skip_decref_vx: |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1320 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1321 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1322 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1323 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1324 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1325 | case BINARY_SUBTRACT: |
| 1326 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1327 | v = TOP(); |
Tim Peters | c1e6d96 | 2001-10-05 20:21:03 +0000 | [diff] [blame] | 1328 | if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) { |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1329 | /* INLINE: int - int */ |
| 1330 | register long a, b, i; |
Guido van Rossum | cf183ac | 1998-12-04 18:51:36 +0000 | [diff] [blame] | 1331 | a = PyInt_AS_LONG(v); |
| 1332 | b = PyInt_AS_LONG(w); |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1333 | i = a - b; |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1334 | if ((i^a) < 0 && (i^~b) < 0) |
| 1335 | goto slow_sub; |
| 1336 | x = PyInt_FromLong(i); |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1337 | } |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1338 | else { |
| 1339 | slow_sub: |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1340 | x = PyNumber_Subtract(v, w); |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1341 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1342 | Py_DECREF(v); |
| 1343 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1344 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1345 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1346 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1347 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1348 | case BINARY_SUBSCR: |
| 1349 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1350 | v = TOP(); |
Tim Peters | b1c4698 | 2001-10-05 20:41:38 +0000 | [diff] [blame] | 1351 | if (PyList_CheckExact(v) && PyInt_CheckExact(w)) { |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1352 | /* INLINE: list[int] */ |
Neal Norwitz | 814e938 | 2006-03-02 07:54:28 +0000 | [diff] [blame] | 1353 | Py_ssize_t i = PyInt_AsSsize_t(w); |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1354 | if (i < 0) |
Guido van Rossum | fa00e95 | 1998-07-08 15:02:37 +0000 | [diff] [blame] | 1355 | i += PyList_GET_SIZE(v); |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 1356 | if (i >= 0 && i < PyList_GET_SIZE(v)) { |
Guido van Rossum | fa00e95 | 1998-07-08 15:02:37 +0000 | [diff] [blame] | 1357 | x = PyList_GET_ITEM(v, i); |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1358 | Py_INCREF(x); |
| 1359 | } |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 1360 | else |
| 1361 | goto slow_get; |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1362 | } |
| 1363 | else |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 1364 | slow_get: |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1365 | x = PyObject_GetItem(v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1366 | Py_DECREF(v); |
| 1367 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1368 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1369 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1370 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1371 | |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1372 | case BINARY_LSHIFT: |
| 1373 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1374 | v = TOP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1375 | x = PyNumber_Lshift(v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1376 | Py_DECREF(v); |
| 1377 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1378 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1379 | if (x != NULL) continue; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1380 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1381 | |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1382 | case BINARY_RSHIFT: |
| 1383 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1384 | v = TOP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1385 | x = PyNumber_Rshift(v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1386 | Py_DECREF(v); |
| 1387 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1388 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1389 | if (x != NULL) continue; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1390 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1391 | |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1392 | case BINARY_AND: |
| 1393 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1394 | v = TOP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1395 | x = PyNumber_And(v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1396 | Py_DECREF(v); |
| 1397 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1398 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1399 | if (x != NULL) continue; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1400 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1401 | |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1402 | case BINARY_XOR: |
| 1403 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1404 | v = TOP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1405 | x = PyNumber_Xor(v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1406 | Py_DECREF(v); |
| 1407 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1408 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1409 | if (x != NULL) continue; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1410 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1411 | |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1412 | case BINARY_OR: |
| 1413 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1414 | v = TOP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1415 | x = PyNumber_Or(v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1416 | Py_DECREF(v); |
| 1417 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1418 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1419 | if (x != NULL) continue; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1420 | break; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1421 | |
Raymond Hettinger | dd80f76 | 2004-03-07 07:31:06 +0000 | [diff] [blame] | 1422 | case LIST_APPEND: |
| 1423 | w = POP(); |
Antoine Pitrou | d0c3515 | 2008-12-17 00:38:28 +0000 | [diff] [blame] | 1424 | v = stack_pointer[-oparg]; |
Raymond Hettinger | dd80f76 | 2004-03-07 07:31:06 +0000 | [diff] [blame] | 1425 | err = PyList_Append(v, w); |
Raymond Hettinger | dd80f76 | 2004-03-07 07:31:06 +0000 | [diff] [blame] | 1426 | Py_DECREF(w); |
Raymond Hettinger | fba1cfc | 2004-03-12 16:33:17 +0000 | [diff] [blame] | 1427 | if (err == 0) { |
| 1428 | PREDICT(JUMP_ABSOLUTE); |
| 1429 | continue; |
| 1430 | } |
Raymond Hettinger | dd80f76 | 2004-03-07 07:31:06 +0000 | [diff] [blame] | 1431 | break; |
| 1432 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1433 | case INPLACE_POWER: |
| 1434 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1435 | v = TOP(); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1436 | x = PyNumber_InPlacePower(v, w, Py_None); |
| 1437 | Py_DECREF(v); |
| 1438 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1439 | SET_TOP(x); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1440 | if (x != NULL) continue; |
| 1441 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1442 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1443 | case INPLACE_MULTIPLY: |
| 1444 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1445 | v = TOP(); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1446 | x = PyNumber_InPlaceMultiply(v, w); |
| 1447 | Py_DECREF(v); |
| 1448 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1449 | SET_TOP(x); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1450 | if (x != NULL) continue; |
| 1451 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1452 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1453 | case INPLACE_DIVIDE: |
Tim Peters | 54b1191 | 2001-12-25 18:49:11 +0000 | [diff] [blame] | 1454 | if (!_Py_QnewFlag) { |
| 1455 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1456 | v = TOP(); |
Tim Peters | 54b1191 | 2001-12-25 18:49:11 +0000 | [diff] [blame] | 1457 | x = PyNumber_InPlaceDivide(v, w); |
| 1458 | Py_DECREF(v); |
| 1459 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1460 | SET_TOP(x); |
Tim Peters | 54b1191 | 2001-12-25 18:49:11 +0000 | [diff] [blame] | 1461 | if (x != NULL) continue; |
| 1462 | break; |
| 1463 | } |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1464 | /* -Qnew is in effect: fall through to |
Tim Peters | 54b1191 | 2001-12-25 18:49:11 +0000 | [diff] [blame] | 1465 | INPLACE_TRUE_DIVIDE */ |
| 1466 | case INPLACE_TRUE_DIVIDE: |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1467 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1468 | v = TOP(); |
Tim Peters | 54b1191 | 2001-12-25 18:49:11 +0000 | [diff] [blame] | 1469 | x = PyNumber_InPlaceTrueDivide(v, w); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1470 | Py_DECREF(v); |
| 1471 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1472 | SET_TOP(x); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1473 | if (x != NULL) continue; |
| 1474 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1475 | |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1476 | case INPLACE_FLOOR_DIVIDE: |
| 1477 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1478 | v = TOP(); |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1479 | x = PyNumber_InPlaceFloorDivide(v, w); |
| 1480 | Py_DECREF(v); |
| 1481 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1482 | SET_TOP(x); |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1483 | if (x != NULL) continue; |
| 1484 | break; |
| 1485 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1486 | case INPLACE_MODULO: |
| 1487 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1488 | v = TOP(); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1489 | x = PyNumber_InPlaceRemainder(v, w); |
| 1490 | Py_DECREF(v); |
| 1491 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1492 | SET_TOP(x); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1493 | if (x != NULL) continue; |
| 1494 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1495 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1496 | case INPLACE_ADD: |
| 1497 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1498 | v = TOP(); |
Tim Peters | c1e6d96 | 2001-10-05 20:21:03 +0000 | [diff] [blame] | 1499 | if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1500 | /* INLINE: int + int */ |
| 1501 | register long a, b, i; |
| 1502 | a = PyInt_AS_LONG(v); |
| 1503 | b = PyInt_AS_LONG(w); |
| 1504 | i = a + b; |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1505 | if ((i^a) < 0 && (i^b) < 0) |
| 1506 | goto slow_iadd; |
| 1507 | x = PyInt_FromLong(i); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1508 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1509 | else if (PyString_CheckExact(v) && |
| 1510 | PyString_CheckExact(w)) { |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 1511 | x = string_concatenate(v, w, f, next_instr); |
| 1512 | /* string_concatenate consumed the ref to v */ |
| 1513 | goto skip_decref_v; |
| 1514 | } |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1515 | else { |
| 1516 | slow_iadd: |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1517 | x = PyNumber_InPlaceAdd(v, w); |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1518 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1519 | Py_DECREF(v); |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 1520 | skip_decref_v: |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1521 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1522 | SET_TOP(x); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1523 | if (x != NULL) continue; |
| 1524 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1525 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1526 | case INPLACE_SUBTRACT: |
| 1527 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1528 | v = TOP(); |
Tim Peters | c1e6d96 | 2001-10-05 20:21:03 +0000 | [diff] [blame] | 1529 | if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1530 | /* INLINE: int - int */ |
| 1531 | register long a, b, i; |
| 1532 | a = PyInt_AS_LONG(v); |
| 1533 | b = PyInt_AS_LONG(w); |
| 1534 | i = a - b; |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1535 | if ((i^a) < 0 && (i^~b) < 0) |
| 1536 | goto slow_isub; |
| 1537 | x = PyInt_FromLong(i); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1538 | } |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1539 | else { |
| 1540 | slow_isub: |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1541 | x = PyNumber_InPlaceSubtract(v, w); |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1542 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1543 | Py_DECREF(v); |
| 1544 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1545 | SET_TOP(x); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1546 | if (x != NULL) continue; |
| 1547 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1548 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1549 | case INPLACE_LSHIFT: |
| 1550 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1551 | v = TOP(); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1552 | x = PyNumber_InPlaceLshift(v, w); |
| 1553 | Py_DECREF(v); |
| 1554 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1555 | SET_TOP(x); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1556 | if (x != NULL) continue; |
| 1557 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1558 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1559 | case INPLACE_RSHIFT: |
| 1560 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1561 | v = TOP(); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1562 | x = PyNumber_InPlaceRshift(v, w); |
| 1563 | Py_DECREF(v); |
| 1564 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1565 | SET_TOP(x); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1566 | if (x != NULL) continue; |
| 1567 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1568 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1569 | case INPLACE_AND: |
| 1570 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1571 | v = TOP(); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1572 | x = PyNumber_InPlaceAnd(v, w); |
| 1573 | Py_DECREF(v); |
| 1574 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1575 | SET_TOP(x); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1576 | if (x != NULL) continue; |
| 1577 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1578 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1579 | case INPLACE_XOR: |
| 1580 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1581 | v = TOP(); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1582 | x = PyNumber_InPlaceXor(v, w); |
| 1583 | Py_DECREF(v); |
| 1584 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1585 | SET_TOP(x); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1586 | if (x != NULL) continue; |
| 1587 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1588 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1589 | case INPLACE_OR: |
| 1590 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1591 | v = TOP(); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1592 | x = PyNumber_InPlaceOr(v, w); |
| 1593 | Py_DECREF(v); |
| 1594 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1595 | SET_TOP(x); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1596 | if (x != NULL) continue; |
| 1597 | break; |
| 1598 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1599 | case SLICE+0: |
| 1600 | case SLICE+1: |
| 1601 | case SLICE+2: |
| 1602 | case SLICE+3: |
| 1603 | if ((opcode-SLICE) & 2) |
| 1604 | w = POP(); |
| 1605 | else |
| 1606 | w = NULL; |
| 1607 | if ((opcode-SLICE) & 1) |
| 1608 | v = POP(); |
| 1609 | else |
| 1610 | v = NULL; |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1611 | u = TOP(); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1612 | x = apply_slice(u, v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1613 | Py_DECREF(u); |
| 1614 | Py_XDECREF(v); |
| 1615 | Py_XDECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1616 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1617 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1618 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1619 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1620 | case STORE_SLICE+0: |
| 1621 | case STORE_SLICE+1: |
| 1622 | case STORE_SLICE+2: |
| 1623 | case STORE_SLICE+3: |
| 1624 | if ((opcode-STORE_SLICE) & 2) |
| 1625 | w = POP(); |
| 1626 | else |
| 1627 | w = NULL; |
| 1628 | if ((opcode-STORE_SLICE) & 1) |
| 1629 | v = POP(); |
| 1630 | else |
| 1631 | v = NULL; |
| 1632 | u = POP(); |
| 1633 | t = POP(); |
| 1634 | err = assign_slice(u, v, w, t); /* u[v:w] = t */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1635 | Py_DECREF(t); |
| 1636 | Py_DECREF(u); |
| 1637 | Py_XDECREF(v); |
| 1638 | Py_XDECREF(w); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1639 | if (err == 0) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1640 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1641 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1642 | case DELETE_SLICE+0: |
| 1643 | case DELETE_SLICE+1: |
| 1644 | case DELETE_SLICE+2: |
| 1645 | case DELETE_SLICE+3: |
| 1646 | if ((opcode-DELETE_SLICE) & 2) |
| 1647 | w = POP(); |
| 1648 | else |
| 1649 | w = NULL; |
| 1650 | if ((opcode-DELETE_SLICE) & 1) |
| 1651 | v = POP(); |
| 1652 | else |
| 1653 | v = NULL; |
| 1654 | u = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1655 | err = assign_slice(u, v, w, (PyObject *)NULL); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1656 | /* del u[v:w] */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1657 | Py_DECREF(u); |
| 1658 | Py_XDECREF(v); |
| 1659 | Py_XDECREF(w); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1660 | if (err == 0) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1661 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1662 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1663 | case STORE_SUBSCR: |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1664 | w = TOP(); |
| 1665 | v = SECOND(); |
| 1666 | u = THIRD(); |
| 1667 | STACKADJ(-3); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1668 | /* v[w] = u */ |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1669 | err = PyObject_SetItem(v, w, u); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1670 | Py_DECREF(u); |
| 1671 | Py_DECREF(v); |
| 1672 | Py_DECREF(w); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1673 | if (err == 0) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1674 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1675 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1676 | case DELETE_SUBSCR: |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1677 | w = TOP(); |
| 1678 | v = SECOND(); |
| 1679 | STACKADJ(-2); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1680 | /* del v[w] */ |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1681 | err = PyObject_DelItem(v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1682 | Py_DECREF(v); |
| 1683 | Py_DECREF(w); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1684 | if (err == 0) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1685 | break; |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1686 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1687 | case PRINT_EXPR: |
| 1688 | v = POP(); |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 1689 | w = PySys_GetObject("displayhook"); |
| 1690 | if (w == NULL) { |
| 1691 | PyErr_SetString(PyExc_RuntimeError, |
| 1692 | "lost sys.displayhook"); |
| 1693 | err = -1; |
Moshe Zadka | f5df383 | 2001-01-11 11:55:37 +0000 | [diff] [blame] | 1694 | x = NULL; |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 1695 | } |
| 1696 | if (err == 0) { |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 1697 | x = PyTuple_Pack(1, v); |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 1698 | if (x == NULL) |
| 1699 | err = -1; |
| 1700 | } |
| 1701 | if (err == 0) { |
| 1702 | w = PyEval_CallObject(w, x); |
Moshe Zadka | f5df383 | 2001-01-11 11:55:37 +0000 | [diff] [blame] | 1703 | Py_XDECREF(w); |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 1704 | if (w == NULL) |
| 1705 | err = -1; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1706 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1707 | Py_DECREF(v); |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 1708 | Py_XDECREF(x); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1709 | break; |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 1710 | |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1711 | case PRINT_ITEM_TO: |
| 1712 | w = stream = POP(); |
| 1713 | /* fall through to PRINT_ITEM */ |
| 1714 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1715 | case PRINT_ITEM: |
| 1716 | v = POP(); |
Barry Warsaw | 093abe0 | 2000-08-29 04:56:13 +0000 | [diff] [blame] | 1717 | if (stream == NULL || stream == Py_None) { |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1718 | w = PySys_GetObject("stdout"); |
| 1719 | if (w == NULL) { |
| 1720 | PyErr_SetString(PyExc_RuntimeError, |
| 1721 | "lost sys.stdout"); |
| 1722 | err = -1; |
| 1723 | } |
Guido van Rossum | 8f18320 | 1997-12-31 05:53:15 +0000 | [diff] [blame] | 1724 | } |
Neal Norwitz | c5131bc | 2003-06-29 14:48:32 +0000 | [diff] [blame] | 1725 | /* PyFile_SoftSpace() can exececute arbitrary code |
| 1726 | if sys.stdout is an instance with a __getattr__. |
| 1727 | If __getattr__ raises an exception, w will |
| 1728 | be freed, so we need to prevent that temporarily. */ |
| 1729 | Py_XINCREF(w); |
Tim Peters | 8e5fd53 | 2002-03-24 19:25:00 +0000 | [diff] [blame] | 1730 | if (w != NULL && PyFile_SoftSpace(w, 0)) |
Guido van Rossum | be27026 | 1997-05-22 22:26:18 +0000 | [diff] [blame] | 1731 | err = PyFile_WriteString(" ", w); |
| 1732 | if (err == 0) |
| 1733 | err = PyFile_WriteObject(v, w, Py_PRINT_RAW); |
Marc-André Lemburg | 0c4d8d0 | 2001-11-20 15:17:25 +0000 | [diff] [blame] | 1734 | if (err == 0) { |
Tim Peters | 8e5fd53 | 2002-03-24 19:25:00 +0000 | [diff] [blame] | 1735 | /* XXX move into writeobject() ? */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1736 | if (PyString_Check(v)) { |
| 1737 | char *s = PyString_AS_STRING(v); |
| 1738 | Py_ssize_t len = PyString_GET_SIZE(v); |
Tim Peters | 8e5fd53 | 2002-03-24 19:25:00 +0000 | [diff] [blame] | 1739 | if (len == 0 || |
| 1740 | !isspace(Py_CHARMASK(s[len-1])) || |
| 1741 | s[len-1] == ' ') |
| 1742 | PyFile_SoftSpace(w, 1); |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 1743 | } |
Martin v. Löwis | 8d3ce5a | 2001-12-18 22:36:40 +0000 | [diff] [blame] | 1744 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | 0c4d8d0 | 2001-11-20 15:17:25 +0000 | [diff] [blame] | 1745 | else if (PyUnicode_Check(v)) { |
| 1746 | Py_UNICODE *s = PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 6685128 | 2006-04-22 11:40:03 +0000 | [diff] [blame] | 1747 | Py_ssize_t len = PyUnicode_GET_SIZE(v); |
Tim Peters | 8e5fd53 | 2002-03-24 19:25:00 +0000 | [diff] [blame] | 1748 | if (len == 0 || |
| 1749 | !Py_UNICODE_ISSPACE(s[len-1]) || |
| 1750 | s[len-1] == ' ') |
| 1751 | PyFile_SoftSpace(w, 1); |
Marc-André Lemburg | 0c4d8d0 | 2001-11-20 15:17:25 +0000 | [diff] [blame] | 1752 | } |
Michael W. Hudson | d95c828 | 2002-05-20 13:56:11 +0000 | [diff] [blame] | 1753 | #endif |
Tim Peters | 8e5fd53 | 2002-03-24 19:25:00 +0000 | [diff] [blame] | 1754 | else |
| 1755 | PyFile_SoftSpace(w, 1); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1756 | } |
Neal Norwitz | c5131bc | 2003-06-29 14:48:32 +0000 | [diff] [blame] | 1757 | Py_XDECREF(w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1758 | Py_DECREF(v); |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1759 | Py_XDECREF(stream); |
| 1760 | stream = NULL; |
| 1761 | if (err == 0) |
| 1762 | continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1763 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1764 | |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1765 | case PRINT_NEWLINE_TO: |
| 1766 | w = stream = POP(); |
| 1767 | /* fall through to PRINT_NEWLINE */ |
| 1768 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1769 | case PRINT_NEWLINE: |
Barry Warsaw | 093abe0 | 2000-08-29 04:56:13 +0000 | [diff] [blame] | 1770 | if (stream == NULL || stream == Py_None) { |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1771 | w = PySys_GetObject("stdout"); |
Jeffrey Yasskin | 2d873bd | 2008-12-08 18:55:24 +0000 | [diff] [blame] | 1772 | if (w == NULL) { |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1773 | PyErr_SetString(PyExc_RuntimeError, |
| 1774 | "lost sys.stdout"); |
Jeffrey Yasskin | 2d873bd | 2008-12-08 18:55:24 +0000 | [diff] [blame] | 1775 | why = WHY_EXCEPTION; |
| 1776 | } |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1777 | } |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1778 | if (w != NULL) { |
Georg Brandl | aa76d77 | 2008-07-01 20:56:03 +0000 | [diff] [blame] | 1779 | /* w.write() may replace sys.stdout, so we |
| 1780 | * have to keep our reference to it */ |
Amaury Forgeot d'Arc | bdd941f | 2008-07-01 20:38:04 +0000 | [diff] [blame] | 1781 | Py_INCREF(w); |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1782 | err = PyFile_WriteString("\n", w); |
| 1783 | if (err == 0) |
| 1784 | PyFile_SoftSpace(w, 0); |
Amaury Forgeot d'Arc | bdd941f | 2008-07-01 20:38:04 +0000 | [diff] [blame] | 1785 | Py_DECREF(w); |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1786 | } |
| 1787 | Py_XDECREF(stream); |
| 1788 | stream = NULL; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1789 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1790 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1791 | |
| 1792 | #ifdef CASE_TOO_BIG |
| 1793 | default: switch (opcode) { |
| 1794 | #endif |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1795 | case RAISE_VARARGS: |
| 1796 | u = v = w = NULL; |
| 1797 | switch (oparg) { |
| 1798 | case 3: |
| 1799 | u = POP(); /* traceback */ |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1800 | /* Fallthrough */ |
| 1801 | case 2: |
| 1802 | v = POP(); /* value */ |
| 1803 | /* Fallthrough */ |
| 1804 | case 1: |
| 1805 | w = POP(); /* exc */ |
Guido van Rossum | d295f12 | 1998-04-09 21:39:57 +0000 | [diff] [blame] | 1806 | case 0: /* Fallthrough */ |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 1807 | why = do_raise(w, v, u); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1808 | break; |
| 1809 | default: |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1810 | PyErr_SetString(PyExc_SystemError, |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1811 | "bad RAISE_VARARGS oparg"); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1812 | why = WHY_EXCEPTION; |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 1813 | break; |
| 1814 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1815 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1816 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1817 | case LOAD_LOCALS: |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 1818 | if ((x = f->f_locals) != NULL) { |
| 1819 | Py_INCREF(x); |
| 1820 | PUSH(x); |
| 1821 | continue; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1822 | } |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 1823 | PyErr_SetString(PyExc_SystemError, "no locals"); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1824 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1825 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1826 | case RETURN_VALUE: |
| 1827 | retval = POP(); |
| 1828 | why = WHY_RETURN; |
Raymond Hettinger | 1dd8309 | 2004-02-06 18:32:33 +0000 | [diff] [blame] | 1829 | goto fast_block_end; |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 1830 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1831 | case YIELD_VALUE: |
| 1832 | retval = POP(); |
Tim Peters | 8c96369 | 2001-06-23 05:26:56 +0000 | [diff] [blame] | 1833 | f->f_stacktop = stack_pointer; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1834 | why = WHY_YIELD; |
Raymond Hettinger | 1dd8309 | 2004-02-06 18:32:33 +0000 | [diff] [blame] | 1835 | goto fast_yield; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1836 | |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 1837 | case EXEC_STMT: |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1838 | w = TOP(); |
| 1839 | v = SECOND(); |
| 1840 | u = THIRD(); |
| 1841 | STACKADJ(-3); |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 1842 | READ_TIMESTAMP(intr0); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 1843 | err = exec_statement(f, u, v, w); |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 1844 | READ_TIMESTAMP(intr1); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1845 | Py_DECREF(u); |
| 1846 | Py_DECREF(v); |
| 1847 | Py_DECREF(w); |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 1848 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1849 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1850 | case POP_BLOCK: |
| 1851 | { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1852 | PyTryBlock *b = PyFrame_BlockPop(f); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1853 | while (STACK_LEVEL() > b->b_level) { |
| 1854 | v = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1855 | Py_DECREF(v); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1856 | } |
| 1857 | } |
Raymond Hettinger | 7eddd78 | 2004-04-07 14:38:08 +0000 | [diff] [blame] | 1858 | continue; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1859 | |
Jeffrey Yasskin | 9063a99 | 2008-03-03 01:27:03 +0000 | [diff] [blame] | 1860 | PREDICTED(END_FINALLY); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1861 | case END_FINALLY: |
| 1862 | v = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1863 | if (PyInt_Check(v)) { |
Raymond Hettinger | 7c95865 | 2004-04-06 10:11:10 +0000 | [diff] [blame] | 1864 | why = (enum why_code) PyInt_AS_LONG(v); |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 1865 | assert(why != WHY_YIELD); |
Raymond Hettinger | c8aa08b | 2004-04-11 14:59:33 +0000 | [diff] [blame] | 1866 | if (why == WHY_RETURN || |
| 1867 | why == WHY_CONTINUE) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1868 | retval = POP(); |
| 1869 | } |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 1870 | else if (PyExceptionClass_Check(v) || |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1871 | PyString_Check(v)) { |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1872 | w = POP(); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1873 | u = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1874 | PyErr_Restore(v, w, u); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1875 | why = WHY_RERAISE; |
Guido van Rossum | 0db1ef9 | 1995-07-28 23:06:00 +0000 | [diff] [blame] | 1876 | break; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1877 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1878 | else if (v != Py_None) { |
| 1879 | PyErr_SetString(PyExc_SystemError, |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1880 | "'finally' pops bad exception"); |
| 1881 | why = WHY_EXCEPTION; |
| 1882 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1883 | Py_DECREF(v); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1884 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1885 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1886 | case BUILD_CLASS: |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1887 | u = TOP(); |
| 1888 | v = SECOND(); |
| 1889 | w = THIRD(); |
| 1890 | STACKADJ(-2); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1891 | x = build_class(u, v, w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1892 | SET_TOP(x); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1893 | Py_DECREF(u); |
| 1894 | Py_DECREF(v); |
| 1895 | Py_DECREF(w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1896 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1897 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1898 | case STORE_NAME: |
Skip Montanaro | 496e658 | 2002-08-06 17:47:40 +0000 | [diff] [blame] | 1899 | w = GETITEM(names, oparg); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1900 | v = POP(); |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 1901 | if ((x = f->f_locals) != NULL) { |
Raymond Hettinger | 66bd233 | 2004-08-02 08:30:07 +0000 | [diff] [blame] | 1902 | if (PyDict_CheckExact(x)) |
Raymond Hettinger | 214b1c3 | 2004-07-02 06:41:07 +0000 | [diff] [blame] | 1903 | err = PyDict_SetItem(x, w, v); |
| 1904 | else |
| 1905 | err = PyObject_SetItem(x, w, v); |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 1906 | Py_DECREF(v); |
Raymond Hettinger | 7eddd78 | 2004-04-07 14:38:08 +0000 | [diff] [blame] | 1907 | if (err == 0) continue; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1908 | break; |
| 1909 | } |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 1910 | PyErr_Format(PyExc_SystemError, |
| 1911 | "no locals found when storing %s", |
| 1912 | PyObject_REPR(w)); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1913 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1914 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1915 | case DELETE_NAME: |
Skip Montanaro | 496e658 | 2002-08-06 17:47:40 +0000 | [diff] [blame] | 1916 | w = GETITEM(names, oparg); |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 1917 | if ((x = f->f_locals) != NULL) { |
Raymond Hettinger | 214b1c3 | 2004-07-02 06:41:07 +0000 | [diff] [blame] | 1918 | if ((err = PyObject_DelItem(x, w)) != 0) |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 1919 | format_exc_check_arg(PyExc_NameError, |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 1920 | NAME_ERROR_MSG, |
| 1921 | w); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1922 | break; |
| 1923 | } |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 1924 | PyErr_Format(PyExc_SystemError, |
| 1925 | "no locals when deleting %s", |
| 1926 | PyObject_REPR(w)); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1927 | break; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1928 | |
Raymond Hettinger | 7dc5221 | 2003-03-16 20:14:44 +0000 | [diff] [blame] | 1929 | PREDICTED_WITH_ARG(UNPACK_SEQUENCE); |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 1930 | case UNPACK_SEQUENCE: |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1931 | v = POP(); |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 1932 | if (PyTuple_CheckExact(v) && |
| 1933 | PyTuple_GET_SIZE(v) == oparg) { |
| 1934 | PyObject **items = \ |
| 1935 | ((PyTupleObject *)v)->ob_item; |
Raymond Hettinger | f114a3a | 2004-03-08 23:25:30 +0000 | [diff] [blame] | 1936 | while (oparg--) { |
| 1937 | w = items[oparg]; |
| 1938 | Py_INCREF(w); |
| 1939 | PUSH(w); |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 1940 | } |
Raymond Hettinger | 7eddd78 | 2004-04-07 14:38:08 +0000 | [diff] [blame] | 1941 | Py_DECREF(v); |
| 1942 | continue; |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 1943 | } else if (PyList_CheckExact(v) && |
| 1944 | PyList_GET_SIZE(v) == oparg) { |
| 1945 | PyObject **items = \ |
| 1946 | ((PyListObject *)v)->ob_item; |
Raymond Hettinger | f114a3a | 2004-03-08 23:25:30 +0000 | [diff] [blame] | 1947 | while (oparg--) { |
| 1948 | w = items[oparg]; |
| 1949 | Py_INCREF(w); |
| 1950 | PUSH(w); |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 1951 | } |
Raymond Hettinger | f114a3a | 2004-03-08 23:25:30 +0000 | [diff] [blame] | 1952 | } else if (unpack_iterable(v, oparg, |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 1953 | stack_pointer + oparg)) { |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 1954 | stack_pointer += oparg; |
Georg Brandl | 5cb76c1 | 2007-03-21 09:00:39 +0000 | [diff] [blame] | 1955 | } else { |
| 1956 | /* unpack_iterable() raised an exception */ |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 1957 | why = WHY_EXCEPTION; |
Tim Peters | 8b13b3e | 2001-09-30 05:58:42 +0000 | [diff] [blame] | 1958 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1959 | Py_DECREF(v); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1960 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1961 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1962 | case STORE_ATTR: |
Skip Montanaro | 496e658 | 2002-08-06 17:47:40 +0000 | [diff] [blame] | 1963 | w = GETITEM(names, oparg); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1964 | v = TOP(); |
| 1965 | u = SECOND(); |
| 1966 | STACKADJ(-2); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1967 | err = PyObject_SetAttr(v, w, u); /* v.w = u */ |
| 1968 | Py_DECREF(v); |
| 1969 | Py_DECREF(u); |
Raymond Hettinger | 7eddd78 | 2004-04-07 14:38:08 +0000 | [diff] [blame] | 1970 | if (err == 0) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1971 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1972 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1973 | case DELETE_ATTR: |
Skip Montanaro | 496e658 | 2002-08-06 17:47:40 +0000 | [diff] [blame] | 1974 | w = GETITEM(names, oparg); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1975 | v = POP(); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 1976 | err = PyObject_SetAttr(v, w, (PyObject *)NULL); |
| 1977 | /* del v.w */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1978 | Py_DECREF(v); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1979 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1980 | |
Guido van Rossum | 32c6cdf | 1991-12-10 13:52:46 +0000 | [diff] [blame] | 1981 | case STORE_GLOBAL: |
Skip Montanaro | 496e658 | 2002-08-06 17:47:40 +0000 | [diff] [blame] | 1982 | w = GETITEM(names, oparg); |
Guido van Rossum | 32c6cdf | 1991-12-10 13:52:46 +0000 | [diff] [blame] | 1983 | v = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1984 | err = PyDict_SetItem(f->f_globals, w, v); |
| 1985 | Py_DECREF(v); |
Raymond Hettinger | 7eddd78 | 2004-04-07 14:38:08 +0000 | [diff] [blame] | 1986 | if (err == 0) continue; |
Guido van Rossum | 32c6cdf | 1991-12-10 13:52:46 +0000 | [diff] [blame] | 1987 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1988 | |
Guido van Rossum | 32c6cdf | 1991-12-10 13:52:46 +0000 | [diff] [blame] | 1989 | case DELETE_GLOBAL: |
Skip Montanaro | 496e658 | 2002-08-06 17:47:40 +0000 | [diff] [blame] | 1990 | w = GETITEM(names, oparg); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1991 | if ((err = PyDict_DelItem(f->f_globals, w)) != 0) |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 1992 | format_exc_check_arg( |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1993 | PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w); |
Guido van Rossum | 32c6cdf | 1991-12-10 13:52:46 +0000 | [diff] [blame] | 1994 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1995 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1996 | case LOAD_NAME: |
Skip Montanaro | 496e658 | 2002-08-06 17:47:40 +0000 | [diff] [blame] | 1997 | w = GETITEM(names, oparg); |
Raymond Hettinger | 214b1c3 | 2004-07-02 06:41:07 +0000 | [diff] [blame] | 1998 | if ((v = f->f_locals) == NULL) { |
Jeremy Hylton | c862cf4 | 2001-01-19 03:25:05 +0000 | [diff] [blame] | 1999 | PyErr_Format(PyExc_SystemError, |
| 2000 | "no locals when loading %s", |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 2001 | PyObject_REPR(w)); |
Jeffrey Yasskin | 2d873bd | 2008-12-08 18:55:24 +0000 | [diff] [blame] | 2002 | why = WHY_EXCEPTION; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2003 | break; |
| 2004 | } |
Michael W. Hudson | a3711f7 | 2004-08-02 14:50:43 +0000 | [diff] [blame] | 2005 | if (PyDict_CheckExact(v)) { |
Raymond Hettinger | 214b1c3 | 2004-07-02 06:41:07 +0000 | [diff] [blame] | 2006 | x = PyDict_GetItem(v, w); |
Michael W. Hudson | a3711f7 | 2004-08-02 14:50:43 +0000 | [diff] [blame] | 2007 | Py_XINCREF(x); |
| 2008 | } |
Raymond Hettinger | 214b1c3 | 2004-07-02 06:41:07 +0000 | [diff] [blame] | 2009 | else { |
| 2010 | x = PyObject_GetItem(v, w); |
| 2011 | if (x == NULL && PyErr_Occurred()) { |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 2012 | if (!PyErr_ExceptionMatches( |
| 2013 | PyExc_KeyError)) |
Raymond Hettinger | 214b1c3 | 2004-07-02 06:41:07 +0000 | [diff] [blame] | 2014 | break; |
| 2015 | PyErr_Clear(); |
| 2016 | } |
| 2017 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2018 | if (x == NULL) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2019 | x = PyDict_GetItem(f->f_globals, w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2020 | if (x == NULL) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2021 | x = PyDict_GetItem(f->f_builtins, w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2022 | if (x == NULL) { |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 2023 | format_exc_check_arg( |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2024 | PyExc_NameError, |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 2025 | NAME_ERROR_MSG, w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2026 | break; |
| 2027 | } |
| 2028 | } |
Michael W. Hudson | a3711f7 | 2004-08-02 14:50:43 +0000 | [diff] [blame] | 2029 | Py_INCREF(x); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2030 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2031 | PUSH(x); |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 2032 | continue; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2033 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2034 | case LOAD_GLOBAL: |
Skip Montanaro | 496e658 | 2002-08-06 17:47:40 +0000 | [diff] [blame] | 2035 | w = GETITEM(names, oparg); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2036 | if (PyString_CheckExact(w)) { |
Guido van Rossum | d8dbf84 | 2002-08-19 21:17:53 +0000 | [diff] [blame] | 2037 | /* Inline the PyDict_GetItem() calls. |
| 2038 | WARNING: this is an extreme speed hack. |
| 2039 | Do not try this at home. */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2040 | long hash = ((PyStringObject *)w)->ob_shash; |
Guido van Rossum | 3a4dfc8 | 2002-08-19 20:24:07 +0000 | [diff] [blame] | 2041 | if (hash != -1) { |
Guido van Rossum | 3a4dfc8 | 2002-08-19 20:24:07 +0000 | [diff] [blame] | 2042 | PyDictObject *d; |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 2043 | PyDictEntry *e; |
Guido van Rossum | 3a4dfc8 | 2002-08-19 20:24:07 +0000 | [diff] [blame] | 2044 | d = (PyDictObject *)(f->f_globals); |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 2045 | e = d->ma_lookup(d, w, hash); |
| 2046 | if (e == NULL) { |
| 2047 | x = NULL; |
| 2048 | break; |
| 2049 | } |
| 2050 | x = e->me_value; |
Guido van Rossum | 3a4dfc8 | 2002-08-19 20:24:07 +0000 | [diff] [blame] | 2051 | if (x != NULL) { |
| 2052 | Py_INCREF(x); |
| 2053 | PUSH(x); |
| 2054 | continue; |
| 2055 | } |
| 2056 | d = (PyDictObject *)(f->f_builtins); |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 2057 | e = d->ma_lookup(d, w, hash); |
| 2058 | if (e == NULL) { |
| 2059 | x = NULL; |
| 2060 | break; |
| 2061 | } |
| 2062 | x = e->me_value; |
Guido van Rossum | 3a4dfc8 | 2002-08-19 20:24:07 +0000 | [diff] [blame] | 2063 | if (x != NULL) { |
| 2064 | Py_INCREF(x); |
| 2065 | PUSH(x); |
| 2066 | continue; |
| 2067 | } |
| 2068 | goto load_global_error; |
| 2069 | } |
| 2070 | } |
| 2071 | /* This is the un-inlined version of the code above */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2072 | x = PyDict_GetItem(f->f_globals, w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2073 | if (x == NULL) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2074 | x = PyDict_GetItem(f->f_builtins, w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2075 | if (x == NULL) { |
Guido van Rossum | 3a4dfc8 | 2002-08-19 20:24:07 +0000 | [diff] [blame] | 2076 | load_global_error: |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 2077 | format_exc_check_arg( |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2078 | PyExc_NameError, |
Guido van Rossum | 3a4dfc8 | 2002-08-19 20:24:07 +0000 | [diff] [blame] | 2079 | GLOBAL_NAME_ERROR_MSG, w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2080 | break; |
| 2081 | } |
| 2082 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2083 | Py_INCREF(x); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2084 | PUSH(x); |
Raymond Hettinger | 7eddd78 | 2004-04-07 14:38:08 +0000 | [diff] [blame] | 2085 | continue; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2086 | |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2087 | case DELETE_FAST: |
Guido van Rossum | 2e4c899 | 1998-05-12 20:27:36 +0000 | [diff] [blame] | 2088 | x = GETLOCAL(oparg); |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 2089 | if (x != NULL) { |
| 2090 | SETLOCAL(oparg, NULL); |
| 2091 | continue; |
Guido van Rossum | 2e4c899 | 1998-05-12 20:27:36 +0000 | [diff] [blame] | 2092 | } |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 2093 | format_exc_check_arg( |
| 2094 | PyExc_UnboundLocalError, |
| 2095 | UNBOUNDLOCAL_ERROR_MSG, |
| 2096 | PyTuple_GetItem(co->co_varnames, oparg) |
| 2097 | ); |
| 2098 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2099 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2100 | case LOAD_CLOSURE: |
Jeremy Hylton | 2b724da | 2001-01-29 22:51:52 +0000 | [diff] [blame] | 2101 | x = freevars[oparg]; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2102 | Py_INCREF(x); |
| 2103 | PUSH(x); |
Raymond Hettinger | 7eddd78 | 2004-04-07 14:38:08 +0000 | [diff] [blame] | 2104 | if (x != NULL) continue; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2105 | break; |
| 2106 | |
| 2107 | case LOAD_DEREF: |
Jeremy Hylton | 2b724da | 2001-01-29 22:51:52 +0000 | [diff] [blame] | 2108 | x = freevars[oparg]; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2109 | w = PyCell_Get(x); |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 2110 | if (w != NULL) { |
| 2111 | PUSH(w); |
| 2112 | continue; |
Jeremy Hylton | 2524d69 | 2001-02-05 17:23:16 +0000 | [diff] [blame] | 2113 | } |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 2114 | err = -1; |
| 2115 | /* Don't stomp existing exception */ |
| 2116 | if (PyErr_Occurred()) |
| 2117 | break; |
Richard Jones | cebbefc | 2006-05-23 18:28:17 +0000 | [diff] [blame] | 2118 | if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) { |
| 2119 | v = PyTuple_GET_ITEM(co->co_cellvars, |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 2120 | oparg); |
| 2121 | format_exc_check_arg( |
| 2122 | PyExc_UnboundLocalError, |
| 2123 | UNBOUNDLOCAL_ERROR_MSG, |
| 2124 | v); |
| 2125 | } else { |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 2126 | v = PyTuple_GET_ITEM(co->co_freevars, oparg - |
| 2127 | PyTuple_GET_SIZE(co->co_cellvars)); |
| 2128 | format_exc_check_arg(PyExc_NameError, |
| 2129 | UNBOUNDFREE_ERROR_MSG, v); |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 2130 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2131 | break; |
| 2132 | |
| 2133 | case STORE_DEREF: |
| 2134 | w = POP(); |
Jeremy Hylton | 2b724da | 2001-01-29 22:51:52 +0000 | [diff] [blame] | 2135 | x = freevars[oparg]; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2136 | PyCell_Set(x, w); |
Jeremy Hylton | 30c9f39 | 2001-03-13 01:58:22 +0000 | [diff] [blame] | 2137 | Py_DECREF(w); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2138 | continue; |
| 2139 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2140 | case BUILD_TUPLE: |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2141 | x = PyTuple_New(oparg); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2142 | if (x != NULL) { |
Raymond Hettinger | 5bed456 | 2004-04-10 23:34:17 +0000 | [diff] [blame] | 2143 | for (; --oparg >= 0;) { |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2144 | w = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2145 | PyTuple_SET_ITEM(x, oparg, w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2146 | } |
| 2147 | PUSH(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 2148 | continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2149 | } |
| 2150 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2151 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2152 | case BUILD_LIST: |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2153 | x = PyList_New(oparg); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2154 | if (x != NULL) { |
Raymond Hettinger | 5bed456 | 2004-04-10 23:34:17 +0000 | [diff] [blame] | 2155 | for (; --oparg >= 0;) { |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2156 | w = POP(); |
Guido van Rossum | 5053efc | 1998-08-04 15:27:50 +0000 | [diff] [blame] | 2157 | PyList_SET_ITEM(x, oparg, w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2158 | } |
| 2159 | PUSH(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 2160 | continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2161 | } |
| 2162 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2163 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2164 | case BUILD_MAP: |
Raymond Hettinger | fd7ed40 | 2007-12-18 21:24:09 +0000 | [diff] [blame] | 2165 | x = _PyDict_NewPresized((Py_ssize_t)oparg); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2166 | PUSH(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 2167 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2168 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2169 | |
Raymond Hettinger | effde12 | 2007-12-18 18:26:18 +0000 | [diff] [blame] | 2170 | case STORE_MAP: |
| 2171 | w = TOP(); /* key */ |
| 2172 | u = SECOND(); /* value */ |
| 2173 | v = THIRD(); /* dict */ |
| 2174 | STACKADJ(-2); |
| 2175 | assert (PyDict_CheckExact(v)); |
| 2176 | err = PyDict_SetItem(v, w, u); /* v[w] = u */ |
| 2177 | Py_DECREF(u); |
| 2178 | Py_DECREF(w); |
| 2179 | if (err == 0) continue; |
| 2180 | break; |
| 2181 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2182 | case LOAD_ATTR: |
Skip Montanaro | 496e658 | 2002-08-06 17:47:40 +0000 | [diff] [blame] | 2183 | w = GETITEM(names, oparg); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 2184 | v = TOP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2185 | x = PyObject_GetAttr(v, w); |
| 2186 | Py_DECREF(v); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 2187 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 2188 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2189 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2190 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2191 | case COMPARE_OP: |
| 2192 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 2193 | v = TOP(); |
Raymond Hettinger | 4bad9ba | 2003-01-19 05:08:13 +0000 | [diff] [blame] | 2194 | if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) { |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 2195 | /* INLINE: cmp(int, int) */ |
| 2196 | register long a, b; |
| 2197 | register int res; |
Guido van Rossum | cf183ac | 1998-12-04 18:51:36 +0000 | [diff] [blame] | 2198 | a = PyInt_AS_LONG(v); |
| 2199 | b = PyInt_AS_LONG(w); |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 2200 | switch (oparg) { |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 2201 | case PyCmp_LT: res = a < b; break; |
| 2202 | case PyCmp_LE: res = a <= b; break; |
| 2203 | case PyCmp_EQ: res = a == b; break; |
| 2204 | case PyCmp_NE: res = a != b; break; |
| 2205 | case PyCmp_GT: res = a > b; break; |
| 2206 | case PyCmp_GE: res = a >= b; break; |
| 2207 | case PyCmp_IS: res = v == w; break; |
| 2208 | case PyCmp_IS_NOT: res = v != w; break; |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 2209 | default: goto slow_compare; |
| 2210 | } |
| 2211 | x = res ? Py_True : Py_False; |
| 2212 | Py_INCREF(x); |
| 2213 | } |
| 2214 | else { |
| 2215 | slow_compare: |
| 2216 | x = cmp_outcome(oparg, v, w); |
| 2217 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2218 | Py_DECREF(v); |
| 2219 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 2220 | SET_TOP(x); |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 2221 | if (x == NULL) break; |
Jeffrey Yasskin | 68d6852 | 2009-02-28 19:03:21 +0000 | [diff] [blame] | 2222 | PREDICT(POP_JUMP_IF_FALSE); |
| 2223 | PREDICT(POP_JUMP_IF_TRUE); |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 2224 | continue; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2225 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2226 | case IMPORT_NAME: |
Skip Montanaro | 496e658 | 2002-08-06 17:47:40 +0000 | [diff] [blame] | 2227 | w = GETITEM(names, oparg); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2228 | x = PyDict_GetItemString(f->f_builtins, "__import__"); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2229 | if (x == NULL) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2230 | PyErr_SetString(PyExc_ImportError, |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 2231 | "__import__ not found"); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2232 | break; |
| 2233 | } |
Guido van Rossum | 1d9a9ea | 2008-01-23 20:19:01 +0000 | [diff] [blame] | 2234 | Py_INCREF(x); |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 2235 | v = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 2236 | u = TOP(); |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 2237 | if (PyInt_AsLong(u) != -1 || PyErr_Occurred()) |
| 2238 | w = PyTuple_Pack(5, |
| 2239 | w, |
| 2240 | f->f_globals, |
| 2241 | f->f_locals == NULL ? |
| 2242 | Py_None : f->f_locals, |
| 2243 | v, |
| 2244 | u); |
| 2245 | else |
| 2246 | w = PyTuple_Pack(4, |
| 2247 | w, |
| 2248 | f->f_globals, |
| 2249 | f->f_locals == NULL ? |
| 2250 | Py_None : f->f_locals, |
| 2251 | v); |
| 2252 | Py_DECREF(v); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2253 | Py_DECREF(u); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2254 | if (w == NULL) { |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 2255 | u = POP(); |
Guido van Rossum | 1d9a9ea | 2008-01-23 20:19:01 +0000 | [diff] [blame] | 2256 | Py_DECREF(x); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2257 | x = NULL; |
| 2258 | break; |
| 2259 | } |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 2260 | READ_TIMESTAMP(intr0); |
Guido van Rossum | 1d9a9ea | 2008-01-23 20:19:01 +0000 | [diff] [blame] | 2261 | v = x; |
| 2262 | x = PyEval_CallObject(v, w); |
| 2263 | Py_DECREF(v); |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 2264 | READ_TIMESTAMP(intr1); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2265 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 2266 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 2267 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2268 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2269 | |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2270 | case IMPORT_STAR: |
| 2271 | v = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2272 | PyFrame_FastToLocals(f); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2273 | if ((x = f->f_locals) == NULL) { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2274 | PyErr_SetString(PyExc_SystemError, |
Jeremy Hylton | c862cf4 | 2001-01-19 03:25:05 +0000 | [diff] [blame] | 2275 | "no locals found during 'import *'"); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2276 | break; |
| 2277 | } |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 2278 | READ_TIMESTAMP(intr0); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2279 | err = import_all_from(x, v); |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 2280 | READ_TIMESTAMP(intr1); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2281 | PyFrame_LocalsToFast(f, 0); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2282 | Py_DECREF(v); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 2283 | if (err == 0) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2284 | break; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2285 | |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2286 | case IMPORT_FROM: |
Skip Montanaro | 496e658 | 2002-08-06 17:47:40 +0000 | [diff] [blame] | 2287 | w = GETITEM(names, oparg); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2288 | v = TOP(); |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 2289 | READ_TIMESTAMP(intr0); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2290 | x = import_from(v, w); |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 2291 | READ_TIMESTAMP(intr1); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2292 | PUSH(x); |
| 2293 | if (x != NULL) continue; |
| 2294 | break; |
| 2295 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2296 | case JUMP_FORWARD: |
| 2297 | JUMPBY(oparg); |
Neil Schemenauer | c4b570f | 2003-06-01 19:21:12 +0000 | [diff] [blame] | 2298 | goto fast_next_opcode; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2299 | |
Jeffrey Yasskin | 68d6852 | 2009-02-28 19:03:21 +0000 | [diff] [blame] | 2300 | PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE); |
| 2301 | case POP_JUMP_IF_FALSE: |
| 2302 | w = POP(); |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 2303 | if (w == Py_True) { |
Jeffrey Yasskin | 68d6852 | 2009-02-28 19:03:21 +0000 | [diff] [blame] | 2304 | Py_DECREF(w); |
Neil Schemenauer | c4b570f | 2003-06-01 19:21:12 +0000 | [diff] [blame] | 2305 | goto fast_next_opcode; |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 2306 | } |
Raymond Hettinger | 21012b8 | 2003-02-26 18:11:50 +0000 | [diff] [blame] | 2307 | if (w == Py_False) { |
Jeffrey Yasskin | 68d6852 | 2009-02-28 19:03:21 +0000 | [diff] [blame] | 2308 | Py_DECREF(w); |
| 2309 | JUMPTO(oparg); |
Neil Schemenauer | c4b570f | 2003-06-01 19:21:12 +0000 | [diff] [blame] | 2310 | goto fast_next_opcode; |
Raymond Hettinger | 21012b8 | 2003-02-26 18:11:50 +0000 | [diff] [blame] | 2311 | } |
| 2312 | err = PyObject_IsTrue(w); |
Jeffrey Yasskin | 68d6852 | 2009-02-28 19:03:21 +0000 | [diff] [blame] | 2313 | Py_DECREF(w); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 2314 | if (err > 0) |
| 2315 | err = 0; |
| 2316 | else if (err == 0) |
Jeffrey Yasskin | 68d6852 | 2009-02-28 19:03:21 +0000 | [diff] [blame] | 2317 | JUMPTO(oparg); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 2318 | else |
| 2319 | break; |
| 2320 | continue; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2321 | |
Jeffrey Yasskin | 68d6852 | 2009-02-28 19:03:21 +0000 | [diff] [blame] | 2322 | PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE); |
| 2323 | case POP_JUMP_IF_TRUE: |
| 2324 | w = POP(); |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 2325 | if (w == Py_False) { |
Jeffrey Yasskin | 68d6852 | 2009-02-28 19:03:21 +0000 | [diff] [blame] | 2326 | Py_DECREF(w); |
Neil Schemenauer | c4b570f | 2003-06-01 19:21:12 +0000 | [diff] [blame] | 2327 | goto fast_next_opcode; |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 2328 | } |
Raymond Hettinger | 21012b8 | 2003-02-26 18:11:50 +0000 | [diff] [blame] | 2329 | if (w == Py_True) { |
Jeffrey Yasskin | 68d6852 | 2009-02-28 19:03:21 +0000 | [diff] [blame] | 2330 | Py_DECREF(w); |
| 2331 | JUMPTO(oparg); |
| 2332 | goto fast_next_opcode; |
| 2333 | } |
| 2334 | err = PyObject_IsTrue(w); |
| 2335 | Py_DECREF(w); |
| 2336 | if (err > 0) { |
| 2337 | err = 0; |
| 2338 | JUMPTO(oparg); |
| 2339 | } |
| 2340 | else if (err == 0) |
| 2341 | ; |
| 2342 | else |
| 2343 | break; |
| 2344 | continue; |
| 2345 | |
| 2346 | case JUMP_IF_FALSE_OR_POP: |
| 2347 | w = TOP(); |
| 2348 | if (w == Py_True) { |
| 2349 | STACKADJ(-1); |
| 2350 | Py_DECREF(w); |
| 2351 | goto fast_next_opcode; |
| 2352 | } |
| 2353 | if (w == Py_False) { |
| 2354 | JUMPTO(oparg); |
| 2355 | goto fast_next_opcode; |
| 2356 | } |
| 2357 | err = PyObject_IsTrue(w); |
| 2358 | if (err > 0) { |
| 2359 | STACKADJ(-1); |
| 2360 | Py_DECREF(w); |
| 2361 | err = 0; |
| 2362 | } |
| 2363 | else if (err == 0) |
| 2364 | JUMPTO(oparg); |
| 2365 | else |
| 2366 | break; |
| 2367 | continue; |
| 2368 | |
| 2369 | case JUMP_IF_TRUE_OR_POP: |
| 2370 | w = TOP(); |
| 2371 | if (w == Py_False) { |
| 2372 | STACKADJ(-1); |
| 2373 | Py_DECREF(w); |
| 2374 | goto fast_next_opcode; |
| 2375 | } |
| 2376 | if (w == Py_True) { |
| 2377 | JUMPTO(oparg); |
Neil Schemenauer | c4b570f | 2003-06-01 19:21:12 +0000 | [diff] [blame] | 2378 | goto fast_next_opcode; |
Raymond Hettinger | 21012b8 | 2003-02-26 18:11:50 +0000 | [diff] [blame] | 2379 | } |
| 2380 | err = PyObject_IsTrue(w); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 2381 | if (err > 0) { |
| 2382 | err = 0; |
Jeffrey Yasskin | 68d6852 | 2009-02-28 19:03:21 +0000 | [diff] [blame] | 2383 | JUMPTO(oparg); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 2384 | } |
Jeffrey Yasskin | 68d6852 | 2009-02-28 19:03:21 +0000 | [diff] [blame] | 2385 | else if (err == 0) { |
| 2386 | STACKADJ(-1); |
| 2387 | Py_DECREF(w); |
| 2388 | } |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 2389 | else |
| 2390 | break; |
| 2391 | continue; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2392 | |
Raymond Hettinger | fba1cfc | 2004-03-12 16:33:17 +0000 | [diff] [blame] | 2393 | PREDICTED_WITH_ARG(JUMP_ABSOLUTE); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2394 | case JUMP_ABSOLUTE: |
| 2395 | JUMPTO(oparg); |
Raymond Hettinger | dc1d1ba | 2007-11-07 02:45:46 +0000 | [diff] [blame] | 2396 | #if FAST_LOOPS |
| 2397 | /* Enabling this path speeds-up all while and for-loops by bypassing |
| 2398 | the per-loop checks for signals. By default, this should be turned-off |
| 2399 | because it prevents detection of a control-break in tight loops like |
| 2400 | "while 1: pass". Compile with this option turned-on when you need |
| 2401 | the speed-up and do not need break checking inside tight loops (ones |
Antoine Pitrou | c2cc80c | 2008-07-25 22:13:52 +0000 | [diff] [blame] | 2402 | that contain only instructions ending with goto fast_next_opcode). |
Raymond Hettinger | dc1d1ba | 2007-11-07 02:45:46 +0000 | [diff] [blame] | 2403 | */ |
| 2404 | goto fast_next_opcode; |
| 2405 | #else |
Neil Schemenauer | ca2a2f1 | 2003-05-30 23:59:44 +0000 | [diff] [blame] | 2406 | continue; |
Raymond Hettinger | dc1d1ba | 2007-11-07 02:45:46 +0000 | [diff] [blame] | 2407 | #endif |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2408 | |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2409 | case GET_ITER: |
| 2410 | /* before: [obj]; after [getiter(obj)] */ |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 2411 | v = TOP(); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2412 | x = PyObject_GetIter(v); |
| 2413 | Py_DECREF(v); |
| 2414 | if (x != NULL) { |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 2415 | SET_TOP(x); |
Raymond Hettinger | 7dc5221 | 2003-03-16 20:14:44 +0000 | [diff] [blame] | 2416 | PREDICT(FOR_ITER); |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2417 | continue; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2418 | } |
Raymond Hettinger | 8bb90a5 | 2003-01-14 12:43:10 +0000 | [diff] [blame] | 2419 | STACKADJ(-1); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2420 | break; |
| 2421 | |
Raymond Hettinger | 7dc5221 | 2003-03-16 20:14:44 +0000 | [diff] [blame] | 2422 | PREDICTED_WITH_ARG(FOR_ITER); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2423 | case FOR_ITER: |
| 2424 | /* before: [iter]; after: [iter, iter()] *or* [] */ |
| 2425 | v = TOP(); |
Raymond Hettinger | db0de9e | 2004-03-12 08:41:36 +0000 | [diff] [blame] | 2426 | x = (*v->ob_type->tp_iternext)(v); |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2427 | if (x != NULL) { |
| 2428 | PUSH(x); |
Raymond Hettinger | 7dc5221 | 2003-03-16 20:14:44 +0000 | [diff] [blame] | 2429 | PREDICT(STORE_FAST); |
| 2430 | PREDICT(UNPACK_SEQUENCE); |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2431 | continue; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2432 | } |
Raymond Hettinger | db0de9e | 2004-03-12 08:41:36 +0000 | [diff] [blame] | 2433 | if (PyErr_Occurred()) { |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 2434 | if (!PyErr_ExceptionMatches( |
| 2435 | PyExc_StopIteration)) |
Raymond Hettinger | db0de9e | 2004-03-12 08:41:36 +0000 | [diff] [blame] | 2436 | break; |
| 2437 | PyErr_Clear(); |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2438 | } |
Raymond Hettinger | db0de9e | 2004-03-12 08:41:36 +0000 | [diff] [blame] | 2439 | /* iterator ended normally */ |
| 2440 | x = v = POP(); |
| 2441 | Py_DECREF(v); |
| 2442 | JUMPBY(oparg); |
| 2443 | continue; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2444 | |
Raymond Hettinger | 2d783e9 | 2004-03-12 09:12:22 +0000 | [diff] [blame] | 2445 | case BREAK_LOOP: |
| 2446 | why = WHY_BREAK; |
| 2447 | goto fast_block_end; |
| 2448 | |
| 2449 | case CONTINUE_LOOP: |
| 2450 | retval = PyInt_FromLong(oparg); |
Neal Norwitz | 02104df | 2006-05-19 06:31:23 +0000 | [diff] [blame] | 2451 | if (!retval) { |
| 2452 | x = NULL; |
| 2453 | break; |
| 2454 | } |
Raymond Hettinger | 2d783e9 | 2004-03-12 09:12:22 +0000 | [diff] [blame] | 2455 | why = WHY_CONTINUE; |
| 2456 | goto fast_block_end; |
| 2457 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2458 | case SETUP_LOOP: |
| 2459 | case SETUP_EXCEPT: |
| 2460 | case SETUP_FINALLY: |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 2461 | /* NOTE: If you add any new block-setup opcodes that |
| 2462 | are not try/except/finally handlers, you may need |
| 2463 | to update the PyGen_NeedsFinalizing() function. |
| 2464 | */ |
Phillip J. Eby | 2ba9661 | 2006-04-10 17:51:05 +0000 | [diff] [blame] | 2465 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2466 | PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg, |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 2467 | STACK_LEVEL()); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 2468 | continue; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2469 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 2470 | case WITH_CLEANUP: |
| 2471 | { |
Nick Coghlan | 7af53be | 2008-03-07 14:13:28 +0000 | [diff] [blame] | 2472 | /* At the top of the stack are 1-3 values indicating |
| 2473 | how/why we entered the finally clause: |
| 2474 | - TOP = None |
| 2475 | - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval |
| 2476 | - TOP = WHY_*; no retval below it |
| 2477 | - (TOP, SECOND, THIRD) = exc_info() |
| 2478 | Below them is EXIT, the context.__exit__ bound method. |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 2479 | In the last case, we must call |
Nick Coghlan | 7af53be | 2008-03-07 14:13:28 +0000 | [diff] [blame] | 2480 | EXIT(TOP, SECOND, THIRD) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 2481 | otherwise we must call |
Nick Coghlan | 7af53be | 2008-03-07 14:13:28 +0000 | [diff] [blame] | 2482 | EXIT(None, None, None) |
| 2483 | |
| 2484 | In all cases, we remove EXIT from the stack, leaving |
| 2485 | the rest in the same order. |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 2486 | |
| 2487 | In addition, if the stack represents an exception, |
Guido van Rossum | f669436 | 2006-03-10 02:28:35 +0000 | [diff] [blame] | 2488 | *and* the function call returns a 'true' value, we |
| 2489 | "zap" this information, to prevent END_FINALLY from |
| 2490 | re-raising the exception. (But non-local gotos |
| 2491 | should still be resumed.) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 2492 | */ |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 2493 | |
Nick Coghlan | 7af53be | 2008-03-07 14:13:28 +0000 | [diff] [blame] | 2494 | PyObject *exit_func; |
| 2495 | |
| 2496 | u = POP(); |
| 2497 | if (u == Py_None) { |
| 2498 | exit_func = TOP(); |
| 2499 | SET_TOP(u); |
| 2500 | v = w = Py_None; |
| 2501 | } |
| 2502 | else if (PyInt_Check(u)) { |
| 2503 | switch(PyInt_AS_LONG(u)) { |
| 2504 | case WHY_RETURN: |
| 2505 | case WHY_CONTINUE: |
| 2506 | /* Retval in TOP. */ |
| 2507 | exit_func = SECOND(); |
| 2508 | SET_SECOND(TOP()); |
| 2509 | SET_TOP(u); |
| 2510 | break; |
| 2511 | default: |
| 2512 | exit_func = TOP(); |
| 2513 | SET_TOP(u); |
| 2514 | break; |
| 2515 | } |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 2516 | u = v = w = Py_None; |
| 2517 | } |
| 2518 | else { |
Nick Coghlan | 7af53be | 2008-03-07 14:13:28 +0000 | [diff] [blame] | 2519 | v = TOP(); |
| 2520 | w = SECOND(); |
| 2521 | exit_func = THIRD(); |
| 2522 | SET_TOP(u); |
| 2523 | SET_SECOND(v); |
| 2524 | SET_THIRD(w); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 2525 | } |
Guido van Rossum | f669436 | 2006-03-10 02:28:35 +0000 | [diff] [blame] | 2526 | /* XXX Not the fastest way to call it... */ |
Nick Coghlan | 7af53be | 2008-03-07 14:13:28 +0000 | [diff] [blame] | 2527 | x = PyObject_CallFunctionObjArgs(exit_func, u, v, w, |
| 2528 | NULL); |
Amaury Forgeot d'Arc | ad9b599 | 2008-12-10 23:22:49 +0000 | [diff] [blame] | 2529 | Py_DECREF(exit_func); |
| 2530 | if (x == NULL) |
Guido van Rossum | f669436 | 2006-03-10 02:28:35 +0000 | [diff] [blame] | 2531 | break; /* Go to error exit */ |
Amaury Forgeot d'Arc | ad9b599 | 2008-12-10 23:22:49 +0000 | [diff] [blame] | 2532 | |
| 2533 | if (u != Py_None) |
| 2534 | err = PyObject_IsTrue(x); |
| 2535 | else |
| 2536 | err = 0; |
| 2537 | Py_DECREF(x); |
| 2538 | |
| 2539 | if (err < 0) |
| 2540 | break; /* Go to error exit */ |
| 2541 | else if (err > 0) { |
| 2542 | err = 0; |
Guido van Rossum | f669436 | 2006-03-10 02:28:35 +0000 | [diff] [blame] | 2543 | /* There was an exception and a true return */ |
Nick Coghlan | 7af53be | 2008-03-07 14:13:28 +0000 | [diff] [blame] | 2544 | STACKADJ(-2); |
Guido van Rossum | f669436 | 2006-03-10 02:28:35 +0000 | [diff] [blame] | 2545 | Py_INCREF(Py_None); |
| 2546 | SET_TOP(Py_None); |
Guido van Rossum | f669436 | 2006-03-10 02:28:35 +0000 | [diff] [blame] | 2547 | Py_DECREF(u); |
| 2548 | Py_DECREF(v); |
| 2549 | Py_DECREF(w); |
| 2550 | } else { |
Nick Coghlan | 7af53be | 2008-03-07 14:13:28 +0000 | [diff] [blame] | 2551 | /* The stack was rearranged to remove EXIT |
| 2552 | above. Let END_FINALLY do its thing */ |
Guido van Rossum | f669436 | 2006-03-10 02:28:35 +0000 | [diff] [blame] | 2553 | } |
Jeffrey Yasskin | 9063a99 | 2008-03-03 01:27:03 +0000 | [diff] [blame] | 2554 | PREDICT(END_FINALLY); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 2555 | break; |
| 2556 | } |
| 2557 | |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 2558 | case CALL_FUNCTION: |
Armin Rigo | 8817fcd | 2004-06-17 10:22:40 +0000 | [diff] [blame] | 2559 | { |
| 2560 | PyObject **sp; |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 2561 | PCALL(PCALL_ALL); |
Armin Rigo | 8817fcd | 2004-06-17 10:22:40 +0000 | [diff] [blame] | 2562 | sp = stack_pointer; |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 2563 | #ifdef WITH_TSC |
Armin Rigo | 8817fcd | 2004-06-17 10:22:40 +0000 | [diff] [blame] | 2564 | x = call_function(&sp, oparg, &intr0, &intr1); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 2565 | #else |
Armin Rigo | 8817fcd | 2004-06-17 10:22:40 +0000 | [diff] [blame] | 2566 | x = call_function(&sp, oparg); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 2567 | #endif |
Armin Rigo | 8817fcd | 2004-06-17 10:22:40 +0000 | [diff] [blame] | 2568 | stack_pointer = sp; |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 2569 | PUSH(x); |
| 2570 | if (x != NULL) |
| 2571 | continue; |
| 2572 | break; |
Armin Rigo | 8817fcd | 2004-06-17 10:22:40 +0000 | [diff] [blame] | 2573 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2574 | |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 2575 | case CALL_FUNCTION_VAR: |
| 2576 | case CALL_FUNCTION_KW: |
| 2577 | case CALL_FUNCTION_VAR_KW: |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 2578 | { |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 2579 | int na = oparg & 0xff; |
| 2580 | int nk = (oparg>>8) & 0xff; |
| 2581 | int flags = (opcode - CALL_FUNCTION) & 3; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 2582 | int n = na + 2 * nk; |
Armin Rigo | 8817fcd | 2004-06-17 10:22:40 +0000 | [diff] [blame] | 2583 | PyObject **pfunc, *func, **sp; |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 2584 | PCALL(PCALL_ALL); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 2585 | if (flags & CALL_FLAG_VAR) |
| 2586 | n++; |
| 2587 | if (flags & CALL_FLAG_KW) |
| 2588 | n++; |
| 2589 | pfunc = stack_pointer - n - 1; |
| 2590 | func = *pfunc; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 2591 | |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2592 | if (PyMethod_Check(func) |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 2593 | && PyMethod_GET_SELF(func) != NULL) { |
| 2594 | PyObject *self = PyMethod_GET_SELF(func); |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 2595 | Py_INCREF(self); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 2596 | func = PyMethod_GET_FUNCTION(func); |
| 2597 | Py_INCREF(func); |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 2598 | Py_DECREF(*pfunc); |
| 2599 | *pfunc = self; |
| 2600 | na++; |
| 2601 | n++; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2602 | } else |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 2603 | Py_INCREF(func); |
Armin Rigo | 8817fcd | 2004-06-17 10:22:40 +0000 | [diff] [blame] | 2604 | sp = stack_pointer; |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 2605 | READ_TIMESTAMP(intr0); |
Armin Rigo | 8817fcd | 2004-06-17 10:22:40 +0000 | [diff] [blame] | 2606 | x = ext_do_call(func, &sp, flags, na, nk); |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 2607 | READ_TIMESTAMP(intr1); |
Armin Rigo | 8817fcd | 2004-06-17 10:22:40 +0000 | [diff] [blame] | 2608 | stack_pointer = sp; |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 2609 | Py_DECREF(func); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 2610 | |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 2611 | while (stack_pointer > pfunc) { |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 2612 | w = POP(); |
| 2613 | Py_DECREF(w); |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 2614 | } |
| 2615 | PUSH(x); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2616 | if (x != NULL) |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 2617 | continue; |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 2618 | break; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 2619 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2620 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2621 | case MAKE_FUNCTION: |
| 2622 | v = POP(); /* code object */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2623 | x = PyFunction_New(v, f->f_globals); |
| 2624 | Py_DECREF(v); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2625 | /* XXX Maybe this should be a separate opcode? */ |
| 2626 | if (x != NULL && oparg > 0) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2627 | v = PyTuple_New(oparg); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2628 | if (v == NULL) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2629 | Py_DECREF(x); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2630 | x = NULL; |
| 2631 | break; |
| 2632 | } |
Raymond Hettinger | 5bed456 | 2004-04-10 23:34:17 +0000 | [diff] [blame] | 2633 | while (--oparg >= 0) { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2634 | w = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2635 | PyTuple_SET_ITEM(v, oparg, w); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2636 | } |
| 2637 | err = PyFunction_SetDefaults(x, v); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2638 | Py_DECREF(v); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2639 | } |
| 2640 | PUSH(x); |
| 2641 | break; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2642 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2643 | case MAKE_CLOSURE: |
| 2644 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2645 | v = POP(); /* code object */ |
| 2646 | x = PyFunction_New(v, f->f_globals); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2647 | Py_DECREF(v); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2648 | if (x != NULL) { |
| 2649 | v = POP(); |
Jeffrey Yasskin | 2d873bd | 2008-12-08 18:55:24 +0000 | [diff] [blame] | 2650 | if (PyFunction_SetClosure(x, v) != 0) { |
| 2651 | /* Can't happen unless bytecode is corrupt. */ |
| 2652 | why = WHY_EXCEPTION; |
| 2653 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2654 | Py_DECREF(v); |
| 2655 | } |
| 2656 | if (x != NULL && oparg > 0) { |
| 2657 | v = PyTuple_New(oparg); |
| 2658 | if (v == NULL) { |
| 2659 | Py_DECREF(x); |
| 2660 | x = NULL; |
| 2661 | break; |
| 2662 | } |
Raymond Hettinger | 5bed456 | 2004-04-10 23:34:17 +0000 | [diff] [blame] | 2663 | while (--oparg >= 0) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2664 | w = POP(); |
| 2665 | PyTuple_SET_ITEM(v, oparg, w); |
| 2666 | } |
Jeffrey Yasskin | 2d873bd | 2008-12-08 18:55:24 +0000 | [diff] [blame] | 2667 | if (PyFunction_SetDefaults(x, v) != 0) { |
| 2668 | /* Can't happen unless |
| 2669 | PyFunction_SetDefaults changes. */ |
| 2670 | why = WHY_EXCEPTION; |
| 2671 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2672 | Py_DECREF(v); |
| 2673 | } |
| 2674 | PUSH(x); |
| 2675 | break; |
| 2676 | } |
| 2677 | |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2678 | case BUILD_SLICE: |
| 2679 | if (oparg == 3) |
| 2680 | w = POP(); |
| 2681 | else |
| 2682 | w = NULL; |
| 2683 | v = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 2684 | u = TOP(); |
Guido van Rossum | 1aa1483 | 1997-01-21 05:34:20 +0000 | [diff] [blame] | 2685 | x = PySlice_New(u, v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2686 | Py_DECREF(u); |
| 2687 | Py_DECREF(v); |
| 2688 | Py_XDECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 2689 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 2690 | if (x != NULL) continue; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2691 | break; |
| 2692 | |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 2693 | case EXTENDED_ARG: |
| 2694 | opcode = NEXTOP(); |
Raymond Hettinger | 5bed456 | 2004-04-10 23:34:17 +0000 | [diff] [blame] | 2695 | oparg = oparg<<16 | NEXTARG(); |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 2696 | goto dispatch_opcode; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2697 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2698 | default: |
| 2699 | fprintf(stderr, |
| 2700 | "XXX lineno: %d, opcode: %d\n", |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 2701 | PyCode_Addr2Line(f->f_code, f->f_lasti), |
| 2702 | opcode); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2703 | PyErr_SetString(PyExc_SystemError, "unknown opcode"); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2704 | why = WHY_EXCEPTION; |
| 2705 | break; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 2706 | |
| 2707 | #ifdef CASE_TOO_BIG |
| 2708 | } |
| 2709 | #endif |
| 2710 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2711 | } /* switch */ |
| 2712 | |
| 2713 | on_error: |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2714 | |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 2715 | READ_TIMESTAMP(inst1); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 2716 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2717 | /* Quickly continue if no error occurred */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2718 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2719 | if (why == WHY_NOT) { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2720 | if (err == 0 && x != NULL) { |
| 2721 | #ifdef CHECKEXC |
Guido van Rossum | eb894eb | 1999-03-09 16:16:45 +0000 | [diff] [blame] | 2722 | /* This check is expensive! */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2723 | if (PyErr_Occurred()) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2724 | fprintf(stderr, |
| 2725 | "XXX undetected error\n"); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 2726 | else { |
| 2727 | #endif |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 2728 | READ_TIMESTAMP(loop1); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2729 | continue; /* Normal, fast path */ |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 2730 | #ifdef CHECKEXC |
| 2731 | } |
| 2732 | #endif |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2733 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2734 | why = WHY_EXCEPTION; |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2735 | x = Py_None; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2736 | err = 0; |
| 2737 | } |
| 2738 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2739 | /* Double-check exception status */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2740 | |
Raymond Hettinger | c8aa08b | 2004-04-11 14:59:33 +0000 | [diff] [blame] | 2741 | if (why == WHY_EXCEPTION || why == WHY_RERAISE) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2742 | if (!PyErr_Occurred()) { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2743 | PyErr_SetString(PyExc_SystemError, |
Guido van Rossum | eb894eb | 1999-03-09 16:16:45 +0000 | [diff] [blame] | 2744 | "error return without exception set"); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2745 | why = WHY_EXCEPTION; |
| 2746 | } |
| 2747 | } |
Guido van Rossum | eb894eb | 1999-03-09 16:16:45 +0000 | [diff] [blame] | 2748 | #ifdef CHECKEXC |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2749 | else { |
Guido van Rossum | eb894eb | 1999-03-09 16:16:45 +0000 | [diff] [blame] | 2750 | /* This check is expensive! */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2751 | if (PyErr_Occurred()) { |
Neal Norwitz | 8250fbe | 2008-01-27 17:12:15 +0000 | [diff] [blame] | 2752 | char buf[128]; |
Jeremy Hylton | 904ed86 | 2003-11-05 17:29:35 +0000 | [diff] [blame] | 2753 | sprintf(buf, "Stack unwind with exception " |
| 2754 | "set and why=%d", why); |
| 2755 | Py_FatalError(buf); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2756 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2757 | } |
| 2758 | #endif |
| 2759 | |
| 2760 | /* Log traceback info if this is a real exception */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2761 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2762 | if (why == WHY_EXCEPTION) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2763 | PyTraceBack_Here(f); |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 2764 | |
Fred Drake | 8f51f54 | 2001-10-04 14:48:42 +0000 | [diff] [blame] | 2765 | if (tstate->c_tracefunc != NULL) |
| 2766 | call_exc_trace(tstate->c_tracefunc, |
| 2767 | tstate->c_traceobj, f); |
Guido van Rossum | 014518f | 1998-11-23 21:09:51 +0000 | [diff] [blame] | 2768 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2769 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2770 | /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2771 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2772 | if (why == WHY_RERAISE) |
| 2773 | why = WHY_EXCEPTION; |
| 2774 | |
| 2775 | /* Unwind stacks if a (pseudo) exception occurred */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2776 | |
Raymond Hettinger | 1dd8309 | 2004-02-06 18:32:33 +0000 | [diff] [blame] | 2777 | fast_block_end: |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 2778 | while (why != WHY_NOT && f->f_iblock > 0) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2779 | PyTryBlock *b = PyFrame_BlockPop(f); |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 2780 | |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 2781 | assert(why != WHY_YIELD); |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 2782 | if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) { |
| 2783 | /* For a continue inside a try block, |
| 2784 | don't pop the block for the loop. */ |
Thomas Wouters | 1ee6422 | 2001-09-24 19:32:01 +0000 | [diff] [blame] | 2785 | PyFrame_BlockSetup(f, b->b_type, b->b_handler, |
| 2786 | b->b_level); |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 2787 | why = WHY_NOT; |
| 2788 | JUMPTO(PyInt_AS_LONG(retval)); |
| 2789 | Py_DECREF(retval); |
| 2790 | break; |
| 2791 | } |
| 2792 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2793 | while (STACK_LEVEL() > b->b_level) { |
| 2794 | v = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2795 | Py_XDECREF(v); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2796 | } |
| 2797 | if (b->b_type == SETUP_LOOP && why == WHY_BREAK) { |
| 2798 | why = WHY_NOT; |
| 2799 | JUMPTO(b->b_handler); |
| 2800 | break; |
| 2801 | } |
| 2802 | if (b->b_type == SETUP_FINALLY || |
Guido van Rossum | 150b2df | 1996-12-05 23:17:11 +0000 | [diff] [blame] | 2803 | (b->b_type == SETUP_EXCEPT && |
| 2804 | why == WHY_EXCEPTION)) { |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2805 | if (why == WHY_EXCEPTION) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2806 | PyObject *exc, *val, *tb; |
| 2807 | PyErr_Fetch(&exc, &val, &tb); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2808 | if (val == NULL) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2809 | val = Py_None; |
| 2810 | Py_INCREF(val); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2811 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2812 | /* Make the raw exception data |
| 2813 | available to the handler, |
| 2814 | so a program can emulate the |
| 2815 | Python main loop. Don't do |
| 2816 | this for 'finally'. */ |
| 2817 | if (b->b_type == SETUP_EXCEPT) { |
Barry Warsaw | eaedc7c | 1997-08-28 22:36:40 +0000 | [diff] [blame] | 2818 | PyErr_NormalizeException( |
| 2819 | &exc, &val, &tb); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2820 | set_exc_info(tstate, |
| 2821 | exc, val, tb); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2822 | } |
Jeremy Hylton | c631489 | 2001-09-26 19:24:45 +0000 | [diff] [blame] | 2823 | if (tb == NULL) { |
| 2824 | Py_INCREF(Py_None); |
| 2825 | PUSH(Py_None); |
| 2826 | } else |
| 2827 | PUSH(tb); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2828 | PUSH(val); |
| 2829 | PUSH(exc); |
| 2830 | } |
| 2831 | else { |
Raymond Hettinger | 06032cb | 2004-04-06 09:37:35 +0000 | [diff] [blame] | 2832 | if (why & (WHY_RETURN | WHY_CONTINUE)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2833 | PUSH(retval); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2834 | v = PyInt_FromLong((long)why); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2835 | PUSH(v); |
| 2836 | } |
| 2837 | why = WHY_NOT; |
| 2838 | JUMPTO(b->b_handler); |
| 2839 | break; |
| 2840 | } |
| 2841 | } /* unwind stack */ |
| 2842 | |
| 2843 | /* End the loop if we still have an error (or return) */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2844 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2845 | if (why != WHY_NOT) |
| 2846 | break; |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 2847 | READ_TIMESTAMP(loop1); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2848 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2849 | } /* main loop */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2850 | |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 2851 | assert(why != WHY_YIELD); |
| 2852 | /* Pop remaining stack entries. */ |
| 2853 | while (!EMPTY()) { |
| 2854 | v = POP(); |
| 2855 | Py_XDECREF(v); |
Guido van Rossum | 35974fb | 2001-12-06 21:28:18 +0000 | [diff] [blame] | 2856 | } |
| 2857 | |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 2858 | if (why != WHY_RETURN) |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 2859 | retval = NULL; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2860 | |
Raymond Hettinger | 1dd8309 | 2004-02-06 18:32:33 +0000 | [diff] [blame] | 2861 | fast_yield: |
Fred Drake | 9e3ad78 | 2001-07-03 23:39:52 +0000 | [diff] [blame] | 2862 | if (tstate->use_tracing) { |
Barry Warsaw | e2eca0b | 2005-08-15 18:14:19 +0000 | [diff] [blame] | 2863 | if (tstate->c_tracefunc) { |
| 2864 | if (why == WHY_RETURN || why == WHY_YIELD) { |
| 2865 | if (call_trace(tstate->c_tracefunc, |
| 2866 | tstate->c_traceobj, f, |
| 2867 | PyTrace_RETURN, retval)) { |
| 2868 | Py_XDECREF(retval); |
| 2869 | retval = NULL; |
| 2870 | why = WHY_EXCEPTION; |
| 2871 | } |
| 2872 | } |
| 2873 | else if (why == WHY_EXCEPTION) { |
| 2874 | call_trace_protected(tstate->c_tracefunc, |
| 2875 | tstate->c_traceobj, f, |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 2876 | PyTrace_RETURN, NULL); |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 2877 | } |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 2878 | } |
Fred Drake | 8f51f54 | 2001-10-04 14:48:42 +0000 | [diff] [blame] | 2879 | if (tstate->c_profilefunc) { |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 2880 | if (why == WHY_EXCEPTION) |
| 2881 | call_trace_protected(tstate->c_profilefunc, |
| 2882 | tstate->c_profileobj, f, |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 2883 | PyTrace_RETURN, NULL); |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 2884 | else if (call_trace(tstate->c_profilefunc, |
| 2885 | tstate->c_profileobj, f, |
| 2886 | PyTrace_RETURN, retval)) { |
Fred Drake | 9e3ad78 | 2001-07-03 23:39:52 +0000 | [diff] [blame] | 2887 | Py_XDECREF(retval); |
| 2888 | retval = NULL; |
| 2889 | why = WHY_EXCEPTION; |
| 2890 | } |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 2891 | } |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 2892 | } |
Guido van Rossum | a424013 | 1997-01-21 21:18:36 +0000 | [diff] [blame] | 2893 | |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 2894 | if (tstate->frame->f_exc_type != NULL) |
| 2895 | reset_exc_info(tstate); |
| 2896 | else { |
| 2897 | assert(tstate->frame->f_exc_value == NULL); |
| 2898 | assert(tstate->frame->f_exc_traceback == NULL); |
| 2899 | } |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2900 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2901 | /* pop frame */ |
Thomas Wouters | ae406c6 | 2007-09-19 17:27:43 +0000 | [diff] [blame] | 2902 | exit_eval_frame: |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 2903 | Py_LeaveRecursiveCall(); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2904 | tstate->frame = f->f_back; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2905 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 2906 | return retval; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2907 | } |
| 2908 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 2909 | /* This is gonna seem *real weird*, but if you put some other code between |
Martin v. Löwis | 8d97e33 | 2004-06-27 15:43:12 +0000 | [diff] [blame] | 2910 | PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 2911 | the test in the if statements in Misc/gdbinit (pystack and pystackv). */ |
Skip Montanaro | 786ea6b | 2004-03-01 15:44:05 +0000 | [diff] [blame] | 2912 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2913 | PyObject * |
| 2914 | PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals, |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2915 | PyObject **args, int argcount, PyObject **kws, int kwcount, |
| 2916 | PyObject **defs, int defcount, PyObject *closure) |
| 2917 | { |
| 2918 | register PyFrameObject *f; |
| 2919 | register PyObject *retval = NULL; |
| 2920 | register PyObject **fastlocals, **freevars; |
| 2921 | PyThreadState *tstate = PyThreadState_GET(); |
| 2922 | PyObject *x, *u; |
| 2923 | |
| 2924 | if (globals == NULL) { |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 2925 | PyErr_SetString(PyExc_SystemError, |
Jeremy Hylton | 910d7d4 | 2001-08-12 21:52:24 +0000 | [diff] [blame] | 2926 | "PyEval_EvalCodeEx: NULL globals"); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2927 | return NULL; |
| 2928 | } |
| 2929 | |
Neal Norwitz | df6a649 | 2006-08-13 18:10:10 +0000 | [diff] [blame] | 2930 | assert(tstate != NULL); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 2931 | assert(globals != NULL); |
| 2932 | f = PyFrame_New(tstate, co, globals, locals); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2933 | if (f == NULL) |
| 2934 | return NULL; |
| 2935 | |
| 2936 | fastlocals = f->f_localsplus; |
Richard Jones | cebbefc | 2006-05-23 18:28:17 +0000 | [diff] [blame] | 2937 | freevars = f->f_localsplus + co->co_nlocals; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2938 | |
| 2939 | if (co->co_argcount > 0 || |
| 2940 | co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) { |
| 2941 | int i; |
| 2942 | int n = argcount; |
| 2943 | PyObject *kwdict = NULL; |
| 2944 | if (co->co_flags & CO_VARKEYWORDS) { |
| 2945 | kwdict = PyDict_New(); |
| 2946 | if (kwdict == NULL) |
| 2947 | goto fail; |
| 2948 | i = co->co_argcount; |
| 2949 | if (co->co_flags & CO_VARARGS) |
| 2950 | i++; |
| 2951 | SETLOCAL(i, kwdict); |
| 2952 | } |
| 2953 | if (argcount > co->co_argcount) { |
| 2954 | if (!(co->co_flags & CO_VARARGS)) { |
| 2955 | PyErr_Format(PyExc_TypeError, |
| 2956 | "%.200s() takes %s %d " |
| 2957 | "%sargument%s (%d given)", |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2958 | PyString_AsString(co->co_name), |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2959 | defcount ? "at most" : "exactly", |
| 2960 | co->co_argcount, |
| 2961 | kwcount ? "non-keyword " : "", |
| 2962 | co->co_argcount == 1 ? "" : "s", |
| 2963 | argcount); |
| 2964 | goto fail; |
| 2965 | } |
| 2966 | n = co->co_argcount; |
| 2967 | } |
| 2968 | for (i = 0; i < n; i++) { |
| 2969 | x = args[i]; |
| 2970 | Py_INCREF(x); |
| 2971 | SETLOCAL(i, x); |
| 2972 | } |
| 2973 | if (co->co_flags & CO_VARARGS) { |
| 2974 | u = PyTuple_New(argcount - n); |
| 2975 | if (u == NULL) |
| 2976 | goto fail; |
| 2977 | SETLOCAL(co->co_argcount, u); |
| 2978 | for (i = n; i < argcount; i++) { |
| 2979 | x = args[i]; |
| 2980 | Py_INCREF(x); |
| 2981 | PyTuple_SET_ITEM(u, i-n, x); |
| 2982 | } |
| 2983 | } |
| 2984 | for (i = 0; i < kwcount; i++) { |
Antoine Pitrou | c2cc80c | 2008-07-25 22:13:52 +0000 | [diff] [blame] | 2985 | PyObject **co_varnames; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2986 | PyObject *keyword = kws[2*i]; |
| 2987 | PyObject *value = kws[2*i + 1]; |
| 2988 | int j; |
Benjamin Peterson | 78821dd | 2009-01-25 17:15:10 +0000 | [diff] [blame] | 2989 | if (keyword == NULL || !(PyString_Check(keyword) |
| 2990 | #ifdef Py_USING_UNICODE |
| 2991 | || PyUnicode_Check(keyword) |
| 2992 | #endif |
| 2993 | )) { |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2994 | PyErr_Format(PyExc_TypeError, |
| 2995 | "%.200s() keywords must be strings", |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2996 | PyString_AsString(co->co_name)); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2997 | goto fail; |
| 2998 | } |
Antoine Pitrou | c2cc80c | 2008-07-25 22:13:52 +0000 | [diff] [blame] | 2999 | /* Speed hack: do raw pointer compares. As names are |
| 3000 | normally interned this should almost always hit. */ |
| 3001 | co_varnames = PySequence_Fast_ITEMS(co->co_varnames); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3002 | for (j = 0; j < co->co_argcount; j++) { |
Antoine Pitrou | c2cc80c | 2008-07-25 22:13:52 +0000 | [diff] [blame] | 3003 | PyObject *nm = co_varnames[j]; |
| 3004 | if (nm == keyword) |
| 3005 | goto kw_found; |
| 3006 | } |
| 3007 | /* Slow fallback, just in case */ |
| 3008 | for (j = 0; j < co->co_argcount; j++) { |
| 3009 | PyObject *nm = co_varnames[j]; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3010 | int cmp = PyObject_RichCompareBool( |
| 3011 | keyword, nm, Py_EQ); |
| 3012 | if (cmp > 0) |
Antoine Pitrou | c2cc80c | 2008-07-25 22:13:52 +0000 | [diff] [blame] | 3013 | goto kw_found; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3014 | else if (cmp < 0) |
| 3015 | goto fail; |
| 3016 | } |
| 3017 | /* Check errors from Compare */ |
| 3018 | if (PyErr_Occurred()) |
| 3019 | goto fail; |
| 3020 | if (j >= co->co_argcount) { |
| 3021 | if (kwdict == NULL) { |
Benjamin Peterson | e18ef19 | 2009-01-20 14:21:16 +0000 | [diff] [blame] | 3022 | PyObject *kwd_str = kwd_as_string(keyword); |
| 3023 | if (kwd_str) { |
| 3024 | PyErr_Format(PyExc_TypeError, |
| 3025 | "%.200s() got an unexpected " |
| 3026 | "keyword argument '%.400s'", |
| 3027 | PyString_AsString(co->co_name), |
| 3028 | PyString_AsString(kwd_str)); |
| 3029 | Py_DECREF(kwd_str); |
| 3030 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3031 | goto fail; |
| 3032 | } |
| 3033 | PyDict_SetItem(kwdict, keyword, value); |
Antoine Pitrou | c2cc80c | 2008-07-25 22:13:52 +0000 | [diff] [blame] | 3034 | continue; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3035 | } |
Antoine Pitrou | c2cc80c | 2008-07-25 22:13:52 +0000 | [diff] [blame] | 3036 | kw_found: |
| 3037 | if (GETLOCAL(j) != NULL) { |
Benjamin Peterson | e18ef19 | 2009-01-20 14:21:16 +0000 | [diff] [blame] | 3038 | PyObject *kwd_str = kwd_as_string(keyword); |
| 3039 | if (kwd_str) { |
| 3040 | PyErr_Format(PyExc_TypeError, |
| 3041 | "%.200s() got multiple " |
| 3042 | "values for keyword " |
| 3043 | "argument '%.400s'", |
| 3044 | PyString_AsString(co->co_name), |
| 3045 | PyString_AsString(kwd_str)); |
| 3046 | Py_DECREF(kwd_str); |
| 3047 | } |
Antoine Pitrou | c2cc80c | 2008-07-25 22:13:52 +0000 | [diff] [blame] | 3048 | goto fail; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3049 | } |
Antoine Pitrou | c2cc80c | 2008-07-25 22:13:52 +0000 | [diff] [blame] | 3050 | Py_INCREF(value); |
| 3051 | SETLOCAL(j, value); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3052 | } |
| 3053 | if (argcount < co->co_argcount) { |
| 3054 | int m = co->co_argcount - defcount; |
| 3055 | for (i = argcount; i < m; i++) { |
| 3056 | if (GETLOCAL(i) == NULL) { |
| 3057 | PyErr_Format(PyExc_TypeError, |
| 3058 | "%.200s() takes %s %d " |
| 3059 | "%sargument%s (%d given)", |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3060 | PyString_AsString(co->co_name), |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3061 | ((co->co_flags & CO_VARARGS) || |
| 3062 | defcount) ? "at least" |
| 3063 | : "exactly", |
| 3064 | m, kwcount ? "non-keyword " : "", |
| 3065 | m == 1 ? "" : "s", i); |
| 3066 | goto fail; |
| 3067 | } |
| 3068 | } |
| 3069 | if (n > m) |
| 3070 | i = n - m; |
| 3071 | else |
| 3072 | i = 0; |
| 3073 | for (; i < defcount; i++) { |
| 3074 | if (GETLOCAL(m+i) == NULL) { |
| 3075 | PyObject *def = defs[i]; |
| 3076 | Py_INCREF(def); |
| 3077 | SETLOCAL(m+i, def); |
| 3078 | } |
| 3079 | } |
| 3080 | } |
| 3081 | } |
| 3082 | else { |
| 3083 | if (argcount > 0 || kwcount > 0) { |
| 3084 | PyErr_Format(PyExc_TypeError, |
| 3085 | "%.200s() takes no arguments (%d given)", |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3086 | PyString_AsString(co->co_name), |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3087 | argcount + kwcount); |
| 3088 | goto fail; |
| 3089 | } |
| 3090 | } |
| 3091 | /* Allocate and initialize storage for cell vars, and copy free |
| 3092 | vars into frame. This isn't too efficient right now. */ |
Richard Jones | cebbefc | 2006-05-23 18:28:17 +0000 | [diff] [blame] | 3093 | if (PyTuple_GET_SIZE(co->co_cellvars)) { |
Neal Norwitz | 245ce8d | 2006-06-12 02:16:10 +0000 | [diff] [blame] | 3094 | int i, j, nargs, found; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3095 | char *cellname, *argname; |
| 3096 | PyObject *c; |
| 3097 | |
| 3098 | nargs = co->co_argcount; |
| 3099 | if (co->co_flags & CO_VARARGS) |
| 3100 | nargs++; |
| 3101 | if (co->co_flags & CO_VARKEYWORDS) |
| 3102 | nargs++; |
| 3103 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3104 | /* Initialize each cell var, taking into account |
| 3105 | cell vars that are initialized from arguments. |
| 3106 | |
| 3107 | Should arrange for the compiler to put cellvars |
| 3108 | that are arguments at the beginning of the cellvars |
| 3109 | list so that we can march over it more efficiently? |
| 3110 | */ |
Richard Jones | cebbefc | 2006-05-23 18:28:17 +0000 | [diff] [blame] | 3111 | for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3112 | cellname = PyString_AS_STRING( |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3113 | PyTuple_GET_ITEM(co->co_cellvars, i)); |
| 3114 | found = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3115 | for (j = 0; j < nargs; j++) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3116 | argname = PyString_AS_STRING( |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3117 | PyTuple_GET_ITEM(co->co_varnames, j)); |
| 3118 | if (strcmp(cellname, argname) == 0) { |
| 3119 | c = PyCell_New(GETLOCAL(j)); |
| 3120 | if (c == NULL) |
| 3121 | goto fail; |
Richard Jones | cebbefc | 2006-05-23 18:28:17 +0000 | [diff] [blame] | 3122 | GETLOCAL(co->co_nlocals + i) = c; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3123 | found = 1; |
| 3124 | break; |
| 3125 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3126 | } |
| 3127 | if (found == 0) { |
| 3128 | c = PyCell_New(NULL); |
| 3129 | if (c == NULL) |
| 3130 | goto fail; |
Richard Jones | cebbefc | 2006-05-23 18:28:17 +0000 | [diff] [blame] | 3131 | SETLOCAL(co->co_nlocals + i, c); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3132 | } |
| 3133 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3134 | } |
Richard Jones | cebbefc | 2006-05-23 18:28:17 +0000 | [diff] [blame] | 3135 | if (PyTuple_GET_SIZE(co->co_freevars)) { |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3136 | int i; |
Richard Jones | cebbefc | 2006-05-23 18:28:17 +0000 | [diff] [blame] | 3137 | for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) { |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3138 | PyObject *o = PyTuple_GET_ITEM(closure, i); |
| 3139 | Py_INCREF(o); |
Richard Jones | cebbefc | 2006-05-23 18:28:17 +0000 | [diff] [blame] | 3140 | freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3141 | } |
| 3142 | } |
| 3143 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3144 | if (co->co_flags & CO_GENERATOR) { |
Neil Schemenauer | 2b13ce8 | 2001-06-21 02:41:10 +0000 | [diff] [blame] | 3145 | /* Don't need to keep the reference to f_back, it will be set |
| 3146 | * when the generator is resumed. */ |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 3147 | Py_XDECREF(f->f_back); |
Neil Schemenauer | 2b13ce8 | 2001-06-21 02:41:10 +0000 | [diff] [blame] | 3148 | f->f_back = NULL; |
| 3149 | |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 3150 | PCALL(PCALL_GENERATOR); |
| 3151 | |
Neil Schemenauer | 2b13ce8 | 2001-06-21 02:41:10 +0000 | [diff] [blame] | 3152 | /* Create a new generator that owns the ready to run frame |
| 3153 | * and return that as the value. */ |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 3154 | return PyGen_New(f); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3155 | } |
| 3156 | |
Thomas Wouters | ae406c6 | 2007-09-19 17:27:43 +0000 | [diff] [blame] | 3157 | retval = PyEval_EvalFrameEx(f,0); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3158 | |
Thomas Wouters | ae406c6 | 2007-09-19 17:27:43 +0000 | [diff] [blame] | 3159 | fail: /* Jump here from prelude on failure */ |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3160 | |
Tim Peters | b13680b | 2001-11-27 23:29:29 +0000 | [diff] [blame] | 3161 | /* decref'ing the frame can cause __del__ methods to get invoked, |
| 3162 | which can call back into Python. While we're done with the |
| 3163 | current Python frame (f), the associated C stack is still in use, |
| 3164 | so recursion_depth must be boosted for the duration. |
| 3165 | */ |
| 3166 | assert(tstate != NULL); |
| 3167 | ++tstate->recursion_depth; |
Thomas Wouters | ae406c6 | 2007-09-19 17:27:43 +0000 | [diff] [blame] | 3168 | Py_DECREF(f); |
Tim Peters | b13680b | 2001-11-27 23:29:29 +0000 | [diff] [blame] | 3169 | --tstate->recursion_depth; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3170 | return retval; |
| 3171 | } |
| 3172 | |
| 3173 | |
Benjamin Peterson | 78821dd | 2009-01-25 17:15:10 +0000 | [diff] [blame] | 3174 | |
Benjamin Peterson | e18ef19 | 2009-01-20 14:21:16 +0000 | [diff] [blame] | 3175 | static PyObject * |
| 3176 | kwd_as_string(PyObject *kwd) { |
Benjamin Peterson | 78821dd | 2009-01-25 17:15:10 +0000 | [diff] [blame] | 3177 | #ifdef Py_USING_UNICODE |
Benjamin Peterson | e18ef19 | 2009-01-20 14:21:16 +0000 | [diff] [blame] | 3178 | if (PyString_Check(kwd)) { |
Benjamin Peterson | 78821dd | 2009-01-25 17:15:10 +0000 | [diff] [blame] | 3179 | #else |
| 3180 | assert(PyString_Check(kwd)); |
| 3181 | #endif |
Benjamin Peterson | e18ef19 | 2009-01-20 14:21:16 +0000 | [diff] [blame] | 3182 | Py_INCREF(kwd); |
| 3183 | return kwd; |
Benjamin Peterson | 78821dd | 2009-01-25 17:15:10 +0000 | [diff] [blame] | 3184 | #ifdef Py_USING_UNICODE |
Benjamin Peterson | e18ef19 | 2009-01-20 14:21:16 +0000 | [diff] [blame] | 3185 | } |
Benjamin Peterson | 78821dd | 2009-01-25 17:15:10 +0000 | [diff] [blame] | 3186 | return _PyUnicode_AsDefaultEncodedString(kwd, "replace"); |
| 3187 | #endif |
Benjamin Peterson | e18ef19 | 2009-01-20 14:21:16 +0000 | [diff] [blame] | 3188 | } |
| 3189 | |
| 3190 | |
Guido van Rossum | c9fbb72 | 2003-03-01 03:36:33 +0000 | [diff] [blame] | 3191 | /* Implementation notes for set_exc_info() and reset_exc_info(): |
| 3192 | |
| 3193 | - Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and |
| 3194 | 'exc_traceback'. These always travel together. |
| 3195 | |
| 3196 | - tstate->curexc_ZZZ is the "hot" exception that is set by |
| 3197 | PyErr_SetString(), cleared by PyErr_Clear(), and so on. |
| 3198 | |
| 3199 | - Once an exception is caught by an except clause, it is transferred |
| 3200 | from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info() |
| 3201 | can pick it up. This is the primary task of set_exc_info(). |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 3202 | XXX That can't be right: set_exc_info() doesn't look at tstate->curexc_ZZZ. |
Guido van Rossum | c9fbb72 | 2003-03-01 03:36:33 +0000 | [diff] [blame] | 3203 | |
| 3204 | - Now let me explain the complicated dance with frame->f_exc_ZZZ. |
| 3205 | |
| 3206 | Long ago, when none of this existed, there were just a few globals: |
| 3207 | one set corresponding to the "hot" exception, and one set |
| 3208 | corresponding to sys.exc_ZZZ. (Actually, the latter weren't C |
| 3209 | globals; they were simply stored as sys.exc_ZZZ. For backwards |
| 3210 | compatibility, they still are!) The problem was that in code like |
| 3211 | this: |
| 3212 | |
| 3213 | try: |
| 3214 | "something that may fail" |
| 3215 | except "some exception": |
| 3216 | "do something else first" |
| 3217 | "print the exception from sys.exc_ZZZ." |
| 3218 | |
| 3219 | if "do something else first" invoked something that raised and caught |
| 3220 | an exception, sys.exc_ZZZ were overwritten. That was a frequent |
| 3221 | cause of subtle bugs. I fixed this by changing the semantics as |
| 3222 | follows: |
| 3223 | |
| 3224 | - Within one frame, sys.exc_ZZZ will hold the last exception caught |
| 3225 | *in that frame*. |
| 3226 | |
| 3227 | - But initially, and as long as no exception is caught in a given |
| 3228 | frame, sys.exc_ZZZ will hold the last exception caught in the |
| 3229 | previous frame (or the frame before that, etc.). |
| 3230 | |
| 3231 | The first bullet fixed the bug in the above example. The second |
| 3232 | bullet was for backwards compatibility: it was (and is) common to |
| 3233 | have a function that is called when an exception is caught, and to |
| 3234 | have that function access the caught exception via sys.exc_ZZZ. |
| 3235 | (Example: traceback.print_exc()). |
| 3236 | |
| 3237 | At the same time I fixed the problem that sys.exc_ZZZ weren't |
| 3238 | thread-safe, by introducing sys.exc_info() which gets it from tstate; |
| 3239 | but that's really a separate improvement. |
| 3240 | |
| 3241 | The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ |
| 3242 | variables to what they were before the current frame was called. The |
| 3243 | set_exc_info() function saves them on the frame so that |
| 3244 | reset_exc_info() can restore them. The invariant is that |
| 3245 | frame->f_exc_ZZZ is NULL iff the current frame never caught an |
| 3246 | exception (where "catching" an exception applies only to successful |
| 3247 | except clauses); and if the current frame ever caught an exception, |
| 3248 | frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ |
| 3249 | at the start of the current frame. |
| 3250 | |
| 3251 | */ |
| 3252 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 3253 | static void |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3254 | set_exc_info(PyThreadState *tstate, |
| 3255 | PyObject *type, PyObject *value, PyObject *tb) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3256 | { |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 3257 | PyFrameObject *frame = tstate->frame; |
Guido van Rossum | df4c308 | 1997-05-20 17:06:11 +0000 | [diff] [blame] | 3258 | PyObject *tmp_type, *tmp_value, *tmp_tb; |
Barry Warsaw | 4249f54 | 1997-08-22 21:26:19 +0000 | [diff] [blame] | 3259 | |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 3260 | assert(type != NULL); |
| 3261 | assert(frame != NULL); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3262 | if (frame->f_exc_type == NULL) { |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 3263 | assert(frame->f_exc_value == NULL); |
| 3264 | assert(frame->f_exc_traceback == NULL); |
| 3265 | /* This frame didn't catch an exception before. */ |
| 3266 | /* Save previous exception of this thread in this frame. */ |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3267 | if (tstate->exc_type == NULL) { |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 3268 | /* XXX Why is this set to Py_None? */ |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3269 | Py_INCREF(Py_None); |
| 3270 | tstate->exc_type = Py_None; |
| 3271 | } |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 3272 | Py_INCREF(tstate->exc_type); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3273 | Py_XINCREF(tstate->exc_value); |
| 3274 | Py_XINCREF(tstate->exc_traceback); |
| 3275 | frame->f_exc_type = tstate->exc_type; |
| 3276 | frame->f_exc_value = tstate->exc_value; |
| 3277 | frame->f_exc_traceback = tstate->exc_traceback; |
| 3278 | } |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 3279 | /* Set new exception for this thread. */ |
Guido van Rossum | df4c308 | 1997-05-20 17:06:11 +0000 | [diff] [blame] | 3280 | tmp_type = tstate->exc_type; |
| 3281 | tmp_value = tstate->exc_value; |
| 3282 | tmp_tb = tstate->exc_traceback; |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 3283 | Py_INCREF(type); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3284 | Py_XINCREF(value); |
| 3285 | Py_XINCREF(tb); |
| 3286 | tstate->exc_type = type; |
| 3287 | tstate->exc_value = value; |
| 3288 | tstate->exc_traceback = tb; |
Guido van Rossum | df4c308 | 1997-05-20 17:06:11 +0000 | [diff] [blame] | 3289 | Py_XDECREF(tmp_type); |
| 3290 | Py_XDECREF(tmp_value); |
| 3291 | Py_XDECREF(tmp_tb); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3292 | /* For b/w compatibility */ |
| 3293 | PySys_SetObject("exc_type", type); |
| 3294 | PySys_SetObject("exc_value", value); |
| 3295 | PySys_SetObject("exc_traceback", tb); |
| 3296 | } |
| 3297 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 3298 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3299 | reset_exc_info(PyThreadState *tstate) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3300 | { |
| 3301 | PyFrameObject *frame; |
Guido van Rossum | df4c308 | 1997-05-20 17:06:11 +0000 | [diff] [blame] | 3302 | PyObject *tmp_type, *tmp_value, *tmp_tb; |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 3303 | |
| 3304 | /* It's a precondition that the thread state's frame caught an |
| 3305 | * exception -- verify in a debug build. |
| 3306 | */ |
| 3307 | assert(tstate != NULL); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3308 | frame = tstate->frame; |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 3309 | assert(frame != NULL); |
| 3310 | assert(frame->f_exc_type != NULL); |
| 3311 | |
| 3312 | /* Copy the frame's exception info back to the thread state. */ |
| 3313 | tmp_type = tstate->exc_type; |
| 3314 | tmp_value = tstate->exc_value; |
| 3315 | tmp_tb = tstate->exc_traceback; |
| 3316 | Py_INCREF(frame->f_exc_type); |
| 3317 | Py_XINCREF(frame->f_exc_value); |
| 3318 | Py_XINCREF(frame->f_exc_traceback); |
| 3319 | tstate->exc_type = frame->f_exc_type; |
| 3320 | tstate->exc_value = frame->f_exc_value; |
| 3321 | tstate->exc_traceback = frame->f_exc_traceback; |
| 3322 | Py_XDECREF(tmp_type); |
| 3323 | Py_XDECREF(tmp_value); |
| 3324 | Py_XDECREF(tmp_tb); |
| 3325 | |
| 3326 | /* For b/w compatibility */ |
| 3327 | PySys_SetObject("exc_type", frame->f_exc_type); |
| 3328 | PySys_SetObject("exc_value", frame->f_exc_value); |
| 3329 | PySys_SetObject("exc_traceback", frame->f_exc_traceback); |
| 3330 | |
| 3331 | /* Clear the frame's exception info. */ |
Guido van Rossum | df4c308 | 1997-05-20 17:06:11 +0000 | [diff] [blame] | 3332 | tmp_type = frame->f_exc_type; |
| 3333 | tmp_value = frame->f_exc_value; |
| 3334 | tmp_tb = frame->f_exc_traceback; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3335 | frame->f_exc_type = NULL; |
| 3336 | frame->f_exc_value = NULL; |
| 3337 | frame->f_exc_traceback = NULL; |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 3338 | Py_DECREF(tmp_type); |
Guido van Rossum | df4c308 | 1997-05-20 17:06:11 +0000 | [diff] [blame] | 3339 | Py_XDECREF(tmp_value); |
| 3340 | Py_XDECREF(tmp_tb); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3341 | } |
| 3342 | |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3343 | /* Logic for the raise statement (too complicated for inlining). |
| 3344 | This *consumes* a reference count to each of its arguments. */ |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 3345 | static enum why_code |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3346 | do_raise(PyObject *type, PyObject *value, PyObject *tb) |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3347 | { |
Guido van Rossum | d295f12 | 1998-04-09 21:39:57 +0000 | [diff] [blame] | 3348 | if (type == NULL) { |
| 3349 | /* Reraise */ |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 3350 | PyThreadState *tstate = PyThreadState_GET(); |
Guido van Rossum | d295f12 | 1998-04-09 21:39:57 +0000 | [diff] [blame] | 3351 | type = tstate->exc_type == NULL ? Py_None : tstate->exc_type; |
| 3352 | value = tstate->exc_value; |
| 3353 | tb = tstate->exc_traceback; |
| 3354 | Py_XINCREF(type); |
| 3355 | Py_XINCREF(value); |
| 3356 | Py_XINCREF(tb); |
| 3357 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3358 | |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3359 | /* We support the following forms of raise: |
| 3360 | raise <class>, <classinstance> |
| 3361 | raise <class>, <argument tuple> |
| 3362 | raise <class>, None |
| 3363 | raise <class>, <argument> |
| 3364 | raise <classinstance>, None |
| 3365 | raise <string>, <object> |
| 3366 | raise <string>, None |
| 3367 | |
| 3368 | An omitted second argument is the same as None. |
| 3369 | |
| 3370 | In addition, raise <tuple>, <anything> is the same as |
| 3371 | raising the tuple's first item (and it better have one!); |
| 3372 | this rule is applied recursively. |
| 3373 | |
| 3374 | Finally, an optional third argument can be supplied, which |
| 3375 | gives the traceback to be substituted (useful when |
| 3376 | re-raising an exception after examining it). */ |
| 3377 | |
| 3378 | /* First, check the traceback argument, replacing None with |
| 3379 | NULL. */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3380 | if (tb == Py_None) { |
| 3381 | Py_DECREF(tb); |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3382 | tb = NULL; |
| 3383 | } |
| 3384 | else if (tb != NULL && !PyTraceBack_Check(tb)) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3385 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3386 | "raise: arg 3 must be a traceback or None"); |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3387 | goto raise_error; |
| 3388 | } |
| 3389 | |
| 3390 | /* Next, replace a missing value with None */ |
| 3391 | if (value == NULL) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3392 | value = Py_None; |
| 3393 | Py_INCREF(value); |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3394 | } |
| 3395 | |
| 3396 | /* Next, repeatedly, replace a tuple exception with its first item */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3397 | while (PyTuple_Check(type) && PyTuple_Size(type) > 0) { |
| 3398 | PyObject *tmp = type; |
| 3399 | type = PyTuple_GET_ITEM(type, 0); |
| 3400 | Py_INCREF(type); |
| 3401 | Py_DECREF(tmp); |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3402 | } |
| 3403 | |
Brett Cannon | 129bd52 | 2007-01-30 21:34:36 +0000 | [diff] [blame] | 3404 | if (PyExceptionClass_Check(type)) |
Barry Warsaw | 4249f54 | 1997-08-22 21:26:19 +0000 | [diff] [blame] | 3405 | PyErr_NormalizeException(&type, &value, &tb); |
| 3406 | |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 3407 | else if (PyExceptionInstance_Check(type)) { |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3408 | /* Raising an instance. The value should be a dummy. */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3409 | if (value != Py_None) { |
| 3410 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3411 | "instance exception may not have a separate value"); |
| 3412 | goto raise_error; |
| 3413 | } |
| 3414 | else { |
| 3415 | /* Normalize to raise <class>, <instance> */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3416 | Py_DECREF(value); |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3417 | value = type; |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 3418 | type = PyExceptionInstance_Class(type); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3419 | Py_INCREF(type); |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3420 | } |
| 3421 | } |
| 3422 | else { |
| 3423 | /* Not something you can raise. You get an exception |
| 3424 | anyway, just not what you specified :-) */ |
Jeremy Hylton | 960d948 | 2001-04-27 02:25:33 +0000 | [diff] [blame] | 3425 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | f19a7b9 | 2008-04-27 18:40:21 +0000 | [diff] [blame] | 3426 | "exceptions must be classes or instances, not %s", |
| 3427 | type->ob_type->tp_name); |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3428 | goto raise_error; |
| 3429 | } |
Guido van Rossum | 504153d | 2008-03-18 04:26:48 +0000 | [diff] [blame] | 3430 | |
| 3431 | assert(PyExceptionClass_Check(type)); |
| 3432 | if (Py_Py3kWarningFlag && PyClass_Check(type)) { |
Benjamin Peterson | 9f4f481 | 2008-04-27 03:01:45 +0000 | [diff] [blame] | 3433 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
Benjamin Peterson | f19a7b9 | 2008-04-27 18:40:21 +0000 | [diff] [blame] | 3434 | "exceptions must derive from BaseException " |
| 3435 | "in 3.x", 1) < 0) |
Guido van Rossum | 504153d | 2008-03-18 04:26:48 +0000 | [diff] [blame] | 3436 | goto raise_error; |
| 3437 | } |
| 3438 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3439 | PyErr_Restore(type, value, tb); |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3440 | if (tb == NULL) |
| 3441 | return WHY_EXCEPTION; |
| 3442 | else |
| 3443 | return WHY_RERAISE; |
| 3444 | raise_error: |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3445 | Py_XDECREF(value); |
| 3446 | Py_XDECREF(type); |
| 3447 | Py_XDECREF(tb); |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3448 | return WHY_EXCEPTION; |
| 3449 | } |
| 3450 | |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 3451 | /* Iterate v argcnt times and store the results on the stack (via decreasing |
| 3452 | sp). Return 1 for success, 0 if error. */ |
| 3453 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 3454 | static int |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 3455 | unpack_iterable(PyObject *v, int argcnt, PyObject **sp) |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 3456 | { |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 3457 | int i = 0; |
| 3458 | PyObject *it; /* iter(v) */ |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 3459 | PyObject *w; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3460 | |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 3461 | assert(v != NULL); |
| 3462 | |
| 3463 | it = PyObject_GetIter(v); |
| 3464 | if (it == NULL) |
| 3465 | goto Error; |
| 3466 | |
| 3467 | for (; i < argcnt; i++) { |
| 3468 | w = PyIter_Next(it); |
| 3469 | if (w == NULL) { |
| 3470 | /* Iterator done, via error or exhaustion. */ |
| 3471 | if (!PyErr_Occurred()) { |
| 3472 | PyErr_Format(PyExc_ValueError, |
| 3473 | "need more than %d value%s to unpack", |
| 3474 | i, i == 1 ? "" : "s"); |
| 3475 | } |
| 3476 | goto Error; |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 3477 | } |
| 3478 | *--sp = w; |
| 3479 | } |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 3480 | |
| 3481 | /* We better have exhausted the iterator now. */ |
| 3482 | w = PyIter_Next(it); |
| 3483 | if (w == NULL) { |
| 3484 | if (PyErr_Occurred()) |
| 3485 | goto Error; |
| 3486 | Py_DECREF(it); |
| 3487 | return 1; |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 3488 | } |
Guido van Rossum | bb8f59a | 2001-12-03 19:33:25 +0000 | [diff] [blame] | 3489 | Py_DECREF(w); |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 3490 | PyErr_SetString(PyExc_ValueError, "too many values to unpack"); |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 3491 | /* fall through */ |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 3492 | Error: |
Barry Warsaw | 9101055 | 1997-08-25 22:30:51 +0000 | [diff] [blame] | 3493 | for (; i > 0; i--, sp++) |
| 3494 | Py_DECREF(*sp); |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 3495 | Py_XDECREF(it); |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 3496 | return 0; |
| 3497 | } |
| 3498 | |
| 3499 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 3500 | #ifdef LLTRACE |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 3501 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3502 | prtrace(PyObject *v, char *str) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3503 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3504 | printf("%s ", str); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3505 | if (PyObject_Print(v, stdout, 0) != 0) |
| 3506 | PyErr_Clear(); /* Don't know what else to do */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3507 | printf("\n"); |
Guido van Rossum | cc229ea | 2000-05-04 00:55:17 +0000 | [diff] [blame] | 3508 | return 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3509 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3510 | #endif |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3511 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 3512 | static void |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3513 | call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f) |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 3514 | { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3515 | PyObject *type, *value, *traceback, *arg; |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 3516 | int err; |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3517 | PyErr_Fetch(&type, &value, &traceback); |
Guido van Rossum | bd9ccca | 1992-04-09 14:58:08 +0000 | [diff] [blame] | 3518 | if (value == NULL) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3519 | value = Py_None; |
| 3520 | Py_INCREF(value); |
Guido van Rossum | bd9ccca | 1992-04-09 14:58:08 +0000 | [diff] [blame] | 3521 | } |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 3522 | arg = PyTuple_Pack(3, type, value, traceback); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 3523 | if (arg == NULL) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3524 | PyErr_Restore(type, value, traceback); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 3525 | return; |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 3526 | } |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3527 | err = call_trace(func, self, f, PyTrace_EXCEPTION, arg); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3528 | Py_DECREF(arg); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 3529 | if (err == 0) |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3530 | PyErr_Restore(type, value, traceback); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 3531 | else { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3532 | Py_XDECREF(type); |
| 3533 | Py_XDECREF(value); |
| 3534 | Py_XDECREF(traceback); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 3535 | } |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 3536 | } |
| 3537 | |
Amaury Forgeot d'Arc | 0d75f09 | 2007-11-13 21:54:28 +0000 | [diff] [blame] | 3538 | static int |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 3539 | call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame, |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 3540 | int what, PyObject *arg) |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 3541 | { |
| 3542 | PyObject *type, *value, *traceback; |
| 3543 | int err; |
| 3544 | PyErr_Fetch(&type, &value, &traceback); |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 3545 | err = call_trace(func, obj, frame, what, arg); |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 3546 | if (err == 0) |
Amaury Forgeot d'Arc | 0d75f09 | 2007-11-13 21:54:28 +0000 | [diff] [blame] | 3547 | { |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 3548 | PyErr_Restore(type, value, traceback); |
Amaury Forgeot d'Arc | 0d75f09 | 2007-11-13 21:54:28 +0000 | [diff] [blame] | 3549 | return 0; |
| 3550 | } |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 3551 | else { |
| 3552 | Py_XDECREF(type); |
| 3553 | Py_XDECREF(value); |
| 3554 | Py_XDECREF(traceback); |
Amaury Forgeot d'Arc | 0d75f09 | 2007-11-13 21:54:28 +0000 | [diff] [blame] | 3555 | return -1; |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 3556 | } |
| 3557 | } |
| 3558 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 3559 | static int |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3560 | call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame, |
| 3561 | int what, PyObject *arg) |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 3562 | { |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3563 | register PyThreadState *tstate = frame->f_tstate; |
| 3564 | int result; |
| 3565 | if (tstate->tracing) |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 3566 | return 0; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3567 | tstate->tracing++; |
Fred Drake | 9e3ad78 | 2001-07-03 23:39:52 +0000 | [diff] [blame] | 3568 | tstate->use_tracing = 0; |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3569 | result = func(obj, frame, what, arg); |
Fred Drake | 9e3ad78 | 2001-07-03 23:39:52 +0000 | [diff] [blame] | 3570 | tstate->use_tracing = ((tstate->c_tracefunc != NULL) |
| 3571 | || (tstate->c_profilefunc != NULL)); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3572 | tstate->tracing--; |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3573 | return result; |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 3574 | } |
| 3575 | |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 3576 | PyObject * |
| 3577 | _PyEval_CallTracing(PyObject *func, PyObject *args) |
| 3578 | { |
| 3579 | PyFrameObject *frame = PyEval_GetFrame(); |
| 3580 | PyThreadState *tstate = frame->f_tstate; |
| 3581 | int save_tracing = tstate->tracing; |
| 3582 | int save_use_tracing = tstate->use_tracing; |
| 3583 | PyObject *result; |
| 3584 | |
| 3585 | tstate->tracing = 0; |
| 3586 | tstate->use_tracing = ((tstate->c_tracefunc != NULL) |
| 3587 | || (tstate->c_profilefunc != NULL)); |
| 3588 | result = PyObject_Call(func, args, NULL); |
| 3589 | tstate->tracing = save_tracing; |
| 3590 | tstate->use_tracing = save_use_tracing; |
| 3591 | return result; |
| 3592 | } |
| 3593 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 3594 | static int |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 3595 | maybe_call_line_trace(Py_tracefunc func, PyObject *obj, |
Armin Rigo | bf57a14 | 2004-03-22 19:24:58 +0000 | [diff] [blame] | 3596 | PyFrameObject *frame, int *instr_lb, int *instr_ub, |
| 3597 | int *instr_prev) |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 3598 | { |
Michael W. Hudson | 006c752 | 2002-11-08 13:08:46 +0000 | [diff] [blame] | 3599 | int result = 0; |
| 3600 | |
Jeremy Hylton | a4ebc13 | 2006-04-18 14:47:00 +0000 | [diff] [blame] | 3601 | /* If the last instruction executed isn't in the current |
| 3602 | instruction window, reset the window. If the last |
| 3603 | instruction happens to fall at the start of a line or if it |
| 3604 | represents a jump backwards, call the trace function. |
| 3605 | */ |
Michael W. Hudson | 53d58bb | 2002-08-30 13:09:51 +0000 | [diff] [blame] | 3606 | if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) { |
Thomas Wouters | ae406c6 | 2007-09-19 17:27:43 +0000 | [diff] [blame] | 3607 | int line; |
| 3608 | PyAddrPair bounds; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 3609 | |
Thomas Wouters | ae406c6 | 2007-09-19 17:27:43 +0000 | [diff] [blame] | 3610 | line = PyCode_CheckLineNumber(frame->f_code, frame->f_lasti, |
| 3611 | &bounds); |
| 3612 | if (line >= 0) { |
Michael W. Hudson | 02ff6a9 | 2002-09-11 15:36:32 +0000 | [diff] [blame] | 3613 | frame->f_lineno = line; |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 3614 | result = call_trace(func, obj, frame, |
Michael W. Hudson | 006c752 | 2002-11-08 13:08:46 +0000 | [diff] [blame] | 3615 | PyTrace_LINE, Py_None); |
Thomas Wouters | ae406c6 | 2007-09-19 17:27:43 +0000 | [diff] [blame] | 3616 | } |
| 3617 | *instr_lb = bounds.ap_lower; |
| 3618 | *instr_ub = bounds.ap_upper; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 3619 | } |
Armin Rigo | bf57a14 | 2004-03-22 19:24:58 +0000 | [diff] [blame] | 3620 | else if (frame->f_lasti <= *instr_prev) { |
Jeremy Hylton | a4ebc13 | 2006-04-18 14:47:00 +0000 | [diff] [blame] | 3621 | result = call_trace(func, obj, frame, PyTrace_LINE, Py_None); |
Armin Rigo | bf57a14 | 2004-03-22 19:24:58 +0000 | [diff] [blame] | 3622 | } |
| 3623 | *instr_prev = frame->f_lasti; |
Michael W. Hudson | 006c752 | 2002-11-08 13:08:46 +0000 | [diff] [blame] | 3624 | return result; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 3625 | } |
| 3626 | |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3627 | void |
| 3628 | PyEval_SetProfile(Py_tracefunc func, PyObject *arg) |
Fred Drake | d083839 | 2001-06-16 21:02:31 +0000 | [diff] [blame] | 3629 | { |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 3630 | PyThreadState *tstate = PyThreadState_GET(); |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3631 | PyObject *temp = tstate->c_profileobj; |
| 3632 | Py_XINCREF(arg); |
| 3633 | tstate->c_profilefunc = NULL; |
| 3634 | tstate->c_profileobj = NULL; |
Brett Cannon | 55fa66d | 2005-06-25 07:07:35 +0000 | [diff] [blame] | 3635 | /* Must make sure that tracing is not ignored if 'temp' is freed */ |
Fred Drake | 9e3ad78 | 2001-07-03 23:39:52 +0000 | [diff] [blame] | 3636 | tstate->use_tracing = tstate->c_tracefunc != NULL; |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3637 | Py_XDECREF(temp); |
| 3638 | tstate->c_profilefunc = func; |
| 3639 | tstate->c_profileobj = arg; |
Brett Cannon | 55fa66d | 2005-06-25 07:07:35 +0000 | [diff] [blame] | 3640 | /* Flag that tracing or profiling is turned on */ |
Fred Drake | 9e3ad78 | 2001-07-03 23:39:52 +0000 | [diff] [blame] | 3641 | tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL); |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3642 | } |
| 3643 | |
| 3644 | void |
| 3645 | PyEval_SetTrace(Py_tracefunc func, PyObject *arg) |
| 3646 | { |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 3647 | PyThreadState *tstate = PyThreadState_GET(); |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3648 | PyObject *temp = tstate->c_traceobj; |
Jeffrey Yasskin | fd8a1ec | 2008-12-03 06:46:45 +0000 | [diff] [blame] | 3649 | _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL); |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3650 | Py_XINCREF(arg); |
| 3651 | tstate->c_tracefunc = NULL; |
| 3652 | tstate->c_traceobj = NULL; |
Brett Cannon | 55fa66d | 2005-06-25 07:07:35 +0000 | [diff] [blame] | 3653 | /* Must make sure that profiling is not ignored if 'temp' is freed */ |
Fred Drake | 9e3ad78 | 2001-07-03 23:39:52 +0000 | [diff] [blame] | 3654 | tstate->use_tracing = tstate->c_profilefunc != NULL; |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3655 | Py_XDECREF(temp); |
| 3656 | tstate->c_tracefunc = func; |
| 3657 | tstate->c_traceobj = arg; |
Brett Cannon | 55fa66d | 2005-06-25 07:07:35 +0000 | [diff] [blame] | 3658 | /* Flag that tracing or profiling is turned on */ |
Fred Drake | 9e3ad78 | 2001-07-03 23:39:52 +0000 | [diff] [blame] | 3659 | tstate->use_tracing = ((func != NULL) |
| 3660 | || (tstate->c_profilefunc != NULL)); |
Fred Drake | d083839 | 2001-06-16 21:02:31 +0000 | [diff] [blame] | 3661 | } |
| 3662 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3663 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3664 | PyEval_GetBuiltins(void) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 3665 | { |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 3666 | PyFrameObject *current_frame = PyEval_GetFrame(); |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 3667 | if (current_frame == NULL) |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 3668 | return PyThreadState_GET()->interp->builtins; |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 3669 | else |
| 3670 | return current_frame->f_builtins; |
| 3671 | } |
| 3672 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3673 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3674 | PyEval_GetLocals(void) |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 3675 | { |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 3676 | PyFrameObject *current_frame = PyEval_GetFrame(); |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 3677 | if (current_frame == NULL) |
| 3678 | return NULL; |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3679 | PyFrame_FastToLocals(current_frame); |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 3680 | return current_frame->f_locals; |
| 3681 | } |
| 3682 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3683 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3684 | PyEval_GetGlobals(void) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3685 | { |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 3686 | PyFrameObject *current_frame = PyEval_GetFrame(); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3687 | if (current_frame == NULL) |
| 3688 | return NULL; |
| 3689 | else |
| 3690 | return current_frame->f_globals; |
| 3691 | } |
| 3692 | |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 3693 | PyFrameObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3694 | PyEval_GetFrame(void) |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 3695 | { |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 3696 | PyThreadState *tstate = PyThreadState_GET(); |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 3697 | return _PyThreadState_GetFrame(tstate); |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 3698 | } |
| 3699 | |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 3700 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3701 | PyEval_GetRestricted(void) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 3702 | { |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 3703 | PyFrameObject *current_frame = PyEval_GetFrame(); |
Neal Norwitz | b9845e7 | 2006-06-12 02:11:18 +0000 | [diff] [blame] | 3704 | return current_frame == NULL ? 0 : PyFrame_IsRestricted(current_frame); |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 3705 | } |
| 3706 | |
Guido van Rossum | be27026 | 1997-05-22 22:26:18 +0000 | [diff] [blame] | 3707 | int |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 3708 | PyEval_MergeCompilerFlags(PyCompilerFlags *cf) |
Jeremy Hylton | 061d106 | 2001-03-22 02:32:48 +0000 | [diff] [blame] | 3709 | { |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 3710 | PyFrameObject *current_frame = PyEval_GetFrame(); |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 3711 | int result = cf->cf_flags != 0; |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 3712 | |
| 3713 | if (current_frame != NULL) { |
| 3714 | const int codeflags = current_frame->f_code->co_flags; |
Tim Peters | e2c18e9 | 2001-08-17 20:47:47 +0000 | [diff] [blame] | 3715 | const int compilerflags = codeflags & PyCF_MASK; |
| 3716 | if (compilerflags) { |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 3717 | result = 1; |
Tim Peters | e2c18e9 | 2001-08-17 20:47:47 +0000 | [diff] [blame] | 3718 | cf->cf_flags |= compilerflags; |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 3719 | } |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 3720 | #if 0 /* future keyword */ |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 3721 | if (codeflags & CO_GENERATOR_ALLOWED) { |
| 3722 | result = 1; |
| 3723 | cf->cf_flags |= CO_GENERATOR_ALLOWED; |
| 3724 | } |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 3725 | #endif |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 3726 | } |
| 3727 | return result; |
Jeremy Hylton | 061d106 | 2001-03-22 02:32:48 +0000 | [diff] [blame] | 3728 | } |
| 3729 | |
| 3730 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3731 | Py_FlushLine(void) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3732 | { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3733 | PyObject *f = PySys_GetObject("stdout"); |
Guido van Rossum | be27026 | 1997-05-22 22:26:18 +0000 | [diff] [blame] | 3734 | if (f == NULL) |
| 3735 | return 0; |
| 3736 | if (!PyFile_SoftSpace(f, 0)) |
| 3737 | return 0; |
| 3738 | return PyFile_WriteString("\n", f); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3739 | } |
| 3740 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3741 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3742 | /* External interface to call any callable object. |
| 3743 | The arg must be a tuple or NULL. */ |
Guido van Rossum | 83bf35c | 1991-07-27 21:32:34 +0000 | [diff] [blame] | 3744 | |
Guido van Rossum | d7ed683 | 1997-08-30 15:02:50 +0000 | [diff] [blame] | 3745 | #undef PyEval_CallObject |
| 3746 | /* for backward compatibility: export this interface */ |
| 3747 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3748 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3749 | PyEval_CallObject(PyObject *func, PyObject *arg) |
Guido van Rossum | 83bf35c | 1991-07-27 21:32:34 +0000 | [diff] [blame] | 3750 | { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3751 | return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3752 | } |
Guido van Rossum | d7ed683 | 1997-08-30 15:02:50 +0000 | [diff] [blame] | 3753 | #define PyEval_CallObject(func,arg) \ |
| 3754 | PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL) |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 3755 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3756 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3757 | PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3758 | { |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3759 | PyObject *result; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3760 | |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3761 | if (arg == NULL) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3762 | arg = PyTuple_New(0); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3763 | if (arg == NULL) |
| 3764 | return NULL; |
| 3765 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3766 | else if (!PyTuple_Check(arg)) { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3767 | PyErr_SetString(PyExc_TypeError, |
| 3768 | "argument list must be a tuple"); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3769 | return NULL; |
| 3770 | } |
| 3771 | else |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3772 | Py_INCREF(arg); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3773 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3774 | if (kw != NULL && !PyDict_Check(kw)) { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3775 | PyErr_SetString(PyExc_TypeError, |
| 3776 | "keyword list must be a dictionary"); |
Guido van Rossum | 25826c9 | 2000-04-21 21:17:39 +0000 | [diff] [blame] | 3777 | Py_DECREF(arg); |
Guido van Rossum | e3e61c1 | 1995-08-04 04:14:47 +0000 | [diff] [blame] | 3778 | return NULL; |
| 3779 | } |
| 3780 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3781 | result = PyObject_Call(func, arg, kw); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3782 | Py_DECREF(arg); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3783 | return result; |
| 3784 | } |
| 3785 | |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 3786 | const char * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3787 | PyEval_GetFuncName(PyObject *func) |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 3788 | { |
| 3789 | if (PyMethod_Check(func)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3790 | return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func)); |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 3791 | else if (PyFunction_Check(func)) |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3792 | return PyString_AsString(((PyFunctionObject*)func)->func_name); |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 3793 | else if (PyCFunction_Check(func)) |
| 3794 | return ((PyCFunctionObject*)func)->m_ml->ml_name; |
| 3795 | else if (PyClass_Check(func)) |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3796 | return PyString_AsString(((PyClassObject*)func)->cl_name); |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 3797 | else if (PyInstance_Check(func)) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3798 | return PyString_AsString( |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 3799 | ((PyInstanceObject*)func)->in_class->cl_name); |
| 3800 | } else { |
| 3801 | return func->ob_type->tp_name; |
| 3802 | } |
| 3803 | } |
| 3804 | |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 3805 | const char * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3806 | PyEval_GetFuncDesc(PyObject *func) |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 3807 | { |
| 3808 | if (PyMethod_Check(func)) |
| 3809 | return "()"; |
| 3810 | else if (PyFunction_Check(func)) |
| 3811 | return "()"; |
| 3812 | else if (PyCFunction_Check(func)) |
| 3813 | return "()"; |
| 3814 | else if (PyClass_Check(func)) |
| 3815 | return " constructor"; |
| 3816 | else if (PyInstance_Check(func)) { |
| 3817 | return " instance"; |
| 3818 | } else { |
| 3819 | return " object"; |
| 3820 | } |
| 3821 | } |
| 3822 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 3823 | static void |
Jeremy Hylton | 192690e | 2002-08-16 18:36:11 +0000 | [diff] [blame] | 3824 | err_args(PyObject *func, int flags, int nargs) |
| 3825 | { |
| 3826 | if (flags & METH_NOARGS) |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 3827 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 86c659a | 2002-08-23 14:11:35 +0000 | [diff] [blame] | 3828 | "%.200s() takes no arguments (%d given)", |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 3829 | ((PyCFunctionObject *)func)->m_ml->ml_name, |
Jeremy Hylton | 192690e | 2002-08-16 18:36:11 +0000 | [diff] [blame] | 3830 | nargs); |
| 3831 | else |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 3832 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 86c659a | 2002-08-23 14:11:35 +0000 | [diff] [blame] | 3833 | "%.200s() takes exactly one argument (%d given)", |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 3834 | ((PyCFunctionObject *)func)->m_ml->ml_name, |
Jeremy Hylton | 192690e | 2002-08-16 18:36:11 +0000 | [diff] [blame] | 3835 | nargs); |
| 3836 | } |
| 3837 | |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 3838 | #define C_TRACE(x, call) \ |
Nicholas Bastin | d858a77 | 2004-06-25 23:31:06 +0000 | [diff] [blame] | 3839 | if (tstate->use_tracing && tstate->c_profilefunc) { \ |
| 3840 | if (call_trace(tstate->c_profilefunc, \ |
| 3841 | tstate->c_profileobj, \ |
| 3842 | tstate->frame, PyTrace_C_CALL, \ |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 3843 | func)) { \ |
| 3844 | x = NULL; \ |
| 3845 | } \ |
| 3846 | else { \ |
| 3847 | x = call; \ |
| 3848 | if (tstate->c_profilefunc != NULL) { \ |
| 3849 | if (x == NULL) { \ |
| 3850 | call_trace_protected(tstate->c_profilefunc, \ |
| 3851 | tstate->c_profileobj, \ |
| 3852 | tstate->frame, PyTrace_C_EXCEPTION, \ |
| 3853 | func); \ |
| 3854 | /* XXX should pass (type, value, tb) */ \ |
| 3855 | } else { \ |
| 3856 | if (call_trace(tstate->c_profilefunc, \ |
| 3857 | tstate->c_profileobj, \ |
| 3858 | tstate->frame, PyTrace_C_RETURN, \ |
| 3859 | func)) { \ |
| 3860 | Py_DECREF(x); \ |
| 3861 | x = NULL; \ |
| 3862 | } \ |
| 3863 | } \ |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 3864 | } \ |
Nicholas Bastin | d858a77 | 2004-06-25 23:31:06 +0000 | [diff] [blame] | 3865 | } \ |
| 3866 | } else { \ |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 3867 | x = call; \ |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 3868 | } |
| 3869 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 3870 | static PyObject * |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 3871 | call_function(PyObject ***pp_stack, int oparg |
| 3872 | #ifdef WITH_TSC |
| 3873 | , uint64* pintr0, uint64* pintr1 |
| 3874 | #endif |
| 3875 | ) |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 3876 | { |
| 3877 | int na = oparg & 0xff; |
| 3878 | int nk = (oparg>>8) & 0xff; |
| 3879 | int n = na + 2 * nk; |
| 3880 | PyObject **pfunc = (*pp_stack) - n - 1; |
| 3881 | PyObject *func = *pfunc; |
| 3882 | PyObject *x, *w; |
| 3883 | |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 3884 | /* Always dispatch PyCFunction first, because these are |
| 3885 | presumed to be the most frequent callable object. |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 3886 | */ |
| 3887 | if (PyCFunction_Check(func) && nk == 0) { |
| 3888 | int flags = PyCFunction_GET_FLAGS(func); |
Nicholas Bastin | d858a77 | 2004-06-25 23:31:06 +0000 | [diff] [blame] | 3889 | PyThreadState *tstate = PyThreadState_GET(); |
Raymond Hettinger | a7f56bc | 2004-06-26 04:34:33 +0000 | [diff] [blame] | 3890 | |
| 3891 | PCALL(PCALL_CFUNCTION); |
Jeremy Hylton | 192690e | 2002-08-16 18:36:11 +0000 | [diff] [blame] | 3892 | if (flags & (METH_NOARGS | METH_O)) { |
| 3893 | PyCFunction meth = PyCFunction_GET_FUNCTION(func); |
| 3894 | PyObject *self = PyCFunction_GET_SELF(func); |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 3895 | if (flags & METH_NOARGS && na == 0) { |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 3896 | C_TRACE(x, (*meth)(self,NULL)); |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 3897 | } |
Jeremy Hylton | 192690e | 2002-08-16 18:36:11 +0000 | [diff] [blame] | 3898 | else if (flags & METH_O && na == 1) { |
| 3899 | PyObject *arg = EXT_POP(*pp_stack); |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 3900 | C_TRACE(x, (*meth)(self,arg)); |
Jeremy Hylton | 192690e | 2002-08-16 18:36:11 +0000 | [diff] [blame] | 3901 | Py_DECREF(arg); |
| 3902 | } |
| 3903 | else { |
| 3904 | err_args(func, flags, na); |
| 3905 | x = NULL; |
| 3906 | } |
| 3907 | } |
| 3908 | else { |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 3909 | PyObject *callargs; |
| 3910 | callargs = load_args(pp_stack, na); |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 3911 | READ_TIMESTAMP(*pintr0); |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 3912 | C_TRACE(x, PyCFunction_Call(func,callargs,NULL)); |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 3913 | READ_TIMESTAMP(*pintr1); |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 3914 | Py_XDECREF(callargs); |
| 3915 | } |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 3916 | } else { |
| 3917 | if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) { |
| 3918 | /* optimize access to bound methods */ |
| 3919 | PyObject *self = PyMethod_GET_SELF(func); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 3920 | PCALL(PCALL_METHOD); |
| 3921 | PCALL(PCALL_BOUND_METHOD); |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 3922 | Py_INCREF(self); |
| 3923 | func = PyMethod_GET_FUNCTION(func); |
| 3924 | Py_INCREF(func); |
| 3925 | Py_DECREF(*pfunc); |
| 3926 | *pfunc = self; |
| 3927 | na++; |
| 3928 | n++; |
| 3929 | } else |
| 3930 | Py_INCREF(func); |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 3931 | READ_TIMESTAMP(*pintr0); |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 3932 | if (PyFunction_Check(func)) |
| 3933 | x = fast_function(func, pp_stack, n, na, nk); |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 3934 | else |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 3935 | x = do_call(func, pp_stack, na, nk); |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 3936 | READ_TIMESTAMP(*pintr1); |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 3937 | Py_DECREF(func); |
| 3938 | } |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 3939 | |
Armin Rigo | d34fa52 | 2006-03-28 19:10:40 +0000 | [diff] [blame] | 3940 | /* Clear the stack of the function object. Also removes |
| 3941 | the arguments in case they weren't consumed already |
| 3942 | (fast_function() and err_args() leave them on the stack). |
Thomas Wouters | 7f59732 | 2006-03-01 05:32:33 +0000 | [diff] [blame] | 3943 | */ |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 3944 | while ((*pp_stack) > pfunc) { |
| 3945 | w = EXT_POP(*pp_stack); |
| 3946 | Py_DECREF(w); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 3947 | PCALL(PCALL_POP); |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 3948 | } |
| 3949 | return x; |
| 3950 | } |
| 3951 | |
Jeremy Hylton | 192690e | 2002-08-16 18:36:11 +0000 | [diff] [blame] | 3952 | /* The fast_function() function optimize calls for which no argument |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3953 | tuple is necessary; the objects are passed directly from the stack. |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 3954 | For the simplest case -- a function that takes only positional |
| 3955 | arguments and is called with only positional arguments -- it |
| 3956 | inlines the most primitive frame setup code from |
| 3957 | PyEval_EvalCodeEx(), which vastly reduces the checks that must be |
| 3958 | done before evaluating the frame. |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3959 | */ |
| 3960 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 3961 | static PyObject * |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3962 | fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk) |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3963 | { |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 3964 | PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3965 | PyObject *globals = PyFunction_GET_GLOBALS(func); |
| 3966 | PyObject *argdefs = PyFunction_GET_DEFAULTS(func); |
| 3967 | PyObject **d = NULL; |
| 3968 | int nd = 0; |
| 3969 | |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 3970 | PCALL(PCALL_FUNCTION); |
| 3971 | PCALL(PCALL_FAST_FUNCTION); |
Raymond Hettinger | 40174c3 | 2003-05-31 07:04:16 +0000 | [diff] [blame] | 3972 | if (argdefs == NULL && co->co_argcount == n && nk==0 && |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 3973 | co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) { |
| 3974 | PyFrameObject *f; |
| 3975 | PyObject *retval = NULL; |
| 3976 | PyThreadState *tstate = PyThreadState_GET(); |
| 3977 | PyObject **fastlocals, **stack; |
| 3978 | int i; |
| 3979 | |
| 3980 | PCALL(PCALL_FASTER_FUNCTION); |
| 3981 | assert(globals != NULL); |
| 3982 | /* XXX Perhaps we should create a specialized |
| 3983 | PyFrame_New() that doesn't take locals, but does |
| 3984 | take builtins without sanity checking them. |
| 3985 | */ |
Neal Norwitz | df6a649 | 2006-08-13 18:10:10 +0000 | [diff] [blame] | 3986 | assert(tstate != NULL); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 3987 | f = PyFrame_New(tstate, co, globals, NULL); |
| 3988 | if (f == NULL) |
| 3989 | return NULL; |
| 3990 | |
| 3991 | fastlocals = f->f_localsplus; |
| 3992 | stack = (*pp_stack) - n; |
| 3993 | |
| 3994 | for (i = 0; i < n; i++) { |
| 3995 | Py_INCREF(*stack); |
| 3996 | fastlocals[i] = *stack++; |
| 3997 | } |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 3998 | retval = PyEval_EvalFrameEx(f,0); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 3999 | ++tstate->recursion_depth; |
| 4000 | Py_DECREF(f); |
| 4001 | --tstate->recursion_depth; |
| 4002 | return retval; |
| 4003 | } |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4004 | if (argdefs != NULL) { |
| 4005 | d = &PyTuple_GET_ITEM(argdefs, 0); |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4006 | nd = Py_SIZE(argdefs); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4007 | } |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4008 | return PyEval_EvalCodeEx(co, globals, |
| 4009 | (PyObject *)NULL, (*pp_stack)-n, na, |
| 4010 | (*pp_stack)-2*nk, nk, d, nd, |
| 4011 | PyFunction_GET_CLOSURE(func)); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4012 | } |
| 4013 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4014 | static PyObject * |
Ka-Ping Yee | 2057970 | 2001-01-15 22:14:16 +0000 | [diff] [blame] | 4015 | update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack, |
| 4016 | PyObject *func) |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4017 | { |
| 4018 | PyObject *kwdict = NULL; |
| 4019 | if (orig_kwdict == NULL) |
| 4020 | kwdict = PyDict_New(); |
| 4021 | else { |
| 4022 | kwdict = PyDict_Copy(orig_kwdict); |
| 4023 | Py_DECREF(orig_kwdict); |
| 4024 | } |
| 4025 | if (kwdict == NULL) |
| 4026 | return NULL; |
Raymond Hettinger | 5bed456 | 2004-04-10 23:34:17 +0000 | [diff] [blame] | 4027 | while (--nk >= 0) { |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4028 | int err; |
| 4029 | PyObject *value = EXT_POP(*pp_stack); |
| 4030 | PyObject *key = EXT_POP(*pp_stack); |
| 4031 | if (PyDict_GetItem(kwdict, key) != NULL) { |
Thomas Wouters | ae406c6 | 2007-09-19 17:27:43 +0000 | [diff] [blame] | 4032 | PyErr_Format(PyExc_TypeError, |
| 4033 | "%.200s%s got multiple values " |
| 4034 | "for keyword argument '%.200s'", |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4035 | PyEval_GetFuncName(func), |
| 4036 | PyEval_GetFuncDesc(func), |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4037 | PyString_AsString(key)); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4038 | Py_DECREF(key); |
| 4039 | Py_DECREF(value); |
| 4040 | Py_DECREF(kwdict); |
| 4041 | return NULL; |
| 4042 | } |
| 4043 | err = PyDict_SetItem(kwdict, key, value); |
| 4044 | Py_DECREF(key); |
| 4045 | Py_DECREF(value); |
| 4046 | if (err) { |
| 4047 | Py_DECREF(kwdict); |
| 4048 | return NULL; |
| 4049 | } |
| 4050 | } |
| 4051 | return kwdict; |
| 4052 | } |
| 4053 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4054 | static PyObject * |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4055 | update_star_args(int nstack, int nstar, PyObject *stararg, |
| 4056 | PyObject ***pp_stack) |
| 4057 | { |
| 4058 | PyObject *callargs, *w; |
| 4059 | |
| 4060 | callargs = PyTuple_New(nstack + nstar); |
| 4061 | if (callargs == NULL) { |
| 4062 | return NULL; |
| 4063 | } |
| 4064 | if (nstar) { |
| 4065 | int i; |
| 4066 | for (i = 0; i < nstar; i++) { |
| 4067 | PyObject *a = PyTuple_GET_ITEM(stararg, i); |
| 4068 | Py_INCREF(a); |
| 4069 | PyTuple_SET_ITEM(callargs, nstack + i, a); |
| 4070 | } |
| 4071 | } |
Raymond Hettinger | 5bed456 | 2004-04-10 23:34:17 +0000 | [diff] [blame] | 4072 | while (--nstack >= 0) { |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4073 | w = EXT_POP(*pp_stack); |
| 4074 | PyTuple_SET_ITEM(callargs, nstack, w); |
| 4075 | } |
| 4076 | return callargs; |
| 4077 | } |
| 4078 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4079 | static PyObject * |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4080 | load_args(PyObject ***pp_stack, int na) |
| 4081 | { |
| 4082 | PyObject *args = PyTuple_New(na); |
| 4083 | PyObject *w; |
| 4084 | |
| 4085 | if (args == NULL) |
| 4086 | return NULL; |
Raymond Hettinger | 5bed456 | 2004-04-10 23:34:17 +0000 | [diff] [blame] | 4087 | while (--na >= 0) { |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4088 | w = EXT_POP(*pp_stack); |
| 4089 | PyTuple_SET_ITEM(args, na, w); |
| 4090 | } |
| 4091 | return args; |
| 4092 | } |
| 4093 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4094 | static PyObject * |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4095 | do_call(PyObject *func, PyObject ***pp_stack, int na, int nk) |
| 4096 | { |
| 4097 | PyObject *callargs = NULL; |
| 4098 | PyObject *kwdict = NULL; |
| 4099 | PyObject *result = NULL; |
| 4100 | |
| 4101 | if (nk > 0) { |
Ka-Ping Yee | 2057970 | 2001-01-15 22:14:16 +0000 | [diff] [blame] | 4102 | kwdict = update_keyword_args(NULL, nk, pp_stack, func); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4103 | if (kwdict == NULL) |
| 4104 | goto call_fail; |
| 4105 | } |
| 4106 | callargs = load_args(pp_stack, na); |
| 4107 | if (callargs == NULL) |
| 4108 | goto call_fail; |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4109 | #ifdef CALL_PROFILE |
| 4110 | /* At this point, we have to look at the type of func to |
| 4111 | update the call stats properly. Do it here so as to avoid |
| 4112 | exposing the call stats machinery outside ceval.c |
| 4113 | */ |
| 4114 | if (PyFunction_Check(func)) |
| 4115 | PCALL(PCALL_FUNCTION); |
| 4116 | else if (PyMethod_Check(func)) |
| 4117 | PCALL(PCALL_METHOD); |
| 4118 | else if (PyType_Check(func)) |
| 4119 | PCALL(PCALL_TYPE); |
| 4120 | else |
| 4121 | PCALL(PCALL_OTHER); |
| 4122 | #endif |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4123 | result = PyObject_Call(func, callargs, kwdict); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4124 | call_fail: |
| 4125 | Py_XDECREF(callargs); |
| 4126 | Py_XDECREF(kwdict); |
| 4127 | return result; |
| 4128 | } |
| 4129 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4130 | static PyObject * |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4131 | ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk) |
| 4132 | { |
| 4133 | int nstar = 0; |
| 4134 | PyObject *callargs = NULL; |
| 4135 | PyObject *stararg = NULL; |
| 4136 | PyObject *kwdict = NULL; |
| 4137 | PyObject *result = NULL; |
| 4138 | |
| 4139 | if (flags & CALL_FLAG_KW) { |
| 4140 | kwdict = EXT_POP(*pp_stack); |
Georg Brandl | 2134e75 | 2007-05-21 20:34:16 +0000 | [diff] [blame] | 4141 | if (!PyDict_Check(kwdict)) { |
| 4142 | PyObject *d; |
| 4143 | d = PyDict_New(); |
| 4144 | if (d == NULL) |
| 4145 | goto ext_call_fail; |
| 4146 | if (PyDict_Update(d, kwdict) != 0) { |
| 4147 | Py_DECREF(d); |
| 4148 | /* PyDict_Update raises attribute |
| 4149 | * error (percolated from an attempt |
| 4150 | * to get 'keys' attribute) instead of |
| 4151 | * a type error if its second argument |
| 4152 | * is not a mapping. |
| 4153 | */ |
| 4154 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 4155 | PyErr_Format(PyExc_TypeError, |
| 4156 | "%.200s%.200s argument after ** " |
| 4157 | "must be a mapping, not %.200s", |
| 4158 | PyEval_GetFuncName(func), |
| 4159 | PyEval_GetFuncDesc(func), |
| 4160 | kwdict->ob_type->tp_name); |
| 4161 | } |
| 4162 | goto ext_call_fail; |
| 4163 | } |
| 4164 | Py_DECREF(kwdict); |
| 4165 | kwdict = d; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4166 | } |
| 4167 | } |
| 4168 | if (flags & CALL_FLAG_VAR) { |
| 4169 | stararg = EXT_POP(*pp_stack); |
| 4170 | if (!PyTuple_Check(stararg)) { |
| 4171 | PyObject *t = NULL; |
| 4172 | t = PySequence_Tuple(stararg); |
| 4173 | if (t == NULL) { |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4174 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 4175 | PyErr_Format(PyExc_TypeError, |
Georg Brandl | 2134e75 | 2007-05-21 20:34:16 +0000 | [diff] [blame] | 4176 | "%.200s%.200s argument after * " |
| 4177 | "must be a sequence, not %200s", |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4178 | PyEval_GetFuncName(func), |
Georg Brandl | 2134e75 | 2007-05-21 20:34:16 +0000 | [diff] [blame] | 4179 | PyEval_GetFuncDesc(func), |
| 4180 | stararg->ob_type->tp_name); |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4181 | } |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4182 | goto ext_call_fail; |
| 4183 | } |
| 4184 | Py_DECREF(stararg); |
| 4185 | stararg = t; |
| 4186 | } |
| 4187 | nstar = PyTuple_GET_SIZE(stararg); |
| 4188 | } |
| 4189 | if (nk > 0) { |
Ka-Ping Yee | 2057970 | 2001-01-15 22:14:16 +0000 | [diff] [blame] | 4190 | kwdict = update_keyword_args(kwdict, nk, pp_stack, func); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4191 | if (kwdict == NULL) |
| 4192 | goto ext_call_fail; |
| 4193 | } |
| 4194 | callargs = update_star_args(na, nstar, stararg, pp_stack); |
| 4195 | if (callargs == NULL) |
| 4196 | goto ext_call_fail; |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4197 | #ifdef CALL_PROFILE |
| 4198 | /* At this point, we have to look at the type of func to |
| 4199 | update the call stats properly. Do it here so as to avoid |
| 4200 | exposing the call stats machinery outside ceval.c |
| 4201 | */ |
| 4202 | if (PyFunction_Check(func)) |
| 4203 | PCALL(PCALL_FUNCTION); |
| 4204 | else if (PyMethod_Check(func)) |
| 4205 | PCALL(PCALL_METHOD); |
| 4206 | else if (PyType_Check(func)) |
| 4207 | PCALL(PCALL_TYPE); |
| 4208 | else |
| 4209 | PCALL(PCALL_OTHER); |
| 4210 | #endif |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4211 | result = PyObject_Call(func, callargs, kwdict); |
Thomas Wouters | ae406c6 | 2007-09-19 17:27:43 +0000 | [diff] [blame] | 4212 | ext_call_fail: |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4213 | Py_XDECREF(callargs); |
| 4214 | Py_XDECREF(kwdict); |
| 4215 | Py_XDECREF(stararg); |
| 4216 | return result; |
| 4217 | } |
| 4218 | |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 4219 | /* Extract a slice index from a PyInt or PyLong or an object with the |
| 4220 | nb_index slot defined, and store in *pi. |
| 4221 | Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX, |
| 4222 | and silently boost values less than -PY_SSIZE_T_MAX-1 to -PY_SSIZE_T_MAX-1. |
Martin v. Löwis | dde99d2 | 2006-02-17 15:57:41 +0000 | [diff] [blame] | 4223 | Return 0 on error, 1 on success. |
Tim Peters | cb479e7 | 2001-12-16 19:11:44 +0000 | [diff] [blame] | 4224 | */ |
Tim Peters | b519638 | 2001-12-16 19:44:20 +0000 | [diff] [blame] | 4225 | /* Note: If v is NULL, return success without storing into *pi. This |
| 4226 | is because_PyEval_SliceIndex() is called by apply_slice(), which can be |
| 4227 | called by the SLICE opcode with v and/or w equal to NULL. |
Tim Peters | cb479e7 | 2001-12-16 19:11:44 +0000 | [diff] [blame] | 4228 | */ |
Guido van Rossum | 20c6add | 2000-05-08 14:06:50 +0000 | [diff] [blame] | 4229 | int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4230 | _PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4231 | { |
Tim Peters | b519638 | 2001-12-16 19:44:20 +0000 | [diff] [blame] | 4232 | if (v != NULL) { |
Martin v. Löwis | dde99d2 | 2006-02-17 15:57:41 +0000 | [diff] [blame] | 4233 | Py_ssize_t x; |
Andrew M. Kuchling | 2194b16 | 2000-02-23 22:18:48 +0000 | [diff] [blame] | 4234 | if (PyInt_Check(v)) { |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 4235 | /* XXX(nnorwitz): I think PyInt_AS_LONG is correct, |
| 4236 | however, it looks like it should be AsSsize_t. |
| 4237 | There should be a comment here explaining why. |
| 4238 | */ |
| 4239 | x = PyInt_AS_LONG(v); |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 4240 | } |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 4241 | else if (PyIndex_Check(v)) { |
| 4242 | x = PyNumber_AsSsize_t(v, NULL); |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 4243 | if (x == -1 && PyErr_Occurred()) |
| 4244 | return 0; |
| 4245 | } |
| 4246 | else { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 4247 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 4248 | "slice indices must be integers or " |
| 4249 | "None or have an __index__ method"); |
Guido van Rossum | 20c6add | 2000-05-08 14:06:50 +0000 | [diff] [blame] | 4250 | return 0; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4251 | } |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 4252 | *pi = x; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4253 | } |
Guido van Rossum | 20c6add | 2000-05-08 14:06:50 +0000 | [diff] [blame] | 4254 | return 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4255 | } |
| 4256 | |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 4257 | #undef ISINDEX |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 4258 | #define ISINDEX(x) ((x) == NULL || \ |
| 4259 | PyInt_Check(x) || PyLong_Check(x) || PyIndex_Check(x)) |
Guido van Rossum | 50d756e | 2001-08-18 17:43:36 +0000 | [diff] [blame] | 4260 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4261 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4262 | apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4263 | { |
Guido van Rossum | 50d756e | 2001-08-18 17:43:36 +0000 | [diff] [blame] | 4264 | PyTypeObject *tp = u->ob_type; |
| 4265 | PySequenceMethods *sq = tp->tp_as_sequence; |
| 4266 | |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 4267 | if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) { |
Martin v. Löwis | dde99d2 | 2006-02-17 15:57:41 +0000 | [diff] [blame] | 4268 | Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX; |
Guido van Rossum | 50d756e | 2001-08-18 17:43:36 +0000 | [diff] [blame] | 4269 | if (!_PyEval_SliceIndex(v, &ilow)) |
| 4270 | return NULL; |
| 4271 | if (!_PyEval_SliceIndex(w, &ihigh)) |
| 4272 | return NULL; |
| 4273 | return PySequence_GetSlice(u, ilow, ihigh); |
| 4274 | } |
| 4275 | else { |
| 4276 | PyObject *slice = PySlice_New(v, w, NULL); |
Guido van Rossum | 354797c | 2001-12-03 19:45:06 +0000 | [diff] [blame] | 4277 | if (slice != NULL) { |
| 4278 | PyObject *res = PyObject_GetItem(u, slice); |
| 4279 | Py_DECREF(slice); |
| 4280 | return res; |
| 4281 | } |
Guido van Rossum | 50d756e | 2001-08-18 17:43:36 +0000 | [diff] [blame] | 4282 | else |
| 4283 | return NULL; |
| 4284 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4285 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4286 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4287 | static int |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4288 | assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x) |
| 4289 | /* u[v:w] = x */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4290 | { |
Guido van Rossum | 50d756e | 2001-08-18 17:43:36 +0000 | [diff] [blame] | 4291 | PyTypeObject *tp = u->ob_type; |
| 4292 | PySequenceMethods *sq = tp->tp_as_sequence; |
| 4293 | |
Georg Brandl | 0fca97a | 2007-03-05 22:28:08 +0000 | [diff] [blame] | 4294 | if (sq && sq->sq_ass_slice && ISINDEX(v) && ISINDEX(w)) { |
Martin v. Löwis | dde99d2 | 2006-02-17 15:57:41 +0000 | [diff] [blame] | 4295 | Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX; |
Guido van Rossum | 50d756e | 2001-08-18 17:43:36 +0000 | [diff] [blame] | 4296 | if (!_PyEval_SliceIndex(v, &ilow)) |
| 4297 | return -1; |
| 4298 | if (!_PyEval_SliceIndex(w, &ihigh)) |
| 4299 | return -1; |
| 4300 | if (x == NULL) |
| 4301 | return PySequence_DelSlice(u, ilow, ihigh); |
| 4302 | else |
| 4303 | return PySequence_SetSlice(u, ilow, ihigh, x); |
| 4304 | } |
| 4305 | else { |
| 4306 | PyObject *slice = PySlice_New(v, w, NULL); |
| 4307 | if (slice != NULL) { |
Guido van Rossum | 354797c | 2001-12-03 19:45:06 +0000 | [diff] [blame] | 4308 | int res; |
Guido van Rossum | 50d756e | 2001-08-18 17:43:36 +0000 | [diff] [blame] | 4309 | if (x != NULL) |
Guido van Rossum | 354797c | 2001-12-03 19:45:06 +0000 | [diff] [blame] | 4310 | res = PyObject_SetItem(u, slice, x); |
Guido van Rossum | 50d756e | 2001-08-18 17:43:36 +0000 | [diff] [blame] | 4311 | else |
Guido van Rossum | 354797c | 2001-12-03 19:45:06 +0000 | [diff] [blame] | 4312 | res = PyObject_DelItem(u, slice); |
| 4313 | Py_DECREF(slice); |
| 4314 | return res; |
Guido van Rossum | 50d756e | 2001-08-18 17:43:36 +0000 | [diff] [blame] | 4315 | } |
| 4316 | else |
| 4317 | return -1; |
| 4318 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4319 | } |
| 4320 | |
Guido van Rossum | 04edb52 | 2008-03-18 02:49:46 +0000 | [diff] [blame] | 4321 | #define Py3kExceptionClass_Check(x) \ |
| 4322 | (PyType_Check((x)) && \ |
| 4323 | PyType_FastSubclass((PyTypeObject*)(x), Py_TPFLAGS_BASE_EXC_SUBCLASS)) |
| 4324 | |
| 4325 | #define CANNOT_CATCH_MSG "catching classes that don't inherit from " \ |
Georg Brandl | d5b635f | 2008-03-25 08:29:14 +0000 | [diff] [blame] | 4326 | "BaseException is not allowed in 3.x" |
Guido van Rossum | 04edb52 | 2008-03-18 02:49:46 +0000 | [diff] [blame] | 4327 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4328 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4329 | cmp_outcome(int op, register PyObject *v, register PyObject *w) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4330 | { |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4331 | int res = 0; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4332 | switch (op) { |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 4333 | case PyCmp_IS: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4334 | res = (v == w); |
Raymond Hettinger | 4bad9ba | 2003-01-19 05:08:13 +0000 | [diff] [blame] | 4335 | break; |
| 4336 | case PyCmp_IS_NOT: |
| 4337 | res = (v != w); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4338 | break; |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 4339 | case PyCmp_IN: |
Raymond Hettinger | 4bad9ba | 2003-01-19 05:08:13 +0000 | [diff] [blame] | 4340 | res = PySequence_Contains(w, v); |
| 4341 | if (res < 0) |
| 4342 | return NULL; |
| 4343 | break; |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 4344 | case PyCmp_NOT_IN: |
Guido van Rossum | 7e33c6e | 1998-05-22 00:52:29 +0000 | [diff] [blame] | 4345 | res = PySequence_Contains(w, v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4346 | if (res < 0) |
| 4347 | return NULL; |
Raymond Hettinger | 4bad9ba | 2003-01-19 05:08:13 +0000 | [diff] [blame] | 4348 | res = !res; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4349 | break; |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 4350 | case PyCmp_EXC_MATCH: |
Brett Cannon | 129bd52 | 2007-01-30 21:34:36 +0000 | [diff] [blame] | 4351 | if (PyTuple_Check(w)) { |
| 4352 | Py_ssize_t i, length; |
| 4353 | length = PyTuple_Size(w); |
| 4354 | for (i = 0; i < length; i += 1) { |
| 4355 | PyObject *exc = PyTuple_GET_ITEM(w, i); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4356 | if (PyString_Check(exc)) { |
Brett Cannon | 129bd52 | 2007-01-30 21:34:36 +0000 | [diff] [blame] | 4357 | int ret_val; |
| 4358 | ret_val = PyErr_WarnEx( |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 4359 | PyExc_DeprecationWarning, |
| 4360 | "catching of string " |
| 4361 | "exceptions is deprecated", 1); |
Benjamin Peterson | f19a7b9 | 2008-04-27 18:40:21 +0000 | [diff] [blame] | 4362 | if (ret_val < 0) |
Brett Cannon | 129bd52 | 2007-01-30 21:34:36 +0000 | [diff] [blame] | 4363 | return NULL; |
| 4364 | } |
Guido van Rossum | 20bda58 | 2008-03-18 03:15:05 +0000 | [diff] [blame] | 4365 | else if (Py_Py3kWarningFlag && |
| 4366 | !PyTuple_Check(exc) && |
| 4367 | !Py3kExceptionClass_Check(exc)) |
Guido van Rossum | 04edb52 | 2008-03-18 02:49:46 +0000 | [diff] [blame] | 4368 | { |
| 4369 | int ret_val; |
| 4370 | ret_val = PyErr_WarnEx( |
| 4371 | PyExc_DeprecationWarning, |
| 4372 | CANNOT_CATCH_MSG, 1); |
Benjamin Peterson | f19a7b9 | 2008-04-27 18:40:21 +0000 | [diff] [blame] | 4373 | if (ret_val < 0) |
Guido van Rossum | 04edb52 | 2008-03-18 02:49:46 +0000 | [diff] [blame] | 4374 | return NULL; |
| 4375 | } |
Brett Cannon | 129bd52 | 2007-01-30 21:34:36 +0000 | [diff] [blame] | 4376 | } |
| 4377 | } |
| 4378 | else { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4379 | if (PyString_Check(w)) { |
Brett Cannon | 129bd52 | 2007-01-30 21:34:36 +0000 | [diff] [blame] | 4380 | int ret_val; |
| 4381 | ret_val = PyErr_WarnEx( |
| 4382 | PyExc_DeprecationWarning, |
| 4383 | "catching of string " |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 4384 | "exceptions is deprecated", 1); |
Benjamin Peterson | f19a7b9 | 2008-04-27 18:40:21 +0000 | [diff] [blame] | 4385 | if (ret_val < 0) |
Brett Cannon | 129bd52 | 2007-01-30 21:34:36 +0000 | [diff] [blame] | 4386 | return NULL; |
| 4387 | } |
Guido van Rossum | 20bda58 | 2008-03-18 03:15:05 +0000 | [diff] [blame] | 4388 | else if (Py_Py3kWarningFlag && |
| 4389 | !PyTuple_Check(w) && |
| 4390 | !Py3kExceptionClass_Check(w)) |
Guido van Rossum | 04edb52 | 2008-03-18 02:49:46 +0000 | [diff] [blame] | 4391 | { |
| 4392 | int ret_val; |
| 4393 | ret_val = PyErr_WarnEx( |
| 4394 | PyExc_DeprecationWarning, |
| 4395 | CANNOT_CATCH_MSG, 1); |
Benjamin Peterson | f19a7b9 | 2008-04-27 18:40:21 +0000 | [diff] [blame] | 4396 | if (ret_val < 0) |
Guido van Rossum | 04edb52 | 2008-03-18 02:49:46 +0000 | [diff] [blame] | 4397 | return NULL; |
| 4398 | } |
Brett Cannon | 129bd52 | 2007-01-30 21:34:36 +0000 | [diff] [blame] | 4399 | } |
Barry Warsaw | 4249f54 | 1997-08-22 21:26:19 +0000 | [diff] [blame] | 4400 | res = PyErr_GivenExceptionMatches(v, w); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4401 | break; |
| 4402 | default: |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4403 | return PyObject_RichCompare(v, w, op); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4404 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4405 | v = res ? Py_True : Py_False; |
| 4406 | Py_INCREF(v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4407 | return v; |
| 4408 | } |
| 4409 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4410 | static PyObject * |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 4411 | import_from(PyObject *v, PyObject *name) |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 4412 | { |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 4413 | PyObject *x; |
| 4414 | |
| 4415 | x = PyObject_GetAttr(v, name); |
| 4416 | if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) { |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 4417 | PyErr_Format(PyExc_ImportError, |
| 4418 | "cannot import name %.230s", |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4419 | PyString_AsString(name)); |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 4420 | } |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 4421 | return x; |
| 4422 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4423 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4424 | static int |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 4425 | import_all_from(PyObject *locals, PyObject *v) |
| 4426 | { |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 4427 | PyObject *all = PyObject_GetAttrString(v, "__all__"); |
| 4428 | PyObject *dict, *name, *value; |
| 4429 | int skip_leading_underscores = 0; |
| 4430 | int pos, err; |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 4431 | |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 4432 | if (all == NULL) { |
| 4433 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 4434 | return -1; /* Unexpected error */ |
| 4435 | PyErr_Clear(); |
| 4436 | dict = PyObject_GetAttrString(v, "__dict__"); |
| 4437 | if (dict == NULL) { |
| 4438 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 4439 | return -1; |
| 4440 | PyErr_SetString(PyExc_ImportError, |
| 4441 | "from-import-* object has no __dict__ and no __all__"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4442 | return -1; |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 4443 | } |
| 4444 | all = PyMapping_Keys(dict); |
| 4445 | Py_DECREF(dict); |
| 4446 | if (all == NULL) |
| 4447 | return -1; |
| 4448 | skip_leading_underscores = 1; |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 4449 | } |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 4450 | |
| 4451 | for (pos = 0, err = 0; ; pos++) { |
| 4452 | name = PySequence_GetItem(all, pos); |
| 4453 | if (name == NULL) { |
| 4454 | if (!PyErr_ExceptionMatches(PyExc_IndexError)) |
| 4455 | err = -1; |
| 4456 | else |
| 4457 | PyErr_Clear(); |
| 4458 | break; |
| 4459 | } |
| 4460 | if (skip_leading_underscores && |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4461 | PyString_Check(name) && |
| 4462 | PyString_AS_STRING(name)[0] == '_') |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 4463 | { |
| 4464 | Py_DECREF(name); |
| 4465 | continue; |
| 4466 | } |
| 4467 | value = PyObject_GetAttr(v, name); |
| 4468 | if (value == NULL) |
| 4469 | err = -1; |
Armin Rigo | 7037085 | 2006-11-29 21:59:22 +0000 | [diff] [blame] | 4470 | else if (PyDict_CheckExact(locals)) |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 4471 | err = PyDict_SetItem(locals, name, value); |
Armin Rigo | 7037085 | 2006-11-29 21:59:22 +0000 | [diff] [blame] | 4472 | else |
| 4473 | err = PyObject_SetItem(locals, name, value); |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 4474 | Py_DECREF(name); |
| 4475 | Py_XDECREF(value); |
| 4476 | if (err != 0) |
| 4477 | break; |
| 4478 | } |
| 4479 | Py_DECREF(all); |
| 4480 | return err; |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 4481 | } |
| 4482 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4483 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4484 | build_class(PyObject *methods, PyObject *bases, PyObject *name) |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 4485 | { |
Guido van Rossum | 7851eea | 2001-09-12 19:19:18 +0000 | [diff] [blame] | 4486 | PyObject *metaclass = NULL, *result, *base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4487 | |
| 4488 | if (PyDict_Check(methods)) |
| 4489 | metaclass = PyDict_GetItemString(methods, "__metaclass__"); |
Guido van Rossum | 7851eea | 2001-09-12 19:19:18 +0000 | [diff] [blame] | 4490 | if (metaclass != NULL) |
Guido van Rossum | 2556f2e | 2001-12-06 14:09:56 +0000 | [diff] [blame] | 4491 | Py_INCREF(metaclass); |
Guido van Rossum | 7851eea | 2001-09-12 19:19:18 +0000 | [diff] [blame] | 4492 | else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) { |
| 4493 | base = PyTuple_GET_ITEM(bases, 0); |
| 4494 | metaclass = PyObject_GetAttrString(base, "__class__"); |
| 4495 | if (metaclass == NULL) { |
| 4496 | PyErr_Clear(); |
| 4497 | metaclass = (PyObject *)base->ob_type; |
| 4498 | Py_INCREF(metaclass); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 4499 | } |
| 4500 | } |
Guido van Rossum | 7851eea | 2001-09-12 19:19:18 +0000 | [diff] [blame] | 4501 | else { |
| 4502 | PyObject *g = PyEval_GetGlobals(); |
| 4503 | if (g != NULL && PyDict_Check(g)) |
| 4504 | metaclass = PyDict_GetItemString(g, "__metaclass__"); |
| 4505 | if (metaclass == NULL) |
| 4506 | metaclass = (PyObject *) &PyClass_Type; |
| 4507 | Py_INCREF(metaclass); |
| 4508 | } |
Jeremy Hylton | 7c1e347 | 2007-02-26 16:14:51 +0000 | [diff] [blame] | 4509 | result = PyObject_CallFunctionObjArgs(metaclass, name, bases, methods, |
Thomas Wouters | ae406c6 | 2007-09-19 17:27:43 +0000 | [diff] [blame] | 4510 | NULL); |
Guido van Rossum | 7851eea | 2001-09-12 19:19:18 +0000 | [diff] [blame] | 4511 | Py_DECREF(metaclass); |
Raymond Hettinger | f2c0830 | 2004-06-05 06:16:22 +0000 | [diff] [blame] | 4512 | if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) { |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 4513 | /* A type error here likely means that the user passed |
Raymond Hettinger | f2c0830 | 2004-06-05 06:16:22 +0000 | [diff] [blame] | 4514 | in a base that was not a class (such the random module |
| 4515 | instead of the random.random type). Help them out with |
Raymond Hettinger | cfc3192 | 2004-09-16 16:41:57 +0000 | [diff] [blame] | 4516 | by augmenting the error message with more information.*/ |
| 4517 | |
| 4518 | PyObject *ptype, *pvalue, *ptraceback; |
| 4519 | |
| 4520 | PyErr_Fetch(&ptype, &pvalue, &ptraceback); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4521 | if (PyString_Check(pvalue)) { |
Raymond Hettinger | cfc3192 | 2004-09-16 16:41:57 +0000 | [diff] [blame] | 4522 | PyObject *newmsg; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4523 | newmsg = PyString_FromFormat( |
Jeremy Hylton | 7c1e347 | 2007-02-26 16:14:51 +0000 | [diff] [blame] | 4524 | "Error when calling the metaclass bases\n" |
Thomas Wouters | ae406c6 | 2007-09-19 17:27:43 +0000 | [diff] [blame] | 4525 | " %s", |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4526 | PyString_AS_STRING(pvalue)); |
Raymond Hettinger | cfc3192 | 2004-09-16 16:41:57 +0000 | [diff] [blame] | 4527 | if (newmsg != NULL) { |
| 4528 | Py_DECREF(pvalue); |
| 4529 | pvalue = newmsg; |
| 4530 | } |
| 4531 | } |
| 4532 | PyErr_Restore(ptype, pvalue, ptraceback); |
Raymond Hettinger | f2c0830 | 2004-06-05 06:16:22 +0000 | [diff] [blame] | 4533 | } |
Guido van Rossum | 7851eea | 2001-09-12 19:19:18 +0000 | [diff] [blame] | 4534 | return result; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 4535 | } |
| 4536 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4537 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4538 | exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals, |
| 4539 | PyObject *locals) |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 4540 | { |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 4541 | int n; |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4542 | PyObject *v; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4543 | int plain = 0; |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 4544 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4545 | if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None && |
| 4546 | ((n = PyTuple_Size(prog)) == 2 || n == 3)) { |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 4547 | /* Backward compatibility hack */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4548 | globals = PyTuple_GetItem(prog, 1); |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 4549 | if (n == 3) |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4550 | locals = PyTuple_GetItem(prog, 2); |
| 4551 | prog = PyTuple_GetItem(prog, 0); |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 4552 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4553 | if (globals == Py_None) { |
| 4554 | globals = PyEval_GetGlobals(); |
| 4555 | if (locals == Py_None) { |
| 4556 | locals = PyEval_GetLocals(); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4557 | plain = 1; |
| 4558 | } |
Neal Norwitz | df6a649 | 2006-08-13 18:10:10 +0000 | [diff] [blame] | 4559 | if (!globals || !locals) { |
| 4560 | PyErr_SetString(PyExc_SystemError, |
| 4561 | "globals and locals cannot be NULL"); |
| 4562 | return -1; |
| 4563 | } |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 4564 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4565 | else if (locals == Py_None) |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 4566 | locals = globals; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4567 | if (!PyString_Check(prog) && |
Benjamin Peterson | 78821dd | 2009-01-25 17:15:10 +0000 | [diff] [blame] | 4568 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 4569 | !PyUnicode_Check(prog) && |
Benjamin Peterson | 78821dd | 2009-01-25 17:15:10 +0000 | [diff] [blame] | 4570 | #endif |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4571 | !PyCode_Check(prog) && |
| 4572 | !PyFile_Check(prog)) { |
| 4573 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4574 | "exec: arg 1 must be a string, file, or code object"); |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 4575 | return -1; |
| 4576 | } |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4577 | if (!PyDict_Check(globals)) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4578 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4579 | "exec: arg 2 must be a dictionary or None"); |
| 4580 | return -1; |
| 4581 | } |
Raymond Hettinger | 66bd233 | 2004-08-02 08:30:07 +0000 | [diff] [blame] | 4582 | if (!PyMapping_Check(locals)) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4583 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 66bd233 | 2004-08-02 08:30:07 +0000 | [diff] [blame] | 4584 | "exec: arg 3 must be a mapping or None"); |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 4585 | return -1; |
| 4586 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4587 | if (PyDict_GetItemString(globals, "__builtins__") == NULL) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 4588 | PyDict_SetItemString(globals, "__builtins__", f->f_builtins); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4589 | if (PyCode_Check(prog)) { |
Jeremy Hylton | 733c893 | 2001-12-13 19:51:56 +0000 | [diff] [blame] | 4590 | if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) { |
| 4591 | PyErr_SetString(PyExc_TypeError, |
| 4592 | "code object passed to exec may not contain free variables"); |
| 4593 | return -1; |
| 4594 | } |
Guido van Rossum | a400d8a | 2000-01-12 22:45:54 +0000 | [diff] [blame] | 4595 | v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals); |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 4596 | } |
Guido van Rossum | a400d8a | 2000-01-12 22:45:54 +0000 | [diff] [blame] | 4597 | else if (PyFile_Check(prog)) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4598 | FILE *fp = PyFile_AsFile(prog); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4599 | char *name = PyString_AsString(PyFile_Name(prog)); |
Jeremy Hylton | 714b112 | 2007-02-25 16:01:58 +0000 | [diff] [blame] | 4600 | PyCompilerFlags cf; |
Thomas Wouters | ae406c6 | 2007-09-19 17:27:43 +0000 | [diff] [blame] | 4601 | if (name == NULL) |
| 4602 | return -1; |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 4603 | cf.cf_flags = 0; |
| 4604 | if (PyEval_MergeCompilerFlags(&cf)) |
Jeremy Hylton | bc32024 | 2001-03-22 02:47:58 +0000 | [diff] [blame] | 4605 | v = PyRun_FileFlags(fp, name, Py_file_input, globals, |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 4606 | locals, &cf); |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 4607 | else |
Jeremy Hylton | bc32024 | 2001-03-22 02:47:58 +0000 | [diff] [blame] | 4608 | v = PyRun_File(fp, name, Py_file_input, globals, |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 4609 | locals); |
Guido van Rossum | a400d8a | 2000-01-12 22:45:54 +0000 | [diff] [blame] | 4610 | } |
| 4611 | else { |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 4612 | PyObject *tmp = NULL; |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 4613 | char *str; |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 4614 | PyCompilerFlags cf; |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 4615 | cf.cf_flags = 0; |
| 4616 | #ifdef Py_USING_UNICODE |
| 4617 | if (PyUnicode_Check(prog)) { |
| 4618 | tmp = PyUnicode_AsUTF8String(prog); |
| 4619 | if (tmp == NULL) |
| 4620 | return -1; |
| 4621 | prog = tmp; |
| 4622 | cf.cf_flags |= PyCF_SOURCE_IS_UTF8; |
| 4623 | } |
| 4624 | #endif |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4625 | if (PyString_AsStringAndSize(prog, &str, NULL)) |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 4626 | return -1; |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 4627 | if (PyEval_MergeCompilerFlags(&cf)) |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 4628 | v = PyRun_StringFlags(str, Py_file_input, globals, |
Jeremy Hylton | bc32024 | 2001-03-22 02:47:58 +0000 | [diff] [blame] | 4629 | locals, &cf); |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 4630 | else |
Jeremy Hylton | bc32024 | 2001-03-22 02:47:58 +0000 | [diff] [blame] | 4631 | v = PyRun_String(str, Py_file_input, globals, locals); |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 4632 | Py_XDECREF(tmp); |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 4633 | } |
Guido van Rossum | a400d8a | 2000-01-12 22:45:54 +0000 | [diff] [blame] | 4634 | if (plain) |
| 4635 | PyFrame_LocalsToFast(f, 0); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4636 | if (v == NULL) |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 4637 | return -1; |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4638 | Py_DECREF(v); |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 4639 | return 0; |
| 4640 | } |
Guido van Rossum | 24c1374 | 1995-02-14 09:42:43 +0000 | [diff] [blame] | 4641 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4642 | static void |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 4643 | format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj) |
| 4644 | { |
| 4645 | char *obj_str; |
| 4646 | |
| 4647 | if (!obj) |
| 4648 | return; |
| 4649 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4650 | obj_str = PyString_AsString(obj); |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 4651 | if (!obj_str) |
| 4652 | return; |
| 4653 | |
| 4654 | PyErr_Format(exc, format_str, obj_str); |
| 4655 | } |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 4656 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4657 | static PyObject * |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 4658 | string_concatenate(PyObject *v, PyObject *w, |
| 4659 | PyFrameObject *f, unsigned char *next_instr) |
| 4660 | { |
| 4661 | /* This function implements 'variable += expr' when both arguments |
| 4662 | are strings. */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4663 | Py_ssize_t v_len = PyString_GET_SIZE(v); |
| 4664 | Py_ssize_t w_len = PyString_GET_SIZE(w); |
Armin Rigo | 97ff047 | 2006-08-09 15:37:26 +0000 | [diff] [blame] | 4665 | Py_ssize_t new_len = v_len + w_len; |
| 4666 | if (new_len < 0) { |
| 4667 | PyErr_SetString(PyExc_OverflowError, |
| 4668 | "strings are too large to concat"); |
| 4669 | return NULL; |
| 4670 | } |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 4671 | |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 4672 | if (v->ob_refcnt == 2) { |
| 4673 | /* In the common case, there are 2 references to the value |
| 4674 | * stored in 'variable' when the += is performed: one on the |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 4675 | * value stack (in 'v') and one still stored in the |
| 4676 | * 'variable'. We try to delete the variable now to reduce |
| 4677 | * the refcnt to 1. |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 4678 | */ |
| 4679 | switch (*next_instr) { |
| 4680 | case STORE_FAST: |
| 4681 | { |
| 4682 | int oparg = PEEKARG(); |
| 4683 | PyObject **fastlocals = f->f_localsplus; |
| 4684 | if (GETLOCAL(oparg) == v) |
| 4685 | SETLOCAL(oparg, NULL); |
| 4686 | break; |
| 4687 | } |
| 4688 | case STORE_DEREF: |
| 4689 | { |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 4690 | PyObject **freevars = (f->f_localsplus + |
| 4691 | f->f_code->co_nlocals); |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 4692 | PyObject *c = freevars[PEEKARG()]; |
| 4693 | if (PyCell_GET(c) == v) |
| 4694 | PyCell_Set(c, NULL); |
| 4695 | break; |
| 4696 | } |
| 4697 | case STORE_NAME: |
| 4698 | { |
| 4699 | PyObject *names = f->f_code->co_names; |
| 4700 | PyObject *name = GETITEM(names, PEEKARG()); |
| 4701 | PyObject *locals = f->f_locals; |
| 4702 | if (PyDict_CheckExact(locals) && |
| 4703 | PyDict_GetItem(locals, name) == v) { |
| 4704 | if (PyDict_DelItem(locals, name) != 0) { |
| 4705 | PyErr_Clear(); |
| 4706 | } |
| 4707 | } |
| 4708 | break; |
| 4709 | } |
| 4710 | } |
| 4711 | } |
| 4712 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4713 | if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) { |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 4714 | /* Now we own the last reference to 'v', so we can resize it |
| 4715 | * in-place. |
| 4716 | */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4717 | if (_PyString_Resize(&v, new_len) != 0) { |
| 4718 | /* XXX if _PyString_Resize() fails, 'v' has been |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 4719 | * deallocated so it cannot be put back into |
| 4720 | * 'variable'. The MemoryError is raised when there |
| 4721 | * is no value in 'variable', which might (very |
| 4722 | * remotely) be a cause of incompatibilities. |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 4723 | */ |
| 4724 | return NULL; |
| 4725 | } |
| 4726 | /* copy 'w' into the newly allocated area of 'v' */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4727 | memcpy(PyString_AS_STRING(v) + v_len, |
| 4728 | PyString_AS_STRING(w), w_len); |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 4729 | return v; |
| 4730 | } |
| 4731 | else { |
| 4732 | /* When in-place resizing is not an option. */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4733 | PyString_Concat(&v, w); |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 4734 | return v; |
| 4735 | } |
| 4736 | } |
| 4737 | |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 4738 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 4739 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4740 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4741 | getarray(long a[256]) |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 4742 | { |
| 4743 | int i; |
| 4744 | PyObject *l = PyList_New(256); |
| 4745 | if (l == NULL) return NULL; |
| 4746 | for (i = 0; i < 256; i++) { |
| 4747 | PyObject *x = PyInt_FromLong(a[i]); |
| 4748 | if (x == NULL) { |
| 4749 | Py_DECREF(l); |
| 4750 | return NULL; |
| 4751 | } |
| 4752 | PyList_SetItem(l, i, x); |
| 4753 | } |
| 4754 | for (i = 0; i < 256; i++) |
| 4755 | a[i] = 0; |
| 4756 | return l; |
| 4757 | } |
| 4758 | |
| 4759 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4760 | _Py_GetDXProfile(PyObject *self, PyObject *args) |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 4761 | { |
| 4762 | #ifndef DXPAIRS |
| 4763 | return getarray(dxp); |
| 4764 | #else |
| 4765 | int i; |
| 4766 | PyObject *l = PyList_New(257); |
| 4767 | if (l == NULL) return NULL; |
| 4768 | for (i = 0; i < 257; i++) { |
| 4769 | PyObject *x = getarray(dxpairs[i]); |
| 4770 | if (x == NULL) { |
| 4771 | Py_DECREF(l); |
| 4772 | return NULL; |
| 4773 | } |
| 4774 | PyList_SetItem(l, i, x); |
| 4775 | } |
| 4776 | return l; |
| 4777 | #endif |
| 4778 | } |
| 4779 | |
| 4780 | #endif |