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