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