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