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