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 |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 112 | static PyObject * call_function(PyObject ***, Py_ssize_t, PyObject *, uint64*, uint64*); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 113 | #else |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 114 | static PyObject * call_function(PyObject ***, Py_ssize_t, PyObject *); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 115 | #endif |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame^] | 116 | static PyObject * fast_function(PyObject *, PyObject **, Py_ssize_t, PyObject *); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 117 | static PyObject * do_call_core(PyObject *, PyObject *, PyObject *); |
| 118 | static PyObject * create_keyword_args(PyObject *, PyObject ***, PyObject *); |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 119 | static PyObject * load_args(PyObject ***, Py_ssize_t); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 120 | |
Guido van Rossum | 0a066c0 | 1992-03-27 17:29:15 +0000 | [diff] [blame] | 121 | #ifdef LLTRACE |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 122 | static int lltrace; |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 123 | static int prtrace(PyObject *, const char *); |
Guido van Rossum | 0a066c0 | 1992-03-27 17:29:15 +0000 | [diff] [blame] | 124 | #endif |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 125 | static int call_trace(Py_tracefunc, PyObject *, |
| 126 | PyThreadState *, PyFrameObject *, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 127 | int, PyObject *); |
Amaury Forgeot d'Arc | f05149a | 2007-11-13 01:05:30 +0000 | [diff] [blame] | 128 | static int call_trace_protected(Py_tracefunc, PyObject *, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 129 | PyThreadState *, PyFrameObject *, |
| 130 | int, PyObject *); |
| 131 | static void call_exc_trace(Py_tracefunc, PyObject *, |
| 132 | PyThreadState *, PyFrameObject *); |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 133 | static int maybe_call_line_trace(Py_tracefunc, PyObject *, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 134 | PyThreadState *, PyFrameObject *, int *, int *, int *); |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 135 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 136 | static PyObject * cmp_outcome(int, PyObject *, PyObject *); |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 137 | static PyObject * import_name(PyFrameObject *, PyObject *, PyObject *, PyObject *); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 138 | static PyObject * import_from(PyObject *, PyObject *); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 139 | static int import_all_from(PyObject *, PyObject *); |
Neal Norwitz | da059e3 | 2007-08-26 05:33:45 +0000 | [diff] [blame] | 140 | static void format_exc_check_arg(PyObject *, const char *, PyObject *); |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 141 | static void format_exc_unbound(PyCodeObject *co, int oparg); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 142 | static PyObject * unicode_concatenate(PyObject *, PyObject *, |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 143 | PyFrameObject *, const unsigned short *); |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 144 | static PyObject * special_lookup(PyObject *, _Py_Identifier *); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 145 | |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 146 | #define NAME_ERROR_MSG \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 147 | "name '%.200s' is not defined" |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 148 | #define UNBOUNDLOCAL_ERROR_MSG \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 149 | "local variable '%.200s' referenced before assignment" |
Jeremy Hylton | c76770c | 2001-04-13 16:51:46 +0000 | [diff] [blame] | 150 | #define UNBOUNDFREE_ERROR_MSG \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 151 | "free variable '%.200s' referenced before assignment" \ |
| 152 | " in enclosing scope" |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 153 | |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 154 | /* Dynamic execution profile */ |
| 155 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 156 | #ifdef DXPAIRS |
| 157 | static long dxpairs[257][256]; |
| 158 | #define dxp dxpairs[256] |
| 159 | #else |
| 160 | static long dxp[256]; |
| 161 | #endif |
| 162 | #endif |
| 163 | |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 164 | /* Function call profile */ |
| 165 | #ifdef CALL_PROFILE |
| 166 | #define PCALL_NUM 11 |
| 167 | static int pcall[PCALL_NUM]; |
| 168 | |
| 169 | #define PCALL_ALL 0 |
| 170 | #define PCALL_FUNCTION 1 |
| 171 | #define PCALL_FAST_FUNCTION 2 |
| 172 | #define PCALL_FASTER_FUNCTION 3 |
| 173 | #define PCALL_METHOD 4 |
| 174 | #define PCALL_BOUND_METHOD 5 |
| 175 | #define PCALL_CFUNCTION 6 |
| 176 | #define PCALL_TYPE 7 |
| 177 | #define PCALL_GENERATOR 8 |
| 178 | #define PCALL_OTHER 9 |
| 179 | #define PCALL_POP 10 |
| 180 | |
| 181 | /* Notes about the statistics |
| 182 | |
| 183 | PCALL_FAST stats |
| 184 | |
| 185 | FAST_FUNCTION means no argument tuple needs to be created. |
| 186 | FASTER_FUNCTION means that the fast-path frame setup code is used. |
| 187 | |
| 188 | If there is a method call where the call can be optimized by changing |
| 189 | the argument tuple and calling the function directly, it gets recorded |
| 190 | twice. |
| 191 | |
| 192 | As a result, the relationship among the statistics appears to be |
| 193 | PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD + |
| 194 | PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER |
| 195 | PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION |
| 196 | PCALL_METHOD > PCALL_BOUND_METHOD |
| 197 | */ |
| 198 | |
| 199 | #define PCALL(POS) pcall[POS]++ |
| 200 | |
| 201 | PyObject * |
| 202 | PyEval_GetCallStats(PyObject *self) |
| 203 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 204 | return Py_BuildValue("iiiiiiiiiii", |
| 205 | pcall[0], pcall[1], pcall[2], pcall[3], |
| 206 | pcall[4], pcall[5], pcall[6], pcall[7], |
| 207 | pcall[8], pcall[9], pcall[10]); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 208 | } |
| 209 | #else |
| 210 | #define PCALL(O) |
| 211 | |
| 212 | PyObject * |
| 213 | PyEval_GetCallStats(PyObject *self) |
| 214 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 215 | Py_INCREF(Py_None); |
| 216 | return Py_None; |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 217 | } |
| 218 | #endif |
| 219 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 220 | |
Benjamin Peterson | d2be5b4 | 2010-09-10 22:47:02 +0000 | [diff] [blame] | 221 | #ifdef WITH_THREAD |
| 222 | #define GIL_REQUEST _Py_atomic_load_relaxed(&gil_drop_request) |
| 223 | #else |
| 224 | #define GIL_REQUEST 0 |
| 225 | #endif |
| 226 | |
Jeffrey Yasskin | 3937083 | 2010-05-03 19:29:34 +0000 | [diff] [blame] | 227 | /* This can set eval_breaker to 0 even though gil_drop_request became |
| 228 | 1. We believe this is all right because the eval loop will release |
| 229 | the GIL eventually anyway. */ |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 230 | #define COMPUTE_EVAL_BREAKER() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 231 | _Py_atomic_store_relaxed( \ |
| 232 | &eval_breaker, \ |
Benjamin Peterson | d2be5b4 | 2010-09-10 22:47:02 +0000 | [diff] [blame] | 233 | GIL_REQUEST | \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 234 | _Py_atomic_load_relaxed(&pendingcalls_to_do) | \ |
| 235 | pending_async_exc) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 236 | |
Benjamin Peterson | d2be5b4 | 2010-09-10 22:47:02 +0000 | [diff] [blame] | 237 | #ifdef WITH_THREAD |
| 238 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 239 | #define SET_GIL_DROP_REQUEST() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 240 | do { \ |
| 241 | _Py_atomic_store_relaxed(&gil_drop_request, 1); \ |
| 242 | _Py_atomic_store_relaxed(&eval_breaker, 1); \ |
| 243 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 244 | |
| 245 | #define RESET_GIL_DROP_REQUEST() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 246 | do { \ |
| 247 | _Py_atomic_store_relaxed(&gil_drop_request, 0); \ |
| 248 | COMPUTE_EVAL_BREAKER(); \ |
| 249 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 250 | |
Benjamin Peterson | d2be5b4 | 2010-09-10 22:47:02 +0000 | [diff] [blame] | 251 | #endif |
| 252 | |
Jeffrey Yasskin | 3937083 | 2010-05-03 19:29:34 +0000 | [diff] [blame] | 253 | /* Pending calls are only modified under pending_lock */ |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 254 | #define SIGNAL_PENDING_CALLS() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 255 | do { \ |
| 256 | _Py_atomic_store_relaxed(&pendingcalls_to_do, 1); \ |
| 257 | _Py_atomic_store_relaxed(&eval_breaker, 1); \ |
| 258 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 259 | |
| 260 | #define UNSIGNAL_PENDING_CALLS() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 261 | do { \ |
| 262 | _Py_atomic_store_relaxed(&pendingcalls_to_do, 0); \ |
| 263 | COMPUTE_EVAL_BREAKER(); \ |
| 264 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 265 | |
| 266 | #define SIGNAL_ASYNC_EXC() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 267 | do { \ |
| 268 | pending_async_exc = 1; \ |
| 269 | _Py_atomic_store_relaxed(&eval_breaker, 1); \ |
| 270 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 271 | |
| 272 | #define UNSIGNAL_ASYNC_EXC() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 273 | do { pending_async_exc = 0; COMPUTE_EVAL_BREAKER(); } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 274 | |
| 275 | |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 276 | #ifdef WITH_THREAD |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 277 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 278 | #ifdef HAVE_ERRNO_H |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 279 | #include <errno.h> |
Guido van Rossum | 2571cc8 | 1999-04-07 16:07:23 +0000 | [diff] [blame] | 280 | #endif |
Guido van Rossum | 49b5606 | 1998-10-01 20:42:43 +0000 | [diff] [blame] | 281 | #include "pythread.h" |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 282 | |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 283 | static PyThread_type_lock pending_lock = 0; /* for pending calls */ |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 284 | static long main_thread = 0; |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 285 | /* This single variable consolidates all requests to break out of the fast path |
| 286 | in the eval loop. */ |
Jeffrey Yasskin | 3937083 | 2010-05-03 19:29:34 +0000 | [diff] [blame] | 287 | static _Py_atomic_int eval_breaker = {0}; |
| 288 | /* Request for dropping the GIL */ |
| 289 | static _Py_atomic_int gil_drop_request = {0}; |
| 290 | /* Request for running pending calls. */ |
| 291 | static _Py_atomic_int pendingcalls_to_do = {0}; |
| 292 | /* Request for looking at the `async_exc` field of the current thread state. |
| 293 | Guarded by the GIL. */ |
| 294 | static int pending_async_exc = 0; |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 295 | |
| 296 | #include "ceval_gil.h" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 297 | |
Tim Peters | 7f468f2 | 2004-10-11 02:40:51 +0000 | [diff] [blame] | 298 | int |
| 299 | PyEval_ThreadsInitialized(void) |
| 300 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 301 | return gil_created(); |
Tim Peters | 7f468f2 | 2004-10-11 02:40:51 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 304 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 305 | PyEval_InitThreads(void) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 306 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 307 | if (gil_created()) |
| 308 | return; |
| 309 | create_gil(); |
| 310 | take_gil(PyThreadState_GET()); |
| 311 | main_thread = PyThread_get_thread_ident(); |
| 312 | if (!pending_lock) |
| 313 | pending_lock = PyThread_allocate_lock(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 314 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 315 | |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 316 | void |
Antoine Pitrou | 1df1536 | 2010-09-13 14:16:46 +0000 | [diff] [blame] | 317 | _PyEval_FiniThreads(void) |
| 318 | { |
| 319 | if (!gil_created()) |
| 320 | return; |
| 321 | destroy_gil(); |
| 322 | assert(!gil_created()); |
| 323 | } |
| 324 | |
| 325 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 326 | PyEval_AcquireLock(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 327 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 328 | PyThreadState *tstate = PyThreadState_GET(); |
| 329 | if (tstate == NULL) |
| 330 | Py_FatalError("PyEval_AcquireLock: current thread state is NULL"); |
| 331 | take_gil(tstate); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 335 | PyEval_ReleaseLock(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 336 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 337 | /* This function must succeed when the current thread state is NULL. |
| 338 | We therefore avoid PyThreadState_GET() which dumps a fatal error |
| 339 | in debug mode. |
| 340 | */ |
| 341 | drop_gil((PyThreadState*)_Py_atomic_load_relaxed( |
| 342 | &_PyThreadState_Current)); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 346 | PyEval_AcquireThread(PyThreadState *tstate) |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 347 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 348 | if (tstate == NULL) |
| 349 | Py_FatalError("PyEval_AcquireThread: NULL new thread state"); |
| 350 | /* Check someone has called PyEval_InitThreads() to create the lock */ |
| 351 | assert(gil_created()); |
| 352 | take_gil(tstate); |
| 353 | if (PyThreadState_Swap(tstate) != NULL) |
| 354 | Py_FatalError( |
| 355 | "PyEval_AcquireThread: non-NULL old thread state"); |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 359 | PyEval_ReleaseThread(PyThreadState *tstate) |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 360 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 361 | if (tstate == NULL) |
| 362 | Py_FatalError("PyEval_ReleaseThread: NULL thread state"); |
| 363 | if (PyThreadState_Swap(NULL) != tstate) |
| 364 | Py_FatalError("PyEval_ReleaseThread: wrong thread state"); |
| 365 | drop_gil(tstate); |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 366 | } |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 367 | |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 368 | /* This function is called from PyOS_AfterFork to destroy all threads which are |
| 369 | * not running in the child process, and clear internal locks which might be |
| 370 | * held by those threads. (This could also be done using pthread_atfork |
| 371 | * mechanism, at least for the pthreads implementation.) */ |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 372 | |
| 373 | void |
| 374 | PyEval_ReInitThreads(void) |
| 375 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 376 | _Py_IDENTIFIER(_after_fork); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 377 | PyObject *threading, *result; |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 378 | PyThreadState *current_tstate = PyThreadState_GET(); |
Jesse Noller | a851397 | 2008-07-17 16:49:17 +0000 | [diff] [blame] | 379 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 380 | if (!gil_created()) |
| 381 | return; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 382 | recreate_gil(); |
| 383 | pending_lock = PyThread_allocate_lock(); |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 384 | take_gil(current_tstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 385 | main_thread = PyThread_get_thread_ident(); |
Jesse Noller | a851397 | 2008-07-17 16:49:17 +0000 | [diff] [blame] | 386 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 387 | /* Update the threading module with the new state. |
| 388 | */ |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 389 | threading = PyMapping_GetItemString(current_tstate->interp->modules, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 390 | "threading"); |
| 391 | if (threading == NULL) { |
| 392 | /* threading not imported */ |
| 393 | PyErr_Clear(); |
| 394 | return; |
| 395 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 396 | result = _PyObject_CallMethodId(threading, &PyId__after_fork, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 397 | if (result == NULL) |
| 398 | PyErr_WriteUnraisable(threading); |
| 399 | else |
| 400 | Py_DECREF(result); |
| 401 | Py_DECREF(threading); |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 402 | |
| 403 | /* Destroy all threads except the current one */ |
| 404 | _PyThreadState_DeleteExcept(current_tstate); |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 405 | } |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 406 | |
| 407 | #else |
Jeffrey Yasskin | 3937083 | 2010-05-03 19:29:34 +0000 | [diff] [blame] | 408 | static _Py_atomic_int eval_breaker = {0}; |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 409 | static int pending_async_exc = 0; |
| 410 | #endif /* WITH_THREAD */ |
| 411 | |
| 412 | /* This function is used to signal that async exceptions are waiting to be |
| 413 | raised, therefore it is also useful in non-threaded builds. */ |
| 414 | |
| 415 | void |
| 416 | _PyEval_SignalAsyncExc(void) |
| 417 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 418 | SIGNAL_ASYNC_EXC(); |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 419 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 420 | |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 421 | /* Functions save_thread and restore_thread are always defined so |
| 422 | dynamically loaded modules needn't be compiled separately for use |
| 423 | with and without threads: */ |
| 424 | |
Guido van Rossum | 2fca21f7 | 1997-07-18 23:56:58 +0000 | [diff] [blame] | 425 | PyThreadState * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 426 | PyEval_SaveThread(void) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 427 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 428 | PyThreadState *tstate = PyThreadState_Swap(NULL); |
| 429 | if (tstate == NULL) |
| 430 | Py_FatalError("PyEval_SaveThread: NULL tstate"); |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 431 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 432 | if (gil_created()) |
| 433 | drop_gil(tstate); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 434 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 435 | return tstate; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 439 | PyEval_RestoreThread(PyThreadState *tstate) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 440 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 441 | if (tstate == NULL) |
| 442 | Py_FatalError("PyEval_RestoreThread: NULL tstate"); |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 443 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 444 | if (gil_created()) { |
| 445 | int err = errno; |
| 446 | take_gil(tstate); |
Antoine Pitrou | 0d5e52d | 2011-05-04 20:02:30 +0200 | [diff] [blame] | 447 | /* _Py_Finalizing is protected by the GIL */ |
| 448 | if (_Py_Finalizing && tstate != _Py_Finalizing) { |
| 449 | drop_gil(tstate); |
| 450 | PyThread_exit_thread(); |
| 451 | assert(0); /* unreachable */ |
| 452 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 453 | errno = err; |
| 454 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 455 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 456 | PyThreadState_Swap(tstate); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 460 | /* Mechanism whereby asynchronously executing callbacks (e.g. UNIX |
| 461 | signal handlers or Mac I/O completion routines) can schedule calls |
| 462 | to a function to be called synchronously. |
| 463 | The synchronous function is called with one void* argument. |
| 464 | It should return 0 for success or -1 for failure -- failure should |
| 465 | be accompanied by an exception. |
| 466 | |
| 467 | If registry succeeds, the registry function returns 0; if it fails |
| 468 | (e.g. due to too many pending calls) it returns -1 (without setting |
| 469 | an exception condition). |
| 470 | |
| 471 | Note that because registry may occur from within signal handlers, |
| 472 | or other asynchronous events, calling malloc() is unsafe! |
| 473 | |
| 474 | #ifdef WITH_THREAD |
| 475 | Any thread can schedule pending calls, but only the main thread |
| 476 | will execute them. |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 477 | There is no facility to schedule calls to a particular thread, but |
| 478 | that should be easy to change, should that ever be required. In |
| 479 | that case, the static variables here should go into the python |
| 480 | threadstate. |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 481 | #endif |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 482 | */ |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 483 | |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 484 | #ifdef WITH_THREAD |
| 485 | |
| 486 | /* The WITH_THREAD implementation is thread-safe. It allows |
| 487 | scheduling to be made from any thread, and even from an executing |
| 488 | callback. |
| 489 | */ |
| 490 | |
| 491 | #define NPENDINGCALLS 32 |
| 492 | static struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 493 | int (*func)(void *); |
| 494 | void *arg; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 495 | } pendingcalls[NPENDINGCALLS]; |
| 496 | static int pendingfirst = 0; |
| 497 | static int pendinglast = 0; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 498 | |
| 499 | int |
| 500 | Py_AddPendingCall(int (*func)(void *), void *arg) |
| 501 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 502 | int i, j, result=0; |
| 503 | PyThread_type_lock lock = pending_lock; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 504 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 505 | /* try a few times for the lock. Since this mechanism is used |
| 506 | * for signal handling (on the main thread), there is a (slim) |
| 507 | * chance that a signal is delivered on the same thread while we |
| 508 | * hold the lock during the Py_MakePendingCalls() function. |
| 509 | * This avoids a deadlock in that case. |
| 510 | * Note that signals can be delivered on any thread. In particular, |
| 511 | * on Windows, a SIGINT is delivered on a system-created worker |
| 512 | * thread. |
| 513 | * We also check for lock being NULL, in the unlikely case that |
| 514 | * this function is called before any bytecode evaluation takes place. |
| 515 | */ |
| 516 | if (lock != NULL) { |
| 517 | for (i = 0; i<100; i++) { |
| 518 | if (PyThread_acquire_lock(lock, NOWAIT_LOCK)) |
| 519 | break; |
| 520 | } |
| 521 | if (i == 100) |
| 522 | return -1; |
| 523 | } |
| 524 | |
| 525 | i = pendinglast; |
| 526 | j = (i + 1) % NPENDINGCALLS; |
| 527 | if (j == pendingfirst) { |
| 528 | result = -1; /* Queue full */ |
| 529 | } else { |
| 530 | pendingcalls[i].func = func; |
| 531 | pendingcalls[i].arg = arg; |
| 532 | pendinglast = j; |
| 533 | } |
| 534 | /* signal main loop */ |
| 535 | SIGNAL_PENDING_CALLS(); |
| 536 | if (lock != NULL) |
| 537 | PyThread_release_lock(lock); |
| 538 | return result; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | int |
| 542 | Py_MakePendingCalls(void) |
| 543 | { |
Charles-François Natali | f23339a | 2011-07-23 18:15:43 +0200 | [diff] [blame] | 544 | static int busy = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 545 | int i; |
| 546 | int r = 0; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 547 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 548 | if (!pending_lock) { |
| 549 | /* initial allocation of the lock */ |
| 550 | pending_lock = PyThread_allocate_lock(); |
| 551 | if (pending_lock == NULL) |
| 552 | return -1; |
| 553 | } |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 554 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 555 | /* only service pending calls on main thread */ |
| 556 | if (main_thread && PyThread_get_thread_ident() != main_thread) |
| 557 | return 0; |
| 558 | /* don't perform recursive pending calls */ |
Charles-François Natali | f23339a | 2011-07-23 18:15:43 +0200 | [diff] [blame] | 559 | if (busy) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 560 | return 0; |
Charles-François Natali | f23339a | 2011-07-23 18:15:43 +0200 | [diff] [blame] | 561 | busy = 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 562 | /* perform a bounded number of calls, in case of recursion */ |
| 563 | for (i=0; i<NPENDINGCALLS; i++) { |
| 564 | int j; |
| 565 | int (*func)(void *); |
| 566 | void *arg = NULL; |
| 567 | |
| 568 | /* pop one item off the queue while holding the lock */ |
| 569 | PyThread_acquire_lock(pending_lock, WAIT_LOCK); |
| 570 | j = pendingfirst; |
| 571 | if (j == pendinglast) { |
| 572 | func = NULL; /* Queue empty */ |
| 573 | } else { |
| 574 | func = pendingcalls[j].func; |
| 575 | arg = pendingcalls[j].arg; |
| 576 | pendingfirst = (j + 1) % NPENDINGCALLS; |
| 577 | } |
| 578 | if (pendingfirst != pendinglast) |
| 579 | SIGNAL_PENDING_CALLS(); |
| 580 | else |
| 581 | UNSIGNAL_PENDING_CALLS(); |
| 582 | PyThread_release_lock(pending_lock); |
| 583 | /* having released the lock, perform the callback */ |
| 584 | if (func == NULL) |
| 585 | break; |
| 586 | r = func(arg); |
| 587 | if (r) |
| 588 | break; |
| 589 | } |
Charles-François Natali | f23339a | 2011-07-23 18:15:43 +0200 | [diff] [blame] | 590 | busy = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 591 | return r; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | #else /* if ! defined WITH_THREAD */ |
| 595 | |
| 596 | /* |
| 597 | WARNING! ASYNCHRONOUSLY EXECUTING CODE! |
| 598 | This code is used for signal handling in python that isn't built |
| 599 | with WITH_THREAD. |
| 600 | Don't use this implementation when Py_AddPendingCalls() can happen |
| 601 | on a different thread! |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 602 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 603 | There are two possible race conditions: |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 604 | (1) nested asynchronous calls to Py_AddPendingCall() |
| 605 | (2) AddPendingCall() calls made while pending calls are being processed. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 606 | |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 607 | (1) is very unlikely because typically signal delivery |
| 608 | is blocked during signal handling. So it should be impossible. |
| 609 | (2) is a real possibility. |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 610 | The current code is safe against (2), but not against (1). |
| 611 | The safety against (2) is derived from the fact that only one |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 612 | thread is present, interrupted by signals, and that the critical |
| 613 | section is protected with the "busy" variable. On Windows, which |
| 614 | delivers SIGINT on a system thread, this does not hold and therefore |
| 615 | Windows really shouldn't use this version. |
| 616 | The two threads could theoretically wiggle around the "busy" variable. |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 617 | */ |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 618 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 619 | #define NPENDINGCALLS 32 |
| 620 | static struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 621 | int (*func)(void *); |
| 622 | void *arg; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 623 | } pendingcalls[NPENDINGCALLS]; |
| 624 | static volatile int pendingfirst = 0; |
| 625 | static volatile int pendinglast = 0; |
Benjamin Peterson | 08ec84c | 2010-05-30 14:49:32 +0000 | [diff] [blame] | 626 | static _Py_atomic_int pendingcalls_to_do = {0}; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 627 | |
| 628 | int |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 629 | Py_AddPendingCall(int (*func)(void *), void *arg) |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 630 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 631 | static volatile int busy = 0; |
| 632 | int i, j; |
| 633 | /* XXX Begin critical section */ |
| 634 | if (busy) |
| 635 | return -1; |
| 636 | busy = 1; |
| 637 | i = pendinglast; |
| 638 | j = (i + 1) % NPENDINGCALLS; |
| 639 | if (j == pendingfirst) { |
| 640 | busy = 0; |
| 641 | return -1; /* Queue full */ |
| 642 | } |
| 643 | pendingcalls[i].func = func; |
| 644 | pendingcalls[i].arg = arg; |
| 645 | pendinglast = j; |
Skip Montanaro | d581d77 | 2002-09-03 20:10:45 +0000 | [diff] [blame] | 646 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 647 | SIGNAL_PENDING_CALLS(); |
| 648 | busy = 0; |
| 649 | /* XXX End critical section */ |
| 650 | return 0; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 651 | } |
| 652 | |
Guido van Rossum | 180d7b4 | 1994-09-29 09:45:57 +0000 | [diff] [blame] | 653 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 654 | Py_MakePendingCalls(void) |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 655 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 656 | static int busy = 0; |
| 657 | if (busy) |
| 658 | return 0; |
| 659 | busy = 1; |
| 660 | UNSIGNAL_PENDING_CALLS(); |
| 661 | for (;;) { |
| 662 | int i; |
| 663 | int (*func)(void *); |
| 664 | void *arg; |
| 665 | i = pendingfirst; |
| 666 | if (i == pendinglast) |
| 667 | break; /* Queue empty */ |
| 668 | func = pendingcalls[i].func; |
| 669 | arg = pendingcalls[i].arg; |
| 670 | pendingfirst = (i + 1) % NPENDINGCALLS; |
| 671 | if (func(arg) < 0) { |
| 672 | busy = 0; |
| 673 | SIGNAL_PENDING_CALLS(); /* We're not done yet */ |
| 674 | return -1; |
| 675 | } |
| 676 | } |
| 677 | busy = 0; |
| 678 | return 0; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 679 | } |
| 680 | |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 681 | #endif /* WITH_THREAD */ |
| 682 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 683 | |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 684 | /* The interpreter's recursion limit */ |
| 685 | |
Hye-Shik Chang | b6fa281 | 2005-04-04 15:49:02 +0000 | [diff] [blame] | 686 | #ifndef Py_DEFAULT_RECURSION_LIMIT |
| 687 | #define Py_DEFAULT_RECURSION_LIMIT 1000 |
| 688 | #endif |
| 689 | static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT; |
| 690 | int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 691 | |
Vladimir Marangozov | 7bd25be | 2000-09-01 11:07:19 +0000 | [diff] [blame] | 692 | int |
| 693 | Py_GetRecursionLimit(void) |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 694 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 695 | return recursion_limit; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 696 | } |
| 697 | |
Vladimir Marangozov | 7bd25be | 2000-09-01 11:07:19 +0000 | [diff] [blame] | 698 | void |
| 699 | Py_SetRecursionLimit(int new_limit) |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 700 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 701 | recursion_limit = new_limit; |
| 702 | _Py_CheckRecursionLimit = recursion_limit; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 703 | } |
| 704 | |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 705 | /* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall() |
| 706 | if the recursion_depth reaches _Py_CheckRecursionLimit. |
| 707 | If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit |
| 708 | to guarantee that _Py_CheckRecursiveCall() is regularly called. |
| 709 | Without USE_STACKCHECK, there is no need for this. */ |
| 710 | int |
Serhiy Storchaka | 5fa22fc | 2015-06-21 16:26:28 +0300 | [diff] [blame] | 711 | _Py_CheckRecursiveCall(const char *where) |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 712 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 713 | PyThreadState *tstate = PyThreadState_GET(); |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 714 | |
| 715 | #ifdef USE_STACKCHECK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 716 | if (PyOS_CheckStack()) { |
| 717 | --tstate->recursion_depth; |
| 718 | PyErr_SetString(PyExc_MemoryError, "Stack overflow"); |
| 719 | return -1; |
| 720 | } |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 721 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 722 | _Py_CheckRecursionLimit = recursion_limit; |
| 723 | if (tstate->recursion_critical) |
| 724 | /* Somebody asked that we don't check for recursion. */ |
| 725 | return 0; |
| 726 | if (tstate->overflowed) { |
| 727 | if (tstate->recursion_depth > recursion_limit + 50) { |
| 728 | /* Overflowing while handling an overflow. Give up. */ |
| 729 | Py_FatalError("Cannot recover from stack overflow."); |
| 730 | } |
| 731 | return 0; |
| 732 | } |
| 733 | if (tstate->recursion_depth > recursion_limit) { |
| 734 | --tstate->recursion_depth; |
| 735 | tstate->overflowed = 1; |
Yury Selivanov | f488fb4 | 2015-07-03 01:04:23 -0400 | [diff] [blame] | 736 | PyErr_Format(PyExc_RecursionError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 737 | "maximum recursion depth exceeded%s", |
| 738 | where); |
| 739 | return -1; |
| 740 | } |
| 741 | return 0; |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 742 | } |
| 743 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 744 | /* Status code for main loop (reason for stack unwind) */ |
Raymond Hettinger | 7c95865 | 2004-04-06 10:11:10 +0000 | [diff] [blame] | 745 | enum why_code { |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 746 | WHY_NOT = 0x0001, /* No error */ |
| 747 | WHY_EXCEPTION = 0x0002, /* Exception occurred */ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 748 | WHY_RETURN = 0x0008, /* 'return' statement */ |
| 749 | WHY_BREAK = 0x0010, /* 'break' statement */ |
| 750 | WHY_CONTINUE = 0x0020, /* 'continue' statement */ |
| 751 | WHY_YIELD = 0x0040, /* 'yield' operator */ |
| 752 | WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */ |
Raymond Hettinger | 7c95865 | 2004-04-06 10:11:10 +0000 | [diff] [blame] | 753 | }; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 754 | |
Benjamin Peterson | 8788024 | 2011-07-03 16:48:31 -0500 | [diff] [blame] | 755 | static void save_exc_state(PyThreadState *, PyFrameObject *); |
| 756 | static void swap_exc_state(PyThreadState *, PyFrameObject *); |
| 757 | static void restore_and_clear_exc_state(PyThreadState *, PyFrameObject *); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 758 | static int do_raise(PyObject *, PyObject *); |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 759 | static int unpack_iterable(PyObject *, int, int, PyObject **); |
Guido van Rossum | 1aa1483 | 1997-01-21 05:34:20 +0000 | [diff] [blame] | 760 | |
Jeffrey Yasskin | 008d8ef | 2008-12-06 17:09:27 +0000 | [diff] [blame] | 761 | /* Records whether tracing is on for any thread. Counts the number of |
| 762 | threads for which tstate->c_tracefunc is non-NULL, so if the value |
| 763 | is 0, we know we don't have to check this thread's c_tracefunc. |
| 764 | This speeds up the if statement in PyEval_EvalFrameEx() after |
| 765 | fast_next_opcode*/ |
| 766 | static int _Py_TracingPossible = 0; |
| 767 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 768 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 769 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 770 | PyObject * |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 771 | PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 772 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 773 | return PyEval_EvalCodeEx(co, |
| 774 | globals, locals, |
| 775 | (PyObject **)NULL, 0, |
| 776 | (PyObject **)NULL, 0, |
| 777 | (PyObject **)NULL, 0, |
| 778 | NULL, NULL); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | |
| 782 | /* Interpreter main loop */ |
| 783 | |
Martin v. Löwis | 8d97e33 | 2004-06-27 15:43:12 +0000 | [diff] [blame] | 784 | PyObject * |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 785 | PyEval_EvalFrame(PyFrameObject *f) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 786 | /* This is for backward compatibility with extension modules that |
| 787 | used this API; core interpreter code should call |
| 788 | PyEval_EvalFrameEx() */ |
| 789 | return PyEval_EvalFrameEx(f, 0); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 793 | PyEval_EvalFrameEx(PyFrameObject *f, int throwflag) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 794 | { |
Brett Cannon | 3cebf93 | 2016-09-05 15:33:46 -0700 | [diff] [blame] | 795 | PyThreadState *tstate = PyThreadState_GET(); |
| 796 | return tstate->interp->eval_frame(f, throwflag); |
| 797 | } |
| 798 | |
| 799 | PyObject * |
| 800 | _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag) |
| 801 | { |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 802 | #ifdef DXPAIRS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 803 | int lastopcode = 0; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 804 | #endif |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 805 | PyObject **stack_pointer; /* Next free slot in value stack */ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 806 | const unsigned short *next_instr; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 807 | int opcode; /* Current opcode */ |
| 808 | int oparg; /* Current opcode argument, if any */ |
| 809 | enum why_code why; /* Reason for block stack unwind */ |
| 810 | PyObject **fastlocals, **freevars; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 811 | PyObject *retval = NULL; /* Return value */ |
| 812 | PyThreadState *tstate = PyThreadState_GET(); |
| 813 | PyCodeObject *co; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 814 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 815 | /* when tracing we set things up so that |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 816 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 817 | not (instr_lb <= current_bytecode_offset < instr_ub) |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 818 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 819 | is true when the line being executed has changed. The |
| 820 | initial values are such as to make this false the first |
| 821 | time it is tested. */ |
| 822 | int instr_ub = -1, instr_lb = 0, instr_prev = -1; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 823 | |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 824 | const unsigned short *first_instr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 825 | PyObject *names; |
| 826 | PyObject *consts; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 827 | |
Brett Cannon | 368b4b7 | 2012-04-02 12:17:59 -0400 | [diff] [blame] | 828 | #ifdef LLTRACE |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 829 | _Py_IDENTIFIER(__ltrace__); |
Brett Cannon | 368b4b7 | 2012-04-02 12:17:59 -0400 | [diff] [blame] | 830 | #endif |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 831 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 832 | /* Computed GOTOs, or |
| 833 | the-optimization-commonly-but-improperly-known-as-"threaded code" |
| 834 | using gcc's labels-as-values extension |
| 835 | (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html). |
| 836 | |
| 837 | The traditional bytecode evaluation loop uses a "switch" statement, which |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 838 | decent compilers will optimize as a single indirect branch instruction |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 839 | combined with a lookup table of jump addresses. However, since the |
| 840 | indirect jump instruction is shared by all opcodes, the CPU will have a |
| 841 | hard time making the right prediction for where to jump next (actually, |
| 842 | it will be always wrong except in the uncommon case of a sequence of |
| 843 | several identical opcodes). |
| 844 | |
| 845 | "Threaded code" in contrast, uses an explicit jump table and an explicit |
| 846 | indirect jump instruction at the end of each opcode. Since the jump |
| 847 | instruction is at a different address for each opcode, the CPU will make a |
| 848 | separate prediction for each of these instructions, which is equivalent to |
| 849 | predicting the second opcode of each opcode pair. These predictions have |
| 850 | a much better chance to turn out valid, especially in small bytecode loops. |
| 851 | |
| 852 | A mispredicted branch on a modern CPU flushes the whole pipeline and |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 853 | can cost several CPU cycles (depending on the pipeline depth), |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 854 | and potentially many more instructions (depending on the pipeline width). |
| 855 | A correctly predicted branch, however, is nearly free. |
| 856 | |
| 857 | At the time of this writing, the "threaded code" version is up to 15-20% |
| 858 | faster than the normal "switch" version, depending on the compiler and the |
| 859 | CPU architecture. |
| 860 | |
| 861 | We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined, |
| 862 | because it would render the measurements invalid. |
| 863 | |
| 864 | |
| 865 | NOTE: care must be taken that the compiler doesn't try to "optimize" the |
| 866 | indirect jumps by sharing them between all opcodes. Such optimizations |
| 867 | can be disabled on gcc by using the -fno-gcse flag (or possibly |
| 868 | -fno-crossjumping). |
| 869 | */ |
| 870 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 871 | #ifdef DYNAMIC_EXECUTION_PROFILE |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 872 | #undef USE_COMPUTED_GOTOS |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 873 | #define USE_COMPUTED_GOTOS 0 |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 874 | #endif |
| 875 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 876 | #ifdef HAVE_COMPUTED_GOTOS |
| 877 | #ifndef USE_COMPUTED_GOTOS |
| 878 | #define USE_COMPUTED_GOTOS 1 |
| 879 | #endif |
| 880 | #else |
| 881 | #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS |
| 882 | #error "Computed gotos are not supported on this compiler." |
| 883 | #endif |
| 884 | #undef USE_COMPUTED_GOTOS |
| 885 | #define USE_COMPUTED_GOTOS 0 |
| 886 | #endif |
| 887 | |
| 888 | #if USE_COMPUTED_GOTOS |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 889 | /* Import the static jump table */ |
| 890 | #include "opcode_targets.h" |
| 891 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 892 | #define TARGET(op) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 893 | TARGET_##op: \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 894 | case op: |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 895 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 896 | #define DISPATCH() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 897 | { \ |
| 898 | if (!_Py_atomic_load_relaxed(&eval_breaker)) { \ |
| 899 | FAST_DISPATCH(); \ |
| 900 | } \ |
| 901 | continue; \ |
| 902 | } |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 903 | |
| 904 | #ifdef LLTRACE |
| 905 | #define FAST_DISPATCH() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 906 | { \ |
| 907 | if (!lltrace && !_Py_TracingPossible) { \ |
| 908 | f->f_lasti = INSTR_OFFSET(); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 909 | NEXTOPARG(); \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 910 | goto *opcode_targets[opcode]; \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 911 | } \ |
| 912 | goto fast_next_opcode; \ |
| 913 | } |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 914 | #else |
| 915 | #define FAST_DISPATCH() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 916 | { \ |
| 917 | if (!_Py_TracingPossible) { \ |
| 918 | f->f_lasti = INSTR_OFFSET(); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 919 | NEXTOPARG(); \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 920 | goto *opcode_targets[opcode]; \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 921 | } \ |
| 922 | goto fast_next_opcode; \ |
| 923 | } |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 924 | #endif |
| 925 | |
| 926 | #else |
| 927 | #define TARGET(op) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 928 | case op: |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 929 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 930 | #define DISPATCH() continue |
| 931 | #define FAST_DISPATCH() goto fast_next_opcode |
| 932 | #endif |
| 933 | |
| 934 | |
Neal Norwitz | a81d220 | 2002-07-14 00:27:26 +0000 | [diff] [blame] | 935 | /* Tuple access macros */ |
| 936 | |
| 937 | #ifndef Py_DEBUG |
| 938 | #define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i)) |
| 939 | #else |
| 940 | #define GETITEM(v, i) PyTuple_GetItem((v), (i)) |
| 941 | #endif |
| 942 | |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 943 | #ifdef WITH_TSC |
| 944 | /* Use Pentium timestamp counter to mark certain events: |
| 945 | inst0 -- beginning of switch statement for opcode dispatch |
| 946 | inst1 -- end of switch statement (may be skipped) |
| 947 | loop0 -- the top of the mainloop |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 948 | loop1 -- place where control returns again to top of mainloop |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 949 | (may be skipped) |
| 950 | intr1 -- beginning of long interruption |
| 951 | intr2 -- end of long interruption |
| 952 | |
| 953 | Many opcodes call out to helper C functions. In some cases, the |
| 954 | time in those functions should be counted towards the time for the |
| 955 | opcode, but not in all cases. For example, a CALL_FUNCTION opcode |
| 956 | calls another Python function; there's no point in charge all the |
| 957 | bytecode executed by the called function to the caller. |
| 958 | |
| 959 | It's hard to make a useful judgement statically. In the presence |
| 960 | of operator overloading, it's impossible to tell if a call will |
| 961 | execute new Python code or not. |
| 962 | |
| 963 | It's a case-by-case judgement. I'll use intr1 for the following |
| 964 | cases: |
| 965 | |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 966 | IMPORT_STAR |
| 967 | IMPORT_FROM |
| 968 | CALL_FUNCTION (and friends) |
| 969 | |
| 970 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 971 | uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0; |
| 972 | int ticked = 0; |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 973 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 974 | READ_TIMESTAMP(inst0); |
| 975 | READ_TIMESTAMP(inst1); |
| 976 | READ_TIMESTAMP(loop0); |
| 977 | READ_TIMESTAMP(loop1); |
Michael W. Hudson | 800ba23 | 2004-08-12 18:19:17 +0000 | [diff] [blame] | 978 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 979 | /* shut up the compiler */ |
| 980 | opcode = 0; |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 981 | #endif |
| 982 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 983 | /* Code access macros */ |
| 984 | |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 985 | #ifdef WORDS_BIGENDIAN |
| 986 | #define OPCODE(word) ((word) >> 8) |
| 987 | #define OPARG(word) ((word) & 255) |
| 988 | #else |
| 989 | #define OPCODE(word) ((word) & 255) |
| 990 | #define OPARG(word) ((word) >> 8) |
| 991 | #endif |
| 992 | /* The integer overflow is checked by an assertion below. */ |
| 993 | #define INSTR_OFFSET() (2*(int)(next_instr - first_instr)) |
| 994 | #define NEXTOPARG() do { \ |
| 995 | unsigned short word = *next_instr; \ |
| 996 | opcode = OPCODE(word); \ |
| 997 | oparg = OPARG(word); \ |
| 998 | next_instr++; \ |
| 999 | } while (0) |
| 1000 | #define JUMPTO(x) (next_instr = first_instr + (x)/2) |
| 1001 | #define JUMPBY(x) (next_instr += (x)/2) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1002 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 1003 | /* OpCode prediction macros |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1004 | Some opcodes tend to come in pairs thus making it possible to |
| 1005 | predict the second code when the first is run. For example, |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 1006 | 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] | 1007 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1008 | Verifying the prediction costs a single high-speed test of a register |
| 1009 | variable against a constant. If the pairing was good, then the |
| 1010 | processor's own internal branch predication has a high likelihood of |
| 1011 | success, resulting in a nearly zero-overhead transition to the |
| 1012 | next opcode. A successful prediction saves a trip through the eval-loop |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1013 | including its unpredictable switch-case branch. Combined with the |
| 1014 | processor's internal branch prediction, a successful PREDICT has the |
| 1015 | effect of making the two opcodes run as if they were a single new opcode |
| 1016 | with the bodies combined. |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 1017 | |
Georg Brandl | 86b2fb9 | 2008-07-16 03:43:04 +0000 | [diff] [blame] | 1018 | If collecting opcode statistics, your choices are to either keep the |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1019 | predictions turned-on and interpret the results as if some opcodes |
| 1020 | had been combined or turn-off predictions so that the opcode frequency |
| 1021 | counter updates for both opcodes. |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 1022 | |
| 1023 | Opcode prediction is disabled with threaded code, since the latter allows |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1024 | the CPU to record separate branch prediction information for each |
| 1025 | opcode. |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 1026 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 1027 | */ |
| 1028 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 1029 | #if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1030 | #define PREDICT(op) if (0) goto PRED_##op |
Raymond Hettinger | a721698 | 2004-02-08 19:59:27 +0000 | [diff] [blame] | 1031 | #else |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1032 | #define PREDICT(op) \ |
| 1033 | do{ \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 1034 | unsigned short word = *next_instr; \ |
| 1035 | opcode = OPCODE(word); \ |
| 1036 | if (opcode == op){ \ |
| 1037 | oparg = OPARG(word); \ |
| 1038 | next_instr++; \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1039 | goto PRED_##op; \ |
| 1040 | } \ |
| 1041 | } while(0) |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 1042 | #endif |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1043 | #define PREDICTED(op) PRED_##op: |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 1044 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 1045 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1046 | /* Stack manipulation macros */ |
| 1047 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1048 | /* The stack can grow at most MAXINT deep, as co_nlocals and |
| 1049 | co_stacksize are ints. */ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 1050 | #define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack)) |
| 1051 | #define EMPTY() (STACK_LEVEL() == 0) |
| 1052 | #define TOP() (stack_pointer[-1]) |
| 1053 | #define SECOND() (stack_pointer[-2]) |
| 1054 | #define THIRD() (stack_pointer[-3]) |
| 1055 | #define FOURTH() (stack_pointer[-4]) |
| 1056 | #define PEEK(n) (stack_pointer[-(n)]) |
| 1057 | #define SET_TOP(v) (stack_pointer[-1] = (v)) |
| 1058 | #define SET_SECOND(v) (stack_pointer[-2] = (v)) |
| 1059 | #define SET_THIRD(v) (stack_pointer[-3] = (v)) |
| 1060 | #define SET_FOURTH(v) (stack_pointer[-4] = (v)) |
| 1061 | #define SET_VALUE(n, v) (stack_pointer[-(n)] = (v)) |
| 1062 | #define BASIC_STACKADJ(n) (stack_pointer += n) |
| 1063 | #define BASIC_PUSH(v) (*stack_pointer++ = (v)) |
| 1064 | #define BASIC_POP() (*--stack_pointer) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1065 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 1066 | #ifdef LLTRACE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1067 | #define PUSH(v) { (void)(BASIC_PUSH(v), \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 1068 | lltrace && prtrace(TOP(), "push")); \ |
| 1069 | assert(STACK_LEVEL() <= co->co_stacksize); } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1070 | #define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 1071 | BASIC_POP()) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1072 | #define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 1073 | lltrace && prtrace(TOP(), "stackadj")); \ |
| 1074 | assert(STACK_LEVEL() <= co->co_stacksize); } |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 1075 | #define EXT_POP(STACK_POINTER) ((void)(lltrace && \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 1076 | prtrace((STACK_POINTER)[-1], "ext_pop")), \ |
| 1077 | *--(STACK_POINTER)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1078 | #else |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 1079 | #define PUSH(v) BASIC_PUSH(v) |
| 1080 | #define POP() BASIC_POP() |
| 1081 | #define STACKADJ(n) BASIC_STACKADJ(n) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 1082 | #define EXT_POP(STACK_POINTER) (*--(STACK_POINTER)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1083 | #endif |
| 1084 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1085 | /* Local variable macros */ |
| 1086 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1087 | #define GETLOCAL(i) (fastlocals[i]) |
Guido van Rossum | cfbf1a3 | 2002-03-28 20:17:52 +0000 | [diff] [blame] | 1088 | |
| 1089 | /* The SETLOCAL() macro must not DECREF the local variable in-place and |
| 1090 | then store the new value; it must copy the old value to a temporary |
| 1091 | value, then store the new value, and then DECREF the temporary value. |
| 1092 | This is because it is possible that during the DECREF the frame is |
| 1093 | accessed by other code (e.g. a __del__ method or gc.collect()) and the |
| 1094 | variable would be pointing to already-freed memory. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1095 | #define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 1096 | GETLOCAL(i) = value; \ |
| 1097 | Py_XDECREF(tmp); } while (0) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1098 | |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 1099 | |
| 1100 | #define UNWIND_BLOCK(b) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1101 | while (STACK_LEVEL() > (b)->b_level) { \ |
| 1102 | PyObject *v = POP(); \ |
| 1103 | Py_XDECREF(v); \ |
| 1104 | } |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 1105 | |
| 1106 | #define UNWIND_EXCEPT_HANDLER(b) \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1107 | do { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1108 | PyObject *type, *value, *traceback; \ |
| 1109 | assert(STACK_LEVEL() >= (b)->b_level + 3); \ |
| 1110 | while (STACK_LEVEL() > (b)->b_level + 3) { \ |
| 1111 | value = POP(); \ |
| 1112 | Py_XDECREF(value); \ |
| 1113 | } \ |
| 1114 | type = tstate->exc_type; \ |
| 1115 | value = tstate->exc_value; \ |
| 1116 | traceback = tstate->exc_traceback; \ |
| 1117 | tstate->exc_type = POP(); \ |
| 1118 | tstate->exc_value = POP(); \ |
| 1119 | tstate->exc_traceback = POP(); \ |
| 1120 | Py_XDECREF(type); \ |
| 1121 | Py_XDECREF(value); \ |
| 1122 | Py_XDECREF(traceback); \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1123 | } while(0) |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 1124 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 1125 | /* Start of code */ |
| 1126 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1127 | /* push frame */ |
| 1128 | if (Py_EnterRecursiveCall("")) |
| 1129 | return NULL; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1130 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1131 | tstate->frame = f; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1132 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1133 | if (tstate->use_tracing) { |
| 1134 | if (tstate->c_tracefunc != NULL) { |
| 1135 | /* tstate->c_tracefunc, if defined, is a |
| 1136 | function that will be called on *every* entry |
| 1137 | to a code block. Its return value, if not |
| 1138 | None, is a function that will be called at |
| 1139 | the start of each executed line of code. |
| 1140 | (Actually, the function must return itself |
| 1141 | in order to continue tracing.) The trace |
| 1142 | functions are called with three arguments: |
| 1143 | a pointer to the current frame, a string |
| 1144 | indicating why the function is called, and |
| 1145 | an argument which depends on the situation. |
| 1146 | The global trace function is also called |
| 1147 | whenever an exception is detected. */ |
| 1148 | if (call_trace_protected(tstate->c_tracefunc, |
| 1149 | tstate->c_traceobj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 1150 | tstate, f, PyTrace_CALL, Py_None)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1151 | /* Trace function raised an error */ |
| 1152 | goto exit_eval_frame; |
| 1153 | } |
| 1154 | } |
| 1155 | if (tstate->c_profilefunc != NULL) { |
| 1156 | /* Similar for c_profilefunc, except it needn't |
| 1157 | return itself and isn't called for "line" events */ |
| 1158 | if (call_trace_protected(tstate->c_profilefunc, |
| 1159 | tstate->c_profileobj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 1160 | tstate, f, PyTrace_CALL, Py_None)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1161 | /* Profile function raised an error */ |
| 1162 | goto exit_eval_frame; |
| 1163 | } |
| 1164 | } |
| 1165 | } |
Neil Schemenauer | 6c0f200 | 2001-09-04 19:03:35 +0000 | [diff] [blame] | 1166 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1167 | co = f->f_code; |
| 1168 | names = co->co_names; |
| 1169 | consts = co->co_consts; |
| 1170 | fastlocals = f->f_localsplus; |
| 1171 | freevars = f->f_localsplus + co->co_nlocals; |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 1172 | assert(PyBytes_Check(co->co_code)); |
| 1173 | assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX); |
| 1174 | assert(PyBytes_GET_SIZE(co->co_code) % 2 == 0); |
Serhiy Storchaka | 74f2fe6 | 2016-05-25 20:35:44 +0300 | [diff] [blame] | 1175 | assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), 2)); |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 1176 | first_instr = (unsigned short*) PyBytes_AS_STRING(co->co_code); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1177 | /* |
| 1178 | f->f_lasti refers to the index of the last instruction, |
| 1179 | 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] | 1180 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1181 | YIELD_FROM sets f_lasti to itself, in order to repeatedly yield |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 1182 | multiple values. |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 1183 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1184 | When the PREDICT() macros are enabled, some opcode pairs follow in |
| 1185 | direct succession without updating f->f_lasti. A successful |
| 1186 | prediction effectively links the two codes together as if they |
| 1187 | were a single new opcode; accordingly,f->f_lasti will point to |
| 1188 | the first code in the pair (for instance, GET_ITER followed by |
| 1189 | 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] | 1190 | to the beginning of the combined pair.) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1191 | */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1192 | next_instr = first_instr; |
| 1193 | if (f->f_lasti >= 0) { |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 1194 | assert(f->f_lasti % 2 == 0); |
| 1195 | next_instr += f->f_lasti/2 + 1; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1196 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1197 | stack_pointer = f->f_stacktop; |
| 1198 | assert(stack_pointer != NULL); |
| 1199 | f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */ |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 1200 | f->f_executing = 1; |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1201 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1202 | if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) { |
Victor Stinner | 26f7b8a | 2015-01-31 10:29:47 +0100 | [diff] [blame] | 1203 | 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] | 1204 | /* We were in an except handler when we left, |
| 1205 | restore the exception state which was put aside |
| 1206 | (see YIELD_VALUE). */ |
Benjamin Peterson | 8788024 | 2011-07-03 16:48:31 -0500 | [diff] [blame] | 1207 | swap_exc_state(tstate, f); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1208 | } |
Benjamin Peterson | 8788024 | 2011-07-03 16:48:31 -0500 | [diff] [blame] | 1209 | else |
| 1210 | save_exc_state(tstate, f); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1211 | } |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 1212 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1213 | #ifdef LLTRACE |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 1214 | lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1215 | #endif |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1216 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1217 | why = WHY_NOT; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1218 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1219 | if (throwflag) /* support for generator.throw() */ |
| 1220 | goto error; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1221 | |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 1222 | #ifdef Py_DEBUG |
| 1223 | /* PyEval_EvalFrameEx() must not be called with an exception set, |
| 1224 | because it may clear it (directly or indirectly) and so the |
Martin Panter | 9955a37 | 2015-10-07 10:26:23 +0000 | [diff] [blame] | 1225 | caller loses its exception */ |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 1226 | assert(!PyErr_Occurred()); |
| 1227 | #endif |
| 1228 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1229 | for (;;) { |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 1230 | #ifdef WITH_TSC |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1231 | if (inst1 == 0) { |
| 1232 | /* Almost surely, the opcode executed a break |
| 1233 | or a continue, preventing inst1 from being set |
| 1234 | on the way out of the loop. |
| 1235 | */ |
| 1236 | READ_TIMESTAMP(inst1); |
| 1237 | loop1 = inst1; |
| 1238 | } |
| 1239 | dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1, |
| 1240 | intr0, intr1); |
| 1241 | ticked = 0; |
| 1242 | inst1 = 0; |
| 1243 | intr0 = 0; |
| 1244 | intr1 = 0; |
| 1245 | READ_TIMESTAMP(loop0); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 1246 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1247 | assert(stack_pointer >= f->f_valuestack); /* else underflow */ |
| 1248 | assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */ |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 1249 | assert(!PyErr_Occurred()); |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 1250 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1251 | /* Do periodic things. Doing this every time through |
| 1252 | the loop would add too much overhead, so we do it |
| 1253 | only every Nth instruction. We also do it if |
| 1254 | ``pendingcalls_to_do'' is set, i.e. when an asynchronous |
| 1255 | event needs attention (e.g. a signal handler or |
| 1256 | async I/O handler); see Py_AddPendingCall() and |
| 1257 | Py_MakePendingCalls() above. */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1258 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1259 | if (_Py_atomic_load_relaxed(&eval_breaker)) { |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 1260 | if (OPCODE(*next_instr) == SETUP_FINALLY) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1261 | /* Make the last opcode before |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 1262 | a try: finally: block uninterruptible. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1263 | goto fast_next_opcode; |
| 1264 | } |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 1265 | #ifdef WITH_TSC |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1266 | ticked = 1; |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 1267 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1268 | if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1269 | if (Py_MakePendingCalls() < 0) |
| 1270 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1271 | } |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 1272 | #ifdef WITH_THREAD |
Benjamin Peterson | d2be5b4 | 2010-09-10 22:47:02 +0000 | [diff] [blame] | 1273 | if (_Py_atomic_load_relaxed(&gil_drop_request)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1274 | /* Give another thread a chance */ |
| 1275 | if (PyThreadState_Swap(NULL) != tstate) |
| 1276 | Py_FatalError("ceval: tstate mix-up"); |
| 1277 | drop_gil(tstate); |
| 1278 | |
| 1279 | /* Other threads may run now */ |
| 1280 | |
| 1281 | take_gil(tstate); |
Benjamin Peterson | 17548dd | 2014-06-16 22:59:07 -0700 | [diff] [blame] | 1282 | |
| 1283 | /* Check if we should make a quick exit. */ |
| 1284 | if (_Py_Finalizing && _Py_Finalizing != tstate) { |
| 1285 | drop_gil(tstate); |
| 1286 | PyThread_exit_thread(); |
| 1287 | } |
| 1288 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1289 | if (PyThreadState_Swap(tstate) != NULL) |
| 1290 | Py_FatalError("ceval: orphan tstate"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1291 | } |
Benjamin Peterson | d2be5b4 | 2010-09-10 22:47:02 +0000 | [diff] [blame] | 1292 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1293 | /* Check for asynchronous exceptions. */ |
| 1294 | if (tstate->async_exc != NULL) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1295 | PyObject *exc = tstate->async_exc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1296 | tstate->async_exc = NULL; |
| 1297 | UNSIGNAL_ASYNC_EXC(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1298 | PyErr_SetNone(exc); |
| 1299 | Py_DECREF(exc); |
| 1300 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1301 | } |
| 1302 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1303 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1304 | fast_next_opcode: |
| 1305 | f->f_lasti = INSTR_OFFSET(); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1306 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1307 | /* line-by-line tracing support */ |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1308 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1309 | if (_Py_TracingPossible && |
Benjamin Peterson | 51f4616 | 2013-01-23 08:38:47 -0500 | [diff] [blame] | 1310 | tstate->c_tracefunc != NULL && !tstate->tracing) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1311 | int err; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1312 | /* see maybe_call_line_trace |
| 1313 | for expository comments */ |
| 1314 | f->f_stacktop = stack_pointer; |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 1315 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1316 | err = maybe_call_line_trace(tstate->c_tracefunc, |
| 1317 | tstate->c_traceobj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 1318 | tstate, f, |
| 1319 | &instr_lb, &instr_ub, &instr_prev); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1320 | /* Reload possibly changed frame fields */ |
| 1321 | JUMPTO(f->f_lasti); |
| 1322 | if (f->f_stacktop != NULL) { |
| 1323 | stack_pointer = f->f_stacktop; |
| 1324 | f->f_stacktop = NULL; |
| 1325 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1326 | if (err) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1327 | /* trace function raised an exception */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1328 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1329 | } |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1330 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1331 | /* Extract opcode and argument */ |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1332 | |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 1333 | NEXTOPARG(); |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 1334 | dispatch_opcode: |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 1335 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 1336 | #ifdef DXPAIRS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1337 | dxpairs[lastopcode][opcode]++; |
| 1338 | lastopcode = opcode; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 1339 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1340 | dxp[opcode]++; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 1341 | #endif |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1342 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 1343 | #ifdef LLTRACE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1344 | /* Instruction tracing */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1345 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1346 | if (lltrace) { |
| 1347 | if (HAS_ARG(opcode)) { |
| 1348 | printf("%d: %d, %d\n", |
| 1349 | f->f_lasti, opcode, oparg); |
| 1350 | } |
| 1351 | else { |
| 1352 | printf("%d: %d\n", |
| 1353 | f->f_lasti, opcode); |
| 1354 | } |
| 1355 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1356 | #endif |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 1357 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1358 | /* Main switch on opcode */ |
| 1359 | READ_TIMESTAMP(inst0); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 1360 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1361 | switch (opcode) { |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1362 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1363 | /* BEWARE! |
| 1364 | It is essential that any operation that fails sets either |
| 1365 | x to NULL, err to nonzero, or why to anything but WHY_NOT, |
| 1366 | and that no operation that succeeds does this! */ |
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 | TARGET(NOP) |
| 1369 | FAST_DISPATCH(); |
Raymond Hettinger | 9c18e81 | 2004-06-21 16:31:15 +0000 | [diff] [blame] | 1370 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1371 | TARGET(LOAD_FAST) { |
| 1372 | PyObject *value = GETLOCAL(oparg); |
| 1373 | if (value == NULL) { |
| 1374 | format_exc_check_arg(PyExc_UnboundLocalError, |
| 1375 | UNBOUNDLOCAL_ERROR_MSG, |
| 1376 | PyTuple_GetItem(co->co_varnames, oparg)); |
| 1377 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1378 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1379 | Py_INCREF(value); |
| 1380 | PUSH(value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1381 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1382 | } |
| 1383 | |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 1384 | PREDICTED(LOAD_CONST); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1385 | TARGET(LOAD_CONST) { |
| 1386 | PyObject *value = GETITEM(consts, oparg); |
| 1387 | Py_INCREF(value); |
| 1388 | PUSH(value); |
| 1389 | FAST_DISPATCH(); |
| 1390 | } |
Neil Schemenauer | 6354386 | 2002-02-17 19:10:14 +0000 | [diff] [blame] | 1391 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1392 | PREDICTED(STORE_FAST); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1393 | TARGET(STORE_FAST) { |
| 1394 | PyObject *value = POP(); |
| 1395 | SETLOCAL(oparg, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1396 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1397 | } |
Neil Schemenauer | 6354386 | 2002-02-17 19:10:14 +0000 | [diff] [blame] | 1398 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1399 | TARGET(POP_TOP) { |
| 1400 | PyObject *value = POP(); |
| 1401 | Py_DECREF(value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1402 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1403 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1404 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1405 | TARGET(ROT_TWO) { |
| 1406 | PyObject *top = TOP(); |
| 1407 | PyObject *second = SECOND(); |
| 1408 | SET_TOP(second); |
| 1409 | SET_SECOND(top); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1410 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1411 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1412 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1413 | TARGET(ROT_THREE) { |
| 1414 | PyObject *top = TOP(); |
| 1415 | PyObject *second = SECOND(); |
| 1416 | PyObject *third = THIRD(); |
| 1417 | SET_TOP(second); |
| 1418 | SET_SECOND(third); |
| 1419 | SET_THIRD(top); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1420 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1421 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1422 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1423 | TARGET(DUP_TOP) { |
| 1424 | PyObject *top = TOP(); |
| 1425 | Py_INCREF(top); |
| 1426 | PUSH(top); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1427 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1428 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1429 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1430 | TARGET(DUP_TOP_TWO) { |
| 1431 | PyObject *top = TOP(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1432 | PyObject *second = SECOND(); |
Benjamin Peterson | f208df3 | 2012-10-12 11:37:56 -0400 | [diff] [blame] | 1433 | Py_INCREF(top); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1434 | Py_INCREF(second); |
Antoine Pitrou | 74a69fa | 2010-09-04 18:43:52 +0000 | [diff] [blame] | 1435 | STACKADJ(2); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1436 | SET_TOP(top); |
| 1437 | SET_SECOND(second); |
Antoine Pitrou | 74a69fa | 2010-09-04 18:43:52 +0000 | [diff] [blame] | 1438 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1439 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1440 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1441 | TARGET(UNARY_POSITIVE) { |
| 1442 | PyObject *value = TOP(); |
| 1443 | PyObject *res = PyNumber_Positive(value); |
| 1444 | Py_DECREF(value); |
| 1445 | SET_TOP(res); |
| 1446 | if (res == NULL) |
| 1447 | goto error; |
| 1448 | DISPATCH(); |
| 1449 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1450 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1451 | TARGET(UNARY_NEGATIVE) { |
| 1452 | PyObject *value = TOP(); |
| 1453 | PyObject *res = PyNumber_Negative(value); |
| 1454 | Py_DECREF(value); |
| 1455 | SET_TOP(res); |
| 1456 | if (res == NULL) |
| 1457 | goto error; |
| 1458 | DISPATCH(); |
| 1459 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1460 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1461 | TARGET(UNARY_NOT) { |
| 1462 | PyObject *value = TOP(); |
| 1463 | int err = PyObject_IsTrue(value); |
| 1464 | Py_DECREF(value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1465 | if (err == 0) { |
| 1466 | Py_INCREF(Py_True); |
| 1467 | SET_TOP(Py_True); |
| 1468 | DISPATCH(); |
| 1469 | } |
| 1470 | else if (err > 0) { |
| 1471 | Py_INCREF(Py_False); |
| 1472 | SET_TOP(Py_False); |
| 1473 | err = 0; |
| 1474 | DISPATCH(); |
| 1475 | } |
| 1476 | STACKADJ(-1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1477 | goto error; |
| 1478 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1479 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1480 | TARGET(UNARY_INVERT) { |
| 1481 | PyObject *value = TOP(); |
| 1482 | PyObject *res = PyNumber_Invert(value); |
| 1483 | Py_DECREF(value); |
| 1484 | SET_TOP(res); |
| 1485 | if (res == NULL) |
| 1486 | goto error; |
| 1487 | DISPATCH(); |
| 1488 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1489 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1490 | TARGET(BINARY_POWER) { |
| 1491 | PyObject *exp = POP(); |
| 1492 | PyObject *base = TOP(); |
| 1493 | PyObject *res = PyNumber_Power(base, exp, Py_None); |
| 1494 | Py_DECREF(base); |
| 1495 | Py_DECREF(exp); |
| 1496 | SET_TOP(res); |
| 1497 | if (res == NULL) |
| 1498 | goto error; |
| 1499 | DISPATCH(); |
| 1500 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1501 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1502 | TARGET(BINARY_MULTIPLY) { |
| 1503 | PyObject *right = POP(); |
| 1504 | PyObject *left = TOP(); |
| 1505 | PyObject *res = PyNumber_Multiply(left, right); |
| 1506 | Py_DECREF(left); |
| 1507 | Py_DECREF(right); |
| 1508 | SET_TOP(res); |
| 1509 | if (res == NULL) |
| 1510 | goto error; |
| 1511 | DISPATCH(); |
| 1512 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1513 | |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1514 | TARGET(BINARY_MATRIX_MULTIPLY) { |
| 1515 | PyObject *right = POP(); |
| 1516 | PyObject *left = TOP(); |
| 1517 | PyObject *res = PyNumber_MatrixMultiply(left, right); |
| 1518 | Py_DECREF(left); |
| 1519 | Py_DECREF(right); |
| 1520 | SET_TOP(res); |
| 1521 | if (res == NULL) |
| 1522 | goto error; |
| 1523 | DISPATCH(); |
| 1524 | } |
| 1525 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1526 | TARGET(BINARY_TRUE_DIVIDE) { |
| 1527 | PyObject *divisor = POP(); |
| 1528 | PyObject *dividend = TOP(); |
| 1529 | PyObject *quotient = PyNumber_TrueDivide(dividend, divisor); |
| 1530 | Py_DECREF(dividend); |
| 1531 | Py_DECREF(divisor); |
| 1532 | SET_TOP(quotient); |
| 1533 | if (quotient == NULL) |
| 1534 | goto error; |
| 1535 | DISPATCH(); |
| 1536 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1537 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1538 | TARGET(BINARY_FLOOR_DIVIDE) { |
| 1539 | PyObject *divisor = POP(); |
| 1540 | PyObject *dividend = TOP(); |
| 1541 | PyObject *quotient = PyNumber_FloorDivide(dividend, divisor); |
| 1542 | Py_DECREF(dividend); |
| 1543 | Py_DECREF(divisor); |
| 1544 | SET_TOP(quotient); |
| 1545 | if (quotient == NULL) |
| 1546 | goto error; |
| 1547 | DISPATCH(); |
| 1548 | } |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1549 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1550 | TARGET(BINARY_MODULO) { |
| 1551 | PyObject *divisor = POP(); |
| 1552 | PyObject *dividend = TOP(); |
| 1553 | PyObject *res = PyUnicode_CheckExact(dividend) ? |
| 1554 | PyUnicode_Format(dividend, divisor) : |
| 1555 | PyNumber_Remainder(dividend, divisor); |
| 1556 | Py_DECREF(divisor); |
| 1557 | Py_DECREF(dividend); |
| 1558 | SET_TOP(res); |
| 1559 | if (res == NULL) |
| 1560 | goto error; |
| 1561 | DISPATCH(); |
| 1562 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1563 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1564 | TARGET(BINARY_ADD) { |
| 1565 | PyObject *right = POP(); |
| 1566 | PyObject *left = TOP(); |
| 1567 | PyObject *sum; |
| 1568 | if (PyUnicode_CheckExact(left) && |
| 1569 | PyUnicode_CheckExact(right)) { |
| 1570 | sum = unicode_concatenate(left, right, f, next_instr); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1571 | /* unicode_concatenate consumed the ref to left */ |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 1572 | } |
| 1573 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1574 | sum = PyNumber_Add(left, right); |
| 1575 | Py_DECREF(left); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 1576 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1577 | Py_DECREF(right); |
| 1578 | SET_TOP(sum); |
| 1579 | if (sum == NULL) |
| 1580 | goto error; |
| 1581 | DISPATCH(); |
| 1582 | } |
| 1583 | |
| 1584 | TARGET(BINARY_SUBTRACT) { |
| 1585 | PyObject *right = POP(); |
| 1586 | PyObject *left = TOP(); |
| 1587 | PyObject *diff = PyNumber_Subtract(left, right); |
| 1588 | Py_DECREF(right); |
| 1589 | Py_DECREF(left); |
| 1590 | SET_TOP(diff); |
| 1591 | if (diff == NULL) |
| 1592 | goto error; |
| 1593 | DISPATCH(); |
| 1594 | } |
| 1595 | |
| 1596 | TARGET(BINARY_SUBSCR) { |
| 1597 | PyObject *sub = POP(); |
| 1598 | PyObject *container = TOP(); |
| 1599 | PyObject *res = PyObject_GetItem(container, sub); |
| 1600 | Py_DECREF(container); |
| 1601 | Py_DECREF(sub); |
| 1602 | SET_TOP(res); |
| 1603 | if (res == NULL) |
| 1604 | goto error; |
| 1605 | DISPATCH(); |
| 1606 | } |
| 1607 | |
| 1608 | TARGET(BINARY_LSHIFT) { |
| 1609 | PyObject *right = POP(); |
| 1610 | PyObject *left = TOP(); |
| 1611 | PyObject *res = PyNumber_Lshift(left, right); |
| 1612 | Py_DECREF(left); |
| 1613 | Py_DECREF(right); |
| 1614 | SET_TOP(res); |
| 1615 | if (res == NULL) |
| 1616 | goto error; |
| 1617 | DISPATCH(); |
| 1618 | } |
| 1619 | |
| 1620 | TARGET(BINARY_RSHIFT) { |
| 1621 | PyObject *right = POP(); |
| 1622 | PyObject *left = TOP(); |
| 1623 | PyObject *res = PyNumber_Rshift(left, right); |
| 1624 | Py_DECREF(left); |
| 1625 | Py_DECREF(right); |
| 1626 | SET_TOP(res); |
| 1627 | if (res == NULL) |
| 1628 | goto error; |
| 1629 | DISPATCH(); |
| 1630 | } |
| 1631 | |
| 1632 | TARGET(BINARY_AND) { |
| 1633 | PyObject *right = POP(); |
| 1634 | PyObject *left = TOP(); |
| 1635 | PyObject *res = PyNumber_And(left, right); |
| 1636 | Py_DECREF(left); |
| 1637 | Py_DECREF(right); |
| 1638 | SET_TOP(res); |
| 1639 | if (res == NULL) |
| 1640 | goto error; |
| 1641 | DISPATCH(); |
| 1642 | } |
| 1643 | |
| 1644 | TARGET(BINARY_XOR) { |
| 1645 | PyObject *right = POP(); |
| 1646 | PyObject *left = TOP(); |
| 1647 | PyObject *res = PyNumber_Xor(left, right); |
| 1648 | Py_DECREF(left); |
| 1649 | Py_DECREF(right); |
| 1650 | SET_TOP(res); |
| 1651 | if (res == NULL) |
| 1652 | goto error; |
| 1653 | DISPATCH(); |
| 1654 | } |
| 1655 | |
| 1656 | TARGET(BINARY_OR) { |
| 1657 | PyObject *right = POP(); |
| 1658 | PyObject *left = TOP(); |
| 1659 | PyObject *res = PyNumber_Or(left, right); |
| 1660 | Py_DECREF(left); |
| 1661 | Py_DECREF(right); |
| 1662 | SET_TOP(res); |
| 1663 | if (res == NULL) |
| 1664 | goto error; |
| 1665 | DISPATCH(); |
| 1666 | } |
| 1667 | |
| 1668 | TARGET(LIST_APPEND) { |
| 1669 | PyObject *v = POP(); |
| 1670 | PyObject *list = PEEK(oparg); |
| 1671 | int err; |
| 1672 | err = PyList_Append(list, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1673 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1674 | if (err != 0) |
| 1675 | goto error; |
| 1676 | PREDICT(JUMP_ABSOLUTE); |
| 1677 | DISPATCH(); |
| 1678 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1679 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1680 | TARGET(SET_ADD) { |
| 1681 | PyObject *v = POP(); |
| 1682 | PyObject *set = stack_pointer[-oparg]; |
| 1683 | int err; |
| 1684 | err = PySet_Add(set, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1685 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1686 | if (err != 0) |
| 1687 | goto error; |
| 1688 | PREDICT(JUMP_ABSOLUTE); |
| 1689 | DISPATCH(); |
| 1690 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1691 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1692 | TARGET(INPLACE_POWER) { |
| 1693 | PyObject *exp = POP(); |
| 1694 | PyObject *base = TOP(); |
| 1695 | PyObject *res = PyNumber_InPlacePower(base, exp, Py_None); |
| 1696 | Py_DECREF(base); |
| 1697 | Py_DECREF(exp); |
| 1698 | SET_TOP(res); |
| 1699 | if (res == NULL) |
| 1700 | goto error; |
| 1701 | DISPATCH(); |
| 1702 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1703 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1704 | TARGET(INPLACE_MULTIPLY) { |
| 1705 | PyObject *right = POP(); |
| 1706 | PyObject *left = TOP(); |
| 1707 | PyObject *res = PyNumber_InPlaceMultiply(left, right); |
| 1708 | Py_DECREF(left); |
| 1709 | Py_DECREF(right); |
| 1710 | SET_TOP(res); |
| 1711 | if (res == NULL) |
| 1712 | goto error; |
| 1713 | DISPATCH(); |
| 1714 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1715 | |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1716 | TARGET(INPLACE_MATRIX_MULTIPLY) { |
| 1717 | PyObject *right = POP(); |
| 1718 | PyObject *left = TOP(); |
| 1719 | PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right); |
| 1720 | Py_DECREF(left); |
| 1721 | Py_DECREF(right); |
| 1722 | SET_TOP(res); |
| 1723 | if (res == NULL) |
| 1724 | goto error; |
| 1725 | DISPATCH(); |
| 1726 | } |
| 1727 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1728 | TARGET(INPLACE_TRUE_DIVIDE) { |
| 1729 | PyObject *divisor = POP(); |
| 1730 | PyObject *dividend = TOP(); |
| 1731 | PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor); |
| 1732 | Py_DECREF(dividend); |
| 1733 | Py_DECREF(divisor); |
| 1734 | SET_TOP(quotient); |
| 1735 | if (quotient == NULL) |
| 1736 | goto error; |
| 1737 | DISPATCH(); |
| 1738 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1739 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1740 | TARGET(INPLACE_FLOOR_DIVIDE) { |
| 1741 | PyObject *divisor = POP(); |
| 1742 | PyObject *dividend = TOP(); |
| 1743 | PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor); |
| 1744 | Py_DECREF(dividend); |
| 1745 | Py_DECREF(divisor); |
| 1746 | SET_TOP(quotient); |
| 1747 | if (quotient == NULL) |
| 1748 | goto error; |
| 1749 | DISPATCH(); |
| 1750 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1751 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1752 | TARGET(INPLACE_MODULO) { |
| 1753 | PyObject *right = POP(); |
| 1754 | PyObject *left = TOP(); |
| 1755 | PyObject *mod = PyNumber_InPlaceRemainder(left, right); |
| 1756 | Py_DECREF(left); |
| 1757 | Py_DECREF(right); |
| 1758 | SET_TOP(mod); |
| 1759 | if (mod == NULL) |
| 1760 | goto error; |
| 1761 | DISPATCH(); |
| 1762 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1763 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1764 | TARGET(INPLACE_ADD) { |
| 1765 | PyObject *right = POP(); |
| 1766 | PyObject *left = TOP(); |
| 1767 | PyObject *sum; |
| 1768 | if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) { |
| 1769 | sum = unicode_concatenate(left, right, f, next_instr); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1770 | /* unicode_concatenate consumed the ref to left */ |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 1771 | } |
| 1772 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1773 | sum = PyNumber_InPlaceAdd(left, right); |
| 1774 | Py_DECREF(left); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 1775 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1776 | Py_DECREF(right); |
| 1777 | SET_TOP(sum); |
| 1778 | if (sum == NULL) |
| 1779 | goto error; |
| 1780 | DISPATCH(); |
| 1781 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1782 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1783 | TARGET(INPLACE_SUBTRACT) { |
| 1784 | PyObject *right = POP(); |
| 1785 | PyObject *left = TOP(); |
| 1786 | PyObject *diff = PyNumber_InPlaceSubtract(left, right); |
| 1787 | Py_DECREF(left); |
| 1788 | Py_DECREF(right); |
| 1789 | SET_TOP(diff); |
| 1790 | if (diff == NULL) |
| 1791 | goto error; |
| 1792 | DISPATCH(); |
| 1793 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1794 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1795 | TARGET(INPLACE_LSHIFT) { |
| 1796 | PyObject *right = POP(); |
| 1797 | PyObject *left = TOP(); |
| 1798 | PyObject *res = PyNumber_InPlaceLshift(left, right); |
| 1799 | Py_DECREF(left); |
| 1800 | Py_DECREF(right); |
| 1801 | SET_TOP(res); |
| 1802 | if (res == NULL) |
| 1803 | goto error; |
| 1804 | DISPATCH(); |
| 1805 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1806 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1807 | TARGET(INPLACE_RSHIFT) { |
| 1808 | PyObject *right = POP(); |
| 1809 | PyObject *left = TOP(); |
| 1810 | PyObject *res = PyNumber_InPlaceRshift(left, right); |
| 1811 | Py_DECREF(left); |
| 1812 | Py_DECREF(right); |
| 1813 | SET_TOP(res); |
| 1814 | if (res == NULL) |
| 1815 | goto error; |
| 1816 | DISPATCH(); |
| 1817 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1818 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1819 | TARGET(INPLACE_AND) { |
| 1820 | PyObject *right = POP(); |
| 1821 | PyObject *left = TOP(); |
| 1822 | PyObject *res = PyNumber_InPlaceAnd(left, right); |
| 1823 | Py_DECREF(left); |
| 1824 | Py_DECREF(right); |
| 1825 | SET_TOP(res); |
| 1826 | if (res == NULL) |
| 1827 | goto error; |
| 1828 | DISPATCH(); |
| 1829 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1830 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1831 | TARGET(INPLACE_XOR) { |
| 1832 | PyObject *right = POP(); |
| 1833 | PyObject *left = TOP(); |
| 1834 | PyObject *res = PyNumber_InPlaceXor(left, right); |
| 1835 | Py_DECREF(left); |
| 1836 | Py_DECREF(right); |
| 1837 | SET_TOP(res); |
| 1838 | if (res == NULL) |
| 1839 | goto error; |
| 1840 | DISPATCH(); |
| 1841 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1842 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1843 | TARGET(INPLACE_OR) { |
| 1844 | PyObject *right = POP(); |
| 1845 | PyObject *left = TOP(); |
| 1846 | PyObject *res = PyNumber_InPlaceOr(left, right); |
| 1847 | Py_DECREF(left); |
| 1848 | Py_DECREF(right); |
| 1849 | SET_TOP(res); |
| 1850 | if (res == NULL) |
| 1851 | goto error; |
| 1852 | DISPATCH(); |
| 1853 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1854 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1855 | TARGET(STORE_SUBSCR) { |
| 1856 | PyObject *sub = TOP(); |
| 1857 | PyObject *container = SECOND(); |
| 1858 | PyObject *v = THIRD(); |
| 1859 | int err; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1860 | STACKADJ(-3); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1861 | /* container[sub] = v */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1862 | err = PyObject_SetItem(container, sub, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1863 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1864 | Py_DECREF(container); |
| 1865 | Py_DECREF(sub); |
| 1866 | if (err != 0) |
| 1867 | goto error; |
| 1868 | DISPATCH(); |
| 1869 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1870 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1871 | TARGET(STORE_ANNOTATION) { |
| 1872 | _Py_IDENTIFIER(__annotations__); |
| 1873 | PyObject *ann_dict; |
| 1874 | PyObject *ann = POP(); |
| 1875 | PyObject *name = GETITEM(names, oparg); |
| 1876 | int err; |
| 1877 | if (f->f_locals == NULL) { |
| 1878 | PyErr_Format(PyExc_SystemError, |
| 1879 | "no locals found when storing annotation"); |
| 1880 | Py_DECREF(ann); |
| 1881 | goto error; |
| 1882 | } |
| 1883 | /* first try to get __annotations__ from locals... */ |
| 1884 | if (PyDict_CheckExact(f->f_locals)) { |
| 1885 | ann_dict = _PyDict_GetItemId(f->f_locals, |
| 1886 | &PyId___annotations__); |
| 1887 | if (ann_dict == NULL) { |
| 1888 | PyErr_SetString(PyExc_NameError, |
| 1889 | "__annotations__ not found"); |
| 1890 | Py_DECREF(ann); |
| 1891 | goto error; |
| 1892 | } |
| 1893 | Py_INCREF(ann_dict); |
| 1894 | } |
| 1895 | else { |
| 1896 | PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__); |
| 1897 | if (ann_str == NULL) { |
| 1898 | Py_DECREF(ann); |
| 1899 | goto error; |
| 1900 | } |
| 1901 | ann_dict = PyObject_GetItem(f->f_locals, ann_str); |
| 1902 | if (ann_dict == NULL) { |
| 1903 | if (PyErr_ExceptionMatches(PyExc_KeyError)) { |
| 1904 | PyErr_SetString(PyExc_NameError, |
| 1905 | "__annotations__ not found"); |
| 1906 | } |
| 1907 | Py_DECREF(ann); |
| 1908 | goto error; |
| 1909 | } |
| 1910 | } |
| 1911 | /* ...if succeeded, __annotations__[name] = ann */ |
| 1912 | if (PyDict_CheckExact(ann_dict)) { |
| 1913 | err = PyDict_SetItem(ann_dict, name, ann); |
| 1914 | } |
| 1915 | else { |
| 1916 | err = PyObject_SetItem(ann_dict, name, ann); |
| 1917 | } |
| 1918 | Py_DECREF(ann_dict); |
Yury Selivanov | 50c584f | 2016-09-08 23:38:21 -0700 | [diff] [blame] | 1919 | Py_DECREF(ann); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1920 | if (err != 0) { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1921 | goto error; |
| 1922 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1923 | DISPATCH(); |
| 1924 | } |
| 1925 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1926 | TARGET(DELETE_SUBSCR) { |
| 1927 | PyObject *sub = TOP(); |
| 1928 | PyObject *container = SECOND(); |
| 1929 | int err; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1930 | STACKADJ(-2); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1931 | /* del container[sub] */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1932 | err = PyObject_DelItem(container, sub); |
| 1933 | Py_DECREF(container); |
| 1934 | Py_DECREF(sub); |
| 1935 | if (err != 0) |
| 1936 | goto error; |
| 1937 | DISPATCH(); |
| 1938 | } |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1939 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1940 | TARGET(PRINT_EXPR) { |
Victor Stinner | cab75e3 | 2013-11-06 22:38:37 +0100 | [diff] [blame] | 1941 | _Py_IDENTIFIER(displayhook); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1942 | PyObject *value = POP(); |
Victor Stinner | cab75e3 | 2013-11-06 22:38:37 +0100 | [diff] [blame] | 1943 | PyObject *hook = _PySys_GetObjectId(&PyId_displayhook); |
Benjamin Peterson | fe1bcb6 | 2012-10-12 11:40:01 -0400 | [diff] [blame] | 1944 | PyObject *res; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1945 | if (hook == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1946 | PyErr_SetString(PyExc_RuntimeError, |
| 1947 | "lost sys.displayhook"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1948 | Py_DECREF(value); |
| 1949 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1950 | } |
Benjamin Peterson | fe1bcb6 | 2012-10-12 11:40:01 -0400 | [diff] [blame] | 1951 | res = PyObject_CallFunctionObjArgs(hook, value, NULL); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1952 | Py_DECREF(value); |
| 1953 | if (res == NULL) |
| 1954 | goto error; |
| 1955 | Py_DECREF(res); |
| 1956 | DISPATCH(); |
| 1957 | } |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 1958 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1959 | #ifdef CASE_TOO_BIG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1960 | default: switch (opcode) { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1961 | #endif |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1962 | TARGET(RAISE_VARARGS) { |
| 1963 | PyObject *cause = NULL, *exc = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1964 | switch (oparg) { |
| 1965 | case 2: |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1966 | cause = POP(); /* cause */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1967 | case 1: |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1968 | exc = POP(); /* exc */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1969 | case 0: /* Fallthrough */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1970 | if (do_raise(exc, cause)) { |
| 1971 | why = WHY_EXCEPTION; |
| 1972 | goto fast_block_end; |
| 1973 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1974 | break; |
| 1975 | default: |
| 1976 | PyErr_SetString(PyExc_SystemError, |
| 1977 | "bad RAISE_VARARGS oparg"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1978 | break; |
| 1979 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1980 | goto error; |
| 1981 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1982 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1983 | TARGET(RETURN_VALUE) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1984 | retval = POP(); |
| 1985 | why = WHY_RETURN; |
| 1986 | goto fast_block_end; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1987 | } |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 1988 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1989 | TARGET(GET_AITER) { |
Yury Selivanov | 6ef0590 | 2015-05-28 11:21:31 -0400 | [diff] [blame] | 1990 | unaryfunc getter = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1991 | PyObject *iter = NULL; |
| 1992 | PyObject *awaitable = NULL; |
| 1993 | PyObject *obj = TOP(); |
| 1994 | PyTypeObject *type = Py_TYPE(obj); |
| 1995 | |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 1996 | if (type->tp_as_async != NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1997 | getter = type->tp_as_async->am_aiter; |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 1998 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1999 | |
| 2000 | if (getter != NULL) { |
| 2001 | iter = (*getter)(obj); |
| 2002 | Py_DECREF(obj); |
| 2003 | if (iter == NULL) { |
| 2004 | SET_TOP(NULL); |
| 2005 | goto error; |
| 2006 | } |
| 2007 | } |
| 2008 | else { |
| 2009 | SET_TOP(NULL); |
| 2010 | PyErr_Format( |
| 2011 | PyExc_TypeError, |
| 2012 | "'async for' requires an object with " |
| 2013 | "__aiter__ method, got %.100s", |
| 2014 | type->tp_name); |
| 2015 | Py_DECREF(obj); |
| 2016 | goto error; |
| 2017 | } |
| 2018 | |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 2019 | if (Py_TYPE(iter)->tp_as_async != NULL && |
| 2020 | Py_TYPE(iter)->tp_as_async->am_anext != NULL) { |
| 2021 | |
| 2022 | /* Starting with CPython 3.5.2 __aiter__ should return |
| 2023 | asynchronous iterators directly (not awaitables that |
| 2024 | resolve to asynchronous iterators.) |
| 2025 | |
| 2026 | Therefore, we check if the object that was returned |
| 2027 | from __aiter__ has an __anext__ method. If it does, |
| 2028 | we wrap it in an awaitable that resolves to `iter`. |
| 2029 | |
| 2030 | See http://bugs.python.org/issue27243 for more |
| 2031 | details. |
| 2032 | */ |
| 2033 | |
| 2034 | PyObject *wrapper = _PyAIterWrapper_New(iter); |
| 2035 | Py_DECREF(iter); |
| 2036 | SET_TOP(wrapper); |
| 2037 | DISPATCH(); |
| 2038 | } |
| 2039 | |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 2040 | awaitable = _PyCoro_GetAwaitableIter(iter); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2041 | if (awaitable == NULL) { |
| 2042 | SET_TOP(NULL); |
| 2043 | PyErr_Format( |
| 2044 | PyExc_TypeError, |
| 2045 | "'async for' received an invalid object " |
| 2046 | "from __aiter__: %.100s", |
| 2047 | Py_TYPE(iter)->tp_name); |
| 2048 | |
| 2049 | Py_DECREF(iter); |
| 2050 | goto error; |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 2051 | } else { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2052 | Py_DECREF(iter); |
| 2053 | |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 2054 | if (PyErr_WarnFormat( |
| 2055 | PyExc_PendingDeprecationWarning, 1, |
| 2056 | "'%.100s' implements legacy __aiter__ protocol; " |
| 2057 | "__aiter__ should return an asynchronous " |
| 2058 | "iterator, not awaitable", |
| 2059 | type->tp_name)) |
| 2060 | { |
| 2061 | /* Warning was converted to an error. */ |
| 2062 | Py_DECREF(awaitable); |
| 2063 | SET_TOP(NULL); |
| 2064 | goto error; |
| 2065 | } |
| 2066 | } |
| 2067 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2068 | SET_TOP(awaitable); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 2069 | PREDICT(LOAD_CONST); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2070 | DISPATCH(); |
| 2071 | } |
| 2072 | |
| 2073 | TARGET(GET_ANEXT) { |
Yury Selivanov | 6ef0590 | 2015-05-28 11:21:31 -0400 | [diff] [blame] | 2074 | unaryfunc getter = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2075 | PyObject *next_iter = NULL; |
| 2076 | PyObject *awaitable = NULL; |
| 2077 | PyObject *aiter = TOP(); |
| 2078 | PyTypeObject *type = Py_TYPE(aiter); |
| 2079 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 2080 | if (PyAsyncGen_CheckExact(aiter)) { |
| 2081 | awaitable = type->tp_as_async->am_anext(aiter); |
| 2082 | if (awaitable == NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2083 | goto error; |
| 2084 | } |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 2085 | } else { |
| 2086 | if (type->tp_as_async != NULL){ |
| 2087 | getter = type->tp_as_async->am_anext; |
| 2088 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2089 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 2090 | if (getter != NULL) { |
| 2091 | next_iter = (*getter)(aiter); |
| 2092 | if (next_iter == NULL) { |
| 2093 | goto error; |
| 2094 | } |
| 2095 | } |
| 2096 | else { |
| 2097 | PyErr_Format( |
| 2098 | PyExc_TypeError, |
| 2099 | "'async for' requires an iterator with " |
| 2100 | "__anext__ method, got %.100s", |
| 2101 | type->tp_name); |
| 2102 | goto error; |
| 2103 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2104 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 2105 | awaitable = _PyCoro_GetAwaitableIter(next_iter); |
| 2106 | if (awaitable == NULL) { |
| 2107 | PyErr_Format( |
| 2108 | PyExc_TypeError, |
| 2109 | "'async for' received an invalid object " |
| 2110 | "from __anext__: %.100s", |
| 2111 | Py_TYPE(next_iter)->tp_name); |
| 2112 | |
| 2113 | Py_DECREF(next_iter); |
| 2114 | goto error; |
| 2115 | } else { |
| 2116 | Py_DECREF(next_iter); |
| 2117 | } |
| 2118 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2119 | |
| 2120 | PUSH(awaitable); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 2121 | PREDICT(LOAD_CONST); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2122 | DISPATCH(); |
| 2123 | } |
| 2124 | |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 2125 | PREDICTED(GET_AWAITABLE); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2126 | TARGET(GET_AWAITABLE) { |
| 2127 | PyObject *iterable = TOP(); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 2128 | PyObject *iter = _PyCoro_GetAwaitableIter(iterable); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2129 | |
| 2130 | Py_DECREF(iterable); |
| 2131 | |
Yury Selivanov | c724bae | 2016-03-02 11:30:46 -0500 | [diff] [blame] | 2132 | if (iter != NULL && PyCoro_CheckExact(iter)) { |
| 2133 | PyObject *yf = _PyGen_yf((PyGenObject*)iter); |
| 2134 | if (yf != NULL) { |
| 2135 | /* `iter` is a coroutine object that is being |
| 2136 | awaited, `yf` is a pointer to the current awaitable |
| 2137 | being awaited on. */ |
| 2138 | Py_DECREF(yf); |
| 2139 | Py_CLEAR(iter); |
| 2140 | PyErr_SetString( |
| 2141 | PyExc_RuntimeError, |
| 2142 | "coroutine is being awaited already"); |
| 2143 | /* The code below jumps to `error` if `iter` is NULL. */ |
| 2144 | } |
| 2145 | } |
| 2146 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2147 | SET_TOP(iter); /* Even if it's NULL */ |
| 2148 | |
| 2149 | if (iter == NULL) { |
| 2150 | goto error; |
| 2151 | } |
| 2152 | |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 2153 | PREDICT(LOAD_CONST); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2154 | DISPATCH(); |
| 2155 | } |
| 2156 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2157 | TARGET(YIELD_FROM) { |
| 2158 | PyObject *v = POP(); |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 2159 | PyObject *receiver = TOP(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2160 | int err; |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 2161 | if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) { |
| 2162 | retval = _PyGen_Send((PyGenObject *)receiver, v); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 2163 | } else { |
Benjamin Peterson | 302e790 | 2012-03-20 23:17:04 -0400 | [diff] [blame] | 2164 | _Py_IDENTIFIER(send); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2165 | if (v == Py_None) |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 2166 | retval = Py_TYPE(receiver)->tp_iternext(receiver); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 2167 | else |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 2168 | retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 2169 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2170 | Py_DECREF(v); |
| 2171 | if (retval == NULL) { |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 2172 | PyObject *val; |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 2173 | if (tstate->c_tracefunc != NULL |
| 2174 | && PyErr_ExceptionMatches(PyExc_StopIteration)) |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 2175 | call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f); |
Nick Coghlan | c40bc09 | 2012-06-17 15:15:49 +1000 | [diff] [blame] | 2176 | err = _PyGen_FetchStopIterationValue(&val); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2177 | if (err < 0) |
| 2178 | goto error; |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 2179 | Py_DECREF(receiver); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2180 | SET_TOP(val); |
| 2181 | DISPATCH(); |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2182 | } |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 2183 | /* receiver remains on stack, retval is value to be yielded */ |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2184 | f->f_stacktop = stack_pointer; |
| 2185 | why = WHY_YIELD; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 2186 | /* and repeat... */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 2187 | f->f_lasti -= 2; |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2188 | goto fast_yield; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2189 | } |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2190 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2191 | TARGET(YIELD_VALUE) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2192 | retval = POP(); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 2193 | |
| 2194 | if (co->co_flags & CO_ASYNC_GENERATOR) { |
| 2195 | PyObject *w = _PyAsyncGenValueWrapperNew(retval); |
| 2196 | Py_DECREF(retval); |
| 2197 | if (w == NULL) { |
| 2198 | retval = NULL; |
| 2199 | goto error; |
| 2200 | } |
| 2201 | retval = w; |
| 2202 | } |
| 2203 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2204 | f->f_stacktop = stack_pointer; |
| 2205 | why = WHY_YIELD; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2206 | goto fast_yield; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2207 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2208 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2209 | TARGET(POP_EXCEPT) { |
| 2210 | PyTryBlock *b = PyFrame_BlockPop(f); |
| 2211 | if (b->b_type != EXCEPT_HANDLER) { |
| 2212 | PyErr_SetString(PyExc_SystemError, |
| 2213 | "popped block is not an except handler"); |
| 2214 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2215 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2216 | UNWIND_EXCEPT_HANDLER(b); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2217 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2218 | } |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 2219 | |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 2220 | PREDICTED(POP_BLOCK); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2221 | TARGET(POP_BLOCK) { |
| 2222 | PyTryBlock *b = PyFrame_BlockPop(f); |
| 2223 | UNWIND_BLOCK(b); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2224 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2225 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2226 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2227 | PREDICTED(END_FINALLY); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2228 | TARGET(END_FINALLY) { |
| 2229 | PyObject *status = POP(); |
| 2230 | if (PyLong_Check(status)) { |
| 2231 | why = (enum why_code) PyLong_AS_LONG(status); |
| 2232 | assert(why != WHY_YIELD && why != WHY_EXCEPTION); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2233 | if (why == WHY_RETURN || |
| 2234 | why == WHY_CONTINUE) |
| 2235 | retval = POP(); |
| 2236 | if (why == WHY_SILENCED) { |
| 2237 | /* An exception was silenced by 'with', we must |
| 2238 | manually unwind the EXCEPT_HANDLER block which was |
| 2239 | created when the exception was caught, otherwise |
| 2240 | the stack will be in an inconsistent state. */ |
| 2241 | PyTryBlock *b = PyFrame_BlockPop(f); |
| 2242 | assert(b->b_type == EXCEPT_HANDLER); |
| 2243 | UNWIND_EXCEPT_HANDLER(b); |
| 2244 | why = WHY_NOT; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2245 | Py_DECREF(status); |
| 2246 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2247 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2248 | Py_DECREF(status); |
| 2249 | goto fast_block_end; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2250 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2251 | else if (PyExceptionClass_Check(status)) { |
| 2252 | PyObject *exc = POP(); |
| 2253 | PyObject *tb = POP(); |
| 2254 | PyErr_Restore(status, exc, tb); |
| 2255 | why = WHY_EXCEPTION; |
| 2256 | goto fast_block_end; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2257 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2258 | else if (status != Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2259 | PyErr_SetString(PyExc_SystemError, |
| 2260 | "'finally' pops bad exception"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2261 | Py_DECREF(status); |
| 2262 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2263 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2264 | Py_DECREF(status); |
| 2265 | DISPATCH(); |
| 2266 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2267 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2268 | TARGET(LOAD_BUILD_CLASS) { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2269 | _Py_IDENTIFIER(__build_class__); |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2270 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2271 | PyObject *bc; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2272 | if (PyDict_CheckExact(f->f_builtins)) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2273 | bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__); |
| 2274 | if (bc == NULL) { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2275 | PyErr_SetString(PyExc_NameError, |
| 2276 | "__build_class__ not found"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2277 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2278 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2279 | Py_INCREF(bc); |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2280 | } |
| 2281 | else { |
| 2282 | PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__); |
| 2283 | if (build_class_str == NULL) |
| 2284 | break; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2285 | bc = PyObject_GetItem(f->f_builtins, build_class_str); |
| 2286 | if (bc == NULL) { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2287 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2288 | PyErr_SetString(PyExc_NameError, |
| 2289 | "__build_class__ not found"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2290 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2291 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2292 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2293 | PUSH(bc); |
Benjamin Peterson | 00f86f2 | 2012-10-10 14:10:33 -0400 | [diff] [blame] | 2294 | DISPATCH(); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2295 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2296 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2297 | TARGET(STORE_NAME) { |
| 2298 | PyObject *name = GETITEM(names, oparg); |
| 2299 | PyObject *v = POP(); |
| 2300 | PyObject *ns = f->f_locals; |
| 2301 | int err; |
| 2302 | if (ns == NULL) { |
| 2303 | PyErr_Format(PyExc_SystemError, |
| 2304 | "no locals found when storing %R", name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2305 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2306 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2307 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2308 | if (PyDict_CheckExact(ns)) |
| 2309 | err = PyDict_SetItem(ns, name, v); |
| 2310 | else |
| 2311 | err = PyObject_SetItem(ns, name, v); |
| 2312 | Py_DECREF(v); |
| 2313 | if (err != 0) |
| 2314 | goto error; |
| 2315 | DISPATCH(); |
| 2316 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2317 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2318 | TARGET(DELETE_NAME) { |
| 2319 | PyObject *name = GETITEM(names, oparg); |
| 2320 | PyObject *ns = f->f_locals; |
| 2321 | int err; |
| 2322 | if (ns == NULL) { |
| 2323 | PyErr_Format(PyExc_SystemError, |
| 2324 | "no locals when deleting %R", name); |
| 2325 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2326 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2327 | err = PyObject_DelItem(ns, name); |
| 2328 | if (err != 0) { |
| 2329 | format_exc_check_arg(PyExc_NameError, |
| 2330 | NAME_ERROR_MSG, |
| 2331 | name); |
| 2332 | goto error; |
| 2333 | } |
| 2334 | DISPATCH(); |
| 2335 | } |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 2336 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 2337 | PREDICTED(UNPACK_SEQUENCE); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2338 | TARGET(UNPACK_SEQUENCE) { |
| 2339 | PyObject *seq = POP(), *item, **items; |
| 2340 | if (PyTuple_CheckExact(seq) && |
| 2341 | PyTuple_GET_SIZE(seq) == oparg) { |
| 2342 | items = ((PyTupleObject *)seq)->ob_item; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2343 | while (oparg--) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2344 | item = items[oparg]; |
| 2345 | Py_INCREF(item); |
| 2346 | PUSH(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2347 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2348 | } else if (PyList_CheckExact(seq) && |
| 2349 | PyList_GET_SIZE(seq) == oparg) { |
| 2350 | items = ((PyListObject *)seq)->ob_item; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2351 | while (oparg--) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2352 | item = items[oparg]; |
| 2353 | Py_INCREF(item); |
| 2354 | PUSH(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2355 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2356 | } else if (unpack_iterable(seq, oparg, -1, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2357 | stack_pointer + oparg)) { |
| 2358 | STACKADJ(oparg); |
| 2359 | } else { |
| 2360 | /* unpack_iterable() raised an exception */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2361 | Py_DECREF(seq); |
| 2362 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2363 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2364 | Py_DECREF(seq); |
Benjamin Peterson | 00f86f2 | 2012-10-10 14:10:33 -0400 | [diff] [blame] | 2365 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2366 | } |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 2367 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2368 | TARGET(UNPACK_EX) { |
| 2369 | int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8); |
| 2370 | PyObject *seq = POP(); |
| 2371 | |
| 2372 | if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8, |
| 2373 | stack_pointer + totalargs)) { |
| 2374 | stack_pointer += totalargs; |
| 2375 | } else { |
| 2376 | Py_DECREF(seq); |
| 2377 | goto error; |
| 2378 | } |
| 2379 | Py_DECREF(seq); |
| 2380 | DISPATCH(); |
| 2381 | } |
| 2382 | |
| 2383 | TARGET(STORE_ATTR) { |
| 2384 | PyObject *name = GETITEM(names, oparg); |
| 2385 | PyObject *owner = TOP(); |
| 2386 | PyObject *v = SECOND(); |
| 2387 | int err; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2388 | STACKADJ(-2); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2389 | err = PyObject_SetAttr(owner, name, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2390 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2391 | Py_DECREF(owner); |
| 2392 | if (err != 0) |
| 2393 | goto error; |
| 2394 | DISPATCH(); |
| 2395 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2396 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2397 | TARGET(DELETE_ATTR) { |
| 2398 | PyObject *name = GETITEM(names, oparg); |
| 2399 | PyObject *owner = POP(); |
| 2400 | int err; |
| 2401 | err = PyObject_SetAttr(owner, name, (PyObject *)NULL); |
| 2402 | Py_DECREF(owner); |
| 2403 | if (err != 0) |
| 2404 | goto error; |
| 2405 | DISPATCH(); |
| 2406 | } |
| 2407 | |
| 2408 | TARGET(STORE_GLOBAL) { |
| 2409 | PyObject *name = GETITEM(names, oparg); |
| 2410 | PyObject *v = POP(); |
| 2411 | int err; |
| 2412 | err = PyDict_SetItem(f->f_globals, name, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2413 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2414 | if (err != 0) |
| 2415 | goto error; |
| 2416 | DISPATCH(); |
| 2417 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2418 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2419 | TARGET(DELETE_GLOBAL) { |
| 2420 | PyObject *name = GETITEM(names, oparg); |
| 2421 | int err; |
| 2422 | err = PyDict_DelItem(f->f_globals, name); |
| 2423 | if (err != 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2424 | format_exc_check_arg( |
Ezio Melotti | 04a2955 | 2013-03-03 15:12:44 +0200 | [diff] [blame] | 2425 | PyExc_NameError, NAME_ERROR_MSG, name); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2426 | goto error; |
Benjamin Peterson | 00f86f2 | 2012-10-10 14:10:33 -0400 | [diff] [blame] | 2427 | } |
| 2428 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2429 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2430 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2431 | TARGET(LOAD_NAME) { |
| 2432 | PyObject *name = GETITEM(names, oparg); |
| 2433 | PyObject *locals = f->f_locals; |
| 2434 | PyObject *v; |
| 2435 | if (locals == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2436 | PyErr_Format(PyExc_SystemError, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2437 | "no locals when loading %R", name); |
| 2438 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2439 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2440 | if (PyDict_CheckExact(locals)) { |
| 2441 | v = PyDict_GetItem(locals, name); |
| 2442 | Py_XINCREF(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2443 | } |
| 2444 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2445 | v = PyObject_GetItem(locals, name); |
Victor Stinner | e20310f | 2015-11-05 13:56:58 +0100 | [diff] [blame] | 2446 | if (v == NULL) { |
Benjamin Peterson | 9272279 | 2012-12-15 12:51:05 -0500 | [diff] [blame] | 2447 | if (!PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2448 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2449 | PyErr_Clear(); |
| 2450 | } |
| 2451 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2452 | if (v == NULL) { |
| 2453 | v = PyDict_GetItem(f->f_globals, name); |
| 2454 | Py_XINCREF(v); |
| 2455 | if (v == NULL) { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2456 | if (PyDict_CheckExact(f->f_builtins)) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2457 | v = PyDict_GetItem(f->f_builtins, name); |
| 2458 | if (v == NULL) { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2459 | format_exc_check_arg( |
| 2460 | PyExc_NameError, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2461 | NAME_ERROR_MSG, name); |
| 2462 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2463 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2464 | Py_INCREF(v); |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2465 | } |
| 2466 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2467 | v = PyObject_GetItem(f->f_builtins, name); |
| 2468 | if (v == NULL) { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2469 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2470 | format_exc_check_arg( |
| 2471 | PyExc_NameError, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2472 | NAME_ERROR_MSG, name); |
| 2473 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2474 | } |
Benjamin Peterson | 20f9c3c | 2010-07-20 22:39:34 +0000 | [diff] [blame] | 2475 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2476 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2477 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2478 | PUSH(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2479 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2480 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2481 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2482 | TARGET(LOAD_GLOBAL) { |
| 2483 | PyObject *name = GETITEM(names, oparg); |
| 2484 | PyObject *v; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2485 | if (PyDict_CheckExact(f->f_globals) |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2486 | && PyDict_CheckExact(f->f_builtins)) |
| 2487 | { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2488 | v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals, |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2489 | (PyDictObject *)f->f_builtins, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2490 | name); |
| 2491 | if (v == NULL) { |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2492 | if (!_PyErr_OCCURRED()) { |
| 2493 | /* _PyDict_LoadGlobal() returns NULL without raising |
| 2494 | * an exception if the key doesn't exist */ |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2495 | format_exc_check_arg(PyExc_NameError, |
Ezio Melotti | 04a2955 | 2013-03-03 15:12:44 +0200 | [diff] [blame] | 2496 | NAME_ERROR_MSG, name); |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2497 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2498 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2499 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2500 | Py_INCREF(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2501 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2502 | else { |
| 2503 | /* Slow-path if globals or builtins is not a dict */ |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2504 | |
| 2505 | /* namespace 1: globals */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2506 | v = PyObject_GetItem(f->f_globals, name); |
| 2507 | if (v == NULL) { |
Victor Stinner | 60a1d3c | 2015-11-05 13:55:20 +0100 | [diff] [blame] | 2508 | if (!PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2509 | goto error; |
| 2510 | PyErr_Clear(); |
| 2511 | |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2512 | /* namespace 2: builtins */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2513 | v = PyObject_GetItem(f->f_builtins, name); |
| 2514 | if (v == NULL) { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2515 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2516 | format_exc_check_arg( |
| 2517 | PyExc_NameError, |
Ezio Melotti | 04a2955 | 2013-03-03 15:12:44 +0200 | [diff] [blame] | 2518 | NAME_ERROR_MSG, name); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2519 | goto error; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2520 | } |
| 2521 | } |
| 2522 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2523 | PUSH(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2524 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2525 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2526 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2527 | TARGET(DELETE_FAST) { |
| 2528 | PyObject *v = GETLOCAL(oparg); |
| 2529 | if (v != NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2530 | SETLOCAL(oparg, NULL); |
| 2531 | DISPATCH(); |
| 2532 | } |
| 2533 | format_exc_check_arg( |
| 2534 | PyExc_UnboundLocalError, |
| 2535 | UNBOUNDLOCAL_ERROR_MSG, |
| 2536 | PyTuple_GetItem(co->co_varnames, oparg) |
| 2537 | ); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2538 | goto error; |
| 2539 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2540 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2541 | TARGET(DELETE_DEREF) { |
| 2542 | PyObject *cell = freevars[oparg]; |
| 2543 | if (PyCell_GET(cell) != NULL) { |
| 2544 | PyCell_Set(cell, NULL); |
Benjamin Peterson | 00ebe2c | 2010-09-10 22:02:31 +0000 | [diff] [blame] | 2545 | DISPATCH(); |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 2546 | } |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 2547 | format_exc_unbound(co, oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2548 | goto error; |
| 2549 | } |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 2550 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2551 | TARGET(LOAD_CLOSURE) { |
| 2552 | PyObject *cell = freevars[oparg]; |
| 2553 | Py_INCREF(cell); |
| 2554 | PUSH(cell); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2555 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2556 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2557 | |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2558 | TARGET(LOAD_CLASSDEREF) { |
| 2559 | PyObject *name, *value, *locals = f->f_locals; |
Victor Stinner | d3dfd0e | 2013-05-16 23:48:01 +0200 | [diff] [blame] | 2560 | Py_ssize_t idx; |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2561 | assert(locals); |
| 2562 | assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars)); |
| 2563 | idx = oparg - PyTuple_GET_SIZE(co->co_cellvars); |
| 2564 | assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars)); |
| 2565 | name = PyTuple_GET_ITEM(co->co_freevars, idx); |
| 2566 | if (PyDict_CheckExact(locals)) { |
| 2567 | value = PyDict_GetItem(locals, name); |
| 2568 | Py_XINCREF(value); |
| 2569 | } |
| 2570 | else { |
| 2571 | value = PyObject_GetItem(locals, name); |
Victor Stinner | e20310f | 2015-11-05 13:56:58 +0100 | [diff] [blame] | 2572 | if (value == NULL) { |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2573 | if (!PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2574 | goto error; |
| 2575 | PyErr_Clear(); |
| 2576 | } |
| 2577 | } |
| 2578 | if (!value) { |
| 2579 | PyObject *cell = freevars[oparg]; |
| 2580 | value = PyCell_GET(cell); |
| 2581 | if (value == NULL) { |
| 2582 | format_exc_unbound(co, oparg); |
| 2583 | goto error; |
| 2584 | } |
| 2585 | Py_INCREF(value); |
| 2586 | } |
| 2587 | PUSH(value); |
| 2588 | DISPATCH(); |
| 2589 | } |
| 2590 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2591 | TARGET(LOAD_DEREF) { |
| 2592 | PyObject *cell = freevars[oparg]; |
| 2593 | PyObject *value = PyCell_GET(cell); |
| 2594 | if (value == NULL) { |
| 2595 | format_exc_unbound(co, oparg); |
| 2596 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2597 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2598 | Py_INCREF(value); |
| 2599 | PUSH(value); |
| 2600 | DISPATCH(); |
| 2601 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2602 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2603 | TARGET(STORE_DEREF) { |
| 2604 | PyObject *v = POP(); |
| 2605 | PyObject *cell = freevars[oparg]; |
| 2606 | PyCell_Set(cell, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2607 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2608 | DISPATCH(); |
| 2609 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2610 | |
Serhiy Storchaka | ea525a2 | 2016-09-06 22:07:53 +0300 | [diff] [blame] | 2611 | TARGET(BUILD_STRING) { |
| 2612 | PyObject *str; |
| 2613 | PyObject *empty = PyUnicode_New(0, 0); |
| 2614 | if (empty == NULL) { |
| 2615 | goto error; |
| 2616 | } |
| 2617 | str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg); |
| 2618 | Py_DECREF(empty); |
| 2619 | if (str == NULL) |
| 2620 | goto error; |
| 2621 | while (--oparg >= 0) { |
| 2622 | PyObject *item = POP(); |
| 2623 | Py_DECREF(item); |
| 2624 | } |
| 2625 | PUSH(str); |
| 2626 | DISPATCH(); |
| 2627 | } |
| 2628 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2629 | TARGET(BUILD_TUPLE) { |
| 2630 | PyObject *tup = PyTuple_New(oparg); |
| 2631 | if (tup == NULL) |
| 2632 | goto error; |
| 2633 | while (--oparg >= 0) { |
| 2634 | PyObject *item = POP(); |
| 2635 | PyTuple_SET_ITEM(tup, oparg, item); |
| 2636 | } |
| 2637 | PUSH(tup); |
| 2638 | DISPATCH(); |
| 2639 | } |
| 2640 | |
| 2641 | TARGET(BUILD_LIST) { |
| 2642 | PyObject *list = PyList_New(oparg); |
| 2643 | if (list == NULL) |
| 2644 | goto error; |
| 2645 | while (--oparg >= 0) { |
| 2646 | PyObject *item = POP(); |
| 2647 | PyList_SET_ITEM(list, oparg, item); |
| 2648 | } |
| 2649 | PUSH(list); |
| 2650 | DISPATCH(); |
| 2651 | } |
| 2652 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 2653 | TARGET(BUILD_TUPLE_UNPACK) |
| 2654 | TARGET(BUILD_LIST_UNPACK) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2655 | int convert_to_tuple = opcode == BUILD_TUPLE_UNPACK; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2656 | Py_ssize_t i; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 2657 | PyObject *sum; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2658 | PyObject *return_value; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 2659 | |
| 2660 | if (convert_to_tuple && oparg == 1 && PyTuple_CheckExact(TOP())) { |
| 2661 | DISPATCH(); |
| 2662 | } |
| 2663 | |
| 2664 | sum = PyList_New(0); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2665 | if (sum == NULL) |
| 2666 | goto error; |
| 2667 | |
| 2668 | for (i = oparg; i > 0; i--) { |
| 2669 | PyObject *none_val; |
| 2670 | |
| 2671 | none_val = _PyList_Extend((PyListObject *)sum, PEEK(i)); |
| 2672 | if (none_val == NULL) { |
| 2673 | Py_DECREF(sum); |
| 2674 | goto error; |
| 2675 | } |
| 2676 | Py_DECREF(none_val); |
| 2677 | } |
| 2678 | |
| 2679 | if (convert_to_tuple) { |
| 2680 | return_value = PyList_AsTuple(sum); |
| 2681 | Py_DECREF(sum); |
| 2682 | if (return_value == NULL) |
| 2683 | goto error; |
| 2684 | } |
| 2685 | else { |
| 2686 | return_value = sum; |
| 2687 | } |
| 2688 | |
| 2689 | while (oparg--) |
| 2690 | Py_DECREF(POP()); |
| 2691 | PUSH(return_value); |
| 2692 | DISPATCH(); |
| 2693 | } |
| 2694 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2695 | TARGET(BUILD_SET) { |
| 2696 | PyObject *set = PySet_New(NULL); |
| 2697 | int err = 0; |
Raymond Hettinger | 4c483ad | 2016-09-08 14:45:40 -0700 | [diff] [blame] | 2698 | int i; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2699 | if (set == NULL) |
| 2700 | goto error; |
Raymond Hettinger | 4c483ad | 2016-09-08 14:45:40 -0700 | [diff] [blame] | 2701 | for (i = oparg; i > 0; i--) { |
| 2702 | PyObject *item = PEEK(i); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2703 | if (err == 0) |
| 2704 | err = PySet_Add(set, item); |
| 2705 | Py_DECREF(item); |
| 2706 | } |
Raymond Hettinger | 4c483ad | 2016-09-08 14:45:40 -0700 | [diff] [blame] | 2707 | STACKADJ(-oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2708 | if (err != 0) { |
| 2709 | Py_DECREF(set); |
| 2710 | goto error; |
| 2711 | } |
| 2712 | PUSH(set); |
| 2713 | DISPATCH(); |
| 2714 | } |
| 2715 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2716 | TARGET(BUILD_SET_UNPACK) { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2717 | Py_ssize_t i; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2718 | PyObject *sum = PySet_New(NULL); |
| 2719 | if (sum == NULL) |
| 2720 | goto error; |
| 2721 | |
| 2722 | for (i = oparg; i > 0; i--) { |
| 2723 | if (_PySet_Update(sum, PEEK(i)) < 0) { |
| 2724 | Py_DECREF(sum); |
| 2725 | goto error; |
| 2726 | } |
| 2727 | } |
| 2728 | |
| 2729 | while (oparg--) |
| 2730 | Py_DECREF(POP()); |
| 2731 | PUSH(sum); |
| 2732 | DISPATCH(); |
| 2733 | } |
| 2734 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2735 | TARGET(BUILD_MAP) { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2736 | Py_ssize_t i; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2737 | PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg); |
| 2738 | if (map == NULL) |
| 2739 | goto error; |
Benjamin Peterson | d5d77aa | 2015-07-05 10:37:25 -0500 | [diff] [blame] | 2740 | for (i = oparg; i > 0; i--) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2741 | int err; |
Benjamin Peterson | d5d77aa | 2015-07-05 10:37:25 -0500 | [diff] [blame] | 2742 | PyObject *key = PEEK(2*i); |
| 2743 | PyObject *value = PEEK(2*i - 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2744 | err = PyDict_SetItem(map, key, value); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2745 | if (err != 0) { |
| 2746 | Py_DECREF(map); |
| 2747 | goto error; |
| 2748 | } |
| 2749 | } |
Benjamin Peterson | d5d77aa | 2015-07-05 10:37:25 -0500 | [diff] [blame] | 2750 | |
| 2751 | while (oparg--) { |
| 2752 | Py_DECREF(POP()); |
| 2753 | Py_DECREF(POP()); |
| 2754 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2755 | PUSH(map); |
| 2756 | DISPATCH(); |
| 2757 | } |
| 2758 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2759 | TARGET(SETUP_ANNOTATIONS) { |
| 2760 | _Py_IDENTIFIER(__annotations__); |
| 2761 | int err; |
| 2762 | PyObject *ann_dict; |
| 2763 | if (f->f_locals == NULL) { |
| 2764 | PyErr_Format(PyExc_SystemError, |
| 2765 | "no locals found when setting up annotations"); |
| 2766 | goto error; |
| 2767 | } |
| 2768 | /* check if __annotations__ in locals()... */ |
| 2769 | if (PyDict_CheckExact(f->f_locals)) { |
| 2770 | ann_dict = _PyDict_GetItemId(f->f_locals, |
| 2771 | &PyId___annotations__); |
| 2772 | if (ann_dict == NULL) { |
| 2773 | /* ...if not, create a new one */ |
| 2774 | ann_dict = PyDict_New(); |
| 2775 | if (ann_dict == NULL) { |
| 2776 | goto error; |
| 2777 | } |
| 2778 | err = _PyDict_SetItemId(f->f_locals, |
| 2779 | &PyId___annotations__, ann_dict); |
| 2780 | Py_DECREF(ann_dict); |
| 2781 | if (err != 0) { |
| 2782 | goto error; |
| 2783 | } |
| 2784 | } |
| 2785 | } |
| 2786 | else { |
| 2787 | /* do the same if locals() is not a dict */ |
| 2788 | PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__); |
| 2789 | if (ann_str == NULL) { |
| 2790 | break; |
| 2791 | } |
| 2792 | ann_dict = PyObject_GetItem(f->f_locals, ann_str); |
| 2793 | if (ann_dict == NULL) { |
| 2794 | if (!PyErr_ExceptionMatches(PyExc_KeyError)) { |
| 2795 | goto error; |
| 2796 | } |
| 2797 | PyErr_Clear(); |
| 2798 | ann_dict = PyDict_New(); |
| 2799 | if (ann_dict == NULL) { |
| 2800 | goto error; |
| 2801 | } |
| 2802 | err = PyObject_SetItem(f->f_locals, ann_str, ann_dict); |
| 2803 | Py_DECREF(ann_dict); |
| 2804 | if (err != 0) { |
| 2805 | goto error; |
| 2806 | } |
| 2807 | } |
| 2808 | else { |
| 2809 | Py_DECREF(ann_dict); |
| 2810 | } |
| 2811 | } |
| 2812 | DISPATCH(); |
| 2813 | } |
| 2814 | |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 2815 | TARGET(BUILD_CONST_KEY_MAP) { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2816 | Py_ssize_t i; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 2817 | PyObject *map; |
| 2818 | PyObject *keys = TOP(); |
| 2819 | if (!PyTuple_CheckExact(keys) || |
| 2820 | PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) { |
| 2821 | PyErr_SetString(PyExc_SystemError, |
| 2822 | "bad BUILD_CONST_KEY_MAP keys argument"); |
| 2823 | goto error; |
| 2824 | } |
| 2825 | map = _PyDict_NewPresized((Py_ssize_t)oparg); |
| 2826 | if (map == NULL) { |
| 2827 | goto error; |
| 2828 | } |
| 2829 | for (i = oparg; i > 0; i--) { |
| 2830 | int err; |
| 2831 | PyObject *key = PyTuple_GET_ITEM(keys, oparg - i); |
| 2832 | PyObject *value = PEEK(i + 1); |
| 2833 | err = PyDict_SetItem(map, key, value); |
| 2834 | if (err != 0) { |
| 2835 | Py_DECREF(map); |
| 2836 | goto error; |
| 2837 | } |
| 2838 | } |
| 2839 | |
| 2840 | Py_DECREF(POP()); |
| 2841 | while (oparg--) { |
| 2842 | Py_DECREF(POP()); |
| 2843 | } |
| 2844 | PUSH(map); |
| 2845 | DISPATCH(); |
| 2846 | } |
| 2847 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 2848 | TARGET(BUILD_MAP_UNPACK_WITH_CALL) |
| 2849 | TARGET(BUILD_MAP_UNPACK) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2850 | int with_call = opcode == BUILD_MAP_UNPACK_WITH_CALL; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2851 | Py_ssize_t i; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 2852 | PyObject *sum; |
| 2853 | |
| 2854 | if (with_call && oparg == 1 && PyDict_CheckExact(TOP())) { |
| 2855 | DISPATCH(); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2856 | } |
| 2857 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 2858 | sum = PyDict_New(); |
| 2859 | if (sum == NULL) |
| 2860 | goto error; |
| 2861 | |
| 2862 | for (i = oparg; i > 0; i--) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2863 | PyObject *arg = PEEK(i); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 2864 | if (with_call && PyDict_Size(sum)) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2865 | PyObject *intersection = _PyDictView_Intersect(sum, arg); |
| 2866 | |
| 2867 | if (intersection == NULL) { |
| 2868 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 2869 | PyObject *func = PEEK(2 + oparg); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2870 | PyErr_Format(PyExc_TypeError, |
| 2871 | "%.200s%.200s argument after ** " |
| 2872 | "must be a mapping, not %.200s", |
| 2873 | PyEval_GetFuncName(func), |
| 2874 | PyEval_GetFuncDesc(func), |
| 2875 | arg->ob_type->tp_name); |
| 2876 | } |
| 2877 | Py_DECREF(sum); |
| 2878 | goto error; |
| 2879 | } |
| 2880 | |
| 2881 | if (PySet_GET_SIZE(intersection)) { |
| 2882 | Py_ssize_t idx = 0; |
| 2883 | PyObject *key; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 2884 | PyObject *func = PEEK(2 + oparg); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2885 | Py_hash_t hash; |
| 2886 | _PySet_NextEntry(intersection, &idx, &key, &hash); |
| 2887 | if (!PyUnicode_Check(key)) { |
| 2888 | PyErr_Format(PyExc_TypeError, |
| 2889 | "%.200s%.200s keywords must be strings", |
| 2890 | PyEval_GetFuncName(func), |
| 2891 | PyEval_GetFuncDesc(func)); |
| 2892 | } else { |
| 2893 | PyErr_Format(PyExc_TypeError, |
| 2894 | "%.200s%.200s got multiple " |
| 2895 | "values for keyword argument '%U'", |
| 2896 | PyEval_GetFuncName(func), |
| 2897 | PyEval_GetFuncDesc(func), |
| 2898 | key); |
| 2899 | } |
| 2900 | Py_DECREF(intersection); |
| 2901 | Py_DECREF(sum); |
| 2902 | goto error; |
| 2903 | } |
| 2904 | Py_DECREF(intersection); |
| 2905 | } |
| 2906 | |
| 2907 | if (PyDict_Update(sum, arg) < 0) { |
| 2908 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 2909 | PyErr_Format(PyExc_TypeError, |
| 2910 | "'%.200s' object is not a mapping", |
| 2911 | arg->ob_type->tp_name); |
| 2912 | } |
| 2913 | Py_DECREF(sum); |
| 2914 | goto error; |
| 2915 | } |
| 2916 | } |
| 2917 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 2918 | while (oparg--) |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2919 | Py_DECREF(POP()); |
| 2920 | PUSH(sum); |
| 2921 | DISPATCH(); |
| 2922 | } |
| 2923 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2924 | TARGET(MAP_ADD) { |
| 2925 | PyObject *key = TOP(); |
| 2926 | PyObject *value = SECOND(); |
| 2927 | PyObject *map; |
| 2928 | int err; |
| 2929 | STACKADJ(-2); |
| 2930 | map = stack_pointer[-oparg]; /* dict */ |
| 2931 | assert(PyDict_CheckExact(map)); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 2932 | err = PyDict_SetItem(map, key, value); /* map[key] = value */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2933 | Py_DECREF(value); |
| 2934 | Py_DECREF(key); |
| 2935 | if (err != 0) |
| 2936 | goto error; |
| 2937 | PREDICT(JUMP_ABSOLUTE); |
| 2938 | DISPATCH(); |
| 2939 | } |
| 2940 | |
| 2941 | TARGET(LOAD_ATTR) { |
| 2942 | PyObject *name = GETITEM(names, oparg); |
| 2943 | PyObject *owner = TOP(); |
| 2944 | PyObject *res = PyObject_GetAttr(owner, name); |
| 2945 | Py_DECREF(owner); |
| 2946 | SET_TOP(res); |
| 2947 | if (res == NULL) |
| 2948 | goto error; |
| 2949 | DISPATCH(); |
| 2950 | } |
| 2951 | |
| 2952 | TARGET(COMPARE_OP) { |
| 2953 | PyObject *right = POP(); |
| 2954 | PyObject *left = TOP(); |
| 2955 | PyObject *res = cmp_outcome(oparg, left, right); |
| 2956 | Py_DECREF(left); |
| 2957 | Py_DECREF(right); |
| 2958 | SET_TOP(res); |
| 2959 | if (res == NULL) |
| 2960 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2961 | PREDICT(POP_JUMP_IF_FALSE); |
| 2962 | PREDICT(POP_JUMP_IF_TRUE); |
| 2963 | DISPATCH(); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2964 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2965 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2966 | TARGET(IMPORT_NAME) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2967 | PyObject *name = GETITEM(names, oparg); |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 2968 | PyObject *fromlist = POP(); |
| 2969 | PyObject *level = TOP(); |
| 2970 | PyObject *res; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2971 | READ_TIMESTAMP(intr0); |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 2972 | res = import_name(f, name, fromlist, level); |
| 2973 | Py_DECREF(level); |
| 2974 | Py_DECREF(fromlist); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2975 | READ_TIMESTAMP(intr1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2976 | SET_TOP(res); |
| 2977 | if (res == NULL) |
| 2978 | goto error; |
| 2979 | DISPATCH(); |
| 2980 | } |
| 2981 | |
| 2982 | TARGET(IMPORT_STAR) { |
| 2983 | PyObject *from = POP(), *locals; |
| 2984 | int err; |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 2985 | if (PyFrame_FastToLocalsWithError(f) < 0) |
| 2986 | goto error; |
| 2987 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2988 | locals = f->f_locals; |
| 2989 | if (locals == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2990 | PyErr_SetString(PyExc_SystemError, |
| 2991 | "no locals found during 'import *'"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2992 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2993 | } |
| 2994 | READ_TIMESTAMP(intr0); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2995 | err = import_all_from(locals, from); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2996 | READ_TIMESTAMP(intr1); |
| 2997 | PyFrame_LocalsToFast(f, 0); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2998 | Py_DECREF(from); |
| 2999 | if (err != 0) |
| 3000 | goto error; |
| 3001 | DISPATCH(); |
| 3002 | } |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3003 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3004 | TARGET(IMPORT_FROM) { |
| 3005 | PyObject *name = GETITEM(names, oparg); |
| 3006 | PyObject *from = TOP(); |
| 3007 | PyObject *res; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3008 | READ_TIMESTAMP(intr0); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3009 | res = import_from(from, name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3010 | READ_TIMESTAMP(intr1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3011 | PUSH(res); |
| 3012 | if (res == NULL) |
| 3013 | goto error; |
| 3014 | DISPATCH(); |
| 3015 | } |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3016 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3017 | TARGET(JUMP_FORWARD) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3018 | JUMPBY(oparg); |
| 3019 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3020 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3021 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 3022 | PREDICTED(POP_JUMP_IF_FALSE); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3023 | TARGET(POP_JUMP_IF_FALSE) { |
| 3024 | PyObject *cond = POP(); |
| 3025 | int err; |
| 3026 | if (cond == Py_True) { |
| 3027 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3028 | FAST_DISPATCH(); |
| 3029 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3030 | if (cond == Py_False) { |
| 3031 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3032 | JUMPTO(oparg); |
| 3033 | FAST_DISPATCH(); |
| 3034 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3035 | err = PyObject_IsTrue(cond); |
| 3036 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3037 | if (err > 0) |
| 3038 | err = 0; |
| 3039 | else if (err == 0) |
| 3040 | JUMPTO(oparg); |
| 3041 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3042 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3043 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3044 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3045 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 3046 | PREDICTED(POP_JUMP_IF_TRUE); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3047 | TARGET(POP_JUMP_IF_TRUE) { |
| 3048 | PyObject *cond = POP(); |
| 3049 | int err; |
| 3050 | if (cond == Py_False) { |
| 3051 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3052 | FAST_DISPATCH(); |
| 3053 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3054 | if (cond == Py_True) { |
| 3055 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3056 | JUMPTO(oparg); |
| 3057 | FAST_DISPATCH(); |
| 3058 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3059 | err = PyObject_IsTrue(cond); |
| 3060 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3061 | if (err > 0) { |
| 3062 | err = 0; |
| 3063 | JUMPTO(oparg); |
| 3064 | } |
| 3065 | else if (err == 0) |
| 3066 | ; |
| 3067 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3068 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3069 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3070 | } |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 3071 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3072 | TARGET(JUMP_IF_FALSE_OR_POP) { |
| 3073 | PyObject *cond = TOP(); |
| 3074 | int err; |
| 3075 | if (cond == Py_True) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3076 | STACKADJ(-1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3077 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3078 | FAST_DISPATCH(); |
| 3079 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3080 | if (cond == Py_False) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3081 | JUMPTO(oparg); |
| 3082 | FAST_DISPATCH(); |
| 3083 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3084 | err = PyObject_IsTrue(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3085 | if (err > 0) { |
| 3086 | STACKADJ(-1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3087 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3088 | err = 0; |
| 3089 | } |
| 3090 | else if (err == 0) |
| 3091 | JUMPTO(oparg); |
| 3092 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3093 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3094 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3095 | } |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 3096 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3097 | TARGET(JUMP_IF_TRUE_OR_POP) { |
| 3098 | PyObject *cond = TOP(); |
| 3099 | int err; |
| 3100 | if (cond == Py_False) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3101 | STACKADJ(-1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3102 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3103 | FAST_DISPATCH(); |
| 3104 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3105 | if (cond == Py_True) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3106 | JUMPTO(oparg); |
| 3107 | FAST_DISPATCH(); |
| 3108 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3109 | err = PyObject_IsTrue(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3110 | if (err > 0) { |
| 3111 | err = 0; |
| 3112 | JUMPTO(oparg); |
| 3113 | } |
| 3114 | else if (err == 0) { |
| 3115 | STACKADJ(-1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3116 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3117 | } |
| 3118 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3119 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3120 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3121 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3122 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 3123 | PREDICTED(JUMP_ABSOLUTE); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3124 | TARGET(JUMP_ABSOLUTE) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3125 | JUMPTO(oparg); |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 3126 | #if FAST_LOOPS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3127 | /* Enabling this path speeds-up all while and for-loops by bypassing |
| 3128 | the per-loop checks for signals. By default, this should be turned-off |
| 3129 | because it prevents detection of a control-break in tight loops like |
| 3130 | "while 1: pass". Compile with this option turned-on when you need |
| 3131 | the speed-up and do not need break checking inside tight loops (ones |
| 3132 | that contain only instructions ending with FAST_DISPATCH). |
| 3133 | */ |
| 3134 | FAST_DISPATCH(); |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 3135 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3136 | DISPATCH(); |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 3137 | #endif |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3138 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3139 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3140 | TARGET(GET_ITER) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3141 | /* before: [obj]; after [getiter(obj)] */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3142 | PyObject *iterable = TOP(); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 3143 | PyObject *iter = PyObject_GetIter(iterable); |
| 3144 | Py_DECREF(iterable); |
| 3145 | SET_TOP(iter); |
| 3146 | if (iter == NULL) |
| 3147 | goto error; |
| 3148 | PREDICT(FOR_ITER); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 3149 | PREDICT(CALL_FUNCTION); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 3150 | DISPATCH(); |
| 3151 | } |
| 3152 | |
| 3153 | TARGET(GET_YIELD_FROM_ITER) { |
| 3154 | /* before: [obj]; after [getiter(obj)] */ |
| 3155 | PyObject *iterable = TOP(); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3156 | PyObject *iter; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 3157 | if (PyCoro_CheckExact(iterable)) { |
| 3158 | /* `iterable` is a coroutine */ |
| 3159 | if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) { |
| 3160 | /* and it is used in a 'yield from' expression of a |
| 3161 | regular generator. */ |
| 3162 | Py_DECREF(iterable); |
| 3163 | SET_TOP(NULL); |
| 3164 | PyErr_SetString(PyExc_TypeError, |
| 3165 | "cannot 'yield from' a coroutine object " |
| 3166 | "in a non-coroutine generator"); |
| 3167 | goto error; |
| 3168 | } |
| 3169 | } |
| 3170 | else if (!PyGen_CheckExact(iterable)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3171 | /* `iterable` is not a generator. */ |
| 3172 | iter = PyObject_GetIter(iterable); |
| 3173 | Py_DECREF(iterable); |
| 3174 | SET_TOP(iter); |
| 3175 | if (iter == NULL) |
| 3176 | goto error; |
| 3177 | } |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 3178 | PREDICT(LOAD_CONST); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3179 | DISPATCH(); |
| 3180 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3181 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 3182 | PREDICTED(FOR_ITER); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3183 | TARGET(FOR_ITER) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3184 | /* before: [iter]; after: [iter, iter()] *or* [] */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3185 | PyObject *iter = TOP(); |
| 3186 | PyObject *next = (*iter->ob_type->tp_iternext)(iter); |
| 3187 | if (next != NULL) { |
| 3188 | PUSH(next); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3189 | PREDICT(STORE_FAST); |
| 3190 | PREDICT(UNPACK_SEQUENCE); |
| 3191 | DISPATCH(); |
| 3192 | } |
| 3193 | if (PyErr_Occurred()) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3194 | if (!PyErr_ExceptionMatches(PyExc_StopIteration)) |
| 3195 | goto error; |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 3196 | else if (tstate->c_tracefunc != NULL) |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3197 | call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3198 | PyErr_Clear(); |
| 3199 | } |
| 3200 | /* iterator ended normally */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3201 | STACKADJ(-1); |
| 3202 | Py_DECREF(iter); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3203 | JUMPBY(oparg); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 3204 | PREDICT(POP_BLOCK); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3205 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3206 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3207 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3208 | TARGET(BREAK_LOOP) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3209 | why = WHY_BREAK; |
| 3210 | goto fast_block_end; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3211 | } |
Raymond Hettinger | 2d783e9 | 2004-03-12 09:12:22 +0000 | [diff] [blame] | 3212 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3213 | TARGET(CONTINUE_LOOP) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3214 | retval = PyLong_FromLong(oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3215 | if (retval == NULL) |
| 3216 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3217 | why = WHY_CONTINUE; |
| 3218 | goto fast_block_end; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3219 | } |
Raymond Hettinger | 2d783e9 | 2004-03-12 09:12:22 +0000 | [diff] [blame] | 3220 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 3221 | TARGET(SETUP_LOOP) |
| 3222 | TARGET(SETUP_EXCEPT) |
| 3223 | TARGET(SETUP_FINALLY) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3224 | /* NOTE: If you add any new block-setup opcodes that |
| 3225 | are not try/except/finally handlers, you may need |
| 3226 | to update the PyGen_NeedsFinalizing() function. |
| 3227 | */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3228 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3229 | PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg, |
| 3230 | STACK_LEVEL()); |
| 3231 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3232 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3233 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3234 | TARGET(BEFORE_ASYNC_WITH) { |
| 3235 | _Py_IDENTIFIER(__aexit__); |
| 3236 | _Py_IDENTIFIER(__aenter__); |
| 3237 | |
| 3238 | PyObject *mgr = TOP(); |
| 3239 | PyObject *exit = special_lookup(mgr, &PyId___aexit__), |
| 3240 | *enter; |
| 3241 | PyObject *res; |
| 3242 | if (exit == NULL) |
| 3243 | goto error; |
| 3244 | SET_TOP(exit); |
| 3245 | enter = special_lookup(mgr, &PyId___aenter__); |
| 3246 | Py_DECREF(mgr); |
| 3247 | if (enter == NULL) |
| 3248 | goto error; |
| 3249 | res = PyObject_CallFunctionObjArgs(enter, NULL); |
| 3250 | Py_DECREF(enter); |
| 3251 | if (res == NULL) |
| 3252 | goto error; |
| 3253 | PUSH(res); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 3254 | PREDICT(GET_AWAITABLE); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3255 | DISPATCH(); |
| 3256 | } |
| 3257 | |
| 3258 | TARGET(SETUP_ASYNC_WITH) { |
| 3259 | PyObject *res = POP(); |
| 3260 | /* Setup the finally block before pushing the result |
| 3261 | of __aenter__ on the stack. */ |
| 3262 | PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg, |
| 3263 | STACK_LEVEL()); |
| 3264 | PUSH(res); |
| 3265 | DISPATCH(); |
| 3266 | } |
| 3267 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3268 | TARGET(SETUP_WITH) { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 3269 | _Py_IDENTIFIER(__exit__); |
| 3270 | _Py_IDENTIFIER(__enter__); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3271 | PyObject *mgr = TOP(); |
| 3272 | PyObject *exit = special_lookup(mgr, &PyId___exit__), *enter; |
| 3273 | PyObject *res; |
| 3274 | if (exit == NULL) |
| 3275 | goto error; |
| 3276 | SET_TOP(exit); |
| 3277 | enter = special_lookup(mgr, &PyId___enter__); |
| 3278 | Py_DECREF(mgr); |
| 3279 | if (enter == NULL) |
| 3280 | goto error; |
| 3281 | res = PyObject_CallFunctionObjArgs(enter, NULL); |
| 3282 | Py_DECREF(enter); |
| 3283 | if (res == NULL) |
| 3284 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3285 | /* Setup the finally block before pushing the result |
| 3286 | of __enter__ on the stack. */ |
| 3287 | PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg, |
| 3288 | STACK_LEVEL()); |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 3289 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3290 | PUSH(res); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3291 | DISPATCH(); |
| 3292 | } |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 3293 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3294 | TARGET(WITH_CLEANUP_START) { |
Benjamin Peterson | 8f16948 | 2013-10-29 22:25:06 -0400 | [diff] [blame] | 3295 | /* At the top of the stack are 1-6 values indicating |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3296 | how/why we entered the finally clause: |
| 3297 | - TOP = None |
| 3298 | - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval |
| 3299 | - TOP = WHY_*; no retval below it |
| 3300 | - (TOP, SECOND, THIRD) = exc_info() |
| 3301 | (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER |
| 3302 | Below them is EXIT, the context.__exit__ bound method. |
| 3303 | In the last case, we must call |
| 3304 | EXIT(TOP, SECOND, THIRD) |
| 3305 | otherwise we must call |
| 3306 | EXIT(None, None, None) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3307 | |
Benjamin Peterson | 8f16948 | 2013-10-29 22:25:06 -0400 | [diff] [blame] | 3308 | In the first three cases, we remove EXIT from the |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3309 | stack, leaving the rest in the same order. In the |
Benjamin Peterson | 8f16948 | 2013-10-29 22:25:06 -0400 | [diff] [blame] | 3310 | fourth case, we shift the bottom 3 values of the |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3311 | stack down, and replace the empty spot with NULL. |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 3312 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3313 | In addition, if the stack represents an exception, |
| 3314 | *and* the function call returns a 'true' value, we |
| 3315 | push WHY_SILENCED onto the stack. END_FINALLY will |
| 3316 | then not re-raise the exception. (But non-local |
| 3317 | gotos should still be resumed.) |
| 3318 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3319 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3320 | PyObject *exit_func; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3321 | PyObject *exc = TOP(), *val = Py_None, *tb = Py_None, *res; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3322 | if (exc == Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3323 | (void)POP(); |
| 3324 | exit_func = TOP(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3325 | SET_TOP(exc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3326 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3327 | else if (PyLong_Check(exc)) { |
| 3328 | STACKADJ(-1); |
| 3329 | switch (PyLong_AsLong(exc)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3330 | case WHY_RETURN: |
| 3331 | case WHY_CONTINUE: |
| 3332 | /* Retval in TOP. */ |
| 3333 | exit_func = SECOND(); |
| 3334 | SET_SECOND(TOP()); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3335 | SET_TOP(exc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3336 | break; |
| 3337 | default: |
| 3338 | exit_func = TOP(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3339 | SET_TOP(exc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3340 | break; |
| 3341 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3342 | exc = Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3343 | } |
| 3344 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3345 | PyObject *tp2, *exc2, *tb2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3346 | PyTryBlock *block; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3347 | val = SECOND(); |
| 3348 | tb = THIRD(); |
| 3349 | tp2 = FOURTH(); |
| 3350 | exc2 = PEEK(5); |
| 3351 | tb2 = PEEK(6); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3352 | exit_func = PEEK(7); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3353 | SET_VALUE(7, tb2); |
| 3354 | SET_VALUE(6, exc2); |
| 3355 | SET_VALUE(5, tp2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3356 | /* UNWIND_EXCEPT_HANDLER will pop this off. */ |
| 3357 | SET_FOURTH(NULL); |
| 3358 | /* We just shifted the stack down, so we have |
| 3359 | to tell the except handler block that the |
| 3360 | values are lower than it expects. */ |
| 3361 | block = &f->f_blockstack[f->f_iblock - 1]; |
| 3362 | assert(block->b_type == EXCEPT_HANDLER); |
| 3363 | block->b_level--; |
| 3364 | } |
| 3365 | /* XXX Not the fastest way to call it... */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3366 | res = PyObject_CallFunctionObjArgs(exit_func, exc, val, tb, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3367 | Py_DECREF(exit_func); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3368 | if (res == NULL) |
| 3369 | goto error; |
Amaury Forgeot d'Arc | 10b24e8 | 2008-12-10 23:49:33 +0000 | [diff] [blame] | 3370 | |
Nick Coghlan | baaadbf | 2015-05-13 15:54:02 +1000 | [diff] [blame] | 3371 | Py_INCREF(exc); /* Duplicating the exception on the stack */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3372 | PUSH(exc); |
| 3373 | PUSH(res); |
| 3374 | PREDICT(WITH_CLEANUP_FINISH); |
| 3375 | DISPATCH(); |
| 3376 | } |
| 3377 | |
| 3378 | PREDICTED(WITH_CLEANUP_FINISH); |
| 3379 | TARGET(WITH_CLEANUP_FINISH) { |
| 3380 | PyObject *res = POP(); |
| 3381 | PyObject *exc = POP(); |
| 3382 | int err; |
| 3383 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3384 | if (exc != Py_None) |
| 3385 | err = PyObject_IsTrue(res); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3386 | else |
| 3387 | err = 0; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3388 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3389 | Py_DECREF(res); |
Nick Coghlan | baaadbf | 2015-05-13 15:54:02 +1000 | [diff] [blame] | 3390 | Py_DECREF(exc); |
Amaury Forgeot d'Arc | 10b24e8 | 2008-12-10 23:49:33 +0000 | [diff] [blame] | 3391 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3392 | if (err < 0) |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3393 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3394 | else if (err > 0) { |
| 3395 | err = 0; |
| 3396 | /* There was an exception and a True return */ |
| 3397 | PUSH(PyLong_FromLong((long) WHY_SILENCED)); |
| 3398 | } |
| 3399 | PREDICT(END_FINALLY); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3400 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3401 | } |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3402 | |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 3403 | PREDICTED(CALL_FUNCTION); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3404 | TARGET(CALL_FUNCTION) { |
| 3405 | PyObject **sp, *res; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3406 | PCALL(PCALL_ALL); |
| 3407 | sp = stack_pointer; |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 3408 | #ifdef WITH_TSC |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3409 | res = call_function(&sp, oparg, NULL, &intr0, &intr1); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 3410 | #else |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3411 | res = call_function(&sp, oparg, NULL); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 3412 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3413 | stack_pointer = sp; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3414 | PUSH(res); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3415 | if (res == NULL) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3416 | goto error; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3417 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3418 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3419 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3420 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3421 | TARGET(CALL_FUNCTION_KW) { |
| 3422 | PyObject **sp, *res, *names; |
| 3423 | |
| 3424 | names = POP(); |
| 3425 | assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3426 | PCALL(PCALL_ALL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3427 | sp = stack_pointer; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3428 | #ifdef WITH_TSC |
| 3429 | res = call_function(&sp, oparg, names, &intr0, &intr1); |
| 3430 | #else |
| 3431 | res = call_function(&sp, oparg, names); |
| 3432 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3433 | stack_pointer = sp; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3434 | PUSH(res); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3435 | Py_DECREF(names); |
| 3436 | |
| 3437 | if (res == NULL) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3438 | goto error; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3439 | } |
| 3440 | DISPATCH(); |
| 3441 | } |
| 3442 | |
| 3443 | TARGET(CALL_FUNCTION_EX) { |
| 3444 | PyObject *func, *callargs, *kwargs = NULL, *result; |
| 3445 | PCALL(PCALL_ALL); |
| 3446 | if (oparg & 0x01) { |
| 3447 | kwargs = POP(); |
| 3448 | assert(PyDict_CheckExact(kwargs)); |
| 3449 | } |
| 3450 | callargs = POP(); |
| 3451 | assert(PyTuple_CheckExact(callargs)); |
| 3452 | func = TOP(); |
| 3453 | |
| 3454 | READ_TIMESTAMP(intr0); |
| 3455 | result = do_call_core(func, callargs, kwargs); |
| 3456 | READ_TIMESTAMP(intr1); |
| 3457 | Py_DECREF(func); |
| 3458 | Py_DECREF(callargs); |
| 3459 | Py_XDECREF(kwargs); |
| 3460 | |
| 3461 | SET_TOP(result); |
| 3462 | if (result == NULL) { |
| 3463 | goto error; |
| 3464 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3465 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3466 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3467 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 3468 | TARGET(MAKE_FUNCTION) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3469 | PyObject *qualname = POP(); |
| 3470 | PyObject *codeobj = POP(); |
| 3471 | PyFunctionObject *func = (PyFunctionObject *) |
| 3472 | PyFunction_NewWithQualName(codeobj, f->f_globals, qualname); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 3473 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3474 | Py_DECREF(codeobj); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3475 | Py_DECREF(qualname); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3476 | if (func == NULL) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3477 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3478 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 3479 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3480 | if (oparg & 0x08) { |
| 3481 | assert(PyTuple_CheckExact(TOP())); |
| 3482 | func ->func_closure = POP(); |
| 3483 | } |
| 3484 | if (oparg & 0x04) { |
| 3485 | assert(PyDict_CheckExact(TOP())); |
| 3486 | func->func_annotations = POP(); |
| 3487 | } |
| 3488 | if (oparg & 0x02) { |
| 3489 | assert(PyDict_CheckExact(TOP())); |
| 3490 | func->func_kwdefaults = POP(); |
| 3491 | } |
| 3492 | if (oparg & 0x01) { |
| 3493 | assert(PyTuple_CheckExact(TOP())); |
| 3494 | func->func_defaults = POP(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3495 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 3496 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3497 | PUSH((PyObject *)func); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3498 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3499 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 3500 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3501 | TARGET(BUILD_SLICE) { |
| 3502 | PyObject *start, *stop, *step, *slice; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3503 | if (oparg == 3) |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3504 | step = POP(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3505 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3506 | step = NULL; |
| 3507 | stop = POP(); |
| 3508 | start = TOP(); |
| 3509 | slice = PySlice_New(start, stop, step); |
| 3510 | Py_DECREF(start); |
| 3511 | Py_DECREF(stop); |
| 3512 | Py_XDECREF(step); |
| 3513 | SET_TOP(slice); |
| 3514 | if (slice == NULL) |
| 3515 | goto error; |
| 3516 | DISPATCH(); |
| 3517 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 3518 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3519 | TARGET(FORMAT_VALUE) { |
| 3520 | /* Handles f-string value formatting. */ |
| 3521 | PyObject *result; |
| 3522 | PyObject *fmt_spec; |
| 3523 | PyObject *value; |
| 3524 | PyObject *(*conv_fn)(PyObject *); |
| 3525 | int which_conversion = oparg & FVC_MASK; |
| 3526 | int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC; |
| 3527 | |
| 3528 | fmt_spec = have_fmt_spec ? POP() : NULL; |
Eric V. Smith | 135d5f4 | 2016-02-05 18:23:08 -0500 | [diff] [blame] | 3529 | value = POP(); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3530 | |
| 3531 | /* See if any conversion is specified. */ |
| 3532 | switch (which_conversion) { |
| 3533 | case FVC_STR: conv_fn = PyObject_Str; break; |
| 3534 | case FVC_REPR: conv_fn = PyObject_Repr; break; |
| 3535 | case FVC_ASCII: conv_fn = PyObject_ASCII; break; |
| 3536 | |
| 3537 | /* Must be 0 (meaning no conversion), since only four |
| 3538 | values are allowed by (oparg & FVC_MASK). */ |
| 3539 | default: conv_fn = NULL; break; |
| 3540 | } |
| 3541 | |
| 3542 | /* If there's a conversion function, call it and replace |
| 3543 | value with that result. Otherwise, just use value, |
| 3544 | without conversion. */ |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3545 | if (conv_fn != NULL) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3546 | result = conv_fn(value); |
| 3547 | Py_DECREF(value); |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3548 | if (result == NULL) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3549 | Py_XDECREF(fmt_spec); |
| 3550 | goto error; |
| 3551 | } |
| 3552 | value = result; |
| 3553 | } |
| 3554 | |
| 3555 | /* If value is a unicode object, and there's no fmt_spec, |
| 3556 | then we know the result of format(value) is value |
| 3557 | itself. In that case, skip calling format(). I plan to |
| 3558 | move this optimization in to PyObject_Format() |
| 3559 | itself. */ |
| 3560 | if (PyUnicode_CheckExact(value) && fmt_spec == NULL) { |
| 3561 | /* Do nothing, just transfer ownership to result. */ |
| 3562 | result = value; |
| 3563 | } else { |
| 3564 | /* Actually call format(). */ |
| 3565 | result = PyObject_Format(value, fmt_spec); |
| 3566 | Py_DECREF(value); |
| 3567 | Py_XDECREF(fmt_spec); |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3568 | if (result == NULL) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3569 | goto error; |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3570 | } |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3571 | } |
| 3572 | |
Eric V. Smith | 135d5f4 | 2016-02-05 18:23:08 -0500 | [diff] [blame] | 3573 | PUSH(result); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3574 | DISPATCH(); |
| 3575 | } |
| 3576 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3577 | TARGET(EXTENDED_ARG) { |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 3578 | int oldoparg = oparg; |
| 3579 | NEXTOPARG(); |
| 3580 | oparg |= oldoparg << 8; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3581 | goto dispatch_opcode; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3582 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 3583 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3584 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 3585 | #if USE_COMPUTED_GOTOS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3586 | _unknown_opcode: |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 3587 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3588 | default: |
| 3589 | fprintf(stderr, |
| 3590 | "XXX lineno: %d, opcode: %d\n", |
| 3591 | PyFrame_GetLineNumber(f), |
| 3592 | opcode); |
| 3593 | PyErr_SetString(PyExc_SystemError, "unknown opcode"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3594 | goto error; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 3595 | |
| 3596 | #ifdef CASE_TOO_BIG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3597 | } |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 3598 | #endif |
| 3599 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3600 | } /* switch */ |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3601 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3602 | /* This should never be reached. Every opcode should end with DISPATCH() |
| 3603 | or goto error. */ |
| 3604 | assert(0); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3605 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3606 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3607 | READ_TIMESTAMP(inst1); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 3608 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3609 | assert(why == WHY_NOT); |
| 3610 | why = WHY_EXCEPTION; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3611 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3612 | /* Double-check exception status. */ |
Victor Stinner | 365b693 | 2013-07-12 00:11:58 +0200 | [diff] [blame] | 3613 | #ifdef NDEBUG |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3614 | if (!PyErr_Occurred()) |
| 3615 | PyErr_SetString(PyExc_SystemError, |
| 3616 | "error return without exception set"); |
Victor Stinner | 365b693 | 2013-07-12 00:11:58 +0200 | [diff] [blame] | 3617 | #else |
| 3618 | assert(PyErr_Occurred()); |
| 3619 | #endif |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3620 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3621 | /* Log traceback info. */ |
| 3622 | PyTraceBack_Here(f); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3623 | |
Benjamin Peterson | 51f4616 | 2013-01-23 08:38:47 -0500 | [diff] [blame] | 3624 | if (tstate->c_tracefunc != NULL) |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3625 | call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, |
| 3626 | tstate, f); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3627 | |
Raymond Hettinger | 1dd8309 | 2004-02-06 18:32:33 +0000 | [diff] [blame] | 3628 | fast_block_end: |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3629 | assert(why != WHY_NOT); |
| 3630 | |
| 3631 | /* Unwind stacks if a (pseudo) exception occurred */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3632 | while (why != WHY_NOT && f->f_iblock > 0) { |
| 3633 | /* Peek at the current block. */ |
| 3634 | PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1]; |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3635 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3636 | assert(why != WHY_YIELD); |
| 3637 | if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) { |
| 3638 | why = WHY_NOT; |
| 3639 | JUMPTO(PyLong_AS_LONG(retval)); |
| 3640 | Py_DECREF(retval); |
| 3641 | break; |
| 3642 | } |
| 3643 | /* Now we have to pop the block. */ |
| 3644 | f->f_iblock--; |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3645 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3646 | if (b->b_type == EXCEPT_HANDLER) { |
| 3647 | UNWIND_EXCEPT_HANDLER(b); |
| 3648 | continue; |
| 3649 | } |
| 3650 | UNWIND_BLOCK(b); |
| 3651 | if (b->b_type == SETUP_LOOP && why == WHY_BREAK) { |
| 3652 | why = WHY_NOT; |
| 3653 | JUMPTO(b->b_handler); |
| 3654 | break; |
| 3655 | } |
| 3656 | if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT |
| 3657 | || b->b_type == SETUP_FINALLY)) { |
| 3658 | PyObject *exc, *val, *tb; |
| 3659 | int handler = b->b_handler; |
| 3660 | /* Beware, this invalidates all b->b_* fields */ |
| 3661 | PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL()); |
| 3662 | PUSH(tstate->exc_traceback); |
| 3663 | PUSH(tstate->exc_value); |
| 3664 | if (tstate->exc_type != NULL) { |
| 3665 | PUSH(tstate->exc_type); |
| 3666 | } |
| 3667 | else { |
| 3668 | Py_INCREF(Py_None); |
| 3669 | PUSH(Py_None); |
| 3670 | } |
| 3671 | PyErr_Fetch(&exc, &val, &tb); |
| 3672 | /* Make the raw exception data |
| 3673 | available to the handler, |
| 3674 | so a program can emulate the |
| 3675 | Python main loop. */ |
| 3676 | PyErr_NormalizeException( |
| 3677 | &exc, &val, &tb); |
Victor Stinner | 7eab0d0 | 2013-07-15 21:16:27 +0200 | [diff] [blame] | 3678 | if (tb != NULL) |
| 3679 | PyException_SetTraceback(val, tb); |
| 3680 | else |
| 3681 | PyException_SetTraceback(val, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3682 | Py_INCREF(exc); |
| 3683 | tstate->exc_type = exc; |
| 3684 | Py_INCREF(val); |
| 3685 | tstate->exc_value = val; |
| 3686 | tstate->exc_traceback = tb; |
| 3687 | if (tb == NULL) |
| 3688 | tb = Py_None; |
| 3689 | Py_INCREF(tb); |
| 3690 | PUSH(tb); |
| 3691 | PUSH(val); |
| 3692 | PUSH(exc); |
| 3693 | why = WHY_NOT; |
| 3694 | JUMPTO(handler); |
| 3695 | break; |
| 3696 | } |
| 3697 | if (b->b_type == SETUP_FINALLY) { |
| 3698 | if (why & (WHY_RETURN | WHY_CONTINUE)) |
| 3699 | PUSH(retval); |
| 3700 | PUSH(PyLong_FromLong((long)why)); |
| 3701 | why = WHY_NOT; |
| 3702 | JUMPTO(b->b_handler); |
| 3703 | break; |
| 3704 | } |
| 3705 | } /* unwind stack */ |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3706 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3707 | /* End the loop if we still have an error (or return) */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3708 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3709 | if (why != WHY_NOT) |
| 3710 | break; |
| 3711 | READ_TIMESTAMP(loop1); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3712 | |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 3713 | assert(!PyErr_Occurred()); |
| 3714 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3715 | } /* main loop */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3716 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3717 | assert(why != WHY_YIELD); |
| 3718 | /* Pop remaining stack entries. */ |
| 3719 | while (!EMPTY()) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3720 | PyObject *o = POP(); |
| 3721 | Py_XDECREF(o); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3722 | } |
Guido van Rossum | 35974fb | 2001-12-06 21:28:18 +0000 | [diff] [blame] | 3723 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3724 | if (why != WHY_RETURN) |
| 3725 | retval = NULL; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3726 | |
Victor Stinner | 4a7cc88 | 2015-03-06 23:35:27 +0100 | [diff] [blame] | 3727 | assert((retval != NULL) ^ (PyErr_Occurred() != NULL)); |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 3728 | |
Raymond Hettinger | 1dd8309 | 2004-02-06 18:32:33 +0000 | [diff] [blame] | 3729 | fast_yield: |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 3730 | if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) { |
Victor Stinner | 26f7b8a | 2015-01-31 10:29:47 +0100 | [diff] [blame] | 3731 | |
Benjamin Peterson | ac91341 | 2011-07-03 16:25:11 -0500 | [diff] [blame] | 3732 | /* The purpose of this block is to put aside the generator's exception |
| 3733 | state and restore that of the calling frame. If the current |
| 3734 | exception state is from the caller, we clear the exception values |
| 3735 | on the generator frame, so they are not swapped back in latter. The |
| 3736 | origin of the current exception state is determined by checking for |
| 3737 | except handler blocks, which we must be in iff a new exception |
| 3738 | state came into existence in this frame. (An uncaught exception |
| 3739 | would have why == WHY_EXCEPTION, and we wouldn't be here). */ |
| 3740 | int i; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3741 | for (i = 0; i < f->f_iblock; i++) { |
| 3742 | if (f->f_blockstack[i].b_type == EXCEPT_HANDLER) { |
Benjamin Peterson | ac91341 | 2011-07-03 16:25:11 -0500 | [diff] [blame] | 3743 | break; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3744 | } |
| 3745 | } |
Benjamin Peterson | ac91341 | 2011-07-03 16:25:11 -0500 | [diff] [blame] | 3746 | if (i == f->f_iblock) |
| 3747 | /* We did not create this exception. */ |
Benjamin Peterson | 8788024 | 2011-07-03 16:48:31 -0500 | [diff] [blame] | 3748 | restore_and_clear_exc_state(tstate, f); |
Benjamin Peterson | ac91341 | 2011-07-03 16:25:11 -0500 | [diff] [blame] | 3749 | else |
Benjamin Peterson | 8788024 | 2011-07-03 16:48:31 -0500 | [diff] [blame] | 3750 | swap_exc_state(tstate, f); |
Benjamin Peterson | ac91341 | 2011-07-03 16:25:11 -0500 | [diff] [blame] | 3751 | } |
Benjamin Peterson | 83195c3 | 2011-07-03 13:44:00 -0500 | [diff] [blame] | 3752 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3753 | if (tstate->use_tracing) { |
Benjamin Peterson | 51f4616 | 2013-01-23 08:38:47 -0500 | [diff] [blame] | 3754 | if (tstate->c_tracefunc) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3755 | if (why == WHY_RETURN || why == WHY_YIELD) { |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3756 | if (call_trace(tstate->c_tracefunc, tstate->c_traceobj, |
| 3757 | tstate, f, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3758 | PyTrace_RETURN, retval)) { |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 3759 | Py_CLEAR(retval); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3760 | why = WHY_EXCEPTION; |
| 3761 | } |
| 3762 | } |
| 3763 | else if (why == WHY_EXCEPTION) { |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3764 | call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj, |
| 3765 | tstate, f, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3766 | PyTrace_RETURN, NULL); |
| 3767 | } |
| 3768 | } |
| 3769 | if (tstate->c_profilefunc) { |
| 3770 | if (why == WHY_EXCEPTION) |
| 3771 | call_trace_protected(tstate->c_profilefunc, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3772 | tstate->c_profileobj, |
| 3773 | tstate, f, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3774 | PyTrace_RETURN, NULL); |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3775 | else if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, |
| 3776 | tstate, f, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3777 | PyTrace_RETURN, retval)) { |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 3778 | Py_CLEAR(retval); |
Brett Cannon | b94767f | 2011-02-22 20:15:44 +0000 | [diff] [blame] | 3779 | /* why = WHY_EXCEPTION; */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3780 | } |
| 3781 | } |
| 3782 | } |
Guido van Rossum | a424013 | 1997-01-21 21:18:36 +0000 | [diff] [blame] | 3783 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3784 | /* pop frame */ |
Thomas Wouters | ce272b6 | 2007-09-19 21:19:28 +0000 | [diff] [blame] | 3785 | exit_eval_frame: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3786 | Py_LeaveRecursiveCall(); |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 3787 | f->f_executing = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3788 | tstate->frame = f->f_back; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3789 | |
Victor Stinner | efde146 | 2015-03-21 15:04:43 +0100 | [diff] [blame] | 3790 | return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx"); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3791 | } |
| 3792 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3793 | static void |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3794 | format_missing(const char *kind, PyCodeObject *co, PyObject *names) |
| 3795 | { |
| 3796 | int err; |
| 3797 | Py_ssize_t len = PyList_GET_SIZE(names); |
| 3798 | PyObject *name_str, *comma, *tail, *tmp; |
| 3799 | |
| 3800 | assert(PyList_CheckExact(names)); |
| 3801 | assert(len >= 1); |
| 3802 | /* Deal with the joys of natural language. */ |
| 3803 | switch (len) { |
| 3804 | case 1: |
| 3805 | name_str = PyList_GET_ITEM(names, 0); |
| 3806 | Py_INCREF(name_str); |
| 3807 | break; |
| 3808 | case 2: |
| 3809 | name_str = PyUnicode_FromFormat("%U and %U", |
| 3810 | PyList_GET_ITEM(names, len - 2), |
| 3811 | PyList_GET_ITEM(names, len - 1)); |
| 3812 | break; |
| 3813 | default: |
| 3814 | tail = PyUnicode_FromFormat(", %U, and %U", |
| 3815 | PyList_GET_ITEM(names, len - 2), |
| 3816 | PyList_GET_ITEM(names, len - 1)); |
Benjamin Peterson | d1ab608 | 2012-06-01 11:18:22 -0700 | [diff] [blame] | 3817 | if (tail == NULL) |
| 3818 | return; |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3819 | /* Chop off the last two objects in the list. This shouldn't actually |
| 3820 | fail, but we can't be too careful. */ |
| 3821 | err = PyList_SetSlice(names, len - 2, len, NULL); |
| 3822 | if (err == -1) { |
| 3823 | Py_DECREF(tail); |
| 3824 | return; |
| 3825 | } |
| 3826 | /* Stitch everything up into a nice comma-separated list. */ |
| 3827 | comma = PyUnicode_FromString(", "); |
| 3828 | if (comma == NULL) { |
| 3829 | Py_DECREF(tail); |
| 3830 | return; |
| 3831 | } |
| 3832 | tmp = PyUnicode_Join(comma, names); |
| 3833 | Py_DECREF(comma); |
| 3834 | if (tmp == NULL) { |
| 3835 | Py_DECREF(tail); |
| 3836 | return; |
| 3837 | } |
| 3838 | name_str = PyUnicode_Concat(tmp, tail); |
| 3839 | Py_DECREF(tmp); |
| 3840 | Py_DECREF(tail); |
| 3841 | break; |
| 3842 | } |
| 3843 | if (name_str == NULL) |
| 3844 | return; |
| 3845 | PyErr_Format(PyExc_TypeError, |
| 3846 | "%U() missing %i required %s argument%s: %U", |
| 3847 | co->co_name, |
| 3848 | len, |
| 3849 | kind, |
| 3850 | len == 1 ? "" : "s", |
| 3851 | name_str); |
| 3852 | Py_DECREF(name_str); |
| 3853 | } |
| 3854 | |
| 3855 | static void |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3856 | missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount, |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3857 | PyObject **fastlocals) |
| 3858 | { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3859 | Py_ssize_t i, j = 0; |
| 3860 | Py_ssize_t start, end; |
| 3861 | int positional = (defcount != -1); |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3862 | const char *kind = positional ? "positional" : "keyword-only"; |
| 3863 | PyObject *missing_names; |
| 3864 | |
| 3865 | /* Compute the names of the arguments that are missing. */ |
| 3866 | missing_names = PyList_New(missing); |
| 3867 | if (missing_names == NULL) |
| 3868 | return; |
| 3869 | if (positional) { |
| 3870 | start = 0; |
| 3871 | end = co->co_argcount - defcount; |
| 3872 | } |
| 3873 | else { |
| 3874 | start = co->co_argcount; |
| 3875 | end = start + co->co_kwonlyargcount; |
| 3876 | } |
| 3877 | for (i = start; i < end; i++) { |
| 3878 | if (GETLOCAL(i) == NULL) { |
| 3879 | PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i); |
| 3880 | PyObject *name = PyObject_Repr(raw); |
| 3881 | if (name == NULL) { |
| 3882 | Py_DECREF(missing_names); |
| 3883 | return; |
| 3884 | } |
| 3885 | PyList_SET_ITEM(missing_names, j++, name); |
| 3886 | } |
| 3887 | } |
| 3888 | assert(j == missing); |
| 3889 | format_missing(kind, co, missing_names); |
| 3890 | Py_DECREF(missing_names); |
| 3891 | } |
| 3892 | |
| 3893 | static void |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3894 | too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount, |
| 3895 | PyObject **fastlocals) |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3896 | { |
| 3897 | int plural; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3898 | Py_ssize_t kwonly_given = 0; |
| 3899 | Py_ssize_t i; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3900 | PyObject *sig, *kwonly_sig; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3901 | Py_ssize_t co_argcount = co->co_argcount; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3902 | |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3903 | assert((co->co_flags & CO_VARARGS) == 0); |
| 3904 | /* Count missing keyword-only args. */ |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3905 | for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) { |
| 3906 | if (GETLOCAL(i) != NULL) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3907 | kwonly_given++; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3908 | } |
| 3909 | } |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3910 | if (defcount) { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3911 | Py_ssize_t atleast = co_argcount - defcount; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3912 | plural = 1; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3913 | sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3914 | } |
| 3915 | else { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3916 | plural = (co_argcount != 1); |
| 3917 | sig = PyUnicode_FromFormat("%zd", co_argcount); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3918 | } |
| 3919 | if (sig == NULL) |
| 3920 | return; |
| 3921 | if (kwonly_given) { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3922 | const char *format = " positional argument%s (and %zd keyword-only argument%s)"; |
| 3923 | kwonly_sig = PyUnicode_FromFormat(format, |
| 3924 | given != 1 ? "s" : "", |
| 3925 | kwonly_given, |
| 3926 | kwonly_given != 1 ? "s" : ""); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3927 | if (kwonly_sig == NULL) { |
| 3928 | Py_DECREF(sig); |
| 3929 | return; |
| 3930 | } |
| 3931 | } |
| 3932 | else { |
| 3933 | /* This will not fail. */ |
| 3934 | kwonly_sig = PyUnicode_FromString(""); |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3935 | assert(kwonly_sig != NULL); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3936 | } |
| 3937 | PyErr_Format(PyExc_TypeError, |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3938 | "%U() takes %U positional argument%s but %zd%U %s given", |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3939 | co->co_name, |
| 3940 | sig, |
| 3941 | plural ? "s" : "", |
| 3942 | given, |
| 3943 | kwonly_sig, |
| 3944 | given == 1 && !kwonly_given ? "was" : "were"); |
| 3945 | Py_DECREF(sig); |
| 3946 | Py_DECREF(kwonly_sig); |
| 3947 | } |
| 3948 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3949 | /* 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] | 3950 | PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3951 | the test in the if statements in Misc/gdbinit (pystack and pystackv). */ |
Skip Montanaro | 786ea6b | 2004-03-01 15:44:05 +0000 | [diff] [blame] | 3952 | |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 3953 | static PyObject * |
| 3954 | _PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals, |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3955 | PyObject **args, Py_ssize_t argcount, |
| 3956 | PyObject **kws, Py_ssize_t kwcount, |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3957 | PyObject *kwnames, PyObject **kwstack, |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3958 | PyObject **defs, Py_ssize_t defcount, |
| 3959 | PyObject *kwdefs, PyObject *closure, |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 3960 | PyObject *name, PyObject *qualname) |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3961 | { |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 3962 | PyCodeObject* co = (PyCodeObject*)_co; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 3963 | PyFrameObject *f; |
| 3964 | PyObject *retval = NULL; |
| 3965 | PyObject **fastlocals, **freevars; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3966 | PyThreadState *tstate; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3967 | PyObject *x, *u; |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 3968 | const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount; |
| 3969 | Py_ssize_t i, n; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3970 | PyObject *kwdict; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3971 | Py_ssize_t kwcount2 = kwnames == NULL ? 0 : PyTuple_GET_SIZE(kwnames); |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3972 | |
| 3973 | assert((kwcount == 0) || (kws != NULL)); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3974 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3975 | if (globals == NULL) { |
| 3976 | PyErr_SetString(PyExc_SystemError, |
| 3977 | "PyEval_EvalCodeEx: NULL globals"); |
| 3978 | return NULL; |
| 3979 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3980 | |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3981 | /* Create the frame */ |
| 3982 | tstate = PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3983 | assert(tstate != NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3984 | f = PyFrame_New(tstate, co, globals, locals); |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3985 | if (f == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3986 | return NULL; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3987 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3988 | fastlocals = f->f_localsplus; |
| 3989 | freevars = f->f_localsplus + co->co_nlocals; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3990 | |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3991 | /* Create a dictionary for keyword parameters (**kwags) */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3992 | if (co->co_flags & CO_VARKEYWORDS) { |
| 3993 | kwdict = PyDict_New(); |
| 3994 | if (kwdict == NULL) |
| 3995 | goto fail; |
| 3996 | i = total_args; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3997 | if (co->co_flags & CO_VARARGS) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3998 | i++; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3999 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4000 | SETLOCAL(i, kwdict); |
| 4001 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4002 | else { |
| 4003 | kwdict = NULL; |
| 4004 | } |
| 4005 | |
| 4006 | /* Copy positional arguments into local variables */ |
| 4007 | if (argcount > co->co_argcount) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4008 | n = co->co_argcount; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4009 | } |
| 4010 | else { |
| 4011 | n = argcount; |
| 4012 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4013 | for (i = 0; i < n; i++) { |
| 4014 | x = args[i]; |
| 4015 | Py_INCREF(x); |
| 4016 | SETLOCAL(i, x); |
| 4017 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4018 | |
| 4019 | /* Pack other positional arguments into the *args argument */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4020 | if (co->co_flags & CO_VARARGS) { |
| 4021 | u = PyTuple_New(argcount - n); |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4022 | if (u == NULL) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4023 | goto fail; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4024 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4025 | SETLOCAL(total_args, u); |
| 4026 | for (i = n; i < argcount; i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4027 | x = args[i]; |
| 4028 | Py_INCREF(x); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4029 | PyTuple_SET_ITEM(u, i-n, x); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4030 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4031 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4032 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4033 | /* Handle keyword arguments passed as an array of (key, value) pairs */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4034 | for (i = 0; i < kwcount; i++) { |
| 4035 | PyObject **co_varnames; |
| 4036 | PyObject *keyword = kws[2*i]; |
| 4037 | PyObject *value = kws[2*i + 1]; |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 4038 | Py_ssize_t j; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4039 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4040 | if (keyword == NULL || !PyUnicode_Check(keyword)) { |
| 4041 | PyErr_Format(PyExc_TypeError, |
| 4042 | "%U() keywords must be strings", |
| 4043 | co->co_name); |
| 4044 | goto fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4045 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4046 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4047 | /* Speed hack: do raw pointer compares. As names are |
| 4048 | normally interned this should almost always hit. */ |
| 4049 | co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item; |
| 4050 | for (j = 0; j < total_args; j++) { |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 4051 | PyObject *name = co_varnames[j]; |
| 4052 | if (name == keyword) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4053 | goto kw_found; |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 4054 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4055 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4056 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4057 | /* Slow fallback, just in case */ |
| 4058 | for (j = 0; j < total_args; j++) { |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 4059 | PyObject *name = co_varnames[j]; |
| 4060 | int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ); |
| 4061 | if (cmp > 0) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4062 | goto kw_found; |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 4063 | } |
| 4064 | else if (cmp < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4065 | goto fail; |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 4066 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4067 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4068 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4069 | if (j >= total_args && kwdict == NULL) { |
| 4070 | PyErr_Format(PyExc_TypeError, |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 4071 | "%U() got an unexpected keyword argument '%S'", |
| 4072 | co->co_name, keyword); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4073 | goto fail; |
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 | |
Christian Heimes | 0bd447f | 2013-07-20 14:48:10 +0200 | [diff] [blame] | 4076 | if (PyDict_SetItem(kwdict, keyword, value) == -1) { |
| 4077 | goto fail; |
| 4078 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4079 | continue; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4080 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4081 | kw_found: |
| 4082 | if (GETLOCAL(j) != NULL) { |
| 4083 | PyErr_Format(PyExc_TypeError, |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 4084 | "%U() got multiple values for argument '%S'", |
| 4085 | co->co_name, keyword); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4086 | goto fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4087 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4088 | Py_INCREF(value); |
| 4089 | SETLOCAL(j, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4090 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4091 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4092 | /* Handle keyword arguments passed as keys tuple + values array */ |
| 4093 | for (i = 0; i < kwcount2; i++) { |
| 4094 | PyObject **co_varnames; |
| 4095 | PyObject *keyword = PyTuple_GET_ITEM(kwnames, i); |
| 4096 | PyObject *value = kwstack[i]; |
| 4097 | int j; |
| 4098 | if (keyword == NULL || !PyUnicode_Check(keyword)) { |
| 4099 | PyErr_Format(PyExc_TypeError, |
| 4100 | "%U() keywords must be strings", |
| 4101 | co->co_name); |
| 4102 | goto fail; |
| 4103 | } |
| 4104 | /* Speed hack: do raw pointer compares. As names are |
| 4105 | normally interned this should almost always hit. */ |
| 4106 | co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item; |
| 4107 | for (j = 0; j < total_args; j++) { |
| 4108 | PyObject *nm = co_varnames[j]; |
| 4109 | if (nm == keyword) |
| 4110 | goto kw_found2; |
| 4111 | } |
| 4112 | /* Slow fallback, just in case */ |
| 4113 | for (j = 0; j < total_args; j++) { |
| 4114 | PyObject *nm = co_varnames[j]; |
| 4115 | int cmp = PyObject_RichCompareBool( |
| 4116 | keyword, nm, Py_EQ); |
| 4117 | if (cmp > 0) |
| 4118 | goto kw_found2; |
| 4119 | else if (cmp < 0) |
| 4120 | goto fail; |
| 4121 | } |
| 4122 | if (j >= total_args && kwdict == NULL) { |
| 4123 | PyErr_Format(PyExc_TypeError, |
| 4124 | "%U() got an unexpected " |
| 4125 | "keyword argument '%S'", |
| 4126 | co->co_name, |
| 4127 | keyword); |
| 4128 | goto fail; |
| 4129 | } |
| 4130 | if (PyDict_SetItem(kwdict, keyword, value) == -1) { |
| 4131 | goto fail; |
| 4132 | } |
| 4133 | continue; |
| 4134 | kw_found2: |
| 4135 | if (GETLOCAL(j) != NULL) { |
| 4136 | PyErr_Format(PyExc_TypeError, |
| 4137 | "%U() got multiple " |
| 4138 | "values for argument '%S'", |
| 4139 | co->co_name, |
| 4140 | keyword); |
| 4141 | goto fail; |
| 4142 | } |
| 4143 | Py_INCREF(value); |
| 4144 | SETLOCAL(j, value); |
| 4145 | } |
| 4146 | |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4147 | /* Check the number of positional arguments */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4148 | if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) { |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4149 | too_many_positional(co, argcount, defcount, fastlocals); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4150 | goto fail; |
| 4151 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4152 | |
| 4153 | /* Add missing positional arguments (copy default values from defs) */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4154 | if (argcount < co->co_argcount) { |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 4155 | Py_ssize_t m = co->co_argcount - defcount; |
| 4156 | Py_ssize_t missing = 0; |
| 4157 | for (i = argcount; i < m; i++) { |
| 4158 | if (GETLOCAL(i) == NULL) { |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4159 | missing++; |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 4160 | } |
| 4161 | } |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4162 | if (missing) { |
| 4163 | missing_arguments(co, missing, defcount, fastlocals); |
| 4164 | goto fail; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4165 | } |
| 4166 | if (n > m) |
| 4167 | i = n - m; |
| 4168 | else |
| 4169 | i = 0; |
| 4170 | for (; i < defcount; i++) { |
| 4171 | if (GETLOCAL(m+i) == NULL) { |
| 4172 | PyObject *def = defs[i]; |
| 4173 | Py_INCREF(def); |
| 4174 | SETLOCAL(m+i, def); |
| 4175 | } |
| 4176 | } |
| 4177 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4178 | |
| 4179 | /* Add missing keyword arguments (copy default values from kwdefs) */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4180 | if (co->co_kwonlyargcount > 0) { |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 4181 | Py_ssize_t missing = 0; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4182 | for (i = co->co_argcount; i < total_args; i++) { |
| 4183 | PyObject *name; |
| 4184 | if (GETLOCAL(i) != NULL) |
| 4185 | continue; |
| 4186 | name = PyTuple_GET_ITEM(co->co_varnames, i); |
| 4187 | if (kwdefs != NULL) { |
| 4188 | PyObject *def = PyDict_GetItem(kwdefs, name); |
| 4189 | if (def) { |
| 4190 | Py_INCREF(def); |
| 4191 | SETLOCAL(i, def); |
| 4192 | continue; |
| 4193 | } |
| 4194 | } |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4195 | missing++; |
| 4196 | } |
| 4197 | if (missing) { |
| 4198 | missing_arguments(co, missing, -1, fastlocals); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4199 | goto fail; |
| 4200 | } |
| 4201 | } |
| 4202 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4203 | /* Allocate and initialize storage for cell vars, and copy free |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4204 | vars into frame. */ |
| 4205 | for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4206 | PyObject *c; |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4207 | int arg; |
| 4208 | /* Possibly account for the cell variable being an argument. */ |
| 4209 | if (co->co_cell2arg != NULL && |
Guido van Rossum | 6832c81 | 2013-05-10 08:47:42 -0700 | [diff] [blame] | 4210 | (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) { |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4211 | c = PyCell_New(GETLOCAL(arg)); |
Benjamin Peterson | 159ae41 | 2013-05-12 18:16:06 -0500 | [diff] [blame] | 4212 | /* Clear the local copy. */ |
| 4213 | SETLOCAL(arg, NULL); |
Guido van Rossum | 6832c81 | 2013-05-10 08:47:42 -0700 | [diff] [blame] | 4214 | } |
| 4215 | else { |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4216 | c = PyCell_New(NULL); |
Guido van Rossum | 6832c81 | 2013-05-10 08:47:42 -0700 | [diff] [blame] | 4217 | } |
Benjamin Peterson | 159ae41 | 2013-05-12 18:16:06 -0500 | [diff] [blame] | 4218 | if (c == NULL) |
| 4219 | goto fail; |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4220 | SETLOCAL(co->co_nlocals + i, c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4221 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4222 | |
| 4223 | /* Copy closure variables to free variables */ |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4224 | for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) { |
| 4225 | PyObject *o = PyTuple_GET_ITEM(closure, i); |
| 4226 | Py_INCREF(o); |
| 4227 | freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4228 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4229 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4230 | /* Handle generator/coroutine/asynchronous generator */ |
| 4231 | if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4232 | PyObject *gen; |
Yury Selivanov | 94c2263 | 2015-06-04 10:16:51 -0400 | [diff] [blame] | 4233 | PyObject *coro_wrapper = tstate->coroutine_wrapper; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 4234 | int is_coro = co->co_flags & CO_COROUTINE; |
Yury Selivanov | 94c2263 | 2015-06-04 10:16:51 -0400 | [diff] [blame] | 4235 | |
| 4236 | if (is_coro && tstate->in_coroutine_wrapper) { |
| 4237 | assert(coro_wrapper != NULL); |
| 4238 | PyErr_Format(PyExc_RuntimeError, |
| 4239 | "coroutine wrapper %.200R attempted " |
| 4240 | "to recursively wrap %.200R", |
| 4241 | coro_wrapper, |
| 4242 | co); |
| 4243 | goto fail; |
| 4244 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4245 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4246 | /* Don't need to keep the reference to f_back, it will be set |
| 4247 | * when the generator is resumed. */ |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 4248 | Py_CLEAR(f->f_back); |
Neil Schemenauer | 2b13ce8 | 2001-06-21 02:41:10 +0000 | [diff] [blame] | 4249 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4250 | PCALL(PCALL_GENERATOR); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4251 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4252 | /* Create a new generator that owns the ready to run frame |
| 4253 | * and return that as the value. */ |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 4254 | if (is_coro) { |
| 4255 | gen = PyCoro_New(f, name, qualname); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4256 | } else if (co->co_flags & CO_ASYNC_GENERATOR) { |
| 4257 | gen = PyAsyncGen_New(f, name, qualname); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 4258 | } else { |
| 4259 | gen = PyGen_NewWithQualName(f, name, qualname); |
| 4260 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4261 | if (gen == NULL) |
| 4262 | return NULL; |
| 4263 | |
Yury Selivanov | 94c2263 | 2015-06-04 10:16:51 -0400 | [diff] [blame] | 4264 | if (is_coro && coro_wrapper != NULL) { |
| 4265 | PyObject *wrapped; |
| 4266 | tstate->in_coroutine_wrapper = 1; |
| 4267 | wrapped = PyObject_CallFunction(coro_wrapper, "N", gen); |
| 4268 | tstate->in_coroutine_wrapper = 0; |
| 4269 | return wrapped; |
| 4270 | } |
Yury Selivanov | aab3c4a | 2015-06-02 18:43:51 -0400 | [diff] [blame] | 4271 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4272 | return gen; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4273 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4274 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4275 | retval = PyEval_EvalFrameEx(f,0); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4276 | |
Thomas Wouters | ce272b6 | 2007-09-19 21:19:28 +0000 | [diff] [blame] | 4277 | fail: /* Jump here from prelude on failure */ |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4278 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4279 | /* decref'ing the frame can cause __del__ methods to get invoked, |
| 4280 | which can call back into Python. While we're done with the |
| 4281 | current Python frame (f), the associated C stack is still in use, |
| 4282 | so recursion_depth must be boosted for the duration. |
| 4283 | */ |
| 4284 | assert(tstate != NULL); |
| 4285 | ++tstate->recursion_depth; |
| 4286 | Py_DECREF(f); |
| 4287 | --tstate->recursion_depth; |
| 4288 | return retval; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4289 | } |
| 4290 | |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 4291 | PyObject * |
| 4292 | PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals, |
| 4293 | PyObject **args, int argcount, PyObject **kws, int kwcount, |
| 4294 | PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure) |
| 4295 | { |
| 4296 | return _PyEval_EvalCodeWithName(_co, globals, locals, |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4297 | args, argcount, |
| 4298 | kws, kwcount, |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4299 | NULL, NULL, |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4300 | defs, defcount, |
| 4301 | kwdefs, closure, |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 4302 | NULL, NULL); |
| 4303 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4304 | |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4305 | static PyObject * |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4306 | special_lookup(PyObject *o, _Py_Identifier *id) |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4307 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4308 | PyObject *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4309 | res = _PyObject_LookupSpecial(o, id); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4310 | if (res == NULL && !PyErr_Occurred()) { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4311 | PyErr_SetObject(PyExc_AttributeError, id->object); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4312 | return NULL; |
| 4313 | } |
| 4314 | return res; |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4315 | } |
| 4316 | |
| 4317 | |
Benjamin Peterson | 8788024 | 2011-07-03 16:48:31 -0500 | [diff] [blame] | 4318 | /* These 3 functions deal with the exception state of generators. */ |
| 4319 | |
| 4320 | static void |
| 4321 | save_exc_state(PyThreadState *tstate, PyFrameObject *f) |
| 4322 | { |
| 4323 | PyObject *type, *value, *traceback; |
| 4324 | Py_XINCREF(tstate->exc_type); |
| 4325 | Py_XINCREF(tstate->exc_value); |
| 4326 | Py_XINCREF(tstate->exc_traceback); |
| 4327 | type = f->f_exc_type; |
| 4328 | value = f->f_exc_value; |
| 4329 | traceback = f->f_exc_traceback; |
| 4330 | f->f_exc_type = tstate->exc_type; |
| 4331 | f->f_exc_value = tstate->exc_value; |
| 4332 | f->f_exc_traceback = tstate->exc_traceback; |
| 4333 | Py_XDECREF(type); |
| 4334 | Py_XDECREF(value); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 4335 | Py_XDECREF(traceback); |
Benjamin Peterson | 8788024 | 2011-07-03 16:48:31 -0500 | [diff] [blame] | 4336 | } |
| 4337 | |
| 4338 | static void |
| 4339 | swap_exc_state(PyThreadState *tstate, PyFrameObject *f) |
| 4340 | { |
| 4341 | PyObject *tmp; |
| 4342 | tmp = tstate->exc_type; |
| 4343 | tstate->exc_type = f->f_exc_type; |
| 4344 | f->f_exc_type = tmp; |
| 4345 | tmp = tstate->exc_value; |
| 4346 | tstate->exc_value = f->f_exc_value; |
| 4347 | f->f_exc_value = tmp; |
| 4348 | tmp = tstate->exc_traceback; |
| 4349 | tstate->exc_traceback = f->f_exc_traceback; |
| 4350 | f->f_exc_traceback = tmp; |
| 4351 | } |
| 4352 | |
| 4353 | static void |
| 4354 | restore_and_clear_exc_state(PyThreadState *tstate, PyFrameObject *f) |
| 4355 | { |
| 4356 | PyObject *type, *value, *tb; |
| 4357 | type = tstate->exc_type; |
| 4358 | value = tstate->exc_value; |
| 4359 | tb = tstate->exc_traceback; |
| 4360 | tstate->exc_type = f->f_exc_type; |
| 4361 | tstate->exc_value = f->f_exc_value; |
| 4362 | tstate->exc_traceback = f->f_exc_traceback; |
| 4363 | f->f_exc_type = NULL; |
| 4364 | f->f_exc_value = NULL; |
| 4365 | f->f_exc_traceback = NULL; |
| 4366 | Py_XDECREF(type); |
| 4367 | Py_XDECREF(value); |
| 4368 | Py_XDECREF(tb); |
| 4369 | } |
| 4370 | |
| 4371 | |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 4372 | /* Logic for the raise statement (too complicated for inlining). |
| 4373 | This *consumes* a reference count to each of its arguments. */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4374 | static int |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4375 | do_raise(PyObject *exc, PyObject *cause) |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 4376 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4377 | PyObject *type = NULL, *value = NULL; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4378 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4379 | if (exc == NULL) { |
| 4380 | /* Reraise */ |
| 4381 | PyThreadState *tstate = PyThreadState_GET(); |
| 4382 | PyObject *tb; |
| 4383 | type = tstate->exc_type; |
| 4384 | value = tstate->exc_value; |
| 4385 | tb = tstate->exc_traceback; |
Victor Stinner | eec9331 | 2016-08-18 18:13:10 +0200 | [diff] [blame] | 4386 | if (type == Py_None || type == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4387 | PyErr_SetString(PyExc_RuntimeError, |
| 4388 | "No active exception to reraise"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4389 | return 0; |
| 4390 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4391 | Py_XINCREF(type); |
| 4392 | Py_XINCREF(value); |
| 4393 | Py_XINCREF(tb); |
| 4394 | PyErr_Restore(type, value, tb); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4395 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4396 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4397 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4398 | /* We support the following forms of raise: |
| 4399 | raise |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4400 | raise <instance> |
| 4401 | raise <type> */ |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 4402 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4403 | if (PyExceptionClass_Check(exc)) { |
| 4404 | type = exc; |
| 4405 | value = PyObject_CallObject(exc, NULL); |
| 4406 | if (value == NULL) |
| 4407 | goto raise_error; |
Benjamin Peterson | 5afa03a | 2011-07-15 14:09:26 -0500 | [diff] [blame] | 4408 | if (!PyExceptionInstance_Check(value)) { |
| 4409 | PyErr_Format(PyExc_TypeError, |
| 4410 | "calling %R should have returned an instance of " |
| 4411 | "BaseException, not %R", |
| 4412 | type, Py_TYPE(value)); |
| 4413 | goto raise_error; |
| 4414 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4415 | } |
| 4416 | else if (PyExceptionInstance_Check(exc)) { |
| 4417 | value = exc; |
| 4418 | type = PyExceptionInstance_Class(exc); |
| 4419 | Py_INCREF(type); |
| 4420 | } |
| 4421 | else { |
| 4422 | /* Not something you can raise. You get an exception |
| 4423 | anyway, just not what you specified :-) */ |
| 4424 | Py_DECREF(exc); |
| 4425 | PyErr_SetString(PyExc_TypeError, |
| 4426 | "exceptions must derive from BaseException"); |
| 4427 | goto raise_error; |
| 4428 | } |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4429 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4430 | if (cause) { |
| 4431 | PyObject *fixed_cause; |
| 4432 | if (PyExceptionClass_Check(cause)) { |
| 4433 | fixed_cause = PyObject_CallObject(cause, NULL); |
| 4434 | if (fixed_cause == NULL) |
| 4435 | goto raise_error; |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 4436 | Py_DECREF(cause); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4437 | } |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 4438 | else if (PyExceptionInstance_Check(cause)) { |
| 4439 | fixed_cause = cause; |
| 4440 | } |
| 4441 | else if (cause == Py_None) { |
| 4442 | Py_DECREF(cause); |
| 4443 | fixed_cause = NULL; |
| 4444 | } |
| 4445 | else { |
| 4446 | PyErr_SetString(PyExc_TypeError, |
| 4447 | "exception causes must derive from " |
| 4448 | "BaseException"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4449 | goto raise_error; |
| 4450 | } |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 4451 | PyException_SetCause(value, fixed_cause); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4452 | } |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4453 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4454 | PyErr_SetObject(type, value); |
| 4455 | /* PyErr_SetObject incref's its arguments */ |
| 4456 | Py_XDECREF(value); |
| 4457 | Py_XDECREF(type); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4458 | return 0; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4459 | |
| 4460 | raise_error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4461 | Py_XDECREF(value); |
| 4462 | Py_XDECREF(type); |
| 4463 | Py_XDECREF(cause); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4464 | return 0; |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 4465 | } |
| 4466 | |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4467 | /* 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] | 4468 | sp). Return 1 for success, 0 if error. |
Antoine Pitrou | 9a2310d | 2008-07-25 22:39:39 +0000 | [diff] [blame] | 4469 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4470 | If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack |
| 4471 | with a variable target. |
| 4472 | */ |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4473 | |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 4474 | static int |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4475 | unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp) |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 4476 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4477 | int i = 0, j = 0; |
| 4478 | Py_ssize_t ll = 0; |
| 4479 | PyObject *it; /* iter(v) */ |
| 4480 | PyObject *w; |
| 4481 | PyObject *l = NULL; /* variable list */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4482 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4483 | assert(v != NULL); |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4484 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4485 | it = PyObject_GetIter(v); |
| 4486 | if (it == NULL) |
| 4487 | goto Error; |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4488 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4489 | for (; i < argcnt; i++) { |
| 4490 | w = PyIter_Next(it); |
| 4491 | if (w == NULL) { |
| 4492 | /* Iterator done, via error or exhaustion. */ |
| 4493 | if (!PyErr_Occurred()) { |
R David Murray | 4171bbe | 2015-04-15 17:08:45 -0400 | [diff] [blame] | 4494 | if (argcntafter == -1) { |
| 4495 | PyErr_Format(PyExc_ValueError, |
| 4496 | "not enough values to unpack (expected %d, got %d)", |
| 4497 | argcnt, i); |
| 4498 | } |
| 4499 | else { |
| 4500 | PyErr_Format(PyExc_ValueError, |
| 4501 | "not enough values to unpack " |
| 4502 | "(expected at least %d, got %d)", |
| 4503 | argcnt + argcntafter, i); |
| 4504 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4505 | } |
| 4506 | goto Error; |
| 4507 | } |
| 4508 | *--sp = w; |
| 4509 | } |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4510 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4511 | if (argcntafter == -1) { |
| 4512 | /* We better have exhausted the iterator now. */ |
| 4513 | w = PyIter_Next(it); |
| 4514 | if (w == NULL) { |
| 4515 | if (PyErr_Occurred()) |
| 4516 | goto Error; |
| 4517 | Py_DECREF(it); |
| 4518 | return 1; |
| 4519 | } |
| 4520 | Py_DECREF(w); |
R David Murray | 4171bbe | 2015-04-15 17:08:45 -0400 | [diff] [blame] | 4521 | PyErr_Format(PyExc_ValueError, |
| 4522 | "too many values to unpack (expected %d)", |
| 4523 | argcnt); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4524 | goto Error; |
| 4525 | } |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4526 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4527 | l = PySequence_List(it); |
| 4528 | if (l == NULL) |
| 4529 | goto Error; |
| 4530 | *--sp = l; |
| 4531 | i++; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4532 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4533 | ll = PyList_GET_SIZE(l); |
| 4534 | if (ll < argcntafter) { |
R David Murray | 4171bbe | 2015-04-15 17:08:45 -0400 | [diff] [blame] | 4535 | PyErr_Format(PyExc_ValueError, |
| 4536 | "not enough values to unpack (expected at least %d, got %zd)", |
| 4537 | argcnt + argcntafter, argcnt + ll); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4538 | goto Error; |
| 4539 | } |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4540 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4541 | /* Pop the "after-variable" args off the list. */ |
| 4542 | for (j = argcntafter; j > 0; j--, i++) { |
| 4543 | *--sp = PyList_GET_ITEM(l, ll - j); |
| 4544 | } |
| 4545 | /* Resize the list. */ |
| 4546 | Py_SIZE(l) = ll - argcntafter; |
| 4547 | Py_DECREF(it); |
| 4548 | return 1; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4549 | |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4550 | Error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4551 | for (; i > 0; i--, sp++) |
| 4552 | Py_DECREF(*sp); |
| 4553 | Py_XDECREF(it); |
| 4554 | return 0; |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 4555 | } |
| 4556 | |
| 4557 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 4558 | #ifdef LLTRACE |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4559 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4560 | prtrace(PyObject *v, const char *str) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4561 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4562 | printf("%s ", str); |
| 4563 | if (PyObject_Print(v, stdout, 0) != 0) |
| 4564 | PyErr_Clear(); /* Don't know what else to do */ |
| 4565 | printf("\n"); |
| 4566 | return 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4567 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4568 | #endif |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4569 | |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4570 | static void |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4571 | call_exc_trace(Py_tracefunc func, PyObject *self, |
| 4572 | PyThreadState *tstate, PyFrameObject *f) |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4573 | { |
Victor Stinner | aaa8ed8 | 2013-07-10 13:57:55 +0200 | [diff] [blame] | 4574 | PyObject *type, *value, *traceback, *orig_traceback, *arg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4575 | int err; |
Antoine Pitrou | 8933521 | 2013-11-23 14:05:23 +0100 | [diff] [blame] | 4576 | PyErr_Fetch(&type, &value, &orig_traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4577 | if (value == NULL) { |
| 4578 | value = Py_None; |
| 4579 | Py_INCREF(value); |
| 4580 | } |
Antoine Pitrou | 8933521 | 2013-11-23 14:05:23 +0100 | [diff] [blame] | 4581 | PyErr_NormalizeException(&type, &value, &orig_traceback); |
| 4582 | traceback = (orig_traceback != NULL) ? orig_traceback : Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4583 | arg = PyTuple_Pack(3, type, value, traceback); |
| 4584 | if (arg == NULL) { |
Antoine Pitrou | 8933521 | 2013-11-23 14:05:23 +0100 | [diff] [blame] | 4585 | PyErr_Restore(type, value, orig_traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4586 | return; |
| 4587 | } |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4588 | err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4589 | Py_DECREF(arg); |
| 4590 | if (err == 0) |
Victor Stinner | aaa8ed8 | 2013-07-10 13:57:55 +0200 | [diff] [blame] | 4591 | PyErr_Restore(type, value, orig_traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4592 | else { |
| 4593 | Py_XDECREF(type); |
| 4594 | Py_XDECREF(value); |
Victor Stinner | aaa8ed8 | 2013-07-10 13:57:55 +0200 | [diff] [blame] | 4595 | Py_XDECREF(orig_traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4596 | } |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4597 | } |
| 4598 | |
Amaury Forgeot d'Arc | f05149a | 2007-11-13 01:05:30 +0000 | [diff] [blame] | 4599 | static int |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4600 | call_trace_protected(Py_tracefunc func, PyObject *obj, |
| 4601 | PyThreadState *tstate, PyFrameObject *frame, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4602 | int what, PyObject *arg) |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 4603 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4604 | PyObject *type, *value, *traceback; |
| 4605 | int err; |
| 4606 | PyErr_Fetch(&type, &value, &traceback); |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4607 | err = call_trace(func, obj, tstate, frame, what, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4608 | if (err == 0) |
| 4609 | { |
| 4610 | PyErr_Restore(type, value, traceback); |
| 4611 | return 0; |
| 4612 | } |
| 4613 | else { |
| 4614 | Py_XDECREF(type); |
| 4615 | Py_XDECREF(value); |
| 4616 | Py_XDECREF(traceback); |
| 4617 | return -1; |
| 4618 | } |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 4619 | } |
| 4620 | |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4621 | static int |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4622 | call_trace(Py_tracefunc func, PyObject *obj, |
| 4623 | PyThreadState *tstate, PyFrameObject *frame, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4624 | int what, PyObject *arg) |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 4625 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4626 | int result; |
| 4627 | if (tstate->tracing) |
| 4628 | return 0; |
| 4629 | tstate->tracing++; |
| 4630 | tstate->use_tracing = 0; |
| 4631 | result = func(obj, frame, what, arg); |
| 4632 | tstate->use_tracing = ((tstate->c_tracefunc != NULL) |
| 4633 | || (tstate->c_profilefunc != NULL)); |
| 4634 | tstate->tracing--; |
| 4635 | return result; |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 4636 | } |
| 4637 | |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 4638 | PyObject * |
| 4639 | _PyEval_CallTracing(PyObject *func, PyObject *args) |
| 4640 | { |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4641 | PyThreadState *tstate = PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4642 | int save_tracing = tstate->tracing; |
| 4643 | int save_use_tracing = tstate->use_tracing; |
| 4644 | PyObject *result; |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 4645 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4646 | tstate->tracing = 0; |
| 4647 | tstate->use_tracing = ((tstate->c_tracefunc != NULL) |
| 4648 | || (tstate->c_profilefunc != NULL)); |
| 4649 | result = PyObject_Call(func, args, NULL); |
| 4650 | tstate->tracing = save_tracing; |
| 4651 | tstate->use_tracing = save_use_tracing; |
| 4652 | return result; |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 4653 | } |
| 4654 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 4655 | /* 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] | 4656 | static int |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 4657 | maybe_call_line_trace(Py_tracefunc func, PyObject *obj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4658 | PyThreadState *tstate, PyFrameObject *frame, |
| 4659 | int *instr_lb, int *instr_ub, int *instr_prev) |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 4660 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4661 | int result = 0; |
| 4662 | int line = frame->f_lineno; |
Michael W. Hudson | 006c752 | 2002-11-08 13:08:46 +0000 | [diff] [blame] | 4663 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4664 | /* If the last instruction executed isn't in the current |
| 4665 | instruction window, reset the window. |
| 4666 | */ |
| 4667 | if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) { |
| 4668 | PyAddrPair bounds; |
| 4669 | line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti, |
| 4670 | &bounds); |
| 4671 | *instr_lb = bounds.ap_lower; |
| 4672 | *instr_ub = bounds.ap_upper; |
| 4673 | } |
| 4674 | /* If the last instruction falls at the start of a line or if |
| 4675 | it represents a jump backwards, update the frame's line |
| 4676 | number and call the trace function. */ |
| 4677 | if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) { |
| 4678 | frame->f_lineno = line; |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4679 | result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4680 | } |
| 4681 | *instr_prev = frame->f_lasti; |
| 4682 | return result; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 4683 | } |
| 4684 | |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 4685 | void |
| 4686 | PyEval_SetProfile(Py_tracefunc func, PyObject *arg) |
Fred Drake | d083839 | 2001-06-16 21:02:31 +0000 | [diff] [blame] | 4687 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4688 | PyThreadState *tstate = PyThreadState_GET(); |
| 4689 | PyObject *temp = tstate->c_profileobj; |
| 4690 | Py_XINCREF(arg); |
| 4691 | tstate->c_profilefunc = NULL; |
| 4692 | tstate->c_profileobj = NULL; |
| 4693 | /* Must make sure that tracing is not ignored if 'temp' is freed */ |
| 4694 | tstate->use_tracing = tstate->c_tracefunc != NULL; |
| 4695 | Py_XDECREF(temp); |
| 4696 | tstate->c_profilefunc = func; |
| 4697 | tstate->c_profileobj = arg; |
| 4698 | /* Flag that tracing or profiling is turned on */ |
| 4699 | tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL); |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 4700 | } |
| 4701 | |
| 4702 | void |
| 4703 | PyEval_SetTrace(Py_tracefunc func, PyObject *arg) |
| 4704 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4705 | PyThreadState *tstate = PyThreadState_GET(); |
| 4706 | PyObject *temp = tstate->c_traceobj; |
| 4707 | _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL); |
| 4708 | Py_XINCREF(arg); |
| 4709 | tstate->c_tracefunc = NULL; |
| 4710 | tstate->c_traceobj = NULL; |
| 4711 | /* Must make sure that profiling is not ignored if 'temp' is freed */ |
| 4712 | tstate->use_tracing = tstate->c_profilefunc != NULL; |
| 4713 | Py_XDECREF(temp); |
| 4714 | tstate->c_tracefunc = func; |
| 4715 | tstate->c_traceobj = arg; |
| 4716 | /* Flag that tracing or profiling is turned on */ |
| 4717 | tstate->use_tracing = ((func != NULL) |
| 4718 | || (tstate->c_profilefunc != NULL)); |
Fred Drake | d083839 | 2001-06-16 21:02:31 +0000 | [diff] [blame] | 4719 | } |
| 4720 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4721 | void |
Yury Selivanov | d8cf382 | 2015-06-01 12:15:23 -0400 | [diff] [blame] | 4722 | _PyEval_SetCoroutineWrapper(PyObject *wrapper) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4723 | { |
| 4724 | PyThreadState *tstate = PyThreadState_GET(); |
| 4725 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4726 | Py_XINCREF(wrapper); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 4727 | Py_XSETREF(tstate->coroutine_wrapper, wrapper); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4728 | } |
| 4729 | |
| 4730 | PyObject * |
Yury Selivanov | d8cf382 | 2015-06-01 12:15:23 -0400 | [diff] [blame] | 4731 | _PyEval_GetCoroutineWrapper(void) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4732 | { |
| 4733 | PyThreadState *tstate = PyThreadState_GET(); |
| 4734 | return tstate->coroutine_wrapper; |
| 4735 | } |
| 4736 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4737 | void |
| 4738 | _PyEval_SetAsyncGenFirstiter(PyObject *firstiter) |
| 4739 | { |
| 4740 | PyThreadState *tstate = PyThreadState_GET(); |
| 4741 | |
| 4742 | Py_XINCREF(firstiter); |
| 4743 | Py_XSETREF(tstate->async_gen_firstiter, firstiter); |
| 4744 | } |
| 4745 | |
| 4746 | PyObject * |
| 4747 | _PyEval_GetAsyncGenFirstiter(void) |
| 4748 | { |
| 4749 | PyThreadState *tstate = PyThreadState_GET(); |
| 4750 | return tstate->async_gen_firstiter; |
| 4751 | } |
| 4752 | |
| 4753 | void |
| 4754 | _PyEval_SetAsyncGenFinalizer(PyObject *finalizer) |
| 4755 | { |
| 4756 | PyThreadState *tstate = PyThreadState_GET(); |
| 4757 | |
| 4758 | Py_XINCREF(finalizer); |
| 4759 | Py_XSETREF(tstate->async_gen_finalizer, finalizer); |
| 4760 | } |
| 4761 | |
| 4762 | PyObject * |
| 4763 | _PyEval_GetAsyncGenFinalizer(void) |
| 4764 | { |
| 4765 | PyThreadState *tstate = PyThreadState_GET(); |
| 4766 | return tstate->async_gen_finalizer; |
| 4767 | } |
| 4768 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4769 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4770 | PyEval_GetBuiltins(void) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 4771 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4772 | PyFrameObject *current_frame = PyEval_GetFrame(); |
| 4773 | if (current_frame == NULL) |
| 4774 | return PyThreadState_GET()->interp->builtins; |
| 4775 | else |
| 4776 | return current_frame->f_builtins; |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 4777 | } |
| 4778 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4779 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4780 | PyEval_GetLocals(void) |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 4781 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4782 | PyFrameObject *current_frame = PyEval_GetFrame(); |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 4783 | if (current_frame == NULL) { |
| 4784 | PyErr_SetString(PyExc_SystemError, "frame does not exist"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4785 | return NULL; |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 4786 | } |
| 4787 | |
| 4788 | if (PyFrame_FastToLocalsWithError(current_frame) < 0) |
| 4789 | return NULL; |
| 4790 | |
| 4791 | assert(current_frame->f_locals != NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4792 | return current_frame->f_locals; |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 4793 | } |
| 4794 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4795 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4796 | PyEval_GetGlobals(void) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4797 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4798 | PyFrameObject *current_frame = PyEval_GetFrame(); |
| 4799 | if (current_frame == NULL) |
| 4800 | return NULL; |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 4801 | |
| 4802 | assert(current_frame->f_globals != NULL); |
| 4803 | return current_frame->f_globals; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4804 | } |
| 4805 | |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 4806 | PyFrameObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4807 | PyEval_GetFrame(void) |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 4808 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4809 | PyThreadState *tstate = PyThreadState_GET(); |
| 4810 | return _PyThreadState_GetFrame(tstate); |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 4811 | } |
| 4812 | |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 4813 | int |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 4814 | PyEval_MergeCompilerFlags(PyCompilerFlags *cf) |
Jeremy Hylton | 061d106 | 2001-03-22 02:32:48 +0000 | [diff] [blame] | 4815 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4816 | PyFrameObject *current_frame = PyEval_GetFrame(); |
| 4817 | int result = cf->cf_flags != 0; |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 4818 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4819 | if (current_frame != NULL) { |
| 4820 | const int codeflags = current_frame->f_code->co_flags; |
| 4821 | const int compilerflags = codeflags & PyCF_MASK; |
| 4822 | if (compilerflags) { |
| 4823 | result = 1; |
| 4824 | cf->cf_flags |= compilerflags; |
| 4825 | } |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 4826 | #if 0 /* future keyword */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4827 | if (codeflags & CO_GENERATOR_ALLOWED) { |
| 4828 | result = 1; |
| 4829 | cf->cf_flags |= CO_GENERATOR_ALLOWED; |
| 4830 | } |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 4831 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4832 | } |
| 4833 | return result; |
Jeremy Hylton | 061d106 | 2001-03-22 02:32:48 +0000 | [diff] [blame] | 4834 | } |
| 4835 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4836 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4837 | /* External interface to call any callable object. |
Antoine Pitrou | 8689a10 | 2010-04-01 16:53:15 +0000 | [diff] [blame] | 4838 | 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] | 4839 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4840 | PyObject * |
Victor Stinner | 8a31c82 | 2016-08-19 17:12:23 +0200 | [diff] [blame] | 4841 | PyEval_CallObjectWithKeywords(PyObject *func, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4842 | { |
Victor Stinner | 59b356d | 2015-03-16 11:52:32 +0100 | [diff] [blame] | 4843 | #ifdef Py_DEBUG |
| 4844 | /* PyEval_CallObjectWithKeywords() must not be called with an exception |
| 4845 | set. It raises a new exception if parameters are invalid or if |
| 4846 | PyTuple_New() fails, and so the original exception is lost. */ |
| 4847 | assert(!PyErr_Occurred()); |
| 4848 | #endif |
| 4849 | |
Victor Stinner | 8a31c82 | 2016-08-19 17:12:23 +0200 | [diff] [blame] | 4850 | if (args == NULL) { |
Victor Stinner | 155ea65 | 2016-08-22 23:26:00 +0200 | [diff] [blame] | 4851 | return _PyObject_FastCallDict(func, NULL, 0, kwargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4852 | } |
Victor Stinner | 155ea65 | 2016-08-22 23:26:00 +0200 | [diff] [blame] | 4853 | |
| 4854 | if (!PyTuple_Check(args)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4855 | PyErr_SetString(PyExc_TypeError, |
| 4856 | "argument list must be a tuple"); |
| 4857 | return NULL; |
| 4858 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4859 | |
Victor Stinner | 8a31c82 | 2016-08-19 17:12:23 +0200 | [diff] [blame] | 4860 | if (kwargs != NULL && !PyDict_Check(kwargs)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4861 | PyErr_SetString(PyExc_TypeError, |
| 4862 | "keyword list must be a dictionary"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4863 | return NULL; |
| 4864 | } |
Guido van Rossum | e3e61c1 | 1995-08-04 04:14:47 +0000 | [diff] [blame] | 4865 | |
Victor Stinner | 6e2333d | 2016-08-23 00:25:01 +0200 | [diff] [blame] | 4866 | return PyObject_Call(func, args, kwargs); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4867 | } |
| 4868 | |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 4869 | const char * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4870 | PyEval_GetFuncName(PyObject *func) |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4871 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4872 | if (PyMethod_Check(func)) |
| 4873 | return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func)); |
| 4874 | else if (PyFunction_Check(func)) |
| 4875 | return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name); |
| 4876 | else if (PyCFunction_Check(func)) |
| 4877 | return ((PyCFunctionObject*)func)->m_ml->ml_name; |
| 4878 | else |
| 4879 | return func->ob_type->tp_name; |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4880 | } |
| 4881 | |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 4882 | const char * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4883 | PyEval_GetFuncDesc(PyObject *func) |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4884 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4885 | if (PyMethod_Check(func)) |
| 4886 | return "()"; |
| 4887 | else if (PyFunction_Check(func)) |
| 4888 | return "()"; |
| 4889 | else if (PyCFunction_Check(func)) |
| 4890 | return "()"; |
| 4891 | else |
| 4892 | return " object"; |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4893 | } |
| 4894 | |
Neal Norwitz | addfe0c | 2002-11-10 14:33:26 +0000 | [diff] [blame] | 4895 | static void |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4896 | err_args(PyObject *func, int flags, Py_ssize_t nargs) |
Jeremy Hylton | 192690e | 2002-08-16 18:36:11 +0000 | [diff] [blame] | 4897 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4898 | if (flags & METH_NOARGS) |
| 4899 | PyErr_Format(PyExc_TypeError, |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4900 | "%.200s() takes no arguments (%zd given)", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4901 | ((PyCFunctionObject *)func)->m_ml->ml_name, |
| 4902 | nargs); |
| 4903 | else |
| 4904 | PyErr_Format(PyExc_TypeError, |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4905 | "%.200s() takes exactly one argument (%zd given)", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4906 | ((PyCFunctionObject *)func)->m_ml->ml_name, |
| 4907 | nargs); |
Jeremy Hylton | 192690e | 2002-08-16 18:36:11 +0000 | [diff] [blame] | 4908 | } |
| 4909 | |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 4910 | #define C_TRACE(x, call) \ |
Nicholas Bastin | d858a77 | 2004-06-25 23:31:06 +0000 | [diff] [blame] | 4911 | if (tstate->use_tracing && tstate->c_profilefunc) { \ |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4912 | if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \ |
| 4913 | tstate, tstate->frame, \ |
| 4914 | PyTrace_C_CALL, func)) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4915 | x = NULL; \ |
| 4916 | } \ |
| 4917 | else { \ |
| 4918 | x = call; \ |
| 4919 | if (tstate->c_profilefunc != NULL) { \ |
| 4920 | if (x == NULL) { \ |
| 4921 | call_trace_protected(tstate->c_profilefunc, \ |
| 4922 | tstate->c_profileobj, \ |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4923 | tstate, tstate->frame, \ |
| 4924 | PyTrace_C_EXCEPTION, func); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4925 | /* XXX should pass (type, value, tb) */ \ |
| 4926 | } else { \ |
| 4927 | if (call_trace(tstate->c_profilefunc, \ |
| 4928 | tstate->c_profileobj, \ |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4929 | tstate, tstate->frame, \ |
| 4930 | PyTrace_C_RETURN, func)) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4931 | Py_DECREF(x); \ |
| 4932 | x = NULL; \ |
| 4933 | } \ |
| 4934 | } \ |
| 4935 | } \ |
| 4936 | } \ |
Nicholas Bastin | d858a77 | 2004-06-25 23:31:06 +0000 | [diff] [blame] | 4937 | } else { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4938 | x = call; \ |
| 4939 | } |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 4940 | |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 4941 | static PyObject * |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame^] | 4942 | call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 4943 | #ifdef WITH_TSC |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4944 | , uint64* pintr0, uint64* pintr1 |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 4945 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4946 | ) |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 4947 | { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4948 | PyObject **pfunc = (*pp_stack) - oparg - 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4949 | PyObject *func = *pfunc; |
| 4950 | PyObject *x, *w; |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame^] | 4951 | Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames); |
| 4952 | Py_ssize_t nargs = oparg - nkwargs; |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 4953 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4954 | /* Always dispatch PyCFunction first, because these are |
| 4955 | presumed to be the most frequent callable object. |
| 4956 | */ |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4957 | if (PyCFunction_Check(func)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4958 | int flags = PyCFunction_GET_FLAGS(func); |
| 4959 | PyThreadState *tstate = PyThreadState_GET(); |
Raymond Hettinger | a7f56bc | 2004-06-26 04:34:33 +0000 | [diff] [blame] | 4960 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4961 | PCALL(PCALL_CFUNCTION); |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame^] | 4962 | if (kwnames == NULL && flags & (METH_NOARGS | METH_O)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4963 | PyCFunction meth = PyCFunction_GET_FUNCTION(func); |
| 4964 | PyObject *self = PyCFunction_GET_SELF(func); |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4965 | if (flags & METH_NOARGS && nargs == 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4966 | C_TRACE(x, (*meth)(self,NULL)); |
Victor Stinner | 4a7cc88 | 2015-03-06 23:35:27 +0100 | [diff] [blame] | 4967 | |
Victor Stinner | efde146 | 2015-03-21 15:04:43 +0100 | [diff] [blame] | 4968 | x = _Py_CheckFunctionResult(func, x, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4969 | } |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4970 | else if (flags & METH_O && nargs == 1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4971 | PyObject *arg = EXT_POP(*pp_stack); |
| 4972 | C_TRACE(x, (*meth)(self,arg)); |
| 4973 | Py_DECREF(arg); |
Victor Stinner | 4a7cc88 | 2015-03-06 23:35:27 +0100 | [diff] [blame] | 4974 | |
Victor Stinner | efde146 | 2015-03-21 15:04:43 +0100 | [diff] [blame] | 4975 | x = _Py_CheckFunctionResult(func, x, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4976 | } |
| 4977 | else { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4978 | err_args(func, flags, nargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4979 | x = NULL; |
| 4980 | } |
| 4981 | } |
| 4982 | else { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4983 | PyObject *callargs, *kwdict = NULL; |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame^] | 4984 | if (kwnames != NULL) { |
| 4985 | kwdict = create_keyword_args(kwnames, pp_stack, func); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4986 | if (kwdict == NULL) { |
| 4987 | x = NULL; |
| 4988 | goto cfuncerror; |
| 4989 | } |
| 4990 | } |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4991 | callargs = load_args(pp_stack, nargs); |
Victor Stinner | 0ff0f54 | 2013-07-08 22:27:42 +0200 | [diff] [blame] | 4992 | if (callargs != NULL) { |
| 4993 | READ_TIMESTAMP(*pintr0); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4994 | C_TRACE(x, PyCFunction_Call(func, callargs, kwdict)); |
Victor Stinner | 0ff0f54 | 2013-07-08 22:27:42 +0200 | [diff] [blame] | 4995 | READ_TIMESTAMP(*pintr1); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4996 | Py_DECREF(callargs); |
Victor Stinner | 0ff0f54 | 2013-07-08 22:27:42 +0200 | [diff] [blame] | 4997 | } |
| 4998 | else { |
| 4999 | x = NULL; |
| 5000 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 5001 | Py_XDECREF(kwdict); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5002 | } |
Victor Stinner | 4a7cc88 | 2015-03-06 23:35:27 +0100 | [diff] [blame] | 5003 | } |
| 5004 | else { |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame^] | 5005 | Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames); |
| 5006 | PyObject **stack; |
| 5007 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 5008 | if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) { |
| 5009 | /* optimize access to bound methods */ |
| 5010 | PyObject *self = PyMethod_GET_SELF(func); |
| 5011 | PCALL(PCALL_METHOD); |
| 5012 | PCALL(PCALL_BOUND_METHOD); |
| 5013 | Py_INCREF(self); |
| 5014 | func = PyMethod_GET_FUNCTION(func); |
| 5015 | Py_INCREF(func); |
| 5016 | Py_SETREF(*pfunc, self); |
| 5017 | nargs++; |
| 5018 | } |
| 5019 | else { |
| 5020 | Py_INCREF(func); |
| 5021 | } |
Victor Stinner | 4a7cc88 | 2015-03-06 23:35:27 +0100 | [diff] [blame] | 5022 | |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame^] | 5023 | stack = (*pp_stack) - nargs - nkwargs; |
| 5024 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 5025 | READ_TIMESTAMP(*pintr0); |
| 5026 | if (PyFunction_Check(func)) { |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame^] | 5027 | x = fast_function(func, stack, nargs, kwnames); |
| 5028 | } |
| 5029 | else { |
| 5030 | x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 5031 | } |
| 5032 | READ_TIMESTAMP(*pintr1); |
| 5033 | |
| 5034 | Py_DECREF(func); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5035 | } |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 5036 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 5037 | cfuncerror: |
| 5038 | assert((x != NULL) ^ (PyErr_Occurred() != NULL)); |
| 5039 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5040 | /* Clear the stack of the function object. Also removes |
| 5041 | the arguments in case they weren't consumed already |
Victor Stinner | e90bdb1 | 2016-08-25 23:26:50 +0200 | [diff] [blame] | 5042 | (fast_function() and err_args() leave them on the stack). |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5043 | */ |
| 5044 | while ((*pp_stack) > pfunc) { |
| 5045 | w = EXT_POP(*pp_stack); |
| 5046 | Py_DECREF(w); |
| 5047 | PCALL(PCALL_POP); |
| 5048 | } |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 5049 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5050 | return x; |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 5051 | } |
| 5052 | |
Victor Stinner | e90bdb1 | 2016-08-25 23:26:50 +0200 | [diff] [blame] | 5053 | /* The fast_function() function optimize calls for which no argument |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 5054 | tuple is necessary; the objects are passed directly from the stack. |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 5055 | For the simplest case -- a function that takes only positional |
| 5056 | arguments and is called with only positional arguments -- it |
| 5057 | inlines the most primitive frame setup code from |
| 5058 | PyEval_EvalCodeEx(), which vastly reduces the checks that must be |
| 5059 | done before evaluating the frame. |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 5060 | */ |
| 5061 | |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 5062 | static PyObject* |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame^] | 5063 | _PyFunction_FastCall(PyCodeObject *co, PyObject **args, Py_ssize_t nargs, |
| 5064 | PyObject *globals) |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 5065 | { |
| 5066 | PyFrameObject *f; |
| 5067 | PyThreadState *tstate = PyThreadState_GET(); |
| 5068 | PyObject **fastlocals; |
| 5069 | Py_ssize_t i; |
| 5070 | PyObject *result; |
| 5071 | |
| 5072 | PCALL(PCALL_FASTER_FUNCTION); |
| 5073 | assert(globals != NULL); |
| 5074 | /* XXX Perhaps we should create a specialized |
| 5075 | PyFrame_New() that doesn't take locals, but does |
| 5076 | take builtins without sanity checking them. |
| 5077 | */ |
| 5078 | assert(tstate != NULL); |
| 5079 | f = PyFrame_New(tstate, co, globals, NULL); |
| 5080 | if (f == NULL) { |
| 5081 | return NULL; |
| 5082 | } |
| 5083 | |
| 5084 | fastlocals = f->f_localsplus; |
| 5085 | |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 5086 | for (i = 0; i < nargs; i++) { |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 5087 | Py_INCREF(*args); |
| 5088 | fastlocals[i] = *args++; |
| 5089 | } |
| 5090 | result = PyEval_EvalFrameEx(f,0); |
| 5091 | |
| 5092 | ++tstate->recursion_depth; |
| 5093 | Py_DECREF(f); |
| 5094 | --tstate->recursion_depth; |
| 5095 | |
| 5096 | return result; |
| 5097 | } |
| 5098 | |
Victor Stinner | e90bdb1 | 2016-08-25 23:26:50 +0200 | [diff] [blame] | 5099 | static PyObject * |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame^] | 5100 | fast_function(PyObject *func, PyObject **stack, |
| 5101 | Py_ssize_t nargs, PyObject *kwnames) |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 5102 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5103 | PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func); |
| 5104 | PyObject *globals = PyFunction_GET_GLOBALS(func); |
| 5105 | PyObject *argdefs = PyFunction_GET_DEFAULTS(func); |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 5106 | PyObject *kwdefs, *closure, *name, *qualname; |
| 5107 | PyObject **d; |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame^] | 5108 | Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 5109 | Py_ssize_t nd; |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame^] | 5110 | |
| 5111 | assert((nargs == 0 && nkwargs == 0) || stack != NULL); |
Victor Stinner | 577e1f8 | 2016-08-25 00:29:32 +0200 | [diff] [blame] | 5112 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5113 | PCALL(PCALL_FUNCTION); |
| 5114 | PCALL(PCALL_FAST_FUNCTION); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 5115 | |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 5116 | if (co->co_kwonlyargcount == 0 && nkwargs == 0 && |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 5117 | co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) |
| 5118 | { |
Victor Stinner | 2eedc11 | 2016-08-22 12:29:42 +0200 | [diff] [blame] | 5119 | if (argdefs == NULL && co->co_argcount == nargs) { |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame^] | 5120 | return _PyFunction_FastCall(co, stack, nargs, globals); |
Victor Stinner | 2eedc11 | 2016-08-22 12:29:42 +0200 | [diff] [blame] | 5121 | } |
| 5122 | else if (nargs == 0 && argdefs != NULL |
| 5123 | && co->co_argcount == Py_SIZE(argdefs)) { |
| 5124 | /* function called with no arguments, but all parameters have |
| 5125 | a default value: use default values as arguments .*/ |
| 5126 | stack = &PyTuple_GET_ITEM(argdefs, 0); |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame^] | 5127 | return _PyFunction_FastCall(co, stack, Py_SIZE(argdefs), globals); |
Victor Stinner | 2eedc11 | 2016-08-22 12:29:42 +0200 | [diff] [blame] | 5128 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5129 | } |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 5130 | |
| 5131 | kwdefs = PyFunction_GET_KW_DEFAULTS(func); |
| 5132 | closure = PyFunction_GET_CLOSURE(func); |
| 5133 | name = ((PyFunctionObject *)func) -> func_name; |
| 5134 | qualname = ((PyFunctionObject *)func) -> func_qualname; |
| 5135 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5136 | if (argdefs != NULL) { |
| 5137 | d = &PyTuple_GET_ITEM(argdefs, 0); |
| 5138 | nd = Py_SIZE(argdefs); |
| 5139 | } |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 5140 | else { |
| 5141 | d = NULL; |
| 5142 | nd = 0; |
| 5143 | } |
| 5144 | return _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL, |
Victor Stinner | 2eedc11 | 2016-08-22 12:29:42 +0200 | [diff] [blame] | 5145 | stack, nargs, |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 5146 | NULL, 0, |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame^] | 5147 | kwnames, stack + nargs, |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 5148 | d, (int)nd, kwdefs, |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 5149 | closure, name, qualname); |
| 5150 | } |
| 5151 | |
| 5152 | PyObject * |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame^] | 5153 | _PyFunction_FastCallKeywords(PyObject *func, PyObject **stack, |
| 5154 | Py_ssize_t nargs, PyObject *kwnames) |
| 5155 | { |
| 5156 | return fast_function(func, stack, nargs, kwnames); |
| 5157 | } |
| 5158 | |
| 5159 | PyObject * |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 5160 | _PyFunction_FastCallDict(PyObject *func, PyObject **args, Py_ssize_t nargs, |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 5161 | PyObject *kwargs) |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 5162 | { |
| 5163 | PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func); |
| 5164 | PyObject *globals = PyFunction_GET_GLOBALS(func); |
| 5165 | PyObject *argdefs = PyFunction_GET_DEFAULTS(func); |
| 5166 | PyObject *kwdefs, *closure, *name, *qualname; |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 5167 | PyObject *kwtuple, **k; |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 5168 | PyObject **d; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 5169 | Py_ssize_t nd, nk; |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 5170 | PyObject *result; |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 5171 | |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 5172 | assert(func != NULL); |
| 5173 | assert(nargs >= 0); |
| 5174 | assert(nargs == 0 || args != NULL); |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 5175 | assert(kwargs == NULL || PyDict_Check(kwargs)); |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 5176 | |
Victor Stinner | 577e1f8 | 2016-08-25 00:29:32 +0200 | [diff] [blame] | 5177 | PCALL(PCALL_FUNCTION); |
| 5178 | PCALL(PCALL_FAST_FUNCTION); |
| 5179 | |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 5180 | if (co->co_kwonlyargcount == 0 && |
| 5181 | (kwargs == NULL || PyDict_Size(kwargs) == 0) && |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 5182 | co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) |
| 5183 | { |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 5184 | /* Fast paths */ |
Victor Stinner | 2eedc11 | 2016-08-22 12:29:42 +0200 | [diff] [blame] | 5185 | if (argdefs == NULL && co->co_argcount == nargs) { |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame^] | 5186 | return _PyFunction_FastCall(co, args, nargs, globals); |
Victor Stinner | 2eedc11 | 2016-08-22 12:29:42 +0200 | [diff] [blame] | 5187 | } |
| 5188 | else if (nargs == 0 && argdefs != NULL |
| 5189 | && co->co_argcount == Py_SIZE(argdefs)) { |
| 5190 | /* function called with no arguments, but all parameters have |
| 5191 | a default value: use default values as arguments .*/ |
| 5192 | args = &PyTuple_GET_ITEM(argdefs, 0); |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame^] | 5193 | return _PyFunction_FastCall(co, args, Py_SIZE(argdefs), globals); |
Victor Stinner | 2eedc11 | 2016-08-22 12:29:42 +0200 | [diff] [blame] | 5194 | } |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 5195 | } |
| 5196 | |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 5197 | if (kwargs != NULL) { |
| 5198 | Py_ssize_t pos, i; |
| 5199 | nk = PyDict_Size(kwargs); |
| 5200 | |
| 5201 | kwtuple = PyTuple_New(2 * nk); |
| 5202 | if (kwtuple == NULL) { |
| 5203 | return NULL; |
| 5204 | } |
| 5205 | |
| 5206 | k = &PyTuple_GET_ITEM(kwtuple, 0); |
| 5207 | pos = i = 0; |
| 5208 | while (PyDict_Next(kwargs, &pos, &k[i], &k[i+1])) { |
| 5209 | Py_INCREF(k[i]); |
| 5210 | Py_INCREF(k[i+1]); |
| 5211 | i += 2; |
| 5212 | } |
| 5213 | nk = i / 2; |
| 5214 | } |
| 5215 | else { |
| 5216 | kwtuple = NULL; |
| 5217 | k = NULL; |
| 5218 | nk = 0; |
| 5219 | } |
| 5220 | |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 5221 | kwdefs = PyFunction_GET_KW_DEFAULTS(func); |
| 5222 | closure = PyFunction_GET_CLOSURE(func); |
| 5223 | name = ((PyFunctionObject *)func) -> func_name; |
| 5224 | qualname = ((PyFunctionObject *)func) -> func_qualname; |
| 5225 | |
| 5226 | if (argdefs != NULL) { |
| 5227 | d = &PyTuple_GET_ITEM(argdefs, 0); |
| 5228 | nd = Py_SIZE(argdefs); |
| 5229 | } |
| 5230 | else { |
| 5231 | d = NULL; |
| 5232 | nd = 0; |
| 5233 | } |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 5234 | |
| 5235 | result = _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL, |
| 5236 | args, nargs, |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 5237 | k, nk, |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 5238 | NULL, NULL, |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 5239 | d, nd, kwdefs, |
| 5240 | closure, name, qualname); |
| 5241 | Py_XDECREF(kwtuple); |
| 5242 | return result; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 5243 | } |
| 5244 | |
| 5245 | static PyObject * |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 5246 | create_keyword_args(PyObject *names, PyObject ***pp_stack, |
Ka-Ping Yee | 2057970 | 2001-01-15 22:14:16 +0000 | [diff] [blame] | 5247 | PyObject *func) |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 5248 | { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 5249 | Py_ssize_t nk = PyTuple_GET_SIZE(names); |
| 5250 | PyObject *kwdict = _PyDict_NewPresized(nk); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5251 | if (kwdict == NULL) |
| 5252 | return NULL; |
| 5253 | while (--nk >= 0) { |
| 5254 | int err; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 5255 | PyObject *key = PyTuple_GET_ITEM(names, nk); |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame^] | 5256 | PyObject *value = EXT_POP(*pp_stack); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5257 | if (PyDict_GetItem(kwdict, key) != NULL) { |
| 5258 | PyErr_Format(PyExc_TypeError, |
| 5259 | "%.200s%s got multiple values " |
| 5260 | "for keyword argument '%U'", |
| 5261 | PyEval_GetFuncName(func), |
| 5262 | PyEval_GetFuncDesc(func), |
| 5263 | key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5264 | Py_DECREF(value); |
| 5265 | Py_DECREF(kwdict); |
| 5266 | return NULL; |
| 5267 | } |
| 5268 | err = PyDict_SetItem(kwdict, key, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5269 | Py_DECREF(value); |
| 5270 | if (err) { |
| 5271 | Py_DECREF(kwdict); |
| 5272 | return NULL; |
| 5273 | } |
| 5274 | } |
| 5275 | return kwdict; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 5276 | } |
| 5277 | |
| 5278 | static PyObject * |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 5279 | load_args(PyObject ***pp_stack, Py_ssize_t nargs) |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 5280 | { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 5281 | PyObject *args = PyTuple_New(nargs); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 5282 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 5283 | if (args == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5284 | return NULL; |
| 5285 | } |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 5286 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 5287 | while (--nargs >= 0) { |
| 5288 | PyObject *arg= EXT_POP(*pp_stack); |
| 5289 | PyTuple_SET_ITEM(args, nargs, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5290 | } |
| 5291 | return args; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 5292 | } |
| 5293 | |
| 5294 | static PyObject * |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 5295 | do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict) |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 5296 | { |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 5297 | #ifdef CALL_PROFILE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5298 | /* At this point, we have to look at the type of func to |
| 5299 | update the call stats properly. Do it here so as to avoid |
| 5300 | exposing the call stats machinery outside ceval.c |
| 5301 | */ |
| 5302 | if (PyFunction_Check(func)) |
| 5303 | PCALL(PCALL_FUNCTION); |
| 5304 | else if (PyMethod_Check(func)) |
| 5305 | PCALL(PCALL_METHOD); |
| 5306 | else if (PyType_Check(func)) |
| 5307 | PCALL(PCALL_TYPE); |
| 5308 | else if (PyCFunction_Check(func)) |
| 5309 | PCALL(PCALL_CFUNCTION); |
| 5310 | else |
| 5311 | PCALL(PCALL_OTHER); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 5312 | #endif |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 5313 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5314 | if (PyCFunction_Check(func)) { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 5315 | PyObject *result; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5316 | PyThreadState *tstate = PyThreadState_GET(); |
| 5317 | C_TRACE(result, PyCFunction_Call(func, callargs, kwdict)); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 5318 | return result; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5319 | } |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 5320 | else { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 5321 | return PyObject_Call(func, callargs, kwdict); |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 5322 | } |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 5323 | } |
| 5324 | |
Serhiy Storchaka | 483405b | 2015-02-17 10:14:30 +0200 | [diff] [blame] | 5325 | /* 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] | 5326 | nb_index slot defined, and store in *pi. |
| 5327 | Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX, |
| 5328 | 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] | 5329 | Return 0 on error, 1 on success. |
Tim Peters | cb479e7 | 2001-12-16 19:11:44 +0000 | [diff] [blame] | 5330 | */ |
Tim Peters | b519638 | 2001-12-16 19:44:20 +0000 | [diff] [blame] | 5331 | /* Note: If v is NULL, return success without storing into *pi. This |
| 5332 | is because_PyEval_SliceIndex() is called by apply_slice(), which can be |
| 5333 | 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] | 5334 | */ |
Guido van Rossum | 20c6add | 2000-05-08 14:06:50 +0000 | [diff] [blame] | 5335 | int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5336 | _PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5337 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5338 | if (v != NULL) { |
| 5339 | Py_ssize_t x; |
| 5340 | if (PyIndex_Check(v)) { |
| 5341 | x = PyNumber_AsSsize_t(v, NULL); |
| 5342 | if (x == -1 && PyErr_Occurred()) |
| 5343 | return 0; |
| 5344 | } |
| 5345 | else { |
| 5346 | PyErr_SetString(PyExc_TypeError, |
| 5347 | "slice indices must be integers or " |
| 5348 | "None or have an __index__ method"); |
| 5349 | return 0; |
| 5350 | } |
| 5351 | *pi = x; |
| 5352 | } |
| 5353 | return 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5354 | } |
| 5355 | |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 5356 | #define CANNOT_CATCH_MSG "catching classes that do not inherit from "\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5357 | "BaseException is not allowed" |
Brett Cannon | f74225d | 2007-02-26 21:10:16 +0000 | [diff] [blame] | 5358 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 5359 | static PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 5360 | cmp_outcome(int op, PyObject *v, PyObject *w) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5361 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5362 | int res = 0; |
| 5363 | switch (op) { |
| 5364 | case PyCmp_IS: |
| 5365 | res = (v == w); |
| 5366 | break; |
| 5367 | case PyCmp_IS_NOT: |
| 5368 | res = (v != w); |
| 5369 | break; |
| 5370 | case PyCmp_IN: |
| 5371 | res = PySequence_Contains(w, v); |
| 5372 | if (res < 0) |
| 5373 | return NULL; |
| 5374 | break; |
| 5375 | case PyCmp_NOT_IN: |
| 5376 | res = PySequence_Contains(w, v); |
| 5377 | if (res < 0) |
| 5378 | return NULL; |
| 5379 | res = !res; |
| 5380 | break; |
| 5381 | case PyCmp_EXC_MATCH: |
| 5382 | if (PyTuple_Check(w)) { |
| 5383 | Py_ssize_t i, length; |
| 5384 | length = PyTuple_Size(w); |
| 5385 | for (i = 0; i < length; i += 1) { |
| 5386 | PyObject *exc = PyTuple_GET_ITEM(w, i); |
| 5387 | if (!PyExceptionClass_Check(exc)) { |
| 5388 | PyErr_SetString(PyExc_TypeError, |
| 5389 | CANNOT_CATCH_MSG); |
| 5390 | return NULL; |
| 5391 | } |
| 5392 | } |
| 5393 | } |
| 5394 | else { |
| 5395 | if (!PyExceptionClass_Check(w)) { |
| 5396 | PyErr_SetString(PyExc_TypeError, |
| 5397 | CANNOT_CATCH_MSG); |
| 5398 | return NULL; |
| 5399 | } |
| 5400 | } |
| 5401 | res = PyErr_GivenExceptionMatches(v, w); |
| 5402 | break; |
| 5403 | default: |
| 5404 | return PyObject_RichCompare(v, w, op); |
| 5405 | } |
| 5406 | v = res ? Py_True : Py_False; |
| 5407 | Py_INCREF(v); |
| 5408 | return v; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5409 | } |
| 5410 | |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 5411 | static PyObject * |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 5412 | import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level) |
| 5413 | { |
| 5414 | _Py_IDENTIFIER(__import__); |
Victor Stinner | df142fd | 2016-08-20 00:44:42 +0200 | [diff] [blame] | 5415 | PyObject *import_func, *res; |
| 5416 | PyObject* stack[5]; |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 5417 | |
| 5418 | import_func = _PyDict_GetItemId(f->f_builtins, &PyId___import__); |
| 5419 | if (import_func == NULL) { |
| 5420 | PyErr_SetString(PyExc_ImportError, "__import__ not found"); |
| 5421 | return NULL; |
| 5422 | } |
| 5423 | |
| 5424 | /* Fast path for not overloaded __import__. */ |
| 5425 | if (import_func == PyThreadState_GET()->interp->import_func) { |
| 5426 | int ilevel = _PyLong_AsInt(level); |
| 5427 | if (ilevel == -1 && PyErr_Occurred()) { |
| 5428 | return NULL; |
| 5429 | } |
| 5430 | res = PyImport_ImportModuleLevelObject( |
| 5431 | name, |
| 5432 | f->f_globals, |
| 5433 | f->f_locals == NULL ? Py_None : f->f_locals, |
| 5434 | fromlist, |
| 5435 | ilevel); |
| 5436 | return res; |
| 5437 | } |
| 5438 | |
| 5439 | Py_INCREF(import_func); |
Victor Stinner | df142fd | 2016-08-20 00:44:42 +0200 | [diff] [blame] | 5440 | |
| 5441 | stack[0] = name; |
| 5442 | stack[1] = f->f_globals; |
| 5443 | stack[2] = f->f_locals == NULL ? Py_None : f->f_locals; |
| 5444 | stack[3] = fromlist; |
| 5445 | stack[4] = level; |
Victor Stinner | 559bb6a | 2016-08-22 22:48:54 +0200 | [diff] [blame] | 5446 | res = _PyObject_FastCall(import_func, stack, 5); |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 5447 | Py_DECREF(import_func); |
| 5448 | return res; |
| 5449 | } |
| 5450 | |
| 5451 | static PyObject * |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 5452 | import_from(PyObject *v, PyObject *name) |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 5453 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5454 | PyObject *x; |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5455 | _Py_IDENTIFIER(__name__); |
| 5456 | PyObject *fullmodname, *pkgname; |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 5457 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5458 | x = PyObject_GetAttr(v, name); |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5459 | if (x != NULL || !PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 5460 | return x; |
| 5461 | /* Issue #17636: in case this failed because of a circular relative |
| 5462 | import, try to fallback on reading the module directly from |
| 5463 | sys.modules. */ |
| 5464 | PyErr_Clear(); |
| 5465 | pkgname = _PyObject_GetAttrId(v, &PyId___name__); |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5466 | if (pkgname == NULL) { |
| 5467 | goto error; |
| 5468 | } |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5469 | fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name); |
| 5470 | Py_DECREF(pkgname); |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5471 | if (fullmodname == NULL) { |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5472 | return NULL; |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5473 | } |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5474 | x = PyDict_GetItem(PyImport_GetModuleDict(), fullmodname); |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5475 | Py_DECREF(fullmodname); |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5476 | if (x == NULL) { |
| 5477 | goto error; |
| 5478 | } |
| 5479 | Py_INCREF(x); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5480 | return x; |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5481 | error: |
| 5482 | PyErr_Format(PyExc_ImportError, "cannot import name %R", name); |
| 5483 | return NULL; |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 5484 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 5485 | |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 5486 | static int |
| 5487 | import_all_from(PyObject *locals, PyObject *v) |
| 5488 | { |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 5489 | _Py_IDENTIFIER(__all__); |
| 5490 | _Py_IDENTIFIER(__dict__); |
| 5491 | PyObject *all = _PyObject_GetAttrId(v, &PyId___all__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5492 | PyObject *dict, *name, *value; |
| 5493 | int skip_leading_underscores = 0; |
| 5494 | int pos, err; |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 5495 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5496 | if (all == NULL) { |
| 5497 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 5498 | return -1; /* Unexpected error */ |
| 5499 | PyErr_Clear(); |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 5500 | dict = _PyObject_GetAttrId(v, &PyId___dict__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5501 | if (dict == NULL) { |
| 5502 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 5503 | return -1; |
| 5504 | PyErr_SetString(PyExc_ImportError, |
| 5505 | "from-import-* object has no __dict__ and no __all__"); |
| 5506 | return -1; |
| 5507 | } |
| 5508 | all = PyMapping_Keys(dict); |
| 5509 | Py_DECREF(dict); |
| 5510 | if (all == NULL) |
| 5511 | return -1; |
| 5512 | skip_leading_underscores = 1; |
| 5513 | } |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 5514 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5515 | for (pos = 0, err = 0; ; pos++) { |
| 5516 | name = PySequence_GetItem(all, pos); |
| 5517 | if (name == NULL) { |
| 5518 | if (!PyErr_ExceptionMatches(PyExc_IndexError)) |
| 5519 | err = -1; |
| 5520 | else |
| 5521 | PyErr_Clear(); |
| 5522 | break; |
| 5523 | } |
| 5524 | if (skip_leading_underscores && |
| 5525 | PyUnicode_Check(name) && |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5526 | PyUnicode_READY(name) != -1 && |
| 5527 | PyUnicode_READ_CHAR(name, 0) == '_') |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5528 | { |
| 5529 | Py_DECREF(name); |
| 5530 | continue; |
| 5531 | } |
| 5532 | value = PyObject_GetAttr(v, name); |
| 5533 | if (value == NULL) |
| 5534 | err = -1; |
| 5535 | else if (PyDict_CheckExact(locals)) |
| 5536 | err = PyDict_SetItem(locals, name, value); |
| 5537 | else |
| 5538 | err = PyObject_SetItem(locals, name, value); |
| 5539 | Py_DECREF(name); |
| 5540 | Py_XDECREF(value); |
| 5541 | if (err != 0) |
| 5542 | break; |
| 5543 | } |
| 5544 | Py_DECREF(all); |
| 5545 | return err; |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 5546 | } |
| 5547 | |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 5548 | static void |
Neal Norwitz | da059e3 | 2007-08-26 05:33:45 +0000 | [diff] [blame] | 5549 | format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj) |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5550 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5551 | const char *obj_str; |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5552 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5553 | if (!obj) |
| 5554 | return; |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5555 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5556 | obj_str = _PyUnicode_AsString(obj); |
| 5557 | if (!obj_str) |
| 5558 | return; |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5559 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5560 | PyErr_Format(exc, format_str, obj_str); |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5561 | } |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5562 | |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 5563 | static void |
| 5564 | format_exc_unbound(PyCodeObject *co, int oparg) |
| 5565 | { |
| 5566 | PyObject *name; |
| 5567 | /* Don't stomp existing exception */ |
| 5568 | if (PyErr_Occurred()) |
| 5569 | return; |
| 5570 | if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) { |
| 5571 | name = PyTuple_GET_ITEM(co->co_cellvars, |
| 5572 | oparg); |
| 5573 | format_exc_check_arg( |
| 5574 | PyExc_UnboundLocalError, |
| 5575 | UNBOUNDLOCAL_ERROR_MSG, |
| 5576 | name); |
| 5577 | } else { |
| 5578 | name = PyTuple_GET_ITEM(co->co_freevars, oparg - |
| 5579 | PyTuple_GET_SIZE(co->co_cellvars)); |
| 5580 | format_exc_check_arg(PyExc_NameError, |
| 5581 | UNBOUNDFREE_ERROR_MSG, name); |
| 5582 | } |
| 5583 | } |
| 5584 | |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5585 | static PyObject * |
| 5586 | unicode_concatenate(PyObject *v, PyObject *w, |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 5587 | PyFrameObject *f, const unsigned short *next_instr) |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5588 | { |
| 5589 | PyObject *res; |
| 5590 | if (Py_REFCNT(v) == 2) { |
| 5591 | /* In the common case, there are 2 references to the value |
| 5592 | * stored in 'variable' when the += is performed: one on the |
| 5593 | * value stack (in 'v') and one still stored in the |
| 5594 | * 'variable'. We try to delete the variable now to reduce |
| 5595 | * the refcnt to 1. |
| 5596 | */ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 5597 | int opcode, oparg; |
| 5598 | NEXTOPARG(); |
| 5599 | switch (opcode) { |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5600 | case STORE_FAST: |
| 5601 | { |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5602 | PyObject **fastlocals = f->f_localsplus; |
| 5603 | if (GETLOCAL(oparg) == v) |
| 5604 | SETLOCAL(oparg, NULL); |
| 5605 | break; |
| 5606 | } |
| 5607 | case STORE_DEREF: |
| 5608 | { |
| 5609 | PyObject **freevars = (f->f_localsplus + |
| 5610 | f->f_code->co_nlocals); |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 5611 | PyObject *c = freevars[oparg]; |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5612 | if (PyCell_GET(c) == v) |
| 5613 | PyCell_Set(c, NULL); |
| 5614 | break; |
| 5615 | } |
| 5616 | case STORE_NAME: |
| 5617 | { |
| 5618 | PyObject *names = f->f_code->co_names; |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 5619 | PyObject *name = GETITEM(names, oparg); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5620 | PyObject *locals = f->f_locals; |
| 5621 | if (PyDict_CheckExact(locals) && |
| 5622 | PyDict_GetItem(locals, name) == v) { |
| 5623 | if (PyDict_DelItem(locals, name) != 0) { |
| 5624 | PyErr_Clear(); |
| 5625 | } |
| 5626 | } |
| 5627 | break; |
| 5628 | } |
| 5629 | } |
| 5630 | } |
| 5631 | res = v; |
| 5632 | PyUnicode_Append(&res, w); |
| 5633 | return res; |
| 5634 | } |
| 5635 | |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5636 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 5637 | |
Skip Montanaro | f118cb1 | 2001-10-15 20:51:38 +0000 | [diff] [blame] | 5638 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 5639 | getarray(long a[256]) |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5640 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5641 | int i; |
| 5642 | PyObject *l = PyList_New(256); |
| 5643 | if (l == NULL) return NULL; |
| 5644 | for (i = 0; i < 256; i++) { |
| 5645 | PyObject *x = PyLong_FromLong(a[i]); |
| 5646 | if (x == NULL) { |
| 5647 | Py_DECREF(l); |
| 5648 | return NULL; |
| 5649 | } |
| 5650 | PyList_SetItem(l, i, x); |
| 5651 | } |
| 5652 | for (i = 0; i < 256; i++) |
| 5653 | a[i] = 0; |
| 5654 | return l; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5655 | } |
| 5656 | |
| 5657 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 5658 | _Py_GetDXProfile(PyObject *self, PyObject *args) |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5659 | { |
| 5660 | #ifndef DXPAIRS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5661 | return getarray(dxp); |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5662 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5663 | int i; |
| 5664 | PyObject *l = PyList_New(257); |
| 5665 | if (l == NULL) return NULL; |
| 5666 | for (i = 0; i < 257; i++) { |
| 5667 | PyObject *x = getarray(dxpairs[i]); |
| 5668 | if (x == NULL) { |
| 5669 | Py_DECREF(l); |
| 5670 | return NULL; |
| 5671 | } |
| 5672 | PyList_SetItem(l, i, x); |
| 5673 | } |
| 5674 | return l; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5675 | #endif |
| 5676 | } |
| 5677 | |
| 5678 | #endif |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 5679 | |
| 5680 | Py_ssize_t |
| 5681 | _PyEval_RequestCodeExtraIndex(freefunc free) |
| 5682 | { |
| 5683 | PyThreadState *tstate = PyThreadState_Get(); |
| 5684 | Py_ssize_t new_index; |
| 5685 | |
| 5686 | if (tstate->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) { |
| 5687 | return -1; |
| 5688 | } |
| 5689 | new_index = tstate->co_extra_user_count++; |
| 5690 | tstate->co_extra_freefuncs[new_index] = free; |
| 5691 | return new_index; |
| 5692 | } |