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