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