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