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 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9 | /* enable more aggressive intra-module optimizations, where available */ |
| 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 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +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 |
| 31 | section should work for GCC on any PowerPC platform, |
| 32 | irrespective of OS. POWER? Who knows :-) */ |
| 33 | |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 34 | #define READ_TIMESTAMP(var) ppc_getcounter(&var) |
Michael W. Hudson | 800ba23 | 2004-08-12 18:19:17 +0000 | [diff] [blame] | 35 | |
| 36 | static void |
| 37 | ppc_getcounter(uint64 *v) |
| 38 | { |
| 39 | register unsigned long tbu, tb, tbu2; |
| 40 | |
| 41 | loop: |
| 42 | asm volatile ("mftbu %0" : "=r" (tbu) ); |
| 43 | asm volatile ("mftb %0" : "=r" (tb) ); |
| 44 | asm volatile ("mftbu %0" : "=r" (tbu2)); |
| 45 | if (__builtin_expect(tbu != tbu2, 0)) goto loop; |
| 46 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 47 | /* The slightly peculiar way of writing the next lines is |
Michael W. Hudson | 800ba23 | 2004-08-12 18:19:17 +0000 | [diff] [blame] | 48 | compiled better by GCC than any other way I tried. */ |
| 49 | ((long*)(v))[0] = tbu; |
| 50 | ((long*)(v))[1] = tb; |
| 51 | } |
| 52 | |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 53 | #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] | 54 | |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 55 | #define READ_TIMESTAMP(val) \ |
| 56 | __asm__ __volatile__("rdtsc" : "=A" (val)) |
Michael W. Hudson | 800ba23 | 2004-08-12 18:19:17 +0000 | [diff] [blame] | 57 | |
| 58 | #endif |
| 59 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 60 | 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] | 61 | uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1) |
| 62 | { |
| 63 | uint64 intr, inst, loop; |
| 64 | PyThreadState *tstate = PyThreadState_Get(); |
| 65 | if (!tstate->interp->tscdump) |
| 66 | return; |
| 67 | intr = intr1 - intr0; |
| 68 | inst = inst1 - inst0 - intr; |
| 69 | loop = loop1 - loop0 - intr; |
| 70 | fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n", |
| 71 | opcode, ticked, inst, loop); |
| 72 | } |
Michael W. Hudson | 800ba23 | 2004-08-12 18:19:17 +0000 | [diff] [blame] | 73 | |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 74 | #endif |
| 75 | |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 76 | /* Turn this on if your compiler chokes on the big switch: */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 77 | /* #define CASE_TOO_BIG 1 */ |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 78 | |
Guido van Rossum | 408027e | 1996-12-30 16:17:54 +0000 | [diff] [blame] | 79 | #ifdef Py_DEBUG |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 80 | /* For debugging the interpreter: */ |
| 81 | #define LLTRACE 1 /* Low-level trace feature */ |
| 82 | #define CHECKEXC 1 /* Double-check exception checking */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 83 | #endif |
| 84 | |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 85 | typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *); |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 86 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 87 | /* Forward declarations */ |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 88 | #ifdef WITH_TSC |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 89 | static PyObject * call_function(PyObject ***, int, uint64*, uint64*); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 90 | #else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 91 | static PyObject * call_function(PyObject ***, int); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 92 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 93 | static PyObject * fast_function(PyObject *, PyObject ***, int, int, int); |
| 94 | static PyObject * do_call(PyObject *, PyObject ***, int, int); |
| 95 | static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int); |
| 96 | static PyObject * update_keyword_args(PyObject *, int, PyObject ***,PyObject *); |
| 97 | static PyObject * update_star_args(int, int, PyObject *, PyObject ***); |
| 98 | static PyObject * load_args(PyObject ***, int); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 99 | #define CALL_FLAG_VAR 1 |
| 100 | #define CALL_FLAG_KW 2 |
| 101 | |
Guido van Rossum | 0a066c0 | 1992-03-27 17:29:15 +0000 | [diff] [blame] | 102 | #ifdef LLTRACE |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 103 | static int lltrace; |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 104 | static int prtrace(PyObject *, char *); |
Guido van Rossum | 0a066c0 | 1992-03-27 17:29:15 +0000 | [diff] [blame] | 105 | #endif |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 106 | static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *, |
| 107 | int, PyObject *); |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 108 | static void call_trace_protected(Py_tracefunc, PyObject *, |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 109 | PyFrameObject *, int, PyObject *); |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 110 | static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *); |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 111 | static int maybe_call_line_trace(Py_tracefunc, PyObject *, |
Armin Rigo | bf57a14 | 2004-03-22 19:24:58 +0000 | [diff] [blame] | 112 | PyFrameObject *, int *, int *, int *); |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 113 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 114 | static PyObject * apply_slice(PyObject *, PyObject *, PyObject *); |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 115 | static int assign_slice(PyObject *, PyObject *, |
| 116 | PyObject *, PyObject *); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 117 | static PyObject * cmp_outcome(int, PyObject *, PyObject *); |
| 118 | static PyObject * import_from(PyObject *, PyObject *); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 119 | static int import_all_from(PyObject *, PyObject *); |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 120 | static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *); |
| 121 | static void reset_exc_info(PyThreadState *); |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 122 | static void format_exc_check_arg(PyObject *, char *, PyObject *); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 123 | static PyObject * string_concatenate(PyObject *, PyObject *, |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 124 | PyFrameObject *, unsigned char *); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 125 | |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 126 | #define NAME_ERROR_MSG \ |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 127 | "name '%.200s' is not defined" |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 128 | #define GLOBAL_NAME_ERROR_MSG \ |
| 129 | "global name '%.200s' is not defined" |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 130 | #define UNBOUNDLOCAL_ERROR_MSG \ |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 131 | "local variable '%.200s' referenced before assignment" |
Jeremy Hylton | c76770c | 2001-04-13 16:51:46 +0000 | [diff] [blame] | 132 | #define UNBOUNDFREE_ERROR_MSG \ |
| 133 | "free variable '%.200s' referenced before assignment" \ |
| 134 | " in enclosing scope" |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 135 | |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 136 | /* Dynamic execution profile */ |
| 137 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 138 | #ifdef DXPAIRS |
| 139 | static long dxpairs[257][256]; |
| 140 | #define dxp dxpairs[256] |
| 141 | #else |
| 142 | static long dxp[256]; |
| 143 | #endif |
| 144 | #endif |
| 145 | |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 146 | /* Function call profile */ |
| 147 | #ifdef CALL_PROFILE |
| 148 | #define PCALL_NUM 11 |
| 149 | static int pcall[PCALL_NUM]; |
| 150 | |
| 151 | #define PCALL_ALL 0 |
| 152 | #define PCALL_FUNCTION 1 |
| 153 | #define PCALL_FAST_FUNCTION 2 |
| 154 | #define PCALL_FASTER_FUNCTION 3 |
| 155 | #define PCALL_METHOD 4 |
| 156 | #define PCALL_BOUND_METHOD 5 |
| 157 | #define PCALL_CFUNCTION 6 |
| 158 | #define PCALL_TYPE 7 |
| 159 | #define PCALL_GENERATOR 8 |
| 160 | #define PCALL_OTHER 9 |
| 161 | #define PCALL_POP 10 |
| 162 | |
| 163 | /* Notes about the statistics |
| 164 | |
| 165 | PCALL_FAST stats |
| 166 | |
| 167 | FAST_FUNCTION means no argument tuple needs to be created. |
| 168 | FASTER_FUNCTION means that the fast-path frame setup code is used. |
| 169 | |
| 170 | If there is a method call where the call can be optimized by changing |
| 171 | the argument tuple and calling the function directly, it gets recorded |
| 172 | twice. |
| 173 | |
| 174 | As a result, the relationship among the statistics appears to be |
| 175 | PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD + |
| 176 | PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER |
| 177 | PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION |
| 178 | PCALL_METHOD > PCALL_BOUND_METHOD |
| 179 | */ |
| 180 | |
| 181 | #define PCALL(POS) pcall[POS]++ |
| 182 | |
| 183 | PyObject * |
| 184 | PyEval_GetCallStats(PyObject *self) |
| 185 | { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 186 | return Py_BuildValue("iiiiiiiiiii", |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 187 | pcall[0], pcall[1], pcall[2], pcall[3], |
| 188 | pcall[4], pcall[5], pcall[6], pcall[7], |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 189 | pcall[8], pcall[9], pcall[10]); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 190 | } |
| 191 | #else |
| 192 | #define PCALL(O) |
| 193 | |
| 194 | PyObject * |
| 195 | PyEval_GetCallStats(PyObject *self) |
| 196 | { |
| 197 | Py_INCREF(Py_None); |
| 198 | return Py_None; |
| 199 | } |
| 200 | #endif |
| 201 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 202 | |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 203 | #ifdef WITH_THREAD |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 204 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 205 | #ifdef HAVE_ERRNO_H |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 206 | #include <errno.h> |
Guido van Rossum | 2571cc8 | 1999-04-07 16:07:23 +0000 | [diff] [blame] | 207 | #endif |
Guido van Rossum | 49b5606 | 1998-10-01 20:42:43 +0000 | [diff] [blame] | 208 | #include "pythread.h" |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 209 | |
Guido van Rossum | b8b6d0c | 2003-06-28 21:53:52 +0000 | [diff] [blame] | 210 | static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */ |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 211 | static long main_thread = 0; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 212 | |
Tim Peters | 7f468f2 | 2004-10-11 02:40:51 +0000 | [diff] [blame] | 213 | int |
| 214 | PyEval_ThreadsInitialized(void) |
| 215 | { |
| 216 | return interpreter_lock != 0; |
| 217 | } |
| 218 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 219 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 220 | PyEval_InitThreads(void) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 221 | { |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 222 | if (interpreter_lock) |
Sjoerd Mullender | ed59d20 | 1993-01-06 13:36:38 +0000 | [diff] [blame] | 223 | return; |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 224 | interpreter_lock = PyThread_allocate_lock(); |
| 225 | PyThread_acquire_lock(interpreter_lock, 1); |
| 226 | main_thread = PyThread_get_thread_ident(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 227 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 228 | |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 229 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 230 | PyEval_AcquireLock(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 231 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 232 | PyThread_acquire_lock(interpreter_lock, 1); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 236 | PyEval_ReleaseLock(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 237 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 238 | PyThread_release_lock(interpreter_lock); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 242 | PyEval_AcquireThread(PyThreadState *tstate) |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 243 | { |
| 244 | if (tstate == NULL) |
| 245 | Py_FatalError("PyEval_AcquireThread: NULL new thread state"); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 246 | /* Check someone has called PyEval_InitThreads() to create the lock */ |
| 247 | assert(interpreter_lock); |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 248 | PyThread_acquire_lock(interpreter_lock, 1); |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 249 | if (PyThreadState_Swap(tstate) != NULL) |
| 250 | Py_FatalError( |
| 251 | "PyEval_AcquireThread: non-NULL old thread state"); |
| 252 | } |
| 253 | |
| 254 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 255 | PyEval_ReleaseThread(PyThreadState *tstate) |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 256 | { |
| 257 | if (tstate == NULL) |
| 258 | Py_FatalError("PyEval_ReleaseThread: NULL thread state"); |
| 259 | if (PyThreadState_Swap(NULL) != tstate) |
| 260 | Py_FatalError("PyEval_ReleaseThread: wrong thread state"); |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 261 | PyThread_release_lock(interpreter_lock); |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 262 | } |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 263 | |
| 264 | /* This function is called from PyOS_AfterFork to ensure that newly |
| 265 | created child processes don't hold locks referring to threads which |
| 266 | are not running in the child process. (This could also be done using |
| 267 | pthread_atfork mechanism, at least for the pthreads implementation.) */ |
| 268 | |
| 269 | void |
| 270 | PyEval_ReInitThreads(void) |
| 271 | { |
| 272 | if (!interpreter_lock) |
| 273 | return; |
| 274 | /*XXX Can't use PyThread_free_lock here because it does too |
| 275 | much error-checking. Doing this cleanly would require |
| 276 | adding a new function to each thread_*.h. Instead, just |
| 277 | create a new lock and waste a little bit of memory */ |
| 278 | interpreter_lock = PyThread_allocate_lock(); |
| 279 | PyThread_acquire_lock(interpreter_lock, 1); |
| 280 | main_thread = PyThread_get_thread_ident(); |
| 281 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 282 | #endif |
| 283 | |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 284 | /* Functions save_thread and restore_thread are always defined so |
| 285 | dynamically loaded modules needn't be compiled separately for use |
| 286 | with and without threads: */ |
| 287 | |
Guido van Rossum | 2fca21f7 | 1997-07-18 23:56:58 +0000 | [diff] [blame] | 288 | PyThreadState * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 289 | PyEval_SaveThread(void) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 290 | { |
Guido van Rossum | b74eca9 | 1997-09-30 22:03:16 +0000 | [diff] [blame] | 291 | PyThreadState *tstate = PyThreadState_Swap(NULL); |
| 292 | if (tstate == NULL) |
| 293 | Py_FatalError("PyEval_SaveThread: NULL tstate"); |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 294 | #ifdef WITH_THREAD |
Guido van Rossum | b74eca9 | 1997-09-30 22:03:16 +0000 | [diff] [blame] | 295 | if (interpreter_lock) |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 296 | PyThread_release_lock(interpreter_lock); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 297 | #endif |
Guido van Rossum | b74eca9 | 1997-09-30 22:03:16 +0000 | [diff] [blame] | 298 | return tstate; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 302 | PyEval_RestoreThread(PyThreadState *tstate) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 303 | { |
Guido van Rossum | b74eca9 | 1997-09-30 22:03:16 +0000 | [diff] [blame] | 304 | if (tstate == NULL) |
| 305 | Py_FatalError("PyEval_RestoreThread: NULL tstate"); |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 306 | #ifdef WITH_THREAD |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 307 | if (interpreter_lock) { |
Guido van Rossum | b74eca9 | 1997-09-30 22:03:16 +0000 | [diff] [blame] | 308 | int err = errno; |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 309 | PyThread_acquire_lock(interpreter_lock, 1); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 310 | errno = err; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 311 | } |
| 312 | #endif |
Guido van Rossum | b74eca9 | 1997-09-30 22:03:16 +0000 | [diff] [blame] | 313 | PyThreadState_Swap(tstate); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 317 | /* Mechanism whereby asynchronously executing callbacks (e.g. UNIX |
| 318 | signal handlers or Mac I/O completion routines) can schedule calls |
| 319 | to a function to be called synchronously. |
| 320 | The synchronous function is called with one void* argument. |
| 321 | It should return 0 for success or -1 for failure -- failure should |
| 322 | be accompanied by an exception. |
| 323 | |
| 324 | If registry succeeds, the registry function returns 0; if it fails |
| 325 | (e.g. due to too many pending calls) it returns -1 (without setting |
| 326 | an exception condition). |
| 327 | |
| 328 | Note that because registry may occur from within signal handlers, |
| 329 | or other asynchronous events, calling malloc() is unsafe! |
| 330 | |
| 331 | #ifdef WITH_THREAD |
| 332 | Any thread can schedule pending calls, but only the main thread |
| 333 | will execute them. |
| 334 | #endif |
| 335 | |
| 336 | XXX WARNING! ASYNCHRONOUSLY EXECUTING CODE! |
| 337 | There are two possible race conditions: |
| 338 | (1) nested asynchronous registry calls; |
| 339 | (2) registry calls made while pending calls are being processed. |
| 340 | While (1) is very unlikely, (2) is a real possibility. |
| 341 | The current code is safe against (2), but not against (1). |
| 342 | The safety against (2) is derived from the fact that only one |
| 343 | thread (the main thread) ever takes things out of the queue. |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 344 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 345 | XXX Darn! With the advent of thread state, we should have an array |
| 346 | of pending calls per thread in the thread state! Later... |
| 347 | */ |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 348 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 349 | #define NPENDINGCALLS 32 |
| 350 | static struct { |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 351 | int (*func)(void *); |
| 352 | void *arg; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 353 | } pendingcalls[NPENDINGCALLS]; |
| 354 | static volatile int pendingfirst = 0; |
| 355 | static volatile int pendinglast = 0; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 356 | static volatile int things_to_do = 0; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 357 | |
| 358 | int |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 359 | Py_AddPendingCall(int (*func)(void *), void *arg) |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 360 | { |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 361 | static volatile int busy = 0; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 362 | int i, j; |
| 363 | /* XXX Begin critical section */ |
| 364 | /* XXX If you want this to be safe against nested |
| 365 | XXX asynchronous calls, you'll have to work harder! */ |
Guido van Rossum | 180d7b4 | 1994-09-29 09:45:57 +0000 | [diff] [blame] | 366 | if (busy) |
| 367 | return -1; |
| 368 | busy = 1; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 369 | i = pendinglast; |
| 370 | j = (i + 1) % NPENDINGCALLS; |
Guido van Rossum | 04e7032 | 2002-07-17 16:57:13 +0000 | [diff] [blame] | 371 | if (j == pendingfirst) { |
| 372 | busy = 0; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 373 | return -1; /* Queue full */ |
Guido van Rossum | 04e7032 | 2002-07-17 16:57:13 +0000 | [diff] [blame] | 374 | } |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 375 | pendingcalls[i].func = func; |
| 376 | pendingcalls[i].arg = arg; |
| 377 | pendinglast = j; |
Skip Montanaro | d581d77 | 2002-09-03 20:10:45 +0000 | [diff] [blame] | 378 | |
| 379 | _Py_Ticker = 0; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 380 | things_to_do = 1; /* Signal main loop */ |
Guido van Rossum | 180d7b4 | 1994-09-29 09:45:57 +0000 | [diff] [blame] | 381 | busy = 0; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 382 | /* XXX End critical section */ |
| 383 | return 0; |
| 384 | } |
| 385 | |
Guido van Rossum | 180d7b4 | 1994-09-29 09:45:57 +0000 | [diff] [blame] | 386 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 387 | Py_MakePendingCalls(void) |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 388 | { |
Guido van Rossum | 180d7b4 | 1994-09-29 09:45:57 +0000 | [diff] [blame] | 389 | static int busy = 0; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 390 | #ifdef WITH_THREAD |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 391 | if (main_thread && PyThread_get_thread_ident() != main_thread) |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 392 | return 0; |
| 393 | #endif |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 394 | if (busy) |
Guido van Rossum | 180d7b4 | 1994-09-29 09:45:57 +0000 | [diff] [blame] | 395 | return 0; |
| 396 | busy = 1; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 397 | things_to_do = 0; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 398 | for (;;) { |
| 399 | int i; |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 400 | int (*func)(void *); |
| 401 | void *arg; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 402 | i = pendingfirst; |
| 403 | if (i == pendinglast) |
| 404 | break; /* Queue empty */ |
| 405 | func = pendingcalls[i].func; |
| 406 | arg = pendingcalls[i].arg; |
| 407 | pendingfirst = (i + 1) % NPENDINGCALLS; |
Guido van Rossum | 180d7b4 | 1994-09-29 09:45:57 +0000 | [diff] [blame] | 408 | if (func(arg) < 0) { |
| 409 | busy = 0; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 410 | things_to_do = 1; /* We're not done yet */ |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 411 | return -1; |
Guido van Rossum | 180d7b4 | 1994-09-29 09:45:57 +0000 | [diff] [blame] | 412 | } |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 413 | } |
Guido van Rossum | 180d7b4 | 1994-09-29 09:45:57 +0000 | [diff] [blame] | 414 | busy = 0; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 415 | return 0; |
| 416 | } |
| 417 | |
| 418 | |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 419 | /* The interpreter's recursion limit */ |
| 420 | |
Hye-Shik Chang | b6fa281 | 2005-04-04 15:49:02 +0000 | [diff] [blame] | 421 | #ifndef Py_DEFAULT_RECURSION_LIMIT |
| 422 | #define Py_DEFAULT_RECURSION_LIMIT 1000 |
| 423 | #endif |
| 424 | static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT; |
| 425 | int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 426 | |
Vladimir Marangozov | 7bd25be | 2000-09-01 11:07:19 +0000 | [diff] [blame] | 427 | int |
| 428 | Py_GetRecursionLimit(void) |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 429 | { |
| 430 | return recursion_limit; |
| 431 | } |
| 432 | |
Vladimir Marangozov | 7bd25be | 2000-09-01 11:07:19 +0000 | [diff] [blame] | 433 | void |
| 434 | Py_SetRecursionLimit(int new_limit) |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 435 | { |
| 436 | recursion_limit = new_limit; |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 437 | _Py_CheckRecursionLimit = recursion_limit; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 440 | /* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall() |
| 441 | if the recursion_depth reaches _Py_CheckRecursionLimit. |
| 442 | If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit |
| 443 | to guarantee that _Py_CheckRecursiveCall() is regularly called. |
| 444 | Without USE_STACKCHECK, there is no need for this. */ |
| 445 | int |
| 446 | _Py_CheckRecursiveCall(char *where) |
| 447 | { |
| 448 | PyThreadState *tstate = PyThreadState_GET(); |
| 449 | |
| 450 | #ifdef USE_STACKCHECK |
| 451 | if (PyOS_CheckStack()) { |
| 452 | --tstate->recursion_depth; |
| 453 | PyErr_SetString(PyExc_MemoryError, "Stack overflow"); |
| 454 | return -1; |
| 455 | } |
| 456 | #endif |
| 457 | if (tstate->recursion_depth > recursion_limit) { |
| 458 | --tstate->recursion_depth; |
| 459 | PyErr_Format(PyExc_RuntimeError, |
| 460 | "maximum recursion depth exceeded%s", |
| 461 | where); |
| 462 | return -1; |
| 463 | } |
| 464 | _Py_CheckRecursionLimit = recursion_limit; |
| 465 | return 0; |
| 466 | } |
| 467 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 468 | /* Status code for main loop (reason for stack unwind) */ |
Raymond Hettinger | 7c95865 | 2004-04-06 10:11:10 +0000 | [diff] [blame] | 469 | enum why_code { |
| 470 | WHY_NOT = 0x0001, /* No error */ |
| 471 | WHY_EXCEPTION = 0x0002, /* Exception occurred */ |
| 472 | WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */ |
| 473 | WHY_RETURN = 0x0008, /* 'return' statement */ |
| 474 | WHY_BREAK = 0x0010, /* 'break' statement */ |
| 475 | WHY_CONTINUE = 0x0020, /* 'continue' statement */ |
| 476 | WHY_YIELD = 0x0040 /* 'yield' operator */ |
| 477 | }; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 478 | |
Raymond Hettinger | 7c95865 | 2004-04-06 10:11:10 +0000 | [diff] [blame] | 479 | static enum why_code do_raise(PyObject *, PyObject *, PyObject *); |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 480 | static int unpack_iterable(PyObject *, int, PyObject **); |
Guido van Rossum | 1aa1483 | 1997-01-21 05:34:20 +0000 | [diff] [blame] | 481 | |
Skip Montanaro | d581d77 | 2002-09-03 20:10:45 +0000 | [diff] [blame] | 482 | /* for manipulating the thread switch and periodic "stuff" - used to be |
| 483 | per thread, now just a pair o' globals */ |
Skip Montanaro | 99dba27 | 2002-09-03 20:19:06 +0000 | [diff] [blame] | 484 | int _Py_CheckInterval = 100; |
| 485 | volatile int _Py_Ticker = 100; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 486 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 487 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 488 | PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 489 | { |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 490 | /* XXX raise SystemError if globals is NULL */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 491 | return PyEval_EvalCodeEx(co, |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 492 | globals, locals, |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 493 | (PyObject **)NULL, 0, |
| 494 | (PyObject **)NULL, 0, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 495 | (PyObject **)NULL, 0, |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 496 | NULL, NULL); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | |
| 500 | /* Interpreter main loop */ |
| 501 | |
Martin v. Löwis | 8d97e33 | 2004-06-27 15:43:12 +0000 | [diff] [blame] | 502 | PyObject * |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 503 | PyEval_EvalFrame(PyFrameObject *f) { |
| 504 | /* This is for backward compatibility with extension modules that |
| 505 | used this API; core interpreter code should call PyEval_EvalFrameEx() */ |
| 506 | return PyEval_EvalFrameEx(f, 0); |
| 507 | } |
| 508 | |
| 509 | PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 510 | PyEval_EvalFrameEx(PyFrameObject *f, int throwflag) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 511 | { |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 512 | #ifdef DXPAIRS |
| 513 | int lastopcode = 0; |
| 514 | #endif |
Armin Rigo | 8817fcd | 2004-06-17 10:22:40 +0000 | [diff] [blame] | 515 | register PyObject **stack_pointer; /* Next free slot in value stack */ |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 516 | register unsigned char *next_instr; |
Armin Rigo | 8817fcd | 2004-06-17 10:22:40 +0000 | [diff] [blame] | 517 | register int opcode; /* Current opcode */ |
| 518 | register int oparg; /* Current opcode argument, if any */ |
Raymond Hettinger | 7c95865 | 2004-04-06 10:11:10 +0000 | [diff] [blame] | 519 | register enum why_code why; /* Reason for block stack unwind */ |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 520 | register int err; /* Error status -- nonzero if error */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 521 | register PyObject *x; /* Result object -- NULL if error */ |
| 522 | register PyObject *v; /* Temporary objects popped off stack */ |
| 523 | register PyObject *w; |
| 524 | register PyObject *u; |
| 525 | register PyObject *t; |
Jeremy Hylton | 2b724da | 2001-01-29 22:51:52 +0000 | [diff] [blame] | 526 | register PyObject **fastlocals, **freevars; |
Guido van Rossum | 014518f | 1998-11-23 21:09:51 +0000 | [diff] [blame] | 527 | PyObject *retval = NULL; /* Return value */ |
Guido van Rossum | 885553e | 1998-12-21 18:33:30 +0000 | [diff] [blame] | 528 | PyThreadState *tstate = PyThreadState_GET(); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 529 | PyCodeObject *co; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 530 | |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 531 | /* when tracing we set things up so that |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 532 | |
| 533 | not (instr_lb <= current_bytecode_offset < instr_ub) |
| 534 | |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 535 | is true when the line being executed has changed. The |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 536 | initial values are such as to make this false the first |
| 537 | time it is tested. */ |
Armin Rigo | bf57a14 | 2004-03-22 19:24:58 +0000 | [diff] [blame] | 538 | int instr_ub = -1, instr_lb = 0, instr_prev = -1; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 539 | |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 540 | unsigned char *first_instr; |
Skip Montanaro | 04d80f8 | 2002-08-04 21:03:35 +0000 | [diff] [blame] | 541 | PyObject *names; |
| 542 | PyObject *consts; |
Neal Norwitz | 5f5153e | 2005-10-21 04:28:38 +0000 | [diff] [blame] | 543 | #if defined(Py_DEBUG) || defined(LLTRACE) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 544 | /* Make it easier to find out where we are with a debugger */ |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 545 | char *filename; |
Guido van Rossum | 99bec95 | 1992-09-03 20:29:45 +0000 | [diff] [blame] | 546 | #endif |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 547 | |
Neal Norwitz | a81d220 | 2002-07-14 00:27:26 +0000 | [diff] [blame] | 548 | /* Tuple access macros */ |
| 549 | |
| 550 | #ifndef Py_DEBUG |
| 551 | #define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i)) |
| 552 | #else |
| 553 | #define GETITEM(v, i) PyTuple_GetItem((v), (i)) |
| 554 | #endif |
| 555 | |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 556 | #ifdef WITH_TSC |
| 557 | /* Use Pentium timestamp counter to mark certain events: |
| 558 | inst0 -- beginning of switch statement for opcode dispatch |
| 559 | inst1 -- end of switch statement (may be skipped) |
| 560 | loop0 -- the top of the mainloop |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 561 | loop1 -- place where control returns again to top of mainloop |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 562 | (may be skipped) |
| 563 | intr1 -- beginning of long interruption |
| 564 | intr2 -- end of long interruption |
| 565 | |
| 566 | Many opcodes call out to helper C functions. In some cases, the |
| 567 | time in those functions should be counted towards the time for the |
| 568 | opcode, but not in all cases. For example, a CALL_FUNCTION opcode |
| 569 | calls another Python function; there's no point in charge all the |
| 570 | bytecode executed by the called function to the caller. |
| 571 | |
| 572 | It's hard to make a useful judgement statically. In the presence |
| 573 | of operator overloading, it's impossible to tell if a call will |
| 574 | execute new Python code or not. |
| 575 | |
| 576 | It's a case-by-case judgement. I'll use intr1 for the following |
| 577 | cases: |
| 578 | |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 579 | IMPORT_STAR |
| 580 | IMPORT_FROM |
| 581 | CALL_FUNCTION (and friends) |
| 582 | |
| 583 | */ |
| 584 | uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0; |
| 585 | int ticked = 0; |
| 586 | |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 587 | READ_TIMESTAMP(inst0); |
| 588 | READ_TIMESTAMP(inst1); |
| 589 | READ_TIMESTAMP(loop0); |
| 590 | READ_TIMESTAMP(loop1); |
Michael W. Hudson | 800ba23 | 2004-08-12 18:19:17 +0000 | [diff] [blame] | 591 | |
| 592 | /* shut up the compiler */ |
| 593 | opcode = 0; |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 594 | #endif |
| 595 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 596 | /* Code access macros */ |
| 597 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 598 | #define INSTR_OFFSET() ((int)(next_instr - first_instr)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 599 | #define NEXTOP() (*next_instr++) |
Raymond Hettinger | 5bed456 | 2004-04-10 23:34:17 +0000 | [diff] [blame] | 600 | #define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2]) |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 601 | #define PEEKARG() ((next_instr[2]<<8) + next_instr[1]) |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 602 | #define JUMPTO(x) (next_instr = first_instr + (x)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 603 | #define JUMPBY(x) (next_instr += (x)) |
| 604 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 605 | /* OpCode prediction macros |
| 606 | Some opcodes tend to come in pairs thus making it possible to predict |
| 607 | the second code when the first is run. For example, COMPARE_OP is often |
| 608 | followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And, those opcodes are often |
| 609 | followed by a POP_TOP. |
| 610 | |
| 611 | Verifying the prediction costs a single high-speed test of register |
Raymond Hettinger | ac207292 | 2003-03-16 15:41:11 +0000 | [diff] [blame] | 612 | variable against a constant. If the pairing was good, then the |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 613 | processor has a high likelihood of making its own successful branch |
| 614 | prediction which results in a nearly zero overhead transition to the |
| 615 | next opcode. |
| 616 | |
| 617 | A successful prediction saves a trip through the eval-loop including |
| 618 | its two unpredictable branches, the HASARG test and the switch-case. |
Raymond Hettinger | a721698 | 2004-02-08 19:59:27 +0000 | [diff] [blame] | 619 | |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 620 | If collecting opcode statistics, turn off prediction so that |
| 621 | statistics are accurately maintained (the predictions bypass |
Raymond Hettinger | a721698 | 2004-02-08 19:59:27 +0000 | [diff] [blame] | 622 | the opcode frequency counter updates). |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 623 | */ |
| 624 | |
Raymond Hettinger | a721698 | 2004-02-08 19:59:27 +0000 | [diff] [blame] | 625 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 626 | #define PREDICT(op) if (0) goto PRED_##op |
| 627 | #else |
Raymond Hettinger | ac207292 | 2003-03-16 15:41:11 +0000 | [diff] [blame] | 628 | #define PREDICT(op) if (*next_instr == op) goto PRED_##op |
Raymond Hettinger | a721698 | 2004-02-08 19:59:27 +0000 | [diff] [blame] | 629 | #endif |
| 630 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 631 | #define PREDICTED(op) PRED_##op: next_instr++ |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 632 | #define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3 |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 633 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 634 | /* Stack manipulation macros */ |
| 635 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 636 | /* The stack can grow at most MAXINT deep, as co_nlocals and |
| 637 | co_stacksize are ints. */ |
| 638 | #define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 639 | #define EMPTY() (STACK_LEVEL() == 0) |
| 640 | #define TOP() (stack_pointer[-1]) |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 641 | #define SECOND() (stack_pointer[-2]) |
| 642 | #define THIRD() (stack_pointer[-3]) |
| 643 | #define FOURTH() (stack_pointer[-4]) |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 644 | #define SET_TOP(v) (stack_pointer[-1] = (v)) |
| 645 | #define SET_SECOND(v) (stack_pointer[-2] = (v)) |
| 646 | #define SET_THIRD(v) (stack_pointer[-3] = (v)) |
| 647 | #define SET_FOURTH(v) (stack_pointer[-4] = (v)) |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 648 | #define BASIC_STACKADJ(n) (stack_pointer += n) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 649 | #define BASIC_PUSH(v) (*stack_pointer++ = (v)) |
| 650 | #define BASIC_POP() (*--stack_pointer) |
| 651 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 652 | #ifdef LLTRACE |
Jeremy Hylton | 1436815 | 2001-10-17 13:29:30 +0000 | [diff] [blame] | 653 | #define PUSH(v) { (void)(BASIC_PUSH(v), \ |
| 654 | lltrace && prtrace(TOP(), "push")); \ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 655 | assert(STACK_LEVEL() <= co->co_stacksize); } |
Fred Drake | de26cfc | 2001-10-13 06:11:28 +0000 | [diff] [blame] | 656 | #define POP() ((void)(lltrace && prtrace(TOP(), "pop")), BASIC_POP()) |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 657 | #define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \ |
| 658 | lltrace && prtrace(TOP(), "stackadj")); \ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 659 | assert(STACK_LEVEL() <= co->co_stacksize); } |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 660 | #define EXT_POP(STACK_POINTER) (lltrace && prtrace(*(STACK_POINTER), "ext_pop"), *--(STACK_POINTER)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 661 | #else |
| 662 | #define PUSH(v) BASIC_PUSH(v) |
| 663 | #define POP() BASIC_POP() |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 664 | #define STACKADJ(n) BASIC_STACKADJ(n) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 665 | #define EXT_POP(STACK_POINTER) (*--(STACK_POINTER)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 666 | #endif |
| 667 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 668 | /* Local variable macros */ |
| 669 | |
| 670 | #define GETLOCAL(i) (fastlocals[i]) |
Guido van Rossum | cfbf1a3 | 2002-03-28 20:17:52 +0000 | [diff] [blame] | 671 | |
| 672 | /* The SETLOCAL() macro must not DECREF the local variable in-place and |
| 673 | then store the new value; it must copy the old value to a temporary |
| 674 | value, then store the new value, and then DECREF the temporary value. |
| 675 | This is because it is possible that during the DECREF the frame is |
| 676 | accessed by other code (e.g. a __del__ method or gc.collect()) and the |
| 677 | variable would be pointing to already-freed memory. */ |
| 678 | #define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \ |
| 679 | GETLOCAL(i) = value; \ |
| 680 | Py_XDECREF(tmp); } while (0) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 681 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 682 | /* Start of code */ |
| 683 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 684 | if (f == NULL) |
| 685 | return NULL; |
| 686 | |
Armin Rigo | 1d313ab | 2003-10-25 14:33:09 +0000 | [diff] [blame] | 687 | /* push frame */ |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 688 | if (Py_EnterRecursiveCall("")) |
Armin Rigo | 1d313ab | 2003-10-25 14:33:09 +0000 | [diff] [blame] | 689 | return NULL; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 690 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 691 | tstate->frame = f; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 692 | |
Neil Schemenauer | 6c0f200 | 2001-09-04 19:03:35 +0000 | [diff] [blame] | 693 | if (tstate->use_tracing) { |
| 694 | if (tstate->c_tracefunc != NULL) { |
| 695 | /* tstate->c_tracefunc, if defined, is a |
| 696 | function that will be called on *every* entry |
| 697 | to a code block. Its return value, if not |
| 698 | None, is a function that will be called at |
| 699 | the start of each executed line of code. |
| 700 | (Actually, the function must return itself |
| 701 | in order to continue tracing.) The trace |
| 702 | functions are called with three arguments: |
| 703 | a pointer to the current frame, a string |
| 704 | indicating why the function is called, and |
| 705 | an argument which depends on the situation. |
| 706 | The global trace function is also called |
| 707 | whenever an exception is detected. */ |
| 708 | if (call_trace(tstate->c_tracefunc, tstate->c_traceobj, |
| 709 | f, PyTrace_CALL, Py_None)) { |
Neil Schemenauer | 6c0f200 | 2001-09-04 19:03:35 +0000 | [diff] [blame] | 710 | /* Trace function raised an error */ |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 711 | goto exit_eval_frame; |
Neil Schemenauer | 6c0f200 | 2001-09-04 19:03:35 +0000 | [diff] [blame] | 712 | } |
| 713 | } |
| 714 | if (tstate->c_profilefunc != NULL) { |
| 715 | /* Similar for c_profilefunc, except it needn't |
| 716 | return itself and isn't called for "line" events */ |
| 717 | if (call_trace(tstate->c_profilefunc, |
| 718 | tstate->c_profileobj, |
| 719 | f, PyTrace_CALL, Py_None)) { |
Neil Schemenauer | 6c0f200 | 2001-09-04 19:03:35 +0000 | [diff] [blame] | 720 | /* Profile function raised an error */ |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 721 | goto exit_eval_frame; |
Neil Schemenauer | 6c0f200 | 2001-09-04 19:03:35 +0000 | [diff] [blame] | 722 | } |
| 723 | } |
| 724 | } |
| 725 | |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 726 | co = f->f_code; |
| 727 | names = co->co_names; |
| 728 | consts = co->co_consts; |
| 729 | fastlocals = f->f_localsplus; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 730 | freevars = f->f_localsplus + co->co_nlocals; |
Brett Cannon | c9371d4 | 2005-06-25 08:23:41 +0000 | [diff] [blame] | 731 | first_instr = (unsigned char*) PyString_AS_STRING(co->co_code); |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 732 | /* An explanation is in order for the next line. |
| 733 | |
| 734 | f->f_lasti now refers to the index of the last instruction |
| 735 | executed. You might think this was obvious from the name, but |
| 736 | this wasn't always true before 2.3! PyFrame_New now sets |
| 737 | f->f_lasti to -1 (i.e. the index *before* the first instruction) |
| 738 | and YIELD_VALUE doesn't fiddle with f_lasti any more. So this |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 739 | does work. Promise. |
| 740 | |
| 741 | When the PREDICT() macros are enabled, some opcode pairs follow in |
| 742 | direct succession without updating f->f_lasti. A successful |
| 743 | prediction effectively links the two codes together as if they |
| 744 | were a single new opcode; accordingly,f->f_lasti will point to |
| 745 | the first code in the pair (for instance, GET_ITER followed by |
| 746 | FOR_ITER is effectively a single opcode and f->f_lasti will point |
| 747 | at to the beginning of the combined pair.) |
| 748 | */ |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 749 | next_instr = first_instr + f->f_lasti + 1; |
| 750 | stack_pointer = f->f_stacktop; |
| 751 | assert(stack_pointer != NULL); |
| 752 | f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */ |
| 753 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 754 | #ifdef LLTRACE |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 755 | lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 756 | #endif |
Neal Norwitz | 5f5153e | 2005-10-21 04:28:38 +0000 | [diff] [blame] | 757 | #if defined(Py_DEBUG) || defined(LLTRACE) |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 758 | filename = PyString_AsString(co->co_filename); |
| 759 | #endif |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 760 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 761 | why = WHY_NOT; |
| 762 | err = 0; |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 763 | x = Py_None; /* Not a reference, just anything non-NULL */ |
Fred Drake | 48fba73 | 2000-10-11 13:54:07 +0000 | [diff] [blame] | 764 | w = NULL; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 765 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 766 | if (throwflag) { /* support for generator.throw() */ |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 767 | why = WHY_EXCEPTION; |
| 768 | goto on_error; |
| 769 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 770 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 771 | for (;;) { |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 772 | #ifdef WITH_TSC |
| 773 | if (inst1 == 0) { |
| 774 | /* Almost surely, the opcode executed a break |
| 775 | or a continue, preventing inst1 from being set |
| 776 | on the way out of the loop. |
| 777 | */ |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 778 | READ_TIMESTAMP(inst1); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 779 | loop1 = inst1; |
| 780 | } |
| 781 | dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1, |
| 782 | intr0, intr1); |
| 783 | ticked = 0; |
| 784 | inst1 = 0; |
| 785 | intr0 = 0; |
| 786 | intr1 = 0; |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 787 | READ_TIMESTAMP(loop0); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 788 | #endif |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 789 | assert(stack_pointer >= f->f_valuestack); /* else underflow */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 790 | assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */ |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 791 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 792 | /* Do periodic things. Doing this every time through |
| 793 | the loop would add too much overhead, so we do it |
| 794 | only every Nth instruction. We also do it if |
| 795 | ``things_to_do'' is set, i.e. when an asynchronous |
| 796 | event needs attention (e.g. a signal handler or |
| 797 | async I/O handler); see Py_AddPendingCall() and |
| 798 | Py_MakePendingCalls() above. */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 799 | |
Skip Montanaro | d581d77 | 2002-09-03 20:10:45 +0000 | [diff] [blame] | 800 | if (--_Py_Ticker < 0) { |
Guido van Rossum | b8b6d0c | 2003-06-28 21:53:52 +0000 | [diff] [blame] | 801 | if (*next_instr == SETUP_FINALLY) { |
| 802 | /* Make the last opcode before |
| 803 | a try: finally: block uninterruptable. */ |
| 804 | goto fast_next_opcode; |
| 805 | } |
Skip Montanaro | d581d77 | 2002-09-03 20:10:45 +0000 | [diff] [blame] | 806 | _Py_Ticker = _Py_CheckInterval; |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 807 | tstate->tick_counter++; |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 808 | #ifdef WITH_TSC |
| 809 | ticked = 1; |
| 810 | #endif |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 811 | if (things_to_do) { |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 812 | if (Py_MakePendingCalls() < 0) { |
| 813 | why = WHY_EXCEPTION; |
| 814 | goto on_error; |
| 815 | } |
Kurt B. Kaiser | 4c79a83 | 2004-11-23 18:06:08 +0000 | [diff] [blame] | 816 | if (things_to_do) |
| 817 | /* MakePendingCalls() didn't succeed. |
| 818 | Force early re-execution of this |
| 819 | "periodic" code, possibly after |
| 820 | a thread switch */ |
| 821 | _Py_Ticker = 0; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 822 | } |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 823 | #ifdef WITH_THREAD |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 824 | if (interpreter_lock) { |
| 825 | /* Give another thread a chance */ |
| 826 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 827 | if (PyThreadState_Swap(NULL) != tstate) |
| 828 | Py_FatalError("ceval: tstate mix-up"); |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 829 | PyThread_release_lock(interpreter_lock); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 830 | |
| 831 | /* Other threads may run now */ |
| 832 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 833 | PyThread_acquire_lock(interpreter_lock, 1); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 834 | if (PyThreadState_Swap(tstate) != NULL) |
| 835 | Py_FatalError("ceval: orphan tstate"); |
Guido van Rossum | b8b6d0c | 2003-06-28 21:53:52 +0000 | [diff] [blame] | 836 | |
| 837 | /* Check for thread interrupts */ |
| 838 | |
| 839 | if (tstate->async_exc != NULL) { |
| 840 | x = tstate->async_exc; |
| 841 | tstate->async_exc = NULL; |
| 842 | PyErr_SetNone(x); |
| 843 | Py_DECREF(x); |
| 844 | why = WHY_EXCEPTION; |
| 845 | goto on_error; |
| 846 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 847 | } |
| 848 | #endif |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 849 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 850 | |
Neil Schemenauer | 6354386 | 2002-02-17 19:10:14 +0000 | [diff] [blame] | 851 | fast_next_opcode: |
Guido van Rossum | 99bec95 | 1992-09-03 20:29:45 +0000 | [diff] [blame] | 852 | f->f_lasti = INSTR_OFFSET(); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 853 | |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 854 | /* line-by-line tracing support */ |
| 855 | |
| 856 | if (tstate->c_tracefunc != NULL && !tstate->tracing) { |
| 857 | /* see maybe_call_line_trace |
| 858 | for expository comments */ |
| 859 | f->f_stacktop = stack_pointer; |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 860 | |
Michael W. Hudson | 58ee2af | 2003-04-29 16:18:47 +0000 | [diff] [blame] | 861 | err = maybe_call_line_trace(tstate->c_tracefunc, |
| 862 | tstate->c_traceobj, |
Armin Rigo | bf57a14 | 2004-03-22 19:24:58 +0000 | [diff] [blame] | 863 | f, &instr_lb, &instr_ub, |
| 864 | &instr_prev); |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 865 | /* Reload possibly changed frame fields */ |
| 866 | JUMPTO(f->f_lasti); |
Michael W. Hudson | 58ee2af | 2003-04-29 16:18:47 +0000 | [diff] [blame] | 867 | if (f->f_stacktop != NULL) { |
| 868 | stack_pointer = f->f_stacktop; |
| 869 | f->f_stacktop = NULL; |
| 870 | } |
| 871 | if (err) { |
| 872 | /* trace function raised an exception */ |
| 873 | goto on_error; |
| 874 | } |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | /* Extract opcode and argument */ |
| 878 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 879 | opcode = NEXTOP(); |
Armin Rigo | 8817fcd | 2004-06-17 10:22:40 +0000 | [diff] [blame] | 880 | oparg = 0; /* allows oparg to be stored in a register because |
| 881 | it doesn't have to be remembered across a full loop */ |
Raymond Hettinger | 5bed456 | 2004-04-10 23:34:17 +0000 | [diff] [blame] | 882 | if (HAS_ARG(opcode)) |
| 883 | oparg = NEXTARG(); |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 884 | dispatch_opcode: |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 885 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 886 | #ifdef DXPAIRS |
| 887 | dxpairs[lastopcode][opcode]++; |
| 888 | lastopcode = opcode; |
| 889 | #endif |
| 890 | dxp[opcode]++; |
| 891 | #endif |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 892 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 893 | #ifdef LLTRACE |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 894 | /* Instruction tracing */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 895 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 896 | if (lltrace) { |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 897 | if (HAS_ARG(opcode)) { |
| 898 | printf("%d: %d, %d\n", |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 899 | f->f_lasti, opcode, oparg); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 900 | } |
| 901 | else { |
| 902 | printf("%d: %d\n", |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 903 | f->f_lasti, opcode); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 904 | } |
| 905 | } |
| 906 | #endif |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 907 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 908 | /* Main switch on opcode */ |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 909 | READ_TIMESTAMP(inst0); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 910 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 911 | switch (opcode) { |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 912 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 913 | /* BEWARE! |
| 914 | It is essential that any operation that fails sets either |
| 915 | x to NULL, err to nonzero, or why to anything but WHY_NOT, |
| 916 | and that no operation that succeeds does this! */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 917 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 918 | /* case STOP_CODE: this is an error! */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 919 | |
Raymond Hettinger | 9c18e81 | 2004-06-21 16:31:15 +0000 | [diff] [blame] | 920 | case NOP: |
| 921 | goto fast_next_opcode; |
| 922 | |
Neil Schemenauer | 6354386 | 2002-02-17 19:10:14 +0000 | [diff] [blame] | 923 | case LOAD_FAST: |
| 924 | x = GETLOCAL(oparg); |
| 925 | if (x != NULL) { |
| 926 | Py_INCREF(x); |
| 927 | PUSH(x); |
| 928 | goto fast_next_opcode; |
| 929 | } |
| 930 | format_exc_check_arg(PyExc_UnboundLocalError, |
| 931 | UNBOUNDLOCAL_ERROR_MSG, |
| 932 | PyTuple_GetItem(co->co_varnames, oparg)); |
| 933 | break; |
| 934 | |
| 935 | case LOAD_CONST: |
Skip Montanaro | 04d80f8 | 2002-08-04 21:03:35 +0000 | [diff] [blame] | 936 | x = GETITEM(consts, oparg); |
Neil Schemenauer | 6354386 | 2002-02-17 19:10:14 +0000 | [diff] [blame] | 937 | Py_INCREF(x); |
| 938 | PUSH(x); |
| 939 | goto fast_next_opcode; |
| 940 | |
Raymond Hettinger | 7dc5221 | 2003-03-16 20:14:44 +0000 | [diff] [blame] | 941 | PREDICTED_WITH_ARG(STORE_FAST); |
Neil Schemenauer | 6354386 | 2002-02-17 19:10:14 +0000 | [diff] [blame] | 942 | case STORE_FAST: |
| 943 | v = POP(); |
| 944 | SETLOCAL(oparg, v); |
| 945 | goto fast_next_opcode; |
| 946 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 947 | PREDICTED(POP_TOP); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 948 | case POP_TOP: |
| 949 | v = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 950 | Py_DECREF(v); |
Neil Schemenauer | 6354386 | 2002-02-17 19:10:14 +0000 | [diff] [blame] | 951 | goto fast_next_opcode; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 952 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 953 | case ROT_TWO: |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 954 | v = TOP(); |
| 955 | w = SECOND(); |
| 956 | SET_TOP(w); |
| 957 | SET_SECOND(v); |
Raymond Hettinger | 080cb32 | 2003-03-14 01:37:42 +0000 | [diff] [blame] | 958 | goto fast_next_opcode; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 959 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 960 | case ROT_THREE: |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 961 | v = TOP(); |
| 962 | w = SECOND(); |
| 963 | x = THIRD(); |
| 964 | SET_TOP(w); |
| 965 | SET_SECOND(x); |
| 966 | SET_THIRD(v); |
Raymond Hettinger | 080cb32 | 2003-03-14 01:37:42 +0000 | [diff] [blame] | 967 | goto fast_next_opcode; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 968 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 969 | case ROT_FOUR: |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 970 | u = TOP(); |
| 971 | v = SECOND(); |
| 972 | w = THIRD(); |
| 973 | x = FOURTH(); |
| 974 | SET_TOP(v); |
| 975 | SET_SECOND(w); |
| 976 | SET_THIRD(x); |
| 977 | SET_FOURTH(u); |
Raymond Hettinger | 080cb32 | 2003-03-14 01:37:42 +0000 | [diff] [blame] | 978 | goto fast_next_opcode; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 979 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 980 | case DUP_TOP: |
| 981 | v = TOP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 982 | Py_INCREF(v); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 983 | PUSH(v); |
Raymond Hettinger | 080cb32 | 2003-03-14 01:37:42 +0000 | [diff] [blame] | 984 | goto fast_next_opcode; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 985 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 986 | case DUP_TOPX: |
Raymond Hettinger | 4bad9ba | 2003-01-19 05:08:13 +0000 | [diff] [blame] | 987 | if (oparg == 2) { |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 988 | x = TOP(); |
Tim Peters | 35ba689 | 2000-10-11 07:04:49 +0000 | [diff] [blame] | 989 | Py_INCREF(x); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 990 | w = SECOND(); |
Tim Peters | 35ba689 | 2000-10-11 07:04:49 +0000 | [diff] [blame] | 991 | Py_INCREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 992 | STACKADJ(2); |
| 993 | SET_TOP(x); |
| 994 | SET_SECOND(w); |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 995 | goto fast_next_opcode; |
Raymond Hettinger | 4bad9ba | 2003-01-19 05:08:13 +0000 | [diff] [blame] | 996 | } else if (oparg == 3) { |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 997 | x = TOP(); |
Tim Peters | 35ba689 | 2000-10-11 07:04:49 +0000 | [diff] [blame] | 998 | Py_INCREF(x); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 999 | w = SECOND(); |
Tim Peters | 35ba689 | 2000-10-11 07:04:49 +0000 | [diff] [blame] | 1000 | Py_INCREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1001 | v = THIRD(); |
Tim Peters | 35ba689 | 2000-10-11 07:04:49 +0000 | [diff] [blame] | 1002 | Py_INCREF(v); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1003 | STACKADJ(3); |
| 1004 | SET_TOP(x); |
| 1005 | SET_SECOND(w); |
| 1006 | SET_THIRD(v); |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 1007 | goto fast_next_opcode; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1008 | } |
Raymond Hettinger | 4bad9ba | 2003-01-19 05:08:13 +0000 | [diff] [blame] | 1009 | Py_FatalError("invalid argument to DUP_TOPX" |
| 1010 | " (bytecode corruption?)"); |
Tim Peters | 35ba689 | 2000-10-11 07:04:49 +0000 | [diff] [blame] | 1011 | break; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1012 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1013 | case UNARY_POSITIVE: |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1014 | v = TOP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1015 | x = PyNumber_Positive(v); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1016 | Py_DECREF(v); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1017 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1018 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1019 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1020 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1021 | case UNARY_NEGATIVE: |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1022 | v = TOP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1023 | x = PyNumber_Negative(v); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1024 | Py_DECREF(v); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1025 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1026 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1027 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1028 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1029 | case UNARY_NOT: |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1030 | v = TOP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1031 | err = PyObject_IsTrue(v); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1032 | Py_DECREF(v); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1033 | if (err == 0) { |
| 1034 | Py_INCREF(Py_True); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1035 | SET_TOP(Py_True); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1036 | continue; |
| 1037 | } |
| 1038 | else if (err > 0) { |
| 1039 | Py_INCREF(Py_False); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1040 | SET_TOP(Py_False); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1041 | err = 0; |
| 1042 | continue; |
| 1043 | } |
Raymond Hettinger | 8bb90a5 | 2003-01-14 12:43:10 +0000 | [diff] [blame] | 1044 | STACKADJ(-1); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1045 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1046 | |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1047 | case UNARY_INVERT: |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1048 | v = TOP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1049 | x = PyNumber_Invert(v); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1050 | Py_DECREF(v); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1051 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1052 | if (x != NULL) continue; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1053 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1054 | |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1055 | case BINARY_POWER: |
| 1056 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1057 | v = TOP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1058 | x = PyNumber_Power(v, w, Py_None); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1059 | Py_DECREF(v); |
| 1060 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1061 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1062 | if (x != NULL) continue; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1063 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1064 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1065 | case BINARY_MULTIPLY: |
| 1066 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1067 | v = TOP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1068 | x = PyNumber_Multiply(v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1069 | Py_DECREF(v); |
| 1070 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1071 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1072 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1073 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1074 | |
Tim Peters | 3caca23 | 2001-12-06 06:23:26 +0000 | [diff] [blame] | 1075 | case BINARY_TRUE_DIVIDE: |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1076 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1077 | v = TOP(); |
Tim Peters | 3caca23 | 2001-12-06 06:23:26 +0000 | [diff] [blame] | 1078 | x = PyNumber_TrueDivide(v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1079 | Py_DECREF(v); |
| 1080 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1081 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1082 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1083 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1084 | |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1085 | case BINARY_FLOOR_DIVIDE: |
| 1086 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1087 | v = TOP(); |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1088 | x = PyNumber_FloorDivide(v, w); |
| 1089 | Py_DECREF(v); |
| 1090 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1091 | SET_TOP(x); |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1092 | if (x != NULL) continue; |
| 1093 | break; |
| 1094 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1095 | case BINARY_MODULO: |
| 1096 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1097 | v = TOP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1098 | x = PyNumber_Remainder(v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1099 | Py_DECREF(v); |
| 1100 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1101 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1102 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1103 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1104 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1105 | case BINARY_ADD: |
| 1106 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1107 | v = TOP(); |
Tim Peters | c1e6d96 | 2001-10-05 20:21:03 +0000 | [diff] [blame] | 1108 | if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) { |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1109 | /* INLINE: int + int */ |
| 1110 | register long a, b, i; |
Guido van Rossum | cf183ac | 1998-12-04 18:51:36 +0000 | [diff] [blame] | 1111 | a = PyInt_AS_LONG(v); |
| 1112 | b = PyInt_AS_LONG(w); |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1113 | i = a + b; |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1114 | if ((i^a) < 0 && (i^b) < 0) |
| 1115 | goto slow_add; |
| 1116 | x = PyInt_FromLong(i); |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1117 | } |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 1118 | else if (PyString_CheckExact(v) && |
| 1119 | PyString_CheckExact(w)) { |
| 1120 | x = string_concatenate(v, w, f, next_instr); |
| 1121 | /* string_concatenate consumed the ref to v */ |
| 1122 | goto skip_decref_vx; |
| 1123 | } |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1124 | else { |
| 1125 | slow_add: |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1126 | x = PyNumber_Add(v, w); |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1127 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1128 | Py_DECREF(v); |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 1129 | skip_decref_vx: |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1130 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1131 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1132 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1133 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1134 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1135 | case BINARY_SUBTRACT: |
| 1136 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1137 | v = TOP(); |
Tim Peters | c1e6d96 | 2001-10-05 20:21:03 +0000 | [diff] [blame] | 1138 | if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) { |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1139 | /* INLINE: int - int */ |
| 1140 | register long a, b, i; |
Guido van Rossum | cf183ac | 1998-12-04 18:51:36 +0000 | [diff] [blame] | 1141 | a = PyInt_AS_LONG(v); |
| 1142 | b = PyInt_AS_LONG(w); |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1143 | i = a - b; |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1144 | if ((i^a) < 0 && (i^~b) < 0) |
| 1145 | goto slow_sub; |
| 1146 | x = PyInt_FromLong(i); |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1147 | } |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1148 | else { |
| 1149 | slow_sub: |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1150 | x = PyNumber_Subtract(v, w); |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1151 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1152 | Py_DECREF(v); |
| 1153 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1154 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1155 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1156 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1157 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1158 | case BINARY_SUBSCR: |
| 1159 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1160 | v = TOP(); |
Tim Peters | b1c4698 | 2001-10-05 20:41:38 +0000 | [diff] [blame] | 1161 | if (PyList_CheckExact(v) && PyInt_CheckExact(w)) { |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1162 | /* INLINE: list[int] */ |
Neal Norwitz | 814e938 | 2006-03-02 07:54:28 +0000 | [diff] [blame] | 1163 | Py_ssize_t i = PyInt_AsSsize_t(w); |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1164 | if (i < 0) |
Guido van Rossum | fa00e95 | 1998-07-08 15:02:37 +0000 | [diff] [blame] | 1165 | i += PyList_GET_SIZE(v); |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 1166 | if (i >= 0 && i < PyList_GET_SIZE(v)) { |
Guido van Rossum | fa00e95 | 1998-07-08 15:02:37 +0000 | [diff] [blame] | 1167 | x = PyList_GET_ITEM(v, i); |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1168 | Py_INCREF(x); |
| 1169 | } |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 1170 | else |
| 1171 | goto slow_get; |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1172 | } |
| 1173 | else |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 1174 | slow_get: |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1175 | x = PyObject_GetItem(v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1176 | Py_DECREF(v); |
| 1177 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1178 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1179 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1180 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1181 | |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1182 | case BINARY_LSHIFT: |
| 1183 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1184 | v = TOP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1185 | x = PyNumber_Lshift(v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1186 | Py_DECREF(v); |
| 1187 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1188 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1189 | if (x != NULL) continue; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1190 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1191 | |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1192 | case BINARY_RSHIFT: |
| 1193 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1194 | v = TOP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1195 | x = PyNumber_Rshift(v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1196 | Py_DECREF(v); |
| 1197 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1198 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1199 | if (x != NULL) continue; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1200 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1201 | |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1202 | case BINARY_AND: |
| 1203 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1204 | v = TOP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1205 | x = PyNumber_And(v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1206 | Py_DECREF(v); |
| 1207 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1208 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1209 | if (x != NULL) continue; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1210 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1211 | |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1212 | case BINARY_XOR: |
| 1213 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1214 | v = TOP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1215 | x = PyNumber_Xor(v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1216 | Py_DECREF(v); |
| 1217 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1218 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1219 | if (x != NULL) continue; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1220 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1221 | |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1222 | case BINARY_OR: |
| 1223 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1224 | v = TOP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1225 | x = PyNumber_Or(v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1226 | Py_DECREF(v); |
| 1227 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1228 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1229 | if (x != NULL) continue; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1230 | break; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1231 | |
Raymond Hettinger | dd80f76 | 2004-03-07 07:31:06 +0000 | [diff] [blame] | 1232 | case LIST_APPEND: |
| 1233 | w = POP(); |
| 1234 | v = POP(); |
| 1235 | err = PyList_Append(v, w); |
| 1236 | Py_DECREF(v); |
| 1237 | Py_DECREF(w); |
Raymond Hettinger | fba1cfc | 2004-03-12 16:33:17 +0000 | [diff] [blame] | 1238 | if (err == 0) { |
| 1239 | PREDICT(JUMP_ABSOLUTE); |
| 1240 | continue; |
| 1241 | } |
Raymond Hettinger | dd80f76 | 2004-03-07 07:31:06 +0000 | [diff] [blame] | 1242 | break; |
| 1243 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1244 | case INPLACE_POWER: |
| 1245 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1246 | v = TOP(); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1247 | x = PyNumber_InPlacePower(v, w, Py_None); |
| 1248 | Py_DECREF(v); |
| 1249 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1250 | SET_TOP(x); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1251 | if (x != NULL) continue; |
| 1252 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1253 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1254 | case INPLACE_MULTIPLY: |
| 1255 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1256 | v = TOP(); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1257 | x = PyNumber_InPlaceMultiply(v, w); |
| 1258 | Py_DECREF(v); |
| 1259 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1260 | SET_TOP(x); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1261 | if (x != NULL) continue; |
| 1262 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1263 | |
Tim Peters | 54b1191 | 2001-12-25 18:49:11 +0000 | [diff] [blame] | 1264 | case INPLACE_TRUE_DIVIDE: |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1265 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1266 | v = TOP(); |
Tim Peters | 54b1191 | 2001-12-25 18:49:11 +0000 | [diff] [blame] | 1267 | x = PyNumber_InPlaceTrueDivide(v, w); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1268 | Py_DECREF(v); |
| 1269 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1270 | SET_TOP(x); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1271 | if (x != NULL) continue; |
| 1272 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1273 | |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1274 | case INPLACE_FLOOR_DIVIDE: |
| 1275 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1276 | v = TOP(); |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1277 | x = PyNumber_InPlaceFloorDivide(v, w); |
| 1278 | Py_DECREF(v); |
| 1279 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1280 | SET_TOP(x); |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1281 | if (x != NULL) continue; |
| 1282 | break; |
| 1283 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1284 | case INPLACE_MODULO: |
| 1285 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1286 | v = TOP(); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1287 | x = PyNumber_InPlaceRemainder(v, w); |
| 1288 | Py_DECREF(v); |
| 1289 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1290 | SET_TOP(x); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1291 | if (x != NULL) continue; |
| 1292 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1293 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1294 | case INPLACE_ADD: |
| 1295 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1296 | v = TOP(); |
Tim Peters | c1e6d96 | 2001-10-05 20:21:03 +0000 | [diff] [blame] | 1297 | if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1298 | /* INLINE: int + int */ |
| 1299 | register long a, b, i; |
| 1300 | a = PyInt_AS_LONG(v); |
| 1301 | b = PyInt_AS_LONG(w); |
| 1302 | i = a + b; |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1303 | if ((i^a) < 0 && (i^b) < 0) |
| 1304 | goto slow_iadd; |
| 1305 | x = PyInt_FromLong(i); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1306 | } |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 1307 | else if (PyString_CheckExact(v) && |
| 1308 | PyString_CheckExact(w)) { |
| 1309 | x = string_concatenate(v, w, f, next_instr); |
| 1310 | /* string_concatenate consumed the ref to v */ |
| 1311 | goto skip_decref_v; |
| 1312 | } |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1313 | else { |
| 1314 | slow_iadd: |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1315 | x = PyNumber_InPlaceAdd(v, w); |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1316 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1317 | Py_DECREF(v); |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 1318 | skip_decref_v: |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1319 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1320 | SET_TOP(x); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1321 | if (x != NULL) continue; |
| 1322 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1323 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1324 | case INPLACE_SUBTRACT: |
| 1325 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1326 | v = TOP(); |
Tim Peters | c1e6d96 | 2001-10-05 20:21:03 +0000 | [diff] [blame] | 1327 | if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1328 | /* INLINE: int - int */ |
| 1329 | register long a, b, i; |
| 1330 | a = PyInt_AS_LONG(v); |
| 1331 | b = PyInt_AS_LONG(w); |
| 1332 | i = a - b; |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1333 | if ((i^a) < 0 && (i^~b) < 0) |
| 1334 | goto slow_isub; |
| 1335 | x = PyInt_FromLong(i); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1336 | } |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1337 | else { |
| 1338 | slow_isub: |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1339 | x = PyNumber_InPlaceSubtract(v, w); |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1340 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1341 | Py_DECREF(v); |
| 1342 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1343 | SET_TOP(x); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1344 | if (x != NULL) continue; |
| 1345 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1346 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1347 | case INPLACE_LSHIFT: |
| 1348 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1349 | v = TOP(); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1350 | x = PyNumber_InPlaceLshift(v, w); |
| 1351 | Py_DECREF(v); |
| 1352 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1353 | SET_TOP(x); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1354 | if (x != NULL) continue; |
| 1355 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1356 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1357 | case INPLACE_RSHIFT: |
| 1358 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1359 | v = TOP(); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1360 | x = PyNumber_InPlaceRshift(v, w); |
| 1361 | Py_DECREF(v); |
| 1362 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1363 | SET_TOP(x); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1364 | if (x != NULL) continue; |
| 1365 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1366 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1367 | case INPLACE_AND: |
| 1368 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1369 | v = TOP(); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1370 | x = PyNumber_InPlaceAnd(v, w); |
| 1371 | Py_DECREF(v); |
| 1372 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1373 | SET_TOP(x); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1374 | if (x != NULL) continue; |
| 1375 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1376 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1377 | case INPLACE_XOR: |
| 1378 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1379 | v = TOP(); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1380 | x = PyNumber_InPlaceXor(v, w); |
| 1381 | Py_DECREF(v); |
| 1382 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1383 | SET_TOP(x); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1384 | if (x != NULL) continue; |
| 1385 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1386 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1387 | case INPLACE_OR: |
| 1388 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1389 | v = TOP(); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1390 | x = PyNumber_InPlaceOr(v, w); |
| 1391 | Py_DECREF(v); |
| 1392 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1393 | SET_TOP(x); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1394 | if (x != NULL) continue; |
| 1395 | break; |
| 1396 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1397 | case SLICE+0: |
| 1398 | case SLICE+1: |
| 1399 | case SLICE+2: |
| 1400 | case SLICE+3: |
| 1401 | if ((opcode-SLICE) & 2) |
| 1402 | w = POP(); |
| 1403 | else |
| 1404 | w = NULL; |
| 1405 | if ((opcode-SLICE) & 1) |
| 1406 | v = POP(); |
| 1407 | else |
| 1408 | v = NULL; |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1409 | u = TOP(); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1410 | x = apply_slice(u, v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1411 | Py_DECREF(u); |
| 1412 | Py_XDECREF(v); |
| 1413 | Py_XDECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1414 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1415 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1416 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1417 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1418 | case STORE_SLICE+0: |
| 1419 | case STORE_SLICE+1: |
| 1420 | case STORE_SLICE+2: |
| 1421 | case STORE_SLICE+3: |
| 1422 | if ((opcode-STORE_SLICE) & 2) |
| 1423 | w = POP(); |
| 1424 | else |
| 1425 | w = NULL; |
| 1426 | if ((opcode-STORE_SLICE) & 1) |
| 1427 | v = POP(); |
| 1428 | else |
| 1429 | v = NULL; |
| 1430 | u = POP(); |
| 1431 | t = POP(); |
| 1432 | err = assign_slice(u, v, w, t); /* u[v:w] = t */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1433 | Py_DECREF(t); |
| 1434 | Py_DECREF(u); |
| 1435 | Py_XDECREF(v); |
| 1436 | Py_XDECREF(w); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1437 | if (err == 0) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1438 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1439 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1440 | case DELETE_SLICE+0: |
| 1441 | case DELETE_SLICE+1: |
| 1442 | case DELETE_SLICE+2: |
| 1443 | case DELETE_SLICE+3: |
| 1444 | if ((opcode-DELETE_SLICE) & 2) |
| 1445 | w = POP(); |
| 1446 | else |
| 1447 | w = NULL; |
| 1448 | if ((opcode-DELETE_SLICE) & 1) |
| 1449 | v = POP(); |
| 1450 | else |
| 1451 | v = NULL; |
| 1452 | u = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1453 | err = assign_slice(u, v, w, (PyObject *)NULL); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1454 | /* del u[v:w] */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1455 | Py_DECREF(u); |
| 1456 | Py_XDECREF(v); |
| 1457 | Py_XDECREF(w); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1458 | if (err == 0) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1459 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1460 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1461 | case STORE_SUBSCR: |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1462 | w = TOP(); |
| 1463 | v = SECOND(); |
| 1464 | u = THIRD(); |
| 1465 | STACKADJ(-3); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1466 | /* v[w] = u */ |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1467 | err = PyObject_SetItem(v, w, u); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1468 | Py_DECREF(u); |
| 1469 | Py_DECREF(v); |
| 1470 | Py_DECREF(w); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1471 | if (err == 0) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1472 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1473 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1474 | case DELETE_SUBSCR: |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1475 | w = TOP(); |
| 1476 | v = SECOND(); |
| 1477 | STACKADJ(-2); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1478 | /* del v[w] */ |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1479 | err = PyObject_DelItem(v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1480 | Py_DECREF(v); |
| 1481 | Py_DECREF(w); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1482 | if (err == 0) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1483 | break; |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1484 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1485 | case PRINT_EXPR: |
| 1486 | v = POP(); |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 1487 | w = PySys_GetObject("displayhook"); |
| 1488 | if (w == NULL) { |
| 1489 | PyErr_SetString(PyExc_RuntimeError, |
| 1490 | "lost sys.displayhook"); |
| 1491 | err = -1; |
Moshe Zadka | f5df383 | 2001-01-11 11:55:37 +0000 | [diff] [blame] | 1492 | x = NULL; |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 1493 | } |
| 1494 | if (err == 0) { |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 1495 | x = PyTuple_Pack(1, v); |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 1496 | if (x == NULL) |
| 1497 | err = -1; |
| 1498 | } |
| 1499 | if (err == 0) { |
| 1500 | w = PyEval_CallObject(w, x); |
Moshe Zadka | f5df383 | 2001-01-11 11:55:37 +0000 | [diff] [blame] | 1501 | Py_XDECREF(w); |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 1502 | if (w == NULL) |
| 1503 | err = -1; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1504 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1505 | Py_DECREF(v); |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 1506 | Py_XDECREF(x); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1507 | break; |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 1508 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1509 | #ifdef CASE_TOO_BIG |
| 1510 | default: switch (opcode) { |
| 1511 | #endif |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1512 | case RAISE_VARARGS: |
| 1513 | u = v = w = NULL; |
| 1514 | switch (oparg) { |
| 1515 | case 3: |
| 1516 | u = POP(); /* traceback */ |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1517 | /* Fallthrough */ |
| 1518 | case 2: |
| 1519 | v = POP(); /* value */ |
| 1520 | /* Fallthrough */ |
| 1521 | case 1: |
| 1522 | w = POP(); /* exc */ |
Guido van Rossum | d295f12 | 1998-04-09 21:39:57 +0000 | [diff] [blame] | 1523 | case 0: /* Fallthrough */ |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 1524 | why = do_raise(w, v, u); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1525 | break; |
| 1526 | default: |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1527 | PyErr_SetString(PyExc_SystemError, |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1528 | "bad RAISE_VARARGS oparg"); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1529 | why = WHY_EXCEPTION; |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 1530 | break; |
| 1531 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1532 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1533 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 1534 | case STORE_LOCALS: |
| 1535 | x = POP(); |
| 1536 | v = f->f_locals; |
| 1537 | Py_XDECREF(v); |
| 1538 | f->f_locals = x; |
| 1539 | continue; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1540 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1541 | case RETURN_VALUE: |
| 1542 | retval = POP(); |
| 1543 | why = WHY_RETURN; |
Raymond Hettinger | 1dd8309 | 2004-02-06 18:32:33 +0000 | [diff] [blame] | 1544 | goto fast_block_end; |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 1545 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1546 | case YIELD_VALUE: |
| 1547 | retval = POP(); |
Tim Peters | 8c96369 | 2001-06-23 05:26:56 +0000 | [diff] [blame] | 1548 | f->f_stacktop = stack_pointer; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1549 | why = WHY_YIELD; |
Raymond Hettinger | 1dd8309 | 2004-02-06 18:32:33 +0000 | [diff] [blame] | 1550 | goto fast_yield; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1551 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1552 | case POP_BLOCK: |
| 1553 | { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1554 | PyTryBlock *b = PyFrame_BlockPop(f); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1555 | while (STACK_LEVEL() > b->b_level) { |
| 1556 | v = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1557 | Py_DECREF(v); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1558 | } |
| 1559 | } |
Raymond Hettinger | 7eddd78 | 2004-04-07 14:38:08 +0000 | [diff] [blame] | 1560 | continue; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1561 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1562 | case END_FINALLY: |
| 1563 | v = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1564 | if (PyInt_Check(v)) { |
Raymond Hettinger | 7c95865 | 2004-04-06 10:11:10 +0000 | [diff] [blame] | 1565 | why = (enum why_code) PyInt_AS_LONG(v); |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 1566 | assert(why != WHY_YIELD); |
Raymond Hettinger | c8aa08b | 2004-04-11 14:59:33 +0000 | [diff] [blame] | 1567 | if (why == WHY_RETURN || |
| 1568 | why == WHY_CONTINUE) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1569 | retval = POP(); |
| 1570 | } |
Brett Cannon | f74225d | 2007-02-26 21:10:16 +0000 | [diff] [blame] | 1571 | else if (PyExceptionClass_Check(v)) { |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1572 | w = POP(); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1573 | u = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1574 | PyErr_Restore(v, w, u); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1575 | why = WHY_RERAISE; |
Guido van Rossum | 0db1ef9 | 1995-07-28 23:06:00 +0000 | [diff] [blame] | 1576 | break; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1577 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1578 | else if (v != Py_None) { |
| 1579 | PyErr_SetString(PyExc_SystemError, |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1580 | "'finally' pops bad exception"); |
| 1581 | why = WHY_EXCEPTION; |
| 1582 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1583 | Py_DECREF(v); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1584 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1585 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 1586 | case LOAD_BUILD_CLASS: |
| 1587 | x = PyDict_GetItemString(f->f_builtins, |
| 1588 | "__build_class__"); |
| 1589 | if (x == NULL) { |
| 1590 | PyErr_SetString(PyExc_ImportError, |
| 1591 | "__build_class__ not found"); |
| 1592 | break; |
| 1593 | } |
| 1594 | Py_INCREF(x); |
| 1595 | PUSH(x); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1596 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1597 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1598 | case STORE_NAME: |
Skip Montanaro | 496e658 | 2002-08-06 17:47:40 +0000 | [diff] [blame] | 1599 | w = GETITEM(names, oparg); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1600 | v = POP(); |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 1601 | if ((x = f->f_locals) != NULL) { |
Raymond Hettinger | 66bd233 | 2004-08-02 08:30:07 +0000 | [diff] [blame] | 1602 | if (PyDict_CheckExact(x)) |
Raymond Hettinger | 214b1c3 | 2004-07-02 06:41:07 +0000 | [diff] [blame] | 1603 | err = PyDict_SetItem(x, w, v); |
| 1604 | else |
| 1605 | err = PyObject_SetItem(x, w, v); |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 1606 | Py_DECREF(v); |
Raymond Hettinger | 7eddd78 | 2004-04-07 14:38:08 +0000 | [diff] [blame] | 1607 | if (err == 0) continue; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1608 | break; |
| 1609 | } |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 1610 | PyErr_Format(PyExc_SystemError, |
| 1611 | "no locals found when storing %s", |
| 1612 | PyObject_REPR(w)); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1613 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1614 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1615 | case DELETE_NAME: |
Skip Montanaro | 496e658 | 2002-08-06 17:47:40 +0000 | [diff] [blame] | 1616 | w = GETITEM(names, oparg); |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 1617 | if ((x = f->f_locals) != NULL) { |
Raymond Hettinger | 214b1c3 | 2004-07-02 06:41:07 +0000 | [diff] [blame] | 1618 | if ((err = PyObject_DelItem(x, w)) != 0) |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 1619 | format_exc_check_arg(PyExc_NameError, |
| 1620 | NAME_ERROR_MSG ,w); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1621 | break; |
| 1622 | } |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 1623 | PyErr_Format(PyExc_SystemError, |
| 1624 | "no locals when deleting %s", |
| 1625 | PyObject_REPR(w)); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1626 | break; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1627 | |
Raymond Hettinger | 7dc5221 | 2003-03-16 20:14:44 +0000 | [diff] [blame] | 1628 | PREDICTED_WITH_ARG(UNPACK_SEQUENCE); |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 1629 | case UNPACK_SEQUENCE: |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1630 | v = POP(); |
Raymond Hettinger | f114a3a | 2004-03-08 23:25:30 +0000 | [diff] [blame] | 1631 | if (PyTuple_CheckExact(v) && PyTuple_GET_SIZE(v) == oparg) { |
| 1632 | PyObject **items = ((PyTupleObject *)v)->ob_item; |
| 1633 | while (oparg--) { |
| 1634 | w = items[oparg]; |
| 1635 | Py_INCREF(w); |
| 1636 | PUSH(w); |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 1637 | } |
Raymond Hettinger | 7eddd78 | 2004-04-07 14:38:08 +0000 | [diff] [blame] | 1638 | Py_DECREF(v); |
| 1639 | continue; |
Raymond Hettinger | f114a3a | 2004-03-08 23:25:30 +0000 | [diff] [blame] | 1640 | } else if (PyList_CheckExact(v) && PyList_GET_SIZE(v) == oparg) { |
| 1641 | PyObject **items = ((PyListObject *)v)->ob_item; |
| 1642 | while (oparg--) { |
| 1643 | w = items[oparg]; |
| 1644 | Py_INCREF(w); |
| 1645 | PUSH(w); |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 1646 | } |
Raymond Hettinger | f114a3a | 2004-03-08 23:25:30 +0000 | [diff] [blame] | 1647 | } else if (unpack_iterable(v, oparg, |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 1648 | stack_pointer + oparg)) |
| 1649 | stack_pointer += oparg; |
Tim Peters | 8b13b3e | 2001-09-30 05:58:42 +0000 | [diff] [blame] | 1650 | else { |
| 1651 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 1652 | PyErr_SetString(PyExc_TypeError, |
| 1653 | "unpack non-sequence"); |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 1654 | why = WHY_EXCEPTION; |
Tim Peters | 8b13b3e | 2001-09-30 05:58:42 +0000 | [diff] [blame] | 1655 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1656 | Py_DECREF(v); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1657 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1658 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1659 | case STORE_ATTR: |
Skip Montanaro | 496e658 | 2002-08-06 17:47:40 +0000 | [diff] [blame] | 1660 | w = GETITEM(names, oparg); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1661 | v = TOP(); |
| 1662 | u = SECOND(); |
| 1663 | STACKADJ(-2); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1664 | err = PyObject_SetAttr(v, w, u); /* v.w = u */ |
| 1665 | Py_DECREF(v); |
| 1666 | Py_DECREF(u); |
Raymond Hettinger | 7eddd78 | 2004-04-07 14:38:08 +0000 | [diff] [blame] | 1667 | if (err == 0) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1668 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1669 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1670 | case DELETE_ATTR: |
Skip Montanaro | 496e658 | 2002-08-06 17:47:40 +0000 | [diff] [blame] | 1671 | w = GETITEM(names, oparg); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1672 | v = POP(); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 1673 | err = PyObject_SetAttr(v, w, (PyObject *)NULL); |
| 1674 | /* del v.w */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1675 | Py_DECREF(v); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1676 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1677 | |
Guido van Rossum | 32c6cdf | 1991-12-10 13:52:46 +0000 | [diff] [blame] | 1678 | case STORE_GLOBAL: |
Skip Montanaro | 496e658 | 2002-08-06 17:47:40 +0000 | [diff] [blame] | 1679 | w = GETITEM(names, oparg); |
Guido van Rossum | 32c6cdf | 1991-12-10 13:52:46 +0000 | [diff] [blame] | 1680 | v = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1681 | err = PyDict_SetItem(f->f_globals, w, v); |
| 1682 | Py_DECREF(v); |
Raymond Hettinger | 7eddd78 | 2004-04-07 14:38:08 +0000 | [diff] [blame] | 1683 | if (err == 0) continue; |
Guido van Rossum | 32c6cdf | 1991-12-10 13:52:46 +0000 | [diff] [blame] | 1684 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1685 | |
Guido van Rossum | 32c6cdf | 1991-12-10 13:52:46 +0000 | [diff] [blame] | 1686 | case DELETE_GLOBAL: |
Skip Montanaro | 496e658 | 2002-08-06 17:47:40 +0000 | [diff] [blame] | 1687 | w = GETITEM(names, oparg); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1688 | if ((err = PyDict_DelItem(f->f_globals, w)) != 0) |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 1689 | format_exc_check_arg( |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1690 | PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w); |
Guido van Rossum | 32c6cdf | 1991-12-10 13:52:46 +0000 | [diff] [blame] | 1691 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1692 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1693 | case LOAD_NAME: |
Skip Montanaro | 496e658 | 2002-08-06 17:47:40 +0000 | [diff] [blame] | 1694 | w = GETITEM(names, oparg); |
Raymond Hettinger | 214b1c3 | 2004-07-02 06:41:07 +0000 | [diff] [blame] | 1695 | if ((v = f->f_locals) == NULL) { |
Jeremy Hylton | c862cf4 | 2001-01-19 03:25:05 +0000 | [diff] [blame] | 1696 | PyErr_Format(PyExc_SystemError, |
| 1697 | "no locals when loading %s", |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 1698 | PyObject_REPR(w)); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1699 | break; |
| 1700 | } |
Michael W. Hudson | a3711f7 | 2004-08-02 14:50:43 +0000 | [diff] [blame] | 1701 | if (PyDict_CheckExact(v)) { |
Raymond Hettinger | 214b1c3 | 2004-07-02 06:41:07 +0000 | [diff] [blame] | 1702 | x = PyDict_GetItem(v, w); |
Michael W. Hudson | a3711f7 | 2004-08-02 14:50:43 +0000 | [diff] [blame] | 1703 | Py_XINCREF(x); |
| 1704 | } |
Raymond Hettinger | 214b1c3 | 2004-07-02 06:41:07 +0000 | [diff] [blame] | 1705 | else { |
| 1706 | x = PyObject_GetItem(v, w); |
| 1707 | if (x == NULL && PyErr_Occurred()) { |
| 1708 | if (!PyErr_ExceptionMatches(PyExc_KeyError)) |
| 1709 | break; |
| 1710 | PyErr_Clear(); |
| 1711 | } |
| 1712 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1713 | if (x == NULL) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1714 | x = PyDict_GetItem(f->f_globals, w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1715 | if (x == NULL) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1716 | x = PyDict_GetItem(f->f_builtins, w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1717 | if (x == NULL) { |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 1718 | format_exc_check_arg( |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1719 | PyExc_NameError, |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 1720 | NAME_ERROR_MSG ,w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1721 | break; |
| 1722 | } |
| 1723 | } |
Michael W. Hudson | a3711f7 | 2004-08-02 14:50:43 +0000 | [diff] [blame] | 1724 | Py_INCREF(x); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1725 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1726 | PUSH(x); |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 1727 | continue; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1728 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1729 | case LOAD_GLOBAL: |
Skip Montanaro | 496e658 | 2002-08-06 17:47:40 +0000 | [diff] [blame] | 1730 | w = GETITEM(names, oparg); |
Guido van Rossum | 3a4dfc8 | 2002-08-19 20:24:07 +0000 | [diff] [blame] | 1731 | if (PyString_CheckExact(w)) { |
Guido van Rossum | d8dbf84 | 2002-08-19 21:17:53 +0000 | [diff] [blame] | 1732 | /* Inline the PyDict_GetItem() calls. |
| 1733 | WARNING: this is an extreme speed hack. |
| 1734 | Do not try this at home. */ |
Guido van Rossum | 3a4dfc8 | 2002-08-19 20:24:07 +0000 | [diff] [blame] | 1735 | long hash = ((PyStringObject *)w)->ob_shash; |
| 1736 | if (hash != -1) { |
Guido van Rossum | 3a4dfc8 | 2002-08-19 20:24:07 +0000 | [diff] [blame] | 1737 | PyDictObject *d; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1738 | PyDictEntry *e; |
Guido van Rossum | 3a4dfc8 | 2002-08-19 20:24:07 +0000 | [diff] [blame] | 1739 | d = (PyDictObject *)(f->f_globals); |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1740 | e = d->ma_lookup(d, w, hash); |
| 1741 | if (e == NULL) { |
| 1742 | x = NULL; |
| 1743 | break; |
| 1744 | } |
| 1745 | x = e->me_value; |
Guido van Rossum | 3a4dfc8 | 2002-08-19 20:24:07 +0000 | [diff] [blame] | 1746 | if (x != NULL) { |
| 1747 | Py_INCREF(x); |
| 1748 | PUSH(x); |
| 1749 | continue; |
| 1750 | } |
| 1751 | d = (PyDictObject *)(f->f_builtins); |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1752 | e = d->ma_lookup(d, w, hash); |
| 1753 | if (e == NULL) { |
| 1754 | x = NULL; |
| 1755 | break; |
| 1756 | } |
| 1757 | x = e->me_value; |
Guido van Rossum | 3a4dfc8 | 2002-08-19 20:24:07 +0000 | [diff] [blame] | 1758 | if (x != NULL) { |
| 1759 | Py_INCREF(x); |
| 1760 | PUSH(x); |
| 1761 | continue; |
| 1762 | } |
| 1763 | goto load_global_error; |
| 1764 | } |
| 1765 | } |
| 1766 | /* This is the un-inlined version of the code above */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1767 | x = PyDict_GetItem(f->f_globals, w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1768 | if (x == NULL) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1769 | x = PyDict_GetItem(f->f_builtins, w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1770 | if (x == NULL) { |
Guido van Rossum | 3a4dfc8 | 2002-08-19 20:24:07 +0000 | [diff] [blame] | 1771 | load_global_error: |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 1772 | format_exc_check_arg( |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1773 | PyExc_NameError, |
Guido van Rossum | 3a4dfc8 | 2002-08-19 20:24:07 +0000 | [diff] [blame] | 1774 | GLOBAL_NAME_ERROR_MSG, w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1775 | break; |
| 1776 | } |
| 1777 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1778 | Py_INCREF(x); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1779 | PUSH(x); |
Raymond Hettinger | 7eddd78 | 2004-04-07 14:38:08 +0000 | [diff] [blame] | 1780 | continue; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1781 | |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 1782 | case DELETE_FAST: |
Guido van Rossum | 2e4c899 | 1998-05-12 20:27:36 +0000 | [diff] [blame] | 1783 | x = GETLOCAL(oparg); |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 1784 | if (x != NULL) { |
| 1785 | SETLOCAL(oparg, NULL); |
| 1786 | continue; |
Guido van Rossum | 2e4c899 | 1998-05-12 20:27:36 +0000 | [diff] [blame] | 1787 | } |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 1788 | format_exc_check_arg( |
| 1789 | PyExc_UnboundLocalError, |
| 1790 | UNBOUNDLOCAL_ERROR_MSG, |
| 1791 | PyTuple_GetItem(co->co_varnames, oparg) |
| 1792 | ); |
| 1793 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1794 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1795 | case LOAD_CLOSURE: |
Jeremy Hylton | 2b724da | 2001-01-29 22:51:52 +0000 | [diff] [blame] | 1796 | x = freevars[oparg]; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1797 | Py_INCREF(x); |
| 1798 | PUSH(x); |
Raymond Hettinger | 7eddd78 | 2004-04-07 14:38:08 +0000 | [diff] [blame] | 1799 | if (x != NULL) continue; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1800 | break; |
| 1801 | |
| 1802 | case LOAD_DEREF: |
Jeremy Hylton | 2b724da | 2001-01-29 22:51:52 +0000 | [diff] [blame] | 1803 | x = freevars[oparg]; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1804 | w = PyCell_Get(x); |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 1805 | if (w != NULL) { |
| 1806 | PUSH(w); |
| 1807 | continue; |
Jeremy Hylton | 2524d69 | 2001-02-05 17:23:16 +0000 | [diff] [blame] | 1808 | } |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 1809 | err = -1; |
| 1810 | /* Don't stomp existing exception */ |
| 1811 | if (PyErr_Occurred()) |
| 1812 | break; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1813 | if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) { |
| 1814 | v = PyTuple_GET_ITEM(co->co_cellvars, |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 1815 | oparg); |
| 1816 | format_exc_check_arg( |
| 1817 | PyExc_UnboundLocalError, |
| 1818 | UNBOUNDLOCAL_ERROR_MSG, |
| 1819 | v); |
| 1820 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1821 | v = PyTuple_GET_ITEM( |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 1822 | co->co_freevars, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1823 | oparg - PyTuple_GET_SIZE(co->co_cellvars)); |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 1824 | format_exc_check_arg( |
| 1825 | PyExc_NameError, |
| 1826 | UNBOUNDFREE_ERROR_MSG, |
| 1827 | v); |
| 1828 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1829 | break; |
| 1830 | |
| 1831 | case STORE_DEREF: |
| 1832 | w = POP(); |
Jeremy Hylton | 2b724da | 2001-01-29 22:51:52 +0000 | [diff] [blame] | 1833 | x = freevars[oparg]; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1834 | PyCell_Set(x, w); |
Jeremy Hylton | 30c9f39 | 2001-03-13 01:58:22 +0000 | [diff] [blame] | 1835 | Py_DECREF(w); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1836 | continue; |
| 1837 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1838 | case BUILD_TUPLE: |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1839 | x = PyTuple_New(oparg); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1840 | if (x != NULL) { |
Raymond Hettinger | 5bed456 | 2004-04-10 23:34:17 +0000 | [diff] [blame] | 1841 | for (; --oparg >= 0;) { |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1842 | w = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1843 | PyTuple_SET_ITEM(x, oparg, w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1844 | } |
| 1845 | PUSH(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1846 | continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1847 | } |
| 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 BUILD_LIST: |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1851 | x = PyList_New(oparg); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1852 | if (x != NULL) { |
Raymond Hettinger | 5bed456 | 2004-04-10 23:34:17 +0000 | [diff] [blame] | 1853 | for (; --oparg >= 0;) { |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1854 | w = POP(); |
Guido van Rossum | 5053efc | 1998-08-04 15:27:50 +0000 | [diff] [blame] | 1855 | PyList_SET_ITEM(x, oparg, w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1856 | } |
| 1857 | PUSH(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1858 | continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1859 | } |
| 1860 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1861 | |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 1862 | case BUILD_SET: |
| 1863 | x = PySet_New(NULL); |
| 1864 | if (x != NULL) { |
| 1865 | for (; --oparg >= 0;) { |
| 1866 | w = POP(); |
| 1867 | if (err == 0) |
| 1868 | err = PySet_Add(x, w); |
| 1869 | Py_DECREF(w); |
| 1870 | } |
| 1871 | if (err != 0) { |
| 1872 | Py_DECREF(x); |
| 1873 | break; |
| 1874 | } |
| 1875 | PUSH(x); |
| 1876 | continue; |
| 1877 | } |
| 1878 | break; |
| 1879 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1880 | case BUILD_MAP: |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1881 | x = PyDict_New(); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1882 | PUSH(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1883 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1884 | break; |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 1885 | |
| 1886 | case MAKE_BYTES: |
| 1887 | w = POP(); |
| 1888 | if (PyString_Check(w)) |
| 1889 | x = PyBytes_FromStringAndSize( |
| 1890 | PyString_AS_STRING(w), |
| 1891 | PyString_GET_SIZE(w)); |
| 1892 | else |
| 1893 | x = NULL; |
| 1894 | Py_DECREF(w); |
| 1895 | PUSH(x); |
| 1896 | if (x != NULL) continue; |
| 1897 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1898 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1899 | case LOAD_ATTR: |
Skip Montanaro | 496e658 | 2002-08-06 17:47:40 +0000 | [diff] [blame] | 1900 | w = GETITEM(names, oparg); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1901 | v = TOP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1902 | x = PyObject_GetAttr(v, w); |
| 1903 | Py_DECREF(v); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1904 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1905 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1906 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1907 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1908 | case COMPARE_OP: |
| 1909 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1910 | v = TOP(); |
Raymond Hettinger | 4bad9ba | 2003-01-19 05:08:13 +0000 | [diff] [blame] | 1911 | if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) { |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1912 | /* INLINE: cmp(int, int) */ |
| 1913 | register long a, b; |
| 1914 | register int res; |
Guido van Rossum | cf183ac | 1998-12-04 18:51:36 +0000 | [diff] [blame] | 1915 | a = PyInt_AS_LONG(v); |
| 1916 | b = PyInt_AS_LONG(w); |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1917 | switch (oparg) { |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 1918 | case PyCmp_LT: res = a < b; break; |
| 1919 | case PyCmp_LE: res = a <= b; break; |
| 1920 | case PyCmp_EQ: res = a == b; break; |
| 1921 | case PyCmp_NE: res = a != b; break; |
| 1922 | case PyCmp_GT: res = a > b; break; |
| 1923 | case PyCmp_GE: res = a >= b; break; |
| 1924 | case PyCmp_IS: res = v == w; break; |
| 1925 | case PyCmp_IS_NOT: res = v != w; break; |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1926 | default: goto slow_compare; |
| 1927 | } |
| 1928 | x = res ? Py_True : Py_False; |
| 1929 | Py_INCREF(x); |
| 1930 | } |
| 1931 | else { |
| 1932 | slow_compare: |
| 1933 | x = cmp_outcome(oparg, v, w); |
| 1934 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1935 | Py_DECREF(v); |
| 1936 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1937 | SET_TOP(x); |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 1938 | if (x == NULL) break; |
| 1939 | PREDICT(JUMP_IF_FALSE); |
| 1940 | PREDICT(JUMP_IF_TRUE); |
| 1941 | continue; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1942 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1943 | case IMPORT_NAME: |
Skip Montanaro | 496e658 | 2002-08-06 17:47:40 +0000 | [diff] [blame] | 1944 | w = GETITEM(names, oparg); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1945 | x = PyDict_GetItemString(f->f_builtins, "__import__"); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1946 | if (x == NULL) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1947 | PyErr_SetString(PyExc_ImportError, |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1948 | "__import__ not found"); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1949 | break; |
| 1950 | } |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 1951 | v = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1952 | u = TOP(); |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 1953 | if (PyInt_AsLong(u) != -1 || PyErr_Occurred()) |
| 1954 | w = PyTuple_Pack(5, |
| 1955 | w, |
| 1956 | f->f_globals, |
| 1957 | f->f_locals == NULL ? |
| 1958 | Py_None : f->f_locals, |
| 1959 | v, |
| 1960 | u); |
| 1961 | else |
| 1962 | w = PyTuple_Pack(4, |
| 1963 | w, |
| 1964 | f->f_globals, |
| 1965 | f->f_locals == NULL ? |
| 1966 | Py_None : f->f_locals, |
| 1967 | v); |
| 1968 | Py_DECREF(v); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1969 | Py_DECREF(u); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1970 | if (w == NULL) { |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1971 | u = POP(); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1972 | x = NULL; |
| 1973 | break; |
| 1974 | } |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 1975 | READ_TIMESTAMP(intr0); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1976 | x = PyEval_CallObject(x, w); |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 1977 | READ_TIMESTAMP(intr1); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1978 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1979 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1980 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1981 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1982 | |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 1983 | case IMPORT_STAR: |
| 1984 | v = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1985 | PyFrame_FastToLocals(f); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1986 | if ((x = f->f_locals) == NULL) { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 1987 | PyErr_SetString(PyExc_SystemError, |
Jeremy Hylton | c862cf4 | 2001-01-19 03:25:05 +0000 | [diff] [blame] | 1988 | "no locals found during 'import *'"); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1989 | break; |
| 1990 | } |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 1991 | READ_TIMESTAMP(intr0); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 1992 | err = import_all_from(x, v); |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 1993 | READ_TIMESTAMP(intr1); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1994 | PyFrame_LocalsToFast(f, 0); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 1995 | Py_DECREF(v); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1996 | if (err == 0) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1997 | break; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1998 | |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 1999 | case IMPORT_FROM: |
Skip Montanaro | 496e658 | 2002-08-06 17:47:40 +0000 | [diff] [blame] | 2000 | w = GETITEM(names, oparg); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2001 | v = TOP(); |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 2002 | READ_TIMESTAMP(intr0); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2003 | x = import_from(v, w); |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 2004 | READ_TIMESTAMP(intr1); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2005 | PUSH(x); |
| 2006 | if (x != NULL) continue; |
| 2007 | break; |
| 2008 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2009 | case JUMP_FORWARD: |
| 2010 | JUMPBY(oparg); |
Neil Schemenauer | c4b570f | 2003-06-01 19:21:12 +0000 | [diff] [blame] | 2011 | goto fast_next_opcode; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2012 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 2013 | PREDICTED_WITH_ARG(JUMP_IF_FALSE); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2014 | case JUMP_IF_FALSE: |
Raymond Hettinger | 21012b8 | 2003-02-26 18:11:50 +0000 | [diff] [blame] | 2015 | w = TOP(); |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 2016 | if (w == Py_True) { |
| 2017 | PREDICT(POP_TOP); |
Neil Schemenauer | c4b570f | 2003-06-01 19:21:12 +0000 | [diff] [blame] | 2018 | goto fast_next_opcode; |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 2019 | } |
Raymond Hettinger | 21012b8 | 2003-02-26 18:11:50 +0000 | [diff] [blame] | 2020 | if (w == Py_False) { |
| 2021 | JUMPBY(oparg); |
Neil Schemenauer | c4b570f | 2003-06-01 19:21:12 +0000 | [diff] [blame] | 2022 | goto fast_next_opcode; |
Raymond Hettinger | 21012b8 | 2003-02-26 18:11:50 +0000 | [diff] [blame] | 2023 | } |
| 2024 | err = PyObject_IsTrue(w); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 2025 | if (err > 0) |
| 2026 | err = 0; |
| 2027 | else if (err == 0) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2028 | JUMPBY(oparg); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 2029 | else |
| 2030 | break; |
| 2031 | continue; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2032 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 2033 | PREDICTED_WITH_ARG(JUMP_IF_TRUE); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2034 | case JUMP_IF_TRUE: |
Raymond Hettinger | 21012b8 | 2003-02-26 18:11:50 +0000 | [diff] [blame] | 2035 | w = TOP(); |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 2036 | if (w == Py_False) { |
| 2037 | PREDICT(POP_TOP); |
Neil Schemenauer | c4b570f | 2003-06-01 19:21:12 +0000 | [diff] [blame] | 2038 | goto fast_next_opcode; |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 2039 | } |
Raymond Hettinger | 21012b8 | 2003-02-26 18:11:50 +0000 | [diff] [blame] | 2040 | if (w == Py_True) { |
| 2041 | JUMPBY(oparg); |
Neil Schemenauer | c4b570f | 2003-06-01 19:21:12 +0000 | [diff] [blame] | 2042 | goto fast_next_opcode; |
Raymond Hettinger | 21012b8 | 2003-02-26 18:11:50 +0000 | [diff] [blame] | 2043 | } |
| 2044 | err = PyObject_IsTrue(w); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 2045 | if (err > 0) { |
| 2046 | err = 0; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2047 | JUMPBY(oparg); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 2048 | } |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 2049 | else if (err == 0) |
| 2050 | ; |
| 2051 | else |
| 2052 | break; |
| 2053 | continue; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2054 | |
Raymond Hettinger | fba1cfc | 2004-03-12 16:33:17 +0000 | [diff] [blame] | 2055 | PREDICTED_WITH_ARG(JUMP_ABSOLUTE); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2056 | case JUMP_ABSOLUTE: |
| 2057 | JUMPTO(oparg); |
Neil Schemenauer | ca2a2f1 | 2003-05-30 23:59:44 +0000 | [diff] [blame] | 2058 | continue; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2059 | |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2060 | case GET_ITER: |
| 2061 | /* before: [obj]; after [getiter(obj)] */ |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 2062 | v = TOP(); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2063 | x = PyObject_GetIter(v); |
| 2064 | Py_DECREF(v); |
| 2065 | if (x != NULL) { |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 2066 | SET_TOP(x); |
Raymond Hettinger | 7dc5221 | 2003-03-16 20:14:44 +0000 | [diff] [blame] | 2067 | PREDICT(FOR_ITER); |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2068 | continue; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2069 | } |
Raymond Hettinger | 8bb90a5 | 2003-01-14 12:43:10 +0000 | [diff] [blame] | 2070 | STACKADJ(-1); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2071 | break; |
| 2072 | |
Raymond Hettinger | 7dc5221 | 2003-03-16 20:14:44 +0000 | [diff] [blame] | 2073 | PREDICTED_WITH_ARG(FOR_ITER); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2074 | case FOR_ITER: |
| 2075 | /* before: [iter]; after: [iter, iter()] *or* [] */ |
| 2076 | v = TOP(); |
Raymond Hettinger | db0de9e | 2004-03-12 08:41:36 +0000 | [diff] [blame] | 2077 | x = (*v->ob_type->tp_iternext)(v); |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2078 | if (x != NULL) { |
| 2079 | PUSH(x); |
Raymond Hettinger | 7dc5221 | 2003-03-16 20:14:44 +0000 | [diff] [blame] | 2080 | PREDICT(STORE_FAST); |
| 2081 | PREDICT(UNPACK_SEQUENCE); |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2082 | continue; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2083 | } |
Raymond Hettinger | db0de9e | 2004-03-12 08:41:36 +0000 | [diff] [blame] | 2084 | if (PyErr_Occurred()) { |
| 2085 | if (!PyErr_ExceptionMatches(PyExc_StopIteration)) |
| 2086 | break; |
| 2087 | PyErr_Clear(); |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2088 | } |
Raymond Hettinger | db0de9e | 2004-03-12 08:41:36 +0000 | [diff] [blame] | 2089 | /* iterator ended normally */ |
| 2090 | x = v = POP(); |
| 2091 | Py_DECREF(v); |
| 2092 | JUMPBY(oparg); |
| 2093 | continue; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2094 | |
Raymond Hettinger | 2d783e9 | 2004-03-12 09:12:22 +0000 | [diff] [blame] | 2095 | case BREAK_LOOP: |
| 2096 | why = WHY_BREAK; |
| 2097 | goto fast_block_end; |
| 2098 | |
| 2099 | case CONTINUE_LOOP: |
| 2100 | retval = PyInt_FromLong(oparg); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2101 | if (!retval) { |
| 2102 | x = NULL; |
| 2103 | break; |
| 2104 | } |
Raymond Hettinger | 2d783e9 | 2004-03-12 09:12:22 +0000 | [diff] [blame] | 2105 | why = WHY_CONTINUE; |
| 2106 | goto fast_block_end; |
| 2107 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2108 | case SETUP_LOOP: |
| 2109 | case SETUP_EXCEPT: |
| 2110 | case SETUP_FINALLY: |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 2111 | /* NOTE: If you add any new block-setup opcodes that are |
| 2112 | not try/except/finally handlers, you may need to |
| 2113 | update the PyGen_NeedsFinalizing() function. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2114 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2115 | PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg, |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 2116 | STACK_LEVEL()); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 2117 | continue; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2118 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 2119 | case WITH_CLEANUP: |
| 2120 | { |
| 2121 | /* TOP is the context.__exit__ bound method. |
| 2122 | Below that are 1-3 values indicating how/why |
| 2123 | we entered the finally clause: |
| 2124 | - SECOND = None |
Guido van Rossum | f669436 | 2006-03-10 02:28:35 +0000 | [diff] [blame] | 2125 | - (SECOND, THIRD) = (WHY_{RETURN,CONTINUE}), retval |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 2126 | - SECOND = WHY_*; no retval below it |
| 2127 | - (SECOND, THIRD, FOURTH) = exc_info() |
| 2128 | In the last case, we must call |
| 2129 | TOP(SECOND, THIRD, FOURTH) |
| 2130 | otherwise we must call |
| 2131 | TOP(None, None, None) |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 2132 | |
| 2133 | In addition, if the stack represents an exception, |
Guido van Rossum | f669436 | 2006-03-10 02:28:35 +0000 | [diff] [blame] | 2134 | *and* the function call returns a 'true' value, we |
| 2135 | "zap" this information, to prevent END_FINALLY from |
| 2136 | re-raising the exception. (But non-local gotos |
| 2137 | should still be resumed.) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 2138 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2139 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 2140 | x = TOP(); |
| 2141 | u = SECOND(); |
| 2142 | if (PyInt_Check(u) || u == Py_None) { |
| 2143 | u = v = w = Py_None; |
| 2144 | } |
| 2145 | else { |
| 2146 | v = THIRD(); |
| 2147 | w = FOURTH(); |
| 2148 | } |
Guido van Rossum | f669436 | 2006-03-10 02:28:35 +0000 | [diff] [blame] | 2149 | /* XXX Not the fastest way to call it... */ |
| 2150 | x = PyObject_CallFunctionObjArgs(x, u, v, w, NULL); |
| 2151 | if (x == NULL) |
| 2152 | break; /* Go to error exit */ |
| 2153 | if (u != Py_None && PyObject_IsTrue(x)) { |
| 2154 | /* There was an exception and a true return */ |
| 2155 | Py_DECREF(x); |
| 2156 | x = TOP(); /* Again */ |
| 2157 | STACKADJ(-3); |
| 2158 | Py_INCREF(Py_None); |
| 2159 | SET_TOP(Py_None); |
| 2160 | Py_DECREF(x); |
| 2161 | Py_DECREF(u); |
| 2162 | Py_DECREF(v); |
| 2163 | Py_DECREF(w); |
| 2164 | } else { |
| 2165 | /* Let END_FINALLY do its thing */ |
| 2166 | Py_DECREF(x); |
| 2167 | x = POP(); |
| 2168 | Py_DECREF(x); |
| 2169 | } |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 2170 | break; |
| 2171 | } |
| 2172 | |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 2173 | case CALL_FUNCTION: |
Armin Rigo | 8817fcd | 2004-06-17 10:22:40 +0000 | [diff] [blame] | 2174 | { |
| 2175 | PyObject **sp; |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 2176 | PCALL(PCALL_ALL); |
Armin Rigo | 8817fcd | 2004-06-17 10:22:40 +0000 | [diff] [blame] | 2177 | sp = stack_pointer; |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 2178 | #ifdef WITH_TSC |
Armin Rigo | 8817fcd | 2004-06-17 10:22:40 +0000 | [diff] [blame] | 2179 | x = call_function(&sp, oparg, &intr0, &intr1); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 2180 | #else |
Armin Rigo | 8817fcd | 2004-06-17 10:22:40 +0000 | [diff] [blame] | 2181 | x = call_function(&sp, oparg); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 2182 | #endif |
Armin Rigo | 8817fcd | 2004-06-17 10:22:40 +0000 | [diff] [blame] | 2183 | stack_pointer = sp; |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 2184 | PUSH(x); |
| 2185 | if (x != NULL) |
| 2186 | continue; |
| 2187 | break; |
Armin Rigo | 8817fcd | 2004-06-17 10:22:40 +0000 | [diff] [blame] | 2188 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2189 | |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 2190 | case CALL_FUNCTION_VAR: |
| 2191 | case CALL_FUNCTION_KW: |
| 2192 | case CALL_FUNCTION_VAR_KW: |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 2193 | { |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 2194 | int na = oparg & 0xff; |
| 2195 | int nk = (oparg>>8) & 0xff; |
| 2196 | int flags = (opcode - CALL_FUNCTION) & 3; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 2197 | int n = na + 2 * nk; |
Armin Rigo | 8817fcd | 2004-06-17 10:22:40 +0000 | [diff] [blame] | 2198 | PyObject **pfunc, *func, **sp; |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 2199 | PCALL(PCALL_ALL); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 2200 | if (flags & CALL_FLAG_VAR) |
| 2201 | n++; |
| 2202 | if (flags & CALL_FLAG_KW) |
| 2203 | n++; |
| 2204 | pfunc = stack_pointer - n - 1; |
| 2205 | func = *pfunc; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 2206 | |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2207 | if (PyMethod_Check(func) |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 2208 | && PyMethod_GET_SELF(func) != NULL) { |
| 2209 | PyObject *self = PyMethod_GET_SELF(func); |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 2210 | Py_INCREF(self); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 2211 | func = PyMethod_GET_FUNCTION(func); |
| 2212 | Py_INCREF(func); |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 2213 | Py_DECREF(*pfunc); |
| 2214 | *pfunc = self; |
| 2215 | na++; |
| 2216 | n++; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2217 | } else |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 2218 | Py_INCREF(func); |
Armin Rigo | 8817fcd | 2004-06-17 10:22:40 +0000 | [diff] [blame] | 2219 | sp = stack_pointer; |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 2220 | READ_TIMESTAMP(intr0); |
Armin Rigo | 8817fcd | 2004-06-17 10:22:40 +0000 | [diff] [blame] | 2221 | x = ext_do_call(func, &sp, flags, na, nk); |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 2222 | READ_TIMESTAMP(intr1); |
Armin Rigo | 8817fcd | 2004-06-17 10:22:40 +0000 | [diff] [blame] | 2223 | stack_pointer = sp; |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 2224 | Py_DECREF(func); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 2225 | |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 2226 | while (stack_pointer > pfunc) { |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 2227 | w = POP(); |
| 2228 | Py_DECREF(w); |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 2229 | } |
| 2230 | PUSH(x); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2231 | if (x != NULL) |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 2232 | continue; |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 2233 | break; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 2234 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2235 | |
Guido van Rossum | 0240b92 | 2007-02-26 21:23:50 +0000 | [diff] [blame] | 2236 | case MAKE_CLOSURE: |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2237 | case MAKE_FUNCTION: |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2238 | { |
| 2239 | int posdefaults = oparg & 0xff; |
| 2240 | int kwdefaults = (oparg>>8) & 0xff; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2241 | int num_annotations = (oparg >> 16) & 0x7fff; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2242 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2243 | v = POP(); /* code object */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2244 | x = PyFunction_New(v, f->f_globals); |
| 2245 | Py_DECREF(v); |
Guido van Rossum | 0240b92 | 2007-02-26 21:23:50 +0000 | [diff] [blame] | 2246 | |
| 2247 | if (x != NULL && opcode == MAKE_CLOSURE) { |
| 2248 | v = POP(); |
| 2249 | err = PyFunction_SetClosure(x, v); |
| 2250 | Py_DECREF(v); |
| 2251 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2252 | |
| 2253 | if (x != NULL && num_annotations > 0) { |
| 2254 | Py_ssize_t name_ix; |
| 2255 | u = POP(); /* names of args with annotations */ |
| 2256 | v = PyDict_New(); |
| 2257 | if (v == NULL) { |
| 2258 | Py_DECREF(x); |
| 2259 | x = NULL; |
| 2260 | break; |
| 2261 | } |
| 2262 | name_ix = PyTuple_Size(u); |
| 2263 | assert(num_annotations == name_ix+1); |
| 2264 | while (name_ix > 0) { |
| 2265 | --name_ix; |
| 2266 | t = PyTuple_GET_ITEM(u, name_ix); |
| 2267 | w = POP(); |
| 2268 | /* XXX(nnorwitz): check for errors */ |
| 2269 | PyDict_SetItem(v, t, w); |
| 2270 | Py_DECREF(w); |
| 2271 | } |
| 2272 | |
| 2273 | err = PyFunction_SetAnnotations(x, v); |
| 2274 | Py_DECREF(v); |
| 2275 | Py_DECREF(u); |
| 2276 | } |
| 2277 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2278 | /* XXX Maybe this should be a separate opcode? */ |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2279 | if (x != NULL && posdefaults > 0) { |
| 2280 | v = PyTuple_New(posdefaults); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2281 | if (v == NULL) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2282 | Py_DECREF(x); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2283 | x = NULL; |
| 2284 | break; |
| 2285 | } |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2286 | while (--posdefaults >= 0) { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2287 | w = POP(); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2288 | PyTuple_SET_ITEM(v, posdefaults, w); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2289 | } |
| 2290 | err = PyFunction_SetDefaults(x, v); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2291 | Py_DECREF(v); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2292 | } |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2293 | if (x != NULL && kwdefaults > 0) { |
| 2294 | v = PyDict_New(); |
| 2295 | if (v == NULL) { |
| 2296 | Py_DECREF(x); |
| 2297 | x = NULL; |
| 2298 | break; |
| 2299 | } |
| 2300 | while (--kwdefaults >= 0) { |
| 2301 | w = POP(); /* default value */ |
| 2302 | u = POP(); /* kw only arg name */ |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2303 | /* XXX(nnorwitz): check for errors */ |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2304 | PyDict_SetItem(v, u, w); |
Georg Brandl | 94ab000 | 2007-02-26 13:58:18 +0000 | [diff] [blame] | 2305 | Py_DECREF(w); |
| 2306 | Py_DECREF(u); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2307 | } |
| 2308 | err = PyFunction_SetKwDefaults(x, v); |
| 2309 | Py_DECREF(v); |
| 2310 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2311 | PUSH(x); |
| 2312 | break; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2313 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2314 | |
| 2315 | case BUILD_SLICE: |
| 2316 | if (oparg == 3) |
| 2317 | w = POP(); |
| 2318 | else |
| 2319 | w = NULL; |
| 2320 | v = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 2321 | u = TOP(); |
Guido van Rossum | 1aa1483 | 1997-01-21 05:34:20 +0000 | [diff] [blame] | 2322 | x = PySlice_New(u, v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2323 | Py_DECREF(u); |
| 2324 | Py_DECREF(v); |
| 2325 | Py_XDECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 2326 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 2327 | if (x != NULL) continue; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2328 | break; |
| 2329 | |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 2330 | case EXTENDED_ARG: |
| 2331 | opcode = NEXTOP(); |
Raymond Hettinger | 5bed456 | 2004-04-10 23:34:17 +0000 | [diff] [blame] | 2332 | oparg = oparg<<16 | NEXTARG(); |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 2333 | goto dispatch_opcode; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2334 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2335 | default: |
| 2336 | fprintf(stderr, |
| 2337 | "XXX lineno: %d, opcode: %d\n", |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 2338 | PyCode_Addr2Line(f->f_code, f->f_lasti), |
| 2339 | opcode); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2340 | PyErr_SetString(PyExc_SystemError, "unknown opcode"); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2341 | why = WHY_EXCEPTION; |
| 2342 | break; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 2343 | |
| 2344 | #ifdef CASE_TOO_BIG |
| 2345 | } |
| 2346 | #endif |
| 2347 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2348 | } /* switch */ |
| 2349 | |
| 2350 | on_error: |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2351 | |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 2352 | READ_TIMESTAMP(inst1); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 2353 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2354 | /* Quickly continue if no error occurred */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2355 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2356 | if (why == WHY_NOT) { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2357 | if (err == 0 && x != NULL) { |
| 2358 | #ifdef CHECKEXC |
Guido van Rossum | eb894eb | 1999-03-09 16:16:45 +0000 | [diff] [blame] | 2359 | /* This check is expensive! */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2360 | if (PyErr_Occurred()) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2361 | fprintf(stderr, |
| 2362 | "XXX undetected error\n"); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 2363 | else { |
| 2364 | #endif |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 2365 | READ_TIMESTAMP(loop1); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2366 | continue; /* Normal, fast path */ |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 2367 | #ifdef CHECKEXC |
| 2368 | } |
| 2369 | #endif |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2370 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2371 | why = WHY_EXCEPTION; |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2372 | x = Py_None; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2373 | err = 0; |
| 2374 | } |
| 2375 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2376 | /* Double-check exception status */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2377 | |
Raymond Hettinger | c8aa08b | 2004-04-11 14:59:33 +0000 | [diff] [blame] | 2378 | if (why == WHY_EXCEPTION || why == WHY_RERAISE) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2379 | if (!PyErr_Occurred()) { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2380 | PyErr_SetString(PyExc_SystemError, |
Guido van Rossum | eb894eb | 1999-03-09 16:16:45 +0000 | [diff] [blame] | 2381 | "error return without exception set"); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2382 | why = WHY_EXCEPTION; |
| 2383 | } |
| 2384 | } |
Guido van Rossum | eb894eb | 1999-03-09 16:16:45 +0000 | [diff] [blame] | 2385 | #ifdef CHECKEXC |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2386 | else { |
Guido van Rossum | eb894eb | 1999-03-09 16:16:45 +0000 | [diff] [blame] | 2387 | /* This check is expensive! */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2388 | if (PyErr_Occurred()) { |
Jeremy Hylton | 904ed86 | 2003-11-05 17:29:35 +0000 | [diff] [blame] | 2389 | char buf[1024]; |
| 2390 | sprintf(buf, "Stack unwind with exception " |
| 2391 | "set and why=%d", why); |
| 2392 | Py_FatalError(buf); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2393 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2394 | } |
| 2395 | #endif |
| 2396 | |
| 2397 | /* Log traceback info if this is a real exception */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2398 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2399 | if (why == WHY_EXCEPTION) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2400 | PyTraceBack_Here(f); |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 2401 | |
Fred Drake | 8f51f54 | 2001-10-04 14:48:42 +0000 | [diff] [blame] | 2402 | if (tstate->c_tracefunc != NULL) |
| 2403 | call_exc_trace(tstate->c_tracefunc, |
| 2404 | tstate->c_traceobj, f); |
Guido van Rossum | 014518f | 1998-11-23 21:09:51 +0000 | [diff] [blame] | 2405 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2406 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2407 | /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2408 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2409 | if (why == WHY_RERAISE) |
| 2410 | why = WHY_EXCEPTION; |
| 2411 | |
| 2412 | /* Unwind stacks if a (pseudo) exception occurred */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2413 | |
Raymond Hettinger | 1dd8309 | 2004-02-06 18:32:33 +0000 | [diff] [blame] | 2414 | fast_block_end: |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 2415 | while (why != WHY_NOT && f->f_iblock > 0) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2416 | PyTryBlock *b = PyFrame_BlockPop(f); |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 2417 | |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 2418 | assert(why != WHY_YIELD); |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 2419 | if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) { |
| 2420 | /* For a continue inside a try block, |
| 2421 | don't pop the block for the loop. */ |
Thomas Wouters | 1ee6422 | 2001-09-24 19:32:01 +0000 | [diff] [blame] | 2422 | PyFrame_BlockSetup(f, b->b_type, b->b_handler, |
| 2423 | b->b_level); |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 2424 | why = WHY_NOT; |
| 2425 | JUMPTO(PyInt_AS_LONG(retval)); |
| 2426 | Py_DECREF(retval); |
| 2427 | break; |
| 2428 | } |
| 2429 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2430 | while (STACK_LEVEL() > b->b_level) { |
| 2431 | v = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2432 | Py_XDECREF(v); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2433 | } |
| 2434 | if (b->b_type == SETUP_LOOP && why == WHY_BREAK) { |
| 2435 | why = WHY_NOT; |
| 2436 | JUMPTO(b->b_handler); |
| 2437 | break; |
| 2438 | } |
| 2439 | if (b->b_type == SETUP_FINALLY || |
Guido van Rossum | 150b2df | 1996-12-05 23:17:11 +0000 | [diff] [blame] | 2440 | (b->b_type == SETUP_EXCEPT && |
| 2441 | why == WHY_EXCEPTION)) { |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2442 | if (why == WHY_EXCEPTION) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2443 | PyObject *exc, *val, *tb; |
| 2444 | PyErr_Fetch(&exc, &val, &tb); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2445 | if (val == NULL) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2446 | val = Py_None; |
| 2447 | Py_INCREF(val); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2448 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2449 | /* Make the raw exception data |
| 2450 | available to the handler, |
| 2451 | so a program can emulate the |
| 2452 | Python main loop. Don't do |
| 2453 | this for 'finally'. */ |
| 2454 | if (b->b_type == SETUP_EXCEPT) { |
Barry Warsaw | eaedc7c | 1997-08-28 22:36:40 +0000 | [diff] [blame] | 2455 | PyErr_NormalizeException( |
| 2456 | &exc, &val, &tb); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2457 | set_exc_info(tstate, |
| 2458 | exc, val, tb); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2459 | } |
Jeremy Hylton | c631489 | 2001-09-26 19:24:45 +0000 | [diff] [blame] | 2460 | if (tb == NULL) { |
| 2461 | Py_INCREF(Py_None); |
| 2462 | PUSH(Py_None); |
| 2463 | } else |
| 2464 | PUSH(tb); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2465 | PUSH(val); |
| 2466 | PUSH(exc); |
| 2467 | } |
| 2468 | else { |
Raymond Hettinger | 06032cb | 2004-04-06 09:37:35 +0000 | [diff] [blame] | 2469 | if (why & (WHY_RETURN | WHY_CONTINUE)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2470 | PUSH(retval); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2471 | v = PyInt_FromLong((long)why); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2472 | PUSH(v); |
| 2473 | } |
| 2474 | why = WHY_NOT; |
| 2475 | JUMPTO(b->b_handler); |
| 2476 | break; |
| 2477 | } |
| 2478 | } /* unwind stack */ |
| 2479 | |
| 2480 | /* End the loop if we still have an error (or return) */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2481 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2482 | if (why != WHY_NOT) |
| 2483 | break; |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 2484 | READ_TIMESTAMP(loop1); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2485 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2486 | } /* main loop */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2487 | |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 2488 | assert(why != WHY_YIELD); |
| 2489 | /* Pop remaining stack entries. */ |
| 2490 | while (!EMPTY()) { |
| 2491 | v = POP(); |
| 2492 | Py_XDECREF(v); |
Guido van Rossum | 35974fb | 2001-12-06 21:28:18 +0000 | [diff] [blame] | 2493 | } |
| 2494 | |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 2495 | if (why != WHY_RETURN) |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 2496 | retval = NULL; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2497 | |
Raymond Hettinger | 1dd8309 | 2004-02-06 18:32:33 +0000 | [diff] [blame] | 2498 | fast_yield: |
Fred Drake | 9e3ad78 | 2001-07-03 23:39:52 +0000 | [diff] [blame] | 2499 | if (tstate->use_tracing) { |
Barry Warsaw | e2eca0b | 2005-08-15 18:14:19 +0000 | [diff] [blame] | 2500 | if (tstate->c_tracefunc) { |
| 2501 | if (why == WHY_RETURN || why == WHY_YIELD) { |
| 2502 | if (call_trace(tstate->c_tracefunc, |
| 2503 | tstate->c_traceobj, f, |
| 2504 | PyTrace_RETURN, retval)) { |
| 2505 | Py_XDECREF(retval); |
| 2506 | retval = NULL; |
| 2507 | why = WHY_EXCEPTION; |
| 2508 | } |
| 2509 | } |
| 2510 | else if (why == WHY_EXCEPTION) { |
| 2511 | call_trace_protected(tstate->c_tracefunc, |
| 2512 | tstate->c_traceobj, f, |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 2513 | PyTrace_RETURN, NULL); |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 2514 | } |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 2515 | } |
Fred Drake | 8f51f54 | 2001-10-04 14:48:42 +0000 | [diff] [blame] | 2516 | if (tstate->c_profilefunc) { |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 2517 | if (why == WHY_EXCEPTION) |
| 2518 | call_trace_protected(tstate->c_profilefunc, |
| 2519 | tstate->c_profileobj, f, |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 2520 | PyTrace_RETURN, NULL); |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 2521 | else if (call_trace(tstate->c_profilefunc, |
| 2522 | tstate->c_profileobj, f, |
| 2523 | PyTrace_RETURN, retval)) { |
Fred Drake | 9e3ad78 | 2001-07-03 23:39:52 +0000 | [diff] [blame] | 2524 | Py_XDECREF(retval); |
| 2525 | retval = NULL; |
| 2526 | why = WHY_EXCEPTION; |
| 2527 | } |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 2528 | } |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 2529 | } |
Guido van Rossum | a424013 | 1997-01-21 21:18:36 +0000 | [diff] [blame] | 2530 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2531 | if (tstate->frame->f_exc_type != NULL) |
| 2532 | reset_exc_info(tstate); |
| 2533 | else { |
| 2534 | assert(tstate->frame->f_exc_value == NULL); |
| 2535 | assert(tstate->frame->f_exc_traceback == NULL); |
| 2536 | } |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2537 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2538 | /* pop frame */ |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 2539 | exit_eval_frame: |
| 2540 | Py_LeaveRecursiveCall(); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2541 | tstate->frame = f->f_back; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2542 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 2543 | return retval; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2544 | } |
| 2545 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 2546 | /* 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] | 2547 | PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 2548 | the test in the if statements in Misc/gdbinit (pystack and pystackv). */ |
Skip Montanaro | 786ea6b | 2004-03-01 15:44:05 +0000 | [diff] [blame] | 2549 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2550 | PyObject * |
| 2551 | PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals, |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2552 | PyObject **args, int argcount, PyObject **kws, int kwcount, |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2553 | PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure) |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2554 | { |
| 2555 | register PyFrameObject *f; |
| 2556 | register PyObject *retval = NULL; |
| 2557 | register PyObject **fastlocals, **freevars; |
| 2558 | PyThreadState *tstate = PyThreadState_GET(); |
| 2559 | PyObject *x, *u; |
| 2560 | |
| 2561 | if (globals == NULL) { |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 2562 | PyErr_SetString(PyExc_SystemError, |
Jeremy Hylton | 910d7d4 | 2001-08-12 21:52:24 +0000 | [diff] [blame] | 2563 | "PyEval_EvalCodeEx: NULL globals"); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2564 | return NULL; |
| 2565 | } |
| 2566 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 2567 | assert(tstate != NULL); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 2568 | assert(globals != NULL); |
| 2569 | f = PyFrame_New(tstate, co, globals, locals); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2570 | if (f == NULL) |
| 2571 | return NULL; |
| 2572 | |
| 2573 | fastlocals = f->f_localsplus; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2574 | freevars = f->f_localsplus + co->co_nlocals; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2575 | |
| 2576 | if (co->co_argcount > 0 || |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2577 | co->co_kwonlyargcount > 0 || |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2578 | co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) { |
| 2579 | int i; |
| 2580 | int n = argcount; |
| 2581 | PyObject *kwdict = NULL; |
| 2582 | if (co->co_flags & CO_VARKEYWORDS) { |
| 2583 | kwdict = PyDict_New(); |
| 2584 | if (kwdict == NULL) |
| 2585 | goto fail; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2586 | i = co->co_argcount + co->co_kwonlyargcount; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2587 | if (co->co_flags & CO_VARARGS) |
| 2588 | i++; |
| 2589 | SETLOCAL(i, kwdict); |
| 2590 | } |
| 2591 | if (argcount > co->co_argcount) { |
| 2592 | if (!(co->co_flags & CO_VARARGS)) { |
| 2593 | PyErr_Format(PyExc_TypeError, |
| 2594 | "%.200s() takes %s %d " |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2595 | "%spositional argument%s (%d given)", |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2596 | PyString_AsString(co->co_name), |
| 2597 | defcount ? "at most" : "exactly", |
| 2598 | co->co_argcount, |
| 2599 | kwcount ? "non-keyword " : "", |
| 2600 | co->co_argcount == 1 ? "" : "s", |
| 2601 | argcount); |
| 2602 | goto fail; |
| 2603 | } |
| 2604 | n = co->co_argcount; |
| 2605 | } |
| 2606 | for (i = 0; i < n; i++) { |
| 2607 | x = args[i]; |
| 2608 | Py_INCREF(x); |
| 2609 | SETLOCAL(i, x); |
| 2610 | } |
| 2611 | if (co->co_flags & CO_VARARGS) { |
| 2612 | u = PyTuple_New(argcount - n); |
| 2613 | if (u == NULL) |
| 2614 | goto fail; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2615 | SETLOCAL(co->co_argcount + co->co_kwonlyargcount, u); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2616 | for (i = n; i < argcount; i++) { |
| 2617 | x = args[i]; |
| 2618 | Py_INCREF(x); |
| 2619 | PyTuple_SET_ITEM(u, i-n, x); |
| 2620 | } |
| 2621 | } |
| 2622 | for (i = 0; i < kwcount; i++) { |
| 2623 | PyObject *keyword = kws[2*i]; |
| 2624 | PyObject *value = kws[2*i + 1]; |
| 2625 | int j; |
| 2626 | if (keyword == NULL || !PyString_Check(keyword)) { |
| 2627 | PyErr_Format(PyExc_TypeError, |
| 2628 | "%.200s() keywords must be strings", |
| 2629 | PyString_AsString(co->co_name)); |
| 2630 | goto fail; |
| 2631 | } |
| 2632 | /* XXX slow -- speed up using dictionary? */ |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2633 | for (j = 0; |
| 2634 | j < co->co_argcount + co->co_kwonlyargcount; |
| 2635 | j++) { |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2636 | PyObject *nm = PyTuple_GET_ITEM( |
| 2637 | co->co_varnames, j); |
| 2638 | int cmp = PyObject_RichCompareBool( |
| 2639 | keyword, nm, Py_EQ); |
| 2640 | if (cmp > 0) |
| 2641 | break; |
| 2642 | else if (cmp < 0) |
| 2643 | goto fail; |
| 2644 | } |
| 2645 | /* Check errors from Compare */ |
| 2646 | if (PyErr_Occurred()) |
| 2647 | goto fail; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2648 | if (j >= co->co_argcount + co->co_kwonlyargcount) { |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2649 | if (kwdict == NULL) { |
| 2650 | PyErr_Format(PyExc_TypeError, |
| 2651 | "%.200s() got an unexpected " |
| 2652 | "keyword argument '%.400s'", |
| 2653 | PyString_AsString(co->co_name), |
| 2654 | PyString_AsString(keyword)); |
| 2655 | goto fail; |
| 2656 | } |
| 2657 | PyDict_SetItem(kwdict, keyword, value); |
| 2658 | } |
| 2659 | else { |
| 2660 | if (GETLOCAL(j) != NULL) { |
| 2661 | PyErr_Format(PyExc_TypeError, |
| 2662 | "%.200s() got multiple " |
| 2663 | "values for keyword " |
| 2664 | "argument '%.400s'", |
| 2665 | PyString_AsString(co->co_name), |
| 2666 | PyString_AsString(keyword)); |
| 2667 | goto fail; |
| 2668 | } |
| 2669 | Py_INCREF(value); |
| 2670 | SETLOCAL(j, value); |
| 2671 | } |
| 2672 | } |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2673 | if (co->co_kwonlyargcount > 0) { |
| 2674 | for (i = co->co_argcount; |
| 2675 | i < co->co_argcount + co->co_kwonlyargcount; |
| 2676 | i++) { |
Guido van Rossum | 29602e4 | 2006-11-22 04:45:33 +0000 | [diff] [blame] | 2677 | PyObject *name, *def; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2678 | if (GETLOCAL(i) != NULL) |
| 2679 | continue; |
Guido van Rossum | 29602e4 | 2006-11-22 04:45:33 +0000 | [diff] [blame] | 2680 | name = PyTuple_GET_ITEM(co->co_varnames, i); |
| 2681 | def = NULL; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2682 | if (kwdefs != NULL) |
| 2683 | def = PyDict_GetItem(kwdefs, name); |
| 2684 | if (def != NULL) { |
| 2685 | Py_INCREF(def); |
| 2686 | SETLOCAL(i, def); |
| 2687 | continue; |
| 2688 | } |
| 2689 | PyErr_Format(PyExc_TypeError, |
| 2690 | "%.200s() needs " |
Brett Cannon | e33a611 | 2007-01-29 23:43:38 +0000 | [diff] [blame] | 2691 | "keyword-only argument %s", |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2692 | PyString_AsString(co->co_name), |
| 2693 | PyString_AsString(name)); |
| 2694 | goto fail; |
| 2695 | } |
| 2696 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2697 | if (argcount < co->co_argcount) { |
| 2698 | int m = co->co_argcount - defcount; |
| 2699 | for (i = argcount; i < m; i++) { |
| 2700 | if (GETLOCAL(i) == NULL) { |
| 2701 | PyErr_Format(PyExc_TypeError, |
| 2702 | "%.200s() takes %s %d " |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2703 | "%spositional argument%s " |
| 2704 | "(%d given)", |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2705 | PyString_AsString(co->co_name), |
| 2706 | ((co->co_flags & CO_VARARGS) || |
| 2707 | defcount) ? "at least" |
| 2708 | : "exactly", |
| 2709 | m, kwcount ? "non-keyword " : "", |
| 2710 | m == 1 ? "" : "s", i); |
| 2711 | goto fail; |
| 2712 | } |
| 2713 | } |
| 2714 | if (n > m) |
| 2715 | i = n - m; |
| 2716 | else |
| 2717 | i = 0; |
| 2718 | for (; i < defcount; i++) { |
| 2719 | if (GETLOCAL(m+i) == NULL) { |
| 2720 | PyObject *def = defs[i]; |
| 2721 | Py_INCREF(def); |
| 2722 | SETLOCAL(m+i, def); |
| 2723 | } |
| 2724 | } |
| 2725 | } |
| 2726 | } |
| 2727 | else { |
| 2728 | if (argcount > 0 || kwcount > 0) { |
| 2729 | PyErr_Format(PyExc_TypeError, |
| 2730 | "%.200s() takes no arguments (%d given)", |
| 2731 | PyString_AsString(co->co_name), |
| 2732 | argcount + kwcount); |
| 2733 | goto fail; |
| 2734 | } |
| 2735 | } |
| 2736 | /* Allocate and initialize storage for cell vars, and copy free |
| 2737 | vars into frame. This isn't too efficient right now. */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2738 | if (PyTuple_GET_SIZE(co->co_cellvars)) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2739 | int i, j, nargs, found; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2740 | char *cellname, *argname; |
| 2741 | PyObject *c; |
| 2742 | |
| 2743 | nargs = co->co_argcount; |
| 2744 | if (co->co_flags & CO_VARARGS) |
| 2745 | nargs++; |
| 2746 | if (co->co_flags & CO_VARKEYWORDS) |
| 2747 | nargs++; |
| 2748 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2749 | /* Initialize each cell var, taking into account |
| 2750 | cell vars that are initialized from arguments. |
| 2751 | |
| 2752 | Should arrange for the compiler to put cellvars |
| 2753 | that are arguments at the beginning of the cellvars |
| 2754 | list so that we can march over it more efficiently? |
| 2755 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2756 | for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) { |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2757 | cellname = PyString_AS_STRING( |
| 2758 | PyTuple_GET_ITEM(co->co_cellvars, i)); |
| 2759 | found = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2760 | for (j = 0; j < nargs; j++) { |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2761 | argname = PyString_AS_STRING( |
| 2762 | PyTuple_GET_ITEM(co->co_varnames, j)); |
| 2763 | if (strcmp(cellname, argname) == 0) { |
| 2764 | c = PyCell_New(GETLOCAL(j)); |
| 2765 | if (c == NULL) |
| 2766 | goto fail; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2767 | GETLOCAL(co->co_nlocals + i) = c; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2768 | found = 1; |
| 2769 | break; |
| 2770 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2771 | } |
| 2772 | if (found == 0) { |
| 2773 | c = PyCell_New(NULL); |
| 2774 | if (c == NULL) |
| 2775 | goto fail; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2776 | SETLOCAL(co->co_nlocals + i, c); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2777 | } |
| 2778 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2779 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2780 | if (PyTuple_GET_SIZE(co->co_freevars)) { |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2781 | int i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2782 | for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) { |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2783 | PyObject *o = PyTuple_GET_ITEM(closure, i); |
| 2784 | Py_INCREF(o); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2785 | freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2786 | } |
| 2787 | } |
| 2788 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2789 | if (co->co_flags & CO_GENERATOR) { |
Neil Schemenauer | 2b13ce8 | 2001-06-21 02:41:10 +0000 | [diff] [blame] | 2790 | /* Don't need to keep the reference to f_back, it will be set |
| 2791 | * when the generator is resumed. */ |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 2792 | Py_XDECREF(f->f_back); |
Neil Schemenauer | 2b13ce8 | 2001-06-21 02:41:10 +0000 | [diff] [blame] | 2793 | f->f_back = NULL; |
| 2794 | |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 2795 | PCALL(PCALL_GENERATOR); |
| 2796 | |
Neil Schemenauer | 2b13ce8 | 2001-06-21 02:41:10 +0000 | [diff] [blame] | 2797 | /* Create a new generator that owns the ready to run frame |
| 2798 | * and return that as the value. */ |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 2799 | return PyGen_New(f); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2800 | } |
| 2801 | |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 2802 | retval = PyEval_EvalFrameEx(f,0); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2803 | |
| 2804 | fail: /* Jump here from prelude on failure */ |
| 2805 | |
Tim Peters | b13680b | 2001-11-27 23:29:29 +0000 | [diff] [blame] | 2806 | /* decref'ing the frame can cause __del__ methods to get invoked, |
| 2807 | which can call back into Python. While we're done with the |
| 2808 | current Python frame (f), the associated C stack is still in use, |
| 2809 | so recursion_depth must be boosted for the duration. |
| 2810 | */ |
| 2811 | assert(tstate != NULL); |
| 2812 | ++tstate->recursion_depth; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2813 | Py_DECREF(f); |
Tim Peters | b13680b | 2001-11-27 23:29:29 +0000 | [diff] [blame] | 2814 | --tstate->recursion_depth; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2815 | return retval; |
| 2816 | } |
| 2817 | |
| 2818 | |
Guido van Rossum | c9fbb72 | 2003-03-01 03:36:33 +0000 | [diff] [blame] | 2819 | /* Implementation notes for set_exc_info() and reset_exc_info(): |
| 2820 | |
| 2821 | - Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and |
| 2822 | 'exc_traceback'. These always travel together. |
| 2823 | |
| 2824 | - tstate->curexc_ZZZ is the "hot" exception that is set by |
| 2825 | PyErr_SetString(), cleared by PyErr_Clear(), and so on. |
| 2826 | |
| 2827 | - Once an exception is caught by an except clause, it is transferred |
| 2828 | from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info() |
| 2829 | can pick it up. This is the primary task of set_exc_info(). |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2830 | 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] | 2831 | |
| 2832 | - Now let me explain the complicated dance with frame->f_exc_ZZZ. |
| 2833 | |
| 2834 | Long ago, when none of this existed, there were just a few globals: |
| 2835 | one set corresponding to the "hot" exception, and one set |
| 2836 | corresponding to sys.exc_ZZZ. (Actually, the latter weren't C |
| 2837 | globals; they were simply stored as sys.exc_ZZZ. For backwards |
| 2838 | compatibility, they still are!) The problem was that in code like |
| 2839 | this: |
| 2840 | |
| 2841 | try: |
| 2842 | "something that may fail" |
| 2843 | except "some exception": |
| 2844 | "do something else first" |
| 2845 | "print the exception from sys.exc_ZZZ." |
| 2846 | |
| 2847 | if "do something else first" invoked something that raised and caught |
| 2848 | an exception, sys.exc_ZZZ were overwritten. That was a frequent |
| 2849 | cause of subtle bugs. I fixed this by changing the semantics as |
| 2850 | follows: |
| 2851 | |
| 2852 | - Within one frame, sys.exc_ZZZ will hold the last exception caught |
| 2853 | *in that frame*. |
| 2854 | |
| 2855 | - But initially, and as long as no exception is caught in a given |
| 2856 | frame, sys.exc_ZZZ will hold the last exception caught in the |
| 2857 | previous frame (or the frame before that, etc.). |
| 2858 | |
| 2859 | The first bullet fixed the bug in the above example. The second |
| 2860 | bullet was for backwards compatibility: it was (and is) common to |
| 2861 | have a function that is called when an exception is caught, and to |
| 2862 | have that function access the caught exception via sys.exc_ZZZ. |
| 2863 | (Example: traceback.print_exc()). |
| 2864 | |
| 2865 | At the same time I fixed the problem that sys.exc_ZZZ weren't |
| 2866 | thread-safe, by introducing sys.exc_info() which gets it from tstate; |
| 2867 | but that's really a separate improvement. |
| 2868 | |
| 2869 | The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ |
| 2870 | variables to what they were before the current frame was called. The |
| 2871 | set_exc_info() function saves them on the frame so that |
| 2872 | reset_exc_info() can restore them. The invariant is that |
| 2873 | frame->f_exc_ZZZ is NULL iff the current frame never caught an |
| 2874 | exception (where "catching" an exception applies only to successful |
| 2875 | except clauses); and if the current frame ever caught an exception, |
| 2876 | frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ |
| 2877 | at the start of the current frame. |
| 2878 | |
| 2879 | */ |
| 2880 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2881 | static void |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2882 | set_exc_info(PyThreadState *tstate, |
| 2883 | PyObject *type, PyObject *value, PyObject *tb) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2884 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2885 | PyFrameObject *frame = tstate->frame; |
Guido van Rossum | df4c308 | 1997-05-20 17:06:11 +0000 | [diff] [blame] | 2886 | PyObject *tmp_type, *tmp_value, *tmp_tb; |
Barry Warsaw | 4249f54 | 1997-08-22 21:26:19 +0000 | [diff] [blame] | 2887 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2888 | assert(type != NULL); |
| 2889 | assert(frame != NULL); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2890 | if (frame->f_exc_type == NULL) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2891 | assert(frame->f_exc_value == NULL); |
| 2892 | assert(frame->f_exc_traceback == NULL); |
| 2893 | /* This frame didn't catch an exception before. */ |
| 2894 | /* Save previous exception of this thread in this frame. */ |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2895 | if (tstate->exc_type == NULL) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2896 | /* XXX Why is this set to Py_None? */ |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2897 | Py_INCREF(Py_None); |
| 2898 | tstate->exc_type = Py_None; |
| 2899 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2900 | Py_INCREF(tstate->exc_type); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2901 | Py_XINCREF(tstate->exc_value); |
| 2902 | Py_XINCREF(tstate->exc_traceback); |
| 2903 | frame->f_exc_type = tstate->exc_type; |
| 2904 | frame->f_exc_value = tstate->exc_value; |
| 2905 | frame->f_exc_traceback = tstate->exc_traceback; |
| 2906 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2907 | /* Set new exception for this thread. */ |
Guido van Rossum | df4c308 | 1997-05-20 17:06:11 +0000 | [diff] [blame] | 2908 | tmp_type = tstate->exc_type; |
| 2909 | tmp_value = tstate->exc_value; |
| 2910 | tmp_tb = tstate->exc_traceback; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2911 | Py_INCREF(type); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2912 | Py_XINCREF(value); |
| 2913 | Py_XINCREF(tb); |
| 2914 | tstate->exc_type = type; |
| 2915 | tstate->exc_value = value; |
| 2916 | tstate->exc_traceback = tb; |
Guido van Rossum | df4c308 | 1997-05-20 17:06:11 +0000 | [diff] [blame] | 2917 | Py_XDECREF(tmp_type); |
| 2918 | Py_XDECREF(tmp_value); |
| 2919 | Py_XDECREF(tmp_tb); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2920 | /* For b/w compatibility */ |
| 2921 | PySys_SetObject("exc_type", type); |
| 2922 | PySys_SetObject("exc_value", value); |
| 2923 | PySys_SetObject("exc_traceback", tb); |
| 2924 | } |
| 2925 | |
| 2926 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2927 | reset_exc_info(PyThreadState *tstate) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2928 | { |
| 2929 | PyFrameObject *frame; |
Guido van Rossum | df4c308 | 1997-05-20 17:06:11 +0000 | [diff] [blame] | 2930 | PyObject *tmp_type, *tmp_value, *tmp_tb; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2931 | |
| 2932 | /* It's a precondition that the thread state's frame caught an |
| 2933 | * exception -- verify in a debug build. |
| 2934 | */ |
| 2935 | assert(tstate != NULL); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2936 | frame = tstate->frame; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2937 | assert(frame != NULL); |
| 2938 | assert(frame->f_exc_type != NULL); |
| 2939 | |
| 2940 | /* Copy the frame's exception info back to the thread state. */ |
| 2941 | tmp_type = tstate->exc_type; |
| 2942 | tmp_value = tstate->exc_value; |
| 2943 | tmp_tb = tstate->exc_traceback; |
| 2944 | Py_INCREF(frame->f_exc_type); |
| 2945 | Py_XINCREF(frame->f_exc_value); |
| 2946 | Py_XINCREF(frame->f_exc_traceback); |
| 2947 | tstate->exc_type = frame->f_exc_type; |
| 2948 | tstate->exc_value = frame->f_exc_value; |
| 2949 | tstate->exc_traceback = frame->f_exc_traceback; |
| 2950 | Py_XDECREF(tmp_type); |
| 2951 | Py_XDECREF(tmp_value); |
| 2952 | Py_XDECREF(tmp_tb); |
| 2953 | |
| 2954 | /* For b/w compatibility */ |
| 2955 | PySys_SetObject("exc_type", frame->f_exc_type); |
| 2956 | PySys_SetObject("exc_value", frame->f_exc_value); |
| 2957 | PySys_SetObject("exc_traceback", frame->f_exc_traceback); |
| 2958 | |
| 2959 | /* Clear the frame's exception info. */ |
Guido van Rossum | df4c308 | 1997-05-20 17:06:11 +0000 | [diff] [blame] | 2960 | tmp_type = frame->f_exc_type; |
| 2961 | tmp_value = frame->f_exc_value; |
| 2962 | tmp_tb = frame->f_exc_traceback; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2963 | frame->f_exc_type = NULL; |
| 2964 | frame->f_exc_value = NULL; |
| 2965 | frame->f_exc_traceback = NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2966 | Py_DECREF(tmp_type); |
Guido van Rossum | df4c308 | 1997-05-20 17:06:11 +0000 | [diff] [blame] | 2967 | Py_XDECREF(tmp_value); |
| 2968 | Py_XDECREF(tmp_tb); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2969 | } |
| 2970 | |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 2971 | /* Logic for the raise statement (too complicated for inlining). |
| 2972 | This *consumes* a reference count to each of its arguments. */ |
Raymond Hettinger | 7c95865 | 2004-04-06 10:11:10 +0000 | [diff] [blame] | 2973 | static enum why_code |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2974 | do_raise(PyObject *type, PyObject *value, PyObject *tb) |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 2975 | { |
Guido van Rossum | d295f12 | 1998-04-09 21:39:57 +0000 | [diff] [blame] | 2976 | if (type == NULL) { |
| 2977 | /* Reraise */ |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 2978 | PyThreadState *tstate = PyThreadState_GET(); |
Guido van Rossum | d295f12 | 1998-04-09 21:39:57 +0000 | [diff] [blame] | 2979 | type = tstate->exc_type == NULL ? Py_None : tstate->exc_type; |
| 2980 | value = tstate->exc_value; |
| 2981 | tb = tstate->exc_traceback; |
| 2982 | Py_XINCREF(type); |
| 2983 | Py_XINCREF(value); |
| 2984 | Py_XINCREF(tb); |
| 2985 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2986 | |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 2987 | /* We support the following forms of raise: |
| 2988 | raise <class>, <classinstance> |
| 2989 | raise <class>, <argument tuple> |
| 2990 | raise <class>, None |
| 2991 | raise <class>, <argument> |
| 2992 | raise <classinstance>, None |
| 2993 | raise <string>, <object> |
| 2994 | raise <string>, None |
| 2995 | |
| 2996 | An omitted second argument is the same as None. |
| 2997 | |
| 2998 | In addition, raise <tuple>, <anything> is the same as |
| 2999 | raising the tuple's first item (and it better have one!); |
| 3000 | this rule is applied recursively. |
| 3001 | |
| 3002 | Finally, an optional third argument can be supplied, which |
| 3003 | gives the traceback to be substituted (useful when |
| 3004 | re-raising an exception after examining it). */ |
| 3005 | |
| 3006 | /* First, check the traceback argument, replacing None with |
| 3007 | NULL. */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3008 | if (tb == Py_None) { |
| 3009 | Py_DECREF(tb); |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3010 | tb = NULL; |
| 3011 | } |
| 3012 | else if (tb != NULL && !PyTraceBack_Check(tb)) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3013 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3014 | "raise: arg 3 must be a traceback or None"); |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3015 | goto raise_error; |
| 3016 | } |
| 3017 | |
| 3018 | /* Next, replace a missing value with None */ |
| 3019 | if (value == NULL) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3020 | value = Py_None; |
| 3021 | Py_INCREF(value); |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3022 | } |
| 3023 | |
| 3024 | /* Next, repeatedly, replace a tuple exception with its first item */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3025 | while (PyTuple_Check(type) && PyTuple_Size(type) > 0) { |
| 3026 | PyObject *tmp = type; |
| 3027 | type = PyTuple_GET_ITEM(type, 0); |
| 3028 | Py_INCREF(type); |
| 3029 | Py_DECREF(tmp); |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3030 | } |
| 3031 | |
Guido van Rossum | 45aecf4 | 2006-03-15 04:58:47 +0000 | [diff] [blame] | 3032 | if (PyExceptionClass_Check(type)) |
Barry Warsaw | 4249f54 | 1997-08-22 21:26:19 +0000 | [diff] [blame] | 3033 | PyErr_NormalizeException(&type, &value, &tb); |
| 3034 | |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 3035 | else if (PyExceptionInstance_Check(type)) { |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3036 | /* Raising an instance. The value should be a dummy. */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3037 | if (value != Py_None) { |
| 3038 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3039 | "instance exception may not have a separate value"); |
| 3040 | goto raise_error; |
| 3041 | } |
| 3042 | else { |
| 3043 | /* Normalize to raise <class>, <instance> */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3044 | Py_DECREF(value); |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3045 | value = type; |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 3046 | type = PyExceptionInstance_Class(type); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3047 | Py_INCREF(type); |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3048 | } |
| 3049 | } |
| 3050 | else { |
| 3051 | /* Not something you can raise. You get an exception |
| 3052 | anyway, just not what you specified :-) */ |
Guido van Rossum | 45aecf4 | 2006-03-15 04:58:47 +0000 | [diff] [blame] | 3053 | PyErr_SetString(PyExc_TypeError, |
| 3054 | "exceptions must derive from BaseException"); |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3055 | goto raise_error; |
| 3056 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3057 | PyErr_Restore(type, value, tb); |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3058 | if (tb == NULL) |
| 3059 | return WHY_EXCEPTION; |
| 3060 | else |
| 3061 | return WHY_RERAISE; |
| 3062 | raise_error: |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3063 | Py_XDECREF(value); |
| 3064 | Py_XDECREF(type); |
| 3065 | Py_XDECREF(tb); |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3066 | return WHY_EXCEPTION; |
| 3067 | } |
| 3068 | |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 3069 | /* Iterate v argcnt times and store the results on the stack (via decreasing |
| 3070 | sp). Return 1 for success, 0 if error. */ |
| 3071 | |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 3072 | static int |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 3073 | unpack_iterable(PyObject *v, int argcnt, PyObject **sp) |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 3074 | { |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 3075 | int i = 0; |
| 3076 | PyObject *it; /* iter(v) */ |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 3077 | PyObject *w; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3078 | |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 3079 | assert(v != NULL); |
| 3080 | |
| 3081 | it = PyObject_GetIter(v); |
| 3082 | if (it == NULL) |
| 3083 | goto Error; |
| 3084 | |
| 3085 | for (; i < argcnt; i++) { |
| 3086 | w = PyIter_Next(it); |
| 3087 | if (w == NULL) { |
| 3088 | /* Iterator done, via error or exhaustion. */ |
| 3089 | if (!PyErr_Occurred()) { |
| 3090 | PyErr_Format(PyExc_ValueError, |
| 3091 | "need more than %d value%s to unpack", |
| 3092 | i, i == 1 ? "" : "s"); |
| 3093 | } |
| 3094 | goto Error; |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 3095 | } |
| 3096 | *--sp = w; |
| 3097 | } |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 3098 | |
| 3099 | /* We better have exhausted the iterator now. */ |
| 3100 | w = PyIter_Next(it); |
| 3101 | if (w == NULL) { |
| 3102 | if (PyErr_Occurred()) |
| 3103 | goto Error; |
| 3104 | Py_DECREF(it); |
| 3105 | return 1; |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 3106 | } |
Guido van Rossum | bb8f59a | 2001-12-03 19:33:25 +0000 | [diff] [blame] | 3107 | Py_DECREF(w); |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 3108 | PyErr_SetString(PyExc_ValueError, "too many values to unpack"); |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 3109 | /* fall through */ |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 3110 | Error: |
Barry Warsaw | 9101055 | 1997-08-25 22:30:51 +0000 | [diff] [blame] | 3111 | for (; i > 0; i--, sp++) |
| 3112 | Py_DECREF(*sp); |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 3113 | Py_XDECREF(it); |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 3114 | return 0; |
| 3115 | } |
| 3116 | |
| 3117 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 3118 | #ifdef LLTRACE |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3119 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3120 | prtrace(PyObject *v, char *str) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3121 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3122 | printf("%s ", str); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3123 | if (PyObject_Print(v, stdout, 0) != 0) |
| 3124 | PyErr_Clear(); /* Don't know what else to do */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3125 | printf("\n"); |
Guido van Rossum | cc229ea | 2000-05-04 00:55:17 +0000 | [diff] [blame] | 3126 | return 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3127 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3128 | #endif |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3129 | |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 3130 | static void |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3131 | call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f) |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 3132 | { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3133 | PyObject *type, *value, *traceback, *arg; |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 3134 | int err; |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3135 | PyErr_Fetch(&type, &value, &traceback); |
Guido van Rossum | bd9ccca | 1992-04-09 14:58:08 +0000 | [diff] [blame] | 3136 | if (value == NULL) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3137 | value = Py_None; |
| 3138 | Py_INCREF(value); |
Guido van Rossum | bd9ccca | 1992-04-09 14:58:08 +0000 | [diff] [blame] | 3139 | } |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 3140 | arg = PyTuple_Pack(3, type, value, traceback); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 3141 | if (arg == NULL) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3142 | PyErr_Restore(type, value, traceback); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 3143 | return; |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 3144 | } |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3145 | err = call_trace(func, self, f, PyTrace_EXCEPTION, arg); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3146 | Py_DECREF(arg); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 3147 | if (err == 0) |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3148 | PyErr_Restore(type, value, traceback); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 3149 | else { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3150 | Py_XDECREF(type); |
| 3151 | Py_XDECREF(value); |
| 3152 | Py_XDECREF(traceback); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 3153 | } |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 3154 | } |
| 3155 | |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 3156 | static void |
| 3157 | call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame, |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 3158 | int what, PyObject *arg) |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 3159 | { |
| 3160 | PyObject *type, *value, *traceback; |
| 3161 | int err; |
| 3162 | PyErr_Fetch(&type, &value, &traceback); |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 3163 | err = call_trace(func, obj, frame, what, arg); |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 3164 | if (err == 0) |
| 3165 | PyErr_Restore(type, value, traceback); |
| 3166 | else { |
| 3167 | Py_XDECREF(type); |
| 3168 | Py_XDECREF(value); |
| 3169 | Py_XDECREF(traceback); |
| 3170 | } |
| 3171 | } |
| 3172 | |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 3173 | static int |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3174 | call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame, |
| 3175 | int what, PyObject *arg) |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 3176 | { |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3177 | register PyThreadState *tstate = frame->f_tstate; |
| 3178 | int result; |
| 3179 | if (tstate->tracing) |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 3180 | return 0; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3181 | tstate->tracing++; |
Fred Drake | 9e3ad78 | 2001-07-03 23:39:52 +0000 | [diff] [blame] | 3182 | tstate->use_tracing = 0; |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3183 | result = func(obj, frame, what, arg); |
Fred Drake | 9e3ad78 | 2001-07-03 23:39:52 +0000 | [diff] [blame] | 3184 | tstate->use_tracing = ((tstate->c_tracefunc != NULL) |
| 3185 | || (tstate->c_profilefunc != NULL)); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3186 | tstate->tracing--; |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3187 | return result; |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 3188 | } |
| 3189 | |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 3190 | PyObject * |
| 3191 | _PyEval_CallTracing(PyObject *func, PyObject *args) |
| 3192 | { |
| 3193 | PyFrameObject *frame = PyEval_GetFrame(); |
| 3194 | PyThreadState *tstate = frame->f_tstate; |
| 3195 | int save_tracing = tstate->tracing; |
| 3196 | int save_use_tracing = tstate->use_tracing; |
| 3197 | PyObject *result; |
| 3198 | |
| 3199 | tstate->tracing = 0; |
| 3200 | tstate->use_tracing = ((tstate->c_tracefunc != NULL) |
| 3201 | || (tstate->c_profilefunc != NULL)); |
| 3202 | result = PyObject_Call(func, args, NULL); |
| 3203 | tstate->tracing = save_tracing; |
| 3204 | tstate->use_tracing = save_use_tracing; |
| 3205 | return result; |
| 3206 | } |
| 3207 | |
Michael W. Hudson | 006c752 | 2002-11-08 13:08:46 +0000 | [diff] [blame] | 3208 | static int |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 3209 | maybe_call_line_trace(Py_tracefunc func, PyObject *obj, |
Armin Rigo | bf57a14 | 2004-03-22 19:24:58 +0000 | [diff] [blame] | 3210 | PyFrameObject *frame, int *instr_lb, int *instr_ub, |
| 3211 | int *instr_prev) |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 3212 | { |
Michael W. Hudson | 006c752 | 2002-11-08 13:08:46 +0000 | [diff] [blame] | 3213 | int result = 0; |
| 3214 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3215 | /* If the last instruction executed isn't in the current |
| 3216 | instruction window, reset the window. If the last |
| 3217 | instruction happens to fall at the start of a line or if it |
| 3218 | represents a jump backwards, call the trace function. |
| 3219 | */ |
Michael W. Hudson | 53d58bb | 2002-08-30 13:09:51 +0000 | [diff] [blame] | 3220 | if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3221 | int line; |
| 3222 | PyAddrPair bounds; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 3223 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3224 | line = PyCode_CheckLineNumber(frame->f_code, frame->f_lasti, |
| 3225 | &bounds); |
| 3226 | if (line >= 0) { |
Michael W. Hudson | 02ff6a9 | 2002-09-11 15:36:32 +0000 | [diff] [blame] | 3227 | frame->f_lineno = line; |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 3228 | result = call_trace(func, obj, frame, |
Michael W. Hudson | 006c752 | 2002-11-08 13:08:46 +0000 | [diff] [blame] | 3229 | PyTrace_LINE, Py_None); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3230 | } |
| 3231 | *instr_lb = bounds.ap_lower; |
| 3232 | *instr_ub = bounds.ap_upper; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 3233 | } |
Armin Rigo | bf57a14 | 2004-03-22 19:24:58 +0000 | [diff] [blame] | 3234 | else if (frame->f_lasti <= *instr_prev) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3235 | result = call_trace(func, obj, frame, PyTrace_LINE, Py_None); |
Armin Rigo | bf57a14 | 2004-03-22 19:24:58 +0000 | [diff] [blame] | 3236 | } |
| 3237 | *instr_prev = frame->f_lasti; |
Michael W. Hudson | 006c752 | 2002-11-08 13:08:46 +0000 | [diff] [blame] | 3238 | return result; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 3239 | } |
| 3240 | |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3241 | void |
| 3242 | PyEval_SetProfile(Py_tracefunc func, PyObject *arg) |
Fred Drake | d083839 | 2001-06-16 21:02:31 +0000 | [diff] [blame] | 3243 | { |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 3244 | PyThreadState *tstate = PyThreadState_GET(); |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3245 | PyObject *temp = tstate->c_profileobj; |
| 3246 | Py_XINCREF(arg); |
| 3247 | tstate->c_profilefunc = NULL; |
| 3248 | tstate->c_profileobj = NULL; |
Brett Cannon | 55fa66d | 2005-06-25 07:07:35 +0000 | [diff] [blame] | 3249 | /* Must make sure that tracing is not ignored if 'temp' is freed */ |
Fred Drake | 9e3ad78 | 2001-07-03 23:39:52 +0000 | [diff] [blame] | 3250 | tstate->use_tracing = tstate->c_tracefunc != NULL; |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3251 | Py_XDECREF(temp); |
| 3252 | tstate->c_profilefunc = func; |
| 3253 | tstate->c_profileobj = arg; |
Brett Cannon | 55fa66d | 2005-06-25 07:07:35 +0000 | [diff] [blame] | 3254 | /* Flag that tracing or profiling is turned on */ |
Fred Drake | 9e3ad78 | 2001-07-03 23:39:52 +0000 | [diff] [blame] | 3255 | tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL); |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3256 | } |
| 3257 | |
| 3258 | void |
| 3259 | PyEval_SetTrace(Py_tracefunc func, PyObject *arg) |
| 3260 | { |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 3261 | PyThreadState *tstate = PyThreadState_GET(); |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3262 | PyObject *temp = tstate->c_traceobj; |
| 3263 | Py_XINCREF(arg); |
| 3264 | tstate->c_tracefunc = NULL; |
| 3265 | tstate->c_traceobj = NULL; |
Brett Cannon | 55fa66d | 2005-06-25 07:07:35 +0000 | [diff] [blame] | 3266 | /* Must make sure that profiling is not ignored if 'temp' is freed */ |
Fred Drake | 9e3ad78 | 2001-07-03 23:39:52 +0000 | [diff] [blame] | 3267 | tstate->use_tracing = tstate->c_profilefunc != NULL; |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3268 | Py_XDECREF(temp); |
| 3269 | tstate->c_tracefunc = func; |
| 3270 | tstate->c_traceobj = arg; |
Brett Cannon | 55fa66d | 2005-06-25 07:07:35 +0000 | [diff] [blame] | 3271 | /* Flag that tracing or profiling is turned on */ |
Fred Drake | 9e3ad78 | 2001-07-03 23:39:52 +0000 | [diff] [blame] | 3272 | tstate->use_tracing = ((func != NULL) |
| 3273 | || (tstate->c_profilefunc != NULL)); |
Fred Drake | d083839 | 2001-06-16 21:02:31 +0000 | [diff] [blame] | 3274 | } |
| 3275 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3276 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3277 | PyEval_GetBuiltins(void) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 3278 | { |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 3279 | PyFrameObject *current_frame = PyEval_GetFrame(); |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 3280 | if (current_frame == NULL) |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 3281 | return PyThreadState_GET()->interp->builtins; |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 3282 | else |
| 3283 | return current_frame->f_builtins; |
| 3284 | } |
| 3285 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3286 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3287 | PyEval_GetLocals(void) |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 3288 | { |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 3289 | PyFrameObject *current_frame = PyEval_GetFrame(); |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 3290 | if (current_frame == NULL) |
| 3291 | return NULL; |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3292 | PyFrame_FastToLocals(current_frame); |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 3293 | return current_frame->f_locals; |
| 3294 | } |
| 3295 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3296 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3297 | PyEval_GetGlobals(void) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3298 | { |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 3299 | PyFrameObject *current_frame = PyEval_GetFrame(); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3300 | if (current_frame == NULL) |
| 3301 | return NULL; |
| 3302 | else |
| 3303 | return current_frame->f_globals; |
| 3304 | } |
| 3305 | |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 3306 | PyFrameObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3307 | PyEval_GetFrame(void) |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 3308 | { |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 3309 | PyThreadState *tstate = PyThreadState_GET(); |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 3310 | return _PyThreadState_GetFrame(tstate); |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 3311 | } |
| 3312 | |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 3313 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3314 | PyEval_GetRestricted(void) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 3315 | { |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 3316 | PyFrameObject *current_frame = PyEval_GetFrame(); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3317 | return current_frame == NULL ? 0 : PyFrame_IsRestricted(current_frame); |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 3318 | } |
| 3319 | |
Guido van Rossum | be27026 | 1997-05-22 22:26:18 +0000 | [diff] [blame] | 3320 | int |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 3321 | PyEval_MergeCompilerFlags(PyCompilerFlags *cf) |
Jeremy Hylton | 061d106 | 2001-03-22 02:32:48 +0000 | [diff] [blame] | 3322 | { |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 3323 | PyFrameObject *current_frame = PyEval_GetFrame(); |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 3324 | int result = cf->cf_flags != 0; |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 3325 | |
| 3326 | if (current_frame != NULL) { |
| 3327 | const int codeflags = current_frame->f_code->co_flags; |
Tim Peters | e2c18e9 | 2001-08-17 20:47:47 +0000 | [diff] [blame] | 3328 | const int compilerflags = codeflags & PyCF_MASK; |
| 3329 | if (compilerflags) { |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 3330 | result = 1; |
Tim Peters | e2c18e9 | 2001-08-17 20:47:47 +0000 | [diff] [blame] | 3331 | cf->cf_flags |= compilerflags; |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 3332 | } |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 3333 | #if 0 /* future keyword */ |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 3334 | if (codeflags & CO_GENERATOR_ALLOWED) { |
| 3335 | result = 1; |
| 3336 | cf->cf_flags |= CO_GENERATOR_ALLOWED; |
| 3337 | } |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 3338 | #endif |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 3339 | } |
| 3340 | return result; |
Jeremy Hylton | 061d106 | 2001-03-22 02:32:48 +0000 | [diff] [blame] | 3341 | } |
| 3342 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3343 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3344 | /* External interface to call any callable object. |
| 3345 | The arg must be a tuple or NULL. */ |
Guido van Rossum | 83bf35c | 1991-07-27 21:32:34 +0000 | [diff] [blame] | 3346 | |
Guido van Rossum | d7ed683 | 1997-08-30 15:02:50 +0000 | [diff] [blame] | 3347 | #undef PyEval_CallObject |
| 3348 | /* for backward compatibility: export this interface */ |
| 3349 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3350 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3351 | PyEval_CallObject(PyObject *func, PyObject *arg) |
Guido van Rossum | 83bf35c | 1991-07-27 21:32:34 +0000 | [diff] [blame] | 3352 | { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3353 | return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3354 | } |
Guido van Rossum | d7ed683 | 1997-08-30 15:02:50 +0000 | [diff] [blame] | 3355 | #define PyEval_CallObject(func,arg) \ |
| 3356 | PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL) |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 3357 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3358 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3359 | PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3360 | { |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3361 | PyObject *result; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3362 | |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3363 | if (arg == NULL) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3364 | arg = PyTuple_New(0); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3365 | if (arg == NULL) |
| 3366 | return NULL; |
| 3367 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3368 | else if (!PyTuple_Check(arg)) { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3369 | PyErr_SetString(PyExc_TypeError, |
| 3370 | "argument list must be a tuple"); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3371 | return NULL; |
| 3372 | } |
| 3373 | else |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3374 | Py_INCREF(arg); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3375 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3376 | if (kw != NULL && !PyDict_Check(kw)) { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3377 | PyErr_SetString(PyExc_TypeError, |
| 3378 | "keyword list must be a dictionary"); |
Guido van Rossum | 25826c9 | 2000-04-21 21:17:39 +0000 | [diff] [blame] | 3379 | Py_DECREF(arg); |
Guido van Rossum | e3e61c1 | 1995-08-04 04:14:47 +0000 | [diff] [blame] | 3380 | return NULL; |
| 3381 | } |
| 3382 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3383 | result = PyObject_Call(func, arg, kw); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3384 | Py_DECREF(arg); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3385 | return result; |
| 3386 | } |
| 3387 | |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 3388 | const char * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3389 | PyEval_GetFuncName(PyObject *func) |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 3390 | { |
| 3391 | if (PyMethod_Check(func)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3392 | return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func)); |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 3393 | else if (PyFunction_Check(func)) |
| 3394 | return PyString_AsString(((PyFunctionObject*)func)->func_name); |
| 3395 | else if (PyCFunction_Check(func)) |
| 3396 | return ((PyCFunctionObject*)func)->m_ml->ml_name; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 3397 | else |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 3398 | return func->ob_type->tp_name; |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 3399 | } |
| 3400 | |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 3401 | const char * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3402 | PyEval_GetFuncDesc(PyObject *func) |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 3403 | { |
| 3404 | if (PyMethod_Check(func)) |
| 3405 | return "()"; |
| 3406 | else if (PyFunction_Check(func)) |
| 3407 | return "()"; |
| 3408 | else if (PyCFunction_Check(func)) |
| 3409 | return "()"; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 3410 | else |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 3411 | return " object"; |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 3412 | } |
| 3413 | |
Neal Norwitz | addfe0c | 2002-11-10 14:33:26 +0000 | [diff] [blame] | 3414 | static void |
Jeremy Hylton | 192690e | 2002-08-16 18:36:11 +0000 | [diff] [blame] | 3415 | err_args(PyObject *func, int flags, int nargs) |
| 3416 | { |
| 3417 | if (flags & METH_NOARGS) |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 3418 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 86c659a | 2002-08-23 14:11:35 +0000 | [diff] [blame] | 3419 | "%.200s() takes no arguments (%d given)", |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 3420 | ((PyCFunctionObject *)func)->m_ml->ml_name, |
Jeremy Hylton | 192690e | 2002-08-16 18:36:11 +0000 | [diff] [blame] | 3421 | nargs); |
| 3422 | else |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 3423 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 86c659a | 2002-08-23 14:11:35 +0000 | [diff] [blame] | 3424 | "%.200s() takes exactly one argument (%d given)", |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 3425 | ((PyCFunctionObject *)func)->m_ml->ml_name, |
Jeremy Hylton | 192690e | 2002-08-16 18:36:11 +0000 | [diff] [blame] | 3426 | nargs); |
| 3427 | } |
| 3428 | |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 3429 | #define C_TRACE(x, call) \ |
Nicholas Bastin | d858a77 | 2004-06-25 23:31:06 +0000 | [diff] [blame] | 3430 | if (tstate->use_tracing && tstate->c_profilefunc) { \ |
| 3431 | if (call_trace(tstate->c_profilefunc, \ |
| 3432 | tstate->c_profileobj, \ |
| 3433 | tstate->frame, PyTrace_C_CALL, \ |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 3434 | func)) { \ |
| 3435 | x = NULL; \ |
| 3436 | } \ |
| 3437 | else { \ |
| 3438 | x = call; \ |
| 3439 | if (tstate->c_profilefunc != NULL) { \ |
| 3440 | if (x == NULL) { \ |
| 3441 | call_trace_protected(tstate->c_profilefunc, \ |
| 3442 | tstate->c_profileobj, \ |
| 3443 | tstate->frame, PyTrace_C_EXCEPTION, \ |
| 3444 | func); \ |
| 3445 | /* XXX should pass (type, value, tb) */ \ |
| 3446 | } else { \ |
| 3447 | if (call_trace(tstate->c_profilefunc, \ |
| 3448 | tstate->c_profileobj, \ |
| 3449 | tstate->frame, PyTrace_C_RETURN, \ |
| 3450 | func)) { \ |
| 3451 | Py_DECREF(x); \ |
| 3452 | x = NULL; \ |
| 3453 | } \ |
| 3454 | } \ |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 3455 | } \ |
Nicholas Bastin | d858a77 | 2004-06-25 23:31:06 +0000 | [diff] [blame] | 3456 | } \ |
| 3457 | } else { \ |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 3458 | x = call; \ |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 3459 | } |
| 3460 | |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 3461 | static PyObject * |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 3462 | call_function(PyObject ***pp_stack, int oparg |
| 3463 | #ifdef WITH_TSC |
| 3464 | , uint64* pintr0, uint64* pintr1 |
| 3465 | #endif |
| 3466 | ) |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 3467 | { |
| 3468 | int na = oparg & 0xff; |
| 3469 | int nk = (oparg>>8) & 0xff; |
| 3470 | int n = na + 2 * nk; |
| 3471 | PyObject **pfunc = (*pp_stack) - n - 1; |
| 3472 | PyObject *func = *pfunc; |
| 3473 | PyObject *x, *w; |
| 3474 | |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 3475 | /* Always dispatch PyCFunction first, because these are |
| 3476 | presumed to be the most frequent callable object. |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 3477 | */ |
| 3478 | if (PyCFunction_Check(func) && nk == 0) { |
| 3479 | int flags = PyCFunction_GET_FLAGS(func); |
Nicholas Bastin | d858a77 | 2004-06-25 23:31:06 +0000 | [diff] [blame] | 3480 | PyThreadState *tstate = PyThreadState_GET(); |
Raymond Hettinger | a7f56bc | 2004-06-26 04:34:33 +0000 | [diff] [blame] | 3481 | |
| 3482 | PCALL(PCALL_CFUNCTION); |
Jeremy Hylton | 192690e | 2002-08-16 18:36:11 +0000 | [diff] [blame] | 3483 | if (flags & (METH_NOARGS | METH_O)) { |
| 3484 | PyCFunction meth = PyCFunction_GET_FUNCTION(func); |
| 3485 | PyObject *self = PyCFunction_GET_SELF(func); |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 3486 | if (flags & METH_NOARGS && na == 0) { |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 3487 | C_TRACE(x, (*meth)(self,NULL)); |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 3488 | } |
Jeremy Hylton | 192690e | 2002-08-16 18:36:11 +0000 | [diff] [blame] | 3489 | else if (flags & METH_O && na == 1) { |
| 3490 | PyObject *arg = EXT_POP(*pp_stack); |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 3491 | C_TRACE(x, (*meth)(self,arg)); |
Jeremy Hylton | 192690e | 2002-08-16 18:36:11 +0000 | [diff] [blame] | 3492 | Py_DECREF(arg); |
| 3493 | } |
| 3494 | else { |
| 3495 | err_args(func, flags, na); |
| 3496 | x = NULL; |
| 3497 | } |
| 3498 | } |
| 3499 | else { |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 3500 | PyObject *callargs; |
| 3501 | callargs = load_args(pp_stack, na); |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 3502 | READ_TIMESTAMP(*pintr0); |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 3503 | C_TRACE(x, PyCFunction_Call(func,callargs,NULL)); |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 3504 | READ_TIMESTAMP(*pintr1); |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 3505 | Py_XDECREF(callargs); |
| 3506 | } |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 3507 | } else { |
| 3508 | if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) { |
| 3509 | /* optimize access to bound methods */ |
| 3510 | PyObject *self = PyMethod_GET_SELF(func); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 3511 | PCALL(PCALL_METHOD); |
| 3512 | PCALL(PCALL_BOUND_METHOD); |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 3513 | Py_INCREF(self); |
| 3514 | func = PyMethod_GET_FUNCTION(func); |
| 3515 | Py_INCREF(func); |
| 3516 | Py_DECREF(*pfunc); |
| 3517 | *pfunc = self; |
| 3518 | na++; |
| 3519 | n++; |
| 3520 | } else |
| 3521 | Py_INCREF(func); |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 3522 | READ_TIMESTAMP(*pintr0); |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 3523 | if (PyFunction_Check(func)) |
| 3524 | x = fast_function(func, pp_stack, n, na, nk); |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 3525 | else |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 3526 | x = do_call(func, pp_stack, na, nk); |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 3527 | READ_TIMESTAMP(*pintr1); |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 3528 | Py_DECREF(func); |
| 3529 | } |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 3530 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3531 | /* Clear the stack of the function object. Also removes |
| 3532 | the arguments in case they weren't consumed already |
| 3533 | (fast_function() and err_args() leave them on the stack). |
Thomas Wouters | 7f59732 | 2006-03-01 05:32:33 +0000 | [diff] [blame] | 3534 | */ |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 3535 | while ((*pp_stack) > pfunc) { |
| 3536 | w = EXT_POP(*pp_stack); |
| 3537 | Py_DECREF(w); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 3538 | PCALL(PCALL_POP); |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 3539 | } |
| 3540 | return x; |
| 3541 | } |
| 3542 | |
Jeremy Hylton | 192690e | 2002-08-16 18:36:11 +0000 | [diff] [blame] | 3543 | /* The fast_function() function optimize calls for which no argument |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3544 | tuple is necessary; the objects are passed directly from the stack. |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 3545 | For the simplest case -- a function that takes only positional |
| 3546 | arguments and is called with only positional arguments -- it |
| 3547 | inlines the most primitive frame setup code from |
| 3548 | PyEval_EvalCodeEx(), which vastly reduces the checks that must be |
| 3549 | done before evaluating the frame. |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3550 | */ |
| 3551 | |
| 3552 | static PyObject * |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3553 | 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] | 3554 | { |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 3555 | PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3556 | PyObject *globals = PyFunction_GET_GLOBALS(func); |
| 3557 | PyObject *argdefs = PyFunction_GET_DEFAULTS(func); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 3558 | PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3559 | PyObject **d = NULL; |
| 3560 | int nd = 0; |
| 3561 | |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 3562 | PCALL(PCALL_FUNCTION); |
| 3563 | PCALL(PCALL_FAST_FUNCTION); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 3564 | if (argdefs == NULL && co->co_argcount == n && |
| 3565 | co->co_kwonlyargcount == 0 && nk==0 && |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 3566 | co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) { |
| 3567 | PyFrameObject *f; |
| 3568 | PyObject *retval = NULL; |
| 3569 | PyThreadState *tstate = PyThreadState_GET(); |
| 3570 | PyObject **fastlocals, **stack; |
| 3571 | int i; |
| 3572 | |
| 3573 | PCALL(PCALL_FASTER_FUNCTION); |
| 3574 | assert(globals != NULL); |
| 3575 | /* XXX Perhaps we should create a specialized |
| 3576 | PyFrame_New() that doesn't take locals, but does |
| 3577 | take builtins without sanity checking them. |
| 3578 | */ |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 3579 | assert(tstate != NULL); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 3580 | f = PyFrame_New(tstate, co, globals, NULL); |
| 3581 | if (f == NULL) |
| 3582 | return NULL; |
| 3583 | |
| 3584 | fastlocals = f->f_localsplus; |
| 3585 | stack = (*pp_stack) - n; |
| 3586 | |
| 3587 | for (i = 0; i < n; i++) { |
| 3588 | Py_INCREF(*stack); |
| 3589 | fastlocals[i] = *stack++; |
| 3590 | } |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 3591 | retval = PyEval_EvalFrameEx(f,0); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 3592 | ++tstate->recursion_depth; |
| 3593 | Py_DECREF(f); |
| 3594 | --tstate->recursion_depth; |
| 3595 | return retval; |
| 3596 | } |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3597 | if (argdefs != NULL) { |
| 3598 | d = &PyTuple_GET_ITEM(argdefs, 0); |
| 3599 | nd = ((PyTupleObject *)argdefs)->ob_size; |
| 3600 | } |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 3601 | return PyEval_EvalCodeEx(co, globals, |
| 3602 | (PyObject *)NULL, (*pp_stack)-n, na, |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 3603 | (*pp_stack)-2*nk, nk, d, nd, kwdefs, |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 3604 | PyFunction_GET_CLOSURE(func)); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3605 | } |
| 3606 | |
| 3607 | static PyObject * |
Ka-Ping Yee | 2057970 | 2001-01-15 22:14:16 +0000 | [diff] [blame] | 3608 | update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack, |
| 3609 | PyObject *func) |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3610 | { |
| 3611 | PyObject *kwdict = NULL; |
| 3612 | if (orig_kwdict == NULL) |
| 3613 | kwdict = PyDict_New(); |
| 3614 | else { |
| 3615 | kwdict = PyDict_Copy(orig_kwdict); |
| 3616 | Py_DECREF(orig_kwdict); |
| 3617 | } |
| 3618 | if (kwdict == NULL) |
| 3619 | return NULL; |
Raymond Hettinger | 5bed456 | 2004-04-10 23:34:17 +0000 | [diff] [blame] | 3620 | while (--nk >= 0) { |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3621 | int err; |
| 3622 | PyObject *value = EXT_POP(*pp_stack); |
| 3623 | PyObject *key = EXT_POP(*pp_stack); |
| 3624 | if (PyDict_GetItem(kwdict, key) != NULL) { |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3625 | PyErr_Format(PyExc_TypeError, |
Ka-Ping Yee | 2057970 | 2001-01-15 22:14:16 +0000 | [diff] [blame] | 3626 | "%.200s%s got multiple values " |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 3627 | "for keyword argument '%.200s'", |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3628 | PyEval_GetFuncName(func), |
| 3629 | PyEval_GetFuncDesc(func), |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 3630 | PyString_AsString(key)); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3631 | Py_DECREF(key); |
| 3632 | Py_DECREF(value); |
| 3633 | Py_DECREF(kwdict); |
| 3634 | return NULL; |
| 3635 | } |
| 3636 | err = PyDict_SetItem(kwdict, key, value); |
| 3637 | Py_DECREF(key); |
| 3638 | Py_DECREF(value); |
| 3639 | if (err) { |
| 3640 | Py_DECREF(kwdict); |
| 3641 | return NULL; |
| 3642 | } |
| 3643 | } |
| 3644 | return kwdict; |
| 3645 | } |
| 3646 | |
| 3647 | static PyObject * |
| 3648 | update_star_args(int nstack, int nstar, PyObject *stararg, |
| 3649 | PyObject ***pp_stack) |
| 3650 | { |
| 3651 | PyObject *callargs, *w; |
| 3652 | |
| 3653 | callargs = PyTuple_New(nstack + nstar); |
| 3654 | if (callargs == NULL) { |
| 3655 | return NULL; |
| 3656 | } |
| 3657 | if (nstar) { |
| 3658 | int i; |
| 3659 | for (i = 0; i < nstar; i++) { |
| 3660 | PyObject *a = PyTuple_GET_ITEM(stararg, i); |
| 3661 | Py_INCREF(a); |
| 3662 | PyTuple_SET_ITEM(callargs, nstack + i, a); |
| 3663 | } |
| 3664 | } |
Raymond Hettinger | 5bed456 | 2004-04-10 23:34:17 +0000 | [diff] [blame] | 3665 | while (--nstack >= 0) { |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3666 | w = EXT_POP(*pp_stack); |
| 3667 | PyTuple_SET_ITEM(callargs, nstack, w); |
| 3668 | } |
| 3669 | return callargs; |
| 3670 | } |
| 3671 | |
| 3672 | static PyObject * |
| 3673 | load_args(PyObject ***pp_stack, int na) |
| 3674 | { |
| 3675 | PyObject *args = PyTuple_New(na); |
| 3676 | PyObject *w; |
| 3677 | |
| 3678 | if (args == NULL) |
| 3679 | return NULL; |
Raymond Hettinger | 5bed456 | 2004-04-10 23:34:17 +0000 | [diff] [blame] | 3680 | while (--na >= 0) { |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3681 | w = EXT_POP(*pp_stack); |
| 3682 | PyTuple_SET_ITEM(args, na, w); |
| 3683 | } |
| 3684 | return args; |
| 3685 | } |
| 3686 | |
| 3687 | static PyObject * |
| 3688 | do_call(PyObject *func, PyObject ***pp_stack, int na, int nk) |
| 3689 | { |
| 3690 | PyObject *callargs = NULL; |
| 3691 | PyObject *kwdict = NULL; |
| 3692 | PyObject *result = NULL; |
| 3693 | |
| 3694 | if (nk > 0) { |
Ka-Ping Yee | 2057970 | 2001-01-15 22:14:16 +0000 | [diff] [blame] | 3695 | kwdict = update_keyword_args(NULL, nk, pp_stack, func); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3696 | if (kwdict == NULL) |
| 3697 | goto call_fail; |
| 3698 | } |
| 3699 | callargs = load_args(pp_stack, na); |
| 3700 | if (callargs == NULL) |
| 3701 | goto call_fail; |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 3702 | #ifdef CALL_PROFILE |
| 3703 | /* At this point, we have to look at the type of func to |
| 3704 | update the call stats properly. Do it here so as to avoid |
| 3705 | exposing the call stats machinery outside ceval.c |
| 3706 | */ |
| 3707 | if (PyFunction_Check(func)) |
| 3708 | PCALL(PCALL_FUNCTION); |
| 3709 | else if (PyMethod_Check(func)) |
| 3710 | PCALL(PCALL_METHOD); |
| 3711 | else if (PyType_Check(func)) |
| 3712 | PCALL(PCALL_TYPE); |
| 3713 | else |
| 3714 | PCALL(PCALL_OTHER); |
| 3715 | #endif |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3716 | result = PyObject_Call(func, callargs, kwdict); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3717 | call_fail: |
| 3718 | Py_XDECREF(callargs); |
| 3719 | Py_XDECREF(kwdict); |
| 3720 | return result; |
| 3721 | } |
| 3722 | |
| 3723 | static PyObject * |
| 3724 | ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk) |
| 3725 | { |
| 3726 | int nstar = 0; |
| 3727 | PyObject *callargs = NULL; |
| 3728 | PyObject *stararg = NULL; |
| 3729 | PyObject *kwdict = NULL; |
| 3730 | PyObject *result = NULL; |
| 3731 | |
| 3732 | if (flags & CALL_FLAG_KW) { |
| 3733 | kwdict = EXT_POP(*pp_stack); |
| 3734 | if (!(kwdict && PyDict_Check(kwdict))) { |
Ka-Ping Yee | 2057970 | 2001-01-15 22:14:16 +0000 | [diff] [blame] | 3735 | PyErr_Format(PyExc_TypeError, |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 3736 | "%s%s argument after ** " |
| 3737 | "must be a dictionary", |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3738 | PyEval_GetFuncName(func), |
| 3739 | PyEval_GetFuncDesc(func)); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3740 | goto ext_call_fail; |
| 3741 | } |
| 3742 | } |
| 3743 | if (flags & CALL_FLAG_VAR) { |
| 3744 | stararg = EXT_POP(*pp_stack); |
| 3745 | if (!PyTuple_Check(stararg)) { |
| 3746 | PyObject *t = NULL; |
| 3747 | t = PySequence_Tuple(stararg); |
| 3748 | if (t == NULL) { |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 3749 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 3750 | PyErr_Format(PyExc_TypeError, |
| 3751 | "%s%s argument after * " |
| 3752 | "must be a sequence", |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3753 | PyEval_GetFuncName(func), |
| 3754 | PyEval_GetFuncDesc(func)); |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 3755 | } |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3756 | goto ext_call_fail; |
| 3757 | } |
| 3758 | Py_DECREF(stararg); |
| 3759 | stararg = t; |
| 3760 | } |
| 3761 | nstar = PyTuple_GET_SIZE(stararg); |
| 3762 | } |
| 3763 | if (nk > 0) { |
Ka-Ping Yee | 2057970 | 2001-01-15 22:14:16 +0000 | [diff] [blame] | 3764 | kwdict = update_keyword_args(kwdict, nk, pp_stack, func); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3765 | if (kwdict == NULL) |
| 3766 | goto ext_call_fail; |
| 3767 | } |
| 3768 | callargs = update_star_args(na, nstar, stararg, pp_stack); |
| 3769 | if (callargs == NULL) |
| 3770 | goto ext_call_fail; |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 3771 | #ifdef CALL_PROFILE |
| 3772 | /* At this point, we have to look at the type of func to |
| 3773 | update the call stats properly. Do it here so as to avoid |
| 3774 | exposing the call stats machinery outside ceval.c |
| 3775 | */ |
| 3776 | if (PyFunction_Check(func)) |
| 3777 | PCALL(PCALL_FUNCTION); |
| 3778 | else if (PyMethod_Check(func)) |
| 3779 | PCALL(PCALL_METHOD); |
| 3780 | else if (PyType_Check(func)) |
| 3781 | PCALL(PCALL_TYPE); |
| 3782 | else |
| 3783 | PCALL(PCALL_OTHER); |
| 3784 | #endif |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3785 | result = PyObject_Call(func, callargs, kwdict); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3786 | ext_call_fail: |
| 3787 | Py_XDECREF(callargs); |
| 3788 | Py_XDECREF(kwdict); |
| 3789 | Py_XDECREF(stararg); |
| 3790 | return result; |
| 3791 | } |
| 3792 | |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 3793 | /* Extract a slice index from a PyInt or PyLong or an object with the |
| 3794 | nb_index slot defined, and store in *pi. |
| 3795 | Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX, |
| 3796 | 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] | 3797 | Return 0 on error, 1 on success. |
Tim Peters | cb479e7 | 2001-12-16 19:11:44 +0000 | [diff] [blame] | 3798 | */ |
Tim Peters | b519638 | 2001-12-16 19:44:20 +0000 | [diff] [blame] | 3799 | /* Note: If v is NULL, return success without storing into *pi. This |
| 3800 | is because_PyEval_SliceIndex() is called by apply_slice(), which can be |
| 3801 | 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] | 3802 | */ |
Guido van Rossum | 20c6add | 2000-05-08 14:06:50 +0000 | [diff] [blame] | 3803 | int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3804 | _PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3805 | { |
Tim Peters | b519638 | 2001-12-16 19:44:20 +0000 | [diff] [blame] | 3806 | if (v != NULL) { |
Martin v. Löwis | dde99d2 | 2006-02-17 15:57:41 +0000 | [diff] [blame] | 3807 | Py_ssize_t x; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3808 | if (PyInt_CheckExact(v)) { |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 3809 | /* XXX(nnorwitz): I think PyInt_AS_LONG is correct, |
| 3810 | however, it looks like it should be AsSsize_t. |
| 3811 | There should be a comment here explaining why. |
| 3812 | */ |
| 3813 | x = PyInt_AS_LONG(v); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3814 | } |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 3815 | else if (PyIndex_Check(v)) { |
| 3816 | x = PyNumber_AsSsize_t(v, NULL); |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 3817 | if (x == -1 && PyErr_Occurred()) |
| 3818 | return 0; |
| 3819 | } |
| 3820 | else { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3821 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 3822 | "slice indices must be integers or " |
| 3823 | "None or have an __index__ method"); |
Guido van Rossum | 20c6add | 2000-05-08 14:06:50 +0000 | [diff] [blame] | 3824 | return 0; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3825 | } |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3826 | *pi = x; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3827 | } |
Guido van Rossum | 20c6add | 2000-05-08 14:06:50 +0000 | [diff] [blame] | 3828 | return 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3829 | } |
| 3830 | |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 3831 | #undef ISINDEX |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 3832 | #define ISINDEX(x) ((x) == NULL || \ |
| 3833 | PyInt_Check(x) || PyLong_Check(x) || PyIndex_Check(x)) |
Guido van Rossum | 50d756e | 2001-08-18 17:43:36 +0000 | [diff] [blame] | 3834 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3835 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3836 | 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] | 3837 | { |
Guido van Rossum | 50d756e | 2001-08-18 17:43:36 +0000 | [diff] [blame] | 3838 | PyTypeObject *tp = u->ob_type; |
| 3839 | PySequenceMethods *sq = tp->tp_as_sequence; |
| 3840 | |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 3841 | if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) { |
Martin v. Löwis | dde99d2 | 2006-02-17 15:57:41 +0000 | [diff] [blame] | 3842 | Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX; |
Guido van Rossum | 50d756e | 2001-08-18 17:43:36 +0000 | [diff] [blame] | 3843 | if (!_PyEval_SliceIndex(v, &ilow)) |
| 3844 | return NULL; |
| 3845 | if (!_PyEval_SliceIndex(w, &ihigh)) |
| 3846 | return NULL; |
| 3847 | return PySequence_GetSlice(u, ilow, ihigh); |
| 3848 | } |
| 3849 | else { |
| 3850 | PyObject *slice = PySlice_New(v, w, NULL); |
Guido van Rossum | 354797c | 2001-12-03 19:45:06 +0000 | [diff] [blame] | 3851 | if (slice != NULL) { |
| 3852 | PyObject *res = PyObject_GetItem(u, slice); |
| 3853 | Py_DECREF(slice); |
| 3854 | return res; |
| 3855 | } |
Guido van Rossum | 50d756e | 2001-08-18 17:43:36 +0000 | [diff] [blame] | 3856 | else |
| 3857 | return NULL; |
| 3858 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3859 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3860 | |
| 3861 | static int |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3862 | assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x) |
| 3863 | /* u[v:w] = x */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3864 | { |
Guido van Rossum | 50d756e | 2001-08-18 17:43:36 +0000 | [diff] [blame] | 3865 | PyTypeObject *tp = u->ob_type; |
| 3866 | PySequenceMethods *sq = tp->tp_as_sequence; |
| 3867 | |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 3868 | if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) { |
Martin v. Löwis | dde99d2 | 2006-02-17 15:57:41 +0000 | [diff] [blame] | 3869 | Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX; |
Guido van Rossum | 50d756e | 2001-08-18 17:43:36 +0000 | [diff] [blame] | 3870 | if (!_PyEval_SliceIndex(v, &ilow)) |
| 3871 | return -1; |
| 3872 | if (!_PyEval_SliceIndex(w, &ihigh)) |
| 3873 | return -1; |
| 3874 | if (x == NULL) |
| 3875 | return PySequence_DelSlice(u, ilow, ihigh); |
| 3876 | else |
| 3877 | return PySequence_SetSlice(u, ilow, ihigh, x); |
| 3878 | } |
| 3879 | else { |
| 3880 | PyObject *slice = PySlice_New(v, w, NULL); |
| 3881 | if (slice != NULL) { |
Guido van Rossum | 354797c | 2001-12-03 19:45:06 +0000 | [diff] [blame] | 3882 | int res; |
Guido van Rossum | 50d756e | 2001-08-18 17:43:36 +0000 | [diff] [blame] | 3883 | if (x != NULL) |
Guido van Rossum | 354797c | 2001-12-03 19:45:06 +0000 | [diff] [blame] | 3884 | res = PyObject_SetItem(u, slice, x); |
Guido van Rossum | 50d756e | 2001-08-18 17:43:36 +0000 | [diff] [blame] | 3885 | else |
Guido van Rossum | 354797c | 2001-12-03 19:45:06 +0000 | [diff] [blame] | 3886 | res = PyObject_DelItem(u, slice); |
| 3887 | Py_DECREF(slice); |
| 3888 | return res; |
Guido van Rossum | 50d756e | 2001-08-18 17:43:36 +0000 | [diff] [blame] | 3889 | } |
| 3890 | else |
| 3891 | return -1; |
| 3892 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3893 | } |
| 3894 | |
Brett Cannon | 3959046 | 2007-02-26 22:01:14 +0000 | [diff] [blame] | 3895 | #define CANNOT_CATCH_MSG "catching classes that do not inherit from"\ |
| 3896 | "BaseException is not allowed" |
Brett Cannon | f74225d | 2007-02-26 21:10:16 +0000 | [diff] [blame] | 3897 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3898 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3899 | cmp_outcome(int op, register PyObject *v, register PyObject *w) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3900 | { |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3901 | int res = 0; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3902 | switch (op) { |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 3903 | case PyCmp_IS: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3904 | res = (v == w); |
Raymond Hettinger | 4bad9ba | 2003-01-19 05:08:13 +0000 | [diff] [blame] | 3905 | break; |
| 3906 | case PyCmp_IS_NOT: |
| 3907 | res = (v != w); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3908 | break; |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 3909 | case PyCmp_IN: |
Raymond Hettinger | 4bad9ba | 2003-01-19 05:08:13 +0000 | [diff] [blame] | 3910 | res = PySequence_Contains(w, v); |
| 3911 | if (res < 0) |
| 3912 | return NULL; |
| 3913 | break; |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 3914 | case PyCmp_NOT_IN: |
Guido van Rossum | 7e33c6e | 1998-05-22 00:52:29 +0000 | [diff] [blame] | 3915 | res = PySequence_Contains(w, v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3916 | if (res < 0) |
| 3917 | return NULL; |
Raymond Hettinger | 4bad9ba | 2003-01-19 05:08:13 +0000 | [diff] [blame] | 3918 | res = !res; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3919 | break; |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 3920 | case PyCmp_EXC_MATCH: |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 3921 | if (PyTuple_Check(w)) { |
| 3922 | Py_ssize_t i, length; |
| 3923 | length = PyTuple_Size(w); |
| 3924 | for (i = 0; i < length; i += 1) { |
| 3925 | PyObject *exc = PyTuple_GET_ITEM(w, i); |
Brett Cannon | 3959046 | 2007-02-26 22:01:14 +0000 | [diff] [blame] | 3926 | if (!PyExceptionClass_Check(exc)) { |
| 3927 | PyErr_SetString(PyExc_TypeError, |
| 3928 | CANNOT_CATCH_MSG); |
Brett Cannon | f74225d | 2007-02-26 21:10:16 +0000 | [diff] [blame] | 3929 | return NULL; |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 3930 | } |
| 3931 | } |
| 3932 | } |
| 3933 | else { |
Brett Cannon | 3959046 | 2007-02-26 22:01:14 +0000 | [diff] [blame] | 3934 | if (!PyExceptionClass_Check(w)) { |
| 3935 | PyErr_SetString(PyExc_TypeError, |
| 3936 | CANNOT_CATCH_MSG); |
Brett Cannon | f74225d | 2007-02-26 21:10:16 +0000 | [diff] [blame] | 3937 | return NULL; |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 3938 | } |
| 3939 | } |
Barry Warsaw | 4249f54 | 1997-08-22 21:26:19 +0000 | [diff] [blame] | 3940 | res = PyErr_GivenExceptionMatches(v, w); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3941 | break; |
| 3942 | default: |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3943 | return PyObject_RichCompare(v, w, op); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3944 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3945 | v = res ? Py_True : Py_False; |
| 3946 | Py_INCREF(v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3947 | return v; |
| 3948 | } |
| 3949 | |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3950 | static PyObject * |
| 3951 | import_from(PyObject *v, PyObject *name) |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 3952 | { |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 3953 | PyObject *x; |
| 3954 | |
| 3955 | x = PyObject_GetAttr(v, name); |
| 3956 | if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) { |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3957 | PyErr_Format(PyExc_ImportError, |
| 3958 | "cannot import name %.230s", |
| 3959 | PyString_AsString(name)); |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 3960 | } |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3961 | return x; |
| 3962 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3963 | |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3964 | static int |
| 3965 | import_all_from(PyObject *locals, PyObject *v) |
| 3966 | { |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 3967 | PyObject *all = PyObject_GetAttrString(v, "__all__"); |
| 3968 | PyObject *dict, *name, *value; |
| 3969 | int skip_leading_underscores = 0; |
| 3970 | int pos, err; |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3971 | |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 3972 | if (all == NULL) { |
| 3973 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 3974 | return -1; /* Unexpected error */ |
| 3975 | PyErr_Clear(); |
| 3976 | dict = PyObject_GetAttrString(v, "__dict__"); |
| 3977 | if (dict == NULL) { |
| 3978 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 3979 | return -1; |
| 3980 | PyErr_SetString(PyExc_ImportError, |
| 3981 | "from-import-* object has no __dict__ and no __all__"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3982 | return -1; |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 3983 | } |
| 3984 | all = PyMapping_Keys(dict); |
| 3985 | Py_DECREF(dict); |
| 3986 | if (all == NULL) |
| 3987 | return -1; |
| 3988 | skip_leading_underscores = 1; |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 3989 | } |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 3990 | |
| 3991 | for (pos = 0, err = 0; ; pos++) { |
| 3992 | name = PySequence_GetItem(all, pos); |
| 3993 | if (name == NULL) { |
| 3994 | if (!PyErr_ExceptionMatches(PyExc_IndexError)) |
| 3995 | err = -1; |
| 3996 | else |
| 3997 | PyErr_Clear(); |
| 3998 | break; |
| 3999 | } |
| 4000 | if (skip_leading_underscores && |
| 4001 | PyString_Check(name) && |
| 4002 | PyString_AS_STRING(name)[0] == '_') |
| 4003 | { |
| 4004 | Py_DECREF(name); |
| 4005 | continue; |
| 4006 | } |
| 4007 | value = PyObject_GetAttr(v, name); |
| 4008 | if (value == NULL) |
| 4009 | err = -1; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4010 | else if (PyDict_CheckExact(locals)) |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 4011 | err = PyDict_SetItem(locals, name, value); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4012 | else |
| 4013 | err = PyObject_SetItem(locals, name, value); |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 4014 | Py_DECREF(name); |
| 4015 | Py_XDECREF(value); |
| 4016 | if (err != 0) |
| 4017 | break; |
| 4018 | } |
| 4019 | Py_DECREF(all); |
| 4020 | return err; |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 4021 | } |
| 4022 | |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4023 | static void |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 4024 | format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj) |
| 4025 | { |
| 4026 | char *obj_str; |
| 4027 | |
| 4028 | if (!obj) |
| 4029 | return; |
| 4030 | |
| 4031 | obj_str = PyString_AsString(obj); |
| 4032 | if (!obj_str) |
| 4033 | return; |
| 4034 | |
| 4035 | PyErr_Format(exc, format_str, obj_str); |
| 4036 | } |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 4037 | |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 4038 | static PyObject * |
| 4039 | string_concatenate(PyObject *v, PyObject *w, |
| 4040 | PyFrameObject *f, unsigned char *next_instr) |
| 4041 | { |
| 4042 | /* This function implements 'variable += expr' when both arguments |
| 4043 | are strings. */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4044 | Py_ssize_t v_len = PyString_GET_SIZE(v); |
| 4045 | Py_ssize_t w_len = PyString_GET_SIZE(w); |
| 4046 | Py_ssize_t new_len = v_len + w_len; |
| 4047 | if (new_len < 0) { |
| 4048 | PyErr_SetString(PyExc_OverflowError, |
| 4049 | "strings are too large to concat"); |
| 4050 | return NULL; |
| 4051 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4052 | |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 4053 | if (v->ob_refcnt == 2) { |
| 4054 | /* In the common case, there are 2 references to the value |
| 4055 | * stored in 'variable' when the += is performed: one on the |
| 4056 | * value stack (in 'v') and one still stored in the 'variable'. |
| 4057 | * We try to delete the variable now to reduce the refcnt to 1. |
| 4058 | */ |
| 4059 | switch (*next_instr) { |
| 4060 | case STORE_FAST: |
| 4061 | { |
| 4062 | int oparg = PEEKARG(); |
| 4063 | PyObject **fastlocals = f->f_localsplus; |
| 4064 | if (GETLOCAL(oparg) == v) |
| 4065 | SETLOCAL(oparg, NULL); |
| 4066 | break; |
| 4067 | } |
| 4068 | case STORE_DEREF: |
| 4069 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4070 | PyObject **freevars = f->f_localsplus + f->f_code->co_nlocals; |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 4071 | PyObject *c = freevars[PEEKARG()]; |
| 4072 | if (PyCell_GET(c) == v) |
| 4073 | PyCell_Set(c, NULL); |
| 4074 | break; |
| 4075 | } |
| 4076 | case STORE_NAME: |
| 4077 | { |
| 4078 | PyObject *names = f->f_code->co_names; |
| 4079 | PyObject *name = GETITEM(names, PEEKARG()); |
| 4080 | PyObject *locals = f->f_locals; |
| 4081 | if (PyDict_CheckExact(locals) && |
| 4082 | PyDict_GetItem(locals, name) == v) { |
| 4083 | if (PyDict_DelItem(locals, name) != 0) { |
| 4084 | PyErr_Clear(); |
| 4085 | } |
| 4086 | } |
| 4087 | break; |
| 4088 | } |
| 4089 | } |
| 4090 | } |
| 4091 | |
Armin Rigo | 618fbf5 | 2004-08-07 20:58:32 +0000 | [diff] [blame] | 4092 | if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) { |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 4093 | /* Now we own the last reference to 'v', so we can resize it |
| 4094 | * in-place. |
| 4095 | */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4096 | if (_PyString_Resize(&v, new_len) != 0) { |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 4097 | /* XXX if _PyString_Resize() fails, 'v' has been |
| 4098 | * deallocated so it cannot be put back into 'variable'. |
| 4099 | * The MemoryError is raised when there is no value in |
| 4100 | * 'variable', which might (very remotely) be a cause |
| 4101 | * of incompatibilities. |
| 4102 | */ |
| 4103 | return NULL; |
| 4104 | } |
| 4105 | /* copy 'w' into the newly allocated area of 'v' */ |
| 4106 | memcpy(PyString_AS_STRING(v) + v_len, |
| 4107 | PyString_AS_STRING(w), w_len); |
| 4108 | return v; |
| 4109 | } |
| 4110 | else { |
| 4111 | /* When in-place resizing is not an option. */ |
| 4112 | PyString_Concat(&v, w); |
| 4113 | return v; |
| 4114 | } |
| 4115 | } |
| 4116 | |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 4117 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 4118 | |
Skip Montanaro | f118cb1 | 2001-10-15 20:51:38 +0000 | [diff] [blame] | 4119 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4120 | getarray(long a[256]) |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 4121 | { |
| 4122 | int i; |
| 4123 | PyObject *l = PyList_New(256); |
| 4124 | if (l == NULL) return NULL; |
| 4125 | for (i = 0; i < 256; i++) { |
| 4126 | PyObject *x = PyInt_FromLong(a[i]); |
| 4127 | if (x == NULL) { |
| 4128 | Py_DECREF(l); |
| 4129 | return NULL; |
| 4130 | } |
| 4131 | PyList_SetItem(l, i, x); |
| 4132 | } |
| 4133 | for (i = 0; i < 256; i++) |
| 4134 | a[i] = 0; |
| 4135 | return l; |
| 4136 | } |
| 4137 | |
| 4138 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4139 | _Py_GetDXProfile(PyObject *self, PyObject *args) |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 4140 | { |
| 4141 | #ifndef DXPAIRS |
| 4142 | return getarray(dxp); |
| 4143 | #else |
| 4144 | int i; |
| 4145 | PyObject *l = PyList_New(257); |
| 4146 | if (l == NULL) return NULL; |
| 4147 | for (i = 0; i < 257; i++) { |
| 4148 | PyObject *x = getarray(dxpairs[i]); |
| 4149 | if (x == NULL) { |
| 4150 | Py_DECREF(l); |
| 4151 | return NULL; |
| 4152 | } |
| 4153 | PyList_SetItem(l, i, x); |
| 4154 | } |
| 4155 | return l; |
| 4156 | #endif |
| 4157 | } |
| 4158 | |
| 4159 | #endif |