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