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 |
Victor Stinner | e90bdb1 | 2016-08-25 23:26:50 +0200 | [diff] [blame] | 116 | static PyObject * fast_function(PyObject *, PyObject **, Py_ssize_t, Py_ssize_t); |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 117 | static PyObject * do_call(PyObject *, PyObject ***, Py_ssize_t, Py_ssize_t); |
| 118 | static PyObject * ext_do_call(PyObject *, PyObject ***, int, Py_ssize_t, Py_ssize_t); |
| 119 | static PyObject * update_keyword_args(PyObject *, Py_ssize_t, PyObject ***, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 120 | PyObject *); |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 121 | static PyObject * update_star_args(Py_ssize_t, Py_ssize_t, PyObject *, PyObject ***); |
| 122 | static PyObject * load_args(PyObject ***, Py_ssize_t); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 123 | #define CALL_FLAG_VAR 1 |
| 124 | #define CALL_FLAG_KW 2 |
| 125 | |
Guido van Rossum | 0a066c0 | 1992-03-27 17:29:15 +0000 | [diff] [blame] | 126 | #ifdef LLTRACE |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 127 | static int lltrace; |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 128 | static int prtrace(PyObject *, const char *); |
Guido van Rossum | 0a066c0 | 1992-03-27 17:29:15 +0000 | [diff] [blame] | 129 | #endif |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 130 | static int call_trace(Py_tracefunc, PyObject *, |
| 131 | PyThreadState *, PyFrameObject *, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 132 | int, PyObject *); |
Amaury Forgeot d'Arc | f05149a | 2007-11-13 01:05:30 +0000 | [diff] [blame] | 133 | static int call_trace_protected(Py_tracefunc, PyObject *, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 134 | PyThreadState *, PyFrameObject *, |
| 135 | int, PyObject *); |
| 136 | static void call_exc_trace(Py_tracefunc, PyObject *, |
| 137 | PyThreadState *, PyFrameObject *); |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 138 | static int maybe_call_line_trace(Py_tracefunc, PyObject *, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 139 | PyThreadState *, PyFrameObject *, int *, int *, int *); |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 140 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 141 | static PyObject * cmp_outcome(int, PyObject *, PyObject *); |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 142 | static PyObject * import_name(PyFrameObject *, PyObject *, PyObject *, PyObject *); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 143 | static PyObject * import_from(PyObject *, PyObject *); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 144 | static int import_all_from(PyObject *, PyObject *); |
Neal Norwitz | da059e3 | 2007-08-26 05:33:45 +0000 | [diff] [blame] | 145 | static void format_exc_check_arg(PyObject *, const char *, PyObject *); |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 146 | static void format_exc_unbound(PyCodeObject *co, int oparg); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 147 | static PyObject * unicode_concatenate(PyObject *, PyObject *, |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 148 | PyFrameObject *, const unsigned short *); |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 149 | static PyObject * special_lookup(PyObject *, _Py_Identifier *); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 150 | |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 151 | #define NAME_ERROR_MSG \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 152 | "name '%.200s' is not defined" |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 153 | #define UNBOUNDLOCAL_ERROR_MSG \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 154 | "local variable '%.200s' referenced before assignment" |
Jeremy Hylton | c76770c | 2001-04-13 16:51:46 +0000 | [diff] [blame] | 155 | #define UNBOUNDFREE_ERROR_MSG \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 156 | "free variable '%.200s' referenced before assignment" \ |
| 157 | " in enclosing scope" |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 158 | |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 159 | /* Dynamic execution profile */ |
| 160 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 161 | #ifdef DXPAIRS |
| 162 | static long dxpairs[257][256]; |
| 163 | #define dxp dxpairs[256] |
| 164 | #else |
| 165 | static long dxp[256]; |
| 166 | #endif |
| 167 | #endif |
| 168 | |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 169 | /* Function call profile */ |
| 170 | #ifdef CALL_PROFILE |
| 171 | #define PCALL_NUM 11 |
| 172 | static int pcall[PCALL_NUM]; |
| 173 | |
| 174 | #define PCALL_ALL 0 |
| 175 | #define PCALL_FUNCTION 1 |
| 176 | #define PCALL_FAST_FUNCTION 2 |
| 177 | #define PCALL_FASTER_FUNCTION 3 |
| 178 | #define PCALL_METHOD 4 |
| 179 | #define PCALL_BOUND_METHOD 5 |
| 180 | #define PCALL_CFUNCTION 6 |
| 181 | #define PCALL_TYPE 7 |
| 182 | #define PCALL_GENERATOR 8 |
| 183 | #define PCALL_OTHER 9 |
| 184 | #define PCALL_POP 10 |
| 185 | |
| 186 | /* Notes about the statistics |
| 187 | |
| 188 | PCALL_FAST stats |
| 189 | |
| 190 | FAST_FUNCTION means no argument tuple needs to be created. |
| 191 | FASTER_FUNCTION means that the fast-path frame setup code is used. |
| 192 | |
| 193 | If there is a method call where the call can be optimized by changing |
| 194 | the argument tuple and calling the function directly, it gets recorded |
| 195 | twice. |
| 196 | |
| 197 | As a result, the relationship among the statistics appears to be |
| 198 | PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD + |
| 199 | PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER |
| 200 | PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION |
| 201 | PCALL_METHOD > PCALL_BOUND_METHOD |
| 202 | */ |
| 203 | |
| 204 | #define PCALL(POS) pcall[POS]++ |
| 205 | |
| 206 | PyObject * |
| 207 | PyEval_GetCallStats(PyObject *self) |
| 208 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 209 | return Py_BuildValue("iiiiiiiiiii", |
| 210 | pcall[0], pcall[1], pcall[2], pcall[3], |
| 211 | pcall[4], pcall[5], pcall[6], pcall[7], |
| 212 | pcall[8], pcall[9], pcall[10]); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 213 | } |
| 214 | #else |
| 215 | #define PCALL(O) |
| 216 | |
| 217 | PyObject * |
| 218 | PyEval_GetCallStats(PyObject *self) |
| 219 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 220 | Py_INCREF(Py_None); |
| 221 | return Py_None; |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 222 | } |
| 223 | #endif |
| 224 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 225 | |
Benjamin Peterson | d2be5b4 | 2010-09-10 22:47:02 +0000 | [diff] [blame] | 226 | #ifdef WITH_THREAD |
| 227 | #define GIL_REQUEST _Py_atomic_load_relaxed(&gil_drop_request) |
| 228 | #else |
| 229 | #define GIL_REQUEST 0 |
| 230 | #endif |
| 231 | |
Jeffrey Yasskin | 3937083 | 2010-05-03 19:29:34 +0000 | [diff] [blame] | 232 | /* This can set eval_breaker to 0 even though gil_drop_request became |
| 233 | 1. We believe this is all right because the eval loop will release |
| 234 | the GIL eventually anyway. */ |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 235 | #define COMPUTE_EVAL_BREAKER() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 236 | _Py_atomic_store_relaxed( \ |
| 237 | &eval_breaker, \ |
Benjamin Peterson | d2be5b4 | 2010-09-10 22:47:02 +0000 | [diff] [blame] | 238 | GIL_REQUEST | \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 239 | _Py_atomic_load_relaxed(&pendingcalls_to_do) | \ |
| 240 | pending_async_exc) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 241 | |
Benjamin Peterson | d2be5b4 | 2010-09-10 22:47:02 +0000 | [diff] [blame] | 242 | #ifdef WITH_THREAD |
| 243 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 244 | #define SET_GIL_DROP_REQUEST() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 245 | do { \ |
| 246 | _Py_atomic_store_relaxed(&gil_drop_request, 1); \ |
| 247 | _Py_atomic_store_relaxed(&eval_breaker, 1); \ |
| 248 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 249 | |
| 250 | #define RESET_GIL_DROP_REQUEST() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 251 | do { \ |
| 252 | _Py_atomic_store_relaxed(&gil_drop_request, 0); \ |
| 253 | COMPUTE_EVAL_BREAKER(); \ |
| 254 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 255 | |
Benjamin Peterson | d2be5b4 | 2010-09-10 22:47:02 +0000 | [diff] [blame] | 256 | #endif |
| 257 | |
Jeffrey Yasskin | 3937083 | 2010-05-03 19:29:34 +0000 | [diff] [blame] | 258 | /* Pending calls are only modified under pending_lock */ |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 259 | #define SIGNAL_PENDING_CALLS() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 260 | do { \ |
| 261 | _Py_atomic_store_relaxed(&pendingcalls_to_do, 1); \ |
| 262 | _Py_atomic_store_relaxed(&eval_breaker, 1); \ |
| 263 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 264 | |
| 265 | #define UNSIGNAL_PENDING_CALLS() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 266 | do { \ |
| 267 | _Py_atomic_store_relaxed(&pendingcalls_to_do, 0); \ |
| 268 | COMPUTE_EVAL_BREAKER(); \ |
| 269 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 270 | |
| 271 | #define SIGNAL_ASYNC_EXC() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 272 | do { \ |
| 273 | pending_async_exc = 1; \ |
| 274 | _Py_atomic_store_relaxed(&eval_breaker, 1); \ |
| 275 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 276 | |
| 277 | #define UNSIGNAL_ASYNC_EXC() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 278 | do { pending_async_exc = 0; COMPUTE_EVAL_BREAKER(); } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 279 | |
| 280 | |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 281 | #ifdef WITH_THREAD |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 282 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 283 | #ifdef HAVE_ERRNO_H |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 284 | #include <errno.h> |
Guido van Rossum | 2571cc8 | 1999-04-07 16:07:23 +0000 | [diff] [blame] | 285 | #endif |
Guido van Rossum | 49b5606 | 1998-10-01 20:42:43 +0000 | [diff] [blame] | 286 | #include "pythread.h" |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 287 | |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 288 | static PyThread_type_lock pending_lock = 0; /* for pending calls */ |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 289 | static long main_thread = 0; |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 290 | /* This single variable consolidates all requests to break out of the fast path |
| 291 | in the eval loop. */ |
Jeffrey Yasskin | 3937083 | 2010-05-03 19:29:34 +0000 | [diff] [blame] | 292 | static _Py_atomic_int eval_breaker = {0}; |
| 293 | /* Request for dropping the GIL */ |
| 294 | static _Py_atomic_int gil_drop_request = {0}; |
| 295 | /* Request for running pending calls. */ |
| 296 | static _Py_atomic_int pendingcalls_to_do = {0}; |
| 297 | /* Request for looking at the `async_exc` field of the current thread state. |
| 298 | Guarded by the GIL. */ |
| 299 | static int pending_async_exc = 0; |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 300 | |
| 301 | #include "ceval_gil.h" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 302 | |
Tim Peters | 7f468f2 | 2004-10-11 02:40:51 +0000 | [diff] [blame] | 303 | int |
| 304 | PyEval_ThreadsInitialized(void) |
| 305 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 306 | return gil_created(); |
Tim Peters | 7f468f2 | 2004-10-11 02:40:51 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 309 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 310 | PyEval_InitThreads(void) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 311 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 312 | if (gil_created()) |
| 313 | return; |
| 314 | create_gil(); |
| 315 | take_gil(PyThreadState_GET()); |
| 316 | main_thread = PyThread_get_thread_ident(); |
| 317 | if (!pending_lock) |
| 318 | pending_lock = PyThread_allocate_lock(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 319 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 320 | |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 321 | void |
Antoine Pitrou | 1df1536 | 2010-09-13 14:16:46 +0000 | [diff] [blame] | 322 | _PyEval_FiniThreads(void) |
| 323 | { |
| 324 | if (!gil_created()) |
| 325 | return; |
| 326 | destroy_gil(); |
| 327 | assert(!gil_created()); |
| 328 | } |
| 329 | |
| 330 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 331 | PyEval_AcquireLock(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 332 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 333 | PyThreadState *tstate = PyThreadState_GET(); |
| 334 | if (tstate == NULL) |
| 335 | Py_FatalError("PyEval_AcquireLock: current thread state is NULL"); |
| 336 | take_gil(tstate); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 340 | PyEval_ReleaseLock(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 341 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 342 | /* This function must succeed when the current thread state is NULL. |
| 343 | We therefore avoid PyThreadState_GET() which dumps a fatal error |
| 344 | in debug mode. |
| 345 | */ |
| 346 | drop_gil((PyThreadState*)_Py_atomic_load_relaxed( |
| 347 | &_PyThreadState_Current)); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 351 | PyEval_AcquireThread(PyThreadState *tstate) |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 352 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 353 | if (tstate == NULL) |
| 354 | Py_FatalError("PyEval_AcquireThread: NULL new thread state"); |
| 355 | /* Check someone has called PyEval_InitThreads() to create the lock */ |
| 356 | assert(gil_created()); |
| 357 | take_gil(tstate); |
| 358 | if (PyThreadState_Swap(tstate) != NULL) |
| 359 | Py_FatalError( |
| 360 | "PyEval_AcquireThread: non-NULL old thread state"); |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 364 | PyEval_ReleaseThread(PyThreadState *tstate) |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 365 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 366 | if (tstate == NULL) |
| 367 | Py_FatalError("PyEval_ReleaseThread: NULL thread state"); |
| 368 | if (PyThreadState_Swap(NULL) != tstate) |
| 369 | Py_FatalError("PyEval_ReleaseThread: wrong thread state"); |
| 370 | drop_gil(tstate); |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 371 | } |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 372 | |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 373 | /* This function is called from PyOS_AfterFork to destroy all threads which are |
| 374 | * not running in the child process, and clear internal locks which might be |
| 375 | * held by those threads. (This could also be done using pthread_atfork |
| 376 | * mechanism, at least for the pthreads implementation.) */ |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 377 | |
| 378 | void |
| 379 | PyEval_ReInitThreads(void) |
| 380 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 381 | _Py_IDENTIFIER(_after_fork); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 382 | PyObject *threading, *result; |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 383 | PyThreadState *current_tstate = PyThreadState_GET(); |
Jesse Noller | a851397 | 2008-07-17 16:49:17 +0000 | [diff] [blame] | 384 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 385 | if (!gil_created()) |
| 386 | return; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 387 | recreate_gil(); |
| 388 | pending_lock = PyThread_allocate_lock(); |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 389 | take_gil(current_tstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 390 | main_thread = PyThread_get_thread_ident(); |
Jesse Noller | a851397 | 2008-07-17 16:49:17 +0000 | [diff] [blame] | 391 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 392 | /* Update the threading module with the new state. |
| 393 | */ |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 394 | threading = PyMapping_GetItemString(current_tstate->interp->modules, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 395 | "threading"); |
| 396 | if (threading == NULL) { |
| 397 | /* threading not imported */ |
| 398 | PyErr_Clear(); |
| 399 | return; |
| 400 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 401 | result = _PyObject_CallMethodId(threading, &PyId__after_fork, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 402 | if (result == NULL) |
| 403 | PyErr_WriteUnraisable(threading); |
| 404 | else |
| 405 | Py_DECREF(result); |
| 406 | Py_DECREF(threading); |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 407 | |
| 408 | /* Destroy all threads except the current one */ |
| 409 | _PyThreadState_DeleteExcept(current_tstate); |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 410 | } |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 411 | |
| 412 | #else |
Jeffrey Yasskin | 3937083 | 2010-05-03 19:29:34 +0000 | [diff] [blame] | 413 | static _Py_atomic_int eval_breaker = {0}; |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 414 | static int pending_async_exc = 0; |
| 415 | #endif /* WITH_THREAD */ |
| 416 | |
| 417 | /* This function is used to signal that async exceptions are waiting to be |
| 418 | raised, therefore it is also useful in non-threaded builds. */ |
| 419 | |
| 420 | void |
| 421 | _PyEval_SignalAsyncExc(void) |
| 422 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 423 | SIGNAL_ASYNC_EXC(); |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 424 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 425 | |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 426 | /* Functions save_thread and restore_thread are always defined so |
| 427 | dynamically loaded modules needn't be compiled separately for use |
| 428 | with and without threads: */ |
| 429 | |
Guido van Rossum | 2fca21f7 | 1997-07-18 23:56:58 +0000 | [diff] [blame] | 430 | PyThreadState * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 431 | PyEval_SaveThread(void) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 432 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 433 | PyThreadState *tstate = PyThreadState_Swap(NULL); |
| 434 | if (tstate == NULL) |
| 435 | Py_FatalError("PyEval_SaveThread: NULL tstate"); |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 436 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 437 | if (gil_created()) |
| 438 | drop_gil(tstate); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 439 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 440 | return tstate; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 444 | PyEval_RestoreThread(PyThreadState *tstate) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 445 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 446 | if (tstate == NULL) |
| 447 | Py_FatalError("PyEval_RestoreThread: NULL tstate"); |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 448 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 449 | if (gil_created()) { |
| 450 | int err = errno; |
| 451 | take_gil(tstate); |
Antoine Pitrou | 0d5e52d | 2011-05-04 20:02:30 +0200 | [diff] [blame] | 452 | /* _Py_Finalizing is protected by the GIL */ |
| 453 | if (_Py_Finalizing && tstate != _Py_Finalizing) { |
| 454 | drop_gil(tstate); |
| 455 | PyThread_exit_thread(); |
| 456 | assert(0); /* unreachable */ |
| 457 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 458 | errno = err; |
| 459 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 460 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 461 | PyThreadState_Swap(tstate); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 465 | /* Mechanism whereby asynchronously executing callbacks (e.g. UNIX |
| 466 | signal handlers or Mac I/O completion routines) can schedule calls |
| 467 | to a function to be called synchronously. |
| 468 | The synchronous function is called with one void* argument. |
| 469 | It should return 0 for success or -1 for failure -- failure should |
| 470 | be accompanied by an exception. |
| 471 | |
| 472 | If registry succeeds, the registry function returns 0; if it fails |
| 473 | (e.g. due to too many pending calls) it returns -1 (without setting |
| 474 | an exception condition). |
| 475 | |
| 476 | Note that because registry may occur from within signal handlers, |
| 477 | or other asynchronous events, calling malloc() is unsafe! |
| 478 | |
| 479 | #ifdef WITH_THREAD |
| 480 | Any thread can schedule pending calls, but only the main thread |
| 481 | will execute them. |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 482 | There is no facility to schedule calls to a particular thread, but |
| 483 | that should be easy to change, should that ever be required. In |
| 484 | that case, the static variables here should go into the python |
| 485 | threadstate. |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 486 | #endif |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 487 | */ |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 488 | |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 489 | #ifdef WITH_THREAD |
| 490 | |
| 491 | /* The WITH_THREAD implementation is thread-safe. It allows |
| 492 | scheduling to be made from any thread, and even from an executing |
| 493 | callback. |
| 494 | */ |
| 495 | |
| 496 | #define NPENDINGCALLS 32 |
| 497 | static struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 498 | int (*func)(void *); |
| 499 | void *arg; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 500 | } pendingcalls[NPENDINGCALLS]; |
| 501 | static int pendingfirst = 0; |
| 502 | static int pendinglast = 0; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 503 | |
| 504 | int |
| 505 | Py_AddPendingCall(int (*func)(void *), void *arg) |
| 506 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 507 | int i, j, result=0; |
| 508 | PyThread_type_lock lock = pending_lock; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 509 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 510 | /* try a few times for the lock. Since this mechanism is used |
| 511 | * for signal handling (on the main thread), there is a (slim) |
| 512 | * chance that a signal is delivered on the same thread while we |
| 513 | * hold the lock during the Py_MakePendingCalls() function. |
| 514 | * This avoids a deadlock in that case. |
| 515 | * Note that signals can be delivered on any thread. In particular, |
| 516 | * on Windows, a SIGINT is delivered on a system-created worker |
| 517 | * thread. |
| 518 | * We also check for lock being NULL, in the unlikely case that |
| 519 | * this function is called before any bytecode evaluation takes place. |
| 520 | */ |
| 521 | if (lock != NULL) { |
| 522 | for (i = 0; i<100; i++) { |
| 523 | if (PyThread_acquire_lock(lock, NOWAIT_LOCK)) |
| 524 | break; |
| 525 | } |
| 526 | if (i == 100) |
| 527 | return -1; |
| 528 | } |
| 529 | |
| 530 | i = pendinglast; |
| 531 | j = (i + 1) % NPENDINGCALLS; |
| 532 | if (j == pendingfirst) { |
| 533 | result = -1; /* Queue full */ |
| 534 | } else { |
| 535 | pendingcalls[i].func = func; |
| 536 | pendingcalls[i].arg = arg; |
| 537 | pendinglast = j; |
| 538 | } |
| 539 | /* signal main loop */ |
| 540 | SIGNAL_PENDING_CALLS(); |
| 541 | if (lock != NULL) |
| 542 | PyThread_release_lock(lock); |
| 543 | return result; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | int |
| 547 | Py_MakePendingCalls(void) |
| 548 | { |
Charles-François Natali | f23339a | 2011-07-23 18:15:43 +0200 | [diff] [blame] | 549 | static int busy = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 550 | int i; |
| 551 | int r = 0; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 552 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 553 | if (!pending_lock) { |
| 554 | /* initial allocation of the lock */ |
| 555 | pending_lock = PyThread_allocate_lock(); |
| 556 | if (pending_lock == NULL) |
| 557 | return -1; |
| 558 | } |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 559 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 560 | /* only service pending calls on main thread */ |
| 561 | if (main_thread && PyThread_get_thread_ident() != main_thread) |
| 562 | return 0; |
| 563 | /* don't perform recursive pending calls */ |
Charles-François Natali | f23339a | 2011-07-23 18:15:43 +0200 | [diff] [blame] | 564 | if (busy) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 565 | return 0; |
Charles-François Natali | f23339a | 2011-07-23 18:15:43 +0200 | [diff] [blame] | 566 | busy = 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 567 | /* perform a bounded number of calls, in case of recursion */ |
| 568 | for (i=0; i<NPENDINGCALLS; i++) { |
| 569 | int j; |
| 570 | int (*func)(void *); |
| 571 | void *arg = NULL; |
| 572 | |
| 573 | /* pop one item off the queue while holding the lock */ |
| 574 | PyThread_acquire_lock(pending_lock, WAIT_LOCK); |
| 575 | j = pendingfirst; |
| 576 | if (j == pendinglast) { |
| 577 | func = NULL; /* Queue empty */ |
| 578 | } else { |
| 579 | func = pendingcalls[j].func; |
| 580 | arg = pendingcalls[j].arg; |
| 581 | pendingfirst = (j + 1) % NPENDINGCALLS; |
| 582 | } |
| 583 | if (pendingfirst != pendinglast) |
| 584 | SIGNAL_PENDING_CALLS(); |
| 585 | else |
| 586 | UNSIGNAL_PENDING_CALLS(); |
| 587 | PyThread_release_lock(pending_lock); |
| 588 | /* having released the lock, perform the callback */ |
| 589 | if (func == NULL) |
| 590 | break; |
| 591 | r = func(arg); |
| 592 | if (r) |
| 593 | break; |
| 594 | } |
Charles-François Natali | f23339a | 2011-07-23 18:15:43 +0200 | [diff] [blame] | 595 | busy = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 596 | return r; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | #else /* if ! defined WITH_THREAD */ |
| 600 | |
| 601 | /* |
| 602 | WARNING! ASYNCHRONOUSLY EXECUTING CODE! |
| 603 | This code is used for signal handling in python that isn't built |
| 604 | with WITH_THREAD. |
| 605 | Don't use this implementation when Py_AddPendingCalls() can happen |
| 606 | on a different thread! |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 607 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 608 | There are two possible race conditions: |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 609 | (1) nested asynchronous calls to Py_AddPendingCall() |
| 610 | (2) AddPendingCall() calls made while pending calls are being processed. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 611 | |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 612 | (1) is very unlikely because typically signal delivery |
| 613 | is blocked during signal handling. So it should be impossible. |
| 614 | (2) is a real possibility. |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 615 | The current code is safe against (2), but not against (1). |
| 616 | The safety against (2) is derived from the fact that only one |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 617 | thread is present, interrupted by signals, and that the critical |
| 618 | section is protected with the "busy" variable. On Windows, which |
| 619 | delivers SIGINT on a system thread, this does not hold and therefore |
| 620 | Windows really shouldn't use this version. |
| 621 | The two threads could theoretically wiggle around the "busy" variable. |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 622 | */ |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 623 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 624 | #define NPENDINGCALLS 32 |
| 625 | static struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 626 | int (*func)(void *); |
| 627 | void *arg; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 628 | } pendingcalls[NPENDINGCALLS]; |
| 629 | static volatile int pendingfirst = 0; |
| 630 | static volatile int pendinglast = 0; |
Benjamin Peterson | 08ec84c | 2010-05-30 14:49:32 +0000 | [diff] [blame] | 631 | static _Py_atomic_int pendingcalls_to_do = {0}; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 632 | |
| 633 | int |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 634 | Py_AddPendingCall(int (*func)(void *), void *arg) |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 635 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 636 | static volatile int busy = 0; |
| 637 | int i, j; |
| 638 | /* XXX Begin critical section */ |
| 639 | if (busy) |
| 640 | return -1; |
| 641 | busy = 1; |
| 642 | i = pendinglast; |
| 643 | j = (i + 1) % NPENDINGCALLS; |
| 644 | if (j == pendingfirst) { |
| 645 | busy = 0; |
| 646 | return -1; /* Queue full */ |
| 647 | } |
| 648 | pendingcalls[i].func = func; |
| 649 | pendingcalls[i].arg = arg; |
| 650 | pendinglast = j; |
Skip Montanaro | d581d77 | 2002-09-03 20:10:45 +0000 | [diff] [blame] | 651 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 652 | SIGNAL_PENDING_CALLS(); |
| 653 | busy = 0; |
| 654 | /* XXX End critical section */ |
| 655 | return 0; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 656 | } |
| 657 | |
Guido van Rossum | 180d7b4 | 1994-09-29 09:45:57 +0000 | [diff] [blame] | 658 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 659 | Py_MakePendingCalls(void) |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 660 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 661 | static int busy = 0; |
| 662 | if (busy) |
| 663 | return 0; |
| 664 | busy = 1; |
| 665 | UNSIGNAL_PENDING_CALLS(); |
| 666 | for (;;) { |
| 667 | int i; |
| 668 | int (*func)(void *); |
| 669 | void *arg; |
| 670 | i = pendingfirst; |
| 671 | if (i == pendinglast) |
| 672 | break; /* Queue empty */ |
| 673 | func = pendingcalls[i].func; |
| 674 | arg = pendingcalls[i].arg; |
| 675 | pendingfirst = (i + 1) % NPENDINGCALLS; |
| 676 | if (func(arg) < 0) { |
| 677 | busy = 0; |
| 678 | SIGNAL_PENDING_CALLS(); /* We're not done yet */ |
| 679 | return -1; |
| 680 | } |
| 681 | } |
| 682 | busy = 0; |
| 683 | return 0; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 684 | } |
| 685 | |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 686 | #endif /* WITH_THREAD */ |
| 687 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 688 | |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 689 | /* The interpreter's recursion limit */ |
| 690 | |
Hye-Shik Chang | b6fa281 | 2005-04-04 15:49:02 +0000 | [diff] [blame] | 691 | #ifndef Py_DEFAULT_RECURSION_LIMIT |
| 692 | #define Py_DEFAULT_RECURSION_LIMIT 1000 |
| 693 | #endif |
| 694 | static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT; |
| 695 | int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 696 | |
Vladimir Marangozov | 7bd25be | 2000-09-01 11:07:19 +0000 | [diff] [blame] | 697 | int |
| 698 | Py_GetRecursionLimit(void) |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 699 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 700 | return recursion_limit; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 701 | } |
| 702 | |
Vladimir Marangozov | 7bd25be | 2000-09-01 11:07:19 +0000 | [diff] [blame] | 703 | void |
| 704 | Py_SetRecursionLimit(int new_limit) |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 705 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 706 | recursion_limit = new_limit; |
| 707 | _Py_CheckRecursionLimit = recursion_limit; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 708 | } |
| 709 | |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 710 | /* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall() |
| 711 | if the recursion_depth reaches _Py_CheckRecursionLimit. |
| 712 | If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit |
| 713 | to guarantee that _Py_CheckRecursiveCall() is regularly called. |
| 714 | Without USE_STACKCHECK, there is no need for this. */ |
| 715 | int |
Serhiy Storchaka | 5fa22fc | 2015-06-21 16:26:28 +0300 | [diff] [blame] | 716 | _Py_CheckRecursiveCall(const char *where) |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 717 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 718 | PyThreadState *tstate = PyThreadState_GET(); |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 719 | |
| 720 | #ifdef USE_STACKCHECK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 721 | if (PyOS_CheckStack()) { |
| 722 | --tstate->recursion_depth; |
| 723 | PyErr_SetString(PyExc_MemoryError, "Stack overflow"); |
| 724 | return -1; |
| 725 | } |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 726 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 727 | _Py_CheckRecursionLimit = recursion_limit; |
| 728 | if (tstate->recursion_critical) |
| 729 | /* Somebody asked that we don't check for recursion. */ |
| 730 | return 0; |
| 731 | if (tstate->overflowed) { |
| 732 | if (tstate->recursion_depth > recursion_limit + 50) { |
| 733 | /* Overflowing while handling an overflow. Give up. */ |
| 734 | Py_FatalError("Cannot recover from stack overflow."); |
| 735 | } |
| 736 | return 0; |
| 737 | } |
| 738 | if (tstate->recursion_depth > recursion_limit) { |
| 739 | --tstate->recursion_depth; |
| 740 | tstate->overflowed = 1; |
Yury Selivanov | f488fb4 | 2015-07-03 01:04:23 -0400 | [diff] [blame] | 741 | PyErr_Format(PyExc_RecursionError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 742 | "maximum recursion depth exceeded%s", |
| 743 | where); |
| 744 | return -1; |
| 745 | } |
| 746 | return 0; |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 747 | } |
| 748 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 749 | /* Status code for main loop (reason for stack unwind) */ |
Raymond Hettinger | 7c95865 | 2004-04-06 10:11:10 +0000 | [diff] [blame] | 750 | enum why_code { |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 751 | WHY_NOT = 0x0001, /* No error */ |
| 752 | WHY_EXCEPTION = 0x0002, /* Exception occurred */ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 753 | WHY_RETURN = 0x0008, /* 'return' statement */ |
| 754 | WHY_BREAK = 0x0010, /* 'break' statement */ |
| 755 | WHY_CONTINUE = 0x0020, /* 'continue' statement */ |
| 756 | WHY_YIELD = 0x0040, /* 'yield' operator */ |
| 757 | WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */ |
Raymond Hettinger | 7c95865 | 2004-04-06 10:11:10 +0000 | [diff] [blame] | 758 | }; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 759 | |
Benjamin Peterson | 8788024 | 2011-07-03 16:48:31 -0500 | [diff] [blame] | 760 | static void save_exc_state(PyThreadState *, PyFrameObject *); |
| 761 | static void swap_exc_state(PyThreadState *, PyFrameObject *); |
| 762 | static void restore_and_clear_exc_state(PyThreadState *, PyFrameObject *); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 763 | static int do_raise(PyObject *, PyObject *); |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 764 | static int unpack_iterable(PyObject *, int, int, PyObject **); |
Guido van Rossum | 1aa1483 | 1997-01-21 05:34:20 +0000 | [diff] [blame] | 765 | |
Jeffrey Yasskin | 008d8ef | 2008-12-06 17:09:27 +0000 | [diff] [blame] | 766 | /* Records whether tracing is on for any thread. Counts the number of |
| 767 | threads for which tstate->c_tracefunc is non-NULL, so if the value |
| 768 | is 0, we know we don't have to check this thread's c_tracefunc. |
| 769 | This speeds up the if statement in PyEval_EvalFrameEx() after |
| 770 | fast_next_opcode*/ |
| 771 | static int _Py_TracingPossible = 0; |
| 772 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 773 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 774 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 775 | PyObject * |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 776 | PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 777 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 778 | return PyEval_EvalCodeEx(co, |
| 779 | globals, locals, |
| 780 | (PyObject **)NULL, 0, |
| 781 | (PyObject **)NULL, 0, |
| 782 | (PyObject **)NULL, 0, |
| 783 | NULL, NULL); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | |
| 787 | /* Interpreter main loop */ |
| 788 | |
Martin v. Löwis | 8d97e33 | 2004-06-27 15:43:12 +0000 | [diff] [blame] | 789 | PyObject * |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 790 | PyEval_EvalFrame(PyFrameObject *f) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 791 | /* This is for backward compatibility with extension modules that |
| 792 | used this API; core interpreter code should call |
| 793 | PyEval_EvalFrameEx() */ |
| 794 | return PyEval_EvalFrameEx(f, 0); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 798 | PyEval_EvalFrameEx(PyFrameObject *f, int throwflag) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 799 | { |
Brett Cannon | 3cebf93 | 2016-09-05 15:33:46 -0700 | [diff] [blame] | 800 | PyThreadState *tstate = PyThreadState_GET(); |
| 801 | return tstate->interp->eval_frame(f, throwflag); |
| 802 | } |
| 803 | |
| 804 | PyObject * |
| 805 | _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag) |
| 806 | { |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 807 | #ifdef DXPAIRS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 808 | int lastopcode = 0; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 809 | #endif |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 810 | PyObject **stack_pointer; /* Next free slot in value stack */ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 811 | const unsigned short *next_instr; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 812 | int opcode; /* Current opcode */ |
| 813 | int oparg; /* Current opcode argument, if any */ |
| 814 | enum why_code why; /* Reason for block stack unwind */ |
| 815 | PyObject **fastlocals, **freevars; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 816 | PyObject *retval = NULL; /* Return value */ |
| 817 | PyThreadState *tstate = PyThreadState_GET(); |
| 818 | PyCodeObject *co; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 819 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 820 | /* when tracing we set things up so that |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 821 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 822 | not (instr_lb <= current_bytecode_offset < instr_ub) |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 823 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 824 | is true when the line being executed has changed. The |
| 825 | initial values are such as to make this false the first |
| 826 | time it is tested. */ |
| 827 | int instr_ub = -1, instr_lb = 0, instr_prev = -1; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 828 | |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 829 | const unsigned short *first_instr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 830 | PyObject *names; |
| 831 | PyObject *consts; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 832 | |
Brett Cannon | 368b4b7 | 2012-04-02 12:17:59 -0400 | [diff] [blame] | 833 | #ifdef LLTRACE |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 834 | _Py_IDENTIFIER(__ltrace__); |
Brett Cannon | 368b4b7 | 2012-04-02 12:17:59 -0400 | [diff] [blame] | 835 | #endif |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 836 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 837 | /* Computed GOTOs, or |
| 838 | the-optimization-commonly-but-improperly-known-as-"threaded code" |
| 839 | using gcc's labels-as-values extension |
| 840 | (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html). |
| 841 | |
| 842 | The traditional bytecode evaluation loop uses a "switch" statement, which |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 843 | decent compilers will optimize as a single indirect branch instruction |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 844 | combined with a lookup table of jump addresses. However, since the |
| 845 | indirect jump instruction is shared by all opcodes, the CPU will have a |
| 846 | hard time making the right prediction for where to jump next (actually, |
| 847 | it will be always wrong except in the uncommon case of a sequence of |
| 848 | several identical opcodes). |
| 849 | |
| 850 | "Threaded code" in contrast, uses an explicit jump table and an explicit |
| 851 | indirect jump instruction at the end of each opcode. Since the jump |
| 852 | instruction is at a different address for each opcode, the CPU will make a |
| 853 | separate prediction for each of these instructions, which is equivalent to |
| 854 | predicting the second opcode of each opcode pair. These predictions have |
| 855 | a much better chance to turn out valid, especially in small bytecode loops. |
| 856 | |
| 857 | A mispredicted branch on a modern CPU flushes the whole pipeline and |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 858 | can cost several CPU cycles (depending on the pipeline depth), |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 859 | and potentially many more instructions (depending on the pipeline width). |
| 860 | A correctly predicted branch, however, is nearly free. |
| 861 | |
| 862 | At the time of this writing, the "threaded code" version is up to 15-20% |
| 863 | faster than the normal "switch" version, depending on the compiler and the |
| 864 | CPU architecture. |
| 865 | |
| 866 | We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined, |
| 867 | because it would render the measurements invalid. |
| 868 | |
| 869 | |
| 870 | NOTE: care must be taken that the compiler doesn't try to "optimize" the |
| 871 | indirect jumps by sharing them between all opcodes. Such optimizations |
| 872 | can be disabled on gcc by using the -fno-gcse flag (or possibly |
| 873 | -fno-crossjumping). |
| 874 | */ |
| 875 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 876 | #ifdef DYNAMIC_EXECUTION_PROFILE |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 877 | #undef USE_COMPUTED_GOTOS |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 878 | #define USE_COMPUTED_GOTOS 0 |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 879 | #endif |
| 880 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 881 | #ifdef HAVE_COMPUTED_GOTOS |
| 882 | #ifndef USE_COMPUTED_GOTOS |
| 883 | #define USE_COMPUTED_GOTOS 1 |
| 884 | #endif |
| 885 | #else |
| 886 | #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS |
| 887 | #error "Computed gotos are not supported on this compiler." |
| 888 | #endif |
| 889 | #undef USE_COMPUTED_GOTOS |
| 890 | #define USE_COMPUTED_GOTOS 0 |
| 891 | #endif |
| 892 | |
| 893 | #if USE_COMPUTED_GOTOS |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 894 | /* Import the static jump table */ |
| 895 | #include "opcode_targets.h" |
| 896 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 897 | #define TARGET(op) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 898 | TARGET_##op: \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 899 | case op: |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 900 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 901 | #define DISPATCH() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 902 | { \ |
| 903 | if (!_Py_atomic_load_relaxed(&eval_breaker)) { \ |
| 904 | FAST_DISPATCH(); \ |
| 905 | } \ |
| 906 | continue; \ |
| 907 | } |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 908 | |
| 909 | #ifdef LLTRACE |
| 910 | #define FAST_DISPATCH() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 911 | { \ |
| 912 | if (!lltrace && !_Py_TracingPossible) { \ |
| 913 | f->f_lasti = INSTR_OFFSET(); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 914 | NEXTOPARG(); \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 915 | goto *opcode_targets[opcode]; \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 916 | } \ |
| 917 | goto fast_next_opcode; \ |
| 918 | } |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 919 | #else |
| 920 | #define FAST_DISPATCH() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 921 | { \ |
| 922 | if (!_Py_TracingPossible) { \ |
| 923 | f->f_lasti = INSTR_OFFSET(); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 924 | NEXTOPARG(); \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 925 | goto *opcode_targets[opcode]; \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 926 | } \ |
| 927 | goto fast_next_opcode; \ |
| 928 | } |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 929 | #endif |
| 930 | |
| 931 | #else |
| 932 | #define TARGET(op) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 933 | case op: |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 934 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 935 | #define DISPATCH() continue |
| 936 | #define FAST_DISPATCH() goto fast_next_opcode |
| 937 | #endif |
| 938 | |
| 939 | |
Neal Norwitz | a81d220 | 2002-07-14 00:27:26 +0000 | [diff] [blame] | 940 | /* Tuple access macros */ |
| 941 | |
| 942 | #ifndef Py_DEBUG |
| 943 | #define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i)) |
| 944 | #else |
| 945 | #define GETITEM(v, i) PyTuple_GetItem((v), (i)) |
| 946 | #endif |
| 947 | |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 948 | #ifdef WITH_TSC |
| 949 | /* Use Pentium timestamp counter to mark certain events: |
| 950 | inst0 -- beginning of switch statement for opcode dispatch |
| 951 | inst1 -- end of switch statement (may be skipped) |
| 952 | loop0 -- the top of the mainloop |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 953 | loop1 -- place where control returns again to top of mainloop |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 954 | (may be skipped) |
| 955 | intr1 -- beginning of long interruption |
| 956 | intr2 -- end of long interruption |
| 957 | |
| 958 | Many opcodes call out to helper C functions. In some cases, the |
| 959 | time in those functions should be counted towards the time for the |
| 960 | opcode, but not in all cases. For example, a CALL_FUNCTION opcode |
| 961 | calls another Python function; there's no point in charge all the |
| 962 | bytecode executed by the called function to the caller. |
| 963 | |
| 964 | It's hard to make a useful judgement statically. In the presence |
| 965 | of operator overloading, it's impossible to tell if a call will |
| 966 | execute new Python code or not. |
| 967 | |
| 968 | It's a case-by-case judgement. I'll use intr1 for the following |
| 969 | cases: |
| 970 | |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 971 | IMPORT_STAR |
| 972 | IMPORT_FROM |
| 973 | CALL_FUNCTION (and friends) |
| 974 | |
| 975 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 976 | uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0; |
| 977 | int ticked = 0; |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 978 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 979 | READ_TIMESTAMP(inst0); |
| 980 | READ_TIMESTAMP(inst1); |
| 981 | READ_TIMESTAMP(loop0); |
| 982 | READ_TIMESTAMP(loop1); |
Michael W. Hudson | 800ba23 | 2004-08-12 18:19:17 +0000 | [diff] [blame] | 983 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 984 | /* shut up the compiler */ |
| 985 | opcode = 0; |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 986 | #endif |
| 987 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 988 | /* Code access macros */ |
| 989 | |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 990 | #ifdef WORDS_BIGENDIAN |
| 991 | #define OPCODE(word) ((word) >> 8) |
| 992 | #define OPARG(word) ((word) & 255) |
| 993 | #else |
| 994 | #define OPCODE(word) ((word) & 255) |
| 995 | #define OPARG(word) ((word) >> 8) |
| 996 | #endif |
| 997 | /* The integer overflow is checked by an assertion below. */ |
| 998 | #define INSTR_OFFSET() (2*(int)(next_instr - first_instr)) |
| 999 | #define NEXTOPARG() do { \ |
| 1000 | unsigned short word = *next_instr; \ |
| 1001 | opcode = OPCODE(word); \ |
| 1002 | oparg = OPARG(word); \ |
| 1003 | next_instr++; \ |
| 1004 | } while (0) |
| 1005 | #define JUMPTO(x) (next_instr = first_instr + (x)/2) |
| 1006 | #define JUMPBY(x) (next_instr += (x)/2) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1007 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 1008 | /* OpCode prediction macros |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1009 | Some opcodes tend to come in pairs thus making it possible to |
| 1010 | predict the second code when the first is run. For example, |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 1011 | COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE. |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 1012 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1013 | Verifying the prediction costs a single high-speed test of a register |
| 1014 | variable against a constant. If the pairing was good, then the |
| 1015 | processor's own internal branch predication has a high likelihood of |
| 1016 | success, resulting in a nearly zero-overhead transition to the |
| 1017 | next opcode. A successful prediction saves a trip through the eval-loop |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1018 | including its unpredictable switch-case branch. Combined with the |
| 1019 | processor's internal branch prediction, a successful PREDICT has the |
| 1020 | effect of making the two opcodes run as if they were a single new opcode |
| 1021 | with the bodies combined. |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 1022 | |
Georg Brandl | 86b2fb9 | 2008-07-16 03:43:04 +0000 | [diff] [blame] | 1023 | If collecting opcode statistics, your choices are to either keep the |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1024 | predictions turned-on and interpret the results as if some opcodes |
| 1025 | had been combined or turn-off predictions so that the opcode frequency |
| 1026 | counter updates for both opcodes. |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 1027 | |
| 1028 | Opcode prediction is disabled with threaded code, since the latter allows |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1029 | the CPU to record separate branch prediction information for each |
| 1030 | opcode. |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 1031 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 1032 | */ |
| 1033 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 1034 | #if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1035 | #define PREDICT(op) if (0) goto PRED_##op |
Raymond Hettinger | a721698 | 2004-02-08 19:59:27 +0000 | [diff] [blame] | 1036 | #else |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1037 | #define PREDICT(op) \ |
| 1038 | do{ \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 1039 | unsigned short word = *next_instr; \ |
| 1040 | opcode = OPCODE(word); \ |
| 1041 | if (opcode == op){ \ |
| 1042 | oparg = OPARG(word); \ |
| 1043 | next_instr++; \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1044 | goto PRED_##op; \ |
| 1045 | } \ |
| 1046 | } while(0) |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 1047 | #endif |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1048 | #define PREDICTED(op) PRED_##op: |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 1049 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 1050 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1051 | /* Stack manipulation macros */ |
| 1052 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1053 | /* The stack can grow at most MAXINT deep, as co_nlocals and |
| 1054 | co_stacksize are ints. */ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 1055 | #define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack)) |
| 1056 | #define EMPTY() (STACK_LEVEL() == 0) |
| 1057 | #define TOP() (stack_pointer[-1]) |
| 1058 | #define SECOND() (stack_pointer[-2]) |
| 1059 | #define THIRD() (stack_pointer[-3]) |
| 1060 | #define FOURTH() (stack_pointer[-4]) |
| 1061 | #define PEEK(n) (stack_pointer[-(n)]) |
| 1062 | #define SET_TOP(v) (stack_pointer[-1] = (v)) |
| 1063 | #define SET_SECOND(v) (stack_pointer[-2] = (v)) |
| 1064 | #define SET_THIRD(v) (stack_pointer[-3] = (v)) |
| 1065 | #define SET_FOURTH(v) (stack_pointer[-4] = (v)) |
| 1066 | #define SET_VALUE(n, v) (stack_pointer[-(n)] = (v)) |
| 1067 | #define BASIC_STACKADJ(n) (stack_pointer += n) |
| 1068 | #define BASIC_PUSH(v) (*stack_pointer++ = (v)) |
| 1069 | #define BASIC_POP() (*--stack_pointer) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1070 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 1071 | #ifdef LLTRACE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1072 | #define PUSH(v) { (void)(BASIC_PUSH(v), \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 1073 | lltrace && prtrace(TOP(), "push")); \ |
| 1074 | assert(STACK_LEVEL() <= co->co_stacksize); } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1075 | #define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 1076 | BASIC_POP()) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1077 | #define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 1078 | lltrace && prtrace(TOP(), "stackadj")); \ |
| 1079 | assert(STACK_LEVEL() <= co->co_stacksize); } |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 1080 | #define EXT_POP(STACK_POINTER) ((void)(lltrace && \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 1081 | prtrace((STACK_POINTER)[-1], "ext_pop")), \ |
| 1082 | *--(STACK_POINTER)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1083 | #else |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 1084 | #define PUSH(v) BASIC_PUSH(v) |
| 1085 | #define POP() BASIC_POP() |
| 1086 | #define STACKADJ(n) BASIC_STACKADJ(n) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 1087 | #define EXT_POP(STACK_POINTER) (*--(STACK_POINTER)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1088 | #endif |
| 1089 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1090 | /* Local variable macros */ |
| 1091 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1092 | #define GETLOCAL(i) (fastlocals[i]) |
Guido van Rossum | cfbf1a3 | 2002-03-28 20:17:52 +0000 | [diff] [blame] | 1093 | |
| 1094 | /* The SETLOCAL() macro must not DECREF the local variable in-place and |
| 1095 | then store the new value; it must copy the old value to a temporary |
| 1096 | value, then store the new value, and then DECREF the temporary value. |
| 1097 | This is because it is possible that during the DECREF the frame is |
| 1098 | accessed by other code (e.g. a __del__ method or gc.collect()) and the |
| 1099 | variable would be pointing to already-freed memory. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1100 | #define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 1101 | GETLOCAL(i) = value; \ |
| 1102 | Py_XDECREF(tmp); } while (0) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1103 | |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 1104 | |
| 1105 | #define UNWIND_BLOCK(b) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1106 | while (STACK_LEVEL() > (b)->b_level) { \ |
| 1107 | PyObject *v = POP(); \ |
| 1108 | Py_XDECREF(v); \ |
| 1109 | } |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 1110 | |
| 1111 | #define UNWIND_EXCEPT_HANDLER(b) \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1112 | do { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1113 | PyObject *type, *value, *traceback; \ |
| 1114 | assert(STACK_LEVEL() >= (b)->b_level + 3); \ |
| 1115 | while (STACK_LEVEL() > (b)->b_level + 3) { \ |
| 1116 | value = POP(); \ |
| 1117 | Py_XDECREF(value); \ |
| 1118 | } \ |
| 1119 | type = tstate->exc_type; \ |
| 1120 | value = tstate->exc_value; \ |
| 1121 | traceback = tstate->exc_traceback; \ |
| 1122 | tstate->exc_type = POP(); \ |
| 1123 | tstate->exc_value = POP(); \ |
| 1124 | tstate->exc_traceback = POP(); \ |
| 1125 | Py_XDECREF(type); \ |
| 1126 | Py_XDECREF(value); \ |
| 1127 | Py_XDECREF(traceback); \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1128 | } while(0) |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 1129 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 1130 | /* Start of code */ |
| 1131 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1132 | /* push frame */ |
| 1133 | if (Py_EnterRecursiveCall("")) |
| 1134 | return NULL; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1135 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1136 | tstate->frame = f; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1137 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1138 | if (tstate->use_tracing) { |
| 1139 | if (tstate->c_tracefunc != NULL) { |
| 1140 | /* tstate->c_tracefunc, if defined, is a |
| 1141 | function that will be called on *every* entry |
| 1142 | to a code block. Its return value, if not |
| 1143 | None, is a function that will be called at |
| 1144 | the start of each executed line of code. |
| 1145 | (Actually, the function must return itself |
| 1146 | in order to continue tracing.) The trace |
| 1147 | functions are called with three arguments: |
| 1148 | a pointer to the current frame, a string |
| 1149 | indicating why the function is called, and |
| 1150 | an argument which depends on the situation. |
| 1151 | The global trace function is also called |
| 1152 | whenever an exception is detected. */ |
| 1153 | if (call_trace_protected(tstate->c_tracefunc, |
| 1154 | tstate->c_traceobj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 1155 | tstate, f, PyTrace_CALL, Py_None)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1156 | /* Trace function raised an error */ |
| 1157 | goto exit_eval_frame; |
| 1158 | } |
| 1159 | } |
| 1160 | if (tstate->c_profilefunc != NULL) { |
| 1161 | /* Similar for c_profilefunc, except it needn't |
| 1162 | return itself and isn't called for "line" events */ |
| 1163 | if (call_trace_protected(tstate->c_profilefunc, |
| 1164 | tstate->c_profileobj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 1165 | tstate, f, PyTrace_CALL, Py_None)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1166 | /* Profile function raised an error */ |
| 1167 | goto exit_eval_frame; |
| 1168 | } |
| 1169 | } |
| 1170 | } |
Neil Schemenauer | 6c0f200 | 2001-09-04 19:03:35 +0000 | [diff] [blame] | 1171 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1172 | co = f->f_code; |
| 1173 | names = co->co_names; |
| 1174 | consts = co->co_consts; |
| 1175 | fastlocals = f->f_localsplus; |
| 1176 | freevars = f->f_localsplus + co->co_nlocals; |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 1177 | assert(PyBytes_Check(co->co_code)); |
| 1178 | assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX); |
| 1179 | assert(PyBytes_GET_SIZE(co->co_code) % 2 == 0); |
Serhiy Storchaka | 74f2fe6 | 2016-05-25 20:35:44 +0300 | [diff] [blame] | 1180 | assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), 2)); |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 1181 | first_instr = (unsigned short*) PyBytes_AS_STRING(co->co_code); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1182 | /* |
| 1183 | f->f_lasti refers to the index of the last instruction, |
| 1184 | unless it's -1 in which case next_instr should be first_instr. |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1185 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1186 | YIELD_FROM sets f_lasti to itself, in order to repeatedly yield |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 1187 | multiple values. |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 1188 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1189 | When the PREDICT() macros are enabled, some opcode pairs follow in |
| 1190 | direct succession without updating f->f_lasti. A successful |
| 1191 | prediction effectively links the two codes together as if they |
| 1192 | were a single new opcode; accordingly,f->f_lasti will point to |
| 1193 | the first code in the pair (for instance, GET_ITER followed by |
| 1194 | FOR_ITER is effectively a single opcode and f->f_lasti will point |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1195 | to the beginning of the combined pair.) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1196 | */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1197 | next_instr = first_instr; |
| 1198 | if (f->f_lasti >= 0) { |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 1199 | assert(f->f_lasti % 2 == 0); |
| 1200 | next_instr += f->f_lasti/2 + 1; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1201 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1202 | stack_pointer = f->f_stacktop; |
| 1203 | assert(stack_pointer != NULL); |
| 1204 | f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */ |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 1205 | f->f_executing = 1; |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1206 | |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1207 | if (co->co_flags & (CO_GENERATOR | CO_COROUTINE)) { |
Victor Stinner | 26f7b8a | 2015-01-31 10:29:47 +0100 | [diff] [blame] | 1208 | 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] | 1209 | /* We were in an except handler when we left, |
| 1210 | restore the exception state which was put aside |
| 1211 | (see YIELD_VALUE). */ |
Benjamin Peterson | 8788024 | 2011-07-03 16:48:31 -0500 | [diff] [blame] | 1212 | swap_exc_state(tstate, f); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1213 | } |
Benjamin Peterson | 8788024 | 2011-07-03 16:48:31 -0500 | [diff] [blame] | 1214 | else |
| 1215 | save_exc_state(tstate, f); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1216 | } |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 1217 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1218 | #ifdef LLTRACE |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 1219 | lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1220 | #endif |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1221 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1222 | why = WHY_NOT; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1223 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1224 | if (throwflag) /* support for generator.throw() */ |
| 1225 | goto error; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1226 | |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 1227 | #ifdef Py_DEBUG |
| 1228 | /* PyEval_EvalFrameEx() must not be called with an exception set, |
| 1229 | because it may clear it (directly or indirectly) and so the |
Martin Panter | 9955a37 | 2015-10-07 10:26:23 +0000 | [diff] [blame] | 1230 | caller loses its exception */ |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 1231 | assert(!PyErr_Occurred()); |
| 1232 | #endif |
| 1233 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1234 | for (;;) { |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 1235 | #ifdef WITH_TSC |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1236 | if (inst1 == 0) { |
| 1237 | /* Almost surely, the opcode executed a break |
| 1238 | or a continue, preventing inst1 from being set |
| 1239 | on the way out of the loop. |
| 1240 | */ |
| 1241 | READ_TIMESTAMP(inst1); |
| 1242 | loop1 = inst1; |
| 1243 | } |
| 1244 | dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1, |
| 1245 | intr0, intr1); |
| 1246 | ticked = 0; |
| 1247 | inst1 = 0; |
| 1248 | intr0 = 0; |
| 1249 | intr1 = 0; |
| 1250 | READ_TIMESTAMP(loop0); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 1251 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1252 | assert(stack_pointer >= f->f_valuestack); /* else underflow */ |
| 1253 | assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */ |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 1254 | assert(!PyErr_Occurred()); |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 1255 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1256 | /* Do periodic things. Doing this every time through |
| 1257 | the loop would add too much overhead, so we do it |
| 1258 | only every Nth instruction. We also do it if |
| 1259 | ``pendingcalls_to_do'' is set, i.e. when an asynchronous |
| 1260 | event needs attention (e.g. a signal handler or |
| 1261 | async I/O handler); see Py_AddPendingCall() and |
| 1262 | Py_MakePendingCalls() above. */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1263 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1264 | if (_Py_atomic_load_relaxed(&eval_breaker)) { |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 1265 | if (OPCODE(*next_instr) == SETUP_FINALLY) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1266 | /* Make the last opcode before |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 1267 | a try: finally: block uninterruptible. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1268 | goto fast_next_opcode; |
| 1269 | } |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 1270 | #ifdef WITH_TSC |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1271 | ticked = 1; |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 1272 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1273 | if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1274 | if (Py_MakePendingCalls() < 0) |
| 1275 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1276 | } |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 1277 | #ifdef WITH_THREAD |
Benjamin Peterson | d2be5b4 | 2010-09-10 22:47:02 +0000 | [diff] [blame] | 1278 | if (_Py_atomic_load_relaxed(&gil_drop_request)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1279 | /* Give another thread a chance */ |
| 1280 | if (PyThreadState_Swap(NULL) != tstate) |
| 1281 | Py_FatalError("ceval: tstate mix-up"); |
| 1282 | drop_gil(tstate); |
| 1283 | |
| 1284 | /* Other threads may run now */ |
| 1285 | |
| 1286 | take_gil(tstate); |
Benjamin Peterson | 17548dd | 2014-06-16 22:59:07 -0700 | [diff] [blame] | 1287 | |
| 1288 | /* Check if we should make a quick exit. */ |
| 1289 | if (_Py_Finalizing && _Py_Finalizing != tstate) { |
| 1290 | drop_gil(tstate); |
| 1291 | PyThread_exit_thread(); |
| 1292 | } |
| 1293 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1294 | if (PyThreadState_Swap(tstate) != NULL) |
| 1295 | Py_FatalError("ceval: orphan tstate"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1296 | } |
Benjamin Peterson | d2be5b4 | 2010-09-10 22:47:02 +0000 | [diff] [blame] | 1297 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1298 | /* Check for asynchronous exceptions. */ |
| 1299 | if (tstate->async_exc != NULL) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1300 | PyObject *exc = tstate->async_exc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1301 | tstate->async_exc = NULL; |
| 1302 | UNSIGNAL_ASYNC_EXC(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1303 | PyErr_SetNone(exc); |
| 1304 | Py_DECREF(exc); |
| 1305 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1306 | } |
| 1307 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1308 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1309 | fast_next_opcode: |
| 1310 | f->f_lasti = INSTR_OFFSET(); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1311 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1312 | /* line-by-line tracing support */ |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1313 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1314 | if (_Py_TracingPossible && |
Benjamin Peterson | 51f4616 | 2013-01-23 08:38:47 -0500 | [diff] [blame] | 1315 | tstate->c_tracefunc != NULL && !tstate->tracing) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1316 | int err; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1317 | /* see maybe_call_line_trace |
| 1318 | for expository comments */ |
| 1319 | f->f_stacktop = stack_pointer; |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 1320 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1321 | err = maybe_call_line_trace(tstate->c_tracefunc, |
| 1322 | tstate->c_traceobj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 1323 | tstate, f, |
| 1324 | &instr_lb, &instr_ub, &instr_prev); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1325 | /* Reload possibly changed frame fields */ |
| 1326 | JUMPTO(f->f_lasti); |
| 1327 | if (f->f_stacktop != NULL) { |
| 1328 | stack_pointer = f->f_stacktop; |
| 1329 | f->f_stacktop = NULL; |
| 1330 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1331 | if (err) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1332 | /* trace function raised an exception */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1333 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1334 | } |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1335 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1336 | /* Extract opcode and argument */ |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1337 | |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 1338 | NEXTOPARG(); |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 1339 | dispatch_opcode: |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 1340 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 1341 | #ifdef DXPAIRS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1342 | dxpairs[lastopcode][opcode]++; |
| 1343 | lastopcode = opcode; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 1344 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1345 | dxp[opcode]++; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 1346 | #endif |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1347 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 1348 | #ifdef LLTRACE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1349 | /* Instruction tracing */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1350 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1351 | if (lltrace) { |
| 1352 | if (HAS_ARG(opcode)) { |
| 1353 | printf("%d: %d, %d\n", |
| 1354 | f->f_lasti, opcode, oparg); |
| 1355 | } |
| 1356 | else { |
| 1357 | printf("%d: %d\n", |
| 1358 | f->f_lasti, opcode); |
| 1359 | } |
| 1360 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1361 | #endif |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 1362 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1363 | /* Main switch on opcode */ |
| 1364 | READ_TIMESTAMP(inst0); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 1365 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1366 | switch (opcode) { |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1367 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1368 | /* BEWARE! |
| 1369 | It is essential that any operation that fails sets either |
| 1370 | x to NULL, err to nonzero, or why to anything but WHY_NOT, |
| 1371 | and that no operation that succeeds does this! */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1372 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1373 | TARGET(NOP) |
| 1374 | FAST_DISPATCH(); |
Raymond Hettinger | 9c18e81 | 2004-06-21 16:31:15 +0000 | [diff] [blame] | 1375 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1376 | TARGET(LOAD_FAST) { |
| 1377 | PyObject *value = GETLOCAL(oparg); |
| 1378 | if (value == NULL) { |
| 1379 | format_exc_check_arg(PyExc_UnboundLocalError, |
| 1380 | UNBOUNDLOCAL_ERROR_MSG, |
| 1381 | PyTuple_GetItem(co->co_varnames, oparg)); |
| 1382 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1383 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1384 | Py_INCREF(value); |
| 1385 | PUSH(value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1386 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1387 | } |
| 1388 | |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 1389 | PREDICTED(LOAD_CONST); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1390 | TARGET(LOAD_CONST) { |
| 1391 | PyObject *value = GETITEM(consts, oparg); |
| 1392 | Py_INCREF(value); |
| 1393 | PUSH(value); |
| 1394 | FAST_DISPATCH(); |
| 1395 | } |
Neil Schemenauer | 6354386 | 2002-02-17 19:10:14 +0000 | [diff] [blame] | 1396 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1397 | PREDICTED(STORE_FAST); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1398 | TARGET(STORE_FAST) { |
| 1399 | PyObject *value = POP(); |
| 1400 | SETLOCAL(oparg, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1401 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1402 | } |
Neil Schemenauer | 6354386 | 2002-02-17 19:10:14 +0000 | [diff] [blame] | 1403 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1404 | TARGET(POP_TOP) { |
| 1405 | PyObject *value = POP(); |
| 1406 | Py_DECREF(value); |
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_TWO) { |
| 1411 | PyObject *top = TOP(); |
| 1412 | PyObject *second = SECOND(); |
| 1413 | SET_TOP(second); |
| 1414 | SET_SECOND(top); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1415 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1416 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1417 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1418 | TARGET(ROT_THREE) { |
| 1419 | PyObject *top = TOP(); |
| 1420 | PyObject *second = SECOND(); |
| 1421 | PyObject *third = THIRD(); |
| 1422 | SET_TOP(second); |
| 1423 | SET_SECOND(third); |
| 1424 | SET_THIRD(top); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1425 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1426 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1427 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1428 | TARGET(DUP_TOP) { |
| 1429 | PyObject *top = TOP(); |
| 1430 | Py_INCREF(top); |
| 1431 | PUSH(top); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1432 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1433 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1434 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1435 | TARGET(DUP_TOP_TWO) { |
| 1436 | PyObject *top = TOP(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1437 | PyObject *second = SECOND(); |
Benjamin Peterson | f208df3 | 2012-10-12 11:37:56 -0400 | [diff] [blame] | 1438 | Py_INCREF(top); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1439 | Py_INCREF(second); |
Antoine Pitrou | 74a69fa | 2010-09-04 18:43:52 +0000 | [diff] [blame] | 1440 | STACKADJ(2); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1441 | SET_TOP(top); |
| 1442 | SET_SECOND(second); |
Antoine Pitrou | 74a69fa | 2010-09-04 18:43:52 +0000 | [diff] [blame] | 1443 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1444 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1445 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1446 | TARGET(UNARY_POSITIVE) { |
| 1447 | PyObject *value = TOP(); |
| 1448 | PyObject *res = PyNumber_Positive(value); |
| 1449 | Py_DECREF(value); |
| 1450 | SET_TOP(res); |
| 1451 | if (res == NULL) |
| 1452 | goto error; |
| 1453 | DISPATCH(); |
| 1454 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1455 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1456 | TARGET(UNARY_NEGATIVE) { |
| 1457 | PyObject *value = TOP(); |
| 1458 | PyObject *res = PyNumber_Negative(value); |
| 1459 | Py_DECREF(value); |
| 1460 | SET_TOP(res); |
| 1461 | if (res == NULL) |
| 1462 | goto error; |
| 1463 | DISPATCH(); |
| 1464 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1465 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1466 | TARGET(UNARY_NOT) { |
| 1467 | PyObject *value = TOP(); |
| 1468 | int err = PyObject_IsTrue(value); |
| 1469 | Py_DECREF(value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1470 | if (err == 0) { |
| 1471 | Py_INCREF(Py_True); |
| 1472 | SET_TOP(Py_True); |
| 1473 | DISPATCH(); |
| 1474 | } |
| 1475 | else if (err > 0) { |
| 1476 | Py_INCREF(Py_False); |
| 1477 | SET_TOP(Py_False); |
| 1478 | err = 0; |
| 1479 | DISPATCH(); |
| 1480 | } |
| 1481 | STACKADJ(-1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1482 | goto error; |
| 1483 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1484 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1485 | TARGET(UNARY_INVERT) { |
| 1486 | PyObject *value = TOP(); |
| 1487 | PyObject *res = PyNumber_Invert(value); |
| 1488 | Py_DECREF(value); |
| 1489 | SET_TOP(res); |
| 1490 | if (res == NULL) |
| 1491 | goto error; |
| 1492 | DISPATCH(); |
| 1493 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1494 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1495 | TARGET(BINARY_POWER) { |
| 1496 | PyObject *exp = POP(); |
| 1497 | PyObject *base = TOP(); |
| 1498 | PyObject *res = PyNumber_Power(base, exp, Py_None); |
| 1499 | Py_DECREF(base); |
| 1500 | Py_DECREF(exp); |
| 1501 | SET_TOP(res); |
| 1502 | if (res == NULL) |
| 1503 | goto error; |
| 1504 | DISPATCH(); |
| 1505 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1506 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1507 | TARGET(BINARY_MULTIPLY) { |
| 1508 | PyObject *right = POP(); |
| 1509 | PyObject *left = TOP(); |
| 1510 | PyObject *res = PyNumber_Multiply(left, right); |
| 1511 | Py_DECREF(left); |
| 1512 | Py_DECREF(right); |
| 1513 | SET_TOP(res); |
| 1514 | if (res == NULL) |
| 1515 | goto error; |
| 1516 | DISPATCH(); |
| 1517 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1518 | |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1519 | TARGET(BINARY_MATRIX_MULTIPLY) { |
| 1520 | PyObject *right = POP(); |
| 1521 | PyObject *left = TOP(); |
| 1522 | PyObject *res = PyNumber_MatrixMultiply(left, right); |
| 1523 | Py_DECREF(left); |
| 1524 | Py_DECREF(right); |
| 1525 | SET_TOP(res); |
| 1526 | if (res == NULL) |
| 1527 | goto error; |
| 1528 | DISPATCH(); |
| 1529 | } |
| 1530 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1531 | TARGET(BINARY_TRUE_DIVIDE) { |
| 1532 | PyObject *divisor = POP(); |
| 1533 | PyObject *dividend = TOP(); |
| 1534 | PyObject *quotient = PyNumber_TrueDivide(dividend, divisor); |
| 1535 | Py_DECREF(dividend); |
| 1536 | Py_DECREF(divisor); |
| 1537 | SET_TOP(quotient); |
| 1538 | if (quotient == NULL) |
| 1539 | goto error; |
| 1540 | DISPATCH(); |
| 1541 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1542 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1543 | TARGET(BINARY_FLOOR_DIVIDE) { |
| 1544 | PyObject *divisor = POP(); |
| 1545 | PyObject *dividend = TOP(); |
| 1546 | PyObject *quotient = PyNumber_FloorDivide(dividend, divisor); |
| 1547 | Py_DECREF(dividend); |
| 1548 | Py_DECREF(divisor); |
| 1549 | SET_TOP(quotient); |
| 1550 | if (quotient == NULL) |
| 1551 | goto error; |
| 1552 | DISPATCH(); |
| 1553 | } |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1554 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1555 | TARGET(BINARY_MODULO) { |
| 1556 | PyObject *divisor = POP(); |
| 1557 | PyObject *dividend = TOP(); |
| 1558 | PyObject *res = PyUnicode_CheckExact(dividend) ? |
| 1559 | PyUnicode_Format(dividend, divisor) : |
| 1560 | PyNumber_Remainder(dividend, divisor); |
| 1561 | Py_DECREF(divisor); |
| 1562 | Py_DECREF(dividend); |
| 1563 | SET_TOP(res); |
| 1564 | if (res == NULL) |
| 1565 | goto error; |
| 1566 | DISPATCH(); |
| 1567 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1568 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1569 | TARGET(BINARY_ADD) { |
| 1570 | PyObject *right = POP(); |
| 1571 | PyObject *left = TOP(); |
| 1572 | PyObject *sum; |
| 1573 | if (PyUnicode_CheckExact(left) && |
| 1574 | PyUnicode_CheckExact(right)) { |
| 1575 | sum = unicode_concatenate(left, right, f, next_instr); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1576 | /* unicode_concatenate consumed the ref to left */ |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 1577 | } |
| 1578 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1579 | sum = PyNumber_Add(left, right); |
| 1580 | Py_DECREF(left); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 1581 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1582 | Py_DECREF(right); |
| 1583 | SET_TOP(sum); |
| 1584 | if (sum == NULL) |
| 1585 | goto error; |
| 1586 | DISPATCH(); |
| 1587 | } |
| 1588 | |
| 1589 | TARGET(BINARY_SUBTRACT) { |
| 1590 | PyObject *right = POP(); |
| 1591 | PyObject *left = TOP(); |
| 1592 | PyObject *diff = PyNumber_Subtract(left, right); |
| 1593 | Py_DECREF(right); |
| 1594 | Py_DECREF(left); |
| 1595 | SET_TOP(diff); |
| 1596 | if (diff == NULL) |
| 1597 | goto error; |
| 1598 | DISPATCH(); |
| 1599 | } |
| 1600 | |
| 1601 | TARGET(BINARY_SUBSCR) { |
| 1602 | PyObject *sub = POP(); |
| 1603 | PyObject *container = TOP(); |
| 1604 | PyObject *res = PyObject_GetItem(container, sub); |
| 1605 | Py_DECREF(container); |
| 1606 | Py_DECREF(sub); |
| 1607 | SET_TOP(res); |
| 1608 | if (res == NULL) |
| 1609 | goto error; |
| 1610 | DISPATCH(); |
| 1611 | } |
| 1612 | |
| 1613 | TARGET(BINARY_LSHIFT) { |
| 1614 | PyObject *right = POP(); |
| 1615 | PyObject *left = TOP(); |
| 1616 | PyObject *res = PyNumber_Lshift(left, right); |
| 1617 | Py_DECREF(left); |
| 1618 | Py_DECREF(right); |
| 1619 | SET_TOP(res); |
| 1620 | if (res == NULL) |
| 1621 | goto error; |
| 1622 | DISPATCH(); |
| 1623 | } |
| 1624 | |
| 1625 | TARGET(BINARY_RSHIFT) { |
| 1626 | PyObject *right = POP(); |
| 1627 | PyObject *left = TOP(); |
| 1628 | PyObject *res = PyNumber_Rshift(left, right); |
| 1629 | Py_DECREF(left); |
| 1630 | Py_DECREF(right); |
| 1631 | SET_TOP(res); |
| 1632 | if (res == NULL) |
| 1633 | goto error; |
| 1634 | DISPATCH(); |
| 1635 | } |
| 1636 | |
| 1637 | TARGET(BINARY_AND) { |
| 1638 | PyObject *right = POP(); |
| 1639 | PyObject *left = TOP(); |
| 1640 | PyObject *res = PyNumber_And(left, right); |
| 1641 | Py_DECREF(left); |
| 1642 | Py_DECREF(right); |
| 1643 | SET_TOP(res); |
| 1644 | if (res == NULL) |
| 1645 | goto error; |
| 1646 | DISPATCH(); |
| 1647 | } |
| 1648 | |
| 1649 | TARGET(BINARY_XOR) { |
| 1650 | PyObject *right = POP(); |
| 1651 | PyObject *left = TOP(); |
| 1652 | PyObject *res = PyNumber_Xor(left, right); |
| 1653 | Py_DECREF(left); |
| 1654 | Py_DECREF(right); |
| 1655 | SET_TOP(res); |
| 1656 | if (res == NULL) |
| 1657 | goto error; |
| 1658 | DISPATCH(); |
| 1659 | } |
| 1660 | |
| 1661 | TARGET(BINARY_OR) { |
| 1662 | PyObject *right = POP(); |
| 1663 | PyObject *left = TOP(); |
| 1664 | PyObject *res = PyNumber_Or(left, right); |
| 1665 | Py_DECREF(left); |
| 1666 | Py_DECREF(right); |
| 1667 | SET_TOP(res); |
| 1668 | if (res == NULL) |
| 1669 | goto error; |
| 1670 | DISPATCH(); |
| 1671 | } |
| 1672 | |
| 1673 | TARGET(LIST_APPEND) { |
| 1674 | PyObject *v = POP(); |
| 1675 | PyObject *list = PEEK(oparg); |
| 1676 | int err; |
| 1677 | err = PyList_Append(list, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1678 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1679 | if (err != 0) |
| 1680 | goto error; |
| 1681 | PREDICT(JUMP_ABSOLUTE); |
| 1682 | DISPATCH(); |
| 1683 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1684 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1685 | TARGET(SET_ADD) { |
| 1686 | PyObject *v = POP(); |
| 1687 | PyObject *set = stack_pointer[-oparg]; |
| 1688 | int err; |
| 1689 | err = PySet_Add(set, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1690 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1691 | if (err != 0) |
| 1692 | goto error; |
| 1693 | PREDICT(JUMP_ABSOLUTE); |
| 1694 | DISPATCH(); |
| 1695 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1696 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1697 | TARGET(INPLACE_POWER) { |
| 1698 | PyObject *exp = POP(); |
| 1699 | PyObject *base = TOP(); |
| 1700 | PyObject *res = PyNumber_InPlacePower(base, exp, Py_None); |
| 1701 | Py_DECREF(base); |
| 1702 | Py_DECREF(exp); |
| 1703 | SET_TOP(res); |
| 1704 | if (res == NULL) |
| 1705 | goto error; |
| 1706 | DISPATCH(); |
| 1707 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1708 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1709 | TARGET(INPLACE_MULTIPLY) { |
| 1710 | PyObject *right = POP(); |
| 1711 | PyObject *left = TOP(); |
| 1712 | PyObject *res = PyNumber_InPlaceMultiply(left, right); |
| 1713 | Py_DECREF(left); |
| 1714 | Py_DECREF(right); |
| 1715 | SET_TOP(res); |
| 1716 | if (res == NULL) |
| 1717 | goto error; |
| 1718 | DISPATCH(); |
| 1719 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1720 | |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1721 | TARGET(INPLACE_MATRIX_MULTIPLY) { |
| 1722 | PyObject *right = POP(); |
| 1723 | PyObject *left = TOP(); |
| 1724 | PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right); |
| 1725 | Py_DECREF(left); |
| 1726 | Py_DECREF(right); |
| 1727 | SET_TOP(res); |
| 1728 | if (res == NULL) |
| 1729 | goto error; |
| 1730 | DISPATCH(); |
| 1731 | } |
| 1732 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1733 | TARGET(INPLACE_TRUE_DIVIDE) { |
| 1734 | PyObject *divisor = POP(); |
| 1735 | PyObject *dividend = TOP(); |
| 1736 | PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor); |
| 1737 | Py_DECREF(dividend); |
| 1738 | Py_DECREF(divisor); |
| 1739 | SET_TOP(quotient); |
| 1740 | if (quotient == NULL) |
| 1741 | goto error; |
| 1742 | DISPATCH(); |
| 1743 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1744 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1745 | TARGET(INPLACE_FLOOR_DIVIDE) { |
| 1746 | PyObject *divisor = POP(); |
| 1747 | PyObject *dividend = TOP(); |
| 1748 | PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor); |
| 1749 | Py_DECREF(dividend); |
| 1750 | Py_DECREF(divisor); |
| 1751 | SET_TOP(quotient); |
| 1752 | if (quotient == NULL) |
| 1753 | goto error; |
| 1754 | DISPATCH(); |
| 1755 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1756 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1757 | TARGET(INPLACE_MODULO) { |
| 1758 | PyObject *right = POP(); |
| 1759 | PyObject *left = TOP(); |
| 1760 | PyObject *mod = PyNumber_InPlaceRemainder(left, right); |
| 1761 | Py_DECREF(left); |
| 1762 | Py_DECREF(right); |
| 1763 | SET_TOP(mod); |
| 1764 | if (mod == NULL) |
| 1765 | goto error; |
| 1766 | DISPATCH(); |
| 1767 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1768 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1769 | TARGET(INPLACE_ADD) { |
| 1770 | PyObject *right = POP(); |
| 1771 | PyObject *left = TOP(); |
| 1772 | PyObject *sum; |
| 1773 | if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) { |
| 1774 | sum = unicode_concatenate(left, right, f, next_instr); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1775 | /* unicode_concatenate consumed the ref to left */ |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 1776 | } |
| 1777 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1778 | sum = PyNumber_InPlaceAdd(left, right); |
| 1779 | Py_DECREF(left); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 1780 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1781 | Py_DECREF(right); |
| 1782 | SET_TOP(sum); |
| 1783 | if (sum == NULL) |
| 1784 | goto error; |
| 1785 | DISPATCH(); |
| 1786 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1787 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1788 | TARGET(INPLACE_SUBTRACT) { |
| 1789 | PyObject *right = POP(); |
| 1790 | PyObject *left = TOP(); |
| 1791 | PyObject *diff = PyNumber_InPlaceSubtract(left, right); |
| 1792 | Py_DECREF(left); |
| 1793 | Py_DECREF(right); |
| 1794 | SET_TOP(diff); |
| 1795 | if (diff == NULL) |
| 1796 | goto error; |
| 1797 | DISPATCH(); |
| 1798 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1799 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1800 | TARGET(INPLACE_LSHIFT) { |
| 1801 | PyObject *right = POP(); |
| 1802 | PyObject *left = TOP(); |
| 1803 | PyObject *res = PyNumber_InPlaceLshift(left, right); |
| 1804 | Py_DECREF(left); |
| 1805 | Py_DECREF(right); |
| 1806 | SET_TOP(res); |
| 1807 | if (res == NULL) |
| 1808 | goto error; |
| 1809 | DISPATCH(); |
| 1810 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1811 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1812 | TARGET(INPLACE_RSHIFT) { |
| 1813 | PyObject *right = POP(); |
| 1814 | PyObject *left = TOP(); |
| 1815 | PyObject *res = PyNumber_InPlaceRshift(left, right); |
| 1816 | Py_DECREF(left); |
| 1817 | Py_DECREF(right); |
| 1818 | SET_TOP(res); |
| 1819 | if (res == NULL) |
| 1820 | goto error; |
| 1821 | DISPATCH(); |
| 1822 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1823 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1824 | TARGET(INPLACE_AND) { |
| 1825 | PyObject *right = POP(); |
| 1826 | PyObject *left = TOP(); |
| 1827 | PyObject *res = PyNumber_InPlaceAnd(left, right); |
| 1828 | Py_DECREF(left); |
| 1829 | Py_DECREF(right); |
| 1830 | SET_TOP(res); |
| 1831 | if (res == NULL) |
| 1832 | goto error; |
| 1833 | DISPATCH(); |
| 1834 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1835 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1836 | TARGET(INPLACE_XOR) { |
| 1837 | PyObject *right = POP(); |
| 1838 | PyObject *left = TOP(); |
| 1839 | PyObject *res = PyNumber_InPlaceXor(left, right); |
| 1840 | Py_DECREF(left); |
| 1841 | Py_DECREF(right); |
| 1842 | SET_TOP(res); |
| 1843 | if (res == NULL) |
| 1844 | goto error; |
| 1845 | DISPATCH(); |
| 1846 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1847 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1848 | TARGET(INPLACE_OR) { |
| 1849 | PyObject *right = POP(); |
| 1850 | PyObject *left = TOP(); |
| 1851 | PyObject *res = PyNumber_InPlaceOr(left, right); |
| 1852 | Py_DECREF(left); |
| 1853 | Py_DECREF(right); |
| 1854 | SET_TOP(res); |
| 1855 | if (res == NULL) |
| 1856 | goto error; |
| 1857 | DISPATCH(); |
| 1858 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1859 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1860 | TARGET(STORE_SUBSCR) { |
| 1861 | PyObject *sub = TOP(); |
| 1862 | PyObject *container = SECOND(); |
| 1863 | PyObject *v = THIRD(); |
| 1864 | int err; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1865 | STACKADJ(-3); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1866 | /* container[sub] = v */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1867 | err = PyObject_SetItem(container, sub, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1868 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1869 | Py_DECREF(container); |
| 1870 | Py_DECREF(sub); |
| 1871 | if (err != 0) |
| 1872 | goto error; |
| 1873 | DISPATCH(); |
| 1874 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1875 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1876 | TARGET(STORE_ANNOTATION) { |
| 1877 | _Py_IDENTIFIER(__annotations__); |
| 1878 | PyObject *ann_dict; |
| 1879 | PyObject *ann = POP(); |
| 1880 | PyObject *name = GETITEM(names, oparg); |
| 1881 | int err; |
| 1882 | if (f->f_locals == NULL) { |
| 1883 | PyErr_Format(PyExc_SystemError, |
| 1884 | "no locals found when storing annotation"); |
| 1885 | Py_DECREF(ann); |
| 1886 | goto error; |
| 1887 | } |
| 1888 | /* first try to get __annotations__ from locals... */ |
| 1889 | if (PyDict_CheckExact(f->f_locals)) { |
| 1890 | ann_dict = _PyDict_GetItemId(f->f_locals, |
| 1891 | &PyId___annotations__); |
| 1892 | if (ann_dict == NULL) { |
| 1893 | PyErr_SetString(PyExc_NameError, |
| 1894 | "__annotations__ not found"); |
| 1895 | Py_DECREF(ann); |
| 1896 | goto error; |
| 1897 | } |
| 1898 | Py_INCREF(ann_dict); |
| 1899 | } |
| 1900 | else { |
| 1901 | PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__); |
| 1902 | if (ann_str == NULL) { |
| 1903 | Py_DECREF(ann); |
| 1904 | goto error; |
| 1905 | } |
| 1906 | ann_dict = PyObject_GetItem(f->f_locals, ann_str); |
| 1907 | if (ann_dict == NULL) { |
| 1908 | if (PyErr_ExceptionMatches(PyExc_KeyError)) { |
| 1909 | PyErr_SetString(PyExc_NameError, |
| 1910 | "__annotations__ not found"); |
| 1911 | } |
| 1912 | Py_DECREF(ann); |
| 1913 | goto error; |
| 1914 | } |
| 1915 | } |
| 1916 | /* ...if succeeded, __annotations__[name] = ann */ |
| 1917 | if (PyDict_CheckExact(ann_dict)) { |
| 1918 | err = PyDict_SetItem(ann_dict, name, ann); |
| 1919 | } |
| 1920 | else { |
| 1921 | err = PyObject_SetItem(ann_dict, name, ann); |
| 1922 | } |
| 1923 | Py_DECREF(ann_dict); |
| 1924 | if (err != 0) { |
| 1925 | Py_DECREF(ann); |
| 1926 | goto error; |
| 1927 | } |
| 1928 | Py_DECREF(ann); |
| 1929 | DISPATCH(); |
| 1930 | } |
| 1931 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1932 | TARGET(DELETE_SUBSCR) { |
| 1933 | PyObject *sub = TOP(); |
| 1934 | PyObject *container = SECOND(); |
| 1935 | int err; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1936 | STACKADJ(-2); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1937 | /* del container[sub] */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1938 | err = PyObject_DelItem(container, sub); |
| 1939 | Py_DECREF(container); |
| 1940 | Py_DECREF(sub); |
| 1941 | if (err != 0) |
| 1942 | goto error; |
| 1943 | DISPATCH(); |
| 1944 | } |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1945 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1946 | TARGET(PRINT_EXPR) { |
Victor Stinner | cab75e3 | 2013-11-06 22:38:37 +0100 | [diff] [blame] | 1947 | _Py_IDENTIFIER(displayhook); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1948 | PyObject *value = POP(); |
Victor Stinner | cab75e3 | 2013-11-06 22:38:37 +0100 | [diff] [blame] | 1949 | PyObject *hook = _PySys_GetObjectId(&PyId_displayhook); |
Benjamin Peterson | fe1bcb6 | 2012-10-12 11:40:01 -0400 | [diff] [blame] | 1950 | PyObject *res; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1951 | if (hook == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1952 | PyErr_SetString(PyExc_RuntimeError, |
| 1953 | "lost sys.displayhook"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1954 | Py_DECREF(value); |
| 1955 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1956 | } |
Benjamin Peterson | fe1bcb6 | 2012-10-12 11:40:01 -0400 | [diff] [blame] | 1957 | res = PyObject_CallFunctionObjArgs(hook, value, NULL); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1958 | Py_DECREF(value); |
| 1959 | if (res == NULL) |
| 1960 | goto error; |
| 1961 | Py_DECREF(res); |
| 1962 | DISPATCH(); |
| 1963 | } |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 1964 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1965 | #ifdef CASE_TOO_BIG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1966 | default: switch (opcode) { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1967 | #endif |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1968 | TARGET(RAISE_VARARGS) { |
| 1969 | PyObject *cause = NULL, *exc = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1970 | switch (oparg) { |
| 1971 | case 2: |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1972 | cause = POP(); /* cause */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1973 | case 1: |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1974 | exc = POP(); /* exc */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1975 | case 0: /* Fallthrough */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1976 | if (do_raise(exc, cause)) { |
| 1977 | why = WHY_EXCEPTION; |
| 1978 | goto fast_block_end; |
| 1979 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1980 | break; |
| 1981 | default: |
| 1982 | PyErr_SetString(PyExc_SystemError, |
| 1983 | "bad RAISE_VARARGS oparg"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1984 | break; |
| 1985 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1986 | goto error; |
| 1987 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1988 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1989 | TARGET(RETURN_VALUE) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1990 | retval = POP(); |
| 1991 | why = WHY_RETURN; |
| 1992 | goto fast_block_end; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1993 | } |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 1994 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1995 | TARGET(GET_AITER) { |
Yury Selivanov | 6ef0590 | 2015-05-28 11:21:31 -0400 | [diff] [blame] | 1996 | unaryfunc getter = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1997 | PyObject *iter = NULL; |
| 1998 | PyObject *awaitable = NULL; |
| 1999 | PyObject *obj = TOP(); |
| 2000 | PyTypeObject *type = Py_TYPE(obj); |
| 2001 | |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 2002 | if (type->tp_as_async != NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2003 | getter = type->tp_as_async->am_aiter; |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 2004 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2005 | |
| 2006 | if (getter != NULL) { |
| 2007 | iter = (*getter)(obj); |
| 2008 | Py_DECREF(obj); |
| 2009 | if (iter == NULL) { |
| 2010 | SET_TOP(NULL); |
| 2011 | goto error; |
| 2012 | } |
| 2013 | } |
| 2014 | else { |
| 2015 | SET_TOP(NULL); |
| 2016 | PyErr_Format( |
| 2017 | PyExc_TypeError, |
| 2018 | "'async for' requires an object with " |
| 2019 | "__aiter__ method, got %.100s", |
| 2020 | type->tp_name); |
| 2021 | Py_DECREF(obj); |
| 2022 | goto error; |
| 2023 | } |
| 2024 | |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 2025 | if (Py_TYPE(iter)->tp_as_async != NULL && |
| 2026 | Py_TYPE(iter)->tp_as_async->am_anext != NULL) { |
| 2027 | |
| 2028 | /* Starting with CPython 3.5.2 __aiter__ should return |
| 2029 | asynchronous iterators directly (not awaitables that |
| 2030 | resolve to asynchronous iterators.) |
| 2031 | |
| 2032 | Therefore, we check if the object that was returned |
| 2033 | from __aiter__ has an __anext__ method. If it does, |
| 2034 | we wrap it in an awaitable that resolves to `iter`. |
| 2035 | |
| 2036 | See http://bugs.python.org/issue27243 for more |
| 2037 | details. |
| 2038 | */ |
| 2039 | |
| 2040 | PyObject *wrapper = _PyAIterWrapper_New(iter); |
| 2041 | Py_DECREF(iter); |
| 2042 | SET_TOP(wrapper); |
| 2043 | DISPATCH(); |
| 2044 | } |
| 2045 | |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 2046 | awaitable = _PyCoro_GetAwaitableIter(iter); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2047 | if (awaitable == NULL) { |
| 2048 | SET_TOP(NULL); |
| 2049 | PyErr_Format( |
| 2050 | PyExc_TypeError, |
| 2051 | "'async for' received an invalid object " |
| 2052 | "from __aiter__: %.100s", |
| 2053 | Py_TYPE(iter)->tp_name); |
| 2054 | |
| 2055 | Py_DECREF(iter); |
| 2056 | goto error; |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 2057 | } else { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2058 | Py_DECREF(iter); |
| 2059 | |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 2060 | if (PyErr_WarnFormat( |
| 2061 | PyExc_PendingDeprecationWarning, 1, |
| 2062 | "'%.100s' implements legacy __aiter__ protocol; " |
| 2063 | "__aiter__ should return an asynchronous " |
| 2064 | "iterator, not awaitable", |
| 2065 | type->tp_name)) |
| 2066 | { |
| 2067 | /* Warning was converted to an error. */ |
| 2068 | Py_DECREF(awaitable); |
| 2069 | SET_TOP(NULL); |
| 2070 | goto error; |
| 2071 | } |
| 2072 | } |
| 2073 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2074 | SET_TOP(awaitable); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 2075 | PREDICT(LOAD_CONST); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2076 | DISPATCH(); |
| 2077 | } |
| 2078 | |
| 2079 | TARGET(GET_ANEXT) { |
Yury Selivanov | 6ef0590 | 2015-05-28 11:21:31 -0400 | [diff] [blame] | 2080 | unaryfunc getter = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2081 | PyObject *next_iter = NULL; |
| 2082 | PyObject *awaitable = NULL; |
| 2083 | PyObject *aiter = TOP(); |
| 2084 | PyTypeObject *type = Py_TYPE(aiter); |
| 2085 | |
| 2086 | if (type->tp_as_async != NULL) |
| 2087 | getter = type->tp_as_async->am_anext; |
| 2088 | |
| 2089 | if (getter != NULL) { |
| 2090 | next_iter = (*getter)(aiter); |
| 2091 | if (next_iter == NULL) { |
| 2092 | goto error; |
| 2093 | } |
| 2094 | } |
| 2095 | else { |
| 2096 | PyErr_Format( |
| 2097 | PyExc_TypeError, |
| 2098 | "'async for' requires an iterator with " |
| 2099 | "__anext__ method, got %.100s", |
| 2100 | type->tp_name); |
| 2101 | goto error; |
| 2102 | } |
| 2103 | |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 2104 | awaitable = _PyCoro_GetAwaitableIter(next_iter); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2105 | if (awaitable == NULL) { |
| 2106 | PyErr_Format( |
| 2107 | PyExc_TypeError, |
| 2108 | "'async for' received an invalid object " |
| 2109 | "from __anext__: %.100s", |
| 2110 | Py_TYPE(next_iter)->tp_name); |
| 2111 | |
| 2112 | Py_DECREF(next_iter); |
| 2113 | goto error; |
| 2114 | } else |
| 2115 | Py_DECREF(next_iter); |
| 2116 | |
| 2117 | PUSH(awaitable); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 2118 | PREDICT(LOAD_CONST); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2119 | DISPATCH(); |
| 2120 | } |
| 2121 | |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 2122 | PREDICTED(GET_AWAITABLE); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2123 | TARGET(GET_AWAITABLE) { |
| 2124 | PyObject *iterable = TOP(); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 2125 | PyObject *iter = _PyCoro_GetAwaitableIter(iterable); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2126 | |
| 2127 | Py_DECREF(iterable); |
| 2128 | |
Yury Selivanov | c724bae | 2016-03-02 11:30:46 -0500 | [diff] [blame] | 2129 | if (iter != NULL && PyCoro_CheckExact(iter)) { |
| 2130 | PyObject *yf = _PyGen_yf((PyGenObject*)iter); |
| 2131 | if (yf != NULL) { |
| 2132 | /* `iter` is a coroutine object that is being |
| 2133 | awaited, `yf` is a pointer to the current awaitable |
| 2134 | being awaited on. */ |
| 2135 | Py_DECREF(yf); |
| 2136 | Py_CLEAR(iter); |
| 2137 | PyErr_SetString( |
| 2138 | PyExc_RuntimeError, |
| 2139 | "coroutine is being awaited already"); |
| 2140 | /* The code below jumps to `error` if `iter` is NULL. */ |
| 2141 | } |
| 2142 | } |
| 2143 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2144 | SET_TOP(iter); /* Even if it's NULL */ |
| 2145 | |
| 2146 | if (iter == NULL) { |
| 2147 | goto error; |
| 2148 | } |
| 2149 | |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 2150 | PREDICT(LOAD_CONST); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2151 | DISPATCH(); |
| 2152 | } |
| 2153 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2154 | TARGET(YIELD_FROM) { |
| 2155 | PyObject *v = POP(); |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 2156 | PyObject *receiver = TOP(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2157 | int err; |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 2158 | if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) { |
| 2159 | retval = _PyGen_Send((PyGenObject *)receiver, v); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 2160 | } else { |
Benjamin Peterson | 302e790 | 2012-03-20 23:17:04 -0400 | [diff] [blame] | 2161 | _Py_IDENTIFIER(send); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2162 | if (v == Py_None) |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 2163 | retval = Py_TYPE(receiver)->tp_iternext(receiver); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 2164 | else |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 2165 | retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 2166 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2167 | Py_DECREF(v); |
| 2168 | if (retval == NULL) { |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 2169 | PyObject *val; |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 2170 | if (tstate->c_tracefunc != NULL |
| 2171 | && PyErr_ExceptionMatches(PyExc_StopIteration)) |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 2172 | call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f); |
Nick Coghlan | c40bc09 | 2012-06-17 15:15:49 +1000 | [diff] [blame] | 2173 | err = _PyGen_FetchStopIterationValue(&val); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2174 | if (err < 0) |
| 2175 | goto error; |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 2176 | Py_DECREF(receiver); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2177 | SET_TOP(val); |
| 2178 | DISPATCH(); |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2179 | } |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 2180 | /* receiver remains on stack, retval is value to be yielded */ |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2181 | f->f_stacktop = stack_pointer; |
| 2182 | why = WHY_YIELD; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 2183 | /* and repeat... */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 2184 | f->f_lasti -= 2; |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2185 | goto fast_yield; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2186 | } |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2187 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2188 | TARGET(YIELD_VALUE) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2189 | retval = POP(); |
| 2190 | f->f_stacktop = stack_pointer; |
| 2191 | why = WHY_YIELD; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2192 | goto fast_yield; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2193 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2194 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2195 | TARGET(POP_EXCEPT) { |
| 2196 | PyTryBlock *b = PyFrame_BlockPop(f); |
| 2197 | if (b->b_type != EXCEPT_HANDLER) { |
| 2198 | PyErr_SetString(PyExc_SystemError, |
| 2199 | "popped block is not an except handler"); |
| 2200 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2201 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2202 | UNWIND_EXCEPT_HANDLER(b); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2203 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2204 | } |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 2205 | |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 2206 | PREDICTED(POP_BLOCK); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2207 | TARGET(POP_BLOCK) { |
| 2208 | PyTryBlock *b = PyFrame_BlockPop(f); |
| 2209 | UNWIND_BLOCK(b); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2210 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2211 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2212 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2213 | PREDICTED(END_FINALLY); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2214 | TARGET(END_FINALLY) { |
| 2215 | PyObject *status = POP(); |
| 2216 | if (PyLong_Check(status)) { |
| 2217 | why = (enum why_code) PyLong_AS_LONG(status); |
| 2218 | assert(why != WHY_YIELD && why != WHY_EXCEPTION); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2219 | if (why == WHY_RETURN || |
| 2220 | why == WHY_CONTINUE) |
| 2221 | retval = POP(); |
| 2222 | if (why == WHY_SILENCED) { |
| 2223 | /* An exception was silenced by 'with', we must |
| 2224 | manually unwind the EXCEPT_HANDLER block which was |
| 2225 | created when the exception was caught, otherwise |
| 2226 | the stack will be in an inconsistent state. */ |
| 2227 | PyTryBlock *b = PyFrame_BlockPop(f); |
| 2228 | assert(b->b_type == EXCEPT_HANDLER); |
| 2229 | UNWIND_EXCEPT_HANDLER(b); |
| 2230 | why = WHY_NOT; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2231 | Py_DECREF(status); |
| 2232 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2233 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2234 | Py_DECREF(status); |
| 2235 | goto fast_block_end; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2236 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2237 | else if (PyExceptionClass_Check(status)) { |
| 2238 | PyObject *exc = POP(); |
| 2239 | PyObject *tb = POP(); |
| 2240 | PyErr_Restore(status, exc, tb); |
| 2241 | why = WHY_EXCEPTION; |
| 2242 | goto fast_block_end; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2243 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2244 | else if (status != Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2245 | PyErr_SetString(PyExc_SystemError, |
| 2246 | "'finally' pops bad exception"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2247 | Py_DECREF(status); |
| 2248 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2249 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2250 | Py_DECREF(status); |
| 2251 | DISPATCH(); |
| 2252 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2253 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2254 | TARGET(LOAD_BUILD_CLASS) { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2255 | _Py_IDENTIFIER(__build_class__); |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2256 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2257 | PyObject *bc; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2258 | if (PyDict_CheckExact(f->f_builtins)) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2259 | bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__); |
| 2260 | if (bc == NULL) { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2261 | PyErr_SetString(PyExc_NameError, |
| 2262 | "__build_class__ not found"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2263 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2264 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2265 | Py_INCREF(bc); |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2266 | } |
| 2267 | else { |
| 2268 | PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__); |
| 2269 | if (build_class_str == NULL) |
| 2270 | break; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2271 | bc = PyObject_GetItem(f->f_builtins, build_class_str); |
| 2272 | if (bc == NULL) { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2273 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2274 | PyErr_SetString(PyExc_NameError, |
| 2275 | "__build_class__ not found"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2276 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2277 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2278 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2279 | PUSH(bc); |
Benjamin Peterson | 00f86f2 | 2012-10-10 14:10:33 -0400 | [diff] [blame] | 2280 | DISPATCH(); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2281 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2282 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2283 | TARGET(STORE_NAME) { |
| 2284 | PyObject *name = GETITEM(names, oparg); |
| 2285 | PyObject *v = POP(); |
| 2286 | PyObject *ns = f->f_locals; |
| 2287 | int err; |
| 2288 | if (ns == NULL) { |
| 2289 | PyErr_Format(PyExc_SystemError, |
| 2290 | "no locals found when storing %R", name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2291 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2292 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2293 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2294 | if (PyDict_CheckExact(ns)) |
| 2295 | err = PyDict_SetItem(ns, name, v); |
| 2296 | else |
| 2297 | err = PyObject_SetItem(ns, name, v); |
| 2298 | Py_DECREF(v); |
| 2299 | if (err != 0) |
| 2300 | goto error; |
| 2301 | DISPATCH(); |
| 2302 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2303 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2304 | TARGET(DELETE_NAME) { |
| 2305 | PyObject *name = GETITEM(names, oparg); |
| 2306 | PyObject *ns = f->f_locals; |
| 2307 | int err; |
| 2308 | if (ns == NULL) { |
| 2309 | PyErr_Format(PyExc_SystemError, |
| 2310 | "no locals when deleting %R", name); |
| 2311 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2312 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2313 | err = PyObject_DelItem(ns, name); |
| 2314 | if (err != 0) { |
| 2315 | format_exc_check_arg(PyExc_NameError, |
| 2316 | NAME_ERROR_MSG, |
| 2317 | name); |
| 2318 | goto error; |
| 2319 | } |
| 2320 | DISPATCH(); |
| 2321 | } |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 2322 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 2323 | PREDICTED(UNPACK_SEQUENCE); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2324 | TARGET(UNPACK_SEQUENCE) { |
| 2325 | PyObject *seq = POP(), *item, **items; |
| 2326 | if (PyTuple_CheckExact(seq) && |
| 2327 | PyTuple_GET_SIZE(seq) == oparg) { |
| 2328 | items = ((PyTupleObject *)seq)->ob_item; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2329 | while (oparg--) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2330 | item = items[oparg]; |
| 2331 | Py_INCREF(item); |
| 2332 | PUSH(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2333 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2334 | } else if (PyList_CheckExact(seq) && |
| 2335 | PyList_GET_SIZE(seq) == oparg) { |
| 2336 | items = ((PyListObject *)seq)->ob_item; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2337 | while (oparg--) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2338 | item = items[oparg]; |
| 2339 | Py_INCREF(item); |
| 2340 | PUSH(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2341 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2342 | } else if (unpack_iterable(seq, oparg, -1, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2343 | stack_pointer + oparg)) { |
| 2344 | STACKADJ(oparg); |
| 2345 | } else { |
| 2346 | /* unpack_iterable() raised an exception */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2347 | Py_DECREF(seq); |
| 2348 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2349 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2350 | Py_DECREF(seq); |
Benjamin Peterson | 00f86f2 | 2012-10-10 14:10:33 -0400 | [diff] [blame] | 2351 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2352 | } |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 2353 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2354 | TARGET(UNPACK_EX) { |
| 2355 | int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8); |
| 2356 | PyObject *seq = POP(); |
| 2357 | |
| 2358 | if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8, |
| 2359 | stack_pointer + totalargs)) { |
| 2360 | stack_pointer += totalargs; |
| 2361 | } else { |
| 2362 | Py_DECREF(seq); |
| 2363 | goto error; |
| 2364 | } |
| 2365 | Py_DECREF(seq); |
| 2366 | DISPATCH(); |
| 2367 | } |
| 2368 | |
| 2369 | TARGET(STORE_ATTR) { |
| 2370 | PyObject *name = GETITEM(names, oparg); |
| 2371 | PyObject *owner = TOP(); |
| 2372 | PyObject *v = SECOND(); |
| 2373 | int err; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2374 | STACKADJ(-2); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2375 | err = PyObject_SetAttr(owner, name, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2376 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2377 | Py_DECREF(owner); |
| 2378 | if (err != 0) |
| 2379 | goto error; |
| 2380 | DISPATCH(); |
| 2381 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2382 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2383 | TARGET(DELETE_ATTR) { |
| 2384 | PyObject *name = GETITEM(names, oparg); |
| 2385 | PyObject *owner = POP(); |
| 2386 | int err; |
| 2387 | err = PyObject_SetAttr(owner, name, (PyObject *)NULL); |
| 2388 | Py_DECREF(owner); |
| 2389 | if (err != 0) |
| 2390 | goto error; |
| 2391 | DISPATCH(); |
| 2392 | } |
| 2393 | |
| 2394 | TARGET(STORE_GLOBAL) { |
| 2395 | PyObject *name = GETITEM(names, oparg); |
| 2396 | PyObject *v = POP(); |
| 2397 | int err; |
| 2398 | err = PyDict_SetItem(f->f_globals, name, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2399 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2400 | if (err != 0) |
| 2401 | goto error; |
| 2402 | DISPATCH(); |
| 2403 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2404 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2405 | TARGET(DELETE_GLOBAL) { |
| 2406 | PyObject *name = GETITEM(names, oparg); |
| 2407 | int err; |
| 2408 | err = PyDict_DelItem(f->f_globals, name); |
| 2409 | if (err != 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2410 | format_exc_check_arg( |
Ezio Melotti | 04a2955 | 2013-03-03 15:12:44 +0200 | [diff] [blame] | 2411 | PyExc_NameError, NAME_ERROR_MSG, name); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2412 | goto error; |
Benjamin Peterson | 00f86f2 | 2012-10-10 14:10:33 -0400 | [diff] [blame] | 2413 | } |
| 2414 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2415 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2416 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2417 | TARGET(LOAD_NAME) { |
| 2418 | PyObject *name = GETITEM(names, oparg); |
| 2419 | PyObject *locals = f->f_locals; |
| 2420 | PyObject *v; |
| 2421 | if (locals == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2422 | PyErr_Format(PyExc_SystemError, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2423 | "no locals when loading %R", name); |
| 2424 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2425 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2426 | if (PyDict_CheckExact(locals)) { |
| 2427 | v = PyDict_GetItem(locals, name); |
| 2428 | Py_XINCREF(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2429 | } |
| 2430 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2431 | v = PyObject_GetItem(locals, name); |
Victor Stinner | e20310f | 2015-11-05 13:56:58 +0100 | [diff] [blame] | 2432 | if (v == NULL) { |
Benjamin Peterson | 9272279 | 2012-12-15 12:51:05 -0500 | [diff] [blame] | 2433 | if (!PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2434 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2435 | PyErr_Clear(); |
| 2436 | } |
| 2437 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2438 | if (v == NULL) { |
| 2439 | v = PyDict_GetItem(f->f_globals, name); |
| 2440 | Py_XINCREF(v); |
| 2441 | if (v == NULL) { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2442 | if (PyDict_CheckExact(f->f_builtins)) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2443 | v = PyDict_GetItem(f->f_builtins, name); |
| 2444 | if (v == NULL) { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2445 | format_exc_check_arg( |
| 2446 | PyExc_NameError, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2447 | NAME_ERROR_MSG, name); |
| 2448 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2449 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2450 | Py_INCREF(v); |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2451 | } |
| 2452 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2453 | v = PyObject_GetItem(f->f_builtins, name); |
| 2454 | if (v == NULL) { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2455 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2456 | format_exc_check_arg( |
| 2457 | PyExc_NameError, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2458 | NAME_ERROR_MSG, name); |
| 2459 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2460 | } |
Benjamin Peterson | 20f9c3c | 2010-07-20 22:39:34 +0000 | [diff] [blame] | 2461 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2462 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2463 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2464 | PUSH(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2465 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2466 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2467 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2468 | TARGET(LOAD_GLOBAL) { |
| 2469 | PyObject *name = GETITEM(names, oparg); |
| 2470 | PyObject *v; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2471 | if (PyDict_CheckExact(f->f_globals) |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2472 | && PyDict_CheckExact(f->f_builtins)) |
| 2473 | { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2474 | v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals, |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2475 | (PyDictObject *)f->f_builtins, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2476 | name); |
| 2477 | if (v == NULL) { |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2478 | if (!_PyErr_OCCURRED()) { |
| 2479 | /* _PyDict_LoadGlobal() returns NULL without raising |
| 2480 | * an exception if the key doesn't exist */ |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2481 | format_exc_check_arg(PyExc_NameError, |
Ezio Melotti | 04a2955 | 2013-03-03 15:12:44 +0200 | [diff] [blame] | 2482 | NAME_ERROR_MSG, name); |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2483 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2484 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2485 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2486 | Py_INCREF(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2487 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2488 | else { |
| 2489 | /* Slow-path if globals or builtins is not a dict */ |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2490 | |
| 2491 | /* namespace 1: globals */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2492 | v = PyObject_GetItem(f->f_globals, name); |
| 2493 | if (v == NULL) { |
Victor Stinner | 60a1d3c | 2015-11-05 13:55:20 +0100 | [diff] [blame] | 2494 | if (!PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2495 | goto error; |
| 2496 | PyErr_Clear(); |
| 2497 | |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2498 | /* namespace 2: builtins */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2499 | v = PyObject_GetItem(f->f_builtins, name); |
| 2500 | if (v == NULL) { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2501 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2502 | format_exc_check_arg( |
| 2503 | PyExc_NameError, |
Ezio Melotti | 04a2955 | 2013-03-03 15:12:44 +0200 | [diff] [blame] | 2504 | NAME_ERROR_MSG, name); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2505 | goto error; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2506 | } |
| 2507 | } |
| 2508 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2509 | PUSH(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2510 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2511 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2512 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2513 | TARGET(DELETE_FAST) { |
| 2514 | PyObject *v = GETLOCAL(oparg); |
| 2515 | if (v != NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2516 | SETLOCAL(oparg, NULL); |
| 2517 | DISPATCH(); |
| 2518 | } |
| 2519 | format_exc_check_arg( |
| 2520 | PyExc_UnboundLocalError, |
| 2521 | UNBOUNDLOCAL_ERROR_MSG, |
| 2522 | PyTuple_GetItem(co->co_varnames, oparg) |
| 2523 | ); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2524 | goto error; |
| 2525 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2526 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2527 | TARGET(DELETE_DEREF) { |
| 2528 | PyObject *cell = freevars[oparg]; |
| 2529 | if (PyCell_GET(cell) != NULL) { |
| 2530 | PyCell_Set(cell, NULL); |
Benjamin Peterson | 00ebe2c | 2010-09-10 22:02:31 +0000 | [diff] [blame] | 2531 | DISPATCH(); |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 2532 | } |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 2533 | format_exc_unbound(co, oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2534 | goto error; |
| 2535 | } |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 2536 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2537 | TARGET(LOAD_CLOSURE) { |
| 2538 | PyObject *cell = freevars[oparg]; |
| 2539 | Py_INCREF(cell); |
| 2540 | PUSH(cell); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2541 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2542 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2543 | |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2544 | TARGET(LOAD_CLASSDEREF) { |
| 2545 | PyObject *name, *value, *locals = f->f_locals; |
Victor Stinner | d3dfd0e | 2013-05-16 23:48:01 +0200 | [diff] [blame] | 2546 | Py_ssize_t idx; |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2547 | assert(locals); |
| 2548 | assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars)); |
| 2549 | idx = oparg - PyTuple_GET_SIZE(co->co_cellvars); |
| 2550 | assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars)); |
| 2551 | name = PyTuple_GET_ITEM(co->co_freevars, idx); |
| 2552 | if (PyDict_CheckExact(locals)) { |
| 2553 | value = PyDict_GetItem(locals, name); |
| 2554 | Py_XINCREF(value); |
| 2555 | } |
| 2556 | else { |
| 2557 | value = PyObject_GetItem(locals, name); |
Victor Stinner | e20310f | 2015-11-05 13:56:58 +0100 | [diff] [blame] | 2558 | if (value == NULL) { |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2559 | if (!PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2560 | goto error; |
| 2561 | PyErr_Clear(); |
| 2562 | } |
| 2563 | } |
| 2564 | if (!value) { |
| 2565 | PyObject *cell = freevars[oparg]; |
| 2566 | value = PyCell_GET(cell); |
| 2567 | if (value == NULL) { |
| 2568 | format_exc_unbound(co, oparg); |
| 2569 | goto error; |
| 2570 | } |
| 2571 | Py_INCREF(value); |
| 2572 | } |
| 2573 | PUSH(value); |
| 2574 | DISPATCH(); |
| 2575 | } |
| 2576 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2577 | TARGET(LOAD_DEREF) { |
| 2578 | PyObject *cell = freevars[oparg]; |
| 2579 | PyObject *value = PyCell_GET(cell); |
| 2580 | if (value == NULL) { |
| 2581 | format_exc_unbound(co, oparg); |
| 2582 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2583 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2584 | Py_INCREF(value); |
| 2585 | PUSH(value); |
| 2586 | DISPATCH(); |
| 2587 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2588 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2589 | TARGET(STORE_DEREF) { |
| 2590 | PyObject *v = POP(); |
| 2591 | PyObject *cell = freevars[oparg]; |
| 2592 | PyCell_Set(cell, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2593 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2594 | DISPATCH(); |
| 2595 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2596 | |
Serhiy Storchaka | ea525a2 | 2016-09-06 22:07:53 +0300 | [diff] [blame] | 2597 | TARGET(BUILD_STRING) { |
| 2598 | PyObject *str; |
| 2599 | PyObject *empty = PyUnicode_New(0, 0); |
| 2600 | if (empty == NULL) { |
| 2601 | goto error; |
| 2602 | } |
| 2603 | str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg); |
| 2604 | Py_DECREF(empty); |
| 2605 | if (str == NULL) |
| 2606 | goto error; |
| 2607 | while (--oparg >= 0) { |
| 2608 | PyObject *item = POP(); |
| 2609 | Py_DECREF(item); |
| 2610 | } |
| 2611 | PUSH(str); |
| 2612 | DISPATCH(); |
| 2613 | } |
| 2614 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2615 | TARGET(BUILD_TUPLE) { |
| 2616 | PyObject *tup = PyTuple_New(oparg); |
| 2617 | if (tup == NULL) |
| 2618 | goto error; |
| 2619 | while (--oparg >= 0) { |
| 2620 | PyObject *item = POP(); |
| 2621 | PyTuple_SET_ITEM(tup, oparg, item); |
| 2622 | } |
| 2623 | PUSH(tup); |
| 2624 | DISPATCH(); |
| 2625 | } |
| 2626 | |
| 2627 | TARGET(BUILD_LIST) { |
| 2628 | PyObject *list = PyList_New(oparg); |
| 2629 | if (list == NULL) |
| 2630 | goto error; |
| 2631 | while (--oparg >= 0) { |
| 2632 | PyObject *item = POP(); |
| 2633 | PyList_SET_ITEM(list, oparg, item); |
| 2634 | } |
| 2635 | PUSH(list); |
| 2636 | DISPATCH(); |
| 2637 | } |
| 2638 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 2639 | TARGET(BUILD_TUPLE_UNPACK) |
| 2640 | TARGET(BUILD_LIST_UNPACK) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2641 | int convert_to_tuple = opcode == BUILD_TUPLE_UNPACK; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2642 | Py_ssize_t i; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2643 | PyObject *sum = PyList_New(0); |
| 2644 | PyObject *return_value; |
| 2645 | if (sum == NULL) |
| 2646 | goto error; |
| 2647 | |
| 2648 | for (i = oparg; i > 0; i--) { |
| 2649 | PyObject *none_val; |
| 2650 | |
| 2651 | none_val = _PyList_Extend((PyListObject *)sum, PEEK(i)); |
| 2652 | if (none_val == NULL) { |
| 2653 | Py_DECREF(sum); |
| 2654 | goto error; |
| 2655 | } |
| 2656 | Py_DECREF(none_val); |
| 2657 | } |
| 2658 | |
| 2659 | if (convert_to_tuple) { |
| 2660 | return_value = PyList_AsTuple(sum); |
| 2661 | Py_DECREF(sum); |
| 2662 | if (return_value == NULL) |
| 2663 | goto error; |
| 2664 | } |
| 2665 | else { |
| 2666 | return_value = sum; |
| 2667 | } |
| 2668 | |
| 2669 | while (oparg--) |
| 2670 | Py_DECREF(POP()); |
| 2671 | PUSH(return_value); |
| 2672 | DISPATCH(); |
| 2673 | } |
| 2674 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2675 | TARGET(BUILD_SET) { |
| 2676 | PyObject *set = PySet_New(NULL); |
| 2677 | int err = 0; |
Raymond Hettinger | 4c483ad | 2016-09-08 14:45:40 -0700 | [diff] [blame] | 2678 | int i; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2679 | if (set == NULL) |
| 2680 | goto error; |
Raymond Hettinger | 4c483ad | 2016-09-08 14:45:40 -0700 | [diff] [blame] | 2681 | for (i = oparg; i > 0; i--) { |
| 2682 | PyObject *item = PEEK(i); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2683 | if (err == 0) |
| 2684 | err = PySet_Add(set, item); |
| 2685 | Py_DECREF(item); |
| 2686 | } |
Raymond Hettinger | 4c483ad | 2016-09-08 14:45:40 -0700 | [diff] [blame] | 2687 | STACKADJ(-oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2688 | if (err != 0) { |
| 2689 | Py_DECREF(set); |
| 2690 | goto error; |
| 2691 | } |
| 2692 | PUSH(set); |
| 2693 | DISPATCH(); |
| 2694 | } |
| 2695 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2696 | TARGET(BUILD_SET_UNPACK) { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2697 | Py_ssize_t i; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2698 | PyObject *sum = PySet_New(NULL); |
| 2699 | if (sum == NULL) |
| 2700 | goto error; |
| 2701 | |
| 2702 | for (i = oparg; i > 0; i--) { |
| 2703 | if (_PySet_Update(sum, PEEK(i)) < 0) { |
| 2704 | Py_DECREF(sum); |
| 2705 | goto error; |
| 2706 | } |
| 2707 | } |
| 2708 | |
| 2709 | while (oparg--) |
| 2710 | Py_DECREF(POP()); |
| 2711 | PUSH(sum); |
| 2712 | DISPATCH(); |
| 2713 | } |
| 2714 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2715 | TARGET(BUILD_MAP) { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2716 | Py_ssize_t i; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2717 | PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg); |
| 2718 | if (map == NULL) |
| 2719 | goto error; |
Benjamin Peterson | d5d77aa | 2015-07-05 10:37:25 -0500 | [diff] [blame] | 2720 | for (i = oparg; i > 0; i--) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2721 | int err; |
Benjamin Peterson | d5d77aa | 2015-07-05 10:37:25 -0500 | [diff] [blame] | 2722 | PyObject *key = PEEK(2*i); |
| 2723 | PyObject *value = PEEK(2*i - 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2724 | err = PyDict_SetItem(map, key, value); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2725 | if (err != 0) { |
| 2726 | Py_DECREF(map); |
| 2727 | goto error; |
| 2728 | } |
| 2729 | } |
Benjamin Peterson | d5d77aa | 2015-07-05 10:37:25 -0500 | [diff] [blame] | 2730 | |
| 2731 | while (oparg--) { |
| 2732 | Py_DECREF(POP()); |
| 2733 | Py_DECREF(POP()); |
| 2734 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2735 | PUSH(map); |
| 2736 | DISPATCH(); |
| 2737 | } |
| 2738 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2739 | TARGET(SETUP_ANNOTATIONS) { |
| 2740 | _Py_IDENTIFIER(__annotations__); |
| 2741 | int err; |
| 2742 | PyObject *ann_dict; |
| 2743 | if (f->f_locals == NULL) { |
| 2744 | PyErr_Format(PyExc_SystemError, |
| 2745 | "no locals found when setting up annotations"); |
| 2746 | goto error; |
| 2747 | } |
| 2748 | /* check if __annotations__ in locals()... */ |
| 2749 | if (PyDict_CheckExact(f->f_locals)) { |
| 2750 | ann_dict = _PyDict_GetItemId(f->f_locals, |
| 2751 | &PyId___annotations__); |
| 2752 | if (ann_dict == NULL) { |
| 2753 | /* ...if not, create a new one */ |
| 2754 | ann_dict = PyDict_New(); |
| 2755 | if (ann_dict == NULL) { |
| 2756 | goto error; |
| 2757 | } |
| 2758 | err = _PyDict_SetItemId(f->f_locals, |
| 2759 | &PyId___annotations__, ann_dict); |
| 2760 | Py_DECREF(ann_dict); |
| 2761 | if (err != 0) { |
| 2762 | goto error; |
| 2763 | } |
| 2764 | } |
| 2765 | } |
| 2766 | else { |
| 2767 | /* do the same if locals() is not a dict */ |
| 2768 | PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__); |
| 2769 | if (ann_str == NULL) { |
| 2770 | break; |
| 2771 | } |
| 2772 | ann_dict = PyObject_GetItem(f->f_locals, ann_str); |
| 2773 | if (ann_dict == NULL) { |
| 2774 | if (!PyErr_ExceptionMatches(PyExc_KeyError)) { |
| 2775 | goto error; |
| 2776 | } |
| 2777 | PyErr_Clear(); |
| 2778 | ann_dict = PyDict_New(); |
| 2779 | if (ann_dict == NULL) { |
| 2780 | goto error; |
| 2781 | } |
| 2782 | err = PyObject_SetItem(f->f_locals, ann_str, ann_dict); |
| 2783 | Py_DECREF(ann_dict); |
| 2784 | if (err != 0) { |
| 2785 | goto error; |
| 2786 | } |
| 2787 | } |
| 2788 | else { |
| 2789 | Py_DECREF(ann_dict); |
| 2790 | } |
| 2791 | } |
| 2792 | DISPATCH(); |
| 2793 | } |
| 2794 | |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 2795 | TARGET(BUILD_CONST_KEY_MAP) { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2796 | Py_ssize_t i; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 2797 | PyObject *map; |
| 2798 | PyObject *keys = TOP(); |
| 2799 | if (!PyTuple_CheckExact(keys) || |
| 2800 | PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) { |
| 2801 | PyErr_SetString(PyExc_SystemError, |
| 2802 | "bad BUILD_CONST_KEY_MAP keys argument"); |
| 2803 | goto error; |
| 2804 | } |
| 2805 | map = _PyDict_NewPresized((Py_ssize_t)oparg); |
| 2806 | if (map == NULL) { |
| 2807 | goto error; |
| 2808 | } |
| 2809 | for (i = oparg; i > 0; i--) { |
| 2810 | int err; |
| 2811 | PyObject *key = PyTuple_GET_ITEM(keys, oparg - i); |
| 2812 | PyObject *value = PEEK(i + 1); |
| 2813 | err = PyDict_SetItem(map, key, value); |
| 2814 | if (err != 0) { |
| 2815 | Py_DECREF(map); |
| 2816 | goto error; |
| 2817 | } |
| 2818 | } |
| 2819 | |
| 2820 | Py_DECREF(POP()); |
| 2821 | while (oparg--) { |
| 2822 | Py_DECREF(POP()); |
| 2823 | } |
| 2824 | PUSH(map); |
| 2825 | DISPATCH(); |
| 2826 | } |
| 2827 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 2828 | TARGET(BUILD_MAP_UNPACK_WITH_CALL) |
| 2829 | TARGET(BUILD_MAP_UNPACK) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2830 | int with_call = opcode == BUILD_MAP_UNPACK_WITH_CALL; |
| 2831 | int num_maps; |
| 2832 | int function_location; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2833 | Py_ssize_t i; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2834 | PyObject *sum = PyDict_New(); |
| 2835 | if (sum == NULL) |
| 2836 | goto error; |
| 2837 | if (with_call) { |
| 2838 | num_maps = oparg & 0xff; |
| 2839 | function_location = (oparg>>8) & 0xff; |
| 2840 | } |
| 2841 | else { |
| 2842 | num_maps = oparg; |
| 2843 | } |
| 2844 | |
| 2845 | for (i = num_maps; i > 0; i--) { |
| 2846 | PyObject *arg = PEEK(i); |
| 2847 | if (with_call) { |
| 2848 | PyObject *intersection = _PyDictView_Intersect(sum, arg); |
| 2849 | |
| 2850 | if (intersection == NULL) { |
| 2851 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 2852 | PyObject *func = ( |
| 2853 | PEEK(function_location + num_maps)); |
| 2854 | PyErr_Format(PyExc_TypeError, |
| 2855 | "%.200s%.200s argument after ** " |
| 2856 | "must be a mapping, not %.200s", |
| 2857 | PyEval_GetFuncName(func), |
| 2858 | PyEval_GetFuncDesc(func), |
| 2859 | arg->ob_type->tp_name); |
| 2860 | } |
| 2861 | Py_DECREF(sum); |
| 2862 | goto error; |
| 2863 | } |
| 2864 | |
| 2865 | if (PySet_GET_SIZE(intersection)) { |
| 2866 | Py_ssize_t idx = 0; |
| 2867 | PyObject *key; |
| 2868 | PyObject *func = PEEK(function_location + num_maps); |
| 2869 | Py_hash_t hash; |
| 2870 | _PySet_NextEntry(intersection, &idx, &key, &hash); |
| 2871 | if (!PyUnicode_Check(key)) { |
| 2872 | PyErr_Format(PyExc_TypeError, |
| 2873 | "%.200s%.200s keywords must be strings", |
| 2874 | PyEval_GetFuncName(func), |
| 2875 | PyEval_GetFuncDesc(func)); |
| 2876 | } else { |
| 2877 | PyErr_Format(PyExc_TypeError, |
| 2878 | "%.200s%.200s got multiple " |
| 2879 | "values for keyword argument '%U'", |
| 2880 | PyEval_GetFuncName(func), |
| 2881 | PyEval_GetFuncDesc(func), |
| 2882 | key); |
| 2883 | } |
| 2884 | Py_DECREF(intersection); |
| 2885 | Py_DECREF(sum); |
| 2886 | goto error; |
| 2887 | } |
| 2888 | Py_DECREF(intersection); |
| 2889 | } |
| 2890 | |
| 2891 | if (PyDict_Update(sum, arg) < 0) { |
| 2892 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 2893 | PyErr_Format(PyExc_TypeError, |
| 2894 | "'%.200s' object is not a mapping", |
| 2895 | arg->ob_type->tp_name); |
| 2896 | } |
| 2897 | Py_DECREF(sum); |
| 2898 | goto error; |
| 2899 | } |
| 2900 | } |
| 2901 | |
| 2902 | while (num_maps--) |
| 2903 | Py_DECREF(POP()); |
| 2904 | PUSH(sum); |
| 2905 | DISPATCH(); |
| 2906 | } |
| 2907 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2908 | TARGET(MAP_ADD) { |
| 2909 | PyObject *key = TOP(); |
| 2910 | PyObject *value = SECOND(); |
| 2911 | PyObject *map; |
| 2912 | int err; |
| 2913 | STACKADJ(-2); |
| 2914 | map = stack_pointer[-oparg]; /* dict */ |
| 2915 | assert(PyDict_CheckExact(map)); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 2916 | err = PyDict_SetItem(map, key, value); /* map[key] = value */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2917 | Py_DECREF(value); |
| 2918 | Py_DECREF(key); |
| 2919 | if (err != 0) |
| 2920 | goto error; |
| 2921 | PREDICT(JUMP_ABSOLUTE); |
| 2922 | DISPATCH(); |
| 2923 | } |
| 2924 | |
| 2925 | TARGET(LOAD_ATTR) { |
| 2926 | PyObject *name = GETITEM(names, oparg); |
| 2927 | PyObject *owner = TOP(); |
| 2928 | PyObject *res = PyObject_GetAttr(owner, name); |
| 2929 | Py_DECREF(owner); |
| 2930 | SET_TOP(res); |
| 2931 | if (res == NULL) |
| 2932 | goto error; |
| 2933 | DISPATCH(); |
| 2934 | } |
| 2935 | |
| 2936 | TARGET(COMPARE_OP) { |
| 2937 | PyObject *right = POP(); |
| 2938 | PyObject *left = TOP(); |
| 2939 | PyObject *res = cmp_outcome(oparg, left, right); |
| 2940 | Py_DECREF(left); |
| 2941 | Py_DECREF(right); |
| 2942 | SET_TOP(res); |
| 2943 | if (res == NULL) |
| 2944 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2945 | PREDICT(POP_JUMP_IF_FALSE); |
| 2946 | PREDICT(POP_JUMP_IF_TRUE); |
| 2947 | DISPATCH(); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2948 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2949 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2950 | TARGET(IMPORT_NAME) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2951 | PyObject *name = GETITEM(names, oparg); |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 2952 | PyObject *fromlist = POP(); |
| 2953 | PyObject *level = TOP(); |
| 2954 | PyObject *res; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2955 | READ_TIMESTAMP(intr0); |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 2956 | res = import_name(f, name, fromlist, level); |
| 2957 | Py_DECREF(level); |
| 2958 | Py_DECREF(fromlist); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2959 | READ_TIMESTAMP(intr1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2960 | SET_TOP(res); |
| 2961 | if (res == NULL) |
| 2962 | goto error; |
| 2963 | DISPATCH(); |
| 2964 | } |
| 2965 | |
| 2966 | TARGET(IMPORT_STAR) { |
| 2967 | PyObject *from = POP(), *locals; |
| 2968 | int err; |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 2969 | if (PyFrame_FastToLocalsWithError(f) < 0) |
| 2970 | goto error; |
| 2971 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2972 | locals = f->f_locals; |
| 2973 | if (locals == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2974 | PyErr_SetString(PyExc_SystemError, |
| 2975 | "no locals found during 'import *'"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2976 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2977 | } |
| 2978 | READ_TIMESTAMP(intr0); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2979 | err = import_all_from(locals, from); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2980 | READ_TIMESTAMP(intr1); |
| 2981 | PyFrame_LocalsToFast(f, 0); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2982 | Py_DECREF(from); |
| 2983 | if (err != 0) |
| 2984 | goto error; |
| 2985 | DISPATCH(); |
| 2986 | } |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2987 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2988 | TARGET(IMPORT_FROM) { |
| 2989 | PyObject *name = GETITEM(names, oparg); |
| 2990 | PyObject *from = TOP(); |
| 2991 | PyObject *res; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2992 | READ_TIMESTAMP(intr0); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2993 | res = import_from(from, name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2994 | READ_TIMESTAMP(intr1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2995 | PUSH(res); |
| 2996 | if (res == NULL) |
| 2997 | goto error; |
| 2998 | DISPATCH(); |
| 2999 | } |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3000 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3001 | TARGET(JUMP_FORWARD) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3002 | JUMPBY(oparg); |
| 3003 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3004 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3005 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 3006 | PREDICTED(POP_JUMP_IF_FALSE); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3007 | TARGET(POP_JUMP_IF_FALSE) { |
| 3008 | PyObject *cond = POP(); |
| 3009 | int err; |
| 3010 | if (cond == Py_True) { |
| 3011 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3012 | FAST_DISPATCH(); |
| 3013 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3014 | if (cond == Py_False) { |
| 3015 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3016 | JUMPTO(oparg); |
| 3017 | FAST_DISPATCH(); |
| 3018 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3019 | err = PyObject_IsTrue(cond); |
| 3020 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3021 | if (err > 0) |
| 3022 | err = 0; |
| 3023 | else if (err == 0) |
| 3024 | JUMPTO(oparg); |
| 3025 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3026 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3027 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3028 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3029 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 3030 | PREDICTED(POP_JUMP_IF_TRUE); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3031 | TARGET(POP_JUMP_IF_TRUE) { |
| 3032 | PyObject *cond = POP(); |
| 3033 | int err; |
| 3034 | if (cond == Py_False) { |
| 3035 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3036 | FAST_DISPATCH(); |
| 3037 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3038 | if (cond == Py_True) { |
| 3039 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3040 | JUMPTO(oparg); |
| 3041 | FAST_DISPATCH(); |
| 3042 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3043 | err = PyObject_IsTrue(cond); |
| 3044 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3045 | if (err > 0) { |
| 3046 | err = 0; |
| 3047 | JUMPTO(oparg); |
| 3048 | } |
| 3049 | else if (err == 0) |
| 3050 | ; |
| 3051 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3052 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3053 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3054 | } |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 3055 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3056 | TARGET(JUMP_IF_FALSE_OR_POP) { |
| 3057 | PyObject *cond = TOP(); |
| 3058 | int err; |
| 3059 | if (cond == Py_True) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3060 | STACKADJ(-1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3061 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3062 | FAST_DISPATCH(); |
| 3063 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3064 | if (cond == Py_False) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3065 | JUMPTO(oparg); |
| 3066 | FAST_DISPATCH(); |
| 3067 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3068 | err = PyObject_IsTrue(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3069 | if (err > 0) { |
| 3070 | STACKADJ(-1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3071 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3072 | err = 0; |
| 3073 | } |
| 3074 | else if (err == 0) |
| 3075 | JUMPTO(oparg); |
| 3076 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3077 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3078 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3079 | } |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 3080 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3081 | TARGET(JUMP_IF_TRUE_OR_POP) { |
| 3082 | PyObject *cond = TOP(); |
| 3083 | int err; |
| 3084 | if (cond == Py_False) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3085 | STACKADJ(-1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3086 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3087 | FAST_DISPATCH(); |
| 3088 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3089 | if (cond == Py_True) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3090 | JUMPTO(oparg); |
| 3091 | FAST_DISPATCH(); |
| 3092 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3093 | err = PyObject_IsTrue(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3094 | if (err > 0) { |
| 3095 | err = 0; |
| 3096 | JUMPTO(oparg); |
| 3097 | } |
| 3098 | else if (err == 0) { |
| 3099 | STACKADJ(-1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3100 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3101 | } |
| 3102 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3103 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3104 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3105 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3106 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 3107 | PREDICTED(JUMP_ABSOLUTE); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3108 | TARGET(JUMP_ABSOLUTE) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3109 | JUMPTO(oparg); |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 3110 | #if FAST_LOOPS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3111 | /* Enabling this path speeds-up all while and for-loops by bypassing |
| 3112 | the per-loop checks for signals. By default, this should be turned-off |
| 3113 | because it prevents detection of a control-break in tight loops like |
| 3114 | "while 1: pass". Compile with this option turned-on when you need |
| 3115 | the speed-up and do not need break checking inside tight loops (ones |
| 3116 | that contain only instructions ending with FAST_DISPATCH). |
| 3117 | */ |
| 3118 | FAST_DISPATCH(); |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 3119 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3120 | DISPATCH(); |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 3121 | #endif |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3122 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3123 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3124 | TARGET(GET_ITER) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3125 | /* before: [obj]; after [getiter(obj)] */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3126 | PyObject *iterable = TOP(); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 3127 | PyObject *iter = PyObject_GetIter(iterable); |
| 3128 | Py_DECREF(iterable); |
| 3129 | SET_TOP(iter); |
| 3130 | if (iter == NULL) |
| 3131 | goto error; |
| 3132 | PREDICT(FOR_ITER); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 3133 | PREDICT(CALL_FUNCTION); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 3134 | DISPATCH(); |
| 3135 | } |
| 3136 | |
| 3137 | TARGET(GET_YIELD_FROM_ITER) { |
| 3138 | /* before: [obj]; after [getiter(obj)] */ |
| 3139 | PyObject *iterable = TOP(); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3140 | PyObject *iter; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 3141 | if (PyCoro_CheckExact(iterable)) { |
| 3142 | /* `iterable` is a coroutine */ |
| 3143 | if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) { |
| 3144 | /* and it is used in a 'yield from' expression of a |
| 3145 | regular generator. */ |
| 3146 | Py_DECREF(iterable); |
| 3147 | SET_TOP(NULL); |
| 3148 | PyErr_SetString(PyExc_TypeError, |
| 3149 | "cannot 'yield from' a coroutine object " |
| 3150 | "in a non-coroutine generator"); |
| 3151 | goto error; |
| 3152 | } |
| 3153 | } |
| 3154 | else if (!PyGen_CheckExact(iterable)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3155 | /* `iterable` is not a generator. */ |
| 3156 | iter = PyObject_GetIter(iterable); |
| 3157 | Py_DECREF(iterable); |
| 3158 | SET_TOP(iter); |
| 3159 | if (iter == NULL) |
| 3160 | goto error; |
| 3161 | } |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 3162 | PREDICT(LOAD_CONST); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3163 | DISPATCH(); |
| 3164 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3165 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 3166 | PREDICTED(FOR_ITER); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3167 | TARGET(FOR_ITER) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3168 | /* before: [iter]; after: [iter, iter()] *or* [] */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3169 | PyObject *iter = TOP(); |
| 3170 | PyObject *next = (*iter->ob_type->tp_iternext)(iter); |
| 3171 | if (next != NULL) { |
| 3172 | PUSH(next); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3173 | PREDICT(STORE_FAST); |
| 3174 | PREDICT(UNPACK_SEQUENCE); |
| 3175 | DISPATCH(); |
| 3176 | } |
| 3177 | if (PyErr_Occurred()) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3178 | if (!PyErr_ExceptionMatches(PyExc_StopIteration)) |
| 3179 | goto error; |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 3180 | else if (tstate->c_tracefunc != NULL) |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3181 | call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3182 | PyErr_Clear(); |
| 3183 | } |
| 3184 | /* iterator ended normally */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3185 | STACKADJ(-1); |
| 3186 | Py_DECREF(iter); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3187 | JUMPBY(oparg); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 3188 | PREDICT(POP_BLOCK); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3189 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3190 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3191 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3192 | TARGET(BREAK_LOOP) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3193 | why = WHY_BREAK; |
| 3194 | goto fast_block_end; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3195 | } |
Raymond Hettinger | 2d783e9 | 2004-03-12 09:12:22 +0000 | [diff] [blame] | 3196 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3197 | TARGET(CONTINUE_LOOP) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3198 | retval = PyLong_FromLong(oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3199 | if (retval == NULL) |
| 3200 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3201 | why = WHY_CONTINUE; |
| 3202 | goto fast_block_end; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3203 | } |
Raymond Hettinger | 2d783e9 | 2004-03-12 09:12:22 +0000 | [diff] [blame] | 3204 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 3205 | TARGET(SETUP_LOOP) |
| 3206 | TARGET(SETUP_EXCEPT) |
| 3207 | TARGET(SETUP_FINALLY) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3208 | /* NOTE: If you add any new block-setup opcodes that |
| 3209 | are not try/except/finally handlers, you may need |
| 3210 | to update the PyGen_NeedsFinalizing() function. |
| 3211 | */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3212 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3213 | PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg, |
| 3214 | STACK_LEVEL()); |
| 3215 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3216 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3217 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3218 | TARGET(BEFORE_ASYNC_WITH) { |
| 3219 | _Py_IDENTIFIER(__aexit__); |
| 3220 | _Py_IDENTIFIER(__aenter__); |
| 3221 | |
| 3222 | PyObject *mgr = TOP(); |
| 3223 | PyObject *exit = special_lookup(mgr, &PyId___aexit__), |
| 3224 | *enter; |
| 3225 | PyObject *res; |
| 3226 | if (exit == NULL) |
| 3227 | goto error; |
| 3228 | SET_TOP(exit); |
| 3229 | enter = special_lookup(mgr, &PyId___aenter__); |
| 3230 | Py_DECREF(mgr); |
| 3231 | if (enter == NULL) |
| 3232 | goto error; |
| 3233 | res = PyObject_CallFunctionObjArgs(enter, NULL); |
| 3234 | Py_DECREF(enter); |
| 3235 | if (res == NULL) |
| 3236 | goto error; |
| 3237 | PUSH(res); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 3238 | PREDICT(GET_AWAITABLE); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3239 | DISPATCH(); |
| 3240 | } |
| 3241 | |
| 3242 | TARGET(SETUP_ASYNC_WITH) { |
| 3243 | PyObject *res = POP(); |
| 3244 | /* Setup the finally block before pushing the result |
| 3245 | of __aenter__ on the stack. */ |
| 3246 | PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg, |
| 3247 | STACK_LEVEL()); |
| 3248 | PUSH(res); |
| 3249 | DISPATCH(); |
| 3250 | } |
| 3251 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3252 | TARGET(SETUP_WITH) { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 3253 | _Py_IDENTIFIER(__exit__); |
| 3254 | _Py_IDENTIFIER(__enter__); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3255 | PyObject *mgr = TOP(); |
| 3256 | PyObject *exit = special_lookup(mgr, &PyId___exit__), *enter; |
| 3257 | PyObject *res; |
| 3258 | if (exit == NULL) |
| 3259 | goto error; |
| 3260 | SET_TOP(exit); |
| 3261 | enter = special_lookup(mgr, &PyId___enter__); |
| 3262 | Py_DECREF(mgr); |
| 3263 | if (enter == NULL) |
| 3264 | goto error; |
| 3265 | res = PyObject_CallFunctionObjArgs(enter, NULL); |
| 3266 | Py_DECREF(enter); |
| 3267 | if (res == NULL) |
| 3268 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3269 | /* Setup the finally block before pushing the result |
| 3270 | of __enter__ on the stack. */ |
| 3271 | PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg, |
| 3272 | STACK_LEVEL()); |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 3273 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3274 | PUSH(res); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3275 | DISPATCH(); |
| 3276 | } |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 3277 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3278 | TARGET(WITH_CLEANUP_START) { |
Benjamin Peterson | 8f16948 | 2013-10-29 22:25:06 -0400 | [diff] [blame] | 3279 | /* At the top of the stack are 1-6 values indicating |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3280 | how/why we entered the finally clause: |
| 3281 | - TOP = None |
| 3282 | - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval |
| 3283 | - TOP = WHY_*; no retval below it |
| 3284 | - (TOP, SECOND, THIRD) = exc_info() |
| 3285 | (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER |
| 3286 | Below them is EXIT, the context.__exit__ bound method. |
| 3287 | In the last case, we must call |
| 3288 | EXIT(TOP, SECOND, THIRD) |
| 3289 | otherwise we must call |
| 3290 | EXIT(None, None, None) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3291 | |
Benjamin Peterson | 8f16948 | 2013-10-29 22:25:06 -0400 | [diff] [blame] | 3292 | In the first three cases, we remove EXIT from the |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3293 | stack, leaving the rest in the same order. In the |
Benjamin Peterson | 8f16948 | 2013-10-29 22:25:06 -0400 | [diff] [blame] | 3294 | fourth case, we shift the bottom 3 values of the |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3295 | stack down, and replace the empty spot with NULL. |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 3296 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3297 | In addition, if the stack represents an exception, |
| 3298 | *and* the function call returns a 'true' value, we |
| 3299 | push WHY_SILENCED onto the stack. END_FINALLY will |
| 3300 | then not re-raise the exception. (But non-local |
| 3301 | gotos should still be resumed.) |
| 3302 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3303 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3304 | PyObject *exit_func; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3305 | PyObject *exc = TOP(), *val = Py_None, *tb = Py_None, *res; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3306 | if (exc == Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3307 | (void)POP(); |
| 3308 | exit_func = TOP(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3309 | SET_TOP(exc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3310 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3311 | else if (PyLong_Check(exc)) { |
| 3312 | STACKADJ(-1); |
| 3313 | switch (PyLong_AsLong(exc)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3314 | case WHY_RETURN: |
| 3315 | case WHY_CONTINUE: |
| 3316 | /* Retval in TOP. */ |
| 3317 | exit_func = SECOND(); |
| 3318 | SET_SECOND(TOP()); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3319 | SET_TOP(exc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3320 | break; |
| 3321 | default: |
| 3322 | exit_func = TOP(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3323 | SET_TOP(exc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3324 | break; |
| 3325 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3326 | exc = Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3327 | } |
| 3328 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3329 | PyObject *tp2, *exc2, *tb2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3330 | PyTryBlock *block; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3331 | val = SECOND(); |
| 3332 | tb = THIRD(); |
| 3333 | tp2 = FOURTH(); |
| 3334 | exc2 = PEEK(5); |
| 3335 | tb2 = PEEK(6); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3336 | exit_func = PEEK(7); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3337 | SET_VALUE(7, tb2); |
| 3338 | SET_VALUE(6, exc2); |
| 3339 | SET_VALUE(5, tp2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3340 | /* UNWIND_EXCEPT_HANDLER will pop this off. */ |
| 3341 | SET_FOURTH(NULL); |
| 3342 | /* We just shifted the stack down, so we have |
| 3343 | to tell the except handler block that the |
| 3344 | values are lower than it expects. */ |
| 3345 | block = &f->f_blockstack[f->f_iblock - 1]; |
| 3346 | assert(block->b_type == EXCEPT_HANDLER); |
| 3347 | block->b_level--; |
| 3348 | } |
| 3349 | /* XXX Not the fastest way to call it... */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3350 | res = PyObject_CallFunctionObjArgs(exit_func, exc, val, tb, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3351 | Py_DECREF(exit_func); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3352 | if (res == NULL) |
| 3353 | goto error; |
Amaury Forgeot d'Arc | 10b24e8 | 2008-12-10 23:49:33 +0000 | [diff] [blame] | 3354 | |
Nick Coghlan | baaadbf | 2015-05-13 15:54:02 +1000 | [diff] [blame] | 3355 | Py_INCREF(exc); /* Duplicating the exception on the stack */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3356 | PUSH(exc); |
| 3357 | PUSH(res); |
| 3358 | PREDICT(WITH_CLEANUP_FINISH); |
| 3359 | DISPATCH(); |
| 3360 | } |
| 3361 | |
| 3362 | PREDICTED(WITH_CLEANUP_FINISH); |
| 3363 | TARGET(WITH_CLEANUP_FINISH) { |
| 3364 | PyObject *res = POP(); |
| 3365 | PyObject *exc = POP(); |
| 3366 | int err; |
| 3367 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3368 | if (exc != Py_None) |
| 3369 | err = PyObject_IsTrue(res); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3370 | else |
| 3371 | err = 0; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3372 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3373 | Py_DECREF(res); |
Nick Coghlan | baaadbf | 2015-05-13 15:54:02 +1000 | [diff] [blame] | 3374 | Py_DECREF(exc); |
Amaury Forgeot d'Arc | 10b24e8 | 2008-12-10 23:49:33 +0000 | [diff] [blame] | 3375 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3376 | if (err < 0) |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3377 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3378 | else if (err > 0) { |
| 3379 | err = 0; |
| 3380 | /* There was an exception and a True return */ |
| 3381 | PUSH(PyLong_FromLong((long) WHY_SILENCED)); |
| 3382 | } |
| 3383 | PREDICT(END_FINALLY); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3384 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3385 | } |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3386 | |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 3387 | PREDICTED(CALL_FUNCTION); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3388 | TARGET(CALL_FUNCTION) { |
| 3389 | PyObject **sp, *res; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3390 | PCALL(PCALL_ALL); |
| 3391 | sp = stack_pointer; |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 3392 | #ifdef WITH_TSC |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3393 | res = call_function(&sp, oparg, &intr0, &intr1); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 3394 | #else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3395 | res = call_function(&sp, oparg); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 3396 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3397 | stack_pointer = sp; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3398 | PUSH(res); |
| 3399 | if (res == NULL) |
| 3400 | goto error; |
| 3401 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3402 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3403 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 3404 | TARGET(CALL_FUNCTION_VAR) |
| 3405 | TARGET(CALL_FUNCTION_KW) |
| 3406 | TARGET(CALL_FUNCTION_VAR_KW) { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3407 | Py_ssize_t nargs = oparg & 0xff; |
| 3408 | Py_ssize_t nkwargs = (oparg>>8) & 0xff; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3409 | int flags = (opcode - CALL_FUNCTION) & 3; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3410 | Py_ssize_t n; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3411 | PyObject **pfunc, *func, **sp, *res; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3412 | PCALL(PCALL_ALL); |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3413 | |
| 3414 | n = nargs + 2 * nkwargs; |
| 3415 | if (flags & CALL_FLAG_VAR) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3416 | n++; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3417 | } |
| 3418 | if (flags & CALL_FLAG_KW) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3419 | n++; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3420 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3421 | pfunc = stack_pointer - n - 1; |
| 3422 | func = *pfunc; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3423 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3424 | if (PyMethod_Check(func) |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 3425 | && PyMethod_GET_SELF(func) != NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3426 | PyObject *self = PyMethod_GET_SELF(func); |
| 3427 | Py_INCREF(self); |
| 3428 | func = PyMethod_GET_FUNCTION(func); |
| 3429 | Py_INCREF(func); |
Serhiy Storchaka | 57a01d3 | 2016-04-10 18:05:40 +0300 | [diff] [blame] | 3430 | Py_SETREF(*pfunc, self); |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3431 | nargs++; |
| 3432 | } |
| 3433 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3434 | Py_INCREF(func); |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3435 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3436 | sp = stack_pointer; |
| 3437 | READ_TIMESTAMP(intr0); |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3438 | res = ext_do_call(func, &sp, flags, nargs, nkwargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3439 | READ_TIMESTAMP(intr1); |
| 3440 | stack_pointer = sp; |
| 3441 | Py_DECREF(func); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3442 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3443 | while (stack_pointer > pfunc) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3444 | PyObject *o = POP(); |
| 3445 | Py_DECREF(o); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3446 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3447 | PUSH(res); |
| 3448 | if (res == NULL) |
| 3449 | goto error; |
| 3450 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3451 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3452 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 3453 | TARGET(MAKE_FUNCTION) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3454 | PyObject *qualname = POP(); |
| 3455 | PyObject *codeobj = POP(); |
| 3456 | PyFunctionObject *func = (PyFunctionObject *) |
| 3457 | PyFunction_NewWithQualName(codeobj, f->f_globals, qualname); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 3458 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3459 | Py_DECREF(codeobj); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3460 | Py_DECREF(qualname); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3461 | if (func == NULL) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3462 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3463 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 3464 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3465 | if (oparg & 0x08) { |
| 3466 | assert(PyTuple_CheckExact(TOP())); |
| 3467 | func ->func_closure = POP(); |
| 3468 | } |
| 3469 | if (oparg & 0x04) { |
| 3470 | assert(PyDict_CheckExact(TOP())); |
| 3471 | func->func_annotations = POP(); |
| 3472 | } |
| 3473 | if (oparg & 0x02) { |
| 3474 | assert(PyDict_CheckExact(TOP())); |
| 3475 | func->func_kwdefaults = POP(); |
| 3476 | } |
| 3477 | if (oparg & 0x01) { |
| 3478 | assert(PyTuple_CheckExact(TOP())); |
| 3479 | func->func_defaults = POP(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3480 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 3481 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3482 | PUSH((PyObject *)func); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3483 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3484 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 3485 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3486 | TARGET(BUILD_SLICE) { |
| 3487 | PyObject *start, *stop, *step, *slice; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3488 | if (oparg == 3) |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3489 | step = POP(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3490 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3491 | step = NULL; |
| 3492 | stop = POP(); |
| 3493 | start = TOP(); |
| 3494 | slice = PySlice_New(start, stop, step); |
| 3495 | Py_DECREF(start); |
| 3496 | Py_DECREF(stop); |
| 3497 | Py_XDECREF(step); |
| 3498 | SET_TOP(slice); |
| 3499 | if (slice == NULL) |
| 3500 | goto error; |
| 3501 | DISPATCH(); |
| 3502 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 3503 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3504 | TARGET(FORMAT_VALUE) { |
| 3505 | /* Handles f-string value formatting. */ |
| 3506 | PyObject *result; |
| 3507 | PyObject *fmt_spec; |
| 3508 | PyObject *value; |
| 3509 | PyObject *(*conv_fn)(PyObject *); |
| 3510 | int which_conversion = oparg & FVC_MASK; |
| 3511 | int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC; |
| 3512 | |
| 3513 | fmt_spec = have_fmt_spec ? POP() : NULL; |
Eric V. Smith | 135d5f4 | 2016-02-05 18:23:08 -0500 | [diff] [blame] | 3514 | value = POP(); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3515 | |
| 3516 | /* See if any conversion is specified. */ |
| 3517 | switch (which_conversion) { |
| 3518 | case FVC_STR: conv_fn = PyObject_Str; break; |
| 3519 | case FVC_REPR: conv_fn = PyObject_Repr; break; |
| 3520 | case FVC_ASCII: conv_fn = PyObject_ASCII; break; |
| 3521 | |
| 3522 | /* Must be 0 (meaning no conversion), since only four |
| 3523 | values are allowed by (oparg & FVC_MASK). */ |
| 3524 | default: conv_fn = NULL; break; |
| 3525 | } |
| 3526 | |
| 3527 | /* If there's a conversion function, call it and replace |
| 3528 | value with that result. Otherwise, just use value, |
| 3529 | without conversion. */ |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3530 | if (conv_fn != NULL) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3531 | result = conv_fn(value); |
| 3532 | Py_DECREF(value); |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3533 | if (result == NULL) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3534 | Py_XDECREF(fmt_spec); |
| 3535 | goto error; |
| 3536 | } |
| 3537 | value = result; |
| 3538 | } |
| 3539 | |
| 3540 | /* If value is a unicode object, and there's no fmt_spec, |
| 3541 | then we know the result of format(value) is value |
| 3542 | itself. In that case, skip calling format(). I plan to |
| 3543 | move this optimization in to PyObject_Format() |
| 3544 | itself. */ |
| 3545 | if (PyUnicode_CheckExact(value) && fmt_spec == NULL) { |
| 3546 | /* Do nothing, just transfer ownership to result. */ |
| 3547 | result = value; |
| 3548 | } else { |
| 3549 | /* Actually call format(). */ |
| 3550 | result = PyObject_Format(value, fmt_spec); |
| 3551 | Py_DECREF(value); |
| 3552 | Py_XDECREF(fmt_spec); |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3553 | if (result == NULL) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3554 | goto error; |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3555 | } |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3556 | } |
| 3557 | |
Eric V. Smith | 135d5f4 | 2016-02-05 18:23:08 -0500 | [diff] [blame] | 3558 | PUSH(result); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3559 | DISPATCH(); |
| 3560 | } |
| 3561 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3562 | TARGET(EXTENDED_ARG) { |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 3563 | int oldoparg = oparg; |
| 3564 | NEXTOPARG(); |
| 3565 | oparg |= oldoparg << 8; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3566 | goto dispatch_opcode; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3567 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 3568 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3569 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 3570 | #if USE_COMPUTED_GOTOS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3571 | _unknown_opcode: |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 3572 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3573 | default: |
| 3574 | fprintf(stderr, |
| 3575 | "XXX lineno: %d, opcode: %d\n", |
| 3576 | PyFrame_GetLineNumber(f), |
| 3577 | opcode); |
| 3578 | PyErr_SetString(PyExc_SystemError, "unknown opcode"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3579 | goto error; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 3580 | |
| 3581 | #ifdef CASE_TOO_BIG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3582 | } |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 3583 | #endif |
| 3584 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3585 | } /* switch */ |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3586 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3587 | /* This should never be reached. Every opcode should end with DISPATCH() |
| 3588 | or goto error. */ |
| 3589 | assert(0); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3590 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3591 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3592 | READ_TIMESTAMP(inst1); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 3593 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3594 | assert(why == WHY_NOT); |
| 3595 | why = WHY_EXCEPTION; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3596 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3597 | /* Double-check exception status. */ |
Victor Stinner | 365b693 | 2013-07-12 00:11:58 +0200 | [diff] [blame] | 3598 | #ifdef NDEBUG |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3599 | if (!PyErr_Occurred()) |
| 3600 | PyErr_SetString(PyExc_SystemError, |
| 3601 | "error return without exception set"); |
Victor Stinner | 365b693 | 2013-07-12 00:11:58 +0200 | [diff] [blame] | 3602 | #else |
| 3603 | assert(PyErr_Occurred()); |
| 3604 | #endif |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3605 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3606 | /* Log traceback info. */ |
| 3607 | PyTraceBack_Here(f); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3608 | |
Benjamin Peterson | 51f4616 | 2013-01-23 08:38:47 -0500 | [diff] [blame] | 3609 | if (tstate->c_tracefunc != NULL) |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3610 | call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, |
| 3611 | tstate, f); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3612 | |
Raymond Hettinger | 1dd8309 | 2004-02-06 18:32:33 +0000 | [diff] [blame] | 3613 | fast_block_end: |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3614 | assert(why != WHY_NOT); |
| 3615 | |
| 3616 | /* Unwind stacks if a (pseudo) exception occurred */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3617 | while (why != WHY_NOT && f->f_iblock > 0) { |
| 3618 | /* Peek at the current block. */ |
| 3619 | PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1]; |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3620 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3621 | assert(why != WHY_YIELD); |
| 3622 | if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) { |
| 3623 | why = WHY_NOT; |
| 3624 | JUMPTO(PyLong_AS_LONG(retval)); |
| 3625 | Py_DECREF(retval); |
| 3626 | break; |
| 3627 | } |
| 3628 | /* Now we have to pop the block. */ |
| 3629 | f->f_iblock--; |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3630 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3631 | if (b->b_type == EXCEPT_HANDLER) { |
| 3632 | UNWIND_EXCEPT_HANDLER(b); |
| 3633 | continue; |
| 3634 | } |
| 3635 | UNWIND_BLOCK(b); |
| 3636 | if (b->b_type == SETUP_LOOP && why == WHY_BREAK) { |
| 3637 | why = WHY_NOT; |
| 3638 | JUMPTO(b->b_handler); |
| 3639 | break; |
| 3640 | } |
| 3641 | if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT |
| 3642 | || b->b_type == SETUP_FINALLY)) { |
| 3643 | PyObject *exc, *val, *tb; |
| 3644 | int handler = b->b_handler; |
| 3645 | /* Beware, this invalidates all b->b_* fields */ |
| 3646 | PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL()); |
| 3647 | PUSH(tstate->exc_traceback); |
| 3648 | PUSH(tstate->exc_value); |
| 3649 | if (tstate->exc_type != NULL) { |
| 3650 | PUSH(tstate->exc_type); |
| 3651 | } |
| 3652 | else { |
| 3653 | Py_INCREF(Py_None); |
| 3654 | PUSH(Py_None); |
| 3655 | } |
| 3656 | PyErr_Fetch(&exc, &val, &tb); |
| 3657 | /* Make the raw exception data |
| 3658 | available to the handler, |
| 3659 | so a program can emulate the |
| 3660 | Python main loop. */ |
| 3661 | PyErr_NormalizeException( |
| 3662 | &exc, &val, &tb); |
Victor Stinner | 7eab0d0 | 2013-07-15 21:16:27 +0200 | [diff] [blame] | 3663 | if (tb != NULL) |
| 3664 | PyException_SetTraceback(val, tb); |
| 3665 | else |
| 3666 | PyException_SetTraceback(val, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3667 | Py_INCREF(exc); |
| 3668 | tstate->exc_type = exc; |
| 3669 | Py_INCREF(val); |
| 3670 | tstate->exc_value = val; |
| 3671 | tstate->exc_traceback = tb; |
| 3672 | if (tb == NULL) |
| 3673 | tb = Py_None; |
| 3674 | Py_INCREF(tb); |
| 3675 | PUSH(tb); |
| 3676 | PUSH(val); |
| 3677 | PUSH(exc); |
| 3678 | why = WHY_NOT; |
| 3679 | JUMPTO(handler); |
| 3680 | break; |
| 3681 | } |
| 3682 | if (b->b_type == SETUP_FINALLY) { |
| 3683 | if (why & (WHY_RETURN | WHY_CONTINUE)) |
| 3684 | PUSH(retval); |
| 3685 | PUSH(PyLong_FromLong((long)why)); |
| 3686 | why = WHY_NOT; |
| 3687 | JUMPTO(b->b_handler); |
| 3688 | break; |
| 3689 | } |
| 3690 | } /* unwind stack */ |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3691 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3692 | /* End the loop if we still have an error (or return) */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3693 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3694 | if (why != WHY_NOT) |
| 3695 | break; |
| 3696 | READ_TIMESTAMP(loop1); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3697 | |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 3698 | assert(!PyErr_Occurred()); |
| 3699 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3700 | } /* main loop */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3701 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3702 | assert(why != WHY_YIELD); |
| 3703 | /* Pop remaining stack entries. */ |
| 3704 | while (!EMPTY()) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3705 | PyObject *o = POP(); |
| 3706 | Py_XDECREF(o); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3707 | } |
Guido van Rossum | 35974fb | 2001-12-06 21:28:18 +0000 | [diff] [blame] | 3708 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3709 | if (why != WHY_RETURN) |
| 3710 | retval = NULL; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3711 | |
Victor Stinner | 4a7cc88 | 2015-03-06 23:35:27 +0100 | [diff] [blame] | 3712 | assert((retval != NULL) ^ (PyErr_Occurred() != NULL)); |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 3713 | |
Raymond Hettinger | 1dd8309 | 2004-02-06 18:32:33 +0000 | [diff] [blame] | 3714 | fast_yield: |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 3715 | if (co->co_flags & (CO_GENERATOR | CO_COROUTINE)) { |
Victor Stinner | 26f7b8a | 2015-01-31 10:29:47 +0100 | [diff] [blame] | 3716 | |
Benjamin Peterson | ac91341 | 2011-07-03 16:25:11 -0500 | [diff] [blame] | 3717 | /* The purpose of this block is to put aside the generator's exception |
| 3718 | state and restore that of the calling frame. If the current |
| 3719 | exception state is from the caller, we clear the exception values |
| 3720 | on the generator frame, so they are not swapped back in latter. The |
| 3721 | origin of the current exception state is determined by checking for |
| 3722 | except handler blocks, which we must be in iff a new exception |
| 3723 | state came into existence in this frame. (An uncaught exception |
| 3724 | would have why == WHY_EXCEPTION, and we wouldn't be here). */ |
| 3725 | int i; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3726 | for (i = 0; i < f->f_iblock; i++) { |
| 3727 | if (f->f_blockstack[i].b_type == EXCEPT_HANDLER) { |
Benjamin Peterson | ac91341 | 2011-07-03 16:25:11 -0500 | [diff] [blame] | 3728 | break; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3729 | } |
| 3730 | } |
Benjamin Peterson | ac91341 | 2011-07-03 16:25:11 -0500 | [diff] [blame] | 3731 | if (i == f->f_iblock) |
| 3732 | /* We did not create this exception. */ |
Benjamin Peterson | 8788024 | 2011-07-03 16:48:31 -0500 | [diff] [blame] | 3733 | restore_and_clear_exc_state(tstate, f); |
Benjamin Peterson | ac91341 | 2011-07-03 16:25:11 -0500 | [diff] [blame] | 3734 | else |
Benjamin Peterson | 8788024 | 2011-07-03 16:48:31 -0500 | [diff] [blame] | 3735 | swap_exc_state(tstate, f); |
Benjamin Peterson | ac91341 | 2011-07-03 16:25:11 -0500 | [diff] [blame] | 3736 | } |
Benjamin Peterson | 83195c3 | 2011-07-03 13:44:00 -0500 | [diff] [blame] | 3737 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3738 | if (tstate->use_tracing) { |
Benjamin Peterson | 51f4616 | 2013-01-23 08:38:47 -0500 | [diff] [blame] | 3739 | if (tstate->c_tracefunc) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3740 | if (why == WHY_RETURN || why == WHY_YIELD) { |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3741 | if (call_trace(tstate->c_tracefunc, tstate->c_traceobj, |
| 3742 | tstate, f, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3743 | PyTrace_RETURN, retval)) { |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 3744 | Py_CLEAR(retval); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3745 | why = WHY_EXCEPTION; |
| 3746 | } |
| 3747 | } |
| 3748 | else if (why == WHY_EXCEPTION) { |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3749 | call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj, |
| 3750 | tstate, f, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3751 | PyTrace_RETURN, NULL); |
| 3752 | } |
| 3753 | } |
| 3754 | if (tstate->c_profilefunc) { |
| 3755 | if (why == WHY_EXCEPTION) |
| 3756 | call_trace_protected(tstate->c_profilefunc, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3757 | tstate->c_profileobj, |
| 3758 | tstate, f, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3759 | PyTrace_RETURN, NULL); |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3760 | else if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, |
| 3761 | tstate, f, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3762 | PyTrace_RETURN, retval)) { |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 3763 | Py_CLEAR(retval); |
Brett Cannon | b94767f | 2011-02-22 20:15:44 +0000 | [diff] [blame] | 3764 | /* why = WHY_EXCEPTION; */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3765 | } |
| 3766 | } |
| 3767 | } |
Guido van Rossum | a424013 | 1997-01-21 21:18:36 +0000 | [diff] [blame] | 3768 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3769 | /* pop frame */ |
Thomas Wouters | ce272b6 | 2007-09-19 21:19:28 +0000 | [diff] [blame] | 3770 | exit_eval_frame: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3771 | Py_LeaveRecursiveCall(); |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 3772 | f->f_executing = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3773 | tstate->frame = f->f_back; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3774 | |
Victor Stinner | efde146 | 2015-03-21 15:04:43 +0100 | [diff] [blame] | 3775 | return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx"); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3776 | } |
| 3777 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3778 | static void |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3779 | format_missing(const char *kind, PyCodeObject *co, PyObject *names) |
| 3780 | { |
| 3781 | int err; |
| 3782 | Py_ssize_t len = PyList_GET_SIZE(names); |
| 3783 | PyObject *name_str, *comma, *tail, *tmp; |
| 3784 | |
| 3785 | assert(PyList_CheckExact(names)); |
| 3786 | assert(len >= 1); |
| 3787 | /* Deal with the joys of natural language. */ |
| 3788 | switch (len) { |
| 3789 | case 1: |
| 3790 | name_str = PyList_GET_ITEM(names, 0); |
| 3791 | Py_INCREF(name_str); |
| 3792 | break; |
| 3793 | case 2: |
| 3794 | name_str = PyUnicode_FromFormat("%U and %U", |
| 3795 | PyList_GET_ITEM(names, len - 2), |
| 3796 | PyList_GET_ITEM(names, len - 1)); |
| 3797 | break; |
| 3798 | default: |
| 3799 | tail = PyUnicode_FromFormat(", %U, and %U", |
| 3800 | PyList_GET_ITEM(names, len - 2), |
| 3801 | PyList_GET_ITEM(names, len - 1)); |
Benjamin Peterson | d1ab608 | 2012-06-01 11:18:22 -0700 | [diff] [blame] | 3802 | if (tail == NULL) |
| 3803 | return; |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3804 | /* Chop off the last two objects in the list. This shouldn't actually |
| 3805 | fail, but we can't be too careful. */ |
| 3806 | err = PyList_SetSlice(names, len - 2, len, NULL); |
| 3807 | if (err == -1) { |
| 3808 | Py_DECREF(tail); |
| 3809 | return; |
| 3810 | } |
| 3811 | /* Stitch everything up into a nice comma-separated list. */ |
| 3812 | comma = PyUnicode_FromString(", "); |
| 3813 | if (comma == NULL) { |
| 3814 | Py_DECREF(tail); |
| 3815 | return; |
| 3816 | } |
| 3817 | tmp = PyUnicode_Join(comma, names); |
| 3818 | Py_DECREF(comma); |
| 3819 | if (tmp == NULL) { |
| 3820 | Py_DECREF(tail); |
| 3821 | return; |
| 3822 | } |
| 3823 | name_str = PyUnicode_Concat(tmp, tail); |
| 3824 | Py_DECREF(tmp); |
| 3825 | Py_DECREF(tail); |
| 3826 | break; |
| 3827 | } |
| 3828 | if (name_str == NULL) |
| 3829 | return; |
| 3830 | PyErr_Format(PyExc_TypeError, |
| 3831 | "%U() missing %i required %s argument%s: %U", |
| 3832 | co->co_name, |
| 3833 | len, |
| 3834 | kind, |
| 3835 | len == 1 ? "" : "s", |
| 3836 | name_str); |
| 3837 | Py_DECREF(name_str); |
| 3838 | } |
| 3839 | |
| 3840 | static void |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3841 | missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount, |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3842 | PyObject **fastlocals) |
| 3843 | { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3844 | Py_ssize_t i, j = 0; |
| 3845 | Py_ssize_t start, end; |
| 3846 | int positional = (defcount != -1); |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3847 | const char *kind = positional ? "positional" : "keyword-only"; |
| 3848 | PyObject *missing_names; |
| 3849 | |
| 3850 | /* Compute the names of the arguments that are missing. */ |
| 3851 | missing_names = PyList_New(missing); |
| 3852 | if (missing_names == NULL) |
| 3853 | return; |
| 3854 | if (positional) { |
| 3855 | start = 0; |
| 3856 | end = co->co_argcount - defcount; |
| 3857 | } |
| 3858 | else { |
| 3859 | start = co->co_argcount; |
| 3860 | end = start + co->co_kwonlyargcount; |
| 3861 | } |
| 3862 | for (i = start; i < end; i++) { |
| 3863 | if (GETLOCAL(i) == NULL) { |
| 3864 | PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i); |
| 3865 | PyObject *name = PyObject_Repr(raw); |
| 3866 | if (name == NULL) { |
| 3867 | Py_DECREF(missing_names); |
| 3868 | return; |
| 3869 | } |
| 3870 | PyList_SET_ITEM(missing_names, j++, name); |
| 3871 | } |
| 3872 | } |
| 3873 | assert(j == missing); |
| 3874 | format_missing(kind, co, missing_names); |
| 3875 | Py_DECREF(missing_names); |
| 3876 | } |
| 3877 | |
| 3878 | static void |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3879 | too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount, |
| 3880 | PyObject **fastlocals) |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3881 | { |
| 3882 | int plural; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3883 | Py_ssize_t kwonly_given = 0; |
| 3884 | Py_ssize_t i; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3885 | PyObject *sig, *kwonly_sig; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3886 | Py_ssize_t co_argcount = co->co_argcount; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3887 | |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3888 | assert((co->co_flags & CO_VARARGS) == 0); |
| 3889 | /* Count missing keyword-only args. */ |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3890 | for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) { |
| 3891 | if (GETLOCAL(i) != NULL) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3892 | kwonly_given++; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3893 | } |
| 3894 | } |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3895 | if (defcount) { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3896 | Py_ssize_t atleast = co_argcount - defcount; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3897 | plural = 1; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3898 | sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3899 | } |
| 3900 | else { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3901 | plural = (co_argcount != 1); |
| 3902 | sig = PyUnicode_FromFormat("%zd", co_argcount); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3903 | } |
| 3904 | if (sig == NULL) |
| 3905 | return; |
| 3906 | if (kwonly_given) { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3907 | const char *format = " positional argument%s (and %zd keyword-only argument%s)"; |
| 3908 | kwonly_sig = PyUnicode_FromFormat(format, |
| 3909 | given != 1 ? "s" : "", |
| 3910 | kwonly_given, |
| 3911 | kwonly_given != 1 ? "s" : ""); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3912 | if (kwonly_sig == NULL) { |
| 3913 | Py_DECREF(sig); |
| 3914 | return; |
| 3915 | } |
| 3916 | } |
| 3917 | else { |
| 3918 | /* This will not fail. */ |
| 3919 | kwonly_sig = PyUnicode_FromString(""); |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3920 | assert(kwonly_sig != NULL); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3921 | } |
| 3922 | PyErr_Format(PyExc_TypeError, |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3923 | "%U() takes %U positional argument%s but %zd%U %s given", |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3924 | co->co_name, |
| 3925 | sig, |
| 3926 | plural ? "s" : "", |
| 3927 | given, |
| 3928 | kwonly_sig, |
| 3929 | given == 1 && !kwonly_given ? "was" : "were"); |
| 3930 | Py_DECREF(sig); |
| 3931 | Py_DECREF(kwonly_sig); |
| 3932 | } |
| 3933 | |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 3934 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3935 | /* 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] | 3936 | PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3937 | the test in the if statements in Misc/gdbinit (pystack and pystackv). */ |
Skip Montanaro | 786ea6b | 2004-03-01 15:44:05 +0000 | [diff] [blame] | 3938 | |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 3939 | static PyObject * |
| 3940 | _PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals, |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3941 | PyObject **args, Py_ssize_t argcount, |
| 3942 | PyObject **kws, Py_ssize_t kwcount, |
| 3943 | PyObject **defs, Py_ssize_t defcount, |
| 3944 | PyObject *kwdefs, PyObject *closure, |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 3945 | PyObject *name, PyObject *qualname) |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3946 | { |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 3947 | PyCodeObject* co = (PyCodeObject*)_co; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 3948 | PyFrameObject *f; |
| 3949 | PyObject *retval = NULL; |
| 3950 | PyObject **fastlocals, **freevars; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3951 | PyThreadState *tstate; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3952 | PyObject *x, *u; |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 3953 | const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount; |
| 3954 | Py_ssize_t i, n; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3955 | PyObject *kwdict; |
| 3956 | |
| 3957 | assert((kwcount == 0) || (kws != NULL)); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3958 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3959 | if (globals == NULL) { |
| 3960 | PyErr_SetString(PyExc_SystemError, |
| 3961 | "PyEval_EvalCodeEx: NULL globals"); |
| 3962 | return NULL; |
| 3963 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3964 | |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3965 | /* Create the frame */ |
| 3966 | tstate = PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3967 | assert(tstate != NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3968 | f = PyFrame_New(tstate, co, globals, locals); |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3969 | if (f == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3970 | return NULL; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3971 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3972 | fastlocals = f->f_localsplus; |
| 3973 | freevars = f->f_localsplus + co->co_nlocals; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3974 | |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3975 | /* Create a dictionary for keyword parameters (**kwags) */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3976 | if (co->co_flags & CO_VARKEYWORDS) { |
| 3977 | kwdict = PyDict_New(); |
| 3978 | if (kwdict == NULL) |
| 3979 | goto fail; |
| 3980 | i = total_args; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3981 | if (co->co_flags & CO_VARARGS) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3982 | i++; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3983 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3984 | SETLOCAL(i, kwdict); |
| 3985 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3986 | else { |
| 3987 | kwdict = NULL; |
| 3988 | } |
| 3989 | |
| 3990 | /* Copy positional arguments into local variables */ |
| 3991 | if (argcount > co->co_argcount) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3992 | n = co->co_argcount; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3993 | } |
| 3994 | else { |
| 3995 | n = argcount; |
| 3996 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3997 | for (i = 0; i < n; i++) { |
| 3998 | x = args[i]; |
| 3999 | Py_INCREF(x); |
| 4000 | SETLOCAL(i, x); |
| 4001 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4002 | |
| 4003 | /* Pack other positional arguments into the *args argument */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4004 | if (co->co_flags & CO_VARARGS) { |
| 4005 | u = PyTuple_New(argcount - n); |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4006 | if (u == NULL) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4007 | goto fail; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4008 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4009 | SETLOCAL(total_args, u); |
| 4010 | for (i = n; i < argcount; i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4011 | x = args[i]; |
| 4012 | Py_INCREF(x); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4013 | PyTuple_SET_ITEM(u, i-n, x); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4014 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4015 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4016 | |
| 4017 | /* Handle keyword arguments (passed as an array of (key, value)) */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4018 | for (i = 0; i < kwcount; i++) { |
| 4019 | PyObject **co_varnames; |
| 4020 | PyObject *keyword = kws[2*i]; |
| 4021 | PyObject *value = kws[2*i + 1]; |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 4022 | Py_ssize_t j; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4023 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4024 | if (keyword == NULL || !PyUnicode_Check(keyword)) { |
| 4025 | PyErr_Format(PyExc_TypeError, |
| 4026 | "%U() keywords must be strings", |
| 4027 | co->co_name); |
| 4028 | goto fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4029 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4030 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4031 | /* Speed hack: do raw pointer compares. As names are |
| 4032 | normally interned this should almost always hit. */ |
| 4033 | co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item; |
| 4034 | for (j = 0; j < total_args; j++) { |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 4035 | PyObject *name = co_varnames[j]; |
| 4036 | if (name == keyword) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4037 | goto kw_found; |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 4038 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4039 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4040 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4041 | /* Slow fallback, just in case */ |
| 4042 | for (j = 0; j < total_args; j++) { |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 4043 | PyObject *name = co_varnames[j]; |
| 4044 | int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ); |
| 4045 | if (cmp > 0) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4046 | goto kw_found; |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 4047 | } |
| 4048 | else if (cmp < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4049 | goto fail; |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 4050 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4051 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4052 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4053 | if (j >= total_args && kwdict == NULL) { |
| 4054 | PyErr_Format(PyExc_TypeError, |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 4055 | "%U() got an unexpected keyword argument '%S'", |
| 4056 | co->co_name, keyword); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4057 | goto fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4058 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4059 | |
Christian Heimes | 0bd447f | 2013-07-20 14:48:10 +0200 | [diff] [blame] | 4060 | if (PyDict_SetItem(kwdict, keyword, value) == -1) { |
| 4061 | goto fail; |
| 4062 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4063 | continue; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4064 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4065 | kw_found: |
| 4066 | if (GETLOCAL(j) != NULL) { |
| 4067 | PyErr_Format(PyExc_TypeError, |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 4068 | "%U() got multiple values for argument '%S'", |
| 4069 | co->co_name, keyword); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4070 | goto fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4071 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4072 | Py_INCREF(value); |
| 4073 | SETLOCAL(j, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4074 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4075 | |
| 4076 | /* Check the number of positional arguments */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4077 | if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) { |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4078 | too_many_positional(co, argcount, defcount, fastlocals); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4079 | goto fail; |
| 4080 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4081 | |
| 4082 | /* Add missing positional arguments (copy default values from defs) */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4083 | if (argcount < co->co_argcount) { |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 4084 | Py_ssize_t m = co->co_argcount - defcount; |
| 4085 | Py_ssize_t missing = 0; |
| 4086 | for (i = argcount; i < m; i++) { |
| 4087 | if (GETLOCAL(i) == NULL) { |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4088 | missing++; |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 4089 | } |
| 4090 | } |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4091 | if (missing) { |
| 4092 | missing_arguments(co, missing, defcount, fastlocals); |
| 4093 | goto fail; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4094 | } |
| 4095 | if (n > m) |
| 4096 | i = n - m; |
| 4097 | else |
| 4098 | i = 0; |
| 4099 | for (; i < defcount; i++) { |
| 4100 | if (GETLOCAL(m+i) == NULL) { |
| 4101 | PyObject *def = defs[i]; |
| 4102 | Py_INCREF(def); |
| 4103 | SETLOCAL(m+i, def); |
| 4104 | } |
| 4105 | } |
| 4106 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4107 | |
| 4108 | /* Add missing keyword arguments (copy default values from kwdefs) */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4109 | if (co->co_kwonlyargcount > 0) { |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 4110 | Py_ssize_t missing = 0; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4111 | for (i = co->co_argcount; i < total_args; i++) { |
| 4112 | PyObject *name; |
| 4113 | if (GETLOCAL(i) != NULL) |
| 4114 | continue; |
| 4115 | name = PyTuple_GET_ITEM(co->co_varnames, i); |
| 4116 | if (kwdefs != NULL) { |
| 4117 | PyObject *def = PyDict_GetItem(kwdefs, name); |
| 4118 | if (def) { |
| 4119 | Py_INCREF(def); |
| 4120 | SETLOCAL(i, def); |
| 4121 | continue; |
| 4122 | } |
| 4123 | } |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4124 | missing++; |
| 4125 | } |
| 4126 | if (missing) { |
| 4127 | missing_arguments(co, missing, -1, fastlocals); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4128 | goto fail; |
| 4129 | } |
| 4130 | } |
| 4131 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4132 | /* Allocate and initialize storage for cell vars, and copy free |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4133 | vars into frame. */ |
| 4134 | for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4135 | PyObject *c; |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4136 | int arg; |
| 4137 | /* Possibly account for the cell variable being an argument. */ |
| 4138 | if (co->co_cell2arg != NULL && |
Guido van Rossum | 6832c81 | 2013-05-10 08:47:42 -0700 | [diff] [blame] | 4139 | (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) { |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4140 | c = PyCell_New(GETLOCAL(arg)); |
Benjamin Peterson | 159ae41 | 2013-05-12 18:16:06 -0500 | [diff] [blame] | 4141 | /* Clear the local copy. */ |
| 4142 | SETLOCAL(arg, NULL); |
Guido van Rossum | 6832c81 | 2013-05-10 08:47:42 -0700 | [diff] [blame] | 4143 | } |
| 4144 | else { |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4145 | c = PyCell_New(NULL); |
Guido van Rossum | 6832c81 | 2013-05-10 08:47:42 -0700 | [diff] [blame] | 4146 | } |
Benjamin Peterson | 159ae41 | 2013-05-12 18:16:06 -0500 | [diff] [blame] | 4147 | if (c == NULL) |
| 4148 | goto fail; |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4149 | SETLOCAL(co->co_nlocals + i, c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4150 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4151 | |
| 4152 | /* Copy closure variables to free variables */ |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4153 | for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) { |
| 4154 | PyObject *o = PyTuple_GET_ITEM(closure, i); |
| 4155 | Py_INCREF(o); |
| 4156 | freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4157 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4158 | |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4159 | /* Handle generator/coroutine */ |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 4160 | if (co->co_flags & (CO_GENERATOR | CO_COROUTINE)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4161 | PyObject *gen; |
Yury Selivanov | 94c2263 | 2015-06-04 10:16:51 -0400 | [diff] [blame] | 4162 | PyObject *coro_wrapper = tstate->coroutine_wrapper; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 4163 | int is_coro = co->co_flags & CO_COROUTINE; |
Yury Selivanov | 94c2263 | 2015-06-04 10:16:51 -0400 | [diff] [blame] | 4164 | |
| 4165 | if (is_coro && tstate->in_coroutine_wrapper) { |
| 4166 | assert(coro_wrapper != NULL); |
| 4167 | PyErr_Format(PyExc_RuntimeError, |
| 4168 | "coroutine wrapper %.200R attempted " |
| 4169 | "to recursively wrap %.200R", |
| 4170 | coro_wrapper, |
| 4171 | co); |
| 4172 | goto fail; |
| 4173 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4174 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4175 | /* Don't need to keep the reference to f_back, it will be set |
| 4176 | * when the generator is resumed. */ |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 4177 | Py_CLEAR(f->f_back); |
Neil Schemenauer | 2b13ce8 | 2001-06-21 02:41:10 +0000 | [diff] [blame] | 4178 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4179 | PCALL(PCALL_GENERATOR); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4180 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4181 | /* Create a new generator that owns the ready to run frame |
| 4182 | * and return that as the value. */ |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 4183 | if (is_coro) { |
| 4184 | gen = PyCoro_New(f, name, qualname); |
| 4185 | } else { |
| 4186 | gen = PyGen_NewWithQualName(f, name, qualname); |
| 4187 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4188 | if (gen == NULL) |
| 4189 | return NULL; |
| 4190 | |
Yury Selivanov | 94c2263 | 2015-06-04 10:16:51 -0400 | [diff] [blame] | 4191 | if (is_coro && coro_wrapper != NULL) { |
| 4192 | PyObject *wrapped; |
| 4193 | tstate->in_coroutine_wrapper = 1; |
| 4194 | wrapped = PyObject_CallFunction(coro_wrapper, "N", gen); |
| 4195 | tstate->in_coroutine_wrapper = 0; |
| 4196 | return wrapped; |
| 4197 | } |
Yury Selivanov | aab3c4a | 2015-06-02 18:43:51 -0400 | [diff] [blame] | 4198 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4199 | return gen; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4200 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4201 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4202 | retval = PyEval_EvalFrameEx(f,0); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4203 | |
Thomas Wouters | ce272b6 | 2007-09-19 21:19:28 +0000 | [diff] [blame] | 4204 | fail: /* Jump here from prelude on failure */ |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4205 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4206 | /* decref'ing the frame can cause __del__ methods to get invoked, |
| 4207 | which can call back into Python. While we're done with the |
| 4208 | current Python frame (f), the associated C stack is still in use, |
| 4209 | so recursion_depth must be boosted for the duration. |
| 4210 | */ |
| 4211 | assert(tstate != NULL); |
| 4212 | ++tstate->recursion_depth; |
| 4213 | Py_DECREF(f); |
| 4214 | --tstate->recursion_depth; |
| 4215 | return retval; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4216 | } |
| 4217 | |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 4218 | PyObject * |
| 4219 | PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals, |
| 4220 | PyObject **args, int argcount, PyObject **kws, int kwcount, |
| 4221 | PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure) |
| 4222 | { |
| 4223 | return _PyEval_EvalCodeWithName(_co, globals, locals, |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4224 | args, argcount, |
| 4225 | kws, kwcount, |
| 4226 | defs, defcount, |
| 4227 | kwdefs, closure, |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 4228 | NULL, NULL); |
| 4229 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4230 | |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4231 | static PyObject * |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4232 | special_lookup(PyObject *o, _Py_Identifier *id) |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4233 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4234 | PyObject *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4235 | res = _PyObject_LookupSpecial(o, id); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4236 | if (res == NULL && !PyErr_Occurred()) { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4237 | PyErr_SetObject(PyExc_AttributeError, id->object); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4238 | return NULL; |
| 4239 | } |
| 4240 | return res; |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4241 | } |
| 4242 | |
| 4243 | |
Benjamin Peterson | 8788024 | 2011-07-03 16:48:31 -0500 | [diff] [blame] | 4244 | /* These 3 functions deal with the exception state of generators. */ |
| 4245 | |
| 4246 | static void |
| 4247 | save_exc_state(PyThreadState *tstate, PyFrameObject *f) |
| 4248 | { |
| 4249 | PyObject *type, *value, *traceback; |
| 4250 | Py_XINCREF(tstate->exc_type); |
| 4251 | Py_XINCREF(tstate->exc_value); |
| 4252 | Py_XINCREF(tstate->exc_traceback); |
| 4253 | type = f->f_exc_type; |
| 4254 | value = f->f_exc_value; |
| 4255 | traceback = f->f_exc_traceback; |
| 4256 | f->f_exc_type = tstate->exc_type; |
| 4257 | f->f_exc_value = tstate->exc_value; |
| 4258 | f->f_exc_traceback = tstate->exc_traceback; |
| 4259 | Py_XDECREF(type); |
| 4260 | Py_XDECREF(value); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 4261 | Py_XDECREF(traceback); |
Benjamin Peterson | 8788024 | 2011-07-03 16:48:31 -0500 | [diff] [blame] | 4262 | } |
| 4263 | |
| 4264 | static void |
| 4265 | swap_exc_state(PyThreadState *tstate, PyFrameObject *f) |
| 4266 | { |
| 4267 | PyObject *tmp; |
| 4268 | tmp = tstate->exc_type; |
| 4269 | tstate->exc_type = f->f_exc_type; |
| 4270 | f->f_exc_type = tmp; |
| 4271 | tmp = tstate->exc_value; |
| 4272 | tstate->exc_value = f->f_exc_value; |
| 4273 | f->f_exc_value = tmp; |
| 4274 | tmp = tstate->exc_traceback; |
| 4275 | tstate->exc_traceback = f->f_exc_traceback; |
| 4276 | f->f_exc_traceback = tmp; |
| 4277 | } |
| 4278 | |
| 4279 | static void |
| 4280 | restore_and_clear_exc_state(PyThreadState *tstate, PyFrameObject *f) |
| 4281 | { |
| 4282 | PyObject *type, *value, *tb; |
| 4283 | type = tstate->exc_type; |
| 4284 | value = tstate->exc_value; |
| 4285 | tb = tstate->exc_traceback; |
| 4286 | tstate->exc_type = f->f_exc_type; |
| 4287 | tstate->exc_value = f->f_exc_value; |
| 4288 | tstate->exc_traceback = f->f_exc_traceback; |
| 4289 | f->f_exc_type = NULL; |
| 4290 | f->f_exc_value = NULL; |
| 4291 | f->f_exc_traceback = NULL; |
| 4292 | Py_XDECREF(type); |
| 4293 | Py_XDECREF(value); |
| 4294 | Py_XDECREF(tb); |
| 4295 | } |
| 4296 | |
| 4297 | |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 4298 | /* Logic for the raise statement (too complicated for inlining). |
| 4299 | This *consumes* a reference count to each of its arguments. */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4300 | static int |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4301 | do_raise(PyObject *exc, PyObject *cause) |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 4302 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4303 | PyObject *type = NULL, *value = NULL; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4304 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4305 | if (exc == NULL) { |
| 4306 | /* Reraise */ |
| 4307 | PyThreadState *tstate = PyThreadState_GET(); |
| 4308 | PyObject *tb; |
| 4309 | type = tstate->exc_type; |
| 4310 | value = tstate->exc_value; |
| 4311 | tb = tstate->exc_traceback; |
Victor Stinner | eec9331 | 2016-08-18 18:13:10 +0200 | [diff] [blame] | 4312 | if (type == Py_None || type == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4313 | PyErr_SetString(PyExc_RuntimeError, |
| 4314 | "No active exception to reraise"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4315 | return 0; |
| 4316 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4317 | Py_XINCREF(type); |
| 4318 | Py_XINCREF(value); |
| 4319 | Py_XINCREF(tb); |
| 4320 | PyErr_Restore(type, value, tb); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4321 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4322 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4323 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4324 | /* We support the following forms of raise: |
| 4325 | raise |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4326 | raise <instance> |
| 4327 | raise <type> */ |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 4328 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4329 | if (PyExceptionClass_Check(exc)) { |
| 4330 | type = exc; |
| 4331 | value = PyObject_CallObject(exc, NULL); |
| 4332 | if (value == NULL) |
| 4333 | goto raise_error; |
Benjamin Peterson | 5afa03a | 2011-07-15 14:09:26 -0500 | [diff] [blame] | 4334 | if (!PyExceptionInstance_Check(value)) { |
| 4335 | PyErr_Format(PyExc_TypeError, |
| 4336 | "calling %R should have returned an instance of " |
| 4337 | "BaseException, not %R", |
| 4338 | type, Py_TYPE(value)); |
| 4339 | goto raise_error; |
| 4340 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4341 | } |
| 4342 | else if (PyExceptionInstance_Check(exc)) { |
| 4343 | value = exc; |
| 4344 | type = PyExceptionInstance_Class(exc); |
| 4345 | Py_INCREF(type); |
| 4346 | } |
| 4347 | else { |
| 4348 | /* Not something you can raise. You get an exception |
| 4349 | anyway, just not what you specified :-) */ |
| 4350 | Py_DECREF(exc); |
| 4351 | PyErr_SetString(PyExc_TypeError, |
| 4352 | "exceptions must derive from BaseException"); |
| 4353 | goto raise_error; |
| 4354 | } |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4355 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4356 | if (cause) { |
| 4357 | PyObject *fixed_cause; |
| 4358 | if (PyExceptionClass_Check(cause)) { |
| 4359 | fixed_cause = PyObject_CallObject(cause, NULL); |
| 4360 | if (fixed_cause == NULL) |
| 4361 | goto raise_error; |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 4362 | Py_DECREF(cause); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4363 | } |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 4364 | else if (PyExceptionInstance_Check(cause)) { |
| 4365 | fixed_cause = cause; |
| 4366 | } |
| 4367 | else if (cause == Py_None) { |
| 4368 | Py_DECREF(cause); |
| 4369 | fixed_cause = NULL; |
| 4370 | } |
| 4371 | else { |
| 4372 | PyErr_SetString(PyExc_TypeError, |
| 4373 | "exception causes must derive from " |
| 4374 | "BaseException"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4375 | goto raise_error; |
| 4376 | } |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 4377 | PyException_SetCause(value, fixed_cause); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4378 | } |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4379 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4380 | PyErr_SetObject(type, value); |
| 4381 | /* PyErr_SetObject incref's its arguments */ |
| 4382 | Py_XDECREF(value); |
| 4383 | Py_XDECREF(type); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4384 | return 0; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4385 | |
| 4386 | raise_error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4387 | Py_XDECREF(value); |
| 4388 | Py_XDECREF(type); |
| 4389 | Py_XDECREF(cause); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4390 | return 0; |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 4391 | } |
| 4392 | |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4393 | /* 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] | 4394 | sp). Return 1 for success, 0 if error. |
Antoine Pitrou | 9a2310d | 2008-07-25 22:39:39 +0000 | [diff] [blame] | 4395 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4396 | If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack |
| 4397 | with a variable target. |
| 4398 | */ |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4399 | |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 4400 | static int |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4401 | unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp) |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 4402 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4403 | int i = 0, j = 0; |
| 4404 | Py_ssize_t ll = 0; |
| 4405 | PyObject *it; /* iter(v) */ |
| 4406 | PyObject *w; |
| 4407 | PyObject *l = NULL; /* variable list */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4408 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4409 | assert(v != NULL); |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4410 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4411 | it = PyObject_GetIter(v); |
| 4412 | if (it == NULL) |
| 4413 | goto Error; |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4414 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4415 | for (; i < argcnt; i++) { |
| 4416 | w = PyIter_Next(it); |
| 4417 | if (w == NULL) { |
| 4418 | /* Iterator done, via error or exhaustion. */ |
| 4419 | if (!PyErr_Occurred()) { |
R David Murray | 4171bbe | 2015-04-15 17:08:45 -0400 | [diff] [blame] | 4420 | if (argcntafter == -1) { |
| 4421 | PyErr_Format(PyExc_ValueError, |
| 4422 | "not enough values to unpack (expected %d, got %d)", |
| 4423 | argcnt, i); |
| 4424 | } |
| 4425 | else { |
| 4426 | PyErr_Format(PyExc_ValueError, |
| 4427 | "not enough values to unpack " |
| 4428 | "(expected at least %d, got %d)", |
| 4429 | argcnt + argcntafter, i); |
| 4430 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4431 | } |
| 4432 | goto Error; |
| 4433 | } |
| 4434 | *--sp = w; |
| 4435 | } |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4436 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4437 | if (argcntafter == -1) { |
| 4438 | /* We better have exhausted the iterator now. */ |
| 4439 | w = PyIter_Next(it); |
| 4440 | if (w == NULL) { |
| 4441 | if (PyErr_Occurred()) |
| 4442 | goto Error; |
| 4443 | Py_DECREF(it); |
| 4444 | return 1; |
| 4445 | } |
| 4446 | Py_DECREF(w); |
R David Murray | 4171bbe | 2015-04-15 17:08:45 -0400 | [diff] [blame] | 4447 | PyErr_Format(PyExc_ValueError, |
| 4448 | "too many values to unpack (expected %d)", |
| 4449 | argcnt); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4450 | goto Error; |
| 4451 | } |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4452 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4453 | l = PySequence_List(it); |
| 4454 | if (l == NULL) |
| 4455 | goto Error; |
| 4456 | *--sp = l; |
| 4457 | i++; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4458 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4459 | ll = PyList_GET_SIZE(l); |
| 4460 | if (ll < argcntafter) { |
R David Murray | 4171bbe | 2015-04-15 17:08:45 -0400 | [diff] [blame] | 4461 | PyErr_Format(PyExc_ValueError, |
| 4462 | "not enough values to unpack (expected at least %d, got %zd)", |
| 4463 | argcnt + argcntafter, argcnt + ll); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4464 | goto Error; |
| 4465 | } |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4466 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4467 | /* Pop the "after-variable" args off the list. */ |
| 4468 | for (j = argcntafter; j > 0; j--, i++) { |
| 4469 | *--sp = PyList_GET_ITEM(l, ll - j); |
| 4470 | } |
| 4471 | /* Resize the list. */ |
| 4472 | Py_SIZE(l) = ll - argcntafter; |
| 4473 | Py_DECREF(it); |
| 4474 | return 1; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4475 | |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4476 | Error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4477 | for (; i > 0; i--, sp++) |
| 4478 | Py_DECREF(*sp); |
| 4479 | Py_XDECREF(it); |
| 4480 | return 0; |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 4481 | } |
| 4482 | |
| 4483 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 4484 | #ifdef LLTRACE |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4485 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4486 | prtrace(PyObject *v, const char *str) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4487 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4488 | printf("%s ", str); |
| 4489 | if (PyObject_Print(v, stdout, 0) != 0) |
| 4490 | PyErr_Clear(); /* Don't know what else to do */ |
| 4491 | printf("\n"); |
| 4492 | return 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4493 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4494 | #endif |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4495 | |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4496 | static void |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4497 | call_exc_trace(Py_tracefunc func, PyObject *self, |
| 4498 | PyThreadState *tstate, PyFrameObject *f) |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4499 | { |
Victor Stinner | aaa8ed8 | 2013-07-10 13:57:55 +0200 | [diff] [blame] | 4500 | PyObject *type, *value, *traceback, *orig_traceback, *arg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4501 | int err; |
Antoine Pitrou | 8933521 | 2013-11-23 14:05:23 +0100 | [diff] [blame] | 4502 | PyErr_Fetch(&type, &value, &orig_traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4503 | if (value == NULL) { |
| 4504 | value = Py_None; |
| 4505 | Py_INCREF(value); |
| 4506 | } |
Antoine Pitrou | 8933521 | 2013-11-23 14:05:23 +0100 | [diff] [blame] | 4507 | PyErr_NormalizeException(&type, &value, &orig_traceback); |
| 4508 | traceback = (orig_traceback != NULL) ? orig_traceback : Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4509 | arg = PyTuple_Pack(3, type, value, traceback); |
| 4510 | if (arg == NULL) { |
Antoine Pitrou | 8933521 | 2013-11-23 14:05:23 +0100 | [diff] [blame] | 4511 | PyErr_Restore(type, value, orig_traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4512 | return; |
| 4513 | } |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4514 | err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4515 | Py_DECREF(arg); |
| 4516 | if (err == 0) |
Victor Stinner | aaa8ed8 | 2013-07-10 13:57:55 +0200 | [diff] [blame] | 4517 | PyErr_Restore(type, value, orig_traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4518 | else { |
| 4519 | Py_XDECREF(type); |
| 4520 | Py_XDECREF(value); |
Victor Stinner | aaa8ed8 | 2013-07-10 13:57:55 +0200 | [diff] [blame] | 4521 | Py_XDECREF(orig_traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4522 | } |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4523 | } |
| 4524 | |
Amaury Forgeot d'Arc | f05149a | 2007-11-13 01:05:30 +0000 | [diff] [blame] | 4525 | static int |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4526 | call_trace_protected(Py_tracefunc func, PyObject *obj, |
| 4527 | PyThreadState *tstate, PyFrameObject *frame, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4528 | int what, PyObject *arg) |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 4529 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4530 | PyObject *type, *value, *traceback; |
| 4531 | int err; |
| 4532 | PyErr_Fetch(&type, &value, &traceback); |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4533 | err = call_trace(func, obj, tstate, frame, what, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4534 | if (err == 0) |
| 4535 | { |
| 4536 | PyErr_Restore(type, value, traceback); |
| 4537 | return 0; |
| 4538 | } |
| 4539 | else { |
| 4540 | Py_XDECREF(type); |
| 4541 | Py_XDECREF(value); |
| 4542 | Py_XDECREF(traceback); |
| 4543 | return -1; |
| 4544 | } |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 4545 | } |
| 4546 | |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4547 | static int |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4548 | call_trace(Py_tracefunc func, PyObject *obj, |
| 4549 | PyThreadState *tstate, PyFrameObject *frame, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4550 | int what, PyObject *arg) |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 4551 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4552 | int result; |
| 4553 | if (tstate->tracing) |
| 4554 | return 0; |
| 4555 | tstate->tracing++; |
| 4556 | tstate->use_tracing = 0; |
| 4557 | result = func(obj, frame, what, arg); |
| 4558 | tstate->use_tracing = ((tstate->c_tracefunc != NULL) |
| 4559 | || (tstate->c_profilefunc != NULL)); |
| 4560 | tstate->tracing--; |
| 4561 | return result; |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 4562 | } |
| 4563 | |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 4564 | PyObject * |
| 4565 | _PyEval_CallTracing(PyObject *func, PyObject *args) |
| 4566 | { |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4567 | PyThreadState *tstate = PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4568 | int save_tracing = tstate->tracing; |
| 4569 | int save_use_tracing = tstate->use_tracing; |
| 4570 | PyObject *result; |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 4571 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4572 | tstate->tracing = 0; |
| 4573 | tstate->use_tracing = ((tstate->c_tracefunc != NULL) |
| 4574 | || (tstate->c_profilefunc != NULL)); |
| 4575 | result = PyObject_Call(func, args, NULL); |
| 4576 | tstate->tracing = save_tracing; |
| 4577 | tstate->use_tracing = save_use_tracing; |
| 4578 | return result; |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 4579 | } |
| 4580 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 4581 | /* 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] | 4582 | static int |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 4583 | maybe_call_line_trace(Py_tracefunc func, PyObject *obj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4584 | PyThreadState *tstate, PyFrameObject *frame, |
| 4585 | int *instr_lb, int *instr_ub, int *instr_prev) |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 4586 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4587 | int result = 0; |
| 4588 | int line = frame->f_lineno; |
Michael W. Hudson | 006c752 | 2002-11-08 13:08:46 +0000 | [diff] [blame] | 4589 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4590 | /* If the last instruction executed isn't in the current |
| 4591 | instruction window, reset the window. |
| 4592 | */ |
| 4593 | if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) { |
| 4594 | PyAddrPair bounds; |
| 4595 | line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti, |
| 4596 | &bounds); |
| 4597 | *instr_lb = bounds.ap_lower; |
| 4598 | *instr_ub = bounds.ap_upper; |
| 4599 | } |
| 4600 | /* If the last instruction falls at the start of a line or if |
| 4601 | it represents a jump backwards, update the frame's line |
| 4602 | number and call the trace function. */ |
| 4603 | if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) { |
| 4604 | frame->f_lineno = line; |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4605 | result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4606 | } |
| 4607 | *instr_prev = frame->f_lasti; |
| 4608 | return result; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 4609 | } |
| 4610 | |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 4611 | void |
| 4612 | PyEval_SetProfile(Py_tracefunc func, PyObject *arg) |
Fred Drake | d083839 | 2001-06-16 21:02:31 +0000 | [diff] [blame] | 4613 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4614 | PyThreadState *tstate = PyThreadState_GET(); |
| 4615 | PyObject *temp = tstate->c_profileobj; |
| 4616 | Py_XINCREF(arg); |
| 4617 | tstate->c_profilefunc = NULL; |
| 4618 | tstate->c_profileobj = NULL; |
| 4619 | /* Must make sure that tracing is not ignored if 'temp' is freed */ |
| 4620 | tstate->use_tracing = tstate->c_tracefunc != NULL; |
| 4621 | Py_XDECREF(temp); |
| 4622 | tstate->c_profilefunc = func; |
| 4623 | tstate->c_profileobj = arg; |
| 4624 | /* Flag that tracing or profiling is turned on */ |
| 4625 | tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL); |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 4626 | } |
| 4627 | |
| 4628 | void |
| 4629 | PyEval_SetTrace(Py_tracefunc func, PyObject *arg) |
| 4630 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4631 | PyThreadState *tstate = PyThreadState_GET(); |
| 4632 | PyObject *temp = tstate->c_traceobj; |
| 4633 | _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL); |
| 4634 | Py_XINCREF(arg); |
| 4635 | tstate->c_tracefunc = NULL; |
| 4636 | tstate->c_traceobj = NULL; |
| 4637 | /* Must make sure that profiling is not ignored if 'temp' is freed */ |
| 4638 | tstate->use_tracing = tstate->c_profilefunc != NULL; |
| 4639 | Py_XDECREF(temp); |
| 4640 | tstate->c_tracefunc = func; |
| 4641 | tstate->c_traceobj = arg; |
| 4642 | /* Flag that tracing or profiling is turned on */ |
| 4643 | tstate->use_tracing = ((func != NULL) |
| 4644 | || (tstate->c_profilefunc != NULL)); |
Fred Drake | d083839 | 2001-06-16 21:02:31 +0000 | [diff] [blame] | 4645 | } |
| 4646 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4647 | void |
Yury Selivanov | d8cf382 | 2015-06-01 12:15:23 -0400 | [diff] [blame] | 4648 | _PyEval_SetCoroutineWrapper(PyObject *wrapper) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4649 | { |
| 4650 | PyThreadState *tstate = PyThreadState_GET(); |
| 4651 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4652 | Py_XINCREF(wrapper); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 4653 | Py_XSETREF(tstate->coroutine_wrapper, wrapper); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4654 | } |
| 4655 | |
| 4656 | PyObject * |
Yury Selivanov | d8cf382 | 2015-06-01 12:15:23 -0400 | [diff] [blame] | 4657 | _PyEval_GetCoroutineWrapper(void) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4658 | { |
| 4659 | PyThreadState *tstate = PyThreadState_GET(); |
| 4660 | return tstate->coroutine_wrapper; |
| 4661 | } |
| 4662 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4663 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4664 | PyEval_GetBuiltins(void) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 4665 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4666 | PyFrameObject *current_frame = PyEval_GetFrame(); |
| 4667 | if (current_frame == NULL) |
| 4668 | return PyThreadState_GET()->interp->builtins; |
| 4669 | else |
| 4670 | return current_frame->f_builtins; |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 4671 | } |
| 4672 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4673 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4674 | PyEval_GetLocals(void) |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 4675 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4676 | PyFrameObject *current_frame = PyEval_GetFrame(); |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 4677 | if (current_frame == NULL) { |
| 4678 | PyErr_SetString(PyExc_SystemError, "frame does not exist"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4679 | return NULL; |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 4680 | } |
| 4681 | |
| 4682 | if (PyFrame_FastToLocalsWithError(current_frame) < 0) |
| 4683 | return NULL; |
| 4684 | |
| 4685 | assert(current_frame->f_locals != NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4686 | return current_frame->f_locals; |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 4687 | } |
| 4688 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4689 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4690 | PyEval_GetGlobals(void) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4691 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4692 | PyFrameObject *current_frame = PyEval_GetFrame(); |
| 4693 | if (current_frame == NULL) |
| 4694 | return NULL; |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 4695 | |
| 4696 | assert(current_frame->f_globals != NULL); |
| 4697 | return current_frame->f_globals; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4698 | } |
| 4699 | |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 4700 | PyFrameObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4701 | PyEval_GetFrame(void) |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 4702 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4703 | PyThreadState *tstate = PyThreadState_GET(); |
| 4704 | return _PyThreadState_GetFrame(tstate); |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 4705 | } |
| 4706 | |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 4707 | int |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 4708 | PyEval_MergeCompilerFlags(PyCompilerFlags *cf) |
Jeremy Hylton | 061d106 | 2001-03-22 02:32:48 +0000 | [diff] [blame] | 4709 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4710 | PyFrameObject *current_frame = PyEval_GetFrame(); |
| 4711 | int result = cf->cf_flags != 0; |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 4712 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4713 | if (current_frame != NULL) { |
| 4714 | const int codeflags = current_frame->f_code->co_flags; |
| 4715 | const int compilerflags = codeflags & PyCF_MASK; |
| 4716 | if (compilerflags) { |
| 4717 | result = 1; |
| 4718 | cf->cf_flags |= compilerflags; |
| 4719 | } |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 4720 | #if 0 /* future keyword */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4721 | if (codeflags & CO_GENERATOR_ALLOWED) { |
| 4722 | result = 1; |
| 4723 | cf->cf_flags |= CO_GENERATOR_ALLOWED; |
| 4724 | } |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 4725 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4726 | } |
| 4727 | return result; |
Jeremy Hylton | 061d106 | 2001-03-22 02:32:48 +0000 | [diff] [blame] | 4728 | } |
| 4729 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4730 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4731 | /* External interface to call any callable object. |
Antoine Pitrou | 8689a10 | 2010-04-01 16:53:15 +0000 | [diff] [blame] | 4732 | 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] | 4733 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4734 | PyObject * |
Victor Stinner | 8a31c82 | 2016-08-19 17:12:23 +0200 | [diff] [blame] | 4735 | PyEval_CallObjectWithKeywords(PyObject *func, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4736 | { |
Victor Stinner | 59b356d | 2015-03-16 11:52:32 +0100 | [diff] [blame] | 4737 | #ifdef Py_DEBUG |
| 4738 | /* PyEval_CallObjectWithKeywords() must not be called with an exception |
| 4739 | set. It raises a new exception if parameters are invalid or if |
| 4740 | PyTuple_New() fails, and so the original exception is lost. */ |
| 4741 | assert(!PyErr_Occurred()); |
| 4742 | #endif |
| 4743 | |
Victor Stinner | 8a31c82 | 2016-08-19 17:12:23 +0200 | [diff] [blame] | 4744 | if (args == NULL) { |
Victor Stinner | 155ea65 | 2016-08-22 23:26:00 +0200 | [diff] [blame] | 4745 | return _PyObject_FastCallDict(func, NULL, 0, kwargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4746 | } |
Victor Stinner | 155ea65 | 2016-08-22 23:26:00 +0200 | [diff] [blame] | 4747 | |
| 4748 | if (!PyTuple_Check(args)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4749 | PyErr_SetString(PyExc_TypeError, |
| 4750 | "argument list must be a tuple"); |
| 4751 | return NULL; |
| 4752 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4753 | |
Victor Stinner | 8a31c82 | 2016-08-19 17:12:23 +0200 | [diff] [blame] | 4754 | if (kwargs != NULL && !PyDict_Check(kwargs)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4755 | PyErr_SetString(PyExc_TypeError, |
| 4756 | "keyword list must be a dictionary"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4757 | return NULL; |
| 4758 | } |
Guido van Rossum | e3e61c1 | 1995-08-04 04:14:47 +0000 | [diff] [blame] | 4759 | |
Victor Stinner | 6e2333d | 2016-08-23 00:25:01 +0200 | [diff] [blame] | 4760 | return PyObject_Call(func, args, kwargs); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4761 | } |
| 4762 | |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 4763 | const char * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4764 | PyEval_GetFuncName(PyObject *func) |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4765 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4766 | if (PyMethod_Check(func)) |
| 4767 | return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func)); |
| 4768 | else if (PyFunction_Check(func)) |
| 4769 | return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name); |
| 4770 | else if (PyCFunction_Check(func)) |
| 4771 | return ((PyCFunctionObject*)func)->m_ml->ml_name; |
| 4772 | else |
| 4773 | return func->ob_type->tp_name; |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4774 | } |
| 4775 | |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 4776 | const char * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4777 | PyEval_GetFuncDesc(PyObject *func) |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4778 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4779 | if (PyMethod_Check(func)) |
| 4780 | return "()"; |
| 4781 | else if (PyFunction_Check(func)) |
| 4782 | return "()"; |
| 4783 | else if (PyCFunction_Check(func)) |
| 4784 | return "()"; |
| 4785 | else |
| 4786 | return " object"; |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4787 | } |
| 4788 | |
Neal Norwitz | addfe0c | 2002-11-10 14:33:26 +0000 | [diff] [blame] | 4789 | static void |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4790 | err_args(PyObject *func, int flags, Py_ssize_t nargs) |
Jeremy Hylton | 192690e | 2002-08-16 18:36:11 +0000 | [diff] [blame] | 4791 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4792 | if (flags & METH_NOARGS) |
| 4793 | PyErr_Format(PyExc_TypeError, |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4794 | "%.200s() takes no arguments (%zd given)", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4795 | ((PyCFunctionObject *)func)->m_ml->ml_name, |
| 4796 | nargs); |
| 4797 | else |
| 4798 | PyErr_Format(PyExc_TypeError, |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4799 | "%.200s() takes exactly one argument (%zd given)", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4800 | ((PyCFunctionObject *)func)->m_ml->ml_name, |
| 4801 | nargs); |
Jeremy Hylton | 192690e | 2002-08-16 18:36:11 +0000 | [diff] [blame] | 4802 | } |
| 4803 | |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 4804 | #define C_TRACE(x, call) \ |
Nicholas Bastin | d858a77 | 2004-06-25 23:31:06 +0000 | [diff] [blame] | 4805 | if (tstate->use_tracing && tstate->c_profilefunc) { \ |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4806 | if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \ |
| 4807 | tstate, tstate->frame, \ |
| 4808 | PyTrace_C_CALL, func)) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4809 | x = NULL; \ |
| 4810 | } \ |
| 4811 | else { \ |
| 4812 | x = call; \ |
| 4813 | if (tstate->c_profilefunc != NULL) { \ |
| 4814 | if (x == NULL) { \ |
| 4815 | call_trace_protected(tstate->c_profilefunc, \ |
| 4816 | tstate->c_profileobj, \ |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4817 | tstate, tstate->frame, \ |
| 4818 | PyTrace_C_EXCEPTION, func); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4819 | /* XXX should pass (type, value, tb) */ \ |
| 4820 | } else { \ |
| 4821 | if (call_trace(tstate->c_profilefunc, \ |
| 4822 | tstate->c_profileobj, \ |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4823 | tstate, tstate->frame, \ |
| 4824 | PyTrace_C_RETURN, func)) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4825 | Py_DECREF(x); \ |
| 4826 | x = NULL; \ |
| 4827 | } \ |
| 4828 | } \ |
| 4829 | } \ |
| 4830 | } \ |
Nicholas Bastin | d858a77 | 2004-06-25 23:31:06 +0000 | [diff] [blame] | 4831 | } else { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4832 | x = call; \ |
| 4833 | } |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 4834 | |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 4835 | static PyObject * |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 4836 | call_function(PyObject ***pp_stack, int oparg |
| 4837 | #ifdef WITH_TSC |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4838 | , uint64* pintr0, uint64* pintr1 |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 4839 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4840 | ) |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 4841 | { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4842 | Py_ssize_t nargs = oparg & 0xff; |
| 4843 | Py_ssize_t nkwargs = (oparg>>8) & 0xff; |
| 4844 | int n = nargs + 2 * nkwargs; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4845 | PyObject **pfunc = (*pp_stack) - n - 1; |
| 4846 | PyObject *func = *pfunc; |
| 4847 | PyObject *x, *w; |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 4848 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4849 | /* Always dispatch PyCFunction first, because these are |
| 4850 | presumed to be the most frequent callable object. |
| 4851 | */ |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4852 | if (PyCFunction_Check(func) && nkwargs == 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4853 | int flags = PyCFunction_GET_FLAGS(func); |
| 4854 | PyThreadState *tstate = PyThreadState_GET(); |
Raymond Hettinger | a7f56bc | 2004-06-26 04:34:33 +0000 | [diff] [blame] | 4855 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4856 | PCALL(PCALL_CFUNCTION); |
| 4857 | if (flags & (METH_NOARGS | METH_O)) { |
| 4858 | PyCFunction meth = PyCFunction_GET_FUNCTION(func); |
| 4859 | PyObject *self = PyCFunction_GET_SELF(func); |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4860 | if (flags & METH_NOARGS && nargs == 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4861 | C_TRACE(x, (*meth)(self,NULL)); |
Victor Stinner | 4a7cc88 | 2015-03-06 23:35:27 +0100 | [diff] [blame] | 4862 | |
Victor Stinner | efde146 | 2015-03-21 15:04:43 +0100 | [diff] [blame] | 4863 | x = _Py_CheckFunctionResult(func, x, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4864 | } |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4865 | else if (flags & METH_O && nargs == 1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4866 | PyObject *arg = EXT_POP(*pp_stack); |
| 4867 | C_TRACE(x, (*meth)(self,arg)); |
| 4868 | Py_DECREF(arg); |
Victor Stinner | 4a7cc88 | 2015-03-06 23:35:27 +0100 | [diff] [blame] | 4869 | |
Victor Stinner | efde146 | 2015-03-21 15:04:43 +0100 | [diff] [blame] | 4870 | x = _Py_CheckFunctionResult(func, x, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4871 | } |
| 4872 | else { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4873 | err_args(func, flags, nargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4874 | x = NULL; |
| 4875 | } |
| 4876 | } |
| 4877 | else { |
| 4878 | PyObject *callargs; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4879 | callargs = load_args(pp_stack, nargs); |
Victor Stinner | 0ff0f54 | 2013-07-08 22:27:42 +0200 | [diff] [blame] | 4880 | if (callargs != NULL) { |
| 4881 | READ_TIMESTAMP(*pintr0); |
| 4882 | C_TRACE(x, PyCFunction_Call(func,callargs,NULL)); |
| 4883 | READ_TIMESTAMP(*pintr1); |
| 4884 | Py_XDECREF(callargs); |
| 4885 | } |
| 4886 | else { |
| 4887 | x = NULL; |
| 4888 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4889 | } |
Victor Stinner | 4a7cc88 | 2015-03-06 23:35:27 +0100 | [diff] [blame] | 4890 | } |
| 4891 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4892 | if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) { |
| 4893 | /* optimize access to bound methods */ |
| 4894 | PyObject *self = PyMethod_GET_SELF(func); |
| 4895 | PCALL(PCALL_METHOD); |
| 4896 | PCALL(PCALL_BOUND_METHOD); |
| 4897 | Py_INCREF(self); |
| 4898 | func = PyMethod_GET_FUNCTION(func); |
| 4899 | Py_INCREF(func); |
Serhiy Storchaka | 57a01d3 | 2016-04-10 18:05:40 +0300 | [diff] [blame] | 4900 | Py_SETREF(*pfunc, self); |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4901 | nargs++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4902 | n++; |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4903 | } |
| 4904 | else { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4905 | Py_INCREF(func); |
| 4906 | } |
| 4907 | READ_TIMESTAMP(*pintr0); |
| 4908 | if (PyFunction_Check(func)) { |
Victor Stinner | e90bdb1 | 2016-08-25 23:26:50 +0200 | [diff] [blame] | 4909 | x = fast_function(func, (*pp_stack) - n, nargs, nkwargs); |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4910 | } |
| 4911 | else { |
| 4912 | x = do_call(func, pp_stack, nargs, nkwargs); |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4913 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4914 | READ_TIMESTAMP(*pintr1); |
| 4915 | Py_DECREF(func); |
Victor Stinner | 4a7cc88 | 2015-03-06 23:35:27 +0100 | [diff] [blame] | 4916 | |
| 4917 | assert((x != NULL) ^ (PyErr_Occurred() != NULL)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4918 | } |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 4919 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4920 | /* Clear the stack of the function object. Also removes |
| 4921 | the arguments in case they weren't consumed already |
Victor Stinner | e90bdb1 | 2016-08-25 23:26:50 +0200 | [diff] [blame] | 4922 | (fast_function() and err_args() leave them on the stack). |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4923 | */ |
| 4924 | while ((*pp_stack) > pfunc) { |
| 4925 | w = EXT_POP(*pp_stack); |
| 4926 | Py_DECREF(w); |
| 4927 | PCALL(PCALL_POP); |
| 4928 | } |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 4929 | |
Victor Stinner | 4a7cc88 | 2015-03-06 23:35:27 +0100 | [diff] [blame] | 4930 | assert((x != NULL) ^ (PyErr_Occurred() != NULL)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4931 | return x; |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 4932 | } |
| 4933 | |
Victor Stinner | e90bdb1 | 2016-08-25 23:26:50 +0200 | [diff] [blame] | 4934 | /* The fast_function() function optimize calls for which no argument |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4935 | tuple is necessary; the objects are passed directly from the stack. |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4936 | For the simplest case -- a function that takes only positional |
| 4937 | arguments and is called with only positional arguments -- it |
| 4938 | inlines the most primitive frame setup code from |
| 4939 | PyEval_EvalCodeEx(), which vastly reduces the checks that must be |
| 4940 | done before evaluating the frame. |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4941 | */ |
| 4942 | |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4943 | static PyObject* |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4944 | _PyFunction_FastCallNoKw(PyCodeObject *co, PyObject **args, Py_ssize_t nargs, |
Victor Stinner | 2eedc11 | 2016-08-22 12:29:42 +0200 | [diff] [blame] | 4945 | PyObject *globals) |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4946 | { |
| 4947 | PyFrameObject *f; |
| 4948 | PyThreadState *tstate = PyThreadState_GET(); |
| 4949 | PyObject **fastlocals; |
| 4950 | Py_ssize_t i; |
| 4951 | PyObject *result; |
| 4952 | |
| 4953 | PCALL(PCALL_FASTER_FUNCTION); |
| 4954 | assert(globals != NULL); |
| 4955 | /* XXX Perhaps we should create a specialized |
| 4956 | PyFrame_New() that doesn't take locals, but does |
| 4957 | take builtins without sanity checking them. |
| 4958 | */ |
| 4959 | assert(tstate != NULL); |
| 4960 | f = PyFrame_New(tstate, co, globals, NULL); |
| 4961 | if (f == NULL) { |
| 4962 | return NULL; |
| 4963 | } |
| 4964 | |
| 4965 | fastlocals = f->f_localsplus; |
| 4966 | |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4967 | for (i = 0; i < nargs; i++) { |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4968 | Py_INCREF(*args); |
| 4969 | fastlocals[i] = *args++; |
| 4970 | } |
| 4971 | result = PyEval_EvalFrameEx(f,0); |
| 4972 | |
| 4973 | ++tstate->recursion_depth; |
| 4974 | Py_DECREF(f); |
| 4975 | --tstate->recursion_depth; |
| 4976 | |
| 4977 | return result; |
| 4978 | } |
| 4979 | |
Victor Stinner | 2eedc11 | 2016-08-22 12:29:42 +0200 | [diff] [blame] | 4980 | /* Similar to _PyFunction_FastCall() but keywords are passed a (key, value) |
| 4981 | pairs in stack */ |
Victor Stinner | e90bdb1 | 2016-08-25 23:26:50 +0200 | [diff] [blame] | 4982 | static PyObject * |
| 4983 | fast_function(PyObject *func, PyObject **stack, Py_ssize_t nargs, Py_ssize_t nkwargs) |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4984 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4985 | PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func); |
| 4986 | PyObject *globals = PyFunction_GET_GLOBALS(func); |
| 4987 | PyObject *argdefs = PyFunction_GET_DEFAULTS(func); |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4988 | PyObject *kwdefs, *closure, *name, *qualname; |
| 4989 | PyObject **d; |
| 4990 | int nd; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4991 | |
Victor Stinner | 577e1f8 | 2016-08-25 00:29:32 +0200 | [diff] [blame] | 4992 | assert(func != NULL); |
| 4993 | assert(nargs >= 0); |
| 4994 | assert(nkwargs >= 0); |
| 4995 | assert((nargs == 0 && nkwargs == 0) || stack != NULL); |
| 4996 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4997 | PCALL(PCALL_FUNCTION); |
| 4998 | PCALL(PCALL_FAST_FUNCTION); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4999 | |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 5000 | if (co->co_kwonlyargcount == 0 && nkwargs == 0 && |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 5001 | co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) |
| 5002 | { |
Victor Stinner | 2eedc11 | 2016-08-22 12:29:42 +0200 | [diff] [blame] | 5003 | if (argdefs == NULL && co->co_argcount == nargs) { |
| 5004 | return _PyFunction_FastCallNoKw(co, stack, nargs, globals); |
| 5005 | } |
| 5006 | else if (nargs == 0 && argdefs != NULL |
| 5007 | && co->co_argcount == Py_SIZE(argdefs)) { |
| 5008 | /* function called with no arguments, but all parameters have |
| 5009 | a default value: use default values as arguments .*/ |
| 5010 | stack = &PyTuple_GET_ITEM(argdefs, 0); |
| 5011 | return _PyFunction_FastCallNoKw(co, stack, Py_SIZE(argdefs), |
| 5012 | globals); |
| 5013 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5014 | } |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 5015 | |
| 5016 | kwdefs = PyFunction_GET_KW_DEFAULTS(func); |
| 5017 | closure = PyFunction_GET_CLOSURE(func); |
| 5018 | name = ((PyFunctionObject *)func) -> func_name; |
| 5019 | qualname = ((PyFunctionObject *)func) -> func_qualname; |
| 5020 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5021 | if (argdefs != NULL) { |
| 5022 | d = &PyTuple_GET_ITEM(argdefs, 0); |
| 5023 | nd = Py_SIZE(argdefs); |
| 5024 | } |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 5025 | else { |
| 5026 | d = NULL; |
| 5027 | nd = 0; |
| 5028 | } |
| 5029 | return _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL, |
Victor Stinner | 2eedc11 | 2016-08-22 12:29:42 +0200 | [diff] [blame] | 5030 | stack, nargs, |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 5031 | stack + nargs, nkwargs, |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 5032 | d, nd, kwdefs, |
| 5033 | closure, name, qualname); |
| 5034 | } |
| 5035 | |
| 5036 | PyObject * |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 5037 | _PyFunction_FastCallDict(PyObject *func, PyObject **args, Py_ssize_t nargs, |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 5038 | PyObject *kwargs) |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 5039 | { |
| 5040 | PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func); |
| 5041 | PyObject *globals = PyFunction_GET_GLOBALS(func); |
| 5042 | PyObject *argdefs = PyFunction_GET_DEFAULTS(func); |
| 5043 | PyObject *kwdefs, *closure, *name, *qualname; |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 5044 | PyObject *kwtuple, **k; |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 5045 | PyObject **d; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 5046 | Py_ssize_t nd, nk; |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 5047 | PyObject *result; |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 5048 | |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 5049 | assert(func != NULL); |
| 5050 | assert(nargs >= 0); |
| 5051 | assert(nargs == 0 || args != NULL); |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 5052 | assert(kwargs == NULL || PyDict_Check(kwargs)); |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 5053 | |
Victor Stinner | 577e1f8 | 2016-08-25 00:29:32 +0200 | [diff] [blame] | 5054 | PCALL(PCALL_FUNCTION); |
| 5055 | PCALL(PCALL_FAST_FUNCTION); |
| 5056 | |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 5057 | if (co->co_kwonlyargcount == 0 && |
| 5058 | (kwargs == NULL || PyDict_Size(kwargs) == 0) && |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 5059 | co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) |
| 5060 | { |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 5061 | /* Fast paths */ |
Victor Stinner | 2eedc11 | 2016-08-22 12:29:42 +0200 | [diff] [blame] | 5062 | if (argdefs == NULL && co->co_argcount == nargs) { |
| 5063 | return _PyFunction_FastCallNoKw(co, args, nargs, globals); |
| 5064 | } |
| 5065 | else if (nargs == 0 && argdefs != NULL |
| 5066 | && co->co_argcount == Py_SIZE(argdefs)) { |
| 5067 | /* function called with no arguments, but all parameters have |
| 5068 | a default value: use default values as arguments .*/ |
| 5069 | args = &PyTuple_GET_ITEM(argdefs, 0); |
| 5070 | return _PyFunction_FastCallNoKw(co, args, Py_SIZE(argdefs), |
| 5071 | globals); |
| 5072 | } |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 5073 | } |
| 5074 | |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 5075 | if (kwargs != NULL) { |
| 5076 | Py_ssize_t pos, i; |
| 5077 | nk = PyDict_Size(kwargs); |
| 5078 | |
| 5079 | kwtuple = PyTuple_New(2 * nk); |
| 5080 | if (kwtuple == NULL) { |
| 5081 | return NULL; |
| 5082 | } |
| 5083 | |
| 5084 | k = &PyTuple_GET_ITEM(kwtuple, 0); |
| 5085 | pos = i = 0; |
| 5086 | while (PyDict_Next(kwargs, &pos, &k[i], &k[i+1])) { |
| 5087 | Py_INCREF(k[i]); |
| 5088 | Py_INCREF(k[i+1]); |
| 5089 | i += 2; |
| 5090 | } |
| 5091 | nk = i / 2; |
| 5092 | } |
| 5093 | else { |
| 5094 | kwtuple = NULL; |
| 5095 | k = NULL; |
| 5096 | nk = 0; |
| 5097 | } |
| 5098 | |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 5099 | kwdefs = PyFunction_GET_KW_DEFAULTS(func); |
| 5100 | closure = PyFunction_GET_CLOSURE(func); |
| 5101 | name = ((PyFunctionObject *)func) -> func_name; |
| 5102 | qualname = ((PyFunctionObject *)func) -> func_qualname; |
| 5103 | |
| 5104 | if (argdefs != NULL) { |
| 5105 | d = &PyTuple_GET_ITEM(argdefs, 0); |
| 5106 | nd = Py_SIZE(argdefs); |
| 5107 | } |
| 5108 | else { |
| 5109 | d = NULL; |
| 5110 | nd = 0; |
| 5111 | } |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 5112 | |
| 5113 | result = _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL, |
| 5114 | args, nargs, |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 5115 | k, nk, |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 5116 | d, nd, kwdefs, |
| 5117 | closure, name, qualname); |
| 5118 | Py_XDECREF(kwtuple); |
| 5119 | return result; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 5120 | } |
| 5121 | |
| 5122 | static PyObject * |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 5123 | update_keyword_args(PyObject *orig_kwdict, Py_ssize_t nk, PyObject ***pp_stack, |
Ka-Ping Yee | 2057970 | 2001-01-15 22:14:16 +0000 | [diff] [blame] | 5124 | PyObject *func) |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 5125 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5126 | PyObject *kwdict = NULL; |
| 5127 | if (orig_kwdict == NULL) |
| 5128 | kwdict = PyDict_New(); |
| 5129 | else { |
| 5130 | kwdict = PyDict_Copy(orig_kwdict); |
| 5131 | Py_DECREF(orig_kwdict); |
| 5132 | } |
| 5133 | if (kwdict == NULL) |
| 5134 | return NULL; |
| 5135 | while (--nk >= 0) { |
| 5136 | int err; |
| 5137 | PyObject *value = EXT_POP(*pp_stack); |
| 5138 | PyObject *key = EXT_POP(*pp_stack); |
| 5139 | if (PyDict_GetItem(kwdict, key) != NULL) { |
| 5140 | PyErr_Format(PyExc_TypeError, |
| 5141 | "%.200s%s got multiple values " |
| 5142 | "for keyword argument '%U'", |
| 5143 | PyEval_GetFuncName(func), |
| 5144 | PyEval_GetFuncDesc(func), |
| 5145 | key); |
| 5146 | Py_DECREF(key); |
| 5147 | Py_DECREF(value); |
| 5148 | Py_DECREF(kwdict); |
| 5149 | return NULL; |
| 5150 | } |
| 5151 | err = PyDict_SetItem(kwdict, key, value); |
| 5152 | Py_DECREF(key); |
| 5153 | Py_DECREF(value); |
| 5154 | if (err) { |
| 5155 | Py_DECREF(kwdict); |
| 5156 | return NULL; |
| 5157 | } |
| 5158 | } |
| 5159 | return kwdict; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 5160 | } |
| 5161 | |
| 5162 | static PyObject * |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 5163 | update_star_args(Py_ssize_t nstack, Py_ssize_t nstar, PyObject *stararg, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5164 | PyObject ***pp_stack) |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 5165 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5166 | PyObject *callargs, *w; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 5167 | |
Serhiy Storchaka | 79d6e8d | 2016-04-19 23:37:17 +0300 | [diff] [blame] | 5168 | if (!nstack) { |
| 5169 | if (!stararg) { |
| 5170 | /* There are no positional arguments on the stack and there is no |
| 5171 | sequence to be unpacked. */ |
| 5172 | return PyTuple_New(0); |
| 5173 | } |
| 5174 | if (PyTuple_CheckExact(stararg)) { |
| 5175 | /* No arguments are passed on the stack and the sequence is not a |
| 5176 | tuple subclass so we can just pass the stararg tuple directly |
| 5177 | to the function. */ |
| 5178 | Py_INCREF(stararg); |
| 5179 | return stararg; |
| 5180 | } |
| 5181 | } |
| 5182 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5183 | callargs = PyTuple_New(nstack + nstar); |
| 5184 | if (callargs == NULL) { |
| 5185 | return NULL; |
| 5186 | } |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 5187 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5188 | if (nstar) { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 5189 | Py_ssize_t i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5190 | for (i = 0; i < nstar; i++) { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 5191 | PyObject *arg = PyTuple_GET_ITEM(stararg, i); |
| 5192 | Py_INCREF(arg); |
| 5193 | PyTuple_SET_ITEM(callargs, nstack + i, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5194 | } |
| 5195 | } |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 5196 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5197 | while (--nstack >= 0) { |
| 5198 | w = EXT_POP(*pp_stack); |
| 5199 | PyTuple_SET_ITEM(callargs, nstack, w); |
| 5200 | } |
| 5201 | return callargs; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 5202 | } |
| 5203 | |
| 5204 | static PyObject * |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 5205 | load_args(PyObject ***pp_stack, Py_ssize_t na) |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 5206 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5207 | PyObject *args = PyTuple_New(na); |
| 5208 | PyObject *w; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 5209 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5210 | if (args == NULL) |
| 5211 | return NULL; |
| 5212 | while (--na >= 0) { |
| 5213 | w = EXT_POP(*pp_stack); |
| 5214 | PyTuple_SET_ITEM(args, na, w); |
| 5215 | } |
| 5216 | return args; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 5217 | } |
| 5218 | |
| 5219 | static PyObject * |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 5220 | do_call(PyObject *func, PyObject ***pp_stack, Py_ssize_t nargs, Py_ssize_t nkwargs) |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 5221 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5222 | PyObject *callargs = NULL; |
| 5223 | PyObject *kwdict = NULL; |
| 5224 | PyObject *result = NULL; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 5225 | |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 5226 | if (nkwargs > 0) { |
| 5227 | kwdict = update_keyword_args(NULL, nkwargs, pp_stack, func); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5228 | if (kwdict == NULL) |
| 5229 | goto call_fail; |
| 5230 | } |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 5231 | callargs = load_args(pp_stack, nargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5232 | if (callargs == NULL) |
| 5233 | goto call_fail; |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 5234 | #ifdef CALL_PROFILE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5235 | /* At this point, we have to look at the type of func to |
| 5236 | update the call stats properly. Do it here so as to avoid |
| 5237 | exposing the call stats machinery outside ceval.c |
| 5238 | */ |
| 5239 | if (PyFunction_Check(func)) |
| 5240 | PCALL(PCALL_FUNCTION); |
| 5241 | else if (PyMethod_Check(func)) |
| 5242 | PCALL(PCALL_METHOD); |
| 5243 | else if (PyType_Check(func)) |
| 5244 | PCALL(PCALL_TYPE); |
| 5245 | else if (PyCFunction_Check(func)) |
| 5246 | PCALL(PCALL_CFUNCTION); |
| 5247 | else |
| 5248 | PCALL(PCALL_OTHER); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 5249 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5250 | if (PyCFunction_Check(func)) { |
| 5251 | PyThreadState *tstate = PyThreadState_GET(); |
| 5252 | C_TRACE(result, PyCFunction_Call(func, callargs, kwdict)); |
| 5253 | } |
| 5254 | else |
| 5255 | result = PyObject_Call(func, callargs, kwdict); |
Thomas Wouters | 7ce29ca | 2007-09-19 21:56:32 +0000 | [diff] [blame] | 5256 | call_fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5257 | Py_XDECREF(callargs); |
| 5258 | Py_XDECREF(kwdict); |
| 5259 | return result; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 5260 | } |
| 5261 | |
| 5262 | static PyObject * |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 5263 | ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, |
| 5264 | Py_ssize_t nargs, Py_ssize_t nkwargs) |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 5265 | { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 5266 | Py_ssize_t nstar; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5267 | PyObject *callargs = NULL; |
| 5268 | PyObject *stararg = NULL; |
| 5269 | PyObject *kwdict = NULL; |
| 5270 | PyObject *result = NULL; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 5271 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5272 | if (flags & CALL_FLAG_KW) { |
| 5273 | kwdict = EXT_POP(*pp_stack); |
Serhiy Storchaka | ce41287 | 2016-05-08 23:36:44 +0300 | [diff] [blame] | 5274 | if (!PyDict_CheckExact(kwdict)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5275 | PyObject *d; |
| 5276 | d = PyDict_New(); |
| 5277 | if (d == NULL) |
| 5278 | goto ext_call_fail; |
| 5279 | if (PyDict_Update(d, kwdict) != 0) { |
| 5280 | Py_DECREF(d); |
| 5281 | /* PyDict_Update raises attribute |
| 5282 | * error (percolated from an attempt |
| 5283 | * to get 'keys' attribute) instead of |
| 5284 | * a type error if its second argument |
| 5285 | * is not a mapping. |
| 5286 | */ |
| 5287 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 5288 | PyErr_Format(PyExc_TypeError, |
| 5289 | "%.200s%.200s argument after ** " |
| 5290 | "must be a mapping, not %.200s", |
| 5291 | PyEval_GetFuncName(func), |
| 5292 | PyEval_GetFuncDesc(func), |
| 5293 | kwdict->ob_type->tp_name); |
| 5294 | } |
| 5295 | goto ext_call_fail; |
| 5296 | } |
| 5297 | Py_DECREF(kwdict); |
| 5298 | kwdict = d; |
| 5299 | } |
| 5300 | } |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 5301 | |
| 5302 | if (nkwargs > 0) { |
| 5303 | kwdict = update_keyword_args(kwdict, nkwargs, pp_stack, func); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 5304 | if (kwdict == NULL) |
| 5305 | goto ext_call_fail; |
| 5306 | } |
| 5307 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5308 | if (flags & CALL_FLAG_VAR) { |
| 5309 | stararg = EXT_POP(*pp_stack); |
| 5310 | if (!PyTuple_Check(stararg)) { |
| 5311 | PyObject *t = NULL; |
Martin Panter | b594422 | 2016-01-31 06:30:56 +0000 | [diff] [blame] | 5312 | if (Py_TYPE(stararg)->tp_iter == NULL && |
| 5313 | !PySequence_Check(stararg)) { |
| 5314 | PyErr_Format(PyExc_TypeError, |
| 5315 | "%.200s%.200s argument after * " |
| 5316 | "must be an iterable, not %.200s", |
| 5317 | PyEval_GetFuncName(func), |
| 5318 | PyEval_GetFuncDesc(func), |
| 5319 | stararg->ob_type->tp_name); |
| 5320 | goto ext_call_fail; |
| 5321 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5322 | t = PySequence_Tuple(stararg); |
| 5323 | if (t == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5324 | goto ext_call_fail; |
| 5325 | } |
| 5326 | Py_DECREF(stararg); |
| 5327 | stararg = t; |
| 5328 | } |
| 5329 | nstar = PyTuple_GET_SIZE(stararg); |
| 5330 | } |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 5331 | else { |
| 5332 | nstar = 0; |
| 5333 | } |
| 5334 | |
| 5335 | callargs = update_star_args(nargs, nstar, stararg, pp_stack); |
| 5336 | if (callargs == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5337 | goto ext_call_fail; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 5338 | } |
| 5339 | |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 5340 | #ifdef CALL_PROFILE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5341 | /* At this point, we have to look at the type of func to |
| 5342 | update the call stats properly. Do it here so as to avoid |
| 5343 | exposing the call stats machinery outside ceval.c |
| 5344 | */ |
| 5345 | if (PyFunction_Check(func)) |
| 5346 | PCALL(PCALL_FUNCTION); |
| 5347 | else if (PyMethod_Check(func)) |
| 5348 | PCALL(PCALL_METHOD); |
| 5349 | else if (PyType_Check(func)) |
| 5350 | PCALL(PCALL_TYPE); |
| 5351 | else if (PyCFunction_Check(func)) |
| 5352 | PCALL(PCALL_CFUNCTION); |
| 5353 | else |
| 5354 | PCALL(PCALL_OTHER); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 5355 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5356 | if (PyCFunction_Check(func)) { |
| 5357 | PyThreadState *tstate = PyThreadState_GET(); |
| 5358 | C_TRACE(result, PyCFunction_Call(func, callargs, kwdict)); |
| 5359 | } |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 5360 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5361 | result = PyObject_Call(func, callargs, kwdict); |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 5362 | } |
| 5363 | |
Thomas Wouters | ce272b6 | 2007-09-19 21:19:28 +0000 | [diff] [blame] | 5364 | ext_call_fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5365 | Py_XDECREF(callargs); |
| 5366 | Py_XDECREF(kwdict); |
| 5367 | Py_XDECREF(stararg); |
| 5368 | return result; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 5369 | } |
| 5370 | |
Serhiy Storchaka | 483405b | 2015-02-17 10:14:30 +0200 | [diff] [blame] | 5371 | /* 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] | 5372 | nb_index slot defined, and store in *pi. |
| 5373 | Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX, |
| 5374 | 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] | 5375 | Return 0 on error, 1 on success. |
Tim Peters | cb479e7 | 2001-12-16 19:11:44 +0000 | [diff] [blame] | 5376 | */ |
Tim Peters | b519638 | 2001-12-16 19:44:20 +0000 | [diff] [blame] | 5377 | /* Note: If v is NULL, return success without storing into *pi. This |
| 5378 | is because_PyEval_SliceIndex() is called by apply_slice(), which can be |
| 5379 | 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] | 5380 | */ |
Guido van Rossum | 20c6add | 2000-05-08 14:06:50 +0000 | [diff] [blame] | 5381 | int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5382 | _PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5383 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5384 | if (v != NULL) { |
| 5385 | Py_ssize_t x; |
| 5386 | if (PyIndex_Check(v)) { |
| 5387 | x = PyNumber_AsSsize_t(v, NULL); |
| 5388 | if (x == -1 && PyErr_Occurred()) |
| 5389 | return 0; |
| 5390 | } |
| 5391 | else { |
| 5392 | PyErr_SetString(PyExc_TypeError, |
| 5393 | "slice indices must be integers or " |
| 5394 | "None or have an __index__ method"); |
| 5395 | return 0; |
| 5396 | } |
| 5397 | *pi = x; |
| 5398 | } |
| 5399 | return 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5400 | } |
| 5401 | |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 5402 | #define CANNOT_CATCH_MSG "catching classes that do not inherit from "\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5403 | "BaseException is not allowed" |
Brett Cannon | f74225d | 2007-02-26 21:10:16 +0000 | [diff] [blame] | 5404 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 5405 | static PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 5406 | cmp_outcome(int op, PyObject *v, PyObject *w) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5407 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5408 | int res = 0; |
| 5409 | switch (op) { |
| 5410 | case PyCmp_IS: |
| 5411 | res = (v == w); |
| 5412 | break; |
| 5413 | case PyCmp_IS_NOT: |
| 5414 | res = (v != w); |
| 5415 | break; |
| 5416 | case PyCmp_IN: |
| 5417 | res = PySequence_Contains(w, v); |
| 5418 | if (res < 0) |
| 5419 | return NULL; |
| 5420 | break; |
| 5421 | case PyCmp_NOT_IN: |
| 5422 | res = PySequence_Contains(w, v); |
| 5423 | if (res < 0) |
| 5424 | return NULL; |
| 5425 | res = !res; |
| 5426 | break; |
| 5427 | case PyCmp_EXC_MATCH: |
| 5428 | if (PyTuple_Check(w)) { |
| 5429 | Py_ssize_t i, length; |
| 5430 | length = PyTuple_Size(w); |
| 5431 | for (i = 0; i < length; i += 1) { |
| 5432 | PyObject *exc = PyTuple_GET_ITEM(w, i); |
| 5433 | if (!PyExceptionClass_Check(exc)) { |
| 5434 | PyErr_SetString(PyExc_TypeError, |
| 5435 | CANNOT_CATCH_MSG); |
| 5436 | return NULL; |
| 5437 | } |
| 5438 | } |
| 5439 | } |
| 5440 | else { |
| 5441 | if (!PyExceptionClass_Check(w)) { |
| 5442 | PyErr_SetString(PyExc_TypeError, |
| 5443 | CANNOT_CATCH_MSG); |
| 5444 | return NULL; |
| 5445 | } |
| 5446 | } |
| 5447 | res = PyErr_GivenExceptionMatches(v, w); |
| 5448 | break; |
| 5449 | default: |
| 5450 | return PyObject_RichCompare(v, w, op); |
| 5451 | } |
| 5452 | v = res ? Py_True : Py_False; |
| 5453 | Py_INCREF(v); |
| 5454 | return v; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5455 | } |
| 5456 | |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 5457 | static PyObject * |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 5458 | import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level) |
| 5459 | { |
| 5460 | _Py_IDENTIFIER(__import__); |
Victor Stinner | df142fd | 2016-08-20 00:44:42 +0200 | [diff] [blame] | 5461 | PyObject *import_func, *res; |
| 5462 | PyObject* stack[5]; |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 5463 | |
| 5464 | import_func = _PyDict_GetItemId(f->f_builtins, &PyId___import__); |
| 5465 | if (import_func == NULL) { |
| 5466 | PyErr_SetString(PyExc_ImportError, "__import__ not found"); |
| 5467 | return NULL; |
| 5468 | } |
| 5469 | |
| 5470 | /* Fast path for not overloaded __import__. */ |
| 5471 | if (import_func == PyThreadState_GET()->interp->import_func) { |
| 5472 | int ilevel = _PyLong_AsInt(level); |
| 5473 | if (ilevel == -1 && PyErr_Occurred()) { |
| 5474 | return NULL; |
| 5475 | } |
| 5476 | res = PyImport_ImportModuleLevelObject( |
| 5477 | name, |
| 5478 | f->f_globals, |
| 5479 | f->f_locals == NULL ? Py_None : f->f_locals, |
| 5480 | fromlist, |
| 5481 | ilevel); |
| 5482 | return res; |
| 5483 | } |
| 5484 | |
| 5485 | Py_INCREF(import_func); |
Victor Stinner | df142fd | 2016-08-20 00:44:42 +0200 | [diff] [blame] | 5486 | |
| 5487 | stack[0] = name; |
| 5488 | stack[1] = f->f_globals; |
| 5489 | stack[2] = f->f_locals == NULL ? Py_None : f->f_locals; |
| 5490 | stack[3] = fromlist; |
| 5491 | stack[4] = level; |
Victor Stinner | 559bb6a | 2016-08-22 22:48:54 +0200 | [diff] [blame] | 5492 | res = _PyObject_FastCall(import_func, stack, 5); |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 5493 | Py_DECREF(import_func); |
| 5494 | return res; |
| 5495 | } |
| 5496 | |
| 5497 | static PyObject * |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 5498 | import_from(PyObject *v, PyObject *name) |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 5499 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5500 | PyObject *x; |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5501 | _Py_IDENTIFIER(__name__); |
| 5502 | PyObject *fullmodname, *pkgname; |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 5503 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5504 | x = PyObject_GetAttr(v, name); |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5505 | if (x != NULL || !PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 5506 | return x; |
| 5507 | /* Issue #17636: in case this failed because of a circular relative |
| 5508 | import, try to fallback on reading the module directly from |
| 5509 | sys.modules. */ |
| 5510 | PyErr_Clear(); |
| 5511 | pkgname = _PyObject_GetAttrId(v, &PyId___name__); |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5512 | if (pkgname == NULL) { |
| 5513 | goto error; |
| 5514 | } |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5515 | fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name); |
| 5516 | Py_DECREF(pkgname); |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5517 | if (fullmodname == NULL) { |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5518 | return NULL; |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5519 | } |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5520 | x = PyDict_GetItem(PyImport_GetModuleDict(), fullmodname); |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5521 | Py_DECREF(fullmodname); |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5522 | if (x == NULL) { |
| 5523 | goto error; |
| 5524 | } |
| 5525 | Py_INCREF(x); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5526 | return x; |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5527 | error: |
| 5528 | PyErr_Format(PyExc_ImportError, "cannot import name %R", name); |
| 5529 | return NULL; |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 5530 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 5531 | |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 5532 | static int |
| 5533 | import_all_from(PyObject *locals, PyObject *v) |
| 5534 | { |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 5535 | _Py_IDENTIFIER(__all__); |
| 5536 | _Py_IDENTIFIER(__dict__); |
| 5537 | PyObject *all = _PyObject_GetAttrId(v, &PyId___all__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5538 | PyObject *dict, *name, *value; |
| 5539 | int skip_leading_underscores = 0; |
| 5540 | int pos, err; |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 5541 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5542 | if (all == NULL) { |
| 5543 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 5544 | return -1; /* Unexpected error */ |
| 5545 | PyErr_Clear(); |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 5546 | dict = _PyObject_GetAttrId(v, &PyId___dict__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5547 | if (dict == NULL) { |
| 5548 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 5549 | return -1; |
| 5550 | PyErr_SetString(PyExc_ImportError, |
| 5551 | "from-import-* object has no __dict__ and no __all__"); |
| 5552 | return -1; |
| 5553 | } |
| 5554 | all = PyMapping_Keys(dict); |
| 5555 | Py_DECREF(dict); |
| 5556 | if (all == NULL) |
| 5557 | return -1; |
| 5558 | skip_leading_underscores = 1; |
| 5559 | } |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 5560 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5561 | for (pos = 0, err = 0; ; pos++) { |
| 5562 | name = PySequence_GetItem(all, pos); |
| 5563 | if (name == NULL) { |
| 5564 | if (!PyErr_ExceptionMatches(PyExc_IndexError)) |
| 5565 | err = -1; |
| 5566 | else |
| 5567 | PyErr_Clear(); |
| 5568 | break; |
| 5569 | } |
| 5570 | if (skip_leading_underscores && |
| 5571 | PyUnicode_Check(name) && |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5572 | PyUnicode_READY(name) != -1 && |
| 5573 | PyUnicode_READ_CHAR(name, 0) == '_') |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5574 | { |
| 5575 | Py_DECREF(name); |
| 5576 | continue; |
| 5577 | } |
| 5578 | value = PyObject_GetAttr(v, name); |
| 5579 | if (value == NULL) |
| 5580 | err = -1; |
| 5581 | else if (PyDict_CheckExact(locals)) |
| 5582 | err = PyDict_SetItem(locals, name, value); |
| 5583 | else |
| 5584 | err = PyObject_SetItem(locals, name, value); |
| 5585 | Py_DECREF(name); |
| 5586 | Py_XDECREF(value); |
| 5587 | if (err != 0) |
| 5588 | break; |
| 5589 | } |
| 5590 | Py_DECREF(all); |
| 5591 | return err; |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 5592 | } |
| 5593 | |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 5594 | static void |
Neal Norwitz | da059e3 | 2007-08-26 05:33:45 +0000 | [diff] [blame] | 5595 | format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj) |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5596 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5597 | const char *obj_str; |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5598 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5599 | if (!obj) |
| 5600 | return; |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5601 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5602 | obj_str = _PyUnicode_AsString(obj); |
| 5603 | if (!obj_str) |
| 5604 | return; |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5605 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5606 | PyErr_Format(exc, format_str, obj_str); |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5607 | } |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5608 | |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 5609 | static void |
| 5610 | format_exc_unbound(PyCodeObject *co, int oparg) |
| 5611 | { |
| 5612 | PyObject *name; |
| 5613 | /* Don't stomp existing exception */ |
| 5614 | if (PyErr_Occurred()) |
| 5615 | return; |
| 5616 | if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) { |
| 5617 | name = PyTuple_GET_ITEM(co->co_cellvars, |
| 5618 | oparg); |
| 5619 | format_exc_check_arg( |
| 5620 | PyExc_UnboundLocalError, |
| 5621 | UNBOUNDLOCAL_ERROR_MSG, |
| 5622 | name); |
| 5623 | } else { |
| 5624 | name = PyTuple_GET_ITEM(co->co_freevars, oparg - |
| 5625 | PyTuple_GET_SIZE(co->co_cellvars)); |
| 5626 | format_exc_check_arg(PyExc_NameError, |
| 5627 | UNBOUNDFREE_ERROR_MSG, name); |
| 5628 | } |
| 5629 | } |
| 5630 | |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5631 | static PyObject * |
| 5632 | unicode_concatenate(PyObject *v, PyObject *w, |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 5633 | PyFrameObject *f, const unsigned short *next_instr) |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5634 | { |
| 5635 | PyObject *res; |
| 5636 | if (Py_REFCNT(v) == 2) { |
| 5637 | /* In the common case, there are 2 references to the value |
| 5638 | * stored in 'variable' when the += is performed: one on the |
| 5639 | * value stack (in 'v') and one still stored in the |
| 5640 | * 'variable'. We try to delete the variable now to reduce |
| 5641 | * the refcnt to 1. |
| 5642 | */ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 5643 | int opcode, oparg; |
| 5644 | NEXTOPARG(); |
| 5645 | switch (opcode) { |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5646 | case STORE_FAST: |
| 5647 | { |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5648 | PyObject **fastlocals = f->f_localsplus; |
| 5649 | if (GETLOCAL(oparg) == v) |
| 5650 | SETLOCAL(oparg, NULL); |
| 5651 | break; |
| 5652 | } |
| 5653 | case STORE_DEREF: |
| 5654 | { |
| 5655 | PyObject **freevars = (f->f_localsplus + |
| 5656 | f->f_code->co_nlocals); |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 5657 | PyObject *c = freevars[oparg]; |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5658 | if (PyCell_GET(c) == v) |
| 5659 | PyCell_Set(c, NULL); |
| 5660 | break; |
| 5661 | } |
| 5662 | case STORE_NAME: |
| 5663 | { |
| 5664 | PyObject *names = f->f_code->co_names; |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 5665 | PyObject *name = GETITEM(names, oparg); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5666 | PyObject *locals = f->f_locals; |
| 5667 | if (PyDict_CheckExact(locals) && |
| 5668 | PyDict_GetItem(locals, name) == v) { |
| 5669 | if (PyDict_DelItem(locals, name) != 0) { |
| 5670 | PyErr_Clear(); |
| 5671 | } |
| 5672 | } |
| 5673 | break; |
| 5674 | } |
| 5675 | } |
| 5676 | } |
| 5677 | res = v; |
| 5678 | PyUnicode_Append(&res, w); |
| 5679 | return res; |
| 5680 | } |
| 5681 | |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5682 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 5683 | |
Skip Montanaro | f118cb1 | 2001-10-15 20:51:38 +0000 | [diff] [blame] | 5684 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 5685 | getarray(long a[256]) |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5686 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5687 | int i; |
| 5688 | PyObject *l = PyList_New(256); |
| 5689 | if (l == NULL) return NULL; |
| 5690 | for (i = 0; i < 256; i++) { |
| 5691 | PyObject *x = PyLong_FromLong(a[i]); |
| 5692 | if (x == NULL) { |
| 5693 | Py_DECREF(l); |
| 5694 | return NULL; |
| 5695 | } |
| 5696 | PyList_SetItem(l, i, x); |
| 5697 | } |
| 5698 | for (i = 0; i < 256; i++) |
| 5699 | a[i] = 0; |
| 5700 | return l; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5701 | } |
| 5702 | |
| 5703 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 5704 | _Py_GetDXProfile(PyObject *self, PyObject *args) |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5705 | { |
| 5706 | #ifndef DXPAIRS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5707 | return getarray(dxp); |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5708 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5709 | int i; |
| 5710 | PyObject *l = PyList_New(257); |
| 5711 | if (l == NULL) return NULL; |
| 5712 | for (i = 0; i < 257; i++) { |
| 5713 | PyObject *x = getarray(dxpairs[i]); |
| 5714 | if (x == NULL) { |
| 5715 | Py_DECREF(l); |
| 5716 | return NULL; |
| 5717 | } |
| 5718 | PyList_SetItem(l, i, x); |
| 5719 | } |
| 5720 | return l; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5721 | #endif |
| 5722 | } |
| 5723 | |
| 5724 | #endif |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 5725 | |
| 5726 | Py_ssize_t |
| 5727 | _PyEval_RequestCodeExtraIndex(freefunc free) |
| 5728 | { |
| 5729 | PyThreadState *tstate = PyThreadState_Get(); |
| 5730 | Py_ssize_t new_index; |
| 5731 | |
| 5732 | if (tstate->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) { |
| 5733 | return -1; |
| 5734 | } |
| 5735 | new_index = tstate->co_extra_user_count++; |
| 5736 | tstate->co_extra_freefuncs[new_index] = free; |
| 5737 | return new_index; |
| 5738 | } |