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