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