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