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