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