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