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