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" |
Victor Stinner | 4a21e57 | 2020-04-15 02:35:41 +0200 | [diff] [blame] | 6 | #include "structmember.h" // PyMemberDef |
Victor Stinner | 384621c | 2020-06-22 17:27:35 +0200 | [diff] [blame] | 7 | #include "pycore_code.h" // _PyOpcache |
Victor Stinner | 4a3fe08 | 2020-04-14 14:26:24 +0200 | [diff] [blame] | 8 | #include "pycore_interp.h" // PyInterpreterState.co_extra_freefuncs |
Victor Stinner | 81a7be3 | 2020-04-14 15:14:01 +0200 | [diff] [blame] | 9 | #include "pycore_pystate.h" // _PyInterpreterState_GET() |
Victor Stinner | 384621c | 2020-06-22 17:27:35 +0200 | [diff] [blame] | 10 | #include "pycore_tuple.h" // _PyTuple_ITEMS() |
Victor Stinner | a9f05d6 | 2019-05-24 23:57:23 +0200 | [diff] [blame] | 11 | #include "clinic/codeobject.c.h" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 12 | |
Brett Cannon | d0600ed | 2016-09-07 14:30:39 -0700 | [diff] [blame] | 13 | /* Holder for co_extra information */ |
| 14 | typedef struct { |
| 15 | Py_ssize_t ce_size; |
Serhiy Storchaka | 378ebb6 | 2017-07-04 15:06:16 +0300 | [diff] [blame] | 16 | void *ce_extras[1]; |
Brett Cannon | d0600ed | 2016-09-07 14:30:39 -0700 | [diff] [blame] | 17 | } _PyCodeObjectExtra; |
| 18 | |
Victor Stinner | a9f05d6 | 2019-05-24 23:57:23 +0200 | [diff] [blame] | 19 | /*[clinic input] |
| 20 | class code "PyCodeObject *" "&PyCode_Type" |
| 21 | [clinic start generated code]*/ |
| 22 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=78aa5d576683bb4b]*/ |
| 23 | |
Benjamin Peterson | 8e0ad46 | 2017-09-07 23:35:53 -0700 | [diff] [blame] | 24 | /* all_name_chars(s): true iff s matches [a-zA-Z0-9_]* */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 25 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 26 | all_name_chars(PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 27 | { |
Serhiy Storchaka | 09f3d08 | 2016-10-04 18:17:22 +0300 | [diff] [blame] | 28 | const unsigned char *s, *e; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 29 | |
Serhiy Storchaka | e3b2b4b | 2017-09-08 09:58:51 +0300 | [diff] [blame] | 30 | if (!PyUnicode_IS_ASCII(o)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 31 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 32 | |
Serhiy Storchaka | 09f3d08 | 2016-10-04 18:17:22 +0300 | [diff] [blame] | 33 | s = PyUnicode_1BYTE_DATA(o); |
| 34 | e = s + PyUnicode_GET_LENGTH(o); |
Benjamin Peterson | 9020ac7 | 2017-09-07 18:06:23 -0700 | [diff] [blame] | 35 | for (; s != e; s++) { |
Benjamin Peterson | 2b7953d | 2017-09-08 10:35:49 -0700 | [diff] [blame] | 36 | if (!Py_ISALNUM(*s) && *s != '_') |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 37 | return 0; |
| 38 | } |
| 39 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Victor Stinner | a278313 | 2020-01-27 23:24:13 +0100 | [diff] [blame] | 42 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 43 | intern_strings(PyObject *tuple) |
| 44 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 45 | Py_ssize_t i; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 46 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 47 | for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) { |
| 48 | PyObject *v = PyTuple_GET_ITEM(tuple, i); |
| 49 | if (v == NULL || !PyUnicode_CheckExact(v)) { |
Victor Stinner | a278313 | 2020-01-27 23:24:13 +0100 | [diff] [blame] | 50 | PyErr_SetString(PyExc_SystemError, |
| 51 | "non-string found in code slot"); |
| 52 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 53 | } |
Victor Stinner | d17a693 | 2018-11-09 16:56:48 +0100 | [diff] [blame] | 54 | PyUnicode_InternInPlace(&_PyTuple_ITEMS(tuple)[i]); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 55 | } |
Victor Stinner | a278313 | 2020-01-27 23:24:13 +0100 | [diff] [blame] | 56 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Serhiy Storchaka | 00a0fc1 | 2016-09-30 10:07:26 +0300 | [diff] [blame] | 59 | /* Intern selected string constants */ |
| 60 | static int |
Victor Stinner | a278313 | 2020-01-27 23:24:13 +0100 | [diff] [blame] | 61 | intern_string_constants(PyObject *tuple, int *modified) |
Serhiy Storchaka | 00a0fc1 | 2016-09-30 10:07:26 +0300 | [diff] [blame] | 62 | { |
Victor Stinner | a278313 | 2020-01-27 23:24:13 +0100 | [diff] [blame] | 63 | for (Py_ssize_t i = PyTuple_GET_SIZE(tuple); --i >= 0; ) { |
Serhiy Storchaka | 00a0fc1 | 2016-09-30 10:07:26 +0300 | [diff] [blame] | 64 | PyObject *v = PyTuple_GET_ITEM(tuple, i); |
| 65 | if (PyUnicode_CheckExact(v)) { |
Serhiy Storchaka | e3b2b4b | 2017-09-08 09:58:51 +0300 | [diff] [blame] | 66 | if (PyUnicode_READY(v) == -1) { |
Victor Stinner | a278313 | 2020-01-27 23:24:13 +0100 | [diff] [blame] | 67 | return -1; |
Serhiy Storchaka | e3b2b4b | 2017-09-08 09:58:51 +0300 | [diff] [blame] | 68 | } |
Victor Stinner | a278313 | 2020-01-27 23:24:13 +0100 | [diff] [blame] | 69 | |
Serhiy Storchaka | 00a0fc1 | 2016-09-30 10:07:26 +0300 | [diff] [blame] | 70 | if (all_name_chars(v)) { |
| 71 | PyObject *w = v; |
| 72 | PyUnicode_InternInPlace(&v); |
| 73 | if (w != v) { |
| 74 | PyTuple_SET_ITEM(tuple, i, v); |
Victor Stinner | a278313 | 2020-01-27 23:24:13 +0100 | [diff] [blame] | 75 | if (modified) { |
| 76 | *modified = 1; |
| 77 | } |
Serhiy Storchaka | 00a0fc1 | 2016-09-30 10:07:26 +0300 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | } |
| 81 | else if (PyTuple_CheckExact(v)) { |
Victor Stinner | a278313 | 2020-01-27 23:24:13 +0100 | [diff] [blame] | 82 | if (intern_string_constants(v, NULL) < 0) { |
| 83 | return -1; |
| 84 | } |
Serhiy Storchaka | 00a0fc1 | 2016-09-30 10:07:26 +0300 | [diff] [blame] | 85 | } |
| 86 | else if (PyFrozenSet_CheckExact(v)) { |
Yury Selivanov | d2fd359 | 2016-11-09 09:42:14 -0500 | [diff] [blame] | 87 | PyObject *w = v; |
Serhiy Storchaka | 00a0fc1 | 2016-09-30 10:07:26 +0300 | [diff] [blame] | 88 | PyObject *tmp = PySequence_Tuple(v); |
| 89 | if (tmp == NULL) { |
Victor Stinner | a278313 | 2020-01-27 23:24:13 +0100 | [diff] [blame] | 90 | return -1; |
Serhiy Storchaka | 00a0fc1 | 2016-09-30 10:07:26 +0300 | [diff] [blame] | 91 | } |
Victor Stinner | a278313 | 2020-01-27 23:24:13 +0100 | [diff] [blame] | 92 | int tmp_modified = 0; |
| 93 | if (intern_string_constants(tmp, &tmp_modified) < 0) { |
| 94 | Py_DECREF(tmp); |
| 95 | return -1; |
| 96 | } |
| 97 | if (tmp_modified) { |
Serhiy Storchaka | 00a0fc1 | 2016-09-30 10:07:26 +0300 | [diff] [blame] | 98 | v = PyFrozenSet_New(tmp); |
| 99 | if (v == NULL) { |
Victor Stinner | a278313 | 2020-01-27 23:24:13 +0100 | [diff] [blame] | 100 | Py_DECREF(tmp); |
| 101 | return -1; |
Serhiy Storchaka | 00a0fc1 | 2016-09-30 10:07:26 +0300 | [diff] [blame] | 102 | } |
Victor Stinner | a278313 | 2020-01-27 23:24:13 +0100 | [diff] [blame] | 103 | |
| 104 | PyTuple_SET_ITEM(tuple, i, v); |
| 105 | Py_DECREF(w); |
| 106 | if (modified) { |
| 107 | *modified = 1; |
Serhiy Storchaka | 00a0fc1 | 2016-09-30 10:07:26 +0300 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | Py_DECREF(tmp); |
| 111 | } |
| 112 | } |
Victor Stinner | a278313 | 2020-01-27 23:24:13 +0100 | [diff] [blame] | 113 | return 0; |
Serhiy Storchaka | 00a0fc1 | 2016-09-30 10:07:26 +0300 | [diff] [blame] | 114 | } |
| 115 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 116 | PyCodeObject * |
Pablo Galindo | 4a2edc3 | 2019-07-01 11:35:05 +0100 | [diff] [blame] | 117 | PyCode_NewWithPosOnlyArgs(int argcount, int posonlyargcount, int kwonlyargcount, |
| 118 | int nlocals, int stacksize, int flags, |
| 119 | PyObject *code, PyObject *consts, PyObject *names, |
| 120 | PyObject *varnames, PyObject *freevars, PyObject *cellvars, |
| 121 | PyObject *filename, PyObject *name, int firstlineno, |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 122 | PyObject *linetable) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 123 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 124 | PyCodeObject *co; |
Serhiy Storchaka | 5bb8b91 | 2016-12-16 19:19:02 +0200 | [diff] [blame] | 125 | Py_ssize_t *cell2arg = NULL; |
Serhiy Storchaka | bd47384 | 2018-07-16 09:10:19 +0300 | [diff] [blame] | 126 | Py_ssize_t i, n_cellvars, n_varnames, total_args; |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 127 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 128 | /* Check argument types */ |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 129 | if (argcount < posonlyargcount || posonlyargcount < 0 || |
| 130 | kwonlyargcount < 0 || nlocals < 0 || |
| 131 | stacksize < 0 || flags < 0 || |
Victor Stinner | a9f05d6 | 2019-05-24 23:57:23 +0200 | [diff] [blame] | 132 | code == NULL || !PyBytes_Check(code) || |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 133 | consts == NULL || !PyTuple_Check(consts) || |
| 134 | names == NULL || !PyTuple_Check(names) || |
| 135 | varnames == NULL || !PyTuple_Check(varnames) || |
| 136 | freevars == NULL || !PyTuple_Check(freevars) || |
| 137 | cellvars == NULL || !PyTuple_Check(cellvars) || |
| 138 | name == NULL || !PyUnicode_Check(name) || |
| 139 | filename == NULL || !PyUnicode_Check(filename) || |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 140 | linetable == NULL || !PyBytes_Check(linetable)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 141 | PyErr_BadInternalCall(); |
| 142 | return NULL; |
| 143 | } |
Victor Stinner | 7c74de4 | 2013-10-10 15:55:14 +0200 | [diff] [blame] | 144 | |
Victor Stinner | a9f05d6 | 2019-05-24 23:57:23 +0200 | [diff] [blame] | 145 | /* Ensure that strings are ready Unicode string */ |
| 146 | if (PyUnicode_READY(name) < 0) { |
Victor Stinner | 7c74de4 | 2013-10-10 15:55:14 +0200 | [diff] [blame] | 147 | return NULL; |
Victor Stinner | a9f05d6 | 2019-05-24 23:57:23 +0200 | [diff] [blame] | 148 | } |
| 149 | if (PyUnicode_READY(filename) < 0) { |
| 150 | return NULL; |
| 151 | } |
Victor Stinner | 7c74de4 | 2013-10-10 15:55:14 +0200 | [diff] [blame] | 152 | |
Victor Stinner | a278313 | 2020-01-27 23:24:13 +0100 | [diff] [blame] | 153 | if (intern_strings(names) < 0) { |
| 154 | return NULL; |
| 155 | } |
| 156 | if (intern_strings(varnames) < 0) { |
| 157 | return NULL; |
| 158 | } |
| 159 | if (intern_strings(freevars) < 0) { |
| 160 | return NULL; |
| 161 | } |
| 162 | if (intern_strings(cellvars) < 0) { |
| 163 | return NULL; |
| 164 | } |
| 165 | if (intern_string_constants(consts, NULL) < 0) { |
| 166 | return NULL; |
| 167 | } |
Nick Coghlan | 078f181 | 2017-12-03 11:12:20 +1000 | [diff] [blame] | 168 | |
Ammar Askar | 3b3b83c | 2020-06-10 23:31:22 +0000 | [diff] [blame] | 169 | /* Make sure that code is indexable with an int, this is |
| 170 | a long running assumption in ceval.c and many parts of |
| 171 | the interpreter. */ |
| 172 | if (PyBytes_GET_SIZE(code) > INT_MAX) { |
| 173 | PyErr_SetString(PyExc_OverflowError, "co_code larger than INT_MAX"); |
| 174 | return NULL; |
| 175 | } |
| 176 | |
Nick Coghlan | 078f181 | 2017-12-03 11:12:20 +1000 | [diff] [blame] | 177 | /* Check for any inner or outer closure references */ |
| 178 | n_cellvars = PyTuple_GET_SIZE(cellvars); |
| 179 | if (!n_cellvars && !PyTuple_GET_SIZE(freevars)) { |
| 180 | flags |= CO_NOFREE; |
| 181 | } else { |
| 182 | flags &= ~CO_NOFREE; |
| 183 | } |
| 184 | |
Serhiy Storchaka | bd47384 | 2018-07-16 09:10:19 +0300 | [diff] [blame] | 185 | n_varnames = PyTuple_GET_SIZE(varnames); |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 186 | if (argcount <= n_varnames && kwonlyargcount <= n_varnames) { |
Serhiy Storchaka | bd47384 | 2018-07-16 09:10:19 +0300 | [diff] [blame] | 187 | /* Never overflows. */ |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 188 | total_args = (Py_ssize_t)argcount + (Py_ssize_t)kwonlyargcount + |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 189 | ((flags & CO_VARARGS) != 0) + ((flags & CO_VARKEYWORDS) != 0); |
Serhiy Storchaka | bd47384 | 2018-07-16 09:10:19 +0300 | [diff] [blame] | 190 | } |
| 191 | else { |
| 192 | total_args = n_varnames + 1; |
| 193 | } |
| 194 | if (total_args > n_varnames) { |
| 195 | PyErr_SetString(PyExc_ValueError, "code: varnames is too small"); |
| 196 | return NULL; |
| 197 | } |
| 198 | |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 199 | /* Create mapping between cells and arguments if needed. */ |
| 200 | if (n_cellvars) { |
Benjamin Peterson | 1bf494b | 2016-09-07 11:28:35 -0700 | [diff] [blame] | 201 | bool used_cell2arg = false; |
Serhiy Storchaka | 5bb8b91 | 2016-12-16 19:19:02 +0200 | [diff] [blame] | 202 | cell2arg = PyMem_NEW(Py_ssize_t, n_cellvars); |
| 203 | if (cell2arg == NULL) { |
| 204 | PyErr_NoMemory(); |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 205 | return NULL; |
Serhiy Storchaka | 5bb8b91 | 2016-12-16 19:19:02 +0200 | [diff] [blame] | 206 | } |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 207 | /* Find cells which are also arguments. */ |
| 208 | for (i = 0; i < n_cellvars; i++) { |
| 209 | Py_ssize_t j; |
| 210 | PyObject *cell = PyTuple_GET_ITEM(cellvars, i); |
Serhiy Storchaka | 5bb8b91 | 2016-12-16 19:19:02 +0200 | [diff] [blame] | 211 | cell2arg[i] = CO_CELL_NOT_AN_ARG; |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 212 | for (j = 0; j < total_args; j++) { |
| 213 | PyObject *arg = PyTuple_GET_ITEM(varnames, j); |
Serhiy Storchaka | 5bb8b91 | 2016-12-16 19:19:02 +0200 | [diff] [blame] | 214 | int cmp = PyUnicode_Compare(cell, arg); |
| 215 | if (cmp == -1 && PyErr_Occurred()) { |
Victor Stinner | 00d7abd | 2020-12-01 09:56:42 +0100 | [diff] [blame] | 216 | PyMem_Free(cell2arg); |
Serhiy Storchaka | 5bb8b91 | 2016-12-16 19:19:02 +0200 | [diff] [blame] | 217 | return NULL; |
| 218 | } |
| 219 | if (cmp == 0) { |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 220 | cell2arg[i] = j; |
Benjamin Peterson | 1bf494b | 2016-09-07 11:28:35 -0700 | [diff] [blame] | 221 | used_cell2arg = true; |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 222 | break; |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | if (!used_cell2arg) { |
Victor Stinner | 00d7abd | 2020-12-01 09:56:42 +0100 | [diff] [blame] | 227 | PyMem_Free(cell2arg); |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 228 | cell2arg = NULL; |
| 229 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 230 | } |
Victor Stinner | 9205520 | 2020-04-08 00:38:15 +0200 | [diff] [blame] | 231 | co = PyObject_New(PyCodeObject, &PyCode_Type); |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 232 | if (co == NULL) { |
| 233 | if (cell2arg) |
Victor Stinner | 00d7abd | 2020-12-01 09:56:42 +0100 | [diff] [blame] | 234 | PyMem_Free(cell2arg); |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 235 | return NULL; |
| 236 | } |
| 237 | co->co_argcount = argcount; |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 238 | co->co_posonlyargcount = posonlyargcount; |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 239 | co->co_kwonlyargcount = kwonlyargcount; |
| 240 | co->co_nlocals = nlocals; |
| 241 | co->co_stacksize = stacksize; |
| 242 | co->co_flags = flags; |
| 243 | Py_INCREF(code); |
| 244 | co->co_code = code; |
| 245 | Py_INCREF(consts); |
| 246 | co->co_consts = consts; |
| 247 | Py_INCREF(names); |
| 248 | co->co_names = names; |
| 249 | Py_INCREF(varnames); |
| 250 | co->co_varnames = varnames; |
| 251 | Py_INCREF(freevars); |
| 252 | co->co_freevars = freevars; |
| 253 | Py_INCREF(cellvars); |
| 254 | co->co_cellvars = cellvars; |
| 255 | co->co_cell2arg = cell2arg; |
| 256 | Py_INCREF(filename); |
| 257 | co->co_filename = filename; |
| 258 | Py_INCREF(name); |
| 259 | co->co_name = name; |
| 260 | co->co_firstlineno = firstlineno; |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 261 | Py_INCREF(linetable); |
| 262 | co->co_linetable = linetable; |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 263 | co->co_zombieframe = NULL; |
| 264 | co->co_weakreflist = NULL; |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 265 | co->co_extra = NULL; |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 266 | |
| 267 | co->co_opcache_map = NULL; |
| 268 | co->co_opcache = NULL; |
| 269 | co->co_opcache_flag = 0; |
| 270 | co->co_opcache_size = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 271 | return co; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Pablo Galindo | 4a2edc3 | 2019-07-01 11:35:05 +0100 | [diff] [blame] | 274 | PyCodeObject * |
| 275 | PyCode_New(int argcount, int kwonlyargcount, |
| 276 | int nlocals, int stacksize, int flags, |
| 277 | PyObject *code, PyObject *consts, PyObject *names, |
| 278 | PyObject *varnames, PyObject *freevars, PyObject *cellvars, |
| 279 | PyObject *filename, PyObject *name, int firstlineno, |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 280 | PyObject *linetable) |
Pablo Galindo | 4a2edc3 | 2019-07-01 11:35:05 +0100 | [diff] [blame] | 281 | { |
| 282 | return PyCode_NewWithPosOnlyArgs(argcount, 0, kwonlyargcount, nlocals, |
| 283 | stacksize, flags, code, consts, names, |
| 284 | varnames, freevars, cellvars, filename, |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 285 | name, firstlineno, linetable); |
Pablo Galindo | 4a2edc3 | 2019-07-01 11:35:05 +0100 | [diff] [blame] | 286 | } |
| 287 | |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 288 | int |
| 289 | _PyCode_InitOpcache(PyCodeObject *co) |
| 290 | { |
| 291 | Py_ssize_t co_size = PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT); |
| 292 | co->co_opcache_map = (unsigned char *)PyMem_Calloc(co_size, 1); |
| 293 | if (co->co_opcache_map == NULL) { |
| 294 | return -1; |
| 295 | } |
| 296 | |
| 297 | _Py_CODEUNIT *opcodes = (_Py_CODEUNIT*)PyBytes_AS_STRING(co->co_code); |
| 298 | Py_ssize_t opts = 0; |
| 299 | |
| 300 | for (Py_ssize_t i = 0; i < co_size;) { |
| 301 | unsigned char opcode = _Py_OPCODE(opcodes[i]); |
| 302 | i++; // 'i' is now aligned to (next_instr - first_instr) |
| 303 | |
Pablo Galindo | 109826c | 2020-10-20 06:22:44 +0100 | [diff] [blame] | 304 | // TODO: LOAD_METHOD |
| 305 | if (opcode == LOAD_GLOBAL || opcode == LOAD_ATTR) { |
Victor Stinner | ea9f168 | 2019-06-04 17:08:24 +0200 | [diff] [blame] | 306 | opts++; |
| 307 | co->co_opcache_map[i] = (unsigned char)opts; |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 308 | if (opts > 254) { |
| 309 | break; |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | if (opts) { |
| 315 | co->co_opcache = (_PyOpcache *)PyMem_Calloc(opts, sizeof(_PyOpcache)); |
| 316 | if (co->co_opcache == NULL) { |
Victor Stinner | 00d7abd | 2020-12-01 09:56:42 +0100 | [diff] [blame] | 317 | PyMem_Free(co->co_opcache_map); |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 318 | return -1; |
| 319 | } |
| 320 | } |
| 321 | else { |
Victor Stinner | 00d7abd | 2020-12-01 09:56:42 +0100 | [diff] [blame] | 322 | PyMem_Free(co->co_opcache_map); |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 323 | co->co_opcache_map = NULL; |
| 324 | co->co_opcache = NULL; |
| 325 | } |
| 326 | |
Victor Stinner | 376ce98 | 2019-06-12 04:41:16 +0200 | [diff] [blame] | 327 | co->co_opcache_size = (unsigned char)opts; |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 328 | return 0; |
| 329 | } |
| 330 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 331 | PyCodeObject * |
| 332 | PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno) |
| 333 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 334 | static PyObject *emptystring = NULL; |
| 335 | static PyObject *nulltuple = NULL; |
| 336 | PyObject *filename_ob = NULL; |
| 337 | PyObject *funcname_ob = NULL; |
| 338 | PyCodeObject *result = NULL; |
| 339 | if (emptystring == NULL) { |
| 340 | emptystring = PyBytes_FromString(""); |
| 341 | if (emptystring == NULL) |
| 342 | goto failed; |
| 343 | } |
| 344 | if (nulltuple == NULL) { |
| 345 | nulltuple = PyTuple_New(0); |
| 346 | if (nulltuple == NULL) |
| 347 | goto failed; |
| 348 | } |
| 349 | funcname_ob = PyUnicode_FromString(funcname); |
| 350 | if (funcname_ob == NULL) |
| 351 | goto failed; |
| 352 | filename_ob = PyUnicode_DecodeFSDefault(filename); |
| 353 | if (filename_ob == NULL) |
| 354 | goto failed; |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 355 | |
Pablo Galindo | 4a2edc3 | 2019-07-01 11:35:05 +0100 | [diff] [blame] | 356 | result = PyCode_NewWithPosOnlyArgs( |
| 357 | 0, /* argcount */ |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 358 | 0, /* posonlyargcount */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 359 | 0, /* kwonlyargcount */ |
| 360 | 0, /* nlocals */ |
| 361 | 0, /* stacksize */ |
| 362 | 0, /* flags */ |
| 363 | emptystring, /* code */ |
| 364 | nulltuple, /* consts */ |
| 365 | nulltuple, /* names */ |
| 366 | nulltuple, /* varnames */ |
| 367 | nulltuple, /* freevars */ |
| 368 | nulltuple, /* cellvars */ |
| 369 | filename_ob, /* filename */ |
| 370 | funcname_ob, /* name */ |
| 371 | firstlineno, /* firstlineno */ |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 372 | emptystring /* linetable */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 373 | ); |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 374 | |
| 375 | failed: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 376 | Py_XDECREF(funcname_ob); |
| 377 | Py_XDECREF(filename_ob); |
| 378 | return result; |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 379 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 380 | |
| 381 | #define OFF(x) offsetof(PyCodeObject, x) |
| 382 | |
| 383 | static PyMemberDef code_memberlist[] = { |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 384 | {"co_argcount", T_INT, OFF(co_argcount), READONLY}, |
| 385 | {"co_posonlyargcount", T_INT, OFF(co_posonlyargcount), READONLY}, |
| 386 | {"co_kwonlyargcount", T_INT, OFF(co_kwonlyargcount), READONLY}, |
| 387 | {"co_nlocals", T_INT, OFF(co_nlocals), READONLY}, |
| 388 | {"co_stacksize",T_INT, OFF(co_stacksize), READONLY}, |
| 389 | {"co_flags", T_INT, OFF(co_flags), READONLY}, |
| 390 | {"co_code", T_OBJECT, OFF(co_code), READONLY}, |
| 391 | {"co_consts", T_OBJECT, OFF(co_consts), READONLY}, |
| 392 | {"co_names", T_OBJECT, OFF(co_names), READONLY}, |
| 393 | {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY}, |
| 394 | {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY}, |
| 395 | {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY}, |
| 396 | {"co_filename", T_OBJECT, OFF(co_filename), READONLY}, |
| 397 | {"co_name", T_OBJECT, OFF(co_name), READONLY}, |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 398 | {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY}, |
| 399 | {"co_linetable", T_OBJECT, OFF(co_linetable), READONLY}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 400 | {NULL} /* Sentinel */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 401 | }; |
| 402 | |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 403 | static int |
| 404 | emit_pair(PyObject **bytes, int *offset, int a, int b) |
| 405 | { |
| 406 | Py_ssize_t len = PyBytes_GET_SIZE(*bytes); |
| 407 | if (*offset + 2 >= len) { |
| 408 | if (_PyBytes_Resize(bytes, len * 2) < 0) |
| 409 | return 0; |
| 410 | } |
Pablo Galindo | 86e322f | 2021-01-30 13:54:22 +0000 | [diff] [blame] | 411 | unsigned char *lnotab = (unsigned char *) PyBytes_AS_STRING(*bytes); |
| 412 | lnotab += *offset; |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 413 | *lnotab++ = a; |
| 414 | *lnotab++ = b; |
| 415 | *offset += 2; |
| 416 | return 1; |
| 417 | } |
| 418 | |
| 419 | static int |
| 420 | emit_delta(PyObject **bytes, int bdelta, int ldelta, int *offset) |
| 421 | { |
| 422 | while (bdelta > 255) { |
| 423 | if (!emit_pair(bytes, offset, 255, 0)) { |
| 424 | return 0; |
| 425 | } |
| 426 | bdelta -= 255; |
| 427 | } |
| 428 | while (ldelta > 127) { |
| 429 | if (!emit_pair(bytes, offset, bdelta, 127)) { |
| 430 | return 0; |
| 431 | } |
| 432 | bdelta = 0; |
| 433 | ldelta -= 127; |
| 434 | } |
| 435 | while (ldelta < -128) { |
| 436 | if (!emit_pair(bytes, offset, bdelta, -128)) { |
| 437 | return 0; |
| 438 | } |
| 439 | bdelta = 0; |
| 440 | ldelta += 128; |
| 441 | } |
| 442 | return emit_pair(bytes, offset, bdelta, ldelta); |
| 443 | } |
| 444 | |
| 445 | static PyObject * |
| 446 | code_getlnotab(PyCodeObject *code, void *closure) |
| 447 | { |
| 448 | PyCodeAddressRange bounds; |
| 449 | PyObject *bytes; |
| 450 | int table_offset = 0; |
| 451 | int code_offset = 0; |
| 452 | int line = code->co_firstlineno; |
| 453 | bytes = PyBytes_FromStringAndSize(NULL, 64); |
| 454 | if (bytes == NULL) { |
| 455 | return NULL; |
| 456 | } |
| 457 | _PyCode_InitAddressRange(code, &bounds); |
| 458 | while (PyLineTable_NextAddressRange(&bounds)) { |
| 459 | if (bounds.ar_computed_line != line) { |
| 460 | int bdelta = bounds.ar_start - code_offset; |
| 461 | int ldelta = bounds.ar_computed_line - line; |
| 462 | if (!emit_delta(&bytes, bdelta, ldelta, &table_offset)) { |
| 463 | Py_DECREF(bytes); |
| 464 | return NULL; |
| 465 | } |
| 466 | code_offset = bounds.ar_start; |
| 467 | line = bounds.ar_computed_line; |
| 468 | } |
| 469 | } |
| 470 | _PyBytes_Resize(&bytes, table_offset); |
| 471 | return bytes; |
| 472 | } |
| 473 | |
| 474 | |
| 475 | static PyGetSetDef code_getsetlist[] = { |
| 476 | {"co_lnotab", (getter)code_getlnotab, NULL, NULL}, |
| 477 | {0} |
| 478 | }; |
| 479 | |
| 480 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 481 | /* Helper for code_new: return a shallow copy of a tuple that is |
| 482 | guaranteed to contain exact strings, by converting string subclasses |
| 483 | to exact strings and complaining if a non-string is found. */ |
| 484 | static PyObject* |
| 485 | validate_and_copy_tuple(PyObject *tup) |
| 486 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 487 | PyObject *newtuple; |
| 488 | PyObject *item; |
| 489 | Py_ssize_t i, len; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 490 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 491 | len = PyTuple_GET_SIZE(tup); |
| 492 | newtuple = PyTuple_New(len); |
| 493 | if (newtuple == NULL) |
| 494 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 495 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 496 | for (i = 0; i < len; i++) { |
| 497 | item = PyTuple_GET_ITEM(tup, i); |
| 498 | if (PyUnicode_CheckExact(item)) { |
| 499 | Py_INCREF(item); |
| 500 | } |
| 501 | else if (!PyUnicode_Check(item)) { |
| 502 | PyErr_Format( |
| 503 | PyExc_TypeError, |
| 504 | "name tuples must contain only " |
| 505 | "strings, not '%.500s'", |
Victor Stinner | 58ac700 | 2020-02-07 03:04:21 +0100 | [diff] [blame] | 506 | Py_TYPE(item)->tp_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 507 | Py_DECREF(newtuple); |
| 508 | return NULL; |
| 509 | } |
| 510 | else { |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 511 | item = _PyUnicode_Copy(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 512 | if (item == NULL) { |
| 513 | Py_DECREF(newtuple); |
| 514 | return NULL; |
| 515 | } |
| 516 | } |
| 517 | PyTuple_SET_ITEM(newtuple, i, item); |
| 518 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 519 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 520 | return newtuple; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Serhiy Storchaka | 0f9aa47 | 2020-07-10 10:12:04 +0300 | [diff] [blame] | 523 | /*[clinic input] |
| 524 | @classmethod |
| 525 | code.__new__ as code_new |
| 526 | |
| 527 | argcount: int |
| 528 | posonlyargcount: int |
| 529 | kwonlyargcount: int |
| 530 | nlocals: int |
| 531 | stacksize: int |
| 532 | flags: int |
| 533 | codestring as code: object(subclass_of="&PyBytes_Type") |
| 534 | constants as consts: object(subclass_of="&PyTuple_Type") |
| 535 | names: object(subclass_of="&PyTuple_Type") |
| 536 | varnames: object(subclass_of="&PyTuple_Type") |
| 537 | filename: unicode |
| 538 | name: unicode |
| 539 | firstlineno: int |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 540 | linetable: object(subclass_of="&PyBytes_Type") |
Serhiy Storchaka | 0f9aa47 | 2020-07-10 10:12:04 +0300 | [diff] [blame] | 541 | freevars: object(subclass_of="&PyTuple_Type", c_default="NULL") = () |
| 542 | cellvars: object(subclass_of="&PyTuple_Type", c_default="NULL") = () |
| 543 | / |
| 544 | |
| 545 | Create a code object. Not for the faint of heart. |
| 546 | [clinic start generated code]*/ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 547 | |
| 548 | static PyObject * |
Serhiy Storchaka | 0f9aa47 | 2020-07-10 10:12:04 +0300 | [diff] [blame] | 549 | code_new_impl(PyTypeObject *type, int argcount, int posonlyargcount, |
| 550 | int kwonlyargcount, int nlocals, int stacksize, int flags, |
| 551 | PyObject *code, PyObject *consts, PyObject *names, |
| 552 | PyObject *varnames, PyObject *filename, PyObject *name, |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 553 | int firstlineno, PyObject *linetable, PyObject *freevars, |
Serhiy Storchaka | 0f9aa47 | 2020-07-10 10:12:04 +0300 | [diff] [blame] | 554 | PyObject *cellvars) |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 555 | /*[clinic end generated code: output=42c1839b082ba293 input=0ec80da632b99f57]*/ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 556 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 557 | PyObject *co = NULL; |
Serhiy Storchaka | 0f9aa47 | 2020-07-10 10:12:04 +0300 | [diff] [blame] | 558 | PyObject *ournames = NULL; |
| 559 | PyObject *ourvarnames = NULL; |
| 560 | PyObject *ourfreevars = NULL; |
| 561 | PyObject *ourcellvars = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 562 | |
Pablo Galindo | 3b57f50 | 2019-06-01 21:18:48 +0100 | [diff] [blame] | 563 | if (PySys_Audit("code.__new__", "OOOiiiiii", |
| 564 | code, filename, name, argcount, posonlyargcount, |
| 565 | kwonlyargcount, nlocals, stacksize, flags) < 0) { |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 566 | goto cleanup; |
| 567 | } |
| 568 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 569 | if (argcount < 0) { |
| 570 | PyErr_SetString( |
| 571 | PyExc_ValueError, |
| 572 | "code: argcount must not be negative"); |
| 573 | goto cleanup; |
| 574 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 575 | |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 576 | if (posonlyargcount < 0) { |
| 577 | PyErr_SetString( |
| 578 | PyExc_ValueError, |
| 579 | "code: posonlyargcount must not be negative"); |
| 580 | goto cleanup; |
| 581 | } |
| 582 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 583 | if (kwonlyargcount < 0) { |
| 584 | PyErr_SetString( |
| 585 | PyExc_ValueError, |
| 586 | "code: kwonlyargcount must not be negative"); |
| 587 | goto cleanup; |
| 588 | } |
| 589 | if (nlocals < 0) { |
| 590 | PyErr_SetString( |
| 591 | PyExc_ValueError, |
| 592 | "code: nlocals must not be negative"); |
| 593 | goto cleanup; |
| 594 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 595 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 596 | ournames = validate_and_copy_tuple(names); |
| 597 | if (ournames == NULL) |
| 598 | goto cleanup; |
| 599 | ourvarnames = validate_and_copy_tuple(varnames); |
| 600 | if (ourvarnames == NULL) |
| 601 | goto cleanup; |
| 602 | if (freevars) |
| 603 | ourfreevars = validate_and_copy_tuple(freevars); |
| 604 | else |
| 605 | ourfreevars = PyTuple_New(0); |
| 606 | if (ourfreevars == NULL) |
| 607 | goto cleanup; |
| 608 | if (cellvars) |
| 609 | ourcellvars = validate_and_copy_tuple(cellvars); |
| 610 | else |
| 611 | ourcellvars = PyTuple_New(0); |
| 612 | if (ourcellvars == NULL) |
| 613 | goto cleanup; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 614 | |
Pablo Galindo | 4a2edc3 | 2019-07-01 11:35:05 +0100 | [diff] [blame] | 615 | co = (PyObject *)PyCode_NewWithPosOnlyArgs(argcount, posonlyargcount, |
| 616 | kwonlyargcount, |
| 617 | nlocals, stacksize, flags, |
| 618 | code, consts, ournames, |
| 619 | ourvarnames, ourfreevars, |
| 620 | ourcellvars, filename, |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 621 | name, firstlineno, linetable); |
Victor Stinner | a278313 | 2020-01-27 23:24:13 +0100 | [diff] [blame] | 622 | cleanup: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 623 | Py_XDECREF(ournames); |
| 624 | Py_XDECREF(ourvarnames); |
| 625 | Py_XDECREF(ourfreevars); |
| 626 | Py_XDECREF(ourcellvars); |
| 627 | return co; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | static void |
| 631 | code_dealloc(PyCodeObject *co) |
| 632 | { |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 633 | if (co->co_opcache != NULL) { |
Victor Stinner | 00d7abd | 2020-12-01 09:56:42 +0100 | [diff] [blame] | 634 | PyMem_Free(co->co_opcache); |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 635 | } |
| 636 | if (co->co_opcache_map != NULL) { |
Victor Stinner | 00d7abd | 2020-12-01 09:56:42 +0100 | [diff] [blame] | 637 | PyMem_Free(co->co_opcache_map); |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 638 | } |
| 639 | co->co_opcache_flag = 0; |
| 640 | co->co_opcache_size = 0; |
| 641 | |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 642 | if (co->co_extra != NULL) { |
Victor Stinner | 81a7be3 | 2020-04-14 15:14:01 +0200 | [diff] [blame] | 643 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
Brett Cannon | d0600ed | 2016-09-07 14:30:39 -0700 | [diff] [blame] | 644 | _PyCodeObjectExtra *co_extra = co->co_extra; |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 645 | |
Brett Cannon | d0600ed | 2016-09-07 14:30:39 -0700 | [diff] [blame] | 646 | for (Py_ssize_t i = 0; i < co_extra->ce_size; i++) { |
Dino Viehland | f3cffd2 | 2017-06-21 14:44:36 -0700 | [diff] [blame] | 647 | freefunc free_extra = interp->co_extra_freefuncs[i]; |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 648 | |
| 649 | if (free_extra != NULL) { |
Brett Cannon | d0600ed | 2016-09-07 14:30:39 -0700 | [diff] [blame] | 650 | free_extra(co_extra->ce_extras[i]); |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 651 | } |
| 652 | } |
| 653 | |
Victor Stinner | 23e7944 | 2017-06-28 02:12:00 +0200 | [diff] [blame] | 654 | PyMem_Free(co_extra); |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 655 | } |
| 656 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 657 | Py_XDECREF(co->co_code); |
| 658 | Py_XDECREF(co->co_consts); |
| 659 | Py_XDECREF(co->co_names); |
| 660 | Py_XDECREF(co->co_varnames); |
| 661 | Py_XDECREF(co->co_freevars); |
| 662 | Py_XDECREF(co->co_cellvars); |
| 663 | Py_XDECREF(co->co_filename); |
| 664 | Py_XDECREF(co->co_name); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 665 | Py_XDECREF(co->co_linetable); |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 666 | if (co->co_cell2arg != NULL) |
Victor Stinner | 00d7abd | 2020-12-01 09:56:42 +0100 | [diff] [blame] | 667 | PyMem_Free(co->co_cell2arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 668 | if (co->co_zombieframe != NULL) |
| 669 | PyObject_GC_Del(co->co_zombieframe); |
| 670 | if (co->co_weakreflist != NULL) |
| 671 | PyObject_ClearWeakRefs((PyObject*)co); |
Victor Stinner | 32bd68c | 2020-12-01 10:37:39 +0100 | [diff] [blame] | 672 | PyObject_Free(co); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | static PyObject * |
Victor Stinner | a9f05d6 | 2019-05-24 23:57:23 +0200 | [diff] [blame] | 676 | code_sizeof(PyCodeObject *co, PyObject *Py_UNUSED(args)) |
Martin v. Löwis | 3bbd2fa | 2012-07-26 22:23:23 +0200 | [diff] [blame] | 677 | { |
Dong-hee Na | b4dc6af | 2017-04-20 16:31:17 +0900 | [diff] [blame] | 678 | Py_ssize_t res = _PyObject_SIZE(Py_TYPE(co)); |
| 679 | _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra*) co->co_extra; |
Martin v. Löwis | 3bbd2fa | 2012-07-26 22:23:23 +0200 | [diff] [blame] | 680 | |
Serhiy Storchaka | 378ebb6 | 2017-07-04 15:06:16 +0300 | [diff] [blame] | 681 | if (co->co_cell2arg != NULL && co->co_cellvars != NULL) { |
Serhiy Storchaka | 5bb8b91 | 2016-12-16 19:19:02 +0200 | [diff] [blame] | 682 | res += PyTuple_GET_SIZE(co->co_cellvars) * sizeof(Py_ssize_t); |
Serhiy Storchaka | 378ebb6 | 2017-07-04 15:06:16 +0300 | [diff] [blame] | 683 | } |
| 684 | if (co_extra != NULL) { |
| 685 | res += sizeof(_PyCodeObjectExtra) + |
| 686 | (co_extra->ce_size-1) * sizeof(co_extra->ce_extras[0]); |
| 687 | } |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 688 | if (co->co_opcache != NULL) { |
| 689 | assert(co->co_opcache_map != NULL); |
| 690 | // co_opcache_map |
| 691 | res += PyBytes_GET_SIZE(co->co_code) / sizeof(_Py_CODEUNIT); |
| 692 | // co_opcache |
| 693 | res += co->co_opcache_size * sizeof(_PyOpcache); |
| 694 | } |
Martin v. Löwis | 3bbd2fa | 2012-07-26 22:23:23 +0200 | [diff] [blame] | 695 | return PyLong_FromSsize_t(res); |
| 696 | } |
| 697 | |
Victor Stinner | a9f05d6 | 2019-05-24 23:57:23 +0200 | [diff] [blame] | 698 | /*[clinic input] |
| 699 | code.replace |
| 700 | |
| 701 | * |
| 702 | co_argcount: int(c_default="self->co_argcount") = -1 |
| 703 | co_posonlyargcount: int(c_default="self->co_posonlyargcount") = -1 |
| 704 | co_kwonlyargcount: int(c_default="self->co_kwonlyargcount") = -1 |
| 705 | co_nlocals: int(c_default="self->co_nlocals") = -1 |
| 706 | co_stacksize: int(c_default="self->co_stacksize") = -1 |
| 707 | co_flags: int(c_default="self->co_flags") = -1 |
| 708 | co_firstlineno: int(c_default="self->co_firstlineno") = -1 |
| 709 | co_code: PyBytesObject(c_default="(PyBytesObject *)self->co_code") = None |
| 710 | co_consts: object(subclass_of="&PyTuple_Type", c_default="self->co_consts") = None |
| 711 | co_names: object(subclass_of="&PyTuple_Type", c_default="self->co_names") = None |
| 712 | co_varnames: object(subclass_of="&PyTuple_Type", c_default="self->co_varnames") = None |
| 713 | co_freevars: object(subclass_of="&PyTuple_Type", c_default="self->co_freevars") = None |
| 714 | co_cellvars: object(subclass_of="&PyTuple_Type", c_default="self->co_cellvars") = None |
| 715 | co_filename: unicode(c_default="self->co_filename") = None |
| 716 | co_name: unicode(c_default="self->co_name") = None |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 717 | co_linetable: PyBytesObject(c_default="(PyBytesObject *)self->co_linetable") = None |
Victor Stinner | a9f05d6 | 2019-05-24 23:57:23 +0200 | [diff] [blame] | 718 | |
Anthony Sottile | 22424c0 | 2020-01-01 01:11:16 -0500 | [diff] [blame] | 719 | Return a copy of the code object with new values for the specified fields. |
Victor Stinner | a9f05d6 | 2019-05-24 23:57:23 +0200 | [diff] [blame] | 720 | [clinic start generated code]*/ |
| 721 | |
| 722 | static PyObject * |
| 723 | code_replace_impl(PyCodeObject *self, int co_argcount, |
| 724 | int co_posonlyargcount, int co_kwonlyargcount, |
| 725 | int co_nlocals, int co_stacksize, int co_flags, |
| 726 | int co_firstlineno, PyBytesObject *co_code, |
| 727 | PyObject *co_consts, PyObject *co_names, |
| 728 | PyObject *co_varnames, PyObject *co_freevars, |
| 729 | PyObject *co_cellvars, PyObject *co_filename, |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 730 | PyObject *co_name, PyBytesObject *co_linetable) |
| 731 | /*[clinic end generated code: output=50d77e668d3b449b input=a5f997b173d7f636]*/ |
Victor Stinner | a9f05d6 | 2019-05-24 23:57:23 +0200 | [diff] [blame] | 732 | { |
| 733 | #define CHECK_INT_ARG(ARG) \ |
| 734 | if (ARG < 0) { \ |
| 735 | PyErr_SetString(PyExc_ValueError, \ |
| 736 | #ARG " must be a positive integer"); \ |
| 737 | return NULL; \ |
| 738 | } |
| 739 | |
| 740 | CHECK_INT_ARG(co_argcount); |
| 741 | CHECK_INT_ARG(co_posonlyargcount); |
| 742 | CHECK_INT_ARG(co_kwonlyargcount); |
| 743 | CHECK_INT_ARG(co_nlocals); |
| 744 | CHECK_INT_ARG(co_stacksize); |
| 745 | CHECK_INT_ARG(co_flags); |
| 746 | CHECK_INT_ARG(co_firstlineno); |
| 747 | |
| 748 | #undef CHECK_INT_ARG |
| 749 | |
Steve Dower | c7c01ab | 2019-11-26 16:27:50 -0800 | [diff] [blame] | 750 | if (PySys_Audit("code.__new__", "OOOiiiiii", |
| 751 | co_code, co_filename, co_name, co_argcount, |
| 752 | co_posonlyargcount, co_kwonlyargcount, co_nlocals, |
| 753 | co_stacksize, co_flags) < 0) { |
| 754 | return NULL; |
| 755 | } |
| 756 | |
Pablo Galindo | 4a2edc3 | 2019-07-01 11:35:05 +0100 | [diff] [blame] | 757 | return (PyObject *)PyCode_NewWithPosOnlyArgs( |
Victor Stinner | a9f05d6 | 2019-05-24 23:57:23 +0200 | [diff] [blame] | 758 | co_argcount, co_posonlyargcount, co_kwonlyargcount, co_nlocals, |
| 759 | co_stacksize, co_flags, (PyObject*)co_code, co_consts, co_names, |
| 760 | co_varnames, co_freevars, co_cellvars, co_filename, co_name, |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 761 | co_firstlineno, (PyObject*)co_linetable); |
Victor Stinner | a9f05d6 | 2019-05-24 23:57:23 +0200 | [diff] [blame] | 762 | } |
| 763 | |
Martin v. Löwis | 3bbd2fa | 2012-07-26 22:23:23 +0200 | [diff] [blame] | 764 | static PyObject * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 765 | code_repr(PyCodeObject *co) |
| 766 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 767 | int lineno; |
| 768 | if (co->co_firstlineno != 0) |
| 769 | lineno = co->co_firstlineno; |
| 770 | else |
| 771 | lineno = -1; |
| 772 | if (co->co_filename && PyUnicode_Check(co->co_filename)) { |
| 773 | return PyUnicode_FromFormat( |
Victor Stinner | aaa4e9a | 2011-01-05 03:33:26 +0000 | [diff] [blame] | 774 | "<code object %U at %p, file \"%U\", line %d>", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 775 | co->co_name, co, co->co_filename, lineno); |
| 776 | } else { |
| 777 | return PyUnicode_FromFormat( |
Victor Stinner | aaa4e9a | 2011-01-05 03:33:26 +0000 | [diff] [blame] | 778 | "<code object %U at %p, file ???, line %d>", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 779 | co->co_name, co, lineno); |
| 780 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 781 | } |
| 782 | |
Victor Stinner | efb2413 | 2016-01-22 12:33:12 +0100 | [diff] [blame] | 783 | PyObject* |
| 784 | _PyCode_ConstantKey(PyObject *op) |
| 785 | { |
| 786 | PyObject *key; |
| 787 | |
Serhiy Storchaka | b7e1eff | 2018-04-19 08:28:04 +0300 | [diff] [blame] | 788 | /* Py_None and Py_Ellipsis are singletons. */ |
Victor Stinner | efb2413 | 2016-01-22 12:33:12 +0100 | [diff] [blame] | 789 | if (op == Py_None || op == Py_Ellipsis |
| 790 | || PyLong_CheckExact(op) |
Victor Stinner | efb2413 | 2016-01-22 12:33:12 +0100 | [diff] [blame] | 791 | || PyUnicode_CheckExact(op) |
| 792 | /* code_richcompare() uses _PyCode_ConstantKey() internally */ |
Serhiy Storchaka | b7e1eff | 2018-04-19 08:28:04 +0300 | [diff] [blame] | 793 | || PyCode_Check(op)) |
| 794 | { |
| 795 | /* Objects of these types are always different from object of other |
| 796 | * type and from tuples. */ |
| 797 | Py_INCREF(op); |
| 798 | key = op; |
| 799 | } |
| 800 | else if (PyBool_Check(op) || PyBytes_CheckExact(op)) { |
| 801 | /* Make booleans different from integers 0 and 1. |
| 802 | * Avoid BytesWarning from comparing bytes with strings. */ |
Victor Stinner | efb2413 | 2016-01-22 12:33:12 +0100 | [diff] [blame] | 803 | key = PyTuple_Pack(2, Py_TYPE(op), op); |
| 804 | } |
| 805 | else if (PyFloat_CheckExact(op)) { |
| 806 | double d = PyFloat_AS_DOUBLE(op); |
| 807 | /* all we need is to make the tuple different in either the 0.0 |
| 808 | * or -0.0 case from all others, just to avoid the "coercion". |
| 809 | */ |
| 810 | if (d == 0.0 && copysign(1.0, d) < 0.0) |
| 811 | key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None); |
| 812 | else |
| 813 | key = PyTuple_Pack(2, Py_TYPE(op), op); |
| 814 | } |
| 815 | else if (PyComplex_CheckExact(op)) { |
| 816 | Py_complex z; |
| 817 | int real_negzero, imag_negzero; |
| 818 | /* For the complex case we must make complex(x, 0.) |
| 819 | different from complex(x, -0.) and complex(0., y) |
| 820 | different from complex(-0., y), for any x and y. |
| 821 | All four complex zeros must be distinguished.*/ |
| 822 | z = PyComplex_AsCComplex(op); |
| 823 | real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0; |
| 824 | imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0; |
| 825 | /* use True, False and None singleton as tags for the real and imag |
| 826 | * sign, to make tuples different */ |
| 827 | if (real_negzero && imag_negzero) { |
| 828 | key = PyTuple_Pack(3, Py_TYPE(op), op, Py_True); |
| 829 | } |
| 830 | else if (imag_negzero) { |
| 831 | key = PyTuple_Pack(3, Py_TYPE(op), op, Py_False); |
| 832 | } |
| 833 | else if (real_negzero) { |
| 834 | key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None); |
| 835 | } |
| 836 | else { |
| 837 | key = PyTuple_Pack(2, Py_TYPE(op), op); |
| 838 | } |
| 839 | } |
| 840 | else if (PyTuple_CheckExact(op)) { |
| 841 | Py_ssize_t i, len; |
| 842 | PyObject *tuple; |
| 843 | |
| 844 | len = PyTuple_GET_SIZE(op); |
| 845 | tuple = PyTuple_New(len); |
| 846 | if (tuple == NULL) |
| 847 | return NULL; |
| 848 | |
| 849 | for (i=0; i < len; i++) { |
| 850 | PyObject *item, *item_key; |
| 851 | |
| 852 | item = PyTuple_GET_ITEM(op, i); |
| 853 | item_key = _PyCode_ConstantKey(item); |
| 854 | if (item_key == NULL) { |
| 855 | Py_DECREF(tuple); |
| 856 | return NULL; |
| 857 | } |
| 858 | |
| 859 | PyTuple_SET_ITEM(tuple, i, item_key); |
| 860 | } |
| 861 | |
Serhiy Storchaka | 713640c | 2017-01-24 20:49:26 +0200 | [diff] [blame] | 862 | key = PyTuple_Pack(2, tuple, op); |
Victor Stinner | efb2413 | 2016-01-22 12:33:12 +0100 | [diff] [blame] | 863 | Py_DECREF(tuple); |
| 864 | } |
| 865 | else if (PyFrozenSet_CheckExact(op)) { |
| 866 | Py_ssize_t pos = 0; |
| 867 | PyObject *item; |
| 868 | Py_hash_t hash; |
| 869 | Py_ssize_t i, len; |
| 870 | PyObject *tuple, *set; |
| 871 | |
| 872 | len = PySet_GET_SIZE(op); |
| 873 | tuple = PyTuple_New(len); |
| 874 | if (tuple == NULL) |
| 875 | return NULL; |
| 876 | |
| 877 | i = 0; |
| 878 | while (_PySet_NextEntry(op, &pos, &item, &hash)) { |
| 879 | PyObject *item_key; |
| 880 | |
| 881 | item_key = _PyCode_ConstantKey(item); |
| 882 | if (item_key == NULL) { |
| 883 | Py_DECREF(tuple); |
| 884 | return NULL; |
| 885 | } |
| 886 | |
| 887 | assert(i < len); |
| 888 | PyTuple_SET_ITEM(tuple, i, item_key); |
| 889 | i++; |
| 890 | } |
| 891 | set = PyFrozenSet_New(tuple); |
| 892 | Py_DECREF(tuple); |
| 893 | if (set == NULL) |
| 894 | return NULL; |
| 895 | |
Serhiy Storchaka | 713640c | 2017-01-24 20:49:26 +0200 | [diff] [blame] | 896 | key = PyTuple_Pack(2, set, op); |
Victor Stinner | efb2413 | 2016-01-22 12:33:12 +0100 | [diff] [blame] | 897 | Py_DECREF(set); |
| 898 | return key; |
| 899 | } |
| 900 | else { |
Martin Panter | 6245cb3 | 2016-04-15 02:14:19 +0000 | [diff] [blame] | 901 | /* for other types, use the object identifier as a unique identifier |
Victor Stinner | efb2413 | 2016-01-22 12:33:12 +0100 | [diff] [blame] | 902 | * to ensure that they are seen as unequal. */ |
| 903 | PyObject *obj_id = PyLong_FromVoidPtr(op); |
| 904 | if (obj_id == NULL) |
| 905 | return NULL; |
| 906 | |
Serhiy Storchaka | 713640c | 2017-01-24 20:49:26 +0200 | [diff] [blame] | 907 | key = PyTuple_Pack(2, obj_id, op); |
Victor Stinner | efb2413 | 2016-01-22 12:33:12 +0100 | [diff] [blame] | 908 | Py_DECREF(obj_id); |
| 909 | } |
| 910 | return key; |
| 911 | } |
| 912 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 913 | static PyObject * |
| 914 | code_richcompare(PyObject *self, PyObject *other, int op) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 915 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 916 | PyCodeObject *co, *cp; |
| 917 | int eq; |
Victor Stinner | efb2413 | 2016-01-22 12:33:12 +0100 | [diff] [blame] | 918 | PyObject *consts1, *consts2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 919 | PyObject *res; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 920 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 921 | if ((op != Py_EQ && op != Py_NE) || |
| 922 | !PyCode_Check(self) || |
| 923 | !PyCode_Check(other)) { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 924 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 925 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 926 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 927 | co = (PyCodeObject *)self; |
| 928 | cp = (PyCodeObject *)other; |
Guido van Rossum | b6bb0c7 | 2006-08-24 04:12:18 +0000 | [diff] [blame] | 929 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 930 | eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ); |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 931 | if (!eq) goto unequal; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 932 | eq = co->co_argcount == cp->co_argcount; |
| 933 | if (!eq) goto unequal; |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 934 | eq = co->co_posonlyargcount == cp->co_posonlyargcount; |
| 935 | if (!eq) goto unequal; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 936 | eq = co->co_kwonlyargcount == cp->co_kwonlyargcount; |
| 937 | if (!eq) goto unequal; |
| 938 | eq = co->co_nlocals == cp->co_nlocals; |
| 939 | if (!eq) goto unequal; |
| 940 | eq = co->co_flags == cp->co_flags; |
| 941 | if (!eq) goto unequal; |
| 942 | eq = co->co_firstlineno == cp->co_firstlineno; |
| 943 | if (!eq) goto unequal; |
| 944 | eq = PyObject_RichCompareBool(co->co_code, cp->co_code, Py_EQ); |
| 945 | if (eq <= 0) goto unequal; |
Victor Stinner | efb2413 | 2016-01-22 12:33:12 +0100 | [diff] [blame] | 946 | |
| 947 | /* compare constants */ |
| 948 | consts1 = _PyCode_ConstantKey(co->co_consts); |
| 949 | if (!consts1) |
| 950 | return NULL; |
| 951 | consts2 = _PyCode_ConstantKey(cp->co_consts); |
| 952 | if (!consts2) { |
| 953 | Py_DECREF(consts1); |
| 954 | return NULL; |
| 955 | } |
| 956 | eq = PyObject_RichCompareBool(consts1, consts2, Py_EQ); |
| 957 | Py_DECREF(consts1); |
| 958 | Py_DECREF(consts2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 959 | if (eq <= 0) goto unequal; |
Victor Stinner | efb2413 | 2016-01-22 12:33:12 +0100 | [diff] [blame] | 960 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 961 | eq = PyObject_RichCompareBool(co->co_names, cp->co_names, Py_EQ); |
| 962 | if (eq <= 0) goto unequal; |
| 963 | eq = PyObject_RichCompareBool(co->co_varnames, cp->co_varnames, Py_EQ); |
| 964 | if (eq <= 0) goto unequal; |
| 965 | eq = PyObject_RichCompareBool(co->co_freevars, cp->co_freevars, Py_EQ); |
| 966 | if (eq <= 0) goto unequal; |
| 967 | eq = PyObject_RichCompareBool(co->co_cellvars, cp->co_cellvars, Py_EQ); |
| 968 | if (eq <= 0) goto unequal; |
Guido van Rossum | b6bb0c7 | 2006-08-24 04:12:18 +0000 | [diff] [blame] | 969 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 970 | if (op == Py_EQ) |
| 971 | res = Py_True; |
| 972 | else |
| 973 | res = Py_False; |
| 974 | goto done; |
Guido van Rossum | b6bb0c7 | 2006-08-24 04:12:18 +0000 | [diff] [blame] | 975 | |
| 976 | unequal: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 977 | if (eq < 0) |
| 978 | return NULL; |
| 979 | if (op == Py_NE) |
| 980 | res = Py_True; |
| 981 | else |
| 982 | res = Py_False; |
Guido van Rossum | b6bb0c7 | 2006-08-24 04:12:18 +0000 | [diff] [blame] | 983 | |
| 984 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 985 | Py_INCREF(res); |
| 986 | return res; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 987 | } |
| 988 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 989 | static Py_hash_t |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 990 | code_hash(PyCodeObject *co) |
| 991 | { |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 992 | Py_hash_t h, h0, h1, h2, h3, h4, h5, h6; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 993 | h0 = PyObject_Hash(co->co_name); |
| 994 | if (h0 == -1) return -1; |
| 995 | h1 = PyObject_Hash(co->co_code); |
| 996 | if (h1 == -1) return -1; |
| 997 | h2 = PyObject_Hash(co->co_consts); |
| 998 | if (h2 == -1) return -1; |
| 999 | h3 = PyObject_Hash(co->co_names); |
| 1000 | if (h3 == -1) return -1; |
| 1001 | h4 = PyObject_Hash(co->co_varnames); |
| 1002 | if (h4 == -1) return -1; |
| 1003 | h5 = PyObject_Hash(co->co_freevars); |
| 1004 | if (h5 == -1) return -1; |
| 1005 | h6 = PyObject_Hash(co->co_cellvars); |
| 1006 | if (h6 == -1) return -1; |
| 1007 | h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^ |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1008 | co->co_argcount ^ co->co_posonlyargcount ^ co->co_kwonlyargcount ^ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1009 | co->co_nlocals ^ co->co_flags; |
| 1010 | if (h == -1) h = -2; |
| 1011 | return h; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1012 | } |
| 1013 | |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 1014 | typedef struct { |
| 1015 | PyObject_HEAD |
| 1016 | PyCodeObject *li_code; |
| 1017 | PyCodeAddressRange li_line; |
| 1018 | char *li_end; |
| 1019 | } lineiterator; |
| 1020 | |
| 1021 | |
| 1022 | static void |
| 1023 | lineiter_dealloc(lineiterator *li) |
| 1024 | { |
| 1025 | Py_DECREF(li->li_code); |
| 1026 | Py_TYPE(li)->tp_free(li); |
| 1027 | } |
| 1028 | |
| 1029 | static PyObject * |
| 1030 | lineiter_next(lineiterator *li) |
| 1031 | { |
| 1032 | PyCodeAddressRange *bounds = &li->li_line; |
| 1033 | if (!PyLineTable_NextAddressRange(bounds)) { |
| 1034 | return NULL; |
| 1035 | } |
| 1036 | PyObject *start = NULL; |
| 1037 | PyObject *end = NULL; |
| 1038 | PyObject *line = NULL; |
| 1039 | PyObject *result = PyTuple_New(3); |
| 1040 | start = PyLong_FromLong(bounds->ar_start); |
| 1041 | end = PyLong_FromLong(bounds->ar_end); |
| 1042 | if (bounds->ar_line < 0) { |
| 1043 | Py_INCREF(Py_None); |
| 1044 | line = Py_None; |
| 1045 | } |
| 1046 | else { |
| 1047 | line = PyLong_FromLong(bounds->ar_line); |
| 1048 | } |
| 1049 | if (result == NULL || start == NULL || end == NULL || line == NULL) { |
| 1050 | goto error; |
| 1051 | } |
| 1052 | PyTuple_SET_ITEM(result, 0, start); |
| 1053 | PyTuple_SET_ITEM(result, 1, end); |
| 1054 | PyTuple_SET_ITEM(result, 2, line); |
| 1055 | return result; |
| 1056 | error: |
| 1057 | Py_XDECREF(start); |
| 1058 | Py_XDECREF(end); |
| 1059 | Py_XDECREF(line); |
| 1060 | Py_XDECREF(result); |
| 1061 | return result; |
| 1062 | } |
| 1063 | |
| 1064 | static PyTypeObject LineIterator = { |
| 1065 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 1066 | "line_iterator", /* tp_name */ |
| 1067 | sizeof(lineiterator), /* tp_basicsize */ |
| 1068 | 0, /* tp_itemsize */ |
| 1069 | /* methods */ |
| 1070 | (destructor)lineiter_dealloc, /* tp_dealloc */ |
| 1071 | 0, /* tp_vectorcall_offset */ |
| 1072 | 0, /* tp_getattr */ |
| 1073 | 0, /* tp_setattr */ |
| 1074 | 0, /* tp_as_async */ |
| 1075 | 0, /* tp_repr */ |
| 1076 | 0, /* tp_as_number */ |
| 1077 | 0, /* tp_as_sequence */ |
| 1078 | 0, /* tp_as_mapping */ |
| 1079 | 0, /* tp_hash */ |
| 1080 | 0, /* tp_call */ |
| 1081 | 0, /* tp_str */ |
| 1082 | 0, /* tp_getattro */ |
| 1083 | 0, /* tp_setattro */ |
| 1084 | 0, /* tp_as_buffer */ |
| 1085 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 1086 | 0, /* tp_doc */ |
| 1087 | 0, /* tp_traverse */ |
| 1088 | 0, /* tp_clear */ |
| 1089 | 0, /* tp_richcompare */ |
| 1090 | 0, /* tp_weaklistoffset */ |
| 1091 | PyObject_SelfIter, /* tp_iter */ |
| 1092 | (iternextfunc)lineiter_next, /* tp_iternext */ |
| 1093 | 0, /* tp_methods */ |
| 1094 | 0, /* tp_members */ |
| 1095 | 0, /* tp_getset */ |
| 1096 | 0, /* tp_base */ |
| 1097 | 0, /* tp_dict */ |
| 1098 | 0, /* tp_descr_get */ |
| 1099 | 0, /* tp_descr_set */ |
| 1100 | 0, /* tp_dictoffset */ |
| 1101 | 0, /* tp_init */ |
| 1102 | 0, /* tp_alloc */ |
| 1103 | 0, /* tp_new */ |
| 1104 | PyObject_Del, /* tp_free */ |
| 1105 | }; |
| 1106 | |
| 1107 | static PyObject * |
| 1108 | code_linesiterator(PyCodeObject *code, PyObject *Py_UNUSED(args)) |
| 1109 | { |
| 1110 | lineiterator *li = (lineiterator *)PyType_GenericAlloc(&LineIterator, 0); |
| 1111 | if (li == NULL) { |
| 1112 | return NULL; |
| 1113 | } |
| 1114 | Py_INCREF(code); |
| 1115 | li->li_code = code; |
| 1116 | _PyCode_InitAddressRange(code, &li->li_line); |
| 1117 | return (PyObject *)li; |
| 1118 | } |
| 1119 | |
| 1120 | static void |
| 1121 | retreat(PyCodeAddressRange *bounds) |
| 1122 | { |
| 1123 | int ldelta = ((signed char *)bounds->lo_next)[-1]; |
| 1124 | if (ldelta == -128) { |
| 1125 | ldelta = 0; |
| 1126 | } |
| 1127 | bounds->ar_computed_line -= ldelta; |
| 1128 | bounds->lo_next -= 2; |
| 1129 | bounds->ar_end = bounds->ar_start; |
| 1130 | bounds->ar_start -= ((unsigned char *)bounds->lo_next)[-2]; |
| 1131 | ldelta = ((signed char *)bounds->lo_next)[-1]; |
| 1132 | if (ldelta == -128) { |
| 1133 | bounds->ar_line = -1; |
| 1134 | } |
| 1135 | else { |
| 1136 | bounds->ar_line = bounds->ar_computed_line; |
| 1137 | } |
| 1138 | } |
| 1139 | |
| 1140 | static void |
| 1141 | advance(PyCodeAddressRange *bounds) |
| 1142 | { |
| 1143 | bounds->ar_start = bounds->ar_end; |
| 1144 | int delta = ((unsigned char *)bounds->lo_next)[0]; |
| 1145 | assert (delta < 255); |
| 1146 | bounds->ar_end += delta; |
| 1147 | int ldelta = ((signed char *)bounds->lo_next)[1]; |
| 1148 | bounds->lo_next += 2; |
| 1149 | if (ldelta == -128) { |
| 1150 | bounds->ar_line = -1; |
| 1151 | } |
| 1152 | else { |
| 1153 | bounds->ar_computed_line += ldelta; |
| 1154 | bounds->ar_line = bounds->ar_computed_line; |
| 1155 | } |
| 1156 | } |
| 1157 | |
| 1158 | static inline int |
| 1159 | at_end(PyCodeAddressRange *bounds) { |
| 1160 | return ((unsigned char *)bounds->lo_next)[0] == 255; |
| 1161 | } |
| 1162 | |
| 1163 | int |
| 1164 | PyLineTable_PreviousAddressRange(PyCodeAddressRange *range) |
| 1165 | { |
| 1166 | if (range->ar_start <= 0) { |
| 1167 | return 0; |
| 1168 | } |
| 1169 | retreat(range); |
| 1170 | while (range->ar_start == range->ar_end) { |
| 1171 | assert(range->ar_start > 0); |
| 1172 | retreat(range); |
| 1173 | } |
| 1174 | return 1; |
| 1175 | } |
| 1176 | |
| 1177 | int |
| 1178 | PyLineTable_NextAddressRange(PyCodeAddressRange *range) |
| 1179 | { |
| 1180 | if (at_end(range)) { |
| 1181 | return 0; |
| 1182 | } |
| 1183 | advance(range); |
| 1184 | while (range->ar_start == range->ar_end) { |
| 1185 | assert(!at_end(range)); |
| 1186 | advance(range); |
| 1187 | } |
| 1188 | return 1; |
| 1189 | } |
| 1190 | |
| 1191 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1192 | /* XXX code objects need to participate in GC? */ |
| 1193 | |
Martin v. Löwis | 3bbd2fa | 2012-07-26 22:23:23 +0200 | [diff] [blame] | 1194 | static struct PyMethodDef code_methods[] = { |
| 1195 | {"__sizeof__", (PyCFunction)code_sizeof, METH_NOARGS}, |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 1196 | {"co_lines", (PyCFunction)code_linesiterator, METH_NOARGS}, |
Victor Stinner | a9f05d6 | 2019-05-24 23:57:23 +0200 | [diff] [blame] | 1197 | CODE_REPLACE_METHODDEF |
Martin v. Löwis | 3bbd2fa | 2012-07-26 22:23:23 +0200 | [diff] [blame] | 1198 | {NULL, NULL} /* sentinel */ |
| 1199 | }; |
| 1200 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1201 | PyTypeObject PyCode_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1202 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 1203 | "code", |
| 1204 | sizeof(PyCodeObject), |
| 1205 | 0, |
| 1206 | (destructor)code_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 1207 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1208 | 0, /* tp_getattr */ |
| 1209 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 1210 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1211 | (reprfunc)code_repr, /* tp_repr */ |
| 1212 | 0, /* tp_as_number */ |
| 1213 | 0, /* tp_as_sequence */ |
| 1214 | 0, /* tp_as_mapping */ |
| 1215 | (hashfunc)code_hash, /* tp_hash */ |
| 1216 | 0, /* tp_call */ |
| 1217 | 0, /* tp_str */ |
| 1218 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1219 | 0, /* tp_setattro */ |
| 1220 | 0, /* tp_as_buffer */ |
| 1221 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
Serhiy Storchaka | 0f9aa47 | 2020-07-10 10:12:04 +0300 | [diff] [blame] | 1222 | code_new__doc__, /* tp_doc */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1223 | 0, /* tp_traverse */ |
| 1224 | 0, /* tp_clear */ |
| 1225 | code_richcompare, /* tp_richcompare */ |
| 1226 | offsetof(PyCodeObject, co_weakreflist), /* tp_weaklistoffset */ |
| 1227 | 0, /* tp_iter */ |
| 1228 | 0, /* tp_iternext */ |
Martin v. Löwis | 3bbd2fa | 2012-07-26 22:23:23 +0200 | [diff] [blame] | 1229 | code_methods, /* tp_methods */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1230 | code_memberlist, /* tp_members */ |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 1231 | code_getsetlist, /* tp_getset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1232 | 0, /* tp_base */ |
| 1233 | 0, /* tp_dict */ |
| 1234 | 0, /* tp_descr_get */ |
| 1235 | 0, /* tp_descr_set */ |
| 1236 | 0, /* tp_dictoffset */ |
| 1237 | 0, /* tp_init */ |
| 1238 | 0, /* tp_alloc */ |
| 1239 | code_new, /* tp_new */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1240 | }; |
| 1241 | |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 1242 | /* Use co_linetable to compute the line number from a bytecode index, addrq. See |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 1243 | lnotab_notes.txt for the details of the lnotab representation. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1244 | */ |
| 1245 | |
| 1246 | int |
| 1247 | PyCode_Addr2Line(PyCodeObject *co, int addrq) |
| 1248 | { |
Mark Shannon | fcb55c0 | 2021-04-01 16:00:31 +0100 | [diff] [blame] | 1249 | if (addrq < 0) { |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 1250 | return co->co_firstlineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1251 | } |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 1252 | assert(addrq >= 0 && addrq < PyBytes_GET_SIZE(co->co_code)); |
| 1253 | PyCodeAddressRange bounds; |
| 1254 | _PyCode_InitAddressRange(co, &bounds); |
| 1255 | return _PyCode_CheckLineNumber(addrq, &bounds); |
| 1256 | } |
| 1257 | |
| 1258 | void |
| 1259 | PyLineTable_InitAddressRange(char *linetable, int firstlineno, PyCodeAddressRange *range) |
| 1260 | { |
| 1261 | range->lo_next = linetable; |
| 1262 | range->ar_start = -1; |
| 1263 | range->ar_end = 0; |
Mark Shannon | ee9f98d | 2021-01-05 12:04:10 +0000 | [diff] [blame] | 1264 | range->ar_computed_line = firstlineno; |
| 1265 | range->ar_line = -1; |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 1266 | } |
| 1267 | |
| 1268 | int |
| 1269 | _PyCode_InitAddressRange(PyCodeObject* co, PyCodeAddressRange *bounds) |
| 1270 | { |
| 1271 | char *linetable = PyBytes_AS_STRING(co->co_linetable); |
| 1272 | PyLineTable_InitAddressRange(linetable, co->co_firstlineno, bounds); |
| 1273 | return bounds->ar_line; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1274 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1275 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 1276 | /* Update *bounds to describe the first and one-past-the-last instructions in |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 1277 | the same line as lasti. Return the number of that line, or -1 if lasti is out of bounds. */ |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 1278 | int |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 1279 | _PyCode_CheckLineNumber(int lasti, PyCodeAddressRange *bounds) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1280 | { |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 1281 | while (bounds->ar_end <= lasti) { |
| 1282 | if (!PyLineTable_NextAddressRange(bounds)) { |
| 1283 | return -1; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1284 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1285 | } |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 1286 | while (bounds->ar_start > lasti) { |
| 1287 | if (!PyLineTable_PreviousAddressRange(bounds)) { |
| 1288 | return -1; |
| 1289 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1290 | } |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 1291 | return bounds->ar_line; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1292 | } |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 1293 | |
| 1294 | |
| 1295 | int |
| 1296 | _PyCode_GetExtra(PyObject *code, Py_ssize_t index, void **extra) |
| 1297 | { |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 1298 | if (!PyCode_Check(code)) { |
| 1299 | PyErr_BadInternalCall(); |
Brett Cannon | 3788b85 | 2016-09-07 12:51:08 -0700 | [diff] [blame] | 1300 | return -1; |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 1301 | } |
| 1302 | |
Brett Cannon | d0600ed | 2016-09-07 14:30:39 -0700 | [diff] [blame] | 1303 | PyCodeObject *o = (PyCodeObject*) code; |
| 1304 | _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra*) o->co_extra; |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 1305 | |
Brett Cannon | d0600ed | 2016-09-07 14:30:39 -0700 | [diff] [blame] | 1306 | if (co_extra == NULL || co_extra->ce_size <= index) { |
Dino Viehland | f3cffd2 | 2017-06-21 14:44:36 -0700 | [diff] [blame] | 1307 | *extra = NULL; |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 1308 | return 0; |
| 1309 | } |
| 1310 | |
Brett Cannon | d0600ed | 2016-09-07 14:30:39 -0700 | [diff] [blame] | 1311 | *extra = co_extra->ce_extras[index]; |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 1312 | return 0; |
| 1313 | } |
| 1314 | |
| 1315 | |
| 1316 | int |
| 1317 | _PyCode_SetExtra(PyObject *code, Py_ssize_t index, void *extra) |
| 1318 | { |
Victor Stinner | 81a7be3 | 2020-04-14 15:14:01 +0200 | [diff] [blame] | 1319 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 1320 | |
| 1321 | if (!PyCode_Check(code) || index < 0 || |
Dino Viehland | f3cffd2 | 2017-06-21 14:44:36 -0700 | [diff] [blame] | 1322 | index >= interp->co_extra_user_count) { |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 1323 | PyErr_BadInternalCall(); |
Brett Cannon | 3788b85 | 2016-09-07 12:51:08 -0700 | [diff] [blame] | 1324 | return -1; |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 1325 | } |
| 1326 | |
Brett Cannon | d0600ed | 2016-09-07 14:30:39 -0700 | [diff] [blame] | 1327 | PyCodeObject *o = (PyCodeObject*) code; |
| 1328 | _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra *) o->co_extra; |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 1329 | |
Serhiy Storchaka | 378ebb6 | 2017-07-04 15:06:16 +0300 | [diff] [blame] | 1330 | if (co_extra == NULL || co_extra->ce_size <= index) { |
| 1331 | Py_ssize_t i = (co_extra == NULL ? 0 : co_extra->ce_size); |
| 1332 | co_extra = PyMem_Realloc( |
| 1333 | co_extra, |
| 1334 | sizeof(_PyCodeObjectExtra) + |
| 1335 | (interp->co_extra_user_count-1) * sizeof(void*)); |
Brian Coleman | 6a9122c | 2017-03-02 10:32:18 +0000 | [diff] [blame] | 1336 | if (co_extra == NULL) { |
Brett Cannon | 3788b85 | 2016-09-07 12:51:08 -0700 | [diff] [blame] | 1337 | return -1; |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 1338 | } |
Serhiy Storchaka | 378ebb6 | 2017-07-04 15:06:16 +0300 | [diff] [blame] | 1339 | for (; i < interp->co_extra_user_count; i++) { |
Brett Cannon | d0600ed | 2016-09-07 14:30:39 -0700 | [diff] [blame] | 1340 | co_extra->ce_extras[i] = NULL; |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 1341 | } |
Dino Viehland | f3cffd2 | 2017-06-21 14:44:36 -0700 | [diff] [blame] | 1342 | co_extra->ce_size = interp->co_extra_user_count; |
Serhiy Storchaka | 378ebb6 | 2017-07-04 15:06:16 +0300 | [diff] [blame] | 1343 | o->co_extra = co_extra; |
Dino Viehland | f3cffd2 | 2017-06-21 14:44:36 -0700 | [diff] [blame] | 1344 | } |
| 1345 | |
| 1346 | if (co_extra->ce_extras[index] != NULL) { |
| 1347 | freefunc free = interp->co_extra_freefuncs[index]; |
Dino Viehland | f3cffd2 | 2017-06-21 14:44:36 -0700 | [diff] [blame] | 1348 | if (free != NULL) { |
| 1349 | free(co_extra->ce_extras[index]); |
| 1350 | } |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 1351 | } |
| 1352 | |
Brett Cannon | d0600ed | 2016-09-07 14:30:39 -0700 | [diff] [blame] | 1353 | co_extra->ce_extras[index] = extra; |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 1354 | return 0; |
| 1355 | } |