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