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