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