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