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(); |
Xiang Zhang | b4f0e98 | 2017-03-01 14:28:14 +0800 | [diff] [blame] | 1449 | if (PyString_CheckExact(v) |
| 1450 | && (!PyString_Check(w) || PyString_CheckExact(w))) { |
| 1451 | /* fast path; string formatting, but not if the RHS is a str subclass |
| 1452 | (see issue28598) */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1453 | x = PyString_Format(v, w); |
Xiang Zhang | b4f0e98 | 2017-03-01 14:28:14 +0800 | [diff] [blame] | 1454 | } else { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1455 | x = PyNumber_Remainder(v, w); |
Xiang Zhang | b4f0e98 | 2017-03-01 14:28:14 +0800 | [diff] [blame] | 1456 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1457 | Py_DECREF(v); |
| 1458 | Py_DECREF(w); |
| 1459 | SET_TOP(x); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 1460 | if (x != NULL) DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1461 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1462 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1463 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1464 | TARGET_NOARG(BINARY_ADD) |
| 1465 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1466 | w = POP(); |
| 1467 | v = TOP(); |
| 1468 | if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) { |
| 1469 | /* INLINE: int + int */ |
| 1470 | register long a, b, i; |
| 1471 | a = PyInt_AS_LONG(v); |
| 1472 | b = PyInt_AS_LONG(w); |
| 1473 | /* cast to avoid undefined behaviour |
| 1474 | on overflow */ |
| 1475 | i = (long)((unsigned long)a + b); |
| 1476 | if ((i^a) < 0 && (i^b) < 0) |
| 1477 | goto slow_add; |
| 1478 | x = PyInt_FromLong(i); |
| 1479 | } |
| 1480 | else if (PyString_CheckExact(v) && |
| 1481 | PyString_CheckExact(w)) { |
| 1482 | x = string_concatenate(v, w, f, next_instr); |
| 1483 | /* string_concatenate consumed the ref to v */ |
| 1484 | goto skip_decref_vx; |
| 1485 | } |
| 1486 | else { |
| 1487 | slow_add: |
| 1488 | x = PyNumber_Add(v, w); |
| 1489 | } |
| 1490 | Py_DECREF(v); |
| 1491 | skip_decref_vx: |
| 1492 | Py_DECREF(w); |
| 1493 | SET_TOP(x); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 1494 | if (x != NULL) DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1495 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1496 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1497 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1498 | TARGET_NOARG(BINARY_SUBTRACT) |
| 1499 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1500 | w = POP(); |
| 1501 | v = TOP(); |
| 1502 | if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) { |
| 1503 | /* INLINE: int - int */ |
| 1504 | register long a, b, i; |
| 1505 | a = PyInt_AS_LONG(v); |
| 1506 | b = PyInt_AS_LONG(w); |
| 1507 | /* cast to avoid undefined behaviour |
| 1508 | on overflow */ |
| 1509 | i = (long)((unsigned long)a - b); |
| 1510 | if ((i^a) < 0 && (i^~b) < 0) |
| 1511 | goto slow_sub; |
| 1512 | x = PyInt_FromLong(i); |
| 1513 | } |
| 1514 | else { |
| 1515 | slow_sub: |
| 1516 | x = PyNumber_Subtract(v, w); |
| 1517 | } |
| 1518 | Py_DECREF(v); |
| 1519 | Py_DECREF(w); |
| 1520 | SET_TOP(x); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 1521 | if (x != NULL) DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1522 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1523 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1524 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1525 | TARGET_NOARG(BINARY_SUBSCR) |
| 1526 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1527 | w = POP(); |
| 1528 | v = TOP(); |
| 1529 | if (PyList_CheckExact(v) && PyInt_CheckExact(w)) { |
| 1530 | /* INLINE: list[int] */ |
| 1531 | Py_ssize_t i = PyInt_AsSsize_t(w); |
| 1532 | if (i < 0) |
| 1533 | i += PyList_GET_SIZE(v); |
| 1534 | if (i >= 0 && i < PyList_GET_SIZE(v)) { |
| 1535 | x = PyList_GET_ITEM(v, i); |
| 1536 | Py_INCREF(x); |
| 1537 | } |
| 1538 | else |
| 1539 | goto slow_get; |
| 1540 | } |
| 1541 | else |
| 1542 | slow_get: |
| 1543 | x = PyObject_GetItem(v, w); |
| 1544 | Py_DECREF(v); |
| 1545 | Py_DECREF(w); |
| 1546 | SET_TOP(x); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 1547 | if (x != NULL) DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1548 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1549 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1550 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1551 | TARGET_NOARG(BINARY_LSHIFT) |
| 1552 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1553 | w = POP(); |
| 1554 | v = TOP(); |
| 1555 | x = PyNumber_Lshift(v, w); |
| 1556 | Py_DECREF(v); |
| 1557 | Py_DECREF(w); |
| 1558 | SET_TOP(x); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 1559 | if (x != NULL) DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1560 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1561 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1562 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1563 | TARGET_NOARG(BINARY_RSHIFT) |
| 1564 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1565 | w = POP(); |
| 1566 | v = TOP(); |
| 1567 | x = PyNumber_Rshift(v, w); |
| 1568 | Py_DECREF(v); |
| 1569 | Py_DECREF(w); |
| 1570 | SET_TOP(x); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 1571 | if (x != NULL) DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1572 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1573 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1574 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1575 | TARGET_NOARG(BINARY_AND) |
| 1576 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1577 | w = POP(); |
| 1578 | v = TOP(); |
| 1579 | x = PyNumber_And(v, w); |
| 1580 | Py_DECREF(v); |
| 1581 | Py_DECREF(w); |
| 1582 | SET_TOP(x); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 1583 | if (x != NULL) DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1584 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1585 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1586 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1587 | TARGET_NOARG(BINARY_XOR) |
| 1588 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1589 | w = POP(); |
| 1590 | v = TOP(); |
| 1591 | x = PyNumber_Xor(v, w); |
| 1592 | Py_DECREF(v); |
| 1593 | Py_DECREF(w); |
| 1594 | SET_TOP(x); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 1595 | if (x != NULL) DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1596 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1597 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1598 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1599 | TARGET_NOARG(BINARY_OR) |
| 1600 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1601 | w = POP(); |
| 1602 | v = TOP(); |
| 1603 | x = PyNumber_Or(v, w); |
| 1604 | Py_DECREF(v); |
| 1605 | Py_DECREF(w); |
| 1606 | SET_TOP(x); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 1607 | if (x != NULL) DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1608 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1609 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1610 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1611 | TARGET(LIST_APPEND) |
| 1612 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1613 | w = POP(); |
| 1614 | v = PEEK(oparg); |
| 1615 | err = PyList_Append(v, w); |
| 1616 | Py_DECREF(w); |
| 1617 | if (err == 0) { |
| 1618 | PREDICT(JUMP_ABSOLUTE); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 1619 | DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1620 | } |
| 1621 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1622 | } |
Raymond Hettinger | dd80f76 | 2004-03-07 07:31:06 +0000 | [diff] [blame] | 1623 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1624 | TARGET(SET_ADD) |
| 1625 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1626 | w = POP(); |
| 1627 | v = stack_pointer[-oparg]; |
| 1628 | err = PySet_Add(v, w); |
| 1629 | Py_DECREF(w); |
| 1630 | if (err == 0) { |
| 1631 | PREDICT(JUMP_ABSOLUTE); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 1632 | DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1633 | } |
| 1634 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1635 | } |
Alexandre Vassalotti | b646547 | 2010-01-11 22:36:12 +0000 | [diff] [blame] | 1636 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1637 | TARGET_NOARG(INPLACE_POWER) |
| 1638 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1639 | w = POP(); |
| 1640 | v = TOP(); |
| 1641 | x = PyNumber_InPlacePower(v, w, Py_None); |
| 1642 | Py_DECREF(v); |
| 1643 | Py_DECREF(w); |
| 1644 | SET_TOP(x); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 1645 | if (x != NULL) DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1646 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1647 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1648 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1649 | TARGET_NOARG(INPLACE_MULTIPLY) |
| 1650 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1651 | w = POP(); |
| 1652 | v = TOP(); |
| 1653 | x = PyNumber_InPlaceMultiply(v, w); |
| 1654 | Py_DECREF(v); |
| 1655 | Py_DECREF(w); |
| 1656 | SET_TOP(x); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 1657 | if (x != NULL) DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1658 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1659 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1660 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1661 | TARGET_NOARG(INPLACE_DIVIDE) |
| 1662 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1663 | if (!_Py_QnewFlag) { |
| 1664 | w = POP(); |
| 1665 | v = TOP(); |
| 1666 | x = PyNumber_InPlaceDivide(v, w); |
| 1667 | Py_DECREF(v); |
| 1668 | Py_DECREF(w); |
| 1669 | SET_TOP(x); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 1670 | if (x != NULL) DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1671 | break; |
| 1672 | } |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1673 | } |
Stefan Krah | 7ff7825 | 2010-06-23 18:12:09 +0000 | [diff] [blame] | 1674 | /* -Qnew is in effect: fall through to |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1675 | INPLACE_TRUE_DIVIDE */ |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1676 | TARGET_NOARG(INPLACE_TRUE_DIVIDE) |
| 1677 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1678 | w = POP(); |
| 1679 | v = TOP(); |
| 1680 | x = PyNumber_InPlaceTrueDivide(v, w); |
| 1681 | Py_DECREF(v); |
| 1682 | Py_DECREF(w); |
| 1683 | SET_TOP(x); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 1684 | if (x != NULL) DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1685 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1686 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1687 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1688 | TARGET_NOARG(INPLACE_FLOOR_DIVIDE) |
| 1689 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1690 | w = POP(); |
| 1691 | v = TOP(); |
| 1692 | x = PyNumber_InPlaceFloorDivide(v, w); |
| 1693 | Py_DECREF(v); |
| 1694 | Py_DECREF(w); |
| 1695 | SET_TOP(x); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 1696 | if (x != NULL) DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1697 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1698 | } |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1699 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1700 | TARGET_NOARG(INPLACE_MODULO) |
| 1701 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1702 | w = POP(); |
| 1703 | v = TOP(); |
| 1704 | x = PyNumber_InPlaceRemainder(v, w); |
| 1705 | Py_DECREF(v); |
| 1706 | Py_DECREF(w); |
| 1707 | SET_TOP(x); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 1708 | if (x != NULL) DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1709 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1710 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1711 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1712 | TARGET_NOARG(INPLACE_ADD) |
| 1713 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1714 | w = POP(); |
| 1715 | v = TOP(); |
| 1716 | if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) { |
| 1717 | /* INLINE: int + int */ |
| 1718 | register long a, b, i; |
| 1719 | a = PyInt_AS_LONG(v); |
| 1720 | b = PyInt_AS_LONG(w); |
| 1721 | i = a + b; |
| 1722 | if ((i^a) < 0 && (i^b) < 0) |
| 1723 | goto slow_iadd; |
| 1724 | x = PyInt_FromLong(i); |
| 1725 | } |
| 1726 | else if (PyString_CheckExact(v) && |
| 1727 | PyString_CheckExact(w)) { |
| 1728 | x = string_concatenate(v, w, f, next_instr); |
| 1729 | /* string_concatenate consumed the ref to v */ |
| 1730 | goto skip_decref_v; |
| 1731 | } |
| 1732 | else { |
| 1733 | slow_iadd: |
| 1734 | x = PyNumber_InPlaceAdd(v, w); |
| 1735 | } |
| 1736 | Py_DECREF(v); |
| 1737 | skip_decref_v: |
| 1738 | Py_DECREF(w); |
| 1739 | SET_TOP(x); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 1740 | if (x != NULL) DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1741 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1742 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1743 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1744 | TARGET_NOARG(INPLACE_SUBTRACT) |
| 1745 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1746 | w = POP(); |
| 1747 | v = TOP(); |
| 1748 | if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) { |
| 1749 | /* INLINE: int - int */ |
| 1750 | register long a, b, i; |
| 1751 | a = PyInt_AS_LONG(v); |
| 1752 | b = PyInt_AS_LONG(w); |
| 1753 | i = a - b; |
| 1754 | if ((i^a) < 0 && (i^~b) < 0) |
| 1755 | goto slow_isub; |
| 1756 | x = PyInt_FromLong(i); |
| 1757 | } |
| 1758 | else { |
| 1759 | slow_isub: |
| 1760 | x = PyNumber_InPlaceSubtract(v, w); |
| 1761 | } |
| 1762 | Py_DECREF(v); |
| 1763 | Py_DECREF(w); |
| 1764 | SET_TOP(x); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 1765 | if (x != NULL) DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1766 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1767 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1768 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1769 | TARGET_NOARG(INPLACE_LSHIFT) |
| 1770 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1771 | w = POP(); |
| 1772 | v = TOP(); |
| 1773 | x = PyNumber_InPlaceLshift(v, w); |
| 1774 | Py_DECREF(v); |
| 1775 | Py_DECREF(w); |
| 1776 | SET_TOP(x); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 1777 | if (x != NULL) DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1778 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1779 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1780 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1781 | TARGET_NOARG(INPLACE_RSHIFT) |
| 1782 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1783 | w = POP(); |
| 1784 | v = TOP(); |
| 1785 | x = PyNumber_InPlaceRshift(v, w); |
| 1786 | Py_DECREF(v); |
| 1787 | Py_DECREF(w); |
| 1788 | SET_TOP(x); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 1789 | if (x != NULL) DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1790 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1791 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1792 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1793 | TARGET_NOARG(INPLACE_AND) |
| 1794 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1795 | w = POP(); |
| 1796 | v = TOP(); |
| 1797 | x = PyNumber_InPlaceAnd(v, w); |
| 1798 | Py_DECREF(v); |
| 1799 | Py_DECREF(w); |
| 1800 | SET_TOP(x); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 1801 | if (x != NULL) DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1802 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1803 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1804 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1805 | TARGET_NOARG(INPLACE_XOR) |
| 1806 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1807 | w = POP(); |
| 1808 | v = TOP(); |
| 1809 | x = PyNumber_InPlaceXor(v, w); |
| 1810 | Py_DECREF(v); |
| 1811 | Py_DECREF(w); |
| 1812 | SET_TOP(x); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 1813 | if (x != NULL) DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1814 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1815 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1816 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1817 | TARGET_NOARG(INPLACE_OR) |
| 1818 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1819 | w = POP(); |
| 1820 | v = TOP(); |
| 1821 | x = PyNumber_InPlaceOr(v, w); |
| 1822 | Py_DECREF(v); |
| 1823 | Py_DECREF(w); |
| 1824 | SET_TOP(x); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 1825 | if (x != NULL) DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1826 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1827 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1828 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1829 | |
| 1830 | |
| 1831 | TARGET_WITH_IMPL_NOARG(SLICE, _slice) |
| 1832 | TARGET_WITH_IMPL_NOARG(SLICE_1, _slice) |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 1833 | TARGET_WITH_IMPL_NOARG(SLICE_2, _slice) |
| 1834 | TARGET_WITH_IMPL_NOARG(SLICE_3, _slice) |
| 1835 | _slice: |
| 1836 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1837 | if ((opcode-SLICE) & 2) |
| 1838 | w = POP(); |
| 1839 | else |
| 1840 | w = NULL; |
| 1841 | if ((opcode-SLICE) & 1) |
| 1842 | v = POP(); |
| 1843 | else |
| 1844 | v = NULL; |
| 1845 | u = TOP(); |
| 1846 | x = apply_slice(u, v, w); |
| 1847 | Py_DECREF(u); |
| 1848 | Py_XDECREF(v); |
| 1849 | Py_XDECREF(w); |
| 1850 | SET_TOP(x); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 1851 | if (x != NULL) DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1852 | break; |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 1853 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1854 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1855 | |
| 1856 | TARGET_WITH_IMPL_NOARG(STORE_SLICE, _store_slice) |
| 1857 | TARGET_WITH_IMPL_NOARG(STORE_SLICE_1, _store_slice) |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 1858 | TARGET_WITH_IMPL_NOARG(STORE_SLICE_2, _store_slice) |
| 1859 | TARGET_WITH_IMPL_NOARG(STORE_SLICE_3, _store_slice) |
| 1860 | _store_slice: |
| 1861 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1862 | if ((opcode-STORE_SLICE) & 2) |
| 1863 | w = POP(); |
| 1864 | else |
| 1865 | w = NULL; |
| 1866 | if ((opcode-STORE_SLICE) & 1) |
| 1867 | v = POP(); |
| 1868 | else |
| 1869 | v = NULL; |
| 1870 | u = POP(); |
| 1871 | t = POP(); |
| 1872 | err = assign_slice(u, v, w, t); /* u[v:w] = t */ |
| 1873 | Py_DECREF(t); |
| 1874 | Py_DECREF(u); |
| 1875 | Py_XDECREF(v); |
| 1876 | Py_XDECREF(w); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 1877 | if (err == 0) DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1878 | break; |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 1879 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1880 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1881 | |
| 1882 | TARGET_WITH_IMPL_NOARG(DELETE_SLICE, _delete_slice) |
| 1883 | TARGET_WITH_IMPL_NOARG(DELETE_SLICE_1, _delete_slice) |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 1884 | TARGET_WITH_IMPL_NOARG(DELETE_SLICE_2, _delete_slice) |
| 1885 | TARGET_WITH_IMPL_NOARG(DELETE_SLICE_3, _delete_slice) |
| 1886 | _delete_slice: |
| 1887 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1888 | if ((opcode-DELETE_SLICE) & 2) |
| 1889 | w = POP(); |
| 1890 | else |
| 1891 | w = NULL; |
| 1892 | if ((opcode-DELETE_SLICE) & 1) |
| 1893 | v = POP(); |
| 1894 | else |
| 1895 | v = NULL; |
| 1896 | u = POP(); |
| 1897 | err = assign_slice(u, v, w, (PyObject *)NULL); |
| 1898 | /* del u[v:w] */ |
| 1899 | Py_DECREF(u); |
| 1900 | Py_XDECREF(v); |
| 1901 | Py_XDECREF(w); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 1902 | if (err == 0) DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1903 | break; |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 1904 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1905 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1906 | TARGET_NOARG(STORE_SUBSCR) |
| 1907 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1908 | w = TOP(); |
| 1909 | v = SECOND(); |
| 1910 | u = THIRD(); |
| 1911 | STACKADJ(-3); |
| 1912 | /* v[w] = u */ |
| 1913 | err = PyObject_SetItem(v, w, u); |
| 1914 | Py_DECREF(u); |
| 1915 | Py_DECREF(v); |
| 1916 | Py_DECREF(w); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 1917 | if (err == 0) DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1918 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1919 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1920 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1921 | TARGET_NOARG(DELETE_SUBSCR) |
| 1922 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1923 | w = TOP(); |
| 1924 | v = SECOND(); |
| 1925 | STACKADJ(-2); |
| 1926 | /* del v[w] */ |
| 1927 | err = PyObject_DelItem(v, w); |
| 1928 | Py_DECREF(v); |
| 1929 | Py_DECREF(w); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 1930 | if (err == 0) DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1931 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1932 | } |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1933 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1934 | TARGET_NOARG(PRINT_EXPR) |
| 1935 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1936 | v = POP(); |
| 1937 | w = PySys_GetObject("displayhook"); |
| 1938 | if (w == NULL) { |
| 1939 | PyErr_SetString(PyExc_RuntimeError, |
| 1940 | "lost sys.displayhook"); |
| 1941 | err = -1; |
| 1942 | x = NULL; |
| 1943 | } |
| 1944 | if (err == 0) { |
| 1945 | x = PyTuple_Pack(1, v); |
| 1946 | if (x == NULL) |
| 1947 | err = -1; |
| 1948 | } |
| 1949 | if (err == 0) { |
| 1950 | w = PyEval_CallObject(w, x); |
| 1951 | Py_XDECREF(w); |
| 1952 | if (w == NULL) |
| 1953 | err = -1; |
| 1954 | } |
| 1955 | Py_DECREF(v); |
| 1956 | Py_XDECREF(x); |
| 1957 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1958 | } |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 1959 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1960 | TARGET_NOARG(PRINT_ITEM_TO) |
| 1961 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1962 | w = stream = POP(); |
| 1963 | /* fall through to PRINT_ITEM */ |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1964 | } |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1965 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 1966 | TARGET_NOARG(PRINT_ITEM) |
| 1967 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1968 | v = POP(); |
| 1969 | if (stream == NULL || stream == Py_None) { |
| 1970 | w = PySys_GetObject("stdout"); |
| 1971 | if (w == NULL) { |
| 1972 | PyErr_SetString(PyExc_RuntimeError, |
| 1973 | "lost sys.stdout"); |
| 1974 | err = -1; |
| 1975 | } |
| 1976 | } |
| 1977 | /* PyFile_SoftSpace() can exececute arbitrary code |
| 1978 | if sys.stdout is an instance with a __getattr__. |
| 1979 | If __getattr__ raises an exception, w will |
| 1980 | be freed, so we need to prevent that temporarily. */ |
| 1981 | Py_XINCREF(w); |
| 1982 | if (w != NULL && PyFile_SoftSpace(w, 0)) |
| 1983 | err = PyFile_WriteString(" ", w); |
| 1984 | if (err == 0) |
| 1985 | err = PyFile_WriteObject(v, w, Py_PRINT_RAW); |
| 1986 | if (err == 0) { |
| 1987 | /* XXX move into writeobject() ? */ |
| 1988 | if (PyString_Check(v)) { |
Stefan Krah | 7ff7825 | 2010-06-23 18:12:09 +0000 | [diff] [blame] | 1989 | char *s = PyString_AS_STRING(v); |
| 1990 | Py_ssize_t len = PyString_GET_SIZE(v); |
| 1991 | if (len == 0 || |
| 1992 | !isspace(Py_CHARMASK(s[len-1])) || |
| 1993 | s[len-1] == ' ') |
| 1994 | PyFile_SoftSpace(w, 1); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1995 | } |
Martin v. Löwis | 8d3ce5a | 2001-12-18 22:36:40 +0000 | [diff] [blame] | 1996 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1997 | else if (PyUnicode_Check(v)) { |
Stefan Krah | 7ff7825 | 2010-06-23 18:12:09 +0000 | [diff] [blame] | 1998 | Py_UNICODE *s = PyUnicode_AS_UNICODE(v); |
| 1999 | Py_ssize_t len = PyUnicode_GET_SIZE(v); |
| 2000 | if (len == 0 || |
| 2001 | !Py_UNICODE_ISSPACE(s[len-1]) || |
| 2002 | s[len-1] == ' ') |
| 2003 | PyFile_SoftSpace(w, 1); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2004 | } |
Michael W. Hudson | d95c828 | 2002-05-20 13:56:11 +0000 | [diff] [blame] | 2005 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2006 | else |
Stefan Krah | 7ff7825 | 2010-06-23 18:12:09 +0000 | [diff] [blame] | 2007 | PyFile_SoftSpace(w, 1); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2008 | } |
| 2009 | Py_XDECREF(w); |
| 2010 | Py_DECREF(v); |
| 2011 | Py_XDECREF(stream); |
| 2012 | stream = NULL; |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2013 | if (err == 0) DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2014 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2015 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2016 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2017 | TARGET_NOARG(PRINT_NEWLINE_TO) |
| 2018 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2019 | w = stream = POP(); |
| 2020 | /* fall through to PRINT_NEWLINE */ |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2021 | } |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 2022 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2023 | TARGET_NOARG(PRINT_NEWLINE) |
| 2024 | { |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2025 | if (stream == NULL || stream == Py_None) |
| 2026 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2027 | w = PySys_GetObject("stdout"); |
| 2028 | if (w == NULL) { |
| 2029 | PyErr_SetString(PyExc_RuntimeError, |
| 2030 | "lost sys.stdout"); |
| 2031 | why = WHY_EXCEPTION; |
| 2032 | } |
| 2033 | } |
| 2034 | if (w != NULL) { |
| 2035 | /* w.write() may replace sys.stdout, so we |
| 2036 | * have to keep our reference to it */ |
| 2037 | Py_INCREF(w); |
| 2038 | err = PyFile_WriteString("\n", w); |
| 2039 | if (err == 0) |
| 2040 | PyFile_SoftSpace(w, 0); |
| 2041 | Py_DECREF(w); |
| 2042 | } |
| 2043 | Py_XDECREF(stream); |
| 2044 | stream = NULL; |
| 2045 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2046 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2047 | |
| 2048 | #ifdef CASE_TOO_BIG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2049 | default: switch (opcode) { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2050 | #endif |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2051 | |
| 2052 | TARGET(RAISE_VARARGS) |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2053 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2054 | u = v = w = NULL; |
| 2055 | switch (oparg) { |
| 2056 | case 3: |
| 2057 | u = POP(); /* traceback */ |
| 2058 | /* Fallthrough */ |
| 2059 | case 2: |
| 2060 | v = POP(); /* value */ |
| 2061 | /* Fallthrough */ |
| 2062 | case 1: |
| 2063 | w = POP(); /* exc */ |
| 2064 | case 0: /* Fallthrough */ |
| 2065 | why = do_raise(w, v, u); |
| 2066 | break; |
| 2067 | default: |
| 2068 | PyErr_SetString(PyExc_SystemError, |
| 2069 | "bad RAISE_VARARGS oparg"); |
| 2070 | why = WHY_EXCEPTION; |
| 2071 | break; |
| 2072 | } |
| 2073 | break; |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2074 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2075 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2076 | TARGET_NOARG(LOAD_LOCALS) |
| 2077 | { |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2078 | if ((x = f->f_locals) != NULL) |
| 2079 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2080 | Py_INCREF(x); |
| 2081 | PUSH(x); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2082 | DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2083 | } |
| 2084 | PyErr_SetString(PyExc_SystemError, "no locals"); |
| 2085 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2086 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2087 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2088 | TARGET_NOARG(RETURN_VALUE) |
| 2089 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2090 | retval = POP(); |
| 2091 | why = WHY_RETURN; |
| 2092 | goto fast_block_end; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2093 | } |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 2094 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2095 | TARGET_NOARG(YIELD_VALUE) |
| 2096 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2097 | retval = POP(); |
| 2098 | f->f_stacktop = stack_pointer; |
| 2099 | why = WHY_YIELD; |
| 2100 | goto fast_yield; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2101 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2102 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2103 | TARGET_NOARG(EXEC_STMT) |
| 2104 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2105 | w = TOP(); |
| 2106 | v = SECOND(); |
| 2107 | u = THIRD(); |
| 2108 | STACKADJ(-3); |
| 2109 | READ_TIMESTAMP(intr0); |
| 2110 | err = exec_statement(f, u, v, w); |
| 2111 | READ_TIMESTAMP(intr1); |
| 2112 | Py_DECREF(u); |
| 2113 | Py_DECREF(v); |
| 2114 | Py_DECREF(w); |
| 2115 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2116 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2117 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2118 | TARGET_NOARG(POP_BLOCK) |
| 2119 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2120 | { |
| 2121 | PyTryBlock *b = PyFrame_BlockPop(f); |
| 2122 | while (STACK_LEVEL() > b->b_level) { |
| 2123 | v = POP(); |
| 2124 | Py_DECREF(v); |
| 2125 | } |
| 2126 | } |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2127 | DISPATCH(); |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2128 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2129 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2130 | PREDICTED(END_FINALLY); |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2131 | TARGET_NOARG(END_FINALLY) |
| 2132 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2133 | v = POP(); |
| 2134 | if (PyInt_Check(v)) { |
| 2135 | why = (enum why_code) PyInt_AS_LONG(v); |
| 2136 | assert(why != WHY_YIELD); |
| 2137 | if (why == WHY_RETURN || |
| 2138 | why == WHY_CONTINUE) |
| 2139 | retval = POP(); |
| 2140 | } |
| 2141 | else if (PyExceptionClass_Check(v) || |
| 2142 | PyString_Check(v)) { |
| 2143 | w = POP(); |
| 2144 | u = POP(); |
| 2145 | PyErr_Restore(v, w, u); |
| 2146 | why = WHY_RERAISE; |
| 2147 | break; |
| 2148 | } |
| 2149 | else if (v != Py_None) { |
| 2150 | PyErr_SetString(PyExc_SystemError, |
| 2151 | "'finally' pops bad exception"); |
| 2152 | why = WHY_EXCEPTION; |
| 2153 | } |
| 2154 | Py_DECREF(v); |
| 2155 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2156 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2157 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2158 | TARGET_NOARG(BUILD_CLASS) |
| 2159 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2160 | u = TOP(); |
| 2161 | v = SECOND(); |
| 2162 | w = THIRD(); |
| 2163 | STACKADJ(-2); |
| 2164 | x = build_class(u, v, w); |
| 2165 | SET_TOP(x); |
| 2166 | Py_DECREF(u); |
| 2167 | Py_DECREF(v); |
| 2168 | Py_DECREF(w); |
| 2169 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2170 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2171 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2172 | TARGET(STORE_NAME) |
| 2173 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2174 | w = GETITEM(names, oparg); |
| 2175 | v = POP(); |
| 2176 | if ((x = f->f_locals) != NULL) { |
| 2177 | if (PyDict_CheckExact(x)) |
| 2178 | err = PyDict_SetItem(x, w, v); |
| 2179 | else |
| 2180 | err = PyObject_SetItem(x, w, v); |
| 2181 | Py_DECREF(v); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2182 | if (err == 0) DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2183 | break; |
| 2184 | } |
Serhiy Storchaka | a86c091 | 2014-11-19 00:11:05 +0200 | [diff] [blame] | 2185 | t = PyObject_Repr(w); |
| 2186 | if (t == NULL) |
| 2187 | break; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2188 | PyErr_Format(PyExc_SystemError, |
| 2189 | "no locals found when storing %s", |
Serhiy Storchaka | a86c091 | 2014-11-19 00:11:05 +0200 | [diff] [blame] | 2190 | PyString_AS_STRING(t)); |
| 2191 | Py_DECREF(t); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2192 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2193 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2194 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2195 | TARGET(DELETE_NAME) |
| 2196 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2197 | w = GETITEM(names, oparg); |
| 2198 | if ((x = f->f_locals) != NULL) { |
| 2199 | if ((err = PyObject_DelItem(x, w)) != 0) |
| 2200 | format_exc_check_arg(PyExc_NameError, |
| 2201 | NAME_ERROR_MSG, |
| 2202 | w); |
| 2203 | break; |
| 2204 | } |
Serhiy Storchaka | a86c091 | 2014-11-19 00:11:05 +0200 | [diff] [blame] | 2205 | t = PyObject_Repr(w); |
| 2206 | if (t == NULL) |
| 2207 | break; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2208 | PyErr_Format(PyExc_SystemError, |
| 2209 | "no locals when deleting %s", |
Serhiy Storchaka | a86c091 | 2014-11-19 00:11:05 +0200 | [diff] [blame] | 2210 | PyString_AS_STRING(w)); |
| 2211 | Py_DECREF(t); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2212 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2213 | } |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 2214 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2215 | PREDICTED_WITH_ARG(UNPACK_SEQUENCE); |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2216 | TARGET(UNPACK_SEQUENCE) |
| 2217 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2218 | v = POP(); |
| 2219 | if (PyTuple_CheckExact(v) && |
| 2220 | PyTuple_GET_SIZE(v) == oparg) { |
| 2221 | PyObject **items = \ |
| 2222 | ((PyTupleObject *)v)->ob_item; |
| 2223 | while (oparg--) { |
| 2224 | w = items[oparg]; |
| 2225 | Py_INCREF(w); |
| 2226 | PUSH(w); |
| 2227 | } |
| 2228 | Py_DECREF(v); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2229 | DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2230 | } else if (PyList_CheckExact(v) && |
| 2231 | PyList_GET_SIZE(v) == oparg) { |
| 2232 | PyObject **items = \ |
| 2233 | ((PyListObject *)v)->ob_item; |
| 2234 | while (oparg--) { |
| 2235 | w = items[oparg]; |
| 2236 | Py_INCREF(w); |
| 2237 | PUSH(w); |
| 2238 | } |
| 2239 | } else if (unpack_iterable(v, oparg, |
| 2240 | stack_pointer + oparg)) { |
| 2241 | STACKADJ(oparg); |
| 2242 | } else { |
| 2243 | /* unpack_iterable() raised an exception */ |
| 2244 | why = WHY_EXCEPTION; |
| 2245 | } |
| 2246 | Py_DECREF(v); |
| 2247 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2248 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2249 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2250 | |
| 2251 | TARGET(STORE_ATTR) |
| 2252 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2253 | w = GETITEM(names, oparg); |
| 2254 | v = TOP(); |
| 2255 | u = SECOND(); |
| 2256 | STACKADJ(-2); |
| 2257 | err = PyObject_SetAttr(v, w, u); /* v.w = u */ |
| 2258 | Py_DECREF(v); |
| 2259 | Py_DECREF(u); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2260 | if (err == 0) DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2261 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2262 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2263 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2264 | TARGET(DELETE_ATTR) |
| 2265 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2266 | w = GETITEM(names, oparg); |
| 2267 | v = POP(); |
| 2268 | err = PyObject_SetAttr(v, w, (PyObject *)NULL); |
| 2269 | /* del v.w */ |
| 2270 | Py_DECREF(v); |
| 2271 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2272 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2273 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2274 | |
| 2275 | TARGET(STORE_GLOBAL) |
| 2276 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2277 | w = GETITEM(names, oparg); |
| 2278 | v = POP(); |
| 2279 | err = PyDict_SetItem(f->f_globals, w, v); |
| 2280 | Py_DECREF(v); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2281 | if (err == 0) DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2282 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2283 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2284 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2285 | TARGET(DELETE_GLOBAL) |
| 2286 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2287 | w = GETITEM(names, oparg); |
| 2288 | if ((err = PyDict_DelItem(f->f_globals, w)) != 0) |
| 2289 | format_exc_check_arg( |
| 2290 | PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w); |
| 2291 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2292 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2293 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2294 | TARGET(LOAD_NAME) |
| 2295 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2296 | w = GETITEM(names, oparg); |
| 2297 | if ((v = f->f_locals) == NULL) { |
Serhiy Storchaka | a86c091 | 2014-11-19 00:11:05 +0200 | [diff] [blame] | 2298 | why = WHY_EXCEPTION; |
| 2299 | t = PyObject_Repr(w); |
| 2300 | if (t == NULL) |
| 2301 | break; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2302 | PyErr_Format(PyExc_SystemError, |
| 2303 | "no locals when loading %s", |
Serhiy Storchaka | a86c091 | 2014-11-19 00:11:05 +0200 | [diff] [blame] | 2304 | PyString_AS_STRING(w)); |
| 2305 | Py_DECREF(t); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2306 | break; |
| 2307 | } |
| 2308 | if (PyDict_CheckExact(v)) { |
| 2309 | x = PyDict_GetItem(v, w); |
| 2310 | Py_XINCREF(x); |
| 2311 | } |
| 2312 | else { |
| 2313 | x = PyObject_GetItem(v, w); |
| 2314 | if (x == NULL && PyErr_Occurred()) { |
| 2315 | if (!PyErr_ExceptionMatches( |
| 2316 | PyExc_KeyError)) |
| 2317 | break; |
| 2318 | PyErr_Clear(); |
| 2319 | } |
| 2320 | } |
| 2321 | if (x == NULL) { |
| 2322 | x = PyDict_GetItem(f->f_globals, w); |
| 2323 | if (x == NULL) { |
| 2324 | x = PyDict_GetItem(f->f_builtins, w); |
| 2325 | if (x == NULL) { |
| 2326 | format_exc_check_arg( |
| 2327 | PyExc_NameError, |
| 2328 | NAME_ERROR_MSG, w); |
| 2329 | break; |
| 2330 | } |
| 2331 | } |
| 2332 | Py_INCREF(x); |
| 2333 | } |
| 2334 | PUSH(x); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2335 | DISPATCH(); |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2336 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2337 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2338 | TARGET(LOAD_GLOBAL) |
| 2339 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2340 | w = GETITEM(names, oparg); |
| 2341 | if (PyString_CheckExact(w)) { |
| 2342 | /* Inline the PyDict_GetItem() calls. |
| 2343 | WARNING: this is an extreme speed hack. |
| 2344 | Do not try this at home. */ |
| 2345 | long hash = ((PyStringObject *)w)->ob_shash; |
| 2346 | if (hash != -1) { |
| 2347 | PyDictObject *d; |
| 2348 | PyDictEntry *e; |
| 2349 | d = (PyDictObject *)(f->f_globals); |
| 2350 | e = d->ma_lookup(d, w, hash); |
| 2351 | if (e == NULL) { |
| 2352 | x = NULL; |
| 2353 | break; |
| 2354 | } |
| 2355 | x = e->me_value; |
| 2356 | if (x != NULL) { |
| 2357 | Py_INCREF(x); |
| 2358 | PUSH(x); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2359 | DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2360 | } |
| 2361 | d = (PyDictObject *)(f->f_builtins); |
| 2362 | e = d->ma_lookup(d, w, hash); |
| 2363 | if (e == NULL) { |
| 2364 | x = NULL; |
| 2365 | break; |
| 2366 | } |
| 2367 | x = e->me_value; |
| 2368 | if (x != NULL) { |
| 2369 | Py_INCREF(x); |
| 2370 | PUSH(x); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2371 | DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2372 | } |
| 2373 | goto load_global_error; |
| 2374 | } |
| 2375 | } |
| 2376 | /* This is the un-inlined version of the code above */ |
| 2377 | x = PyDict_GetItem(f->f_globals, w); |
| 2378 | if (x == NULL) { |
| 2379 | x = PyDict_GetItem(f->f_builtins, w); |
| 2380 | if (x == NULL) { |
| 2381 | load_global_error: |
| 2382 | format_exc_check_arg( |
| 2383 | PyExc_NameError, |
| 2384 | GLOBAL_NAME_ERROR_MSG, w); |
| 2385 | break; |
| 2386 | } |
| 2387 | } |
| 2388 | Py_INCREF(x); |
| 2389 | PUSH(x); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2390 | DISPATCH(); |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2391 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2392 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2393 | TARGET(DELETE_FAST) |
| 2394 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2395 | x = GETLOCAL(oparg); |
| 2396 | if (x != NULL) { |
| 2397 | SETLOCAL(oparg, NULL); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2398 | DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2399 | } |
| 2400 | format_exc_check_arg( |
| 2401 | PyExc_UnboundLocalError, |
| 2402 | UNBOUNDLOCAL_ERROR_MSG, |
| 2403 | PyTuple_GetItem(co->co_varnames, oparg) |
| 2404 | ); |
| 2405 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2406 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2407 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2408 | TARGET(LOAD_CLOSURE) |
| 2409 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2410 | x = freevars[oparg]; |
| 2411 | Py_INCREF(x); |
| 2412 | PUSH(x); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2413 | if (x != NULL) DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2414 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2415 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2416 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2417 | TARGET(LOAD_DEREF) |
| 2418 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2419 | x = freevars[oparg]; |
| 2420 | w = PyCell_Get(x); |
| 2421 | if (w != NULL) { |
| 2422 | PUSH(w); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2423 | DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2424 | } |
| 2425 | err = -1; |
| 2426 | /* Don't stomp existing exception */ |
| 2427 | if (PyErr_Occurred()) |
| 2428 | break; |
| 2429 | if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) { |
| 2430 | v = PyTuple_GET_ITEM(co->co_cellvars, |
Stefan Krah | 7ff7825 | 2010-06-23 18:12:09 +0000 | [diff] [blame] | 2431 | oparg); |
| 2432 | format_exc_check_arg( |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2433 | PyExc_UnboundLocalError, |
| 2434 | UNBOUNDLOCAL_ERROR_MSG, |
| 2435 | v); |
| 2436 | } else { |
| 2437 | v = PyTuple_GET_ITEM(co->co_freevars, oparg - |
| 2438 | PyTuple_GET_SIZE(co->co_cellvars)); |
| 2439 | format_exc_check_arg(PyExc_NameError, |
| 2440 | UNBOUNDFREE_ERROR_MSG, v); |
| 2441 | } |
| 2442 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2443 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2444 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2445 | TARGET(STORE_DEREF) |
| 2446 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2447 | w = POP(); |
| 2448 | x = freevars[oparg]; |
| 2449 | PyCell_Set(x, w); |
| 2450 | Py_DECREF(w); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2451 | DISPATCH(); |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2452 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2453 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2454 | TARGET(BUILD_TUPLE) |
| 2455 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2456 | x = PyTuple_New(oparg); |
| 2457 | if (x != NULL) { |
| 2458 | for (; --oparg >= 0;) { |
| 2459 | w = POP(); |
| 2460 | PyTuple_SET_ITEM(x, oparg, w); |
| 2461 | } |
| 2462 | PUSH(x); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2463 | DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2464 | } |
| 2465 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2466 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2467 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2468 | TARGET(BUILD_LIST) |
| 2469 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2470 | x = PyList_New(oparg); |
| 2471 | if (x != NULL) { |
| 2472 | for (; --oparg >= 0;) { |
| 2473 | w = POP(); |
| 2474 | PyList_SET_ITEM(x, oparg, w); |
| 2475 | } |
| 2476 | PUSH(x); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2477 | DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2478 | } |
| 2479 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2480 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2481 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2482 | TARGET(BUILD_SET) |
| 2483 | { |
Raymond Hettinger | e62a694 | 2016-09-08 15:25:19 -0700 | [diff] [blame] | 2484 | int i; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2485 | x = PySet_New(NULL); |
| 2486 | if (x != NULL) { |
Raymond Hettinger | e62a694 | 2016-09-08 15:25:19 -0700 | [diff] [blame] | 2487 | for (i = oparg; i > 0; i--) { |
| 2488 | w = PEEK(i); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2489 | if (err == 0) |
| 2490 | err = PySet_Add(x, w); |
| 2491 | Py_DECREF(w); |
| 2492 | } |
Raymond Hettinger | e62a694 | 2016-09-08 15:25:19 -0700 | [diff] [blame] | 2493 | STACKADJ(-oparg); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2494 | if (err != 0) { |
| 2495 | Py_DECREF(x); |
| 2496 | break; |
| 2497 | } |
| 2498 | PUSH(x); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2499 | DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2500 | } |
| 2501 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2502 | } |
Alexandre Vassalotti | ee936a2 | 2010-01-09 23:35:54 +0000 | [diff] [blame] | 2503 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2504 | TARGET(BUILD_MAP) |
| 2505 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2506 | x = _PyDict_NewPresized((Py_ssize_t)oparg); |
| 2507 | PUSH(x); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2508 | if (x != NULL) DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2509 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2510 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2511 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2512 | TARGET_NOARG(STORE_MAP) |
| 2513 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2514 | w = TOP(); /* key */ |
| 2515 | u = SECOND(); /* value */ |
| 2516 | v = THIRD(); /* dict */ |
| 2517 | STACKADJ(-2); |
| 2518 | assert (PyDict_CheckExact(v)); |
| 2519 | err = PyDict_SetItem(v, w, u); /* v[w] = u */ |
| 2520 | Py_DECREF(u); |
| 2521 | Py_DECREF(w); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2522 | if (err == 0) DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2523 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2524 | } |
Raymond Hettinger | effde12 | 2007-12-18 18:26:18 +0000 | [diff] [blame] | 2525 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2526 | TARGET(MAP_ADD) |
| 2527 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2528 | w = TOP(); /* key */ |
| 2529 | u = SECOND(); /* value */ |
| 2530 | STACKADJ(-2); |
| 2531 | v = stack_pointer[-oparg]; /* dict */ |
| 2532 | assert (PyDict_CheckExact(v)); |
| 2533 | err = PyDict_SetItem(v, w, u); /* v[w] = u */ |
| 2534 | Py_DECREF(u); |
| 2535 | Py_DECREF(w); |
| 2536 | if (err == 0) { |
| 2537 | PREDICT(JUMP_ABSOLUTE); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2538 | DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2539 | } |
| 2540 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2541 | } |
Alexandre Vassalotti | b646547 | 2010-01-11 22:36:12 +0000 | [diff] [blame] | 2542 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2543 | TARGET(LOAD_ATTR) |
| 2544 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2545 | w = GETITEM(names, oparg); |
| 2546 | v = TOP(); |
| 2547 | x = PyObject_GetAttr(v, w); |
| 2548 | Py_DECREF(v); |
| 2549 | SET_TOP(x); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2550 | if (x != NULL) DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2551 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2552 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2553 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2554 | TARGET(COMPARE_OP) |
| 2555 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2556 | w = POP(); |
| 2557 | v = TOP(); |
| 2558 | if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) { |
| 2559 | /* INLINE: cmp(int, int) */ |
| 2560 | register long a, b; |
| 2561 | register int res; |
| 2562 | a = PyInt_AS_LONG(v); |
| 2563 | b = PyInt_AS_LONG(w); |
| 2564 | switch (oparg) { |
| 2565 | case PyCmp_LT: res = a < b; break; |
| 2566 | case PyCmp_LE: res = a <= b; break; |
| 2567 | case PyCmp_EQ: res = a == b; break; |
| 2568 | case PyCmp_NE: res = a != b; break; |
| 2569 | case PyCmp_GT: res = a > b; break; |
| 2570 | case PyCmp_GE: res = a >= b; break; |
| 2571 | case PyCmp_IS: res = v == w; break; |
| 2572 | case PyCmp_IS_NOT: res = v != w; break; |
| 2573 | default: goto slow_compare; |
| 2574 | } |
| 2575 | x = res ? Py_True : Py_False; |
| 2576 | Py_INCREF(x); |
| 2577 | } |
| 2578 | else { |
| 2579 | slow_compare: |
| 2580 | x = cmp_outcome(oparg, v, w); |
| 2581 | } |
| 2582 | Py_DECREF(v); |
| 2583 | Py_DECREF(w); |
| 2584 | SET_TOP(x); |
| 2585 | if (x == NULL) break; |
| 2586 | PREDICT(POP_JUMP_IF_FALSE); |
| 2587 | PREDICT(POP_JUMP_IF_TRUE); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2588 | DISPATCH(); |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2589 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2590 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2591 | TARGET(IMPORT_NAME) |
| 2592 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2593 | w = GETITEM(names, oparg); |
| 2594 | x = PyDict_GetItemString(f->f_builtins, "__import__"); |
| 2595 | if (x == NULL) { |
| 2596 | PyErr_SetString(PyExc_ImportError, |
| 2597 | "__import__ not found"); |
| 2598 | break; |
| 2599 | } |
| 2600 | Py_INCREF(x); |
| 2601 | v = POP(); |
| 2602 | u = TOP(); |
| 2603 | if (PyInt_AsLong(u) != -1 || PyErr_Occurred()) |
| 2604 | w = PyTuple_Pack(5, |
| 2605 | w, |
| 2606 | f->f_globals, |
| 2607 | f->f_locals == NULL ? |
| 2608 | Py_None : f->f_locals, |
| 2609 | v, |
| 2610 | u); |
| 2611 | else |
| 2612 | w = PyTuple_Pack(4, |
| 2613 | w, |
| 2614 | f->f_globals, |
| 2615 | f->f_locals == NULL ? |
| 2616 | Py_None : f->f_locals, |
| 2617 | v); |
| 2618 | Py_DECREF(v); |
| 2619 | Py_DECREF(u); |
| 2620 | if (w == NULL) { |
| 2621 | u = POP(); |
| 2622 | Py_DECREF(x); |
| 2623 | x = NULL; |
| 2624 | break; |
| 2625 | } |
| 2626 | READ_TIMESTAMP(intr0); |
| 2627 | v = x; |
| 2628 | x = PyEval_CallObject(v, w); |
| 2629 | Py_DECREF(v); |
| 2630 | READ_TIMESTAMP(intr1); |
| 2631 | Py_DECREF(w); |
| 2632 | SET_TOP(x); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2633 | if (x != NULL) DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2634 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2635 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2636 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2637 | TARGET_NOARG(IMPORT_STAR) |
| 2638 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2639 | v = POP(); |
| 2640 | PyFrame_FastToLocals(f); |
| 2641 | if ((x = f->f_locals) == NULL) { |
| 2642 | PyErr_SetString(PyExc_SystemError, |
| 2643 | "no locals found during 'import *'"); |
Serhiy Storchaka | 9fbb65e | 2017-03-08 13:44:33 +0200 | [diff] [blame] | 2644 | Py_DECREF(v); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2645 | break; |
| 2646 | } |
| 2647 | READ_TIMESTAMP(intr0); |
| 2648 | err = import_all_from(x, v); |
| 2649 | READ_TIMESTAMP(intr1); |
| 2650 | PyFrame_LocalsToFast(f, 0); |
| 2651 | Py_DECREF(v); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2652 | if (err == 0) DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2653 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2654 | } |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2655 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2656 | TARGET(IMPORT_FROM) |
| 2657 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2658 | w = GETITEM(names, oparg); |
| 2659 | v = TOP(); |
| 2660 | READ_TIMESTAMP(intr0); |
| 2661 | x = import_from(v, w); |
| 2662 | READ_TIMESTAMP(intr1); |
| 2663 | PUSH(x); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2664 | if (x != NULL) DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2665 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2666 | } |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2667 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2668 | TARGET(JUMP_FORWARD) |
| 2669 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2670 | JUMPBY(oparg); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2671 | FAST_DISPATCH(); |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2672 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2673 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2674 | PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE); |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2675 | TARGET(POP_JUMP_IF_FALSE) |
| 2676 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2677 | w = POP(); |
| 2678 | if (w == Py_True) { |
| 2679 | Py_DECREF(w); |
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 | if (w == Py_False) { |
| 2683 | Py_DECREF(w); |
| 2684 | JUMPTO(oparg); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2685 | FAST_DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2686 | } |
| 2687 | err = PyObject_IsTrue(w); |
| 2688 | Py_DECREF(w); |
| 2689 | if (err > 0) |
| 2690 | err = 0; |
| 2691 | else if (err == 0) |
| 2692 | JUMPTO(oparg); |
| 2693 | else |
| 2694 | break; |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2695 | DISPATCH(); |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2696 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2697 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2698 | PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE); |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2699 | TARGET(POP_JUMP_IF_TRUE) |
| 2700 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2701 | w = POP(); |
| 2702 | if (w == Py_False) { |
| 2703 | Py_DECREF(w); |
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 | if (w == Py_True) { |
| 2707 | Py_DECREF(w); |
| 2708 | JUMPTO(oparg); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2709 | FAST_DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2710 | } |
| 2711 | err = PyObject_IsTrue(w); |
| 2712 | Py_DECREF(w); |
| 2713 | if (err > 0) { |
| 2714 | err = 0; |
| 2715 | JUMPTO(oparg); |
| 2716 | } |
| 2717 | else if (err == 0) |
| 2718 | ; |
| 2719 | else |
| 2720 | break; |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2721 | DISPATCH(); |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2722 | } |
Jeffrey Yasskin | 68d6852 | 2009-02-28 19:03:21 +0000 | [diff] [blame] | 2723 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2724 | TARGET(JUMP_IF_FALSE_OR_POP) |
| 2725 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2726 | w = TOP(); |
| 2727 | if (w == Py_True) { |
| 2728 | STACKADJ(-1); |
| 2729 | Py_DECREF(w); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2730 | FAST_DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2731 | } |
| 2732 | if (w == Py_False) { |
| 2733 | JUMPTO(oparg); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2734 | FAST_DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2735 | } |
| 2736 | err = PyObject_IsTrue(w); |
| 2737 | if (err > 0) { |
| 2738 | STACKADJ(-1); |
| 2739 | Py_DECREF(w); |
| 2740 | err = 0; |
| 2741 | } |
| 2742 | else if (err == 0) |
| 2743 | JUMPTO(oparg); |
| 2744 | else |
| 2745 | break; |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2746 | DISPATCH(); |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2747 | } |
Jeffrey Yasskin | 68d6852 | 2009-02-28 19:03:21 +0000 | [diff] [blame] | 2748 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2749 | TARGET(JUMP_IF_TRUE_OR_POP) |
| 2750 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2751 | w = TOP(); |
| 2752 | if (w == Py_False) { |
| 2753 | STACKADJ(-1); |
| 2754 | Py_DECREF(w); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2755 | FAST_DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2756 | } |
| 2757 | if (w == Py_True) { |
| 2758 | JUMPTO(oparg); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2759 | FAST_DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2760 | } |
| 2761 | err = PyObject_IsTrue(w); |
| 2762 | if (err > 0) { |
| 2763 | err = 0; |
| 2764 | JUMPTO(oparg); |
| 2765 | } |
| 2766 | else if (err == 0) { |
| 2767 | STACKADJ(-1); |
| 2768 | Py_DECREF(w); |
| 2769 | } |
| 2770 | else |
| 2771 | break; |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2772 | DISPATCH(); |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2773 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2774 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2775 | PREDICTED_WITH_ARG(JUMP_ABSOLUTE); |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2776 | TARGET(JUMP_ABSOLUTE) |
| 2777 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2778 | JUMPTO(oparg); |
Raymond Hettinger | dc1d1ba | 2007-11-07 02:45:46 +0000 | [diff] [blame] | 2779 | #if FAST_LOOPS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2780 | /* Enabling this path speeds-up all while and for-loops by bypassing |
| 2781 | the per-loop checks for signals. By default, this should be turned-off |
| 2782 | because it prevents detection of a control-break in tight loops like |
| 2783 | "while 1: pass". Compile with this option turned-on when you need |
| 2784 | the speed-up and do not need break checking inside tight loops (ones |
| 2785 | that contain only instructions ending with goto fast_next_opcode). |
| 2786 | */ |
| 2787 | goto fast_next_opcode; |
Raymond Hettinger | dc1d1ba | 2007-11-07 02:45:46 +0000 | [diff] [blame] | 2788 | #else |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2789 | DISPATCH(); |
Raymond Hettinger | dc1d1ba | 2007-11-07 02:45:46 +0000 | [diff] [blame] | 2790 | #endif |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2791 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2792 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2793 | TARGET_NOARG(GET_ITER) |
| 2794 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2795 | /* before: [obj]; after [getiter(obj)] */ |
| 2796 | v = TOP(); |
| 2797 | x = PyObject_GetIter(v); |
| 2798 | Py_DECREF(v); |
| 2799 | if (x != NULL) { |
| 2800 | SET_TOP(x); |
| 2801 | PREDICT(FOR_ITER); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2802 | DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2803 | } |
| 2804 | STACKADJ(-1); |
| 2805 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2806 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2807 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2808 | PREDICTED_WITH_ARG(FOR_ITER); |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2809 | TARGET(FOR_ITER) |
| 2810 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2811 | /* before: [iter]; after: [iter, iter()] *or* [] */ |
| 2812 | v = TOP(); |
| 2813 | x = (*v->ob_type->tp_iternext)(v); |
| 2814 | if (x != NULL) { |
| 2815 | PUSH(x); |
| 2816 | PREDICT(STORE_FAST); |
| 2817 | PREDICT(UNPACK_SEQUENCE); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2818 | DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2819 | } |
| 2820 | if (PyErr_Occurred()) { |
| 2821 | if (!PyErr_ExceptionMatches( |
| 2822 | PyExc_StopIteration)) |
| 2823 | break; |
| 2824 | PyErr_Clear(); |
| 2825 | } |
| 2826 | /* iterator ended normally */ |
| 2827 | x = v = POP(); |
| 2828 | Py_DECREF(v); |
| 2829 | JUMPBY(oparg); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2830 | DISPATCH(); |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2831 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2832 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2833 | TARGET_NOARG(BREAK_LOOP) |
| 2834 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2835 | why = WHY_BREAK; |
| 2836 | goto fast_block_end; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2837 | } |
Raymond Hettinger | 2d783e9 | 2004-03-12 09:12:22 +0000 | [diff] [blame] | 2838 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2839 | TARGET(CONTINUE_LOOP) |
| 2840 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2841 | retval = PyInt_FromLong(oparg); |
| 2842 | if (!retval) { |
| 2843 | x = NULL; |
| 2844 | break; |
| 2845 | } |
| 2846 | why = WHY_CONTINUE; |
| 2847 | goto fast_block_end; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2848 | } |
Raymond Hettinger | 2d783e9 | 2004-03-12 09:12:22 +0000 | [diff] [blame] | 2849 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2850 | TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally) |
| 2851 | TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally) |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2852 | TARGET(SETUP_FINALLY) |
| 2853 | _setup_finally: |
| 2854 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2855 | /* NOTE: If you add any new block-setup opcodes that |
| 2856 | are not try/except/finally handlers, you may need |
| 2857 | to update the PyGen_NeedsFinalizing() function. |
| 2858 | */ |
Phillip J. Eby | 2ba9661 | 2006-04-10 17:51:05 +0000 | [diff] [blame] | 2859 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2860 | PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg, |
| 2861 | STACK_LEVEL()); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2862 | DISPATCH(); |
| 2863 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2864 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2865 | |
| 2866 | |
| 2867 | TARGET(SETUP_WITH) |
| 2868 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2869 | { |
| 2870 | static PyObject *exit, *enter; |
| 2871 | w = TOP(); |
| 2872 | x = special_lookup(w, "__exit__", &exit); |
| 2873 | if (!x) |
| 2874 | break; |
| 2875 | SET_TOP(x); |
| 2876 | u = special_lookup(w, "__enter__", &enter); |
| 2877 | Py_DECREF(w); |
| 2878 | if (!u) { |
| 2879 | x = NULL; |
| 2880 | break; |
| 2881 | } |
| 2882 | x = PyObject_CallFunctionObjArgs(u, NULL); |
| 2883 | Py_DECREF(u); |
| 2884 | if (!x) |
| 2885 | break; |
| 2886 | /* Setup a finally block (SETUP_WITH as a block is |
| 2887 | equivalent to SETUP_FINALLY except it normalizes |
| 2888 | the exception) before pushing the result of |
| 2889 | __enter__ on the stack. */ |
| 2890 | PyFrame_BlockSetup(f, SETUP_WITH, INSTR_OFFSET() + oparg, |
| 2891 | STACK_LEVEL()); |
Benjamin Peterson | 1880d8b | 2009-05-25 13:13:44 +0000 | [diff] [blame] | 2892 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2893 | PUSH(x); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2894 | DISPATCH(); |
| 2895 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2896 | } |
Benjamin Peterson | 1880d8b | 2009-05-25 13:13:44 +0000 | [diff] [blame] | 2897 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2898 | TARGET_NOARG(WITH_CLEANUP) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2899 | { |
| 2900 | /* At the top of the stack are 1-3 values indicating |
| 2901 | how/why we entered the finally clause: |
| 2902 | - TOP = None |
| 2903 | - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval |
| 2904 | - TOP = WHY_*; no retval below it |
| 2905 | - (TOP, SECOND, THIRD) = exc_info() |
| 2906 | Below them is EXIT, the context.__exit__ bound method. |
| 2907 | In the last case, we must call |
| 2908 | EXIT(TOP, SECOND, THIRD) |
| 2909 | otherwise we must call |
| 2910 | EXIT(None, None, None) |
Nick Coghlan | 7af53be | 2008-03-07 14:13:28 +0000 | [diff] [blame] | 2911 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2912 | In all cases, we remove EXIT from the stack, leaving |
| 2913 | the rest in the same order. |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 2914 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2915 | In addition, if the stack represents an exception, |
| 2916 | *and* the function call returns a 'true' value, we |
| 2917 | "zap" this information, to prevent END_FINALLY from |
| 2918 | re-raising the exception. (But non-local gotos |
| 2919 | should still be resumed.) |
| 2920 | */ |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 2921 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2922 | PyObject *exit_func; |
Nick Coghlan | 7af53be | 2008-03-07 14:13:28 +0000 | [diff] [blame] | 2923 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2924 | u = POP(); |
| 2925 | if (u == Py_None) { |
| 2926 | exit_func = TOP(); |
| 2927 | SET_TOP(u); |
| 2928 | v = w = Py_None; |
| 2929 | } |
| 2930 | else if (PyInt_Check(u)) { |
| 2931 | switch(PyInt_AS_LONG(u)) { |
| 2932 | case WHY_RETURN: |
| 2933 | case WHY_CONTINUE: |
| 2934 | /* Retval in TOP. */ |
| 2935 | exit_func = SECOND(); |
| 2936 | SET_SECOND(TOP()); |
| 2937 | SET_TOP(u); |
| 2938 | break; |
| 2939 | default: |
| 2940 | exit_func = TOP(); |
| 2941 | SET_TOP(u); |
| 2942 | break; |
| 2943 | } |
| 2944 | u = v = w = Py_None; |
| 2945 | } |
| 2946 | else { |
| 2947 | v = TOP(); |
| 2948 | w = SECOND(); |
| 2949 | exit_func = THIRD(); |
| 2950 | SET_TOP(u); |
| 2951 | SET_SECOND(v); |
| 2952 | SET_THIRD(w); |
| 2953 | } |
| 2954 | /* XXX Not the fastest way to call it... */ |
| 2955 | x = PyObject_CallFunctionObjArgs(exit_func, u, v, w, |
| 2956 | NULL); |
| 2957 | Py_DECREF(exit_func); |
| 2958 | if (x == NULL) |
| 2959 | break; /* Go to error exit */ |
Amaury Forgeot d'Arc | ad9b599 | 2008-12-10 23:22:49 +0000 | [diff] [blame] | 2960 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2961 | if (u != Py_None) |
| 2962 | err = PyObject_IsTrue(x); |
| 2963 | else |
| 2964 | err = 0; |
| 2965 | Py_DECREF(x); |
Amaury Forgeot d'Arc | ad9b599 | 2008-12-10 23:22:49 +0000 | [diff] [blame] | 2966 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2967 | if (err < 0) |
| 2968 | break; /* Go to error exit */ |
| 2969 | else if (err > 0) { |
| 2970 | err = 0; |
| 2971 | /* There was an exception and a true return */ |
| 2972 | STACKADJ(-2); |
| 2973 | Py_INCREF(Py_None); |
| 2974 | SET_TOP(Py_None); |
| 2975 | Py_DECREF(u); |
| 2976 | Py_DECREF(v); |
| 2977 | Py_DECREF(w); |
| 2978 | } else { |
| 2979 | /* The stack was rearranged to remove EXIT |
| 2980 | above. Let END_FINALLY do its thing */ |
| 2981 | } |
| 2982 | PREDICT(END_FINALLY); |
| 2983 | break; |
| 2984 | } |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 2985 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2986 | TARGET(CALL_FUNCTION) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2987 | { |
| 2988 | PyObject **sp; |
| 2989 | PCALL(PCALL_ALL); |
| 2990 | sp = stack_pointer; |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 2991 | #ifdef WITH_TSC |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2992 | x = call_function(&sp, oparg, &intr0, &intr1); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 2993 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2994 | x = call_function(&sp, oparg); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 2995 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2996 | stack_pointer = sp; |
| 2997 | PUSH(x); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2998 | if (x != NULL) DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2999 | break; |
| 3000 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3001 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 3002 | TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw) |
| 3003 | TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw) |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 3004 | TARGET(CALL_FUNCTION_VAR_KW) |
| 3005 | _call_function_var_kw: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3006 | { |
| 3007 | int na = oparg & 0xff; |
| 3008 | int nk = (oparg>>8) & 0xff; |
| 3009 | int flags = (opcode - CALL_FUNCTION) & 3; |
| 3010 | int n = na + 2 * nk; |
| 3011 | PyObject **pfunc, *func, **sp; |
| 3012 | PCALL(PCALL_ALL); |
| 3013 | if (flags & CALL_FLAG_VAR) |
| 3014 | n++; |
| 3015 | if (flags & CALL_FLAG_KW) |
| 3016 | n++; |
| 3017 | pfunc = stack_pointer - n - 1; |
| 3018 | func = *pfunc; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3019 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3020 | if (PyMethod_Check(func) |
Stefan Krah | 7ff7825 | 2010-06-23 18:12:09 +0000 | [diff] [blame] | 3021 | && PyMethod_GET_SELF(func) != NULL) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3022 | PyObject *self = PyMethod_GET_SELF(func); |
| 3023 | Py_INCREF(self); |
| 3024 | func = PyMethod_GET_FUNCTION(func); |
| 3025 | Py_INCREF(func); |
| 3026 | Py_DECREF(*pfunc); |
| 3027 | *pfunc = self; |
| 3028 | na++; |
| 3029 | } else |
| 3030 | Py_INCREF(func); |
| 3031 | sp = stack_pointer; |
| 3032 | READ_TIMESTAMP(intr0); |
| 3033 | x = ext_do_call(func, &sp, flags, na, nk); |
| 3034 | READ_TIMESTAMP(intr1); |
| 3035 | stack_pointer = sp; |
| 3036 | Py_DECREF(func); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3037 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3038 | while (stack_pointer > pfunc) { |
| 3039 | w = POP(); |
| 3040 | Py_DECREF(w); |
| 3041 | } |
| 3042 | PUSH(x); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 3043 | if (x != NULL) DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3044 | break; |
| 3045 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3046 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 3047 | |
| 3048 | TARGET(MAKE_FUNCTION) |
| 3049 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3050 | v = POP(); /* code object */ |
| 3051 | x = PyFunction_New(v, f->f_globals); |
| 3052 | Py_DECREF(v); |
| 3053 | /* XXX Maybe this should be a separate opcode? */ |
| 3054 | if (x != NULL && oparg > 0) { |
| 3055 | v = PyTuple_New(oparg); |
| 3056 | if (v == NULL) { |
| 3057 | Py_DECREF(x); |
| 3058 | x = NULL; |
| 3059 | break; |
| 3060 | } |
| 3061 | while (--oparg >= 0) { |
| 3062 | w = POP(); |
| 3063 | PyTuple_SET_ITEM(v, oparg, w); |
| 3064 | } |
| 3065 | err = PyFunction_SetDefaults(x, v); |
| 3066 | Py_DECREF(v); |
| 3067 | } |
| 3068 | PUSH(x); |
| 3069 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 3070 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 3071 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 3072 | TARGET(MAKE_CLOSURE) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3073 | { |
| 3074 | v = POP(); /* code object */ |
| 3075 | x = PyFunction_New(v, f->f_globals); |
| 3076 | Py_DECREF(v); |
| 3077 | if (x != NULL) { |
| 3078 | v = POP(); |
| 3079 | if (PyFunction_SetClosure(x, v) != 0) { |
| 3080 | /* Can't happen unless bytecode is corrupt. */ |
| 3081 | why = WHY_EXCEPTION; |
| 3082 | } |
| 3083 | Py_DECREF(v); |
| 3084 | } |
| 3085 | if (x != NULL && oparg > 0) { |
| 3086 | v = PyTuple_New(oparg); |
| 3087 | if (v == NULL) { |
| 3088 | Py_DECREF(x); |
| 3089 | x = NULL; |
| 3090 | break; |
| 3091 | } |
| 3092 | while (--oparg >= 0) { |
| 3093 | w = POP(); |
| 3094 | PyTuple_SET_ITEM(v, oparg, w); |
| 3095 | } |
| 3096 | if (PyFunction_SetDefaults(x, v) != 0) { |
| 3097 | /* Can't happen unless |
| 3098 | PyFunction_SetDefaults changes. */ |
| 3099 | why = WHY_EXCEPTION; |
| 3100 | } |
| 3101 | Py_DECREF(v); |
| 3102 | } |
| 3103 | PUSH(x); |
| 3104 | break; |
| 3105 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3106 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 3107 | TARGET(BUILD_SLICE) |
| 3108 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3109 | if (oparg == 3) |
| 3110 | w = POP(); |
| 3111 | else |
| 3112 | w = NULL; |
| 3113 | v = POP(); |
| 3114 | u = TOP(); |
| 3115 | x = PySlice_New(u, v, w); |
| 3116 | Py_DECREF(u); |
| 3117 | Py_DECREF(v); |
| 3118 | Py_XDECREF(w); |
| 3119 | SET_TOP(x); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 3120 | if (x != NULL) DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3121 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 3122 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 3123 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 3124 | TARGET(EXTENDED_ARG) |
| 3125 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3126 | opcode = NEXTOP(); |
| 3127 | oparg = oparg<<16 | NEXTARG(); |
| 3128 | goto dispatch_opcode; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 3129 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 3130 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 3131 | #if USE_COMPUTED_GOTOS |
| 3132 | _unknown_opcode: |
| 3133 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3134 | default: |
| 3135 | fprintf(stderr, |
| 3136 | "XXX lineno: %d, opcode: %d\n", |
| 3137 | PyFrame_GetLineNumber(f), |
| 3138 | opcode); |
| 3139 | PyErr_SetString(PyExc_SystemError, "unknown opcode"); |
| 3140 | why = WHY_EXCEPTION; |
| 3141 | break; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 3142 | |
| 3143 | #ifdef CASE_TOO_BIG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3144 | } |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 3145 | #endif |
| 3146 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3147 | } /* switch */ |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3148 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3149 | on_error: |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3150 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3151 | READ_TIMESTAMP(inst1); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 3152 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3153 | /* Quickly continue if no error occurred */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3154 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3155 | if (why == WHY_NOT) { |
| 3156 | if (err == 0 && x != NULL) { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3157 | #ifdef CHECKEXC |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3158 | /* This check is expensive! */ |
| 3159 | if (PyErr_Occurred()) |
| 3160 | fprintf(stderr, |
| 3161 | "XXX undetected error\n"); |
| 3162 | else { |
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 | READ_TIMESTAMP(loop1); |
| 3165 | continue; /* Normal, fast path */ |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 3166 | #ifdef CHECKEXC |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3167 | } |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 3168 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3169 | } |
| 3170 | why = WHY_EXCEPTION; |
| 3171 | x = Py_None; |
| 3172 | err = 0; |
| 3173 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3174 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3175 | /* Double-check exception status */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3176 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3177 | if (why == WHY_EXCEPTION || why == WHY_RERAISE) { |
| 3178 | if (!PyErr_Occurred()) { |
| 3179 | PyErr_SetString(PyExc_SystemError, |
| 3180 | "error return without exception set"); |
| 3181 | why = WHY_EXCEPTION; |
| 3182 | } |
| 3183 | } |
Guido van Rossum | eb894eb | 1999-03-09 16:16:45 +0000 | [diff] [blame] | 3184 | #ifdef CHECKEXC |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3185 | else { |
| 3186 | /* This check is expensive! */ |
| 3187 | if (PyErr_Occurred()) { |
| 3188 | char buf[128]; |
| 3189 | sprintf(buf, "Stack unwind with exception " |
| 3190 | "set and why=%d", why); |
| 3191 | Py_FatalError(buf); |
| 3192 | } |
| 3193 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3194 | #endif |
| 3195 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3196 | /* Log traceback info if this is a real exception */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3197 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3198 | if (why == WHY_EXCEPTION) { |
| 3199 | PyTraceBack_Here(f); |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 3200 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3201 | if (tstate->c_tracefunc != NULL) |
| 3202 | call_exc_trace(tstate->c_tracefunc, |
| 3203 | tstate->c_traceobj, f); |
| 3204 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3205 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3206 | /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3207 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3208 | if (why == WHY_RERAISE) |
| 3209 | why = WHY_EXCEPTION; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3210 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3211 | /* Unwind stacks if a (pseudo) exception occurred */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3212 | |
Raymond Hettinger | 1dd8309 | 2004-02-06 18:32:33 +0000 | [diff] [blame] | 3213 | fast_block_end: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3214 | while (why != WHY_NOT && f->f_iblock > 0) { |
| 3215 | /* Peek at the current block. */ |
| 3216 | PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1]; |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3217 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3218 | assert(why != WHY_YIELD); |
| 3219 | if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) { |
| 3220 | why = WHY_NOT; |
| 3221 | JUMPTO(PyInt_AS_LONG(retval)); |
| 3222 | Py_DECREF(retval); |
| 3223 | break; |
| 3224 | } |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3225 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3226 | /* Now we have to pop the block. */ |
| 3227 | f->f_iblock--; |
Benjamin Peterson | 4a3cf19 | 2009-07-01 23:45:19 +0000 | [diff] [blame] | 3228 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3229 | while (STACK_LEVEL() > b->b_level) { |
| 3230 | v = POP(); |
| 3231 | Py_XDECREF(v); |
| 3232 | } |
| 3233 | if (b->b_type == SETUP_LOOP && why == WHY_BREAK) { |
| 3234 | why = WHY_NOT; |
| 3235 | JUMPTO(b->b_handler); |
| 3236 | break; |
| 3237 | } |
| 3238 | if (b->b_type == SETUP_FINALLY || |
| 3239 | (b->b_type == SETUP_EXCEPT && |
| 3240 | why == WHY_EXCEPTION) || |
| 3241 | b->b_type == SETUP_WITH) { |
| 3242 | if (why == WHY_EXCEPTION) { |
| 3243 | PyObject *exc, *val, *tb; |
| 3244 | PyErr_Fetch(&exc, &val, &tb); |
| 3245 | if (val == NULL) { |
| 3246 | val = Py_None; |
| 3247 | Py_INCREF(val); |
| 3248 | } |
| 3249 | /* Make the raw exception data |
| 3250 | available to the handler, |
| 3251 | so a program can emulate the |
| 3252 | Python main loop. Don't do |
| 3253 | this for 'finally'. */ |
| 3254 | if (b->b_type == SETUP_EXCEPT || |
| 3255 | b->b_type == SETUP_WITH) { |
| 3256 | PyErr_NormalizeException( |
| 3257 | &exc, &val, &tb); |
| 3258 | set_exc_info(tstate, |
| 3259 | exc, val, tb); |
| 3260 | } |
| 3261 | if (tb == NULL) { |
| 3262 | Py_INCREF(Py_None); |
| 3263 | PUSH(Py_None); |
| 3264 | } else |
| 3265 | PUSH(tb); |
| 3266 | PUSH(val); |
| 3267 | PUSH(exc); |
| 3268 | } |
| 3269 | else { |
| 3270 | if (why & (WHY_RETURN | WHY_CONTINUE)) |
| 3271 | PUSH(retval); |
| 3272 | v = PyInt_FromLong((long)why); |
| 3273 | PUSH(v); |
| 3274 | } |
| 3275 | why = WHY_NOT; |
| 3276 | JUMPTO(b->b_handler); |
| 3277 | break; |
| 3278 | } |
| 3279 | } /* unwind stack */ |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3280 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3281 | /* End the loop if we still have an error (or return) */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3282 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3283 | if (why != WHY_NOT) |
| 3284 | break; |
| 3285 | READ_TIMESTAMP(loop1); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3286 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3287 | } /* main loop */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3288 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3289 | assert(why != WHY_YIELD); |
| 3290 | /* Pop remaining stack entries. */ |
| 3291 | while (!EMPTY()) { |
| 3292 | v = POP(); |
| 3293 | Py_XDECREF(v); |
| 3294 | } |
Guido van Rossum | 35974fb | 2001-12-06 21:28:18 +0000 | [diff] [blame] | 3295 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3296 | if (why != WHY_RETURN) |
| 3297 | retval = NULL; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3298 | |
Raymond Hettinger | 1dd8309 | 2004-02-06 18:32:33 +0000 | [diff] [blame] | 3299 | fast_yield: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3300 | if (tstate->use_tracing) { |
| 3301 | if (tstate->c_tracefunc) { |
| 3302 | if (why == WHY_RETURN || why == WHY_YIELD) { |
| 3303 | if (call_trace(tstate->c_tracefunc, |
| 3304 | tstate->c_traceobj, f, |
| 3305 | PyTrace_RETURN, retval)) { |
| 3306 | Py_XDECREF(retval); |
| 3307 | retval = NULL; |
| 3308 | why = WHY_EXCEPTION; |
| 3309 | } |
| 3310 | } |
| 3311 | else if (why == WHY_EXCEPTION) { |
| 3312 | call_trace_protected(tstate->c_tracefunc, |
| 3313 | tstate->c_traceobj, f, |
| 3314 | PyTrace_RETURN, NULL); |
| 3315 | } |
| 3316 | } |
| 3317 | if (tstate->c_profilefunc) { |
| 3318 | if (why == WHY_EXCEPTION) |
| 3319 | call_trace_protected(tstate->c_profilefunc, |
| 3320 | tstate->c_profileobj, f, |
| 3321 | PyTrace_RETURN, NULL); |
| 3322 | else if (call_trace(tstate->c_profilefunc, |
| 3323 | tstate->c_profileobj, f, |
| 3324 | PyTrace_RETURN, retval)) { |
| 3325 | Py_XDECREF(retval); |
| 3326 | retval = NULL; |
| 3327 | why = WHY_EXCEPTION; |
| 3328 | } |
| 3329 | } |
| 3330 | } |
Guido van Rossum | a424013 | 1997-01-21 21:18:36 +0000 | [diff] [blame] | 3331 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3332 | if (tstate->frame->f_exc_type != NULL) |
| 3333 | reset_exc_info(tstate); |
| 3334 | else { |
| 3335 | assert(tstate->frame->f_exc_value == NULL); |
| 3336 | assert(tstate->frame->f_exc_traceback == NULL); |
| 3337 | } |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3338 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3339 | /* pop frame */ |
Thomas Wouters | ae406c6 | 2007-09-19 17:27:43 +0000 | [diff] [blame] | 3340 | exit_eval_frame: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3341 | Py_LeaveRecursiveCall(); |
| 3342 | tstate->frame = f->f_back; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3343 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3344 | return retval; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3345 | } |
| 3346 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3347 | /* 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] | 3348 | PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3349 | the test in the if statements in Misc/gdbinit (pystack and pystackv). */ |
Skip Montanaro | 786ea6b | 2004-03-01 15:44:05 +0000 | [diff] [blame] | 3350 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3351 | PyObject * |
| 3352 | PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3353 | PyObject **args, int argcount, PyObject **kws, int kwcount, |
| 3354 | PyObject **defs, int defcount, PyObject *closure) |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3355 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3356 | register PyFrameObject *f; |
| 3357 | register PyObject *retval = NULL; |
| 3358 | register PyObject **fastlocals, **freevars; |
| 3359 | PyThreadState *tstate = PyThreadState_GET(); |
| 3360 | PyObject *x, *u; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3361 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3362 | if (globals == NULL) { |
| 3363 | PyErr_SetString(PyExc_SystemError, |
| 3364 | "PyEval_EvalCodeEx: NULL globals"); |
| 3365 | return NULL; |
| 3366 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3367 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3368 | assert(tstate != NULL); |
| 3369 | assert(globals != NULL); |
| 3370 | f = PyFrame_New(tstate, co, globals, locals); |
| 3371 | if (f == NULL) |
| 3372 | return NULL; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3373 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3374 | fastlocals = f->f_localsplus; |
| 3375 | freevars = f->f_localsplus + co->co_nlocals; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3376 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3377 | if (co->co_argcount > 0 || |
| 3378 | co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) { |
| 3379 | int i; |
| 3380 | int n = argcount; |
| 3381 | PyObject *kwdict = NULL; |
| 3382 | if (co->co_flags & CO_VARKEYWORDS) { |
| 3383 | kwdict = PyDict_New(); |
| 3384 | if (kwdict == NULL) |
| 3385 | goto fail; |
| 3386 | i = co->co_argcount; |
| 3387 | if (co->co_flags & CO_VARARGS) |
| 3388 | i++; |
| 3389 | SETLOCAL(i, kwdict); |
| 3390 | } |
| 3391 | if (argcount > co->co_argcount) { |
| 3392 | if (!(co->co_flags & CO_VARARGS)) { |
| 3393 | PyErr_Format(PyExc_TypeError, |
| 3394 | "%.200s() takes %s %d " |
| 3395 | "argument%s (%d given)", |
| 3396 | PyString_AsString(co->co_name), |
| 3397 | defcount ? "at most" : "exactly", |
| 3398 | co->co_argcount, |
| 3399 | co->co_argcount == 1 ? "" : "s", |
Benjamin Peterson | da4faba | 2010-09-25 03:27:12 +0000 | [diff] [blame] | 3400 | argcount + kwcount); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3401 | goto fail; |
| 3402 | } |
| 3403 | n = co->co_argcount; |
| 3404 | } |
| 3405 | for (i = 0; i < n; i++) { |
| 3406 | x = args[i]; |
| 3407 | Py_INCREF(x); |
| 3408 | SETLOCAL(i, x); |
| 3409 | } |
| 3410 | if (co->co_flags & CO_VARARGS) { |
| 3411 | u = PyTuple_New(argcount - n); |
| 3412 | if (u == NULL) |
| 3413 | goto fail; |
| 3414 | SETLOCAL(co->co_argcount, u); |
| 3415 | for (i = n; i < argcount; i++) { |
| 3416 | x = args[i]; |
| 3417 | Py_INCREF(x); |
| 3418 | PyTuple_SET_ITEM(u, i-n, x); |
| 3419 | } |
| 3420 | } |
| 3421 | for (i = 0; i < kwcount; i++) { |
| 3422 | PyObject **co_varnames; |
| 3423 | PyObject *keyword = kws[2*i]; |
| 3424 | PyObject *value = kws[2*i + 1]; |
| 3425 | int j; |
| 3426 | if (keyword == NULL || !(PyString_Check(keyword) |
Benjamin Peterson | 78821dd | 2009-01-25 17:15:10 +0000 | [diff] [blame] | 3427 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3428 | || PyUnicode_Check(keyword) |
Benjamin Peterson | 78821dd | 2009-01-25 17:15:10 +0000 | [diff] [blame] | 3429 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3430 | )) { |
| 3431 | PyErr_Format(PyExc_TypeError, |
| 3432 | "%.200s() keywords must be strings", |
| 3433 | PyString_AsString(co->co_name)); |
| 3434 | goto fail; |
| 3435 | } |
| 3436 | /* Speed hack: do raw pointer compares. As names are |
| 3437 | normally interned this should almost always hit. */ |
| 3438 | co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item; |
| 3439 | for (j = 0; j < co->co_argcount; j++) { |
| 3440 | PyObject *nm = co_varnames[j]; |
| 3441 | if (nm == keyword) |
| 3442 | goto kw_found; |
| 3443 | } |
| 3444 | /* Slow fallback, just in case */ |
| 3445 | for (j = 0; j < co->co_argcount; j++) { |
| 3446 | PyObject *nm = co_varnames[j]; |
| 3447 | int cmp = PyObject_RichCompareBool( |
| 3448 | keyword, nm, Py_EQ); |
| 3449 | if (cmp > 0) |
| 3450 | goto kw_found; |
| 3451 | else if (cmp < 0) |
| 3452 | goto fail; |
| 3453 | } |
| 3454 | if (kwdict == NULL) { |
| 3455 | PyObject *kwd_str = kwd_as_string(keyword); |
| 3456 | if (kwd_str) { |
| 3457 | PyErr_Format(PyExc_TypeError, |
| 3458 | "%.200s() got an unexpected " |
| 3459 | "keyword argument '%.400s'", |
| 3460 | PyString_AsString(co->co_name), |
| 3461 | PyString_AsString(kwd_str)); |
| 3462 | Py_DECREF(kwd_str); |
| 3463 | } |
| 3464 | goto fail; |
| 3465 | } |
| 3466 | PyDict_SetItem(kwdict, keyword, value); |
| 3467 | continue; |
| 3468 | kw_found: |
| 3469 | if (GETLOCAL(j) != NULL) { |
| 3470 | PyObject *kwd_str = kwd_as_string(keyword); |
| 3471 | if (kwd_str) { |
| 3472 | PyErr_Format(PyExc_TypeError, |
| 3473 | "%.200s() got multiple " |
| 3474 | "values for keyword " |
| 3475 | "argument '%.400s'", |
| 3476 | PyString_AsString(co->co_name), |
| 3477 | PyString_AsString(kwd_str)); |
| 3478 | Py_DECREF(kwd_str); |
| 3479 | } |
| 3480 | goto fail; |
| 3481 | } |
| 3482 | Py_INCREF(value); |
| 3483 | SETLOCAL(j, value); |
| 3484 | } |
| 3485 | if (argcount < co->co_argcount) { |
| 3486 | int m = co->co_argcount - defcount; |
| 3487 | for (i = argcount; i < m; i++) { |
| 3488 | if (GETLOCAL(i) == NULL) { |
| 3489 | int j, given = 0; |
| 3490 | for (j = 0; j < co->co_argcount; j++) |
| 3491 | if (GETLOCAL(j)) |
| 3492 | given++; |
| 3493 | PyErr_Format(PyExc_TypeError, |
| 3494 | "%.200s() takes %s %d " |
| 3495 | "argument%s (%d given)", |
| 3496 | PyString_AsString(co->co_name), |
| 3497 | ((co->co_flags & CO_VARARGS) || |
| 3498 | defcount) ? "at least" |
| 3499 | : "exactly", |
| 3500 | m, m == 1 ? "" : "s", given); |
| 3501 | goto fail; |
| 3502 | } |
| 3503 | } |
| 3504 | if (n > m) |
| 3505 | i = n - m; |
| 3506 | else |
| 3507 | i = 0; |
| 3508 | for (; i < defcount; i++) { |
| 3509 | if (GETLOCAL(m+i) == NULL) { |
| 3510 | PyObject *def = defs[i]; |
| 3511 | Py_INCREF(def); |
| 3512 | SETLOCAL(m+i, def); |
| 3513 | } |
| 3514 | } |
| 3515 | } |
| 3516 | } |
| 3517 | else if (argcount > 0 || kwcount > 0) { |
| 3518 | PyErr_Format(PyExc_TypeError, |
| 3519 | "%.200s() takes no arguments (%d given)", |
| 3520 | PyString_AsString(co->co_name), |
| 3521 | argcount + kwcount); |
| 3522 | goto fail; |
| 3523 | } |
| 3524 | /* Allocate and initialize storage for cell vars, and copy free |
| 3525 | vars into frame. This isn't too efficient right now. */ |
| 3526 | if (PyTuple_GET_SIZE(co->co_cellvars)) { |
| 3527 | int i, j, nargs, found; |
| 3528 | char *cellname, *argname; |
| 3529 | PyObject *c; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3530 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3531 | nargs = co->co_argcount; |
| 3532 | if (co->co_flags & CO_VARARGS) |
| 3533 | nargs++; |
| 3534 | if (co->co_flags & CO_VARKEYWORDS) |
| 3535 | nargs++; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3536 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3537 | /* Initialize each cell var, taking into account |
| 3538 | cell vars that are initialized from arguments. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3539 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3540 | Should arrange for the compiler to put cellvars |
| 3541 | that are arguments at the beginning of the cellvars |
| 3542 | list so that we can march over it more efficiently? |
| 3543 | */ |
| 3544 | for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) { |
| 3545 | cellname = PyString_AS_STRING( |
| 3546 | PyTuple_GET_ITEM(co->co_cellvars, i)); |
| 3547 | found = 0; |
| 3548 | for (j = 0; j < nargs; j++) { |
| 3549 | argname = PyString_AS_STRING( |
| 3550 | PyTuple_GET_ITEM(co->co_varnames, j)); |
| 3551 | if (strcmp(cellname, argname) == 0) { |
| 3552 | c = PyCell_New(GETLOCAL(j)); |
| 3553 | if (c == NULL) |
| 3554 | goto fail; |
| 3555 | GETLOCAL(co->co_nlocals + i) = c; |
| 3556 | found = 1; |
| 3557 | break; |
| 3558 | } |
| 3559 | } |
| 3560 | if (found == 0) { |
| 3561 | c = PyCell_New(NULL); |
| 3562 | if (c == NULL) |
| 3563 | goto fail; |
| 3564 | SETLOCAL(co->co_nlocals + i, c); |
| 3565 | } |
| 3566 | } |
| 3567 | } |
| 3568 | if (PyTuple_GET_SIZE(co->co_freevars)) { |
| 3569 | int i; |
| 3570 | for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) { |
| 3571 | PyObject *o = PyTuple_GET_ITEM(closure, i); |
| 3572 | Py_INCREF(o); |
| 3573 | freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o; |
| 3574 | } |
| 3575 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3576 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3577 | if (co->co_flags & CO_GENERATOR) { |
| 3578 | /* Don't need to keep the reference to f_back, it will be set |
| 3579 | * when the generator is resumed. */ |
Serhiy Storchaka | 98a9722 | 2014-02-09 13:14:04 +0200 | [diff] [blame] | 3580 | Py_CLEAR(f->f_back); |
Neil Schemenauer | 2b13ce8 | 2001-06-21 02:41:10 +0000 | [diff] [blame] | 3581 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3582 | PCALL(PCALL_GENERATOR); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 3583 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3584 | /* Create a new generator that owns the ready to run frame |
| 3585 | * and return that as the value. */ |
| 3586 | return PyGen_New(f); |
| 3587 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3588 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3589 | retval = PyEval_EvalFrameEx(f,0); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3590 | |
Thomas Wouters | ae406c6 | 2007-09-19 17:27:43 +0000 | [diff] [blame] | 3591 | fail: /* Jump here from prelude on failure */ |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3592 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3593 | /* decref'ing the frame can cause __del__ methods to get invoked, |
| 3594 | which can call back into Python. While we're done with the |
| 3595 | current Python frame (f), the associated C stack is still in use, |
| 3596 | so recursion_depth must be boosted for the duration. |
| 3597 | */ |
| 3598 | assert(tstate != NULL); |
| 3599 | ++tstate->recursion_depth; |
| 3600 | Py_DECREF(f); |
| 3601 | --tstate->recursion_depth; |
| 3602 | return retval; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3603 | } |
| 3604 | |
| 3605 | |
Benjamin Peterson | 1880d8b | 2009-05-25 13:13:44 +0000 | [diff] [blame] | 3606 | static PyObject * |
| 3607 | special_lookup(PyObject *o, char *meth, PyObject **cache) |
| 3608 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3609 | PyObject *res; |
| 3610 | if (PyInstance_Check(o)) { |
| 3611 | if (!*cache) |
| 3612 | return PyObject_GetAttrString(o, meth); |
| 3613 | else |
| 3614 | return PyObject_GetAttr(o, *cache); |
| 3615 | } |
| 3616 | res = _PyObject_LookupSpecial(o, meth, cache); |
| 3617 | if (res == NULL && !PyErr_Occurred()) { |
| 3618 | PyErr_SetObject(PyExc_AttributeError, *cache); |
| 3619 | return NULL; |
| 3620 | } |
| 3621 | return res; |
Benjamin Peterson | 1880d8b | 2009-05-25 13:13:44 +0000 | [diff] [blame] | 3622 | } |
| 3623 | |
Benjamin Peterson | 78821dd | 2009-01-25 17:15:10 +0000 | [diff] [blame] | 3624 | |
Benjamin Peterson | e18ef19 | 2009-01-20 14:21:16 +0000 | [diff] [blame] | 3625 | static PyObject * |
| 3626 | kwd_as_string(PyObject *kwd) { |
Benjamin Peterson | 78821dd | 2009-01-25 17:15:10 +0000 | [diff] [blame] | 3627 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3628 | if (PyString_Check(kwd)) { |
Benjamin Peterson | 78821dd | 2009-01-25 17:15:10 +0000 | [diff] [blame] | 3629 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3630 | assert(PyString_Check(kwd)); |
Benjamin Peterson | 78821dd | 2009-01-25 17:15:10 +0000 | [diff] [blame] | 3631 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3632 | Py_INCREF(kwd); |
| 3633 | return kwd; |
Benjamin Peterson | 78821dd | 2009-01-25 17:15:10 +0000 | [diff] [blame] | 3634 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3635 | } |
| 3636 | return _PyUnicode_AsDefaultEncodedString(kwd, "replace"); |
Benjamin Peterson | 78821dd | 2009-01-25 17:15:10 +0000 | [diff] [blame] | 3637 | #endif |
Benjamin Peterson | e18ef19 | 2009-01-20 14:21:16 +0000 | [diff] [blame] | 3638 | } |
| 3639 | |
| 3640 | |
Guido van Rossum | c9fbb72 | 2003-03-01 03:36:33 +0000 | [diff] [blame] | 3641 | /* Implementation notes for set_exc_info() and reset_exc_info(): |
| 3642 | |
| 3643 | - Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and |
| 3644 | 'exc_traceback'. These always travel together. |
| 3645 | |
| 3646 | - tstate->curexc_ZZZ is the "hot" exception that is set by |
| 3647 | PyErr_SetString(), cleared by PyErr_Clear(), and so on. |
| 3648 | |
| 3649 | - Once an exception is caught by an except clause, it is transferred |
| 3650 | from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info() |
| 3651 | 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] | 3652 | 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] | 3653 | |
| 3654 | - Now let me explain the complicated dance with frame->f_exc_ZZZ. |
| 3655 | |
| 3656 | Long ago, when none of this existed, there were just a few globals: |
| 3657 | one set corresponding to the "hot" exception, and one set |
| 3658 | corresponding to sys.exc_ZZZ. (Actually, the latter weren't C |
| 3659 | globals; they were simply stored as sys.exc_ZZZ. For backwards |
| 3660 | compatibility, they still are!) The problem was that in code like |
| 3661 | this: |
| 3662 | |
| 3663 | try: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3664 | "something that may fail" |
Guido van Rossum | c9fbb72 | 2003-03-01 03:36:33 +0000 | [diff] [blame] | 3665 | except "some exception": |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3666 | "do something else first" |
| 3667 | "print the exception from sys.exc_ZZZ." |
Guido van Rossum | c9fbb72 | 2003-03-01 03:36:33 +0000 | [diff] [blame] | 3668 | |
| 3669 | if "do something else first" invoked something that raised and caught |
| 3670 | an exception, sys.exc_ZZZ were overwritten. That was a frequent |
| 3671 | cause of subtle bugs. I fixed this by changing the semantics as |
| 3672 | follows: |
| 3673 | |
| 3674 | - Within one frame, sys.exc_ZZZ will hold the last exception caught |
| 3675 | *in that frame*. |
| 3676 | |
| 3677 | - But initially, and as long as no exception is caught in a given |
| 3678 | frame, sys.exc_ZZZ will hold the last exception caught in the |
| 3679 | previous frame (or the frame before that, etc.). |
| 3680 | |
| 3681 | The first bullet fixed the bug in the above example. The second |
| 3682 | bullet was for backwards compatibility: it was (and is) common to |
| 3683 | have a function that is called when an exception is caught, and to |
| 3684 | have that function access the caught exception via sys.exc_ZZZ. |
| 3685 | (Example: traceback.print_exc()). |
| 3686 | |
| 3687 | At the same time I fixed the problem that sys.exc_ZZZ weren't |
| 3688 | thread-safe, by introducing sys.exc_info() which gets it from tstate; |
| 3689 | but that's really a separate improvement. |
| 3690 | |
| 3691 | The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ |
| 3692 | variables to what they were before the current frame was called. The |
| 3693 | set_exc_info() function saves them on the frame so that |
| 3694 | reset_exc_info() can restore them. The invariant is that |
| 3695 | frame->f_exc_ZZZ is NULL iff the current frame never caught an |
| 3696 | exception (where "catching" an exception applies only to successful |
| 3697 | except clauses); and if the current frame ever caught an exception, |
| 3698 | frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ |
| 3699 | at the start of the current frame. |
| 3700 | |
| 3701 | */ |
| 3702 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 3703 | static void |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3704 | set_exc_info(PyThreadState *tstate, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3705 | PyObject *type, PyObject *value, PyObject *tb) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3706 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3707 | PyFrameObject *frame = tstate->frame; |
| 3708 | PyObject *tmp_type, *tmp_value, *tmp_tb; |
Barry Warsaw | 4249f54 | 1997-08-22 21:26:19 +0000 | [diff] [blame] | 3709 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3710 | assert(type != NULL); |
| 3711 | assert(frame != NULL); |
| 3712 | if (frame->f_exc_type == NULL) { |
| 3713 | assert(frame->f_exc_value == NULL); |
| 3714 | assert(frame->f_exc_traceback == NULL); |
| 3715 | /* This frame didn't catch an exception before. */ |
| 3716 | /* Save previous exception of this thread in this frame. */ |
| 3717 | if (tstate->exc_type == NULL) { |
| 3718 | /* XXX Why is this set to Py_None? */ |
| 3719 | Py_INCREF(Py_None); |
| 3720 | tstate->exc_type = Py_None; |
| 3721 | } |
| 3722 | Py_INCREF(tstate->exc_type); |
| 3723 | Py_XINCREF(tstate->exc_value); |
| 3724 | Py_XINCREF(tstate->exc_traceback); |
| 3725 | frame->f_exc_type = tstate->exc_type; |
| 3726 | frame->f_exc_value = tstate->exc_value; |
| 3727 | frame->f_exc_traceback = tstate->exc_traceback; |
| 3728 | } |
| 3729 | /* Set new exception for this thread. */ |
| 3730 | tmp_type = tstate->exc_type; |
| 3731 | tmp_value = tstate->exc_value; |
| 3732 | tmp_tb = tstate->exc_traceback; |
| 3733 | Py_INCREF(type); |
| 3734 | Py_XINCREF(value); |
| 3735 | Py_XINCREF(tb); |
| 3736 | tstate->exc_type = type; |
| 3737 | tstate->exc_value = value; |
| 3738 | tstate->exc_traceback = tb; |
| 3739 | Py_XDECREF(tmp_type); |
| 3740 | Py_XDECREF(tmp_value); |
| 3741 | Py_XDECREF(tmp_tb); |
| 3742 | /* For b/w compatibility */ |
| 3743 | PySys_SetObject("exc_type", type); |
| 3744 | PySys_SetObject("exc_value", value); |
| 3745 | PySys_SetObject("exc_traceback", tb); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3746 | } |
| 3747 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 3748 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3749 | reset_exc_info(PyThreadState *tstate) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3750 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3751 | PyFrameObject *frame; |
| 3752 | PyObject *tmp_type, *tmp_value, *tmp_tb; |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 3753 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3754 | /* It's a precondition that the thread state's frame caught an |
| 3755 | * exception -- verify in a debug build. |
| 3756 | */ |
| 3757 | assert(tstate != NULL); |
| 3758 | frame = tstate->frame; |
| 3759 | assert(frame != NULL); |
| 3760 | assert(frame->f_exc_type != NULL); |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 3761 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3762 | /* Copy the frame's exception info back to the thread state. */ |
| 3763 | tmp_type = tstate->exc_type; |
| 3764 | tmp_value = tstate->exc_value; |
| 3765 | tmp_tb = tstate->exc_traceback; |
| 3766 | Py_INCREF(frame->f_exc_type); |
| 3767 | Py_XINCREF(frame->f_exc_value); |
| 3768 | Py_XINCREF(frame->f_exc_traceback); |
| 3769 | tstate->exc_type = frame->f_exc_type; |
| 3770 | tstate->exc_value = frame->f_exc_value; |
| 3771 | tstate->exc_traceback = frame->f_exc_traceback; |
| 3772 | Py_XDECREF(tmp_type); |
| 3773 | Py_XDECREF(tmp_value); |
| 3774 | Py_XDECREF(tmp_tb); |
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 | /* For b/w compatibility */ |
| 3777 | PySys_SetObject("exc_type", frame->f_exc_type); |
| 3778 | PySys_SetObject("exc_value", frame->f_exc_value); |
| 3779 | PySys_SetObject("exc_traceback", frame->f_exc_traceback); |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 3780 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3781 | /* Clear the frame's exception info. */ |
| 3782 | tmp_type = frame->f_exc_type; |
| 3783 | tmp_value = frame->f_exc_value; |
| 3784 | tmp_tb = frame->f_exc_traceback; |
| 3785 | frame->f_exc_type = NULL; |
| 3786 | frame->f_exc_value = NULL; |
| 3787 | frame->f_exc_traceback = NULL; |
| 3788 | Py_DECREF(tmp_type); |
| 3789 | Py_XDECREF(tmp_value); |
| 3790 | Py_XDECREF(tmp_tb); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3791 | } |
| 3792 | |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3793 | /* Logic for the raise statement (too complicated for inlining). |
| 3794 | This *consumes* a reference count to each of its arguments. */ |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 3795 | static enum why_code |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3796 | do_raise(PyObject *type, PyObject *value, PyObject *tb) |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3797 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3798 | if (type == NULL) { |
| 3799 | /* Reraise */ |
| 3800 | PyThreadState *tstate = PyThreadState_GET(); |
| 3801 | type = tstate->exc_type == NULL ? Py_None : tstate->exc_type; |
| 3802 | value = tstate->exc_value; |
| 3803 | tb = tstate->exc_traceback; |
| 3804 | Py_XINCREF(type); |
| 3805 | Py_XINCREF(value); |
| 3806 | Py_XINCREF(tb); |
| 3807 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3808 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3809 | /* We support the following forms of raise: |
| 3810 | raise <class>, <classinstance> |
| 3811 | raise <class>, <argument tuple> |
| 3812 | raise <class>, None |
| 3813 | raise <class>, <argument> |
| 3814 | raise <classinstance>, None |
| 3815 | raise <string>, <object> |
| 3816 | raise <string>, None |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3817 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3818 | An omitted second argument is the same as None. |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3819 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3820 | In addition, raise <tuple>, <anything> is the same as |
| 3821 | raising the tuple's first item (and it better have one!); |
| 3822 | this rule is applied recursively. |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3823 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3824 | Finally, an optional third argument can be supplied, which |
| 3825 | gives the traceback to be substituted (useful when |
| 3826 | re-raising an exception after examining it). */ |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3827 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3828 | /* First, check the traceback argument, replacing None with |
| 3829 | NULL. */ |
| 3830 | if (tb == Py_None) { |
| 3831 | Py_DECREF(tb); |
| 3832 | tb = NULL; |
| 3833 | } |
| 3834 | else if (tb != NULL && !PyTraceBack_Check(tb)) { |
| 3835 | PyErr_SetString(PyExc_TypeError, |
| 3836 | "raise: arg 3 must be a traceback or None"); |
| 3837 | goto raise_error; |
| 3838 | } |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3839 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3840 | /* Next, replace a missing value with None */ |
| 3841 | if (value == NULL) { |
| 3842 | value = Py_None; |
| 3843 | Py_INCREF(value); |
| 3844 | } |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3845 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3846 | /* Next, repeatedly, replace a tuple exception with its first item */ |
| 3847 | while (PyTuple_Check(type) && PyTuple_Size(type) > 0) { |
| 3848 | PyObject *tmp = type; |
| 3849 | type = PyTuple_GET_ITEM(type, 0); |
| 3850 | Py_INCREF(type); |
| 3851 | Py_DECREF(tmp); |
| 3852 | } |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3853 | |
Benjamin Peterson | c3349cd | 2011-07-15 14:15:40 -0500 | [diff] [blame] | 3854 | if (PyExceptionClass_Check(type)) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3855 | PyErr_NormalizeException(&type, &value, &tb); |
Benjamin Peterson | c3349cd | 2011-07-15 14:15:40 -0500 | [diff] [blame] | 3856 | if (!PyExceptionInstance_Check(value)) { |
| 3857 | PyErr_Format(PyExc_TypeError, |
| 3858 | "calling %s() should have returned an instance of " |
| 3859 | "BaseException, not '%s'", |
| 3860 | ((PyTypeObject *)type)->tp_name, |
| 3861 | Py_TYPE(value)->tp_name); |
| 3862 | goto raise_error; |
| 3863 | } |
| 3864 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3865 | else if (PyExceptionInstance_Check(type)) { |
| 3866 | /* Raising an instance. The value should be a dummy. */ |
| 3867 | if (value != Py_None) { |
| 3868 | PyErr_SetString(PyExc_TypeError, |
| 3869 | "instance exception may not have a separate value"); |
| 3870 | goto raise_error; |
| 3871 | } |
| 3872 | else { |
| 3873 | /* Normalize to raise <class>, <instance> */ |
| 3874 | Py_DECREF(value); |
| 3875 | value = type; |
| 3876 | type = PyExceptionInstance_Class(type); |
| 3877 | Py_INCREF(type); |
| 3878 | } |
| 3879 | } |
| 3880 | else { |
| 3881 | /* Not something you can raise. You get an exception |
| 3882 | anyway, just not what you specified :-) */ |
| 3883 | PyErr_Format(PyExc_TypeError, |
| 3884 | "exceptions must be old-style classes or " |
| 3885 | "derived from BaseException, not %s", |
| 3886 | type->ob_type->tp_name); |
| 3887 | goto raise_error; |
| 3888 | } |
Guido van Rossum | 504153d | 2008-03-18 04:26:48 +0000 | [diff] [blame] | 3889 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3890 | assert(PyExceptionClass_Check(type)); |
| 3891 | if (Py_Py3kWarningFlag && PyClass_Check(type)) { |
| 3892 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
| 3893 | "exceptions must derive from BaseException " |
| 3894 | "in 3.x", 1) < 0) |
| 3895 | goto raise_error; |
| 3896 | } |
Guido van Rossum | 504153d | 2008-03-18 04:26:48 +0000 | [diff] [blame] | 3897 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3898 | PyErr_Restore(type, value, tb); |
| 3899 | if (tb == NULL) |
| 3900 | return WHY_EXCEPTION; |
| 3901 | else |
| 3902 | return WHY_RERAISE; |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3903 | raise_error: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3904 | Py_XDECREF(value); |
| 3905 | Py_XDECREF(type); |
| 3906 | Py_XDECREF(tb); |
| 3907 | return WHY_EXCEPTION; |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3908 | } |
| 3909 | |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 3910 | /* Iterate v argcnt times and store the results on the stack (via decreasing |
| 3911 | sp). Return 1 for success, 0 if error. */ |
| 3912 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 3913 | static int |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 3914 | unpack_iterable(PyObject *v, int argcnt, PyObject **sp) |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 3915 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3916 | int i = 0; |
| 3917 | PyObject *it; /* iter(v) */ |
| 3918 | PyObject *w; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3919 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3920 | assert(v != NULL); |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 3921 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3922 | it = PyObject_GetIter(v); |
| 3923 | if (it == NULL) |
| 3924 | goto Error; |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 3925 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3926 | for (; i < argcnt; i++) { |
| 3927 | w = PyIter_Next(it); |
| 3928 | if (w == NULL) { |
| 3929 | /* Iterator done, via error or exhaustion. */ |
| 3930 | if (!PyErr_Occurred()) { |
| 3931 | PyErr_Format(PyExc_ValueError, |
| 3932 | "need more than %d value%s to unpack", |
| 3933 | i, i == 1 ? "" : "s"); |
| 3934 | } |
| 3935 | goto Error; |
| 3936 | } |
| 3937 | *--sp = w; |
| 3938 | } |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 3939 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3940 | /* We better have exhausted the iterator now. */ |
| 3941 | w = PyIter_Next(it); |
| 3942 | if (w == NULL) { |
| 3943 | if (PyErr_Occurred()) |
| 3944 | goto Error; |
| 3945 | Py_DECREF(it); |
| 3946 | return 1; |
| 3947 | } |
| 3948 | Py_DECREF(w); |
| 3949 | PyErr_SetString(PyExc_ValueError, "too many values to unpack"); |
| 3950 | /* fall through */ |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 3951 | Error: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3952 | for (; i > 0; i--, sp++) |
| 3953 | Py_DECREF(*sp); |
| 3954 | Py_XDECREF(it); |
| 3955 | return 0; |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 3956 | } |
| 3957 | |
| 3958 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 3959 | #ifdef LLTRACE |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 3960 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3961 | prtrace(PyObject *v, char *str) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3962 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3963 | printf("%s ", str); |
| 3964 | if (PyObject_Print(v, stdout, 0) != 0) |
| 3965 | PyErr_Clear(); /* Don't know what else to do */ |
| 3966 | printf("\n"); |
| 3967 | return 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3968 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3969 | #endif |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3970 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 3971 | static void |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3972 | call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f) |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 3973 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3974 | PyObject *type, *value, *traceback, *arg; |
| 3975 | int err; |
| 3976 | PyErr_Fetch(&type, &value, &traceback); |
| 3977 | if (value == NULL) { |
| 3978 | value = Py_None; |
| 3979 | Py_INCREF(value); |
| 3980 | } |
| 3981 | arg = PyTuple_Pack(3, type, value, traceback); |
| 3982 | if (arg == NULL) { |
| 3983 | PyErr_Restore(type, value, traceback); |
| 3984 | return; |
| 3985 | } |
| 3986 | err = call_trace(func, self, f, PyTrace_EXCEPTION, arg); |
| 3987 | Py_DECREF(arg); |
| 3988 | if (err == 0) |
| 3989 | PyErr_Restore(type, value, traceback); |
| 3990 | else { |
| 3991 | Py_XDECREF(type); |
| 3992 | Py_XDECREF(value); |
| 3993 | Py_XDECREF(traceback); |
| 3994 | } |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 3995 | } |
| 3996 | |
Amaury Forgeot d'Arc | 0d75f09 | 2007-11-13 21:54:28 +0000 | [diff] [blame] | 3997 | static int |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 3998 | call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3999 | int what, PyObject *arg) |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 4000 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4001 | PyObject *type, *value, *traceback; |
| 4002 | int err; |
| 4003 | PyErr_Fetch(&type, &value, &traceback); |
| 4004 | err = call_trace(func, obj, frame, what, arg); |
| 4005 | if (err == 0) |
| 4006 | { |
| 4007 | PyErr_Restore(type, value, traceback); |
| 4008 | return 0; |
| 4009 | } |
| 4010 | else { |
| 4011 | Py_XDECREF(type); |
| 4012 | Py_XDECREF(value); |
| 4013 | Py_XDECREF(traceback); |
| 4014 | return -1; |
| 4015 | } |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 4016 | } |
| 4017 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4018 | static int |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 4019 | call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4020 | int what, PyObject *arg) |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 4021 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4022 | register PyThreadState *tstate = frame->f_tstate; |
| 4023 | int result; |
| 4024 | if (tstate->tracing) |
| 4025 | return 0; |
| 4026 | tstate->tracing++; |
| 4027 | tstate->use_tracing = 0; |
| 4028 | result = func(obj, frame, what, arg); |
| 4029 | tstate->use_tracing = ((tstate->c_tracefunc != NULL) |
| 4030 | || (tstate->c_profilefunc != NULL)); |
| 4031 | tstate->tracing--; |
| 4032 | return result; |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 4033 | } |
| 4034 | |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 4035 | PyObject * |
| 4036 | _PyEval_CallTracing(PyObject *func, PyObject *args) |
| 4037 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4038 | PyFrameObject *frame = PyEval_GetFrame(); |
| 4039 | PyThreadState *tstate = frame->f_tstate; |
| 4040 | int save_tracing = tstate->tracing; |
| 4041 | int save_use_tracing = tstate->use_tracing; |
| 4042 | PyObject *result; |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 4043 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4044 | tstate->tracing = 0; |
| 4045 | tstate->use_tracing = ((tstate->c_tracefunc != NULL) |
| 4046 | || (tstate->c_profilefunc != NULL)); |
| 4047 | result = PyObject_Call(func, args, NULL); |
| 4048 | tstate->tracing = save_tracing; |
| 4049 | tstate->use_tracing = save_use_tracing; |
| 4050 | return result; |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 4051 | } |
| 4052 | |
Jeffrey Yasskin | 655d835 | 2009-05-23 23:23:01 +0000 | [diff] [blame] | 4053 | /* See Objects/lnotab_notes.txt for a description of how tracing works. */ |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4054 | static int |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 4055 | maybe_call_line_trace(Py_tracefunc func, PyObject *obj, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4056 | PyFrameObject *frame, int *instr_lb, int *instr_ub, |
| 4057 | int *instr_prev) |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 4058 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4059 | int result = 0; |
| 4060 | int line = frame->f_lineno; |
Michael W. Hudson | 006c752 | 2002-11-08 13:08:46 +0000 | [diff] [blame] | 4061 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4062 | /* If the last instruction executed isn't in the current |
| 4063 | instruction window, reset the window. |
| 4064 | */ |
| 4065 | if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) { |
| 4066 | PyAddrPair bounds; |
| 4067 | line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti, |
| 4068 | &bounds); |
| 4069 | *instr_lb = bounds.ap_lower; |
| 4070 | *instr_ub = bounds.ap_upper; |
| 4071 | } |
| 4072 | /* If the last instruction falls at the start of a line or if |
| 4073 | it represents a jump backwards, update the frame's line |
| 4074 | number and call the trace function. */ |
| 4075 | if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) { |
| 4076 | frame->f_lineno = line; |
| 4077 | result = call_trace(func, obj, frame, PyTrace_LINE, Py_None); |
| 4078 | } |
| 4079 | *instr_prev = frame->f_lasti; |
| 4080 | return result; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 4081 | } |
| 4082 | |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 4083 | void |
| 4084 | PyEval_SetProfile(Py_tracefunc func, PyObject *arg) |
Fred Drake | d083839 | 2001-06-16 21:02:31 +0000 | [diff] [blame] | 4085 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4086 | PyThreadState *tstate = PyThreadState_GET(); |
| 4087 | PyObject *temp = tstate->c_profileobj; |
| 4088 | Py_XINCREF(arg); |
| 4089 | tstate->c_profilefunc = NULL; |
| 4090 | tstate->c_profileobj = NULL; |
| 4091 | /* Must make sure that tracing is not ignored if 'temp' is freed */ |
| 4092 | tstate->use_tracing = tstate->c_tracefunc != NULL; |
| 4093 | Py_XDECREF(temp); |
| 4094 | tstate->c_profilefunc = func; |
| 4095 | tstate->c_profileobj = arg; |
| 4096 | /* Flag that tracing or profiling is turned on */ |
| 4097 | tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL); |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 4098 | } |
| 4099 | |
| 4100 | void |
| 4101 | PyEval_SetTrace(Py_tracefunc func, PyObject *arg) |
| 4102 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4103 | PyThreadState *tstate = PyThreadState_GET(); |
| 4104 | PyObject *temp = tstate->c_traceobj; |
| 4105 | _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL); |
| 4106 | Py_XINCREF(arg); |
| 4107 | tstate->c_tracefunc = NULL; |
| 4108 | tstate->c_traceobj = NULL; |
| 4109 | /* Must make sure that profiling is not ignored if 'temp' is freed */ |
| 4110 | tstate->use_tracing = tstate->c_profilefunc != NULL; |
| 4111 | Py_XDECREF(temp); |
| 4112 | tstate->c_tracefunc = func; |
| 4113 | tstate->c_traceobj = arg; |
| 4114 | /* Flag that tracing or profiling is turned on */ |
| 4115 | tstate->use_tracing = ((func != NULL) |
| 4116 | || (tstate->c_profilefunc != NULL)); |
Fred Drake | d083839 | 2001-06-16 21:02:31 +0000 | [diff] [blame] | 4117 | } |
| 4118 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4119 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4120 | PyEval_GetBuiltins(void) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 4121 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4122 | PyFrameObject *current_frame = PyEval_GetFrame(); |
| 4123 | if (current_frame == NULL) |
| 4124 | return PyThreadState_GET()->interp->builtins; |
| 4125 | else |
| 4126 | return current_frame->f_builtins; |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 4127 | } |
| 4128 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4129 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4130 | PyEval_GetLocals(void) |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 4131 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4132 | PyFrameObject *current_frame = PyEval_GetFrame(); |
| 4133 | if (current_frame == NULL) |
| 4134 | return NULL; |
| 4135 | PyFrame_FastToLocals(current_frame); |
| 4136 | return current_frame->f_locals; |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 4137 | } |
| 4138 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4139 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4140 | PyEval_GetGlobals(void) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4141 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4142 | PyFrameObject *current_frame = PyEval_GetFrame(); |
| 4143 | if (current_frame == NULL) |
| 4144 | return NULL; |
| 4145 | else |
| 4146 | return current_frame->f_globals; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4147 | } |
| 4148 | |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 4149 | PyFrameObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4150 | PyEval_GetFrame(void) |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 4151 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4152 | PyThreadState *tstate = PyThreadState_GET(); |
| 4153 | return _PyThreadState_GetFrame(tstate); |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 4154 | } |
| 4155 | |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 4156 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4157 | PyEval_GetRestricted(void) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 4158 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4159 | PyFrameObject *current_frame = PyEval_GetFrame(); |
| 4160 | return current_frame == NULL ? 0 : PyFrame_IsRestricted(current_frame); |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 4161 | } |
| 4162 | |
Guido van Rossum | be27026 | 1997-05-22 22:26:18 +0000 | [diff] [blame] | 4163 | int |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 4164 | PyEval_MergeCompilerFlags(PyCompilerFlags *cf) |
Jeremy Hylton | 061d106 | 2001-03-22 02:32:48 +0000 | [diff] [blame] | 4165 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4166 | PyFrameObject *current_frame = PyEval_GetFrame(); |
| 4167 | int result = cf->cf_flags != 0; |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 4168 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4169 | if (current_frame != NULL) { |
| 4170 | const int codeflags = current_frame->f_code->co_flags; |
| 4171 | const int compilerflags = codeflags & PyCF_MASK; |
| 4172 | if (compilerflags) { |
| 4173 | result = 1; |
| 4174 | cf->cf_flags |= compilerflags; |
| 4175 | } |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 4176 | #if 0 /* future keyword */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4177 | if (codeflags & CO_GENERATOR_ALLOWED) { |
| 4178 | result = 1; |
| 4179 | cf->cf_flags |= CO_GENERATOR_ALLOWED; |
| 4180 | } |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 4181 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4182 | } |
| 4183 | return result; |
Jeremy Hylton | 061d106 | 2001-03-22 02:32:48 +0000 | [diff] [blame] | 4184 | } |
| 4185 | |
| 4186 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4187 | Py_FlushLine(void) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4188 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4189 | PyObject *f = PySys_GetObject("stdout"); |
| 4190 | if (f == NULL) |
| 4191 | return 0; |
| 4192 | if (!PyFile_SoftSpace(f, 0)) |
| 4193 | return 0; |
| 4194 | return PyFile_WriteString("\n", f); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4195 | } |
| 4196 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4197 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4198 | /* External interface to call any callable object. |
Antoine Pitrou | 76c8649 | 2010-04-01 16:42:11 +0000 | [diff] [blame] | 4199 | 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] | 4200 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4201 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4202 | PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4203 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4204 | PyObject *result; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4205 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4206 | if (arg == NULL) { |
| 4207 | arg = PyTuple_New(0); |
| 4208 | if (arg == NULL) |
| 4209 | return NULL; |
| 4210 | } |
| 4211 | else if (!PyTuple_Check(arg)) { |
| 4212 | PyErr_SetString(PyExc_TypeError, |
| 4213 | "argument list must be a tuple"); |
| 4214 | return NULL; |
| 4215 | } |
| 4216 | else |
| 4217 | Py_INCREF(arg); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4218 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4219 | if (kw != NULL && !PyDict_Check(kw)) { |
| 4220 | PyErr_SetString(PyExc_TypeError, |
| 4221 | "keyword list must be a dictionary"); |
| 4222 | Py_DECREF(arg); |
| 4223 | return NULL; |
| 4224 | } |
Guido van Rossum | e3e61c1 | 1995-08-04 04:14:47 +0000 | [diff] [blame] | 4225 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4226 | result = PyObject_Call(func, arg, kw); |
| 4227 | Py_DECREF(arg); |
| 4228 | return result; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4229 | } |
| 4230 | |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 4231 | const char * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4232 | PyEval_GetFuncName(PyObject *func) |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4233 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4234 | if (PyMethod_Check(func)) |
| 4235 | return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func)); |
| 4236 | else if (PyFunction_Check(func)) |
| 4237 | return PyString_AsString(((PyFunctionObject*)func)->func_name); |
| 4238 | else if (PyCFunction_Check(func)) |
| 4239 | return ((PyCFunctionObject*)func)->m_ml->ml_name; |
| 4240 | else if (PyClass_Check(func)) |
| 4241 | return PyString_AsString(((PyClassObject*)func)->cl_name); |
| 4242 | else if (PyInstance_Check(func)) { |
| 4243 | return PyString_AsString( |
| 4244 | ((PyInstanceObject*)func)->in_class->cl_name); |
| 4245 | } else { |
| 4246 | return func->ob_type->tp_name; |
| 4247 | } |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4248 | } |
| 4249 | |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 4250 | const char * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4251 | PyEval_GetFuncDesc(PyObject *func) |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4252 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4253 | if (PyMethod_Check(func)) |
| 4254 | return "()"; |
| 4255 | else if (PyFunction_Check(func)) |
| 4256 | return "()"; |
| 4257 | else if (PyCFunction_Check(func)) |
| 4258 | return "()"; |
| 4259 | else if (PyClass_Check(func)) |
| 4260 | return " constructor"; |
| 4261 | else if (PyInstance_Check(func)) { |
| 4262 | return " instance"; |
| 4263 | } else { |
| 4264 | return " object"; |
| 4265 | } |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4266 | } |
| 4267 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4268 | static void |
Jeremy Hylton | 192690e | 2002-08-16 18:36:11 +0000 | [diff] [blame] | 4269 | err_args(PyObject *func, int flags, int nargs) |
| 4270 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4271 | if (flags & METH_NOARGS) |
| 4272 | PyErr_Format(PyExc_TypeError, |
| 4273 | "%.200s() takes no arguments (%d given)", |
| 4274 | ((PyCFunctionObject *)func)->m_ml->ml_name, |
| 4275 | nargs); |
| 4276 | else |
| 4277 | PyErr_Format(PyExc_TypeError, |
| 4278 | "%.200s() takes exactly one argument (%d given)", |
| 4279 | ((PyCFunctionObject *)func)->m_ml->ml_name, |
| 4280 | nargs); |
Jeremy Hylton | 192690e | 2002-08-16 18:36:11 +0000 | [diff] [blame] | 4281 | } |
| 4282 | |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 4283 | #define C_TRACE(x, call) \ |
Nicholas Bastin | d858a77 | 2004-06-25 23:31:06 +0000 | [diff] [blame] | 4284 | if (tstate->use_tracing && tstate->c_profilefunc) { \ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4285 | if (call_trace(tstate->c_profilefunc, \ |
| 4286 | tstate->c_profileobj, \ |
| 4287 | tstate->frame, PyTrace_C_CALL, \ |
| 4288 | func)) { \ |
| 4289 | x = NULL; \ |
| 4290 | } \ |
| 4291 | else { \ |
| 4292 | x = call; \ |
| 4293 | if (tstate->c_profilefunc != NULL) { \ |
| 4294 | if (x == NULL) { \ |
| 4295 | call_trace_protected(tstate->c_profilefunc, \ |
| 4296 | tstate->c_profileobj, \ |
| 4297 | tstate->frame, PyTrace_C_EXCEPTION, \ |
| 4298 | func); \ |
| 4299 | /* XXX should pass (type, value, tb) */ \ |
| 4300 | } else { \ |
| 4301 | if (call_trace(tstate->c_profilefunc, \ |
| 4302 | tstate->c_profileobj, \ |
| 4303 | tstate->frame, PyTrace_C_RETURN, \ |
| 4304 | func)) { \ |
| 4305 | Py_DECREF(x); \ |
| 4306 | x = NULL; \ |
| 4307 | } \ |
| 4308 | } \ |
| 4309 | } \ |
| 4310 | } \ |
Nicholas Bastin | d858a77 | 2004-06-25 23:31:06 +0000 | [diff] [blame] | 4311 | } else { \ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4312 | x = call; \ |
| 4313 | } |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 4314 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4315 | static PyObject * |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 4316 | call_function(PyObject ***pp_stack, int oparg |
| 4317 | #ifdef WITH_TSC |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4318 | , uint64* pintr0, uint64* pintr1 |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 4319 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4320 | ) |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 4321 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4322 | int na = oparg & 0xff; |
| 4323 | int nk = (oparg>>8) & 0xff; |
| 4324 | int n = na + 2 * nk; |
| 4325 | PyObject **pfunc = (*pp_stack) - n - 1; |
| 4326 | PyObject *func = *pfunc; |
| 4327 | PyObject *x, *w; |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 4328 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4329 | /* Always dispatch PyCFunction first, because these are |
| 4330 | presumed to be the most frequent callable object. |
| 4331 | */ |
| 4332 | if (PyCFunction_Check(func) && nk == 0) { |
| 4333 | int flags = PyCFunction_GET_FLAGS(func); |
| 4334 | PyThreadState *tstate = PyThreadState_GET(); |
Raymond Hettinger | a7f56bc | 2004-06-26 04:34:33 +0000 | [diff] [blame] | 4335 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4336 | PCALL(PCALL_CFUNCTION); |
| 4337 | if (flags & (METH_NOARGS | METH_O)) { |
| 4338 | PyCFunction meth = PyCFunction_GET_FUNCTION(func); |
| 4339 | PyObject *self = PyCFunction_GET_SELF(func); |
| 4340 | if (flags & METH_NOARGS && na == 0) { |
| 4341 | C_TRACE(x, (*meth)(self,NULL)); |
| 4342 | } |
| 4343 | else if (flags & METH_O && na == 1) { |
| 4344 | PyObject *arg = EXT_POP(*pp_stack); |
| 4345 | C_TRACE(x, (*meth)(self,arg)); |
| 4346 | Py_DECREF(arg); |
| 4347 | } |
| 4348 | else { |
| 4349 | err_args(func, flags, na); |
| 4350 | x = NULL; |
| 4351 | } |
| 4352 | } |
| 4353 | else { |
| 4354 | PyObject *callargs; |
| 4355 | callargs = load_args(pp_stack, na); |
| 4356 | READ_TIMESTAMP(*pintr0); |
| 4357 | C_TRACE(x, PyCFunction_Call(func,callargs,NULL)); |
| 4358 | READ_TIMESTAMP(*pintr1); |
| 4359 | Py_XDECREF(callargs); |
| 4360 | } |
| 4361 | } else { |
| 4362 | if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) { |
| 4363 | /* optimize access to bound methods */ |
| 4364 | PyObject *self = PyMethod_GET_SELF(func); |
| 4365 | PCALL(PCALL_METHOD); |
| 4366 | PCALL(PCALL_BOUND_METHOD); |
| 4367 | Py_INCREF(self); |
| 4368 | func = PyMethod_GET_FUNCTION(func); |
| 4369 | Py_INCREF(func); |
Serhiy Storchaka | 763a61c | 2016-04-10 18:05:12 +0300 | [diff] [blame] | 4370 | Py_SETREF(*pfunc, self); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4371 | na++; |
| 4372 | n++; |
| 4373 | } else |
| 4374 | Py_INCREF(func); |
| 4375 | READ_TIMESTAMP(*pintr0); |
| 4376 | if (PyFunction_Check(func)) |
| 4377 | x = fast_function(func, pp_stack, n, na, nk); |
| 4378 | else |
| 4379 | x = do_call(func, pp_stack, na, nk); |
| 4380 | READ_TIMESTAMP(*pintr1); |
| 4381 | Py_DECREF(func); |
| 4382 | } |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 4383 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4384 | /* Clear the stack of the function object. Also removes |
| 4385 | the arguments in case they weren't consumed already |
| 4386 | (fast_function() and err_args() leave them on the stack). |
| 4387 | */ |
| 4388 | while ((*pp_stack) > pfunc) { |
| 4389 | w = EXT_POP(*pp_stack); |
| 4390 | Py_DECREF(w); |
| 4391 | PCALL(PCALL_POP); |
| 4392 | } |
| 4393 | return x; |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 4394 | } |
| 4395 | |
Jeremy Hylton | 192690e | 2002-08-16 18:36:11 +0000 | [diff] [blame] | 4396 | /* The fast_function() function optimize calls for which no argument |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4397 | tuple is necessary; the objects are passed directly from the stack. |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4398 | For the simplest case -- a function that takes only positional |
| 4399 | arguments and is called with only positional arguments -- it |
| 4400 | inlines the most primitive frame setup code from |
| 4401 | PyEval_EvalCodeEx(), which vastly reduces the checks that must be |
| 4402 | done before evaluating the frame. |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4403 | */ |
| 4404 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4405 | static PyObject * |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4406 | 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] | 4407 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4408 | PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func); |
| 4409 | PyObject *globals = PyFunction_GET_GLOBALS(func); |
| 4410 | PyObject *argdefs = PyFunction_GET_DEFAULTS(func); |
| 4411 | PyObject **d = NULL; |
| 4412 | int nd = 0; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4413 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4414 | PCALL(PCALL_FUNCTION); |
| 4415 | PCALL(PCALL_FAST_FUNCTION); |
| 4416 | if (argdefs == NULL && co->co_argcount == n && nk==0 && |
| 4417 | co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) { |
| 4418 | PyFrameObject *f; |
| 4419 | PyObject *retval = NULL; |
| 4420 | PyThreadState *tstate = PyThreadState_GET(); |
| 4421 | PyObject **fastlocals, **stack; |
| 4422 | int i; |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4423 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4424 | PCALL(PCALL_FASTER_FUNCTION); |
| 4425 | assert(globals != NULL); |
| 4426 | /* XXX Perhaps we should create a specialized |
| 4427 | PyFrame_New() that doesn't take locals, but does |
| 4428 | take builtins without sanity checking them. |
| 4429 | */ |
| 4430 | assert(tstate != NULL); |
| 4431 | f = PyFrame_New(tstate, co, globals, NULL); |
| 4432 | if (f == NULL) |
| 4433 | return NULL; |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4434 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4435 | fastlocals = f->f_localsplus; |
| 4436 | stack = (*pp_stack) - n; |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4437 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4438 | for (i = 0; i < n; i++) { |
| 4439 | Py_INCREF(*stack); |
| 4440 | fastlocals[i] = *stack++; |
| 4441 | } |
| 4442 | retval = PyEval_EvalFrameEx(f,0); |
| 4443 | ++tstate->recursion_depth; |
| 4444 | Py_DECREF(f); |
| 4445 | --tstate->recursion_depth; |
| 4446 | return retval; |
| 4447 | } |
| 4448 | if (argdefs != NULL) { |
| 4449 | d = &PyTuple_GET_ITEM(argdefs, 0); |
| 4450 | nd = Py_SIZE(argdefs); |
| 4451 | } |
| 4452 | return PyEval_EvalCodeEx(co, globals, |
| 4453 | (PyObject *)NULL, (*pp_stack)-n, na, |
| 4454 | (*pp_stack)-2*nk, nk, d, nd, |
| 4455 | PyFunction_GET_CLOSURE(func)); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4456 | } |
| 4457 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4458 | static PyObject * |
Ka-Ping Yee | 2057970 | 2001-01-15 22:14:16 +0000 | [diff] [blame] | 4459 | update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack, |
| 4460 | PyObject *func) |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4461 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4462 | PyObject *kwdict = NULL; |
| 4463 | if (orig_kwdict == NULL) |
| 4464 | kwdict = PyDict_New(); |
| 4465 | else { |
| 4466 | kwdict = PyDict_Copy(orig_kwdict); |
| 4467 | Py_DECREF(orig_kwdict); |
| 4468 | } |
| 4469 | if (kwdict == NULL) |
| 4470 | return NULL; |
| 4471 | while (--nk >= 0) { |
| 4472 | int err; |
| 4473 | PyObject *value = EXT_POP(*pp_stack); |
| 4474 | PyObject *key = EXT_POP(*pp_stack); |
| 4475 | if (PyDict_GetItem(kwdict, key) != NULL) { |
| 4476 | PyErr_Format(PyExc_TypeError, |
| 4477 | "%.200s%s got multiple values " |
| 4478 | "for keyword argument '%.200s'", |
| 4479 | PyEval_GetFuncName(func), |
| 4480 | PyEval_GetFuncDesc(func), |
| 4481 | PyString_AsString(key)); |
| 4482 | Py_DECREF(key); |
| 4483 | Py_DECREF(value); |
| 4484 | Py_DECREF(kwdict); |
| 4485 | return NULL; |
| 4486 | } |
| 4487 | err = PyDict_SetItem(kwdict, key, value); |
| 4488 | Py_DECREF(key); |
| 4489 | Py_DECREF(value); |
| 4490 | if (err) { |
| 4491 | Py_DECREF(kwdict); |
| 4492 | return NULL; |
| 4493 | } |
| 4494 | } |
| 4495 | return kwdict; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4496 | } |
| 4497 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4498 | static PyObject * |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4499 | update_star_args(int nstack, int nstar, PyObject *stararg, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4500 | PyObject ***pp_stack) |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4501 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4502 | PyObject *callargs, *w; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4503 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4504 | callargs = PyTuple_New(nstack + nstar); |
| 4505 | if (callargs == NULL) { |
| 4506 | return NULL; |
| 4507 | } |
| 4508 | if (nstar) { |
| 4509 | int i; |
| 4510 | for (i = 0; i < nstar; i++) { |
| 4511 | PyObject *a = PyTuple_GET_ITEM(stararg, i); |
| 4512 | Py_INCREF(a); |
| 4513 | PyTuple_SET_ITEM(callargs, nstack + i, a); |
| 4514 | } |
| 4515 | } |
| 4516 | while (--nstack >= 0) { |
| 4517 | w = EXT_POP(*pp_stack); |
| 4518 | PyTuple_SET_ITEM(callargs, nstack, w); |
| 4519 | } |
| 4520 | return callargs; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4521 | } |
| 4522 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4523 | static PyObject * |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4524 | load_args(PyObject ***pp_stack, int na) |
| 4525 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4526 | PyObject *args = PyTuple_New(na); |
| 4527 | PyObject *w; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4528 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4529 | if (args == NULL) |
| 4530 | return NULL; |
| 4531 | while (--na >= 0) { |
| 4532 | w = EXT_POP(*pp_stack); |
| 4533 | PyTuple_SET_ITEM(args, na, w); |
| 4534 | } |
| 4535 | return args; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4536 | } |
| 4537 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4538 | static PyObject * |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4539 | do_call(PyObject *func, PyObject ***pp_stack, int na, int nk) |
| 4540 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4541 | PyObject *callargs = NULL; |
| 4542 | PyObject *kwdict = NULL; |
| 4543 | PyObject *result = NULL; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4544 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4545 | if (nk > 0) { |
| 4546 | kwdict = update_keyword_args(NULL, nk, pp_stack, func); |
| 4547 | if (kwdict == NULL) |
| 4548 | goto call_fail; |
| 4549 | } |
| 4550 | callargs = load_args(pp_stack, na); |
| 4551 | if (callargs == NULL) |
| 4552 | goto call_fail; |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4553 | #ifdef CALL_PROFILE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4554 | /* At this point, we have to look at the type of func to |
| 4555 | update the call stats properly. Do it here so as to avoid |
| 4556 | exposing the call stats machinery outside ceval.c |
| 4557 | */ |
| 4558 | if (PyFunction_Check(func)) |
| 4559 | PCALL(PCALL_FUNCTION); |
| 4560 | else if (PyMethod_Check(func)) |
| 4561 | PCALL(PCALL_METHOD); |
| 4562 | else if (PyType_Check(func)) |
| 4563 | PCALL(PCALL_TYPE); |
| 4564 | else if (PyCFunction_Check(func)) |
| 4565 | PCALL(PCALL_CFUNCTION); |
| 4566 | else |
| 4567 | PCALL(PCALL_OTHER); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4568 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4569 | if (PyCFunction_Check(func)) { |
| 4570 | PyThreadState *tstate = PyThreadState_GET(); |
| 4571 | C_TRACE(result, PyCFunction_Call(func, callargs, kwdict)); |
| 4572 | } |
| 4573 | else |
| 4574 | result = PyObject_Call(func, callargs, kwdict); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4575 | call_fail: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4576 | Py_XDECREF(callargs); |
| 4577 | Py_XDECREF(kwdict); |
| 4578 | return result; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4579 | } |
| 4580 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4581 | static PyObject * |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4582 | ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk) |
| 4583 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4584 | int nstar = 0; |
| 4585 | PyObject *callargs = NULL; |
| 4586 | PyObject *stararg = NULL; |
| 4587 | PyObject *kwdict = NULL; |
| 4588 | PyObject *result = NULL; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4589 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4590 | if (flags & CALL_FLAG_KW) { |
| 4591 | kwdict = EXT_POP(*pp_stack); |
| 4592 | if (!PyDict_Check(kwdict)) { |
| 4593 | PyObject *d; |
| 4594 | d = PyDict_New(); |
| 4595 | if (d == NULL) |
| 4596 | goto ext_call_fail; |
| 4597 | if (PyDict_Update(d, kwdict) != 0) { |
| 4598 | Py_DECREF(d); |
| 4599 | /* PyDict_Update raises attribute |
| 4600 | * error (percolated from an attempt |
| 4601 | * to get 'keys' attribute) instead of |
| 4602 | * a type error if its second argument |
| 4603 | * is not a mapping. |
| 4604 | */ |
| 4605 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 4606 | PyErr_Format(PyExc_TypeError, |
| 4607 | "%.200s%.200s argument after ** " |
| 4608 | "must be a mapping, not %.200s", |
| 4609 | PyEval_GetFuncName(func), |
| 4610 | PyEval_GetFuncDesc(func), |
| 4611 | kwdict->ob_type->tp_name); |
| 4612 | } |
| 4613 | goto ext_call_fail; |
| 4614 | } |
| 4615 | Py_DECREF(kwdict); |
| 4616 | kwdict = d; |
| 4617 | } |
| 4618 | } |
| 4619 | if (flags & CALL_FLAG_VAR) { |
| 4620 | stararg = EXT_POP(*pp_stack); |
| 4621 | if (!PyTuple_Check(stararg)) { |
| 4622 | PyObject *t = NULL; |
| 4623 | t = PySequence_Tuple(stararg); |
| 4624 | if (t == NULL) { |
Martin Panter | 0bb165e | 2016-01-31 06:30:56 +0000 | [diff] [blame] | 4625 | if (PyErr_ExceptionMatches(PyExc_TypeError) && |
| 4626 | /* Don't mask TypeError raised from a generator */ |
| 4627 | !PyGen_Check(stararg)) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4628 | PyErr_Format(PyExc_TypeError, |
| 4629 | "%.200s%.200s argument after * " |
Martin Panter | 0bb165e | 2016-01-31 06:30:56 +0000 | [diff] [blame] | 4630 | "must be an iterable, not %200s", |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4631 | PyEval_GetFuncName(func), |
| 4632 | PyEval_GetFuncDesc(func), |
| 4633 | stararg->ob_type->tp_name); |
| 4634 | } |
| 4635 | goto ext_call_fail; |
| 4636 | } |
| 4637 | Py_DECREF(stararg); |
| 4638 | stararg = t; |
| 4639 | } |
| 4640 | nstar = PyTuple_GET_SIZE(stararg); |
| 4641 | } |
| 4642 | if (nk > 0) { |
| 4643 | kwdict = update_keyword_args(kwdict, nk, pp_stack, func); |
| 4644 | if (kwdict == NULL) |
| 4645 | goto ext_call_fail; |
| 4646 | } |
| 4647 | callargs = update_star_args(na, nstar, stararg, pp_stack); |
| 4648 | if (callargs == NULL) |
| 4649 | goto ext_call_fail; |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4650 | #ifdef CALL_PROFILE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4651 | /* At this point, we have to look at the type of func to |
| 4652 | update the call stats properly. Do it here so as to avoid |
| 4653 | exposing the call stats machinery outside ceval.c |
| 4654 | */ |
| 4655 | if (PyFunction_Check(func)) |
| 4656 | PCALL(PCALL_FUNCTION); |
| 4657 | else if (PyMethod_Check(func)) |
| 4658 | PCALL(PCALL_METHOD); |
| 4659 | else if (PyType_Check(func)) |
| 4660 | PCALL(PCALL_TYPE); |
| 4661 | else if (PyCFunction_Check(func)) |
| 4662 | PCALL(PCALL_CFUNCTION); |
| 4663 | else |
| 4664 | PCALL(PCALL_OTHER); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4665 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4666 | if (PyCFunction_Check(func)) { |
| 4667 | PyThreadState *tstate = PyThreadState_GET(); |
| 4668 | C_TRACE(result, PyCFunction_Call(func, callargs, kwdict)); |
| 4669 | } |
| 4670 | else |
| 4671 | result = PyObject_Call(func, callargs, kwdict); |
Thomas Wouters | ae406c6 | 2007-09-19 17:27:43 +0000 | [diff] [blame] | 4672 | ext_call_fail: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4673 | Py_XDECREF(callargs); |
| 4674 | Py_XDECREF(kwdict); |
| 4675 | Py_XDECREF(stararg); |
| 4676 | return result; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4677 | } |
| 4678 | |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 4679 | /* Extract a slice index from a PyInt or PyLong or an object with the |
| 4680 | nb_index slot defined, and store in *pi. |
| 4681 | Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX, |
Xiang Zhang | 05469fa | 2017-05-10 19:20:28 +0800 | [diff] [blame^] | 4682 | and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN. |
Martin v. Löwis | dde99d2 | 2006-02-17 15:57:41 +0000 | [diff] [blame] | 4683 | Return 0 on error, 1 on success. |
Tim Peters | cb479e7 | 2001-12-16 19:11:44 +0000 | [diff] [blame] | 4684 | */ |
Tim Peters | b519638 | 2001-12-16 19:44:20 +0000 | [diff] [blame] | 4685 | /* Note: If v is NULL, return success without storing into *pi. This |
| 4686 | is because_PyEval_SliceIndex() is called by apply_slice(), which can be |
| 4687 | 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] | 4688 | */ |
Guido van Rossum | 20c6add | 2000-05-08 14:06:50 +0000 | [diff] [blame] | 4689 | int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4690 | _PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4691 | { |
Serhiy Storchaka | 079f21f | 2017-03-30 20:32:18 +0300 | [diff] [blame] | 4692 | if (v != NULL && v != Py_None) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4693 | Py_ssize_t x; |
| 4694 | if (PyInt_Check(v)) { |
| 4695 | /* XXX(nnorwitz): I think PyInt_AS_LONG is correct, |
| 4696 | however, it looks like it should be AsSsize_t. |
| 4697 | There should be a comment here explaining why. |
| 4698 | */ |
| 4699 | x = PyInt_AS_LONG(v); |
| 4700 | } |
| 4701 | else if (PyIndex_Check(v)) { |
| 4702 | x = PyNumber_AsSsize_t(v, NULL); |
| 4703 | if (x == -1 && PyErr_Occurred()) |
| 4704 | return 0; |
| 4705 | } |
| 4706 | else { |
| 4707 | PyErr_SetString(PyExc_TypeError, |
| 4708 | "slice indices must be integers or " |
| 4709 | "None or have an __index__ method"); |
| 4710 | return 0; |
| 4711 | } |
| 4712 | *pi = x; |
| 4713 | } |
| 4714 | return 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4715 | } |
| 4716 | |
Serhiy Storchaka | 079f21f | 2017-03-30 20:32:18 +0300 | [diff] [blame] | 4717 | int |
| 4718 | _PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi) |
| 4719 | { |
| 4720 | Py_ssize_t x; |
| 4721 | if (PyIndex_Check(v)) { |
| 4722 | x = PyNumber_AsSsize_t(v, NULL); |
| 4723 | if (x == -1 && PyErr_Occurred()) |
| 4724 | return 0; |
| 4725 | } |
| 4726 | else { |
| 4727 | PyErr_SetString(PyExc_TypeError, |
| 4728 | "slice indices must be integers or " |
| 4729 | "have an __index__ method"); |
| 4730 | return 0; |
| 4731 | } |
| 4732 | *pi = x; |
| 4733 | return 1; |
| 4734 | } |
| 4735 | |
| 4736 | |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 4737 | #undef ISINDEX |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 4738 | #define ISINDEX(x) ((x) == NULL || \ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4739 | PyInt_Check(x) || PyLong_Check(x) || PyIndex_Check(x)) |
Guido van Rossum | 50d756e | 2001-08-18 17:43:36 +0000 | [diff] [blame] | 4740 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4741 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4742 | 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] | 4743 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4744 | PyTypeObject *tp = u->ob_type; |
| 4745 | PySequenceMethods *sq = tp->tp_as_sequence; |
Guido van Rossum | 50d756e | 2001-08-18 17:43:36 +0000 | [diff] [blame] | 4746 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4747 | if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) { |
| 4748 | Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX; |
| 4749 | if (!_PyEval_SliceIndex(v, &ilow)) |
| 4750 | return NULL; |
| 4751 | if (!_PyEval_SliceIndex(w, &ihigh)) |
| 4752 | return NULL; |
| 4753 | return PySequence_GetSlice(u, ilow, ihigh); |
| 4754 | } |
| 4755 | else { |
| 4756 | PyObject *slice = PySlice_New(v, w, NULL); |
| 4757 | if (slice != NULL) { |
| 4758 | PyObject *res = PyObject_GetItem(u, slice); |
| 4759 | Py_DECREF(slice); |
| 4760 | return res; |
| 4761 | } |
| 4762 | else |
| 4763 | return NULL; |
| 4764 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4765 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4766 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4767 | static int |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4768 | assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4769 | /* u[v:w] = x */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4770 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4771 | PyTypeObject *tp = u->ob_type; |
| 4772 | PySequenceMethods *sq = tp->tp_as_sequence; |
Guido van Rossum | 50d756e | 2001-08-18 17:43:36 +0000 | [diff] [blame] | 4773 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4774 | if (sq && sq->sq_ass_slice && ISINDEX(v) && ISINDEX(w)) { |
| 4775 | Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX; |
| 4776 | if (!_PyEval_SliceIndex(v, &ilow)) |
| 4777 | return -1; |
| 4778 | if (!_PyEval_SliceIndex(w, &ihigh)) |
| 4779 | return -1; |
| 4780 | if (x == NULL) |
| 4781 | return PySequence_DelSlice(u, ilow, ihigh); |
| 4782 | else |
| 4783 | return PySequence_SetSlice(u, ilow, ihigh, x); |
| 4784 | } |
| 4785 | else { |
| 4786 | PyObject *slice = PySlice_New(v, w, NULL); |
| 4787 | if (slice != NULL) { |
| 4788 | int res; |
| 4789 | if (x != NULL) |
| 4790 | res = PyObject_SetItem(u, slice, x); |
| 4791 | else |
| 4792 | res = PyObject_DelItem(u, slice); |
| 4793 | Py_DECREF(slice); |
| 4794 | return res; |
| 4795 | } |
| 4796 | else |
| 4797 | return -1; |
| 4798 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4799 | } |
| 4800 | |
Guido van Rossum | 04edb52 | 2008-03-18 02:49:46 +0000 | [diff] [blame] | 4801 | #define Py3kExceptionClass_Check(x) \ |
| 4802 | (PyType_Check((x)) && \ |
| 4803 | PyType_FastSubclass((PyTypeObject*)(x), Py_TPFLAGS_BASE_EXC_SUBCLASS)) |
| 4804 | |
| 4805 | #define CANNOT_CATCH_MSG "catching classes that don't inherit from " \ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4806 | "BaseException is not allowed in 3.x" |
Guido van Rossum | 04edb52 | 2008-03-18 02:49:46 +0000 | [diff] [blame] | 4807 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4808 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4809 | cmp_outcome(int op, register PyObject *v, register PyObject *w) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4810 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4811 | int res = 0; |
| 4812 | switch (op) { |
| 4813 | case PyCmp_IS: |
| 4814 | res = (v == w); |
| 4815 | break; |
| 4816 | case PyCmp_IS_NOT: |
| 4817 | res = (v != w); |
| 4818 | break; |
| 4819 | case PyCmp_IN: |
| 4820 | res = PySequence_Contains(w, v); |
| 4821 | if (res < 0) |
| 4822 | return NULL; |
| 4823 | break; |
| 4824 | case PyCmp_NOT_IN: |
| 4825 | res = PySequence_Contains(w, v); |
| 4826 | if (res < 0) |
| 4827 | return NULL; |
| 4828 | res = !res; |
| 4829 | break; |
| 4830 | case PyCmp_EXC_MATCH: |
| 4831 | if (PyTuple_Check(w)) { |
| 4832 | Py_ssize_t i, length; |
| 4833 | length = PyTuple_Size(w); |
| 4834 | for (i = 0; i < length; i += 1) { |
| 4835 | PyObject *exc = PyTuple_GET_ITEM(w, i); |
| 4836 | if (PyString_Check(exc)) { |
| 4837 | int ret_val; |
| 4838 | ret_val = PyErr_WarnEx( |
| 4839 | PyExc_DeprecationWarning, |
| 4840 | "catching of string " |
| 4841 | "exceptions is deprecated", 1); |
| 4842 | if (ret_val < 0) |
| 4843 | return NULL; |
| 4844 | } |
| 4845 | else if (Py_Py3kWarningFlag && |
| 4846 | !PyTuple_Check(exc) && |
| 4847 | !Py3kExceptionClass_Check(exc)) |
| 4848 | { |
| 4849 | int ret_val; |
| 4850 | ret_val = PyErr_WarnEx( |
| 4851 | PyExc_DeprecationWarning, |
| 4852 | CANNOT_CATCH_MSG, 1); |
| 4853 | if (ret_val < 0) |
| 4854 | return NULL; |
| 4855 | } |
| 4856 | } |
| 4857 | } |
| 4858 | else { |
| 4859 | if (PyString_Check(w)) { |
| 4860 | int ret_val; |
| 4861 | ret_val = PyErr_WarnEx( |
| 4862 | PyExc_DeprecationWarning, |
| 4863 | "catching of string " |
| 4864 | "exceptions is deprecated", 1); |
| 4865 | if (ret_val < 0) |
| 4866 | return NULL; |
| 4867 | } |
| 4868 | else if (Py_Py3kWarningFlag && |
| 4869 | !PyTuple_Check(w) && |
| 4870 | !Py3kExceptionClass_Check(w)) |
| 4871 | { |
| 4872 | int ret_val; |
| 4873 | ret_val = PyErr_WarnEx( |
| 4874 | PyExc_DeprecationWarning, |
| 4875 | CANNOT_CATCH_MSG, 1); |
| 4876 | if (ret_val < 0) |
| 4877 | return NULL; |
| 4878 | } |
| 4879 | } |
| 4880 | res = PyErr_GivenExceptionMatches(v, w); |
| 4881 | break; |
| 4882 | default: |
| 4883 | return PyObject_RichCompare(v, w, op); |
| 4884 | } |
| 4885 | v = res ? Py_True : Py_False; |
| 4886 | Py_INCREF(v); |
| 4887 | return v; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4888 | } |
| 4889 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4890 | static PyObject * |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 4891 | import_from(PyObject *v, PyObject *name) |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 4892 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4893 | PyObject *x; |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 4894 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4895 | x = PyObject_GetAttr(v, name); |
| 4896 | if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 4897 | PyErr_Format(PyExc_ImportError, |
| 4898 | "cannot import name %.230s", |
| 4899 | PyString_AsString(name)); |
| 4900 | } |
| 4901 | return x; |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 4902 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4903 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4904 | static int |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 4905 | import_all_from(PyObject *locals, PyObject *v) |
| 4906 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4907 | PyObject *all = PyObject_GetAttrString(v, "__all__"); |
| 4908 | PyObject *dict, *name, *value; |
| 4909 | int skip_leading_underscores = 0; |
| 4910 | int pos, err; |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 4911 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4912 | if (all == NULL) { |
| 4913 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 4914 | return -1; /* Unexpected error */ |
| 4915 | PyErr_Clear(); |
| 4916 | dict = PyObject_GetAttrString(v, "__dict__"); |
| 4917 | if (dict == NULL) { |
| 4918 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 4919 | return -1; |
| 4920 | PyErr_SetString(PyExc_ImportError, |
| 4921 | "from-import-* object has no __dict__ and no __all__"); |
| 4922 | return -1; |
| 4923 | } |
| 4924 | all = PyMapping_Keys(dict); |
| 4925 | Py_DECREF(dict); |
| 4926 | if (all == NULL) |
| 4927 | return -1; |
| 4928 | skip_leading_underscores = 1; |
| 4929 | } |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 4930 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4931 | for (pos = 0, err = 0; ; pos++) { |
| 4932 | name = PySequence_GetItem(all, pos); |
| 4933 | if (name == NULL) { |
| 4934 | if (!PyErr_ExceptionMatches(PyExc_IndexError)) |
| 4935 | err = -1; |
| 4936 | else |
| 4937 | PyErr_Clear(); |
| 4938 | break; |
| 4939 | } |
| 4940 | if (skip_leading_underscores && |
| 4941 | PyString_Check(name) && |
| 4942 | PyString_AS_STRING(name)[0] == '_') |
| 4943 | { |
| 4944 | Py_DECREF(name); |
| 4945 | continue; |
| 4946 | } |
| 4947 | value = PyObject_GetAttr(v, name); |
| 4948 | if (value == NULL) |
| 4949 | err = -1; |
| 4950 | else if (PyDict_CheckExact(locals)) |
| 4951 | err = PyDict_SetItem(locals, name, value); |
| 4952 | else |
| 4953 | err = PyObject_SetItem(locals, name, value); |
| 4954 | Py_DECREF(name); |
| 4955 | Py_XDECREF(value); |
| 4956 | if (err != 0) |
| 4957 | break; |
| 4958 | } |
| 4959 | Py_DECREF(all); |
| 4960 | return err; |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 4961 | } |
| 4962 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4963 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4964 | build_class(PyObject *methods, PyObject *bases, PyObject *name) |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 4965 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4966 | PyObject *metaclass = NULL, *result, *base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4967 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4968 | if (PyDict_Check(methods)) |
| 4969 | metaclass = PyDict_GetItemString(methods, "__metaclass__"); |
| 4970 | if (metaclass != NULL) |
| 4971 | Py_INCREF(metaclass); |
| 4972 | else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) { |
| 4973 | base = PyTuple_GET_ITEM(bases, 0); |
| 4974 | metaclass = PyObject_GetAttrString(base, "__class__"); |
| 4975 | if (metaclass == NULL) { |
| 4976 | PyErr_Clear(); |
| 4977 | metaclass = (PyObject *)base->ob_type; |
| 4978 | Py_INCREF(metaclass); |
| 4979 | } |
| 4980 | } |
| 4981 | else { |
| 4982 | PyObject *g = PyEval_GetGlobals(); |
| 4983 | if (g != NULL && PyDict_Check(g)) |
| 4984 | metaclass = PyDict_GetItemString(g, "__metaclass__"); |
| 4985 | if (metaclass == NULL) |
| 4986 | metaclass = (PyObject *) &PyClass_Type; |
| 4987 | Py_INCREF(metaclass); |
| 4988 | } |
| 4989 | result = PyObject_CallFunctionObjArgs(metaclass, name, bases, methods, |
| 4990 | NULL); |
| 4991 | Py_DECREF(metaclass); |
| 4992 | if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 4993 | /* A type error here likely means that the user passed |
| 4994 | in a base that was not a class (such the random module |
| 4995 | instead of the random.random type). Help them out with |
| 4996 | by augmenting the error message with more information.*/ |
Raymond Hettinger | cfc3192 | 2004-09-16 16:41:57 +0000 | [diff] [blame] | 4997 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4998 | PyObject *ptype, *pvalue, *ptraceback; |
Raymond Hettinger | cfc3192 | 2004-09-16 16:41:57 +0000 | [diff] [blame] | 4999 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5000 | PyErr_Fetch(&ptype, &pvalue, &ptraceback); |
| 5001 | if (PyString_Check(pvalue)) { |
| 5002 | PyObject *newmsg; |
| 5003 | newmsg = PyString_FromFormat( |
| 5004 | "Error when calling the metaclass bases\n" |
| 5005 | " %s", |
| 5006 | PyString_AS_STRING(pvalue)); |
| 5007 | if (newmsg != NULL) { |
| 5008 | Py_DECREF(pvalue); |
| 5009 | pvalue = newmsg; |
| 5010 | } |
| 5011 | } |
| 5012 | PyErr_Restore(ptype, pvalue, ptraceback); |
| 5013 | } |
| 5014 | return result; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 5015 | } |
| 5016 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 5017 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 5018 | exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5019 | PyObject *locals) |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 5020 | { |
Benjamin Peterson | d2903bd | 2014-08-09 19:39:36 -0700 | [diff] [blame] | 5021 | int n; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5022 | PyObject *v; |
| 5023 | int plain = 0; |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 5024 | |
Benjamin Peterson | d2903bd | 2014-08-09 19:39:36 -0700 | [diff] [blame] | 5025 | if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None && |
| 5026 | ((n = PyTuple_Size(prog)) == 2 || n == 3)) { |
| 5027 | /* Backward compatibility hack */ |
| 5028 | globals = PyTuple_GetItem(prog, 1); |
| 5029 | if (n == 3) |
| 5030 | locals = PyTuple_GetItem(prog, 2); |
| 5031 | prog = PyTuple_GetItem(prog, 0); |
| 5032 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5033 | if (globals == Py_None) { |
| 5034 | globals = PyEval_GetGlobals(); |
| 5035 | if (locals == Py_None) { |
| 5036 | locals = PyEval_GetLocals(); |
| 5037 | plain = 1; |
| 5038 | } |
| 5039 | if (!globals || !locals) { |
| 5040 | PyErr_SetString(PyExc_SystemError, |
| 5041 | "globals and locals cannot be NULL"); |
| 5042 | return -1; |
| 5043 | } |
| 5044 | } |
| 5045 | else if (locals == Py_None) |
| 5046 | locals = globals; |
| 5047 | if (!PyString_Check(prog) && |
Benjamin Peterson | 78821dd | 2009-01-25 17:15:10 +0000 | [diff] [blame] | 5048 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5049 | !PyUnicode_Check(prog) && |
Benjamin Peterson | 78821dd | 2009-01-25 17:15:10 +0000 | [diff] [blame] | 5050 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5051 | !PyCode_Check(prog) && |
| 5052 | !PyFile_Check(prog)) { |
| 5053 | PyErr_SetString(PyExc_TypeError, |
| 5054 | "exec: arg 1 must be a string, file, or code object"); |
| 5055 | return -1; |
| 5056 | } |
| 5057 | if (!PyDict_Check(globals)) { |
| 5058 | PyErr_SetString(PyExc_TypeError, |
| 5059 | "exec: arg 2 must be a dictionary or None"); |
| 5060 | return -1; |
| 5061 | } |
| 5062 | if (!PyMapping_Check(locals)) { |
| 5063 | PyErr_SetString(PyExc_TypeError, |
| 5064 | "exec: arg 3 must be a mapping or None"); |
| 5065 | return -1; |
| 5066 | } |
| 5067 | if (PyDict_GetItemString(globals, "__builtins__") == NULL) |
| 5068 | PyDict_SetItemString(globals, "__builtins__", f->f_builtins); |
| 5069 | if (PyCode_Check(prog)) { |
| 5070 | if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) { |
| 5071 | PyErr_SetString(PyExc_TypeError, |
| 5072 | "code object passed to exec may not contain free variables"); |
| 5073 | return -1; |
| 5074 | } |
| 5075 | v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals); |
| 5076 | } |
| 5077 | else if (PyFile_Check(prog)) { |
| 5078 | FILE *fp = PyFile_AsFile(prog); |
| 5079 | char *name = PyString_AsString(PyFile_Name(prog)); |
| 5080 | PyCompilerFlags cf; |
| 5081 | if (name == NULL) |
| 5082 | return -1; |
| 5083 | cf.cf_flags = 0; |
| 5084 | if (PyEval_MergeCompilerFlags(&cf)) |
| 5085 | v = PyRun_FileFlags(fp, name, Py_file_input, globals, |
| 5086 | locals, &cf); |
| 5087 | else |
| 5088 | v = PyRun_File(fp, name, Py_file_input, globals, |
| 5089 | locals); |
| 5090 | } |
| 5091 | else { |
| 5092 | PyObject *tmp = NULL; |
| 5093 | char *str; |
| 5094 | PyCompilerFlags cf; |
| 5095 | cf.cf_flags = 0; |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 5096 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5097 | if (PyUnicode_Check(prog)) { |
| 5098 | tmp = PyUnicode_AsUTF8String(prog); |
| 5099 | if (tmp == NULL) |
| 5100 | return -1; |
| 5101 | prog = tmp; |
| 5102 | cf.cf_flags |= PyCF_SOURCE_IS_UTF8; |
| 5103 | } |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 5104 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5105 | if (PyString_AsStringAndSize(prog, &str, NULL)) |
| 5106 | return -1; |
| 5107 | if (PyEval_MergeCompilerFlags(&cf)) |
| 5108 | v = PyRun_StringFlags(str, Py_file_input, globals, |
| 5109 | locals, &cf); |
| 5110 | else |
| 5111 | v = PyRun_String(str, Py_file_input, globals, locals); |
| 5112 | Py_XDECREF(tmp); |
| 5113 | } |
| 5114 | if (plain) |
| 5115 | PyFrame_LocalsToFast(f, 0); |
| 5116 | if (v == NULL) |
| 5117 | return -1; |
| 5118 | Py_DECREF(v); |
| 5119 | return 0; |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 5120 | } |
Guido van Rossum | 24c1374 | 1995-02-14 09:42:43 +0000 | [diff] [blame] | 5121 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 5122 | static void |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5123 | format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj) |
| 5124 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5125 | char *obj_str; |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5126 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5127 | if (!obj) |
| 5128 | return; |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5129 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5130 | obj_str = PyString_AsString(obj); |
| 5131 | if (!obj_str) |
| 5132 | return; |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5133 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5134 | PyErr_Format(exc, format_str, obj_str); |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5135 | } |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5136 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 5137 | static PyObject * |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 5138 | string_concatenate(PyObject *v, PyObject *w, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5139 | PyFrameObject *f, unsigned char *next_instr) |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 5140 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5141 | /* This function implements 'variable += expr' when both arguments |
| 5142 | are strings. */ |
| 5143 | Py_ssize_t v_len = PyString_GET_SIZE(v); |
| 5144 | Py_ssize_t w_len = PyString_GET_SIZE(w); |
| 5145 | Py_ssize_t new_len = v_len + w_len; |
| 5146 | if (new_len < 0) { |
| 5147 | PyErr_SetString(PyExc_OverflowError, |
| 5148 | "strings are too large to concat"); |
| 5149 | return NULL; |
| 5150 | } |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 5151 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5152 | if (v->ob_refcnt == 2) { |
| 5153 | /* In the common case, there are 2 references to the value |
| 5154 | * stored in 'variable' when the += is performed: one on the |
| 5155 | * value stack (in 'v') and one still stored in the |
| 5156 | * 'variable'. We try to delete the variable now to reduce |
| 5157 | * the refcnt to 1. |
| 5158 | */ |
| 5159 | switch (*next_instr) { |
| 5160 | case STORE_FAST: |
| 5161 | { |
| 5162 | int oparg = PEEKARG(); |
| 5163 | PyObject **fastlocals = f->f_localsplus; |
| 5164 | if (GETLOCAL(oparg) == v) |
| 5165 | SETLOCAL(oparg, NULL); |
| 5166 | break; |
| 5167 | } |
| 5168 | case STORE_DEREF: |
| 5169 | { |
| 5170 | PyObject **freevars = (f->f_localsplus + |
| 5171 | f->f_code->co_nlocals); |
| 5172 | PyObject *c = freevars[PEEKARG()]; |
| 5173 | if (PyCell_GET(c) == v) |
| 5174 | PyCell_Set(c, NULL); |
| 5175 | break; |
| 5176 | } |
| 5177 | case STORE_NAME: |
| 5178 | { |
| 5179 | PyObject *names = f->f_code->co_names; |
| 5180 | PyObject *name = GETITEM(names, PEEKARG()); |
| 5181 | PyObject *locals = f->f_locals; |
| 5182 | if (PyDict_CheckExact(locals) && |
| 5183 | PyDict_GetItem(locals, name) == v) { |
| 5184 | if (PyDict_DelItem(locals, name) != 0) { |
| 5185 | PyErr_Clear(); |
| 5186 | } |
| 5187 | } |
| 5188 | break; |
| 5189 | } |
| 5190 | } |
| 5191 | } |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 5192 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5193 | if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) { |
| 5194 | /* Now we own the last reference to 'v', so we can resize it |
| 5195 | * in-place. |
| 5196 | */ |
| 5197 | if (_PyString_Resize(&v, new_len) != 0) { |
| 5198 | /* XXX if _PyString_Resize() fails, 'v' has been |
| 5199 | * deallocated so it cannot be put back into |
| 5200 | * 'variable'. The MemoryError is raised when there |
| 5201 | * is no value in 'variable', which might (very |
| 5202 | * remotely) be a cause of incompatibilities. |
| 5203 | */ |
| 5204 | return NULL; |
| 5205 | } |
| 5206 | /* copy 'w' into the newly allocated area of 'v' */ |
| 5207 | memcpy(PyString_AS_STRING(v) + v_len, |
| 5208 | PyString_AS_STRING(w), w_len); |
| 5209 | return v; |
| 5210 | } |
| 5211 | else { |
| 5212 | /* When in-place resizing is not an option. */ |
| 5213 | PyString_Concat(&v, w); |
| 5214 | return v; |
| 5215 | } |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 5216 | } |
| 5217 | |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5218 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 5219 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 5220 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 5221 | getarray(long a[256]) |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5222 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5223 | int i; |
| 5224 | PyObject *l = PyList_New(256); |
| 5225 | if (l == NULL) return NULL; |
| 5226 | for (i = 0; i < 256; i++) { |
| 5227 | PyObject *x = PyInt_FromLong(a[i]); |
| 5228 | if (x == NULL) { |
| 5229 | Py_DECREF(l); |
| 5230 | return NULL; |
| 5231 | } |
| 5232 | PyList_SetItem(l, i, x); |
| 5233 | } |
| 5234 | for (i = 0; i < 256; i++) |
| 5235 | a[i] = 0; |
| 5236 | return l; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5237 | } |
| 5238 | |
| 5239 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 5240 | _Py_GetDXProfile(PyObject *self, PyObject *args) |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5241 | { |
| 5242 | #ifndef DXPAIRS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5243 | return getarray(dxp); |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5244 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5245 | int i; |
| 5246 | PyObject *l = PyList_New(257); |
| 5247 | if (l == NULL) return NULL; |
| 5248 | for (i = 0; i < 257; i++) { |
| 5249 | PyObject *x = getarray(dxpairs[i]); |
| 5250 | if (x == NULL) { |
| 5251 | Py_DECREF(l); |
| 5252 | return NULL; |
| 5253 | } |
| 5254 | PyList_SetItem(l, i, x); |
| 5255 | } |
| 5256 | return l; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5257 | #endif |
| 5258 | } |
| 5259 | |
| 5260 | #endif |