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