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