Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2 | /* Execute compiled code */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4 | /* XXX TO DO: |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 5 | XXX speed up searching for keywords by using a dictionary |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 6 | XXX document it! |
| 7 | */ |
| 8 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 9 | /* enable more aggressive intra-module optimizations, where available */ |
Fredrik Lundh | 57640f5 | 2006-05-26 11:54:04 +0000 | [diff] [blame] | 10 | #define PY_LOCAL_AGGRESSIVE |
| 11 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 12 | #include "Python.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 13 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 14 | #include "code.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 15 | #include "frameobject.h" |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 16 | #include "eval.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 17 | #include "opcode.h" |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 18 | #include "structmember.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 19 | |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 20 | #include <ctype.h> |
| 21 | |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 22 | #ifndef WITH_TSC |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 23 | |
| 24 | #define READ_TIMESTAMP(var) |
| 25 | |
| 26 | #else |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 27 | |
| 28 | typedef unsigned long long uint64; |
| 29 | |
Michael W. Hudson | 800ba23 | 2004-08-12 18:19:17 +0000 | [diff] [blame] | 30 | #if defined(__ppc__) /* <- Don't know if this is the correct symbol; this |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 31 | section should work for GCC on any PowerPC |
| 32 | platform, irrespective of OS. |
| 33 | POWER? Who knows :-) */ |
Michael W. Hudson | 800ba23 | 2004-08-12 18:19:17 +0000 | [diff] [blame] | 34 | |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 35 | #define READ_TIMESTAMP(var) ppc_getcounter(&var) |
Michael W. Hudson | 800ba23 | 2004-08-12 18:19:17 +0000 | [diff] [blame] | 36 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 37 | static void |
Michael W. Hudson | 800ba23 | 2004-08-12 18:19:17 +0000 | [diff] [blame] | 38 | ppc_getcounter(uint64 *v) |
| 39 | { |
| 40 | register unsigned long tbu, tb, tbu2; |
| 41 | |
| 42 | loop: |
| 43 | asm volatile ("mftbu %0" : "=r" (tbu) ); |
| 44 | asm volatile ("mftb %0" : "=r" (tb) ); |
| 45 | asm volatile ("mftbu %0" : "=r" (tbu2)); |
| 46 | if (__builtin_expect(tbu != tbu2, 0)) goto loop; |
| 47 | |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 48 | /* The slightly peculiar way of writing the next lines is |
Michael W. Hudson | 800ba23 | 2004-08-12 18:19:17 +0000 | [diff] [blame] | 49 | compiled better by GCC than any other way I tried. */ |
| 50 | ((long*)(v))[0] = tbu; |
| 51 | ((long*)(v))[1] = tb; |
| 52 | } |
| 53 | |
Mark Dickinson | 504a151 | 2009-10-31 10:11:28 +0000 | [diff] [blame] | 54 | #elif defined(__i386__) |
| 55 | |
| 56 | /* 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] | 57 | |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 58 | #define READ_TIMESTAMP(val) \ |
| 59 | __asm__ __volatile__("rdtsc" : "=A" (val)) |
Michael W. Hudson | 800ba23 | 2004-08-12 18:19:17 +0000 | [diff] [blame] | 60 | |
Mark Dickinson | 504a151 | 2009-10-31 10:11:28 +0000 | [diff] [blame] | 61 | #elif defined(__x86_64__) |
| 62 | |
| 63 | /* for gcc/x86_64, the "A" constraint in DI mode means *either* rax *or* rdx; |
| 64 | not edx:eax as it does for i386. Since rdtsc puts its result in edx:eax |
| 65 | even in 64-bit mode, we need to use "a" and "d" for the lower and upper |
| 66 | 32-bit pieces of the result. */ |
| 67 | |
| 68 | #define READ_TIMESTAMP(val) \ |
| 69 | __asm__ __volatile__("rdtsc" : \ |
| 70 | "=a" (((int*)&(val))[0]), "=d" (((int*)&(val))[1])); |
| 71 | |
| 72 | |
| 73 | #else |
| 74 | |
| 75 | #error "Don't know how to implement timestamp counter for this architecture" |
| 76 | |
Michael W. Hudson | 800ba23 | 2004-08-12 18:19:17 +0000 | [diff] [blame] | 77 | #endif |
| 78 | |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 79 | 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] | 80 | uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1) |
| 81 | { |
| 82 | uint64 intr, inst, loop; |
| 83 | PyThreadState *tstate = PyThreadState_Get(); |
| 84 | if (!tstate->interp->tscdump) |
| 85 | return; |
| 86 | intr = intr1 - intr0; |
| 87 | inst = inst1 - inst0 - intr; |
| 88 | loop = loop1 - loop0 - intr; |
| 89 | fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n", |
| 90 | opcode, ticked, inst, loop); |
| 91 | } |
Michael W. Hudson | 800ba23 | 2004-08-12 18:19:17 +0000 | [diff] [blame] | 92 | |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 93 | #endif |
| 94 | |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 95 | /* Turn this on if your compiler chokes on the big switch: */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 96 | /* #define CASE_TOO_BIG 1 */ |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 97 | |
Guido van Rossum | 408027e | 1996-12-30 16:17:54 +0000 | [diff] [blame] | 98 | #ifdef Py_DEBUG |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 99 | /* For debugging the interpreter: */ |
| 100 | #define LLTRACE 1 /* Low-level trace feature */ |
| 101 | #define CHECKEXC 1 /* Double-check exception checking */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 102 | #endif |
| 103 | |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 104 | typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *); |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 105 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 106 | /* Forward declarations */ |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 107 | #ifdef WITH_TSC |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 108 | static PyObject * call_function(PyObject ***, int, uint64*, uint64*); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 109 | #else |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 110 | static PyObject * call_function(PyObject ***, int); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 111 | #endif |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 112 | static PyObject * fast_function(PyObject *, PyObject ***, int, int, int); |
| 113 | static PyObject * do_call(PyObject *, PyObject ***, int, int); |
| 114 | static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int); |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 115 | static PyObject * update_keyword_args(PyObject *, int, PyObject ***, |
| 116 | PyObject *); |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 117 | static PyObject * update_star_args(int, int, PyObject *, PyObject ***); |
| 118 | static PyObject * load_args(PyObject ***, int); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 119 | #define CALL_FLAG_VAR 1 |
| 120 | #define CALL_FLAG_KW 2 |
| 121 | |
Guido van Rossum | 0a066c0 | 1992-03-27 17:29:15 +0000 | [diff] [blame] | 122 | #ifdef LLTRACE |
Fredrik Lundh | 1b94940 | 2006-05-26 12:01:49 +0000 | [diff] [blame] | 123 | static int lltrace; |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 124 | static int prtrace(PyObject *, char *); |
Guido van Rossum | 0a066c0 | 1992-03-27 17:29:15 +0000 | [diff] [blame] | 125 | #endif |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 126 | static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *, |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 127 | int, PyObject *); |
Amaury Forgeot d'Arc | 0d75f09 | 2007-11-13 21:54:28 +0000 | [diff] [blame] | 128 | static int call_trace_protected(Py_tracefunc, PyObject *, |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 129 | PyFrameObject *, int, PyObject *); |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 130 | static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *); |
| 131 | static int maybe_call_line_trace(Py_tracefunc, PyObject *, |
Armin Rigo | bf57a14 | 2004-03-22 19:24:58 +0000 | [diff] [blame] | 132 | PyFrameObject *, int *, int *, int *); |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 133 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 134 | static PyObject * apply_slice(PyObject *, PyObject *, PyObject *); |
| 135 | static int assign_slice(PyObject *, PyObject *, |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 136 | PyObject *, PyObject *); |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 137 | static PyObject * cmp_outcome(int, PyObject *, PyObject *); |
| 138 | static PyObject * import_from(PyObject *, PyObject *); |
| 139 | static int import_all_from(PyObject *, PyObject *); |
| 140 | static PyObject * build_class(PyObject *, PyObject *, PyObject *); |
| 141 | static int exec_statement(PyFrameObject *, |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 142 | PyObject *, PyObject *, PyObject *); |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 143 | static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *); |
| 144 | static void reset_exc_info(PyThreadState *); |
| 145 | static void format_exc_check_arg(PyObject *, char *, PyObject *); |
| 146 | static PyObject * string_concatenate(PyObject *, PyObject *, |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 147 | PyFrameObject *, unsigned char *); |
Benjamin Peterson | e18ef19 | 2009-01-20 14:21:16 +0000 | [diff] [blame] | 148 | static PyObject * kwd_as_string(PyObject *); |
Benjamin Peterson | 1880d8b | 2009-05-25 13:13:44 +0000 | [diff] [blame] | 149 | static PyObject * special_lookup(PyObject *, char *, PyObject **); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 150 | |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 151 | #define NAME_ERROR_MSG \ |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 152 | "name '%.200s' is not defined" |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 153 | #define GLOBAL_NAME_ERROR_MSG \ |
| 154 | "global name '%.200s' is not defined" |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 155 | #define UNBOUNDLOCAL_ERROR_MSG \ |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 156 | "local variable '%.200s' referenced before assignment" |
Jeremy Hylton | c76770c | 2001-04-13 16:51:46 +0000 | [diff] [blame] | 157 | #define UNBOUNDFREE_ERROR_MSG \ |
| 158 | "free variable '%.200s' referenced before assignment" \ |
| 159 | " in enclosing scope" |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 160 | |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 161 | /* Dynamic execution profile */ |
| 162 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 163 | #ifdef DXPAIRS |
| 164 | static long dxpairs[257][256]; |
| 165 | #define dxp dxpairs[256] |
| 166 | #else |
| 167 | static long dxp[256]; |
| 168 | #endif |
| 169 | #endif |
| 170 | |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 171 | /* Function call profile */ |
| 172 | #ifdef CALL_PROFILE |
| 173 | #define PCALL_NUM 11 |
| 174 | static int pcall[PCALL_NUM]; |
| 175 | |
| 176 | #define PCALL_ALL 0 |
| 177 | #define PCALL_FUNCTION 1 |
| 178 | #define PCALL_FAST_FUNCTION 2 |
| 179 | #define PCALL_FASTER_FUNCTION 3 |
| 180 | #define PCALL_METHOD 4 |
| 181 | #define PCALL_BOUND_METHOD 5 |
| 182 | #define PCALL_CFUNCTION 6 |
| 183 | #define PCALL_TYPE 7 |
| 184 | #define PCALL_GENERATOR 8 |
| 185 | #define PCALL_OTHER 9 |
| 186 | #define PCALL_POP 10 |
| 187 | |
| 188 | /* Notes about the statistics |
| 189 | |
| 190 | PCALL_FAST stats |
| 191 | |
| 192 | FAST_FUNCTION means no argument tuple needs to be created. |
| 193 | FASTER_FUNCTION means that the fast-path frame setup code is used. |
| 194 | |
| 195 | If there is a method call where the call can be optimized by changing |
| 196 | the argument tuple and calling the function directly, it gets recorded |
| 197 | twice. |
| 198 | |
| 199 | As a result, the relationship among the statistics appears to be |
| 200 | PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD + |
| 201 | PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER |
| 202 | PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION |
| 203 | PCALL_METHOD > PCALL_BOUND_METHOD |
| 204 | */ |
| 205 | |
| 206 | #define PCALL(POS) pcall[POS]++ |
| 207 | |
| 208 | PyObject * |
| 209 | PyEval_GetCallStats(PyObject *self) |
| 210 | { |
Andrew M. Kuchling | 1f3ebe0 | 2006-10-27 13:22:46 +0000 | [diff] [blame] | 211 | return Py_BuildValue("iiiiiiiiiii", |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 212 | pcall[0], pcall[1], pcall[2], pcall[3], |
| 213 | pcall[4], pcall[5], pcall[6], pcall[7], |
Andrew M. Kuchling | 1f3ebe0 | 2006-10-27 13:22:46 +0000 | [diff] [blame] | 214 | pcall[8], pcall[9], pcall[10]); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 215 | } |
| 216 | #else |
| 217 | #define PCALL(O) |
| 218 | |
| 219 | PyObject * |
| 220 | PyEval_GetCallStats(PyObject *self) |
| 221 | { |
| 222 | Py_INCREF(Py_None); |
| 223 | return Py_None; |
| 224 | } |
| 225 | #endif |
| 226 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 227 | |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 228 | #ifdef WITH_THREAD |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 229 | |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 230 | #ifdef HAVE_ERRNO_H |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 231 | #include <errno.h> |
Guido van Rossum | 2571cc8 | 1999-04-07 16:07:23 +0000 | [diff] [blame] | 232 | #endif |
Guido van Rossum | 49b5606 | 1998-10-01 20:42:43 +0000 | [diff] [blame] | 233 | #include "pythread.h" |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 234 | |
Guido van Rossum | b8b6d0c | 2003-06-28 21:53:52 +0000 | [diff] [blame] | 235 | static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */ |
Kristján Valur Jónsson | 0e91938 | 2009-01-09 20:31:26 +0000 | [diff] [blame] | 236 | static PyThread_type_lock pending_lock = 0; /* for pending calls */ |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 237 | static long main_thread = 0; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 238 | |
Tim Peters | 7f468f2 | 2004-10-11 02:40:51 +0000 | [diff] [blame] | 239 | int |
| 240 | PyEval_ThreadsInitialized(void) |
| 241 | { |
| 242 | return interpreter_lock != 0; |
| 243 | } |
| 244 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 245 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 246 | PyEval_InitThreads(void) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 247 | { |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 248 | if (interpreter_lock) |
Sjoerd Mullender | ed59d20 | 1993-01-06 13:36:38 +0000 | [diff] [blame] | 249 | return; |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 250 | interpreter_lock = PyThread_allocate_lock(); |
| 251 | PyThread_acquire_lock(interpreter_lock, 1); |
| 252 | main_thread = PyThread_get_thread_ident(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 253 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 254 | |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 255 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 256 | PyEval_AcquireLock(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 257 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 258 | PyThread_acquire_lock(interpreter_lock, 1); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 262 | PyEval_ReleaseLock(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 263 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 264 | PyThread_release_lock(interpreter_lock); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 268 | PyEval_AcquireThread(PyThreadState *tstate) |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 269 | { |
| 270 | if (tstate == NULL) |
| 271 | Py_FatalError("PyEval_AcquireThread: NULL new thread state"); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 272 | /* Check someone has called PyEval_InitThreads() to create the lock */ |
| 273 | assert(interpreter_lock); |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 274 | PyThread_acquire_lock(interpreter_lock, 1); |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 275 | if (PyThreadState_Swap(tstate) != NULL) |
| 276 | Py_FatalError( |
| 277 | "PyEval_AcquireThread: non-NULL old thread state"); |
| 278 | } |
| 279 | |
| 280 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 281 | PyEval_ReleaseThread(PyThreadState *tstate) |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 282 | { |
| 283 | if (tstate == NULL) |
| 284 | Py_FatalError("PyEval_ReleaseThread: NULL thread state"); |
| 285 | if (PyThreadState_Swap(NULL) != tstate) |
| 286 | Py_FatalError("PyEval_ReleaseThread: wrong thread state"); |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 287 | PyThread_release_lock(interpreter_lock); |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 288 | } |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 289 | |
| 290 | /* This function is called from PyOS_AfterFork to ensure that newly |
| 291 | created child processes don't hold locks referring to threads which |
| 292 | are not running in the child process. (This could also be done using |
| 293 | pthread_atfork mechanism, at least for the pthreads implementation.) */ |
| 294 | |
| 295 | void |
| 296 | PyEval_ReInitThreads(void) |
| 297 | { |
Jesse Noller | 5e62ca4 | 2008-07-16 20:03:47 +0000 | [diff] [blame] | 298 | PyObject *threading, *result; |
| 299 | PyThreadState *tstate; |
| 300 | |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 301 | if (!interpreter_lock) |
| 302 | return; |
| 303 | /*XXX Can't use PyThread_free_lock here because it does too |
| 304 | much error-checking. Doing this cleanly would require |
| 305 | adding a new function to each thread_*.h. Instead, just |
| 306 | create a new lock and waste a little bit of memory */ |
| 307 | interpreter_lock = PyThread_allocate_lock(); |
Kristján Valur Jónsson | 0e91938 | 2009-01-09 20:31:26 +0000 | [diff] [blame] | 308 | pending_lock = PyThread_allocate_lock(); |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 309 | PyThread_acquire_lock(interpreter_lock, 1); |
| 310 | main_thread = PyThread_get_thread_ident(); |
Jesse Noller | 5e62ca4 | 2008-07-16 20:03:47 +0000 | [diff] [blame] | 311 | |
| 312 | /* Update the threading module with the new state. |
| 313 | */ |
| 314 | tstate = PyThreadState_GET(); |
| 315 | threading = PyMapping_GetItemString(tstate->interp->modules, |
| 316 | "threading"); |
| 317 | if (threading == NULL) { |
| 318 | /* threading not imported */ |
| 319 | PyErr_Clear(); |
| 320 | return; |
| 321 | } |
| 322 | result = PyObject_CallMethod(threading, "_after_fork", NULL); |
| 323 | if (result == NULL) |
| 324 | PyErr_WriteUnraisable(threading); |
| 325 | else |
| 326 | Py_DECREF(result); |
| 327 | Py_DECREF(threading); |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 328 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 329 | #endif |
| 330 | |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 331 | /* Functions save_thread and restore_thread are always defined so |
| 332 | dynamically loaded modules needn't be compiled separately for use |
| 333 | with and without threads: */ |
| 334 | |
Guido van Rossum | 2fca21f7 | 1997-07-18 23:56:58 +0000 | [diff] [blame] | 335 | PyThreadState * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 336 | PyEval_SaveThread(void) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 337 | { |
Guido van Rossum | b74eca9 | 1997-09-30 22:03:16 +0000 | [diff] [blame] | 338 | PyThreadState *tstate = PyThreadState_Swap(NULL); |
| 339 | if (tstate == NULL) |
| 340 | Py_FatalError("PyEval_SaveThread: NULL tstate"); |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 341 | #ifdef WITH_THREAD |
Guido van Rossum | b74eca9 | 1997-09-30 22:03:16 +0000 | [diff] [blame] | 342 | if (interpreter_lock) |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 343 | PyThread_release_lock(interpreter_lock); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 344 | #endif |
Guido van Rossum | b74eca9 | 1997-09-30 22:03:16 +0000 | [diff] [blame] | 345 | return tstate; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 349 | PyEval_RestoreThread(PyThreadState *tstate) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 350 | { |
Guido van Rossum | b74eca9 | 1997-09-30 22:03:16 +0000 | [diff] [blame] | 351 | if (tstate == NULL) |
| 352 | Py_FatalError("PyEval_RestoreThread: NULL tstate"); |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 353 | #ifdef WITH_THREAD |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 354 | if (interpreter_lock) { |
Guido van Rossum | b74eca9 | 1997-09-30 22:03:16 +0000 | [diff] [blame] | 355 | int err = errno; |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 356 | PyThread_acquire_lock(interpreter_lock, 1); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 357 | errno = err; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 358 | } |
| 359 | #endif |
Guido van Rossum | b74eca9 | 1997-09-30 22:03:16 +0000 | [diff] [blame] | 360 | PyThreadState_Swap(tstate); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 364 | /* Mechanism whereby asynchronously executing callbacks (e.g. UNIX |
| 365 | signal handlers or Mac I/O completion routines) can schedule calls |
| 366 | to a function to be called synchronously. |
| 367 | The synchronous function is called with one void* argument. |
| 368 | It should return 0 for success or -1 for failure -- failure should |
| 369 | be accompanied by an exception. |
| 370 | |
| 371 | If registry succeeds, the registry function returns 0; if it fails |
| 372 | (e.g. due to too many pending calls) it returns -1 (without setting |
| 373 | an exception condition). |
| 374 | |
| 375 | Note that because registry may occur from within signal handlers, |
| 376 | or other asynchronous events, calling malloc() is unsafe! |
| 377 | |
| 378 | #ifdef WITH_THREAD |
| 379 | Any thread can schedule pending calls, but only the main thread |
| 380 | will execute them. |
Kristján Valur Jónsson | 0e91938 | 2009-01-09 20:31:26 +0000 | [diff] [blame] | 381 | There is no facility to schedule calls to a particular thread, but |
| 382 | that should be easy to change, should that ever be required. In |
| 383 | that case, the static variables here should go into the python |
| 384 | threadstate. |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 385 | #endif |
Kristján Valur Jónsson | 0e91938 | 2009-01-09 20:31:26 +0000 | [diff] [blame] | 386 | */ |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 387 | |
Kristján Valur Jónsson | 0e91938 | 2009-01-09 20:31:26 +0000 | [diff] [blame] | 388 | #ifdef WITH_THREAD |
| 389 | |
| 390 | /* The WITH_THREAD implementation is thread-safe. It allows |
| 391 | scheduling to be made from any thread, and even from an executing |
| 392 | callback. |
| 393 | */ |
| 394 | |
| 395 | #define NPENDINGCALLS 32 |
| 396 | static struct { |
| 397 | int (*func)(void *); |
| 398 | void *arg; |
| 399 | } pendingcalls[NPENDINGCALLS]; |
| 400 | static int pendingfirst = 0; |
| 401 | static int pendinglast = 0; |
| 402 | static volatile int pendingcalls_to_do = 1; /* trigger initialization of lock */ |
| 403 | static char pendingbusy = 0; |
| 404 | |
| 405 | int |
| 406 | Py_AddPendingCall(int (*func)(void *), void *arg) |
| 407 | { |
| 408 | int i, j, result=0; |
| 409 | PyThread_type_lock lock = pending_lock; |
| 410 | |
| 411 | /* try a few times for the lock. Since this mechanism is used |
| 412 | * for signal handling (on the main thread), there is a (slim) |
| 413 | * chance that a signal is delivered on the same thread while we |
| 414 | * hold the lock during the Py_MakePendingCalls() function. |
| 415 | * This avoids a deadlock in that case. |
| 416 | * Note that signals can be delivered on any thread. In particular, |
| 417 | * on Windows, a SIGINT is delivered on a system-created worker |
| 418 | * thread. |
| 419 | * We also check for lock being NULL, in the unlikely case that |
| 420 | * this function is called before any bytecode evaluation takes place. |
| 421 | */ |
| 422 | if (lock != NULL) { |
| 423 | for (i = 0; i<100; i++) { |
| 424 | if (PyThread_acquire_lock(lock, NOWAIT_LOCK)) |
| 425 | break; |
| 426 | } |
| 427 | if (i == 100) |
| 428 | return -1; |
| 429 | } |
| 430 | |
| 431 | i = pendinglast; |
| 432 | j = (i + 1) % NPENDINGCALLS; |
| 433 | if (j == pendingfirst) { |
| 434 | result = -1; /* Queue full */ |
| 435 | } else { |
| 436 | pendingcalls[i].func = func; |
| 437 | pendingcalls[i].arg = arg; |
| 438 | pendinglast = j; |
| 439 | } |
| 440 | /* signal main loop */ |
| 441 | _Py_Ticker = 0; |
| 442 | pendingcalls_to_do = 1; |
| 443 | if (lock != NULL) |
| 444 | PyThread_release_lock(lock); |
| 445 | return result; |
| 446 | } |
| 447 | |
| 448 | int |
| 449 | Py_MakePendingCalls(void) |
| 450 | { |
| 451 | int i; |
| 452 | int r = 0; |
| 453 | |
| 454 | if (!pending_lock) { |
| 455 | /* initial allocation of the lock */ |
| 456 | pending_lock = PyThread_allocate_lock(); |
| 457 | if (pending_lock == NULL) |
| 458 | return -1; |
| 459 | } |
| 460 | |
| 461 | /* only service pending calls on main thread */ |
| 462 | if (main_thread && PyThread_get_thread_ident() != main_thread) |
| 463 | return 0; |
| 464 | /* don't perform recursive pending calls */ |
| 465 | if (pendingbusy) |
| 466 | return 0; |
| 467 | pendingbusy = 1; |
| 468 | /* perform a bounded number of calls, in case of recursion */ |
| 469 | for (i=0; i<NPENDINGCALLS; i++) { |
| 470 | int j; |
| 471 | int (*func)(void *); |
Mark Dickinson | 9e58a37 | 2009-02-08 17:33:11 +0000 | [diff] [blame] | 472 | void *arg = NULL; |
Kristján Valur Jónsson | 0e91938 | 2009-01-09 20:31:26 +0000 | [diff] [blame] | 473 | |
| 474 | /* pop one item off the queue while holding the lock */ |
| 475 | PyThread_acquire_lock(pending_lock, WAIT_LOCK); |
| 476 | j = pendingfirst; |
| 477 | if (j == pendinglast) { |
| 478 | func = NULL; /* Queue empty */ |
| 479 | } else { |
| 480 | func = pendingcalls[j].func; |
| 481 | arg = pendingcalls[j].arg; |
| 482 | pendingfirst = (j + 1) % NPENDINGCALLS; |
| 483 | } |
| 484 | pendingcalls_to_do = pendingfirst != pendinglast; |
| 485 | PyThread_release_lock(pending_lock); |
| 486 | /* having released the lock, perform the callback */ |
| 487 | if (func == NULL) |
| 488 | break; |
| 489 | r = func(arg); |
| 490 | if (r) |
| 491 | break; |
| 492 | } |
| 493 | pendingbusy = 0; |
| 494 | return r; |
| 495 | } |
| 496 | |
| 497 | #else /* if ! defined WITH_THREAD */ |
| 498 | |
| 499 | /* |
| 500 | WARNING! ASYNCHRONOUSLY EXECUTING CODE! |
| 501 | This code is used for signal handling in python that isn't built |
| 502 | with WITH_THREAD. |
| 503 | Don't use this implementation when Py_AddPendingCalls() can happen |
| 504 | on a different thread! |
| 505 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 506 | There are two possible race conditions: |
Kristján Valur Jónsson | 0e91938 | 2009-01-09 20:31:26 +0000 | [diff] [blame] | 507 | (1) nested asynchronous calls to Py_AddPendingCall() |
| 508 | (2) AddPendingCall() calls made while pending calls are being processed. |
| 509 | |
| 510 | (1) is very unlikely because typically signal delivery |
| 511 | is blocked during signal handling. So it should be impossible. |
| 512 | (2) is a real possibility. |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 513 | The current code is safe against (2), but not against (1). |
| 514 | The safety against (2) is derived from the fact that only one |
Kristján Valur Jónsson | 0e91938 | 2009-01-09 20:31:26 +0000 | [diff] [blame] | 515 | thread is present, interrupted by signals, and that the critical |
| 516 | section is protected with the "busy" variable. On Windows, which |
| 517 | delivers SIGINT on a system thread, this does not hold and therefore |
| 518 | Windows really shouldn't use this version. |
| 519 | The two threads could theoretically wiggle around the "busy" variable. |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 520 | */ |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 521 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 522 | #define NPENDINGCALLS 32 |
| 523 | static struct { |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 524 | int (*func)(void *); |
| 525 | void *arg; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 526 | } pendingcalls[NPENDINGCALLS]; |
| 527 | static volatile int pendingfirst = 0; |
| 528 | static volatile int pendinglast = 0; |
Kristján Valur Jónsson | 0e91938 | 2009-01-09 20:31:26 +0000 | [diff] [blame] | 529 | static volatile int pendingcalls_to_do = 0; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 530 | |
| 531 | int |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 532 | Py_AddPendingCall(int (*func)(void *), void *arg) |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 533 | { |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 534 | static volatile int busy = 0; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 535 | int i, j; |
| 536 | /* XXX Begin critical section */ |
Guido van Rossum | 180d7b4 | 1994-09-29 09:45:57 +0000 | [diff] [blame] | 537 | if (busy) |
| 538 | return -1; |
| 539 | busy = 1; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 540 | i = pendinglast; |
| 541 | j = (i + 1) % NPENDINGCALLS; |
Guido van Rossum | 04e7032 | 2002-07-17 16:57:13 +0000 | [diff] [blame] | 542 | if (j == pendingfirst) { |
| 543 | busy = 0; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 544 | return -1; /* Queue full */ |
Guido van Rossum | 04e7032 | 2002-07-17 16:57:13 +0000 | [diff] [blame] | 545 | } |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 546 | pendingcalls[i].func = func; |
| 547 | pendingcalls[i].arg = arg; |
| 548 | pendinglast = j; |
Skip Montanaro | d581d77 | 2002-09-03 20:10:45 +0000 | [diff] [blame] | 549 | |
| 550 | _Py_Ticker = 0; |
Kristján Valur Jónsson | 0e91938 | 2009-01-09 20:31:26 +0000 | [diff] [blame] | 551 | pendingcalls_to_do = 1; /* Signal main loop */ |
Guido van Rossum | 180d7b4 | 1994-09-29 09:45:57 +0000 | [diff] [blame] | 552 | busy = 0; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 553 | /* XXX End critical section */ |
| 554 | return 0; |
| 555 | } |
| 556 | |
Guido van Rossum | 180d7b4 | 1994-09-29 09:45:57 +0000 | [diff] [blame] | 557 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 558 | Py_MakePendingCalls(void) |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 559 | { |
Guido van Rossum | 180d7b4 | 1994-09-29 09:45:57 +0000 | [diff] [blame] | 560 | static int busy = 0; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 561 | if (busy) |
Guido van Rossum | 180d7b4 | 1994-09-29 09:45:57 +0000 | [diff] [blame] | 562 | return 0; |
| 563 | busy = 1; |
Kristján Valur Jónsson | 0e91938 | 2009-01-09 20:31:26 +0000 | [diff] [blame] | 564 | pendingcalls_to_do = 0; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 565 | for (;;) { |
| 566 | int i; |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 567 | int (*func)(void *); |
| 568 | void *arg; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 569 | i = pendingfirst; |
| 570 | if (i == pendinglast) |
| 571 | break; /* Queue empty */ |
| 572 | func = pendingcalls[i].func; |
| 573 | arg = pendingcalls[i].arg; |
| 574 | pendingfirst = (i + 1) % NPENDINGCALLS; |
Guido van Rossum | 180d7b4 | 1994-09-29 09:45:57 +0000 | [diff] [blame] | 575 | if (func(arg) < 0) { |
| 576 | busy = 0; |
Kristján Valur Jónsson | 0e91938 | 2009-01-09 20:31:26 +0000 | [diff] [blame] | 577 | pendingcalls_to_do = 1; /* We're not done yet */ |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 578 | return -1; |
Guido van Rossum | 180d7b4 | 1994-09-29 09:45:57 +0000 | [diff] [blame] | 579 | } |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 580 | } |
Guido van Rossum | 180d7b4 | 1994-09-29 09:45:57 +0000 | [diff] [blame] | 581 | busy = 0; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 582 | return 0; |
| 583 | } |
| 584 | |
Kristján Valur Jónsson | 0e91938 | 2009-01-09 20:31:26 +0000 | [diff] [blame] | 585 | #endif /* WITH_THREAD */ |
| 586 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 587 | |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 588 | /* The interpreter's recursion limit */ |
| 589 | |
Hye-Shik Chang | b6fa281 | 2005-04-04 15:49:02 +0000 | [diff] [blame] | 590 | #ifndef Py_DEFAULT_RECURSION_LIMIT |
| 591 | #define Py_DEFAULT_RECURSION_LIMIT 1000 |
| 592 | #endif |
| 593 | static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT; |
| 594 | int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 595 | |
Vladimir Marangozov | 7bd25be | 2000-09-01 11:07:19 +0000 | [diff] [blame] | 596 | int |
| 597 | Py_GetRecursionLimit(void) |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 598 | { |
| 599 | return recursion_limit; |
| 600 | } |
| 601 | |
Vladimir Marangozov | 7bd25be | 2000-09-01 11:07:19 +0000 | [diff] [blame] | 602 | void |
| 603 | Py_SetRecursionLimit(int new_limit) |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 604 | { |
| 605 | recursion_limit = new_limit; |
Thomas Wouters | ae406c6 | 2007-09-19 17:27:43 +0000 | [diff] [blame] | 606 | _Py_CheckRecursionLimit = recursion_limit; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 607 | } |
| 608 | |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 609 | /* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall() |
| 610 | if the recursion_depth reaches _Py_CheckRecursionLimit. |
| 611 | If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit |
| 612 | to guarantee that _Py_CheckRecursiveCall() is regularly called. |
| 613 | Without USE_STACKCHECK, there is no need for this. */ |
| 614 | int |
| 615 | _Py_CheckRecursiveCall(char *where) |
| 616 | { |
| 617 | PyThreadState *tstate = PyThreadState_GET(); |
| 618 | |
| 619 | #ifdef USE_STACKCHECK |
| 620 | if (PyOS_CheckStack()) { |
| 621 | --tstate->recursion_depth; |
| 622 | PyErr_SetString(PyExc_MemoryError, "Stack overflow"); |
| 623 | return -1; |
| 624 | } |
| 625 | #endif |
| 626 | if (tstate->recursion_depth > recursion_limit) { |
| 627 | --tstate->recursion_depth; |
| 628 | PyErr_Format(PyExc_RuntimeError, |
| 629 | "maximum recursion depth exceeded%s", |
| 630 | where); |
| 631 | return -1; |
| 632 | } |
Thomas Wouters | ae406c6 | 2007-09-19 17:27:43 +0000 | [diff] [blame] | 633 | _Py_CheckRecursionLimit = recursion_limit; |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 634 | return 0; |
| 635 | } |
| 636 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 637 | /* Status code for main loop (reason for stack unwind) */ |
Raymond Hettinger | 7c95865 | 2004-04-06 10:11:10 +0000 | [diff] [blame] | 638 | enum why_code { |
| 639 | WHY_NOT = 0x0001, /* No error */ |
| 640 | WHY_EXCEPTION = 0x0002, /* Exception occurred */ |
| 641 | WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */ |
| 642 | WHY_RETURN = 0x0008, /* 'return' statement */ |
| 643 | WHY_BREAK = 0x0010, /* 'break' statement */ |
| 644 | WHY_CONTINUE = 0x0020, /* 'continue' statement */ |
| 645 | WHY_YIELD = 0x0040 /* 'yield' operator */ |
| 646 | }; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 647 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 648 | static enum why_code do_raise(PyObject *, PyObject *, PyObject *); |
| 649 | static int unpack_iterable(PyObject *, int, PyObject **); |
Guido van Rossum | 1aa1483 | 1997-01-21 05:34:20 +0000 | [diff] [blame] | 650 | |
Jeffrey Yasskin | fd8a1ec | 2008-12-03 06:46:45 +0000 | [diff] [blame] | 651 | /* Records whether tracing is on for any thread. Counts the number of |
| 652 | threads for which tstate->c_tracefunc is non-NULL, so if the value |
| 653 | is 0, we know we don't have to check this thread's c_tracefunc. |
| 654 | This speeds up the if statement in PyEval_EvalFrameEx() after |
| 655 | fast_next_opcode*/ |
| 656 | static int _Py_TracingPossible = 0; |
| 657 | |
Skip Montanaro | d581d77 | 2002-09-03 20:10:45 +0000 | [diff] [blame] | 658 | /* for manipulating the thread switch and periodic "stuff" - used to be |
| 659 | per thread, now just a pair o' globals */ |
Skip Montanaro | 99dba27 | 2002-09-03 20:19:06 +0000 | [diff] [blame] | 660 | int _Py_CheckInterval = 100; |
Kristján Valur Jónsson | 0e91938 | 2009-01-09 20:31:26 +0000 | [diff] [blame] | 661 | volatile int _Py_Ticker = 0; /* so that we hit a "tick" first thing */ |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 662 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 663 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 664 | PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 665 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 666 | return PyEval_EvalCodeEx(co, |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 667 | globals, locals, |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 668 | (PyObject **)NULL, 0, |
| 669 | (PyObject **)NULL, 0, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 670 | (PyObject **)NULL, 0, |
| 671 | NULL); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | |
| 675 | /* Interpreter main loop */ |
| 676 | |
Martin v. Löwis | 8d97e33 | 2004-06-27 15:43:12 +0000 | [diff] [blame] | 677 | PyObject * |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 678 | PyEval_EvalFrame(PyFrameObject *f) { |
| 679 | /* This is for backward compatibility with extension modules that |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 680 | used this API; core interpreter code should call |
| 681 | PyEval_EvalFrameEx() */ |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 682 | return PyEval_EvalFrameEx(f, 0); |
| 683 | } |
| 684 | |
| 685 | PyObject * |
Anthony Baxter | a863d33 | 2006-04-11 07:43:46 +0000 | [diff] [blame] | 686 | PyEval_EvalFrameEx(PyFrameObject *f, int throwflag) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 687 | { |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 688 | #ifdef DXPAIRS |
| 689 | int lastopcode = 0; |
| 690 | #endif |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 691 | register PyObject **stack_pointer; /* Next free slot in value stack */ |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 692 | register unsigned char *next_instr; |
Armin Rigo | 8817fcd | 2004-06-17 10:22:40 +0000 | [diff] [blame] | 693 | register int opcode; /* Current opcode */ |
| 694 | register int oparg; /* Current opcode argument, if any */ |
Raymond Hettinger | 7c95865 | 2004-04-06 10:11:10 +0000 | [diff] [blame] | 695 | register enum why_code why; /* Reason for block stack unwind */ |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 696 | register int err; /* Error status -- nonzero if error */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 697 | register PyObject *x; /* Result object -- NULL if error */ |
| 698 | register PyObject *v; /* Temporary objects popped off stack */ |
| 699 | register PyObject *w; |
| 700 | register PyObject *u; |
| 701 | register PyObject *t; |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 702 | register PyObject *stream = NULL; /* for PRINT opcodes */ |
Jeremy Hylton | 2b724da | 2001-01-29 22:51:52 +0000 | [diff] [blame] | 703 | register PyObject **fastlocals, **freevars; |
Guido van Rossum | 014518f | 1998-11-23 21:09:51 +0000 | [diff] [blame] | 704 | PyObject *retval = NULL; /* Return value */ |
Guido van Rossum | 885553e | 1998-12-21 18:33:30 +0000 | [diff] [blame] | 705 | PyThreadState *tstate = PyThreadState_GET(); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 706 | PyCodeObject *co; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 707 | |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 708 | /* when tracing we set things up so that |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 709 | |
| 710 | not (instr_lb <= current_bytecode_offset < instr_ub) |
| 711 | |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 712 | is true when the line being executed has changed. The |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 713 | initial values are such as to make this false the first |
| 714 | time it is tested. */ |
Armin Rigo | bf57a14 | 2004-03-22 19:24:58 +0000 | [diff] [blame] | 715 | int instr_ub = -1, instr_lb = 0, instr_prev = -1; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 716 | |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 717 | unsigned char *first_instr; |
Skip Montanaro | 04d80f8 | 2002-08-04 21:03:35 +0000 | [diff] [blame] | 718 | PyObject *names; |
| 719 | PyObject *consts; |
Neal Norwitz | 5f5153e | 2005-10-21 04:28:38 +0000 | [diff] [blame] | 720 | #if defined(Py_DEBUG) || defined(LLTRACE) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 721 | /* Make it easier to find out where we are with a debugger */ |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 722 | char *filename; |
Guido van Rossum | 99bec95 | 1992-09-03 20:29:45 +0000 | [diff] [blame] | 723 | #endif |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 724 | |
Neal Norwitz | a81d220 | 2002-07-14 00:27:26 +0000 | [diff] [blame] | 725 | /* Tuple access macros */ |
| 726 | |
| 727 | #ifndef Py_DEBUG |
| 728 | #define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i)) |
| 729 | #else |
| 730 | #define GETITEM(v, i) PyTuple_GetItem((v), (i)) |
| 731 | #endif |
| 732 | |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 733 | #ifdef WITH_TSC |
| 734 | /* Use Pentium timestamp counter to mark certain events: |
| 735 | inst0 -- beginning of switch statement for opcode dispatch |
| 736 | inst1 -- end of switch statement (may be skipped) |
| 737 | loop0 -- the top of the mainloop |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 738 | loop1 -- place where control returns again to top of mainloop |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 739 | (may be skipped) |
| 740 | intr1 -- beginning of long interruption |
| 741 | intr2 -- end of long interruption |
| 742 | |
| 743 | Many opcodes call out to helper C functions. In some cases, the |
| 744 | time in those functions should be counted towards the time for the |
| 745 | opcode, but not in all cases. For example, a CALL_FUNCTION opcode |
| 746 | calls another Python function; there's no point in charge all the |
| 747 | bytecode executed by the called function to the caller. |
| 748 | |
| 749 | It's hard to make a useful judgement statically. In the presence |
| 750 | of operator overloading, it's impossible to tell if a call will |
| 751 | execute new Python code or not. |
| 752 | |
| 753 | It's a case-by-case judgement. I'll use intr1 for the following |
| 754 | cases: |
| 755 | |
| 756 | EXEC_STMT |
| 757 | IMPORT_STAR |
| 758 | IMPORT_FROM |
| 759 | CALL_FUNCTION (and friends) |
| 760 | |
| 761 | */ |
| 762 | uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0; |
| 763 | int ticked = 0; |
| 764 | |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 765 | READ_TIMESTAMP(inst0); |
| 766 | READ_TIMESTAMP(inst1); |
| 767 | READ_TIMESTAMP(loop0); |
| 768 | READ_TIMESTAMP(loop1); |
Michael W. Hudson | 800ba23 | 2004-08-12 18:19:17 +0000 | [diff] [blame] | 769 | |
| 770 | /* shut up the compiler */ |
| 771 | opcode = 0; |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 772 | #endif |
| 773 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 774 | /* Code access macros */ |
| 775 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 776 | #define INSTR_OFFSET() ((int)(next_instr - first_instr)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 777 | #define NEXTOP() (*next_instr++) |
Raymond Hettinger | 5bed456 | 2004-04-10 23:34:17 +0000 | [diff] [blame] | 778 | #define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2]) |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 779 | #define PEEKARG() ((next_instr[2]<<8) + next_instr[1]) |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 780 | #define JUMPTO(x) (next_instr = first_instr + (x)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 781 | #define JUMPBY(x) (next_instr += (x)) |
| 782 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 783 | /* OpCode prediction macros |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 784 | Some opcodes tend to come in pairs thus making it possible to |
| 785 | predict the second code when the first is run. For example, |
Jeffrey Yasskin | 68d6852 | 2009-02-28 19:03:21 +0000 | [diff] [blame] | 786 | GET_ITER is often followed by FOR_ITER. And FOR_ITER is often |
| 787 | followed by STORE_FAST or UNPACK_SEQUENCE. |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 788 | |
Raymond Hettinger | afae11e | 2008-07-05 02:11:55 +0000 | [diff] [blame] | 789 | Verifying the prediction costs a single high-speed test of a register |
Raymond Hettinger | ac207292 | 2003-03-16 15:41:11 +0000 | [diff] [blame] | 790 | variable against a constant. If the pairing was good, then the |
Raymond Hettinger | afae11e | 2008-07-05 02:11:55 +0000 | [diff] [blame] | 791 | processor's own internal branch predication has a high likelihood of |
| 792 | success, resulting in a nearly zero-overhead transition to the |
| 793 | next opcode. A successful prediction saves a trip through the eval-loop |
Antoine Pitrou | c2cc80c | 2008-07-25 22:13:52 +0000 | [diff] [blame] | 794 | including its two unpredictable branches, the HAS_ARG test and the |
Raymond Hettinger | afae11e | 2008-07-05 02:11:55 +0000 | [diff] [blame] | 795 | switch-case. Combined with the processor's internal branch prediction, |
Antoine Pitrou | c2cc80c | 2008-07-25 22:13:52 +0000 | [diff] [blame] | 796 | a successful PREDICT has the effect of making the two opcodes run as if |
Raymond Hettinger | afae11e | 2008-07-05 02:11:55 +0000 | [diff] [blame] | 797 | they were a single new opcode with the bodies combined. |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 798 | |
Raymond Hettinger | afae11e | 2008-07-05 02:11:55 +0000 | [diff] [blame] | 799 | If collecting opcode statistics, your choices are to either keep the |
| 800 | predictions turned-on and interpret the results as if some opcodes |
| 801 | had been combined or turn-off predictions so that the opcode frequency |
| 802 | counter updates for both opcodes. |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 803 | */ |
| 804 | |
Raymond Hettinger | a721698 | 2004-02-08 19:59:27 +0000 | [diff] [blame] | 805 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 806 | #define PREDICT(op) if (0) goto PRED_##op |
| 807 | #else |
Raymond Hettinger | ac207292 | 2003-03-16 15:41:11 +0000 | [diff] [blame] | 808 | #define PREDICT(op) if (*next_instr == op) goto PRED_##op |
Raymond Hettinger | a721698 | 2004-02-08 19:59:27 +0000 | [diff] [blame] | 809 | #endif |
| 810 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 811 | #define PREDICTED(op) PRED_##op: next_instr++ |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 812 | #define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3 |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 813 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 814 | /* Stack manipulation macros */ |
| 815 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 816 | /* The stack can grow at most MAXINT deep, as co_nlocals and |
| 817 | co_stacksize are ints. */ |
| 818 | #define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 819 | #define EMPTY() (STACK_LEVEL() == 0) |
| 820 | #define TOP() (stack_pointer[-1]) |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 821 | #define SECOND() (stack_pointer[-2]) |
| 822 | #define THIRD() (stack_pointer[-3]) |
| 823 | #define FOURTH() (stack_pointer[-4]) |
Benjamin Peterson | b8338ab | 2009-06-28 16:08:02 +0000 | [diff] [blame] | 824 | #define PEEK(n) (stack_pointer[-(n)]) |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 825 | #define SET_TOP(v) (stack_pointer[-1] = (v)) |
| 826 | #define SET_SECOND(v) (stack_pointer[-2] = (v)) |
| 827 | #define SET_THIRD(v) (stack_pointer[-3] = (v)) |
| 828 | #define SET_FOURTH(v) (stack_pointer[-4] = (v)) |
Benjamin Peterson | b8338ab | 2009-06-28 16:08:02 +0000 | [diff] [blame] | 829 | #define SET_VALUE(n, v) (stack_pointer[-(n)] = (v)) |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 830 | #define BASIC_STACKADJ(n) (stack_pointer += n) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 831 | #define BASIC_PUSH(v) (*stack_pointer++ = (v)) |
| 832 | #define BASIC_POP() (*--stack_pointer) |
| 833 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 834 | #ifdef LLTRACE |
Jeremy Hylton | 1436815 | 2001-10-17 13:29:30 +0000 | [diff] [blame] | 835 | #define PUSH(v) { (void)(BASIC_PUSH(v), \ |
| 836 | lltrace && prtrace(TOP(), "push")); \ |
Richard Jones | cebbefc | 2006-05-23 18:28:17 +0000 | [diff] [blame] | 837 | assert(STACK_LEVEL() <= co->co_stacksize); } |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 838 | #define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \ |
| 839 | BASIC_POP()) |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 840 | #define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \ |
| 841 | lltrace && prtrace(TOP(), "stackadj")); \ |
Richard Jones | cebbefc | 2006-05-23 18:28:17 +0000 | [diff] [blame] | 842 | assert(STACK_LEVEL() <= co->co_stacksize); } |
Christian Heimes | 52729ac | 2007-12-14 02:33:57 +0000 | [diff] [blame] | 843 | #define EXT_POP(STACK_POINTER) ((void)(lltrace && \ |
| 844 | prtrace((STACK_POINTER)[-1], "ext_pop")), \ |
| 845 | *--(STACK_POINTER)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 846 | #else |
| 847 | #define PUSH(v) BASIC_PUSH(v) |
| 848 | #define POP() BASIC_POP() |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 849 | #define STACKADJ(n) BASIC_STACKADJ(n) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 850 | #define EXT_POP(STACK_POINTER) (*--(STACK_POINTER)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 851 | #endif |
| 852 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 853 | /* Local variable macros */ |
| 854 | |
| 855 | #define GETLOCAL(i) (fastlocals[i]) |
Guido van Rossum | cfbf1a3 | 2002-03-28 20:17:52 +0000 | [diff] [blame] | 856 | |
| 857 | /* The SETLOCAL() macro must not DECREF the local variable in-place and |
| 858 | then store the new value; it must copy the old value to a temporary |
| 859 | value, then store the new value, and then DECREF the temporary value. |
| 860 | This is because it is possible that during the DECREF the frame is |
| 861 | accessed by other code (e.g. a __del__ method or gc.collect()) and the |
| 862 | variable would be pointing to already-freed memory. */ |
| 863 | #define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \ |
| 864 | GETLOCAL(i) = value; \ |
| 865 | Py_XDECREF(tmp); } while (0) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 866 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 867 | /* Start of code */ |
| 868 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 869 | if (f == NULL) |
| 870 | return NULL; |
| 871 | |
Armin Rigo | 1d313ab | 2003-10-25 14:33:09 +0000 | [diff] [blame] | 872 | /* push frame */ |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 873 | if (Py_EnterRecursiveCall("")) |
Armin Rigo | 1d313ab | 2003-10-25 14:33:09 +0000 | [diff] [blame] | 874 | return NULL; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 875 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 876 | tstate->frame = f; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 877 | |
Neil Schemenauer | 6c0f200 | 2001-09-04 19:03:35 +0000 | [diff] [blame] | 878 | if (tstate->use_tracing) { |
| 879 | if (tstate->c_tracefunc != NULL) { |
| 880 | /* tstate->c_tracefunc, if defined, is a |
| 881 | function that will be called on *every* entry |
| 882 | to a code block. Its return value, if not |
| 883 | None, is a function that will be called at |
| 884 | the start of each executed line of code. |
| 885 | (Actually, the function must return itself |
| 886 | in order to continue tracing.) The trace |
| 887 | functions are called with three arguments: |
| 888 | a pointer to the current frame, a string |
| 889 | indicating why the function is called, and |
| 890 | an argument which depends on the situation. |
| 891 | The global trace function is also called |
| 892 | whenever an exception is detected. */ |
Antoine Pitrou | c2cc80c | 2008-07-25 22:13:52 +0000 | [diff] [blame] | 893 | if (call_trace_protected(tstate->c_tracefunc, |
Amaury Forgeot d'Arc | 0d75f09 | 2007-11-13 21:54:28 +0000 | [diff] [blame] | 894 | tstate->c_traceobj, |
| 895 | f, PyTrace_CALL, Py_None)) { |
Neil Schemenauer | 6c0f200 | 2001-09-04 19:03:35 +0000 | [diff] [blame] | 896 | /* Trace function raised an error */ |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 897 | goto exit_eval_frame; |
Neil Schemenauer | 6c0f200 | 2001-09-04 19:03:35 +0000 | [diff] [blame] | 898 | } |
| 899 | } |
| 900 | if (tstate->c_profilefunc != NULL) { |
| 901 | /* Similar for c_profilefunc, except it needn't |
| 902 | return itself and isn't called for "line" events */ |
Amaury Forgeot d'Arc | 0d75f09 | 2007-11-13 21:54:28 +0000 | [diff] [blame] | 903 | if (call_trace_protected(tstate->c_profilefunc, |
| 904 | tstate->c_profileobj, |
| 905 | f, PyTrace_CALL, Py_None)) { |
Neil Schemenauer | 6c0f200 | 2001-09-04 19:03:35 +0000 | [diff] [blame] | 906 | /* Profile function raised an error */ |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 907 | goto exit_eval_frame; |
Neil Schemenauer | 6c0f200 | 2001-09-04 19:03:35 +0000 | [diff] [blame] | 908 | } |
| 909 | } |
| 910 | } |
| 911 | |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 912 | co = f->f_code; |
| 913 | names = co->co_names; |
| 914 | consts = co->co_consts; |
| 915 | fastlocals = f->f_localsplus; |
Richard Jones | cebbefc | 2006-05-23 18:28:17 +0000 | [diff] [blame] | 916 | freevars = f->f_localsplus + co->co_nlocals; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 917 | first_instr = (unsigned char*) PyString_AS_STRING(co->co_code); |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 918 | /* An explanation is in order for the next line. |
| 919 | |
| 920 | f->f_lasti now refers to the index of the last instruction |
| 921 | executed. You might think this was obvious from the name, but |
| 922 | this wasn't always true before 2.3! PyFrame_New now sets |
| 923 | f->f_lasti to -1 (i.e. the index *before* the first instruction) |
| 924 | and YIELD_VALUE doesn't fiddle with f_lasti any more. So this |
Antoine Pitrou | c2cc80c | 2008-07-25 22:13:52 +0000 | [diff] [blame] | 925 | does work. Promise. |
Raymond Hettinger | 4bd97d4 | 2007-01-06 01:14:41 +0000 | [diff] [blame] | 926 | |
| 927 | When the PREDICT() macros are enabled, some opcode pairs follow in |
Antoine Pitrou | c2cc80c | 2008-07-25 22:13:52 +0000 | [diff] [blame] | 928 | direct succession without updating f->f_lasti. A successful |
Raymond Hettinger | 4bd97d4 | 2007-01-06 01:14:41 +0000 | [diff] [blame] | 929 | prediction effectively links the two codes together as if they |
| 930 | were a single new opcode; accordingly,f->f_lasti will point to |
| 931 | the first code in the pair (for instance, GET_ITER followed by |
| 932 | FOR_ITER is effectively a single opcode and f->f_lasti will point |
| 933 | at to the beginning of the combined pair.) |
| 934 | */ |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 935 | next_instr = first_instr + f->f_lasti + 1; |
| 936 | stack_pointer = f->f_stacktop; |
| 937 | assert(stack_pointer != NULL); |
| 938 | f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */ |
| 939 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 940 | #ifdef LLTRACE |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 941 | lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 942 | #endif |
Neal Norwitz | 5f5153e | 2005-10-21 04:28:38 +0000 | [diff] [blame] | 943 | #if defined(Py_DEBUG) || defined(LLTRACE) |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 944 | filename = PyString_AsString(co->co_filename); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 945 | #endif |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 946 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 947 | why = WHY_NOT; |
| 948 | err = 0; |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 949 | x = Py_None; /* Not a reference, just anything non-NULL */ |
Fred Drake | 48fba73 | 2000-10-11 13:54:07 +0000 | [diff] [blame] | 950 | w = NULL; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 951 | |
Anthony Baxter | a863d33 | 2006-04-11 07:43:46 +0000 | [diff] [blame] | 952 | if (throwflag) { /* support for generator.throw() */ |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 953 | why = WHY_EXCEPTION; |
| 954 | goto on_error; |
| 955 | } |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 956 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 957 | for (;;) { |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 958 | #ifdef WITH_TSC |
| 959 | if (inst1 == 0) { |
| 960 | /* Almost surely, the opcode executed a break |
| 961 | or a continue, preventing inst1 from being set |
| 962 | on the way out of the loop. |
| 963 | */ |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 964 | READ_TIMESTAMP(inst1); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 965 | loop1 = inst1; |
| 966 | } |
| 967 | dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1, |
| 968 | intr0, intr1); |
| 969 | ticked = 0; |
| 970 | inst1 = 0; |
| 971 | intr0 = 0; |
| 972 | intr1 = 0; |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 973 | READ_TIMESTAMP(loop0); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 974 | #endif |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 975 | assert(stack_pointer >= f->f_valuestack); /* else underflow */ |
Richard Jones | cebbefc | 2006-05-23 18:28:17 +0000 | [diff] [blame] | 976 | assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */ |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 977 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 978 | /* Do periodic things. Doing this every time through |
| 979 | the loop would add too much overhead, so we do it |
| 980 | only every Nth instruction. We also do it if |
Kristján Valur Jónsson | 0e91938 | 2009-01-09 20:31:26 +0000 | [diff] [blame] | 981 | ``pendingcalls_to_do'' is set, i.e. when an asynchronous |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 982 | event needs attention (e.g. a signal handler or |
| 983 | async I/O handler); see Py_AddPendingCall() and |
| 984 | Py_MakePendingCalls() above. */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 985 | |
Skip Montanaro | d581d77 | 2002-09-03 20:10:45 +0000 | [diff] [blame] | 986 | if (--_Py_Ticker < 0) { |
Thomas Wouters | ae406c6 | 2007-09-19 17:27:43 +0000 | [diff] [blame] | 987 | if (*next_instr == SETUP_FINALLY) { |
| 988 | /* Make the last opcode before |
| 989 | a try: finally: block uninterruptable. */ |
| 990 | goto fast_next_opcode; |
| 991 | } |
Skip Montanaro | d581d77 | 2002-09-03 20:10:45 +0000 | [diff] [blame] | 992 | _Py_Ticker = _Py_CheckInterval; |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 993 | tstate->tick_counter++; |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 994 | #ifdef WITH_TSC |
| 995 | ticked = 1; |
| 996 | #endif |
Kristján Valur Jónsson | 0e91938 | 2009-01-09 20:31:26 +0000 | [diff] [blame] | 997 | if (pendingcalls_to_do) { |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 998 | if (Py_MakePendingCalls() < 0) { |
| 999 | why = WHY_EXCEPTION; |
| 1000 | goto on_error; |
| 1001 | } |
Kristján Valur Jónsson | 0e91938 | 2009-01-09 20:31:26 +0000 | [diff] [blame] | 1002 | if (pendingcalls_to_do) |
Kurt B. Kaiser | 4c79a83 | 2004-11-23 18:06:08 +0000 | [diff] [blame] | 1003 | /* MakePendingCalls() didn't succeed. |
| 1004 | Force early re-execution of this |
| 1005 | "periodic" code, possibly after |
| 1006 | a thread switch */ |
| 1007 | _Py_Ticker = 0; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1008 | } |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 1009 | #ifdef WITH_THREAD |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1010 | if (interpreter_lock) { |
| 1011 | /* Give another thread a chance */ |
| 1012 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1013 | if (PyThreadState_Swap(NULL) != tstate) |
| 1014 | Py_FatalError("ceval: tstate mix-up"); |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 1015 | PyThread_release_lock(interpreter_lock); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1016 | |
| 1017 | /* Other threads may run now */ |
| 1018 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 1019 | PyThread_acquire_lock(interpreter_lock, 1); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1020 | if (PyThreadState_Swap(tstate) != NULL) |
| 1021 | Py_FatalError("ceval: orphan tstate"); |
Guido van Rossum | b8b6d0c | 2003-06-28 21:53:52 +0000 | [diff] [blame] | 1022 | |
| 1023 | /* Check for thread interrupts */ |
| 1024 | |
| 1025 | if (tstate->async_exc != NULL) { |
| 1026 | x = tstate->async_exc; |
| 1027 | tstate->async_exc = NULL; |
| 1028 | PyErr_SetNone(x); |
| 1029 | Py_DECREF(x); |
| 1030 | why = WHY_EXCEPTION; |
| 1031 | goto on_error; |
| 1032 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1033 | } |
| 1034 | #endif |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1035 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1036 | |
Neil Schemenauer | 6354386 | 2002-02-17 19:10:14 +0000 | [diff] [blame] | 1037 | fast_next_opcode: |
Guido van Rossum | 99bec95 | 1992-09-03 20:29:45 +0000 | [diff] [blame] | 1038 | f->f_lasti = INSTR_OFFSET(); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1039 | |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1040 | /* line-by-line tracing support */ |
| 1041 | |
Jeffrey Yasskin | fd8a1ec | 2008-12-03 06:46:45 +0000 | [diff] [blame] | 1042 | if (_Py_TracingPossible && |
| 1043 | tstate->c_tracefunc != NULL && !tstate->tracing) { |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1044 | /* see maybe_call_line_trace |
| 1045 | for expository comments */ |
| 1046 | f->f_stacktop = stack_pointer; |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 1047 | |
Michael W. Hudson | 58ee2af | 2003-04-29 16:18:47 +0000 | [diff] [blame] | 1048 | err = maybe_call_line_trace(tstate->c_tracefunc, |
| 1049 | tstate->c_traceobj, |
Armin Rigo | bf57a14 | 2004-03-22 19:24:58 +0000 | [diff] [blame] | 1050 | f, &instr_lb, &instr_ub, |
| 1051 | &instr_prev); |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1052 | /* Reload possibly changed frame fields */ |
| 1053 | JUMPTO(f->f_lasti); |
Michael W. Hudson | 58ee2af | 2003-04-29 16:18:47 +0000 | [diff] [blame] | 1054 | if (f->f_stacktop != NULL) { |
| 1055 | stack_pointer = f->f_stacktop; |
| 1056 | f->f_stacktop = NULL; |
| 1057 | } |
| 1058 | if (err) { |
| 1059 | /* trace function raised an exception */ |
| 1060 | goto on_error; |
| 1061 | } |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
| 1064 | /* Extract opcode and argument */ |
| 1065 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1066 | opcode = NEXTOP(); |
Armin Rigo | 8817fcd | 2004-06-17 10:22:40 +0000 | [diff] [blame] | 1067 | oparg = 0; /* allows oparg to be stored in a register because |
| 1068 | it doesn't have to be remembered across a full loop */ |
Raymond Hettinger | 5bed456 | 2004-04-10 23:34:17 +0000 | [diff] [blame] | 1069 | if (HAS_ARG(opcode)) |
| 1070 | oparg = NEXTARG(); |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 1071 | dispatch_opcode: |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 1072 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 1073 | #ifdef DXPAIRS |
| 1074 | dxpairs[lastopcode][opcode]++; |
| 1075 | lastopcode = opcode; |
| 1076 | #endif |
| 1077 | dxp[opcode]++; |
| 1078 | #endif |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1079 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 1080 | #ifdef LLTRACE |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1081 | /* Instruction tracing */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1082 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 1083 | if (lltrace) { |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1084 | if (HAS_ARG(opcode)) { |
| 1085 | printf("%d: %d, %d\n", |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 1086 | f->f_lasti, opcode, oparg); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1087 | } |
| 1088 | else { |
| 1089 | printf("%d: %d\n", |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 1090 | f->f_lasti, opcode); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1091 | } |
| 1092 | } |
| 1093 | #endif |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 1094 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1095 | /* Main switch on opcode */ |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 1096 | READ_TIMESTAMP(inst0); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 1097 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1098 | switch (opcode) { |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1099 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1100 | /* BEWARE! |
| 1101 | It is essential that any operation that fails sets either |
| 1102 | x to NULL, err to nonzero, or why to anything but WHY_NOT, |
| 1103 | and that no operation that succeeds does this! */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1104 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1105 | /* case STOP_CODE: this is an error! */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1106 | |
Raymond Hettinger | 9c18e81 | 2004-06-21 16:31:15 +0000 | [diff] [blame] | 1107 | case NOP: |
| 1108 | goto fast_next_opcode; |
| 1109 | |
Neil Schemenauer | 6354386 | 2002-02-17 19:10:14 +0000 | [diff] [blame] | 1110 | case LOAD_FAST: |
| 1111 | x = GETLOCAL(oparg); |
| 1112 | if (x != NULL) { |
| 1113 | Py_INCREF(x); |
| 1114 | PUSH(x); |
| 1115 | goto fast_next_opcode; |
| 1116 | } |
| 1117 | format_exc_check_arg(PyExc_UnboundLocalError, |
| 1118 | UNBOUNDLOCAL_ERROR_MSG, |
| 1119 | PyTuple_GetItem(co->co_varnames, oparg)); |
| 1120 | break; |
| 1121 | |
| 1122 | case LOAD_CONST: |
Skip Montanaro | 04d80f8 | 2002-08-04 21:03:35 +0000 | [diff] [blame] | 1123 | x = GETITEM(consts, oparg); |
Neil Schemenauer | 6354386 | 2002-02-17 19:10:14 +0000 | [diff] [blame] | 1124 | Py_INCREF(x); |
| 1125 | PUSH(x); |
| 1126 | goto fast_next_opcode; |
| 1127 | |
Raymond Hettinger | 7dc5221 | 2003-03-16 20:14:44 +0000 | [diff] [blame] | 1128 | PREDICTED_WITH_ARG(STORE_FAST); |
Neil Schemenauer | 6354386 | 2002-02-17 19:10:14 +0000 | [diff] [blame] | 1129 | case STORE_FAST: |
| 1130 | v = POP(); |
| 1131 | SETLOCAL(oparg, v); |
| 1132 | goto fast_next_opcode; |
| 1133 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1134 | case POP_TOP: |
| 1135 | v = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1136 | Py_DECREF(v); |
Neil Schemenauer | 6354386 | 2002-02-17 19:10:14 +0000 | [diff] [blame] | 1137 | goto fast_next_opcode; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1138 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1139 | case ROT_TWO: |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1140 | v = TOP(); |
| 1141 | w = SECOND(); |
| 1142 | SET_TOP(w); |
| 1143 | SET_SECOND(v); |
Raymond Hettinger | 080cb32 | 2003-03-14 01:37:42 +0000 | [diff] [blame] | 1144 | goto fast_next_opcode; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1145 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1146 | case ROT_THREE: |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1147 | v = TOP(); |
| 1148 | w = SECOND(); |
| 1149 | x = THIRD(); |
| 1150 | SET_TOP(w); |
| 1151 | SET_SECOND(x); |
| 1152 | SET_THIRD(v); |
Raymond Hettinger | 080cb32 | 2003-03-14 01:37:42 +0000 | [diff] [blame] | 1153 | goto fast_next_opcode; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1154 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1155 | case ROT_FOUR: |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1156 | u = TOP(); |
| 1157 | v = SECOND(); |
| 1158 | w = THIRD(); |
| 1159 | x = FOURTH(); |
| 1160 | SET_TOP(v); |
| 1161 | SET_SECOND(w); |
| 1162 | SET_THIRD(x); |
| 1163 | SET_FOURTH(u); |
Raymond Hettinger | 080cb32 | 2003-03-14 01:37:42 +0000 | [diff] [blame] | 1164 | goto fast_next_opcode; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1165 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1166 | case DUP_TOP: |
| 1167 | v = TOP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1168 | Py_INCREF(v); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1169 | PUSH(v); |
Raymond Hettinger | 080cb32 | 2003-03-14 01:37:42 +0000 | [diff] [blame] | 1170 | goto fast_next_opcode; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1171 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1172 | case DUP_TOPX: |
Raymond Hettinger | 4bad9ba | 2003-01-19 05:08:13 +0000 | [diff] [blame] | 1173 | if (oparg == 2) { |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1174 | x = TOP(); |
Tim Peters | 35ba689 | 2000-10-11 07:04:49 +0000 | [diff] [blame] | 1175 | Py_INCREF(x); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1176 | w = SECOND(); |
Tim Peters | 35ba689 | 2000-10-11 07:04:49 +0000 | [diff] [blame] | 1177 | Py_INCREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1178 | STACKADJ(2); |
| 1179 | SET_TOP(x); |
| 1180 | SET_SECOND(w); |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 1181 | goto fast_next_opcode; |
Raymond Hettinger | 4bad9ba | 2003-01-19 05:08:13 +0000 | [diff] [blame] | 1182 | } else if (oparg == 3) { |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1183 | x = TOP(); |
Tim Peters | 35ba689 | 2000-10-11 07:04:49 +0000 | [diff] [blame] | 1184 | Py_INCREF(x); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1185 | w = SECOND(); |
Tim Peters | 35ba689 | 2000-10-11 07:04:49 +0000 | [diff] [blame] | 1186 | Py_INCREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1187 | v = THIRD(); |
Tim Peters | 35ba689 | 2000-10-11 07:04:49 +0000 | [diff] [blame] | 1188 | Py_INCREF(v); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1189 | STACKADJ(3); |
| 1190 | SET_TOP(x); |
| 1191 | SET_SECOND(w); |
| 1192 | SET_THIRD(v); |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 1193 | goto fast_next_opcode; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1194 | } |
Raymond Hettinger | 4bad9ba | 2003-01-19 05:08:13 +0000 | [diff] [blame] | 1195 | Py_FatalError("invalid argument to DUP_TOPX" |
| 1196 | " (bytecode corruption?)"); |
Jeffrey Yasskin | 2d873bd | 2008-12-08 18:55:24 +0000 | [diff] [blame] | 1197 | /* Never returns, so don't bother to set why. */ |
Tim Peters | 35ba689 | 2000-10-11 07:04:49 +0000 | [diff] [blame] | 1198 | break; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1199 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1200 | case UNARY_POSITIVE: |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1201 | v = TOP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1202 | x = PyNumber_Positive(v); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1203 | Py_DECREF(v); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1204 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1205 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1206 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1207 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1208 | case UNARY_NEGATIVE: |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1209 | v = TOP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1210 | x = PyNumber_Negative(v); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1211 | Py_DECREF(v); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1212 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1213 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1214 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1215 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1216 | case UNARY_NOT: |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1217 | v = TOP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1218 | err = PyObject_IsTrue(v); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1219 | Py_DECREF(v); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1220 | if (err == 0) { |
| 1221 | Py_INCREF(Py_True); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1222 | SET_TOP(Py_True); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1223 | continue; |
| 1224 | } |
| 1225 | else if (err > 0) { |
| 1226 | Py_INCREF(Py_False); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1227 | SET_TOP(Py_False); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1228 | err = 0; |
| 1229 | continue; |
| 1230 | } |
Raymond Hettinger | 8bb90a5 | 2003-01-14 12:43:10 +0000 | [diff] [blame] | 1231 | STACKADJ(-1); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1232 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1233 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1234 | case UNARY_CONVERT: |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1235 | v = TOP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1236 | x = PyObject_Repr(v); |
| 1237 | Py_DECREF(v); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1238 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1239 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1240 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1241 | |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1242 | case UNARY_INVERT: |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1243 | v = TOP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1244 | x = PyNumber_Invert(v); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1245 | Py_DECREF(v); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1246 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1247 | if (x != NULL) continue; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1248 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1249 | |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1250 | case BINARY_POWER: |
| 1251 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1252 | v = TOP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1253 | x = PyNumber_Power(v, w, Py_None); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1254 | Py_DECREF(v); |
| 1255 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1256 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1257 | if (x != NULL) continue; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1258 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1259 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1260 | case BINARY_MULTIPLY: |
| 1261 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1262 | v = TOP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1263 | x = PyNumber_Multiply(v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1264 | Py_DECREF(v); |
| 1265 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1266 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1267 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1268 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1269 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1270 | case BINARY_DIVIDE: |
Tim Peters | 3caca23 | 2001-12-06 06:23:26 +0000 | [diff] [blame] | 1271 | if (!_Py_QnewFlag) { |
| 1272 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1273 | v = TOP(); |
Tim Peters | 3caca23 | 2001-12-06 06:23:26 +0000 | [diff] [blame] | 1274 | x = PyNumber_Divide(v, w); |
| 1275 | Py_DECREF(v); |
| 1276 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1277 | SET_TOP(x); |
Tim Peters | 3caca23 | 2001-12-06 06:23:26 +0000 | [diff] [blame] | 1278 | if (x != NULL) continue; |
| 1279 | break; |
| 1280 | } |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1281 | /* -Qnew is in effect: fall through to |
Tim Peters | 3caca23 | 2001-12-06 06:23:26 +0000 | [diff] [blame] | 1282 | BINARY_TRUE_DIVIDE */ |
| 1283 | case BINARY_TRUE_DIVIDE: |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1284 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1285 | v = TOP(); |
Tim Peters | 3caca23 | 2001-12-06 06:23:26 +0000 | [diff] [blame] | 1286 | x = PyNumber_TrueDivide(v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1287 | Py_DECREF(v); |
| 1288 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1289 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1290 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1291 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1292 | |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1293 | case BINARY_FLOOR_DIVIDE: |
| 1294 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1295 | v = TOP(); |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1296 | x = PyNumber_FloorDivide(v, w); |
| 1297 | Py_DECREF(v); |
| 1298 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1299 | SET_TOP(x); |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1300 | if (x != NULL) continue; |
| 1301 | break; |
| 1302 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1303 | case BINARY_MODULO: |
| 1304 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1305 | v = TOP(); |
Collin Winter | 8725dce | 2009-02-20 19:30:41 +0000 | [diff] [blame] | 1306 | if (PyString_CheckExact(v)) |
| 1307 | x = PyString_Format(v, w); |
| 1308 | else |
| 1309 | x = PyNumber_Remainder(v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1310 | Py_DECREF(v); |
| 1311 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1312 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1313 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1314 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1315 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1316 | case BINARY_ADD: |
| 1317 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1318 | v = TOP(); |
Tim Peters | c1e6d96 | 2001-10-05 20:21:03 +0000 | [diff] [blame] | 1319 | if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) { |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1320 | /* INLINE: int + int */ |
| 1321 | register long a, b, i; |
Guido van Rossum | cf183ac | 1998-12-04 18:51:36 +0000 | [diff] [blame] | 1322 | a = PyInt_AS_LONG(v); |
| 1323 | b = PyInt_AS_LONG(w); |
Mark Dickinson | 3439818 | 2009-12-02 17:33:41 +0000 | [diff] [blame] | 1324 | /* cast to avoid undefined behaviour |
| 1325 | on overflow */ |
| 1326 | i = (long)((unsigned long)a + b); |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1327 | if ((i^a) < 0 && (i^b) < 0) |
| 1328 | goto slow_add; |
| 1329 | x = PyInt_FromLong(i); |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1330 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1331 | else if (PyString_CheckExact(v) && |
| 1332 | PyString_CheckExact(w)) { |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 1333 | x = string_concatenate(v, w, f, next_instr); |
| 1334 | /* string_concatenate consumed the ref to v */ |
| 1335 | goto skip_decref_vx; |
| 1336 | } |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1337 | else { |
| 1338 | slow_add: |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1339 | x = PyNumber_Add(v, w); |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1340 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1341 | Py_DECREF(v); |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 1342 | skip_decref_vx: |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1343 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1344 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1345 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1346 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1347 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1348 | case BINARY_SUBTRACT: |
| 1349 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1350 | v = TOP(); |
Tim Peters | c1e6d96 | 2001-10-05 20:21:03 +0000 | [diff] [blame] | 1351 | if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) { |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1352 | /* INLINE: int - int */ |
| 1353 | register long a, b, i; |
Guido van Rossum | cf183ac | 1998-12-04 18:51:36 +0000 | [diff] [blame] | 1354 | a = PyInt_AS_LONG(v); |
| 1355 | b = PyInt_AS_LONG(w); |
Mark Dickinson | 3439818 | 2009-12-02 17:33:41 +0000 | [diff] [blame] | 1356 | /* cast to avoid undefined behaviour |
| 1357 | on overflow */ |
| 1358 | i = (long)((unsigned long)a - b); |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1359 | if ((i^a) < 0 && (i^~b) < 0) |
| 1360 | goto slow_sub; |
| 1361 | x = PyInt_FromLong(i); |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1362 | } |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1363 | else { |
| 1364 | slow_sub: |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1365 | x = PyNumber_Subtract(v, w); |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1366 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1367 | Py_DECREF(v); |
| 1368 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1369 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1370 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1371 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1372 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1373 | case BINARY_SUBSCR: |
| 1374 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1375 | v = TOP(); |
Tim Peters | b1c4698 | 2001-10-05 20:41:38 +0000 | [diff] [blame] | 1376 | if (PyList_CheckExact(v) && PyInt_CheckExact(w)) { |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1377 | /* INLINE: list[int] */ |
Neal Norwitz | 814e938 | 2006-03-02 07:54:28 +0000 | [diff] [blame] | 1378 | Py_ssize_t i = PyInt_AsSsize_t(w); |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1379 | if (i < 0) |
Guido van Rossum | fa00e95 | 1998-07-08 15:02:37 +0000 | [diff] [blame] | 1380 | i += PyList_GET_SIZE(v); |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 1381 | if (i >= 0 && i < PyList_GET_SIZE(v)) { |
Guido van Rossum | fa00e95 | 1998-07-08 15:02:37 +0000 | [diff] [blame] | 1382 | x = PyList_GET_ITEM(v, i); |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1383 | Py_INCREF(x); |
| 1384 | } |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 1385 | else |
| 1386 | goto slow_get; |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1387 | } |
| 1388 | else |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 1389 | slow_get: |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1390 | x = PyObject_GetItem(v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1391 | Py_DECREF(v); |
| 1392 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1393 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1394 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1395 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1396 | |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1397 | case BINARY_LSHIFT: |
| 1398 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1399 | v = TOP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1400 | x = PyNumber_Lshift(v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1401 | Py_DECREF(v); |
| 1402 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1403 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1404 | if (x != NULL) continue; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1405 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1406 | |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1407 | case BINARY_RSHIFT: |
| 1408 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1409 | v = TOP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1410 | x = PyNumber_Rshift(v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1411 | Py_DECREF(v); |
| 1412 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1413 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1414 | if (x != NULL) continue; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1415 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1416 | |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1417 | case BINARY_AND: |
| 1418 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1419 | v = TOP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1420 | x = PyNumber_And(v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1421 | Py_DECREF(v); |
| 1422 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1423 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1424 | if (x != NULL) continue; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1425 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1426 | |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1427 | case BINARY_XOR: |
| 1428 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1429 | v = TOP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1430 | x = PyNumber_Xor(v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1431 | Py_DECREF(v); |
| 1432 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1433 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1434 | if (x != NULL) continue; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1435 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1436 | |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1437 | case BINARY_OR: |
| 1438 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1439 | v = TOP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1440 | x = PyNumber_Or(v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1441 | Py_DECREF(v); |
| 1442 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1443 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1444 | if (x != NULL) continue; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1445 | break; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1446 | |
Raymond Hettinger | dd80f76 | 2004-03-07 07:31:06 +0000 | [diff] [blame] | 1447 | case LIST_APPEND: |
| 1448 | w = POP(); |
Benjamin Peterson | 8f7b94e | 2009-06-28 16:14:07 +0000 | [diff] [blame] | 1449 | v = PEEK(oparg); |
Raymond Hettinger | dd80f76 | 2004-03-07 07:31:06 +0000 | [diff] [blame] | 1450 | err = PyList_Append(v, w); |
Raymond Hettinger | dd80f76 | 2004-03-07 07:31:06 +0000 | [diff] [blame] | 1451 | Py_DECREF(w); |
Raymond Hettinger | fba1cfc | 2004-03-12 16:33:17 +0000 | [diff] [blame] | 1452 | if (err == 0) { |
| 1453 | PREDICT(JUMP_ABSOLUTE); |
| 1454 | continue; |
| 1455 | } |
Raymond Hettinger | dd80f76 | 2004-03-07 07:31:06 +0000 | [diff] [blame] | 1456 | break; |
| 1457 | |
Alexandre Vassalotti | b646547 | 2010-01-11 22:36:12 +0000 | [diff] [blame] | 1458 | case SET_ADD: |
| 1459 | w = POP(); |
| 1460 | v = stack_pointer[-oparg]; |
| 1461 | err = PySet_Add(v, w); |
| 1462 | Py_DECREF(w); |
| 1463 | if (err == 0) { |
| 1464 | PREDICT(JUMP_ABSOLUTE); |
| 1465 | continue; |
| 1466 | } |
| 1467 | break; |
| 1468 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1469 | case INPLACE_POWER: |
| 1470 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1471 | v = TOP(); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1472 | x = PyNumber_InPlacePower(v, w, Py_None); |
| 1473 | Py_DECREF(v); |
| 1474 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1475 | SET_TOP(x); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1476 | if (x != NULL) continue; |
| 1477 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1478 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1479 | case INPLACE_MULTIPLY: |
| 1480 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1481 | v = TOP(); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1482 | x = PyNumber_InPlaceMultiply(v, w); |
| 1483 | Py_DECREF(v); |
| 1484 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1485 | SET_TOP(x); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1486 | if (x != NULL) continue; |
| 1487 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1488 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1489 | case INPLACE_DIVIDE: |
Tim Peters | 54b1191 | 2001-12-25 18:49:11 +0000 | [diff] [blame] | 1490 | if (!_Py_QnewFlag) { |
| 1491 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1492 | v = TOP(); |
Tim Peters | 54b1191 | 2001-12-25 18:49:11 +0000 | [diff] [blame] | 1493 | x = PyNumber_InPlaceDivide(v, w); |
| 1494 | Py_DECREF(v); |
| 1495 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1496 | SET_TOP(x); |
Tim Peters | 54b1191 | 2001-12-25 18:49:11 +0000 | [diff] [blame] | 1497 | if (x != NULL) continue; |
| 1498 | break; |
| 1499 | } |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1500 | /* -Qnew is in effect: fall through to |
Tim Peters | 54b1191 | 2001-12-25 18:49:11 +0000 | [diff] [blame] | 1501 | INPLACE_TRUE_DIVIDE */ |
| 1502 | case INPLACE_TRUE_DIVIDE: |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1503 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1504 | v = TOP(); |
Tim Peters | 54b1191 | 2001-12-25 18:49:11 +0000 | [diff] [blame] | 1505 | x = PyNumber_InPlaceTrueDivide(v, w); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1506 | Py_DECREF(v); |
| 1507 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1508 | SET_TOP(x); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1509 | if (x != NULL) continue; |
| 1510 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1511 | |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1512 | case INPLACE_FLOOR_DIVIDE: |
| 1513 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1514 | v = TOP(); |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1515 | x = PyNumber_InPlaceFloorDivide(v, w); |
| 1516 | Py_DECREF(v); |
| 1517 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1518 | SET_TOP(x); |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1519 | if (x != NULL) continue; |
| 1520 | break; |
| 1521 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1522 | case INPLACE_MODULO: |
| 1523 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1524 | v = TOP(); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1525 | x = PyNumber_InPlaceRemainder(v, w); |
| 1526 | Py_DECREF(v); |
| 1527 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1528 | SET_TOP(x); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1529 | if (x != NULL) continue; |
| 1530 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1531 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1532 | case INPLACE_ADD: |
| 1533 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1534 | v = TOP(); |
Tim Peters | c1e6d96 | 2001-10-05 20:21:03 +0000 | [diff] [blame] | 1535 | if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1536 | /* INLINE: int + int */ |
| 1537 | register long a, b, i; |
| 1538 | a = PyInt_AS_LONG(v); |
| 1539 | b = PyInt_AS_LONG(w); |
| 1540 | i = a + b; |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1541 | if ((i^a) < 0 && (i^b) < 0) |
| 1542 | goto slow_iadd; |
| 1543 | x = PyInt_FromLong(i); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1544 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1545 | else if (PyString_CheckExact(v) && |
| 1546 | PyString_CheckExact(w)) { |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 1547 | x = string_concatenate(v, w, f, next_instr); |
| 1548 | /* string_concatenate consumed the ref to v */ |
| 1549 | goto skip_decref_v; |
| 1550 | } |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1551 | else { |
| 1552 | slow_iadd: |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1553 | x = PyNumber_InPlaceAdd(v, w); |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1554 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1555 | Py_DECREF(v); |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 1556 | skip_decref_v: |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1557 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1558 | SET_TOP(x); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1559 | if (x != NULL) continue; |
| 1560 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1561 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1562 | case INPLACE_SUBTRACT: |
| 1563 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1564 | v = TOP(); |
Tim Peters | c1e6d96 | 2001-10-05 20:21:03 +0000 | [diff] [blame] | 1565 | if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1566 | /* INLINE: int - int */ |
| 1567 | register long a, b, i; |
| 1568 | a = PyInt_AS_LONG(v); |
| 1569 | b = PyInt_AS_LONG(w); |
| 1570 | i = a - b; |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1571 | if ((i^a) < 0 && (i^~b) < 0) |
| 1572 | goto slow_isub; |
| 1573 | x = PyInt_FromLong(i); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1574 | } |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1575 | else { |
| 1576 | slow_isub: |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1577 | x = PyNumber_InPlaceSubtract(v, w); |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1578 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1579 | Py_DECREF(v); |
| 1580 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1581 | SET_TOP(x); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1582 | if (x != NULL) continue; |
| 1583 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1584 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1585 | case INPLACE_LSHIFT: |
| 1586 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1587 | v = TOP(); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1588 | x = PyNumber_InPlaceLshift(v, w); |
| 1589 | Py_DECREF(v); |
| 1590 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1591 | SET_TOP(x); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1592 | if (x != NULL) continue; |
| 1593 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1594 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1595 | case INPLACE_RSHIFT: |
| 1596 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1597 | v = TOP(); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1598 | x = PyNumber_InPlaceRshift(v, w); |
| 1599 | Py_DECREF(v); |
| 1600 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1601 | SET_TOP(x); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1602 | if (x != NULL) continue; |
| 1603 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1604 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1605 | case INPLACE_AND: |
| 1606 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1607 | v = TOP(); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1608 | x = PyNumber_InPlaceAnd(v, w); |
| 1609 | Py_DECREF(v); |
| 1610 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1611 | SET_TOP(x); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1612 | if (x != NULL) continue; |
| 1613 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1614 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1615 | case INPLACE_XOR: |
| 1616 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1617 | v = TOP(); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1618 | x = PyNumber_InPlaceXor(v, w); |
| 1619 | Py_DECREF(v); |
| 1620 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1621 | SET_TOP(x); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1622 | if (x != NULL) continue; |
| 1623 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1624 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1625 | case INPLACE_OR: |
| 1626 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1627 | v = TOP(); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1628 | x = PyNumber_InPlaceOr(v, w); |
| 1629 | Py_DECREF(v); |
| 1630 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1631 | SET_TOP(x); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1632 | if (x != NULL) continue; |
| 1633 | break; |
| 1634 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1635 | case SLICE+0: |
| 1636 | case SLICE+1: |
| 1637 | case SLICE+2: |
| 1638 | case SLICE+3: |
| 1639 | if ((opcode-SLICE) & 2) |
| 1640 | w = POP(); |
| 1641 | else |
| 1642 | w = NULL; |
| 1643 | if ((opcode-SLICE) & 1) |
| 1644 | v = POP(); |
| 1645 | else |
| 1646 | v = NULL; |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1647 | u = TOP(); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1648 | x = apply_slice(u, v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1649 | Py_DECREF(u); |
| 1650 | Py_XDECREF(v); |
| 1651 | Py_XDECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1652 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1653 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1654 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1655 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1656 | case STORE_SLICE+0: |
| 1657 | case STORE_SLICE+1: |
| 1658 | case STORE_SLICE+2: |
| 1659 | case STORE_SLICE+3: |
| 1660 | if ((opcode-STORE_SLICE) & 2) |
| 1661 | w = POP(); |
| 1662 | else |
| 1663 | w = NULL; |
| 1664 | if ((opcode-STORE_SLICE) & 1) |
| 1665 | v = POP(); |
| 1666 | else |
| 1667 | v = NULL; |
| 1668 | u = POP(); |
| 1669 | t = POP(); |
| 1670 | err = assign_slice(u, v, w, t); /* u[v:w] = t */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1671 | Py_DECREF(t); |
| 1672 | Py_DECREF(u); |
| 1673 | Py_XDECREF(v); |
| 1674 | Py_XDECREF(w); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1675 | if (err == 0) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1676 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1677 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1678 | case DELETE_SLICE+0: |
| 1679 | case DELETE_SLICE+1: |
| 1680 | case DELETE_SLICE+2: |
| 1681 | case DELETE_SLICE+3: |
| 1682 | if ((opcode-DELETE_SLICE) & 2) |
| 1683 | w = POP(); |
| 1684 | else |
| 1685 | w = NULL; |
| 1686 | if ((opcode-DELETE_SLICE) & 1) |
| 1687 | v = POP(); |
| 1688 | else |
| 1689 | v = NULL; |
| 1690 | u = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1691 | err = assign_slice(u, v, w, (PyObject *)NULL); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1692 | /* del u[v:w] */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1693 | Py_DECREF(u); |
| 1694 | Py_XDECREF(v); |
| 1695 | Py_XDECREF(w); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1696 | if (err == 0) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1697 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1698 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1699 | case STORE_SUBSCR: |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1700 | w = TOP(); |
| 1701 | v = SECOND(); |
| 1702 | u = THIRD(); |
| 1703 | STACKADJ(-3); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1704 | /* v[w] = u */ |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1705 | err = PyObject_SetItem(v, w, u); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1706 | Py_DECREF(u); |
| 1707 | Py_DECREF(v); |
| 1708 | Py_DECREF(w); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1709 | if (err == 0) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1710 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1711 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1712 | case DELETE_SUBSCR: |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1713 | w = TOP(); |
| 1714 | v = SECOND(); |
| 1715 | STACKADJ(-2); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1716 | /* del v[w] */ |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1717 | err = PyObject_DelItem(v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1718 | Py_DECREF(v); |
| 1719 | Py_DECREF(w); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1720 | if (err == 0) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1721 | break; |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1722 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1723 | case PRINT_EXPR: |
| 1724 | v = POP(); |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 1725 | w = PySys_GetObject("displayhook"); |
| 1726 | if (w == NULL) { |
| 1727 | PyErr_SetString(PyExc_RuntimeError, |
| 1728 | "lost sys.displayhook"); |
| 1729 | err = -1; |
Moshe Zadka | f5df383 | 2001-01-11 11:55:37 +0000 | [diff] [blame] | 1730 | x = NULL; |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 1731 | } |
| 1732 | if (err == 0) { |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 1733 | x = PyTuple_Pack(1, v); |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 1734 | if (x == NULL) |
| 1735 | err = -1; |
| 1736 | } |
| 1737 | if (err == 0) { |
| 1738 | w = PyEval_CallObject(w, x); |
Moshe Zadka | f5df383 | 2001-01-11 11:55:37 +0000 | [diff] [blame] | 1739 | Py_XDECREF(w); |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 1740 | if (w == NULL) |
| 1741 | err = -1; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1742 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1743 | Py_DECREF(v); |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 1744 | Py_XDECREF(x); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1745 | break; |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 1746 | |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1747 | case PRINT_ITEM_TO: |
| 1748 | w = stream = POP(); |
| 1749 | /* fall through to PRINT_ITEM */ |
| 1750 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1751 | case PRINT_ITEM: |
| 1752 | v = POP(); |
Barry Warsaw | 093abe0 | 2000-08-29 04:56:13 +0000 | [diff] [blame] | 1753 | if (stream == NULL || stream == Py_None) { |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1754 | w = PySys_GetObject("stdout"); |
| 1755 | if (w == NULL) { |
| 1756 | PyErr_SetString(PyExc_RuntimeError, |
| 1757 | "lost sys.stdout"); |
| 1758 | err = -1; |
| 1759 | } |
Guido van Rossum | 8f18320 | 1997-12-31 05:53:15 +0000 | [diff] [blame] | 1760 | } |
Neal Norwitz | c5131bc | 2003-06-29 14:48:32 +0000 | [diff] [blame] | 1761 | /* PyFile_SoftSpace() can exececute arbitrary code |
| 1762 | if sys.stdout is an instance with a __getattr__. |
| 1763 | If __getattr__ raises an exception, w will |
| 1764 | be freed, so we need to prevent that temporarily. */ |
| 1765 | Py_XINCREF(w); |
Tim Peters | 8e5fd53 | 2002-03-24 19:25:00 +0000 | [diff] [blame] | 1766 | if (w != NULL && PyFile_SoftSpace(w, 0)) |
Guido van Rossum | be27026 | 1997-05-22 22:26:18 +0000 | [diff] [blame] | 1767 | err = PyFile_WriteString(" ", w); |
| 1768 | if (err == 0) |
| 1769 | err = PyFile_WriteObject(v, w, Py_PRINT_RAW); |
Marc-André Lemburg | 0c4d8d0 | 2001-11-20 15:17:25 +0000 | [diff] [blame] | 1770 | if (err == 0) { |
Tim Peters | 8e5fd53 | 2002-03-24 19:25:00 +0000 | [diff] [blame] | 1771 | /* XXX move into writeobject() ? */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1772 | if (PyString_Check(v)) { |
| 1773 | char *s = PyString_AS_STRING(v); |
| 1774 | Py_ssize_t len = PyString_GET_SIZE(v); |
Tim Peters | 8e5fd53 | 2002-03-24 19:25:00 +0000 | [diff] [blame] | 1775 | if (len == 0 || |
| 1776 | !isspace(Py_CHARMASK(s[len-1])) || |
| 1777 | s[len-1] == ' ') |
| 1778 | PyFile_SoftSpace(w, 1); |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 1779 | } |
Martin v. Löwis | 8d3ce5a | 2001-12-18 22:36:40 +0000 | [diff] [blame] | 1780 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | 0c4d8d0 | 2001-11-20 15:17:25 +0000 | [diff] [blame] | 1781 | else if (PyUnicode_Check(v)) { |
| 1782 | Py_UNICODE *s = PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 6685128 | 2006-04-22 11:40:03 +0000 | [diff] [blame] | 1783 | Py_ssize_t len = PyUnicode_GET_SIZE(v); |
Tim Peters | 8e5fd53 | 2002-03-24 19:25:00 +0000 | [diff] [blame] | 1784 | if (len == 0 || |
| 1785 | !Py_UNICODE_ISSPACE(s[len-1]) || |
| 1786 | s[len-1] == ' ') |
| 1787 | PyFile_SoftSpace(w, 1); |
Marc-André Lemburg | 0c4d8d0 | 2001-11-20 15:17:25 +0000 | [diff] [blame] | 1788 | } |
Michael W. Hudson | d95c828 | 2002-05-20 13:56:11 +0000 | [diff] [blame] | 1789 | #endif |
Tim Peters | 8e5fd53 | 2002-03-24 19:25:00 +0000 | [diff] [blame] | 1790 | else |
| 1791 | PyFile_SoftSpace(w, 1); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1792 | } |
Neal Norwitz | c5131bc | 2003-06-29 14:48:32 +0000 | [diff] [blame] | 1793 | Py_XDECREF(w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1794 | Py_DECREF(v); |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1795 | Py_XDECREF(stream); |
| 1796 | stream = NULL; |
| 1797 | if (err == 0) |
| 1798 | continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1799 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1800 | |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1801 | case PRINT_NEWLINE_TO: |
| 1802 | w = stream = POP(); |
| 1803 | /* fall through to PRINT_NEWLINE */ |
| 1804 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1805 | case PRINT_NEWLINE: |
Barry Warsaw | 093abe0 | 2000-08-29 04:56:13 +0000 | [diff] [blame] | 1806 | if (stream == NULL || stream == Py_None) { |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1807 | w = PySys_GetObject("stdout"); |
Jeffrey Yasskin | 2d873bd | 2008-12-08 18:55:24 +0000 | [diff] [blame] | 1808 | if (w == NULL) { |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1809 | PyErr_SetString(PyExc_RuntimeError, |
| 1810 | "lost sys.stdout"); |
Jeffrey Yasskin | 2d873bd | 2008-12-08 18:55:24 +0000 | [diff] [blame] | 1811 | why = WHY_EXCEPTION; |
| 1812 | } |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1813 | } |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1814 | if (w != NULL) { |
Georg Brandl | aa76d77 | 2008-07-01 20:56:03 +0000 | [diff] [blame] | 1815 | /* w.write() may replace sys.stdout, so we |
| 1816 | * have to keep our reference to it */ |
Amaury Forgeot d'Arc | bdd941f | 2008-07-01 20:38:04 +0000 | [diff] [blame] | 1817 | Py_INCREF(w); |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1818 | err = PyFile_WriteString("\n", w); |
| 1819 | if (err == 0) |
| 1820 | PyFile_SoftSpace(w, 0); |
Amaury Forgeot d'Arc | bdd941f | 2008-07-01 20:38:04 +0000 | [diff] [blame] | 1821 | Py_DECREF(w); |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1822 | } |
| 1823 | Py_XDECREF(stream); |
| 1824 | stream = NULL; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1825 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1826 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1827 | |
| 1828 | #ifdef CASE_TOO_BIG |
| 1829 | default: switch (opcode) { |
| 1830 | #endif |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1831 | case RAISE_VARARGS: |
| 1832 | u = v = w = NULL; |
| 1833 | switch (oparg) { |
| 1834 | case 3: |
| 1835 | u = POP(); /* traceback */ |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1836 | /* Fallthrough */ |
| 1837 | case 2: |
| 1838 | v = POP(); /* value */ |
| 1839 | /* Fallthrough */ |
| 1840 | case 1: |
| 1841 | w = POP(); /* exc */ |
Guido van Rossum | d295f12 | 1998-04-09 21:39:57 +0000 | [diff] [blame] | 1842 | case 0: /* Fallthrough */ |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 1843 | why = do_raise(w, v, u); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1844 | break; |
| 1845 | default: |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1846 | PyErr_SetString(PyExc_SystemError, |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1847 | "bad RAISE_VARARGS oparg"); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1848 | why = WHY_EXCEPTION; |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 1849 | break; |
| 1850 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1851 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1852 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1853 | case LOAD_LOCALS: |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 1854 | if ((x = f->f_locals) != NULL) { |
| 1855 | Py_INCREF(x); |
| 1856 | PUSH(x); |
| 1857 | continue; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1858 | } |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 1859 | PyErr_SetString(PyExc_SystemError, "no locals"); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1860 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1861 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1862 | case RETURN_VALUE: |
| 1863 | retval = POP(); |
| 1864 | why = WHY_RETURN; |
Raymond Hettinger | 1dd8309 | 2004-02-06 18:32:33 +0000 | [diff] [blame] | 1865 | goto fast_block_end; |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 1866 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1867 | case YIELD_VALUE: |
| 1868 | retval = POP(); |
Tim Peters | 8c96369 | 2001-06-23 05:26:56 +0000 | [diff] [blame] | 1869 | f->f_stacktop = stack_pointer; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1870 | why = WHY_YIELD; |
Raymond Hettinger | 1dd8309 | 2004-02-06 18:32:33 +0000 | [diff] [blame] | 1871 | goto fast_yield; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1872 | |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 1873 | case EXEC_STMT: |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1874 | w = TOP(); |
| 1875 | v = SECOND(); |
| 1876 | u = THIRD(); |
| 1877 | STACKADJ(-3); |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 1878 | READ_TIMESTAMP(intr0); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 1879 | err = exec_statement(f, u, v, w); |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 1880 | READ_TIMESTAMP(intr1); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1881 | Py_DECREF(u); |
| 1882 | Py_DECREF(v); |
| 1883 | Py_DECREF(w); |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 1884 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1885 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1886 | case POP_BLOCK: |
| 1887 | { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1888 | PyTryBlock *b = PyFrame_BlockPop(f); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1889 | while (STACK_LEVEL() > b->b_level) { |
| 1890 | v = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1891 | Py_DECREF(v); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1892 | } |
| 1893 | } |
Raymond Hettinger | 7eddd78 | 2004-04-07 14:38:08 +0000 | [diff] [blame] | 1894 | continue; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1895 | |
Jeffrey Yasskin | 9063a99 | 2008-03-03 01:27:03 +0000 | [diff] [blame] | 1896 | PREDICTED(END_FINALLY); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1897 | case END_FINALLY: |
| 1898 | v = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1899 | if (PyInt_Check(v)) { |
Raymond Hettinger | 7c95865 | 2004-04-06 10:11:10 +0000 | [diff] [blame] | 1900 | why = (enum why_code) PyInt_AS_LONG(v); |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 1901 | assert(why != WHY_YIELD); |
Raymond Hettinger | c8aa08b | 2004-04-11 14:59:33 +0000 | [diff] [blame] | 1902 | if (why == WHY_RETURN || |
| 1903 | why == WHY_CONTINUE) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1904 | retval = POP(); |
| 1905 | } |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 1906 | else if (PyExceptionClass_Check(v) || |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1907 | PyString_Check(v)) { |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1908 | w = POP(); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1909 | u = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1910 | PyErr_Restore(v, w, u); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1911 | why = WHY_RERAISE; |
Guido van Rossum | 0db1ef9 | 1995-07-28 23:06:00 +0000 | [diff] [blame] | 1912 | break; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1913 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1914 | else if (v != Py_None) { |
| 1915 | PyErr_SetString(PyExc_SystemError, |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1916 | "'finally' pops bad exception"); |
| 1917 | why = WHY_EXCEPTION; |
| 1918 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1919 | Py_DECREF(v); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1920 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1921 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1922 | case BUILD_CLASS: |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1923 | u = TOP(); |
| 1924 | v = SECOND(); |
| 1925 | w = THIRD(); |
| 1926 | STACKADJ(-2); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1927 | x = build_class(u, v, w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1928 | SET_TOP(x); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1929 | Py_DECREF(u); |
| 1930 | Py_DECREF(v); |
| 1931 | Py_DECREF(w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1932 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1933 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1934 | case STORE_NAME: |
Skip Montanaro | 496e658 | 2002-08-06 17:47:40 +0000 | [diff] [blame] | 1935 | w = GETITEM(names, oparg); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1936 | v = POP(); |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 1937 | if ((x = f->f_locals) != NULL) { |
Raymond Hettinger | 66bd233 | 2004-08-02 08:30:07 +0000 | [diff] [blame] | 1938 | if (PyDict_CheckExact(x)) |
Raymond Hettinger | 214b1c3 | 2004-07-02 06:41:07 +0000 | [diff] [blame] | 1939 | err = PyDict_SetItem(x, w, v); |
| 1940 | else |
| 1941 | err = PyObject_SetItem(x, w, v); |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 1942 | Py_DECREF(v); |
Raymond Hettinger | 7eddd78 | 2004-04-07 14:38:08 +0000 | [diff] [blame] | 1943 | if (err == 0) continue; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1944 | break; |
| 1945 | } |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 1946 | PyErr_Format(PyExc_SystemError, |
| 1947 | "no locals found when storing %s", |
| 1948 | PyObject_REPR(w)); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1949 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1950 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1951 | case DELETE_NAME: |
Skip Montanaro | 496e658 | 2002-08-06 17:47:40 +0000 | [diff] [blame] | 1952 | w = GETITEM(names, oparg); |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 1953 | if ((x = f->f_locals) != NULL) { |
Raymond Hettinger | 214b1c3 | 2004-07-02 06:41:07 +0000 | [diff] [blame] | 1954 | if ((err = PyObject_DelItem(x, w)) != 0) |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 1955 | format_exc_check_arg(PyExc_NameError, |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 1956 | NAME_ERROR_MSG, |
| 1957 | w); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1958 | break; |
| 1959 | } |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 1960 | PyErr_Format(PyExc_SystemError, |
| 1961 | "no locals when deleting %s", |
| 1962 | PyObject_REPR(w)); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1963 | break; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1964 | |
Raymond Hettinger | 7dc5221 | 2003-03-16 20:14:44 +0000 | [diff] [blame] | 1965 | PREDICTED_WITH_ARG(UNPACK_SEQUENCE); |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 1966 | case UNPACK_SEQUENCE: |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1967 | v = POP(); |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 1968 | if (PyTuple_CheckExact(v) && |
| 1969 | PyTuple_GET_SIZE(v) == oparg) { |
| 1970 | PyObject **items = \ |
| 1971 | ((PyTupleObject *)v)->ob_item; |
Raymond Hettinger | f114a3a | 2004-03-08 23:25:30 +0000 | [diff] [blame] | 1972 | while (oparg--) { |
| 1973 | w = items[oparg]; |
| 1974 | Py_INCREF(w); |
| 1975 | PUSH(w); |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 1976 | } |
Raymond Hettinger | 7eddd78 | 2004-04-07 14:38:08 +0000 | [diff] [blame] | 1977 | Py_DECREF(v); |
| 1978 | continue; |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 1979 | } else if (PyList_CheckExact(v) && |
| 1980 | PyList_GET_SIZE(v) == oparg) { |
| 1981 | PyObject **items = \ |
| 1982 | ((PyListObject *)v)->ob_item; |
Raymond Hettinger | f114a3a | 2004-03-08 23:25:30 +0000 | [diff] [blame] | 1983 | while (oparg--) { |
| 1984 | w = items[oparg]; |
| 1985 | Py_INCREF(w); |
| 1986 | PUSH(w); |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 1987 | } |
Raymond Hettinger | f114a3a | 2004-03-08 23:25:30 +0000 | [diff] [blame] | 1988 | } else if (unpack_iterable(v, oparg, |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 1989 | stack_pointer + oparg)) { |
Benjamin Peterson | 8f7b94e | 2009-06-28 16:14:07 +0000 | [diff] [blame] | 1990 | STACKADJ(oparg); |
Georg Brandl | 5cb76c1 | 2007-03-21 09:00:39 +0000 | [diff] [blame] | 1991 | } else { |
| 1992 | /* unpack_iterable() raised an exception */ |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 1993 | why = WHY_EXCEPTION; |
Tim Peters | 8b13b3e | 2001-09-30 05:58:42 +0000 | [diff] [blame] | 1994 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1995 | Py_DECREF(v); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1996 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1997 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1998 | case STORE_ATTR: |
Skip Montanaro | 496e658 | 2002-08-06 17:47:40 +0000 | [diff] [blame] | 1999 | w = GETITEM(names, oparg); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 2000 | v = TOP(); |
| 2001 | u = SECOND(); |
| 2002 | STACKADJ(-2); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2003 | err = PyObject_SetAttr(v, w, u); /* v.w = u */ |
| 2004 | Py_DECREF(v); |
| 2005 | Py_DECREF(u); |
Raymond Hettinger | 7eddd78 | 2004-04-07 14:38:08 +0000 | [diff] [blame] | 2006 | if (err == 0) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2007 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2008 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2009 | case DELETE_ATTR: |
Skip Montanaro | 496e658 | 2002-08-06 17:47:40 +0000 | [diff] [blame] | 2010 | w = GETITEM(names, oparg); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2011 | v = POP(); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2012 | err = PyObject_SetAttr(v, w, (PyObject *)NULL); |
| 2013 | /* del v.w */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2014 | Py_DECREF(v); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2015 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2016 | |
Guido van Rossum | 32c6cdf | 1991-12-10 13:52:46 +0000 | [diff] [blame] | 2017 | case STORE_GLOBAL: |
Skip Montanaro | 496e658 | 2002-08-06 17:47:40 +0000 | [diff] [blame] | 2018 | w = GETITEM(names, oparg); |
Guido van Rossum | 32c6cdf | 1991-12-10 13:52:46 +0000 | [diff] [blame] | 2019 | v = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2020 | err = PyDict_SetItem(f->f_globals, w, v); |
| 2021 | Py_DECREF(v); |
Raymond Hettinger | 7eddd78 | 2004-04-07 14:38:08 +0000 | [diff] [blame] | 2022 | if (err == 0) continue; |
Guido van Rossum | 32c6cdf | 1991-12-10 13:52:46 +0000 | [diff] [blame] | 2023 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2024 | |
Guido van Rossum | 32c6cdf | 1991-12-10 13:52:46 +0000 | [diff] [blame] | 2025 | case DELETE_GLOBAL: |
Skip Montanaro | 496e658 | 2002-08-06 17:47:40 +0000 | [diff] [blame] | 2026 | w = GETITEM(names, oparg); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2027 | if ((err = PyDict_DelItem(f->f_globals, w)) != 0) |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 2028 | format_exc_check_arg( |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2029 | PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w); |
Guido van Rossum | 32c6cdf | 1991-12-10 13:52:46 +0000 | [diff] [blame] | 2030 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2031 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2032 | case LOAD_NAME: |
Skip Montanaro | 496e658 | 2002-08-06 17:47:40 +0000 | [diff] [blame] | 2033 | w = GETITEM(names, oparg); |
Raymond Hettinger | 214b1c3 | 2004-07-02 06:41:07 +0000 | [diff] [blame] | 2034 | if ((v = f->f_locals) == NULL) { |
Jeremy Hylton | c862cf4 | 2001-01-19 03:25:05 +0000 | [diff] [blame] | 2035 | PyErr_Format(PyExc_SystemError, |
| 2036 | "no locals when loading %s", |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 2037 | PyObject_REPR(w)); |
Jeffrey Yasskin | 2d873bd | 2008-12-08 18:55:24 +0000 | [diff] [blame] | 2038 | why = WHY_EXCEPTION; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2039 | break; |
| 2040 | } |
Michael W. Hudson | a3711f7 | 2004-08-02 14:50:43 +0000 | [diff] [blame] | 2041 | if (PyDict_CheckExact(v)) { |
Raymond Hettinger | 214b1c3 | 2004-07-02 06:41:07 +0000 | [diff] [blame] | 2042 | x = PyDict_GetItem(v, w); |
Michael W. Hudson | a3711f7 | 2004-08-02 14:50:43 +0000 | [diff] [blame] | 2043 | Py_XINCREF(x); |
| 2044 | } |
Raymond Hettinger | 214b1c3 | 2004-07-02 06:41:07 +0000 | [diff] [blame] | 2045 | else { |
| 2046 | x = PyObject_GetItem(v, w); |
| 2047 | if (x == NULL && PyErr_Occurred()) { |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 2048 | if (!PyErr_ExceptionMatches( |
| 2049 | PyExc_KeyError)) |
Raymond Hettinger | 214b1c3 | 2004-07-02 06:41:07 +0000 | [diff] [blame] | 2050 | break; |
| 2051 | PyErr_Clear(); |
| 2052 | } |
| 2053 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2054 | if (x == NULL) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2055 | x = PyDict_GetItem(f->f_globals, w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2056 | if (x == NULL) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2057 | x = PyDict_GetItem(f->f_builtins, w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2058 | if (x == NULL) { |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 2059 | format_exc_check_arg( |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2060 | PyExc_NameError, |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 2061 | NAME_ERROR_MSG, w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2062 | break; |
| 2063 | } |
| 2064 | } |
Michael W. Hudson | a3711f7 | 2004-08-02 14:50:43 +0000 | [diff] [blame] | 2065 | Py_INCREF(x); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2066 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2067 | PUSH(x); |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 2068 | continue; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2069 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2070 | case LOAD_GLOBAL: |
Skip Montanaro | 496e658 | 2002-08-06 17:47:40 +0000 | [diff] [blame] | 2071 | w = GETITEM(names, oparg); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2072 | if (PyString_CheckExact(w)) { |
Guido van Rossum | d8dbf84 | 2002-08-19 21:17:53 +0000 | [diff] [blame] | 2073 | /* Inline the PyDict_GetItem() calls. |
| 2074 | WARNING: this is an extreme speed hack. |
| 2075 | Do not try this at home. */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2076 | long hash = ((PyStringObject *)w)->ob_shash; |
Guido van Rossum | 3a4dfc8 | 2002-08-19 20:24:07 +0000 | [diff] [blame] | 2077 | if (hash != -1) { |
Guido van Rossum | 3a4dfc8 | 2002-08-19 20:24:07 +0000 | [diff] [blame] | 2078 | PyDictObject *d; |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 2079 | PyDictEntry *e; |
Guido van Rossum | 3a4dfc8 | 2002-08-19 20:24:07 +0000 | [diff] [blame] | 2080 | d = (PyDictObject *)(f->f_globals); |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 2081 | e = d->ma_lookup(d, w, hash); |
| 2082 | if (e == NULL) { |
| 2083 | x = NULL; |
| 2084 | break; |
| 2085 | } |
| 2086 | x = e->me_value; |
Guido van Rossum | 3a4dfc8 | 2002-08-19 20:24:07 +0000 | [diff] [blame] | 2087 | if (x != NULL) { |
| 2088 | Py_INCREF(x); |
| 2089 | PUSH(x); |
| 2090 | continue; |
| 2091 | } |
| 2092 | d = (PyDictObject *)(f->f_builtins); |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 2093 | e = d->ma_lookup(d, w, hash); |
| 2094 | if (e == NULL) { |
| 2095 | x = NULL; |
| 2096 | break; |
| 2097 | } |
| 2098 | x = e->me_value; |
Guido van Rossum | 3a4dfc8 | 2002-08-19 20:24:07 +0000 | [diff] [blame] | 2099 | if (x != NULL) { |
| 2100 | Py_INCREF(x); |
| 2101 | PUSH(x); |
| 2102 | continue; |
| 2103 | } |
| 2104 | goto load_global_error; |
| 2105 | } |
| 2106 | } |
| 2107 | /* This is the un-inlined version of the code above */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2108 | x = PyDict_GetItem(f->f_globals, w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2109 | if (x == NULL) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2110 | x = PyDict_GetItem(f->f_builtins, w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2111 | if (x == NULL) { |
Guido van Rossum | 3a4dfc8 | 2002-08-19 20:24:07 +0000 | [diff] [blame] | 2112 | load_global_error: |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 2113 | format_exc_check_arg( |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2114 | PyExc_NameError, |
Guido van Rossum | 3a4dfc8 | 2002-08-19 20:24:07 +0000 | [diff] [blame] | 2115 | GLOBAL_NAME_ERROR_MSG, w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2116 | break; |
| 2117 | } |
| 2118 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2119 | Py_INCREF(x); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2120 | PUSH(x); |
Raymond Hettinger | 7eddd78 | 2004-04-07 14:38:08 +0000 | [diff] [blame] | 2121 | continue; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2122 | |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2123 | case DELETE_FAST: |
Guido van Rossum | 2e4c899 | 1998-05-12 20:27:36 +0000 | [diff] [blame] | 2124 | x = GETLOCAL(oparg); |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 2125 | if (x != NULL) { |
| 2126 | SETLOCAL(oparg, NULL); |
| 2127 | continue; |
Guido van Rossum | 2e4c899 | 1998-05-12 20:27:36 +0000 | [diff] [blame] | 2128 | } |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 2129 | format_exc_check_arg( |
| 2130 | PyExc_UnboundLocalError, |
| 2131 | UNBOUNDLOCAL_ERROR_MSG, |
| 2132 | PyTuple_GetItem(co->co_varnames, oparg) |
| 2133 | ); |
| 2134 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2135 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2136 | case LOAD_CLOSURE: |
Jeremy Hylton | 2b724da | 2001-01-29 22:51:52 +0000 | [diff] [blame] | 2137 | x = freevars[oparg]; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2138 | Py_INCREF(x); |
| 2139 | PUSH(x); |
Raymond Hettinger | 7eddd78 | 2004-04-07 14:38:08 +0000 | [diff] [blame] | 2140 | if (x != NULL) continue; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2141 | break; |
| 2142 | |
| 2143 | case LOAD_DEREF: |
Jeremy Hylton | 2b724da | 2001-01-29 22:51:52 +0000 | [diff] [blame] | 2144 | x = freevars[oparg]; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2145 | w = PyCell_Get(x); |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 2146 | if (w != NULL) { |
| 2147 | PUSH(w); |
| 2148 | continue; |
Jeremy Hylton | 2524d69 | 2001-02-05 17:23:16 +0000 | [diff] [blame] | 2149 | } |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 2150 | err = -1; |
| 2151 | /* Don't stomp existing exception */ |
| 2152 | if (PyErr_Occurred()) |
| 2153 | break; |
Richard Jones | cebbefc | 2006-05-23 18:28:17 +0000 | [diff] [blame] | 2154 | if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) { |
| 2155 | v = PyTuple_GET_ITEM(co->co_cellvars, |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 2156 | oparg); |
| 2157 | format_exc_check_arg( |
| 2158 | PyExc_UnboundLocalError, |
| 2159 | UNBOUNDLOCAL_ERROR_MSG, |
| 2160 | v); |
| 2161 | } else { |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 2162 | v = PyTuple_GET_ITEM(co->co_freevars, oparg - |
| 2163 | PyTuple_GET_SIZE(co->co_cellvars)); |
| 2164 | format_exc_check_arg(PyExc_NameError, |
| 2165 | UNBOUNDFREE_ERROR_MSG, v); |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 2166 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2167 | break; |
| 2168 | |
| 2169 | case STORE_DEREF: |
| 2170 | w = POP(); |
Jeremy Hylton | 2b724da | 2001-01-29 22:51:52 +0000 | [diff] [blame] | 2171 | x = freevars[oparg]; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2172 | PyCell_Set(x, w); |
Jeremy Hylton | 30c9f39 | 2001-03-13 01:58:22 +0000 | [diff] [blame] | 2173 | Py_DECREF(w); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2174 | continue; |
| 2175 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2176 | case BUILD_TUPLE: |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2177 | x = PyTuple_New(oparg); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2178 | if (x != NULL) { |
Raymond Hettinger | 5bed456 | 2004-04-10 23:34:17 +0000 | [diff] [blame] | 2179 | for (; --oparg >= 0;) { |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2180 | w = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2181 | PyTuple_SET_ITEM(x, oparg, w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2182 | } |
| 2183 | PUSH(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 2184 | continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2185 | } |
| 2186 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2187 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2188 | case BUILD_LIST: |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2189 | x = PyList_New(oparg); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2190 | if (x != NULL) { |
Raymond Hettinger | 5bed456 | 2004-04-10 23:34:17 +0000 | [diff] [blame] | 2191 | for (; --oparg >= 0;) { |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2192 | w = POP(); |
Guido van Rossum | 5053efc | 1998-08-04 15:27:50 +0000 | [diff] [blame] | 2193 | PyList_SET_ITEM(x, oparg, w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2194 | } |
| 2195 | PUSH(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 2196 | continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2197 | } |
| 2198 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2199 | |
Alexandre Vassalotti | ee936a2 | 2010-01-09 23:35:54 +0000 | [diff] [blame] | 2200 | case BUILD_SET: |
| 2201 | x = PySet_New(NULL); |
| 2202 | if (x != NULL) { |
| 2203 | for (; --oparg >= 0;) { |
| 2204 | w = POP(); |
| 2205 | if (err == 0) |
| 2206 | err = PySet_Add(x, w); |
| 2207 | Py_DECREF(w); |
| 2208 | } |
| 2209 | if (err != 0) { |
| 2210 | Py_DECREF(x); |
| 2211 | break; |
| 2212 | } |
| 2213 | PUSH(x); |
| 2214 | continue; |
| 2215 | } |
| 2216 | break; |
| 2217 | |
| 2218 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2219 | case BUILD_MAP: |
Raymond Hettinger | fd7ed40 | 2007-12-18 21:24:09 +0000 | [diff] [blame] | 2220 | x = _PyDict_NewPresized((Py_ssize_t)oparg); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2221 | PUSH(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 2222 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2223 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2224 | |
Raymond Hettinger | effde12 | 2007-12-18 18:26:18 +0000 | [diff] [blame] | 2225 | case STORE_MAP: |
| 2226 | w = TOP(); /* key */ |
| 2227 | u = SECOND(); /* value */ |
| 2228 | v = THIRD(); /* dict */ |
| 2229 | STACKADJ(-2); |
| 2230 | assert (PyDict_CheckExact(v)); |
| 2231 | err = PyDict_SetItem(v, w, u); /* v[w] = u */ |
| 2232 | Py_DECREF(u); |
| 2233 | Py_DECREF(w); |
| 2234 | if (err == 0) continue; |
| 2235 | break; |
| 2236 | |
Alexandre Vassalotti | b646547 | 2010-01-11 22:36:12 +0000 | [diff] [blame] | 2237 | case MAP_ADD: |
| 2238 | w = TOP(); /* key */ |
| 2239 | u = SECOND(); /* value */ |
| 2240 | STACKADJ(-2); |
| 2241 | v = stack_pointer[-oparg]; /* dict */ |
| 2242 | assert (PyDict_CheckExact(v)); |
| 2243 | err = PyDict_SetItem(v, w, u); /* v[w] = u */ |
| 2244 | Py_DECREF(u); |
| 2245 | Py_DECREF(w); |
| 2246 | if (err == 0) { |
| 2247 | PREDICT(JUMP_ABSOLUTE); |
| 2248 | continue; |
| 2249 | } |
| 2250 | break; |
| 2251 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2252 | case LOAD_ATTR: |
Skip Montanaro | 496e658 | 2002-08-06 17:47:40 +0000 | [diff] [blame] | 2253 | w = GETITEM(names, oparg); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 2254 | v = TOP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2255 | x = PyObject_GetAttr(v, w); |
| 2256 | Py_DECREF(v); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 2257 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 2258 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2259 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2260 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2261 | case COMPARE_OP: |
| 2262 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 2263 | v = TOP(); |
Raymond Hettinger | 4bad9ba | 2003-01-19 05:08:13 +0000 | [diff] [blame] | 2264 | if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) { |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 2265 | /* INLINE: cmp(int, int) */ |
| 2266 | register long a, b; |
| 2267 | register int res; |
Guido van Rossum | cf183ac | 1998-12-04 18:51:36 +0000 | [diff] [blame] | 2268 | a = PyInt_AS_LONG(v); |
| 2269 | b = PyInt_AS_LONG(w); |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 2270 | switch (oparg) { |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 2271 | case PyCmp_LT: res = a < b; break; |
| 2272 | case PyCmp_LE: res = a <= b; break; |
| 2273 | case PyCmp_EQ: res = a == b; break; |
| 2274 | case PyCmp_NE: res = a != b; break; |
| 2275 | case PyCmp_GT: res = a > b; break; |
| 2276 | case PyCmp_GE: res = a >= b; break; |
| 2277 | case PyCmp_IS: res = v == w; break; |
| 2278 | case PyCmp_IS_NOT: res = v != w; break; |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 2279 | default: goto slow_compare; |
| 2280 | } |
| 2281 | x = res ? Py_True : Py_False; |
| 2282 | Py_INCREF(x); |
| 2283 | } |
| 2284 | else { |
| 2285 | slow_compare: |
| 2286 | x = cmp_outcome(oparg, v, w); |
| 2287 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2288 | Py_DECREF(v); |
| 2289 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 2290 | SET_TOP(x); |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 2291 | if (x == NULL) break; |
Jeffrey Yasskin | 68d6852 | 2009-02-28 19:03:21 +0000 | [diff] [blame] | 2292 | PREDICT(POP_JUMP_IF_FALSE); |
| 2293 | PREDICT(POP_JUMP_IF_TRUE); |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 2294 | continue; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2295 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2296 | case IMPORT_NAME: |
Skip Montanaro | 496e658 | 2002-08-06 17:47:40 +0000 | [diff] [blame] | 2297 | w = GETITEM(names, oparg); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2298 | x = PyDict_GetItemString(f->f_builtins, "__import__"); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2299 | if (x == NULL) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2300 | PyErr_SetString(PyExc_ImportError, |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 2301 | "__import__ not found"); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2302 | break; |
| 2303 | } |
Guido van Rossum | 1d9a9ea | 2008-01-23 20:19:01 +0000 | [diff] [blame] | 2304 | Py_INCREF(x); |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 2305 | v = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 2306 | u = TOP(); |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 2307 | if (PyInt_AsLong(u) != -1 || PyErr_Occurred()) |
| 2308 | w = PyTuple_Pack(5, |
| 2309 | w, |
| 2310 | f->f_globals, |
| 2311 | f->f_locals == NULL ? |
| 2312 | Py_None : f->f_locals, |
| 2313 | v, |
| 2314 | u); |
| 2315 | else |
| 2316 | w = PyTuple_Pack(4, |
| 2317 | w, |
| 2318 | f->f_globals, |
| 2319 | f->f_locals == NULL ? |
| 2320 | Py_None : f->f_locals, |
| 2321 | v); |
| 2322 | Py_DECREF(v); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2323 | Py_DECREF(u); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2324 | if (w == NULL) { |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 2325 | u = POP(); |
Guido van Rossum | 1d9a9ea | 2008-01-23 20:19:01 +0000 | [diff] [blame] | 2326 | Py_DECREF(x); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2327 | x = NULL; |
| 2328 | break; |
| 2329 | } |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 2330 | READ_TIMESTAMP(intr0); |
Guido van Rossum | 1d9a9ea | 2008-01-23 20:19:01 +0000 | [diff] [blame] | 2331 | v = x; |
| 2332 | x = PyEval_CallObject(v, w); |
| 2333 | Py_DECREF(v); |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 2334 | READ_TIMESTAMP(intr1); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2335 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 2336 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 2337 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2338 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2339 | |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2340 | case IMPORT_STAR: |
| 2341 | v = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2342 | PyFrame_FastToLocals(f); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2343 | if ((x = f->f_locals) == NULL) { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2344 | PyErr_SetString(PyExc_SystemError, |
Jeremy Hylton | c862cf4 | 2001-01-19 03:25:05 +0000 | [diff] [blame] | 2345 | "no locals found during 'import *'"); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2346 | break; |
| 2347 | } |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 2348 | READ_TIMESTAMP(intr0); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2349 | err = import_all_from(x, v); |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 2350 | READ_TIMESTAMP(intr1); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2351 | PyFrame_LocalsToFast(f, 0); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2352 | Py_DECREF(v); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 2353 | if (err == 0) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2354 | break; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2355 | |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2356 | case IMPORT_FROM: |
Skip Montanaro | 496e658 | 2002-08-06 17:47:40 +0000 | [diff] [blame] | 2357 | w = GETITEM(names, oparg); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2358 | v = TOP(); |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 2359 | READ_TIMESTAMP(intr0); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2360 | x = import_from(v, w); |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 2361 | READ_TIMESTAMP(intr1); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2362 | PUSH(x); |
| 2363 | if (x != NULL) continue; |
| 2364 | break; |
| 2365 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2366 | case JUMP_FORWARD: |
| 2367 | JUMPBY(oparg); |
Neil Schemenauer | c4b570f | 2003-06-01 19:21:12 +0000 | [diff] [blame] | 2368 | goto fast_next_opcode; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2369 | |
Jeffrey Yasskin | 68d6852 | 2009-02-28 19:03:21 +0000 | [diff] [blame] | 2370 | PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE); |
| 2371 | case POP_JUMP_IF_FALSE: |
| 2372 | w = POP(); |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 2373 | if (w == Py_True) { |
Jeffrey Yasskin | 68d6852 | 2009-02-28 19:03:21 +0000 | [diff] [blame] | 2374 | Py_DECREF(w); |
Neil Schemenauer | c4b570f | 2003-06-01 19:21:12 +0000 | [diff] [blame] | 2375 | goto fast_next_opcode; |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 2376 | } |
Raymond Hettinger | 21012b8 | 2003-02-26 18:11:50 +0000 | [diff] [blame] | 2377 | if (w == Py_False) { |
Jeffrey Yasskin | 68d6852 | 2009-02-28 19:03:21 +0000 | [diff] [blame] | 2378 | Py_DECREF(w); |
| 2379 | JUMPTO(oparg); |
Neil Schemenauer | c4b570f | 2003-06-01 19:21:12 +0000 | [diff] [blame] | 2380 | goto fast_next_opcode; |
Raymond Hettinger | 21012b8 | 2003-02-26 18:11:50 +0000 | [diff] [blame] | 2381 | } |
| 2382 | err = PyObject_IsTrue(w); |
Jeffrey Yasskin | 68d6852 | 2009-02-28 19:03:21 +0000 | [diff] [blame] | 2383 | Py_DECREF(w); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 2384 | if (err > 0) |
| 2385 | err = 0; |
| 2386 | else if (err == 0) |
Jeffrey Yasskin | 68d6852 | 2009-02-28 19:03:21 +0000 | [diff] [blame] | 2387 | JUMPTO(oparg); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 2388 | else |
| 2389 | break; |
| 2390 | continue; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2391 | |
Jeffrey Yasskin | 68d6852 | 2009-02-28 19:03:21 +0000 | [diff] [blame] | 2392 | PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE); |
| 2393 | case POP_JUMP_IF_TRUE: |
| 2394 | w = POP(); |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 2395 | if (w == Py_False) { |
Jeffrey Yasskin | 68d6852 | 2009-02-28 19:03:21 +0000 | [diff] [blame] | 2396 | Py_DECREF(w); |
Neil Schemenauer | c4b570f | 2003-06-01 19:21:12 +0000 | [diff] [blame] | 2397 | goto fast_next_opcode; |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 2398 | } |
Raymond Hettinger | 21012b8 | 2003-02-26 18:11:50 +0000 | [diff] [blame] | 2399 | if (w == Py_True) { |
Jeffrey Yasskin | 68d6852 | 2009-02-28 19:03:21 +0000 | [diff] [blame] | 2400 | Py_DECREF(w); |
| 2401 | JUMPTO(oparg); |
| 2402 | goto fast_next_opcode; |
| 2403 | } |
| 2404 | err = PyObject_IsTrue(w); |
| 2405 | Py_DECREF(w); |
| 2406 | if (err > 0) { |
| 2407 | err = 0; |
| 2408 | JUMPTO(oparg); |
| 2409 | } |
| 2410 | else if (err == 0) |
| 2411 | ; |
| 2412 | else |
| 2413 | break; |
| 2414 | continue; |
| 2415 | |
| 2416 | case JUMP_IF_FALSE_OR_POP: |
| 2417 | w = TOP(); |
| 2418 | if (w == Py_True) { |
| 2419 | STACKADJ(-1); |
| 2420 | Py_DECREF(w); |
| 2421 | goto fast_next_opcode; |
| 2422 | } |
| 2423 | if (w == Py_False) { |
| 2424 | JUMPTO(oparg); |
| 2425 | goto fast_next_opcode; |
| 2426 | } |
| 2427 | err = PyObject_IsTrue(w); |
| 2428 | if (err > 0) { |
| 2429 | STACKADJ(-1); |
| 2430 | Py_DECREF(w); |
| 2431 | err = 0; |
| 2432 | } |
| 2433 | else if (err == 0) |
| 2434 | JUMPTO(oparg); |
| 2435 | else |
| 2436 | break; |
| 2437 | continue; |
| 2438 | |
| 2439 | case JUMP_IF_TRUE_OR_POP: |
| 2440 | w = TOP(); |
| 2441 | if (w == Py_False) { |
| 2442 | STACKADJ(-1); |
| 2443 | Py_DECREF(w); |
| 2444 | goto fast_next_opcode; |
| 2445 | } |
| 2446 | if (w == Py_True) { |
| 2447 | JUMPTO(oparg); |
Neil Schemenauer | c4b570f | 2003-06-01 19:21:12 +0000 | [diff] [blame] | 2448 | goto fast_next_opcode; |
Raymond Hettinger | 21012b8 | 2003-02-26 18:11:50 +0000 | [diff] [blame] | 2449 | } |
| 2450 | err = PyObject_IsTrue(w); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 2451 | if (err > 0) { |
| 2452 | err = 0; |
Jeffrey Yasskin | 68d6852 | 2009-02-28 19:03:21 +0000 | [diff] [blame] | 2453 | JUMPTO(oparg); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 2454 | } |
Jeffrey Yasskin | 68d6852 | 2009-02-28 19:03:21 +0000 | [diff] [blame] | 2455 | else if (err == 0) { |
| 2456 | STACKADJ(-1); |
| 2457 | Py_DECREF(w); |
| 2458 | } |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 2459 | else |
| 2460 | break; |
| 2461 | continue; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2462 | |
Raymond Hettinger | fba1cfc | 2004-03-12 16:33:17 +0000 | [diff] [blame] | 2463 | PREDICTED_WITH_ARG(JUMP_ABSOLUTE); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2464 | case JUMP_ABSOLUTE: |
| 2465 | JUMPTO(oparg); |
Raymond Hettinger | dc1d1ba | 2007-11-07 02:45:46 +0000 | [diff] [blame] | 2466 | #if FAST_LOOPS |
| 2467 | /* Enabling this path speeds-up all while and for-loops by bypassing |
| 2468 | the per-loop checks for signals. By default, this should be turned-off |
| 2469 | because it prevents detection of a control-break in tight loops like |
| 2470 | "while 1: pass". Compile with this option turned-on when you need |
| 2471 | the speed-up and do not need break checking inside tight loops (ones |
Antoine Pitrou | c2cc80c | 2008-07-25 22:13:52 +0000 | [diff] [blame] | 2472 | that contain only instructions ending with goto fast_next_opcode). |
Raymond Hettinger | dc1d1ba | 2007-11-07 02:45:46 +0000 | [diff] [blame] | 2473 | */ |
| 2474 | goto fast_next_opcode; |
| 2475 | #else |
Neil Schemenauer | ca2a2f1 | 2003-05-30 23:59:44 +0000 | [diff] [blame] | 2476 | continue; |
Raymond Hettinger | dc1d1ba | 2007-11-07 02:45:46 +0000 | [diff] [blame] | 2477 | #endif |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2478 | |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2479 | case GET_ITER: |
| 2480 | /* before: [obj]; after [getiter(obj)] */ |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 2481 | v = TOP(); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2482 | x = PyObject_GetIter(v); |
| 2483 | Py_DECREF(v); |
| 2484 | if (x != NULL) { |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 2485 | SET_TOP(x); |
Raymond Hettinger | 7dc5221 | 2003-03-16 20:14:44 +0000 | [diff] [blame] | 2486 | PREDICT(FOR_ITER); |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2487 | continue; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2488 | } |
Raymond Hettinger | 8bb90a5 | 2003-01-14 12:43:10 +0000 | [diff] [blame] | 2489 | STACKADJ(-1); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2490 | break; |
| 2491 | |
Raymond Hettinger | 7dc5221 | 2003-03-16 20:14:44 +0000 | [diff] [blame] | 2492 | PREDICTED_WITH_ARG(FOR_ITER); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2493 | case FOR_ITER: |
| 2494 | /* before: [iter]; after: [iter, iter()] *or* [] */ |
| 2495 | v = TOP(); |
Raymond Hettinger | db0de9e | 2004-03-12 08:41:36 +0000 | [diff] [blame] | 2496 | x = (*v->ob_type->tp_iternext)(v); |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2497 | if (x != NULL) { |
| 2498 | PUSH(x); |
Raymond Hettinger | 7dc5221 | 2003-03-16 20:14:44 +0000 | [diff] [blame] | 2499 | PREDICT(STORE_FAST); |
| 2500 | PREDICT(UNPACK_SEQUENCE); |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2501 | continue; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2502 | } |
Raymond Hettinger | db0de9e | 2004-03-12 08:41:36 +0000 | [diff] [blame] | 2503 | if (PyErr_Occurred()) { |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 2504 | if (!PyErr_ExceptionMatches( |
| 2505 | PyExc_StopIteration)) |
Raymond Hettinger | db0de9e | 2004-03-12 08:41:36 +0000 | [diff] [blame] | 2506 | break; |
| 2507 | PyErr_Clear(); |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2508 | } |
Raymond Hettinger | db0de9e | 2004-03-12 08:41:36 +0000 | [diff] [blame] | 2509 | /* iterator ended normally */ |
| 2510 | x = v = POP(); |
| 2511 | Py_DECREF(v); |
| 2512 | JUMPBY(oparg); |
| 2513 | continue; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2514 | |
Raymond Hettinger | 2d783e9 | 2004-03-12 09:12:22 +0000 | [diff] [blame] | 2515 | case BREAK_LOOP: |
| 2516 | why = WHY_BREAK; |
| 2517 | goto fast_block_end; |
| 2518 | |
| 2519 | case CONTINUE_LOOP: |
| 2520 | retval = PyInt_FromLong(oparg); |
Neal Norwitz | 02104df | 2006-05-19 06:31:23 +0000 | [diff] [blame] | 2521 | if (!retval) { |
| 2522 | x = NULL; |
| 2523 | break; |
| 2524 | } |
Raymond Hettinger | 2d783e9 | 2004-03-12 09:12:22 +0000 | [diff] [blame] | 2525 | why = WHY_CONTINUE; |
| 2526 | goto fast_block_end; |
| 2527 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2528 | case SETUP_LOOP: |
| 2529 | case SETUP_EXCEPT: |
| 2530 | case SETUP_FINALLY: |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 2531 | /* NOTE: If you add any new block-setup opcodes that |
| 2532 | are not try/except/finally handlers, you may need |
| 2533 | to update the PyGen_NeedsFinalizing() function. |
| 2534 | */ |
Phillip J. Eby | 2ba9661 | 2006-04-10 17:51:05 +0000 | [diff] [blame] | 2535 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2536 | PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg, |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 2537 | STACK_LEVEL()); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 2538 | continue; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2539 | |
Benjamin Peterson | 1880d8b | 2009-05-25 13:13:44 +0000 | [diff] [blame] | 2540 | case SETUP_WITH: |
| 2541 | { |
| 2542 | static PyObject *exit, *enter; |
| 2543 | w = TOP(); |
| 2544 | x = special_lookup(w, "__exit__", &exit); |
| 2545 | if (!x) |
| 2546 | break; |
| 2547 | SET_TOP(x); |
| 2548 | u = special_lookup(w, "__enter__", &enter); |
| 2549 | Py_DECREF(w); |
| 2550 | if (!u) { |
| 2551 | x = NULL; |
| 2552 | break; |
| 2553 | } |
| 2554 | x = PyObject_CallFunctionObjArgs(u, NULL); |
| 2555 | Py_DECREF(u); |
| 2556 | if (!x) |
| 2557 | break; |
Benjamin Peterson | 565d785 | 2010-02-05 02:12:14 +0000 | [diff] [blame] | 2558 | /* Setup a finally block (SETUP_WITH as a block is |
| 2559 | equivalent to SETUP_FINALLY except it normalizes |
| 2560 | the exception) before pushing the result of |
| 2561 | __enter__ on the stack. */ |
| 2562 | PyFrame_BlockSetup(f, SETUP_WITH, INSTR_OFFSET() + oparg, |
Benjamin Peterson | 1880d8b | 2009-05-25 13:13:44 +0000 | [diff] [blame] | 2563 | STACK_LEVEL()); |
| 2564 | |
| 2565 | PUSH(x); |
| 2566 | continue; |
| 2567 | } |
| 2568 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 2569 | case WITH_CLEANUP: |
| 2570 | { |
Nick Coghlan | 7af53be | 2008-03-07 14:13:28 +0000 | [diff] [blame] | 2571 | /* At the top of the stack are 1-3 values indicating |
| 2572 | how/why we entered the finally clause: |
| 2573 | - TOP = None |
| 2574 | - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval |
| 2575 | - TOP = WHY_*; no retval below it |
| 2576 | - (TOP, SECOND, THIRD) = exc_info() |
| 2577 | Below them is EXIT, the context.__exit__ bound method. |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 2578 | In the last case, we must call |
Nick Coghlan | 7af53be | 2008-03-07 14:13:28 +0000 | [diff] [blame] | 2579 | EXIT(TOP, SECOND, THIRD) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 2580 | otherwise we must call |
Nick Coghlan | 7af53be | 2008-03-07 14:13:28 +0000 | [diff] [blame] | 2581 | EXIT(None, None, None) |
| 2582 | |
| 2583 | In all cases, we remove EXIT from the stack, leaving |
| 2584 | the rest in the same order. |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 2585 | |
| 2586 | In addition, if the stack represents an exception, |
Guido van Rossum | f669436 | 2006-03-10 02:28:35 +0000 | [diff] [blame] | 2587 | *and* the function call returns a 'true' value, we |
| 2588 | "zap" this information, to prevent END_FINALLY from |
| 2589 | re-raising the exception. (But non-local gotos |
| 2590 | should still be resumed.) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 2591 | */ |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 2592 | |
Nick Coghlan | 7af53be | 2008-03-07 14:13:28 +0000 | [diff] [blame] | 2593 | PyObject *exit_func; |
| 2594 | |
| 2595 | u = POP(); |
| 2596 | if (u == Py_None) { |
| 2597 | exit_func = TOP(); |
| 2598 | SET_TOP(u); |
| 2599 | v = w = Py_None; |
| 2600 | } |
| 2601 | else if (PyInt_Check(u)) { |
| 2602 | switch(PyInt_AS_LONG(u)) { |
| 2603 | case WHY_RETURN: |
| 2604 | case WHY_CONTINUE: |
| 2605 | /* Retval in TOP. */ |
| 2606 | exit_func = SECOND(); |
| 2607 | SET_SECOND(TOP()); |
| 2608 | SET_TOP(u); |
| 2609 | break; |
| 2610 | default: |
| 2611 | exit_func = TOP(); |
| 2612 | SET_TOP(u); |
| 2613 | break; |
| 2614 | } |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 2615 | u = v = w = Py_None; |
| 2616 | } |
| 2617 | else { |
Nick Coghlan | 7af53be | 2008-03-07 14:13:28 +0000 | [diff] [blame] | 2618 | v = TOP(); |
| 2619 | w = SECOND(); |
| 2620 | exit_func = THIRD(); |
| 2621 | SET_TOP(u); |
| 2622 | SET_SECOND(v); |
| 2623 | SET_THIRD(w); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 2624 | } |
Guido van Rossum | f669436 | 2006-03-10 02:28:35 +0000 | [diff] [blame] | 2625 | /* XXX Not the fastest way to call it... */ |
Nick Coghlan | 7af53be | 2008-03-07 14:13:28 +0000 | [diff] [blame] | 2626 | x = PyObject_CallFunctionObjArgs(exit_func, u, v, w, |
| 2627 | NULL); |
Amaury Forgeot d'Arc | ad9b599 | 2008-12-10 23:22:49 +0000 | [diff] [blame] | 2628 | Py_DECREF(exit_func); |
| 2629 | if (x == NULL) |
Guido van Rossum | f669436 | 2006-03-10 02:28:35 +0000 | [diff] [blame] | 2630 | break; /* Go to error exit */ |
Amaury Forgeot d'Arc | ad9b599 | 2008-12-10 23:22:49 +0000 | [diff] [blame] | 2631 | |
| 2632 | if (u != Py_None) |
| 2633 | err = PyObject_IsTrue(x); |
| 2634 | else |
| 2635 | err = 0; |
| 2636 | Py_DECREF(x); |
| 2637 | |
| 2638 | if (err < 0) |
| 2639 | break; /* Go to error exit */ |
| 2640 | else if (err > 0) { |
| 2641 | err = 0; |
Guido van Rossum | f669436 | 2006-03-10 02:28:35 +0000 | [diff] [blame] | 2642 | /* There was an exception and a true return */ |
Nick Coghlan | 7af53be | 2008-03-07 14:13:28 +0000 | [diff] [blame] | 2643 | STACKADJ(-2); |
Guido van Rossum | f669436 | 2006-03-10 02:28:35 +0000 | [diff] [blame] | 2644 | Py_INCREF(Py_None); |
| 2645 | SET_TOP(Py_None); |
Guido van Rossum | f669436 | 2006-03-10 02:28:35 +0000 | [diff] [blame] | 2646 | Py_DECREF(u); |
| 2647 | Py_DECREF(v); |
| 2648 | Py_DECREF(w); |
| 2649 | } else { |
Nick Coghlan | 7af53be | 2008-03-07 14:13:28 +0000 | [diff] [blame] | 2650 | /* The stack was rearranged to remove EXIT |
| 2651 | above. Let END_FINALLY do its thing */ |
Guido van Rossum | f669436 | 2006-03-10 02:28:35 +0000 | [diff] [blame] | 2652 | } |
Jeffrey Yasskin | 9063a99 | 2008-03-03 01:27:03 +0000 | [diff] [blame] | 2653 | PREDICT(END_FINALLY); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 2654 | break; |
| 2655 | } |
| 2656 | |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 2657 | case CALL_FUNCTION: |
Armin Rigo | 8817fcd | 2004-06-17 10:22:40 +0000 | [diff] [blame] | 2658 | { |
| 2659 | PyObject **sp; |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 2660 | PCALL(PCALL_ALL); |
Armin Rigo | 8817fcd | 2004-06-17 10:22:40 +0000 | [diff] [blame] | 2661 | sp = stack_pointer; |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 2662 | #ifdef WITH_TSC |
Armin Rigo | 8817fcd | 2004-06-17 10:22:40 +0000 | [diff] [blame] | 2663 | x = call_function(&sp, oparg, &intr0, &intr1); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 2664 | #else |
Armin Rigo | 8817fcd | 2004-06-17 10:22:40 +0000 | [diff] [blame] | 2665 | x = call_function(&sp, oparg); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 2666 | #endif |
Armin Rigo | 8817fcd | 2004-06-17 10:22:40 +0000 | [diff] [blame] | 2667 | stack_pointer = sp; |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 2668 | PUSH(x); |
| 2669 | if (x != NULL) |
| 2670 | continue; |
| 2671 | break; |
Armin Rigo | 8817fcd | 2004-06-17 10:22:40 +0000 | [diff] [blame] | 2672 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2673 | |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 2674 | case CALL_FUNCTION_VAR: |
| 2675 | case CALL_FUNCTION_KW: |
| 2676 | case CALL_FUNCTION_VAR_KW: |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 2677 | { |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 2678 | int na = oparg & 0xff; |
| 2679 | int nk = (oparg>>8) & 0xff; |
| 2680 | int flags = (opcode - CALL_FUNCTION) & 3; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 2681 | int n = na + 2 * nk; |
Armin Rigo | 8817fcd | 2004-06-17 10:22:40 +0000 | [diff] [blame] | 2682 | PyObject **pfunc, *func, **sp; |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 2683 | PCALL(PCALL_ALL); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 2684 | if (flags & CALL_FLAG_VAR) |
| 2685 | n++; |
| 2686 | if (flags & CALL_FLAG_KW) |
| 2687 | n++; |
| 2688 | pfunc = stack_pointer - n - 1; |
| 2689 | func = *pfunc; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 2690 | |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2691 | if (PyMethod_Check(func) |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 2692 | && PyMethod_GET_SELF(func) != NULL) { |
| 2693 | PyObject *self = PyMethod_GET_SELF(func); |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 2694 | Py_INCREF(self); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 2695 | func = PyMethod_GET_FUNCTION(func); |
| 2696 | Py_INCREF(func); |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 2697 | Py_DECREF(*pfunc); |
| 2698 | *pfunc = self; |
| 2699 | na++; |
| 2700 | n++; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2701 | } else |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 2702 | Py_INCREF(func); |
Armin Rigo | 8817fcd | 2004-06-17 10:22:40 +0000 | [diff] [blame] | 2703 | sp = stack_pointer; |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 2704 | READ_TIMESTAMP(intr0); |
Armin Rigo | 8817fcd | 2004-06-17 10:22:40 +0000 | [diff] [blame] | 2705 | x = ext_do_call(func, &sp, flags, na, nk); |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 2706 | READ_TIMESTAMP(intr1); |
Armin Rigo | 8817fcd | 2004-06-17 10:22:40 +0000 | [diff] [blame] | 2707 | stack_pointer = sp; |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 2708 | Py_DECREF(func); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 2709 | |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 2710 | while (stack_pointer > pfunc) { |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 2711 | w = POP(); |
| 2712 | Py_DECREF(w); |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 2713 | } |
| 2714 | PUSH(x); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2715 | if (x != NULL) |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 2716 | continue; |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 2717 | break; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 2718 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2719 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2720 | case MAKE_FUNCTION: |
| 2721 | v = POP(); /* code object */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2722 | x = PyFunction_New(v, f->f_globals); |
| 2723 | Py_DECREF(v); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2724 | /* XXX Maybe this should be a separate opcode? */ |
| 2725 | if (x != NULL && oparg > 0) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2726 | v = PyTuple_New(oparg); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2727 | if (v == NULL) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2728 | Py_DECREF(x); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2729 | x = NULL; |
| 2730 | break; |
| 2731 | } |
Raymond Hettinger | 5bed456 | 2004-04-10 23:34:17 +0000 | [diff] [blame] | 2732 | while (--oparg >= 0) { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2733 | w = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2734 | PyTuple_SET_ITEM(v, oparg, w); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2735 | } |
| 2736 | err = PyFunction_SetDefaults(x, v); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2737 | Py_DECREF(v); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2738 | } |
| 2739 | PUSH(x); |
| 2740 | break; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2741 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2742 | case MAKE_CLOSURE: |
| 2743 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2744 | v = POP(); /* code object */ |
| 2745 | x = PyFunction_New(v, f->f_globals); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2746 | Py_DECREF(v); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2747 | if (x != NULL) { |
| 2748 | v = POP(); |
Jeffrey Yasskin | 2d873bd | 2008-12-08 18:55:24 +0000 | [diff] [blame] | 2749 | if (PyFunction_SetClosure(x, v) != 0) { |
| 2750 | /* Can't happen unless bytecode is corrupt. */ |
| 2751 | why = WHY_EXCEPTION; |
| 2752 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2753 | Py_DECREF(v); |
| 2754 | } |
| 2755 | if (x != NULL && oparg > 0) { |
| 2756 | v = PyTuple_New(oparg); |
| 2757 | if (v == NULL) { |
| 2758 | Py_DECREF(x); |
| 2759 | x = NULL; |
| 2760 | break; |
| 2761 | } |
Raymond Hettinger | 5bed456 | 2004-04-10 23:34:17 +0000 | [diff] [blame] | 2762 | while (--oparg >= 0) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2763 | w = POP(); |
| 2764 | PyTuple_SET_ITEM(v, oparg, w); |
| 2765 | } |
Jeffrey Yasskin | 2d873bd | 2008-12-08 18:55:24 +0000 | [diff] [blame] | 2766 | if (PyFunction_SetDefaults(x, v) != 0) { |
| 2767 | /* Can't happen unless |
| 2768 | PyFunction_SetDefaults changes. */ |
| 2769 | why = WHY_EXCEPTION; |
| 2770 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2771 | Py_DECREF(v); |
| 2772 | } |
| 2773 | PUSH(x); |
| 2774 | break; |
| 2775 | } |
| 2776 | |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2777 | case BUILD_SLICE: |
| 2778 | if (oparg == 3) |
| 2779 | w = POP(); |
| 2780 | else |
| 2781 | w = NULL; |
| 2782 | v = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 2783 | u = TOP(); |
Guido van Rossum | 1aa1483 | 1997-01-21 05:34:20 +0000 | [diff] [blame] | 2784 | x = PySlice_New(u, v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2785 | Py_DECREF(u); |
| 2786 | Py_DECREF(v); |
| 2787 | Py_XDECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 2788 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 2789 | if (x != NULL) continue; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2790 | break; |
| 2791 | |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 2792 | case EXTENDED_ARG: |
| 2793 | opcode = NEXTOP(); |
Raymond Hettinger | 5bed456 | 2004-04-10 23:34:17 +0000 | [diff] [blame] | 2794 | oparg = oparg<<16 | NEXTARG(); |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 2795 | goto dispatch_opcode; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2796 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2797 | default: |
| 2798 | fprintf(stderr, |
| 2799 | "XXX lineno: %d, opcode: %d\n", |
Jeffrey Yasskin | f7f858d | 2009-05-08 22:23:21 +0000 | [diff] [blame] | 2800 | PyFrame_GetLineNumber(f), |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 2801 | opcode); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2802 | PyErr_SetString(PyExc_SystemError, "unknown opcode"); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2803 | why = WHY_EXCEPTION; |
| 2804 | break; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 2805 | |
| 2806 | #ifdef CASE_TOO_BIG |
| 2807 | } |
| 2808 | #endif |
| 2809 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2810 | } /* switch */ |
| 2811 | |
| 2812 | on_error: |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2813 | |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 2814 | READ_TIMESTAMP(inst1); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 2815 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2816 | /* Quickly continue if no error occurred */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2817 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2818 | if (why == WHY_NOT) { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2819 | if (err == 0 && x != NULL) { |
| 2820 | #ifdef CHECKEXC |
Guido van Rossum | eb894eb | 1999-03-09 16:16:45 +0000 | [diff] [blame] | 2821 | /* This check is expensive! */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2822 | if (PyErr_Occurred()) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2823 | fprintf(stderr, |
| 2824 | "XXX undetected error\n"); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 2825 | else { |
| 2826 | #endif |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 2827 | READ_TIMESTAMP(loop1); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2828 | continue; /* Normal, fast path */ |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 2829 | #ifdef CHECKEXC |
| 2830 | } |
| 2831 | #endif |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2832 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2833 | why = WHY_EXCEPTION; |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2834 | x = Py_None; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2835 | err = 0; |
| 2836 | } |
| 2837 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2838 | /* Double-check exception status */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2839 | |
Raymond Hettinger | c8aa08b | 2004-04-11 14:59:33 +0000 | [diff] [blame] | 2840 | if (why == WHY_EXCEPTION || why == WHY_RERAISE) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2841 | if (!PyErr_Occurred()) { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2842 | PyErr_SetString(PyExc_SystemError, |
Guido van Rossum | eb894eb | 1999-03-09 16:16:45 +0000 | [diff] [blame] | 2843 | "error return without exception set"); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2844 | why = WHY_EXCEPTION; |
| 2845 | } |
| 2846 | } |
Guido van Rossum | eb894eb | 1999-03-09 16:16:45 +0000 | [diff] [blame] | 2847 | #ifdef CHECKEXC |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2848 | else { |
Guido van Rossum | eb894eb | 1999-03-09 16:16:45 +0000 | [diff] [blame] | 2849 | /* This check is expensive! */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2850 | if (PyErr_Occurred()) { |
Neal Norwitz | 8250fbe | 2008-01-27 17:12:15 +0000 | [diff] [blame] | 2851 | char buf[128]; |
Jeremy Hylton | 904ed86 | 2003-11-05 17:29:35 +0000 | [diff] [blame] | 2852 | sprintf(buf, "Stack unwind with exception " |
| 2853 | "set and why=%d", why); |
| 2854 | Py_FatalError(buf); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2855 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2856 | } |
| 2857 | #endif |
| 2858 | |
| 2859 | /* Log traceback info if this is a real exception */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2860 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2861 | if (why == WHY_EXCEPTION) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2862 | PyTraceBack_Here(f); |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 2863 | |
Fred Drake | 8f51f54 | 2001-10-04 14:48:42 +0000 | [diff] [blame] | 2864 | if (tstate->c_tracefunc != NULL) |
| 2865 | call_exc_trace(tstate->c_tracefunc, |
| 2866 | tstate->c_traceobj, f); |
Guido van Rossum | 014518f | 1998-11-23 21:09:51 +0000 | [diff] [blame] | 2867 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2868 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2869 | /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2870 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2871 | if (why == WHY_RERAISE) |
| 2872 | why = WHY_EXCEPTION; |
| 2873 | |
| 2874 | /* Unwind stacks if a (pseudo) exception occurred */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2875 | |
Raymond Hettinger | 1dd8309 | 2004-02-06 18:32:33 +0000 | [diff] [blame] | 2876 | fast_block_end: |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 2877 | while (why != WHY_NOT && f->f_iblock > 0) { |
Benjamin Peterson | 4a3cf19 | 2009-07-01 23:45:19 +0000 | [diff] [blame] | 2878 | /* Peek at the current block. */ |
| 2879 | PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1]; |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 2880 | |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 2881 | assert(why != WHY_YIELD); |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 2882 | if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) { |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 2883 | why = WHY_NOT; |
| 2884 | JUMPTO(PyInt_AS_LONG(retval)); |
| 2885 | Py_DECREF(retval); |
| 2886 | break; |
| 2887 | } |
| 2888 | |
Benjamin Peterson | 4a3cf19 | 2009-07-01 23:45:19 +0000 | [diff] [blame] | 2889 | /* Now we have to pop the block. */ |
| 2890 | f->f_iblock--; |
| 2891 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2892 | while (STACK_LEVEL() > b->b_level) { |
| 2893 | v = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2894 | Py_XDECREF(v); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2895 | } |
| 2896 | if (b->b_type == SETUP_LOOP && why == WHY_BREAK) { |
| 2897 | why = WHY_NOT; |
| 2898 | JUMPTO(b->b_handler); |
| 2899 | break; |
| 2900 | } |
| 2901 | if (b->b_type == SETUP_FINALLY || |
Guido van Rossum | 150b2df | 1996-12-05 23:17:11 +0000 | [diff] [blame] | 2902 | (b->b_type == SETUP_EXCEPT && |
Benjamin Peterson | 565d785 | 2010-02-05 02:12:14 +0000 | [diff] [blame] | 2903 | why == WHY_EXCEPTION) || |
| 2904 | b->b_type == SETUP_WITH) { |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2905 | if (why == WHY_EXCEPTION) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2906 | PyObject *exc, *val, *tb; |
| 2907 | PyErr_Fetch(&exc, &val, &tb); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2908 | if (val == NULL) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2909 | val = Py_None; |
| 2910 | Py_INCREF(val); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2911 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2912 | /* Make the raw exception data |
| 2913 | available to the handler, |
| 2914 | so a program can emulate the |
| 2915 | Python main loop. Don't do |
| 2916 | this for 'finally'. */ |
Benjamin Peterson | 565d785 | 2010-02-05 02:12:14 +0000 | [diff] [blame] | 2917 | if (b->b_type == SETUP_EXCEPT || |
| 2918 | b->b_type == SETUP_WITH) { |
Barry Warsaw | eaedc7c | 1997-08-28 22:36:40 +0000 | [diff] [blame] | 2919 | PyErr_NormalizeException( |
| 2920 | &exc, &val, &tb); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2921 | set_exc_info(tstate, |
| 2922 | exc, val, tb); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2923 | } |
Jeremy Hylton | c631489 | 2001-09-26 19:24:45 +0000 | [diff] [blame] | 2924 | if (tb == NULL) { |
| 2925 | Py_INCREF(Py_None); |
| 2926 | PUSH(Py_None); |
| 2927 | } else |
| 2928 | PUSH(tb); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2929 | PUSH(val); |
| 2930 | PUSH(exc); |
| 2931 | } |
| 2932 | else { |
Raymond Hettinger | 06032cb | 2004-04-06 09:37:35 +0000 | [diff] [blame] | 2933 | if (why & (WHY_RETURN | WHY_CONTINUE)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2934 | PUSH(retval); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2935 | v = PyInt_FromLong((long)why); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2936 | PUSH(v); |
| 2937 | } |
| 2938 | why = WHY_NOT; |
| 2939 | JUMPTO(b->b_handler); |
| 2940 | break; |
| 2941 | } |
| 2942 | } /* unwind stack */ |
| 2943 | |
| 2944 | /* End the loop if we still have an error (or return) */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2945 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2946 | if (why != WHY_NOT) |
| 2947 | break; |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 2948 | READ_TIMESTAMP(loop1); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2949 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2950 | } /* main loop */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2951 | |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 2952 | assert(why != WHY_YIELD); |
| 2953 | /* Pop remaining stack entries. */ |
| 2954 | while (!EMPTY()) { |
| 2955 | v = POP(); |
| 2956 | Py_XDECREF(v); |
Guido van Rossum | 35974fb | 2001-12-06 21:28:18 +0000 | [diff] [blame] | 2957 | } |
| 2958 | |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 2959 | if (why != WHY_RETURN) |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 2960 | retval = NULL; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2961 | |
Raymond Hettinger | 1dd8309 | 2004-02-06 18:32:33 +0000 | [diff] [blame] | 2962 | fast_yield: |
Fred Drake | 9e3ad78 | 2001-07-03 23:39:52 +0000 | [diff] [blame] | 2963 | if (tstate->use_tracing) { |
Barry Warsaw | e2eca0b | 2005-08-15 18:14:19 +0000 | [diff] [blame] | 2964 | if (tstate->c_tracefunc) { |
| 2965 | if (why == WHY_RETURN || why == WHY_YIELD) { |
| 2966 | if (call_trace(tstate->c_tracefunc, |
| 2967 | tstate->c_traceobj, f, |
| 2968 | PyTrace_RETURN, retval)) { |
| 2969 | Py_XDECREF(retval); |
| 2970 | retval = NULL; |
| 2971 | why = WHY_EXCEPTION; |
| 2972 | } |
| 2973 | } |
| 2974 | else if (why == WHY_EXCEPTION) { |
| 2975 | call_trace_protected(tstate->c_tracefunc, |
| 2976 | tstate->c_traceobj, f, |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 2977 | PyTrace_RETURN, NULL); |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 2978 | } |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 2979 | } |
Fred Drake | 8f51f54 | 2001-10-04 14:48:42 +0000 | [diff] [blame] | 2980 | if (tstate->c_profilefunc) { |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 2981 | if (why == WHY_EXCEPTION) |
| 2982 | call_trace_protected(tstate->c_profilefunc, |
| 2983 | tstate->c_profileobj, f, |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 2984 | PyTrace_RETURN, NULL); |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 2985 | else if (call_trace(tstate->c_profilefunc, |
| 2986 | tstate->c_profileobj, f, |
| 2987 | PyTrace_RETURN, retval)) { |
Fred Drake | 9e3ad78 | 2001-07-03 23:39:52 +0000 | [diff] [blame] | 2988 | Py_XDECREF(retval); |
| 2989 | retval = NULL; |
| 2990 | why = WHY_EXCEPTION; |
| 2991 | } |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 2992 | } |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 2993 | } |
Guido van Rossum | a424013 | 1997-01-21 21:18:36 +0000 | [diff] [blame] | 2994 | |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 2995 | if (tstate->frame->f_exc_type != NULL) |
| 2996 | reset_exc_info(tstate); |
| 2997 | else { |
| 2998 | assert(tstate->frame->f_exc_value == NULL); |
| 2999 | assert(tstate->frame->f_exc_traceback == NULL); |
| 3000 | } |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3001 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3002 | /* pop frame */ |
Thomas Wouters | ae406c6 | 2007-09-19 17:27:43 +0000 | [diff] [blame] | 3003 | exit_eval_frame: |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 3004 | Py_LeaveRecursiveCall(); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3005 | tstate->frame = f->f_back; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3006 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 3007 | return retval; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3008 | } |
| 3009 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3010 | /* 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] | 3011 | PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3012 | the test in the if statements in Misc/gdbinit (pystack and pystackv). */ |
Skip Montanaro | 786ea6b | 2004-03-01 15:44:05 +0000 | [diff] [blame] | 3013 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3014 | PyObject * |
| 3015 | PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals, |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3016 | PyObject **args, int argcount, PyObject **kws, int kwcount, |
| 3017 | PyObject **defs, int defcount, PyObject *closure) |
| 3018 | { |
| 3019 | register PyFrameObject *f; |
| 3020 | register PyObject *retval = NULL; |
| 3021 | register PyObject **fastlocals, **freevars; |
| 3022 | PyThreadState *tstate = PyThreadState_GET(); |
| 3023 | PyObject *x, *u; |
| 3024 | |
| 3025 | if (globals == NULL) { |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 3026 | PyErr_SetString(PyExc_SystemError, |
Jeremy Hylton | 910d7d4 | 2001-08-12 21:52:24 +0000 | [diff] [blame] | 3027 | "PyEval_EvalCodeEx: NULL globals"); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3028 | return NULL; |
| 3029 | } |
| 3030 | |
Neal Norwitz | df6a649 | 2006-08-13 18:10:10 +0000 | [diff] [blame] | 3031 | assert(tstate != NULL); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 3032 | assert(globals != NULL); |
| 3033 | f = PyFrame_New(tstate, co, globals, locals); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3034 | if (f == NULL) |
| 3035 | return NULL; |
| 3036 | |
| 3037 | fastlocals = f->f_localsplus; |
Richard Jones | cebbefc | 2006-05-23 18:28:17 +0000 | [diff] [blame] | 3038 | freevars = f->f_localsplus + co->co_nlocals; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3039 | |
| 3040 | if (co->co_argcount > 0 || |
| 3041 | co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) { |
| 3042 | int i; |
| 3043 | int n = argcount; |
| 3044 | PyObject *kwdict = NULL; |
| 3045 | if (co->co_flags & CO_VARKEYWORDS) { |
| 3046 | kwdict = PyDict_New(); |
| 3047 | if (kwdict == NULL) |
| 3048 | goto fail; |
| 3049 | i = co->co_argcount; |
| 3050 | if (co->co_flags & CO_VARARGS) |
| 3051 | i++; |
| 3052 | SETLOCAL(i, kwdict); |
| 3053 | } |
| 3054 | if (argcount > co->co_argcount) { |
| 3055 | if (!(co->co_flags & CO_VARARGS)) { |
| 3056 | PyErr_Format(PyExc_TypeError, |
| 3057 | "%.200s() takes %s %d " |
Benjamin Peterson | 9654589 | 2010-03-21 20:21:00 +0000 | [diff] [blame] | 3058 | "argument%s (%d given)", |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3059 | PyString_AsString(co->co_name), |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3060 | defcount ? "at most" : "exactly", |
| 3061 | co->co_argcount, |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3062 | co->co_argcount == 1 ? "" : "s", |
Benjamin Peterson | bb9d726 | 2010-03-21 20:30:30 +0000 | [diff] [blame] | 3063 | argcount + kwcount); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3064 | goto fail; |
| 3065 | } |
| 3066 | n = co->co_argcount; |
| 3067 | } |
| 3068 | for (i = 0; i < n; i++) { |
| 3069 | x = args[i]; |
| 3070 | Py_INCREF(x); |
| 3071 | SETLOCAL(i, x); |
| 3072 | } |
| 3073 | if (co->co_flags & CO_VARARGS) { |
| 3074 | u = PyTuple_New(argcount - n); |
| 3075 | if (u == NULL) |
| 3076 | goto fail; |
| 3077 | SETLOCAL(co->co_argcount, u); |
| 3078 | for (i = n; i < argcount; i++) { |
| 3079 | x = args[i]; |
| 3080 | Py_INCREF(x); |
| 3081 | PyTuple_SET_ITEM(u, i-n, x); |
| 3082 | } |
| 3083 | } |
| 3084 | for (i = 0; i < kwcount; i++) { |
Antoine Pitrou | c2cc80c | 2008-07-25 22:13:52 +0000 | [diff] [blame] | 3085 | PyObject **co_varnames; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3086 | PyObject *keyword = kws[2*i]; |
| 3087 | PyObject *value = kws[2*i + 1]; |
| 3088 | int j; |
Benjamin Peterson | 78821dd | 2009-01-25 17:15:10 +0000 | [diff] [blame] | 3089 | if (keyword == NULL || !(PyString_Check(keyword) |
| 3090 | #ifdef Py_USING_UNICODE |
| 3091 | || PyUnicode_Check(keyword) |
| 3092 | #endif |
| 3093 | )) { |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3094 | PyErr_Format(PyExc_TypeError, |
| 3095 | "%.200s() keywords must be strings", |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3096 | PyString_AsString(co->co_name)); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3097 | goto fail; |
| 3098 | } |
Antoine Pitrou | c2cc80c | 2008-07-25 22:13:52 +0000 | [diff] [blame] | 3099 | /* Speed hack: do raw pointer compares. As names are |
| 3100 | normally interned this should almost always hit. */ |
Benjamin Peterson | 88e7eba | 2010-03-21 19:39:52 +0000 | [diff] [blame] | 3101 | co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3102 | for (j = 0; j < co->co_argcount; j++) { |
Antoine Pitrou | c2cc80c | 2008-07-25 22:13:52 +0000 | [diff] [blame] | 3103 | PyObject *nm = co_varnames[j]; |
| 3104 | if (nm == keyword) |
| 3105 | goto kw_found; |
| 3106 | } |
| 3107 | /* Slow fallback, just in case */ |
| 3108 | for (j = 0; j < co->co_argcount; j++) { |
| 3109 | PyObject *nm = co_varnames[j]; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3110 | int cmp = PyObject_RichCompareBool( |
| 3111 | keyword, nm, Py_EQ); |
| 3112 | if (cmp > 0) |
Antoine Pitrou | c2cc80c | 2008-07-25 22:13:52 +0000 | [diff] [blame] | 3113 | goto kw_found; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3114 | else if (cmp < 0) |
| 3115 | goto fail; |
| 3116 | } |
Benjamin Peterson | d51b259 | 2010-03-21 19:35:39 +0000 | [diff] [blame] | 3117 | if (kwdict == NULL) { |
| 3118 | PyObject *kwd_str = kwd_as_string(keyword); |
| 3119 | if (kwd_str) { |
| 3120 | PyErr_Format(PyExc_TypeError, |
| 3121 | "%.200s() got an unexpected " |
| 3122 | "keyword argument '%.400s'", |
| 3123 | PyString_AsString(co->co_name), |
| 3124 | PyString_AsString(kwd_str)); |
| 3125 | Py_DECREF(kwd_str); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3126 | } |
Benjamin Peterson | d51b259 | 2010-03-21 19:35:39 +0000 | [diff] [blame] | 3127 | goto fail; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3128 | } |
Benjamin Peterson | d51b259 | 2010-03-21 19:35:39 +0000 | [diff] [blame] | 3129 | PyDict_SetItem(kwdict, keyword, value); |
| 3130 | continue; |
| 3131 | kw_found: |
Antoine Pitrou | c2cc80c | 2008-07-25 22:13:52 +0000 | [diff] [blame] | 3132 | if (GETLOCAL(j) != NULL) { |
Benjamin Peterson | e18ef19 | 2009-01-20 14:21:16 +0000 | [diff] [blame] | 3133 | PyObject *kwd_str = kwd_as_string(keyword); |
| 3134 | if (kwd_str) { |
| 3135 | PyErr_Format(PyExc_TypeError, |
| 3136 | "%.200s() got multiple " |
| 3137 | "values for keyword " |
| 3138 | "argument '%.400s'", |
| 3139 | PyString_AsString(co->co_name), |
| 3140 | PyString_AsString(kwd_str)); |
| 3141 | Py_DECREF(kwd_str); |
| 3142 | } |
Antoine Pitrou | c2cc80c | 2008-07-25 22:13:52 +0000 | [diff] [blame] | 3143 | goto fail; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3144 | } |
Antoine Pitrou | c2cc80c | 2008-07-25 22:13:52 +0000 | [diff] [blame] | 3145 | Py_INCREF(value); |
| 3146 | SETLOCAL(j, value); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3147 | } |
| 3148 | if (argcount < co->co_argcount) { |
| 3149 | int m = co->co_argcount - defcount; |
| 3150 | for (i = argcount; i < m; i++) { |
| 3151 | if (GETLOCAL(i) == NULL) { |
Benjamin Peterson | 9654589 | 2010-03-21 20:21:00 +0000 | [diff] [blame] | 3152 | int j, given = 0; |
| 3153 | for (j = 0; j < co->co_argcount; j++) |
| 3154 | if (GETLOCAL(j)) |
| 3155 | given++; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3156 | PyErr_Format(PyExc_TypeError, |
| 3157 | "%.200s() takes %s %d " |
Benjamin Peterson | 9654589 | 2010-03-21 20:21:00 +0000 | [diff] [blame] | 3158 | "argument%s (%d given)", |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3159 | PyString_AsString(co->co_name), |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3160 | ((co->co_flags & CO_VARARGS) || |
| 3161 | defcount) ? "at least" |
| 3162 | : "exactly", |
Benjamin Peterson | 9654589 | 2010-03-21 20:21:00 +0000 | [diff] [blame] | 3163 | m, m == 1 ? "" : "s", given); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3164 | goto fail; |
| 3165 | } |
| 3166 | } |
| 3167 | if (n > m) |
| 3168 | i = n - m; |
| 3169 | else |
| 3170 | i = 0; |
| 3171 | for (; i < defcount; i++) { |
| 3172 | if (GETLOCAL(m+i) == NULL) { |
| 3173 | PyObject *def = defs[i]; |
| 3174 | Py_INCREF(def); |
| 3175 | SETLOCAL(m+i, def); |
| 3176 | } |
| 3177 | } |
| 3178 | } |
| 3179 | } |
Benjamin Peterson | 56472c2 | 2010-03-21 19:24:08 +0000 | [diff] [blame] | 3180 | else if (argcount > 0 || kwcount > 0) { |
| 3181 | PyErr_Format(PyExc_TypeError, |
| 3182 | "%.200s() takes no arguments (%d given)", |
| 3183 | PyString_AsString(co->co_name), |
| 3184 | argcount + kwcount); |
| 3185 | goto fail; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3186 | } |
| 3187 | /* Allocate and initialize storage for cell vars, and copy free |
| 3188 | vars into frame. This isn't too efficient right now. */ |
Richard Jones | cebbefc | 2006-05-23 18:28:17 +0000 | [diff] [blame] | 3189 | if (PyTuple_GET_SIZE(co->co_cellvars)) { |
Neal Norwitz | 245ce8d | 2006-06-12 02:16:10 +0000 | [diff] [blame] | 3190 | int i, j, nargs, found; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3191 | char *cellname, *argname; |
| 3192 | PyObject *c; |
| 3193 | |
| 3194 | nargs = co->co_argcount; |
| 3195 | if (co->co_flags & CO_VARARGS) |
| 3196 | nargs++; |
| 3197 | if (co->co_flags & CO_VARKEYWORDS) |
| 3198 | nargs++; |
| 3199 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3200 | /* Initialize each cell var, taking into account |
| 3201 | cell vars that are initialized from arguments. |
| 3202 | |
| 3203 | Should arrange for the compiler to put cellvars |
| 3204 | that are arguments at the beginning of the cellvars |
| 3205 | list so that we can march over it more efficiently? |
| 3206 | */ |
Richard Jones | cebbefc | 2006-05-23 18:28:17 +0000 | [diff] [blame] | 3207 | for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3208 | cellname = PyString_AS_STRING( |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3209 | PyTuple_GET_ITEM(co->co_cellvars, i)); |
| 3210 | found = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3211 | for (j = 0; j < nargs; j++) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3212 | argname = PyString_AS_STRING( |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3213 | PyTuple_GET_ITEM(co->co_varnames, j)); |
| 3214 | if (strcmp(cellname, argname) == 0) { |
| 3215 | c = PyCell_New(GETLOCAL(j)); |
| 3216 | if (c == NULL) |
| 3217 | goto fail; |
Richard Jones | cebbefc | 2006-05-23 18:28:17 +0000 | [diff] [blame] | 3218 | GETLOCAL(co->co_nlocals + i) = c; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3219 | found = 1; |
| 3220 | break; |
| 3221 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3222 | } |
| 3223 | if (found == 0) { |
| 3224 | c = PyCell_New(NULL); |
| 3225 | if (c == NULL) |
| 3226 | goto fail; |
Richard Jones | cebbefc | 2006-05-23 18:28:17 +0000 | [diff] [blame] | 3227 | SETLOCAL(co->co_nlocals + i, c); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3228 | } |
| 3229 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3230 | } |
Richard Jones | cebbefc | 2006-05-23 18:28:17 +0000 | [diff] [blame] | 3231 | if (PyTuple_GET_SIZE(co->co_freevars)) { |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3232 | int i; |
Richard Jones | cebbefc | 2006-05-23 18:28:17 +0000 | [diff] [blame] | 3233 | for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) { |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3234 | PyObject *o = PyTuple_GET_ITEM(closure, i); |
| 3235 | Py_INCREF(o); |
Richard Jones | cebbefc | 2006-05-23 18:28:17 +0000 | [diff] [blame] | 3236 | freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3237 | } |
| 3238 | } |
| 3239 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3240 | if (co->co_flags & CO_GENERATOR) { |
Neil Schemenauer | 2b13ce8 | 2001-06-21 02:41:10 +0000 | [diff] [blame] | 3241 | /* Don't need to keep the reference to f_back, it will be set |
| 3242 | * when the generator is resumed. */ |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 3243 | Py_XDECREF(f->f_back); |
Neil Schemenauer | 2b13ce8 | 2001-06-21 02:41:10 +0000 | [diff] [blame] | 3244 | f->f_back = NULL; |
| 3245 | |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 3246 | PCALL(PCALL_GENERATOR); |
| 3247 | |
Neil Schemenauer | 2b13ce8 | 2001-06-21 02:41:10 +0000 | [diff] [blame] | 3248 | /* Create a new generator that owns the ready to run frame |
| 3249 | * and return that as the value. */ |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 3250 | return PyGen_New(f); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3251 | } |
| 3252 | |
Thomas Wouters | ae406c6 | 2007-09-19 17:27:43 +0000 | [diff] [blame] | 3253 | retval = PyEval_EvalFrameEx(f,0); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3254 | |
Thomas Wouters | ae406c6 | 2007-09-19 17:27:43 +0000 | [diff] [blame] | 3255 | fail: /* Jump here from prelude on failure */ |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3256 | |
Tim Peters | b13680b | 2001-11-27 23:29:29 +0000 | [diff] [blame] | 3257 | /* decref'ing the frame can cause __del__ methods to get invoked, |
| 3258 | which can call back into Python. While we're done with the |
| 3259 | current Python frame (f), the associated C stack is still in use, |
| 3260 | so recursion_depth must be boosted for the duration. |
| 3261 | */ |
| 3262 | assert(tstate != NULL); |
| 3263 | ++tstate->recursion_depth; |
Thomas Wouters | ae406c6 | 2007-09-19 17:27:43 +0000 | [diff] [blame] | 3264 | Py_DECREF(f); |
Tim Peters | b13680b | 2001-11-27 23:29:29 +0000 | [diff] [blame] | 3265 | --tstate->recursion_depth; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3266 | return retval; |
| 3267 | } |
| 3268 | |
| 3269 | |
Benjamin Peterson | 1880d8b | 2009-05-25 13:13:44 +0000 | [diff] [blame] | 3270 | static PyObject * |
| 3271 | special_lookup(PyObject *o, char *meth, PyObject **cache) |
| 3272 | { |
| 3273 | PyObject *res; |
| 3274 | if (PyInstance_Check(o)) { |
| 3275 | if (!*cache) |
| 3276 | return PyObject_GetAttrString(o, meth); |
| 3277 | else |
| 3278 | return PyObject_GetAttr(o, *cache); |
| 3279 | } |
| 3280 | res = _PyObject_LookupSpecial(o, meth, cache); |
| 3281 | if (res == NULL && !PyErr_Occurred()) { |
| 3282 | PyErr_SetObject(PyExc_AttributeError, *cache); |
| 3283 | return NULL; |
| 3284 | } |
| 3285 | return res; |
| 3286 | } |
| 3287 | |
Benjamin Peterson | 78821dd | 2009-01-25 17:15:10 +0000 | [diff] [blame] | 3288 | |
Benjamin Peterson | e18ef19 | 2009-01-20 14:21:16 +0000 | [diff] [blame] | 3289 | static PyObject * |
| 3290 | kwd_as_string(PyObject *kwd) { |
Benjamin Peterson | 78821dd | 2009-01-25 17:15:10 +0000 | [diff] [blame] | 3291 | #ifdef Py_USING_UNICODE |
Benjamin Peterson | e18ef19 | 2009-01-20 14:21:16 +0000 | [diff] [blame] | 3292 | if (PyString_Check(kwd)) { |
Benjamin Peterson | 78821dd | 2009-01-25 17:15:10 +0000 | [diff] [blame] | 3293 | #else |
| 3294 | assert(PyString_Check(kwd)); |
| 3295 | #endif |
Benjamin Peterson | e18ef19 | 2009-01-20 14:21:16 +0000 | [diff] [blame] | 3296 | Py_INCREF(kwd); |
| 3297 | return kwd; |
Benjamin Peterson | 78821dd | 2009-01-25 17:15:10 +0000 | [diff] [blame] | 3298 | #ifdef Py_USING_UNICODE |
Benjamin Peterson | e18ef19 | 2009-01-20 14:21:16 +0000 | [diff] [blame] | 3299 | } |
Benjamin Peterson | 78821dd | 2009-01-25 17:15:10 +0000 | [diff] [blame] | 3300 | return _PyUnicode_AsDefaultEncodedString(kwd, "replace"); |
| 3301 | #endif |
Benjamin Peterson | e18ef19 | 2009-01-20 14:21:16 +0000 | [diff] [blame] | 3302 | } |
| 3303 | |
| 3304 | |
Guido van Rossum | c9fbb72 | 2003-03-01 03:36:33 +0000 | [diff] [blame] | 3305 | /* Implementation notes for set_exc_info() and reset_exc_info(): |
| 3306 | |
| 3307 | - Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and |
| 3308 | 'exc_traceback'. These always travel together. |
| 3309 | |
| 3310 | - tstate->curexc_ZZZ is the "hot" exception that is set by |
| 3311 | PyErr_SetString(), cleared by PyErr_Clear(), and so on. |
| 3312 | |
| 3313 | - Once an exception is caught by an except clause, it is transferred |
| 3314 | from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info() |
| 3315 | can pick it up. This is the primary task of set_exc_info(). |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 3316 | 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] | 3317 | |
| 3318 | - Now let me explain the complicated dance with frame->f_exc_ZZZ. |
| 3319 | |
| 3320 | Long ago, when none of this existed, there were just a few globals: |
| 3321 | one set corresponding to the "hot" exception, and one set |
| 3322 | corresponding to sys.exc_ZZZ. (Actually, the latter weren't C |
| 3323 | globals; they were simply stored as sys.exc_ZZZ. For backwards |
| 3324 | compatibility, they still are!) The problem was that in code like |
| 3325 | this: |
| 3326 | |
| 3327 | try: |
| 3328 | "something that may fail" |
| 3329 | except "some exception": |
| 3330 | "do something else first" |
| 3331 | "print the exception from sys.exc_ZZZ." |
| 3332 | |
| 3333 | if "do something else first" invoked something that raised and caught |
| 3334 | an exception, sys.exc_ZZZ were overwritten. That was a frequent |
| 3335 | cause of subtle bugs. I fixed this by changing the semantics as |
| 3336 | follows: |
| 3337 | |
| 3338 | - Within one frame, sys.exc_ZZZ will hold the last exception caught |
| 3339 | *in that frame*. |
| 3340 | |
| 3341 | - But initially, and as long as no exception is caught in a given |
| 3342 | frame, sys.exc_ZZZ will hold the last exception caught in the |
| 3343 | previous frame (or the frame before that, etc.). |
| 3344 | |
| 3345 | The first bullet fixed the bug in the above example. The second |
| 3346 | bullet was for backwards compatibility: it was (and is) common to |
| 3347 | have a function that is called when an exception is caught, and to |
| 3348 | have that function access the caught exception via sys.exc_ZZZ. |
| 3349 | (Example: traceback.print_exc()). |
| 3350 | |
| 3351 | At the same time I fixed the problem that sys.exc_ZZZ weren't |
| 3352 | thread-safe, by introducing sys.exc_info() which gets it from tstate; |
| 3353 | but that's really a separate improvement. |
| 3354 | |
| 3355 | The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ |
| 3356 | variables to what they were before the current frame was called. The |
| 3357 | set_exc_info() function saves them on the frame so that |
| 3358 | reset_exc_info() can restore them. The invariant is that |
| 3359 | frame->f_exc_ZZZ is NULL iff the current frame never caught an |
| 3360 | exception (where "catching" an exception applies only to successful |
| 3361 | except clauses); and if the current frame ever caught an exception, |
| 3362 | frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ |
| 3363 | at the start of the current frame. |
| 3364 | |
| 3365 | */ |
| 3366 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 3367 | static void |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3368 | set_exc_info(PyThreadState *tstate, |
| 3369 | PyObject *type, PyObject *value, PyObject *tb) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3370 | { |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 3371 | PyFrameObject *frame = tstate->frame; |
Guido van Rossum | df4c308 | 1997-05-20 17:06:11 +0000 | [diff] [blame] | 3372 | PyObject *tmp_type, *tmp_value, *tmp_tb; |
Barry Warsaw | 4249f54 | 1997-08-22 21:26:19 +0000 | [diff] [blame] | 3373 | |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 3374 | assert(type != NULL); |
| 3375 | assert(frame != NULL); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3376 | if (frame->f_exc_type == NULL) { |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 3377 | assert(frame->f_exc_value == NULL); |
| 3378 | assert(frame->f_exc_traceback == NULL); |
| 3379 | /* This frame didn't catch an exception before. */ |
| 3380 | /* Save previous exception of this thread in this frame. */ |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3381 | if (tstate->exc_type == NULL) { |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 3382 | /* XXX Why is this set to Py_None? */ |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3383 | Py_INCREF(Py_None); |
| 3384 | tstate->exc_type = Py_None; |
| 3385 | } |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 3386 | Py_INCREF(tstate->exc_type); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3387 | Py_XINCREF(tstate->exc_value); |
| 3388 | Py_XINCREF(tstate->exc_traceback); |
| 3389 | frame->f_exc_type = tstate->exc_type; |
| 3390 | frame->f_exc_value = tstate->exc_value; |
| 3391 | frame->f_exc_traceback = tstate->exc_traceback; |
| 3392 | } |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 3393 | /* Set new exception for this thread. */ |
Guido van Rossum | df4c308 | 1997-05-20 17:06:11 +0000 | [diff] [blame] | 3394 | tmp_type = tstate->exc_type; |
| 3395 | tmp_value = tstate->exc_value; |
| 3396 | tmp_tb = tstate->exc_traceback; |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 3397 | Py_INCREF(type); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3398 | Py_XINCREF(value); |
| 3399 | Py_XINCREF(tb); |
| 3400 | tstate->exc_type = type; |
| 3401 | tstate->exc_value = value; |
| 3402 | tstate->exc_traceback = tb; |
Guido van Rossum | df4c308 | 1997-05-20 17:06:11 +0000 | [diff] [blame] | 3403 | Py_XDECREF(tmp_type); |
| 3404 | Py_XDECREF(tmp_value); |
| 3405 | Py_XDECREF(tmp_tb); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3406 | /* For b/w compatibility */ |
| 3407 | PySys_SetObject("exc_type", type); |
| 3408 | PySys_SetObject("exc_value", value); |
| 3409 | PySys_SetObject("exc_traceback", tb); |
| 3410 | } |
| 3411 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 3412 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3413 | reset_exc_info(PyThreadState *tstate) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3414 | { |
| 3415 | PyFrameObject *frame; |
Guido van Rossum | df4c308 | 1997-05-20 17:06:11 +0000 | [diff] [blame] | 3416 | PyObject *tmp_type, *tmp_value, *tmp_tb; |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 3417 | |
| 3418 | /* It's a precondition that the thread state's frame caught an |
| 3419 | * exception -- verify in a debug build. |
| 3420 | */ |
| 3421 | assert(tstate != NULL); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3422 | frame = tstate->frame; |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 3423 | assert(frame != NULL); |
| 3424 | assert(frame->f_exc_type != NULL); |
| 3425 | |
| 3426 | /* Copy the frame's exception info back to the thread state. */ |
| 3427 | tmp_type = tstate->exc_type; |
| 3428 | tmp_value = tstate->exc_value; |
| 3429 | tmp_tb = tstate->exc_traceback; |
| 3430 | Py_INCREF(frame->f_exc_type); |
| 3431 | Py_XINCREF(frame->f_exc_value); |
| 3432 | Py_XINCREF(frame->f_exc_traceback); |
| 3433 | tstate->exc_type = frame->f_exc_type; |
| 3434 | tstate->exc_value = frame->f_exc_value; |
| 3435 | tstate->exc_traceback = frame->f_exc_traceback; |
| 3436 | Py_XDECREF(tmp_type); |
| 3437 | Py_XDECREF(tmp_value); |
| 3438 | Py_XDECREF(tmp_tb); |
| 3439 | |
| 3440 | /* For b/w compatibility */ |
| 3441 | PySys_SetObject("exc_type", frame->f_exc_type); |
| 3442 | PySys_SetObject("exc_value", frame->f_exc_value); |
| 3443 | PySys_SetObject("exc_traceback", frame->f_exc_traceback); |
| 3444 | |
| 3445 | /* Clear the frame's exception info. */ |
Guido van Rossum | df4c308 | 1997-05-20 17:06:11 +0000 | [diff] [blame] | 3446 | tmp_type = frame->f_exc_type; |
| 3447 | tmp_value = frame->f_exc_value; |
| 3448 | tmp_tb = frame->f_exc_traceback; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3449 | frame->f_exc_type = NULL; |
| 3450 | frame->f_exc_value = NULL; |
| 3451 | frame->f_exc_traceback = NULL; |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 3452 | Py_DECREF(tmp_type); |
Guido van Rossum | df4c308 | 1997-05-20 17:06:11 +0000 | [diff] [blame] | 3453 | Py_XDECREF(tmp_value); |
| 3454 | Py_XDECREF(tmp_tb); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3455 | } |
| 3456 | |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3457 | /* Logic for the raise statement (too complicated for inlining). |
| 3458 | This *consumes* a reference count to each of its arguments. */ |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 3459 | static enum why_code |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3460 | do_raise(PyObject *type, PyObject *value, PyObject *tb) |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3461 | { |
Guido van Rossum | d295f12 | 1998-04-09 21:39:57 +0000 | [diff] [blame] | 3462 | if (type == NULL) { |
| 3463 | /* Reraise */ |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 3464 | PyThreadState *tstate = PyThreadState_GET(); |
Guido van Rossum | d295f12 | 1998-04-09 21:39:57 +0000 | [diff] [blame] | 3465 | type = tstate->exc_type == NULL ? Py_None : tstate->exc_type; |
| 3466 | value = tstate->exc_value; |
| 3467 | tb = tstate->exc_traceback; |
| 3468 | Py_XINCREF(type); |
| 3469 | Py_XINCREF(value); |
| 3470 | Py_XINCREF(tb); |
| 3471 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3472 | |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3473 | /* We support the following forms of raise: |
| 3474 | raise <class>, <classinstance> |
| 3475 | raise <class>, <argument tuple> |
| 3476 | raise <class>, None |
| 3477 | raise <class>, <argument> |
| 3478 | raise <classinstance>, None |
| 3479 | raise <string>, <object> |
| 3480 | raise <string>, None |
| 3481 | |
| 3482 | An omitted second argument is the same as None. |
| 3483 | |
| 3484 | In addition, raise <tuple>, <anything> is the same as |
| 3485 | raising the tuple's first item (and it better have one!); |
| 3486 | this rule is applied recursively. |
| 3487 | |
| 3488 | Finally, an optional third argument can be supplied, which |
| 3489 | gives the traceback to be substituted (useful when |
| 3490 | re-raising an exception after examining it). */ |
| 3491 | |
| 3492 | /* First, check the traceback argument, replacing None with |
| 3493 | NULL. */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3494 | if (tb == Py_None) { |
| 3495 | Py_DECREF(tb); |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3496 | tb = NULL; |
| 3497 | } |
| 3498 | else if (tb != NULL && !PyTraceBack_Check(tb)) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3499 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3500 | "raise: arg 3 must be a traceback or None"); |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3501 | goto raise_error; |
| 3502 | } |
| 3503 | |
| 3504 | /* Next, replace a missing value with None */ |
| 3505 | if (value == NULL) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3506 | value = Py_None; |
| 3507 | Py_INCREF(value); |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3508 | } |
| 3509 | |
| 3510 | /* Next, repeatedly, replace a tuple exception with its first item */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3511 | while (PyTuple_Check(type) && PyTuple_Size(type) > 0) { |
| 3512 | PyObject *tmp = type; |
| 3513 | type = PyTuple_GET_ITEM(type, 0); |
| 3514 | Py_INCREF(type); |
| 3515 | Py_DECREF(tmp); |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3516 | } |
| 3517 | |
Brett Cannon | 129bd52 | 2007-01-30 21:34:36 +0000 | [diff] [blame] | 3518 | if (PyExceptionClass_Check(type)) |
Barry Warsaw | 4249f54 | 1997-08-22 21:26:19 +0000 | [diff] [blame] | 3519 | PyErr_NormalizeException(&type, &value, &tb); |
| 3520 | |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 3521 | else if (PyExceptionInstance_Check(type)) { |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3522 | /* Raising an instance. The value should be a dummy. */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3523 | if (value != Py_None) { |
| 3524 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3525 | "instance exception may not have a separate value"); |
| 3526 | goto raise_error; |
| 3527 | } |
| 3528 | else { |
| 3529 | /* Normalize to raise <class>, <instance> */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3530 | Py_DECREF(value); |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3531 | value = type; |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 3532 | type = PyExceptionInstance_Class(type); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3533 | Py_INCREF(type); |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3534 | } |
| 3535 | } |
| 3536 | else { |
| 3537 | /* Not something you can raise. You get an exception |
| 3538 | anyway, just not what you specified :-) */ |
Jeremy Hylton | 960d948 | 2001-04-27 02:25:33 +0000 | [diff] [blame] | 3539 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 9c4742e | 2010-03-07 00:00:37 +0000 | [diff] [blame] | 3540 | "exceptions must be old-style classes or " |
| 3541 | "derived from BaseException, not %s", |
| 3542 | type->ob_type->tp_name); |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3543 | goto raise_error; |
| 3544 | } |
Guido van Rossum | 504153d | 2008-03-18 04:26:48 +0000 | [diff] [blame] | 3545 | |
| 3546 | assert(PyExceptionClass_Check(type)); |
| 3547 | if (Py_Py3kWarningFlag && PyClass_Check(type)) { |
Benjamin Peterson | 9f4f481 | 2008-04-27 03:01:45 +0000 | [diff] [blame] | 3548 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
Benjamin Peterson | f19a7b9 | 2008-04-27 18:40:21 +0000 | [diff] [blame] | 3549 | "exceptions must derive from BaseException " |
| 3550 | "in 3.x", 1) < 0) |
Guido van Rossum | 504153d | 2008-03-18 04:26:48 +0000 | [diff] [blame] | 3551 | goto raise_error; |
| 3552 | } |
| 3553 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3554 | PyErr_Restore(type, value, tb); |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3555 | if (tb == NULL) |
| 3556 | return WHY_EXCEPTION; |
| 3557 | else |
| 3558 | return WHY_RERAISE; |
| 3559 | raise_error: |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3560 | Py_XDECREF(value); |
| 3561 | Py_XDECREF(type); |
| 3562 | Py_XDECREF(tb); |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3563 | return WHY_EXCEPTION; |
| 3564 | } |
| 3565 | |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 3566 | /* Iterate v argcnt times and store the results on the stack (via decreasing |
| 3567 | sp). Return 1 for success, 0 if error. */ |
| 3568 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 3569 | static int |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 3570 | unpack_iterable(PyObject *v, int argcnt, PyObject **sp) |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 3571 | { |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 3572 | int i = 0; |
| 3573 | PyObject *it; /* iter(v) */ |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 3574 | PyObject *w; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3575 | |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 3576 | assert(v != NULL); |
| 3577 | |
| 3578 | it = PyObject_GetIter(v); |
| 3579 | if (it == NULL) |
| 3580 | goto Error; |
| 3581 | |
| 3582 | for (; i < argcnt; i++) { |
| 3583 | w = PyIter_Next(it); |
| 3584 | if (w == NULL) { |
| 3585 | /* Iterator done, via error or exhaustion. */ |
| 3586 | if (!PyErr_Occurred()) { |
| 3587 | PyErr_Format(PyExc_ValueError, |
| 3588 | "need more than %d value%s to unpack", |
| 3589 | i, i == 1 ? "" : "s"); |
| 3590 | } |
| 3591 | goto Error; |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 3592 | } |
| 3593 | *--sp = w; |
| 3594 | } |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 3595 | |
| 3596 | /* We better have exhausted the iterator now. */ |
| 3597 | w = PyIter_Next(it); |
| 3598 | if (w == NULL) { |
| 3599 | if (PyErr_Occurred()) |
| 3600 | goto Error; |
| 3601 | Py_DECREF(it); |
| 3602 | return 1; |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 3603 | } |
Guido van Rossum | bb8f59a | 2001-12-03 19:33:25 +0000 | [diff] [blame] | 3604 | Py_DECREF(w); |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 3605 | PyErr_SetString(PyExc_ValueError, "too many values to unpack"); |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 3606 | /* fall through */ |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 3607 | Error: |
Barry Warsaw | 9101055 | 1997-08-25 22:30:51 +0000 | [diff] [blame] | 3608 | for (; i > 0; i--, sp++) |
| 3609 | Py_DECREF(*sp); |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 3610 | Py_XDECREF(it); |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 3611 | return 0; |
| 3612 | } |
| 3613 | |
| 3614 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 3615 | #ifdef LLTRACE |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 3616 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3617 | prtrace(PyObject *v, char *str) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3618 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3619 | printf("%s ", str); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3620 | if (PyObject_Print(v, stdout, 0) != 0) |
| 3621 | PyErr_Clear(); /* Don't know what else to do */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3622 | printf("\n"); |
Guido van Rossum | cc229ea | 2000-05-04 00:55:17 +0000 | [diff] [blame] | 3623 | return 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3624 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3625 | #endif |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3626 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 3627 | static void |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3628 | call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f) |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 3629 | { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3630 | PyObject *type, *value, *traceback, *arg; |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 3631 | int err; |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3632 | PyErr_Fetch(&type, &value, &traceback); |
Guido van Rossum | bd9ccca | 1992-04-09 14:58:08 +0000 | [diff] [blame] | 3633 | if (value == NULL) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3634 | value = Py_None; |
| 3635 | Py_INCREF(value); |
Guido van Rossum | bd9ccca | 1992-04-09 14:58:08 +0000 | [diff] [blame] | 3636 | } |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 3637 | arg = PyTuple_Pack(3, type, value, traceback); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 3638 | if (arg == NULL) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3639 | PyErr_Restore(type, value, traceback); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 3640 | return; |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 3641 | } |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3642 | err = call_trace(func, self, f, PyTrace_EXCEPTION, arg); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3643 | Py_DECREF(arg); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 3644 | if (err == 0) |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3645 | PyErr_Restore(type, value, traceback); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 3646 | else { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3647 | Py_XDECREF(type); |
| 3648 | Py_XDECREF(value); |
| 3649 | Py_XDECREF(traceback); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 3650 | } |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 3651 | } |
| 3652 | |
Amaury Forgeot d'Arc | 0d75f09 | 2007-11-13 21:54:28 +0000 | [diff] [blame] | 3653 | static int |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 3654 | call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame, |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 3655 | int what, PyObject *arg) |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 3656 | { |
| 3657 | PyObject *type, *value, *traceback; |
| 3658 | int err; |
| 3659 | PyErr_Fetch(&type, &value, &traceback); |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 3660 | err = call_trace(func, obj, frame, what, arg); |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 3661 | if (err == 0) |
Amaury Forgeot d'Arc | 0d75f09 | 2007-11-13 21:54:28 +0000 | [diff] [blame] | 3662 | { |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 3663 | PyErr_Restore(type, value, traceback); |
Amaury Forgeot d'Arc | 0d75f09 | 2007-11-13 21:54:28 +0000 | [diff] [blame] | 3664 | return 0; |
| 3665 | } |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 3666 | else { |
| 3667 | Py_XDECREF(type); |
| 3668 | Py_XDECREF(value); |
| 3669 | Py_XDECREF(traceback); |
Amaury Forgeot d'Arc | 0d75f09 | 2007-11-13 21:54:28 +0000 | [diff] [blame] | 3670 | return -1; |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 3671 | } |
| 3672 | } |
| 3673 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 3674 | static int |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3675 | call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame, |
| 3676 | int what, PyObject *arg) |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 3677 | { |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3678 | register PyThreadState *tstate = frame->f_tstate; |
| 3679 | int result; |
| 3680 | if (tstate->tracing) |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 3681 | return 0; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3682 | tstate->tracing++; |
Fred Drake | 9e3ad78 | 2001-07-03 23:39:52 +0000 | [diff] [blame] | 3683 | tstate->use_tracing = 0; |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3684 | result = func(obj, frame, what, arg); |
Fred Drake | 9e3ad78 | 2001-07-03 23:39:52 +0000 | [diff] [blame] | 3685 | tstate->use_tracing = ((tstate->c_tracefunc != NULL) |
| 3686 | || (tstate->c_profilefunc != NULL)); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3687 | tstate->tracing--; |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3688 | return result; |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 3689 | } |
| 3690 | |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 3691 | PyObject * |
| 3692 | _PyEval_CallTracing(PyObject *func, PyObject *args) |
| 3693 | { |
| 3694 | PyFrameObject *frame = PyEval_GetFrame(); |
| 3695 | PyThreadState *tstate = frame->f_tstate; |
| 3696 | int save_tracing = tstate->tracing; |
| 3697 | int save_use_tracing = tstate->use_tracing; |
| 3698 | PyObject *result; |
| 3699 | |
| 3700 | tstate->tracing = 0; |
| 3701 | tstate->use_tracing = ((tstate->c_tracefunc != NULL) |
| 3702 | || (tstate->c_profilefunc != NULL)); |
| 3703 | result = PyObject_Call(func, args, NULL); |
| 3704 | tstate->tracing = save_tracing; |
| 3705 | tstate->use_tracing = save_use_tracing; |
| 3706 | return result; |
| 3707 | } |
| 3708 | |
Jeffrey Yasskin | 655d835 | 2009-05-23 23:23:01 +0000 | [diff] [blame] | 3709 | /* See Objects/lnotab_notes.txt for a description of how tracing works. */ |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 3710 | static int |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 3711 | maybe_call_line_trace(Py_tracefunc func, PyObject *obj, |
Armin Rigo | bf57a14 | 2004-03-22 19:24:58 +0000 | [diff] [blame] | 3712 | PyFrameObject *frame, int *instr_lb, int *instr_ub, |
| 3713 | int *instr_prev) |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 3714 | { |
Michael W. Hudson | 006c752 | 2002-11-08 13:08:46 +0000 | [diff] [blame] | 3715 | int result = 0; |
Jeffrey Yasskin | 655d835 | 2009-05-23 23:23:01 +0000 | [diff] [blame] | 3716 | int line = frame->f_lineno; |
Michael W. Hudson | 006c752 | 2002-11-08 13:08:46 +0000 | [diff] [blame] | 3717 | |
Jeremy Hylton | a4ebc13 | 2006-04-18 14:47:00 +0000 | [diff] [blame] | 3718 | /* If the last instruction executed isn't in the current |
Jeffrey Yasskin | 655d835 | 2009-05-23 23:23:01 +0000 | [diff] [blame] | 3719 | instruction window, reset the window. |
Jeremy Hylton | a4ebc13 | 2006-04-18 14:47:00 +0000 | [diff] [blame] | 3720 | */ |
Jeffrey Yasskin | 655d835 | 2009-05-23 23:23:01 +0000 | [diff] [blame] | 3721 | if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) { |
Thomas Wouters | ae406c6 | 2007-09-19 17:27:43 +0000 | [diff] [blame] | 3722 | PyAddrPair bounds; |
Jeffrey Yasskin | 655d835 | 2009-05-23 23:23:01 +0000 | [diff] [blame] | 3723 | line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti, |
| 3724 | &bounds); |
Thomas Wouters | ae406c6 | 2007-09-19 17:27:43 +0000 | [diff] [blame] | 3725 | *instr_lb = bounds.ap_lower; |
| 3726 | *instr_ub = bounds.ap_upper; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 3727 | } |
Jeffrey Yasskin | 655d835 | 2009-05-23 23:23:01 +0000 | [diff] [blame] | 3728 | /* If the last instruction falls at the start of a line or if |
| 3729 | it represents a jump backwards, update the frame's line |
| 3730 | number and call the trace function. */ |
| 3731 | if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) { |
| 3732 | frame->f_lineno = line; |
Jeremy Hylton | a4ebc13 | 2006-04-18 14:47:00 +0000 | [diff] [blame] | 3733 | result = call_trace(func, obj, frame, PyTrace_LINE, Py_None); |
Armin Rigo | bf57a14 | 2004-03-22 19:24:58 +0000 | [diff] [blame] | 3734 | } |
| 3735 | *instr_prev = frame->f_lasti; |
Michael W. Hudson | 006c752 | 2002-11-08 13:08:46 +0000 | [diff] [blame] | 3736 | return result; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 3737 | } |
| 3738 | |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3739 | void |
| 3740 | PyEval_SetProfile(Py_tracefunc func, PyObject *arg) |
Fred Drake | d083839 | 2001-06-16 21:02:31 +0000 | [diff] [blame] | 3741 | { |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 3742 | PyThreadState *tstate = PyThreadState_GET(); |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3743 | PyObject *temp = tstate->c_profileobj; |
| 3744 | Py_XINCREF(arg); |
| 3745 | tstate->c_profilefunc = NULL; |
| 3746 | tstate->c_profileobj = NULL; |
Brett Cannon | 55fa66d | 2005-06-25 07:07:35 +0000 | [diff] [blame] | 3747 | /* Must make sure that tracing is not ignored if 'temp' is freed */ |
Fred Drake | 9e3ad78 | 2001-07-03 23:39:52 +0000 | [diff] [blame] | 3748 | tstate->use_tracing = tstate->c_tracefunc != NULL; |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3749 | Py_XDECREF(temp); |
| 3750 | tstate->c_profilefunc = func; |
| 3751 | tstate->c_profileobj = arg; |
Brett Cannon | 55fa66d | 2005-06-25 07:07:35 +0000 | [diff] [blame] | 3752 | /* Flag that tracing or profiling is turned on */ |
Fred Drake | 9e3ad78 | 2001-07-03 23:39:52 +0000 | [diff] [blame] | 3753 | tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL); |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3754 | } |
| 3755 | |
| 3756 | void |
| 3757 | PyEval_SetTrace(Py_tracefunc func, PyObject *arg) |
| 3758 | { |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 3759 | PyThreadState *tstate = PyThreadState_GET(); |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3760 | PyObject *temp = tstate->c_traceobj; |
Jeffrey Yasskin | fd8a1ec | 2008-12-03 06:46:45 +0000 | [diff] [blame] | 3761 | _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL); |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3762 | Py_XINCREF(arg); |
| 3763 | tstate->c_tracefunc = NULL; |
| 3764 | tstate->c_traceobj = NULL; |
Brett Cannon | 55fa66d | 2005-06-25 07:07:35 +0000 | [diff] [blame] | 3765 | /* Must make sure that profiling is not ignored if 'temp' is freed */ |
Fred Drake | 9e3ad78 | 2001-07-03 23:39:52 +0000 | [diff] [blame] | 3766 | tstate->use_tracing = tstate->c_profilefunc != NULL; |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3767 | Py_XDECREF(temp); |
| 3768 | tstate->c_tracefunc = func; |
| 3769 | tstate->c_traceobj = arg; |
Brett Cannon | 55fa66d | 2005-06-25 07:07:35 +0000 | [diff] [blame] | 3770 | /* Flag that tracing or profiling is turned on */ |
Fred Drake | 9e3ad78 | 2001-07-03 23:39:52 +0000 | [diff] [blame] | 3771 | tstate->use_tracing = ((func != NULL) |
| 3772 | || (tstate->c_profilefunc != NULL)); |
Fred Drake | d083839 | 2001-06-16 21:02:31 +0000 | [diff] [blame] | 3773 | } |
| 3774 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3775 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3776 | PyEval_GetBuiltins(void) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 3777 | { |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 3778 | PyFrameObject *current_frame = PyEval_GetFrame(); |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 3779 | if (current_frame == NULL) |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 3780 | return PyThreadState_GET()->interp->builtins; |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 3781 | else |
| 3782 | return current_frame->f_builtins; |
| 3783 | } |
| 3784 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3785 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3786 | PyEval_GetLocals(void) |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 3787 | { |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 3788 | PyFrameObject *current_frame = PyEval_GetFrame(); |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 3789 | if (current_frame == NULL) |
| 3790 | return NULL; |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3791 | PyFrame_FastToLocals(current_frame); |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 3792 | return current_frame->f_locals; |
| 3793 | } |
| 3794 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3795 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3796 | PyEval_GetGlobals(void) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3797 | { |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 3798 | PyFrameObject *current_frame = PyEval_GetFrame(); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3799 | if (current_frame == NULL) |
| 3800 | return NULL; |
| 3801 | else |
| 3802 | return current_frame->f_globals; |
| 3803 | } |
| 3804 | |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 3805 | PyFrameObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3806 | PyEval_GetFrame(void) |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 3807 | { |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 3808 | PyThreadState *tstate = PyThreadState_GET(); |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 3809 | return _PyThreadState_GetFrame(tstate); |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 3810 | } |
| 3811 | |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 3812 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3813 | PyEval_GetRestricted(void) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 3814 | { |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 3815 | PyFrameObject *current_frame = PyEval_GetFrame(); |
Neal Norwitz | b9845e7 | 2006-06-12 02:11:18 +0000 | [diff] [blame] | 3816 | return current_frame == NULL ? 0 : PyFrame_IsRestricted(current_frame); |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 3817 | } |
| 3818 | |
Guido van Rossum | be27026 | 1997-05-22 22:26:18 +0000 | [diff] [blame] | 3819 | int |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 3820 | PyEval_MergeCompilerFlags(PyCompilerFlags *cf) |
Jeremy Hylton | 061d106 | 2001-03-22 02:32:48 +0000 | [diff] [blame] | 3821 | { |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 3822 | PyFrameObject *current_frame = PyEval_GetFrame(); |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 3823 | int result = cf->cf_flags != 0; |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 3824 | |
| 3825 | if (current_frame != NULL) { |
| 3826 | const int codeflags = current_frame->f_code->co_flags; |
Tim Peters | e2c18e9 | 2001-08-17 20:47:47 +0000 | [diff] [blame] | 3827 | const int compilerflags = codeflags & PyCF_MASK; |
| 3828 | if (compilerflags) { |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 3829 | result = 1; |
Tim Peters | e2c18e9 | 2001-08-17 20:47:47 +0000 | [diff] [blame] | 3830 | cf->cf_flags |= compilerflags; |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 3831 | } |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 3832 | #if 0 /* future keyword */ |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 3833 | if (codeflags & CO_GENERATOR_ALLOWED) { |
| 3834 | result = 1; |
| 3835 | cf->cf_flags |= CO_GENERATOR_ALLOWED; |
| 3836 | } |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 3837 | #endif |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 3838 | } |
| 3839 | return result; |
Jeremy Hylton | 061d106 | 2001-03-22 02:32:48 +0000 | [diff] [blame] | 3840 | } |
| 3841 | |
| 3842 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3843 | Py_FlushLine(void) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3844 | { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3845 | PyObject *f = PySys_GetObject("stdout"); |
Guido van Rossum | be27026 | 1997-05-22 22:26:18 +0000 | [diff] [blame] | 3846 | if (f == NULL) |
| 3847 | return 0; |
| 3848 | if (!PyFile_SoftSpace(f, 0)) |
| 3849 | return 0; |
| 3850 | return PyFile_WriteString("\n", f); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3851 | } |
| 3852 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3853 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3854 | /* External interface to call any callable object. |
Antoine Pitrou | 76c8649 | 2010-04-01 16:42:11 +0000 | [diff] [blame] | 3855 | The arg must be a tuple or NULL. The kw must be a dict or NULL. */ |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 3856 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3857 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3858 | PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3859 | { |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3860 | PyObject *result; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3861 | |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3862 | if (arg == NULL) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3863 | arg = PyTuple_New(0); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3864 | if (arg == NULL) |
| 3865 | return NULL; |
| 3866 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3867 | else if (!PyTuple_Check(arg)) { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3868 | PyErr_SetString(PyExc_TypeError, |
| 3869 | "argument list must be a tuple"); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3870 | return NULL; |
| 3871 | } |
| 3872 | else |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3873 | Py_INCREF(arg); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3874 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3875 | if (kw != NULL && !PyDict_Check(kw)) { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3876 | PyErr_SetString(PyExc_TypeError, |
| 3877 | "keyword list must be a dictionary"); |
Guido van Rossum | 25826c9 | 2000-04-21 21:17:39 +0000 | [diff] [blame] | 3878 | Py_DECREF(arg); |
Guido van Rossum | e3e61c1 | 1995-08-04 04:14:47 +0000 | [diff] [blame] | 3879 | return NULL; |
| 3880 | } |
| 3881 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3882 | result = PyObject_Call(func, arg, kw); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3883 | Py_DECREF(arg); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3884 | return result; |
| 3885 | } |
| 3886 | |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 3887 | const char * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3888 | PyEval_GetFuncName(PyObject *func) |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 3889 | { |
| 3890 | if (PyMethod_Check(func)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3891 | return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func)); |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 3892 | else if (PyFunction_Check(func)) |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3893 | return PyString_AsString(((PyFunctionObject*)func)->func_name); |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 3894 | else if (PyCFunction_Check(func)) |
| 3895 | return ((PyCFunctionObject*)func)->m_ml->ml_name; |
| 3896 | else if (PyClass_Check(func)) |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3897 | return PyString_AsString(((PyClassObject*)func)->cl_name); |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 3898 | else if (PyInstance_Check(func)) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3899 | return PyString_AsString( |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 3900 | ((PyInstanceObject*)func)->in_class->cl_name); |
| 3901 | } else { |
| 3902 | return func->ob_type->tp_name; |
| 3903 | } |
| 3904 | } |
| 3905 | |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 3906 | const char * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3907 | PyEval_GetFuncDesc(PyObject *func) |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 3908 | { |
| 3909 | if (PyMethod_Check(func)) |
| 3910 | return "()"; |
| 3911 | else if (PyFunction_Check(func)) |
| 3912 | return "()"; |
| 3913 | else if (PyCFunction_Check(func)) |
| 3914 | return "()"; |
| 3915 | else if (PyClass_Check(func)) |
| 3916 | return " constructor"; |
| 3917 | else if (PyInstance_Check(func)) { |
| 3918 | return " instance"; |
| 3919 | } else { |
| 3920 | return " object"; |
| 3921 | } |
| 3922 | } |
| 3923 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 3924 | static void |
Jeremy Hylton | 192690e | 2002-08-16 18:36:11 +0000 | [diff] [blame] | 3925 | err_args(PyObject *func, int flags, int nargs) |
| 3926 | { |
| 3927 | if (flags & METH_NOARGS) |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 3928 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 86c659a | 2002-08-23 14:11:35 +0000 | [diff] [blame] | 3929 | "%.200s() takes no arguments (%d given)", |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 3930 | ((PyCFunctionObject *)func)->m_ml->ml_name, |
Jeremy Hylton | 192690e | 2002-08-16 18:36:11 +0000 | [diff] [blame] | 3931 | nargs); |
| 3932 | else |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 3933 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 86c659a | 2002-08-23 14:11:35 +0000 | [diff] [blame] | 3934 | "%.200s() takes exactly one argument (%d given)", |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 3935 | ((PyCFunctionObject *)func)->m_ml->ml_name, |
Jeremy Hylton | 192690e | 2002-08-16 18:36:11 +0000 | [diff] [blame] | 3936 | nargs); |
| 3937 | } |
| 3938 | |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 3939 | #define C_TRACE(x, call) \ |
Nicholas Bastin | d858a77 | 2004-06-25 23:31:06 +0000 | [diff] [blame] | 3940 | if (tstate->use_tracing && tstate->c_profilefunc) { \ |
| 3941 | if (call_trace(tstate->c_profilefunc, \ |
| 3942 | tstate->c_profileobj, \ |
| 3943 | tstate->frame, PyTrace_C_CALL, \ |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 3944 | func)) { \ |
| 3945 | x = NULL; \ |
| 3946 | } \ |
| 3947 | else { \ |
| 3948 | x = call; \ |
| 3949 | if (tstate->c_profilefunc != NULL) { \ |
| 3950 | if (x == NULL) { \ |
| 3951 | call_trace_protected(tstate->c_profilefunc, \ |
| 3952 | tstate->c_profileobj, \ |
| 3953 | tstate->frame, PyTrace_C_EXCEPTION, \ |
| 3954 | func); \ |
| 3955 | /* XXX should pass (type, value, tb) */ \ |
| 3956 | } else { \ |
| 3957 | if (call_trace(tstate->c_profilefunc, \ |
| 3958 | tstate->c_profileobj, \ |
| 3959 | tstate->frame, PyTrace_C_RETURN, \ |
| 3960 | func)) { \ |
| 3961 | Py_DECREF(x); \ |
| 3962 | x = NULL; \ |
| 3963 | } \ |
| 3964 | } \ |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 3965 | } \ |
Nicholas Bastin | d858a77 | 2004-06-25 23:31:06 +0000 | [diff] [blame] | 3966 | } \ |
| 3967 | } else { \ |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 3968 | x = call; \ |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 3969 | } |
| 3970 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 3971 | static PyObject * |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 3972 | call_function(PyObject ***pp_stack, int oparg |
| 3973 | #ifdef WITH_TSC |
| 3974 | , uint64* pintr0, uint64* pintr1 |
| 3975 | #endif |
| 3976 | ) |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 3977 | { |
| 3978 | int na = oparg & 0xff; |
| 3979 | int nk = (oparg>>8) & 0xff; |
| 3980 | int n = na + 2 * nk; |
| 3981 | PyObject **pfunc = (*pp_stack) - n - 1; |
| 3982 | PyObject *func = *pfunc; |
| 3983 | PyObject *x, *w; |
| 3984 | |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 3985 | /* Always dispatch PyCFunction first, because these are |
| 3986 | presumed to be the most frequent callable object. |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 3987 | */ |
| 3988 | if (PyCFunction_Check(func) && nk == 0) { |
| 3989 | int flags = PyCFunction_GET_FLAGS(func); |
Nicholas Bastin | d858a77 | 2004-06-25 23:31:06 +0000 | [diff] [blame] | 3990 | PyThreadState *tstate = PyThreadState_GET(); |
Raymond Hettinger | a7f56bc | 2004-06-26 04:34:33 +0000 | [diff] [blame] | 3991 | |
| 3992 | PCALL(PCALL_CFUNCTION); |
Jeremy Hylton | 192690e | 2002-08-16 18:36:11 +0000 | [diff] [blame] | 3993 | if (flags & (METH_NOARGS | METH_O)) { |
| 3994 | PyCFunction meth = PyCFunction_GET_FUNCTION(func); |
| 3995 | PyObject *self = PyCFunction_GET_SELF(func); |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 3996 | if (flags & METH_NOARGS && na == 0) { |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 3997 | C_TRACE(x, (*meth)(self,NULL)); |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 3998 | } |
Jeremy Hylton | 192690e | 2002-08-16 18:36:11 +0000 | [diff] [blame] | 3999 | else if (flags & METH_O && na == 1) { |
| 4000 | PyObject *arg = EXT_POP(*pp_stack); |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 4001 | C_TRACE(x, (*meth)(self,arg)); |
Jeremy Hylton | 192690e | 2002-08-16 18:36:11 +0000 | [diff] [blame] | 4002 | Py_DECREF(arg); |
| 4003 | } |
| 4004 | else { |
| 4005 | err_args(func, flags, na); |
| 4006 | x = NULL; |
| 4007 | } |
| 4008 | } |
| 4009 | else { |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 4010 | PyObject *callargs; |
| 4011 | callargs = load_args(pp_stack, na); |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 4012 | READ_TIMESTAMP(*pintr0); |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 4013 | C_TRACE(x, PyCFunction_Call(func,callargs,NULL)); |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 4014 | READ_TIMESTAMP(*pintr1); |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 4015 | Py_XDECREF(callargs); |
| 4016 | } |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 4017 | } else { |
| 4018 | if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) { |
| 4019 | /* optimize access to bound methods */ |
| 4020 | PyObject *self = PyMethod_GET_SELF(func); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4021 | PCALL(PCALL_METHOD); |
| 4022 | PCALL(PCALL_BOUND_METHOD); |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 4023 | Py_INCREF(self); |
| 4024 | func = PyMethod_GET_FUNCTION(func); |
| 4025 | Py_INCREF(func); |
| 4026 | Py_DECREF(*pfunc); |
| 4027 | *pfunc = self; |
| 4028 | na++; |
| 4029 | n++; |
| 4030 | } else |
| 4031 | Py_INCREF(func); |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 4032 | READ_TIMESTAMP(*pintr0); |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 4033 | if (PyFunction_Check(func)) |
| 4034 | x = fast_function(func, pp_stack, n, na, nk); |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 4035 | else |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 4036 | x = do_call(func, pp_stack, na, nk); |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 4037 | READ_TIMESTAMP(*pintr1); |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 4038 | Py_DECREF(func); |
| 4039 | } |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 4040 | |
Armin Rigo | d34fa52 | 2006-03-28 19:10:40 +0000 | [diff] [blame] | 4041 | /* Clear the stack of the function object. Also removes |
| 4042 | the arguments in case they weren't consumed already |
| 4043 | (fast_function() and err_args() leave them on the stack). |
Thomas Wouters | 7f59732 | 2006-03-01 05:32:33 +0000 | [diff] [blame] | 4044 | */ |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 4045 | while ((*pp_stack) > pfunc) { |
| 4046 | w = EXT_POP(*pp_stack); |
| 4047 | Py_DECREF(w); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4048 | PCALL(PCALL_POP); |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 4049 | } |
| 4050 | return x; |
| 4051 | } |
| 4052 | |
Jeremy Hylton | 192690e | 2002-08-16 18:36:11 +0000 | [diff] [blame] | 4053 | /* The fast_function() function optimize calls for which no argument |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4054 | tuple is necessary; the objects are passed directly from the stack. |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4055 | For the simplest case -- a function that takes only positional |
| 4056 | arguments and is called with only positional arguments -- it |
| 4057 | inlines the most primitive frame setup code from |
| 4058 | PyEval_EvalCodeEx(), which vastly reduces the checks that must be |
| 4059 | done before evaluating the frame. |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4060 | */ |
| 4061 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4062 | static PyObject * |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4063 | 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] | 4064 | { |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4065 | PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4066 | PyObject *globals = PyFunction_GET_GLOBALS(func); |
| 4067 | PyObject *argdefs = PyFunction_GET_DEFAULTS(func); |
| 4068 | PyObject **d = NULL; |
| 4069 | int nd = 0; |
| 4070 | |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4071 | PCALL(PCALL_FUNCTION); |
| 4072 | PCALL(PCALL_FAST_FUNCTION); |
Raymond Hettinger | 40174c3 | 2003-05-31 07:04:16 +0000 | [diff] [blame] | 4073 | if (argdefs == NULL && co->co_argcount == n && nk==0 && |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4074 | co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) { |
| 4075 | PyFrameObject *f; |
| 4076 | PyObject *retval = NULL; |
| 4077 | PyThreadState *tstate = PyThreadState_GET(); |
| 4078 | PyObject **fastlocals, **stack; |
| 4079 | int i; |
| 4080 | |
| 4081 | PCALL(PCALL_FASTER_FUNCTION); |
| 4082 | assert(globals != NULL); |
| 4083 | /* XXX Perhaps we should create a specialized |
| 4084 | PyFrame_New() that doesn't take locals, but does |
| 4085 | take builtins without sanity checking them. |
| 4086 | */ |
Neal Norwitz | df6a649 | 2006-08-13 18:10:10 +0000 | [diff] [blame] | 4087 | assert(tstate != NULL); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4088 | f = PyFrame_New(tstate, co, globals, NULL); |
| 4089 | if (f == NULL) |
| 4090 | return NULL; |
| 4091 | |
| 4092 | fastlocals = f->f_localsplus; |
| 4093 | stack = (*pp_stack) - n; |
| 4094 | |
| 4095 | for (i = 0; i < n; i++) { |
| 4096 | Py_INCREF(*stack); |
| 4097 | fastlocals[i] = *stack++; |
| 4098 | } |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 4099 | retval = PyEval_EvalFrameEx(f,0); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4100 | ++tstate->recursion_depth; |
| 4101 | Py_DECREF(f); |
| 4102 | --tstate->recursion_depth; |
| 4103 | return retval; |
| 4104 | } |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4105 | if (argdefs != NULL) { |
| 4106 | d = &PyTuple_GET_ITEM(argdefs, 0); |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4107 | nd = Py_SIZE(argdefs); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4108 | } |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4109 | return PyEval_EvalCodeEx(co, globals, |
| 4110 | (PyObject *)NULL, (*pp_stack)-n, na, |
| 4111 | (*pp_stack)-2*nk, nk, d, nd, |
| 4112 | PyFunction_GET_CLOSURE(func)); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4113 | } |
| 4114 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4115 | static PyObject * |
Ka-Ping Yee | 2057970 | 2001-01-15 22:14:16 +0000 | [diff] [blame] | 4116 | update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack, |
| 4117 | PyObject *func) |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4118 | { |
| 4119 | PyObject *kwdict = NULL; |
| 4120 | if (orig_kwdict == NULL) |
| 4121 | kwdict = PyDict_New(); |
| 4122 | else { |
| 4123 | kwdict = PyDict_Copy(orig_kwdict); |
| 4124 | Py_DECREF(orig_kwdict); |
| 4125 | } |
| 4126 | if (kwdict == NULL) |
| 4127 | return NULL; |
Raymond Hettinger | 5bed456 | 2004-04-10 23:34:17 +0000 | [diff] [blame] | 4128 | while (--nk >= 0) { |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4129 | int err; |
| 4130 | PyObject *value = EXT_POP(*pp_stack); |
| 4131 | PyObject *key = EXT_POP(*pp_stack); |
| 4132 | if (PyDict_GetItem(kwdict, key) != NULL) { |
Thomas Wouters | ae406c6 | 2007-09-19 17:27:43 +0000 | [diff] [blame] | 4133 | PyErr_Format(PyExc_TypeError, |
| 4134 | "%.200s%s got multiple values " |
| 4135 | "for keyword argument '%.200s'", |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4136 | PyEval_GetFuncName(func), |
| 4137 | PyEval_GetFuncDesc(func), |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4138 | PyString_AsString(key)); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4139 | Py_DECREF(key); |
| 4140 | Py_DECREF(value); |
| 4141 | Py_DECREF(kwdict); |
| 4142 | return NULL; |
| 4143 | } |
| 4144 | err = PyDict_SetItem(kwdict, key, value); |
| 4145 | Py_DECREF(key); |
| 4146 | Py_DECREF(value); |
| 4147 | if (err) { |
| 4148 | Py_DECREF(kwdict); |
| 4149 | return NULL; |
| 4150 | } |
| 4151 | } |
| 4152 | return kwdict; |
| 4153 | } |
| 4154 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4155 | static PyObject * |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4156 | update_star_args(int nstack, int nstar, PyObject *stararg, |
| 4157 | PyObject ***pp_stack) |
| 4158 | { |
| 4159 | PyObject *callargs, *w; |
| 4160 | |
| 4161 | callargs = PyTuple_New(nstack + nstar); |
| 4162 | if (callargs == NULL) { |
| 4163 | return NULL; |
| 4164 | } |
| 4165 | if (nstar) { |
| 4166 | int i; |
| 4167 | for (i = 0; i < nstar; i++) { |
| 4168 | PyObject *a = PyTuple_GET_ITEM(stararg, i); |
| 4169 | Py_INCREF(a); |
| 4170 | PyTuple_SET_ITEM(callargs, nstack + i, a); |
| 4171 | } |
| 4172 | } |
Raymond Hettinger | 5bed456 | 2004-04-10 23:34:17 +0000 | [diff] [blame] | 4173 | while (--nstack >= 0) { |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4174 | w = EXT_POP(*pp_stack); |
| 4175 | PyTuple_SET_ITEM(callargs, nstack, w); |
| 4176 | } |
| 4177 | return callargs; |
| 4178 | } |
| 4179 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4180 | static PyObject * |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4181 | load_args(PyObject ***pp_stack, int na) |
| 4182 | { |
| 4183 | PyObject *args = PyTuple_New(na); |
| 4184 | PyObject *w; |
| 4185 | |
| 4186 | if (args == NULL) |
| 4187 | return NULL; |
Raymond Hettinger | 5bed456 | 2004-04-10 23:34:17 +0000 | [diff] [blame] | 4188 | while (--na >= 0) { |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4189 | w = EXT_POP(*pp_stack); |
| 4190 | PyTuple_SET_ITEM(args, na, w); |
| 4191 | } |
| 4192 | return args; |
| 4193 | } |
| 4194 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4195 | static PyObject * |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4196 | do_call(PyObject *func, PyObject ***pp_stack, int na, int nk) |
| 4197 | { |
| 4198 | PyObject *callargs = NULL; |
| 4199 | PyObject *kwdict = NULL; |
| 4200 | PyObject *result = NULL; |
| 4201 | |
| 4202 | if (nk > 0) { |
Ka-Ping Yee | 2057970 | 2001-01-15 22:14:16 +0000 | [diff] [blame] | 4203 | kwdict = update_keyword_args(NULL, nk, pp_stack, func); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4204 | if (kwdict == NULL) |
| 4205 | goto call_fail; |
| 4206 | } |
| 4207 | callargs = load_args(pp_stack, na); |
| 4208 | if (callargs == NULL) |
| 4209 | goto call_fail; |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4210 | #ifdef CALL_PROFILE |
| 4211 | /* At this point, we have to look at the type of func to |
| 4212 | update the call stats properly. Do it here so as to avoid |
| 4213 | exposing the call stats machinery outside ceval.c |
| 4214 | */ |
| 4215 | if (PyFunction_Check(func)) |
| 4216 | PCALL(PCALL_FUNCTION); |
| 4217 | else if (PyMethod_Check(func)) |
| 4218 | PCALL(PCALL_METHOD); |
| 4219 | else if (PyType_Check(func)) |
| 4220 | PCALL(PCALL_TYPE); |
Antoine Pitrou | 46dbe27 | 2009-05-30 21:27:00 +0000 | [diff] [blame] | 4221 | else if (PyCFunction_Check(func)) |
| 4222 | PCALL(PCALL_CFUNCTION); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4223 | else |
| 4224 | PCALL(PCALL_OTHER); |
| 4225 | #endif |
Antoine Pitrou | 46dbe27 | 2009-05-30 21:27:00 +0000 | [diff] [blame] | 4226 | if (PyCFunction_Check(func)) { |
| 4227 | PyThreadState *tstate = PyThreadState_GET(); |
| 4228 | C_TRACE(result, PyCFunction_Call(func, callargs, kwdict)); |
| 4229 | } |
| 4230 | else |
| 4231 | result = PyObject_Call(func, callargs, kwdict); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4232 | call_fail: |
| 4233 | Py_XDECREF(callargs); |
| 4234 | Py_XDECREF(kwdict); |
| 4235 | return result; |
| 4236 | } |
| 4237 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4238 | static PyObject * |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4239 | ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk) |
| 4240 | { |
| 4241 | int nstar = 0; |
| 4242 | PyObject *callargs = NULL; |
| 4243 | PyObject *stararg = NULL; |
| 4244 | PyObject *kwdict = NULL; |
| 4245 | PyObject *result = NULL; |
| 4246 | |
| 4247 | if (flags & CALL_FLAG_KW) { |
| 4248 | kwdict = EXT_POP(*pp_stack); |
Georg Brandl | 2134e75 | 2007-05-21 20:34:16 +0000 | [diff] [blame] | 4249 | if (!PyDict_Check(kwdict)) { |
| 4250 | PyObject *d; |
| 4251 | d = PyDict_New(); |
| 4252 | if (d == NULL) |
| 4253 | goto ext_call_fail; |
| 4254 | if (PyDict_Update(d, kwdict) != 0) { |
| 4255 | Py_DECREF(d); |
| 4256 | /* PyDict_Update raises attribute |
| 4257 | * error (percolated from an attempt |
| 4258 | * to get 'keys' attribute) instead of |
| 4259 | * a type error if its second argument |
| 4260 | * is not a mapping. |
| 4261 | */ |
| 4262 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 4263 | PyErr_Format(PyExc_TypeError, |
| 4264 | "%.200s%.200s argument after ** " |
| 4265 | "must be a mapping, not %.200s", |
| 4266 | PyEval_GetFuncName(func), |
| 4267 | PyEval_GetFuncDesc(func), |
| 4268 | kwdict->ob_type->tp_name); |
| 4269 | } |
| 4270 | goto ext_call_fail; |
| 4271 | } |
| 4272 | Py_DECREF(kwdict); |
| 4273 | kwdict = d; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4274 | } |
| 4275 | } |
| 4276 | if (flags & CALL_FLAG_VAR) { |
| 4277 | stararg = EXT_POP(*pp_stack); |
| 4278 | if (!PyTuple_Check(stararg)) { |
| 4279 | PyObject *t = NULL; |
| 4280 | t = PySequence_Tuple(stararg); |
| 4281 | if (t == NULL) { |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4282 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 4283 | PyErr_Format(PyExc_TypeError, |
Georg Brandl | 2134e75 | 2007-05-21 20:34:16 +0000 | [diff] [blame] | 4284 | "%.200s%.200s argument after * " |
| 4285 | "must be a sequence, not %200s", |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4286 | PyEval_GetFuncName(func), |
Georg Brandl | 2134e75 | 2007-05-21 20:34:16 +0000 | [diff] [blame] | 4287 | PyEval_GetFuncDesc(func), |
| 4288 | stararg->ob_type->tp_name); |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4289 | } |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4290 | goto ext_call_fail; |
| 4291 | } |
| 4292 | Py_DECREF(stararg); |
| 4293 | stararg = t; |
| 4294 | } |
| 4295 | nstar = PyTuple_GET_SIZE(stararg); |
| 4296 | } |
| 4297 | if (nk > 0) { |
Ka-Ping Yee | 2057970 | 2001-01-15 22:14:16 +0000 | [diff] [blame] | 4298 | kwdict = update_keyword_args(kwdict, nk, pp_stack, func); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4299 | if (kwdict == NULL) |
| 4300 | goto ext_call_fail; |
| 4301 | } |
| 4302 | callargs = update_star_args(na, nstar, stararg, pp_stack); |
| 4303 | if (callargs == NULL) |
| 4304 | goto ext_call_fail; |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4305 | #ifdef CALL_PROFILE |
| 4306 | /* At this point, we have to look at the type of func to |
| 4307 | update the call stats properly. Do it here so as to avoid |
| 4308 | exposing the call stats machinery outside ceval.c |
| 4309 | */ |
| 4310 | if (PyFunction_Check(func)) |
| 4311 | PCALL(PCALL_FUNCTION); |
| 4312 | else if (PyMethod_Check(func)) |
| 4313 | PCALL(PCALL_METHOD); |
| 4314 | else if (PyType_Check(func)) |
| 4315 | PCALL(PCALL_TYPE); |
Antoine Pitrou | 46dbe27 | 2009-05-30 21:27:00 +0000 | [diff] [blame] | 4316 | else if (PyCFunction_Check(func)) |
| 4317 | PCALL(PCALL_CFUNCTION); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4318 | else |
| 4319 | PCALL(PCALL_OTHER); |
| 4320 | #endif |
Antoine Pitrou | 46dbe27 | 2009-05-30 21:27:00 +0000 | [diff] [blame] | 4321 | if (PyCFunction_Check(func)) { |
| 4322 | PyThreadState *tstate = PyThreadState_GET(); |
| 4323 | C_TRACE(result, PyCFunction_Call(func, callargs, kwdict)); |
| 4324 | } |
| 4325 | else |
| 4326 | result = PyObject_Call(func, callargs, kwdict); |
Thomas Wouters | ae406c6 | 2007-09-19 17:27:43 +0000 | [diff] [blame] | 4327 | ext_call_fail: |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4328 | Py_XDECREF(callargs); |
| 4329 | Py_XDECREF(kwdict); |
| 4330 | Py_XDECREF(stararg); |
| 4331 | return result; |
| 4332 | } |
| 4333 | |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 4334 | /* Extract a slice index from a PyInt or PyLong or an object with the |
| 4335 | nb_index slot defined, and store in *pi. |
| 4336 | Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX, |
| 4337 | 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] | 4338 | Return 0 on error, 1 on success. |
Tim Peters | cb479e7 | 2001-12-16 19:11:44 +0000 | [diff] [blame] | 4339 | */ |
Tim Peters | b519638 | 2001-12-16 19:44:20 +0000 | [diff] [blame] | 4340 | /* Note: If v is NULL, return success without storing into *pi. This |
| 4341 | is because_PyEval_SliceIndex() is called by apply_slice(), which can be |
| 4342 | 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] | 4343 | */ |
Guido van Rossum | 20c6add | 2000-05-08 14:06:50 +0000 | [diff] [blame] | 4344 | int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4345 | _PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4346 | { |
Tim Peters | b519638 | 2001-12-16 19:44:20 +0000 | [diff] [blame] | 4347 | if (v != NULL) { |
Martin v. Löwis | dde99d2 | 2006-02-17 15:57:41 +0000 | [diff] [blame] | 4348 | Py_ssize_t x; |
Andrew M. Kuchling | 2194b16 | 2000-02-23 22:18:48 +0000 | [diff] [blame] | 4349 | if (PyInt_Check(v)) { |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 4350 | /* XXX(nnorwitz): I think PyInt_AS_LONG is correct, |
| 4351 | however, it looks like it should be AsSsize_t. |
| 4352 | There should be a comment here explaining why. |
| 4353 | */ |
| 4354 | x = PyInt_AS_LONG(v); |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 4355 | } |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 4356 | else if (PyIndex_Check(v)) { |
| 4357 | x = PyNumber_AsSsize_t(v, NULL); |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 4358 | if (x == -1 && PyErr_Occurred()) |
| 4359 | return 0; |
| 4360 | } |
| 4361 | else { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 4362 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 4363 | "slice indices must be integers or " |
| 4364 | "None or have an __index__ method"); |
Guido van Rossum | 20c6add | 2000-05-08 14:06:50 +0000 | [diff] [blame] | 4365 | return 0; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4366 | } |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 4367 | *pi = x; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4368 | } |
Guido van Rossum | 20c6add | 2000-05-08 14:06:50 +0000 | [diff] [blame] | 4369 | return 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4370 | } |
| 4371 | |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 4372 | #undef ISINDEX |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 4373 | #define ISINDEX(x) ((x) == NULL || \ |
| 4374 | PyInt_Check(x) || PyLong_Check(x) || PyIndex_Check(x)) |
Guido van Rossum | 50d756e | 2001-08-18 17:43:36 +0000 | [diff] [blame] | 4375 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4376 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4377 | 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] | 4378 | { |
Guido van Rossum | 50d756e | 2001-08-18 17:43:36 +0000 | [diff] [blame] | 4379 | PyTypeObject *tp = u->ob_type; |
| 4380 | PySequenceMethods *sq = tp->tp_as_sequence; |
| 4381 | |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 4382 | if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) { |
Martin v. Löwis | dde99d2 | 2006-02-17 15:57:41 +0000 | [diff] [blame] | 4383 | Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX; |
Guido van Rossum | 50d756e | 2001-08-18 17:43:36 +0000 | [diff] [blame] | 4384 | if (!_PyEval_SliceIndex(v, &ilow)) |
| 4385 | return NULL; |
| 4386 | if (!_PyEval_SliceIndex(w, &ihigh)) |
| 4387 | return NULL; |
| 4388 | return PySequence_GetSlice(u, ilow, ihigh); |
| 4389 | } |
| 4390 | else { |
| 4391 | PyObject *slice = PySlice_New(v, w, NULL); |
Guido van Rossum | 354797c | 2001-12-03 19:45:06 +0000 | [diff] [blame] | 4392 | if (slice != NULL) { |
| 4393 | PyObject *res = PyObject_GetItem(u, slice); |
| 4394 | Py_DECREF(slice); |
| 4395 | return res; |
| 4396 | } |
Guido van Rossum | 50d756e | 2001-08-18 17:43:36 +0000 | [diff] [blame] | 4397 | else |
| 4398 | return NULL; |
| 4399 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4400 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4401 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4402 | static int |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4403 | assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x) |
| 4404 | /* u[v:w] = x */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4405 | { |
Guido van Rossum | 50d756e | 2001-08-18 17:43:36 +0000 | [diff] [blame] | 4406 | PyTypeObject *tp = u->ob_type; |
| 4407 | PySequenceMethods *sq = tp->tp_as_sequence; |
| 4408 | |
Georg Brandl | 0fca97a | 2007-03-05 22:28:08 +0000 | [diff] [blame] | 4409 | if (sq && sq->sq_ass_slice && ISINDEX(v) && ISINDEX(w)) { |
Martin v. Löwis | dde99d2 | 2006-02-17 15:57:41 +0000 | [diff] [blame] | 4410 | Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX; |
Guido van Rossum | 50d756e | 2001-08-18 17:43:36 +0000 | [diff] [blame] | 4411 | if (!_PyEval_SliceIndex(v, &ilow)) |
| 4412 | return -1; |
| 4413 | if (!_PyEval_SliceIndex(w, &ihigh)) |
| 4414 | return -1; |
| 4415 | if (x == NULL) |
| 4416 | return PySequence_DelSlice(u, ilow, ihigh); |
| 4417 | else |
| 4418 | return PySequence_SetSlice(u, ilow, ihigh, x); |
| 4419 | } |
| 4420 | else { |
| 4421 | PyObject *slice = PySlice_New(v, w, NULL); |
| 4422 | if (slice != NULL) { |
Guido van Rossum | 354797c | 2001-12-03 19:45:06 +0000 | [diff] [blame] | 4423 | int res; |
Guido van Rossum | 50d756e | 2001-08-18 17:43:36 +0000 | [diff] [blame] | 4424 | if (x != NULL) |
Guido van Rossum | 354797c | 2001-12-03 19:45:06 +0000 | [diff] [blame] | 4425 | res = PyObject_SetItem(u, slice, x); |
Guido van Rossum | 50d756e | 2001-08-18 17:43:36 +0000 | [diff] [blame] | 4426 | else |
Guido van Rossum | 354797c | 2001-12-03 19:45:06 +0000 | [diff] [blame] | 4427 | res = PyObject_DelItem(u, slice); |
| 4428 | Py_DECREF(slice); |
| 4429 | return res; |
Guido van Rossum | 50d756e | 2001-08-18 17:43:36 +0000 | [diff] [blame] | 4430 | } |
| 4431 | else |
| 4432 | return -1; |
| 4433 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4434 | } |
| 4435 | |
Guido van Rossum | 04edb52 | 2008-03-18 02:49:46 +0000 | [diff] [blame] | 4436 | #define Py3kExceptionClass_Check(x) \ |
| 4437 | (PyType_Check((x)) && \ |
| 4438 | PyType_FastSubclass((PyTypeObject*)(x), Py_TPFLAGS_BASE_EXC_SUBCLASS)) |
| 4439 | |
| 4440 | #define CANNOT_CATCH_MSG "catching classes that don't inherit from " \ |
Georg Brandl | d5b635f | 2008-03-25 08:29:14 +0000 | [diff] [blame] | 4441 | "BaseException is not allowed in 3.x" |
Guido van Rossum | 04edb52 | 2008-03-18 02:49:46 +0000 | [diff] [blame] | 4442 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4443 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4444 | cmp_outcome(int op, register PyObject *v, register PyObject *w) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4445 | { |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4446 | int res = 0; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4447 | switch (op) { |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 4448 | case PyCmp_IS: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4449 | res = (v == w); |
Raymond Hettinger | 4bad9ba | 2003-01-19 05:08:13 +0000 | [diff] [blame] | 4450 | break; |
| 4451 | case PyCmp_IS_NOT: |
| 4452 | res = (v != w); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4453 | break; |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 4454 | case PyCmp_IN: |
Raymond Hettinger | 4bad9ba | 2003-01-19 05:08:13 +0000 | [diff] [blame] | 4455 | res = PySequence_Contains(w, v); |
| 4456 | if (res < 0) |
| 4457 | return NULL; |
| 4458 | break; |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 4459 | case PyCmp_NOT_IN: |
Guido van Rossum | 7e33c6e | 1998-05-22 00:52:29 +0000 | [diff] [blame] | 4460 | res = PySequence_Contains(w, v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4461 | if (res < 0) |
| 4462 | return NULL; |
Raymond Hettinger | 4bad9ba | 2003-01-19 05:08:13 +0000 | [diff] [blame] | 4463 | res = !res; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4464 | break; |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 4465 | case PyCmp_EXC_MATCH: |
Brett Cannon | 129bd52 | 2007-01-30 21:34:36 +0000 | [diff] [blame] | 4466 | if (PyTuple_Check(w)) { |
| 4467 | Py_ssize_t i, length; |
| 4468 | length = PyTuple_Size(w); |
| 4469 | for (i = 0; i < length; i += 1) { |
| 4470 | PyObject *exc = PyTuple_GET_ITEM(w, i); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4471 | if (PyString_Check(exc)) { |
Brett Cannon | 129bd52 | 2007-01-30 21:34:36 +0000 | [diff] [blame] | 4472 | int ret_val; |
| 4473 | ret_val = PyErr_WarnEx( |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 4474 | PyExc_DeprecationWarning, |
| 4475 | "catching of string " |
| 4476 | "exceptions is deprecated", 1); |
Benjamin Peterson | f19a7b9 | 2008-04-27 18:40:21 +0000 | [diff] [blame] | 4477 | if (ret_val < 0) |
Brett Cannon | 129bd52 | 2007-01-30 21:34:36 +0000 | [diff] [blame] | 4478 | return NULL; |
| 4479 | } |
Guido van Rossum | 20bda58 | 2008-03-18 03:15:05 +0000 | [diff] [blame] | 4480 | else if (Py_Py3kWarningFlag && |
| 4481 | !PyTuple_Check(exc) && |
| 4482 | !Py3kExceptionClass_Check(exc)) |
Guido van Rossum | 04edb52 | 2008-03-18 02:49:46 +0000 | [diff] [blame] | 4483 | { |
| 4484 | int ret_val; |
| 4485 | ret_val = PyErr_WarnEx( |
| 4486 | PyExc_DeprecationWarning, |
| 4487 | CANNOT_CATCH_MSG, 1); |
Benjamin Peterson | f19a7b9 | 2008-04-27 18:40:21 +0000 | [diff] [blame] | 4488 | if (ret_val < 0) |
Guido van Rossum | 04edb52 | 2008-03-18 02:49:46 +0000 | [diff] [blame] | 4489 | return NULL; |
| 4490 | } |
Brett Cannon | 129bd52 | 2007-01-30 21:34:36 +0000 | [diff] [blame] | 4491 | } |
| 4492 | } |
| 4493 | else { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4494 | if (PyString_Check(w)) { |
Brett Cannon | 129bd52 | 2007-01-30 21:34:36 +0000 | [diff] [blame] | 4495 | int ret_val; |
| 4496 | ret_val = PyErr_WarnEx( |
| 4497 | PyExc_DeprecationWarning, |
| 4498 | "catching of string " |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 4499 | "exceptions is deprecated", 1); |
Benjamin Peterson | f19a7b9 | 2008-04-27 18:40:21 +0000 | [diff] [blame] | 4500 | if (ret_val < 0) |
Brett Cannon | 129bd52 | 2007-01-30 21:34:36 +0000 | [diff] [blame] | 4501 | return NULL; |
| 4502 | } |
Guido van Rossum | 20bda58 | 2008-03-18 03:15:05 +0000 | [diff] [blame] | 4503 | else if (Py_Py3kWarningFlag && |
| 4504 | !PyTuple_Check(w) && |
| 4505 | !Py3kExceptionClass_Check(w)) |
Guido van Rossum | 04edb52 | 2008-03-18 02:49:46 +0000 | [diff] [blame] | 4506 | { |
| 4507 | int ret_val; |
| 4508 | ret_val = PyErr_WarnEx( |
| 4509 | PyExc_DeprecationWarning, |
| 4510 | CANNOT_CATCH_MSG, 1); |
Benjamin Peterson | f19a7b9 | 2008-04-27 18:40:21 +0000 | [diff] [blame] | 4511 | if (ret_val < 0) |
Guido van Rossum | 04edb52 | 2008-03-18 02:49:46 +0000 | [diff] [blame] | 4512 | return NULL; |
| 4513 | } |
Brett Cannon | 129bd52 | 2007-01-30 21:34:36 +0000 | [diff] [blame] | 4514 | } |
Barry Warsaw | 4249f54 | 1997-08-22 21:26:19 +0000 | [diff] [blame] | 4515 | res = PyErr_GivenExceptionMatches(v, w); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4516 | break; |
| 4517 | default: |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4518 | return PyObject_RichCompare(v, w, op); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4519 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4520 | v = res ? Py_True : Py_False; |
| 4521 | Py_INCREF(v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4522 | return v; |
| 4523 | } |
| 4524 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4525 | static PyObject * |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 4526 | import_from(PyObject *v, PyObject *name) |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 4527 | { |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 4528 | PyObject *x; |
| 4529 | |
| 4530 | x = PyObject_GetAttr(v, name); |
| 4531 | if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) { |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 4532 | PyErr_Format(PyExc_ImportError, |
| 4533 | "cannot import name %.230s", |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4534 | PyString_AsString(name)); |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 4535 | } |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 4536 | return x; |
| 4537 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4538 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4539 | static int |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 4540 | import_all_from(PyObject *locals, PyObject *v) |
| 4541 | { |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 4542 | PyObject *all = PyObject_GetAttrString(v, "__all__"); |
| 4543 | PyObject *dict, *name, *value; |
| 4544 | int skip_leading_underscores = 0; |
| 4545 | int pos, err; |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 4546 | |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 4547 | if (all == NULL) { |
| 4548 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 4549 | return -1; /* Unexpected error */ |
| 4550 | PyErr_Clear(); |
| 4551 | dict = PyObject_GetAttrString(v, "__dict__"); |
| 4552 | if (dict == NULL) { |
| 4553 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 4554 | return -1; |
| 4555 | PyErr_SetString(PyExc_ImportError, |
| 4556 | "from-import-* object has no __dict__ and no __all__"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4557 | return -1; |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 4558 | } |
| 4559 | all = PyMapping_Keys(dict); |
| 4560 | Py_DECREF(dict); |
| 4561 | if (all == NULL) |
| 4562 | return -1; |
| 4563 | skip_leading_underscores = 1; |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 4564 | } |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 4565 | |
| 4566 | for (pos = 0, err = 0; ; pos++) { |
| 4567 | name = PySequence_GetItem(all, pos); |
| 4568 | if (name == NULL) { |
| 4569 | if (!PyErr_ExceptionMatches(PyExc_IndexError)) |
| 4570 | err = -1; |
| 4571 | else |
| 4572 | PyErr_Clear(); |
| 4573 | break; |
| 4574 | } |
| 4575 | if (skip_leading_underscores && |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4576 | PyString_Check(name) && |
| 4577 | PyString_AS_STRING(name)[0] == '_') |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 4578 | { |
| 4579 | Py_DECREF(name); |
| 4580 | continue; |
| 4581 | } |
| 4582 | value = PyObject_GetAttr(v, name); |
| 4583 | if (value == NULL) |
| 4584 | err = -1; |
Armin Rigo | 7037085 | 2006-11-29 21:59:22 +0000 | [diff] [blame] | 4585 | else if (PyDict_CheckExact(locals)) |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 4586 | err = PyDict_SetItem(locals, name, value); |
Armin Rigo | 7037085 | 2006-11-29 21:59:22 +0000 | [diff] [blame] | 4587 | else |
| 4588 | err = PyObject_SetItem(locals, name, value); |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 4589 | Py_DECREF(name); |
| 4590 | Py_XDECREF(value); |
| 4591 | if (err != 0) |
| 4592 | break; |
| 4593 | } |
| 4594 | Py_DECREF(all); |
| 4595 | return err; |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 4596 | } |
| 4597 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4598 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4599 | build_class(PyObject *methods, PyObject *bases, PyObject *name) |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 4600 | { |
Guido van Rossum | 7851eea | 2001-09-12 19:19:18 +0000 | [diff] [blame] | 4601 | PyObject *metaclass = NULL, *result, *base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4602 | |
| 4603 | if (PyDict_Check(methods)) |
| 4604 | metaclass = PyDict_GetItemString(methods, "__metaclass__"); |
Guido van Rossum | 7851eea | 2001-09-12 19:19:18 +0000 | [diff] [blame] | 4605 | if (metaclass != NULL) |
Guido van Rossum | 2556f2e | 2001-12-06 14:09:56 +0000 | [diff] [blame] | 4606 | Py_INCREF(metaclass); |
Guido van Rossum | 7851eea | 2001-09-12 19:19:18 +0000 | [diff] [blame] | 4607 | else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) { |
| 4608 | base = PyTuple_GET_ITEM(bases, 0); |
| 4609 | metaclass = PyObject_GetAttrString(base, "__class__"); |
| 4610 | if (metaclass == NULL) { |
| 4611 | PyErr_Clear(); |
| 4612 | metaclass = (PyObject *)base->ob_type; |
| 4613 | Py_INCREF(metaclass); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 4614 | } |
| 4615 | } |
Guido van Rossum | 7851eea | 2001-09-12 19:19:18 +0000 | [diff] [blame] | 4616 | else { |
| 4617 | PyObject *g = PyEval_GetGlobals(); |
| 4618 | if (g != NULL && PyDict_Check(g)) |
| 4619 | metaclass = PyDict_GetItemString(g, "__metaclass__"); |
| 4620 | if (metaclass == NULL) |
| 4621 | metaclass = (PyObject *) &PyClass_Type; |
| 4622 | Py_INCREF(metaclass); |
| 4623 | } |
Jeremy Hylton | 7c1e347 | 2007-02-26 16:14:51 +0000 | [diff] [blame] | 4624 | result = PyObject_CallFunctionObjArgs(metaclass, name, bases, methods, |
Thomas Wouters | ae406c6 | 2007-09-19 17:27:43 +0000 | [diff] [blame] | 4625 | NULL); |
Guido van Rossum | 7851eea | 2001-09-12 19:19:18 +0000 | [diff] [blame] | 4626 | Py_DECREF(metaclass); |
Raymond Hettinger | f2c0830 | 2004-06-05 06:16:22 +0000 | [diff] [blame] | 4627 | if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) { |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 4628 | /* A type error here likely means that the user passed |
Raymond Hettinger | f2c0830 | 2004-06-05 06:16:22 +0000 | [diff] [blame] | 4629 | in a base that was not a class (such the random module |
| 4630 | instead of the random.random type). Help them out with |
Raymond Hettinger | cfc3192 | 2004-09-16 16:41:57 +0000 | [diff] [blame] | 4631 | by augmenting the error message with more information.*/ |
| 4632 | |
| 4633 | PyObject *ptype, *pvalue, *ptraceback; |
| 4634 | |
| 4635 | PyErr_Fetch(&ptype, &pvalue, &ptraceback); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4636 | if (PyString_Check(pvalue)) { |
Raymond Hettinger | cfc3192 | 2004-09-16 16:41:57 +0000 | [diff] [blame] | 4637 | PyObject *newmsg; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4638 | newmsg = PyString_FromFormat( |
Jeremy Hylton | 7c1e347 | 2007-02-26 16:14:51 +0000 | [diff] [blame] | 4639 | "Error when calling the metaclass bases\n" |
Thomas Wouters | ae406c6 | 2007-09-19 17:27:43 +0000 | [diff] [blame] | 4640 | " %s", |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4641 | PyString_AS_STRING(pvalue)); |
Raymond Hettinger | cfc3192 | 2004-09-16 16:41:57 +0000 | [diff] [blame] | 4642 | if (newmsg != NULL) { |
| 4643 | Py_DECREF(pvalue); |
| 4644 | pvalue = newmsg; |
| 4645 | } |
| 4646 | } |
| 4647 | PyErr_Restore(ptype, pvalue, ptraceback); |
Raymond Hettinger | f2c0830 | 2004-06-05 06:16:22 +0000 | [diff] [blame] | 4648 | } |
Guido van Rossum | 7851eea | 2001-09-12 19:19:18 +0000 | [diff] [blame] | 4649 | return result; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 4650 | } |
| 4651 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4652 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4653 | exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals, |
| 4654 | PyObject *locals) |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 4655 | { |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 4656 | int n; |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4657 | PyObject *v; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4658 | int plain = 0; |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 4659 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4660 | if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None && |
| 4661 | ((n = PyTuple_Size(prog)) == 2 || n == 3)) { |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 4662 | /* Backward compatibility hack */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4663 | globals = PyTuple_GetItem(prog, 1); |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 4664 | if (n == 3) |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4665 | locals = PyTuple_GetItem(prog, 2); |
| 4666 | prog = PyTuple_GetItem(prog, 0); |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 4667 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4668 | if (globals == Py_None) { |
| 4669 | globals = PyEval_GetGlobals(); |
| 4670 | if (locals == Py_None) { |
| 4671 | locals = PyEval_GetLocals(); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4672 | plain = 1; |
| 4673 | } |
Neal Norwitz | df6a649 | 2006-08-13 18:10:10 +0000 | [diff] [blame] | 4674 | if (!globals || !locals) { |
| 4675 | PyErr_SetString(PyExc_SystemError, |
| 4676 | "globals and locals cannot be NULL"); |
| 4677 | return -1; |
| 4678 | } |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 4679 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4680 | else if (locals == Py_None) |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 4681 | locals = globals; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4682 | if (!PyString_Check(prog) && |
Benjamin Peterson | 78821dd | 2009-01-25 17:15:10 +0000 | [diff] [blame] | 4683 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 4684 | !PyUnicode_Check(prog) && |
Benjamin Peterson | 78821dd | 2009-01-25 17:15:10 +0000 | [diff] [blame] | 4685 | #endif |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4686 | !PyCode_Check(prog) && |
| 4687 | !PyFile_Check(prog)) { |
| 4688 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4689 | "exec: arg 1 must be a string, file, or code object"); |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 4690 | return -1; |
| 4691 | } |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4692 | if (!PyDict_Check(globals)) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4693 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4694 | "exec: arg 2 must be a dictionary or None"); |
| 4695 | return -1; |
| 4696 | } |
Raymond Hettinger | 66bd233 | 2004-08-02 08:30:07 +0000 | [diff] [blame] | 4697 | if (!PyMapping_Check(locals)) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4698 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 66bd233 | 2004-08-02 08:30:07 +0000 | [diff] [blame] | 4699 | "exec: arg 3 must be a mapping or None"); |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 4700 | return -1; |
| 4701 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4702 | if (PyDict_GetItemString(globals, "__builtins__") == NULL) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 4703 | PyDict_SetItemString(globals, "__builtins__", f->f_builtins); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4704 | if (PyCode_Check(prog)) { |
Jeremy Hylton | 733c893 | 2001-12-13 19:51:56 +0000 | [diff] [blame] | 4705 | if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) { |
| 4706 | PyErr_SetString(PyExc_TypeError, |
| 4707 | "code object passed to exec may not contain free variables"); |
| 4708 | return -1; |
| 4709 | } |
Guido van Rossum | a400d8a | 2000-01-12 22:45:54 +0000 | [diff] [blame] | 4710 | v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals); |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 4711 | } |
Guido van Rossum | a400d8a | 2000-01-12 22:45:54 +0000 | [diff] [blame] | 4712 | else if (PyFile_Check(prog)) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4713 | FILE *fp = PyFile_AsFile(prog); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4714 | char *name = PyString_AsString(PyFile_Name(prog)); |
Jeremy Hylton | 714b112 | 2007-02-25 16:01:58 +0000 | [diff] [blame] | 4715 | PyCompilerFlags cf; |
Thomas Wouters | ae406c6 | 2007-09-19 17:27:43 +0000 | [diff] [blame] | 4716 | if (name == NULL) |
| 4717 | return -1; |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 4718 | cf.cf_flags = 0; |
| 4719 | if (PyEval_MergeCompilerFlags(&cf)) |
Jeremy Hylton | bc32024 | 2001-03-22 02:47:58 +0000 | [diff] [blame] | 4720 | v = PyRun_FileFlags(fp, name, Py_file_input, globals, |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 4721 | locals, &cf); |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 4722 | else |
Jeremy Hylton | bc32024 | 2001-03-22 02:47:58 +0000 | [diff] [blame] | 4723 | v = PyRun_File(fp, name, Py_file_input, globals, |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 4724 | locals); |
Guido van Rossum | a400d8a | 2000-01-12 22:45:54 +0000 | [diff] [blame] | 4725 | } |
| 4726 | else { |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 4727 | PyObject *tmp = NULL; |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 4728 | char *str; |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 4729 | PyCompilerFlags cf; |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 4730 | cf.cf_flags = 0; |
| 4731 | #ifdef Py_USING_UNICODE |
| 4732 | if (PyUnicode_Check(prog)) { |
| 4733 | tmp = PyUnicode_AsUTF8String(prog); |
| 4734 | if (tmp == NULL) |
| 4735 | return -1; |
| 4736 | prog = tmp; |
| 4737 | cf.cf_flags |= PyCF_SOURCE_IS_UTF8; |
| 4738 | } |
| 4739 | #endif |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4740 | if (PyString_AsStringAndSize(prog, &str, NULL)) |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 4741 | return -1; |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 4742 | if (PyEval_MergeCompilerFlags(&cf)) |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 4743 | v = PyRun_StringFlags(str, Py_file_input, globals, |
Jeremy Hylton | bc32024 | 2001-03-22 02:47:58 +0000 | [diff] [blame] | 4744 | locals, &cf); |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 4745 | else |
Jeremy Hylton | bc32024 | 2001-03-22 02:47:58 +0000 | [diff] [blame] | 4746 | v = PyRun_String(str, Py_file_input, globals, locals); |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 4747 | Py_XDECREF(tmp); |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 4748 | } |
Guido van Rossum | a400d8a | 2000-01-12 22:45:54 +0000 | [diff] [blame] | 4749 | if (plain) |
| 4750 | PyFrame_LocalsToFast(f, 0); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4751 | if (v == NULL) |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 4752 | return -1; |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4753 | Py_DECREF(v); |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 4754 | return 0; |
| 4755 | } |
Guido van Rossum | 24c1374 | 1995-02-14 09:42:43 +0000 | [diff] [blame] | 4756 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4757 | static void |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 4758 | format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj) |
| 4759 | { |
| 4760 | char *obj_str; |
| 4761 | |
| 4762 | if (!obj) |
| 4763 | return; |
| 4764 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4765 | obj_str = PyString_AsString(obj); |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 4766 | if (!obj_str) |
| 4767 | return; |
| 4768 | |
| 4769 | PyErr_Format(exc, format_str, obj_str); |
| 4770 | } |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 4771 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4772 | static PyObject * |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 4773 | string_concatenate(PyObject *v, PyObject *w, |
| 4774 | PyFrameObject *f, unsigned char *next_instr) |
| 4775 | { |
| 4776 | /* This function implements 'variable += expr' when both arguments |
| 4777 | are strings. */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4778 | Py_ssize_t v_len = PyString_GET_SIZE(v); |
| 4779 | Py_ssize_t w_len = PyString_GET_SIZE(w); |
Armin Rigo | 97ff047 | 2006-08-09 15:37:26 +0000 | [diff] [blame] | 4780 | Py_ssize_t new_len = v_len + w_len; |
| 4781 | if (new_len < 0) { |
| 4782 | PyErr_SetString(PyExc_OverflowError, |
| 4783 | "strings are too large to concat"); |
| 4784 | return NULL; |
| 4785 | } |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 4786 | |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 4787 | if (v->ob_refcnt == 2) { |
| 4788 | /* In the common case, there are 2 references to the value |
| 4789 | * stored in 'variable' when the += is performed: one on the |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 4790 | * value stack (in 'v') and one still stored in the |
| 4791 | * 'variable'. We try to delete the variable now to reduce |
| 4792 | * the refcnt to 1. |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 4793 | */ |
| 4794 | switch (*next_instr) { |
| 4795 | case STORE_FAST: |
| 4796 | { |
| 4797 | int oparg = PEEKARG(); |
| 4798 | PyObject **fastlocals = f->f_localsplus; |
| 4799 | if (GETLOCAL(oparg) == v) |
| 4800 | SETLOCAL(oparg, NULL); |
| 4801 | break; |
| 4802 | } |
| 4803 | case STORE_DEREF: |
| 4804 | { |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 4805 | PyObject **freevars = (f->f_localsplus + |
| 4806 | f->f_code->co_nlocals); |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 4807 | PyObject *c = freevars[PEEKARG()]; |
| 4808 | if (PyCell_GET(c) == v) |
| 4809 | PyCell_Set(c, NULL); |
| 4810 | break; |
| 4811 | } |
| 4812 | case STORE_NAME: |
| 4813 | { |
| 4814 | PyObject *names = f->f_code->co_names; |
| 4815 | PyObject *name = GETITEM(names, PEEKARG()); |
| 4816 | PyObject *locals = f->f_locals; |
| 4817 | if (PyDict_CheckExact(locals) && |
| 4818 | PyDict_GetItem(locals, name) == v) { |
| 4819 | if (PyDict_DelItem(locals, name) != 0) { |
| 4820 | PyErr_Clear(); |
| 4821 | } |
| 4822 | } |
| 4823 | break; |
| 4824 | } |
| 4825 | } |
| 4826 | } |
| 4827 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4828 | if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) { |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 4829 | /* Now we own the last reference to 'v', so we can resize it |
| 4830 | * in-place. |
| 4831 | */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4832 | if (_PyString_Resize(&v, new_len) != 0) { |
| 4833 | /* XXX if _PyString_Resize() fails, 'v' has been |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 4834 | * deallocated so it cannot be put back into |
| 4835 | * 'variable'. The MemoryError is raised when there |
| 4836 | * is no value in 'variable', which might (very |
| 4837 | * remotely) be a cause of incompatibilities. |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 4838 | */ |
| 4839 | return NULL; |
| 4840 | } |
| 4841 | /* copy 'w' into the newly allocated area of 'v' */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4842 | memcpy(PyString_AS_STRING(v) + v_len, |
| 4843 | PyString_AS_STRING(w), w_len); |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 4844 | return v; |
| 4845 | } |
| 4846 | else { |
| 4847 | /* When in-place resizing is not an option. */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4848 | PyString_Concat(&v, w); |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 4849 | return v; |
| 4850 | } |
| 4851 | } |
| 4852 | |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 4853 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 4854 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4855 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4856 | getarray(long a[256]) |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 4857 | { |
| 4858 | int i; |
| 4859 | PyObject *l = PyList_New(256); |
| 4860 | if (l == NULL) return NULL; |
| 4861 | for (i = 0; i < 256; i++) { |
| 4862 | PyObject *x = PyInt_FromLong(a[i]); |
| 4863 | if (x == NULL) { |
| 4864 | Py_DECREF(l); |
| 4865 | return NULL; |
| 4866 | } |
| 4867 | PyList_SetItem(l, i, x); |
| 4868 | } |
| 4869 | for (i = 0; i < 256; i++) |
| 4870 | a[i] = 0; |
| 4871 | return l; |
| 4872 | } |
| 4873 | |
| 4874 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4875 | _Py_GetDXProfile(PyObject *self, PyObject *args) |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 4876 | { |
| 4877 | #ifndef DXPAIRS |
| 4878 | return getarray(dxp); |
| 4879 | #else |
| 4880 | int i; |
| 4881 | PyObject *l = PyList_New(257); |
| 4882 | if (l == NULL) return NULL; |
| 4883 | for (i = 0; i < 257; i++) { |
| 4884 | PyObject *x = getarray(dxpairs[i]); |
| 4885 | if (x == NULL) { |
| 4886 | Py_DECREF(l); |
| 4887 | return NULL; |
| 4888 | } |
| 4889 | PyList_SetItem(l, i, x); |
| 4890 | } |
| 4891 | return l; |
| 4892 | #endif |
| 4893 | } |
| 4894 | |
| 4895 | #endif |