Benjamin Peterson | 1bf494b | 2016-09-07 11:28:35 -0700 | [diff] [blame] | 1 | #include <stdbool.h> |
| 2 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3 | #include "Python.h" |
| 4 | #include "code.h" |
| 5 | #include "structmember.h" |
| 6 | |
Brett Cannon | d0600ed | 2016-09-07 14:30:39 -0700 | [diff] [blame] | 7 | /* Holder for co_extra information */ |
| 8 | typedef struct { |
| 9 | Py_ssize_t ce_size; |
Serhiy Storchaka | 378ebb6 | 2017-07-04 15:06:16 +0300 | [diff] [blame] | 10 | void *ce_extras[1]; |
Brett Cannon | d0600ed | 2016-09-07 14:30:39 -0700 | [diff] [blame] | 11 | } _PyCodeObjectExtra; |
| 12 | |
Benjamin Peterson | 8e0ad46 | 2017-09-07 23:35:53 -0700 | [diff] [blame] | 13 | /* all_name_chars(s): true iff s matches [a-zA-Z0-9_]* */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 14 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15 | all_name_chars(PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 16 | { |
Serhiy Storchaka | 09f3d08 | 2016-10-04 18:17:22 +0300 | [diff] [blame] | 17 | const unsigned char *s, *e; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 18 | |
Serhiy Storchaka | e3b2b4b | 2017-09-08 09:58:51 +0300 | [diff] [blame] | 19 | if (!PyUnicode_IS_ASCII(o)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 20 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 21 | |
Serhiy Storchaka | 09f3d08 | 2016-10-04 18:17:22 +0300 | [diff] [blame] | 22 | s = PyUnicode_1BYTE_DATA(o); |
| 23 | e = s + PyUnicode_GET_LENGTH(o); |
Benjamin Peterson | 9020ac7 | 2017-09-07 18:06:23 -0700 | [diff] [blame] | 24 | for (; s != e; s++) { |
Benjamin Peterson | 2b7953d | 2017-09-08 10:35:49 -0700 | [diff] [blame] | 25 | if (!Py_ISALNUM(*s) && *s != '_') |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 26 | return 0; |
| 27 | } |
| 28 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | static void |
| 32 | intern_strings(PyObject *tuple) |
| 33 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 34 | Py_ssize_t i; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 35 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 36 | for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) { |
| 37 | PyObject *v = PyTuple_GET_ITEM(tuple, i); |
| 38 | if (v == NULL || !PyUnicode_CheckExact(v)) { |
| 39 | Py_FatalError("non-string found in code slot"); |
| 40 | } |
| 41 | PyUnicode_InternInPlace(&PyTuple_GET_ITEM(tuple, i)); |
| 42 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Serhiy Storchaka | 00a0fc1 | 2016-09-30 10:07:26 +0300 | [diff] [blame] | 45 | /* Intern selected string constants */ |
| 46 | static int |
| 47 | intern_string_constants(PyObject *tuple) |
| 48 | { |
| 49 | int modified = 0; |
| 50 | Py_ssize_t i; |
| 51 | |
| 52 | for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) { |
| 53 | PyObject *v = PyTuple_GET_ITEM(tuple, i); |
| 54 | if (PyUnicode_CheckExact(v)) { |
Serhiy Storchaka | e3b2b4b | 2017-09-08 09:58:51 +0300 | [diff] [blame] | 55 | if (PyUnicode_READY(v) == -1) { |
| 56 | PyErr_Clear(); |
| 57 | continue; |
| 58 | } |
Serhiy Storchaka | 00a0fc1 | 2016-09-30 10:07:26 +0300 | [diff] [blame] | 59 | if (all_name_chars(v)) { |
| 60 | PyObject *w = v; |
| 61 | PyUnicode_InternInPlace(&v); |
| 62 | if (w != v) { |
| 63 | PyTuple_SET_ITEM(tuple, i, v); |
| 64 | modified = 1; |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | else if (PyTuple_CheckExact(v)) { |
| 69 | intern_string_constants(v); |
| 70 | } |
| 71 | else if (PyFrozenSet_CheckExact(v)) { |
Yury Selivanov | d2fd359 | 2016-11-09 09:42:14 -0500 | [diff] [blame] | 72 | PyObject *w = v; |
Serhiy Storchaka | 00a0fc1 | 2016-09-30 10:07:26 +0300 | [diff] [blame] | 73 | PyObject *tmp = PySequence_Tuple(v); |
| 74 | if (tmp == NULL) { |
| 75 | PyErr_Clear(); |
| 76 | continue; |
| 77 | } |
| 78 | if (intern_string_constants(tmp)) { |
| 79 | v = PyFrozenSet_New(tmp); |
| 80 | if (v == NULL) { |
| 81 | PyErr_Clear(); |
| 82 | } |
| 83 | else { |
| 84 | PyTuple_SET_ITEM(tuple, i, v); |
Yury Selivanov | d2fd359 | 2016-11-09 09:42:14 -0500 | [diff] [blame] | 85 | Py_DECREF(w); |
Serhiy Storchaka | 00a0fc1 | 2016-09-30 10:07:26 +0300 | [diff] [blame] | 86 | modified = 1; |
| 87 | } |
| 88 | } |
| 89 | Py_DECREF(tmp); |
| 90 | } |
| 91 | } |
| 92 | return modified; |
| 93 | } |
| 94 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 95 | |
| 96 | PyCodeObject * |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 97 | PyCode_New(int argcount, int kwonlyargcount, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 98 | int nlocals, int stacksize, int flags, |
| 99 | PyObject *code, PyObject *consts, PyObject *names, |
| 100 | PyObject *varnames, PyObject *freevars, PyObject *cellvars, |
| 101 | PyObject *filename, PyObject *name, int firstlineno, |
| 102 | PyObject *lnotab) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 103 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 104 | PyCodeObject *co; |
Serhiy Storchaka | 5bb8b91 | 2016-12-16 19:19:02 +0200 | [diff] [blame] | 105 | Py_ssize_t *cell2arg = NULL; |
Serhiy Storchaka | bd47384 | 2018-07-16 09:10:19 +0300 | [diff] [blame^] | 106 | Py_ssize_t i, n_cellvars, n_varnames, total_args; |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 107 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 108 | /* Check argument types */ |
| 109 | if (argcount < 0 || kwonlyargcount < 0 || nlocals < 0 || |
Serhiy Storchaka | 460bd0d | 2016-11-20 12:16:46 +0200 | [diff] [blame] | 110 | code == NULL || !PyBytes_Check(code) || |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 111 | consts == NULL || !PyTuple_Check(consts) || |
| 112 | names == NULL || !PyTuple_Check(names) || |
| 113 | varnames == NULL || !PyTuple_Check(varnames) || |
| 114 | freevars == NULL || !PyTuple_Check(freevars) || |
| 115 | cellvars == NULL || !PyTuple_Check(cellvars) || |
| 116 | name == NULL || !PyUnicode_Check(name) || |
| 117 | filename == NULL || !PyUnicode_Check(filename) || |
Serhiy Storchaka | 460bd0d | 2016-11-20 12:16:46 +0200 | [diff] [blame] | 118 | lnotab == NULL || !PyBytes_Check(lnotab)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 119 | PyErr_BadInternalCall(); |
| 120 | return NULL; |
| 121 | } |
Victor Stinner | 7c74de4 | 2013-10-10 15:55:14 +0200 | [diff] [blame] | 122 | |
| 123 | /* Ensure that the filename is a ready Unicode string */ |
| 124 | if (PyUnicode_READY(filename) < 0) |
| 125 | return NULL; |
| 126 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 127 | intern_strings(names); |
| 128 | intern_strings(varnames); |
| 129 | intern_strings(freevars); |
| 130 | intern_strings(cellvars); |
Serhiy Storchaka | 00a0fc1 | 2016-09-30 10:07:26 +0300 | [diff] [blame] | 131 | intern_string_constants(consts); |
Nick Coghlan | 078f181 | 2017-12-03 11:12:20 +1000 | [diff] [blame] | 132 | |
| 133 | /* Check for any inner or outer closure references */ |
| 134 | n_cellvars = PyTuple_GET_SIZE(cellvars); |
| 135 | if (!n_cellvars && !PyTuple_GET_SIZE(freevars)) { |
| 136 | flags |= CO_NOFREE; |
| 137 | } else { |
| 138 | flags &= ~CO_NOFREE; |
| 139 | } |
| 140 | |
Serhiy Storchaka | bd47384 | 2018-07-16 09:10:19 +0300 | [diff] [blame^] | 141 | n_varnames = PyTuple_GET_SIZE(varnames); |
| 142 | if (argcount <= n_varnames && kwonlyargcount <= n_varnames) { |
| 143 | /* Never overflows. */ |
| 144 | total_args = (Py_ssize_t)argcount + (Py_ssize_t)kwonlyargcount + |
| 145 | ((flags & CO_VARARGS) != 0) + ((flags & CO_VARKEYWORDS) != 0); |
| 146 | } |
| 147 | else { |
| 148 | total_args = n_varnames + 1; |
| 149 | } |
| 150 | if (total_args > n_varnames) { |
| 151 | PyErr_SetString(PyExc_ValueError, "code: varnames is too small"); |
| 152 | return NULL; |
| 153 | } |
| 154 | |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 155 | /* Create mapping between cells and arguments if needed. */ |
| 156 | if (n_cellvars) { |
Benjamin Peterson | 1bf494b | 2016-09-07 11:28:35 -0700 | [diff] [blame] | 157 | bool used_cell2arg = false; |
Serhiy Storchaka | 5bb8b91 | 2016-12-16 19:19:02 +0200 | [diff] [blame] | 158 | cell2arg = PyMem_NEW(Py_ssize_t, n_cellvars); |
| 159 | if (cell2arg == NULL) { |
| 160 | PyErr_NoMemory(); |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 161 | return NULL; |
Serhiy Storchaka | 5bb8b91 | 2016-12-16 19:19:02 +0200 | [diff] [blame] | 162 | } |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 163 | /* Find cells which are also arguments. */ |
| 164 | for (i = 0; i < n_cellvars; i++) { |
| 165 | Py_ssize_t j; |
| 166 | PyObject *cell = PyTuple_GET_ITEM(cellvars, i); |
Serhiy Storchaka | 5bb8b91 | 2016-12-16 19:19:02 +0200 | [diff] [blame] | 167 | cell2arg[i] = CO_CELL_NOT_AN_ARG; |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 168 | for (j = 0; j < total_args; j++) { |
| 169 | PyObject *arg = PyTuple_GET_ITEM(varnames, j); |
Serhiy Storchaka | 5bb8b91 | 2016-12-16 19:19:02 +0200 | [diff] [blame] | 170 | int cmp = PyUnicode_Compare(cell, arg); |
| 171 | if (cmp == -1 && PyErr_Occurred()) { |
| 172 | PyMem_FREE(cell2arg); |
| 173 | return NULL; |
| 174 | } |
| 175 | if (cmp == 0) { |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 176 | cell2arg[i] = j; |
Benjamin Peterson | 1bf494b | 2016-09-07 11:28:35 -0700 | [diff] [blame] | 177 | used_cell2arg = true; |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 178 | break; |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | if (!used_cell2arg) { |
| 183 | PyMem_FREE(cell2arg); |
| 184 | cell2arg = NULL; |
| 185 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 186 | } |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 187 | co = PyObject_NEW(PyCodeObject, &PyCode_Type); |
| 188 | if (co == NULL) { |
| 189 | if (cell2arg) |
| 190 | PyMem_FREE(cell2arg); |
| 191 | return NULL; |
| 192 | } |
| 193 | co->co_argcount = argcount; |
| 194 | co->co_kwonlyargcount = kwonlyargcount; |
| 195 | co->co_nlocals = nlocals; |
| 196 | co->co_stacksize = stacksize; |
| 197 | co->co_flags = flags; |
| 198 | Py_INCREF(code); |
| 199 | co->co_code = code; |
| 200 | Py_INCREF(consts); |
| 201 | co->co_consts = consts; |
| 202 | Py_INCREF(names); |
| 203 | co->co_names = names; |
| 204 | Py_INCREF(varnames); |
| 205 | co->co_varnames = varnames; |
| 206 | Py_INCREF(freevars); |
| 207 | co->co_freevars = freevars; |
| 208 | Py_INCREF(cellvars); |
| 209 | co->co_cellvars = cellvars; |
| 210 | co->co_cell2arg = cell2arg; |
| 211 | Py_INCREF(filename); |
| 212 | co->co_filename = filename; |
| 213 | Py_INCREF(name); |
| 214 | co->co_name = name; |
| 215 | co->co_firstlineno = firstlineno; |
| 216 | Py_INCREF(lnotab); |
| 217 | co->co_lnotab = lnotab; |
| 218 | co->co_zombieframe = NULL; |
| 219 | co->co_weakreflist = NULL; |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 220 | co->co_extra = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 221 | return co; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 224 | PyCodeObject * |
| 225 | PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno) |
| 226 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 227 | static PyObject *emptystring = NULL; |
| 228 | static PyObject *nulltuple = NULL; |
| 229 | PyObject *filename_ob = NULL; |
| 230 | PyObject *funcname_ob = NULL; |
| 231 | PyCodeObject *result = NULL; |
| 232 | if (emptystring == NULL) { |
| 233 | emptystring = PyBytes_FromString(""); |
| 234 | if (emptystring == NULL) |
| 235 | goto failed; |
| 236 | } |
| 237 | if (nulltuple == NULL) { |
| 238 | nulltuple = PyTuple_New(0); |
| 239 | if (nulltuple == NULL) |
| 240 | goto failed; |
| 241 | } |
| 242 | funcname_ob = PyUnicode_FromString(funcname); |
| 243 | if (funcname_ob == NULL) |
| 244 | goto failed; |
| 245 | filename_ob = PyUnicode_DecodeFSDefault(filename); |
| 246 | if (filename_ob == NULL) |
| 247 | goto failed; |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 248 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 249 | result = PyCode_New(0, /* argcount */ |
| 250 | 0, /* kwonlyargcount */ |
| 251 | 0, /* nlocals */ |
| 252 | 0, /* stacksize */ |
| 253 | 0, /* flags */ |
| 254 | emptystring, /* code */ |
| 255 | nulltuple, /* consts */ |
| 256 | nulltuple, /* names */ |
| 257 | nulltuple, /* varnames */ |
| 258 | nulltuple, /* freevars */ |
| 259 | nulltuple, /* cellvars */ |
| 260 | filename_ob, /* filename */ |
| 261 | funcname_ob, /* name */ |
| 262 | firstlineno, /* firstlineno */ |
| 263 | emptystring /* lnotab */ |
| 264 | ); |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 265 | |
| 266 | failed: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 267 | Py_XDECREF(funcname_ob); |
| 268 | Py_XDECREF(filename_ob); |
| 269 | return result; |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 270 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 271 | |
| 272 | #define OFF(x) offsetof(PyCodeObject, x) |
| 273 | |
| 274 | static PyMemberDef code_memberlist[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 275 | {"co_argcount", T_INT, OFF(co_argcount), READONLY}, |
| 276 | {"co_kwonlyargcount", T_INT, OFF(co_kwonlyargcount), READONLY}, |
| 277 | {"co_nlocals", T_INT, OFF(co_nlocals), READONLY}, |
| 278 | {"co_stacksize",T_INT, OFF(co_stacksize), READONLY}, |
| 279 | {"co_flags", T_INT, OFF(co_flags), READONLY}, |
| 280 | {"co_code", T_OBJECT, OFF(co_code), READONLY}, |
| 281 | {"co_consts", T_OBJECT, OFF(co_consts), READONLY}, |
| 282 | {"co_names", T_OBJECT, OFF(co_names), READONLY}, |
| 283 | {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY}, |
| 284 | {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY}, |
| 285 | {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY}, |
| 286 | {"co_filename", T_OBJECT, OFF(co_filename), READONLY}, |
| 287 | {"co_name", T_OBJECT, OFF(co_name), READONLY}, |
| 288 | {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY}, |
| 289 | {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY}, |
| 290 | {NULL} /* Sentinel */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 291 | }; |
| 292 | |
| 293 | /* Helper for code_new: return a shallow copy of a tuple that is |
| 294 | guaranteed to contain exact strings, by converting string subclasses |
| 295 | to exact strings and complaining if a non-string is found. */ |
| 296 | static PyObject* |
| 297 | validate_and_copy_tuple(PyObject *tup) |
| 298 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 299 | PyObject *newtuple; |
| 300 | PyObject *item; |
| 301 | Py_ssize_t i, len; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 302 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 303 | len = PyTuple_GET_SIZE(tup); |
| 304 | newtuple = PyTuple_New(len); |
| 305 | if (newtuple == NULL) |
| 306 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 307 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 308 | for (i = 0; i < len; i++) { |
| 309 | item = PyTuple_GET_ITEM(tup, i); |
| 310 | if (PyUnicode_CheckExact(item)) { |
| 311 | Py_INCREF(item); |
| 312 | } |
| 313 | else if (!PyUnicode_Check(item)) { |
| 314 | PyErr_Format( |
| 315 | PyExc_TypeError, |
| 316 | "name tuples must contain only " |
| 317 | "strings, not '%.500s'", |
| 318 | item->ob_type->tp_name); |
| 319 | Py_DECREF(newtuple); |
| 320 | return NULL; |
| 321 | } |
| 322 | else { |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 323 | item = _PyUnicode_Copy(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 324 | if (item == NULL) { |
| 325 | Py_DECREF(newtuple); |
| 326 | return NULL; |
| 327 | } |
| 328 | } |
| 329 | PyTuple_SET_ITEM(newtuple, i, item); |
| 330 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 331 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 332 | return newtuple; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | PyDoc_STRVAR(code_doc, |
Georg Brandl | 8c1a50a | 2009-07-18 09:07:48 +0000 | [diff] [blame] | 336 | "code(argcount, kwonlyargcount, nlocals, stacksize, flags, codestring,\n\ |
Georg Brandl | a1e7e13 | 2008-01-26 09:39:23 +0000 | [diff] [blame] | 337 | constants, names, varnames, filename, name, firstlineno,\n\ |
| 338 | lnotab[, freevars[, cellvars]])\n\ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 339 | \n\ |
| 340 | Create a code object. Not for the faint of heart."); |
| 341 | |
| 342 | static PyObject * |
| 343 | code_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 344 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 345 | int argcount; |
| 346 | int kwonlyargcount; |
| 347 | int nlocals; |
| 348 | int stacksize; |
| 349 | int flags; |
| 350 | PyObject *co = NULL; |
| 351 | PyObject *code; |
| 352 | PyObject *consts; |
| 353 | PyObject *names, *ournames = NULL; |
| 354 | PyObject *varnames, *ourvarnames = NULL; |
| 355 | PyObject *freevars = NULL, *ourfreevars = NULL; |
| 356 | PyObject *cellvars = NULL, *ourcellvars = NULL; |
| 357 | PyObject *filename; |
| 358 | PyObject *name; |
| 359 | int firstlineno; |
| 360 | PyObject *lnotab; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 361 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 362 | if (!PyArg_ParseTuple(args, "iiiiiSO!O!O!UUiS|O!O!:code", |
| 363 | &argcount, &kwonlyargcount, |
| 364 | &nlocals, &stacksize, &flags, |
| 365 | &code, |
| 366 | &PyTuple_Type, &consts, |
| 367 | &PyTuple_Type, &names, |
| 368 | &PyTuple_Type, &varnames, |
| 369 | &filename, &name, |
| 370 | &firstlineno, &lnotab, |
| 371 | &PyTuple_Type, &freevars, |
| 372 | &PyTuple_Type, &cellvars)) |
| 373 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 374 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 375 | if (argcount < 0) { |
| 376 | PyErr_SetString( |
| 377 | PyExc_ValueError, |
| 378 | "code: argcount must not be negative"); |
| 379 | goto cleanup; |
| 380 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 381 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 382 | if (kwonlyargcount < 0) { |
| 383 | PyErr_SetString( |
| 384 | PyExc_ValueError, |
| 385 | "code: kwonlyargcount must not be negative"); |
| 386 | goto cleanup; |
| 387 | } |
| 388 | if (nlocals < 0) { |
| 389 | PyErr_SetString( |
| 390 | PyExc_ValueError, |
| 391 | "code: nlocals must not be negative"); |
| 392 | goto cleanup; |
| 393 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 394 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 395 | ournames = validate_and_copy_tuple(names); |
| 396 | if (ournames == NULL) |
| 397 | goto cleanup; |
| 398 | ourvarnames = validate_and_copy_tuple(varnames); |
| 399 | if (ourvarnames == NULL) |
| 400 | goto cleanup; |
| 401 | if (freevars) |
| 402 | ourfreevars = validate_and_copy_tuple(freevars); |
| 403 | else |
| 404 | ourfreevars = PyTuple_New(0); |
| 405 | if (ourfreevars == NULL) |
| 406 | goto cleanup; |
| 407 | if (cellvars) |
| 408 | ourcellvars = validate_and_copy_tuple(cellvars); |
| 409 | else |
| 410 | ourcellvars = PyTuple_New(0); |
| 411 | if (ourcellvars == NULL) |
| 412 | goto cleanup; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 413 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 414 | co = (PyObject *)PyCode_New(argcount, kwonlyargcount, |
| 415 | nlocals, stacksize, flags, |
| 416 | code, consts, ournames, ourvarnames, |
| 417 | ourfreevars, ourcellvars, filename, |
| 418 | name, firstlineno, lnotab); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 419 | cleanup: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 420 | Py_XDECREF(ournames); |
| 421 | Py_XDECREF(ourvarnames); |
| 422 | Py_XDECREF(ourfreevars); |
| 423 | Py_XDECREF(ourcellvars); |
| 424 | return co; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | static void |
| 428 | code_dealloc(PyCodeObject *co) |
| 429 | { |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 430 | if (co->co_extra != NULL) { |
Dino Viehland | f3cffd2 | 2017-06-21 14:44:36 -0700 | [diff] [blame] | 431 | PyInterpreterState *interp = PyThreadState_Get()->interp; |
Brett Cannon | d0600ed | 2016-09-07 14:30:39 -0700 | [diff] [blame] | 432 | _PyCodeObjectExtra *co_extra = co->co_extra; |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 433 | |
Brett Cannon | d0600ed | 2016-09-07 14:30:39 -0700 | [diff] [blame] | 434 | for (Py_ssize_t i = 0; i < co_extra->ce_size; i++) { |
Dino Viehland | f3cffd2 | 2017-06-21 14:44:36 -0700 | [diff] [blame] | 435 | freefunc free_extra = interp->co_extra_freefuncs[i]; |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 436 | |
| 437 | if (free_extra != NULL) { |
Brett Cannon | d0600ed | 2016-09-07 14:30:39 -0700 | [diff] [blame] | 438 | free_extra(co_extra->ce_extras[i]); |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 439 | } |
| 440 | } |
| 441 | |
Victor Stinner | 23e7944 | 2017-06-28 02:12:00 +0200 | [diff] [blame] | 442 | PyMem_Free(co_extra); |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 443 | } |
| 444 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 445 | Py_XDECREF(co->co_code); |
| 446 | Py_XDECREF(co->co_consts); |
| 447 | Py_XDECREF(co->co_names); |
| 448 | Py_XDECREF(co->co_varnames); |
| 449 | Py_XDECREF(co->co_freevars); |
| 450 | Py_XDECREF(co->co_cellvars); |
| 451 | Py_XDECREF(co->co_filename); |
| 452 | Py_XDECREF(co->co_name); |
| 453 | Py_XDECREF(co->co_lnotab); |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 454 | if (co->co_cell2arg != NULL) |
| 455 | PyMem_FREE(co->co_cell2arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 456 | if (co->co_zombieframe != NULL) |
| 457 | PyObject_GC_Del(co->co_zombieframe); |
| 458 | if (co->co_weakreflist != NULL) |
| 459 | PyObject_ClearWeakRefs((PyObject*)co); |
| 460 | PyObject_DEL(co); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | static PyObject * |
Martin v. Löwis | 3bbd2fa | 2012-07-26 22:23:23 +0200 | [diff] [blame] | 464 | code_sizeof(PyCodeObject *co, void *unused) |
| 465 | { |
Dong-hee Na | b4dc6af | 2017-04-20 16:31:17 +0900 | [diff] [blame] | 466 | Py_ssize_t res = _PyObject_SIZE(Py_TYPE(co)); |
| 467 | _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra*) co->co_extra; |
Martin v. Löwis | 3bbd2fa | 2012-07-26 22:23:23 +0200 | [diff] [blame] | 468 | |
Serhiy Storchaka | 378ebb6 | 2017-07-04 15:06:16 +0300 | [diff] [blame] | 469 | if (co->co_cell2arg != NULL && co->co_cellvars != NULL) { |
Serhiy Storchaka | 5bb8b91 | 2016-12-16 19:19:02 +0200 | [diff] [blame] | 470 | res += PyTuple_GET_SIZE(co->co_cellvars) * sizeof(Py_ssize_t); |
Serhiy Storchaka | 378ebb6 | 2017-07-04 15:06:16 +0300 | [diff] [blame] | 471 | } |
| 472 | if (co_extra != NULL) { |
| 473 | res += sizeof(_PyCodeObjectExtra) + |
| 474 | (co_extra->ce_size-1) * sizeof(co_extra->ce_extras[0]); |
| 475 | } |
Martin v. Löwis | 3bbd2fa | 2012-07-26 22:23:23 +0200 | [diff] [blame] | 476 | return PyLong_FromSsize_t(res); |
| 477 | } |
| 478 | |
| 479 | static PyObject * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 480 | code_repr(PyCodeObject *co) |
| 481 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 482 | int lineno; |
| 483 | if (co->co_firstlineno != 0) |
| 484 | lineno = co->co_firstlineno; |
| 485 | else |
| 486 | lineno = -1; |
| 487 | if (co->co_filename && PyUnicode_Check(co->co_filename)) { |
| 488 | return PyUnicode_FromFormat( |
Victor Stinner | aaa4e9a | 2011-01-05 03:33:26 +0000 | [diff] [blame] | 489 | "<code object %U at %p, file \"%U\", line %d>", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 490 | co->co_name, co, co->co_filename, lineno); |
| 491 | } else { |
| 492 | return PyUnicode_FromFormat( |
Victor Stinner | aaa4e9a | 2011-01-05 03:33:26 +0000 | [diff] [blame] | 493 | "<code object %U at %p, file ???, line %d>", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 494 | co->co_name, co, lineno); |
| 495 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Victor Stinner | efb2413 | 2016-01-22 12:33:12 +0100 | [diff] [blame] | 498 | PyObject* |
| 499 | _PyCode_ConstantKey(PyObject *op) |
| 500 | { |
| 501 | PyObject *key; |
| 502 | |
Serhiy Storchaka | b7e1eff | 2018-04-19 08:28:04 +0300 | [diff] [blame] | 503 | /* Py_None and Py_Ellipsis are singletons. */ |
Victor Stinner | efb2413 | 2016-01-22 12:33:12 +0100 | [diff] [blame] | 504 | if (op == Py_None || op == Py_Ellipsis |
| 505 | || PyLong_CheckExact(op) |
Victor Stinner | efb2413 | 2016-01-22 12:33:12 +0100 | [diff] [blame] | 506 | || PyUnicode_CheckExact(op) |
| 507 | /* code_richcompare() uses _PyCode_ConstantKey() internally */ |
Serhiy Storchaka | b7e1eff | 2018-04-19 08:28:04 +0300 | [diff] [blame] | 508 | || PyCode_Check(op)) |
| 509 | { |
| 510 | /* Objects of these types are always different from object of other |
| 511 | * type and from tuples. */ |
| 512 | Py_INCREF(op); |
| 513 | key = op; |
| 514 | } |
| 515 | else if (PyBool_Check(op) || PyBytes_CheckExact(op)) { |
| 516 | /* Make booleans different from integers 0 and 1. |
| 517 | * Avoid BytesWarning from comparing bytes with strings. */ |
Victor Stinner | efb2413 | 2016-01-22 12:33:12 +0100 | [diff] [blame] | 518 | key = PyTuple_Pack(2, Py_TYPE(op), op); |
| 519 | } |
| 520 | else if (PyFloat_CheckExact(op)) { |
| 521 | double d = PyFloat_AS_DOUBLE(op); |
| 522 | /* all we need is to make the tuple different in either the 0.0 |
| 523 | * or -0.0 case from all others, just to avoid the "coercion". |
| 524 | */ |
| 525 | if (d == 0.0 && copysign(1.0, d) < 0.0) |
| 526 | key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None); |
| 527 | else |
| 528 | key = PyTuple_Pack(2, Py_TYPE(op), op); |
| 529 | } |
| 530 | else if (PyComplex_CheckExact(op)) { |
| 531 | Py_complex z; |
| 532 | int real_negzero, imag_negzero; |
| 533 | /* For the complex case we must make complex(x, 0.) |
| 534 | different from complex(x, -0.) and complex(0., y) |
| 535 | different from complex(-0., y), for any x and y. |
| 536 | All four complex zeros must be distinguished.*/ |
| 537 | z = PyComplex_AsCComplex(op); |
| 538 | real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0; |
| 539 | imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0; |
| 540 | /* use True, False and None singleton as tags for the real and imag |
| 541 | * sign, to make tuples different */ |
| 542 | if (real_negzero && imag_negzero) { |
| 543 | key = PyTuple_Pack(3, Py_TYPE(op), op, Py_True); |
| 544 | } |
| 545 | else if (imag_negzero) { |
| 546 | key = PyTuple_Pack(3, Py_TYPE(op), op, Py_False); |
| 547 | } |
| 548 | else if (real_negzero) { |
| 549 | key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None); |
| 550 | } |
| 551 | else { |
| 552 | key = PyTuple_Pack(2, Py_TYPE(op), op); |
| 553 | } |
| 554 | } |
| 555 | else if (PyTuple_CheckExact(op)) { |
| 556 | Py_ssize_t i, len; |
| 557 | PyObject *tuple; |
| 558 | |
| 559 | len = PyTuple_GET_SIZE(op); |
| 560 | tuple = PyTuple_New(len); |
| 561 | if (tuple == NULL) |
| 562 | return NULL; |
| 563 | |
| 564 | for (i=0; i < len; i++) { |
| 565 | PyObject *item, *item_key; |
| 566 | |
| 567 | item = PyTuple_GET_ITEM(op, i); |
| 568 | item_key = _PyCode_ConstantKey(item); |
| 569 | if (item_key == NULL) { |
| 570 | Py_DECREF(tuple); |
| 571 | return NULL; |
| 572 | } |
| 573 | |
| 574 | PyTuple_SET_ITEM(tuple, i, item_key); |
| 575 | } |
| 576 | |
Serhiy Storchaka | 713640c | 2017-01-24 20:49:26 +0200 | [diff] [blame] | 577 | key = PyTuple_Pack(2, tuple, op); |
Victor Stinner | efb2413 | 2016-01-22 12:33:12 +0100 | [diff] [blame] | 578 | Py_DECREF(tuple); |
| 579 | } |
| 580 | else if (PyFrozenSet_CheckExact(op)) { |
| 581 | Py_ssize_t pos = 0; |
| 582 | PyObject *item; |
| 583 | Py_hash_t hash; |
| 584 | Py_ssize_t i, len; |
| 585 | PyObject *tuple, *set; |
| 586 | |
| 587 | len = PySet_GET_SIZE(op); |
| 588 | tuple = PyTuple_New(len); |
| 589 | if (tuple == NULL) |
| 590 | return NULL; |
| 591 | |
| 592 | i = 0; |
| 593 | while (_PySet_NextEntry(op, &pos, &item, &hash)) { |
| 594 | PyObject *item_key; |
| 595 | |
| 596 | item_key = _PyCode_ConstantKey(item); |
| 597 | if (item_key == NULL) { |
| 598 | Py_DECREF(tuple); |
| 599 | return NULL; |
| 600 | } |
| 601 | |
| 602 | assert(i < len); |
| 603 | PyTuple_SET_ITEM(tuple, i, item_key); |
| 604 | i++; |
| 605 | } |
| 606 | set = PyFrozenSet_New(tuple); |
| 607 | Py_DECREF(tuple); |
| 608 | if (set == NULL) |
| 609 | return NULL; |
| 610 | |
Serhiy Storchaka | 713640c | 2017-01-24 20:49:26 +0200 | [diff] [blame] | 611 | key = PyTuple_Pack(2, set, op); |
Victor Stinner | efb2413 | 2016-01-22 12:33:12 +0100 | [diff] [blame] | 612 | Py_DECREF(set); |
| 613 | return key; |
| 614 | } |
| 615 | else { |
Martin Panter | 6245cb3 | 2016-04-15 02:14:19 +0000 | [diff] [blame] | 616 | /* for other types, use the object identifier as a unique identifier |
Victor Stinner | efb2413 | 2016-01-22 12:33:12 +0100 | [diff] [blame] | 617 | * to ensure that they are seen as unequal. */ |
| 618 | PyObject *obj_id = PyLong_FromVoidPtr(op); |
| 619 | if (obj_id == NULL) |
| 620 | return NULL; |
| 621 | |
Serhiy Storchaka | 713640c | 2017-01-24 20:49:26 +0200 | [diff] [blame] | 622 | key = PyTuple_Pack(2, obj_id, op); |
Victor Stinner | efb2413 | 2016-01-22 12:33:12 +0100 | [diff] [blame] | 623 | Py_DECREF(obj_id); |
| 624 | } |
| 625 | return key; |
| 626 | } |
| 627 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 628 | static PyObject * |
| 629 | code_richcompare(PyObject *self, PyObject *other, int op) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 630 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 631 | PyCodeObject *co, *cp; |
| 632 | int eq; |
Victor Stinner | efb2413 | 2016-01-22 12:33:12 +0100 | [diff] [blame] | 633 | PyObject *consts1, *consts2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 634 | PyObject *res; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 635 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 636 | if ((op != Py_EQ && op != Py_NE) || |
| 637 | !PyCode_Check(self) || |
| 638 | !PyCode_Check(other)) { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 639 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 640 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 641 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 642 | co = (PyCodeObject *)self; |
| 643 | cp = (PyCodeObject *)other; |
Guido van Rossum | b6bb0c7 | 2006-08-24 04:12:18 +0000 | [diff] [blame] | 644 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 645 | eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ); |
| 646 | if (eq <= 0) goto unequal; |
| 647 | eq = co->co_argcount == cp->co_argcount; |
| 648 | if (!eq) goto unequal; |
| 649 | eq = co->co_kwonlyargcount == cp->co_kwonlyargcount; |
| 650 | if (!eq) goto unequal; |
| 651 | eq = co->co_nlocals == cp->co_nlocals; |
| 652 | if (!eq) goto unequal; |
| 653 | eq = co->co_flags == cp->co_flags; |
| 654 | if (!eq) goto unequal; |
| 655 | eq = co->co_firstlineno == cp->co_firstlineno; |
| 656 | if (!eq) goto unequal; |
| 657 | eq = PyObject_RichCompareBool(co->co_code, cp->co_code, Py_EQ); |
| 658 | if (eq <= 0) goto unequal; |
Victor Stinner | efb2413 | 2016-01-22 12:33:12 +0100 | [diff] [blame] | 659 | |
| 660 | /* compare constants */ |
| 661 | consts1 = _PyCode_ConstantKey(co->co_consts); |
| 662 | if (!consts1) |
| 663 | return NULL; |
| 664 | consts2 = _PyCode_ConstantKey(cp->co_consts); |
| 665 | if (!consts2) { |
| 666 | Py_DECREF(consts1); |
| 667 | return NULL; |
| 668 | } |
| 669 | eq = PyObject_RichCompareBool(consts1, consts2, Py_EQ); |
| 670 | Py_DECREF(consts1); |
| 671 | Py_DECREF(consts2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 672 | if (eq <= 0) goto unequal; |
Victor Stinner | efb2413 | 2016-01-22 12:33:12 +0100 | [diff] [blame] | 673 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 674 | eq = PyObject_RichCompareBool(co->co_names, cp->co_names, Py_EQ); |
| 675 | if (eq <= 0) goto unequal; |
| 676 | eq = PyObject_RichCompareBool(co->co_varnames, cp->co_varnames, Py_EQ); |
| 677 | if (eq <= 0) goto unequal; |
| 678 | eq = PyObject_RichCompareBool(co->co_freevars, cp->co_freevars, Py_EQ); |
| 679 | if (eq <= 0) goto unequal; |
| 680 | eq = PyObject_RichCompareBool(co->co_cellvars, cp->co_cellvars, Py_EQ); |
| 681 | if (eq <= 0) goto unequal; |
Guido van Rossum | b6bb0c7 | 2006-08-24 04:12:18 +0000 | [diff] [blame] | 682 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 683 | if (op == Py_EQ) |
| 684 | res = Py_True; |
| 685 | else |
| 686 | res = Py_False; |
| 687 | goto done; |
Guido van Rossum | b6bb0c7 | 2006-08-24 04:12:18 +0000 | [diff] [blame] | 688 | |
| 689 | unequal: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 690 | if (eq < 0) |
| 691 | return NULL; |
| 692 | if (op == Py_NE) |
| 693 | res = Py_True; |
| 694 | else |
| 695 | res = Py_False; |
Guido van Rossum | b6bb0c7 | 2006-08-24 04:12:18 +0000 | [diff] [blame] | 696 | |
| 697 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 698 | Py_INCREF(res); |
| 699 | return res; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 700 | } |
| 701 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 702 | static Py_hash_t |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 703 | code_hash(PyCodeObject *co) |
| 704 | { |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 705 | Py_hash_t h, h0, h1, h2, h3, h4, h5, h6; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 706 | h0 = PyObject_Hash(co->co_name); |
| 707 | if (h0 == -1) return -1; |
| 708 | h1 = PyObject_Hash(co->co_code); |
| 709 | if (h1 == -1) return -1; |
| 710 | h2 = PyObject_Hash(co->co_consts); |
| 711 | if (h2 == -1) return -1; |
| 712 | h3 = PyObject_Hash(co->co_names); |
| 713 | if (h3 == -1) return -1; |
| 714 | h4 = PyObject_Hash(co->co_varnames); |
| 715 | if (h4 == -1) return -1; |
| 716 | h5 = PyObject_Hash(co->co_freevars); |
| 717 | if (h5 == -1) return -1; |
| 718 | h6 = PyObject_Hash(co->co_cellvars); |
| 719 | if (h6 == -1) return -1; |
| 720 | h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^ |
| 721 | co->co_argcount ^ co->co_kwonlyargcount ^ |
| 722 | co->co_nlocals ^ co->co_flags; |
| 723 | if (h == -1) h = -2; |
| 724 | return h; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | /* XXX code objects need to participate in GC? */ |
| 728 | |
Martin v. Löwis | 3bbd2fa | 2012-07-26 22:23:23 +0200 | [diff] [blame] | 729 | static struct PyMethodDef code_methods[] = { |
| 730 | {"__sizeof__", (PyCFunction)code_sizeof, METH_NOARGS}, |
| 731 | {NULL, NULL} /* sentinel */ |
| 732 | }; |
| 733 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 734 | PyTypeObject PyCode_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 735 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 736 | "code", |
| 737 | sizeof(PyCodeObject), |
| 738 | 0, |
| 739 | (destructor)code_dealloc, /* tp_dealloc */ |
| 740 | 0, /* tp_print */ |
| 741 | 0, /* tp_getattr */ |
| 742 | 0, /* tp_setattr */ |
| 743 | 0, /* tp_reserved */ |
| 744 | (reprfunc)code_repr, /* tp_repr */ |
| 745 | 0, /* tp_as_number */ |
| 746 | 0, /* tp_as_sequence */ |
| 747 | 0, /* tp_as_mapping */ |
| 748 | (hashfunc)code_hash, /* tp_hash */ |
| 749 | 0, /* tp_call */ |
| 750 | 0, /* tp_str */ |
| 751 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 752 | 0, /* tp_setattro */ |
| 753 | 0, /* tp_as_buffer */ |
| 754 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 755 | code_doc, /* tp_doc */ |
| 756 | 0, /* tp_traverse */ |
| 757 | 0, /* tp_clear */ |
| 758 | code_richcompare, /* tp_richcompare */ |
| 759 | offsetof(PyCodeObject, co_weakreflist), /* tp_weaklistoffset */ |
| 760 | 0, /* tp_iter */ |
| 761 | 0, /* tp_iternext */ |
Martin v. Löwis | 3bbd2fa | 2012-07-26 22:23:23 +0200 | [diff] [blame] | 762 | code_methods, /* tp_methods */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 763 | code_memberlist, /* tp_members */ |
| 764 | 0, /* tp_getset */ |
| 765 | 0, /* tp_base */ |
| 766 | 0, /* tp_dict */ |
| 767 | 0, /* tp_descr_get */ |
| 768 | 0, /* tp_descr_set */ |
| 769 | 0, /* tp_dictoffset */ |
| 770 | 0, /* tp_init */ |
| 771 | 0, /* tp_alloc */ |
| 772 | code_new, /* tp_new */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 773 | }; |
| 774 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 775 | /* Use co_lnotab to compute the line number from a bytecode index, addrq. See |
| 776 | lnotab_notes.txt for the details of the lnotab representation. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 777 | */ |
| 778 | |
| 779 | int |
| 780 | PyCode_Addr2Line(PyCodeObject *co, int addrq) |
| 781 | { |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 782 | Py_ssize_t size = PyBytes_Size(co->co_lnotab) / 2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 783 | unsigned char *p = (unsigned char*)PyBytes_AsString(co->co_lnotab); |
| 784 | int line = co->co_firstlineno; |
| 785 | int addr = 0; |
| 786 | while (--size >= 0) { |
| 787 | addr += *p++; |
| 788 | if (addr > addrq) |
| 789 | break; |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 790 | line += (signed char)*p; |
| 791 | p++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 792 | } |
| 793 | return line; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 794 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 795 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 796 | /* Update *bounds to describe the first and one-past-the-last instructions in |
| 797 | the same line as lasti. Return the number of that line. */ |
| 798 | int |
| 799 | _PyCode_CheckLineNumber(PyCodeObject* co, int lasti, PyAddrPair *bounds) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 800 | { |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 801 | Py_ssize_t size; |
| 802 | int addr, line; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 803 | unsigned char* p; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 804 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 805 | p = (unsigned char*)PyBytes_AS_STRING(co->co_lnotab); |
| 806 | size = PyBytes_GET_SIZE(co->co_lnotab) / 2; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 807 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 808 | addr = 0; |
| 809 | line = co->co_firstlineno; |
| 810 | assert(line > 0); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 811 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 812 | /* possible optimization: if f->f_lasti == instr_ub |
| 813 | (likely to be a common case) then we already know |
| 814 | instr_lb -- if we stored the matching value of p |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 815 | somewhere we could skip the first while loop. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 816 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 817 | /* See lnotab_notes.txt for the description of |
| 818 | co_lnotab. A point to remember: increments to p |
| 819 | come in (addr, line) pairs. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 820 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 821 | bounds->ap_lower = 0; |
| 822 | while (size > 0) { |
| 823 | if (addr + *p > lasti) |
| 824 | break; |
| 825 | addr += *p++; |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 826 | if ((signed char)*p) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 827 | bounds->ap_lower = addr; |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 828 | line += (signed char)*p; |
| 829 | p++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 830 | --size; |
| 831 | } |
| 832 | |
| 833 | if (size > 0) { |
| 834 | while (--size >= 0) { |
| 835 | addr += *p++; |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 836 | if ((signed char)*p) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 837 | break; |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 838 | p++; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 839 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 840 | bounds->ap_upper = addr; |
| 841 | } |
| 842 | else { |
| 843 | bounds->ap_upper = INT_MAX; |
| 844 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 845 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 846 | return line; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 847 | } |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 848 | |
| 849 | |
| 850 | int |
| 851 | _PyCode_GetExtra(PyObject *code, Py_ssize_t index, void **extra) |
| 852 | { |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 853 | if (!PyCode_Check(code)) { |
| 854 | PyErr_BadInternalCall(); |
Brett Cannon | 3788b85 | 2016-09-07 12:51:08 -0700 | [diff] [blame] | 855 | return -1; |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 856 | } |
| 857 | |
Brett Cannon | d0600ed | 2016-09-07 14:30:39 -0700 | [diff] [blame] | 858 | PyCodeObject *o = (PyCodeObject*) code; |
| 859 | _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra*) o->co_extra; |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 860 | |
Brett Cannon | d0600ed | 2016-09-07 14:30:39 -0700 | [diff] [blame] | 861 | if (co_extra == NULL || co_extra->ce_size <= index) { |
Dino Viehland | f3cffd2 | 2017-06-21 14:44:36 -0700 | [diff] [blame] | 862 | *extra = NULL; |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 863 | return 0; |
| 864 | } |
| 865 | |
Brett Cannon | d0600ed | 2016-09-07 14:30:39 -0700 | [diff] [blame] | 866 | *extra = co_extra->ce_extras[index]; |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 867 | return 0; |
| 868 | } |
| 869 | |
| 870 | |
| 871 | int |
| 872 | _PyCode_SetExtra(PyObject *code, Py_ssize_t index, void *extra) |
| 873 | { |
Dino Viehland | f3cffd2 | 2017-06-21 14:44:36 -0700 | [diff] [blame] | 874 | PyInterpreterState *interp = PyThreadState_Get()->interp; |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 875 | |
| 876 | if (!PyCode_Check(code) || index < 0 || |
Dino Viehland | f3cffd2 | 2017-06-21 14:44:36 -0700 | [diff] [blame] | 877 | index >= interp->co_extra_user_count) { |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 878 | PyErr_BadInternalCall(); |
Brett Cannon | 3788b85 | 2016-09-07 12:51:08 -0700 | [diff] [blame] | 879 | return -1; |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 880 | } |
| 881 | |
Brett Cannon | d0600ed | 2016-09-07 14:30:39 -0700 | [diff] [blame] | 882 | PyCodeObject *o = (PyCodeObject*) code; |
| 883 | _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra *) o->co_extra; |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 884 | |
Serhiy Storchaka | 378ebb6 | 2017-07-04 15:06:16 +0300 | [diff] [blame] | 885 | if (co_extra == NULL || co_extra->ce_size <= index) { |
| 886 | Py_ssize_t i = (co_extra == NULL ? 0 : co_extra->ce_size); |
| 887 | co_extra = PyMem_Realloc( |
| 888 | co_extra, |
| 889 | sizeof(_PyCodeObjectExtra) + |
| 890 | (interp->co_extra_user_count-1) * sizeof(void*)); |
Brian Coleman | 6a9122c | 2017-03-02 10:32:18 +0000 | [diff] [blame] | 891 | if (co_extra == NULL) { |
Brett Cannon | 3788b85 | 2016-09-07 12:51:08 -0700 | [diff] [blame] | 892 | return -1; |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 893 | } |
Serhiy Storchaka | 378ebb6 | 2017-07-04 15:06:16 +0300 | [diff] [blame] | 894 | for (; i < interp->co_extra_user_count; i++) { |
Brett Cannon | d0600ed | 2016-09-07 14:30:39 -0700 | [diff] [blame] | 895 | co_extra->ce_extras[i] = NULL; |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 896 | } |
Dino Viehland | f3cffd2 | 2017-06-21 14:44:36 -0700 | [diff] [blame] | 897 | co_extra->ce_size = interp->co_extra_user_count; |
Serhiy Storchaka | 378ebb6 | 2017-07-04 15:06:16 +0300 | [diff] [blame] | 898 | o->co_extra = co_extra; |
Dino Viehland | f3cffd2 | 2017-06-21 14:44:36 -0700 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | if (co_extra->ce_extras[index] != NULL) { |
| 902 | freefunc free = interp->co_extra_freefuncs[index]; |
Dino Viehland | f3cffd2 | 2017-06-21 14:44:36 -0700 | [diff] [blame] | 903 | if (free != NULL) { |
| 904 | free(co_extra->ce_extras[index]); |
| 905 | } |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 906 | } |
| 907 | |
Brett Cannon | d0600ed | 2016-09-07 14:30:39 -0700 | [diff] [blame] | 908 | co_extra->ce_extras[index] = extra; |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 909 | return 0; |
| 910 | } |