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