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 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2601 | w = GETITEM(names, oparg); |
| 2602 | x = PyDict_GetItemString(f->f_builtins, "__import__"); |
| 2603 | if (x == NULL) { |
| 2604 | PyErr_SetString(PyExc_ImportError, |
| 2605 | "__import__ not found"); |
| 2606 | break; |
| 2607 | } |
| 2608 | Py_INCREF(x); |
| 2609 | v = POP(); |
| 2610 | u = TOP(); |
| 2611 | if (PyInt_AsLong(u) != -1 || PyErr_Occurred()) |
| 2612 | w = PyTuple_Pack(5, |
| 2613 | w, |
| 2614 | f->f_globals, |
| 2615 | f->f_locals == NULL ? |
| 2616 | Py_None : f->f_locals, |
| 2617 | v, |
| 2618 | u); |
| 2619 | else |
| 2620 | w = PyTuple_Pack(4, |
| 2621 | w, |
| 2622 | f->f_globals, |
| 2623 | f->f_locals == NULL ? |
| 2624 | Py_None : f->f_locals, |
| 2625 | v); |
| 2626 | Py_DECREF(v); |
| 2627 | Py_DECREF(u); |
| 2628 | if (w == NULL) { |
| 2629 | u = POP(); |
| 2630 | Py_DECREF(x); |
| 2631 | x = NULL; |
| 2632 | break; |
| 2633 | } |
| 2634 | READ_TIMESTAMP(intr0); |
| 2635 | v = x; |
| 2636 | x = PyEval_CallObject(v, w); |
| 2637 | Py_DECREF(v); |
| 2638 | READ_TIMESTAMP(intr1); |
| 2639 | Py_DECREF(w); |
| 2640 | SET_TOP(x); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2641 | if (x != NULL) DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2642 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2643 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2644 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2645 | TARGET_NOARG(IMPORT_STAR) |
| 2646 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2647 | v = POP(); |
| 2648 | PyFrame_FastToLocals(f); |
| 2649 | if ((x = f->f_locals) == NULL) { |
| 2650 | PyErr_SetString(PyExc_SystemError, |
| 2651 | "no locals found during 'import *'"); |
Serhiy Storchaka | 9fbb65e | 2017-03-08 13:44:33 +0200 | [diff] [blame] | 2652 | Py_DECREF(v); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2653 | break; |
| 2654 | } |
| 2655 | READ_TIMESTAMP(intr0); |
| 2656 | err = import_all_from(x, v); |
| 2657 | READ_TIMESTAMP(intr1); |
| 2658 | PyFrame_LocalsToFast(f, 0); |
| 2659 | Py_DECREF(v); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2660 | if (err == 0) DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2661 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2662 | } |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2663 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2664 | TARGET(IMPORT_FROM) |
| 2665 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2666 | w = GETITEM(names, oparg); |
| 2667 | v = TOP(); |
| 2668 | READ_TIMESTAMP(intr0); |
| 2669 | x = import_from(v, w); |
| 2670 | READ_TIMESTAMP(intr1); |
| 2671 | PUSH(x); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2672 | if (x != NULL) DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2673 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2674 | } |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2675 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2676 | TARGET(JUMP_FORWARD) |
| 2677 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2678 | JUMPBY(oparg); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2679 | FAST_DISPATCH(); |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2680 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2681 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2682 | PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE); |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2683 | TARGET(POP_JUMP_IF_FALSE) |
| 2684 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2685 | w = POP(); |
| 2686 | if (w == Py_True) { |
| 2687 | Py_DECREF(w); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2688 | FAST_DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2689 | } |
| 2690 | if (w == Py_False) { |
| 2691 | Py_DECREF(w); |
| 2692 | JUMPTO(oparg); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2693 | FAST_DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2694 | } |
| 2695 | err = PyObject_IsTrue(w); |
| 2696 | Py_DECREF(w); |
| 2697 | if (err > 0) |
| 2698 | err = 0; |
| 2699 | else if (err == 0) |
| 2700 | JUMPTO(oparg); |
| 2701 | else |
| 2702 | break; |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2703 | DISPATCH(); |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2704 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2705 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2706 | PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE); |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2707 | TARGET(POP_JUMP_IF_TRUE) |
| 2708 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2709 | w = POP(); |
| 2710 | if (w == Py_False) { |
| 2711 | Py_DECREF(w); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2712 | FAST_DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2713 | } |
| 2714 | if (w == Py_True) { |
| 2715 | Py_DECREF(w); |
| 2716 | JUMPTO(oparg); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2717 | FAST_DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2718 | } |
| 2719 | err = PyObject_IsTrue(w); |
| 2720 | Py_DECREF(w); |
| 2721 | if (err > 0) { |
| 2722 | err = 0; |
| 2723 | JUMPTO(oparg); |
| 2724 | } |
| 2725 | else if (err == 0) |
| 2726 | ; |
| 2727 | else |
| 2728 | break; |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2729 | DISPATCH(); |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2730 | } |
Jeffrey Yasskin | 68d6852 | 2009-02-28 19:03:21 +0000 | [diff] [blame] | 2731 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2732 | TARGET(JUMP_IF_FALSE_OR_POP) |
| 2733 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2734 | w = TOP(); |
| 2735 | if (w == Py_True) { |
| 2736 | STACKADJ(-1); |
| 2737 | Py_DECREF(w); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2738 | FAST_DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2739 | } |
| 2740 | if (w == Py_False) { |
| 2741 | JUMPTO(oparg); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2742 | FAST_DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2743 | } |
| 2744 | err = PyObject_IsTrue(w); |
| 2745 | if (err > 0) { |
| 2746 | STACKADJ(-1); |
| 2747 | Py_DECREF(w); |
| 2748 | err = 0; |
| 2749 | } |
| 2750 | else if (err == 0) |
| 2751 | JUMPTO(oparg); |
| 2752 | else |
| 2753 | break; |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2754 | DISPATCH(); |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2755 | } |
Jeffrey Yasskin | 68d6852 | 2009-02-28 19:03:21 +0000 | [diff] [blame] | 2756 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2757 | TARGET(JUMP_IF_TRUE_OR_POP) |
| 2758 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2759 | w = TOP(); |
| 2760 | if (w == Py_False) { |
| 2761 | STACKADJ(-1); |
| 2762 | Py_DECREF(w); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2763 | FAST_DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2764 | } |
| 2765 | if (w == Py_True) { |
| 2766 | JUMPTO(oparg); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2767 | FAST_DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2768 | } |
| 2769 | err = PyObject_IsTrue(w); |
| 2770 | if (err > 0) { |
| 2771 | err = 0; |
| 2772 | JUMPTO(oparg); |
| 2773 | } |
| 2774 | else if (err == 0) { |
| 2775 | STACKADJ(-1); |
| 2776 | Py_DECREF(w); |
| 2777 | } |
| 2778 | else |
| 2779 | break; |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2780 | DISPATCH(); |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2781 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2782 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2783 | PREDICTED_WITH_ARG(JUMP_ABSOLUTE); |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2784 | TARGET(JUMP_ABSOLUTE) |
| 2785 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2786 | JUMPTO(oparg); |
Raymond Hettinger | dc1d1ba | 2007-11-07 02:45:46 +0000 | [diff] [blame] | 2787 | #if FAST_LOOPS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2788 | /* Enabling this path speeds-up all while and for-loops by bypassing |
| 2789 | the per-loop checks for signals. By default, this should be turned-off |
| 2790 | because it prevents detection of a control-break in tight loops like |
| 2791 | "while 1: pass". Compile with this option turned-on when you need |
| 2792 | the speed-up and do not need break checking inside tight loops (ones |
| 2793 | that contain only instructions ending with goto fast_next_opcode). |
| 2794 | */ |
| 2795 | goto fast_next_opcode; |
Raymond Hettinger | dc1d1ba | 2007-11-07 02:45:46 +0000 | [diff] [blame] | 2796 | #else |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2797 | DISPATCH(); |
Raymond Hettinger | dc1d1ba | 2007-11-07 02:45:46 +0000 | [diff] [blame] | 2798 | #endif |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2799 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2800 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2801 | TARGET_NOARG(GET_ITER) |
| 2802 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2803 | /* before: [obj]; after [getiter(obj)] */ |
| 2804 | v = TOP(); |
| 2805 | x = PyObject_GetIter(v); |
| 2806 | Py_DECREF(v); |
| 2807 | if (x != NULL) { |
| 2808 | SET_TOP(x); |
| 2809 | PREDICT(FOR_ITER); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2810 | DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2811 | } |
| 2812 | STACKADJ(-1); |
| 2813 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2814 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2815 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2816 | PREDICTED_WITH_ARG(FOR_ITER); |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2817 | TARGET(FOR_ITER) |
| 2818 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2819 | /* before: [iter]; after: [iter, iter()] *or* [] */ |
| 2820 | v = TOP(); |
| 2821 | x = (*v->ob_type->tp_iternext)(v); |
| 2822 | if (x != NULL) { |
| 2823 | PUSH(x); |
| 2824 | PREDICT(STORE_FAST); |
| 2825 | PREDICT(UNPACK_SEQUENCE); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2826 | DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2827 | } |
| 2828 | if (PyErr_Occurred()) { |
| 2829 | if (!PyErr_ExceptionMatches( |
| 2830 | PyExc_StopIteration)) |
| 2831 | break; |
| 2832 | PyErr_Clear(); |
| 2833 | } |
| 2834 | /* iterator ended normally */ |
| 2835 | x = v = POP(); |
| 2836 | Py_DECREF(v); |
| 2837 | JUMPBY(oparg); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2838 | DISPATCH(); |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2839 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2840 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2841 | TARGET_NOARG(BREAK_LOOP) |
| 2842 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2843 | why = WHY_BREAK; |
| 2844 | goto fast_block_end; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2845 | } |
Raymond Hettinger | 2d783e9 | 2004-03-12 09:12:22 +0000 | [diff] [blame] | 2846 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2847 | TARGET(CONTINUE_LOOP) |
| 2848 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2849 | retval = PyInt_FromLong(oparg); |
| 2850 | if (!retval) { |
| 2851 | x = NULL; |
| 2852 | break; |
| 2853 | } |
| 2854 | why = WHY_CONTINUE; |
| 2855 | goto fast_block_end; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2856 | } |
Raymond Hettinger | 2d783e9 | 2004-03-12 09:12:22 +0000 | [diff] [blame] | 2857 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2858 | TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally) |
| 2859 | TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally) |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2860 | TARGET(SETUP_FINALLY) |
| 2861 | _setup_finally: |
| 2862 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2863 | /* NOTE: If you add any new block-setup opcodes that |
| 2864 | are not try/except/finally handlers, you may need |
| 2865 | to update the PyGen_NeedsFinalizing() function. |
| 2866 | */ |
Phillip J. Eby | 2ba9661 | 2006-04-10 17:51:05 +0000 | [diff] [blame] | 2867 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2868 | PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg, |
| 2869 | STACK_LEVEL()); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2870 | DISPATCH(); |
| 2871 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2872 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2873 | |
| 2874 | |
| 2875 | TARGET(SETUP_WITH) |
| 2876 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2877 | { |
| 2878 | static PyObject *exit, *enter; |
| 2879 | w = TOP(); |
| 2880 | x = special_lookup(w, "__exit__", &exit); |
| 2881 | if (!x) |
| 2882 | break; |
| 2883 | SET_TOP(x); |
| 2884 | u = special_lookup(w, "__enter__", &enter); |
| 2885 | Py_DECREF(w); |
| 2886 | if (!u) { |
| 2887 | x = NULL; |
| 2888 | break; |
| 2889 | } |
| 2890 | x = PyObject_CallFunctionObjArgs(u, NULL); |
| 2891 | Py_DECREF(u); |
| 2892 | if (!x) |
| 2893 | break; |
| 2894 | /* Setup a finally block (SETUP_WITH as a block is |
| 2895 | equivalent to SETUP_FINALLY except it normalizes |
| 2896 | the exception) before pushing the result of |
| 2897 | __enter__ on the stack. */ |
| 2898 | PyFrame_BlockSetup(f, SETUP_WITH, INSTR_OFFSET() + oparg, |
| 2899 | STACK_LEVEL()); |
Benjamin Peterson | 1880d8b | 2009-05-25 13:13:44 +0000 | [diff] [blame] | 2900 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2901 | PUSH(x); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 2902 | DISPATCH(); |
| 2903 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2904 | } |
Benjamin Peterson | 1880d8b | 2009-05-25 13:13:44 +0000 | [diff] [blame] | 2905 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2906 | TARGET_NOARG(WITH_CLEANUP) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2907 | { |
| 2908 | /* At the top of the stack are 1-3 values indicating |
| 2909 | how/why we entered the finally clause: |
| 2910 | - TOP = None |
| 2911 | - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval |
| 2912 | - TOP = WHY_*; no retval below it |
| 2913 | - (TOP, SECOND, THIRD) = exc_info() |
| 2914 | Below them is EXIT, the context.__exit__ bound method. |
| 2915 | In the last case, we must call |
| 2916 | EXIT(TOP, SECOND, THIRD) |
| 2917 | otherwise we must call |
| 2918 | EXIT(None, None, None) |
Nick Coghlan | 7af53be | 2008-03-07 14:13:28 +0000 | [diff] [blame] | 2919 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2920 | In all cases, we remove EXIT from the stack, leaving |
| 2921 | the rest in the same order. |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 2922 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2923 | In addition, if the stack represents an exception, |
| 2924 | *and* the function call returns a 'true' value, we |
| 2925 | "zap" this information, to prevent END_FINALLY from |
| 2926 | re-raising the exception. (But non-local gotos |
| 2927 | should still be resumed.) |
| 2928 | */ |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 2929 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2930 | PyObject *exit_func; |
Nick Coghlan | 7af53be | 2008-03-07 14:13:28 +0000 | [diff] [blame] | 2931 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2932 | u = POP(); |
| 2933 | if (u == Py_None) { |
| 2934 | exit_func = TOP(); |
| 2935 | SET_TOP(u); |
| 2936 | v = w = Py_None; |
| 2937 | } |
| 2938 | else if (PyInt_Check(u)) { |
| 2939 | switch(PyInt_AS_LONG(u)) { |
| 2940 | case WHY_RETURN: |
| 2941 | case WHY_CONTINUE: |
| 2942 | /* Retval in TOP. */ |
| 2943 | exit_func = SECOND(); |
| 2944 | SET_SECOND(TOP()); |
| 2945 | SET_TOP(u); |
| 2946 | break; |
| 2947 | default: |
| 2948 | exit_func = TOP(); |
| 2949 | SET_TOP(u); |
| 2950 | break; |
| 2951 | } |
| 2952 | u = v = w = Py_None; |
| 2953 | } |
| 2954 | else { |
| 2955 | v = TOP(); |
| 2956 | w = SECOND(); |
| 2957 | exit_func = THIRD(); |
| 2958 | SET_TOP(u); |
| 2959 | SET_SECOND(v); |
| 2960 | SET_THIRD(w); |
| 2961 | } |
| 2962 | /* XXX Not the fastest way to call it... */ |
| 2963 | x = PyObject_CallFunctionObjArgs(exit_func, u, v, w, |
| 2964 | NULL); |
| 2965 | Py_DECREF(exit_func); |
| 2966 | if (x == NULL) |
| 2967 | break; /* Go to error exit */ |
Amaury Forgeot d'Arc | ad9b599 | 2008-12-10 23:22:49 +0000 | [diff] [blame] | 2968 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2969 | if (u != Py_None) |
| 2970 | err = PyObject_IsTrue(x); |
| 2971 | else |
| 2972 | err = 0; |
| 2973 | Py_DECREF(x); |
Amaury Forgeot d'Arc | ad9b599 | 2008-12-10 23:22:49 +0000 | [diff] [blame] | 2974 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2975 | if (err < 0) |
| 2976 | break; /* Go to error exit */ |
| 2977 | else if (err > 0) { |
| 2978 | err = 0; |
| 2979 | /* There was an exception and a true return */ |
| 2980 | STACKADJ(-2); |
| 2981 | Py_INCREF(Py_None); |
| 2982 | SET_TOP(Py_None); |
| 2983 | Py_DECREF(u); |
| 2984 | Py_DECREF(v); |
| 2985 | Py_DECREF(w); |
| 2986 | } else { |
| 2987 | /* The stack was rearranged to remove EXIT |
| 2988 | above. Let END_FINALLY do its thing */ |
| 2989 | } |
| 2990 | PREDICT(END_FINALLY); |
| 2991 | break; |
| 2992 | } |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 2993 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 2994 | TARGET(CALL_FUNCTION) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2995 | { |
| 2996 | PyObject **sp; |
| 2997 | PCALL(PCALL_ALL); |
| 2998 | sp = stack_pointer; |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 2999 | #ifdef WITH_TSC |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3000 | x = call_function(&sp, oparg, &intr0, &intr1); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 3001 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3002 | x = call_function(&sp, oparg); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 3003 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3004 | stack_pointer = sp; |
| 3005 | PUSH(x); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 3006 | if (x != NULL) DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3007 | break; |
| 3008 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3009 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 3010 | TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw) |
| 3011 | TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw) |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 3012 | TARGET(CALL_FUNCTION_VAR_KW) |
| 3013 | _call_function_var_kw: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3014 | { |
| 3015 | int na = oparg & 0xff; |
| 3016 | int nk = (oparg>>8) & 0xff; |
| 3017 | int flags = (opcode - CALL_FUNCTION) & 3; |
| 3018 | int n = na + 2 * nk; |
| 3019 | PyObject **pfunc, *func, **sp; |
| 3020 | PCALL(PCALL_ALL); |
| 3021 | if (flags & CALL_FLAG_VAR) |
| 3022 | n++; |
| 3023 | if (flags & CALL_FLAG_KW) |
| 3024 | n++; |
| 3025 | pfunc = stack_pointer - n - 1; |
| 3026 | func = *pfunc; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3027 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3028 | if (PyMethod_Check(func) |
Stefan Krah | 7ff7825 | 2010-06-23 18:12:09 +0000 | [diff] [blame] | 3029 | && PyMethod_GET_SELF(func) != NULL) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3030 | PyObject *self = PyMethod_GET_SELF(func); |
| 3031 | Py_INCREF(self); |
| 3032 | func = PyMethod_GET_FUNCTION(func); |
| 3033 | Py_INCREF(func); |
| 3034 | Py_DECREF(*pfunc); |
| 3035 | *pfunc = self; |
| 3036 | na++; |
| 3037 | } else |
| 3038 | Py_INCREF(func); |
| 3039 | sp = stack_pointer; |
| 3040 | READ_TIMESTAMP(intr0); |
| 3041 | x = ext_do_call(func, &sp, flags, na, nk); |
| 3042 | READ_TIMESTAMP(intr1); |
| 3043 | stack_pointer = sp; |
| 3044 | Py_DECREF(func); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3045 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3046 | while (stack_pointer > pfunc) { |
| 3047 | w = POP(); |
| 3048 | Py_DECREF(w); |
| 3049 | } |
| 3050 | PUSH(x); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 3051 | if (x != NULL) DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3052 | break; |
| 3053 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3054 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 3055 | |
| 3056 | TARGET(MAKE_FUNCTION) |
| 3057 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3058 | v = POP(); /* code object */ |
| 3059 | x = PyFunction_New(v, f->f_globals); |
| 3060 | Py_DECREF(v); |
| 3061 | /* XXX Maybe this should be a separate opcode? */ |
| 3062 | if (x != NULL && oparg > 0) { |
| 3063 | v = PyTuple_New(oparg); |
| 3064 | if (v == NULL) { |
| 3065 | Py_DECREF(x); |
| 3066 | x = NULL; |
| 3067 | break; |
| 3068 | } |
| 3069 | while (--oparg >= 0) { |
| 3070 | w = POP(); |
| 3071 | PyTuple_SET_ITEM(v, oparg, w); |
| 3072 | } |
| 3073 | err = PyFunction_SetDefaults(x, v); |
| 3074 | Py_DECREF(v); |
| 3075 | } |
| 3076 | PUSH(x); |
| 3077 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 3078 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 3079 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 3080 | TARGET(MAKE_CLOSURE) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3081 | { |
| 3082 | v = POP(); /* code object */ |
| 3083 | x = PyFunction_New(v, f->f_globals); |
| 3084 | Py_DECREF(v); |
| 3085 | if (x != NULL) { |
| 3086 | v = POP(); |
| 3087 | if (PyFunction_SetClosure(x, v) != 0) { |
| 3088 | /* Can't happen unless bytecode is corrupt. */ |
| 3089 | why = WHY_EXCEPTION; |
| 3090 | } |
| 3091 | Py_DECREF(v); |
| 3092 | } |
| 3093 | if (x != NULL && oparg > 0) { |
| 3094 | v = PyTuple_New(oparg); |
| 3095 | if (v == NULL) { |
| 3096 | Py_DECREF(x); |
| 3097 | x = NULL; |
| 3098 | break; |
| 3099 | } |
| 3100 | while (--oparg >= 0) { |
| 3101 | w = POP(); |
| 3102 | PyTuple_SET_ITEM(v, oparg, w); |
| 3103 | } |
| 3104 | if (PyFunction_SetDefaults(x, v) != 0) { |
| 3105 | /* Can't happen unless |
| 3106 | PyFunction_SetDefaults changes. */ |
| 3107 | why = WHY_EXCEPTION; |
| 3108 | } |
| 3109 | Py_DECREF(v); |
| 3110 | } |
| 3111 | PUSH(x); |
| 3112 | break; |
| 3113 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3114 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 3115 | TARGET(BUILD_SLICE) |
| 3116 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3117 | if (oparg == 3) |
| 3118 | w = POP(); |
| 3119 | else |
| 3120 | w = NULL; |
| 3121 | v = POP(); |
| 3122 | u = TOP(); |
| 3123 | x = PySlice_New(u, v, w); |
| 3124 | Py_DECREF(u); |
| 3125 | Py_DECREF(v); |
| 3126 | Py_XDECREF(w); |
| 3127 | SET_TOP(x); |
Benjamin Peterson | 14462d4 | 2015-08-19 20:38:39 -0700 | [diff] [blame] | 3128 | if (x != NULL) DISPATCH(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3129 | break; |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 3130 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 3131 | |
Benjamin Peterson | 2c992a0 | 2015-05-28 12:45:31 -0500 | [diff] [blame] | 3132 | TARGET(EXTENDED_ARG) |
| 3133 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3134 | opcode = NEXTOP(); |
| 3135 | oparg = oparg<<16 | NEXTARG(); |
| 3136 | goto dispatch_opcode; |
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 | #if USE_COMPUTED_GOTOS |
| 3140 | _unknown_opcode: |
| 3141 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3142 | default: |
| 3143 | fprintf(stderr, |
| 3144 | "XXX lineno: %d, opcode: %d\n", |
| 3145 | PyFrame_GetLineNumber(f), |
| 3146 | opcode); |
| 3147 | PyErr_SetString(PyExc_SystemError, "unknown opcode"); |
| 3148 | why = WHY_EXCEPTION; |
| 3149 | break; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 3150 | |
| 3151 | #ifdef CASE_TOO_BIG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3152 | } |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 3153 | #endif |
| 3154 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3155 | } /* switch */ |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3156 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3157 | on_error: |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3158 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3159 | READ_TIMESTAMP(inst1); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 3160 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3161 | /* Quickly continue if no error occurred */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3162 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3163 | if (why == WHY_NOT) { |
| 3164 | if (err == 0 && x != NULL) { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3165 | #ifdef CHECKEXC |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3166 | /* This check is expensive! */ |
| 3167 | if (PyErr_Occurred()) |
| 3168 | fprintf(stderr, |
| 3169 | "XXX undetected error\n"); |
| 3170 | else { |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 3171 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3172 | READ_TIMESTAMP(loop1); |
| 3173 | continue; /* Normal, fast path */ |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 3174 | #ifdef CHECKEXC |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3175 | } |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 3176 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3177 | } |
| 3178 | why = WHY_EXCEPTION; |
| 3179 | x = Py_None; |
| 3180 | err = 0; |
| 3181 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3182 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3183 | /* Double-check exception status */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3184 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3185 | if (why == WHY_EXCEPTION || why == WHY_RERAISE) { |
| 3186 | if (!PyErr_Occurred()) { |
| 3187 | PyErr_SetString(PyExc_SystemError, |
| 3188 | "error return without exception set"); |
| 3189 | why = WHY_EXCEPTION; |
| 3190 | } |
| 3191 | } |
Guido van Rossum | eb894eb | 1999-03-09 16:16:45 +0000 | [diff] [blame] | 3192 | #ifdef CHECKEXC |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3193 | else { |
| 3194 | /* This check is expensive! */ |
| 3195 | if (PyErr_Occurred()) { |
| 3196 | char buf[128]; |
| 3197 | sprintf(buf, "Stack unwind with exception " |
| 3198 | "set and why=%d", why); |
| 3199 | Py_FatalError(buf); |
| 3200 | } |
| 3201 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3202 | #endif |
| 3203 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3204 | /* Log traceback info if this is a real exception */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3205 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3206 | if (why == WHY_EXCEPTION) { |
| 3207 | PyTraceBack_Here(f); |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 3208 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3209 | if (tstate->c_tracefunc != NULL) |
| 3210 | call_exc_trace(tstate->c_tracefunc, |
| 3211 | tstate->c_traceobj, f); |
| 3212 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3213 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3214 | /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3215 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3216 | if (why == WHY_RERAISE) |
| 3217 | why = WHY_EXCEPTION; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3218 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3219 | /* Unwind stacks if a (pseudo) exception occurred */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3220 | |
Raymond Hettinger | 1dd8309 | 2004-02-06 18:32:33 +0000 | [diff] [blame] | 3221 | fast_block_end: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3222 | while (why != WHY_NOT && f->f_iblock > 0) { |
| 3223 | /* Peek at the current block. */ |
| 3224 | PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1]; |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3225 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3226 | assert(why != WHY_YIELD); |
| 3227 | if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) { |
| 3228 | why = WHY_NOT; |
| 3229 | JUMPTO(PyInt_AS_LONG(retval)); |
| 3230 | Py_DECREF(retval); |
| 3231 | break; |
| 3232 | } |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3233 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3234 | /* Now we have to pop the block. */ |
| 3235 | f->f_iblock--; |
Benjamin Peterson | 4a3cf19 | 2009-07-01 23:45:19 +0000 | [diff] [blame] | 3236 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3237 | while (STACK_LEVEL() > b->b_level) { |
| 3238 | v = POP(); |
| 3239 | Py_XDECREF(v); |
| 3240 | } |
| 3241 | if (b->b_type == SETUP_LOOP && why == WHY_BREAK) { |
| 3242 | why = WHY_NOT; |
| 3243 | JUMPTO(b->b_handler); |
| 3244 | break; |
| 3245 | } |
| 3246 | if (b->b_type == SETUP_FINALLY || |
| 3247 | (b->b_type == SETUP_EXCEPT && |
| 3248 | why == WHY_EXCEPTION) || |
| 3249 | b->b_type == SETUP_WITH) { |
| 3250 | if (why == WHY_EXCEPTION) { |
| 3251 | PyObject *exc, *val, *tb; |
| 3252 | PyErr_Fetch(&exc, &val, &tb); |
| 3253 | if (val == NULL) { |
| 3254 | val = Py_None; |
| 3255 | Py_INCREF(val); |
| 3256 | } |
| 3257 | /* Make the raw exception data |
| 3258 | available to the handler, |
| 3259 | so a program can emulate the |
| 3260 | Python main loop. Don't do |
| 3261 | this for 'finally'. */ |
| 3262 | if (b->b_type == SETUP_EXCEPT || |
| 3263 | b->b_type == SETUP_WITH) { |
| 3264 | PyErr_NormalizeException( |
| 3265 | &exc, &val, &tb); |
| 3266 | set_exc_info(tstate, |
| 3267 | exc, val, tb); |
| 3268 | } |
| 3269 | if (tb == NULL) { |
| 3270 | Py_INCREF(Py_None); |
| 3271 | PUSH(Py_None); |
| 3272 | } else |
| 3273 | PUSH(tb); |
| 3274 | PUSH(val); |
| 3275 | PUSH(exc); |
| 3276 | } |
| 3277 | else { |
| 3278 | if (why & (WHY_RETURN | WHY_CONTINUE)) |
| 3279 | PUSH(retval); |
| 3280 | v = PyInt_FromLong((long)why); |
| 3281 | PUSH(v); |
| 3282 | } |
| 3283 | why = WHY_NOT; |
| 3284 | JUMPTO(b->b_handler); |
| 3285 | break; |
| 3286 | } |
| 3287 | } /* unwind stack */ |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3288 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3289 | /* End the loop if we still have an error (or return) */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3290 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3291 | if (why != WHY_NOT) |
| 3292 | break; |
| 3293 | READ_TIMESTAMP(loop1); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3294 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3295 | } /* main loop */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3296 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3297 | assert(why != WHY_YIELD); |
| 3298 | /* Pop remaining stack entries. */ |
| 3299 | while (!EMPTY()) { |
| 3300 | v = POP(); |
| 3301 | Py_XDECREF(v); |
| 3302 | } |
Guido van Rossum | 35974fb | 2001-12-06 21:28:18 +0000 | [diff] [blame] | 3303 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3304 | if (why != WHY_RETURN) |
| 3305 | retval = NULL; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3306 | |
Raymond Hettinger | 1dd8309 | 2004-02-06 18:32:33 +0000 | [diff] [blame] | 3307 | fast_yield: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3308 | if (tstate->use_tracing) { |
| 3309 | if (tstate->c_tracefunc) { |
| 3310 | if (why == WHY_RETURN || why == WHY_YIELD) { |
| 3311 | if (call_trace(tstate->c_tracefunc, |
| 3312 | tstate->c_traceobj, f, |
| 3313 | PyTrace_RETURN, retval)) { |
| 3314 | Py_XDECREF(retval); |
| 3315 | retval = NULL; |
| 3316 | why = WHY_EXCEPTION; |
| 3317 | } |
| 3318 | } |
| 3319 | else if (why == WHY_EXCEPTION) { |
| 3320 | call_trace_protected(tstate->c_tracefunc, |
| 3321 | tstate->c_traceobj, f, |
| 3322 | PyTrace_RETURN, NULL); |
| 3323 | } |
| 3324 | } |
| 3325 | if (tstate->c_profilefunc) { |
| 3326 | if (why == WHY_EXCEPTION) |
| 3327 | call_trace_protected(tstate->c_profilefunc, |
| 3328 | tstate->c_profileobj, f, |
| 3329 | PyTrace_RETURN, NULL); |
| 3330 | else if (call_trace(tstate->c_profilefunc, |
| 3331 | tstate->c_profileobj, f, |
| 3332 | PyTrace_RETURN, retval)) { |
| 3333 | Py_XDECREF(retval); |
| 3334 | retval = NULL; |
| 3335 | why = WHY_EXCEPTION; |
| 3336 | } |
| 3337 | } |
| 3338 | } |
Guido van Rossum | a424013 | 1997-01-21 21:18:36 +0000 | [diff] [blame] | 3339 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3340 | if (tstate->frame->f_exc_type != NULL) |
| 3341 | reset_exc_info(tstate); |
| 3342 | else { |
| 3343 | assert(tstate->frame->f_exc_value == NULL); |
| 3344 | assert(tstate->frame->f_exc_traceback == NULL); |
| 3345 | } |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3346 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3347 | /* pop frame */ |
Thomas Wouters | ae406c6 | 2007-09-19 17:27:43 +0000 | [diff] [blame] | 3348 | exit_eval_frame: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3349 | Py_LeaveRecursiveCall(); |
| 3350 | tstate->frame = f->f_back; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3351 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3352 | return retval; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3353 | } |
| 3354 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3355 | /* 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] | 3356 | PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3357 | the test in the if statements in Misc/gdbinit (pystack and pystackv). */ |
Skip Montanaro | 786ea6b | 2004-03-01 15:44:05 +0000 | [diff] [blame] | 3358 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3359 | PyObject * |
| 3360 | PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3361 | PyObject **args, int argcount, PyObject **kws, int kwcount, |
| 3362 | PyObject **defs, int defcount, PyObject *closure) |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3363 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3364 | register PyFrameObject *f; |
| 3365 | register PyObject *retval = NULL; |
| 3366 | register PyObject **fastlocals, **freevars; |
| 3367 | PyThreadState *tstate = PyThreadState_GET(); |
| 3368 | PyObject *x, *u; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3369 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3370 | if (globals == NULL) { |
| 3371 | PyErr_SetString(PyExc_SystemError, |
| 3372 | "PyEval_EvalCodeEx: NULL globals"); |
| 3373 | return NULL; |
| 3374 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3375 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3376 | assert(tstate != NULL); |
| 3377 | assert(globals != NULL); |
| 3378 | f = PyFrame_New(tstate, co, globals, locals); |
| 3379 | if (f == NULL) |
| 3380 | return NULL; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3381 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3382 | fastlocals = f->f_localsplus; |
| 3383 | freevars = f->f_localsplus + co->co_nlocals; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3384 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3385 | if (co->co_argcount > 0 || |
| 3386 | co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) { |
| 3387 | int i; |
| 3388 | int n = argcount; |
| 3389 | PyObject *kwdict = NULL; |
| 3390 | if (co->co_flags & CO_VARKEYWORDS) { |
| 3391 | kwdict = PyDict_New(); |
| 3392 | if (kwdict == NULL) |
| 3393 | goto fail; |
| 3394 | i = co->co_argcount; |
| 3395 | if (co->co_flags & CO_VARARGS) |
| 3396 | i++; |
| 3397 | SETLOCAL(i, kwdict); |
| 3398 | } |
| 3399 | if (argcount > co->co_argcount) { |
| 3400 | if (!(co->co_flags & CO_VARARGS)) { |
| 3401 | PyErr_Format(PyExc_TypeError, |
| 3402 | "%.200s() takes %s %d " |
| 3403 | "argument%s (%d given)", |
| 3404 | PyString_AsString(co->co_name), |
| 3405 | defcount ? "at most" : "exactly", |
| 3406 | co->co_argcount, |
| 3407 | co->co_argcount == 1 ? "" : "s", |
Benjamin Peterson | da4faba | 2010-09-25 03:27:12 +0000 | [diff] [blame] | 3408 | argcount + kwcount); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3409 | goto fail; |
| 3410 | } |
| 3411 | n = co->co_argcount; |
| 3412 | } |
| 3413 | for (i = 0; i < n; i++) { |
| 3414 | x = args[i]; |
| 3415 | Py_INCREF(x); |
| 3416 | SETLOCAL(i, x); |
| 3417 | } |
| 3418 | if (co->co_flags & CO_VARARGS) { |
| 3419 | u = PyTuple_New(argcount - n); |
| 3420 | if (u == NULL) |
| 3421 | goto fail; |
| 3422 | SETLOCAL(co->co_argcount, u); |
| 3423 | for (i = n; i < argcount; i++) { |
| 3424 | x = args[i]; |
| 3425 | Py_INCREF(x); |
| 3426 | PyTuple_SET_ITEM(u, i-n, x); |
| 3427 | } |
| 3428 | } |
| 3429 | for (i = 0; i < kwcount; i++) { |
| 3430 | PyObject **co_varnames; |
| 3431 | PyObject *keyword = kws[2*i]; |
| 3432 | PyObject *value = kws[2*i + 1]; |
| 3433 | int j; |
| 3434 | if (keyword == NULL || !(PyString_Check(keyword) |
Benjamin Peterson | 78821dd | 2009-01-25 17:15:10 +0000 | [diff] [blame] | 3435 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3436 | || PyUnicode_Check(keyword) |
Benjamin Peterson | 78821dd | 2009-01-25 17:15:10 +0000 | [diff] [blame] | 3437 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3438 | )) { |
| 3439 | PyErr_Format(PyExc_TypeError, |
| 3440 | "%.200s() keywords must be strings", |
| 3441 | PyString_AsString(co->co_name)); |
| 3442 | goto fail; |
| 3443 | } |
| 3444 | /* Speed hack: do raw pointer compares. As names are |
| 3445 | normally interned this should almost always hit. */ |
| 3446 | co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item; |
| 3447 | for (j = 0; j < co->co_argcount; j++) { |
| 3448 | PyObject *nm = co_varnames[j]; |
| 3449 | if (nm == keyword) |
| 3450 | goto kw_found; |
| 3451 | } |
| 3452 | /* Slow fallback, just in case */ |
| 3453 | for (j = 0; j < co->co_argcount; j++) { |
| 3454 | PyObject *nm = co_varnames[j]; |
| 3455 | int cmp = PyObject_RichCompareBool( |
| 3456 | keyword, nm, Py_EQ); |
| 3457 | if (cmp > 0) |
| 3458 | goto kw_found; |
| 3459 | else if (cmp < 0) |
| 3460 | goto fail; |
| 3461 | } |
| 3462 | if (kwdict == NULL) { |
| 3463 | PyObject *kwd_str = kwd_as_string(keyword); |
| 3464 | if (kwd_str) { |
| 3465 | PyErr_Format(PyExc_TypeError, |
| 3466 | "%.200s() got an unexpected " |
| 3467 | "keyword argument '%.400s'", |
| 3468 | PyString_AsString(co->co_name), |
| 3469 | PyString_AsString(kwd_str)); |
| 3470 | Py_DECREF(kwd_str); |
| 3471 | } |
| 3472 | goto fail; |
| 3473 | } |
| 3474 | PyDict_SetItem(kwdict, keyword, value); |
| 3475 | continue; |
| 3476 | kw_found: |
| 3477 | if (GETLOCAL(j) != NULL) { |
| 3478 | PyObject *kwd_str = kwd_as_string(keyword); |
| 3479 | if (kwd_str) { |
| 3480 | PyErr_Format(PyExc_TypeError, |
| 3481 | "%.200s() got multiple " |
| 3482 | "values for keyword " |
| 3483 | "argument '%.400s'", |
| 3484 | PyString_AsString(co->co_name), |
| 3485 | PyString_AsString(kwd_str)); |
| 3486 | Py_DECREF(kwd_str); |
| 3487 | } |
| 3488 | goto fail; |
| 3489 | } |
| 3490 | Py_INCREF(value); |
| 3491 | SETLOCAL(j, value); |
| 3492 | } |
| 3493 | if (argcount < co->co_argcount) { |
| 3494 | int m = co->co_argcount - defcount; |
| 3495 | for (i = argcount; i < m; i++) { |
| 3496 | if (GETLOCAL(i) == NULL) { |
| 3497 | int j, given = 0; |
| 3498 | for (j = 0; j < co->co_argcount; j++) |
| 3499 | if (GETLOCAL(j)) |
| 3500 | given++; |
| 3501 | PyErr_Format(PyExc_TypeError, |
| 3502 | "%.200s() takes %s %d " |
| 3503 | "argument%s (%d given)", |
| 3504 | PyString_AsString(co->co_name), |
| 3505 | ((co->co_flags & CO_VARARGS) || |
| 3506 | defcount) ? "at least" |
| 3507 | : "exactly", |
| 3508 | m, m == 1 ? "" : "s", given); |
| 3509 | goto fail; |
| 3510 | } |
| 3511 | } |
| 3512 | if (n > m) |
| 3513 | i = n - m; |
| 3514 | else |
| 3515 | i = 0; |
| 3516 | for (; i < defcount; i++) { |
| 3517 | if (GETLOCAL(m+i) == NULL) { |
| 3518 | PyObject *def = defs[i]; |
| 3519 | Py_INCREF(def); |
| 3520 | SETLOCAL(m+i, def); |
| 3521 | } |
| 3522 | } |
| 3523 | } |
| 3524 | } |
| 3525 | else if (argcount > 0 || kwcount > 0) { |
| 3526 | PyErr_Format(PyExc_TypeError, |
| 3527 | "%.200s() takes no arguments (%d given)", |
| 3528 | PyString_AsString(co->co_name), |
| 3529 | argcount + kwcount); |
| 3530 | goto fail; |
| 3531 | } |
| 3532 | /* Allocate and initialize storage for cell vars, and copy free |
| 3533 | vars into frame. This isn't too efficient right now. */ |
| 3534 | if (PyTuple_GET_SIZE(co->co_cellvars)) { |
| 3535 | int i, j, nargs, found; |
| 3536 | char *cellname, *argname; |
| 3537 | PyObject *c; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3538 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3539 | nargs = co->co_argcount; |
| 3540 | if (co->co_flags & CO_VARARGS) |
| 3541 | nargs++; |
| 3542 | if (co->co_flags & CO_VARKEYWORDS) |
| 3543 | nargs++; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3544 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3545 | /* Initialize each cell var, taking into account |
| 3546 | cell vars that are initialized from arguments. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3547 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3548 | Should arrange for the compiler to put cellvars |
| 3549 | that are arguments at the beginning of the cellvars |
| 3550 | list so that we can march over it more efficiently? |
| 3551 | */ |
| 3552 | for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) { |
| 3553 | cellname = PyString_AS_STRING( |
| 3554 | PyTuple_GET_ITEM(co->co_cellvars, i)); |
| 3555 | found = 0; |
| 3556 | for (j = 0; j < nargs; j++) { |
| 3557 | argname = PyString_AS_STRING( |
| 3558 | PyTuple_GET_ITEM(co->co_varnames, j)); |
| 3559 | if (strcmp(cellname, argname) == 0) { |
| 3560 | c = PyCell_New(GETLOCAL(j)); |
| 3561 | if (c == NULL) |
| 3562 | goto fail; |
| 3563 | GETLOCAL(co->co_nlocals + i) = c; |
| 3564 | found = 1; |
| 3565 | break; |
| 3566 | } |
| 3567 | } |
| 3568 | if (found == 0) { |
| 3569 | c = PyCell_New(NULL); |
| 3570 | if (c == NULL) |
| 3571 | goto fail; |
| 3572 | SETLOCAL(co->co_nlocals + i, c); |
| 3573 | } |
| 3574 | } |
| 3575 | } |
| 3576 | if (PyTuple_GET_SIZE(co->co_freevars)) { |
| 3577 | int i; |
| 3578 | for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) { |
| 3579 | PyObject *o = PyTuple_GET_ITEM(closure, i); |
| 3580 | Py_INCREF(o); |
| 3581 | freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o; |
| 3582 | } |
| 3583 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3584 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3585 | if (co->co_flags & CO_GENERATOR) { |
| 3586 | /* Don't need to keep the reference to f_back, it will be set |
| 3587 | * when the generator is resumed. */ |
Serhiy Storchaka | 98a9722 | 2014-02-09 13:14:04 +0200 | [diff] [blame] | 3588 | Py_CLEAR(f->f_back); |
Neil Schemenauer | 2b13ce8 | 2001-06-21 02:41:10 +0000 | [diff] [blame] | 3589 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3590 | PCALL(PCALL_GENERATOR); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 3591 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3592 | /* Create a new generator that owns the ready to run frame |
| 3593 | * and return that as the value. */ |
| 3594 | return PyGen_New(f); |
| 3595 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3596 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3597 | retval = PyEval_EvalFrameEx(f,0); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3598 | |
Thomas Wouters | ae406c6 | 2007-09-19 17:27:43 +0000 | [diff] [blame] | 3599 | fail: /* Jump here from prelude on failure */ |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3600 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3601 | /* decref'ing the frame can cause __del__ methods to get invoked, |
| 3602 | which can call back into Python. While we're done with the |
| 3603 | current Python frame (f), the associated C stack is still in use, |
| 3604 | so recursion_depth must be boosted for the duration. |
| 3605 | */ |
| 3606 | assert(tstate != NULL); |
| 3607 | ++tstate->recursion_depth; |
| 3608 | Py_DECREF(f); |
| 3609 | --tstate->recursion_depth; |
| 3610 | return retval; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3611 | } |
| 3612 | |
| 3613 | |
Benjamin Peterson | 1880d8b | 2009-05-25 13:13:44 +0000 | [diff] [blame] | 3614 | static PyObject * |
| 3615 | special_lookup(PyObject *o, char *meth, PyObject **cache) |
| 3616 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3617 | PyObject *res; |
| 3618 | if (PyInstance_Check(o)) { |
| 3619 | if (!*cache) |
| 3620 | return PyObject_GetAttrString(o, meth); |
| 3621 | else |
| 3622 | return PyObject_GetAttr(o, *cache); |
| 3623 | } |
| 3624 | res = _PyObject_LookupSpecial(o, meth, cache); |
| 3625 | if (res == NULL && !PyErr_Occurred()) { |
| 3626 | PyErr_SetObject(PyExc_AttributeError, *cache); |
| 3627 | return NULL; |
| 3628 | } |
| 3629 | return res; |
Benjamin Peterson | 1880d8b | 2009-05-25 13:13:44 +0000 | [diff] [blame] | 3630 | } |
| 3631 | |
Benjamin Peterson | 78821dd | 2009-01-25 17:15:10 +0000 | [diff] [blame] | 3632 | |
Benjamin Peterson | e18ef19 | 2009-01-20 14:21:16 +0000 | [diff] [blame] | 3633 | static PyObject * |
| 3634 | kwd_as_string(PyObject *kwd) { |
Benjamin Peterson | 78821dd | 2009-01-25 17:15:10 +0000 | [diff] [blame] | 3635 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3636 | if (PyString_Check(kwd)) { |
Benjamin Peterson | 78821dd | 2009-01-25 17:15:10 +0000 | [diff] [blame] | 3637 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3638 | assert(PyString_Check(kwd)); |
Benjamin Peterson | 78821dd | 2009-01-25 17:15:10 +0000 | [diff] [blame] | 3639 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3640 | Py_INCREF(kwd); |
| 3641 | return 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 | } |
| 3644 | return _PyUnicode_AsDefaultEncodedString(kwd, "replace"); |
Benjamin Peterson | 78821dd | 2009-01-25 17:15:10 +0000 | [diff] [blame] | 3645 | #endif |
Benjamin Peterson | e18ef19 | 2009-01-20 14:21:16 +0000 | [diff] [blame] | 3646 | } |
| 3647 | |
| 3648 | |
Guido van Rossum | c9fbb72 | 2003-03-01 03:36:33 +0000 | [diff] [blame] | 3649 | /* Implementation notes for set_exc_info() and reset_exc_info(): |
| 3650 | |
| 3651 | - Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and |
| 3652 | 'exc_traceback'. These always travel together. |
| 3653 | |
| 3654 | - tstate->curexc_ZZZ is the "hot" exception that is set by |
| 3655 | PyErr_SetString(), cleared by PyErr_Clear(), and so on. |
| 3656 | |
| 3657 | - Once an exception is caught by an except clause, it is transferred |
| 3658 | from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info() |
| 3659 | 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] | 3660 | 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] | 3661 | |
| 3662 | - Now let me explain the complicated dance with frame->f_exc_ZZZ. |
| 3663 | |
| 3664 | Long ago, when none of this existed, there were just a few globals: |
| 3665 | one set corresponding to the "hot" exception, and one set |
| 3666 | corresponding to sys.exc_ZZZ. (Actually, the latter weren't C |
| 3667 | globals; they were simply stored as sys.exc_ZZZ. For backwards |
| 3668 | compatibility, they still are!) The problem was that in code like |
| 3669 | this: |
| 3670 | |
| 3671 | try: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3672 | "something that may fail" |
Guido van Rossum | c9fbb72 | 2003-03-01 03:36:33 +0000 | [diff] [blame] | 3673 | except "some exception": |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3674 | "do something else first" |
| 3675 | "print the exception from sys.exc_ZZZ." |
Guido van Rossum | c9fbb72 | 2003-03-01 03:36:33 +0000 | [diff] [blame] | 3676 | |
| 3677 | if "do something else first" invoked something that raised and caught |
| 3678 | an exception, sys.exc_ZZZ were overwritten. That was a frequent |
| 3679 | cause of subtle bugs. I fixed this by changing the semantics as |
| 3680 | follows: |
| 3681 | |
| 3682 | - Within one frame, sys.exc_ZZZ will hold the last exception caught |
| 3683 | *in that frame*. |
| 3684 | |
| 3685 | - But initially, and as long as no exception is caught in a given |
| 3686 | frame, sys.exc_ZZZ will hold the last exception caught in the |
| 3687 | previous frame (or the frame before that, etc.). |
| 3688 | |
| 3689 | The first bullet fixed the bug in the above example. The second |
| 3690 | bullet was for backwards compatibility: it was (and is) common to |
| 3691 | have a function that is called when an exception is caught, and to |
| 3692 | have that function access the caught exception via sys.exc_ZZZ. |
| 3693 | (Example: traceback.print_exc()). |
| 3694 | |
| 3695 | At the same time I fixed the problem that sys.exc_ZZZ weren't |
| 3696 | thread-safe, by introducing sys.exc_info() which gets it from tstate; |
| 3697 | but that's really a separate improvement. |
| 3698 | |
| 3699 | The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ |
| 3700 | variables to what they were before the current frame was called. The |
| 3701 | set_exc_info() function saves them on the frame so that |
| 3702 | reset_exc_info() can restore them. The invariant is that |
| 3703 | frame->f_exc_ZZZ is NULL iff the current frame never caught an |
| 3704 | exception (where "catching" an exception applies only to successful |
| 3705 | except clauses); and if the current frame ever caught an exception, |
| 3706 | frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ |
| 3707 | at the start of the current frame. |
| 3708 | |
| 3709 | */ |
| 3710 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 3711 | static void |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3712 | set_exc_info(PyThreadState *tstate, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3713 | PyObject *type, PyObject *value, PyObject *tb) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3714 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3715 | PyFrameObject *frame = tstate->frame; |
| 3716 | PyObject *tmp_type, *tmp_value, *tmp_tb; |
Barry Warsaw | 4249f54 | 1997-08-22 21:26:19 +0000 | [diff] [blame] | 3717 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3718 | assert(type != NULL); |
| 3719 | assert(frame != NULL); |
| 3720 | if (frame->f_exc_type == NULL) { |
| 3721 | assert(frame->f_exc_value == NULL); |
| 3722 | assert(frame->f_exc_traceback == NULL); |
| 3723 | /* This frame didn't catch an exception before. */ |
| 3724 | /* Save previous exception of this thread in this frame. */ |
| 3725 | if (tstate->exc_type == NULL) { |
| 3726 | /* XXX Why is this set to Py_None? */ |
| 3727 | Py_INCREF(Py_None); |
| 3728 | tstate->exc_type = Py_None; |
| 3729 | } |
| 3730 | Py_INCREF(tstate->exc_type); |
| 3731 | Py_XINCREF(tstate->exc_value); |
| 3732 | Py_XINCREF(tstate->exc_traceback); |
| 3733 | frame->f_exc_type = tstate->exc_type; |
| 3734 | frame->f_exc_value = tstate->exc_value; |
| 3735 | frame->f_exc_traceback = tstate->exc_traceback; |
| 3736 | } |
| 3737 | /* Set new exception for this thread. */ |
| 3738 | tmp_type = tstate->exc_type; |
| 3739 | tmp_value = tstate->exc_value; |
| 3740 | tmp_tb = tstate->exc_traceback; |
| 3741 | Py_INCREF(type); |
| 3742 | Py_XINCREF(value); |
| 3743 | Py_XINCREF(tb); |
| 3744 | tstate->exc_type = type; |
| 3745 | tstate->exc_value = value; |
| 3746 | tstate->exc_traceback = tb; |
| 3747 | Py_XDECREF(tmp_type); |
| 3748 | Py_XDECREF(tmp_value); |
| 3749 | Py_XDECREF(tmp_tb); |
| 3750 | /* For b/w compatibility */ |
| 3751 | PySys_SetObject("exc_type", type); |
| 3752 | PySys_SetObject("exc_value", value); |
| 3753 | PySys_SetObject("exc_traceback", tb); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3754 | } |
| 3755 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 3756 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3757 | reset_exc_info(PyThreadState *tstate) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3758 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3759 | PyFrameObject *frame; |
| 3760 | PyObject *tmp_type, *tmp_value, *tmp_tb; |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 3761 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3762 | /* It's a precondition that the thread state's frame caught an |
| 3763 | * exception -- verify in a debug build. |
| 3764 | */ |
| 3765 | assert(tstate != NULL); |
| 3766 | frame = tstate->frame; |
| 3767 | assert(frame != NULL); |
| 3768 | assert(frame->f_exc_type != NULL); |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 3769 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3770 | /* Copy the frame's exception info back to the thread state. */ |
| 3771 | tmp_type = tstate->exc_type; |
| 3772 | tmp_value = tstate->exc_value; |
| 3773 | tmp_tb = tstate->exc_traceback; |
| 3774 | Py_INCREF(frame->f_exc_type); |
| 3775 | Py_XINCREF(frame->f_exc_value); |
| 3776 | Py_XINCREF(frame->f_exc_traceback); |
| 3777 | tstate->exc_type = frame->f_exc_type; |
| 3778 | tstate->exc_value = frame->f_exc_value; |
| 3779 | tstate->exc_traceback = frame->f_exc_traceback; |
| 3780 | Py_XDECREF(tmp_type); |
| 3781 | Py_XDECREF(tmp_value); |
| 3782 | Py_XDECREF(tmp_tb); |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 3783 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3784 | /* For b/w compatibility */ |
| 3785 | PySys_SetObject("exc_type", frame->f_exc_type); |
| 3786 | PySys_SetObject("exc_value", frame->f_exc_value); |
| 3787 | PySys_SetObject("exc_traceback", frame->f_exc_traceback); |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 3788 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3789 | /* Clear the frame's exception info. */ |
| 3790 | tmp_type = frame->f_exc_type; |
| 3791 | tmp_value = frame->f_exc_value; |
| 3792 | tmp_tb = frame->f_exc_traceback; |
| 3793 | frame->f_exc_type = NULL; |
| 3794 | frame->f_exc_value = NULL; |
| 3795 | frame->f_exc_traceback = NULL; |
| 3796 | Py_DECREF(tmp_type); |
| 3797 | Py_XDECREF(tmp_value); |
| 3798 | Py_XDECREF(tmp_tb); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3799 | } |
| 3800 | |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3801 | /* Logic for the raise statement (too complicated for inlining). |
| 3802 | This *consumes* a reference count to each of its arguments. */ |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 3803 | static enum why_code |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3804 | do_raise(PyObject *type, PyObject *value, PyObject *tb) |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3805 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3806 | if (type == NULL) { |
| 3807 | /* Reraise */ |
| 3808 | PyThreadState *tstate = PyThreadState_GET(); |
| 3809 | type = tstate->exc_type == NULL ? Py_None : tstate->exc_type; |
| 3810 | value = tstate->exc_value; |
| 3811 | tb = tstate->exc_traceback; |
| 3812 | Py_XINCREF(type); |
| 3813 | Py_XINCREF(value); |
| 3814 | Py_XINCREF(tb); |
| 3815 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3816 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3817 | /* We support the following forms of raise: |
| 3818 | raise <class>, <classinstance> |
| 3819 | raise <class>, <argument tuple> |
| 3820 | raise <class>, None |
| 3821 | raise <class>, <argument> |
| 3822 | raise <classinstance>, None |
| 3823 | raise <string>, <object> |
| 3824 | raise <string>, None |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3825 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3826 | An omitted second argument is the same as None. |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3827 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3828 | In addition, raise <tuple>, <anything> is the same as |
| 3829 | raising the tuple's first item (and it better have one!); |
| 3830 | this rule is applied recursively. |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3831 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3832 | Finally, an optional third argument can be supplied, which |
| 3833 | gives the traceback to be substituted (useful when |
| 3834 | re-raising an exception after examining it). */ |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3835 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3836 | /* First, check the traceback argument, replacing None with |
| 3837 | NULL. */ |
| 3838 | if (tb == Py_None) { |
| 3839 | Py_DECREF(tb); |
| 3840 | tb = NULL; |
| 3841 | } |
| 3842 | else if (tb != NULL && !PyTraceBack_Check(tb)) { |
| 3843 | PyErr_SetString(PyExc_TypeError, |
| 3844 | "raise: arg 3 must be a traceback or None"); |
| 3845 | goto raise_error; |
| 3846 | } |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3847 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3848 | /* Next, replace a missing value with None */ |
| 3849 | if (value == NULL) { |
| 3850 | value = Py_None; |
| 3851 | Py_INCREF(value); |
| 3852 | } |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3853 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3854 | /* Next, repeatedly, replace a tuple exception with its first item */ |
| 3855 | while (PyTuple_Check(type) && PyTuple_Size(type) > 0) { |
| 3856 | PyObject *tmp = type; |
| 3857 | type = PyTuple_GET_ITEM(type, 0); |
| 3858 | Py_INCREF(type); |
| 3859 | Py_DECREF(tmp); |
| 3860 | } |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3861 | |
Benjamin Peterson | c3349cd | 2011-07-15 14:15:40 -0500 | [diff] [blame] | 3862 | if (PyExceptionClass_Check(type)) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3863 | PyErr_NormalizeException(&type, &value, &tb); |
Benjamin Peterson | c3349cd | 2011-07-15 14:15:40 -0500 | [diff] [blame] | 3864 | if (!PyExceptionInstance_Check(value)) { |
| 3865 | PyErr_Format(PyExc_TypeError, |
| 3866 | "calling %s() should have returned an instance of " |
| 3867 | "BaseException, not '%s'", |
| 3868 | ((PyTypeObject *)type)->tp_name, |
| 3869 | Py_TYPE(value)->tp_name); |
| 3870 | goto raise_error; |
| 3871 | } |
| 3872 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3873 | else if (PyExceptionInstance_Check(type)) { |
| 3874 | /* Raising an instance. The value should be a dummy. */ |
| 3875 | if (value != Py_None) { |
| 3876 | PyErr_SetString(PyExc_TypeError, |
| 3877 | "instance exception may not have a separate value"); |
| 3878 | goto raise_error; |
| 3879 | } |
| 3880 | else { |
| 3881 | /* Normalize to raise <class>, <instance> */ |
| 3882 | Py_DECREF(value); |
| 3883 | value = type; |
| 3884 | type = PyExceptionInstance_Class(type); |
| 3885 | Py_INCREF(type); |
| 3886 | } |
| 3887 | } |
| 3888 | else { |
| 3889 | /* Not something you can raise. You get an exception |
| 3890 | anyway, just not what you specified :-) */ |
| 3891 | PyErr_Format(PyExc_TypeError, |
| 3892 | "exceptions must be old-style classes or " |
| 3893 | "derived from BaseException, not %s", |
| 3894 | type->ob_type->tp_name); |
| 3895 | goto raise_error; |
| 3896 | } |
Guido van Rossum | 504153d | 2008-03-18 04:26:48 +0000 | [diff] [blame] | 3897 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3898 | assert(PyExceptionClass_Check(type)); |
| 3899 | if (Py_Py3kWarningFlag && PyClass_Check(type)) { |
| 3900 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
| 3901 | "exceptions must derive from BaseException " |
| 3902 | "in 3.x", 1) < 0) |
| 3903 | goto raise_error; |
| 3904 | } |
Guido van Rossum | 504153d | 2008-03-18 04:26:48 +0000 | [diff] [blame] | 3905 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3906 | PyErr_Restore(type, value, tb); |
| 3907 | if (tb == NULL) |
| 3908 | return WHY_EXCEPTION; |
| 3909 | else |
| 3910 | return WHY_RERAISE; |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3911 | raise_error: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3912 | Py_XDECREF(value); |
| 3913 | Py_XDECREF(type); |
| 3914 | Py_XDECREF(tb); |
| 3915 | return WHY_EXCEPTION; |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3916 | } |
| 3917 | |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 3918 | /* Iterate v argcnt times and store the results on the stack (via decreasing |
| 3919 | sp). Return 1 for success, 0 if error. */ |
| 3920 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 3921 | static int |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 3922 | unpack_iterable(PyObject *v, int argcnt, PyObject **sp) |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 3923 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3924 | int i = 0; |
| 3925 | PyObject *it; /* iter(v) */ |
| 3926 | PyObject *w; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3927 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3928 | assert(v != NULL); |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 3929 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3930 | it = PyObject_GetIter(v); |
| 3931 | if (it == NULL) |
| 3932 | goto Error; |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 3933 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3934 | for (; i < argcnt; i++) { |
| 3935 | w = PyIter_Next(it); |
| 3936 | if (w == NULL) { |
| 3937 | /* Iterator done, via error or exhaustion. */ |
| 3938 | if (!PyErr_Occurred()) { |
| 3939 | PyErr_Format(PyExc_ValueError, |
| 3940 | "need more than %d value%s to unpack", |
| 3941 | i, i == 1 ? "" : "s"); |
| 3942 | } |
| 3943 | goto Error; |
| 3944 | } |
| 3945 | *--sp = w; |
| 3946 | } |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 3947 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3948 | /* We better have exhausted the iterator now. */ |
| 3949 | w = PyIter_Next(it); |
| 3950 | if (w == NULL) { |
| 3951 | if (PyErr_Occurred()) |
| 3952 | goto Error; |
| 3953 | Py_DECREF(it); |
| 3954 | return 1; |
| 3955 | } |
| 3956 | Py_DECREF(w); |
| 3957 | PyErr_SetString(PyExc_ValueError, "too many values to unpack"); |
| 3958 | /* fall through */ |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 3959 | Error: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3960 | for (; i > 0; i--, sp++) |
| 3961 | Py_DECREF(*sp); |
| 3962 | Py_XDECREF(it); |
| 3963 | return 0; |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 3964 | } |
| 3965 | |
| 3966 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 3967 | #ifdef LLTRACE |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 3968 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3969 | prtrace(PyObject *v, char *str) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3970 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3971 | printf("%s ", str); |
| 3972 | if (PyObject_Print(v, stdout, 0) != 0) |
| 3973 | PyErr_Clear(); /* Don't know what else to do */ |
| 3974 | printf("\n"); |
| 3975 | return 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3976 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3977 | #endif |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3978 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 3979 | static void |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 3980 | call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f) |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 3981 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3982 | PyObject *type, *value, *traceback, *arg; |
| 3983 | int err; |
| 3984 | PyErr_Fetch(&type, &value, &traceback); |
| 3985 | if (value == NULL) { |
| 3986 | value = Py_None; |
| 3987 | Py_INCREF(value); |
| 3988 | } |
| 3989 | arg = PyTuple_Pack(3, type, value, traceback); |
| 3990 | if (arg == NULL) { |
| 3991 | PyErr_Restore(type, value, traceback); |
| 3992 | return; |
| 3993 | } |
| 3994 | err = call_trace(func, self, f, PyTrace_EXCEPTION, arg); |
| 3995 | Py_DECREF(arg); |
| 3996 | if (err == 0) |
| 3997 | PyErr_Restore(type, value, traceback); |
| 3998 | else { |
| 3999 | Py_XDECREF(type); |
| 4000 | Py_XDECREF(value); |
| 4001 | Py_XDECREF(traceback); |
| 4002 | } |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4003 | } |
| 4004 | |
Amaury Forgeot d'Arc | 0d75f09 | 2007-11-13 21:54:28 +0000 | [diff] [blame] | 4005 | static int |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 4006 | call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4007 | int what, PyObject *arg) |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 4008 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4009 | PyObject *type, *value, *traceback; |
| 4010 | int err; |
| 4011 | PyErr_Fetch(&type, &value, &traceback); |
| 4012 | err = call_trace(func, obj, frame, what, arg); |
| 4013 | if (err == 0) |
| 4014 | { |
| 4015 | PyErr_Restore(type, value, traceback); |
| 4016 | return 0; |
| 4017 | } |
| 4018 | else { |
| 4019 | Py_XDECREF(type); |
| 4020 | Py_XDECREF(value); |
| 4021 | Py_XDECREF(traceback); |
| 4022 | return -1; |
| 4023 | } |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 4024 | } |
| 4025 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4026 | static int |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 4027 | call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4028 | int what, PyObject *arg) |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 4029 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4030 | register PyThreadState *tstate = frame->f_tstate; |
| 4031 | int result; |
| 4032 | if (tstate->tracing) |
| 4033 | return 0; |
| 4034 | tstate->tracing++; |
| 4035 | tstate->use_tracing = 0; |
| 4036 | result = func(obj, frame, what, arg); |
| 4037 | tstate->use_tracing = ((tstate->c_tracefunc != NULL) |
| 4038 | || (tstate->c_profilefunc != NULL)); |
| 4039 | tstate->tracing--; |
| 4040 | return result; |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 4041 | } |
| 4042 | |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 4043 | PyObject * |
| 4044 | _PyEval_CallTracing(PyObject *func, PyObject *args) |
| 4045 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4046 | PyFrameObject *frame = PyEval_GetFrame(); |
| 4047 | PyThreadState *tstate = frame->f_tstate; |
| 4048 | int save_tracing = tstate->tracing; |
| 4049 | int save_use_tracing = tstate->use_tracing; |
| 4050 | PyObject *result; |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 4051 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4052 | tstate->tracing = 0; |
| 4053 | tstate->use_tracing = ((tstate->c_tracefunc != NULL) |
| 4054 | || (tstate->c_profilefunc != NULL)); |
| 4055 | result = PyObject_Call(func, args, NULL); |
| 4056 | tstate->tracing = save_tracing; |
| 4057 | tstate->use_tracing = save_use_tracing; |
| 4058 | return result; |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 4059 | } |
| 4060 | |
Jeffrey Yasskin | 655d835 | 2009-05-23 23:23:01 +0000 | [diff] [blame] | 4061 | /* See Objects/lnotab_notes.txt for a description of how tracing works. */ |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4062 | static int |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 4063 | maybe_call_line_trace(Py_tracefunc func, PyObject *obj, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4064 | PyFrameObject *frame, int *instr_lb, int *instr_ub, |
| 4065 | int *instr_prev) |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 4066 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4067 | int result = 0; |
| 4068 | int line = frame->f_lineno; |
Michael W. Hudson | 006c752 | 2002-11-08 13:08:46 +0000 | [diff] [blame] | 4069 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4070 | /* If the last instruction executed isn't in the current |
| 4071 | instruction window, reset the window. |
| 4072 | */ |
| 4073 | if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) { |
| 4074 | PyAddrPair bounds; |
| 4075 | line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti, |
| 4076 | &bounds); |
| 4077 | *instr_lb = bounds.ap_lower; |
| 4078 | *instr_ub = bounds.ap_upper; |
| 4079 | } |
| 4080 | /* If the last instruction falls at the start of a line or if |
| 4081 | it represents a jump backwards, update the frame's line |
| 4082 | number and call the trace function. */ |
| 4083 | if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) { |
| 4084 | frame->f_lineno = line; |
| 4085 | result = call_trace(func, obj, frame, PyTrace_LINE, Py_None); |
| 4086 | } |
| 4087 | *instr_prev = frame->f_lasti; |
| 4088 | return result; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 4089 | } |
| 4090 | |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 4091 | void |
| 4092 | PyEval_SetProfile(Py_tracefunc func, PyObject *arg) |
Fred Drake | d083839 | 2001-06-16 21:02:31 +0000 | [diff] [blame] | 4093 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4094 | PyThreadState *tstate = PyThreadState_GET(); |
| 4095 | PyObject *temp = tstate->c_profileobj; |
| 4096 | Py_XINCREF(arg); |
| 4097 | tstate->c_profilefunc = NULL; |
| 4098 | tstate->c_profileobj = NULL; |
| 4099 | /* Must make sure that tracing is not ignored if 'temp' is freed */ |
| 4100 | tstate->use_tracing = tstate->c_tracefunc != NULL; |
| 4101 | Py_XDECREF(temp); |
| 4102 | tstate->c_profilefunc = func; |
| 4103 | tstate->c_profileobj = arg; |
| 4104 | /* Flag that tracing or profiling is turned on */ |
| 4105 | tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL); |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 4106 | } |
| 4107 | |
| 4108 | void |
| 4109 | PyEval_SetTrace(Py_tracefunc func, PyObject *arg) |
| 4110 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4111 | PyThreadState *tstate = PyThreadState_GET(); |
| 4112 | PyObject *temp = tstate->c_traceobj; |
| 4113 | _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL); |
| 4114 | Py_XINCREF(arg); |
| 4115 | tstate->c_tracefunc = NULL; |
| 4116 | tstate->c_traceobj = NULL; |
| 4117 | /* Must make sure that profiling is not ignored if 'temp' is freed */ |
| 4118 | tstate->use_tracing = tstate->c_profilefunc != NULL; |
| 4119 | Py_XDECREF(temp); |
| 4120 | tstate->c_tracefunc = func; |
| 4121 | tstate->c_traceobj = arg; |
| 4122 | /* Flag that tracing or profiling is turned on */ |
| 4123 | tstate->use_tracing = ((func != NULL) |
| 4124 | || (tstate->c_profilefunc != NULL)); |
Fred Drake | d083839 | 2001-06-16 21:02:31 +0000 | [diff] [blame] | 4125 | } |
| 4126 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4127 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4128 | PyEval_GetBuiltins(void) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 4129 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4130 | PyFrameObject *current_frame = PyEval_GetFrame(); |
| 4131 | if (current_frame == NULL) |
| 4132 | return PyThreadState_GET()->interp->builtins; |
| 4133 | else |
| 4134 | return current_frame->f_builtins; |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 4135 | } |
| 4136 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4137 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4138 | PyEval_GetLocals(void) |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 4139 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4140 | PyFrameObject *current_frame = PyEval_GetFrame(); |
| 4141 | if (current_frame == NULL) |
| 4142 | return NULL; |
| 4143 | PyFrame_FastToLocals(current_frame); |
| 4144 | return current_frame->f_locals; |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 4145 | } |
| 4146 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4147 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4148 | PyEval_GetGlobals(void) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4149 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4150 | PyFrameObject *current_frame = PyEval_GetFrame(); |
| 4151 | if (current_frame == NULL) |
| 4152 | return NULL; |
| 4153 | else |
| 4154 | return current_frame->f_globals; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4155 | } |
| 4156 | |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 4157 | PyFrameObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4158 | PyEval_GetFrame(void) |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 4159 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4160 | PyThreadState *tstate = PyThreadState_GET(); |
| 4161 | return _PyThreadState_GetFrame(tstate); |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 4162 | } |
| 4163 | |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 4164 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4165 | PyEval_GetRestricted(void) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 4166 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4167 | PyFrameObject *current_frame = PyEval_GetFrame(); |
| 4168 | return current_frame == NULL ? 0 : PyFrame_IsRestricted(current_frame); |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 4169 | } |
| 4170 | |
Guido van Rossum | be27026 | 1997-05-22 22:26:18 +0000 | [diff] [blame] | 4171 | int |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 4172 | PyEval_MergeCompilerFlags(PyCompilerFlags *cf) |
Jeremy Hylton | 061d106 | 2001-03-22 02:32:48 +0000 | [diff] [blame] | 4173 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4174 | PyFrameObject *current_frame = PyEval_GetFrame(); |
| 4175 | int result = cf->cf_flags != 0; |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 4176 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4177 | if (current_frame != NULL) { |
| 4178 | const int codeflags = current_frame->f_code->co_flags; |
| 4179 | const int compilerflags = codeflags & PyCF_MASK; |
| 4180 | if (compilerflags) { |
| 4181 | result = 1; |
| 4182 | cf->cf_flags |= compilerflags; |
| 4183 | } |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 4184 | #if 0 /* future keyword */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4185 | if (codeflags & CO_GENERATOR_ALLOWED) { |
| 4186 | result = 1; |
| 4187 | cf->cf_flags |= CO_GENERATOR_ALLOWED; |
| 4188 | } |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 4189 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4190 | } |
| 4191 | return result; |
Jeremy Hylton | 061d106 | 2001-03-22 02:32:48 +0000 | [diff] [blame] | 4192 | } |
| 4193 | |
| 4194 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4195 | Py_FlushLine(void) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4196 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4197 | PyObject *f = PySys_GetObject("stdout"); |
| 4198 | if (f == NULL) |
| 4199 | return 0; |
| 4200 | if (!PyFile_SoftSpace(f, 0)) |
| 4201 | return 0; |
| 4202 | return PyFile_WriteString("\n", f); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4203 | } |
| 4204 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4205 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4206 | /* External interface to call any callable object. |
Antoine Pitrou | 76c8649 | 2010-04-01 16:42:11 +0000 | [diff] [blame] | 4207 | 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] | 4208 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4209 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4210 | PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4211 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4212 | PyObject *result; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4213 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4214 | if (arg == NULL) { |
| 4215 | arg = PyTuple_New(0); |
| 4216 | if (arg == NULL) |
| 4217 | return NULL; |
| 4218 | } |
| 4219 | else if (!PyTuple_Check(arg)) { |
| 4220 | PyErr_SetString(PyExc_TypeError, |
| 4221 | "argument list must be a tuple"); |
| 4222 | return NULL; |
| 4223 | } |
| 4224 | else |
| 4225 | Py_INCREF(arg); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4226 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4227 | if (kw != NULL && !PyDict_Check(kw)) { |
| 4228 | PyErr_SetString(PyExc_TypeError, |
| 4229 | "keyword list must be a dictionary"); |
| 4230 | Py_DECREF(arg); |
| 4231 | return NULL; |
| 4232 | } |
Guido van Rossum | e3e61c1 | 1995-08-04 04:14:47 +0000 | [diff] [blame] | 4233 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4234 | result = PyObject_Call(func, arg, kw); |
| 4235 | Py_DECREF(arg); |
| 4236 | return result; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4237 | } |
| 4238 | |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 4239 | const char * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4240 | PyEval_GetFuncName(PyObject *func) |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4241 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4242 | if (PyMethod_Check(func)) |
| 4243 | return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func)); |
| 4244 | else if (PyFunction_Check(func)) |
| 4245 | return PyString_AsString(((PyFunctionObject*)func)->func_name); |
| 4246 | else if (PyCFunction_Check(func)) |
| 4247 | return ((PyCFunctionObject*)func)->m_ml->ml_name; |
| 4248 | else if (PyClass_Check(func)) |
| 4249 | return PyString_AsString(((PyClassObject*)func)->cl_name); |
| 4250 | else if (PyInstance_Check(func)) { |
| 4251 | return PyString_AsString( |
| 4252 | ((PyInstanceObject*)func)->in_class->cl_name); |
| 4253 | } else { |
| 4254 | return func->ob_type->tp_name; |
| 4255 | } |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4256 | } |
| 4257 | |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 4258 | const char * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4259 | PyEval_GetFuncDesc(PyObject *func) |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4260 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4261 | if (PyMethod_Check(func)) |
| 4262 | return "()"; |
| 4263 | else if (PyFunction_Check(func)) |
| 4264 | return "()"; |
| 4265 | else if (PyCFunction_Check(func)) |
| 4266 | return "()"; |
| 4267 | else if (PyClass_Check(func)) |
| 4268 | return " constructor"; |
| 4269 | else if (PyInstance_Check(func)) { |
| 4270 | return " instance"; |
| 4271 | } else { |
| 4272 | return " object"; |
| 4273 | } |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4274 | } |
| 4275 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4276 | static void |
Jeremy Hylton | 192690e | 2002-08-16 18:36:11 +0000 | [diff] [blame] | 4277 | err_args(PyObject *func, int flags, int nargs) |
| 4278 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4279 | if (flags & METH_NOARGS) |
| 4280 | PyErr_Format(PyExc_TypeError, |
| 4281 | "%.200s() takes no arguments (%d given)", |
| 4282 | ((PyCFunctionObject *)func)->m_ml->ml_name, |
| 4283 | nargs); |
| 4284 | else |
| 4285 | PyErr_Format(PyExc_TypeError, |
| 4286 | "%.200s() takes exactly one argument (%d given)", |
| 4287 | ((PyCFunctionObject *)func)->m_ml->ml_name, |
| 4288 | nargs); |
Jeremy Hylton | 192690e | 2002-08-16 18:36:11 +0000 | [diff] [blame] | 4289 | } |
| 4290 | |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 4291 | #define C_TRACE(x, call) \ |
Nicholas Bastin | d858a77 | 2004-06-25 23:31:06 +0000 | [diff] [blame] | 4292 | if (tstate->use_tracing && tstate->c_profilefunc) { \ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4293 | if (call_trace(tstate->c_profilefunc, \ |
| 4294 | tstate->c_profileobj, \ |
| 4295 | tstate->frame, PyTrace_C_CALL, \ |
| 4296 | func)) { \ |
| 4297 | x = NULL; \ |
| 4298 | } \ |
| 4299 | else { \ |
| 4300 | x = call; \ |
| 4301 | if (tstate->c_profilefunc != NULL) { \ |
| 4302 | if (x == NULL) { \ |
| 4303 | call_trace_protected(tstate->c_profilefunc, \ |
| 4304 | tstate->c_profileobj, \ |
| 4305 | tstate->frame, PyTrace_C_EXCEPTION, \ |
| 4306 | func); \ |
| 4307 | /* XXX should pass (type, value, tb) */ \ |
| 4308 | } else { \ |
| 4309 | if (call_trace(tstate->c_profilefunc, \ |
| 4310 | tstate->c_profileobj, \ |
| 4311 | tstate->frame, PyTrace_C_RETURN, \ |
| 4312 | func)) { \ |
| 4313 | Py_DECREF(x); \ |
| 4314 | x = NULL; \ |
| 4315 | } \ |
| 4316 | } \ |
| 4317 | } \ |
| 4318 | } \ |
Nicholas Bastin | d858a77 | 2004-06-25 23:31:06 +0000 | [diff] [blame] | 4319 | } else { \ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4320 | x = call; \ |
| 4321 | } |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 4322 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4323 | static PyObject * |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 4324 | call_function(PyObject ***pp_stack, int oparg |
| 4325 | #ifdef WITH_TSC |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4326 | , uint64* pintr0, uint64* pintr1 |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 4327 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4328 | ) |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 4329 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4330 | int na = oparg & 0xff; |
| 4331 | int nk = (oparg>>8) & 0xff; |
| 4332 | int n = na + 2 * nk; |
| 4333 | PyObject **pfunc = (*pp_stack) - n - 1; |
| 4334 | PyObject *func = *pfunc; |
| 4335 | PyObject *x, *w; |
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 | /* Always dispatch PyCFunction first, because these are |
| 4338 | presumed to be the most frequent callable object. |
| 4339 | */ |
| 4340 | if (PyCFunction_Check(func) && nk == 0) { |
| 4341 | int flags = PyCFunction_GET_FLAGS(func); |
| 4342 | PyThreadState *tstate = PyThreadState_GET(); |
Raymond Hettinger | a7f56bc | 2004-06-26 04:34:33 +0000 | [diff] [blame] | 4343 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4344 | PCALL(PCALL_CFUNCTION); |
| 4345 | if (flags & (METH_NOARGS | METH_O)) { |
| 4346 | PyCFunction meth = PyCFunction_GET_FUNCTION(func); |
| 4347 | PyObject *self = PyCFunction_GET_SELF(func); |
| 4348 | if (flags & METH_NOARGS && na == 0) { |
| 4349 | C_TRACE(x, (*meth)(self,NULL)); |
| 4350 | } |
| 4351 | else if (flags & METH_O && na == 1) { |
| 4352 | PyObject *arg = EXT_POP(*pp_stack); |
| 4353 | C_TRACE(x, (*meth)(self,arg)); |
| 4354 | Py_DECREF(arg); |
| 4355 | } |
| 4356 | else { |
| 4357 | err_args(func, flags, na); |
| 4358 | x = NULL; |
| 4359 | } |
| 4360 | } |
| 4361 | else { |
| 4362 | PyObject *callargs; |
| 4363 | callargs = load_args(pp_stack, na); |
| 4364 | READ_TIMESTAMP(*pintr0); |
| 4365 | C_TRACE(x, PyCFunction_Call(func,callargs,NULL)); |
| 4366 | READ_TIMESTAMP(*pintr1); |
| 4367 | Py_XDECREF(callargs); |
| 4368 | } |
| 4369 | } else { |
| 4370 | if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) { |
| 4371 | /* optimize access to bound methods */ |
| 4372 | PyObject *self = PyMethod_GET_SELF(func); |
| 4373 | PCALL(PCALL_METHOD); |
| 4374 | PCALL(PCALL_BOUND_METHOD); |
| 4375 | Py_INCREF(self); |
| 4376 | func = PyMethod_GET_FUNCTION(func); |
| 4377 | Py_INCREF(func); |
Serhiy Storchaka | 763a61c | 2016-04-10 18:05:12 +0300 | [diff] [blame] | 4378 | Py_SETREF(*pfunc, self); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4379 | na++; |
| 4380 | n++; |
| 4381 | } else |
| 4382 | Py_INCREF(func); |
| 4383 | READ_TIMESTAMP(*pintr0); |
| 4384 | if (PyFunction_Check(func)) |
| 4385 | x = fast_function(func, pp_stack, n, na, nk); |
| 4386 | else |
| 4387 | x = do_call(func, pp_stack, na, nk); |
| 4388 | READ_TIMESTAMP(*pintr1); |
| 4389 | Py_DECREF(func); |
| 4390 | } |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 4391 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4392 | /* Clear the stack of the function object. Also removes |
| 4393 | the arguments in case they weren't consumed already |
| 4394 | (fast_function() and err_args() leave them on the stack). |
| 4395 | */ |
| 4396 | while ((*pp_stack) > pfunc) { |
| 4397 | w = EXT_POP(*pp_stack); |
| 4398 | Py_DECREF(w); |
| 4399 | PCALL(PCALL_POP); |
| 4400 | } |
| 4401 | return x; |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 4402 | } |
| 4403 | |
Jeremy Hylton | 192690e | 2002-08-16 18:36:11 +0000 | [diff] [blame] | 4404 | /* The fast_function() function optimize calls for which no argument |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4405 | tuple is necessary; the objects are passed directly from the stack. |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4406 | For the simplest case -- a function that takes only positional |
| 4407 | arguments and is called with only positional arguments -- it |
| 4408 | inlines the most primitive frame setup code from |
| 4409 | PyEval_EvalCodeEx(), which vastly reduces the checks that must be |
| 4410 | done before evaluating the frame. |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4411 | */ |
| 4412 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4413 | static PyObject * |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4414 | 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] | 4415 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4416 | PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func); |
| 4417 | PyObject *globals = PyFunction_GET_GLOBALS(func); |
| 4418 | PyObject *argdefs = PyFunction_GET_DEFAULTS(func); |
| 4419 | PyObject **d = NULL; |
| 4420 | int nd = 0; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4421 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4422 | PCALL(PCALL_FUNCTION); |
| 4423 | PCALL(PCALL_FAST_FUNCTION); |
| 4424 | if (argdefs == NULL && co->co_argcount == n && nk==0 && |
| 4425 | co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) { |
| 4426 | PyFrameObject *f; |
| 4427 | PyObject *retval = NULL; |
| 4428 | PyThreadState *tstate = PyThreadState_GET(); |
| 4429 | PyObject **fastlocals, **stack; |
| 4430 | int i; |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4431 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4432 | PCALL(PCALL_FASTER_FUNCTION); |
| 4433 | assert(globals != NULL); |
| 4434 | /* XXX Perhaps we should create a specialized |
| 4435 | PyFrame_New() that doesn't take locals, but does |
| 4436 | take builtins without sanity checking them. |
| 4437 | */ |
| 4438 | assert(tstate != NULL); |
| 4439 | f = PyFrame_New(tstate, co, globals, NULL); |
| 4440 | if (f == NULL) |
| 4441 | return NULL; |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4442 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4443 | fastlocals = f->f_localsplus; |
| 4444 | stack = (*pp_stack) - n; |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4445 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4446 | for (i = 0; i < n; i++) { |
| 4447 | Py_INCREF(*stack); |
| 4448 | fastlocals[i] = *stack++; |
| 4449 | } |
| 4450 | retval = PyEval_EvalFrameEx(f,0); |
| 4451 | ++tstate->recursion_depth; |
| 4452 | Py_DECREF(f); |
| 4453 | --tstate->recursion_depth; |
| 4454 | return retval; |
| 4455 | } |
| 4456 | if (argdefs != NULL) { |
| 4457 | d = &PyTuple_GET_ITEM(argdefs, 0); |
| 4458 | nd = Py_SIZE(argdefs); |
| 4459 | } |
| 4460 | return PyEval_EvalCodeEx(co, globals, |
| 4461 | (PyObject *)NULL, (*pp_stack)-n, na, |
| 4462 | (*pp_stack)-2*nk, nk, d, nd, |
| 4463 | PyFunction_GET_CLOSURE(func)); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4464 | } |
| 4465 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4466 | static PyObject * |
Ka-Ping Yee | 2057970 | 2001-01-15 22:14:16 +0000 | [diff] [blame] | 4467 | update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack, |
| 4468 | PyObject *func) |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4469 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4470 | PyObject *kwdict = NULL; |
| 4471 | if (orig_kwdict == NULL) |
| 4472 | kwdict = PyDict_New(); |
| 4473 | else { |
| 4474 | kwdict = PyDict_Copy(orig_kwdict); |
| 4475 | Py_DECREF(orig_kwdict); |
| 4476 | } |
| 4477 | if (kwdict == NULL) |
| 4478 | return NULL; |
| 4479 | while (--nk >= 0) { |
| 4480 | int err; |
| 4481 | PyObject *value = EXT_POP(*pp_stack); |
| 4482 | PyObject *key = EXT_POP(*pp_stack); |
| 4483 | if (PyDict_GetItem(kwdict, key) != NULL) { |
| 4484 | PyErr_Format(PyExc_TypeError, |
| 4485 | "%.200s%s got multiple values " |
| 4486 | "for keyword argument '%.200s'", |
| 4487 | PyEval_GetFuncName(func), |
| 4488 | PyEval_GetFuncDesc(func), |
| 4489 | PyString_AsString(key)); |
| 4490 | Py_DECREF(key); |
| 4491 | Py_DECREF(value); |
| 4492 | Py_DECREF(kwdict); |
| 4493 | return NULL; |
| 4494 | } |
| 4495 | err = PyDict_SetItem(kwdict, key, value); |
| 4496 | Py_DECREF(key); |
| 4497 | Py_DECREF(value); |
| 4498 | if (err) { |
| 4499 | Py_DECREF(kwdict); |
| 4500 | return NULL; |
| 4501 | } |
| 4502 | } |
| 4503 | return kwdict; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4504 | } |
| 4505 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4506 | static PyObject * |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4507 | update_star_args(int nstack, int nstar, PyObject *stararg, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4508 | PyObject ***pp_stack) |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4509 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4510 | PyObject *callargs, *w; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4511 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4512 | callargs = PyTuple_New(nstack + nstar); |
| 4513 | if (callargs == NULL) { |
| 4514 | return NULL; |
| 4515 | } |
| 4516 | if (nstar) { |
| 4517 | int i; |
| 4518 | for (i = 0; i < nstar; i++) { |
| 4519 | PyObject *a = PyTuple_GET_ITEM(stararg, i); |
| 4520 | Py_INCREF(a); |
| 4521 | PyTuple_SET_ITEM(callargs, nstack + i, a); |
| 4522 | } |
| 4523 | } |
| 4524 | while (--nstack >= 0) { |
| 4525 | w = EXT_POP(*pp_stack); |
| 4526 | PyTuple_SET_ITEM(callargs, nstack, w); |
| 4527 | } |
| 4528 | return callargs; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4529 | } |
| 4530 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4531 | static PyObject * |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4532 | load_args(PyObject ***pp_stack, int na) |
| 4533 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4534 | PyObject *args = PyTuple_New(na); |
| 4535 | PyObject *w; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4536 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4537 | if (args == NULL) |
| 4538 | return NULL; |
| 4539 | while (--na >= 0) { |
| 4540 | w = EXT_POP(*pp_stack); |
| 4541 | PyTuple_SET_ITEM(args, na, w); |
| 4542 | } |
| 4543 | return args; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4544 | } |
| 4545 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4546 | static PyObject * |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4547 | do_call(PyObject *func, PyObject ***pp_stack, int na, int nk) |
| 4548 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4549 | PyObject *callargs = NULL; |
| 4550 | PyObject *kwdict = NULL; |
| 4551 | PyObject *result = NULL; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4552 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4553 | if (nk > 0) { |
| 4554 | kwdict = update_keyword_args(NULL, nk, pp_stack, func); |
| 4555 | if (kwdict == NULL) |
| 4556 | goto call_fail; |
| 4557 | } |
| 4558 | callargs = load_args(pp_stack, na); |
| 4559 | if (callargs == NULL) |
| 4560 | goto call_fail; |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4561 | #ifdef CALL_PROFILE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4562 | /* At this point, we have to look at the type of func to |
| 4563 | update the call stats properly. Do it here so as to avoid |
| 4564 | exposing the call stats machinery outside ceval.c |
| 4565 | */ |
| 4566 | if (PyFunction_Check(func)) |
| 4567 | PCALL(PCALL_FUNCTION); |
| 4568 | else if (PyMethod_Check(func)) |
| 4569 | PCALL(PCALL_METHOD); |
| 4570 | else if (PyType_Check(func)) |
| 4571 | PCALL(PCALL_TYPE); |
| 4572 | else if (PyCFunction_Check(func)) |
| 4573 | PCALL(PCALL_CFUNCTION); |
| 4574 | else |
| 4575 | PCALL(PCALL_OTHER); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4576 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4577 | if (PyCFunction_Check(func)) { |
| 4578 | PyThreadState *tstate = PyThreadState_GET(); |
| 4579 | C_TRACE(result, PyCFunction_Call(func, callargs, kwdict)); |
| 4580 | } |
| 4581 | else |
| 4582 | result = PyObject_Call(func, callargs, kwdict); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4583 | call_fail: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4584 | Py_XDECREF(callargs); |
| 4585 | Py_XDECREF(kwdict); |
| 4586 | return result; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4587 | } |
| 4588 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4589 | static PyObject * |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4590 | ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk) |
| 4591 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4592 | int nstar = 0; |
| 4593 | PyObject *callargs = NULL; |
| 4594 | PyObject *stararg = NULL; |
| 4595 | PyObject *kwdict = NULL; |
| 4596 | PyObject *result = NULL; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4597 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4598 | if (flags & CALL_FLAG_KW) { |
| 4599 | kwdict = EXT_POP(*pp_stack); |
| 4600 | if (!PyDict_Check(kwdict)) { |
| 4601 | PyObject *d; |
| 4602 | d = PyDict_New(); |
| 4603 | if (d == NULL) |
| 4604 | goto ext_call_fail; |
| 4605 | if (PyDict_Update(d, kwdict) != 0) { |
| 4606 | Py_DECREF(d); |
| 4607 | /* PyDict_Update raises attribute |
| 4608 | * error (percolated from an attempt |
| 4609 | * to get 'keys' attribute) instead of |
| 4610 | * a type error if its second argument |
| 4611 | * is not a mapping. |
| 4612 | */ |
| 4613 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 4614 | PyErr_Format(PyExc_TypeError, |
| 4615 | "%.200s%.200s argument after ** " |
| 4616 | "must be a mapping, not %.200s", |
| 4617 | PyEval_GetFuncName(func), |
| 4618 | PyEval_GetFuncDesc(func), |
| 4619 | kwdict->ob_type->tp_name); |
| 4620 | } |
| 4621 | goto ext_call_fail; |
| 4622 | } |
| 4623 | Py_DECREF(kwdict); |
| 4624 | kwdict = d; |
| 4625 | } |
| 4626 | } |
| 4627 | if (flags & CALL_FLAG_VAR) { |
| 4628 | stararg = EXT_POP(*pp_stack); |
| 4629 | if (!PyTuple_Check(stararg)) { |
| 4630 | PyObject *t = NULL; |
| 4631 | t = PySequence_Tuple(stararg); |
| 4632 | if (t == NULL) { |
Martin Panter | 0bb165e | 2016-01-31 06:30:56 +0000 | [diff] [blame] | 4633 | if (PyErr_ExceptionMatches(PyExc_TypeError) && |
| 4634 | /* Don't mask TypeError raised from a generator */ |
| 4635 | !PyGen_Check(stararg)) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4636 | PyErr_Format(PyExc_TypeError, |
| 4637 | "%.200s%.200s argument after * " |
Martin Panter | 0bb165e | 2016-01-31 06:30:56 +0000 | [diff] [blame] | 4638 | "must be an iterable, not %200s", |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4639 | PyEval_GetFuncName(func), |
| 4640 | PyEval_GetFuncDesc(func), |
| 4641 | stararg->ob_type->tp_name); |
| 4642 | } |
| 4643 | goto ext_call_fail; |
| 4644 | } |
| 4645 | Py_DECREF(stararg); |
| 4646 | stararg = t; |
| 4647 | } |
| 4648 | nstar = PyTuple_GET_SIZE(stararg); |
| 4649 | } |
| 4650 | if (nk > 0) { |
| 4651 | kwdict = update_keyword_args(kwdict, nk, pp_stack, func); |
| 4652 | if (kwdict == NULL) |
| 4653 | goto ext_call_fail; |
| 4654 | } |
| 4655 | callargs = update_star_args(na, nstar, stararg, pp_stack); |
| 4656 | if (callargs == NULL) |
| 4657 | goto ext_call_fail; |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4658 | #ifdef CALL_PROFILE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4659 | /* At this point, we have to look at the type of func to |
| 4660 | update the call stats properly. Do it here so as to avoid |
| 4661 | exposing the call stats machinery outside ceval.c |
| 4662 | */ |
| 4663 | if (PyFunction_Check(func)) |
| 4664 | PCALL(PCALL_FUNCTION); |
| 4665 | else if (PyMethod_Check(func)) |
| 4666 | PCALL(PCALL_METHOD); |
| 4667 | else if (PyType_Check(func)) |
| 4668 | PCALL(PCALL_TYPE); |
| 4669 | else if (PyCFunction_Check(func)) |
| 4670 | PCALL(PCALL_CFUNCTION); |
| 4671 | else |
| 4672 | PCALL(PCALL_OTHER); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4673 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4674 | if (PyCFunction_Check(func)) { |
| 4675 | PyThreadState *tstate = PyThreadState_GET(); |
| 4676 | C_TRACE(result, PyCFunction_Call(func, callargs, kwdict)); |
| 4677 | } |
| 4678 | else |
| 4679 | result = PyObject_Call(func, callargs, kwdict); |
Thomas Wouters | ae406c6 | 2007-09-19 17:27:43 +0000 | [diff] [blame] | 4680 | ext_call_fail: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4681 | Py_XDECREF(callargs); |
| 4682 | Py_XDECREF(kwdict); |
| 4683 | Py_XDECREF(stararg); |
| 4684 | return result; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4685 | } |
| 4686 | |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 4687 | /* Extract a slice index from a PyInt or PyLong or an object with the |
| 4688 | nb_index slot defined, and store in *pi. |
| 4689 | 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] | 4690 | 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] | 4691 | Return 0 on error, 1 on success. |
Tim Peters | cb479e7 | 2001-12-16 19:11:44 +0000 | [diff] [blame] | 4692 | */ |
Tim Peters | b519638 | 2001-12-16 19:44:20 +0000 | [diff] [blame] | 4693 | /* Note: If v is NULL, return success without storing into *pi. This |
| 4694 | is because_PyEval_SliceIndex() is called by apply_slice(), which can be |
| 4695 | 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] | 4696 | */ |
Guido van Rossum | 20c6add | 2000-05-08 14:06:50 +0000 | [diff] [blame] | 4697 | int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4698 | _PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4699 | { |
Serhiy Storchaka | 079f21f | 2017-03-30 20:32:18 +0300 | [diff] [blame] | 4700 | if (v != NULL && v != Py_None) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4701 | Py_ssize_t x; |
| 4702 | if (PyInt_Check(v)) { |
| 4703 | /* XXX(nnorwitz): I think PyInt_AS_LONG is correct, |
| 4704 | however, it looks like it should be AsSsize_t. |
| 4705 | There should be a comment here explaining why. |
| 4706 | */ |
| 4707 | x = PyInt_AS_LONG(v); |
| 4708 | } |
| 4709 | else if (PyIndex_Check(v)) { |
| 4710 | x = PyNumber_AsSsize_t(v, NULL); |
| 4711 | if (x == -1 && PyErr_Occurred()) |
| 4712 | return 0; |
| 4713 | } |
| 4714 | else { |
| 4715 | PyErr_SetString(PyExc_TypeError, |
| 4716 | "slice indices must be integers or " |
| 4717 | "None or have an __index__ method"); |
| 4718 | return 0; |
| 4719 | } |
| 4720 | *pi = x; |
| 4721 | } |
| 4722 | return 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4723 | } |
| 4724 | |
Serhiy Storchaka | 079f21f | 2017-03-30 20:32:18 +0300 | [diff] [blame] | 4725 | int |
| 4726 | _PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi) |
| 4727 | { |
| 4728 | Py_ssize_t x; |
| 4729 | if (PyIndex_Check(v)) { |
| 4730 | x = PyNumber_AsSsize_t(v, NULL); |
| 4731 | if (x == -1 && PyErr_Occurred()) |
| 4732 | return 0; |
| 4733 | } |
| 4734 | else { |
| 4735 | PyErr_SetString(PyExc_TypeError, |
| 4736 | "slice indices must be integers or " |
| 4737 | "have an __index__ method"); |
| 4738 | return 0; |
| 4739 | } |
| 4740 | *pi = x; |
| 4741 | return 1; |
| 4742 | } |
| 4743 | |
| 4744 | |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 4745 | #undef ISINDEX |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 4746 | #define ISINDEX(x) ((x) == NULL || \ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4747 | PyInt_Check(x) || PyLong_Check(x) || PyIndex_Check(x)) |
Guido van Rossum | 50d756e | 2001-08-18 17:43:36 +0000 | [diff] [blame] | 4748 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4749 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4750 | 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] | 4751 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4752 | PyTypeObject *tp = u->ob_type; |
| 4753 | PySequenceMethods *sq = tp->tp_as_sequence; |
Guido van Rossum | 50d756e | 2001-08-18 17:43:36 +0000 | [diff] [blame] | 4754 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4755 | if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) { |
| 4756 | Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX; |
| 4757 | if (!_PyEval_SliceIndex(v, &ilow)) |
| 4758 | return NULL; |
| 4759 | if (!_PyEval_SliceIndex(w, &ihigh)) |
| 4760 | return NULL; |
| 4761 | return PySequence_GetSlice(u, ilow, ihigh); |
| 4762 | } |
| 4763 | else { |
| 4764 | PyObject *slice = PySlice_New(v, w, NULL); |
| 4765 | if (slice != NULL) { |
| 4766 | PyObject *res = PyObject_GetItem(u, slice); |
| 4767 | Py_DECREF(slice); |
| 4768 | return res; |
| 4769 | } |
| 4770 | else |
| 4771 | return NULL; |
| 4772 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4773 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4774 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4775 | static int |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4776 | assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4777 | /* u[v:w] = x */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4778 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4779 | PyTypeObject *tp = u->ob_type; |
| 4780 | PySequenceMethods *sq = tp->tp_as_sequence; |
Guido van Rossum | 50d756e | 2001-08-18 17:43:36 +0000 | [diff] [blame] | 4781 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4782 | if (sq && sq->sq_ass_slice && ISINDEX(v) && ISINDEX(w)) { |
| 4783 | Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX; |
| 4784 | if (!_PyEval_SliceIndex(v, &ilow)) |
| 4785 | return -1; |
| 4786 | if (!_PyEval_SliceIndex(w, &ihigh)) |
| 4787 | return -1; |
| 4788 | if (x == NULL) |
| 4789 | return PySequence_DelSlice(u, ilow, ihigh); |
| 4790 | else |
| 4791 | return PySequence_SetSlice(u, ilow, ihigh, x); |
| 4792 | } |
| 4793 | else { |
| 4794 | PyObject *slice = PySlice_New(v, w, NULL); |
| 4795 | if (slice != NULL) { |
| 4796 | int res; |
| 4797 | if (x != NULL) |
| 4798 | res = PyObject_SetItem(u, slice, x); |
| 4799 | else |
| 4800 | res = PyObject_DelItem(u, slice); |
| 4801 | Py_DECREF(slice); |
| 4802 | return res; |
| 4803 | } |
| 4804 | else |
| 4805 | return -1; |
| 4806 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4807 | } |
| 4808 | |
Guido van Rossum | 04edb52 | 2008-03-18 02:49:46 +0000 | [diff] [blame] | 4809 | #define Py3kExceptionClass_Check(x) \ |
| 4810 | (PyType_Check((x)) && \ |
| 4811 | PyType_FastSubclass((PyTypeObject*)(x), Py_TPFLAGS_BASE_EXC_SUBCLASS)) |
| 4812 | |
| 4813 | #define CANNOT_CATCH_MSG "catching classes that don't inherit from " \ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4814 | "BaseException is not allowed in 3.x" |
Guido van Rossum | 04edb52 | 2008-03-18 02:49:46 +0000 | [diff] [blame] | 4815 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4816 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4817 | cmp_outcome(int op, register PyObject *v, register PyObject *w) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4818 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4819 | int res = 0; |
| 4820 | switch (op) { |
| 4821 | case PyCmp_IS: |
| 4822 | res = (v == w); |
| 4823 | break; |
| 4824 | case PyCmp_IS_NOT: |
| 4825 | res = (v != w); |
| 4826 | break; |
| 4827 | case PyCmp_IN: |
| 4828 | res = PySequence_Contains(w, v); |
| 4829 | if (res < 0) |
| 4830 | return NULL; |
| 4831 | break; |
| 4832 | case PyCmp_NOT_IN: |
| 4833 | res = PySequence_Contains(w, v); |
| 4834 | if (res < 0) |
| 4835 | return NULL; |
| 4836 | res = !res; |
| 4837 | break; |
| 4838 | case PyCmp_EXC_MATCH: |
| 4839 | if (PyTuple_Check(w)) { |
| 4840 | Py_ssize_t i, length; |
| 4841 | length = PyTuple_Size(w); |
| 4842 | for (i = 0; i < length; i += 1) { |
| 4843 | PyObject *exc = PyTuple_GET_ITEM(w, i); |
| 4844 | if (PyString_Check(exc)) { |
| 4845 | int ret_val; |
| 4846 | ret_val = PyErr_WarnEx( |
| 4847 | PyExc_DeprecationWarning, |
| 4848 | "catching of string " |
| 4849 | "exceptions is deprecated", 1); |
| 4850 | if (ret_val < 0) |
| 4851 | return NULL; |
| 4852 | } |
| 4853 | else if (Py_Py3kWarningFlag && |
| 4854 | !PyTuple_Check(exc) && |
| 4855 | !Py3kExceptionClass_Check(exc)) |
| 4856 | { |
| 4857 | int ret_val; |
| 4858 | ret_val = PyErr_WarnEx( |
| 4859 | PyExc_DeprecationWarning, |
| 4860 | CANNOT_CATCH_MSG, 1); |
| 4861 | if (ret_val < 0) |
| 4862 | return NULL; |
| 4863 | } |
| 4864 | } |
| 4865 | } |
| 4866 | else { |
| 4867 | if (PyString_Check(w)) { |
| 4868 | int ret_val; |
| 4869 | ret_val = PyErr_WarnEx( |
| 4870 | PyExc_DeprecationWarning, |
| 4871 | "catching of string " |
| 4872 | "exceptions is deprecated", 1); |
| 4873 | if (ret_val < 0) |
| 4874 | return NULL; |
| 4875 | } |
| 4876 | else if (Py_Py3kWarningFlag && |
| 4877 | !PyTuple_Check(w) && |
| 4878 | !Py3kExceptionClass_Check(w)) |
| 4879 | { |
| 4880 | int ret_val; |
| 4881 | ret_val = PyErr_WarnEx( |
| 4882 | PyExc_DeprecationWarning, |
| 4883 | CANNOT_CATCH_MSG, 1); |
| 4884 | if (ret_val < 0) |
| 4885 | return NULL; |
| 4886 | } |
| 4887 | } |
| 4888 | res = PyErr_GivenExceptionMatches(v, w); |
| 4889 | break; |
| 4890 | default: |
| 4891 | return PyObject_RichCompare(v, w, op); |
| 4892 | } |
| 4893 | v = res ? Py_True : Py_False; |
| 4894 | Py_INCREF(v); |
| 4895 | return v; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4896 | } |
| 4897 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4898 | static PyObject * |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 4899 | import_from(PyObject *v, PyObject *name) |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 4900 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4901 | PyObject *x; |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 4902 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4903 | x = PyObject_GetAttr(v, name); |
| 4904 | if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 4905 | PyErr_Format(PyExc_ImportError, |
| 4906 | "cannot import name %.230s", |
| 4907 | PyString_AsString(name)); |
| 4908 | } |
| 4909 | return x; |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 4910 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4911 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4912 | static int |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 4913 | import_all_from(PyObject *locals, PyObject *v) |
| 4914 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4915 | PyObject *all = PyObject_GetAttrString(v, "__all__"); |
| 4916 | PyObject *dict, *name, *value; |
| 4917 | int skip_leading_underscores = 0; |
| 4918 | int pos, err; |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 4919 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4920 | if (all == NULL) { |
| 4921 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 4922 | return -1; /* Unexpected error */ |
| 4923 | PyErr_Clear(); |
| 4924 | dict = PyObject_GetAttrString(v, "__dict__"); |
| 4925 | if (dict == NULL) { |
| 4926 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 4927 | return -1; |
| 4928 | PyErr_SetString(PyExc_ImportError, |
| 4929 | "from-import-* object has no __dict__ and no __all__"); |
| 4930 | return -1; |
| 4931 | } |
| 4932 | all = PyMapping_Keys(dict); |
| 4933 | Py_DECREF(dict); |
| 4934 | if (all == NULL) |
| 4935 | return -1; |
| 4936 | skip_leading_underscores = 1; |
| 4937 | } |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 4938 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4939 | for (pos = 0, err = 0; ; pos++) { |
| 4940 | name = PySequence_GetItem(all, pos); |
| 4941 | if (name == NULL) { |
| 4942 | if (!PyErr_ExceptionMatches(PyExc_IndexError)) |
| 4943 | err = -1; |
| 4944 | else |
| 4945 | PyErr_Clear(); |
| 4946 | break; |
| 4947 | } |
| 4948 | if (skip_leading_underscores && |
| 4949 | PyString_Check(name) && |
| 4950 | PyString_AS_STRING(name)[0] == '_') |
| 4951 | { |
| 4952 | Py_DECREF(name); |
| 4953 | continue; |
| 4954 | } |
| 4955 | value = PyObject_GetAttr(v, name); |
| 4956 | if (value == NULL) |
| 4957 | err = -1; |
| 4958 | else if (PyDict_CheckExact(locals)) |
| 4959 | err = PyDict_SetItem(locals, name, value); |
| 4960 | else |
| 4961 | err = PyObject_SetItem(locals, name, value); |
| 4962 | Py_DECREF(name); |
| 4963 | Py_XDECREF(value); |
| 4964 | if (err != 0) |
| 4965 | break; |
| 4966 | } |
| 4967 | Py_DECREF(all); |
| 4968 | return err; |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 4969 | } |
| 4970 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 4971 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4972 | build_class(PyObject *methods, PyObject *bases, PyObject *name) |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 4973 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4974 | PyObject *metaclass = NULL, *result, *base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4975 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4976 | if (PyDict_Check(methods)) |
| 4977 | metaclass = PyDict_GetItemString(methods, "__metaclass__"); |
| 4978 | if (metaclass != NULL) |
| 4979 | Py_INCREF(metaclass); |
| 4980 | else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) { |
| 4981 | base = PyTuple_GET_ITEM(bases, 0); |
| 4982 | metaclass = PyObject_GetAttrString(base, "__class__"); |
| 4983 | if (metaclass == NULL) { |
| 4984 | PyErr_Clear(); |
| 4985 | metaclass = (PyObject *)base->ob_type; |
| 4986 | Py_INCREF(metaclass); |
| 4987 | } |
| 4988 | } |
| 4989 | else { |
| 4990 | PyObject *g = PyEval_GetGlobals(); |
| 4991 | if (g != NULL && PyDict_Check(g)) |
| 4992 | metaclass = PyDict_GetItemString(g, "__metaclass__"); |
| 4993 | if (metaclass == NULL) |
| 4994 | metaclass = (PyObject *) &PyClass_Type; |
| 4995 | Py_INCREF(metaclass); |
| 4996 | } |
| 4997 | result = PyObject_CallFunctionObjArgs(metaclass, name, bases, methods, |
| 4998 | NULL); |
| 4999 | Py_DECREF(metaclass); |
| 5000 | if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 5001 | /* A type error here likely means that the user passed |
| 5002 | in a base that was not a class (such the random module |
| 5003 | instead of the random.random type). Help them out with |
| 5004 | by augmenting the error message with more information.*/ |
Raymond Hettinger | cfc3192 | 2004-09-16 16:41:57 +0000 | [diff] [blame] | 5005 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5006 | PyObject *ptype, *pvalue, *ptraceback; |
Raymond Hettinger | cfc3192 | 2004-09-16 16:41:57 +0000 | [diff] [blame] | 5007 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5008 | PyErr_Fetch(&ptype, &pvalue, &ptraceback); |
| 5009 | if (PyString_Check(pvalue)) { |
| 5010 | PyObject *newmsg; |
| 5011 | newmsg = PyString_FromFormat( |
| 5012 | "Error when calling the metaclass bases\n" |
| 5013 | " %s", |
| 5014 | PyString_AS_STRING(pvalue)); |
| 5015 | if (newmsg != NULL) { |
| 5016 | Py_DECREF(pvalue); |
| 5017 | pvalue = newmsg; |
| 5018 | } |
| 5019 | } |
| 5020 | PyErr_Restore(ptype, pvalue, ptraceback); |
| 5021 | } |
| 5022 | return result; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 5023 | } |
| 5024 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 5025 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 5026 | exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5027 | PyObject *locals) |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 5028 | { |
Benjamin Peterson | d2903bd | 2014-08-09 19:39:36 -0700 | [diff] [blame] | 5029 | int n; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5030 | PyObject *v; |
| 5031 | int plain = 0; |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 5032 | |
Benjamin Peterson | d2903bd | 2014-08-09 19:39:36 -0700 | [diff] [blame] | 5033 | if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None && |
| 5034 | ((n = PyTuple_Size(prog)) == 2 || n == 3)) { |
| 5035 | /* Backward compatibility hack */ |
| 5036 | globals = PyTuple_GetItem(prog, 1); |
| 5037 | if (n == 3) |
| 5038 | locals = PyTuple_GetItem(prog, 2); |
| 5039 | prog = PyTuple_GetItem(prog, 0); |
| 5040 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5041 | if (globals == Py_None) { |
| 5042 | globals = PyEval_GetGlobals(); |
| 5043 | if (locals == Py_None) { |
| 5044 | locals = PyEval_GetLocals(); |
| 5045 | plain = 1; |
| 5046 | } |
| 5047 | if (!globals || !locals) { |
| 5048 | PyErr_SetString(PyExc_SystemError, |
| 5049 | "globals and locals cannot be NULL"); |
| 5050 | return -1; |
| 5051 | } |
| 5052 | } |
| 5053 | else if (locals == Py_None) |
| 5054 | locals = globals; |
| 5055 | if (!PyString_Check(prog) && |
Benjamin Peterson | 78821dd | 2009-01-25 17:15:10 +0000 | [diff] [blame] | 5056 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5057 | !PyUnicode_Check(prog) && |
Benjamin Peterson | 78821dd | 2009-01-25 17:15:10 +0000 | [diff] [blame] | 5058 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5059 | !PyCode_Check(prog) && |
| 5060 | !PyFile_Check(prog)) { |
| 5061 | PyErr_SetString(PyExc_TypeError, |
| 5062 | "exec: arg 1 must be a string, file, or code object"); |
| 5063 | return -1; |
| 5064 | } |
| 5065 | if (!PyDict_Check(globals)) { |
| 5066 | PyErr_SetString(PyExc_TypeError, |
| 5067 | "exec: arg 2 must be a dictionary or None"); |
| 5068 | return -1; |
| 5069 | } |
| 5070 | if (!PyMapping_Check(locals)) { |
| 5071 | PyErr_SetString(PyExc_TypeError, |
| 5072 | "exec: arg 3 must be a mapping or None"); |
| 5073 | return -1; |
| 5074 | } |
| 5075 | if (PyDict_GetItemString(globals, "__builtins__") == NULL) |
| 5076 | PyDict_SetItemString(globals, "__builtins__", f->f_builtins); |
| 5077 | if (PyCode_Check(prog)) { |
| 5078 | if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) { |
| 5079 | PyErr_SetString(PyExc_TypeError, |
| 5080 | "code object passed to exec may not contain free variables"); |
| 5081 | return -1; |
| 5082 | } |
| 5083 | v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals); |
| 5084 | } |
| 5085 | else if (PyFile_Check(prog)) { |
| 5086 | FILE *fp = PyFile_AsFile(prog); |
| 5087 | char *name = PyString_AsString(PyFile_Name(prog)); |
| 5088 | PyCompilerFlags cf; |
| 5089 | if (name == NULL) |
| 5090 | return -1; |
| 5091 | cf.cf_flags = 0; |
| 5092 | if (PyEval_MergeCompilerFlags(&cf)) |
| 5093 | v = PyRun_FileFlags(fp, name, Py_file_input, globals, |
| 5094 | locals, &cf); |
| 5095 | else |
| 5096 | v = PyRun_File(fp, name, Py_file_input, globals, |
| 5097 | locals); |
| 5098 | } |
| 5099 | else { |
| 5100 | PyObject *tmp = NULL; |
| 5101 | char *str; |
| 5102 | PyCompilerFlags cf; |
| 5103 | cf.cf_flags = 0; |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 5104 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5105 | if (PyUnicode_Check(prog)) { |
| 5106 | tmp = PyUnicode_AsUTF8String(prog); |
| 5107 | if (tmp == NULL) |
| 5108 | return -1; |
| 5109 | prog = tmp; |
| 5110 | cf.cf_flags |= PyCF_SOURCE_IS_UTF8; |
| 5111 | } |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 5112 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5113 | if (PyString_AsStringAndSize(prog, &str, NULL)) |
| 5114 | return -1; |
| 5115 | if (PyEval_MergeCompilerFlags(&cf)) |
| 5116 | v = PyRun_StringFlags(str, Py_file_input, globals, |
| 5117 | locals, &cf); |
| 5118 | else |
| 5119 | v = PyRun_String(str, Py_file_input, globals, locals); |
| 5120 | Py_XDECREF(tmp); |
| 5121 | } |
| 5122 | if (plain) |
| 5123 | PyFrame_LocalsToFast(f, 0); |
| 5124 | if (v == NULL) |
| 5125 | return -1; |
| 5126 | Py_DECREF(v); |
| 5127 | return 0; |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 5128 | } |
Guido van Rossum | 24c1374 | 1995-02-14 09:42:43 +0000 | [diff] [blame] | 5129 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 5130 | static void |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5131 | format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj) |
| 5132 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5133 | char *obj_str; |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5134 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5135 | if (!obj) |
| 5136 | return; |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5137 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5138 | obj_str = PyString_AsString(obj); |
| 5139 | if (!obj_str) |
| 5140 | return; |
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 | PyErr_Format(exc, format_str, obj_str); |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5143 | } |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5144 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 5145 | static PyObject * |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 5146 | string_concatenate(PyObject *v, PyObject *w, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5147 | PyFrameObject *f, unsigned char *next_instr) |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 5148 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5149 | /* This function implements 'variable += expr' when both arguments |
| 5150 | are strings. */ |
| 5151 | Py_ssize_t v_len = PyString_GET_SIZE(v); |
| 5152 | Py_ssize_t w_len = PyString_GET_SIZE(w); |
| 5153 | Py_ssize_t new_len = v_len + w_len; |
| 5154 | if (new_len < 0) { |
| 5155 | PyErr_SetString(PyExc_OverflowError, |
| 5156 | "strings are too large to concat"); |
| 5157 | return NULL; |
| 5158 | } |
Tim Peters | 7df5e7f | 2006-05-26 23:14:37 +0000 | [diff] [blame] | 5159 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5160 | if (v->ob_refcnt == 2) { |
| 5161 | /* In the common case, there are 2 references to the value |
| 5162 | * stored in 'variable' when the += is performed: one on the |
| 5163 | * value stack (in 'v') and one still stored in the |
| 5164 | * 'variable'. We try to delete the variable now to reduce |
| 5165 | * the refcnt to 1. |
| 5166 | */ |
| 5167 | switch (*next_instr) { |
| 5168 | case STORE_FAST: |
| 5169 | { |
| 5170 | int oparg = PEEKARG(); |
| 5171 | PyObject **fastlocals = f->f_localsplus; |
| 5172 | if (GETLOCAL(oparg) == v) |
| 5173 | SETLOCAL(oparg, NULL); |
| 5174 | break; |
| 5175 | } |
| 5176 | case STORE_DEREF: |
| 5177 | { |
| 5178 | PyObject **freevars = (f->f_localsplus + |
| 5179 | f->f_code->co_nlocals); |
| 5180 | PyObject *c = freevars[PEEKARG()]; |
| 5181 | if (PyCell_GET(c) == v) |
| 5182 | PyCell_Set(c, NULL); |
| 5183 | break; |
| 5184 | } |
| 5185 | case STORE_NAME: |
| 5186 | { |
| 5187 | PyObject *names = f->f_code->co_names; |
| 5188 | PyObject *name = GETITEM(names, PEEKARG()); |
| 5189 | PyObject *locals = f->f_locals; |
| 5190 | if (PyDict_CheckExact(locals) && |
| 5191 | PyDict_GetItem(locals, name) == v) { |
| 5192 | if (PyDict_DelItem(locals, name) != 0) { |
| 5193 | PyErr_Clear(); |
| 5194 | } |
| 5195 | } |
| 5196 | break; |
| 5197 | } |
| 5198 | } |
| 5199 | } |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 5200 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5201 | if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) { |
| 5202 | /* Now we own the last reference to 'v', so we can resize it |
| 5203 | * in-place. |
| 5204 | */ |
| 5205 | if (_PyString_Resize(&v, new_len) != 0) { |
| 5206 | /* XXX if _PyString_Resize() fails, 'v' has been |
| 5207 | * deallocated so it cannot be put back into |
| 5208 | * 'variable'. The MemoryError is raised when there |
| 5209 | * is no value in 'variable', which might (very |
| 5210 | * remotely) be a cause of incompatibilities. |
| 5211 | */ |
| 5212 | return NULL; |
| 5213 | } |
| 5214 | /* copy 'w' into the newly allocated area of 'v' */ |
| 5215 | memcpy(PyString_AS_STRING(v) + v_len, |
| 5216 | PyString_AS_STRING(w), w_len); |
| 5217 | return v; |
| 5218 | } |
| 5219 | else { |
| 5220 | /* When in-place resizing is not an option. */ |
| 5221 | PyString_Concat(&v, w); |
| 5222 | return v; |
| 5223 | } |
Raymond Hettinger | 52a21b8 | 2004-08-06 18:43:09 +0000 | [diff] [blame] | 5224 | } |
| 5225 | |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5226 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 5227 | |
Fredrik Lundh | 7a83089 | 2006-05-27 10:39:48 +0000 | [diff] [blame] | 5228 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 5229 | getarray(long a[256]) |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5230 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5231 | int i; |
| 5232 | PyObject *l = PyList_New(256); |
| 5233 | if (l == NULL) return NULL; |
| 5234 | for (i = 0; i < 256; i++) { |
| 5235 | PyObject *x = PyInt_FromLong(a[i]); |
| 5236 | if (x == NULL) { |
| 5237 | Py_DECREF(l); |
| 5238 | return NULL; |
| 5239 | } |
| 5240 | PyList_SetItem(l, i, x); |
| 5241 | } |
| 5242 | for (i = 0; i < 256; i++) |
| 5243 | a[i] = 0; |
| 5244 | return l; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5245 | } |
| 5246 | |
| 5247 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 5248 | _Py_GetDXProfile(PyObject *self, PyObject *args) |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5249 | { |
| 5250 | #ifndef DXPAIRS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5251 | return getarray(dxp); |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5252 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5253 | int i; |
| 5254 | PyObject *l = PyList_New(257); |
| 5255 | if (l == NULL) return NULL; |
| 5256 | for (i = 0; i < 257; i++) { |
| 5257 | PyObject *x = getarray(dxpairs[i]); |
| 5258 | if (x == NULL) { |
| 5259 | Py_DECREF(l); |
| 5260 | return NULL; |
| 5261 | } |
| 5262 | PyList_SetItem(l, i, x); |
| 5263 | } |
| 5264 | return l; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5265 | #endif |
| 5266 | } |
| 5267 | |
| 5268 | #endif |