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