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