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