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