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