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