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