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