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