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); |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 139 | static PyObject * special_lookup(PyObject *, char *, PyObject **); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 140 | |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 141 | #define NAME_ERROR_MSG \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 142 | "name '%.200s' is not defined" |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 143 | #define GLOBAL_NAME_ERROR_MSG \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 144 | "global 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 | |
| 365 | /* This function is called from PyOS_AfterFork to ensure that newly |
| 366 | created child processes don't hold locks referring to threads which |
| 367 | are not running in the child process. (This could also be done using |
| 368 | pthread_atfork mechanism, at least for the pthreads implementation.) */ |
| 369 | |
| 370 | void |
| 371 | PyEval_ReInitThreads(void) |
| 372 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 373 | PyObject *threading, *result; |
| 374 | PyThreadState *tstate = PyThreadState_GET(); |
Jesse Noller | a851397 | 2008-07-17 16:49:17 +0000 | [diff] [blame] | 375 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 376 | if (!gil_created()) |
| 377 | return; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 378 | recreate_gil(); |
| 379 | pending_lock = PyThread_allocate_lock(); |
| 380 | take_gil(tstate); |
| 381 | main_thread = PyThread_get_thread_ident(); |
Jesse Noller | a851397 | 2008-07-17 16:49:17 +0000 | [diff] [blame] | 382 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 383 | /* Update the threading module with the new state. |
| 384 | */ |
| 385 | tstate = PyThreadState_GET(); |
| 386 | threading = PyMapping_GetItemString(tstate->interp->modules, |
| 387 | "threading"); |
| 388 | if (threading == NULL) { |
| 389 | /* threading not imported */ |
| 390 | PyErr_Clear(); |
| 391 | return; |
| 392 | } |
| 393 | result = PyObject_CallMethod(threading, "_after_fork", NULL); |
| 394 | if (result == NULL) |
| 395 | PyErr_WriteUnraisable(threading); |
| 396 | else |
| 397 | Py_DECREF(result); |
| 398 | Py_DECREF(threading); |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 399 | } |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 400 | |
| 401 | #else |
Jeffrey Yasskin | 3937083 | 2010-05-03 19:29:34 +0000 | [diff] [blame] | 402 | static _Py_atomic_int eval_breaker = {0}; |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 403 | static int pending_async_exc = 0; |
| 404 | #endif /* WITH_THREAD */ |
| 405 | |
| 406 | /* This function is used to signal that async exceptions are waiting to be |
| 407 | raised, therefore it is also useful in non-threaded builds. */ |
| 408 | |
| 409 | void |
| 410 | _PyEval_SignalAsyncExc(void) |
| 411 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 412 | SIGNAL_ASYNC_EXC(); |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 413 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 414 | |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 415 | /* Functions save_thread and restore_thread are always defined so |
| 416 | dynamically loaded modules needn't be compiled separately for use |
| 417 | with and without threads: */ |
| 418 | |
Guido van Rossum | 2fca21f7 | 1997-07-18 23:56:58 +0000 | [diff] [blame] | 419 | PyThreadState * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 420 | PyEval_SaveThread(void) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 421 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 422 | PyThreadState *tstate = PyThreadState_Swap(NULL); |
| 423 | if (tstate == NULL) |
| 424 | Py_FatalError("PyEval_SaveThread: NULL tstate"); |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 425 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 426 | if (gil_created()) |
| 427 | drop_gil(tstate); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 428 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 429 | return tstate; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 433 | PyEval_RestoreThread(PyThreadState *tstate) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 434 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 435 | if (tstate == NULL) |
| 436 | Py_FatalError("PyEval_RestoreThread: NULL tstate"); |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 437 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 438 | if (gil_created()) { |
| 439 | int err = errno; |
| 440 | take_gil(tstate); |
Antoine Pitrou | 0d5e52d | 2011-05-04 20:02:30 +0200 | [diff] [blame] | 441 | /* _Py_Finalizing is protected by the GIL */ |
| 442 | if (_Py_Finalizing && tstate != _Py_Finalizing) { |
| 443 | drop_gil(tstate); |
| 444 | PyThread_exit_thread(); |
| 445 | assert(0); /* unreachable */ |
| 446 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 447 | errno = err; |
| 448 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 449 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 450 | PyThreadState_Swap(tstate); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 454 | /* Mechanism whereby asynchronously executing callbacks (e.g. UNIX |
| 455 | signal handlers or Mac I/O completion routines) can schedule calls |
| 456 | to a function to be called synchronously. |
| 457 | The synchronous function is called with one void* argument. |
| 458 | It should return 0 for success or -1 for failure -- failure should |
| 459 | be accompanied by an exception. |
| 460 | |
| 461 | If registry succeeds, the registry function returns 0; if it fails |
| 462 | (e.g. due to too many pending calls) it returns -1 (without setting |
| 463 | an exception condition). |
| 464 | |
| 465 | Note that because registry may occur from within signal handlers, |
| 466 | or other asynchronous events, calling malloc() is unsafe! |
| 467 | |
| 468 | #ifdef WITH_THREAD |
| 469 | Any thread can schedule pending calls, but only the main thread |
| 470 | will execute them. |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 471 | There is no facility to schedule calls to a particular thread, but |
| 472 | that should be easy to change, should that ever be required. In |
| 473 | that case, the static variables here should go into the python |
| 474 | threadstate. |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 475 | #endif |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 476 | */ |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 477 | |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 478 | #ifdef WITH_THREAD |
| 479 | |
| 480 | /* The WITH_THREAD implementation is thread-safe. It allows |
| 481 | scheduling to be made from any thread, and even from an executing |
| 482 | callback. |
| 483 | */ |
| 484 | |
| 485 | #define NPENDINGCALLS 32 |
| 486 | static struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 487 | int (*func)(void *); |
| 488 | void *arg; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 489 | } pendingcalls[NPENDINGCALLS]; |
| 490 | static int pendingfirst = 0; |
| 491 | static int pendinglast = 0; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 492 | |
| 493 | int |
| 494 | Py_AddPendingCall(int (*func)(void *), void *arg) |
| 495 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 496 | int i, j, result=0; |
| 497 | PyThread_type_lock lock = pending_lock; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 498 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 499 | /* try a few times for the lock. Since this mechanism is used |
| 500 | * for signal handling (on the main thread), there is a (slim) |
| 501 | * chance that a signal is delivered on the same thread while we |
| 502 | * hold the lock during the Py_MakePendingCalls() function. |
| 503 | * This avoids a deadlock in that case. |
| 504 | * Note that signals can be delivered on any thread. In particular, |
| 505 | * on Windows, a SIGINT is delivered on a system-created worker |
| 506 | * thread. |
| 507 | * We also check for lock being NULL, in the unlikely case that |
| 508 | * this function is called before any bytecode evaluation takes place. |
| 509 | */ |
| 510 | if (lock != NULL) { |
| 511 | for (i = 0; i<100; i++) { |
| 512 | if (PyThread_acquire_lock(lock, NOWAIT_LOCK)) |
| 513 | break; |
| 514 | } |
| 515 | if (i == 100) |
| 516 | return -1; |
| 517 | } |
| 518 | |
| 519 | i = pendinglast; |
| 520 | j = (i + 1) % NPENDINGCALLS; |
| 521 | if (j == pendingfirst) { |
| 522 | result = -1; /* Queue full */ |
| 523 | } else { |
| 524 | pendingcalls[i].func = func; |
| 525 | pendingcalls[i].arg = arg; |
| 526 | pendinglast = j; |
| 527 | } |
| 528 | /* signal main loop */ |
| 529 | SIGNAL_PENDING_CALLS(); |
| 530 | if (lock != NULL) |
| 531 | PyThread_release_lock(lock); |
| 532 | return result; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | int |
| 536 | Py_MakePendingCalls(void) |
| 537 | { |
Charles-François Natali | f23339a | 2011-07-23 18:15:43 +0200 | [diff] [blame] | 538 | static int busy = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 539 | int i; |
| 540 | int r = 0; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 541 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 542 | if (!pending_lock) { |
| 543 | /* initial allocation of the lock */ |
| 544 | pending_lock = PyThread_allocate_lock(); |
| 545 | if (pending_lock == NULL) |
| 546 | return -1; |
| 547 | } |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 548 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 549 | /* only service pending calls on main thread */ |
| 550 | if (main_thread && PyThread_get_thread_ident() != main_thread) |
| 551 | return 0; |
| 552 | /* don't perform recursive pending calls */ |
Charles-François Natali | f23339a | 2011-07-23 18:15:43 +0200 | [diff] [blame] | 553 | if (busy) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 554 | return 0; |
Charles-François Natali | f23339a | 2011-07-23 18:15:43 +0200 | [diff] [blame] | 555 | busy = 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 556 | /* perform a bounded number of calls, in case of recursion */ |
| 557 | for (i=0; i<NPENDINGCALLS; i++) { |
| 558 | int j; |
| 559 | int (*func)(void *); |
| 560 | void *arg = NULL; |
| 561 | |
| 562 | /* pop one item off the queue while holding the lock */ |
| 563 | PyThread_acquire_lock(pending_lock, WAIT_LOCK); |
| 564 | j = pendingfirst; |
| 565 | if (j == pendinglast) { |
| 566 | func = NULL; /* Queue empty */ |
| 567 | } else { |
| 568 | func = pendingcalls[j].func; |
| 569 | arg = pendingcalls[j].arg; |
| 570 | pendingfirst = (j + 1) % NPENDINGCALLS; |
| 571 | } |
| 572 | if (pendingfirst != pendinglast) |
| 573 | SIGNAL_PENDING_CALLS(); |
| 574 | else |
| 575 | UNSIGNAL_PENDING_CALLS(); |
| 576 | PyThread_release_lock(pending_lock); |
| 577 | /* having released the lock, perform the callback */ |
| 578 | if (func == NULL) |
| 579 | break; |
| 580 | r = func(arg); |
| 581 | if (r) |
| 582 | break; |
| 583 | } |
Charles-François Natali | f23339a | 2011-07-23 18:15:43 +0200 | [diff] [blame] | 584 | busy = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 585 | return r; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | #else /* if ! defined WITH_THREAD */ |
| 589 | |
| 590 | /* |
| 591 | WARNING! ASYNCHRONOUSLY EXECUTING CODE! |
| 592 | This code is used for signal handling in python that isn't built |
| 593 | with WITH_THREAD. |
| 594 | Don't use this implementation when Py_AddPendingCalls() can happen |
| 595 | on a different thread! |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 596 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 597 | There are two possible race conditions: |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 598 | (1) nested asynchronous calls to Py_AddPendingCall() |
| 599 | (2) AddPendingCall() calls made while pending calls are being processed. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 600 | |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 601 | (1) is very unlikely because typically signal delivery |
| 602 | is blocked during signal handling. So it should be impossible. |
| 603 | (2) is a real possibility. |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 604 | The current code is safe against (2), but not against (1). |
| 605 | The safety against (2) is derived from the fact that only one |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 606 | thread is present, interrupted by signals, and that the critical |
| 607 | section is protected with the "busy" variable. On Windows, which |
| 608 | delivers SIGINT on a system thread, this does not hold and therefore |
| 609 | Windows really shouldn't use this version. |
| 610 | The two threads could theoretically wiggle around the "busy" variable. |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 611 | */ |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 612 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 613 | #define NPENDINGCALLS 32 |
| 614 | static struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 615 | int (*func)(void *); |
| 616 | void *arg; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 617 | } pendingcalls[NPENDINGCALLS]; |
| 618 | static volatile int pendingfirst = 0; |
| 619 | static volatile int pendinglast = 0; |
Benjamin Peterson | 08ec84c | 2010-05-30 14:49:32 +0000 | [diff] [blame] | 620 | static _Py_atomic_int pendingcalls_to_do = {0}; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 621 | |
| 622 | int |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 623 | Py_AddPendingCall(int (*func)(void *), void *arg) |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 624 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 625 | static volatile int busy = 0; |
| 626 | int i, j; |
| 627 | /* XXX Begin critical section */ |
| 628 | if (busy) |
| 629 | return -1; |
| 630 | busy = 1; |
| 631 | i = pendinglast; |
| 632 | j = (i + 1) % NPENDINGCALLS; |
| 633 | if (j == pendingfirst) { |
| 634 | busy = 0; |
| 635 | return -1; /* Queue full */ |
| 636 | } |
| 637 | pendingcalls[i].func = func; |
| 638 | pendingcalls[i].arg = arg; |
| 639 | pendinglast = j; |
Skip Montanaro | d581d77 | 2002-09-03 20:10:45 +0000 | [diff] [blame] | 640 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 641 | SIGNAL_PENDING_CALLS(); |
| 642 | busy = 0; |
| 643 | /* XXX End critical section */ |
| 644 | return 0; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 645 | } |
| 646 | |
Guido van Rossum | 180d7b4 | 1994-09-29 09:45:57 +0000 | [diff] [blame] | 647 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 648 | Py_MakePendingCalls(void) |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 649 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 650 | static int busy = 0; |
| 651 | if (busy) |
| 652 | return 0; |
| 653 | busy = 1; |
| 654 | UNSIGNAL_PENDING_CALLS(); |
| 655 | for (;;) { |
| 656 | int i; |
| 657 | int (*func)(void *); |
| 658 | void *arg; |
| 659 | i = pendingfirst; |
| 660 | if (i == pendinglast) |
| 661 | break; /* Queue empty */ |
| 662 | func = pendingcalls[i].func; |
| 663 | arg = pendingcalls[i].arg; |
| 664 | pendingfirst = (i + 1) % NPENDINGCALLS; |
| 665 | if (func(arg) < 0) { |
| 666 | busy = 0; |
| 667 | SIGNAL_PENDING_CALLS(); /* We're not done yet */ |
| 668 | return -1; |
| 669 | } |
| 670 | } |
| 671 | busy = 0; |
| 672 | return 0; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 673 | } |
| 674 | |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 675 | #endif /* WITH_THREAD */ |
| 676 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 677 | |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 678 | /* The interpreter's recursion limit */ |
| 679 | |
Hye-Shik Chang | b6fa281 | 2005-04-04 15:49:02 +0000 | [diff] [blame] | 680 | #ifndef Py_DEFAULT_RECURSION_LIMIT |
| 681 | #define Py_DEFAULT_RECURSION_LIMIT 1000 |
| 682 | #endif |
| 683 | static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT; |
| 684 | int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 685 | |
Vladimir Marangozov | 7bd25be | 2000-09-01 11:07:19 +0000 | [diff] [blame] | 686 | int |
| 687 | Py_GetRecursionLimit(void) |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 688 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 689 | return recursion_limit; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 690 | } |
| 691 | |
Vladimir Marangozov | 7bd25be | 2000-09-01 11:07:19 +0000 | [diff] [blame] | 692 | void |
| 693 | Py_SetRecursionLimit(int new_limit) |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 694 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 695 | recursion_limit = new_limit; |
| 696 | _Py_CheckRecursionLimit = recursion_limit; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 697 | } |
| 698 | |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 699 | /* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall() |
| 700 | if the recursion_depth reaches _Py_CheckRecursionLimit. |
| 701 | If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit |
| 702 | to guarantee that _Py_CheckRecursiveCall() is regularly called. |
| 703 | Without USE_STACKCHECK, there is no need for this. */ |
| 704 | int |
| 705 | _Py_CheckRecursiveCall(char *where) |
| 706 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 707 | PyThreadState *tstate = PyThreadState_GET(); |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 708 | |
| 709 | #ifdef USE_STACKCHECK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 710 | if (PyOS_CheckStack()) { |
| 711 | --tstate->recursion_depth; |
| 712 | PyErr_SetString(PyExc_MemoryError, "Stack overflow"); |
| 713 | return -1; |
| 714 | } |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 715 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 716 | _Py_CheckRecursionLimit = recursion_limit; |
| 717 | if (tstate->recursion_critical) |
| 718 | /* Somebody asked that we don't check for recursion. */ |
| 719 | return 0; |
| 720 | if (tstate->overflowed) { |
| 721 | if (tstate->recursion_depth > recursion_limit + 50) { |
| 722 | /* Overflowing while handling an overflow. Give up. */ |
| 723 | Py_FatalError("Cannot recover from stack overflow."); |
| 724 | } |
| 725 | return 0; |
| 726 | } |
| 727 | if (tstate->recursion_depth > recursion_limit) { |
| 728 | --tstate->recursion_depth; |
| 729 | tstate->overflowed = 1; |
| 730 | PyErr_Format(PyExc_RuntimeError, |
| 731 | "maximum recursion depth exceeded%s", |
| 732 | where); |
| 733 | return -1; |
| 734 | } |
| 735 | return 0; |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 736 | } |
| 737 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 738 | /* Status code for main loop (reason for stack unwind) */ |
Raymond Hettinger | 7c95865 | 2004-04-06 10:11:10 +0000 | [diff] [blame] | 739 | enum why_code { |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 740 | WHY_NOT = 0x0001, /* No error */ |
| 741 | WHY_EXCEPTION = 0x0002, /* Exception occurred */ |
| 742 | WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */ |
| 743 | WHY_RETURN = 0x0008, /* 'return' statement */ |
| 744 | WHY_BREAK = 0x0010, /* 'break' statement */ |
| 745 | WHY_CONTINUE = 0x0020, /* 'continue' statement */ |
| 746 | WHY_YIELD = 0x0040, /* 'yield' operator */ |
| 747 | WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */ |
Raymond Hettinger | 7c95865 | 2004-04-06 10:11:10 +0000 | [diff] [blame] | 748 | }; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 749 | |
Benjamin Peterson | 8788024 | 2011-07-03 16:48:31 -0500 | [diff] [blame] | 750 | static void save_exc_state(PyThreadState *, PyFrameObject *); |
| 751 | static void swap_exc_state(PyThreadState *, PyFrameObject *); |
| 752 | static void restore_and_clear_exc_state(PyThreadState *, PyFrameObject *); |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 753 | static enum why_code do_raise(PyObject *, PyObject *); |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 754 | static int unpack_iterable(PyObject *, int, int, PyObject **); |
Guido van Rossum | 1aa1483 | 1997-01-21 05:34:20 +0000 | [diff] [blame] | 755 | |
Jeffrey Yasskin | 008d8ef | 2008-12-06 17:09:27 +0000 | [diff] [blame] | 756 | /* Records whether tracing is on for any thread. Counts the number of |
| 757 | threads for which tstate->c_tracefunc is non-NULL, so if the value |
| 758 | is 0, we know we don't have to check this thread's c_tracefunc. |
| 759 | This speeds up the if statement in PyEval_EvalFrameEx() after |
| 760 | fast_next_opcode*/ |
| 761 | static int _Py_TracingPossible = 0; |
| 762 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 763 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 764 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 765 | PyObject * |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 766 | PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 767 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 768 | return PyEval_EvalCodeEx(co, |
| 769 | globals, locals, |
| 770 | (PyObject **)NULL, 0, |
| 771 | (PyObject **)NULL, 0, |
| 772 | (PyObject **)NULL, 0, |
| 773 | NULL, NULL); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | |
| 777 | /* Interpreter main loop */ |
| 778 | |
Martin v. Löwis | 8d97e33 | 2004-06-27 15:43:12 +0000 | [diff] [blame] | 779 | PyObject * |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 780 | PyEval_EvalFrame(PyFrameObject *f) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 781 | /* This is for backward compatibility with extension modules that |
| 782 | used this API; core interpreter code should call |
| 783 | PyEval_EvalFrameEx() */ |
| 784 | return PyEval_EvalFrameEx(f, 0); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 788 | PyEval_EvalFrameEx(PyFrameObject *f, int throwflag) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 789 | { |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 790 | #ifdef DXPAIRS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 791 | int lastopcode = 0; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 792 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 793 | register PyObject **stack_pointer; /* Next free slot in value stack */ |
| 794 | register unsigned char *next_instr; |
| 795 | register int opcode; /* Current opcode */ |
| 796 | register int oparg; /* Current opcode argument, if any */ |
| 797 | register enum why_code why; /* Reason for block stack unwind */ |
| 798 | register int err; /* Error status -- nonzero if error */ |
| 799 | register PyObject *x; /* Result object -- NULL if error */ |
| 800 | register PyObject *v; /* Temporary objects popped off stack */ |
| 801 | register PyObject *w; |
| 802 | register PyObject *u; |
| 803 | register PyObject *t; |
| 804 | register PyObject **fastlocals, **freevars; |
| 805 | PyObject *retval = NULL; /* Return value */ |
| 806 | PyThreadState *tstate = PyThreadState_GET(); |
| 807 | PyCodeObject *co; |
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 | /* when tracing we set things up so that |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 810 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 811 | not (instr_lb <= current_bytecode_offset < instr_ub) |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 812 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 813 | is true when the line being executed has changed. The |
| 814 | initial values are such as to make this false the first |
| 815 | time it is tested. */ |
| 816 | int instr_ub = -1, instr_lb = 0, instr_prev = -1; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 817 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 818 | unsigned char *first_instr; |
| 819 | PyObject *names; |
| 820 | PyObject *consts; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [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. |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 1170 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1171 | When the PREDICT() macros are enabled, some opcode pairs follow in |
| 1172 | direct succession without updating f->f_lasti. A successful |
| 1173 | prediction effectively links the two codes together as if they |
| 1174 | were a single new opcode; accordingly,f->f_lasti will point to |
| 1175 | the first code in the pair (for instance, GET_ITER followed by |
| 1176 | FOR_ITER is effectively a single opcode and f->f_lasti will point |
| 1177 | at to the beginning of the combined pair.) |
| 1178 | */ |
| 1179 | next_instr = first_instr + f->f_lasti + 1; |
| 1180 | stack_pointer = f->f_stacktop; |
| 1181 | assert(stack_pointer != NULL); |
| 1182 | f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */ |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1183 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1184 | if (co->co_flags & CO_GENERATOR && !throwflag) { |
| 1185 | if (f->f_exc_type != NULL && f->f_exc_type != Py_None) { |
| 1186 | /* We were in an except handler when we left, |
| 1187 | restore the exception state which was put aside |
| 1188 | (see YIELD_VALUE). */ |
Benjamin Peterson | 8788024 | 2011-07-03 16:48:31 -0500 | [diff] [blame] | 1189 | swap_exc_state(tstate, f); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1190 | } |
Benjamin Peterson | 8788024 | 2011-07-03 16:48:31 -0500 | [diff] [blame] | 1191 | else |
| 1192 | save_exc_state(tstate, f); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1193 | } |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 1194 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1195 | #ifdef LLTRACE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1196 | lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1197 | #endif |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1198 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1199 | why = WHY_NOT; |
| 1200 | err = 0; |
| 1201 | x = Py_None; /* Not a reference, just anything non-NULL */ |
| 1202 | w = NULL; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1203 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1204 | if (throwflag) { /* support for generator.throw() */ |
| 1205 | why = WHY_EXCEPTION; |
| 1206 | goto on_error; |
| 1207 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1208 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1209 | for (;;) { |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 1210 | #ifdef WITH_TSC |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1211 | if (inst1 == 0) { |
| 1212 | /* Almost surely, the opcode executed a break |
| 1213 | or a continue, preventing inst1 from being set |
| 1214 | on the way out of the loop. |
| 1215 | */ |
| 1216 | READ_TIMESTAMP(inst1); |
| 1217 | loop1 = inst1; |
| 1218 | } |
| 1219 | dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1, |
| 1220 | intr0, intr1); |
| 1221 | ticked = 0; |
| 1222 | inst1 = 0; |
| 1223 | intr0 = 0; |
| 1224 | intr1 = 0; |
| 1225 | READ_TIMESTAMP(loop0); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 1226 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1227 | assert(stack_pointer >= f->f_valuestack); /* else underflow */ |
| 1228 | assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */ |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 1229 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1230 | /* Do periodic things. Doing this every time through |
| 1231 | the loop would add too much overhead, so we do it |
| 1232 | only every Nth instruction. We also do it if |
| 1233 | ``pendingcalls_to_do'' is set, i.e. when an asynchronous |
| 1234 | event needs attention (e.g. a signal handler or |
| 1235 | async I/O handler); see Py_AddPendingCall() and |
| 1236 | Py_MakePendingCalls() above. */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1237 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1238 | if (_Py_atomic_load_relaxed(&eval_breaker)) { |
| 1239 | if (*next_instr == SETUP_FINALLY) { |
| 1240 | /* Make the last opcode before |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 1241 | a try: finally: block uninterruptible. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1242 | goto fast_next_opcode; |
| 1243 | } |
| 1244 | tstate->tick_counter++; |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 1245 | #ifdef WITH_TSC |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1246 | ticked = 1; |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 1247 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1248 | if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) { |
| 1249 | if (Py_MakePendingCalls() < 0) { |
| 1250 | why = WHY_EXCEPTION; |
| 1251 | goto on_error; |
| 1252 | } |
| 1253 | } |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 1254 | #ifdef WITH_THREAD |
Benjamin Peterson | d2be5b4 | 2010-09-10 22:47:02 +0000 | [diff] [blame] | 1255 | if (_Py_atomic_load_relaxed(&gil_drop_request)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1256 | /* Give another thread a chance */ |
| 1257 | if (PyThreadState_Swap(NULL) != tstate) |
| 1258 | Py_FatalError("ceval: tstate mix-up"); |
| 1259 | drop_gil(tstate); |
| 1260 | |
| 1261 | /* Other threads may run now */ |
| 1262 | |
| 1263 | take_gil(tstate); |
| 1264 | if (PyThreadState_Swap(tstate) != NULL) |
| 1265 | Py_FatalError("ceval: orphan tstate"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1266 | } |
Benjamin Peterson | d2be5b4 | 2010-09-10 22:47:02 +0000 | [diff] [blame] | 1267 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1268 | /* Check for asynchronous exceptions. */ |
| 1269 | if (tstate->async_exc != NULL) { |
| 1270 | x = tstate->async_exc; |
| 1271 | tstate->async_exc = NULL; |
| 1272 | UNSIGNAL_ASYNC_EXC(); |
| 1273 | PyErr_SetNone(x); |
| 1274 | Py_DECREF(x); |
| 1275 | why = WHY_EXCEPTION; |
| 1276 | goto on_error; |
| 1277 | } |
| 1278 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1279 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1280 | fast_next_opcode: |
| 1281 | f->f_lasti = INSTR_OFFSET(); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1282 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1283 | /* line-by-line tracing support */ |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1284 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1285 | if (_Py_TracingPossible && |
| 1286 | tstate->c_tracefunc != NULL && !tstate->tracing) { |
| 1287 | /* see maybe_call_line_trace |
| 1288 | for expository comments */ |
| 1289 | f->f_stacktop = stack_pointer; |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 1290 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1291 | err = maybe_call_line_trace(tstate->c_tracefunc, |
| 1292 | tstate->c_traceobj, |
| 1293 | f, &instr_lb, &instr_ub, |
| 1294 | &instr_prev); |
| 1295 | /* Reload possibly changed frame fields */ |
| 1296 | JUMPTO(f->f_lasti); |
| 1297 | if (f->f_stacktop != NULL) { |
| 1298 | stack_pointer = f->f_stacktop; |
| 1299 | f->f_stacktop = NULL; |
| 1300 | } |
| 1301 | if (err) { |
| 1302 | /* trace function raised an exception */ |
| 1303 | goto on_error; |
| 1304 | } |
| 1305 | } |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1306 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1307 | /* Extract opcode and argument */ |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1308 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1309 | opcode = NEXTOP(); |
| 1310 | oparg = 0; /* allows oparg to be stored in a register because |
| 1311 | it doesn't have to be remembered across a full loop */ |
| 1312 | if (HAS_ARG(opcode)) |
| 1313 | oparg = NEXTARG(); |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 1314 | dispatch_opcode: |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 1315 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 1316 | #ifdef DXPAIRS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1317 | dxpairs[lastopcode][opcode]++; |
| 1318 | lastopcode = opcode; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 1319 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1320 | dxp[opcode]++; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 1321 | #endif |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1322 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 1323 | #ifdef LLTRACE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1324 | /* Instruction tracing */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1325 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1326 | if (lltrace) { |
| 1327 | if (HAS_ARG(opcode)) { |
| 1328 | printf("%d: %d, %d\n", |
| 1329 | f->f_lasti, opcode, oparg); |
| 1330 | } |
| 1331 | else { |
| 1332 | printf("%d: %d\n", |
| 1333 | f->f_lasti, opcode); |
| 1334 | } |
| 1335 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1336 | #endif |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 1337 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1338 | /* Main switch on opcode */ |
| 1339 | READ_TIMESTAMP(inst0); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 1340 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1341 | switch (opcode) { |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1342 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1343 | /* BEWARE! |
| 1344 | It is essential that any operation that fails sets either |
| 1345 | x to NULL, err to nonzero, or why to anything but WHY_NOT, |
| 1346 | and that no operation that succeeds does this! */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1347 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1348 | TARGET(NOP) |
| 1349 | FAST_DISPATCH(); |
Raymond Hettinger | 9c18e81 | 2004-06-21 16:31:15 +0000 | [diff] [blame] | 1350 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1351 | TARGET(LOAD_FAST) |
| 1352 | x = GETLOCAL(oparg); |
| 1353 | if (x != NULL) { |
| 1354 | Py_INCREF(x); |
| 1355 | PUSH(x); |
| 1356 | FAST_DISPATCH(); |
| 1357 | } |
| 1358 | format_exc_check_arg(PyExc_UnboundLocalError, |
| 1359 | UNBOUNDLOCAL_ERROR_MSG, |
| 1360 | PyTuple_GetItem(co->co_varnames, oparg)); |
| 1361 | break; |
Neil Schemenauer | 6354386 | 2002-02-17 19:10:14 +0000 | [diff] [blame] | 1362 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1363 | TARGET(LOAD_CONST) |
| 1364 | x = GETITEM(consts, oparg); |
| 1365 | Py_INCREF(x); |
| 1366 | PUSH(x); |
| 1367 | FAST_DISPATCH(); |
Neil Schemenauer | 6354386 | 2002-02-17 19:10:14 +0000 | [diff] [blame] | 1368 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1369 | PREDICTED_WITH_ARG(STORE_FAST); |
| 1370 | TARGET(STORE_FAST) |
| 1371 | v = POP(); |
| 1372 | SETLOCAL(oparg, v); |
| 1373 | FAST_DISPATCH(); |
Neil Schemenauer | 6354386 | 2002-02-17 19:10:14 +0000 | [diff] [blame] | 1374 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1375 | TARGET(POP_TOP) |
| 1376 | v = POP(); |
| 1377 | Py_DECREF(v); |
| 1378 | FAST_DISPATCH(); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1379 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1380 | TARGET(ROT_TWO) |
| 1381 | v = TOP(); |
| 1382 | w = SECOND(); |
| 1383 | SET_TOP(w); |
| 1384 | SET_SECOND(v); |
| 1385 | FAST_DISPATCH(); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1386 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1387 | TARGET(ROT_THREE) |
| 1388 | v = TOP(); |
| 1389 | w = SECOND(); |
| 1390 | x = THIRD(); |
| 1391 | SET_TOP(w); |
| 1392 | SET_SECOND(x); |
| 1393 | SET_THIRD(v); |
| 1394 | FAST_DISPATCH(); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1395 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1396 | TARGET(DUP_TOP) |
| 1397 | v = TOP(); |
| 1398 | Py_INCREF(v); |
| 1399 | PUSH(v); |
| 1400 | FAST_DISPATCH(); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1401 | |
Antoine Pitrou | 74a69fa | 2010-09-04 18:43:52 +0000 | [diff] [blame] | 1402 | TARGET(DUP_TOP_TWO) |
| 1403 | x = TOP(); |
| 1404 | Py_INCREF(x); |
| 1405 | w = SECOND(); |
| 1406 | Py_INCREF(w); |
| 1407 | STACKADJ(2); |
| 1408 | SET_TOP(x); |
| 1409 | SET_SECOND(w); |
| 1410 | FAST_DISPATCH(); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1411 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1412 | TARGET(UNARY_POSITIVE) |
| 1413 | v = TOP(); |
| 1414 | x = PyNumber_Positive(v); |
| 1415 | Py_DECREF(v); |
| 1416 | SET_TOP(x); |
| 1417 | if (x != NULL) DISPATCH(); |
| 1418 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1419 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1420 | TARGET(UNARY_NEGATIVE) |
| 1421 | v = TOP(); |
| 1422 | x = PyNumber_Negative(v); |
| 1423 | Py_DECREF(v); |
| 1424 | SET_TOP(x); |
| 1425 | if (x != NULL) DISPATCH(); |
| 1426 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1427 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1428 | TARGET(UNARY_NOT) |
| 1429 | v = TOP(); |
| 1430 | err = PyObject_IsTrue(v); |
| 1431 | Py_DECREF(v); |
| 1432 | if (err == 0) { |
| 1433 | Py_INCREF(Py_True); |
| 1434 | SET_TOP(Py_True); |
| 1435 | DISPATCH(); |
| 1436 | } |
| 1437 | else if (err > 0) { |
| 1438 | Py_INCREF(Py_False); |
| 1439 | SET_TOP(Py_False); |
| 1440 | err = 0; |
| 1441 | DISPATCH(); |
| 1442 | } |
| 1443 | STACKADJ(-1); |
| 1444 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1445 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1446 | TARGET(UNARY_INVERT) |
| 1447 | v = TOP(); |
| 1448 | x = PyNumber_Invert(v); |
| 1449 | Py_DECREF(v); |
| 1450 | SET_TOP(x); |
| 1451 | if (x != NULL) DISPATCH(); |
| 1452 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1453 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1454 | TARGET(BINARY_POWER) |
| 1455 | w = POP(); |
| 1456 | v = TOP(); |
| 1457 | x = PyNumber_Power(v, w, Py_None); |
| 1458 | Py_DECREF(v); |
| 1459 | Py_DECREF(w); |
| 1460 | SET_TOP(x); |
| 1461 | if (x != NULL) DISPATCH(); |
| 1462 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1463 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1464 | TARGET(BINARY_MULTIPLY) |
| 1465 | w = POP(); |
| 1466 | v = TOP(); |
| 1467 | x = PyNumber_Multiply(v, w); |
| 1468 | Py_DECREF(v); |
| 1469 | Py_DECREF(w); |
| 1470 | SET_TOP(x); |
| 1471 | if (x != NULL) DISPATCH(); |
| 1472 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1473 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1474 | TARGET(BINARY_TRUE_DIVIDE) |
| 1475 | w = POP(); |
| 1476 | v = TOP(); |
| 1477 | x = PyNumber_TrueDivide(v, w); |
| 1478 | Py_DECREF(v); |
| 1479 | Py_DECREF(w); |
| 1480 | SET_TOP(x); |
| 1481 | if (x != NULL) DISPATCH(); |
| 1482 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1483 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1484 | TARGET(BINARY_FLOOR_DIVIDE) |
| 1485 | w = POP(); |
| 1486 | v = TOP(); |
| 1487 | x = PyNumber_FloorDivide(v, w); |
| 1488 | Py_DECREF(v); |
| 1489 | Py_DECREF(w); |
| 1490 | SET_TOP(x); |
| 1491 | if (x != NULL) DISPATCH(); |
| 1492 | break; |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1493 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1494 | TARGET(BINARY_MODULO) |
| 1495 | w = POP(); |
| 1496 | v = TOP(); |
| 1497 | if (PyUnicode_CheckExact(v)) |
| 1498 | x = PyUnicode_Format(v, w); |
| 1499 | else |
| 1500 | x = PyNumber_Remainder(v, w); |
| 1501 | Py_DECREF(v); |
| 1502 | Py_DECREF(w); |
| 1503 | SET_TOP(x); |
| 1504 | if (x != NULL) DISPATCH(); |
| 1505 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1506 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1507 | TARGET(BINARY_ADD) |
| 1508 | w = POP(); |
| 1509 | v = TOP(); |
Benjamin Peterson | 811c2f1 | 2011-09-30 21:31:21 -0400 | [diff] [blame] | 1510 | x = PyNumber_Add(v, w); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1511 | Py_DECREF(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1512 | Py_DECREF(w); |
| 1513 | SET_TOP(x); |
| 1514 | if (x != NULL) DISPATCH(); |
| 1515 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1516 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1517 | TARGET(BINARY_SUBTRACT) |
| 1518 | w = POP(); |
| 1519 | v = TOP(); |
| 1520 | x = PyNumber_Subtract(v, w); |
| 1521 | Py_DECREF(v); |
| 1522 | Py_DECREF(w); |
| 1523 | SET_TOP(x); |
| 1524 | if (x != NULL) DISPATCH(); |
| 1525 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1526 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1527 | TARGET(BINARY_SUBSCR) |
| 1528 | w = POP(); |
| 1529 | v = TOP(); |
| 1530 | x = PyObject_GetItem(v, w); |
| 1531 | Py_DECREF(v); |
| 1532 | Py_DECREF(w); |
| 1533 | SET_TOP(x); |
| 1534 | if (x != NULL) DISPATCH(); |
| 1535 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1536 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1537 | TARGET(BINARY_LSHIFT) |
| 1538 | w = POP(); |
| 1539 | v = TOP(); |
| 1540 | x = PyNumber_Lshift(v, w); |
| 1541 | Py_DECREF(v); |
| 1542 | Py_DECREF(w); |
| 1543 | SET_TOP(x); |
| 1544 | if (x != NULL) DISPATCH(); |
| 1545 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1546 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1547 | TARGET(BINARY_RSHIFT) |
| 1548 | w = POP(); |
| 1549 | v = TOP(); |
| 1550 | x = PyNumber_Rshift(v, w); |
| 1551 | Py_DECREF(v); |
| 1552 | Py_DECREF(w); |
| 1553 | SET_TOP(x); |
| 1554 | if (x != NULL) DISPATCH(); |
| 1555 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1556 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1557 | TARGET(BINARY_AND) |
| 1558 | w = POP(); |
| 1559 | v = TOP(); |
| 1560 | x = PyNumber_And(v, w); |
| 1561 | Py_DECREF(v); |
| 1562 | Py_DECREF(w); |
| 1563 | SET_TOP(x); |
| 1564 | if (x != NULL) DISPATCH(); |
| 1565 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1566 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1567 | TARGET(BINARY_XOR) |
| 1568 | w = POP(); |
| 1569 | v = TOP(); |
| 1570 | x = PyNumber_Xor(v, w); |
| 1571 | Py_DECREF(v); |
| 1572 | Py_DECREF(w); |
| 1573 | SET_TOP(x); |
| 1574 | if (x != NULL) DISPATCH(); |
| 1575 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1576 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1577 | TARGET(BINARY_OR) |
| 1578 | w = POP(); |
| 1579 | v = TOP(); |
| 1580 | x = PyNumber_Or(v, w); |
| 1581 | Py_DECREF(v); |
| 1582 | Py_DECREF(w); |
| 1583 | SET_TOP(x); |
| 1584 | if (x != NULL) DISPATCH(); |
| 1585 | break; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1586 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1587 | TARGET(LIST_APPEND) |
| 1588 | w = POP(); |
| 1589 | v = PEEK(oparg); |
| 1590 | err = PyList_Append(v, w); |
| 1591 | Py_DECREF(w); |
| 1592 | if (err == 0) { |
| 1593 | PREDICT(JUMP_ABSOLUTE); |
| 1594 | DISPATCH(); |
| 1595 | } |
| 1596 | break; |
Raymond Hettinger | dd80f76 | 2004-03-07 07:31:06 +0000 | [diff] [blame] | 1597 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1598 | TARGET(SET_ADD) |
| 1599 | w = POP(); |
| 1600 | v = stack_pointer[-oparg]; |
| 1601 | err = PySet_Add(v, w); |
| 1602 | Py_DECREF(w); |
| 1603 | if (err == 0) { |
| 1604 | PREDICT(JUMP_ABSOLUTE); |
| 1605 | DISPATCH(); |
| 1606 | } |
| 1607 | break; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1608 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1609 | TARGET(INPLACE_POWER) |
| 1610 | w = POP(); |
| 1611 | v = TOP(); |
| 1612 | x = PyNumber_InPlacePower(v, w, Py_None); |
| 1613 | Py_DECREF(v); |
| 1614 | Py_DECREF(w); |
| 1615 | SET_TOP(x); |
| 1616 | if (x != NULL) DISPATCH(); |
| 1617 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1618 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1619 | TARGET(INPLACE_MULTIPLY) |
| 1620 | w = POP(); |
| 1621 | v = TOP(); |
| 1622 | x = PyNumber_InPlaceMultiply(v, w); |
| 1623 | Py_DECREF(v); |
| 1624 | Py_DECREF(w); |
| 1625 | SET_TOP(x); |
| 1626 | if (x != NULL) DISPATCH(); |
| 1627 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1628 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1629 | TARGET(INPLACE_TRUE_DIVIDE) |
| 1630 | w = POP(); |
| 1631 | v = TOP(); |
| 1632 | x = PyNumber_InPlaceTrueDivide(v, w); |
| 1633 | Py_DECREF(v); |
| 1634 | Py_DECREF(w); |
| 1635 | SET_TOP(x); |
| 1636 | if (x != NULL) DISPATCH(); |
| 1637 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1638 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1639 | TARGET(INPLACE_FLOOR_DIVIDE) |
| 1640 | w = POP(); |
| 1641 | v = TOP(); |
| 1642 | x = PyNumber_InPlaceFloorDivide(v, w); |
| 1643 | Py_DECREF(v); |
| 1644 | Py_DECREF(w); |
| 1645 | SET_TOP(x); |
| 1646 | if (x != NULL) DISPATCH(); |
| 1647 | break; |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1648 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1649 | TARGET(INPLACE_MODULO) |
| 1650 | w = POP(); |
| 1651 | v = TOP(); |
| 1652 | x = PyNumber_InPlaceRemainder(v, w); |
| 1653 | Py_DECREF(v); |
| 1654 | Py_DECREF(w); |
| 1655 | SET_TOP(x); |
| 1656 | if (x != NULL) DISPATCH(); |
| 1657 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1658 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1659 | TARGET(INPLACE_ADD) |
| 1660 | w = POP(); |
| 1661 | v = TOP(); |
Benjamin Peterson | 811c2f1 | 2011-09-30 21:31:21 -0400 | [diff] [blame] | 1662 | x = PyNumber_InPlaceAdd(v, w); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1663 | Py_DECREF(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1664 | Py_DECREF(w); |
| 1665 | SET_TOP(x); |
| 1666 | if (x != NULL) DISPATCH(); |
| 1667 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1668 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1669 | TARGET(INPLACE_SUBTRACT) |
| 1670 | w = POP(); |
| 1671 | v = TOP(); |
| 1672 | x = PyNumber_InPlaceSubtract(v, w); |
| 1673 | Py_DECREF(v); |
| 1674 | Py_DECREF(w); |
| 1675 | SET_TOP(x); |
| 1676 | if (x != NULL) DISPATCH(); |
| 1677 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1678 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1679 | TARGET(INPLACE_LSHIFT) |
| 1680 | w = POP(); |
| 1681 | v = TOP(); |
| 1682 | x = PyNumber_InPlaceLshift(v, w); |
| 1683 | Py_DECREF(v); |
| 1684 | Py_DECREF(w); |
| 1685 | SET_TOP(x); |
| 1686 | if (x != NULL) DISPATCH(); |
| 1687 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1688 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1689 | TARGET(INPLACE_RSHIFT) |
| 1690 | w = POP(); |
| 1691 | v = TOP(); |
| 1692 | x = PyNumber_InPlaceRshift(v, w); |
| 1693 | Py_DECREF(v); |
| 1694 | Py_DECREF(w); |
| 1695 | SET_TOP(x); |
| 1696 | if (x != NULL) DISPATCH(); |
| 1697 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1698 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1699 | TARGET(INPLACE_AND) |
| 1700 | w = POP(); |
| 1701 | v = TOP(); |
| 1702 | x = PyNumber_InPlaceAnd(v, w); |
| 1703 | Py_DECREF(v); |
| 1704 | Py_DECREF(w); |
| 1705 | SET_TOP(x); |
| 1706 | if (x != NULL) DISPATCH(); |
| 1707 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1708 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1709 | TARGET(INPLACE_XOR) |
| 1710 | w = POP(); |
| 1711 | v = TOP(); |
| 1712 | x = PyNumber_InPlaceXor(v, w); |
| 1713 | Py_DECREF(v); |
| 1714 | Py_DECREF(w); |
| 1715 | SET_TOP(x); |
| 1716 | if (x != NULL) DISPATCH(); |
| 1717 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1718 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1719 | TARGET(INPLACE_OR) |
| 1720 | w = POP(); |
| 1721 | v = TOP(); |
| 1722 | x = PyNumber_InPlaceOr(v, w); |
| 1723 | Py_DECREF(v); |
| 1724 | Py_DECREF(w); |
| 1725 | SET_TOP(x); |
| 1726 | if (x != NULL) DISPATCH(); |
| 1727 | break; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1728 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1729 | TARGET(STORE_SUBSCR) |
| 1730 | w = TOP(); |
| 1731 | v = SECOND(); |
| 1732 | u = THIRD(); |
| 1733 | STACKADJ(-3); |
| 1734 | /* v[w] = u */ |
| 1735 | err = PyObject_SetItem(v, w, u); |
| 1736 | Py_DECREF(u); |
| 1737 | Py_DECREF(v); |
| 1738 | Py_DECREF(w); |
| 1739 | if (err == 0) DISPATCH(); |
| 1740 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1741 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1742 | TARGET(DELETE_SUBSCR) |
| 1743 | w = TOP(); |
| 1744 | v = SECOND(); |
| 1745 | STACKADJ(-2); |
| 1746 | /* del v[w] */ |
| 1747 | err = PyObject_DelItem(v, w); |
| 1748 | Py_DECREF(v); |
| 1749 | Py_DECREF(w); |
| 1750 | if (err == 0) DISPATCH(); |
| 1751 | break; |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1752 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1753 | TARGET(PRINT_EXPR) |
| 1754 | v = POP(); |
| 1755 | w = PySys_GetObject("displayhook"); |
| 1756 | if (w == NULL) { |
| 1757 | PyErr_SetString(PyExc_RuntimeError, |
| 1758 | "lost sys.displayhook"); |
| 1759 | err = -1; |
| 1760 | x = NULL; |
| 1761 | } |
| 1762 | if (err == 0) { |
| 1763 | x = PyTuple_Pack(1, v); |
| 1764 | if (x == NULL) |
| 1765 | err = -1; |
| 1766 | } |
| 1767 | if (err == 0) { |
| 1768 | w = PyEval_CallObject(w, x); |
| 1769 | Py_XDECREF(w); |
| 1770 | if (w == NULL) |
| 1771 | err = -1; |
| 1772 | } |
| 1773 | Py_DECREF(v); |
| 1774 | Py_XDECREF(x); |
| 1775 | break; |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 1776 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1777 | #ifdef CASE_TOO_BIG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1778 | default: switch (opcode) { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1779 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1780 | TARGET(RAISE_VARARGS) |
| 1781 | v = w = NULL; |
| 1782 | switch (oparg) { |
| 1783 | case 2: |
| 1784 | v = POP(); /* cause */ |
| 1785 | case 1: |
| 1786 | w = POP(); /* exc */ |
| 1787 | case 0: /* Fallthrough */ |
| 1788 | why = do_raise(w, v); |
| 1789 | break; |
| 1790 | default: |
| 1791 | PyErr_SetString(PyExc_SystemError, |
| 1792 | "bad RAISE_VARARGS oparg"); |
| 1793 | why = WHY_EXCEPTION; |
| 1794 | break; |
| 1795 | } |
| 1796 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1797 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1798 | TARGET(STORE_LOCALS) |
| 1799 | x = POP(); |
| 1800 | v = f->f_locals; |
| 1801 | Py_XDECREF(v); |
| 1802 | f->f_locals = x; |
| 1803 | DISPATCH(); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1804 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1805 | TARGET(RETURN_VALUE) |
| 1806 | retval = POP(); |
| 1807 | why = WHY_RETURN; |
| 1808 | goto fast_block_end; |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 1809 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1810 | TARGET(YIELD_VALUE) |
| 1811 | retval = POP(); |
| 1812 | f->f_stacktop = stack_pointer; |
| 1813 | why = WHY_YIELD; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1814 | goto fast_yield; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1815 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1816 | TARGET(POP_EXCEPT) |
| 1817 | { |
| 1818 | PyTryBlock *b = PyFrame_BlockPop(f); |
| 1819 | if (b->b_type != EXCEPT_HANDLER) { |
| 1820 | PyErr_SetString(PyExc_SystemError, |
| 1821 | "popped block is not an except handler"); |
| 1822 | why = WHY_EXCEPTION; |
| 1823 | break; |
| 1824 | } |
| 1825 | UNWIND_EXCEPT_HANDLER(b); |
| 1826 | } |
| 1827 | DISPATCH(); |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 1828 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1829 | TARGET(POP_BLOCK) |
| 1830 | { |
| 1831 | PyTryBlock *b = PyFrame_BlockPop(f); |
| 1832 | UNWIND_BLOCK(b); |
| 1833 | } |
| 1834 | DISPATCH(); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1835 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1836 | PREDICTED(END_FINALLY); |
| 1837 | TARGET(END_FINALLY) |
| 1838 | v = POP(); |
| 1839 | if (PyLong_Check(v)) { |
| 1840 | why = (enum why_code) PyLong_AS_LONG(v); |
| 1841 | assert(why != WHY_YIELD); |
| 1842 | if (why == WHY_RETURN || |
| 1843 | why == WHY_CONTINUE) |
| 1844 | retval = POP(); |
| 1845 | if (why == WHY_SILENCED) { |
| 1846 | /* An exception was silenced by 'with', we must |
| 1847 | manually unwind the EXCEPT_HANDLER block which was |
| 1848 | created when the exception was caught, otherwise |
| 1849 | the stack will be in an inconsistent state. */ |
| 1850 | PyTryBlock *b = PyFrame_BlockPop(f); |
| 1851 | assert(b->b_type == EXCEPT_HANDLER); |
| 1852 | UNWIND_EXCEPT_HANDLER(b); |
| 1853 | why = WHY_NOT; |
| 1854 | } |
| 1855 | } |
| 1856 | else if (PyExceptionClass_Check(v)) { |
| 1857 | w = POP(); |
| 1858 | u = POP(); |
| 1859 | PyErr_Restore(v, w, u); |
| 1860 | why = WHY_RERAISE; |
| 1861 | break; |
| 1862 | } |
| 1863 | else if (v != Py_None) { |
| 1864 | PyErr_SetString(PyExc_SystemError, |
| 1865 | "'finally' pops bad exception"); |
| 1866 | why = WHY_EXCEPTION; |
| 1867 | } |
| 1868 | Py_DECREF(v); |
| 1869 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1870 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1871 | TARGET(LOAD_BUILD_CLASS) |
| 1872 | x = PyDict_GetItemString(f->f_builtins, |
| 1873 | "__build_class__"); |
| 1874 | if (x == NULL) { |
| 1875 | PyErr_SetString(PyExc_ImportError, |
| 1876 | "__build_class__ not found"); |
| 1877 | break; |
| 1878 | } |
| 1879 | Py_INCREF(x); |
| 1880 | PUSH(x); |
| 1881 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1882 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1883 | TARGET(STORE_NAME) |
| 1884 | w = GETITEM(names, oparg); |
| 1885 | v = POP(); |
| 1886 | if ((x = f->f_locals) != NULL) { |
| 1887 | if (PyDict_CheckExact(x)) |
| 1888 | err = PyDict_SetItem(x, w, v); |
| 1889 | else |
| 1890 | err = PyObject_SetItem(x, w, v); |
| 1891 | Py_DECREF(v); |
| 1892 | if (err == 0) DISPATCH(); |
| 1893 | break; |
| 1894 | } |
| 1895 | PyErr_Format(PyExc_SystemError, |
| 1896 | "no locals found when storing %R", w); |
| 1897 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1898 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1899 | TARGET(DELETE_NAME) |
| 1900 | w = GETITEM(names, oparg); |
| 1901 | if ((x = f->f_locals) != NULL) { |
| 1902 | if ((err = PyObject_DelItem(x, w)) != 0) |
| 1903 | format_exc_check_arg(PyExc_NameError, |
| 1904 | NAME_ERROR_MSG, |
| 1905 | w); |
| 1906 | break; |
| 1907 | } |
| 1908 | PyErr_Format(PyExc_SystemError, |
| 1909 | "no locals when deleting %R", w); |
| 1910 | break; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1911 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1912 | PREDICTED_WITH_ARG(UNPACK_SEQUENCE); |
| 1913 | TARGET(UNPACK_SEQUENCE) |
| 1914 | v = POP(); |
| 1915 | if (PyTuple_CheckExact(v) && |
| 1916 | PyTuple_GET_SIZE(v) == oparg) { |
| 1917 | PyObject **items = \ |
| 1918 | ((PyTupleObject *)v)->ob_item; |
| 1919 | while (oparg--) { |
| 1920 | w = items[oparg]; |
| 1921 | Py_INCREF(w); |
| 1922 | PUSH(w); |
| 1923 | } |
| 1924 | Py_DECREF(v); |
| 1925 | DISPATCH(); |
| 1926 | } else if (PyList_CheckExact(v) && |
| 1927 | PyList_GET_SIZE(v) == oparg) { |
| 1928 | PyObject **items = \ |
| 1929 | ((PyListObject *)v)->ob_item; |
| 1930 | while (oparg--) { |
| 1931 | w = items[oparg]; |
| 1932 | Py_INCREF(w); |
| 1933 | PUSH(w); |
| 1934 | } |
| 1935 | } else if (unpack_iterable(v, oparg, -1, |
| 1936 | stack_pointer + oparg)) { |
| 1937 | STACKADJ(oparg); |
| 1938 | } else { |
| 1939 | /* unpack_iterable() raised an exception */ |
| 1940 | why = WHY_EXCEPTION; |
| 1941 | } |
| 1942 | Py_DECREF(v); |
| 1943 | break; |
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 | TARGET(UNPACK_EX) |
| 1946 | { |
| 1947 | int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8); |
| 1948 | v = POP(); |
Antoine Pitrou | 9a2310d | 2008-07-25 22:39:39 +0000 | [diff] [blame] | 1949 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1950 | if (unpack_iterable(v, oparg & 0xFF, oparg >> 8, |
| 1951 | stack_pointer + totalargs)) { |
| 1952 | stack_pointer += totalargs; |
| 1953 | } else { |
| 1954 | why = WHY_EXCEPTION; |
| 1955 | } |
| 1956 | Py_DECREF(v); |
| 1957 | break; |
| 1958 | } |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 1959 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1960 | TARGET(STORE_ATTR) |
| 1961 | w = GETITEM(names, oparg); |
| 1962 | v = TOP(); |
| 1963 | u = SECOND(); |
| 1964 | STACKADJ(-2); |
| 1965 | err = PyObject_SetAttr(v, w, u); /* v.w = u */ |
| 1966 | Py_DECREF(v); |
| 1967 | Py_DECREF(u); |
| 1968 | if (err == 0) DISPATCH(); |
| 1969 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1970 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1971 | TARGET(DELETE_ATTR) |
| 1972 | w = GETITEM(names, oparg); |
| 1973 | v = POP(); |
| 1974 | err = PyObject_SetAttr(v, w, (PyObject *)NULL); |
| 1975 | /* del v.w */ |
| 1976 | Py_DECREF(v); |
| 1977 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1978 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1979 | TARGET(STORE_GLOBAL) |
| 1980 | w = GETITEM(names, oparg); |
| 1981 | v = POP(); |
| 1982 | err = PyDict_SetItem(f->f_globals, w, v); |
| 1983 | Py_DECREF(v); |
| 1984 | if (err == 0) DISPATCH(); |
| 1985 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1986 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1987 | TARGET(DELETE_GLOBAL) |
| 1988 | w = GETITEM(names, oparg); |
| 1989 | if ((err = PyDict_DelItem(f->f_globals, w)) != 0) |
| 1990 | format_exc_check_arg( |
| 1991 | PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w); |
| 1992 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1993 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1994 | TARGET(LOAD_NAME) |
| 1995 | w = GETITEM(names, oparg); |
| 1996 | if ((v = f->f_locals) == NULL) { |
| 1997 | PyErr_Format(PyExc_SystemError, |
| 1998 | "no locals when loading %R", w); |
| 1999 | why = WHY_EXCEPTION; |
| 2000 | break; |
| 2001 | } |
| 2002 | if (PyDict_CheckExact(v)) { |
| 2003 | x = PyDict_GetItem(v, w); |
| 2004 | Py_XINCREF(x); |
| 2005 | } |
| 2006 | else { |
| 2007 | x = PyObject_GetItem(v, w); |
| 2008 | if (x == NULL && PyErr_Occurred()) { |
| 2009 | if (!PyErr_ExceptionMatches( |
| 2010 | PyExc_KeyError)) |
| 2011 | break; |
| 2012 | PyErr_Clear(); |
| 2013 | } |
| 2014 | } |
| 2015 | if (x == NULL) { |
Benjamin Peterson | 20f9c3c | 2010-07-20 22:39:34 +0000 | [diff] [blame] | 2016 | x = PyDict_GetItem(f->f_globals, w); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2017 | if (x == NULL) { |
Benjamin Peterson | 20f9c3c | 2010-07-20 22:39:34 +0000 | [diff] [blame] | 2018 | x = PyDict_GetItem(f->f_builtins, w); |
| 2019 | if (x == NULL) { |
| 2020 | format_exc_check_arg( |
| 2021 | PyExc_NameError, |
| 2022 | NAME_ERROR_MSG, w); |
| 2023 | break; |
| 2024 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2025 | } |
| 2026 | Py_INCREF(x); |
| 2027 | } |
| 2028 | PUSH(x); |
| 2029 | DISPATCH(); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2030 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2031 | TARGET(LOAD_GLOBAL) |
| 2032 | w = GETITEM(names, oparg); |
| 2033 | if (PyUnicode_CheckExact(w)) { |
| 2034 | /* Inline the PyDict_GetItem() calls. |
| 2035 | WARNING: this is an extreme speed hack. |
| 2036 | Do not try this at home. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2037 | Py_hash_t hash = ((PyASCIIObject *)w)->hash; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2038 | if (hash != -1) { |
| 2039 | PyDictObject *d; |
| 2040 | PyDictEntry *e; |
| 2041 | d = (PyDictObject *)(f->f_globals); |
| 2042 | e = d->ma_lookup(d, w, hash); |
| 2043 | if (e == NULL) { |
| 2044 | x = NULL; |
| 2045 | break; |
| 2046 | } |
| 2047 | x = e->me_value; |
| 2048 | if (x != NULL) { |
| 2049 | Py_INCREF(x); |
| 2050 | PUSH(x); |
| 2051 | DISPATCH(); |
| 2052 | } |
| 2053 | d = (PyDictObject *)(f->f_builtins); |
| 2054 | e = d->ma_lookup(d, w, hash); |
| 2055 | if (e == NULL) { |
| 2056 | x = NULL; |
| 2057 | break; |
| 2058 | } |
| 2059 | x = e->me_value; |
| 2060 | if (x != NULL) { |
| 2061 | Py_INCREF(x); |
| 2062 | PUSH(x); |
| 2063 | DISPATCH(); |
| 2064 | } |
| 2065 | goto load_global_error; |
| 2066 | } |
| 2067 | } |
| 2068 | /* This is the un-inlined version of the code above */ |
| 2069 | x = PyDict_GetItem(f->f_globals, w); |
| 2070 | if (x == NULL) { |
| 2071 | x = PyDict_GetItem(f->f_builtins, w); |
| 2072 | if (x == NULL) { |
| 2073 | load_global_error: |
| 2074 | format_exc_check_arg( |
| 2075 | PyExc_NameError, |
| 2076 | GLOBAL_NAME_ERROR_MSG, w); |
| 2077 | break; |
| 2078 | } |
| 2079 | } |
| 2080 | Py_INCREF(x); |
| 2081 | PUSH(x); |
| 2082 | DISPATCH(); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2083 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2084 | TARGET(DELETE_FAST) |
| 2085 | x = GETLOCAL(oparg); |
| 2086 | if (x != NULL) { |
| 2087 | SETLOCAL(oparg, NULL); |
| 2088 | DISPATCH(); |
| 2089 | } |
| 2090 | format_exc_check_arg( |
| 2091 | PyExc_UnboundLocalError, |
| 2092 | UNBOUNDLOCAL_ERROR_MSG, |
| 2093 | PyTuple_GetItem(co->co_varnames, oparg) |
| 2094 | ); |
| 2095 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2096 | |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 2097 | TARGET(DELETE_DEREF) |
| 2098 | x = freevars[oparg]; |
| 2099 | if (PyCell_GET(x) != NULL) { |
| 2100 | PyCell_Set(x, NULL); |
Benjamin Peterson | 00ebe2c | 2010-09-10 22:02:31 +0000 | [diff] [blame] | 2101 | DISPATCH(); |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 2102 | } |
| 2103 | err = -1; |
| 2104 | format_exc_unbound(co, oparg); |
| 2105 | break; |
| 2106 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2107 | TARGET(LOAD_CLOSURE) |
| 2108 | x = freevars[oparg]; |
| 2109 | Py_INCREF(x); |
| 2110 | PUSH(x); |
| 2111 | if (x != NULL) DISPATCH(); |
| 2112 | break; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2113 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2114 | TARGET(LOAD_DEREF) |
| 2115 | x = freevars[oparg]; |
| 2116 | w = PyCell_Get(x); |
| 2117 | if (w != NULL) { |
| 2118 | PUSH(w); |
| 2119 | DISPATCH(); |
| 2120 | } |
| 2121 | err = -1; |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 2122 | format_exc_unbound(co, oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2123 | break; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2124 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2125 | TARGET(STORE_DEREF) |
| 2126 | w = POP(); |
| 2127 | x = freevars[oparg]; |
| 2128 | PyCell_Set(x, w); |
| 2129 | Py_DECREF(w); |
| 2130 | DISPATCH(); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2131 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2132 | TARGET(BUILD_TUPLE) |
| 2133 | x = PyTuple_New(oparg); |
| 2134 | if (x != NULL) { |
| 2135 | for (; --oparg >= 0;) { |
| 2136 | w = POP(); |
| 2137 | PyTuple_SET_ITEM(x, oparg, w); |
| 2138 | } |
| 2139 | PUSH(x); |
| 2140 | DISPATCH(); |
| 2141 | } |
| 2142 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2143 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2144 | TARGET(BUILD_LIST) |
| 2145 | x = PyList_New(oparg); |
| 2146 | if (x != NULL) { |
| 2147 | for (; --oparg >= 0;) { |
| 2148 | w = POP(); |
| 2149 | PyList_SET_ITEM(x, oparg, w); |
| 2150 | } |
| 2151 | PUSH(x); |
| 2152 | DISPATCH(); |
| 2153 | } |
| 2154 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2155 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2156 | TARGET(BUILD_SET) |
| 2157 | x = PySet_New(NULL); |
| 2158 | if (x != NULL) { |
| 2159 | for (; --oparg >= 0;) { |
| 2160 | w = POP(); |
| 2161 | if (err == 0) |
| 2162 | err = PySet_Add(x, w); |
| 2163 | Py_DECREF(w); |
| 2164 | } |
| 2165 | if (err != 0) { |
| 2166 | Py_DECREF(x); |
| 2167 | break; |
| 2168 | } |
| 2169 | PUSH(x); |
| 2170 | DISPATCH(); |
| 2171 | } |
| 2172 | break; |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 2173 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2174 | TARGET(BUILD_MAP) |
| 2175 | x = _PyDict_NewPresized((Py_ssize_t)oparg); |
| 2176 | PUSH(x); |
| 2177 | if (x != NULL) DISPATCH(); |
| 2178 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2179 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2180 | TARGET(STORE_MAP) |
| 2181 | w = TOP(); /* key */ |
| 2182 | u = SECOND(); /* value */ |
| 2183 | v = THIRD(); /* dict */ |
| 2184 | STACKADJ(-2); |
| 2185 | assert (PyDict_CheckExact(v)); |
| 2186 | err = PyDict_SetItem(v, w, u); /* v[w] = u */ |
| 2187 | Py_DECREF(u); |
| 2188 | Py_DECREF(w); |
| 2189 | if (err == 0) DISPATCH(); |
| 2190 | break; |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 2191 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2192 | TARGET(MAP_ADD) |
| 2193 | w = TOP(); /* key */ |
| 2194 | u = SECOND(); /* value */ |
| 2195 | STACKADJ(-2); |
| 2196 | v = stack_pointer[-oparg]; /* dict */ |
| 2197 | assert (PyDict_CheckExact(v)); |
| 2198 | err = PyDict_SetItem(v, w, u); /* v[w] = u */ |
| 2199 | Py_DECREF(u); |
| 2200 | Py_DECREF(w); |
| 2201 | if (err == 0) { |
| 2202 | PREDICT(JUMP_ABSOLUTE); |
| 2203 | DISPATCH(); |
| 2204 | } |
| 2205 | break; |
Antoine Pitrou | f289ae6 | 2008-12-18 11:06:25 +0000 | [diff] [blame] | 2206 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2207 | TARGET(LOAD_ATTR) |
| 2208 | w = GETITEM(names, oparg); |
| 2209 | v = TOP(); |
| 2210 | x = PyObject_GetAttr(v, w); |
| 2211 | Py_DECREF(v); |
| 2212 | SET_TOP(x); |
| 2213 | if (x != NULL) DISPATCH(); |
| 2214 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2215 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2216 | TARGET(COMPARE_OP) |
| 2217 | w = POP(); |
| 2218 | v = TOP(); |
| 2219 | x = cmp_outcome(oparg, v, w); |
| 2220 | Py_DECREF(v); |
| 2221 | Py_DECREF(w); |
| 2222 | SET_TOP(x); |
| 2223 | if (x == NULL) break; |
| 2224 | PREDICT(POP_JUMP_IF_FALSE); |
| 2225 | PREDICT(POP_JUMP_IF_TRUE); |
| 2226 | DISPATCH(); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2227 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2228 | TARGET(IMPORT_NAME) |
| 2229 | w = GETITEM(names, oparg); |
| 2230 | x = PyDict_GetItemString(f->f_builtins, "__import__"); |
| 2231 | if (x == NULL) { |
| 2232 | PyErr_SetString(PyExc_ImportError, |
| 2233 | "__import__ not found"); |
| 2234 | break; |
| 2235 | } |
| 2236 | Py_INCREF(x); |
| 2237 | v = POP(); |
| 2238 | u = TOP(); |
| 2239 | if (PyLong_AsLong(u) != -1 || PyErr_Occurred()) |
| 2240 | w = PyTuple_Pack(5, |
| 2241 | w, |
| 2242 | f->f_globals, |
| 2243 | f->f_locals == NULL ? |
| 2244 | Py_None : f->f_locals, |
| 2245 | v, |
| 2246 | u); |
| 2247 | else |
| 2248 | w = PyTuple_Pack(4, |
| 2249 | w, |
| 2250 | f->f_globals, |
| 2251 | f->f_locals == NULL ? |
| 2252 | Py_None : f->f_locals, |
| 2253 | v); |
| 2254 | Py_DECREF(v); |
| 2255 | Py_DECREF(u); |
| 2256 | if (w == NULL) { |
| 2257 | u = POP(); |
| 2258 | Py_DECREF(x); |
| 2259 | x = NULL; |
| 2260 | break; |
| 2261 | } |
| 2262 | READ_TIMESTAMP(intr0); |
| 2263 | v = x; |
| 2264 | x = PyEval_CallObject(v, w); |
| 2265 | Py_DECREF(v); |
| 2266 | READ_TIMESTAMP(intr1); |
| 2267 | Py_DECREF(w); |
| 2268 | SET_TOP(x); |
| 2269 | if (x != NULL) DISPATCH(); |
| 2270 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2271 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2272 | TARGET(IMPORT_STAR) |
| 2273 | v = POP(); |
| 2274 | PyFrame_FastToLocals(f); |
| 2275 | if ((x = f->f_locals) == NULL) { |
| 2276 | PyErr_SetString(PyExc_SystemError, |
| 2277 | "no locals found during 'import *'"); |
| 2278 | break; |
| 2279 | } |
| 2280 | READ_TIMESTAMP(intr0); |
| 2281 | err = import_all_from(x, v); |
| 2282 | READ_TIMESTAMP(intr1); |
| 2283 | PyFrame_LocalsToFast(f, 0); |
| 2284 | Py_DECREF(v); |
| 2285 | if (err == 0) DISPATCH(); |
| 2286 | break; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2287 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2288 | TARGET(IMPORT_FROM) |
| 2289 | w = GETITEM(names, oparg); |
| 2290 | v = TOP(); |
| 2291 | READ_TIMESTAMP(intr0); |
| 2292 | x = import_from(v, w); |
| 2293 | READ_TIMESTAMP(intr1); |
| 2294 | PUSH(x); |
| 2295 | if (x != NULL) DISPATCH(); |
| 2296 | break; |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2297 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2298 | TARGET(JUMP_FORWARD) |
| 2299 | JUMPBY(oparg); |
| 2300 | FAST_DISPATCH(); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2301 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2302 | PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE); |
| 2303 | TARGET(POP_JUMP_IF_FALSE) |
| 2304 | w = POP(); |
| 2305 | if (w == Py_True) { |
| 2306 | Py_DECREF(w); |
| 2307 | FAST_DISPATCH(); |
| 2308 | } |
| 2309 | if (w == Py_False) { |
| 2310 | Py_DECREF(w); |
| 2311 | JUMPTO(oparg); |
| 2312 | FAST_DISPATCH(); |
| 2313 | } |
| 2314 | err = PyObject_IsTrue(w); |
| 2315 | Py_DECREF(w); |
| 2316 | if (err > 0) |
| 2317 | err = 0; |
| 2318 | else if (err == 0) |
| 2319 | JUMPTO(oparg); |
| 2320 | else |
| 2321 | break; |
| 2322 | DISPATCH(); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2323 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2324 | PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE); |
| 2325 | TARGET(POP_JUMP_IF_TRUE) |
| 2326 | w = POP(); |
| 2327 | if (w == Py_False) { |
| 2328 | Py_DECREF(w); |
| 2329 | FAST_DISPATCH(); |
| 2330 | } |
| 2331 | if (w == Py_True) { |
| 2332 | Py_DECREF(w); |
| 2333 | JUMPTO(oparg); |
| 2334 | FAST_DISPATCH(); |
| 2335 | } |
| 2336 | err = PyObject_IsTrue(w); |
| 2337 | Py_DECREF(w); |
| 2338 | if (err > 0) { |
| 2339 | err = 0; |
| 2340 | JUMPTO(oparg); |
| 2341 | } |
| 2342 | else if (err == 0) |
| 2343 | ; |
| 2344 | else |
| 2345 | break; |
| 2346 | DISPATCH(); |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 2347 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2348 | TARGET(JUMP_IF_FALSE_OR_POP) |
| 2349 | w = TOP(); |
| 2350 | if (w == Py_True) { |
| 2351 | STACKADJ(-1); |
| 2352 | Py_DECREF(w); |
| 2353 | FAST_DISPATCH(); |
| 2354 | } |
| 2355 | if (w == Py_False) { |
| 2356 | JUMPTO(oparg); |
| 2357 | FAST_DISPATCH(); |
| 2358 | } |
| 2359 | err = PyObject_IsTrue(w); |
| 2360 | if (err > 0) { |
| 2361 | STACKADJ(-1); |
| 2362 | Py_DECREF(w); |
| 2363 | err = 0; |
| 2364 | } |
| 2365 | else if (err == 0) |
| 2366 | JUMPTO(oparg); |
| 2367 | else |
| 2368 | break; |
| 2369 | DISPATCH(); |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 2370 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2371 | TARGET(JUMP_IF_TRUE_OR_POP) |
| 2372 | w = TOP(); |
| 2373 | if (w == Py_False) { |
| 2374 | STACKADJ(-1); |
| 2375 | Py_DECREF(w); |
| 2376 | FAST_DISPATCH(); |
| 2377 | } |
| 2378 | if (w == Py_True) { |
| 2379 | JUMPTO(oparg); |
| 2380 | FAST_DISPATCH(); |
| 2381 | } |
| 2382 | err = PyObject_IsTrue(w); |
| 2383 | if (err > 0) { |
| 2384 | err = 0; |
| 2385 | JUMPTO(oparg); |
| 2386 | } |
| 2387 | else if (err == 0) { |
| 2388 | STACKADJ(-1); |
| 2389 | Py_DECREF(w); |
| 2390 | } |
| 2391 | else |
| 2392 | break; |
| 2393 | DISPATCH(); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2394 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2395 | PREDICTED_WITH_ARG(JUMP_ABSOLUTE); |
| 2396 | TARGET(JUMP_ABSOLUTE) |
| 2397 | JUMPTO(oparg); |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 2398 | #if FAST_LOOPS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2399 | /* Enabling this path speeds-up all while and for-loops by bypassing |
| 2400 | the per-loop checks for signals. By default, this should be turned-off |
| 2401 | because it prevents detection of a control-break in tight loops like |
| 2402 | "while 1: pass". Compile with this option turned-on when you need |
| 2403 | the speed-up and do not need break checking inside tight loops (ones |
| 2404 | that contain only instructions ending with FAST_DISPATCH). |
| 2405 | */ |
| 2406 | FAST_DISPATCH(); |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 2407 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2408 | DISPATCH(); |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 2409 | #endif |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2410 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2411 | TARGET(GET_ITER) |
| 2412 | /* before: [obj]; after [getiter(obj)] */ |
| 2413 | v = TOP(); |
| 2414 | x = PyObject_GetIter(v); |
| 2415 | Py_DECREF(v); |
| 2416 | if (x != NULL) { |
| 2417 | SET_TOP(x); |
| 2418 | PREDICT(FOR_ITER); |
| 2419 | DISPATCH(); |
| 2420 | } |
| 2421 | STACKADJ(-1); |
| 2422 | break; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2423 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2424 | PREDICTED_WITH_ARG(FOR_ITER); |
| 2425 | TARGET(FOR_ITER) |
| 2426 | /* before: [iter]; after: [iter, iter()] *or* [] */ |
| 2427 | v = TOP(); |
| 2428 | x = (*v->ob_type->tp_iternext)(v); |
| 2429 | if (x != NULL) { |
| 2430 | PUSH(x); |
| 2431 | PREDICT(STORE_FAST); |
| 2432 | PREDICT(UNPACK_SEQUENCE); |
| 2433 | DISPATCH(); |
| 2434 | } |
| 2435 | if (PyErr_Occurred()) { |
| 2436 | if (!PyErr_ExceptionMatches( |
| 2437 | PyExc_StopIteration)) |
| 2438 | break; |
| 2439 | PyErr_Clear(); |
| 2440 | } |
| 2441 | /* iterator ended normally */ |
| 2442 | x = v = POP(); |
| 2443 | Py_DECREF(v); |
| 2444 | JUMPBY(oparg); |
| 2445 | DISPATCH(); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2446 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2447 | TARGET(BREAK_LOOP) |
| 2448 | why = WHY_BREAK; |
| 2449 | goto fast_block_end; |
Raymond Hettinger | 2d783e9 | 2004-03-12 09:12:22 +0000 | [diff] [blame] | 2450 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2451 | TARGET(CONTINUE_LOOP) |
| 2452 | retval = PyLong_FromLong(oparg); |
| 2453 | if (!retval) { |
| 2454 | x = NULL; |
| 2455 | break; |
| 2456 | } |
| 2457 | why = WHY_CONTINUE; |
| 2458 | goto fast_block_end; |
Raymond Hettinger | 2d783e9 | 2004-03-12 09:12:22 +0000 | [diff] [blame] | 2459 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2460 | TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally) |
| 2461 | TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally) |
| 2462 | TARGET(SETUP_FINALLY) |
| 2463 | _setup_finally: |
| 2464 | /* NOTE: If you add any new block-setup opcodes that |
| 2465 | are not try/except/finally handlers, you may need |
| 2466 | to update the PyGen_NeedsFinalizing() function. |
| 2467 | */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2468 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2469 | PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg, |
| 2470 | STACK_LEVEL()); |
| 2471 | DISPATCH(); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2472 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2473 | TARGET(SETUP_WITH) |
| 2474 | { |
| 2475 | static PyObject *exit, *enter; |
| 2476 | w = TOP(); |
| 2477 | x = special_lookup(w, "__exit__", &exit); |
| 2478 | if (!x) |
| 2479 | break; |
| 2480 | SET_TOP(x); |
| 2481 | u = special_lookup(w, "__enter__", &enter); |
| 2482 | Py_DECREF(w); |
| 2483 | if (!u) { |
| 2484 | x = NULL; |
| 2485 | break; |
| 2486 | } |
| 2487 | x = PyObject_CallFunctionObjArgs(u, NULL); |
| 2488 | Py_DECREF(u); |
| 2489 | if (!x) |
| 2490 | break; |
| 2491 | /* Setup the finally block before pushing the result |
| 2492 | of __enter__ on the stack. */ |
| 2493 | PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg, |
| 2494 | STACK_LEVEL()); |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 2495 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2496 | PUSH(x); |
| 2497 | DISPATCH(); |
| 2498 | } |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 2499 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2500 | TARGET(WITH_CLEANUP) |
| 2501 | { |
| 2502 | /* At the top of the stack are 1-3 values indicating |
| 2503 | how/why we entered the finally clause: |
| 2504 | - TOP = None |
| 2505 | - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval |
| 2506 | - TOP = WHY_*; no retval below it |
| 2507 | - (TOP, SECOND, THIRD) = exc_info() |
| 2508 | (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER |
| 2509 | Below them is EXIT, the context.__exit__ bound method. |
| 2510 | In the last case, we must call |
| 2511 | EXIT(TOP, SECOND, THIRD) |
| 2512 | otherwise we must call |
| 2513 | EXIT(None, None, None) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 2514 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2515 | In the first two cases, we remove EXIT from the |
| 2516 | stack, leaving the rest in the same order. In the |
| 2517 | third case, we shift the bottom 3 values of the |
| 2518 | stack down, and replace the empty spot with NULL. |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 2519 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2520 | In addition, if the stack represents an exception, |
| 2521 | *and* the function call returns a 'true' value, we |
| 2522 | push WHY_SILENCED onto the stack. END_FINALLY will |
| 2523 | then not re-raise the exception. (But non-local |
| 2524 | gotos should still be resumed.) |
| 2525 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2526 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2527 | PyObject *exit_func; |
| 2528 | u = TOP(); |
| 2529 | if (u == Py_None) { |
| 2530 | (void)POP(); |
| 2531 | exit_func = TOP(); |
| 2532 | SET_TOP(u); |
| 2533 | v = w = Py_None; |
| 2534 | } |
| 2535 | else if (PyLong_Check(u)) { |
| 2536 | (void)POP(); |
| 2537 | switch(PyLong_AsLong(u)) { |
| 2538 | case WHY_RETURN: |
| 2539 | case WHY_CONTINUE: |
| 2540 | /* Retval in TOP. */ |
| 2541 | exit_func = SECOND(); |
| 2542 | SET_SECOND(TOP()); |
| 2543 | SET_TOP(u); |
| 2544 | break; |
| 2545 | default: |
| 2546 | exit_func = TOP(); |
| 2547 | SET_TOP(u); |
| 2548 | break; |
| 2549 | } |
| 2550 | u = v = w = Py_None; |
| 2551 | } |
| 2552 | else { |
| 2553 | PyObject *tp, *exc, *tb; |
| 2554 | PyTryBlock *block; |
| 2555 | v = SECOND(); |
| 2556 | w = THIRD(); |
| 2557 | tp = FOURTH(); |
| 2558 | exc = PEEK(5); |
| 2559 | tb = PEEK(6); |
| 2560 | exit_func = PEEK(7); |
| 2561 | SET_VALUE(7, tb); |
| 2562 | SET_VALUE(6, exc); |
| 2563 | SET_VALUE(5, tp); |
| 2564 | /* UNWIND_EXCEPT_HANDLER will pop this off. */ |
| 2565 | SET_FOURTH(NULL); |
| 2566 | /* We just shifted the stack down, so we have |
| 2567 | to tell the except handler block that the |
| 2568 | values are lower than it expects. */ |
| 2569 | block = &f->f_blockstack[f->f_iblock - 1]; |
| 2570 | assert(block->b_type == EXCEPT_HANDLER); |
| 2571 | block->b_level--; |
| 2572 | } |
| 2573 | /* XXX Not the fastest way to call it... */ |
| 2574 | x = PyObject_CallFunctionObjArgs(exit_func, u, v, w, |
| 2575 | NULL); |
| 2576 | Py_DECREF(exit_func); |
| 2577 | if (x == NULL) |
| 2578 | break; /* Go to error exit */ |
Amaury Forgeot d'Arc | 10b24e8 | 2008-12-10 23:49:33 +0000 | [diff] [blame] | 2579 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2580 | if (u != Py_None) |
| 2581 | err = PyObject_IsTrue(x); |
| 2582 | else |
| 2583 | err = 0; |
| 2584 | Py_DECREF(x); |
Amaury Forgeot d'Arc | 10b24e8 | 2008-12-10 23:49:33 +0000 | [diff] [blame] | 2585 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2586 | if (err < 0) |
| 2587 | break; /* Go to error exit */ |
| 2588 | else if (err > 0) { |
| 2589 | err = 0; |
| 2590 | /* There was an exception and a True return */ |
| 2591 | PUSH(PyLong_FromLong((long) WHY_SILENCED)); |
| 2592 | } |
| 2593 | PREDICT(END_FINALLY); |
| 2594 | break; |
| 2595 | } |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 2596 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2597 | TARGET(CALL_FUNCTION) |
| 2598 | { |
| 2599 | PyObject **sp; |
| 2600 | PCALL(PCALL_ALL); |
| 2601 | sp = stack_pointer; |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 2602 | #ifdef WITH_TSC |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2603 | x = call_function(&sp, oparg, &intr0, &intr1); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 2604 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2605 | x = call_function(&sp, oparg); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 2606 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2607 | stack_pointer = sp; |
| 2608 | PUSH(x); |
| 2609 | if (x != NULL) |
| 2610 | DISPATCH(); |
| 2611 | break; |
| 2612 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2613 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2614 | TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw) |
| 2615 | TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw) |
| 2616 | TARGET(CALL_FUNCTION_VAR_KW) |
| 2617 | _call_function_var_kw: |
| 2618 | { |
| 2619 | int na = oparg & 0xff; |
| 2620 | int nk = (oparg>>8) & 0xff; |
| 2621 | int flags = (opcode - CALL_FUNCTION) & 3; |
| 2622 | int n = na + 2 * nk; |
| 2623 | PyObject **pfunc, *func, **sp; |
| 2624 | PCALL(PCALL_ALL); |
| 2625 | if (flags & CALL_FLAG_VAR) |
| 2626 | n++; |
| 2627 | if (flags & CALL_FLAG_KW) |
| 2628 | n++; |
| 2629 | pfunc = stack_pointer - n - 1; |
| 2630 | func = *pfunc; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 2631 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2632 | if (PyMethod_Check(func) |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 2633 | && PyMethod_GET_SELF(func) != NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2634 | PyObject *self = PyMethod_GET_SELF(func); |
| 2635 | Py_INCREF(self); |
| 2636 | func = PyMethod_GET_FUNCTION(func); |
| 2637 | Py_INCREF(func); |
| 2638 | Py_DECREF(*pfunc); |
| 2639 | *pfunc = self; |
| 2640 | na++; |
Brett Cannon | b94767f | 2011-02-22 20:15:44 +0000 | [diff] [blame] | 2641 | /* n++; */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2642 | } else |
| 2643 | Py_INCREF(func); |
| 2644 | sp = stack_pointer; |
| 2645 | READ_TIMESTAMP(intr0); |
| 2646 | x = ext_do_call(func, &sp, flags, na, nk); |
| 2647 | READ_TIMESTAMP(intr1); |
| 2648 | stack_pointer = sp; |
| 2649 | Py_DECREF(func); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 2650 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2651 | while (stack_pointer > pfunc) { |
| 2652 | w = POP(); |
| 2653 | Py_DECREF(w); |
| 2654 | } |
| 2655 | PUSH(x); |
| 2656 | if (x != NULL) |
| 2657 | DISPATCH(); |
| 2658 | break; |
| 2659 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2660 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2661 | TARGET_WITH_IMPL(MAKE_CLOSURE, _make_function) |
| 2662 | TARGET(MAKE_FUNCTION) |
| 2663 | _make_function: |
| 2664 | { |
| 2665 | int posdefaults = oparg & 0xff; |
| 2666 | int kwdefaults = (oparg>>8) & 0xff; |
| 2667 | int num_annotations = (oparg >> 16) & 0x7fff; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2668 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2669 | v = POP(); /* code object */ |
| 2670 | x = PyFunction_New(v, f->f_globals); |
| 2671 | Py_DECREF(v); |
Antoine Pitrou | 9a2310d | 2008-07-25 22:39:39 +0000 | [diff] [blame] | 2672 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2673 | if (x != NULL && opcode == MAKE_CLOSURE) { |
| 2674 | v = POP(); |
| 2675 | if (PyFunction_SetClosure(x, v) != 0) { |
| 2676 | /* Can't happen unless bytecode is corrupt. */ |
| 2677 | why = WHY_EXCEPTION; |
| 2678 | } |
| 2679 | Py_DECREF(v); |
| 2680 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2681 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2682 | if (x != NULL && num_annotations > 0) { |
| 2683 | Py_ssize_t name_ix; |
| 2684 | u = POP(); /* names of args with annotations */ |
| 2685 | v = PyDict_New(); |
| 2686 | if (v == NULL) { |
| 2687 | Py_DECREF(x); |
| 2688 | x = NULL; |
| 2689 | break; |
| 2690 | } |
| 2691 | name_ix = PyTuple_Size(u); |
| 2692 | assert(num_annotations == name_ix+1); |
| 2693 | while (name_ix > 0) { |
| 2694 | --name_ix; |
| 2695 | t = PyTuple_GET_ITEM(u, name_ix); |
| 2696 | w = POP(); |
| 2697 | /* XXX(nnorwitz): check for errors */ |
| 2698 | PyDict_SetItem(v, t, w); |
| 2699 | Py_DECREF(w); |
| 2700 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2701 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2702 | if (PyFunction_SetAnnotations(x, v) != 0) { |
| 2703 | /* Can't happen unless |
| 2704 | PyFunction_SetAnnotations changes. */ |
| 2705 | why = WHY_EXCEPTION; |
| 2706 | } |
| 2707 | Py_DECREF(v); |
| 2708 | Py_DECREF(u); |
| 2709 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2710 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2711 | /* XXX Maybe this should be a separate opcode? */ |
| 2712 | if (x != NULL && posdefaults > 0) { |
| 2713 | v = PyTuple_New(posdefaults); |
| 2714 | if (v == NULL) { |
| 2715 | Py_DECREF(x); |
| 2716 | x = NULL; |
| 2717 | break; |
| 2718 | } |
| 2719 | while (--posdefaults >= 0) { |
| 2720 | w = POP(); |
| 2721 | PyTuple_SET_ITEM(v, posdefaults, w); |
| 2722 | } |
| 2723 | if (PyFunction_SetDefaults(x, v) != 0) { |
| 2724 | /* Can't happen unless |
| 2725 | PyFunction_SetDefaults changes. */ |
| 2726 | why = WHY_EXCEPTION; |
| 2727 | } |
| 2728 | Py_DECREF(v); |
| 2729 | } |
| 2730 | if (x != NULL && kwdefaults > 0) { |
| 2731 | v = PyDict_New(); |
| 2732 | if (v == NULL) { |
| 2733 | Py_DECREF(x); |
| 2734 | x = NULL; |
| 2735 | break; |
| 2736 | } |
| 2737 | while (--kwdefaults >= 0) { |
| 2738 | w = POP(); /* default value */ |
| 2739 | u = POP(); /* kw only arg name */ |
| 2740 | /* XXX(nnorwitz): check for errors */ |
| 2741 | PyDict_SetItem(v, u, w); |
| 2742 | Py_DECREF(w); |
| 2743 | Py_DECREF(u); |
| 2744 | } |
| 2745 | if (PyFunction_SetKwDefaults(x, v) != 0) { |
| 2746 | /* Can't happen unless |
| 2747 | PyFunction_SetKwDefaults changes. */ |
| 2748 | why = WHY_EXCEPTION; |
| 2749 | } |
| 2750 | Py_DECREF(v); |
| 2751 | } |
| 2752 | PUSH(x); |
| 2753 | break; |
| 2754 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2755 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2756 | TARGET(BUILD_SLICE) |
| 2757 | if (oparg == 3) |
| 2758 | w = POP(); |
| 2759 | else |
| 2760 | w = NULL; |
| 2761 | v = POP(); |
| 2762 | u = TOP(); |
| 2763 | x = PySlice_New(u, v, w); |
| 2764 | Py_DECREF(u); |
| 2765 | Py_DECREF(v); |
| 2766 | Py_XDECREF(w); |
| 2767 | SET_TOP(x); |
| 2768 | if (x != NULL) DISPATCH(); |
| 2769 | break; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2770 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2771 | TARGET(EXTENDED_ARG) |
| 2772 | opcode = NEXTOP(); |
| 2773 | oparg = oparg<<16 | NEXTARG(); |
| 2774 | goto dispatch_opcode; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2775 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 2776 | #if USE_COMPUTED_GOTOS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2777 | _unknown_opcode: |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 2778 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2779 | default: |
| 2780 | fprintf(stderr, |
| 2781 | "XXX lineno: %d, opcode: %d\n", |
| 2782 | PyFrame_GetLineNumber(f), |
| 2783 | opcode); |
| 2784 | PyErr_SetString(PyExc_SystemError, "unknown opcode"); |
| 2785 | why = WHY_EXCEPTION; |
| 2786 | break; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 2787 | |
| 2788 | #ifdef CASE_TOO_BIG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2789 | } |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 2790 | #endif |
| 2791 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2792 | } /* switch */ |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2793 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2794 | on_error: |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2795 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2796 | READ_TIMESTAMP(inst1); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 2797 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2798 | /* Quickly continue if no error occurred */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2799 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2800 | if (why == WHY_NOT) { |
| 2801 | if (err == 0 && x != NULL) { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2802 | #ifdef CHECKEXC |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2803 | /* This check is expensive! */ |
| 2804 | if (PyErr_Occurred()) |
| 2805 | fprintf(stderr, |
| 2806 | "XXX undetected error\n"); |
| 2807 | else { |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 2808 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2809 | READ_TIMESTAMP(loop1); |
| 2810 | continue; /* Normal, fast path */ |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 2811 | #ifdef CHECKEXC |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2812 | } |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 2813 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2814 | } |
| 2815 | why = WHY_EXCEPTION; |
| 2816 | x = Py_None; |
| 2817 | err = 0; |
| 2818 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2819 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2820 | /* Double-check exception status */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2821 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2822 | if (why == WHY_EXCEPTION || why == WHY_RERAISE) { |
| 2823 | if (!PyErr_Occurred()) { |
| 2824 | PyErr_SetString(PyExc_SystemError, |
| 2825 | "error return without exception set"); |
| 2826 | why = WHY_EXCEPTION; |
| 2827 | } |
| 2828 | } |
Guido van Rossum | eb894eb | 1999-03-09 16:16:45 +0000 | [diff] [blame] | 2829 | #ifdef CHECKEXC |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2830 | else { |
| 2831 | /* This check is expensive! */ |
| 2832 | if (PyErr_Occurred()) { |
| 2833 | char buf[128]; |
| 2834 | sprintf(buf, "Stack unwind with exception " |
| 2835 | "set and why=%d", why); |
| 2836 | Py_FatalError(buf); |
| 2837 | } |
| 2838 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2839 | #endif |
| 2840 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2841 | /* Log traceback info if this is a real exception */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2842 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2843 | if (why == WHY_EXCEPTION) { |
| 2844 | PyTraceBack_Here(f); |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 2845 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2846 | if (tstate->c_tracefunc != NULL) |
| 2847 | call_exc_trace(tstate->c_tracefunc, |
| 2848 | tstate->c_traceobj, f); |
| 2849 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2850 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2851 | /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2852 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2853 | if (why == WHY_RERAISE) |
| 2854 | why = WHY_EXCEPTION; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2855 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2856 | /* Unwind stacks if a (pseudo) exception occurred */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2857 | |
Raymond Hettinger | 1dd8309 | 2004-02-06 18:32:33 +0000 | [diff] [blame] | 2858 | fast_block_end: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2859 | while (why != WHY_NOT && f->f_iblock > 0) { |
| 2860 | /* Peek at the current block. */ |
| 2861 | PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1]; |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 2862 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2863 | assert(why != WHY_YIELD); |
| 2864 | if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) { |
| 2865 | why = WHY_NOT; |
| 2866 | JUMPTO(PyLong_AS_LONG(retval)); |
| 2867 | Py_DECREF(retval); |
| 2868 | break; |
| 2869 | } |
| 2870 | /* Now we have to pop the block. */ |
| 2871 | f->f_iblock--; |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 2872 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2873 | if (b->b_type == EXCEPT_HANDLER) { |
| 2874 | UNWIND_EXCEPT_HANDLER(b); |
| 2875 | continue; |
| 2876 | } |
| 2877 | UNWIND_BLOCK(b); |
| 2878 | if (b->b_type == SETUP_LOOP && why == WHY_BREAK) { |
| 2879 | why = WHY_NOT; |
| 2880 | JUMPTO(b->b_handler); |
| 2881 | break; |
| 2882 | } |
| 2883 | if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT |
| 2884 | || b->b_type == SETUP_FINALLY)) { |
| 2885 | PyObject *exc, *val, *tb; |
| 2886 | int handler = b->b_handler; |
| 2887 | /* Beware, this invalidates all b->b_* fields */ |
| 2888 | PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL()); |
| 2889 | PUSH(tstate->exc_traceback); |
| 2890 | PUSH(tstate->exc_value); |
| 2891 | if (tstate->exc_type != NULL) { |
| 2892 | PUSH(tstate->exc_type); |
| 2893 | } |
| 2894 | else { |
| 2895 | Py_INCREF(Py_None); |
| 2896 | PUSH(Py_None); |
| 2897 | } |
| 2898 | PyErr_Fetch(&exc, &val, &tb); |
| 2899 | /* Make the raw exception data |
| 2900 | available to the handler, |
| 2901 | so a program can emulate the |
| 2902 | Python main loop. */ |
| 2903 | PyErr_NormalizeException( |
| 2904 | &exc, &val, &tb); |
| 2905 | PyException_SetTraceback(val, tb); |
| 2906 | Py_INCREF(exc); |
| 2907 | tstate->exc_type = exc; |
| 2908 | Py_INCREF(val); |
| 2909 | tstate->exc_value = val; |
| 2910 | tstate->exc_traceback = tb; |
| 2911 | if (tb == NULL) |
| 2912 | tb = Py_None; |
| 2913 | Py_INCREF(tb); |
| 2914 | PUSH(tb); |
| 2915 | PUSH(val); |
| 2916 | PUSH(exc); |
| 2917 | why = WHY_NOT; |
| 2918 | JUMPTO(handler); |
| 2919 | break; |
| 2920 | } |
| 2921 | if (b->b_type == SETUP_FINALLY) { |
| 2922 | if (why & (WHY_RETURN | WHY_CONTINUE)) |
| 2923 | PUSH(retval); |
| 2924 | PUSH(PyLong_FromLong((long)why)); |
| 2925 | why = WHY_NOT; |
| 2926 | JUMPTO(b->b_handler); |
| 2927 | break; |
| 2928 | } |
| 2929 | } /* unwind stack */ |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2930 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2931 | /* End the loop if we still have an error (or return) */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2932 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2933 | if (why != WHY_NOT) |
| 2934 | break; |
| 2935 | READ_TIMESTAMP(loop1); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2936 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2937 | } /* main loop */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2938 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2939 | assert(why != WHY_YIELD); |
| 2940 | /* Pop remaining stack entries. */ |
| 2941 | while (!EMPTY()) { |
| 2942 | v = POP(); |
| 2943 | Py_XDECREF(v); |
| 2944 | } |
Guido van Rossum | 35974fb | 2001-12-06 21:28:18 +0000 | [diff] [blame] | 2945 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2946 | if (why != WHY_RETURN) |
| 2947 | retval = NULL; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2948 | |
Raymond Hettinger | 1dd8309 | 2004-02-06 18:32:33 +0000 | [diff] [blame] | 2949 | fast_yield: |
Benjamin Peterson | ac91341 | 2011-07-03 16:25:11 -0500 | [diff] [blame] | 2950 | if (co->co_flags & CO_GENERATOR && (why == WHY_YIELD || why == WHY_RETURN)) { |
| 2951 | /* The purpose of this block is to put aside the generator's exception |
| 2952 | state and restore that of the calling frame. If the current |
| 2953 | exception state is from the caller, we clear the exception values |
| 2954 | on the generator frame, so they are not swapped back in latter. The |
| 2955 | origin of the current exception state is determined by checking for |
| 2956 | except handler blocks, which we must be in iff a new exception |
| 2957 | state came into existence in this frame. (An uncaught exception |
| 2958 | would have why == WHY_EXCEPTION, and we wouldn't be here). */ |
| 2959 | int i; |
| 2960 | for (i = 0; i < f->f_iblock; i++) |
| 2961 | if (f->f_blockstack[i].b_type == EXCEPT_HANDLER) |
| 2962 | break; |
| 2963 | if (i == f->f_iblock) |
| 2964 | /* We did not create this exception. */ |
Benjamin Peterson | 8788024 | 2011-07-03 16:48:31 -0500 | [diff] [blame] | 2965 | restore_and_clear_exc_state(tstate, f); |
Benjamin Peterson | ac91341 | 2011-07-03 16:25:11 -0500 | [diff] [blame] | 2966 | else |
Benjamin Peterson | 8788024 | 2011-07-03 16:48:31 -0500 | [diff] [blame] | 2967 | swap_exc_state(tstate, f); |
Benjamin Peterson | ac91341 | 2011-07-03 16:25:11 -0500 | [diff] [blame] | 2968 | } |
Benjamin Peterson | 83195c3 | 2011-07-03 13:44:00 -0500 | [diff] [blame] | 2969 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2970 | if (tstate->use_tracing) { |
| 2971 | if (tstate->c_tracefunc) { |
| 2972 | if (why == WHY_RETURN || why == WHY_YIELD) { |
| 2973 | if (call_trace(tstate->c_tracefunc, |
| 2974 | tstate->c_traceobj, f, |
| 2975 | PyTrace_RETURN, retval)) { |
| 2976 | Py_XDECREF(retval); |
| 2977 | retval = NULL; |
| 2978 | why = WHY_EXCEPTION; |
| 2979 | } |
| 2980 | } |
| 2981 | else if (why == WHY_EXCEPTION) { |
| 2982 | call_trace_protected(tstate->c_tracefunc, |
| 2983 | tstate->c_traceobj, f, |
| 2984 | PyTrace_RETURN, NULL); |
| 2985 | } |
| 2986 | } |
| 2987 | if (tstate->c_profilefunc) { |
| 2988 | if (why == WHY_EXCEPTION) |
| 2989 | call_trace_protected(tstate->c_profilefunc, |
| 2990 | tstate->c_profileobj, f, |
| 2991 | PyTrace_RETURN, NULL); |
| 2992 | else if (call_trace(tstate->c_profilefunc, |
| 2993 | tstate->c_profileobj, f, |
| 2994 | PyTrace_RETURN, retval)) { |
| 2995 | Py_XDECREF(retval); |
| 2996 | retval = NULL; |
Brett Cannon | b94767f | 2011-02-22 20:15:44 +0000 | [diff] [blame] | 2997 | /* why = WHY_EXCEPTION; */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2998 | } |
| 2999 | } |
| 3000 | } |
Guido van Rossum | a424013 | 1997-01-21 21:18:36 +0000 | [diff] [blame] | 3001 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3002 | /* pop frame */ |
Thomas Wouters | ce272b6 | 2007-09-19 21:19:28 +0000 | [diff] [blame] | 3003 | exit_eval_frame: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3004 | Py_LeaveRecursiveCall(); |
| 3005 | tstate->frame = f->f_back; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3006 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3007 | return retval; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3008 | } |
| 3009 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3010 | static void |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3011 | format_missing(const char *kind, PyCodeObject *co, PyObject *names) |
| 3012 | { |
| 3013 | int err; |
| 3014 | Py_ssize_t len = PyList_GET_SIZE(names); |
| 3015 | PyObject *name_str, *comma, *tail, *tmp; |
| 3016 | |
| 3017 | assert(PyList_CheckExact(names)); |
| 3018 | assert(len >= 1); |
| 3019 | /* Deal with the joys of natural language. */ |
| 3020 | switch (len) { |
| 3021 | case 1: |
| 3022 | name_str = PyList_GET_ITEM(names, 0); |
| 3023 | Py_INCREF(name_str); |
| 3024 | break; |
| 3025 | case 2: |
| 3026 | name_str = PyUnicode_FromFormat("%U and %U", |
| 3027 | PyList_GET_ITEM(names, len - 2), |
| 3028 | PyList_GET_ITEM(names, len - 1)); |
| 3029 | break; |
| 3030 | default: |
| 3031 | tail = PyUnicode_FromFormat(", %U, and %U", |
| 3032 | PyList_GET_ITEM(names, len - 2), |
| 3033 | PyList_GET_ITEM(names, len - 1)); |
| 3034 | /* Chop off the last two objects in the list. This shouldn't actually |
| 3035 | fail, but we can't be too careful. */ |
| 3036 | err = PyList_SetSlice(names, len - 2, len, NULL); |
| 3037 | if (err == -1) { |
| 3038 | Py_DECREF(tail); |
| 3039 | return; |
| 3040 | } |
| 3041 | /* Stitch everything up into a nice comma-separated list. */ |
| 3042 | comma = PyUnicode_FromString(", "); |
| 3043 | if (comma == NULL) { |
| 3044 | Py_DECREF(tail); |
| 3045 | return; |
| 3046 | } |
| 3047 | tmp = PyUnicode_Join(comma, names); |
| 3048 | Py_DECREF(comma); |
| 3049 | if (tmp == NULL) { |
| 3050 | Py_DECREF(tail); |
| 3051 | return; |
| 3052 | } |
| 3053 | name_str = PyUnicode_Concat(tmp, tail); |
| 3054 | Py_DECREF(tmp); |
| 3055 | Py_DECREF(tail); |
| 3056 | break; |
| 3057 | } |
| 3058 | if (name_str == NULL) |
| 3059 | return; |
| 3060 | PyErr_Format(PyExc_TypeError, |
| 3061 | "%U() missing %i required %s argument%s: %U", |
| 3062 | co->co_name, |
| 3063 | len, |
| 3064 | kind, |
| 3065 | len == 1 ? "" : "s", |
| 3066 | name_str); |
| 3067 | Py_DECREF(name_str); |
| 3068 | } |
| 3069 | |
| 3070 | static void |
| 3071 | missing_arguments(PyCodeObject *co, int missing, int defcount, |
| 3072 | PyObject **fastlocals) |
| 3073 | { |
| 3074 | int i, j = 0; |
| 3075 | int start, end; |
| 3076 | int positional = defcount != -1; |
| 3077 | const char *kind = positional ? "positional" : "keyword-only"; |
| 3078 | PyObject *missing_names; |
| 3079 | |
| 3080 | /* Compute the names of the arguments that are missing. */ |
| 3081 | missing_names = PyList_New(missing); |
| 3082 | if (missing_names == NULL) |
| 3083 | return; |
| 3084 | if (positional) { |
| 3085 | start = 0; |
| 3086 | end = co->co_argcount - defcount; |
| 3087 | } |
| 3088 | else { |
| 3089 | start = co->co_argcount; |
| 3090 | end = start + co->co_kwonlyargcount; |
| 3091 | } |
| 3092 | for (i = start; i < end; i++) { |
| 3093 | if (GETLOCAL(i) == NULL) { |
| 3094 | PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i); |
| 3095 | PyObject *name = PyObject_Repr(raw); |
| 3096 | if (name == NULL) { |
| 3097 | Py_DECREF(missing_names); |
| 3098 | return; |
| 3099 | } |
| 3100 | PyList_SET_ITEM(missing_names, j++, name); |
| 3101 | } |
| 3102 | } |
| 3103 | assert(j == missing); |
| 3104 | format_missing(kind, co, missing_names); |
| 3105 | Py_DECREF(missing_names); |
| 3106 | } |
| 3107 | |
| 3108 | static void |
| 3109 | too_many_positional(PyCodeObject *co, int given, int defcount, PyObject **fastlocals) |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3110 | { |
| 3111 | int plural; |
| 3112 | int kwonly_given = 0; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3113 | int i; |
| 3114 | PyObject *sig, *kwonly_sig; |
| 3115 | |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3116 | assert((co->co_flags & CO_VARARGS) == 0); |
| 3117 | /* Count missing keyword-only args. */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3118 | 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] | 3119 | if (GETLOCAL(i) != NULL) |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3120 | kwonly_given++; |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3121 | if (defcount) { |
| 3122 | int atleast = co->co_argcount - defcount; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3123 | plural = 1; |
| 3124 | sig = PyUnicode_FromFormat("from %d to %d", atleast, co->co_argcount); |
| 3125 | } |
| 3126 | else { |
| 3127 | plural = co->co_argcount != 1; |
| 3128 | sig = PyUnicode_FromFormat("%d", co->co_argcount); |
| 3129 | } |
| 3130 | if (sig == NULL) |
| 3131 | return; |
| 3132 | if (kwonly_given) { |
| 3133 | const char *format = " positional argument%s (and %d keyword-only argument%s)"; |
| 3134 | kwonly_sig = PyUnicode_FromFormat(format, given != 1 ? "s" : "", kwonly_given, |
| 3135 | kwonly_given != 1 ? "s" : ""); |
| 3136 | if (kwonly_sig == NULL) { |
| 3137 | Py_DECREF(sig); |
| 3138 | return; |
| 3139 | } |
| 3140 | } |
| 3141 | else { |
| 3142 | /* This will not fail. */ |
| 3143 | kwonly_sig = PyUnicode_FromString(""); |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3144 | assert(kwonly_sig != NULL); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3145 | } |
| 3146 | PyErr_Format(PyExc_TypeError, |
| 3147 | "%U() takes %U positional argument%s but %d%U %s given", |
| 3148 | co->co_name, |
| 3149 | sig, |
| 3150 | plural ? "s" : "", |
| 3151 | given, |
| 3152 | kwonly_sig, |
| 3153 | given == 1 && !kwonly_given ? "was" : "were"); |
| 3154 | Py_DECREF(sig); |
| 3155 | Py_DECREF(kwonly_sig); |
| 3156 | } |
| 3157 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3158 | /* 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] | 3159 | PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3160 | the test in the if statements in Misc/gdbinit (pystack and pystackv). */ |
Skip Montanaro | 786ea6b | 2004-03-01 15:44:05 +0000 | [diff] [blame] | 3161 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3162 | PyObject * |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 3163 | PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3164 | PyObject **args, int argcount, PyObject **kws, int kwcount, |
| 3165 | PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure) |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3166 | { |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 3167 | PyCodeObject* co = (PyCodeObject*)_co; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3168 | register PyFrameObject *f; |
| 3169 | register PyObject *retval = NULL; |
| 3170 | register PyObject **fastlocals, **freevars; |
| 3171 | PyThreadState *tstate = PyThreadState_GET(); |
| 3172 | PyObject *x, *u; |
| 3173 | int total_args = co->co_argcount + co->co_kwonlyargcount; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3174 | int i; |
| 3175 | int n = argcount; |
| 3176 | PyObject *kwdict = NULL; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3177 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3178 | if (globals == NULL) { |
| 3179 | PyErr_SetString(PyExc_SystemError, |
| 3180 | "PyEval_EvalCodeEx: NULL globals"); |
| 3181 | return NULL; |
| 3182 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3183 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3184 | assert(tstate != NULL); |
| 3185 | assert(globals != NULL); |
| 3186 | f = PyFrame_New(tstate, co, globals, locals); |
| 3187 | if (f == NULL) |
| 3188 | return NULL; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3189 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3190 | fastlocals = f->f_localsplus; |
| 3191 | freevars = f->f_localsplus + co->co_nlocals; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3192 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3193 | /* Parse arguments. */ |
| 3194 | if (co->co_flags & CO_VARKEYWORDS) { |
| 3195 | kwdict = PyDict_New(); |
| 3196 | if (kwdict == NULL) |
| 3197 | goto fail; |
| 3198 | i = total_args; |
| 3199 | if (co->co_flags & CO_VARARGS) |
| 3200 | i++; |
| 3201 | SETLOCAL(i, kwdict); |
| 3202 | } |
| 3203 | if (argcount > co->co_argcount) |
| 3204 | n = co->co_argcount; |
| 3205 | for (i = 0; i < n; i++) { |
| 3206 | x = args[i]; |
| 3207 | Py_INCREF(x); |
| 3208 | SETLOCAL(i, x); |
| 3209 | } |
| 3210 | if (co->co_flags & CO_VARARGS) { |
| 3211 | u = PyTuple_New(argcount - n); |
| 3212 | if (u == NULL) |
| 3213 | goto fail; |
| 3214 | SETLOCAL(total_args, u); |
| 3215 | for (i = n; i < argcount; i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3216 | x = args[i]; |
| 3217 | Py_INCREF(x); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3218 | PyTuple_SET_ITEM(u, i-n, x); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3219 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3220 | } |
| 3221 | for (i = 0; i < kwcount; i++) { |
| 3222 | PyObject **co_varnames; |
| 3223 | PyObject *keyword = kws[2*i]; |
| 3224 | PyObject *value = kws[2*i + 1]; |
| 3225 | int j; |
| 3226 | if (keyword == NULL || !PyUnicode_Check(keyword)) { |
| 3227 | PyErr_Format(PyExc_TypeError, |
| 3228 | "%U() keywords must be strings", |
| 3229 | co->co_name); |
| 3230 | goto fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3231 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3232 | /* Speed hack: do raw pointer compares. As names are |
| 3233 | normally interned this should almost always hit. */ |
| 3234 | co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item; |
| 3235 | for (j = 0; j < total_args; j++) { |
| 3236 | PyObject *nm = co_varnames[j]; |
| 3237 | if (nm == keyword) |
| 3238 | goto kw_found; |
| 3239 | } |
| 3240 | /* Slow fallback, just in case */ |
| 3241 | for (j = 0; j < total_args; j++) { |
| 3242 | PyObject *nm = co_varnames[j]; |
| 3243 | int cmp = PyObject_RichCompareBool( |
| 3244 | keyword, nm, Py_EQ); |
| 3245 | if (cmp > 0) |
| 3246 | goto kw_found; |
| 3247 | else if (cmp < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3248 | goto fail; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3249 | } |
| 3250 | if (j >= total_args && kwdict == NULL) { |
| 3251 | PyErr_Format(PyExc_TypeError, |
| 3252 | "%U() got an unexpected " |
| 3253 | "keyword argument '%S'", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3254 | co->co_name, |
| 3255 | keyword); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3256 | goto fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3257 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3258 | PyDict_SetItem(kwdict, keyword, value); |
| 3259 | continue; |
| 3260 | kw_found: |
| 3261 | if (GETLOCAL(j) != NULL) { |
| 3262 | PyErr_Format(PyExc_TypeError, |
| 3263 | "%U() got multiple " |
| 3264 | "values for argument '%S'", |
| 3265 | co->co_name, |
| 3266 | keyword); |
| 3267 | goto fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3268 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3269 | Py_INCREF(value); |
| 3270 | SETLOCAL(j, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3271 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3272 | if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) { |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3273 | too_many_positional(co, argcount, defcount, fastlocals); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3274 | goto fail; |
| 3275 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3276 | if (argcount < co->co_argcount) { |
| 3277 | int m = co->co_argcount - defcount; |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3278 | int missing = 0; |
| 3279 | for (i = argcount; i < m; i++) |
| 3280 | if (GETLOCAL(i) == NULL) |
| 3281 | missing++; |
| 3282 | if (missing) { |
| 3283 | missing_arguments(co, missing, defcount, fastlocals); |
| 3284 | goto fail; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3285 | } |
| 3286 | if (n > m) |
| 3287 | i = n - m; |
| 3288 | else |
| 3289 | i = 0; |
| 3290 | for (; i < defcount; i++) { |
| 3291 | if (GETLOCAL(m+i) == NULL) { |
| 3292 | PyObject *def = defs[i]; |
| 3293 | Py_INCREF(def); |
| 3294 | SETLOCAL(m+i, def); |
| 3295 | } |
| 3296 | } |
| 3297 | } |
| 3298 | if (co->co_kwonlyargcount > 0) { |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3299 | int missing = 0; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3300 | for (i = co->co_argcount; i < total_args; i++) { |
| 3301 | PyObject *name; |
| 3302 | if (GETLOCAL(i) != NULL) |
| 3303 | continue; |
| 3304 | name = PyTuple_GET_ITEM(co->co_varnames, i); |
| 3305 | if (kwdefs != NULL) { |
| 3306 | PyObject *def = PyDict_GetItem(kwdefs, name); |
| 3307 | if (def) { |
| 3308 | Py_INCREF(def); |
| 3309 | SETLOCAL(i, def); |
| 3310 | continue; |
| 3311 | } |
| 3312 | } |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3313 | missing++; |
| 3314 | } |
| 3315 | if (missing) { |
| 3316 | missing_arguments(co, missing, -1, fastlocals); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3317 | goto fail; |
| 3318 | } |
| 3319 | } |
| 3320 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3321 | /* Allocate and initialize storage for cell vars, and copy free |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 3322 | vars into frame. */ |
| 3323 | for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3324 | PyObject *c; |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 3325 | int arg; |
| 3326 | /* Possibly account for the cell variable being an argument. */ |
| 3327 | if (co->co_cell2arg != NULL && |
| 3328 | (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) |
| 3329 | c = PyCell_New(GETLOCAL(arg)); |
| 3330 | else |
| 3331 | c = PyCell_New(NULL); |
| 3332 | if (c == NULL) |
| 3333 | goto fail; |
| 3334 | SETLOCAL(co->co_nlocals + i, c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3335 | } |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 3336 | for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) { |
| 3337 | PyObject *o = PyTuple_GET_ITEM(closure, i); |
| 3338 | Py_INCREF(o); |
| 3339 | freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3340 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3341 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3342 | if (co->co_flags & CO_GENERATOR) { |
| 3343 | /* Don't need to keep the reference to f_back, it will be set |
| 3344 | * when the generator is resumed. */ |
| 3345 | Py_XDECREF(f->f_back); |
| 3346 | f->f_back = NULL; |
Neil Schemenauer | 2b13ce8 | 2001-06-21 02:41:10 +0000 | [diff] [blame] | 3347 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3348 | PCALL(PCALL_GENERATOR); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 3349 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3350 | /* Create a new generator that owns the ready to run frame |
| 3351 | * and return that as the value. */ |
| 3352 | return PyGen_New(f); |
| 3353 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3354 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3355 | retval = PyEval_EvalFrameEx(f,0); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3356 | |
Thomas Wouters | ce272b6 | 2007-09-19 21:19:28 +0000 | [diff] [blame] | 3357 | fail: /* Jump here from prelude on failure */ |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3358 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3359 | /* decref'ing the frame can cause __del__ methods to get invoked, |
| 3360 | which can call back into Python. While we're done with the |
| 3361 | current Python frame (f), the associated C stack is still in use, |
| 3362 | so recursion_depth must be boosted for the duration. |
| 3363 | */ |
| 3364 | assert(tstate != NULL); |
| 3365 | ++tstate->recursion_depth; |
| 3366 | Py_DECREF(f); |
| 3367 | --tstate->recursion_depth; |
| 3368 | return retval; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3369 | } |
| 3370 | |
| 3371 | |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 3372 | static PyObject * |
| 3373 | special_lookup(PyObject *o, char *meth, PyObject **cache) |
| 3374 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3375 | PyObject *res; |
| 3376 | res = _PyObject_LookupSpecial(o, meth, cache); |
| 3377 | if (res == NULL && !PyErr_Occurred()) { |
| 3378 | PyErr_SetObject(PyExc_AttributeError, *cache); |
| 3379 | return NULL; |
| 3380 | } |
| 3381 | return res; |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 3382 | } |
| 3383 | |
| 3384 | |
Benjamin Peterson | 8788024 | 2011-07-03 16:48:31 -0500 | [diff] [blame] | 3385 | /* These 3 functions deal with the exception state of generators. */ |
| 3386 | |
| 3387 | static void |
| 3388 | save_exc_state(PyThreadState *tstate, PyFrameObject *f) |
| 3389 | { |
| 3390 | PyObject *type, *value, *traceback; |
| 3391 | Py_XINCREF(tstate->exc_type); |
| 3392 | Py_XINCREF(tstate->exc_value); |
| 3393 | Py_XINCREF(tstate->exc_traceback); |
| 3394 | type = f->f_exc_type; |
| 3395 | value = f->f_exc_value; |
| 3396 | traceback = f->f_exc_traceback; |
| 3397 | f->f_exc_type = tstate->exc_type; |
| 3398 | f->f_exc_value = tstate->exc_value; |
| 3399 | f->f_exc_traceback = tstate->exc_traceback; |
| 3400 | Py_XDECREF(type); |
| 3401 | Py_XDECREF(value); |
| 3402 | Py_XDECREF(traceback); |
| 3403 | } |
| 3404 | |
| 3405 | static void |
| 3406 | swap_exc_state(PyThreadState *tstate, PyFrameObject *f) |
| 3407 | { |
| 3408 | PyObject *tmp; |
| 3409 | tmp = tstate->exc_type; |
| 3410 | tstate->exc_type = f->f_exc_type; |
| 3411 | f->f_exc_type = tmp; |
| 3412 | tmp = tstate->exc_value; |
| 3413 | tstate->exc_value = f->f_exc_value; |
| 3414 | f->f_exc_value = tmp; |
| 3415 | tmp = tstate->exc_traceback; |
| 3416 | tstate->exc_traceback = f->f_exc_traceback; |
| 3417 | f->f_exc_traceback = tmp; |
| 3418 | } |
| 3419 | |
| 3420 | static void |
| 3421 | restore_and_clear_exc_state(PyThreadState *tstate, PyFrameObject *f) |
| 3422 | { |
| 3423 | PyObject *type, *value, *tb; |
| 3424 | type = tstate->exc_type; |
| 3425 | value = tstate->exc_value; |
| 3426 | tb = tstate->exc_traceback; |
| 3427 | tstate->exc_type = f->f_exc_type; |
| 3428 | tstate->exc_value = f->f_exc_value; |
| 3429 | tstate->exc_traceback = f->f_exc_traceback; |
| 3430 | f->f_exc_type = NULL; |
| 3431 | f->f_exc_value = NULL; |
| 3432 | f->f_exc_traceback = NULL; |
| 3433 | Py_XDECREF(type); |
| 3434 | Py_XDECREF(value); |
| 3435 | Py_XDECREF(tb); |
| 3436 | } |
| 3437 | |
| 3438 | |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3439 | /* Logic for the raise statement (too complicated for inlining). |
| 3440 | This *consumes* a reference count to each of its arguments. */ |
Raymond Hettinger | 7c95865 | 2004-04-06 10:11:10 +0000 | [diff] [blame] | 3441 | static enum why_code |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 3442 | do_raise(PyObject *exc, PyObject *cause) |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3443 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3444 | PyObject *type = NULL, *value = NULL; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 3445 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3446 | if (exc == NULL) { |
| 3447 | /* Reraise */ |
| 3448 | PyThreadState *tstate = PyThreadState_GET(); |
| 3449 | PyObject *tb; |
| 3450 | type = tstate->exc_type; |
| 3451 | value = tstate->exc_value; |
| 3452 | tb = tstate->exc_traceback; |
| 3453 | if (type == Py_None) { |
| 3454 | PyErr_SetString(PyExc_RuntimeError, |
| 3455 | "No active exception to reraise"); |
| 3456 | return WHY_EXCEPTION; |
| 3457 | } |
| 3458 | Py_XINCREF(type); |
| 3459 | Py_XINCREF(value); |
| 3460 | Py_XINCREF(tb); |
| 3461 | PyErr_Restore(type, value, tb); |
| 3462 | return WHY_RERAISE; |
| 3463 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3464 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3465 | /* We support the following forms of raise: |
| 3466 | raise |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 3467 | raise <instance> |
| 3468 | raise <type> */ |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3469 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3470 | if (PyExceptionClass_Check(exc)) { |
| 3471 | type = exc; |
| 3472 | value = PyObject_CallObject(exc, NULL); |
| 3473 | if (value == NULL) |
| 3474 | goto raise_error; |
Benjamin Peterson | 5afa03a | 2011-07-15 14:09:26 -0500 | [diff] [blame] | 3475 | if (!PyExceptionInstance_Check(value)) { |
| 3476 | PyErr_Format(PyExc_TypeError, |
| 3477 | "calling %R should have returned an instance of " |
| 3478 | "BaseException, not %R", |
| 3479 | type, Py_TYPE(value)); |
| 3480 | goto raise_error; |
| 3481 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3482 | } |
| 3483 | else if (PyExceptionInstance_Check(exc)) { |
| 3484 | value = exc; |
| 3485 | type = PyExceptionInstance_Class(exc); |
| 3486 | Py_INCREF(type); |
| 3487 | } |
| 3488 | else { |
| 3489 | /* Not something you can raise. You get an exception |
| 3490 | anyway, just not what you specified :-) */ |
| 3491 | Py_DECREF(exc); |
| 3492 | PyErr_SetString(PyExc_TypeError, |
| 3493 | "exceptions must derive from BaseException"); |
| 3494 | goto raise_error; |
| 3495 | } |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 3496 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3497 | if (cause) { |
| 3498 | PyObject *fixed_cause; |
| 3499 | if (PyExceptionClass_Check(cause)) { |
| 3500 | fixed_cause = PyObject_CallObject(cause, NULL); |
| 3501 | if (fixed_cause == NULL) |
| 3502 | goto raise_error; |
| 3503 | Py_DECREF(cause); |
| 3504 | } |
| 3505 | else if (PyExceptionInstance_Check(cause)) { |
| 3506 | fixed_cause = cause; |
| 3507 | } |
| 3508 | else { |
| 3509 | PyErr_SetString(PyExc_TypeError, |
| 3510 | "exception causes must derive from " |
| 3511 | "BaseException"); |
| 3512 | goto raise_error; |
| 3513 | } |
| 3514 | PyException_SetCause(value, fixed_cause); |
| 3515 | } |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 3516 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3517 | PyErr_SetObject(type, value); |
| 3518 | /* PyErr_SetObject incref's its arguments */ |
| 3519 | Py_XDECREF(value); |
| 3520 | Py_XDECREF(type); |
| 3521 | return WHY_EXCEPTION; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 3522 | |
| 3523 | raise_error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3524 | Py_XDECREF(value); |
| 3525 | Py_XDECREF(type); |
| 3526 | Py_XDECREF(cause); |
| 3527 | return WHY_EXCEPTION; |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3528 | } |
| 3529 | |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 3530 | /* 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] | 3531 | sp). Return 1 for success, 0 if error. |
Antoine Pitrou | 9a2310d | 2008-07-25 22:39:39 +0000 | [diff] [blame] | 3532 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 3533 | If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack |
| 3534 | with a variable target. |
| 3535 | */ |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 3536 | |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 3537 | static int |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 3538 | unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp) |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 3539 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3540 | int i = 0, j = 0; |
| 3541 | Py_ssize_t ll = 0; |
| 3542 | PyObject *it; /* iter(v) */ |
| 3543 | PyObject *w; |
| 3544 | PyObject *l = NULL; /* variable list */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3545 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3546 | assert(v != NULL); |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 3547 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3548 | it = PyObject_GetIter(v); |
| 3549 | if (it == NULL) |
| 3550 | goto Error; |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 3551 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3552 | for (; i < argcnt; i++) { |
| 3553 | w = PyIter_Next(it); |
| 3554 | if (w == NULL) { |
| 3555 | /* Iterator done, via error or exhaustion. */ |
| 3556 | if (!PyErr_Occurred()) { |
| 3557 | PyErr_Format(PyExc_ValueError, |
| 3558 | "need more than %d value%s to unpack", |
| 3559 | i, i == 1 ? "" : "s"); |
| 3560 | } |
| 3561 | goto Error; |
| 3562 | } |
| 3563 | *--sp = w; |
| 3564 | } |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 3565 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3566 | if (argcntafter == -1) { |
| 3567 | /* We better have exhausted the iterator now. */ |
| 3568 | w = PyIter_Next(it); |
| 3569 | if (w == NULL) { |
| 3570 | if (PyErr_Occurred()) |
| 3571 | goto Error; |
| 3572 | Py_DECREF(it); |
| 3573 | return 1; |
| 3574 | } |
| 3575 | Py_DECREF(w); |
Georg Brandl | 0310a83 | 2010-07-10 10:32:36 +0000 | [diff] [blame] | 3576 | PyErr_Format(PyExc_ValueError, "too many values to unpack " |
| 3577 | "(expected %d)", argcnt); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3578 | goto Error; |
| 3579 | } |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 3580 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3581 | l = PySequence_List(it); |
| 3582 | if (l == NULL) |
| 3583 | goto Error; |
| 3584 | *--sp = l; |
| 3585 | i++; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 3586 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3587 | ll = PyList_GET_SIZE(l); |
| 3588 | if (ll < argcntafter) { |
| 3589 | PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack", |
| 3590 | argcnt + ll); |
| 3591 | goto Error; |
| 3592 | } |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 3593 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3594 | /* Pop the "after-variable" args off the list. */ |
| 3595 | for (j = argcntafter; j > 0; j--, i++) { |
| 3596 | *--sp = PyList_GET_ITEM(l, ll - j); |
| 3597 | } |
| 3598 | /* Resize the list. */ |
| 3599 | Py_SIZE(l) = ll - argcntafter; |
| 3600 | Py_DECREF(it); |
| 3601 | return 1; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 3602 | |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 3603 | Error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3604 | for (; i > 0; i--, sp++) |
| 3605 | Py_DECREF(*sp); |
| 3606 | Py_XDECREF(it); |
| 3607 | return 0; |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 3608 | } |
| 3609 | |
| 3610 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 3611 | #ifdef LLTRACE |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3612 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3613 | prtrace(PyObject *v, char *str) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3614 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3615 | printf("%s ", str); |
| 3616 | if (PyObject_Print(v, stdout, 0) != 0) |
| 3617 | PyErr_Clear(); /* Don't know what else to do */ |
| 3618 | printf("\n"); |
| 3619 | return 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3620 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3621 | #endif |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3622 | |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 3623 | static void |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3624 | call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f) |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 3625 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3626 | PyObject *type, *value, *traceback, *arg; |
| 3627 | int err; |
| 3628 | PyErr_Fetch(&type, &value, &traceback); |
| 3629 | if (value == NULL) { |
| 3630 | value = Py_None; |
| 3631 | Py_INCREF(value); |
| 3632 | } |
| 3633 | arg = PyTuple_Pack(3, type, value, traceback); |
| 3634 | if (arg == NULL) { |
| 3635 | PyErr_Restore(type, value, traceback); |
| 3636 | return; |
| 3637 | } |
| 3638 | err = call_trace(func, self, f, PyTrace_EXCEPTION, arg); |
| 3639 | Py_DECREF(arg); |
| 3640 | if (err == 0) |
| 3641 | PyErr_Restore(type, value, traceback); |
| 3642 | else { |
| 3643 | Py_XDECREF(type); |
| 3644 | Py_XDECREF(value); |
| 3645 | Py_XDECREF(traceback); |
| 3646 | } |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 3647 | } |
| 3648 | |
Amaury Forgeot d'Arc | f05149a | 2007-11-13 01:05:30 +0000 | [diff] [blame] | 3649 | static int |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 3650 | call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3651 | int what, PyObject *arg) |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 3652 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3653 | PyObject *type, *value, *traceback; |
| 3654 | int err; |
| 3655 | PyErr_Fetch(&type, &value, &traceback); |
| 3656 | err = call_trace(func, obj, frame, what, arg); |
| 3657 | if (err == 0) |
| 3658 | { |
| 3659 | PyErr_Restore(type, value, traceback); |
| 3660 | return 0; |
| 3661 | } |
| 3662 | else { |
| 3663 | Py_XDECREF(type); |
| 3664 | Py_XDECREF(value); |
| 3665 | Py_XDECREF(traceback); |
| 3666 | return -1; |
| 3667 | } |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 3668 | } |
| 3669 | |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 3670 | static int |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3671 | call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3672 | int what, PyObject *arg) |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 3673 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3674 | register PyThreadState *tstate = frame->f_tstate; |
| 3675 | int result; |
| 3676 | if (tstate->tracing) |
| 3677 | return 0; |
| 3678 | tstate->tracing++; |
| 3679 | tstate->use_tracing = 0; |
| 3680 | result = func(obj, frame, what, arg); |
| 3681 | tstate->use_tracing = ((tstate->c_tracefunc != NULL) |
| 3682 | || (tstate->c_profilefunc != NULL)); |
| 3683 | tstate->tracing--; |
| 3684 | return result; |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 3685 | } |
| 3686 | |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 3687 | PyObject * |
| 3688 | _PyEval_CallTracing(PyObject *func, PyObject *args) |
| 3689 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3690 | PyFrameObject *frame = PyEval_GetFrame(); |
| 3691 | PyThreadState *tstate = frame->f_tstate; |
| 3692 | int save_tracing = tstate->tracing; |
| 3693 | int save_use_tracing = tstate->use_tracing; |
| 3694 | PyObject *result; |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 3695 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3696 | tstate->tracing = 0; |
| 3697 | tstate->use_tracing = ((tstate->c_tracefunc != NULL) |
| 3698 | || (tstate->c_profilefunc != NULL)); |
| 3699 | result = PyObject_Call(func, args, NULL); |
| 3700 | tstate->tracing = save_tracing; |
| 3701 | tstate->use_tracing = save_use_tracing; |
| 3702 | return result; |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 3703 | } |
| 3704 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 3705 | /* 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] | 3706 | static int |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 3707 | maybe_call_line_trace(Py_tracefunc func, PyObject *obj, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3708 | PyFrameObject *frame, int *instr_lb, int *instr_ub, |
| 3709 | int *instr_prev) |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 3710 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3711 | int result = 0; |
| 3712 | int line = frame->f_lineno; |
Michael W. Hudson | 006c752 | 2002-11-08 13:08:46 +0000 | [diff] [blame] | 3713 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3714 | /* If the last instruction executed isn't in the current |
| 3715 | instruction window, reset the window. |
| 3716 | */ |
| 3717 | if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) { |
| 3718 | PyAddrPair bounds; |
| 3719 | line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti, |
| 3720 | &bounds); |
| 3721 | *instr_lb = bounds.ap_lower; |
| 3722 | *instr_ub = bounds.ap_upper; |
| 3723 | } |
| 3724 | /* If the last instruction falls at the start of a line or if |
| 3725 | it represents a jump backwards, update the frame's line |
| 3726 | number and call the trace function. */ |
| 3727 | if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) { |
| 3728 | frame->f_lineno = line; |
| 3729 | result = call_trace(func, obj, frame, PyTrace_LINE, Py_None); |
| 3730 | } |
| 3731 | *instr_prev = frame->f_lasti; |
| 3732 | return result; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 3733 | } |
| 3734 | |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3735 | void |
| 3736 | PyEval_SetProfile(Py_tracefunc func, PyObject *arg) |
Fred Drake | d083839 | 2001-06-16 21:02:31 +0000 | [diff] [blame] | 3737 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3738 | PyThreadState *tstate = PyThreadState_GET(); |
| 3739 | PyObject *temp = tstate->c_profileobj; |
| 3740 | Py_XINCREF(arg); |
| 3741 | tstate->c_profilefunc = NULL; |
| 3742 | tstate->c_profileobj = NULL; |
| 3743 | /* Must make sure that tracing is not ignored if 'temp' is freed */ |
| 3744 | tstate->use_tracing = tstate->c_tracefunc != NULL; |
| 3745 | Py_XDECREF(temp); |
| 3746 | tstate->c_profilefunc = func; |
| 3747 | tstate->c_profileobj = arg; |
| 3748 | /* Flag that tracing or profiling is turned on */ |
| 3749 | tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL); |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3750 | } |
| 3751 | |
| 3752 | void |
| 3753 | PyEval_SetTrace(Py_tracefunc func, PyObject *arg) |
| 3754 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3755 | PyThreadState *tstate = PyThreadState_GET(); |
| 3756 | PyObject *temp = tstate->c_traceobj; |
| 3757 | _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL); |
| 3758 | Py_XINCREF(arg); |
| 3759 | tstate->c_tracefunc = NULL; |
| 3760 | tstate->c_traceobj = NULL; |
| 3761 | /* Must make sure that profiling is not ignored if 'temp' is freed */ |
| 3762 | tstate->use_tracing = tstate->c_profilefunc != NULL; |
| 3763 | Py_XDECREF(temp); |
| 3764 | tstate->c_tracefunc = func; |
| 3765 | tstate->c_traceobj = arg; |
| 3766 | /* Flag that tracing or profiling is turned on */ |
| 3767 | tstate->use_tracing = ((func != NULL) |
| 3768 | || (tstate->c_profilefunc != NULL)); |
Fred Drake | d083839 | 2001-06-16 21:02:31 +0000 | [diff] [blame] | 3769 | } |
| 3770 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3771 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3772 | PyEval_GetBuiltins(void) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 3773 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3774 | PyFrameObject *current_frame = PyEval_GetFrame(); |
| 3775 | if (current_frame == NULL) |
| 3776 | return PyThreadState_GET()->interp->builtins; |
| 3777 | else |
| 3778 | return current_frame->f_builtins; |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 3779 | } |
| 3780 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3781 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3782 | PyEval_GetLocals(void) |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 3783 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3784 | PyFrameObject *current_frame = PyEval_GetFrame(); |
| 3785 | if (current_frame == NULL) |
| 3786 | return NULL; |
| 3787 | PyFrame_FastToLocals(current_frame); |
| 3788 | return current_frame->f_locals; |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 3789 | } |
| 3790 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3791 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3792 | PyEval_GetGlobals(void) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3793 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3794 | PyFrameObject *current_frame = PyEval_GetFrame(); |
| 3795 | if (current_frame == NULL) |
| 3796 | return NULL; |
| 3797 | else |
| 3798 | return current_frame->f_globals; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3799 | } |
| 3800 | |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 3801 | PyFrameObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3802 | PyEval_GetFrame(void) |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 3803 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3804 | PyThreadState *tstate = PyThreadState_GET(); |
| 3805 | return _PyThreadState_GetFrame(tstate); |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 3806 | } |
| 3807 | |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 3808 | int |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 3809 | PyEval_MergeCompilerFlags(PyCompilerFlags *cf) |
Jeremy Hylton | 061d106 | 2001-03-22 02:32:48 +0000 | [diff] [blame] | 3810 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3811 | PyFrameObject *current_frame = PyEval_GetFrame(); |
| 3812 | int result = cf->cf_flags != 0; |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 3813 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3814 | if (current_frame != NULL) { |
| 3815 | const int codeflags = current_frame->f_code->co_flags; |
| 3816 | const int compilerflags = codeflags & PyCF_MASK; |
| 3817 | if (compilerflags) { |
| 3818 | result = 1; |
| 3819 | cf->cf_flags |= compilerflags; |
| 3820 | } |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 3821 | #if 0 /* future keyword */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3822 | if (codeflags & CO_GENERATOR_ALLOWED) { |
| 3823 | result = 1; |
| 3824 | cf->cf_flags |= CO_GENERATOR_ALLOWED; |
| 3825 | } |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 3826 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3827 | } |
| 3828 | return result; |
Jeremy Hylton | 061d106 | 2001-03-22 02:32:48 +0000 | [diff] [blame] | 3829 | } |
| 3830 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3831 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3832 | /* External interface to call any callable object. |
Antoine Pitrou | 8689a10 | 2010-04-01 16:53:15 +0000 | [diff] [blame] | 3833 | 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] | 3834 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3835 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3836 | PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3837 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3838 | PyObject *result; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3839 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3840 | if (arg == NULL) { |
| 3841 | arg = PyTuple_New(0); |
| 3842 | if (arg == NULL) |
| 3843 | return NULL; |
| 3844 | } |
| 3845 | else if (!PyTuple_Check(arg)) { |
| 3846 | PyErr_SetString(PyExc_TypeError, |
| 3847 | "argument list must be a tuple"); |
| 3848 | return NULL; |
| 3849 | } |
| 3850 | else |
| 3851 | Py_INCREF(arg); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3852 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3853 | if (kw != NULL && !PyDict_Check(kw)) { |
| 3854 | PyErr_SetString(PyExc_TypeError, |
| 3855 | "keyword list must be a dictionary"); |
| 3856 | Py_DECREF(arg); |
| 3857 | return NULL; |
| 3858 | } |
Guido van Rossum | e3e61c1 | 1995-08-04 04:14:47 +0000 | [diff] [blame] | 3859 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3860 | result = PyObject_Call(func, arg, kw); |
| 3861 | Py_DECREF(arg); |
| 3862 | return result; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3863 | } |
| 3864 | |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 3865 | const char * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3866 | PyEval_GetFuncName(PyObject *func) |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 3867 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3868 | if (PyMethod_Check(func)) |
| 3869 | return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func)); |
| 3870 | else if (PyFunction_Check(func)) |
| 3871 | return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name); |
| 3872 | else if (PyCFunction_Check(func)) |
| 3873 | return ((PyCFunctionObject*)func)->m_ml->ml_name; |
| 3874 | else |
| 3875 | return func->ob_type->tp_name; |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 3876 | } |
| 3877 | |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 3878 | const char * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3879 | PyEval_GetFuncDesc(PyObject *func) |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 3880 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3881 | if (PyMethod_Check(func)) |
| 3882 | return "()"; |
| 3883 | else if (PyFunction_Check(func)) |
| 3884 | return "()"; |
| 3885 | else if (PyCFunction_Check(func)) |
| 3886 | return "()"; |
| 3887 | else |
| 3888 | return " object"; |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 3889 | } |
| 3890 | |
Neal Norwitz | addfe0c | 2002-11-10 14:33:26 +0000 | [diff] [blame] | 3891 | static void |
Jeremy Hylton | 192690e | 2002-08-16 18:36:11 +0000 | [diff] [blame] | 3892 | err_args(PyObject *func, int flags, int nargs) |
| 3893 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3894 | if (flags & METH_NOARGS) |
| 3895 | PyErr_Format(PyExc_TypeError, |
| 3896 | "%.200s() takes no arguments (%d given)", |
| 3897 | ((PyCFunctionObject *)func)->m_ml->ml_name, |
| 3898 | nargs); |
| 3899 | else |
| 3900 | PyErr_Format(PyExc_TypeError, |
| 3901 | "%.200s() takes exactly one argument (%d given)", |
| 3902 | ((PyCFunctionObject *)func)->m_ml->ml_name, |
| 3903 | nargs); |
Jeremy Hylton | 192690e | 2002-08-16 18:36:11 +0000 | [diff] [blame] | 3904 | } |
| 3905 | |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 3906 | #define C_TRACE(x, call) \ |
Nicholas Bastin | d858a77 | 2004-06-25 23:31:06 +0000 | [diff] [blame] | 3907 | if (tstate->use_tracing && tstate->c_profilefunc) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3908 | if (call_trace(tstate->c_profilefunc, \ |
| 3909 | tstate->c_profileobj, \ |
| 3910 | tstate->frame, PyTrace_C_CALL, \ |
| 3911 | func)) { \ |
| 3912 | x = NULL; \ |
| 3913 | } \ |
| 3914 | else { \ |
| 3915 | x = call; \ |
| 3916 | if (tstate->c_profilefunc != NULL) { \ |
| 3917 | if (x == NULL) { \ |
| 3918 | call_trace_protected(tstate->c_profilefunc, \ |
| 3919 | tstate->c_profileobj, \ |
| 3920 | tstate->frame, PyTrace_C_EXCEPTION, \ |
| 3921 | func); \ |
| 3922 | /* XXX should pass (type, value, tb) */ \ |
| 3923 | } else { \ |
| 3924 | if (call_trace(tstate->c_profilefunc, \ |
| 3925 | tstate->c_profileobj, \ |
| 3926 | tstate->frame, PyTrace_C_RETURN, \ |
| 3927 | func)) { \ |
| 3928 | Py_DECREF(x); \ |
| 3929 | x = NULL; \ |
| 3930 | } \ |
| 3931 | } \ |
| 3932 | } \ |
| 3933 | } \ |
Nicholas Bastin | d858a77 | 2004-06-25 23:31:06 +0000 | [diff] [blame] | 3934 | } else { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3935 | x = call; \ |
| 3936 | } |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 3937 | |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 3938 | static PyObject * |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 3939 | call_function(PyObject ***pp_stack, int oparg |
| 3940 | #ifdef WITH_TSC |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3941 | , uint64* pintr0, uint64* pintr1 |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 3942 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3943 | ) |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 3944 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3945 | int na = oparg & 0xff; |
| 3946 | int nk = (oparg>>8) & 0xff; |
| 3947 | int n = na + 2 * nk; |
| 3948 | PyObject **pfunc = (*pp_stack) - n - 1; |
| 3949 | PyObject *func = *pfunc; |
| 3950 | PyObject *x, *w; |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 3951 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3952 | /* Always dispatch PyCFunction first, because these are |
| 3953 | presumed to be the most frequent callable object. |
| 3954 | */ |
| 3955 | if (PyCFunction_Check(func) && nk == 0) { |
| 3956 | int flags = PyCFunction_GET_FLAGS(func); |
| 3957 | PyThreadState *tstate = PyThreadState_GET(); |
Raymond Hettinger | a7f56bc | 2004-06-26 04:34:33 +0000 | [diff] [blame] | 3958 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3959 | PCALL(PCALL_CFUNCTION); |
| 3960 | if (flags & (METH_NOARGS | METH_O)) { |
| 3961 | PyCFunction meth = PyCFunction_GET_FUNCTION(func); |
| 3962 | PyObject *self = PyCFunction_GET_SELF(func); |
| 3963 | if (flags & METH_NOARGS && na == 0) { |
| 3964 | C_TRACE(x, (*meth)(self,NULL)); |
| 3965 | } |
| 3966 | else if (flags & METH_O && na == 1) { |
| 3967 | PyObject *arg = EXT_POP(*pp_stack); |
| 3968 | C_TRACE(x, (*meth)(self,arg)); |
| 3969 | Py_DECREF(arg); |
| 3970 | } |
| 3971 | else { |
| 3972 | err_args(func, flags, na); |
| 3973 | x = NULL; |
| 3974 | } |
| 3975 | } |
| 3976 | else { |
| 3977 | PyObject *callargs; |
| 3978 | callargs = load_args(pp_stack, na); |
| 3979 | READ_TIMESTAMP(*pintr0); |
| 3980 | C_TRACE(x, PyCFunction_Call(func,callargs,NULL)); |
| 3981 | READ_TIMESTAMP(*pintr1); |
| 3982 | Py_XDECREF(callargs); |
| 3983 | } |
| 3984 | } else { |
| 3985 | if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) { |
| 3986 | /* optimize access to bound methods */ |
| 3987 | PyObject *self = PyMethod_GET_SELF(func); |
| 3988 | PCALL(PCALL_METHOD); |
| 3989 | PCALL(PCALL_BOUND_METHOD); |
| 3990 | Py_INCREF(self); |
| 3991 | func = PyMethod_GET_FUNCTION(func); |
| 3992 | Py_INCREF(func); |
| 3993 | Py_DECREF(*pfunc); |
| 3994 | *pfunc = self; |
| 3995 | na++; |
| 3996 | n++; |
| 3997 | } else |
| 3998 | Py_INCREF(func); |
| 3999 | READ_TIMESTAMP(*pintr0); |
| 4000 | if (PyFunction_Check(func)) |
| 4001 | x = fast_function(func, pp_stack, n, na, nk); |
| 4002 | else |
| 4003 | x = do_call(func, pp_stack, na, nk); |
| 4004 | READ_TIMESTAMP(*pintr1); |
| 4005 | Py_DECREF(func); |
| 4006 | } |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 4007 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4008 | /* Clear the stack of the function object. Also removes |
| 4009 | the arguments in case they weren't consumed already |
| 4010 | (fast_function() and err_args() leave them on the stack). |
| 4011 | */ |
| 4012 | while ((*pp_stack) > pfunc) { |
| 4013 | w = EXT_POP(*pp_stack); |
| 4014 | Py_DECREF(w); |
| 4015 | PCALL(PCALL_POP); |
| 4016 | } |
| 4017 | return x; |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 4018 | } |
| 4019 | |
Jeremy Hylton | 192690e | 2002-08-16 18:36:11 +0000 | [diff] [blame] | 4020 | /* The fast_function() function optimize calls for which no argument |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4021 | tuple is necessary; the objects are passed directly from the stack. |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4022 | For the simplest case -- a function that takes only positional |
| 4023 | arguments and is called with only positional arguments -- it |
| 4024 | inlines the most primitive frame setup code from |
| 4025 | PyEval_EvalCodeEx(), which vastly reduces the checks that must be |
| 4026 | done before evaluating the frame. |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4027 | */ |
| 4028 | |
| 4029 | static PyObject * |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4030 | 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] | 4031 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4032 | PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func); |
| 4033 | PyObject *globals = PyFunction_GET_GLOBALS(func); |
| 4034 | PyObject *argdefs = PyFunction_GET_DEFAULTS(func); |
| 4035 | PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func); |
| 4036 | PyObject **d = NULL; |
| 4037 | int nd = 0; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4038 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4039 | PCALL(PCALL_FUNCTION); |
| 4040 | PCALL(PCALL_FAST_FUNCTION); |
| 4041 | if (argdefs == NULL && co->co_argcount == n && |
| 4042 | co->co_kwonlyargcount == 0 && nk==0 && |
| 4043 | co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) { |
| 4044 | PyFrameObject *f; |
| 4045 | PyObject *retval = NULL; |
| 4046 | PyThreadState *tstate = PyThreadState_GET(); |
| 4047 | PyObject **fastlocals, **stack; |
| 4048 | int i; |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4049 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4050 | PCALL(PCALL_FASTER_FUNCTION); |
| 4051 | assert(globals != NULL); |
| 4052 | /* XXX Perhaps we should create a specialized |
| 4053 | PyFrame_New() that doesn't take locals, but does |
| 4054 | take builtins without sanity checking them. |
| 4055 | */ |
| 4056 | assert(tstate != NULL); |
| 4057 | f = PyFrame_New(tstate, co, globals, NULL); |
| 4058 | if (f == NULL) |
| 4059 | return NULL; |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4060 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4061 | fastlocals = f->f_localsplus; |
| 4062 | stack = (*pp_stack) - n; |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4063 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4064 | for (i = 0; i < n; i++) { |
| 4065 | Py_INCREF(*stack); |
| 4066 | fastlocals[i] = *stack++; |
| 4067 | } |
| 4068 | retval = PyEval_EvalFrameEx(f,0); |
| 4069 | ++tstate->recursion_depth; |
| 4070 | Py_DECREF(f); |
| 4071 | --tstate->recursion_depth; |
| 4072 | return retval; |
| 4073 | } |
| 4074 | if (argdefs != NULL) { |
| 4075 | d = &PyTuple_GET_ITEM(argdefs, 0); |
| 4076 | nd = Py_SIZE(argdefs); |
| 4077 | } |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 4078 | return PyEval_EvalCodeEx((PyObject*)co, globals, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4079 | (PyObject *)NULL, (*pp_stack)-n, na, |
| 4080 | (*pp_stack)-2*nk, nk, d, nd, kwdefs, |
| 4081 | PyFunction_GET_CLOSURE(func)); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4082 | } |
| 4083 | |
| 4084 | static PyObject * |
Ka-Ping Yee | 2057970 | 2001-01-15 22:14:16 +0000 | [diff] [blame] | 4085 | update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack, |
| 4086 | PyObject *func) |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4087 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4088 | PyObject *kwdict = NULL; |
| 4089 | if (orig_kwdict == NULL) |
| 4090 | kwdict = PyDict_New(); |
| 4091 | else { |
| 4092 | kwdict = PyDict_Copy(orig_kwdict); |
| 4093 | Py_DECREF(orig_kwdict); |
| 4094 | } |
| 4095 | if (kwdict == NULL) |
| 4096 | return NULL; |
| 4097 | while (--nk >= 0) { |
| 4098 | int err; |
| 4099 | PyObject *value = EXT_POP(*pp_stack); |
| 4100 | PyObject *key = EXT_POP(*pp_stack); |
| 4101 | if (PyDict_GetItem(kwdict, key) != NULL) { |
| 4102 | PyErr_Format(PyExc_TypeError, |
| 4103 | "%.200s%s got multiple values " |
| 4104 | "for keyword argument '%U'", |
| 4105 | PyEval_GetFuncName(func), |
| 4106 | PyEval_GetFuncDesc(func), |
| 4107 | key); |
| 4108 | Py_DECREF(key); |
| 4109 | Py_DECREF(value); |
| 4110 | Py_DECREF(kwdict); |
| 4111 | return NULL; |
| 4112 | } |
| 4113 | err = PyDict_SetItem(kwdict, key, value); |
| 4114 | Py_DECREF(key); |
| 4115 | Py_DECREF(value); |
| 4116 | if (err) { |
| 4117 | Py_DECREF(kwdict); |
| 4118 | return NULL; |
| 4119 | } |
| 4120 | } |
| 4121 | return kwdict; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4122 | } |
| 4123 | |
| 4124 | static PyObject * |
| 4125 | update_star_args(int nstack, int nstar, PyObject *stararg, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4126 | PyObject ***pp_stack) |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4127 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4128 | PyObject *callargs, *w; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4129 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4130 | callargs = PyTuple_New(nstack + nstar); |
| 4131 | if (callargs == NULL) { |
| 4132 | return NULL; |
| 4133 | } |
| 4134 | if (nstar) { |
| 4135 | int i; |
| 4136 | for (i = 0; i < nstar; i++) { |
| 4137 | PyObject *a = PyTuple_GET_ITEM(stararg, i); |
| 4138 | Py_INCREF(a); |
| 4139 | PyTuple_SET_ITEM(callargs, nstack + i, a); |
| 4140 | } |
| 4141 | } |
| 4142 | while (--nstack >= 0) { |
| 4143 | w = EXT_POP(*pp_stack); |
| 4144 | PyTuple_SET_ITEM(callargs, nstack, w); |
| 4145 | } |
| 4146 | return callargs; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4147 | } |
| 4148 | |
| 4149 | static PyObject * |
| 4150 | load_args(PyObject ***pp_stack, int na) |
| 4151 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4152 | PyObject *args = PyTuple_New(na); |
| 4153 | PyObject *w; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4154 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4155 | if (args == NULL) |
| 4156 | return NULL; |
| 4157 | while (--na >= 0) { |
| 4158 | w = EXT_POP(*pp_stack); |
| 4159 | PyTuple_SET_ITEM(args, na, w); |
| 4160 | } |
| 4161 | return args; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4162 | } |
| 4163 | |
| 4164 | static PyObject * |
| 4165 | do_call(PyObject *func, PyObject ***pp_stack, int na, int nk) |
| 4166 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4167 | PyObject *callargs = NULL; |
| 4168 | PyObject *kwdict = NULL; |
| 4169 | PyObject *result = NULL; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4170 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4171 | if (nk > 0) { |
| 4172 | kwdict = update_keyword_args(NULL, nk, pp_stack, func); |
| 4173 | if (kwdict == NULL) |
| 4174 | goto call_fail; |
| 4175 | } |
| 4176 | callargs = load_args(pp_stack, na); |
| 4177 | if (callargs == NULL) |
| 4178 | goto call_fail; |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4179 | #ifdef CALL_PROFILE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4180 | /* At this point, we have to look at the type of func to |
| 4181 | update the call stats properly. Do it here so as to avoid |
| 4182 | exposing the call stats machinery outside ceval.c |
| 4183 | */ |
| 4184 | if (PyFunction_Check(func)) |
| 4185 | PCALL(PCALL_FUNCTION); |
| 4186 | else if (PyMethod_Check(func)) |
| 4187 | PCALL(PCALL_METHOD); |
| 4188 | else if (PyType_Check(func)) |
| 4189 | PCALL(PCALL_TYPE); |
| 4190 | else if (PyCFunction_Check(func)) |
| 4191 | PCALL(PCALL_CFUNCTION); |
| 4192 | else |
| 4193 | PCALL(PCALL_OTHER); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4194 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4195 | if (PyCFunction_Check(func)) { |
| 4196 | PyThreadState *tstate = PyThreadState_GET(); |
| 4197 | C_TRACE(result, PyCFunction_Call(func, callargs, kwdict)); |
| 4198 | } |
| 4199 | else |
| 4200 | result = PyObject_Call(func, callargs, kwdict); |
Thomas Wouters | 7ce29ca | 2007-09-19 21:56:32 +0000 | [diff] [blame] | 4201 | call_fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4202 | Py_XDECREF(callargs); |
| 4203 | Py_XDECREF(kwdict); |
| 4204 | return result; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4205 | } |
| 4206 | |
| 4207 | static PyObject * |
| 4208 | ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk) |
| 4209 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4210 | int nstar = 0; |
| 4211 | PyObject *callargs = NULL; |
| 4212 | PyObject *stararg = NULL; |
| 4213 | PyObject *kwdict = NULL; |
| 4214 | PyObject *result = NULL; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4215 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4216 | if (flags & CALL_FLAG_KW) { |
| 4217 | kwdict = EXT_POP(*pp_stack); |
| 4218 | if (!PyDict_Check(kwdict)) { |
| 4219 | PyObject *d; |
| 4220 | d = PyDict_New(); |
| 4221 | if (d == NULL) |
| 4222 | goto ext_call_fail; |
| 4223 | if (PyDict_Update(d, kwdict) != 0) { |
| 4224 | Py_DECREF(d); |
| 4225 | /* PyDict_Update raises attribute |
| 4226 | * error (percolated from an attempt |
| 4227 | * to get 'keys' attribute) instead of |
| 4228 | * a type error if its second argument |
| 4229 | * is not a mapping. |
| 4230 | */ |
| 4231 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 4232 | PyErr_Format(PyExc_TypeError, |
| 4233 | "%.200s%.200s argument after ** " |
| 4234 | "must be a mapping, not %.200s", |
| 4235 | PyEval_GetFuncName(func), |
| 4236 | PyEval_GetFuncDesc(func), |
| 4237 | kwdict->ob_type->tp_name); |
| 4238 | } |
| 4239 | goto ext_call_fail; |
| 4240 | } |
| 4241 | Py_DECREF(kwdict); |
| 4242 | kwdict = d; |
| 4243 | } |
| 4244 | } |
| 4245 | if (flags & CALL_FLAG_VAR) { |
| 4246 | stararg = EXT_POP(*pp_stack); |
| 4247 | if (!PyTuple_Check(stararg)) { |
| 4248 | PyObject *t = NULL; |
| 4249 | t = PySequence_Tuple(stararg); |
| 4250 | if (t == NULL) { |
| 4251 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 4252 | PyErr_Format(PyExc_TypeError, |
| 4253 | "%.200s%.200s argument after * " |
Victor Stinner | 0a5f65a | 2011-03-22 01:09:21 +0100 | [diff] [blame] | 4254 | "must be a sequence, not %.200s", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4255 | PyEval_GetFuncName(func), |
| 4256 | PyEval_GetFuncDesc(func), |
| 4257 | stararg->ob_type->tp_name); |
| 4258 | } |
| 4259 | goto ext_call_fail; |
| 4260 | } |
| 4261 | Py_DECREF(stararg); |
| 4262 | stararg = t; |
| 4263 | } |
| 4264 | nstar = PyTuple_GET_SIZE(stararg); |
| 4265 | } |
| 4266 | if (nk > 0) { |
| 4267 | kwdict = update_keyword_args(kwdict, nk, pp_stack, func); |
| 4268 | if (kwdict == NULL) |
| 4269 | goto ext_call_fail; |
| 4270 | } |
| 4271 | callargs = update_star_args(na, nstar, stararg, pp_stack); |
| 4272 | if (callargs == NULL) |
| 4273 | goto ext_call_fail; |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4274 | #ifdef CALL_PROFILE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4275 | /* At this point, we have to look at the type of func to |
| 4276 | update the call stats properly. Do it here so as to avoid |
| 4277 | exposing the call stats machinery outside ceval.c |
| 4278 | */ |
| 4279 | if (PyFunction_Check(func)) |
| 4280 | PCALL(PCALL_FUNCTION); |
| 4281 | else if (PyMethod_Check(func)) |
| 4282 | PCALL(PCALL_METHOD); |
| 4283 | else if (PyType_Check(func)) |
| 4284 | PCALL(PCALL_TYPE); |
| 4285 | else if (PyCFunction_Check(func)) |
| 4286 | PCALL(PCALL_CFUNCTION); |
| 4287 | else |
| 4288 | PCALL(PCALL_OTHER); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4289 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4290 | if (PyCFunction_Check(func)) { |
| 4291 | PyThreadState *tstate = PyThreadState_GET(); |
| 4292 | C_TRACE(result, PyCFunction_Call(func, callargs, kwdict)); |
| 4293 | } |
| 4294 | else |
| 4295 | result = PyObject_Call(func, callargs, kwdict); |
Thomas Wouters | ce272b6 | 2007-09-19 21:19:28 +0000 | [diff] [blame] | 4296 | ext_call_fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4297 | Py_XDECREF(callargs); |
| 4298 | Py_XDECREF(kwdict); |
| 4299 | Py_XDECREF(stararg); |
| 4300 | return result; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4301 | } |
| 4302 | |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 4303 | /* Extract a slice index from a PyInt or PyLong or an object with the |
| 4304 | nb_index slot defined, and store in *pi. |
| 4305 | Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX, |
| 4306 | 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] | 4307 | Return 0 on error, 1 on success. |
Tim Peters | cb479e7 | 2001-12-16 19:11:44 +0000 | [diff] [blame] | 4308 | */ |
Tim Peters | b519638 | 2001-12-16 19:44:20 +0000 | [diff] [blame] | 4309 | /* Note: If v is NULL, return success without storing into *pi. This |
| 4310 | is because_PyEval_SliceIndex() is called by apply_slice(), which can be |
| 4311 | 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] | 4312 | */ |
Guido van Rossum | 20c6add | 2000-05-08 14:06:50 +0000 | [diff] [blame] | 4313 | int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4314 | _PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4315 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4316 | if (v != NULL) { |
| 4317 | Py_ssize_t x; |
| 4318 | if (PyIndex_Check(v)) { |
| 4319 | x = PyNumber_AsSsize_t(v, NULL); |
| 4320 | if (x == -1 && PyErr_Occurred()) |
| 4321 | return 0; |
| 4322 | } |
| 4323 | else { |
| 4324 | PyErr_SetString(PyExc_TypeError, |
| 4325 | "slice indices must be integers or " |
| 4326 | "None or have an __index__ method"); |
| 4327 | return 0; |
| 4328 | } |
| 4329 | *pi = x; |
| 4330 | } |
| 4331 | return 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4332 | } |
| 4333 | |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 4334 | #define CANNOT_CATCH_MSG "catching classes that do not inherit from "\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4335 | "BaseException is not allowed" |
Brett Cannon | f74225d | 2007-02-26 21:10:16 +0000 | [diff] [blame] | 4336 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4337 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4338 | cmp_outcome(int op, register PyObject *v, register PyObject *w) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4339 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4340 | int res = 0; |
| 4341 | switch (op) { |
| 4342 | case PyCmp_IS: |
| 4343 | res = (v == w); |
| 4344 | break; |
| 4345 | case PyCmp_IS_NOT: |
| 4346 | res = (v != w); |
| 4347 | break; |
| 4348 | case PyCmp_IN: |
| 4349 | res = PySequence_Contains(w, v); |
| 4350 | if (res < 0) |
| 4351 | return NULL; |
| 4352 | break; |
| 4353 | case PyCmp_NOT_IN: |
| 4354 | res = PySequence_Contains(w, v); |
| 4355 | if (res < 0) |
| 4356 | return NULL; |
| 4357 | res = !res; |
| 4358 | break; |
| 4359 | case PyCmp_EXC_MATCH: |
| 4360 | if (PyTuple_Check(w)) { |
| 4361 | Py_ssize_t i, length; |
| 4362 | length = PyTuple_Size(w); |
| 4363 | for (i = 0; i < length; i += 1) { |
| 4364 | PyObject *exc = PyTuple_GET_ITEM(w, i); |
| 4365 | if (!PyExceptionClass_Check(exc)) { |
| 4366 | PyErr_SetString(PyExc_TypeError, |
| 4367 | CANNOT_CATCH_MSG); |
| 4368 | return NULL; |
| 4369 | } |
| 4370 | } |
| 4371 | } |
| 4372 | else { |
| 4373 | if (!PyExceptionClass_Check(w)) { |
| 4374 | PyErr_SetString(PyExc_TypeError, |
| 4375 | CANNOT_CATCH_MSG); |
| 4376 | return NULL; |
| 4377 | } |
| 4378 | } |
| 4379 | res = PyErr_GivenExceptionMatches(v, w); |
| 4380 | break; |
| 4381 | default: |
| 4382 | return PyObject_RichCompare(v, w, op); |
| 4383 | } |
| 4384 | v = res ? Py_True : Py_False; |
| 4385 | Py_INCREF(v); |
| 4386 | return v; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4387 | } |
| 4388 | |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 4389 | static PyObject * |
| 4390 | import_from(PyObject *v, PyObject *name) |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 4391 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4392 | PyObject *x; |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 4393 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4394 | x = PyObject_GetAttr(v, name); |
| 4395 | if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 4396 | PyErr_Format(PyExc_ImportError, "cannot import name %S", name); |
| 4397 | } |
| 4398 | return x; |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 4399 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4400 | |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 4401 | static int |
| 4402 | import_all_from(PyObject *locals, PyObject *v) |
| 4403 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4404 | PyObject *all = PyObject_GetAttrString(v, "__all__"); |
| 4405 | PyObject *dict, *name, *value; |
| 4406 | int skip_leading_underscores = 0; |
| 4407 | int pos, err; |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 4408 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4409 | if (all == NULL) { |
| 4410 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 4411 | return -1; /* Unexpected error */ |
| 4412 | PyErr_Clear(); |
| 4413 | dict = PyObject_GetAttrString(v, "__dict__"); |
| 4414 | if (dict == NULL) { |
| 4415 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 4416 | return -1; |
| 4417 | PyErr_SetString(PyExc_ImportError, |
| 4418 | "from-import-* object has no __dict__ and no __all__"); |
| 4419 | return -1; |
| 4420 | } |
| 4421 | all = PyMapping_Keys(dict); |
| 4422 | Py_DECREF(dict); |
| 4423 | if (all == NULL) |
| 4424 | return -1; |
| 4425 | skip_leading_underscores = 1; |
| 4426 | } |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 4427 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4428 | for (pos = 0, err = 0; ; pos++) { |
| 4429 | name = PySequence_GetItem(all, pos); |
| 4430 | if (name == NULL) { |
| 4431 | if (!PyErr_ExceptionMatches(PyExc_IndexError)) |
| 4432 | err = -1; |
| 4433 | else |
| 4434 | PyErr_Clear(); |
| 4435 | break; |
| 4436 | } |
| 4437 | if (skip_leading_underscores && |
| 4438 | PyUnicode_Check(name) && |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4439 | PyUnicode_READY(name) != -1 && |
| 4440 | PyUnicode_READ_CHAR(name, 0) == '_') |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4441 | { |
| 4442 | Py_DECREF(name); |
| 4443 | continue; |
| 4444 | } |
| 4445 | value = PyObject_GetAttr(v, name); |
| 4446 | if (value == NULL) |
| 4447 | err = -1; |
| 4448 | else if (PyDict_CheckExact(locals)) |
| 4449 | err = PyDict_SetItem(locals, name, value); |
| 4450 | else |
| 4451 | err = PyObject_SetItem(locals, name, value); |
| 4452 | Py_DECREF(name); |
| 4453 | Py_XDECREF(value); |
| 4454 | if (err != 0) |
| 4455 | break; |
| 4456 | } |
| 4457 | Py_DECREF(all); |
| 4458 | return err; |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 4459 | } |
| 4460 | |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4461 | static void |
Neal Norwitz | da059e3 | 2007-08-26 05:33:45 +0000 | [diff] [blame] | 4462 | format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj) |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 4463 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4464 | const char *obj_str; |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 4465 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4466 | if (!obj) |
| 4467 | return; |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 4468 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4469 | obj_str = _PyUnicode_AsString(obj); |
| 4470 | if (!obj_str) |
| 4471 | return; |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 4472 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4473 | PyErr_Format(exc, format_str, obj_str); |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 4474 | } |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 4475 | |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 4476 | static void |
| 4477 | format_exc_unbound(PyCodeObject *co, int oparg) |
| 4478 | { |
| 4479 | PyObject *name; |
| 4480 | /* Don't stomp existing exception */ |
| 4481 | if (PyErr_Occurred()) |
| 4482 | return; |
| 4483 | if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) { |
| 4484 | name = PyTuple_GET_ITEM(co->co_cellvars, |
| 4485 | oparg); |
| 4486 | format_exc_check_arg( |
| 4487 | PyExc_UnboundLocalError, |
| 4488 | UNBOUNDLOCAL_ERROR_MSG, |
| 4489 | name); |
| 4490 | } else { |
| 4491 | name = PyTuple_GET_ITEM(co->co_freevars, oparg - |
| 4492 | PyTuple_GET_SIZE(co->co_cellvars)); |
| 4493 | format_exc_check_arg(PyExc_NameError, |
| 4494 | UNBOUNDFREE_ERROR_MSG, name); |
| 4495 | } |
| 4496 | } |
| 4497 | |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 4498 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 4499 | |
Skip Montanaro | f118cb1 | 2001-10-15 20:51:38 +0000 | [diff] [blame] | 4500 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4501 | getarray(long a[256]) |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 4502 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4503 | int i; |
| 4504 | PyObject *l = PyList_New(256); |
| 4505 | if (l == NULL) return NULL; |
| 4506 | for (i = 0; i < 256; i++) { |
| 4507 | PyObject *x = PyLong_FromLong(a[i]); |
| 4508 | if (x == NULL) { |
| 4509 | Py_DECREF(l); |
| 4510 | return NULL; |
| 4511 | } |
| 4512 | PyList_SetItem(l, i, x); |
| 4513 | } |
| 4514 | for (i = 0; i < 256; i++) |
| 4515 | a[i] = 0; |
| 4516 | return l; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 4517 | } |
| 4518 | |
| 4519 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4520 | _Py_GetDXProfile(PyObject *self, PyObject *args) |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 4521 | { |
| 4522 | #ifndef DXPAIRS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4523 | return getarray(dxp); |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 4524 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4525 | int i; |
| 4526 | PyObject *l = PyList_New(257); |
| 4527 | if (l == NULL) return NULL; |
| 4528 | for (i = 0; i < 257; i++) { |
| 4529 | PyObject *x = getarray(dxpairs[i]); |
| 4530 | if (x == NULL) { |
| 4531 | Py_DECREF(l); |
| 4532 | return NULL; |
| 4533 | } |
| 4534 | PyList_SetItem(l, i, x); |
| 4535 | } |
| 4536 | return l; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 4537 | #endif |
| 4538 | } |
| 4539 | |
| 4540 | #endif |