Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2 | /* Execute compiled code */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4 | /* XXX TO DO: |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 5 | XXX speed up searching for keywords by using a dictionary |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 6 | XXX document it! |
| 7 | */ |
| 8 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 9 | /* enable more aggressive intra-module optimizations, where available */ |
Fredrik Lundh | 57640f5 | 2006-05-26 11:54:04 +0000 | [diff] [blame] | 10 | #define PY_LOCAL_AGGRESSIVE |
| 11 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 12 | #include "Python.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 13 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 14 | #include "code.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 15 | #include "frameobject.h" |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 16 | #include "eval.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 17 | #include "opcode.h" |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 18 | #include "structmember.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 19 | |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 20 | #include <ctype.h> |
| 21 | |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 22 | #ifndef WITH_TSC |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 23 | |
| 24 | #define READ_TIMESTAMP(var) |
| 25 | |
| 26 | #else |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 27 | |
| 28 | typedef unsigned long long uint64; |
| 29 | |
Michael W. Hudson | 800ba23 | 2004-08-12 18:19:17 +0000 | [diff] [blame] | 30 | #if defined(__ppc__) /* <- Don't know if this is the correct symbol; this |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 31 | section should work for GCC on any PowerPC |
| 32 | platform, irrespective of OS. |
| 33 | POWER? Who knows :-) */ |
Michael W. Hudson | 800ba23 | 2004-08-12 18:19:17 +0000 | [diff] [blame] | 34 | |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 35 | #define READ_TIMESTAMP(var) ppc_getcounter(&var) |
Michael W. Hudson | 800ba23 | 2004-08-12 18:19:17 +0000 | [diff] [blame] | 36 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 37 | static void |
Michael W. Hudson | 800ba23 | 2004-08-12 18:19:17 +0000 | [diff] [blame] | 38 | ppc_getcounter(uint64 *v) |
| 39 | { |
| 40 | register unsigned long tbu, tb, tbu2; |
| 41 | |
| 42 | loop: |
| 43 | asm volatile ("mftbu %0" : "=r" (tbu) ); |
| 44 | asm volatile ("mftb %0" : "=r" (tb) ); |
| 45 | asm volatile ("mftbu %0" : "=r" (tbu2)); |
| 46 | if (__builtin_expect(tbu != tbu2, 0)) goto loop; |
| 47 | |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 48 | /* The slightly peculiar way of writing the next lines is |
Michael W. Hudson | 800ba23 | 2004-08-12 18:19:17 +0000 | [diff] [blame] | 49 | compiled better by GCC than any other way I tried. */ |
| 50 | ((long*)(v))[0] = tbu; |
| 51 | ((long*)(v))[1] = tb; |
| 52 | } |
| 53 | |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 54 | #else /* this is for linux/x86 (and probably any other GCC/x86 combo) */ |
Michael W. Hudson | 800ba23 | 2004-08-12 18:19:17 +0000 | [diff] [blame] | 55 | |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 56 | #define READ_TIMESTAMP(val) \ |
| 57 | __asm__ __volatile__("rdtsc" : "=A" (val)) |
Michael W. Hudson | 800ba23 | 2004-08-12 18:19:17 +0000 | [diff] [blame] | 58 | |
| 59 | #endif |
| 60 | |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 61 | void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1, |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 62 | uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1) |
| 63 | { |
| 64 | uint64 intr, inst, loop; |
| 65 | PyThreadState *tstate = PyThreadState_Get(); |
| 66 | if (!tstate->interp->tscdump) |
| 67 | return; |
| 68 | intr = intr1 - intr0; |
| 69 | inst = inst1 - inst0 - intr; |
| 70 | loop = loop1 - loop0 - intr; |
| 71 | fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n", |
| 72 | opcode, ticked, inst, loop); |
| 73 | } |
Michael W. Hudson | 800ba23 | 2004-08-12 18:19:17 +0000 | [diff] [blame] | 74 | |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 75 | #endif |
| 76 | |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 77 | /* Turn this on if your compiler chokes on the big switch: */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 78 | /* #define CASE_TOO_BIG 1 */ |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 79 | |
Guido van Rossum | 408027e | 1996-12-30 16:17:54 +0000 | [diff] [blame] | 80 | #ifdef Py_DEBUG |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 81 | /* For debugging the interpreter: */ |
| 82 | #define LLTRACE 1 /* Low-level trace feature */ |
| 83 | #define CHECKEXC 1 /* Double-check exception checking */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 84 | #endif |
| 85 | |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 86 | typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *); |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 87 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 88 | /* Forward declarations */ |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 89 | #ifdef WITH_TSC |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 90 | static PyObject * call_function(PyObject ***, int, uint64*, uint64*); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 91 | #else |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 92 | static PyObject * call_function(PyObject ***, int); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 93 | #endif |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 94 | static PyObject * fast_function(PyObject *, PyObject ***, int, int, int); |
| 95 | static PyObject * do_call(PyObject *, PyObject ***, int, int); |
| 96 | static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int); |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 97 | static PyObject * update_keyword_args(PyObject *, int, PyObject ***, |
| 98 | PyObject *); |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 99 | static PyObject * update_star_args(int, int, PyObject *, PyObject ***); |
| 100 | static PyObject * load_args(PyObject ***, int); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 101 | #define CALL_FLAG_VAR 1 |
| 102 | #define CALL_FLAG_KW 2 |
| 103 | |
Guido van Rossum | 0a066c0 | 1992-03-27 17:29:15 +0000 | [diff] [blame] | 104 | #ifdef LLTRACE |
Fredrik Lundh | 1b94940 | 2006-05-26 12:01:49 +0000 | [diff] [blame] | 105 | static int lltrace; |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 106 | static int prtrace(PyObject *, char *); |
Guido van Rossum | 0a066c0 | 1992-03-27 17:29:15 +0000 | [diff] [blame] | 107 | #endif |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 108 | static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *, |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 109 | int, PyObject *); |
Amaury Forgeot d'Arc | 0d75f09 | 2007-11-13 21:54:28 +0000 | [diff] [blame] | 110 | static int call_trace_protected(Py_tracefunc, PyObject *, |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 111 | PyFrameObject *, int, PyObject *); |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 112 | static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *); |
| 113 | static int maybe_call_line_trace(Py_tracefunc, PyObject *, |
Armin Rigo | bf57a14 | 2004-03-22 19:24:58 +0000 | [diff] [blame] | 114 | PyFrameObject *, int *, int *, int *); |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 115 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 116 | static PyObject * apply_slice(PyObject *, PyObject *, PyObject *); |
| 117 | static int assign_slice(PyObject *, PyObject *, |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 118 | PyObject *, PyObject *); |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 119 | static PyObject * cmp_outcome(int, PyObject *, PyObject *); |
| 120 | static PyObject * import_from(PyObject *, PyObject *); |
| 121 | static int import_all_from(PyObject *, PyObject *); |
| 122 | static PyObject * build_class(PyObject *, PyObject *, PyObject *); |
| 123 | static int exec_statement(PyFrameObject *, |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 124 | PyObject *, PyObject *, PyObject *); |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 125 | static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *); |
| 126 | static void reset_exc_info(PyThreadState *); |
| 127 | static void format_exc_check_arg(PyObject *, char *, PyObject *); |
| 128 | static PyObject * string_concatenate(PyObject *, PyObject *, |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 129 | PyFrameObject *, unsigned char *); |
Benjamin Peterson | e18ef19 | 2009-01-20 14:21:16 +0000 | [diff] [blame] | 130 | static PyObject * kwd_as_string(PyObject *); |
Benjamin Peterson | 1880d8b | 2009-05-25 13:13:44 +0000 | [diff] [blame] | 131 | static PyObject * special_lookup(PyObject *, char *, PyObject **); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 132 | |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 133 | #define NAME_ERROR_MSG \ |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 134 | "name '%.200s' is not defined" |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 135 | #define GLOBAL_NAME_ERROR_MSG \ |
| 136 | "global name '%.200s' is not defined" |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 137 | #define UNBOUNDLOCAL_ERROR_MSG \ |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 138 | "local variable '%.200s' referenced before assignment" |
Jeremy Hylton | c76770c | 2001-04-13 16:51:46 +0000 | [diff] [blame] | 139 | #define UNBOUNDFREE_ERROR_MSG \ |
| 140 | "free variable '%.200s' referenced before assignment" \ |
| 141 | " in enclosing scope" |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 142 | |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 143 | /* Dynamic execution profile */ |
| 144 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 145 | #ifdef DXPAIRS |
| 146 | static long dxpairs[257][256]; |
| 147 | #define dxp dxpairs[256] |
| 148 | #else |
| 149 | static long dxp[256]; |
| 150 | #endif |
| 151 | #endif |
| 152 | |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 153 | /* Function call profile */ |
| 154 | #ifdef CALL_PROFILE |
| 155 | #define PCALL_NUM 11 |
| 156 | static int pcall[PCALL_NUM]; |
| 157 | |
| 158 | #define PCALL_ALL 0 |
| 159 | #define PCALL_FUNCTION 1 |
| 160 | #define PCALL_FAST_FUNCTION 2 |
| 161 | #define PCALL_FASTER_FUNCTION 3 |
| 162 | #define PCALL_METHOD 4 |
| 163 | #define PCALL_BOUND_METHOD 5 |
| 164 | #define PCALL_CFUNCTION 6 |
| 165 | #define PCALL_TYPE 7 |
| 166 | #define PCALL_GENERATOR 8 |
| 167 | #define PCALL_OTHER 9 |
| 168 | #define PCALL_POP 10 |
| 169 | |
| 170 | /* Notes about the statistics |
| 171 | |
| 172 | PCALL_FAST stats |
| 173 | |
| 174 | FAST_FUNCTION means no argument tuple needs to be created. |
| 175 | FASTER_FUNCTION means that the fast-path frame setup code is used. |
| 176 | |
| 177 | If there is a method call where the call can be optimized by changing |
| 178 | the argument tuple and calling the function directly, it gets recorded |
| 179 | twice. |
| 180 | |
| 181 | As a result, the relationship among the statistics appears to be |
| 182 | PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD + |
| 183 | PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER |
| 184 | PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION |
| 185 | PCALL_METHOD > PCALL_BOUND_METHOD |
| 186 | */ |
| 187 | |
| 188 | #define PCALL(POS) pcall[POS]++ |
| 189 | |
| 190 | PyObject * |
| 191 | PyEval_GetCallStats(PyObject *self) |
| 192 | { |
Andrew M. Kuchling | 1f3ebe0 | 2006-10-27 13:22:46 +0000 | [diff] [blame] | 193 | return Py_BuildValue("iiiiiiiiiii", |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 194 | pcall[0], pcall[1], pcall[2], pcall[3], |
| 195 | pcall[4], pcall[5], pcall[6], pcall[7], |
Andrew M. Kuchling | 1f3ebe0 | 2006-10-27 13:22:46 +0000 | [diff] [blame] | 196 | pcall[8], pcall[9], pcall[10]); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 197 | } |
| 198 | #else |
| 199 | #define PCALL(O) |
| 200 | |
| 201 | PyObject * |
| 202 | PyEval_GetCallStats(PyObject *self) |
| 203 | { |
| 204 | Py_INCREF(Py_None); |
| 205 | return Py_None; |
| 206 | } |
| 207 | #endif |
| 208 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 209 | |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 210 | #ifdef WITH_THREAD |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 211 | |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 212 | #ifdef HAVE_ERRNO_H |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 213 | #include <errno.h> |
Guido van Rossum | 2571cc8 | 1999-04-07 16:07:23 +0000 | [diff] [blame] | 214 | #endif |
Guido van Rossum | 49b5606 | 1998-10-01 20:42:43 +0000 | [diff] [blame] | 215 | #include "pythread.h" |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 216 | |
Guido van Rossum | b8b6d0c | 2003-06-28 21:53:52 +0000 | [diff] [blame] | 217 | static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */ |
Kristján Valur Jónsson | 0e91938 | 2009-01-09 20:31:26 +0000 | [diff] [blame] | 218 | static PyThread_type_lock pending_lock = 0; /* for pending calls */ |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 219 | static long main_thread = 0; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 220 | |
Tim Peters | 7f468f2 | 2004-10-11 02:40:51 +0000 | [diff] [blame] | 221 | int |
| 222 | PyEval_ThreadsInitialized(void) |
| 223 | { |
| 224 | return interpreter_lock != 0; |
| 225 | } |
| 226 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 227 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 228 | PyEval_InitThreads(void) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 229 | { |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 230 | if (interpreter_lock) |
Sjoerd Mullender | ed59d20 | 1993-01-06 13:36:38 +0000 | [diff] [blame] | 231 | return; |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 232 | interpreter_lock = PyThread_allocate_lock(); |
| 233 | PyThread_acquire_lock(interpreter_lock, 1); |
| 234 | main_thread = PyThread_get_thread_ident(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 235 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 236 | |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 237 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 238 | PyEval_AcquireLock(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 239 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 240 | PyThread_acquire_lock(interpreter_lock, 1); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 244 | PyEval_ReleaseLock(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 245 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 246 | PyThread_release_lock(interpreter_lock); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 250 | PyEval_AcquireThread(PyThreadState *tstate) |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 251 | { |
| 252 | if (tstate == NULL) |
| 253 | Py_FatalError("PyEval_AcquireThread: NULL new thread state"); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 254 | /* Check someone has called PyEval_InitThreads() to create the lock */ |
| 255 | assert(interpreter_lock); |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 256 | PyThread_acquire_lock(interpreter_lock, 1); |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 257 | if (PyThreadState_Swap(tstate) != NULL) |
| 258 | Py_FatalError( |
| 259 | "PyEval_AcquireThread: non-NULL old thread state"); |
| 260 | } |
| 261 | |
| 262 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 263 | PyEval_ReleaseThread(PyThreadState *tstate) |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 264 | { |
| 265 | if (tstate == NULL) |
| 266 | Py_FatalError("PyEval_ReleaseThread: NULL thread state"); |
| 267 | if (PyThreadState_Swap(NULL) != tstate) |
| 268 | Py_FatalError("PyEval_ReleaseThread: wrong thread state"); |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 269 | PyThread_release_lock(interpreter_lock); |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 270 | } |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 271 | |
| 272 | /* This function is called from PyOS_AfterFork to ensure that newly |
| 273 | created child processes don't hold locks referring to threads which |
| 274 | are not running in the child process. (This could also be done using |
| 275 | pthread_atfork mechanism, at least for the pthreads implementation.) */ |
| 276 | |
| 277 | void |
| 278 | PyEval_ReInitThreads(void) |
| 279 | { |
Jesse Noller | 5e62ca4 | 2008-07-16 20:03:47 +0000 | [diff] [blame] | 280 | PyObject *threading, *result; |
| 281 | PyThreadState *tstate; |
| 282 | |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 283 | if (!interpreter_lock) |
| 284 | return; |
| 285 | /*XXX Can't use PyThread_free_lock here because it does too |
| 286 | much error-checking. Doing this cleanly would require |
| 287 | adding a new function to each thread_*.h. Instead, just |
| 288 | create a new lock and waste a little bit of memory */ |
| 289 | interpreter_lock = PyThread_allocate_lock(); |
Kristján Valur Jónsson | 0e91938 | 2009-01-09 20:31:26 +0000 | [diff] [blame] | 290 | pending_lock = PyThread_allocate_lock(); |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 291 | PyThread_acquire_lock(interpreter_lock, 1); |
| 292 | main_thread = PyThread_get_thread_ident(); |
Jesse Noller | 5e62ca4 | 2008-07-16 20:03:47 +0000 | [diff] [blame] | 293 | |
| 294 | /* Update the threading module with the new state. |
| 295 | */ |
| 296 | tstate = PyThreadState_GET(); |
| 297 | threading = PyMapping_GetItemString(tstate->interp->modules, |
| 298 | "threading"); |
| 299 | if (threading == NULL) { |
| 300 | /* threading not imported */ |
| 301 | PyErr_Clear(); |
| 302 | return; |
| 303 | } |
| 304 | result = PyObject_CallMethod(threading, "_after_fork", NULL); |
| 305 | if (result == NULL) |
| 306 | PyErr_WriteUnraisable(threading); |
| 307 | else |
| 308 | Py_DECREF(result); |
| 309 | Py_DECREF(threading); |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 310 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 311 | #endif |
| 312 | |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 313 | /* Functions save_thread and restore_thread are always defined so |
| 314 | dynamically loaded modules needn't be compiled separately for use |
| 315 | with and without threads: */ |
| 316 | |
Guido van Rossum | 2fca21f7 | 1997-07-18 23:56:58 +0000 | [diff] [blame] | 317 | PyThreadState * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 318 | PyEval_SaveThread(void) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 319 | { |
Guido van Rossum | b74eca9 | 1997-09-30 22:03:16 +0000 | [diff] [blame] | 320 | PyThreadState *tstate = PyThreadState_Swap(NULL); |
| 321 | if (tstate == NULL) |
| 322 | Py_FatalError("PyEval_SaveThread: NULL tstate"); |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 323 | #ifdef WITH_THREAD |
Guido van Rossum | b74eca9 | 1997-09-30 22:03:16 +0000 | [diff] [blame] | 324 | if (interpreter_lock) |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 325 | PyThread_release_lock(interpreter_lock); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 326 | #endif |
Guido van Rossum | b74eca9 | 1997-09-30 22:03:16 +0000 | [diff] [blame] | 327 | return tstate; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 331 | PyEval_RestoreThread(PyThreadState *tstate) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 332 | { |
Guido van Rossum | b74eca9 | 1997-09-30 22:03:16 +0000 | [diff] [blame] | 333 | if (tstate == NULL) |
| 334 | Py_FatalError("PyEval_RestoreThread: NULL tstate"); |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 335 | #ifdef WITH_THREAD |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 336 | if (interpreter_lock) { |
Guido van Rossum | b74eca9 | 1997-09-30 22:03:16 +0000 | [diff] [blame] | 337 | int err = errno; |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 338 | PyThread_acquire_lock(interpreter_lock, 1); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 339 | errno = err; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 340 | } |
| 341 | #endif |
Guido van Rossum | b74eca9 | 1997-09-30 22:03:16 +0000 | [diff] [blame] | 342 | PyThreadState_Swap(tstate); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 346 | /* Mechanism whereby asynchronously executing callbacks (e.g. UNIX |
| 347 | signal handlers or Mac I/O completion routines) can schedule calls |
| 348 | to a function to be called synchronously. |
| 349 | The synchronous function is called with one void* argument. |
| 350 | It should return 0 for success or -1 for failure -- failure should |
| 351 | be accompanied by an exception. |
| 352 | |
| 353 | If registry succeeds, the registry function returns 0; if it fails |
| 354 | (e.g. due to too many pending calls) it returns -1 (without setting |
| 355 | an exception condition). |
| 356 | |
| 357 | Note that because registry may occur from within signal handlers, |
| 358 | or other asynchronous events, calling malloc() is unsafe! |
| 359 | |
| 360 | #ifdef WITH_THREAD |
| 361 | Any thread can schedule pending calls, but only the main thread |
| 362 | will execute them. |
Kristján Valur Jónsson | 0e91938 | 2009-01-09 20:31:26 +0000 | [diff] [blame] | 363 | There is no facility to schedule calls to a particular thread, but |
| 364 | that should be easy to change, should that ever be required. In |
| 365 | that case, the static variables here should go into the python |
| 366 | threadstate. |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 367 | #endif |
Kristján Valur Jónsson | 0e91938 | 2009-01-09 20:31:26 +0000 | [diff] [blame] | 368 | */ |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 369 | |
Kristján Valur Jónsson | 0e91938 | 2009-01-09 20:31:26 +0000 | [diff] [blame] | 370 | #ifdef WITH_THREAD |
| 371 | |
| 372 | /* The WITH_THREAD implementation is thread-safe. It allows |
| 373 | scheduling to be made from any thread, and even from an executing |
| 374 | callback. |
| 375 | */ |
| 376 | |
| 377 | #define NPENDINGCALLS 32 |
| 378 | static struct { |
| 379 | int (*func)(void *); |
| 380 | void *arg; |
| 381 | } pendingcalls[NPENDINGCALLS]; |
| 382 | static int pendingfirst = 0; |
| 383 | static int pendinglast = 0; |
| 384 | static volatile int pendingcalls_to_do = 1; /* trigger initialization of lock */ |
| 385 | static char pendingbusy = 0; |
| 386 | |
| 387 | int |
| 388 | Py_AddPendingCall(int (*func)(void *), void *arg) |
| 389 | { |
| 390 | int i, j, result=0; |
| 391 | PyThread_type_lock lock = pending_lock; |
| 392 | |
| 393 | /* try a few times for the lock. Since this mechanism is used |
| 394 | * for signal handling (on the main thread), there is a (slim) |
| 395 | * chance that a signal is delivered on the same thread while we |
| 396 | * hold the lock during the Py_MakePendingCalls() function. |
| 397 | * This avoids a deadlock in that case. |
| 398 | * Note that signals can be delivered on any thread. In particular, |
| 399 | * on Windows, a SIGINT is delivered on a system-created worker |
| 400 | * thread. |
| 401 | * We also check for lock being NULL, in the unlikely case that |
| 402 | * this function is called before any bytecode evaluation takes place. |
| 403 | */ |
| 404 | if (lock != NULL) { |
| 405 | for (i = 0; i<100; i++) { |
| 406 | if (PyThread_acquire_lock(lock, NOWAIT_LOCK)) |
| 407 | break; |
| 408 | } |
| 409 | if (i == 100) |
| 410 | return -1; |
| 411 | } |
| 412 | |
| 413 | i = pendinglast; |
| 414 | j = (i + 1) % NPENDINGCALLS; |
| 415 | if (j == pendingfirst) { |
| 416 | result = -1; /* Queue full */ |
| 417 | } else { |
| 418 | pendingcalls[i].func = func; |
| 419 | pendingcalls[i].arg = arg; |
| 420 | pendinglast = j; |
| 421 | } |
| 422 | /* signal main loop */ |
| 423 | _Py_Ticker = 0; |
| 424 | pendingcalls_to_do = 1; |
| 425 | if (lock != NULL) |
| 426 | PyThread_release_lock(lock); |
| 427 | return result; |
| 428 | } |
| 429 | |
| 430 | int |
| 431 | Py_MakePendingCalls(void) |
| 432 | { |
| 433 | int i; |
| 434 | int r = 0; |
| 435 | |
| 436 | if (!pending_lock) { |
| 437 | /* initial allocation of the lock */ |
| 438 | pending_lock = PyThread_allocate_lock(); |
| 439 | if (pending_lock == NULL) |
| 440 | return -1; |
| 441 | } |
| 442 | |
| 443 | /* only service pending calls on main thread */ |
| 444 | if (main_thread && PyThread_get_thread_ident() != main_thread) |
| 445 | return 0; |
| 446 | /* don't perform recursive pending calls */ |
| 447 | if (pendingbusy) |
| 448 | return 0; |
| 449 | pendingbusy = 1; |
| 450 | /* perform a bounded number of calls, in case of recursion */ |
| 451 | for (i=0; i<NPENDINGCALLS; i++) { |
| 452 | int j; |
| 453 | int (*func)(void *); |
Mark Dickinson | 9e58a37 | 2009-02-08 17:33:11 +0000 | [diff] [blame] | 454 | void *arg = NULL; |
Kristján Valur Jónsson | 0e91938 | 2009-01-09 20:31:26 +0000 | [diff] [blame] | 455 | |
| 456 | /* pop one item off the queue while holding the lock */ |
| 457 | PyThread_acquire_lock(pending_lock, WAIT_LOCK); |
| 458 | j = pendingfirst; |
| 459 | if (j == pendinglast) { |
| 460 | func = NULL; /* Queue empty */ |
| 461 | } else { |
| 462 | func = pendingcalls[j].func; |
| 463 | arg = pendingcalls[j].arg; |
| 464 | pendingfirst = (j + 1) % NPENDINGCALLS; |
| 465 | } |
| 466 | pendingcalls_to_do = pendingfirst != pendinglast; |
| 467 | PyThread_release_lock(pending_lock); |
| 468 | /* having released the lock, perform the callback */ |
| 469 | if (func == NULL) |
| 470 | break; |
| 471 | r = func(arg); |
| 472 | if (r) |
| 473 | break; |
| 474 | } |
| 475 | pendingbusy = 0; |
| 476 | return r; |
| 477 | } |
| 478 | |
| 479 | #else /* if ! defined WITH_THREAD */ |
| 480 | |
| 481 | /* |
| 482 | WARNING! ASYNCHRONOUSLY EXECUTING CODE! |
| 483 | This code is used for signal handling in python that isn't built |
| 484 | with WITH_THREAD. |
| 485 | Don't use this implementation when Py_AddPendingCalls() can happen |
| 486 | on a different thread! |
| 487 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 488 | There are two possible race conditions: |
Kristján Valur Jónsson | 0e91938 | 2009-01-09 20:31:26 +0000 | [diff] [blame] | 489 | (1) nested asynchronous calls to Py_AddPendingCall() |
| 490 | (2) AddPendingCall() calls made while pending calls are being processed. |
| 491 | |
| 492 | (1) is very unlikely because typically signal delivery |
| 493 | is blocked during signal handling. So it should be impossible. |
| 494 | (2) is a real possibility. |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 495 | The current code is safe against (2), but not against (1). |
| 496 | The safety against (2) is derived from the fact that only one |
Kristján Valur Jónsson | 0e91938 | 2009-01-09 20:31:26 +0000 | [diff] [blame] | 497 | thread is present, interrupted by signals, and that the critical |
| 498 | section is protected with the "busy" variable. On Windows, which |
| 499 | delivers SIGINT on a system thread, this does not hold and therefore |
| 500 | Windows really shouldn't use this version. |
| 501 | The two threads could theoretically wiggle around the "busy" variable. |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 502 | */ |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 503 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 504 | #define NPENDINGCALLS 32 |
| 505 | static struct { |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 506 | int (*func)(void *); |
| 507 | void *arg; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 508 | } pendingcalls[NPENDINGCALLS]; |
| 509 | static volatile int pendingfirst = 0; |
| 510 | static volatile int pendinglast = 0; |
Kristján Valur Jónsson | 0e91938 | 2009-01-09 20:31:26 +0000 | [diff] [blame] | 511 | static volatile int pendingcalls_to_do = 0; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 512 | |
| 513 | int |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 514 | Py_AddPendingCall(int (*func)(void *), void *arg) |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 515 | { |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 516 | static volatile int busy = 0; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 517 | int i, j; |
| 518 | /* XXX Begin critical section */ |
Guido van Rossum | 180d7b4 | 1994-09-29 09:45:57 +0000 | [diff] [blame] | 519 | if (busy) |
| 520 | return -1; |
| 521 | busy = 1; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 522 | i = pendinglast; |
| 523 | j = (i + 1) % NPENDINGCALLS; |
Guido van Rossum | 04e7032 | 2002-07-17 16:57:13 +0000 | [diff] [blame] | 524 | if (j == pendingfirst) { |
| 525 | busy = 0; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 526 | return -1; /* Queue full */ |
Guido van Rossum | 04e7032 | 2002-07-17 16:57:13 +0000 | [diff] [blame] | 527 | } |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 528 | pendingcalls[i].func = func; |
| 529 | pendingcalls[i].arg = arg; |
| 530 | pendinglast = j; |
Skip Montanaro | d581d77 | 2002-09-03 20:10:45 +0000 | [diff] [blame] | 531 | |
| 532 | _Py_Ticker = 0; |
Kristján Valur Jónsson | 0e91938 | 2009-01-09 20:31:26 +0000 | [diff] [blame] | 533 | pendingcalls_to_do = 1; /* Signal main loop */ |
Guido van Rossum | 180d7b4 | 1994-09-29 09:45:57 +0000 | [diff] [blame] | 534 | busy = 0; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 535 | /* XXX End critical section */ |
| 536 | return 0; |
| 537 | } |
| 538 | |
Guido van Rossum | 180d7b4 | 1994-09-29 09:45:57 +0000 | [diff] [blame] | 539 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 540 | Py_MakePendingCalls(void) |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 541 | { |
Guido van Rossum | 180d7b4 | 1994-09-29 09:45:57 +0000 | [diff] [blame] | 542 | static int busy = 0; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 543 | if (busy) |
Guido van Rossum | 180d7b4 | 1994-09-29 09:45:57 +0000 | [diff] [blame] | 544 | return 0; |
| 545 | busy = 1; |
Kristján Valur Jónsson | 0e91938 | 2009-01-09 20:31:26 +0000 | [diff] [blame] | 546 | pendingcalls_to_do = 0; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 547 | for (;;) { |
| 548 | int i; |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 549 | int (*func)(void *); |
| 550 | void *arg; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 551 | i = pendingfirst; |
| 552 | if (i == pendinglast) |
| 553 | break; /* Queue empty */ |
| 554 | func = pendingcalls[i].func; |
| 555 | arg = pendingcalls[i].arg; |
| 556 | pendingfirst = (i + 1) % NPENDINGCALLS; |
Guido van Rossum | 180d7b4 | 1994-09-29 09:45:57 +0000 | [diff] [blame] | 557 | if (func(arg) < 0) { |
| 558 | busy = 0; |
Kristján Valur Jónsson | 0e91938 | 2009-01-09 20:31:26 +0000 | [diff] [blame] | 559 | pendingcalls_to_do = 1; /* We're not done yet */ |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 560 | return -1; |
Guido van Rossum | 180d7b4 | 1994-09-29 09:45:57 +0000 | [diff] [blame] | 561 | } |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 562 | } |
Guido van Rossum | 180d7b4 | 1994-09-29 09:45:57 +0000 | [diff] [blame] | 563 | busy = 0; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 564 | return 0; |
| 565 | } |
| 566 | |
Kristján Valur Jónsson | 0e91938 | 2009-01-09 20:31:26 +0000 | [diff] [blame] | 567 | #endif /* WITH_THREAD */ |
| 568 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 569 | |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 570 | /* The interpreter's recursion limit */ |
| 571 | |
Hye-Shik Chang | b6fa281 | 2005-04-04 15:49:02 +0000 | [diff] [blame] | 572 | #ifndef Py_DEFAULT_RECURSION_LIMIT |
| 573 | #define Py_DEFAULT_RECURSION_LIMIT 1000 |
| 574 | #endif |
| 575 | static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT; |
| 576 | int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 577 | |
Vladimir Marangozov | 7bd25be | 2000-09-01 11:07:19 +0000 | [diff] [blame] | 578 | int |
| 579 | Py_GetRecursionLimit(void) |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 580 | { |
| 581 | return recursion_limit; |
| 582 | } |
| 583 | |
Vladimir Marangozov | 7bd25be | 2000-09-01 11:07:19 +0000 | [diff] [blame] | 584 | void |
| 585 | Py_SetRecursionLimit(int new_limit) |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 586 | { |
| 587 | recursion_limit = new_limit; |
Thomas Wouters | ae406c6 | 2007-09-19 17:27:43 +0000 | [diff] [blame] | 588 | _Py_CheckRecursionLimit = recursion_limit; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 591 | /* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall() |
| 592 | if the recursion_depth reaches _Py_CheckRecursionLimit. |
| 593 | If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit |
| 594 | to guarantee that _Py_CheckRecursiveCall() is regularly called. |
| 595 | Without USE_STACKCHECK, there is no need for this. */ |
| 596 | int |
| 597 | _Py_CheckRecursiveCall(char *where) |
| 598 | { |
| 599 | PyThreadState *tstate = PyThreadState_GET(); |
| 600 | |
| 601 | #ifdef USE_STACKCHECK |
| 602 | if (PyOS_CheckStack()) { |
| 603 | --tstate->recursion_depth; |
| 604 | PyErr_SetString(PyExc_MemoryError, "Stack overflow"); |
| 605 | return -1; |
| 606 | } |
| 607 | #endif |
| 608 | if (tstate->recursion_depth > recursion_limit) { |
| 609 | --tstate->recursion_depth; |
| 610 | PyErr_Format(PyExc_RuntimeError, |
| 611 | "maximum recursion depth exceeded%s", |
| 612 | where); |
| 613 | return -1; |
| 614 | } |
Thomas Wouters | ae406c6 | 2007-09-19 17:27:43 +0000 | [diff] [blame] | 615 | _Py_CheckRecursionLimit = recursion_limit; |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 616 | return 0; |
| 617 | } |
| 618 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 619 | /* Status code for main loop (reason for stack unwind) */ |
Raymond Hettinger | 7c95865 | 2004-04-06 10:11:10 +0000 | [diff] [blame] | 620 | enum why_code { |
| 621 | WHY_NOT = 0x0001, /* No error */ |
| 622 | WHY_EXCEPTION = 0x0002, /* Exception occurred */ |
| 623 | WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */ |
| 624 | WHY_RETURN = 0x0008, /* 'return' statement */ |
| 625 | WHY_BREAK = 0x0010, /* 'break' statement */ |
| 626 | WHY_CONTINUE = 0x0020, /* 'continue' statement */ |
| 627 | WHY_YIELD = 0x0040 /* 'yield' operator */ |
| 628 | }; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 629 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 630 | static enum why_code do_raise(PyObject *, PyObject *, PyObject *); |
| 631 | static int unpack_iterable(PyObject *, int, PyObject **); |
Guido van Rossum | 1aa1483 | 1997-01-21 05:34:20 +0000 | [diff] [blame] | 632 | |
Jeffrey Yasskin | fd8a1ec | 2008-12-03 06:46:45 +0000 | [diff] [blame] | 633 | /* Records whether tracing is on for any thread. Counts the number of |
| 634 | threads for which tstate->c_tracefunc is non-NULL, so if the value |
| 635 | is 0, we know we don't have to check this thread's c_tracefunc. |
| 636 | This speeds up the if statement in PyEval_EvalFrameEx() after |
| 637 | fast_next_opcode*/ |
| 638 | static int _Py_TracingPossible = 0; |
| 639 | |
Skip Montanaro | d581d77 | 2002-09-03 20:10:45 +0000 | [diff] [blame] | 640 | /* for manipulating the thread switch and periodic "stuff" - used to be |
| 641 | per thread, now just a pair o' globals */ |
Skip Montanaro | 99dba27 | 2002-09-03 20:19:06 +0000 | [diff] [blame] | 642 | int _Py_CheckInterval = 100; |
Kristján Valur Jónsson | 0e91938 | 2009-01-09 20:31:26 +0000 | [diff] [blame] | 643 | volatile int _Py_Ticker = 0; /* so that we hit a "tick" first thing */ |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 644 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 645 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 646 | PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 647 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 648 | return PyEval_EvalCodeEx(co, |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 649 | globals, locals, |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 650 | (PyObject **)NULL, 0, |
| 651 | (PyObject **)NULL, 0, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 652 | (PyObject **)NULL, 0, |
| 653 | NULL); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | |
| 657 | /* Interpreter main loop */ |
| 658 | |
Martin v. Löwis | 8d97e33 | 2004-06-27 15:43:12 +0000 | [diff] [blame] | 659 | PyObject * |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 660 | PyEval_EvalFrame(PyFrameObject *f) { |
| 661 | /* This is for backward compatibility with extension modules that |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 662 | used this API; core interpreter code should call |
| 663 | PyEval_EvalFrameEx() */ |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 664 | return PyEval_EvalFrameEx(f, 0); |
| 665 | } |
| 666 | |
| 667 | PyObject * |
Anthony Baxter | a863d33 | 2006-04-11 07:43:46 +0000 | [diff] [blame] | 668 | PyEval_EvalFrameEx(PyFrameObject *f, int throwflag) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 669 | { |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 670 | #ifdef DXPAIRS |
| 671 | int lastopcode = 0; |
| 672 | #endif |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 673 | register PyObject **stack_pointer; /* Next free slot in value stack */ |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 674 | register unsigned char *next_instr; |
Armin Rigo | 8817fcd | 2004-06-17 10:22:40 +0000 | [diff] [blame] | 675 | register int opcode; /* Current opcode */ |
| 676 | register int oparg; /* Current opcode argument, if any */ |
Raymond Hettinger | 7c95865 | 2004-04-06 10:11:10 +0000 | [diff] [blame] | 677 | register enum why_code why; /* Reason for block stack unwind */ |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 678 | register int err; /* Error status -- nonzero if error */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 679 | register PyObject *x; /* Result object -- NULL if error */ |
| 680 | register PyObject *v; /* Temporary objects popped off stack */ |
| 681 | register PyObject *w; |
| 682 | register PyObject *u; |
| 683 | register PyObject *t; |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 684 | register PyObject *stream = NULL; /* for PRINT opcodes */ |
Jeremy Hylton | 2b724da | 2001-01-29 22:51:52 +0000 | [diff] [blame] | 685 | register PyObject **fastlocals, **freevars; |
Guido van Rossum | 014518f | 1998-11-23 21:09:51 +0000 | [diff] [blame] | 686 | PyObject *retval = NULL; /* Return value */ |
Guido van Rossum | 885553e | 1998-12-21 18:33:30 +0000 | [diff] [blame] | 687 | PyThreadState *tstate = PyThreadState_GET(); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 688 | PyCodeObject *co; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 689 | |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 690 | /* when tracing we set things up so that |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 691 | |
| 692 | not (instr_lb <= current_bytecode_offset < instr_ub) |
| 693 | |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 694 | is true when the line being executed has changed. The |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 695 | initial values are such as to make this false the first |
| 696 | time it is tested. */ |
Armin Rigo | bf57a14 | 2004-03-22 19:24:58 +0000 | [diff] [blame] | 697 | int instr_ub = -1, instr_lb = 0, instr_prev = -1; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 698 | |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 699 | unsigned char *first_instr; |
Skip Montanaro | 04d80f8 | 2002-08-04 21:03:35 +0000 | [diff] [blame] | 700 | PyObject *names; |
| 701 | PyObject *consts; |
Neal Norwitz | 5f5153e | 2005-10-21 04:28:38 +0000 | [diff] [blame] | 702 | #if defined(Py_DEBUG) || defined(LLTRACE) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 703 | /* Make it easier to find out where we are with a debugger */ |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 704 | char *filename; |
Guido van Rossum | 99bec95 | 1992-09-03 20:29:45 +0000 | [diff] [blame] | 705 | #endif |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 706 | |
Neal Norwitz | a81d220 | 2002-07-14 00:27:26 +0000 | [diff] [blame] | 707 | /* Tuple access macros */ |
| 708 | |
| 709 | #ifndef Py_DEBUG |
| 710 | #define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i)) |
| 711 | #else |
| 712 | #define GETITEM(v, i) PyTuple_GetItem((v), (i)) |
| 713 | #endif |
| 714 | |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 715 | #ifdef WITH_TSC |
| 716 | /* Use Pentium timestamp counter to mark certain events: |
| 717 | inst0 -- beginning of switch statement for opcode dispatch |
| 718 | inst1 -- end of switch statement (may be skipped) |
| 719 | loop0 -- the top of the mainloop |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 720 | loop1 -- place where control returns again to top of mainloop |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 721 | (may be skipped) |
| 722 | intr1 -- beginning of long interruption |
| 723 | intr2 -- end of long interruption |
| 724 | |
| 725 | Many opcodes call out to helper C functions. In some cases, the |
| 726 | time in those functions should be counted towards the time for the |
| 727 | opcode, but not in all cases. For example, a CALL_FUNCTION opcode |
| 728 | calls another Python function; there's no point in charge all the |
| 729 | bytecode executed by the called function to the caller. |
| 730 | |
| 731 | It's hard to make a useful judgement statically. In the presence |
| 732 | of operator overloading, it's impossible to tell if a call will |
| 733 | execute new Python code or not. |
| 734 | |
| 735 | It's a case-by-case judgement. I'll use intr1 for the following |
| 736 | cases: |
| 737 | |
| 738 | EXEC_STMT |
| 739 | IMPORT_STAR |
| 740 | IMPORT_FROM |
| 741 | CALL_FUNCTION (and friends) |
| 742 | |
| 743 | */ |
| 744 | uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0; |
| 745 | int ticked = 0; |
| 746 | |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 747 | READ_TIMESTAMP(inst0); |
| 748 | READ_TIMESTAMP(inst1); |
| 749 | READ_TIMESTAMP(loop0); |
| 750 | READ_TIMESTAMP(loop1); |
Michael W. Hudson | 800ba23 | 2004-08-12 18:19:17 +0000 | [diff] [blame] | 751 | |
| 752 | /* shut up the compiler */ |
| 753 | opcode = 0; |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 754 | #endif |
| 755 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 756 | /* Code access macros */ |
| 757 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 758 | #define INSTR_OFFSET() ((int)(next_instr - first_instr)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 759 | #define NEXTOP() (*next_instr++) |
Raymond Hettinger | 5bed456 | 2004-04-10 23:34:17 +0000 | [diff] [blame] | 760 | #define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2]) |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 761 | #define PEEKARG() ((next_instr[2]<<8) + next_instr[1]) |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 762 | #define JUMPTO(x) (next_instr = first_instr + (x)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 763 | #define JUMPBY(x) (next_instr += (x)) |
| 764 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 765 | /* OpCode prediction macros |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 766 | Some opcodes tend to come in pairs thus making it possible to |
| 767 | predict the second code when the first is run. For example, |
Jeffrey Yasskin | 68d6852 | 2009-02-28 19:03:21 +0000 | [diff] [blame] | 768 | GET_ITER is often followed by FOR_ITER. And FOR_ITER is often |
| 769 | followed by STORE_FAST or UNPACK_SEQUENCE. |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 770 | |
Raymond Hettinger | afae11e | 2008-07-05 02:11:55 +0000 | [diff] [blame] | 771 | Verifying the prediction costs a single high-speed test of a register |
Raymond Hettinger | ac207292 | 2003-03-16 15:41:11 +0000 | [diff] [blame] | 772 | variable against a constant. If the pairing was good, then the |
Raymond Hettinger | afae11e | 2008-07-05 02:11:55 +0000 | [diff] [blame] | 773 | processor's own internal branch predication has a high likelihood of |
| 774 | success, resulting in a nearly zero-overhead transition to the |
| 775 | next opcode. A successful prediction saves a trip through the eval-loop |
Antoine Pitrou | c2cc80c | 2008-07-25 22:13:52 +0000 | [diff] [blame] | 776 | including its two unpredictable branches, the HAS_ARG test and the |
Raymond Hettinger | afae11e | 2008-07-05 02:11:55 +0000 | [diff] [blame] | 777 | switch-case. Combined with the processor's internal branch prediction, |
Antoine Pitrou | c2cc80c | 2008-07-25 22:13:52 +0000 | [diff] [blame] | 778 | a successful PREDICT has the effect of making the two opcodes run as if |
Raymond Hettinger | afae11e | 2008-07-05 02:11:55 +0000 | [diff] [blame] | 779 | they were a single new opcode with the bodies combined. |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 780 | |
Raymond Hettinger | afae11e | 2008-07-05 02:11:55 +0000 | [diff] [blame] | 781 | If collecting opcode statistics, your choices are to either keep the |
| 782 | predictions turned-on and interpret the results as if some opcodes |
| 783 | had been combined or turn-off predictions so that the opcode frequency |
| 784 | counter updates for both opcodes. |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 785 | */ |
| 786 | |
Raymond Hettinger | a721698 | 2004-02-08 19:59:27 +0000 | [diff] [blame] | 787 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 788 | #define PREDICT(op) if (0) goto PRED_##op |
| 789 | #else |
Raymond Hettinger | ac207292 | 2003-03-16 15:41:11 +0000 | [diff] [blame] | 790 | #define PREDICT(op) if (*next_instr == op) goto PRED_##op |
Raymond Hettinger | a721698 | 2004-02-08 19:59:27 +0000 | [diff] [blame] | 791 | #endif |
| 792 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 793 | #define PREDICTED(op) PRED_##op: next_instr++ |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 794 | #define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3 |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 795 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 796 | /* Stack manipulation macros */ |
| 797 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 798 | /* The stack can grow at most MAXINT deep, as co_nlocals and |
| 799 | co_stacksize are ints. */ |
| 800 | #define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 801 | #define EMPTY() (STACK_LEVEL() == 0) |
| 802 | #define TOP() (stack_pointer[-1]) |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 803 | #define SECOND() (stack_pointer[-2]) |
| 804 | #define THIRD() (stack_pointer[-3]) |
| 805 | #define FOURTH() (stack_pointer[-4]) |
Benjamin Peterson | b8338ab | 2009-06-28 16:08:02 +0000 | [diff] [blame] | 806 | #define PEEK(n) (stack_pointer[-(n)]) |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 807 | #define SET_TOP(v) (stack_pointer[-1] = (v)) |
| 808 | #define SET_SECOND(v) (stack_pointer[-2] = (v)) |
| 809 | #define SET_THIRD(v) (stack_pointer[-3] = (v)) |
| 810 | #define SET_FOURTH(v) (stack_pointer[-4] = (v)) |
Benjamin Peterson | b8338ab | 2009-06-28 16:08:02 +0000 | [diff] [blame] | 811 | #define SET_VALUE(n, v) (stack_pointer[-(n)] = (v)) |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 812 | #define BASIC_STACKADJ(n) (stack_pointer += n) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 813 | #define BASIC_PUSH(v) (*stack_pointer++ = (v)) |
| 814 | #define BASIC_POP() (*--stack_pointer) |
| 815 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 816 | #ifdef LLTRACE |
Jeremy Hylton | 1436815 | 2001-10-17 13:29:30 +0000 | [diff] [blame] | 817 | #define PUSH(v) { (void)(BASIC_PUSH(v), \ |
| 818 | lltrace && prtrace(TOP(), "push")); \ |
Richard Jones | cebbefc | 2006-05-23 18:28:17 +0000 | [diff] [blame] | 819 | assert(STACK_LEVEL() <= co->co_stacksize); } |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 820 | #define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \ |
| 821 | BASIC_POP()) |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 822 | #define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \ |
| 823 | lltrace && prtrace(TOP(), "stackadj")); \ |
Richard Jones | cebbefc | 2006-05-23 18:28:17 +0000 | [diff] [blame] | 824 | assert(STACK_LEVEL() <= co->co_stacksize); } |
Christian Heimes | 52729ac | 2007-12-14 02:33:57 +0000 | [diff] [blame] | 825 | #define EXT_POP(STACK_POINTER) ((void)(lltrace && \ |
| 826 | prtrace((STACK_POINTER)[-1], "ext_pop")), \ |
| 827 | *--(STACK_POINTER)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 828 | #else |
| 829 | #define PUSH(v) BASIC_PUSH(v) |
| 830 | #define POP() BASIC_POP() |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 831 | #define STACKADJ(n) BASIC_STACKADJ(n) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 832 | #define EXT_POP(STACK_POINTER) (*--(STACK_POINTER)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 833 | #endif |
| 834 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 835 | /* Local variable macros */ |
| 836 | |
| 837 | #define GETLOCAL(i) (fastlocals[i]) |
Guido van Rossum | cfbf1a3 | 2002-03-28 20:17:52 +0000 | [diff] [blame] | 838 | |
| 839 | /* The SETLOCAL() macro must not DECREF the local variable in-place and |
| 840 | then store the new value; it must copy the old value to a temporary |
| 841 | value, then store the new value, and then DECREF the temporary value. |
| 842 | This is because it is possible that during the DECREF the frame is |
| 843 | accessed by other code (e.g. a __del__ method or gc.collect()) and the |
| 844 | variable would be pointing to already-freed memory. */ |
| 845 | #define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \ |
| 846 | GETLOCAL(i) = value; \ |
| 847 | Py_XDECREF(tmp); } while (0) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 848 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 849 | /* Start of code */ |
| 850 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 851 | if (f == NULL) |
| 852 | return NULL; |
| 853 | |
Armin Rigo | 1d313ab | 2003-10-25 14:33:09 +0000 | [diff] [blame] | 854 | /* push frame */ |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 855 | if (Py_EnterRecursiveCall("")) |
Armin Rigo | 1d313ab | 2003-10-25 14:33:09 +0000 | [diff] [blame] | 856 | return NULL; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 857 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 858 | tstate->frame = f; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 859 | |
Neil Schemenauer | 6c0f200 | 2001-09-04 19:03:35 +0000 | [diff] [blame] | 860 | if (tstate->use_tracing) { |
| 861 | if (tstate->c_tracefunc != NULL) { |
| 862 | /* tstate->c_tracefunc, if defined, is a |
| 863 | function that will be called on *every* entry |
| 864 | to a code block. Its return value, if not |
| 865 | None, is a function that will be called at |
| 866 | the start of each executed line of code. |
| 867 | (Actually, the function must return itself |
| 868 | in order to continue tracing.) The trace |
| 869 | functions are called with three arguments: |
| 870 | a pointer to the current frame, a string |
| 871 | indicating why the function is called, and |
| 872 | an argument which depends on the situation. |
| 873 | The global trace function is also called |
| 874 | whenever an exception is detected. */ |
Antoine Pitrou | c2cc80c | 2008-07-25 22:13:52 +0000 | [diff] [blame] | 875 | if (call_trace_protected(tstate->c_tracefunc, |
Amaury Forgeot d'Arc | 0d75f09 | 2007-11-13 21:54:28 +0000 | [diff] [blame] | 876 | tstate->c_traceobj, |
| 877 | f, PyTrace_CALL, Py_None)) { |
Neil Schemenauer | 6c0f200 | 2001-09-04 19:03:35 +0000 | [diff] [blame] | 878 | /* Trace function raised an error */ |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 879 | goto exit_eval_frame; |
Neil Schemenauer | 6c0f200 | 2001-09-04 19:03:35 +0000 | [diff] [blame] | 880 | } |
| 881 | } |
| 882 | if (tstate->c_profilefunc != NULL) { |
| 883 | /* Similar for c_profilefunc, except it needn't |
| 884 | return itself and isn't called for "line" events */ |
Amaury Forgeot d'Arc | 0d75f09 | 2007-11-13 21:54:28 +0000 | [diff] [blame] | 885 | if (call_trace_protected(tstate->c_profilefunc, |
| 886 | tstate->c_profileobj, |
| 887 | f, PyTrace_CALL, Py_None)) { |
Neil Schemenauer | 6c0f200 | 2001-09-04 19:03:35 +0000 | [diff] [blame] | 888 | /* Profile function raised an error */ |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 889 | goto exit_eval_frame; |
Neil Schemenauer | 6c0f200 | 2001-09-04 19:03:35 +0000 | [diff] [blame] | 890 | } |
| 891 | } |
| 892 | } |
| 893 | |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 894 | co = f->f_code; |
| 895 | names = co->co_names; |
| 896 | consts = co->co_consts; |
| 897 | fastlocals = f->f_localsplus; |
Richard Jones | cebbefc | 2006-05-23 18:28:17 +0000 | [diff] [blame] | 898 | freevars = f->f_localsplus + co->co_nlocals; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 899 | first_instr = (unsigned char*) PyString_AS_STRING(co->co_code); |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 900 | /* An explanation is in order for the next line. |
| 901 | |
| 902 | f->f_lasti now refers to the index of the last instruction |
| 903 | executed. You might think this was obvious from the name, but |
| 904 | this wasn't always true before 2.3! PyFrame_New now sets |
| 905 | f->f_lasti to -1 (i.e. the index *before* the first instruction) |
| 906 | and YIELD_VALUE doesn't fiddle with f_lasti any more. So this |
Antoine Pitrou | c2cc80c | 2008-07-25 22:13:52 +0000 | [diff] [blame] | 907 | does work. Promise. |
Raymond Hettinger | 4bd97d4 | 2007-01-06 01:14:41 +0000 | [diff] [blame] | 908 | |
| 909 | When the PREDICT() macros are enabled, some opcode pairs follow in |
Antoine Pitrou | c2cc80c | 2008-07-25 22:13:52 +0000 | [diff] [blame] | 910 | direct succession without updating f->f_lasti. A successful |
Raymond Hettinger | 4bd97d4 | 2007-01-06 01:14:41 +0000 | [diff] [blame] | 911 | prediction effectively links the two codes together as if they |
| 912 | were a single new opcode; accordingly,f->f_lasti will point to |
| 913 | the first code in the pair (for instance, GET_ITER followed by |
| 914 | FOR_ITER is effectively a single opcode and f->f_lasti will point |
| 915 | at to the beginning of the combined pair.) |
| 916 | */ |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 917 | next_instr = first_instr + f->f_lasti + 1; |
| 918 | stack_pointer = f->f_stacktop; |
| 919 | assert(stack_pointer != NULL); |
| 920 | f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */ |
| 921 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 922 | #ifdef LLTRACE |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 923 | lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 924 | #endif |
Neal Norwitz | 5f5153e | 2005-10-21 04:28:38 +0000 | [diff] [blame] | 925 | #if defined(Py_DEBUG) || defined(LLTRACE) |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 926 | filename = PyString_AsString(co->co_filename); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 927 | #endif |
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 | why = WHY_NOT; |
| 930 | err = 0; |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 931 | x = Py_None; /* Not a reference, just anything non-NULL */ |
Fred Drake | 48fba73 | 2000-10-11 13:54:07 +0000 | [diff] [blame] | 932 | w = NULL; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 933 | |
Anthony Baxter | a863d33 | 2006-04-11 07:43:46 +0000 | [diff] [blame] | 934 | if (throwflag) { /* support for generator.throw() */ |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 935 | why = WHY_EXCEPTION; |
| 936 | goto on_error; |
| 937 | } |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 938 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 939 | for (;;) { |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 940 | #ifdef WITH_TSC |
| 941 | if (inst1 == 0) { |
| 942 | /* Almost surely, the opcode executed a break |
| 943 | or a continue, preventing inst1 from being set |
| 944 | on the way out of the loop. |
| 945 | */ |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 946 | READ_TIMESTAMP(inst1); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 947 | loop1 = inst1; |
| 948 | } |
| 949 | dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1, |
| 950 | intr0, intr1); |
| 951 | ticked = 0; |
| 952 | inst1 = 0; |
| 953 | intr0 = 0; |
| 954 | intr1 = 0; |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 955 | READ_TIMESTAMP(loop0); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 956 | #endif |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 957 | assert(stack_pointer >= f->f_valuestack); /* else underflow */ |
Richard Jones | cebbefc | 2006-05-23 18:28:17 +0000 | [diff] [blame] | 958 | assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */ |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 959 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 960 | /* Do periodic things. Doing this every time through |
| 961 | the loop would add too much overhead, so we do it |
| 962 | only every Nth instruction. We also do it if |
Kristján Valur Jónsson | 0e91938 | 2009-01-09 20:31:26 +0000 | [diff] [blame] | 963 | ``pendingcalls_to_do'' is set, i.e. when an asynchronous |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 964 | event needs attention (e.g. a signal handler or |
| 965 | async I/O handler); see Py_AddPendingCall() and |
| 966 | Py_MakePendingCalls() above. */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 967 | |
Skip Montanaro | d581d77 | 2002-09-03 20:10:45 +0000 | [diff] [blame] | 968 | if (--_Py_Ticker < 0) { |
Thomas Wouters | ae406c6 | 2007-09-19 17:27:43 +0000 | [diff] [blame] | 969 | if (*next_instr == SETUP_FINALLY) { |
| 970 | /* Make the last opcode before |
| 971 | a try: finally: block uninterruptable. */ |
| 972 | goto fast_next_opcode; |
| 973 | } |
Skip Montanaro | d581d77 | 2002-09-03 20:10:45 +0000 | [diff] [blame] | 974 | _Py_Ticker = _Py_CheckInterval; |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 975 | tstate->tick_counter++; |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 976 | #ifdef WITH_TSC |
| 977 | ticked = 1; |
| 978 | #endif |
Kristján Valur Jónsson | 0e91938 | 2009-01-09 20:31:26 +0000 | [diff] [blame] | 979 | if (pendingcalls_to_do) { |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 980 | if (Py_MakePendingCalls() < 0) { |
| 981 | why = WHY_EXCEPTION; |
| 982 | goto on_error; |
| 983 | } |
Kristján Valur Jónsson | 0e91938 | 2009-01-09 20:31:26 +0000 | [diff] [blame] | 984 | if (pendingcalls_to_do) |
Kurt B. Kaiser | 4c79a83 | 2004-11-23 18:06:08 +0000 | [diff] [blame] | 985 | /* MakePendingCalls() didn't succeed. |
| 986 | Force early re-execution of this |
| 987 | "periodic" code, possibly after |
| 988 | a thread switch */ |
| 989 | _Py_Ticker = 0; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 990 | } |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 991 | #ifdef WITH_THREAD |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 992 | if (interpreter_lock) { |
| 993 | /* Give another thread a chance */ |
| 994 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 995 | if (PyThreadState_Swap(NULL) != tstate) |
| 996 | Py_FatalError("ceval: tstate mix-up"); |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 997 | PyThread_release_lock(interpreter_lock); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 998 | |
| 999 | /* Other threads may run now */ |
| 1000 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 1001 | PyThread_acquire_lock(interpreter_lock, 1); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1002 | if (PyThreadState_Swap(tstate) != NULL) |
| 1003 | Py_FatalError("ceval: orphan tstate"); |
Guido van Rossum | b8b6d0c | 2003-06-28 21:53:52 +0000 | [diff] [blame] | 1004 | |
| 1005 | /* Check for thread interrupts */ |
| 1006 | |
| 1007 | if (tstate->async_exc != NULL) { |
| 1008 | x = tstate->async_exc; |
| 1009 | tstate->async_exc = NULL; |
| 1010 | PyErr_SetNone(x); |
| 1011 | Py_DECREF(x); |
| 1012 | why = WHY_EXCEPTION; |
| 1013 | goto on_error; |
| 1014 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1015 | } |
| 1016 | #endif |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1017 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1018 | |
Neil Schemenauer | 6354386 | 2002-02-17 19:10:14 +0000 | [diff] [blame] | 1019 | fast_next_opcode: |
Guido van Rossum | 99bec95 | 1992-09-03 20:29:45 +0000 | [diff] [blame] | 1020 | f->f_lasti = INSTR_OFFSET(); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1021 | |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1022 | /* line-by-line tracing support */ |
| 1023 | |
Jeffrey Yasskin | fd8a1ec | 2008-12-03 06:46:45 +0000 | [diff] [blame] | 1024 | if (_Py_TracingPossible && |
| 1025 | tstate->c_tracefunc != NULL && !tstate->tracing) { |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1026 | /* see maybe_call_line_trace |
| 1027 | for expository comments */ |
| 1028 | f->f_stacktop = stack_pointer; |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 1029 | |
Michael W. Hudson | 58ee2af | 2003-04-29 16:18:47 +0000 | [diff] [blame] | 1030 | err = maybe_call_line_trace(tstate->c_tracefunc, |
| 1031 | tstate->c_traceobj, |
Armin Rigo | bf57a14 | 2004-03-22 19:24:58 +0000 | [diff] [blame] | 1032 | f, &instr_lb, &instr_ub, |
| 1033 | &instr_prev); |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1034 | /* Reload possibly changed frame fields */ |
| 1035 | JUMPTO(f->f_lasti); |
Michael W. Hudson | 58ee2af | 2003-04-29 16:18:47 +0000 | [diff] [blame] | 1036 | if (f->f_stacktop != NULL) { |
| 1037 | stack_pointer = f->f_stacktop; |
| 1038 | f->f_stacktop = NULL; |
| 1039 | } |
| 1040 | if (err) { |
| 1041 | /* trace function raised an exception */ |
| 1042 | goto on_error; |
| 1043 | } |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1044 | } |
| 1045 | |
| 1046 | /* Extract opcode and argument */ |
| 1047 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1048 | opcode = NEXTOP(); |
Armin Rigo | 8817fcd | 2004-06-17 10:22:40 +0000 | [diff] [blame] | 1049 | oparg = 0; /* allows oparg to be stored in a register because |
| 1050 | it doesn't have to be remembered across a full loop */ |
Raymond Hettinger | 5bed456 | 2004-04-10 23:34:17 +0000 | [diff] [blame] | 1051 | if (HAS_ARG(opcode)) |
| 1052 | oparg = NEXTARG(); |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 1053 | dispatch_opcode: |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 1054 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 1055 | #ifdef DXPAIRS |
| 1056 | dxpairs[lastopcode][opcode]++; |
| 1057 | lastopcode = opcode; |
| 1058 | #endif |
| 1059 | dxp[opcode]++; |
| 1060 | #endif |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1061 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 1062 | #ifdef LLTRACE |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1063 | /* Instruction tracing */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1064 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 1065 | if (lltrace) { |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1066 | if (HAS_ARG(opcode)) { |
| 1067 | printf("%d: %d, %d\n", |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 1068 | f->f_lasti, opcode, oparg); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1069 | } |
| 1070 | else { |
| 1071 | printf("%d: %d\n", |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 1072 | f->f_lasti, opcode); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1073 | } |
| 1074 | } |
| 1075 | #endif |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 1076 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1077 | /* Main switch on opcode */ |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 1078 | READ_TIMESTAMP(inst0); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 1079 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1080 | switch (opcode) { |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1081 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1082 | /* BEWARE! |
| 1083 | It is essential that any operation that fails sets either |
| 1084 | x to NULL, err to nonzero, or why to anything but WHY_NOT, |
| 1085 | and that no operation that succeeds does this! */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1086 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1087 | /* case STOP_CODE: this is an error! */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1088 | |
Raymond Hettinger | 9c18e81 | 2004-06-21 16:31:15 +0000 | [diff] [blame] | 1089 | case NOP: |
| 1090 | goto fast_next_opcode; |
| 1091 | |
Neil Schemenauer | 6354386 | 2002-02-17 19:10:14 +0000 | [diff] [blame] | 1092 | case LOAD_FAST: |
| 1093 | x = GETLOCAL(oparg); |
| 1094 | if (x != NULL) { |
| 1095 | Py_INCREF(x); |
| 1096 | PUSH(x); |
| 1097 | goto fast_next_opcode; |
| 1098 | } |
| 1099 | format_exc_check_arg(PyExc_UnboundLocalError, |
| 1100 | UNBOUNDLOCAL_ERROR_MSG, |
| 1101 | PyTuple_GetItem(co->co_varnames, oparg)); |
| 1102 | break; |
| 1103 | |
| 1104 | case LOAD_CONST: |
Skip Montanaro | 04d80f8 | 2002-08-04 21:03:35 +0000 | [diff] [blame] | 1105 | x = GETITEM(consts, oparg); |
Neil Schemenauer | 6354386 | 2002-02-17 19:10:14 +0000 | [diff] [blame] | 1106 | Py_INCREF(x); |
| 1107 | PUSH(x); |
| 1108 | goto fast_next_opcode; |
| 1109 | |
Raymond Hettinger | 7dc5221 | 2003-03-16 20:14:44 +0000 | [diff] [blame] | 1110 | PREDICTED_WITH_ARG(STORE_FAST); |
Neil Schemenauer | 6354386 | 2002-02-17 19:10:14 +0000 | [diff] [blame] | 1111 | case STORE_FAST: |
| 1112 | v = POP(); |
| 1113 | SETLOCAL(oparg, v); |
| 1114 | goto fast_next_opcode; |
| 1115 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1116 | case POP_TOP: |
| 1117 | v = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1118 | Py_DECREF(v); |
Neil Schemenauer | 6354386 | 2002-02-17 19:10:14 +0000 | [diff] [blame] | 1119 | goto fast_next_opcode; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1120 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1121 | case ROT_TWO: |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1122 | v = TOP(); |
| 1123 | w = SECOND(); |
| 1124 | SET_TOP(w); |
| 1125 | SET_SECOND(v); |
Raymond Hettinger | 080cb32 | 2003-03-14 01:37:42 +0000 | [diff] [blame] | 1126 | goto fast_next_opcode; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1127 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1128 | case ROT_THREE: |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1129 | v = TOP(); |
| 1130 | w = SECOND(); |
| 1131 | x = THIRD(); |
| 1132 | SET_TOP(w); |
| 1133 | SET_SECOND(x); |
| 1134 | SET_THIRD(v); |
Raymond Hettinger | 080cb32 | 2003-03-14 01:37:42 +0000 | [diff] [blame] | 1135 | goto fast_next_opcode; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1136 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1137 | case ROT_FOUR: |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1138 | u = TOP(); |
| 1139 | v = SECOND(); |
| 1140 | w = THIRD(); |
| 1141 | x = FOURTH(); |
| 1142 | SET_TOP(v); |
| 1143 | SET_SECOND(w); |
| 1144 | SET_THIRD(x); |
| 1145 | SET_FOURTH(u); |
Raymond Hettinger | 080cb32 | 2003-03-14 01:37:42 +0000 | [diff] [blame] | 1146 | goto fast_next_opcode; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1147 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1148 | case DUP_TOP: |
| 1149 | v = TOP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1150 | Py_INCREF(v); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1151 | PUSH(v); |
Raymond Hettinger | 080cb32 | 2003-03-14 01:37:42 +0000 | [diff] [blame] | 1152 | goto fast_next_opcode; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1153 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1154 | case DUP_TOPX: |
Raymond Hettinger | 4bad9ba | 2003-01-19 05:08:13 +0000 | [diff] [blame] | 1155 | if (oparg == 2) { |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1156 | x = TOP(); |
Tim Peters | 35ba689 | 2000-10-11 07:04:49 +0000 | [diff] [blame] | 1157 | Py_INCREF(x); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1158 | w = SECOND(); |
Tim Peters | 35ba689 | 2000-10-11 07:04:49 +0000 | [diff] [blame] | 1159 | Py_INCREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1160 | STACKADJ(2); |
| 1161 | SET_TOP(x); |
| 1162 | SET_SECOND(w); |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 1163 | goto fast_next_opcode; |
Raymond Hettinger | 4bad9ba | 2003-01-19 05:08:13 +0000 | [diff] [blame] | 1164 | } else if (oparg == 3) { |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1165 | x = TOP(); |
Tim Peters | 35ba689 | 2000-10-11 07:04:49 +0000 | [diff] [blame] | 1166 | Py_INCREF(x); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1167 | w = SECOND(); |
Tim Peters | 35ba689 | 2000-10-11 07:04:49 +0000 | [diff] [blame] | 1168 | Py_INCREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1169 | v = THIRD(); |
Tim Peters | 35ba689 | 2000-10-11 07:04:49 +0000 | [diff] [blame] | 1170 | Py_INCREF(v); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1171 | STACKADJ(3); |
| 1172 | SET_TOP(x); |
| 1173 | SET_SECOND(w); |
| 1174 | SET_THIRD(v); |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 1175 | goto fast_next_opcode; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1176 | } |
Raymond Hettinger | 4bad9ba | 2003-01-19 05:08:13 +0000 | [diff] [blame] | 1177 | Py_FatalError("invalid argument to DUP_TOPX" |
| 1178 | " (bytecode corruption?)"); |
Jeffrey Yasskin | 2d873bd | 2008-12-08 18:55:24 +0000 | [diff] [blame] | 1179 | /* Never returns, so don't bother to set why. */ |
Tim Peters | 35ba689 | 2000-10-11 07:04:49 +0000 | [diff] [blame] | 1180 | break; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1181 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1182 | case UNARY_POSITIVE: |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1183 | v = TOP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1184 | x = PyNumber_Positive(v); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1185 | Py_DECREF(v); |
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 UNARY_NEGATIVE: |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1191 | v = TOP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1192 | x = PyNumber_Negative(v); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1193 | Py_DECREF(v); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1194 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1195 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1196 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1197 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1198 | case UNARY_NOT: |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1199 | v = TOP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1200 | err = PyObject_IsTrue(v); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1201 | Py_DECREF(v); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1202 | if (err == 0) { |
| 1203 | Py_INCREF(Py_True); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1204 | SET_TOP(Py_True); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1205 | continue; |
| 1206 | } |
| 1207 | else if (err > 0) { |
| 1208 | Py_INCREF(Py_False); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1209 | SET_TOP(Py_False); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1210 | err = 0; |
| 1211 | continue; |
| 1212 | } |
Raymond Hettinger | 8bb90a5 | 2003-01-14 12:43:10 +0000 | [diff] [blame] | 1213 | STACKADJ(-1); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1214 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1215 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1216 | case UNARY_CONVERT: |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1217 | v = TOP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1218 | x = PyObject_Repr(v); |
| 1219 | Py_DECREF(v); |
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 | 374a922 | 1991-04-04 10:40:29 +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 UNARY_INVERT: |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1225 | v = TOP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1226 | x = PyNumber_Invert(v); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1227 | Py_DECREF(v); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1228 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1229 | if (x != NULL) continue; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1230 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1231 | |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1232 | case BINARY_POWER: |
| 1233 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1234 | v = TOP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1235 | x = PyNumber_Power(v, w, Py_None); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1236 | Py_DECREF(v); |
| 1237 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1238 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1239 | if (x != NULL) continue; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1240 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1241 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1242 | case BINARY_MULTIPLY: |
| 1243 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1244 | v = TOP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1245 | x = PyNumber_Multiply(v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1246 | Py_DECREF(v); |
| 1247 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1248 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1249 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1250 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1251 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1252 | case BINARY_DIVIDE: |
Tim Peters | 3caca23 | 2001-12-06 06:23:26 +0000 | [diff] [blame] | 1253 | if (!_Py_QnewFlag) { |
| 1254 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1255 | v = TOP(); |
Tim Peters | 3caca23 | 2001-12-06 06:23:26 +0000 | [diff] [blame] | 1256 | x = PyNumber_Divide(v, w); |
| 1257 | Py_DECREF(v); |
| 1258 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1259 | SET_TOP(x); |
Tim Peters | 3caca23 | 2001-12-06 06:23:26 +0000 | [diff] [blame] | 1260 | if (x != NULL) continue; |
| 1261 | break; |
| 1262 | } |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1263 | /* -Qnew is in effect: fall through to |
Tim Peters | 3caca23 | 2001-12-06 06:23:26 +0000 | [diff] [blame] | 1264 | BINARY_TRUE_DIVIDE */ |
| 1265 | case BINARY_TRUE_DIVIDE: |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1266 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1267 | v = TOP(); |
Tim Peters | 3caca23 | 2001-12-06 06:23:26 +0000 | [diff] [blame] | 1268 | x = PyNumber_TrueDivide(v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1269 | Py_DECREF(v); |
| 1270 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1271 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1272 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1273 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1274 | |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1275 | case BINARY_FLOOR_DIVIDE: |
| 1276 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1277 | v = TOP(); |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1278 | x = PyNumber_FloorDivide(v, w); |
| 1279 | Py_DECREF(v); |
| 1280 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1281 | SET_TOP(x); |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1282 | if (x != NULL) continue; |
| 1283 | break; |
| 1284 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1285 | case BINARY_MODULO: |
| 1286 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1287 | v = TOP(); |
Collin Winter | 8725dce | 2009-02-20 19:30:41 +0000 | [diff] [blame] | 1288 | if (PyString_CheckExact(v)) |
| 1289 | x = PyString_Format(v, w); |
| 1290 | else |
| 1291 | x = PyNumber_Remainder(v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1292 | Py_DECREF(v); |
| 1293 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1294 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1295 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1296 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1297 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1298 | case BINARY_ADD: |
| 1299 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1300 | v = TOP(); |
Tim Peters | c1e6d96 | 2001-10-05 20:21:03 +0000 | [diff] [blame] | 1301 | if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) { |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1302 | /* INLINE: int + int */ |
| 1303 | register long a, b, i; |
Guido van Rossum | cf183ac | 1998-12-04 18:51:36 +0000 | [diff] [blame] | 1304 | a = PyInt_AS_LONG(v); |
| 1305 | b = PyInt_AS_LONG(w); |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1306 | i = a + b; |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1307 | if ((i^a) < 0 && (i^b) < 0) |
| 1308 | goto slow_add; |
| 1309 | x = PyInt_FromLong(i); |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1310 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1311 | else if (PyString_CheckExact(v) && |
| 1312 | PyString_CheckExact(w)) { |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 1313 | x = string_concatenate(v, w, f, next_instr); |
| 1314 | /* string_concatenate consumed the ref to v */ |
| 1315 | goto skip_decref_vx; |
| 1316 | } |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1317 | else { |
| 1318 | slow_add: |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1319 | x = PyNumber_Add(v, w); |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1320 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1321 | Py_DECREF(v); |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 1322 | skip_decref_vx: |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1323 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1324 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1325 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1326 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1327 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1328 | case BINARY_SUBTRACT: |
| 1329 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1330 | v = TOP(); |
Tim Peters | c1e6d96 | 2001-10-05 20:21:03 +0000 | [diff] [blame] | 1331 | if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) { |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1332 | /* INLINE: int - int */ |
| 1333 | register long a, b, i; |
Guido van Rossum | cf183ac | 1998-12-04 18:51:36 +0000 | [diff] [blame] | 1334 | a = PyInt_AS_LONG(v); |
| 1335 | b = PyInt_AS_LONG(w); |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1336 | i = a - b; |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1337 | if ((i^a) < 0 && (i^~b) < 0) |
| 1338 | goto slow_sub; |
| 1339 | x = PyInt_FromLong(i); |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1340 | } |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1341 | else { |
| 1342 | slow_sub: |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1343 | x = PyNumber_Subtract(v, w); |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1344 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1345 | Py_DECREF(v); |
| 1346 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1347 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1348 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1349 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1350 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1351 | case BINARY_SUBSCR: |
| 1352 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1353 | v = TOP(); |
Tim Peters | b1c4698 | 2001-10-05 20:41:38 +0000 | [diff] [blame] | 1354 | if (PyList_CheckExact(v) && PyInt_CheckExact(w)) { |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1355 | /* INLINE: list[int] */ |
Neal Norwitz | 814e938 | 2006-03-02 07:54:28 +0000 | [diff] [blame] | 1356 | Py_ssize_t i = PyInt_AsSsize_t(w); |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1357 | if (i < 0) |
Guido van Rossum | fa00e95 | 1998-07-08 15:02:37 +0000 | [diff] [blame] | 1358 | i += PyList_GET_SIZE(v); |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 1359 | if (i >= 0 && i < PyList_GET_SIZE(v)) { |
Guido van Rossum | fa00e95 | 1998-07-08 15:02:37 +0000 | [diff] [blame] | 1360 | x = PyList_GET_ITEM(v, i); |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1361 | Py_INCREF(x); |
| 1362 | } |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 1363 | else |
| 1364 | goto slow_get; |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1365 | } |
| 1366 | else |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 1367 | slow_get: |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1368 | x = PyObject_GetItem(v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1369 | Py_DECREF(v); |
| 1370 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1371 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1372 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1373 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1374 | |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1375 | case BINARY_LSHIFT: |
| 1376 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1377 | v = TOP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1378 | x = PyNumber_Lshift(v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1379 | Py_DECREF(v); |
| 1380 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1381 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1382 | if (x != NULL) continue; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1383 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1384 | |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1385 | case BINARY_RSHIFT: |
| 1386 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1387 | v = TOP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1388 | x = PyNumber_Rshift(v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1389 | Py_DECREF(v); |
| 1390 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1391 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1392 | if (x != NULL) continue; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1393 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1394 | |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1395 | case BINARY_AND: |
| 1396 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1397 | v = TOP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1398 | x = PyNumber_And(v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1399 | Py_DECREF(v); |
| 1400 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1401 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1402 | if (x != NULL) continue; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1403 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1404 | |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1405 | case BINARY_XOR: |
| 1406 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1407 | v = TOP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1408 | x = PyNumber_Xor(v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1409 | Py_DECREF(v); |
| 1410 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1411 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1412 | if (x != NULL) continue; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1413 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1414 | |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1415 | case BINARY_OR: |
| 1416 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1417 | v = TOP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1418 | x = PyNumber_Or(v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1419 | Py_DECREF(v); |
| 1420 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1421 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1422 | if (x != NULL) continue; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1423 | break; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1424 | |
Raymond Hettinger | dd80f76 | 2004-03-07 07:31:06 +0000 | [diff] [blame] | 1425 | case LIST_APPEND: |
| 1426 | w = POP(); |
Benjamin Peterson | 8f7b94e | 2009-06-28 16:14:07 +0000 | [diff] [blame] | 1427 | v = PEEK(oparg); |
Raymond Hettinger | dd80f76 | 2004-03-07 07:31:06 +0000 | [diff] [blame] | 1428 | err = PyList_Append(v, w); |
Raymond Hettinger | dd80f76 | 2004-03-07 07:31:06 +0000 | [diff] [blame] | 1429 | Py_DECREF(w); |
Raymond Hettinger | fba1cfc | 2004-03-12 16:33:17 +0000 | [diff] [blame] | 1430 | if (err == 0) { |
| 1431 | PREDICT(JUMP_ABSOLUTE); |
| 1432 | continue; |
| 1433 | } |
Raymond Hettinger | dd80f76 | 2004-03-07 07:31:06 +0000 | [diff] [blame] | 1434 | break; |
| 1435 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1436 | case INPLACE_POWER: |
| 1437 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1438 | v = TOP(); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1439 | x = PyNumber_InPlacePower(v, w, Py_None); |
| 1440 | Py_DECREF(v); |
| 1441 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1442 | SET_TOP(x); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1443 | if (x != NULL) continue; |
| 1444 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1445 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1446 | case INPLACE_MULTIPLY: |
| 1447 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1448 | v = TOP(); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1449 | x = PyNumber_InPlaceMultiply(v, w); |
| 1450 | Py_DECREF(v); |
| 1451 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1452 | SET_TOP(x); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1453 | if (x != NULL) continue; |
| 1454 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1455 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1456 | case INPLACE_DIVIDE: |
Tim Peters | 54b1191 | 2001-12-25 18:49:11 +0000 | [diff] [blame] | 1457 | if (!_Py_QnewFlag) { |
| 1458 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1459 | v = TOP(); |
Tim Peters | 54b1191 | 2001-12-25 18:49:11 +0000 | [diff] [blame] | 1460 | x = PyNumber_InPlaceDivide(v, w); |
| 1461 | Py_DECREF(v); |
| 1462 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1463 | SET_TOP(x); |
Tim Peters | 54b1191 | 2001-12-25 18:49:11 +0000 | [diff] [blame] | 1464 | if (x != NULL) continue; |
| 1465 | break; |
| 1466 | } |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1467 | /* -Qnew is in effect: fall through to |
Tim Peters | 54b1191 | 2001-12-25 18:49:11 +0000 | [diff] [blame] | 1468 | INPLACE_TRUE_DIVIDE */ |
| 1469 | case INPLACE_TRUE_DIVIDE: |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1470 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1471 | v = TOP(); |
Tim Peters | 54b1191 | 2001-12-25 18:49:11 +0000 | [diff] [blame] | 1472 | x = PyNumber_InPlaceTrueDivide(v, w); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1473 | Py_DECREF(v); |
| 1474 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1475 | SET_TOP(x); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1476 | if (x != NULL) continue; |
| 1477 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1478 | |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1479 | case INPLACE_FLOOR_DIVIDE: |
| 1480 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1481 | v = TOP(); |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1482 | x = PyNumber_InPlaceFloorDivide(v, w); |
| 1483 | Py_DECREF(v); |
| 1484 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1485 | SET_TOP(x); |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1486 | if (x != NULL) continue; |
| 1487 | break; |
| 1488 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1489 | case INPLACE_MODULO: |
| 1490 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1491 | v = TOP(); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1492 | x = PyNumber_InPlaceRemainder(v, w); |
| 1493 | Py_DECREF(v); |
| 1494 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1495 | SET_TOP(x); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1496 | if (x != NULL) continue; |
| 1497 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1498 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1499 | case INPLACE_ADD: |
| 1500 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1501 | v = TOP(); |
Tim Peters | c1e6d96 | 2001-10-05 20:21:03 +0000 | [diff] [blame] | 1502 | if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1503 | /* INLINE: int + int */ |
| 1504 | register long a, b, i; |
| 1505 | a = PyInt_AS_LONG(v); |
| 1506 | b = PyInt_AS_LONG(w); |
| 1507 | i = a + b; |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1508 | if ((i^a) < 0 && (i^b) < 0) |
| 1509 | goto slow_iadd; |
| 1510 | x = PyInt_FromLong(i); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1511 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1512 | else if (PyString_CheckExact(v) && |
| 1513 | PyString_CheckExact(w)) { |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 1514 | x = string_concatenate(v, w, f, next_instr); |
| 1515 | /* string_concatenate consumed the ref to v */ |
| 1516 | goto skip_decref_v; |
| 1517 | } |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1518 | else { |
| 1519 | slow_iadd: |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1520 | x = PyNumber_InPlaceAdd(v, w); |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1521 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1522 | Py_DECREF(v); |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 1523 | skip_decref_v: |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1524 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1525 | SET_TOP(x); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1526 | if (x != NULL) continue; |
| 1527 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1528 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1529 | case INPLACE_SUBTRACT: |
| 1530 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1531 | v = TOP(); |
Tim Peters | c1e6d96 | 2001-10-05 20:21:03 +0000 | [diff] [blame] | 1532 | if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1533 | /* INLINE: int - int */ |
| 1534 | register long a, b, i; |
| 1535 | a = PyInt_AS_LONG(v); |
| 1536 | b = PyInt_AS_LONG(w); |
| 1537 | i = a - b; |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1538 | if ((i^a) < 0 && (i^~b) < 0) |
| 1539 | goto slow_isub; |
| 1540 | x = PyInt_FromLong(i); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1541 | } |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1542 | else { |
| 1543 | slow_isub: |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1544 | x = PyNumber_InPlaceSubtract(v, w); |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1545 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1546 | Py_DECREF(v); |
| 1547 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1548 | SET_TOP(x); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1549 | if (x != NULL) continue; |
| 1550 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1551 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1552 | case INPLACE_LSHIFT: |
| 1553 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1554 | v = TOP(); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1555 | x = PyNumber_InPlaceLshift(v, w); |
| 1556 | Py_DECREF(v); |
| 1557 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1558 | SET_TOP(x); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1559 | if (x != NULL) continue; |
| 1560 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1561 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1562 | case INPLACE_RSHIFT: |
| 1563 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1564 | v = TOP(); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1565 | x = PyNumber_InPlaceRshift(v, w); |
| 1566 | Py_DECREF(v); |
| 1567 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1568 | SET_TOP(x); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1569 | if (x != NULL) continue; |
| 1570 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1571 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1572 | case INPLACE_AND: |
| 1573 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1574 | v = TOP(); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1575 | x = PyNumber_InPlaceAnd(v, w); |
| 1576 | Py_DECREF(v); |
| 1577 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1578 | SET_TOP(x); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1579 | if (x != NULL) continue; |
| 1580 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1581 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1582 | case INPLACE_XOR: |
| 1583 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1584 | v = TOP(); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1585 | x = PyNumber_InPlaceXor(v, w); |
| 1586 | Py_DECREF(v); |
| 1587 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1588 | SET_TOP(x); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1589 | if (x != NULL) continue; |
| 1590 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1591 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1592 | case INPLACE_OR: |
| 1593 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1594 | v = TOP(); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1595 | x = PyNumber_InPlaceOr(v, w); |
| 1596 | Py_DECREF(v); |
| 1597 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1598 | SET_TOP(x); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1599 | if (x != NULL) continue; |
| 1600 | break; |
| 1601 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1602 | case SLICE+0: |
| 1603 | case SLICE+1: |
| 1604 | case SLICE+2: |
| 1605 | case SLICE+3: |
| 1606 | if ((opcode-SLICE) & 2) |
| 1607 | w = POP(); |
| 1608 | else |
| 1609 | w = NULL; |
| 1610 | if ((opcode-SLICE) & 1) |
| 1611 | v = POP(); |
| 1612 | else |
| 1613 | v = NULL; |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1614 | u = TOP(); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1615 | x = apply_slice(u, v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1616 | Py_DECREF(u); |
| 1617 | Py_XDECREF(v); |
| 1618 | Py_XDECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1619 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1620 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1621 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1622 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1623 | case STORE_SLICE+0: |
| 1624 | case STORE_SLICE+1: |
| 1625 | case STORE_SLICE+2: |
| 1626 | case STORE_SLICE+3: |
| 1627 | if ((opcode-STORE_SLICE) & 2) |
| 1628 | w = POP(); |
| 1629 | else |
| 1630 | w = NULL; |
| 1631 | if ((opcode-STORE_SLICE) & 1) |
| 1632 | v = POP(); |
| 1633 | else |
| 1634 | v = NULL; |
| 1635 | u = POP(); |
| 1636 | t = POP(); |
| 1637 | err = assign_slice(u, v, w, t); /* u[v:w] = t */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1638 | Py_DECREF(t); |
| 1639 | Py_DECREF(u); |
| 1640 | Py_XDECREF(v); |
| 1641 | Py_XDECREF(w); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1642 | if (err == 0) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1643 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1644 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1645 | case DELETE_SLICE+0: |
| 1646 | case DELETE_SLICE+1: |
| 1647 | case DELETE_SLICE+2: |
| 1648 | case DELETE_SLICE+3: |
| 1649 | if ((opcode-DELETE_SLICE) & 2) |
| 1650 | w = POP(); |
| 1651 | else |
| 1652 | w = NULL; |
| 1653 | if ((opcode-DELETE_SLICE) & 1) |
| 1654 | v = POP(); |
| 1655 | else |
| 1656 | v = NULL; |
| 1657 | u = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1658 | err = assign_slice(u, v, w, (PyObject *)NULL); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1659 | /* del u[v:w] */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1660 | Py_DECREF(u); |
| 1661 | Py_XDECREF(v); |
| 1662 | Py_XDECREF(w); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1663 | if (err == 0) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1664 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1665 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1666 | case STORE_SUBSCR: |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1667 | w = TOP(); |
| 1668 | v = SECOND(); |
| 1669 | u = THIRD(); |
| 1670 | STACKADJ(-3); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1671 | /* v[w] = u */ |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1672 | err = PyObject_SetItem(v, w, u); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1673 | Py_DECREF(u); |
| 1674 | Py_DECREF(v); |
| 1675 | Py_DECREF(w); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1676 | if (err == 0) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1677 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1678 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1679 | case DELETE_SUBSCR: |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1680 | w = TOP(); |
| 1681 | v = SECOND(); |
| 1682 | STACKADJ(-2); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1683 | /* del v[w] */ |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1684 | err = PyObject_DelItem(v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1685 | Py_DECREF(v); |
| 1686 | Py_DECREF(w); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1687 | if (err == 0) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1688 | break; |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1689 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1690 | case PRINT_EXPR: |
| 1691 | v = POP(); |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 1692 | w = PySys_GetObject("displayhook"); |
| 1693 | if (w == NULL) { |
| 1694 | PyErr_SetString(PyExc_RuntimeError, |
| 1695 | "lost sys.displayhook"); |
| 1696 | err = -1; |
Moshe Zadka | f5df383 | 2001-01-11 11:55:37 +0000 | [diff] [blame] | 1697 | x = NULL; |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 1698 | } |
| 1699 | if (err == 0) { |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 1700 | x = PyTuple_Pack(1, v); |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 1701 | if (x == NULL) |
| 1702 | err = -1; |
| 1703 | } |
| 1704 | if (err == 0) { |
| 1705 | w = PyEval_CallObject(w, x); |
Moshe Zadka | f5df383 | 2001-01-11 11:55:37 +0000 | [diff] [blame] | 1706 | Py_XDECREF(w); |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 1707 | if (w == NULL) |
| 1708 | err = -1; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1709 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1710 | Py_DECREF(v); |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 1711 | Py_XDECREF(x); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1712 | break; |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 1713 | |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1714 | case PRINT_ITEM_TO: |
| 1715 | w = stream = POP(); |
| 1716 | /* fall through to PRINT_ITEM */ |
| 1717 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1718 | case PRINT_ITEM: |
| 1719 | v = POP(); |
Barry Warsaw | 093abe0 | 2000-08-29 04:56:13 +0000 | [diff] [blame] | 1720 | if (stream == NULL || stream == Py_None) { |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1721 | w = PySys_GetObject("stdout"); |
| 1722 | if (w == NULL) { |
| 1723 | PyErr_SetString(PyExc_RuntimeError, |
| 1724 | "lost sys.stdout"); |
| 1725 | err = -1; |
| 1726 | } |
Guido van Rossum | 8f18320 | 1997-12-31 05:53:15 +0000 | [diff] [blame] | 1727 | } |
Neal Norwitz | c5131bc | 2003-06-29 14:48:32 +0000 | [diff] [blame] | 1728 | /* PyFile_SoftSpace() can exececute arbitrary code |
| 1729 | if sys.stdout is an instance with a __getattr__. |
| 1730 | If __getattr__ raises an exception, w will |
| 1731 | be freed, so we need to prevent that temporarily. */ |
| 1732 | Py_XINCREF(w); |
Tim Peters | 8e5fd53 | 2002-03-24 19:25:00 +0000 | [diff] [blame] | 1733 | if (w != NULL && PyFile_SoftSpace(w, 0)) |
Guido van Rossum | be27026 | 1997-05-22 22:26:18 +0000 | [diff] [blame] | 1734 | err = PyFile_WriteString(" ", w); |
| 1735 | if (err == 0) |
| 1736 | err = PyFile_WriteObject(v, w, Py_PRINT_RAW); |
Marc-André Lemburg | 0c4d8d0 | 2001-11-20 15:17:25 +0000 | [diff] [blame] | 1737 | if (err == 0) { |
Tim Peters | 8e5fd53 | 2002-03-24 19:25:00 +0000 | [diff] [blame] | 1738 | /* XXX move into writeobject() ? */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1739 | if (PyString_Check(v)) { |
| 1740 | char *s = PyString_AS_STRING(v); |
| 1741 | Py_ssize_t len = PyString_GET_SIZE(v); |
Tim Peters | 8e5fd53 | 2002-03-24 19:25:00 +0000 | [diff] [blame] | 1742 | if (len == 0 || |
| 1743 | !isspace(Py_CHARMASK(s[len-1])) || |
| 1744 | s[len-1] == ' ') |
| 1745 | PyFile_SoftSpace(w, 1); |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 1746 | } |
Martin v. Löwis | 8d3ce5a | 2001-12-18 22:36:40 +0000 | [diff] [blame] | 1747 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | 0c4d8d0 | 2001-11-20 15:17:25 +0000 | [diff] [blame] | 1748 | else if (PyUnicode_Check(v)) { |
| 1749 | Py_UNICODE *s = PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 6685128 | 2006-04-22 11:40:03 +0000 | [diff] [blame] | 1750 | Py_ssize_t len = PyUnicode_GET_SIZE(v); |
Tim Peters | 8e5fd53 | 2002-03-24 19:25:00 +0000 | [diff] [blame] | 1751 | if (len == 0 || |
| 1752 | !Py_UNICODE_ISSPACE(s[len-1]) || |
| 1753 | s[len-1] == ' ') |
| 1754 | PyFile_SoftSpace(w, 1); |
Marc-André Lemburg | 0c4d8d0 | 2001-11-20 15:17:25 +0000 | [diff] [blame] | 1755 | } |
Michael W. Hudson | d95c828 | 2002-05-20 13:56:11 +0000 | [diff] [blame] | 1756 | #endif |
Tim Peters | 8e5fd53 | 2002-03-24 19:25:00 +0000 | [diff] [blame] | 1757 | else |
| 1758 | PyFile_SoftSpace(w, 1); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1759 | } |
Neal Norwitz | c5131bc | 2003-06-29 14:48:32 +0000 | [diff] [blame] | 1760 | Py_XDECREF(w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1761 | Py_DECREF(v); |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1762 | Py_XDECREF(stream); |
| 1763 | stream = NULL; |
| 1764 | if (err == 0) |
| 1765 | continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1766 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1767 | |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1768 | case PRINT_NEWLINE_TO: |
| 1769 | w = stream = POP(); |
| 1770 | /* fall through to PRINT_NEWLINE */ |
| 1771 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1772 | case PRINT_NEWLINE: |
Barry Warsaw | 093abe0 | 2000-08-29 04:56:13 +0000 | [diff] [blame] | 1773 | if (stream == NULL || stream == Py_None) { |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1774 | w = PySys_GetObject("stdout"); |
Jeffrey Yasskin | 2d873bd | 2008-12-08 18:55:24 +0000 | [diff] [blame] | 1775 | if (w == NULL) { |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1776 | PyErr_SetString(PyExc_RuntimeError, |
| 1777 | "lost sys.stdout"); |
Jeffrey Yasskin | 2d873bd | 2008-12-08 18:55:24 +0000 | [diff] [blame] | 1778 | why = WHY_EXCEPTION; |
| 1779 | } |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1780 | } |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1781 | if (w != NULL) { |
Georg Brandl | aa76d77 | 2008-07-01 20:56:03 +0000 | [diff] [blame] | 1782 | /* w.write() may replace sys.stdout, so we |
| 1783 | * have to keep our reference to it */ |
Amaury Forgeot d'Arc | bdd941f | 2008-07-01 20:38:04 +0000 | [diff] [blame] | 1784 | Py_INCREF(w); |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1785 | err = PyFile_WriteString("\n", w); |
| 1786 | if (err == 0) |
| 1787 | PyFile_SoftSpace(w, 0); |
Amaury Forgeot d'Arc | bdd941f | 2008-07-01 20:38:04 +0000 | [diff] [blame] | 1788 | Py_DECREF(w); |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1789 | } |
| 1790 | Py_XDECREF(stream); |
| 1791 | stream = NULL; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1792 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1793 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1794 | |
| 1795 | #ifdef CASE_TOO_BIG |
| 1796 | default: switch (opcode) { |
| 1797 | #endif |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1798 | case RAISE_VARARGS: |
| 1799 | u = v = w = NULL; |
| 1800 | switch (oparg) { |
| 1801 | case 3: |
| 1802 | u = POP(); /* traceback */ |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1803 | /* Fallthrough */ |
| 1804 | case 2: |
| 1805 | v = POP(); /* value */ |
| 1806 | /* Fallthrough */ |
| 1807 | case 1: |
| 1808 | w = POP(); /* exc */ |
Guido van Rossum | d295f12 | 1998-04-09 21:39:57 +0000 | [diff] [blame] | 1809 | case 0: /* Fallthrough */ |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 1810 | why = do_raise(w, v, u); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1811 | break; |
| 1812 | default: |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1813 | PyErr_SetString(PyExc_SystemError, |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1814 | "bad RAISE_VARARGS oparg"); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1815 | why = WHY_EXCEPTION; |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 1816 | break; |
| 1817 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1818 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1819 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1820 | case LOAD_LOCALS: |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 1821 | if ((x = f->f_locals) != NULL) { |
| 1822 | Py_INCREF(x); |
| 1823 | PUSH(x); |
| 1824 | continue; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1825 | } |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 1826 | PyErr_SetString(PyExc_SystemError, "no locals"); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1827 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1828 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1829 | case RETURN_VALUE: |
| 1830 | retval = POP(); |
| 1831 | why = WHY_RETURN; |
Raymond Hettinger | 1dd8309 | 2004-02-06 18:32:33 +0000 | [diff] [blame] | 1832 | goto fast_block_end; |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 1833 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1834 | case YIELD_VALUE: |
| 1835 | retval = POP(); |
Tim Peters | 8c96369 | 2001-06-23 05:26:56 +0000 | [diff] [blame] | 1836 | f->f_stacktop = stack_pointer; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1837 | why = WHY_YIELD; |
Raymond Hettinger | 1dd8309 | 2004-02-06 18:32:33 +0000 | [diff] [blame] | 1838 | goto fast_yield; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1839 | |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 1840 | case EXEC_STMT: |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1841 | w = TOP(); |
| 1842 | v = SECOND(); |
| 1843 | u = THIRD(); |
| 1844 | STACKADJ(-3); |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 1845 | READ_TIMESTAMP(intr0); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 1846 | err = exec_statement(f, u, v, w); |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 1847 | READ_TIMESTAMP(intr1); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1848 | Py_DECREF(u); |
| 1849 | Py_DECREF(v); |
| 1850 | Py_DECREF(w); |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 1851 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1852 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1853 | case POP_BLOCK: |
| 1854 | { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1855 | PyTryBlock *b = PyFrame_BlockPop(f); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1856 | while (STACK_LEVEL() > b->b_level) { |
| 1857 | v = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1858 | Py_DECREF(v); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1859 | } |
| 1860 | } |
Raymond Hettinger | 7eddd78 | 2004-04-07 14:38:08 +0000 | [diff] [blame] | 1861 | continue; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1862 | |
Jeffrey Yasskin | 9063a99 | 2008-03-03 01:27:03 +0000 | [diff] [blame] | 1863 | PREDICTED(END_FINALLY); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1864 | case END_FINALLY: |
| 1865 | v = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1866 | if (PyInt_Check(v)) { |
Raymond Hettinger | 7c95865 | 2004-04-06 10:11:10 +0000 | [diff] [blame] | 1867 | why = (enum why_code) PyInt_AS_LONG(v); |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 1868 | assert(why != WHY_YIELD); |
Raymond Hettinger | c8aa08b | 2004-04-11 14:59:33 +0000 | [diff] [blame] | 1869 | if (why == WHY_RETURN || |
| 1870 | why == WHY_CONTINUE) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1871 | retval = POP(); |
| 1872 | } |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 1873 | else if (PyExceptionClass_Check(v) || |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1874 | PyString_Check(v)) { |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1875 | w = POP(); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1876 | u = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1877 | PyErr_Restore(v, w, u); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1878 | why = WHY_RERAISE; |
Guido van Rossum | 0db1ef9 | 1995-07-28 23:06:00 +0000 | [diff] [blame] | 1879 | break; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1880 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1881 | else if (v != Py_None) { |
| 1882 | PyErr_SetString(PyExc_SystemError, |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1883 | "'finally' pops bad exception"); |
| 1884 | why = WHY_EXCEPTION; |
| 1885 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1886 | Py_DECREF(v); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1887 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1888 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1889 | case BUILD_CLASS: |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1890 | u = TOP(); |
| 1891 | v = SECOND(); |
| 1892 | w = THIRD(); |
| 1893 | STACKADJ(-2); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1894 | x = build_class(u, v, w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1895 | SET_TOP(x); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1896 | Py_DECREF(u); |
| 1897 | Py_DECREF(v); |
| 1898 | Py_DECREF(w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1899 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1900 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1901 | case STORE_NAME: |
Skip Montanaro | 496e658 | 2002-08-06 17:47:40 +0000 | [diff] [blame] | 1902 | w = GETITEM(names, oparg); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1903 | v = POP(); |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 1904 | if ((x = f->f_locals) != NULL) { |
Raymond Hettinger | 66bd233 | 2004-08-02 08:30:07 +0000 | [diff] [blame] | 1905 | if (PyDict_CheckExact(x)) |
Raymond Hettinger | 214b1c3 | 2004-07-02 06:41:07 +0000 | [diff] [blame] | 1906 | err = PyDict_SetItem(x, w, v); |
| 1907 | else |
| 1908 | err = PyObject_SetItem(x, w, v); |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 1909 | Py_DECREF(v); |
Raymond Hettinger | 7eddd78 | 2004-04-07 14:38:08 +0000 | [diff] [blame] | 1910 | if (err == 0) continue; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1911 | break; |
| 1912 | } |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 1913 | PyErr_Format(PyExc_SystemError, |
| 1914 | "no locals found when storing %s", |
| 1915 | PyObject_REPR(w)); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1916 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1917 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1918 | case DELETE_NAME: |
Skip Montanaro | 496e658 | 2002-08-06 17:47:40 +0000 | [diff] [blame] | 1919 | w = GETITEM(names, oparg); |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 1920 | if ((x = f->f_locals) != NULL) { |
Raymond Hettinger | 214b1c3 | 2004-07-02 06:41:07 +0000 | [diff] [blame] | 1921 | if ((err = PyObject_DelItem(x, w)) != 0) |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 1922 | format_exc_check_arg(PyExc_NameError, |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 1923 | NAME_ERROR_MSG, |
| 1924 | w); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1925 | break; |
| 1926 | } |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 1927 | PyErr_Format(PyExc_SystemError, |
| 1928 | "no locals when deleting %s", |
| 1929 | PyObject_REPR(w)); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1930 | break; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1931 | |
Raymond Hettinger | 7dc5221 | 2003-03-16 20:14:44 +0000 | [diff] [blame] | 1932 | PREDICTED_WITH_ARG(UNPACK_SEQUENCE); |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 1933 | case UNPACK_SEQUENCE: |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1934 | v = POP(); |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 1935 | if (PyTuple_CheckExact(v) && |
| 1936 | PyTuple_GET_SIZE(v) == oparg) { |
| 1937 | PyObject **items = \ |
| 1938 | ((PyTupleObject *)v)->ob_item; |
Raymond Hettinger | f114a3a | 2004-03-08 23:25:30 +0000 | [diff] [blame] | 1939 | while (oparg--) { |
| 1940 | w = items[oparg]; |
| 1941 | Py_INCREF(w); |
| 1942 | PUSH(w); |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 1943 | } |
Raymond Hettinger | 7eddd78 | 2004-04-07 14:38:08 +0000 | [diff] [blame] | 1944 | Py_DECREF(v); |
| 1945 | continue; |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 1946 | } else if (PyList_CheckExact(v) && |
| 1947 | PyList_GET_SIZE(v) == oparg) { |
| 1948 | PyObject **items = \ |
| 1949 | ((PyListObject *)v)->ob_item; |
Raymond Hettinger | f114a3a | 2004-03-08 23:25:30 +0000 | [diff] [blame] | 1950 | while (oparg--) { |
| 1951 | w = items[oparg]; |
| 1952 | Py_INCREF(w); |
| 1953 | PUSH(w); |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 1954 | } |
Raymond Hettinger | f114a3a | 2004-03-08 23:25:30 +0000 | [diff] [blame] | 1955 | } else if (unpack_iterable(v, oparg, |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 1956 | stack_pointer + oparg)) { |
Benjamin Peterson | 8f7b94e | 2009-06-28 16:14:07 +0000 | [diff] [blame] | 1957 | STACKADJ(oparg); |
Georg Brandl | 5cb76c1 | 2007-03-21 09:00:39 +0000 | [diff] [blame] | 1958 | } else { |
| 1959 | /* unpack_iterable() raised an exception */ |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 1960 | why = WHY_EXCEPTION; |
Tim Peters | 8b13b3e | 2001-09-30 05:58:42 +0000 | [diff] [blame] | 1961 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1962 | Py_DECREF(v); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1963 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1964 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1965 | case STORE_ATTR: |
Skip Montanaro | 496e658 | 2002-08-06 17:47:40 +0000 | [diff] [blame] | 1966 | w = GETITEM(names, oparg); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 1967 | v = TOP(); |
| 1968 | u = SECOND(); |
| 1969 | STACKADJ(-2); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1970 | err = PyObject_SetAttr(v, w, u); /* v.w = u */ |
| 1971 | Py_DECREF(v); |
| 1972 | Py_DECREF(u); |
Raymond Hettinger | 7eddd78 | 2004-04-07 14:38:08 +0000 | [diff] [blame] | 1973 | if (err == 0) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1974 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1975 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1976 | case DELETE_ATTR: |
Skip Montanaro | 496e658 | 2002-08-06 17:47:40 +0000 | [diff] [blame] | 1977 | w = GETITEM(names, oparg); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1978 | v = POP(); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 1979 | err = PyObject_SetAttr(v, w, (PyObject *)NULL); |
| 1980 | /* del v.w */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1981 | Py_DECREF(v); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1982 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1983 | |
Guido van Rossum | 32c6cdf | 1991-12-10 13:52:46 +0000 | [diff] [blame] | 1984 | case STORE_GLOBAL: |
Skip Montanaro | 496e658 | 2002-08-06 17:47:40 +0000 | [diff] [blame] | 1985 | w = GETITEM(names, oparg); |
Guido van Rossum | 32c6cdf | 1991-12-10 13:52:46 +0000 | [diff] [blame] | 1986 | v = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1987 | err = PyDict_SetItem(f->f_globals, w, v); |
| 1988 | Py_DECREF(v); |
Raymond Hettinger | 7eddd78 | 2004-04-07 14:38:08 +0000 | [diff] [blame] | 1989 | if (err == 0) continue; |
Guido van Rossum | 32c6cdf | 1991-12-10 13:52:46 +0000 | [diff] [blame] | 1990 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1991 | |
Guido van Rossum | 32c6cdf | 1991-12-10 13:52:46 +0000 | [diff] [blame] | 1992 | case DELETE_GLOBAL: |
Skip Montanaro | 496e658 | 2002-08-06 17:47:40 +0000 | [diff] [blame] | 1993 | w = GETITEM(names, oparg); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1994 | if ((err = PyDict_DelItem(f->f_globals, w)) != 0) |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 1995 | format_exc_check_arg( |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1996 | PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w); |
Guido van Rossum | 32c6cdf | 1991-12-10 13:52:46 +0000 | [diff] [blame] | 1997 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1998 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1999 | case LOAD_NAME: |
Skip Montanaro | 496e658 | 2002-08-06 17:47:40 +0000 | [diff] [blame] | 2000 | w = GETITEM(names, oparg); |
Raymond Hettinger | 214b1c3 | 2004-07-02 06:41:07 +0000 | [diff] [blame] | 2001 | if ((v = f->f_locals) == NULL) { |
Jeremy Hylton | c862cf4 | 2001-01-19 03:25:05 +0000 | [diff] [blame] | 2002 | PyErr_Format(PyExc_SystemError, |
| 2003 | "no locals when loading %s", |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 2004 | PyObject_REPR(w)); |
Jeffrey Yasskin | 2d873bd | 2008-12-08 18:55:24 +0000 | [diff] [blame] | 2005 | why = WHY_EXCEPTION; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2006 | break; |
| 2007 | } |
Michael W. Hudson | a3711f7 | 2004-08-02 14:50:43 +0000 | [diff] [blame] | 2008 | if (PyDict_CheckExact(v)) { |
Raymond Hettinger | 214b1c3 | 2004-07-02 06:41:07 +0000 | [diff] [blame] | 2009 | x = PyDict_GetItem(v, w); |
Michael W. Hudson | a3711f7 | 2004-08-02 14:50:43 +0000 | [diff] [blame] | 2010 | Py_XINCREF(x); |
| 2011 | } |
Raymond Hettinger | 214b1c3 | 2004-07-02 06:41:07 +0000 | [diff] [blame] | 2012 | else { |
| 2013 | x = PyObject_GetItem(v, w); |
| 2014 | if (x == NULL && PyErr_Occurred()) { |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 2015 | if (!PyErr_ExceptionMatches( |
| 2016 | PyExc_KeyError)) |
Raymond Hettinger | 214b1c3 | 2004-07-02 06:41:07 +0000 | [diff] [blame] | 2017 | break; |
| 2018 | PyErr_Clear(); |
| 2019 | } |
| 2020 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2021 | if (x == NULL) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2022 | x = PyDict_GetItem(f->f_globals, w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2023 | if (x == NULL) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2024 | x = PyDict_GetItem(f->f_builtins, w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2025 | if (x == NULL) { |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 2026 | format_exc_check_arg( |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2027 | PyExc_NameError, |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 2028 | NAME_ERROR_MSG, w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2029 | break; |
| 2030 | } |
| 2031 | } |
Michael W. Hudson | a3711f7 | 2004-08-02 14:50:43 +0000 | [diff] [blame] | 2032 | Py_INCREF(x); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2033 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2034 | PUSH(x); |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 2035 | continue; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2036 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2037 | case LOAD_GLOBAL: |
Skip Montanaro | 496e658 | 2002-08-06 17:47:40 +0000 | [diff] [blame] | 2038 | w = GETITEM(names, oparg); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2039 | if (PyString_CheckExact(w)) { |
Guido van Rossum | d8dbf84 | 2002-08-19 21:17:53 +0000 | [diff] [blame] | 2040 | /* Inline the PyDict_GetItem() calls. |
| 2041 | WARNING: this is an extreme speed hack. |
| 2042 | Do not try this at home. */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2043 | long hash = ((PyStringObject *)w)->ob_shash; |
Guido van Rossum | 3a4dfc8 | 2002-08-19 20:24:07 +0000 | [diff] [blame] | 2044 | if (hash != -1) { |
Guido van Rossum | 3a4dfc8 | 2002-08-19 20:24:07 +0000 | [diff] [blame] | 2045 | PyDictObject *d; |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 2046 | PyDictEntry *e; |
Guido van Rossum | 3a4dfc8 | 2002-08-19 20:24:07 +0000 | [diff] [blame] | 2047 | d = (PyDictObject *)(f->f_globals); |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 2048 | e = d->ma_lookup(d, w, hash); |
| 2049 | if (e == NULL) { |
| 2050 | x = NULL; |
| 2051 | break; |
| 2052 | } |
| 2053 | x = e->me_value; |
Guido van Rossum | 3a4dfc8 | 2002-08-19 20:24:07 +0000 | [diff] [blame] | 2054 | if (x != NULL) { |
| 2055 | Py_INCREF(x); |
| 2056 | PUSH(x); |
| 2057 | continue; |
| 2058 | } |
| 2059 | d = (PyDictObject *)(f->f_builtins); |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 2060 | e = d->ma_lookup(d, w, hash); |
| 2061 | if (e == NULL) { |
| 2062 | x = NULL; |
| 2063 | break; |
| 2064 | } |
| 2065 | x = e->me_value; |
Guido van Rossum | 3a4dfc8 | 2002-08-19 20:24:07 +0000 | [diff] [blame] | 2066 | if (x != NULL) { |
| 2067 | Py_INCREF(x); |
| 2068 | PUSH(x); |
| 2069 | continue; |
| 2070 | } |
| 2071 | goto load_global_error; |
| 2072 | } |
| 2073 | } |
| 2074 | /* This is the un-inlined version of the code above */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2075 | x = PyDict_GetItem(f->f_globals, w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2076 | if (x == NULL) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2077 | x = PyDict_GetItem(f->f_builtins, w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2078 | if (x == NULL) { |
Guido van Rossum | 3a4dfc8 | 2002-08-19 20:24:07 +0000 | [diff] [blame] | 2079 | load_global_error: |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 2080 | format_exc_check_arg( |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2081 | PyExc_NameError, |
Guido van Rossum | 3a4dfc8 | 2002-08-19 20:24:07 +0000 | [diff] [blame] | 2082 | GLOBAL_NAME_ERROR_MSG, w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2083 | break; |
| 2084 | } |
| 2085 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2086 | Py_INCREF(x); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2087 | PUSH(x); |
Raymond Hettinger | 7eddd78 | 2004-04-07 14:38:08 +0000 | [diff] [blame] | 2088 | continue; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2089 | |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2090 | case DELETE_FAST: |
Guido van Rossum | 2e4c899 | 1998-05-12 20:27:36 +0000 | [diff] [blame] | 2091 | x = GETLOCAL(oparg); |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 2092 | if (x != NULL) { |
| 2093 | SETLOCAL(oparg, NULL); |
| 2094 | continue; |
Guido van Rossum | 2e4c899 | 1998-05-12 20:27:36 +0000 | [diff] [blame] | 2095 | } |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 2096 | format_exc_check_arg( |
| 2097 | PyExc_UnboundLocalError, |
| 2098 | UNBOUNDLOCAL_ERROR_MSG, |
| 2099 | PyTuple_GetItem(co->co_varnames, oparg) |
| 2100 | ); |
| 2101 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2102 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2103 | case LOAD_CLOSURE: |
Jeremy Hylton | 2b724da | 2001-01-29 22:51:52 +0000 | [diff] [blame] | 2104 | x = freevars[oparg]; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2105 | Py_INCREF(x); |
| 2106 | PUSH(x); |
Raymond Hettinger | 7eddd78 | 2004-04-07 14:38:08 +0000 | [diff] [blame] | 2107 | if (x != NULL) continue; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2108 | break; |
| 2109 | |
| 2110 | case LOAD_DEREF: |
Jeremy Hylton | 2b724da | 2001-01-29 22:51:52 +0000 | [diff] [blame] | 2111 | x = freevars[oparg]; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2112 | w = PyCell_Get(x); |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 2113 | if (w != NULL) { |
| 2114 | PUSH(w); |
| 2115 | continue; |
Jeremy Hylton | 2524d69 | 2001-02-05 17:23:16 +0000 | [diff] [blame] | 2116 | } |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 2117 | err = -1; |
| 2118 | /* Don't stomp existing exception */ |
| 2119 | if (PyErr_Occurred()) |
| 2120 | break; |
Richard Jones | cebbefc | 2006-05-23 18:28:17 +0000 | [diff] [blame] | 2121 | if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) { |
| 2122 | v = PyTuple_GET_ITEM(co->co_cellvars, |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 2123 | oparg); |
| 2124 | format_exc_check_arg( |
| 2125 | PyExc_UnboundLocalError, |
| 2126 | UNBOUNDLOCAL_ERROR_MSG, |
| 2127 | v); |
| 2128 | } else { |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 2129 | v = PyTuple_GET_ITEM(co->co_freevars, oparg - |
| 2130 | PyTuple_GET_SIZE(co->co_cellvars)); |
| 2131 | format_exc_check_arg(PyExc_NameError, |
| 2132 | UNBOUNDFREE_ERROR_MSG, v); |
Raymond Hettinger | 467a698 | 2004-04-07 11:39:21 +0000 | [diff] [blame] | 2133 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2134 | break; |
| 2135 | |
| 2136 | case STORE_DEREF: |
| 2137 | w = POP(); |
Jeremy Hylton | 2b724da | 2001-01-29 22:51:52 +0000 | [diff] [blame] | 2138 | x = freevars[oparg]; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2139 | PyCell_Set(x, w); |
Jeremy Hylton | 30c9f39 | 2001-03-13 01:58:22 +0000 | [diff] [blame] | 2140 | Py_DECREF(w); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2141 | continue; |
| 2142 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2143 | case BUILD_TUPLE: |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2144 | x = PyTuple_New(oparg); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2145 | if (x != NULL) { |
Raymond Hettinger | 5bed456 | 2004-04-10 23:34:17 +0000 | [diff] [blame] | 2146 | for (; --oparg >= 0;) { |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2147 | w = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2148 | PyTuple_SET_ITEM(x, oparg, w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2149 | } |
| 2150 | PUSH(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 2151 | continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2152 | } |
| 2153 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2154 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2155 | case BUILD_LIST: |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2156 | x = PyList_New(oparg); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2157 | if (x != NULL) { |
Raymond Hettinger | 5bed456 | 2004-04-10 23:34:17 +0000 | [diff] [blame] | 2158 | for (; --oparg >= 0;) { |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2159 | w = POP(); |
Guido van Rossum | 5053efc | 1998-08-04 15:27:50 +0000 | [diff] [blame] | 2160 | PyList_SET_ITEM(x, oparg, w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2161 | } |
| 2162 | PUSH(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 2163 | continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2164 | } |
| 2165 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2166 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2167 | case BUILD_MAP: |
Raymond Hettinger | fd7ed40 | 2007-12-18 21:24:09 +0000 | [diff] [blame] | 2168 | x = _PyDict_NewPresized((Py_ssize_t)oparg); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2169 | PUSH(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 2170 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2171 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2172 | |
Raymond Hettinger | effde12 | 2007-12-18 18:26:18 +0000 | [diff] [blame] | 2173 | case STORE_MAP: |
| 2174 | w = TOP(); /* key */ |
| 2175 | u = SECOND(); /* value */ |
| 2176 | v = THIRD(); /* dict */ |
| 2177 | STACKADJ(-2); |
| 2178 | assert (PyDict_CheckExact(v)); |
| 2179 | err = PyDict_SetItem(v, w, u); /* v[w] = u */ |
| 2180 | Py_DECREF(u); |
| 2181 | Py_DECREF(w); |
| 2182 | if (err == 0) continue; |
| 2183 | break; |
| 2184 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2185 | case LOAD_ATTR: |
Skip Montanaro | 496e658 | 2002-08-06 17:47:40 +0000 | [diff] [blame] | 2186 | w = GETITEM(names, oparg); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 2187 | v = TOP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2188 | x = PyObject_GetAttr(v, w); |
| 2189 | Py_DECREF(v); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 2190 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 2191 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2192 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2193 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2194 | case COMPARE_OP: |
| 2195 | w = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 2196 | v = TOP(); |
Raymond Hettinger | 4bad9ba | 2003-01-19 05:08:13 +0000 | [diff] [blame] | 2197 | if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) { |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 2198 | /* INLINE: cmp(int, int) */ |
| 2199 | register long a, b; |
| 2200 | register int res; |
Guido van Rossum | cf183ac | 1998-12-04 18:51:36 +0000 | [diff] [blame] | 2201 | a = PyInt_AS_LONG(v); |
| 2202 | b = PyInt_AS_LONG(w); |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 2203 | switch (oparg) { |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 2204 | case PyCmp_LT: res = a < b; break; |
| 2205 | case PyCmp_LE: res = a <= b; break; |
| 2206 | case PyCmp_EQ: res = a == b; break; |
| 2207 | case PyCmp_NE: res = a != b; break; |
| 2208 | case PyCmp_GT: res = a > b; break; |
| 2209 | case PyCmp_GE: res = a >= b; break; |
| 2210 | case PyCmp_IS: res = v == w; break; |
| 2211 | case PyCmp_IS_NOT: res = v != w; break; |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 2212 | default: goto slow_compare; |
| 2213 | } |
| 2214 | x = res ? Py_True : Py_False; |
| 2215 | Py_INCREF(x); |
| 2216 | } |
| 2217 | else { |
| 2218 | slow_compare: |
| 2219 | x = cmp_outcome(oparg, v, w); |
| 2220 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2221 | Py_DECREF(v); |
| 2222 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 2223 | SET_TOP(x); |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 2224 | if (x == NULL) break; |
Jeffrey Yasskin | 68d6852 | 2009-02-28 19:03:21 +0000 | [diff] [blame] | 2225 | PREDICT(POP_JUMP_IF_FALSE); |
| 2226 | PREDICT(POP_JUMP_IF_TRUE); |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 2227 | continue; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2228 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2229 | case IMPORT_NAME: |
Skip Montanaro | 496e658 | 2002-08-06 17:47:40 +0000 | [diff] [blame] | 2230 | w = GETITEM(names, oparg); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2231 | x = PyDict_GetItemString(f->f_builtins, "__import__"); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2232 | if (x == NULL) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2233 | PyErr_SetString(PyExc_ImportError, |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 2234 | "__import__ not found"); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2235 | break; |
| 2236 | } |
Guido van Rossum | 1d9a9ea | 2008-01-23 20:19:01 +0000 | [diff] [blame] | 2237 | Py_INCREF(x); |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 2238 | v = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 2239 | u = TOP(); |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 2240 | if (PyInt_AsLong(u) != -1 || PyErr_Occurred()) |
| 2241 | w = PyTuple_Pack(5, |
| 2242 | w, |
| 2243 | f->f_globals, |
| 2244 | f->f_locals == NULL ? |
| 2245 | Py_None : f->f_locals, |
| 2246 | v, |
| 2247 | u); |
| 2248 | else |
| 2249 | w = PyTuple_Pack(4, |
| 2250 | w, |
| 2251 | f->f_globals, |
| 2252 | f->f_locals == NULL ? |
| 2253 | Py_None : f->f_locals, |
| 2254 | v); |
| 2255 | Py_DECREF(v); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2256 | Py_DECREF(u); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2257 | if (w == NULL) { |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 2258 | u = POP(); |
Guido van Rossum | 1d9a9ea | 2008-01-23 20:19:01 +0000 | [diff] [blame] | 2259 | Py_DECREF(x); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2260 | x = NULL; |
| 2261 | break; |
| 2262 | } |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 2263 | READ_TIMESTAMP(intr0); |
Guido van Rossum | 1d9a9ea | 2008-01-23 20:19:01 +0000 | [diff] [blame] | 2264 | v = x; |
| 2265 | x = PyEval_CallObject(v, w); |
| 2266 | Py_DECREF(v); |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 2267 | READ_TIMESTAMP(intr1); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2268 | Py_DECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 2269 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 2270 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2271 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2272 | |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2273 | case IMPORT_STAR: |
| 2274 | v = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2275 | PyFrame_FastToLocals(f); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2276 | if ((x = f->f_locals) == NULL) { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2277 | PyErr_SetString(PyExc_SystemError, |
Jeremy Hylton | c862cf4 | 2001-01-19 03:25:05 +0000 | [diff] [blame] | 2278 | "no locals found during 'import *'"); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2279 | break; |
| 2280 | } |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 2281 | READ_TIMESTAMP(intr0); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2282 | err = import_all_from(x, v); |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 2283 | READ_TIMESTAMP(intr1); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2284 | PyFrame_LocalsToFast(f, 0); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2285 | Py_DECREF(v); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 2286 | if (err == 0) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2287 | break; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2288 | |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2289 | case IMPORT_FROM: |
Skip Montanaro | 496e658 | 2002-08-06 17:47:40 +0000 | [diff] [blame] | 2290 | w = GETITEM(names, oparg); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2291 | v = TOP(); |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 2292 | READ_TIMESTAMP(intr0); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2293 | x = import_from(v, w); |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 2294 | READ_TIMESTAMP(intr1); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2295 | PUSH(x); |
| 2296 | if (x != NULL) continue; |
| 2297 | break; |
| 2298 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2299 | case JUMP_FORWARD: |
| 2300 | JUMPBY(oparg); |
Neil Schemenauer | c4b570f | 2003-06-01 19:21:12 +0000 | [diff] [blame] | 2301 | goto fast_next_opcode; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2302 | |
Jeffrey Yasskin | 68d6852 | 2009-02-28 19:03:21 +0000 | [diff] [blame] | 2303 | PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE); |
| 2304 | case POP_JUMP_IF_FALSE: |
| 2305 | w = POP(); |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 2306 | if (w == Py_True) { |
Jeffrey Yasskin | 68d6852 | 2009-02-28 19:03:21 +0000 | [diff] [blame] | 2307 | Py_DECREF(w); |
Neil Schemenauer | c4b570f | 2003-06-01 19:21:12 +0000 | [diff] [blame] | 2308 | goto fast_next_opcode; |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 2309 | } |
Raymond Hettinger | 21012b8 | 2003-02-26 18:11:50 +0000 | [diff] [blame] | 2310 | if (w == Py_False) { |
Jeffrey Yasskin | 68d6852 | 2009-02-28 19:03:21 +0000 | [diff] [blame] | 2311 | Py_DECREF(w); |
| 2312 | JUMPTO(oparg); |
Neil Schemenauer | c4b570f | 2003-06-01 19:21:12 +0000 | [diff] [blame] | 2313 | goto fast_next_opcode; |
Raymond Hettinger | 21012b8 | 2003-02-26 18:11:50 +0000 | [diff] [blame] | 2314 | } |
| 2315 | err = PyObject_IsTrue(w); |
Jeffrey Yasskin | 68d6852 | 2009-02-28 19:03:21 +0000 | [diff] [blame] | 2316 | Py_DECREF(w); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 2317 | if (err > 0) |
| 2318 | err = 0; |
| 2319 | else if (err == 0) |
Jeffrey Yasskin | 68d6852 | 2009-02-28 19:03:21 +0000 | [diff] [blame] | 2320 | JUMPTO(oparg); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 2321 | else |
| 2322 | break; |
| 2323 | continue; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2324 | |
Jeffrey Yasskin | 68d6852 | 2009-02-28 19:03:21 +0000 | [diff] [blame] | 2325 | PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE); |
| 2326 | case POP_JUMP_IF_TRUE: |
| 2327 | w = POP(); |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 2328 | if (w == Py_False) { |
Jeffrey Yasskin | 68d6852 | 2009-02-28 19:03:21 +0000 | [diff] [blame] | 2329 | Py_DECREF(w); |
Neil Schemenauer | c4b570f | 2003-06-01 19:21:12 +0000 | [diff] [blame] | 2330 | goto fast_next_opcode; |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 2331 | } |
Raymond Hettinger | 21012b8 | 2003-02-26 18:11:50 +0000 | [diff] [blame] | 2332 | if (w == Py_True) { |
Jeffrey Yasskin | 68d6852 | 2009-02-28 19:03:21 +0000 | [diff] [blame] | 2333 | Py_DECREF(w); |
| 2334 | JUMPTO(oparg); |
| 2335 | goto fast_next_opcode; |
| 2336 | } |
| 2337 | err = PyObject_IsTrue(w); |
| 2338 | Py_DECREF(w); |
| 2339 | if (err > 0) { |
| 2340 | err = 0; |
| 2341 | JUMPTO(oparg); |
| 2342 | } |
| 2343 | else if (err == 0) |
| 2344 | ; |
| 2345 | else |
| 2346 | break; |
| 2347 | continue; |
| 2348 | |
| 2349 | case JUMP_IF_FALSE_OR_POP: |
| 2350 | w = TOP(); |
| 2351 | if (w == Py_True) { |
| 2352 | STACKADJ(-1); |
| 2353 | Py_DECREF(w); |
| 2354 | goto fast_next_opcode; |
| 2355 | } |
| 2356 | if (w == Py_False) { |
| 2357 | JUMPTO(oparg); |
| 2358 | goto fast_next_opcode; |
| 2359 | } |
| 2360 | err = PyObject_IsTrue(w); |
| 2361 | if (err > 0) { |
| 2362 | STACKADJ(-1); |
| 2363 | Py_DECREF(w); |
| 2364 | err = 0; |
| 2365 | } |
| 2366 | else if (err == 0) |
| 2367 | JUMPTO(oparg); |
| 2368 | else |
| 2369 | break; |
| 2370 | continue; |
| 2371 | |
| 2372 | case JUMP_IF_TRUE_OR_POP: |
| 2373 | w = TOP(); |
| 2374 | if (w == Py_False) { |
| 2375 | STACKADJ(-1); |
| 2376 | Py_DECREF(w); |
| 2377 | goto fast_next_opcode; |
| 2378 | } |
| 2379 | if (w == Py_True) { |
| 2380 | JUMPTO(oparg); |
Neil Schemenauer | c4b570f | 2003-06-01 19:21:12 +0000 | [diff] [blame] | 2381 | goto fast_next_opcode; |
Raymond Hettinger | 21012b8 | 2003-02-26 18:11:50 +0000 | [diff] [blame] | 2382 | } |
| 2383 | err = PyObject_IsTrue(w); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 2384 | if (err > 0) { |
| 2385 | err = 0; |
Jeffrey Yasskin | 68d6852 | 2009-02-28 19:03:21 +0000 | [diff] [blame] | 2386 | JUMPTO(oparg); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 2387 | } |
Jeffrey Yasskin | 68d6852 | 2009-02-28 19:03:21 +0000 | [diff] [blame] | 2388 | else if (err == 0) { |
| 2389 | STACKADJ(-1); |
| 2390 | Py_DECREF(w); |
| 2391 | } |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 2392 | else |
| 2393 | break; |
| 2394 | continue; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2395 | |
Raymond Hettinger | fba1cfc | 2004-03-12 16:33:17 +0000 | [diff] [blame] | 2396 | PREDICTED_WITH_ARG(JUMP_ABSOLUTE); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2397 | case JUMP_ABSOLUTE: |
| 2398 | JUMPTO(oparg); |
Raymond Hettinger | dc1d1ba | 2007-11-07 02:45:46 +0000 | [diff] [blame] | 2399 | #if FAST_LOOPS |
| 2400 | /* Enabling this path speeds-up all while and for-loops by bypassing |
| 2401 | the per-loop checks for signals. By default, this should be turned-off |
| 2402 | because it prevents detection of a control-break in tight loops like |
| 2403 | "while 1: pass". Compile with this option turned-on when you need |
| 2404 | the speed-up and do not need break checking inside tight loops (ones |
Antoine Pitrou | c2cc80c | 2008-07-25 22:13:52 +0000 | [diff] [blame] | 2405 | that contain only instructions ending with goto fast_next_opcode). |
Raymond Hettinger | dc1d1ba | 2007-11-07 02:45:46 +0000 | [diff] [blame] | 2406 | */ |
| 2407 | goto fast_next_opcode; |
| 2408 | #else |
Neil Schemenauer | ca2a2f1 | 2003-05-30 23:59:44 +0000 | [diff] [blame] | 2409 | continue; |
Raymond Hettinger | dc1d1ba | 2007-11-07 02:45:46 +0000 | [diff] [blame] | 2410 | #endif |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2411 | |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2412 | case GET_ITER: |
| 2413 | /* before: [obj]; after [getiter(obj)] */ |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 2414 | v = TOP(); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2415 | x = PyObject_GetIter(v); |
| 2416 | Py_DECREF(v); |
| 2417 | if (x != NULL) { |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 2418 | SET_TOP(x); |
Raymond Hettinger | 7dc5221 | 2003-03-16 20:14:44 +0000 | [diff] [blame] | 2419 | PREDICT(FOR_ITER); |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2420 | continue; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2421 | } |
Raymond Hettinger | 8bb90a5 | 2003-01-14 12:43:10 +0000 | [diff] [blame] | 2422 | STACKADJ(-1); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2423 | break; |
| 2424 | |
Raymond Hettinger | 7dc5221 | 2003-03-16 20:14:44 +0000 | [diff] [blame] | 2425 | PREDICTED_WITH_ARG(FOR_ITER); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2426 | case FOR_ITER: |
| 2427 | /* before: [iter]; after: [iter, iter()] *or* [] */ |
| 2428 | v = TOP(); |
Raymond Hettinger | db0de9e | 2004-03-12 08:41:36 +0000 | [diff] [blame] | 2429 | x = (*v->ob_type->tp_iternext)(v); |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2430 | if (x != NULL) { |
| 2431 | PUSH(x); |
Raymond Hettinger | 7dc5221 | 2003-03-16 20:14:44 +0000 | [diff] [blame] | 2432 | PREDICT(STORE_FAST); |
| 2433 | PREDICT(UNPACK_SEQUENCE); |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2434 | continue; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2435 | } |
Raymond Hettinger | db0de9e | 2004-03-12 08:41:36 +0000 | [diff] [blame] | 2436 | if (PyErr_Occurred()) { |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 2437 | if (!PyErr_ExceptionMatches( |
| 2438 | PyExc_StopIteration)) |
Raymond Hettinger | db0de9e | 2004-03-12 08:41:36 +0000 | [diff] [blame] | 2439 | break; |
| 2440 | PyErr_Clear(); |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2441 | } |
Raymond Hettinger | db0de9e | 2004-03-12 08:41:36 +0000 | [diff] [blame] | 2442 | /* iterator ended normally */ |
| 2443 | x = v = POP(); |
| 2444 | Py_DECREF(v); |
| 2445 | JUMPBY(oparg); |
| 2446 | continue; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2447 | |
Raymond Hettinger | 2d783e9 | 2004-03-12 09:12:22 +0000 | [diff] [blame] | 2448 | case BREAK_LOOP: |
| 2449 | why = WHY_BREAK; |
| 2450 | goto fast_block_end; |
| 2451 | |
| 2452 | case CONTINUE_LOOP: |
| 2453 | retval = PyInt_FromLong(oparg); |
Neal Norwitz | 02104df | 2006-05-19 06:31:23 +0000 | [diff] [blame] | 2454 | if (!retval) { |
| 2455 | x = NULL; |
| 2456 | break; |
| 2457 | } |
Raymond Hettinger | 2d783e9 | 2004-03-12 09:12:22 +0000 | [diff] [blame] | 2458 | why = WHY_CONTINUE; |
| 2459 | goto fast_block_end; |
| 2460 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2461 | case SETUP_LOOP: |
| 2462 | case SETUP_EXCEPT: |
| 2463 | case SETUP_FINALLY: |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 2464 | /* NOTE: If you add any new block-setup opcodes that |
| 2465 | are not try/except/finally handlers, you may need |
| 2466 | to update the PyGen_NeedsFinalizing() function. |
| 2467 | */ |
Phillip J. Eby | 2ba9661 | 2006-04-10 17:51:05 +0000 | [diff] [blame] | 2468 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2469 | PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg, |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 2470 | STACK_LEVEL()); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 2471 | continue; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2472 | |
Benjamin Peterson | 1880d8b | 2009-05-25 13:13:44 +0000 | [diff] [blame] | 2473 | case SETUP_WITH: |
| 2474 | { |
| 2475 | static PyObject *exit, *enter; |
| 2476 | w = TOP(); |
| 2477 | x = special_lookup(w, "__exit__", &exit); |
| 2478 | if (!x) |
| 2479 | break; |
| 2480 | SET_TOP(x); |
| 2481 | u = special_lookup(w, "__enter__", &enter); |
| 2482 | Py_DECREF(w); |
| 2483 | if (!u) { |
| 2484 | x = NULL; |
| 2485 | break; |
| 2486 | } |
| 2487 | x = PyObject_CallFunctionObjArgs(u, NULL); |
| 2488 | Py_DECREF(u); |
| 2489 | if (!x) |
| 2490 | break; |
| 2491 | /* Setup the finally block before pushing the result |
| 2492 | of __enter__ on the stack. */ |
| 2493 | PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg, |
| 2494 | STACK_LEVEL()); |
| 2495 | |
| 2496 | PUSH(x); |
| 2497 | continue; |
| 2498 | } |
| 2499 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 2500 | case WITH_CLEANUP: |
| 2501 | { |
Nick Coghlan | 7af53be | 2008-03-07 14:13:28 +0000 | [diff] [blame] | 2502 | /* At the top of the stack are 1-3 values indicating |
| 2503 | how/why we entered the finally clause: |
| 2504 | - TOP = None |
| 2505 | - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval |
| 2506 | - TOP = WHY_*; no retval below it |
| 2507 | - (TOP, SECOND, THIRD) = exc_info() |
| 2508 | Below them is EXIT, the context.__exit__ bound method. |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 2509 | In the last case, we must call |
Nick Coghlan | 7af53be | 2008-03-07 14:13:28 +0000 | [diff] [blame] | 2510 | EXIT(TOP, SECOND, THIRD) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 2511 | otherwise we must call |
Nick Coghlan | 7af53be | 2008-03-07 14:13:28 +0000 | [diff] [blame] | 2512 | EXIT(None, None, None) |
| 2513 | |
| 2514 | In all cases, we remove EXIT from the stack, leaving |
| 2515 | the rest in the same order. |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 2516 | |
| 2517 | In addition, if the stack represents an exception, |
Guido van Rossum | f669436 | 2006-03-10 02:28:35 +0000 | [diff] [blame] | 2518 | *and* the function call returns a 'true' value, we |
| 2519 | "zap" this information, to prevent END_FINALLY from |
| 2520 | re-raising the exception. (But non-local gotos |
| 2521 | should still be resumed.) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 2522 | */ |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 2523 | |
Nick Coghlan | 7af53be | 2008-03-07 14:13:28 +0000 | [diff] [blame] | 2524 | PyObject *exit_func; |
| 2525 | |
| 2526 | u = POP(); |
| 2527 | if (u == Py_None) { |
| 2528 | exit_func = TOP(); |
| 2529 | SET_TOP(u); |
| 2530 | v = w = Py_None; |
| 2531 | } |
| 2532 | else if (PyInt_Check(u)) { |
| 2533 | switch(PyInt_AS_LONG(u)) { |
| 2534 | case WHY_RETURN: |
| 2535 | case WHY_CONTINUE: |
| 2536 | /* Retval in TOP. */ |
| 2537 | exit_func = SECOND(); |
| 2538 | SET_SECOND(TOP()); |
| 2539 | SET_TOP(u); |
| 2540 | break; |
| 2541 | default: |
| 2542 | exit_func = TOP(); |
| 2543 | SET_TOP(u); |
| 2544 | break; |
| 2545 | } |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 2546 | u = v = w = Py_None; |
| 2547 | } |
| 2548 | else { |
Nick Coghlan | 7af53be | 2008-03-07 14:13:28 +0000 | [diff] [blame] | 2549 | v = TOP(); |
| 2550 | w = SECOND(); |
| 2551 | exit_func = THIRD(); |
| 2552 | SET_TOP(u); |
| 2553 | SET_SECOND(v); |
| 2554 | SET_THIRD(w); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 2555 | } |
Guido van Rossum | f669436 | 2006-03-10 02:28:35 +0000 | [diff] [blame] | 2556 | /* XXX Not the fastest way to call it... */ |
Nick Coghlan | 7af53be | 2008-03-07 14:13:28 +0000 | [diff] [blame] | 2557 | x = PyObject_CallFunctionObjArgs(exit_func, u, v, w, |
| 2558 | NULL); |
Amaury Forgeot d'Arc | ad9b599 | 2008-12-10 23:22:49 +0000 | [diff] [blame] | 2559 | Py_DECREF(exit_func); |
| 2560 | if (x == NULL) |
Guido van Rossum | f669436 | 2006-03-10 02:28:35 +0000 | [diff] [blame] | 2561 | break; /* Go to error exit */ |
Amaury Forgeot d'Arc | ad9b599 | 2008-12-10 23:22:49 +0000 | [diff] [blame] | 2562 | |
| 2563 | if (u != Py_None) |
| 2564 | err = PyObject_IsTrue(x); |
| 2565 | else |
| 2566 | err = 0; |
| 2567 | Py_DECREF(x); |
| 2568 | |
| 2569 | if (err < 0) |
| 2570 | break; /* Go to error exit */ |
| 2571 | else if (err > 0) { |
| 2572 | err = 0; |
Guido van Rossum | f669436 | 2006-03-10 02:28:35 +0000 | [diff] [blame] | 2573 | /* There was an exception and a true return */ |
Nick Coghlan | 7af53be | 2008-03-07 14:13:28 +0000 | [diff] [blame] | 2574 | STACKADJ(-2); |
Guido van Rossum | f669436 | 2006-03-10 02:28:35 +0000 | [diff] [blame] | 2575 | Py_INCREF(Py_None); |
| 2576 | SET_TOP(Py_None); |
Guido van Rossum | f669436 | 2006-03-10 02:28:35 +0000 | [diff] [blame] | 2577 | Py_DECREF(u); |
| 2578 | Py_DECREF(v); |
| 2579 | Py_DECREF(w); |
| 2580 | } else { |
Nick Coghlan | 7af53be | 2008-03-07 14:13:28 +0000 | [diff] [blame] | 2581 | /* The stack was rearranged to remove EXIT |
| 2582 | above. Let END_FINALLY do its thing */ |
Guido van Rossum | f669436 | 2006-03-10 02:28:35 +0000 | [diff] [blame] | 2583 | } |
Jeffrey Yasskin | 9063a99 | 2008-03-03 01:27:03 +0000 | [diff] [blame] | 2584 | PREDICT(END_FINALLY); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 2585 | break; |
| 2586 | } |
| 2587 | |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 2588 | case CALL_FUNCTION: |
Armin Rigo | 8817fcd | 2004-06-17 10:22:40 +0000 | [diff] [blame] | 2589 | { |
| 2590 | PyObject **sp; |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 2591 | PCALL(PCALL_ALL); |
Armin Rigo | 8817fcd | 2004-06-17 10:22:40 +0000 | [diff] [blame] | 2592 | sp = stack_pointer; |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 2593 | #ifdef WITH_TSC |
Armin Rigo | 8817fcd | 2004-06-17 10:22:40 +0000 | [diff] [blame] | 2594 | x = call_function(&sp, oparg, &intr0, &intr1); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 2595 | #else |
Armin Rigo | 8817fcd | 2004-06-17 10:22:40 +0000 | [diff] [blame] | 2596 | x = call_function(&sp, oparg); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 2597 | #endif |
Armin Rigo | 8817fcd | 2004-06-17 10:22:40 +0000 | [diff] [blame] | 2598 | stack_pointer = sp; |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 2599 | PUSH(x); |
| 2600 | if (x != NULL) |
| 2601 | continue; |
| 2602 | break; |
Armin Rigo | 8817fcd | 2004-06-17 10:22:40 +0000 | [diff] [blame] | 2603 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2604 | |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 2605 | case CALL_FUNCTION_VAR: |
| 2606 | case CALL_FUNCTION_KW: |
| 2607 | case CALL_FUNCTION_VAR_KW: |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 2608 | { |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 2609 | int na = oparg & 0xff; |
| 2610 | int nk = (oparg>>8) & 0xff; |
| 2611 | int flags = (opcode - CALL_FUNCTION) & 3; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 2612 | int n = na + 2 * nk; |
Armin Rigo | 8817fcd | 2004-06-17 10:22:40 +0000 | [diff] [blame] | 2613 | PyObject **pfunc, *func, **sp; |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 2614 | PCALL(PCALL_ALL); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 2615 | if (flags & CALL_FLAG_VAR) |
| 2616 | n++; |
| 2617 | if (flags & CALL_FLAG_KW) |
| 2618 | n++; |
| 2619 | pfunc = stack_pointer - n - 1; |
| 2620 | func = *pfunc; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 2621 | |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2622 | if (PyMethod_Check(func) |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 2623 | && PyMethod_GET_SELF(func) != NULL) { |
| 2624 | PyObject *self = PyMethod_GET_SELF(func); |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 2625 | Py_INCREF(self); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 2626 | func = PyMethod_GET_FUNCTION(func); |
| 2627 | Py_INCREF(func); |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 2628 | Py_DECREF(*pfunc); |
| 2629 | *pfunc = self; |
| 2630 | na++; |
| 2631 | n++; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2632 | } else |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 2633 | Py_INCREF(func); |
Armin Rigo | 8817fcd | 2004-06-17 10:22:40 +0000 | [diff] [blame] | 2634 | sp = stack_pointer; |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 2635 | READ_TIMESTAMP(intr0); |
Armin Rigo | 8817fcd | 2004-06-17 10:22:40 +0000 | [diff] [blame] | 2636 | x = ext_do_call(func, &sp, flags, na, nk); |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 2637 | READ_TIMESTAMP(intr1); |
Armin Rigo | 8817fcd | 2004-06-17 10:22:40 +0000 | [diff] [blame] | 2638 | stack_pointer = sp; |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 2639 | Py_DECREF(func); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 2640 | |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 2641 | while (stack_pointer > pfunc) { |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 2642 | w = POP(); |
| 2643 | Py_DECREF(w); |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 2644 | } |
| 2645 | PUSH(x); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2646 | if (x != NULL) |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 2647 | continue; |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 2648 | break; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 2649 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2650 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2651 | case MAKE_FUNCTION: |
| 2652 | v = POP(); /* code object */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2653 | x = PyFunction_New(v, f->f_globals); |
| 2654 | Py_DECREF(v); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2655 | /* XXX Maybe this should be a separate opcode? */ |
| 2656 | if (x != NULL && oparg > 0) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2657 | v = PyTuple_New(oparg); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2658 | if (v == NULL) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2659 | Py_DECREF(x); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2660 | x = NULL; |
| 2661 | break; |
| 2662 | } |
Raymond Hettinger | 5bed456 | 2004-04-10 23:34:17 +0000 | [diff] [blame] | 2663 | while (--oparg >= 0) { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2664 | w = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2665 | PyTuple_SET_ITEM(v, oparg, w); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2666 | } |
| 2667 | err = PyFunction_SetDefaults(x, v); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2668 | Py_DECREF(v); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2669 | } |
| 2670 | PUSH(x); |
| 2671 | break; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2672 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2673 | case MAKE_CLOSURE: |
| 2674 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2675 | v = POP(); /* code object */ |
| 2676 | x = PyFunction_New(v, f->f_globals); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2677 | Py_DECREF(v); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2678 | if (x != NULL) { |
| 2679 | v = POP(); |
Jeffrey Yasskin | 2d873bd | 2008-12-08 18:55:24 +0000 | [diff] [blame] | 2680 | if (PyFunction_SetClosure(x, v) != 0) { |
| 2681 | /* Can't happen unless bytecode is corrupt. */ |
| 2682 | why = WHY_EXCEPTION; |
| 2683 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2684 | Py_DECREF(v); |
| 2685 | } |
| 2686 | if (x != NULL && oparg > 0) { |
| 2687 | v = PyTuple_New(oparg); |
| 2688 | if (v == NULL) { |
| 2689 | Py_DECREF(x); |
| 2690 | x = NULL; |
| 2691 | break; |
| 2692 | } |
Raymond Hettinger | 5bed456 | 2004-04-10 23:34:17 +0000 | [diff] [blame] | 2693 | while (--oparg >= 0) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2694 | w = POP(); |
| 2695 | PyTuple_SET_ITEM(v, oparg, w); |
| 2696 | } |
Jeffrey Yasskin | 2d873bd | 2008-12-08 18:55:24 +0000 | [diff] [blame] | 2697 | if (PyFunction_SetDefaults(x, v) != 0) { |
| 2698 | /* Can't happen unless |
| 2699 | PyFunction_SetDefaults changes. */ |
| 2700 | why = WHY_EXCEPTION; |
| 2701 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2702 | Py_DECREF(v); |
| 2703 | } |
| 2704 | PUSH(x); |
| 2705 | break; |
| 2706 | } |
| 2707 | |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2708 | case BUILD_SLICE: |
| 2709 | if (oparg == 3) |
| 2710 | w = POP(); |
| 2711 | else |
| 2712 | w = NULL; |
| 2713 | v = POP(); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 2714 | u = TOP(); |
Guido van Rossum | 1aa1483 | 1997-01-21 05:34:20 +0000 | [diff] [blame] | 2715 | x = PySlice_New(u, v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2716 | Py_DECREF(u); |
| 2717 | Py_DECREF(v); |
| 2718 | Py_XDECREF(w); |
Raymond Hettinger | 663004b | 2003-01-09 15:24:30 +0000 | [diff] [blame] | 2719 | SET_TOP(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 2720 | if (x != NULL) continue; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2721 | break; |
| 2722 | |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 2723 | case EXTENDED_ARG: |
| 2724 | opcode = NEXTOP(); |
Raymond Hettinger | 5bed456 | 2004-04-10 23:34:17 +0000 | [diff] [blame] | 2725 | oparg = oparg<<16 | NEXTARG(); |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 2726 | goto dispatch_opcode; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2727 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2728 | default: |
| 2729 | fprintf(stderr, |
| 2730 | "XXX lineno: %d, opcode: %d\n", |
Jeffrey Yasskin | f7f858d | 2009-05-08 22:23:21 +0000 | [diff] [blame] | 2731 | PyFrame_GetLineNumber(f), |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 2732 | opcode); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2733 | PyErr_SetString(PyExc_SystemError, "unknown opcode"); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2734 | why = WHY_EXCEPTION; |
| 2735 | break; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 2736 | |
| 2737 | #ifdef CASE_TOO_BIG |
| 2738 | } |
| 2739 | #endif |
| 2740 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2741 | } /* switch */ |
| 2742 | |
| 2743 | on_error: |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2744 | |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 2745 | READ_TIMESTAMP(inst1); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 2746 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2747 | /* Quickly continue if no error occurred */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2748 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2749 | if (why == WHY_NOT) { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2750 | if (err == 0 && x != NULL) { |
| 2751 | #ifdef CHECKEXC |
Guido van Rossum | eb894eb | 1999-03-09 16:16:45 +0000 | [diff] [blame] | 2752 | /* This check is expensive! */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2753 | if (PyErr_Occurred()) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2754 | fprintf(stderr, |
| 2755 | "XXX undetected error\n"); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 2756 | else { |
| 2757 | #endif |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 2758 | READ_TIMESTAMP(loop1); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2759 | continue; /* Normal, fast path */ |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 2760 | #ifdef CHECKEXC |
| 2761 | } |
| 2762 | #endif |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2763 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2764 | why = WHY_EXCEPTION; |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2765 | x = Py_None; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2766 | err = 0; |
| 2767 | } |
| 2768 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2769 | /* Double-check exception status */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2770 | |
Raymond Hettinger | c8aa08b | 2004-04-11 14:59:33 +0000 | [diff] [blame] | 2771 | if (why == WHY_EXCEPTION || why == WHY_RERAISE) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2772 | if (!PyErr_Occurred()) { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2773 | PyErr_SetString(PyExc_SystemError, |
Guido van Rossum | eb894eb | 1999-03-09 16:16:45 +0000 | [diff] [blame] | 2774 | "error return without exception set"); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2775 | why = WHY_EXCEPTION; |
| 2776 | } |
| 2777 | } |
Guido van Rossum | eb894eb | 1999-03-09 16:16:45 +0000 | [diff] [blame] | 2778 | #ifdef CHECKEXC |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2779 | else { |
Guido van Rossum | eb894eb | 1999-03-09 16:16:45 +0000 | [diff] [blame] | 2780 | /* This check is expensive! */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2781 | if (PyErr_Occurred()) { |
Neal Norwitz | 8250fbe | 2008-01-27 17:12:15 +0000 | [diff] [blame] | 2782 | char buf[128]; |
Jeremy Hylton | 904ed86 | 2003-11-05 17:29:35 +0000 | [diff] [blame] | 2783 | sprintf(buf, "Stack unwind with exception " |
| 2784 | "set and why=%d", why); |
| 2785 | Py_FatalError(buf); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2786 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2787 | } |
| 2788 | #endif |
| 2789 | |
| 2790 | /* Log traceback info if this is a real exception */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2791 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2792 | if (why == WHY_EXCEPTION) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2793 | PyTraceBack_Here(f); |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 2794 | |
Fred Drake | 8f51f54 | 2001-10-04 14:48:42 +0000 | [diff] [blame] | 2795 | if (tstate->c_tracefunc != NULL) |
| 2796 | call_exc_trace(tstate->c_tracefunc, |
| 2797 | tstate->c_traceobj, f); |
Guido van Rossum | 014518f | 1998-11-23 21:09:51 +0000 | [diff] [blame] | 2798 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2799 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2800 | /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2801 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2802 | if (why == WHY_RERAISE) |
| 2803 | why = WHY_EXCEPTION; |
| 2804 | |
| 2805 | /* Unwind stacks if a (pseudo) exception occurred */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2806 | |
Raymond Hettinger | 1dd8309 | 2004-02-06 18:32:33 +0000 | [diff] [blame] | 2807 | fast_block_end: |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 2808 | while (why != WHY_NOT && f->f_iblock > 0) { |
Benjamin Peterson | 4a3cf19 | 2009-07-01 23:45:19 +0000 | [diff] [blame] | 2809 | /* Peek at the current block. */ |
| 2810 | PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1]; |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 2811 | |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 2812 | assert(why != WHY_YIELD); |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 2813 | if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) { |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 2814 | why = WHY_NOT; |
| 2815 | JUMPTO(PyInt_AS_LONG(retval)); |
| 2816 | Py_DECREF(retval); |
| 2817 | break; |
| 2818 | } |
| 2819 | |
Benjamin Peterson | 4a3cf19 | 2009-07-01 23:45:19 +0000 | [diff] [blame] | 2820 | /* Now we have to pop the block. */ |
| 2821 | f->f_iblock--; |
| 2822 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2823 | while (STACK_LEVEL() > b->b_level) { |
| 2824 | v = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2825 | Py_XDECREF(v); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2826 | } |
| 2827 | if (b->b_type == SETUP_LOOP && why == WHY_BREAK) { |
| 2828 | why = WHY_NOT; |
| 2829 | JUMPTO(b->b_handler); |
| 2830 | break; |
| 2831 | } |
| 2832 | if (b->b_type == SETUP_FINALLY || |
Guido van Rossum | 150b2df | 1996-12-05 23:17:11 +0000 | [diff] [blame] | 2833 | (b->b_type == SETUP_EXCEPT && |
| 2834 | why == WHY_EXCEPTION)) { |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2835 | if (why == WHY_EXCEPTION) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2836 | PyObject *exc, *val, *tb; |
| 2837 | PyErr_Fetch(&exc, &val, &tb); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2838 | if (val == NULL) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2839 | val = Py_None; |
| 2840 | Py_INCREF(val); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2841 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2842 | /* Make the raw exception data |
| 2843 | available to the handler, |
| 2844 | so a program can emulate the |
| 2845 | Python main loop. Don't do |
| 2846 | this for 'finally'. */ |
| 2847 | if (b->b_type == SETUP_EXCEPT) { |
Barry Warsaw | eaedc7c | 1997-08-28 22:36:40 +0000 | [diff] [blame] | 2848 | PyErr_NormalizeException( |
| 2849 | &exc, &val, &tb); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2850 | set_exc_info(tstate, |
| 2851 | exc, val, tb); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2852 | } |
Jeremy Hylton | c631489 | 2001-09-26 19:24:45 +0000 | [diff] [blame] | 2853 | if (tb == NULL) { |
| 2854 | Py_INCREF(Py_None); |
| 2855 | PUSH(Py_None); |
| 2856 | } else |
| 2857 | PUSH(tb); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2858 | PUSH(val); |
| 2859 | PUSH(exc); |
| 2860 | } |
| 2861 | else { |
Raymond Hettinger | 06032cb | 2004-04-06 09:37:35 +0000 | [diff] [blame] | 2862 | if (why & (WHY_RETURN | WHY_CONTINUE)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2863 | PUSH(retval); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2864 | v = PyInt_FromLong((long)why); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2865 | PUSH(v); |
| 2866 | } |
| 2867 | why = WHY_NOT; |
| 2868 | JUMPTO(b->b_handler); |
| 2869 | break; |
| 2870 | } |
| 2871 | } /* unwind stack */ |
| 2872 | |
| 2873 | /* End the loop if we still have an error (or return) */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2874 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2875 | if (why != WHY_NOT) |
| 2876 | break; |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 2877 | READ_TIMESTAMP(loop1); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2878 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2879 | } /* main loop */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2880 | |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 2881 | assert(why != WHY_YIELD); |
| 2882 | /* Pop remaining stack entries. */ |
| 2883 | while (!EMPTY()) { |
| 2884 | v = POP(); |
| 2885 | Py_XDECREF(v); |
Guido van Rossum | 35974fb | 2001-12-06 21:28:18 +0000 | [diff] [blame] | 2886 | } |
| 2887 | |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 2888 | if (why != WHY_RETURN) |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 2889 | retval = NULL; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2890 | |
Raymond Hettinger | 1dd8309 | 2004-02-06 18:32:33 +0000 | [diff] [blame] | 2891 | fast_yield: |
Fred Drake | 9e3ad78 | 2001-07-03 23:39:52 +0000 | [diff] [blame] | 2892 | if (tstate->use_tracing) { |
Barry Warsaw | e2eca0b | 2005-08-15 18:14:19 +0000 | [diff] [blame] | 2893 | if (tstate->c_tracefunc) { |
| 2894 | if (why == WHY_RETURN || why == WHY_YIELD) { |
| 2895 | if (call_trace(tstate->c_tracefunc, |
| 2896 | tstate->c_traceobj, f, |
| 2897 | PyTrace_RETURN, retval)) { |
| 2898 | Py_XDECREF(retval); |
| 2899 | retval = NULL; |
| 2900 | why = WHY_EXCEPTION; |
| 2901 | } |
| 2902 | } |
| 2903 | else if (why == WHY_EXCEPTION) { |
| 2904 | call_trace_protected(tstate->c_tracefunc, |
| 2905 | tstate->c_traceobj, f, |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 2906 | PyTrace_RETURN, NULL); |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 2907 | } |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 2908 | } |
Fred Drake | 8f51f54 | 2001-10-04 14:48:42 +0000 | [diff] [blame] | 2909 | if (tstate->c_profilefunc) { |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 2910 | if (why == WHY_EXCEPTION) |
| 2911 | call_trace_protected(tstate->c_profilefunc, |
| 2912 | tstate->c_profileobj, f, |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 2913 | PyTrace_RETURN, NULL); |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 2914 | else if (call_trace(tstate->c_profilefunc, |
| 2915 | tstate->c_profileobj, f, |
| 2916 | PyTrace_RETURN, retval)) { |
Fred Drake | 9e3ad78 | 2001-07-03 23:39:52 +0000 | [diff] [blame] | 2917 | Py_XDECREF(retval); |
| 2918 | retval = NULL; |
| 2919 | why = WHY_EXCEPTION; |
| 2920 | } |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 2921 | } |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 2922 | } |
Guido van Rossum | a424013 | 1997-01-21 21:18:36 +0000 | [diff] [blame] | 2923 | |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 2924 | if (tstate->frame->f_exc_type != NULL) |
| 2925 | reset_exc_info(tstate); |
| 2926 | else { |
| 2927 | assert(tstate->frame->f_exc_value == NULL); |
| 2928 | assert(tstate->frame->f_exc_traceback == NULL); |
| 2929 | } |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2930 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2931 | /* pop frame */ |
Thomas Wouters | ae406c6 | 2007-09-19 17:27:43 +0000 | [diff] [blame] | 2932 | exit_eval_frame: |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 2933 | Py_LeaveRecursiveCall(); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2934 | tstate->frame = f->f_back; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2935 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 2936 | return retval; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2937 | } |
| 2938 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 2939 | /* 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] | 2940 | PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 2941 | the test in the if statements in Misc/gdbinit (pystack and pystackv). */ |
Skip Montanaro | 786ea6b | 2004-03-01 15:44:05 +0000 | [diff] [blame] | 2942 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2943 | PyObject * |
| 2944 | PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals, |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2945 | PyObject **args, int argcount, PyObject **kws, int kwcount, |
| 2946 | PyObject **defs, int defcount, PyObject *closure) |
| 2947 | { |
| 2948 | register PyFrameObject *f; |
| 2949 | register PyObject *retval = NULL; |
| 2950 | register PyObject **fastlocals, **freevars; |
| 2951 | PyThreadState *tstate = PyThreadState_GET(); |
| 2952 | PyObject *x, *u; |
| 2953 | |
| 2954 | if (globals == NULL) { |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 2955 | PyErr_SetString(PyExc_SystemError, |
Jeremy Hylton | 910d7d4 | 2001-08-12 21:52:24 +0000 | [diff] [blame] | 2956 | "PyEval_EvalCodeEx: NULL globals"); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2957 | return NULL; |
| 2958 | } |
| 2959 | |
Neal Norwitz | df6a649 | 2006-08-13 18:10:10 +0000 | [diff] [blame] | 2960 | assert(tstate != NULL); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 2961 | assert(globals != NULL); |
| 2962 | f = PyFrame_New(tstate, co, globals, locals); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2963 | if (f == NULL) |
| 2964 | return NULL; |
| 2965 | |
| 2966 | fastlocals = f->f_localsplus; |
Richard Jones | cebbefc | 2006-05-23 18:28:17 +0000 | [diff] [blame] | 2967 | freevars = f->f_localsplus + co->co_nlocals; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2968 | |
| 2969 | if (co->co_argcount > 0 || |
| 2970 | co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) { |
| 2971 | int i; |
| 2972 | int n = argcount; |
| 2973 | PyObject *kwdict = NULL; |
| 2974 | if (co->co_flags & CO_VARKEYWORDS) { |
| 2975 | kwdict = PyDict_New(); |
| 2976 | if (kwdict == NULL) |
| 2977 | goto fail; |
| 2978 | i = co->co_argcount; |
| 2979 | if (co->co_flags & CO_VARARGS) |
| 2980 | i++; |
| 2981 | SETLOCAL(i, kwdict); |
| 2982 | } |
| 2983 | if (argcount > co->co_argcount) { |
| 2984 | if (!(co->co_flags & CO_VARARGS)) { |
| 2985 | PyErr_Format(PyExc_TypeError, |
| 2986 | "%.200s() takes %s %d " |
| 2987 | "%sargument%s (%d given)", |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2988 | PyString_AsString(co->co_name), |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2989 | defcount ? "at most" : "exactly", |
| 2990 | co->co_argcount, |
| 2991 | kwcount ? "non-keyword " : "", |
| 2992 | co->co_argcount == 1 ? "" : "s", |
| 2993 | argcount); |
| 2994 | goto fail; |
| 2995 | } |
| 2996 | n = co->co_argcount; |
| 2997 | } |
| 2998 | for (i = 0; i < n; i++) { |
| 2999 | x = args[i]; |
| 3000 | Py_INCREF(x); |
| 3001 | SETLOCAL(i, x); |
| 3002 | } |
| 3003 | if (co->co_flags & CO_VARARGS) { |
| 3004 | u = PyTuple_New(argcount - n); |
| 3005 | if (u == NULL) |
| 3006 | goto fail; |
| 3007 | SETLOCAL(co->co_argcount, u); |
| 3008 | for (i = n; i < argcount; i++) { |
| 3009 | x = args[i]; |
| 3010 | Py_INCREF(x); |
| 3011 | PyTuple_SET_ITEM(u, i-n, x); |
| 3012 | } |
| 3013 | } |
| 3014 | for (i = 0; i < kwcount; i++) { |
Antoine Pitrou | c2cc80c | 2008-07-25 22:13:52 +0000 | [diff] [blame] | 3015 | PyObject **co_varnames; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3016 | PyObject *keyword = kws[2*i]; |
| 3017 | PyObject *value = kws[2*i + 1]; |
| 3018 | int j; |
Benjamin Peterson | 78821dd | 2009-01-25 17:15:10 +0000 | [diff] [blame] | 3019 | if (keyword == NULL || !(PyString_Check(keyword) |
| 3020 | #ifdef Py_USING_UNICODE |
| 3021 | || PyUnicode_Check(keyword) |
| 3022 | #endif |
| 3023 | )) { |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3024 | PyErr_Format(PyExc_TypeError, |
| 3025 | "%.200s() keywords must be strings", |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3026 | PyString_AsString(co->co_name)); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3027 | goto fail; |
| 3028 | } |
Antoine Pitrou | c2cc80c | 2008-07-25 22:13:52 +0000 | [diff] [blame] | 3029 | /* Speed hack: do raw pointer compares. As names are |
| 3030 | normally interned this should almost always hit. */ |
| 3031 | co_varnames = PySequence_Fast_ITEMS(co->co_varnames); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3032 | for (j = 0; j < co->co_argcount; j++) { |
Antoine Pitrou | c2cc80c | 2008-07-25 22:13:52 +0000 | [diff] [blame] | 3033 | PyObject *nm = co_varnames[j]; |
| 3034 | if (nm == keyword) |
| 3035 | goto kw_found; |
| 3036 | } |
| 3037 | /* Slow fallback, just in case */ |
| 3038 | for (j = 0; j < co->co_argcount; j++) { |
| 3039 | PyObject *nm = co_varnames[j]; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3040 | int cmp = PyObject_RichCompareBool( |
| 3041 | keyword, nm, Py_EQ); |
| 3042 | if (cmp > 0) |
Antoine Pitrou | c2cc80c | 2008-07-25 22:13:52 +0000 | [diff] [blame] | 3043 | goto kw_found; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3044 | else if (cmp < 0) |
| 3045 | goto fail; |
| 3046 | } |
| 3047 | /* Check errors from Compare */ |
| 3048 | if (PyErr_Occurred()) |
| 3049 | goto fail; |
| 3050 | if (j >= co->co_argcount) { |
| 3051 | if (kwdict == NULL) { |
Benjamin Peterson | e18ef19 | 2009-01-20 14:21:16 +0000 | [diff] [blame] | 3052 | PyObject *kwd_str = kwd_as_string(keyword); |
| 3053 | if (kwd_str) { |
| 3054 | PyErr_Format(PyExc_TypeError, |
| 3055 | "%.200s() got an unexpected " |
| 3056 | "keyword argument '%.400s'", |
| 3057 | PyString_AsString(co->co_name), |
| 3058 | PyString_AsString(kwd_str)); |
| 3059 | Py_DECREF(kwd_str); |
| 3060 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3061 | goto fail; |
| 3062 | } |
| 3063 | PyDict_SetItem(kwdict, keyword, value); |
Antoine Pitrou | c2cc80c | 2008-07-25 22:13:52 +0000 | [diff] [blame] | 3064 | continue; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3065 | } |
Antoine Pitrou | c2cc80c | 2008-07-25 22:13:52 +0000 | [diff] [blame] | 3066 | kw_found: |
| 3067 | if (GETLOCAL(j) != NULL) { |
Benjamin Peterson | e18ef19 | 2009-01-20 14:21:16 +0000 | [diff] [blame] | 3068 | PyObject *kwd_str = kwd_as_string(keyword); |
| 3069 | if (kwd_str) { |
| 3070 | PyErr_Format(PyExc_TypeError, |
| 3071 | "%.200s() got multiple " |
| 3072 | "values for keyword " |
| 3073 | "argument '%.400s'", |
| 3074 | PyString_AsString(co->co_name), |
| 3075 | PyString_AsString(kwd_str)); |
| 3076 | Py_DECREF(kwd_str); |
| 3077 | } |
Antoine Pitrou | c2cc80c | 2008-07-25 22:13:52 +0000 | [diff] [blame] | 3078 | goto fail; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3079 | } |
Antoine Pitrou | c2cc80c | 2008-07-25 22:13:52 +0000 | [diff] [blame] | 3080 | Py_INCREF(value); |
| 3081 | SETLOCAL(j, value); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3082 | } |
| 3083 | if (argcount < co->co_argcount) { |
| 3084 | int m = co->co_argcount - defcount; |
| 3085 | for (i = argcount; i < m; i++) { |
| 3086 | if (GETLOCAL(i) == NULL) { |
| 3087 | PyErr_Format(PyExc_TypeError, |
| 3088 | "%.200s() takes %s %d " |
| 3089 | "%sargument%s (%d given)", |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3090 | PyString_AsString(co->co_name), |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3091 | ((co->co_flags & CO_VARARGS) || |
| 3092 | defcount) ? "at least" |
| 3093 | : "exactly", |
| 3094 | m, kwcount ? "non-keyword " : "", |
| 3095 | m == 1 ? "" : "s", i); |
| 3096 | goto fail; |
| 3097 | } |
| 3098 | } |
| 3099 | if (n > m) |
| 3100 | i = n - m; |
| 3101 | else |
| 3102 | i = 0; |
| 3103 | for (; i < defcount; i++) { |
| 3104 | if (GETLOCAL(m+i) == NULL) { |
| 3105 | PyObject *def = defs[i]; |
| 3106 | Py_INCREF(def); |
| 3107 | SETLOCAL(m+i, def); |
| 3108 | } |
| 3109 | } |
| 3110 | } |
| 3111 | } |
| 3112 | else { |
| 3113 | if (argcount > 0 || kwcount > 0) { |
| 3114 | PyErr_Format(PyExc_TypeError, |
| 3115 | "%.200s() takes no arguments (%d given)", |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3116 | PyString_AsString(co->co_name), |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3117 | argcount + kwcount); |
| 3118 | goto fail; |
| 3119 | } |
| 3120 | } |
| 3121 | /* Allocate and initialize storage for cell vars, and copy free |
| 3122 | vars into frame. This isn't too efficient right now. */ |
Richard Jones | cebbefc | 2006-05-23 18:28:17 +0000 | [diff] [blame] | 3123 | if (PyTuple_GET_SIZE(co->co_cellvars)) { |
Neal Norwitz | 245ce8d | 2006-06-12 02:16:10 +0000 | [diff] [blame] | 3124 | int i, j, nargs, found; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3125 | char *cellname, *argname; |
| 3126 | PyObject *c; |
| 3127 | |
| 3128 | nargs = co->co_argcount; |
| 3129 | if (co->co_flags & CO_VARARGS) |
| 3130 | nargs++; |
| 3131 | if (co->co_flags & CO_VARKEYWORDS) |
| 3132 | nargs++; |
| 3133 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3134 | /* Initialize each cell var, taking into account |
| 3135 | cell vars that are initialized from arguments. |
| 3136 | |
| 3137 | Should arrange for the compiler to put cellvars |
| 3138 | that are arguments at the beginning of the cellvars |
| 3139 | list so that we can march over it more efficiently? |
| 3140 | */ |
Richard Jones | cebbefc | 2006-05-23 18:28:17 +0000 | [diff] [blame] | 3141 | for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3142 | cellname = PyString_AS_STRING( |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3143 | PyTuple_GET_ITEM(co->co_cellvars, i)); |
| 3144 | found = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3145 | for (j = 0; j < nargs; j++) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3146 | argname = PyString_AS_STRING( |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3147 | PyTuple_GET_ITEM(co->co_varnames, j)); |
| 3148 | if (strcmp(cellname, argname) == 0) { |
| 3149 | c = PyCell_New(GETLOCAL(j)); |
| 3150 | if (c == NULL) |
| 3151 | goto fail; |
Richard Jones | cebbefc | 2006-05-23 18:28:17 +0000 | [diff] [blame] | 3152 | GETLOCAL(co->co_nlocals + i) = c; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3153 | found = 1; |
| 3154 | break; |
| 3155 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3156 | } |
| 3157 | if (found == 0) { |
| 3158 | c = PyCell_New(NULL); |
| 3159 | if (c == NULL) |
| 3160 | goto fail; |
Richard Jones | cebbefc | 2006-05-23 18:28:17 +0000 | [diff] [blame] | 3161 | SETLOCAL(co->co_nlocals + i, c); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3162 | } |
| 3163 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3164 | } |
Richard Jones | cebbefc | 2006-05-23 18:28:17 +0000 | [diff] [blame] | 3165 | if (PyTuple_GET_SIZE(co->co_freevars)) { |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3166 | int i; |
Richard Jones | cebbefc | 2006-05-23 18:28:17 +0000 | [diff] [blame] | 3167 | for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) { |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3168 | PyObject *o = PyTuple_GET_ITEM(closure, i); |
| 3169 | Py_INCREF(o); |
Richard Jones | cebbefc | 2006-05-23 18:28:17 +0000 | [diff] [blame] | 3170 | freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3171 | } |
| 3172 | } |
| 3173 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3174 | if (co->co_flags & CO_GENERATOR) { |
Neil Schemenauer | 2b13ce8 | 2001-06-21 02:41:10 +0000 | [diff] [blame] | 3175 | /* Don't need to keep the reference to f_back, it will be set |
| 3176 | * when the generator is resumed. */ |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 3177 | Py_XDECREF(f->f_back); |
Neil Schemenauer | 2b13ce8 | 2001-06-21 02:41:10 +0000 | [diff] [blame] | 3178 | f->f_back = NULL; |
| 3179 | |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 3180 | PCALL(PCALL_GENERATOR); |
| 3181 | |
Neil Schemenauer | 2b13ce8 | 2001-06-21 02:41:10 +0000 | [diff] [blame] | 3182 | /* Create a new generator that owns the ready to run frame |
| 3183 | * and return that as the value. */ |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 3184 | return PyGen_New(f); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3185 | } |
| 3186 | |
Thomas Wouters | ae406c6 | 2007-09-19 17:27:43 +0000 | [diff] [blame] | 3187 | retval = PyEval_EvalFrameEx(f,0); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3188 | |
Thomas Wouters | ae406c6 | 2007-09-19 17:27:43 +0000 | [diff] [blame] | 3189 | fail: /* Jump here from prelude on failure */ |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3190 | |
Tim Peters | b13680b | 2001-11-27 23:29:29 +0000 | [diff] [blame] | 3191 | /* decref'ing the frame can cause __del__ methods to get invoked, |
| 3192 | which can call back into Python. While we're done with the |
| 3193 | current Python frame (f), the associated C stack is still in use, |
| 3194 | so recursion_depth must be boosted for the duration. |
| 3195 | */ |
| 3196 | assert(tstate != NULL); |
| 3197 | ++tstate->recursion_depth; |
Thomas Wouters | ae406c6 | 2007-09-19 17:27:43 +0000 | [diff] [blame] | 3198 | Py_DECREF(f); |
Tim Peters | b13680b | 2001-11-27 23:29:29 +0000 | [diff] [blame] | 3199 | --tstate->recursion_depth; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3200 | return retval; |
| 3201 | } |
| 3202 | |
| 3203 | |
Benjamin Peterson | 1880d8b | 2009-05-25 13:13:44 +0000 | [diff] [blame] | 3204 | static PyObject * |
| 3205 | special_lookup(PyObject *o, char *meth, PyObject **cache) |
| 3206 | { |
| 3207 | PyObject *res; |
| 3208 | if (PyInstance_Check(o)) { |
| 3209 | if (!*cache) |
| 3210 | return PyObject_GetAttrString(o, meth); |
| 3211 | else |
| 3212 | return PyObject_GetAttr(o, *cache); |
| 3213 | } |
| 3214 | res = _PyObject_LookupSpecial(o, meth, cache); |
| 3215 | if (res == NULL && !PyErr_Occurred()) { |
| 3216 | PyErr_SetObject(PyExc_AttributeError, *cache); |
| 3217 | return NULL; |
| 3218 | } |
| 3219 | return res; |
| 3220 | } |
| 3221 | |
Benjamin Peterson | 78821dd | 2009-01-25 17:15:10 +0000 | [diff] [blame] | 3222 | |
Benjamin Peterson | e18ef19 | 2009-01-20 14:21:16 +0000 | [diff] [blame] | 3223 | static PyObject * |
| 3224 | kwd_as_string(PyObject *kwd) { |
Benjamin Peterson | 78821dd | 2009-01-25 17:15:10 +0000 | [diff] [blame] | 3225 | #ifdef Py_USING_UNICODE |
Benjamin Peterson | e18ef19 | 2009-01-20 14:21:16 +0000 | [diff] [blame] | 3226 | if (PyString_Check(kwd)) { |
Benjamin Peterson | 78821dd | 2009-01-25 17:15:10 +0000 | [diff] [blame] | 3227 | #else |
| 3228 | assert(PyString_Check(kwd)); |
| 3229 | #endif |
Benjamin Peterson | e18ef19 | 2009-01-20 14:21:16 +0000 | [diff] [blame] | 3230 | Py_INCREF(kwd); |
| 3231 | return kwd; |
Benjamin Peterson | 78821dd | 2009-01-25 17:15:10 +0000 | [diff] [blame] | 3232 | #ifdef Py_USING_UNICODE |
Benjamin Peterson | e18ef19 | 2009-01-20 14:21:16 +0000 | [diff] [blame] | 3233 | } |
Benjamin Peterson | 78821dd | 2009-01-25 17:15:10 +0000 | [diff] [blame] | 3234 | return _PyUnicode_AsDefaultEncodedString(kwd, "replace"); |
| 3235 | #endif |
Benjamin Peterson | e18ef19 | 2009-01-20 14:21:16 +0000 | [diff] [blame] | 3236 | } |
| 3237 | |
| 3238 | |
Guido van Rossum | c9fbb72 | 2003-03-01 03:36:33 +0000 | [diff] [blame] | 3239 | /* Implementation notes for set_exc_info() and reset_exc_info(): |
| 3240 | |
| 3241 | - Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and |
| 3242 | 'exc_traceback'. These always travel together. |
| 3243 | |
| 3244 | - tstate->curexc_ZZZ is the "hot" exception that is set by |
| 3245 | PyErr_SetString(), cleared by PyErr_Clear(), and so on. |
| 3246 | |
| 3247 | - Once an exception is caught by an except clause, it is transferred |
| 3248 | from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info() |
| 3249 | 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] | 3250 | 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] | 3251 | |
| 3252 | - Now let me explain the complicated dance with frame->f_exc_ZZZ. |
| 3253 | |
| 3254 | Long ago, when none of this existed, there were just a few globals: |
| 3255 | one set corresponding to the "hot" exception, and one set |
| 3256 | corresponding to sys.exc_ZZZ. (Actually, the latter weren't C |
| 3257 | globals; they were simply stored as sys.exc_ZZZ. For backwards |
| 3258 | compatibility, they still are!) The problem was that in code like |
| 3259 | this: |
| 3260 | |
| 3261 | try: |
| 3262 | "something that may fail" |
| 3263 | except "some exception": |
| 3264 | "do something else first" |
| 3265 | "print the exception from sys.exc_ZZZ." |
| 3266 | |
| 3267 | if "do something else first" invoked something that raised and caught |
| 3268 | an exception, sys.exc_ZZZ were overwritten. That was a frequent |
| 3269 | cause of subtle bugs. I fixed this by changing the semantics as |
| 3270 | follows: |
| 3271 | |
| 3272 | - Within one frame, sys.exc_ZZZ will hold the last exception caught |
| 3273 | *in that frame*. |
| 3274 | |
| 3275 | - But initially, and as long as no exception is caught in a given |
| 3276 | frame, sys.exc_ZZZ will hold the last exception caught in the |
| 3277 | previous frame (or the frame before that, etc.). |
| 3278 | |
| 3279 | The first bullet fixed the bug in the above example. The second |
| 3280 | bullet was for backwards compatibility: it was (and is) common to |
| 3281 | have a function that is called when an exception is caught, and to |
| 3282 | have that function access the caught exception via sys.exc_ZZZ. |
| 3283 | (Example: traceback.print_exc()). |
| 3284 | |
| 3285 | At the same time I fixed the problem that sys.exc_ZZZ weren't |
| 3286 | thread-safe, by introducing sys.exc_info() which gets it from tstate; |
| 3287 | but that's really a separate improvement. |
| 3288 | |
| 3289 | The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ |
| 3290 | variables to what they were before the current frame was called. The |
| 3291 | set_exc_info() function saves them on the frame so that |
| 3292 | reset_exc_info() can restore them. The invariant is that |
| 3293 | frame->f_exc_ZZZ is NULL iff the current frame never caught an |
| 3294 | exception (where "catching" an exception applies only to successful |
| 3295 | except clauses); and if the current frame ever caught an exception, |
| 3296 | frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ |
| 3297 | at the start of the current frame. |
| 3298 | |
| 3299 | */ |
| 3300 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 3301 | static void |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3302 | set_exc_info(PyThreadState *tstate, |
| 3303 | PyObject *type, PyObject *value, PyObject *tb) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3304 | { |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 3305 | PyFrameObject *frame = tstate->frame; |
Guido van Rossum | df4c308 | 1997-05-20 17:06:11 +0000 | [diff] [blame] | 3306 | PyObject *tmp_type, *tmp_value, *tmp_tb; |
Barry Warsaw | 4249f54 | 1997-08-22 21:26:19 +0000 | [diff] [blame] | 3307 | |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 3308 | assert(type != NULL); |
| 3309 | assert(frame != NULL); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3310 | if (frame->f_exc_type == NULL) { |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 3311 | assert(frame->f_exc_value == NULL); |
| 3312 | assert(frame->f_exc_traceback == NULL); |
| 3313 | /* This frame didn't catch an exception before. */ |
| 3314 | /* Save previous exception of this thread in this frame. */ |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3315 | if (tstate->exc_type == NULL) { |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 3316 | /* XXX Why is this set to Py_None? */ |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3317 | Py_INCREF(Py_None); |
| 3318 | tstate->exc_type = Py_None; |
| 3319 | } |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 3320 | Py_INCREF(tstate->exc_type); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3321 | Py_XINCREF(tstate->exc_value); |
| 3322 | Py_XINCREF(tstate->exc_traceback); |
| 3323 | frame->f_exc_type = tstate->exc_type; |
| 3324 | frame->f_exc_value = tstate->exc_value; |
| 3325 | frame->f_exc_traceback = tstate->exc_traceback; |
| 3326 | } |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 3327 | /* Set new exception for this thread. */ |
Guido van Rossum | df4c308 | 1997-05-20 17:06:11 +0000 | [diff] [blame] | 3328 | tmp_type = tstate->exc_type; |
| 3329 | tmp_value = tstate->exc_value; |
| 3330 | tmp_tb = tstate->exc_traceback; |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 3331 | Py_INCREF(type); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3332 | Py_XINCREF(value); |
| 3333 | Py_XINCREF(tb); |
| 3334 | tstate->exc_type = type; |
| 3335 | tstate->exc_value = value; |
| 3336 | tstate->exc_traceback = tb; |
Guido van Rossum | df4c308 | 1997-05-20 17:06:11 +0000 | [diff] [blame] | 3337 | Py_XDECREF(tmp_type); |
| 3338 | Py_XDECREF(tmp_value); |
| 3339 | Py_XDECREF(tmp_tb); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3340 | /* For b/w compatibility */ |
| 3341 | PySys_SetObject("exc_type", type); |
| 3342 | PySys_SetObject("exc_value", value); |
| 3343 | PySys_SetObject("exc_traceback", tb); |
| 3344 | } |
| 3345 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 3346 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3347 | reset_exc_info(PyThreadState *tstate) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3348 | { |
| 3349 | PyFrameObject *frame; |
Guido van Rossum | df4c308 | 1997-05-20 17:06:11 +0000 | [diff] [blame] | 3350 | PyObject *tmp_type, *tmp_value, *tmp_tb; |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 3351 | |
| 3352 | /* It's a precondition that the thread state's frame caught an |
| 3353 | * exception -- verify in a debug build. |
| 3354 | */ |
| 3355 | assert(tstate != NULL); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3356 | frame = tstate->frame; |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 3357 | assert(frame != NULL); |
| 3358 | assert(frame->f_exc_type != NULL); |
| 3359 | |
| 3360 | /* Copy the frame's exception info back to the thread state. */ |
| 3361 | tmp_type = tstate->exc_type; |
| 3362 | tmp_value = tstate->exc_value; |
| 3363 | tmp_tb = tstate->exc_traceback; |
| 3364 | Py_INCREF(frame->f_exc_type); |
| 3365 | Py_XINCREF(frame->f_exc_value); |
| 3366 | Py_XINCREF(frame->f_exc_traceback); |
| 3367 | tstate->exc_type = frame->f_exc_type; |
| 3368 | tstate->exc_value = frame->f_exc_value; |
| 3369 | tstate->exc_traceback = frame->f_exc_traceback; |
| 3370 | Py_XDECREF(tmp_type); |
| 3371 | Py_XDECREF(tmp_value); |
| 3372 | Py_XDECREF(tmp_tb); |
| 3373 | |
| 3374 | /* For b/w compatibility */ |
| 3375 | PySys_SetObject("exc_type", frame->f_exc_type); |
| 3376 | PySys_SetObject("exc_value", frame->f_exc_value); |
| 3377 | PySys_SetObject("exc_traceback", frame->f_exc_traceback); |
| 3378 | |
| 3379 | /* Clear the frame's exception info. */ |
Guido van Rossum | df4c308 | 1997-05-20 17:06:11 +0000 | [diff] [blame] | 3380 | tmp_type = frame->f_exc_type; |
| 3381 | tmp_value = frame->f_exc_value; |
| 3382 | tmp_tb = frame->f_exc_traceback; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3383 | frame->f_exc_type = NULL; |
| 3384 | frame->f_exc_value = NULL; |
| 3385 | frame->f_exc_traceback = NULL; |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 3386 | Py_DECREF(tmp_type); |
Guido van Rossum | df4c308 | 1997-05-20 17:06:11 +0000 | [diff] [blame] | 3387 | Py_XDECREF(tmp_value); |
| 3388 | Py_XDECREF(tmp_tb); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3389 | } |
| 3390 | |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3391 | /* Logic for the raise statement (too complicated for inlining). |
| 3392 | This *consumes* a reference count to each of its arguments. */ |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 3393 | static enum why_code |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3394 | do_raise(PyObject *type, PyObject *value, PyObject *tb) |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3395 | { |
Guido van Rossum | d295f12 | 1998-04-09 21:39:57 +0000 | [diff] [blame] | 3396 | if (type == NULL) { |
| 3397 | /* Reraise */ |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 3398 | PyThreadState *tstate = PyThreadState_GET(); |
Guido van Rossum | d295f12 | 1998-04-09 21:39:57 +0000 | [diff] [blame] | 3399 | type = tstate->exc_type == NULL ? Py_None : tstate->exc_type; |
| 3400 | value = tstate->exc_value; |
| 3401 | tb = tstate->exc_traceback; |
| 3402 | Py_XINCREF(type); |
| 3403 | Py_XINCREF(value); |
| 3404 | Py_XINCREF(tb); |
| 3405 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3406 | |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3407 | /* We support the following forms of raise: |
| 3408 | raise <class>, <classinstance> |
| 3409 | raise <class>, <argument tuple> |
| 3410 | raise <class>, None |
| 3411 | raise <class>, <argument> |
| 3412 | raise <classinstance>, None |
| 3413 | raise <string>, <object> |
| 3414 | raise <string>, None |
| 3415 | |
| 3416 | An omitted second argument is the same as None. |
| 3417 | |
| 3418 | In addition, raise <tuple>, <anything> is the same as |
| 3419 | raising the tuple's first item (and it better have one!); |
| 3420 | this rule is applied recursively. |
| 3421 | |
| 3422 | Finally, an optional third argument can be supplied, which |
| 3423 | gives the traceback to be substituted (useful when |
| 3424 | re-raising an exception after examining it). */ |
| 3425 | |
| 3426 | /* First, check the traceback argument, replacing None with |
| 3427 | NULL. */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3428 | if (tb == Py_None) { |
| 3429 | Py_DECREF(tb); |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3430 | tb = NULL; |
| 3431 | } |
| 3432 | else if (tb != NULL && !PyTraceBack_Check(tb)) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3433 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3434 | "raise: arg 3 must be a traceback or None"); |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3435 | goto raise_error; |
| 3436 | } |
| 3437 | |
| 3438 | /* Next, replace a missing value with None */ |
| 3439 | if (value == NULL) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3440 | value = Py_None; |
| 3441 | Py_INCREF(value); |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3442 | } |
| 3443 | |
| 3444 | /* Next, repeatedly, replace a tuple exception with its first item */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3445 | while (PyTuple_Check(type) && PyTuple_Size(type) > 0) { |
| 3446 | PyObject *tmp = type; |
| 3447 | type = PyTuple_GET_ITEM(type, 0); |
| 3448 | Py_INCREF(type); |
| 3449 | Py_DECREF(tmp); |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3450 | } |
| 3451 | |
Brett Cannon | 129bd52 | 2007-01-30 21:34:36 +0000 | [diff] [blame] | 3452 | if (PyExceptionClass_Check(type)) |
Barry Warsaw | 4249f54 | 1997-08-22 21:26:19 +0000 | [diff] [blame] | 3453 | PyErr_NormalizeException(&type, &value, &tb); |
| 3454 | |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 3455 | else if (PyExceptionInstance_Check(type)) { |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3456 | /* Raising an instance. The value should be a dummy. */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3457 | if (value != Py_None) { |
| 3458 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3459 | "instance exception may not have a separate value"); |
| 3460 | goto raise_error; |
| 3461 | } |
| 3462 | else { |
| 3463 | /* Normalize to raise <class>, <instance> */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3464 | Py_DECREF(value); |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3465 | value = type; |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 3466 | type = PyExceptionInstance_Class(type); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3467 | Py_INCREF(type); |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3468 | } |
| 3469 | } |
| 3470 | else { |
| 3471 | /* Not something you can raise. You get an exception |
| 3472 | anyway, just not what you specified :-) */ |
Jeremy Hylton | 960d948 | 2001-04-27 02:25:33 +0000 | [diff] [blame] | 3473 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | f19a7b9 | 2008-04-27 18:40:21 +0000 | [diff] [blame] | 3474 | "exceptions must be classes or instances, not %s", |
| 3475 | type->ob_type->tp_name); |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3476 | goto raise_error; |
| 3477 | } |
Guido van Rossum | 504153d | 2008-03-18 04:26:48 +0000 | [diff] [blame] | 3478 | |
| 3479 | assert(PyExceptionClass_Check(type)); |
| 3480 | if (Py_Py3kWarningFlag && PyClass_Check(type)) { |
Benjamin Peterson | 9f4f481 | 2008-04-27 03:01:45 +0000 | [diff] [blame] | 3481 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
Benjamin Peterson | f19a7b9 | 2008-04-27 18:40:21 +0000 | [diff] [blame] | 3482 | "exceptions must derive from BaseException " |
| 3483 | "in 3.x", 1) < 0) |
Guido van Rossum | 504153d | 2008-03-18 04:26:48 +0000 | [diff] [blame] | 3484 | goto raise_error; |
| 3485 | } |
| 3486 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3487 | PyErr_Restore(type, value, tb); |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3488 | if (tb == NULL) |
| 3489 | return WHY_EXCEPTION; |
| 3490 | else |
| 3491 | return WHY_RERAISE; |
| 3492 | raise_error: |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3493 | Py_XDECREF(value); |
| 3494 | Py_XDECREF(type); |
| 3495 | Py_XDECREF(tb); |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3496 | return WHY_EXCEPTION; |
| 3497 | } |
| 3498 | |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 3499 | /* Iterate v argcnt times and store the results on the stack (via decreasing |
| 3500 | sp). Return 1 for success, 0 if error. */ |
| 3501 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 3502 | static int |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 3503 | unpack_iterable(PyObject *v, int argcnt, PyObject **sp) |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 3504 | { |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 3505 | int i = 0; |
| 3506 | PyObject *it; /* iter(v) */ |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 3507 | PyObject *w; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3508 | |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 3509 | assert(v != NULL); |
| 3510 | |
| 3511 | it = PyObject_GetIter(v); |
| 3512 | if (it == NULL) |
| 3513 | goto Error; |
| 3514 | |
| 3515 | for (; i < argcnt; i++) { |
| 3516 | w = PyIter_Next(it); |
| 3517 | if (w == NULL) { |
| 3518 | /* Iterator done, via error or exhaustion. */ |
| 3519 | if (!PyErr_Occurred()) { |
| 3520 | PyErr_Format(PyExc_ValueError, |
| 3521 | "need more than %d value%s to unpack", |
| 3522 | i, i == 1 ? "" : "s"); |
| 3523 | } |
| 3524 | goto Error; |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 3525 | } |
| 3526 | *--sp = w; |
| 3527 | } |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 3528 | |
| 3529 | /* We better have exhausted the iterator now. */ |
| 3530 | w = PyIter_Next(it); |
| 3531 | if (w == NULL) { |
| 3532 | if (PyErr_Occurred()) |
| 3533 | goto Error; |
| 3534 | Py_DECREF(it); |
| 3535 | return 1; |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 3536 | } |
Guido van Rossum | bb8f59a | 2001-12-03 19:33:25 +0000 | [diff] [blame] | 3537 | Py_DECREF(w); |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 3538 | PyErr_SetString(PyExc_ValueError, "too many values to unpack"); |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 3539 | /* fall through */ |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 3540 | Error: |
Barry Warsaw | 9101055 | 1997-08-25 22:30:51 +0000 | [diff] [blame] | 3541 | for (; i > 0; i--, sp++) |
| 3542 | Py_DECREF(*sp); |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 3543 | Py_XDECREF(it); |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 3544 | return 0; |
| 3545 | } |
| 3546 | |
| 3547 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 3548 | #ifdef LLTRACE |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 3549 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3550 | prtrace(PyObject *v, char *str) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3551 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3552 | printf("%s ", str); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3553 | if (PyObject_Print(v, stdout, 0) != 0) |
| 3554 | PyErr_Clear(); /* Don't know what else to do */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3555 | printf("\n"); |
Guido van Rossum | cc229ea | 2000-05-04 00:55:17 +0000 | [diff] [blame] | 3556 | return 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3557 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3558 | #endif |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3559 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 3560 | static void |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3561 | call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f) |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 3562 | { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3563 | PyObject *type, *value, *traceback, *arg; |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 3564 | int err; |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3565 | PyErr_Fetch(&type, &value, &traceback); |
Guido van Rossum | bd9ccca | 1992-04-09 14:58:08 +0000 | [diff] [blame] | 3566 | if (value == NULL) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3567 | value = Py_None; |
| 3568 | Py_INCREF(value); |
Guido van Rossum | bd9ccca | 1992-04-09 14:58:08 +0000 | [diff] [blame] | 3569 | } |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 3570 | arg = PyTuple_Pack(3, type, value, traceback); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 3571 | if (arg == NULL) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3572 | PyErr_Restore(type, value, traceback); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 3573 | return; |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 3574 | } |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3575 | err = call_trace(func, self, f, PyTrace_EXCEPTION, arg); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3576 | Py_DECREF(arg); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 3577 | if (err == 0) |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3578 | PyErr_Restore(type, value, traceback); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 3579 | else { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3580 | Py_XDECREF(type); |
| 3581 | Py_XDECREF(value); |
| 3582 | Py_XDECREF(traceback); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 3583 | } |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 3584 | } |
| 3585 | |
Amaury Forgeot d'Arc | 0d75f09 | 2007-11-13 21:54:28 +0000 | [diff] [blame] | 3586 | static int |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 3587 | call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame, |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 3588 | int what, PyObject *arg) |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 3589 | { |
| 3590 | PyObject *type, *value, *traceback; |
| 3591 | int err; |
| 3592 | PyErr_Fetch(&type, &value, &traceback); |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 3593 | err = call_trace(func, obj, frame, what, arg); |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 3594 | if (err == 0) |
Amaury Forgeot d'Arc | 0d75f09 | 2007-11-13 21:54:28 +0000 | [diff] [blame] | 3595 | { |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 3596 | PyErr_Restore(type, value, traceback); |
Amaury Forgeot d'Arc | 0d75f09 | 2007-11-13 21:54:28 +0000 | [diff] [blame] | 3597 | return 0; |
| 3598 | } |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 3599 | else { |
| 3600 | Py_XDECREF(type); |
| 3601 | Py_XDECREF(value); |
| 3602 | Py_XDECREF(traceback); |
Amaury Forgeot d'Arc | 0d75f09 | 2007-11-13 21:54:28 +0000 | [diff] [blame] | 3603 | return -1; |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 3604 | } |
| 3605 | } |
| 3606 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 3607 | static int |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3608 | call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame, |
| 3609 | int what, PyObject *arg) |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 3610 | { |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3611 | register PyThreadState *tstate = frame->f_tstate; |
| 3612 | int result; |
| 3613 | if (tstate->tracing) |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 3614 | return 0; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3615 | tstate->tracing++; |
Fred Drake | 9e3ad78 | 2001-07-03 23:39:52 +0000 | [diff] [blame] | 3616 | tstate->use_tracing = 0; |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3617 | result = func(obj, frame, what, arg); |
Fred Drake | 9e3ad78 | 2001-07-03 23:39:52 +0000 | [diff] [blame] | 3618 | tstate->use_tracing = ((tstate->c_tracefunc != NULL) |
| 3619 | || (tstate->c_profilefunc != NULL)); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3620 | tstate->tracing--; |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3621 | return result; |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 3622 | } |
| 3623 | |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 3624 | PyObject * |
| 3625 | _PyEval_CallTracing(PyObject *func, PyObject *args) |
| 3626 | { |
| 3627 | PyFrameObject *frame = PyEval_GetFrame(); |
| 3628 | PyThreadState *tstate = frame->f_tstate; |
| 3629 | int save_tracing = tstate->tracing; |
| 3630 | int save_use_tracing = tstate->use_tracing; |
| 3631 | PyObject *result; |
| 3632 | |
| 3633 | tstate->tracing = 0; |
| 3634 | tstate->use_tracing = ((tstate->c_tracefunc != NULL) |
| 3635 | || (tstate->c_profilefunc != NULL)); |
| 3636 | result = PyObject_Call(func, args, NULL); |
| 3637 | tstate->tracing = save_tracing; |
| 3638 | tstate->use_tracing = save_use_tracing; |
| 3639 | return result; |
| 3640 | } |
| 3641 | |
Jeffrey Yasskin | 655d835 | 2009-05-23 23:23:01 +0000 | [diff] [blame] | 3642 | /* See Objects/lnotab_notes.txt for a description of how tracing works. */ |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 3643 | static int |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 3644 | maybe_call_line_trace(Py_tracefunc func, PyObject *obj, |
Armin Rigo | bf57a14 | 2004-03-22 19:24:58 +0000 | [diff] [blame] | 3645 | PyFrameObject *frame, int *instr_lb, int *instr_ub, |
| 3646 | int *instr_prev) |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 3647 | { |
Michael W. Hudson | 006c752 | 2002-11-08 13:08:46 +0000 | [diff] [blame] | 3648 | int result = 0; |
Jeffrey Yasskin | 655d835 | 2009-05-23 23:23:01 +0000 | [diff] [blame] | 3649 | int line = frame->f_lineno; |
Michael W. Hudson | 006c752 | 2002-11-08 13:08:46 +0000 | [diff] [blame] | 3650 | |
Jeremy Hylton | a4ebc13 | 2006-04-18 14:47:00 +0000 | [diff] [blame] | 3651 | /* If the last instruction executed isn't in the current |
Jeffrey Yasskin | 655d835 | 2009-05-23 23:23:01 +0000 | [diff] [blame] | 3652 | instruction window, reset the window. |
Jeremy Hylton | a4ebc13 | 2006-04-18 14:47:00 +0000 | [diff] [blame] | 3653 | */ |
Jeffrey Yasskin | 655d835 | 2009-05-23 23:23:01 +0000 | [diff] [blame] | 3654 | if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) { |
Thomas Wouters | ae406c6 | 2007-09-19 17:27:43 +0000 | [diff] [blame] | 3655 | PyAddrPair bounds; |
Jeffrey Yasskin | 655d835 | 2009-05-23 23:23:01 +0000 | [diff] [blame] | 3656 | line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti, |
| 3657 | &bounds); |
Thomas Wouters | ae406c6 | 2007-09-19 17:27:43 +0000 | [diff] [blame] | 3658 | *instr_lb = bounds.ap_lower; |
| 3659 | *instr_ub = bounds.ap_upper; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 3660 | } |
Jeffrey Yasskin | 655d835 | 2009-05-23 23:23:01 +0000 | [diff] [blame] | 3661 | /* If the last instruction falls at the start of a line or if |
| 3662 | it represents a jump backwards, update the frame's line |
| 3663 | number and call the trace function. */ |
| 3664 | if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) { |
| 3665 | frame->f_lineno = line; |
Jeremy Hylton | a4ebc13 | 2006-04-18 14:47:00 +0000 | [diff] [blame] | 3666 | result = call_trace(func, obj, frame, PyTrace_LINE, Py_None); |
Armin Rigo | bf57a14 | 2004-03-22 19:24:58 +0000 | [diff] [blame] | 3667 | } |
| 3668 | *instr_prev = frame->f_lasti; |
Michael W. Hudson | 006c752 | 2002-11-08 13:08:46 +0000 | [diff] [blame] | 3669 | return result; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 3670 | } |
| 3671 | |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3672 | void |
| 3673 | PyEval_SetProfile(Py_tracefunc func, PyObject *arg) |
Fred Drake | d083839 | 2001-06-16 21:02:31 +0000 | [diff] [blame] | 3674 | { |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 3675 | PyThreadState *tstate = PyThreadState_GET(); |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3676 | PyObject *temp = tstate->c_profileobj; |
| 3677 | Py_XINCREF(arg); |
| 3678 | tstate->c_profilefunc = NULL; |
| 3679 | tstate->c_profileobj = NULL; |
Brett Cannon | 55fa66d | 2005-06-25 07:07:35 +0000 | [diff] [blame] | 3680 | /* Must make sure that tracing is not ignored if 'temp' is freed */ |
Fred Drake | 9e3ad78 | 2001-07-03 23:39:52 +0000 | [diff] [blame] | 3681 | tstate->use_tracing = tstate->c_tracefunc != NULL; |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3682 | Py_XDECREF(temp); |
| 3683 | tstate->c_profilefunc = func; |
| 3684 | tstate->c_profileobj = arg; |
Brett Cannon | 55fa66d | 2005-06-25 07:07:35 +0000 | [diff] [blame] | 3685 | /* Flag that tracing or profiling is turned on */ |
Fred Drake | 9e3ad78 | 2001-07-03 23:39:52 +0000 | [diff] [blame] | 3686 | tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL); |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3687 | } |
| 3688 | |
| 3689 | void |
| 3690 | PyEval_SetTrace(Py_tracefunc func, PyObject *arg) |
| 3691 | { |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 3692 | PyThreadState *tstate = PyThreadState_GET(); |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3693 | PyObject *temp = tstate->c_traceobj; |
Jeffrey Yasskin | fd8a1ec | 2008-12-03 06:46:45 +0000 | [diff] [blame] | 3694 | _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL); |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3695 | Py_XINCREF(arg); |
| 3696 | tstate->c_tracefunc = NULL; |
| 3697 | tstate->c_traceobj = NULL; |
Brett Cannon | 55fa66d | 2005-06-25 07:07:35 +0000 | [diff] [blame] | 3698 | /* Must make sure that profiling is not ignored if 'temp' is freed */ |
Fred Drake | 9e3ad78 | 2001-07-03 23:39:52 +0000 | [diff] [blame] | 3699 | tstate->use_tracing = tstate->c_profilefunc != NULL; |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3700 | Py_XDECREF(temp); |
| 3701 | tstate->c_tracefunc = func; |
| 3702 | tstate->c_traceobj = arg; |
Brett Cannon | 55fa66d | 2005-06-25 07:07:35 +0000 | [diff] [blame] | 3703 | /* Flag that tracing or profiling is turned on */ |
Fred Drake | 9e3ad78 | 2001-07-03 23:39:52 +0000 | [diff] [blame] | 3704 | tstate->use_tracing = ((func != NULL) |
| 3705 | || (tstate->c_profilefunc != NULL)); |
Fred Drake | d083839 | 2001-06-16 21:02:31 +0000 | [diff] [blame] | 3706 | } |
| 3707 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3708 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3709 | PyEval_GetBuiltins(void) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 3710 | { |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 3711 | PyFrameObject *current_frame = PyEval_GetFrame(); |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 3712 | if (current_frame == NULL) |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 3713 | return PyThreadState_GET()->interp->builtins; |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 3714 | else |
| 3715 | return current_frame->f_builtins; |
| 3716 | } |
| 3717 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3718 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3719 | PyEval_GetLocals(void) |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 3720 | { |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 3721 | PyFrameObject *current_frame = PyEval_GetFrame(); |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 3722 | if (current_frame == NULL) |
| 3723 | return NULL; |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3724 | PyFrame_FastToLocals(current_frame); |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 3725 | return current_frame->f_locals; |
| 3726 | } |
| 3727 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3728 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3729 | PyEval_GetGlobals(void) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3730 | { |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 3731 | PyFrameObject *current_frame = PyEval_GetFrame(); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3732 | if (current_frame == NULL) |
| 3733 | return NULL; |
| 3734 | else |
| 3735 | return current_frame->f_globals; |
| 3736 | } |
| 3737 | |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 3738 | PyFrameObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3739 | PyEval_GetFrame(void) |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 3740 | { |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 3741 | PyThreadState *tstate = PyThreadState_GET(); |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 3742 | return _PyThreadState_GetFrame(tstate); |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 3743 | } |
| 3744 | |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 3745 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3746 | PyEval_GetRestricted(void) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 3747 | { |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 3748 | PyFrameObject *current_frame = PyEval_GetFrame(); |
Neal Norwitz | b9845e7 | 2006-06-12 02:11:18 +0000 | [diff] [blame] | 3749 | return current_frame == NULL ? 0 : PyFrame_IsRestricted(current_frame); |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 3750 | } |
| 3751 | |
Guido van Rossum | be27026 | 1997-05-22 22:26:18 +0000 | [diff] [blame] | 3752 | int |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 3753 | PyEval_MergeCompilerFlags(PyCompilerFlags *cf) |
Jeremy Hylton | 061d106 | 2001-03-22 02:32:48 +0000 | [diff] [blame] | 3754 | { |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 3755 | PyFrameObject *current_frame = PyEval_GetFrame(); |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 3756 | int result = cf->cf_flags != 0; |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 3757 | |
| 3758 | if (current_frame != NULL) { |
| 3759 | const int codeflags = current_frame->f_code->co_flags; |
Tim Peters | e2c18e9 | 2001-08-17 20:47:47 +0000 | [diff] [blame] | 3760 | const int compilerflags = codeflags & PyCF_MASK; |
| 3761 | if (compilerflags) { |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 3762 | result = 1; |
Tim Peters | e2c18e9 | 2001-08-17 20:47:47 +0000 | [diff] [blame] | 3763 | cf->cf_flags |= compilerflags; |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 3764 | } |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 3765 | #if 0 /* future keyword */ |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 3766 | if (codeflags & CO_GENERATOR_ALLOWED) { |
| 3767 | result = 1; |
| 3768 | cf->cf_flags |= CO_GENERATOR_ALLOWED; |
| 3769 | } |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 3770 | #endif |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 3771 | } |
| 3772 | return result; |
Jeremy Hylton | 061d106 | 2001-03-22 02:32:48 +0000 | [diff] [blame] | 3773 | } |
| 3774 | |
| 3775 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3776 | Py_FlushLine(void) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3777 | { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3778 | PyObject *f = PySys_GetObject("stdout"); |
Guido van Rossum | be27026 | 1997-05-22 22:26:18 +0000 | [diff] [blame] | 3779 | if (f == NULL) |
| 3780 | return 0; |
| 3781 | if (!PyFile_SoftSpace(f, 0)) |
| 3782 | return 0; |
| 3783 | return PyFile_WriteString("\n", f); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3784 | } |
| 3785 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3786 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3787 | /* External interface to call any callable object. |
| 3788 | The arg must be a tuple or NULL. */ |
Guido van Rossum | 83bf35c | 1991-07-27 21:32:34 +0000 | [diff] [blame] | 3789 | |
Guido van Rossum | d7ed683 | 1997-08-30 15:02:50 +0000 | [diff] [blame] | 3790 | #undef PyEval_CallObject |
| 3791 | /* for backward compatibility: export this interface */ |
| 3792 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3793 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3794 | PyEval_CallObject(PyObject *func, PyObject *arg) |
Guido van Rossum | 83bf35c | 1991-07-27 21:32:34 +0000 | [diff] [blame] | 3795 | { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3796 | return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3797 | } |
Guido van Rossum | d7ed683 | 1997-08-30 15:02:50 +0000 | [diff] [blame] | 3798 | #define PyEval_CallObject(func,arg) \ |
| 3799 | PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL) |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 3800 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3801 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3802 | PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3803 | { |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3804 | PyObject *result; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3805 | |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3806 | if (arg == NULL) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3807 | arg = PyTuple_New(0); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3808 | if (arg == NULL) |
| 3809 | return NULL; |
| 3810 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3811 | else if (!PyTuple_Check(arg)) { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3812 | PyErr_SetString(PyExc_TypeError, |
| 3813 | "argument list must be a tuple"); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3814 | return NULL; |
| 3815 | } |
| 3816 | else |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3817 | Py_INCREF(arg); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3818 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3819 | if (kw != NULL && !PyDict_Check(kw)) { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3820 | PyErr_SetString(PyExc_TypeError, |
| 3821 | "keyword list must be a dictionary"); |
Guido van Rossum | 25826c9 | 2000-04-21 21:17:39 +0000 | [diff] [blame] | 3822 | Py_DECREF(arg); |
Guido van Rossum | e3e61c1 | 1995-08-04 04:14:47 +0000 | [diff] [blame] | 3823 | return NULL; |
| 3824 | } |
| 3825 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3826 | result = PyObject_Call(func, arg, kw); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3827 | Py_DECREF(arg); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3828 | return result; |
| 3829 | } |
| 3830 | |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 3831 | const char * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3832 | PyEval_GetFuncName(PyObject *func) |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 3833 | { |
| 3834 | if (PyMethod_Check(func)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3835 | return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func)); |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 3836 | else if (PyFunction_Check(func)) |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3837 | return PyString_AsString(((PyFunctionObject*)func)->func_name); |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 3838 | else if (PyCFunction_Check(func)) |
| 3839 | return ((PyCFunctionObject*)func)->m_ml->ml_name; |
| 3840 | else if (PyClass_Check(func)) |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3841 | return PyString_AsString(((PyClassObject*)func)->cl_name); |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 3842 | else if (PyInstance_Check(func)) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3843 | return PyString_AsString( |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 3844 | ((PyInstanceObject*)func)->in_class->cl_name); |
| 3845 | } else { |
| 3846 | return func->ob_type->tp_name; |
| 3847 | } |
| 3848 | } |
| 3849 | |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 3850 | const char * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3851 | PyEval_GetFuncDesc(PyObject *func) |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 3852 | { |
| 3853 | if (PyMethod_Check(func)) |
| 3854 | return "()"; |
| 3855 | else if (PyFunction_Check(func)) |
| 3856 | return "()"; |
| 3857 | else if (PyCFunction_Check(func)) |
| 3858 | return "()"; |
| 3859 | else if (PyClass_Check(func)) |
| 3860 | return " constructor"; |
| 3861 | else if (PyInstance_Check(func)) { |
| 3862 | return " instance"; |
| 3863 | } else { |
| 3864 | return " object"; |
| 3865 | } |
| 3866 | } |
| 3867 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 3868 | static void |
Jeremy Hylton | 192690e | 2002-08-16 18:36:11 +0000 | [diff] [blame] | 3869 | err_args(PyObject *func, int flags, int nargs) |
| 3870 | { |
| 3871 | if (flags & METH_NOARGS) |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 3872 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 86c659a | 2002-08-23 14:11:35 +0000 | [diff] [blame] | 3873 | "%.200s() takes no arguments (%d given)", |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 3874 | ((PyCFunctionObject *)func)->m_ml->ml_name, |
Jeremy Hylton | 192690e | 2002-08-16 18:36:11 +0000 | [diff] [blame] | 3875 | nargs); |
| 3876 | else |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 3877 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 86c659a | 2002-08-23 14:11:35 +0000 | [diff] [blame] | 3878 | "%.200s() takes exactly one argument (%d given)", |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 3879 | ((PyCFunctionObject *)func)->m_ml->ml_name, |
Jeremy Hylton | 192690e | 2002-08-16 18:36:11 +0000 | [diff] [blame] | 3880 | nargs); |
| 3881 | } |
| 3882 | |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 3883 | #define C_TRACE(x, call) \ |
Nicholas Bastin | d858a77 | 2004-06-25 23:31:06 +0000 | [diff] [blame] | 3884 | if (tstate->use_tracing && tstate->c_profilefunc) { \ |
| 3885 | if (call_trace(tstate->c_profilefunc, \ |
| 3886 | tstate->c_profileobj, \ |
| 3887 | tstate->frame, PyTrace_C_CALL, \ |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 3888 | func)) { \ |
| 3889 | x = NULL; \ |
| 3890 | } \ |
| 3891 | else { \ |
| 3892 | x = call; \ |
| 3893 | if (tstate->c_profilefunc != NULL) { \ |
| 3894 | if (x == NULL) { \ |
| 3895 | call_trace_protected(tstate->c_profilefunc, \ |
| 3896 | tstate->c_profileobj, \ |
| 3897 | tstate->frame, PyTrace_C_EXCEPTION, \ |
| 3898 | func); \ |
| 3899 | /* XXX should pass (type, value, tb) */ \ |
| 3900 | } else { \ |
| 3901 | if (call_trace(tstate->c_profilefunc, \ |
| 3902 | tstate->c_profileobj, \ |
| 3903 | tstate->frame, PyTrace_C_RETURN, \ |
| 3904 | func)) { \ |
| 3905 | Py_DECREF(x); \ |
| 3906 | x = NULL; \ |
| 3907 | } \ |
| 3908 | } \ |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 3909 | } \ |
Nicholas Bastin | d858a77 | 2004-06-25 23:31:06 +0000 | [diff] [blame] | 3910 | } \ |
| 3911 | } else { \ |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 3912 | x = call; \ |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 3913 | } |
| 3914 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 3915 | static PyObject * |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 3916 | call_function(PyObject ***pp_stack, int oparg |
| 3917 | #ifdef WITH_TSC |
| 3918 | , uint64* pintr0, uint64* pintr1 |
| 3919 | #endif |
| 3920 | ) |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 3921 | { |
| 3922 | int na = oparg & 0xff; |
| 3923 | int nk = (oparg>>8) & 0xff; |
| 3924 | int n = na + 2 * nk; |
| 3925 | PyObject **pfunc = (*pp_stack) - n - 1; |
| 3926 | PyObject *func = *pfunc; |
| 3927 | PyObject *x, *w; |
| 3928 | |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 3929 | /* Always dispatch PyCFunction first, because these are |
| 3930 | presumed to be the most frequent callable object. |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 3931 | */ |
| 3932 | if (PyCFunction_Check(func) && nk == 0) { |
| 3933 | int flags = PyCFunction_GET_FLAGS(func); |
Nicholas Bastin | d858a77 | 2004-06-25 23:31:06 +0000 | [diff] [blame] | 3934 | PyThreadState *tstate = PyThreadState_GET(); |
Raymond Hettinger | a7f56bc | 2004-06-26 04:34:33 +0000 | [diff] [blame] | 3935 | |
| 3936 | PCALL(PCALL_CFUNCTION); |
Jeremy Hylton | 192690e | 2002-08-16 18:36:11 +0000 | [diff] [blame] | 3937 | if (flags & (METH_NOARGS | METH_O)) { |
| 3938 | PyCFunction meth = PyCFunction_GET_FUNCTION(func); |
| 3939 | PyObject *self = PyCFunction_GET_SELF(func); |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 3940 | if (flags & METH_NOARGS && na == 0) { |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 3941 | C_TRACE(x, (*meth)(self,NULL)); |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 3942 | } |
Jeremy Hylton | 192690e | 2002-08-16 18:36:11 +0000 | [diff] [blame] | 3943 | else if (flags & METH_O && na == 1) { |
| 3944 | PyObject *arg = EXT_POP(*pp_stack); |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 3945 | C_TRACE(x, (*meth)(self,arg)); |
Jeremy Hylton | 192690e | 2002-08-16 18:36:11 +0000 | [diff] [blame] | 3946 | Py_DECREF(arg); |
| 3947 | } |
| 3948 | else { |
| 3949 | err_args(func, flags, na); |
| 3950 | x = NULL; |
| 3951 | } |
| 3952 | } |
| 3953 | else { |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 3954 | PyObject *callargs; |
| 3955 | callargs = load_args(pp_stack, na); |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 3956 | READ_TIMESTAMP(*pintr0); |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 3957 | C_TRACE(x, PyCFunction_Call(func,callargs,NULL)); |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 3958 | READ_TIMESTAMP(*pintr1); |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 3959 | Py_XDECREF(callargs); |
| 3960 | } |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 3961 | } else { |
| 3962 | if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) { |
| 3963 | /* optimize access to bound methods */ |
| 3964 | PyObject *self = PyMethod_GET_SELF(func); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 3965 | PCALL(PCALL_METHOD); |
| 3966 | PCALL(PCALL_BOUND_METHOD); |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 3967 | Py_INCREF(self); |
| 3968 | func = PyMethod_GET_FUNCTION(func); |
| 3969 | Py_INCREF(func); |
| 3970 | Py_DECREF(*pfunc); |
| 3971 | *pfunc = self; |
| 3972 | na++; |
| 3973 | n++; |
| 3974 | } else |
| 3975 | Py_INCREF(func); |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 3976 | READ_TIMESTAMP(*pintr0); |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 3977 | if (PyFunction_Check(func)) |
| 3978 | x = fast_function(func, pp_stack, n, na, nk); |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 3979 | else |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 3980 | x = do_call(func, pp_stack, na, nk); |
Michael W. Hudson | 75eabd2 | 2005-01-18 15:56:11 +0000 | [diff] [blame] | 3981 | READ_TIMESTAMP(*pintr1); |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 3982 | Py_DECREF(func); |
| 3983 | } |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 3984 | |
Armin Rigo | d34fa52 | 2006-03-28 19:10:40 +0000 | [diff] [blame] | 3985 | /* Clear the stack of the function object. Also removes |
| 3986 | the arguments in case they weren't consumed already |
| 3987 | (fast_function() and err_args() leave them on the stack). |
Thomas Wouters | 7f59732 | 2006-03-01 05:32:33 +0000 | [diff] [blame] | 3988 | */ |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 3989 | while ((*pp_stack) > pfunc) { |
| 3990 | w = EXT_POP(*pp_stack); |
| 3991 | Py_DECREF(w); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 3992 | PCALL(PCALL_POP); |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 3993 | } |
| 3994 | return x; |
| 3995 | } |
| 3996 | |
Jeremy Hylton | 192690e | 2002-08-16 18:36:11 +0000 | [diff] [blame] | 3997 | /* The fast_function() function optimize calls for which no argument |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3998 | tuple is necessary; the objects are passed directly from the stack. |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 3999 | For the simplest case -- a function that takes only positional |
| 4000 | arguments and is called with only positional arguments -- it |
| 4001 | inlines the most primitive frame setup code from |
| 4002 | PyEval_EvalCodeEx(), which vastly reduces the checks that must be |
| 4003 | done before evaluating the frame. |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4004 | */ |
| 4005 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4006 | static PyObject * |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4007 | 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] | 4008 | { |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4009 | PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4010 | PyObject *globals = PyFunction_GET_GLOBALS(func); |
| 4011 | PyObject *argdefs = PyFunction_GET_DEFAULTS(func); |
| 4012 | PyObject **d = NULL; |
| 4013 | int nd = 0; |
| 4014 | |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4015 | PCALL(PCALL_FUNCTION); |
| 4016 | PCALL(PCALL_FAST_FUNCTION); |
Raymond Hettinger | 40174c3 | 2003-05-31 07:04:16 +0000 | [diff] [blame] | 4017 | if (argdefs == NULL && co->co_argcount == n && nk==0 && |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4018 | co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) { |
| 4019 | PyFrameObject *f; |
| 4020 | PyObject *retval = NULL; |
| 4021 | PyThreadState *tstate = PyThreadState_GET(); |
| 4022 | PyObject **fastlocals, **stack; |
| 4023 | int i; |
| 4024 | |
| 4025 | PCALL(PCALL_FASTER_FUNCTION); |
| 4026 | assert(globals != NULL); |
| 4027 | /* XXX Perhaps we should create a specialized |
| 4028 | PyFrame_New() that doesn't take locals, but does |
| 4029 | take builtins without sanity checking them. |
| 4030 | */ |
Neal Norwitz | df6a649 | 2006-08-13 18:10:10 +0000 | [diff] [blame] | 4031 | assert(tstate != NULL); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4032 | f = PyFrame_New(tstate, co, globals, NULL); |
| 4033 | if (f == NULL) |
| 4034 | return NULL; |
| 4035 | |
| 4036 | fastlocals = f->f_localsplus; |
| 4037 | stack = (*pp_stack) - n; |
| 4038 | |
| 4039 | for (i = 0; i < n; i++) { |
| 4040 | Py_INCREF(*stack); |
| 4041 | fastlocals[i] = *stack++; |
| 4042 | } |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 4043 | retval = PyEval_EvalFrameEx(f,0); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4044 | ++tstate->recursion_depth; |
| 4045 | Py_DECREF(f); |
| 4046 | --tstate->recursion_depth; |
| 4047 | return retval; |
| 4048 | } |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4049 | if (argdefs != NULL) { |
| 4050 | d = &PyTuple_GET_ITEM(argdefs, 0); |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4051 | nd = Py_SIZE(argdefs); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4052 | } |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4053 | return PyEval_EvalCodeEx(co, globals, |
| 4054 | (PyObject *)NULL, (*pp_stack)-n, na, |
| 4055 | (*pp_stack)-2*nk, nk, d, nd, |
| 4056 | PyFunction_GET_CLOSURE(func)); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4057 | } |
| 4058 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4059 | static PyObject * |
Ka-Ping Yee | 2057970 | 2001-01-15 22:14:16 +0000 | [diff] [blame] | 4060 | update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack, |
| 4061 | PyObject *func) |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4062 | { |
| 4063 | PyObject *kwdict = NULL; |
| 4064 | if (orig_kwdict == NULL) |
| 4065 | kwdict = PyDict_New(); |
| 4066 | else { |
| 4067 | kwdict = PyDict_Copy(orig_kwdict); |
| 4068 | Py_DECREF(orig_kwdict); |
| 4069 | } |
| 4070 | if (kwdict == NULL) |
| 4071 | return NULL; |
Raymond Hettinger | 5bed456 | 2004-04-10 23:34:17 +0000 | [diff] [blame] | 4072 | while (--nk >= 0) { |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4073 | int err; |
| 4074 | PyObject *value = EXT_POP(*pp_stack); |
| 4075 | PyObject *key = EXT_POP(*pp_stack); |
| 4076 | if (PyDict_GetItem(kwdict, key) != NULL) { |
Thomas Wouters | ae406c6 | 2007-09-19 17:27:43 +0000 | [diff] [blame] | 4077 | PyErr_Format(PyExc_TypeError, |
| 4078 | "%.200s%s got multiple values " |
| 4079 | "for keyword argument '%.200s'", |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4080 | PyEval_GetFuncName(func), |
| 4081 | PyEval_GetFuncDesc(func), |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4082 | PyString_AsString(key)); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4083 | Py_DECREF(key); |
| 4084 | Py_DECREF(value); |
| 4085 | Py_DECREF(kwdict); |
| 4086 | return NULL; |
| 4087 | } |
| 4088 | err = PyDict_SetItem(kwdict, key, value); |
| 4089 | Py_DECREF(key); |
| 4090 | Py_DECREF(value); |
| 4091 | if (err) { |
| 4092 | Py_DECREF(kwdict); |
| 4093 | return NULL; |
| 4094 | } |
| 4095 | } |
| 4096 | return kwdict; |
| 4097 | } |
| 4098 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4099 | static PyObject * |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4100 | update_star_args(int nstack, int nstar, PyObject *stararg, |
| 4101 | PyObject ***pp_stack) |
| 4102 | { |
| 4103 | PyObject *callargs, *w; |
| 4104 | |
| 4105 | callargs = PyTuple_New(nstack + nstar); |
| 4106 | if (callargs == NULL) { |
| 4107 | return NULL; |
| 4108 | } |
| 4109 | if (nstar) { |
| 4110 | int i; |
| 4111 | for (i = 0; i < nstar; i++) { |
| 4112 | PyObject *a = PyTuple_GET_ITEM(stararg, i); |
| 4113 | Py_INCREF(a); |
| 4114 | PyTuple_SET_ITEM(callargs, nstack + i, a); |
| 4115 | } |
| 4116 | } |
Raymond Hettinger | 5bed456 | 2004-04-10 23:34:17 +0000 | [diff] [blame] | 4117 | while (--nstack >= 0) { |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4118 | w = EXT_POP(*pp_stack); |
| 4119 | PyTuple_SET_ITEM(callargs, nstack, w); |
| 4120 | } |
| 4121 | return callargs; |
| 4122 | } |
| 4123 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4124 | static PyObject * |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4125 | load_args(PyObject ***pp_stack, int na) |
| 4126 | { |
| 4127 | PyObject *args = PyTuple_New(na); |
| 4128 | PyObject *w; |
| 4129 | |
| 4130 | if (args == NULL) |
| 4131 | return NULL; |
Raymond Hettinger | 5bed456 | 2004-04-10 23:34:17 +0000 | [diff] [blame] | 4132 | while (--na >= 0) { |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4133 | w = EXT_POP(*pp_stack); |
| 4134 | PyTuple_SET_ITEM(args, na, w); |
| 4135 | } |
| 4136 | return args; |
| 4137 | } |
| 4138 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4139 | static PyObject * |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4140 | do_call(PyObject *func, PyObject ***pp_stack, int na, int nk) |
| 4141 | { |
| 4142 | PyObject *callargs = NULL; |
| 4143 | PyObject *kwdict = NULL; |
| 4144 | PyObject *result = NULL; |
| 4145 | |
| 4146 | if (nk > 0) { |
Ka-Ping Yee | 2057970 | 2001-01-15 22:14:16 +0000 | [diff] [blame] | 4147 | kwdict = update_keyword_args(NULL, nk, pp_stack, func); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4148 | if (kwdict == NULL) |
| 4149 | goto call_fail; |
| 4150 | } |
| 4151 | callargs = load_args(pp_stack, na); |
| 4152 | if (callargs == NULL) |
| 4153 | goto call_fail; |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4154 | #ifdef CALL_PROFILE |
| 4155 | /* At this point, we have to look at the type of func to |
| 4156 | update the call stats properly. Do it here so as to avoid |
| 4157 | exposing the call stats machinery outside ceval.c |
| 4158 | */ |
| 4159 | if (PyFunction_Check(func)) |
| 4160 | PCALL(PCALL_FUNCTION); |
| 4161 | else if (PyMethod_Check(func)) |
| 4162 | PCALL(PCALL_METHOD); |
| 4163 | else if (PyType_Check(func)) |
| 4164 | PCALL(PCALL_TYPE); |
Antoine Pitrou | 46dbe27 | 2009-05-30 21:27:00 +0000 | [diff] [blame] | 4165 | else if (PyCFunction_Check(func)) |
| 4166 | PCALL(PCALL_CFUNCTION); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4167 | else |
| 4168 | PCALL(PCALL_OTHER); |
| 4169 | #endif |
Antoine Pitrou | 46dbe27 | 2009-05-30 21:27:00 +0000 | [diff] [blame] | 4170 | if (PyCFunction_Check(func)) { |
| 4171 | PyThreadState *tstate = PyThreadState_GET(); |
| 4172 | C_TRACE(result, PyCFunction_Call(func, callargs, kwdict)); |
| 4173 | } |
| 4174 | else |
| 4175 | result = PyObject_Call(func, callargs, kwdict); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4176 | call_fail: |
| 4177 | Py_XDECREF(callargs); |
| 4178 | Py_XDECREF(kwdict); |
| 4179 | return result; |
| 4180 | } |
| 4181 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4182 | static PyObject * |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4183 | ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk) |
| 4184 | { |
| 4185 | int nstar = 0; |
| 4186 | PyObject *callargs = NULL; |
| 4187 | PyObject *stararg = NULL; |
| 4188 | PyObject *kwdict = NULL; |
| 4189 | PyObject *result = NULL; |
| 4190 | |
| 4191 | if (flags & CALL_FLAG_KW) { |
| 4192 | kwdict = EXT_POP(*pp_stack); |
Georg Brandl | 2134e75 | 2007-05-21 20:34:16 +0000 | [diff] [blame] | 4193 | if (!PyDict_Check(kwdict)) { |
| 4194 | PyObject *d; |
| 4195 | d = PyDict_New(); |
| 4196 | if (d == NULL) |
| 4197 | goto ext_call_fail; |
| 4198 | if (PyDict_Update(d, kwdict) != 0) { |
| 4199 | Py_DECREF(d); |
| 4200 | /* PyDict_Update raises attribute |
| 4201 | * error (percolated from an attempt |
| 4202 | * to get 'keys' attribute) instead of |
| 4203 | * a type error if its second argument |
| 4204 | * is not a mapping. |
| 4205 | */ |
| 4206 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 4207 | PyErr_Format(PyExc_TypeError, |
| 4208 | "%.200s%.200s argument after ** " |
| 4209 | "must be a mapping, not %.200s", |
| 4210 | PyEval_GetFuncName(func), |
| 4211 | PyEval_GetFuncDesc(func), |
| 4212 | kwdict->ob_type->tp_name); |
| 4213 | } |
| 4214 | goto ext_call_fail; |
| 4215 | } |
| 4216 | Py_DECREF(kwdict); |
| 4217 | kwdict = d; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4218 | } |
| 4219 | } |
| 4220 | if (flags & CALL_FLAG_VAR) { |
| 4221 | stararg = EXT_POP(*pp_stack); |
| 4222 | if (!PyTuple_Check(stararg)) { |
| 4223 | PyObject *t = NULL; |
| 4224 | t = PySequence_Tuple(stararg); |
| 4225 | if (t == NULL) { |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4226 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 4227 | PyErr_Format(PyExc_TypeError, |
Georg Brandl | 2134e75 | 2007-05-21 20:34:16 +0000 | [diff] [blame] | 4228 | "%.200s%.200s argument after * " |
| 4229 | "must be a sequence, not %200s", |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4230 | PyEval_GetFuncName(func), |
Georg Brandl | 2134e75 | 2007-05-21 20:34:16 +0000 | [diff] [blame] | 4231 | PyEval_GetFuncDesc(func), |
| 4232 | stararg->ob_type->tp_name); |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4233 | } |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4234 | goto ext_call_fail; |
| 4235 | } |
| 4236 | Py_DECREF(stararg); |
| 4237 | stararg = t; |
| 4238 | } |
| 4239 | nstar = PyTuple_GET_SIZE(stararg); |
| 4240 | } |
| 4241 | if (nk > 0) { |
Ka-Ping Yee | 2057970 | 2001-01-15 22:14:16 +0000 | [diff] [blame] | 4242 | kwdict = update_keyword_args(kwdict, nk, pp_stack, func); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4243 | if (kwdict == NULL) |
| 4244 | goto ext_call_fail; |
| 4245 | } |
| 4246 | callargs = update_star_args(na, nstar, stararg, pp_stack); |
| 4247 | if (callargs == NULL) |
| 4248 | goto ext_call_fail; |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4249 | #ifdef CALL_PROFILE |
| 4250 | /* At this point, we have to look at the type of func to |
| 4251 | update the call stats properly. Do it here so as to avoid |
| 4252 | exposing the call stats machinery outside ceval.c |
| 4253 | */ |
| 4254 | if (PyFunction_Check(func)) |
| 4255 | PCALL(PCALL_FUNCTION); |
| 4256 | else if (PyMethod_Check(func)) |
| 4257 | PCALL(PCALL_METHOD); |
| 4258 | else if (PyType_Check(func)) |
| 4259 | PCALL(PCALL_TYPE); |
Antoine Pitrou | 46dbe27 | 2009-05-30 21:27:00 +0000 | [diff] [blame] | 4260 | else if (PyCFunction_Check(func)) |
| 4261 | PCALL(PCALL_CFUNCTION); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4262 | else |
| 4263 | PCALL(PCALL_OTHER); |
| 4264 | #endif |
Antoine Pitrou | 46dbe27 | 2009-05-30 21:27:00 +0000 | [diff] [blame] | 4265 | if (PyCFunction_Check(func)) { |
| 4266 | PyThreadState *tstate = PyThreadState_GET(); |
| 4267 | C_TRACE(result, PyCFunction_Call(func, callargs, kwdict)); |
| 4268 | } |
| 4269 | else |
| 4270 | result = PyObject_Call(func, callargs, kwdict); |
Thomas Wouters | ae406c6 | 2007-09-19 17:27:43 +0000 | [diff] [blame] | 4271 | ext_call_fail: |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4272 | Py_XDECREF(callargs); |
| 4273 | Py_XDECREF(kwdict); |
| 4274 | Py_XDECREF(stararg); |
| 4275 | return result; |
| 4276 | } |
| 4277 | |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 4278 | /* Extract a slice index from a PyInt or PyLong or an object with the |
| 4279 | nb_index slot defined, and store in *pi. |
| 4280 | Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX, |
| 4281 | 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] | 4282 | Return 0 on error, 1 on success. |
Tim Peters | cb479e7 | 2001-12-16 19:11:44 +0000 | [diff] [blame] | 4283 | */ |
Tim Peters | b519638 | 2001-12-16 19:44:20 +0000 | [diff] [blame] | 4284 | /* Note: If v is NULL, return success without storing into *pi. This |
| 4285 | is because_PyEval_SliceIndex() is called by apply_slice(), which can be |
| 4286 | 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] | 4287 | */ |
Guido van Rossum | 20c6add | 2000-05-08 14:06:50 +0000 | [diff] [blame] | 4288 | int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4289 | _PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4290 | { |
Tim Peters | b519638 | 2001-12-16 19:44:20 +0000 | [diff] [blame] | 4291 | if (v != NULL) { |
Martin v. Löwis | dde99d2 | 2006-02-17 15:57:41 +0000 | [diff] [blame] | 4292 | Py_ssize_t x; |
Andrew M. Kuchling | 2194b16 | 2000-02-23 22:18:48 +0000 | [diff] [blame] | 4293 | if (PyInt_Check(v)) { |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 4294 | /* XXX(nnorwitz): I think PyInt_AS_LONG is correct, |
| 4295 | however, it looks like it should be AsSsize_t. |
| 4296 | There should be a comment here explaining why. |
| 4297 | */ |
| 4298 | x = PyInt_AS_LONG(v); |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 4299 | } |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 4300 | else if (PyIndex_Check(v)) { |
| 4301 | x = PyNumber_AsSsize_t(v, NULL); |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 4302 | if (x == -1 && PyErr_Occurred()) |
| 4303 | return 0; |
| 4304 | } |
| 4305 | else { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 4306 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 4307 | "slice indices must be integers or " |
| 4308 | "None or have an __index__ method"); |
Guido van Rossum | 20c6add | 2000-05-08 14:06:50 +0000 | [diff] [blame] | 4309 | return 0; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4310 | } |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 4311 | *pi = x; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4312 | } |
Guido van Rossum | 20c6add | 2000-05-08 14:06:50 +0000 | [diff] [blame] | 4313 | return 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4314 | } |
| 4315 | |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 4316 | #undef ISINDEX |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 4317 | #define ISINDEX(x) ((x) == NULL || \ |
| 4318 | PyInt_Check(x) || PyLong_Check(x) || PyIndex_Check(x)) |
Guido van Rossum | 50d756e | 2001-08-18 17:43:36 +0000 | [diff] [blame] | 4319 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4320 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4321 | 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] | 4322 | { |
Guido van Rossum | 50d756e | 2001-08-18 17:43:36 +0000 | [diff] [blame] | 4323 | PyTypeObject *tp = u->ob_type; |
| 4324 | PySequenceMethods *sq = tp->tp_as_sequence; |
| 4325 | |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 4326 | if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) { |
Martin v. Löwis | dde99d2 | 2006-02-17 15:57:41 +0000 | [diff] [blame] | 4327 | Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX; |
Guido van Rossum | 50d756e | 2001-08-18 17:43:36 +0000 | [diff] [blame] | 4328 | if (!_PyEval_SliceIndex(v, &ilow)) |
| 4329 | return NULL; |
| 4330 | if (!_PyEval_SliceIndex(w, &ihigh)) |
| 4331 | return NULL; |
| 4332 | return PySequence_GetSlice(u, ilow, ihigh); |
| 4333 | } |
| 4334 | else { |
| 4335 | PyObject *slice = PySlice_New(v, w, NULL); |
Guido van Rossum | 354797c | 2001-12-03 19:45:06 +0000 | [diff] [blame] | 4336 | if (slice != NULL) { |
| 4337 | PyObject *res = PyObject_GetItem(u, slice); |
| 4338 | Py_DECREF(slice); |
| 4339 | return res; |
| 4340 | } |
Guido van Rossum | 50d756e | 2001-08-18 17:43:36 +0000 | [diff] [blame] | 4341 | else |
| 4342 | return NULL; |
| 4343 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4344 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4345 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4346 | static int |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4347 | assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x) |
| 4348 | /* u[v:w] = x */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4349 | { |
Guido van Rossum | 50d756e | 2001-08-18 17:43:36 +0000 | [diff] [blame] | 4350 | PyTypeObject *tp = u->ob_type; |
| 4351 | PySequenceMethods *sq = tp->tp_as_sequence; |
| 4352 | |
Georg Brandl | 0fca97a | 2007-03-05 22:28:08 +0000 | [diff] [blame] | 4353 | if (sq && sq->sq_ass_slice && ISINDEX(v) && ISINDEX(w)) { |
Martin v. Löwis | dde99d2 | 2006-02-17 15:57:41 +0000 | [diff] [blame] | 4354 | Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX; |
Guido van Rossum | 50d756e | 2001-08-18 17:43:36 +0000 | [diff] [blame] | 4355 | if (!_PyEval_SliceIndex(v, &ilow)) |
| 4356 | return -1; |
| 4357 | if (!_PyEval_SliceIndex(w, &ihigh)) |
| 4358 | return -1; |
| 4359 | if (x == NULL) |
| 4360 | return PySequence_DelSlice(u, ilow, ihigh); |
| 4361 | else |
| 4362 | return PySequence_SetSlice(u, ilow, ihigh, x); |
| 4363 | } |
| 4364 | else { |
| 4365 | PyObject *slice = PySlice_New(v, w, NULL); |
| 4366 | if (slice != NULL) { |
Guido van Rossum | 354797c | 2001-12-03 19:45:06 +0000 | [diff] [blame] | 4367 | int res; |
Guido van Rossum | 50d756e | 2001-08-18 17:43:36 +0000 | [diff] [blame] | 4368 | if (x != NULL) |
Guido van Rossum | 354797c | 2001-12-03 19:45:06 +0000 | [diff] [blame] | 4369 | res = PyObject_SetItem(u, slice, x); |
Guido van Rossum | 50d756e | 2001-08-18 17:43:36 +0000 | [diff] [blame] | 4370 | else |
Guido van Rossum | 354797c | 2001-12-03 19:45:06 +0000 | [diff] [blame] | 4371 | res = PyObject_DelItem(u, slice); |
| 4372 | Py_DECREF(slice); |
| 4373 | return res; |
Guido van Rossum | 50d756e | 2001-08-18 17:43:36 +0000 | [diff] [blame] | 4374 | } |
| 4375 | else |
| 4376 | return -1; |
| 4377 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4378 | } |
| 4379 | |
Guido van Rossum | 04edb52 | 2008-03-18 02:49:46 +0000 | [diff] [blame] | 4380 | #define Py3kExceptionClass_Check(x) \ |
| 4381 | (PyType_Check((x)) && \ |
| 4382 | PyType_FastSubclass((PyTypeObject*)(x), Py_TPFLAGS_BASE_EXC_SUBCLASS)) |
| 4383 | |
| 4384 | #define CANNOT_CATCH_MSG "catching classes that don't inherit from " \ |
Georg Brandl | d5b635f | 2008-03-25 08:29:14 +0000 | [diff] [blame] | 4385 | "BaseException is not allowed in 3.x" |
Guido van Rossum | 04edb52 | 2008-03-18 02:49:46 +0000 | [diff] [blame] | 4386 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4387 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4388 | cmp_outcome(int op, register PyObject *v, register PyObject *w) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4389 | { |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4390 | int res = 0; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4391 | switch (op) { |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 4392 | case PyCmp_IS: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4393 | res = (v == w); |
Raymond Hettinger | 4bad9ba | 2003-01-19 05:08:13 +0000 | [diff] [blame] | 4394 | break; |
| 4395 | case PyCmp_IS_NOT: |
| 4396 | res = (v != w); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4397 | break; |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 4398 | case PyCmp_IN: |
Raymond Hettinger | 4bad9ba | 2003-01-19 05:08:13 +0000 | [diff] [blame] | 4399 | res = PySequence_Contains(w, v); |
| 4400 | if (res < 0) |
| 4401 | return NULL; |
| 4402 | break; |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 4403 | case PyCmp_NOT_IN: |
Guido van Rossum | 7e33c6e | 1998-05-22 00:52:29 +0000 | [diff] [blame] | 4404 | res = PySequence_Contains(w, v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4405 | if (res < 0) |
| 4406 | return NULL; |
Raymond Hettinger | 4bad9ba | 2003-01-19 05:08:13 +0000 | [diff] [blame] | 4407 | res = !res; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4408 | break; |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 4409 | case PyCmp_EXC_MATCH: |
Brett Cannon | 129bd52 | 2007-01-30 21:34:36 +0000 | [diff] [blame] | 4410 | if (PyTuple_Check(w)) { |
| 4411 | Py_ssize_t i, length; |
| 4412 | length = PyTuple_Size(w); |
| 4413 | for (i = 0; i < length; i += 1) { |
| 4414 | PyObject *exc = PyTuple_GET_ITEM(w, i); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4415 | if (PyString_Check(exc)) { |
Brett Cannon | 129bd52 | 2007-01-30 21:34:36 +0000 | [diff] [blame] | 4416 | int ret_val; |
| 4417 | ret_val = PyErr_WarnEx( |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 4418 | PyExc_DeprecationWarning, |
| 4419 | "catching of string " |
| 4420 | "exceptions is deprecated", 1); |
Benjamin Peterson | f19a7b9 | 2008-04-27 18:40:21 +0000 | [diff] [blame] | 4421 | if (ret_val < 0) |
Brett Cannon | 129bd52 | 2007-01-30 21:34:36 +0000 | [diff] [blame] | 4422 | return NULL; |
| 4423 | } |
Guido van Rossum | 20bda58 | 2008-03-18 03:15:05 +0000 | [diff] [blame] | 4424 | else if (Py_Py3kWarningFlag && |
| 4425 | !PyTuple_Check(exc) && |
| 4426 | !Py3kExceptionClass_Check(exc)) |
Guido van Rossum | 04edb52 | 2008-03-18 02:49:46 +0000 | [diff] [blame] | 4427 | { |
| 4428 | int ret_val; |
| 4429 | ret_val = PyErr_WarnEx( |
| 4430 | PyExc_DeprecationWarning, |
| 4431 | CANNOT_CATCH_MSG, 1); |
Benjamin Peterson | f19a7b9 | 2008-04-27 18:40:21 +0000 | [diff] [blame] | 4432 | if (ret_val < 0) |
Guido van Rossum | 04edb52 | 2008-03-18 02:49:46 +0000 | [diff] [blame] | 4433 | return NULL; |
| 4434 | } |
Brett Cannon | 129bd52 | 2007-01-30 21:34:36 +0000 | [diff] [blame] | 4435 | } |
| 4436 | } |
| 4437 | else { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4438 | if (PyString_Check(w)) { |
Brett Cannon | 129bd52 | 2007-01-30 21:34:36 +0000 | [diff] [blame] | 4439 | int ret_val; |
| 4440 | ret_val = PyErr_WarnEx( |
| 4441 | PyExc_DeprecationWarning, |
| 4442 | "catching of string " |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 4443 | "exceptions is deprecated", 1); |
Benjamin Peterson | f19a7b9 | 2008-04-27 18:40:21 +0000 | [diff] [blame] | 4444 | if (ret_val < 0) |
Brett Cannon | 129bd52 | 2007-01-30 21:34:36 +0000 | [diff] [blame] | 4445 | return NULL; |
| 4446 | } |
Guido van Rossum | 20bda58 | 2008-03-18 03:15:05 +0000 | [diff] [blame] | 4447 | else if (Py_Py3kWarningFlag && |
| 4448 | !PyTuple_Check(w) && |
| 4449 | !Py3kExceptionClass_Check(w)) |
Guido van Rossum | 04edb52 | 2008-03-18 02:49:46 +0000 | [diff] [blame] | 4450 | { |
| 4451 | int ret_val; |
| 4452 | ret_val = PyErr_WarnEx( |
| 4453 | PyExc_DeprecationWarning, |
| 4454 | CANNOT_CATCH_MSG, 1); |
Benjamin Peterson | f19a7b9 | 2008-04-27 18:40:21 +0000 | [diff] [blame] | 4455 | if (ret_val < 0) |
Guido van Rossum | 04edb52 | 2008-03-18 02:49:46 +0000 | [diff] [blame] | 4456 | return NULL; |
| 4457 | } |
Brett Cannon | 129bd52 | 2007-01-30 21:34:36 +0000 | [diff] [blame] | 4458 | } |
Barry Warsaw | 4249f54 | 1997-08-22 21:26:19 +0000 | [diff] [blame] | 4459 | res = PyErr_GivenExceptionMatches(v, w); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4460 | break; |
| 4461 | default: |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4462 | return PyObject_RichCompare(v, w, op); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4463 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4464 | v = res ? Py_True : Py_False; |
| 4465 | Py_INCREF(v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4466 | return v; |
| 4467 | } |
| 4468 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4469 | static PyObject * |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 4470 | import_from(PyObject *v, PyObject *name) |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 4471 | { |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 4472 | PyObject *x; |
| 4473 | |
| 4474 | x = PyObject_GetAttr(v, name); |
| 4475 | if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) { |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 4476 | PyErr_Format(PyExc_ImportError, |
| 4477 | "cannot import name %.230s", |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4478 | PyString_AsString(name)); |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 4479 | } |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 4480 | return x; |
| 4481 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4482 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4483 | static int |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 4484 | import_all_from(PyObject *locals, PyObject *v) |
| 4485 | { |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 4486 | PyObject *all = PyObject_GetAttrString(v, "__all__"); |
| 4487 | PyObject *dict, *name, *value; |
| 4488 | int skip_leading_underscores = 0; |
| 4489 | int pos, err; |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 4490 | |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 4491 | if (all == NULL) { |
| 4492 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 4493 | return -1; /* Unexpected error */ |
| 4494 | PyErr_Clear(); |
| 4495 | dict = PyObject_GetAttrString(v, "__dict__"); |
| 4496 | if (dict == NULL) { |
| 4497 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 4498 | return -1; |
| 4499 | PyErr_SetString(PyExc_ImportError, |
| 4500 | "from-import-* object has no __dict__ and no __all__"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4501 | return -1; |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 4502 | } |
| 4503 | all = PyMapping_Keys(dict); |
| 4504 | Py_DECREF(dict); |
| 4505 | if (all == NULL) |
| 4506 | return -1; |
| 4507 | skip_leading_underscores = 1; |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 4508 | } |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 4509 | |
| 4510 | for (pos = 0, err = 0; ; pos++) { |
| 4511 | name = PySequence_GetItem(all, pos); |
| 4512 | if (name == NULL) { |
| 4513 | if (!PyErr_ExceptionMatches(PyExc_IndexError)) |
| 4514 | err = -1; |
| 4515 | else |
| 4516 | PyErr_Clear(); |
| 4517 | break; |
| 4518 | } |
| 4519 | if (skip_leading_underscores && |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4520 | PyString_Check(name) && |
| 4521 | PyString_AS_STRING(name)[0] == '_') |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 4522 | { |
| 4523 | Py_DECREF(name); |
| 4524 | continue; |
| 4525 | } |
| 4526 | value = PyObject_GetAttr(v, name); |
| 4527 | if (value == NULL) |
| 4528 | err = -1; |
Armin Rigo | 7037085 | 2006-11-29 21:59:22 +0000 | [diff] [blame] | 4529 | else if (PyDict_CheckExact(locals)) |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 4530 | err = PyDict_SetItem(locals, name, value); |
Armin Rigo | 7037085 | 2006-11-29 21:59:22 +0000 | [diff] [blame] | 4531 | else |
| 4532 | err = PyObject_SetItem(locals, name, value); |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 4533 | Py_DECREF(name); |
| 4534 | Py_XDECREF(value); |
| 4535 | if (err != 0) |
| 4536 | break; |
| 4537 | } |
| 4538 | Py_DECREF(all); |
| 4539 | return err; |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 4540 | } |
| 4541 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4542 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4543 | build_class(PyObject *methods, PyObject *bases, PyObject *name) |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 4544 | { |
Guido van Rossum | 7851eea | 2001-09-12 19:19:18 +0000 | [diff] [blame] | 4545 | PyObject *metaclass = NULL, *result, *base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4546 | |
| 4547 | if (PyDict_Check(methods)) |
| 4548 | metaclass = PyDict_GetItemString(methods, "__metaclass__"); |
Guido van Rossum | 7851eea | 2001-09-12 19:19:18 +0000 | [diff] [blame] | 4549 | if (metaclass != NULL) |
Guido van Rossum | 2556f2e | 2001-12-06 14:09:56 +0000 | [diff] [blame] | 4550 | Py_INCREF(metaclass); |
Guido van Rossum | 7851eea | 2001-09-12 19:19:18 +0000 | [diff] [blame] | 4551 | else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) { |
| 4552 | base = PyTuple_GET_ITEM(bases, 0); |
| 4553 | metaclass = PyObject_GetAttrString(base, "__class__"); |
| 4554 | if (metaclass == NULL) { |
| 4555 | PyErr_Clear(); |
| 4556 | metaclass = (PyObject *)base->ob_type; |
| 4557 | Py_INCREF(metaclass); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 4558 | } |
| 4559 | } |
Guido van Rossum | 7851eea | 2001-09-12 19:19:18 +0000 | [diff] [blame] | 4560 | else { |
| 4561 | PyObject *g = PyEval_GetGlobals(); |
| 4562 | if (g != NULL && PyDict_Check(g)) |
| 4563 | metaclass = PyDict_GetItemString(g, "__metaclass__"); |
| 4564 | if (metaclass == NULL) |
| 4565 | metaclass = (PyObject *) &PyClass_Type; |
| 4566 | Py_INCREF(metaclass); |
| 4567 | } |
Jeremy Hylton | 7c1e347 | 2007-02-26 16:14:51 +0000 | [diff] [blame] | 4568 | result = PyObject_CallFunctionObjArgs(metaclass, name, bases, methods, |
Thomas Wouters | ae406c6 | 2007-09-19 17:27:43 +0000 | [diff] [blame] | 4569 | NULL); |
Guido van Rossum | 7851eea | 2001-09-12 19:19:18 +0000 | [diff] [blame] | 4570 | Py_DECREF(metaclass); |
Raymond Hettinger | f2c0830 | 2004-06-05 06:16:22 +0000 | [diff] [blame] | 4571 | if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) { |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 4572 | /* A type error here likely means that the user passed |
Raymond Hettinger | f2c0830 | 2004-06-05 06:16:22 +0000 | [diff] [blame] | 4573 | in a base that was not a class (such the random module |
| 4574 | instead of the random.random type). Help them out with |
Raymond Hettinger | cfc3192 | 2004-09-16 16:41:57 +0000 | [diff] [blame] | 4575 | by augmenting the error message with more information.*/ |
| 4576 | |
| 4577 | PyObject *ptype, *pvalue, *ptraceback; |
| 4578 | |
| 4579 | PyErr_Fetch(&ptype, &pvalue, &ptraceback); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4580 | if (PyString_Check(pvalue)) { |
Raymond Hettinger | cfc3192 | 2004-09-16 16:41:57 +0000 | [diff] [blame] | 4581 | PyObject *newmsg; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4582 | newmsg = PyString_FromFormat( |
Jeremy Hylton | 7c1e347 | 2007-02-26 16:14:51 +0000 | [diff] [blame] | 4583 | "Error when calling the metaclass bases\n" |
Thomas Wouters | ae406c6 | 2007-09-19 17:27:43 +0000 | [diff] [blame] | 4584 | " %s", |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4585 | PyString_AS_STRING(pvalue)); |
Raymond Hettinger | cfc3192 | 2004-09-16 16:41:57 +0000 | [diff] [blame] | 4586 | if (newmsg != NULL) { |
| 4587 | Py_DECREF(pvalue); |
| 4588 | pvalue = newmsg; |
| 4589 | } |
| 4590 | } |
| 4591 | PyErr_Restore(ptype, pvalue, ptraceback); |
Raymond Hettinger | f2c0830 | 2004-06-05 06:16:22 +0000 | [diff] [blame] | 4592 | } |
Guido van Rossum | 7851eea | 2001-09-12 19:19:18 +0000 | [diff] [blame] | 4593 | return result; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 4594 | } |
| 4595 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4596 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4597 | exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals, |
| 4598 | PyObject *locals) |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 4599 | { |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 4600 | int n; |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4601 | PyObject *v; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4602 | int plain = 0; |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 4603 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4604 | if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None && |
| 4605 | ((n = PyTuple_Size(prog)) == 2 || n == 3)) { |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 4606 | /* Backward compatibility hack */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4607 | globals = PyTuple_GetItem(prog, 1); |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 4608 | if (n == 3) |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4609 | locals = PyTuple_GetItem(prog, 2); |
| 4610 | prog = PyTuple_GetItem(prog, 0); |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 4611 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4612 | if (globals == Py_None) { |
| 4613 | globals = PyEval_GetGlobals(); |
| 4614 | if (locals == Py_None) { |
| 4615 | locals = PyEval_GetLocals(); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4616 | plain = 1; |
| 4617 | } |
Neal Norwitz | df6a649 | 2006-08-13 18:10:10 +0000 | [diff] [blame] | 4618 | if (!globals || !locals) { |
| 4619 | PyErr_SetString(PyExc_SystemError, |
| 4620 | "globals and locals cannot be NULL"); |
| 4621 | return -1; |
| 4622 | } |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 4623 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4624 | else if (locals == Py_None) |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 4625 | locals = globals; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4626 | if (!PyString_Check(prog) && |
Benjamin Peterson | 78821dd | 2009-01-25 17:15:10 +0000 | [diff] [blame] | 4627 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 4628 | !PyUnicode_Check(prog) && |
Benjamin Peterson | 78821dd | 2009-01-25 17:15:10 +0000 | [diff] [blame] | 4629 | #endif |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4630 | !PyCode_Check(prog) && |
| 4631 | !PyFile_Check(prog)) { |
| 4632 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4633 | "exec: arg 1 must be a string, file, or code object"); |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 4634 | return -1; |
| 4635 | } |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4636 | if (!PyDict_Check(globals)) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4637 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4638 | "exec: arg 2 must be a dictionary or None"); |
| 4639 | return -1; |
| 4640 | } |
Raymond Hettinger | 66bd233 | 2004-08-02 08:30:07 +0000 | [diff] [blame] | 4641 | if (!PyMapping_Check(locals)) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4642 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 66bd233 | 2004-08-02 08:30:07 +0000 | [diff] [blame] | 4643 | "exec: arg 3 must be a mapping or None"); |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 4644 | return -1; |
| 4645 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4646 | if (PyDict_GetItemString(globals, "__builtins__") == NULL) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 4647 | PyDict_SetItemString(globals, "__builtins__", f->f_builtins); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4648 | if (PyCode_Check(prog)) { |
Jeremy Hylton | 733c893 | 2001-12-13 19:51:56 +0000 | [diff] [blame] | 4649 | if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) { |
| 4650 | PyErr_SetString(PyExc_TypeError, |
| 4651 | "code object passed to exec may not contain free variables"); |
| 4652 | return -1; |
| 4653 | } |
Guido van Rossum | a400d8a | 2000-01-12 22:45:54 +0000 | [diff] [blame] | 4654 | v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals); |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 4655 | } |
Guido van Rossum | a400d8a | 2000-01-12 22:45:54 +0000 | [diff] [blame] | 4656 | else if (PyFile_Check(prog)) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4657 | FILE *fp = PyFile_AsFile(prog); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4658 | char *name = PyString_AsString(PyFile_Name(prog)); |
Jeremy Hylton | 714b112 | 2007-02-25 16:01:58 +0000 | [diff] [blame] | 4659 | PyCompilerFlags cf; |
Thomas Wouters | ae406c6 | 2007-09-19 17:27:43 +0000 | [diff] [blame] | 4660 | if (name == NULL) |
| 4661 | return -1; |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 4662 | cf.cf_flags = 0; |
| 4663 | if (PyEval_MergeCompilerFlags(&cf)) |
Jeremy Hylton | bc32024 | 2001-03-22 02:47:58 +0000 | [diff] [blame] | 4664 | v = PyRun_FileFlags(fp, name, Py_file_input, globals, |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 4665 | locals, &cf); |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 4666 | else |
Jeremy Hylton | bc32024 | 2001-03-22 02:47:58 +0000 | [diff] [blame] | 4667 | v = PyRun_File(fp, name, Py_file_input, globals, |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 4668 | locals); |
Guido van Rossum | a400d8a | 2000-01-12 22:45:54 +0000 | [diff] [blame] | 4669 | } |
| 4670 | else { |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 4671 | PyObject *tmp = NULL; |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 4672 | char *str; |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 4673 | PyCompilerFlags cf; |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 4674 | cf.cf_flags = 0; |
| 4675 | #ifdef Py_USING_UNICODE |
| 4676 | if (PyUnicode_Check(prog)) { |
| 4677 | tmp = PyUnicode_AsUTF8String(prog); |
| 4678 | if (tmp == NULL) |
| 4679 | return -1; |
| 4680 | prog = tmp; |
| 4681 | cf.cf_flags |= PyCF_SOURCE_IS_UTF8; |
| 4682 | } |
| 4683 | #endif |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4684 | if (PyString_AsStringAndSize(prog, &str, NULL)) |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 4685 | return -1; |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 4686 | if (PyEval_MergeCompilerFlags(&cf)) |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 4687 | v = PyRun_StringFlags(str, Py_file_input, globals, |
Jeremy Hylton | bc32024 | 2001-03-22 02:47:58 +0000 | [diff] [blame] | 4688 | locals, &cf); |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 4689 | else |
Jeremy Hylton | bc32024 | 2001-03-22 02:47:58 +0000 | [diff] [blame] | 4690 | v = PyRun_String(str, Py_file_input, globals, locals); |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 4691 | Py_XDECREF(tmp); |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 4692 | } |
Guido van Rossum | a400d8a | 2000-01-12 22:45:54 +0000 | [diff] [blame] | 4693 | if (plain) |
| 4694 | PyFrame_LocalsToFast(f, 0); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4695 | if (v == NULL) |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 4696 | return -1; |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4697 | Py_DECREF(v); |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 4698 | return 0; |
| 4699 | } |
Guido van Rossum | 24c1374 | 1995-02-14 09:42:43 +0000 | [diff] [blame] | 4700 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4701 | static void |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 4702 | format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj) |
| 4703 | { |
| 4704 | char *obj_str; |
| 4705 | |
| 4706 | if (!obj) |
| 4707 | return; |
| 4708 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4709 | obj_str = PyString_AsString(obj); |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 4710 | if (!obj_str) |
| 4711 | return; |
| 4712 | |
| 4713 | PyErr_Format(exc, format_str, obj_str); |
| 4714 | } |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 4715 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4716 | static PyObject * |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 4717 | string_concatenate(PyObject *v, PyObject *w, |
| 4718 | PyFrameObject *f, unsigned char *next_instr) |
| 4719 | { |
| 4720 | /* This function implements 'variable += expr' when both arguments |
| 4721 | are strings. */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4722 | Py_ssize_t v_len = PyString_GET_SIZE(v); |
| 4723 | Py_ssize_t w_len = PyString_GET_SIZE(w); |
Armin Rigo | 97ff047 | 2006-08-09 15:37:26 +0000 | [diff] [blame] | 4724 | Py_ssize_t new_len = v_len + w_len; |
| 4725 | if (new_len < 0) { |
| 4726 | PyErr_SetString(PyExc_OverflowError, |
| 4727 | "strings are too large to concat"); |
| 4728 | return NULL; |
| 4729 | } |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 4730 | |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 4731 | if (v->ob_refcnt == 2) { |
| 4732 | /* In the common case, there are 2 references to the value |
| 4733 | * stored in 'variable' when the += is performed: one on the |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 4734 | * value stack (in 'v') and one still stored in the |
| 4735 | * 'variable'. We try to delete the variable now to reduce |
| 4736 | * the refcnt to 1. |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 4737 | */ |
| 4738 | switch (*next_instr) { |
| 4739 | case STORE_FAST: |
| 4740 | { |
| 4741 | int oparg = PEEKARG(); |
| 4742 | PyObject **fastlocals = f->f_localsplus; |
| 4743 | if (GETLOCAL(oparg) == v) |
| 4744 | SETLOCAL(oparg, NULL); |
| 4745 | break; |
| 4746 | } |
| 4747 | case STORE_DEREF: |
| 4748 | { |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 4749 | PyObject **freevars = (f->f_localsplus + |
| 4750 | f->f_code->co_nlocals); |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 4751 | PyObject *c = freevars[PEEKARG()]; |
| 4752 | if (PyCell_GET(c) == v) |
| 4753 | PyCell_Set(c, NULL); |
| 4754 | break; |
| 4755 | } |
| 4756 | case STORE_NAME: |
| 4757 | { |
| 4758 | PyObject *names = f->f_code->co_names; |
| 4759 | PyObject *name = GETITEM(names, PEEKARG()); |
| 4760 | PyObject *locals = f->f_locals; |
| 4761 | if (PyDict_CheckExact(locals) && |
| 4762 | PyDict_GetItem(locals, name) == v) { |
| 4763 | if (PyDict_DelItem(locals, name) != 0) { |
| 4764 | PyErr_Clear(); |
| 4765 | } |
| 4766 | } |
| 4767 | break; |
| 4768 | } |
| 4769 | } |
| 4770 | } |
| 4771 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4772 | if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) { |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 4773 | /* Now we own the last reference to 'v', so we can resize it |
| 4774 | * in-place. |
| 4775 | */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4776 | if (_PyString_Resize(&v, new_len) != 0) { |
| 4777 | /* XXX if _PyString_Resize() fails, 'v' has been |
Thomas Wouters | e217602 | 2007-09-20 17:35:10 +0000 | [diff] [blame] | 4778 | * deallocated so it cannot be put back into |
| 4779 | * 'variable'. The MemoryError is raised when there |
| 4780 | * is no value in 'variable', which might (very |
| 4781 | * remotely) be a cause of incompatibilities. |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 4782 | */ |
| 4783 | return NULL; |
| 4784 | } |
| 4785 | /* copy 'w' into the newly allocated area of 'v' */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4786 | memcpy(PyString_AS_STRING(v) + v_len, |
| 4787 | PyString_AS_STRING(w), w_len); |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 4788 | return v; |
| 4789 | } |
| 4790 | else { |
| 4791 | /* When in-place resizing is not an option. */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4792 | PyString_Concat(&v, w); |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 4793 | return v; |
| 4794 | } |
| 4795 | } |
| 4796 | |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 4797 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 4798 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4799 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4800 | getarray(long a[256]) |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 4801 | { |
| 4802 | int i; |
| 4803 | PyObject *l = PyList_New(256); |
| 4804 | if (l == NULL) return NULL; |
| 4805 | for (i = 0; i < 256; i++) { |
| 4806 | PyObject *x = PyInt_FromLong(a[i]); |
| 4807 | if (x == NULL) { |
| 4808 | Py_DECREF(l); |
| 4809 | return NULL; |
| 4810 | } |
| 4811 | PyList_SetItem(l, i, x); |
| 4812 | } |
| 4813 | for (i = 0; i < 256; i++) |
| 4814 | a[i] = 0; |
| 4815 | return l; |
| 4816 | } |
| 4817 | |
| 4818 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4819 | _Py_GetDXProfile(PyObject *self, PyObject *args) |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 4820 | { |
| 4821 | #ifndef DXPAIRS |
| 4822 | return getarray(dxp); |
| 4823 | #else |
| 4824 | int i; |
| 4825 | PyObject *l = PyList_New(257); |
| 4826 | if (l == NULL) return NULL; |
| 4827 | for (i = 0; i < 257; i++) { |
| 4828 | PyObject *x = getarray(dxpairs[i]); |
| 4829 | if (x == NULL) { |
| 4830 | Py_DECREF(l); |
| 4831 | return NULL; |
| 4832 | } |
| 4833 | PyList_SetItem(l, i, x); |
| 4834 | } |
| 4835 | return l; |
| 4836 | #endif |
| 4837 | } |
| 4838 | |
| 4839 | #endif |