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