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